Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Phone batch: stop when the client cancels the call, and the skill says where to end a batch

Xiaolei Shiafa11569b419
diff --git a/server/drivers/phone-proxy.test.ts b/server/drivers/phone-proxy.test.ts
index 36684e3cea..480d00a6f7 100644
--- a/server/drivers/phone-proxy.test.ts
+++ b/server/drivers/phone-proxy.test.ts
@@ -364,7 +364,7 @@ describe("phone MCP tool surface", () => {
describe.skipIf(process.platform === "win32")("phone MCP calls through a stand-in adb", () => {
type Response = { result: { content: Array<{ type: string; text: string }>; isError?: boolean } };
- type Call = (name: string, args: Record<string, unknown>) => Promise<Response>;
+ type Call = (name: string, args: Record<string, unknown>, cancel?: "this call" | "another call") => Promise<Response>;
let home = "";
let adb = "";
const proxies: ChildProcess[] = [];
@@ -385,9 +385,11 @@ describe.skipIf(process.platform === "win32")("phone MCP calls through a stand-i
waiting.get(response.id)?.(response);
});
let id = 0;
- return (name, args) => new Promise((resolve) => {
+ return (name, args, cancel) => new Promise((resolve) => {
waiting.set(++id, resolve);
proxy.stdin!.write(`${JSON.stringify({ jsonrpc: "2.0", id, method: "tools/call", params: { name, arguments: args } })}\n`);
+ const cancelled = JSON.stringify({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: cancel === "this call" ? id : id + 1000 } });
+ if (cancel) setTimeout(() => proxy.stdin!.write(`${cancelled}\n`), 100);
});
};
@@ -469,6 +471,16 @@ esac
expect(adbLog()).toEqual(["devices -l"]);
});
+ it.each([
+ { cancel: "this call" as const, rest: `Stopped before step 2: The client cancelled this call. ${EARLIER_AND_LATER}`, adb: [] },
+ { cancel: "another call" as const, rest: "2. tap: Tapped 540,1200", adb: ["-s FAKE123 shell input tap 540 1200"] },
+ ])("stops a batch at the next step only when the client cancels $cancel", async ({ cancel, rest, adb }) => {
+ rmSync(join(home, "adb.log"), { force: true });
+ const response = await call("batch", { steps: [{ action: "wait", seconds: 0.4 }, { action: "tap", x: 540, y: 1200 }] }, cancel);
+ expect(response.result).toEqual({ content: [{ type: "text", text: `1. wait: Waited 0.4 s\n${rest}` }] });
+ expect(adbLog()).toEqual(["devices -l", ...adb]);
+ });
+
it("stops a batch at the next step once the harness refuses the claim", async () => {
const answers = [200, 409];
const harness = createServer((request, response) => {
diff --git a/server/drivers/phone-proxy.ts b/server/drivers/phone-proxy.ts
index c829f43b42..2fb129d1cc 100644
--- a/server/drivers/phone-proxy.ts
+++ b/server/drivers/phone-proxy.ts
@@ -597,11 +597,15 @@ const send = (message: Json) => process.stdout.write(`${JSON.stringify(message)}
const ok = (id: unknown, result: unknown) => send({ jsonrpc: "2.0", id, result });
const rpcError = (id: unknown, code: number, message: string) => send({ jsonrpc: "2.0", id, error: { code, message } });
+// MCP: a client that gives up on a call sends notifications/cancelled with that call's request id.
+const callsCancelled = new Map<unknown, boolean>();
+
async function handle(message: Json, ensureClaim: () => Promise<ClaimOutcome>) {
const id = message.id;
const method = message.method;
const params = (message.params ?? {}) as Json;
if (method === "initialize") return ok(id, { protocolVersion: String(params.protocolVersion ?? "2024-11-05"), capabilities: { tools: {} }, serverInfo: { name: "openmausbot-phone", version: "1" } });
+ if (method === "notifications/cancelled" && callsCancelled.has(params.requestId)) callsCancelled.set(params.requestId, true);
if (method === "notifications/initialized" || method === "notifications/cancelled") return;
if (method === "ping") return ok(id, {});
if (method === "tools/list") return ok(id, { tools: TOOLS });
@@ -610,10 +614,17 @@ async function handle(message: Json, ensureClaim: () => Promise<ClaimOutcome>) {
if (!TOOLS.some((tool) => tool.name === name)) return rpcError(id, -32602, `Unknown tool: ${name}`);
const claim = await ensureClaim();
if (!claim.ok) return ok(id, textResult(claim.message, true));
+ callsCancelled.set(id, false);
+ const stillWanted = async (): Promise<ClaimOutcome> => {
+ const stillClaimed = await ensureClaim();
+ return callsCancelled.get(id) ? { ok: false, message: "The client cancelled this call." } : stillClaimed;
+ };
try {
- return ok(id, await callTool(name, (params.arguments ?? {}) as Json, ensureClaim));
+ return ok(id, await callTool(name, (params.arguments ?? {}) as Json, stillWanted));
} catch (error) {
return ok(id, textResult(error instanceof Error ? error.message : String(error), true));
+ } finally {
+ callsCancelled.delete(id);
}
}
if (id !== undefined) rpcError(id, -32601, `Method not found: ${String(method)}`);
diff --git a/skills/phone-harness/SKILL.md b/skills/phone-harness/SKILL.md
index e432969b43..6c29a1ba16 100644
--- a/skills/phone-harness/SKILL.md
+++ b/skills/phone-harness/SKILL.md
@@ -21,8 +21,9 @@ with Bash, raw `adb`, `subprocess`, an emulator, network ADB, or Tailscale.
use `screenshot` and pixel `tap` only when accessibility text cannot
identify the target.
4. Use `swipe`, `type_text`, and `press` for interaction. When you know the
- next few steps, put them in one `batch`. To wait for content to load, add a
- `wait` step to the batch, not Bash `sleep`.
+ next few steps, put them in one `batch`, and end the batch where the next
+ screen is uncertain, such as after a step that may open a dialog. To wait
+ for content to load, add a `wait` step to the batch, not Bash `sleep`.
The tools operate the user's real phone. Navigate and read only what the task
needs. Stop before sending, posting, purchasing, booking, deleting, changing