Skip to content

Defer the built-in long tail behind tool_search (33% off every request) - #547

Merged
FMXExpress merged 2 commits into
mainfrom
claude/builtin-tool-disclosure
Aug 14, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/builtin-tool-disclosure

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

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 exactly this and was pointed only at MCP tools, while the gateway/serve profile registers 34 built-ins and most turns touch a handful.

db_*, workflow_*, spawn*, session_* now register deferred, revealed on demand by the tool_search that already exists.

Same task, after: 33,206 → 22,320 bytes (33% smaller). First use of a deferred tool costs one extra turn.

Two things worth review

The deferral lives in RegisterImpl behind a latch, not a one-shot sweep. The sweep was my first attempt and it silently missed the spawn family, which registers after the disclosure setup. Order-dependence is precisely the failure mode #538 taught, so the registry owns the rule and every entry point inherits it. ToolIsLongTail sits beside ToolIsSerialOnly for the same reason.

tool_search's gate had to widen. It was conditional on MCPProgressiveDisclosure alone, which would have left deferred built-ins unreachable with MCP disclosure off. It now registers when either flag is on. 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 haven't pinned down, not a broken rule. 33% is what's measured today; closing that path is worth roughly another 7%.

Gates

test-prompt-batching, test-mcp-disclosure, test-config-mcp-flags, test-subagent-default, test-skills-prompt, test-tool-rpc, make smoke — all green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6


Generated by Claude Code

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
(#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

@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: 1995f8c386

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

Comment on lines +180 to +181
if Cfg.BuiltinProgressiveDisclosure then
Reg.DeferLongTail := True;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Register tool_search when MCP startup is skipped

With BuiltinProgressiveDisclosure enabled by default, this latch marks subsequently registered spawn* tools as deferred. However, the --no-mcp paths in Cmd.Agent, Cmd.TUI, Cmd.Serve, and Cmd.Gateway skip ConnectMCPServers, which is currently the only code that calls RegisterMCPDisclosureTools and establishes the primary registry used by the deferred-tools prompt. Consequently, spawn, spawn_background, and related tools disappear from provider definitions without exposing either tool_search or the deferred-tool instructions, so disabling MCP unintentionally makes the default subagent tools undiscoverable. Install the disclosure surface independently of MCP connectivity before enabling this latch.

Useful? React with 👍 / 👎.

Codex is right and the bug is worse than the gap I flagged. I had latched
built-in deferral in RegisterSkillDisclosureTools so it would run in every
registry path -- which decoupled it from the thing that makes deferral
survivable. On the --no-mcp paths (Cmd.Agent, TUI, Serve, Gateway)
ConnectMCPServers is skipped, and that is the only caller of
RegisterMCPDisclosureTools, so spawn*/db_*/workflow_* would have been
deferred with tool_search never registered and no deferred-tools section
in the prompt: not merely invisible, undiscoverable and unreachable.
Disabling MCP would have silently removed the subagent tools.

The fix restores the coupling and makes it structural rather than
conventional: the latch is set in exactly one place, eleven lines below
the Register call for tool_search in the same function, so a registry can
never acquire deferred built-ins without also acquiring the tool that
reveals them. `grep -rn "DeferLongTail :="` returns one line, and that is
the invariant.

Then the disclosure surface is installed independent of MCP connectivity,
which is what Codex asked for: Cmd.Agent's ConnectMCP registers it on the
NoMCP branch before returning, and PasClaw.Agent registers it in the else
of `if FUseMCP`. Both sites needed PasClaw.MCP.Disclosure added to uses.

Measured after the change on the same relay task: 21 visible tools, 22320
bytes vs the 33206 baseline (33%), tool_search present.

The spawn family is still visible -- unchanged from the previous commit
and still not root-caused. Worth stating plainly that this is now the SAFE
direction of that bug: tools present but unbatched costs bytes, whereas
the shape Codex found costs capability.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
@FMXExpress
FMXExpress merged commit aca0ce3 into main Aug 14, 2026
FMXExpress pushed a commit that referenced this pull request Aug 15, 2026
…k text

test-subagent-bg and test-heartbeat have both been failing on unmodified
main. One root cause: the per-turn date/time stamp lands on the outbound
copy of the history, so a scripted or fake provider legitimately receives
`body + clock` -- and both tests assert on the exact text the provider saw.

    heartbeat    want "check the build status briefly"
                 got  "check the build status briefly\n\n[current date/time: ...]"

    subagent_bg  want "ECHO[summarise rust ownership]"
                 got  "ECHO[summarise rust ownership\n\n[current date/time: ...]]"

The feature is right and the assertions were stale. A heartbeat wake-up and a
spawned subagent both benefit from knowing what time it is; nothing about
either case argues for suppressing the clock.

So the fix is on the test side, but not by hand-matching the stamp. The shape
was written inline in HistWithTurnClock, which meant any test coping with it
had to respell the format and would rot the day it moved. It now has one
owner -- TurnClockPrefix -- plus an exported StripTurnClock that both tests
go through.

StripTurnClock took two passes. The first assumed the stamp ended the string,
which holds for a message body but not for a spawn_wait status that QUOTES a
stamped prompt and continues with more text after it. It now scans to the
closing bracket and loops; the stamp carries no nested brackets, so the first
] after the marker closes it.

In subagent_bg the strip goes in the fake provider's echo rather than around
five separate assertions: those tests check WHICH TASK the job ran, not the
transport decoration around it, so one strip at the source is both smaller
and more faithful to what is being asserted.

Also in this commit, from Codex on #551: NewStampedSession.

