A desktop coding agent with free access to frontier models.
TypeScript
fix: a later turn checks the server's settings again and confirms its chat
ogsamrat13a6a39666df
diff --git a/apps/server/src/model-modes/model-modes.test.ts b/apps/server/src/model-modes/model-modes.test.tsindex 86273ce6..9e82208c 100644--- a/apps/server/src/model-modes/model-modes.test.ts+++ b/apps/server/src/model-modes/model-modes.test.ts@@ -420,4 +420,43 @@ describe("model modes", () => {expect(order).toEqual(["bind 200", "upstream 503", "bind 200", "upstream"]);rmSync(dir, { recursive: true, force: true });});++ test("broker: after a failed capabilities check, the next turn asks again and binds explicitly before upstream, with the strict header", async () => {+ const dir = mkdtempSync(path.join(tmpdir(), "strict-caps-fail-"));+ const order: string[] = [];+ const binds: Record<string, unknown>[] = [];+ let capsDown = true;+ const b = new OmniRushGatewayBroker({+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },+ engineToken: "local-engine-token",+ strictStateFile: path.join(dir, "strict-sessions.json"),+ chatPin: () => ({ box: false, revision: null, capturePolicy: "client-v3" }),+ fetch: async (input, init) => {+ const url = new URL(String(input));+ if (url.pathname === "/omnirush/collect/capabilities") {+ order.push(capsDown ? "caps 503" : "caps ok");+ if (capsDown) return Response.json({ detail: "unavailable" }, { status: 503 });+ 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>);+ order.push("bind");+ return Response.json({ binding: { model: "gpt-6-astra", mode: "native", capture_policy: "client-v3", verified: true }, enforcement: "enforce", kept: [] });+ }+ order.push(`upstream strict=${new Headers(init?.headers).get("x-omnirush-strict-session") ?? "none"}`);+ return Response.json({ output: [] });+ },+ });+ await b.sessionUploadCapabilities();+ const post = () => b.handle(request({ model: "gpt-6-astra", tools: [{ type: "function", name: "bash" }] }, { "x-omnirush-session-id": "ses_capsfail" }), "responses");+ // First turn: still no answer; the request carries the strict header (the gateway binds the chat itself).+ expect((await post()).status).toBe(200);+ // The server answers again; the next turn asks before its first request and binds explicitly.+ capsDown = false;+ await new Promise((resolve) => setTimeout(resolve, 2_100));+ expect((await post()).status).toBe(200);+ expect(order).toEqual(["caps 503", "caps 503", "upstream strict=1", "caps ok", "bind", "upstream strict=1"]);+ expect(binds[0]).toMatchObject({ session_id: "ses_capsfail", capture_policy: "client-v3" });+ rmSync(dir, { recursive: true, force: true });+ }, 20_000);});diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.tsindex c685ea92..95615eec 100644--- a/apps/server/src/omnirush-gateway-broker.ts+++ b/apps/server/src/omnirush-gateway-broker.ts@@ -16,7 +16,7 @@ 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_BIND_UNCONFIRMED, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED, sessionBinder, strictSessions, type StrictSessions } from "./model-modes/strict-sessions.mjs";+import { MODE_NOT_BOUND, MODEL_NOT_BOUND, SESSION_BIND_UNCONFIRMED, STRICT_SESSION_HEADER, SESSION_BINDING_CONFLICT, SESSION_MODEL_USED, VERIFICATION_REQUIRED, sessionBinder, strictSessions, type StrictSessions } from "./model-modes/strict-sessions.mjs";import { ClaudeDeviceAccess, type ClaudeDeviceRequest } from "./claude-code/access.js";import type { ClaudeAuthorization } from "./claude-code/runtime.js";@@ -25,6 +25,9 @@ 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;+/** An unknown strict switch: capabilities asked again at most this often, each wait at most this long. */+const STRICT_ASK_MS = 2_000;+const STRICT_ASK_WAIT_MS = 5_000;/** 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";@@ -1296,6 +1299,10 @@ export class OmniRushGatewayBroker {private strictBind: StrictSessions | null = null;/** The server whose capabilities answer `strictBind` is (null: none in this process yet). */private strictOrigin: string | null = null;+ /** When capabilities were last asked again for an unknown switch. */+ private strictAskedAt = 0;+ /** A capabilities check failed and none answered since. */+ private capabilitiesFailed = false;private readonly strictStateFile?: string;private readonly onCapturePolicy?: BrokerOptions["onCapturePolicy"];private readonly onBinding?: BrokerOptions["onBinding"];@@ -1667,11 +1674,13 @@ export class OmniRushGatewayBroker {// The key after the probe: a 401 inside it may have rotated the pair.const settledKey = this.state ? this.capabilityKey(this.state) : null;if (capabilities === null) {+ this.capabilitiesFailed = true;if (settledKey) this.capabilityFailure = { gatewayUrl, accountKey: settledKey, until: Date.now() + TRACE_CAPABILITY_RETRY_MS };return failed();}if (settledKey && this.state?.gatewayUrl === gatewayUrl) {this.capabilityFailure = null;+ this.capabilitiesFailed = false;this.capabilityCache = { gatewayUrl, accountKey: settledKey, capabilities, expiresAt: Date.now() + TRACE_CAPABILITY_TTL_MS };this.rememberStrict(gatewayUrl, capabilities);}@@ -2061,7 +2070,10 @@ export class OmniRushGatewayBroker {const fixed = this.fixedModelRefusal(request, path, body);if (fixed) return fixed;// The last capabilities answer's switch (this server's, kept on disk until it answers): a model request never waits for one.+ // The switch not known yet (capabilities failed, nothing kept): asked again, bounded, before this request.+ if (this.strictUnknown()) await this.askStrictAgain();const strict = this.currentStrict();+ const strictHeader = strict?.header ?? (this.strictUnknown() ? STRICT_SESSION_HEADER : null);const unbound = strict ? await this.strictBindRefusal(strict, request, path, body) : null;if (unbound) return unbound;const forwardedBody = path === "responses" && body !== undefined@@ -2075,7 +2087,7 @@ export class OmniRushGatewayBroker {}if (!this.state) break;try {- const response = 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, strictHeader);return await this.strictRetry(response, async () => {// The gateway could not confirm the chat: it is bound again before the same request goes up.const session = request.headers.get("x-omnirush-session-id");@@ -2084,7 +2096,7 @@ export class OmniRushGatewayBroker {const refused = await this.strictBindRefusal(strict, request, path, body);if (refused) return refused;}- return this.forward(request, path, forwardedBody, this.state?.accessToken, requestId, strict?.header ?? null);+ return this.forward(request, path, forwardedBody, this.state?.accessToken, requestId, strictHeader);}, request.signal);} catch (error) {if (request.signal.aborted) throw error;@@ -2254,6 +2266,27 @@ export class OmniRushGatewayBroker {return response;}+ /** Whether this server's strict-session switch is unknown: its capabilities check failed, no answer since and none kept on disk. */+ private strictUnknown(): boolean {+ if (!this.state || !this.capabilitiesFailed) return false;+ const origin = originOf(this.state.gatewayUrl);+ return this.strictOrigin !== origin && !Object.hasOwn(this.savedStrict(), origin);+ }++ /**+ * Capabilities asked again past their failure cooldown (at most once per STRICT_ASK_MS, waited for at+ * most STRICT_ASK_WAIT_MS): a turn after a failed check binds explicitly as soon as the server answers.+ */+ private async askStrictAgain(): Promise<void> {+ if (Date.now() - this.strictAskedAt < STRICT_ASK_MS) return;+ this.strictAskedAt = Date.now();+ this.capabilityFailure = null;+ let timer: ReturnType<typeof setTimeout> | undefined;+ const late = new Promise<void>((resolve) => { timer = setTimeout(resolve, STRICT_ASK_WAIT_MS); });+ await Promise.race([this.sessionUploadCapabilities().then(() => undefined, () => undefined), late]);+ clearTimeout(timer);+ }+/** The strict sessions in force for the current server: its last answer in this process, else the one kept on disk. */private currentStrict(): StrictSessions | null {if (!this.state) return null;