Skip to content

Commit 8c96629

Browse files
authored
fix(delegation): read task-local mode in getEnvironmentDetails and validateToolUse (#1625)
* fix(delegation): read task-local mode in getEnvironmentDetails and validateToolUse * test(delegation): cover custom-tool execute context and state fallback
1 parent 072b6f3 commit 8c96629

9 files changed

Lines changed: 329 additions & 10 deletions

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"check-types": "turbo check-types --log-order grouped --output-logs new-only",
1414
"test": "turbo test --log-order grouped --output-logs new-only",
1515
"test:mutation-ci": "node --test scripts/stryker-diff.test.mjs",
16-
"lifecycle:model-check": "tsx scripts/check-task-lifecycle.ts && tsx scripts/check-task-store-concurrency.ts && tsx scripts/check-provider-handoff-scheduler.ts && pnpm cleanup-protocol:model-check && pnpm parser-scope:model-check && tsx scripts/check-completion-persistence.ts",
16+
"lifecycle:model-check": "tsx scripts/check-task-lifecycle.ts && tsx scripts/check-task-store-concurrency.ts && tsx scripts/check-provider-handoff-scheduler.ts && pnpm cleanup-protocol:model-check && pnpm parser-scope:model-check && tsx scripts/check-completion-persistence.ts && tsx scripts/check-delegated-mode-readers.ts",
1717
"cleanup-protocol:model-check": "tsx scripts/check-task-cleanup-protocol.ts",
1818
"parser-scope:model-check": "node scripts/run-native-tool-call-parser-scoping.mjs",
1919
"mcp:integration-check": "tsx scripts/check-mcp-oauth-integration.ts",
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// check-delegated-mode-readers.ts
2+
//
3+
// Refinement check for the delegated-child mode-reader invariant (issue #1623).
4+
//
5+
// check-provider-handoff-scheduler.ts verifies the write side: that
6+
// selectHandoffExecutionContext stores the task-local mode correctly.
7+
// This script verifies the read side: that the mode observable by
8+
// tool-validation readers is the task-local mode, not the shared provider mode.
9+
//
10+
// The VS Code-dependent readers (getEnvironmentDetails,
11+
// presentAssistantMessage) are covered by their vitest regression tests.
12+
// This script covers the pure-TS parts of the invariant chain and proves
13+
// that the two sources of mode are observably different, so any reader
14+
// that uses the wrong source silently produces wrong behavior.
15+
//
16+
// Invariant: for any delegated child task C with taskMode = M,
17+
// toolAllowedForMode(tool, M) ≠ toolAllowedForMode(tool, providerMode)
18+
// whenever M ≠ providerMode and the two modes differ on the tool's group.
19+
20+
import assert from "node:assert/strict"
21+
22+
import { DEFAULT_MODES } from "../packages/types/src/mode"
23+
24+
import { TOOL_GROUPS, ALWAYS_AVAILABLE_TOOLS, TOOL_ALIASES } from "../src/shared/tools"
25+
import { selectHandoffExecutionContext, type TaskExecutionContext } from "../src/core/task/providerHandoff"
26+
27+
// ---------------------------------------------------------------------------
28+
// Minimal inline mode-allows-tool check.
29+
// Avoids importing src/shared/modes.ts, which pulls in VS Code.
30+
// Only covers built-in modes (no custom modes, no file-regex options).
31+
// That is enough to prove the behavioral divergence this check needs.
32+
// ---------------------------------------------------------------------------
33+
34+
type ModeConfig = (typeof DEFAULT_MODES)[number]
35+
type GroupEntry = ModeConfig["groups"][number]
36+
37+
function groupName(entry: GroupEntry): string {
38+
return Array.isArray(entry) ? entry[0] : (entry as string)
39+
}
40+
41+
function toolAllowedForMode(tool: string, modeSlug: string): boolean {
42+
const resolvedTool = (TOOL_ALIASES as Record<string, string>)[tool] ?? tool
43+
if ((ALWAYS_AVAILABLE_TOOLS as readonly string[]).includes(resolvedTool)) return true
44+
const mode = DEFAULT_MODES.find((m) => m.slug === modeSlug)
45+
if (!mode) return false
46+
for (const entry of mode.groups) {
47+
const groupTools = (TOOL_GROUPS as Record<string, { tools: readonly string[] }>)[groupName(entry)]?.tools ?? []
48+
if (groupTools.includes(resolvedTool)) return true
49+
}
50+
return false
51+
}
52+
53+
// ---------------------------------------------------------------------------
54+
// Scenario: parent in "orchestrator" mode delegates child to "code".
55+
// Regression behavior: both readers used providerMode ("orchestrator").
56+
// Correct behavior: readers use taskMode ("code").
57+
//
58+
// orchestrator groups: [] → apply_diff blocked
59+
// code groups: [...edit] → apply_diff allowed
60+
// ---------------------------------------------------------------------------
61+
62+
const parentCtx: TaskExecutionContext = {
63+
mode: "orchestrator",
64+
apiConfigName: undefined,
65+
apiConfiguration: { apiProvider: "anthropic", consecutiveMistakeLimit: 3 },
66+
}
67+
68+
// 1. Handoff stores the task-local mode, not the parent mode.
69+
const childCtx = selectHandoffExecutionContext(parentCtx, "code", parentCtx.mode, false, undefined)
70+
assert.equal(childCtx.mode, "code", "handoff must store the requested task-local mode")
71+
assert.notEqual(childCtx.mode, parentCtx.mode, "test scenario requires divergent provider and task modes")
72+
73+
// 2. The two modes produce observably different tool-validation outcomes.
74+
assert.equal(toolAllowedForMode("apply_diff", "orchestrator"), false, "orchestrator has no edit group")
75+
assert.equal(toolAllowedForMode("apply_diff", "code"), true, "code has the edit group")
76+
77+
// 3. Regression claim: a reader that consumes providerMode rejects apply_diff;
78+
// a reader that consumes taskMode correctly allows it.
79+
const viaProviderMode = toolAllowedForMode("apply_diff", parentCtx.mode) // "orchestrator" — wrong source
80+
const viaTaskMode = toolAllowedForMode("apply_diff", childCtx.mode) // "code" — correct source
81+
assert.equal(viaProviderMode, false, "stale provider mode rejects apply_diff (regression behavior)")
82+
assert.equal(viaTaskMode, true, "task-local mode allows apply_diff (correct behavior)")
83+
84+
// 4. Additional mode pairs that show the same divergence.
85+
const DIVERGENT_PAIRS: Array<{
86+
label: string
87+
providerMode: string
88+
taskMode: string
89+
probe: string
90+
blockedInProvider: boolean
91+
allowedInTask: boolean
92+
}> = [
93+
// orchestrator → code: edit tools blocked at provider level, allowed at task level
94+
{
95+
label: "orchestrator→code apply_diff",
96+
providerMode: "orchestrator",
97+
taskMode: "code",
98+
probe: "apply_diff",
99+
blockedInProvider: true,
100+
allowedInTask: true,
101+
},
102+
// orchestrator → code: command tools blocked at provider level, allowed at task level
103+
{
104+
label: "orchestrator→code execute_command",
105+
providerMode: "orchestrator",
106+
taskMode: "code",
107+
probe: "execute_command",
108+
blockedInProvider: true,
109+
allowedInTask: true,
110+
},
111+
// code → ask: edit tools allowed at provider level, blocked at task level
112+
{
113+
label: "code→ask apply_diff",
114+
providerMode: "code",
115+
taskMode: "ask",
116+
probe: "apply_diff",
117+
blockedInProvider: false,
118+
allowedInTask: false,
119+
},
120+
// ask → code: edit tools blocked at provider level, allowed at task level
121+
{
122+
label: "ask→code write_to_file",
123+
providerMode: "ask",
124+
taskMode: "code",
125+
probe: "write_to_file",
126+
blockedInProvider: true,
127+
allowedInTask: true,
128+
},
129+
]
130+
131+
for (const pair of DIVERGENT_PAIRS) {
132+
const ctx = selectHandoffExecutionContext(
133+
{ ...parentCtx, mode: pair.providerMode },
134+
pair.taskMode,
135+
pair.providerMode,
136+
false,
137+
undefined,
138+
)
139+
assert.equal(ctx.mode, pair.taskMode, `${pair.label}: handoff must store task-local mode`)
140+
assert.equal(
141+
toolAllowedForMode(pair.probe, pair.providerMode),
142+
!pair.blockedInProvider,
143+
`${pair.label}: wrong provider-mode result`,
144+
)
145+
assert.equal(
146+
toolAllowedForMode(pair.probe, pair.taskMode),
147+
pair.allowedInTask,
148+
`${pair.label}: wrong task-mode result`,
149+
)
150+
// The two sources disagree, so using the wrong one is always observable.
151+
assert.notEqual(
152+
toolAllowedForMode(pair.probe, pair.providerMode),
153+
toolAllowedForMode(pair.probe, pair.taskMode),
154+
`${pair.label}: provider and task mode must differ on this probe tool`,
155+
)
156+
}
157+
158+
// 5. For every built-in mode as a delegation target: selectHandoffExecutionContext
159+
// always stores the requested mode, regardless of parent mode.
160+
for (const mode of DEFAULT_MODES) {
161+
const ctx = selectHandoffExecutionContext(parentCtx, mode.slug, parentCtx.mode, false, undefined)
162+
assert.equal(ctx.mode, mode.slug, `handoff must store ${mode.slug}, not parent mode ${parentCtx.mode}`)
163+
}
164+
165+
console.log(
166+
`Delegated mode reader check passed: ` +
167+
`regression scenario verified, ` +
168+
`${DIVERGENT_PAIRS.length} divergent-mode pairs checked, ` +
169+
`${DEFAULT_MODES.length}/${DEFAULT_MODES.length} built-in modes verified`,
170+
)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
7777
}),
7878
}),
7979
},
80+
getTaskMode: vi.fn().mockResolvedValue("code"),
8081
say: vi.fn().mockResolvedValue(undefined),
8182
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
8283
}
@@ -122,6 +123,51 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
122123
})
123124
})
124125

