A desktop coding agent with free access to frontier models.
TypeScript
fix(archive): count the app-start window from session activity, not the last archive write
ogsamrat7f46d69ed973
diff --git a/apps/server/src/session-archive/README.md b/apps/server/src/session-archive/README.mdindex 2bccb371..0df87b45 100644--- a/apps/server/src/session-archive/README.md+++ b/apps/server/src/session-archive/README.md@@ -155,7 +155,7 @@ reach the archive with the next turn, or never. A final archive captures it:- `turn_incomplete`: the observer stopped following a turn (`turnIncomplete`: past its 24-hour safety bound, or on an unexpected error; never at a server stop, whose final archives are `app_quit`). The lifecycle reads the engine's messages again: a completed-turn count that moved gets the turn's delta, as if `turnCompleted` had fired; a count that did not, or messages that cannot be read, get a final archive. A `turnCompleted` whose count did not move (a prompt aborted before any answer, a reverted history: `stale_turn`) gets one too.- `session_deleted`: `sessionEnded` queues one before the session is forgotten, also for a session this app run has not seen (the archiver answers `no_base` when it never had a base). The session's record is marked `ended`.- `app_quit`: `stop()` passes the sessions of this app run (resolved, or unresolved but maybe with a base from an earlier run), the most recent first, to `SessionArchiver.stop({finals, budgetMs})`. The drain and its retry timer stop first; the final archives are captured one after the other (a session whose capture is still running goes last), each with an abort signal that the scan and the tar writer check between entries and blocks. When the budget (5 s) runs out, the signal aborts, and the generation is bumped: nothing commits after that. They are uploaded at the next start.- - `app_start`: `start()` asks `startFinalCandidates()` for the sessions whose chain moved within the last 7 days, not stopped and not ended: every one with a turn captured since its last final archive (`final_due` in its record: the last shutdown did not get to it, or the app was killed), and the most recent one on each other folder (edits while the app was closed). At most 10, the most recent first.+ - `app_start`: `start()` asks `startFinalCandidates()` for the sessions active within the last 7 days, not stopped and not ended: every one with a turn captured since its last final archive (`final_due` in its record: the last shutdown did not get to it, or the app was killed), and the most recently active one on each other folder (edits while the app was closed). At most 10, the most recently active first. Active means the session's base, a prompt (`captureBase` on a session with a base, once per app run) or a turn end (`captureDelta`, also when nothing changed): `last_activity_at` in its record. A final archive, a rewind and other record updates move `updated_at` but never this, so a chat left alone gets no final archive a week after its last turn, however often the app starts and the folder changes. A record from before 1.0.11 counts from its `updated_at` (its last archive), and keeps that time from then on.- **Guards.** `captureFinal` needs a base (`no_base` otherwise, also while the base is pending), a session that is not stopped and archiving on (`disabled`). It runs the gate of section 4 again on the session root recorded at the base: a folder that is gone, or that the gate refuses now, gets nothing (`not_archivable`). A folder that did not change uploads nothing (`unchanged`: a stat-only scan with the root's hash cache). Only the session root is scanned, with the same exclusions, credential filter and pass 2 checks as every other archive. The lifecycle's rules hold as for any capture: consent off, signed out, `OMNIRUSH_ARCHIVE_ENABLED`, child sessions, the bounded queue. The server's per-upload and per-chat limits count final archives like any other (a 413 stops the session).- **Several chats on one folder.** Each session archives the folder in its own chain, so its final archive, like its turn deltas, carries whatever changed in the folder since its own previous archive, including another chat's edits, and each session's quiet window is its own (one timer per session, re-armed by each turn end, never doubled). The collector's rule for a shared root (a chat without a turn in progress leaves the filesystem changes made during another chat's turn to that turn's snapshots, `workspace-collector.ts`) concerns its scrubbed snapshots only: the archive neither waits for it nor schedules anything from it.- **A server without final archives.** A backend from before 1.0.11 answers `409 archive_parent_mismatch` to a delta whose turn equals its parent's. Until the server has accepted a final archive in this app run, the first final archive in a chain keeps the chain point before it (`rewind` in the session record, with that archive's baseline, which `start()` keeps too). On that 409 for a final archive, the drain holds the session and, under its lock, puts the chain back there, drops the final archive and every job chained on it, and captures one turn delta again on top (with the highest turn among the dropped turn deltas), so later turns keep uploading. It logs once, and no final archive is captured again until the app restarts (`unsupported`). Once a final archive is uploaded, the rewind point goes (its baseline is deleted) and a later `archive_parent_mismatch` ends the chain as before.@@ -210,7 +210,7 @@ void sessionArchiver.captureBase(sessionId, root, completedTurns).then(() => ses- `root` is the folder the agent started in (`input.workspace.path`), as a real path: the gate `lstat`s it and refuses a symlinked root. The home directory, a filesystem root and the app's own directories are always refused.- `completedTurns` is 0 for a new session. For a resumed session, it is the number of turns the session has already completed.-- The call is cheap when there is nothing to do. With a base already captured, it only reads the session record, with no network. Without `.git`, it runs a few `lstat` calls: the root, then its parents up to the nearest `.git`, home or a guarded directory. Otherwise it fetches `GET /archives/key`, which also checks the archive consent: a 428 means nothing is packed.+- The call is cheap when there is nothing to do. With a base already captured, it only reads the session record and writes the prompt's time into it (the app-start window), with no network. Without `.git`, it runs a few `lstat` calls: the root, then its parents up to the nearest `.git`, home or a guarded directory. Otherwise it fetches `GET /archives/key`, which also checks the archive consent: a 428 means nothing is packed.- Child (sub-agent) sessions share the parent's root and must not be archived. Call this for root sessions only.- Keep a per-process `Set` of session ids already handed to `captureBase`. `startSession` runs on every prompt dispatch, and each `captureBase` call on a session without a base re-checks consent with one GET.@@ -365,4 +365,4 @@ cd apps/server && bun --conditions=development test src/session-archive- a chain conflict and an unknown kid;- sign-out, and a slow part PUT aborted within a second of `signOut()` (the upload aborted with the credentials still held, nothing completed or sent afterwards) and of `stop()` (the job kept, resumed by the next start);- the app's state dir under the root pruned;- - final archives: none without a base or when unchanged; the equal-turn wire values (sequence, parent, turn), chained finals, `trigger` and `reason` in the manifest; a server without final archives (`strictTurns`): one refused create, the chain put back, the turn delta queued behind the final captured again, later turns uploaded, one log line; shutdown finals within the budget, a hanging one cut at it and captured at the next start; the app-start candidates (a final due, the most recent session per folder; not deleted or older than 7 days); a deleted session whose folder is gone.+ - final archives: none without a base or when unchanged; the equal-turn wire values (sequence, parent, turn), chained finals, `trigger` and `reason` in the manifest; a server without final archives (`strictTurns`): one refused create, the chain put back, the turn delta queued behind the final captured again, later turns uploaded, one log line; shutdown finals within the budget, a hanging one cut at it and captured at the next start; the app-start candidates (a final due, the most recently active session per folder; not deleted or older than 7 days); the 7-day window counted from the last base, prompt or turn (an unchanged one too), never extended by a final archive (app starts every 6 days with the folder edited: finals stop a week after the last turn), by a refused final's rewind, or in a record from before 1.0.11; a deleted session whose folder is gone.diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex 610c1dee..2594c707 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -594,10 +594,114 @@ describe("SessionArchiver final archives", () => {const members = await openArchive(object.object!);expect(manifestOf(members)).toMatchObject({ trigger: "final", reason: "app_start", turn: 2 });expect(members.map((member) => member.name)).toContain("closed.txt");- // Its final settles the session whose last turn had none; the folder's most recent archive is now that one.+ // Its final settles the session whose last turn had none. A final is not activity: the folder's most+ // recently active chat is still the one with the latest turn.clock += 1_000;expect(await next.captureFinal("ses_start_due_01", "app_start")).toMatchObject({ status: "queued", sequence: 2 });- expect(await next.startFinalCandidates()).toEqual(["ses_start_due_01"]);+ expect(await next.startFinalCandidates()).toEqual(["ses_start_last_01"]);+ });++ test("app start: the 7-day window counts from the chat's last base, prompt or turn; its final archives never extend it", async () => {+ const server = new FakeArchiveServer();+ const { root } = await project();+ const state = await tempDir("state");+ const day = 24 * 60 * 60_000;+ let clock = Date.now() - 40 * day;+ const appRun = () => archiver(server, state, { now: () => new Date(clock) });+ const id = "ses_window_turns_1";++ // Day 0: the chat's base and its one turn.+ const first = appRun();+ expect((await first.captureBase(id, root)).status).toBe("queued");+ await writeFile(join(root, "turn1.txt"), "turn 1\n");+ expect((await first.captureDelta(id, root, 1)).status).toBe("queued");+ expect(await first.drain()).toMatchObject({ uploaded: 2 });+ await first.stop();++ // Day 6: the folder was edited outside the app, which then starts: within the window, one app_start final.+ clock += 6 * day;+ await writeFile(join(root, "outside.txt"), "day 6\n");+ const second = appRun();+ expect(await second.startFinalCandidates()).toEqual([id]);+ expect(await second.captureFinal(id, "app_start")).toMatchObject({ status: "queued", sequence: 2 });+ expect(await second.drain()).toMatchObject({ uploaded: 1 });+ await second.stop();++ // Day 12: edited again, the app starts again. The final six days ago was not activity: nothing is checked.+ clock += 6 * day;+ await writeFile(join(root, "outside.txt"), "day 12\n");+ const third = appRun();+ expect(await third.startFinalCandidates()).toEqual([]);+ expect(server.objects()).toHaveLength(3);++ // A prompt on the chat (its start step; the base exists) makes it active again.+ expect(await third.captureBase(id, root, 1)).toEqual({ status: "skipped", reason: "exists" });+ expect(await third.startFinalCandidates()).toEqual([id]);+ // Day 13: its turn; day 15: a turn that changed nothing, which counts too.+ clock += day;+ expect(await third.captureDelta(id, root, 2)).toMatchObject({ status: "queued", sequence: 3 });+ clock += 2 * day;+ expect(await third.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "unchanged" });+ await writeFile(join(root, "outside.txt"), "day 15\n");+ expect(await third.captureFinal(id, "idle")).toMatchObject({ status: "queued", sequence: 4 });+ expect(await third.drain()).toMatchObject({ uploaded: 2, pending: 0 });+ await third.stop();++ // Day 21, six days after that last turn (eight after the last change): checked once more; day 23, eight days after it: no longer.+ clock += 6 * day;+ await writeFile(join(root, "outside.txt"), "day 21\n");+ const fourth = appRun();+ expect(await fourth.startFinalCandidates()).toEqual([id]);+ expect(await fourth.captureFinal(id, "app_start")).toMatchObject({ status: "queued", sequence: 5 });+ expect(await fourth.drain()).toMatchObject({ uploaded: 1 });+ await fourth.stop();+ clock += 2 * day;+ await writeFile(join(root, "outside.txt"), "day 23\n");+ expect(await appRun().startFinalCandidates()).toEqual([]);+ const finals = await Promise.all(server.objects().map(async (object) => manifestOf(await openArchive(object.object!)).reason));+ expect(finals).toEqual([undefined, undefined, "app_start", undefined, "idle", "app_start"]);+ });++ test("app start window: a record from before 1.0.11 counts from its last archive, and a final refused by a server without them does not extend it", async () => {+ const server = new FakeArchiveServer();+ server.strictTurns = true;+ const { root } = await project();+ const state = await tempDir("state");+ const day = 24 * 60 * 60_000;+ let clock = Date.now() - 40 * day;+ const appRun = () => archiver(server, state, { now: () => new Date(clock) });+ const id = "ses_window_legacy";++ const first = appRun();+ expect((await first.captureBase(id, root)).status).toBe("queued");+ await writeFile(join(root, "turn1.txt"), "turn 1\n");+ expect((await first.captureDelta(id, root, 1)).status).toBe("queued");+ expect(await first.drain()).toMatchObject({ uploaded: 2 });+ await first.stop();+ // The session record as 1.0.10 wrote it: no activity time, no final archive fields.+ const sessions = join(state, ARCHIVE_STATE_DIRECTORY, "sessions");+ const [name] = await readdir(sessions);+ const record = JSON.parse(await readFile(join(sessions, name!), "utf8")) as Record<string, unknown>;+ for (const field of ["last_activity_at", "final_due", "rewind", "ended"]) delete record[field];+ await writeFile(join(sessions, name!), `${JSON.stringify(record)}\n`);++ // Day 6: within the window (its last archive); the server refuses the final and the chain goes back.+ clock += 6 * day;+ await writeFile(join(root, "outside.txt"), "day 6\n");+ const second = appRun();+ expect(await second.startFinalCandidates()).toEqual([id]);+ expect(await second.captureFinal(id, "app_start")).toMatchObject({ status: "queued", sequence: 2 });+ expect(await second.drain()).toMatchObject({ uploaded: 0, dropped: 1 });+ // Waits for the rewind, which rewrote the record (updated_at is day 6, and a final is due again).+ expect(await second.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "unsupported" });+ await second.stop();+ const rewound = JSON.parse(await readFile(join(sessions, name!), "utf8")) as Record<string, unknown>;+ expect(rewound).toMatchObject({ next_sequence: 2, final_due: true, updated_at: new Date(clock).toISOString(), last_activity_at: record.updated_at });++ // Day 12: neither the final nor its rewind moved the window.+ clock += 6 * day;+ expect(await appRun().startFinalCandidates()).toEqual([]);+ expect(server.objects()).toHaveLength(2);});test("a session deleted with its folder gone gets no final, and is not checked at the next start", async () => {diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 0011f4b4..5f539cf1 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -59,7 +59,7 @@ const MAX_JOB_AGE_MS = 7 * 24 * 60 * 60_000;const JOB_RETRY_BASE_MS = 60_000;const JOB_RETRY_MAX_MS = 60 * 60_000;const SIGN_OUT_ABORT_TIMEOUT_MS = 5_000;-/** At app start, sessions whose chain moved within this long may get a final archive (startFinalCandidates). */+/** At app start, sessions active (a base, a prompt, a turn) within this long may get a final archive (startFinalCandidates). */const START_FINAL_WINDOW_MS = 7 * 24 * 60 * 60_000;/** At most this many sessions are scanned for a final archive at app start. */const MAX_START_FINALS = 10;@@ -153,7 +153,15 @@ const sessionStateSchema = z.object({final_due: z.boolean().optional(),/** When the session was deleted in the app: no final archive at app start. */ended: z.string().nullable().optional(),-});+ /**+ * When the session was last active: its base, a prompt (captureBase once+ * per app run) or a turn end (captureDelta). Never a final archive, a+ * rewind or other bookkeeping, which move updated_at: the app-start window+ * counts from this. A record without it (1.0.10) counts from its+ * updated_at, and keeps that time from then on.+ */+ last_activity_at: z.string().optional(),+}).transform((state) => ({ ...state, last_activity_at: state.last_activity_at ?? state.updated_at }));type SessionState = z.infer<typeof sessionStateSchema>;const createRequestSchema = z.object({@@ -303,15 +311,20 @@ export class SessionArchiver {/*** Base archive at session start (new session, or a resumed one without a* base). No-op when the gate says not archivable, archiving is off, or the- * session already has a base. Checks consent through GET /archives/key- * before packing anything.+ * session already has a base (its record then only notes the prompt as+ * activity, for the app-start window). Checks consent through GET+ * /archives/key before packing anything.*/captureBase(sessionId: string, root: string, turn = 0): Promise<CaptureResult> {return this.guard("base", sessionId, turn, async () => this.withSession(sessionId, async () => {const generation = this.generation;const state = await this.loadSession(sessionId);if (state?.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };- if (state && state.next_sequence > 0) return { status: "skipped", reason: "exists" };+ if (state && state.next_sequence > 0) {+ // A prompt on a session archived in an earlier app run: it is active again.+ if (generation === this.generation) await this.saveSession({ ...state, last_activity_at: this.now().toISOString() });+ return { status: "skipped", reason: "exists" };+ }return this.captureBaseLocked(sessionId, resolve(root), turn, generation);}));}@@ -321,15 +334,19 @@ export class SessionArchiver {* stopped archiving, or when nothing in the folder changed. A session whose* base could not be captured yet (key unavailable) gets its base instead.* A null turn (the engine's messages could not be read) follows the last- * archived turn.+ * archived turn. 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> {return this.guard("delta", sessionId, turn ?? 0, async () => this.withSession(sessionId, async () => {const generation = this.generation;if (this.disabled) return { status: "skipped", reason: "disabled" };- const state = await this.loadSession(sessionId);- if (!state) return { status: "skipped", reason: "no_base" };- if (state.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };+ const loaded = await this.loadSession(sessionId);+ if (!loaded) return { status: "skipped", reason: "no_base" };+ if (loaded.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };+ // A turn ended: the session was active, whatever this capture does.+ const state: SessionState = { ...loaded, last_activity_at: this.now().toISOString() };+ if (generation === this.generation) await this.saveSession(state);if (resolve(root) !== state.root) this.log("warn", "OmniRush archive delta root differs from the session root; using the session root", { sessionId });if (state.next_sequence === 0) return this.captureBaseLocked(sessionId, state.root, turn ?? 0, generation);const next = turn ?? (state.last_turn ?? 0) + 1;@@ -384,10 +401,12 @@ export class SessionArchiver {/*** The sessions to give a final archive once at app start: every session* with a turn captured since its last final archive (the app quit before- * that final, or crashed), and the most recent session on each other- * folder (the folder may have changed while the app was closed). Only- * sessions whose chain moved within the last 7 days, not stopped and not- * deleted; the most recent first, at most 10.+ * that final, or crashed), and the most recently active session on each+ * other folder (the folder may have changed while the app was closed).+ * Only sessions active (a base, a prompt, a turn) within the last 7 days,+ * not stopped and not deleted: a final archive never extends the window,+ * so a chat left alone gets none a week after its last turn, however+ * often the app starts. The most recently active first, at most 10.*/async startFinalCandidates(): Promise<string[]> {try {@@ -400,10 +419,10 @@ export class SessionArchiver {const parsed = sessionStateSchema.safeParse(await readJsonFile(join(this.dirs.sessions, name)));if (!parsed.success || name !== `${stateKey(parsed.data.session_id)}.json`) continue;const state = parsed.data;- if (state.stopped || state.ended || state.next_sequence === 0 || !(nowMs - Date.parse(state.updated_at) <= START_FINAL_WINDOW_MS)) continue;+ if (state.stopped || state.ended || state.next_sequence === 0 || !(nowMs - Date.parse(state.last_activity_at) <= START_FINAL_WINDOW_MS)) continue;sessions.push(state);}- sessions.sort((left, right) => right.updated_at.localeCompare(left.updated_at));+ sessions.sort((left, right) => right.last_activity_at.localeCompare(left.last_activity_at));const roots = new Set<string>();const picked: string[] = [];for (const state of sessions) {@@ -563,6 +582,7 @@ export class SessionArchiver {baseline: null,stopped: null,updated_at: this.now().toISOString(),+ last_activity_at: this.now().toISOString(),};if (fetched.status === "unavailable") {// Remembered with next_sequence 0: the next captureDelta tries the base again.diff --git a/docs/project-archive.md b/docs/project-archive.mdindex 2f66b5ad..5d9347c6 100644--- a/docs/project-archive.md+++ b/docs/project-archive.md@@ -77,9 +77,11 @@ archived.- when you delete the chat in the app, if its folder still exists;- when you quit the app. The app spends at most about 5 seconds on thiswhile it shuts down; the copy is uploaded the next time the app runs;- - when the app starts again, for chats archived in the last 7 days:+ - when the app starts again, for chats you sent a message in, or that+ finished a turn, in the last 7 days (a final copy does not count as+ use, so a chat you left gets none a week after its last turn):anything the last shutdown did not get to, and changes made while the- app was closed (for the most recent chat on each folder).+ app was closed (for the chat used most recently on each folder).A folder that did not change uploads nothing. A final copy is only takenfor a chat that already has its full copy, and only while the folder is