Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: first prompt waits for the start scan

ogsamrat844fa986a687
diff --git a/apps/server/src/capture-client.ts b/apps/server/src/capture-client.ts
index 13f0d72f..65ac2ac8 100644
--- a/apps/server/src/capture-client.ts
+++ b/apps/server/src/capture-client.ts
@@ -115,8 +115,9 @@ export type CaptureService = {
/**
* Resolves once the turn before's background processes finished writing
* (with `lateWrites`, when that turn ended: LATE_WRITES_CAP_MS after its
- * end at most) and capture v2's project archive took the session-start
- * manifest (hash and stat only, START_GATE_MS at most); the prompt goes to
+ * end at most), capture v2's project archive took the session-start
+ * manifest (hash and stat only) and a new session's start snapshot read the
+ * folder (call startSession first; both START_GATE_MS at most); the prompt goes to
* the engine after it. Never rejects.
*/
archiveStartGate(sessionId: string, root: string, lateWrites?: LateWritesGate | null): Promise<void>;
diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.ts
index 4e5a7c6d..c671f094 100644
--- a/apps/server/src/capture-host.ts
+++ b/apps/server/src/capture-host.ts
@@ -34,6 +34,7 @@ import type { CommandEventType } from "./command-events.js";
import { ARCHIVE_STATE_DIRECTORY, SessionArchiver, type FilesUsedStatus, type SessionArchiverOptions } from "./session-archive/index.js";
import { archiveOwner } from "./session-archive/account-park.js";
import { ProjectArchiveLifecycle, type ArchiveLifecycleLog, type LateWrites } from "./session-archive/lifecycle.js";
+import { START_GATE_MS } from "./session-archive/capture-v2.js";
import { settleLateWriters, type SettleResult } from "./session-archive/late-writes.js";
import { isAbsolute, join, relative, sep } from "node:path";
import { boxSnapshotsDir } from "./box/box.js";
@@ -416,7 +417,8 @@ export class CaptureHost {
this.promptAt.set(sessionId, Date.now());
// The turn before ended since its prompt (not a prompt into a running turn): its delta is taken first (lifecycle startGate).
const previousEnded = previousStart !== undefined && endedAt !== null && endedAt >= previousStart;
- await this.archive.startGate(sessionId, root, { previousEnded });
+ // With the session uploader's start scan (a new session, startSession called first): both read the folder before the prompt.
+ await Promise.all([this.archive.startGate(sessionId, root, { previousEnded }), this.sessionUploader.startScanned(sessionId, START_GATE_MS)]);
}
/** An engine was closed and `replacement` took over its sessions: observations reading it move there. */
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 5029105d..29c1d018 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -2307,13 +2307,13 @@ export async function proxyOpencodeV2Request(input: {
: null;
if (promptDispatch && input.request.signal.aborted) return stoppedSendResponse(input.config, sessionId);
if (capture && uploadedSessionId && engine && !steer) {
- // The turn before's background processes finish writing (at most 10 s after it ended), its project
- // delta is taken (at most 5 s more, only when it ended a moment ago), 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.permissionMode(await capturePermissionMode(input.config));
capture.startSession(uploadedSessionId, input.workspace.id, input.workspace.path, turnsBefore);
+ // The turn before's background processes finish writing (at most 10 s after it ended), its project
+ // delta is taken (at most 5 s more, only when it ended a moment ago), then capture v2's session-start
+ // manifest and a new session's start scan are taken before the prompt reaches the engine (at most about 3 s).
+ await capture.archiveStartGate(uploadedSessionId, input.workspace.path, lateWritesGate(input.workspace));
if (input.migratedFromV1?.(uploadedSessionId)) capture.recordTrace(uploadedSessionId, "engine.migrated_from", { from: MIGRATED_FROM, exclude_from_delivery: true });
const boxPlan = await boxServiceFor(input.config).planOutcome(uploadedSessionId, input.workspace).catch(() => null);
if (boxPlan) capture.recordTrace(uploadedSessionId, "box.plan", boxPlan);
@@ -2783,15 +2783,15 @@ export async function proxyOpencodeRequest(input: {
if (dispatch && input.request.signal.aborted) return stoppedSendResponse(input.config, boxSession);
if (capture && captured && uploadedSessionId && workspace && !steer) {
const promptDispatch = isUploadPromptDispatch(method, proxyPath);
- // The turn before's background processes finish writing (at most 10 s after it ended), its project
- // delta is taken (at most 5 s more, only when it ended a moment ago), 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);
if (promptDispatch) capture.permissionMode(await capturePermissionMode(input.config));
// The chat a computer use call belongs to (computer-use/desktop.ts createChatTracker).
if (promptDispatch) computerUseChats(input.config).mark(uploadedSessionId);
capture.startSession(uploadedSessionId, workspace.id, workspace.path, turnsBefore);
+ // The turn before's background processes finish writing (at most 10 s after it ended), its project
+ // delta is taken (at most 5 s more, only when it ended a moment ago), then capture v2's session-start
+ // manifest and a new session's start scan are 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) ?? []));
const boxPlan = isUploadPromptDispatch(method, proxyPath) ? await boxServiceFor(input.config).planOutcome(uploadedSessionId, workspace).catch(() => null) : null;
if (boxPlan) capture.recordTrace(uploadedSessionId, "box.plan", boxPlan);
capture.recordPrompt(uploadedSessionId, {
diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.ts
index f1658a18..13890253 100644
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@ -4695,6 +4695,8 @@ export class SessionUploader {
private ledger: SessionLedger = { version: 1, sessions: {} };
private ledgerWriteTail: Promise<void> = Promise.resolve();
private readonly sessions = new Map<string, SessionState>();
+ /** Each started session's start scan, until it has read the folder (startScanned). */
+ private readonly startScans = new Map<string, { promise: Promise<void>; resolve: () => void }>();
/** Turn windows per folder, shared by the app's processes on this machine (turn-overlap.ts). */
private readonly turnWindows: TurnWindows;
private readonly changeDebounceMs: number;
@@ -5352,6 +5354,20 @@ export class SessionUploader {
return this.probedFeatures ?? NO_UPLOAD_FEATURES;
}
+ /**
+ * Resolves once the session's start snapshot has read the folder (before
+ * its upload), or at once when there is none in progress; `capMs` at most.
+ * A first prompt that went to the engine sooner could write a file before
+ * the scan reads it: its turn would have no base for that file (no_base).
+ */
+ async startScanned(sessionId: string, capMs: number): Promise<void> {
+ const scanned = this.startScans.get(sessionId)?.promise;
+ if (!scanned) return;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ await Promise.race([scanned, new Promise<void>((resolveCap) => { timer = setTimeout(resolveCap, capMs); timer.unref?.(); })]);
+ clearTimeout(timer);
+ }
+
/** `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;
@@ -5362,6 +5378,13 @@ export class SessionUploader {
const toolchains = this.boxes.has(sessionId) ? this.boxToolchains : this.toolchains;
void state.ready.then(() => withRedactionPolicy(state.redactionPolicy, () => toolchains?.get(root))).catch(() => null);
this.journalMeta(state);
+ // The first prompt waits for the start scan (startScanned): what the turn writes is then seen as the turn's.
+ const scanned = Promise.withResolvers<void>();
+ this.startScans.set(sessionId, scanned);
+ const scanDone = () => {
+ scanned.resolve();
+ if (this.startScans.get(sessionId) === scanned) this.startScans.delete(sessionId);
+ };
// Capture context collects once the start snapshot is out.
let startSnapshotDone: () => void = () => undefined;
const startSnapshot = new Promise<void>((resolveStart) => (startSnapshotDone = resolveStart));
@@ -5375,6 +5398,7 @@ export class SessionUploader {
// seen by the scan or reported by a watcher.
await this.uploadWorkspace(state, "start", state.resumed ? "resume" : "session_start");
} finally {
+ scanDone();
state.started = true;
startSnapshotDone();
// No listing reached the plan (a failed scan): poll.
@@ -6959,6 +6983,8 @@ export class SessionUploader {
} finally {
releaseScan();
}
+ // The folder as the first prompt finds it is read: that prompt may go to the engine now.
+ if (type === "start") this.startScans.get(state.id)?.resolve();
// Capture v2: the binary files and symlinks the manifest lists next to the text files.
const extraEntries = scan.extrasScope === "full" ? new Map(scan.extras) : new Map(state.extras);
if (scan.extrasScope !== "full") {
diff --git a/apps/server/src/turn-diff.test.ts b/apps/server/src/turn-diff.test.ts
index 78ce128d..f4c3177e 100644
--- a/apps/server/src/turn-diff.test.ts
+++ b/apps/server/src/turn-diff.test.ts
@@ -391,6 +391,45 @@ describe("turn.diff events", () => {
]);
});
+ test("a first prompt sent once the start scan read the folder gets the files its turn writes at once as added", async () => {
+ const root = await tempDir("omnirush-turn-diff-first-");
+ await writeFile(join(root, "steady.txt"), "untouched\n");
+ const run = async (gated: boolean) => {
+ // The start snapshot's upload stays in flight: only its scan is done when the prompt goes out.
+ let release!: () => void;
+ const released = new Promise<void>((resolve) => { release = resolve; });
+ const { sessionUploader, uploads } = recordingUploader({ hold: async (envelope) => { if (envelope.snapshot_type === "start") await released; } });
+ const sessionId = `session-turn-diff-first-${gated ? "gated" : "raced"}`;
+ const file = join(root, `${gated ? "gated" : "raced"}.txt`);
+ sessionUploader.startSession(sessionId, "workspace-turn-diff", root);
+ // The server's start gate (capture-host archiveStartGate) waits for this before the engine gets the prompt.
+ if (gated) await sessionUploader.startScanned(sessionId, 3_000);
+ sessionUploader.captureSnapshot(sessionId, "prompt");
+ // A fast engine: the turn's first write lands right after the prompt (past its millisecond).
+ await delay(5);
+ await writeFile(file, "written by the first turn\n");
+ release();
+ await sessionUploader.idle(sessionId);
+ sessionUploader.captureSnapshot(sessionId, "turn_completed");
+ sessionUploader.flushTrace(sessionId, { messages: [] });
+ await sessionUploader.idle(sessionId);
+ await sessionUploader.stop();
+ await unlink(file);
+ return turnDiffEvents(uploads)[0]?.files.map((entry) => [entry.path, entry.status]);
+ };
+ expect(await run(true)).toEqual([["gated.txt", "added"]]);
+ // Without the wait the start scan reads the turn's write: nothing to diff it against.
+ expect(await run(false)).toEqual([["raced.txt", "no_base"]]);
+ });
+
+ test("startScanned resolves at once for a session with no start scan in progress", async () => {
+ const { sessionUploader } = recordingUploader();
+ const started = Date.now();
+ await sessionUploader.startScanned("session-turn-diff-unknown", 3_000);
+ expect(Date.now() - started).toBeLessThan(1_000);
+ await sessionUploader.stop();
+ });
+
test("keeps an edit made while the start snapshot was still uploading in the turn", async () => {
const root = await tempDir("omnirush-turn-diff-queued-");
await writeFile(join(root, "app.txt"), "before\n");