Skip to content

Markdown render: use hex byte constants for box-drawing glyphs (fix ──── mojibake) - #157

Merged
FMXExpress merged 2 commits into
mainfrom
claude/utf8-bom-sweep-batch2
Jun 6, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/utf8-bom-sweep-batch2

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

TL;DR

Reported: ──── (U+2500 × 4) divider in the markdown renderer's fenced-code-block separator mojibakes to ──── under build-delphi.bat. Classic UTF-8 → CP1252 misinterpretation: E2 94 80 read byte-by-byte through CP1252 gives ─.

Why not the PR #152 preamble?

Reflexively, the fix should be the same BOM + {$IFDEF FPC}{$CODEPAGE UTF8}{$ENDIF} preamble that PR #152 used. I tried it. It breaks Linux+FPC.

With the preamble, FPC stores '•' / '│' / '─' literals as AnsiString CP_UTF8. Mixed-codepage concatenation with Ansi.* fields then triggers an implicit UnicodeString round-trip, and the final cast back to AnsiString drops U+2022 to ?. Confirmed by byte-level dump — bullet output became ? (0x3F) after applying the preamble. The test even failed:

[1;38;2;62;93;185m?[0m one
[1;38;2;62;93;185m?[0m two
[1;38;2;62;93;185m?[0m three

The fix

Hex byte constants instead of UTF-8 string literals:

const
  GLYPH_BULLET  = #$E2#$80#$A2;   { U+2022 BULLET         }
  GLYPH_VBAR    = #$E2#$94#$82;   { U+2502 BOX VERTICAL   }
  GLYPH_HBAR    = #$E2#$94#$80;   { U+2500 BOX HORIZONTAL }
  GLYPH_DIVIDER = GLYPH_HBAR + GLYPH_HBAR + GLYPH_HBAR + GLYPH_HBAR;

#$XX constants are raw bytes — neither Delphi nor FPC tries to transcode them through the source-file codepage. The bytes go straight into the AnsiString and straight out to the terminal, which is UTF-8 aware.

Verified

  • make test-markdown-render — all 12 cases pass (was breaking on TestBulletList with the preamble approach)
  • make smoke — all 11 commands OK
  • Direct byte dump on the fenced-code divider produces e2 94 80 e2 94 80 e2 94 80 e2 94 80 — four correct UTF-8 horizontals
  • Live on Windows (build-delphi.bat): rebuild and pasclaw agent -m "give me a fenced code block in markdown" — divider should render as ────, no ─ mojibake.

Scope

Single-file change. The PR #152 preamble pattern is still correct for the files that use it (their non-ASCII string content is plain é / … / ✓ which goes through codepage transcoding cleanly). The markdown renderer's Ansi.X + '─' + Ansi.Reset triple-codepage concat is where the preamble trips. If any other file mojibakes a glyph at runtime under build-delphi.bat, the same byte-constant approach applies there.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

User report: \`────\` (U+2500 x 4) divider in fenced code-block output
mojibakes to \`────\` under build-delphi.bat. Classic UTF-8 -> CP1252
misinterpretation:

  U+2500 in UTF-8 is E2 94 80. Read as CP1252:
    E2 -> â
    94 -> ”
    80 -> €

The reflexive fix (UTF-8 BOM + {\$CODEPAGE UTF8} preamble like
PR #152 used) breaks Linux+FPC: with the directive, '•' / '│' /
'─' literals get stored as AnsiString CP_UTF8, then mixed-codepage
concatenation with Ansi.* fields triggers an implicit
UnicodeString round-trip, and the final cast back to AnsiString
drops U+2022 to '?'. Confirmed by direct byte dump — bullet output
became '?' (0x3F) after applying the preamble.

Switch to hex byte constants instead:

  GLYPH_BULLET  = #\$E2#\$80#\$A2;   { U+2022 BULLET         }
  GLYPH_VBAR    = #\$E2#\$94#\$82;   { U+2502 BOX VERTICAL   }
  GLYPH_HBAR    = #\$E2#\$94#\$80;   { U+2500 BOX HORIZONTAL }
  GLYPH_DIVIDER = GLYPH_HBAR x 4;

\`#\$XX\` constants are raw bytes — neither Delphi nor FPC tries to
transcode them through the source-file codepage. The bytes go
straight into the AnsiString and straight out to the terminal,
which is UTF-8 aware.

Verified Linux+FPC:
  - make test-markdown-render -> all 12 cases OK (was breaking on
    TestBulletList with the preamble approach)
  - make smoke                -> all 11 commands OK
  - Byte-level check on fenced-code divider produces
    e2 94 80 e2 94 80 e2 94 80 e2 94 80 — four correct UTF-8 box
    horizontals.

Under Delphi: same byte sequence emitted by the same hex constants
— no source-file codepage involved.

Scoped to one file. The wider sweep (23 files identified with the
preamble pattern + non-ASCII literals) is the wrong target — same
mixed-codepage concat issue would surface there. If any of those
files mojibakes a glyph at runtime under build-delphi.bat, the
same byte-constant approach applies.

@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: c1ba333ba6

ℹ️ 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".

wire and an UTF-8-aware terminal renders them correctly. }
GLYPH_BULLET = #$E2#$80#$A2; { U+2022 BULLET }
GLYPH_VBAR = #$E2#$94#$82; { U+2502 BOX VERTICAL }
GLYPH_HBAR = #$E2#$94#$80; { U+2500 BOX HORIZONTAL }

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 Use a Delphi-safe glyph representation

In the Windows Delphi path inspected via build-delphi.bat (dcc64/MSBuild), string is Unicode and console output goes through PrintLn → WriteConsoleW(..., PWideChar(S), ...) in PasClaw.CliUI.pas, so #$E2#$94#$80 is not emitted as raw UTF-8 bytes; it becomes three UTF-16 code units (U+00E2/U+0094/U+0080). This means fenced-code separators still render as mojibake/control characters under the exact Delphi build this change is meant to fix; the same issue applies to the bullet and vertical-bar constants above.

Useful? React with 👍 / 👎.

Codex P2 on PR #157: under Delphi `string` = `UnicodeString`, so
#$E2#$80#$A2 is three UTF-16 code units (U+00E2 / U+0094 / U+0080)
— `â` plus two C1 controls — not the UTF-8 bytes for U+2022. The
WriteConsoleW(PWideChar, Length) path in PasClaw.CliUI ships those
three wide chars straight to the console, producing mojibake under
build-delphi.bat (which is exactly what this PR was meant to fix).

Pick the literal form per compiler:
  FPC mode delphi → #$E2#$80#$A2  (raw UTF-8 bytes — AnsiString)
  Delphi          → #$2022        (single WideChar — UnicodeString)

WriteConsoleW renders #$2022 directly; TEncoding.UTF8.GetBytes
encodes it to the same 3-byte UTF-8 sequence for redirected stdout.
FPC's RTL writes the byte literal verbatim. No BOM, no codepage
directive, no mixed-codepage concat trap.

Tests pick the matching literal form so the assertion isn't
codepage-dependent. All 12 cases pass on Linux+FPC; byte-dump
confirms wire output unchanged (e2 80 a2 / e2 94 82 / e2 94 80 ×4).
@FMXExpress
FMXExpress merged commit 89b91e9 into main Jun 6, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
PasClaw.JSON.Parse routes through fpjson's GetJSON wrapper, which in
turn pipes the input into TJSONScanner with the `joUTF8` option set
(it's the default). Inside jsonreader.pp there is this branch:

  tkString : if (joUTF8 in Options) and (DefaultSystemCodePage<>CP_UTF8) then
               StringValue(TJSONStringType(UTF8Decode(CurrentTokenString)))

On a container locale with DefaultSystemCodePage=0 (any non-CP_UTF8
value triggers this), the round-trip UTF8Decode → TJSONStringType cast
silently re-encodes the parsed token through the system codepage. The
result is an AnsiString whose tag is CP_ACP rather than CP_UTF8, and
every codepoint the system CP can't represent gets clipped to a
single trailing byte (or replaced with '?' on a strict-ASCII
fallback). Wire-visible mojibake:

  ¶  UTF-8 C2 B6 → stored as bare B6 → GetStr returns 1 byte
  é  UTF-8 C3 A9 → stored as bare A9 → GetStr returns 1 byte
  中 UTF-8 E4 B8 AD → stored as bare AD → GetStr returns 1 byte
  •  UTF-8 E2 80 A2 → stored as bare A2 → GetStr returns 1 byte

This is the bug surfaced by toolview_tests.TestEditHashlineCall
(the hashline patch header `¶path` lost its lead byte in transit,
producing "fs_edit_hashline(<B6>src/foo.pas)" downstream) and lurks
across every PasClaw boundary that round-trips non-ASCII through
JSON: provider responses (assistant content with em-dashes, smart
quotes, accents), MCP tool I/O, session persistence, gateway
SSE deltas, channel inbound payloads.

Fix: bypass GetJSON entirely. Construct a TStringStream over the
input bytes and drive TJSONParser directly with EMPTY options —
specifically, NO joUTF8. The scanner's string-token path is then a
plain `Move` of the raw bytes into the token AnsiString
(jsonscanner.pp:405), no UTF-8 round-trip happens, and the bytes
PasClaw passed in are the bytes fpjson stores and AsString returns.
Applied to both TJsonObject.Parse and TJsonArray.Parse.

PasClaw treats every string at every boundary as UTF-8 already
(HTTP via Indy + TagUTF8 at PR FMXExpress#149, file IO via TEncoding.UTF8 +
TagUTF8 in ReadFileText, stdout via PasClaw.CliUI.Write on a
UTF-8 terminal). The fpjson "UseUTF8 decode" pass was a redundant
extra round-trip that only served to corrupt them.

Symmetric Add/PutStr behaviour is unchanged because the write side
never went through the lossy branch — fpjson's AnsiString Add just
stores the bytes passed in, and the tag survives the trip through
AsJSON.

Regression coverage in src/tests/json_utf8_roundtrip_tests.pas:
  - U+00B6 (¶, 2-byte UTF-8) round-trip through Object.GetStr
  - U+00E9 (é, 2-byte UTF-8) round-trip
  - U+4E2D + U+2022 (3-byte UTF-8 sequences) round-trip
  - Plain ASCII round-trip (regression guard)
  - JSON escapes (\\" \\\\ \\n) still parsed correctly
  - TJsonArray.Parse + ItemStr symmetric coverage
All 6 pass.

Wired into `make test` as `test-json-utf8-roundtrip`. The previously-
failing toolview TestEditHashlineCall now passes; the separate
TestArgTruncation failure is a distinct ToolView ellipsis/glyph
mojibake (mixed-codepage concat trap, same class as PR FMXExpress#157), not
addressed here.
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
TestArgTruncation has been red since the unit landed: the `'…'`
literal in Ellipsize and the `'✗'` literal in FormatToolResultLine
get clipped to `?` when concatenated with strings carrying a
different codepage tag. ToolView opens with `{$CODEPAGE UTF8}` so
its literals are tagged CP_UTF8, but JSON-parsed Summary strings
arrive tagged CP_0 (system default); the mixed-codepage concat trap
strips every UTF-8 lead byte that the destination CP can't
represent.

Same class of bug PR FMXExpress#157 fixed for PasClaw.Markdown.Render's
box-drawing glyphs. Same fix shape: replace the literals with
per-compiler hex byte constants that are codepage-agnostic.

  FPC mode delphi (string = AnsiString, Char = AnsiChar)
    raw UTF-8 byte sequence — bytes go on the wire as-is, no
    transcode triggers.

  Delphi (string = UnicodeString, Char = WideChar)
    single WideChar at the Unicode codepoint — WriteConsoleW
    renders it directly, TEncoding.UTF8 encodes it to the same
    UTF-8 bytes for redirected stdout.

Glyph table:
  TV_CALL_GLYPH    U+23FA ⏺  E2 8F BA
  TV_RESULT_GLYPH  U+23BF ⎿  E2 8E BF
  TV_ELLIPSIS      U+2026 …  E2 80 A6
  TV_ERROR_GLYPH   U+2717 ✗  E2 9C 97

Promoted the inline `'…'` and `'✗'` literals to named constants
alongside the existing TV_CALL_GLYPH / TV_RESULT_GLYPH so the four
codepoints live in one place and the compiler-split guards aren't
sprinkled through the call sites.

toolview_tests.TestArgTruncation and TestResultError pick their
assertion literal the same way — IFDEF FPC chooses the 3-byte UTF-8
form, ELSE the single WideChar — so the test isn't tied to a
codepage tag either.

Byte-dump on Linux+FPC confirms the wire output is exactly
E2 8F BA / E2 80 A6 / E2 8E BF / E2 9C 97 — no mojibake.

Full make test on Linux/FPC is now ALL GREEN for the first time:
smoke, hashline, toolview, anthropic-server-tools,
openai-server-tools, println-helper, utf8-codepage-tag,
gemini-schema-strip, markdown-render, json-utf8-roundtrip.
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