Skip to content

Commit 575d35d

Browse files
committed
Narrow the change set to the tool-policy work and its regression tests
Remove the task-lifecycle and history-persistence work from this branch: the metadata-fetch timeout bound, the waiter-detach signal plumbing, and the post-summarization cancellation guard revert to main; that work is preserved outside the branch for a follow-up. What remains is the prompt/tool-policy change for Zoo-Code-Org#1240 and Zoo-Code-Org#505, plus two fixes the review asked for. A new builder-layer test pins that modelInfo.excludedTools excluding use_mcp_tool removes the dynamic mcp--* declarations from the sent tools, like a user-level disable. And a disabled or excluded attempt_completion now honors the tool allowlist end to end: it leaves the effective policy set and the callable allowlist, and execution rejects the call with the standard validation-error tool_result instead of completing the task.
1 parent 73670b8 commit 575d35d

18 files changed

Lines changed: 336 additions & 1501 deletions

‎apps/vscode-e2e/src/suite/restart-persistence.test.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ async function quitGracefully(): Promise<void> {
2525
await vscode.commands.executeCommand("workbench.action.quit")
2626
}
2727

28-
// The API history file is committed via a backup-rename swap under an advisory
29-
// lock, so an immediate post-completion read can transiently observe it as
30-
// missing. Poll the same read the assertion uses until it reports a non-empty
31-
// history; the assertion semantics below are unchanged.
32-
async function waitForApiConversationHistoryLength(api: RooCodeAPI, taskId: string): Promise<number> {
33-
let length = 0
34-
await waitFor(async () => {
35-
length = await api.getTaskApiConversationHistoryLength(taskId)
36-
return length > 0
37-
})
38-
return length
39-
}
40-
4128
async function runCreate(api: RooCodeAPI): Promise<void> {
4229
let taskId: string | undefined
4330
let createPhasePassed = false
@@ -56,11 +43,6 @@ async function runCreate(api: RooCodeAPI): Promise<void> {
5643
})
5744
await waitUntilCompleted({ api, taskId })
5845
assert.strictEqual(sawMarker, true, `Completion should include ${MARKER}`)
59-
const historyItem = await api.getTaskHistoryItem(taskId)
60-
assert.ok(historyItem, "Completed task should have a history item")
61-
assert.ok(historyItem.task.includes("RESTART_PERSISTENCE_SMOKE"), "History title should include the marker")
62-
const conversationLength = await waitForApiConversationHistoryLength(api, taskId)
63-
assert.ok(conversationLength > 0, "Completed task should persist API conversation history")
6446

