A desktop coding agent with free access to frontier models.
TypeScript
feat(archive): upload the files the agent touches in a folder without git
ogsamratba85f16cd23b
diff --git a/apps/server/src/capture-client.test.ts b/apps/server/src/capture-client.test.tsindex 442f37e8..570ef0e5 100644--- a/apps/server/src/capture-client.test.ts+++ b/apps/server/src/capture-client.test.ts@@ -1,6 +1,7 @@import { afterEach, describe, expect, test } from "bun:test";import { execFile } from "node:child_process";-import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises";+import { randomBytes } from "node:crypto";+import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { promisify } from "node:util";@@ -8,6 +9,7 @@ import { zstdDecompressSync } from "node:zlib";import { startCaptureService, type CaptureService, type CaptureServiceOptions } from "./capture-client.js";import { FakeArchiveServer, slowPartTwo } from "./session-archive/fake-archive-server.js";+import { openArchive } from "./session-archive/test-helpers.js";/*** The capture worker: the collector's and the archiver's work runs off the@@ -324,6 +326,61 @@ describe("capture worker", () => {}}, 60_000);+ test("on the worker, the paths the collector sees a session touch reach the touched-files archive: a folder without .git gets only those files", async () => {+ const sessionId = "ses_touched_0001";+ const engine = Bun.serve({+ hostname: "127.0.0.1",+ port: 0,+ fetch: (request) => (new URL(request.url).pathname.endsWith("/message") ? Response.json([]) : Response.json({ id: sessionId })),+ });+ cleanups.push(() => engine.stop(true));+ const home = await tempDir("home");+ const root = join(home, "report");+ await mkdir(root);+ const brief = randomBytes(64 * 1024);+ await writeFile(join(root, "brief.pdf"), brief);+ await writeFile(join(root, "untouched.txt"), "never touched\n");+ const archive = new FakeArchiveServer();+ archive.policy = { touched_files: true };+ const stateDir = await tempDir("state");+ const capture = service({+ stateDir,+ collector: { upload: uploadSink().upload },+ archive: {+ enabled: true,+ excludedDirs: [],+ folderGate: { homeDir: home },+ request: (path, init) => archive.respond(`https://api.omnirush.test/omnirush/${path}`, {+ method: init.method,+ headers: { authorization: `Bearer ${archive.token}` },+ ...(init.body === undefined ? {} : { body: init.body }),+ }),+ refreshAccessToken: async () => null,+ fetch: archive.respond,+ baseIdleMs: 0,+ },+ });+ capture.startSession(sessionId, "workspace-touched", root);+ capture.archiveSessionStarted(sessionId, root, { baseUrl: `http://127.0.0.1:${engine.port}`, headers: [], search: "", engine: "v1" });+ await until(() => archive.calls.some((call) => call.path === "archives/key"), 20_000, "the key read");+ await capture.idle();+ expect(capture.mode()).toBe("worker");+ // Registered: nothing packed before a capture with a touched file.+ expect(archive.callPaths()).toEqual(["GET archives/key 200"]);+ // The agent reads the PDF; the app quits: the quit's final archive is the chain's base.+ capture.recordTrace(sessionId, "tool.read", { input: { filePath: join(root, "brief.pdf") } });+ await capture.idle();+ await capture.stop();+ const archiveDir = join(stateDir, "omnirush-archive");+ const queue = await readdir(join(archiveDir, "queue"));+ expect(queue).toHaveLength(1);+ const record = JSON.parse(await readFile(join(archiveDir, "queue", queue[0]!), "utf8"));+ expect(record.request).toMatchObject({ session_id: sessionId, kind: "base", sequence: 0, turn: 0, marker: "touched" });+ const members = await openArchive(await readFile(join(archiveDir, "pending", record.sealed_file)));+ expect(members.map((member) => member.name)).toEqual(["__omnirush__/manifest.json", "brief.pdf"]);+ expect(members[1]!.content.equals(brief)).toBe(true);+ }, 60_000);+test("OMNIRUSH_CAPTURE_WORKER=0 captures in-process with the same envelopes", async () => {const root = await syntheticWorkspace(20);const results: Array<Array<[string, string, number]>> = [];diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex cba76081..9862771f 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -93,6 +93,8 @@ export class CaptureHost {engineVersion: options.engineVersion,log: options.log,...(options.onSessionClosed ? { onSessionClosed: options.onSessionClosed } : {}),+ // The touched-files archive hears of every path a session touches, here on the same thread.+ onPathTouched: (sessionId, path) => this.archive.pathTouched(sessionId, path),});const { enabled, excludedDirs, folderGate, baseIdleMs, baseMaxDeferMs, ...auth } = options.archive;this.archive = new ProjectArchiveLifecycle({diff --git a/apps/server/src/collector-observer.test.ts b/apps/server/src/collector-observer.test.tsindex d67289d3..8ed8367f 100644--- a/apps/server/src/collector-observer.test.ts+++ b/apps/server/src/collector-observer.test.ts@@ -246,6 +246,10 @@ class RecordingArchiver implements ProjectArchiver {return [];}+ recordTouched(): void {}++ forgetTouched(): void {}+async drain(): Promise<DrainResult> {return { uploaded: 0, pending: 0, dropped: 0, blocked: null, disabled: false };}diff --git a/apps/server/src/session-archive/README.md b/apps/server/src/session-archive/README.mdindex 8000fb8a..d3955f00 100644--- a/apps/server/src/session-archive/README.md+++ b/apps/server/src/session-archive/README.md@@ -2,7 +2,9 @@The desktop side of the OmniRush project archive. When the folder an agentsession started in contains `.git`, the whole folder is uploaded (with the-all-folders policy on, any other folder too, see "All folders" below):+all-folders policy on, any other folder too, see "All folders" below; with+the touched-files policy on, any other folder gets only the files the agent+touched there, see "Touched files" below):- at session start: a **base** archive, including `.git/` and gitignored content;- after every completed turn that changed anything: a **delta** archive with the@@ -31,8 +33,9 @@ The user-facing description is `docs/project-archive.md`.| File | Responsibility || --- | --- |-| `detect.ts` | `isArchivableProject(root, detectors?, options?)`, `gitMarkerDetector` and `gitParentDetector` (section 4). A pluggable gate; the `.git` marker, in the root or (`git_parent`) in the nearest parent (a `.git` folder there must hold `HEAD`), never at any account's home on any volume or share, a filesystem, drive, share or mount root, or in a system or app directory (on a UNC share such as `\\wsl$\<distro>`, also `usr`, `etc`, `var`, `opt` and `root`). A `git_parent` root is archived alone, without the parent's `.git`. `folderDetector` / `refusedFolderRoot` for the all-folders policy (4.4) |-| `policy.ts` | `parseArchivePolicy`: `policy.all_folders` from the GET /archives/key body; anything but `true` is off |+| `detect.ts` | `isArchivableProject(root, detectors?, options?)`, `gitMarkerDetector` and `gitParentDetector` (section 4). A pluggable gate; the `.git` marker, in the root or (`git_parent`) in the nearest parent (a `.git` folder there must hold `HEAD`), never at any account's home on any volume or share, a filesystem, drive, share or mount root, or in a system or app directory (on a UNC share such as `\\wsl$\<distro>`, also `usr`, `etc`, `var`, `opt` and `root`). A `git_parent` root is archived alone, without the parent's `.git`. `folderDetector` / `folderRootRefusal` / `refusedFolderRoot` for the all-folders and touched-files policies (4.4): markers `folder` and `touched` |+| `policy.ts` | `parseArchivePolicy`: `policy.all_folders` and `policy.touched_files` from the GET /archives/key body; anything but `true` is off |+| `touched.ts` | Touched-files mode: `TouchedPathStore` (each session's touched paths, appended to `touched/<session>.jsonl`), `scanTouchedFiles` (pass 1 over those paths only: lstat from the root, never through a link out of it, a touched folder never expanded) and `touchedChange` (the delta and the next baseline) || `manifest.ts` | Scan with the exclusions and credential filter (5.2, 5.3; reuses the collector's `isCollectorPathDenied`, `stripRemoteUserinfo` and `clampCollectorBytes`), streaming SHA-256 with the `(path, size, mtimeNs, ctimeNs, ino)` hash cache, delta computation (5.8), `manifest.json` (5.6) and the git block (`path` from `git rev-parse --show-prefix`; git runs with `GIT_CEILING_DIRECTORIES` set to the real home so it never climbs into home) || `pack.ts` | Streaming pax tar writer (5.5) with unstable-entry detection (5.9), and the file -> tar -> zstd -> ORSEAL01 -> temp file pipeline (5.10, 13.1) || `seal.ts` | Streaming ORSEAL01 sealer and opener (section 6) over Node `crypto` |@@ -62,6 +65,8 @@ await archiver.captureBase(sessionId, root, turn?); // -> CaptureResultawait archiver.captureDelta(sessionId, root, turn); // -> CaptureResultawait archiver.captureFinal(sessionId, reason, {signal}); // -> CaptureResult (a final archive)await archiver.startFinalCandidates(); // -> session ids to check at app start+archiver.recordTouched(sessionId, path); // a path the session touched (touched-files mode)+archiver.forgetTouched(sessionId); // drop them (a child session)await archiver.drain(); // -> DrainResultawait archiver.signOut(); // drop everythingawait archiver.stop({finals?, budgetMs?}); // shutdown, final archives first@@ -74,8 +79,9 @@ and reported in the result.`CaptureResult` is `{status: "queued", archiveId, kind, sequence, size}` or`{status: "skipped", reason}`. The reasons of section 13.2 are-`not_archivable`, `disabled`, `exists`, `no_base`, `unchanged` and `stopped`.-This implementation adds five more:+`not_archivable`, `disabled`, `exists`, `no_base`, `unchanged` and `stopped`+(`unchanged` is also a touched-files session registered, or captured, with+no touched file yet: no base). This implementation adds five more:- `unavailable`: the key could not be fetched (offline, 5xx, 401/403). The session is remembered as base-pending, and the next `captureDelta` captures the base instead.- `stale_turn`: `turn` is not greater than the last archived turn. A turn delta must move the turn on (the server answers `archive_parent_mismatch` to a lower one). Nothing is consumed; the lifecycle then takes a final archive, so the changes are not held back until the next turn.@@ -121,6 +127,7 @@ in `captureServicesByServer` and calls:| The collector observer starts following a turn (on the worker): the first of an observation, or the next one after a request that came in while a turn settled | `turnFollowed(sessionId)`: the session's idle final archive is off until that turn ends || The collector observer's `turn_completed` (on the worker), with the engine messages it just read (null when it could not read them), however long the turn ran | `turnCompleted(sessionId, messages)` || The collector observer stops following a turn without seeing it settle: `session.observer_timeout` (its 24-hour safety bound) or `session.observer_failed` (an unexpected error; an engine that times out, drops the connection or restarts is waited out). A server stop ends an observation without this call | `turnIncomplete(sessionId)` |+| The collector's `onPathTouched` (on the worker, in `capture-host.ts`, so no message crosses to the main thread): a path in the root from a trace event (`recordTrace`, a child session's messages), a watcher event, the reconcile pass, the end-of-turn artifact scan, or a change capture's scan, before the collector's own denylist | `pathTouched(sessionId, path)`: passed to `SessionArchiver.recordTouched` for a session this run started that may be archived (resolved with a root, or not resolved yet); a child session's paths are dropped (`forgetTouched`) || `collector.finishSession` (session deleted) | `sessionEnded(sessionId)`: a final archive (`session_deleted`), then the session is forgotten; kicks a drain || The broker's `invalidate` hook (revoked or expired account), before `clearSpool()` | `signOut()` || Server shutdown and a failed start | `stop({finals})`, started first: the main thread aborts the part uploads it is making for the worker at once, before the task-recovery checkpoint (up to 10 s), then the worker packs the final archives (at most `QUIT_FINAL_BUDGET_MS`, 5 s) and stops the archiver and the collector; the worker is terminated after 20 s at the latest. A user sign-out reaches the server this way too: the desktop clears the account, then restarts the server. So `server.ts` asks the account store (`omnirushGatewayCredentials.latest()`, at most 1 s) whether the account is still there, and without it no final archive is packed |@@ -157,8 +164,8 @@ 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.-- **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 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.+- **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.@@ -297,7 +304,7 @@ against a loopback sink).## Behaviour notes- **Consent.** A 428, or a `503 archive_disabled`, from any route turns archiving off without error. Every queued archive is dropped (consent belongs to the user), sessions that lost a queued archive stop, and captures skip as `disabled` until a later `captureBase` finds the key route open again. A server without the routes (404 on `/archives/key`) counts as disabled.-- **Stopping a session.** A chain-ending 409, a 413 or a 422 drops that session's jobs and stops the session. A 7-day-old job does the same. The next session starts over with a new base. The exception is `archive_parent_mismatch` on a final archive before the server accepted one (see "Final archives").+- **Stopping a session.** A chain-ending 409, a 413 or a 422 drops that session's jobs and stops the session (one `warn` line; the code is `archive_marker_not_allowed` when the server says so, else `archive_request_invalid`). A 7-day-old job does the same. The next session starts over with a new base. The exception is `archive_parent_mismatch` on a final archive before the server accepted one (see "Final archives").- **`409 archive_kid_unknown`.** Section 7.9 says to refetch the key and seal again. The plaintext is not kept, so:- on a **base**, the job is dropped and the session goes back to base-pending: the next `captureDelta` packs a new base with the current key;- on a **delta**, the chain cannot be re-sealed, so the session stops.@@ -308,8 +315,14 @@ against a loopback sink).- `root_app_data`: the desktop's userData dir (`OMNIRUSH_DESKTOP_USER_DATA_DIR`, set by the desktop app) and anything inside or above it; an `AppData` or `Library/Application Support` folder anywhere; and, in home and in the account folders beside it, every folder whose name starts with `.`, `Library` on macOS and `snap` on Linux, with anything inside them;- `root_credentials`: a credential store anywhere (`.ssh`, `.aws`, `.gnupg`, `.kube`, `.docker`, `.azure`, `.password-store`, `Keychains`, `.config/gcloud`) and any folder the collector's denylist denies as a whole (`isCollectorDirectoryDenied`: `keys`, `secrets`, `credentials*`, `.env*`, `node_modules`, `.git`, key-store suffixes, ...), with anything inside them. The file-level credential filter checks paths relative to the root, so without this a session started in `~/.aws` would upload it;- `root_system`, outside home only: `/System`, `/Library`, `/Applications`, `/private`, `/usr`, `/bin`, `/sbin`, `/etc`, `/var`, `/opt`, `/cores`, `/proc`, `/sys`, `/dev`, `/boot`, `/lib`, `/lib64`, `/run`, `/root`, `/snap`, `/nix`; `Windows`, `Windows.old`, `Program Files`, `Program Files (x86)`, `ProgramData`, `$Recycle.Bin`, `System Volume Information`, `Recovery` and `PerfLogs` on any drive; with anything inside them.- - **The policy probe.** Only a root that passes every refusal asks for the policy (`allFoldersPolicy`). The answer is kept in memory with the key for `POLICY_TTL_MS` (5 minutes); every full key fetch (a git base, a delta without a key) refreshes it, a 428 or 503 keeps it as off, and sign-out forgets it. Without a kept answer the archiver sends one probe (`ArchiveUploader.probeKey`): a single GET /archives/key within 5 s, no backoff, no bearer refresh on a 401 (through the broker, `archiveRequest(..., { refresh: false })`), shared by the sessions that start meanwhile. Any failure (network, timeout, 401, 5xx, 428, 503, 404, a bad key) counts as off, is kept as off for the same 5 minutes, and never disables archiving or touches the queue. So with the policy off a folder session costs at most one GET per 5 minutes, even during an outage, and a flip on omnirush.ai reaches a running app within 5 minutes with no release.+ - **The policy probe.** Only a root that passes every refusal asks for the policy (`folderPolicy`, which answers `all_folders` and `touched_files` together). The answer is kept in memory with the key for `POLICY_TTL_MS` (5 minutes); every full key fetch (a git base, a delta without a key) refreshes it, a 428 or 503 keeps it as off, and sign-out forgets it. Without a kept answer the archiver sends one probe (`ArchiveUploader.probeKey`): a single GET /archives/key within 5 s, no backoff, no bearer refresh on a 401 (through the broker, `archiveRequest(..., { refresh: false })`), shared by the sessions that start meanwhile. Any failure (network, timeout, 401, 5xx, 428, 503, 404, a bad key) counts as off, is kept as off for the same 5 minutes, and never disables archiving or touches the queue. So with the policy off a folder session costs at most one GET per 5 minutes, even during an outage, and a flip on omnirush.ai reaches a running app within 5 minutes with no release.- **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 collector (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. `TouchedPathStore` keeps them per session: in memory until the gate has run (a git, folder or 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 not touched chains any more, 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). 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.+ - **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`.- **Part order.** Parts are uploaded one at a time, in order, streamed from the sealed file. The spec allows up to 4 in parallel; that is a possible later optimisation.- **Pass 2 never reads outside the root.** Node has no `openat`, so pass 2 cannot pin a directory handle. Instead it `lstat`s each directory once, parents first, when the walk enters it. If a directory is no longer a real directory (for example it was swapped for a symlink by `npm link` or a tool), nothing below it is read. Each file is opened with `O_NOFOLLOW | O_NONBLOCK | O_NOCTTY`, so a FIFO swapped in never blocks the capture. The handle's `fstat` must show a regular file with the `st_dev` and `st_ino` that pass 1 `lstat`ed, which catches a swap made after the directory check. Otherwise the member is zero-filled at its manifest size and listed in `unstable.json`, and the next delta sends it again. A changed size or mtime on the same inode still copies the bytes (5.9). This adds about 3% to pass 2 on a 56k-entry tree.- **Memory.** Pass 2 streams through a pool of four 1 MiB tar blocks, recycled once zstd has consumed them. The sealer holds one 1 MiB chunk, and the sink awaits each file write. The manifest is serialised on the fly: once to learn its size, once while it is written. Short-lived zstd and AES-GCM output buffers are the remaining churn. An upload holds the part it is sending (the server's part size, 64 MiB by default). In Bun, a full GC is requested every 64 MiB, every 10k files and around each capture (about 2 ms each at these heap sizes); elsewhere this is a no-op.@@ -345,6 +358,7 @@ cd apps/server && bun --conditions=development test src/session-archive- pax header edge cases;- a multi-MiB `writeSealedArchive` round trip checked with the `zstd` CLI, and a stopped writer rejecting.- `manifest.test.ts`: UTF-8 byte order; the credential filter; the exclusions (credential, FIFO, app state, `__omnirush__`, unreadable, non-UTF-8); the hash cache; a stopped scan reading nothing; delta for add, modify (content and mode), delete, rename, file to dir, dir to file and symlink retarget; unstable re-send; `.git` changes; manifest JSON, with a delta's `trigger` and a final archive's `reason`; the git block and its `path`; `git status` never rewriting `.git/index`.+- `touched.test.ts`: `scanTouchedFiles` with a binary, a PDF, an untouched file, a touched folder (never expanded), a credential file, `__omnirush__`, an app dir, a symlink inside (followed), links out of the root and a folder linked out of it (skipped; every path the scan looked at is inside the root), a missing file and a FIFO; the hash cache used and kept; plain relative paths only; `touchedChange` (added, modified, gone, unreadable kept); `TouchedPathStore` (paths in memory until tracked, the file read back by a new store, a torn line, forget, a session that is something else, clear).- `detect.test.ts`: a `.git` dir; a gitfile; no `.git`; an invalid gitfile; a `.git` symlink; home, `/` and app dirs refused; `root_not_directory`; detector plug-ins; `git_parent` (nearest parent wins, a `.git` folder without `HEAD` there qualifies nothing, root `.git` preferred, dotfiles home, system dirs, posix and `path.win32` walks including `/Volumes/<disk>` homes and WSL shares); the all-folders gate: policy off and on, git first, every refusal on macOS, Linux and Windows paths, credential and app-data folders anywhere and in other accounts, Windows device, long and trailing-dot paths, and credential folders on disk and through a link refused without asking the policy.- `upload.test.ts` (in-process fake API and S3):- the happy path;@@ -352,7 +366,8 @@ cd apps/server && bun --conditions=development test src/session-archive- a part S3 already holds;- an expired URL (403);- 428 and 503;- - the key route's policy: only `all_folders: true` turns it on, and the key works either way;+ - the key route's policy: only `all_folders: true` and `touched_files: true` turn them on, each on its own, and the key works either way;+ - `422 archive_marker_not_allowed` for a `touched` or `folder` create stops the session with that code; any other 422 is `archive_request_invalid`;- the policy probe: one attempt with no backoff and no refresh for 502, 500, 503, 429, 401 and a network error; `refresh: false` through the request hook;- a 401 with a refresh, and a failed refresh;- NoSuchUpload leading to a re-create;@@ -361,9 +376,10 @@ cd apps/server && bun --conditions=development test src/session-archive- replay, chain conflicts and an unknown kid;- backoff;- the broker request hook.-- `lifecycle.test.ts` (fake archiver and engine, plus runs on the real archiver): the turn count on v1 and v2 message shapes; the feature flag; one base per root session per app run at the real path; child sessions ignored; deltas numbered from the engine and ordered after the base, and after the last archived turn when the messages could not be read; restart resume; a session whose start could not read the engine resolved at its next completed turn (its delta kept, also on the real archiver across an app restart), and a child resolved that way still ignored; an unreadable start retried after 1, 2, 5 and 10 minutes, one retry at a time, and never once archiving stopped; signed-out start clearing; sign-out stopping queued steps; 428 off until the recheck; failures contained. Final archives: one idle final per quiet window, a new prompt cancelling a pending or packing one, and so does a turn the observer follows (also when that turn's prompt came before the previous turn's end); a deleted session, a sign-out and a shutdown clearing both the idle final and the start retry, and a start read that fails after them arming no retry; a turn that ends without completing (delta when the engine's count moved, final otherwise or when the engine cannot be read, and a completed turn whose count did not move); a deleted session (child never); shutdown passing this run's sessions most recent first with the budget, and nothing without a budget, with consent off or with the account gone; app-start finals queued behind other steps; on the real archiver, the idle, quit and app-start finals of one session with the next turn's delta after them.+- `lifecycle.test.ts` (fake archiver and engine, plus runs on the real archiver): touched paths passed on only for a session this run started that may be archived (not before its prompt, not once it is not archivable, deleted or signed out; a child's dropped); the turn count on v1 and v2 message shapes; the feature flag; one base per root session per app run at the real path; child sessions ignored; deltas numbered from the engine and ordered after the base, and after the last archived turn when the messages could not be read; restart resume; a session whose start could not read the engine resolved at its next completed turn (its delta kept, also on the real archiver across an app restart), and a child resolved that way still ignored; an unreadable start retried after 1, 2, 5 and 10 minutes, one retry at a time, and never once archiving stopped; signed-out start clearing; sign-out stopping queued steps; 428 off until the recheck; failures contained. Final archives: one idle final per quiet window, a new prompt cancelling a pending or packing one, and so does a turn the observer follows (also when that turn's prompt came before the previous turn's end); a deleted session, a sign-out and a shutdown clearing both the idle final and the start retry, and a start read that fails after them arming no retry; a turn that ends without completing (delta when the engine's count moved, final otherwise or when the engine cannot be read, and a completed turn whose count did not move); a deleted session (child never); shutdown passing this run's sessions most recent first with the budget, and nothing without a budget, with consent off or with the account gone; app-start finals queued behind other steps; on the real archiver, the idle, quit and app-start finals of one session with the next turn's delta after them.- `../project-archive.test.ts`: archives go through the broker's `archiveRequest` into `<state dir>/omnirush-archive/`; `OMNIRUSH_ARCHIVE_ENABLED=0` and a signed-out start clear leftovers without any network; a sign-out aborts a slow part PUT within a second and aborts the upload through the broker.-- `../capture-client.test.ts`: stopping the capture worker packs a final archive, and none when the account is gone.+- `../capture-client.test.ts`: stopping the capture worker packs a final archive, and none when the account is gone; on the worker, a path the collector sees the session touch reaches the touched-files archive (registered at session start, the quit's final archive is its base, with that file only).+- `../workspace-collector.test.ts`: every path a session touches inside the root reaches `onPathTouched` (a tool's absolute path, a credential name, a file the watcher sees land), none outside it.- `../collector-observer.test.ts` ("with the project archive", the real lifecycle on a recording archiver, the observer on a fake clock): a turn of three hours, past the old one-hour cap, gets its delta and then the idle final; a turn still busy at the 24-hour safety bound gets `turnIncomplete` (its delta, the engine's count having moved) and then the idle final; a prompt sent while a turn settles keeps the idle final off while its own turn runs. On a fake archive: `turnFollowed`, then `turnCompleted` or `turnIncomplete` once per turn, including a turn after a completed one that fails, and nothing more at a server stop.- `../omnirush-gateway-broker.test.ts`: `archiveRequest` URL, bearer, 401 refresh and path allowlist.- `../workspace-collector.server.e2e.test.ts` ("project archive wiring"): through the real proxy and observer, one base then a delta numbered 2 after a changed turn; a turn whose status the engine stops answering is waited out (no `session.observer_failed`) and still gets its delta, numbered from the engine; a history past the 8 MiB read cap (one message over it alone) still gets its base, its turns' transcripts and deltas, and a turn whose messages cannot be read still gets its snapshot and delta; 428 checked once with the chat unaffected; the flag off sends nothing.@@ -377,4 +393,5 @@ 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;+ - 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; 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.diff --git a/apps/server/src/session-archive/detect.test.ts b/apps/server/src/session-archive/detect.test.tsindex f3a4f87b..c6d54fca 100644--- a/apps/server/src/session-archive/detect.test.ts+++ b/apps/server/src/session-archive/detect.test.ts@@ -207,16 +207,16 @@ describe("all-folders gate (4.4)", () => {const NO_MARKER = { archivable: false, reason: "no_marker", marker: null };/** A policy stub that counts how often it was asked. */- function policy(allFolders: boolean) {+ function policy(allFolders: boolean, touchedFiles = false) {const asked = { count: 0 };- return { asked, allFolders: async () => (asked.count += 1, allFolders) };+ return { asked, read: async () => (asked.count += 1, { allFolders, touchedFiles }) };}test("policy off: a folder without .git stays not archivable, exactly as git-only", async () => {const home = await tempDir("folder-home");await mkdir(join(home, "notes"));const off = policy(false);- expect(await isArchivableProject(join(home, "notes"), [gitMarkerDetector, folderDetector(off.allFolders, { homeDir: home })])).toEqual(NO_MARKER);+ expect(await isArchivableProject(join(home, "notes"), [gitMarkerDetector, folderDetector(off.read, { homeDir: home })])).toEqual(NO_MARKER);expect(off.asked.count).toBe(1);});@@ -224,20 +224,20 @@ describe("all-folders gate (4.4)", () => {const home = await tempDir("folder-home");await mkdir(join(home, "projects/x"), { recursive: true });const on = policy(true);- const detectors = [gitMarkerDetector, folderDetector(on.allFolders, { homeDir: home })];+ const detectors = [gitMarkerDetector, folderDetector(on.read, { homeDir: home })];expect(await isArchivableProject(join(home, "projects/x"), detectors)).toEqual(FOLDER);expect(await isArchivableProject(join(home, "projects"), detectors)).toEqual(FOLDER);expect(on.asked.count).toBe(2);expect(await isArchivableProject(home, detectors, { homeDir: home })).toEqual({ archivable: false, reason: "root_too_broad", marker: null });// The folder detector refuses home by itself too, without asking.- expect(await folderDetector(on.allFolders, { homeDir: home })(home)).toBeNull();+ expect(await folderDetector(on.read, { homeDir: home })(home)).toBeNull();expect(on.asked.count).toBe(2);});test("git is still preferred, and any .git entry keeps the git result without asking the policy", async () => {const home = await tempDir("folder-home");const on = policy(true);- const detectors = [gitMarkerDetector, folderDetector(on.allFolders, { homeDir: home })];+ const detectors = [gitMarkerDetector, folderDetector(on.read, { homeDir: home })];await mkdir(join(home, "repo/.git"), { recursive: true });expect(await isArchivableProject(join(home, "repo"), detectors)).toEqual({ archivable: true, reason: "git_dir", marker: ".git" });await mkdir(join(home, "broken"));@@ -255,7 +255,7 @@ describe("all-folders gate (4.4)", () => {await mkdir(join(userData, "managed-opencode-workdir"), { recursive: true });await symlink("/usr", join(home, "system"));const on = policy(true);- const detector = folderDetector(on.allFolders, { homeDir: home, userDataDir: userData });+ const detector = folderDetector(on.read, { homeDir: home, userDataDir: userData });for (const root of [userData, join(userData, "managed-opencode-workdir"), join(home, ".config"), join(home, "system/share"), "/usr", "/usr/share"]) {expect({ root, result: await detector(root) }).toEqual({ root, result: null });}@@ -440,7 +440,7 @@ describe("all-folders gate (4.4)", () => {await mkdir(join(home, "work/links"), { recursive: true });await symlink(join(home, ".aws"), join(home, "work/links/aws"));const on = policy(true);- const detector = folderDetector(on.allFolders, { homeDir: home });+ const detector = folderDetector(on.read, { homeDir: home });// work/links/aws/sso is a real directory reached through a link: its resolved form is ~/.aws/sso.for (const root of [...roots.map((dir) => join(home, dir)), join(home, "work/links/aws/sso")]) {expect({ root, result: await detector(root) }).toEqual({ root, result: null });diff --git a/apps/server/src/session-archive/detect.ts b/apps/server/src/session-archive/detect.tsindex 43ffea21..e34f4851 100644--- a/apps/server/src/session-archive/detect.ts+++ b/apps/server/src/session-archive/detect.ts@@ -3,22 +3,23 @@* the agent started in is a project. v1 recognises one marker, a `.git`* entry, in the folder itself or in the nearest parent that may hold one* (a folder inside a repository); further markers plug in as detectors. The- * all-folders policy (4.4) adds one more, `folderDetector`: any other folder- * that is not too broad to be one project and is not, or is not inside, a- * credential, app-data or system location. Git roots never get those- * refusals.+ * all-folders and touched-files policies (4.4) add one more,+ * `folderDetector`: any other folder that is not too broad to be one project+ * and is not, or is not inside, a credential, app-data or system location.+ * Git roots never get those refusals.*/import { lstat, open, realpath } from "node:fs/promises";import { homedir } from "node:os";import path, { isAbsolute, join, parse, posix, relative, resolve, sep, win32 } from "node:path";import { isCollectorDirectoryDenied } from "../workspace-collector.js";+import type { ArchivePolicy } from "./policy.js";export type ArchivableProject = {archivable: boolean;- /** git_dir | git_file | git_parent | folder | no_marker | gitfile_invalid | root_not_directory | root_too_broad */+ /** git_dir | git_file | git_parent | folder | touched | no_marker | gitfile_invalid | root_not_directory | root_too_broad */reason: string;- /** The marker that qualified the root (".git", "folder"), null when not archivable. */+ /** The marker that qualified the root (".git", "folder", "touched"), null when not archivable. */marker: string | null;};@@ -228,10 +229,12 @@ export const gitParentDetector: ProjectMarkerDetector = async (root, options = {/** The gate's detectors: `.git` in the root first, then in the nearest parent. */export const defaultProjectDetectors: readonly ProjectMarkerDetector[] = [gitMarkerDetector, gitParentDetector];-// --- a folder without git (the all-folders policy) ------------------------------+// --- a folder without git (the all-folders and touched-files policies) ----------/** The marker of a folder archived by the all-folders policy (4.4); the server accepts it only while that policy is on. */export const FOLDER_MARKER = "folder";+/** The marker of the files the agent touched in a plain folder (touched-files policy); the server accepts it only while that policy is on. */+export const TOUCHED_MARKER = "touched";/** Top-level system and app directories of macOS and Linux (one list: the other system's names are absent). */const FOLDER_POSIX_SYSTEM_DIRS = [@@ -341,25 +344,38 @@ export type FolderGateOptions = {};/**- * The all-folders marker (4.4): a root with no `.git` entry at all is a- * project, marker `folder`, when refusedFolderRoot accepts it in every- * form (as given and resolved through symlinks) and `allFolders()` says- * the policy is on. The policy is asked last, only for such a root. Never- * throws.+ * Why `root` may not be archived as a plain folder, in any form of it (as+ * given and resolved through symlinks), or null when it may: the folder+ * refusals shared by the all-folders and the touched-files policies.*/-export function folderDetector(allFolders: () => Promise<boolean>, options: FolderGateOptions = {}): ProjectMarkerDetector {+export async function folderRootRefusal(root: string, options: FolderGateOptions = {}): Promise<FolderRefusal | null> {+ const context: FolderGateContext = {+ platform: process.platform,+ homes: await pathForms(options.homeDir ?? homedir()),+ userData: options.userDataDir ? await pathForms(options.userDataDir) : [],+ };+ for (const form of await pathForms(root)) {+ const refused = refusedFolderRoot(form, context);+ if (refused) return refused;+ }+ return null;+}++/**+ * The folder markers (4.4): a root with no `.git` entry at all, that+ * folderRootRefusal accepts, is a project with marker `folder` while+ * `policy()` has `allFolders` on, or else with marker `touched` (only the+ * files the agent touches there) while it has `touchedFiles` on. The policy+ * is asked last, only for such a root. Never throws.+ */+export function folderDetector(policy: () => Promise<ArchivePolicy>, options: FolderGateOptions = {}): ProjectMarkerDetector {return async (root) => {try {if (await lstat(join(root, ".git")).then(() => true, () => false)) return null;- const context: FolderGateContext = {- platform: process.platform,- homes: await pathForms(options.homeDir ?? homedir()),- userData: options.userDataDir ? await pathForms(options.userDataDir) : [],- };- for (const form of await pathForms(root)) {- if (refusedFolderRoot(form, context)) return null;- }- return (await allFolders()) ? { archivable: true, reason: "folder", marker: FOLDER_MARKER } : null;+ if (await folderRootRefusal(root, options)) return null;+ const answer = await policy();+ if (answer.allFolders) return { archivable: true, reason: "folder", marker: FOLDER_MARKER };+ return answer.touchedFiles ? { archivable: true, reason: "touched", marker: TOUCHED_MARKER } : null;} catch {return null;}diff --git a/apps/server/src/session-archive/fake-archive-server.ts b/apps/server/src/session-archive/fake-archive-server.tsindex 081d8736..6a4ec86a 100644--- a/apps/server/src/session-archive/fake-archive-server.ts+++ b/apps/server/src/session-archive/fake-archive-server.ts@@ -5,7 +5,8 @@* consent, replay and restart semantics, presigned URL expiry, NoSuchUpload,* part listing and completion checks. A delta's turn may repeat its parent's* (a final archive), as the backend accepts from 1.0.11 on; `strictTurns`- * emulates the backend before that.+ * emulates the backend before that. A `folder` or `touched` archive is+ * refused (422 archive_marker_not_allowed) unless `policy` has its flag on.*/import { createHash, randomUUID } from "node:crypto";@@ -149,6 +150,10 @@ export class FakeArchiveServer {return json(422, { detail: [] });}if (kind === "base" && (sequence !== 0 || parentId !== null)) return json(422, { detail: [] });+ // A folder or touched-files archive only while the key route's policy has its flag on (backend spec 4.4).+ const flag = request.marker === "folder" ? "all_folders" : request.marker === "touched" ? "touched_files" : null;+ const policy = new Map(typeof this.policy === "object" && this.policy !== null ? Object.entries(this.policy) : []);+ if (flag && policy.get(flag) !== true) return json(422, { detail: "archive_marker_not_allowed" });if (kid !== testKeys.kid) return json(409, { detail: "archive_kid_unknown" });const existing = this.archives.get(archiveId);if (existing) {diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex 805d433d..d410f719 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -1,7 +1,7 @@import { afterEach, describe, expect, test } from "bun:test";import { execFile } from "node:child_process";import { createHash, randomBytes } from "node:crypto";-import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";+import { mkdir, readdir, readFile, rm, symlink, writeFile } from "node:fs/promises";import { basename, join } from "node:path";import { promisify } from "node:util";@@ -870,3 +870,244 @@ describe("SessionArchiver final archives", () => {expect(server.objects()).toHaveLength(1);});});++describe("SessionArchiver touched files", () => {+ const TOUCHED_ON = { touched_files: true };++ /** A folder without .git in a home folder of its own, and the archiver whose folder gate knows that home. */+ async function touchedSetup(options: Partial<SessionArchiverOptions> & { policy?: unknown } = {}) {+ const server = new FakeArchiveServer();+ server.policy = "policy" in options ? options.policy : TOUCHED_ON;+ const home = await tempDir("home");+ const root = join(home, "report");+ await mkdir(root);+ const state = options.stateDir ?? (await tempDir("state"));+ const logs: Array<{ level: string; message: string; attributes?: Record<string, unknown> }> = [];+ const make = (extra: Partial<SessionArchiverOptions> = {}) => archiver(server, state, {+ folderGate: { homeDir: home },+ touchedFlushMs: 5,+ log: (level, message, attributes) => logs.push({ level, message, ...(attributes ? { attributes } : {}) }),+ ...options,+ ...extra,+ });+ return { server, home, root, state, logs, make, subject: make() };+ }++ const names = async (object: { object: Buffer | null }) => (await openArchive(object.object!)).map((member) => member.name);++ test("a folder without .git: only the files the agent touched, byte for byte; untouched files, credentials and links out of the folder never", async () => {+ const { server, root, subject } = await touchedSetup();+ const outside = await tempDir("outside");+ await writeFile(join(outside, "secret.txt"), "OUTSIDE_MARKER");+ const brief = randomBytes(200 * 1024);+ await writeFile(join(root, "brief.pdf"), brief);+ await writeFile(join(root, "notes.txt"), "never touched\n");+ await writeFile(join(root, ".env"), "API_KEY=sk-live-1234567890\n");+ await symlink(join(outside, "secret.txt"), join(root, "link-out"));+ const id = "ses_touched_files";++ // Session start registers the session; nothing is packed yet.+ expect(await subject.captureBase(id, root)).toEqual({ status: "skipped", reason: "unchanged" });+ expect(server.callPaths()).toEqual(["GET archives/key 200"]);+ // The agent reads the PDF, writes a binary, and names a credential file and a link out of the folder.+ subject.recordTouched(id, "brief.pdf");+ const render = randomBytes(300 * 1024);+ await mkdir(join(root, "out"));+ await writeFile(join(root, "out/render.png"), render);+ subject.recordTouched(id, "out/render.png");+ subject.recordTouched(id, "out");+ subject.recordTouched(id, ".env");+ subject.recordTouched(id, "link-out");++ expect(await subject.captureDelta(id, root, 1)).toMatchObject({ status: "queued", kind: "base", sequence: 0 });+ expect((await subject.drain()).uploaded).toBe(1);+ const [base] = server.objects();+ expect(base!.request).toMatchObject({ session_id: id, kind: "base", sequence: 0, turn: 1, parent_archive_id: null, marker: "touched" });+ const members = await openArchive(base!.object!);+ expect(members.map((member) => member.name)).toEqual(["__omnirush__/manifest.json", "brief.pdf", "out/render.png"]);+ expect(members[1]!.content.equals(brief)).toBe(true);+ expect(members[2]!.content.equals(render)).toBe(true);+ const manifest = manifestOf(members);+ expect(manifest).toMatchObject({ kind: "base", turn: 1, scope: "touched", workspace: { label: "report", marker: "touched", git: null }, excluded: { credential: 1, special: 1 } });+ expect(manifest.files).toEqual([+ { path: "brief.pdf", type: "file", mode: expect.any(Number), size: brief.length, sha256: sha256(brief) },+ { path: "out/render.png", type: "file", mode: expect.any(Number), size: render.length, sha256: sha256(render) },+ ]);+ expect("deleted" in manifest).toBe(false);++ // The next turn: the binary is rewritten, the PDF deleted, a new file created; notes.txt is still untouched.+ const rerender = randomBytes(1024);+ await writeFile(join(root, "out/render.png"), rerender);+ await rm(join(root, "brief.pdf"));+ await writeFile(join(root, "summary.md"), "# summary\n");+ subject.recordTouched(id, "summary.md");+ subject.recordTouched(id, "brief.pdf");+ expect(await subject.captureDelta(id, root, 2)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });+ expect((await subject.drain()).uploaded).toBe(1);+ const delta = server.objects()[1]!;+ expect(delta.request).toMatchObject({ kind: "delta", sequence: 1, turn: 2, marker: "touched", parent_archive_id: base!.request.archive_id });+ const deltaMembers = await openArchive(delta.object!);+ expect(deltaMembers.slice(1).map((member) => member.name)).toEqual(["out/render.png", "summary.md"]);+ expect(deltaMembers[1]!.content.equals(rerender)).toBe(true);+ expect(manifestOf(deltaMembers)).toMatchObject({ kind: "delta", trigger: "turn", scope: "touched", deleted: ["brief.pdf"] });++ // Nothing touched changed: nothing is sent, even though an untouched file did.+ await writeFile(join(root, "notes.txt"), "edited by the user, never touched by the agent\n");+ expect(await subject.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "unchanged" });+ for (const object of server.objects()) {+ expect(await names(object)).not.toContain("notes.txt");+ expect(object.object!.includes(Buffer.from("OUTSIDE_MARKER"))).toBe(false);+ }+ });++ test("a git folder is archived as today with touched_files on, and keeps no touched paths", async () => {+ const { server, state, make } = await touchedSetup();+ const { root } = await project();+ const subject = make();+ expect(await subject.captureBase("ses_git_touched", root)).toMatchObject({ status: "queued", kind: "base" });+ subject.recordTouched("ses_git_touched", "src/app.ts");+ await subject.drain();+ expect(server.objects()[0]!.request.marker).toBe(".git");+ expect(await names(server.objects()[0]!)).toEqual(expect.arrayContaining([".git/HEAD", "assets/scene.blend", "src/app.ts"]));+ expect(manifestOf(await openArchive(server.objects()[0]!.object!))).not.toHaveProperty("scope");+ await subject.stop();+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).toEqual([]);+ });++ test("all_folders wins over touched_files: the whole folder", async () => {+ const { server, root, subject } = await touchedSetup({ policy: { all_folders: true, touched_files: true } });+ await writeFile(join(root, "a.txt"), "a\n");+ expect(await subject.captureBase("ses_all_and_touched", root)).toMatchObject({ status: "queued", kind: "base" });+ await subject.drain();+ expect(server.objects()[0]!.request.marker).toBe("folder");+ });++ test("with the policy off or absent nothing is uploaded or kept, and the refused folders never are with it on", async () => {+ for (const policy of [undefined, { touched_files: false }, { touched_files: "true" }, { all_folders: false }]) {+ const { server, root, state, subject } = await touchedSetup({ policy });+ await writeFile(join(root, "brief.pdf"), randomBytes(1024));+ expect(await subject.captureBase("ses_touched_off", root)).toEqual({ status: "skipped", reason: "not_archivable" });+ subject.recordTouched("ses_touched_off", "brief.pdf");+ expect(await subject.captureDelta("ses_touched_off", root, 1)).toEqual({ status: "skipped", reason: "no_base" });+ expect(await subject.captureFinal("ses_touched_off", "app_quit")).toEqual({ status: "skipped", reason: "no_base" });+ await subject.stop();+ expect(server.callPaths()).toEqual(["GET archives/key 200"]);+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).toEqual([]);+ }++ // The folder refusals: home, a credential folder, the userData dir, a system folder, and a folder inside the app's own state.+ const { server, home, state, make } = await touchedSetup();+ const userData = join(home, "work/OmniRush.ai");+ for (const dir of [".ssh", "work/OmniRush.ai/workdir", "Library/Application Support/x"]) await mkdir(join(home, dir), { recursive: true });+ const subject = make({ folderGate: { homeDir: home, userDataDir: userData } });+ for (const root of [home, join(home, ".ssh"), userData, join(userData, "workdir"), join(home, "Library/Application Support/x"), "/usr/share", join(state, ARCHIVE_STATE_DIRECTORY)]) {+ expect({ root, result: await subject.captureBase("ses_touched_refused", root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });+ }+ expect(server.calls).toEqual([]);+ });++ test("finals and app start for touched chains, and the chain and its touched paths survive a restart", async () => {+ let now = Date.parse("2026-09-23T10:00:00Z");+ const { server, root, state, make } = await touchedSetup({ now: () => new Date(now) });+ const first = make();+ const id = "ses_touched_final";+ const unbased = "ses_touched_nobase";+ expect(await first.captureBase(id, root, 4)).toEqual({ status: "skipped", reason: "unchanged" });+ // A final archive with nothing touched yet: nothing, and still no base.+ expect(await first.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "unchanged" });+ await writeFile(join(root, "draft.md"), "draft one\n");+ first.recordTouched(id, "draft.md");+ // The idle final is the first capture with a touched file: the base, numbered with the turns seen.+ expect(await first.captureFinal(id, "idle")).toMatchObject({ status: "queued", kind: "base", sequence: 0 });+ await writeFile(join(root, "chart.svg"), "<svg/>\n");+ first.recordTouched(id, "chart.svg");+ expect(await first.captureFinal(id, "app_quit")).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });+ // Another session on the folder touches a file, and the app quits before any capture of it.+ now += 1_000;+ expect(await first.captureBase(unbased, root, 0)).toEqual({ status: "skipped", reason: "unchanged" });+ await writeFile(join(root, "table.csv"), "a,b\n");+ first.recordTouched(unbased, "table.csv");+ await first.stop();++ // Edited while the app was closed.+ await writeFile(join(root, "draft.md"), "draft two, while the app was closed\n");+ now += 60_000;+ const second = make();+ expect(await second.startFinalCandidates()).toEqual([unbased, id]);+ expect(await second.captureFinal(unbased, "app_start")).toMatchObject({ status: "queued", kind: "base", sequence: 0 });+ expect(await second.captureFinal(id, "app_start")).toMatchObject({ status: "queued", kind: "delta", sequence: 2 });+ // The touched paths from before the restart still count: the next turn's edit of draft.md goes out.+ await writeFile(join(root, "draft.md"), "draft three\n");+ expect(await second.captureDelta(id, root, 5)).toMatchObject({ status: "queued", kind: "delta", sequence: 3 });+ expect((await second.drain()).uploaded).toBe(5);++ const chain = server.objects().filter((object) => object.request.session_id === id);+ expect(chain.map((object) => [object.request.kind, object.request.sequence, object.request.turn, object.request.marker])).toEqual([+ ["base", 0, 4, "touched"], ["delta", 1, 4, "touched"], ["delta", 2, 4, "touched"], ["delta", 3, 5, "touched"],+ ]);+ const manifests = await Promise.all(chain.map(async (object) => manifestOf(await openArchive(object.object!))));+ expect(manifests.map((manifest) => [manifest.trigger, manifest.reason, manifest.scope])).toEqual([+ [undefined, undefined, "touched"], ["final", "app_quit", "touched"], ["final", "app_start", "touched"], ["turn", undefined, "touched"],+ ]);+ expect(await Promise.all(chain.map(names))).toEqual([+ ["__omnirush__/manifest.json", "draft.md"], ["__omnirush__/manifest.json", "chart.svg"], ["__omnirush__/manifest.json", "draft.md"], ["__omnirush__/manifest.json", "draft.md"],+ ]);+ const other = server.objects().find((object) => object.request.session_id === unbased)!;+ expect(other.request).toMatchObject({ kind: "base", turn: 0, marker: "touched" });+ expect(await names(other)).toEqual(["__omnirush__/manifest.json", "table.csv"]);++ // Deleted in the app: its final, then its touched paths are gone.+ await writeFile(join(root, "table.csv"), "a,b\n1,2\n");+ expect(await second.captureFinal(unbased, "session_deleted")).toMatchObject({ status: "queued", kind: "delta" });+ expect((await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).length).toBe(1);+ });++ test("a touched chain pauses while its policy is off and catches up once it is on again", async () => {+ let now = Date.parse("2026-09-23T10:00:00Z");+ const { server, root, subject } = await touchedSetup({ now: () => new Date(now) });+ const id = "ses_touched_pause";+ await subject.captureBase(id, root, 0);+ await writeFile(join(root, "a.bin"), randomBytes(512));+ subject.recordTouched(id, "a.bin");+ expect(await subject.captureDelta(id, root, 1)).toMatchObject({ status: "queued", kind: "base" });+ server.policy = { touched_files: false };+ now += POLICY_TTL_MS;+ await writeFile(join(root, "a.bin"), randomBytes(512));+ expect(await subject.captureDelta(id, root, 2)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureFinal(id, "idle")).toEqual({ status: "skipped", reason: "not_archivable" });+ server.policy = TOUCHED_ON;+ now += POLICY_TTL_MS;+ expect(await subject.captureDelta(id, root, 3)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });+ expect((await subject.drain()).uploaded).toBe(2);+ });++ test("422 archive_marker_not_allowed drops the session's chain once, with no retry, and no other touched chain starts until the policy is asked again", async () => {+ let now = Date.parse("2026-09-23T10:00:00Z");+ const { server, root, logs, subject } = await touchedSetup({ now: () => new Date(now) });+ const id = "ses_touched_refuse";+ await subject.captureBase(id, root, 0);+ await writeFile(join(root, "a.bin"), randomBytes(512));+ subject.recordTouched(id, "a.bin");+ expect(await subject.captureDelta(id, root, 1)).toMatchObject({ status: "queued", kind: "base" });+ await writeFile(join(root, "a.bin"), randomBytes(512));+ expect(await subject.captureDelta(id, root, 2)).toMatchObject({ status: "queued", kind: "delta" });+ // The server turned the policy off after the key was read: it refuses the create.+ server.policy = { touched_files: false };+ expect(await subject.drain()).toMatchObject({ uploaded: 0, dropped: 2, pending: 0 });+ expect(server.calls.filter((call) => call.method === "POST")).toEqual([expect.objectContaining({ path: "archives", status: 422 })]);+ expect(logs.filter((log) => log.message === "OmniRush archiving stopped for a session")).toEqual([+ { level: "warn", message: "OmniRush archiving stopped for a session", attributes: { sessionId: id, code: "archive_marker_not_allowed", droppedJobs: 2 } },+ ]);+ await writeFile(join(root, "a.bin"), randomBytes(512));+ expect(await subject.captureDelta(id, root, 3)).toEqual({ status: "skipped", reason: "stopped" });+ expect(await subject.drain()).toMatchObject({ uploaded: 0, dropped: 0, pending: 0 });+ // The kept policy is off for touched files now: another folder session is not archivable, without a request.+ const calls = server.calls.length;+ expect(await subject.captureBase("ses_touched_other", root, 0)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(server.calls.length).toBe(calls);+ now += POLICY_TTL_MS;+ expect(await subject.captureBase("ses_touched_later", root, 0)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(server.callPaths().at(-1)).toBe("GET archives/key 200");+ });+});diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 01dd4faa..f7fc036e 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -4,9 +4,12 @@* policy on, any folder detect.ts accepts) gets a base archive of the* whole folder at session start, a delta after every completed turn that* changed anything, and a final delta (the last turn's number again) when the- * folder changed after that turn; each is sealed to the omnirush.ai archive- * key, queued durably under the collector state dir and uploaded to S3- * through presigned multipart URLs. The embedded server drives it through+ * folder changed after that turn. Any other folder detect.ts accepts gets,+ * with the touched-files policy on, the same chain holding only the files+ * the agent touched there (touched.ts), its base at the first capture that+ * has one. Each archive is sealed to the omnirush.ai archive key, queued+ * durably under the collector state dir and uploaded to S3 through+ * presigned multipart URLs. The embedded server drives it through* lifecycle.ts; see README.md.*/import { randomBytes, randomUUID } from "node:crypto";@@ -15,7 +18,16 @@ import { join, resolve } from "node:path";import { z } from "zod";import { externalFetch } from "../server-fetch.js";-import { defaultProjectDetectors, FOLDER_MARKER, folderDetector, isArchivableProject, type FolderGateOptions, type ProjectMarkerDetector } from "./detect.js";+import {+ defaultProjectDetectors,+ FOLDER_MARKER,+ folderDetector,+ folderRootRefusal,+ isArchivableProject,+ TOUCHED_MARKER,+ type FolderGateOptions,+ type ProjectMarkerDetector,+} from "./detect.js";import { hintGarbageCollection, readJsonFile, stateKey, writeChunksAtomic, writeJsonAtomic } from "./files.js";import {ArchiveHashCache,@@ -27,14 +39,20 @@ import {parseBaselineText,readArchiveGit,scanArchiveTree,+ type ArchiveEntry,+ type ArchiveGit,type ArchiveKind,type ArchiveTrigger,+ type ExcludedCounts,type FinalReason,type ScannedEntry,} from "./manifest.js";import { writeSealedArchive } from "./pack.js";+import { POLICY_OFF, type ArchivePolicy } from "./policy.js";import { SEAL_CONTENT } from "./seal.js";+import { scanTouchedFiles, touchedChange, TouchedPathStore } from "./touched.js";import {+ ARCHIVE_MARKER_NOT_ALLOWED,ArchiveUploader,DEFAULT_RETRY_POLICY,type ArchiveApiRequest,@@ -66,9 +84,10 @@ 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;/**- * How long an all-folders policy answer (4.4) is reused for folders without- * `.git`: sessions started meanwhile send no probe, and a policy flipped on- * omnirush.ai reaches a running app within this time.+ * How long a folder policy answer (4.4: all folders, touched files) is+ * reused for folders without `.git`: sessions started meanwhile send no+ * probe, and a policy flipped on omnirush.ai reaches a running app within+ * this time.*/export const POLICY_TTL_MS = 5 * 60_000;@@ -89,7 +108,7 @@ export type SessionArchiverOptions = {/** Default false: the credential filter of section 5.3 applies. */archiveIncludeCredentialFiles?: boolean;detectors?: ProjectMarkerDetector[];- /** Where the all-folders policy (4.4) may not archive a folder without `.git`: the userData dir (and, in tests, home). */+ /** Where the all-folders and touched-files policies (4.4) may not archive a folder without `.git`: the userData dir (and, in tests, home). */folderGate?: FolderGateOptions;log?: ArchiveLog;/** Tests. */@@ -98,6 +117,8 @@ export type SessionArchiverOptions = {random?: { uuid(): string; bytes(n: number): Buffer };/** Tests: backoff timings of the upload client. */retry?: Partial<RetryPolicy>;+ /** Tests: how soon reported touched paths are written (2 s). */+ touchedFlushMs?: number;};export type CaptureSkipReason =@@ -163,6 +184,8 @@ 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(),});type SessionState = z.infer<typeof sessionStateSchema>;@@ -221,9 +244,16 @@ function errorSummary(error: unknown): string {return "unknown error";}+/** Whether `policy` lets a chain with this marker capture: a plain folder needs all_folders, touched files touched_files, git nothing. */+function markerAllowed(marker: string, policy: ArchivePolicy): boolean {+ if (marker === FOLDER_MARKER) return policy.allFolders;+ if (marker === TOUCHED_MARKER) return policy.touchedFiles;+ return true;+}+/** How a capture was asked for. */type CaptureOptions = {- /** Deltas only: a completed turn, or a final archive with its reason. */+ /** Deltas: a completed turn, or a final archive with its reason (a touched-files base a final archive captures says "final" too, without it in its manifest). */trigger?: ArchiveTrigger;reason?: FinalReason;/** A final archive of a session deleted in the app: its record is marked ended. */@@ -234,7 +264,7 @@ type CaptureOptions = {export class SessionArchiver {private readonly dir: string;- private readonly dirs: { sessions: string; baselines: string; hashCache: string; queue: string; pending: string; tmp: string };+ private readonly dirs: { sessions: string; baselines: string; hashCache: string; queue: string; pending: string; tmp: string; touched: string };private readonly uploader: ArchiveUploader;private readonly detectors: readonly ProjectMarkerDetector[];private readonly folderGate: FolderGateOptions | undefined;@@ -247,8 +277,8 @@ export class SessionArchiver {private started: Promise<void> | null = null;private key: ArchiveKey | null = null;private disabled: string | null = null;- /** The last all-folders policy answer, kept with the key for POLICY_TTL_MS; a failed probe is kept as off. */- private policy: { allFolders: boolean; at: number } | null = null;+ /** The last folder policy answer, kept with the key for POLICY_TTL_MS; a failed probe is kept as off. */+ private policy: { value: ArchivePolicy; at: number } | null = null;/** The probe in flight, shared by the sessions that start meanwhile. */private policyProbe: Promise<KeyResult> | null = null;private readonly sessionTails = new Map<string, Promise<void>>();@@ -266,6 +296,8 @@ export class SessionArchiver {private finalsAccepted = false;/** The server refused a final archive (a backend without them): none is captured until the app restarts. */private finalsRefused = false;+ /** The paths each touched-files session touched (touched.ts). */+ private readonly touched: TouchedPathStore;constructor(options: SessionArchiverOptions) {const stateDir = resolve(options.stateDir);@@ -277,6 +309,7 @@ export class SessionArchiver {queue: join(this.dir, "queue"),pending: join(this.dir, "pending"),tmp: join(this.dir, "tmp"),+ touched: join(this.dir, "touched"),};this.log = options.log ?? (() => undefined);this.now = options.now ?? (() => new Date());@@ -299,6 +332,16 @@ export class SessionArchiver {this.folderGate = options.folderGate;this.appDirs = [stateDir, ...(options.excludedDirs ?? []).map((dir) => resolve(dir))];this.includeCredentials = options.archiveIncludeCredentialFiles === true;+ this.touched = new TouchedPathStore(this.dirs.touched, {+ ready: () => this.start(),+ modeOf: async (sessionId) => {+ const state = await this.loadSession(sessionId);+ if (!state) return "unknown";+ return state.marker === TOUCHED_MARKER && !state.stopped && !state.ended ? "tracked" : "ignored";+ },+ log: this.log,+ ...(options.touchedFlushMs !== undefined ? { flushMs: options.touchedFlushMs } : {}),+ });}// --- public API -------------------------------------------------------------------@@ -321,8 +364,10 @@ export class SessionArchiver {* 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 policy is on (allFoldersPolicy); with it off- * nothing is written and only that policy is read.+ * 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.*/captureBase(sessionId: string, root: string, turn = 0): Promise<CaptureResult> {return this.guard("base", sessionId, turn, async () => this.withSession(sessionId, async () => {@@ -330,17 +375,18 @@ export class SessionArchiver {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" };- return this.captureBaseLocked(sessionId, resolve(root), turn, generation);+ return this.captureBaseLocked(sessionId, resolve(root), turn, generation, true);}));}/*** Delta after a completed turn. No-op without a base, when the session* stopped archiving, when nothing in the folder changed, or for a plain- * folder (marker `folder`) while the all-folders policy is off. 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.+ * 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.*/captureDelta(sessionId: string, root: string, turn: number | null): Promise<CaptureResult> {return this.guard("delta", sessionId, turn ?? 0, async () => this.withSession(sessionId, async () => {@@ -350,14 +396,17 @@ export class SessionArchiver {if (!state) return { status: "skipped", reason: "no_base" };if (state.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };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 deltas pause while the all-folders policy is off; the next one after it is on again catches up.- if (state.marker === FOLDER_MARKER && !(await this.allFoldersPolicy(generation)).allFolders) return { status: "skipped", reason: "not_archivable" };+ // 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" };+ // 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" });}));}@@ -368,48 +417,81 @@ export class SessionArchiver {* the app quits or starts again). A delta like any other, numbered with* the last archived turn again, whose manifest says `"trigger": "final"`* and why. Only for a session with a base, whose root the gate still- * accepts; nothing when the folder did not change. `signal` cancels it- * until its commit.+ * accepts (a plain folder or touched files: the folder refusals, and their+ * policy is on); nothing when the folder did not change. A touched-files+ * session without a base gets its base here once it has a touched file,+ * numbered with the completed turns last seen. `signal` cancels it until+ * its commit.*/captureFinal(sessionId: string, reason: FinalReason, options: { signal?: AbortSignal } = {}): Promise<CaptureResult> {const { signal } = options;+ const ended = reason === "session_deleted";return this.guard("delta", sessionId, 0, async () => this.withSession(sessionId, async () => {- const generation = this.generation;- if (signal?.aborted) return { status: "skipped", reason: "cancelled" };- if (this.disabled || !this.uploader.configured) return { status: "skipped", reason: "disabled" };- const state = await this.loadSession(sessionId);- if (!state || state.next_sequence === 0 || state.last_turn === null) return { status: "skipped", reason: "no_base" };- if (state.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };- const ended = reason === "session_deleted";- const skip = async (skipReason: CaptureSkipReason): Promise<CaptureResult> => {- if (ended && generation === this.generation) await this.saveSession({ ...state, final_due: false, ended: this.now().toISOString(), updated_at: this.now().toISOString() });- return { status: "skipped", reason: skipReason };- };- if (this.finalsRefused) return skip("unsupported");- // The folder may be gone, or no longer what the start-time gate accepted (a plain folder: the all-folders policy is still on).- const detectors = state.marker === FOLDER_MARKER ? [...this.detectors, this.folderDetector(generation)] : this.detectors;- const gate = await isArchivableProject(state.root, detectors, { appDirs: this.appDirs });- if (!gate.archivable) return skip("not_archivable");- const key = await this.currentKey(signal);- if (signal?.aborted) return { status: "skipped", reason: "cancelled" };- if (key === "disabled") return { status: "skipped", reason: "disabled" };- if (key === "unavailable") return { status: "skipped", reason: "unavailable" };- try {- return await this.captureArchive(state, "delta", state.last_turn, key, generation, { trigger: "final", reason, ended, ...(signal ? { signal } : {}) });- } catch (error) {- if (signal?.aborted) return { status: "skipped", reason: "cancelled" };- throw error;+ const result = await this.captureFinalLocked(sessionId, reason, ended, signal);+ if (ended) {+ // A deleted session keeps no touched paths once its record says so (or there is none).+ const after = await this.loadSession(sessionId);+ if (!after || after.ended) await this.touched.forget(sessionId);}+ return result;}));}+ private async captureFinalLocked(sessionId: string, reason: FinalReason, ended: boolean, signal?: AbortSignal): Promise<CaptureResult> {+ const generation = this.generation;+ if (signal?.aborted) return { status: "skipped", reason: "cancelled" };+ if (this.disabled || !this.uploader.configured) return { status: "skipped", reason: "disabled" };+ const state = await this.loadSession(sessionId);+ const unbased = state?.next_sequence === 0 && state.marker === TOUCHED_MARKER;+ const turn = unbased ? state.turn_seen ?? 0 : state && state.next_sequence > 0 ? state.last_turn : null;+ if (!state || turn === null) return { status: "skipped", reason: "no_base" };+ if (state.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };+ const skip = async (skipReason: CaptureSkipReason): Promise<CaptureResult> => {+ if (ended && generation === this.generation) await this.saveSession({ ...state, final_due: false, ended: this.now().toISOString(), updated_at: this.now().toISOString() });+ return { status: "skipped", reason: skipReason };+ };+ // A base repeats no turn: a backend without final archives takes it.+ if (this.finalsRefused && !unbased) return skip("unsupported");+ // The folder may be gone, or no longer what the start-time gate accepted.+ if (!(await this.chainAllowed(state, generation))) return skip("not_archivable");+ const key = await this.currentKey(signal);+ if (signal?.aborted) return { status: "skipped", reason: "cancelled" };+ if (key === "disabled") return { status: "skipped", reason: "disabled" };+ if (key === "unavailable") return { status: "skipped", reason: "unavailable" };+ try {+ return await this.captureArchive(state, unbased ? "base" : "delta", turn, key, generation, { trigger: "final", reason, ended, ...(signal ? { signal } : {}) });+ } catch (error) {+ if (signal?.aborted) return { status: "skipped", reason: "cancelled" };+ throw error;+ }+ }++ /**+ * A path the session touched, workspace-relative (portable `/`), as the+ * collector reports it: a tool's path in the trace, or a change the+ * watcher saw. Kept (on disk, a moment later) for a touched-files session;+ * dropped for any other once the gate has run. Cheap: called for every+ * file event.+ */+ recordTouched(sessionId: string, path: string): void {+ if (this.disabled || !SESSION_ID_PATTERN.test(sessionId) || this.stoppedSessions.has(sessionId)) return;+ this.touched.note(sessionId, path);+ }++ /** The session is not archived (a child session): its reported paths go. */+ forgetTouched(sessionId: string): void {+ void this.touched.forget(sessionId).catch((error: unknown) => this.log("warn", "OmniRush touched-files paths could not be removed", { sessionId, error: errorSummary(error) }));+ }+/*** 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.+ * 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.*/async startFinalCandidates(): Promise<string[]> {try {@@ -422,15 +504,19 @@ 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 || !(nowMs - Date.parse(state.updated_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));const roots = new Set<string>();const picked: string[] = [];for (const state of sessions) {- const newestOnRoot = !roots.has(state.root);- roots.add(state.root);+ // A touched-files chain holds only its own session's files: it counts as a folder of its own.+ const folder = state.marker === TOUCHED_MARKER ? `${state.root}\0${state.session_id}` : state.root;+ const newestOnRoot = !roots.has(folder);+ roots.add(folder);if (state.final_due || newestOnRoot) picked.push(state.session_id);}return picked.slice(0, MAX_START_FINALS);@@ -467,6 +553,7 @@ export class SessionArchiver {*/async signOut(): Promise<void> {this.generation += 1;+ this.touched.clear();this.clearRetryTimer();this.drainController?.abort();try {@@ -507,6 +594,8 @@ export class SessionArchiver {async stop(options: { finals?: readonly string[]; budgetMs?: number } = {}): Promise<void> {this.clearRetryTimer();this.drainController?.abort();+ // Touched paths reported in the last moments reach disk for the next start.+ await this.touched.flush().catch((error: unknown) => this.log("warn", "OmniRush touched-files paths could not be written", { error: errorSummary(error) }));const budgetMs = options.budgetMs ?? 0;if (options.finals && options.finals.length > 0 && budgetMs > 0) await this.quitFinals(options.finals, budgetMs);this.generation += 1;@@ -567,11 +656,19 @@ export class SessionArchiver {return run;}- private async captureBaseLocked(sessionId: string, root: string, turn: number, generation: number): Promise<CaptureResult> {+ /**+ * The gate, then the base: a whole-folder base at once; a touched-files+ * session is registered and gets its base from the first capture that has+ * a touched file (at session start, `lazy`, only registered).+ */+ private async captureBaseLocked(sessionId: string, root: string, turn: number, generation: number, lazy = false): Promise<CaptureResult> {if (!this.uploader.configured) return { status: "skipped", reason: "disabled" };- // A root no git detector qualified, and that no folder refusal stops, asks for the all-folders policy (4.4).+ // A root no git detector qualified, and that no folder refusal stops, asks for the folder policy (4.4).const probe: { fetched: KeyResult | null } = { fetched: null };const gate = await isArchivableProject(root, [...this.detectors, this.folderDetector(generation, probe)], { appDirs: this.appDirs });+ const touched = gate.marker === TOUCHED_MARKER;+ // Whatever else the gate said, the session keeps no touched paths.+ if (!touched) await this.touched.forget(sessionId);if (!gate.archivable || !gate.marker) return { status: "skipped", reason: "not_archivable" };// The key and the consent check (7.2): a probe made for this base already holds them; else the full fetch.const fetched = probe.fetched ?? await this.uploader.fetchKey();@@ -581,7 +678,10 @@ export class SessionArchiver {return { status: "skipped", reason: "disabled" };}// The policy was turned off since the answer this folder was let in on.- if (gate.marker === FOLDER_MARKER && fetched.status === "ok" && !fetched.policy.allFolders) return { status: "skipped", reason: "not_archivable" };+ if (fetched.status === "ok" && !markerAllowed(gate.marker, fetched.policy)) {+ if (touched) await this.touched.forget(sessionId);+ return { status: "skipped", reason: "not_archivable" };+ }const state: SessionState = {v: 1,session_id: sessionId,@@ -593,7 +693,9 @@ export class SessionArchiver {baseline: null,stopped: null,updated_at: this.now().toISOString(),+ ...(touched ? { turn_seen: turn } : {}),};+ if (touched) this.touched.track(sessionId);if (fetched.status === "unavailable") {// 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);@@ -601,6 +703,10 @@ export class SessionArchiver {return { status: "skipped", reason: "unavailable" };}await this.enable(fetched.key);+ if (touched && lazy) {+ if (generation === this.generation) await this.saveSession(state);+ return { status: "skipped", reason: "unchanged" };+ }try {return await this.captureArchive(state, "base", turn, fetched.key, generation);} catch (error) {@@ -610,27 +716,27 @@ export class SessionArchiver {}}- /** The all-folders detector (4.4) with its refusals and the policy; `probe` receives the key response of a probe it made. */+ /** The folder detector (4.4) with its refusals and the policy; `probe` receives the key response of a probe it made. */private folderDetector(generation: number, probe?: { fetched: KeyResult | null }): ProjectMarkerDetector {return folderDetector(async () => {- const answer = await this.allFoldersPolicy(generation);+ const answer = await this.folderPolicy(generation);if (probe) probe.fetched = answer.fetched;- return answer.allFolders;+ return answer.policy;}, this.folderGate);}/**- * The all-folders policy (4.4) for a folder the gate would otherwise let in:- * the answer kept with the key while it is younger than POLICY_TTL_MS, else- * one probe (a single GET /archives/key, no backoff, no bearer refresh).- * Anything but a valid key with `policy.all_folders: true` is off; a failed- * probe is kept as off too, so an outage costs one request per- * POLICY_TTL_MS, never a retry storm. `fetched` is the probe's answer when- * this call made one: the base reuses it for its key.+ * The folder policy (4.4: all folders, touched files) for a folder the+ * gate would otherwise let in: the answer kept with the key while it is+ * younger than POLICY_TTL_MS, else one probe (a single GET /archives/key,+ * no backoff, no bearer refresh). Anything but a valid key with the flag+ * `true` is off; a failed probe is kept as off too, so an outage costs one+ * request per POLICY_TTL_MS, never a retry storm. `fetched` is the probe's+ * answer when this call made one: the base reuses it for its key.*/- private async allFoldersPolicy(generation: number): Promise<{ allFolders: boolean; fetched: KeyResult | null }> {+ private async folderPolicy(generation: number): Promise<{ policy: ArchivePolicy; fetched: KeyResult | null }> {const kept = this.policy;- if (kept && this.now().getTime() - kept.at < POLICY_TTL_MS) return { allFolders: kept.allFolders, fetched: null };+ if (kept && this.now().getTime() - kept.at < POLICY_TTL_MS) return { policy: kept.value, fetched: null };let probe = this.policyProbe;if (!probe) {const started = this.uploader.probeKey();@@ -642,18 +748,45 @@ export class SessionArchiver {}const fetched = await probe;this.rememberPolicy(fetched, generation, true);- return { allFolders: fetched.status === "ok" && fetched.policy.allFolders, fetched };+ return { policy: fetched.status === "ok" ? fetched.policy : POLICY_OFF, fetched };+ }++ /** Whether the policy a plain folder's (`folder`) or touched files' (`touched`) chain needs is on. */+ private async policyAllows(marker: string, generation: number): Promise<boolean> {+ return markerAllowed(marker, (await this.folderPolicy(generation)).policy);+ }++ /**+ * Whether a chain may still capture (a final archive): a git root still+ * passes the gate; a plain folder's or touched files' root still passes+ * the root checks and the folder refusals, and their policy is on.+ */+ private async chainAllowed(state: SessionState, generation: number): Promise<boolean> {+ if (state.marker !== FOLDER_MARKER && state.marker !== TOUCHED_MARKER) return (await isArchivableProject(state.root, this.detectors, { appDirs: this.appDirs })).archivable;+ const accepted: ProjectMarkerDetector = async (root) => ((await folderRootRefusal(root, this.folderGate)) ? null : { archivable: true, reason: state.marker, marker: state.marker });+ if (!(await isArchivableProject(state.root, [accepted], { appDirs: this.appDirs })).archivable) return false;+ return this.policyAllows(state.marker, generation);+ }++ /** 422 archive_marker_not_allowed: the kept policy says off for that marker, until it is asked again (POLICY_TTL_MS). */+ private markerRefused(marker: string, generation: number): void {+ if (generation !== this.generation) return;+ const value = this.policy?.value ?? POLICY_OFF;+ this.policy = {+ value: { allFolders: value.allFolders && marker !== FOLDER_MARKER, touchedFiles: value.touchedFiles && marker !== TOUCHED_MARKER },+ at: this.now().getTime(),+ };}/**- * Keeps a key response's policy for allFoldersPolicy. A full fetch (git+ * Keeps a key response's policy for folderPolicy. A full fetch (git* bases, deltas) counts only when it answered; a probe counts whatever it* got. Nothing from before a sign-out is kept.*/private rememberPolicy(fetched: KeyResult, generation: number, probe: boolean): void {if (generation !== this.generation) return;if (fetched.status === "unavailable" && !probe) return;- this.policy = { allFolders: fetched.status === "ok" && fetched.policy.allFolders, at: this.now().getTime() };+ this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at: this.now().getTime() };}private async currentKey(signal?: AbortSignal): Promise<ArchiveKey | "disabled" | "unavailable"> {@@ -690,29 +823,59 @@ export class SessionArchiver {const rootKey = stateKey(state.root);const { signal } = options;const cache = await this.loadHashCache(rootKey);- const scanning = scanArchiveTree(state.root, { excludedDirs: this.appDirs, includeCredentialFiles: this.includeCredentials, hashCache: cache, ...(signal ? { signal } : {}) });- // A plain folder sends no git block (workspace.git null), even inside a larger repository.- const withGit = state.marker !== FOLDER_MARKER;- const gitReading = kind === "base" && withGit ? readArchiveGit(state.root) : null;- const scan = await scanning;- let files: ScannedEntry[] = scan.entries;+ const touched = state.marker === TOUCHED_MARKER;+ let files: ScannedEntry[];let deleted: string[] | undefined;- if (kind === "delta") {- const baseline = state.baseline ? parseBaselineText(await readText(join(this.dirs.baselines, state.baseline))) : null;+ let excluded: ExcludedCounts;+ /** The chain's entry list after this archive: the next baseline. */+ let next: readonly ArchiveEntry[];+ let git: ArchiveGit | null = null;+ if (touched) {+ const baseline = kind === "delta" ? await this.readBaseline(state) : [];if (!baseline) {await this.markStopped(sessionId, "baseline_missing");return { status: "skipped", reason: "stopped" };}- const delta = computeArchiveDelta(baseline, scan.entries);- if (isArchiveDeltaEmpty(delta)) {+ // Every path the session touched, and every file the chain holds (to see it go).+ const paths = await this.touched.snapshot(sessionId);+ for (const entry of baseline) paths.add(entry.path);+ const scan = await scanTouchedFiles(state.root, paths, { excludedDirs: this.appDirs, includeCredentialFiles: this.includeCredentials, hashCache: cache, ...(signal ? { signal } : {}) });+ const change = touchedChange(baseline, scan);+ if (change.files.length === 0 && change.deleted.length === 0) {if (cache.changed) await this.saveHashCache(rootKey, cache);- if (generation === this.generation) await this.noteUnchanged(state, options);+ if (generation === this.generation) await this.noteUnchanged(state, kind, turn, options);return { status: "skipped", reason: "unchanged" };}- files = delta.files;- deleted = delta.deleted;+ files = change.files;+ deleted = kind === "delta" ? change.deleted : undefined;+ excluded = scan.excluded;+ next = change.next;+ } else {+ const scanning = scanArchiveTree(state.root, { excludedDirs: this.appDirs, includeCredentialFiles: this.includeCredentials, hashCache: cache, ...(signal ? { signal } : {}) });+ // A plain folder sends no git block (workspace.git null), even inside a larger repository.+ const withGit = state.marker !== FOLDER_MARKER;+ const gitReading = kind === "base" && withGit ? readArchiveGit(state.root) : null;+ const scan = await scanning;+ files = scan.entries;+ excluded = scan.excluded;+ next = scan.entries;+ if (kind === "delta") {+ const baseline = await this.readBaseline(state);+ if (!baseline) {+ await this.markStopped(sessionId, "baseline_missing");+ return { status: "skipped", reason: "stopped" };+ }+ const delta = computeArchiveDelta(baseline, scan.entries);+ if (isArchiveDeltaEmpty(delta)) {+ if (cache.changed) await this.saveHashCache(rootKey, cache);+ if (generation === this.generation) await this.noteUnchanged(state, kind, turn, options);+ return { status: "skipped", reason: "unchanged" };+ }+ files = delta.files;+ deleted = delta.deleted;+ }+ git = withGit ? await (gitReading ?? readArchiveGit(state.root)) : null;}- const git = withGit ? await (gitReading ?? readArchiveGit(state.root)) : null;const archiveId = this.random.uuid();const sequence = state.next_sequence;const createdAt = this.now();@@ -726,12 +889,13 @@ export class SessionArchiver {parentArchiveId: kind === "base" ? null : state.last_archive_id,...(kind === "delta" && options.trigger ? { trigger: options.trigger } : {}),...(kind === "delta" && options.reason ? { reason: options.reason } : {}),+ ...(touched ? { scope: "touched" as const } : {}),label: archiveLabel(state.root),marker: state.marker,git,files,...(deleted ? { deleted } : {}),- excluded: scan.excluded,+ excluded,});const partial = join(this.dirs.tmp, `${archiveId}.partial`);let sealed: Awaited<ReturnType<typeof writeSealedArchive>>;@@ -780,15 +944,15 @@ export class SessionArchiver {upload: null,};await writeJsonAtomic(join(this.dirs.queue, `${archiveId}.json`), record);- // 3: the new baseline is the full scan; entries that changed while packing get a null hash so the next delta sends them again.+ // 3: the new baseline is the full scan (touched files: the chain's files); entries that changed while packing get a null hash so the next delta sends them again.const unstable = new Set(sealed.unstable);for (const path of unstable) cache.forget(path);const baselineName = `${sessionKey}-${sequence}.json`;- await writeChunksAtomic(join(this.dirs.baselines, baselineName), baselineChunks(scan.entries, unstable));+ await writeChunksAtomic(join(this.dirs.baselines, baselineName), baselineChunks(next, unstable));// 4: commit. Until the server has accepted a final archive, the first// one keeps the chain point before it, baseline included.const previousBaseline = state.baseline;- const rewind = state.rewind ?? (options.trigger === "final" && !this.finalsAccepted+ const rewind = state.rewind ?? (kind === "delta" && options.trigger === "final" && !this.finalsAccepted? { next_sequence: state.next_sequence, last_archive_id: state.last_archive_id, last_turn: state.last_turn, baseline: state.baseline }: null);await this.saveSession({@@ -811,6 +975,7 @@ export class SessionArchiver {sequence,turn,...(options.trigger === "final" ? { trigger: "final", reason: options.reason } : {}),+ ...(touched ? { scope: "touched" } : {}),bytes: sealed.size,files: files.length,...(deleted ? { deleted: deleted.length } : {}),@@ -819,8 +984,21 @@ export class SessionArchiver {return { status: "queued", archiveId, kind, sequence, size: sealed.size };}- /** Nothing changed: after a turn the session's final is due (checked at the next start), a final settles it. */- private async noteUnchanged(state: SessionState, options: CaptureOptions): Promise<void> {+ /** The entry list after the chain's last archive; null when it cannot be read (the chain cannot go on). */+ private async readBaseline(state: SessionState): Promise<ArchiveEntry[] | null> {+ return state.baseline ? parseBaselineText(await readText(join(this.dirs.baselines, state.baseline))) : null;+ }++ /**+ * Nothing changed: after a turn the session's final is due (checked at the+ * next start), a final settles it. A touched-files session without a base+ * (nothing touched yet) stays registered, with the turns seen so far.+ */+ private async noteUnchanged(state: SessionState, kind: ArchiveKind, turn: number, options: CaptureOptions): Promise<void> {+ if (kind === "base") {+ await this.saveSession({ ...state, turn_seen: turn, ...(options.ended ? { ended: this.now().toISOString() } : {}), updated_at: this.now().toISOString() });+ return;+ }const final = options.trigger === "final";if (final && !state.final_due && !options.ended) return;await this.saveSession({@@ -923,6 +1101,8 @@ export class SessionArchiver {// A server without final archives refuses the repeated turn number: not a broken chain.result.dropped += await this.rewindRefusedFinal(record);} else {+ // The server does not take this marker: its policy is off here too until the next answer, so no other chain starts meanwhile.+ if (outcome.code === ARCHIVE_MARKER_NOT_ALLOWED) this.markerRefused(record.request.marker, generation);result.dropped += await this.stopSession(sessionId, outcome.code);}held.add(sessionId);@@ -934,6 +1114,8 @@ export class SessionArchiver {// Nothing exists on the server yet: the base is captured again with the current key.result.dropped += await this.resetToNoBase(sessionId);} else {+ // The server does not take this marker: its policy is off here too until the next answer, so no other chain starts meanwhile.+ if (outcome.code === ARCHIVE_MARKER_NOT_ALLOWED) this.markerRefused(record.request.marker, generation);result.dropped += await this.stopSession(sessionId, outcome.code);}held.add(sessionId);@@ -977,7 +1159,7 @@ export class SessionArchiver {if (!this.disabled) this.log("info", "OmniRush project archiving is off for this account", { code });this.disabled = code;this.key = null;- this.policy = { allFolders: false, at: this.now().getTime() };+ this.policy = { value: POLICY_OFF, at: this.now().getTime() };await this.saveArchiverState();const records = await this.listQueue();for (const record of records) {@@ -1013,11 +1195,12 @@ export class SessionArchiver {}).catch((error) => this.log("warn", "OmniRush archive session stop failed", { sessionId, error: errorSummary(error) }));}- /** Under the session lock: the stopped flag, and any job queued since. */+ /** Under the session lock: the stopped flag, and any job queued since; a stopped session keeps no touched paths. */private async markStopped(sessionId: string, code: string): Promise<void> {for (const record of await this.listQueue()) {if (record.request.session_id === sessionId) await this.deleteJob(record);}+ await this.touched.forget(sessionId);const state = await this.loadSession(sessionId);if (state && !state.stopped) await this.saveSession({ ...state, stopped: code, updated_at: this.now().toISOString() });}@@ -1246,5 +1429,10 @@ export class SessionArchiver {for (const name of await readdir(this.dirs.hashCache)) {if (name.endsWith(".tmp")) await rm(join(this.dirs.hashCache, name), { force: true });}+ // Touched paths only of touched-files sessions still archiving.+ for (const name of await readdir(this.dirs.touched)) {+ const session = name.endsWith(".jsonl") ? sessions.get(name.slice(0, -".jsonl".length)) : undefined;+ if (!session || session.marker !== TOUCHED_MARKER || session.stopped || session.ended) await rm(join(this.dirs.touched, name), { force: true });+ }}}diff --git a/apps/server/src/session-archive/lifecycle.test.ts b/apps/server/src/session-archive/lifecycle.test.tsindex f379b278..b239e52f 100644--- a/apps/server/src/session-archive/lifecycle.test.ts+++ b/apps/server/src/session-archive/lifecycle.test.ts@@ -34,6 +34,8 @@ class FakeArchiver implements ProjectArchiver {startFinals: string[] = [];/** The options of each stop() call. */readonly stops: Array<{ finals?: readonly string[]; budgetMs?: number }> = [];+ /** recordTouched and forgetTouched calls (not in `calls`). */+ readonly touched: string[] = [];async captureBase(sessionId: string, root: string, turn = 0): Promise<CaptureResult> {this.calls.push(`base ${sessionId} ${root} ${turn}`);@@ -54,6 +56,14 @@ class FakeArchiver implements ProjectArchiver {return this.startFinals;}+ recordTouched(sessionId: string, path: string): void {+ this.touched.push(`record ${sessionId} ${path}`);+ }++ forgetTouched(sessionId: string): void {+ this.touched.push(`forget ${sessionId}`);+ }+async drain(): Promise<DrainResult> {this.calls.push("drain");return this.drainResult();@@ -211,6 +221,43 @@ describe("ProjectArchiveLifecycle", () => {expect(child.reads).toEqual({ session: 1, messages: 0 });});+ test("touched paths reach the archiver only for a session this run started that may be archived", async () => {+ const archiver = new FakeArchiver();+ const { subject } = lifecycle(archiver);+ const root = await tempDir("root");+ // Before its first prompt, the session is not known here.+ subject.pathTouched("ses_touch_0001", "early.txt");+ subject.sessionStarted({ sessionId: "ses_touch_0001", root, engine: engine().reader });+ // While its start step waits, and once it is resolved with a root.+ subject.pathTouched("ses_touch_0001", "brief.pdf");+ await subject.settled();+ subject.pathTouched("ses_touch_0001", "out/render.png");+ // Not archivable: nothing more.+ archiver.base = async () => skipped("not_archivable");+ subject.sessionStarted({ sessionId: "ses_touch_0002", root, engine: engine().reader });+ await subject.settled();+ subject.pathTouched("ses_touch_0002", "x.txt");+ // A child session: what it reported goes, and nothing more is passed on.+ subject.sessionStarted({ sessionId: "ses_touch_child", root, engine: engine({ parentID: "ses_touch_0001" }).reader });+ subject.pathTouched("ses_touch_child", "child.txt");+ await subject.settled();+ subject.pathTouched("ses_touch_child", "later.txt");+ // Deleted: forgotten.+ subject.sessionEnded("ses_touch_0001");+ subject.pathTouched("ses_touch_0001", "after.txt");+ await subject.settled();+ expect(archiver.touched).toEqual([+ "record ses_touch_0001 brief.pdf",+ "record ses_touch_0001 out/render.png",+ "record ses_touch_child child.txt",+ "forget ses_touch_child",+ ]);+ // Signed out: nothing.+ await subject.signOut();+ subject.pathTouched("ses_touch_0001", "signed-out.txt");+ expect(archiver.touched).toHaveLength(4);+ });+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.tsindex 2383f140..c74f6b74 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -15,7 +15,10 @@ import { realpath } from "node:fs/promises";import type { CaptureResult, DrainResult, FinalReason, SessionArchiver } from "./index.js";-export type ProjectArchiver = Pick<SessionArchiver, "captureBase" | "captureDelta" | "captureFinal" | "startFinalCandidates" | "drain" | "signOut" | "stop">;+export type ProjectArchiver = Pick<+ SessionArchiver,+ "captureBase" | "captureDelta" | "captureFinal" | "startFinalCandidates" | "recordTouched" | "forgetTouched" | "drain" | "signOut" | "stop"+>;/** Engine reads for one session, resolving to the parsed JSON, or null when it cannot be read. */export type ArchiveEngineReads = {@@ -268,6 +271,18 @@ export class ProjectArchiveLifecycle {this.armIdleFinal(sessionId, record);}+ /**+ * The collector saw the session touch a path (workspace-relative): a+ * tool's path in the trace, or a change on disk. Kept for a touched-files+ * session (the archiver drops it for any other); nothing for a session+ * this app run has not started or knows is not archived.+ */+ pathTouched(sessionId: string, path: string): void {+ const record = this.sessions.get(sessionId);+ if (!this.active || this.consentOff || !record || (!record.root && !record.start)) return;+ this.archiver.recordTouched(sessionId, path);+ }+/*** The observer follows a turn of the session: it is not quiet. A turn that* ended just before (its turnCompleted can come after the next prompt's@@ -365,6 +380,7 @@ export class ProjectArchiveLifecycle {if (parent === undefined) return this.unresolved(sessionId, record, "the engine session could not be read");if (parent !== null) {record.start = null;+ this.archiver.forgetTouched(sessionId);return true;}const root = await realpath(start.root).catch(() => null);diff --git a/apps/server/src/session-archive/manifest.ts b/apps/server/src/session-archive/manifest.tsindex f764a1f3..d70283d2 100644--- a/apps/server/src/session-archive/manifest.ts+++ b/apps/server/src/session-archive/manifest.ts@@ -17,9 +17,9 @@ import { hintGarbageCollection } from "./files.js";export const ARCHIVE_SCHEMA = "omnirush.archive.v1";export const RESERVED_ROOT_NAME = "__omnirush__";-const STAT_CONCURRENCY = 64;-const HASH_CONCURRENCY = 6;-const HASH_READ_BYTES = 128 * 1024;+export const STAT_CONCURRENCY = 64;+export const HASH_CONCURRENCY = 6;+export const HASH_READ_BYTES = 128 * 1024;/** Bytes hashed, or entries inspected or hashed, between two garbage collection hints. */const GC_HINT_BYTES = 64 * 1024 * 1024;const GC_HINT_ENTRIES = 10_000;@@ -43,6 +43,12 @@ export type ArchiveKind = "base" | "delta";export type ArchiveTrigger = "turn" | "final";/** What prompted a final archive (manifest.json `reason`). */export type FinalReason = "idle" | "turn_incomplete" | "session_deleted" | "app_quit" | "app_start";+/**+ * manifest.json `scope` of a touched-files archive: its `files` are only the+ * files the agent touched, and a path it does not list is not deleted (only+ * `deleted` deletes). The whole-folder archives have no `scope`.+ */+export type ArchiveScope = "touched";export type ArchiveEntryType = "file" | "dir" | "symlink";/** One manifest `files` item (section 5.4). */@@ -104,6 +110,7 @@ export type ArchiveManifest = {parent_archive_id: string | null;trigger?: ArchiveTrigger;reason?: FinalReason;+ scope?: ArchiveScope;workspace: { label: string; marker: string; git: ArchiveGit | null };files: ArchiveEntry[];deleted?: string[];@@ -340,7 +347,7 @@ export type ScanResult = { entries: ScannedEntry[]; excluded: ExcludedCounts };* pull plain data items, so a 200k-entry level costs 200k small objects, not* 200k suspended async frames.*/-async function forEachBounded<T>(items: readonly T[], limit: number, operation: (item: T) => Promise<void>): Promise<void> {+export async function forEachBounded<T>(items: readonly T[], limit: number, operation: (item: T) => Promise<void>): Promise<void> {let next = 0;const worker = async (): Promise<void> => {while (next < items.length) {@@ -354,7 +361,7 @@ async function forEachBounded<T>(items: readonly T[], limit: number, operation:const NAME_DECODER = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });-function decodeName(raw: Buffer): string | null {+export function decodeName(raw: Buffer): string | null {try {return NAME_DECODER.decode(raw);} catch {@@ -362,12 +369,12 @@ function decodeName(raw: Buffer): string | null {}}-function portable(path: string): string {+export function portable(path: string): string {return sep === "/" ? path : path.split(sep).join("/");}/** The excluded directories that lie strictly under the root, as root-relative paths. */-async function excludedRelativeDirs(root: string, dirs: readonly string[]): Promise<Set<string>> {+export async function excludedRelativeDirs(root: string, dirs: readonly string[]): Promise<Set<string>> {const rootForms = new Set([resolve(root)]);try {rootForms.add(await realpath(root));@@ -396,7 +403,7 @@ async function excludedRelativeDirs(root: string, dirs: readonly string[]): Prom// Strings kept per entry are built with join(), which yields one flat string;// template concatenation in JSC yields a rope that keeps every piece alive,// roughly doubling the per-entry cost on a 200k-entry tree.-function statFields(stats: BigIntStats) {+export function statFields(stats: BigIntStats) {return {mode: Number(stats.mode & 0o7777n),mtime: Number(stats.mtimeNs / 1_000_000_000n),@@ -638,6 +645,8 @@ export type ManifestInput = {/** Deltas only: a completed turn, or a final archive (then with its reason). */trigger?: ArchiveTrigger;reason?: FinalReason;+ /** Touched-files archives only. */+ scope?: ArchiveScope;label: string;marker: string;git: ArchiveGit | null;@@ -667,6 +676,7 @@ export function manifestSource(input: ManifestInput): ManifestSource {parent_archive_id: input.parentArchiveId,...(input.trigger ? { trigger: input.trigger } : {}),...(input.reason ? { reason: input.reason } : {}),+ ...(input.scope ? { scope: input.scope } : {}),workspace: { label: input.label, marker: input.marker, git: input.git },});const deleted = input.kind === "delta" ? `,"deleted":${JSON.stringify(input.deleted ?? [])}` : "";diff --git a/apps/server/src/session-archive/policy.ts b/apps/server/src/session-archive/policy.tsindex 33bda97a..b9f8fc38 100644--- a/apps/server/src/session-archive/policy.ts+++ b/apps/server/src/session-archive/policy.ts@@ -1,15 +1,21 @@/*** The archive policy GET /archives/key sends beside the key (backend spec* 4.4). `policy.all_folders: true` archives every folder a session starts- * in, not only git repositories. Anything else (no policy, a server that- * predates it, a value that is not the boolean true) keeps git only.+ * in, not only git repositories; `policy.touched_files: true` archives, in+ * a folder that is neither, the files the agent touched there. Anything+ * else (no policy, a server that predates it, a value that is not the+ * boolean true) is off: git only.*/import { z } from "zod";-export type ArchivePolicy = { allFolders: boolean };+export type ArchivePolicy = { allFolders: boolean; touchedFiles: boolean };++/** Both off: what a failed probe, a missing policy or a disabled account means. */+export const POLICY_OFF: ArchivePolicy = { allFolders: false, touchedFiles: false };const allFoldersSchema = z.object({ policy: z.object({ all_folders: z.literal(true) }) });+const touchedFilesSchema = z.object({ policy: z.object({ touched_files: z.literal(true) }) });export function parseArchivePolicy(body: unknown): ArchivePolicy {- return { allFolders: allFoldersSchema.safeParse(body).success };+ return { allFolders: allFoldersSchema.safeParse(body).success, touchedFiles: touchedFilesSchema.safeParse(body).success };}diff --git a/apps/server/src/session-archive/touched.test.ts b/apps/server/src/session-archive/touched.test.tsnew file mode 100644index 00000000..fcb1b655--- /dev/null+++ b/apps/server/src/session-archive/touched.test.ts@@ -0,0 +1,166 @@+import { afterEach, describe, expect, test } from "bun:test";+import { execFile } from "node:child_process";+import { createHash, randomBytes } from "node:crypto";+import { appendFile, mkdir, readdir, readFile, symlink, writeFile } from "node:fs/promises";+import { join, sep } from "node:path";+import { promisify } from "node:util";++import { ArchiveHashCache, type ArchiveEntry } from "./manifest.js";+import { cleanupTempDirs, tempDir } from "./test-helpers.js";+import { scanTouchedFiles, touchedChange, TouchedPathStore, touchedPathParts } from "./touched.js";++const execFileAsync = promisify(execFile);++afterEach(cleanupTempDirs);++const sha256 = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex");++/** Paths the scan looked at, and whether every one of them lies in `root`. */+function accessLog(root: string) {+ const paths: string[] = [];+ return { paths, onAccess: (path: string) => paths.push(path), outside: () => paths.filter((path) => path !== root && !path.startsWith(`${root}${sep}`)) };+}++describe("scanTouchedFiles", () => {+ test("only touched regular files inside the root, hashed from the file itself; nothing outside is looked at", async () => {+ const outside = await tempDir("touched-outside");+ await writeFile(join(outside, "secret.txt"), "OUTSIDE");+ await mkdir(join(outside, "dir"));+ await writeFile(join(outside, "dir/inner.txt"), "OUTSIDE INNER");+ const root = await tempDir("touched-root");+ const render = randomBytes(64 * 1024);+ const brief = randomBytes(48 * 1024);+ await writeFile(join(root, "render.png"), render);+ await writeFile(join(root, "brief.pdf"), brief);+ await writeFile(join(root, "untouched.txt"), "never touched");+ await mkdir(join(root, "sub"));+ await writeFile(join(root, "sub/a.txt"), "inside a touched folder");+ await writeFile(join(root, ".env"), "TOKEN=abc");+ await mkdir(join(root, "__omnirush__"));+ await writeFile(join(root, "__omnirush__/x"), "reserved");+ await mkdir(join(root, ".omnirush-state"));+ await writeFile(join(root, ".omnirush-state/ledger.json"), "{}");+ await mkdir(join(root, "real"));+ const data = Buffer.from("a,b\n1,2\n");+ await writeFile(join(root, "real/data.csv"), data);+ await symlink("real/data.csv", join(root, "alias.csv"));+ await symlink(join(outside, "secret.txt"), join(root, "link-out"));+ await symlink("../touched-outside-escape/x", join(root, "link-up"));+ await symlink(join(outside, "dir"), join(root, "linked-dir"));+ await execFileAsync("mkfifo", [join(root, "pipe")]);+ const access = accessLog(root);++ const scan = await scanTouchedFiles(root, [+ "render.png", "brief.pdf", "sub", ".env", "__omnirush__/x", ".omnirush-state/ledger.json", "alias.csv", "link-out", "link-up",+ "linked-dir/inner.txt", "missing.txt", "pipe", "../touched-outside/secret.txt", `${outside}/secret.txt`, "sub/./a.txt", "",+ ], { excludedDirs: [join(root, ".omnirush-state")], onAccess: access.onAccess });++ expect(scan.entries.map((entry) => [entry.path, entry.type, entry.size, entry.sha256])).toEqual([+ ["brief.pdf", "file", brief.length, sha256(brief)],+ ["real/data.csv", "file", data.length, sha256(data)],+ ["render.png", "file", render.length, sha256(render)],+ ]);+ expect([...scan.gone].sort()).toEqual(["alias.csv", "link-out", "link-up", "linked-dir/inner.txt", "missing.txt", "pipe", "sub"]);+ expect(scan.excluded).toEqual({ credential: 1, special: 3, app_state: 1, reserved: 1, unreadable: 0, non_utf8: 0 });+ expect(access.paths.length).toBeGreaterThan(0);+ expect(access.outside()).toEqual([]);+ // A touched folder is never expanded.+ expect(access.paths).not.toContain(join(root, "sub/a.txt"));+ });++ test("credentials are kept only with archiveIncludeCredentialFiles; the hash cache is used and never pruned", async () => {+ const root = await tempDir("touched-cache");+ await writeFile(join(root, ".env"), "TOKEN=abc");+ await writeFile(join(root, "a.bin"), randomBytes(1024));+ const cache = ArchiveHashCache.fromText(null);+ const first = await scanTouchedFiles(root, ["a.bin", ".env"], { includeCredentialFiles: true, hashCache: cache });+ expect(first.entries.map((entry) => entry.path)).toEqual([".env", "a.bin"]);+ const text = [...cache.jsonChunks()].join("");+ // A later scan of one path keeps the other's row, and takes a.bin from the cache.+ const reloaded = ArchiveHashCache.fromText(text);+ const second = await scanTouchedFiles(root, ["a.bin"], { hashCache: reloaded });+ expect(second.entries[0]!.sha256).toBe(first.entries[1]!.sha256);+ expect([...reloaded.jsonChunks()].join("")).toContain('".env"');+ });++ test("touched paths must be plain relative paths", () => {+ expect(touchedPathParts("docs/brief.pdf")).toEqual(["docs", "brief.pdf"]);+ for (const path of ["", "/etc/hosts", "../x", "a/../b", "a//b", "./a", "a/.", "a\0b"]) expect([path, touchedPathParts(path)]).toEqual([path, null]);+ });+});++describe("touchedChange", () => {+ test("a delta carries added and modified files, deletes only what is gone, and keeps what it could not read", () => {+ const entry = (path: string, sha: string): ArchiveEntry => ({ path, type: "file", mode: 0o644, size: 1, sha256: sha.repeat(64) });+ const scanned = (path: string, sha: string) => ({ ...entry(path, sha), mtime: 0, statKey: "1:0:0:1", dev: 1 });+ const baseline = [entry("doc.pdf", "a"), entry("gone.txt", "b"), entry("locked.bin", "c"), entry("same.png", "d")];+ const change = touchedChange(baseline, {+ entries: [scanned("doc.pdf", "e"), scanned("new.txt", "f"), scanned("same.png", "d")],+ gone: new Set(["gone.txt", "never-archived.txt"]),+ excluded: { credential: 0, special: 0, app_state: 0, reserved: 0, unreadable: 1, non_utf8: 0 },+ });+ expect(change.files.map((file) => file.path)).toEqual(["doc.pdf", "new.txt"]);+ expect(change.deleted).toEqual(["gone.txt"]);+ expect(change.next.map((file) => [file.path, file.sha256?.[0]])).toEqual([["doc.pdf", "e"], ["locked.bin", "c"], ["new.txt", "f"], ["same.png", "d"]]);+ });+});++describe("TouchedPathStore", () => {+ function store(dir: string, mode: "tracked" | "ignored" | "unknown" = "unknown") {+ const logs: string[] = [];+ return {+ logs,+ store: new TouchedPathStore(dir, { ready: async () => undefined, modeOf: async () => mode, log: (_level, message) => logs.push(message), flushMs: 60_000 }),+ };+ }++ test("paths wait in memory until the session is tracked, reach a file that a new store reads back, and go with forget", async () => {+ const dir = await tempDir("touched-store");+ const { store: first } = store(dir);+ first.note("ses_touched_01", "brief.pdf");+ first.note("ses_touched_01", "brief.pdf");+ first.note("ses_touched_01", "../outside.txt");+ await first.flush();+ expect(await readdir(dir)).toEqual([]);+ first.track("ses_touched_01");+ first.note("ses_touched_01", "out/render.png");+ await first.flush();+ const [file] = await readdir(dir);+ expect(file).toMatch(/\.jsonl$/);+ expect((await readFile(join(dir, file!), "utf8")).split("\n")).toEqual(['{"v":1,"session_id":"ses_touched_01"}', '"brief.pdf"', '"out/render.png"', ""]);++ // A torn last line (a crash while appending) is skipped.+ await appendFile(join(dir, file!), '"half');+ const { store: second } = store(dir, "tracked");+ expect([...(await second.snapshot("ses_touched_01"))].sort()).toEqual(["brief.pdf", "out/render.png"]);+ second.note("ses_touched_01", "brief.pdf");+ second.note("ses_touched_01", "notes.md");+ expect([...(await second.snapshot("ses_touched_01"))].sort()).toEqual(["brief.pdf", "notes.md", "out/render.png"]);+ expect(await second.has("ses_touched_01")).toBe(true);+ // The path written after the torn line is on a line of its own.+ const { store: third } = store(dir, "tracked");+ expect([...(await third.snapshot("ses_touched_01"))].sort()).toEqual(["brief.pdf", "notes.md", "out/render.png"]);++ await second.forget("ses_touched_01");+ expect(await readdir(dir)).toEqual([]);+ second.note("ses_touched_01", "later.txt");+ await second.flush();+ expect(await readdir(dir)).toEqual([]);+ });++ test("a session the record says is something else keeps nothing; clear() drops what is in memory", async () => {+ const dir = await tempDir("touched-ignored");+ const { store: ignored } = store(dir, "ignored");+ ignored.note("ses_git_000001", "src/app.ts");+ await new Promise((resolvePromise) => setTimeout(resolvePromise, 10));+ ignored.note("ses_git_000001", "src/other.ts");+ await ignored.flush();+ expect(await ignored.has("ses_git_000001")).toBe(false);+ const { store: cleared } = store(dir);+ cleared.note("ses_touched_02", "a.txt");+ cleared.clear();+ cleared.track("ses_touched_02");+ await cleared.flush();+ expect(await readdir(dir)).toEqual([]);+ });+});diff --git a/apps/server/src/session-archive/touched.ts b/apps/server/src/session-archive/touched.tsnew file mode 100644index 00000000..5f523d15--- /dev/null+++ b/apps/server/src/session-archive/touched.ts@@ -0,0 +1,539 @@+/**+ * The touched-files archive (policy `touched_files`, README "Touched+ * files"): in a session folder that is neither a git repository nor inside+ * one, only the files the agent touched there are archived, byte for byte.+ * The collector reports every path the session touched (the trace's tool+ * paths, the watcher's changes); TouchedPathStore keeps them per session on+ * disk, and scanTouchedFiles turns them into archive entries at capture+ * time: regular files inside the root only, never through a symlinked+ * folder, a touched folder never expanded, with the whole-folder scan's+ * exclusions. Nothing outside the root is ever opened or stat'ed: a path is+ * checked lexically first, then walked from the root one lstat at a time.+ */+import { createHash } from "node:crypto";+import type { BigIntStats } from "node:fs";+import { appendFile, lstat, open, readFile, readlink, realpath, rm, writeFile } from "node:fs/promises";+import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";++import {+ HASH_CONCURRENCY,+ HASH_READ_BYTES,+ OPEN_ENTRY_FLAGS,+ RESERVED_ROOT_NAME,+ STAT_CONCURRENCY,+ compareArchivePaths,+ decodeName,+ emptyExcludedCounts,+ excludedRelativeDirs,+ forEachBounded,+ isArchiveCredentialPath,+ isArchiveEntryModified,+ isSameFileIdentity,+ portable,+ statFields,+ type ArchiveEntry,+ type ArchiveHashCache,+ type ExcludedCounts,+ type ScannedEntry,+} from "./manifest.js";+import { stateKey } from "./files.js";+import type { ArchiveLog } from "./upload.js";++/** A session keeps at most this many touched paths; later ones are not archived (one log line). */+export const MAX_TOUCHED_PATHS = 100_000;+const MAX_TOUCHED_PATH_CHARS = 4_096;+/** A touched symlink is followed to a file inside the root through at most this many links. */+const MAX_LINK_HOPS = 8;+/** Reported paths reach the session's file within this long. */+const TOUCHED_FLUSH_MS = 2_000;+const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;++// --- scan -------------------------------------------------------------------------++export type TouchedScanOptions = {+ /** Absolute app state/temp/data directories; a path under one inside the root is counted as app_state. */+ excludedDirs?: readonly string[];+ /** archiveIncludeCredentialFiles: turns the credential filter off entirely. */+ includeCredentialFiles?: boolean;+ /** The root's hash cache; the scan looks up and adds entries and never prunes it (it sees only part of the root). */+ hashCache?: ArchiveHashCache;+ /** Stops the scan between paths: it then rejects with the signal's reason. */+ signal?: AbortSignal;+ /** Tests: every absolute path the scan lstats, reads a link of or opens, before it does. */+ onAccess?: (path: string) => void;+};++export type TouchedScanResult = {+ /** The touched regular files inside the root, sorted by UTF-8 bytes. */+ entries: ScannedEntry[];+ /**+ * Touched paths that hold no regular file inside the root any more: gone,+ * a folder, a symlink, a special file, or reached through a folder that is+ * no longer a real folder. A delta deletes those it archived before.+ */+ gone: Set<string>;+ excluded: ExcludedCounts;+};++/** A touched path's components when it is a plain relative path (portable `/`), else null. */+export function touchedPathParts(path: string): string[] | null {+ if (!path || path.length > MAX_TOUCHED_PATH_CHARS || path.includes("\0") || path.startsWith("/")) return null;+ const parts = path.split("/");+ if (parts.some((part) => part === "" || part === "." || part === "..")) return null;+ // Windows: no drive, no stream (`file:stream`), no separator inside a name.+ if (sep === "\\" && parts.some((part) => part.includes("\\") || part.includes(":"))) return null;+ return parts;+}++type DirState = "dir" | "gone" | "unreadable";++function missing(error: unknown): boolean {+ const code = error instanceof Error && "code" in error ? error.code : undefined;+ return code === "ENOENT" || code === "ENOTDIR";+}++/**+ * The root-relative path a symlink at `path` (absolute, under real folders)+ * points to, when it stays inside the root: resolved lexically against the+ * root as given and as its real path, so that nothing outside is looked at.+ * Null when it points outside, at the root itself, or cannot be read.+ */+function linkTargetInside(path: string, target: string, roots: readonly string[]): string | null {+ const absolute = isAbsolute(target) ? resolve(target) : resolve(dirname(path), target);+ for (const root of roots) {+ const rel = relative(root, absolute);+ if (rel && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)) return portable(rel);+ }+ return null;+}++/**+ * Pass 1 of a touched-files capture: each path (and each symlink target+ * inside the root it leads to) walked from the root with lstat, never+ * following a link, then hashed; excluded like the whole-folder scan+ * (credential files, app state, reserved names, special files, unreadable+ * and non-UTF-8 names). A path that is not plain, or whose link leads out of+ * the root, is skipped without touching the file system outside the root.+ */+export async function scanTouchedFiles(root: string, paths: Iterable<string>, options: TouchedScanOptions = {}): Promise<TouchedScanResult> {+ const { signal, hashCache: cache } = options;+ const access = options.onAccess ?? (() => undefined);+ const base = resolve(root);+ access(base);+ const roots = [base];+ const real = await realpath(base);+ if (real !== base) roots.push(real);+ const pruned = [...(await excludedRelativeDirs(base, options.excludedDirs ?? []))];+ const includeCredentials = options.includeCredentialFiles === true;+ const excluded = emptyExcludedCounts();+ const gone = new Set<string>();+ const entries: ScannedEntry[] = [];+ const seen = new Set<string>();+ const dirs = new Map<string, Promise<DirState>>();++ /** Whether `relDir` is a real folder under real folders (a symlink or a file in the way is "gone": the path is not inside the root). */+ const dirState = (parts: readonly string[], depth: number): Promise<DirState> => {+ const key = parts.slice(0, depth).join("/");+ let known = dirs.get(key);+ if (!known) {+ known = (async () => {+ if (depth > 1) {+ const parent = await dirState(parts, depth - 1);+ if (parent !== "dir") return parent;+ }+ const absolute = join(base, ...parts.slice(0, depth));+ access(absolute);+ try {+ const stats = await lstat(absolute);+ return stats.isDirectory() ? "dir" : "gone";+ } catch (error) {+ return missing(error) ? "gone" : "unreadable";+ }+ })();+ dirs.set(key, known);+ }+ return known;+ };++ const visit = async (path: string, hops: number): Promise<void> => {+ signal?.throwIfAborted();+ if (seen.has(path)) return;+ seen.add(path);+ const parts = touchedPathParts(path);+ if (!parts) return;+ if (LONE_SURROGATE.test(path)) {+ excluded.non_utf8 += 1;+ return;+ }+ if (parts[0] === RESERVED_ROOT_NAME) {+ excluded.reserved += 1;+ return;+ }+ if (pruned.some((dir) => path === dir || path.startsWith(`${dir}/`))) {+ excluded.app_state += 1;+ return;+ }+ if (!includeCredentials && isArchiveCredentialPath(path)) {+ excluded.credential += 1;+ return;+ }+ if (parts.length > 1) {+ const parent = await dirState(parts, parts.length - 1);+ if (parent === "unreadable") excluded.unreadable += 1;+ if (parent !== "dir") {+ if (parent === "gone") gone.add(path);+ return;+ }+ }+ const absolute = join(base, ...parts);+ let stats: BigIntStats;+ try {+ access(absolute);+ stats = await lstat(absolute, { bigint: true });+ } catch (error) {+ if (missing(error)) gone.add(path);+ else excluded.unreadable += 1;+ return;+ }+ if (stats.isDirectory()) {+ // A touched folder is never expanded.+ gone.add(path);+ return;+ }+ if (stats.isSymbolicLink()) {+ gone.add(path);+ let target: string | null;+ try {+ access(absolute);+ target = decodeName(await readlink(absolute, { encoding: "buffer" }));+ } catch {+ excluded.unreadable += 1;+ return;+ }+ if (target === null) {+ excluded.non_utf8 += 1;+ return;+ }+ const inside = hops < MAX_LINK_HOPS ? linkTargetInside(absolute, target, roots) : null;+ // A link out of the root (or a chain too long) is skipped: its target is never looked at.+ if (inside === null) excluded.special += 1;+ else await visit(inside, hops + 1);+ return;+ }+ if (!stats.isFile()) {+ gone.add(path);+ excluded.special += 1;+ return;+ }+ entries.push({ path, type: "file", size: Number(stats.size), sha256: null, ...statFields(stats) });+ };++ await forEachBounded([...paths], STAT_CONCURRENCY, (path) => visit(path, 0));++ const unreadable = new Set<ScannedEntry>();+ const buffers: Buffer[] = [];+ await forEachBounded(entries, HASH_CONCURRENCY, async (entry) => {+ signal?.throwIfAborted();+ const cached = cache?.lookup(entry) ?? null;+ if (cached) {+ entry.sha256 = cached;+ return;+ }+ const buffer = buffers.pop() ?? Buffer.allocUnsafe(HASH_READ_BYTES);+ try {+ const absolute = join(base, ...entry.path.split("/"));+ access(absolute);+ const sha256 = await hashTouchedFile(absolute, entry, buffer, signal);+ if (sha256 === null) {+ unreadable.add(entry);+ cache?.forget(entry.path);+ return;+ }+ entry.sha256 = sha256;+ cache?.remember(entry);+ } finally {+ buffers.push(buffer);+ }+ });+ signal?.throwIfAborted();+ for (const path of gone) cache?.forget(path);+ excluded.unreadable += unreadable.size;+ const kept = unreadable.size > 0 ? entries.filter((entry) => !unreadable.has(entry)) : entries;+ kept.sort((left, right) => compareArchivePaths(left.path, right.path));+ return { entries: kept, gone, excluded };+}++/** SHA-256 of the first entry.size bytes of the very file pass 1 lstat'ed (same st_dev and st_ino), else null. */+async function hashTouchedFile(absolute: string, entry: ScannedEntry, buffer: Buffer, signal?: AbortSignal): Promise<string | null> {+ let handle: Awaited<ReturnType<typeof open>>;+ try {+ handle = await open(absolute, OPEN_ENTRY_FLAGS);+ } catch {+ return null;+ }+ try {+ const stats = await handle.stat({ bigint: true });+ if (!stats.isFile() || !isSameFileIdentity(entry, stats)) return null;+ const hash = createHash("sha256");+ let remaining = entry.size;+ while (remaining > 0) {+ if (signal?.aborted) return null;+ const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), null);+ if (bytesRead === 0) break;+ hash.update(buffer.subarray(0, bytesRead));+ remaining -= bytesRead;+ }+ return hash.digest("hex");+ } catch {+ return null;+ } finally {+ await handle.close().catch(() => undefined);+ }+}++export type TouchedChange = {+ /** What the archive carries: every touched file for a base, the added and modified ones for a delta. */+ files: ScannedEntry[];+ /** Files an earlier archive of the chain holds that are gone from the folder. */+ deleted: string[];+ /** The chain's entry list after this archive: the next baseline. */+ next: ArchiveEntry[];+};++/**+ * A touched-files archive against the chain's baseline (empty for a base).+ * A baseline file the scan did not see and that is not gone (unreadable for+ * now) stays in the baseline as it was; only a gone one is deleted.+ */+export function touchedChange(baseline: readonly ArchiveEntry[], scan: TouchedScanResult): TouchedChange {+ const current = new Set(scan.entries.map((entry) => entry.path));+ const previous = new Map(baseline.map((entry) => [entry.path, entry]));+ const files = scan.entries.filter((entry) => {+ const old = previous.get(entry.path);+ return !old || isArchiveEntryModified(old, entry);+ });+ const kept = baseline.filter((entry) => !current.has(entry.path) && !scan.gone.has(entry.path));+ const deleted = baseline.filter((entry) => !current.has(entry.path) && scan.gone.has(entry.path)).map((entry) => entry.path);+ const next: ArchiveEntry[] = [...scan.entries, ...kept].sort((left, right) => compareArchivePaths(left.path, right.path));+ return { files, deleted: deleted.sort(compareArchivePaths), next };+}++// --- the touched set ------------------------------------------------------------------++type TouchedSession = {+ /** unknown until the gate has run for the session (paths wait in memory); ignored: not a touched-files chain. */+ mode: "unknown" | "tracked" | "ignored";+ /** The paths in the session's file (null until read) and those written since. */+ known: Set<string> | null;+ /** Reported and not in `known` yet. */+ pending: Set<string>;+ /** Being appended to the file right now. */+ writing: readonly string[];+ /** The file ends in a line torn by a crash: the next append starts a new line. */+ torn: boolean;+ capped: boolean;+};++/**+ * The paths each touched-files session touched, from its first prompt on:+ * appended to `<dir>/<session key>.jsonl` (a header line, then one JSON+ * string per line; a torn last line is skipped) a moment after they are+ * reported, so they survive an app restart. Paths of a session the gate has+ * not decided on yet wait in memory; `track` keeps them, `forget` drops them+ * and the file.+ */+export class TouchedPathStore {+ private readonly sessions = new Map<string, TouchedSession>();+ private readonly tails = new Map<string, Promise<void>>();+ private timer: ReturnType<typeof setTimeout> | null = null;+ /** Bumped by clear(): writes started before it never land. */+ private epoch = 0;++ constructor(+ private readonly dir: string,+ private readonly options: {+ /** Resolves once the state dir is recovered. */+ ready: () => Promise<void>;+ /** What the session's record says it is: a touched-files chain, something else, or not known yet. */+ modeOf: (sessionId: string) => Promise<"tracked" | "ignored" | "unknown">;+ log: ArchiveLog;+ flushMs?: number;+ },+ ) {}++ /** A path the collector reported for the session (workspace-relative, portable). Cheap: a repeat is a lookup. */+ note(sessionId: string, path: string): void {+ let session = this.sessions.get(sessionId);+ if (!session) {+ session = { mode: "unknown", known: null, pending: new Set(), writing: [], torn: false, capped: false };+ this.sessions.set(sessionId, session);+ const created = session;+ const epoch = this.epoch;+ void this.options.modeOf(sessionId).then((mode) => {+ if (epoch !== this.epoch || this.sessions.get(sessionId) !== created || created.mode !== "unknown" || mode === "unknown") return;+ if (mode === "tracked") this.track(sessionId);+ else this.drop(created);+ }, () => undefined);+ }+ if (session.mode === "ignored" || session.known?.has(path) || session.pending.has(path) || touchedPathParts(path) === null) return;+ if ((session.known?.size ?? 0) + session.pending.size + session.writing.length >= MAX_TOUCHED_PATHS) {+ if (!session.capped) this.options.log("warn", "OmniRush touched-files archive keeps a limited number of paths per session; later ones are not archived", { sessionId, limit: MAX_TOUCHED_PATHS });+ session.capped = true;+ return;+ }+ session.pending.add(path);+ if (session.mode === "tracked") this.schedule();+ }++ /** The session is a touched-files chain: its paths are kept on disk from now on. */+ track(sessionId: string): void {+ const session = this.sessions.get(sessionId);+ if (session) session.mode = "tracked";+ else this.sessions.set(sessionId, { mode: "tracked", known: null, pending: new Set(), writing: [], torn: false, capped: false });+ this.schedule();+ }++ /** The session is not (or no longer) a touched-files chain: nothing more is kept, and its file goes. */+ forget(sessionId: string): Promise<void> {+ const session = this.sessions.get(sessionId);+ if (session) this.drop(session);+ else this.sessions.set(sessionId, { mode: "ignored", known: null, pending: new Set(), writing: [], torn: false, capped: false });+ const epoch = this.epoch;+ return this.serial(sessionId, async () => {+ if (epoch === this.epoch) await rm(this.file(sessionId), { force: true });+ });+ }++ /** Every path the session touched: its file and what is not written yet. The session counts as tracked from now on. */+ snapshot(sessionId: string): Promise<Set<string>> {+ this.track(sessionId);+ return this.serial(sessionId, async () => {+ await this.write(sessionId);+ const session = this.sessions.get(sessionId);+ return new Set([...(session?.known ?? []), ...(session?.pending ?? [])]);+ });+ }++ /** Whether the session has touched paths (in memory or on disk). */+ async has(sessionId: string): Promise<boolean> {+ const session = this.sessions.get(sessionId);+ if (session && session.pending.size + (session.known?.size ?? 0) > 0) return true;+ return (await this.load(sessionId)).paths.size > 0;+ }++ /** Writes every tracked session's pending paths now. */+ async flush(): Promise<void> {+ this.clearTimer();+ await Promise.all([...this.sessions].filter(([, session]) => session.mode === "tracked" && session.pending.size > 0).map(([sessionId]) => this.serial(sessionId, () => this.write(sessionId))));+ }++ /** Sign-out: everything in memory goes, and no write started before lands. */+ clear(): void {+ this.epoch += 1;+ this.clearTimer();+ this.sessions.clear();+ }++ private drop(session: TouchedSession): void {+ session.mode = "ignored";+ session.known = null;+ session.pending = new Set();+ }++ private file(sessionId: string): string {+ return join(this.dir, `${stateKey(sessionId)}.jsonl`);+ }++ private schedule(): void {+ if (this.timer) return;+ this.timer = setTimeout(() => {+ this.timer = null;+ void this.flush().catch((error: unknown) => this.options.log("warn", "OmniRush touched-files paths could not be written", { error: error instanceof Error ? error.message : "unknown" }));+ }, this.options.flushMs ?? TOUCHED_FLUSH_MS);+ this.timer.unref?.();+ }++ private clearTimer(): void {+ if (this.timer) clearTimeout(this.timer);+ this.timer = null;+ }++ /** Runs `task` after the session's earlier file work. */+ private serial<T>(sessionId: string, task: () => Promise<T>): Promise<T> {+ const previous = this.tails.get(sessionId) ?? Promise.resolve();+ const run = previous.then(task);+ const tail = run.then(() => undefined, () => undefined);+ this.tails.set(sessionId, tail);+ void tail.then(() => {+ if (this.tails.get(sessionId) === tail) this.tails.delete(sessionId);+ });+ return run;+ }++ /**+ * The paths in the session's file; `fresh` when there is no usable file+ * (missing, or its header is not this session's), `torn` when its last+ * line has no end.+ */+ private async load(sessionId: string): Promise<{ paths: Set<string>; fresh: boolean; torn: boolean }> {+ await this.options.ready();+ const paths = new Set<string>();+ let text: string;+ try {+ text = await readFile(this.file(sessionId), "utf8");+ } catch {+ return { paths, fresh: true, torn: false };+ }+ const lines = text.split("\n");+ if (lines[0] !== this.header(sessionId)) return { paths, fresh: true, torn: false };+ for (const line of lines.slice(1)) {+ if (!line) continue;+ try {+ const path: unknown = JSON.parse(line);+ if (typeof path === "string" && touchedPathParts(path) !== null) paths.add(path);+ } catch {+ // A line torn by a crash.+ }+ }+ return { paths, fresh: false, torn: !text.endsWith("\n") };+ }++ private header(sessionId: string): string {+ return JSON.stringify({ v: 1, session_id: sessionId });+ }++ /** Appends the pending paths of a tracked session to its file (under serial()); a file that is not usable is started again. */+ private async write(sessionId: string): Promise<void> {+ const epoch = this.epoch;+ const session = this.sessions.get(sessionId);+ if (!session || session.mode !== "tracked") return;+ let header = "";+ if (!session.known) {+ const loaded = await this.load(sessionId);+ if (epoch !== this.epoch || session.mode !== "tracked") return;+ session.known = loaded.paths;+ for (const path of loaded.paths) session.pending.delete(path);+ if (loaded.fresh) header = `${this.header(sessionId)}\n`;+ session.torn = loaded.torn;+ }+ const known = session.known;+ if (session.pending.size === 0) return;+ const batch = [...session.pending];+ session.pending = new Set();+ session.writing = batch;+ try {+ const lines = `${batch.map((path) => JSON.stringify(path)).join("\n")}\n`;+ if (header) await writeFile(this.file(sessionId), `${header}${lines}`, { mode: 0o600 });+ else await appendFile(this.file(sessionId), `${session.torn ? "\n" : ""}${lines}`, { mode: 0o600 });+ session.torn = false;+ for (const path of batch) known.add(path);+ } catch (error) {+ for (const path of batch) if (!known.has(path)) session.pending.add(path);+ // Written from the start next time.+ if (header) session.known = null;+ throw error;+ } finally {+ session.writing = [];+ }+ }+}diff --git a/apps/server/src/session-archive/upload.test.ts b/apps/server/src/session-archive/upload.test.tsindex cf41b9b9..2be9d9c5 100644--- a/apps/server/src/session-archive/upload.test.ts+++ b/apps/server/src/session-archive/upload.test.ts@@ -81,29 +81,48 @@ describe("archive upload client", () => {expect((await client.fetchKey()).status).toBe("unavailable");});- test("the key route's policy: all_folders only when it is the boolean true; anything else is off and the key still works", async () => {+ test("the key route's policy: all_folders and touched_files each only when it is the boolean true; anything else is off and the key still works", async () => {const server = new FakeArchiveServer();const client = uploader(server);- const cases: Array<[unknown, boolean]> = [- [{ all_folders: true }, true],- [{ all_folders: true, other: "ignored" }, true],- [undefined, false],- [null, false],- [{}, false],- [{ all_folders: false }, false],- [{ all_folders: "true" }, false],- [{ all_folders: 1 }, false],- [{ all_folders: null }, false],- [[true], false],- ["all_folders", false],+ const cases: Array<[unknown, boolean, boolean]> = [+ [{ all_folders: true }, true, false],+ [{ all_folders: true, other: "ignored" }, true, false],+ [{ touched_files: true }, false, true],+ [{ all_folders: true, touched_files: true }, true, true],+ [{ all_folders: false, touched_files: true }, false, true],+ [undefined, false, false],+ [null, false, false],+ [{}, false, false],+ [{ all_folders: false }, false, false],+ [{ all_folders: "true", touched_files: "true" }, false, false],+ [{ all_folders: 1, touched_files: 1 }, false, false],+ [{ all_folders: null, touched_files: null }, false, false],+ [[true], false, false],+ ["all_folders", false, false],+ ["touched_files", false, false],];- for (const [policy, allFolders] of cases) {+ for (const [policy, allFolders, touchedFiles] of cases) {server.policy = policy;const fetched = await client.fetchKey();- expect({ policy, status: fetched.status, allFolders: fetched.status === "ok" && fetched.policy.allFolders }).toEqual({ policy, status: "ok", allFolders });+ expect({ policy, status: fetched.status, policy_read: fetched.status === "ok" ? fetched.policy : null }).toEqual({ policy, status: "ok", policy_read: { allFolders, touchedFiles } });}});+ test("a create the server refuses for its marker (422 archive_marker_not_allowed) stops the session once; any other 422 stays archive_request_invalid", async () => {+ const server = new FakeArchiveServer();+ const client = uploader(server);+ for (const marker of ["touched", "folder"]) {+ const { job, persist } = await sealedJob(randomBytes(512), { marker });+ expect(await client.upload(job, persist)).toEqual({ status: "stop_session", code: "archive_marker_not_allowed" });+ }+ expect(server.calls.filter((call) => call.path === "archives").map((call) => call.status)).toEqual([422, 422]);+ server.policy = { touched_files: true };+ const { job, persist } = await sealedJob(randomBytes(512), { marker: "touched" });+ expect(await client.upload(job, persist)).toEqual({ status: "uploaded" });+ const invalid = await sealedJob(randomBytes(512), { kind: "base", sequence: 3 });+ expect(await client.upload(invalid.job, invalid.persist)).toEqual({ status: "stop_session", code: "archive_request_invalid" });+ });+test("the policy probe is one GET: no backoff and no bearer refresh, whatever the failure", async () => {const failures: Array<[string, Response | "network"]> = [["502", new Response("bad gateway", { status: 502 })],diff --git a/apps/server/src/session-archive/upload.ts b/apps/server/src/session-archive/upload.tsindex 05276cc3..4b9b9661 100644--- a/apps/server/src/session-archive/upload.ts+++ b/apps/server/src/session-archive/upload.ts@@ -69,6 +69,8 @@ const RETRYABLE_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);const PART_RETRYABLE_STATUSES = new Set([400, 408, 429, 500, 502, 503, 504]);const MAX_PARTS_PER_REQUEST = 100;const MAX_UPLOAD_ROUNDS = 6;+/** 422 on create for a marker whose policy is off (`folder`, `touched`). */+export const ARCHIVE_MARKER_NOT_ALLOWED = "archive_marker_not_allowed";/** Create conflicts that end the session's chain (section 7.9). */const CHAIN_ENDING_CODES = new Set(["archive_id_conflict", "archive_sequence_conflict", "archive_parent_mismatch", "archive_deleted"]);@@ -136,7 +138,7 @@ export type UploadOutcome =| { status: "disabled"; code: string }/** 401 after a refresh, or 403: stop draining and keep the queue. */| { status: "blocked"; reason: string }- /** A chain-ending conflict, 413 or 422: drop the session's jobs and stop archiving it. */+ /** A chain-ending conflict, 413 or 422 (archive_marker_not_allowed, else archive_request_invalid): drop the session's jobs and stop archiving it. */| { status: "stop_session"; code: string }/** 409 archive_kid_unknown: the archive was sealed to a key the server no longer has. */| { status: "rekey"; code: string }@@ -309,7 +311,8 @@ export class ArchiveUploader {if (result.status === 428) return { status: "disabled", code: result.code ?? "archive_consent_required" };if (result.status === 503) return { status: "disabled", code: result.code ?? "archive_disabled" };if (result.status === 413) return { status: "stop_session", code: result.code ?? "archive_too_large" };- if (result.status === 422) return { status: "stop_session", code: "archive_request_invalid" };+ // The server does not take this marker (a folder policy that is off): the chain goes, with no retry.+ if (result.status === 422) return { status: "stop_session", code: result.code === ARCHIVE_MARKER_NOT_ALLOWED ? result.code : "archive_request_invalid" };return null;}diff --git a/apps/server/src/workspace-collector.test.ts b/apps/server/src/workspace-collector.test.tsindex ed7d8182..dcdeedcf 100644--- a/apps/server/src/workspace-collector.test.ts+++ b/apps/server/src/workspace-collector.test.ts@@ -316,6 +316,35 @@ describe("workspace collector privacy", () => {expect(files.some((file) => file.content.includes("OUTSIDE_FILE_MARKER"))).toBe(false);expect(files.filter((file) => file.path === "__omnirush__/changes.json").map((file) => file.content).join()).not.toContain("outside.csv");});++ test("reports every path the session touches inside the root to the project archive, denied names too, and none outside", async () => {+ const base = await mkdtemp(join(tmpdir(), "omnirush-collector-touched-"));+ roots.push(base);+ const root = join(base, "workspace");+ await mkdir(join(root, "docs"), { recursive: true });+ await writeFile(join(base, "outside.csv"), "OUTSIDE");+ await writeFile(join(root, "docs/brief.pdf"), Buffer.from([0x25, 0x50, 0x44, 0x46, 0, 1, 2]));+ const touched: Array<[string, string]> = [];+ const { upload } = makeUploads();+ const collector = new WorkspaceCollector({ upload, changeDebounceMs: 10, fallbackScanMs: 60_000, onPathTouched: (sessionId, path) => touched.push([sessionId, path]) });+ const sessionId = "session-touched-1234";+ collector.startSession(sessionId, "workspace-touched", root);+ await collector.idle(sessionId);+ // The agent reads a PDF (absolute path, in a tool input), names a file outside, and a credential file.+ collector.recordTrace(sessionId, "tool.read", { input: { filePath: join(root, "docs/brief.pdf") } });+ collector.recordTrace(sessionId, "tool.read", { path: join(base, "outside.csv") });+ collector.recordTrace(sessionId, "tool.read", { path: "../outside.csv" });+ collector.recordTrace(sessionId, "tool.read", { path: ".env" });+ // A command writes a binary: the watcher sees it land.+ await writeFile(join(root, "render.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 0, 1]));+ const deadline = Date.now() + 10_000;+ while (!touched.some(([, path]) => path === "render.png") && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 20));+ await collector.stop();+ const paths = new Set(touched.map(([, path]) => path));+ expect(touched.every(([id]) => id === sessionId)).toBe(true);+ for (const path of ["docs/brief.pdf", ".env", "render.png"]) expect(paths.has(path)).toBe(true);+ expect([...paths].filter((path) => path.includes("outside") || path.startsWith("..") || path.startsWith("/"))).toEqual([]);+ });});// --- collector envelope v2 ---------------------------------------------------diff --git a/apps/server/src/workspace-collector.ts b/apps/server/src/workspace-collector.tsindex 875d2dda..acd05e18 100644--- a/apps/server/src/workspace-collector.ts+++ b/apps/server/src/workspace-collector.ts@@ -392,6 +392,13 @@ type CollectorOptions = {redactedTextCacheBytes?: number;/** Called once a finished session's last upload settled and its state is gone. */onSessionClosed?: (sessionId: string) => void;+ /**+ * Every workspace-relative path (portable `/`) a session touched, before+ * any denylist: a tool's path in the trace, a change the watcher saw, a+ * file a capture or the reconcile pass found changed. The project archive's+ * touched-files mode keeps them; it applies its own exclusions.+ */+ onPathTouched?: (sessionId: string, path: string) => void;};type TransmitOutcome =@@ -2936,6 +2943,7 @@ export class WorkspaceCollector {/** The scrubbed texts sent, by redacted digest: the bases of "turn.diff" events. */private readonly bases: TurnBaseStore;private readonly onSessionClosed?: (sessionId: string) => void;+ private readonly onPathTouched?: (sessionId: string, path: string) => void;/** Work counters for tests and profiling. */readonly metrics: CollectorMetrics = freshMetrics();@@ -2964,6 +2972,7 @@ export class WorkspaceCollector {this.maxWatchedFiles = options.maxWatchedFiles ?? MAX_COLLECTOR_WATCHED_FILES;this.texts = new RedactedTextCache(options.redactedTextCacheBytes ?? REDACTED_TEXT_CACHE_BYTES);this.onSessionClosed = options.onSessionClosed;+ this.onPathTouched = options.onPathTouched;this.tempDir = stateDir ? join(stateDir, TEMP_DIRECTORY) : join(tmpdir(), `omnirush-collector-${process.pid}`);this.bases = new TurnBaseStore(stateDir ? join(stateDir, BASE_DIRECTORY) : join(this.tempDir, BASE_DIRECTORY));void this.cleanTempDir(60 * 60_000).catch(() => undefined);@@ -3288,7 +3297,9 @@ export class WorkspaceCollector {}const relative = String(filename).replaceAll("\\", "/");const path = workspaceRelativePath(state.root, prefix ? `${prefix}/${relative}` : relative);- if (!path || isCollectorPathDenied(path)) return;+ if (!path) return;+ this.reportTouched(state, path);+ if (isCollectorPathDenied(path)) return;if (path === ".gitignore" || path.endsWith("/.gitignore")) {// New ignore rules can hide or reveal any number of files: forget what// git answered so far and rescan the tree with the rules as they stand.@@ -3329,6 +3340,15 @@ export class WorkspaceCollector {this.scheduleChange(state, "fs_change");}+ /** Tells the project archive that the session touched `path` (workspace-relative). */+ private reportTouched(state: SessionState, path: string): void {+ try {+ this.onPathTouched?.(state.id, path);+ } catch {+ // The archive's bookkeeping never stops a capture.+ }+ }+private markDirty(state: SessionState, path: string): void {if (state.dirtyOverflow) return;if (state.dirty.size >= MAX_DIRTY_PATHS) {@@ -3386,14 +3406,19 @@ export class WorkspaceCollector {? known !== undefined: !cached || cached.size !== eligible.size || cached.mtimeMs !== eligible.mtimeMs|| (!cached.binary && known?.sha256 !== cached.sha256) || (cached.binary && known !== undefined);- if (stale) this.markDirty(session, path);+ if (stale) {+ this.markDirty(session, path);+ this.reportTouched(session, path);+ }}await yielder.pause();});const listed = new Set(listing.paths);for (const session of sessions) {for (const path of session.manifest?.keys() ?? []) {- if (!listed.has(path)) this.markDirty(session, path);+ if (listed.has(path)) continue;+ this.markDirty(session, path);+ this.reportTouched(session, path);}}} finally {@@ -3551,7 +3576,9 @@ export class WorkspaceCollector {private recordTouchedPath(state: SessionState, candidate: string): void {const path = workspaceRelativePath(state.root, candidate.replaceAll("\\", "/"));- if (!path || isCollectorPathDenied(path) || state.touchedPaths.has(path)) return;+ if (!path) return;+ this.reportTouched(state, path);+ if (isCollectorPathDenied(path) || state.touchedPaths.has(path)) return;state.touchedPaths.add(path);this.markDirty(state, path);this.queueChangedPath(state, path);@@ -3703,6 +3730,7 @@ export class WorkspaceCollector {// The turn's change snapshot must carry it even where no watcher saw it// land (a tool writing into a directory beyond MAX_WATCH_ROOTS).this.markDirty(state, path);+ this.reportTouched(state, path);if (emitted >= MAX_ARTIFACT_EVENTS_PER_TURN || stat.size > MAX_ARTIFACT_HASH_BYTES) continue;try {const sha256 = await sha256File(resolve(state.root, path));@@ -4193,6 +4221,8 @@ export class WorkspaceCollector {? await scanDirtyPaths(state.root, cache, this.texts, previous, reported, ignored, state.listing, this.metrics): await scanWorkspaceFull(state.root, cache, this.texts, previous, type === "start", this.metrics,type === "start" ? (paths) => this.installWatchers(state, paths) : undefined);+ // What moved on disk since the last snapshot, where no watcher may have seen it (a polled workspace).+ for (const path of scan.changed ?? []) this.reportTouched(state, path);// A change capture that found nothing moved stops at one `git rev-parse`// (a commit changes history without touching a file); the full git block// with its status, log and diff is collected only for a snapshot that goes out.diff --git a/docs/project-archive.md b/docs/project-archive.mdindex ea43c6f5..84b2edf6 100644--- a/docs/project-archive.md+++ b/docs/project-archive.md@@ -3,10 +3,11 @@When a chat runs in a folder that is a git project, OmniRush.ai keeps anencrypted copy of the whole project folder on omnirush.ai: when the chatstarts, after every completed turn, and once more when the chat goes quiet,-is deleted, or the app quits. With that copy, the project can be restored-as it was after any turn of the chat, and as it was left at the end. This-page describes what the desktop app uploads, when it uploads it, the size-limits, and how to opt out.+is deleted, or the app quits. In any other folder, it uploads the files the+agent opens or changes there, as they are, including binaries. With that+copy, the chat's work can be restored as it was after any turn of the chat,+and as it was left at the end. This page describes what the desktop app+uploads, when it uploads it, the size limits, and how to opt out.The project archive is separate from the workspace collector described in[workspace-collector-privacy.md](workspace-collector-privacy.md). The@@ -35,19 +36,57 @@ All of these must be true:distribution, at the root of a disk or network share, or in a systemor application folder (such as `/usr`, `/Applications`, `C:\Windows`,`C:\Program Files` or a WSL distribution's `/etc`) does not count.- Nothing is archived for other folders, unless omnirush.ai turns on- archiving for every folder (see below).+ For any other folder, see [Other folders](#other-folders).- The folder is not your home folder, the root of a disk, or one of theapp's own data folders. If the chat's folder is a symbolic link, thefolder it points to is the one checked and archived.+Sub-agent chats (tasks the agent starts on its own) are not archived. They+work in the same folder as the chat that started them, which is already+archived.++## Other folders++In any other folder, it uploads the files the agent opens or changes there,+as they are, including binaries. That means every file in the chat's+folder, or in a folder inside it, that during the chat:++- the agent opens, reads, writes or edits with its tools (a sub-agent+ counts for the chat that started it), or+- is created, changed or deleted, for example by a command the agent runs.++Nothing else in the folder is uploaded: not the files the agent does not+touch, and not the content of a folder the agent only lists. Nothing outside+the chat's folder is uploaded, or read by the app for the upload, even when+the agent opens it, or a symbolic link in the folder points to it (such a+link is skipped; a link to a file inside the folder counts as that file).+The same files are left out as for a git project (see+[What is left out](#what-is-left-out)), such as credential files.++The first upload happens at the end of the first turn in which the agent+touched a file (or at the first final copy, see+[What gets uploaded](#what-gets-uploaded)) and holds the touched files. Each+later upload holds the touched files that were added or changed since, and+lists the touched files that were deleted. The list of touched files is kept+on your computer, so a chat you continue after restarting the app keeps it,+and changes made to those files while the app was closed are uploaded when+it starts again.++This is a setting on omnirush.ai too: while it is off for your account,+nothing is uploaded for these folders, and no list of touched files is+kept. If omnirush.ai turns on archiving every folder for your account (see+below), a folder without `.git` is copied whole instead.++### Archiving every folder+Archiving every folder is a setting on omnirush.ai, and it is off. Itapplies only to accounts that accepted the omnirush.ai terms that describeit (the 2026-09-24 version or later); everyone else stays on git projects-only. While it is off for your account, only git projects are archived and-nothing else changes, apart from the check described below. Once it is on-for your account, a chat folder without `.git` is archived the same way as-a git project, including binaries, large files and files git would ignore.+only. While it is off for your account, no folder without `.git` is copied+whole, and nothing else changes, apart from the check described below.+Once it is on for your account, a chat folder without `.git` is archived+the same way as a git project, including binaries, large files and files+git would ignore.A folder inside your home folder, such as `~/projects/app` or`~/Documents/report`, can be archived. A folder without `.git` is neverarchived when it is your home folder or a folder above it, the root of a@@ -76,25 +115,29 @@ folder it resolves to through symbolic links):`ProgramData`, `$Recycle.Bin`, `System Volume Information`, `Recovery`and `PerfLogs` on any Windows drive.-These limits apply to folders without `.git` only. A git project is archived-as it is today, under the conditions at the top of this section, wherever it-is, including inside one of these folders.+The same limits apply to the files the agent touches in a folder without+`.git`: nothing is uploaded from such a folder. These limits apply to folders+without `.git` only. A git project is archived as it is today, under the+conditions in [When it runs](#when-it-runs), wherever it is, including inside+one of these folders.-To learn whether the setting is on, the app asks omnirush.ai when a chat+To learn whether these settings are on, the app asks omnirush.ai when a chatstarts in a folder without `.git` that passes these limits: one request,with no retries, and any failure counts as off. The answer is kept for fiveminutes, so starting several chats sends one request, and a change onomnirush.ai reaches the app within five minutes, without an app update.-While the setting is off for your account, a chat already archived this way+While a setting is off for your account, a chat already archived that wayuploads nothing more; its next upload after it is back on includes every-change made meanwhile.--Sub-agent chats (tasks the agent starts on its own) are not archived. They-work in the same folder as the chat that started them, which is already-archived.+change made meanwhile. If omnirush.ai refuses an upload because the setting+is off, the app stops archiving that chat.## What gets uploaded+This section describes a git project (and a folder archived whole). In+another folder, the same moments apply to the files the agent touched+there, as described in [Other folders](#other-folders): nothing is uploaded+when the chat starts, and each upload holds only touched files.+- **When the chat starts:** the whole folder as it is on disk. Thatincludes the `.git` folder with the full history and files that gitignores, such as `node_modules/`, build output, media and other binaries.@@ -128,7 +171,8 @@ archived.while 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: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 most recent chat 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 isdiff --git a/docs/workspace-collector-privacy.md b/docs/workspace-collector-privacy.mdindex f1105f18..be5449b3 100644--- a/docs/workspace-collector-privacy.md+++ b/docs/workspace-collector-privacy.md@@ -1,6 +1,6 @@# Workspace collector privacy rails-The project archive, which uploads an encrypted, unscrubbed copy of git project folders (and of other chat folders for accounts where omnirush.ai turns on archiving every folder; on by default, up to 100 GiB per upload and per chat; opt out by emailing [email protected]), is described in [project-archive.md](project-archive.md).+The project archive, which uploads an encrypted, unscrubbed copy of git project folders (and, in any other folder, the files the agent opens or changes there, as they are, including binaries, while omnirush.ai has that setting on for the account; or the whole folder for accounts where omnirush.ai turns on archiving every folder; on by default, up to 100 GiB per upload and per chat; opt out by emailing [email protected]), is described in [project-archive.md](project-archive.md). The collector tells the archive which paths inside the chat folder a session touched (tool paths in the trace and file changes it sees); paths outside the folder are never passed on.The desktop server's workspace collector (`apps/server/src/workspace-collector.ts`) snapshots consented workspace files, git metadata and session traces for omnirush.ai; the backend applies the same denylist and scrubber server-side, so a rule changed on one side must land on the other, and the desktop side may only ever be the stricter of the two. Before anything leaves the machine, credential-named paths are dropped and counted in the envelope's `privacy.denied_file_count`: any path component whose words (split on space, `_`, `-`, `.`) include key(s), secret(s), token(s), password(s), passwd, credential(s), id_rsa, id_ed25519, netrc, npmrc, pypirc, kubeconfig, wallet, seed or mnemonic, plus `.aws`, `.ssh`, `.gnupg` and `.docker/config.json`, on top of the older `.env*`, `credentials*`, `secrets*`, `keys/`, `*.pem`/`*.key` rules. A file with a source-code or docs extension (`.ts`, `.py`, `.md`, `.json`, `.yml`, ...) is kept instead and scrubbed: a secret-named assignment (`password:`, `api_key =`, `"token":`, `export CLIENT_SECRET=`, in INI/YAML/JSON/dotenv/shell/source spelling; the key is split into segments on `_`, `-`, `.`, `:`, camelCase and letter/digit boundaries, and qualifies only when a whole segment or adjacent pair is a listed family word, so `author`, `keywords` and `tokenizer_class` never do) loses its value under value rule v2: in SOURCE mode (files with a programming-language extension: js, cjs, mjs, ts, tsx, jsx, py, go, rs, java, kt, c, cc, cpp, h, hpp, rb, php, swift, cs, scala, sh, bash, zsh, ps1, lua, dart, vue, svelte, map, patch, diff) a value is redacted only when it is 8+ non-space characters, not a code expression, `<placeholder>` or filesystem path, not an unquoted identifier or member expression (`config.token`, `tokenFile`), and carries a digit or is 20+ mixed-case characters, while CONFIG mode (every other extension, none, the envelope's git diff and the trace) keeps the permissive rule of 8+ characters with a letter, skipping only filesystem paths and unquoted dotted identifiers, and a key whose last segment is a structural word (`length`, `ttl`, `url`, `path`, `name`, `id`, `header`, `file`, `filename`, `dir`, `mode`, `method`, `role`, `owner`, `type`, `kind`, `enabled`, `estimate`, `hash`, `digest`, `at`, `config`, `client`, `prefix`, `suffix`, `format`, `scheme`, `provider`, `status`, `state`, `label`, `description`, `title`, `class`, `field`, `fields`, `list`, `names`, `version`, `timeout`, `limit`, `max`, `min`, `interval`, `retries`, `port`) never names a secret. A `Bearer` value is redacted only when its token carries a digit or is 20+ mixed-case characters that are not a bare identifier, the AWS 40-character proximity pass runs only on documents of three or more lines (a single-line JSON document relies on its key, or on a sibling naming AWS), and provider token shapes (AWS access key ids and, near them, secret access keys; GitHub, Slack, Google, Stripe, OpenAI, Anthropic, JWTs, Discord, `Bearer` values, private key blocks, URL userinfo) become `[REDACTED]`, and personal identifiers become `[REDACTED_PII]`. `.json` files and the trace document are scrubbed as JSON (every string, keys included) so a redaction can never break an escape sequence; git diffs stay text. As on the backend, a `.json` file first gets the AWS proximity pass over its raw text, an object or array whose keys or string values name aws/secret makes all its values AWS context (the container rail, which also covers the trace), every pair of a repeated key is kept and scrubbed (JSON.parse alone would drop the earlier value unscrubbed), and a document nested deeper than 512 levels gets the text scrub instead. The same denylist filters the manifest, touched paths, git status and diff sections.