fix(desktop): publish omissions only from completed checkpoints (#13389)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 10:54:56 +00:00
1 parent 0b62db4d63
commit 359f854477
3 files changed
+132 -11

No files matched your search

@@ -13,6 +13,7 @@ import {
mkdir,
readdir,
readFile,
realpath,
writeFile,
type FileHandle,
} from "node:fs/promises";
@@ -59,6 +60,7 @@ import * as linuxInventory from "../../desktop/LinuxApplicationInventory.js";
import { X11WindowObserver } from "../../desktop/X11WindowObserver.js";
import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
import { CaptureFile } from "./DesktopCaptureOutput.js";
import { indexState, observedFileAt } from "./DesktopStateVersions.js";
import {
captureDesktopApplication,
type DesktopCaptureRequest,
@@ -335,6 +337,116 @@ it.each([false, true])(
},
);
it.skipIf(process.platform === "win32")(
"publishes omissions only from checkpoints completed before a later read is cancelled",
async () => {
const { root, home, environment } = await fixture();
const physicalHome = await realpath(home);
const output = join(root, "out");
const baseline = join(physicalHome, "baseline-too-large.txt");
const committed = join(physicalHome, "committed-too-large.txt");
const abandoned = join(physicalHome, "a-abandoned-too-large.txt");
const interrupted = join(physicalHome, "b-interrupted.txt");
const oversized = "x".repeat(17);
await writeFile(baseline, oversized);
const controller = new AbortController();
const reason = new Error("cancel at the next physical content read");
const original = DesktopContentStore.prototype.retain;
const observed: string[] = [];
const retain = vi
.spyOn(DesktopContentStore.prototype, "retain")
.mockImplementation(async function (
this: DesktopContentStore,
path,
version,
signal,
) {
if (path === interrupted) controller.abort(reason);
const outcome = await original.call(this, path, version, signal);
if (outcome.kind === "omitted") observed.push(path);
return outcome;
});
try {
const desktopHost = host(environment);
const recorded = captureFixture();
const evidence = await captureDesktopApplication(
{ ...request(output), statePaths: [home], maxFileBytes: 16 },
{
...desktopHost,
desktop: {
...desktopHost.desktop,
inspectApplication: async () => ({
ok: true,
value: {
inventory: { ...recorded.application, state_directories: [] },
limitations: [],
signingKeys: new Map(),
},
}),
runningApplicationWindows: async () => ({
state: "listed",
windows: [],
}),
runSession: async ({ checkpoint }) => {
const seen = { openedFiles: new Set<string>(), titles: [] };
await writeFile(committed, oversized);
await checkpoint("saved", "request", 500, seen);
await writeFile(abandoned, oversized);
await writeFile(interrupted, "small");
await expect(
checkpoint("interrupted", "request", 1000, seen),
).rejects.toMatchObject({
reason: "cancelled",
cause: reason,
});
return {
session: { ...recorded.session, end_reason: "cancelled" },
unreleased: undefined,
unreadableWindowSamples: 0,
};
},
},
},
controller.signal,
);
if ("error" in evidence) throw new Error(JSON.stringify(evidence));
const capture = desktopApplicationCaptureSchema.parse(
evidence.normalized_result,
);
expect(observed).toEqual([baseline, committed, abandoned]);
expect(capture.state.checkpoints.map(({ name }) => name)).toEqual([
"baseline",
"saved",
]);
const published = desktopApplicationCaptureSchema.parse(
JSON.parse(await readFile(join(output, "capture.json"), "utf8"))
.normalized_result,
);
const index = indexState(published);
expect(observedFileAt(baseline, 0, index)).toEqual({
present: true,
sha256: null,
});
expect(observedFileAt(committed, 1, index)).toEqual({
present: true,
sha256: null,
});
expect(observedFileAt(abandoned, 0, index)).toEqual({
present: false,
sha256: null,
});
expect(capture.state.omitted_content).toEqual([
{ path: baseline, reason: "too_large" },
{ path: committed, reason: "too_large" },
]);
} finally {
retain.mockRestore();
}
},
);
it.skipIf(process.platform !== "linux")(
"returns the published evidence when cancellation arrives during publication",
async () => {
@@ -22,6 +22,7 @@ import {
checkpointTaken,
type DesktopApplicationCapture,
type DesktopCaptureEvent,
type DesktopCheckpoint,
type DesktopSession,
} from "../../domain/desktop/desktopCapture.js";
import { createEvidence, type Evidence } from "../../domain/evidence.js";
@@ -271,9 +272,17 @@ const recordCapture = async (
store,
);
const recorder = new CheckpointRecorder(scope, store, host.desktop.walkHome);
const acknowledge = async (titles: readonly string[] = []) => {
const taken = recorder.checkpoints.at(-1);
if (taken === undefined) return;
let committedOmissions = 0;
const recordCheckpoint = async (
name: string,
trigger: DesktopCheckpoint["trigger"],
atMs: number,
titles: readonly string[] = [],
) => {
const taken = await recorder.record(name, trigger, atMs, signal);
// A cancelled walk can leave observations in the content store that
// have no published checkpoint to establish when they were observed.
committedOmissions = store.omissions.length;
const event = checkpointTaken(
taken,
home,
@@ -286,8 +295,7 @@ const recordCapture = async (
);
};
await host.report("Recording the starting state.");
await recorder.record("baseline", "baseline", 0, signal);
await acknowledge();
await recordCheckpoint("baseline", "baseline", 0);
await host.report(`Launching ${inventory.argv.join(" ")}.`);
const outcome = await host.desktop.runSession({
argv: inventory.argv,
@@ -305,8 +313,7 @@ const recordCapture = async (
checkpoint: async (name, trigger, atMs, seen) => {
await host.report(`Recording checkpoint ${name}.`);
recorder.openedFiles = seen.openedFiles;
await recorder.record(name, trigger, atMs, signal);
await acknowledge(seen.titles);
await recordCheckpoint(name, trigger, atMs, seen.titles);
},
report: host.report,
signal,
@@ -336,7 +343,7 @@ const recordCapture = async (
checkpoints: recorder.checkpoints,
content_files: store.files,
content_bytes: store.bytes,
omitted_content: [...store.omissions],
omitted_content: store.omissions.slice(0, committedOmissions),
unretained_opened_directories: [...unretained],
},
limitations: [
@@ -119,7 +119,7 @@ export class CheckpointRecorder {
trigger: DesktopCheckpoint["trigger"],
atMs: number,
signal?: AbortSignal,
): Promise<void> {
): Promise<DesktopCheckpoint> {
const { roots, excluded, maxEntries, contentRoots } = this.scope;
const walk = await this.walkHome(roots, excluded, maxEntries, signal);
for (const mountPoint of walk.mountPoints) this.mountPoints.add(mountPoint);
@@ -136,7 +136,7 @@ export class CheckpointRecorder {
} else
({ changes, unconfirmed } = await this.#changes(walk, retained, signal));
this.#previous = walk;
this.checkpoints.push({
const checkpoint: DesktopCheckpoint = {
name,
trigger,
at_ms: atMs,
@@ -147,6 +147,8 @@ export class CheckpointRecorder {
unconfirmed_changes: unconfirmed,
changes,
retained,
});
};
this.checkpoints.push(checkpoint);
return checkpoint;
}
}