Skip to content

Hashline: fix fs_read header mojibake on Windows (¶ → ¶) - #238

Merged
FMXExpress merged 2 commits into
mainfrom
claude/hashline-fs-read-utf8-pilcrow
Jun 12, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/hashline-fs-read-utf8-pilcrow

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Fixes the ¶C:\... mojibake Eli hit in fs_read output on Windows. Three-part fix:

Root cause

PasClaw.Hashline.pas had HL_FILE_PREFIX = '¶' (and similar for '↑'/'↓') as bare character literals in the const block. Without {$CODEPAGE UTF8} on the unit:

  • Linux (FPC default reads source as UTF-8): bytes interpreted correctly, tests passed.
  • Windows (FPC default reads source as CP1252): UTF-8 bytes 0xC2 0xB6 got read as two Latin-1 chars. Even when the byte values were preserved correctly, the resulting AnsiString carried no codepage tag — when FormatHashlineHeader's result hit the JSON serialiser, the bytes got re-encoded as if they were CP1252 characters, producing ¶ (UTF-8 0xC3 0x82 0xC2 0xB6).

Why the naive fix didn't work

Adding {$CODEPAGE UTF8} alone broke the parser: FPC under that directive promotes a bare const X = 'literal' to a single-codepoint UnicodeString-flavoured value. Length() returns 1 codepoint instead of 3 UTF-8 bytes, and the validator's byte-arithmetic Copy(L, p+1, Length(HL_PAYLOAD_BELOW)) captures only the first byte. The validator silently stopped flagging 60:↓bad inline payload as invalid.

Three-part fix

  1. {$CODEPAGE UTF8} on the unit so concatenation results in FormatHashlineHeader carry CP_UTF8 instead of defaulting to system ANSI when reaching the JSON serialiser.
  2. Byte-literal constants on the FPC side to sidestep the directive's literal-promotion gotcha:
    HL_FILE_PREFIX:   AnsiString = #$C2#$B6;
    HL_PAYLOAD_ABOVE: AnsiString = #$E2#$86#$91;
    HL_PAYLOAD_BELOW: AnsiString = #$E2#$86#$93;
    Unit initialization SetCodePages each to CP_UTF8 so they identify as UTF-8 to downstream code.
  3. Explicit SetCodePage(RawByteString(Result), CP_UTF8, False) in FormatHashlineHeader because concatenation across mixed-CP operands produces CP=0 ("use default") even when the bytes are valid UTF-8 — explicitly stamping the tag is the only way to make the JSON serialiser pass bytes through verbatim on Windows.

