A desktop coding agent with free access to frontier models.
TypeScript
fix: chats hold their settings through server hiccups
ogsamrat3595b40b32c2
diff --git a/apps/server/src/box/box-required-service.test.ts b/apps/server/src/box/box-required-service.test.tsindex baa77b4d..423cc770 100644--- a/apps/server/src/box/box-required-service.test.ts+++ b/apps/server/src/box/box-required-service.test.ts@@ -8,7 +8,7 @@ import { planBox, rememberServerDefault, REQUIRED_REDETECT_MS, resolveMode, servimport type { RunCommand } from "./run.js";import { DESKTOP_PROJECTS_FILE } from "./plugin-lib.js";import { setupState } from "./setup.js";-import { BoxRequiredError, BoxService, type BoxWorkspace } from "./service.js";+import { BoxRequiredError, BoxService, PINNED_BOX_NOTE, type BoxWorkspace } from "./service.js";const dirs: string[] = [];afterEach(async () => {@@ -168,7 +168,7 @@ describe("required sandbox: the desktop's projects", () => {expect(JSON.parse(await readFile(join(home, DESKTOP_PROJECTS_FILE), "utf8")).native_chats).toEqual(["ses_kimi_new"]);});- test("offer level (backend #505): a chat pins where it started and its revision; a box chat is refused when its project's box goes off; a native chat stays native when it goes on", async () => {+ test("offer level (backend #505): a chat pins where it started and its revision; a box chat keeps its box when its project's box goes off; a native chat stays native when it goes on", async () => {const stateDir = await temp("state");const home = await temp("boxhome");const root = await temp("project");@@ -178,7 +178,8 @@ describe("required sandbox: the desktop's projects", () => {const settings: Settings = new Map();const boxes = service({ stateDir, workspaces: [workspace], settings, env: { OMNIRUSH_BOX_HOME: home } });const frozen: string[] = [];- boxes.ensure = async () => ((await boxes.status(workspace)).on ? fakeRun(root, frozen) : null);+ let boxCanRun = true;+ boxes.ensure = async (_workspace, options) => (boxCanRun && (options?.keep || (await boxes.status(workspace)).on) ? fakeRun(root, frozen) : null);// Offer: the project's box is off until the person turns it on.expect(await boxes.beforePrompt("ses_native1", workspace)).toBeNull();expect(boxes.chatPin("ses_native1")).toEqual({ box: false, revision: "lb1:5050a1b2c3d4" });@@ -189,10 +190,29 @@ describe("required sandbox: the desktop's projects", () => {await boxes.beforePrompt("ses_native1", workspace, { turnsBefore: 1 });expect(frozen).toEqual(["ses_box1 turn-1"]);expect(await boxes.planOutcome("ses_native1", workspace)).toMatchObject({ native: true, reason: "kept_old_chat" });- // Turned off since: the box chat is refused, never moved here.+ // Turned off since: the box chat keeps its box (one line says new chats follow the setting), never moved here.settings.set("ws_pin", { mode: "off", offered: true });- expect(await boxes.pinnedBoxGone("ses_box1", workspace)).toBe(true);- expect(await boxes.pinnedBoxGone("ses_native1", workspace)).toBe(false);+ expect(await boxes.pinnedBoxUnavailable("ses_box1", workspace)).toBeNull();+ expect(await boxes.pinnedBoxUnavailable("ses_native1", workspace)).toBeNull();+ await boxes.beforePrompt("ses_box1", workspace, { turnsBefore: 1 });+ expect(frozen).toEqual(["ses_box1 turn-1", "ses_box1 turn-2"]);+ expect((await boxes.status(workspace, { sessionId: "ses_box1" })).notice).toBe(PINNED_BOX_NOTE);+ expect((await boxes.status(workspace, { sessionId: "ses_native1" })).notice).not.toBe(PINNED_BOX_NOTE);+ // Refused only when the box truly cannot run, in plain words.+ boxCanRun = false;+ expect(await boxes.pinnedBoxUnavailable("ses_box1", workspace)).toBe("the sandbox cannot start on this computer right now");+ boxCanRun = true;+ // A chat is pinned at its first prompt whether or not it is captured, and keeps its capture policy.+ await boxes.beforePrompt("ses_nocapture", workspace, { capture: false });+ expect(boxes.chatPin("ses_nocapture")).toEqual({ box: false, revision: "lb1:5050a1b2c3d4" });+ await boxes.setCapturePolicy("ses_nocapture", "client-raw");+ expect(boxes.chatPin("ses_nocapture")?.capturePolicy).toBe("client-raw");+ // Pins are never evicted.+ const pins: unknown = Reflect.get(boxes, "pins");+ if (!(pins instanceof Map)) throw new Error("no pins map");+ for (let i = 0; i < 4001; i += 1) pins.set(`ses_fill_${i}`, { box: false, revision: null });+ await boxes.beforePrompt("ses_after_fill", workspace);+ expect(boxes.chatPin("ses_box1")?.box).toBe(true);// The status names the server's starts, the revision and the chat's pin.const status = await boxes.status(workspace, { sessionId: "ses_box1" });expect(status.modelStart?.["kimi-k3.1-pro"]).toEqual({ start: "native", switchable: false });@@ -201,6 +221,7 @@ describe("required sandbox: the desktop's projects", () => {// A new service (app restart) reads the pins back.const again = service({ stateDir, workspaces: [workspace], settings, env: { OMNIRUSH_BOX_HOME: home } });expect(again.chatPin("ses_box1")?.box).toBe(true);+ expect(again.chatPin("ses_nocapture")?.capturePolicy).toBe("client-raw");});test("the first prompt waits while the image downloads, then runs in the box", async () => {diff --git a/apps/server/src/box/service.ts b/apps/server/src/box/service.tsindex 7ad3cfde..ef1772ab 100644--- a/apps/server/src/box/service.ts+++ b/apps/server/src/box/service.ts@@ -44,6 +44,12 @@ import { layerSizes, pullProgress, type PullProgress } from "./pull-progress.js"import { detectRuntime, type BackendAlternative, type DetectOptions, type RuntimeInfo, type RuntimeResult } from "./runtime.js";/** What the capture side needs of a chat's box (plain data: it crosses to the capture worker). */+/** A box chat after its project's box was turned off. */+export const PINNED_BOX_NOTE = "This chat keeps running in the sandbox. New chats follow the new setting.";++/** A chat's pin from its first prompt: where it runs, the local_box revision, the capture policy its uploads keep. */+export type ChatPin = { box: boolean; revision: string | null; capturePolicy?: string; verified?: boolean; lateReason?: string };+export type BoxCaptureInfo = {key: string;root: string;@@ -68,7 +74,7 @@ export type BoxStatus = {/** The local_box policy revision new chats pin. */revision: string | null;/** This chat's pin (asked for one chat): where it runs and the revision it started under; null before its first prompt. */- chatPin: { box: boolean; revision: string | null } | null;+ chatPin: ChatPin | null;/** The project's own choice (null: the server's default). */mode: ProjectBoxMode;serverDefault: ServerBoxDefault;@@ -234,7 +240,7 @@ export class BoxService {* never moves: a box chat whose project's box is turned off is refused* (pinnedBoxGone), a chat that ran here stays here. Kept in chat-pins.json.*/- private pins: Map<string, { box: boolean; revision: string | null }> | null = null;+ private pins: Map<string, ChatPin> | null = null;/** Chats whose box.plan event went out in this run. */private readonly outcomeSent = new Set<string>();private readonly home: string;@@ -406,6 +412,8 @@ export class BoxService {*/async engineBits(): Promise<{ shell: string; plugin: string } | null> {let wanted = this.serverDefault() !== "off" || this.required().required || ["1", "on", "true"].includes(String(this.env.OMNIRUSH_BOX ?? "").trim().toLowerCase());+ // A chat that started in the box keeps it after its project's box was turned off.+ wanted ||= [...this.loadPins().values()].some((pin) => pin.box);for (const workspace of this.options.workspaces()) {if (wanted) break;if (workspace.workspaceType !== "local") continue;@@ -424,7 +432,7 @@ export class BoxService {* The project's run when its box is on and can start (planned once, in* the background; a downloading image is looked at again later), else null.*/- ensure(workspace: BoxWorkspace): Promise<BoxRun | null> {+ ensure(workspace: BoxWorkspace, { keep = false }: { keep?: boolean } = {}): Promise<BoxRun | null> {if (workspace.workspaceType !== "local") return Promise.resolve(null);const root = boxRoot(workspace);const key = boxKey(root);@@ -433,7 +441,8 @@ export class BoxService {if (project.planning) return project.planning;project.planning = (async () => {const { on, hostBuilds: gate } = await this.mode(workspace);- if (!on) {+ // `keep`: a chat that started in the box stays there after its project's box was turned off.+ if (!on && !keep) {if (gate?.native && project.notice !== gate.notice) this.log("info", "box: the server's default not applied, the folder has this computer's builds", { key, things: gate.things });this.dropRun(project);project.notice = gate?.native ? gate.notice : null;@@ -667,6 +676,7 @@ export class BoxService {container,platform: this.platform,notice: on ? project?.notice ?? (runtime.ok ? null : `Sandbox off for now: ${runtime.reason}.`)+ : chatPin?.box ? PINNED_BOX_NOTE: gate?.native ? gate.notice: hostOnly ? hostOnly.notice: lessRepro ? LESS_REPRO_NOTICE@@ -825,9 +835,11 @@ export class BoxService {await this.writeProjects();this.log("info", "box: a chat that ran on this machine stays there", { sessionId, turnsBefore, required: true });}- const run = required && !this.nativeChats.has(sessionId) ? await this.waitForBox(workspace) : await this.ensure(workspace).catch(() => null);+ // One pin per chat: a chat that started in the box keeps it, whatever the project's setting says now.+ const keep = this.chatPin(sessionId)?.box === true;+ const run = required && !this.nativeChats.has(sessionId) ? await this.waitForBox(workspace) : await this.ensure(workspace, { keep }).catch(() => null);if (!run || run.native) {- if (capture) await this.pin(sessionId, false);+ await this.pin(sessionId, false);return null;}// A chat with turns but no checkpoint ran on this machine (reopened, or started in this run before the@@ -840,7 +852,7 @@ export class BoxService {this.log("info", "box: a chat that ran on this machine stays there", { sessionId, turnsBefore });}if (this.nativeChats.has(sessionId) || !capture) {- if (capture) await this.pin(sessionId, false);+ await this.pin(sessionId, false);return null;}this.boxedChats.add(sessionId);@@ -905,7 +917,7 @@ export class BoxService {return join(this.options.stateDir, "chat-pins.json");}- private loadPins(): Map<string, { box: boolean; revision: string | null }> {+ private loadPins(): Map<string, ChatPin> {if (this.pins) return this.pins;this.pins = new Map();try {@@ -913,8 +925,14 @@ export class BoxService {if (value && typeof value === "object") {for (const [id, pin] of Object.entries(value)) {if (pin && typeof pin === "object" && typeof (pin as { box?: unknown }).box === "boolean") {- const revision = (pin as { revision?: unknown }).revision;- this.pins.set(id, { box: (pin as { box: boolean }).box, revision: typeof revision === "string" ? revision : null });+ const { revision, capturePolicy, verified, lateReason } = pin as { revision?: unknown; capturePolicy?: unknown; verified?: unknown; lateReason?: unknown };+ this.pins.set(id, {+ box: (pin as { box: boolean }).box,+ revision: typeof revision === "string" ? revision : null,+ ...(typeof capturePolicy === "string" && capturePolicy ? { capturePolicy } : {}),+ ...(typeof verified === "boolean" ? { verified } : {}),+ ...(typeof lateReason === "string" && lateReason ? { lateReason } : {}),+ });}}}@@ -922,25 +940,44 @@ export class BoxService {return this.pins;}- /** A chat's pin ({ box, revision }), or null before its first prompt. */- chatPin(sessionId: string): { box: boolean; revision: string | null } | null {+ /** A chat's pin ({ box, revision, capturePolicy? }), or null before its first prompt. Never evicted. */+ chatPin(sessionId: string): ChatPin | null {return this.loadPins().get(sessionId) ?? null;}+ /** The capture policy a chat's uploads keep: set once at its first prompt, or to the one the server's binding stored. */+ async setCapturePolicy(sessionId: string, policy: string): Promise<void> {+ await this.updatePin(sessionId, { capturePolicy: policy });+ }++ /** What the server's binding says of a chat: whether it is verified, and why it was bound late (never verified then). */+ async updatePin(sessionId: string, patch: Partial<Pick<ChatPin, "capturePolicy" | "verified" | "lateReason">>): Promise<void> {+ const pins = this.loadPins();+ const pin = pins.get(sessionId);+ if (!pin || Object.entries(patch).every(([key, value]) => pin[key as keyof ChatPin] === value)) return;+ pins.set(sessionId, { ...pin, ...patch });+ await writeFileAtomic(this.pinFile(), `${JSON.stringify(Object.fromEntries(pins))}\n`).catch(() => undefined);+ }+private async pin(sessionId: string, box: boolean): Promise<void> {const pins = this.loadPins();if (pins.has(sessionId)) return;const revision = serverLocalBox(this.options.stateDir)?.revision;pins.set(sessionId, { box, revision: typeof revision === "string" ? revision : null });- while (pins.size > 4000) pins.delete(pins.keys().next().value!);await writeFileAtomic(this.pinFile(), `${JSON.stringify(Object.fromEntries(pins))}\n`).catch(() => undefined);}- /** Whether a chat pinned to the box would run on this computer now (its project's box turned off): its prompt is refused. */- async pinnedBoxGone(sessionId: string, workspace: BoxWorkspace): Promise<boolean> {- if (workspace.workspaceType !== "local" || !this.chatPin(sessionId)?.box) return false;- const mode = await this.mode(workspace).catch(() => null);- return Boolean(mode && !mode.on);+ /**+ * Why a chat pinned to the box cannot run in it now (no backend, an image+ * that cannot be had), or null: it keeps the box even when its project's+ * box was turned off since, and is refused only when the box truly cannot run.+ */+ async pinnedBoxUnavailable(sessionId: string, workspace: BoxWorkspace): Promise<string | null> {+ if (workspace.workspaceType !== "local" || !this.chatPin(sessionId)?.box) return null;+ const run = await this.ensure(workspace, { keep: true }).catch(() => null);+ if (run && !run.native) return null;+ const project = this.projects.get(boxKey(boxRoot(workspace)));+ return project?.waitingImage ? null : project?.notice ?? "the sandbox cannot start on this computer right now";}async planOutcome(sessionId: string, workspace: BoxWorkspace): Promise<BoxPlanOutcome | null> {diff --git a/apps/server/src/capture-client.ts b/apps/server/src/capture-client.tsindex da37b52d..439d3fff 100644--- a/apps/server/src/capture-client.ts+++ b/apps/server/src/capture-client.ts@@ -85,6 +85,10 @@ export type CaptureService = {startSession(sessionId: string, workspaceId: string, root: string, turnsBefore?: number): void;/** The chat's prompts run in its project's Linux sandbox (box/service.ts), or no longer (null); before startSession. */boxSession(sessionId: string, box: BoxCaptureInfo | null): void;+ /** A tracked chat's capture policy once settled; null when it is not tracked or not settled. */+ sessionPolicy(sessionId: string): Promise<string | null>;+ /** The capture policy the server's binding keeps for the chat: its uploads follow it. */+ pinPolicy(sessionId: string, policy: string): void;/** The app's permission mode, named in the setup record of the sessions started after it; before startSession. */permissionMode(mode: ApprovalMode | null): void;recordTrace(sessionId: string, type: string, data?: unknown): void;@@ -213,6 +217,15 @@ class CaptureClient implements CaptureService {this.send({ kind: "call", id: null, method: "startSession", args: [sessionId, workspaceId, root, turnsBefore] });}+ async sessionPolicy(sessionId: string): Promise<string | null> {+ const value = await this.query({ kind: "call", id: null, method: "sessionPolicy", args: [sessionId] }).catch(() => null);+ return typeof value === "string" ? value : null;+ }++ pinPolicy(sessionId: string, policy: string): void {+ this.send({ kind: "call", id: null, method: "pinPolicy", args: [sessionId, policy] });+ }+boxSession(sessionId: string, box: BoxCaptureInfo | null): void {this.send({ kind: "call", id: null, method: "boxSession", args: [sessionId, box] });}diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.tsindex 2274ddea..00384a08 100644--- a/apps/server/src/capture-host.ts+++ b/apps/server/src/capture-host.ts@@ -11,6 +11,7 @@import { createMediaRewriter } from "./computer-use/media-capture.js";import type { ApprovalMode } from "./approval-mode.js";import type { ContextOptions } from "./context/index.js";+import { defaultRedactionPolicy } from "./context/redact-policy.js";import { readPromptAttachments, promptBodyForTrace, v2PromptBodyForTrace } from "./session-upload-attachments.js";import {readSessionTranscript,@@ -238,6 +239,17 @@ export class CaptureHost {this.sessionUploader.setPermissionMode(mode);}+ /** A tracked chat's capture policy once settled (null: not tracked, or not settled yet). */+ async sessionPolicy(sessionId: string): Promise<string | null> {+ // An untracked chat (its uploads off): the policy a session starting now gets.+ return this.sessionUploader.hasSession(sessionId) ? await this.sessionUploader.settledPolicy(sessionId) : defaultRedactionPolicy();+ }++ /** The capture policy the server's binding keeps for the chat. */+ pinPolicy(sessionId: string, policy: string): void {+ this.sessionUploader.pinPolicy(sessionId, policy);+ }+/** The chat's prompts run in its project's box (or no longer: null); sent before its startSession. */boxSession(sessionId: string, box: BoxCaptureInfo | null): void {this.sessionUploader.setBox(sessionId, box);diff --git a/apps/server/src/capture-protocol.ts b/apps/server/src/capture-protocol.tsindex 63cf4069..3f55e302 100644--- a/apps/server/src/capture-protocol.ts+++ b/apps/server/src/capture-protocol.ts@@ -21,6 +21,8 @@ export type CaptureStopOptions = { archiveFinals: boolean };export type CaptureCalls = {startSession: [sessionId: string, workspaceId: string, root: string, turnsBefore?: number];boxSession: [sessionId: string, box: BoxCaptureInfo | null];+ sessionPolicy: [sessionId: string];+ pinPolicy: [sessionId: string, policy: string];permissionMode: [mode: ApprovalMode | null];recordTrace: [sessionId: string, type: string, data?: unknown];flushTrace: [sessionId: string, finalTrace?: unknown];@@ -175,6 +177,10 @@ export function invokeCapture(host: CaptureHost, call: CaptureCall): unknown {return host.startSession(...call.args);case "boxSession":return host.boxSession(...call.args);+ case "sessionPolicy":+ return host.sessionPolicy(...call.args);+ case "pinPolicy":+ return host.pinPolicy(...call.args);case "permissionMode":return host.permissionMode(...call.args);case "recordTrace":diff --git a/apps/server/src/model-modes/PARITY.sha256 b/apps/server/src/model-modes/PARITY.sha256index 2366b79f..859b8874 100644--- a/apps/server/src/model-modes/PARITY.sha256+++ b/apps/server/src/model-modes/PARITY.sha256@@ -2,5 +2,5 @@# A change to one must be ported to the other and this list updated in both.609bbc9cb508b4f79295e0b2f1f4707bd1facbee83835d5db7eac02fa088f43b model-modes.mjsef8a96d1e71ea3f5f3ab7a96796d042c009d90cc2896e7132b8dfc4486540699 model-modes.d.mts-f2259a4313af33359362e5bc603a1b197d2736fc0844aee73dbc696058304d07 strict-sessions.mjs-9795dfe99dd6d8e89053232ef54b50877f4724ace6b9d946aa4c8e48b11c6495 strict-sessions.d.mts+7aa8c758c3d9a148911bda110d90f4542a67be9d996718228feb788680100f67 strict-sessions.mjs+81049cae72b0dfe53f769835acd649b50aff1737c8c3e40f1647328a09f9ef05 strict-sessions.d.mtsdiff --git a/apps/server/src/model-modes/model-modes.test.ts b/apps/server/src/model-modes/model-modes.test.tsindex 6d9cd691..2e9dd0af 100644--- a/apps/server/src/model-modes/model-modes.test.ts+++ b/apps/server/src/model-modes/model-modes.test.ts@@ -168,7 +168,7 @@ describe("model modes", () => {expect(calls.map((c) => c.model)).toEqual(["kimi-k3.1-pro", "kimi-k3.1-pro", "kimi-k3.1-pro"]);});- test("broker: strict sessions bind once before a chat's first agent request; conflicts and verification stop here; failures never block", async () => {+ test("broker: strict sessions (observe) bind once before a chat's first agent request; conflicts and verification stop here; failures never block", async () => {type Bind = Record<string, unknown>;const binds: Bind[] = [];const sent: { session: string | null; strict: string | null }[] = [];@@ -186,7 +186,7 @@ describe("model modes", () => {fetch: async (input, init) => {const url = new URL(String(input));const headers = new Headers(init?.headers);- if (url.pathname === "/omnirush/collect/capabilities") return Response.json({ schema_versions: [1, 2, 3], ...(strictOn ? { strict_sessions: { mode: "enforce", bind: true, path: "/omnirush/device/session-binding", header: "X-OmniRush-Strict-Session", implicit: true } } : {}) });+ if (url.pathname === "/omnirush/collect/capabilities") return Response.json({ schema_versions: [1, 2, 3], ...(strictOn ? { strict_sessions: { mode: "observe", bind: true, path: "/omnirush/device/session-binding", header: "X-OmniRush-Strict-Session", implicit: true } } : {}) });if (url.pathname === "/omnirush/device/session-binding") {const body = JSON.parse(String(init?.body)) as Bind & { session_id: string; parent_session_id?: string; model: string; mode: string };binds.push(body);@@ -222,7 +222,7 @@ describe("model modes", () => {expect((await post("ses_root")).status).toBe(200);expect((await post("ses_root", {}, { model: "gpt-6-astra" })).status).toBe(200);expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_root" }, "responses/compact"), "responses/compact")).status).toBe(200);- expect(binds).toEqual([{ session_id: "ses_root", model: "gpt-6-astra", mode: "native", capture_policy: "legacy" }]);+ expect(binds).toEqual([{ session_id: "ses_root", model: "gpt-6-astra", mode: "native" }]);expect(sent.slice(1).map((s) => s.strict)).toEqual(["1", "1", "1", "1"]);// A sub-agent names its root.expect((await post("ses_child", { "x-parent-session-id": "ses_root" })).status).toBe(200);@@ -262,4 +262,119 @@ describe("model modes", () => {expect(gwUsed.headers.get("x-should-retry")).toBe("false");expect(((await gwUsed.json()) as { error: { message: string } }).error.message).toBe("This chat already ran on another model. Start a new chat to use this one.");});++ test("broker: strict sessions under enforce fail closed: a capabilities outage, a bind timeout or a 5xx sends nothing upstream", async () => {+ const dir = mkdtempSync(path.join(tmpdir(), "strict-enforce-"));+ const strictStateFile = path.join(dir, "strict-sessions.json");+ const upstream: string[] = [];+ const binds: Record<string, unknown>[] = [];+ let caps: "enforce" | "down" = "enforce";+ let bindAnswer: "timeout" | "5xx" | "ok" = "timeout";+ const pins = new Map<string, { box: boolean; revision: string | null; capturePolicy?: string }>();+ const stored: [string, string][] = [];+ const noted: [string, { verified: boolean; lateReason?: string }][] = [];+ const make = () => new OmniRushGatewayBroker({+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },+ engineToken: "local-engine-token",+ strictStateFile,+ bindTimeoutMs: 100,+ chatPin: (id) => pins.get(id) ?? null,+ onCapturePolicy: (id, policy) => stored.push([id, policy]),+ onBinding: (id, binding) => noted.push([id, binding]),+ fetch: async (input, init) => {+ const url = new URL(String(input));+ if (url.pathname === "/omnirush/collect/capabilities") {+ if (caps === "down") throw new TypeError("fetch failed");+ return Response.json({ schema_versions: [1, 2, 3], strict_sessions: { mode: "enforce", bind: true, path: "/omnirush/device/session-binding", header: "X-OmniRush-Strict-Session" } });+ }+ if (url.pathname === "/omnirush/device/session-binding") {+ binds.push(JSON.parse(String(init?.body)) as Record<string, unknown>);+ if (bindAnswer === "timeout") return new Promise<Response>((_resolve, reject) => init?.signal?.addEventListener("abort", () => reject(new DOMException("timed out", "TimeoutError"))));+ if (bindAnswer === "5xx") return Response.json({ detail: "unavailable" }, { status: 503 });+ return Response.json({ binding: { model: "gpt-6-astra", mode: "native", capture_policy: "client-raw", bound_late: true, late_reason: "history_incomplete", verified: false }, enforcement: "enforce", kept: ["capture_policy"] });+ }+ upstream.push(new Headers(init?.headers).get("x-omnirush-strict-session") ?? "none");+ return Response.json({ output: [] });+ },+ });+ const tools = [{ type: "function", name: "bash" }];+ // The first run learns enforce and keeps it on disk.+ await make().sessionUploadCapabilities();+ // The next start: capabilities cannot be fetched; the kept enforce stands.+ caps = "down";+ const b = make();+ await b.sessionUploadCapabilities();+ const post = (session: string) => b.handle(request({ model: "gpt-6-astra", tools }, { "x-omnirush-session-id": session }), "responses");+ pins.set("ses_timeout", { box: false, revision: null, capturePolicy: "legacy" });+ pins.set("ses_5xx", { box: false, revision: null, capturePolicy: "legacy" });+ const timedOut = await post("ses_timeout");+ // 503: the engine sends it again by itself.+ expect(timedOut.status).toBe(503);+ expect(((await timedOut.json()) as { error: { code: string; message: string } }).error).toMatchObject({ code: "session_bind_unconfirmed", message: "Couldn't confirm this chat with the server. Try again." });+ bindAnswer = "5xx";+ expect((await post("ses_5xx")).status).toBe(503);+ // No capture policy pinned yet: not bound, not sent.+ const before = binds.length;+ expect((await post("ses_unpinned")).status).toBe(503);+ expect(binds.length).toBe(before);+ expect(upstream).toEqual([]);+ // The bind comes through: sent with the chat's capture policy, the server's stored one goes to the pin.+ bindAnswer = "ok";+ expect((await post("ses_timeout")).status).toBe(200);+ expect(binds.at(-1)).toMatchObject({ session_id: "ses_timeout", capture_policy: "legacy" });+ expect(stored).toEqual([["ses_timeout", "client-raw"]]);+ // A late binding is kept with its reason, never verified.+ expect(noted).toEqual([["ses_timeout", { verified: false, lateReason: "history_incomplete" }]]);+ expect(upstream).toEqual(["1"]);+ rmSync(dir, { recursive: true, force: true });+ });++ test("broker: a box chat's unmarked requests (title, compaction, after a restart) stay box requests by its durable pin", async () => {+ const calls: Call[] = [];+ const b = new OmniRushGatewayBroker({+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },+ engineToken: "local-engine-token",+ chatPin: (id) => (id === "ses_boxed" ? { box: true, revision: null } : null),+ fetch: async (input, init) => {+ const raw = init?.body;+ const text = typeof raw === "string" ? raw : new TextDecoder().decode(raw as ArrayBuffer);+ calls.push({ model: JSON.parse(text).model, box: new Headers(init?.headers).get("x-omnirush-box"), path: new URL(String(input)).pathname });+ return Response.json({ output: [] });+ },+ });+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_boxed" }, "responses/compact"), "responses/compact")).status).toBe(200);+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_plain" }), "responses")).status).toBe(200);+ expect(calls.map((c) => c.box)).toEqual(["1", null]);+ });++ test("broker: strict_session_unavailable is sent again after Retry-After, bounded; the engine sees one answer", async () => {+ let refuse = 1;+ let hits = 0;+ let inferences = 0;+ const b = new OmniRushGatewayBroker({+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },+ engineToken: "local-engine-token",+ fetch: async () => {+ hits += 1;+ if (refuse > 0) {+ refuse -= 1;+ return Response.json({ detail: "strict_session_unavailable", error: { type: "server_error", code: "strict_session_unavailable", message: "Try again.", omnirush: { code: "strict_session_unavailable", retry: true } } }, { status: 503, headers: { "x-should-retry": "true", "retry-after": hits === 1 ? "1" : "0" } });+ }+ inferences += 1;+ return Response.json({ output: [] });+ },+ });+ const post = () => b.handle(request({ model: "gpt-6-astra", tools: [{ type: "function", name: "bash" }] }, { "x-omnirush-session-id": "ses_retry" }), "responses");+ const started = Date.now();+ expect((await post()).status).toBe(200);+ expect(Date.now() - started).toBeGreaterThanOrEqual(1000);+ expect([hits, inferences]).toEqual([2, 1]);+ // Still unavailable after the retries: retryable for the engine, never a final refusal.+ refuse = 10;+ hits = 0;+ const still = await post();+ expect(still.status).toBe(503);+ expect(still.headers.get("x-should-retry")).not.toBe("false");+ expect(hits).toBe(4);+ });});diff --git a/apps/server/src/model-modes/strict-sessions.d.mts b/apps/server/src/model-modes/strict-sessions.d.mtsindex 0a2c1903..6fdc6bac 100644--- a/apps/server/src/model-modes/strict-sessions.d.mts+++ b/apps/server/src/model-modes/strict-sessions.d.mts@@ -13,13 +13,16 @@ export const MODEL_NOT_BOUND: "model_not_bound";export const MODE_NOT_BOUND: "mode_not_bound";export const BIND_TIMEOUT_MS: number;export const VERIFICATION_MESSAGE: string;+export const SESSION_BIND_UNCONFIRMED: "session_bind_unconfirmed";+export const UNCONFIRMED_MESSAGE: string;-export type ChatPin = { model: string; mode: ModelMode; revision?: string | null };-export type BindChat = ChatPin & { path: string | null; sessionId: string; rootId?: string | null; capturePolicy?: string | null; root?: ChatPin | null };-export type BindOutcome = { ok: true } | { refused: { code: string; message: string } };+export type ChatPin = { model: string; mode: ModelMode; revision?: string | null; capturePolicy?: string | null };+export type BindChat = ChatPin & { path: string | null; enforce?: boolean; sessionId: string; rootId?: string | null; root?: ChatPin | null };+export type BindOutcome = { ok: true; binding?: Record<string, unknown> } | { refused: { code: string; message: string; retry: boolean } };+export type StrictSessions = { mode: "observe" | "enforce"; path: string; header: string };export type BindPost = (path: string, body: Record<string, unknown>, signal: AbortSignal) => Promise<{ status: number; body: unknown }>;-export function strictSessions(capabilities: unknown): { path: string; header: string } | null;+export function strictSessions(capabilities: unknown): StrictSessions | null;export function bindingConflictMessage(binding: unknown, model: string): string;export function modelUsedMessage(modelsUsed: unknown, model: string): string;export function sessionBinder(post: BindPost, options?: { timeoutMs?: number }): (chat: BindChat) => Promise<BindOutcome>;diff --git a/apps/server/src/model-modes/strict-sessions.mjs b/apps/server/src/model-modes/strict-sessions.mjsindex 05da8bcc..40ff264f 100644--- a/apps/server/src/model-modes/strict-sessions.mjs+++ b/apps/server/src/model-modes/strict-sessions.mjs@@ -1,18 +1,21 @@// Strict sessions: each chat binds its model and project mode with the server.//// GET /collect/capabilities `strict_sessions: {mode, bind, path, header}`: while-// `bind` is true, a chat's first agent request (where the client pins its-// model) is preceded by POST <path> {session_id, parent_session_id?, model,-// mode, revision?, capture_policy?}. A sub-agent names its root chat; a-// root the server does not know yet is bound first and the child sent once-// more. A conflict (the chat is bound to another model or mode, or already-// ran on another model unbound) or a verification refusal stops the request-// here; anything else (a Claude-// chat, a refused model, a network failure, a 5xx, a timeout) never holds-// the chat up: the request goes on unbound and a failed bind is tried again-// on the next request. While binding is on, every model request of a chat-// (compactions, titles and sub-agents too) carries the server's `header`-// (X-OmniRush-Strict-Session): 1, so the gateway can bind it on the spot when the explicit bind failed.+// `bind` is true, a chat's first request (where the client pins its model)+// is preceded by POST <path> {session_id, parent_session_id?, model, mode,+// revision?, capture_policy?}. A sub-agent names its root chat; a root the+// server does not know yet is bound first and the child sent once more. A+// conflict (the chat is bound to another model or mode, or already ran on+// another model unbound) or a verification refusal stops the request here.+// A Claude chat or a refused model goes on unbound. A bind that got no answer+// (a network failure, a 5xx, a timeout) depends on the switch: under+// `observe` the request goes on unbound and the bind is tried again on the+// next request; under `enforce` (or the last known `enforce` while the server+// cannot be asked) the request gets a retryable local error and nothing is+// sent, and so does a first prompt whose capture policy is not known yet.+// While binding is on, every model request of a chat (compactions, titles and+// sub-agents too) carries the server's `header` (X-OmniRush-Strict-Session):+// 1, so the gateway can bind it on the spot when the explicit bind failed.// No `strict_sessions` (an older server): no bind, no header.//// The same file ships in omnirush-cli (src/model-modes/) and omnirush-gui@@ -28,19 +31,25 @@ export const MODEL_NOT_BOUND = "model_not_bound";export const MODE_NOT_BOUND = "mode_not_bound";export const BIND_TIMEOUT_MS = 5_000;export const VERIFICATION_MESSAGE = "Your account needs to be verified before it can use models. See https://omnirush.ai/console";+export const SESSION_BIND_UNCONFIRMED = "session_bind_unconfirmed";+export const UNCONFIRMED_MESSAGE = "Couldn't confirm this chat with the server. Try again.";+const MODES = new Set(["off", "observe", "enforce"]);const PATH = /^\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/;const HEADER = /^x-[a-z0-9-]{1,62}$/;/*** A capabilities answer's strict sessions while the server asks clients to- * bind: { path, header } (header lower case), else null.+ * bind: { mode, path, header } (header lower case; mode "observe" or+ * "enforce", an unknown one read as "enforce"), else null. Clients keep the+ * last one per server on disk: it stands while the server cannot be asked.*/export function strictSessions(capabilities) {const block = capabilities && typeof capabilities === "object" ? capabilities.strict_sessions : null;if (!block || typeof block !== "object" || block.bind !== true) return null;const header = typeof block.header === "string" ? block.header.trim().toLowerCase() : "";return {+ mode: MODES.has(block.mode) && block.mode !== "off" ? block.mode : "enforce",path: typeof block.path === "string" && PATH.test(block.path) ? block.path : STRICT_BIND_PATH,header: HEADER.test(header) ? header : STRICT_SESSION_HEADER,};@@ -69,12 +78,14 @@ function detailOf(answer) {/*** The binder of one client process. `post(path, body, signal)` sends the* bind (the client's bearer and origin) and resolves { status, body } (body- * parsed JSON or null); it may throw. bind(chat) resolves { ok: true } or- * { refused: { code, message } }, where chat is { path, sessionId, rootId?,- * model, mode, revision?, capturePolicy?, root?: { model, mode, revision? } }- * (root: the root chat's own pin, for binding it first). A session bound (or- * one the server will not bind: a Claude chat, a refused model) is not asked- * again in this process.+ * parsed JSON or null); it may throw. bind(chat) resolves { ok: true,+ * binding? } (binding: the server's stored binding of a 200, whose+ * capture_policy the chat then keeps) or { refused: { code, message, retry } },+ * where chat is { path, enforce?, sessionId, rootId?, model, mode, revision?,+ * capturePolicy?, root?: { model, mode, revision?, capturePolicy? } } (root:+ * the root chat's own pin, for binding it first). A session bound (or one the+ * server will not bind: a Claude chat, a refused model) is not asked again in+ * this process; the cache is keyed on the client's own id, never the echo.*/export function sessionBinder(post, { timeoutMs = BIND_TIMEOUT_MS } = {}) {const settled = new Set();@@ -95,11 +106,14 @@ export function sessionBinder(post, { timeoutMs = BIND_TIMEOUT_MS } = {}) {});return async function bind(chat) {if (!chat || !chat.path || !chat.sessionId || !chat.model || settled.has(chat.sessionId)) return { ok: true };+ const unconfirmed = { refused: { code: SESSION_BIND_UNCONFIRMED, message: UNCONFIRMED_MESSAGE, retry: true } };+ if (chat.enforce && !chat.capturePolicy) return unconfirmed;const parent = chat.rootId && chat.rootId !== chat.sessionId ? chat.rootId : null;const body = bodyOf(chat.sessionId, parent, chat, chat.capturePolicy);let answer = await send(chat.path, body);if (parent && answer && answer.status === 409 && detailOf(answer) === SESSION_UNKNOWN_PARENT) {- const root = await send(chat.path, bodyOf(parent, null, chat.root && chat.root.model ? chat.root : chat, chat.capturePolicy));+ const rootPin = chat.root && chat.root.model ? chat.root : chat;+ const root = await send(chat.path, bodyOf(parent, null, rootPin, rootPin.capturePolicy || chat.capturePolicy));if (root && root.status === 200) {settle(parent);answer = await send(chat.path, body);@@ -108,16 +122,21 @@ export function sessionBinder(post, { timeoutMs = BIND_TIMEOUT_MS } = {}) {const status = answer ? answer.status : 0;const detail = detailOf(answer);if (detail === SESSION_BINDING_CONFLICT) {- return { refused: { code: SESSION_BINDING_CONFLICT, message: bindingConflictMessage(answer.body.binding, chat.model) } };+ return { refused: { code: SESSION_BINDING_CONFLICT, message: bindingConflictMessage(answer.body.binding, chat.model), retry: false } };}if (detail === SESSION_MODEL_USED) {- return { refused: { code: SESSION_MODEL_USED, message: modelUsedMessage(answer.body.models_used, chat.model) } };+ return { refused: { code: SESSION_MODEL_USED, message: modelUsedMessage(answer.body.models_used, chat.model), retry: false } };}if (detail === VERIFICATION_REQUIRED) {- return { refused: { code: VERIFICATION_REQUIRED, message: VERIFICATION_MESSAGE } };+ return { refused: { code: VERIFICATION_REQUIRED, message: VERIFICATION_MESSAGE, retry: false } };}// Bound (bound_late too), or a chat the server will not bind (a Claude chat, a refused model): settled.- if (status === 200 || status === 400 || status === 422) settle(chat.sessionId);- return { ok: true };+ if (status === 200 || status === 400 || status === 422) {+ settle(chat.sessionId);+ const binding = status === 200 && answer.body && typeof answer.body.binding === "object" && answer.body.binding ? answer.body.binding : null;+ return binding ? { ok: true, binding } : { ok: true };+ }+ // No answer (network, 5xx, timeout, another status): enforce holds the request, observe lets it go.+ return chat.enforce ? unconfirmed : { ok: true };};}diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.tsindex d4ba3660..bc0fd020 100644--- a/apps/server/src/omnirush-gateway-broker.ts+++ b/apps/server/src/omnirush-gateway-broker.ts@@ -16,11 +16,13 @@ import { builtinOmniRushModelCatalog, omnirushModelWantsReasoningSummary } fromimport { RejectedAttachments, isRejectedAttachmentError, type Attachment } from "./rejected-attachments.js";import { gatewayUrlAllowed, RELEASE_GATEWAY_ERROR } from "./release-guard.js";import { BOX_HEADER, MODEL_NOT_IN_MODE, notInModeMessage, projectMode } from "./model-modes/model-modes.mjs";-import { MODE_NOT_BOUND, MODEL_NOT_BOUND, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED, sessionBinder, strictSessions } from "./model-modes/strict-sessions.mjs";-import { redactionPolicy } from "./context/redact-policy.js";+import { MODE_NOT_BOUND, MODEL_NOT_BOUND, SESSION_BIND_UNCONFIRMED, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED, sessionBinder, strictSessions, type StrictSessions } from "./model-modes/strict-sessions.mjs";/** A main chat's request on another model than the one it started on (one model per chat). */const SESSION_MODEL_FIXED = "session_model_fixed";+/** The gateway's retryable strict-session refusal (503, Retry-After), and how often the same request is sent again. */+const STRICT_UNAVAILABLE = "strict_session_unavailable";+const STRICT_RETRIES = 3;/** A chat's title request (opencode-plugins/omnirush-title-recovery.ts marks it) and the chat's own model. */const TITLE_HEADER = "x-omnirush-title";const CHAT_MODEL_HEADER = "x-omnirush-chat-model";@@ -94,7 +96,15 @@ type BrokerOptions = {/** Hears the gateway's mandatory-update signals (see GatewayUpdateSignal). */onUpdateSignal?: (signal: GatewayUpdateSignal) => void;/** A chat's box pin ({ box, revision }, box/service.ts chatPin): the revision a strict-session bind names. */- chatPin?: (sessionId: string) => { box: boolean; revision: string | null } | null;+ chatPin?: (sessionId: string) => { box: boolean; revision: string | null; capturePolicy?: string } | null;+ /** The capture policy the server's binding stored for a chat, when it is not the pinned one: its pin and uploads follow it. */+ onCapturePolicy?: (sessionId: string, policy: string) => void;+ /** What a chat's binding says: verified, or bound late and why (a late binding is never verified). */+ onBinding?: (sessionId: string, binding: { verified: boolean; lateReason?: string }) => void;+ /** The last strict_sessions answer per server, kept on disk: it stands while capabilities cannot be fetched. */+ strictStateFile?: string;+ /** How long a strict-session bind may take (BIND_TIMEOUT_MS unless a test shrinks it). */+ bindTimeoutMs?: number;/*** The desktop app's version: every request to omnirush.ai then names it in* `X-OmniRush-Client: gui/<version>`, so the backend knows an updated app@@ -229,7 +239,7 @@ const GATEWAY_ERROR_COPY: Record<string, string> = {[SESSION_MODEL_USED]: "this chat already ran on another model. Start a new chat to use this one.",};/** Refusals whose own message is shown as it is (the server's or the broker's words). */-const OWN_WORDS = new Set([UPDATE_REQUIRED_CODE, MODEL_NOT_IN_MODE, SESSION_MODEL_FIXED, MODEL_NOT_BOUND, MODE_NOT_BOUND, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED]);+const OWN_WORDS = new Set([UPDATE_REQUIRED_CODE, MODEL_NOT_IN_MODE, SESSION_MODEL_FIXED, MODEL_NOT_BOUND, MODE_NOT_BOUND, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED, SESSION_BIND_UNCONFIRMED]);/** Readable copy for a gateway error code, or null for a code it does not know. */export function gatewayErrorMessage(code: string, detail?: string | null): string | null {@@ -956,6 +966,10 @@ function upstreamUrl(gatewayUrl: string, path: string): string {}/** `<gateway root>/<path>`: the gateway URL with its trailing `/` and `/v1` stripped, as for every omnirush.ai API route. */+function originOf(gatewayUrl: string): string {+ try { return new URL(gatewayUrl).origin; } catch { return gatewayUrl; }+}+function apiUrl(gatewayUrl: string, path: string): string {const url = new URL(gatewayUrl);url.pathname = `${url.pathname.replace(/\/+$/, "").replace(/\/v1$/, "")}/${path}`;@@ -1277,14 +1291,11 @@ export class OmniRushGatewayBroker {private readonly onUpdateSignal?: (signal: GatewayUpdateSignal) => void;private readonly chatPin?: BrokerOptions["chatPin"];/** The strict sessions ({ path, header }) of the last capabilities answer (null: off, or an older server). */- private strictBind: { path: string; header: string } | null = null;+ private strictBind: StrictSessions | null = null;+ private readonly strictStateFile?: string;+ private readonly onCapturePolicy?: BrokerOptions["onCapturePolicy"];+ private readonly onBinding?: BrokerOptions["onBinding"];/** Strict sessions: each chat binds its model and mode once per process (model-modes/strict-sessions.mjs). */- private readonly bindSession = sessionBinder((path, payload, signal) => this.withDeviceBearer((state) => this.fetcher(new URL(path, state.gatewayUrl).toString(), {- method: "POST",- headers: { Authorization: `Bearer ${state.accessToken}`, "Content-Type": "application/json", Accept: "application/json" },- body: JSON.stringify(payload),- signal,- })).then(async (response) => ({ status: response.status, body: await response.json().catch(() => null) })));private readonly reasoningSummaryFor: (model: string) => boolean | Promise<boolean>;private readonly clientHeader: string | null;private refreshInFlight: Promise<boolean> | null = null;@@ -1300,6 +1311,16 @@ export class OmniRushGatewayBroker {private capabilityProbe: { key: string; promise: Promise<TraceCapabilities | null> } | null = null;private capabilityFailure: { gatewayUrl: string; accountKey: string; until: number } | null = null;+ private readonly bindSession: ReturnType<typeof sessionBinder>;+ private makeBinder(timeoutMs?: number): ReturnType<typeof sessionBinder> {+ return sessionBinder((path, payload, signal) => this.withDeviceBearer((state) => this.fetcher(new URL(path, state.gatewayUrl).toString(), {+ method: "POST",+ headers: { Authorization: `Bearer ${state.accessToken}`, "Content-Type": "application/json", Accept: "application/json" },+ body: JSON.stringify(payload),+ signal,+ })).then(async (response) => ({ status: response.status, body: await response.json().catch(() => null) })), timeoutMs ? { timeoutMs } : {});+ }+constructor(options: BrokerOptions) {const gatewayUrl = options.credentials ? normalizedGatewayUrl(options.credentials.gatewayUrl) : null;this.state = gatewayUrl && options.credentials?.accessToken && options.credentials.refreshToken@@ -1337,6 +1358,11 @@ export class OmniRushGatewayBroker {this.modelInMode = options.modelInMode;this.onUpdateSignal = options.onUpdateSignal;this.chatPin = options.chatPin;+ this.bindSession = this.makeBinder(options.bindTimeoutMs);+ this.onCapturePolicy = options.onCapturePolicy;+ this.onBinding = options.onBinding;+ this.strictStateFile = options.strictStateFile;+ if (this.state) this.strictBind = this.savedStrict()[originOf(this.state.gatewayUrl)] ?? null;this.reasoningSummaryFor = options.reasoningSummaryFor ?? defaultReasoningSummaryFor;}@@ -1641,7 +1667,7 @@ export class OmniRushGatewayBroker {if (settledKey && this.state?.gatewayUrl === gatewayUrl) {this.capabilityFailure = null;this.capabilityCache = { gatewayUrl, accountKey: settledKey, capabilities, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };- this.strictBind = strictSessions(capabilities);+ this.rememberStrict(gatewayUrl, capabilities);}return capabilities;}@@ -1987,7 +2013,7 @@ export class OmniRushGatewayBroker {if (fixed) return fixed;// The last capabilities answer's switch: a model request never waits for one.const strict = this.state ? this.strictBind : null;- const unbound = strict ? await this.strictBindRefusal(strict.path, request, path, body) : null;+ const unbound = strict ? await this.strictBindRefusal(strict, request, path, body) : null;if (unbound) return unbound;const forwardedBody = path === "responses" && body !== undefined? await withReasoningSummary(body, this.reasoningSummaryFor)@@ -2000,7 +2026,8 @@ export class OmniRushGatewayBroker {}if (!this.state) break;try {- return await this.forward(request, path, forwardedBody, attempt === 0 ? accessToken : this.state.accessToken, requestId, strict?.header ?? null);+ const response = await this.forward(request, path, forwardedBody, attempt === 0 ? accessToken : this.state.accessToken, requestId, strict?.header ?? null);+ return await this.strictRetry(response, () => this.forward(request, path, forwardedBody, this.state?.accessToken, requestId, strict?.header ?? null), request.signal);} catch (error) {if (request.signal.aborted) throw error;failure = fetchFailure(error);@@ -2104,7 +2131,7 @@ export class OmniRushGatewayBroker {* its main chat. A conflict or a verification refusal is answered here and* never sent; anything else goes on (model-modes/strict-sessions.mjs).*/- private async strictBindRefusal(bindPath: string, request: Request, path: string, body: ArrayBuffer | string | undefined): Promise<Response | null> {+ private async strictBindRefusal(strict: StrictSessions, request: Request, path: string, body: ArrayBuffer | string | undefined): Promise<Response | null> {const session = request.headers.get("x-omnirush-session-id");if (body === undefined || (path !== "responses" && path !== "responses/compact") || !session) return null;let parsed: unknown;@@ -2118,22 +2145,82 @@ export class OmniRushGatewayBroker {const rootId = request.headers.get(PARENT_SESSION_HEADER);const rootModel = rootId ? this.sessionModels.get(rootId) : null;const rootPin = rootId ? this.chatPin?.(rootId) ?? null : null;+ const ownPin = this.chatPin?.(session) ?? null;+ // A sub-agent has no prompt of its own: it takes its main chat's capture policy, as the server does.+ const capturePolicy = ownPin?.capturePolicy ?? rootPin?.capturePolicy ?? null;const outcome = await this.bindSession({- path: bindPath,+ path: strict.path,+ enforce: strict.mode === "enforce",sessionId: session,rootId,model: this.sessionModels.get(session) ?? model,mode: projectMode(this.boxRequest(request)),- revision: this.chatPin?.(session)?.revision ?? rootPin?.revision ?? null,- capturePolicy: redactionPolicy(),- root: rootId && rootModel ? { model: rootModel, mode: projectMode(rootPin?.box ?? this.boxSessions.has(rootId)), revision: rootPin?.revision ?? null } : null,+ revision: ownPin?.revision ?? rootPin?.revision ?? null,+ capturePolicy,+ root: rootId && rootModel ? { model: rootModel, mode: projectMode(rootPin?.box ?? this.boxSessions.has(rootId)), revision: rootPin?.revision ?? null, capturePolicy: rootPin?.capturePolicy ?? null } : null,});+ // The server keeps a chat's first capture policy: its pin and uploads follow the stored one.+ const stored = "binding" in outcome ? outcome.binding?.capture_policy : undefined;+ if (typeof stored === "string" && stored && stored !== capturePolicy) {+ try { this.onCapturePolicy?.(session, stored); } catch { /* a listener never breaks the request */ }+ }+ if ("binding" in outcome && outcome.binding) {+ const binding = outcome.binding;+ const late = typeof binding.late_reason === "string" && binding.late_reason ? binding.late_reason : binding.bound_late === true ? "bound_late" : null;+ try { this.onBinding?.(session, { verified: binding.verified === true && !late, ...(late ? { lateReason: late } : {}) }); } catch { /* a listener never breaks the request */ }+ }if (!("refused" in outcome)) return null;this.log?.("info", "omnirush.ai refused this chat's binding; not sent", { code: outcome.refused.code });- const { code, message } = outcome.refused;+ const { code, message, retry } = outcome.refused;+ if (retry) return Response.json({ detail: code, error: { type: "server_error", code, message } }, { status: 503, headers: { "x-should-retry": "true" } });return Response.json({ detail: code, error: { type: "invalid_request_error", code, message } }, { status: code === VERIFICATION_REQUIRED ? 403 : 400, headers: { "x-should-retry": "false" } });}+ /**+ * strict_session_unavailable (503 under enforce: the server could not look up or bind the chat): the same+ * request again after its Retry-After, at most STRICT_RETRIES times, never on another model or unbound;+ * the engine gets one answer, so the prompt stays one turn.+ */+ private async strictRetry(response: Response, again: () => Promise<Response>, signal: AbortSignal): Promise<Response> {+ for (let attempt = 0; response.status === 503 && attempt < STRICT_RETRIES; attempt += 1) {+ const text = await response.clone().text().catch(() => "");+ if (!text.includes(`"${STRICT_UNAVAILABLE}"`)) break;+ const after = response.headers.has("retry-after") ? Number(response.headers.get("retry-after")) : Number.NaN;+ const wait = (Number.isFinite(after) && after >= 0 ? Math.min(after, 30) : 3) * 1000;+ await response.body?.cancel().catch(() => undefined);+ this.log?.("info", "omnirush.ai could not confirm this chat yet; the same request again", { attempt: attempt + 1, waitMs: wait });+ await pause(wait, signal);+ if (signal.aborted || !this.state) return response;+ response = await again();+ }+ return response;+ }++ private savedStrict(): Record<string, StrictSessions | null> {+ if (!this.strictStateFile) return {};+ try {+ const saved: unknown = JSON.parse(readFileSync(this.strictStateFile, "utf8"));+ if (!isRecord(saved)) return {};+ return Object.fromEntries(Object.entries(saved).map(([origin, value]) => [origin, isRecord(value) ? strictSessions({ strict_sessions: { ...value, bind: true } }) : null]));+ } catch {+ return {};+ }+ }++ /** A capabilities answer's strict sessions: in force now, and kept on disk for this server. */+ private rememberStrict(gatewayUrl: string, capabilities: TraceCapabilities): void {+ this.strictBind = strictSessions(capabilities);+ if (!this.strictStateFile) return;+ try {+ mkdirSync(dirname(this.strictStateFile), { recursive: true, mode: 0o700 });+ const tmp = `${this.strictStateFile}.${randomUUID()}.tmp`;+ writeFileSync(tmp, JSON.stringify({ ...this.savedStrict(), [originOf(gatewayUrl)]: this.strictBind }), { mode: 0o600 });+ renameSync(tmp, this.strictStateFile);+ } catch {+ // Kept in memory; the next answer writes again.+ }+ }+/** Whether a request comes from a box chat: marked so, or from a chat (or its main chat) marked earlier. */private boxRequest(request: Request): boolean {const sessions = [request.headers.get("x-omnirush-session-id"), request.headers.get(PARENT_SESSION_HEADER)].filter((id): id is string => Boolean(id));@@ -2145,7 +2232,8 @@ export class OmniRushGatewayBroker {while (this.boxSessions.size > 1024) this.boxSessions.delete(this.boxSessions.values().next().value!);return true;}- return sessions.some((id) => this.boxSessions.has(id));+ // The chat's durable pin (box/service.ts chatPin): a title, a compaction or a request after a restart is unmarked.+ return sessions.some((id) => this.boxSessions.has(id) || this.chatPin?.(id)?.box === true);}private refresh(expectedAccessToken: string): Promise<boolean> {diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 8c67f8ed..88cfa570 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -1286,6 +1286,15 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {},// Strict sessions: a chat's bind names the box revision it started under.chatPin: (sessionId) => boxServiceFor(config).chatPin(sessionId),+ // The capture policy the server stored for a chat: its pin and its uploads keep that one.+ onCapturePolicy: (sessionId, policy) => {+ void boxServiceFor(config).setCapturePolicy(sessionId, policy);+ captureServicesByServer.get(config)?.pinPolicy(sessionId, policy);+ },+ // Whether the chat's binding is verified, and why it was bound late (kept in its pin, never shown).+ onBinding: (sessionId, binding) => { void boxServiceFor(config).updatePin(sessionId, binding); },+ // The last strict-session switch per server: it stands while capabilities cannot be fetched.+ strictStateFile: join(runtimeStorageDir(config), "strict-sessions.json"),// The desktop's update gate (banner, blocked view); session uploads and// project archives go on whatever it says....(config.omnirushGatewayCredentials?.onUpdateSignal ? { onUpdateSignal: config.omnirushGatewayCredentials.onUpdateSignal } : {}),@@ -2132,7 +2141,8 @@ export async function proxyOpencodeV2Request(input: {const turnsBefore = capture && uploadedSessionId && engine ? await turnsBeforeCapture(capture, uploadedSessionId, engine) : 0;// The Linux sandbox (box/): a prompt of a workspace whose box is on starts the box, and takes the turn's checkpoint when it is captured.// One pin per chat: a chat that runs in the box is not moved to this computer when its project's box is off.- if (promptDispatch && sessionId && await boxServiceFor(input.config).pinnedBoxGone(sessionId, input.workspace)) throw new ApiError(409, "chat_pinned_box", PINNED_BOX_MESSAGE);+ const pinnedBoxWhy = promptDispatch && sessionId ? await boxServiceFor(input.config).pinnedBoxUnavailable(sessionId, input.workspace) : null;+ if (pinnedBoxWhy) throw new ApiError(409, "chat_pinned_box", pinnedBoxMessage(pinnedBoxWhy));const boxInfo = promptDispatch && sessionId? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine), turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body) }): null;@@ -2150,6 +2160,7 @@ export async function proxyOpencodeV2Request(input: {capture.captureSnapshot(uploadedSessionId, "prompt");capture.archiveSessionStarted(uploadedSessionId, input.workspace.path, engine);}+ if (promptDispatch && sessionId) void pinChatCapturePolicy(input.config, capture, sessionId);// A parallel task's first prompt (routes/workspace-tasks.ts): only task workspaces.if (input.workspace.task && promptDispatch && sessionId) {await noteTaskPrompt(input.workspace, sessionId, (id, type, data) => recordOpenSessionTrace(input.config, id, type, data));@@ -2572,7 +2583,8 @@ export async function proxyOpencodeRequest(input: {const turnsBefore = capture && captured && uploadedSessionId && workspace && !steer && isUploadPromptDispatch(method, proxyPath)? await turnsBeforeCapture(capture, uploadedSessionId, engineTarget(baseUrl, headers, search)): 0;- if (workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath) && await boxServiceFor(input.config).pinnedBoxGone(uploadedSessionId, workspace)) throw new ApiError(409, "chat_pinned_box", PINNED_BOX_MESSAGE);+ const pinnedBoxWhy = workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath) ? await boxServiceFor(input.config).pinnedBoxUnavailable(uploadedSessionId, workspace) : null;+ if (pinnedBoxWhy) throw new ApiError(409, "chat_pinned_box", pinnedBoxMessage(pinnedBoxWhy));const boxInfo = workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath)? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured && !steer, turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body) }): null;@@ -2606,6 +2618,7 @@ export async function proxyOpencodeRequest(input: {capture.archiveSessionStarted(uploadedSessionId, workspace.path, engineTarget(baseUrl, headers, search));}}+ if (workspace && uploadedSessionId && !steer && isUploadPromptDispatch(method, proxyPath)) void pinChatCapturePolicy(input.config, capture, uploadedSessionId);// A parallel task's first prompt (routes/workspace-tasks.ts): only task workspaces.if (workspace?.task && uploadedSessionId && !steer && isUploadPromptDispatch(method, proxyPath)) {await noteTaskPrompt(workspace, uploadedSessionId, (id, type, data) => recordOpenSessionTrace(input.config, id, type, data));@@ -7875,7 +7888,25 @@ function parseWorkspaceExportSensitiveMode(input: string | null): WorkspaceExporthrow new ApiError(400, "invalid_workspace_export_sensitive_mode", `Invalid workspace export sensitive mode: ${trimmed}`);}-const PINNED_BOX_MESSAGE = "This chat runs in the sandbox. Turn the sandbox back on for this project, or start a new chat.";+/**+ * The capture policy a chat's uploads keep, in its pin at its first prompt+ * (the uploader's settled policy, after its startSession); a strict-session+ * bind sends it. Without a capture service nothing of the chat is uploaded:+ * "none". Unsettled (null): the pin waits for the next prompt. Never holds the+ * prompt up: under enforce a model request before it is pinned gets a+ * retryable local error and goes once it is.+ */+async function pinChatCapturePolicy(config: ServerConfig, capture: CaptureService | undefined, sessionId: string): Promise<void> {+ const boxes = boxServiceFor(config);+ if (boxes.chatPin(sessionId)?.capturePolicy) return;+ const policy = capture ? await capture.sessionPolicy(sessionId).catch(() => null) : "none";+ if (policy) await boxes.setCapturePolicy(sessionId, policy);+}++/** A chat that started in the box, when the box cannot run now (it never moves to this computer). */+function pinnedBoxMessage(why: string): string {+ return `This chat runs in the sandbox, which can't start right now (${why.replace(/\.$/, "")}). Try again, or start a new chat.`;+}/*** Whether a prompt request names an omnirush.ai model whose new chats start ondiff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.tsindex e538b4a1..2e5f2c69 100644--- a/apps/server/src/session-uploader.ts+++ b/apps/server/src/session-uploader.ts@@ -5845,6 +5845,30 @@ export class SessionUploader {this.scheduleChange(state, "fs_change");}+ /**+ * A tracked session's capture policy once its ledger was read (its pin is+ * final then), waiting at most `timeoutMs`; null while it is not settled or+ * for a session this uploader does not track.+ */+ async settledPolicy(sessionId: string, timeoutMs = 3_000): Promise<RedactionPolicy | null> {+ const state = this.sessions.get(sessionId);+ if (!state) return null;+ let timer: ReturnType<typeof setTimeout> | undefined;+ 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;+ }++ /** The capture policy the server stored for the chat (its binding): the session's uploads keep it. */+ pinPolicy(sessionId: string, policy: string): void {+ if (policy !== CLIENT_RAW && policy !== CLIENT_V3 && policy !== "legacy") return;+ const state = this.sessions.get(sessionId);+ if (state) state.redactionPolicy = policy;+ const record = this.ledger.sessions[sessionId];+ if (record) record.redactionPolicy = policy;+ }+/** Whether the session uploader is currently tracking this session. */hasSession(sessionId: string): boolean {const state = this.sessions.get(sessionId);