Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: project changes saved right after the first edit

ogsamratf5b6ad9b353c
diff --git a/apps/server/src/session-archive/README.md b/apps/server/src/session-archive/README.md
index 0b9a2786..ca9e8cc4 100644
--- a/apps/server/src/session-archive/README.md
+++ b/apps/server/src/session-archive/README.md
@@ -376,7 +376,7 @@ backend's `docs/omnirush-project-archive.md` ("Capture v2").
- **Key and consent.** A folder base reuses the key from the probe it just made. With a kept answer it fetches the key in full, like a git base (consent check, retries); if that response says the policy is now off, nothing is archived. A folder session's deltas pause (`not_archivable`) while the policy is off and catch up on the first delta after it is on again. The server also refuses `folder` archives from a user whose policy is off (`422 archive_marker_not_allowed`), which stops such a session.
- **Touched files.** GET /archives/key also carries `policy.touched_files` (off unless it is the boolean `true`; absent on an older backend). Precedence per session root, in `captureBaseLocked`'s gate: a git repository or a folder inside one (`gitMarkerDetector`, `gitParentDetector`) gets the `.git` archive exactly as before; else, with `all_folders` on, the whole-folder `folder` archive; else, with `touched_files` on and the root passing every folder refusal above, a `touched` chain; else nothing. The same probe, TTL and kept answer serve both flags.
- **The touched set.** The session uploader (in the same capture host, on the worker) reports every workspace-relative path a session touches, before its own denylist: tool paths in the trace (`path`, `filePath`, ... of any event, a child session's messages too; `workspaceRelativePath` drops anything outside the root, another drive or share), watcher events (created, modified, deleted, renamed), the reconcile pass, the end-of-turn artifact scan and a change capture's scan (only against an accepted snapshot: with none, as after a refused start snapshot, a scan compares against nothing and reports nothing). `TouchedPathStore` keeps them per session: in memory until the gate has run (a refused session drops them), then appended to `omnirush-archive/touched/<session key>.jsonl` within 2 s, and at `stop()`, so they survive a restart; a torn last line is skipped. At most `MAX_TOUCHED_PATHS` (100,000) per session, one `warn` past it. A stopped or deleted session's file goes; `start()` removes the files of sessions that are stopped, ended or unknown (every archived chain keeps its touched paths across a restart), and sign-out removes them all.
- - **Captures.** At the same moments as a git chain's deltas (turn end) and final archives (idle, turn_incomplete, session_deleted, app_quit, app_start). `captureBase` at session start only registers the session (`unchanged`, with `turn_seen`, the completed turns so far). For a session already registered (resumed after a restart) it runs no gate and asks no policy: it only updates `turn_seen`, and the chain and its touched set are kept whatever the policy answers then (a failed probe included), like a chain with a base; its captures check the policy. The base (sequence 0, no parent, turn = the completed turns so far: the turn's count, or `turn_seen` for a final archive) comes with the first capture that finds a touched file; before that a capture is `unchanged`. Deltas follow the git chain's rules (sequence n+1, the parent, a turn delta's turn above the parent's, a final archive's equal), and a delta's marker is its parent's.
+ - **Captures.** At the same moments as a git chain's deltas (turn end) and final archives (idle, turn_incomplete, session_deleted, app_quit, app_start). `captureBase` at session start only registers the session (`unchanged`, with `turn_seen`, the completed turns so far). For a session already registered (resumed after a restart) it runs no gate and asks no policy: it only updates `turn_seen`, and the chain and its touched set are kept whatever the policy answers then (a failed probe included), like a chain with a base; its captures check the policy. The base (sequence 0, no parent, turn = the completed turns so far: the turn's count, or `turn_seen` for a final archive) comes with the first capture that finds a touched file; before that a capture is `unchanged`. The lifecycle asks for that capture `TOUCHED_BASE_DELAY_MS` (30 s) after the session's first touched path in an app run (a final archive with reason `first_touch`, only while `touchedBaseDue`), so a long first turn, or an app quit before the turn ends, no longer leaves the session without a copy for hours or until the next app start. Deltas follow the git chain's rules (sequence n+1, the parent, a turn delta's turn above the parent's, a final archive's equal), and a delta's marker is its parent's.
- **What is archived.** `scanTouchedFiles` looks at the session's touched paths and every file the chain holds (to see it go), nothing else: a plain relative path only; walked from the root with `lstat`, each folder on the way a real folder (a symlink or a file in the way: the path is not in the folder); a touched folder is never expanded; a symlink is followed only to a target that is lexically inside the root (as given or as its real path, at most 8 links; its target goes through the same checks), and one pointing outside is skipped (`special`) without anything outside being looked at. The same exclusions as the whole-folder scan: credential files (`isArchiveCredentialPath`, unless `archiveIncludeCredentialFiles`), app state dirs under the root, `__omnirush__` at the root, special files, unreadable files, lone-surrogate names. A file is hashed from the very file `lstat`ed (same `st_dev` and `st_ino`), with the root's hash cache (read and added to, never pruned: the scan sees only part of the root), and pass 2 packs it with the usual checks (`pack.ts`). Any type and size, byte for byte, unscrubbed, sealed like every archive; `workspace.git` is `null`.
- **Manifest.** `"scope": "touched"` in the sealed `manifest.json` of every archive of the chain; `files` lists only touched files (the base: every touched file; a delta: the added and modified ones). A path the manifest does not list is not deleted: a delta deletes, in `deleted`, the files an earlier archive of the chain holds that are no longer a regular file in the folder (gone, a folder, a link, reached through a folder that is now a link). A file that cannot be read for now stays in the baseline as it was.
- **Policy off.** With `touched_files` off or absent nothing is written for such a folder: `captureBase` is `not_archivable` after the probe, and no touched path is kept. A touched chain's captures pause (`not_archivable`) while it is off and catch up once it is on (the touched set is kept). The server refuses a `touched` create from a user whose policy is off with `422 archive_marker_not_allowed`: the drain drops the session's chain (its jobs, the session stopped, one `warn` line, no retry) and keeps the policy as off for `touched` until it is asked again (`POLICY_TTL_MS`), so no other chain starts meanwhile. The same holds for `folder`.
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index a2666c43..46409d41 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -932,6 +932,18 @@ export class SessionArchiver {
* dropped for any other once the gate has run. Cheap: called for every
* file event.
*/
+ /**
+ * A touched-files chain still waiting for its base: the base comes with the
+ * first capture that finds a touched file, so the lifecycle asks for one
+ * soon after the first touch instead of waiting for the turn to end.
+ */
+ async touchedBaseDue(sessionId: string): Promise<boolean> {
+ if (this.disabled || !SESSION_ID_PATTERN.test(sessionId) || this.stoppedSessions.has(sessionId)) return false;
+ await this.start();
+ const state = await this.loadSession(sessionId);
+ return state?.next_sequence === 0 && state.marker === TOUCHED_MARKER && !state.stopped;
+ }
+
recordTouched(sessionId: string, path: string): void {
if (this.disabled || !SESSION_ID_PATTERN.test(sessionId) || this.stoppedSessions.has(sessionId)) return;
// An absolute path is a file outside the workspace (outside.ts), kept in its normal form.
diff --git a/apps/server/src/session-archive/lifecycle.test.ts b/apps/server/src/session-archive/lifecycle.test.ts
index f8e00d07..148b016b 100644
--- a/apps/server/src/session-archive/lifecycle.test.ts
+++ b/apps/server/src/session-archive/lifecycle.test.ts
@@ -36,6 +36,12 @@ class FakeArchiver implements ProjectArchiver {
readonly stops: Array<{ finals?: readonly string[]; budgetMs?: number }> = [];
/** recordTouched and forgetTouched calls (not in `calls`). */
readonly touched: string[] = [];
+ /** What touchedBaseDue answers (a touched-files chain still without its base). */
+ baseDue = false;
+
+ async touchedBaseDue(_sessionId: string): Promise<boolean> {
+ return this.baseDue;
+ }
async captureBase(sessionId: string, root: string, turn = 0): Promise<CaptureResult> {
this.calls.push(`base ${sessionId} ${root} ${turn}`);
@@ -115,7 +121,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; turnDeltaWaitMs?: number } = {}) {
+function lifecycle(archiver: ProjectArchiver, options: { enabled?: boolean; accountMissing?: boolean; now?: () => number; concurrency?: number; finalIdleMs?: number; quitBudgetMs?: number; turnDeltaWaitMs?: number; touchedBaseDelayMs?: number } = {}) {
const logs: Array<{ level: string; message: string; attributes?: Record<string, unknown> }> = [];
const subject = new ProjectArchiveLifecycle({
archiver,
@@ -128,6 +134,7 @@ function lifecycle(archiver: ProjectArchiver, options: { enabled?: boolean; acco
...(options.finalIdleMs !== undefined ? { finalIdleMs: options.finalIdleMs } : {}),
...(options.quitBudgetMs !== undefined ? { quitBudgetMs: options.quitBudgetMs } : {}),
...(options.turnDeltaWaitMs !== undefined ? { turnDeltaWaitMs: options.turnDeltaWaitMs } : {}),
+ ...(options.touchedBaseDelayMs !== undefined ? { touchedBaseDelayMs: options.touchedBaseDelayMs } : {}),
});
return { subject, logs };
}
@@ -297,6 +304,31 @@ describe("ProjectArchiveLifecycle", () => {
expect(archiver.touched).toHaveLength(4);
});
+ test("a touched-files base is taken soon after the first touched path, not at the turn's end", async () => {
+ const archiver = new FakeArchiver();
+ archiver.base = async () => skipped("unchanged");
+ archiver.baseDue = true;
+ archiver.final = async () => queued("base");
+ const { subject } = lifecycle(archiver, { touchedBaseDelayMs: 10 });
+ const root = await tempDir("root");
+ subject.sessionStarted({ sessionId: "ses_touch_0003", root, engine: engine().reader });
+ await subject.settled();
+ subject.pathTouched("ses_touch_0003", "a.py");
+ subject.pathTouched("ses_touch_0003", "b.py");
+ await new Promise((resolve) => setTimeout(resolve, 40));
+ await subject.settled();
+ // One ask per session and run, though two paths came.
+ expect(archiver.finals()).toEqual(["final ses_touch_0003 first_touch"]);
+ // A chain that has its base (a git or project folder, or a base taken since): no extra capture.
+ archiver.baseDue = false;
+ subject.sessionStarted({ sessionId: "ses_touch_0004", root, engine: engine().reader });
+ await subject.settled();
+ subject.pathTouched("ses_touch_0004", "c.py");
+ await new Promise((resolve) => setTimeout(resolve, 40));
+ await subject.settled();
+ expect(archiver.finals()).toEqual(["final ses_touch_0003 first_touch"]);
+ });
+
test("delta turn numbers come from the engine's completed turns, in order after the base", async () => {
const archiver = new FakeArchiver();
const { subject } = lifecycle(archiver);
diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.ts
index c2384420..c311fa9b 100644
--- a/apps/server/src/session-archive/lifecycle.ts
+++ b/apps/server/src/session-archive/lifecycle.ts
@@ -23,7 +23,7 @@ import type { UsedToolCall } from "./files-used.js";
export type ProjectArchiver = Pick<
SessionArchiver,
"captureBase" | "captureDelta" | "captureFinal" | "startFinalCandidates" | "recordTouched" | "forgetTouched" | "drain" | "signOut" | "stop"
-> & Partial<Pick<SessionArchiver, "claimAccount" | "discardAll" | "pendingStatus" | "startManifest" | "hasStartManifest" | "captureState" | "recordAttachments" | "recordMedia" | "recordBinary" | "filesUsedToolCallEnded" | "filesUsedTurnStarted" | "filesUsedTurnEnded" | "filesUsedMaybe" | "filesUsedStatus">>;
+> & Partial<Pick<SessionArchiver, "claimAccount" | "discardAll" | "pendingStatus" | "startManifest" | "hasStartManifest" | "captureState" | "recordAttachments" | "recordMedia" | "recordBinary" | "filesUsedToolCallEnded" | "filesUsedTurnStarted" | "filesUsedTurnEnded" | "filesUsedMaybe" | "filesUsedStatus" | "touchedBaseDue">>;
/** Engine reads for one session, resolving to the parsed JSON, or null when it cannot be read. */
export type ArchiveEngineReads = {
@@ -58,6 +58,8 @@ export type ProjectArchiveLifecycleOptions = {
baseMaxDeferMs?: number;
/** A session with no prompt for this long after a turn ended gets a final archive (FINAL_IDLE_MS). */
finalIdleMs?: number;
+ /** A touched-files session's base is taken this long after its first touched path (TOUCHED_BASE_DELAY_MS). */
+ touchedBaseDelayMs?: 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). */
@@ -100,6 +102,8 @@ export function baseRetried(result: CaptureResult): boolean {
const DEFAULT_CONSENT_RECHECK_MS = 10 * 60_000;
/** The quiet window after a turn: with no new prompt by then, the session gets a final archive. */
export const FINAL_IDLE_MS = 10 * 60_000;
+/** A touched-files base this long after the session's first touched path (its paths are on disk within 2 s). */
+export const TOUCHED_BASE_DELAY_MS = 30_000;
/**
* What shutdown spends, at most, packing final archives (the desktop app
* waits for the server to stop before it quits). What does not fit is
@@ -174,6 +178,8 @@ type SessionRecord = {
retries?: { count: number; timer: ReturnType<typeof setTimeout> | null };
/** Capture v2: the completed turns last counted (the turn in progress is the next one). */
turns?: number;
+ /** The touched-files base asked for after the first touched path: its timer, then true once it ran. */
+ touchedBase?: ReturnType<typeof setTimeout> | true;
/** Capture v2: a prompt went out and this turn's first tool call has not been seen yet. */
awaitingTool?: boolean;
/** When the latest prompt, and the one before it, were dispatched. */
@@ -212,6 +218,7 @@ export class ProjectArchiveLifecycle {
private readonly baseIdleMs: number;
private readonly baseMaxDeferMs: number;
private readonly finalIdleMs: number;
+ private readonly touchedBaseDelayMs: number;
private readonly quitBudgetMs: number;
private readonly turnDeltaWaitMs: number;
private readonly now: () => number;
@@ -253,6 +260,7 @@ export class ProjectArchiveLifecycle {
this.baseIdleMs = Math.max(0, options.baseIdleMs ?? 0);
this.baseMaxDeferMs = Math.max(this.baseIdleMs, options.baseMaxDeferMs ?? this.baseIdleMs * 5);
this.finalIdleMs = Math.max(0, options.finalIdleMs ?? FINAL_IDLE_MS);
+ this.touchedBaseDelayMs = Math.max(0, options.touchedBaseDelayMs ?? TOUCHED_BASE_DELAY_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;
@@ -478,6 +486,19 @@ export class ProjectArchiveLifecycle {
const record = this.sessions.get(sessionId);
if (!this.active || this.consentOff || !record || (!record.root && !record.start)) return;
this.archiver.recordTouched(sessionId, path);
+ // A touched-files base waited for the turn's end (hours on a long first turn, a day when the app quit first).
+ if (record.root && !record.touchedBase && this.archiver.touchedBaseDue) {
+ const timer = setTimeout(() => {
+ record.touchedBase = true;
+ this.schedule(sessionId, "final", async () => {
+ if (this.consentOff || this.sessions.get(sessionId) !== record || !record.root) return;
+ if (!(await this.archiver.touchedBaseDue?.(sessionId))) return;
+ this.settle(await this.archiver.captureFinal(sessionId, "first_touch"));
+ }, undefined, true);
+ }, this.touchedBaseDelayMs);
+ timer.unref?.();
+ record.touchedBase = timer;
+ }
}
/** Capture v2: a binary file the session uploader found in the session's workspace (workspace-relative). */
@@ -786,6 +807,7 @@ export class ProjectArchiveLifecycle {
/** The session is forgotten (deleted, signed out, shutdown): none of its timers runs any more. */
private release(record: SessionRecord): void {
+ if (record.touchedBase && record.touchedBase !== true) clearTimeout(record.touchedBase);
this.cancelIdleFinal(record);
this.cancelRetry(record);
this.turnSettled(record);
diff --git a/apps/server/src/session-archive/manifest.ts b/apps/server/src/session-archive/manifest.ts
index 5e14405c..a3a23ad7 100644
--- a/apps/server/src/session-archive/manifest.ts
+++ b/apps/server/src/session-archive/manifest.ts
@@ -61,7 +61,7 @@ export type ArchiveKind = "base" | "delta";
*/
export type ArchiveTrigger = "turn" | "final" | "pre_tool";
/** What prompted a final archive (manifest.json `reason`). */
-export type FinalReason = "idle" | "turn_incomplete" | "session_deleted" | "app_quit" | "app_start";
+export type FinalReason = "idle" | "turn_incomplete" | "session_deleted" | "app_quit" | "app_start" | "first_touch";
/**
* manifest.json `reason`: a final archive's, or "late_writes" on a pre_tool
* state holding files written after the turn before ended (a background