A desktop coding agent with free access to frontier models.
TypeScript
fix: requests wait for the new engine and it gets catalog changes
ogsamrat7bdde15120d2
diff --git a/apps/server/src/engine-v2-preview.test.ts b/apps/server/src/engine-v2-preview.test.tsindex 89f137b3..8c309721 100644--- a/apps/server/src/engine-v2-preview.test.ts+++ b/apps/server/src/engine-v2-preview.test.ts@@ -1,10 +1,11 @@import { expect, test } from "bun:test";import { existsSync } from "node:fs";-import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import {+ awaitEngineConnection,createEngineV2Preview,mapRuntimeProvidersToV2Specs,readEngineV2PreviewState,@@ -12,6 +13,9 @@ import {writeEngineV2PreviewState,} from "./engine-v2-preview.js";import type { ServerConfig } from "./types.js";+import { engineV2Root } from "./engine-v2-migration.js";+import { builtinOmniRushModelCatalog, writeOmniRushModelCatalog } from "./omnirush-model-catalog.js";+import { runtimeStorageDir } from "./runtime-db.js";import { buildOmniRushV2Instructions, waitForOmniRushV2Skills } from "./opencode-v2-instructions.js";const realV2Binary = process.env.OMNIRUSH_OPENCODE2_BIN?.trim()@@ -269,3 +273,70 @@ test("null or empty native endpoint overrides cannot bypass catalog origin validexpect(result.specs).toEqual([]);}});++test("a request sent while the engine starts gets its connection once the engine is up", async () => {+ let up: { url: string } | undefined;+ let finish!: () => void;+ const starting = new Promise<void>((resolveStart) => { finish = () => { up = { url: "http://127.0.0.1:4097" }; resolveStart(); }; });+ setTimeout(finish, 150);+ const began = Date.now();+ const connection = await awaitEngineConnection({ connection: () => up, starting: () => (up ? undefined : starting), failed: () => false }, 5_000);+ expect(connection).toEqual({ url: "http://127.0.0.1:4097" });+ expect(Date.now() - began).toBeLessThan(2_000);+ // Before the start began (the server starts the engine after its routes): polled until it is up.+ let later: { url: string } | undefined;+ setTimeout(() => { later = { url: "http://127.0.0.1:4098" }; }, 120);+ expect(await awaitEngineConnection({ connection: () => later, starting: () => undefined, failed: () => false }, 5_000, 20)).toEqual({ url: "http://127.0.0.1:4098" });+});++test("an engine that never starts, or whose start failed, gives no connection within the bound", async () => {+ const began = Date.now();+ expect(await awaitEngineConnection({ connection: () => undefined, starting: () => new Promise<never>(() => undefined), failed: () => false }, 300, 50)).toBeNull();+ const waited = Date.now() - began;+ expect(waited).toBeGreaterThanOrEqual(250);+ expect(waited).toBeLessThan(1_500);+ const failedAt = Date.now();+ expect(await awaitEngineConnection({ connection: () => undefined, starting: () => undefined, failed: () => true }, 5_000)).toBeNull();+ expect(Date.now() - failedAt).toBeLessThan(500);+});++realV2Test("a request during start waits for the engine, and a catalog change brings its models to the running engine", async () => {+ const root = await mkdtemp(join(tmpdir(), "omnirush-engine-v2-catalog-"));+ const previousBinary = process.env.OMNIRUSH_OPENCODE2_BIN;+ const config: ServerConfig = {+ ...testConfig(root),+ port: 9,+ omnirushGatewayCredentials: { gatewayUrl: "https://gateway.invalid", accessToken: "a", refreshToken: "r" },+ omnirushEngineToken: "engine-token",+ };+ await writeEngineV2PreviewState(config, { enabled: true, chatRouting: false });+ process.env.OMNIRUSH_OPENCODE2_BIN = realV2Binary;+ const preview = createEngineV2Preview({ config, deferStart: true });+ const engineModels = async () => {+ const written = JSON.parse(await readFile(join(engineV2Root(runtimeStorageDir(config)), "config", "opencode.json"), "utf8"));+ return Object.keys(written.providers?.omnirush?.models ?? {}).sort();+ };+ try {+ preview.start();+ // Sent while it starts: answered once the engine is up, not refused.+ const connection = await preview.awaitConnection(60_000);+ expect(connection?.url).toMatch(/^http:\/\/127\.0\.0\.1:/);+ // Before the first catalog sync: the built-in catalog.+ expect(await engineModels()).toEqual(["gpt-6-astra", "gpt-6-sol", "gpt-6.1-sol"]);+ const synced = builtinOmniRushModelCatalog();+ synced.push({ ...synced[0]!, id: "kimi-k3.1-pro", display_name: "Kimi K3.1 Pro", family: "Kimi", default: false });+ await writeOmniRushModelCatalog(config, synced);+ await preview.refreshProviders();+ expect(await engineModels()).toEqual(["gpt-6-astra", "gpt-6-sol", "gpt-6.1-sol", "kimi-k3.1-pro"]);+ // Under the gateway provider (our broker's address), the only one the engine may use.+ const written = JSON.parse(await readFile(join(engineV2Root(runtimeStorageDir(config)), "config", "opencode.json"), "utf8"));+ expect(Object.keys(written.providers)).toEqual(["omnirush"]);+ expect(written.providers.omnirush.settings.baseURL).toBe("http://127.0.0.1:9/omnirush-gateway/v1");+ expect(preview.status().catalogModelIds).toContain("kimi-k3.1-pro");+ } finally {+ await preview.stop();+ if (previousBinary === undefined) delete process.env.OMNIRUSH_OPENCODE2_BIN;+ else process.env.OMNIRUSH_OPENCODE2_BIN = previousBinary;+ await rm(root, { recursive: true, force: true });+ }+}, 120_000);diff --git a/apps/server/src/engine-v2-preview.ts b/apps/server/src/engine-v2-preview.tsindex 39da789b..f71dc126 100644--- a/apps/server/src/engine-v2-preview.ts+++ b/apps/server/src/engine-v2-preview.ts@@ -71,6 +71,10 @@ export interface EngineV2Preview {setEnabled(enabled: boolean): Promise<EngineV2PreviewStatus>;setChatRouting(chatRouting: boolean): Promise<EngineV2PreviewStatus>;connection(): { url: string; username: string; password: string } | undefined;+ /** The connection once the engine is up, waiting while it starts (at most `timeoutMs`); null when it is not up by then or its start failed. */+ awaitConnection(timeoutMs: number): Promise<{ url: string; username: string; password: string } | null>;+ /** The account's providers and model catalog again (a catalog change); resolves once the engine has them. */+ refreshProviders(): Promise<void>;ensureWorkspaceReady(directory: string): Promise<void>;syncWorkspaceMcp(workspaceId: string, directory: string): Promise<void>;/** Whether a chat came from the 1.x store (engine-v2-migration.ts). */@@ -279,6 +283,32 @@ export async function assertSessionPluginActive(}}+/**+ * Waits for `probe.connection()` while the engine starts: re-checked when its start settles and every+ * `pollMs`, `timeoutMs` at most. Null when the time ran out, or no start is under way and the last one failed.+ */+export async function awaitEngineConnection<T>(+ probe: { connection(): T | undefined; starting(): Promise<unknown> | undefined; failed(): boolean },+ timeoutMs: number,+ pollMs = 100,+): Promise<T | null> {+ const deadline = Date.now() + timeoutMs;+ for (;;) {+ const ready = probe.connection();+ if (ready) return ready;+ const left = deadline - Date.now();+ if (left <= 0) return null;+ const starting = probe.starting();+ if (!starting && probe.failed()) return null;+ let timer: ReturnType<typeof setTimeout> | undefined;+ await Promise.race([+ ...(starting ? [starting.then(() => undefined, () => undefined)] : []),+ new Promise<void>((resolveTick) => { timer = setTimeout(resolveTick, Math.min(pollMs, left)); }),+ ]);+ clearTimeout(timer);+ }+}+export function createEngineV2Preview(options: { config: ServerConfig; env?: Pick<EnvService, "list" | "onChange">; deferStart?: boolean }): EngineV2Preview {const { config } = options;const rootDir = engineV2Root(runtimeStorageDir(config));@@ -634,9 +664,24 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Picawait stopRuntime();}+ function awaitConnection(timeoutMs: number) {+ return awaitEngineConnection({+ connection,+ starting: () => startPromise,+ // Turned off, stopped, or its last start failed (recordStartError): nothing to wait for.+ failed: () => !enabled || !allowRunning || (!running && lastError !== undefined),+ }, timeoutMs);+ }++ async function refreshProviders(): Promise<void> {+ if (!sidecar) return;+ scheduleMirror();+ if (mirrorInFlight) await mirrorInFlight;+ }+function startWhenReady(): void {if (enabled) void start().catch(recordStartError);}if (!options.deferStart) startWhenReady();- return { start: startWhenReady, status, setEnabled, setChatRouting, connection, ensureWorkspaceReady, syncWorkspaceMcp, migratedFromV1: (id) => migrated.has(id), stop };+ return { start: startWhenReady, status, setEnabled, setChatRouting, connection, awaitConnection, refreshProviders, ensureWorkspaceReady, syncWorkspaceMcp, migratedFromV1: (id) => migrated.has(id), stop };}diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 29c1d018..38d067ad 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -306,6 +306,8 @@ async function capturePermissionMode(config: ServerConfig): Promise<ApprovalMode/** Automatic compaction (session-archive/auto-compact.ts), one per server: its counts live as long as the app. */const autoCompactorsByServer = new WeakMap<ServerConfig, AutoCompactor>();+/** How long a request waits for the 2.x engine while it starts (its boot timeout, managed-opencode-v2.ts). */+const ENGINE_V2_START_WAIT_MS = 60_000;const engineV2ByServer = new WeakMap<ServerConfig, EngineV2Preview>();const editorLspByServer = new WeakMap<ServerConfig, ReturnType<typeof createEditorLsp>>();function editorLspFor(config: ServerConfig): ReturnType<typeof createEditorLsp> {@@ -1724,9 +1726,12 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {await managedDesktopPolicy(config).assertRequest(request, mount.restPath, true);await assertProxiedEngineRequestWithinProviderLock(request, mount.restPath);const workspace = await resolveWorkspaceWithoutBootstrap(config, mount.workspaceId);- const connection = engineV2Preview.connection();+ // Right after the app starts the engine may still be starting: the request waits for it (bounded).+ const connection = await engineV2Preview.awaitConnection(ENGINE_V2_START_WAIT_MS);if (!connection) {- return finalize(jsonResponse({ error: "engine_v2_preview_not_running" }, 503));+ const notRunning = jsonResponse({ error: "engine_v2_preview_not_running", retryable: true, message: "The engine is not running yet. Try again in a moment." }, 503);+ notRunning.headers.set("Retry-After", "5");+ return finalize(notRunning);}assertEngineStartedWithinLock(connection.url, request.method, mount.restPath);proxyService = "opencode";@@ -1997,6 +2002,8 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {config,broker: gatewayBroker,reloadEngine: async () => {+ // The 2.x engine runs every chat: it gets the new models from its providers (engine-v2-preview.ts mirror).+ await engineV2Preview.refreshProviders();if (config.workspaces.length === 0) return;await reloadOpencodeEngine(config, resolveEngineRuntimeWorkspace(config), engineMcpServerState, {forceStandby: true,