diff --git a/.agents/skills/reverse-engineer-anything/references/desktop-build.md b/.agents/skills/reverse-engineer-anything/references/desktop-build.md index 2a4c739fd1..efad507a94 100644 --- a/.agents/skills/reverse-engineer-anything/references/desktop-build.md +++ b/.agents/skills/reverse-engineer-anything/references/desktop-build.md @@ -136,7 +136,9 @@ the first source that has the app: `~/Documents`: build the document in the app, or write its source where the app edits its format as text, then save it again from the app. Make it look real but synthetic: the thing the task changes, plus a few things - the change must leave alone. For a starting file of the requester's, + the change must leave alone. Keep it to one document: the bundle's app + opens the seed's document at launch only when there is exactly one, and + otherwise starts on its own screen. For a starting file of the requester's, read it here and rebuild it in the app with synthetic data in place of personal data; REA cannot copy a file into the computer. 4. Only once the seed is saved, `mark_build_checkpoint` with name `seed`. @@ -225,9 +227,11 @@ checks. Give every `check_build_task` the task exactly as exported. 1. `start_build_computer` with `{"source": {"kind": "bundle", "path": ""}}`. The app starts - with the seed's files in place but need not open the document: open it - from the prompt's path, as a solving agent would. Take a `wait` - screenshot first, since the desktop may not have drawn yet. + with the seed's files in place and opens the seed's document when the + seed holds exactly one and the app takes a file. When the export's + `limitations` say it opens no document, open it from the prompt's path, + as a solving agent would. Take a `wait` screenshot first, since the + desktop may not have drawn yet. 2. `check_build_task` with `stage: "seed"` and `directory`, the absolute path of a new or empty folder for the record. A check must fail. Recording starts. Later stages take no `directory`. diff --git a/bridge/agentenv/capture.py b/bridge/agentenv/capture.py index 2cb2daf6ce..0162918f17 100644 --- a/bridge/agentenv/capture.py +++ b/bridge/agentenv/capture.py @@ -213,6 +213,7 @@ def run(arguments: dict) -> int: rea = shutil.which("rea") if rea is None: return _say({"error": "REA is not installed in this computer"}) + os.makedirs(os.path.dirname(arguments["stdout"]), exist_ok=True) for path, text in arguments.get("files", {}).items(): os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8") as handle: diff --git a/docs/desktop-application-export.md b/docs/desktop-application-export.md index 83fca6bc1a..c3b15bc6b8 100644 --- a/docs/desktop-application-export.md +++ b/docs/desktop-application-export.md @@ -25,6 +25,9 @@ a `.deb` file; see [Applications from a .deb file](#applications-from-a-deb-file `--seed-checkpoint` names the checkpoint whose state seeds the bundle, the baseline by default, and `--end-checkpoint` the one a task must pass on, the capture's last by default; see [The seed checkpoint](#the-seed-checkpoint). +`--synthetic-seed` says the seed was made for the task, as a build agent makes +it in a [build computer](desktop-build-witness.md), rather than taken from the +user's own data; see [A seed made for the task](#a-seed-made-for-the-task). The command returns Evidence with operation `export_desktop_application` and predicate type `rea.desktop-application-export`, listing the environment name, @@ -124,6 +127,17 @@ The class declares: desktop entry without `Path`), which a relative argument or program path such as `./app` also needs, and still starts if that folder is missing. The export refuses a capture that saw no application window. +- The seed's document, opened at launch, so the app shows it rather than its + start screen: when the seed holds exactly one file the state view gives the + `document` role, and the desktop entry's `Exec` takes files (a `%f`, `%F`, + `%u` or `%U` field code), its path under `$HOME` is appended to the command. + A command that already names a document, absolute or from its working + directory, as one captured with `--argument ` does, is kept as it is; + a settings path under the home does not count. A seed with no document launches + as captured. With several, no desktop entry or an `Exec` that takes no file, + the app launches as captured and the README and the result's limitations say + why. A reset + relaunches the app, so it opens the document again. - For an Electron application (the capture found its fuse sentinel or a `resources/app.asar`), `--no-sandbox` right after the program unless the command already has it: Chromium's sandbox needs user namespaces the @@ -185,6 +199,16 @@ The export refuses a name the capture does not hold exactly once, and an end checkpoint taken before the seed checkpoint; `inspect-desktop-application-state` lists the names in `capture.checkpoints`. +## A seed made for the task + +By default the seed is the user's real data: the README and the result's +limitations say to keep the bundle local and not publish it. With +`--synthetic-seed` they say instead that the seed was made for the task, and +to read `envs//seed/` before publishing the bundle; the export cannot +tell the two apart, so the flag is the caller's word. `export_build_environment` +passes it, since the building agent makes the seed in the build computer. The +Evidence records it as `synthetic_seed`. + ## Host name and user A seed can hold the capture host's name and the user's: Geany saved its mail @@ -337,6 +361,7 @@ when a DOCTYPE names a DTD. Known differences remain: and then gives the file an `error`, and so does a table or column name that is not UTF-8. -The seed is the user's real data. Keep the bundle local and do not publish it. +Unless it was [made for the task](#a-seed-made-for-the-task), the seed is the +user's real data. Keep the bundle local and do not publish it. The class was checked against agentenv-framework 0.9.1431 with `python -m agentenv_protocol.computer._declare` and `agent-env run --dry-run`. diff --git a/docs/desktop-build-witness.md b/docs/desktop-build-witness.md index f15af6cfaa..0d064b7429 100644 --- a/docs/desktop-build-witness.md +++ b/docs/desktop-build-witness.md @@ -27,7 +27,10 @@ See [desktop builds](https://github.com/morluto/rea/blob/main/.agents/skills/rev ## Build 1. `start_build_computer` from a request. REA is installed in the computer. -2. `start_build_capture`. REA writes an apt configuration under +2. `start_build_capture`. REA first checks the installed runtime's export + schema for synthetic-seed support. If the archive is too old, repack a + current REA, update `REA_BUILD_COMPUTER_REA_ARCHIVE`, restart the REA MCP + server and start a new build computer. REA then writes an apt configuration under `~/.rea-build/apt` and runs `apt-get update` as the computer's user, so the capture finds the package's sources (`package_lists` counts the lists, and `package_lists_error` gives apt's exit status and last lines when the @@ -55,8 +58,12 @@ See [desktop builds](https://github.com/morluto/rea/blob/main/.agents/skills/rev --end-checkpoint end`, so it seeds what was saved at `seed`, scrubs the computer's host name and user from it, and refuses a task whose checks all pass on the seed. A task larger than 96 KiB as REA passes it to the - computer is refused before anything runs. A file moved to the desktop Trash is not seeded, - and the export's limitations name what the Trash held at `seed`. For a + computer is refused before anything runs. It also passes `--synthetic-seed`, + since the agent made the seed for the task, so the README does not call it + the user's real data. When the seed holds exactly one document and the + app's desktop entry takes files, the bundle's app opens that document at + launch. A file moved to the desktop Trash is not seeded, and the export's + limitations name what the Trash held at `seed`. For a `.deb` app that no apt source the capture found offers, the export takes the request's URL with `--package-url`; one apt offers installs from apt. REA lists the bundle's files with their sizes and SHA-256 in the computer, into a file beside the bundle that it reads @@ -108,7 +115,8 @@ cancels the call in progress on it and refuses the calls still waiting. ## Witness Start a fresh computer from the exported folder with `start_build_computer` -and `{"kind": "bundle", "path": ...}`; agentenv boots the app with the seed. +and `{"kind": "bundle", "path": ...}`; agentenv boots the app with the seed, +on the seed's document when the export opens one. Then, with the task exactly as exported: | `stage` | Before it | Its checks must | diff --git a/docs/specs/desktop-app-environments/build-witness/spec.md b/docs/specs/desktop-app-environments/build-witness/spec.md index 5576b02c66..4cded391d1 100644 --- a/docs/specs/desktop-app-environments/build-witness/spec.md +++ b/docs/specs/desktop-app-environments/build-witness/spec.md @@ -84,6 +84,12 @@ and the recording and writes the record as Evidence. and the export's limitations name what the Trash held at the seed; a start from a bundle folder writes no bytecode into it; the first screenshot waits, briefly and bounded, until the frame is not one colour. +- Gaps of the blind validation's solve runs: the exported app opens the + seed's one document at launch, when its desktop entry takes files, so a + solving agent starts in the document rather than on the app's start + screen; and `export_build_environment` passes `--synthetic-seed`, so the + README says the seed was made for the task instead of calling it the + user's real data. - Gaps of the second review: calls on one computer (actions, capture steps, checks and close) run one at a time in arrival order, so a stage's diff --git a/docs/specs/desktop-app-environments/build-witness/tasks.md b/docs/specs/desktop-app-environments/build-witness/tasks.md index aa58273dbd..32b38129f0 100644 --- a/docs/specs/desktop-app-environments/build-witness/tasks.md +++ b/docs/specs/desktop-app-environments/build-witness/tasks.md @@ -11,3 +11,4 @@ - [x] Fix the blind validation run's gaps and the export's install source for a `.deb`. - [x] Fix the second review's gaps: one call at a time per computer, the recording's request id, undecided checks and the bundle's file list. - [x] Leave a failure that a cut view may have caused undecided: a missing line, row or value. +- [x] Open the seed's one document at launch, and word a seed made for the task as such. diff --git a/docs/specs/desktop-app-environments/build-witness/verification.md b/docs/specs/desktop-app-environments/build-witness/verification.md index c48898d609..f0d53e8048 100644 --- a/docs/specs/desktop-app-environments/build-witness/verification.md +++ b/docs/specs/desktop-app-environments/build-witness/verification.md @@ -245,6 +245,41 @@ tests added, and passes now. - `DesktopBuildTurns.test.ts`: a running call that finishes after a close keeps its result. +## Opening the seed's document + +Run on 2026-11-05: source tests and gates on macOS; the desktop source tests +again on the Ubuntu 24.04 amd64 VM (40 files, 422 passed, none skipped). + +- `seedDocument.test.ts`: the seed's one document is appended when `Exec` + holds `%F`, `%U`, `%f` or `%u`, and the launch respells it as + `"$HOME"/'Documents/poster.svg'`; two documents, none, an `Exec` without a + file field code and a capture without a desktop entry keep the command and + give the reason; a command already naming a file under the home is kept. +- `appExport.test.ts`: the README says what the launch opens, or why it + opens nothing; a seed made for the task is worded as such in the README + and the class, and a captured one as the user's real data. +- `DesktopApplicationExport.test.ts`: from a seed-and-end capture launched + through `notes %U`, the class launches `notes` with the note; through + `notes`, or without a desktop entry, and from a baseline seed with no + document, it launches as captured and a limitation says why; + `--synthetic-seed` changes the limitation and the README and is recorded + as `synthetic_seed`. +- `buildCaptureMcp.test.ts`: `export_build_environment` runs the export + with `--synthetic-seed`, and the real CLI's README for that capture says + the seed was made for the task. + +The blind Inkscape build's capture was gone with its build computer, so the +bundle could not be exported again. Instead, a copy of that bundle had only +its `Launch` command replaced by the one this code renders for its seed (one +document, `Documents/poster.svg`; the image's `Exec=inkscape %F`). A computer +of it came up in 47 s with Inkscape showing `poster.svg` and no Welcome +screen, and `data/reset` relaunched it on the poster again. Three Claude Code +runs with Sonnet 5.5 all passed, in 11, 9 and 10 turns for $0.70, $0.46 and +$0.72 ($0.63 on average); the same bundle opening on the Welcome screen had +taken 18, 23 and 21 turns for $0.70, $1.55 and $0.51 ($0.92). Cache reads +fell from 585-714K tokens to 181-225K; the cost is mostly cache writes, +which varied from 17K to 189K tokens whatever the turns. + ## Limits of this verification - The witness acts were scripted by this agent from the task's prompt; no diff --git a/docs/specs/desktop-app-environments/export/spec.md b/docs/specs/desktop-app-environments/export/spec.md index 4b6e2722b8..6ee49b47e7 100644 --- a/docs/specs/desktop-app-environments/export/spec.md +++ b/docs/specs/desktop-app-environments/export/spec.md @@ -27,7 +27,12 @@ state through `data/add`, `data/get` and `data/reset`. class match with a window ready check, `paths` from the state view's roles (state directories of application state files, or a folder beside one that differs only in letter case in the files' spelling, and folders of - documents, nested ones collapsed) and the skill file. + documents, nested ones collapsed) and the skill file. When the seed holds + exactly one document and the desktop entry's `Exec` takes files, the + launch appends its path under `$HOME`; a command that already names a + document, absolute or from its working directory, is kept, a seed with no document launches as captured, + and otherwise the README and the limitations say why the app opens no + document. - With paths, it declares all three data hooks and calls agentenv's helpers: seed the home once per boot then `keep_initial`, `add`, `reset`, and `get` plus a data part holding a typed JSON view of each file. Without paths it @@ -38,7 +43,8 @@ state through `data/add`, `data/get` and `data/reset`. application are compiled into the user's dconf database with `dconf-cli`, without D-Bus. - The README and the result state the agentenv-framework release the class was - checked against (0.9.1431), and that the seed is real user data. + checked against (0.9.1431), and that the seed is real user data, unless + `--synthetic-seed` says it was made for the task. ## Non-goals diff --git a/src/application/desktop/DesktopApplicationExport.test.ts b/src/application/desktop/DesktopApplicationExport.test.ts index f7cca04023..ebac7538ba 100644 --- a/src/application/desktop/DesktopApplicationExport.test.ts +++ b/src/application/desktop/DesktopApplicationExport.test.ts @@ -14,14 +14,24 @@ import type { AptRepository, DesktopApplicationCapture, } from "../../domain/desktop/desktopCapture.js"; -import { exportDesktopApplication } from "./DesktopApplicationExport.js"; import { + NO_LAUNCH_DOCUMENT, + launchDocumentNote, + seedWording, +} from "../../domain/desktop/appExport.js"; +import { + exportDesktopApplication, + seedLimitation, +} from "./DesktopApplicationExport.js"; +import { + EDITED_NOTE, HOME, capture, change, checkpoint, readers, retained, + seedEndCapture, writeCaptureFolder, } from "./desktopCapture.fixture.js"; @@ -660,6 +670,153 @@ describe("exportDesktopApplication home paths", () => { }); }); +/** Export the seed-then-end capture, launched from a desktop entry with `exec`, seeded at `seed`. */ +const exportSeeded = async ( + exec: string | null, + options: { readonly syntheticSeed?: boolean } = {}, +) => { + const local = seedEndCapture(); + const folder = await writeCaptureFolder( + { + ...local, + application: { + ...local.application, + desktop_entry: + exec === null + ? null + : { + id: "org.example.notes", + path: "/usr/share/applications/org.example.notes.desktop", + name: "Notes", + exec, + icon: null, + startup_wm_class: null, + categories: [], + mime_types: [], + terminal: false, + }, + }, + }, + undefined, + [EDITED_NOTE], + ); + const directory = join( + await createTestTempDirectory("desktop-export-"), + "bundle", + ); + const result = await exportDesktopApplication( + { + capture: folder, + directory, + name: "notes", + tasks: [], + seedCheckpoint: "seed", + endCheckpoint: "end", + ...options, + }, + readers, + ); + if (result instanceof Error) throw result; + const read = (path: string) => readFile(join(directory, path), "utf8"); + return { + result, + module: await read("envs/notes/notes_app.py"), + readme: await read("README.md"), + }; +}; + +/** The limitations on what the launch opens, found by the lead their formatter gives them. */ +const launchLimitations = (limitations: readonly string[]) => + limitations.filter((line) => line.includes(NO_LAUNCH_DOCUMENT)); + +describe("exportDesktopApplication launch document", () => { + it("opens the seed's one document when the desktop entry takes files", async () => { + const { result, module, readme } = await exportSeeded("notes %U"); + + expect(module).toContain( + `command=["sh", "-c", "cd \\"$HOME\\" 2>/dev/null; exec 'notes' \\"$HOME\\"/'Documents/today.txt'"]`, + ); + expect(readme).toContain( + launchDocumentNote({ + opens: { kind: "seed", document: "Documents/today.txt" }, + }), + ); + expect(readme).toContain("`~/Documents/today.txt`"); + expect(launchLimitations(result.limitations)).toEqual([]); + }); + + it.each(["notes", null])( + "launches as captured, and says why in the README and limitations, when Exec is %j", + async (exec) => { + const opened = await exportSeeded("notes %U"); + const { result, module, readme } = await exportSeeded(exec); + const launch = launchLimitations(result.limitations); + + expect(module).toContain( + `command=["sh", "-c", "cd \\"$HOME\\" 2>/dev/null; exec 'notes'"]`, + ); + expect(launch).toHaveLength(1); + expect( + result.limitations.filter((line) => !launch.includes(line)), + ).toEqual(opened.result.limitations); + for (const line of launch) expect(readme).toContain(line); + }, + ); + + it("launches as captured from a baseline seed, which holds no document", async () => { + const local = capture(); + const { result, directory } = await exportCapture({ + ...local, + application: { + ...local.application, + desktop_entry: { + id: "org.example.notes", + path: "/usr/share/applications/org.example.notes.desktop", + name: "Notes", + exec: "notes %F", + icon: null, + startup_wm_class: null, + categories: [], + mime_types: [], + terminal: false, + }, + }, + }); + + if (result instanceof Error) throw result; + expect(launchLimitations(result.limitations)).toEqual([]); + expect( + await readFile(join(directory, "envs/notes/notes_app.py"), "utf8"), + ).toContain( + `command=["sh", "-c", "cd \\"$HOME\\" 2>/dev/null; exec 'notes'"]`, + ); + }); + + it("says a seed made for the task was made for it, and a captured one is the user's own", async () => { + const made = await exportSeeded("notes %U", { syntheticSeed: true }); + const captured = await exportSeeded("notes %U"); + const notice = (syntheticSeed: boolean) => + seedWording({ name: "notes", seedCheckpoint: "seed", syntheticSeed }) + .notice; + + expect(made.result.parameters).toMatchObject({ synthetic_seed: true }); + expect(captured.result.parameters).toMatchObject({ synthetic_seed: false }); + for (const [exported, synthetic] of [ + [made, true], + [captured, false], + ] as const) { + expect(exported.result.limitations).toContain(seedLimitation(synthetic)); + expect(exported.result.limitations).not.toContain( + seedLimitation(!synthetic), + ); + expect(exported.readme).toContain(notice(synthetic)); + expect(exported.readme).not.toContain(notice(!synthetic)); + } + expect(made.readme).toContain("`envs/notes/seed/`"); + expect(captured.readme).not.toContain("`envs/notes/seed/`"); + }); +}); + describe("exportDesktopApplication bundle writes", () => { it.each(["cancelled", "out of space"])( "removes the unpublished bundle when its write fails (%s)", diff --git a/src/application/desktop/DesktopApplicationExport.ts b/src/application/desktop/DesktopApplicationExport.ts index 4ced49bd57..eba8d18622 100644 --- a/src/application/desktop/DesktopApplicationExport.ts +++ b/src/application/desktop/DesktopApplicationExport.ts @@ -4,7 +4,7 @@ import { appModuleFile, className, exportedStateFiles, - launchCommand, + launchDocumentLimitation, renderAppClass, renderEnvToml, renderInstallScript, @@ -15,10 +15,7 @@ import { type HomePathCollision, type SeedScrubReport, } from "../../domain/desktop/appExport.js"; -import { - INSTALL_SCRIPT, - electronLaunchArguments, -} from "../../domain/desktop/appExportInstall.js"; +import { INSTALL_SCRIPT } from "../../domain/desktop/appExportInstall.js"; import { installLimitations } from "../../domain/desktop/appExportInstallLimitations.js"; import type { DesktopApplicationCapture } from "../../domain/desktop/desktopCapture.js"; import { @@ -29,7 +26,6 @@ import { import { isWithinAny } from "../../domain/desktop/desktopStateDiff.js"; import type { HostIdentity } from "../../domain/desktop/hostIdentity.js"; import { scrubDconf } from "../../domain/desktop/seedScrub.js"; -import { isTextualFormat } from "../../domain/desktop/stateFormats.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import type { JsonValue } from "../../domain/jsonValue.js"; import { @@ -56,6 +52,7 @@ import { type SeedVersion, } from "./DesktopExportFiles.js"; import { withRetainedContent } from "./DesktopRetainedContent.js"; +import { planLaunch, seedVersions } from "./DesktopExportLaunch.js"; import { selectStateEnds } from "./DesktopStateVersions.js"; import { admitDesktopTask, type AdmittedTask } from "./DesktopTaskExport.js"; @@ -76,6 +73,8 @@ export interface DesktopExportRequest { readonly seedCheckpoint?: string | undefined; /** The checkpoint a task's end state is read at; the last when omitted. */ readonly endCheckpoint?: string | undefined; + /** The seed was made for the task, as in a build computer, rather than taken from the user's own data. */ + readonly syntheticSeed?: boolean | undefined; } /** What the export wrote. */ @@ -197,15 +196,8 @@ const planExport = ( context.seed, context.identity, ); - const seeds = new Map(); - const textSeeds: string[] = []; - for (const [home, path] of files) { - const version = versionAt(path, context.seed); - if (!isWithinAny(home, paths) || version?.present !== true) continue; - if (version.sha256 === null) continue; - seeds.set(home, { sha256: version.sha256, format: version.format }); - if (isTextualFormat(version.format)) textSeeds.push(home); - } + const { seeds, textSeeds } = seedVersions(files, paths, context.seed); + const launch = planLaunch(capture, electron, seeds, files); const plan: DesktopAppExportPlan = { name: source, className: className(source), @@ -221,17 +213,15 @@ const planExport = ( ? {} : { executablePackage: install.executablePackage }), electron, - command: launchCommand( - electronLaunchArguments(application.argv, electron), - host.home, - application.working_directory, - ), + command: launch.command, + opens: launch.opens, windowClass, paths, capturedHome: host.home, seedCheckpoint: state.capture.checkpoints[context.seed] ?? "baseline", + syntheticSeed: request.syntheticSeed === true, seedFiles: [...seeds.keys()].sort(), - textSeeds: textSeeds.sort(), + textSeeds, unseededFiles: unseededFiles(capture, homePath, files, { paths, seed: context.seed, @@ -255,10 +245,14 @@ const planExport = ( }; }; -const EXPORT_LIMITATIONS = [ - "The seed is the user's real state at the seed checkpoint; keep the bundle local.", - "Binary state files are seeded as captured; only text files, UTF-8 or UTF-16 with a byte-order mark, have the capture host's name, user@host and home path replaced.", -]; +/** The limitation on where the seed came from: made for the task, or the user's real state, kept local. */ +export const seedLimitation = (syntheticSeed: boolean): string => + syntheticSeed + ? "The seed was made for the task, not taken from a user's own data; read it before publishing the bundle." + : "The seed is the user's real state at the seed checkpoint; keep the bundle local."; + +const SCRUB_LIMITATION = + "Binary state files are seeded as captured; only text files, UTF-8 or UTF-16 with a byte-order mark, have the capture host's name, user@host and home path replaced."; /** The bundle's generated files, the class module flagged when its dconf keys hold the identity, and its README, made once the seeds' scrub is known. */ const bundleText = ( @@ -308,8 +302,11 @@ const exportLimitations = ( }, ): string[] => { const opened = capture.state.unretained_opened_directories; + const opens = launchDocumentLimitation(plan.opens); return [ - ...EXPORT_LIMITATIONS, + seedLimitation(plan.syntheticSeed), + SCRUB_LIMITATION, + ...(opens === undefined ? [] : [opens]), ...facts.install, ...(plan.unseededFiles.length === 0 ? [] @@ -454,6 +451,7 @@ export const exportDesktopApplication = async ( package_url: request.packageUrl !== undefined, seed_checkpoint: request.seedCheckpoint ?? null, end_checkpoint: request.endCheckpoint ?? null, + synthetic_seed: request.syntheticSeed === true, }, result: { ...result, diff --git a/src/application/desktop/DesktopBuildCapture.ts b/src/application/desktop/DesktopBuildCapture.ts index 14af49f87c..c5997d61c5 100644 --- a/src/application/desktop/DesktopBuildCapture.ts +++ b/src/application/desktop/DesktopBuildCapture.ts @@ -1,4 +1,7 @@ +import { z } from "zod"; + import { AnalysisError } from "../../domain/analysisErrorBase.js"; +import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js"; import type { BuildRequest } from "../../domain/desktop/appBuild.js"; import { BUILD_PACKAGE_FILE } from "../../domain/desktop/appBuild.js"; import { @@ -18,6 +21,7 @@ import { controller, errorText, refused, + runRea, WORK, type Answer, } from "./DesktopBuildCommands.js"; @@ -40,6 +44,87 @@ const NOT_RUNNING = [ "Start one with start_build_capture.", ] as const; +const exportOptionsSchema = z.object({ + options: z.object({ properties: z.record(z.string(), z.unknown()) }), +}); +const repackRuntime = + "Run npm run pack:build-computer-runtime -- /absolute/path/rea-prod.tar.xz in a current REA checkout, set REA_BUILD_COMPUTER_REA_ARCHIVE to that archive, restart the REA MCP server, and start a new build computer."; +const schemaCommandError = (exitCode: number | null, detail: string) => + new ProviderAdapterError(AGENTENV_PROVIDER, START, { + userMessage: `The build computer's REA export schema command ${exitCode === null ? "ended without an exit status" : `exited ${String(exitCode)}`}: ${detail}`, + remediationAction: `Inspect the command error. If the runtime is outdated, ${repackRuntime}`, + }); + +/** Check the installed runtime's advertised export contract before capturing the user's work. */ +const checkExportRuntime = async ( + computer: ControlledComputer, + signal: AbortSignal, +): Promise> => { + try { + const path = `${WORK}/export-schema.json`; + const command = await runRea( + computer, + ["export-desktop-application", "--schema", "--json"], + { stdout: path, timeoutS: 30, signal }, + ); + if (command.exitCode !== 0 && command.stderr !== "") + return err(schemaCommandError(command.exitCode, command.stderr)); + if (command.stdoutBytes > 1024 * 1024) + return err( + adapterError( + START, + "The build computer's REA export schema exceeds 1 MiB.", + ), + ); + const content = ( + await computer.homeFiles([path], command.stdoutBytes, signal) + ).get(path); + if (content === undefined) + return err( + adapterError( + START, + "The build computer did not return its REA export schema.", + ), + ); + if (command.exitCode !== 0) + return err( + schemaCommandError( + command.exitCode, + content.toString("utf8").slice(-2048), + ), + ); + const schema = exportOptionsSchema.safeParse( + JSON.parse(content.toString("utf8")), + ); + if (!schema.success) + return err( + adapterError( + START, + "The build computer's REA returned an invalid export schema.", + ), + ); + if (!("syntheticSeed" in schema.data.options.properties)) + return err( + refused( + START, + "The REA runtime in this build computer does not support synthetic seeds required by build export.", + repackRuntime, + ), + ); + return ok(undefined); + } catch (cause: unknown) { + return err( + signal.aborted + ? new AnalysisCancelledError(START, { cause: signal.reason }) + : adapterError( + START, + `Could not read the build computer's REA export schema: ${errorText(cause)}`, + cause, + ), + ); + } +}; + type CheckpointTaken = Omit< Extract, "kind" @@ -154,6 +239,8 @@ export class DesktopBuildCapture { !(await this.#release(previous, signal)) ) return err(unreleasedError(START, previous, undefined)); + const runtime = await checkExportRuntime(this.computer, signal); + if (!runtime.ok) return runtime; this.#count += 1; const run: CaptureRun = { folder: `${WORK}/captures/${String(this.#count)}`, diff --git a/src/application/desktop/DesktopBuildCommands.ts b/src/application/desktop/DesktopBuildCommands.ts index 114e5c9fbf..356817e6a0 100644 --- a/src/application/desktop/DesktopBuildCommands.ts +++ b/src/application/desktop/DesktopBuildCommands.ts @@ -74,9 +74,10 @@ export const runRea = ( options: { readonly stdout: string; readonly files?: Readonly>; + readonly timeoutS?: number; readonly signal: AbortSignal; }, -): Promise => +): ReturnType => runControllerRea(computer, argv, { ...options, work: WORK }); /** The Evidence result, or the reported failure, a REA command wrote to a file of `bytes` in the computer. */ diff --git a/src/application/desktop/DesktopBuildExport.ts b/src/application/desktop/DesktopBuildExport.ts index 6b1ad72b77..becee0b379 100644 --- a/src/application/desktop/DesktopBuildExport.ts +++ b/src/application/desktop/DesktopBuildExport.ts @@ -137,7 +137,7 @@ export const exportBuild = async ( ), ); try { - const bytes = await runRea( + const { stdoutBytes: bytes } = await runRea( computer, [ "export-desktop-application", @@ -146,6 +146,8 @@ export const exportBuild = async ( "--task", task, ...ENDS, + // The building agent made the seed in this computer for the task. + "--synthetic-seed", ...(request.name === undefined ? [] : ["--name", request.name]), ...(capture.packageUrl === undefined ? [] diff --git a/src/application/desktop/DesktopBuildSummary.ts b/src/application/desktop/DesktopBuildSummary.ts index c63628c595..a7c63a08c3 100644 --- a/src/application/desktop/DesktopBuildSummary.ts +++ b/src/application/desktop/DesktopBuildSummary.ts @@ -87,7 +87,7 @@ const draftTask = async ( draft_refusal: `No draft: the capture has no ${missing.join(" or ")} checkpoint; capture again and mark ${SEED_CHECKPOINT} once the seed is saved and ${END_CHECKPOINT} once the edit is.`, }; const stdout = `${folder}/draft.json`; - const bytes = await runRea( + const { stdoutBytes: bytes } = await runRea( computer, [ "describe-desktop-application-task", diff --git a/src/application/desktop/DesktopBuildWitness.test.ts b/src/application/desktop/DesktopBuildWitness.test.ts new file mode 100644 index 0000000000..5e836e4ebf --- /dev/null +++ b/src/application/desktop/DesktopBuildWitness.test.ts @@ -0,0 +1,71 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +import { describe, expect, it } from "vitest"; + +import { + AnalysisCancelledError, + AnalysisInputError, +} from "../../domain/analysisErrorCore.js"; +import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; +import { BuildComputerActionLog } from "./DesktopBuildActionLog.js"; +import { + DesktopBuildWitness, + type WitnessedComputer, +} from "./DesktopBuildWitness.js"; + +const task = { + name: "add-eggs", + prompt: "Add eggs.", + checks: [ + { + kind: "lines_present" as const, + file: "Documents/today.txt", + lines: ["eggs"], + }, + ], +}; + +const computer = { + appData: () => Promise.reject(new Error("unread")), + startRecording: () => Promise.reject(new Error("unstarted")), + endRecording: () => Promise.reject(new Error("unended")), + recordingSegments: () => Promise.reject(new Error("unfetched")), +} as WitnessedComputer; + +const witnessAt = async (bundle: string) => + new DesktopBuildWitness(computer, new BuildComputerActionLog(), bundle); + +describe("bundle task reads", () => { + it("returns a cancelled result when reading the bundle's task is aborted", async () => { + const bundle = await createTestTempDirectory("witness-bundle-"); + const directory = await createTestTempDirectory("witness-record-"); + const signal = AbortSignal.abort(); + const result = await witnessAt(bundle).then((witness) => + witness.check({ task, stage: "seed", directory }, signal), + ); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.error).toBeInstanceOf(AnalysisCancelledError); + if (!(result.error instanceof AnalysisCancelledError)) return; + expect(result.error.operation).toBe("check_build_task"); + }); + + it("returns an adapter error when the bundle path cannot be read", async () => { + const root = await createTestTempDirectory("witness-bundle-file-"); + const bundle = join(root, "not-a-directory"); + await writeFile(bundle, "bundle"); + const directory = await createTestTempDirectory("witness-record-"); + const result = await witnessAt(bundle).then((witness) => + witness.check( + { task, stage: "seed", directory }, + new AbortController().signal, + ), + ); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.error).toBeInstanceOf(AnalysisInputError); + if (!(result.error instanceof AnalysisInputError)) return; + expect(result.error.userMessage).toContain(bundle); + }); +}); diff --git a/src/application/desktop/DesktopBuildWitness.ts b/src/application/desktop/DesktopBuildWitness.ts index c0dd1436ee..b77bfd0951 100644 --- a/src/application/desktop/DesktopBuildWitness.ts +++ b/src/application/desktop/DesktopBuildWitness.ts @@ -44,6 +44,7 @@ import { readHostFile } from "../../desktop/HostFileRead.js"; import { isPortableBundleName } from "../environment/environmentBundle.js"; import type { ReadonlyActionLog } from "./DesktopBuildActionLog.js"; import { errorText } from "./DesktopBuildCommands.js"; +import { desktopOperationFailure } from "./DesktopCaptureRefusals.js"; import { writeWitnessRecord, type EndedWitness, @@ -491,10 +492,23 @@ export class DesktopBuildWitness { "Give a new or empty folder.", ), ); - const differs = - this.bundle === undefined - ? undefined - : await bundleTaskDifference(this.bundle, task, signal); + let differs: string | undefined; + if (this.bundle !== undefined) { + try { + differs = await bundleTaskDifference(this.bundle, task, signal); + } catch (cause: unknown) { + // A cancelled read, an I/O failure, or a handle that would not close + // stays a Result. A cleanup lease stays bound to that error. + return err( + desktopOperationFailure( + cause, + signal, + CHECK, + "Reading the bundle's task", + ), + ); + } + } if (differs !== undefined) return err( refused( diff --git a/src/application/desktop/DesktopExportLaunch.ts b/src/application/desktop/DesktopExportLaunch.ts new file mode 100644 index 0000000000..84b40ec2f9 --- /dev/null +++ b/src/application/desktop/DesktopExportLaunch.ts @@ -0,0 +1,65 @@ +import { + launchCommand, + type DesktopAppExportPlan, +} from "../../domain/desktop/appExport.js"; +import { electronLaunchArguments } from "../../domain/desktop/appExportInstall.js"; +import type { DesktopApplicationCapture } from "../../domain/desktop/desktopCapture.js"; +import { + versionAt, + type DesktopApplicationState, +} from "../../domain/desktop/desktopState.js"; +import { isWithinAny } from "../../domain/desktop/desktopStateDiff.js"; +import { openSeedDocument } from "../../domain/desktop/seedDocument.js"; +import { isTextualFormat } from "../../domain/desktop/stateFormats.js"; +import type { SeedVersion } from "./DesktopExportFiles.js"; + +type StatePath = DesktopApplicationState["paths"][number]; + +/** The owned files present at the seed with their digests, and those read as text. */ +export const seedVersions = ( + files: ReadonlyMap, + paths: readonly string[], + seed: number, +): { + readonly seeds: ReadonlyMap; + readonly textSeeds: readonly string[]; +} => { + const seeds = new Map(); + const textSeeds: string[] = []; + for (const [home, path] of files) { + const version = versionAt(path, seed); + if (!isWithinAny(home, paths) || version?.present !== true) continue; + if (version.sha256 === null) continue; + seeds.set(home, { sha256: version.sha256, format: version.format }); + if (isTextualFormat(version.format)) textSeeds.push(home); + } + return { seeds, textSeeds: textSeeds.sort() }; +}; + +/** The launch command, opening the seed's one document when the app takes files. */ +export const planLaunch = ( + { application, host }: DesktopApplicationCapture, + electron: boolean, + seeds: ReadonlyMap, + files: ReadonlyMap, +): Pick => { + const isDocument = (home: string) => files.get(home)?.role === "document"; + const launch = openSeedDocument( + electronLaunchArguments(application.argv, electron), + { + home: host.home, + workingDirectory: application.working_directory, + exec: application.desktop_entry?.exec ?? null, + documents: [...seeds.keys()].filter(isDocument).sort(), + isDocument, + }, + ); + return { + command: launchCommand( + launch.argv, + host.home, + application.working_directory, + ), + opens: launch.opens, + }; +}; diff --git a/src/cli/coreNativeCommands.ts b/src/cli/coreNativeCommands.ts index ac94085e69..7bc6589cdb 100644 --- a/src/cli/coreNativeCommands.ts +++ b/src/cli/coreNativeCommands.ts @@ -336,7 +336,9 @@ export const registerCoreNativeCommands = ( cli.command(CLI_COMMANDS.inspectNativeDataType, { description: "Inspect one recovered database type or typed data object", args: z.object({ - path: localPathStringSchema.describe("Local native executable or app path"), + path: localPathStringSchema.describe( + "Local native executable or app path", + ), }), options: z.object({ type: z.string().optional().describe("Exact database type category path"), diff --git a/src/cli/desktopCaptureCommands.ts b/src/cli/desktopCaptureCommands.ts index 0516dbe89c..3d62e592dd 100644 --- a/src/cli/desktopCaptureCommands.ts +++ b/src/cli/desktopCaptureCommands.ts @@ -168,6 +168,12 @@ const registerDesktopExportCommand = ( .describe( "http or https URL the computer downloads the captured --package-file from when the bundle is put, for an application installed from a .deb file", ), + syntheticSeed: z + .boolean() + .default(false) + .describe( + "The seed was made for the task, as in a build computer, not taken from the user's own data; the README says so instead of keeping the bundle local", + ), ...checkpointOptions, }), run: ({ args, options }) => @@ -194,6 +200,7 @@ const registerDesktopExportCommand = ( packageUrl: options.packageUrl, seedCheckpoint: options.seedCheckpoint, endCheckpoint: options.endCheckpoint, + syntheticSeed: options.syntheticSeed, }, readers, signal, diff --git a/src/desktop/BuildComputerController.ts b/src/desktop/BuildComputerController.ts index f5670c044a..2bb829d4ba 100644 --- a/src/desktop/BuildComputerController.ts +++ b/src/desktop/BuildComputerController.ts @@ -90,7 +90,8 @@ export const controller = async ( /** * Run one `rea` command in the computer with its output in a file under the - * work folder, after writing the files it reads; the output's size. + * work folder, after writing the files it reads. Preserve the exit status and + * stderr alongside the output size so callers can diagnose command failures. */ export const runRea = async ( computer: ControlledComputer, @@ -99,9 +100,14 @@ export const runRea = async ( readonly work: string; readonly stdout: string; readonly files?: Readonly>; + readonly timeoutS?: number; readonly signal: AbortSignal; }, -): Promise => { +): Promise<{ + readonly stdoutBytes: number; + readonly exitCode: number | null; + readonly stderr: string; +}> => { const answer = answerSchema.parse( await runBuildController( computer, @@ -111,16 +117,20 @@ export const runRea = async ( argv: [...argv], stdout: options.stdout, files: { ...options.files }, - timeout_s: 1800, + timeout_s: options.timeoutS ?? 1800, }, - { timeoutS: 1800, signal: options.signal }, + { timeoutS: options.timeoutS ?? 1800, signal: options.signal }, ), ); if (answer.exit_code === undefined || answer.stdout_bytes === undefined) throw new Error( answer.error ?? `rea ${argv[0] ?? ""} did not finish in the computer.`, ); - return answer.stdout_bytes; + return { + stdoutBytes: answer.stdout_bytes, + exitCode: answer.exit_code, + stderr: answer.stderr ?? "", + }; }; const listedSchema = z.object({ listing_bytes: z.int().nonnegative() }); diff --git a/src/domain/desktop/appExport.test.ts b/src/domain/desktop/appExport.test.ts index b0bccc2324..748023955f 100644 --- a/src/domain/desktop/appExport.test.ts +++ b/src/domain/desktop/appExport.test.ts @@ -5,13 +5,17 @@ import { className, environmentName, launchCommand, + launchDocumentLimitation, + launchDocumentNote, ownedPaths, renderAppClass, renderEnvToml, renderInstallScript, renderReadme, + seedWording, type DesktopAppExportPlan, } from "./appExport.js"; +import type { LaunchDocument } from "./seedDocument.js"; const plan: DesktopAppExportPlan = { name: "mousepad", @@ -26,10 +30,12 @@ const plan: DesktopAppExportPlan = { packageFile: null, electron: false, command: ["mousepad"], + opens: { kind: "empty" }, windowClass: "Mousepad", paths: [".config/Mousepad", "Documents"], capturedHome: "/home/ada", seedCheckpoint: "baseline", + syntheticSeed: false, seedFiles: ["Documents/todo.txt"], textSeeds: ["Documents/todo.txt"], unseededFiles: [], @@ -309,3 +315,72 @@ describe("renderers", () => { ); }); }); + +describe("README and class wording", () => { + it("adds one README line on what the launch opens: the seed's document, or why it opens none", () => { + const command = launchCommand( + ["mousepad", "/home/ada/Documents/todo.txt"], + "/home/ada", + "/home/ada", + ); + const readme = (opens: LaunchDocument) => + renderReadme({ ...plan, command, opens }); + const bare = readme({ kind: "empty" }); + const seed = { kind: "seed", document: "Documents/todo.txt" } as const; + const none = { + kind: "none", + reason: "the test gives this reason", + } as const; + + expect(command.join(" ")).toContain(`"$HOME"/'Documents/todo.txt'`); + expect(bare).toContain(`\`${command.join(" ")}\``); + for (const opens of [seed, none]) { + const note = launchDocumentNote({ opens }); + expect(note).not.toBe(""); + expect(readme(opens).replace(note, "")).toBe(bare); + } + expect(readme(seed)).toContain("`~/Documents/todo.txt`"); + expect(bare).not.toContain("~/Documents/todo.txt"); + const limitation = launchDocumentLimitation(none); + expect(limitation).toContain(none.reason); + expect(launchDocumentNote({ opens: none })).toContain(limitation); + expect(readme({ kind: "captured" })).toBe(bare); + for (const opens of [ + seed, + { kind: "captured" }, + { kind: "empty" }, + ] as const) + expect(launchDocumentLimitation(opens)).toBeUndefined(); + }); + + it("says the seed is the user's own data to keep local, unless it was made for the task", () => { + const own = { ...plan, seedCheckpoint: "drafted" }; + const made = { ...own, syntheticSeed: true }; + const wording = { own: seedWording(own), made: seedWording(made) }; + // The README's seed line and closing notice, and the class's docstring. + const renderedIn = { + origin: renderReadme, + notice: renderReadme, + docstring: renderAppClass, + } satisfies Record< + keyof typeof wording.own, + (from: DesktopAppExportPlan) => string + >; + + for (const [from, kept, other] of [ + [own, wording.own, wording.made], + [made, wording.made, wording.own], + ] as const) + for (const key of ["origin", "notice", "docstring"] as const) { + const rendered = renderedIn[key](from); + expect(rendered).toContain(kept[key]); + expect(rendered).not.toContain(other[key]); + } + for (const { origin, docstring } of [wording.own, wording.made]) { + expect(origin).toContain("`drafted`"); + expect(docstring).toContain("drafted"); + } + expect(renderReadme(made)).toContain("`envs/mousepad/seed/`"); + expect(renderReadme(own)).not.toContain("`envs/mousepad/seed/`"); + }); +}); diff --git a/src/domain/desktop/appExport.ts b/src/domain/desktop/appExport.ts index 790db86b6c..c9062d9e71 100644 --- a/src/domain/desktop/appExport.ts +++ b/src/domain/desktop/appExport.ts @@ -14,6 +14,8 @@ import type { DesktopApplicationCapture } from "./desktopCapture.js"; import type { DesktopApplicationState } from "./desktopState.js"; import { isWithinAny } from "./desktopStateDiff.js"; import { SEED_HOME } from "./hostIdentity.js"; +import { capturedFileUrl, type LaunchDocument } from "./seedDocument.js"; +import { posix } from "node:path"; import { stateDirectoryOf } from "./stateRoles.js"; /** agentenv-framework release the exported class was checked against. */ @@ -42,12 +44,16 @@ export interface DesktopAppExportPlan { /** Whether the app is Electron, whose launch then runs without Chromium's sandbox. */ readonly electron: boolean; readonly command: readonly string[]; + /** What the launch opens: the seed's one document, a file the captured command names, or nothing. */ + readonly opens: LaunchDocument; readonly windowClass: string; /** Home-relative paths the app owns for data add, get and reset. */ readonly paths: readonly string[]; readonly capturedHome: string; /** The checkpoint whose state the seed holds. */ readonly seedCheckpoint: string; + /** Whether the seed was made for the task, as in a build computer, rather than taken from the user's own data. */ + readonly syntheticSeed: boolean; /** Home-relative files copied into the computer's home before the first launch. */ readonly seedFiles: readonly string[]; /** Seed files read as text, which get the seed's home path respelled. */ @@ -241,12 +247,37 @@ export const launchCommand = ( capturedHome: string, workingDirectory: string, ): readonly string[] => { + const homeUrl = (value: string): string | undefined => { + const parsed = capturedFileUrl(value); + if (parsed === undefined) return undefined; + const { url, path } = parsed; + const root = posix.normalize(capturedHome); + if ( + path !== root && + !(root === "/" ? path.startsWith("/") : path.startsWith(`${root}/`)) + ) + return undefined; + const segments = url.pathname.split("/"); + const count = root.split("/").filter(Boolean).length; + const encodedSuffix = segments.slice(count + 1).join("/"); + const suffix = + (segments.length > count + 1 ? `/${encodedSuffix}` : "") + + url.search + + url.hash; + // The framework executes this command in a Python environment. Encode the + // runtime home as a URL path; shell quoting alone does not escape # or spaces. + const encodedHome = `"$(python3 -I -c 'import os,urllib.parse;print(urllib.parse.quote(os.environ["HOME"],safe="/"),end="")')"`; + const prefix = + /^file:\/\/(?:localhost)?(?=\/)/iu.exec(value)?.[0] ?? "file://"; + return `${shellQuote(prefix)}${encodedHome}${shellQuote(suffix)}`; + }; const underHome = (path: string) => - path === capturedHome + homeUrl(path) ?? + (path === capturedHome ? `"$HOME"` : path.startsWith(`${capturedHome}/`) ? `"$HOME"/${shellQuote(path.slice(capturedHome.length + 1))}` - : undefined; + : undefined); const respell = (argument: string) => { const equals = argument.indexOf("="); const value = @@ -355,13 +386,32 @@ export const renderInstallScript = (file: ExportPackageFile): string => "", ].join("\n"); +/** How the class and README say where the seed came from: made for the task, or the user's own data, kept local. */ +export const seedWording = ({ + name, + seedCheckpoint, + syntheticSeed, +}: Pick) => + syntheticSeed + ? { + docstring: `The seed was made for the task, as saved at checkpoint ${docText(seedCheckpoint)}.`, + origin: `made for the task, as saved at checkpoint \`${seedCheckpoint}\``, + notice: `The seed was made for this task, not taken from a user's own data; read \`envs/${name}/seed/\` before publishing the bundle.`, + } + : { + docstring: `The seed is the user's state at checkpoint ${docText(seedCheckpoint)}: keep this bundle local.`, + origin: `from the user's home at checkpoint \`${seedCheckpoint}\``, + notice: + "The seed is the user's real data. Keep this bundle local; do not publish it.", + }; + /** The computer app class: install, launch, owned paths, seed and data hooks. */ export const renderAppClass = (plan: DesktopAppExportPlan): string => { const hooks = classBody(plan); return `"""${docText(plan.title)} as an agentenv computer app, exported by REA from a desktop application capture. Package: ${plan.aptPackage}${plan.packageVersion === null ? "" : ` (captured at ${plan.packageVersion})`}. Capture Evidence: ${plan.captureEvidenceId}. -The seed is the user's state at checkpoint ${docText(plan.seedCheckpoint)}: keep this bundle local. +${seedWording(plan).docstring} """ ${PYTHON_IMPORTS} @@ -451,6 +501,27 @@ const installNotes = (plan: DesktopAppExportPlan): string => .map((line) => `\n${line}`) .join(""); +/** The start of the limitation of a launch that opens no document; its reason follows. */ +export const NO_LAUNCH_DOCUMENT = "The app opens no document at launch"; + +/** The limitation of a launch that opens no document; none when it opens one. */ +export const launchDocumentLimitation = ( + opens: LaunchDocument, +): string | undefined => + opens.kind === "none" + ? `${NO_LAUNCH_DOCUMENT}, since ${opens.reason}.` + : undefined; + +/** The README line on what the launch opens; none when the captured command names its own file. */ +export const launchDocumentNote = ({ + opens, +}: Pick): string => + opens.kind === "seed" + ? `\n- At launch the app opens \`~/${opens.document}\`, the seed's one document.` + : opens.kind === "none" + ? `\n- ${launchDocumentLimitation(opens)}` + : ""; + /** Bundle files: seeds the export rewrote to remove the capture host's identity, and files that still hold it. */ export interface SeedScrubReport { readonly scrubbed: readonly string[]; @@ -490,10 +561,10 @@ ${DESKTOP_EXPORT_FRAMEWORK_VERSION}. - \`envs/${plan.name}/\` is a computer app on the \`ubuntu-24.04\` base with the \`reference\` driver, for ${plan.architectures.map((architecture) => `\`${architecture}\``).join(" and ")}. It installs ${installs(plan)}, launches \`${plan.command.join(" ")}\` at boot and is ready - when a window of class \`${plan.windowClass}\` appears.${installNotes(plan)} + when a window of class \`${plan.windowClass}\` appears.${installNotes(plan)}${launchDocumentNote(plan)} - Owned paths: ${plan.paths.length === 0 ? "none" : plan.paths.map((path) => `\`~/${path}\``).join(", ")}. \`data/get\` returns their files and a typed JSON view of each; \`data/reset\` restores the seed. -- Seed: ${plan.seedFiles.length} file(s) from the user's home at checkpoint \`${plan.seedCheckpoint}\`${plan.dconf.length === 0 ? "" : ", and the application's dconf keys"}. +- Seed: ${plan.seedFiles.length} file(s) ${seedWording(plan).origin}${plan.dconf.length === 0 ? "" : ", and the application's dconf keys"}. Text files and dconf keys spell the capture host's name as \`localhost\`, \`user@host\` as \`user@localhost\` and the home as \`${SEED_HOME}\`, which the computer respells to its own at boot.${scrubNotes(scrub)}${ plan.unseededFiles.length === 0 @@ -506,7 +577,7 @@ ${DESKTOP_EXPORT_FRAMEWORK_VERSION}. ${taskSection.length === 0 ? "Run it with `agent-env run `." : "Run the task with `agent-env run `, or only the deploy with `--task smoke`."} -The seed is the user's real data. Keep this bundle local; do not publish it. +${seedWording(plan).notice} ${plan.dconf.length === 0 ? "" : "dconf keys are compiled into the user's dconf database, with `dconf-cli`, once when the app first starts; `data/reset` does not restore them.\n"}${taskSection}`; /** A smoke task that deploys the app. */ diff --git a/src/domain/desktop/desktopEntry.ts b/src/domain/desktop/desktopEntry.ts index 29a3464cc6..4733c1782e 100644 --- a/src/domain/desktop/desktopEntry.ts +++ b/src/domain/desktop/desktopEntry.ts @@ -207,6 +207,12 @@ export interface DesktopExecContext { const FILE_FIELD_CODES = new Set(["%f", "%F", "%u", "%U"]); +/** Whether an `Exec` value takes files: it holds a `%f`, `%F`, `%u` or `%U` field code. */ +export const execTakesFiles = (exec: string): boolean => { + const tokens = tokenizeDesktopExec(exec); + return tokens.ok && tokens.value.some((token) => FILE_FIELD_CODES.has(token)); +}; + /** * Expand field codes for a launch with no files, then append caller arguments. * File and URL codes are dropped, so the caller's arguments take their place. diff --git a/src/domain/desktop/seedDocument.test.ts b/src/domain/desktop/seedDocument.test.ts new file mode 100644 index 0000000000..15f13e9f87 --- /dev/null +++ b/src/domain/desktop/seedDocument.test.ts @@ -0,0 +1,157 @@ +import { describe, expect, it } from "vitest"; + +import { launchCommand } from "./appExport.js"; +import { openSeedDocument } from "./seedDocument.js"; + +const HOME = "/home/ada"; +const poster = ["Documents/poster.svg"]; +/** The capture's home and working directory, with documents under `Documents`. */ +const AT_HOME = { + home: HOME, + workingDirectory: HOME, + isDocument: (path: string) => path.startsWith("Documents/"), +}; + +/** The reason given for a launch that opens no document, which must not be blank. */ +const reasonOf = ({ opens }: ReturnType): string => { + if (opens.kind !== "none") + throw new Error(`the launch opens ${opens.kind}, not none`); + expect(opens.reason.trim()).not.toBe(""); + return opens.reason; +}; + +it("keeps a captured file URL without appending the same document", () => { + const url = `file://${HOME}/Documents/poster%20one.svg?mode=edit#page`; + const argv = ["inkscape", url]; + expect( + openSeedDocument(argv, { + ...AT_HOME, + exec: "inkscape %U", + documents: ["Documents/poster one.svg"], + }), + ).toEqual({ argv, opens: { kind: "captured" } }); +}); + +it("recognizes a captured relative filename containing a colon", () => { + const argv = ["app", "Budget:2026.svg"]; + expect( + openSeedDocument(argv, { + ...AT_HOME, + workingDirectory: `${HOME}/Documents`, + exec: "app %U", + documents: ["Documents/Budget:2026.svg"], + isDocument: (path) => path === "Documents/Budget:2026.svg", + }), + ).toEqual({ argv, opens: { kind: "captured" } }); +}); + +describe("openSeedDocument", () => { + it("appends the seed's one document when Exec takes files, respelled under $HOME by the launch", () => { + const opened = openSeedDocument(["inkscape"], { + ...AT_HOME, + exec: "inkscape %F", + documents: poster, + }); + + expect(opened).toEqual({ + argv: ["inkscape", `${HOME}/Documents/poster.svg`], + opens: { kind: "seed", document: "Documents/poster.svg" }, + }); + expect(launchCommand(opened.argv, HOME, HOME)).toEqual([ + "sh", + "-c", + `cd "$HOME" 2>/dev/null; exec 'inkscape' "$HOME"/'Documents/poster.svg'`, + ]); + }); + + it.each(["mousepad %U", "app %f", "env A=1 app %u", "app %U --new-window"])( + "opens the document for Exec %j", + (exec) => { + expect( + openSeedDocument(["app"], { ...AT_HOME, exec, documents: poster }) + .opens, + ).toEqual({ kind: "seed", document: "Documents/poster.svg" }); + }, + ); + + it("keeps the launch and says why when the seed holds several documents, and opens none when it holds none", () => { + const documents = ["Documents/a.svg", "Documents/b.svg"]; + const several = openSeedDocument(["inkscape"], { + ...AT_HOME, + exec: "inkscape %F", + documents, + }); + + expect(several.argv).toEqual(["inkscape"]); + const reason = reasonOf(several); + for (const document of documents) expect(reason).toContain(`~/${document}`); + expect( + openSeedDocument(["inkscape"], { + ...AT_HOME, + exec: "inkscape %F", + documents: [], + }), + ).toEqual({ + argv: ["inkscape"], + opens: { kind: "empty" }, + }); + }); + + it("keeps the launch and says why when Exec takes no file or there is no desktop entry", () => { + const reasons = ["xcalc", "app %i %c", null].map((exec) => { + const opened = openSeedDocument(["app"], { + ...AT_HOME, + exec, + documents: poster, + }); + expect(opened.argv).toEqual(["app"]); + return reasonOf(opened); + }); + + // Both Execs that take no file have the same cause. + expect(reasons[1]).toBe(reasons[0]); + }); + + it("gives each cause of opening no document its own reason", () => { + const reasons = [ + { exec: "app %F", documents: ["Documents/a.svg", "Documents/b.svg"] }, + { exec: "xcalc", documents: poster }, + { exec: null, documents: poster }, + ].map((facts) => + reasonOf(openSeedDocument(["app"], { ...AT_HOME, ...facts })), + ); + + expect(new Set(reasons).size).toBe(reasons.length); + }); + + it("leaves a command that already names a document alone, absolute or from its working directory", () => { + for (const [argv, workingDirectory] of [ + [["typora", `${HOME}/Documents/trip.md`], HOME], + [["typora", "Documents/trip.md"], HOME], + [["typora", "trip.md"], `${HOME}/Documents`], + ] as const) + expect( + openSeedDocument(argv, { + ...AT_HOME, + workingDirectory, + exec: "typora %U", + documents: poster, + }), + ).toEqual({ argv, opens: { kind: "captured" } }); + }); + + it("still opens the seed's document when the command names only settings under the home", () => { + const argv = ["geany", "--config", `${HOME}/.config/geany`]; + + expect( + openSeedDocument(argv, { + ...AT_HOME, + exec: "geany %F", + documents: poster, + }), + ).toEqual({ + argv: [...argv, `${HOME}/Documents/poster.svg`], + opens: { kind: "seed", document: "Documents/poster.svg" }, + }); + }); +}); diff --git a/src/domain/desktop/seedDocument.ts b/src/domain/desktop/seedDocument.ts new file mode 100644 index 0000000000..975c6fa9d0 --- /dev/null +++ b/src/domain/desktop/seedDocument.ts @@ -0,0 +1,102 @@ +import { posix } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { execTakesFiles } from "./desktopEntry.js"; + +/** What the exported app opens at launch. */ +export type LaunchDocument = + /** The seed's one document, by home path. */ + | { readonly kind: "seed"; readonly document: string } + /** A document the captured command already names. */ + | { readonly kind: "captured" } + /** Nothing, since the seed holds no document. */ + | { readonly kind: "empty" } + /** No document, for the reason given, which follows "since". */ + | { readonly kind: "none"; readonly reason: string }; + +/** Parse a captured Linux file URL independently of the exporter host OS. */ +export const capturedFileUrl = ( + value: string, +): { readonly url: URL; readonly path: string } | undefined => { + if (!/^file:/iu.test(value)) return undefined; + try { + const url = new URL(value); + return url.protocol === "file:" + ? { url, path: fileURLToPath(url, { windows: false }) } + : undefined; + } catch { + return undefined; + } +}; + +/** The home path an argument names, absolute or from the working directory; none for an option or a path outside the home. */ +const homePathOf = ( + argument: string, + home: string, + workingDirectory: string, + known: (path: string) => boolean, +): string | undefined => { + if (argument === "" || argument.startsWith("-")) return undefined; + let input = argument; + if (/^[A-Za-z][A-Za-z0-9+.-]*:/u.test(argument)) { + const candidate = posix.resolve(workingDirectory, argument); + if (candidate.startsWith(`${home}/`)) { + const relative = candidate.slice(home.length + 1); + if (known(relative)) return relative; + } + const parsed = capturedFileUrl(argument); + if (parsed === undefined) return undefined; + input = parsed.path; + } + const path = posix.resolve(workingDirectory, input); + return path.startsWith(`${home}/`) ? path.slice(home.length + 1) : undefined; +}; + +/** + * The launch arguments, with the seed's document appended when the seed holds + * exactly one and the desktop entry's `Exec` takes a file, so the app opens on + * it rather than on its start screen. A command that already names a document, + * absolute or from its working directory, is kept as it is. + */ +export const openSeedDocument = ( + argv: readonly string[], + facts: { + readonly home: string; + readonly workingDirectory: string; + readonly exec: string | null; + readonly documents: readonly string[]; + /** Whether a home path is a document the capture saw. */ + readonly isDocument: (homePath: string) => boolean; + }, +): { readonly argv: readonly string[]; readonly opens: LaunchDocument } => { + const { home, exec, documents } = facts; + const none = (reason: string) => ({ + argv, + opens: { kind: "none", reason } as const, + }); + const named = argv + .slice(1) + .map((argument) => + homePathOf(argument, home, facts.workingDirectory, facts.isDocument), + ); + if (named.some((path) => path !== undefined && facts.isDocument(path))) + return { argv, opens: { kind: "captured" } }; + const [document] = documents; + if (document === undefined) return { argv, opens: { kind: "empty" } }; + if (documents.length > 1) + return none( + `the seed holds ${String(documents.length)} documents (${documents.map((path) => `~/${path}`).join(", ")}) and the app opens one only when it holds exactly one`, + ); + if (exec === null) + return none( + "the capture launched the program without a desktop entry, so whether it takes a file is unknown", + ); + if (!execTakesFiles(exec)) + return none( + "its desktop entry's Exec takes no file (no %f, %F, %u or %U field code)", + ); + return { + argv: [...argv, `${home}/${document}`], + opens: { kind: "seed", document }, + }; +}; diff --git a/tests/boundary/mcp/buildCaptureMcp.test.ts b/tests/boundary/mcp/buildCaptureMcp.test.ts index eee2a49441..f9f9a4d0f5 100644 --- a/tests/boundary/mcp/buildCaptureMcp.test.ts +++ b/tests/boundary/mcp/buildCaptureMcp.test.ts @@ -4,6 +4,8 @@ import { join } from "node:path"; import { expect, it } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; +import { seedWording } from "../../../src/domain/desktop/appExport.js"; +import { parseEvidence } from "../../../src/domain/evidence.js"; import { BUILD_REQUEST as REQUEST, BUILD_TASK as TASK, @@ -140,6 +142,7 @@ it("captures the app inside the computer, acknowledges checkpoints, drafts the t agentenv: string[]; }[]; expect(calls.map(({ argv }) => argv[0])).toEqual([ + "export-desktop-application", "capture-desktop-application", "describe-desktop-application-task", "export-desktop-application", @@ -149,7 +152,7 @@ it("captures the app inside the computer, acknowledges checkpoints, drafts the t agentenv: [], apt_config: join(rea.home, ".rea-build/apt/apt.conf"), }); - expect(calls[0]?.argv).toEqual( + expect(calls[1]?.argv).toEqual( expect.arrayContaining([ "--desktop-entry", "org.example.notes", @@ -157,7 +160,7 @@ it("captures the app inside the computer, acknowledges checkpoints, drafts the t join(rea.home, ".rea-build"), ]), ); - expect(calls.slice(1).map(({ argv }) => argv)).toEqual([ + expect(calls.slice(2).map(({ argv }) => argv)).toEqual([ expect.arrayContaining([ "--seed-checkpoint", "seed", @@ -169,11 +172,63 @@ it("captures the app inside the computer, acknowledges checkpoints, drafts the t "seed", "--end-checkpoint", "end", + // The agent made the seed in the computer, so the README need not keep it local. + "--synthetic-seed", ]), ]); + expect( + parseEvidence(JSON.parse(await readFile(outputs.export, "utf8"))) + .parameters, + ).toMatchObject({ synthetic_seed: true }); + expect(await readFile(join(directory, "README.md"), "utf8")).toContain( + seedWording({ + name: exported.environment, + seedCheckpoint: "seed", + syntheticSeed: true, + }).notice, + ); expectNoToken(computer, [started, seed, stopped, exported]); }); +it("refuses an older build runtime before capturing the app", async () => { + const { rea, client } = await fakes({ env: { FAKE_REA_OLD_EXPORT: "1" } }); + const id = await startBuildComputer(client, REQUEST); + const { error } = parseMcpToolError( + await client.callTool({ + name: "start_build_capture", + arguments: { computer_id: id }, + }), + ); + expect(error.message).toContain("does not support synthetic seeds"); + expect(JSON.stringify(error)).toContain("pack:build-computer-runtime"); + expect(JSON.stringify(error)).toContain("restart the REA MCP server"); + const calls = (await rea.notes("calls.jsonl")) as { argv: string[] }[]; + expect(calls.map(({ argv }) => argv[0])).not.toContain( + "capture-desktop-application", + ); +}); + +it("reports an unsuccessful runtime schema command without diagnosing its cause", async () => { + const { rea, client } = await fakes({ + env: { FAKE_REA_SCHEMA_FAILURE: "1" }, + }); + const id = await startBuildComputer(client, REQUEST); + const { error } = parseMcpToolError( + await client.callTool({ + name: "start_build_capture", + arguments: { computer_id: id }, + }), + ); + expect(error.message).toContain("exited 23"); + expect(error.message).toContain("runtime cannot load its export command"); + expect(error.message).not.toContain("does not support synthetic seeds"); + expect(JSON.stringify(error)).toContain("restart the REA MCP server"); + const calls = (await rea.notes("calls.jsonl")) as { argv: string[] }[]; + expect(calls.map(({ argv }) => argv[0])).not.toContain( + "capture-desktop-application", + ); +}); + it("refuses a task too large for one command argument before the export runs in the computer", async () => { const { rea, client } = await fakes(); const id = await startBuildComputer(client, REQUEST); @@ -193,9 +248,12 @@ it("refuses a task too large for one command argument before the export runs in ); expect(error.code).toBe("invalid_request"); const calls = (await rea.notes("calls.jsonl")) as { argv: string[] }[]; - expect(calls.map(({ argv }) => argv[0])).not.toContain( - "export-desktop-application", - ); + expect( + calls.some( + ({ argv }) => + argv[0] === "export-desktop-application" && !argv.includes("--schema"), + ), + ).toBe(false); }); it("refuses capture steps out of order and a capture of a bundle's computer", async () => { diff --git a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts index 1d4dafb956..5bab4970f4 100644 --- a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts +++ b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts @@ -91,7 +91,9 @@ it("returns a checkpoint taken in the computer when its reply was lost and the n }); it("finishes a stop whose draft reply was lost when stop is asked again, and repeats its result", async () => { - const { rea, client } = await fakes({ dropReplies: ["run"] }); + const { rea, client } = await fakes({ + dropReplies: ["run:describe-desktop-application-task"], + }); const id = await startBuildComputer(client, BUILD_REQUEST); await captureSeedAndEnd(client, id); const stop = () => call(client, "stop_build_capture", { computer_id: id }); @@ -250,7 +252,10 @@ const captureDebRequest = async ( ({ argv }) => argv, ); const argv = (command: string) => - calls.find((candidate) => candidate[0] === command) ?? []; + calls.find( + (candidate) => + candidate[0] === command && !candidate.includes("--schema"), + ) ?? []; expect(argv("capture-desktop-application")).toEqual( expect.arrayContaining(["--package-file", "/opt/rea/package.deb"]), ); diff --git a/tests/boundary/providers/desktopLaunchCommand.test.ts b/tests/boundary/providers/desktopLaunchCommand.test.ts new file mode 100644 index 0000000000..753296bdbd --- /dev/null +++ b/tests/boundary/providers/desktopLaunchCommand.test.ts @@ -0,0 +1,69 @@ +import { expect, it } from "vitest"; +import { spawnSync } from "node:child_process"; +import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; + +import { launchCommand } from "../../../src/domain/desktop/appExport.js"; + +it.skipIf(process.platform === "win32")( + "does not import seeded Python modules while encoding a file URL", + () => { + const home = mkdtempSync(join(tmpdir(), "rea-url-home-")); + try { + writeFileSync( + join(home, "urllib.py"), + 'open("seed-ran", "w").write("unexpected")\n', + ); + const command = launchCommand( + [ + process.execPath, + "-e", + "process.stdout.write(process.argv[1])", + "file:///home/ada/Documents/report.txt", + ], + "/home/ada", + "/home/ada", + ); + const result = spawnSync(command[0]!, command.slice(1), { + encoding: "utf8", + env: { ...process.env, HOME: home }, + }); + expect(result.status).toBe(0); + expect(result.stdout).toBe( + pathToFileURL(join(home, "Documents/report.txt")).href, + ); + expect(existsSync(join(home, "seed-ran"))).toBe(false); + } finally { + rmSync(home, { recursive: true, force: true }); + } + }, +); + +it.skipIf(process.platform === "win32")( + "launches captured file URLs under the runtime home with their encoded suffix, query, and fragment", + () => { + for (const prefix of ["file://", "file://localhost"]) { + const captured = `${prefix}/home/ada/Documents/%41%20budget.svg?mode=edit#page`; + const command = launchCommand( + [ + process.execPath, + "-e", + "process.stdout.write(process.argv[1])", + captured, + ], + "/home/ada", + "/home/ada", + ); + const result = spawnSync(command[0]!, command.slice(1), { + encoding: "utf8", + env: { ...process.env, HOME: "/tmp/Run #测" }, + }); + expect(result.status).toBe(0); + expect(result.stdout).toBe( + `${prefix}/tmp/Run%20%23%E6%B5%8B/Documents/%41%20budget.svg?mode=edit#page`, + ); + } + }, +); diff --git a/tests/fixtures/buildCaptureFakes.ts b/tests/fixtures/buildCaptureFakes.ts index de280b287b..c2a7f11ecc 100644 --- a/tests/fixtures/buildCaptureFakes.ts +++ b/tests/fixtures/buildCaptureFakes.ts @@ -53,6 +53,13 @@ const realCliOutputs = async (install: CapturedInstall | undefined) => { const task = join(work, "task.json"); await writeFile(task, JSON.stringify(BUILD_TASK)); const bundle = join(work, "bundle"); + const schema = await serveCli([ + "export-desktop-application", + "--schema", + "--json", + ]); + expect(schema.exitCode, schema.stdout).toBe(0); + await writeFile(join(work, "schema.json"), schema.stdout); const draft = await serveCli([ "describe-desktop-application-task", capture, @@ -66,6 +73,7 @@ const realCliOutputs = async (install: CapturedInstall | undefined) => { "--task", task, ...ENDS, + "--synthetic-seed", ...(install?.packageUrl === undefined ? [] : ["--package-url", install.packageUrl]), @@ -79,6 +87,7 @@ const realCliOutputs = async (install: CapturedInstall | undefined) => { draft: join(work, "draft.json"), export: join(work, "export.json"), bundle, + schema: join(work, "schema.json"), }; }; diff --git a/tests/fixtures/fakeAgentEnvComputer.ts b/tests/fixtures/fakeAgentEnvComputer.ts index a607ab673b..2032b735f6 100644 --- a/tests/fixtures/fakeAgentEnvComputer.ts +++ b/tests/fixtures/fakeAgentEnvComputer.ts @@ -304,7 +304,14 @@ const execRoute = async ( ) => { const answer = await runExec(exec, token, argv); const { dropReplies } = exec; - if (dropReplies === undefined || dropReplies[0] !== argv[4]) return answer; + const requested = dropReplies?.[0]; + const [command, operation] = requested?.split(":") ?? []; + if ( + command !== argv[4] || + (operation !== undefined && !argv[5]?.includes(`\"${operation}\"`)) + ) + return answer; + if (dropReplies === undefined) return answer; dropReplies.shift(); return LOST; }; diff --git a/tests/fixtures/fakeBuildComputerRea.ts b/tests/fixtures/fakeBuildComputerRea.ts index 76c91e405a..659d6f71bd 100644 --- a/tests/fixtures/fakeBuildComputerRea.ts +++ b/tests/fixtures/fakeBuildComputerRea.ts @@ -69,6 +69,16 @@ elif command == "describe-desktop-application-task": with open(os.environ["FAKE_REA_DRAFT"]) as handle: sys.stdout.write(handle.read()) elif command == "export-desktop-application": + if "--schema" in args: + if os.environ.get("FAKE_REA_SCHEMA_FAILURE"): + print("runtime cannot load its export command", file=sys.stderr) + sys.exit(23) + with open(os.environ["FAKE_REA_SCHEMA"]) as handle: + schema = json.load(handle) + if os.environ.get("FAKE_REA_OLD_EXPORT"): + schema["options"]["properties"].pop("syntheticSeed", None) + print(json.dumps(schema)) + sys.exit(0) with open(option(args, "--task")) as handle: note("tasks.jsonl", json.load(handle)) exported = os.environ["FAKE_REA_EXPORT"] @@ -116,6 +126,7 @@ export interface FakeReaOutputs { readonly draft: string; readonly export: string; readonly bundle: string; + readonly schema: string; } export interface FakeBuildComputerRea { @@ -155,6 +166,7 @@ export const fakeBuildComputerRea = async ( FAKE_REA_DRAFT: outputs.draft, FAKE_REA_EXPORT: outputs.export, FAKE_REA_BUNDLE: outputs.bundle, + FAKE_REA_SCHEMA: outputs.schema, }, notes: async (name) => { const path = join(notes, name);