The profile stamp started life inline in RunInteractive, which covered only
the session that path creates. `agent --session <id> -m ...` (RunSingleTurn)
and interactive /new both persist sessions too, and both were writing them
with no profile -- so resuming either fell back to the ambient profile and
silently dropped the sandbox the session was created under, which is the
exact failure this feature exists to prevent.

Fixed structurally rather than by repeating the assignment at three call
sites -- the same shape Codex rightly rejected on #547. Session creation now
has one stamped entry point and `grep "TSession.Create("` in Cmd.Agent
returns only the line inside it, so a fourth path cannot forget the stamp.

`make -k test` is now fully clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
FMXExpress pushed a commit that referenced this pull request Aug 15, 2026
Tool schemas were 11,792 bytes on every request -- 47-58% of a turn against a
conversation that started at 528 bytes. Driving the agent through the relay
across two real workloads (building a 3700-line program, then editing this
repo) it called read_file, write_file, edit_file, grep_files and shell_exec,
and nothing else, across 13 provider calls.

ToolIsLongTail only knew about four PREFIX FAMILIES -- db_, workflow_,
spawn_, session_ -- and the expensive tools are not in any of them:

    web_fetch 1058   execute_code 996   find_files 791
    apply_patch 698  memory_fetch 639   append_file 407   = 4589 B/call

Measured after deferring those six:

    tools     11792 -> 7191 bytes  (-39%)
    visible   16 -> 10

Two things had to be true first, and neither was.

Every one of those six is named in the system prompt, 1 to 8 times each. So
deferral had to leave the model able to act on a prompt that talks about a
tool it cannot see -- otherwise this is #547 again in a different costume.
Two blockers in the deferred section:

  - it listed BARE NAMES. Fine for db_query, useless for anything whose name
    is not self-describing. Entries now carry a one-line summary, which costs
    ~60 bytes against the ~800 a full schema costs -- that ratio is the whole
    argument for deferring rather than dropping.

  - it said "You have N MCP tool(s)". True when only MCP servers were
    deferred; false since built-ins joined the list in #547, and actively
    misleading to an operator with no MCP server configured, who would
    reasonably read the section as not applying.

Excluded on purpose, with the reasoning written down next to the list:
tool_output_get is the handle other tools' truncated results tell the model to
call, so deferring the recovery path is exactly the mistake #547 made;
tool_search is the discovery mechanism; memory_search is the memory contract
and is named eight times in the prompt.

Also folded in: DeferBuiltinTools kept its OWN copy of the prefix list while
RegisterImpl derived IsDeferred from ToolIsLongTail. Two lists that had to
agree about what the long tail is, and a drift would have deferred a tool on
one path but not the other. ToolIsLongTail is now the only list.

Verified against the running binary, both reachability paths:

  - fail-open dispatch: called deferred find_files directly, with no
    tool_search first. It ran and returned a real answer, so deferral costs
    zero round trips whenever the model knows the name -- which the listing
    now guarantees it does.
  - tool_search: loading web_fetch moved it into the callable array on the
    very next request, 7191 -> 8251 bytes, exactly its 1060-byte schema.

One bug caught mid-round: FirstSentenceOf cut find_files at "(e.g." A period
two characters back now marks an abbreviation rather than a sentence end.

test-mcp-disclosure passes all ten assertions.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
FMXExpress pushed a commit that referenced this pull request Oct 21, 2026
…disables it

An improve-mode loop run against PasClaw's own building ability, with
the harness checked in under bench/ragbench. The metric is request
bytes (about tokens) to accomplish one fixed task: a gateway runs
"build a Python RAG pipeline" through /v1/chat with the relay
provider, and worker.py stands in for the model, recording every
request's size and answering from a fixed playbook, so a before/after
comparison measures PasClaw's overhead and not model variance.

Benchmark. 6 turns, 156,064 request bytes on a default gateway. With
--no-mcp, 228,556.

Profile. Tool schemas were 15,703 of a 24,214-byte request (65%); the
system prompt 7,773 (32%, Rules 4,499); the conversation 491 bytes
growing to 3,760 by the last turn. Tool results were already terse.
Of 19 visible tools the task called three. Four of the visible ones
act on the agent's own state or UI rather than on the task -- desktop,
memory_write, memory_fetch, shell_background -- and were 3,972 bytes
on every request. The --no-mcp number had a different cause: the
gateway, serve and TUI only reached RegisterMCPDisclosureTools
through ConnectMCPServers, which --no-mcp skipped, so those surfaces
carried all 36 schemas and had no tool_search. The CLI's ConnectMCP
already handled this (Codex P1 on PR #547); the other three did not.

Improve. Those four names join the long-tail set, and the set now
lives in one place: PasClaw.Tools.Types.ToolIsLongTail, read by both
the registration sweep (DeferBuiltinTools) and the registry's
DeferLongTail latch. The first cut kept a second list in the sweep,
and shell_background -- registered by the background coordinator
after disclosure -- stayed visible; one predicate makes that
impossible. The three --no-mcp call sites now register disclosure the
way the CLI does.

Verify. Same playbook, same files written, same test output:
156,064 -> 132,562 bytes (-15.1%), 15 visible tools. --no-mcp:
228,556 -> 132,562 (-42.0%), now identical to the default. What the
measurement does not cover: a task that needs a deferred tool pays one
extra tool_search turn (about 20 KB on this profile), so a db_- or
spawn-heavy task is not represented; nor is model quality, which a
fixed playbook cannot see.

Research. The next cost by size is the Rules section (4.5 KB, 19% of a
request, re-sent every turn); trimming it is a prompt-quality change
this harness cannot judge, so it is noted in the README, not done.

Tested in mcp_disclosure_tests: the sweep defers the named and
prefixed tools registered before it, the latch defers the ones
registered after it, read_file and shell_exec stay visible, and the
provider sees exactly the visible set.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
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