A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
remove shared worker snapshot spill cache (#407)
kt18ea40867723
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex 43ec95b972..a6705c8a74 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -13,8 +13,10 @@- Fixed IPython edit diffs replacing syntax highlighting with a single foreground color ([ENG-4616](https://linear.app/primeintellect/issue/ENG-4616/syntax-highlighting-is-overridden-in-diff-view)).- Fixed Prime Inference login leaving new sessions without a persisted model selection ([ENG-4573](https://linear.app/primeintellect/issue/ENG-4573/prompt-for-model-selection-after-prime-inference-login)).- Fixed empty prompt placeholders hiding the input caret.+- Fixed update restarts starting concurrent daemon supervisors or unlinking a replacement supervisor's socket ([ENG-4600](https://linear.app/primeintellect/issue/ENG-4600/prevent-concurrent-daemon-supervisors-after-update-restart)).- Fixed automatic model selection preferring other configured providers over Prime Inference's GLM 5.2 default.- Fixed missing ripgrep blocking subagents and added actionable installation guidance for the optional search helper ([ENG-4572](https://linear.app/primeintellect/issue/ENG-4572/ripgrep-not-installed)).+- Removed the shared worker snapshot spill cache to prevent concurrent workers from deleting each other's snapshot chunks ([ENG-4601](https://linear.app/primeintellect/issue/ENG-4601/remove-shared-worker-snapshot-spill-cache-directories)).- Fixed narrow slash-command descriptions ending abruptly or clearing the prompt background, and added a content-sized popup above the input with the same distinct surface as `/btw` ([ENG-4542](https://linear.app/primeintellect/issue/ENG-4542/command-descriptions-are-cut-off-on-narrow-screens)).- Fixed snapshot transfers terminating resident workers, stranding partial readers, or rejecting identical retries ([ENG-4602](https://linear.app/primeintellect/issue/ENG-4602/make-snapshot-transfers-idempotent-and-non-fatal)).- Fixed the resume picker opening on an older session instead of the newest session ([ENG-4630](https://linear.app/primeintellect/issue/ENG-4630/show-latest-sessions-first-in-resume-list)).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 202e0546e4..19549a3d40 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@@ -931,7 +931,7 @@ export class DaemonAgentConnection implements AgentConnection {return;}if ("snapshotId" in message && this.ignoredSnapshotIds.has(message.snapshotId)) {- if (message.type === "session_snapshot_end") {+ if (message.type === "session_snapshot_end" || message.type === "session_snapshot_failed") {this.ignoredSnapshotIds.delete(message.snapshotId);}return;@@ -949,6 +949,17 @@ export class DaemonAgentConnection implements AgentConnection {await this.completeSnapshotAssembly(message);return;}+ if (message.type === "session_snapshot_failed") {+ const assembly = this.getSnapshotAssembly(message.snapshotId);+ const purpose = assembly.begin?.purpose ?? "attach";+ const snapshotError = new Error(message.error);+ this.rejectSnapshotAssembly(message.snapshotId, assembly, snapshotError);+ this.ignoreSnapshotId(message.snapshotId);+ if (purpose === "replacement" || purpose === "resync") {+ await this.recoverFailedSnapshot(purpose, snapshotError);+ }+ return;+ }if (this.isStaleSequencedMessage(message)) {return;}@@ -1223,6 +1234,35 @@ export class DaemonAgentConnection implements AgentConnection {}}+ private async recoverFailedSnapshot(purpose: "replacement" | "resync", snapshotError: Error): Promise<void> {+ this.latestSnapshotIsFresh = false;+ if (purpose === "replacement") {+ this.latestSnapshot = undefined;+ }+ try {+ const snapshot = await this.getInitialSnapshot();+ if (this.disposed) {+ return;+ }+ this.attachedSessionId = snapshot.state.sessionId;+ this.attachedSessionFile = snapshot.state.sessionFile;+ if (purpose === "replacement") {+ await this.emit({ type: "session_replaced", state: snapshot.state, messages: snapshot.messages });+ } else {+ await this.emit({ type: "session_resynced", snapshot });+ }+ } catch (recoveryError) {+ if (this.disposed) {+ return;+ }+ this.terminalCloseEmitted = true;+ await this.emit({+ type: "closed",+ error: `Failed to recover from a ${purpose} snapshot transfer. Snapshot error: ${formatErrorSentence(snapshotError)} Recovery error: ${formatErrorSentence(recoveryError)} ${this.formatDaemonDiagnosticContext()}`,+ });+ }+ }+private async waitForSnapshot(snapshotId: string): Promise<DaemonSessionSnapshot> {const assembly = this.getSnapshotAssembly(snapshotId);try {diff --git a/packages/coding-agent/src/modes/daemon/active-session-state.ts b/packages/coding-agent/src/modes/daemon/active-session-state.tsindex 098aa7987a..48196e155d 100644--- a/packages/coding-agent/src/modes/daemon/active-session-state.ts+++ b/packages/coding-agent/src/modes/daemon/active-session-state.ts@@ -19,6 +19,7 @@ export interface DaemonSocketClient {snapshotStreaming?: boolean;snapshotActiveSessionIds?: Set<string>;snapshotActiveSessionCounts?: Map<string, number>;+ snapshotTransferAbortControllers?: Map<string, AbortController>;detachInput: () => void;supportsExtensionUi: boolean;capabilities: Set<DaemonClientCapability>;diff --git a/packages/coding-agent/src/modes/daemon/daemon-mode.ts b/packages/coding-agent/src/modes/daemon/daemon-mode.tsindex 6a0f32d3c4..d5ac89d1c9 100644--- a/packages/coding-agent/src/modes/daemon/daemon-mode.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-mode.ts@@ -156,7 +156,11 @@ import {SESSION_LEASE_OWNER_ID_ENV,SESSION_LEASES_ENABLED_ENV,} from "./daemon-worker-protocol.js";-import { SNAPSHOT_TARGET_CHUNK_BYTES, SnapshotTranscriptCache } from "./snapshot-transcript-cache.js";+import {+ createSnapshotTranscriptChunks,+ SNAPSHOT_TARGET_CHUNK_BYTES,+ type SnapshotTranscriptChunkSource,+} from "./snapshot-transcript-cache.js";import { WorkerRecoveryJournal } from "./worker-recovery-journal.js";export interface DaemonModeOptions {@@ -174,6 +178,7 @@ export type { SessionActivity, SessionLifecycle, SessionSummary } from "./daemonexport { defaultDaemonSocketPath } from "./daemon-socket.js";const structuredLog = getLogger("coding-agent.daemon");+const WORKER_SNAPSHOT_TERMINAL_DRAIN_TIMEOUT_MS = 1_000;const DAEMON_COMMAND_TYPES: ReadonlySet<string> = new Set(["ack_result",@@ -1993,9 +1998,9 @@ export class AgentDaemon {client.transport === "private-framed" &&daemonClientCapabilitiesForSession(client, state.activeSessionId).has("chunked_snapshot");this.adoptClientEnv(state, filterClientEnv(command.env));- if (streamsSnapshot) {- markClientSnapshotStreaming(client, state.activeSessionId);- }+ const snapshotSignal = streamsSnapshot+ ? markClientSnapshotStreaming(client, state.activeSessionId)+ : undefined;state.clients.add(client);client.attachedActiveSessionIds.add(state.activeSessionId);let result: DaemonAttachResult;@@ -2011,14 +2016,15 @@ export class AgentDaemon {throw error;}if (streamsSnapshot) {- let transcript: SnapshotTranscriptCache;+ const snapshotId = `${state.activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`;+ let transcript: SnapshotTranscriptChunkSource;try {- transcript = new SnapshotTranscriptCache({+ transcript = createSnapshotTranscriptChunks({activeSessionId: state.activeSessionId,- snapshotId: `${state.activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`,+ snapshotId,messages: result.snapshot.messages,- cacheRoot: join(this.agentDir, "worker-snapshot-cache"),targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,+ signal: snapshotSignal,});} catch (error) {state.clients.delete(client);@@ -2032,15 +2038,20 @@ export class AgentDaemon {messages: result.messages ? [] : undefined,snapshot: { ...result.snapshot, messages: [] },snapshotStream: {- id: transcript.snapshotId,+ id: snapshotId,messageCount: result.snapshot.messages.length,- targetChunkBytes: transcript.targetChunkBytes,+ targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,},};setImmediate(() => {- void this.streamWorkerSnapshot(client, streamedResult, transcript, "attach", true).catch((error) =>- this.log(`could not stream attach snapshot: ${String(error)}`),- );+ void this.streamWorkerSnapshot(+ client,+ streamedResult,+ transcript,+ "attach",+ snapshotSignal,+ true,+ ).catch((error) => this.log(`could not stream attach snapshot: ${String(error)}`));});return success(command.id, "attach", streamedResult);}@@ -2808,8 +2819,9 @@ export class AgentDaemon {private async streamWorkerSnapshot(client: DaemonSocketClient,result: DaemonAttachResult,- transcript: SnapshotTranscriptCache,+ transcript: SnapshotTranscriptChunkSource,purpose: "attach" | "replacement" | "catchup" = "attach",+ signal?: AbortSignal,snapshotAlreadyMarked = false,): Promise<void> {const stream = result.snapshotStream;@@ -2817,47 +2829,103 @@ export class AgentDaemon {if (snapshotAlreadyMarked) {finishClientSnapshotStreaming(client, result.activeSessionId);}- transcript.dispose();+ transcript.dispose?.();return;}- if (!snapshotAlreadyMarked) {- markClientSnapshotStreaming(client, result.activeSessionId);+ if (snapshotAlreadyMarked && !signal) {+ throw new Error(`Snapshot ${stream.id} is missing its transfer signal`);}+ const transferSignal = signal ?? markClientSnapshotStreaming(client, result.activeSessionId);if (client.socket.destroyed) {finishClientSnapshotStreaming(client, result.activeSessionId);- transcript.dispose();+ transcript.dispose?.();return;}const { messages: _messages, ...snapshot } = result.snapshot;- try {- if (- !(await this.writeWorkerSnapshotRecord(+ const snapshotBegin: DaemonOutbound = {+ type: "session_snapshot_begin",+ activeSessionId: result.activeSessionId,+ snapshotId: stream.id,+ snapshot,+ messageCount: stream.messageCount,+ targetChunkBytes: stream.targetChunkBytes,+ purpose: purpose === "catchup" ? "resync" : purpose,+ };+ const deliverSnapshotFailure = async (streamError: Error, includeBegin = false): Promise<void> => {+ transcript.markFailed?.(streamError);+ if (client.socket.destroyed) {+ return;+ }+ try {+ if (+ includeBegin &&+ !(await this.writeWorkerSnapshotRecord(+ client,+ snapshotBegin,+ purpose,+ undefined,+ WORKER_SNAPSHOT_TERMINAL_DRAIN_TIMEOUT_MS,+ ))+ ) {+ if (!client.socket.destroyed) {+ client.socket.destroy(streamError);+ }+ return;+ }+ const delivered = await this.writeWorkerSnapshotRecord(client,{- type: "session_snapshot_begin",+ type: "session_snapshot_failed",activeSessionId: result.activeSessionId,snapshotId: stream.id,- snapshot,- messageCount: stream.messageCount,- targetChunkBytes: stream.targetChunkBytes,- purpose: purpose === "catchup" ? "resync" : purpose,+ error: streamError.message,},purpose,- ))- ) {+ undefined,+ WORKER_SNAPSHOT_TERMINAL_DRAIN_TIMEOUT_MS,+ );+ if (!delivered && !client.socket.destroyed) {+ client.socket.destroy(streamError);+ }+ } catch (deliveryError) {+ client.socket.destroy(deliveryError instanceof Error ? deliveryError : new Error(String(deliveryError)));+ }+ };+ try {+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`), true);return;}- for (let index = 0; index < transcript.chunkCount; index++) {+ if (!(await this.writeWorkerSnapshotRecord(client, snapshotBegin, purpose, transferSignal))) {+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`));+ }+ return;+ }+ let chunkCount = 0;+ for await (const chunk of transcript) {+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`));+ return;+ }const headerMessage: DaemonOutbound = {type: "session_snapshot_chunk",activeSessionId: result.activeSessionId,snapshotId: stream.id,- index,+ index: chunkCount,messages: [],};- if (!(await this.writeWorkerSnapshotBuffer(client, transcript.readChunk(index), headerMessage, purpose))) {+ if (!(await this.writeWorkerSnapshotBuffer(client, chunk, headerMessage, purpose, transferSignal))) {+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`));+ }return;}+ chunkCount++;+ }+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`));+ return;}await this.writeWorkerSnapshotRecord(client,@@ -2865,21 +2933,24 @@ export class AgentDaemon {type: "session_snapshot_end",activeSessionId: result.activeSessionId,snapshotId: stream.id,- chunkCount: transcript.chunkCount,+ chunkCount,lastEventSequence: result.lastEventSequence,lastEventCursor: result.lastEventCursor,},purpose,+ transferSignal,);} catch (error) {+ if (transferSignal.aborted) {+ await deliverSnapshotFailure(new Error(`Snapshot ${stream.id} was aborted`));+ return;+ }const streamError = error instanceof Error ? error : new Error(String(error));- transcript.markFailed(streamError);- transcript.dispose();- client.socket.destroy(streamError);+ await deliverSnapshotFailure(streamError);throw streamError;} finally {finishClientSnapshotStreaming(client, result.activeSessionId);- transcript.dispose();+ transcript.dispose?.();if (!client.snapshotStreaming && client.catchupActiveSessionIds?.size) {void this.catchUpBackpressuredClient(client).catch((error) =>this.log(`could not catch up snapshot client ${client.id}: ${String(error)}`),@@ -2892,8 +2963,17 @@ export class AgentDaemon {client: DaemonSocketClient,message: DaemonOutbound,purpose: "attach" | "replacement" | "catchup",+ signal?: AbortSignal,+ drainTimeoutMs?: number,): Promise<boolean> {- return this.writeWorkerSnapshotBuffer(client, Buffer.from(serializeJsonLine(message)), message, purpose);+ return this.writeWorkerSnapshotBuffer(+ client,+ Buffer.from(serializeJsonLine(message)),+ message,+ purpose,+ signal,+ drainTimeoutMs,+ );}private async writeWorkerSnapshotBuffer(@@ -2901,8 +2981,10 @@ export class AgentDaemon {buffer: Buffer,message: DaemonOutbound,purpose: "attach" | "replacement" | "catchup",+ signal?: AbortSignal,+ drainTimeoutMs?: number,): Promise<boolean> {- if (client.socket.destroyed) {+ if (signal?.aborted || client.socket.destroyed) {return false;}if (this.writeSerialized(client, buffer, message, "jsonl", purpose)) {@@ -2910,6 +2992,7 @@ export class AgentDaemon {}return new Promise<boolean>((resolveDrain) => {let settled = false;+ let drainTimeout: NodeJS.Timeout | undefined;const finish = (value: boolean) => {if (settled) {return;@@ -2918,13 +3001,26 @@ export class AgentDaemon {client.socket.off("drain", onDrain);client.socket.off("close", onClose);client.socket.off("error", onClose);+ signal?.removeEventListener("abort", onAbort);+ if (drainTimeout) {+ clearTimeout(drainTimeout);+ }resolveDrain(value);};const onDrain = () => finish(true);const onClose = () => finish(false);+ const onAbort = () => finish(false);client.socket.once("drain", onDrain);client.socket.once("close", onClose);client.socket.once("error", onClose);+ signal?.addEventListener("abort", onAbort, { once: true });+ if (drainTimeoutMs !== undefined) {+ drainTimeout = setTimeout(() => finish(false), drainTimeoutMs);+ drainTimeout.unref();+ }+ if (signal?.aborted || client.socket.destroyed) {+ finish(false);+ }});}@@ -3257,6 +3353,7 @@ export class AgentDaemon {private detachClientFromSession(client: DaemonSocketClient, state: ActiveSessionState): void {this.abortSideQuestionsFor(client, state.activeSessionId);+ abortClientSnapshotStreaming(client, state.activeSessionId);detachClientFromActiveSession(client, state);this.write(client, { type: "session_detached", activeSessionId: state.activeSessionId });// Abandoned new-chat: discard it so it doesn't linger in memory or leave an@@ -3528,6 +3625,7 @@ export class AgentDaemon {this.detachClientFromSession(client, state);}}+ abortClientSnapshotStreaming(client);}private findActiveSessionByFile(sessionPath: string): ActiveSessionState | undefined {@@ -3705,23 +3803,30 @@ export class AgentDaemon {type: "attach",activeSessionId: state.activeSessionId,});- const transcript = new SnapshotTranscriptCache({- activeSessionId: state.activeSessionId,- snapshotId: `${state.activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`,- messages: result.snapshot.messages,- cacheRoot: join(this.agentDir, "worker-snapshot-cache"),- targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,- });+ const snapshotId = `${state.activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`;const accepted = this.write(client, {...sequencedMessage,messages: [],snapshotFollows: true,});if (!accepted) {- transcript.dispose();this.queueClientCatchup(client, state.activeSessionId, "replacement");continue;}+ const snapshotSignal = markClientSnapshotStreaming(client, state.activeSessionId);+ let transcript: SnapshotTranscriptChunkSource;+ try {+ transcript = createSnapshotTranscriptChunks({+ activeSessionId: state.activeSessionId,+ snapshotId,+ messages: result.snapshot.messages,+ targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,+ signal: snapshotSignal,+ });+ } catch (error) {+ finishClientSnapshotStreaming(client, state.activeSessionId);+ throw error;+ }void this.streamWorkerSnapshot(client,{@@ -3729,13 +3834,15 @@ export class AgentDaemon {messages: result.messages ? [] : undefined,snapshot: { ...result.snapshot, messages: [] },snapshotStream: {- id: transcript.snapshotId,+ id: snapshotId,messageCount: result.snapshot.messages.length,- targetChunkBytes: transcript.targetChunkBytes,+ targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,},},transcript,"replacement",+ snapshotSignal,+ true,).catch((error) => {this.log(`could not stream replacement snapshot: ${String(error)}`);this.queueClientCatchup(client, state.activeSessionId, "replacement");@@ -3838,13 +3945,21 @@ export class AgentDaemon {),});}- const transcript = new SnapshotTranscriptCache({- activeSessionId,- snapshotId: `${activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`,- messages: result.snapshot.messages,- cacheRoot: join(this.agentDir, "worker-snapshot-cache"),- targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,- });+ const snapshotId = `${activeSessionId}-${state.eventGeneration}-${state.lastEventSequence}`;+ const snapshotSignal = markClientSnapshotStreaming(client, activeSessionId);+ let transcript: SnapshotTranscriptChunkSource;+ try {+ transcript = createSnapshotTranscriptChunks({+ activeSessionId,+ snapshotId,+ messages: result.snapshot.messages,+ targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,+ signal: snapshotSignal,+ });+ } catch (error) {+ finishClientSnapshotStreaming(client, activeSessionId);+ throw error;+ }await this.streamWorkerSnapshot(client,{@@ -3852,13 +3967,15 @@ export class AgentDaemon {messages: result.messages ? [] : undefined,snapshot: { ...result.snapshot, messages: [] },snapshotStream: {- id: transcript.snapshotId,+ id: snapshotId,messageCount: result.snapshot.messages.length,- targetChunkBytes: transcript.targetChunkBytes,+ targetChunkBytes: SNAPSHOT_TARGET_CHUNK_BYTES,},},transcript,purpose === "replacement" ? "replacement" : "catchup",+ snapshotSignal,+ true,);continue;}@@ -4021,6 +4138,7 @@ export class AgentDaemon {this.log(`shutting down (exit ${exitCode}); closing ${this.sessions.size} active session(s)`);const closingReason: DaemonClosingReason = this.updateRestartPreparing ? "update" : "shutdown";for (const client of this.clients) {+ abortClientSnapshotStreaming(client);this.write(client, { type: "daemon_closing", reason: closingReason });}@@ -4121,7 +4239,7 @@ function daemonClientSupportsExtensionUi(client: DaemonSocketClient, activeSessireturn client.capabilitiesByActiveSessionId?.get(activeSessionId)?.has("extension_ui") ?? client.supportsExtensionUi;}-export function markClientSnapshotStreaming(client: DaemonSocketClient, activeSessionId: string): void {+export function markClientSnapshotStreaming(client: DaemonSocketClient, activeSessionId: string): AbortSignal {client.snapshotStreaming = true;client.snapshotActiveSessionIds ??= new Set();client.snapshotActiveSessionIds.add(activeSessionId);@@ -4130,6 +4248,23 @@ export function markClientSnapshotStreaming(client: DaemonSocketClient, activeSeactiveSessionId,(client.snapshotActiveSessionCounts.get(activeSessionId) ?? 0) + 1,);+ client.snapshotTransferAbortControllers ??= new Map();+ let controller = client.snapshotTransferAbortControllers.get(activeSessionId);+ if (!controller || controller.signal.aborted) {+ controller = new AbortController();+ client.snapshotTransferAbortControllers.set(activeSessionId, controller);+ }+ return controller.signal;+}++function abortClientSnapshotStreaming(client: DaemonSocketClient, activeSessionId?: string): void {+ if (activeSessionId) {+ client.snapshotTransferAbortControllers?.get(activeSessionId)?.abort();+ return;+ }+ for (const controller of client.snapshotTransferAbortControllers?.values() ?? []) {+ controller.abort();+ }}export function finishClientSnapshotStreaming(client: DaemonSocketClient, activeSessionId: string): void {@@ -4139,6 +4274,7 @@ export function finishClientSnapshotStreaming(client: DaemonSocketClient, active} else {client.snapshotActiveSessionCounts?.delete(activeSessionId);client.snapshotActiveSessionIds?.delete(activeSessionId);+ client.snapshotTransferAbortControllers?.delete(activeSessionId);}client.snapshotStreaming = (client.snapshotActiveSessionIds?.size ?? 0) > 0;if (!client.snapshotStreaming) {diff --git a/packages/coding-agent/src/modes/daemon/daemon-protocol.ts b/packages/coding-agent/src/modes/daemon/daemon-protocol.tsindex 5044dd4bd1..cb0455a7c3 100644--- a/packages/coding-agent/src/modes/daemon/daemon-protocol.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-protocol.ts@@ -594,6 +594,12 @@ export type DaemonOutbound =lastEventSequence: DaemonEventSequence;lastEventCursor?: DaemonEventCursor;}+ | {+ type: "session_snapshot_failed";+ activeSessionId: string;+ snapshotId: string;+ error: string;+ }| { type: "session_detached"; activeSessionId: string }| { type: "session_closed"; activeSessionId: string; reason: DaemonSessionClosedReason; meta?: DaemonEventMeta }| {diff --git a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts b/packages/coding-agent/src/modes/daemon/daemon-supervisor.tsindex 97df573266..0b2cc45f96 100644--- a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts@@ -1912,6 +1912,11 @@ export class DaemonSupervisor {client.snapshotActiveSessionIds = new Set();}client.snapshotActiveSessionIds.add(result.activeSessionId);+ client.snapshotActiveSessionCounts ??= new Map();+ client.snapshotActiveSessionCounts.set(+ result.activeSessionId,+ (client.snapshotActiveSessionCounts.get(result.activeSessionId) ?? 0) + 1,+ );const { messages: _messages, ...snapshotHeader } = result.snapshot;try {if (@@ -1949,10 +1954,30 @@ export class DaemonSupervisor {} catch (error) {const streamError = error instanceof Error ? error : new Error(String(error));this.failWorkerSnapshotCache(worker, result.activeSessionId, streamError);- client.socket.destroy(streamError);+ if (!client.socket.destroyed) {+ try {+ const delivered = await this.writeSnapshotRecord(client, {+ type: "session_snapshot_failed",+ activeSessionId: result.activeSessionId,+ snapshotId: stream.id,+ error: streamError.message,+ });+ if (!delivered && !client.socket.destroyed) {+ client.socket.destroy(streamError);+ }+ } catch (deliveryError) {+ client.socket.destroy(deliveryError instanceof Error ? deliveryError : new Error(String(deliveryError)));+ }+ }throw streamError;} finally {- client.snapshotActiveSessionIds?.delete(result.activeSessionId);+ const streamCount = client.snapshotActiveSessionCounts?.get(result.activeSessionId) ?? 1;+ if (streamCount > 1) {+ client.snapshotActiveSessionCounts?.set(result.activeSessionId, streamCount - 1);+ } else {+ client.snapshotActiveSessionCounts?.delete(result.activeSessionId);+ client.snapshotActiveSessionIds?.delete(result.activeSessionId);+ }client.snapshotStreaming = (client.snapshotActiveSessionIds?.size ?? 0) > 0;if (!client.snapshotStreaming) {client.backpressured = false;@@ -2366,6 +2391,49 @@ export class DaemonSupervisor {}return;}+ if (outboundType === "session_snapshot_failed" && activeSessionId) {+ try {+ const failed = JSON.parse(frame.payload.toString("utf8")) as Extract<+ DaemonOutbound,+ { type: "session_snapshot_failed" }+ >;+ const transcript = worker.transcriptCaches.get(activeSessionId);+ const cachedResult = worker.snapshotCache.get(activeSessionId);+ const expectedSnapshotId = transcript?.snapshotId ?? cachedResult?.snapshotStream?.id;+ if (+ failed.type !== "session_snapshot_failed" ||+ failed.activeSessionId !== activeSessionId ||+ typeof failed.snapshotId !== "string" ||+ typeof failed.error !== "string" ||+ expectedSnapshotId !== failed.snapshotId+ ) {+ throw new Error("Worker returned an invalid snapshot failure frame");+ }+ this.failWorkerSnapshotCache(worker, activeSessionId, new Error(failed.error));+ if (snapshotPurpose === "replacement" || snapshotPurpose === "catchup") {+ for (const client of this.clients) {+ if (!client.attachedActiveSessionIds.has(activeSessionId)) continue;+ if (snapshotPurpose === "replacement" && client.capabilities.has("chunked_snapshot")) continue;+ this.queueCatchup(+ client,+ activeSessionId,+ snapshotPurpose === "replacement" ? "replacement" : "resync",+ );+ void this.catchUpClient(client).catch((error) =>+ this.log(`Failed to catch up client ${client.id}: ${String(error)}`),+ );+ }+ }+ } catch (error) {+ this.failWorkerSnapshotCache(+ worker,+ activeSessionId,+ error instanceof Error ? error : new Error(String(error)),+ true,+ );+ }+ return;+ }if (outboundType === "daemon_hello" ||outboundType === "response" ||diff --git a/packages/coding-agent/src/modes/daemon/snapshot-transcript-cache.ts b/packages/coding-agent/src/modes/daemon/snapshot-transcript-cache.tsindex 5996c63118..874c5632e7 100644--- a/packages/coding-agent/src/modes/daemon/snapshot-transcript-cache.ts+++ b/packages/coding-agent/src/modes/daemon/snapshot-transcript-cache.ts@@ -19,6 +19,58 @@ export interface SnapshotTranscriptCacheOptions {memoryCacheBytes?: number;}+export type SnapshotTranscriptChunkSource = (Iterable<Buffer> | AsyncIterable<Buffer>) & {+ markFailed?(error: Error): void;+ dispose?(): void;+};++export function createSnapshotTranscriptChunks(options: {+ activeSessionId: string;+ snapshotId: string;+ messages: readonly AgentMessage[];+ targetChunkBytes?: number;+ signal?: AbortSignal;+}): Iterable<Buffer> {+ const messages = [...options.messages];+ const targetChunkBytes = options.targetChunkBytes ?? SNAPSHOT_TARGET_CHUNK_BYTES;+ return {+ *[Symbol.iterator](): Iterator<Buffer> {+ options.signal?.throwIfAborted();+ let serializedMessages: string[] = [];+ let serializedBytes = 0;+ let index = 0;+ const flush = (): Buffer | undefined => {+ if (serializedMessages.length === 0) {+ return undefined;+ }+ const prefix =+ `{"type":"session_snapshot_chunk","activeSessionId":${JSON.stringify(options.activeSessionId)},` ++ `"snapshotId":${JSON.stringify(options.snapshotId)},"index":${index},"messages":[`;+ const line = Buffer.from(`${prefix}${serializedMessages.join(",")}]}\n`);+ serializedMessages = [];+ serializedBytes = 0;+ index++;+ return line;+ };++ for (const message of messages) {+ options.signal?.throwIfAborted();+ const serialized = JSON.stringify(message);+ const bytes = Buffer.byteLength(serialized) + (serializedMessages.length > 0 ? 1 : 0);+ if (serializedMessages.length > 0 && serializedBytes + bytes > targetChunkBytes) {+ const chunk = flush();+ if (chunk) yield chunk;+ }+ serializedMessages.push(serialized);+ serializedBytes += bytes;+ }+ options.signal?.throwIfAborted();+ const chunk = flush();+ if (chunk) yield chunk;+ },+ };+}+export class SnapshotTranscriptCache {private readonly chunks: SnapshotTranscriptChunk[] = [];private cacheDirectory?: string;@@ -76,6 +128,12 @@ export class SnapshotTranscriptCache {return readFileSync(chunk.path);}+ *[Symbol.iterator](): Iterator<Buffer> {+ for (let index = 0; index < this.chunkCount; index++) {+ yield this.readChunk(index);+ }+ }+appendEncodedChunk(buffer: Buffer): void {if (this.completed || this.failure || this.disposed) {throw new Error(`Snapshot transcript ${this.snapshotId} is not writable`);diff --git a/packages/coding-agent/test/agent-connection-daemon.test.ts b/packages/coding-agent/test/agent-connection-daemon.test.tsindex eba3fd2d9b..a4428d3ccb 100644--- a/packages/coding-agent/test/agent-connection-daemon.test.ts+++ b/packages/coding-agent/test/agent-connection-daemon.test.ts@@ -42,6 +42,7 @@ class FakeDaemonClient {reconnectError: Error | undefined;attachFailures = 0;connectionStateGate: Promise<void> | undefined;+ connectionStateFactory: ((activeSessionId: string) => AgentConnectionState) | undefined;abortBashUnknownCommand = false;abortAndClearQueueUnknownCommand = false;updateRestartSessions: Array<Record<string, unknown>> = [];@@ -101,7 +102,9 @@ class FakeDaemonClient {type: "response",command: command.type,success: true,- data: createConnectionState(command.activeSessionId, "session-current"),+ data:+ this.connectionStateFactory?.(command.activeSessionId) ??+ createConnectionState(command.activeSessionId, "session-current"),};case "get_messages":return {@@ -1271,6 +1274,54 @@ describe("DaemonAgentConnection", () => {expect((connection as unknown as { snapshotAssemblies: Map<string, unknown> }).snapshotAssemblies.size).toBe(0);});+ it("rejects one failed snapshot without interrupting another session on the shared client", async () => {+ const fakeClient = new FakeDaemonClient();+ const sibling = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-2");+ await sibling.attach();+ const siblingEvents: AgentConnectionEvent[] = [];+ sibling.subscribe((event) => {+ siblingEvents.push(event);+ });+ fakeClient.attachResultFactory = (command) => {+ const result = createAttachResult(command.activeSessionId, command.clientId, command.capabilities, 12);+ if (command.activeSessionId !== "active-1") {+ return result;+ }+ const { messages: _messages, ...snapshot } = result.snapshot;+ queueMicrotask(() => {+ fakeClient.emitMessage({+ type: "session_snapshot_begin",+ activeSessionId: command.activeSessionId,+ snapshotId: "snapshot-failed",+ snapshot,+ messageCount: 0,+ targetChunkBytes: 512 * 1024,+ });+ fakeClient.emitMessage({+ type: "session_snapshot_failed",+ activeSessionId: command.activeSessionId,+ snapshotId: "snapshot-failed",+ error: "snapshot encoder failed",+ });+ });+ return {+ ...result,+ snapshot: { ...result.snapshot, messages: [] },+ snapshotStream: { id: "snapshot-failed", messageCount: 0, targetChunkBytes: 512 * 1024 },+ };+ };+ const failed = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1");++ await expect(failed.attach()).rejects.toThrow("snapshot encoder failed");+ emitSequencedQueueUpdate(fakeClient, "active-2", 13);+ await vi.waitFor(() => expect(siblingEvents).toHaveLength(1));++ expect(siblingEvents[0]).toMatchObject({ type: "session_event", event: { type: "queue_update" } });+ expect(fakeClient.closeCount).toBe(0);+ await failed.dispose();+ await sibling.dispose();+ });+it("assembles chunked attach snapshots even when chunks arrive before the attach response continuation", async () => {const fakeClient = new FakeDaemonClient();const messages: AgentMessage[] = [@@ -1406,6 +1457,99 @@ describe("DaemonAgentConnection", () => {expect((connection as unknown as { snapshotAssemblies: Map<string, unknown> }).snapshotAssemblies.size).toBe(0);});+ it.each(["replacement", "resync"] as const)(+ "recovers a failed chunked %s snapshot without interrupting a sibling session",+ async (purpose) => {+ const fakeClient = new FakeDaemonClient();+ const connection = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-1");+ const sibling = new DaemonAgentConnection(asDaemonClient(fakeClient), "active-2");+ await Promise.all([connection.attach(), sibling.attach()]);+ const events: AgentConnectionEvent[] = [];+ const siblingEvents: AgentConnectionEvent[] = [];+ connection.subscribe((event) => {+ events.push(event);+ });+ sibling.subscribe((event) => {+ siblingEvents.push(event);+ });+ const recoveredSessionId = purpose === "replacement" ? "session-next" : "session-current";+ fakeClient.connectionStateFactory = (activeSessionId) =>+ createConnectionState(+ activeSessionId,+ activeSessionId === "active-1" ? recoveredSessionId : "session-sibling",+ );+ fakeClient.requests.length = 0;+ const snapshotId = `snapshot-failed-${purpose}`;+ const full = createAttachResult("active-1", "client-1", undefined, 13, {+ state: createConnectionState("active-1", recoveredSessionId),+ });+ const { messages: _messages, ...snapshot } = full.snapshot;+ if (purpose === "replacement") {+ fakeClient.emitMessage({+ type: "session_replaced",+ activeSessionId: "active-1",+ state: createConnectionState("active-1", recoveredSessionId),+ messages: [],+ snapshotFollows: true,+ meta: {+ id: "active-1:13",+ protocol: DAEMON_PROTOCOL_INFO,+ activeSessionId: "active-1",+ sequence: 13,+ cursor: { generation: "generation-active-1", sequence: 13 },+ emittedAt: "2026-01-01T00:00:00.000Z",+ },+ });+ }+ fakeClient.emitMessage({+ type: "session_snapshot_begin",+ activeSessionId: "active-1",+ snapshotId,+ snapshot,+ messageCount: 1,+ targetChunkBytes: 512 * 1024,+ purpose,+ });+ fakeClient.emitMessage({+ type: "session_snapshot_failed",+ activeSessionId: "active-1",+ snapshotId,+ error: `${purpose} snapshot failed`,+ });++ await vi.waitFor(() => expect(events).toHaveLength(1));+ if (purpose === "replacement") {+ expect(events[0]).toMatchObject({+ type: "session_replaced",+ state: { sessionId: recoveredSessionId },+ messages: [{ role: "user", content: "current prompt", timestamp: 4 }],+ });+ } else {+ expect(events[0]).toMatchObject({+ type: "session_resynced",+ snapshot: {+ state: { sessionId: recoveredSessionId },+ messages: [{ role: "user", content: "current prompt", timestamp: 4 }],+ },+ });+ }+ expect(fakeClient.requests.map((request) => request.type)).toEqual([+ "get_connection_state",+ "get_messages",+ "get_session_context",+ ]);+ emitSequencedQueueUpdate(fakeClient, "active-2", 13);+ await vi.waitFor(() => expect(siblingEvents).toHaveLength(1));+ expect(siblingEvents[0]).toMatchObject({ type: "session_event", event: { type: "queue_update" } });+ expect(fakeClient.closeCount).toBe(0);+ expect((connection as unknown as { snapshotAssemblies: Map<string, unknown> }).snapshotAssemblies.size).toBe(+ 0,+ );+ await connection.dispose();+ await sibling.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-mode.test.ts b/packages/coding-agent/test/daemon-mode.test.tsindex 35a1221f64..4dcfdafdf9 100644--- a/packages/coding-agent/test/daemon-mode.test.ts+++ b/packages/coding-agent/test/daemon-mode.test.ts@@ -2442,10 +2442,11 @@ describe("daemon mode helpers", () => {messages: [],});- expect(write).toHaveBeenCalledTimes(1);+ expect(write).toHaveBeenCalledTimes(2);const replacementFrame = String(write.mock.calls[0]?.[0]);expect(replacementFrame).toContain('"type":"session_replaced"');- expect(replacementFrame).not.toContain('"snapshotFollows":true');+ expect(replacementFrame).toContain('"snapshotFollows":true');+ expect(String(write.mock.calls[1]?.[0])).toContain('"type":"session_snapshot_begin"');} finally {rmSync(root, { recursive: true, force: true });}diff --git a/packages/coding-agent/test/suite/regressions/4601-worker-snapshot-cache.test.ts b/packages/coding-agent/test/suite/regressions/4601-worker-snapshot-cache.test.tsnew file mode 100644index 0000000000..e94ed1c256--- /dev/null+++ b/packages/coding-agent/test/suite/regressions/4601-worker-snapshot-cache.test.ts@@ -0,0 +1,571 @@+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import type { Socket } from "node:net";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { PassThrough } from "node:stream";+import type { AgentMessage } from "@earendil-works/pi-agent-core";+import { afterEach, describe, expect, it, vi } from "vitest";+import type { ActiveSessionState, DaemonSocketClient } from "../../../src/modes/daemon/active-session-state.js";+import { AgentDaemon, markClientSnapshotStreaming } from "../../../src/modes/daemon/daemon-mode.js";+import {+ DAEMON_PROTOCOL_INFO,+ type DaemonAttachResult,+ type DaemonOutbound,+} from "../../../src/modes/daemon/daemon-protocol.js";+import { DaemonSupervisor } from "../../../src/modes/daemon/daemon-supervisor.js";+import {+ createSnapshotTranscriptChunks,+ SnapshotTranscriptCache,+ type SnapshotTranscriptChunkSource,+} from "../../../src/modes/daemon/snapshot-transcript-cache.js";++const tempDirectories: string[] = [];+const activeSessionId = "active-4601";+const snapshotId = "snapshot-4601";++afterEach(() => {+ for (const directory of tempDirectories.splice(0)) {+ rmSync(directory, { recursive: true, force: true });+ }+});++function tempDirectory(): string {+ const directory = mkdtempSync(join(tmpdir(), "eng-4601-"));+ tempDirectories.push(directory);+ return directory;+}++function messages(label: string): AgentMessage[] {+ return [+ { role: "user", content: `${label}-first`, timestamp: 1 },+ { role: "user", content: `${label}-second`, timestamp: 2 },+ ];+}++function streamedResult(+ messageCount: number,+ targetChunkBytes = 1,+ resultActiveSessionId = activeSessionId,+ resultSnapshotId = snapshotId,+): DaemonAttachResult {+ return {+ protocol: DAEMON_PROTOCOL_INFO,+ activeSessionId: resultActiveSessionId,+ snapshot: {+ activeSessionId: resultActiveSessionId,+ summary: {+ id: resultActiveSessionId,+ activeSessionId: resultActiveSessionId,+ lifecycle: "live",+ activity: "idle",+ sessionId: `session-${resultActiveSessionId}`,+ cwd: "/tmp",+ isStreaming: false,+ isCompacting: false,+ attachedClients: 0,+ messageCount,+ pendingMessageCount: 0,+ },+ state: {+ activeSessionId: resultActiveSessionId,+ sessionId: `session-${resultActiveSessionId}`,+ } as DaemonAttachResult["snapshot"]["state"],+ messages: [],+ lastEventSequence: 1,+ },+ replay: { status: "complete", toSequence: 1 },+ lastEventSequence: 1,+ snapshotStream: { id: resultSnapshotId, messageCount, targetChunkBytes },+ client: { id: "supervisor", capabilities: ["chunked_snapshot"] },+ };+}++function socketClient(id: string): { client: DaemonSocketClient; socket: PassThrough } {+ const socket = new PassThrough();+ return {+ socket,+ client: {+ id,+ socket: socket as unknown as Socket,+ transport: "private-framed",+ attachedActiveSessionIds: new Set([activeSessionId]),+ catchupActiveSessionIds: new Set<string>(),+ detachInput: () => {},+ supportsExtensionUi: false,+ capabilities: new Set(["chunked_snapshot"]),+ },+ };+}++describe("ENG-4601 worker snapshot cache", () => {+ it("preserves legacy JSONL bytes from a stable message-array snapshot", () => {+ const transcript: AgentMessage[] = [{ role: "user", content: 'line1\n"quoted"\\tail', timestamp: 1 }];+ const chunks = createSnapshotTranscriptChunks({+ activeSessionId: 'active-"\\',+ snapshotId: "snapshot-\n",+ messages: transcript,+ });+ transcript.push({ role: "user", content: "late", timestamp: 2 });++ expect([...chunks].map((chunk) => chunk.toString("utf8"))).toEqual([+ '{"type":"session_snapshot_chunk","activeSessionId":"active-\\"\\\\","snapshotId":"snapshot-\\n","index":0,"messages":[{"role":"user","content":"line1\\n\\"quoted\\"\\\\tail","timestamp":1}]}\n',+ ]);+ expect([...createSnapshotTranscriptChunks({ activeSessionId, snapshotId, messages: [] })]).toEqual([]);+ });++ it.each([1, 2])(+ "keeps %i same-ID worker stream(s) bounded by socket drain without shared cleanup",+ async (count) => {+ const root = tempDirectory();+ const oldSharedDirectory = join(root, "worker-snapshot-cache", snapshotId);+ const sentinel = join(oldSharedDirectory, "0.jsonl");+ mkdirSync(oldSharedDirectory, { recursive: true });+ writeFileSync(sentinel, "unrelated-worker");+ const daemon = new AgentDaemon(join(root, "worker.sock"), {+ defaultSessionConfig: { agentDir: root, cwd: root },+ createRuntime: async () => {+ throw new Error("unexpected runtime creation");+ },+ });+ const releases: Array<(accepted: boolean) => void> = [];+ const produced: string[] = [];+ const internals = daemon as unknown as {+ writeWorkerSnapshotBuffer(+ client: DaemonSocketClient,+ buffer: Buffer,+ message: DaemonOutbound,+ ): Promise<boolean>;+ streamWorkerSnapshot(+ client: DaemonSocketClient,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptChunkSource,+ ): Promise<void>;+ };+ internals.writeWorkerSnapshotBuffer = async (_client, _buffer, message) => {+ if (message.type !== "session_snapshot_chunk" || message.index !== 0) {+ return true;+ }+ return new Promise<boolean>((resolve) => releases.push(resolve));+ };+ const sockets = Array.from({ length: count }, (_, index) => socketClient(`worker-${index}`));+ const streams = sockets.map(({ client }, index) => {+ const encoded = createSnapshotTranscriptChunks({+ activeSessionId,+ snapshotId,+ messages: messages(String(index)),+ targetChunkBytes: 1,+ });+ const tracked: SnapshotTranscriptChunkSource = {+ async *[Symbol.asyncIterator]() {+ let chunkIndex = 0;+ for (const chunk of encoded) {+ produced.push(`${index}:${chunkIndex++}`);+ yield chunk;+ }+ },+ };+ return internals.streamWorkerSnapshot(client, streamedResult(2), tracked);+ });++ for (let attempt = 0; attempt < 10 && releases.length < count; attempt++) {+ await Promise.resolve();+ }+ expect(releases).toHaveLength(count);+ expect(produced).toEqual(Array.from({ length: count }, (_, index) => `${index}:0`));+ for (const release of releases) release(true);+ await Promise.all(streams);++ expect(produced).toEqual([+ ...Array.from({ length: count }, (_, index) => `${index}:0`),+ ...Array.from({ length: count }, (_, index) => `${index}:1`),+ ]);+ expect(readFileSync(sentinel, "utf8")).toBe("unrelated-worker");+ for (const { socket } of sockets) socket.destroy();+ },+ );++ it("terminates a pre-begin abort without interrupting a sibling stream", async () => {+ const root = tempDirectory();+ const daemon = new AgentDaemon(join(root, "worker.sock"), {+ defaultSessionConfig: { agentDir: root, cwd: root },+ createRuntime: async () => {+ throw new Error("unexpected runtime creation");+ },+ });+ const { client, socket } = socketClient("worker-pre-begin-abort");+ const siblingActiveSessionId = "active-4601-pre-begin-sibling";+ const siblingSnapshotId = "snapshot-4601-pre-begin-sibling";+ client.attachedActiveSessionIds.add(siblingActiveSessionId);+ const state = {+ activeSessionId,+ clients: new Set([client]),+ extensionUiRequests: new Map(),+ runtime: { metadata: { kind: "subagent" } },+ } as unknown as ActiveSessionState;+ const written: DaemonOutbound[] = [];+ const signal = markClientSnapshotStreaming(client, activeSessionId);+ const siblingSignal = markClientSnapshotStreaming(client, siblingActiveSessionId);+ const internals = daemon as unknown as {+ writeSerialized(client: DaemonSocketClient, buffer: string | Buffer, message?: DaemonOutbound): boolean;+ streamWorkerSnapshot(+ client: DaemonSocketClient,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptChunkSource,+ purpose: "attach",+ signal: AbortSignal,+ snapshotAlreadyMarked: boolean,+ ): Promise<void>;+ detachClientFromSession(client: DaemonSocketClient, state: ActiveSessionState): void;+ };+ internals.writeSerialized = (_client, _buffer, message) => {+ if (message) written.push(message);+ return true;+ };++ internals.detachClientFromSession(client, state);+ await Promise.all([+ internals.streamWorkerSnapshot(+ client,+ streamedResult(2),+ createSnapshotTranscriptChunks({ activeSessionId, snapshotId, messages: messages("aborted"), signal }),+ "attach",+ signal,+ true,+ ),+ internals.streamWorkerSnapshot(+ client,+ streamedResult(2, 1, siblingActiveSessionId, siblingSnapshotId),+ createSnapshotTranscriptChunks({+ activeSessionId: siblingActiveSessionId,+ snapshotId: siblingSnapshotId,+ messages: messages("sibling"),+ signal: siblingSignal,+ }),+ "attach",+ siblingSignal,+ true,+ ),+ ]);++ expect(+ written+ .filter((message) => "snapshotId" in message && message.activeSessionId === activeSessionId)+ .map((message) => message.type),+ ).toEqual(["session_snapshot_begin", "session_snapshot_failed"]);+ expect(written).toContainEqual(+ expect.objectContaining({+ type: "session_snapshot_end",+ activeSessionId: siblingActiveSessionId,+ snapshotId: siblingSnapshotId,+ }),+ );+ expect(client.attachedActiveSessionIds.has(siblingActiveSessionId)).toBe(true);+ expect(client.snapshotStreaming).toBe(false);+ expect(socket.destroyed).toBe(false);+ socket.destroy();+ });++ it("terminates an aborted worker snapshot without interrupting a sibling stream", async () => {+ const root = tempDirectory();+ const daemon = new AgentDaemon(join(root, "worker.sock"), {+ defaultSessionConfig: { agentDir: root, cwd: root },+ createRuntime: async () => {+ throw new Error("unexpected runtime creation");+ },+ });+ const { client, socket } = socketClient("worker-detach");+ const siblingActiveSessionId = "active-4601-sibling";+ const siblingSnapshotId = "snapshot-4601-sibling";+ client.attachedActiveSessionIds.add(siblingActiveSessionId);+ const state = {+ activeSessionId,+ clients: new Set([client]),+ extensionUiRequests: new Map(),+ runtime: { metadata: { kind: "subagent" } },+ } as unknown as ActiveSessionState;+ const written: DaemonOutbound[] = [];+ const produced: number[] = [];+ const signal = markClientSnapshotStreaming(client, activeSessionId);+ const siblingSignal = markClientSnapshotStreaming(client, siblingActiveSessionId);+ const encoded = createSnapshotTranscriptChunks({+ activeSessionId,+ snapshotId,+ messages: messages("detach"),+ targetChunkBytes: 1,+ signal,+ });+ const transcript: SnapshotTranscriptChunkSource = {+ *[Symbol.iterator]() {+ let index = 0;+ for (const chunk of encoded) {+ produced.push(index++);+ yield chunk;+ }+ },+ };+ const siblingTranscript = createSnapshotTranscriptChunks({+ activeSessionId: siblingActiveSessionId,+ snapshotId: siblingSnapshotId,+ messages: messages("sibling"),+ targetChunkBytes: 1,+ signal: siblingSignal,+ });+ const internals = daemon as unknown as {+ writeSerialized(client: DaemonSocketClient, buffer: string | Buffer, message?: DaemonOutbound): boolean;+ streamWorkerSnapshot(+ client: DaemonSocketClient,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptChunkSource,+ purpose: "attach",+ signal: AbortSignal,+ snapshotAlreadyMarked: boolean,+ ): Promise<void>;+ detachClientFromSession(client: DaemonSocketClient, state: ActiveSessionState): void;+ };+ internals.writeSerialized = (_client, _buffer, message) => {+ if (message) written.push(message);+ return message?.type !== "session_snapshot_chunk" || message.activeSessionId === siblingActiveSessionId;+ };++ const stream = internals.streamWorkerSnapshot(client, streamedResult(2), transcript, "attach", signal, true);+ const siblingStream = internals.streamWorkerSnapshot(+ client,+ streamedResult(2, 1, siblingActiveSessionId, siblingSnapshotId),+ siblingTranscript,+ "attach",+ siblingSignal,+ true,+ );+ for (let attempt = 0; attempt < 10 && socket.listenerCount("drain") === 0; attempt++) {+ await Promise.resolve();+ }+ expect(produced).toEqual([0]);+ expect(socket.listenerCount("drain")).toBe(1);++ internals.detachClientFromSession(client, state);+ await Promise.all([stream, siblingStream]);++ expect(produced).toEqual([0]);+ expect(+ written.some(+ (message) => message.type === "session_snapshot_end" && message.activeSessionId === activeSessionId,+ ),+ ).toBe(false);+ expect(written).toContainEqual({+ type: "session_snapshot_failed",+ activeSessionId,+ snapshotId,+ error: `Snapshot ${snapshotId} was aborted`,+ });+ expect(written).toContainEqual(+ expect.objectContaining({+ type: "session_snapshot_end",+ activeSessionId: siblingActiveSessionId,+ snapshotId: siblingSnapshotId,+ }),+ );+ expect(state.clients.has(client)).toBe(false);+ expect(client.attachedActiveSessionIds.has(activeSessionId)).toBe(false);+ expect(client.attachedActiveSessionIds.has(siblingActiveSessionId)).toBe(true);+ expect(client.snapshotStreaming).toBe(false);+ expect(client.snapshotActiveSessionIds?.size).toBe(0);+ expect(client.snapshotActiveSessionCounts?.size).toBe(0);+ expect(client.snapshotTransferAbortControllers?.size).toBe(0);+ expect(socket.listenerCount("drain")).toBe(0);+ expect(socket.listenerCount("close")).toBe(0);+ expect(socket.listenerCount("error")).toBe(0);+ expect(socket.destroyed).toBe(false);+ socket.destroy();+ });++ it("closes a permanently backpressured worker channel after an aborted snapshot", async () => {+ vi.useFakeTimers();+ try {+ const root = tempDirectory();+ const daemon = new AgentDaemon(join(root, "worker.sock"), {+ defaultSessionConfig: { agentDir: root, cwd: root },+ createRuntime: async () => {+ throw new Error("unexpected runtime creation");+ },+ });+ const { client, socket } = socketClient("worker-permanent-backpressure");+ const siblingActiveSessionId = "active-4601-backpressure-sibling";+ client.attachedActiveSessionIds.add(siblingActiveSessionId);+ socket.on("error", () => {});+ const state = {+ activeSessionId,+ clients: new Set([client]),+ extensionUiRequests: new Map(),+ runtime: { metadata: { kind: "subagent" } },+ } as unknown as ActiveSessionState;+ const signal = markClientSnapshotStreaming(client, activeSessionId);+ const internals = daemon as unknown as {+ writeSerialized(client: DaemonSocketClient, buffer: string | Buffer, message?: DaemonOutbound): boolean;+ streamWorkerSnapshot(+ client: DaemonSocketClient,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptChunkSource,+ purpose: "attach",+ signal: AbortSignal,+ snapshotAlreadyMarked: boolean,+ ): Promise<void>;+ detachClientFromSession(client: DaemonSocketClient, state: ActiveSessionState): void;+ };+ internals.writeSerialized = () => false;+ const stream = internals.streamWorkerSnapshot(+ client,+ streamedResult(2),+ createSnapshotTranscriptChunks({ activeSessionId, snapshotId, messages: messages("blocked"), signal }),+ "attach",+ signal,+ true,+ );+ for (let attempt = 0; attempt < 10 && socket.listenerCount("drain") === 0; attempt++) {+ await Promise.resolve();+ }++ internals.detachClientFromSession(client, state);+ for (let attempt = 0; attempt < 10 && vi.getTimerCount() === 0; attempt++) {+ await Promise.resolve();+ }+ expect(vi.getTimerCount()).toBe(1);+ await vi.advanceTimersByTimeAsync(1_000);+ await stream;++ expect(socket.destroyed).toBe(true);+ expect(client.attachedActiveSessionIds.has(siblingActiveSessionId)).toBe(true);+ expect(client.snapshotStreaming).toBe(false);+ expect(client.snapshotActiveSessionIds?.size).toBe(0);+ expect(client.snapshotTransferAbortControllers?.size).toBe(0);+ expect(socket.listenerCount("drain")).toBe(0);+ expect(socket.listenerCount("close")).toBe(0);+ } finally {+ vi.useRealTimers();+ }+ });++ it("keeps supervisor snapshot state active until overlapping same-session streams finish", async () => {+ const root = tempDirectory();+ const supervisor = new DaemonSupervisor(join(root, "supervisor.sock"), {+ defaultSessionConfig: { agentDir: root, cwd: root },+ descriptorDir: join(root, "state"),+ });+ const { client, socket } = socketClient("supervisor-overlap");+ const firstTranscript = new SnapshotTranscriptCache({+ activeSessionId,+ snapshotId: "snapshot-first",+ messages: messages("first"),+ cacheRoot: root,+ });+ const secondTranscript = new SnapshotTranscriptCache({+ activeSessionId,+ snapshotId: "snapshot-second",+ messages: messages("second"),+ cacheRoot: root,+ });+ const replacementTranscript = new SnapshotTranscriptCache({+ activeSessionId,+ snapshotId: "snapshot-replacement",+ messages: messages("replacement"),+ cacheRoot: root,+ });+ const supervisorWorker = {+ transcriptCaches: new Map([[activeSessionId, replacementTranscript]]),+ };+ const firstResult = {+ ...streamedResult(2),+ snapshotStream: { id: firstTranscript.snapshotId, messageCount: 2, targetChunkBytes: 1 },+ };+ const secondResult = {+ ...streamedResult(2),+ snapshotStream: { id: secondTranscript.snapshotId, messageCount: 2, targetChunkBytes: 1 },+ };+ const replacementResult = {+ ...streamedResult(2),+ snapshotStream: { id: replacementTranscript.snapshotId, messageCount: 2, targetChunkBytes: 1 },+ };+ const written: DaemonOutbound[] = [];+ let releaseSecondBegin!: (accepted: boolean) => void;+ const secondBegin = new Promise<boolean>((resolve) => {+ releaseSecondBegin = resolve;+ });+ const writeSnapshotBuffer = vi.fn((_client: DaemonSocketClient, buffer: Uint8Array) => {+ const message = JSON.parse(Buffer.from(buffer).toString("utf8")) as DaemonOutbound;+ written.push(message);+ if (message.type === "session_snapshot_begin" && message.snapshotId === secondTranscript.snapshotId) {+ return secondBegin;+ }+ return Promise.resolve(true);+ });+ const internals = supervisor as unknown as {+ clients: Set<DaemonSocketClient>;+ pendingReplacementSnapshots: WeakMap<DaemonSocketClient, Map<string, unknown>>;+ writeSnapshotBuffer: typeof writeSnapshotBuffer;+ streamSnapshot(+ client: DaemonSocketClient,+ worker: typeof supervisorWorker,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptCache,+ purpose: "attach" | "replacement" | "resync",+ ): Promise<void>;+ streamReplacementSnapshot(+ worker: typeof supervisorWorker,+ activeSessionId: string,+ result: DaemonAttachResult,+ transcript: SnapshotTranscriptCache,+ ): void;+ };+ internals.clients.add(client);+ internals.writeSnapshotBuffer = writeSnapshotBuffer;++ const firstStream = internals.streamSnapshot(client, supervisorWorker, firstResult, firstTranscript, "attach");+ const secondStream = internals.streamSnapshot(client, supervisorWorker, secondResult, secondTranscript, "attach");+ internals.streamReplacementSnapshot(supervisorWorker, activeSessionId, replacementResult, replacementTranscript);++ await firstStream;++ expect(client.snapshotActiveSessionCounts?.get(activeSessionId)).toBe(1);+ expect(client.snapshotActiveSessionIds?.has(activeSessionId)).toBe(true);+ expect(client.snapshotStreaming).toBe(true);+ expect(internals.pendingReplacementSnapshots.get(client)?.size).toBe(1);+ expect(+ written.some((message) => message.type === "session_snapshot_begin" && message.purpose === "replacement"),+ ).toBe(false);++ releaseSecondBegin(true);+ await secondStream;+ await new Promise<void>((resolve) => setImmediate(resolve));++ const secondEndIndex = written.findIndex(+ (message) => message.type === "session_snapshot_end" && message.snapshotId === secondTranscript.snapshotId,+ );+ const replacementBeginIndex = written.findIndex(+ (message) => message.type === "session_snapshot_begin" && message.purpose === "replacement",+ );+ expect(replacementBeginIndex).toBeGreaterThan(secondEndIndex);+ expect(client.snapshotActiveSessionCounts?.size).toBe(0);+ expect(client.snapshotActiveSessionIds?.size).toBe(0);+ expect(client.snapshotStreaming).toBe(false);+ expect(internals.pendingReplacementSnapshots.get(client)?.size ?? 0).toBe(0);+ socket.destroy();+ });++ it("leaves the supervisor transcript cache file-backed and reader-owned", () => {+ const cache = new SnapshotTranscriptCache({+ activeSessionId,+ snapshotId,+ messages: messages("supervisor"),+ cacheRoot: tempDirectory(),+ memoryCacheBytes: 1,+ targetChunkBytes: 1,+ });+ const release = cache.retain();+ const firstChunk = cache.readChunk(0);++ expect(cache.fileBacked).toBe(true);+ cache.dispose();+ expect(cache.readChunk(0)).toEqual(firstChunk);+ release();+ expect(() => cache.readChunk(0)).toThrow("Unknown snapshot transcript chunk");+ });+});diff --git a/packages/coding-agent/test/suite/regressions/4602-snapshot-transfer-idempotency.test.ts b/packages/coding-agent/test/suite/regressions/4602-snapshot-transfer-idempotency.test.tsindex 65697ce3a5..3143f85c1a 100644--- a/packages/coding-agent/test/suite/regressions/4602-snapshot-transfer-idempotency.test.ts+++ b/packages/coding-agent/test/suite/regressions/4602-snapshot-transfer-idempotency.test.ts@@ -12,9 +12,12 @@ import {} from "../../../src/modes/daemon/daemon-protocol.js";import type { SessionSummary } from "../../../src/modes/daemon/daemon-session-list.js";import { DaemonSupervisor } from "../../../src/modes/daemon/daemon-supervisor.js";-import type { DaemonWorkerFrameHeader } from "../../../src/modes/daemon/daemon-worker-protocol.js";+import {+ type DaemonWorkerFrameHeader,+ isDaemonWorkerFrameHeader,+} from "../../../src/modes/daemon/daemon-worker-protocol.js";import { SnapshotTranscriptCache } from "../../../src/modes/daemon/snapshot-transcript-cache.js";-import type { PrivateFrame } from "../../../src/modes/session-worker/private-framing.js";+import { type PrivateFrame, PrivateFrameDecoder } from "../../../src/modes/session-worker/private-framing.js";const activeSessionId = "active-4602";const snapshotId = "snapshot-4602";@@ -217,7 +220,7 @@ describe("ENG-4602 snapshot transfer containment", () => {expect(unhandled).not.toHaveBeenCalled();});- it("fails the cache and worker channel when streaming rejects after begin", async () => {+ it("fails one worker snapshot without dropping another session on the supervisor channel", async () => {const daemon = new AgentDaemon("/tmp/eng-4602-stream.sock", {defaultSessionConfig: { agentDir: "/tmp", cwd: "/tmp" },createRuntime: async () => {@@ -238,12 +241,13 @@ describe("ENG-4602 snapshot transfer containment", () => {const dispose = vi.spyOn(transcript, "dispose");const socket = new PassThrough();socket.on("error", () => {});- const destroy = vi.spyOn(socket, "destroy");+ const written: Buffer[] = [];+ socket.on("data", (chunk: Buffer) => written.push(Buffer.from(chunk)));const client = {id: "supervisor",socket: socket as unknown as Socket,transport: "private-framed",- attachedActiveSessionIds: new Set([activeSessionId]),+ attachedActiveSessionIds: new Set([activeSessionId, "active-4602-sibling"]),catchupActiveSessionIds: new Set<string>(),detachInput: () => {},supportsExtensionUi: false,@@ -258,11 +262,65 @@ describe("ENG-4602 snapshot transfer containment", () => {};await expect(internals.streamWorkerSnapshot(client, streamedResult([]), transcript)).rejects.toBe(streamError);- expect(socket.destroyed).toBe(true);+ const decoder = new PrivateFrameDecoder(isDaemonWorkerFrameHeader);+ const frames = decoder.push(Buffer.concat(written));+ expect(frames.map((entry) => (entry.header.kind === "outbound" ? entry.header.outboundType : undefined))).toEqual(+ ["session_snapshot_begin", "session_snapshot_failed"],+ );+ expect(JSON.parse(frames[1]!.payload.toString("utf8"))).toMatchObject({+ type: "session_snapshot_failed",+ activeSessionId,+ snapshotId,+ error: streamError.message,+ });+ expect(socket.destroyed).toBe(false);+ expect(client.attachedActiveSessionIds).toContain("active-4602-sibling");expect(transcript.complete).toBe(false);expect(client.snapshotStreaming).toBe(false);expect(markFailed.mock.invocationCallOrder[0]).toBeLessThan(dispose.mock.invocationCallOrder[0]!);- expect(dispose.mock.invocationCallOrder[0]).toBeLessThan(destroy.mock.invocationCallOrder[0]!);+ socket.destroy();+ });++ it("keeps a multi-session worker connected after a scoped snapshot failure frame", () => {+ const supervisor = new DaemonSupervisor("/tmp/eng-4602-supervisor-failure.sock", {+ defaultSessionConfig: { agentDir: "/tmp", cwd: "/tmp" },+ descriptorDir: "/tmp/eng-4602-supervisor-failure-state",+ });+ const { close, worker } = workerHarness();+ worker.summaries.set("active-4602-sibling", {+ ...summary(),+ id: "active-4602-sibling",+ activeSessionId: "active-4602-sibling",+ sessionId: "session-4602-sibling",+ });+ const transcript = new SnapshotTranscriptCache({+ activeSessionId,+ snapshotId,+ cacheRoot: "/tmp",+ });+ worker.snapshotCache.set(activeSessionId, streamedResult([]));+ worker.transcriptCaches.set(activeSessionId, transcript);+ worker.incomingTranscriptActiveSessionIds.add(activeSessionId);+ const internals = supervisor as unknown as {+ handleWorkerFrame(worker: WorkerHarness, frame: PrivateFrame<DaemonWorkerFrameHeader>): void;+ };++ internals.handleWorkerFrame(+ worker,+ frame({+ type: "session_snapshot_failed",+ activeSessionId,+ snapshotId,+ error: "snapshot encoder failed",+ }),+ );++ expect(close).not.toHaveBeenCalled();+ expect(worker.descriptor.lifecycle).toBe("ready");+ expect(worker.summaries.has("active-4602-sibling")).toBe(true);+ expect(worker.snapshotCache.has(activeSessionId)).toBe(false);+ expect(worker.transcriptCaches.has(activeSessionId)).toBe(false);+ expect(worker.incomingTranscriptActiveSessionIds.has(activeSessionId)).toBe(false);});it("quarantines a completed duplicate until exact replacement validation", () => {@@ -696,7 +754,7 @@ describe("ENG-4602 snapshot transfer containment", () => {expect(replaced.worker.transcriptCaches.get(activeSessionId)?.snapshotId).toBe("snapshot-4602-new");});- it("isolates a public snapshot read failure to its destination", async () => {+ it("fails one public snapshot without dropping another session on the shared client", async () => {const supervisor = new DaemonSupervisor("/tmp/eng-4602-supervisor-public.sock", {defaultSessionConfig: { agentDir: "/tmp", cwd: "/tmp" },descriptorDir: "/tmp/eng-4602-supervisor-public-state",@@ -714,8 +772,11 @@ describe("ENG-4602 snapshot transfer containment", () => {});const failedSocket = new PassThrough();failedSocket.on("error", () => {});+ const failedWrites: Buffer[] = [];+ failedSocket.on("data", (chunk: Buffer) => failedWrites.push(Buffer.from(chunk)));const siblingSocket = new PassThrough();const failedClient = socketClient("failed", failedSocket);+ failedClient.attachedActiveSessionIds.add("active-4602-sibling");const siblingClient = socketClient("sibling", siblingSocket);const { close, worker } = workerHarness();worker.snapshotCache.set(activeSessionId, result);@@ -734,10 +795,25 @@ describe("ENG-4602 snapshot transfer containment", () => {await expect(internals.streamSnapshot(failedClient, worker, result, transcript)).rejects.toBe(streamError);- expect(failedSocket.destroyed).toBe(true);+ const records = Buffer.concat(failedWrites)+ .toString("utf8")+ .trim()+ .split("\n")+ .map((line) => JSON.parse(line) as DaemonOutbound);+ expect(records.map((record) => record.type)).toEqual(["session_snapshot_begin", "session_snapshot_failed"]);+ expect(records[1]).toMatchObject({+ type: "session_snapshot_failed",+ activeSessionId,+ snapshotId,+ error: streamError.message,+ });+ expect(failedSocket.destroyed).toBe(false);+ expect(failedClient.attachedActiveSessionIds).toContain("active-4602-sibling");expect(siblingSocket.destroyed).toBe(false);expect(close).not.toHaveBeenCalled();expect(worker.snapshotCache.has(activeSessionId)).toBe(false);expect(worker.transcriptCaches.has(activeSessionId)).toBe(false);+ failedSocket.destroy();+ siblingSocket.destroy();});});