Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Plan: no verdict while two ExitPlanMode calls run

razorback16d01fb114c3fc
diff --git a/hooks/lib/state.ts b/hooks/lib/state.ts
index 5c3e414..4b8574e 100644
--- a/hooks/lib/state.ts
+++ b/hooks/lib/state.ts
@@ -202,11 +202,14 @@ export const S = {
stepsLive: 0,
// The plan (plan.tsx): the main loop's running turn (`turn.start`), the
// verdict the mod's question gave for the PermissionRequest that follows
- // (null: none, the engine's dialog asks), the question asked now ('': none),
- // and the `code` command's path (null: not looked up yet; '': none), and
- // on Linux a GUI editor that waits (its path and flag; [] none).
+ // (null: none, the engine's dialog asks), the main loop's ExitPlanMode
+ // calls in flight (a verdict is used only while one runs), the question
+ // asked now ('': none), and the `code` command's path (null: not looked
+ // up yet; '': none), and on Linux a GUI editor that waits (its path and
+ // flag; [] none).
mainTurnId: '',
planVerdict: null as PlanVerdict | null,
+ plansLive: 0,
planAsking: '',
planCode: null as string | null,
planGui: null as string[] | null,
diff --git a/hooks/register.tsx b/hooks/register.tsx
index 919f18a..104eb22 100644
--- a/hooks/register.tsx
+++ b/hooks/register.tsx
@@ -1157,56 +1157,66 @@ export const register: Register = on => {
// behind it). The person's words deny the call (Claude revises the plan);
// a verdict waits for the PermissionRequest below, and is dropped once the
// call resolves, so it never reaches a later call. No env, no plan, or a
- // failure: no verdict, and the engine's dialog asks.
+ // failure: no verdict, and the engine's dialog asks. The engine may run
+ // two calls at once, and a PermissionRequest does not say which call it
+ // is for: a call that begins while another runs asks nothing (the
+ // engine's dialog asks), and no verdict is used while two run.
on('tool.call', { tool: 'ExitPlanMode' }, async ($, e, next) => {
if (e.agentId) {
subagentPlans.add(e.tool_use_id)
return next(e)
}
- S.planVerdict = null
- let verdict: PlanVerdict | null = null
+ S.plansLive++
try {
- const ready = await planEnv($)
- const callPlan = ready ? planInputOf(e) : ''
- if (ready && callPlan) {
- const id = e.tool_use_id
- const file = planFileOf(e)
- const plan = await currentPlan($, file, callPlan)
- // Always: the row's input is the message's copy, read before an Edit in the same message.
- editedPlans.set(id, plan)
- refresh($)
- const outcome = await decidePlan($, {
- plan, callPlan, file, isMac: ready.isMac, isRemote: S.isRemote,
- onEdited: text => {
- editedPlans.set(id, text)
- refresh($)
- },
- })
- if (outcome.kind === 'feedback') return { deny: outcome.message }
- verdict = outcome.verdict
+ if (S.plansLive > 1) return await next(e)
+ S.planVerdict = null
+ let verdict: PlanVerdict | null = null
+ try {
+ const ready = await planEnv($)
+ const callPlan = ready ? planInputOf(e) : ''
+ if (ready && callPlan) {
+ const id = e.tool_use_id
+ const file = planFileOf(e)
+ const plan = await currentPlan($, file, callPlan)
+ // Always: the row's input is the message's copy, read before an Edit in the same message.
+ editedPlans.set(id, plan)
+ refresh($)
+ const outcome = await decidePlan($, {
+ plan, callPlan, file, isMac: ready.isMac, isRemote: S.isRemote,
+ onEdited: text => {
+ editedPlans.set(id, text)
+ refresh($)
+ },
+ })
+ if (outcome.kind === 'feedback') return { deny: outcome.message }
+ verdict = outcome.verdict
+ }
+ } catch {
+ verdict = null
}
- } catch {
- verdict = null
- }
- S.planVerdict = verdict
- const turnId = S.mainTurnId
- let got: Awaited<ReturnType<typeof next>>
- try {
- got = await next(e)
+ S.planVerdict = verdict
+ const turnId = S.mainTurnId
+ let got: Awaited<ReturnType<typeof next>>
+ try {
+ got = await next(e)
+ } finally {
+ S.planVerdict = null
+ }
+ // The call has run: a failure to schedule changes nothing of its result.
+ try {
+ if (verdict?.kind === 'allow' && verdict.clear && got && typeof got === 'object' && !('deny' in got)) scheduleClear($, verdict.plan, turnId)
+ } catch {}
+ return got
} finally {
- S.planVerdict = null
+ S.plansLive--
}
- // The call has run: a failure to schedule changes nothing of its result.
- try {
- if (verdict?.kind === 'allow' && verdict.clear && got && typeof got === 'object' && !('deny' in got)) scheduleClear($, verdict.plan, turnId)
- } catch {}
- return got
}).catch(($, e, next) => next(e))
// The PermissionRequest of that call: Intelligent UI's verdict at once (the
- // engine's dialog never shows), else the engine's flow.
+ // engine's dialog never shows), else the engine's flow (also while two
+ // calls run: the verdict may be the other call's).
on('classic.PermissionRequest', async ($, e, next) => {
- if (e.tool_name !== 'ExitPlanMode' || e.agent_id || !S.planVerdict) return next(e)
+ if (e.tool_name !== 'ExitPlanMode' || e.agent_id || !S.planVerdict || S.plansLive !== 1) return next(e)
const verdict = S.planVerdict
S.planVerdict = null
return { decision: decisionOf(verdict, e.tool_input) }
diff --git a/tests/plan.test.ts b/tests/plan.test.ts
index 19b2349..68c582d 100644
--- a/tests/plan.test.ts
+++ b/tests/plan.test.ts
@@ -153,6 +153,7 @@ function world($: any, on: any, o: {
raise?: boolean
onAsk?: () => Promise<void>
hold?: Promise<void>
+ before?: (id: string) => Promise<void>
} = {}) {
mock.store(on)
const clock = mock.clock(on)
@@ -236,6 +237,8 @@ function world($: any, on: any, o: {
})
on('tool.call', { tool: 'ExitPlanMode' }, async (_: any, e: any) => {
if (o.raise === false) return { result: { plan: e.plan, isAgent: false, filePath: FILE } }
+ // The engine's own work before PermissionRequest (PreToolUse hooks, tool.check).
+ await o.before?.(e.tool_use_id)
const got: any = await $.classic.PermissionRequest({
tool_name: 'ExitPlanMode', tool_input: { plan: e.plan, planFilePath: e.planFilePath }, permission_mode: 'plan',
...(e.agentId ? { agent_id: e.agentId } : {}),
@@ -577,6 +580,82 @@ test('a verdict whose PermissionRequest never came does not reach a later call',
expect(w.engineAsks()).toBe(1)
})
+// ExitPlanMode is concurrency safe: two calls may run at once, and a
+// PermissionRequest does not say which call it is for.
+const ticks = async () => {
+ for (let i = 0; i < 200; i++) await Promise.resolve()
+}
+
+test('two calls at once: the second asks nothing, and no verdict is used while both run', async ($, on) => {
+ const gates: Record<string, () => void> = {}
+ const waits: Record<string, Promise<void>> = { A: new Promise(r => (gates.A = r)), B: new Promise(r => (gates.B = r)) }
+ const w = world($, on, { answers: [null, PLAN_AUTO], before: id => waits[id] ?? Promise.resolve() })
+ await ready($)
+ // A is rejected (Esc), and waits for its PermissionRequest; B begins.
+ const a = exitPlan($, { tool_use_id: 'A' })
+ await ticks()
+ const b = exitPlan($, { tool_use_id: 'B' })
+ await ticks()
+ expect(w.asked.length).toBe(1)
+ gates.A!()
+ await ticks()
+ gates.B!()
+ await a.catch(() => undefined)
+ await b.catch(() => undefined)
+ // A's verdict reaches no call: the engine's dialog asks for each.
+ expect(w.decisions).toEqual([undefined, undefined])
+ expect(w.engineAsks()).toBe(2)
+ // Both ended: a call on its own is asked about and its verdict used again.
+ await exitPlan($, { tool_use_id: 'C' })
+ expect(w.asked.length).toBe(2)
+ expect(w.decisions[2]).toEqual({ behavior: 'allow', updatedInput: { plan: PLAN, planFilePath: FILE }, updatedPermissions: setMode('acceptEdits') })
+ expect(w.engineAsks()).toBe(2)
+})
+
+test('a call that begins while the question is open asks nothing; once it ends, the verdict is used', async ($, on) => {
+ let second: Promise<unknown> | null = null
+ const w = world($, on, {
+ answers: [PLAN_AUTO],
+ onAsk: async () => {
+ if (second) return
+ second = exitPlan($, { tool_use_id: 'B' })
+ await second
+ },
+ })
+ await ready($)
+ const got: any = await exitPlan($, { tool_use_id: 'A' })
+ expect(second).not.toBe(null)
+ expect(w.asked.length).toBe(1)
+ // B: the engine's dialog; A: the verdict, as B ended before A's PermissionRequest.
+ expect(w.decisions).toEqual([undefined, { behavior: 'allow', updatedInput: { plan: PLAN, planFilePath: FILE }, updatedPermissions: setMode('acceptEdits') }])
+ expect(w.engineAsks()).toBe(1)
+ expect(got.result.plan).toBe(PLAN)
+})
+
+test('a call that begins while the question is open and still runs at the PermissionRequest: the engine asks for both', async ($, on) => {
+ let release!: () => void
+ const gate = new Promise<void>(r => (release = r))
+ let second: Promise<unknown> | null = null
+ const w = world($, on, {
+ answers: [null],
+ before: id => (id === 'B' ? gate : Promise.resolve()),
+ onAsk: async () => {
+ if (second) return
+ second = exitPlan($, { tool_use_id: 'B' })
+ await ticks()
+ },
+ })
+ await ready($)
+ // A is rejected (Esc) while B runs: the deny is not used, the engine asks.
+ await exitPlan($, { tool_use_id: 'A' })
+ expect(w.decisions).toEqual([undefined])
+ release()
+ await second
+ expect(w.asked.length).toBe(1)
+ expect(w.decisions).toEqual([undefined, undefined])
+ expect(w.engineAsks()).toBe(2)
+})
+
test('the line above the dialog is for the Intelligent UI question alone', async ($, on) => {
let during: any = null
let other: any = null