Skip to content

Commit 99f9090

Browse files
DaubnerFedelauna
andauthored
fix: exempt attempt_completion from disabledTools (Zoo-Code-Org#1751)
* fix: exempt attempt_completion from disabledTools A disabledTools entry naming attempt_completion removed the completion tool from prompt generation and API declarations, and runtime validation rejected every call, so a task could never finish. The effective tool policy now partitions such entries out of the user's disabledTools list once, at policy entry, before any filtering step, and surfaces the ignored entries as a single in-task notice per new task. Prompt generation, API declarations, and runtime validation all derive from the same resolved policy, so they cannot disagree about which tools are callable. Stored configuration is left byte-untouched. The other always-available tools remain blockable, and a model-profile excludedTools entry still strips attempt_completion. Code comments that stated the old precedence are corrected (comment-only). Issue: Zoo-Code-Org#1640 * test: cover missing- and corrupt-text branches of the disabled-tools notice The patch-coverage check flagged two untested branches in the ChatRow case that renders the ignored disabled-tools notice: the row renders nothing when the message carries no text, and when the stored payload is not valid JSON. Add spec cases for both so every render path of the notice is covered. * Strengthen exemption tests and guard the ignored-tools notice render Follow-up to PR review feedback on the attempt_completion exemption. presentAssistantMessage-custom-tool.spec.ts asserted only that attemptCompletionTool.handle was called for a disabled attempt_completion entry. The assertion now also pins the arguments: the task, the attempt_completion tool-use block, and the callback object including its toolCallId. ChatRow rendered the ignored disabled-tools warning by calling join on the parsed ignoredTools field after only a truthy check, so a corrupted persisted payload (a string, or an array containing non-strings) threw during render. The field is now treated as unknown and the row renders nothing unless it is a non-empty array of strings. Three regression tests in IgnoredDisabledToolsNotice.spec.tsx cover those payload shapes. The translation mock in IgnoredDisabledToolsNotice.spec.tsx now types its options parameter as Record<string, unknown> instead of Record<string, any>. * Cover the resume path in the ignored disabled-tools notice tests Follow-up to PR review feedback on Zoo-Code-Org#1751. The notice that a disabled attempt_completion entry is ignored was only pinned on the new-task path: the spec drove startTask and asserted exactly one ignored_disabled_tools_warning message. The documented behavior that resuming a saved task does not re-emit the notice had no test. Task.ignored-disabled-tools-notice.spec.ts now also drives resumeTaskFromHistory for a history item whose profile lists attempt_completion in disabledTools. The new test asserts that no ignored_disabled_tools_warning message is said during the resume, next to liveness checks (the resume_task ask is issued and the task loop starts once) so a silent early return cannot pass as a zero-notice result. The provider fixture was extracted from the startTask helper so both paths share it; the existing new-task assertions are unchanged. * fix: drop the availability claim from the ignored-tools notice Follow-up to PR review feedback on Zoo-Code-Org#1751. The notice telling the user that a disabled attempt_completion entry was ignored claimed the tool "will remain available". That is false in one state: when the active model profile's excludedTools also names the tool, the effective policy still strips it, so the notice overpromised. The profile-level excludedTools veto is intentional behavior, so the copy changed rather than the policy. messageTemplate now only states why the entry was ignored: "The following tools cannot be disabled, so their disabledTools entries were ignored: {{tools}}." The string is updated in all 18 locale chat.json files; every locale currently replicated the false claim, so an en-only edit would leave 17 of them asserting it. Task.ts detection and emission are untouched, and {{tools}} is byte-preserved everywhere. Two test changes lock in the fix. In IgnoredDisabledToolsNotice.spec.tsx the two assertion strings move to the new copy and a regression test renders the notice and fails if any availability wording reappears (6 tests to 7). validateToolUse.spec.ts gains a builder/validator agreement table: real buildToolRequirements output driven through the real isToolAllowedForMode and validateToolUse across six rows, including the overlap case where both lists name attempt_completion and the profile veto wins. Additions only; every pre-existing test is byte-identical (27 tests to 33). * fix(task): keep the ignored-disabled-tools notice from blocking task start CodeRabbit follow-ups on Zoo-Code-Org#1751: wrap the startTask notice emission in try/catch (mirroring getEnabledMcpToolsCount) so a rejecting provider state read cannot keep the task loop from starting, with a regression test; and feed a duplicate protocol-tool entry through the startup notice test so a regression of the partitionDisabledToolsForProtocol de-dup fails behaviorally. * test(webview): read real locale files in the ignored-tools notice spec * test(webview): add visual snapshot for the ignored disabled-tools warning row Renders ChatRowContent for an ignored_disabled_tools_warning say message through the Playwright gallery across all four VS Code themes, using the real en locale strings. Baselines are generated in the pinned container (see webview-ui/AGENTS.md). * test(webview): add container-generated baselines for the ignored disabled-tools warning PNGs taken from the webview-visual-regression artifact of the pinned Playwright container workflow (per webview-ui/AGENTS.md fallback), not host renders. * test(task): drop unused vscode import from the ignored-tools notice spec --------- Co-authored-by: Elliott de Launay <[email protected]>
1 parent 2bf1714 commit 99f9090

38 files changed

Lines changed: 975 additions & 74 deletions

‎packages/types/src/global-settings.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ export const globalSettingsSchema = z.object({
304304

305305
/**
306306
* List of native tool names to globally disable.
307-
* Tools in this list will be excluded from prompt generation and rejected at execution time.
307+
* Tools in this list are excluded from prompt generation and rejected at
308+
* execution time. The tool the task loop completes through
309+
* (attempt_completion) cannot be disabled this way: an entry naming it is
310+
* ignored at policy resolution.
308311
*/
309312
disabledTools: z.array(toolNamesSchema).optional(),
310313
})

‎packages/types/src/message.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function isNonBlockingAsk(ask: ClineAsk): ask is NonBlockingAsk {
140140
* - `condense_context_error`: Error occurred during context condensation
141141
* - `codebase_search_result`: Results from searching the codebase
142142
* - `too_many_tools_warning`: Warning that too many MCP tools are enabled, which may confuse the LLM
143+
* - `ignored_disabled_tools_warning`: Warning that a `disabledTools` entry names a protocol tool, which user settings cannot disable
143144
*/
144145
export const clineSays = [
145146
"error",
@@ -170,6 +171,7 @@ export const clineSays = [
170171
"codebase_search_result",
171172
"user_edit_todos",
172173
"too_many_tools_warning",
174+
"ignored_disabled_tools_warning",
173175
"tool",
174176
] as const
175177

‎src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,7 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
625625
})
626626
})
627627

