A desktop coding agent with free access to frontier models.
TypeScript
feat(archive): archive every session folder once omnirush.ai turns on the all-folders policy
ogsamrat3ef584283189
diff --git a/apps/desktop/electron/runtime.mjs b/apps/desktop/electron/runtime.mjsindex 37e30696..c2e71215 100644--- a/apps/desktop/electron/runtime.mjs+++ b/apps/desktop/electron/runtime.mjs@@ -1979,6 +1979,8 @@ export function createRuntimeManager({// from process.env the first time it creates a logger.const logFilePath = resolveOmniRushServerLogFile(userDataDir);process.env.OMNIRUSH_SERVER_LOG_FILE = logFilePath;+ // The project archive never archives this folder, or one inside or above it.+ process.env.OMNIRUSH_DESKTOP_USER_DATA_DIR = userDataDir;omnirushServerState.logFilePath = logFilePath;const { startEmbeddedServer } = await import(embeddedServerImportUrl(embeddedPath));// startEmbeddedServer falls back to an OS-assigned port if `port` racesdiff --git a/apps/server/src/capture-client.test.ts b/apps/server/src/capture-client.test.tsindex e0bed8cb..1c641568 100644--- a/apps/server/src/capture-client.test.ts+++ b/apps/server/src/capture-client.test.ts@@ -272,6 +272,52 @@ describe("capture worker", () => {expect(results[1]).toEqual([]);}, 60_000);+ test("the archiver on the worker reads the all-folders policy from the key and archives a folder without .git only while it is on", async () => {+ 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: "ses_folder_0001" })),+ });+ cleanups.push(() => engine.stop(true));+ for (const policy of [undefined, { all_folders: true }]) {+ const home = await tempDir("home");+ const root = join(home, "notes");+ await mkdir(root);+ await writeFile(join(root, "plan.md"), "# plan\n");+ const archive = new FakeArchiveServer();+ archive.policy = policy;+ const capture = service({+ stateDir: await tempDir("state"),+ 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("ses_folder_0001", "workspace-folder", root);+ capture.archiveSessionStarted("ses_folder_0001", 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");+ if (policy) {+ await until(() => archive.objects().length === 1, 20_000, "the folder's base archive");+ expect(archive.objects()[0]!.request).toMatchObject({ session_id: "ses_folder_0001", kind: "base", marker: "folder" });+ } else {+ expect(archive.callPaths()).toEqual(["GET archives/key 200"]);+ }+ await capture.stop();+ }+ }, 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-client.ts b/apps/server/src/capture-client.tsindex 9ac7809b..1b6fd809 100644--- a/apps/server/src/capture-client.ts+++ b/apps/server/src/capture-client.ts@@ -265,6 +265,7 @@ class CaptureClient implements CaptureService {archive: {enabled: archive.enabled,excludedDirs: archive.excludedDirs,+ folderGate: archive.folderGate,request: Boolean(archive.request),refreshAccessToken: Boolean(archive.refreshAccessToken),...(archive.gatewayUrl !== undefined ? { gatewayUrl: archive.gatewayUrl } : {}),diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex 8257680f..cba76081 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -54,7 +54,7 @@ export type CaptureHostOptions = {gatewayUrl?: string;accessToken?: string;};- archive: Pick<SessionArchiverOptions, "request" | "refreshAccessToken" | "fetch" | "gatewayUrl" | "accessToken"> & {+ archive: Pick<SessionArchiverOptions, "request" | "refreshAccessToken" | "fetch" | "gatewayUrl" | "accessToken" | "folderGate"> & {/** Archiving is on for this device (OMNIRUSH_ARCHIVE_ENABLED) and an account is connected. */enabled: boolean;/** App data, config and cache directories: pruned under a project root, never archived as one. */@@ -94,9 +94,9 @@ export class CaptureHost {log: options.log,...(options.onSessionClosed ? { onSessionClosed: options.onSessionClosed } : {}),});- const { enabled, excludedDirs, baseIdleMs, baseMaxDeferMs, ...auth } = options.archive;+ const { enabled, excludedDirs, folderGate, baseIdleMs, baseMaxDeferMs, ...auth } = options.archive;this.archive = new ProjectArchiveLifecycle({- archiver: new SessionArchiver({ stateDir: options.stateDir, excludedDirs, log: options.log, ...auth }),+ archiver: new SessionArchiver({ stateDir: options.stateDir, excludedDirs, folderGate, log: options.log, ...auth }),enabled: enabled && this.collector.enabled,log: options.log,...(baseIdleMs !== undefined ? { baseIdleMs } : {}),diff --git a/apps/server/src/capture-protocol.ts b/apps/server/src/capture-protocol.tsindex b03b7454..704474be 100644--- a/apps/server/src/capture-protocol.ts+++ b/apps/server/src/capture-protocol.ts@@ -8,6 +8,7 @@* bodies travel as transferred buffers.*/import type { CaptureHost, EngineTarget, PromptRecord } from "./capture-host.js";+import type { FolderGateOptions } from "./session-archive/detect.js";import type { CollectorWebVisit } from "./workspace-collector.js";/** How the capture stops: `archiveFinals` false (the account is gone) packs no final project archives. */@@ -69,6 +70,7 @@ export type CaptureWorkerInit = {archive: {enabled: boolean;excludedDirs: string[];+ folderGate?: FolderGateOptions;request: boolean;refreshAccessToken: boolean;gatewayUrl?: string;diff --git a/apps/server/src/capture-worker.ts b/apps/server/src/capture-worker.tsindex 077a4c0f..d5546a58 100644--- a/apps/server/src/capture-worker.ts+++ b/apps/server/src/capture-worker.ts@@ -122,6 +122,7 @@ const host = new CaptureHost({archive: {enabled: init.archive.enabled,excludedDirs: init.archive.excludedDirs,+ folderGate: init.archive.folderGate,...(init.archive.request? {request: async (path: string, requestInit: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }) => {diff --git a/apps/server/src/project-archive.test.ts b/apps/server/src/project-archive.test.tsindex 01fcb079..3ab27df8 100644--- a/apps/server/src/project-archive.test.ts+++ b/apps/server/src/project-archive.test.ts@@ -4,7 +4,7 @@ import { mkdir, readdir, writeFile } from "node:fs/promises";import { join } from "node:path";import { promisify } from "node:util";-import { createProjectArchive } from "./project-archive.js";+import { createProjectArchive, projectArchiveSettings } from "./project-archive.js";import { FakeArchiveServer, slowPartTwo } from "./session-archive/fake-archive-server.js";import { ARCHIVE_STATE_DIRECTORY } from "./session-archive/index.js";import { cleanupTempDirs, tempDir } from "./session-archive/test-helpers.js";@@ -135,6 +135,14 @@ describe("createProjectArchive", () => {expect(server.calls).toEqual([]);});+ test("the desktop's userData dir (OMNIRUSH_DESKTOP_USER_DATA_DIR) is where the all-folders policy may not archive a folder", async () => {+ const input = { config: serverConfig(await tempDir("state")), gatewayBroker: broker(new FakeArchiveServer()), collectorEnabled: true };+ expect(projectArchiveSettings({ ...input, env: { OMNIRUSH_DESKTOP_USER_DATA_DIR: " /Users/sam/Library/Application Support/ai.omnirush.desktop " } }).folderGate).toEqual({+ userDataDir: "/Users/sam/Library/Application Support/ai.omnirush.desktop",+ });+ expect(projectArchiveSettings({ ...input, env: {} }).folderGate).toEqual({});+ });+test("signed out (no collector account): the same clearing, nothing archived", async () => {const server = new FakeArchiveServer();const gateway = { ...broker(server), enabled: false };diff --git a/apps/server/src/project-archive.ts b/apps/server/src/project-archive.tsindex bbd17ad5..c979b0dd 100644--- a/apps/server/src/project-archive.ts+++ b/apps/server/src/project-archive.ts@@ -10,6 +10,7 @@ import { omnirushConfigDir, omnirushServerDataDir, opencodeCacheDirs, opencodeDaimport type { OmniRushGatewayBroker } from "./omnirush-gateway-broker.js";import { runtimeStorageDir } from "./runtime-db.js";+import type { FolderGateOptions } from "./session-archive/detect.js";import { SessionArchiver, type SessionArchiverOptions } from "./session-archive/index.js";import { ProjectArchiveLifecycle, projectArchiveEnabled, type ArchiveLifecycleLog } from "./session-archive/lifecycle.js";import type { ServerConfig } from "./types.js";@@ -45,13 +46,16 @@ type ProjectArchiveInput = {/*** Where and how a server's archiver works: its state dir, the directories it- * prunes, whether it is on, and how it authenticates. Disabled (signed out,- * or turned off on this device) it gets no bearer, so clearing what a- * previous run left never reaches the network.+ * prunes, where the all-folders policy may not archive a folder (the desktop+ * app's userData dir, which it sets as OMNIRUSH_DESKTOP_USER_DATA_DIR),+ * whether it is on, and how it authenticates. Disabled (signed out, or+ * turned off on this device) it gets no bearer, so clearing what a previous+ * run left never reaches the network.*/export function projectArchiveSettings(input: ProjectArchiveInput): {stateDir: string;excludedDirs: string[];+ folderGate: FolderGateOptions;enabled: boolean;auth: "broker" | "environment" | "none";gatewayUrl?: string;@@ -59,7 +63,8 @@ export function projectArchiveSettings(input: ProjectArchiveInput): {} {const env = input.env ?? process.env;const enabled = projectArchiveEnabled(env) && input.collectorEnabled;- const base = { stateDir: runtimeStorageDir(input.config), excludedDirs: appDirectories(input.config), enabled };+ const userDataDir = env.OMNIRUSH_DESKTOP_USER_DATA_DIR?.trim();+ const base = { stateDir: runtimeStorageDir(input.config), excludedDirs: appDirectories(input.config), folderGate: userDataDir ? { userDataDir } : {}, enabled };if (!enabled) return { ...base, auth: "none" };if (input.gatewayBroker.enabled) return { ...base, auth: "broker" };return {@@ -89,6 +94,7 @@ export function createProjectArchive(input: ProjectArchiveInput & {archiver: new SessionArchiver({stateDir: settings.stateDir,excludedDirs: settings.excludedDirs,+ folderGate: settings.folderGate,log: input.log,...auth,...(input.fetch ? { fetch: input.fetch } : {}),diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex eaf2295e..338ae434 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -873,6 +873,7 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {archive: {enabled: archiveSettings.enabled,excludedDirs: archiveSettings.excludedDirs,+ folderGate: archiveSettings.folderGate,...(archiveSettings.auth === "broker"? {request: (path: string, init: { method: "GET" | "POST"; body?: string; signal?: AbortSignal }) => gatewayBroker.archiveRequest(path, init),diff --git a/apps/server/src/session-archive/README.md b/apps/server/src/session-archive/README.mdindex 2bccb371..49d5d1c8 100644--- a/apps/server/src/session-archive/README.md+++ b/apps/server/src/session-archive/README.md@@ -1,7 +1,8 @@# session-archiveThe desktop side of the OmniRush project archive. When the folder an agent-session started in contains `.git`, the whole folder is uploaded:+session started in contains `.git`, the whole folder is uploaded (with the+all-folders policy on, any other folder too, see "All folders" below):- at session start: a **base** archive, including `.git/` and gitignored content;- after every completed turn that changed anything: a **delta** archive with the@@ -30,7 +31,8 @@ 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; only the `.git` marker is enabled, 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` |+| `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 || `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` |@@ -210,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. 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 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.- 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.@@ -300,6 +302,7 @@ 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.- **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.@@ -335,13 +338,14 @@ 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).+- `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.- `upload.test.ts` (in-process fake API and S3):- the happy path;- resume after a failed part;- 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;- a 401 with a refresh, and a failed refresh;- NoSuchUpload leading to a re-create;- `archive_not_uploading` at complete;@@ -358,7 +362,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`;+ - 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;- 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.tsindex 53417146..377de23c 100644--- a/apps/server/src/session-archive/detect.test.ts+++ b/apps/server/src/session-archive/detect.test.ts@@ -3,7 +3,7 @@ import { mkdir, rm, symlink, writeFile } from "node:fs/promises";import { homedir } from "node:os";import { join, posix, win32 } from "node:path";-import { gitMarkerDetector, gitParentCandidates, gitParentDetector, isArchivableProject, type ProjectMarkerDetector } from "./detect.js";+import { folderDetector, gitMarkerDetector, gitParentCandidates, gitParentDetector, isArchivableProject, refusedFolderRoot, type FolderGateContext, type ProjectMarkerDetector } from "./detect.js";import { cleanupTempDirs, tempDir } from "./test-helpers.js";afterEach(cleanupTempDirs);@@ -201,3 +201,147 @@ describe("project gate", () => {}});});++describe("all-folders gate (4.4)", () => {+ const FOLDER = { archivable: true, reason: "folder", marker: "folder" };+ const NO_MARKER = { archivable: false, reason: "no_marker", marker: null };++ /** A policy stub that counts how often it was asked. */+ function policy(allFolders: boolean) {+ const asked = { count: 0 };+ return { asked, allFolders: async () => (asked.count += 1, allFolders) };+ }++ 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(off.asked.count).toBe(1);+ });++ test("policy on: a folder under home is archivable; home itself is refused before the policy is asked", async () => {+ 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 })];+ 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(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 })];+ 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"));+ await writeFile(join(home, "broken/.git"), "not a gitfile\n");+ expect((await isArchivableProject(join(home, "broken"), detectors)).reason).toBe("gitfile_invalid");+ await mkdir(join(home, "linked"));+ await symlink(join(home, "repo/.git"), join(home, "linked/.git"));+ expect(await isArchivableProject(join(home, "linked"), detectors)).toEqual(NO_MARKER);+ expect(on.asked.count).toBe(0);+ });++ test("the userData dir, a folder inside or above it, a system directory and a path that resolves into one are refused without asking", async () => {+ const home = await tempDir("folder-home");+ const userData = join(home, ".config/OmniRush.ai");+ 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 });+ 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 });+ }+ expect(await isArchivableProject(join(home, "system/share"), [gitMarkerDetector, detector])).toEqual(NO_MARKER);+ expect(on.asked.count).toBe(0);+ });++ test("every refusal, on macOS, Linux and Windows paths", () => {+ const mac: FolderGateContext = { platform: "darwin", homes: ["/Users/sam"], userData: ["/Users/sam/Library/Application Support/ai.omnirush.desktop"] };+ const linux: FolderGateContext = { platform: "linux", homes: ["/home/sam"], userData: ["/home/sam/.config/ai.omnirush.desktop"] };+ const windows: FolderGateContext = { platform: "win32", homes: ["C:\\Users\\sam"], userData: ["C:\\Users\\sam\\AppData\\Roaming\\ai.omnirush.desktop"] };+ const cases: Array<[FolderGateContext, string, ReturnType<typeof refusedFolderRoot>]> = [+ // Filesystem, drive and share roots.+ [mac, "/", "root_too_broad"],+ [mac, "/Volumes", "root_too_broad"],+ [mac, "/Volumes/Backup", "root_too_broad"],+ [linux, "/mnt", "root_too_broad"],+ [linux, "/mnt/c", "root_too_broad"],+ [linux, "/media/sam/usb", "root_too_broad"],+ [windows, "C:\\", "root_too_broad"],+ [windows, "D:\\", "root_too_broad"],+ [windows, "\\\\server\\share", "root_too_broad"],+ [windows, "\\\\server\\share\\", "root_too_broad"],+ // Home itself and anything above it.+ [mac, "/Users/sam", "root_too_broad"],+ [mac, "/users/SAM/", "root_too_broad"],+ [mac, "/Users", "root_too_broad"],+ [linux, "/home/sam", "root_too_broad"],+ [linux, "/home", "root_too_broad"],+ [windows, "C:\\Users\\sam", "root_too_broad"],+ [windows, "c:\\users\\SAM", "root_too_broad"],+ [windows, "C:\\Users", "root_too_broad"],+ // The userData dir, inside it, and above it.+ [mac, "/Users/sam/Library/Application Support/ai.omnirush.desktop", "root_app_data"],+ [mac, "/Users/sam/Library/Application Support/ai.omnirush.desktop/managed-opencode-workdir", "root_app_data"],+ [mac, "/Users/sam/Library/Application Support", "root_app_data"],+ [mac, "/Users/sam/Library", "root_app_data"],+ [linux, "/home/sam/.config/ai.omnirush.desktop", "root_app_data"],+ [linux, "/home/sam/.config", "root_app_data"],+ [windows, "C:\\Users\\sam\\AppData\\Roaming\\ai.omnirush.desktop\\logs", "root_app_data"],+ [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"],+ // 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"]),+ ...[+ "C:\\Windows",+ "C:\\Windows\\System32",+ "c:\\windows",+ "D:\\Windows",+ "C:\\Program Files",+ "C:\\Program Files\\App",+ "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",+ ].map((root): [FolderGateContext, string, "root_system"] => [windows, root, "root_system"]),+ // 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, "/Volumes/Backup/projects/x", null],+ [mac, "/Users/other/x", null],+ [linux, "/home/sam/omnirush.ai", null],+ [linux, "/home/sam/.local/share/x", null],+ [linux, "/srv/site", null],+ [linux, "/tmp/scratch", null],+ [linux, "/Library/x", "root_system"],+ [windows, "C:\\Users\\sam\\omnirush.ai", null],+ [windows, "C:\\Users\\sam\\projects\\x", null],+ [windows, "D:\\work\\x", null],+ [windows, "D:\\Windows Backup", null],+ [windows, "\\\\server\\share\\projects\\x", null],+ ];+ for (const [context, root, expected] of cases) {+ expect({ root, refused: refusedFolderRoot(root, context) }).toEqual({ root, refused: expected });+ }+ });++ 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");+ expect(refusedFolderRoot("/usr/share/x", { platform: "linux", homes: ["/"], userData: [] })).toBe("root_system");+ });+});diff --git a/apps/server/src/session-archive/detect.ts b/apps/server/src/session-archive/detect.tsindex 78324ccb..4584e623 100644--- a/apps/server/src/session-archive/detect.ts+++ b/apps/server/src/session-archive/detect.ts@@ -2,17 +2,19 @@* The project gate (section 4): a session is archived only when the folder* 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.+ * (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.*/import { lstat, open, realpath } from "node:fs/promises";import { homedir } from "node:os";-import path, { isAbsolute, join, parse, relative, resolve, sep } from "node:path";+import path, { isAbsolute, join, parse, posix, relative, resolve, sep, win32 } from "node:path";export type ArchivableProject = {archivable: boolean;- /** git_dir | git_file | git_parent | no_marker | gitfile_invalid | root_not_directory | root_too_broad */+ /** git_dir | git_file | git_parent | folder | no_marker | gitfile_invalid | root_not_directory | root_too_broad */reason: string;- /** The marker that qualified the root (".git"), null when not archivable. */+ /** The marker that qualified the root (".git", "folder"), null when not archivable. */marker: string | null;};@@ -222,6 +224,86 @@ 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) ------------------------------++/** 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";++/** 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"];+/** Windows system directories, on every drive. */+const WINDOWS_SYSTEM_DIRS = ["Windows", "Program Files", "Program Files (x86)", "ProgramData"];+/** 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;++/** What the all-folders gate compares a root with; every path absolute, in `platform`'s syntax. */+export type FolderGateContext = {+ platform: NodeJS.Platform;+ /** The home directory, as given and resolved through symlinks. */+ homes: readonly string[];+ /** The Electron userData directory, the same way; empty when unknown. */+ userData: readonly string[];+};++/**+ * 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.+ */+export function refusedFolderRoot(root: string, context: FolderGateContext): "root_too_broad" | "root_app_data" | "root_system" | 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 within = (child: string, parent: string) => {+ const rel = paths.relative(parent, child);+ return rel === "" || (!rel.startsWith(`..${paths.sep}`) && rel !== ".." && !paths.isAbsolute(rel));+ };+ 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 (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 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;+}++export type FolderGateOptions = {+ /** The Electron userData directory (the desktop sets OMNIRUSH_DESKTOP_USER_DATA_DIR); unknown outside the desktop app. */+ userDataDir?: string;+ /** Tests only; defaults to os.homedir(). */+ homeDir?: string;+};++/**+ * 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.+ */+export function folderDetector(allFolders: () => Promise<boolean>, 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;+ } catch {+ return null;+ }+ };+}+/*** Whether the session root is a project to archive: the root checks first,* then the detectors in order; the first archivable result wins, else thediff --git a/apps/server/src/session-archive/fake-archive-server.ts b/apps/server/src/session-archive/fake-archive-server.tsindex a445d5b6..081d8736 100644--- a/apps/server/src/session-archive/fake-archive-server.ts+++ b/apps/server/src/session-archive/fake-archive-server.ts@@ -43,6 +43,8 @@ export class FakeArchiveServer {gate: { status: number; detail: string } | null = null;/** A backend without final archives: a delta's turn must be greater than its parent's (409 archive_parent_mismatch otherwise). */strictTurns = false;+ /** The key route's `policy` (backend spec 4.4), left out while undefined. */+ policy: unknown = undefined;readonly archives = new Map<string, FakeArchive>();readonly calls: FakeCall[] = [];readonly puts: Array<{ archiveId: string; partNumber: number; status: number }> = [];@@ -121,7 +123,7 @@ export class FakeArchiveServer {if (authorization !== `Bearer ${this.token}`) return json(401, { detail: "invalid_token" });const abort = /^archives\/([^/]+)\/abort$/.exec(path);if (this.gate && !abort) return json(this.gate.status, { detail: this.gate.detail });- if (method === "GET" && path === "archives/key") return json(200, { kid: testKeys.kid, public_key: testKeys.publicB64, alg: "X25519-HKDF-SHA256-A256GCM" });+ if (method === "GET" && path === "archives/key") return json(200, { kid: testKeys.kid, public_key: testKeys.publicB64, alg: "X25519-HKDF-SHA256-A256GCM", policy: this.policy });if (method === "POST" && path === "archives") return this.create(body);const parts = /^archives\/([^/]+)\/parts$/.exec(path);if (method === "POST" && parts) return this.listParts(parts[1]!, body);diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex 610c1dee..725517ab 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -141,13 +141,33 @@ describe("SessionArchiver", () => {expect(names).toEqual(expect.arrayContaining([".env", "id_rsa"]));});- test("a folder without .git is not archived and nothing is sent", async () => {+ test("a folder without .git is not archived while the all-folders policy is off or absent; only the key is read", async () => {+ for (const policy of [undefined, { all_folders: false }, { all_folders: "true" }]) {+ const server = new FakeArchiveServer();+ server.policy = policy;+ const home = await tempDir("home");+ const root = join(home, "plain");+ await mkdir(root);+ await writeFile(join(root, "notes.md"), "hello");+ const state = await tempDir("state");+ const subject = archiver(server, state, { folderGate: { homeDir: home } });+ expect(await subject.captureBase("ses_plain_folder", root)).toEqual({ status: "skipped", reason: "not_archivable" });+ expect(await subject.captureDelta("ses_plain_folder", root, 1)).toEqual({ status: "skipped", reason: "no_base" });+ expect(server.callPaths()).toEqual(["GET archives/key 200"]);+ expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "sessions"))).toEqual([]);+ }+ });++ test("with the all-folders policy on, home itself, the app's userData dir and a system directory are refused and nothing is sent", async () => {const server = new FakeArchiveServer();- const root = await tempDir("plain");- await writeFile(join(root, "notes.md"), "hello");- const subject = archiver(server, await tempDir("state"));- expect(await subject.captureBase("ses_plain_folder", root)).toEqual({ status: "skipped", reason: "not_archivable" });- expect(await subject.captureDelta("ses_plain_folder", root, 1)).toEqual({ status: "skipped", reason: "no_base" });+ server.policy = { all_folders: true };+ const home = await tempDir("home");+ const userData = join(home, "Library/Application Support/OmniRush.ai");+ await mkdir(userData, { recursive: true });+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home, userDataDir: userData } });+ for (const root of [home, userData, join(home, "Library"), "/usr"]) {+ expect({ root, result: await subject.captureBase("ses_refused_folder", root) }).toEqual({ root, result: { status: "skipped", reason: "not_archivable" } });+ }expect(server.calls).toEqual([]);});@@ -214,7 +234,8 @@ describe("SessionArchiver", () => {const detectors = [gitMarkerDetector, (dir: string) => gitParentDetector(dir, { systemDirs: [], homeDir: home })];const subject = archiver(server, await tempDir("state"), { detectors });expect(await subject.captureBase("ses_empty_git", root)).toEqual({ status: "skipped", reason: "not_archivable" });- expect(server.calls).toEqual([]);+ // At most the all-folders policy is asked (it is off); nothing is created.+ expect(server.calls.filter((call) => call.path !== "archives/key")).toEqual([]);// The archive's git, with this folder as the OS home, stops below home: no git block for the folder, nor for// proj itself, whose empty .git passes the gate as git_dir.@@ -226,6 +247,65 @@ describe("SessionArchiver", () => {expect(JSON.parse(stdout)).toEqual([null, null]);});+ test("with the all-folders policy on, a folder without .git is archived whole: binaries and ignored-looking files kept, credentials left out", async () => {+ const server = new FakeArchiveServer();+ server.policy = { all_folders: true };+ const home = await tempDir("home");+ const root = join(home, "render-job");+ await mkdir(join(root, "node_modules/left-pad"), { recursive: true });+ await writeFile(join(root, ".gitignore"), "node_modules/\ndist/\n*.blend1\n");+ await writeFile(join(root, "node_modules/left-pad/index.js"), "module.exports = (s) => s;\n");+ await mkdir(join(root, "dist"));+ await writeFile(join(root, "dist/bundle.js"), "console.log(1);\n");+ await mkdir(join(root, "assets"));+ const blend = randomBytes(300 * 1024);+ await writeFile(join(root, "assets/scene.blend"), blend);+ const backup = randomBytes(64 * 1024);+ await writeFile(join(root, "assets/scene.blend1"), backup);+ await writeFile(join(root, ".env"), "API_KEY=sk-live-1234567890\n");+ await writeFile(join(root, "id_rsa"), "-----BEGIN OPENSSH PRIVATE KEY-----\n");+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: home } });++ const base = await subject.captureBase("ses_plain_folder", root);+ expect(base).toMatchObject({ status: "queued", kind: "base", sequence: 0 });+ expect((await subject.drain()).uploaded).toBe(1);+ // One key read serves the policy and the seal.+ expect(server.callPaths()[0]).toBe("GET archives/key 200");+ expect(server.calls.filter((call) => call.path === "archives/key")).toHaveLength(1);+ const [first] = server.objects();+ expect(first!.request).toMatchObject({ session_id: "ses_plain_folder", kind: "base", sequence: 0, marker: "folder" });+ const members = await openArchive(first!.object!);+ const names = members.map((member) => member.name);+ expect(names).toEqual(expect.arrayContaining([".gitignore", "node_modules/left-pad/index.js", "dist/bundle.js", "assets/scene.blend", "assets/scene.blend1"]));+ expect(names).not.toContain(".env");+ expect(names).not.toContain("id_rsa");+ expect(members.find((member) => member.name === "assets/scene.blend")!.content.equals(blend)).toBe(true);+ expect(members.find((member) => member.name === "assets/scene.blend1")!.content.equals(backup)).toBe(true);+ expect(manifestOf(members)).toMatchObject({ kind: "base", workspace: { label: "render-job", marker: "folder", git: null }, excluded: { credential: 2 } });++ const edited = randomBytes(1024);+ await writeFile(join(root, "assets/scene.blend"), edited);+ expect(await subject.captureDelta("ses_plain_folder", root, 1)).toMatchObject({ status: "queued", kind: "delta", sequence: 1 });+ expect((await subject.drain()).uploaded).toBe(1);+ const second = server.objects()[1]!;+ expect(second.request).toMatchObject({ kind: "delta", sequence: 1, marker: "folder", parent_archive_id: first!.request.archive_id });+ const changed = (await openArchive(second.object!)).slice(1);+ expect(changed.map((member) => member.name)).toEqual(["assets/scene.blend"]);+ expect(changed[0]!.content.equals(edited)).toBe(true);+ });++ test("with the all-folders policy on, a git project is still archived as git", async () => {+ const server = new FakeArchiveServer();+ server.policy = { all_folders: true };+ const { root } = await project();+ const subject = archiver(server, await tempDir("state"), { folderGate: { homeDir: await tempDir("home") } });+ expect(await subject.captureBase("ses_git_first", root)).toMatchObject({ status: "queued", kind: "base" });+ await subject.drain();+ expect(server.objects()[0]!.request.marker).toBe(".git");+ expect(manifestOf(await openArchive(server.objects()[0]!.object!))).toMatchObject({ workspace: { marker: ".git" } });+ expect(server.calls.filter((call) => call.path === "archives/key")).toHaveLength(1);+ });+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" };diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 0011f4b4..649b0f0f 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -1,6 +1,7 @@/*** SessionArchiver (section 13): the desktop side of the OmniRush project- * archive. A session whose root holds a `.git` gets a base archive of the+ * archive. A session whose root holds a `.git` (or, with the all-folders+ * 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@@ -14,7 +15,7 @@ import { join, resolve } from "node:path";import { z } from "zod";import { externalFetch } from "../server-fetch.js";-import { defaultProjectDetectors, isArchivableProject, type ProjectMarkerDetector } from "./detect.js";+import { defaultProjectDetectors, folderDetector, isArchivableProject, type FolderGateOptions, type ProjectMarkerDetector } from "./detect.js";import { hintGarbageCollection, readJsonFile, stateKey, writeChunksAtomic, writeJsonAtomic } from "./files.js";import {ArchiveHashCache,@@ -41,6 +42,7 @@ import {type ArchiveFetch,type ArchiveKey,type ArchiveLog,+ type KeyResult,type RetryPolicy,} from "./upload.js";@@ -81,6 +83,8 @@ 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). */+ folderGate?: FolderGateOptions;log?: ArchiveLog;/** Tests. */now?: () => Date;@@ -227,6 +231,7 @@ export class SessionArchiver {private readonly dirs: { sessions: string; baselines: string; hashCache: string; queue: string; pending: string; tmp: string };private readonly uploader: ArchiveUploader;private readonly detectors: readonly ProjectMarkerDetector[];+ private readonly folderGate: FolderGateOptions | undefined;private readonly appDirs: string[];private readonly includeCredentials: boolean;private readonly log: ArchiveLog;@@ -281,6 +286,7 @@ export class SessionArchiver {log: this.log,});this.detectors = options.detectors ?? defaultProjectDetectors;+ this.folderGate = options.folderGate;this.appDirs = [stateDir, ...(options.excludedDirs ?? []).map((dir) => resolve(dir))];this.includeCredentials = options.archiveIncludeCredentialFiles === true;}@@ -545,9 +551,16 @@ 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" };- const gate = await isArchivableProject(root, this.detectors, { appDirs: this.appDirs });+ // 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 });if (!gate.archivable || !gate.marker) return { status: "skipped", reason: "not_archivable" };- const fetched = await this.uploader.fetchKey();+ const fetched = await fetchKey();if (fetched.status === "disabled") {await this.disable(fetched.code);return { status: "skipped", reason: "disabled" };diff --git a/apps/server/src/session-archive/policy.ts b/apps/server/src/session-archive/policy.tsnew file mode 100644index 00000000..33bda97a--- /dev/null+++ b/apps/server/src/session-archive/policy.ts@@ -0,0 +1,15 @@+/**+ * 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.+ */+import { z } from "zod";++export type ArchivePolicy = { allFolders: boolean };++const allFoldersSchema = z.object({ policy: z.object({ all_folders: z.literal(true) }) });++export function parseArchivePolicy(body: unknown): ArchivePolicy {+ return { allFolders: allFoldersSchema.safeParse(body).success };+}diff --git a/apps/server/src/session-archive/upload.test.ts b/apps/server/src/session-archive/upload.test.tsindex f4e0d457..0a90e0c5 100644--- a/apps/server/src/session-archive/upload.test.ts+++ b/apps/server/src/session-archive/upload.test.ts@@ -81,6 +81,29 @@ 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 () => {+ 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],+ ];+ for (const [policy, allFolders] 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 });+ }+ });+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.tsindex 116f83bb..61420468 100644--- a/apps/server/src/session-archive/upload.ts+++ b/apps/server/src/session-archive/upload.ts@@ -7,6 +7,7 @@import { open, type FileHandle } from "node:fs/promises";import { z } from "zod";+import { parseArchivePolicy, type ArchivePolicy } from "./policy.js";import { SEAL_ALG, SEAL_CONTENT, sealKid } from "./seal.js";export type ArchiveFetch = (input: string, init?: RequestInit) => Promise<Response>;@@ -120,7 +121,8 @@ export type ArchiveUploadJob = {export type ArchiveKey = { kid: string; publicKey: Buffer; alg: string };export type KeyResult =- | { status: "ok"; key: ArchiveKey }+ /** The policy comes with the key, in the same response. */+ | { status: "ok"; key: ArchiveKey; policy: ArchivePolicy }| { status: "disabled"; code: string }| { status: "unavailable"; reason: string };@@ -317,7 +319,7 @@ export class ArchiveUploader {if (publicKey.length !== 32 || publicKey.toString("base64") !== parsed.data.public_key || sealKid(publicKey) !== parsed.data.kid || parsed.data.alg !== SEAL_ALG) {return { status: "unavailable", reason: "invalid key response" };}- return { status: "ok", key: { kid: parsed.data.kid, publicKey, alg: parsed.data.alg } };+ return { status: "ok", key: { kid: parsed.data.kid, publicKey, alg: parsed.data.alg }, policy: parseArchivePolicy(result.body) };}if (result.kind === "error" && result.status === 404) return { status: "disabled", code: "archive_routes_missing" };const outcome = this.common(result);diff --git a/docs/project-archive.md b/docs/project-archive.mdindex 2f66b5ad..f3ca4696 100644--- a/docs/project-archive.md+++ b/docs/project-archive.md@@ -35,11 +35,23 @@ 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.+ Nothing is archived for other folders, unless omnirush.ai turns on+ archiving for every folder (see below).- 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.+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.+Sub-agent chats (tasks the agent starts on its own) are not archived. Theywork in the same folder as the chat that started them, which is alreadyarchived.