A desktop coding agent with free access to frontier models.
TypeScript
fix: computer use deny without a chat, cancelled calls and root chats
ogsamrat33d2d6a16024
diff --git a/apps/server/src/computer-use/contract-policy.test.ts b/apps/server/src/computer-use/contract-policy.test.tsindex ff73ea85..3d961663 100644--- a/apps/server/src/computer-use/contract-policy.test.ts+++ b/apps/server/src/computer-use/contract-policy.test.ts@@ -322,6 +322,12 @@ describe("policy: approvals per permission mode", () => {expect(actApproval({ ...plain, mode: "auto", target: { text: "Send" } }, on).ask).toBe(false);});+ test("approval_timeout_seconds is capped at 120 s (inside the engine's call timeout)", () => {+ expect(normalizeLimits({ approval_timeout_seconds: 600 }).approval_timeout_seconds).toBe(120);+ expect(normalizeLimits({ approval_timeout_seconds: 90 }).approval_timeout_seconds).toBe(90);+ expect(normalizeLimits({}).approval_timeout_seconds).toBe(120);+ });+test("Allow for this chat: until the chat ends by default; the server's value is an upper bound", () => {expect(DEFAULT_LIMITS.chat_approval_seconds).toBe(0);expect(normalizeLimits({ chat_approval_seconds: 1800 }).chat_approval_seconds).toBe(1800);diff --git a/apps/server/src/computer-use/portable-host.test.ts b/apps/server/src/computer-use/portable-host.test.tsindex c05f57dd..57c1ea31 100644--- a/apps/server/src/computer-use/portable-host.test.ts+++ b/apps/server/src/computer-use/portable-host.test.ts@@ -328,6 +328,53 @@ describe("portable host: MCP surface and consent", () => {}});+ test("a Deny with no chat named withdraws that app's grants in every chat; a named chat's Deny only its own", async () => {+ const open = { app_id: "/usr/bin/gedit", mode: "control", purpose: "Write" };+ const r = rig("normal");+ let chat: string | null = "ses_A";+ const answers: Array<"chat" | "deny"> = ["chat", "chat", "deny", "chat", "chat"];+ const host = await createPortableHost({ native: r.native, settings: () => r.settings, context: () => ({ ancestors: [], chat }), approve: { open: async (request: OpenRequest) => { r.opens.push(request); return { decision: answers.shift() ?? "deny", window_id: request.window_id }; }, act: async () => ({ allow: true }) } });+ const c = host.connect();+ const cycle = async () => { const x = await call(c, "computer_open_session", open); if (x.state.session_id) await call(c, "computer_close_session", { session_id: x.state.session_id }); return x; };+ await cycle(); chat = "ses_B"; await cycle();+ expect(r.opens.length).toBe(2);+ // Both granted: reused without asking.+ chat = "ses_A"; await cycle(); chat = "ses_B"; await cycle();+ expect(r.opens.length).toBe(2);+ // Several chats run (no chat named): the person denies X. Both grants go.+ chat = null;+ expect((await cycle()).state).toMatchObject({ ok: false, code: "approval_denied" });+ chat = "ses_A"; await cycle(); chat = "ses_B"; await cycle();+ expect(r.opens.length).toBe(5);+ await host.close();+ });++ test("the engine cancelling the call (notifications/cancelled) ends the question: nothing opens, no grant changes, the lease is free", async () => {+ const r = rig("normal");+ const gate: { release?: (v: { decision: "chat" }) => void } = {};+ const host = await createPortableHost({ native: r.native, settings: () => r.settings, context: () => ({ ancestors: [], chat: "ses_1" }), approve: { open: async (request: OpenRequest) => { r.opens.push(request); return await new Promise((resolve) => { gate.release = resolve as never; }); }, act: async () => ({ allow: true }) } });+ const c = host.connect();+ const pending = c.handle({ jsonrpc: "2.0", id: 7, method: "tools/call", params: { name: "computer_open_session", arguments: { app_id: "/usr/bin/gedit", mode: "control", purpose: "Write" } } }) as Promise<{ result: { content: Array<{ text: string }> } }>;+ while (!gate.release) await new Promise((resolve) => setTimeout(resolve, 10));+ // A cancel for another request id changes nothing; the one in flight ends it.+ expect(await c.handle({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: 99 } })).toBeNull();+ expect(await c.handle({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: 7 } })).toBeNull();+ const answer = JSON.parse((await pending).result.content[0]!.text) as Record<string, unknown>;+ expect(answer).toMatchObject({ ok: false, code: "approval_denied" });+ expect(String(answer.message)).toContain("cancelled");+ // The person's late Allow opens nothing.+ gate.release!({ decision: "chat" });+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(host.state()).toEqual([]);+ expect(r.native.calls.some((x) => x.method === "start")).toBe(false);+ // The next open is asked (no grant was kept), and is not session_busy.+ const next = c.handle({ jsonrpc: "2.0", id: 8, method: "tools/call", params: { name: "computer_open_session", arguments: { app_id: "/usr/bin/gedit", mode: "control", purpose: "Write" } } }) as Promise<{ result: { content: Array<{ text: string }> } }>;+ while (r.opens.length < 2) await new Promise((resolve) => setTimeout(resolve, 10));+ gate.release!({ decision: "chat" });+ expect(JSON.parse((await next).result.content[0]!.text)).toMatchObject({ ok: true });+ await host.close();+ });+test("off by the server: no session", async () => {const r = rig("normal", { enabled: false });const host = await r.hostPromise;diff --git a/apps/server/src/computer-use/shared/PARITY.sha256 b/apps/server/src/computer-use/shared/PARITY.sha256index 66c82dd4..803c8d9c 100644--- a/apps/server/src/computer-use/shared/PARITY.sha256+++ b/apps/server/src/computer-use/shared/PARITY.sha256@@ -1,6 +1,6 @@# Computer use (contract v2, policy, media refs, image codecs, Windows/X11 host): the same files in omnirush-cli (src/computer-use/) and omnirush-gui (apps/server/src/computer-use/shared/).# A change to one must be ported to the other and this list updated in both.-59abb350a935c9462f3ea2e863396cdc785634dd2907f2f8947d87e9b30ec209 contract.mjs+a347ff267f8d1c8a06d14a5d75df11ea55aca3e6ccd6c78da1105a7a2a89c4eb contract.mjsaef0973f520f56ab8ad2699c14428bb2dd7ddb4b00dff823e4598e64d0c4a35b contract.d.mtsafde13ef4a34121c2d2b6cf1e2d4f592e48540979759d73b47ee9a721b8a36ac policy.mjs3422c097d4a598017d242ac5b517f30e96f91cdbe93b844d6b238ae6a7c9dea7 policy.d.mts@@ -9,5 +9,5 @@ a96246342720364eb3e5843ba70fbbf640365f94597fc4dbb6a7be7d95254ec4 image.mjse47c9ff67b9fce2ec12307737e973ce22551719c482b87f601569bd4f8a77e17 media.mjseabdf78c56733577d05aad490d9d7d5ece6c22e9b3ea9e6e9803d1a4a546fdd4 media.d.mtsa0bd964a5d0c7c5e26b4ce4ed8e8300f80a0a3a9da5d7f49c36f6ac2c907e934 native-worker.mjs-2210a0bf9337cbd2d3ee49f60337c0dc1aefca08705d449a596e20673f70905d portable-host.mjs+004402157beaa63f6fbfa99598e413abcb7891fabacd1a407ed71f02fd9ea882 portable-host.mjsfb2c1dae95e233af20798e0d6700e6f32197f6d4dbf226911beb35fcbff934d9 portable-host.d.mtsdiff --git a/apps/server/src/computer-use/shared/contract.mjs b/apps/server/src/computer-use/shared/contract.mjsindex 0a7bd7d2..7dcab58e 100644--- a/apps/server/src/computer-use/shared/contract.mjs+++ b/apps/server/src/computer-use/shared/contract.mjs@@ -77,7 +77,9 @@ export const LIMIT_RANGES = Object.freeze({model_long_side: [320, 1440],full_long_side: [640, 7680],observation_ttl_seconds: [2, 120],- approval_timeout_seconds: [10, 600],+ // At most 120 s: a question must end inside the engine's computer-use call timeout (CLI 150 s, less its+ // 7 s policy read; desktop 300 s), or a late answer would come with nobody waiting for it.+ approval_timeout_seconds: [10, 120],chat_approval_seconds: [0, 30 * 86400],media_full_bytes: [0, 2 * 1024 * 1024 * 1024],media_model_bytes: [0, 2 * 1024 * 1024 * 1024],diff --git a/apps/server/src/computer-use/shared/portable-host.mjs b/apps/server/src/computer-use/shared/portable-host.mjsindex c636040a..79a17cf5 100644--- a/apps/server/src/computer-use/shared/portable-host.mjs+++ b/apps/server/src/computer-use/shared/portable-host.mjs@@ -449,15 +449,18 @@ export async function createPortableHost({ native: firstNative, nativeFactory =async function askAct(s, request) {const l = limits();const expired = timeout(l.approval_timeout_seconds * 1000).then(() => ({ allow: false }));+ // The engine cancelling the call (notifications/cancelled) ends the question: the act is not done.+ const cancelled = new Promise((resolve) => { s.pendingAct = { resolve }; });s.waiting = true; update();try {return await Promise.race([+ cancelled,Promise.resolve(approve.act({ session_id: s.id, request_id: randomUUID(), chat: s.chat ?? null, app: { app_id: s.window.appId, app_name: s.window.appName, bundle_or_exe: s.window.executable, pid: s.window.pid }, window_title: s.window.title, purpose: s.purpose, intent: request.intent, action: request.action, reasons: request.reasons, points: request.points, frame: { width: request.frame.model.width, height: request.frame.model.height, sha256: request.frame.sha, data_url: `data:${request.frame.mime};base64,${request.frame.bytes.toString("base64")}` } })).then((v) => ({ allow: v?.allow === true })),expired,]);} catch {return { allow: false };- } finally { s.waiting = false; update(); }+ } finally { s.pendingAct = null; s.waiting = false; update(); }}async function discover() {@@ -508,7 +511,8 @@ export async function createPortableHost({ native: firstNative, nativeFactory =// The server's bound now (it may have been lowered since): 0 = until the chat ends.const fresh = grant && grant.epoch === epoch && (l.chat_approval_seconds === 0 || Date.now() - grant.at < l.chat_approval_seconds * 1000);let answer;- if (fresh) answer = { decision: "chat", window_id: String(preferred.id), reused: true };+ if (connection.cancelled) answer = { decision: "deny", cancelled: true };+ else if (fresh) answer = { decision: "chat", window_id: String(preferred.id), reused: true };else {const pendingAnswer = new Promise((resolve) => { s.pendingApproval = { resolve }; });const request = { session_id: s.id, request_id: randomUUID(), app: { app_id: preferred.appId, app_name: preferred.appName, bundle_or_exe: preferred.executable, pid: preferred.pid }, windows: windows.map((w) => ({ id: String(w.id), title: w.title })), window_id: String(preferred.id), mode: s.mode, purpose: s.purpose, os, seconds: l.session_seconds, chat_seconds: l.chat_approval_seconds, chat: s.chat ?? null };@@ -520,14 +524,23 @@ export async function createPortableHost({ native: firstNative, nativeFactory =s.pendingApproval = null;}if (!sessions.has(s.id)) return mcpResult(failure("approval_denied", "The request ended without access."));+ // Cancelled while the answer came in: nobody waits for this session, so it does not open.+ if (connection.cancelled && answer?.decision !== "deny") answer = { decision: "deny", cancelled: true };if (!answer || !["chat", "once"].includes(answer.decision)) {- // A Deny (or no answer) withdraws any earlier "Allow for this chat" for this app in this chat.- for (const key of [...grants.keys()]) if (key.startsWith(`${s.chat ?? ""}\0${preferred.appId}\0`)) grants.delete(key);+ // A Deny (or no answer) withdraws any earlier "Allow for this chat" for this app: in this chat, or, when+ // no chat is named (several running, or the copied CLI connection), in every chat. A call the engine+ // cancelled is no answer from the person: it changes no grant and opens nothing.+ if (!answer?.cancelled) {+ for (const key of [...grants.keys()]) {+ const [chat, app] = key.split("\0");+ if (app === preferred.appId && (!s.chat || chat === s.chat)) grants.delete(key);+ }+ }sessions.delete(s.id); if (lease === s.id) lease = null;s.approval = null;- emit(s, "denied", answer?.timeout ? "limit" : "person");+ emit(s, "denied", answer?.cancelled ? "agent" : answer?.timeout ? "limit" : "person", answer?.cancelled ? { reason: "cancelled" } : {});update();- return mcpResult(failure("approval_denied", answer?.timeout ? "Nobody answered the request. Ask the person before trying again." : "The person did not allow access to this window."));+ return mcpResult(failure("approval_denied", answer?.cancelled ? "The request was cancelled." : answer?.timeout ? "Nobody answered the request. Ask the person before trying again." : "The person did not allow access to this window."));}const chosen = windows.find((w) => String(w.id) === String(answer.window_id ?? preferred.id)) ?? preferred;try {@@ -589,7 +602,21 @@ export async function createPortableHost({ native: firstNative, nativeFactory =id: connection.id,async handle(message) {if (!record(message) || message.jsonrpc !== "2.0" || !RPC.has(message.method)) return { jsonrpc: "2.0", id: message?.id ?? null, error: { code: -32601, message: "Method not available." } };- if (message.method === "notifications/initialized" || message.method === "notifications/cancelled") return null;+ if (message.method === "notifications/initialized") return null;+ if (message.method === "notifications/cancelled") {+ // The engine gave up on the call in flight (its timeout, or the person stopped the turn): a question+ // it is waiting on ends unanswered, so nothing opens or acts after it.+ const id = message.params?.requestId;+ if (id !== undefined && id !== null && id === connection.inflight) {+ connection.cancelled = true;+ for (const s of sessions.values()) {+ if (s.connection !== connection) continue;+ s.pendingApproval?.resolve?.({ decision: "deny", cancelled: true });+ s.pendingAct?.resolve?.({ allow: false });+ }+ }+ return null;+ }let result;try {if (message.method === "initialize") result = { protocolVersion: typeof message.params?.protocolVersion === "string" ? message.params.protocolVersion : "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: SERVER_NAME, version: CONTRACT_VERSION }, instructions: INSTRUCTIONS };@@ -600,8 +627,8 @@ export async function createPortableHost({ native: firstNative, nativeFactory =const light = name === "computer_session_status" || name === "computer_close_session";if (connection.busy && !light) result = mcpResult(failure("busy", "Wait for the previous computer use call to finish."));else {- if (!light) connection.busy = true;- try { result = await call(connection, name, args); } finally { if (!light) connection.busy = false; }+ if (!light) { connection.busy = true; connection.inflight = message.id ?? null; connection.cancelled = false; }+ try { result = await call(connection, name, args); } finally { if (!light) { connection.busy = false; connection.inflight = null; } }}}} catch (error) {diff --git a/apps/server/src/engine-pool.test.ts b/apps/server/src/engine-pool.test.tsindex 86c97538..1947f6ef 100644--- a/apps/server/src/engine-pool.test.ts+++ b/apps/server/src/engine-pool.test.ts@@ -13,6 +13,7 @@ import {setEnginePoolForConfig,type EnginePoolHooks,type EngineSpawnTemplate,+ rootChats,} from "./engine-pool.js";import { createManagedOpencodeServer, type ManagedOpencodeServer } from "./managed-opencode.js";import { proxyOpencodeRequest, startServer } from "./server.js";@@ -101,6 +102,13 @@ async function writeFakeEngineBin(root: string): Promise<string> {" const entries = busySessions(server.port, url.searchParams.get('directory')).map((id) => [id, { type: 'busy' }]);"," return Response.json(Object.fromEntries(entries));"," }",+ " if (/^\\/session\\/[^/]+$/.test(url.pathname) && request.method === 'GET') {",+ " let parents = {};",+ " try { parents = JSON.parse(readFileSync(statePath, 'utf8')).__parents ?? {}; } catch {}",+ " const id = decodeURIComponent(url.pathname.slice('/session/'.length));",+ " if (!(id in parents)) return new Response('not found', { status: 404 });",+ " return Response.json({ id, ...(parents[id] ? { parentID: parents[id] } : {}) });",+ " }"," if (url.pathname === '/permission') {"," const owner = Number(url.searchParams.get('owner') ?? 0);"," if (owner && owner !== server.port) return Response.json([]);",@@ -566,6 +574,36 @@ describe("engine pool", () => {expect(await fixture.logLines()).toContain(`${oldPort} SIGTERM`);});+ test("activeChats: busy sessions count as their root chat; an unreadable session leaves the answer unknown", async () => {+ const fixture = await createFixture();+ const { pool, primary } = await createPool(fixture);+ const port = portOf(primary.url);+ const setParents = async (parents: Record<string, string | null>) => {+ const state = JSON.parse(await readFile(fixture.statePath, "utf8").catch(() => "{}")) as Record<string, unknown>;+ state.__parents = parents;+ await writeFile(fixture.statePath, JSON.stringify(state));+ };+ await setParents({ ses_root: null, ses_sub: "ses_root", ses_subsub: "ses_sub", ses_other: null });+ // A background sub-agent (two levels down) whose chat is not busy itself: that chat runs.+ await fixture.setBusyForDirectory(port, "/dir-a", ["ses_subsub"]);+ expect(await pool.activeChats(["/dir-a"])).toEqual({ sessions: ["ses_root"], unknown: 0 });+ // The chat and its sub-agent: one chat. Another chat too: two.+ await fixture.setBusyForDirectory(port, "/dir-a", ["ses_root", "ses_sub"]);+ expect(await pool.activeChats(["/dir-a"])).toEqual({ sessions: ["ses_root"], unknown: 0 });+ await fixture.setBusyForDirectory(port, "/dir-a", ["ses_sub", "ses_other"]);+ expect((await pool.activeChats(["/dir-a"])).sessions.sort()).toEqual(["ses_other", "ses_root"]);+ // A session the engine cannot describe: unknown (callers then name no chat).+ await fixture.setBusyForDirectory(port, "/dir-a", ["ses_missing"]);+ expect((await pool.activeChats(["/dir-a"])).unknown).toBe(1);+ });++ test("rootChats follows parents to the root and refuses a chain that is too long", async () => {+ const parents: Record<string, string | null> = { a: null, b: "a", c: "b", x: null };+ expect((await rootChats(["c", "b", "x"], async (id) => parents[id] ?? null)).sort()).toEqual(["a", "x"]);+ const loop: Record<string, string> = { p: "q", q: "p" };+ await expect(rootChats(["p"], async (id) => loop[id] ?? null, 4)).rejects.toThrow("too deep");+ });+test("reports an active draining generation until it retires", async () => {const fixture = await createFixture();const { pool, primary } = await createPool(fixture);diff --git a/apps/server/src/engine-pool.ts b/apps/server/src/engine-pool.tsindex c2ff7f89..a6e931dd 100644--- a/apps/server/src/engine-pool.ts+++ b/apps/server/src/engine-pool.ts@@ -440,6 +440,23 @@ export async function computeEngineConfigFingerprint(template: EngineSpawnTempla.digest("hex");}+/**+ * The chats behind busy sessions: each session's root (following parents up, at most `maxDepth` steps; a+ * longer chain throws, so the caller names no chat). Exported for tests.+ */+export async function rootChats(busy: string[], parentOf: (sessionId: string) => Promise<string | null>, maxDepth = 16): Promise<string[]> {+ const roots = await Promise.all([...new Set(busy)].map(async (sessionId) => {+ let current = sessionId;+ for (let step = 0; ; step += 1) {+ const parent = await parentOf(current);+ if (!parent) return current;+ if (step >= maxDepth) throw new Error("session chain too deep");+ current = parent;+ }+ }));+ return [...new Set(roots)];+}+export class EnginePool {private readonly config: ServerConfig;private readonly template: EngineSpawnTemplate;@@ -753,27 +770,37 @@ export class EnginePool {const payload: unknown = await response.json();if (!isRecord(payload)) throw new Error("unreadable status");const busy = Object.entries(payload).filter(([, status]) => isRecord(status) && status.type !== "idle").map(([sessionId]) => sessionId);- // Parents not known yet, read together (at most 32; more: unknown, so no chat is named).- const unread = busy.filter((sessionId) => !this.sessionParents.has(sessionId));- if (unread.length > 32) throw new Error("too many sessions to read");- await Promise.all(unread.map(async (sessionId) => {+ // Each busy session counts as the chat at the root of its parent chain (a background sub-agent whose+ // chat is idle still counts as that chat). Parents not known yet are read (at most 64 per pass; more:+ // unknown, so no chat is named).+ let reads = 0;+ const parentOf = async (sessionId: string): Promise<string | null> => {+ if (this.sessionParents.has(sessionId)) return this.sessionParents.get(sessionId) ?? null;+ if (++reads > 64) throw new Error("too many sessions to read");const info = new URL(`/session/${encodeURIComponent(sessionId)}`, generation.handle.url);info.searchParams.set("directory", directory);const answer = await loopbackFetch(info.toString(), { headers, signal: AbortSignal.timeout(5_000) });if (!answer.ok) throw new Error(`session ${answer.status}`);const session: unknown = await answer.json();if (!isRecord(session)) throw new Error("unreadable session");- this.sessionParents.set(sessionId, typeof session.parentID === "string" && session.parentID ? session.parentID : null);+ const parent = typeof session.parentID === "string" && session.parentID ? session.parentID : null;+ this.sessionParents.set(sessionId, parent);if (this.sessionParents.size > 4096) this.sessionParents.delete(this.sessionParents.keys().next().value!);- }));- for (const sessionId of busy) if (this.sessionParents.get(sessionId) === null) chats.add(sessionId);+ return parent;+ };+ for (const chat of await rootChats(busy, parentOf)) chats.add(chat);} catch {unknown += 1;}})));// Prompts on their way (the engine may not report them busy yet): chats the person prompted.for (const owned of this.activeSessionsByGeneration.values()) {- for (const sessionId of owned) if (!this.sessionParents.get(sessionId)) chats.add(sessionId);+ for (const sessionId of owned) {+ // A prompt on its way: its chat (the root of a known chain, else the session itself).+ let root = sessionId;+ for (let i = 0; i < 16 && this.sessionParents.get(root); i += 1) root = this.sessionParents.get(root)!;+ chats.add(root);+ }}return { sessions: [...chats], unknown };}