Skip to content

feat(toolfilter): auto mode with a session-fixed set and a restore tool (#434) - #449

Closed
OsherElhadad wants to merge 3 commits into
mainfrom
feat/toolfilter-auto-restore
Closed

OsherElhadad wants to merge 3 commits into
mainfrom
feat/toolfilter-auto-restore

Conversation

@OsherElhadad

@OsherElhadad OsherElhadad commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #434. Default off (toolfilter.auto_remove: true), Anthropic dialect only.

What

  • Candidates: tools declared in >= 5 sessions over >= 7 days with no recorded call (same rule as the dashboard suggestions, one SQL query, cached 5 min per tenant).
  • Decision point: first request of a session (no assistant turn yet) or the first proven-cold request (Trace.ColdCache). The removed set is stored (pinned store key) and applied unchanged for the rest of the session; a warm session is never touched.
  • Never removed: context_guru_*, tools already called in the transcript, tools named in the system prompt (existing prose gate), a forced tool_choice tool; a non-auto tool_choice skips the decision.
  • Never-worse guard: removed tokens >= 2x the stub's tokens, else nothing is removed.
  • context_guru_tools stub lists hidden names plus a clipped one-line description. A call is answered in band (same loop as expand); the tools are re-declared on that continuation in original positions with the stub re-rendered, and the client's next request reproduces the same bytes (one prefix rewrite per restore, no second one). A stub call co-called with a client tool reaches the client; the restore is then picked up from the transcript on the next request.

Live check (controlled, small, one box, real claude -p via a separate proxy)

  • A session that never needs a hidden tool: far fewer tools declared on the main request and a clearly smaller cold-start prefix write and warm read.
  • A session that needs a hidden MCP tool: the agent called context_guru_tools, the tool was re-declared, and it finished with the correct answer in every run. The restore costs one prefix rewrite; the comparison arm was warm, so any penalty seen is an upper bound.
  • Side effect: when the stub lists a tool's exact name the model may call it directly without restoring; the client executes it and nothing breaks.
  • Not measured: effect on task success over a large task set.

Tests: apply (first-turn removal, set fixed under changing history, warm untouched / cold decides, never-remove rules, guard, restore byte-stability), dash (eligibility query), proxy (in-band restore end to end).

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: CHANGES REQUESTED (one blocking defect). Full go test ./... passes on this branch (32 packages ok, no failures); CI build-test pending when I finished.

Verified OK:

  • Eligibility SQL is tenant-scoped on both tables (d.tenant_id = ? and u.tenant_id = d.tenant_id), cached per tenant id, and the per-tenant Store means the stored declarations/stub can only come from that tenant.
  • Decision point: only on a request with no assistant turn or ColdCache; once stored, the set is applied unchanged (drift test passes); a warm undecided session is untouched; context_guru_*, called-in-transcript (at decision time), prose-named, forced tool_choice and non-auto tool_choice are respected; 2x never-worse guard present.
  • Restore only accepts names in the hidden set (the model cannot declare arbitrary tools), re-declares in original order and re-renders the stub deterministically; AutoToolPrefix is pinned.

BLOCKING: the stored record is per SESSION but a session carries several threads with different tool lists (subagents). Once the main thread has decided, a subagent's first request goes through the fixed-set branch, which (a) appends a stub listing every tool the MAIN thread hid, including tools this subagent was never given, and (b) on a restore, RestoreTools rebuilds the whole tools array from the main thread's snapshot (rec.Tools). Repro (apply test helpers): main body decides and hides mcp__a__one, mcp__a__two, Artifact; a subagent request with tools [Read, Artifact] then gets tools=Read,context_guru_tools with a stub naming mcp__a__one and mcp__a__two; calling the stub for mcp__a__one returns tools=Bash,Read,mcp__a__one,context_guru_tools, i.e. the subagent gains Bash and a hidden MCP tool that its allowlist excluded. Restricted subagent tool sets are a policy boundary, so the stub and any restore must be limited to names actually removed from THIS request's body, and RestoreTools must edit the request's own tools array (re-insert only the requested names, in their original relative position) instead of rebuilding from the snapshot. Add a test with a narrower subagent tool set (no extra tools declared, stub lists only its own removals, restore re-adds only those).

Non-blocking:

  1. pendingCallFor in filterDeclarations makes the fixed branch skip filtering for one request if a pending call targets a hidden tool (set flips for that request); acceptable but note it in the doc.
  2. Fixed-set determinism also depends on prose/tool_choice staying constant (both are re-evaluated per request); mention in the file header.
  3. A stub call co-called with a client tool reaches the client, which answers 'tool not found' to the model; documented in the PR, but say so in docs/components/reformat.md.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Fixed in the latest push. The stub now carries an enum of the tools removed from that request's own body, and RestoreTools reads that enum and re-inserts only the requested names into the request's own tools array, after the nearest preceding tool of the stored snapshot that is present (original relative order). It never rebuilds from the main-thread snapshot, so a subagent cannot gain a tool its allowlist excluded. Added TestAutoSubagentThreadIsNeverWidened with your repro (narrower subagent tool set); it fails on the previous commit (stub leaked the main thread's hidden tools) and passes now. The three non-blocking notes are documented in docs/components/reformat.md and the file header. Full go test ./... passes.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: approved after re-review (default-off feature), with one non-blocking caveat. CI all pass; go test ./apply ./proxy ./dash ./components/... ./store green; gofmt clean.

Attacks tried (apply-level, same helpers as the tests):

  • Subagent with allowlist [Read, Artifact] after the main thread decided: tools=Read,context_guru_tools, enum=[Artifact] (only what was removed from its own body). Fixed.
  • Crafted stub call naming Bash, mcp__a__one, nonexistent (outside the enum): 'Not hidden (nothing changed)', tools unchanged. Naming Artifact (in the enum) restores only Artifact into the request's own array.
  • Client-forged stub: a request that itself declares context_guru_tools with enum [Bash, Zzz]: Bash (present in the session snapshot) is re-declared, Zzz is refused. This cannot cross a principal boundary: the snapshot is the same tenant's own earlier declarations, and a client can declare any tool in its own request anyway; the model cannot alter the stub the proxy rendered. The name context_guru_tools is effectively reserved (a client tool with that name is intercepted by the proxy).
  • A forged transcript tool_use of the stub only un-removes names already in the stored removed set, and only for tools present in the request's body.

Caveat (non-blocking, please document or fix later): Restored is stored per SESSION. In the repro, after the subagent restored Artifact, the main thread's next (warm) request came out with Artifact declared again (Bash,Read,Artifact,context_guru_tools), i.e. a subagent's restore rewrites the main thread's prefix once. That contradicts 'a warm prefix is never changed' for multi-thread sessions and costs a full-prefix write on a potentially large main context. Track restores per thread (e.g. keyed by the request's tool-name set) or state the cross-thread cost in docs/components/reformat.md. Minor: the restored definition comes from the main thread's snapshot, not the subagent's own declaration of the same name.

@OsherElhadad OsherElhadad added the ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval label Oct 11, 2026
@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Fixed: removed/restored state is now per thread (keyed by the thread's own tool-name set; the session record only holds the name set the first thread decided on). A restore only touches the thread that asked, and uses that thread's own stored declaration. Added TestAutoSubagentRestoreLeavesMainThreadByteIdentical: after a subagent restores Artifact, the main thread's next warm request has byte-identical tools, and the subagent keeps its restore. Full go test ./... passes.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: re-approved after the per-thread change (label stays). CI all pass; go test ./apply ./proxy ./components/... ./store green; gofmt clean.

Mutation check: with apply/toolauto.go from the previous commit, TestAutoSubagentRestoreLeavesMainThreadByteIdentical FAILS (main thread tools changed from Bash,Read,context_guru_tools to Bash,Read,Artifact,context_guru_tools); with the new code it passes.

Probes (apply level):

  • Main thread restores Artifact in band; its next warm request (same list) is byte-identical to the restored form. Good.
  • Identical-name forks share one key and one state: a fork's warm request sees the same restored form. That is correct for forks that share a cache prefix.
  • Tool list changes mid-session (a new MCP tool appears): the thread gets a new key and is treated as a new thread, filtered by the session's removed set against its own tools. It does not cause an extra prefix bust: the client's own tools change already invalidated the prefix (tools hash first), and the new bytes are stable on the next requests.

Non-blocking: in that mid-session-change case the thread's earlier restores are not carried over (m3 came back without the previously restored Artifact), so the model must call the stub again for a tool it was already using. Inherit the restored names from the old key when the new key's tool set is a superset/subset of it, or note the behaviour in the docs.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: REQUEST CHANGES at 95d0810. The code is fine; only the PR text needs changing. (Posted as a comment: GitHub does not let the author account request changes.)

Verified on a local merge with current main (clean merge):

  • auto_remove is off by default and Anthropic-only.
  • Removing the warm/cold gate fails TestAutoWarmUndecidedSessionUntouchedColdDecides.
  • Discarding the stored per-session set, so it is recomputed every turn, fails TestAutoFirstTurnRemovesAndFixesSet, TestAutoRestoreIsOneRewriteThenStable and TestAutoSubagentThreadIsNeverWidened. So the set is fixed for the session, and a restore costs one rewrite and is then stable.
  • context_guru_* tools are never removed.
  • gofmt and go vet are clean. The apply Auto/Tool, proxy Auto/ToolFilter and dash Auto/Suggest tests pass, and CI is green. All 3 commits are signed off with no AI attribution.

Blocking, PR text only: the Live evidence section publishes controlled-run results: token counts, the cost percentages for both arms, and the restore penalty. Under the repo's publication rule those go to the private research repo. Replace them with a qualitative line, for example "Live check: hidden tools were removed at session start; when one was needed the model restored it through context_guru_tools and finished the task". No code change is needed.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-check: APPROVE at 95d0810. The PR body's Live evidence section is now qualitative, with no token counts or cost percentages. The remaining numbers are the product's own suggestion rule (sessions and days), which is fine. The code is unchanged since the earlier review. This approval covers 95d0810.

Osher-Elhadad added 3 commits October 15, 2026 10:57
Adds `toolfilter.auto_remove` (default off, Anthropic dialect). Tools declared
in >= 5 sessions over >= 7 days with no recorded call are removed at session
start or the first proven-cold request; the set is stored and applied unchanged
for the rest of the session so warm prefixes never change.

A `context_guru_tools` stub lists the hidden tools. When the model calls it the
proxy answers in band and re-declares them on that continuation (one prefix
rewrite, byte-identical on the client's next request).

Never removes context_guru_* tools, tools already called, tools named in the
system prompt, or a forced tool_choice tool. Never-worse guard: removed tokens
must be at least 2x the stub's tokens.

Signed-off-by: Osher-Elhadad <[email protected]>
… own tool list

A session can carry subagent threads with narrower tool lists. The stub now
names, and RestoreTools re-inserts, only tools removed from that request's own
tools (the stub carries an enum of them); a restricted thread is never widened.
Documents the pending-call, prose/tool_choice and co-called-stub caveats.

Signed-off-by: Osher-Elhadad <[email protected]>
A subagent's restore no longer re-declares the tool on the main thread's next
warm request. Each thread (keyed by its own tool-name set) keeps its own
declarations, removed set and restores; restore uses the requesting thread's own
declaration.

Signed-off-by: Osher-Elhadad <[email protected]>
@OsherElhadad
OsherElhadad force-pushed the feat/toolfilter-auto-restore branch from 95d0810 to 425c87c Compare October 15, 2026 11:04

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-check: APPROVE at 425c87c. Rebased onto today's main: git range-diff shows 2 of 3 commits identical; the third (the auto-mode commit) differs only by gofmt alignment of MaxCachedIdx in the Trace struct. The PR body is qualitative (checked earlier). CI is green. This approval covers 425c87c.

@amiddavid

Copy link
Copy Markdown
Collaborator

Closing: main already solves this, with a different design.

@amiddavid amiddavid closed this Nov 26, 2026
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants