diff --git a/docs/desktop-build-witness.md b/docs/desktop-build-witness.md index fb82c0b6e5..38b768c89c 100644 --- a/docs/desktop-build-witness.md +++ b/docs/desktop-build-witness.md @@ -159,8 +159,9 @@ See [desktop builds](https://github.com/morluto/rea/blob/main/.agents/skills/rev A checkpoint name is lowercase letters, digits, dot, dash and underscore; the capture's own `baseline`, `stop`, `time-limit` and `exit` are refused, and so -is a name already taken. The acknowledgement comes from the capture after it -has walked the home and retained what changed: `entries` walked, `changes` +is a name already reported. If cancellation interrupts the optional screenshot, +retry the same name to recover the original acknowledgement. The capture +acknowledges once it has walked the home and retained what changed: `entries` walked, `changes` since the checkpoint before, `retained_files` and `retained_bytes`, and up to ten `changed_files`, home-relative, with `more_changed_files` counting the rest. Folders are left out. Files an app window's title has named so far diff --git a/src/application/desktop/DesktopBuildCapture.ts b/src/application/desktop/DesktopBuildCapture.ts index c8497a0426..80425cc4c6 100644 --- a/src/application/desktop/DesktopBuildCapture.ts +++ b/src/application/desktop/DesktopBuildCapture.ts @@ -71,7 +71,7 @@ const CHECKPOINT_SHOT = { format: "jpeg", quality: 70 } as const; /** * Keep a screenshot of the checkpoint just taken in the capture's screens * folder, where the export finds a screen judge's examples. A screenshot - * that fails leaves the checkpoint as it is; only cancellation propagates. + * that fails leaves the acknowledged checkpoint available. */ const keepScreenshot = async ( computer: CaptureComputer, @@ -86,13 +86,13 @@ const keepScreenshot = async ( signal, ); const image = readComputerScreenshotBytes(shot); - if (image === undefined) return; + if (image === undefined || signal.aborted) return; await computer.addHomeFiles( new Map([[`${folder}/screens/${name}.jpg`, image]]), signal, ); - } catch (cause: unknown) { - if (signal.aborted) throw cause; + } catch { + // A screenshot only enriches a completed acknowledgement. } }; const STOP = "stop_build_capture"; @@ -244,6 +244,8 @@ interface CaptureRun { status: "running" | "unreleased" | "stopped" | "released"; /** Checkpoints the capture acknowledged, then those its own Evidence records. */ checkpoints: Set; + /** Remote acknowledgements whose caller cancelled before receiving them. */ + readonly unreportedCheckpoints: Map; /** The capture's package installs only from its file, so the export takes the request's URL. */ packageFile?: boolean; outputBytes?: number; @@ -359,6 +361,7 @@ export class DesktopBuildCapture { folder: `${WORK}/captures/${String(this.#count)}`, status: "running", checkpoints: new Set(), + unreportedCheckpoints: new Map(), exports: 0, }; this.#run = run; @@ -482,6 +485,12 @@ export class DesktopBuildCapture { ): Promise> { const run = this.#run; if (run?.status !== "running") return err(refused(MARK, ...NOT_RUNNING)); + if (signal.aborted) return err(cancelledBy(MARK, signal)); + const unreported = run.unreportedCheckpoints.get(name); + if (unreported !== undefined) { + run.unreportedCheckpoints.delete(name); + return ok(unreported); + } if (run.checkpoints.has(name)) return err( refused( @@ -518,18 +527,23 @@ export class DesktopBuildCapture { ), ); run.checkpoints.add(name); - await keepScreenshot(this.computer, run.folder, name, signal); const { kind: _kind, changed_files: changed, more_changed_files: more, ...taken } = event; - return ok({ + const receipt: CheckpointTaken = { ...taken, changed_files: changed ?? null, more_changed_files: more ?? null, - }); + }; + await keepScreenshot(this.computer, run.folder, name, signal); + if (signal.aborted) { + run.unreportedCheckpoints.set(name, receipt); + return err(cancelledBy(MARK, signal)); + } + return ok(receipt); } catch (cause: unknown) { return err( cause instanceof AnalysisError diff --git a/src/contracts/desktopBuildToolContracts.ts b/src/contracts/desktopBuildToolContracts.ts index ea3dd2e35b..c2760e4ae7 100644 --- a/src/contracts/desktopBuildToolContracts.ts +++ b/src/contracts/desktopBuildToolContracts.ts @@ -307,7 +307,7 @@ export const DESKTOP_BUILD_TOOL_CONTRACTS = [ }), ), kind: "application", - description: `Checkpoint the running capture under a unique name; returns acknowledged counts and up to ${String(MAX_ACKNOWLEDGED_FILES)} changed files. Files named by window titles rank first, then state_paths/open files, with caches last. Mark ${SEED_CHECKPOINT} after saving the seed and ${END_CHECKPOINT} after saving the first change. For later tasks, build on the preceding change and mark ${END_CHECKPOINT}-; mark near- after saving a near miss that must fail one task check. Duplicate names are refused.`, + description: `Checkpoint the running capture under a unique name; returns acknowledged counts and up to ${String(MAX_ACKNOWLEDGED_FILES)} changed files. Files named by window titles rank first, then state_paths/open files, with caches last. Mark ${SEED_CHECKPOINT} after saving the seed and ${END_CHECKPOINT} after saving the first change. For later tasks, build on the preceding change and mark ${END_CHECKPOINT}-; mark near- after saving a near miss that must fail one task check. If cancelled while saving the optional screenshot, retry the same name to recover its acknowledgement. Repeating a reported name is refused.`, inputSchema: z.strictObject({ computer_id: computerId, name: buildCheckpointNameSchema, diff --git a/tests/boundary/mcp/buildCaptureMcp.test.ts b/tests/boundary/mcp/buildCaptureMcp.test.ts index d578047c1f..ece4988299 100644 --- a/tests/boundary/mcp/buildCaptureMcp.test.ts +++ b/tests/boundary/mcp/buildCaptureMcp.test.ts @@ -270,14 +270,13 @@ it("takes checkpoints from a REA in the computer that predates their changed fil ).structuredContent, ).checkpoint; // The first reply is lost, so the second call reads the checkpoint from the capture's log. - expect( - ( - await client.callTool({ - name: "mark_build_checkpoint", - arguments: { computer_id: id, name: "seed" }, - }) - ).isError, - ).toBe(true); + const lost = parseMcpToolError( + await client.callTool({ + name: "mark_build_checkpoint", + arguments: { computer_id: id, name: "seed" }, + }), + ); + expect(lost.error.code).not.toBe("invalid_request"); const seed = await mark("seed"); const end = await mark("end"); @@ -297,7 +296,12 @@ it("takes checkpoints from a REA in the computer that predates their changed fil changed_files: null, more_changed_files: null, }); - await callOk(client, "stop_build_capture", { computer_id: id }); + const stopped = await callOk(client, "stop_build_capture", { + computer_id: id, + }); + expect(stopped.structuredContent).toMatchObject({ + draft: { seed_checkpoint: "seed", end_checkpoint: "end" }, + }); }); it("returns a draft note's read error when only a dconf database REA cannot read changed between seed and end", async () => { diff --git a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts index 36c88dc89d..1772215a5c 100644 --- a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts +++ b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts @@ -7,7 +7,10 @@ import { expect, it } from "vitest"; import { seedEndCapture } from "../../../src/application/desktop/desktopCapture.fixture.js"; import { toolContract } from "../../../src/contracts/toolContracts.js"; -import type { DesktopApplicationCapture } from "../../../src/domain/desktop/desktopCapture.js"; +import { + captureProgressEvent, + type DesktopApplicationCapture, +} from "../../../src/domain/desktop/desktopCapture.js"; import { VARICAD_URL, varicadPackage, @@ -68,30 +71,58 @@ it("stops a capture whose start reply was lost, and retries that stop before sta await callOk(client, "stop_build_capture", { computer_id: id }); }); -it("returns a checkpoint taken in the computer when its reply was lost and the name is asked again", async () => { - const { client } = await fakes({ dropReplies: ["mark"] }); +it("recovers the original checkpoint acknowledgement after the caller cancels its screenshot", async () => { + const { rea, computer, client } = await fakes({ + screenshotDelaysMs: [0, 0, 60_000], + }); const id = await startBuildComputer(client, BUILD_REQUEST); await callOk(client, "start_build_capture", { computer_id: id }); - const mark = (name: string) => - call(client, "mark_build_checkpoint", { computer_id: id, name }); - expect(parseMcpToolError(await mark("seed")).error.code).not.toBe( - "invalid_request", - ); - const retried = await mark("seed"); - expect(retried.isError, JSON.stringify(retried)).not.toBe(true); - expect(retried.structuredContent).toMatchObject({ - checkpoint: { name: "seed", at_ms: 1000, changes: 1 }, - }); - await callOk(client, "mark_build_checkpoint", { - computer_id: id, - name: "end", - }); - const stopped = await callOk(client, "stop_build_capture", { - computer_id: id, - }); - expect(stopped.structuredContent).toMatchObject({ - draft: { seed_checkpoint: "seed", end_checkpoint: "end" }, - }); + const request = { + name: "mark_build_checkpoint", + arguments: { computer_id: id, name: "seed" }, + }; + const controller = new AbortController(); + const screenshots = () => + computer.calls.filter( + ({ name, arguments: args }) => + name === "computer_screenshot" && args.format === "jpeg", + ).length; + try { + const marking = client.callTool(request, { signal: controller.signal }); + try { + await waitFor(() => expect(screenshots()).toBe(1)); + } finally { + controller.abort(); + await expect(marking).rejects.toThrow(); + } + const recovered = toolContract("mark_build_checkpoint").outputSchema.parse( + (await callOk(client, request.name, request.arguments)).structuredContent, + ); + expect(recovered.checkpoint).toMatchObject({ + name: "seed", + at_ms: 1000, + changes: 1, + retained_files: 1, + retained_bytes: 5, + }); + expect(screenshots()).toBe(1); + expect(parseMcpToolError(await client.callTool(request)).error.code).toBe( + "invalid_request", + ); + + const progress = await readFile( + join(rea.home, ".rea-build/captures/1/progress.log"), + "utf8", + ); + const seeds = progress + .split("\n") + .filter((line) => line.startsWith('{"rea_progress"')) + .map(captureProgressEvent) + .filter((event) => event?.kind === "checkpoint" && event.name === "seed"); + expect(seeds).toEqual([{ kind: "checkpoint", ...recovered.checkpoint }]); + } finally { + await call(client, "stop_build_capture", { computer_id: id }); + } }); it("finishes a stop whose draft reply was lost when stop is asked again, and repeats its result", async () => {