6547
const result: PhaseResult = {
6648
version: PHASE_RESULT_VERSION,

‎src/api/index.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,8 @@ export interface ApiHandler {
130130
* Ensures model metadata has been fetched from the remote API so that getModel()
131131
* returns accurate info (context window, pricing, etc.) instead of hardcoded defaults.
132132
* Only router providers that discover models over the network implement this.
133-
*
134-
* `signal` bounds the caller's wait: when it aborts (e.g. the caller's bounded
135-
* metadata wait expired or the owning task was cancelled), the returned promise
136-
* settles with a rejection so no handler-side waiter outlives its caller.
137-
* Fetchers that observe the signal may also stop their network request; the
138-
* shared, de-duplicated catalog fetch may still complete and populate the model
139-
* cache, which is by design for concurrent waiters.
140133
*/
141-
ensureModelFetched?(signal?: AbortSignal): Promise<void>
134+
ensureModelFetched?(): Promise<void>
142135

143136
/**
144137
* Optional context window for context-management / auto-condense when it must differ from

‎src/api/providers/__tests__/zoo-gateway.spec.ts‎

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -724,72 +724,6 @@ describe("ZooGatewayHandler", () => {
724724
expect(refreshModels).not.toHaveBeenCalled()
725725
})
726726

727-
it("settles the waiter with a rejection when the signal aborts mid-fetch", async () => {
728-
// A caller that gives up must not leave a handler-side waiter pending
729-
// on the (shared) catalog fetch: with an observing signal, the
730-
// ensureModelFetched promise rejects at abort time, while the
731-
// underlying fetch continues untouched for any other waiter.
732-
const { getModels } = await import("../fetchers/modelCache")
733-
vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {}))
734-
735-
const handler = new ZooGatewayHandler(mockOptions)
736-
const controller = new AbortController()
737-
738-
const wait = handler.ensureModelFetched(controller.signal)
739-
// Let the waiter attach its abort listener before cancelling.
740-
await Promise.resolve()
741-
controller.abort()
742-
743-
await expect(wait).rejects.toThrow()
744-
})
745-
746-
it("settles the waiter when the fetch wins against a live signal and detaches the listener", async () => {
747-
// Fetch-wins branch: resolve() must settle the await (a dropped
748-
// resolve or a detached .then handler hangs this test), the abort
749-
// listener must be registered with the real { once: true } options
750-
// object, and the detach must target the *same* event name/handler
751-
// pair that was registered — a mutated event name detaches nothing.
752-
const handler = new ZooGatewayHandler(mockOptions)
753-
const controller = new AbortController()
754-
const addEventListenerSpy = vitest.spyOn(controller.signal, "addEventListener")
755-
const removeEventListenerSpy = vitest.spyOn(controller.signal, "removeEventListener")
756-
757-
await handler.ensureModelFetched(controller.signal)
758-
759-
expect(addEventListenerSpy).toHaveBeenCalledWith("abort", expect.any(Function), { once: true })
760-
const registered = addEventListenerSpy.mock.calls.find(([event]) => event === "abort")
761-
expect(registered).toBeDefined()
762-
expect(removeEventListenerSpy).toHaveBeenCalledWith("abort", registered?.[1])
763-
})
764-
765-
it("rejects a signal-observing waiter with the fetch error and detaches the listener", async () => {
766-
// Rejection-branch twin of the fetch-wins test: reject(error) must
767-
// propagate the catalog failure to the waiter (a dropped reject hangs
768-
// this test) and the listener must be detached under the right event
769-
// name. Existing reject coverage passes no signal, so this branch was
770-
// never executed before.
771-
const { getModels } = await import("../fetchers/modelCache")
772-
vitest.mocked(getModels).mockRejectedValueOnce(new Error("network down"))
773-
774-
const handler = new ZooGatewayHandler(mockOptions)
775-
const controller = new AbortController()
776-
const addEventListenerSpy = vitest.spyOn(controller.signal, "addEventListener")
777-
const removeEventListenerSpy = vitest.spyOn(controller.signal, "removeEventListener")
778-
779-
await expect(handler.ensureModelFetched(controller.signal)).rejects.toThrow("network down")
780-
const registered = addEventListenerSpy.mock.calls.find(([event]) => event === "abort")
781-
expect(registered).toBeDefined()
782-
expect(removeEventListenerSpy).toHaveBeenCalledWith("abort", registered?.[1])
783-
})
784-
785-
it("never starts a wait when the signal is already aborted", async () => {
786-
const handler = new ZooGatewayHandler(mockOptions)
787-
const controller = new AbortController()
788-
controller.abort()
789-
790-
await expect(handler.ensureModelFetched(controller.signal)).rejects.toThrow()
791-
})
792-
793727
it("skips the fetch when models are already populated", async () => {
794728
const handler = new ZooGatewayHandler(mockOptions)
795729
const { getModels, refreshModels } = await import("../fetchers/modelCache")

‎src/api/providers/router-provider.ts‎

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,8 @@ export abstract class RouterProvider extends BaseProvider {
108108
return this.modelFetchPromise
109109
}
110110

111-
async ensureModelFetched(signal?: AbortSignal): Promise<void> {
112-
// A caller that already gave up must not start (or keep) a wait on the
113-
// shared catalog fetch.
114-
if (signal?.aborted) {
115-
throw signal.reason
116-
}
117-
118-
const fetch = this.fetchModel()
119-
if (!signal) {
120-
await fetch
121-
return
122-
}
123-
124-
// Detach this waiter as soon as the signal aborts; the shared in-flight
125-
// fetch continues for any other waiter and still populates the cache.
126-
await new Promise<void>((resolve, reject) => {
127-
const onAbort = () => reject(signal.reason)
128-
signal.addEventListener("abort", onAbort, { once: true })
129-
fetch.then(
130-
() => {
131-
signal.removeEventListener("abort", onAbort)
132-
resolve()
133-
},
134-
(error: unknown) => {
135-
signal.removeEventListener("abort", onAbort)
136-
reject(error)
137-
},
138-
)
139-
})
111+
async ensureModelFetched(): Promise<void> {
112+
await this.fetchModel()
140113
}
141114

142115
override getModel(): { id: string; info: ModelInfo } {

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

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
343343
})
344344
})
345345

346-
it("never marks a protocol tool (attempt_completion) as blocked", async () => {
346+
it("marks a disabled attempt_completion as blocked and answers it with an error tool_result", async () => {
347+
// An explicit disabledTools entry outranks the always-available class,
348+
// so a disabled attempt_completion reaches the validator like any
349+
// other tool; its rejection must surface as the standard validation-
350+
// error tool_result instead of completing the task.
347351
mockTask.assistantMessageContent = [
348352
{
349353
type: "tool_use",
@@ -368,22 +372,103 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
368372
}),
369373
}
370374

375+
// Mirror the real validator's rejection for a requirement that maps
376+
// to false (validateToolUse.spec pins the predicate itself).
377+
vi.mocked(validateToolUse).mockImplementationOnce(() => {
378+
throw new Error('Tool "attempt_completion" is not allowed in code mode.')
379+
})
380+
381+
await presentAssistantMessage(mockTask)
382+
383+
const validateToolUseMock = vi.mocked(validateToolUse)
384+
expect(validateToolUseMock).toHaveBeenCalled()
385+
const toolRequirements = validateToolUseMock.mock.calls[0][3]
386+
expect(toolRequirements).toMatchObject({ attempt_completion: false })
387+
388+
const errorToolResults = mockTask.userMessageContent.filter((block: unknown) => {
389+
const b = block as { type?: string; is_error?: boolean }
390+
return b.type === "tool_result" && b.is_error
391+
})
392+
expect(errorToolResults).toHaveLength(1)
393+
expect(mockTask.consecutiveMistakeCount).toBe(1)
394+
395+
// The completion handler must not run for the rejected call.
396+
const { attemptCompletionTool } = await import("../../tools/AttemptCompletionTool")
397+
expect(attemptCompletionTool.handle).not.toHaveBeenCalled()
398+
})
399+
400+
it("treats a model-excluded attempt_completion as blocked and answers it with an error tool_result", async () => {
401+
// A model excludedTools entry suppresses the protocol tool in the
402+
// effective policy, so the execution gate must see the same
403+
// restriction with disabledTools unset.
404+
mockTask.assistantMessageContent = [
405+
{
406+
type: "tool_use",
407+
id: "tool_call_protocol_excluded_123",
408+
name: "attempt_completion",
409+
params: {},
410+
nativeArgs: {},
411+
partial: false,
412+
},
413+
]
414+
415+
mockTask.api.getModel = () => ({ id: "test-model", info: { excludedTools: ["attempt_completion"] } })
416+
417+
mockTask.providerRef = {
418+
deref: () => ({
419+
getState: vi.fn().mockResolvedValue({
420+
mode: "code",
421+
customModes: [],
422+
experiments: {
423+
customTools: false,
424+
},
425+
}),
426+
}),
427+
}
428+
429+
// Mirror the real validator's rejection for a requirement that maps
430+
// to false (validateToolUse.spec pins the predicate itself).
431+
vi.mocked(validateToolUse).mockImplementationOnce(() => {
432+
throw new Error('Tool "attempt_completion" is not allowed in code mode.')
433+
})
434+
371435
await presentAssistantMessage(mockTask)
372436

373437
const validateToolUseMock = vi.mocked(validateToolUse)
374438
expect(validateToolUseMock).toHaveBeenCalled()
375439
const toolRequirements = validateToolUseMock.mock.calls[0][3]
376-
// Protocol tools never enter toolRequirements, so the validator cannot
377-
// block them even when disabledTools lists them.
378-
expect(toolRequirements).not.toHaveProperty("attempt_completion")
440+
expect(toolRequirements).toMatchObject({ attempt_completion: false })
379441

380-
// With validateToolUse mocked to return normally, the block proceeds
381-
// past validation: no validation-error tool_result is pushed.
382442
const errorToolResults = mockTask.userMessageContent.filter((block: unknown) => {
383443
const b = block as { type?: string; is_error?: boolean }
384444
return b.type === "tool_result" && b.is_error
385445
})
386-
expect(errorToolResults).toEqual([])
446+
expect(errorToolResults).toHaveLength(1)
447+
expect(mockTask.consecutiveMistakeCount).toBe(1)
448+
449+
// The completion handler must not run for the rejected call.
450+
const { attemptCompletionTool } = await import("../../tools/AttemptCompletionTool")
451+
expect(attemptCompletionTool.handle).not.toHaveBeenCalled()
452+
453+
// Absent model metadata must not derail the requirements build: the
454+
// protocol-tool leg simply sees no exclusions, and the call validates
455+
// normally instead of erroring out.
456+
mockTask.api.getModel = () => undefined
457+
mockTask.currentStreamingContentIndex = 0
458+
mockTask.userMessageContent = []
459+
mockTask.consecutiveMistakeCount = 0
460+
mockTask.didAlreadyUseTool = false
461+
mockTask.didCompleteReadingStream = false
462+
463+
await presentAssistantMessage(mockTask)
464+
465+
expect(validateToolUseMock).toHaveBeenCalledTimes(2)
466+
expect(validateToolUseMock.mock.calls[1][3]).toEqual({})
467+
expect(mockTask.consecutiveMistakeCount).toBe(0)
468+
const phase2Errors = mockTask.userMessageContent.filter((block: { type?: string; is_error?: boolean }) => {
469+
return block.type === "tool_result" && block.is_error
470+
})
471+
expect(phase2Errors).toHaveLength(0)
387472
})
388473

389474
it("still marks ordinary tools (ask_followup_question) as blocked", async () => {
@@ -416,8 +501,6 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
416501
const validateToolUseMock = vi.mocked(validateToolUse)
417502
expect(validateToolUseMock).toHaveBeenCalled()
418503
const toolRequirements = validateToolUseMock.mock.calls[0][3]
419-
// Control/ordinary tools remain blockable — the inverse of the
420-
// protocol-tool guarantee.
421504
expect(toolRequirements).toMatchObject({
422505
ask_followup_question: false,
423506
})

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,11 @@ export async function presentAssistantMessage(cline: Task) {
605605
const isCustomTool = Boolean(stateExperiments?.customTools && customToolRegistry.has(block.name))
606606

607607
try {
608-
// Use the exported resolver so `attempt_completion` (and its aliases)
609-
// never enters `toolRequirements` — the runtime validator never blocks a
610-
// protocol tool. See `buildToolRequirements` in effective-tool-policy.ts.
611-
const toolRequirements = buildToolRequirements(disabledTools)
608+
// Build requirements through the shared policy module so every suppressed
609+
// entry — disabled tools, and an excluded or disabled protocol tool — reaches
610+
// the validator, which checks them before the always-available class. See
611+
// `buildToolRequirements` in effective-tool-policy.ts.
612+
const toolRequirements = buildToolRequirements(disabledTools, modelInfo?.info)
612613

613614
validateToolUse(
614615
block.name as ToolName,

‎src/core/prompts/__tests__/sections.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ describe("getRulesSection", () => {
352352

353353
it("states the attempt_completion protocol rule unconditionally", () => {
354354
// The completion sentence is protocol wording — emitted even when the policy
355-
// does not advertise attempt_completion. A raw literal is required: the
356-
// resolver-backed policyFor cannot express this (protocol guarantee re-adds the
357-
// tool in resolveEffectiveToolPolicy step 11).
355+
// does not advertise attempt_completion. The raw literal expresses that state
356+
// directly; a resolver-backed policyFor reaches it only by suppressing the tool
357+
// via disabledTools/excludedTools, coupling this test to the resolver.
358358
const rawPolicy: EffectiveToolPolicy = {
359359
tools: new Set<string>(["read_file"]),
360360
hasMcpGroup: false,

‎src/core/prompts/sections/objective.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { EffectiveToolPolicy } from "../tools/effective-tool-policy"
55
*
66
* Step 3's guidance to ask the user via ask_followup_question is replaced with
77
* best-effort phrasing when that tool is not in the request's effective policy.
8-
* Step 4 names attempt_completion, a protocol tool that is always present in the
9-
* effective tool policy, so it is emitted unconditionally.
8+
* Step 4 names attempt_completion; the sentence is protocol wording, so it is
9+
* emitted unconditionally even when the policy does not advertise the tool.
1010
*
1111
* @param policy The request's effective tool policy.
1212
*/

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)