| 1 | // The human/agent control lease on a shared Codewhale Computer: input tools |
| 2 | // refuse with computer_busy_human_driving while a person drives, observation |
| 3 | // keeps working, and hand-back restores input without a restart. |
| 4 | import { hostKeysLine, attest, attestParams } from "./fixtures/host-decision.mjs"; |
| 5 | import { test, after } from "node:test"; |
| 6 | import assert from "node:assert/strict"; |
| 7 | import { spawn } from "node:child_process"; |
| 8 | import fs from "node:fs"; |
| 9 | import os from "node:os"; |
| 10 | import path from "node:path"; |
| 11 | import { readLease, inputRefusal, watchLease, HUMAN_DRIVING, LEASE_UNREADABLE } from "../src/lease.mjs"; |
| 12 | import { LEASE_GATED_TOOLS, OBSERVATION_TOOLS } from "../src/tools.mjs"; |
| 13 | |
| 14 | const ROOT = path.resolve(import.meta.dirname, ".."); |
| 15 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cu-lease-")); |
| 16 | const LEASE = path.join(dir, "lease.json"); |
| 17 | const writeLease = (value) => { |
| 18 | const tmp = `${LEASE}.tmp`; |
| 19 | fs.writeFileSync(tmp, typeof value === "string" ? value : JSON.stringify(value)); |
| 20 | fs.renameSync(tmp, LEASE); |
| 21 | }; |
| 22 | after(() => fs.rmSync(dir, { recursive: true, force: true })); |
| 23 | |
| 24 | test("readLease: unconfigured, absent, human, expired, agent and malformed", () => { |
| 25 | assert.equal(readLease({ file: null }).state, "none"); |
| 26 | assert.equal(readLease({ file: path.join(dir, "missing.json") }).state, "none"); |
| 27 | const now = Date.parse("2026-09-22T20:00:00Z"); |
| 28 | const read = (text) => () => text; |
| 29 | assert.equal(readLease({ file: "x", now, read: read('{"holder":"human","since":"2026-09-22T19:59:00Z"}') }).state, "human"); |
| 30 | assert.equal(readLease({ file: "x", now, read: read('{"holder":"human","expires_at":"2026-09-22T20:05:00Z"}') }).state, "human"); |
| 31 | const expired = readLease({ file: "x", now, read: read('{"holder":"human","expires_at":"2026-09-22T19:00:00Z"}') }); |
| 32 | assert.equal(expired.state, "none"); |
| 33 | assert.equal(expired.expired, true); |
| 34 | assert.equal(readLease({ file: "x", now, read: read('{"holder":"agent"}') }).state, "agent"); |
| 35 | assert.equal(readLease({ file: "x", now, read: read('{"holder":null}') }).state, "none"); |
| 36 | for (const bad of ["{", "[]", '{"holder":"robot"}', '{"holder":"human","expires_at":"soon"}']) { |
| 37 | assert.equal(readLease({ file: "x", now, read: read(bad) }).state, "unreadable", bad); |
| 38 | } |
| 39 | const eacces = () => { throw Object.assign(new Error("denied"), { code: "EACCES" }); }; |
| 40 | assert.equal(readLease({ file: "x", read: eacces }).state, "unreadable"); |
| 41 | }); |
| 42 | |
| 43 | test("inputRefusal names the code, is retryable, and is null when the agent may act", () => { |
| 44 | const human = inputRefusal("left_click", { state: "human", since: "t0", expires_at: null, generation: 3 }); |
| 45 | assert.equal(human.code, HUMAN_DRIVING); |
| 46 | assert.equal(human.extra.retryable, true); |
| 47 | assert.equal(human.extra.lease.generation, 3); |
| 48 | assert.equal(inputRefusal("left_click", { state: "unreadable", reason: "not JSON" }).code, LEASE_UNREADABLE); |
| 49 | assert.equal(inputRefusal("left_click", { state: "agent" }), null); |
| 50 | assert.equal(inputRefusal("left_click", { state: "none" }), null); |
| 51 | }); |
| 52 | |
| 53 | test("every input tool is gated and no observation tool is", () => { |
| 54 | for (const name of ["left_click", "type", "key", "scroll", "left_click_drag", "left_mouse_down", "mouse_move", |
| 55 | "browser_start", "browser_navigate", "browser_click", "browser_type", "open_application", "kill_app", |
| 56 | "write_clipboard", "set_value", "perform_action", "app_script"]) { |
| 57 | assert.ok(LEASE_GATED_TOOLS.has(name), `${name} must be lease-gated`); |
| 58 | } |
| 59 | for (const name of OBSERVATION_TOOLS) assert.ok(!LEASE_GATED_TOOLS.has(name), `${name} is observation`); |
| 60 | for (const name of ["stop_computer_control", "browser_stop", "computer_switch", "run_actions"]) { |
| 61 | assert.ok(!LEASE_GATED_TOOLS.has(name), `${name} is not itself gated`); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | test("watchLease fires when a person takes the lease", async () => { |
| 66 | let state = '{"holder":"agent"}'; |
| 67 | const seen = []; |
| 68 | const stop = watchLease((s) => seen.push(s), { file: "x", intervalMs: 10, read: () => state }); |
| 69 | await new Promise((r) => setTimeout(r, 40)); |
| 70 | state = '{"holder":"human"}'; |
| 71 | await new Promise((r) => setTimeout(r, 40)); |
| 72 | stop(); |
| 73 | assert.deepEqual(seen, ["human"]); |
| 74 | }); |
| 75 | |
| 76 | // ---- the real MCP server, with a lease file ---- |
| 77 | function startServer() { |
| 78 | const env = { ...process.env, CODEWHALE_CU_LEASE_FILE: LEASE, CODEWHALE_CU_APP: "off", CODEWHALE_CU_APP_WARM: "off", |
| 79 | CODEWHALE_CU_STATE_DIR: fs.mkdtempSync(path.join(dir, "state-")), CODEWHALE_CU_RECORDINGS_DIR: dir }; |
| 80 | const child = spawn(process.execPath, [path.join(ROOT, "mcp/server.mjs")], { env, stdio: ["pipe", "pipe", "ignore"] }); |
| 81 | child.stdin.write(hostKeysLine()); |
| 82 | let buf = ""; |
| 83 | const pending = new Map(); |
| 84 | let nextId = 1; |
| 85 | child.stdout.on("data", (chunk) => { |
| 86 | buf += chunk; |
| 87 | let i; |
| 88 | while ((i = buf.indexOf("\n")) >= 0) { |
| 89 | const line = buf.slice(0, i); buf = buf.slice(i + 1); |
| 90 | let msg; try { msg = JSON.parse(line); } catch { continue; } |
| 91 | pending.get(msg.id)?.(msg); pending.delete(msg.id); |
| 92 | } |
| 93 | }); |
| 94 | const rpc = (method, params) => new Promise((resolve) => { |
| 95 | const id = nextId++; |
| 96 | pending.set(id, resolve); |
| 97 | child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params: attestParams(method, params) })}\n`); |
| 98 | }); |
| 99 | const tool = async (name, args = {}) => JSON.parse((await rpc("tools/call", { name, arguments: args })).result.content[0].text); |
| 100 | return { child, rpc, tool }; |
| 101 | } |
| 102 | |
| 103 | test("MCP server: input refused under a human lease, observation not, input back after hand-back", async (t) => { |
| 104 | const { child, rpc, tool } = startServer(); |
| 105 | t.after(() => child.kill()); |
| 106 | await rpc("initialize", { protocolVersion: "2025-06-18" }); |
| 107 | |
| 108 | writeLease({ holder: "human", since: new Date().toISOString(), expires_at: null, generation: 1 }); |
| 109 | let r = await tool("left_click", { target: { type: "coordinate", x: 10, y: 10 } }); |
| 110 | assert.equal(r.ok, false); |
| 111 | assert.equal(r.error.code, HUMAN_DRIVING); |
| 112 | assert.equal(r.retryable, true); |
| 113 | r = await tool("click", { target: { type: "coordinate", x: 10, y: 10 } }); |
| 114 | assert.equal(r.error.code, HUMAN_DRIVING, "merged names resolve before the gate"); |
| 115 | r = await tool("browser", { action: "navigate", url: "https://example.com" }); |
| 116 | assert.equal(r.error.code, HUMAN_DRIVING); |
| 117 | r = await tool("run_actions", { steps: [{ tool: "type", arguments: { text: "x" } }] }); |
| 118 | assert.equal(r.error.code, HUMAN_DRIVING, "run_actions steps are gated"); |
| 119 | // Observation is not refused by the lease (it may fail for host reasons). |
| 120 | r = await tool("browser", { action: "status" }); |
| 121 | assert.equal(r.ok, true); |
| 122 | r = await tool("screenshot"); |
| 123 | assert.notEqual(r.error?.code, HUMAN_DRIVING); |
| 124 | |
| 125 | writeLease("{ not json"); |
| 126 | r = await tool("type", { text: "x" }); |
| 127 | assert.equal(r.error.code, LEASE_UNREADABLE, "a lease that cannot be read fails closed"); |
| 128 | |
| 129 | writeLease({ holder: "agent", since: new Date().toISOString(), generation: 2 }); |
| 130 | r = await tool("left_click", { target: { type: "coordinate", x: 10, y: 10 } }); |
| 131 | assert.notEqual(r.error?.code, HUMAN_DRIVING, "hand-back restores input with no restart"); |
| 132 | assert.notEqual(r.error?.code, "control_stopped"); |
| 133 | }); |
| 134 |