A desktop coding agent with free access to frontier models.
TypeScript
fix: computer use acts stop when the call was cancelled
ogsamratb5d0b7b1fa2d
diff --git a/apps/server/src/computer-use/portable-host.test.ts b/apps/server/src/computer-use/portable-host.test.tsindex 57c1ea31..fb28f78d 100644--- a/apps/server/src/computer-use/portable-host.test.ts+++ b/apps/server/src/computer-use/portable-host.test.ts@@ -375,6 +375,44 @@ describe("portable host: MCP surface and consent", () => {await host.close();});+ test("a cancel before an act's question, or after its Allow, means no question and no input", async () => {+ // (1) Cancel while the act is being prepared (the stale check's capture): no question, no input.+ {+ const r = rig("careful");+ const { host, c, session, obs } = await opened(r);+ const original = r.native.call.bind(r.native);+ let fired = false;+ r.native.call = async (method: string, params: Record<string, unknown> = {}) => {+ if (method === "capture" && !fired) { fired = true; await c.handle({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: 1 } }); }+ return original(method, params);+ };+ const act = await call(c, "computer_act", { session_id: session, observation_id: obs.state.observation?.id, request_id: "a", intent: "Type", action: { type: "type", text: "x" } });+ expect(act.state).toMatchObject({ ok: false, code: "approval_denied", may_have_acted: false, approval: { asked: false, by: "agent", reason: "cancelled" } });+ expect(r.acts.length).toBe(0);+ expect(r.native.calls.some((x) => x.method === "act")).toBe(false);+ await host.close();+ }+ // (2) The person allows, then the cancel lands before the input: no input.+ {+ const r = rig("careful");+ const { host, c, session, obs } = await opened(r);+ const original = r.native.call.bind(r.native);+ let armed = false;+ const acts = r.acts;+ const push = acts.push.bind(acts);+ acts.push = (...items: ActRequest[]) => { armed = true; return push(...items); };+ r.native.call = async (method: string, params: Record<string, unknown> = {}) => {+ if (armed && method === "state") { armed = false; await c.handle({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: 1 } }); }+ return original(method, params);+ };+ const act = await call(c, "computer_act", { session_id: session, observation_id: obs.state.observation?.id, request_id: "b", intent: "Type", action: { type: "type", text: "x" } });+ expect(act.state).toMatchObject({ ok: false, code: "approval_denied", may_have_acted: false });+ expect(r.acts.length).toBe(1);+ expect(r.native.calls.some((x) => x.method === "act")).toBe(false);+ 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 803c8d9c..c8797795 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-004402157beaa63f6fbfa99598e413abcb7891fabacd1a407ed71f02fd9ea882 portable-host.mjs+ce36d02e9160aab8f50c50defb2724a15e3244eaccf7e00b7d4e0492d2494cf1 portable-host.mjsfb2c1dae95e233af20798e0d6700e6f32197f6d4dbf226911beb35fcbff934d9 portable-host.d.mtsdiff --git a/apps/server/src/computer-use/shared/portable-host.mjs b/apps/server/src/computer-use/shared/portable-host.mjsindex 79a17cf5..32d2d6f0 100644--- a/apps/server/src/computer-use/shared/portable-host.mjs+++ b/apps/server/src/computer-use/shared/portable-host.mjs@@ -397,6 +397,7 @@ export async function createPortableHost({ native: firstNative, nativeFactory =} else {const pointList = action.type === "drag" ? action.path : action.x !== undefined ? [{ x: action.x, y: action.y }] : [];const answer = await askAct(s, { action, intent: args.intent, reasons: decision.reasons, points: pointList, frame: observed });+ if (answer.cancelled || s.connection.cancelled) return finish({ ...common, approval: { asked: answer.asked !== false, reasons: decision.reasons.map((r) => r.code), by: "agent", reason: "cancelled" }, ok: false, code: "approval_denied", message: "The request was cancelled.", may_have_acted: false });approval = { asked: true, reasons: decision.reasons.map((r) => r.code), by: "person" };if (!answer.allow) return finish({ ...common, approval, ok: false, code: "approval_denied", message: "The person did not allow this action. Ask them or choose another way." });if (sessions.get(s.id) !== s || s.phase !== "working") return finish({ ...common, approval, ok: false, code: "paused_by_person", message: "The session was paused or ended." });@@ -411,6 +412,8 @@ export async function createPortableHost({ native: firstNative, nativeFactory =if (after.identity !== current.identity || JSON.stringify(after.bounds) !== JSON.stringify(current.bounds)) return finish({ ...common, approval, ok: false, code: "window_changed", message: "The window moved or changed. Observe again.", next: "computer_observe" });if (!(await hitAll(after))) return finish({ ...common, approval, ok: false, code: "window_occluded", message: "Another window covers this point, or it is outside the approved window. Observe again." });}+ // The engine gave up on this call (before or while it was asked): no input with nobody waiting for it.+ if (s.connection.cancelled) return finish({ ...common, approval: { ...approval, ...(approval.asked ? {} : { by: "agent" }), reason: "cancelled" }, ok: false, code: "approval_denied", message: "The request was cancelled.", may_have_acted: false });const cursorBefore = await native.call("cursor").catch(() => null);const point = mapped.length === 1 ? { model: mapped[0].model, screen: mapped[0].screen } : undefined;const path = action.type === "drag" ? mapped.map((m) => ({ model: m.model, screen: m.screen })) : undefined;@@ -447,10 +450,12 @@ export async function createPortableHost({ native: firstNative, nativeFactory =}async function askAct(s, request) {+ // The engine already gave up on this call: no question.+ if (s.connection.cancelled) return { allow: false, cancelled: true, asked: false };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 }; });+ const cancelled = new Promise((resolve) => { s.pendingAct = { resolve }; }).then(() => ({ allow: false, cancelled: true, asked: true }));s.waiting = true; update();try {return await Promise.race([diff --git a/apps/server/src/engine-pool.ts b/apps/server/src/engine-pool.tsindex a6e931dd..c027e2a1 100644--- a/apps/server/src/engine-pool.ts+++ b/apps/server/src/engine-pool.ts@@ -796,10 +796,13 @@ export class EnginePool {// 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) {- // A prompt on its way: its chat (the root of a known chain, else the session itself).+ // A prompt on its way: its chat (the root of a known chain, else the session itself). A chain too+ // long or a loop: unknown, like rootChats.let root = sessionId;- for (let i = 0; i < 16 && this.sessionParents.get(root); i += 1) root = this.sessionParents.get(root)!;- chats.add(root);+ let steps = 0;+ while (this.sessionParents.get(root) && steps <= 16) { root = this.sessionParents.get(root)!; steps += 1; }+ if (steps > 16) unknown += 1;+ else chats.add(root);}}return { sessions: [...chats], unknown };