test(desktop): inject the build computer's drawn-screenshot wait so its MCP test runs in 2 s (#10999)

This commit is contained in:
morluto authored and GitHub committed 2026-11-15 17:59:26 +08:00
1 parent 05bbb73046
commit 5775c8124f
6 files changed
+56 -12

No files matched your search

@@ -26,9 +26,11 @@ import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { err, ok, type Result } from "../../domain/result.js";
import {
AgentEnvComputer,
COMPUTER_START_TIMING,
AppDataRefusal,
ComputerStartFailure,
computerFailedOnReadiness,
type ComputerStartTiming,
type DriverResult,
} from "../../desktop/AgentEnvComputer.js";
import {
@@ -413,6 +415,7 @@ export class DesktopBuildComputers {
private readonly environment: Readonly<NodeJS.ProcessEnv>,
settle: BuildSettle = SETTLE_TIMING,
private readonly defaults: BuildComputerDefaults = {},
private readonly startTiming: ComputerStartTiming = COMPUTER_START_TIMING,
) {
this.#secrets = new DesktopBuildSecrets(buildSecretsDirectory(environment));
this.#shared = { settle, secrets: this.#secrets };
@@ -510,6 +513,7 @@ export class DesktopBuildComputers {
.report({ phase: START, completed: 0, total: null, message })
.catch(() => undefined);
},
this.startTiming,
);
computer = connected.computer;
const shot = driverCall("screenshot", input.screenshot ?? {});
+4
View File
@@ -119,6 +119,7 @@ import type { JebAnalysisPort } from "../application/jeb/JebAnalysisPort.js";
import type { AdbDeviceAnalysisPort } from "../application/adb/AdbDeviceAnalysisPort.js";
import type { BuildSettle } from "../application/desktop/DesktopBuildSettle.js";
import type { BuildComputerDefaults } from "../desktop/BuildComputerEnvironment.js";
import type { ComputerStartTiming } from "../desktop/AgentEnvComputer.js";
/** Typed injection inputs shared by a capability host, independent of transport. */
export interface CapabilityServiceOptions {
@@ -161,6 +162,8 @@ export interface CapabilityServiceOptions {
readonly desktopBuildSettle?: BuildSettle;
/** Where build computers look for unset settings; the host's PATH and REA cache unless a test isolates them. */
readonly desktopBuildDefaults?: BuildComputerDefaults;
/** How long a build computer waits for its desktop to draw and how often its start reports progress; the shipped timing unless a test shortens it. */
readonly desktopBuildStartTiming?: ComputerStartTiming;
readonly reverseEngineering?: ReverseEngineeringService;
}
@@ -339,6 +342,7 @@ const modules = {
providerEnvironment,
options.desktopBuildSettle,
options.desktopBuildDefaults,
options.desktopBuildStartTiming,
),
(value) => value.closeAll(),
),
+31 -5
View File
@@ -20,12 +20,24 @@ import {
readHomeFiles,
writeHomeFiles,
} from "./ComputerHomeFiles.js";
import { drawnScreenshot } from "./DrawnScreenshot.js";
import { DRAWN_WAIT_MS, drawnScreenshot } from "./DrawnScreenshot.js";
import { awaitHttpReply, type HttpReply } from "./HttpReply.js";
const READY_TIMEOUT_MS = 180_000;
const REQUEST_TIMEOUT_MS = 120_000;
const PROGRESS_MS = 5_000;
/** How long a computer's first screenshot waits for the desktop to draw, and how often its start reports progress. */
export interface ComputerStartTiming {
readonly drawnWaitMs: number;
readonly progressMs: number;
}
/** The shipped start timing; only tests shorten it. */
export const COMPUTER_START_TIMING: ComputerStartTiming = {
drawnWaitMs: DRAWN_WAIT_MS,
progressMs: 5_000,
};
const RECORDINGS = ".agentenv-recordings";
/** A control route's answer: readiness, or a recording's manifest, near 1.5 MB for a day of 15 s segments. */
const CONTROL_RESPONSE_BYTES = 4 * 1024 * 1024;
@@ -252,6 +264,7 @@ const progressHeartbeat = (
helper: ComputerHelper,
phase: { readonly name: string },
progress: ((message: string) => void) | undefined,
intervalMs: number,
) => {
if (progress === undefined) return undefined;
const startedAt = Date.now();
@@ -265,7 +278,7 @@ const progressHeartbeat = (
progress(
`${phase.name} (${String(Math.round((Date.now() - startedAt) / 1000))} s)${line === undefined || line === "" ? "" : `: ${line}`}`,
);
}, PROGRESS_MS);
}, intervalMs);
};
const connectDriver = async (
@@ -341,14 +354,25 @@ export class AgentEnvComputer {
helper: ComputerHelper,
signal: AbortSignal,
progress?: (message: string) => void,
timing: ComputerStartTiming = COMPUTER_START_TIMING,
): Promise<{
readonly computer: AgentEnvComputer;
readonly frame: DriverResult;
}> {
const phase = { name: "agentenv is deploying the computer" };
const heartbeat = progressHeartbeat(helper, phase, progress);
const heartbeat = progressHeartbeat(
helper,
phase,
progress,
timing.progressMs,
);
try {
return await AgentEnvComputer.#connect(helper, signal, phase);
return await AgentEnvComputer.#connect(
helper,
signal,
phase,
timing.drawnWaitMs,
);
} finally {
clearInterval(heartbeat);
}
@@ -358,6 +382,7 @@ export class AgentEnvComputer {
helper: ComputerHelper,
signal: AbortSignal,
phase: { name: string },
drawnWaitMs: number,
): ReturnType<typeof AgentEnvComputer.connect> {
const handshake = await helper.handshake(signal);
if (handshake === undefined || "error" in handshake)
@@ -389,6 +414,7 @@ export class AgentEnvComputer {
const frame = await drawnScreenshot(
(retry) => computer.callTool("computer_screenshot", {}, retry),
signal,
drawnWaitMs,
);
return { computer, frame };
} catch (cause: unknown) {
+6 -4
View File
@@ -4,9 +4,10 @@ import { isOneColourPng } from "../browser/PngVisualDiff.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { Deadline } from "../domain/deadline.js";
/** How often REA retakes a first screenshot the desktop has not drawn on, and the time the retries share. */
/** How often REA retakes a first screenshot the desktop has not drawn on. */
const DRAWN_POLL_MS = 500;
const DRAWN_WAIT_MS = 10_000;
/** The time those retries share unless a test shortens it. */
export const DRAWN_WAIT_MS = 10_000;
/** A driver's screenshot answer, as far as the wait reads it. */
interface Screenshot {
@@ -25,14 +26,15 @@ const undrawn = (shot: Screenshot): boolean => {
/**
* A whole-screen PNG screenshot once the desktop has drawn: readiness can
* pass while the display is still one colour, so REA takes another every
* 500 ms while it is. Retries share 10 s, each given only what is left of
* 500 ms while it is. Retries share `waitMs` (10 s), each given only what is left of
* it; when that runs out, the latest frame is returned.
*/
export const drawnScreenshot = async <Shot extends Screenshot>(
take: (signal: AbortSignal) => Promise<Shot>,
signal: AbortSignal,
waitMs: number = DRAWN_WAIT_MS,
): Promise<Shot> => {
const deadline = Deadline.after(DRAWN_WAIT_MS);
const deadline = Deadline.after(waitMs);
let shot = await take(signal);
while (!shot.isError && undrawn(shot)) {
const left = deadline.remainingMs();
+5 -2
View File
@@ -451,12 +451,15 @@ it("waits for the desktop to draw before it returns the first screenshot", async
it("returns the latest frame when a retried screenshot outlasts the wait for the desktop, sending progress meanwhile", async () => {
// The third screenshot would answer only after the test deadline, so the
// call returns only if the 10 s wait abandons it.
// call returns only if the shortened drawn wait abandons it.
const { computer, runner } = await fakes({
blankFrames: 10_000,
screenshotDelaysMs: [0, 0, 3_600_000],
});
const client = await connect(runner.environment);
const client = await connect(runner.environment, undefined, undefined, {
drawnWaitMs: 2_000,
progressMs: 250,
});
const progress: { message?: string | undefined }[] = [];
const started = await client.callTool(
{
+6 -1
View File
@@ -1,4 +1,5 @@
import type { BuildComputerDefaults } from "../../src/desktop/BuildComputerEnvironment.js";
import type { ComputerStartTiming } from "../../src/desktop/AgentEnvComputer.js";
import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
@@ -41,11 +42,12 @@ const FIRST_READ: SettleTiming = {
boundMs: 0,
};
/** An MCP client of a REA server whose build computers reach the fakes through `environment` and read apps with `settle`. */
/** An MCP client of a REA server whose build computers reach the fakes through `environment`, read apps with `settle` and start on `startTiming`. */
export const connectBuildServer = async (
environment: Readonly<Record<string, string>>,
settle: BuildSettle = FIRST_READ,
defaults?: BuildComputerDefaults,
startTiming?: ComputerStartTiming,
): Promise<Client> => {
const session = createTestBinarySession(() => {
throw new Error("build computers must not start a deep provider");
@@ -60,6 +62,9 @@ export const connectBuildServer = async (
},
desktopBuildSettle: settle,
desktopBuildDefaults: defaults ?? (await isolatedBuildDefaults()),
...(startTiming === undefined
? {}
: { desktopBuildStartTiming: startTiming }),
},
);
const client = new Client({ name: "build-computer-mcp", version: "1" });