Skip to content

fix(tool): avoid UTF-8 misdetection at encoding sample boundaries - #8322

Open
VirgilMing wants to merge 2 commits into
esengine:main-v2from
VirgilMing:codex/fix-tool-encoding-boundaries
Open

fix(tool): avoid UTF-8 misdetection at encoding sample boundaries#8322
VirgilMing wants to merge 2 commits into
esengine:main-v2from
VirgilMing:codex/fix-tool-encoding-boundaries

Conversation

@VirgilMing

@VirgilMing VirgilMing commented Aug 11, 2026

Copy link
Copy Markdown

Summary

  • make native grep and read_file detect encodings from a character-safe bounded sample when the read boundary splits a multibyte sequence
  • prefer strict UTF-8 before the GB18030 fallback without discarding the non-ASCII evidence needed to recognize GB18030
  • add regression coverage for valid UTF-8 Chinese files and the inverse ASCII-header plus long-GB18030-line case

Issues

Fixes #8299

Related: #8321 is a viable alternative implementation; its head also passes this PR's two inverse ASCII-header plus long-GB18030 cases, so the overlapping implementations remain separate pending maintainer direction.

Verification

  • go test -count=1 -run '^(TestGrepUTF8ChinesePeekBoundary|TestGrepUTF8ChineseLongSingleLine|TestGrepGB18030LongLineAfterASCIIHeader|TestReadFileUTF8ChineseLongSingleLine|TestReadFileGB18030LongLineAfterASCIIHeader|TestGrepGB18030|TestGrepGB18030TruncationDoesNotLeakGoroutine|TestReadFileGB18030|TestReadFileStreamsLargeGB18030)$' ./internal/tool/builtin
  • go test -count=1 ./internal/fileutil/encoding
  • go vet ./internal/tool/builtin ./internal/fileutil/encoding

Known unrelated Windows baseline failures:

  • go test -count=1 ./internal/tool/builtin: TestBackgroundBashWaitAndOutput, TestBashTerminalFallsBackWhenUnhandled, and TestBashTerminalSkippedWhenEnvFilteringEnabled invoke POSIX printf after falling back to PowerShell
  • go test -count=1 ./internal/boot: TestGoldenBaselineNoExtensions is shell-environment-dependent on Windows: it fails when shell auto-resolution falls back to PowerShell and passes unchanged when Bash is available; this change does not modify any tool name, description, or schema
  • the broader split run also exposed existing no-Git-Bash assumptions in internal/cli, internal/hook, and internal/shellrun, plus TestStreamSurfacesEarlyConnResetAsInterrupt timing out in internal/provider/openai; all other packages in that split passed

Documentation impact

Documentation-impact: none - the documented tool contract is unchanged; this corrects encoding detection behind the existing behavior.

Cache impact

Cache-impact: none - tool names, descriptions, schemas, and provider-visible prompt content are unchanged.
Cache-guard: focused encoding regression tests plus a diff review of the tool contract methods.
System-prompt-review: N/A

…undary

The native grep scanner detected encoding from an 8 KiB peek. When it ends mid UTF-8 sequence, the truncated prefix may be accepted as GB18030, so Chinese patterns miss and ASCII matches return mojibake.

Select a character-safe detection prefix by preferring strict UTF-8 after removing at most a maximum-width tail, then retaining a prefix that is demonstrably GB18030. This fixes the reported UTF-8 failure without regressing ASCII headers followed by long GB18030 lines.
…GB18030

A no-newline 256 KiB sample ending mid multi-byte sequence was passed to
fileenc.Detect untrimmed; the truncated UTF-8 tail is "valid" GB18030, so
read_file decoded the whole stream into mojibake. Route the sample through
the same detectSample char-boundary trim grep uses, closing the last
encoding-detection gap both tools shared.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skills Skill system (internal/skill, internal/tool) v2 Go rewrite (1.x) — main-v2 branch, active development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 内置grep不能正确处理部分中西混合文件编码问题

1 participant