A desktop coding agent with free access to frontier models.
TypeScript
fix: new chats wait for the server's settings
ogsamrat6ec2d6685cda
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 5ad9d93f..f3dd2d28 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -59,7 +59,7 @@ import {} from "./manifest.js";import { writeSealedArchive } from "./pack.js";import { POLICY_OFF, type ArchivePolicy } from "./policy.js";-import { CLIENT_RAW, CLIENT_V3, defaultRedactionPolicy, setRedactionPolicy } from "../context/redact-policy.js";+import { CLIENT_RAW, CLIENT_V3, defaultRedactionPolicy, setRedactionPolicy, type RedactionPolicy } from "../context/redact-policy.js";import { SEAL_CONTENT } from "./seal.js";import { findLockfiles, scanTouchedFiles, touchedChange, TouchedPathStore, type TouchedScanResult } from "./touched.js";import { isOutsideArchivePath, outsideSourcesOf, scanOutsideFiles } from "./outside.js";@@ -1392,12 +1392,27 @@ export class SessionArchiver {* answer when this call made one: the base reuses it for its key.*//**- * The server's capture switch read now if it is not known yet (the same cached probe as the folder policy):- * afterwards defaultRedactionPolicy() is the server's answer, so a chat starting now pins it, not the- * pre-probe "legacy" default.+ * The server's capture switch as last answered (GET /archives/key, the folder policy's cached probe): the+ * policy a chat starting now pins (client-raw, client-v3, or a confirmed legacy when the server answered+ * without either or turned archiving off). Null while it is unresolved: no answer yet, or only an outage+ * (timeout, 5xx, offline), which is never taken as an answer.*/- async settleCapturePolicy(): Promise<void> {- await this.folderPolicy(this.generation);+ async settleCapturePolicy(): Promise<RedactionPolicy | null> {+ const kept = this.policy;+ if (kept && !kept.outage && this.now().getTime() - kept.at < POLICY_TTL_MS) return defaultRedactionPolicy();+ // Not known, or known only as an outage: ask now (one probe shared with the folder policy).+ 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, this.generation, true);+ return fetched.status === "unavailable" ? null : defaultRedactionPolicy();}private async folderPolicy(generation: number): Promise<{ policy: ArchivePolicy; fetched: KeyResult | null }> {diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex c48312a9..c98554ec 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -207,9 +207,9 @@ const UNRESOLVED_RETRY_DELAYS_MS = [60_000, 2 * 60_000, 5 * 60_000, 10 * 60_000]export class ProjectArchiveLifecycle {private readonly archiver: ProjectArchiver;- /** The server's capture switch, read once if not known yet (SessionArchiver.settleCapturePolicy). */- async settleCapturePolicy(): Promise<void> {- await this.archiver.settleCapturePolicy?.();+ /** The server's capture switch as answered, or null while unresolved (SessionArchiver.settleCapturePolicy). */+ async settleCapturePolicy(): Promise<string | null> {+ return (await this.archiver.settleCapturePolicy?.()) ?? null;}private readonly log: ArchiveLifecycleLog;private readonly concurrency: number;diff --git a/apps/server/src/session-uploader-policy-settle.test.ts b/apps/server/src/session-uploader-policy-settle.test.tsindex 7ef2c3e4..fd7e9e95 100644--- a/apps/server/src/session-uploader-policy-settle.test.ts+++ b/apps/server/src/session-uploader-policy-settle.test.ts@@ -1,10 +1,10 @@import { afterEach, describe, expect, test } from "bun:test";-import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { zstdDecompressSync } from "node:zlib";-import { defaultRedactionPolicy, setRedactionPolicy } from "./context/redact-policy.js";+import { setRedactionPolicy } from "./context/redact-policy.js";import { SessionUploader } from "./session-uploader.js";const roots: string[] = [];@@ -21,66 +21,88 @@ function envelopePolicies(uploads: Array<Record<string, unknown>>): string[] {});}-async function newChat(serverSays: "client-v3" | "client-raw", settleDelayMs: number) {+/**+ * A new chat on an app that has not read the server's capture switch yet (process default legacy). `answers`+ * plays the archiver's /archives/key probe: each call takes the next entry; null is an outage (no answer).+ */+async function newChat(answers: Array<"client-v3" | "client-raw" | "legacy" | null | "hang">, opts: { stateDir?: string } = {}) {const root = await mkdtemp(join(tmpdir(), "omnirush-policy-settle-"));roots.push(root);await writeFile(join(root, "app.txt"), "hello\n");- setRedactionPolicy("legacy"); // the app just started: the server's switch not read yet+ setRedactionPolicy("legacy");const uploads: Array<Record<string, unknown>> = [];- let probes = 0;+ let calls = 0;const uploader = new SessionUploader({+ ...(opts.stateDir ? { stateDir: opts.stateDir } : {}),upload: async (_sessionId, compressed) => {uploads.push(JSON.parse(zstdDecompressSync(compressed).toString("utf8")));return Response.json({ ok: true }, { status: 201 });},- // The archiver's /archives/key probe: the server's switch arrives after a while.settleCapturePolicy: async () => {- probes += 1;- await new Promise((resolve) => setTimeout(resolve, settleDelayMs));- setRedactionPolicy(serverSays);+ const answer = answers[Math.min(calls, answers.length - 1)];+ calls += 1;+ if (answer === "hang") return new Promise<string | null>(() => {});+ if (answer) setRedactionPolicy(answer);+ return answer ?? null;},changeDebounceMs: 10,fallbackScanMs: 60_000,});- const sessionId = `ses-settle-${serverSays}`;- uploader.startSession(sessionId, "ws", root);- const pinned = await uploader.settledPolicy(sessionId);- uploader.recordTrace(sessionId, "tool.call", { name: "x" });- uploader.flushTrace(sessionId);- await uploader.stop();- return { pinned, uploads, probes };+ return { uploader, uploads, calls: () => calls, root };}-describe("a new chat pins the server's capture switch, not the pre-probe default", () => {+describe("a new chat pins only the server's answer to its capture switch", () => {test("v3 server: the pin (what the strict bind sends) is client-v3, and every upload agrees", async () => {- const { pinned, uploads, probes } = await newChat("client-v3", 50);- expect(probes).toBe(1);- expect(pinned).toBe("client-v3");+ const { uploader, uploads, root } = await newChat(["client-v3"]);+ uploader.startSession("ses-settle-v3", "ws", root);+ expect(await uploader.settledPolicy("ses-settle-v3")).toBe("client-v3");+ uploader.recordTrace("ses-settle-v3", "tool.call", { name: "x" });+ uploader.flushTrace("ses-settle-v3");+ await uploader.stop();expect(uploads.length).toBeGreaterThan(0);expect(new Set(envelopePolicies(uploads))).toEqual(new Set(["client-v3"]));});test("raw server (prod today): the pin is client-raw, and every upload agrees", async () => {- const { pinned, uploads } = await newChat("client-raw", 50);- expect(pinned).toBe("client-raw");+ const { uploader, uploads, root } = await newChat(["client-raw"]);+ uploader.startSession("ses-settle-raw", "ws", root);+ expect(await uploader.settledPolicy("ses-settle-raw")).toBe("client-raw");+ uploader.recordTrace("ses-settle-raw", "tool.call", { name: "x" });+ uploader.flushTrace("ses-settle-raw");+ await uploader.stop();expect(new Set(envelopePolicies(uploads))).toEqual(new Set(["client-raw"]));});- test("a switch that never answers holds the chat at most POLICY_SETTLE_MS, then the known default", async () => {- const root = await mkdtemp(join(tmpdir(), "omnirush-policy-settle-"));- roots.push(root);- setRedactionPolicy("legacy");- const uploader = new SessionUploader({- upload: async () => Response.json({ ok: true }, { status: 201 }),- settleCapturePolicy: () => new Promise<void>(() => {}),- changeDebounceMs: 10,- fallbackScanMs: 60_000,- });- const started = Date.now();+ test("a confirmed legacy answer (the server turned neither on) is a pin, unlike no answer", async () => {+ const { uploader, root } = await newChat(["legacy"]);+ uploader.startSession("ses-settle-legacy", "ws", root);+ expect(await uploader.settledPolicy("ses-settle-legacy")).toBe("legacy");+ await uploader.stop();+ });++ test("no answer: unresolved, so no pin (a strict bind refuses), nothing uploaded, no pin kept in the ledger", async () => {+ const stateDir = await mkdtemp(join(tmpdir(), "omnirush-policy-settle-state-"));+ roots.push(stateDir);+ const { uploader, uploads, root } = await newChat(["hang"], { stateDir });uploader.startSession("ses-settle-hang", "ws", root);- const pinned = await uploader.settledPolicy("ses-settle-hang");- expect(Date.now() - started).toBeLessThan(9_000);- expect(pinned).toBe(defaultRedactionPolicy());+ uploader.recordTrace("ses-settle-hang", "tool.call", { name: "x" });+ uploader.flushTrace("ses-settle-hang");+ expect(await uploader.settledPolicy("ses-settle-hang", 200)).toBeNull();+ await uploader.stop();+ expect(uploads).toEqual([]);+ const ledger = await readFile(join(stateDir, "omnirush-sessions.json"), "utf8").catch(() => "");+ expect(ledger).not.toMatch(/"ses-settle-hang"[^}]*redactionPolicy/);+ }, 20_000);++ test("an outage, then an answer: the chat waits, then pins the answer; nothing went up under a guess", async () => {+ const { uploader, uploads, calls, root } = await newChat([null, "client-v3"]);+ uploader.startSession("ses-settle-late", "ws", root);+ uploader.recordTrace("ses-settle-late", "tool.call", { name: "x" });+ uploader.flushTrace("ses-settle-late");+ expect(await uploader.settledPolicy("ses-settle-late", 10_000)).toBe("client-v3");+ expect(calls()).toBeGreaterThanOrEqual(2);await uploader.stop();- }, 15_000);+ expect(uploads.length).toBeGreaterThan(0);+ expect(new Set(envelopePolicies(uploads))).toEqual(new Set(["client-v3"]));+ }, 20_000);});diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.tsindex 81326d72..dcfa5f2d 100644--- a/apps/server/src/session-uploader.ts+++ b/apps/server/src/session-uploader.ts@@ -575,6 +575,8 @@ type SpoolMeta = {};type SessionState = {+ /** False for a new chat until the server's capture switch answered: its uploads wait, no pin is kept. */+ policyResolved: boolean;/** The upload rules pinned at the chat's start (pinnedPolicy): every redaction and envelope of the chat follows them. */redactionPolicy: RedactionPolicy;id: string;@@ -783,10 +785,11 @@ type SessionUploaderOptions = {*/onRecoveredTurn?: (turn: RecoveredTurn) => void;/**- * Reads the server's capture switch if it is not known yet. A new chat waits for it (at most- * POLICY_SETTLE_MS) before it pins its capture policy: never the pre-probe default.+ * The server's capture switch as answered (client-raw, client-v3 or a confirmed legacy), or null while it is+ * unresolved (no answer, or only an outage). A new chat pins only an answer: until there is one its policy is+ * unresolved, nothing of it is uploaded and no pin is kept (settledPolicy says null, so a strict bind waits).*/- settleCapturePolicy?: () => Promise<void>;+ settleCapturePolicy?: () => Promise<string | null>;/*** The server is missing parts of a chat no spooled upload will fill: the* host reads its transcript from the engine (oldest first, in trace-sized@@ -4661,6 +4664,9 @@ const PINNED_CONTEXT_METHODS = ["sessionStarted", "turnStarted", "toolEvent", "tconst PINNED_SESSION_METHODS = ["recordProjectArchive", "recordTrace", "markComputerUse", "recordSessionModel", "recordChildSession", "recordWebVisit", "recordAttachment", "steerTurn", "recordCommand", "captureSnapshot", "finishSession", "flushTrace"] as const;/** How long a new chat waits for the server's capture switch before it pins its capture policy. */+/** A new chat's capture switch, asked again after an unanswered try: from this, doubling, to POLICY_RETRY_MAX_MS. */+export const POLICY_RETRY_MIN_MS = 1_000;+export const POLICY_RETRY_MAX_MS = 30_000;export const POLICY_SETTLE_MS = 5_000;export class SessionUploader {@@ -5005,7 +5011,8 @@ export class SessionUploader {...(state.refused.length > 0 ? { refused: state.refused.slice(-MAX_REFUSED_SEQUENCES) } : {}),// A new segment is judged again once it ends....(state.endedAt ? { endedAt: state.endedAt } : {}),- redactionPolicy: state.redactionPolicy,+ // Never a pin from a guess: an unresolved chat's record has none, so the next start asks the server first.+ ...(state.policyResolved ? { redactionPolicy: state.redactionPolicy } : {}),};}@@ -5030,12 +5037,21 @@ export class SessionUploader {state.lastFailureAt = previous?.lastFailureAt;state.lastSuccessAt = previous?.lastSuccessAt;state.refused = [...(previous?.refused ?? [])];- // A new chat pins the server's capture switch as read now, not the default from before the first probe.+ // A new chat pins the server's capture switch as answered, never a default from before the first answer:+ // until there is one its uploads wait here (state.ready), and the switch is asked again with a backoff.if (!previous && this.settleCapturePolicy) {- let timer: ReturnType<typeof setTimeout> | undefined;- await Promise.race([this.settleCapturePolicy().catch(() => undefined), new Promise<void>((resolve) => { timer = setTimeout(resolve, POLICY_SETTLE_MS); })]);- clearTimeout(timer);- state.redactionPolicy = defaultRedactionPolicy();+ state.policyResolved = false;+ for (let wait = POLICY_RETRY_MIN_MS; ; wait = Math.min(wait * 2, POLICY_RETRY_MAX_MS)) {+ const answer = await this.askCapturePolicy();+ if (answer) {+ state.redactionPolicy = answer;+ state.policyResolved = true;+ break;+ }+ if (this.stopped) throw new Error("capture policy unresolved");+ await new Promise((resolve) => setTimeout(resolve, wait));+ if (this.stopped) throw new Error("capture policy unresolved");+ }}state.redactionPolicy = pinnedPolicy(previous, state.redactionPolicy);// A pending check of the chat's earlier segment is left to this segment's end.@@ -5321,6 +5337,7 @@ export class SessionUploader {private newSessionState(sessionId: string, workspaceId: string, root: string, cache: Map<string, HashCacheEntry>): SessionState {return {redactionPolicy: defaultRedactionPolicy(),+ policyResolved: true,id: sessionId,root,workspaceId,@@ -5874,7 +5891,20 @@ export class SessionUploader {const late = new Promise<false>((resolve) => { timer = setTimeout(() => resolve(false), timeoutMs); });const ready = await Promise.race([state.ready.then(() => true, () => true), late]);clearTimeout(timer);- return ready ? state.redactionPolicy : null;+ // Unresolved (no answer from the server yet): no policy, so no pin and no bind on a guess.+ return ready && state.policyResolved ? state.redactionPolicy : null;+ }++ /** One try at the server's capture switch (at most POLICY_SETTLE_MS): the answer, or null when unresolved. */+ private async askCapturePolicy(): Promise<RedactionPolicy | null> {+ if (!this.settleCapturePolicy) return null;+ let timer: ReturnType<typeof setTimeout> | undefined;+ const answer = await Promise.race([+ this.settleCapturePolicy().catch(() => null),+ new Promise<null>((resolve) => { timer = setTimeout(() => resolve(null), POLICY_SETTLE_MS); }),+ ]);+ clearTimeout(timer);+ return answer === CLIENT_RAW || answer === CLIENT_V3 || answer === "legacy" ? answer : null;}/** The capture policy the server stored for the chat (its binding): the session's uploads keep it. */@@ -6724,7 +6754,9 @@ export class SessionUploader {private enqueue(state: SessionState, operation: () => Promise<void>): void {// The pin is final once the ledger was read (prepareSession): the operation runs under it.- state.tail = state.tail.then(() => state.ready.catch(() => undefined)).then(() => withRedactionPolicy(state.redactionPolicy, operation)).catch((error: unknown) => {+ // A chat whose capture switch never got an answer (stopped while unresolved) uploads nothing under a guess:+ // its trace stays in the journal for the next start, which asks the switch first.+ state.tail = state.tail.then(() => state.ready.catch(() => undefined)).then(() => (state.policyResolved ? withRedactionPolicy(state.redactionPolicy, operation) : undefined)).catch((error: unknown) => {this.log("warn", "OmniRush session upload operation failed", {sessionId: state.id,error: error instanceof Error ? error.message : "unknown",