Skip to content

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

fix(mcp): keep only the newest turn's state in the transcript follower

Jack Rudenko606187ecaf94
diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.ts
index 8a1e8854..8917f2a0 100644
--- a/packages/cli/src/pane/pane-session.ts
+++ b/packages/cli/src/pane/pane-session.ts
@@ -127,6 +127,8 @@ const POLL_BACKSTOP_MS = 1_000;
/** R3-M4: a `finishing` turn with a static screen for this many secondary windows. */
const TURN_END_MISSING_FACTOR = 3;
const MAX_ANOMALY_KEYS = 64;
+/** Settled answers kept for `turnAnswer` (owners read the current or the last one). */
+const KEPT_ANSWERS = 4;
export interface PaneBlock {
kind: "question" | "permission";
@@ -1157,6 +1159,8 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
private finishTurn(turn: SettledTurn, chatOffset: number | null, exitCode: number | null): void {
this.answers.set(turn.index, turn.answer);
+ // owners ask for the current or the last settled turn: keep a few, not the session's all
+ for (const k of this.answers.keys()) if (k <= turn.index - KEPT_ANSWERS) this.answers.delete(k);
this.lastCaptureSource = turn.captureSource;
this.activity = null;
let verdict: FinalVerdict | "continue";
diff --git a/packages/cli/src/pane/transcript-follower.test.ts b/packages/cli/src/pane/transcript-follower.test.ts
index ce00365a..bb7a0a15 100644
--- a/packages/cli/src/pane/transcript-follower.test.ts
+++ b/packages/cli/src/pane/transcript-follower.test.ts
@@ -699,6 +699,22 @@ describe("local commands", () => {
});
});
+describe("bounded state", () => {
+ test("opening a turn drops the previous turn's state, task file included", async () => {
+ const { initialFollowerState, openTurnState } = await import("./transcript-follower.js");
+ const state = initialFollowerState((p) => p);
+ for (let i = 1; i <= 50; i++)
+ openTurnState(state, {
+ index: i,
+ offset: i * 100,
+ witness: text(`turn ${i}`),
+ delivery: { file: `/t/turn-${i}.md`, text: "x".repeat(100_000) },
+ });
+ expect(state.turns).toHaveLength(1);
+ expect(state.turns[0]?.index).toBe(50);
+ });
+});
+
describe("byte-safe tail", () => {
test("a poll that splits a multi-byte character decodes the record exactly once, offsets in bytes", async () => {
const { appendFileSync, mkdtempSync, rmSync, writeFileSync } = await import("node:fs");
diff --git a/packages/cli/src/pane/transcript-follower.ts b/packages/cli/src/pane/transcript-follower.ts
index 67fb9596..894d669f 100644
--- a/packages/cli/src/pane/transcript-follower.ts
+++ b/packages/cli/src/pane/transcript-follower.ts
@@ -545,10 +545,16 @@ export function applySubagentRecord(
return state;
}
+/**
+ * Open turn `t`. Only the newest turn can ever receive a record (offsets only grow and the
+ * new turn's offset is taken after a poll), so the previous turn's state, which can hold a
+ * whole task file and every text block, is dropped here rather than kept for the session.
+ */
export function openTurnState(
state: FollowerState,
t: { index: number; offset: number; witness: Witness; delivery?: TurnDelivery | null }
): FollowerState {
+ state.turns.length = 0;
state.turns.push({
index: t.index,
offset: t.offset,