Delphi build untouched (Delphi reads source as UTF-16 already; everything's {$IFDEF FPC}-guarded).

Test plan

  • FPC make smoke clean.
  • Full make test green (36 targets).
  • Probe shows FormatHashlineHeader now returns CP=65001 with bytes 0xC2 0xB6 ... (was CP=0 before fix).
  • Windows manual: rebuild, run any fs_read — header should show ¶C:\path not ¶C:\path.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

The literal '¶' for HL_FILE_PREFIX (and '↑'/'↓' for the payload
markers) lived as a bare character literal in the unit's const
block. Without {\$CODEPAGE UTF8} on the unit, FPC reads the .pas
source under the default codepage -- UTF-8 on Linux (so existing
tests passed) but CP1252 on Windows (so the UTF-8 bytes 0xC2 0xB6
got read as two Latin-1 chars). The bytes flowed through
FormatHashlineHeader into fs_read's tool result tagged as CP1252;
downstream UTF-8 re-encoding then promoted them to "¶..." which
is exactly the mojibake Eli hit.

Naive fix (adding the directive alone) regressed the parser: FPC
under {\$CODEPAGE UTF8} promotes a bare `const X = 'literal'` to a
single-codepoint UnicodeString-flavoured value -- Length() returns
1 codepoint instead of 3 UTF-8 bytes, and the validator's
byte-arithmetic Copy(L, p+1, Length(HL_PAYLOAD_BELOW)) captures
only the first byte and the equality check against the real 3-byte
sequence always fails. The validator stopped flagging
"60:↓bad inline payload" as invalid.

Proper fix has two parts:

  1. {\$CODEPAGE UTF8} on the unit so concatenation results
     (FormatHashlineHeader's HL_FILE_PREFIX + FilePath + ...)
     carry CP_UTF8 instead of defaulting to the system ANSI CP
     when reaching the JSON serialiser.

  2. The special-char constants spelled as raw byte literals on
     the FPC side:
       HL_FILE_PREFIX:   AnsiString = #\$C2#\$B6;
       HL_PAYLOAD_ABOVE: AnsiString = #\$E2#\$86#\$91;
       HL_PAYLOAD_BELOW: AnsiString = #\$E2#\$86#\$93;
     Bypasses the source-codepage interpretation entirely. Unit
     initialization SetCodePages each to CP_UTF8 so they identify
     as UTF-8 to downstream code that consults StringCodePage.

  3. FormatHashlineHeader's result gets an explicit SetCodePage
     to CP_UTF8 because string concatenation across mixed-CP
     operands produces CP=0 ("use default") even when the bytes
     are valid UTF-8 -- explicitly stamping the tag is the only
     way to make the JSON serialiser pass the bytes through
     verbatim on Windows.

Delphi build untouched (Delphi reads source as UTF-16 already; the
{\$IFDEF FPC} guards everything).

FPC make smoke + full make test (36 targets) green. Eli's
mojibake should disappear on the next Windows rebuild -- fs_read
output should now show "¶C:\..." instead of "¶C:\...".

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 239dc0cda7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

that's how `¶` (0xC2 0xB6) flips to `¶` (0xC3 0x82 0xC2 0xB6)
in fs_read output. Stamp the result as CP_UTF8 so the
downstream serialiser passes the bytes through verbatim. }
SetCodePage(RawByteString(Result), CP_UTF8, False);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Tag the complete hashline output before JSON serialization

On Windows this only stamps the header returned by FormatHashlineHeader, but the actual fs_read result is built by FormatHashlineRead as FormatHashlineHeader(...) + #10 + FormatNumberedLines(...). Per the same code comment, concatenating a CP_UTF8 string with default-codepage operands can produce a CP=0 result, so the string that reaches TJsonObject.PutStr(...).AsJSON from Tool_FSRead/fs_grep can still be treated as CP1252 and re-encoded as ¶. The final concatenated tool output needs to be tagged (or built entirely as CP_UTF8), otherwise the Windows mojibake this commit targets can persist for normal hashline reads.

Useful? React with 👍 / 👎.

Codex P2, legit. My prior commit only tagged FormatHashlineHeader's
result. The real flow is:

  FormatHashlineRead = FormatHashlineHeader + #10 + FormatNumberedLines

and that concatenation across mixed-CP operands resets the result's
codepage tag back to 0 ("use DefaultSystemCodepage" -- CP1252 on
English Windows), so the bytes that reach PasClaw.JSON.PutStr from
Tool_FSRead can still be re-encoded as if they were CP1252 chars.
The ¶ -> ¶ mojibake the PR targets would persist for normal
fs_read calls.

Three additional SetCodePage stamps so the full output reaches the
JSON serialiser with the UTF-8 tag:

  - FormatHashlineRead: the final concat result (the actual string
    Tool_FSRead returns).
  - FormatNumberedLines: TStringBuilder.ToString resets the tag
    regardless of what the appended pieces carried; stamping here
    covers fs_grep's outer builder which appends section bodies.
  - PasClaw.Tools.FS Tool_FSGrep: the outer TStringBuilder.ToString
    that concatenates per-file hashline sections also needs the
    stamp on its return path.

FPC make smoke + full make test (36 targets) green. Probe confirms
FormatHashlineRead now returns CP=65001 with bytes 0xC2 0xB6 ...
(was CP=0 before this commit; only the header was tagged correctly).

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
@FMXExpress
FMXExpress merged commit 31cbf14 into main Jun 12, 2026
FMXExpress pushed a commit that referenced this pull request Jun 13, 2026
Both changelog files were stuck at the 2026-06-10 TUI / background-
subagent / session_search / tool-RPC entries. ~7 weeks of merged
PRs (#214 through #244) had landed without a corresponding entry in
either file -- the git log was the only record of features like the
knowledgebase, inbound MCP server mode, checkpoints + /undo,
heartbeat daemon, shell backends (local | docker), send_message,
promptware defense, Windows shell-output encoding fixes, fs_grep
ripgrep optimisations, OpenTelemetry traces, and `pasclaw agent
--quiet`.

Added (newest first):

  2026-06-13
    #244  agent --quiet log-level clamp

  2026-06-12
    #243  dcc64 search path + agent --quiet / -q
    #242  OpenTelemetry traces (OTLP/HTTP+JSON)
    #241  fs_grep tier 5+6 (byte walker + Boyer-Moore-Horspool)
    #240  fs_grep tier 1-4 (deferred hash + skip-dirs + binary
          sniff + size cap)

  2026-06-11
    #239  Windows shell output: auto-detect UTF-8 vs OEM
    #238  Hashline fs_read header mojibake fix (¶ vs ¶)
    #237  Windows shell output: OEM codepage decode

  2026-06-10
    #233  Shell backends (local | docker via IShellBackend)
    #232  Heartbeat daemon + reversible condensation (CCR)
    #230  send_message tool + condenser families + promptware
          defense + task-aware MEMORY.md slicing

  2026-06-09
    #223  /goal Ralph loop + judge model + JSON-aware condenser
    #222  /undo wired into agent + sample dprojs
    #221  Checkpoints (auto-snapshot + TUI /undo)
    #220  mcp catalog: lenient hub projector
    #219  mcp: SSE / Streamable HTTP / stdio hub transports
    #218  Inbound MCP server mode
    #217  onboard: offer to index docs into the knowledgebase
    #214  Knowledgebase (RAG over operator-curated docs)

Skipped from this changelog (build-system / Delphi-fix / FPC-fix
PRs visible in the git log but not user-facing features):
#215, #216, #224, #225, #226, #227, #228, #229, #231, #234, #236.

Both files (README.md and docs/changelog.md) updated with the same
entries -- intentional mirror so a reader on either surface sees
the same history. Same prose in both; same footnote refs at the
bottom.

Validated: every [#N] reference in both files has a matching [#N]:
URL footnote (no broken links).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants