Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: every request of a chat keeps its model

ogsamrat87673547ea18
diff --git a/apps/server/src/model-modes/model-modes.test.ts b/apps/server/src/model-modes/model-modes.test.ts
index 3d9df721..6d9cd691 100644
--- a/apps/server/src/model-modes/model-modes.test.ts
+++ b/apps/server/src/model-modes/model-modes.test.ts
@@ -4,7 +4,8 @@
// sub-agent fallback for model_not_in_mode.
import { describe, expect, test } from "bun:test";
import { createHash } from "node:crypto";
-import { readFileSync } from "node:fs";
+import { readFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
import path from "node:path";
import { OmniRushGatewayBroker } from "../omnirush-gateway-broker.js";
@@ -102,7 +103,7 @@ describe("model modes", () => {
expect(((await response.json()) as { error: { message: string } }).error.message).toBe("This model isn't available in sandbox projects.");
});
- test("broker: one model per chat; titles (no tools), sub-agents and other chats pass", async () => {
+ test("broker: all context-bearing requests and children inherit the chat model", async () => {
const calls: Call[] = [];
const b = broker(calls, undefined, false);
const tools = [{ type: "function", name: "bash" }];
@@ -110,17 +111,61 @@ describe("model modes", () => {
const refused = await b.handle(request({ model: "gpt-6-astra", tools }, { "x-omnirush-session-id": "ses_one" }), "responses");
expect(refused.status).toBe(400);
expect(((await refused.json()) as { error: { code: string; message: string } }).error).toMatchObject({ code: "session_model_fixed", message: "This chat runs on kimi-k3.1-pro. Start a new chat to use gpt-6-astra." });
- expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_one" }), "responses")).status).toBe(200);
+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_one" }), "responses")).status).toBe(400);
+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_one" }, "responses/compact"), "responses/compact")).status).toBe(400);
+ expect((await b.handle(request({}, { "x-omnirush-session-id": "ses_one" }, "responses/compact"), "responses/compact")).status).toBe(400);
+ expect((await b.handle(request({ model: "gpt-6-astra", tools }, { "x-omnirush-session-id": "ses_child", "x-parent-session-id": "ses_one" }), "responses")).status).toBe(400);
+ expect((await b.handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-session-id": "ses_child", "x-parent-session-id": "ses_one" }), "responses")).status).toBe(200);
+ expect((await b.handle(request({ model: "gpt-6-astra", tools }, { "x-omnirush-session-id": "unknown_child", "x-parent-session-id": "unseen" }), "responses")).status).toBe(400);
+ expect((await b.handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-session-id": "ses_one" }, "responses/compact"), "responses/compact")).status).toBe(200);
expect((await b.handle(request({ model: "gpt-6-astra", tools }, { "x-omnirush-session-id": "ses_two" }), "responses")).status).toBe(200);
- expect(calls.map((c) => c.model)).toEqual(["kimi-k3.1-pro", "gpt-6-astra", "gpt-6-astra"]);
+ expect(calls.map((c) => c.model)).toEqual(["kimi-k3.1-pro", "kimi-k3.1-pro", "kimi-k3.1-pro", "gpt-6-astra"]);
});
- test("broker: a sub-agent's picked model refused for the mode runs on the main model", async () => {
+ test("broker: a new process keeps saved model pins and refuses corrupt pins", async () => {
+ const dir = mkdtempSync(path.join(tmpdir(), "model-pins-"));
+ const file = path.join(dir, "chat-models.json");
+ const calls: string[] = [];
+ const make = () => new OmniRushGatewayBroker({sessionModelsFile: file, credentials: {gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "fixture", refreshToken: "fixture"}, engineToken: "local-engine-token", fetch: async () => {calls.push("upstream"); return Response.json({output: []});}});
+ try {
+ expect((await make().handle(request({model: "gpt-6-astra"}, {"x-omnirush-session-id": "persisted"}), "responses")).status).toBe(200);
+ const resumed = make();
+ expect((await resumed.handle(request({model: "gpt-6-sol"}, {"x-omnirush-session-id": "persisted"}), "responses")).status).toBe(400);
+ expect((await resumed.handle(request({model: "gpt-6-astra"}, {"x-omnirush-session-id": "child", "x-parent-session-id": "persisted"}), "responses")).status).toBe(200);
+ expect(calls).toHaveLength(2);
+ writeFileSync(file, "not-json");
+ expect(make).toThrow();
+ } finally {rmSync(dir, {recursive: true, force: true});}
+ });
+
+ test("broker: a fallback cannot change a session that has already requested another model", async () => {
const calls: Call[] = [];
const response = await broker(calls, (call) => call.model === "kimi-k3.1-pro" ? Response.json(REFUSAL, { status: 400, headers: { "x-should-retry": "false" } }) : Response.json({ output: [] }), false)
.handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-box": "1", "x-omnirush-session-id": "ses_sub", "x-omnirush-subagent-fallback-model": "gpt-6-astra" }), "responses");
- expect(response.status).toBe(200);
- expect(calls.map((c) => c.model)).toEqual(["kimi-k3.1-pro", "gpt-6-astra"]);
+ expect(response.status).toBe(400);
+ expect(calls.map((c) => c.model)).toEqual(["kimi-k3.1-pro"]);
+ });
+
+ test("broker: cooldown cannot rewrite a pinned child's requested model before validation", async () => {
+ const calls: string[] = [];
+ const b = new OmniRushGatewayBroker({credentials: {gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "fixture", refreshToken: "fixture"}, engineToken: "local-engine-token", subagentModelRefused: () => true, fetch: async (_input, init) => {calls.push(JSON.parse(typeof init?.body === "string" ? init.body : new TextDecoder().decode(init?.body as ArrayBuffer)).model); return Response.json({output: []});}});
+ expect((await b.handle(request({model: "gpt-6-astra"}, {"x-omnirush-session-id": "root"}), "responses")).status).toBe(200);
+ expect((await b.handle(request({model: "gpt-6-sol"}, {"x-omnirush-session-id": "child", "x-parent-session-id": "root", "x-omnirush-subagent-fallback-model": "gpt-6-astra"}), "responses")).status).toBe(400);
+ expect(calls).toEqual(["gpt-6-astra"]);
+ });
+
+ test("broker: a chat's title goes on the chat's model, whatever small model the engine picked", async () => {
+ const calls: Call[] = [];
+ const b = broker(calls, undefined, false);
+ const tools = [{ type: "function", name: "bash" }];
+ const title = { "x-omnirush-title": "1", "x-omnirush-chat-model": "kimi-k3.1-pro" };
+ // A new chat's title first: it takes the model the chat's prompt names, and pins it.
+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_t", ...title }), "responses")).status).toBe(200);
+ expect((await b.handle(request({ model: "kimi-k3.1-pro", tools }, { "x-omnirush-session-id": "ses_t" }), "responses")).status).toBe(200);
+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_t", ...title }), "responses")).status).toBe(200);
+ // Unmarked, another model is still refused.
+ expect((await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_t" }), "responses")).status).toBe(400);
+ 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 () => {
@@ -130,10 +175,14 @@ describe("model modes", () => {
const bound = new Map<string, { model: string; mode: string }>([["ses_other", { model: "kimi-k3.1-pro", mode: "native" }]]);
let strictOn = false;
const tools = [{ type: "function", name: "bash" }];
+ const pins = path.join(mkdtempSync(path.join(tmpdir(), "strict-pins-")), "chat-models.json");
+ writeFileSync(pins, JSON.stringify({ ses_lost: "gpt-6-astra" }));
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_lost" ? { box: true, revision: "lb1:0123456789ab" } : null),
+ // A chat pinned here in an earlier run that the server never bound (its child meets session_unknown_parent).
+ sessionModelsFile: pins,
fetch: async (input, init) => {
const url = new URL(String(input));
const headers = new Headers(init?.headers);
diff --git a/apps/server/src/omnirush-gateway-broker.test.ts b/apps/server/src/omnirush-gateway-broker.test.ts
index 68d35f6c..81541878 100644
--- a/apps/server/src/omnirush-gateway-broker.test.ts
+++ b/apps/server/src/omnirush-gateway-broker.test.ts
@@ -1412,7 +1412,7 @@ describe("OmniRush gateway broker session upload deadline", () => {
});
});
-describe("OmniRush gateway broker: sub-agent model fallback", () => {
+describe("OmniRush gateway broker: legacy unbound model fallback", () => {
type Reply = (body: Record<string, unknown>) => Response;
function fallbackBroker(reply: Reply, refused: (model: string) => boolean = () => false) {
const calls: UpstreamCall[] = [];
@@ -1435,10 +1435,7 @@ describe("OmniRush gateway broker: sub-agent model fallback", () => {
const subagentHeaders = {
"x-omnirush-subagent-fallback-model": "gpt-6-astra",
"x-omnirush-subagent-fallback-effort": "max",
- "x-omnirush-subagent-root": "ses_main",
- "x-omnirush-session-id": "ses_child",
"x-omnirush-task-id": "msg_1",
- "x-parent-session-id": "ses_main",
};
const refuse = (status: number, code: string) => Response.json({ detail: code }, { status });
@@ -1459,13 +1456,12 @@ describe("OmniRush gateway broker: sub-agent model fallback", () => {
// The private headers never leave the machine.
for (const call of calls) {
expect([...call.headers.keys()].filter((name) => name.startsWith("x-omnirush-subagent"))).toEqual([]);
- expect(call.headers.get("x-omnirush-session-id")).toBe("ses_child");
- // The main session goes upstream: the gateway files the request under it.
- expect(call.headers.get("x-parent-session-id")).toBe("ses_main");
+ expect(call.headers.get("x-omnirush-session-id")).toBe(null);
+ expect(call.headers.get("x-parent-session-id")).toBe(null);
}
expect(events).toEqual([expect.objectContaining({
- sessionId: "ses_child",
- rootSessionId: "ses_main",
+ sessionId: null,
+ rootSessionId: null,
messageId: "msg_1",
requested: "meta-muse-spark",
used: "gpt-6-astra",
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index cf9bffcf..d4ba3660 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -1,5 +1,6 @@
import { randomUUID, timingSafeEqual } from "node:crypto";
-import { createReadStream } from "node:fs";
+import { createReadStream, readFileSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
+import { dirname } from "node:path";
import { SESSION_UPLOAD_BUDGET, SESSION_UPLOAD_ENDPOINT_PATH, sessionUploadTimeoutMs, type SessionUploadBudget } from "./session-upload-budget.js";
import { externalFetch } from "./server-fetch.js";
@@ -20,6 +21,9 @@ import { redactionPolicy } from "./context/redact-policy.js";
/** 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";
+/** 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";
/** A refused attachment is left out and the model request sent again, at most this often per request. */
const MAX_ATTACHMENT_RETRIES = 2;
const MAX_ATTACHMENT_ERROR_BYTES = 256 * 1024;
@@ -56,6 +60,8 @@ export type SubagentModelFallbackEvent = {
type BrokerOptions = {
credentials?: OmniRushGatewayCredentials;
engineToken?: string;
+ /** Durable model pins; production supplies the runtime-local state file. */
+ sessionModelsFile?: string;
fetch?: typeof externalFetch;
/** Electron's file-aware transport. Standalone runtimes use the bounded Node stream fallback. */
uploadFile?: (url: string, init: {
@@ -1265,8 +1271,9 @@ export class OmniRushGatewayBroker {
* question) still goes out as a box request. Oldest first, bounded.
*/
private readonly boxSessions = new Set<string>();
- /** Each chat's model, from its first agent request (one model per chat; oldest first, bounded). */
+ /** Each chat's model, from its first context-bearing request; pins are not evicted. */
private readonly sessionModels = new Map<string, string>();
+ private readonly sessionModelsFile?: string;
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). */
@@ -1303,6 +1310,16 @@ export class OmniRushGatewayBroker {
}
: null;
this.engineToken = options.engineToken?.trim() ?? "";
+ this.sessionModelsFile = options.sessionModelsFile;
+ if (this.sessionModelsFile) {
+ try {
+ const saved: unknown = JSON.parse(readFileSync(this.sessionModelsFile, "utf8"));
+ if (!isRecord(saved) || Object.values(saved).some(model => typeof model !== "string" || !model)) throw new Error("Invalid saved session model pins");
+ for (const [session, model] of Object.entries(saved)) if (typeof model === "string") this.sessionModels.set(session, model);
+ } catch (error) {
+ if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
+ }
+ }
this.invalidate = options.credentials?.invalidate;
this.refreshOwner = options.credentials?.refresh;
const fetcher = options.fetch ?? externalFetch;
@@ -1384,7 +1401,8 @@ export class OmniRushGatewayBroker {
: await this.requestBody(request, normalizedPath);
// A picked sub-agent model the gateway refused moments ago is not tried
// again for every step of a running sub-agent: it goes to the main model.
- const skipped = requestBody === undefined ? null : this.skipRefusedSubagentModel(request, normalizedPath, requestBody);
+ const sessionBound = request.headers.has("x-omnirush-session-id") || request.headers.has(PARENT_SESSION_HEADER) || request.headers.has(SUBAGENT_ROOT_SESSION_HEADER);
+ const skipped = requestBody === undefined || sessionBound ? null : this.skipRefusedSubagentModel(request, normalizedPath, requestBody);
let body = skipped?.body ?? requestBody;
const attachments = this.leaveOutRefusedAttachments(normalizedPath, body, requestId);
if (attachments) body = attachments.body;
@@ -1431,7 +1449,7 @@ export class OmniRushGatewayBroker {
}
if (skipped) {
this.reportSubagentFallback(request, { ...skipped.event, status: response.status, ok: response.ok, clientRequestId: requestId });
- } else if (!response.ok && response.status !== 401 && body !== undefined) {
+ } else if (!sessionBound && !response.ok && response.status !== 401 && body !== undefined) {
response = await this.subagentFallback(request, normalizedPath, body, response, requestId);
}
await this.noteUpdateSignals(response, true);
@@ -1954,6 +1972,7 @@ export class OmniRushGatewayBroker {
accessToken: string | undefined,
requestId: string,
): Promise<Response> {
+ body = this.titleOnChatModel(request, path, body);
// A model the chat's mode does not allow: refused here as the gateway would, never sent.
if (this.modelInMode && body !== undefined && (path === "responses" || path === "responses/compact")) {
const model = requestedModel(body);
@@ -2029,32 +2048,54 @@ export class OmniRushGatewayBroker {
}
/**
- * A main chat's agent request (it offers tools; titles and summaries do not)
- * whose model is not the one the chat started on: a 400
- * `session_model_fixed`, never sent. Sub-agent chats (they name their main
- * chat) are left to the swarm's own rules.
+ * Every context-bearing request keeps the chat's model, including summaries,
+ * compaction and children. A different model never reaches the gateway.
*/
private fixedModelRefusal(request: Request, path: string, body: ArrayBuffer | string | undefined): Response | null {
- if (body === undefined || path !== "responses" || request.headers.get(PARENT_SESSION_HEADER)) return null;
+ if (body === undefined || (path !== "responses" && path !== "responses/compact")) return null;
const session = request.headers.get("x-omnirush-session-id");
- if (!session) return null;
+ if (!session) return request.headers.has(PARENT_SESSION_HEADER) || request.headers.has(SUBAGENT_ROOT_SESSION_HEADER)
+ ? Response.json({error: {type: "invalid_request_error", code: SESSION_MODEL_FIXED, message: "A sub-agent request must name its session and pinned parent."}}, {status: 400, headers: {"x-should-retry": "false"}})
+ : null;
let parsed: unknown;
try {
parsed = JSON.parse(typeof body === "string" ? body : new TextDecoder().decode(body));
} catch {
return null;
}
- if (!isRecord(parsed) || typeof parsed.model !== "string" || !Array.isArray(parsed.tools) || parsed.tools.length === 0) return null;
+ if (!isRecord(parsed) || typeof parsed.model !== "string" || !parsed.model.replace(/^omnirush\//, "").trim()) return Response.json({error: {type: "invalid_request_error", code: SESSION_MODEL_FIXED, message: "A session request must name its fixed model."}}, {status: 400, headers: {"x-should-retry": "false"}});
const model = parsed.model.replace(/^omnirush\//, "");
- const first = this.sessionModels.get(session);
- if (!first) {
+ const parents = [request.headers.get(SUBAGENT_ROOT_SESSION_HEADER), request.headers.get(PARENT_SESSION_HEADER)].filter((id): id is string => Boolean(id));
+ const pins = [session, ...parents].map(id => this.sessionModels.get(id)).filter((pin): pin is string => Boolean(pin));
+ const first = pins.find(pin => pin !== model);
+ if (!first && !parents.some(id => !this.sessionModels.has(id))) {
+ if (this.sessionModels.has(session)) return null;
+ if (this.sessionModelsFile) {
+ mkdirSync(dirname(this.sessionModelsFile), {recursive: true, mode: 0o700});
+ const tmp = `${this.sessionModelsFile}.${randomUUID()}.tmp`;
+ writeFileSync(tmp, JSON.stringify({...Object.fromEntries(this.sessionModels), [session]: model}), {mode: 0o600});
+ renameSync(tmp, this.sessionModelsFile);
+ }
this.sessionModels.set(session, model);
- while (this.sessionModels.size > 2048) this.sessionModels.delete(this.sessionModels.keys().next().value!);
return null;
}
- if (first === model) return null;
this.log?.("info", "omnirush.ai chat model change refused (one model per chat)", { first, model });
- return Response.json({ detail: SESSION_MODEL_FIXED, error: { type: "invalid_request_error", code: SESSION_MODEL_FIXED, message: `This chat runs on ${first}. Start a new chat to use ${model}.` } }, { status: 400, headers: { "x-should-retry": "false" } });
+ return Response.json({ detail: SESSION_MODEL_FIXED, error: { type: "invalid_request_error", code: SESSION_MODEL_FIXED, message: first ? `This chat runs on ${first}. Start a new chat to use ${model}.` : "Resume the parent chat before continuing this sub-agent; its fixed model is unavailable." } }, { status: 400, headers: { "x-should-retry": "false" } });
+ }
+
+ /**
+ * A chat's title request (the title plugin marks it, with the chat's own
+ * model) goes on the chat's model: the engine picks its cheapest model for
+ * titles, and a chat runs on one model only.
+ */
+ private titleOnChatModel(request: Request, path: string, body: ArrayBuffer | string | undefined): ArrayBuffer | string | undefined {
+ const session = request.headers.get("x-omnirush-session-id");
+ if (body === undefined || path !== "responses" || !session || request.headers.get(TITLE_HEADER) !== "1") return body;
+ const named = (request.headers.get(CHAT_MODEL_HEADER) ?? "").replace(/^omnirush\//, "");
+ const chat = this.sessionModels.get(session) ?? (FALLBACK_MODEL_ID.test(named) ? named : null);
+ const asked = requestedModel(body);
+ if (!chat || !asked || asked.replace(/^omnirush\//, "") === chat) return body;
+ return withModel(body, asked.startsWith("omnirush/") ? `omnirush/${chat}` : chat, null) ?? body;
}
/**
@@ -2065,14 +2106,14 @@ export class OmniRushGatewayBroker {
*/
private async strictBindRefusal(bindPath: string, 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" || !session) return null;
+ if (body === undefined || (path !== "responses" && path !== "responses/compact") || !session) return null;
let parsed: unknown;
try {
parsed = JSON.parse(typeof body === "string" ? body : new TextDecoder().decode(body));
} catch {
return null;
}
- if (!isRecord(parsed) || typeof parsed.model !== "string" || !Array.isArray(parsed.tools) || parsed.tools.length === 0) return null;
+ if (!isRecord(parsed) || typeof parsed.model !== "string") return null;
const model = parsed.model.replace(/^omnirush\//, "");
const rootId = request.headers.get(PARENT_SESSION_HEADER);
const rootModel = rootId ? this.sessionModels.get(rootId) : null;
diff --git a/apps/server/src/opencode-plugins/omnirush-title-recovery.ts b/apps/server/src/opencode-plugins/omnirush-title-recovery.ts
index 6d9bd2b7..95389404 100644
--- a/apps/server/src/opencode-plugins/omnirush-title-recovery.ts
+++ b/apps/server/src/opencode-plugins/omnirush-title-recovery.ts
@@ -162,8 +162,13 @@ export const OmniRushTitleRecovery = async (input: {
return {
"chat.headers": async (request: {
sessionID: string; agent: string; model: { id: string; providerID: string };
+ message?: { model?: { modelID?: string } };
}, output: { headers: Record<string, string> }) => {
if (request.agent !== "title") return;
+ // One model per chat: the broker sends the title on the chat's model, not the engine's cheapest.
+ output.headers["x-omnirush-title"] = "1";
+ const chatModel = request.message?.model?.modelID;
+ if (typeof chatModel === "string" && chatModel) output.headers["x-omnirush-chat-model"] = chatModel;
const id = crypto.randomUUID();
const diagnostic = {
sessionID: request.sessionID, providerID: request.model.providerID,
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 5417a5c2..8c67f8ed 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -1254,6 +1254,7 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
: undefined
);
const gatewayBroker = new OmniRushGatewayBroker({
+ sessionModelsFile: join(runtimeStorageDir(config), "chat-models.json"),
log: (level, message, attributes) => logger.log(level, message, attributes),
credentials: gatewayCredentials
? {