Skip to content

feat(tooldefer): an always-defer list for tool deferral (#878) - #885

Merged
amiddavid merged 1 commit into
mainfrom
feat/always-defer-878
Nov 4, 2026
Merged

amiddavid merged 1 commit into
mainfrom
feat/always-defer-878

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

This PR adds an always-defer list for tool deferral: toolfilter.always_defer. A tool on the list is deferred in every new thread, whatever the user's history says. save-more tools can now add a tool to the list and take it off.

Behavior change (needs the owner's yes before merge)

  • Who: accounts on the ultrahigh preset (defer: auto) that fill the new list.
  • Old: deferral comes from the user's history only. save-more tools showed "always defer: not available yet (tooldefer: an always-defer list (toolfilter.always_defer) for save-more tools #878)".
  • New: a tool on always_defer is deferred in every new thread. save-more tools offers defer:<name> and undefer:<name>.
  • Default: the list is empty. Nothing changes until a user fills it. The feature has no extra switch: the owner asked for it to be on by default (2026-11-04).
  • Off: an empty list, or toolfilter: {defer: off}.

Data dependence

Metadata only. The list is tool names in the account's configuration. Nothing new is stored.

Storage level What it does
off The same as today: with the Store off, nothing is deferred.
metadata only Works. The list is tool names in the configuration.
everything The same as metadata only.

docs/data-dependence.md: one sentence changed (save-more tools also reads the always-defer list). The toolfilter row is unchanged.

What changed

  1. The rule (tooldefer.Decide). A listed tool:
    • skips the history rule (the user's last 3 sessions), the project veto (the project's last 10 sessions) and the 3-session minimum;
    • still obeys the never-defer list, the keep list (keep wins when a name is on both), a use earlier in the thread, a tool that tool_choice forces, and the 200-token minimum (a server: its total).
  2. MCP servers. mcp__<server> lists a whole server. One listed tool of a server does not defer the rest of the server. A server stays one unit, as today.
  3. The thread rule is kept. The list applies at a thread's first request. A change reaches new threads only. The list reads no history, so Rebuild after a lost record gives the same set.
  4. Config. toolfilter.always_defer: [names]. It takes the same names as keep. A skill__ name or a pattern is refused when the config is written.
  5. save-more tools (dash and proxy). Each row can offer defer:<name> or undefer:<name>. The proxy writes them through the same adapt path as keep / unkeep, for hosted and local proxies. The report does not offer defer for a never-defer tool, a kept tool, a removed tool or a tool Claude Code defers itself. The proxy also refuses defer for a never-defer tool. An empty list leaves no key in the document.
  6. Both plugins (cg.py, identical copies). A listed row shows (always deferred). The screen prints the two new commands only when the report offers them. The "Not available yet (tooldefer: an always-defer list (toolfilter.always_defer) for save-more tools #878)" line is gone.
  7. Docs. docs/reference/presets.md (the ultrahigh row), docs/components/reformat.md (a new section on defer, keep and always_defer), docs/plugin/save-more.md, and the save-more skill text.

How it was tested

All on the eval box, CGO_ENABLED=1 go test -race -count=1:

Packages Result
./tooldefer/... ./config/... ./components/reformat/... ok
./proxy/... ok (486 s)
./context-guru-plugin/... (includes the Python tests and the goldens) ok (359 s)
./dash/... ok (591 s, with -timeout 25m: the default 10 min is too short on the box)

New tests:

  • TestDecideAlwaysDeferRuleCases: one case per rule (history, project veto, no history, fewer than 3 sessions, a whole server, never-defer, keep, keep on the server, used in the thread, tool_choice, under 200 tokens, empty list, together with the history rule).
  • TestDecideAlwaysDeferMCPServer: a server's 200-token minimum is its total; one listed tool does not defer its server; a use in the thread keeps the server.
  • TestApplyAlwaysDeferAtTheFirstRequest: decided at the first request, kept for the thread, and defer: off defers nothing.
  • TestApplyAlwaysDeferRebuildsTheSameSet: a lost record is rebuilt with the same bytes.
  • TestToolDeferAlwaysDeferEndToEnd: through the proxy, a listed tool that the history says is used is deferred.
  • TestToolfilterAlwaysDeferParses, TestExplain (new cases), TestSaveMoreToolsAlwaysDefer, TestEditDeferWritesTheAlwaysDeferList, TestDeferOnReadsTheAlwaysDeferList, TestSaveMoreToolsApplyOnALocalProxy (defer, undefer, revert), the plugin golden apply-deferral-defer, and two Python tests in test_costly.py.

Mutations: 25 of 25 killed. Each one breaks one rule and a test fails:

  • Decide: skip the history rule; bypass never-defer, keep, tool_choice, used-in-thread (one mutation each); bypass the 200-token minimum; let one listed tool defer its server; need history; need 3 sessions; drop the list in Apply; drop the list in Rebuild.
  • Explain: the list ignored; the list ahead of keep and never-defer.
  • Config: accept a skill__ name; drop the parsed list.
  • dash: the listed state; keep does not win; offer defer for a never-defer tool; offer defer for a tool Claude Code defers; no undefer.
  • proxy: defer writes nothing; an empty list leaves a key; the list not read back; the list not passed to the rule (two places).

No paid live runs.

Open questions

  • The list defaults to empty. The owner may want default entries; this PR adds none.
  • Two mutations first survived (a never-defer tool that is not kept was offered defer; the proxy did not pass the list to Apply). Both now have tests.

Technical notes

  • Decide used to return early with no history or fewer than 3 sessions. It now goes on when the list is not empty, and the history rule alone still needs 3 sessions.
  • Explain takes the always list too, so the save-more tools row says "on your always-defer list".
  • The dash report also says the listed state when the host has no history yet (Explain == nil), because the list reads no history.
  • deferSettings / editDefer in proxy/savemoretools.go now pass one deferConf struct (on, keep, always) in place of separate values.
  • not_yet stays in the JSON as an empty list, so an older plugin reads it without a change.

Fixes #878

🤖 Generated with Claude Code

Add toolfilter.always_defer: a tool name, or mcp__<server> for a whole
MCP server. With defer: auto, a listed tool is deferred in every new
thread. It skips the history rule (the user's last 3 sessions) and the
project veto (the project's last 10 sessions). It still obeys the
never-defer list, the keep list (keep wins when a name is on both), a
use earlier in the thread, a tool that tool_choice forces, and the
200-token minimum (a server: its total). One listed tool of a server
does not defer the rest of the server.

The list reads no history, so it applies at a thread's first request
without 3 sessions of history, and Rebuild after a lost record gives
the same set. With defer: off nothing is deferred. The list is empty by
default, so nothing changes until a user fills it.

save-more tools offers defer:<name> and undefer:<name>, written through
the same adapt path as keep and unkeep (hosted and local). It does not
offer defer for a never-defer tool, a kept tool, a removed tool or a
tool Claude Code defers itself. The "not available yet (#878)" line is
gone. Both plugins show "(always deferred)" on a listed row and print
the two new commands.

Fixes #878

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid 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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 b6bf81ebcaab approve

Verdict: approve

Findings

No blocking issues found. The implementation is correct and complete.

Non-blocking

  1. Documentation clarity: The always_defer description in toolfilter.go line 31 could be clearer about the server-level behavior. It says "a tool name, or mcp__<server> for a whole server" but the actual logic (line 207 of tooldefer.go) also treats a server as listed when every individual tool of it is on the list. This is tested (TestDecideAlwaysDeferMCPServer) but not documented in the config hint.

  2. Minor: Empty list handling: The editDefer function correctly deletes the always_defer key when the list is empty (line 88 of savemoretools.go), which is good. The test confirms this (TestEditDeferWritesTheAlwaysDeferList). No issue, just noting the design choice.

What I checked

  • Core logic: The always_defer list correctly skips the history rule (lines 146-154 of tooldefer.go) while respecting all other rules: never-defer list, keep list, used in thread, tool_choice, and 200-token minimum. The logic at line 207 (u.always = u.always || u.listed == len(u.names)) correctly treats a server as listed only when ALL its tools are individually listed.

  • Fail-open compliance: The change maintains fail-open. The list is an optional input to the existing decision logic. If deferral fails for any reason, the original request goes through unchanged. No new failure modes introduced.

  • Metadata-only compliance: The always_defer list stores only tool names and server identifiers (metadata), consistent with the existing keep list. Updated in data-dependence.md correctly.

  • No unintended behavior changes: This is a new opt-in feature (empty by default). It does not change existing behavior. The list only affects new threads when explicitly configured. Existing threads keep their decided set (cache discipline maintained).

  • Proxy integration: Both applyToolDefer and applyClientSearch correctly pass the always list to tooldefer. The deferOn function reads it from the pipeline (line 365-376 of tooldefer.go).

  • Dashboard integration: The SaveMoreToolsHost struct includes AlwaysDefer (line 48 of dash/savemoretools.go). The report shows the list (line 131), and the state function checks it (line 147). The defer/undefer actions write it correctly (lines 217-220 of proxy/savemoretools.go).

  • Test coverage: Comprehensive tests added:

    • TestDecideAlwaysDeferRuleCases: 12 cases covering all rule interactions
    • TestDecideAlwaysDeferMCPServer: server-level behavior and the 200-token minimum
    • TestApplyAlwaysDeferAtTheFirstRequest: thread lifecycle and account opt-out
    • TestApplyAlwaysDeferRebuildsTheSameSet: rebuild after restart (no history needed)
    • TestToolDeferAlwaysDeferEndToEnd: full proxy integration
    • TestExplain: updated for the new list
    • TestSaveMoreToolsApplyOnALocalProxy: defer/undefer actions
    • TestEditDeferWritesTheAlwaysDeferList: config editing
    • TestDeferOnReadsTheAlwaysDeferList: pipeline reading
  • Documentation updates:

    • docs/reference/presets.md line 182: updated ultrahigh description
    • docs/data-dependence.md line 237: updated toolfilter row
    • components/reformat/toolfilter.go lines 31-32: config comment
    • tooldefer/tooldefer.go lines 28-35: package doc
  • Config validation: The newToolfilter function validates list entries (line 109 of components/reformat/toolfilter.go). Skills are rejected from always_defer (line 111), which is correct since deferral does not apply to skills.

  • Edge cases handled:

    • Empty list deletes the key (line 88 of proxy/savemoretools.go)
    • Keep list wins over always_defer (line 25 of tooldefer/explain.go)
    • Never-defer list wins (line 23 of tooldefer/explain.go)
    • Server-level listing requires all tools or the server name (line 207 of tooldefer/tooldefer.go)
    • Rebuild gives the same set (no history dependency, line 850 of tooldefer/tooldefer_test.go)

All changes are consistent with the repo's rules: metadata-only, fail-open, no behavior change without permission (this is a new opt-in feature), and comprehensive testing.

@amiddavid amiddavid 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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 b6bf81ebcaab blocking

Verdict: blocking

Findings

  1. tooldefer/tooldefer.go:203-204 — Server always-defer logic can incorrectly defer partial servers
    • What fails: When some (but not all) tools of an MCP server are individually listed in always_defer, the logic at line 203 sets u.always = true for the entire server, causing all tools of that server to be deferred even though only specific tools were listed.
    • Proof: Line 203: u.always = u.always || u.listed == len(u.names). This condition is true when every tool is individually listed OR when the server itself is listed. However, the comment at lines 201-202 states "one listed tool does not defer the rest of its server", which contradicts the implementation. The test at tooldefer/tooldefer_test.go:789-792 confirms the intended behavior: listing one tool (mcp__big__t1) should NOT defer the server, but the current logic would set u.always = true when u.listed == 1 and len(u.names) == 1 for a single-tool server.
    • Fix: The logic should only set u.always = true when the server prefix itself is in the always list, not when individual tools happen to match the count. Change line 203 to: u.always = u.always (remove the || u.listed == len(u.names) part), since u.always is already set correctly at line 195 for the server prefix.

Non-blocking

  • The PR correctly updates all documentation (presets.md, design.md, data-dependence.md) to describe the new feature.
  • The API defense-in-depth checks (proxy/savemoretools.go:197-202) correctly prevent writing Context Guru's own tools or tools on the never-defer list to the always-defer list.
  • The Explain function correctly implements the always-defer logic with proper precedence (keep list wins over always-defer).
  • Test coverage is comprehensive, including end-to-end tests, rebuild tests, and edge cases.
  • The feature correctly maintains metadata-only storage (tool names only, no content).

What I checked

  • Read the full diff (1,089 lines across 11 files)
  • Verified the deferral decision logic in tooldefer/tooldefer.go against the documented rules
  • Checked that the always-defer list respects all existing constraints (keep list, never-defer list, token minimum, thread usage)
  • Verified API safety checks prevent malicious or incorrect tool names
  • Confirmed data-dependence documentation is updated correctly
  • Reviewed all test cases for correctness and coverage
  • Verified the UI/API changes correctly read and write the always-defer list
  • Checked that the feature works at all storage levels (off, metadata-only, everything)

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Reply to reviewer 1, round 1, finding 1 (the server rule): no change. The code does what its comment says.

The line is:

u.always = u.always || u.listed == len(u.names)
  • u.always starts true only when the server itself (mcp__<server>) is on the list (tooldefer.go:195).
  • u.listed == len(u.names) is true only when every tool the server declares is listed. Listing 1 of 3 tools gives 1 == 3, which is false, so nothing of that server is deferred. That is the comment's case: "one listed tool does not defer the rest of its server".
  • The example in the finding is a server with one tool. Listing its only tool is listing every tool, so deferring the server is correct: no other tool of it exists to be deferred by mistake.
  • The proposed fix (drop || u.listed == len(u.names)) would break the documented case "listed by every tool it declares": a user who lists each tool of a server one by one would get nothing deferred.

This is the owner's design (2026-11-04): an MCP server is one unit, deferred when mcp__<server> is listed or when every tool of it is listed. The test table's server cases cover the one-of-several case.

🤖 Generated with Claude Code

@amiddavid amiddavid 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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 b6bf81ebcaab approve

Verdict: approve

Findings

Round 1 finding 1: REJECTED (my error)

The author's explanation is correct. The logic at tooldefer/tooldefer.go:203 is right:

u.always = u.always || u.listed == len(u.names)
  • u.listed == len(u.names) is true only when every tool of the server is individually listed.
  • For a server with 3 tools where only 1 is listed: 1 == 3 is false, so the server is NOT deferred. This matches the comment "one listed tool does not defer the rest of its server".
  • For a server with 1 tool where that tool is listed: 1 == 1 is true, so the server IS deferred. This is correct because listing the only tool means listing all tools.
  • The design allows two ways to defer a server: list mcp__<server>, or list every individual tool. Both are intentional.

My error was misunderstanding the equality check. I thought it would defer when any tools were listed, but it only defers when the count matches exactly (all tools listed).

Non-blocking

None. The implementation is correct and complete.

What I checked

  • Last review: round 1, b6bf81e, 2026-11-04T08:25:38Z
  • New commits read: 0 (verified with git log b6bf81ebcaabe4231a5c9f23b1519b2aba2b55b3..HEAD - empty output)
  • Read in full: 2 reviews, 1 comment, 0 inline comments
  • New items since my last review:
    • Reviewer 2's approve verdict (2026-11-04T08:24:38Z): agrees the implementation is correct, notes the same server logic I questioned but correctly understands it
    • Author's reply to my finding (2026-11-04T09:19:29Z): explains the logic is correct and matches the design

Re-verified the server deferral logic:

  • Line 195 sets u.always true when the server prefix is on the list
  • Line 203 adds the case where every individual tool is listed
  • The equality check u.listed == len(u.names) ensures partial listing does NOT defer
  • Test TestDecideAlwaysDeferMCPServer at line 789 confirms: listing one tool of a multi-tool server defers nothing
  • The design intentionally supports both mcp__<server> and listing every tool individually

The PR is ready to merge.

@amiddavid amiddavid 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.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 b6bf81ebcaab approve

Verdict: approve

Findings

No blocking issues. Reviewer 1's finding is incorrect.

Response to reviewer 1, finding 1

Reviewer 1's claim: The server always-defer logic at line 203 incorrectly defers partial servers.

Status: Rejected. The author's reply is correct.

Verification: The logic u.always = u.always || u.listed == len(u.names) correctly implements "defer when every tool is listed":

  • For a server with 30 tools (line 803 serverTools("big", 30)), listing one tool gives u.listed = 1 and len(u.names) = 30, so 1 == 30 is false. The server is NOT deferred.
  • For a server with 1 tool, listing that tool gives u.listed = 1 and len(u.names) = 1, so 1 == 1 is true. The server IS deferred, which is correct: every tool of it is listed.
  • The test at line 807 confirms: listing mcp__big__t1 (one of 30 tools) does not defer the server.

The code matches its comment ("one listed tool does not defer the rest of its server") and the test. No change needed.

Non-blocking

My round 1 non-blocking notes remain valid:

  1. The config hint could mention that listing every individual tool of a server also defers the server (not just mcp__<server>).
  2. Empty list handling is correct.

What I checked

  • Last review: round 1, b6bf81e, 2026-11-04T08:24:38Z
  • New commits read: 0 (verified with git log b6bf81ebcaabe4231a5c9f23b1519b2aba2b55b3..HEAD, output empty as expected)
  • Read in full: 2 reviews, 1 comment, 0 inline comments
  • New items since my last review:
    • Reviewer 1's round 1 review (2026-11-04T08:25:38Z): raised 1 blocking finding about server logic
    • Author's reply (2026-11-04T09:19:29Z): explained why the finding is incorrect
    • Verified the author's explanation by reading the code at lines 195, 203, and the test at line 807
    • Confirmed the logic is correct: u.listed == len(u.names) is true only when EVERY tool is listed, not when one tool is listed

The PR remains correct and complete. No changes needed.

@amiddavid
amiddavid merged commit 7006271 into main Nov 4, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 4, 2026
amiddavid added a commit that referenced this pull request Nov 4, 2026
…0.2.11) (#921)

Claude Code plugin 0.6.6 -> 0.6.7:
- #889: the preset picker restarts the proxy after a yes, and the status shows a pending preset.
- #891: insights and the status show tool deferral as its own saving lever.
- #877, #885: save-more tools (keep, unkeep, defer, undefer, search on/off, revert).
- #882: ultrahigh is marked experimental in the picker.

Codex plugin 0.2.10 -> 0.2.11:
- #889, #891: the same cg.py changes (vendored copy).
- #882: ultrahigh is marked experimental in the picker.


Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

tooldefer: an always-defer list (toolfilter.always_defer) for save-more tools

1 participant