Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: serialize captured turns across prompt and stop boundaries

Task Builderabe44c8dc5e8
diff --cc apps/server/src/capture-protocol.ts
index 51e17d96,834aafff..e12a5616
--- a/apps/server/src/capture-protocol.ts
+++ b/apps/server/src/capture-protocol.ts
@@@ -23,10 -21,14 +23,14 @@@ export type CaptureCalls =
flushTrace: [sessionId: string, finalTrace?: unknown];
finishSession: [sessionId: string, finalTrace?: unknown];
recordPrompt: [sessionId: string, prompt: PromptRecord];
- captureSnapshot: [sessionId: string, trigger: "prompt" | "turn_completed", outcome?: "aborted"];
+ captureSnapshot: [sessionId: string, trigger: "prompt" | "turn_completed", outcome?: "aborted" | "error" | "incomplete"];
+ promptStartGate: [sessionId: string, workspaceId: string, root: string];
+ pauseTurn: [sessionId: string, reason?: string];
+ hasOpenTurn: [sessionId: string];
+ settleTurn: [sessionId: string];
recordWebVisit: [sessionId: string, visit: UploadWebVisit];
archiveSessionStarted: [sessionId: string, root: string, target: EngineTarget];
- archiveStartGate: [sessionId: string, root: string];
+ archiveStartGate: [sessionId: string, root: string, lateWrites?: LateWritesGate | null];
observeSession: [sessionId: string, target: EngineTarget];
engineReplaced: [closedBaseUrl: string, replacement: EngineReplacement];
sessionDeleted: [sessionId: string];
diff --cc apps/server/src/serialized-turn-gate.ts
index 00000000,4d626634..0cb03a2e
mode 000000,100644..100644
--- a/apps/server/src/serialized-turn-gate.ts
+++ b/apps/server/src/serialized-turn-gate.ts
@@@ -1,0 -1,173 +1,179 @@@
+ import { ApiError } from "./errors.js";
+ import { latestPromptId, readEngineSessionRoot, readSessionTreeQuiescence, stopEngineSession, turnOutcome, type EngineTarget } from "./session-upload-observer.js";
+
+ export type SerializedTurnCapture = {
+ promptStartGate(sessionId: string, workspaceId: string, root: string): Promise<boolean>;
+ pauseTurn(sessionId: string, reason?: string): Promise<void>;
+ hasOpenTurn(sessionId: string): Promise<boolean>;
+ settleTurn(sessionId: string): Promise<void>;
+ };
+ type Input = {
+ key: string;
+ sessionId: string;
+ workspaceId: string;
+ root: string;
+ target: EngineTarget;
+ capture: SerializedTurnCapture;
++ /** Settles late writes and configures the capture target while admission is reserved. */
++ prepare?: () => Promise<void>;
+ };
+ type State = {
+ reserved: boolean;
+ stopping: boolean;
+ blocked: boolean;
+ generation: number;
+ stopped: boolean;
+ pending: Map<string, string | null>;
+ };
+ export class SerializedTurnError extends ApiError {
+ constructor(message: string, status: 409 | 503 = 409) { super(status, status === 409 ? "turn_active" : "turn_unavailable", message); }
+ }
+ export type TurnTicket = {
+ check(): void;
+ finish(accepted: boolean | null): Promise<void>;
+ };
+ const RUNNING = "This turn is still running. Stop it before sending or editing a message.";
+ function bounded<T>(operation: () => Promise<T>, ms: number): Promise<T> {
+ let timer: ReturnType<typeof setTimeout>;
+ return Promise.race([
+ Promise.resolve().then(operation),
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(() => reject(new SerializedTurnError("The turn could not be verified in time. Retry Stop.", 503)), ms);
+ }),
+ ]).finally(() => clearTimeout(timer));
+ }
+
+ /** One reservation per root tree, retained across asynchronous dispatch and Stop. */
+ export class SerializedTurnGate {
+ private readonly states = new Map<string, State>();
+ private async resolve(input: Input, signal: AbortSignal): Promise<{ id: string; state: State }> {
+ const id = await readEngineSessionRoot(input.target, input.sessionId, signal).catch(() => {
+ throw new SerializedTurnError("Session ancestry is unavailable. The request was not sent.", 503);
+ });
+ const key = input.key + "\0" + id;
+ let state = this.states.get(key);
+ if (!state) {
+ state = { reserved: false, stopping: false, blocked: false, generation: 0, stopped: false, pending: new Map() };
+ this.states.set(key, state);
+ }
+ return { id, state };
+ }
+
+ async admit(input: Input, edit = false): Promise<TurnTicket> {
+ const signal = AbortSignal.timeout(60_000);
+ const { id, state } = await this.resolve(input, signal);
+ if (state.reserved || state.stopping || state.blocked) throw new SerializedTurnError(RUNNING);
+ state.reserved = true;
+ const generation = state.generation;
+ let baselineStarted = false;
+ const check = () => {
+ if (generation !== state.generation || state.stopping || state.blocked || signal.aborted) {
+ throw new SerializedTurnError("The request was stopped before it could start.");
+ }
+ };
+ try {
+ const tree = await readSessionTreeQuiescence(input.target, id, signal);
+ if (!tree.known) throw new SerializedTurnError("Agent or tool status is unavailable. The message was not sent.", 503);
+ if (tree.busy) throw new SerializedTurnError(RUNNING);
+ if (!state.stopped) {
+ for (const history of tree.histories.values()) {
+ if (["no_reply", "incomplete"].includes(turnOutcome(history) ?? "")) throw new SerializedTurnError(RUNNING);
+ }
+ }
+ for (const [pending, before] of state.pending) {
+ const history = tree.histories.get(pending);
+ if (!history) throw new SerializedTurnError("The prior request could not be verified.", 503);
+ const outcome = turnOutcome(history);
+ if (latestPromptId(history) === before || !["completed", "stopped", "error"].includes(outcome ?? "")) {
+ throw new SerializedTurnError(RUNNING);
+ }
+ }
+ await bounded(async () => {
+ for (const sessionId of tree.sessionIds) await input.capture.settleTurn(sessionId);
+ }, 60_000);
+ check();
++ if (!edit && input.prepare) {
++ await bounded(input.prepare, 60_000);
++ check();
++ }
+ if (edit) {
+ for (const sessionId of tree.sessionIds) {
+ if (await input.capture.hasOpenTurn(sessionId)) {
+ throw new SerializedTurnError("This turn is paused. Send a correction to continue it; its original prompt and files must remain recorded.");
+ }
+ }
+ } else {
+ baselineStarted = true;
+ if (!await bounded(() => input.capture.promptStartGate(input.sessionId, input.workspaceId, input.root), 60_000)) {
+ throw new SerializedTurnError("The original workspace snapshot is unavailable. The message was not sent.", 503);
+ }
+ }
+ check();
+ const before = latestPromptId(tree.histories.get(input.sessionId) ?? []);
+ let finished = false;
+ return {
+ check,
+ finish: async (accepted) => {
+ if (finished) return;
+ finished = true;
+ if (!edit && accepted === true && generation === state.generation) {
+ state.pending.set(input.sessionId, before); state.stopped = false;
+ }
+ if (accepted === null) state.blocked = true; // Delivery may have succeeded: require verified Stop.
+ try {
+ if (!edit && accepted !== true) await input.capture.pauseTurn(input.sessionId, "dispatch_failed");
+ } finally { state.reserved = false; }
+ },
+ };
+ } catch (error) {
+ if (baselineStarted) await bounded(() => input.capture.pauseTurn(input.sessionId, "dispatch_cancelled"), 20_000).catch(() => { state.blocked = true; });
+ state.reserved = false;
+ throw error instanceof ApiError ? error : new SerializedTurnError("Capture could not verify the turn. The message was not sent.", 503);
+ }
+ }
+
+ async stop(input: Input): Promise<void> {
+ const signal = AbortSignal.timeout(20_000);
+ const { id, state } = await this.resolve(input, signal);
+ if (state.stopping) throw new SerializedTurnError("Stop is still in progress.");
+ state.stopping = true;
+ state.blocked = true;
+ state.generation++;
+ const deadline = Date.now() + 20_000;
+ const wait = <T>(work: () => Promise<T>) => bounded(work, Math.max(1, deadline - Date.now()));
+ let firstError: unknown;
+ try {
+ // Pause capture before cancellation can emit idle/completed events.
+ try { await wait(() => input.capture.pauseTurn(id, "user_stop")); } catch (error) { firstError = error; }
+ try { await stopEngineSession(input.target, id, signal); } catch (error) { firstError ??= error; }
+ const stopped = new Set([id]);
+ let quiet = 0;
+ for (;;) {
+ const tree = await readSessionTreeQuiescence(input.target, id, signal);
+ if (!tree.known) throw new SerializedTurnError("Subagent activity is unavailable. Retry Stop.", 503);
+ for (const sessionId of tree.sessionIds) {
+ if (!stopped.has(sessionId) || tree.busy) {
+ try { await wait(() => input.capture.pauseTurn(sessionId, "user_stop")); } catch (error) { firstError ??= error; }
+ try {
+ await stopEngineSession(input.target, sessionId, signal);
+ stopped.add(sessionId);
+ } catch (error) { firstError ??= error; }
+ }
+ }
+ quiet = !tree.busy && !state.reserved && tree.sessionIds.every(sessionId => stopped.has(sessionId)) ? quiet + 1 : 0;
+ if (quiet >= 2) {
+ for (const sessionId of tree.sessionIds) await wait(() => input.capture.settleTurn(sessionId));
+ if (firstError) throw firstError;
+ state.pending.clear();
+ state.stopped = true;
+ state.blocked = false;
+ return;
+ }
+ if (signal.aborted || Date.now() >= deadline) throw new SerializedTurnError("The agent or a subagent did not stop in time. Retry Stop.", 503);
+ await new Promise(resolve => setTimeout(resolve, 25));
+ }
+ } catch (error) {
+ throw error instanceof ApiError ? error : new SerializedTurnError("Stop could not be verified. Retry Stop before continuing.", 503);
+ } finally { state.stopping = false; }
+ }
+ }
diff --cc apps/server/src/server.ts
index 3e8a8774,1648c272..c89a3c42
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@@ -200,8 -194,8 +200,9 @@@ import { CloudProviderSync, parseCloudP
import { createEngineV2Preview, type EngineV2Preview } from "./engine-v2-preview.js";
import { sessionUploaderEnabled } from "./session-uploader.js";
import { startCaptureService, type CaptureService } from "./capture-client.js";
-import { buildOpencodeProxyUrl, engineTarget, type EngineTarget } from "./session-upload-observer.js";
+import type { LateWritesGate } from "./capture-host.js";
+import { buildOpencodeProxyUrl, engineTarget, turnsBeforeCapture, type EngineTarget } from "./session-upload-observer.js";
+ import { SerializedTurnGate, type SerializedTurnCapture, type TurnTicket } from "./serialized-turn-gate.js";
import { OmniRushGatewayBroker } from "./omnirush-gateway-broker.js";
import { awaitOmniRushModelCatalogSettled, startOmniRushModelCatalogSync } from "./omnirush-model-catalog-sync.js";
import { OmniRushVoiceService, voiceProjectContext } from "./omnirush-voice.js";
@@@ -1803,25 -1783,43 +1826,54 @@@ export async function proxyOpencodeV2Re
const deletedUploadedSessionId = capture?.uploadEnabled && method === "DELETE" && sessionId && /^\/api\/session\/[^/]+$/.test(routePath)
? sessionId
: null;
- const engine = capture && uploadedSessionId ? engineTarget(input.connection.url, headers, target.search, "v2") : null;
- // The Linux sandbox (box/): a prompt of a workspace whose box is on starts the box, and takes the turn's checkpoint when it is captured.
- const boxInfo = promptDispatch && sessionId
- ? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine) })
- : null;
- if (capture && uploadedSessionId && engine) {
- // The turn before's background processes finish writing (at most 10 s after it ended), then
- // capture v2's session-start manifest is taken before the prompt reaches the engine (at most about 3 s).
- await capture.archiveStartGate(uploadedSessionId, input.workspace.path, lateWritesGate(input.workspace));
- capture.boxSession(uploadedSessionId, boxInfo);
- capture.startSession(uploadedSessionId, input.workspace.id, input.workspace.path, await turnsBeforeCapture(capture, uploadedSessionId, engine));
- capture.recordPrompt(uploadedSessionId, { method, path: routePath, body: requestBody, engine: "v2", root: input.workspace.path, attachments: true, dispatchedAt: Date.now() });
- capture.captureSnapshot(uploadedSessionId, "prompt");
- capture.archiveSessionStarted(uploadedSessionId, input.workspace.path, engine);
- }
- const response = await loopbackFetch(target.toString(), { method, headers, body, signal: input.recoverySignal });
- if (capture && uploadedSessionId && engine) {
- capture.recordTrace(uploadedSessionId, "engine.response", { method, path: routePath, status: response.status });
- if (response.ok) capture.observeSession(uploadedSessionId, engine);
+ const engine = capture && sessionId ? engineTarget(input.connection.url, headers, target.search, "v2") : null;
++ const prepareCapture = async () => {
++ const boxInfo = promptDispatch && sessionId
++ ? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine) })
++ : null;
++ if (capture && uploadedSessionId && engine) {
++ await capture.archiveStartGate(uploadedSessionId, input.workspace.path, lateWritesGate(input.workspace));
++ capture.boxSession(uploadedSessionId, boxInfo);
++ capture.startSession(uploadedSessionId, input.workspace.id, input.workspace.path, await turnsBeforeCapture(capture, uploadedSessionId, engine));
++ }
++ };
+ let ticket: TurnTicket | undefined;
+ const mutableSession = method !== "GET" && method !== "HEAD" && sessionId;
+ // Internal task recovery is already serialized by task-recovery.ts and resumes the persisted capture group.
+ if (supportsSerializedTurns(capture) && !input.recoverySignal && mutableSession && engine) {
+ const gate = serializedTurnGateFor(input.config);
+ const admissionInput = {
+ key: input.workspace.id, sessionId, workspaceId: input.workspace.id,
- root: input.workspace.path, target: engine, capture,
++ root: input.workspace.path, target: engine, capture, prepare: prepareCapture,
+ };
+ if (/^\/api\/session\/[^/]+\/(?:abort|interrupt)$/.test(routePath)) {
+ await gate.stop(admissionInput);
+ return jsonResponse({ data: true });
+ }
+ const edit = /\/(?:revert|unrevert|message(?:\/|$))/.test(routePath) && !promptDispatch;
+ if (promptDispatch || edit) ticket = await gate.admit(admissionInput, edit);
+ }
+ let response: Response;
+ try {
+ ticket?.check();
+ await prepareV2Instructions();
++ if (!ticket && promptDispatch) await prepareCapture();
+ if (capture && uploadedSessionId && engine) {
- await capture.archiveStartGate(uploadedSessionId, input.workspace.path);
+ ticket?.check();
++ if (!ticket) capture.captureSnapshot(uploadedSessionId, "prompt");
+ capture.recordPrompt(uploadedSessionId, { method, path: routePath, body: requestBody, engine: "v2", root: input.workspace.path, attachments: true, dispatchedAt: Date.now() });
+ capture.archiveSessionStarted(uploadedSessionId, input.workspace.path, engine);
+ }
+ ticket?.check();
+ response = await loopbackFetch(target.toString(), { method, headers, body, signal: input.recoverySignal });
+ if (capture && uploadedSessionId && engine) {
+ capture.recordTrace(uploadedSessionId, "engine.response", { method, path: routePath, status: response.status });
+ if (response.ok) capture.observeSession(uploadedSessionId, engine);
+ }
+ await ticket?.finish(response.ok);
+ } catch (error) {
+ await ticket?.finish(null);
+ throw error;
}
if (capture && deletedUploadedSessionId && response.ok) capture.sessionDeleted(deletedUploadedSessionId);
if (method === "GET" && /^\/api\/provider(?:\/|$)/.test(decodedPath) && response.ok) {
@@@ -2172,37 -2182,39 +2243,50 @@@ export async function proxyOpencodeRequ
const capture = workspace ? captureServicesByServer.get(input.config) : undefined;
const uploadedSessionId = uploadSessionId(proxyPath);
const deletedUploadedSessionId = uploadDeletedSessionId(method, proxyPath);
- // A chat the engine does not have (a stale tab, a deleted chat) is not
- // started: its prompt is refused, and it would only leave an empty start
- // and end snapshot behind.
+ const promptDispatch = isUploadPromptDispatch(method, proxyPath);
+ const captured = Boolean(capture?.uploadEnabled && uploadedSessionId && workspace && workspace.workspaceType !== "remote"
+ && (capture.hasSession(uploadedSessionId) || await engineHasSession(baseUrl, auth, directory, uploadedSessionId)));
- // The Linux sandbox (box/): a prompt of a workspace whose box is on starts the box, and takes the turn's checkpoint when it is captured.
- const boxInfo = workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath)
- ? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured })
- : null;
- if (capture && captured && uploadedSessionId && workspace) {
- const promptDispatch = isUploadPromptDispatch(method, proxyPath);
- // The turn before's background processes finish writing (at most 10 s after it ended), then
- // capture v2's session-start manifest is taken before the prompt reaches the engine (at most about 3 s).
- if (promptDispatch) await capture.archiveStartGate(uploadedSessionId, workspace.path, lateWritesGate(workspace, pool?.snapshot().generations.map((generation) => generation.pid) ?? []));
- if (promptDispatch) capture.boxSession(uploadedSessionId, boxInfo);
- capture.startSession(uploadedSessionId, workspace.id, workspace.path, promptDispatch ? await turnsBeforeCapture(capture, uploadedSessionId, engineTarget(baseUrl, headers, search)) : 0);
- capture.recordPrompt(uploadedSessionId, {
- method,
- path: normalizeOpencodeProxyPath(proxyPath),
- body,
- engine: "v1",
- root: workspace.path,
- attachments: promptDispatch,
- // Taken before the engine sees the prompt: the session's previous turn ends here.
- ...(promptDispatch ? { dispatchedAt: Date.now() } : {}),
- });
- if (promptDispatch) {
- capture.captureSnapshot(uploadedSessionId, "prompt");
- capture.archiveSessionStarted(uploadedSessionId, workspace.path, engineTarget(baseUrl, headers, search));
++ const prepareCapture = async () => {
++ const boxInfo = workspace && uploadedSessionId && promptDispatch
++ ? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured })
++ : null;
++ if (capture && captured && uploadedSessionId && workspace) {
++ if (promptDispatch) await capture.archiveStartGate(uploadedSessionId, workspace.path, lateWritesGate(workspace, pool?.snapshot().generations.map((generation) => generation.pid) ?? []));
++ if (promptDispatch) capture.boxSession(uploadedSessionId, boxInfo);
++ capture.startSession(uploadedSessionId, workspace.id, workspace.path, promptDispatch ? await turnsBeforeCapture(capture, uploadedSessionId, engineTarget(baseUrl, headers, search)) : 0);
++ }
++ };
+ let ticket: TurnTicket | undefined;
+ const normalizedRoute = normalizeOpencodeProxyPath(proxyPath);
+ const routeSession = normalizedRoute.match(/^\/session\/([^/]+)(?:\/|$)/)?.[1];
+ const mutableSession = routeSession ? decodeEngineRouteParam(routeSession) : null;
+ // Internal task recovery is already serialized by task-recovery.ts and resumes the persisted capture group.
+ if (supportsSerializedTurns(capture) && !input.recoverySignal && mutableSession && workspace && workspace.workspaceType !== "remote"
+ && method !== "GET" && method !== "HEAD") {
+ const gate = serializedTurnGateFor(input.config);
+ const admissionInput = {
+ key: workspace.id, sessionId: mutableSession, workspaceId: workspace.id,
- root: workspace.path, target: engineTarget(baseUrl, headers, search), capture,
++ root: workspace.path, target: engineTarget(baseUrl, headers, search), capture, prepare: prepareCapture,
+ };
+ if (/\/(?:abort|interrupt)$/.test(normalizedRoute)) {
+ await gate.stop(admissionInput);
+ return jsonResponse(true);
}
+ const edit = /\/(?:revert|unrevert|message(?:\/|$))/.test(normalizedRoute) && !promptDispatch;
+ if (promptDispatch || edit) ticket = await gate.admit(admissionInput, edit);
}
+ try {
- if (capture?.uploadEnabled && uploadedSessionId && workspace && workspace.workspaceType !== "remote"
- && (capture.hasSession(uploadedSessionId) || await engineHasSession(baseUrl, auth, directory, uploadedSessionId))) {
- if (promptDispatch) await capture.archiveStartGate(uploadedSessionId, workspace.path);
++ if (!ticket) await prepareCapture();
++ if (capture && captured && uploadedSessionId && workspace) {
+ ticket?.check();
- if (!promptDispatch) capture.startSession(uploadedSessionId, workspace.id, workspace.path);
++ if (promptDispatch && !ticket) capture.captureSnapshot(uploadedSessionId, "prompt");
+ capture.recordPrompt(uploadedSessionId, {
+ method, path: normalizedRoute, body, engine: "v1", root: workspace.path,
+ attachments: promptDispatch, ...(promptDispatch ? { dispatchedAt: Date.now() } : {}),
+ });
+ if (promptDispatch) capture.archiveSessionStarted(uploadedSessionId, workspace.path, engineTarget(baseUrl, headers, search));
+ }
+ } catch (error) { await ticket?.finish(false); throw error; }
if (pool && method === "GET" && isEngineEventPath(proxyPath)) {
// An open engine event stream means this workspace is visible somewhere in
// the UI; hold its instance so the idle reaper leaves it alone until the
diff --cc apps/server/src/session-upload-observer.ts
index 461bdae1,686bfddf..04d3b126
--- a/apps/server/src/session-upload-observer.ts
+++ b/apps/server/src/session-upload-observer.ts
@@@ -326,21 -305,22 +328,25 @@@ async function captureChildSessions(inp
known: Set<string>;
visited: Set<string>;
beforeRecord: (bytes: number) => void;
+ /** Each child's new messages, as recorded (turn-writers.ts). */
+ onChild?: (childId: string, depth: number, messages: readonly unknown[]) => void;
}): Promise<void> {
if (input.depth > MAX_UPLOAD_CHILD_SESSION_DEPTH) return;
- const children = await readEngineJson(input.fetchEngine, `/session/${encodeURIComponent(input.parentSessionId)}/children`, 1024 * 1024)
- .catch(nullOnErrorStatus);
+ const children = await readChildRecords({ fetchEngine: input.fetchEngine, parentSessionId: input.parentSessionId, v2: input.v2 })
+ .catch(error => { if (input.sessionUploader.hasOpenTurn?.(input.rootSessionId)) throw error; return nullOnErrorStatus(error); });
if (!Array.isArray(children)) return;
for (const child of children.slice(0, 200)) {
- const childId = isRecord(child) && typeof child.id === "string" && child.id ? child.id : null;
+ const childId = sessionRecordId(child);
if (!childId || childId === input.rootSessionId || input.visited.has(childId)) continue;
input.visited.add(childId);
- const history = await readEngineHistory(engineMessages(input.fetchEngine, `/session/${encodeURIComponent(childId)}/message`, true), input.checkpoints[childId])
+ const history = await readEngineHistory(engineMessages(
+ input.fetchEngine,
+ input.v2 ? `/api/session/${encodeURIComponent(childId)}/context` : `/session/${encodeURIComponent(childId)}/message`,
+ !input.v2,
+ ), input.checkpoints[childId])
.catch(nullOnErrorStatus);
const newMessages = history?.delta ?? [];
+ if (newMessages.length > 0) input.onChild?.(childId, input.depth, newMessages);
if (history && history.omitted > 0) {
input.sessionUploader.recordTrace(input.rootSessionId, "session.messages_omitted", { child_session_id: childId, count: history.omitted });
}
@@@ -1077,32 -1112,25 +1229,32 @@@ export function observeUploadedSession(
}
pending += bytes;
};
+ const children: WriterSessionMessages[] = [];
- // The v2 daemon has no subagent children route; only its root turn is captured.
- if (!v2) {
- try {
- await captureChildSessions({
- onChild: (childId, depth, childMessages) => children.push({ sessionId: childId, role: "child", depth, messages: childMessages }),
- sessionUploader,
- rootSessionId: sessionId,
- parentSessionId: sessionId,
- depth: 1,
- fetchEngine,
- checkpoints: await sessionUploader.childCheckpoints(sessionId),
- known: new Set(await sessionUploader.childSessionIds(sessionId)),
- visited: new Set([sessionId]),
- beforeRecord: room,
- });
- } catch (error) {
- if (stopped.aborted) throw error;
- sessionUploader.recordTrace(sessionId, "session.children_failed", { error: errorMessage(error) });
- }
+ let childCaptureFailed = false;
+ try {
+ await captureChildSessions({
++ onChild: (childId, depth, childMessages) => children.push({ sessionId: childId, role: "child", depth, messages: childMessages }),
+ sessionUploader,
+ rootSessionId: sessionId,
+ parentSessionId: sessionId,
+ depth: 1,
+ fetchEngine,
+ v2,
+ checkpoints: await sessionUploader.childCheckpoints(sessionId),
+ known: new Set(await sessionUploader.childSessionIds(sessionId)),
+ visited: new Set([sessionId]),
+ beforeRecord: room,
+ });
+ } catch (error) {
+ if (stopped.aborted) throw error;
+ childCaptureFailed = true;
+ sessionUploader.recordTrace(sessionId, "session.children_failed", { error: errorMessage(error) });
}
+ // Sub-agents that wrote files in the workspace: who wrote what, and the files the root and a sub-agent (or two) both wrote.
+ if (history && children.length > 0 && input.turnFiles) {
+ const writers = turnWriters(input.turnFiles.root, [{ sessionId, role: "root", depth: 0, messages: history.delta }, ...children]);
+ if (writers) sessionUploader.recordTrace(sessionId, "turn.writers", writers);
+ }
room(newest.bytes);
// How the turn ended, so a turn stopped with Esc reads as stopped, not as answered.
const outcome = history ? turnOutcome(history.outline) : null;
@@@ -1216,12 -1247,11 +1371,12 @@@
if (status !== "idle") {
observedBusy = true;
idleReads = 0;
+ turnState().idleAt = null;
// The v2 daemon has no children route: nothing to list there.
- if (!v2 && timing.now() - childIdsAt >= CHILD_IDS_INTERVAL_MS) {
+ if (timing.now() - childIdsAt >= CHILD_IDS_INTERVAL_MS) {
childIdsAt = timing.now();
try {
- sessionUploader.noteChildSessionIds(sessionId, await childSessionIdsBelow(fetchEngine, sessionId));
+ sessionUploader.noteChildSessionIds(sessionId, await childSessionIdsBelow(fetchEngine, sessionId, v2));
} catch (error) {
if (stopped.aborted) throw error;
}
@@@ -1231,8 -1261,15 +1386,16 @@@
if (idleReads === 0) {
idleSince = timing.now();
requestsWhenIdle = session.requests;
+ turnState().idleAt = idleSince;
}
+ if (serialized) {
+ const tree = await readSessionTreeQuiescence(session.target, sessionId, stopped);
+ if (!tree.known || tree.busy) {
+ idleReads = 0;
+ if (!tree.known) failures += 1;
+ continue;
+ }
+ }
idleReads += 1;
if (idleReads < 2 || reads < 4) continue;
// Never seen busy: the turn settled only once its answer ended (the prompt may not have started yet).
diff --cc apps/server/src/session-uploader.test.ts
index c2f234ca,0d6d931f..0f9be5b2
--- a/apps/server/src/session-uploader.test.ts
+++ b/apps/server/src/session-uploader.test.ts
@@@ -4225,38 -4225,84 +4225,118 @@@ describe("integrity confirmation", () =
expect(integrityFindings({ good_session: false, reasons: ["no_trace"], segments: [] })).toEqual({ verdict: "not_good", gaps: [], missing: true });
expect(integrityFindings("nope")).toBeNull();
});
+
+ test("keeps the Git baseline and defers only ordinary watched-edit metadata", async () => {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-git-review-"));
+ roots.push(root);
+ await writeFile(join(root, "note.txt"), "baseline\\n");
+ await execFileAsync("git", ["init", "-q", "-b", "main", root]);
+ await execFileAsync("git", ["-C", root, "add", "."]);
+ await execFileAsync("git", ["-C", root, "-c", "[email protected]", "-c", "user.name=t", "commit", "-qm", "baseline"]);
+ const uploads: Array<Record<string, unknown>> = [];
+ const uploader = new SessionUploader({
+ upload: async (_sessionId, compressed) => {
+ uploads.push(JSON.parse(zstdDecompressSync(compressed).toString("utf8")) as Record<string, unknown>);
+ return Response.json({ ok: true }, { status: 201 });
+ },
+ changeDebounceMs: 10,
+ fallbackScanMs: 60_000,
+ });
+ const sessionId = "session-git-review-1234";
+ uploader.startSession(sessionId, "workspace-git", root);
+ await uploader.idle(sessionId);
+ const start = uploads.find((item) => item.snapshot_type === "start")!;
+ const startGit = (start.workspace as { git: { commit: string; branch: string; dirty: string } }).git;
+ expect(startGit.branch).toBe("main");
+ expect(startGit.commit).toMatch(/^[0-9a-f]{40}$/);
+ await writeFile(join(root, "note.txt"), "edited\\n");
+ uploader.captureSnapshot(sessionId, "prompt");
+ await uploader.idle(sessionId);
+ const change = uploads.find((item) => item.snapshot_type === "change")!;
+ expect(change.workspace).toMatchObject({ git: null, git_deferred: "session_end" });
+ await uploader.stop();
+ const end = uploads.find((item) => item.snapshot_type === "end")!;
+ expect((end.workspace as { git: { commit: string } }).git.commit).toBe(startGit.commit);
+ });
+
});
+
+ describe("serialized logical turns", () => {
+ type ProofEvent = { type: string; data: {
+ group_id?: string; phase?: string; prompt_count?: number; baseline_snapshot?: { segment: number; sequence: number };
+ trigger?: string; files?: Array<{ path: string; status: string; diff?: string }>;
+ messages?: Array<{ info: { id: string; role: string } }>;
+ } };
+ for (const restart of [false, true]) test("preserves the original baseline after Stop" + (restart ? " and restart" : ""), async () => {
+ const root = await mkdtemp(join(tmpdir(), "serialized-gui-project-")); roots.push(root);
+ const stateDir = await mkdtemp(join(tmpdir(), "serialized-gui-state-")); roots.push(stateDir);
+ await writeFile(join(root, "a.txt"), "original\n");
+ const events: ProofEvent[] = [];
+ const options: ConstructorParameters<typeof SessionUploader>[0] = {
+ stateDir, toolchain: false, context: false, confirm: false,
+ upload: async (_id: string, compressed: Uint8Array) => {
+ const envelope: { snapshot_type: string; trace?: ProofEvent[]; files?: Array<{ path: string; content: string }> } =
+ JSON.parse(zstdDecompressSync(Buffer.from(compressed)).toString("utf8"));
+ if (envelope.snapshot_type === "trace") {
+ const trace: { events?: ProofEvent[] } = JSON.parse(envelope.files?.find(file => file.path === "__omnirush__/trace.json")?.content ?? "{}");
+ events.push(...(envelope.trace ?? trace.events ?? []));
+ }
+ return Response.json({}, { status: 201 });
+ },
+ };
+ const id = "ses_serialized_gui_fixture";
+ let uploader = new SessionUploader(options);
+ try {
+ uploader.startSession(id, "workspace", root);
+ expect(await uploader.beginTurn(id)).toBe(true);
+ await writeFile(join(root, "a.txt"), "partial\n");
+ await writeFile(join(root, "cli.py"), "child created this\n");
+ await uploader.pauseTurn(id);
+ uploader.captureSnapshot(id, "turn_completed", "aborted");
+ uploader.flushTrace(id, { messages: [{ info: { id: "u1", role: "user" }, parts: [{ type: "text", text: "original task" }] }] });
+ await uploader.idle(id);
+ if (restart) {
+ await uploader.stop();
+ uploader = new SessionUploader(options);
+ uploader.startSession(id, "workspace", root);
+ }
+ expect(await uploader.beginTurn(id)).toBe(true);
+ await writeFile(join(root, "a.txt"), "finished\n");
+ uploader.captureSnapshot(id, "turn_completed");
+ uploader.flushTrace(id, { messages: [{ info: { id: "u2", role: "user" }, parts: [{ type: "text", text: "correction" }] }] });
+ await uploader.idle(id);
+ const groups = events.filter(event => event.type === "collector.replay_group").map(event => event.data);
+ expect(new Set(groups.map(group => group.group_id)).size).toBe(1);
+ expect(groups.some(group => group.phase === "resumed" && group.prompt_count === 2)).toBe(true);
+ expect(groups.at(-1)?.phase).toBe("completed");
+ expect(groups[0]?.baseline_snapshot?.sequence).toBeGreaterThan(0);
+ expect(groups.at(-1)?.baseline_snapshot).toEqual(groups[0]?.baseline_snapshot);
+ const diff = events.filter(event => event.type === "turn.diff").at(-1)?.data.files;
+ expect(diff?.find(file => file.path === "a.txt")?.diff).toContain("-original");
+ expect(diff?.find(file => file.path === "a.txt")?.diff).toContain("+finished");
+ expect(diff?.find(file => file.path === "cli.py")?.status).toBe("added");
+ expect(events.filter(event => event.type === "collector.trigger" && event.data.trigger === "prompt").length).toBe(1);
+ expect(events.filter(event => event.type === "turn.completed").flatMap(event => event.data.messages ?? []).map(message => message.info.id)).toEqual(["u1", "u2"]);
+ expect(await uploader.beginTurn(id)).toBe(true);
+ uploader.flushTrace(id); await uploader.idle(id);
+ expect(new Set(events.filter(event => event.type === "collector.replay_group").map(event => event.data.group_id)).size).toBe(2);
+ } finally { await uploader.stop(); }
+ });
+
+ test("does not reuse an old accepted snapshot when the new one is refused", async () => {
+ const root = await mkdtemp(join(tmpdir(), "serialized-gui-refused-")); roots.push(root);
+ await writeFile(join(root, "a.txt"), "old");
+ let refuse = false;
+ const uploader = new SessionUploader({
+ toolchain: false, context: false,
+ upload: async () => Response.json({}, { status: refuse ? 400 : 201 }),
+ });
+ const id = "ses_serialized_gui_failure";
+ try {
+ uploader.startSession(id, "workspace", root); await uploader.idle(id);
+ await writeFile(join(root, "a.txt"), "changed before prompt");
+ refuse = true;
+ expect(await uploader.beginTurn(id)).toBe(false);
+ } finally { await uploader.stop(); }
+ });
+ });
diff --cc apps/server/src/session-uploader.ts
index a4534f79,6527245b..8b01daa3
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@@ -5048,23 -5017,20 +5143,23 @@@ export class SessionUploader
return this.traceSchemaCache?.features ?? NO_UPLOAD_FEATURES;
}
- startSession(sessionId: string, workspaceId: string, root: string): void {
+ /** `turnsBefore`: the turns a reopened chat already had, so capture context numbers its turns on from them. */
+ startSession(sessionId: string, workspaceId: string, root: string, options: { turnsBefore?: number } = {}): void {
if (!this.enabled || this.sessions.has(sessionId) || !/^[A-Za-z0-9._:-]{8,128}$/.test(sessionId)) return;
- void this.toolchains?.get(root).catch(() => null); // toolchain.ts: collected in the background
+ // toolchain.ts: collected in the background (a box chat's in its box).
+ void (this.boxes.has(sessionId) ? this.boxToolchains : this.toolchains)?.get(root).catch(() => null);
const state = this.newSessionState(sessionId, workspaceId, root, this.acquireCache(root));
- state.ready = this.prepareSession(state);
+ state.ready = this.prepareSession(state).then(() => this.restoreReplayGroup(state));
this.sessions.set(sessionId, state);
this.journalMeta(state);
// Capture context collects once the start snapshot is out.
let startSnapshotDone: () => void = () => undefined;
const startSnapshot = new Promise<void>((resolveStart) => (startSnapshotDone = resolveStart));
- this.context?.sessionStarted(sessionId, root, startSnapshot);
+ // A box chat: its own context (this machine's packages, services, processes and network are not the chat's).
+ this.contextOf(sessionId)?.sessionStarted(sessionId, root, startSnapshot, options.turnsBefore ?? 0);
this.enqueue(state, async () => {
- await state.ready;
try {
+ await state.ready;
// The watchers go up as soon as the start snapshot's listing is in,
// before any file is read: an edit made during the scan is either
// seen by the scan or reported by a watcher.
@@@ -5787,24 -5883,15 +6014,16 @@@
if (trigger === "prompt") {
state.relevance.clear();
state.turnWrittenPaths.clear();
- this.context?.turnStarted(sessionId, Promise.resolve().then(() => state.artifactBaseline));
+ // After the turn's artifact baseline (set just below) is listed.
+ this.contextOf(sessionId)?.turnStarted(sessionId, Promise.resolve().then(() => state.artifactBaseline));
+ state.artifactBaseline = artifactStats(state.root);
+ state.turnStartedAt = state.replayGroup?.startedAt ?? Date.now() + 1;
} else {
- this.context?.turnEnded(sessionId);
+ this.contextOf(sessionId)?.turnEnded(sessionId);
}
- // The artifact baseline must reflect the workspace as the turn begins, not
- // once the queued start snapshot has finished uploading.
- if (trigger === "prompt") state.artifactBaseline = artifactStats(state.root);
- // So must the turn's start: what the agent writes while an earlier upload
- // still holds the queue belongs to this turn. It is the first whole
- // millisecond after the prompt: Date.now() rounds down, and a file written
- // just before may carry a later fraction of the same millisecond.
- if (trigger === "prompt") state.turnStartedAt = Date.now() + 1;
const startedAt = state.turnStartedAt;
state.turnInProgress = trigger === "prompt";
- // A crash from here on leaves the turn open in the journal (or no longer).
this.journalMeta(state, true);
- // The milestone carries whatever edits were held back for another session's turn.
state.changesHeld = false;
if (state.changeTimer) {
clearTimeout(state.changeTimer);
@@@ -5974,8 -6080,18 +6212,18 @@@
this.recordTouchedPath(state, evidence.path, evidence.relevance);
}
// Capture context: the turn's last messages (its tool calls) arrive here, not as turn.messages.
- if (isRecord(finalTrace)) this.context?.turnMessages(sessionId, finalTrace.messages, true);
+ if (isRecord(finalTrace)) this.contextOf(sessionId)?.turnMessages(sessionId, finalTrace.messages, true);
- pendingTrace.push({ at: new Date().toISOString(), type: "turn.completed", data: finalTrace });
+ if (state.replayGroup && isRecord(finalTrace) && Array.isArray(finalTrace.messages)) {
+ const ids = new Set(state.replayGroup.messageIds ?? []);
+ for (const message of finalTrace.messages) {
+ if (!isRecord(message)) continue;
+ const info = isRecord(message.info) ? message.info : message;
+ if (info.role === "user" && typeof info.id === "string") ids.add(info.id);
+ }
+ state.replayGroup.messageIds = [...ids];
+ }
+ const data = state.replayGroup && isRecord(finalTrace) ? { ...finalTrace, replay_group_id: state.replayGroup.id } : finalTrace;
+ pendingTrace.push({ at: new Date().toISOString(), type: "turn.completed", data });
}
if (pendingTrace.length === 0) return;
this.enqueue(state, async () => {
@@@ -6374,8 -6479,9 +6626,9 @@@
// (a commit changes history without touching a file); the full git block
// with its status, log and diff is read only for a snapshot that goes out.
if (type === "change" && scan.changed !== null && scan.changed.size === 0 && scan.removed === 0 && !this.journalHasChanges(state)
- && (await gitHead(state.root)) === state.lastHead) {
+ && !gitChanged) {
accepted = true;
+ state.captureVerified = previous !== null;
return false;
}
// Its status and diff name the files a capped snapshot must keep.
@@@ -6517,15 -6624,15 +6770,19 @@@
// The baseline is the last *accepted* manifest (uploaded or spooled): the
// next change snapshot reports exactly what moved since this one.
accepted = true;
+ state.captureVerified = true;
+ state.lastWorkspaceCapture = { segment: state.segment, sequence: state.sequence };
state.manifest = manifest;
state.extras = extraEntries;
- state.turnManifest ??= turnBaseline(manifest, state.turnStartedAt, [previous]);
+ if (!state.replayGroup?.baselineUnavailable) {
+ state.turnManifest ??= turnBaseline(manifest, state.turnStartedAt, [previous]);
+ }
state.listing = { denied: scan.deniedCount, truncated: scan.manifestTruncated };
- state.lastHead = git?.commit ?? null;
+ if (git) {
+ state.lastHead = git.commit;
+ state.lastBranch = git.branch;
+ state.hasGitSample = true;
+ }
if (journal.length > 0) this.acknowledgeJournal(state, journal);
if (type === "change") state.lastChangeAt = Date.now();
return true;