A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, performance] Fetch session history once per switch (#2432)
Seth Karten236ca7b23482
diff --git a/packages/coding-agent/.changes/daemon-switch-single-history-fetch.md b/packages/coding-agent/.changes/daemon-switch-single-history-fetch.mdnew file mode 100644index 0000000000..84e34532c4--- /dev/null+++ b/packages/coding-agent/.changes/daemon-switch-single-history-fetch.md@@ -0,0 +1,2 @@+- Fixed session switches fetching the full transcript twice: a switch consumes the streamed replacement snapshot instead of refetching, so the history crosses the wire once when switching to a large session, while a switch whose replacement stream fails or never lands still reloads the transcript.+- Fixed a session switch reading a replacement snapshot streamed for another session (an earlier switch's late stream, or another client's switch on the same daemon session) as its own transcript, and made the switch wait end when the connection closes terminally or a re-attach resync delivers the requested session, instead of relaying its timeout.diff --git a/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts b/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.tsindex cb12325f9c..112c661063 100644--- a/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts+++ b/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts@@ -1,4 +1,6 @@import { randomUUID } from "node:crypto";+import { realpathSync } from "node:fs";+import { basename, isAbsolute, resolve } from "node:path";import type { AgentMessage, ThinkingLevel } from "@earendil-works/pi-agent-core";import type { ImageContent, ServiceTier, Transport } from "@earendil-works/pi-ai";import { appendRotatingLog, getAgentLogPath, getDaemonLogPath } from "../../config.js";@@ -130,6 +132,28 @@ interface DaemonRestartRestoreOptions {updateRestart?: boolean;}+/**+ * The streamed replacement snapshot a switchSession call waits on, so a later+ * getInitialSnapshot reads the applied cache instead of refetching the+ * transcript.+ */+interface ReplacementSnapshotExpectation {+ promise: Promise<void>;+ resolve: () => void;+ reject: (error: Error) => void;+ /** Session file the switch asked for; other sessions' replacements must not satisfy it. */+ targetSessionFile?: string;+ /** Set when a newer switch displaced this wait: its cleanup must not invalidate the newer switch's snapshot. */+ superseded?: boolean;+}++/**+ * A transport loss abandoned the in-flight snapshot streams. The connection+ * re-attaches and re-syncs the session, so a switch waiting on a replacement must+ * not treat an abandoned stream as a failed replacement.+ */+class SnapshotTransferAbandonedError extends Error {}+export const DAEMON_REFINE_REQUEST_TIMEOUT_MS = 10 * 60 * 1000;const DAEMON_LONG_RUNNING_REQUEST_TIMEOUT_MS = 24 * 60 * 60 * 1000;export const DAEMON_RECONNECT_TIMEOUT_MS = 60_000;@@ -299,6 +323,8 @@ export class DaemonAgentConnection implements AgentConnection {private readonly completedSnapshots = new Map<string, DaemonSessionSnapshot>();private readonly pendingReattachActiveSessionIds = new Set<string>();private readonly snapshotRecoveryPromises = new Map<string, Promise<void>>();+ /** Latest switch's not-yet-settled replacement snapshot wait; newer switches replace it. */+ private pendingReplacementSnapshot: ReplacementSnapshotExpectation | undefined;private readonly ignoredSnapshotIds = new Set<string>();private rosterStore: AgentsViewRosterStore | undefined;private reconnectPromise?: Promise<void>;@@ -342,7 +368,10 @@ export class DaemonAgentConnection implements AgentConnection {if (!directSessionSurvives) {this.sessionInputPauses.clear();this.sessionInputPauseGeneration++;- this.rejectSnapshotAssemblies(error);+ // The stream died with the transport. A waiting switch is not failed here:+ // a recoverable close re-attaches and its resync snapshot settles the wait,+ // while the terminal paths below abort it.+ this.rejectSnapshotAssemblies(new SnapshotTransferAbandonedError(error.message));}if (this.initialAttachPending) {// attach() owns failure handling until the initial attach settles.@@ -355,6 +384,7 @@ export class DaemonAgentConnection implements AgentConnection {// A lost direct link invalidates the fence (holders learn via the generation bump) yet the session falls back.if (invalidatedInputPause && !(error instanceof DaemonDirectTransportClosedError)) {this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(error);void this.emit({type: "closed",error: "Daemon connection closed while session input was paused; the fence was invalidated.",@@ -366,11 +396,15 @@ export class DaemonAgentConnection implements AgentConnection {if (closeReason === "shutdown") {if (this.updateRestartPending) {// An update-restart recovery already owns the transport; this close joins it.+ // The recovery re-attaches and re-syncs, so in-flight snapshot streams stay+ // alive for the re-sync to satisfy.void this.reconnectAfterUpdate();return;}if (this.shutdownReconnectFailed) {+ // Terminal: nothing will re-sync, so abandoned snapshot streams reject their waiters.this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(error);void this.emit({ type: "closed", error: this.formatDaemonSessionClosedError("shutdown") });return;}@@ -395,6 +429,7 @@ export class DaemonAgentConnection implements AgentConnection {return;}this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(error);void this.emit({ type: "closed", error: this.formatDaemonConnectionClosedError(error) });}@@ -609,6 +644,14 @@ export class DaemonAgentConnection implements AgentConnection {return this.latestSnapshot;}}+ // A streamed snapshot for this session may still be in flight (for example+ // the replacement a warm switch just triggered). Fetching the transcript+ // now would race that stream and ship the full history again, so wait for+ // it bounded and prefer the freshly applied snapshot when it lands.+ await this.waitForPendingSessionSnapshot();+ if (this.latestSnapshotIsFresh && this.latestSnapshot) {+ return this.getInitialSnapshot(options);+ }// The session tree is intentionally not fetched here: it is large on long// sessions and only needed when the user opens the tree/branch selector.// getSessionTree() fetches it lazily via get_session_tree on first use.@@ -706,6 +749,7 @@ export class DaemonAgentConnection implements AgentConnection {.catch(async (error: unknown) => {if (this.disposed || this.terminalCloseEmitted) return;this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(new Error("Failed to recover deferred session events"));await this.emit({ type: "closed", error: `Failed to recover deferred session events: ${String(error)}` });}).finally(() => {@@ -1556,14 +1600,32 @@ export class DaemonAgentConnection implements AgentConnection {options?: AgentConnectionSwitchSessionOptions,): Promise<{ cancelled: boolean }> {const sourceActiveSessionId = this.activeSessionId;+ // The daemon streams the replacement snapshot (session_replaced plus+ // chunked frames) while this switch runs, so it can land before or after+ // the response. Expect it before sending the command so either order is+ // observed; awaiting it keeps the history crossing the wire once. The+ // expectation carries the requested session so a replacement for any other+ // session cannot settle this switch.+ const expectation = this.expectReplacementSnapshot(sessionPath);try {- return await this.requestData<{ cancelled: boolean }>({+ const result = await this.requestData<{ cancelled: boolean; sessionFile?: string }>({type: "switch_session",activeSessionId: sourceActiveSessionId,sessionPath,cwdOverride: options?.cwdOverride,});+ if (result.cancelled) {+ // No replacement happened; a later unrelated one must not settle a stale wait.+ this.failReplacementSnapshot(expectation, "Session switch was cancelled");+ return result;+ }+ await this.awaitReplacementSnapshot(expectation);+ // A superseded switch's request cannot verify the newer switch's+ // snapshot; the newer switch verifies the session it applied.+ if (!expectation.superseded) this.verifySwitchedSessionFile(sessionPath, result.sessionFile);+ return { cancelled: false };} catch (error) {+ this.failReplacementSnapshot(expectation, "Session switch failed");if (!(error instanceof SessionAlreadyActiveError) || !error.activeSessionId) {throw error;}@@ -1805,7 +1867,7 @@ export class DaemonAgentConnection implements AgentConnection {if (this.options.closeClientOnDispose) {this.client.close();}- this.rejectSnapshotAssemblies(new Error("Daemon connection disposed during snapshot transfer"));+ this.abortSnapshotTransfers(new Error("Daemon connection disposed during snapshot transfer"));}async promoteToResident(): Promise<void> {@@ -1926,6 +1988,7 @@ export class DaemonAgentConnection implements AgentConnection {) {this.sessionInputPauses.clear();this.sessionInputPauseGeneration++;+ this.abortSnapshotTransfers(new Error(`Daemon reconnection failed: ${lastError.message}`));this.client.close();await this.emit({ type: "closed", error: `Daemon reconnection failed: ${lastError.message}` });}@@ -2012,6 +2075,19 @@ export class DaemonAgentConnection implements AgentConnection {if (message.type === "session_snapshot_begin") {const assembly = this.getSnapshotAssembly(message.snapshotId);assembly.begin = message;+ // A warm switch waits on the replacement snapshot, or on the resync a+ // reconnect produces for the same session. The assembler applies either one+ // to the cache, which settles the wait; a failed stream settles it the other+ // way so the caller falls back to refetching the transcript promptly.+ const expectation = this.pendingReplacementSnapshot;+ if ((message.purpose === "replacement" || message.purpose === "resync") && expectation) {+ assembly.promise.catch((error: Error) => {+ // An abandoned stream is not a failed replacement: the re-attach resync+ // for the requested session settles the wait instead.+ if (error instanceof SnapshotTransferAbandonedError) return;+ this.failReplacementSnapshot(expectation, error.message);+ });+ }return;}if (message.type === "session_snapshot_chunk") {@@ -2122,6 +2198,8 @@ export class DaemonAgentConnection implements AgentConnection {this.latestSnapshot = latestSnapshot;this.childRosterSequence = undefined;this.latestSnapshotIsFresh = true;+ // The inline snapshot is fully applied: a waiting switch can proceed.+ this.settleReplacementSnapshot(undefined, message.state.sessionFile);await this.emit({ type: "session_replaced", state: message.state, messages: message.messages });return;}@@ -2154,7 +2232,7 @@ export class DaemonAgentConnection implements AgentConnection {}this.terminalCloseEmitted = true;const error = this.formatDaemonSessionClosedError(message.reason);- this.rejectSnapshotAssemblies(new Error(error));+ this.abortSnapshotTransfers(new Error(error));await this.emit({ type: "closed", error });}}@@ -2229,6 +2307,7 @@ export class DaemonAgentConnection implements AgentConnection {this.updateReconnectFailed = true;if (!this.disposed && !this.terminalCloseEmitted) {this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(new Error("Daemon update restoration failed"));await this.emit({type: "closed",error: this.formatUpdateReconnectError(error),@@ -2433,6 +2512,16 @@ export class DaemonAgentConnection implements AgentConnection {return assembly;}+ /**+ * A transfer that can no longer complete: reject every in-flight assembly and+ * release any switch waiting on the replacement stream they carried, so the+ * wait ends now instead of running out its timeout.+ */+ private abortSnapshotTransfers(error: Error): void {+ this.rejectSnapshotAssemblies(error);+ this.failReplacementSnapshot(undefined, error.message);+ }+private rejectSnapshotAssemblies(error: Error): void {for (const assembly of this.snapshotAssemblies.values()) {clearTimeout(assembly.timeout);@@ -2485,6 +2574,7 @@ export class DaemonAgentConnection implements AgentConnection {return;}this.terminalCloseEmitted = true;+ this.abortSnapshotTransfers(new Error(`Failed to recover from a ${purpose} snapshot transfer`));await this.emit({type: "closed",error: `Failed to recover from a ${purpose} snapshot transfer. Snapshot error: ${formatErrorSentence(snapshotError)} Recovery error: ${formatErrorSentence(recoveryError)} ${this.formatDaemonDiagnosticContext()}`,@@ -2508,6 +2598,132 @@ export class DaemonAgentConnection implements AgentConnection {}}+ /**+ * Register the streamed replacement snapshot a switchSession call waits+ * on. The outbound session_replaced can arrive before or after the switch+ * response, so the expectation is registered before sending the command.+ */+ private expectReplacementSnapshot(targetSessionFile?: string): ReplacementSnapshotExpectation {+ // Rapid consecutive switches: the latest expectation wins and a stale+ // one must never resolve the newer wait. Mark the stale wait superseded+ // so its late cleanup cannot mark the newer switch's snapshot stale.+ const stale = this.pendingReplacementSnapshot;+ if (stale) stale.superseded = true;+ this.failReplacementSnapshot(undefined, "Session switch superseded by a newer switch");+ let resolveExpectation!: () => void;+ let rejectExpectation!: (error: Error) => void;+ const promise = new Promise<void>((resolve, reject) => {+ resolveExpectation = resolve;+ rejectExpectation = reject;+ });+ // awaitReplacementSnapshot observes every settlement; rejections here+ // must not surface as unhandled.+ promise.catch(() => undefined);+ const expectation: ReplacementSnapshotExpectation = {+ promise,+ resolve: resolveExpectation,+ reject: rejectExpectation,+ targetSessionFile,+ };+ this.pendingReplacementSnapshot = expectation;+ return expectation;+ }++ /**+ * Settle the pending switch wait once a replacement snapshot was applied.+ * `appliedSessionFile` is the session that replacement carried: a replacement+ * for another session (an earlier switch's late stream, or the replacement+ * another client's switch on this daemon session broadcast to this one) must+ * not leave the wrong transcript marked fresh, so the cache is not served and+ * the caller reloads the session this connection is on.+ */+ private settleReplacementSnapshot(expectation?: ReplacementSnapshotExpectation, appliedSessionFile?: string): void {+ const pending = this.pendingReplacementSnapshot;+ if (!pending || (expectation && pending !== expectation)) return;+ this.pendingReplacementSnapshot = undefined;+ const target = pending.targetSessionFile;+ if (target && (!appliedSessionFile || !isSameSessionTarget(target, appliedSessionFile))) {+ this.latestSnapshotIsFresh = false;+ this.latestSnapshotStateIsFresh = false;+ }+ pending.resolve();+ }++ /** Reject the pending switch wait; a failed or ended switch must never be settled later. */+ private failReplacementSnapshot(expectation: ReplacementSnapshotExpectation | undefined, reason: string): void {+ const pending = this.pendingReplacementSnapshot;+ if (!pending || (expectation && pending !== expectation)) return;+ this.pendingReplacementSnapshot = undefined;+ pending.reject(new Error(reason));+ }++ /**+ * The switch response reports the session file the daemon resolved for the+ * request. A relative request cannot be compared against it before then, so a+ * snapshot of another session that a matching file name let through must not be+ * served as the switched transcript.+ */+ private verifySwitchedSessionFile(requestedSessionFile: string, resolvedSessionFile?: string): void {+ const applied = this.latestSnapshot?.state.sessionFile;+ if (!applied) return;+ // Without the daemon's resolution, only a request that names the file itself is+ // trustworthy: a relative one was matched on its file name alone.+ const unverified = resolvedSessionFile ? applied !== resolvedSessionFile : !isAbsolute(requestedSessionFile);+ if (!unverified) return;+ this.latestSnapshotIsFresh = false;+ this.latestSnapshotStateIsFresh = false;+ }++ /** Bounded wait for the switch's replacement snapshot; any failure falls back to refetching. */+ private async awaitReplacementSnapshot(expectation: ReplacementSnapshotExpectation): Promise<void> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ await Promise.race([+ expectation.promise,+ new Promise<never>((_, rejectTimeout) => {+ timer = setTimeout(+ () => rejectTimeout(new Error("Timed out waiting for the streamed session replacement snapshot")),+ this.options.snapshotTimeoutMs ?? DAEMON_SNAPSHOT_TIMEOUT_MS,+ );+ timer.unref();+ }),+ ]);+ } catch {+ // Timeout or a failed stream: no replacement was applied, so a cached+ // pre-switch snapshot must not stay fresh or the next+ // getInitialSnapshot would serve the previous session's transcript.+ // Invalidate it so the fetch fallback reloads the switched session.+ // A superseded wait is the exception: the newer switch's applied+ // snapshot owns the cache now, and this call must not mark it stale.+ if (!expectation.superseded) this.latestSnapshotIsFresh = false;+ } finally {+ clearTimeout(timer);+ this.failReplacementSnapshot(expectation, "Session switch wait ended");+ }+ }++ /** Bounded wait for an in-flight streamed snapshot so the fetch path does not race it. */+ private async waitForPendingSessionSnapshot(): Promise<void> {+ const pending = [...this.snapshotAssemblies.values()].filter(+ (assembly) =>+ (assembly.begin?.purpose === "replacement" || assembly.begin?.purpose === "resync") &&+ assembly.begin.activeSessionId === this.activeSessionId,+ );+ if (pending.length === 0) return;+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ await Promise.race([+ Promise.allSettled(pending.map((assembly) => assembly.promise)),+ new Promise<void>((resolveTimer) => {+ timer = setTimeout(resolveTimer, this.options.snapshotTimeoutMs ?? DAEMON_SNAPSHOT_TIMEOUT_MS);+ timer.unref();+ }),+ ]);+ } finally {+ clearTimeout(timer);+ }+ }+private applySessionSnapshot(snapshot: DaemonSessionSnapshot, replay?: DaemonReplayInfo): void {this.dropDeferredSessionEventsThrough(snapshot.lastEventSequence, snapshot.lastEventCursor);if (snapshot.lastEventCursor) {@@ -2522,6 +2738,10 @@ export class DaemonAgentConnection implements AgentConnection {this.childRosterSequence = Array.isArray(snapshot.children) ? snapshot.lastEventSequence : undefined;this.latestSnapshotIsFresh = true;this.latestSnapshotStateIsFresh = true;+ // The cache now holds this session, so a switch waiting on that session's+ // replacement - including the resync a re-attach synthesizes locally, which+ // never reaches the daemon-message path - can proceed.+ this.settleReplacementSnapshot(undefined, snapshot.state.sessionFile);}private async completeSnapshotAssembly(@@ -2777,8 +2997,38 @@ function getDaemonMessageCursor(message: DaemonOutbound): DaemonEventCursor | unreturn message.meta?.cursor;}+/**+ * Is the replacement or resync that just arrived the one this switch asked for?+ * Session files are compared as written, as resolved, and through symlinks, so a+ * differently spelled path to the same file is not mistaken for another session.+ * A relative request resolves against this process's cwd, which can differ from+ * the daemon's, so its file name is the only identity both sides share - two+ * sessions sharing a file name in different directories are then+ * indistinguishable, which uniquely named session files make rare; anything else+ * is another session's snapshot.+ */+function isSameSessionTarget(target: string, applied: string): boolean {+ if (target === applied || resolve(target) === resolve(applied)) return true;+ try {+ if (realpathSync(target) === realpathSync(applied)) return true;+ } catch {+ // A path missing on this host (a remote daemon) proves nothing either way.+ }+ return !isAbsolute(target) && sessionFileName(target) === sessionFileName(applied);+}++/** The final segment of a path in either separator spelling. */+function sessionFileName(file: string): string {+ return basename(file.replaceAll("\\", "/"));+}+function invalidatesCachedSnapshot(commandType: DaemonCommandBody["type"]): boolean {switch (commandType) {+ // switch_session's replacement arrives as the streamed replacement+ // snapshot (session_replaced plus chunked frames), which switchSession+ // awaits; a cancelled switch changes nothing, so the response itself+ // must not invalidate the cache and force a full-history refetch.+ case "switch_session":case "attach":case "reattach":case "detach":@@ -2796,6 +3046,7 @@ function invalidatesCachedSnapshot(commandType: DaemonCommandBody["type"]): boolcase "heartbeats_list":case "get_session_context":case "get_session_tree":+ case "get_context_tree":case "get_user_messages_for_forking":case "get_last_assistant_text":case "get_system_prompt":diff --git a/packages/coding-agent/src/modes/daemon/daemon-mode.ts b/packages/coding-agent/src/modes/daemon/daemon-mode.tsindex b5cf89c52b..b00c6e2090 100644--- a/packages/coding-agent/src/modes/daemon/daemon-mode.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-mode.ts@@ -5375,7 +5375,12 @@ export class AgentDaemon {cwdOverride: command.cwdOverride,});await this.rebindCronJobsToState(state);- return success(command.id, "switch_session", result);+ // A client cannot resolve a relative sessionPath the way this process+ // does, so the switch reports the file it resolved for the request.+ return success(command.id, "switch_session", {+ ...result,+ ...(result.cancelled ? {} : { sessionFile: state.runtime.session.sessionFile }),+ });}case "fork": {diff --git a/packages/coding-agent/test/agent-connection-daemon.test.ts b/packages/coding-agent/test/agent-connection-daemon.test.tsindex 0a876358ab..a39a4a0316 100644--- a/packages/coding-agent/test/agent-connection-daemon.test.ts+++ b/packages/coding-agent/test/agent-connection-daemon.test.ts@@ -248,6 +248,34 @@ class FakeDaemonClient {leafId: "user-1",},};+ case "get_context_tree":+ return {+ type: "response",+ command: command.type,+ success: true,+ data: {+ id: "root",+ label: "active-1 name",+ status: "active",+ ownUsage: {+ input: 0,+ output: 0,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 0,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },+ },+ totalUsage: {+ input: 0,+ output: 0,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 0,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },+ },+ children: [],+ },+ };case "get_tool_definition":return {type: "response",@@ -605,6 +633,78 @@ class FakeDaemonClient {}}+/**+ * Emit the daemon's streamed snapshot for one session: the chunked replacement or+ * resync frames a switch consumes, optionally preceded by the inline+ * session_replaced marker and optionally failing instead of completing.+ */+function emitChunkedSnapshot(+ fakeClient: FakeDaemonClient,+ options: {+ purpose: "replacement" | "resync";+ sessionId: string;+ messages?: AgentMessage[];+ sequence?: number;+ inline?: boolean;+ fail?: string;+ omitEnd?: boolean;+ sessionFile?: string;+ },+): void {+ const messages = options.messages ?? [];+ const sequence = options.sequence ?? 14;+ const baseState = createConnectionState("active-1", options.sessionId);+ const state = options.sessionFile === undefined ? baseState : { ...baseState, sessionFile: options.sessionFile };+ const snapshotId = `${options.purpose}-${options.sessionId}`;+ if (options.inline) {+ fakeClient.emitMessage({+ type: "session_replaced",+ activeSessionId: "active-1",+ state,+ messages: [],+ snapshotFollows: true,+ });+ }+ const { messages: _omitted, ...snapshot } = createAttachResult("active-1", "client-1", undefined, sequence, {+ state,+ messages,+ }).snapshot;+ fakeClient.emitMessage({+ type: "session_snapshot_begin",+ activeSessionId: "active-1",+ snapshotId,+ snapshot,+ messageCount: messages.length,+ targetChunkBytes: 512 * 1024,+ purpose: options.purpose,+ });+ if (options.fail) {+ fakeClient.emitMessage({+ type: "session_snapshot_failed",+ activeSessionId: "active-1",+ snapshotId,+ error: options.fail,+ });+ return;+ }+ fakeClient.emitMessage({+ type: "session_snapshot_chunk",+ activeSessionId: "active-1",+ snapshotId,+ index: 0,+ messages,+ });+ if (options.omitEnd) return;+ fakeClient.emitMessage({+ type: "session_snapshot_end",+ activeSessionId: "active-1",+ snapshotId,+ chunkCount: 1,+ lastEventSequence: sequence,+ lastEventCursor: { generation: "generation-active-1", sequence },+ });+}+function asDaemonClient(client: FakeDaemonClient): DaemonTransportClient {return client as unknown as DaemonTransportClient;}@@ -2103,46 +2203,16 @@ describe("DaemonAgentConnection", () => {events.push(event);});- const emitSnapshot = (- purpose: "replacement" | "resync",- snapshotId: string,- sequence: number,- sessionId: string,- ) => {- const messages: AgentMessage[] = [{ role: "user", content: purpose, timestamp: sequence }];- const full = createAttachResult("active-1", "client-1", undefined, sequence, {- state: createConnectionState("active-1", sessionId),- messages,- });- const { messages: _messages, ...snapshot } = full.snapshot;- fakeClient.emitMessage({- type: "session_snapshot_begin",- activeSessionId: "active-1",- snapshotId,- snapshot,- messageCount: messages.length,- targetChunkBytes: 512 * 1024,+ const emitSnapshot = (purpose: "replacement" | "resync", sequence: number, sessionId: string) =>+ emitChunkedSnapshot(fakeClient, {purpose,+ sessionId,+ messages: [{ role: "user", content: purpose, timestamp: sequence }],+ sequence,});- fakeClient.emitMessage({- type: "session_snapshot_chunk",- activeSessionId: "active-1",- snapshotId,- index: 0,- messages,- });- fakeClient.emitMessage({- type: "session_snapshot_end",- activeSessionId: "active-1",- snapshotId,- chunkCount: 1,- lastEventSequence: sequence,- lastEventCursor: { generation: "generation-active-1", sequence },- });- };- emitSnapshot("resync", "snapshot-resync", 13, "session-current");- emitSnapshot("replacement", "snapshot-replacement", 14, "session-next");+ emitSnapshot("resync", 13, "session-current");+ emitSnapshot("replacement", 14, "session-next");await vi.waitFor(() => expect(events).toHaveLength(2));expect(events).toEqual([@@ -2253,6 +2323,398 @@ describe("DaemonAgentConnection", () => {},);+ it("#2399: consumes the streamed replacement snapshot on a warm switch without refetching the transcript", async () => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const replaced = new Promise<AgentConnectionEvent>((resolve) => {+ connection.subscribe((event) => {+ if (event.type === "session_replaced") resolve(event);+ });+ });+ const switchedMessages: AgentMessage[] = [{ role: "user", content: "switched prompt", timestamp: 5 }];+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ // The daemon writes session_replaced (snapshotFollows) and streams the+ // chunked replacement snapshot before the switch responds.+ emitChunkedSnapshot(fakeClient, {+ purpose: "replacement",+ sessionId: "session-switched",+ messages: switchedMessages,+ inline: true,+ });+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ const switchedSessionFile = "/tmp/session-switched.jsonl";+ await expect(connection.switchSession(switchedSessionFile)).resolves.toEqual({ cancelled: false });+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["switch_session"]);+ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot).toMatchObject({ state: { sessionId: "session-switched" }, messages: switchedMessages });+ // The history crossed the wire once, as the chunked snapshot the warm switch+ // consumed: neither get_messages nor get_session_context refetched it.+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["switch_session"]);+ await expect(replaced).resolves.toMatchObject({ type: "session_replaced", messages: switchedMessages });+ await connection.dispose();+ });++ it("#2399: falls back to refetching the transcript when the replacement snapshot stream fails", async () => {+ const fakeClient = new FakeDaemonClient();+ fakeClient.connectionStateFactory = (activeSessionId) =>+ createConnectionState(activeSessionId, "session-switched");+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const replaced = new Promise<AgentConnectionEvent>((resolve) => {+ connection.subscribe((event) => {+ if (event.type === "session_replaced") resolve(event);+ });+ });+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ emitChunkedSnapshot(fakeClient, {+ purpose: "replacement",+ sessionId: "session-switched",+ inline: true,+ fail: "replacement snapshot failed",+ });+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ // The failed stream must not fail the switch: the caller proceeds and the+ // recovery refetches the transcript.+ await expect(connection.switchSession("/tmp/session-switched.jsonl")).resolves.toEqual({ cancelled: false });+ // The recovery emits the replacement event only after the refetch, so the+ // event is the completion signal for the whole fallback.+ await expect(replaced).resolves.toMatchObject({+ type: "session_replaced",+ state: { sessionId: "session-switched" },+ messages: [{ role: "user", content: "current prompt", timestamp: 4 }],+ });+ expect(fakeClient.requests.map((request) => request.type)).toEqual([+ "switch_session",+ "get_connection_state",+ "get_messages",+ "get_session_context",+ ]);+ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot).toMatchObject({+ state: { sessionId: "session-switched" },+ messages: [{ role: "user", content: "current prompt", timestamp: 4 }],+ });+ expect(fakeClient.requests.map((request) => request.type)).toEqual([+ "switch_session",+ "get_connection_state",+ "get_messages",+ "get_session_context",+ ]);+ await connection.dispose();+ });++ it("#2399: refetches the switched session when the replacement snapshot never arrives", async () => {+ const fakeClient = new FakeDaemonClient();+ fakeClient.connectionStateFactory = (activeSessionId) =>+ createConnectionState(activeSessionId, "session-switched");+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 10,+ });+ await connection.attach();+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ // The daemon accepts the switch but never streams a replacement snapshot.+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ // The bounded wait ends without a replacement, so the switch still returns.+ await expect(connection.switchSession("/tmp/session-switched.jsonl")).resolves.toEqual({ cancelled: false });+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["switch_session"]);+ // The stale pre-switch cache must never be served as the switched session.+ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot).toMatchObject({+ state: { sessionId: "session-switched" },+ messages: [{ role: "user", content: "current prompt", timestamp: 4 }],+ });+ expect(fakeClient.requests.map((request) => request.type)).toEqual([+ "switch_session",+ "get_connection_state",+ "get_messages",+ "get_session_context",+ ]);+ await connection.dispose();+ });++ it.each([+ {+ // Another client's switch on the same daemon session replaces the session+ // first, so this client receives a replacement snapshot it never asked for.+ requested: "/tmp/session-switched.jsonl",+ applied: "/tmp/session-elsewhere.jsonl",+ resolved: "/tmp/session-switched.jsonl",+ },+ {+ // The daemon resolves a relative request into another directory, and the+ // replacement this client receives only shares its file name.+ requested: "session-x.jsonl",+ applied: "/tmp/elsewhere/session-x.jsonl",+ resolved: "/tmp/target/session-x.jsonl",+ },+ {+ // Without the daemon's resolution, a relative request can only be matched by+ // file name, so it must not be trusted on its own.+ requested: "session-x.jsonl",+ applied: "/tmp/elsewhere/session-x.jsonl",+ },+ ])(+ "#2432: does not serve a replacement for another session as the switched transcript ($requested)",+ async (scenario) => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const foreignMessages: AgentMessage[] = [{ role: "user", content: "foreign prompt", timestamp: 6 }];+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ emitChunkedSnapshot(fakeClient, {+ purpose: "replacement",+ sessionId: "session-elsewhere",+ sessionFile: scenario.applied,+ messages: foreignMessages,+ inline: true,+ });+ return {+ type: "response",+ command: command.type,+ success: true,+ data: { cancelled: false, ...(scenario.resolved ? { sessionFile: scenario.resolved } : {}) },+ };+ });+ fakeClient.requests.length = 0;++ await expect(connection.switchSession(scenario.requested)).resolves.toEqual({ cancelled: false });+ // The replacement belongs to another session, so the switched transcript is+ // reloaded rather than served from that snapshot.+ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot.messages).toEqual([{ role: "user", content: "current prompt", timestamp: 4 }]);+ expect(fakeClient.requests.map((request) => request.type)).toEqual([+ "switch_session",+ "get_connection_state",+ "get_messages",+ "get_session_context",+ ]);+ await connection.dispose();+ },+ );++ it("#2432: keeps the newer switch's snapshot fresh when a superseded switch settles late", async () => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const newerMessages: AgentMessage[] = [{ role: "user", content: "newer prompt", timestamp: 9 }];+ let releaseOlder: (response: DaemonResponse) => void = () => {};+ const olderResponse = new Promise<DaemonResponse>((resolve) => {+ releaseOlder = resolve;+ });+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ if (command.sessionPath === "/tmp/session-older.jsonl") {+ // The older switch's response lands only after the newer switch finished.+ return olderResponse;+ }+ emitChunkedSnapshot(fakeClient, {+ purpose: "replacement",+ sessionId: "session-newer",+ sessionFile: "/tmp/session-newer.jsonl",+ messages: newerMessages,+ inline: true,+ });+ return {+ type: "response",+ command: command.type,+ success: true,+ data: { cancelled: false, sessionFile: "/tmp/session-newer.jsonl" },+ };+ });+ fakeClient.requests.length = 0;++ const switchedOlder = connection.switchSession("/tmp/session-older.jsonl");+ const switchedNewer = connection.switchSession("/tmp/session-newer.jsonl");+ await expect(switchedNewer).resolves.toEqual({ cancelled: false });+ releaseOlder({+ type: "response",+ command: "switch_session",+ success: true,+ data: { cancelled: false, sessionFile: "/tmp/session-older.jsonl" },+ });+ await expect(switchedOlder).resolves.toEqual({ cancelled: false });++ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot).toMatchObject({+ state: { sessionId: "session-newer" },+ messages: newerMessages,+ });+ // The newer switch's streamed snapshot served the history once: the+ // superseded switch's late cleanup must not mark it stale and force a+ // full transcript refetch after the rapid session change.+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["switch_session", "switch_session"]);+ await connection.dispose();+ });++ it("#2432: ends a switch wait when the reconnect fails instead of relaying the timeout", async () => {+ const fakeClient = new FakeDaemonClient();+ fakeClient.connectionStateFactory = (activeSessionId) =>+ createConnectionState(activeSessionId, "session-switched");+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ recoverDaemon: async () => undefined,+ reconnectTimeoutMs: 300,+ snapshotTimeoutMs: 5_000,+ });+ const closedEvents: AgentConnectionEvent[] = [];+ connection.subscribe((event) => {+ if (event.type === "closed") closedEvents.push(event);+ });+ await connection.attach();+ fakeClient.reconnectError = new Error("daemon unavailable");+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ // The transport closes before any replacement frame and never comes back.+ fakeClient.connected = false;+ fakeClient.emitClose(new Error("socket closed"));+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ const startedAt = Date.now();+ await expect(connection.switchSession("/tmp/session-switched.jsonl")).resolves.toEqual({ cancelled: false });+ // The reconnect gave up, so the wait ends with the connection error rather+ // than after snapshotTimeoutMs.+ expect(Date.now() - startedAt).toBeLessThan(1_000);+ expect(closedEvents[0]).toMatchObject({ type: "closed", error: expect.stringContaining("reconnection failed") });+ await connection.dispose();+ });++ it("#2432: keeps a switch wait alive when a recoverable close abandons the replacement stream", async () => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ recoverDaemon: async () => undefined,+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const syncedMessages: AgentMessage[] = [{ role: "user", content: "synced prompt", timestamp: 7 }];+ // The re-attach lands on the session the switch moved to, and streams it back.+ fakeClient.attachResultFactory = (command) =>+ createAttachResult(command.activeSessionId, command.clientId, command.capabilities, 14, {+ state: createConnectionState(command.activeSessionId, "session-switched"),+ messages: syncedMessages,+ });+ let releaseReattach: () => void = () => {};+ const reattachGate = new Promise<void>((resolve) => {+ releaseReattach = resolve;+ });+ let holdReattach = false;+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (holdReattach && command.type === "attach") await reattachGate;+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ // The replacement stream has begun, and the transport closes before its end+ // frame, so the assembly is abandoned mid-transfer.+ emitChunkedSnapshot(fakeClient, {+ purpose: "replacement",+ sessionId: "session-switched",+ messages: syncedMessages,+ omitEnd: true,+ });+ await nextMessageLoopTurn();+ holdReattach = true;+ fakeClient.connected = false;+ fakeClient.emitClose(new Error("socket closed"));+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ const switched = connection.switchSession("/tmp/session-switched.jsonl");+ let settled = false;+ void switched.then(() => {+ settled = true;+ });+ // The abandoned stream must not settle the switch; only the re-attach can.+ await nextMessageLoopTurn();+ await nextMessageLoopTurn();+ expect(settled).toBe(false);++ releaseReattach();+ await expect(switched).resolves.toEqual({ cancelled: false });+ const snapshot = await connection.getInitialSnapshot();+ expect(snapshot.messages).toEqual(syncedMessages);+ // The re-attached snapshot serves the transcript, so nothing is refetched.+ expect(fakeClient.requests.filter((request) => request.type.startsWith("get_"))).toEqual([]);+ await connection.dispose();+ });++ it("#2432: ends a switch wait when the transport closes before the replacement begins", async () => {+ const fakeClient = new FakeDaemonClient();+ fakeClient.connectionStateFactory = (activeSessionId) =>+ createConnectionState(activeSessionId, "session-switched");+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1", {+ snapshotTimeoutMs: 5_000,+ });+ await connection.attach();+ const request = fakeClient.request.bind(fakeClient);+ vi.spyOn(fakeClient, "request").mockImplementation(async (command, ...options) => {+ if (command.type !== "switch_session") return request(command, ...options);+ fakeClient.requests.push(command);+ // The daemon accepts the switch, then the connection dies before any+ // replacement frame arrives.+ fakeClient.connected = false;+ fakeClient.emitClose(new Error("socket closed"));+ return { type: "response", command: command.type, success: true, data: { cancelled: false } };+ });+ fakeClient.requests.length = 0;++ const startedAt = Date.now();+ await expect(connection.switchSession("/tmp/session-switched.jsonl")).resolves.toEqual({ cancelled: false });+ // Nothing can settle the wait once the transport is gone, so it must not+ // sit out snapshotTimeoutMs before the caller reloads the session.+ expect(Date.now() - startedAt).toBeLessThan(1_000);+ await connection.dispose();+ });++ it("#2399: keeps the cached snapshot fresh across get_context_tree reads", async () => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1");+ await connection.attach();+ const snapshot = await connection.getInitialSnapshot();+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["attach"]);+ fakeClient.requests.length = 0;++ // get_context_tree is a pure read: it must not invalidate the snapshot.+ await expect(connection.getContextTree()).resolves.toMatchObject({ id: "root" });+ await expect(connection.getInitialSnapshot()).resolves.toBe(snapshot);+ expect(fakeClient.requests.map((request) => request.type)).toEqual(["get_context_tree"]);+ await connection.dispose();+ });+it("keeps attach snapshots usable when the daemon omits duplicate session context", async () => {const fakeClient = new FakeDaemonClient();const messages: AgentMessage[] = [{ role: "user", content: "snapshot prompt", timestamp: 1 }];diff --git a/packages/coding-agent/test/daemon-supervisor-process.test.ts b/packages/coding-agent/test/daemon-supervisor-process.test.tsindex 038da8c422..914743f529 100644--- a/packages/coding-agent/test/daemon-supervisor-process.test.ts+++ b/packages/coding-agent/test/daemon-supervisor-process.test.ts@@ -1555,6 +1555,12 @@ describe("daemon supervisor resident workers", () => {expect(replacementMessageCounts).toContain(0);const switchedBack = await client.request({ type: "switch_session", activeSessionId, sessionPath: sessionFile });expect(switchedBack.success).toBe(true);+ // The switch reports the file it resolved, so a client that sent a relative+ // path can still correlate the replacement snapshot it waits on.+ expect(switchedBack.success ? switchedBack.data : undefined).toMatchObject({+ cancelled: false,+ sessionFile,+ });const restoredReplacementDeadline = Date.now() + 5000;while (replacementMessageCounts.at(-1) !== 2 && Date.now() < restoredReplacementDeadline) {await new Promise((resolveDelay) => setTimeout(resolveDelay, 10));diff --git a/scripts/bench-switch-doublefetch.ts b/scripts/bench-switch-doublefetch.tsnew file mode 100644index 0000000000..0b3fb95067--- /dev/null+++ b/scripts/bench-switch-doublefetch.ts@@ -0,0 +1,426 @@+/**+ * Local benchmark: full-history bytes on the wire + wall-clock latency for a+ * warm session switch into a large (48k-entry) transcript.+ *+ * Method: spawn the real daemon (built dist) over a local socket with an+ * isolated agent dir, attach a real DaemonAgentConnection (source via tsx,+ * direct worker link like the TUI), then measure the switch window:+ * 1. await connection.switchSession(largeSessionPath)+ * 2. await connection.getInitialSnapshot() (the TUI's post-switch render)+ * and account every daemon->client frame (JSON payload size) plus every+ * command response the client issued during the window, split by whether the+ * frame carries the full transcript (chunked snapshot, inline replacement,+ * get_messages response, get_session_context response).+ *+ * Usage:+ * npx tsx scripts/bench-switch-doublefetch.ts --trials 3+ */+import { spawn, type ChildProcess } from "node:child_process";+import { connect } from "node:net";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { performance } from "node:perf_hooks";+import { fileURLToPath } from "node:url";++import { DaemonClient } from "../packages/coding-agent/src/modes/daemon/daemon-client.js";+import { DaemonAgentConnection } from "../packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.js";+import { createDaemonSessionTransport } from "../packages/coding-agent/src/modes/daemon/daemon-routed-client.js";+import type { DaemonCommand, DaemonResponse } from "../packages/coding-agent/src/modes/daemon/daemon-protocol.js";+import {+ DAEMON_WORKER_ACTIVE_SESSION_ID_ENV,+ DAEMON_WORKER_INSTANCE_ID_ENV,+ DAEMON_WORKER_RECOVERY_JOURNAL_ENV,+ DAEMON_WORKER_ROLE_ENV,+ DAEMON_WORKER_STARTUP_GATE_FD_ENV,+ DAEMON_WORKER_SUPERVISOR_SOCKET_ENV,+ DAEMON_WORKER_TOKEN_ENV,+} from "../packages/coding-agent/src/modes/daemon/daemon-worker-protocol.js";++// Fixture shape ported from scripts/benchmarks/ui.py (_session_lines): a+// deterministic realistic mix of user / assistant(thinking+text+toolCall) /+// toolResult entries per step, so a 16000-step transcript matches the 18k-step+// 48k-entry session PR #2399 profiled (~35MB per full-history frame).+const BASE_TIMESTAMP_MS = 946684800000;+const TOOL_OUTPUT_LINES = 60;++function iso(ms: number): string {+ return new Date(ms).toISOString();+}++function sessionLines(name: string, identifier: string, count: number, cwd: string): string {+ let timestamp = BASE_TIMESTAMP_MS;+ let parent: string | null = null;+ const lines: string[] = [];+ const prefix = identifier.replaceAll("-", "").slice(0, 8);+ const link = (entryId: string) => {+ timestamp += 1000;+ const entry: Record<string, unknown> = {+ id: entryId,+ parentId: parent,+ timestamp: iso(timestamp),+ };+ parent = entryId;+ return entry;+ };+ lines.push(+ JSON.stringify({+ type: "session",+ version: 3,+ id: identifier,+ timestamp: iso(timestamp),+ cwd,+ rlmDepth: 0,+ }),+ );+ const info = link(`${prefix}n`);+ info.type = "session_info";+ info.name = name;+ lines.push(JSON.stringify(info));+ const model = link(`${prefix}m`);+ model.type = "model_change";+ model.provider = "prime-inference";+ model.modelId = "internal/glm-5.3-fast";+ lines.push(JSON.stringify(model));+ const usage = {+ input: 100,+ output: 50,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 150,+ cost: { input: 0.0001, output: 0.0001, cacheRead: 0, cacheWrite: 0, total: 0.0002 },+ };+ for (let index = 0; index < count; index++) {+ const final = index === count - 1;+ const user = link(`${prefix}u${index}`);+ user.type = "message";+ user.message = {+ role: "user",+ timestamp: 0,+ content: `Please continue task ${name} step ${index}: review the module, run the checks, and summarize findings for the migration notes.`,+ };+ lines.push(JSON.stringify(user));+ const text = `Working on ${name} step ${index}. The parser handles nested records correctly; the next edit keeps the schema stable while trimming the duplicated branch.`;+ const content: unknown[] = [+ {+ type: "thinking",+ thinking: `Step ${index} for ${name}: check the invariants, then update the affected call sites before running the full suite again to confirm no behavior changed.`,+ },+ { type: "text", text },+ ];+ if (!final) {+ content.push({ type: "toolCall", id: `call-${index}`, name: "ipython", arguments: { code: "pass" } });+ }+ const assistant = link(`${prefix}a${index}`);+ assistant.type = "message";+ assistant.message = {+ role: "assistant",+ timestamp: 0,+ content,+ api: "responses",+ provider: "prime-inference",+ model: "internal/glm-5.3-fast",+ usage,+ stopReason: final ? "stop" : "toolUse",+ };+ lines.push(JSON.stringify(assistant));+ if (!final) {+ const tool = link(`${prefix}t${index}`);+ tool.type = "message";+ tool.message = {+ role: "toolResult",+ toolCallId: `call-${index}`,+ toolName: "ipython",+ content: [{ type: "text", text: "all checks passed\n".repeat(TOOL_OUTPUT_LINES) }],+ isError: false,+ timestamp: 0,+ };+ lines.push(JSON.stringify(tool));+ }+ }+ return `${lines.join("\n")}\n`;+}++interface Counters {+ commandCounts: Map<string, number>;+ responseBytes: Map<string, number>;+ responseMs: Map<string, number>;+ outboundFrames: Map<string, number>;+ outboundBytes: Map<string, number>;+ /** Full transcript crossings in the window: streamed snapshots, inline replacements, and full-history refetches. */+ fullHistoryTransfers: number;+}++function newCounters(): Counters {+ return {+ commandCounts: new Map(),+ responseBytes: new Map(),+ responseMs: new Map(),+ outboundFrames: new Map(),+ outboundBytes: new Map(),+ fullHistoryTransfers: 0,+ };+}++function bump(map: Map<string, number>, key: string, delta: number): void {+ map.set(key, (map.get(key) ?? 0) + delta);+}++function messageKind(message: { type: string }): string {+ return message.type;+}++function fullHistoryBytes(counters: Counters): number {+ let total = 0;+ // Streamed replacement snapshot frames carry the transcript once.+ for (const kind of ["session_snapshot_begin", "session_snapshot_chunk", "session_snapshot_end"]) {+ total += counters.outboundBytes.get(kind) ?? 0;+ }+ // An inline session_replaced carries the full transcript in one frame.+ total += counters.outboundBytes.get("session_replaced") ?? 0;+ // Full-history refetch responses.+ total += counters.responseBytes.get("get_messages") ?? 0;+ total += counters.responseBytes.get("get_session_context") ?? 0;+ return total;+}++async function waitForSocket(socketPath: string, deadlineMs: number): Promise<void> {+ const start = Date.now();+ for (;;) {+ if (Date.now() - start > deadlineMs) throw new Error("daemon socket did not appear");+ const ok = await new Promise<boolean>((resolve) => {+ const socket = connect(socketPath);+ socket.once("connect", () => {+ socket.destroy();+ resolve(true);+ });+ socket.once("error", () => resolve(false));+ });+ if (ok) return;+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+}++function withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {+ return Promise.race([+ promise,+ new Promise<never>((_, reject) => {+ const timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);+ timer.unref?.();+ }),+ ]);+}++interface TrialResult {+ switchMs: number;+ snapshotMs: number;+ totalMs: number;+ commandCounts: Record<string, number>;+ fullHistoryBytes: number;+ fullHistoryTransfers: number;+ totalBytes: number;+}++const TRIAL_DEADLINE_MS = 120_000;++/**+ * The bench itself may run inside a daemon worker. The spawned daemon must not+ * inherit that role: a worker daemon speaks private-framed transport, while the+ * supervisor client used below reads JSONL.+ */+function supervisorEnvironment(agentDir: string): NodeJS.ProcessEnv {+ const environment: NodeJS.ProcessEnv = { ...process.env, PI_CODING_AGENT_DIR: agentDir, PI_OFFLINE: "1", PI_SKIP_VERSION_CHECK: "1" };+ for (const name of [+ DAEMON_WORKER_ROLE_ENV,+ DAEMON_WORKER_TOKEN_ENV,+ DAEMON_WORKER_INSTANCE_ID_ENV,+ DAEMON_WORKER_ACTIVE_SESSION_ID_ENV,+ DAEMON_WORKER_SUPERVISOR_SOCKET_ENV,+ DAEMON_WORKER_RECOVERY_JOURNAL_ENV,+ DAEMON_WORKER_STARTUP_GATE_FD_ENV,+ ]) {+ delete environment[name];+ }+ return environment;+}++async function runTrial(trial: number, agentDir: string, workspace: string): Promise<TrialResult> {+ const sessionsDir = join(agentDir, "sessions");+ mkdirSync(sessionsDir, { recursive: true });+ const smallId = `00000000-0000-4000-8000-0000000000a${trial}`;+ const largeId = `00000000-0000-4000-8000-0000000000b${trial}`;+ const smallPath = join(sessionsDir, `${smallId}.jsonl`);+ const largePath = join(sessionsDir, `${largeId}.jsonl`);+ writeFileSync(smallPath, sessionLines(`bench-small-${trial}`, smallId, 2000, workspace));+ writeFileSync(largePath, sessionLines(`bench-large-${trial}`, largeId, 16000, workspace));++ const socketPath = join(agentDir, `daemon-${trial}.sock`);+ const repoRoot = fileURLToPath(new URL("..", import.meta.url));+ const entrypoint = join(repoRoot, "packages", "coding-agent", "dist", "cli.js");+ const daemon: ChildProcess = spawn(process.execPath, [entrypoint, "--mode", "daemon", "--daemon-socket", socketPath], {+ stdio: "ignore",+ env: supervisorEnvironment(agentDir),+ // Own process group: the supervisor leaves its session worker running on+ // SIGTERM, so the trial stops the whole group when it tears down.+ detached: true,+ });+ const trialStart = performance.now();+ try {+ await waitForSocket(socketPath, 30_000);+ const supervisor = new DaemonClient(socketPath);+ await withTimeout(supervisor.connect(), 15_000, "daemon connect");+ await withTimeout(supervisor.waitForHello(20_000), 25_000, "daemon hello");++ const createResponse = await withTimeout(+ supervisor.request({ id: "c1", type: "create", sessionPath: smallPath } as unknown as DaemonCommand, 30_000),+ 35_000,+ "create session",+ );+ if (!createResponse.success) throw new Error(`create failed: ${createResponse.error}`);+ const activeSessionId = (createResponse.data as { activeSessionId: string }).activeSessionId;++ const transport = await createDaemonSessionTransport(supervisor, activeSessionId, false);+ const routed = (transport as { constructor?: { name?: string } }).constructor?.name === "DaemonRoutedClient";+ console.log(`trial ${trial}: transport ${routed ? "direct worker link (private-framed)" : "supervisor (jsonl)"}`);+ const counters = newCounters();+ transport.onMessage((message) => {+ const kind = messageKind(message);+ bump(counters.outboundFrames, kind, 1);+ bump(counters.outboundBytes, kind, JSON.stringify(message).length + 1);+ if (+ kind === "session_snapshot_end" ||+ (kind === "session_replaced" && Array.isArray((message as { messages?: unknown[] }).messages) && ((message as { messages: unknown[] }).messages.length > 0))+ ) {+ counters.fullHistoryTransfers++;+ }+ });+ const originalRequest = transport.request.bind(transport);+ transport.request = (async (command: DaemonCommand, timeoutMs?: number, options?: unknown) => {+ const started = performance.now();+ const response: DaemonResponse = await originalRequest(+ command,+ timeoutMs,+ options as never,+ );+ const kind = command.type;+ bump(counters.commandCounts, kind, 1);+ bump(counters.responseBytes, kind, JSON.stringify(response).length + 1);+ bump(counters.responseMs, kind, performance.now() - started);+ if (kind === "get_messages" || kind === "get_session_context") {+ counters.fullHistoryTransfers++;+ }+ return response;+ }) as typeof transport.request;++ const connection = new DaemonAgentConnection(transport, activeSessionId, {});+ await withTimeout(connection.attach(), 30_000, "attach");+ await withTimeout(connection.getInitialSnapshot(), 30_000, "initial snapshot");++ // Switch window: reset counters around the warm switch.+ for (const key of [...counters.commandCounts.keys()]) counters.commandCounts.delete(key);+ for (const key of [...counters.responseBytes.keys()]) counters.responseBytes.delete(key);+ for (const key of [...counters.responseMs.keys()]) counters.responseMs.delete(key);+ for (const key of [...counters.outboundFrames.keys()]) counters.outboundFrames.delete(key);+ for (const key of [...counters.outboundBytes.keys()]) counters.outboundBytes.delete(key);+ counters.fullHistoryTransfers = 0;++ const t0 = performance.now();+ const switched = await withTimeout(connection.switchSession(largePath), 60_000, "switchSession");+ const tSwitch = performance.now();+ if (switched.cancelled) throw new Error("switch was cancelled");+ const snapshot = await withTimeout(connection.getInitialSnapshot(), 60_000, "getInitialSnapshot");+ const tSnapshot = performance.now();+ if (snapshot.messages.length === 0) throw new Error("switch produced an empty transcript");++ const result: TrialResult = {+ switchMs: tSwitch - t0,+ snapshotMs: tSnapshot - tSwitch,+ totalMs: tSnapshot - t0,+ commandCounts: Object.fromEntries(counters.commandCounts),+ fullHistoryBytes: fullHistoryBytes(counters),+ fullHistoryTransfers: counters.fullHistoryTransfers,+ totalBytes: [...counters.outboundBytes.values()].reduce((a, b) => a + b, 0) ++ [...counters.responseBytes.values()].reduce((a, b) => a + b, 0),+ };+ await withTimeout(connection.dispose(), 15_000, "dispose");+ // The supervisor's SIGTERM path closes its socket without stopping the detached+ // session worker, so the teardown asks for the worker-stopping shutdown while+ // this socket is still open.+ await withTimeout(+ supervisor.request({ type: "shutdown", force: true } as unknown as DaemonCommand, 10_000),+ 15_000,+ "shutdown",+ ).catch(() => undefined);+ supervisor.close();+ const elapsed = performance.now() - trialStart;+ if (elapsed > TRIAL_DEADLINE_MS) throw new Error(`trial exceeded ${TRIAL_DEADLINE_MS}ms budget`);+ return result;+ } finally {+ await stopDaemon(daemon);+ }+}++/**+ * Wait for the supervisor to finish the shutdown the trial requested, and fall back+ * to signals so a wedged daemon cannot leave the benchmark hanging. A supervisor+ * killed with SIGTERM alone would leave its detached session worker (and that+ * worker's session state) running after the trial.+ */+async function stopDaemon(daemon: ChildProcess): Promise<void> {+ for (const signal of [undefined, "SIGTERM", "SIGKILL"] as const) {+ if (await waitForDaemonExit(daemon, signal === undefined ? 10_000 : 5_000)) return;+ daemon.kill(signal ?? "SIGTERM");+ }+}++/** Resolve true when the daemon has exited within the budget. */+async function waitForDaemonExit(daemon: ChildProcess, timeoutMs: number): Promise<boolean> {+ if (daemon.exitCode !== null || daemon.signalCode !== null) return true;+ return await new Promise<boolean>((resolve) => {+ const timer = setTimeout(() => resolve(false), timeoutMs);+ daemon.once("exit", () => {+ clearTimeout(timer);+ resolve(true);+ });+ });+}++const args = process.argv.slice(2);+const trialsIndex = args.indexOf("--trials");+const trials = trialsIndex === -1 ? 3 : Number(args[trialsIndex + 1] ?? 3);++const median = (values: number[]): number => {+ const sorted = [...values].sort((a, b) => a - b);+ return sorted[Math.floor(sorted.length / 2)];+};++const results: TrialResult[] = [];+for (let trial = 1; trial <= trials; trial++) {+ const agentDir = mkdtempSync(join(tmpdir(), `pi-switch-bench-${trial}-`));+ const workspace = join(agentDir, "workspace");+ mkdirSync(workspace, { recursive: true });+ try {+ const result = await runTrial(trial, agentDir, workspace);+ results.push(result);+ console.log(+ `trial ${trial}: switch ${result.switchMs.toFixed(0)}ms + snapshot ${result.snapshotMs.toFixed(0)}ms = ${result.totalMs.toFixed(0)}ms; full-history transfers ${result.fullHistoryTransfers}; full-history bytes ${(result.fullHistoryBytes / 1024 / 1024).toFixed(1)}MB; total wire bytes ${(result.totalBytes / 1024 / 1024).toFixed(1)}MB; commands ${JSON.stringify(result.commandCounts)}`,+ );+ } finally {+ rmSync(agentDir, { recursive: true, force: true });+ }+}++if (results.length > 0) {+ console.log("");+ console.log(`median switchSession: ${median(results.map((r) => r.switchMs)).toFixed(0)}ms`);+ console.log(`median getInitialSnapshot: ${median(results.map((r) => r.snapshotMs)).toFixed(0)}ms`);+ console.log(`median total switch window: ${median(results.map((r) => r.totalMs)).toFixed(0)}ms`);+ console.log(`median full-history transfers: ${median(results.map((r) => r.fullHistoryTransfers))}`);+ console.log(`median full-history bytes: ${(median(results.map((r) => r.fullHistoryBytes)) / 1024 / 1024).toFixed(1)}MB`);+ console.log(`median total wire bytes: ${(median(results.map((r) => r.totalBytes)) / 1024 / 1024).toFixed(1)}MB`);+}++// Flush stdout before exiting so piped output is not truncated, and leave through+// process.exit: a stray daemon handle must not keep the benchmark alive.+await new Promise<void>((resolve) => process.stdout.write("", () => resolve()));+process.exit(0);diff --git a/scripts/benchmarks/README.md b/scripts/benchmarks/README.mdindex 21871dffd8..14dcc4d0e8 100644--- a/scripts/benchmarks/README.md+++ b/scripts/benchmarks/README.md@@ -60,6 +60,7 @@ and other missing tools. Their setup time and disk usage are outside the install- **Installed footprint:** apparent bytes added after first use in the first fresh home, including stock Python,runtime, and tool assets; excluding download caches, session history, and logs. Shared systemdependencies supplied by the base image and the fixture repository are excluded.+- **Session-switch transcript fetch:** one warm switch into a 48k-entry session through a real daemon over a direct worker link; the value counts how many times the full transcript crosses the wire in the switch window (streamed replacement snapshots, inline replacements, and full-history refetch responses).- **Private frame decode:** one 32 MB frame with a snapshot-chunk routing header pushed through `PrivateFrameDecoder` in 8 KiB socket chunks, seconds per complete decode. This is the wire shape of the multi-MB snapshot and response frames on the daemon-worker and peer transport channels.- **Idle memory:** summed RSS across the benchmark user's entire process tree after input readinessand a one-second settle. Raw results include each process and PSS when Linux permits reading it.diff --git a/scripts/benchmarks/controller.py b/scripts/benchmarks/controller.pyindex 931a2f8749..c0555714fe 100644--- a/scripts/benchmarks/controller.py+++ b/scripts/benchmarks/controller.py@@ -32,6 +32,7 @@ FILES = ("kernel.py","worker.py","ui.py",+ "switch-fetch-bench.mjs","frame-decode-bench.mjs","pyproject.toml","uv.lock",diff --git a/scripts/benchmarks/report.py b/scripts/benchmarks/report.pyindex 3eb9ce0ad2..29857f5d14 100644--- a/scripts/benchmarks/report.py+++ b/scripts/benchmarks/report.py@@ -108,6 +108,14 @@ RUNTIME_METRICS = (),)TRANSPORT_METRICS = (+ Definition(+ "switch_fetch",+ "Full-history transfers per warm session switch",+ 1,+ "transfers",+ 0.5,+ 0,+ ),Definition("frame_decode","Private frame decode, 32 MiB in 8 KiB chunks",@@ -414,6 +422,9 @@ def render(report: Report) -> str:"Restore runs in a fresh kernel, including pandas imports; kernel startup is excluded.","Kernel RSS covers the isolated Python process; loaded RSS follows the pandas workload.","Transport benches run node against the prepared source build, outside the installed home.",+ "The switch benchmark drives one warm switch into a 48k-entry session through a real",+ "daemon and counts full-history crossings: streamed replacement snapshots, inline",+ "replacements, and full-history refetch responses.","Frame decode times one 32 MiB private frame, snapshot-chunk header, pushed in","8 KiB chunks; the wire shape of multi-MB frames on the daemon-worker channels.","UI trials use a fresh fixture set: 194 top-level sessions including one ~40 MB transcript,",diff --git a/scripts/benchmarks/schema.py b/scripts/benchmarks/schema.pyindex 2cd3144e36..623be1227e 100644--- a/scripts/benchmarks/schema.py+++ b/scripts/benchmarks/schema.py@@ -27,6 +27,7 @@ Metric = Literal["restore","kernel_rss","loaded_rss",+ "switch_fetch","frame_decode","resume_large","resume_large_cpu",@@ -236,7 +237,7 @@ PHASE_METRICS: dict[str, tuple[Metric, ...]] = {"kernel_rss","loaded_rss",),- "transport": ("frame_decode",),+ "transport": ("switch_fetch", "frame_decode"),"ui": ("resume_large","resume_large_cpu",diff --git a/scripts/benchmarks/switch-fetch-bench.mjs b/scripts/benchmarks/switch-fetch-bench.mjsnew file mode 100644index 0000000000..94b2da5ae4--- /dev/null+++ b/scripts/benchmarks/switch-fetch-bench.mjs@@ -0,0 +1,348 @@+/**+ * Session-switch transcript-fetch benchmark: one warm switch into a large+ * (16k-step) session through a real daemon and a direct worker link, counting+ * how many times the full transcript crosses the wire in the switch window+ * (streamed replacement snapshots, inline replacements, and full-history+ * refetch responses). Ports the measurement of scripts/bench-switch-doublefetch.ts+ * to the CI harness so benchmark runs compare main and the PR head directly.+ *+ * Runs against the prepared source build and prints one RESULT line with the+ * transfer count; the benchmark worker records it as the switch_fetch metric.+ *+ * Usage: node switch-fetch-bench.mjs --dist <packages/coding-agent dist>+ */+import { spawn } from "node:child_process";+import { connect } from "node:net";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join, resolve } from "node:path";+import { performance } from "node:perf_hooks";+import { pathToFileURL } from "node:url";++const SMALL_STEPS = 2000;+const LARGE_STEPS = 16000;+const TOOL_OUTPUT_LINES = 60;+const BASE_TIMESTAMP_MS = 946684800000;+const TRIAL_DEADLINE_MS = 120_000;++function parseArgs(argv) {+ const args = { dist: "" };+ for (let index = 2; index < argv.length; index++) {+ if (argv[index] === "--dist" && argv[index + 1]) {+ args.dist = argv[index + 1];+ index++;+ }+ }+ if (!args.dist) {+ throw new Error("usage: node switch-fetch-bench.mjs --dist <coding-agent dist>");+ }+ return args;+}++function iso(ms) {+ return new Date(ms).toISOString();+}++/** Fixture shape ported from scripts/benchmarks/ui.py and bench-switch-doublefetch.ts. */+function sessionLines(name, identifier, count, cwd) {+ let timestamp = BASE_TIMESTAMP_MS;+ let parent = null;+ const lines = [];+ const prefix = identifier.replaceAll("-", "").slice(0, 8);+ const link = (entryId) => {+ timestamp += 1000;+ const entry = { id: entryId, parentId: parent, timestamp: iso(timestamp) };+ parent = entryId;+ return entry;+ };+ lines.push(JSON.stringify({ type: "session", version: 3, id: identifier, timestamp: iso(timestamp), cwd, rlmDepth: 0 }));+ const info = link(`${prefix}n`);+ info.type = "session_info";+ info.name = name;+ lines.push(JSON.stringify(info));+ const model = link(`${prefix}m`);+ model.type = "model_change";+ model.provider = "prime-inference";+ model.modelId = "internal/glm-5.3-fast";+ lines.push(JSON.stringify(model));+ const usage = {+ input: 100,+ output: 50,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 150,+ cost: { input: 0.0001, output: 0.0001, cacheRead: 0, cacheWrite: 0, total: 0.0002 },+ };+ for (let index = 0; index < count; index++) {+ const final = index === count - 1;+ const user = link(`${prefix}u${index}`);+ user.type = "message";+ user.message = {+ role: "user",+ timestamp: 0,+ content: `Please continue task ${name} step ${index}: review the module, run the checks, and summarize findings for the migration notes.`,+ };+ lines.push(JSON.stringify(user));+ const content = [+ {+ type: "thinking",+ thinking: `Step ${index} for ${name}: check the invariants, then update the affected call sites before running the full suite again to confirm no behavior changed.`,+ },+ {+ type: "text",+ text: `Working on ${name} step ${index}. The parser handles nested records correctly; the next edit keeps the schema stable while trimming the duplicated branch.`,+ },+ ];+ if (!final) {+ content.push({ type: "toolCall", id: `call-${index}`, name: "ipython", arguments: { code: "pass" } });+ }+ const assistant = link(`${prefix}a${index}`);+ assistant.type = "message";+ assistant.message = {+ role: "assistant",+ timestamp: 0,+ content,+ api: "responses",+ provider: "prime-inference",+ modelId: "internal/glm-5.3-fast",+ usage,+ stopReason: final ? "stop" : "toolUse",+ };+ lines.push(JSON.stringify(assistant));+ if (!final) {+ const tool = link(`${prefix}t${index}`);+ tool.type = "message";+ tool.message = {+ role: "toolResult",+ toolCallId: `call-${index}`,+ toolName: "ipython",+ content: [{ type: "text", text: "all checks passed\n".repeat(TOOL_OUTPUT_LINES) }],+ isError: false,+ timestamp: 0,+ };+ lines.push(JSON.stringify(tool));+ }+ }+ return `${lines.join("\n")}\n`;+}++function bump(map, key, delta) {+ map.set(key, (map.get(key) ?? 0) + delta);+}++async function waitForSocket(socketPath, deadlineMs) {+ const started = Date.now();+ for (;;) {+ if (Date.now() - started > deadlineMs) throw new Error("daemon socket did not appear");+ const connected = await new Promise((resolve) => {+ const probe = connect(socketPath);+ probe.once("connect", () => {+ probe.destroy();+ resolve(true);+ });+ probe.once("error", () => resolve(false));+ });+ if (connected) return;+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+}++function withTimeout(promise, ms, label) {+ return Promise.race([+ promise,+ new Promise((_, reject) => {+ const timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);+ timer.unref?.();+ }),+ ]);+}++async function waitForDaemonExit(daemon, timeoutMs) {+ if (daemon.exitCode !== null || daemon.signalCode !== null) return true;+ return await new Promise((resolve) => {+ const timer = setTimeout(() => resolve(false), timeoutMs);+ daemon.once("exit", () => {+ clearTimeout(timer);+ resolve(true);+ });+ });+}++async function stopDaemon(daemon, { swift = false } = {}) {+ for (const signal of swift ? ["SIGTERM", "SIGKILL"] : [undefined, "SIGTERM", "SIGKILL"]) {+ if (await waitForDaemonExit(daemon, signal === undefined ? 10_000 : 5_000)) return;+ try {+ // The daemon leads its own detached process group, so signalling the group+ // also reaches a session worker a wedged supervisor never stopped.+ process.kill(-daemon.pid, signal ?? "SIGTERM");+ } catch {+ // The group is already gone; wait again before the stronger signal.+ }+ }+}++async function main() {+ // Resolve before spawning the daemon with a different cwd.+ const dist = resolve(parseArgs(process.argv).dist);+ const module = (path) => import(pathToFileURL(join(dist, path)).href);+ const { DaemonClient } = await module("modes/daemon/daemon-client.js");+ const { DaemonAgentConnection } = await module("modes/agent-connection/daemon-agent-connection.js");+ const { createDaemonSessionTransport } = await module("modes/daemon/daemon-routed-client.js");+ const workerProtocol = await module("modes/daemon/daemon-worker-protocol.js");++ const supervisorEnvironment = (agentDir) => {+ const environment = { ...process.env, PI_CODING_AGENT_DIR: agentDir, PI_OFFLINE: "1", PI_SKIP_VERSION_CHECK: "1" };+ for (const name of [+ workerProtocol.DAEMON_WORKER_ROLE_ENV,+ workerProtocol.DAEMON_WORKER_TOKEN_ENV,+ workerProtocol.DAEMON_WORKER_INSTANCE_ID_ENV,+ workerProtocol.DAEMON_WORKER_ACTIVE_SESSION_ID_ENV,+ workerProtocol.DAEMON_WORKER_SUPERVISOR_SOCKET_ENV,+ workerProtocol.DAEMON_WORKER_RECOVERY_JOURNAL_ENV,+ workerProtocol.DAEMON_WORKER_STARTUP_GATE_FD_ENV,+ ]) {+ delete environment[name];+ }+ return environment;+ };++ const agentDir = mkdtempSync(join(tmpdir(), "pi-switch-fetch-bench-"));+ const workspace = join(agentDir, "workspace");+ mkdirSync(workspace, { recursive: true });+ const trialStart = performance.now();+ let daemon;+ const runTrial = async () => {+ const sessionsDir = join(agentDir, "sessions");+ mkdirSync(sessionsDir, { recursive: true });+ const smallId = "00000000-0000-4000-8000-0000000000aa";+ const largeId = "00000000-0000-4000-8000-0000000000bb";+ const smallPath = join(sessionsDir, `${smallId}.jsonl`);+ const largePath = join(sessionsDir, `${largeId}.jsonl`);+ writeFileSync(smallPath, sessionLines("bench-switch-fetch-small", smallId, SMALL_STEPS, workspace));+ writeFileSync(largePath, sessionLines("bench-switch-fetch-large", largeId, LARGE_STEPS, workspace));+ const socketPath = join(agentDir, "daemon.sock");+ daemon = spawn(+ process.execPath,+ [join(dist, "cli.js"), "--mode", "daemon", "--daemon-socket", socketPath],+ { cwd: workspace, env: supervisorEnvironment(agentDir), stdio: "ignore", detached: true },+ );+ await waitForSocket(socketPath, 30_000);+ const supervisor = new DaemonClient(socketPath);+ await withTimeout(supervisor.connect(), 15_000, "daemon connect");+ await withTimeout(supervisor.waitForHello(20_000), 25_000, "daemon hello");+ const createResponse = await withTimeout(+ supervisor.request({ id: "create", type: "create", sessionPath: smallPath }, 30_000),+ 35_000,+ "create session",+ );+ if (!createResponse.success) throw new Error(`create failed: ${createResponse.error}`);+ const activeSessionId = createResponse.data.activeSessionId;++ const transport = await createDaemonSessionTransport(supervisor, activeSessionId, false);+ const commandCounts = new Map();+ const responseBytes = new Map();+ const outboundBytes = new Map();+ let fullHistoryTransfers = 0;+ transport.onMessage((message) => {+ const kind = message.type;+ bump(outboundBytes, kind, JSON.stringify(message).length + 1);+ if (kind === "session_snapshot_end") {+ fullHistoryTransfers++;+ }+ if (kind === "session_replaced" && Array.isArray(message.messages) && message.messages.length > 0) {+ fullHistoryTransfers++;+ }+ });+ const originalRequest = transport.request.bind(transport);+ transport.request = async (command, timeoutMs, options) => {+ const response = await originalRequest(command, timeoutMs, options);+ const kind = command.type;+ bump(commandCounts, kind, 1);+ bump(responseBytes, kind, JSON.stringify(response).length + 1);+ if (kind === "get_messages" || kind === "get_session_context") {+ fullHistoryTransfers++;+ }+ return response;+ };++ const connection = new DaemonAgentConnection(transport, activeSessionId, {});+ await withTimeout(connection.attach(), 30_000, "attach");+ await withTimeout(connection.getInitialSnapshot(), 30_000, "initial snapshot");++ // Switch window: count every full-history crossing while the switch happens.+ commandCounts.clear();+ responseBytes.clear();+ outboundBytes.clear();+ fullHistoryTransfers = 0;++ const started = performance.now();+ const switched = await withTimeout(connection.switchSession(largePath), 60_000, "switchSession");+ if (switched.cancelled) throw new Error("switch was cancelled");+ const snapshot = await withTimeout(connection.getInitialSnapshot(), 60_000, "getInitialSnapshot");+ const switchWindowMs = performance.now() - started;+ if (snapshot.messages.length === 0) throw new Error("switch produced an empty transcript");+ if (fullHistoryTransfers < 1) throw new Error("no full-history transfer was observed during the switch");+ let fullHistoryBytes = 0;+ for (const kind of ["session_snapshot_begin", "session_snapshot_chunk", "session_snapshot_end"]) {+ fullHistoryBytes += outboundBytes.get(kind) ?? 0;+ }+ fullHistoryBytes += outboundBytes.get("session_replaced") ?? 0;+ fullHistoryBytes += responseBytes.get("get_messages") ?? 0;+ fullHistoryBytes += responseBytes.get("get_session_context") ?? 0;+ const commands = Object.fromEntries(commandCounts);+ await withTimeout(connection.dispose(), 15_000, "dispose");+ await withTimeout(+ supervisor.request({ type: "shutdown", force: true }, 10_000),+ 15_000,+ "shutdown",+ ).catch(() => undefined);+ supervisor.close();+ // Await the pipe flush so the recorded value cannot be truncated.+ await new Promise((resolve) => {+ process.stdout.write(+ `RESULT ${JSON.stringify({+ value: fullHistoryTransfers,+ switch_window_ms: Math.round(switchWindowMs),+ full_history_bytes: fullHistoryBytes,+ commands: commands,+ })}\n`,+ resolve,+ );+ });+ };+ // One race bounds the whole trial, not a check at its end: every step keeps+ // its own smaller timeout, and the losing trial's late rejection is expected+ // once the deadline kills the daemon. Teardown still fits the worker timeout.+ const trial = runTrial();+ trial.catch(() => undefined);+ let trialTimer;+ let completed = false;+ try {+ await Promise.race([+ trial,+ new Promise((_, reject) => {+ trialTimer = setTimeout(+ () => reject(new Error(`trial exceeded ${TRIAL_DEADLINE_MS}ms budget`)),+ TRIAL_DEADLINE_MS,+ );+ trialTimer.unref?.();+ }),+ ]);+ completed = true;+ } finally {+ clearTimeout(trialTimer);+ // A lost race kills the daemon immediately; the happy path lets it finish.+ if (daemon) await stopDaemon(daemon, { swift: !completed });+ rmSync(agentDir, { recursive: true, force: true });+ if (completed) {+ // A completed measurement must not become a failure or a hang because a+ // stray daemon handle keeps the event loop alive after teardown.+ process.exit(0);+ }+ }+}++main().catch((error) => {+ process.stderr.write(`switch-fetch-bench failed: ${error?.stack ?? error}\n`);+ process.exit(1);+});diff --git a/scripts/benchmarks/tests/test_benchmarks.py b/scripts/benchmarks/tests/test_benchmarks.pyindex 23a2e7b8f5..31e5382b77 100644--- a/scripts/benchmarks/tests/test_benchmarks.py+++ b/scripts/benchmarks/tests/test_benchmarks.py@@ -18,7 +18,15 @@ from pydantic import ValidationErrorfrom cli import completed_report, main, validate_completion, workflow_sourcefrom controller import Canceled, Controller, cleanup, labelsfrom github import TITLE, GitHub-from report import MARKER, METRICS, RUNTIME_METRICS, TRANSPORT_METRICS, UI_METRICS, comparison, render+from report import (+ MARKER,+ METRICS,+ RUNTIME_METRICS,+ TRANSPORT_METRICS,+ UI_METRICS,+ comparison,+ render,+)from schema import (UI_METRIC_KEYS,Config,@@ -186,8 +194,10 @@ class ReportTests(unittest.TestCase):report.pr_head.metrics["install"] = observations(2.0, 2.0)report.pr_head.metrics["bundle"] = []text = render(report)+ unchanged = len(METRICS) + len(RUNTIME_METRICS) + len(TRANSPORT_METRICS) - 4self.assertIn(- "**Overall: 1 regressed · 1 improved · 14 no clear change · 1 incomplete · "+ "**Overall: 1 regressed · 1 improved · "+ f"{unchanged} no clear change · 1 incomplete · "f"{len(UI_METRICS) + 1} unavailable.**",text,)diff --git a/scripts/benchmarks/tests/test_transport.py b/scripts/benchmarks/tests/test_transport.pyindex 8b0a9eca28..190f825a0d 100644--- a/scripts/benchmarks/tests/test_transport.py+++ b/scripts/benchmarks/tests/test_transport.py@@ -15,11 +15,12 @@ from worker import TRANSPORT_BENCHES, stop_transport_processes, transport, transclass TransportTests(unittest.TestCase):def test_records_each_bench_result_and_failure_with_explicit_timeouts(self):side = Side(sha="a" * 40)- script, metric, timeout = TRANSPORT_BENCHES[0]- failure = subprocess.CalledProcessError(1, "node", output="frame-decode-bench failed: exploded\n")- run_as = Mock(- side_effect=['RESULT {"value": 1.5}\n', failure, subprocess.TimeoutExpired("node", timeout)]- )+ failure = subprocess.CalledProcessError(1, "node", output="bench failed: exploded\n")+ # Every trial drives the full two-bench transport phase: both benches+ # succeed, then both fail, then both time out.+ result = 'RESULT {"value": 1.5}\n'+ timeouts = [subprocess.TimeoutExpired("node", timeout) for _, _, timeout in TRANSPORT_BENCHES]+ run_as = Mock(side_effect=[result, result, failure, failure, *timeouts])with tempfile.TemporaryDirectory() as directory:with (patch("worker.run_as", run_as),@@ -29,8 +30,7 @@ class TransportTests(unittest.TestCase):for trial in range(3):transport(side, trial)saved = sorted(path.name for path in Path(directory).glob("*.output"))- self.assertEqual(- run_as.call_args,+ expected_calls = [call("builder",[@@ -42,12 +42,18 @@ class TransportTests(unittest.TestCase):Path("/home/builder/source"),timeout=timeout,merge_output=True,- ),+ )+ for script, _metric, timeout in TRANSPORT_BENCHES+ ]+ self.assertEqual(run_as.call_args_list, expected_calls * 3)+ for _script, metric, _timeout in TRANSPORT_BENCHES:+ self.assertEqual(side.metrics[metric][0], Observation(trial=0, value=1.5))+ self.assertIn("exploded", side.metrics[metric][1].error)+ self.assertIn("timed out", side.metrics[metric][2].error)+ self.assertEqual(+ saved,+ sorted(f"{metric}-{trial}.output" for trial in (0, 1) for _s, metric, _t in TRANSPORT_BENCHES),)- self.assertEqual(side.metrics[metric][0], Observation(trial=0, value=1.5))- self.assertIn("exploded", side.metrics[metric][1].error)- self.assertIn("timed out", side.metrics[metric][2].error)- self.assertEqual(saved, [f"{metric}-0.output", f"{metric}-1.output"])self.assertEqual({name for _, name, _ in TRANSPORT_BENCHES}, set(PHASE_METRICS["transport"]))self.assertTrue(all(limit > 0 for _, _, limit in TRANSPORT_BENCHES))diff --git a/scripts/benchmarks/tests/test_transport_switch_fetch.py b/scripts/benchmarks/tests/test_transport_switch_fetch.pynew file mode 100644index 0000000000..a6da68cc37--- /dev/null+++ b/scripts/benchmarks/tests/test_transport_switch_fetch.py@@ -0,0 +1,166 @@+from __future__ import annotations++import signal+import subprocess+import tempfile+import unittest+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import Mock, patch++from schema import PHASE_METRICS, ROOT, Observation, Side+from worker import TRANSPORT_BENCHES, stop_transport_processes, transport, transport_value++REPO = ROOT.parent.parent+++class TransportValueTests(unittest.TestCase):+ def test_reads_the_result_line_and_converts_to_float(self):+ self.assertEqual(transport_value('noise\nRESULT {"value": 2}'), 2.0)+ self.assertEqual(transport_value('RESULT {"value": 1, "switch_window_ms": 339}\n'), 1.0)++ def test_rejects_missing_or_malformed_results(self):+ for output in (+ "",+ "no result line at all",+ 'RESULT {"value": "1"}',+ 'RESULT {"value": true}',+ 'RESULT {"value": -1}',+ 'RESULT {"value": float("inf")}',+ 'RESULT {"switch_window_ms": 339}',+ "RESULT [1, 2, 3]",+ ):+ with self.subTest(output=output):+ with self.assertRaises((RuntimeError, ValueError)):+ transport_value(output)+++class TransportTests(unittest.TestCase):+ def test_records_the_harness_result_with_an_explicit_timeout(self):+ side = Side(sha="a" * 40)+ run_as = Mock(return_value='RESULT {"value": 1}\n')+ with tempfile.TemporaryDirectory() as directory:+ with (+ patch("worker.run_as", run_as),+ patch("worker.RESULTS", Path(directory)),+ patch("worker.stop_transport_processes") as stop,+ ):+ transport(side, 0)+ saved = list(Path(directory).glob("*.output"))+ self.assertEqual(side.metrics["switch_fetch"], [Observation(trial=0, value=1.0)])+ for _script, _metric, timeout in TRANSPORT_BENCHES:+ self.assertGreater(timeout, 0)+ # The transport phase drives every registered bench, so the switch-fetch+ # result and its sibling phase entries are recorded together.+ self.assertEqual(run_as.call_count, len(TRANSPORT_BENCHES))+ for script, _metric, timeout in TRANSPORT_BENCHES:+ run_as.assert_any_call(+ "builder",+ [+ "node",+ str(ROOT / script),+ "--dist",+ str(Path("/home/builder/source/packages/coding-agent/dist")),+ ],+ Path("/home/builder/source"),+ timeout=timeout,+ merge_output=True,+ )+ stop.assert_called_once()+ self.assertEqual(+ sorted(path.name for path in saved),+ sorted(f"{metric}-0.output" for _s, metric, _t in TRANSPORT_BENCHES),+ )++ def test_records_the_harness_failure_and_keeps_the_sweep(self):+ side = Side(sha="a" * 40)+ failure = subprocess.CalledProcessError(+ 1, "node", output="switch-fetch-bench failed: switch produced an empty transcript\n"+ )+ with tempfile.TemporaryDirectory() as directory:+ with (+ patch("worker.run_as", Mock(side_effect=failure)),+ patch("worker.RESULTS", Path(directory)),+ patch("worker.stop_transport_processes") as stop,+ ):+ transport(side, 3)+ saved = (Path(directory) / "switch_fetch-3.output").read_text()+ sample = side.metrics["switch_fetch"][0]+ self.assertIsNone(sample.value)+ self.assertIn("non-zero exit status", sample.error)+ self.assertIn("empty transcript", sample.error)+ self.assertIn("empty transcript", saved)+ stop.assert_called_once()++ def test_records_the_timeout_failure_without_harness_output(self):+ side = Side(sha="a" * 40)+ failure = subprocess.TimeoutExpired("node", 180)+ with tempfile.TemporaryDirectory() as directory:+ with (+ patch("worker.run_as", Mock(side_effect=failure)),+ patch("worker.RESULTS", Path(directory)),+ patch("worker.stop_transport_processes") as stop,+ ):+ transport(side, 0)+ saved = list(Path(directory).glob("*.output"))+ sample = side.metrics["switch_fetch"][0]+ self.assertIsNone(sample.value)+ self.assertIn("timed out", sample.error)+ self.assertEqual(saved, [])+ stop.assert_called_once()++ def test_the_transport_phase_covers_every_benchmark_metric(self):+ recorded = {metric for _, metric, _ in TRANSPORT_BENCHES}+ self.assertEqual(recorded, set(PHASE_METRICS["transport"]))+++class StopTransportProcessesTests(unittest.TestCase):+ def test_sweeps_builder_node_processes_with_sigterm_then_returns(self):+ node = SimpleNamespace(name="node", pid=4242)+ server = SimpleNamespace(name="python3", pid=4243)+ kills = []+ os = Mock()+ os.kill = lambda pid, sig: kills.append((pid, sig))+ sightings = [[node, server], []]+ with (+ patch("worker.pwd", Mock(getpwnam=Mock(return_value=SimpleNamespace(pw_uid=1500)))),+ patch("worker.memory", Mock(side_effect=lambda uid: sightings.pop(0) if sightings else [])),+ patch("worker.os", os),+ ):+ stop_transport_processes()+ # Only the daemon-shaped node process is signalled; the python3 artifact server is untouched.+ self.assertEqual(kills, [(4242, signal.SIGTERM)])++ def test_raises_when_node_processes_survive_the_sweep(self):+ node = SimpleNamespace(name="node", pid=4242)+ with (+ patch("worker.pwd", Mock(getpwnam=Mock(return_value=SimpleNamespace(pw_uid=1500)))),+ patch("worker.memory", Mock(return_value=[node])),+ patch("worker.os", Mock()),+ ):+ with self.assertRaisesRegex(RuntimeError, "node processes remain"):+ stop_transport_processes()+++class SwitchFetchHarnessTests(unittest.TestCase):+ # test-policy: allow conditional-or-disabled-test -- the built coding-agent dist is required+ @unittest.skipUnless(+ (REPO / "packages/coding-agent/dist/modes/agent-connection/daemon-agent-connection.js").exists(),+ "the coding-agent build is required to run the switch-fetch harness",+ )+ def test_switch_fetch_harness_counts_one_full_history_transfer(self):+ dist = REPO / "packages/coding-agent/dist"+ completed = subprocess.run(+ ["node", str(ROOT / "switch-fetch-bench.mjs"), "--dist", str(dist)],+ capture_output=True,+ text=True,+ timeout=150,+ check=False,+ )+ self.assertEqual(completed.returncode, 0, completed.stderr)+ transfers = transport_value(completed.stdout)+ self.assertEqual(transfers, 1.0)+++if __name__ == "__main__":+ unittest.main()diff --git a/scripts/benchmarks/worker.py b/scripts/benchmarks/worker.pyindex 145ffed215..161fdb4a66 100644--- a/scripts/benchmarks/worker.py+++ b/scripts/benchmarks/worker.py@@ -43,7 +43,10 @@ VERSION = "0.0.0-benchmark"ORIGIN = "http://127.0.0.1:18741"BUN_VERSION = "1.4.0"# Node transport benches: harness script, recorded metric, and the run timeout.-TRANSPORT_BENCHES: tuple[tuple[str, Metric, int], ...] = (("frame-decode-bench.mjs", "frame_decode", 180),)+TRANSPORT_BENCHES: tuple[tuple[str, Metric, int], ...] = (+ ("switch-fetch-bench.mjs", "switch_fetch", 180),+ ("frame-decode-bench.mjs", "frame_decode", 180),+)def clean_error(error: Exception) -> str: