Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(archive): refuse credential and app-data folders, probe the all-folders policy once, send folders without git

ogsamrat47750a65e504
diff --git a/apps/server/src/capture-client.test.ts b/apps/server/src/capture-client.test.ts
index 1c641568..442f37e8 100644
--- a/apps/server/src/capture-client.test.ts
+++ b/apps/server/src/capture-client.test.ts
@@ -286,6 +286,7 @@ describe("capture worker", () => {
await writeFile(join(root, "plan.md"), "# plan\n");
const archive = new FakeArchiveServer();
archive.policy = policy;
+ const refreshFlags: unknown[] = [];
const capture = service({
stateDir: await tempDir("state"),
collector: { upload: uploadSink().upload },
@@ -293,11 +294,14 @@ describe("capture worker", () => {
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 }),
- }),
+ request: (path, init) => {
+ refreshFlags.push("refresh" in init ? init.refresh : "absent");
+ return 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,
@@ -314,6 +318,8 @@ describe("capture worker", () => {
} else {
expect(archive.callPaths()).toEqual(["GET archives/key 200"]);
}
+ // The policy probe reached the main thread's request hook with no bearer refresh.
+ expect(refreshFlags[0]).toBe(false);
await capture.stop();
}
}, 60_000);
diff --git a/apps/server/src/capture-client.ts b/apps/server/src/capture-client.ts
index 1b6fd809..3ca52271 100644
--- a/apps/server/src/capture-client.ts
+++ b/apps/server/src/capture-client.ts
@@ -423,7 +423,12 @@ class CaptureClient implements CaptureService {
}
case "archiveRequest": {
if (!archive.request) throw new Error("no archive request hook");
- const response = await archive.request(request.path, { method: request.method, ...(request.body !== undefined ? { body: request.body } : {}), signal });
+ const response = await archive.request(request.path, {
+ method: request.method,
+ ...(request.body !== undefined ? { body: request.body } : {}),
+ ...(request.refresh === false ? { refresh: false as const } : {}),
+ signal,
+ });
return { kind: "result", id, ok: true, response: await serializeResponse(response) };
}
case "fetch": {
diff --git a/apps/server/src/capture-protocol.ts b/apps/server/src/capture-protocol.ts
index 704474be..35ad185a 100644
--- a/apps/server/src/capture-protocol.ts
+++ b/apps/server/src/capture-protocol.ts
@@ -39,7 +39,7 @@ export type CaptureCall = { [M in CaptureCallName]: { kind: "call"; id: number |
export type HostRequest =
| { type: "collect"; sessionId: string; body: Uint8Array<ArrayBuffer> }
| { type: "refreshAccessToken" }
- | { type: "archiveRequest"; path: string; method: "GET" | "POST"; body?: string }
+ | { type: "archiveRequest"; path: string; method: "GET" | "POST"; body?: string; refresh?: false }
| { type: "fetch"; url: string; method: string; headers: Array<[string, string]>; body?: Uint8Array<ArrayBuffer> | string };
export type RequestChannel = "collector" | "archive";
diff --git a/apps/server/src/capture-worker.ts b/apps/server/src/capture-worker.ts
index d5546a58..c2f8d9a3 100644
--- a/apps/server/src/capture-worker.ts
+++ b/apps/server/src/capture-worker.ts
@@ -21,6 +21,7 @@ import {
type RequestResult,
type ToWorker,
} from "./capture-protocol.js";
+import type { ArchiveApiRequestInit } from "./session-archive/upload.js";
function mainThreadPort(): MessagePort {
if (!parentPort) throw new Error("capture-worker runs as a worker thread only");
@@ -125,12 +126,13 @@ const host = new CaptureHost({
folderGate: init.archive.folderGate,
...(init.archive.request
? {
- request: async (path: string, requestInit: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }) => {
+ request: async (path: string, requestInit: ArchiveApiRequestInit) => {
const result = await ask("archive", {
type: "archiveRequest",
path,
method: requestInit.method,
...(requestInit.body !== undefined ? { body: requestInit.body } : {}),
+ ...(requestInit.refresh === false ? { refresh: false as const } : {}),
}, requestInit.signal);
if (!result.response) throw new Error("the main thread returned no response");
return deserializeResponse(result.response);
diff --git a/apps/server/src/omnirush-gateway-broker.test.ts b/apps/server/src/omnirush-gateway-broker.test.ts
index 18ed590c..2f093b79 100644
--- a/apps/server/src/omnirush-gateway-broker.test.ts
+++ b/apps/server/src/omnirush-gateway-broker.test.ts
@@ -661,6 +661,28 @@ describe("OmniRush gateway broker project archive requests", () => {
]);
});
+ test("refresh: false (the archiver's policy probe) sends once and returns a 401 as it is, with no refresh", async () => {
+ const calls: string[] = [];
+ const refreshCalls: string[] = [];
+ const broker = new OmniRushGatewayBroker({
+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1/", accessToken: "access-1", refreshToken: "refresh-1" },
+ engineToken: "local-engine-token",
+ fetch: async (input, init) => {
+ const url = String(input);
+ if (url.endsWith("/device/refresh")) {
+ refreshCalls.push(url);
+ return Response.json({ access_token: "access-2", refresh_token: "refresh-2", gateway_url: "https://gateway.example/omnirush/v1" });
+ }
+ calls.push(`${init?.method ?? "GET"} ${url} ${new Headers(init?.headers).get("authorization")}`);
+ return Response.json({ detail: "invalid_token" }, { status: 401 });
+ },
+ });
+ const response = await broker.archiveRequest("archives/key", { method: "GET", refresh: false });
+ expect(response.status).toBe(401);
+ expect(calls).toEqual(["GET https://gateway.example/omnirush/archives/key Bearer access-1"]);
+ expect(refreshCalls).toEqual([]);
+ });
+
test("never leave the archive routes, and answer 401 without an account", async () => {
const urls: string[] = [];
const broker = new OmniRushGatewayBroker({
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index 792074ed..aaca07af 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -615,9 +615,11 @@ export class OmniRushGatewayBroker {
* A project archive API call for the session archiver (`archives/key`,
* `archives`, `archives/<id>/parts|complete|abort`), authenticated like
* collect(): the device bearer and the same bounded 401 refresh. S3 part
- * uploads never come through here; they go to their presigned URLs.
+ * uploads never come through here; they go to their presigned URLs. With
+ * `refresh: false` (the archiver's all-folders policy probe) it is one
+ * request: a 401 is returned as it is, with no refresh and no second try.
*/
- archiveRequest(path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }): Promise<Response> {
+ archiveRequest(path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal; refresh?: false }): Promise<Response> {
if (!ARCHIVE_API_PATH.test(path)) return Promise.resolve(Response.json({ error: "unsupported_archive_path" }, { status: 404 }));
return this.withDeviceBearer((state) => this.fetcher(apiUrl(state.gatewayUrl, path), {
method: init.method,
@@ -628,7 +630,7 @@ export class OmniRushGatewayBroker {
},
...(init.body === undefined ? {} : { body: init.body }),
...(init.signal ? { signal: init.signal } : {}),
- }));
+ }), { refresh: init.refresh !== false });
}
/**
@@ -644,11 +646,16 @@ export class OmniRushGatewayBroker {
}));
}
- /** Sends with the current device bearer; a 401 is retried bounded like handle(): adopt, then spend the adopted refresh token. */
- private async withDeviceBearer(send: (state: CredentialState) => Promise<Response>): Promise<Response> {
+ /**
+ * Sends with the current device bearer; a 401 is retried bounded like
+ * handle(): adopt, then spend the adopted refresh token. `refresh: false`
+ * sends once and returns whatever came back.
+ */
+ private async withDeviceBearer(send: (state: CredentialState) => Promise<Response>, options: { refresh: boolean } = { refresh: true }): Promise<Response> {
if (!this.state) return Response.json({ error: "omnirush_account_required" }, { status: 401 });
let spent = this.state.accessToken;
let response = await send(this.state);
+ if (!options.refresh) return response;
for (let round = 0; round < 2 && response.status === 401 && this.state; round += 1) {
const credentialAlreadyRotated = this.state.accessToken !== spent;
if (!credentialAlreadyRotated && !(await this.refresh(spent))) break;
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 338ae434..5436f70c 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -163,6 +163,7 @@ import { buildOpencodeProxyUrl, engineTarget } from "./collector-observer.js";
import { OmniRushGatewayBroker } from "./omnirush-gateway-broker.js";
import { startOmniRushModelCatalogSync } from "./omnirush-model-catalog-sync.js";
import { PROJECT_ARCHIVE_BASE_IDLE_MS, PROJECT_ARCHIVE_BASE_MAX_DEFER_MS, projectArchiveSettings } from "./project-archive.js";
+import type { ArchiveApiRequestInit } from "./session-archive/upload.js";
import { runtimeStorageDir } from "./runtime-db.js";
import pkg from "../package.json" with { type: "json" };
import constants from "../../../constants.json" with { type: "json" };
@@ -876,7 +877,7 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
folderGate: archiveSettings.folderGate,
...(archiveSettings.auth === "broker"
? {
- request: (path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }) => gatewayBroker.archiveRequest(path, init),
+ request: (path: string, init: ArchiveApiRequestInit) => gatewayBroker.archiveRequest(path, init),
refreshAccessToken: () => gatewayBroker.refreshAccessToken(),
}
: archiveSettings.auth === "environment"
diff --git a/apps/server/src/session-archive/README.md b/apps/server/src/session-archive/README.md
index 49d5d1c8..8000fb8a 100644
--- a/apps/server/src/session-archive/README.md
+++ b/apps/server/src/session-archive/README.md
@@ -212,7 +212,7 @@ void sessionArchiver.captureBase(sessionId, root, completedTurns).then(() => ses
- `root` is the folder the agent started in (`input.workspace.path`), as a real path: the gate `lstat`s it and refuses a symlinked root. The home directory, a filesystem root and the app's own directories are always refused.
- `completedTurns` is 0 for a new session. For a resumed session, it is the number of turns the session has already completed.
-- The call is cheap when there is nothing to do. With a base already captured, it only reads the session record, with no network. Without `.git`, it runs a few `lstat` calls: the root, then its parents up to the nearest `.git`, home or a guarded directory, and for a folder the all-folders gate would accept it fetches `GET /archives/key` for the policy. Otherwise it fetches `GET /archives/key`, which also checks the archive consent: a 428 means nothing is packed.
+- The call is cheap when there is nothing to do. With a base already captured, it only reads the session record, with no network. Without `.git`, it runs a few `lstat` calls: the root, then its parents up to the nearest `.git`, home or a guarded directory, and for a folder the all-folders refusals accept it reads the kept policy or sends one quick probe (at most one GET per 5 minutes, see "All folders" below). Otherwise it fetches `GET /archives/key`, which also checks the archive consent: a 428 means nothing is packed.
- Child (sub-agent) sessions share the parent's root and must not be archived. Call this for root sessions only.
- Keep a per-process `Set` of session ids already handed to `captureBase`. `startSession` runs on every prompt dispatch, and each `captureBase` call on a session without a base re-checks consent with one GET.
@@ -302,7 +302,14 @@ against a loopback sink).
- 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.
This only happens if a key is removed from `OMNIRUSH_ARCHIVE_PRIVATE_KEYS` while archives sealed to it are still queued.
-- **All folders (4.4).** GET /archives/key also carries `policy.all_folders` (off unless it is the boolean `true`; `policy.ts`). While it is off, archiving is git only, as before. While it is on, a session root with no `.git` entry at all is archived too, with marker and reason `folder`: the same whole-folder base and deltas, credential filter, pruning, consent and sign-out. `folderDetector` runs after the other detectors, so git still wins. It refuses, in every form of the root (as given and resolved through symlinks), a disk or share root (`/`, `C:\`, `\\server\share`, `/Volumes/<disk>`, `/mnt/<disk>`, `/media/<user>/<disk>`), the home directory and anything above it, the desktop's userData dir (`OMNIRUSH_DESKTOP_USER_DATA_DIR`, set by the desktop app) and anything inside or above it, and system and app directories and anything inside them (`/System`, `/Library`, `/Applications`, `/private`, `/usr`, `/bin`, `/sbin`, `/etc`, `/var`, `/opt`, `/proc`, `/sys`, `/dev`, `/boot`, `/lib`, `/lib64`, `/run`; `Windows`, `Program Files`, `Program Files (x86)` and `ProgramData` on any drive, and `<home>\AppData`). Inside the home directory only AppData and userData are refused. The policy is asked only for a root that passes, through the same GET the base then uses for its key: a folder session's start costs one GET when the policy is off, and the lifecycle asks once per session per app run. The server refuses `folder` archives while the policy is off (`422 archive_marker_not_allowed`), which stops such a session.
+- **All folders (4.4).** GET /archives/key also carries `policy.all_folders` (off unless it is the boolean `true`; `policy.ts`). The backend answers it per user: true only while `OMNIRUSH_ARCHIVE_ALL_FOLDERS` is on and the user's active consent is version 2026-09-24 or later. While it is off, archiving is git only, as before: the git path (gate, key fetch, archives) is unchanged, nothing is written for a folder without `.git`, and the only addition is the policy probe below. While it is on, a session root with no `.git` entry at all is archived too, with marker and reason `folder`: the same whole-folder base and deltas, credential filter, pruning, consent and sign-out, and `workspace.git` is always `null` (no git block, even when the folder sits inside a larger repository). `folderDetector` runs after the other detectors, so git still wins, and git roots get none of the refusals below.
+ - **Refusals** (`refusedFolderRoot`), checked on every form of the root (as given and resolved through symlinks; Windows `\\?\`, `\\.\` and `\\?\UNC\` prefixes and trailing dots and spaces folded; macOS and Windows without case) before the policy is asked:
+ - `root_too_broad`: a disk or share root (`/`, `C:\`, `\\server\share`, `/Volumes/<disk>`, `/mnt/<disk>`, `/media/<user>/<disk>`), the home directory and anything above it, and another account's folder beside home (`/Users/<other>`, `/Users/Shared`, `/home/<other>`, `C:\Users\Public`) itself;
+ - `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.
+ - **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.
- **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.
@@ -338,7 +345,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`.
-- `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.
+- `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;
- resume after a failed part;
@@ -346,6 +353,7 @@ cd apps/server && bun --conditions=development test src/session-archive
- 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 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;
- `archive_not_uploading` at complete;
@@ -362,7 +370,7 @@ cd apps/server && bun --conditions=development test src/session-archive
- `index.test.ts`:
- the whole folder: `.git/` and gitignored `node_modules/` and `dist/` present, `.env` and `id_rsa` absent unless `archiveIncludeCredentialFiles`; the base then a delta, each decrypted and checked;
- no delta when nothing changed, `stale_turn` and `exists`;
- - no `.git`: not archived with the policy off, absent or not a boolean (only the key read); with it on, archived whole with binaries and ignored-looking files, credentials left out, then a delta; home, userData and a system dir refused without a request; a git project still archived as git;
+ - no `.git`: not archived with the policy off, absent or not a boolean (only the key read); with it on, archived whole with binaries and ignored-looking files, credentials left out, then a delta; home, userData and a system dir refused without a request; sessions in `~/.gnupg`, `~/.aws/sso/cache`, `~/.docker`, `~/.ssh`, `~/.kube`, `~/.config/gh`, `~/Library/Keychains`, Chrome's profile, `AppData` and a `secrets` folder refused without a request; a git project still archived as git; the policy off with the API failing (502, 401, 428, 503, network): one probe per burst, no sleep, no refresh, nothing written or disabled, and the next git session unchanged; the answer kept for `POLICY_TTL_MS`, turned on and off on the server, deltas pausing and catching up, forgotten at sign-out; a folder inside a larger repository sent with `workspace.git: null`;
- consent off at the key route and during a drain;
- app restart resuming a half-uploaded archive;
- a crashed commit discarded;
diff --git a/apps/server/src/session-archive/detect.test.ts b/apps/server/src/session-archive/detect.test.ts
index 377de23c..f3a4f87b 100644
--- a/apps/server/src/session-archive/detect.test.ts
+++ b/apps/server/src/session-archive/detect.test.ts
@@ -299,7 +299,7 @@ describe("all-folders gate (4.4)", () => {
[windows, "C:\\Users\\sam\\AppData\\Roaming", "root_app_data"],
[windows, "C:\\Users\\sam\\AppData", "root_app_data"],
// AppData itself is refused with no userData dir known too.
- [{ ...windows, userData: [] }, "C:\\Users\\sam\\AppData", "root_system"],
+ [{ ...windows, userData: [] }, "C:\\Users\\sam\\AppData", "root_app_data"],
// System and app directories, and anything inside them.
...["/System", "/System/Library", "/Library", "/Library/Developer/x", "/Applications", "/Applications/Foo.app", "/usr", "/usr/local/src/x", "/bin", "/etc", "/var", "/var/folders/ab/T/x", "/private", "/private/tmp/x", "/library/x"].map((root): [FolderGateContext, string, "root_system"] => [mac, root, "root_system"]),
...["/usr", "/usr/share/x", "/etc", "/var/www/site", "/opt", "/opt/app", "/proc", "/proc/1", "/sys", "/sys/class"].map((root): [FolderGateContext, string, "root_system"] => [linux, root, "root_system"]),
@@ -313,18 +313,19 @@ describe("all-folders gate (4.4)", () => {
"C:\\Program Files (x86)\\App",
"C:\\ProgramData",
"C:\\ProgramData\\App",
- "C:\\Users\\sam\\AppData\\Local",
- "C:\\Users\\sam\\AppData\\Local\\Temp\\x",
- "C:\\Users\\sam\\appdata\\locallow",
+ "C:\\Windows.old\\Users\\sam",
+ "C:\\$Recycle.Bin\\S-1-5-21",
+ "C:\\Recovery",
].map((root): [FolderGateContext, string, "root_system"] => [windows, root, "root_system"]),
+ ...["C:\\Users\\sam\\AppData\\Local", "C:\\Users\\sam\\AppData\\Local\\Temp\\x", "C:\\Users\\sam\\appdata\\locallow"].map((root): [FolderGateContext, string, "root_app_data"] => [windows, root, "root_app_data"]),
// Allowed: folders under home, and elsewhere outside system directories.
[mac, "/Users/sam/omnirush.ai", null],
[mac, "/Users/sam/projects/x", null],
- [mac, "/Users/sam/Library/Mobile Documents/com~apple~CloudDocs/x", null],
+ [mac, "/Users/sam/Library/Mobile Documents/com~apple~CloudDocs/x", "root_app_data"],
[mac, "/Volumes/Backup/projects/x", null],
[mac, "/Users/other/x", null],
[linux, "/home/sam/omnirush.ai", null],
- [linux, "/home/sam/.local/share/x", null],
+ [linux, "/home/sam/.local/share/x", "root_app_data"],
[linux, "/srv/site", null],
[linux, "/tmp/scratch", null],
[linux, "/Library/x", "root_system"],
@@ -339,6 +340,118 @@ describe("all-folders gate (4.4)", () => {
}
});
+ test("credential and app-data folders are refused as the session folder, wherever they are and on every platform", () => {
+ const mac: FolderGateContext = { platform: "darwin", homes: ["/Users/sam"], userData: [] };
+ const linux: FolderGateContext = { platform: "linux", homes: ["/home/sam"], userData: [] };
+ const windows: FolderGateContext = { platform: "win32", homes: ["C:\\Users\\sam"], userData: [] };
+ const cases: Array<[FolderGateContext, string, ReturnType<typeof refusedFolderRoot>]> = [
+ // Credential stores in home, and anything inside them.
+ ...[".ssh", ".aws", ".aws/sso/cache", ".gnupg", ".kube", ".docker", ".config/gcloud", ".config/gcloud/legacy_credentials", ".password-store", ".azure", "Library/Keychains"].flatMap(
+ (dir): Array<[FolderGateContext, string, "root_credentials"]> => [[mac, `/Users/sam/${dir}`, "root_credentials"], [linux, `/home/sam/${dir}`, "root_credentials"]],
+ ),
+ [mac, "/Users/sam/.SSH", "root_credentials"],
+ [windows, "C:\\Users\\sam\\.ssh", "root_credentials"],
+ [windows, "C:\\Users\\sam\\.aws\\sso", "root_credentials"],
+ [windows, "C:\\Users\\sam\\.docker", "root_credentials"],
+ [windows, "C:\\Users\\sam\\.kube", "root_credentials"],
+ // The same names outside home: a backup disk, another account, a synced copy.
+ [mac, "/Volumes/Backup/Users/sam/.aws", "root_credentials"],
+ [mac, "/Volumes/Backup/Users/sam/.gnupg/private-keys-v1.d", "root_credentials"],
+ [mac, "/Volumes/Backup/Library/Keychains", "root_credentials"],
+ [mac, "/Users/sam/Dropbox/dotfiles/.ssh", "root_credentials"],
+ [linux, "/srv/backup/sam/.kube", "root_credentials"],
+ [linux, "/mnt/data/home/.config/gcloud", "root_credentials"],
+ [windows, "D:\\Backup\\.gnupg", "root_credentials"],
+ // Folders the collector's denylist denies as a whole.
+ ...["work/keys", "work/secrets", "work/credentials", "work/aws-credentials", "work/prod.secrets", "work/.env.d", "work/certs.pem", "work/app/node_modules/pkg", "work/repo/.git/hooks"].map(
+ (dir): [FolderGateContext, string, "root_credentials"] => [mac, `/Users/sam/${dir}`, "root_credentials"],
+ ),
+ [windows, "C:\\Users\\sam\\work\\Secrets", "root_credentials"],
+ // App data: every dot folder in home, macOS Library, Linux snap, Windows AppData, Library/Application Support anywhere.
+ ...[".config", ".config/gh", ".local", ".local/share/keyrings", ".cache/x", ".mozilla/firefox", ".Trash", ".vscode/extensions", ".npm"].map(
+ (dir): [FolderGateContext, string, "root_app_data"] => [linux, `/home/sam/${dir}`, "root_app_data"],
+ ),
+ ...["Library", "Library/Application Support/Google/Chrome/Default", "Library/Cookies", "Library/Containers/com.x", "Library/Group Containers/x", "library/mail", ".config/gh", ".Trash"].map(
+ (dir): [FolderGateContext, string, "root_app_data"] => [mac, `/Users/sam/${dir}`, "root_app_data"],
+ ),
+ [linux, "/home/sam/snap/firefox/common", "root_app_data"],
+ [linux, "/home/sam/Library/x", null],
+ [windows, "C:\\Users\\sam\\.vscode", "root_app_data"],
+ [windows, "C:\\Users\\sam\\AppData\\Roaming\\gcloud", "root_app_data"],
+ [windows, "D:\\Backup\\AppData\\Roaming", "root_app_data"],
+ [mac, "/Volumes/Backup/Users/sam/Library/Application Support/x", "root_app_data"],
+ // Other accounts and shared folders beside home: refused themselves, and their app data.
+ [mac, "/Users/other", "root_too_broad"],
+ [mac, "/Users/Shared", "root_too_broad"],
+ [mac, "/Users/other/Library/Cookies", "root_app_data"],
+ [mac, "/Users/other/.config", "root_app_data"],
+ [mac, "/Users/other/.ssh", "root_credentials"],
+ [linux, "/home/other", "root_too_broad"],
+ [linux, "/home/other/.local/share", "root_app_data"],
+ [windows, "C:\\Users\\Public", "root_too_broad"],
+ [windows, "C:\\Users\\Default\\AppData\\Local", "root_app_data"],
+ [windows, "C:\\Users\\other\\.ssh", "root_credentials"],
+ // Still allowed: ordinary folders, including names the denylist only checks on files.
+ [mac, "/Users/sam/work/token-service", null],
+ [mac, "/Users/sam/work/keyboard-firmware", null],
+ [mac, "/Users/sam/Documents/report", null],
+ [mac, "/Users/sam/Library-notes", null],
+ [mac, "/Users/Shared/project", null],
+ [mac, "/Users/other/project", null],
+ [linux, "/home/sam/work/dotfiles-site", null],
+ [windows, "C:\\Users\\sam\\work\\x", null],
+ ];
+ for (const [context, root, expected] of cases) {
+ expect({ root, refused: refusedFolderRoot(root, context) }).toEqual({ root, refused: expected });
+ }
+ });
+
+ test("Windows device and long paths, and names with trailing dots or spaces, are refused like their plain form", () => {
+ const windows: FolderGateContext = { platform: "win32", homes: ["C:\\Users\\sam"], userData: [] };
+ const cases: Array<[string, ReturnType<typeof refusedFolderRoot>]> = [
+ ["\\\\?\\C:\\Users\\sam", "root_too_broad"],
+ ["\\\\?\\c:\\users\\SAM\\", "root_too_broad"],
+ ["\\\\.\\C:\\", "root_too_broad"],
+ ["\\\\?\\UNC\\server\\share", "root_too_broad"],
+ ["\\\\?\\UNC\\server\\share\\", "root_too_broad"],
+ ["C:\\Users\\sam.", "root_too_broad"],
+ ["C:\\Users\\sam. .", "root_too_broad"],
+ ["C:\\Users\\sam \\", "root_too_broad"],
+ ["C:\\Users.\\sam", "root_too_broad"],
+ ["\\\\.\\C:\\Windows\\System32", "root_system"],
+ ["\\\\?\\C:\\Program Files.\\App", "root_system"],
+ ["\\\\?\\C:\\Users\\sam\\AppData\\Local", "root_app_data"],
+ ["C:\\Users\\sam\\AppData.\\Local", "root_app_data"],
+ ["\\\\?\\C:\\Users\\sam\\.ssh", "root_credentials"],
+ ["C:\\Users\\sam\\.ssh.", "root_credentials"],
+ ["\\\\?\\C:\\Users\\sam\\projects\\x", null],
+ ["\\\\?\\UNC\\server\\share\\projects\\x", null],
+ ];
+ for (const [root, expected] of cases) {
+ expect({ root, refused: refusedFolderRoot(root, windows) }).toEqual({ root, refused: expected });
+ }
+ });
+
+ test("on disk: a session started in a credential or app-data folder, or through a link into one, is refused without asking the policy", async () => {
+ const home = await tempDir("folder-home");
+ const roots = [".gnupg", ".aws/sso/cache", ".docker", ".ssh", ".kube", ".config/gh", ".config/gcloud", "Library/Keychains", "work/secrets", "AppData/Roaming/x"];
+ for (const dir of roots) await mkdir(join(home, dir), { recursive: true });
+ await writeFile(join(home, ".gnupg/secring.gpg"), "secret keyring");
+ 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 });
+ // 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 });
+ expect({ root, gate: await isArchivableProject(root, [gitMarkerDetector, detector]) }).toEqual({ root, gate: NO_MARKER });
+ }
+ expect(on.asked.count).toBe(0);
+ await mkdir(join(home, "work/app"));
+ expect(await detector(join(home, "work/app"))).toEqual(FOLDER);
+ expect(on.asked.count).toBe(1);
+ });
+
test("a home directory under a system directory keeps its folders, but a home at the disk root does not open the system directories", () => {
expect(refusedFolderRoot("/var/root/project", { platform: "darwin", homes: ["/var/root"], userData: [] })).toBeNull();
expect(refusedFolderRoot("/var/root", { platform: "darwin", homes: ["/var/root"], userData: [] })).toBe("root_too_broad");
diff --git a/apps/server/src/session-archive/detect.ts b/apps/server/src/session-archive/detect.ts
index 4584e623..43ffea21 100644
--- a/apps/server/src/session-archive/detect.ts
+++ b/apps/server/src/session-archive/detect.ts
@@ -4,12 +4,16 @@
* 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.
+ * 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";
+
export type ArchivableProject = {
archivable: boolean;
/** git_dir | git_file | git_parent | folder | no_marker | gitfile_invalid | root_not_directory | root_too_broad */
@@ -230,11 +234,23 @@ export const defaultProjectDetectors: readonly ProjectMarkerDetector[] = [gitMar
export const FOLDER_MARKER = "folder";
/** Top-level system and app directories of macOS and Linux (one list: the other system's names are absent). */
-const FOLDER_POSIX_SYSTEM_DIRS = ["/System", "/Library", "/Applications", "/private", "/usr", "/bin", "/sbin", "/etc", "/var", "/opt", "/proc", "/sys", "/dev", "/boot", "/lib", "/lib64", "/run"];
+const FOLDER_POSIX_SYSTEM_DIRS = [
+ "/System", "/Library", "/Applications", "/private", "/usr", "/bin", "/sbin", "/etc", "/var", "/opt", "/cores",
+ "/proc", "/sys", "/dev", "/boot", "/lib", "/lib64", "/run", "/root", "/snap", "/nix",
+];
/** Windows system directories, on every drive. */
-const WINDOWS_SYSTEM_DIRS = ["Windows", "Program Files", "Program Files (x86)", "ProgramData"];
+const WINDOWS_SYSTEM_DIRS = ["Windows", "Windows.old", "Program Files", "Program Files (x86)", "ProgramData", "$Recycle.Bin", "System Volume Information", "Recovery", "PerfLogs"];
/** Where POSIX systems mount other disks: /Volumes/<disk> (macOS), /mnt/<disk> (Linux, WSL), /media/<user>/<disk>. */
const POSIX_MOUNT_ROOT = /^\/(?:(?:volumes|mnt)(?:\/[^/]+)?|media(?:\/[^/]+){0,2})$/i;
+/** Credential stores, wherever they are: a root that is one, or is inside one, is refused (names compared without case). */
+const CREDENTIAL_DIRS = new Set([".ssh", ".aws", ".gnupg", ".kube", ".docker", ".azure", ".password-store", "keychains"]);
+/** The same for these folder pairs, wherever they are. */
+const CREDENTIAL_DIR_PAIRS: ReadonlyArray<readonly [string, string]> = [[".config", "gcloud"]];
+/** App-data folders, wherever they are (Windows' AppData, and macOS' Library/Application Support outside a home too). */
+const APP_DATA_DIRS = new Set(["appdata"]);
+const APP_DATA_DIR_PAIRS: ReadonlyArray<readonly [string, string]> = [["library", "application support"]];
+
+export type FolderRefusal = "root_too_broad" | "root_app_data" | "root_credentials" | "root_system";
/** What the all-folders gate compares a root with; every path absolute, in `platform`'s syntax. */
export type FolderGateContext = {
@@ -246,27 +262,73 @@ export type FolderGateContext = {
};
/**
- * Why `root` may not be archived as a plain folder, or null when it may:
- * a disk root or share root, the home directory or anything above it, the
- * userData directory, anything inside it or above it, and a system or app
- * directory or anything inside one (Windows: AppData too). Inside the home
- * directory only AppData and userData are refused. macOS and Windows
- * compare without case.
+ * `path` absolute and normalised in `platform`'s syntax; macOS and Windows
+ * compare without case. On Windows, `\\?\C:\x` and `\\.\C:\x` are `C:\x`,
+ * `\\?\UNC\server\share` is `\\server\share`, and a name loses its trailing
+ * dots and spaces (`C:\Users\sam.` is `C:\Users\sam`), as Windows does.
+ */
+export function foldGatePath(path: string, platform: NodeJS.Platform): string {
+ if (platform !== "win32") {
+ const resolved = posix.resolve(path);
+ return platform === "darwin" ? resolved.toLowerCase() : resolved;
+ }
+ let value = path.replaceAll("/", "\\");
+ if (/^\\\\[?.]\\unc\\/i.test(value)) value = `\\\\${value.slice(8)}`;
+ else if (/^\\\\[?.]\\[a-z]:/i.test(value)) value = value.slice(4);
+ const resolved = win32.resolve(value);
+ const { root } = win32.parse(resolved);
+ const names = resolved.slice(root.length).split("\\").map((name) => name.replace(/[. ]+$/, "")).filter(Boolean);
+ return win32.join(root, ...names).toLowerCase();
+}
+
+/**
+ * Why `root` may not be archived as a plain folder, or null when it may.
+ * Refused, and anything inside them:
+ * - a disk or share root, and the home directory or anything above it;
+ * - the Electron userData directory, and anything above it;
+ * - a credential store wherever it is (`.ssh`, `.aws`, `.gnupg`, `.kube`,
+ * `.docker`, `.azure`, `.password-store`, `Keychains`, `.config/gcloud`),
+ * and any folder the collector's denylist denies as a whole (`keys`,
+ * `secrets`, `credentials*`, `.env*`, `node_modules`, `.git`, ...);
+ * - app data wherever it is (`AppData`, `Library/Application Support`);
+ * - in the home directory, and in the other folders beside it (other
+ * accounts, Shared, Public; which are refused themselves too): every
+ * folder whose name starts with `.` (config, caches, credentials), and
+ * `Library` on macOS and `snap` on Linux;
+ * - outside the home directory, the system and app directories.
+ * Every other folder inside the home directory may be archived.
*/
-export function refusedFolderRoot(root: string, context: FolderGateContext): "root_too_broad" | "root_app_data" | "root_system" | null {
+export function refusedFolderRoot(root: string, context: FolderGateContext): FolderRefusal | null {
const paths = context.platform === "win32" ? win32 : posix;
- const fold = (path: string) => (context.platform === "darwin" || context.platform === "win32" ? paths.resolve(path).toLowerCase() : paths.resolve(path));
+ const fold = (path: string) => foldGatePath(path, context.platform);
const within = (child: string, parent: string) => {
const rel = paths.relative(parent, child);
return rel === "" || (!rel.startsWith(`..${paths.sep}`) && rel !== ".." && !paths.isAbsolute(rel));
};
+ const isDiskRoot = (path: string) => paths.parse(path).root === path;
const target = fold(root);
const homes = context.homes.map(fold);
- if (paths.parse(target).root === target || (paths === posix && POSIX_MOUNT_ROOT.test(target))) return "root_too_broad";
+ if (isDiskRoot(target) || (paths === posix && POSIX_MOUNT_ROOT.test(target))) return "root_too_broad";
if (homes.some((home) => within(home, target))) return "root_too_broad";
if (context.userData.map(fold).some((dir) => within(target, dir) || within(dir, target))) return "root_app_data";
- if (paths === win32 && homes.some((home) => within(target, paths.join(home, "appdata")))) return "root_system";
- if (homes.some((home) => paths.parse(home).root !== home && within(target, home))) return null;
+
+ const names = target.slice(paths.parse(target).root.length).split(paths.sep).filter(Boolean).map((name) => name.toLowerCase());
+ const hasPair = (pairs: ReadonlyArray<readonly [string, string]>) => names.some((name, index) => pairs.some(([first, second]) => name === first && names[index + 1] === second));
+ if (names.some((name) => CREDENTIAL_DIRS.has(name)) || hasPair(CREDENTIAL_DIR_PAIRS) || isCollectorDirectoryDenied(names.join("/"))) return "root_credentials";
+ if (names.some((name) => APP_DATA_DIRS.has(name)) || hasPair(APP_DATA_DIR_PAIRS)) return "root_app_data";
+
+ for (const home of homes) {
+ if (isDiskRoot(home)) continue;
+ // The account folder the root is in: home, or a folder beside it (unless home sits right at a disk root).
+ const parent = paths.dirname(home);
+ const base = isDiskRoot(parent) ? home : parent;
+ if (!within(target, base)) continue;
+ const account = base === home ? home : paths.join(parent, paths.relative(parent, target).split(paths.sep)[0]!);
+ if (target === account) return "root_too_broad";
+ const first = paths.relative(account, target).split(paths.sep)[0]!.toLowerCase();
+ if (first.startsWith(".") || (context.platform === "darwin" && first === "library") || (context.platform === "linux" && first === "snap")) return "root_app_data";
+ }
+ if (homes.some((home) => !isDiskRoot(home) && within(target, home))) return null;
const systemDirs = paths === win32 ? WINDOWS_SYSTEM_DIRS.map((dir) => paths.join(paths.parse(target).root, dir)) : FOLDER_POSIX_SYSTEM_DIRS;
return systemDirs.some((dir) => within(target, fold(dir))) ? "root_system" : null;
}
diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.ts
index 725517ab..805d433d 100644
--- a/apps/server/src/session-archive/index.test.ts
+++ b/apps/server/src/session-archive/index.test.ts
@@ -7,7 +7,7 @@ import { promisify } from "node:util";
import { gitMarkerDetector, gitParentDetector } from "./detect.js";
import { FakeArchiveServer, slowPartTwo } from "./fake-archive-server.js";
-import { ARCHIVE_STATE_DIRECTORY, SessionArchiver, type SessionArchiverOptions } from "./index.js";
+import { ARCHIVE_STATE_DIRECTORY, POLICY_TTL_MS, SessionArchiver, type SessionArchiverOptions } from "./index.js";
import { cleanupTempDirs, manifestOf, openArchive, tempDir } from "./test-helpers.js";
const execFileAsync = promisify(execFile);
@@ -306,6 +306,161 @@ describe("SessionArchiver", () => {
expect(server.calls.filter((call) => call.path === "archives/key")).toHaveLength(1);
});
+ test("with the all-folders policy on, a session started in a credential or app-data folder is refused and nothing is sent", async () => {
+ const server = new FakeArchiveServer();
+ server.policy = { all_folders: true };
+ const home = await tempDir("home");
+ const files: Record<string, string> = {
+ ".gnupg/secring.gpg": "old-style secret keyring",
+ ".gnupg/pubring.kbx": "keybox",
+ ".aws/config": "[default]\nregion = us-east-1\n",
+ ".aws/sso/cache/abc.json": "{\"accessToken\":\"aoa-sso-token\"}",
+ ".docker/config.json": "{\"auths\":{\"ghcr.io\":{\"auth\":\"dXNlcjpwYXNz\"}}}",
+ ".ssh/config": "Host *\n",
+ ".ssh/known_hosts": "github.com ssh-ed25519 AAAA\n",
+ ".kube/config": "apiVersion: v1\n",
+ ".config/gh/hosts.yml": "github.com:\n oauth_token: gho_x\n",
+ ".config/gcloud/credentials.db": "sqlite",
+ ".local/share/keyrings/login.keyring": "keyring",
+ ".password-store/bank.gpg": "gpg",
+ "Library/Keychains/login.keychain-db": "keychain",
+ "Library/Application Support/Google/Chrome/Default/Cookies": "cookies",
+ "AppData/Roaming/gcloud/access_tokens.db": "tokens",
+ "work/secrets/prod.txt": "prod",
+ };
+ for (const [path, content] of Object.entries(files)) {
+ await mkdir(join(home, path, ".."), { recursive: true });
+ await writeFile(join(home, path), content);
+ }
+ const state = await tempDir("state");
+ const subject = archiver(server, state, { folderGate: { homeDir: home } });
+ const roots = [...new Set(Object.keys(files).map((path) => join(home, path, "..")))];
+ roots.push(join(home, ".aws"), join(home, ".config"), join(home, ".local"), join(home, "Library"), join(home, "AppData"));
+ for (const [index, root] of roots.entries()) {
+ expect({ root, result: await subject.captureBase(`ses_credential_${index}`, root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });
+ }
+ expect(server.calls).toEqual([]);
+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);
+ });
+
+ test("with the policy off and the API failing, a folder session costs one GET with no backoff and no refresh, and does not change archiving", async () => {
+ const failures: Array<[string, Response | "network"]> = [
+ ["502", new Response("bad gateway", { status: 502 })],
+ ["401", Response.json({ detail: "invalid_token" }, { status: 401 })],
+ ["428", Response.json({ detail: "archive_consent_required" }, { status: 428 })],
+ ["503 archive_disabled", Response.json({ detail: "archive_disabled" }, { status: 503 })],
+ ["network", "network"],
+ ];
+ for (const [name, failure] of failures) {
+ const server = new FakeArchiveServer();
+ let keyAttempts = 0;
+ server.apiHook = ({ path }) => {
+ if (path !== "archives/key") return undefined;
+ keyAttempts += 1;
+ return failure === "network" ? "network" : failure.clone();
+ };
+ const home = await tempDir("home");
+ for (const dir of ["notes", "drafts", "scratch"]) await mkdir(join(home, dir));
+ const sleeps: number[] = [];
+ const refreshes: string[] = [];
+ const state = await tempDir("state");
+ const subject = archiver(server, state, {
+ folderGate: { homeDir: home },
+ retry: { baseMs: 1_000, maxMs: 300_000, attempts: 8, sleep: async (ms) => void sleeps.push(ms) },
+ refreshAccessToken: async () => (refreshes.push("refresh"), server.token),
+ });
+ const started = Date.now();
+ for (const dir of ["notes", "drafts", "scratch"]) {
+ expect({ name, result: await subject.captureBase(`ses_${dir}_folder`, join(home, dir)) }).toEqual({ name, result: { status: "skipped", reason: "not_archivable" } });
+ }
+ expect(Date.now() - started).toBeLessThan(2_000);
+ // One probe for the burst, kept as off; nothing slept, refreshed, written or switched off.
+ expect({ name, keyAttempts, sleeps, refreshes }).toEqual({ name, keyAttempts: 1, sleeps: [], refreshes: [] });
+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);
+ expect((await subject.pendingStatus()).disabled).toBeNull();
+ // A git session right after is exactly as without the policy: its own full key fetch.
+ server.apiHook = null;
+ const { root } = await project();
+ expect(await subject.captureBase("ses_git_after", root)).toMatchObject({ status: "queued", kind: "base" });
+ expect(server.callPaths().filter((call) => call.endsWith(" 200"))).toEqual(["GET archives/key 200"]);
+ }
+ });
+
+ test("the policy answer is kept with the key for POLICY_TTL_MS: one probe per burst, and a change on omnirush.ai is seen after it", async () => {
+ const server = new FakeArchiveServer();
+ let clock = Date.parse("2026-09-24T10:00:00Z");
+ const home = await tempDir("home");
+ const folders = ["a", "b", "c", "d", "e", "f", "g"];
+ for (const dir of folders) {
+ await mkdir(join(home, dir));
+ await writeFile(join(home, dir, "notes.md"), `${dir}\n`);
+ }
+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home }, now: () => new Date(clock) });
+ const keyReads = () => server.calls.filter((call) => call.path === "archives/key").length;
+ const creates = () => server.calls.filter((call) => call.method === "POST" && call.path === "archives").length;
+
+ // Off: a burst of folder sessions sends one probe.
+ for (const dir of ["a", "b"]) expect(await subject.captureBase(`ses_ttl_${dir}_folder`, join(home, dir))).toEqual({ status: "skipped", reason: "not_archivable" });
+ expect(keyReads()).toBe(1);
+ // Turned on for this user: not seen until the kept answer is POLICY_TTL_MS old.
+ server.policy = { all_folders: true };
+ clock += POLICY_TTL_MS - 1;
+ expect(await subject.captureBase("ses_ttl_c_folder", join(home, "c"))).toEqual({ status: "skipped", reason: "not_archivable" });
+ expect(keyReads()).toBe(1);
+ clock += 1;
+ expect(await subject.captureBase("ses_ttl_d_folder", join(home, "d"))).toMatchObject({ status: "queued", kind: "base" });
+ // The probe's key served the base.
+ expect(keyReads()).toBe(2);
+ // On and kept: the next folder base checks consent with a full key fetch, like a git base.
+ clock += 60_000;
+ expect(await subject.captureBase("ses_ttl_e_folder", join(home, "e"))).toMatchObject({ status: "queued", kind: "base" });
+ expect(keyReads()).toBe(3);
+ await subject.drain();
+ expect(server.objects().map((archive) => archive.request.marker)).toEqual(["folder", "folder"]);
+
+ // Turned off again: that full fetch sees it, the folder is not archived, and the answer is kept as off.
+ server.policy = { all_folders: false };
+ expect(await subject.captureBase("ses_ttl_f_folder", join(home, "f"))).toEqual({ status: "skipped", reason: "not_archivable" });
+ expect(await subject.captureBase("ses_ttl_g_folder", join(home, "g"))).toEqual({ status: "skipped", reason: "not_archivable" });
+ expect(keyReads()).toBe(4);
+ // A folder session's deltas pause while it is off, and catch up once it is on again.
+ await writeFile(join(home, "d", "notes.md"), "d, edited\n");
+ const createsBefore = creates();
+ expect(await subject.captureDelta("ses_ttl_d_folder", join(home, "d"), 1)).toEqual({ status: "skipped", reason: "not_archivable" });
+ expect(creates()).toBe(createsBefore);
+ server.policy = { all_folders: true };
+ clock += POLICY_TTL_MS;
+ expect(await subject.captureDelta("ses_ttl_d_folder", join(home, "d"), 2)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });
+ expect(keyReads()).toBe(5);
+
+ // Sign-out forgets the answer: the next account's first folder session asks again.
+ await subject.signOut();
+ subject.setAccessToken(server.token);
+ expect(await subject.captureBase("ses_ttl_g_folder", join(home, "g"))).toMatchObject({ status: "queued", kind: "base" });
+ expect(keyReads()).toBe(6);
+ });
+
+ test("a folder inside a larger git repository is archived as a plain folder: marker folder and workspace.git null", async () => {
+ const server = new FakeArchiveServer();
+ server.policy = { all_folders: true };
+ const home = await tempDir("home");
+ const repo = join(home, "dotfiles");
+ await mkdir(join(repo, "work/proj"), { recursive: true });
+ await git(repo, "init", "-q", "-b", "dotfiles");
+ await git(repo, "remote", "add", "origin", "https://github.com/me/dotfiles.git");
+ await writeFile(join(repo, "work/proj/plan.md"), "# plan\n");
+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home } });
+ expect(await subject.captureBase("ses_nested_folder", join(repo, "work/proj"))).toMatchObject({ status: "queued", kind: "base" });
+ await writeFile(join(repo, "work/proj/plan.md"), "# plan, edited\n");
+ expect(await subject.captureDelta("ses_nested_folder", join(repo, "work/proj"), 1)).toMatchObject({ status: "queued", kind: "delta" });
+ await subject.drain();
+ const [base, delta] = server.objects();
+ expect(base!.request.marker).toBe("folder");
+ expect(delta!.request.marker).toBe("folder");
+ expect(manifestOf(await openArchive(base!.object!))).toMatchObject({ workspace: { label: "proj", marker: "folder", git: null } });
+ expect(manifestOf(await openArchive(delta!.object!))).toMatchObject({ workspace: { marker: "folder", git: null } });
+ });
+
test("428 at /archives/key: nothing is packed, archiving stays off until a later captureBase succeeds", async () => {
const server = new FakeArchiveServer();
server.gate = { status: 428, detail: "archive_consent_required" };
@@ -544,6 +699,28 @@ describe("SessionArchiver final archives", () => {
expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "baselines"))).toHaveLength(1);
});
+ test("a plain folder's final archives pass the all-folders refusals and policy again: one while it is on, none while it is off", async () => {
+ const server = new FakeArchiveServer();
+ server.policy = { all_folders: true };
+ const home = await tempDir("home");
+ const root = join(home, "drafts");
+ await mkdir(root);
+ await writeFile(join(root, "chapter.md"), "one\n");
+ let now = Date.parse("2026-09-23T10:00:00Z");
+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home }, now: () => new Date(now) });
+ expect(await subject.captureBase("ses_folder_final", root, 1)).toMatchObject({ status: "queued", kind: "base" });
+ await writeFile(join(root, "chapter.md"), "two\n");
+ expect(await subject.captureFinal("ses_folder_final", "idle")).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });
+ await subject.drain();
+ expect(server.objects().map((object) => [object.request.marker, object.request.turn])).toEqual([["folder", 1], ["folder", 1]]);
+ expect(manifestOf(await openArchive(server.objects()[1]!.object!))).toMatchObject({ trigger: "final", reason: "idle", workspace: { marker: "folder", git: null } });
+
+ server.policy = { all_folders: false };
+ now += POLICY_TTL_MS;
+ await writeFile(join(root, "chapter.md"), "three\n");
+ expect(await subject.captureFinal("ses_folder_final", "app_quit")).toEqual({ status: "skipped", reason: "not_archivable" });
+ });
+
test("a server without final archives: the refused final is dropped once, the chain goes back, and the turn delta queued behind it is captured again", async () => {
const server = new FakeArchiveServer();
server.strictTurns = true;
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index 649b0f0f..01dd4faa 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -15,7 +15,7 @@ import { join, resolve } from "node:path";
import { z } from "zod";
import { externalFetch } from "../server-fetch.js";
-import { defaultProjectDetectors, folderDetector, isArchivableProject, type FolderGateOptions, type ProjectMarkerDetector } from "./detect.js";
+import { defaultProjectDetectors, FOLDER_MARKER, folderDetector, isArchivableProject, type FolderGateOptions, type ProjectMarkerDetector } from "./detect.js";
import { hintGarbageCollection, readJsonFile, stateKey, writeChunksAtomic, writeJsonAtomic } from "./files.js";
import {
ArchiveHashCache,
@@ -65,6 +65,12 @@ const SIGN_OUT_ABORT_TIMEOUT_MS = 5_000;
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.
+ */
+export const POLICY_TTL_MS = 5 * 60_000;
export type SessionArchiverOptions = {
/** As the collector: the archive routes are derived from it like the collect URL. */
@@ -241,6 +247,10 @@ 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 probe in flight, shared by the sessions that start meanwhile. */
+ private policyProbe: Promise<KeyResult> | null = null;
private readonly sessionTails = new Map<string, Promise<void>>();
/** Sessions stopped by the drain; captures in flight check it before they commit. */
private readonly stoppedSessions = new Map<string, string>();
@@ -310,7 +320,9 @@ export class SessionArchiver {
* Base archive at session start (new session, or a resumed one without a
* base). No-op when the gate says not archivable, archiving is off, or the
* session already has a base. Checks consent through GET /archives/key
- * before packing anything.
+ * 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.
*/
captureBase(sessionId: string, root: string, turn = 0): Promise<CaptureResult> {
return this.guard("base", sessionId, turn, async () => this.withSession(sessionId, async () => {
@@ -324,10 +336,11 @@ export class SessionArchiver {
/**
* Delta after a completed turn. No-op without a base, when the session
- * stopped archiving, or when nothing in the folder changed. A session whose
- * base could not be captured yet (key unavailable) gets its base instead.
- * A null turn (the engine's messages could not be read) follows the last
- * archived turn.
+ * 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.
*/
captureDelta(sessionId: string, root: string, turn: number | null): Promise<CaptureResult> {
return this.guard("delta", sessionId, turn ?? 0, async () => this.withSession(sessionId, async () => {
@@ -340,6 +353,8 @@ export class SessionArchiver {
if (state.next_sequence === 0) return this.captureBaseLocked(sessionId, state.root, turn ?? 0, generation);
const next = 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" };
const key = await this.currentKey();
if (key === "disabled") return { status: "skipped", reason: "disabled" };
if (key === "unavailable") return { status: "skipped", reason: "unavailable" };
@@ -371,8 +386,9 @@ export class SessionArchiver {
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.
- const gate = await isArchivableProject(state.root, this.detectors, { appDirs: this.appDirs });
+ // 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" };
@@ -468,6 +484,8 @@ export class SessionArchiver {
}
this.key = null;
this.disabled = null;
+ this.policy = null;
+ this.policyProbe = null;
this.stoppedSessions.clear();
this.resettingSessions.clear();
this.finalsAccepted = false;
@@ -551,20 +569,19 @@ export class SessionArchiver {
private async captureBaseLocked(sessionId: string, root: string, turn: number, generation: number): Promise<CaptureResult> {
if (!this.uploader.configured) return { status: "skipped", reason: "disabled" };
- // A root no other detector qualified asks the key route for the all-folders policy (4.4); the base reuses that answer.
- let keyFetch: Promise<KeyResult> | null = null;
- const fetchKey = () => (keyFetch ??= this.uploader.fetchKey());
- const folder = folderDetector(async () => {
- const fetched = await fetchKey();
- return fetched.status === "ok" && fetched.policy.allFolders;
- }, this.folderGate);
- const gate = await isArchivableProject(root, [...this.detectors, folder], { appDirs: this.appDirs });
+ // A root no git detector qualified, and that no folder refusal stops, asks for the all-folders policy (4.4).
+ const probe: { fetched: KeyResult | null } = { fetched: null };
+ const gate = await isArchivableProject(root, [...this.detectors, this.folderDetector(generation, probe)], { appDirs: this.appDirs });
if (!gate.archivable || !gate.marker) return { status: "skipped", reason: "not_archivable" };
- const fetched = await fetchKey();
+ // 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();
+ if (!probe.fetched) this.rememberPolicy(fetched, generation, false);
if (fetched.status === "disabled") {
await this.disable(fetched.code);
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" };
const state: SessionState = {
v: 1,
session_id: sessionId,
@@ -578,7 +595,7 @@ export class SessionArchiver {
updated_at: this.now().toISOString(),
};
if (fetched.status === "unavailable") {
- // Remembered with next_sequence 0: the next captureDelta tries the base again.
+ // Remembered with next_sequence 0: the next captureDelta tries the base again (a folder passes the gate and the policy again).
if (generation === this.generation) await this.saveSession(state);
this.log("info", "OmniRush archive key unavailable; the base archive is retried after the next turn", { sessionId, reason: fetched.reason });
return { status: "skipped", reason: "unavailable" };
@@ -593,9 +610,57 @@ 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. */
+ private folderDetector(generation: number, probe?: { fetched: KeyResult | null }): ProjectMarkerDetector {
+ return folderDetector(async () => {
+ const answer = await this.allFoldersPolicy(generation);
+ if (probe) probe.fetched = answer.fetched;
+ return answer.allFolders;
+ }, 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.
+ */
+ private async allFoldersPolicy(generation: number): Promise<{ allFolders: boolean; fetched: KeyResult | null }> {
+ const kept = this.policy;
+ if (kept && this.now().getTime() - kept.at < POLICY_TTL_MS) return { allFolders: kept.allFolders, fetched: null };
+ let probe = this.policyProbe;
+ if (!probe) {
+ const started = this.uploader.probeKey();
+ probe = started;
+ this.policyProbe = started;
+ void started.finally(() => {
+ if (this.policyProbe === started) this.policyProbe = null;
+ });
+ }
+ const fetched = await probe;
+ this.rememberPolicy(fetched, generation, true);
+ return { allFolders: fetched.status === "ok" && fetched.policy.allFolders, fetched };
+ }
+
+ /**
+ * Keeps a key response's policy for allFoldersPolicy. 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() };
+ }
+
private async currentKey(signal?: AbortSignal): Promise<ArchiveKey | "disabled" | "unavailable"> {
if (this.key) return this.key;
+ const generation = this.generation;
const fetched = await this.uploader.fetchKey(signal);
+ this.rememberPolicy(fetched, generation, false);
if (fetched.status === "disabled") {
await this.disable(fetched.code);
return "disabled";
@@ -626,7 +691,9 @@ export class SessionArchiver {
const { signal } = options;
const cache = await this.loadHashCache(rootKey);
const scanning = scanArchiveTree(state.root, { excludedDirs: this.appDirs, includeCredentialFiles: this.includeCredentials, hashCache: cache, ...(signal ? { signal } : {}) });
- const gitReading = kind === "base" ? readArchiveGit(state.root) : null;
+ // 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;
let deleted: string[] | undefined;
@@ -645,7 +712,7 @@ export class SessionArchiver {
files = delta.files;
deleted = delta.deleted;
}
- const git = await (gitReading ?? readArchiveGit(state.root));
+ const git = withGit ? await (gitReading ?? readArchiveGit(state.root)) : null;
const archiveId = this.random.uuid();
const sequence = state.next_sequence;
const createdAt = this.now();
@@ -910,6 +977,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() };
await this.saveArchiverState();
const records = await this.listQueue();
for (const record of records) {
diff --git a/apps/server/src/session-archive/upload.test.ts b/apps/server/src/session-archive/upload.test.ts
index 0a90e0c5..cf41b9b9 100644
--- a/apps/server/src/session-archive/upload.test.ts
+++ b/apps/server/src/session-archive/upload.test.ts
@@ -104,6 +104,84 @@ describe("archive upload client", () => {
}
});
+ 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 })],
+ ["500", Response.json({ detail: "internal" }, { status: 500 })],
+ ["503 maintenance", Response.json({ detail: "maintenance" }, { status: 503 })],
+ ["429", Response.json({ detail: "slow_down" }, { status: 429 })],
+ ["401", Response.json({ detail: "invalid_token" }, { status: 401 })],
+ ["network", "network"],
+ ];
+ for (const [name, failure] of failures) {
+ const server = new FakeArchiveServer();
+ let attempts = 0;
+ server.apiHook = ({ path }) => {
+ if (path !== "archives/key") return undefined;
+ attempts += 1;
+ return failure === "network" ? "network" : failure.clone();
+ };
+ const sleeps: number[] = [];
+ const refreshes: string[] = [];
+ const client = uploader(server, {
+ retry: { baseMs: 1_000, maxMs: 300_000, attempts: 8, sleep: async (ms) => void sleeps.push(ms) },
+ refreshAccessToken: async () => (refreshes.push("refresh"), server.token),
+ });
+ const probed = await client.probeKey();
+ expect({ name, status: probed.status, attempts, sleeps, refreshes }).toEqual({ name, status: "unavailable", attempts: 1, sleeps: [], refreshes: [] });
+ // The full fetch, used for the key once a folder is known to be archived, still retries and refreshes.
+ attempts = 0;
+ await client.fetchKey();
+ expect({ name, fullAttempts: attempts > 1 }).toEqual({ name, fullAttempts: true });
+ }
+ });
+
+ test("the policy probe reads the key and the policy like fetchKey; 428, 503 and 404 answer disabled", async () => {
+ const server = new FakeArchiveServer();
+ const client = uploader(server);
+ server.policy = { all_folders: true };
+ const probed = await client.probeKey();
+ expect(probed.status === "ok" && probed.policy.allFolders && probed.key.kid).toBe(testKeys.kid);
+ server.policy = undefined;
+ const off = await client.probeKey();
+ expect(off.status === "ok" && off.policy.allFolders).toBe(false);
+ server.gate = { status: 428, detail: "archive_consent_required" };
+ expect(await client.probeKey()).toEqual({ status: "disabled", code: "archive_consent_required" });
+ server.gate = { status: 503, detail: "archive_disabled" };
+ expect(await client.probeKey()).toEqual({ status: "disabled", code: "archive_disabled" });
+ server.gate = { status: 404, detail: "not_found" };
+ expect(await client.probeKey()).toEqual({ status: "disabled", code: "archive_routes_missing" });
+ expect(server.calls).toHaveLength(5);
+ });
+
+ test("through the request hook, the probe asks for no refresh and a short timeout; every other call keeps the refresh", async () => {
+ const server = new FakeArchiveServer();
+ const seen: Array<{ path: string; refresh: unknown; signal: boolean }> = [];
+ const client = new ArchiveUploader({
+ fetch: server.respond,
+ retry: FAST_RETRY,
+ request: (path, init) => {
+ seen.push({ path, refresh: "refresh" in init ? init.refresh : "absent", signal: init.signal instanceof AbortSignal });
+ return server.respond(`https://api.omnirush.test/omnirush/${path}`, { method: init.method, headers: { authorization: `Bearer ${server.token}` } });
+ },
+ });
+ expect((await client.probeKey()).status).toBe("ok");
+ expect((await client.fetchKey()).status).toBe("ok");
+ expect(seen).toEqual([
+ { path: "archives/key", refresh: false, signal: true },
+ { path: "archives/key", refresh: "absent", signal: true },
+ ]);
+ // A probe that does not answer within POLICY_PROBE_TIMEOUT_MS is abandoned: an aborted signal ends it at once.
+ const hanging = new ArchiveUploader({
+ fetch: server.respond,
+ request: (_path, init) => new Promise((_resolve, reject) => init.signal?.addEventListener("abort", () => reject(new DOMException("timed out", "TimeoutError")))),
+ });
+ const controller = new AbortController();
+ const pending = hanging.probeKey(controller.signal);
+ controller.abort();
+ expect((await pending).status).toBe("unavailable");
+ });
+
test("428 and 503 archive_disabled on create turn archiving off without an error", async () => {
for (const gate of [{ status: 428, detail: "archive_consent_required" }, { status: 503, detail: "archive_disabled" }]) {
const server = new FakeArchiveServer();
diff --git a/apps/server/src/session-archive/upload.ts b/apps/server/src/session-archive/upload.ts
index 61420468..05276cc3 100644
--- a/apps/server/src/session-archive/upload.ts
+++ b/apps/server/src/session-archive/upload.ts
@@ -17,7 +17,9 @@ export type ArchiveFetch = (input: string, init?: RequestInit) => Promise<Respon
* device session (the gateway broker) attaches the bearer and handles its own
* rotation, as it does for the collector's `upload` hook.
*/
-export type ArchiveApiRequest = (path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }) => Promise<Response>;
+export type ArchiveApiRequest = (path: string, init: ArchiveApiRequestInit) => Promise<Response>;
+/** `refresh: false` returns a 401 as it is, without refreshing the bearer (the all-folders policy probe); absent means true. */
+export type ArchiveApiRequestInit = { method: "GET" | "POST"; body?: string; signal?: AbortSignal; refresh?: false };
export type ArchiveLog = (level: "info" | "warn", message: string, attributes?: Record<string, unknown>) => void;
export type RetryPolicy = {
@@ -56,6 +58,8 @@ export function backoffMs(policy: RetryPolicy, attempt: number): number {
}
const REQUEST_TIMEOUT_MS = 30_000;
+/** The all-folders policy probe: one attempt this long at most, no backoff and no bearer refresh. */
+export const POLICY_PROBE_TIMEOUT_MS = 5_000;
/** A part PUT may take this long at least, and longer for big parts on slow links (32 KB/s floor). */
const PART_MIN_TIMEOUT_MS = 10 * 60_000;
const PART_MIN_BYTES_PER_MS = 32;
@@ -233,9 +237,10 @@ export class ArchiveUploader {
this.token = token?.trim() || null;
}
- private send(method: "GET" | "POST", path: string, body: unknown, signal?: AbortSignal): Promise<Response> {
+ private send(method: "GET" | "POST", path: string, body: unknown, signal?: AbortSignal, probe = false): Promise<Response> {
const payload = body === undefined ? undefined : JSON.stringify(body);
- if (this.options.request) return this.options.request(path, { method, ...(payload === undefined ? {} : { body: payload }), signal: timeoutSignal(REQUEST_TIMEOUT_MS, signal) });
+ const timeout = timeoutSignal(probe ? POLICY_PROBE_TIMEOUT_MS : REQUEST_TIMEOUT_MS, signal);
+ if (this.options.request) return this.options.request(path, { method, ...(payload === undefined ? {} : { body: payload }), signal: timeout, ...(probe ? { refresh: false as const } : {}) });
if (!this.apiRoot || !this.token) return Promise.reject(new Error("archive API not configured"));
return this.options.fetch(`${this.apiRoot}/${path}`, {
method,
@@ -245,7 +250,7 @@ export class ArchiveUploader {
...(payload === undefined ? {} : { "Content-Type": "application/json" }),
},
...(payload === undefined ? {} : { body: payload }),
- signal: timeoutSignal(REQUEST_TIMEOUT_MS, signal),
+ signal: timeout,
});
}
@@ -308,10 +313,39 @@ export class ArchiveUploader {
return null;
}
+ /** One GET with a single attempt: no bearer refresh on a 401, no backoff; a retryable failure is `unavailable`. */
+ private async getOnce(path: string, signal?: AbortSignal): Promise<ApiResult> {
+ if (signal?.aborted) return { kind: "aborted" };
+ try {
+ const response = await this.send("GET", path, undefined, signal, true);
+ if (response.ok) return { kind: "ok", status: response.status, body: await response.json().catch(() => null) };
+ const code = await errorCode(response);
+ if (!RETRYABLE_STATUSES.has(response.status) || (response.status === 503 && code === "archive_disabled")) return { kind: "error", status: response.status, code };
+ return { kind: "unavailable", reason: `status ${response.status}` };
+ } catch (error) {
+ if (signal?.aborted) return { kind: "aborted" };
+ return { kind: "unavailable", reason: error instanceof Error ? error.name : "network error" };
+ }
+ }
+
/** GET /archives/key (7.2). 428, 503 and a server without the route all mean archiving is off. */
async fetchKey(signal?: AbortSignal): Promise<KeyResult> {
if (!this.configured) return { status: "disabled", code: "not_configured" };
- const result = await this.call("GET", "archives/key", undefined, signal);
+ return this.keyResult(await this.call("GET", "archives/key", undefined, signal));
+ }
+
+ /**
+ * GET /archives/key as the all-folders policy probe (4.4): one attempt
+ * within POLICY_PROBE_TIMEOUT_MS, no backoff, and a 401 is not answered
+ * with a bearer refresh. The caller counts anything but `ok` with
+ * `policy.allFolders` as the policy off.
+ */
+ async probeKey(signal?: AbortSignal): Promise<KeyResult> {
+ if (!this.configured) return { status: "disabled", code: "not_configured" };
+ return this.keyResult(await this.getOnce("archives/key", signal));
+ }
+
+ private keyResult(result: ApiResult): KeyResult {
if (result.kind === "ok") {
const parsed = keySchema.safeParse(result.body);
if (!parsed.success) return { status: "unavailable", reason: "invalid key response" };
diff --git a/apps/server/src/workspace-collector.ts b/apps/server/src/workspace-collector.ts
index 6a43b855..875d2dda 100644
--- a/apps/server/src/workspace-collector.ts
+++ b/apps/server/src/workspace-collector.ts
@@ -774,6 +774,17 @@ function hasScrubbedExtension(lower: string): boolean {
return dot > 0 && SCRUBBED_EXTENSIONS.has(lower.slice(dot));
}
+/**
+ * Whether the collector denies every file under this directory path, whatever
+ * the file is called: one of its components meets the unconditional rules
+ * (`.ssh`, `.aws`, `.gnupg`, `keys`, `secrets`, `credentials*`, `.env*`,
+ * `node_modules`, `.git`, a key-store suffix, ...). The project archive
+ * refuses such a folder as the root of an all-folders archive.
+ */
+export function isCollectorDirectoryDenied(path: string): boolean {
+ return hasDeniedComponent(pathComponents(path));
+}
+
/**
* Whether a workspace-relative path may never leave the machine. Repository
* internals, dependency trees and the classic credential files are denied by
diff --git a/docs/project-archive.md b/docs/project-archive.md
index f3ca4696..ea43c6f5 100644
--- a/docs/project-archive.md
+++ b/docs/project-archive.md
@@ -41,16 +41,53 @@ All of these must be true:
app's own data folders. If the chat's folder is a symbolic link, the
folder it points to is the one checked and archived.
-Archiving every folder is a setting on omnirush.ai, and it is off. While it
-is off, only git projects are archived. Once it is on (after the omnirush.ai
-consent terms describe it), a chat folder without `.git` is archived the
-same way, including binaries, large files and files git would ignore. It is
-still never your home folder or a folder above it, the root of a disk or
-network share, the app's own data folders, or a system or app folder such as
-`/System`, `/Library`, `/Applications`, `/usr`, `/etc` or `/var` on macOS,
-`/usr`, `/etc`, `/var`, `/opt` on Linux, or `C:\Windows`, `Program Files`,
-`ProgramData` and your `AppData` folder on Windows (or anything inside them).
-A folder inside your home folder, such as `~/projects/app`, can be archived.
+Archiving every folder is a setting on omnirush.ai, and it is off. It
+applies only to accounts that accepted the omnirush.ai terms that describe
+it (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.
+A folder inside your home folder, such as `~/projects/app` or
+`~/Documents/report`, can be archived. A folder without `.git` is never
+archived when it is your home folder or a folder above it, the root of a
+disk or network share, or another account's home folder or a shared folder
+beside yours (such as `/Users/Shared` or `C:\Users\Public`). Nor when it
+is, or is inside, one of these (checked on the path as given and on the
+folder it resolves to through symbolic links):
+
+- a credential folder, wherever it is: `.ssh`, `.aws`, `.gnupg`, `.kube`,
+ `.docker`, `.azure`, `.password-store`, `Keychains` or `.config/gcloud`,
+ and any folder the credential rules below leave out as a whole, such as a
+ `keys`, `secrets` or `credentials` folder, a `.env...` folder,
+ `node_modules` or `.git`;
+- an app-data folder: the app's own data folders, any folder in your home
+ folder whose name starts with a dot (such as `.config`, `.local` or
+ `.cache`), `~/Library` on macOS (including iCloud Drive's
+ `~/Library/Mobile Documents`), `~/snap` on Linux, `AppData` on Windows,
+ and `Library/Application Support` anywhere. The same applies in another
+ account's home folder;
+- a system or app folder outside your home folder: `/System`, `/Library`,
+ `/Applications`, `/private` (which holds `/tmp`), `/usr`, `/bin`,
+ `/sbin`, `/etc`, `/var`, `/opt` and `/cores` on macOS; `/usr`, `/bin`,
+ `/sbin`, `/etc`, `/var`, `/opt`, `/root`, `/proc`, `/sys`, `/dev`,
+ `/boot`, `/lib`, `/lib64`, `/run`, `/snap` and `/nix` on Linux;
+ `Windows`, `Windows.old`, `Program Files`, `Program Files (x86)`,
+ `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.
+
+To learn whether the setting is on, the app asks omnirush.ai when a chat
+starts 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 five
+minutes, so starting several chats sends one request, and a change on
+omnirush.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
+uploads 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
diff --git a/docs/workspace-collector-privacy.md b/docs/workspace-collector-privacy.md
index aabf60da..f1105f18 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 (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 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 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.