Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(mcp): map the awaiting_permission channel event to input_required
Jack Rudenko415b76b29dcd
diff --git a/packages/cli/src/channel/event-task-status.test.ts b/packages/cli/src/channel/event-task-status.test.tsnew file mode 100644index 00000000..c6c6dca9--- /dev/null+++ b/packages/cli/src/channel/event-task-status.test.ts@@ -0,0 +1,43 @@+/**+ * Every channel event projects onto SEP-1686's `TaskStatus` explicitly. A missing+ * key does not throw: `mapEventToTaskStatus` falls through to "working", which is how+ * `timeout` once reported a dead session as alive and how `awaiting_permission`+ * reported a session waiting on its caller as busy. This walks the runtime list of+ * event types, so a new type needs a row here AND a key in the map.+ */++import { describe, expect, test } from "bun:test";+import { mapEventToTaskStatus } from "../mcp-server.js";+import { CHANNEL_EVENT_TYPES, type ChannelEventType } from "./types.js";++type TaskStatus = ReturnType<typeof mapEventToTaskStatus>;++const EXPECTED = {+ starting: "working",+ running: "working",+ tool_executing: "working",+ waiting_for_input: "input_required",+ awaiting_permission: "input_required",+ finishing: "working",+ completed: "completed",+ failed: "failed",+ cancelled: "cancelled",+ timeout: "failed",+} as const satisfies Record<ChannelEventType, TaskStatus>;++describe("EVENT_TO_TASK_STATUS", () => {+ for (const event of CHANNEL_EVENT_TYPES) {+ test(`${event} → ${EXPECTED[event]}`, () => {+ expect(mapEventToTaskStatus(event)).toBe(EXPECTED[event]);+ });+ }++ test("awaiting_permission asks the caller for input; it is not 'working'", () => {+ expect(mapEventToTaskStatus("awaiting_permission")).toBe("input_required");+ });++ test("the list and the table name the same events", () => {+ const listed: string[] = [...CHANNEL_EVENT_TYPES].sort();+ expect(listed).toEqual(Object.keys(EXPECTED).sort());+ });+});diff --git a/packages/cli/src/channel/stream-json-reducer.ts b/packages/cli/src/channel/stream-json-reducer.tsindex ca837231..77ac2b1e 100644--- a/packages/cli/src/channel/stream-json-reducer.ts+++ b/packages/cli/src/channel/stream-json-reducer.ts@@ -47,7 +47,7 @@ import {STREAM_JSON_EVENT_TYPES,createAssistantTextCapture,} from "../team-stream-capture.js";-import type { ChannelEventType, ReducerCallback, ReducerEvent } from "./types.js";+import type { ReducerCallback, ReducerEvent, SessionStatus } from "./types.js";/*** Frame types that exist only to prove liveness.@@ -83,12 +83,7 @@ function reachesAnswer(return keepUnrecognizedJson;}-const TERMINAL_STATES: readonly ChannelEventType[] = [- "completed",- "failed",- "cancelled",- "timeout",-];+const TERMINAL_STATES: readonly SessionStatus[] = ["completed", "failed", "cancelled", "timeout"];/*** Which states may follow which. Absent from a row ⇒ illegal.@@ -97,7 +92,7 @@ const TERMINAL_STATES: readonly ChannelEventType[] = [* supervisor (timeout, cancel, exit classification) has decided how a session* ended, nothing may revise it.*/-const LEGAL_TRANSITIONS: Record<ChannelEventType, readonly ChannelEventType[]> = {+const LEGAL_TRANSITIONS: Record<SessionStatus, readonly SessionStatus[]> = {starting: ["running", "tool_executing", "waiting_for_input", "finishing", ...TERMINAL_STATES],running: ["tool_executing", "waiting_for_input", "finishing", ...TERMINAL_STATES],tool_executing: ["running", "waiting_for_input", "finishing", ...TERMINAL_STATES],@@ -261,7 +256,7 @@ export function labelForLine(line: string): string | null {}export class StreamJsonReducer {- private _state: ChannelEventType = "starting";+ private _state: SessionStatus = "starting";private disposed = false;/** Incomplete trailing line held until its newline arrives. */@@ -304,7 +299,7 @@ export class StreamJsonReducer {}}- get state(): ChannelEventType {+ get state(): SessionStatus {return this._state;}get claudeSessionId(): string | null {@@ -450,7 +445,7 @@ export class StreamJsonReducer {* reached, and the transition table makes those absorbing — a later caller* cannot revise a verdict that has already been recorded.*/- settle(state: ChannelEventType, opts?: { content?: string }): void {+ settle(state: SessionStatus, opts?: { content?: string }): void {if (this.disposed) return;this.resetToolBatch();this.transition(state, { content: opts?.content });@@ -707,7 +702,7 @@ export class StreamJsonReducer {});}- private transition(newState: ChannelEventType, extra?: Partial<ReducerEvent>): void {+ private transition(newState: SessionStatus, extra?: Partial<ReducerEvent>): void {const prev = this._state;if (prev === newState) {diff --git a/packages/cli/src/channel/types.ts b/packages/cli/src/channel/types.tsindex d39cdc92..105d0282 100644--- a/packages/cli/src/channel/types.ts+++ b/packages/cli/src/channel/types.ts@@ -18,10 +18,31 @@ export type SessionStatus =| "timeout";/**- * The values the channel wire's `event` field may carry. Identical to- * `SessionStatus` — the record and the wire agree.+ * Every value the channel wire's `event` field may carry, as a runtime list so a+ * test can walk it (`event-task-status.test.ts` checks each one against+ * `EVENT_TO_TASK_STATUS`). `ChannelEventType` is derived from it.+ */+export const CHANNEL_EVENT_TYPES = [+ "starting",+ "running",+ "tool_executing",+ "waiting_for_input",+ /** A tool call waits on Claude Code's permission dialog; answered with `send_input`. */+ "awaiting_permission",+ "finishing",+ "completed",+ "failed",+ "cancelled",+ "timeout",+] as const;++/**+ * The values the channel wire's `event` field may carry: every `SessionStatus`, plus+ * `awaiting_permission`, which a session reports on the wire while a permission+ * dialog is open (the interactive pane transport; `SessionStatus` is retired with the+ * stream-json transport).*- * They did not always. This alias was `Exclude<SessionStatus, "timeout">`,+ * The record and the wire did not always agree. This alias was `Exclude<SessionStatus, "timeout">`,* because `EVENT_TO_TASK_STATUS` (mcp-server.ts) maps the event enum onto* SEP-1686's 5-value `TaskStatus` and had **no `"timeout"` key**: it fell* through to `?? "working"`, so emitting `"timeout"` reported a dead session as@@ -33,7 +54,13 @@ export type SessionStatus =* member and `failed` is its only honest projection — so the divergence is gone* and `ReducerEvent` no longer carries a `sessionStatus` override.*/-export type ChannelEventType = SessionStatus;+export type ChannelEventType = (typeof CHANNEL_EVENT_TYPES)[number];++// Compile-time: the list holds every SessionStatus (a status missing from it would+// be emitted on the wire without a SEP-1686 projection test).+type _MissingStatus = Exclude<SessionStatus, ChannelEventType>;+const _everyStatusIsAnEvent: [_MissingStatus] extends [never] ? true : never = true;+void _everyStatusIsAnEvent;export interface SessionInfo {sessionId: string;@@ -196,10 +223,14 @@ export interface ChannelEvent {extraMeta?: Record<string, string>;}-/** One state change out of the stream-json reducer. */+/**+ * One state change out of the stream-json reducer. Its states are `SessionStatus`:+ * print mode has no permission dialog, so the reducer never emits+ * `awaiting_permission`.+ */export interface ReducerEvent {- previousState: ChannelEventType;- newState: ChannelEventType;+ previousState: SessionStatus;+ newState: SessionStatus;content?: string;toolName?: string;toolCount?: number;diff --git a/packages/cli/src/mcp-server.ts b/packages/cli/src/mcp-server.tsindex 29cb68bf..4fdd314f 100644--- a/packages/cli/src/mcp-server.ts+++ b/packages/cli/src/mcp-server.ts@@ -24,6 +24,7 @@ import { assertAgentAvailable } from "./agent-availability.js";import { prehydrateCredentialsForSpawn } from "./auth/credentials/prehydrate.js";import { installWireTap, watchNotificationResult, wrapStateChange } from "./channel/diagnostics.js";import { SessionManager } from "./channel/index.js";+import type { ChannelEventType } from "./channel/types.js";import { TOOL_USE_ID_META_KEY, proveParentForCall } from "./channel/parent-proof.js";import { isSubscriptionProvider } from "./handlers/shared/remote-provider-types.js";import {@@ -2030,18 +2031,24 @@ function resolveToolGroups(mode: string): Set<ToolGroup> {// Migration plan: ai-docs/sessions/.../sep-1686-migration-schema.mdtype TaskStatus = "working" | "input_required" | "completed" | "failed" | "cancelled";-const EVENT_TO_TASK_STATUS = new Map<string, TaskStatus>([- ["starting", "working"],- ["running", "working"],- ["tool_executing", "working"],- ["waiting_for_input", "input_required"],+// A `Record` over `ChannelEventType`, so an event type without a projection is a+// compile error rather than a silent `"working"` from the fall-through below.+const TASK_STATUS_BY_EVENT: Record<ChannelEventType, TaskStatus> = {+ starting: "working",+ running: "working",+ tool_executing: "working",+ waiting_for_input: "input_required",+ // A permission dialog is open and only the caller's `send_input` moves the+ // session on. It had no key, so it fell through to "working": a SEP-1686+ // consumer polled a session that was waiting on it as if it were busy.+ awaiting_permission: "input_required",// Explicit although the fall-through below gives the same value: a missing// key is how the timeout projection hid (see the `timeout` entry). The final// turn ended and the child is exiting; nothing is asked of the caller.- ["finishing", "working"],- ["completed", "completed"],- ["failed", "failed"],- ["cancelled", "cancelled"],+ finishing: "working",+ completed: "completed",+ failed: "failed",+ cancelled: "cancelled",// The key whose ABSENCE forced the channel wire to lie. `mapEventToTaskStatus`// falls through to `?? "working"`, so a session killed by its own timeout was// reported to a SEP-1686 consumer as still working — which is why the timeout@@ -2049,8 +2056,10 @@ const EVENT_TO_TASK_STATUS = new Map<string, TaskStatus>([// `"timeout"`. With this key present `ChannelEventType` is the full// `SessionStatus` and the timeout emits its own event; SEP-1686 has no// `timeout` member, and `failed` is the only honest projection of it.- ["timeout", "failed"],-]);+ timeout: "failed",+};++const EVENT_TO_TASK_STATUS = new Map<string, TaskStatus>(Object.entries(TASK_STATUS_BY_EVENT));// Exported ONLY so the regression guard can call it instead of grepping this// file's source text. The behaviour worth guarding is the fall-through below: a