Skip to content

CliUI: UTF-8 BOM + FPC $CODEPAGE for banner mojibake - #151

Merged
FMXExpress merged 1 commit into
mainfrom
claude/cliui-bom-and-codepage
Jun 6, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/cliui-bom-and-codepage

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Why this is different from the reverted #149

PR #149 used {$IFDEF FPC}{$CODEPAGE UTF8}{$ENDIF} alone. That's a no-op for Delphi — dcc64 has no $CODEPAGE directive (it errors with E1030 if it sees one). Delphi still read CliUI.pas as the current ANSI codepage (CP1252 on English Windows) when there was no BOM, turning each UTF-8 byte of █ (E2 96 88) into 3 separate CP1252-mapped UnicodeChars (â–ˆ). Hence the banner kept mojibaking with build-delphi.bat.

Why banner_in_demo_app.dpr worked under Delphi but PasClaw didn't

Best guess: the user's editor / IDE added a UTF-8 BOM to the standalone on save (the file I sent had no BOM — verified). On their disk, the standalone became BOM-ful, PasClaw stayed BOM-less, and modern Delphi reads the BOM-ful one as UTF-8 and the BOM-less one as CP1252.

Fix

UTF-8 BOM on CliUI.pas. Modern Delphi (XE+) auto-detects the BOM and reads the source as UTF-8 regardless of project setting. msbuild + .dproj honours it the same as direct dcc64.

Kept {$IFDEF FPC}{$CODEPAGE UTF8}{$ENDIF} so FPC stores literals as AnsiString CP_UTF8 — without the directive, BOM alone makes FPC store them as UnicodeString, which produces 6 "AnsiString → UnicodeString" warnings per banner line on the PrintLn(Ansi.X + L + Ansi.Reset) concatenation. $WARN IMPLICIT_STRING_CAST OFF / IMPLICIT_STRING_CAST_LOSS OFF silence the remaining noise from Ansi.* fields (which carry the unit's default codepage tag) concatenating with the now-CP_UTF8 literals.

Verified

  • FPC Linux build clean (501 lines, 0 new CliUI warnings)
  • Banner bytes round-trip: e2 96 88 e2 96 88 ... (U+2588 FULL BLOCK)
  • Delphi Windows build via build-delphi.bat — you (only you can do this)

Scope

Just CliUI.pas. Other source files have non-ASCII literals (✓ markers, ╔ / ╗ panel chars, é in comments), and if those mojibake on Windows we can sweep them with the same 4-line preamble. I'm doing one-file-at-a-time so we can verify the approach before going broad.

If the banner still mojibakes after this

The byte-level cause was something else, and we need to dig further:

  • check pasclaw.exe's embedded resources for source-encoding hints
  • check whether WriteConsoleW is actually being called (vs. the WriteFile fallback path)
  • run pasclaw version 2>&1 | certutil -encodehex - and share the hex dump so I can read the actual bytes coming out of your build

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

The reverted PR #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.
@FMXExpress
FMXExpress merged commit 87de02d into main Jun 6, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jun 6, 2026
Apply the same UTF-8 BOM + {\$IFDEF FPC}{\$CODEPAGE UTF8}{\$ENDIF}
preamble that fixed the PASCLAW banner (PR FMXExpress#151) to every other
user-facing source file with non-ASCII bytes inside string literals.

Scope: 37 .pas files across cmd/, channels/, gateway/, mcp/, skills/,
tools/, tui/. Identified by grep'ing for "'[^']*[^\\x00-\\x7F][^']*'"
(non-ASCII byte inside single-quoted text) and filtering to the
directories that emit text to the user (CLI output, TUI, gateway
responses, channel-bot replies, tool error/status messages).

Pure-comment-only non-ASCII (em-dash in doc blocks, e.g. some lib
units like PasClaw.JSON.pas, PasClaw.Identity.pas) was excluded —
those bytes never reach a user-visible surface.

Preamble per file (identical to CliUI.pas from PR FMXExpress#151):

  {\$IFDEF FPC}
    {\$CODEPAGE UTF8}
    {\$WARN IMPLICIT_STRING_CAST OFF}
    {\$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  {\$ENDIF}

Plus a UTF-8 BOM (EF BB BF) at byte 0 of each file. Delphi
auto-detects the BOM and reads the source as UTF-8 regardless of
project setting — fixing the CP1252 misinterpretation that was
turning each UTF-8 byte of ✓ (E2 9C 93) or ╗ (E2 95 97) into three
separate CP1252 codepoints under build-delphi.bat. The FPC-only
block keeps literals stored as AnsiString CP_UTF8 (so Print/PrintLn
emits the original bytes unchanged on the wire) and silences the
6-warnings-per-line AnsiString↔UnicodeString conversion noise that
the BOM alone produces under FPC mode delphi.

Verified Linux+FPC:
  - Full rebuild clean (18705 lines, 0 new warnings on swept files)
  - make smoke — all 11 commands OK
  - Banner bytes still e2 96 88 ... for U+2588 (no regression)

Files swept (37):
  src/cmd/PasClaw.Cmd.{Agent,Auth,Cron,Gateway,MCP,Model,Onboard,
                       Post,Root,Serve,Session,Skills,Steer,Update,
                       Vault}.pas
  src/pkg/channels/PasClaw.Channels.{Email,IRC,LINE,Matrix,Telegram,
                                      WhatsApp}.pas
  src/pkg/gateway/PasClaw.Gateway.{Server,ToolView}.pas
  src/pkg/mcp/PasClaw.MCP.{Bridge,Catalog,Hub,OAuth}.pas
  src/pkg/skills/PasClaw.Skills.{ClawHub,GitHub,PasClawHub}.pas
  src/pkg/tools/PasClaw.Tools.{FS,Memory,Sandbox,ToolLoop,Vault,
                                WebFetch}.pas
  src/pkg/tui/PasClaw.TUI.pas

Pattern is mechanical: one canonical preamble per file. Diff is
large but each file's change is the same six lines.
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