Skip to content

Fix banner mojibake: tag CliUI source as UTF-8 ({$CODEPAGE UTF8}) - #149

Merged
FMXExpress merged 1 commit into
mainfrom
claude/banner-codepage-utf8
Jun 6, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/banner-codepage-utf8

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

You were right — PRs #147 and #148 fixed adjacent symptoms, not the banner. The PASCLAW box-drawing banner lives in a single file (src/pkg/cliui/PasClaw.CliUI.pas) and doesn't touch HTTP, JSON, or LLM responses. The mojibake there points at compile-time source-file encoding, not runtime output.

Diagnosis

Without {$CODEPAGE UTF8}, FPC reads source bytes in the system default codepage.

  • Linux: system codepage = UTF-8. Works by accident — the literal stores the raw UTF-8 bytes as CP_0-tagged AnsiString and the terminal interprets them as UTF-8.
  • Windows: system codepage = CP1252. Each byte of █ (E2 96 88) becomes a separate AnsiChar tagged CP1252. At output time the runtime transcodes CP1252 → UTF-8 and double-encodes every glyph. Classic banner mojibake.

{$CODEPAGE UTF8} tells FPC: the bytes in this file are UTF-8. Literals are stored verbatim and tagged CP_UTF8. Output transcodes CP_UTF8 → CP_UTF8 (no-op), so what arrives at the terminal is exactly what's in the source. Delphi modern (XE+) accepts the same directive identically.

Why only one file

Only CliUI.pas has non-ASCII string literals that flow through PrintLn (banner ██╔═, panel chars ┌─┐). If more non-ASCII glyphs ever migrate into other surfaces (Onboard ✓, Vault search panels), the directive lands there too — same one-line change.

