A desktop coding agent with free access to frontier models.
TypeScript
Merge branch 'feat/v1.0.11-capture' into feat/v1.0.11-touched-files
ogsamrat15b06808415c
diff --cc apps/server/src/session-archive/README.mdindex a8e94d22,0df87b45..fbbfe74b--- a/apps/server/src/session-archive/README.md+++ b/apps/server/src/session-archive/README.md@@@ -164,8 -155,8 +164,8 @@@ reach the archive with the next turn, o- `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), the most recent one on each other folder (edits while the app was closed), and every touched-files session (its files are its own), also one without a base yet that has touched paths. 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).++ - `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), the most recently active one on each other folder (edits while the app was closed), and every touched-files session (its files are its own), also one without a base yet that has touched paths. At most 10, the most recently active first. Active means the session's base, a prompt (`captureBase` on a session with a base, or on a touched-files session registered in an earlier run, 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 touched-files session without one gets its base instead, when it has a touched file), 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 plain folder or touched files: the root checks and the folder refusals, and their policy must be on): 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.@@@ -219,7 -210,7 +219,7 @@@ void sessionArchiver.captureBase(sessio- `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, and for a folder the all-folders refusals accept it reads the kept policy or sends one quick probe (at most one GET per 5 minutes, see "All folders" below). 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.++- 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, and for a folder the all-folders refusals accept it reads the kept policy or sends one quick probe (at most one GET per 5 minutes, see "All folders" below). 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.@@@ -393,5 -365,4 +393,5 @@@ cd apps/server && bun --conditions=deve- 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;+ - touched files (`touched_files` on): only the touched files, byte for byte (a PDF read, a binary written), untouched files, credentials and a link out of the folder never, the base at the first capture with a touched file, a delta with the changed files and the deleted one, nothing when only an untouched file changed; a git folder archived as today with no touched paths kept; `all_folders` winning over `touched_files`; the policy off, absent or not a boolean: nothing uploaded or kept; the refused folders never archived; finals (the base from an idle final, `app_quit`, `app_start`) and a restart keeping the chain and its touched paths, a session without a base getting it at app start, a deleted session's paths gone; a restart whose key probe fails keeping a chain without a base and its paths, its base catching up with every path once the policy answers; the chain pausing while the policy is off; `422 archive_marker_not_allowed` dropping the chain once, with no retry, and no other touched chain starting until the policy is asked again;- - 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 --cc apps/server/src/session-archive/index.tsindex 3b38bfa3,5f539cf1..9ef6b17e--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@@ -184,9 -153,15 +184,17 @@@ 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(),+ /** A touched-files chain without its base yet: the completed turns last seen, the turn of a base a final archive captures. */+ turn_seen: z.number().int().nonnegative().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({@@@ -362,54 -311,46 +370,65 @@@ 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. A folder without `.git` counts as a project- * only while the all-folders or the touched-files policy is on+ * 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.++ * /archives/key before packing anything. A folder without `.git` counts as++ * a project only while the all-folders or the touched-files policy is on+ * (folderPolicy); with both off nothing is written and only the policy is+ * read. A touched-files session is only registered here ("unchanged"): its+ * base comes with the first delta or final archive that has a touched file.- * One registered before (resumed) runs no gate again.++ * One registered before (resumed) runs no gate again; its record notes the++ * prompt as activity too.*/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);+ // A touched-files chain registered earlier (a resumed session) still waits for its base. Like a+ // chain with one, it keeps its paths whatever the policy answers now: its deltas and finals ask.++ // The prompt counts as its activity too.+ if (state?.marker === TOUCHED_MARKER) {+ if (!state.ended) this.touched.track(sessionId);- if (state.turn_seen !== turn && generation === this.generation) await this.saveSession({ ...state, turn_seen: turn, updated_at: this.now().toISOString() });++ if (generation === this.generation) await this.saveSession({ ...state, turn_seen: turn, updated_at: this.now().toISOString(), last_activity_at: this.now().toISOString() });+ return { status: "skipped", reason: "unchanged" };+ }+ return this.captureBaseLocked(sessionId, resolve(root), turn, generation, true);}));}/*** Delta after a completed turn. No-op without a base, when the session- * 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. Whatever it captures, the turn counts as the session's- * activity (the app-start window).+ * stopped archiving, when nothing in the folder changed, or for a plain+ * folder (marker `folder`) or touched files (marker `touched`) while their+ * policy is off. A session whose base could not be captured yet (key+ * unavailable, or no touched file yet) gets its base instead. A null turn+ * (the engine's messages could not be read) follows the last archived- * turn.++ * 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;+ const touched = state.marker === TOUCHED_MARKER;+ if (state.next_sequence === 0 && !touched) return this.captureBaseLocked(sessionId, state.root, turn ?? 0, generation);+ const next = state.next_sequence === 0 ? turn ?? state.turn_seen ?? 0 : turn ?? (state.last_turn ?? 0) + 1;if (state.last_turn !== null && next <= state.last_turn) return { status: "skipped", reason: "stale_turn" };+ // A plain folder's or touched files' archives pause while their policy is off; the next one after it is on again catches up.+ if ((state.marker === FOLDER_MARKER || touched) && !(await this.policyAllows(state.marker, generation))) return { status: "skipped", reason: "not_archivable" };const key = await this.currentKey();if (key === "disabled") return { status: "skipped", reason: "disabled" };if (key === "unavailable") return { status: "skipped", reason: "unavailable" };@@@ -494,12 -401,12 +513,14 @@@/*** 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), and- * every touched-files session (its files are its own). Only sessions whose- * chain moved within the last 7 days, not stopped and not deleted, with a- * base or (touched files) a touched path; 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.++ * that final, or crashed), the most recently active session on each++ * other folder (the folder may have changed while the app was closed),++ * and every touched-files session (its files are its own). Only sessions++ * active (a base, a prompt, a turn) within the last 7 days, not stopped++ * and not deleted, with a base or (touched files) a touched path: 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 {@@@ -512,12 -419,10 +533,12 @@@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 || !(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;++ if (state.stopped || state.ended || !(nowMs - Date.parse(state.last_activity_at) <= START_FINAL_WINDOW_MS)) continue;+ // Without a base, only a touched-files session that touched something may get one.+ if (state.next_sequence === 0 && !(state.marker === TOUCHED_MARKER && (await this.touched.has(state.session_id)))) 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) {@@@ -701,11 -582,10 +722,12 @@@baseline: null,stopped: null,updated_at: this.now().toISOString(),+ ...(touched ? { turn_seen: turn } : {}),+ last_activity_at: this.now().toISOString(),};+ if (touched) this.touched.track(sessionId);if (fetched.status === "unavailable") {- // Remembered with next_sequence 0: the next captureDelta tries the base again.+ // Remembered with next_sequence 0: the next captureDelta tries the base again (a folder passes the gate and the policy again).if (generation === this.generation) await this.saveSession(state);this.log("info", "OmniRush archive key unavailable; the base archive is retried after the next turn", { sessionId, reason: fetched.reason });return { status: "skipped", reason: "unavailable" };diff --cc docs/project-archive.mdindex 84b2edf6,5d9347c6..a9005971--- a/docs/project-archive.md+++ b/docs/project-archive.md@@@ -169,10 -77,11 +169,12 @@@ when the chat starts, and each upload h- 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, and for every- chat whose touched files are uploaded).- app was closed (for the chat used most recently on each folder).++ app was closed (for the chat used most recently on each folder, and++ for every chat whose touched files are uploaded).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