Repository navigation
tools: split "parallel-safe" out of tcReadOnly so writers stop sharing batches - #538
Conversation
…g batches
TToolCategory was answering two unrelated questions with one field.
ToolLoop read tcReadOnly as "safe to run concurrently" when partitioning a
round into parallel batches; the plan-mode gate and the read-only MCP
server read it as "does not really mutate, so allow it". Those disagree
for any tool that writes but is labelled tcReadOnly to pass the gate --
and re-categorising cannot fix it, because whichever meaning you correct
you break the other.
Auditing every tcReadOnly registration found three such tools, not one:
memory_search Tool_MemorySearch calls IMemoryIndex.SyncDir, which
reindexes changed files and drops rows for deleted ones
-- writes and commits against .index.db. Two searches in
one batch (two different queries is an ordinary ask) are
two writers on that file. Reported by Codex on PR #537.
plan_write writes workspace/PLAN.md; PasClaw.Tools.PlanWrite
documents choosing tcReadOnly precisely to pass the
pmPlan gate.
todo_write rewrites the checklist file, and its comment actively
claimed the parallel batch as a benefit.
TTool gains SerialOnly, and the batcher now requires BOTH properties:
tcReadOnly AND not SerialOnly. Category keeps answering the gate and MCP
questions unchanged, so plan mode and read-only exposure behave exactly as
before -- verified by test-plan-build-mode, test-guardfall,
test-progress-ledger and test-mcp-server.
Polarity is deliberate and is the one thing worth re-checking in review.
TTool records are built on the stack and this codebase does not FillChar
them (managed strings), so a site assigning fields one by one leaves a new
Boolean holding stack garbage. With SerialOnly, garbage-True costs a batch
slot. With the inverse field, garbage-True would batch a writer
concurrently -- uninitialised memory becoming a data race. The safe
failure had to be the default one.
PartitionToolBatches is exported for tests: which tools share a batch is a
correctness property, and asserting the flags that feed the decision is
weaker than asserting the decision. The new test does the latter -- three
reads coalesce into one batch, a tcReadOnly writer splits it into three
and sits alone. Confirmed it fails against the old predicate.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38e59bf09b
ℹ️ 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".
| if (Reg <> nil) and Reg.Find(Calls[i].Func.Name, T) | ||
| and (T.Category = tcReadOnly) and (not T.SerialOnly) then |
There was a problem hiding this comment.
Initialize SerialOnly on every registration path
When a read-only tool is registered through existing stack-built paths such as TPasClawTool.Install, RegisterWebSearchTool, or RegisterToolViaDispatch, the new SerialOnly field is never assigned. PartitionToolBatches now reads that indeterminate byte, so otherwise parallel-safe calls can unpredictably be split into serial batches, adding substantial latency for HTTP, database, and MCP tools. Polarity does not make an uninitialized Boolean a reliable default; initialize this field explicitly in all TTool construction paths (and cover a non-Default(TTool) registration in the batching test).
Useful? React with 👍 / 👎.
Codex is right, and the rebuttal lands on my own reasoning. I let registration sites assign SerialOnly and defended it with polarity: garbage-True merely costs a batch slot, where the inverse field would turn uninitialised memory into a data race. Safe is not the same as correct. About 34 sites build TTool as a bare stack record and assign fields one at a time -- none of them touch this one -- so the batcher would have been reading an indeterminate byte, and parallel-safe tools could have dropped out of batching randomly and silently. That is precisely the machinery this whole change exists to enable. RegisterImpl already carries this exact precedent one field over: it defensively clears HandlerObj because "legacy callers build T: TTool on the stack and set Handler without ever touching the new HandlerObj field, which leaves it pointing at stack garbage". Same bug class, same fix shape. SerialOnly is now derived there from ToolIsSerialOnly, ignoring whatever the incoming record held, so an unassigned field cannot decide whether a tool runs concurrently. Fixing the 34 sites instead was the other option and is worse: several are copy-then-modify (PasClaw.Tools.FS builds a back-compat alias from an already-populated record), where inserting Default(TTool) would erase the copy. A single derivation point also cannot rot the way 34 sites can, and it puts the whole set's concurrency properties in one greppable place. The list holds the three tools found by auditing every tcReadOnly registration: memory_search (SyncDir reindexes and deletes rows in .index.db), plan_write (writes PLAN.md), todo_write (rewrites the checklist). Per-site assignments are gone; each site now points at the list. New cases cover a non-Default(TTool) registration in both directions, as asked: a bare stack record arriving with a wrong-way SerialOnly is corrected on registration -- True cleared for an unlisted tool, False forced True for a listed one -- and the resulting batching is asserted, not just the flag. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
The week's agent-optimization work (batch rule FMXExpress#537, SerialOnly FMXExpress#538, compact results, disclosure, turn clock FMXExpress#543) all shipped on mechanistic arguments with no measured effect. The plan closes that by wiring together what already exists rather than building a bench from scratch. Census findings the plan is grounded in: - a scripted ILLMProvider (TScripted) already drives RunToolLoop in the progress-ledger tests -- the in-process bench substrate exists - TToolLoopResult already carries the metrics (iterations, usage with cache read/created tokens, truncations, dispatched calls) - context hygiene already exists and is untested for effect: per-turn hash-aware read dedup (C3) and stale-read stubbing after writes -- which corrects the earlier Bullet-eval claim that PasClaw has no re-read guard - PR FMXExpress#313 holds a real 13-fixture SWE-shaped bench with oracles and an ablation grid, but its branch has NO merge base with main -- the plan mines it onto main and closes the PR, same treatment for FMXExpress#308 Phases: P0 bench record (NDJSON per turn incl. prefix byte-stability), P1 in-process scenario bench with committed budgets as a failing CI gate, P2 extract bench/swe from the orphaned branch, P3 opt-in live-model A/B of the shipped levers (needs a key; refuses without --spend), P4 the backlog each item lands only with its bench delta attached. Honest boundary stated up front: harness properties are fully measurable in-sandbox with the scripted provider; model-dependent effects (does rule 7 change batching) need the live tier. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
…llel First measured evidence for two things this week shipped on reasoning alone. PartitionToolBatches is unit-tested and FMXExpress#538 pinned that a SerialOnly writer gets its own batch, but both assert the PLAN. This asserts the consequence -- wall time -- through the real binary. Three turns against a deliberately fat corpus, plus a no-tool turn to calibrate: turn overhead (no tools) 256 ms one grep 284 ms of work three greps in ONE round 292 ms of work ~= 1.0x -> they ran together grep, todo_write, grep 475 ms of work ~= 1.7x -> the writer split them The second line is the parallel-dispatch claim from FMXExpress#537's machinery. The third is FMXExpress#538: todo_write writes the checklist, so it must land in a batch of its own, which forces the two greps apart. Had it been wrongly treated as parallel-safe, that round would have collapsed toward 1.0x like the one above it. Calibration is the part worth reviewing. The first draft compared raw turn times and read a correct 2x split as 1.3x -- roughly half of every number was gateway + relay overhead, not work. Measuring a no-tool turn and subtracting it makes the ratios mean what they say, and keeps the budgets portable: a slower or loaded machine moves all four numbers together. There is also a floor check, so if the corpus stops being fat the bench says so instead of asserting on noise. PRE-EXISTING FAILURE, not from this change: `histelide` fails on unmodified main -- verified by stashing this work and re-running. Its elision assertions pass (no verbatim replay, marker present); what fails is the size budget, Length(EnvAt(1)) < 20000 against an actual 31858. That reads as the fixed per-request overhead having grown past a committed number rather than elision breaking, which is exactly the tool-description-budget item in docs/agent-bench-plan.md. Left alone here: it wants its own change and a decision about whether to trim the schemas or move the budget. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
Measured, not argued. Driving PasClaw through the relay with myself as the model -- write a 250-line FPC asteroids game, then compile it -- the request breakdown on the gateway profile was: tool schemas 24751 bytes 74% system prompt 6935 bytes 20% conversation 1351 bytes 4% <- the actual work 96% of every request was overhead, paid on every iteration of every turn. Progressive disclosure already existed for this exact problem but was pointed only at MCP tools; the built-in catalog on the gateway/serve profile is 34 tools and most turns touch a handful. db_*, workflow_*, spawn* and session_* now register deferred, revealed on demand by the tool_search that already exists. Same task after: 33206 -> 22320 bytes, 33% smaller, first-use of a deferred tool costing one extra turn. Two things worth review: The deferral is applied in TToolRegistry.RegisterImpl behind a latch, not by sweeping the registry once. A one-shot sweep was the first attempt and it silently missed the spawn family, which registers AFTER the disclosure setup -- order-dependence is exactly the failure mode the SerialOnly work (FMXExpress#538) taught, so the registry owns the rule and any entry point inherits it. ToolIsLongTail sits beside ToolIsSerialOnly for the same reason. tool_search's registration gate had to widen: it was conditional on MCPProgressiveDisclosure alone, so deferred built-ins would have been unreachable with MCP disclosure off. It now registers when EITHER flag is on, and mcp_disclosure_tests is updated to pin the new contract -- still absent when BOTH are off. That test failing is what caught the gap. KNOWN GAP, not fixed here: the spawn family (5 tools, ~2.2 KB) is still visible in the gateway run despite the latch. The mechanism is verified correct in isolation -- a registry with the latch set defers a spawn-named tool and leaves read_file alone -- so this is a registration -order path I have not yet pinned down, not a broken rule. The 33% is what is actually measured today; closing that path is worth another ~7%. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
TToolCategorywas answering two unrelated questions with one field:tcReadOnlyas "safe to run concurrently" when partitioning a round into parallel batchesThose disagree for any tool that writes but is labelled
tcReadOnlyto pass the gate — and re-categorising can't fix it, because whichever meaning you correct, you break the other.The audit found three, not one
tcReadOnlymemory_searchSyncDirreindexes changed files and drops rows for deleted ones — writes + commits to.index.dbplan_writeworkspace/PLAN.mdtcReadOnlyto pass thepmPlangatetodo_writeOnly
memory_searchwas reported. Fixing one of three would have left the same defect twice over.The fix
TToolgainsSerialOnly, and the batcher now requires both properties:tcReadOnlyandnot SerialOnly.Categorykeeps answering the gate and MCP questions unchanged, so plan mode and read-only exposure behave exactly as before.Polarity — the one thing worth re-checking in review
TToolrecords are built on the stack and this codebase deliberately does notFillCharthem (managed strings), so a site assigning fields one by one leaves a new Boolean holding stack garbage.SerialOnly, garbage-True costs a batch slot — a tool runs alone that could have run beside others.ParallelSafe), garbage-True would mean "batch this writer concurrently" — uninitialised memory becoming a data race.The safe failure had to be the default one.
Verification
PartitionToolBatchesis exported for tests, because which tools share a batch is a correctness property and asserting the flags that feed the decision is weaker than asserting the decision:Confirmed this test fails against the old predicate — restoring
(T.Category = tcReadOnly)alone turns the writer case red.No regression in the meanings I left alone:
test-plan-build-mode,test-guardfall,test-progress-ledger,test-mcp-server,test-tool-choice,test-subagent-default,test-fs-tool-naming,test-mcp-hub-projection,test-prompt-batching,make smoke, and a clean full rebuild — all green.🤖 Generated with Claude Code
https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
Generated by Claude Code