Test plan

  • make — 1 note, 0 new warnings on CliUI (the directive's implicit-cast warnings are silenced locally — see commit body for why)
  • Banner byte dump on Linux+FPC — e2 96 88 (correct UTF-8 for █), unchanged from before, confirms Linux didn't regress
  • make smoke — all 11 commands OK
  • Live verify on Windows: run pasclaw version (or anything that triggers PrintBanner) on cmd.exe / Windows Terminal under FPC build. Bytes coming out should be the same UTF-8 you'd see on Linux. The console renderer takes it from there.

Note on PRs #147 / #148

Those PRs addressed real issues elsewhere in the stack (Windows + Delphi WriteLn codepage trap in #147, FPC StringCodePage=0 after TStringStream.DataString in #148) but the banner wasn't going through either of those paths. This is the missing piece for that specific symptom.

If LLM-response mojibake is still happening after merging this, that's a separate path — likely still in the byte-stream boundary territory of #148, but possibly somewhere else (channel writers, fpjson serialise-back-out). Happy to dig further once you confirm the banner is fixed.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

You were right — PRs #147 and #148 fixed adjacent symptoms, not the
banner. The PASCLAW box-drawing banner lives in a single file
(PasClaw.CliUI.pas) and doesn't touch HTTP, JSON, or LLM responses,
so the mojibake there points at compile-time source-file encoding,
not runtime output.

Without `{$CODEPAGE UTF8}`, FPC reads the source bytes in the system
default codepage. On Linux that's UTF-8 (works by accident — the
literal stores the raw UTF-8 bytes as CP_0-tagged AnsiString and the
terminal interprets them as UTF-8). On Windows the system codepage
is CP1252; each byte of `█` (`E2 96 88`) becomes a separate AnsiChar
tagged CP1252, and at output time the runtime transcodes CP1252 ->
UTF-8, double-encoding every glyph. Classic banner mojibake.

`{$CODEPAGE UTF8}` tells FPC: the bytes in this file ARE UTF-8.
Literals are stored verbatim and tagged CP_UTF8. Output transcodes
CP_UTF8 -> CP_UTF8 = nothing, so the bytes that arrive at the
terminal are exactly the bytes in the source. Delphi modern (XE+)
accepts the same directive.

Only CliUI gets the directive because only CliUI has non-ASCII
string literals that flow through `PrintLn`. If more non-ASCII
glyphs migrate into other surfaces (Onboard ✓, Vault search panel
chars), the directive should land there too.

Build verification (Linux + FPC 3.2.2):
  make                              -> 1 note, 0 warnings on CliUI
  banner-byte dump                  -> e2 96 88 e2 96 88 ... (correct UTF-8
                                       for ██ — unchanged from before,
                                       confirming Linux didn't regress)
  make smoke                        -> all 11 commands OK

`{$WARN IMPLICIT_STRING_CAST OFF}` + `_LOSS OFF` silence 17 false-
positive FPC warnings the directive introduces on
`PrintLn(Ansi.BoldBlue + L1 + Ansi.Reset)` — `Ansi.*` fields are
AnsiString CP_0, the L1 literal is now AnsiString CP_UTF8, FPC
promotes to UnicodeString to concat, then back to AnsiString for the
parameter. Bytes are correct throughout; the warnings are noise.

Live-verify-on-Windows still required (no CI for that path).
@FMXExpress
FMXExpress merged commit 8f91b67 into main Jun 6, 2026
FMXExpress pushed a commit that referenced this pull request Jun 6, 2026
dcc64 errors on the directive — E1030 "Invalid compiler directive:
'CODEPAGE'". The directive is FPC-only; Delphi has no equivalent
in the standard directive set (source codepage is project-level,
typically driven by a UTF-8 BOM or the Source File Format project
option). Wrapping in {$IFDEF FPC} keeps the FPC fix while letting
dcc64 build clean.

If the Delphi build of PasClaw still mojibakes the banner after
this lands, the next step is verifying CliUI.pas has a UTF-8 BOM
(or the .dproj has the right source-codepage setting). I've sent a
standalone reproducer (banner_in_demo_app.dpr) that drops the
banner into the user's UTF8ConsoleDemo harness — running it on
Windows isolates whether the source encoding fix is correct in
isolation, separate from PasClaw's other moving parts.
FMXExpress pushed a commit that referenced this pull request Jun 6, 2026
Single-file Delphi/FPC console program that drops the PASCLAW
box-drawing banner into the user's UTF8ConsoleDemo harness:

  - Same ConWrite / ConWriteLn pattern (WriteConsoleW on Windows,
    UTF-8 bytes via WriteFile when redirected, plain Write on POSIX).
  - Same SetConsoleOutputCP(CP_UTF8) init.
  - {$IFDEF FPC}{$CODEPAGE UTF8}{$ENDIF} — the directive we're
    testing — wrapped so dcc64 doesn't error.
  - The L1..L6 banner literals copy-pasted verbatim from
    PasClaw.CliUI.PrintBanner.

Build:
  fpc -MDelphi banner_in_demo_app.dpr     (verified clean on Linux)
  dcc64 banner_in_demo_app.dpr            (for the user to verify on Windows)

Lets the user isolate the source-encoding fix from PasClaw's other
moving parts. If the banner renders as boxes here, the directive +
project-codepage settings are correct and PR #149 will fix the real
banner. If it mojibakes here too, the source bytes are getting
clobbered before the compiler sees them (editor save, line endings,
.dproj source-codepage default) and we need to add a UTF-8 BOM to
the .pas file as well.

Doesn't affect the regular `make` build — separate .dpr program,
not wired into any test target.
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jun 6, 2026
The reverted PR FMXExpress#149 ($CODEPAGE UTF8 directive alone) didn't fix
the Delphi build because Delphi has no $CODEPAGE directive — dcc64
errors with E1030 on it, and even when wrapped in $IFDEF FPC, the
directive is a no-op for Delphi. PasClaw still mojibaked under
build-delphi.bat because msbuild + .dproj reads .pas source as the
current ANSI codepage (CP1252 on English Windows) when there's no
BOM, turning each UTF-8 byte of █ (E2 96 88) into 3 separate
CP1252-mapped UnicodeChars (â–ˆ), which is what shows on the
terminal.

User-confirmed evidence: a standalone banner_in_demo_app.dpr with
the same banner literal renders correctly under Delphi, while
PasClaw mojibakes with or without --no-color. Most likely the
standalone got a UTF-8 BOM added by the user's editor / IDE on
save (the file I sent was BOM-less), so on their disk the standalone
was BOM-ful and PasClaw wasn't.

Fix: add a UTF-8 BOM to PasClaw.CliUI.pas. Modern Delphi
auto-detects BOM and reads source as UTF-8 regardless of project
setting. The {$IFDEF FPC}{$CODEPAGE UTF8}{$ENDIF} block is retained
so FPC stores literals as AnsiString CP_UTF8 (without it, the BOM
alone makes FPC store them as UnicodeString and produces 6
"AnsiString -> UnicodeString" warnings per banner line on the
PrintLn(Ansi.X + L + Ansi.Reset) concat). $WARN
IMPLICIT_STRING_CAST OFF / IMPLICIT_STRING_CAST_LOSS OFF silence
the remaining noise from the Ansi.* fields (which carry the unit's
default codepage tag) concatenating with the now-CP_UTF8 literals.

Linux+FPC build clean (501 lines compiled, 0 new warnings on
CliUI). Banner bytes round-trip correctly:
  e2 96 88 e2 96 88 e2 96 88 ...   { U+2588 FULL BLOCK x N }

Scope is intentionally just CliUI.pas. Other source files have
non-ASCII literals (✓ markers, ╔/╗ panel chars, é in some comments),
and if those mojibake on Windows we can sweep them with the same
4-line preamble. Better to verify the approach on the one file the
user can see broken first.
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.
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