Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(mcp): decide a turn settled inside send_input as interactive

Jack Rudenko75f7f0a6e4a1
diff --git a/packages/cli/src/channel/session-manager.test.ts b/packages/cli/src/channel/session-manager.test.ts
index 4b888508..fe843ee5 100644
--- a/packages/cli/src/channel/session-manager.test.ts
+++ b/packages/cli/src/channel/session-manager.test.ts
@@ -12,7 +12,16 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { type ChildProcess, spawn } from "node:child_process";
-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { createHash } from "node:crypto";
+import {
+ appendFileSync,
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
@@ -27,6 +36,7 @@ import {
makePaneTestEnv,
waitNoOrphans,
} from "../pane/test-helpers/hermetic-env.js";
+import { transcriptRecords } from "../pane/test-helpers/transcript-fixtures.js";
import { projectsDir, transcriptPathFor } from "../session/session-discovery.js";
import {
SessionManager,
@@ -658,6 +668,34 @@ describe.skipIf(!MAGMUX)(
T_PANE
);
+ test(
+ "send_input whose own step settles the one-shot turn: the session goes interactive and the text is turn 2",
+ async () => {
+ const manager = makeManager();
+ const id = await create(manager, { model: "fake-td_withheld", prompt: "first" });
+ // the answer is in, its turn_duration is not: the turn waits ("finishing")
+ await waitUntil(() => manager.getSession(id).activity === "finishing", 15_000);
+ await Bun.sleep(800); // past the 500 ms corroboration window of that answer
+ // Claude Code writes the end-of-turn record; the send arrives before any poll of ours
+ const td = transcriptRecords("tools-session")[29] as Record<string, unknown>;
+ expect(td.subtype).toBe("turn_duration");
+ appendFileSync(manager.getSession(id).transcriptPath, `${JSON.stringify(td)}\n`);
+ expect(manager.sendInput(id, "second")).toEqual({ success: true, queued: 0 });
+ await waitUntil(
+ () =>
+ manager.getSession(id).turnsCompleted === 2 &&
+ manager.getSession(id).state === "AWAITING_INPUT",
+ 15_000
+ );
+ const info = manager.getSession(id);
+ expect(info.shape).toBe("interactive");
+ expect(manager.getOutput(id).output).toContain(
+ `ANSWER fake-td_withheld ${createHash("sha1").update("second").digest("hex").slice(0, 8)}`
+ );
+ },
+ T_PANE
+ );
+
test(
"getOutput returns the answer prose, never raw records; tail_lines returns the last N",
async () => {
diff --git a/packages/cli/src/channel/session-manager.ts b/packages/cli/src/channel/session-manager.ts
index a01a5117..9a023394 100644
--- a/packages/cli/src/channel/session-manager.ts
+++ b/packages/cli/src/channel/session-manager.ts
@@ -1028,7 +1028,9 @@ export class SessionManager {
/** The verdict of a settled turn: one-shot → classified; interactive → continue (D20). */
private decide(entry: SessionEntry, turn: SettledTurn): FinalVerdict | "continue" {
this.appendAnswer(entry, turn.index, turn.answer);
- if (entry.info.shape === "one-shot") {
+ // The pane's shape at the settle, not entry.info's copy: a send_input whose own step
+ // settles the turn has converted the session before sendInput could update the copy.
+ if (turn.shape === "one-shot") {
const v = classifyRunOutput({
answer: turn.answer,
apiError: turn.apiError,
diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.ts
index c7204bc9..2478679e 100644
--- a/packages/cli/src/pane/pane-session.ts
+++ b/packages/cli/src/pane/pane-session.ts
@@ -1129,6 +1129,7 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
stopReason: decision.stopReason,
captureSource: "transcript",
settledBy: decision.by,
+ shape: this.shape,
delivery: {
mode: d && d.mode !== "refused" ? d.mode : "typed",
linesTotal: t.delivery ? t.delivery.lines : null,
@@ -1228,6 +1229,7 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
stopReason: null,
captureSource: "screen",
settledBy: "screen",
+ shape: this.shape,
delivery: {
mode: d && d.mode !== "refused" ? d.mode : "typed",
linesTotal: null,
@@ -1408,7 +1410,14 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
if (this.shape === "one-shot") this.shape = "interactive";
this.queue.push(text);
this.escapeArmed = true;
- this.step(() => this.evaluate());
+ // Read the transcript first: a turn whose end record already landed settles HERE, and
+ // its owner must see the converted shape (SettledTurn.shape) and the queued text.
+ this.step(() => {
+ this.pollFollower(true);
+ this.evaluate();
+ });
+ // Never report a queued prompt for a session this very step ended.
+ if (isTerminalPhase(this.phase)) return { ok: false, reason: "terminal" };
return { ok: true, queued: this.queue.length };
}
diff --git a/packages/cli/src/pane/test-helpers/fake-interactive-child.ts b/packages/cli/src/pane/test-helpers/fake-interactive-child.ts
index 84025dd2..5cbb3c41 100755
--- a/packages/cli/src/pane/test-helpers/fake-interactive-child.ts
+++ b/packages/cli/src/pane/test-helpers/fake-interactive-child.ts
@@ -450,7 +450,13 @@ function markerScenario(content: string): string {
}
/** Blocking scenarios block turn 1 only; the prompt sent after the decline is answered. */
-const FIRST_TURN_ONLY = new Set(["ask_user", "ask_user_send", "interrupt_td", "permission"]);
+const FIRST_TURN_ONLY = new Set([
+ "ask_user",
+ "ask_user_send",
+ "interrupt_td",
+ "permission",
+ "td_withheld",
+]);
function effectiveScenario(content: string): string {
if (scenario === "marker") return markerScenario(content);
@@ -510,6 +516,11 @@ async function runScenario(name: string, d: Delivered): Promise<void> {
rec.stopSummary();
showHistory(`⏺ ${answerFor(c)}`, finishedRow());
return;
+ case "td_withheld":
+ // the answer, but no turn_duration: the test appends it itself, at the moment it chooses
+ rec.text(answerFor(c));
+ showHistory(`⏺ ${answerFor(c)}`, finishedRow());
+ return;
case "quiet_no_summary":
rec.text(answerFor(c));
showHistory(`⏺ ${answerFor(c)}`, finishedRow());
diff --git a/packages/cli/src/pane/types.ts b/packages/cli/src/pane/types.ts
index 0aee37f3..095cd764 100644
--- a/packages/cli/src/pane/types.ts
+++ b/packages/cli/src/pane/types.ts
@@ -17,6 +17,9 @@ export interface SettledTurn {
stopReason: string | null;
captureSource: "transcript" | "screen" | "none";
settledBy: SettledBy | "screen";
+ /** the session's shape at the moment of the settle: a send converts one-shot to interactive
+ * before its own evaluation, so an owner reads this, never a copy it holds */
+ shape: "one-shot" | "interactive";
/** owners map `complete === false` → prompt_not_read */
delivery: {
mode: Delivery["mode"];