A desktop coding agent with free access to frontier models.
TypeScript
fix: computer use grants only for the one running chat
ogsamrat7817b2225112
diff --git a/apps/desktop/electron/computer-use.mjs b/apps/desktop/electron/computer-use.mjsindex 48d5e45a..31eb027f 100644--- a/apps/desktop/electron/computer-use.mjs+++ b/apps/desktop/electron/computer-use.mjs@@ -176,7 +176,16 @@ async function hostContext() {// The chat now (the one that just called), not the last poll's.const fresh = await serverFetch("/computer-use/state").catch(() => null);if (fresh) server = fresh;- return { ancestors: shared?.ancestors ?? [], self_pids: [process.pid, ...metrics], chat: server?.chat ?? null };+ // No fresh answer: no chat (no "Allow for this chat" is kept or reused), never the last poll's.+ return { ancestors: shared?.ancestors ?? [], self_pids: [process.pid, ...metrics], chat: fresh ? fresh.chat ?? null : null };+}++/** Before each open: the server's switch and limits read now (not a cached answer). False when unread. */+async function hostRefresh() {+ const fresh = await serverFetch("/computer-use/refresh", { method: "POST", body: JSON.stringify({ max_age_ms: 0 }) }).catch(() => null);+ if (!fresh) return false;+ server = fresh;+ return fresh.read === true;}async function reportEvent(event) {@@ -190,7 +199,7 @@ async function startHost() {const { hostModule, mediaModule, policyModule, contractModule, imageModule } = await sharedModules();const spool = server?.spool ? mediaModule.createMediaSpool(server.spool) : null;const callbacks = {- spool, settings: hostSettings, context: hostContext, approve,+ spool, settings: hostSettings, context: hostContext, refresh: hostRefresh, approve,onEvent: (event) => { void reportEvent(event); },onState: () => { pushStatus(); scheduleRefresh(); },onFrame: (frame) => preview?.image(frame.data_url),diff --git a/apps/server/src/computer-use/desktop.test.ts b/apps/server/src/computer-use/desktop.test.tsindex e7e806be..56907337 100644--- a/apps/server/src/computer-use/desktop.test.ts+++ b/apps/server/src/computer-use/desktop.test.ts@@ -62,11 +62,56 @@ describe("switch and built-in entry", () => {chats.mark("ses_a"); chats.mark("ses_b"); chats.mark("ses_a");expect(chats.resolve(null)).toBe("ses_a");expect(chats.resolve(["ses_b"])).toBe("ses_b");+ // For grants only one running prompted chat names a chat; unknown, none or several: none.+ expect(chats.only(null)).toBeNull();+ expect(chats.only([])).toBeNull();+ expect(chats.only(["ses_b"])).toBe("ses_b");+ expect(chats.only(["ses_b", "ses_child_of_b"])).toBe("ses_b");+ expect(chats.only(["ses_a", "ses_b"])).toBeNull();+ expect(chats.only(["ses_unmarked"])).toBeNull();expect(sessionEventOf({ event: "opened", by: "agent", app_id: "/usr/bin/gedit", screen: { id: "0", width: 10, height: 5, scale: 1 }, junk: 1 })).toEqual({ event: "opened", by: "agent", app_id: "/usr/bin/gedit", screen: { id: "0", width: 10, height: 5, scale: 1 } });expect(sessionEventOf({ event: "hacked", by: "agent" })).toBeNull();});});+describe("routes: the chat a grant is kept for, and the policy read before an open", () => {+ test("state names a chat only when exactly one prompted chat runs; refresh reads the server now (max_age_ms) and says whether it did", async () => {+ const routes: Route[] = [];+ const state = tmp();+ const chats = createChatTracker();+ let running: string[] | null = null;+ const ages: number[] = [];+ let failRead = false;+ registerComputerUseRoutes({ routes, stateDir: () => state, jsonResponse: (d, status = 200) => new Response(JSON.stringify(d), { status }), readJsonBody: async (r) => r.json(), approvalMode: async () => "guarded", readCapabilities: async (maxAgeMs) => { ages.push(maxAgeMs); if (failRead) throw new Error("offline"); return { computer_use: { enabled: true, limits: { chat_approval_seconds: 600 } } }; }, applyEngine: async () => undefined, chats, runningSessions: async () => running, recordTrace: () => undefined });+ const hit = async (method: string, p: string, body?: unknown) => {+ const route = routes.find((r) => r.method === method && r.regex.test(p));+ if (!route) throw new Error("no route");+ const response = await route.handler({ request: new Request(`http://x${p}`, { method, ...(body === undefined ? {} : { body: JSON.stringify(body) }) }), url: new URL(`http://x${p}`), params: {} } as never);+ return (await response.json()) as Record<string, unknown>;+ };+ chats.mark("ses_a");+ // The engine's running sessions unknown: no chat, even though one was prompted.+ expect((await hit("GET", "/computer-use/state")).chat).toBeNull();+ running = ["ses_a"];+ expect((await hit("GET", "/computer-use/state")).chat).toBe("ses_a");+ // Switching chat: the new one is named, so a grant from ses_a is not reused there.+ chats.mark("ses_b"); running = ["ses_b"];+ expect((await hit("GET", "/computer-use/state")).chat).toBe("ses_b");+ // Both running: no chat (asked every time).+ running = ["ses_a", "ses_b"];+ expect((await hit("GET", "/computer-use/state")).chat).toBeNull();+ // Before an open the host asks with max_age_ms 0; the new bound is in force at once.+ const read = await hit("POST", "/computer-use/refresh", { max_age_ms: 0 });+ expect(read.read).toBe(true);+ expect(ages.at(-1)).toBe(0);+ expect((read.setting as { limits: { chat_approval_seconds: number } }).limits.chat_approval_seconds).toBe(600);+ expect((await hit("POST", "/computer-use/refresh", {})).read).toBe(true);+ expect(ages.at(-1)).toBe(60_000);+ failRead = true;+ expect((await hit("POST", "/computer-use/refresh", { max_age_ms: 0 })).read).toBe(false);+ });+});+describe("engine MCP plugin", () => {async function run(env: Record<string, string>, config: Record<string, unknown>) {const saved = { ...process.env };diff --git a/apps/server/src/computer-use/desktop.ts b/apps/server/src/computer-use/desktop.tsindex 5bb35cc5..d8a52dcf 100644--- a/apps/server/src/computer-use/desktop.ts+++ b/apps/server/src/computer-use/desktop.ts@@ -160,9 +160,10 @@ export function permissionModeName(mode: string | null | undefined): "careful" |}/**- * The chat a computer use call belongs to: the latest prompted chat that is- * still running (MCP calls carry no chat id). `mark` at every prompt- * dispatch; `resolve` with the engine's running sessions.+ * The chat a computer use call belongs to (MCP calls carry no chat id). `mark`+ * at every prompt dispatch; `only` (grants: exactly one prompted chat running)+ * and `resolve` (traces: the latest prompted chat still running) with the+ * engine's running sessions.*/export function createChatTracker(limit = 64) {const order: string[] = [];@@ -173,6 +174,16 @@ export function createChatTracker(limit = 64) {order.push(sessionId);if (order.length > limit) order.splice(0, order.length - limit);},+ /**+ * The one chat a call can belong to, for "Allow for this chat": exactly one prompted chat is running.+ * Running unknown (null), none or several: null, so no grant is kept or reused (the person is asked).+ */+ only(running: string[] | null): string | null {+ if (!running) return null;+ const live = order.filter((id) => running.includes(id));+ return live.length === 1 ? live[0]! : null;+ },+ /** The chat to record a trace event on: the latest prompted chat still running, else the latest. */resolve(running: string[] | null): string | null {for (let i = order.length - 1; i >= 0; i -= 1) {const id = order[i];diff --git a/apps/server/src/computer-use/portable-host.test.ts b/apps/server/src/computer-use/portable-host.test.tsindex 6dfb4fe9..a7014db8 100644--- a/apps/server/src/computer-use/portable-host.test.ts+++ b/apps/server/src/computer-use/portable-host.test.ts@@ -214,6 +214,25 @@ describe("portable host: MCP surface and consent", () => {} finally { Date.now = realNow; }});+ test("refresh before each open: a lowered bound applies at once; unread: no grant reused; off: refused", async () => {+ const r = rig("normal", { decision: "chat" });+ let reads = 0, ok = true;+ const host = await createPortableHost({ native: r.native, settings: () => r.settings, context: () => ({ ancestors: [], chat: "ses_1" }), refresh: async () => { reads += 1; return ok; }, approve: { open: async (request: OpenRequest) => { r.opens.push(request); return { decision: "chat", window_id: request.window_id }; }, act: async () => ({ allow: true }) } });+ const c = host.connect();+ const once = async () => { const x = await call(c, "computer_open_session", { app_id: "/usr/bin/gedit", mode: "control", purpose: "Write" }); if (x.state.session_id) await call(c, "computer_close_session", { session_id: x.state.session_id }); return x; };+ await once();+ await once();+ expect(r.opens.length).toBe(1);+ ok = false;+ await once();+ expect(r.opens.length).toBe(2);+ ok = true;+ r.settings.enabled = false;+ expect((await once()).state).toMatchObject({ ok: false, code: "disabled" });+ expect(reads).toBe(4);+ 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/routes.ts b/apps/server/src/computer-use/routes.tsindex 1ca479bd..dd638e22 100644--- a/apps/server/src/computer-use/routes.ts+++ b/apps/server/src/computer-use/routes.ts@@ -44,11 +44,12 @@ export type ComputerUseRouteDeps = {export function registerComputerUseRoutes(deps: ComputerUseRouteDeps): void {const { routes, jsonResponse } = deps;- const state = async () => {+ const state = async (read?: boolean) => {const dir = computerUseDir(deps.stateDir());const setting = computerUseSettingFor(dir);const mode = permissionModeName(await deps.approvalMode().catch(() => null));- const chat = deps.chats.resolve(await deps.runningSessions().catch(() => null));+ // The chat "Allow for this chat" is kept for: the one prompted chat that runs, else none (asked every time).+ const chat = deps.chats.only(await deps.runningSessions().catch(() => null));return {enabled: setting.enabled,setting: { enabled: setting.enabled, real_screen: setting.real_screen, virtual_display: setting.virtual_display, limits: setting.limits, deny_apps: setting.deny_apps, models: setting.models, message: setting.message, ask_send_delete: setting.ask_send_delete === true, reason: setting.reason, source: setting.source },@@ -57,19 +58,26 @@ export function registerComputerUseRoutes(deps: ComputerUseRouteDeps): void {spool: mediaSpoolDir(deps.stateDir()),host: readHostFile(dir) !== null,chat,+ ...(read === undefined ? {} : { read }),};};addRoute(routes, "GET", "/computer-use/state", "client", async () => jsonResponse(await state()));- addRoute(routes, "POST", "/computer-use/refresh", "client", async () => {+ // `max_age_ms` (0..60000, default 60000): how old a cached answer may be; the host asks with 0 before each+ // open, so a stricter server value applies at once. `read`: whether the server answered.+ addRoute(routes, "POST", "/computer-use/refresh", "client", async (ctx) => {+ const body = await deps.readJsonBody(ctx.request).catch(() => ({} as Record<string, unknown>));+ const maxAge = typeof body.max_age_ms === "number" && Number.isFinite(body.max_age_ms) ? Math.min(60_000, Math.max(0, Math.round(body.max_age_ms))) : 60_000;+ let read = false;try {- const capabilities = await deps.readCapabilities(60_000);+ const capabilities = await deps.readCapabilities(maxAge);if (rememberComputerUseSwitch(deps.stateDir(), capabilities)) void deps.applyEngine().catch(() => undefined);+ read = true;} catch {// A failed read keeps the last answer.}- return jsonResponse(await state());+ return jsonResponse(await state(read));});// Host and event: only Electron main (its per-launch secret, held in this process); the client token isdiff --git a/apps/server/src/computer-use/shared/PARITY.sha256 b/apps/server/src/computer-use/shared/PARITY.sha256index 69f9416c..01249b83 100644--- a/apps/server/src/computer-use/shared/PARITY.sha256+++ b/apps/server/src/computer-use/shared/PARITY.sha256@@ -9,5 +9,5 @@ a96246342720364eb3e5843ba70fbbf640365f94597fc4dbb6a7be7d95254ec4 image.mjse47c9ff67b9fce2ec12307737e973ce22551719c482b87f601569bd4f8a77e17 media.mjseabdf78c56733577d05aad490d9d7d5ece6c22e9b3ea9e6e9803d1a4a546fdd4 media.d.mtsa0bd964a5d0c7c5e26b4ce4ed8e8300f80a0a3a9da5d7f49c36f6ac2c907e934 native-worker.mjs-bd3ad13f41e8ebbfc22a812901dfcfd2d009be8e3c5ae0304d6e06cbab99808b portable-host.mjs-ecbac3ec99cc39d8b749699d97201074015b5746aa2baf32e17944c86ac8ef42 portable-host.d.mts+77f8fb1b27beef5ceedeb3e2c4721bd9b6a4d86f026281a294049a32ab48359f portable-host.mjs+016ecaad7c070693b86d98efdf1e043d005dd716171139e42493d53c8c767627 portable-host.d.mtsdiff --git a/apps/server/src/computer-use/shared/portable-host.d.mts b/apps/server/src/computer-use/shared/portable-host.d.mtsindex 6251ef4f..724789b7 100644--- a/apps/server/src/computer-use/shared/portable-host.d.mts+++ b/apps/server/src/computer-use/shared/portable-host.d.mts@@ -94,6 +94,8 @@ export function createPortableHost(options: {settings: () => HostSettings;/** May be async (read once per discover / open-session). */context?: () => HostContext | Promise<HostContext>;+ /** Read the server's switch and limits now (before each open); false or a throw: unread, no grant is reused. */+ refresh?: (() => Promise<boolean> | boolean) | null;approve: { open(request: OpenRequest): Promise<OpenAnswer> | OpenAnswer; act(request: ActRequest): Promise<{ allow: boolean }> | { allow: boolean } };onEvent?: (event: SessionEvent) => void;onState?: (states: HostSessionState[]) => void;diff --git a/apps/server/src/computer-use/shared/portable-host.mjs b/apps/server/src/computer-use/shared/portable-host.mjsindex e61a4b86..7e35c2a2 100644--- a/apps/server/src/computer-use/shared/portable-host.mjs+++ b/apps/server/src/computer-use/shared/portable-host.mjs@@ -129,13 +129,14 @@ function describeAction(a) {* - spool: a media spool (media.mjs createMediaSpool) frames go to, or null* - settings(): { enabled, real_screen, limits, deny_apps, mode, ask_send_delete } now* (mode: the chat's permission mode careful|normal|auto; ask_send_delete: the server's send/delete asks)+ * - refresh(): read the server's switch and limits now (before each open); false or a throw: unread* - context(): { ancestors, self_pids, tty_pids, chat } now, or a promise of it* (chat: the chat using computer use, for "Allow for this chat" and per-chat counts)* - approve: { open(request) → { decision: "chat"|"once"|"deny", window_id? },* act(request) → { allow: boolean } }* - onEvent(event): `computer.session` events; onState(states); onFrame(frame)*/-export async function createPortableHost({ native: firstNative, nativeFactory = null, spool = null, settings, context = () => ({}), approve, onEvent = () => {}, onState = () => {}, onFrame = () => {}, platform = process.platform }) {+export async function createPortableHost({ native: firstNative, nativeFactory = null, spool = null, settings, context = () => ({}), refresh = null, approve, onEvent = () => {}, onState = () => {}, onFrame = () => {}, platform = process.platform }) {let native = firstNative;let availability = await native.ready;let nativeDown = false;@@ -476,6 +477,9 @@ export async function createPortableHost({ native: firstNative, nativeFactory =}async function openSession(connection, args) {+ // The server's switch and limits as of now, so a stricter value (off, a shorter chat bound) applies at once.+ // When they could not be read, no "Allow for this chat" is reused (the person is asked).+ const policyRead = typeof refresh === "function" ? await Promise.resolve().then(refresh).then((v) => v !== false, () => false) : true;const config = settings() ?? {};const ctx = (await context()) ?? {};if (!config.enabled) return mcpResult(failure("disabled", config.message || "Computer use is turned off."));@@ -496,7 +500,7 @@ export async function createPortableHost({ native: firstNative, nativeFactory =lease = s.id; sessions.set(s.id, s);update();const grantKey = `${s.chat ?? ""}\0${preferred.appId}\0${s.mode}`;- const grant = s.chat ? grants.get(grantKey) : null;+ const grant = s.chat && policyRead ? grants.get(grantKey) : null;// The server's bound now (it may have been lowered since): 0 = until the chat ends.const fresh = grant && (l.chat_approval_seconds === 0 || Date.now() - grant.at < l.chat_approval_seconds * 1000);let answer;diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 32f41305..79ea032a 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -4977,7 +4977,11 @@ function createRoutes(},applyEngine: () => applyComputerUseEngine(config),chats: computerUseChats(config),- runningSessions: async () => null,+ // The engine's running sessions (null when any engine could not be read: no chat is named then).+ runningSessions: async () => {+ const busy = await collectBusyEngineSessions(config);+ return busy.unknown > 0 ? null : busy.sessions;+ },recordTrace: (sessionId, type, data) => recordOpenSessionTrace(config, sessionId, type, data),});