mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 05:02:40 +00:00
* fix(adb): run adb commands under owned supervision and start the adb server explicitly The adb version probe and device commands now run through runOwnedCommand in their own process group, keeping adb's SIGTERM-first stop through the termination policy. A device command's escaped descendants are swept with its cleanup, and cleanup that fails stays owned by the provider, which retries it before its next operation. Device operations start the adb server with an explicit `adb start-server` first. The server is an external shared service: REA may start it but never stops it, so a device command never forks it into its own cleanup. runOwnedCommand gains a byte-limited stdout collector: output beyond maxStdoutBytes is an output-limit failure, like an exceeded diagnostic budget, and the collected bytes are kept exactly for binary protocols such as screencap. Ownership preparation that cannot start its own helper for lack of host capacity is reported as the command's host-capacity launch failure. * fix(adb): preserve completed refusals, server startup facts, and lazy server start * fix(adb): scope runtime-root command cleanup and preserve incomplete command cleanup * fix(adb): preserve completed bugreports and staging directories across command cleanup failures * fix(adb): preserve bugreport command cleanup across readback failures and project live cleanup resources
156 lines
5.3 KiB
TypeScript
156 lines
5.3 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
import { adbDouble } from "../support/adb/adbProcessDouble.js";
|
|
|
|
import { AdbProvider } from "../../src/adb/AdbProvider.js";
|
|
import { parseEvidence } from "../../src/domain/evidence.js";
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
|
import { toolContract } from "../../src/contracts/toolContracts.js";
|
|
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
|
|
|
vi.mock("../../src/adb/AdbProcess.js", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("../../src/adb/AdbProcess.js")>()),
|
|
runAdbProcess: (await import("../support/adb/adbProcessDouble.js"))
|
|
.runAdbProcessDouble,
|
|
}));
|
|
// A running adb server makes `adb start-server` a successful no-op.
|
|
vi.mock("../../src/adb/AdbServer.js", () => ({
|
|
startAdbServer: async () => ({
|
|
ok: true,
|
|
value: { daemonStarted: false },
|
|
}),
|
|
}));
|
|
|
|
it.each(["start_adb_app", "start_adb_activity"] as const)(
|
|
"%s retains launch evidence through MCP with isolated process mocks",
|
|
async (operation) => {
|
|
const starting =
|
|
"Starting: Intent { act=android.intent.action.MAIN cmp=com.example/.Main }\n";
|
|
let stdout = `${starting}Status: ok\nActivity: com.example/.Main\nWaitTime: 315\nComplete\n`;
|
|
let stderr = "";
|
|
const version =
|
|
"Android Debug Bridge version 1.0.41\nVersion 34.0.5-stub\n";
|
|
adbDouble.version.mockResolvedValue({
|
|
stdout: version,
|
|
stderr: "",
|
|
});
|
|
const calls: string[][] = [];
|
|
adbDouble.device.mockImplementation(async (args) => {
|
|
calls.push([...args]);
|
|
return args.includes("resolve-activity")
|
|
? {
|
|
stdout: Buffer.from("com.example/.Main\n"),
|
|
stderr: Buffer.alloc(0),
|
|
}
|
|
: { stdout: Buffer.from(stdout), stderr: Buffer.from(stderr) };
|
|
});
|
|
onTestFinished(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
const server = createServer(
|
|
{
|
|
kind: "session",
|
|
session: createTestBinarySession(() => {
|
|
throw new Error("must not start deep provider");
|
|
}),
|
|
},
|
|
{ adbDeviceAnalysis: new AdbProvider({ environment: {} }) },
|
|
);
|
|
onTestFinished(() => server.close());
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
const client = new Client(
|
|
{ name: "adb-start-boundary", version: "0.0.0" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(() => client.close());
|
|
await client.connect(clientTransport);
|
|
const input =
|
|
operation === "start_adb_app"
|
|
? { serial: "emulator-5554", package: "com.example" }
|
|
: {
|
|
serial: "emulator-5554",
|
|
action: "android.intent.action.MAIN",
|
|
component: "com.example/.Main",
|
|
};
|
|
const successful = await client.callTool({
|
|
name: operation,
|
|
arguments: input,
|
|
});
|
|
expect(successful.isError).not.toBe(true);
|
|
const evidence = toolContract(operation).outputSchema.parse(
|
|
parseEvidence(
|
|
JSON.parse(
|
|
(successful.content as { text?: string }[])
|
|
.map((part) => part.text ?? "")
|
|
.join(""),
|
|
),
|
|
),
|
|
);
|
|
expect(evidence.confidence).toBe("observed");
|
|
expect(evidence.normalized_result).toMatchObject({
|
|
started_activity: "act=android.intent.action.MAIN cmp=com.example/.Main",
|
|
reported_activity: "com.example/.Main",
|
|
reported_status: "ok",
|
|
});
|
|
expect(evidence.raw_result).toEqual({ stdout, stderr });
|
|
|
|
stdout = `${starting}Status: timeout\nActivity: com.example/.Main\nWaitTime: 10004\nComplete\n`;
|
|
const timedOut = await client.callTool({
|
|
name: operation,
|
|
arguments: input,
|
|
});
|
|
expect(timedOut.isError).toBe(true);
|
|
const failure = JSON.parse(
|
|
(timedOut.content as { text?: string }[])
|
|
.map((part) => part.text ?? "")
|
|
.join(""),
|
|
);
|
|
expect(failure.error).toMatchObject({
|
|
code: "unsupported_target",
|
|
details: {
|
|
captured_output: { stdout, stderr, truncated: false },
|
|
partial_observation: {
|
|
provider_id: "adb",
|
|
operation,
|
|
result: {
|
|
started_activity:
|
|
"act=android.intent.action.MAIN cmp=com.example/.Main",
|
|
reported_activity: "com.example/.Main",
|
|
reported_status: "timeout",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(failure).not.toHaveProperty("confidence");
|
|
|
|
stdout = starting;
|
|
stderr = "Error: Activity class does not exist\n";
|
|
const refused = await client.callTool({
|
|
name: operation,
|
|
arguments: input,
|
|
});
|
|
expect(refused.isError).toBe(true);
|
|
expect(
|
|
(refused.content as { text?: string }[])
|
|
.map((part) => part.text ?? "")
|
|
.join(""),
|
|
).toContain("Activity class does not exist");
|
|
expect(calls.filter((args) => args.includes("start"))).toEqual(
|
|
Array.from({ length: 3 }, () => [
|
|
"-s",
|
|
"emulator-5554",
|
|
"shell",
|
|
"am",
|
|
"start",
|
|
"-W",
|
|
...(operation === "start_adb_app"
|
|
? ["-n", "com.example/.Main"]
|
|
: ["-a", "android.intent.action.MAIN", "-n", "com.example/.Main"]),
|
|
]),
|
|
);
|
|
},
|
|
);
|