Files
reverse-engineering-anything/src/application/desktop/DesktopGzipDocument.test.ts
T
d2f9b8b7de feat(desktop): build several tasks from one capture, and measure each task's pass rate within a budget (#9216)
* docs(desktop): clarify gzip scrub limits

* fix(setup): own desktop build process cleanup

* fix(doctor): compare desktop settings with resolved defaults

* fix(desktop): refuse a build runtime whose export takes no task ends

Several tasks pass --task-end to the build computer's export, so a runtime
packed before it now fails at the start of the capture with the repack fix,
as a runtime without synthetic seeds already did, rather than at export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style(desktop): format the noisy-record draft files with the pinned oxfmt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(desktop): give the root-array draft test its own describe

The records describe grew past the 150-line lint limit with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(doctor): inspect client-recorded desktop runners

* fix(doctor): reuse matching runner probes

* fix(setup): propagate cancellation to desktop actions

* fix(desktop): rewrite a download's launch keys written with spaces around =

The desktop entry parser accepts `Exec = notes %F` and an indented
`TryExec =notes`, so the install script now matches those keys the same way
instead of leaving the original program in place. Adds the feature's tasks.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(desktop): keep why a measured run failed in its solve record

A solve that returned an outcome dropped the runner's error, so a run that
failed to deploy, solve or verify was retried with no reason recorded. The
record, the measure result and difficulty.json now carry it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(setup): stop Hopper on cancel, never report a cancelled setup ready, and keep a failed action's client setting

- The Hopper install gets setup's signal: Linux already honoured it, and the
  macOS installer now stops its downloads and stops before mounting or copying.
- runSetup checks the signal after the final doctor and close, so a
  cancellation there raises instead of returning ready.
- A failed pack or agentenv install no longer writes its archive or runner to
  client configs, so the setting the client already had stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(setup): keep the completed setup result in a cancellation after cleanup

A cancellation during the final doctor or host close carried only the applied
actions, dropping each client's result and backup path and the doctor report.
It now carries the whole result as setup_result, as doctor's cancellation
carries its report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(desktop): move launch execution to boundary suite

* fix(desktop): redact build schema failure output

* refactor(desktop): normalize schema failure once

* fix(desktop): omit unsafe schema parser snippets

* fix(desktop): preserve draft change and selected path evidence

* test(desktop): group retained dconf note regressions

* fix(desktop): respect archive entry format in draft checks

* test(desktop): separate unowned-file draft note cases

* fix(desktop): preserve explicitly selected journal documents

* fix(xml): avoid scanning selected ancestors per tag

* fix(desktop): exclude covered text runs from draft counts

* fix: preserve text when drafting desktop checks

* fix: restore nested desktop state aliases

* refactor: share UUID recognition across draft checks

* fix: retain multiline XML attribute context

* fix: retain relationship identifiers in task drafts

* docs: clarify desktop draft identifier handling

* test(desktop): type formatting run cases as pairs

* fix(desktop): carry document format into selector evaluation

* test(desktop): separate markup boundary regression group

* fix(desktop): propagate formats through draft selectors

* fix(desktop): switch selector format within archive entries

* test(desktop): remove duplicate seed filename case

* fix(setup): retain diagnostics when cancellation overlaps failure

* refactor(setup): keep command cancellation at its owner

* test(setup): verify cancellation at the application boundary

* test(desktop): pass JSON format in exported index parity

* docs(desktop): clarify task checks before scratch export

* fix(desktop): respell selected paths in file URLs

* test(desktop): group captured document spelling cases

* test(desktop): run installer scripts at boundary

* style(desktop): remove trailing blank line

* fix(desktop): bound draft preview serialization

* fix(desktop): restore selected state aliases

* fix(desktop): respect entry format in draft noise

* test(desktop): match exported alias evidence at its JSON boundary

* fix(desktop): retain app state logs in exported seeds

* fix: align desktop numeric seed comparisons

* test(desktop): keep one draft exclusion fixture import

* test(desktop): retain log eligibility across draft test groups

* fix(doctor): resolve empty registered desktop runner

* style: fix desktop launch test formatting

* style: format native command path schema

* fix(desktop): map selected external launch paths

* style(desktop): format seed document regressions

* test(desktop): group launch URL regressions

* fix(desktop): omit redundant nested state aliases

