A desktop coding agent with free access to frontier models.
TypeScript
fix: retry project copy after policy outage
ogsamrat834a2c11f269
diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.tsindex b331075b..cc52c850 100644--- a/apps/server/src/session-archive/index.test.ts+++ b/apps/server/src/session-archive/index.test.ts@@ -383,8 +383,11 @@ describe("SessionArchiver", () => {refreshAccessToken: async () => (refreshes.push("refresh"), server.token),});const started = Date.now();+ // No answer (502, 401, offline) is no refusal: the base is to be tried again (the lifecycle retries it).+ // An answer that says off (428, 503 archive_disabled) is one.+ const expected = ["502", "401", "network"].includes(name) ? "unavailable" : "not_archivable";for (const dir of ["notes", "drafts", "scratch"]) {- expect({ name, result: reasonOf(await subject.captureBase(`ses_${dir}_folder`, join(home, dir))) }).toEqual({ name, result: { status: "skipped", reason: "not_archivable" } });+ expect({ name, result: reasonOf(await subject.captureBase(`ses_${dir}_folder`, join(home, dir))) }).toEqual({ name, result: { status: "skipped", reason: expected } });}expect(Date.now() - started).toBeLessThan(2_000);// One probe for the burst, kept as off; nothing slept, refreshed, written or switched off.diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 507c36d3..48c12883 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -538,7 +538,7 @@ export class SessionArchiver {private key: ArchiveKey | null = null;private disabled: string | null = null;/** The last folder policy answer, kept with the key for POLICY_TTL_MS; a failed probe is kept as off. */- private policy: { value: ArchivePolicy; at: number } | null = null;+ private policy: { value: ArchivePolicy; at: number; outage?: boolean } | 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>>();@@ -1235,10 +1235,15 @@ export class SessionArchiver {const touched = gate.marker === TOUCHED_MARKER;// A session that is not archived keeps no touched paths; any other keeps them.if (!gate.archivable || !gate.marker) {+ // no_marker: a folder without git while the server's folder policies are off (or could not be asked, now or+ // in the outage kept for a minute).+ const outage = probe.fetched ? probe.fetched.status === "unavailable" : this.policy?.outage === true;+ const detail = gate.reason === "no_marker" ? (outage ? "policy_unavailable" : "no_git_folder_policy_off") : gate.reason;+ // The policy could not be asked (offline, a timeout, a 5xx, a bearer to refresh): no answer is no+ // refusal. The base is tried again (the lifecycle's next prompt, turn or retry), its paths kept.+ if (detail === "policy_unavailable") return { status: "skipped", reason: "unavailable", detail };this.dropPrescan(sessionId);await this.touched.forget(sessionId);- // no_marker: a folder without git while the server's folder policies are off (or could not be asked).- const detail = gate.reason === "no_marker" ? (probe.fetched?.status === "unavailable" ? "policy_unavailable" : "no_git_folder_policy_off") : gate.reason;return { status: "skipped", reason: "not_archivable", detail };}// The key and the consent check (7.2): a probe made for this base already holds them; else the full fetch.@@ -1374,7 +1379,7 @@ export class SessionArchiver {// An outage (timeout, 5xx, offline) is kept as off for a minute only, not POLICY_TTL_MS: a folder// without git that starts during a blip is asked again at its next turn instead of going without a base.const at = this.now().getTime() - (fetched.status === "unavailable" ? Math.max(0, POLICY_TTL_MS - POLICY_OUTAGE_TTL_MS) : 0);- this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at };+ this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at, ...(fetched.status === "unavailable" ? { outage: true } : {}) };// The uploader's rules follow the server's switch; anything but a yes keeps the old rules.const rulesBefore = redactionV3();setRedactionPolicy(fetched.status === "ok" && fetched.policy.redactionV3 === true ? CLIENT_V3 : "legacy");diff --git a/apps/server/src/session-archive/lifecycle.test.ts b/apps/server/src/session-archive/lifecycle.test.tsindex 424322ba..f8d75966 100644--- a/apps/server/src/session-archive/lifecycle.test.ts+++ b/apps/server/src/session-archive/lifecycle.test.ts@@ -492,6 +492,38 @@ describe("ProjectArchiveLifecycle", () => {expect(slow.reads).toEqual({ session: 2, messages: 0 });});+ test("a base whose folder policy could not be asked is tried again at the next turn, not skipped for the session", async () => {+ const archiver = new FakeArchiver();+ const { subject, logs } = lifecycle(archiver);+ const root = await tempDir("root");+ const outage: CaptureResult = { status: "skipped", reason: "unavailable", detail: "policy_unavailable" };+ let serverUp = false;+ archiver.base = async () => (serverUp ? queued("base") : outage);+ subject.sessionStarted({ sessionId: "ses_policy_0001", root, engine: engine().reader });+ await subject.settled();+ expect(archiver.captures()).toEqual([`base ses_policy_0001 ${root} 0`]);+ expect(logs.map((log) => log.attributes?.reason)).toEqual(["the folder policy could not be asked"]);+ // The server answers again by the turn's end: the base is taken then, the turn's delta after it.+ serverUp = true;+ subject.turnCompleted("ses_policy_0001", messages(1));+ await subject.settled();+ expect(archiver.captures()).toEqual([`base ses_policy_0001 ${root} 0`, `base ses_policy_0001 ${root} 1`, `delta ses_policy_0001 ${root} 1`]);+ await subject.stop({ finals: false });+ });++ test("a folder policy that is really off still ends the session's archiving: no base asked again", async () => {+ const archiver = new FakeArchiver();+ const { subject } = lifecycle(archiver);+ const root = await tempDir("root");+ archiver.base = async () => ({ status: "skipped", reason: "not_archivable", detail: "no_git_folder_policy_off" });+ subject.sessionStarted({ sessionId: "ses_policy_0002", root, engine: engine().reader });+ await subject.settled();+ subject.turnCompleted("ses_policy_0002", messages(1));+ await subject.settled();+ expect(archiver.captures()).toEqual([`base ses_policy_0002 ${root} 0`]);+ await subject.stop({ finals: false });+ });+test("a session still unreadable when its turn completes stays pending; a child resolved at turn end is never archived", async () => {const archiver = new FakeArchiver();const { subject, logs } = lifecycle(archiver);diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex 7f6e6fe1..14c9f5dd 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -89,6 +89,11 @@ export function projectArchiveOutcome(result: CaptureResult): ProjectArchiveOutcreturn { status: "skipped", reason: result.reason, ...(result.detail ? { detail: result.detail } : {}) };}+/** A base the server's folder policy could not be asked for: not a refusal, the start step runs again (next prompt, turn or retry timer). */+export function baseRetried(result: CaptureResult): boolean {+ return result.status === "skipped" && result.reason === "unavailable" && result.detail === "policy_unavailable";+}+const DEFAULT_CONSENT_RECHECK_MS = 10 * 60_000;/** The quiet window after a turn: with no new prompt by then, the session gets a final archive. */export const FINAL_IDLE_MS = 10 * 60_000;@@ -608,9 +613,9 @@ export class ProjectArchiveLifecycle {record.root = root;const result = await this.archiver.captureBase(sessionId, root, count);this.reportArchive(sessionId, projectArchiveOutcome(result));- if (result.status === "paused") {+ if (result.status === "paused" || baseRetried(result)) {record.start = { root, engine: start.engine };- this.unresolved(sessionId, record, `archive paused: ${result.reason}`);+ this.unresolved(sessionId, record, result.status === "paused" ? `archive paused: ${result.reason}` : "the folder policy could not be asked");}if (result.status === "skipped" && result.reason === "disabled") this.sessions.delete(sessionId);if (result.status === "skipped" && (result.reason === "not_archivable" || result.reason === "stopped")) record.root = null;diff --git a/apps/server/src/session-archive/project-scope.test.ts b/apps/server/src/session-archive/project-scope.test.tsindex c37c9954..d4821b1c 100644--- a/apps/server/src/session-archive/project-scope.test.ts+++ b/apps/server/src/session-archive/project-scope.test.ts@@ -7,7 +7,7 @@ import { zstdDecompressSync } from "node:zlib";import { SessionUploader } from "../session-uploader.js";import { looksLikeProject, projectArchiveSkip } from "./detect.js";-import { SessionArchiver } from "./index.js";+import { POLICY_OUTAGE_TTL_MS, POLICY_TTL_MS, SessionArchiver } from "./index.js";import { parseArchivePolicy } from "./policy.js";import { openSealedBuffer, SEAL_ALG, sealKeyring } from "./seal.js";import { readTar } from "./test-helpers.js";@@ -132,6 +132,45 @@ describe("a folder without git", () => {expect([reasons.get("node_modules"), reasons.get(".env")]).toEqual(["regenerable", "credential"]);});+ test("a folder policy the server did not answer is a retry, not a refusal: now, from the kept outage, and from a 401", async () => {+ const stateDir = temp("omnirush-scope-state-");+ const keyring = sealKeyring([randomBytes(32)]);+ const { publicRaw, kid } = [...keyring.values()][0]!;+ let server: "on" | "down" | "unauthorized" | "off" = "down";+ let asked = 0;+ const request = async (route: string) => {+ if (route !== "archives/key") return new Response("{}", { status: 404 });+ asked += 1;+ if (server === "down") return new Response("{}", { status: 503 });+ if (server === "unauthorized") return new Response("{}", { status: 401 });+ const policy = server === "on" ? { all_folders: false, touched_files: true, project_folders: true, capture_v2: true } : { all_folders: false, touched_files: false, project_folders: false };+ return Response.json({ public_key: publicRaw.toString("base64"), kid, alg: SEAL_ALG, policy });+ };+ let clock = Date.now();+ const archiver = new SessionArchiver({ stateDir, request: request as never, excludedDirs: [stateDir], log: () => undefined, touchedFlushMs: 10, now: () => new Date(clock) });+ stops.push(() => archiver.stop({ budgetMs: 1000 }));+ const root = temp("omnirush-scope-outage-");+ write(join(root, "notes.txt"), "hello\n");+ const outage = { status: "skipped", reason: "unavailable", detail: "policy_unavailable" } as const;+ expect(await archiver.captureBase("ses_scope_outage", root, 0)).toEqual(outage);+ // Within the minute the outage is kept: still said as an outage (it used to read "policy off").+ expect(await archiver.captureBase("ses_scope_outage_kept", root, 0)).toEqual(outage);+ // A 401 (a bearer to refresh) is an outage too.+ server = "unauthorized";+ clock += POLICY_OUTAGE_TTL_MS + 1_000;+ expect(await archiver.captureBase("ses_scope_outage_401", root, 0)).toEqual(outage);+ // The server answers again once the outage is no longer kept: the same session's base goes through.+ server = "on";+ clock += POLICY_OUTAGE_TTL_MS + 1_000;+ const before = asked;+ expect(await archiver.captureBase("ses_scope_outage", root, 1)).toEqual({ status: "skipped", reason: "unchanged", detail: "touched_files_lazy" });+ expect(asked).toBe(before + 1);+ // A real off is still a refusal.+ server = "off";+ clock += POLICY_TTL_MS + 1_000;+ expect(await archiver.captureBase("ses_scope_off", root, 0)).toEqual({ status: "skipped", reason: "not_archivable", detail: "no_git_folder_policy_off" });+ });+test("without project_folders (an older backend) a project folder stays in touched scope", async () => {const { archiver } = rig({ all_folders: false, touched_files: true, capture_v2: true });const root = temp("omnirush-scope-oldbackend-");