A desktop coding agent with free access to frontier models.
TypeScript
feat: the org's cloud connector on the new engine
ogsamratb5375931b9f6
diff --git a/apps/server/src/engine-v2-mcp.ts b/apps/server/src/engine-v2-mcp.tsindex f2c6b984..74bafb95 100644--- a/apps/server/src/engine-v2-mcp.ts+++ b/apps/server/src/engine-v2-mcp.ts@@ -20,12 +20,32 @@ function isRecord(value: unknown): value is Record<string, unknown> {return typeof value === "object" && value !== null && !Array.isArray(value);}+/** The org's cloud connector (cloud-mcp-health.ts): a remote server the app configures, never a project. */+export const CLOUD_MCP_NAME = "omnirush-cloud";++/**+ * The org's cloud connector as the app persisted it (validated and scope-checked when it was set up), or null+ * when it is absent, turned off, or not a plain https (or loopback) remote server.+ */+export function cloudMcpEntry(runtimeMcp: Record<string, unknown>): Record<string, unknown> | null {+ const entry = runtimeMcp[CLOUD_MCP_NAME];+ if (!isRecord(entry) || entry.type !== "remote" || entry.enabled === false || typeof entry.url !== "string") return null;+ try {+ const url = new URL(entry.url);+ const loopback = ["127.0.0.1", "localhost", "[::1]"].includes(url.hostname);+ if (url.protocol !== "https:" && !(loopback && url.protocol === "http:")) return null;+ } catch {+ return null;+ }+ return entry;+}+/** The names the engine's gate allows (fixed for the engine's lifetime). */-export function v2McpGate(stateDir: string, env: Record<string, string | undefined> = process.env): string[] {+export function v2McpGate(stateDir: string, env: Record<string, string | undefined> = process.env, cloud = false): string[] {const policy = currentMcpPolicy(stateDir);const ids = policy?.enabled ? policy.allow.map((entry) => entry.id) : [];const computerUse = computerUseBuiltins(computerUseDir(stateDir), env)[SERVER_ID] ? [SERVER_ID] : [];- return [...new Set([...ids, ...computerUse])].filter((name) => GATE_NAME.test(name)).sort();+ return [...new Set([...ids, ...computerUse, ...(cloud ? [CLOUD_MCP_NAME] : [])])].filter((name) => GATE_NAME.test(name)).sort();}/** One server's 2.x runtime config (the same fields the 1.x engine had), always a direct-tool server. */@@ -49,7 +69,7 @@ export function v2McpConfig(value: Record<string, unknown>, engineEnv: Record<st}/** The servers a project gets on the 2.x engine, by name; fails closed (no servers) on any error. */-export async function desiredV2Mcp(stateDir: string, root: string, engineEnv: Record<string, string>, gate: readonly string[]): Promise<Record<string, Record<string, unknown>>> {+export async function desiredV2Mcp(stateDir: string, root: string, engineEnv: Record<string, string>, gate: readonly string[], cloud: Record<string, unknown> | null = null): Promise<Record<string, Record<string, unknown>>> {try {const target: Record<string, unknown> = { mcp: readWorkspaceMcp(mcpWorkspaceDir(stateDir), root) };// The built-in's name belongs to the app.@@ -71,6 +91,15 @@ export async function desiredV2Mcp(stateDir: string, root: string, engineEnv: Reconst config = isRecord(value) && gate.includes(name) ? v2McpConfig(value, engineEnv) : null;if (config) out[name] = config;}+ // The org's cloud connector: its sign-in rides in its own request headers (the engine's environment never+ // holds it); a direct-tool server like the others.+ if (cloud && gate.includes(CLOUD_MCP_NAME)) {+ const headers = isRecord(cloud.headers)+ ? Object.fromEntries(Object.entries(cloud.headers).filter((entry): entry is [string, string] => typeof entry[1] === "string"))+ : {};+ const timeout = typeof cloud.timeout === "number" && cloud.timeout > 0 ? { timeout: { startup: cloud.timeout, catalog: cloud.timeout, execution: cloud.timeout } } : {};+ out[CLOUD_MCP_NAME] = { type: "remote", url: cloud.url, headers, ...(cloud.oauth === false ? { oauth: false } : {}), codemode: false, ...timeout };+ }return out;} catch {return {};diff --git a/apps/server/src/engine-v2-next.test.ts b/apps/server/src/engine-v2-next.test.tsindex 525e9821..bff1f93e 100644--- a/apps/server/src/engine-v2-next.test.ts+++ b/apps/server/src/engine-v2-next.test.ts@@ -206,3 +206,33 @@ z.close()const plain = { text: "no office", files: [{ uri: "data:image/png;base64,AA==", name: "a.png" }] };expect(await convertOfficeAttachments(plain, dir)).toBe(plain);}));++test("the org's cloud connector reaches the 2.x engine only when it is set up and gated", () => withDir(async (dir) => {+ const { cloudMcpEntry, desiredV2Mcp, v2McpGate, CLOUD_MCP_NAME } = await import("./engine-v2-mcp.js");+ const cloud = { type: "remote", url: "https://den.example.com/mcp", headers: { Authorization: "Bearer org-token" }, timeout: 3000 };+ expect(cloudMcpEntry({ [CLOUD_MCP_NAME]: cloud })).toEqual(cloud);+ expect(cloudMcpEntry({ [CLOUD_MCP_NAME]: { ...cloud, enabled: false } })).toBeNull();+ expect(cloudMcpEntry({ [CLOUD_MCP_NAME]: { ...cloud, url: "http://den.example.com/mcp" } })).toBeNull();+ expect(cloudMcpEntry({ [CLOUD_MCP_NAME]: { ...cloud, url: "http://127.0.0.1:8790/mcp" } })).not.toBeNull();+ expect(cloudMcpEntry({})).toBeNull();+ expect(v2McpGate(dir)).not.toContain(CLOUD_MCP_NAME);+ expect(v2McpGate(dir, process.env, true)).toContain(CLOUD_MCP_NAME);+ const root = join(dir, "proj");+ const engineEnv = { OPENCODE_PASSWORD: "pw" };+ expect(await desiredV2Mcp(dir, root, engineEnv, [CLOUD_MCP_NAME], cloud)).toEqual({+ [CLOUD_MCP_NAME]: { type: "remote", url: "https://den.example.com/mcp", headers: { Authorization: "Bearer org-token" }, codemode: false, timeout: { startup: 3000, catalog: 3000, execution: 3000 } },+ });+ expect(await desiredV2Mcp(dir, root, engineEnv, [], cloud)).toEqual({});+}));++test("archived chats are the app's own flag, applied to session reads", () => withDir(async (dir) => {+ const { archivedAt, setArchived, withArchived } = await import("./archived-chats.js");+ expect(archivedAt(dir, "ses_a")).toBeUndefined();+ setArchived(dir, "ses_a", 1_700);+ expect(archivedAt(dir, "ses_a")).toBe(1_700);+ expect(withArchived(dir, { id: "ses_a", time: { created: 1 } })).toEqual({ id: "ses_a", time: { created: 1, archived: 1_700 } });+ // The engine's own value never wins over the app's flag.+ expect(withArchived(dir, { id: "ses_b", time: { created: 1, archived: 5 } })).toEqual({ id: "ses_b", time: { created: 1 } });+ setArchived(dir, "ses_a", null);+ expect(withArchived(dir, { id: "ses_a", time: { created: 1 } })).toEqual({ id: "ses_a", time: { created: 1 } });+}));diff --git a/apps/server/src/engine-v2-preview.ts b/apps/server/src/engine-v2-preview.tsindex 39129590..39da789b 100644--- a/apps/server/src/engine-v2-preview.ts+++ b/apps/server/src/engine-v2-preview.ts@@ -23,7 +23,8 @@ import { isReleaseBuild } from "./release-guard.js";import { webSearchSetting } from "./web-search.js";import { buildOmniRushRuntimeConfigObject } from "./omnirush-runtime-config.js";import { OMNIRUSH_PROVIDER_ID } from "./provider-lock/provider-lock.mjs";-import { desiredV2Mcp, v2McpGate } from "./engine-v2-mcp.js";+import { CLOUD_MCP_NAME, cloudMcpEntry, desiredV2Mcp, v2McpGate } from "./engine-v2-mcp.js";+import { runtimeMcpMap } from "./runtime-opencode-config-store.js";import { engineDataDir } from "./engine-data-home.js";import { computerUseEngineEnv } from "./computer-use/desktop.js";import { engineV2Root, readMigratedSessions, seedV2StoreFromV1 } from "./engine-v2-migration.js";@@ -307,6 +308,29 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Picconst mcpInFlight = new Map<string, Promise<void>>();const mcpWorkspaces = new Map<string, string>();+ /** The org's cloud connector, as the app persisted it (engine-v2-mcp.ts). */+ const cloudConnector = async () => cloudMcpEntry(runtimeMcpMap(await readGlobalRuntimeOpencodeConfig(config).catch(() => ({}))));++ /**+ * The engine's gate is fixed at its start: when the org's cloud connector is turned on or off, the engine+ * starts again with the new gate, once no chat runs in any open folder.+ * shortcut: a busy engine keeps its gate until the next connector change or app start.+ */+ async function regateCloudWhenIdle(): Promise<void> {+ const active = sidecar;+ if (!active || Boolean(await cloudConnector()) === active.mcpGate.includes(CLOUD_MCP_NAME)) return;+ for (const directory of mcpWorkspaces.keys()) {+ const running = await active.fetchJson("/api/session/active", { directory, timeoutMs: 5_000 }).catch(() => null);+ if (!running || !isRecord(running.json) || !isRecord(running.json.data) || Object.keys(running.json.data).length > 0) {+ lastError = "The cloud connector applies once no chat is running.";+ return;+ }+ }+ if (sidecar !== active) return;+ await closeSidecar();+ await start();+ }+async function syncWorkspaceMcp(workspaceId: string, directory: string): Promise<void> {mcpWorkspaces.set(directory, workspaceId);// Serialize each location, then re-read authoritative state. A queued call@@ -318,7 +342,7 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Picif (!active) throw new Error("OmniRush v2 is not running");if (mcpCleaned.has(directory)) return;// The allowed servers (engine-v2-mcp.ts) through the engine's runtime MCP API; every other one goes.- const desired = await desiredV2Mcp(runtimeStorageDir(config), directory, active.env, active.mcpGate);+ const desired = await desiredV2Mcp(runtimeStorageDir(config), directory, active.env, active.mcpGate, await cloudConnector());const current = await active.fetchJson("/api/mcp", { directory, timeoutMs: 10_000 });if (current.status !== 200 || !isRecord(current.json) || !Array.isArray(current.json.data)) {throw new Error("OmniRush v2 MCP status is unavailable");@@ -477,7 +501,7 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Pic},permissions: async () => executionRules((await readGlobalRuntimeOpencodeConfig(config)).managedPolicy?.execution),webSearch: () => webSearchSetting({ env: process.env, stateDir: runtimeStorageDir(config) }).enabled,- mcpGate: v2McpGate(runtimeStorageDir(config), { ...process.env, ...computerUseEngineEnv(runtimeStorageDir(config)) }),+ mcpGate: v2McpGate(runtimeStorageDir(config), { ...process.env, ...computerUseEngineEnv(runtimeStorageDir(config)) }, Boolean(await cloudConnector())),});sidecar = managed;if (!enabled || !allowRunning) {@@ -492,6 +516,7 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Picconst unsubscribeConfig = onRuntimeOpencodeConfigWrite((_writeConfig, workspaceId) => {const global = isEngineGlobalRuntimeConfigId(workspaceId);if (global) scheduleMirror();+ if (global) void regateCloudWhenIdle();// Connections installed through OmniRush.ai also update already-open// locations while a conversation is active. Request admission joins// the same serialized reconciliation rather than racing it.