* fix(desktop): compare added free text as a whole

* fix(desktop): check recompressed seeds as shipped

* docs(desktop): clarify recovered seed format

* test(desktop): group shipped gzip seed checks

* fix: retain quoted identifier text in task drafts

* fix: recognize Unicode XML in desktop state readers

* fix(desktop): bound nested draft checks

* fix(desktop): explain missing arrays in truncated views

* fix(desktop): find XML type references in archives

* test(desktop): remove duplicated capture assertions

* fix(desktop): retain nested omitted text boundaries

* style(desktop): format draft value imports

* test(desktop): group model and draft value checks

* fix(desktop): map captured launch paths once

* refactor: remove unused whole-text guard state

* refactor: stop passing unused text children

* test: align free-text checks with whole-note policy

* fix: validate recompressed desktop archive seeds

* style(desktop): format archive seed regression

* fix(desktop): retain placed-item sibling guards

* fix(desktop): preserve archive paths and bound doctor reads

* fix(doctor): refuse non-files before descriptor admission

* fix(setup): track remaining cleanup resources across retries

* test(desktop): specify XML format in heap pairing fixture

* fix(setup): skip Hopper launch after cancelled copy

* fix: preserve task measurement limits and cleanup

* fix(desktop): retain reported solve sandbox cleanup

* test(desktop): check retained log seed admission

* fix(doctor): query agentenv for model endpoint

* fix(doctor): preserve uncertainty for empty endpoint override

* fix(desktop): redact runtime schema errors

* fix(desktop): normalize captured file URLs

* fix(desktop): report seeded files by capture path

* fix(desktop): retain startup readiness failure stage

* fix(desktop): retain JSON date checks

* fix(desktop): preserve archive formatting checks

* fix(desktop): preserve explicitly selected journal files

* fix(desktop): report build command transport limits

* docs(desktop): clarify unreadable dconf versions

* test(desktop): remove duplicate history regression

* fix(desktop): align XML names with exported reader

* fix(desktop): create selected state alias targets

* fix(desktop): inspect supported runtime archives

* fix(desktop): keep ordinary text checks exact

* fix(desktop): verify registered build host paths

* fix(desktop): retain record identity in draft checks

* test(desktop): preserve text reference numbers

* fix(desktop): bound download install input reads

* fix(desktop): match placed notes by whole text

* test(desktop): match multiple task export results

* fix(desktop): retain failed measurement file closes

* fix(setup): retain completed actions on cancellation

* test(setup): retain cancelled installer result

* fix(desktop): scrub XML independently of reader compatibility

* fix(desktop): preserve file aliases and reader diagnostics

* style(desktop): format review fixes

* fix(desktop): inspect archives with registered runner

* test(desktop): verify registered archive interpreter

* style(desktop): format capture review fixes

* style(desktop): format review changes

* feat(desktop): target agentenv-framework 0.9.1464

Desktop bundles, setup's --install-agentenv and the doctor's exact-version
check now target 0.9.1464, which keeps an exported app's data/get call,
holds the computer fixture above boot dialogs, leaves an unresolved setting's
build unverified rather than failed, and mounts lock packages instead of
copying them into the image. The web environment lane stays on 0.9.1431.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(desktop): retain per-solve sandbox cleanup roots

* test(desktop): expect the solve cleanup helper among the shipped agentenv helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(desktop): clean up a stopped solve's sandboxes through agentenv 0.9.1482

agentenv-framework 0.9.1482 lists the local sandboxes under a root
(local_sandbox_ids) and rebuilds one killed while it wrote its port map for
teardown. The solve cleanup helper now terminates each sandbox agentenv
lists, keeping its Docker and host-process checks, and reports anything else
left under the root. Desktop bundles, setup's --install-agentenv and the
doctor's version check target 0.9.1482.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(desktop): verify a killed solve's cleanup once its sandbox root is empty

A solve's sandboxes are all local under its private root, and agentenv now
keeps a sandbox's folder until its teardown is confirmed. So when the helper
ends without a report and REA's cleanup empties that root, the cleanup is
verified: a cancelled solve ends as a cancellation and a solve past its
deadline as a failed run, rather than as cleanup the measure cannot vouch for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(desktop): target agentenv-framework 0.9.1485

