Skip to content

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

feat(mcp): expose assistant message ids and pane diagnostics on PaneSession

Jack Rudenko20a01049e5ae
diff --git a/packages/cli/src/pane/index.ts b/packages/cli/src/pane/index.ts
index 715b9742..a839c8a0 100644
--- a/packages/cli/src/pane/index.ts
+++ b/packages/cli/src/pane/index.ts
@@ -35,6 +35,7 @@ export {
} from "./pane-registry.js";
export {
type PaneBlock,
+ type PaneDiagnostics,
type PaneSession,
type PaneSessionOptions,
PaneStartError,
diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.ts
index 4fb7f403..c8d1285e 100644
--- a/packages/cli/src/pane/pane-session.ts
+++ b/packages/cli/src/pane/pane-session.ts
@@ -179,6 +179,21 @@ export type SendResult =
| { ok: true; queued: number }
| { ok: false; reason: "terminal" | "delivery_unavailable" | "unsupported_command" };
+/** What `get_diagnostics` reads from a live pane beyond the snapshot (§4.2). */
+export interface PaneDiagnostics {
+ /** the head of magmux's own stderr (≤ 8 KB) */
+ magmuxStderr: string;
+ /** assistant text before a file-delivered turn's task file was read (X-H4) */
+ preambleBytes: number;
+ /** read coverage of the current turn's task file; null for a typed turn or no turn */
+ readCoverage: {
+ linesTotal: number;
+ linesReturned: number;
+ complete: boolean;
+ reads: number;
+ } | null;
+}
+
export interface PaneSession {
readonly paneId: string;
readonly sockPath: string;
@@ -189,6 +204,9 @@ export interface PaneSession {
capture(sinceSeq?: number, opts?: { spans?: boolean }): CaptureResult | CaptureUnchanged;
send(text: string): SendResult;
turnAnswer(index: number): string;
+ /** main-chain assistant message ids the transcript has shown, first-seen order, each once */
+ assistantMessageIds(): readonly string[];
+ diagnostics(): PaneDiagnostics;
cancel(): { changed: boolean; state: SlotState };
expire(): { changed: boolean; state: SlotState };
reaped(): Promise<void>;
@@ -1356,6 +1374,26 @@ export class PaneSessionImpl implements PaneSession, RegisteredPane {
return cur && cur.index === index ? cur.assistantText.join("\n\n") : "";
}
+ assistantMessageIds(): readonly string[] {
+ return this.follower.assistantMessageIds();
+ }
+
+ diagnostics(): PaneDiagnostics {
+ const cur = this.follower.view().current;
+ return {
+ magmuxStderr: this.stderrHead,
+ preambleBytes: cur?.preambleBytes ?? 0,
+ readCoverage: cur?.delivery
+ ? {
+ linesTotal: cur.delivery.lines,
+ linesReturned: cur.readCoverage.linesReturned,
+ complete: cur.readCoverage.complete,
+ reads: cur.readCoverage.reads,
+ }
+ : null,
+ };
+ }
+
cancel(): { changed: boolean; state: SlotState } {
return this.endBy("cancel");
}
diff --git a/packages/cli/src/pane/transcript-follower.test.ts b/packages/cli/src/pane/transcript-follower.test.ts
index e6a86064..9ef93c4a 100644
--- a/packages/cli/src/pane/transcript-follower.test.ts
+++ b/packages/cli/src/pane/transcript-follower.test.ts
@@ -150,6 +150,21 @@ describe("usage and tool calls", () => {
for (const b of r.message.content) if (b.type === "tool_use") ids.add(b.id);
expect(without.toolCalls).toBe(ids.size);
});
+
+ test("assistantMessageIds lists each main-chain message id once, in order, never a subagent's", () => {
+ const n = "tools-session";
+ const r = run(n, [], {
+ subagents: join(FIXTURES, "transcripts", "tools-session", "subagents"),
+ });
+ const expected: string[] = [];
+ for (const rec of transcriptRecords(n))
+ if (rec.type === "assistant" && !rec.isSidechain && !expected.includes(rec.message.id))
+ expected.push(rec.message.id);
+ const lines = transcriptRecords(n).filter((rec) => rec.type === "assistant").length;
+ expect(expected.length).toBeGreaterThan(1);
+ expect(expected.length).toBeLessThan(lines); // one message spans several records
+ expect([...r.follower.assistantMessageIds()]).toEqual(expected);
+ });
});
describe("Stop hooks, provenHookless and turn_duration", () => {
diff --git a/packages/cli/src/pane/transcript-follower.ts b/packages/cli/src/pane/transcript-follower.ts
index 2cab1143..94646680 100644
--- a/packages/cli/src/pane/transcript-follower.ts
+++ b/packages/cli/src/pane/transcript-follower.ts
@@ -146,6 +146,9 @@ export interface FollowerState {
turns: TurnState[];
usage: Map<string, { in: number; out: number }>;
toolUseIds: Set<string>;
+ /** main-chain assistant message ids, in first-seen order, each once (the monitor's replies) */
+ mainAssistantIds: string[];
+ mainAssistantSeen: Set<string>;
session: SessionFacts;
records: number;
resolvePath: (p: string) => string;
@@ -158,6 +161,8 @@ export function initialFollowerState(
turns: [],
usage: new Map(),
toolUseIds: new Set(),
+ mainAssistantIds: [],
+ mainAssistantSeen: new Set(),
session: { stopHooksSeen: false, provenHookless: false },
records: 0,
resolvePath,
@@ -423,6 +428,15 @@ function acceptIfWitness(t: TurnState, r: Rec, offset: number): void {
t.lastChatOffset = offset;
}
+/** One main-chain assistant message id, once: Claude Code writes one record per content block. */
+function noteMainAssistant(state: FollowerState, r: Rec): void {
+ const id = r.type === "assistant" ? r.message?.id : undefined;
+ if (typeof id !== "string" || !id) return;
+ if (state.mainAssistantSeen.has(id)) return;
+ state.mainAssistantSeen.add(id);
+ state.mainAssistantIds.push(id);
+}
+
/** Pure: fold one main-transcript record at byte `offset` into the state. */
export function applyRecord(
state: FollowerState,
@@ -433,6 +447,7 @@ export function applyRecord(
state.records++;
recordUsage(state, r);
if (r.isSidechain) return state;
+ noteMainAssistant(state, r);
if (r.type === "system" && r.subtype === "stop_hook_summary") {
state.session.stopHooksSeen = true;
state.session.provenHookless = false;
@@ -681,6 +696,11 @@ export class TranscriptFollower {
this.settled++;
}
+ /** Main-chain assistant message ids seen so far, in first-seen order, each once. */
+ assistantMessageIds(): readonly string[] {
+ return this.state.mainAssistantIds;
+ }
+
markSavingOff(): void {
this.savingOff = true;
}