126+
describe("Custom tool mode delegation regression", () => {
127+
// Regression for issue #1623.
128+
// Before the fix, customTool.execute received the shared provider mode
129+
// instead of the task-local mode. A child delegated to "architect" would
130+
// have its custom tool called with "orchestrator".
131+
it("passes the task-local mode to customTool.execute, not the provider mode", async () => {
132+
// Provider says "orchestrator"; task was delegated to "architect".
133+
mockTask.providerRef = {
134+
deref: () => ({
135+
getState: vi.fn().mockResolvedValue({
136+
mode: "orchestrator",
137+
customModes: [],
138+
experiments: { customTools: true },
139+
}),
140+
}),
141+
}
142+
mockTask.getTaskMode = vi.fn().mockResolvedValue("architect")
143+
144+
const executeMock = vi.fn().mockResolvedValue("result")
145+
vi.mocked(customToolRegistry.has).mockReturnValue(true)
146+
vi.mocked(customToolRegistry.get).mockReturnValue({
147+
name: "my_custom_tool",
148+
description: "A custom tool",
149+
execute: executeMock,
150+
})
151+
152+
mockTask.assistantMessageContent = [
153+
{
154+
type: "tool_use",
155+
id: "call_delegation",
156+
name: "my_custom_tool",
157+
params: { value: "test" },
158+
partial: false,
159+
},
160+
]
161+
162+
await presentAssistantMessage(mockTask)
163+
164+
expect(executeMock).toHaveBeenCalledOnce()
165+
const context = executeMock.mock.calls[0][1]
166+
expect(context.mode).toBe("architect")
167+
expect(context.task).toBe(mockTask)
168+
})
169+
})
170+
125171
describe("Custom tool error recording", () => {
126172
it("should record custom tool error as 'custom_tool'", async () => {
127173
const toolCallId = "tool_call_custom_error_123"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
5757
}),
5858
}),
5959
},
60+
getTaskMode: vi.fn().mockResolvedValue("code"),
6061
say: vi.fn().mockResolvedValue(undefined),
6162
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
6263
}

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

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ interface MockTask {
6565
recordToolError: ReturnType<typeof vi.fn>
6666
toolRepetitionDetector: { check: ReturnType<typeof vi.fn> }
6767
providerRef: {
68-
deref: () => {
69-
getState: ReturnType<typeof vi.fn>
70-
getMcpHub?: () => { findServerNameBySanitizedName: (name: string) => string | undefined }
71-
}
68+
deref: () =>
69+
| {
70+
getState: ReturnType<typeof vi.fn>
71+
getMcpHub?: () => { findServerNameBySanitizedName: (name: string) => string | undefined }
72+
}
73+
| undefined
7274
}
7375
say: ReturnType<typeof vi.fn>
7476
ask: ReturnType<typeof vi.fn>
7577
pushToolResultToUserContent: ReturnType<typeof vi.fn>
78+
getTaskMode: ReturnType<typeof vi.fn>
7679
}
7780

