A desktop coding agent with free access to frontier models.
TypeScript
fix: turn delta before the next prompt
ogsamrat3d1076323fd8
diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex 3559793b..2274ddea 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -368,7 +368,9 @@ export class CaptureHost {}}this.promptAt.set(sessionId, Date.now());- await this.archive.startGate(sessionId, root);+ // 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 });}/** 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.tsindex 77037b2f..279adf20 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -2070,8 +2070,9 @@ export async function proxyOpencodeV2Request(input: {? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine), turnsBefore }): 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).+ // 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));@@ -2502,8 +2503,9 @@ export async function proxyOpencodeRequest(input: {: null;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), then- // capture v2's session-start manifest is taken before the prompt reaches the engine (at most about 3 s).+ // 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));diff --git a/apps/server/src/session-archive/capture-v2.test.ts b/apps/server/src/session-archive/capture-v2.test.tsindex a1d12cf0..31fbe0d6 100644--- a/apps/server/src/session-archive/capture-v2.test.ts+++ b/apps/server/src/session-archive/capture-v2.test.ts@@ -180,6 +180,34 @@ describe("capture v2", () => {expect((await open(await archiver.captureFinal(sessionId, "app_quit"))).state!.state).toEqual({ kind: "final", turn: 1, reason: "app_quit" });});+ test("a turn's delta cut at the next prompt: files changed after it stay out (the chain's copy), the next turn's delta has them", async () => {+ const { archiver, open } = rig();+ const root = temp("omnirush-v2-cut-");+ write(join(root, "src", "app.py"), "print(1)\n");+ git(root, "init", "-q");+ git(root, "add", ".");+ git(root, "commit", "-qm", "init");+ const sessionId = "ses_v2_turn_cut_1";+ await archiver.startManifest(sessionId, root);+ await open(await archiver.captureBase(sessionId, root, 0));+ // Turn 1 writes its file, then the next prompt goes out, then turn 2 writes before turn 1's delta runs.+ write(join(root, "turn1.txt"), "one\n");+ await Bun.sleep(30);+ const nextPrompt = Date.now();+ await Bun.sleep(30);+ write(join(root, "src", "app.py"), "print(2)\n");+ write(join(root, "turn2.txt"), "two\n");+ const first = await open(await archiver.captureDelta(sessionId, root, 1, { before: nextPrompt }));+ expect([first.manifest.turn, first.state!.state]).toEqual([1, { kind: "after", turn: 1 }]);+ expect(first.entry("turn1.txt")).toBeDefined();+ expect(first.entry("turn2.txt")).toBeUndefined();+ expect(first.entry("src/app.py")).toBeUndefined();+ const second = await open(await archiver.captureDelta(sessionId, root, 2));+ expect(second.manifest.turn).toBe(2);+ expect(second.bytes("turn2.txt")!.toString("utf8")).toBe("two\n");+ expect(second.bytes("src/app.py")!.toString("utf8")).toBe("print(2)\n");+ });+test("#23: .git byte for byte; the archived config without credentials, the user's untouched", async () => {const { archiver, open } = rig();const root = temp("omnirush-v2-git-");diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex f3cf6a32..a2666c43 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -441,8 +441,9 @@ type CaptureOptions = {* Capture v2, pre_tool: when the turn's first tool call started. A file* changed at or after it is the tool's own write: the state keeps the* chain's copy (none for a new file), and the turn's delta carries it.+ * A turn's delta: when the next prompt went out (captureDelta).*/- before?: number;+ before?: number | (() => number | undefined);/** Capture v2, pre_tool: a file changed after this is a late write of the turn before (manifest reason "late_writes"). */lateAfter?: number;};@@ -743,7 +744,16 @@ export class SessionArchiver {* (archived, or counted at a turn end that changed nothing). Whatever it captures, the turn counts as the session's activity* (the app-start window).*/- captureDelta(sessionId: string, root: string, turn: number | null): Promise<CaptureResult> {+ /**+ * A completed turn's delta. `before` (capture v2): the next prompt went out at+ * that time, before this delta ran; a file changed at or after it is that+ * turn's write, so this delta keeps the chain's copy of it (none for a new+ * file) and the next turn's delta carries it. The cut goes by modification+ * time, like a pre_tool state's: a deletion, or a write that keeps an older+ * time (an archive extracted with its times), after the cut still lands+ * here. It only applies when the prompt's wait for this delta ran out.+ */+ captureDelta(sessionId: string, root: string, turn: number | null, options: { before?: number | (() => number | undefined) } = {}): Promise<CaptureResult> {return this.guard("delta", sessionId, turn ?? 0, async () => this.withSession(sessionId, async () => {const generation = this.generation;if (this.disabled) return { status: "skipped", reason: "disabled" };@@ -767,7 +777,7 @@ export class SessionArchiver {if (key === "unavailable") return { status: "skipped", reason: "unavailable" };// A touched-files session without a base: its base, with this turn, once a touched file is there.if (state.next_sequence === 0) return this.captureArchive(state, "base", next, key, generation);- return this.captureArchive(state, "delta", next, key, generation, { trigger: "turn" });+ return this.captureArchive(state, "delta", next, key, generation, { trigger: "turn", ...(options.before !== undefined ? { before: options.before } : {}) });}));}@@ -1646,7 +1656,8 @@ export class SessionArchiver {if (v2) mergeScans(scan, await this.mediaEntries(sessionId));mergeScans(scan, await this.sandboxEntries(sessionId));if (filesUsed) mergeStaged(scan, await this.filesUsed.entries(sessionId));- if (options.trigger === "pre_tool" && options.before !== undefined) scanAsOf(scan.entries, new Map(baseline.map((entry) => [entry.path, entry])), options.before);+ const asOf = typeof options.before === "function" ? options.before() : options.before;+ if ((options.trigger === "pre_tool" || options.trigger === "turn") && asOf !== undefined) scanAsOf(scan.entries, new Map(baseline.map((entry) => [entry.path, entry])), asOf);const change = touchedChange(baseline, scan);if (change.files.length === 0 && change.deleted.length === 0 && !forced) {if (cache.changed) await this.saveHashCache(rootKey, cache);@@ -1684,7 +1695,8 @@ export class SessionArchiver {enclosing = found.repo;mergeScans(scan, found);}- if (options.trigger === "pre_tool" && options.before !== undefined) scanAsOf(scan.entries, new Map(baseline.map((entry) => [entry.path, entry])), options.before);+ const asOf = typeof options.before === "function" ? options.before() : options.before;+ if ((options.trigger === "pre_tool" || options.trigger === "turn") && asOf !== undefined) scanAsOf(scan.entries, new Map(baseline.map((entry) => [entry.path, entry])), asOf);fullScan = scan.entries;files = scan.entries;excluded = scan.excluded;diff --git a/apps/server/src/session-archive/lifecycle.test.ts b/apps/server/src/session-archive/lifecycle.test.tsindex f8d75966..f8e00d07 100644--- a/apps/server/src/session-archive/lifecycle.test.ts+++ b/apps/server/src/session-archive/lifecycle.test.ts@@ -42,7 +42,7 @@ class FakeArchiver implements ProjectArchiver {return this.base(sessionId, turn);}- async captureDelta(sessionId: string, root: string, turn: number | null): Promise<CaptureResult> {+ async captureDelta(sessionId: string, root: string, turn: number | null, _options: { before?: number | (() => number | undefined) } = {}): Promise<CaptureResult> {this.calls.push(`delta ${sessionId} ${root} ${turn}`);return this.delta(sessionId, turn);}@@ -115,7 +115,7 @@ function engine(input: { parentID?: string; messages?: () => unknown; session?:return { reader, reads };}-function lifecycle(archiver: ProjectArchiver, options: { enabled?: boolean; accountMissing?: boolean; now?: () => number; concurrency?: number; finalIdleMs?: number; quitBudgetMs?: number } = {}) {+function lifecycle(archiver: ProjectArchiver, options: { enabled?: boolean; accountMissing?: boolean; now?: () => number; concurrency?: number; finalIdleMs?: number; quitBudgetMs?: number; turnDeltaWaitMs?: number } = {}) {const logs: Array<{ level: string; message: string; attributes?: Record<string, unknown> }> = [];const subject = new ProjectArchiveLifecycle({archiver,@@ -127,6 +127,7 @@ function lifecycle(archiver: ProjectArchiver, options: { enabled?: boolean; acco...(options.concurrency ? { concurrency: options.concurrency } : {}),...(options.finalIdleMs !== undefined ? { finalIdleMs: options.finalIdleMs } : {}),...(options.quitBudgetMs !== undefined ? { quitBudgetMs: options.quitBudgetMs } : {}),+ ...(options.turnDeltaWaitMs !== undefined ? { turnDeltaWaitMs: options.turnDeltaWaitMs } : {}),});return { subject, logs };}@@ -321,6 +322,180 @@ describe("ProjectArchiveLifecycle", () => {]);});+ test("a prompt right after a turn ended waits for the observer's settle and that turn's delta, then goes out", async () => {+ const archiver = new FakeArchiver();+ const { subject } = lifecycle(archiver);+ const root = await tempDir("root");+ const reads = engine({ messages: () => messages(0) });+ subject.sessionStarted({ sessionId: "ses_gate_0001", root, engine: reads.reader });+ await subject.settled();+ const order: string[] = [];+ archiver.delta = async (_sessionId, turn) => { order.push(`delta ${turn}`); return queued("delta", turn ?? 0); };+ // Turn 1 ended in the engine; the next prompt comes before the observer settled it.+ const gate = subject.startGate("ses_gate_0001", root, { previousEnded: true }).then(() => order.push("prompt"));+ await Bun.sleep(30);+ expect(order).toEqual([]);+ subject.turnCompleted("ses_gate_0001", messages(1), 1_000);+ await gate;+ expect(order).toEqual(["delta 1", "prompt"]);+ // The turn before is archived now: the next gate waits for nothing and reads nothing.+ const before = { ...reads.reads };+ const started = Date.now();+ await subject.startGate("ses_gate_0001", root, { previousEnded: true });+ expect(Date.now() - started).toBeLessThan(50);+ expect(reads.reads).toEqual(before);+ expect(archiver.captures()).toEqual([`base ses_gate_0001 ${root} 0`, `delta ses_gate_0001 ${root} 1`]);+ });++ test("a prompt into a running turn, or a first prompt, waits for nothing", async () => {+ const archiver = new FakeArchiver();+ const { subject } = lifecycle(archiver);+ const root = await tempDir("root");+ const started = Date.now();+ await subject.startGate("ses_gate_0002", root, { previousEnded: true });+ subject.sessionStarted({ sessionId: "ses_gate_0002", root, engine: engine({ messages: () => messages(0) }).reader });+ await subject.settled();+ await subject.startGate("ses_gate_0002", root, { previousEnded: false });+ await subject.startGate("ses_gate_0002", root);+ expect(Date.now() - started).toBeLessThan(200);+ });++ test("the wait ends after turnDeltaWaitMs; a delta that runs after the next prompt went out is cut at that prompt", async () => {+ const archiver = new FakeArchiver();+ let clock = 10_000;+ const { subject, logs } = lifecycle(archiver, { turnDeltaWaitMs: 100, now: () => clock });+ const root = await tempDir("root");+ const reads = engine({ messages: () => messages(0) });+ subject.sessionStarted({ sessionId: "ses_gate_0003", root, engine: reads.reader });+ await subject.settled();+ const cuts: Array<number | undefined> = [];+ const deltaGate = deferred<CaptureResult>();+ archiver.delta = () => deltaGate.promise;+ const capture = archiver.captureDelta.bind(archiver);+ archiver.captureDelta = async (sessionId, root2, turn, options = {}) => {+ const result = await capture(sessionId, root2, turn, options);+ // The archiver reads the cut when its scan ends: here, once the capture finished.+ cuts.push(typeof options.before === "function" ? options.before() : options.before);+ return result;+ };+ // Turn 1 went idle at 10_500 and settled; its delta is slow. The next prompt waits 100 ms, then goes out at 11_000.+ clock = 10_600;+ subject.turnCompleted("ses_gate_0003", messages(1), 10_500);+ const started = Date.now();+ await subject.startGate("ses_gate_0003", root, { previousEnded: true });+ expect(Date.now() - started).toBeLessThan(1_000);+ expect(logs.some((line) => line.message.includes("cut at the prompt"))).toBe(true);+ clock = 11_000;+ subject.sessionStarted({ sessionId: "ses_gate_0003", root, engine: reads.reader });+ deltaGate.resolve(queued("delta", 1));+ await subject.settled();+ // The delta read its cut when it ran: the prompt at 11_000, the first one after the turn went idle.+ expect(cuts).toEqual([11_000]);+ });++ test("a turn the next prompt cut before the observer saw it end: the delta is cut at the first prompt after the turn's own", async () => {+ const archiver = new FakeArchiver();+ let clock = 1_000;+ const { subject } = lifecycle(archiver, { now: () => clock });+ const root = await tempDir("root");+ const turn1 = [+ { info: { id: "user_1", role: "user", time: { created: 1_010 } }, parts: [] },+ { info: { id: "assistant_1", role: "assistant", time: { created: 1_020, completed: 1_500 }, finish: "stop" }, parts: [] },+ ];+ const reads = engine({ messages: () => [] });+ subject.sessionStarted({ sessionId: "ses_gate_0005", root, engine: reads.reader });+ await subject.settled();+ const cuts: Array<number | undefined> = [];+ const deltaGate = deferred<CaptureResult>();+ archiver.delta = () => deltaGate.promise;+ const capture = archiver.captureDelta.bind(archiver);+ archiver.captureDelta = async (sessionId, root2, turn, options = {}) => {+ const result = await capture(sessionId, root2, turn, options);+ cuts.push(typeof options.before === "function" ? options.before() : options.before);+ return result;+ };+ // No idle time (the observer saw the turn cut by the next prompt); the next prompt went out at 1_600.+ clock = 1_550;+ subject.turnCompleted("ses_gate_0005", turn1, null);+ clock = 1_600;+ subject.sessionStarted({ sessionId: "ses_gate_0005", root, engine: reads.reader });+ deltaGate.resolve(queued("delta", 1));+ await subject.settled();+ expect(cuts).toEqual([1_600]);+ });++ test("the cut skips a prompt sent while the turn still answered, and falls back to the idle time for a turn opened before this run", async () => {+ const cutFor = async (turn: Message[], promptsAt: number[], idleAt: number | null) => {+ const archiver = new FakeArchiver();+ let clock = promptsAt[0]!;+ const { subject } = lifecycle(archiver, { now: () => clock });+ const root = await tempDir("root");+ const reads = engine({ messages: () => [] });+ subject.sessionStarted({ sessionId: "ses_gate_0006", root, engine: reads.reader });+ await subject.settled();+ const cuts: Array<number | undefined> = [];+ const deltaGate = deferred<CaptureResult>();+ archiver.delta = () => deltaGate.promise;+ const capture = archiver.captureDelta.bind(archiver);+ archiver.captureDelta = async (sessionId, root2, number, options = {}) => {+ const result = await capture(sessionId, root2, number, options);+ cuts.push(typeof options.before === "function" ? options.before() : options.before);+ return result;+ };+ subject.turnCompleted("ses_gate_0006", turn, idleAt);+ for (const at of promptsAt.slice(1)) {+ clock = at;+ subject.sessionStarted({ sessionId: "ses_gate_0006", root, engine: reads.reader });+ }+ deltaGate.resolve(queued("delta", 1));+ await subject.settled();+ return cuts[0];+ };+ const steered = [+ { info: { id: "user_1", role: "user", time: { created: 1_010 } }, parts: [] },+ { info: { id: "assistant_1", role: "assistant", time: { created: 1_020, completed: 1_300 }, finish: "tool-calls" }, parts: [] },+ // Sent at 1_200 while the turn still answered: a steer, part of turn 1.+ { info: { id: "user_2", role: "user", time: { created: 1_205 } }, parts: [] },+ { info: { id: "assistant_2", role: "assistant", time: { created: 1_310, completed: 1_500 }, finish: "stop" }, parts: [] },+ ];+ // Prompts went out at 1_000 (turn 1), 1_200 (the steer) and 1_600 (the next turn): the cut is 1_600.+ expect(await cutFor(steered, [1_000, 1_200, 1_600], null)).toBe(1_600);+ // A turn opened before this run (its prompt is older than every prompt time here): the idle time decides.+ const older = [+ { info: { id: "user_1", role: "user", time: { created: 500 } }, parts: [] },+ { info: { id: "assistant_1", role: "assistant", time: { created: 510, completed: 900 }, finish: "stop" }, parts: [] },+ ];+ expect(await cutFor(older, [1_000, 2_000], 1_500)).toBe(2_000);+ expect(await cutFor(older, [1_000, 2_000], null)).toBeUndefined();+ });++ test("a delta that runs before the next prompt has no cut; an aborted prompt still gets its final archive", async () => {+ const archiver = new FakeArchiver();+ const { subject } = lifecycle(archiver);+ const root = await tempDir("root");+ const reads = engine({ messages: () => messages(1) });+ subject.sessionStarted({ sessionId: "ses_gate_0004", root, engine: reads.reader });+ await subject.settled();+ const cuts: Array<number | undefined> = [];+ const capture = archiver.captureDelta.bind(archiver);+ archiver.captureDelta = async (sessionId, root2, turn, options = {}) => {+ const result = await capture(sessionId, root2, turn, options);+ // The archiver reads the cut when its scan ends: here, once the capture finished.+ cuts.push(typeof options.before === "function" ? options.before() : options.before);+ return result;+ };+ subject.turnCompleted("ses_gate_0004", messages(1), Date.now());+ await subject.settled();+ await subject.startGate("ses_gate_0004", root, { previousEnded: true });+ subject.sessionStarted({ sessionId: "ses_gate_0004", root, engine: reads.reader });+ // The prompt is stopped before any answer: the count did not move, so the archiver answers stale_turn and a final follows.+ archiver.delta = async () => skipped("stale_turn");+ subject.turnCompleted("ses_gate_0004", messages(1), Date.now());+ await subject.settled();+ expect(cuts).toEqual([undefined, undefined]);+ expect(archiver.finals()).toEqual(["final ses_gate_0004 turn_incomplete"]);+ });+test("after a restart, a resumed session continues from the engine's turn count", async () => {const archiver = new FakeArchiver();const root = await tempDir("root");diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex a4d482af..c2384420 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -16,7 +16,7 @@ import { realpath } from "node:fs/promises";import { attachmentsFromMessages } from "./attachments.js";import type { StartCapture } from "./capture-v2.js";-import { completedTurnCount, promptTurnAt } from "./turn-prompts.js";+import { completedTurnCount, isPromptMessage, promptTurnAt, steerPrompts } from "./turn-prompts.js";import type { CaptureResult, DrainResult, FilesUsedStatus, FinalReason, SessionArchiver } from "./index.js";import type { UsedToolCall } from "./files-used.js";@@ -60,6 +60,8 @@ export type ProjectArchiveLifecycleOptions = {finalIdleMs?: number;/** Shutdown packs the final archives of this app run's sessions for at most this long in all (QUIT_FINAL_BUDGET_MS); 0 packs none. */quitBudgetMs?: number;+ /** A prompt waits at most this long for the turn before's delta (TURN_DELTA_WAIT_MS). */+ turnDeltaWaitMs?: number;/** Tests. */now?: () => number;/** A pre_tool state took files written after the turn before ended (the session uploader records them). */@@ -104,6 +106,13 @@ export const FINAL_IDLE_MS = 10 * 60_000;* checked at the next app start.*/export const QUIT_FINAL_BUDGET_MS = 5_000;+/**+ * How long a prompt waits at most for the turn before (the observer's settle,+ * then that turn's delta) when that turn ended a moment ago (startGate).+ */+export const TURN_DELTA_WAIT_MS = 5_000;+/** The prompt times kept per session (promptTimes). */+const PROMPT_TIMES_KEPT = 8;/** OMNIRUSH_ARCHIVE_ENABLED: on unless set to 0, false, no or off. */export function projectArchiveEnabled(env: NodeJS.ProcessEnv = process.env): boolean {@@ -172,6 +181,12 @@ type SessionRecord = {previousPromptAt?: number;/** When a turn of the session last settled (turnCompleted / turnIncomplete). */endedAt?: number;+ /** When this run's latest prompts went out (the newest last; a turn delta's cut is the first one after its turn ended). */+ promptTimes?: number[];+ /** The latest turn delta queued, until it ran (a prompt's gate waits for it). */+ pendingDelta?: Promise<void> | null;+ /** Prompt gates waiting for the observer to settle the turn before. */+ settleWaiters?: Array<() => void>;};/** A pre_tool state that took late writes of the turn before (lifecycle onLateWrites). */@@ -198,6 +213,7 @@ export class ProjectArchiveLifecycle {private readonly baseMaxDeferMs: number;private readonly finalIdleMs: number;private readonly quitBudgetMs: number;+ private readonly turnDeltaWaitMs: number;private readonly now: () => number;private readonly onLateWrites: ((late: LateWrites) => void) | undefined;private readonly onProjectArchive: ((sessionId: string, outcome: ProjectArchiveOutcome) => void) | undefined;@@ -220,6 +236,8 @@ export class ProjectArchiveLifecycle {private readonly waiting: Array<() => void> = [];/** Final archives wait for a slot behind every other step. */private readonly waitingLow: Array<() => void> = [];+ /** A turn delta a prompt may be waiting for (startGate) goes ahead of other sessions' steps. */+ private readonly waitingHigh: Array<() => void> = [];/** When the last prompt was dispatched on any session: bases wait for a quiet period after it. */private lastPromptAt = 0;/** Bases waiting for the quiet period; stop() and signOut() wake them. */@@ -236,6 +254,7 @@ export class ProjectArchiveLifecycle {this.baseMaxDeferMs = Math.max(this.baseIdleMs, options.baseMaxDeferMs ?? this.baseIdleMs * 5);this.finalIdleMs = Math.max(0, options.finalIdleMs ?? FINAL_IDLE_MS);this.quitBudgetMs = Math.max(0, options.quitBudgetMs ?? QUIT_FINAL_BUDGET_MS);+ this.turnDeltaWaitMs = Math.max(0, options.turnDeltaWaitMs ?? TURN_DELTA_WAIT_MS);this.now = options.now ?? Date.now;this.onLateWrites = options.onLateWrites;this.onProjectArchive = options.onProjectArchive;@@ -305,11 +324,12 @@ export class ProjectArchiveLifecycle {known.awaitingTool = true;known.previousPromptAt = known.promptedAt;known.promptedAt = promptedAt;+ known.promptTimes = [...(known.promptTimes ?? []), promptedAt].slice(-PROMPT_TIMES_KEPT);}// Resolved, or its start step is still queued: nothing to add.if (known && (!known.start || known.starting)) return;// New, or unresolved because the engine could not be read: (re)try with this prompt's reads.- const record: SessionRecord = known ?? { root: null, start: null, starting: false, engine: input.engine, activeAt: promptedAt, idleTimer: null, idleFinal: null, awaitingTool: true, promptedAt };+ const record: SessionRecord = known ?? { root: null, start: null, starting: false, engine: input.engine, activeAt: promptedAt, idleTimer: null, idleFinal: null, awaitingTool: true, promptedAt, promptTimes: [promptedAt] };record.start = { root: input.root, engine: input.engine };record.starting = true;this.sessions.set(input.sessionId, record);@@ -330,7 +350,8 @@ export class ProjectArchiveLifecycle {* start manifest (hash and stat only, at most START_GATE_MS); any later* prompt resolves at once. Never rejects.*/- async startGate(sessionId: string, root: string): Promise<StartCapture | "skipped"> {+ async startGate(sessionId: string, root: string, options: { previousEnded?: boolean } = {}): Promise<StartCapture | "skipped"> {+ if (options.previousEnded) await this.previousTurnArchived(sessionId);if (!this.active || this.consentOff || !this.archiver.startManifest) return "skipped";const known = this.sessions.get(sessionId);if (known && !known.start) return "skipped";@@ -419,17 +440,31 @@ export class ProjectArchiveLifecycle {* (after an app restart the engine can take a minute to answer) is* resolved here first, so the turn is not lost.*/- turnCompleted(sessionId: string, messages: unknown): void {+ turnCompleted(sessionId: string, messages: unknown, idleAt: number | null = null): void {const record = this.sessions.get(sessionId);if (!this.active || this.consentOff || !record) return;const turns = completedTurnCount(messages);if (turns !== null) record.turns = turns;record.endedAt = this.now();- this.schedule(sessionId, "delta", async () => {+ // The next prompt, when it went out before this delta's scan ended, cuts the delta there (captureDelta+ // `before`, read when the scan ends): the first prompt after the one that opened this turn (its message's+ // creation time in the engine), else after the engine went idle.+ const span = turns !== null ? turnSpan(messages, turns) : null;+ const cut = () => {+ const times = record.promptTimes ?? [];+ // The engine's times only count when this run sent that turn's prompt before it (the same clock). A prompt+ // sent while the turn still answered (a steer the classifier let through) is not the next one.+ const after = span !== null && times.some((at) => at <= span.opened) ? Math.max(span.opened, span.answered ?? span.opened) : idleAt;+ return after === null ? undefined : times.find((at) => at > after);+ };+ const delta = this.schedule(sessionId, "delta", async () => {if (this.sessions.get(sessionId) !== record) return;if (record.start && !(await this.resolve(sessionId, record, turns))) return;- await this.captureTurn(sessionId, record, turns);- });+ await this.captureTurn(sessionId, record, turns, false, cut);+ }, undefined, false, true);+ record.pendingDelta = delta;+ void delta.then(() => { if (record.pendingDelta === delta) record.pendingDelta = null; });+ this.turnSettled(record);this.armIdleFinal(sessionId, record);}@@ -522,6 +557,7 @@ export class ProjectArchiveLifecycle {const record = this.sessions.get(sessionId);if (!this.active || this.consentOff || !record) return;record.endedAt = this.now();+ this.turnSettled(record);this.schedule(sessionId, "turn", async () => {if (this.sessions.get(sessionId) !== record) return;if (record.start && !(await this.resolve(sessionId, record, null))) return;@@ -532,6 +568,39 @@ export class ProjectArchiveLifecycle {this.armIdleFinal(sessionId, record);}+ /**+ * A prompt of the session is about to go out right after the turn before+ * ended (startGate, `previousEnded`): that turn's delta is taken first, so it+ * holds the folder as the turn left it. The prompt waits for the observer+ * to settle that turn (it settles a moment after the engine goes idle, and+ * queues the turn's attachments, files used and delta in order), then for+ * that delta, at most turnDeltaWaitMs in all. A prompt that goes out+ * before the delta ran (the wait ended) still never gives it a later write:+ * the delta is cut at the prompt (turnCompleted). Sessions whose turn+ * before was archived wait for nothing and read nothing. Never rejects.+ */+ private async previousTurnArchived(sessionId: string): Promise<void> {+ const record = this.sessions.get(sessionId);+ // A base still queued (it may wait for a quiet period) would hold the prompt: the cut covers that turn instead.+ if (!this.active || this.consentOff || !record || !record.root || record.start || record.starting) return;+ const settled = record.endedAt !== undefined && record.promptedAt !== undefined && record.endedAt >= record.promptedAt;+ if (settled && !record.pendingDelta) return;+ const startedAt = Date.now();+ const gate = async () => {+ if (!settled) await new Promise<void>((resolveSettle) => (record.settleWaiters ??= []).push(resolveSettle));+ await record.pendingDelta;+ };+ const waited = await withDeadline(gate(), this.turnDeltaWaitMs).then(() => true, () => false);+ if (!waited) this.log("info", "OmniRush project archive: a prompt went out before the turn before's delta; the delta is cut at the prompt", { sessionId, waitedMs: Date.now() - startedAt });+ }++ /** The observer settled a turn of the session (turnCompleted / turnIncomplete): prompt gates waiting for it go on. */+ private turnSettled(record: SessionRecord): void {+ const waiters = record.settleWaiters ?? [];+ record.settleWaiters = [];+ for (const waiter of waiters) waiter();+ }+/*** The session was deleted: a last final archive of its folder (when it* has a base and the folder is still there), then it is forgotten, with@@ -654,11 +723,11 @@ export class ProjectArchiveLifecycle {* was reverted) gets a final archive instead, as does a turn whose count* is unknown when `finalWithoutCount`.*/- private async captureTurn(sessionId: string, record: SessionRecord, turns: number | null, finalWithoutCount = false): Promise<void> {+ private async captureTurn(sessionId: string, record: SessionRecord, turns: number | null, finalWithoutCount = false, before?: () => number | undefined): Promise<void> {if (!record.root || this.sessions.get(sessionId) !== record || this.consentOff) return;let result: CaptureResult | null = null;if (!finalWithoutCount) {- result = await this.archiver.captureDelta(sessionId, record.root, turns);+ result = await this.archiver.captureDelta(sessionId, record.root, turns, before !== undefined ? { before } : {});if (result.status === "skipped" && result.reason === "stopped") record.root = null;this.settle(result);}@@ -719,6 +788,7 @@ export class ProjectArchiveLifecycle {private release(record: SessionRecord): void {this.cancelIdleFinal(record);this.cancelRetry(record);+ this.turnSettled(record);}/**@@ -819,7 +889,8 @@ export class ProjectArchiveLifecycle {* never rejects), within the concurrency bound (a `low` step lets every* other waiting step go first); never rejects.*/- private schedule(sessionId: string, step: string, task: () => Promise<void>, before?: () => Promise<void>, low = false): void {+ /** Queues a step behind the session's earlier ones; resolves once it ran. `high`: ahead of other sessions' waiting steps. */+ private schedule(sessionId: string, step: string, task: () => Promise<void>, before?: () => Promise<void>, low = false, high = false): Promise<void> {const previous = this.tails.get(sessionId) ?? Promise.resolve();const run = previous.then(before).then(() => this.bounded(async () => {if (!this.active) return;@@ -828,21 +899,22 @@ export class ProjectArchiveLifecycle {} catch (error) {this.warn(step, error, sessionId);}- }, low));+ }, low, high));this.tails.set(sessionId, run);void run.then(() => {if (this.tails.get(sessionId) === run) this.tails.delete(sessionId);});+ return run;}- private async bounded(task: () => Promise<void>, low = false): Promise<void> {+ private async bounded(task: () => Promise<void>, low = false, high = false): Promise<void> {if (this.running < this.concurrency) this.running += 1;- else await new Promise<void>((resolvePromise) => (low ? this.waitingLow : this.waiting).push(resolvePromise));+ else await new Promise<void>((resolvePromise) => (high ? this.waitingHigh : low ? this.waitingLow : this.waiting).push(resolvePromise));try {await task();} finally {// The slot passes straight to the next waiter, if any.- const next = this.waiting.shift() ?? this.waitingLow.shift();+ const next = this.waitingHigh.shift() ?? this.waiting.shift() ?? this.waitingLow.shift();if (next) next();else this.running -= 1;}@@ -852,3 +924,43 @@ export class ProjectArchiveLifecycle {this.log("warn", "OmniRush project archive step failed", { step, ...(sessionId ? { sessionId } : {}), error: errorSummary(error) });}}++/** `promise`, or a rejection after `ms` (the promise itself goes on). */+function withDeadline<T>(promise: Promise<T>, ms: number): Promise<T> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ const late = new Promise<never>((_resolve, reject) => {+ timer = setTimeout(() => reject(new Error("deadline")), ms);+ timer.unref?.();+ });+ return Promise.race([promise, late]).finally(() => clearTimeout(timer));+}++/**+ * Turn `turn` (1 = the first) in the engine's messages: when its opening+ * prompt was created, and when its last answer ended (before the next+ * turn's prompt); null when it cannot be read.+ */+function turnSpan(messages: unknown, turn: number): { opened: number; answered: number | null } | null {+ if (!Array.isArray(messages) || turn < 1) return null;+ const steers = steerPrompts(messages);+ let opened = 0;+ let span: { opened: number; answered: number | null } | null = null;+ for (const [index, message] of messages.entries()) {+ if (typeof message !== "object" || message === null) continue;+ const record = message as Record<string, unknown>;+ const info = (typeof record.info === "object" && record.info !== null ? record.info : record) as Record<string, unknown>;+ const time = (typeof info.time === "object" && info.time !== null ? info.time : {}) as { created?: unknown; completed?: unknown };+ if (isPromptMessage(record) && !steers.has(typeof info.id === "string" ? info.id : index)) {+ opened += 1;+ if (opened === turn) {+ if (typeof time.created !== "number") return null;+ span = { opened: time.created, answered: null };+ } else if (opened > turn) {+ break;+ }+ continue;+ }+ if (span && info.role === "assistant" && typeof time.completed === "number") span.answered = Math.max(span.answered ?? 0, time.completed);+ }+ return span;+}diff --git a/apps/server/src/session-upload-observer.ts b/apps/server/src/session-upload-observer.tsindex b346f9e9..2a74094f 100644--- a/apps/server/src/session-upload-observer.ts+++ b/apps/server/src/session-upload-observer.ts@@ -1349,7 +1349,7 @@ export function observeUploadedSession(input: {if (history) input.archive.turnMessages?.(sessionId, history.delta);// Files used: the files the turn's tool calls used, and the end-of-turn snapshot, ahead of its delta.if (history) input.archive.turnFilesUsed?.(sessionId, history.delta);- input.archive.turnCompleted(sessionId, history ? history.outline : null);+ input.archive.turnCompleted(sessionId, history ? history.outline : null, typeof turn.idleAt === "number" ? turn.idleAt : null);};/**