mirror of
https://github.com/morluto/rea.git
synced 2026-11-10 16:57:19 +00:00
* fix(desktop): preserve document URLs and value formats * fix(desktop): bound added checks and omit noisy text guards * fix(desktop): exclude unseeded locks from task checks * fix(desktop): preserve draft bounds and launch URLs * fix(desktop): render draft values using their source format * fix(desktop): preserve launch URLs and JSON draft values * fix(desktop): recognize captured file URL documents * fix(desktop): render captured file URLs for runtime home * fix(desktop): distinguish phone numbers from save metadata * fix(desktop): preserve launch URLs and user text * fix(desktop): preserve captured URLs and user text * fix(desktop): preserve local file URL authority * fix(desktop): preserve local file URL authority * fix(desktop): preserve local file URL authority * fix(desktop): render captured file URLs at launch * fix(desktop): classify numeric timestamps using field context * fix(desktop): classify numeric timestamps using field context * fix(desktop): classify numeric timestamps using field context * fix(desktop): retain context for generated draft values * fix(desktop): retain context for generated draft values * fix(desktop): retain context for generated draft values * test(desktop): cover generated IDs in text guards * test(desktop): cover generated IDs in text guards * test(desktop): cover generated IDs in text guards * fix(desktop): isolate launcher URL encoding * fix(desktop): isolate launcher URL encoding * fix(desktop): isolate launcher URL encoding * fix(desktop): isolate launcher URL encoding * fix(desktop): retain clean text in noisy record fields * fix(desktop): classify added line values in one pass * fix(desktop): apply XML quote rules to text drafts * fix(desktop): distinguish XML attributes from quoted text * fix(desktop): preserve parsed XML line context * Format appTaskChecks.ts with the pinned oxfmt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): retain partial XML syntax facts * fix(desktop): recognize markdown autolinks in drafts * fix(desktop): reuse export exclusions in task drafts * fix(desktop): report unchecked client build paths * docs(desktop): clarify gzip scrub limits * fix(desktop): reuse export exclusions in task drafts * docs(desktop): keep the judge command on one line Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(desktop): clarify gzip scrub limits * fix(setup): own desktop build process cleanup * fix(doctor): compare desktop settings with resolved defaults * 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(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): pin reader-free verifier to selector parent * 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 * test(desktop): check retained log seed admission * 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(desktop): compare screen task views by value * fix(desktop): reject inexact reader numbers * fix(desktop): budget export for both reader passes * fix(desktop): bound reader runner argument size * test(desktop): verify chunked reader payloads and parent output * fix(doctor): query agentenv for model endpoint * style(desktop): format reader payload assertion * 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): retain record identity in draft checks * fix(desktop): align timestamp selector digits * fix(desktop): scrub XML independently of reader compatibility * fix(desktop): preserve file aliases and reader diagnostics * fix(desktop): retain remote reader cleanup ownership * style(desktop): format capture review fixes * fix(desktop): retain reader leases across verifier reloads * test(desktop): overlap reader cleanup retries * fix(desktop): run long exports within exec limits --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
368 lines
12 KiB
TypeScript
368 lines
12 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
|
|
import { expect, it } from "vitest";
|
|
|
|
import { readerViewsOfBytes } from "../../../src/application/desktop/DesktopTaskReaders.js";
|
|
import type { DesktopTaskReader } from "../../../src/domain/desktop/appTask.js";
|
|
import {
|
|
readerAnswerView,
|
|
readerCommand,
|
|
PYTHON_READER_CLEANUP,
|
|
} from "../../../src/domain/desktop/appTaskReaders.js";
|
|
import { runReaderHere } from "../../../src/desktop/TaskReaderProcess.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const execute = promisify(execFile);
|
|
const NOTE = "Documents/today.txt";
|
|
const SECRET = "control-token-secret";
|
|
|
|
/** A fake app's headless mode: it reports what it was given, then changes the file it read. */
|
|
const REPORTING_APP = String.raw`
|
|
import json, os, sys
|
|
path = sys.argv[1]
|
|
data = open(path, "rb").read().decode()
|
|
open(path, "ab").write(b" tampered")
|
|
print("App 1.0 starting")
|
|
print("REA " + json.dumps({
|
|
"home": os.environ.get("HOME"), "display": os.environ.get("DISPLAY"), "bus": os.environ.get("DBUS_SESSION_BUS_ADDRESS"),
|
|
"config": os.environ.get("XDG_CONFIG_HOME"), "control": sorted(k for k in os.environ if k.startswith("AGENTENV_")),
|
|
"path": path, "cwd": os.getcwd(), "content": data, "stdin": sys.stdin.read()}))
|
|
print("App quit")
|
|
`;
|
|
|
|
/** A computer's home with the note in it and the fake app beside it. */
|
|
const computer = async () => {
|
|
const root = await createTestTempDirectory("reader-runner-");
|
|
const home = join(root, "home");
|
|
await mkdir(join(home, "Documents"), { recursive: true });
|
|
await writeFile(join(home, NOTE), "milk\neggs\n");
|
|
const app = join(root, "app.py");
|
|
await writeFile(app, REPORTING_APP);
|
|
return { home, app };
|
|
};
|
|
|
|
/** Run the runner as computer-exec runs it in the session: the session's environment, its control token included. */
|
|
const run = async (home: string, reader: DesktopTaskReader, path?: string) => {
|
|
const [python = "", ...rest] = readerCommand(reader, path);
|
|
const { stdout } = await execute(python, rest, {
|
|
cwd: home,
|
|
env: {
|
|
PATH: process.env.PATH ?? "",
|
|
HOME: home,
|
|
DISPLAY: ":99",
|
|
DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
|
|
AGENTENV_CONTROL_TOKEN: SECRET,
|
|
},
|
|
maxBuffer: 4 * 1024 * 1024,
|
|
});
|
|
return stdout;
|
|
};
|
|
|
|
const reporting = (app: string, fields: Partial<DesktopTaskReader> = {}) =>
|
|
({
|
|
id: "report",
|
|
file: NOTE,
|
|
argv: ["python3", app, "{file}"],
|
|
output: "json",
|
|
...fields,
|
|
}) as DesktopTaskReader;
|
|
|
|
it("runs the reader on a private copy of the file in a fresh home, without the display, the session bus or the control token", async () => {
|
|
const { home, app } = await computer();
|
|
const stdout = await run(home, reporting(app, { stdin: "open {file}" }));
|
|
|
|
expect(stdout).not.toContain(SECRET);
|
|
const view = readerAnswerView(stdout);
|
|
const report = view?.view as Record<string, unknown>;
|
|
expect(view?.format).toBe("json");
|
|
expect(report).toMatchObject({
|
|
display: null,
|
|
bus: null,
|
|
control: [],
|
|
content: "milk\neggs\n",
|
|
});
|
|
const copy = String(report.path);
|
|
expect(copy.endsWith("/today.txt")).toBe(true);
|
|
expect(copy.startsWith(home)).toBe(false);
|
|
expect(report.stdin).toBe(`open ${copy}`);
|
|
// The copy and the fresh home are siblings in the runner's own folder.
|
|
expect(String(report.home).startsWith(dirname(dirname(copy)))).toBe(true);
|
|
expect(String(report.config).startsWith(String(report.home))).toBe(true);
|
|
expect(report.cwd).toBe(dirname(copy));
|
|
// The reader changed only its copy, which is gone with its folder.
|
|
expect(await readFile(join(home, NOTE), "utf8")).toBe("milk\neggs\n");
|
|
await expect(access(dirname(dirname(copy)))).rejects.toThrow();
|
|
});
|
|
|
|
it("retains a remotely owned copy until the matching lease removes it", async () => {
|
|
const { home, app } = await computer();
|
|
const root = await createTestTempDirectory("reader-lease-");
|
|
const lease = { path: join(root, "owned"), token: "owner-token" };
|
|
const [python = "", ...args] = readerCommand(
|
|
reporting(app),
|
|
undefined,
|
|
undefined,
|
|
lease,
|
|
);
|
|
const { stdout } = await execute(python, args, {
|
|
env: { ...process.env, HOME: home },
|
|
});
|
|
expect(readerAnswerView(stdout)?.format).toBe("json");
|
|
await expect(
|
|
access(join(lease.path, "file", "today.txt")),
|
|
).resolves.toBeUndefined();
|
|
await expect(
|
|
execute(python, [
|
|
"-I",
|
|
"-c",
|
|
PYTHON_READER_CLEANUP,
|
|
JSON.stringify({ ...lease, token: "other" }),
|
|
]),
|
|
).rejects.toThrow();
|
|
await expect(access(lease.path)).resolves.toBeUndefined();
|
|
const refused = `import shutil\ndef fail(path): raise PermissionError("denied")\nshutil.rmtree = fail\nexec(${JSON.stringify(PYTHON_READER_CLEANUP)})`;
|
|
await expect(
|
|
execute(python, ["-I", "-c", refused, JSON.stringify(lease)]),
|
|
).rejects.toThrow();
|
|
expect(await readFile(`${lease.path}.owner`, "utf8")).toBe(lease.token);
|
|
await execute(python, [
|
|
"-I",
|
|
"-c",
|
|
PYTHON_READER_CLEANUP,
|
|
JSON.stringify(lease),
|
|
]);
|
|
await expect(access(lease.path)).rejects.toThrow();
|
|
await expect(access(`${lease.path}.owner`)).rejects.toThrow();
|
|
});
|
|
|
|
it("passes maximum-length Unicode stdin through bounded arguments to the real reader", async () => {
|
|
const { home, app } = await computer();
|
|
const input = "é".repeat(8190) + "😀" + "é".repeat(57344);
|
|
const reader = reporting(app, { stdin: input });
|
|
const command = readerCommand(reader, undefined);
|
|
expect(Buffer.byteLength(command.slice(4).join(""))).toBeGreaterThan(131072);
|
|
expect(
|
|
Math.max(...command.slice(4).map((part) => Buffer.byteLength(part))),
|
|
).toBeLessThan(131072);
|
|
const report = readerAnswerView(await run(home, reader))?.view as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
expect(report.stdin).toBe(input);
|
|
});
|
|
|
|
it("keeps the display for a reader that asks for it, still without the control token", async () => {
|
|
const { home, app } = await computer();
|
|
const report = readerAnswerView(
|
|
await run(home, reporting(app, { display: true })),
|
|
)?.view as Record<string, unknown>;
|
|
expect(report).toMatchObject({ display: ":99", control: [] });
|
|
});
|
|
|
|
it("reads the file a path names instead of the home's, under the home file's name", async () => {
|
|
const { home, app } = await computer();
|
|
const blob = join(home, "..", "blob-0123");
|
|
await writeFile(blob, "seed bytes");
|
|
const report = readerAnswerView(await run(home, reporting(app), blob))
|
|
?.view as Record<string, unknown>;
|
|
expect(report.content).toBe("seed bytes");
|
|
expect(String(report.path).endsWith("/today.txt")).toBe(true);
|
|
});
|
|
|
|
it("makes the marked lines a text view and drops the rest", async () => {
|
|
const { home } = await computer();
|
|
const view = readerAnswerView(
|
|
await run(home, {
|
|
id: "lines",
|
|
file: NOTE,
|
|
argv: [
|
|
"python3",
|
|
"-c",
|
|
"import sys\nprint('ts> banner')\nfor line in open(sys.argv[1]): print('REA item ' + line.strip())\nprint('REA')\nprint('REAL news')",
|
|
"{file}",
|
|
],
|
|
output: "lines",
|
|
}),
|
|
);
|
|
expect(view).toEqual({
|
|
format: "text",
|
|
view: {
|
|
lines: ["item milk", "item eggs"],
|
|
line_count: 2,
|
|
truncated: false,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("answers absent for a file the home does not hold, without running the reader", async () => {
|
|
const { home } = await computer();
|
|
const stdout = await run(home, {
|
|
id: "gone",
|
|
file: "Documents/gone.txt",
|
|
argv: ["sh", "-c", `touch '${home}/ran'; echo 'REA x' {file}`],
|
|
output: "lines",
|
|
});
|
|
expect(readerAnswerView(stdout)).toBeUndefined();
|
|
await expect(access(join(home, "ran"))).rejects.toThrow();
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"exits non-zero",
|
|
["sh", "-c", "echo 'REA x'; echo 'cannot open {file}' >&2; exit 3"],
|
|
"json",
|
|
["the reader exited 3", "cannot open"],
|
|
],
|
|
[
|
|
"prints no marked line",
|
|
["sh", "-c", "echo hello {file}"],
|
|
"lines",
|
|
['printed no line starting with "REA "'],
|
|
],
|
|
[
|
|
"prints a view that is not JSON",
|
|
["sh", "-c", "echo 'REA {not json' {file}"],
|
|
"json",
|
|
["are not one JSON value"],
|
|
],
|
|
[
|
|
"prints a number JSON cannot hold exactly",
|
|
["sh", "-c", "echo 'REA {\"id\": 9007199254740993}'; : {file}"],
|
|
"json",
|
|
["cannot be read exactly"],
|
|
],
|
|
...["9007199254740993.0", "1e-400", "-0.0"].map((number) => [
|
|
`prints an inexact decimal ${number}`,
|
|
["sh", "-c", `echo 'REA {"id": ${number}}'; : {file}`],
|
|
"json",
|
|
["cannot be read exactly"],
|
|
]),
|
|
[
|
|
"prints more than 1 MiB",
|
|
["python3", "-c", "print('REA ' + 'x' * (1 << 20))", "{file}"],
|
|
"lines",
|
|
["printed more than 1 MiB"],
|
|
],
|
|
[
|
|
"cannot start",
|
|
["no-such-app-binary", "{file}"],
|
|
"lines",
|
|
["could not start no-such-app-binary"],
|
|
],
|
|
])(
|
|
"reports a reader that %s as an error view",
|
|
async (_, argv, output, facts) => {
|
|
const { home } = await computer();
|
|
const view = readerAnswerView(
|
|
await run(home, {
|
|
id: "broken",
|
|
file: NOTE,
|
|
argv: argv as string[],
|
|
output: output as "json" | "lines",
|
|
}),
|
|
);
|
|
expect(view?.format).toBeNull();
|
|
for (const fact of facts) expect(view?.error).toContain(fact);
|
|
},
|
|
);
|
|
|
|
it("stops a reader past its timeout, with the processes it started", async () => {
|
|
const { home } = await computer();
|
|
const started = Date.now();
|
|
const view = readerAnswerView(
|
|
await run(home, {
|
|
id: "slow",
|
|
file: NOTE,
|
|
argv: [
|
|
"sh",
|
|
"-c",
|
|
`(sleep 3; touch "${home}/late") & echo 'REA x'; sleep 30; : {file}`,
|
|
],
|
|
output: "lines",
|
|
timeout_s: 1,
|
|
}),
|
|
);
|
|
expect(view?.error).toContain("ran past its 1 s");
|
|
expect(Date.now() - started).toBeLessThan(10_000);
|
|
await new Promise((resolve) => setTimeout(resolve, 3500));
|
|
await expect(access(join(home, "late"))).rejects.toThrow();
|
|
});
|
|
|
|
it("runs a reader whose file shares a name with the runner's own folders and files", async () => {
|
|
const { home } = await computer();
|
|
for (const name of ["home", "stdout", "stderr"]) {
|
|
await writeFile(join(home, "Documents", name), `${name} bytes`);
|
|
const view = readerAnswerView(
|
|
await run(home, {
|
|
id: "named",
|
|
file: `Documents/${name}`,
|
|
argv: ["sh", "-c", 'echo REA $(cat "$1")', "sh", "{file}"],
|
|
output: "lines",
|
|
}),
|
|
);
|
|
expect(view?.view, name).toEqual({
|
|
lines: [`${name} bytes`],
|
|
line_count: 1,
|
|
truncated: false,
|
|
});
|
|
}
|
|
});
|
|
|
|
it("stops a reader once it has printed more than 1 MiB, rather than at its timeout", async () => {
|
|
const { home } = await computer();
|
|
const started = Date.now();
|
|
const view = readerAnswerView(
|
|
await run(home, {
|
|
id: "flood",
|
|
file: NOTE,
|
|
argv: ["sh", "-c", "yes 'REA x'; : {file}"],
|
|
output: "lines",
|
|
timeout_s: 30,
|
|
}),
|
|
);
|
|
expect(view?.error).toContain("printed more than 1 MiB");
|
|
expect(Date.now() - started).toBeLessThan(10_000);
|
|
});
|
|
|
|
it("stops the reader, the processes it started and its folder when the export is cancelled", async () => {
|
|
const { home } = await computer();
|
|
const out = join(home, "..", "out");
|
|
await mkdir(out);
|
|
const controller = new AbortController();
|
|
const read = readerViewsOfBytes(
|
|
[
|
|
{
|
|
id: "stuck",
|
|
file: NOTE,
|
|
// Python, not the system's sleep, whose environment macOS withholds from the run token sweep.
|
|
argv: [
|
|
"python3",
|
|
"-c",
|
|
`import os, time; open("${out}/cwd", "w").write(os.getcwd()); open("${out}/pid", "w").write(str(os.getpid())); time.sleep(60)`,
|
|
"{file}",
|
|
],
|
|
output: "lines",
|
|
},
|
|
],
|
|
() => Promise.resolve({ kind: "bytes", bytes: Buffer.from("milk\n") }),
|
|
(argv, timeoutS) => runReaderHere(argv, timeoutS, controller.signal),
|
|
);
|
|
const pid = async (): Promise<number> => {
|
|
for (let tries = 0; tries < 200; tries += 1) {
|
|
const text = await readFile(join(out, "pid"), "utf8").catch(() => "");
|
|
if (text.trim() !== "") return Number(text.trim());
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
}
|
|
throw new Error("the reader never started");
|
|
};
|
|
const reader = await pid();
|
|
controller.abort(new Error("cancelled"));
|
|
await expect(read).rejects.toThrow();
|
|
expect(() => process.kill(reader, 0)).toThrow();
|
|
const folder = (await readFile(join(out, "cwd"), "utf8")).trim();
|
|
await expect(access(folder)).rejects.toThrow();
|
|
});
|