7881
describe("presentAssistantMessage - tool usage attribution", () => {
@@ -115,6 +118,7 @@ describe("presentAssistantMessage - tool usage attribution", () => {
115118
say: vi.fn().mockResolvedValue(undefined),
116119
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
117120
pushToolResultToUserContent: vi.fn(),
121+
getTaskMode: vi.fn().mockResolvedValue("code"),
118122
}
119123

120124
mockTask.pushToolResultToUserContent = vi
@@ -316,4 +320,71 @@ describe("presentAssistantMessage - tool usage attribution", () => {
316320
expect(TelemetryService.instance.captureToolUsage).not.toHaveBeenCalled()
317321
})
318322
})
323+
324+
describe("undefined provider state", () => {
325+
// Covers the `state ?? {}` fallback branch (line 347 of presentAssistantMessage.ts).
326+
// When providerRef.deref() returns undefined, state is undefined and the
327+
// destructure falls back to {}, so customModes / experiments / disabledTools
328+
// are all undefined. Tool validation must still use the task-local mode.
329+
it("falls back to empty state when provider is unavailable", async () => {
330+
mockTask.providerRef = { deref: () => undefined }
331+
mockTask.assistantMessageContent = [
332+
{
333+
type: "tool_use",
334+
id: "call_no_state",
335+
name: "read_file",
336+
params: { path: "test.ts" },
337+
nativeArgs: { path: "test.ts" },
338+
partial: false,
339+
},
340+
]
341+
342+
await presentAssistantMessage(mockTask as unknown as Task)
343+
344+
// validateToolUse must still be called with the task-local mode.
345+
const calls = vi.mocked(validateToolUse).mock.calls
346+
expect(calls.length).toBeGreaterThan(0)
347+
expect(calls[0][1]).toBe("code")
348+
// customModes falls back to [] (from the ?? {} path).
349+
expect(calls[0][2]).toEqual([])
350+
})
351+
})
352+
353+
describe("mode delegation regression", () => {
354+
// Regression for issue #1623.
355+
// Before the fix, validateToolUse received the shared provider mode instead
356+
// of the task-local mode, so a child delegated to "architect" mode would have
357+
// its tools validated against "orchestrator".
358+
it("passes the task-local mode to validateToolUse, not the provider mode", async () => {
359+
// Provider says "orchestrator"; task was delegated to "architect".
360+
mockTask.providerRef = {
361+
deref: () => ({
362+
getState: vi.fn().mockResolvedValue({
363+
mode: "orchestrator",
364+
customModes: [],
365+
}),
366+
}),
367+
}
368+
mockTask.getTaskMode = vi.fn().mockResolvedValue("architect")
369+
370+
mockTask.assistantMessageContent = [
371+
{
372+
type: "tool_use",
373+
id: "call_delegation",
374+
name: "read_file",
375+
params: { path: "test.ts" },
376+
nativeArgs: { path: "test.ts" },
377+
partial: false,
378+
},
379+
]
380+
381+
await presentAssistantMessage(mockTask as unknown as Task)
382+
383+
// The key assertion: task-local mode "architect" was passed, not "orchestrator".
384+
const calls = vi.mocked(validateToolUse).mock.calls
385+
expect(calls.length).toBeGreaterThan(0)
386+
expect(calls[0][0]).toBe("read_file")
387+
expect(calls[0][1]).toBe("architect")
388+
})
389+
})
319390
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe("presentAssistantMessage - Unknown Tool Handling", () => {
6060
}),
6161
}),
6262
},
63+
getTaskMode: vi.fn().mockResolvedValue("code"),
6364
say: vi.fn().mockResolvedValue(undefined),
6465
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
6566
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,10 @@ export async function presentAssistantMessage(cline: Task) {
344344

345345
// Fetch state early so it's available for toolDescription and validation
346346
const state = await cline.providerRef.deref()?.getState()
347-
const { mode, customModes, experiments: stateExperiments, disabledTools } = state ?? {}
347+
const { customModes, experiments: stateExperiments, disabledTools } = state ?? {}
348+
// Read the task-local mode, not the shared provider mode.
349+
// A delegated child task may run in a different mode than its parent.
350+
const taskMode = await cline.getTaskMode()
348351

349352
const toolDescription = (): string => {
350353
switch (block.name) {
@@ -617,7 +620,7 @@ export async function presentAssistantMessage(cline: Task) {
617620

618621
validateToolUse(
619622
block.name as ToolName,
620-
mode ?? defaultModeSlug,
623+
taskMode,
621624
customModes ?? [],
622625
toolRequirements,
623626
block.params,
@@ -924,7 +927,7 @@ export async function presentAssistantMessage(cline: Task) {
924927
}
925928

926929
const result = await customTool.execute(customToolArgs, {
927-
mode: mode ?? defaultModeSlug,
930+
mode: taskMode,
928931
task: cline,
929932
})
930933

0 commit comments

Comments
 (0)