From 5775c8124f95b19f453454fb71777fa72db09ac1 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:59:26 +0800 Subject: [PATCH] test(desktop): inject the build computer's drawn-screenshot wait so its MCP test runs in 2 s (#10999) --- .../desktop/DesktopBuildComputers.ts | 4 +++ src/composition/capabilityServices.ts | 4 +++ src/desktop/AgentEnvComputer.ts | 36 ++++++++++++++++--- src/desktop/DrawnScreenshot.ts | 10 +++--- tests/boundary/mcp/buildComputerMcp.test.ts | 7 ++-- tests/fixtures/buildComputerMcp.ts | 7 +++- 6 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/application/desktop/DesktopBuildComputers.ts b/src/application/desktop/DesktopBuildComputers.ts index d13df91851..46111ed665 100644 --- a/src/application/desktop/DesktopBuildComputers.ts +++ b/src/application/desktop/DesktopBuildComputers.ts @@ -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, 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 ?? {}); diff --git a/src/composition/capabilityServices.ts b/src/composition/capabilityServices.ts index 205f281f16..3b28a8f087 100644 --- a/src/composition/capabilityServices.ts +++ b/src/composition/capabilityServices.ts @@ -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(), ), diff --git a/src/desktop/AgentEnvComputer.ts b/src/desktop/AgentEnvComputer.ts index c1d7e78ea3..599fc801ab 100644 --- a/src/desktop/AgentEnvComputer.ts +++ b/src/desktop/AgentEnvComputer.ts @@ -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 { 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) { diff --git a/src/desktop/DrawnScreenshot.ts b/src/desktop/DrawnScreenshot.ts index 8911473f63..df4cbfa51e 100644 --- a/src/desktop/DrawnScreenshot.ts +++ b/src/desktop/DrawnScreenshot.ts @@ -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 ( take: (signal: AbortSignal) => Promise, signal: AbortSignal, + waitMs: number = DRAWN_WAIT_MS, ): Promise => { - 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(); diff --git a/tests/boundary/mcp/buildComputerMcp.test.ts b/tests/boundary/mcp/buildComputerMcp.test.ts index c292bebde8..fd03fb8374 100644 --- a/tests/boundary/mcp/buildComputerMcp.test.ts +++ b/tests/boundary/mcp/buildComputerMcp.test.ts @@ -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( { diff --git a/tests/fixtures/buildComputerMcp.ts b/tests/fixtures/buildComputerMcp.ts index 84adc63e6a..4c2a1cb641 100644 --- a/tests/fixtures/buildComputerMcp.ts +++ b/tests/fixtures/buildComputerMcp.ts @@ -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>, settle: BuildSettle = FIRST_READ, defaults?: BuildComputerDefaults, + startTiming?: ComputerStartTiming, ): Promise => { 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" });