Skip to content

Guard FPC-only $MACRO block in MCP.Bridge for Delphi - #10

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr10-mcp-bridge-macro
May 27, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr10-mcp-bridge-macro

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Fixes [dcc64 Error] PasClaw.MCP.Bridge.pas(57): E1030 Invalid compiler directive: 'MACRO'.

{$MACRO ON} and {$DEFINE name := ...} are FPC-only directives — Delphi rejects them with E1030. The MAKE_HANDLER macro template is never actually expanded anyway; the H_0..H_31 functions below it are hand-written. Wrapping the block in {$IFDEF FPC} keeps it as documentary reference for FPC builds and hides it from Delphi.

Test plan

  • Build under Delphi Win64/Linux64: no E1030 on MCP.Bridge.pas
  • Build under FPC 3.2+: still compiles; macro block parses (even though unused)

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

$MACRO ON and $DEFINE name := ... are FPC directives; Delphi rejects
them with E1030 Invalid compiler directive: MACRO. The MAKE_HANDLER
template is never actually expanded — the H_0..H_31 functions below it
are written out by hand — so the block is documentary only. Wrap it in
an FPC IFDEF so Delphi skips it entirely.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
@FMXExpress
FMXExpress merged commit c3ed3d7 into main May 27, 2026
FMXExpress pushed a commit that referenced this pull request May 27, 2026
Two correctness bugs in the new SSE streamer:

1. SSE comment injection. TSSEStreamer.WriteComment emitted
   `: ' + Note + #10#10` verbatim. NoteToolCall and NoteToolResult
   pass the tool's argsJSON and the tool's ResultText (or the
   200-char preview) straight into the comment body. Any newline in
   that payload followed by `data: ...` or another SSE field caused
   the client to parse the rest as a real event — terminating the
   stream early or feeding bogus deltas into the chat. A shell tool
   that ran `printf '\ndata: [DONE]\n'` was enough to silently end
   the stream from a client's perspective.

   Strip CR and prefix every newline-separated line in the body with
   `: ` so the entire comment stays inside comment-line mode, then
   append the empty-line event terminator. The single-line common
   case is unchanged; multi-line and adversarial payloads are now
   safe.

2. Streaming RunToolLoop failure was misreported as success. The
   stream-path branch called `Streamer.Finalize('(tool loop failed)',
   'stop')`, writing that string as an assistant content delta with
   finish_reason=stop. Since the response headers had already gone
   out as 200 OK, the OpenAI-compatible client saw a perfectly normal
   completion containing the string "(tool loop failed)" and could
   feed that back into the next turn. Meanwhile the non-streaming
   path correctly returned HTTP 502.

   Add Streamer.WriteError which writes an OpenAI-style
   `data: {"error":{"message":...,"type":"server_error"}}` frame
   followed by `data: [DONE]`. Stream clients that recognize the
   error shape surface a real error; clients that don't at least
   see the failure status rather than a fake assistant turn. The
   non-streaming 502 path is unchanged.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
FMXExpress pushed a commit that referenced this pull request Jun 12, 2026
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 pushed a commit that referenced this pull request Jun 12, 2026
Two more cheap wins for fs_grep on top of the tier-1-4 filters
(deferred-hash / blocked-dirs / NUL-sniff / size-cap), this time
in the inner per-file path.

Tier 5 -- walk bytes, not lines. The old ScanFile did:
  - StringReplace(Body, #13, '', [rfReplaceAll])  -- whole-body copy
  - Lines := TStringList.Create / Lines.Text := ... -- one alloc per line
  - LowerCase(Lines[j])                            -- one per match-candidate

None of that's needed. The new walker splits on #10 in place, trims
trailing #13 inline for CRLF, and only Copy()s the line bytes on
lines that actually matched. ~3-5x faster on the per-file path for
typical source files.

Tier 6 -- Boyer-Moore-Horspool substring search. The old Pos(PatLower,
Cmp) advanced one byte at a time. BMH precomputes a 256-entry shift
table once at Tool_FSGrep startup and on a failed alignment slides
the pattern by up to m bytes instead of 1. For the patterns the model
actually queries (TODO, function, class, FIXME, def, return) this
hits ~3-10x fewer text-byte reads than Pos.

Case-insensitive is handled by folding text bytes through a 256-entry
ASCII lower table (built once per call) -- one branch instead of
the old per-line LowerCase(). Pattern is pre-lowered into PatLower
the same way as before.

Output is byte-identical to the old path except:
  - CRLF lines no longer carry a stray #13 in the emitted line text
    (the old StringReplace pass had hidden this; the byte walker
    now trims trailing #13 explicitly).
  - Lone mid-line #13 bytes (extremely rare; old Mac line endings)
    are no longer stripped from output. ripgrep treats them the same
    way -- they're not a line terminator in modern conventions.

Both behaviours are pinned by src/tests/fs_grep_tier5_6_tests.pas
(10 cases): line numbers exact across the walker, CRLF trimming
correct, files without trailing newline still produce a final-line
match, one hashline header per matched file, ignore_case round-trip
through the byte-fold table, BMH overlapping/partial/embedded
patterns, m=1 degenerate case, m > n early exit, empty / newline-
only bodies don't crash, and the hashline ¶path#hash header still
emits in the same shape so fs_edit_hashline downstream still parses
the output.
FMXExpress pushed a commit that referenced this pull request Jun 14, 2026
…iner

Reported (docker backend on Windows):
  execute_code bash 'pwd && ls -la' -> "ls: invalid option -- '<CR>'"
  execute_code bash 'find . -maxdepth 2' -> "not a positive integer ... '2<CR>'"

MakeTempScript writes the script via TStringList.SaveToFile, which uses
the host line ending -- CRLF on Windows. The script is materialised on the
Windows host (tmp is bind-mounted), then run by bash INSIDE the Linux
container, where the trailing CR becomes part of the last token on every
line ('ls -la'+CR, '-maxdepth 2'+CR) and commands fail.

Force LF (Sl.LineBreak := #10) regardless of host. LF is correct for bash
and accepted by PowerShell. No-op on Linux/macOS hosts (already LF), so
the fix is scoped to the Windows-host case.

Build clean; execute_code_tests pass.
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
…iner

Reported (docker backend on Windows):
  execute_code bash 'pwd && ls -la' -> "ls: invalid option -- '<CR>'"
  execute_code bash 'find . -maxdepth 2' -> "not a positive integer ... '2<CR>'"

MakeTempScript writes the script via TStringList.SaveToFile, which uses
the host line ending -- CRLF on Windows. The script is materialised on the
Windows host (tmp is bind-mounted), then run by bash INSIDE the Linux
container, where the trailing CR becomes part of the last token on every
line ('ls -la'+CR, '-maxdepth 2'+CR) and commands fail.

Force LF (Sl.LineBreak := FMXExpress#10) regardless of host. LF is correct for bash
and accepted by PowerShell. No-op on Linux/macOS hosts (already LF), so
the fix is scoped to the Windows-host case.

Build clean; execute_code_tests pass.
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