0.9.1485 keeps a local sandbox's folder until its teardown is confirmed, which
the solve cleanup's verified result relies on. (0.9.1484 failed to publish.)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(desktop): record a solve stopped before it reports as failed, not as a refused request

A helper killed from outside or stopped at its deadline left no report, so the
measure refused with invalid input and told the caller to fix its model
endpoint. A helper a signal ended now gives a failed solve of unknown cost,
which the measure records and stops on; one that exits on its own without a
report still ends the measure as a solve that could not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(desktop): check verifier behavior after selector changes

* test(windows): verify rejected writes during cleanup by effect

* fix(desktop): preserve selected alias target kinds in exports

* fix(cli): retain caller capture paths in environment builds

* fix(ghidra): classify oversized endpoint records consistently

* test: align model and Evidence expectations with current contracts

* fix(desktop): reconcile upstream sandbox cleanup recovery

* fix(docs): link cleanup qualification as repository evidence

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 05:26:28 -05:00

335 lines
12 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { gunzipSync, gzipSync } from "node:zlib";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import type { DesktopApplicationCapture } from "../../domain/desktop/desktopCapture.js";
import { desktopApplicationStateSchema } from "../../domain/desktop/desktopState.js";
import { openStateDocument } from "../../domain/desktop/gzipDocument.js";
import { exportDesktopApplication } from "./DesktopApplicationExport.js";
import { inspectDesktopApplicationState } from "./DesktopApplicationState.js";
import { draftDesktopApplicationTask } from "./DesktopTaskDraft.js";
import {
HOME,
capture,
change,
readers,
retained,
writeCaptureFolder,
} from "./desktopCapture.fixture.js";
const BUDGET = "Documents/Budget.gnumeric";
const HOSTNAME = "e81232be457d";
const SEED = "envs/notes/seed/Documents/Budget.gnumeric";
const CELLS =
"/root/children/[name=gnm:Sheets]/children/[name=gnm:Sheet][>gnm:Name=Budget]/children/[name=gnm:Cells]/children";
/** A Gnumeric workbook's XML with its rent, author and any comment as given. */
const workbook = (
rent: string,
author: string,
comment = "",
) => `<?xml version="1.0" encoding="UTF-8"?>
<gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd">
<gnm:Summary><gnm:Item><gnm:name>author</gnm:name><gnm:val-string>${author}</gnm:val-string></gnm:Item></gnm:Summary>
<gnm:Comment>${comment}</gnm:Comment>
<gnm:Sheets><gnm:Sheet><gnm:Name>Budget</gnm:Name><gnm:Cells>
<gnm:Cell Row="0" Col="0" ValueType="60">Rent</gnm:Cell>
<gnm:Cell Row="0" Col="1" ValueType="40">${rent}</gnm:Cell>
</gnm:Cells></gnm:Sheet></gnm:Sheets>
</gnm:Workbook>
`;
/**
* The notes capture of a host with the workbook, named in a window title,
* saved before and after the user changed the rent and set the author to the
* value the scrub writes, each save made by `save`, with any other files the
* app kept unchanged by their home path.
*/
const budgetCapture = (
save: (text: string) => Buffer,
comment = "",
kept: Readonly<Record<string, Buffer>> = {},
) => {
const before = save(workbook("1200", `ada@${HOSTNAME}`, comment));
const after = save(workbook("1350", "user@localhost", comment));
const base = capture();
const [first, second] = base.state.checkpoints;
if (first === undefined || second === undefined) throw new Error("fixture");
const path = `${HOME}/${BUDGET}`;
const captured: DesktopApplicationCapture = {
...base,
host: { ...base.host, hostname: HOSTNAME, user: "ada" },
session: {
...base.session,
windows: base.session.windows.map((window) => ({
...window,
titles: [...window.titles, "Budget.gnumeric - Notes"],
})),
},
state: {
...base.state,
checkpoints: [
{
...first,
retained: [
...first.retained,
retained(path, before),
...Object.entries(kept).map(([home, bytes]) =>
retained(`${HOME}/${home}`, bytes),
),
],
},
{
...second,
changes: [...second.changes, change(path, { before, after })],
retained: [...second.retained, retained(path, after)],
},
],
},
};
return writeCaptureFolder(captured, undefined, [
before,
after,
...Object.values(kept),
]);
};
const gzipped = (text: string) => gzipSync(text);
const plain = (text: string) => Buffer.from(text);
/** The workbook's path in the capture's state view. */
const budgetState = async (folder: string) => {
const evidence = await inspectDesktopApplicationState(folder, readers);
if (!("normalized_result" in evidence)) throw evidence;
return desktopApplicationStateSchema
.parse(evidence.normalized_result)
.paths.find((path) => path.home_path === BUDGET);
};
const draftOf = async (folder: string) => {
const drafted = await draftDesktopApplicationTask(folder, readers);
if (drafted instanceof Error) throw drafted;
return drafted.normalized_result;
};
/** Export the capture with a task that checks the rent and the author the scrub writes. */
const exportBudget = async (folder: string) => {
const scratch = await createTestTempDirectory("desktop-gzip-task-");
const task = join(scratch, "task.json");
const directory = join(scratch, "bundle");
await writeFile(
task,
JSON.stringify({
name: "rent",
prompt: "Set the rent to 1350 in my budget and save it.",
checks: [
{
id: "rent",
kind: "value_equals",
file: BUDGET,
pointer: `${CELLS}/[name=gnm:Cell][@Row=0][@Col=1]/children/0`,
value: "1350",
},
{
id: "author",
kind: "text_equals",
file: BUDGET,
pointer:
"/root/children/[name=gnm:Summary]/children/[name=gnm:Item]/children/[name=gnm:val-string]",
value: "user@localhost",
},
],
}),
);
const result = await exportDesktopApplication(
{ capture: folder, directory, name: undefined, tasks: [task] },
readers,
);
return { result, directory };
};
describe("documents saved gzip-compressed", () => {
it("reads each version as the XML inside, the view the same workbook has uncompressed, and records the compression", async () => {
const compressed = await budgetState(await budgetCapture(gzipped));
const uncompressed = await budgetState(await budgetCapture(plain));
expect(compressed).toMatchObject({
role: "document",
format: "xml",
versions: [
{ format: "xml", compression: "gzip", view_error: null },
{ format: "xml", compression: "gzip", view_error: null },
],
changes: [{ kind: "values", changed_count: 2 }],
});
expect(uncompressed?.versions[0]?.compression).toBeUndefined();
expect(compressed?.versions.map(({ view }) => view)).toEqual(
uncompressed?.versions.map(({ view }) => view),
);
expect(compressed?.changes).toEqual(uncompressed?.changes);
});
it("drafts the same files, values, checks, guards and notes as the same workbook uncompressed", async () => {
const compressed = await draftOf(await budgetCapture(gzipped));
expect(compressed).toEqual(await draftOf(await budgetCapture(plain)));
expect(
(compressed as { task: { checks: unknown[] } }).task.checks,
).toContainEqual(
expect.objectContaining({
file: BUDGET,
pointer: `${CELLS}/[name=gnm:Cell][@Col=1]`,
value: "1350",
}),
);
});
it("ships the seed scrubbed inside and still gzip, copied as it is at boot, and checks the seed as shipped", async () => {
const { result, directory } = await exportBudget(
await budgetCapture(gzipped),
);
if (result instanceof Error) throw result;
expect(result.normalized_result).toMatchObject({
scrubbed_files: [SEED],
unscrubbed_files: [],
tasks: [{ failing_on_seed: ["rent"] }],
});
const shipped = await readFile(join(directory, SEED));
expect([...shipped.subarray(0, 4)]).toEqual([0x1f, 0x8b, 8, 0]);
expect(gunzipSync(shipped).toString("utf8")).toBe(
workbook("1200", "user@localhost"),
);
const module = await readFile(
join(directory, "envs/notes/notes_app.py"),
"utf8",
);
// Boot respells text seeds as UTF-8, so the gzip workbook is not one of them.
expect(module).toContain(
'TEXT_SEEDS = frozenset([".config/notes/settings.ini"])',
);
});
it("lists gzip seeds recompressed only to clear their headers apart from the document whose content was scrubbed", async () => {
const templates = ["blank", "invoice", "loan"].map(
(name) => `.local/share/notes/templates/${name}.xml.gz`,
);
const kept = Object.fromEntries(
templates.map((home) => [home, gzipSync(`<template name="${home}"/>`)]),
);
const { result, directory } = await exportBudget(
await budgetCapture(gzipped, "", kept),
);
if (result instanceof Error) throw result;
const recompressed = templates.map((home) => `envs/notes/seed/${home}`);
expect(result.normalized_result).toMatchObject({
scrubbed_files: [SEED],
recompressed_files: recompressed,
unscrubbed_files: [],
});
const readme = await readFile(join(directory, "README.md"), "utf8");
expect(readme).toContain(`\`${SEED}\``);
expect(recompressed.filter((path) => readme.includes(path))).toEqual([]);
expect(readme).toMatch(/\b3 gzip file\(s\)/u);
for (const path of recompressed)
expect(
gunzipSync(await readFile(join(directory, path))).toString("utf8"),
).toBe(`<template name="${path.slice("envs/notes/seed/".length)}"/>`);
});
it("lists a gzip seed recompressed, and a LibreOffice registry both scrubbed and left without its recovery list, each in its own list", async () => {
const template = ".local/share/notes/templates/blank.xml.gz";
const registry = ".config/notes/registrymodifications.xcu";
const kept = {
[template]: gzipSync('<template name="blank"/>'),
[registry]: Buffer.from(
[
'<?xml version="1.0" encoding="UTF-8"?>',
'<oor:items xmlns:oor="http://openoffice.org/2001/registry">',
`<item oor:path="/org.openoffice.UserProfile/Data"><prop oor:name="o" oor:op="fuse"><value>ada@${HOSTNAME}</value></prop></item>`,
`<item oor:path="/org.openoffice.Office.Recovery/RecoveryList"><node oor:name="recovery_item_1" oor:op="replace"><prop oor:name="OriginalURL" oor:op="fuse"><value>file://${HOME}/${BUDGET}</value></prop></node></item>`,
"</oor:items>",
"",
].join("\n"),
),
};
const { result, directory } = await exportBudget(
await budgetCapture(gzipped, "", kept),
);
if (result instanceof Error) throw result;
expect(result.normalized_result).toMatchObject({
scrubbed_files: [`envs/notes/seed/${registry}`, SEED],
recompressed_files: [`envs/notes/seed/${template}`],
unscrubbed_files: [],
});
const shipped = await readFile(
join(directory, "envs/notes/seed", registry),
"utf8",
);
expect(shipped).not.toContain("RecoveryList");
expect(shipped).toContain("<value>user@localhost</value>");
expect(
result.limitations.filter((line) => line.includes(`~/${registry}`)),
).toHaveLength(1);
const readme = await readFile(join(directory, "README.md"), "utf8");
expect(readme).toMatch(/\b1 gzip file\(s\)/u);
expect(readme).toContain(`\`~/${registry}\``);
});
});
describe("gzip seeds as shipped", () => {
it("checks a recompressed formerly unreadable seed as shipped and scrubs its document", async () => {
const save = (text: string) =>
gzipSync(text, { level: text.includes(">1200<") ? 9 : 0 });
const folder = await budgetCapture(save, "x".repeat(60_000));
const state = await budgetState(folder);
expect(state?.versions[0]).toMatchObject({
format: null,
compression: "gzip",
});
expect(state?.versions[0]?.view_error).toContain("expands more than");
const { result, directory } = await exportBudget(folder);
if (result instanceof Error) throw result;
expect(result.normalized_result).toMatchObject({
tasks: [{ failing_on_seed: ["rent"] }],
scrubbed_files: [SEED],
});
const shipped = await readFile(join(directory, SEED));
expect(openStateDocument(BUDGET, shipped)).toMatchObject({
format: "xml",
compression: "gzip",
});
expect(gunzipSync(shipped).toString("utf8")).toBe(
workbook("1200", "user@localhost", "x".repeat(60_000)),
);
});
it("keeps a seed the app stored uncompressed readable once scrubbed, where deflating it would pass the 100 times a view reads", async () => {
const stored = (text: string) => gzipSync(text, { level: 0 });
const comment = "x".repeat(60_000);
const { result, directory } = await exportBudget(
await budgetCapture(stored, comment),
);
if (result instanceof Error) throw result;
expect(result.normalized_result).toMatchObject({
tasks: [{ failing_on_seed: ["rent"] }],
});
const shipped = await readFile(join(directory, SEED));
expect(openStateDocument(BUDGET, shipped)).toMatchObject({
format: "xml",
compression: "gzip",
});
expect(openStateDocument(BUDGET, shipped)).not.toHaveProperty("error");
});
});