628-
it("marks a disabled attempt_completion as blocked and answers it with an error tool_result", async () => {
629-
// An explicit disabledTools entry outranks the always-available class,
630-
// so a disabled attempt_completion reaches the validator like any
631-
// other tool; its rejection must surface as the standard validation-
632-
// error tool_result instead of completing the task.
628+
it("ignores a disabled attempt_completion entry and executes the completion normally", async () => {
633629
mockTask.assistantMessageContent = [
634630
{
635631
type: "tool_use",
@@ -655,29 +651,54 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
655651
}),
656652
}
657653

658-
// Mirror the real validator's rejection for a requirement that maps
659-
// to false (validateToolUse.spec pins the predicate itself).
660-
vi.mocked(validateToolUse).mockImplementationOnce(() => {
661-
throw new Error('Tool "attempt_completion" is not allowed in code mode.')
654+
// Mirror validateToolUse's requirements gate — an entry mapped to
655+
// false rejects with the real validator's not-allowed message (pinned
656+
// by validateToolUse.spec) — so a snapshot entry that leaked past the
657+
// protocol partition surfaces as an execution outcome, not only as a
658+
// differing call argument. The partition strips the protocol-tool
659+
// entry, so the map stays empty and the completion executes.
660+
vi.mocked(validateToolUse).mockImplementationOnce((toolName, mode, _customModes, toolRequirements) => {
661+
if (toolRequirements?.[toolName] === false) {
662+
throw new Error(`Tool "${toolName}" is not allowed in ${mode} mode.`)
663+
}
662664
})
663665

666+
// The snapshot carries the disabled entry; the poisoned live list
667+
// above must never be consulted for validation.
664668
await presentAssistantMessage(mockTask, { ...baseSnapshot, disabledTools: ["attempt_completion"] })
665669

666670
const validateToolUseMock = vi.mocked(validateToolUse)
667671
expect(validateToolUseMock).toHaveBeenCalled()
668672
const toolRequirements = validateToolUseMock.mock.calls[0][3]
669-
expect(toolRequirements).toMatchObject({ attempt_completion: false })
673+
// Absent, not merely un-false: the validator is never told the
674+
// completion tool was disabled.
675+
expect(toolRequirements).not.toHaveProperty("attempt_completion")
670676

671677
const errorToolResults = mockTask.userMessageContent.filter((block: unknown) => {
672678
const b = block as { type?: string; is_error?: boolean }
673679
return b.type === "tool_result" && b.is_error
674680
})
675-
expect(errorToolResults).toHaveLength(1)
676-
expect(mockTask.consecutiveMistakeCount).toBe(1)
681+
expect(errorToolResults).toHaveLength(0)
682+
expect(mockTask.consecutiveMistakeCount).toBe(0)
677683

678-
// The completion handler must not run for the rejected call.
679684
const { attemptCompletionTool } = await import("../../tools/AttemptCompletionTool")
680-
expect(attemptCompletionTool.handle).not.toHaveBeenCalled()
685+
expect(attemptCompletionTool.handle).toHaveBeenCalledWith(
686+
mockTask,
687+
expect.objectContaining({
688+
type: "tool_use",
689+
id: "tool_call_protocol_123",
690+
name: "attempt_completion",
691+
partial: false,
692+
}),
693+
expect.objectContaining({
694+
askApproval: expect.any(Function),
695+
handleError: expect.any(Function),
696+
pushToolResult: expect.any(Function),
697+
askFinishSubTaskApproval: expect.any(Function),
698+
toolDescription: expect.any(Function),
699+
toolCallId: "tool_call_protocol_123",
700+
}),
701+
)
681702
})
682703

683704
it("treats a model-excluded attempt_completion as blocked and answers it with an error tool_result", async () => {

‎src/core/assistant-message/presentAssistantMessage.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,10 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request
757757
const isCustomTool = Boolean(stateExperiments?.customTools && customToolRegistry.has(block.name))
758758

759759
try {
760-
// Build requirements through the shared policy module so every suppressed
761-
// entry — disabled tools, and an excluded or disabled protocol tool — reaches
762-
// the validator, which checks them before the always-available class. See
760+
// Build requirements through the shared policy module so every entry that
761+
// carries disabling weight — non-protocol disabled tools, and every excluded
762+
// tool, protocol entries included on that leg — reaches the validator, which
763+
// checks them before the always-available class. See
763764
// `buildToolRequirements` in effective-tool-policy.ts.
764765
const toolRequirements = buildToolRequirements(disabledTools, policySnapshot.modelInfo)
765766

‎src/core/prompts/tools/__tests__/effective-tool-policy.spec.ts‎

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
resolveToolAlias,
99
buildToolRequirements,
1010
isToolDisabledOrExcluded,
11+
partitionDisabledToolsForProtocol,
1112
} from "../effective-tool-policy"
1213
import type { CodeIndexManager } from "../../../../services/code-index/manager"
1314

@@ -123,12 +124,12 @@ describe("resolveEffectiveToolPolicy - disabledTools", () => {
123124
expect(policy.tools.has("write_to_file")).toBe(false)
124125
})
125126

126-
it("removes a protocol tool listed in disabledTools", () => {
127+
it("ignores a disabledTools entry naming a protocol tool", () => {
127128
expect(
128129
policyFor(["read", "edit", "command"], { disabledTools: [...PROTOCOL_TOOLS] }).tools.has(
129130
"attempt_completion",
130131
),
131-
).toBe(false)
132+
).toBe(true)
132133
})
133134

134135
it("keeps the protocol tool when it is neither disabled nor excluded", () => {
@@ -138,6 +139,61 @@ describe("resolveEffectiveToolPolicy - disabledTools", () => {
138139
),
139140
).toBe(true)
140141
})
142+
143+
it("retains the protocol tool even under an otherwise maximally restricted configuration", () => {
144+
const policy = policyFor([], { disabledTools: ["attempt_completion", "read_file"], todoListEnabled: false })
145+
expect(policy.tools.has("attempt_completion")).toBe(true)
146+
expect(policy.tools.has("read_file")).toBe(false)
147+
expect(policy.tools.has("update_todo_list")).toBe(false)
148+
})
149+
150+
it("still strips other entries that name the always-available tools", () => {
151+
// The exemption covers only the protocol entry, never its always-available siblings.
152+
const policy = policyFor(["read", "edit", "command"], {
153+
disabledTools: ["attempt_completion", "ask_followup_question", "switch_mode", "execute_command"],
154+
})
155+
expect(policy.tools.has("attempt_completion")).toBe(true)
156+
expect(policy.tools.has("ask_followup_question")).toBe(false)
157+
expect(policy.tools.has("switch_mode")).toBe(false)
158+
expect(policy.tools.has("execute_command")).toBe(false)
159+
})
160+
161+
it("still strips the protocol tool when only excludedTools names it, beside unrelated disables", () => {
162+
const policy = policyFor(["read", "edit", "command"], {
163+
disabledTools: ["attempt_completion", "execute_command"],
164+
modelInfo: modelInfo({ excludedTools: ["attempt_completion"] }),
165+
})
166+
expect(policy.tools.has("attempt_completion")).toBe(false)
167+
expect(policy.tools.has("execute_command")).toBe(false)
168+
})
169+
170+
it("still strips the protocol tool when both lists name it", () => {
171+
// With a user disable and a model exclusion naming the same tool, the
172+
// model exclusion still suppresses it in the effective policy.
173+
const policy = policyFor(["read", "edit", "command"], {
174+
disabledTools: ["attempt_completion"],
175+
modelInfo: modelInfo({ excludedTools: ["attempt_completion"] }),
176+
})
177+
expect(policy.tools.has("attempt_completion")).toBe(false)
178+
})
179+
180+
it("keeps prompt advertisement and the execution gate agreeing about the protocol tool", () => {
181+
// Agreement by construction: for every combination of the two lists
182+
// naming the completion tool, the policy advertises it exactly when the
183+
// runtime requirements do not reject it.
184+
const cases: Array<[string[] | undefined, string[] | undefined]> = [
185+
[undefined, undefined],
186+
[["attempt_completion"], undefined],
187+
[undefined, ["attempt_completion"]],
188+
[["attempt_completion"], ["attempt_completion"]],
189+
]
190+
for (const [disabledTools, excludedTools] of cases) {
191+
const model = modelInfo(excludedTools ? { excludedTools } : undefined)
192+
const policy = policyFor(["read", "edit", "command"], { disabledTools, modelInfo: model })
193+
const requirements = buildToolRequirements(disabledTools, model)
194+
expect(policy.tools.has("attempt_completion")).toBe(requirements.attempt_completion !== false)
195+
}
196+
})
141197
})
142198

143199
describe("resolveEffectiveToolPolicy - model customization", () => {
@@ -357,29 +413,26 @@ describe("buildToolRequirements", () => {
357413
expect(reqs).toEqual({ write_file: false, write_to_file: false })
358414
})
359415

360-
it("maps a disabled protocol tool to false like any other tool", () => {
416+
it("omits a disabled protocol tool while mapping the other entries", () => {
361417
const reqs = buildToolRequirements([...PROTOCOL_TOOLS, "ask_followup_question", "switch_mode"])
362-
expect(reqs).toEqual({ attempt_completion: false, ask_followup_question: false, switch_mode: false })
418+
expect(reqs).toEqual({ ask_followup_question: false, switch_mode: false })
363419
})
364420

365421
it("adds alias + canonical for real aliases", () => {
366422
const reqs = buildToolRequirements(["write_file"])
367423
expect(Object.keys(reqs).sort()).toEqual(["write_file", "write_to_file"].sort())
368424
})
369425

370-
it("keeps protocol-tool and regular entries together in a mixed list", () => {
371-
// An explicit protocol-tool disable reaches the validator beside the
372-
// regular tools in the same list.
426+
it("drops the protocol-tool entry from a mixed list and keeps the regular ones", () => {
373427
expect(buildToolRequirements(["attempt_completion", "write_file"])).toEqual({
374-
attempt_completion: false,
375428
write_file: false,
376429
write_to_file: false,
377430
})
378431
})
379432

380433
it("maps a protocol tool excluded by the model to false", () => {
381-
// A model excludedTools entry suppresses attempt_completion just as a
382-
// disabledTools entry does, so the execution gate sees it too.
434+
// A model excludedTools entry suppresses attempt_completion at the
435+
// execution gate; only the user disabledTools leg exempts it.
383436
const reqs = buildToolRequirements(undefined, modelInfo({ excludedTools: ["attempt_completion"] }))
384437
expect(reqs).toEqual({ attempt_completion: false })
385438
})
@@ -392,11 +445,24 @@ describe("buildToolRequirements", () => {
392445
it("returns an empty map for a model customization without exclusions", () => {
393446
expect(buildToolRequirements(undefined, modelInfo())).toEqual({})
394447
})
448+
449+
it("omits the protocol entry from disabledTools while keeping the model exclusion of the same tool", () => {
450+
// An ignored user disable does not shield the tool from a model exclusion.
451+
expect(
452+
buildToolRequirements(
453+
["attempt_completion", "write_file"],
454+
modelInfo({ excludedTools: ["attempt_completion"] }),
455+
),
456+
).toEqual({
457+
write_file: false,
458+
write_to_file: false,
459+
attempt_completion: false,
460+
})
461+
})
395462
})
396463

397464
describe("resolveToolAlias", () => {
398465
it("resolves every registered alias to its canonical tool", () => {
399-
// Exercises the module-load ALIAS_TO_CANONICAL map for both registered aliases.
400466
expect(resolveToolAlias("write_file")).toBe("write_to_file")
401467
expect(resolveToolAlias("search_and_replace")).toBe("edit")
402468
})
@@ -421,6 +487,21 @@ describe("PROTOCOL_TOOLS", () => {
421487
it("lists the single protocol tool by canonical name", () => {
422488
expect([...PROTOCOL_TOOLS]).toEqual(["attempt_completion"])
423489
})
490+
491+
it("partitions disabledTools entries naming a protocol tool into ignored", () => {
492+
expect(partitionDisabledToolsForProtocol(undefined)).toEqual({ effective: [], ignored: [] })
493+
expect(partitionDisabledToolsForProtocol([])).toEqual({ effective: [], ignored: [] })
494+
expect(partitionDisabledToolsForProtocol(["execute_command", "attempt_completion", "web_fetch"])).toEqual({
495+
effective: ["execute_command", "web_fetch"],
496+
ignored: ["attempt_completion"],
497+
})
498+
})
499+
500+
it("de-duplicates repeated protocol-tool entries in the ignored list", () => {
501+
expect(
502+
partitionDisabledToolsForProtocol(["attempt_completion", "attempt_completion", "execute_command"]),
503+
).toEqual({ effective: ["execute_command"], ignored: ["attempt_completion"] })
504+
})
424505
})
425506

426507
describe("resolveEffectiveToolPolicy - edit restriction edge cases", () => {
@@ -661,20 +742,21 @@ describe("resolveEffectiveToolPolicy - MCP capability flags", () => {
661742
})
662743

663744
describe("resolveEffectiveToolPolicy - protocol tool honoring (fresh module)", () => {
664-
// A disabled/excluded protocol tool must stay out of the effective set even
665-
// when aliased: the re-add consults the same alias-resolved predicate as the
666-
// exclusion steps, so an alias in disabledTools suppresses the canonical tool.
745+
// The exemption consults the same alias-resolved names as the exclusion
746+
// steps, so an alias of a protocol tool in disabledTools is ignored just
747+
// like the canonical name, while a model excludedTools entry still
748+
// suppresses it.
667749
async function freshResolve() {
668750
vi.resetModules()
669751
const mod = await import("../effective-tool-policy")
670752
return mod.resolveEffectiveToolPolicy
671753
}
672754

673-
it("suppresses the protocol tool when disabledTools lists an alias of it", async () => {
755+
it("ignores a disabledTools alias of the protocol tool", async () => {
674756
// Reset first, then register a temporary alias of attempt_completion, and
675757
// only then load a fresh resolver: its module-load alias map (and with it
676-
// the re-add gate) is built from the shared alias table as it stands at
677-
// import time, so the suppression becomes reachable only through alias
758+
// the exemption) is built from the shared alias table as it stands at
759+
// import time, so the ignored entry is reachable only through alias
678760
// resolution, not a literal name match.
679761
vi.resetModules()
680762
const toolsMod = await import("../../../../shared/tools")
@@ -685,7 +767,7 @@ describe("resolveEffectiveToolPolicy - protocol tool honoring (fresh module)", (
685767
mod
686768
.resolveEffectiveToolPolicy({ mode: "code", disabledTools: ["wp4_attempt_alias"] })
687769
.tools.has("attempt_completion"),
688-
).toBe(false)
770+
).toBe(true)
689771
// Sanity: the injected alias actually resolves through the fresh module.
690772
expect(mod.resolveToolAlias("wp4_attempt_alias")).toBe("attempt_completion")
691773
} finally {
@@ -701,10 +783,10 @@ describe("resolveEffectiveToolPolicy - protocol tool honoring (fresh module)", (
701783
})
702784

703785
it("pins the protocol list and re-adds an unlisted tool independently of the always-available roster", async () => {
704-
// Two positive controls for the suppression test above, on a fresh module:
705-
// the exported protocol list is pinned, and with attempt_completion
706-
// stripped from the always-available roster the unlisted tool must STILL
707-
// be callable — so the re-add step, not the roster, is what guarantees it.
786+
// Two positive controls on a fresh module: the exported protocol list is
787+
// pinned, and with attempt_completion stripped from the always-available
788+
// roster the tool must STILL be callable — so the re-add step, not the
789+
// roster, is what guarantees it.
708790
vi.resetModules()
709791
const toolsMod = await import("../../../../shared/tools")
710792
const mod = await import("../effective-tool-policy")

‎src/core/prompts/tools/__tests__/filter-tools-for-mode.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ describe("filterNativeToolsForMode - disabledTools", () => {
8989
expect(resultNames).not.toContain("search_and_replace")
9090
expect(resultNames).not.toContain("edit")
9191
})
92+
93+
it("keeps the attempt_completion declaration when disabledTools lists it under a maximally restricted mode", () => {
94+
const restrictedMode: ModeConfig = {
95+
slug: "control-only",
96+
name: "Control Only",
97+
roleDefinition: "",
98+
groups: [],
99+
}
100+
const tools = [makeTool("read_file"), makeTool("attempt_completion")]
101+
102+
const result = filterNativeToolsForMode(tools, "control-only", [restrictedMode], undefined, undefined, {
103+
disabledTools: ["attempt_completion", "read_file"],
104+
})
105+
106+
const names = result.map((t) => ("function" in t && t.function ? t.function.name : ""))
107+
expect(names).toContain("attempt_completion")
108+
expect(names).not.toContain("read_file")
109+
})
92110
})
93111

94112
describe("filterNativeToolsForMode - settings round-trips", () => {

0 commit comments

Comments
 (0)