refactor(desktop): pass export task reader runner through a port (#11858)

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
RRFZandmorluto authored and GitHub committed 2026-11-18 19:31:12 -05:00
1 parent 0c83de1642
commit f192180fb5
8 files changed
+103 -9

No files matched your search

+1 -1
View File
@@ -9,6 +9,6 @@
],
"crossProvider": {},
"entry": {
"application -> desktop": 12
"application -> desktop": 11
}
}
@@ -34,6 +34,7 @@ import { isWithinAny } from "../../domain/desktop/desktopStateDiff.js";
import type { HostIdentity } from "../../domain/desktop/hostIdentity.js";
import { scrubDconf } from "../../domain/desktop/seedScrub.js";
import { createEvidence, type Evidence } from "../../domain/evidence.js";
import type { DesktopTaskReaderRunner } from "../../ports/DesktopTaskReader.js";
import type { JsonValue } from "../../domain/jsonValue.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import { analysisErrorWithCancellation } from "../../domain/analysisErrorCancellation.js";
@@ -343,9 +344,14 @@ const NO_SCRUB: SeedScrubReport = {
unscrubbed: [],
};
/** State readers plus the host task reader needed by exportDesktopApplication. */
export interface DesktopExportReaders extends DesktopStateReaders {
readonly taskReader: DesktopTaskReaderRunner;
}
const runExport = async (
request: DesktopExportRequest,
readers: DesktopStateReaders,
readers: DesktopExportReaders,
signal: AbortSignal | undefined,
) => {
const { evidence, capture } = await readDesktopCapture(
@@ -384,6 +390,7 @@ const runExport = async (
ends,
identity,
checkpoints,
taskReader: readers.taskReader,
signal,
});
const admitted = await admitDesktopTasks(
@@ -567,7 +574,7 @@ const publishedExportFailure = (
/** Export one capture folder as an agentenv computer app bundle, reported as Evidence. */
export const exportDesktopApplication = async (
request: DesktopExportRequest,
readers: DesktopStateReaders,
readers: DesktopExportReaders,
signal?: AbortSignal,
): Promise<Evidence | AnalysisError> => {
try {
@@ -14,7 +14,7 @@ import {
type StateEnds,
} from "../../domain/desktop/desktopState.js";
import type { HostIdentity } from "../../domain/desktop/hostIdentity.js";
import { runReaderHere } from "../../desktop/TaskReaderProcess.js";
import type { DesktopTaskReaderRunner } from "../../ports/DesktopTaskReader.js";
import {
shippedSeedBytes,
shippedSeedViews,
@@ -40,6 +40,7 @@ export interface ReaderSources {
readonly identity: HostIdentity;
readonly maxBytes: number;
readonly signal: AbortSignal | undefined;
readonly taskReader: DesktopTaskReaderRunner;
/** The capture's checkpoint names, and the folder a build computer keeps a screenshot of each in, if any. */
readonly checkpoints: readonly string[];
readonly screens: string | undefined;
@@ -105,7 +106,8 @@ const exportReaderViews =
readerViewsOfBytes(
readers,
(reader) => readerFile(sources, content, reader.file, at),
(argv, timeoutS) => runReaderHere(argv, timeoutS, sources.signal),
(argv, timeoutS) =>
sources.taskReader(argv, timeoutS, sources.signal),
),
sources.maxBytes,
);
@@ -601,6 +601,73 @@ describe("exportDesktopApplication with a task's readers", () => {
});
});
describe("exportDesktopApplication taskReader port", () => {
it("calls the injected taskReader port with the command, timeout, and signal", async () => {
const scratch = await createTestTempDirectory("desktop-task-port-");
const taskPath = join(scratch, "task.json");
const directory = join(scratch, "bundle");
const controller = new AbortController();
const calls: Array<{
readonly argv: readonly string[];
readonly timeoutS: number;
readonly signal: AbortSignal | undefined;
}> = [];
await writeFile(
taskPath,
JSON.stringify(
readerTask(["version v2"], {
id: "version",
file: DATABASE,
argv: ["custom-reader", "{file}"],
output: "lines",
timeout_s: 7,
}),
),
);
const result = await exportDesktopApplication(
{
capture: await writeCaptureFolder(capture()),
directory,
name: undefined,
tasks: [taskPath],
},
{
...readers,
taskReader: async (argv, timeoutS, signal) => {
calls.push({ argv, timeoutS, signal });
return {
exit_code: 0,
stdout: `${JSON.stringify({
view: {
format: "text",
view: {
lines: calls.length === 1 ? ["version v2"] : ["version v1"],
line_count: 1,
truncated: false,
},
},
})}\n`,
stderr: "",
timed_out: false,
};
},
},
controller.signal,
);
if (result instanceof Error) throw result;
expect(calls).toHaveLength(2);
expect(calls.every((call) => call.timeoutS === 37)).toBe(true);
expect(calls.every((call) => call.signal === controller.signal)).toBe(true);
expect(
calls.every((call) =>
call.argv.some((part) => part.includes("custom-reader")),
),
).toBe(true);
});
});
/** The notes capture with nothing changed between its checkpoints, as an app that keeps its state on screen leaves it. */
const screenOnlyCapture = () => {
const base = capture();
@@ -6,7 +6,8 @@ import { join } from "node:path";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import type { DesktopApplicationCapture } from "../../domain/desktop/desktopCapture.js";
import { createEvidence } from "../../domain/evidence.js";
import type { DesktopStateReaders } from "./DesktopApplicationState.js";
import { runDesktopTaskReader } from "../../desktop/DesktopCaptureProvider.js";
import type { DesktopExportReaders } from "./DesktopApplicationExport.js";
import {
DESKTOP_OPERATION,
DESKTOP_PROVIDER,
@@ -285,7 +286,7 @@ export const writeCaptureFolder = async (
};
/** A SQLite reader that reports one row per retained version's last byte. */
export const readers: DesktopStateReaders = {
export const readers: DesktopExportReaders = {
sqlite: async (database) => {
// A staged write-ahead log, when present, decides the version instead.
const log = await readFile(`${database}-wal`, "latin1").catch(
@@ -313,4 +314,5 @@ export const readers: DesktopStateReaders = {
};
},
dconf: async () => ({ ok: false, error: "unused" }),
taskReader: runDesktopTaskReader,
};
+5 -2
View File
@@ -29,7 +29,10 @@ import {
DESKTOP_CAPTURE_DEFAULTS,
type DesktopCaptureProgress,
} from "../domain/desktop/desktopCapture.js";
import { createDesktopCaptureProvider } from "../desktop/DesktopCaptureProvider.js";
import {
createDesktopCaptureProvider,
runDesktopTaskReader,
} from "../desktop/DesktopCaptureProvider.js";
import { SessionRequestQueue } from "../desktop/DesktopSessionRequests.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { registerCliCommand } from "./commandRegistry.js";
@@ -407,7 +410,7 @@ const registerDesktopExportCommand = (
...withheldRequestOf(options),
viewPolicy: options.viewPolicy,
},
readers,
{ ...readers, taskReader: runDesktopTaskReader },
signal,
),
);
+5
View File
@@ -1,4 +1,5 @@
import type { DesktopCaptureProvider } from "../ports/DesktopApplicationSession.js";
import type { DesktopTaskReaderRunner } from "../ports/DesktopTaskReader.js";
import {
runDesktopSession,
runningApplicationWindows,
@@ -7,8 +8,12 @@ import { DesktopContentStore } from "./DesktopContentStore.js";
import { walkHomeState } from "./HomeStateWalker.js";
import { inspectLinuxApplication } from "./LinuxApplicationInventory.js";
import { resolveProgram } from "./ProgramLookup.js";
import { runReaderHere } from "./TaskReaderProcess.js";
import { X11WindowObserver } from "./X11WindowObserver.js";
/** Run one exported-task reader command using the desktop host runner. */
export const runDesktopTaskReader: DesktopTaskReaderRunner = runReaderHere;
/**
* The desktop provider's implementation of the capture port: `xprop` window
* observation, the supervised session runner, the home-folder walker and the
+8
View File
@@ -0,0 +1,8 @@
import type { ReaderCommandResult } from "../domain/desktop/appTaskReaders.js";
/** Run one task file reader command against an extracted capture file on disk. */
export type DesktopTaskReaderRunner = (
argv: readonly string[],
timeoutS: number,
signal?: AbortSignal,
) => Promise<ReaderCommandResult>;