A desktop coding agent with free access to frontier models.
TypeScript
feat: per-project model modes
ogsamrat1e4469a285dd
diff --git a/apps/app/src/app/lib/good-session.ts b/apps/app/src/app/lib/good-session.tsindex 22dc7583..2776d804 100644--- a/apps/app/src/app/lib/good-session.ts+++ b/apps/app/src/app/lib/good-session.ts@@ -804,7 +804,7 @@ function sessionErrorOf(message: UIMessage): Record<string, unknown> | null {* final (`"retry": false`). Its messages start with* "omnirush.ai: " (and usually end on a console link); retrying cannot help.*/-const REFUSAL = /(^|Message: )omnirush\.ai:|daily_grant_exhausted|request_policy_refused|cyber_policy|limit_tokens|limit_session_cap|"retry":\s*false|windows_cutoff|session_quality_cap|update_required|client update required/im;+const REFUSAL = /(^|Message: )omnirush\.ai:|daily_grant_exhausted|request_policy_refused|cyber_policy|limit_tokens|limit_session_cap|"retry":\s*false|windows_cutoff|session_quality_cap|model_not_in_mode|This model isn't available (?:in sandbox projects|in normal projects|outside the sandbox|in this project)|update_required|client update required/im;/*** How a turn that ended on an error ended. The engine's MessageAbortedErrordiff --git a/apps/app/src/app/lib/omnirush-server.ts b/apps/app/src/app/lib/omnirush-server.tsindex 70ccfc8a..68b77c62 100644--- a/apps/app/src/app/lib/omnirush-server.ts+++ b/apps/app/src/app/lib/omnirush-server.ts@@ -605,7 +605,7 @@ export type OmniRushSubagentModelState = {/** Whether an omnirush.ai account is signed in (its models can run). */signedIn: boolean;/** The account's model catalog, in the backend's order. */- models: Array<{ id: string; name: string; family: string | null; default: boolean; efforts: string[] }>;+ models: Array<{ id: string; name: string; family: string | null; default: boolean; efforts: string[]; modes?: Array<"native" | "box"> }>;};export type OmniRushDesktopCloudSyncChange = {diff --git a/apps/app/src/components/model-select.tsx b/apps/app/src/components/model-select.tsxindex acb28763..1892e2c1 100644--- a/apps/app/src/components/model-select.tsx+++ b/apps/app/src/components/model-select.tsx@@ -59,6 +59,7 @@ import {resolveOmniRushModelGroup,} from "@/app/utils";import { isSupportedModelProvider, providerCatalogRank } from "@/app/lib/provider-catalog";+import { useModelModeFilter } from "@/react-app/domains/session/surface/model-modes";function getProviderDisplayName(providerId: string) {return providerId@@ -260,7 +261,10 @@ export function ModelSelect({const denAuth = useDenAuth();const favorites = useModelCollectionsStore((state) => state.favorites);const recent = useModelCollectionsStore((state) => state.recent);- const catalogOptions = useModelOptions(open, fallbackOptions, denAuth.isSignedIn);+ // Only the models the open project's mode allows (sandbox or not: model-modes.ts).+ const modeAllows = useModelModeFilter((state) => state.allows);+ const allOptions = useModelOptions(open, fallbackOptions, denAuth.isSignedIn);+ const catalogOptions = React.useMemo(() => allOptions.filter((option) => modeAllows(option)), [allOptions, modeAllows]);const modelOptions = React.useMemo(() => dedupeModelOptions(overlaySelectedBehavior(diff --git a/apps/app/src/react-app/domains/session/surface/model-modes.ts b/apps/app/src/react-app/domains/session/surface/model-modes.tsnew file mode 100644index 00000000..20fefec6--- /dev/null+++ b/apps/app/src/react-app/domains/session/surface/model-modes.ts@@ -0,0 +1,73 @@+// Per-model modes in the app: a sandbox (box) project shows and sends only the+// models that may run in the sandbox, a project without it only the ones that+// may run outside it. The server lists each catalog model's `modes`+// (GET /omnirush/subagent-model, normalized from the backend's catalog by the+// shared apps/server/src/model-modes/model-modes.mjs: no `modes` means both).+// The gateway broker sends X-OmniRush-Box for box chats and refuses a model+// outside the chat's mode before it leaves this computer.+import { useEffect, useMemo } from "react";+import { create } from "zustand";++import type { ModelRef } from "@/app/types";+import type { OmniRushSubagentModelState } from "@/app/lib/omnirush-server";+import { useLinuxSandbox, type LinuxSandboxClient } from "@/react-app/domains/settings/linux-sandbox";+import { useSubagentModel, type SubagentModelClient } from "@/react-app/domains/settings/subagent-model";++type CatalogModels = OmniRushSubagentModelState["models"];+type Mode = "native" | "box";++/** Whether `model` may run in `mode`: any other provider's, an unknown model, or one listing the mode. */+export function modeAllowsModel(models: CatalogModels | undefined, mode: Mode | null, model: ModelRef | null | undefined): boolean {+ if (!mode || !model || model.providerID !== "omnirush") return true;+ const entry = models?.find((candidate) => candidate.id === model.modelID);+ return !entry?.modes || entry.modes.includes(mode);+}++/** The model a project in `mode` uses instead of `model`: the catalog default among the allowed ones, else the first allowed. */+export function modeFallbackModel(models: CatalogModels | undefined, mode: Mode | null, model: ModelRef | null | undefined): ModelRef | null {+ if (modeAllowsModel(models, mode, model)) return model ?? null;+ const allowed = (models ?? []).filter((entry) => !entry.modes || entry.modes.includes(mode!));+ const pick = allowed.find((entry) => entry.default) ?? allowed[0];+ return pick ? { providerID: "omnirush", modelID: pick.id } : null;+}++/** "Kimi K3.1 Pro isn't available in sandbox projects. Using GPT 6 Astra here." */+export function modeSwitchNote(models: CatalogModels | undefined, mode: Mode, from: ModelRef, to: ModelRef): string {+ const name = (ref: ModelRef) => models?.find((entry) => entry.id === ref.modelID)?.name ?? ref.modelID;+ return `${name(from)} isn't available ${mode === "box" ? "in sandbox projects" : "outside the sandbox"}. Using ${name(to)} here.`;+}++/**+ * The open project's rule for every model list the app shows (the composer's+ * model menu, the model picker): set by the session route, all models while+ * no project mode is known.+ */+export const useModelModeFilter = create<{ allows: (model: ModelRef | null | undefined) => boolean }>(() => ({ allows: () => true }));++/**+ * The mode of the open project (or chat: one kept on this computer when the+ * sandbox became required stays native) and its model rules. `mode` is null+ * until the project's sandbox state is known: nothing is filtered meanwhile.+ */+export function useProjectModelModes(+ client: (LinuxSandboxClient & SubagentModelClient) | null,+ workspaceId: string | null,+ sessionId: string | null,+) {+ const sandbox = useLinuxSandbox(client, workspaceId, { fresh: false, sessionId });+ const { state } = useSubagentModel(client);+ const box = sandbox.box;+ const mode: Mode | null = box ? (box.on && !box.chatKept ? "box" : "native") : null;+ const models = state?.models;+ const rules = useMemo(() => ({+ mode,+ allows: (model: ModelRef | null | undefined) => modeAllowsModel(models, mode, model),+ fallback: (model: ModelRef | null | undefined) => modeFallbackModel(models, mode, model),+ note: (from: ModelRef, to: ModelRef) => modeSwitchNote(models, mode ?? "native", from, to),+ }), [mode, models]);+ useEffect(() => {+ useModelModeFilter.setState({ allows: rules.allows });+ }, [rules]);+ useEffect(() => () => useModelModeFilter.setState({ allows: () => true }), []);+ return rules;+}diff --git a/apps/app/src/react-app/shell/session-route.tsx b/apps/app/src/react-app/shell/session-route.tsxindex 27d32243..3c6f1bc3 100644--- a/apps/app/src/react-app/shell/session-route.tsx+++ b/apps/app/src/react-app/shell/session-route.tsx@@ -141,6 +141,7 @@ import { computeModelAvailability, createUnavailableConfirmationGate, sessionModimport { useSessionFindStore } from "@/react-app/domains/session/surface/find-store";import { useModelPicker } from "@/react-app/domains/session/modals/use-model-picker";import { getSessionModelSelection, resolveSendVariant, useSessionModelStore } from "@/react-app/domains/session/surface/session-model-store";+import { useProjectModelModes } from "@/react-app/domains/session/surface/model-modes";import { useWorkbenchStore } from "@/react-app/domains/session/chat/workbench-store";import { resolveWorkbenchPaneEndpoint } from "@/react-app/domains/session/chat/pane-runtime";import {@@ -996,6 +997,26 @@ export function SessionRoute() {cloudProviderSyncReady,entitledModelCount: entitledModelOptions.length,});+ // The open project's mode (sandbox or not) and the models it allows (model-modes.ts).+ const modelModes = useProjectModelModes(selectedWorkspaceEndpoint?.client ?? null, selectedWorkspaceEndpoint?.workspaceId ?? null, selectedSessionId || null);+ const modelModesRef = useRef(modelModes);+ modelModesRef.current = modelModes;+ // The default as this project may use it: one its mode does not allow moves to the allowed default.+ const projectDefaultModel = modelModes.fallback(local.prefs.defaultModel ?? null) ?? local.prefs.defaultModel ?? null;+ /**+ * The model a send in the open project goes out on: one its mode does not+ * allow moves to the allowed default before anything is sent, with a note,+ * and the chat remembers it.+ */+ const modeSendModel = (model: ModelRef | null | undefined, sessionId: string | null): ModelRef | null => {+ const rules = modelModesRef.current;+ if (!model || rules.allows(model)) return model ?? null;+ const next = rules.fallback(model);+ if (!next) return model;+ toast(rules.note(model, next), { id: `model-mode:${sessionId ?? "new"}` });+ if (sessionId) useSessionModelStore.getState().setModel(sessionId, next, null);+ return next;+ };const modelPicker = useModelPicker({client: opencodeClient,baseUrl: opencodeBaseUrl,@@ -1098,7 +1119,12 @@ export function SessionRoute() {const selectedSessionModelSelection = useSessionModelStore((state) =>(selectedSessionId ? state.bySessionId[selectedSessionId] ?? null : null),);- const activeComposerModel = selectedSessionModelSelection?.model ?? local.prefs.defaultModel ?? null;+ const activeComposerModel = selectedSessionModelSelection?.model ?? projectDefaultModel;+ // A chat whose model its project's mode does not allow (the sandbox turned on or off since): moved before any send.+ useEffect(() => {+ const model = selectedSessionModelSelection?.model;+ if (selectedSessionId && model && !modelModes.allows(model)) modeSendModel(model, selectedSessionId);+ }, [modelModes, selectedSessionId, selectedSessionModelSelection?.model]);const activeComposerAvailability = resolveModelAvailability(activeComposerModel);const activeComposerTargetsSession = Boolean(selectedSessionModelSelection && selectedSessionId);const selectedModelUnavailableKey = activeComposerAvailability.status === "unavailable" && activeComposerModel@@ -1393,7 +1419,7 @@ export function SessionRoute() {modelUnavailableMessage,resolveModelAvailability,organizationModelsEmpty,- selectedModel: local.prefs.defaultModel ?? { providerID: "", modelID: "" },+ selectedModel: projectDefaultModel ?? { providerID: "", modelID: "" },omniRushModelsEntitled,omniRushModelsSyncing,onRefreshOrganizationModels: refreshOrganizationModelAccess,@@ -1434,7 +1460,7 @@ export function SessionRoute() {// Per-conversation model memory: a session that picked its own model// sends with it (and its variant) instead of the global default.const sessionModelSelection = getSessionModelSelection(targetSessionId);- const configuredSendModel = sessionModelSelection?.model ?? local.prefs.defaultModel;+ const configuredSendModel = modeSendModel(sessionModelSelection?.model ?? local.prefs.defaultModel, targetSessionId);const sendModel = configuredSendModel;// The effort the picker shows for this model, never a null it hides.const sendVariant = resolveSendVariant(sendModel, sessionModelSelection ? sessionModelSelection.variant : modelVariantValue);@@ -1678,6 +1704,7 @@ export function SessionRoute() {modelLabel,modelUnavailableMessage,organizationModelsEmpty,+ projectDefaultModel,modelVariantLabel,modelVariantValue,navigate,@@ -1794,7 +1821,9 @@ export function SessionRoute() {return { outcome: "cancelled", reason: "context_changed" };}const sessionModelSelection = getSessionModelSelection(targetSessionId);- const configuredSendModel = sessionModelSelection?.model ?? local.prefs.defaultModel;+ const remembered = sessionModelSelection?.model ?? local.prefs.defaultModel;+ // The open project's mode rules apply to its own chats (another project's go to the broker, which refuses).+ const configuredSendModel = workspace.id === selectedWorkspaceId ? modeSendModel(remembered, targetSessionId) : remembered;const sendModel = configuredSendModel;// The effort the picker shows for this model, never a null it hides.const sendVariant = resolveSendVariant(sendModel, sessionModelSelection ? sessionModelSelection.variant : modelVariantValue);@@ -4091,7 +4120,7 @@ export function SessionRoute() {onOpenAutomations={() => navigate(automationsRoute())}onOpenDashboard={() => navigate(dashboardRoute())}onCreateWorkspace={handleOpenCreateWorkspace}- modelOptions={modelPicker.options}+ modelOptions={modelPicker.options.filter((option) => modelModes.allows(option))}selectedModel={paletteSelectedModel}selectedModelBehavior={paletteSelectedModelBehavior}onSelectModel={(next, behavior) => {@@ -4132,7 +4161,7 @@ export function SessionRoute() {/><ModelPickerModalopen={modelPicker.open}- options={modelPicker.options}+ options={modelPicker.options.filter((option) => modelModes.allows(option))}organizationModelsEmpty={organizationModelsEmpty}organizationModelsSettingsUrl={organizationModelsSettingsUrl}diff --git a/apps/app/tests/model-modes.test.ts b/apps/app/tests/model-modes.test.tsnew file mode 100644index 00000000..9a337816--- /dev/null+++ b/apps/app/tests/model-modes.test.ts@@ -0,0 +1,31 @@+import { expect, test } from "bun:test";++import { modeSwitchNote as sharedNote } from "../../server/src/model-modes/model-modes.mjs";+import { modeAllowsModel, modeFallbackModel, modeSwitchNote } from "../src/react-app/domains/session/surface/model-modes";++// As GET /omnirush/subagent-model lists them: `modes` normalized by the server.+const MODELS = [+ { id: "gpt-6-astra", name: "GPT 6 Astra", family: "OpenAI", default: true, efforts: [], modes: ["native", "box"] as Array<"native" | "box"> },+ { id: "kimi-k3.1-pro", name: "Kimi K3.1 Pro", family: "Kimi", default: false, efforts: [], modes: ["native"] as Array<"native" | "box"> },+ { id: "boxonly-x", name: "Box Only X", family: "Other", default: false, efforts: [], modes: ["box"] as Array<"native" | "box"> },+];+const ref = (modelID: string, providerID = "omnirush") => ({ providerID, modelID });++test("a project's mode decides which omnirush models it may use; others, unknown ones and an unknown mode pass", () => {+ expect(modeAllowsModel(MODELS, "box", ref("kimi-k3.1-pro"))).toBe(false);+ expect(modeAllowsModel(MODELS, "native", ref("kimi-k3.1-pro"))).toBe(true);+ expect(modeAllowsModel(MODELS, "native", ref("boxonly-x"))).toBe(false);+ expect(modeAllowsModel(MODELS, "box", ref("boxonly-x"))).toBe(true);+ expect(modeAllowsModel(MODELS, "box", ref("new-model"))).toBe(true);+ expect(modeAllowsModel(MODELS, "box", ref("kimi-k3.1-pro", "openai"))).toBe(true);+ expect(modeAllowsModel(MODELS, null, ref("kimi-k3.1-pro"))).toBe(true);+ // An older server without modes: everything passes.+ expect(modeAllowsModel([{ ...MODELS[1]!, modes: undefined }], "box", ref("kimi-k3.1-pro"))).toBe(true);+});++test("a refused model moves to the default the mode allows, with the CLI's note", () => {+ expect(modeFallbackModel(MODELS, "box", ref("kimi-k3.1-pro"))).toEqual(ref("gpt-6-astra"));+ expect(modeFallbackModel(MODELS, "box", ref("gpt-6-astra"))).toEqual(ref("gpt-6-astra"));+ expect(modeFallbackModel([MODELS[1]!, MODELS[2]!].map((m) => ({ ...m, default: m.id === "kimi-k3.1-pro" })), "box", ref("kimi-k3.1-pro"))).toEqual(ref("boxonly-x"));+ expect(modeSwitchNote(MODELS, "box", ref("kimi-k3.1-pro"), ref("gpt-6-astra"))).toBe(sharedNote("Kimi K3.1 Pro", "GPT 6 Astra", "box"));+});diff --git a/apps/server/src/model-modes/PARITY.sha256 b/apps/server/src/model-modes/PARITY.sha256new file mode 100644index 00000000..9e1743c2--- /dev/null+++ b/apps/server/src/model-modes/PARITY.sha256@@ -0,0 +1,4 @@+# Per-model modes (native / box) and the box header: the same files in omnirush-cli (src/model-modes/) and omnirush-gui (apps/server/src/model-modes/).+# A change to one must be ported to the other and this list updated in both.+a3b32b5e5abff4fb47c2a4c49dea6059cf9c626e39777a8ee98eab94096f833c model-modes.mjs+84254f298a54a1e248791f26c22eef5cf975730401fa7ef2838f21c6ccbc0f25 model-modes.d.mtsdiff --git a/apps/server/src/model-modes/model-modes.d.mts b/apps/server/src/model-modes/model-modes.d.mtsnew file mode 100644index 00000000..fa449841--- /dev/null+++ b/apps/server/src/model-modes/model-modes.d.mts@@ -0,0 +1,17 @@+// Types of model-modes.mjs (the same pair of files in omnirush-cli+// src/model-modes/ and omnirush-gui apps/server/src/model-modes/; PARITY.sha256).++export type ModelMode = "native" | "box";+export type ModeCatalogEntry = { id: string; default?: boolean; display_name?: string; name?: string; modes?: unknown };++export const MODES: readonly ModelMode[];+export const BOX_HEADER: "x-omnirush-box";+export const MODEL_NOT_IN_MODE: "model_not_in_mode";++export function projectMode(box: unknown): ModelMode;+export function modelModes(entry: unknown): ModelMode[];+export function modelAllowedIn(entry: unknown, mode: ModelMode): boolean;+export function modelForMode<T extends ModeCatalogEntry>(catalog: readonly T[] | null | undefined, mode: ModelMode, wanted?: string | null): { model: string | null; models: T[]; note: string | null };+export function modeSwitchNote(from: string, to: string, mode: ModelMode): string;+export function notInModeMessage(mode?: ModelMode | null): string;+export function modeHeaders(box: unknown): Record<string, string>;diff --git a/apps/server/src/model-modes/model-modes.mjs b/apps/server/src/model-modes/model-modes.mjsnew file mode 100644index 00000000..2c957b15--- /dev/null+++ b/apps/server/src/model-modes/model-modes.mjs@@ -0,0 +1,77 @@+// Per-model modes: which projects a model may run in.+//+// GET /omnirush/v1/models may give a model `modes`: ["native"] (projects+// without the sandbox) or ["box"] (sandbox projects). No `modes` (or one that+// is not a list) means both, so every older catalog is unchanged. A list+// names exactly the modes it allows: ["native"] is native only, [] none.+//+// Every gateway request from a box project carries `X-OmniRush-Box: 1`; one+// without it counts as native. The gateway refuses a model outside the+// request's mode with 400 `model_not_in_mode`, before anything is reserved.+//+// The same file ships in omnirush-cli (src/model-modes/) and omnirush-gui+// (apps/server/src/model-modes/); PARITY.sha256 lists it in both. No imports,+// so the desktop's bundler and the CLI's plain node both load it as is.++export const MODES = Object.freeze(["native", "box"]);+export const BOX_HEADER = "x-omnirush-box";+export const MODEL_NOT_IN_MODE = "model_not_in_mode";++/** "box" for a sandbox project, else "native". */+export function projectMode(box) {+ return box ? "box" : "native";+}++/** The modes a catalog entry allows: its `modes` known values, or both when it has no list. */+export function modelModes(entry) {+ const raw = entry && typeof entry === "object" ? entry.modes : undefined;+ if (!Array.isArray(raw)) return [...MODES];+ return MODES.filter((mode) => raw.includes(mode));+}++/** Whether a catalog entry may run in `mode`. */+export function modelAllowedIn(entry, mode) {+ return modelModes(entry).includes(mode);+}++/**+ * The model a project in `mode` starts on: `wanted` (a catalog id) when the+ * catalog allows it there, else the catalog's default among the allowed ones,+ * else the first allowed. `note` says what changed when `wanted` was refused.+ * With no allowed model at all, `model` is null (the gateway would refuse+ * every one) and `models` is empty.+ */+export function modelForMode(catalog, mode, wanted = null) {+ const list = Array.isArray(catalog) ? catalog : [];+ const models = list.filter((entry) => modelAllowedIn(entry, mode));+ const asked = wanted ? list.find((entry) => entry.id === wanted) : null;+ if (asked && models.includes(asked)) return { model: asked.id, models, note: null };+ const fallback = models.find((entry) => entry.default === true) ?? models[0] ?? null;+ const note = asked && fallback ? modeSwitchNote(nameOf(asked), nameOf(fallback), mode) : asked ? notInModeMessage(mode) : null;+ return { model: fallback ? fallback.id : null, models, note };+}++function nameOf(entry) {+ const name = entry.display_name ?? entry.name;+ return typeof name === "string" && name.trim() ? name.trim() : entry.id;+}++function where(mode) {+ return mode === "box" ? "in sandbox projects" : "outside the sandbox";+}++/** "Kimi K3.1 Pro isn't available in sandbox projects. Using GPT 6 Astra here." */+export function modeSwitchNote(from, to, mode) {+ return `${from} isn't available ${where(mode)}. Using ${to} here.`;+}++/** The plain message for a `model_not_in_mode` refusal. */+export function notInModeMessage(mode = null) {+ const scope = mode === "box" || mode === "native" ? where(mode) : "in this project";+ return `This model isn't available ${scope}. Pick another model and send again.`;+}++/** The request headers for a project: `{ "x-omnirush-box": "1" }` in a box project, else none. */+export function modeHeaders(box) {+ return box ? { [BOX_HEADER]: "1" } : {};+}diff --git a/apps/server/src/model-modes/model-modes.test.ts b/apps/server/src/model-modes/model-modes.test.tsnew file mode 100644index 00000000..1d3b4f43--- /dev/null+++ b/apps/server/src/model-modes/model-modes.test.ts@@ -0,0 +1,112 @@+// Per-model modes on the desktop: the shared rules (PARITY.sha256, the same+// files in omnirush-cli src/model-modes/), the catalog keeping `modes`, and+// the gateway broker's X-OmniRush-Box, local refusal, plain message and+// 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 path from "node:path";++import { OmniRushGatewayBroker } from "../omnirush-gateway-broker.js";+import { sanitizeOmniRushModelCatalog } from "../omnirush-model-catalog.js";+import { modelAllowedIn, modelForMode, modelModes, notInModeMessage, projectMode } from "./model-modes.mjs";++const CATALOG = sanitizeOmniRushModelCatalog({+ data: [+ { id: "gpt-6-astra", display_name: "GPT 6 Astra", default: true },+ { id: "kimi-k3.1-pro", display_name: "Kimi K3.1 Pro", family: "Kimi", modes: ["native"] },+ ],+})!;+const REFUSAL = { detail: "model_not_in_mode", error: { type: "invalid_request_error", code: "model_not_in_mode", message: "This model isn't available in sandbox projects.", omnirush: { code: "model_not_in_mode", retry: false } } };++type Call = { model: unknown; box: string | null; path: string };++function broker(calls: Call[], answer: (call: Call) => Response = () => Response.json({ output: [] }), modeCheck = true) {+ return new OmniRushGatewayBroker({+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },+ engineToken: "local-engine-token",+ ...(modeCheck ? { modelInMode: (model: string, box: boolean) => { const entry = CATALOG.find((m) => m.id === model); return !entry || modelAllowedIn(entry, projectMode(box)); } } : {}),+ subagentRetryDelayMs: 1,+ fetch: async (input, init) => {+ const raw = init?.body;+ const text = typeof raw === "string" ? raw : new TextDecoder().decode(raw as ArrayBuffer);+ const call = { model: JSON.parse(text).model, box: new Headers(init?.headers).get("x-omnirush-box"), path: new URL(String(input)).pathname };+ calls.push(call);+ return answer(call);+ },+ });+}++const request = (body: Record<string, unknown>, headers: Record<string, string> = {}, route = "responses") => new Request(`http://127.0.0.1/omnirush-gateway/v1/${route}`, {+ method: "POST",+ headers: { Authorization: "Bearer local-engine-token", "Content-Type": "application/json", ...headers },+ body: JSON.stringify(body),+});++describe("model modes", () => {+ test("CLI/desktop parity: model-modes/PARITY.sha256 matches its files", () => {+ const rows = readFileSync(path.join(import.meta.dir, "PARITY.sha256"), "utf8").split("\n").filter((line) => line && !line.startsWith("#"));+ expect(rows.map((row) => row.split(/\s+/)[1])).toEqual(["model-modes.mjs", "model-modes.d.mts"]);+ for (const row of rows) {+ const [hash, name] = row.split(/\s+/);+ const actual = createHash("sha256").update(readFileSync(path.join(import.meta.dir, name ?? ""))).digest("hex");+ expect(`${name} ${actual}`).toBe(`${name} ${hash}`);+ }+ });++ test("rules: absent means both; a list names what it allows; a refused pick moves to the allowed default", () => {+ expect(modelModes({ id: "a" })).toEqual(["native", "box"]);+ expect(modelModes({ id: "a", modes: ["native"] })).toEqual(["native"]);+ expect(modelForMode(CATALOG, "box", "kimi-k3.1-pro")).toMatchObject({ model: "gpt-6-astra", note: "Kimi K3.1 Pro isn't available in sandbox projects. Using GPT 6 Astra here." });+ expect(modelForMode(CATALOG, "native", "kimi-k3.1-pro")).toMatchObject({ model: "kimi-k3.1-pro", note: null });+ });++ test("the catalog keeps modes only when the backend lists them", () => {+ expect("modes" in CATALOG[0]!).toBe(false);+ expect(CATALOG[1]!.modes).toEqual(["native"]);+ });++ test("broker: a box chat's requests say so, its later unmarked ones too; native ones never", async () => {+ const calls: Call[] = [];+ const b = broker(calls);+ await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_box", "x-omnirush-box": "1" }), "responses");+ // The same chat's compaction without the mark (and a sub-agent naming it as its main chat).+ await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_box" }, "responses/compact"), "responses/compact");+ await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_child", "x-parent-session-id": "ses_box" }), "responses");+ await b.handle(request({ model: "gpt-6-astra" }, { "x-omnirush-session-id": "ses_native" }), "responses");+ expect(calls.map((c) => [c.path, c.box])).toEqual([+ ["/omnirush/v1/responses", "1"],+ ["/omnirush/v1/responses/compact", "1"],+ ["/omnirush/v1/responses", "1"],+ ["/omnirush/v1/responses", null],+ ]);+ });++ test("broker: a model outside the chat's mode is refused here, never sent, in plain words", async () => {+ const calls: Call[] = [];+ const response = await broker(calls).handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-session-id": "ses_box", "x-omnirush-box": "1" }), "responses");+ expect(calls).toEqual([]);+ expect(response.status).toBe(400);+ const body = await response.json() as { error: { code: string; message: string } };+ expect(body.error.code).toBe("model_not_in_mode");+ expect(body.error.message).toBe(notInModeMessage("box"));+ // Native: Kimi goes out.+ expect((await broker(calls).handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-session-id": "ses_n" }), "responses")).status).toBe(200);+ });++ test("broker: the gateway's model_not_in_mode keeps its words", async () => {+ const calls: Call[] = [];+ const response = await broker(calls, () => Response.json(REFUSAL, { status: 400, headers: { "x-should-retry": "false" } }), false)+ .handle(request({ model: "kimi-k3.1-pro" }, { "x-omnirush-box": "1" }), "responses");+ expect(response.status).toBe(400);+ expect(((await response.json()) as { error: { message: string } }).error.message).toBe("This model isn't available in sandbox projects.");+ });++ test("broker: a sub-agent's picked model refused for the mode runs on the main 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"]);+ });+});diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.tsindex deb5921a..5e38aa9f 100644--- a/apps/server/src/omnirush-gateway-broker.ts+++ b/apps/server/src/omnirush-gateway-broker.ts@@ -14,6 +14,7 @@ import {import { builtinOmniRushModelCatalog, omnirushModelWantsReasoningSummary } from "./omnirush-model-catalog.js";import { 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";/** A refused attachment is left out and the model request sent again, at most this often per request. */const MAX_ATTACHMENT_RETRIES = 2;@@ -65,6 +66,12 @@ type BrokerOptions = {sessionUploadBudget?: SessionUploadBudget;/** Hears every sub-agent request moved to the main model (see subagentFallback). */onSubagentFallback?: (event: SubagentModelFallbackEvent) => void;+ /**+ * Whether a catalog model may run in a box (sandbox) chat or a native one+ * (model-modes/). False: the request gets a 400 model_not_in_mode here and+ * is never sent. Unset: every model goes to the gateway, which judges.+ */+ modelInMode?: (model: string, box: boolean) => boolean | Promise<boolean>;/** The pause before a busy picked model is tried once more; tests shrink it. */subagentRetryDelayMs?: number;/** Whether a picked sub-agent model is in its refusal cooldown (sent straight to the main model). */@@ -201,6 +208,8 @@ const GATEWAY_ERROR_COPY: Record<string, string> = {transcription_unavailable: "the transcription service did not answer. Try again in a moment.",audio_unreadable: "the recording could not be read. Try again.",update_required: "this version of OmniRush.ai is no longer supported. Update the app to keep using models; your sessions are kept.",+ // A model outside the project's mode (model-modes/): the gateway's refusal, or the broker's own before sending.+ [MODEL_NOT_IN_MODE]: notInModeMessage().replace(/^T/, "t"),};/** Readable copy for a gateway error code, or null for a code it does not know. */@@ -340,7 +349,7 @@ function carriedFields(payload: Record<string, unknown>, nested: Record<string,* check. Inside a stream (after any output) the engine takes an error frame* for a passing fault and sends the request again, up to five times.*/-const FINAL_STREAM_CODES = new Set(["request_policy_refused", "session_quality_cap", "daily_grant_exhausted", "cyber_policy"]);+const FINAL_STREAM_CODES = new Set(["request_policy_refused", "session_quality_cap", "daily_grant_exhausted", "cyber_policy", MODEL_NOT_IN_MODE]);/*** A final refusal inside a stream as the frame the engine shows once and@@ -405,7 +414,8 @@ function errorEvent(block: Uint8Array, payload: Record<string, unknown>): Uint8Aconst omnirush = omnirushField(nested);if (baseCode && TURN_ENDING_STREAM_CODES.has(baseCode)) return turnEndingEvent(baseCode, payload.sequence_number, omnirush);const original = stringField(nested ? nested.message : payload.message);- const readable = code ? gatewayErrorMessage(code, original) : null;+ // model_not_in_mode names the kind of project the model runs in: the gateway's words.+ const readable = baseCode === MODEL_NOT_IN_MODE && original ? original : code ? gatewayErrorMessage(code, original) : null;const detail = stringField(payload.detail);if (retryRefused(nested) || (detail && FINAL_STREAM_CODES.has(detail)) || (baseCode && FINAL_STREAM_CODES.has(baseCode))) {const named = detail ?? baseCode ?? "omnirush_refused";@@ -666,8 +676,8 @@ function readableErrorBody(text: string, grant: string | null): { error: Record<const code = bareCode ?? stringField(nested?.code);if (!code) return null;const message = (grant && code === "daily_grant_exhausted" ? `omnirush.ai: ${grant.charAt(0).toLowerCase()}${grant.slice(1)}` : null)- // The server names the version to install; keep its words.- ?? (code === UPDATE_REQUIRED_CODE ? stringField(nested?.message) ?? stringField(payload.message) : null)+ // The server names the version to install, or the kind of project the model runs in; keep its words.+ ?? (code === UPDATE_REQUIRED_CODE || code === MODEL_NOT_IN_MODE ? stringField(nested?.message) ?? stringField(payload.message) : null)?? gatewayErrorMessage(code, stringField(nested?.message))?? (bareCode ? `omnirush.ai could not complete the model request (${code}).` : null);if (!message) return null;@@ -1158,6 +1168,8 @@ async function refusalCode(response: Response): Promise<{ code: string | null; fasync function subagentRefusal(response: Response): Promise<SubagentRefusal> {const { code, final } = await refusalCode(response);const reason = code ?? `http_${response.status}`;+ // Final for this model only: outside the chat's mode, the main model may still run here.+ if (code === MODEL_NOT_IN_MODE) return { move: "now", reason };// Marked final by the gateway: not sent again, on this model or another.if (final || (code && ACCOUNT_REFUSED.has(code))) return { move: "never", reason };if (code && SUBAGENT_MODEL_REFUSED.has(code)) return { move: "now", reason };@@ -1233,6 +1245,13 @@ export class OmniRushGatewayBroker {private readonly onSubagentFallback?: BrokerOptions["onSubagentFallback"];private readonly subagentRetryDelayMs: number;private readonly subagentModelRefused?: (model: string) => boolean;+ private readonly modelInMode?: BrokerOptions["modelInMode"];+ /**+ * Chats seen with X-OmniRush-Box (the box engine plugin marks a box chat's+ * requests): a later request of one without the mark (a compaction, a side+ * question) still goes out as a box request. Oldest first, bounded.+ */+ private readonly boxSessions = new Set<string>();private readonly onUpdateSignal?: (signal: GatewayUpdateSignal) => void;private readonly reasoningSummaryFor: (model: string) => boolean | Promise<boolean>;private readonly clientHeader: string | null;@@ -1273,6 +1292,7 @@ export class OmniRushGatewayBroker {this.onSubagentFallback = options.onSubagentFallback;this.subagentRetryDelayMs = options.subagentRetryDelayMs ?? SUBAGENT_RETRY_DELAY_MS;this.subagentModelRefused = options.subagentModelRefused;+ this.modelInMode = options.modelInMode;this.onUpdateSignal = options.onUpdateSignal;this.reasoningSummaryFor = options.reasoningSummaryFor ?? defaultReasoningSummaryFor;}@@ -1905,6 +1925,15 @@ export class OmniRushGatewayBroker {accessToken: string | undefined,requestId: string,): Promise<Response> {+ // 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);+ if (model && !(await this.modelInMode(model, this.boxRequest(request)))) {+ this.log?.("info", "omnirush.ai model not available in this chat's mode; not sent", { model, box: this.boxRequest(request), clientRequestId: requestId });+ const message = notInModeMessage(projectMode(this.boxRequest(request)));+ return Response.json({ detail: MODEL_NOT_IN_MODE, error: { type: "invalid_request_error", code: MODEL_NOT_IN_MODE, message } }, { status: 400, headers: { "x-should-retry": "false" } });+ }+ }const forwardedBody = path === "responses" && body !== undefined? await withReasoningSummary(body, this.reasoningSummaryFor): body;@@ -1951,6 +1980,8 @@ export class OmniRushGatewayBroker {const value = request.headers.get(name);if (value) headers.set(name, value);}+ // A box (sandbox) chat's request: the gateway judges its model in the box mode.+ if (this.boxRequest(request)) headers.set(BOX_HEADER, "1");return this.fetcher(upstreamUrl(this.state.gatewayUrl, path), {method: request.method,headers,@@ -1959,6 +1990,20 @@ export class OmniRushGatewayBroker {});}+ /** 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));+ if (request.headers.get(BOX_HEADER) === "1") {+ for (const id of sessions) {+ this.boxSessions.delete(id);+ this.boxSessions.add(id);+ }+ while (this.boxSessions.size > 1024) this.boxSessions.delete(this.boxSessions.values().next().value!);+ return true;+ }+ return sessions.some((id) => this.boxSessions.has(id));+ }+private refresh(expectedAccessToken: string): Promise<boolean> {if (this.state?.accessToken !== expectedAccessToken) return Promise.resolve(true);this.refreshInFlight ??= (this.refreshOwnerdiff --git a/apps/server/src/omnirush-model-catalog.ts b/apps/server/src/omnirush-model-catalog.tsindex 701ef393..f4ed14a9 100644--- a/apps/server/src/omnirush-model-catalog.ts+++ b/apps/server/src/omnirush-model-catalog.ts@@ -21,6 +21,7 @@ import { runtimeStorageDir } from "./runtime-db.js";import type { ServerConfig } from "./types.js";import { writeFileAtomic } from "./atomic-write.js";import { modelHasWebSearch, type WebSearchSwitch } from "./web-search.js";+import { modelModes, type ModelMode } from "./model-modes/model-modes.mjs";/** Every effort a catalog model may list, lowest first. */export const OMNIRUSH_MODEL_EFFORTS = ["none", "minimal", "low", "medium", "high", "xhigh", "max"] as const;@@ -49,6 +50,8 @@ export type OmniRushCatalogModel = {image_input: boolean;file_input: boolean;};+ /** Where the model may run (model-modes/); absent means both. Kept only when the backend lists it. */+ modes?: ModelMode[];};/** Models in the backend's order, default first. Never empty. */@@ -156,6 +159,7 @@ function sanitizeModel(raw: unknown): OmniRushCatalogModel | null {image_input: flag("image_input"),file_input: flag("file_input"),},+ ...(Array.isArray(raw.modes) ? { modes: modelModes(raw) } : {}),};}diff --git a/apps/server/src/opencode-plugins/omnirush-box.ts b/apps/server/src/opencode-plugins/omnirush-box.tsindex 820ba138..c1faa06c 100644--- a/apps/server/src/opencode-plugins/omnirush-box.ts+++ b/apps/server/src/opencode-plugins/omnirush-box.ts@@ -23,6 +23,7 @@import path from "node:path";+import { BOX_HEADER } from "../model-modes/model-modes.mjs";import { BOX_NOTE, desktopProjects, engineDirs, FILE_TOOLS, keyOf, record, toolPaths, within } from "../box/plugin-lib.js";/** The engine client's one call used here: a chat's parent (a sub-agent's chat has one). */@@ -68,6 +69,10 @@ export const OmniRushBox = async ({ directory, client }: { directory?: string; cif (input.callID) output.env.OMNIRUSH_CALL = String(input.callID);if (input.sessionID) output.env.OMNIRUSH_SESSION = String(input.sessionID);},+ // A box chat's model requests say so (X-OmniRush-Box): the gateway judges their model in the box mode (model-modes/).+ "chat.headers": async (input: { sessionID?: string }, output: { headers?: Record<string, string> }) => {+ if (output?.headers && (await active(input?.sessionID))) output.headers[BOX_HEADER] = "1";+ },"tool.execute.before": async (input: { tool: string; sessionID?: string }, output: { args?: Record<string, unknown> }) => {if (!FILE_TOOLS.has(input.tool) || !(await active(input.sessionID))) return;for (const raw of toolPaths(input.tool, output.args ?? {})) {diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 4bbca5cb..98e1c36d 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -209,6 +209,7 @@ import { omnirushMcpPolicyPluginPath } from "./omnirush-extensions-plugin-path.jimport { isOmniRushUiMcpRegistryEntry } from "./omnirush-ui-mcp-command.js";import { describeIgnored, type McpAllowEntry } from "./mcp-allow/mcp-allow.mjs";import { omnirushModelWantsReasoningSummary, readOmniRushModelCatalog } from "./omnirush-model-catalog.js";+import { modelAllowedIn, modelModes, projectMode } from "./model-modes/model-modes.mjs";import {SUBAGENT_MODEL_FALLBACK_TRACE,readSubagentModelSetting,@@ -1274,6 +1275,12 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {// Reasoning summaries for every reasoning model the account's catalog// lists on the Codex route (not a hardcoded pair of ids).reasoningSummaryFor: async (model) => omnirushModelWantsReasoningSummary(await readOmniRushModelCatalog(config), model),+ // A catalog model outside the chat's mode (box or native, model-modes/) is refused before it is sent;+ // a model the catalog does not list goes to the gateway, which judges.+ modelInMode: async (model, box) => {+ const entry = (await readOmniRushModelCatalog(config)).find((candidate) => candidate.id === model);+ return !entry || modelAllowedIn(entry, projectMode(box));+ },// 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 } : {}),@@ -4548,6 +4555,8 @@ function createRoutes(family: model.family,default: model.default,efforts: model.reasoning_levels,+ // Where the model may run: "native" (projects without the sandbox), "box" (sandbox projects).+ modes: modelModes(model),})),});});