mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* feat(evidence)!: name the Evidence subject by its target identity (format revision 2) The Evidence subject carries subject.identity (kind, sha256, format, architecture, selection) and the Evidence ID commits to it, so raw/DOL entry points and ESP flash offsets of the same bytes are different subjects. Every record declares evidence_format: 2; revision 1 bundles, standalone records and snapshots are refused with the recreate guidance. binaryTargetIdentity is the one projection of an open target, used by Evidence and snapshot comparison. BREAKING CHANGE: Evidence format revision 2. Evidence IDs commit to the subject's target identity (subject.identity, including the raw/DOL entry point or ESP flash offsets), and every record declares evidence_format: 2. Evidence bundles, records and analysis snapshots from earlier releases are refused; recreate the observations with this release. * refactor(analysis-view): read parent subjects through the Evidence identity accessors (#11423) Analysis views compare their parent's file identity (evidenceFileTarget) and take path and digest from evidenceSubjectTarget instead of reading the subject's digest, format and architecture members, which duplicate subject.identity until they are removed. A counted guard (evidence-subject-declaration-read) keeps new direct reads out and records the 20 remaining files. * fix(evidence): carry format refusals through Evidence readers and bind target selections - safeParseEvidence/safeParseImmutableEvidence return the format refusal as an expected failure; every consumer maps it to the one integrity refusal with the recreate guidance. An unmarked object is an earlier record only when it has every member records have always carried. - ArtifactProvider inventory roots and provider subjects that name the target's own bytes carry the target's selection (ESP flash offsets). - Snapshot rows bind only Evidence whose identity, selection included, matches the snapshot target. - Entry-point selections use the raw-binary entry point form. * fix(evidence): refuse unauthenticated or earlier-format records instead of reading them as absent - recognizeEvidence (evidenceValidation) replaces tryParseEvidence: input without an evidence_id is not Evidence (ok(undefined)); a record that names one but fails authentication or is from another format revision is an EvidenceIntegrityError carrying the recreate guidance, never undefined. - Direct analysis retains that refusal as a secondary failure instead of silently skipping source capture; cleanup projection, source-closure annotation and the flutter, debugger and release-exposure recoveries use the same owner (the last three previously read the unauthenticated schema). - A desktop capture.json from an earlier format is refused with the recreate guidance instead of "not desktop application capture Evidence". * fix(evidence): advertise revision 2 ledger examples and rank delivery omissions by serialized size - The obligation ledger and reconstruction coverage examples named evidence_format 1, which revision 2 refuses; they now use EVIDENCE_FORMAT_REVISION. - MCP delivery chose which unretained recovery to omit by a parsed-storage lower bound clipped at the response budget. Once revision 2 subjects made the source record's estimate cross the budget, both candidates tied at the clip and the smaller source was omitted ahead of the larger completed observation. Candidates are now ranked by serialized size, which is what the budget counts. * refactor(native): compare function snapshots by their Evidence target identity (#11450) Function snapshots carry the subject's file target identity instead of the duplicated subject declaration. Call paths and function comparisons compare targets through sameTargetIdentity, so the same bytes opened at a different raw entry point or ESP flash offset no longer count as one coordinate space: build_call_path refuses the mix, and compare_functions no longer matches entries by address across selections. Structural candidates and native function matching read the architecture and digest through evidenceFileTarget. Refs #11417 * refactor(artifacts): compare artifact and WASM summaries by their Evidence target identity (#11461) Artifact comparison authenticates a summary against its retained inventory through sameTargetIdentity and rebinds the alias with the whole target, selection included, so a flash-selected inventory summary taken under an alias path is no longer refused. WASM comparison, the analysis view artifact projection and the inventory root check read the digest, format and display path through the Evidence identity accessors. Refs #11417 * refactor(evidence): read record subjects through the identity accessors in JS, managed, ledger and bundle readers (#11470) JavaScript application graphs, runtime reconciliation, managed inspection and native verification sources, the record summary filter, the ledger's residual-unknown target and bundle scope selection read the subject digest, format and display path through evidenceFileTarget and evidenceSubjectDisplay. No comparison changes: each reader matches one record's facts against its own payload or a caller filter. Refs #11417 * refactor(evidence)!: name the Evidence subject only by its target identity (#11497) * refactor(evidence)!: name the Evidence subject only by its target identity The subject keeps `identity` and the display facts `name` and `local_path`. The duplicated `digest`, `format` and `architecture` members are removed; the format-owned declaration now validates the identity itself. Readers the type checker found move to the owner accessors; untyped reads in verify scripts, the efficiency harness and test matchers move to `subject.identity`. The `evidence-subject-declaration-read` text guard and its baseline are deleted. Fixes #11473 Refs #11417 BREAKING CHANGE: the Evidence subject no longer has `digest`, `format` or `architecture`; read `subject.identity.sha256`, `subject.identity.format` and `subject.identity.architecture`. A record that still carries them is refused. * test(evidence): give the Flutter verifier fixture the identity subject and check the DOL declaration without the ID * test(evidence): drop the removed subject members from the declaration matrix and tamper fixtures * feat(evidence)!: give every Evidence record an explicit subject identity (#11536) * feat(evidence)!: give every Evidence record an explicit subject identity A producer that cannot name a file passes an `unknown` target identity with a closed reason (derived-from-linked-evidence, not-observed, unsupported-subject) instead of no subject. The subject is non-null; unknown subjects have no display facts. The generic missing-subject limitation and `subjectUnavailableReason` are removed; producer-specific explanations are ordinary limitations. Comparison identity requires the derived unknown, and null-subject consumers ask the owner accessors. Refs #11417 BREAKING CHANGE: every Evidence record has a subject. A record without a file target names `subject.identity` `{ "kind": "unknown", "reason": … }` with `name` and `local_path` null, instead of `subject: null` and the limitation "Artifact identity is unavailable for this observation.". Their Evidence IDs change. * fix(evidence): name explicit unknown subjects in JavaScript callers and keep a managed graph's file subject The verification scripts are not type-checked, so five createEvidence callers still passed undefined (or a missing first target) after the subject became required. They now pass unknownTarget("not-observed"). A managed application graph chose the first present input subject. Every subject is now present, so an unknown artifact subject hid a file subject on the member or boundary Evidence. The graph now takes the first input subject that names a file. * test(evidence): assert the subject kind instead of a presence the type now guarantees Every Evidence record has a subject after this change, so test guards and assertions that checked it for null always passed. Each now checks that the fixture subject names a file, or is removed where a later fileIdentity, fileSubjectTarget or fileSubjectDisplay call already requires that. Non-null assertions on Evidence subjects are removed; analysis-view inputs, whose subject is optional, keep an undefined check. * test(evidence): expect the Evidence subject format under its identity Two boundary tests still matched subject.format, which slice 2e moved into subject.identity. toMatchObject is not type-checked against the Evidence shape, so they failed only when run (the signature test on macOS).
724 lines
25 KiB
TypeScript
724 lines
25 KiB
TypeScript
import { Cli } from "incur";
|
|
import { access, readFile, writeFile } from "node:fs/promises";
|
|
import { createHash } from "node:crypto";
|
|
import type { FileHandle } from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
|
|
import { SqliteDatabaseService } from "../../../src/application/sqlite/SqliteDatabaseService.js";
|
|
import { ArtifactFileCleanupFailure } from "../../../src/resources/ArtifactFileOperation.js";
|
|
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
|
|
import { registerSqliteCommands } from "../../../src/cli/sqliteCommands.js";
|
|
import { createSqliteDatabaseService } from "../../../src/composition/sqlite.js";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
|
|
import { primaryAnalysisError } from "../../../src/domain/analysisErrorBase.js";
|
|
import { parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { silentLogger } from "../../../src/logger.js";
|
|
import { SqliteDatabaseProvider } from "../../../src/sqlite/SqliteDatabaseProvider.js";
|
|
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
|
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
|
import {
|
|
createSqliteDatabaseFixture,
|
|
SQLITE_NATIVE_LIMITS_AVAILABLE,
|
|
} from "../../fixtures/sqlite/database.js";
|
|
import {
|
|
createTestWorkspace,
|
|
removeTestWorkspace,
|
|
} from "../../support/workspace/workspaceFixture.js";
|
|
|
|
const nativeInspectionTest = it.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE);
|
|
|
|
const descriptorCleanup = vi.hoisted(() => ({
|
|
path: "",
|
|
denied: false,
|
|
invalidate: false,
|
|
attempts: 0,
|
|
handles: [] as FileHandle[],
|
|
readFailure: undefined as Error | undefined,
|
|
onOpen: undefined as (() => void) | undefined,
|
|
onClose: undefined as (() => void) | undefined,
|
|
reply: undefined as string | undefined,
|
|
}));
|
|
|
|
vi.mock("../../../src/composition/sqlite.js", () => ({
|
|
createSqliteDatabaseService: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("node:fs/promises", async (importOriginal) => {
|
|
const filesystem = await importOriginal<typeof import("node:fs/promises")>();
|
|
return {
|
|
...filesystem,
|
|
open: async (...args: Parameters<typeof filesystem.open>) => {
|
|
const handle = await filesystem.open(...args);
|
|
if (args[0] === descriptorCleanup.path) {
|
|
descriptorCleanup.handles.push(handle);
|
|
const close = handle.close.bind(handle);
|
|
handle.close = async () => {
|
|
// FileHandle.close also asks attached streams to close the invalidated handle.
|
|
if (handle.fd < 0) {
|
|
await close();
|
|
return;
|
|
}
|
|
descriptorCleanup.attempts += 1;
|
|
if (descriptorCleanup.denied) {
|
|
if (descriptorCleanup.invalidate) await close();
|
|
descriptorCleanup.onClose?.();
|
|
throw new Error("injected SQLite descriptor close failure");
|
|
}
|
|
await close();
|
|
descriptorCleanup.onClose?.();
|
|
};
|
|
if (descriptorCleanup.readFailure !== undefined)
|
|
vi.spyOn(handle, "stat").mockRejectedValue(
|
|
descriptorCleanup.readFailure,
|
|
);
|
|
descriptorCleanup.onOpen?.();
|
|
}
|
|
return handle;
|
|
},
|
|
};
|
|
});
|
|
|
|
const fixture = async (
|
|
member: "database" | "WAL" | "journal" | "reply",
|
|
invalidate = false,
|
|
) => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-descriptor-cleanup-");
|
|
const database = createSqliteDatabaseFixture(workspace.root, true);
|
|
const path =
|
|
member === "database"
|
|
? database.path
|
|
: member === "WAL"
|
|
? database.walPath
|
|
: member === "journal"
|
|
? `${database.path}-journal`
|
|
: "";
|
|
if (member === "journal") await writeFile(path, Buffer.alloc(28));
|
|
const roots: PrivateRuntimeRoot[] = [];
|
|
const provider = new SqliteDatabaseProvider(
|
|
process.env,
|
|
(spawn) => {
|
|
if (member !== "reply")
|
|
throw new Error("SQLite worker deliberately unavailable");
|
|
if (descriptorCleanup.reply !== undefined)
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"-e",
|
|
`const fs=require('node:fs');const request=JSON.parse(fs.readFileSync(process.argv[1],'utf8'));fs.writeFileSync(request.reply_path,${JSON.stringify(descriptorCleanup.reply)});`,
|
|
spawn.arguments[2] ?? "",
|
|
],
|
|
});
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: spawn.arguments.map((argument, index) =>
|
|
index === 1
|
|
? fileURLToPath(
|
|
new URL(
|
|
"../../../dist/sqlite/SqliteDatabaseWorker.js",
|
|
import.meta.url,
|
|
),
|
|
)
|
|
: argument,
|
|
),
|
|
});
|
|
},
|
|
async () => {
|
|
const root = await PrivateRuntimeRoot.create({ parent: workspace.root });
|
|
if (member === "reply")
|
|
descriptorCleanup.path = join(root.path, "reply.json");
|
|
roots.push(root);
|
|
return root;
|
|
},
|
|
);
|
|
descriptorCleanup.path = path;
|
|
descriptorCleanup.denied = true;
|
|
descriptorCleanup.invalidate = invalidate;
|
|
descriptorCleanup.attempts = 0;
|
|
descriptorCleanup.handles = [];
|
|
descriptorCleanup.readFailure = undefined;
|
|
descriptorCleanup.onOpen = undefined;
|
|
descriptorCleanup.onClose = undefined;
|
|
descriptorCleanup.reply = undefined;
|
|
onTestFinished(async () => {
|
|
descriptorCleanup.denied = false;
|
|
try {
|
|
await provider.close();
|
|
} catch (cause) {
|
|
if (!invalidate) throw cause;
|
|
}
|
|
for (const handle of descriptorCleanup.handles) await handle.close();
|
|
descriptorCleanup.path = "";
|
|
descriptorCleanup.onClose = undefined;
|
|
descriptorCleanup.reply = undefined;
|
|
database.close();
|
|
await removeTestWorkspace(workspace.root);
|
|
});
|
|
return { provider, roots, path, database };
|
|
};
|
|
|
|
const serveInspection = async (
|
|
provider: SqliteDatabaseProvider,
|
|
path: string,
|
|
) => {
|
|
const previousExitCode = process.exitCode;
|
|
onTestFinished(() => {
|
|
process.exitCode = previousExitCode;
|
|
});
|
|
vi.mocked(createSqliteDatabaseService).mockReturnValue(
|
|
new SqliteDatabaseService(provider),
|
|
);
|
|
const cli = Cli.create("rea-sqlite-cleanup-test", { sync: false });
|
|
registerSqliteCommands(cli, silentLogger, {});
|
|
let stdout = "";
|
|
await cli.serve(["inspect-sqlite-database", path, "--json"], {
|
|
env: {},
|
|
stdout: (text) => {
|
|
stdout += text;
|
|
},
|
|
exit: () => {},
|
|
});
|
|
return { output: JSON.parse(stdout) as unknown, exitCode: process.exitCode };
|
|
};
|
|
|
|
const expectedMember = async (role: string, path: string) => {
|
|
const bytes = await readFile(path);
|
|
return {
|
|
role,
|
|
path,
|
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
bytes: bytes.length,
|
|
};
|
|
};
|
|
|
|
it.each(["database", "WAL", "journal"] as const)(
|
|
"retains a failed %s descriptor and retries it at shutdown",
|
|
async (member) => {
|
|
const selected = await fixture(member);
|
|
const first = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
if (first.ok) throw new Error("Expected incomplete source cleanup");
|
|
expect(first).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [expect.stringContaining(selected.path)],
|
|
},
|
|
});
|
|
const sources =
|
|
member === "journal"
|
|
? []
|
|
: [
|
|
{ role: "database", path: selected.database.path },
|
|
...(member === "WAL"
|
|
? [{ role: "wal", path: selected.database.walPath }]
|
|
: []),
|
|
];
|
|
const members = await Promise.all(
|
|
sources.map((source) => expectedMember(source.role, source.path)),
|
|
);
|
|
if (members.length > 0)
|
|
expect(
|
|
projectAnalysisError(first.error).details?.diagnostics,
|
|
).toMatchObject({
|
|
snapshot_acquisition: { consistency: "unverified", members },
|
|
});
|
|
else
|
|
expect(
|
|
projectAnalysisError(first.error).details?.diagnostics,
|
|
).not.toHaveProperty("snapshot_acquisition");
|
|
expect(first.error.partialObservation).toBeUndefined();
|
|
const handle = descriptorCleanup.handles[0];
|
|
if (handle === undefined)
|
|
throw new Error("Expected owned source descriptor");
|
|
expect(handle.fd).toBeGreaterThanOrEqual(0);
|
|
const blocked = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
expect(blocked).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true },
|
|
});
|
|
expect(selected.roots).toHaveLength(1);
|
|
await expect(selected.provider.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
});
|
|
expect(descriptorCleanup.attempts).toBe(3);
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(descriptorCleanup.attempts).toBe(4);
|
|
expect(handle.fd).toBe(-1);
|
|
if (members.length > 0)
|
|
expect(
|
|
projectAnalysisError(first.error).details?.diagnostics,
|
|
).toMatchObject({
|
|
snapshot_acquisition: { consistency: "unverified", members },
|
|
});
|
|
for (const root of selected.roots)
|
|
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|
|
|
|
it("delivers individually completed database and WAL facts through CLI cleanup failure", async () => {
|
|
const selected = await fixture("WAL");
|
|
const members = await Promise.all([
|
|
expectedMember("database", selected.database.path),
|
|
expectedMember("wal", selected.database.walPath),
|
|
]);
|
|
const result = await serveInspection(
|
|
selected.provider,
|
|
selected.database.path,
|
|
);
|
|
expect(result).toMatchObject({
|
|
exitCode: 1,
|
|
output: {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
diagnostics: {
|
|
primary_error: {
|
|
details: {
|
|
diagnostics: {
|
|
snapshot_acquisition: { consistency: "unverified", members },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it.each(["EACCES", "EPERM"])(
|
|
"retains the original %s inspection diagnostics through CLI shutdown",
|
|
async (code) => {
|
|
const selected = await fixture("database");
|
|
descriptorCleanup.readFailure = Object.assign(
|
|
new Error("injected source read denial"),
|
|
{ code },
|
|
);
|
|
const result = await serveInspection(
|
|
selected.provider,
|
|
selected.database.path,
|
|
);
|
|
expect(result).toMatchObject({
|
|
exitCode: 1,
|
|
output: {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: [expect.stringContaining(selected.path)],
|
|
diagnostics: {
|
|
primary_error: {
|
|
details: {
|
|
diagnostics: {
|
|
previous_error: {
|
|
code: "access_denied",
|
|
details: { path: selected.path, system_code: code },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
cleanup_error: { code: "cleanup_incomplete" },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(descriptorCleanup.attempts).toBe(2);
|
|
},
|
|
);
|
|
|
|
it("retains cancellation diagnostics and the interrupt exit code through CLI shutdown", async () => {
|
|
const selected = await fixture("database");
|
|
descriptorCleanup.onOpen = () => {
|
|
const interrupt = process.listeners("SIGINT").at(-1);
|
|
if (interrupt === undefined)
|
|
throw new Error("Missing CLI interrupt handler");
|
|
interrupt("SIGINT");
|
|
};
|
|
const result = await serveInspection(
|
|
selected.provider,
|
|
selected.database.path,
|
|
);
|
|
expect(result).toMatchObject({
|
|
exitCode: 130,
|
|
output: {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: [expect.stringContaining(selected.path)],
|
|
diagnostics: {
|
|
primary_error: {
|
|
details: {
|
|
diagnostics: { previous_error: { code: "cancelled" } },
|
|
},
|
|
},
|
|
cleanup_error: { code: "cleanup_incomplete" },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(descriptorCleanup.attempts).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it("retains reply-read cancellation and retries the failed reply descriptor close", async () => {
|
|
const selected = await fixture("reply");
|
|
const controller = new AbortController();
|
|
const reason = { requested: "cancel during reply read" };
|
|
descriptorCleanup.onOpen = () => controller.abort(reason);
|
|
const result = await selected.provider.inspect(
|
|
{ path: selected.database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected cancelled reply read");
|
|
expect(result.error).toMatchObject({ _tag: "AnalysisCancelledError" });
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.partialObservation).toBeUndefined();
|
|
const cancelled = {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
execution_failure: "cancelled",
|
|
resources: [expect.stringContaining(descriptorCleanup.path)],
|
|
captured_output: {
|
|
stdout: "",
|
|
stderr: expect.any(String),
|
|
truncated: false,
|
|
},
|
|
},
|
|
};
|
|
// Windows cannot remove a private root while the reply descriptor is open;
|
|
// the retained cancellation is then reported as the root cleanup's cause.
|
|
expect(projectAnalysisError(result.error)).toMatchObject(
|
|
process.platform === "win32"
|
|
? {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: [dirname(descriptorCleanup.path)],
|
|
captured_output: cancelled.details.captured_output,
|
|
diagnostics: {
|
|
previous_error: {
|
|
code: cancelled.code,
|
|
details: {
|
|
execution_failure: "cancelled",
|
|
resources: cancelled.details.resources,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
: cancelled,
|
|
);
|
|
const handle = descriptorCleanup.handles[0];
|
|
if (handle === undefined) throw new Error("Expected owned reply descriptor");
|
|
expect(handle.fd).toBeGreaterThanOrEqual(0);
|
|
await expect(selected.provider.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
});
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(handle.fd).toBe(-1);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
});
|
|
for (const root of selected.roots)
|
|
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
nativeInspectionTest.each([
|
|
{ cancel: false, denied: false },
|
|
{ cancel: true, denied: false },
|
|
{ cancel: false, denied: true },
|
|
{ cancel: true, denied: true },
|
|
])(
|
|
"retains completed SQLite Evidence through reply close (cancel=$cancel, denied=$denied)",
|
|
async ({ cancel, denied }) => {
|
|
const selected = await fixture("reply");
|
|
descriptorCleanup.denied = denied;
|
|
const controller = new AbortController();
|
|
const reason = { requested: "cancel after complete SQLite reply read" };
|
|
if (cancel) descriptorCleanup.onClose = () => controller.abort(reason);
|
|
const input = {
|
|
path: selected.database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
worker_timeout_ms: 4321,
|
|
};
|
|
const databaseBytes = await readFile(input.path);
|
|
const walBytes = await readFile(selected.database.walPath);
|
|
const databaseDigest = createHash("sha256")
|
|
.update(databaseBytes)
|
|
.digest("hex");
|
|
const walDigest = createHash("sha256").update(walBytes).digest("hex");
|
|
const result = await new SqliteDatabaseService(selected.provider).inspect(
|
|
input,
|
|
{ signal: controller.signal },
|
|
);
|
|
expect(result.ok).toBe(!cancel && !denied);
|
|
const evidence = parseEvidence(
|
|
result.ok ? result.value : result.error.partialObservation,
|
|
);
|
|
expect(evidence).toMatchObject({
|
|
operation: "inspect_sqlite_database",
|
|
parameters: input,
|
|
subject: {
|
|
local_path: input.path,
|
|
identity: { format: "file", sha256: databaseDigest },
|
|
},
|
|
normalized_result: {
|
|
artifact: {
|
|
path: input.path,
|
|
sha256: databaseDigest,
|
|
bytes: databaseBytes.length,
|
|
},
|
|
wal: {
|
|
path: selected.database.walPath,
|
|
sha256: walDigest,
|
|
bytes: walBytes.length,
|
|
},
|
|
worker_timeout_ms: input.worker_timeout_ms,
|
|
schema: { scope: "selected_table", completeness: "complete" },
|
|
rows: {
|
|
table: "records",
|
|
columns: ["id"],
|
|
values: [[{ type: "integer", value: "1" }]],
|
|
returned_rows: 1,
|
|
truncated: true,
|
|
},
|
|
},
|
|
});
|
|
if (!result.ok) {
|
|
if (cancel) {
|
|
expect(primaryAnalysisError(result.error)._tag).toBe(
|
|
"AnalysisCancelledError",
|
|
);
|
|
expect(primaryAnalysisError(result.error).cause).toBe(reason);
|
|
}
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
details: { partial_observation: evidence },
|
|
});
|
|
expect(result.error.cleanupIncomplete).toBe(denied);
|
|
if (denied) {
|
|
const handle = descriptorCleanup.handles[0];
|
|
expect(handle?.fd).toBeGreaterThanOrEqual(0);
|
|
await expect(selected.provider.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
});
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(result.error.partialObservation).toEqual(evidence);
|
|
}
|
|
}
|
|
await selected.provider.close();
|
|
expect(descriptorCleanup.handles.every((handle) => handle.fd === -1)).toBe(
|
|
true,
|
|
);
|
|
for (const root of selected.roots)
|
|
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ reply: "{ malformed reply", tag: "AnalysisOutputError" },
|
|
{
|
|
reply: JSON.stringify({
|
|
ok: false,
|
|
reason: "selection",
|
|
message: "Selected table does not exist",
|
|
}),
|
|
tag: "AnalysisInputError",
|
|
},
|
|
])(
|
|
"preserves a completed $tag reply alongside denied close without inventing SQLite Evidence",
|
|
async ({ reply, tag }) => {
|
|
const selected = await fixture("reply");
|
|
descriptorCleanup.reply = reply;
|
|
const result = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
if (result.ok) throw new Error("Expected rejected worker reply");
|
|
expect(primaryAnalysisError(result.error)._tag).toBe(tag);
|
|
expect(result.error.partialObservation).toBeUndefined();
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(primaryAnalysisError(result.error)._tag).toBe(tag);
|
|
expect(descriptorCleanup.handles.every((handle) => handle.fd === -1)).toBe(
|
|
true,
|
|
);
|
|
},
|
|
);
|
|
|
|
nativeInspectionTest(
|
|
"keeps a completed reply close failure primary when abort follows lease retention",
|
|
async () => {
|
|
const selected = await fixture("reply");
|
|
const controller = new AbortController();
|
|
const reason = { requested: "abort after reply close failure capture" };
|
|
let retainedReplyLease: string | undefined;
|
|
const retain = ArtifactResourceScope.prototype.retain;
|
|
const retainSpy = vi
|
|
.spyOn(ArtifactResourceScope.prototype, "retain")
|
|
.mockImplementation(function (this: ArtifactResourceScope, owner) {
|
|
retain.call(this, owner);
|
|
if (
|
|
owner.kind === "lease" &&
|
|
owner.resource === descriptorCleanup.path
|
|
) {
|
|
retainedReplyLease = owner.resource;
|
|
controller.abort(reason);
|
|
}
|
|
});
|
|
|
|
try {
|
|
const result = await selected.provider.inspect(
|
|
{ path: selected.database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
expect(controller.signal.aborted).toBe(true);
|
|
expect(controller.signal.reason).toBe(reason);
|
|
expect(retainedReplyLease).toBe(descriptorCleanup.path);
|
|
expect(result.ok).toBe(false);
|
|
if (result.ok) return;
|
|
|
|
const primary = primaryAnalysisError(result.error);
|
|
expect(primary).toBeInstanceOf(ProviderCleanupError);
|
|
if (!(primary instanceof ProviderCleanupError)) return;
|
|
expect(primary).not.toMatchObject({ _tag: "AnalysisCancelledError" });
|
|
expect(primary.cause).toBeInstanceOf(ArtifactFileCleanupFailure);
|
|
expect(primary.cause).toMatchObject({
|
|
fileCleanupFailure: {
|
|
path: descriptorCleanup.path,
|
|
outcome: { kind: "completed" },
|
|
completionCancellation: undefined,
|
|
},
|
|
});
|
|
expect(primary.cleanupIncomplete).toBe(true);
|
|
expect(primary.cleanupResources).toContain(descriptorCleanup.path);
|
|
|
|
const handle = descriptorCleanup.handles[0];
|
|
if (handle === undefined)
|
|
throw new Error("Expected owned reply descriptor");
|
|
expect(handle.fd).toBeGreaterThanOrEqual(0);
|
|
expect(descriptorCleanup.attempts).toBe(1);
|
|
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(descriptorCleanup.attempts).toBe(2);
|
|
expect(handle.fd).toBe(-1);
|
|
expect(primary.cleanupIncomplete).toBe(false);
|
|
expect(primary).toBeInstanceOf(ProviderCleanupError);
|
|
} finally {
|
|
retainSpy.mockRestore();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("retries source cleanup before admitting another snapshot", async () => {
|
|
const selected = await fixture("database");
|
|
await selected.provider.inspect({ path: selected.database.path });
|
|
const first = descriptorCleanup.handles[0];
|
|
if (first === undefined) throw new Error("Expected source descriptor");
|
|
descriptorCleanup.denied = false;
|
|
const resumed = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
expect(selected.roots).toHaveLength(2);
|
|
expect(first.fd).toBe(-1);
|
|
if (resumed.ok) throw new Error("The worker is deliberately unavailable");
|
|
expect(resumed.error.cleanupIncomplete).toBe(false);
|
|
await selected.provider.close();
|
|
});
|
|
|
|
it("keeps an invalidated descriptor close failure unconfirmed across retries", async () => {
|
|
const selected = await fixture("journal", true);
|
|
const first = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
expect(first).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true },
|
|
});
|
|
expect(descriptorCleanup.handles[0]?.fd).toBe(-1);
|
|
descriptorCleanup.denied = false;
|
|
await expect(selected.provider.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
});
|
|
await expect(selected.provider.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
});
|
|
expect(descriptorCleanup.attempts).toBe(1);
|
|
});
|
|
|
|
it.each(["EACCES", "EPERM"])(
|
|
"preserves %s read diagnostics alongside a failed descriptor close",
|
|
async (code) => {
|
|
const selected = await fixture("database");
|
|
descriptorCleanup.readFailure = Object.assign(
|
|
new Error("injected source read denial"),
|
|
{ code },
|
|
);
|
|
const result = await selected.provider.inspect({
|
|
path: selected.database.path,
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
diagnostics: { previous_error: { code: "access_denied" } },
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected physical source denial");
|
|
expect(
|
|
projectAnalysisError(result.error).details?.diagnostics,
|
|
).not.toHaveProperty("snapshot_acquisition");
|
|
},
|
|
);
|
|
|
|
it.each(["database", "WAL", "journal"] as const)(
|
|
"preserves caller cancellation alongside a failed %s descriptor close",
|
|
async (member) => {
|
|
const selected = await fixture(member);
|
|
const controller = new AbortController();
|
|
const reason = { requested: `cancel while reading ${member}` };
|
|
descriptorCleanup.onOpen = () => controller.abort(reason);
|
|
const result = await selected.provider.inspect(
|
|
{ path: selected.database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected caller cancellation");
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
diagnostics: { previous_error: { code: "cancelled" } },
|
|
},
|
|
});
|
|
descriptorCleanup.denied = false;
|
|
await selected.provider.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
});
|
|
expect(result.error.cleanupPrimary).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
});
|
|
expect(result.error.cleanupPrimary?.cause).toBe(reason);
|
|
if (member === "WAL")
|
|
expect(
|
|
projectAnalysisError(result.error).details?.diagnostics,
|
|
).toMatchObject({
|
|
snapshot_acquisition: {
|
|
consistency: "unverified",
|
|
members: [await expectedMember("database", selected.database.path)],
|
|
},
|
|
});
|
|
for (const root of selected.roots)
|
|
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|