mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 18:06:46 +00:00
* feat(errors)!: add the TargetFailure model, its projection table and conformance registry Introduce one domain-owned TargetFailure value whose kind alone derives the public code, category, retryability, recovery and details, for CLI and MCP alike. Selected-file malformed and truncated refusals are the first area on it: AnalysisMalformedTargetError is replaced by AnalysisTargetFailureError, and ParseFailure's byte-structure variants are TargetFailure variants rather than a parallel type. The shared stableArtifactParserFailureCli loop is replaced by a declarative registry (tests/support/targetFailure/readers/*.ts) and one generated suite that runs every registered case through the CLI and MCP. BREAKING CHANGE: selected-file malformed and truncated refusals add details.failure_kind, and a truncated target's remediation now says to re-acquire the complete file instead of choosing another target. Codes are unchanged. Affected operations: every operation that refuses a selected file's malformed or truncated bytes, including the ELF, PE, Mach-O, DWARF, Go, Rust, BEAM, pyc, devicetree, Apple IPS, kernel module, binary hardening, managed PE/CLI and IL2CPP readers. * test(managed): name the target failure error in the merged status test * docs(reference): name the error CSV instead of linking a non-page file * fix(tests): expect the path reason for non-regular dyld inputs #14225 refuses a non-regular dyld input as ArtifactReaderFailure("path"); dylibFileLifetime still expected "format" and fails on main's shard 9. * fix(tests): expect io, invariant and path where #14225 reclassified stableArtifact expected "format" for an impossible descriptor byte count (now io) and an invalid chunk size (now InternalInvariantError); directoryArtifactReaderOpen expected "format" for a non-file entry (now path, the same change as #14238). All three fail on main. * fix(errors)!: raise container reader byte faults as malformed or truncated targets Delete the ambiguous `format` reason from ArtifactReaderFailure and ArtifactOperationError. Each container reader now raises its owner's fact once, through malformedArtifact / truncatedArtifact, with format, offset, field and measured extents. Short reads inside a measured extent are read-coverage integrity failures, path-kind refusals are `path`, foreign or recovery-requiring inputs are `unsupported`, host tool output is `io`, and REA defects are InternalInvariantError. The trace, NTFS journal, release exposure, web capture, native macOS and web artifact adapters project the reader's parsed facts as AnalysisTargetFailureError instead of invalid_request. Inventory malformed, truncated, unsupported and cancelled reasons take their code from the TargetFailure projection table, so a truncated container is unsupported_target with failure_kind "truncated" on both paths. BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact, analyze_javascript_application, inspect_desktop_application, inspect_keyed_archive, inspect_asset_catalog, inspect_nuget_nupkg, trace_dylib_resolution, trace_web_module_imports and trace_web_source_location report damaged container bytes as unsupported_target (details.reason "malformed" or "truncated", with failure_kind and format) instead of artifact_operation_failed with reason "format", and a truncated container as unsupported_target instead of truncated. inspect_playwright_trace, inspect_ntfs_journal, audit_release_exposure, the web capture inspection operations and the caller-selected import and source maps report damaged selected bytes as unsupported_target instead of invalid_request. The `format` reason no longer appears in details.reason. * test(artifacts): expect target failure codes in compiled CLI and MCP suites * fix(errors)!: refuse malformed SQLite, pwntools and EVM inputs as target failures The SQLite engine's `format` reason is renamed to `malformed` at its owner (SQLITE_INSPECTION_REASONS), and its refusal becomes an AnalysisTargetFailureError at the selected path with format "sqlite". The pwntools and EVM worker replies keep their wire reason "format", which their workers define, and the adapters translate it once into a malformed target failure with the profile's format (elf, elf-core, evm-bytecode) instead of an invalid_format argument issue. SQLite joins the target-failure conformance registry. BREAKING CHANGE: inspect_sqlite_database, inspect_binary_layout, inspect_recorded_crash and inspect_evm_interface report a selected file's malformed bytes as unsupported_target with details.failure_kind "malformed", format_failure, format and path, instead of invalid_request with an invalid_format issue on path. * fix(errors): give the integrity kind a schema-valid code and list the reference page artifact_integrity_mismatch's projection schema requires declared and calculated digests, which a TargetFailure of kind integrity does not carry, so the kind now projects artifact_operation_failed with category integrity_mismatch. The model test validates every kind's projection against analysisErrorProjectionSchema. The reference page joins the documentation sidebar. * fix(artifacts): report a short carrier read as a changed file and keep unsupported desktop probes local A Bun or Node SEA short read inside a range already checked against the measured size meant the file changed, not that it was truncated. The carrier range reader and Bun's readExact now raise read-coverage integrity for it, and keep 'outside the file' as the truncated case. DesktopFileInspection records an unsupported managed-metadata probe as a row issue again, so later file checks still run. * test(errors): expect target failure facts in the pwntools and EVM reply suites The literal-replacement scenario now reads the failure's detail instead of an issues[] message, and a malformed reply wrapped by cleanup failure reports category unsupported_target. * test(errors): register PE DOS-signature and truncated IPS refusals and drop their per-command copies The non-MZ PE imports and load-config cases (PE32/PE32+, ZZ and high-bit signatures) and the truncated Apple IPS body move into the conformance registry, which runs them through the CLI and MCP and now also checks that a refusal leaves the selected file unchanged. The per-command tests keep only their valid-image controls. * test(sqlite): drop the malformed-database provider test the conformance registry covers inspect_sqlite_database/not-a-database in the target-failure registry runs the same non-SQLite bytes through the CLI and MCP, asserts the malformed target facts and checks the selected file is unchanged. * feat(errors)!: make failure_kind the identifier and derive code and retry from it Apply the owner review on #14239 to the TargetFailure model: - remove details.format_failure; failure_kind is the only identifier; - give integrity, conflict and io their own codes (target_integrity_mismatch, input_conflict, host_io_failed) and derive inventory malformed, truncated and unsupported codes from the same table; - add a retry hint (none, backoff, after_change) derived from the kind, with io refined by its transient errno fact; retryable stays the unchanged-retry flag and is true exactly for backoff; - rename the limit-cut result code and category truncated to partial; - drop cancelled and resource from the target kinds and spell identifiers in American English (unrecognized); - return a target failure a tool handler throws as a projected isError result instead of letting the SDK flatten it to text. BREAKING CHANGE: details.format_failure is removed (read details.failure_kind). The truncated result code and category are now partial. Target integrity failures report target_integrity_mismatch, host read failures host_io_failed and disagreeing inputs input_conflict. Inventory operations (inventory_artifact, inspect_artifact, extract_artifact and the other inventory callers) report truncated container bytes as unsupported_target. Every target failure carries a top-level retry hint. * test(sqlite): expect a malformed target for non-database bytes in the CLI suite * fix(artifacts): keep changed-file short reads, invalid offsets and EOF declarations apart PyInstaller reads inside the measured size, and .NET bundle manifest refills, now raise read-coverage integrity on a zero-byte read instead of truncated. A .NET bundle field and a Godot PCK directory entry that extend past the file are truncated (with offset and extents) instead of malformed. An absent or invalid ASAR packed offset is malformed; only a valid range past the archive is truncated. The .NET manifest string offset is reported in file coordinates. * fix(pwntools): report a short ELF header as a truncated target The layout and recorded-crash workers raise a distinct truncated reason when the file ends inside the ELF identification or header, and the decoder projects it as failure_kind truncated instead of inferring it from the message. * fix(artifacts): verify the carrier before blaming a short read on the bytes The NTFS $MFT decoder, positional members (FIT, uImage, Bun, Deno) and .NET bundle members now re-verify the source before reporting a short stream as malformed, so a file that changed during the read reports the change. The truncation site builds the model's measured extent. * docs(android): name unsupported_target for truncated boot image structures * fix(errors)!: report a target that changed during the read as failure_kind changed Inventory integrity failures whose check means the target moved under the read (container, member and unpacked-entry identity, read coverage, traversal coverage) now take the model's changed projection: code artifact_changed, retry backoff and details.failure_kind changed. A selected target that no longer matches the one the session admitted gets its own admitted-identity check and keeps its reopen recovery. BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and the other inventory operations report a target that changed during the read as artifact_changed (retryable, failure_kind changed) instead of artifact_operation_failed. Admission-identity refusals report integrity_check admitted-identity instead of container-identity. * test(artifacts): expect admitted-identity for selections replaced before admission A selected source replaced before admission fails the admitted-identity check rather than container-identity since the changed/admitted split. * test(go): name the fixture and result when an MZ carrier is not refused through MCP * test(verify): expect target failures for damaged ELF, core and EVM inputs in real-provider verifiers The binary-layout, recorded-crash and EVM verifiers still expected invalid_input for malformed or truncated selected bytes. The pwntools and EVM adapters now report those as unsupported_target with failure_kind; a missing path remains invalid_input. * test(web): expect the partial code for a refused source-map admission The limit-cut code is partial; this Windows-lane test arrived from main with the old truncated spelling. * test(errors): check the MCP target failure as the text clients receive The stdio transport removes the private structured carrier from every isError result, so the conformance suite asserted a field that never reaches clients and failed every case. The projected diagnostic is checked from the JSON text instead. * test(artifacts): expect artifact_changed when selected bytes change after inventory Bundle resources replaced after open and unselected JavaScript sources rewritten after inventory now fail a change check, so they report artifact_changed with retry backoff rather than artifact_operation_failed. * fix(errors): report a session digest mismatch as admitted-identity The digest the session admitted does not change when the same request is repeated, so targetDigestChangedFailure was inviting a backoff retry that fails the same way. It now names admitted-identity, which stays artifact_operation_failed and not retryable, and tells the caller to reopen. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(artifacts): classify unused PyInstaller compressed input as layout zlib stops at the first complete stream, so a declared span that arrives intact but is not fully consumed is trailing junk or a second stream, not a file that changed. A source that ends before that span stays read-coverage. The cached-carrier test replaces the file with a different size so a shared timestamp cannot hide the rewrite. Co-authored-by: morluto <morluto@users.noreply.github.com> * test(applications): expect unsupported_target for a damaged trace stream A trace member whose CRC fails is a malformed zip target. The partial Evidence assertion now checks that code, failure_kind and format. Co-authored-by: morluto <morluto@users.noreply.github.com> * test(dylib): expect admitted-identity when the opened target is replaced The session keeps the digest it admitted, so replacing the executable and repeating trace_dylib_resolution is not a retryable container change. Co-authored-by: morluto <morluto@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <morluto@users.noreply.github.com>
1524 lines
50 KiB
TypeScript
1524 lines
50 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { execFile } from "node:child_process";
|
|
import { access, lstat, readFile, symlink, writeFile } from "node:fs/promises";
|
|
import { DatabaseSync } from "node:sqlite";
|
|
import { promisify } from "node:util";
|
|
import { fileURLToPath } from "node:url";
|
|
import { expect, it as nativeTest, onTestFinished, vi } from "vitest";
|
|
import { ZodError } from "zod";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
|
|
import { SQLITE_DATABASE_LIMITS } from "../../../src/sqlite/SqliteDatabaseLimits.js";
|
|
import { SqliteDatabaseProvider } from "../../../src/sqlite/SqliteDatabaseProvider.js";
|
|
import { inspectSqliteDatabaseSnapshot } from "../../../src/sqlite/SqliteDatabaseInspection.js";
|
|
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
|
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
|
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
|
import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js";
|
|
import {
|
|
createSqliteDatabaseFixture,
|
|
createSqliteApplicationCollationFixture,
|
|
SQLITE_NATIVE_LIMITS_AVAILABLE,
|
|
} from "../../fixtures/sqlite/database.js";
|
|
import {
|
|
createTestWorkspace,
|
|
removeTestWorkspace,
|
|
} from "../../support/workspace/workspaceFixture.js";
|
|
|
|
const it = nativeTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE);
|
|
|
|
const workerUrl = new URL(
|
|
"../../../dist/sqlite/SqliteDatabaseWorker.js",
|
|
import.meta.url,
|
|
);
|
|
const createProvider = () =>
|
|
new SqliteDatabaseProvider(process.env, (spawn) =>
|
|
spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: spawn.arguments.map((argument, index) =>
|
|
index === 1 ? fileURLToPath(workerUrl) : argument,
|
|
),
|
|
}),
|
|
);
|
|
|
|
const createFixture = async (wal = false) => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-boundary-");
|
|
const database = createSqliteDatabaseFixture(workspace.root, wal);
|
|
if (!wal) database.close();
|
|
onTestFinished(async () => {
|
|
if (wal) database.close();
|
|
await removeTestWorkspace(workspace.root);
|
|
});
|
|
return { workspace, database };
|
|
};
|
|
|
|
const expectPrivateRootRemoved = (path: string) =>
|
|
expect(access(path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
|
|
nativeTest.each([
|
|
{ input: { path: "relative.db" }, field: "path" },
|
|
{
|
|
input: { path: "/selected.db", table: "records", row_limit: 0 },
|
|
field: "row_limit",
|
|
},
|
|
{ input: { path: "/selected.db", columns: ["id"] }, field: "columns" },
|
|
])(
|
|
"attributes invalid provider input to its argument before allocating resources: %j",
|
|
async ({ input, field }) => {
|
|
const createRuntime = vi.fn(async () => {
|
|
throw new Error("Invalid input must not allocate a SQLite runtime");
|
|
});
|
|
const provider = new SqliteDatabaseProvider({}, undefined, createRuntime);
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({ ...input });
|
|
if (result.ok) throw new Error("Expected an input rejection");
|
|
expect(result.error.cause).toBeInstanceOf(ZodError);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "invalid_request",
|
|
details: { issues: [{ path: [field] }] },
|
|
});
|
|
expect(createRuntime).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it("reports a SQLite worker that fails to launch and removes its snapshot before the next inspection", async () => {
|
|
const { database } = await createFixture();
|
|
const roots: string[] = [];
|
|
let launches = 0;
|
|
const provider = new SqliteDatabaseProvider(
|
|
process.env,
|
|
(spawn) => {
|
|
launches += 1;
|
|
if (launches === 1)
|
|
throw Object.assign(new Error("spawn E2BIG"), { code: "E2BIG" });
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: spawn.arguments.map((argument, index) =>
|
|
index === 1 ? fileURLToPath(workerUrl) : argument,
|
|
),
|
|
});
|
|
},
|
|
async () => {
|
|
const root = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-sqlite-launch-",
|
|
});
|
|
roots.push(root.path);
|
|
return root;
|
|
},
|
|
);
|
|
onTestFinished(() => provider.close());
|
|
|
|
const refused = await provider.inspect({ path: database.path });
|
|
if (refused.ok) throw new Error("Expected a worker launch failure");
|
|
expect(refused.error.diagnostics).toMatchObject({
|
|
phase: "worker",
|
|
reason: "spawn E2BIG",
|
|
});
|
|
expect(refused.error.cleanupIncomplete).toBe(false);
|
|
expect(refused.error.capturedOutput).toBeUndefined();
|
|
const [refusedRoot] = roots;
|
|
if (refusedRoot === undefined) throw new Error("Missing private root");
|
|
await expectPrivateRootRemoved(refusedRoot);
|
|
|
|
const inspected = await provider.inspect({ path: database.path });
|
|
if (!inspected.ok) throw inspected.error;
|
|
expect(launches).toBe(2);
|
|
});
|
|
|
|
it("enforces a selected worker budget and completes the corrected same-database retry", async () => {
|
|
const { database } = await createFixture();
|
|
const original = await readFile(database.path);
|
|
let first = true;
|
|
let root = "";
|
|
const provider = new SqliteDatabaseProvider(process.env, (spawn) => {
|
|
root = spawn.cwd ?? "";
|
|
if (first) {
|
|
first = false;
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
spawn.arguments[0] ?? "--max-old-space-size=256",
|
|
"-e",
|
|
`process.argv[2]=process.argv[1];setTimeout(()=>import(${JSON.stringify(workerUrl.href)}),250)`,
|
|
spawn.arguments[2] ?? "",
|
|
],
|
|
});
|
|
}
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: spawn.arguments.map((arg, index) =>
|
|
index === 1 ? fileURLToPath(workerUrl) : arg,
|
|
),
|
|
});
|
|
});
|
|
onTestFinished(() => provider.close());
|
|
const failed = await provider.inspect({
|
|
path: database.path,
|
|
worker_timeout_ms: 50,
|
|
});
|
|
if (failed.ok) throw new Error("Worker deadline was ignored");
|
|
expect(projectAnalysisError(failed.error)).toMatchObject({
|
|
code: "provider_timeout",
|
|
details: { timeout_ms: 50 },
|
|
});
|
|
expect(failed.error.cleanupIncomplete).toBe(false);
|
|
await expectPrivateRootRemoved(root);
|
|
const corrected = await provider.inspect({
|
|
path: database.path,
|
|
worker_timeout_ms: 2000,
|
|
});
|
|
if (!corrected.ok) throw corrected.error;
|
|
expect(corrected.value.result).toMatchObject({
|
|
worker_timeout_ms: 2000,
|
|
schema: { completeness: "complete" },
|
|
});
|
|
await expectPrivateRootRemoved(root);
|
|
expect(Buffer.compare(await readFile(database.path), original)).toBe(0);
|
|
});
|
|
|
|
nativeTest(
|
|
"retains a timed-out worker's budget and one output copy through owned cleanup retry",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const original = await readFile(database.path);
|
|
let cleanupAllowed = false;
|
|
let launches = 0;
|
|
let root = "";
|
|
const provider = new SqliteDatabaseProvider(process.env, async (spawn) => {
|
|
launches += 1;
|
|
root = spawn.cwd ?? "";
|
|
const launched = await spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"-e",
|
|
"process.stdout.write('ready\\n');setTimeout(()=>process.stdout.write('sqlite-timeout-marker'),100);setInterval(()=>{},1000)",
|
|
],
|
|
});
|
|
return {
|
|
...launched,
|
|
cleanup: async () =>
|
|
cleanupAllowed
|
|
? await (launched.cleanup?.() ??
|
|
cleanupOwnedProcessGroup(launched.ownership))
|
|
: { cleaned: false, reason: "owned timeout cleanup denied" },
|
|
};
|
|
});
|
|
onTestFinished(async () => {
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
});
|
|
const result = await provider.inspect({
|
|
path: database.path,
|
|
worker_timeout_ms: 3000,
|
|
});
|
|
if (result.ok) throw new Error("Expected timeout with unresolved cleanup");
|
|
const projected = projectAnalysisError(result.error);
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
retryable: false,
|
|
details: {
|
|
resources: expect.arrayContaining([root]),
|
|
diagnostics: {
|
|
previous_error: {
|
|
code: "provider_timeout",
|
|
details: { timeout_ms: 3000 },
|
|
remediation: {
|
|
action: expect.stringContaining("worker_timeout_ms"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(
|
|
JSON.stringify(projected).split("sqlite-timeout-marker"),
|
|
).toHaveLength(2);
|
|
await expect(access(root)).resolves.toBeUndefined();
|
|
expect(await provider.inspect({ path: database.path })).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true },
|
|
});
|
|
expect(launches).toBe(1);
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
const resolved = projectAnalysisError(result.error);
|
|
expect(resolved).toMatchObject({
|
|
code: "provider_timeout",
|
|
details: { timeout_ms: 3000 },
|
|
});
|
|
expect(
|
|
JSON.stringify(resolved).split("sqlite-timeout-marker"),
|
|
).toHaveLength(2);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
await expectPrivateRootRemoved(root);
|
|
expect(Buffer.compare(await readFile(database.path), original)).toBe(0);
|
|
},
|
|
);
|
|
|
|
it.each(["completed reply", "malformed reply"] as const)(
|
|
"preserves the primary result for a %s while retaining unconfirmed cleanup ownership",
|
|
async (replyKind) => {
|
|
const { workspace, database } = await createFixture();
|
|
let cleanupAttempts = 0;
|
|
let cleanupAllowed = false;
|
|
let ownedRoot = "";
|
|
const provider = new SqliteDatabaseProvider(process.env, async (spawn) => {
|
|
ownedRoot = spawn.cwd ?? "";
|
|
const requestPath = spawn.arguments[2];
|
|
if (requestPath === undefined)
|
|
throw new Error("SQLite request path missing");
|
|
const request = JSON.parse(await readFile(requestPath, "utf8")) as {
|
|
snapshot_path: string;
|
|
reply_path: string;
|
|
input: Parameters<typeof inspectSqliteDatabaseSnapshot>[1];
|
|
};
|
|
const inspection =
|
|
replyKind === "completed reply"
|
|
? inspectSqliteDatabaseSnapshot(request.snapshot_path, request.input)
|
|
: undefined;
|
|
await writeFile(
|
|
request.reply_path,
|
|
inspection === undefined
|
|
? "{ malformed reply"
|
|
: JSON.stringify({ ok: true, inspection }),
|
|
{ flag: "wx", mode: 0o600 },
|
|
);
|
|
const launched = await spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: ["-e", "process.exit(0)"],
|
|
});
|
|
return {
|
|
...launched,
|
|
cleanup: async () => {
|
|
cleanupAttempts += 1;
|
|
return cleanupAllowed
|
|
? { cleaned: true, signaled: false }
|
|
: { cleaned: false, reason: "fixture cleanup denial" };
|
|
},
|
|
};
|
|
});
|
|
onTestFinished(async () => {
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
await removeTestWorkspace(workspace.root);
|
|
});
|
|
|
|
const result = await provider.inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
cleanupResources: expect.arrayContaining([ownedRoot]),
|
|
diagnostics: {
|
|
reason: "fixture cleanup denial",
|
|
previous_error:
|
|
replyKind === "malformed reply" ? expect.any(Object) : null,
|
|
},
|
|
...(replyKind === "completed reply"
|
|
? {
|
|
partialObservation: {
|
|
operation: "inspect_sqlite_database",
|
|
confidence: "observed",
|
|
normalized_result: {
|
|
schema: { tables: expect.any(Array) },
|
|
rows: { returned_rows: 1 },
|
|
},
|
|
},
|
|
}
|
|
: { partialObservation: undefined }),
|
|
},
|
|
});
|
|
expect(cleanupAttempts).toBe(1);
|
|
await expect(access(`${ownedRoot}/reply.json`)).resolves.toBeUndefined();
|
|
let closeFailure: unknown;
|
|
try {
|
|
await provider.close();
|
|
} catch (cause) {
|
|
closeFailure = cause;
|
|
}
|
|
expect(closeFailure).toMatchObject({
|
|
cleanupIncomplete: true,
|
|
cleanupResources: expect.arrayContaining([ownedRoot]),
|
|
...(replyKind === "completed reply"
|
|
? {
|
|
partialObservation: {
|
|
operation: "inspect_sqlite_database",
|
|
confidence: "observed",
|
|
normalized_result: {
|
|
schema: { tables: expect.any(Array) },
|
|
rows: { returned_rows: 1 },
|
|
},
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
cleanupResources: expect.arrayContaining([ownedRoot]),
|
|
...(replyKind === "completed reply"
|
|
? { partialObservation: { operation: "inspect_sqlite_database" } }
|
|
: {}),
|
|
},
|
|
});
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
expect(cleanupAttempts).toBe(3);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: false, cleanupResources: [] },
|
|
});
|
|
expect(closeFailure).toMatchObject({
|
|
cleanupIncomplete: false,
|
|
cleanupResources: [],
|
|
});
|
|
await expectPrivateRootRemoved(ownedRoot);
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"retains a typed worker output failure after exact runtime-root cleanup retry",
|
|
async () => {
|
|
const { workspace, database } = await createFixture();
|
|
let root: PrivateRuntimeRoot | undefined;
|
|
let rootCloseAttempts = 0;
|
|
const provider = new SqliteDatabaseProvider(
|
|
process.env,
|
|
async (spawn) => {
|
|
const requestPath = spawn.arguments[2];
|
|
if (requestPath === undefined)
|
|
throw new Error("SQLite request path missing");
|
|
const request = JSON.parse(await readFile(requestPath, "utf8")) as {
|
|
reply_path: string;
|
|
};
|
|
await writeFile(
|
|
request.reply_path,
|
|
JSON.stringify({ ok: true, inspection: {} }),
|
|
{ flag: "wx", mode: 0o600 },
|
|
);
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: ["-e", "process.exit(0)"],
|
|
});
|
|
},
|
|
async () => {
|
|
root = await PrivateRuntimeRoot.create({ parent: workspace.root });
|
|
const acquired = root;
|
|
return {
|
|
path: acquired.path,
|
|
close: async () => {
|
|
rootCloseAttempts += 1;
|
|
if (rootCloseAttempts === 1)
|
|
throw new Error("runtime root removal denied once");
|
|
await acquired.close();
|
|
},
|
|
};
|
|
},
|
|
);
|
|
onTestFinished(async () => {
|
|
await provider.close();
|
|
await root?.close();
|
|
await removeTestWorkspace(workspace.root);
|
|
});
|
|
|
|
const result = await provider.inspect({ path: database.path });
|
|
if (result.ok) throw new Error("Expected malformed worker output");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cleanupIncomplete: true,
|
|
diagnostics: {
|
|
previous_error: {
|
|
code: "unreadable_output",
|
|
},
|
|
},
|
|
});
|
|
await provider.close();
|
|
|
|
expect(rootCloseAttempts).toBe(2);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
if (root === undefined) throw new Error("Runtime root was not acquired");
|
|
await expectPrivateRootRemoved(root.path);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "unreadable_output",
|
|
});
|
|
expect(result.error.cleanupPrimary).toMatchObject({
|
|
_tag: "AnalysisOutputError",
|
|
capturedOutput: { stdout: "", stderr: "", truncated: false },
|
|
});
|
|
},
|
|
);
|
|
|
|
it.each(["late-cancellation", "cleanup-failure", "cancelled cleanup-failure"])(
|
|
"preserves completed native-worker observations across %s",
|
|
async (scenario) => {
|
|
const { workspace, database } = await createFixture();
|
|
const original = await readFile(database.path);
|
|
const controller = new AbortController();
|
|
const cancellationReason = {
|
|
requested: "cancel during final root cleanup",
|
|
};
|
|
let cleanupAllowed = false;
|
|
let root: PrivateRuntimeRoot | undefined;
|
|
const provider = new SqliteDatabaseProvider(
|
|
process.env,
|
|
(spawn) =>
|
|
spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: spawn.arguments.map((argument, index) =>
|
|
index === 1 ? fileURLToPath(workerUrl) : argument,
|
|
),
|
|
}),
|
|
async () => {
|
|
root = await PrivateRuntimeRoot.create({ parent: workspace.root });
|
|
const acquired = root;
|
|
return {
|
|
path: acquired.path,
|
|
close: async () => {
|
|
if (scenario !== "cleanup-failure")
|
|
controller.abort(cancellationReason);
|
|
if (scenario !== "late-cancellation" && !cleanupAllowed)
|
|
throw new Error("Completed snapshot cleanup denied");
|
|
await acquired.close();
|
|
},
|
|
};
|
|
},
|
|
);
|
|
onTestFinished(async () => {
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
});
|
|
const result = await provider.inspect(
|
|
{
|
|
path: database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
worker_timeout_ms: 42000,
|
|
},
|
|
{ signal: controller.signal },
|
|
);
|
|
if (scenario === "late-cancellation") {
|
|
expect(controller.signal.aborted).toBe(true);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
cause: cancellationReason,
|
|
partialObservation: {
|
|
operation: "inspect_sqlite_database",
|
|
confidence: "observed",
|
|
subject: {
|
|
local_path: database.path,
|
|
identity: { sha256: digestForTest(original) },
|
|
},
|
|
parameters: {
|
|
path: database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
worker_timeout_ms: 42000,
|
|
},
|
|
normalized_result: {
|
|
worker_timeout_ms: 42000,
|
|
rows: { columns: ["id"], returned_rows: 1 },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (result.ok || result.error._tag !== "AnalysisCancelledError")
|
|
throw new Error("Expected cancellation after final root cleanup");
|
|
expect(result.error.cause).toBe(cancellationReason);
|
|
if (root === undefined) throw new Error("Missing acquired root");
|
|
await expectPrivateRootRemoved(root.path);
|
|
} else {
|
|
// Incomplete cleanup stays primary over a cancellation observed during it.
|
|
expect(controller.signal.aborted).toBe(
|
|
scenario === "cancelled cleanup-failure",
|
|
);
|
|
expect(result.ok ? undefined : result.error).toBeInstanceOf(
|
|
ProviderCleanupError,
|
|
);
|
|
if (result.ok) throw new Error("Expected an unresolved cleanup");
|
|
if (scenario === "cancelled cleanup-failure") {
|
|
const [cancellation] = result.error.secondaryFailures;
|
|
expect(cancellation?._tag).toBe("AnalysisCancelledError");
|
|
expect(cancellation?.cause).toBe(cancellationReason);
|
|
}
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
cleanupIncomplete: true,
|
|
partialObservation: {
|
|
operation: "inspect_sqlite_database",
|
|
confidence: "observed",
|
|
subject: {
|
|
local_path: database.path,
|
|
identity: { sha256: digestForTest(original) },
|
|
},
|
|
parameters: {
|
|
path: database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
worker_timeout_ms: 42000,
|
|
},
|
|
normalized_result: {
|
|
worker_timeout_ms: 42000,
|
|
rows: { columns: ["id"], returned_rows: 1 },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (root === undefined) throw new Error("Missing retained root");
|
|
await expect(access(root.path)).resolves.toBeUndefined();
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
await expectPrivateRootRemoved(root.path);
|
|
}
|
|
expect(Buffer.compare(await readFile(database.path), original)).toBe(0);
|
|
},
|
|
);
|
|
|
|
const digestForTest = (bytes: Buffer): string =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
|
|
it("inspects committed WAL schema and rows while preserving every source artifact", async () => {
|
|
const { database } = await createFixture(true);
|
|
const paths = [database.path, database.walPath, `${database.path}-shm`];
|
|
const original = await Promise.all(
|
|
paths.map(async (path) => ({
|
|
path,
|
|
bytes: await readFile(path),
|
|
stat: await lstat(path),
|
|
})),
|
|
);
|
|
const result = await createProvider().inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
schema_scope: "database",
|
|
row_limit: 1,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
const main = original[0];
|
|
const wal = original[1];
|
|
if (main === undefined || wal === undefined)
|
|
throw new Error("Missing fixture identity");
|
|
expect(result.value.result).toMatchObject({
|
|
artifact: {
|
|
path: database.path,
|
|
bytes: main.bytes.length,
|
|
sha256: createHash("sha256").update(main.bytes).digest("hex"),
|
|
},
|
|
wal: {
|
|
path: database.walPath,
|
|
bytes: wal.bytes.length,
|
|
sha256: createHash("sha256").update(wal.bytes).digest("hex"),
|
|
},
|
|
schema: {
|
|
completeness: "complete",
|
|
tables: expect.arrayContaining([
|
|
{
|
|
name: "records",
|
|
sql: expect.stringContaining("CREATE TABLE"),
|
|
root_page: expect.any(Number),
|
|
kind: "table",
|
|
without_rowid: false,
|
|
strict: false,
|
|
column_count: 7,
|
|
columns_completeness: "complete",
|
|
columns: expect.arrayContaining([
|
|
expect.objectContaining({ name: "generated_value", hidden: 2 }),
|
|
]),
|
|
},
|
|
]),
|
|
indexes: [
|
|
expect.objectContaining({ name: "records_text", table: "records" }),
|
|
],
|
|
views: [
|
|
{
|
|
name: "record_names",
|
|
sql: expect.stringContaining("SELECT text_value"),
|
|
},
|
|
],
|
|
triggers: [
|
|
{
|
|
name: "records_insert",
|
|
table: "records",
|
|
sql: expect.stringContaining("AFTER INSERT"),
|
|
},
|
|
],
|
|
},
|
|
rows: {
|
|
table: "records",
|
|
returned_rows: 1,
|
|
truncated: true,
|
|
row_limit: 1,
|
|
order: "rowid",
|
|
row_offset: 0,
|
|
next_row_offset: 1,
|
|
},
|
|
});
|
|
for (const artifact of original) {
|
|
expect(Buffer.compare(await readFile(artifact.path), artifact.bytes)).toBe(
|
|
0,
|
|
);
|
|
const after = await lstat(artifact.path);
|
|
expect({
|
|
ino: after.ino,
|
|
size: after.size,
|
|
mtimeMs: after.mtimeMs,
|
|
ctimeMs: after.ctimeMs,
|
|
}).toEqual({
|
|
ino: artifact.stat.ino,
|
|
size: artifact.stat.size,
|
|
mtimeMs: artifact.stat.mtimeMs,
|
|
ctimeMs: artifact.stat.ctimeMs,
|
|
});
|
|
}
|
|
});
|
|
|
|
it("preserves SQLite integer, binary, nonfinite, null, and text byte values", async () => {
|
|
const { database } = await createFixture();
|
|
const result = await createProvider().inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
row_limit: 10,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
wal: null,
|
|
rows: {
|
|
columns: [
|
|
"id",
|
|
"integer_value",
|
|
"blob_value",
|
|
"real_value",
|
|
"nullable_value",
|
|
"text_value",
|
|
"generated_value",
|
|
],
|
|
returned_rows: 3,
|
|
truncated: false,
|
|
values: [
|
|
[
|
|
{ type: "integer", value: "1" },
|
|
{ type: "integer", value: "9223372036854775807" },
|
|
{ type: "blob", hex: "001fefff" },
|
|
{ type: "real", value: "Infinity" },
|
|
{ type: "null" },
|
|
{
|
|
type: "text",
|
|
value: "first é\0value",
|
|
bytes_hex: Buffer.from("first é\0value").toString("hex"),
|
|
},
|
|
{
|
|
type: "text",
|
|
value: "integer",
|
|
bytes_hex: Buffer.from("integer").toString("hex"),
|
|
},
|
|
],
|
|
[
|
|
{ type: "integer", value: "2" },
|
|
{ type: "integer", value: "-9223372036854775808" },
|
|
{ type: "blob", hex: "" },
|
|
{ type: "real", value: "-Infinity" },
|
|
{
|
|
type: "text",
|
|
value: "present",
|
|
bytes_hex: Buffer.from("present").toString("hex"),
|
|
},
|
|
{
|
|
type: "text",
|
|
value: "second",
|
|
bytes_hex: Buffer.from("second").toString("hex"),
|
|
},
|
|
{
|
|
type: "text",
|
|
value: "integer",
|
|
bytes_hex: Buffer.from("integer").toString("hex"),
|
|
},
|
|
],
|
|
[
|
|
{ type: "integer", value: "3" },
|
|
{ type: "integer", value: "0" },
|
|
{ type: "blob", hex: "ff" },
|
|
{ type: "real", value: 1.25 },
|
|
{ type: "null" },
|
|
{ type: "text", bytes_hex: "ff00c0af" },
|
|
{
|
|
type: "text",
|
|
value: "integer",
|
|
bytes_hex: Buffer.from("integer").toString("hex"),
|
|
},
|
|
],
|
|
],
|
|
},
|
|
});
|
|
expect(() => JSON.stringify(result.value.result)).not.toThrow();
|
|
});
|
|
|
|
it("selects an exact quoted table and refuses to execute table input as SQL", async () => {
|
|
const { database } = await createFixture();
|
|
const provider = createProvider();
|
|
const selected = await provider.inspect({
|
|
path: database.path,
|
|
table: database.quotedTable,
|
|
row_limit: 1,
|
|
});
|
|
if (!selected.ok) throw selected.error;
|
|
expect(selected.value.result).toMatchObject({
|
|
rows: {
|
|
table: database.quotedTable,
|
|
columns: ["__proto__"],
|
|
values: [
|
|
[
|
|
{
|
|
type: "text",
|
|
value: "exact selected table",
|
|
bytes_hex: Buffer.from("exact selected table").toString("hex"),
|
|
},
|
|
],
|
|
],
|
|
returned_rows: 1,
|
|
truncated: false,
|
|
},
|
|
});
|
|
const unknown = await provider.inspect({
|
|
path: database.path,
|
|
table: 'records"; DROP TABLE records;--',
|
|
row_limit: 1,
|
|
});
|
|
expect(unknown).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
const after = await provider.inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
row_limit: 10,
|
|
});
|
|
if (!after.ok) throw after.error;
|
|
expect(after.value.result).toMatchObject({ rows: { returned_rows: 3 } });
|
|
});
|
|
|
|
it("does not execute views when selecting rows", async () => {
|
|
const { database } = await createFixture();
|
|
const result = await createProvider().inspect({
|
|
path: database.path,
|
|
table: "record_names",
|
|
row_limit: 1,
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
});
|
|
|
|
nativeTest.each(["absent.db", "absent-parent/absent.db"])(
|
|
"classifies a missing %s as an invalid value",
|
|
async (name) => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-missing-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path(name);
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({ path });
|
|
if (result.ok) throw new Error("Expected a missing database rejection");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["path"],
|
|
reason: "invalid_value",
|
|
message: `Selected SQLite database does not exist: ${path}`,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("attributes table and column selection failures to their own arguments", async () => {
|
|
const { database } = await createFixture();
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const projection = async (selection: {
|
|
table: string;
|
|
columns?: string[];
|
|
}) => {
|
|
const result = await provider.inspect({
|
|
path: database.path,
|
|
row_limit: 1,
|
|
...selection,
|
|
});
|
|
if (result.ok) throw new Error("Expected a rejected selection");
|
|
return projectAnalysisError(result.error);
|
|
};
|
|
expect(
|
|
await projection({ table: "records", columns: ["nope"] }),
|
|
).toMatchObject({
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["columns"],
|
|
reason: "invalid_format",
|
|
message: expect.stringMatching(
|
|
/column.*exact name.*table records.*nope/i,
|
|
),
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(await projection({ table: "absent" })).toMatchObject({
|
|
details: { issues: [{ path: ["table"] }] },
|
|
});
|
|
const selected = await provider.inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
columns: ["id"],
|
|
row_limit: 1,
|
|
});
|
|
if (!selected.ok) throw selected.error;
|
|
expect(selected.value.result).toMatchObject({ rows: { columns: ["id"] } });
|
|
});
|
|
|
|
it("reports a projection above SQLite's result-column capacity as capacity, not invalid input", async () => {
|
|
const { database } = await createFixture();
|
|
const reader = new DatabaseSync(database.path, { readOnly: true });
|
|
const engineColumnLimit = (() => {
|
|
try {
|
|
return reader.limits.column;
|
|
} finally {
|
|
reader.close();
|
|
}
|
|
})();
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const columns = (count: number): string[] =>
|
|
Array.from({ length: count }, () => "text_value");
|
|
const atLimit = await provider.inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
columns: columns(engineColumnLimit),
|
|
row_limit: 1,
|
|
});
|
|
if (!atLimit.ok) throw atLimit.error;
|
|
expect(atLimit.value.result).toMatchObject({
|
|
rows: { columns: columns(engineColumnLimit), returned_rows: 1 },
|
|
});
|
|
|
|
const overLimit = await provider.inspect({
|
|
path: database.path,
|
|
table: "records",
|
|
columns: columns(engineColumnLimit + 1),
|
|
row_limit: 1,
|
|
});
|
|
if (overLimit.ok) throw new Error("Expected a rejected projection");
|
|
const projection = projectAnalysisError(overLimit.error);
|
|
expect(projection).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
resource: "capacity",
|
|
reported_limits: {
|
|
selected_result_columns: engineColumnLimit + 1,
|
|
engine_result_column_limit: engineColumnLimit,
|
|
},
|
|
},
|
|
});
|
|
expect(JSON.stringify(projection)).not.toContain("invalid_format");
|
|
});
|
|
|
|
it("reports application-collation row failures as unavailable while retaining schema-only recovery", async () => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-collation-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = createSqliteApplicationCollationFixture(workspace.root);
|
|
const original = await readFile(path);
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const schema = await provider.inspect({ path, schema_scope: "database" });
|
|
if (!schema.ok) throw schema.error;
|
|
expect(schema.value.result).toMatchObject({
|
|
schema: {
|
|
tables: [
|
|
{
|
|
name: "items",
|
|
sql: expect.stringContaining("COLLATE APPLICATION_COLLATION"),
|
|
},
|
|
],
|
|
},
|
|
rows: null,
|
|
});
|
|
for (const columns of [undefined, ["value"], ["generated_value"]]) {
|
|
const result = await provider.inspect({
|
|
path,
|
|
table: "items",
|
|
...(columns === undefined ? {} : { columns }),
|
|
});
|
|
if (result.ok)
|
|
throw new Error("Expected unavailable application collation");
|
|
const projected = projectAnalysisError(result.error);
|
|
expect(projected).toMatchObject({
|
|
code: "capability_unavailable",
|
|
message: expect.stringContaining('rows of table "items"'),
|
|
remediation: {
|
|
action: expect.stringContaining("schema_scope: database"),
|
|
},
|
|
});
|
|
expect(JSON.stringify(projected)).not.toContain("invalid_format");
|
|
expect(projected.remediation.action).not.toContain(
|
|
"Select columns whose values were read",
|
|
);
|
|
}
|
|
expect(Buffer.compare(await readFile(path), original)).toBe(0);
|
|
});
|
|
|
|
nativeTest.runIf(process.platform !== "win32")(
|
|
"rejects a symlink database",
|
|
async () => {
|
|
const { database, workspace } = await createFixture();
|
|
const alias = workspace.path("alias.db");
|
|
await symlink(database.path, alias);
|
|
const provider = createProvider();
|
|
expect(await provider.inspect({ path: alias })).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"rejects an actual open rollback transaction without changing its journal",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const writer = new DatabaseSync(database.path);
|
|
writer.exec(
|
|
"BEGIN IMMEDIATE; INSERT INTO records(id, text_value) VALUES (4, 'uncommitted')",
|
|
);
|
|
try {
|
|
const main = await readFile(database.path);
|
|
const journalPath = `${database.path}-journal`;
|
|
const journal = await readFile(journalPath);
|
|
expect(journal.length).toBeGreaterThan(0);
|
|
expect(
|
|
await createProvider().inspect({ path: database.path }),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
expect(Buffer.compare(await readFile(database.path), main)).toBe(0);
|
|
expect(Buffer.compare(await readFile(journalPath), journal)).toBe(0);
|
|
} finally {
|
|
writer.exec("ROLLBACK");
|
|
writer.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
nativeTest.runIf(process.platform !== "win32")(
|
|
"rejects FIFO input without waiting for a writer",
|
|
async () => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-fifo-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path("database.fifo");
|
|
await promisify(execFile)("mkfifo", [path]);
|
|
const result = await createProvider().inspect({ path });
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"honors a cancelled caller before opening the database",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
const reason = { requested: "cancel before admission" };
|
|
controller.abort(reason);
|
|
const result = await createProvider().inspect(
|
|
{ path: database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok || result.error._tag !== "AnalysisCancelledError")
|
|
throw new Error("Expected pre-admission cancellation");
|
|
expect(result.error.cause).toBe(reason);
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"preserves the exact caller reason when cancellation lands before snapshot work",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
const reason = { requested: "cancel after admission" };
|
|
const provider = new SqliteDatabaseProvider({}, () => {
|
|
throw new Error("SQLite worker must not launch before snapshot work");
|
|
});
|
|
onTestFinished(() => provider.close());
|
|
|
|
const inspection = provider.inspect(
|
|
{ path: database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
controller.abort(reason);
|
|
const result = await inspection;
|
|
if (result.ok || result.error._tag !== "AnalysisCancelledError")
|
|
throw new Error("Expected cancellation before snapshot work");
|
|
expect(result.error.cause).toBe(reason);
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"keeps an owned worker failure primary when cancellation arrives during cleanup",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
const reason = { requested: "stop after worker exit" };
|
|
const provider = new SqliteDatabaseProvider(process.env, async (spawn) => {
|
|
const launched = await spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"-e",
|
|
'process.stderr.write("authored SQLite worker failure"); process.exitCode = 7',
|
|
],
|
|
});
|
|
const cleanup =
|
|
launched.cleanup ??
|
|
(() => cleanupOwnedProcessGroup(launched.ownership));
|
|
return {
|
|
...launched,
|
|
cleanup: async () => {
|
|
const outcome = await cleanup();
|
|
controller.abort(reason);
|
|
return outcome;
|
|
},
|
|
};
|
|
});
|
|
onTestFinished(async () => {
|
|
await provider.close();
|
|
});
|
|
|
|
const result = await provider.inspect(
|
|
{ path: database.path, table: "records" },
|
|
{ signal: controller.signal },
|
|
);
|
|
|
|
expect(controller.signal.reason).toBe(reason);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "ProviderAdapterError",
|
|
diagnostics: {
|
|
failure_kind: "exited",
|
|
stop_requested: false,
|
|
exit_code: 7,
|
|
captured_output: {
|
|
stderr: "authored SQLite worker failure",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"removes an acquired private root when cancelled before snapshot creation",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
let ownedRoot = "";
|
|
const provider = new SqliteDatabaseProvider(
|
|
{},
|
|
() => {
|
|
throw new Error("Cancelled request must not launch SQLite");
|
|
},
|
|
async () => {
|
|
const root = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-sqlite-cancel-root-",
|
|
});
|
|
ownedRoot = root.path;
|
|
controller.abort();
|
|
return root;
|
|
},
|
|
);
|
|
expect(
|
|
await provider.inspect(
|
|
{ path: database.path },
|
|
{ signal: controller.signal },
|
|
),
|
|
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
|
|
await expectPrivateRootRemoved(ownedRoot);
|
|
},
|
|
);
|
|
|
|
it.each(["UTF-8", "UTF-16le", "UTF-16be"] as const)(
|
|
"preserves %s text, a leading BOM and empty table selection",
|
|
async (encoding) => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-encoding-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path("encoding.db");
|
|
const writer = new DatabaseSync(path);
|
|
writer.exec(
|
|
`PRAGMA encoding='${encoding}'; CREATE TABLE "" ("1" TEXT, "0" TEXT)`,
|
|
);
|
|
const value = "\uFEFFé𝄞\0selected";
|
|
writer
|
|
.prepare('INSERT INTO "" VALUES (?, ?)')
|
|
.run(value, "second numeric column");
|
|
writer.close();
|
|
const bytes = Buffer.from(value, encoding === "UTF-8" ? "utf8" : "utf16le");
|
|
if (encoding === "UTF-16be") bytes.swap16();
|
|
const result = await createProvider().inspect({
|
|
path,
|
|
table: "",
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
database_encoding: encoding,
|
|
rows: {
|
|
table: "",
|
|
columns: ["1", "0"],
|
|
returned_rows: 1,
|
|
values: [
|
|
[
|
|
{ type: "text", value, bytes_hex: bytes.toString("hex"), encoding },
|
|
{ type: "text", value: "second numeric column" },
|
|
],
|
|
],
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("returns the first sample of a truncated rowid-shadowing table without a next offset", async () => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-shadowed-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path("shadowed.db");
|
|
const writer = new DatabaseSync(path);
|
|
writer.exec(
|
|
`CREATE TABLE shadowed(rowid TEXT, _rowid_ TEXT, oid TEXT);
|
|
INSERT INTO shadowed VALUES ('a', 'a', 'a'), ('b', 'b', 'b');`,
|
|
);
|
|
writer.close();
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({
|
|
path,
|
|
table: "shadowed",
|
|
row_limit: 1,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
rows: {
|
|
table: "shadowed",
|
|
order: "unspecified",
|
|
row_limit: 1,
|
|
returned_rows: 1,
|
|
truncated: true,
|
|
row_offset: 0,
|
|
next_row_offset: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("attributes a refused row offset to row_offset across the worker round trip", async () => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-shadowed-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path("shadowed.db");
|
|
const writer = new DatabaseSync(path);
|
|
writer.exec(
|
|
`CREATE TABLE shadowed(rowid TEXT, _rowid_ TEXT, oid TEXT);
|
|
INSERT INTO shadowed VALUES ('a', 'a', 'a'), ('b', 'b', 'b');`,
|
|
);
|
|
writer.close();
|
|
const provider = createProvider();
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({
|
|
path,
|
|
table: "shadowed",
|
|
row_offset: 1,
|
|
});
|
|
if (result.ok) throw new Error("Expected a rejected row offset");
|
|
// The worker refuses this offset itself, so the refusal has to survive the
|
|
// reply schema and arrive as an input error naming the caller's argument
|
|
// rather than as an unparsable reply.
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["row_offset"],
|
|
reason: "invalid_format",
|
|
message: expect.stringContaining("row_offset"),
|
|
},
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("retains an unavailable virtual module definition without executing it", async () => {
|
|
const workspace = await createTestWorkspace("rea-sqlite-virtual-");
|
|
onTestFinished(() => removeTestWorkspace(workspace.root));
|
|
const path = workspace.path("virtual.db");
|
|
const writer = new DatabaseSync(path);
|
|
writer.exec(
|
|
"CREATE VIRTUAL TABLE optional_search USING fts5(body); INSERT INTO optional_search VALUES ('selected source')",
|
|
);
|
|
writer.close();
|
|
const bytes = await readFile(path);
|
|
const declaration = bytes.indexOf(Buffer.from("USING fts5"));
|
|
if (declaration < 0)
|
|
throw new Error("SQLite virtual module declaration not found");
|
|
bytes.write("zzzz", declaration + Buffer.byteLength("USING "), "utf8");
|
|
await writeFile(path, bytes);
|
|
const provider = createProvider();
|
|
const result = await provider.inspect({ path, schema_scope: "database" });
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
rows: null,
|
|
schema: {
|
|
tables: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
name: "optional_search",
|
|
kind: "virtual",
|
|
sql: expect.stringContaining("USING zzzz"),
|
|
columns: [],
|
|
columns_completeness: "unsupported",
|
|
}),
|
|
]),
|
|
},
|
|
});
|
|
expect(
|
|
await provider.inspect({ path, table: "optional_search" }),
|
|
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
|
|
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
|
|
});
|
|
|
|
nativeTest(
|
|
"cancels an acquired SQLite worker and removes its private snapshot",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
const reason = { requested: "stop owned SQLite worker" };
|
|
let ownedRoot = "";
|
|
let pid: number | undefined;
|
|
const provider = new SqliteDatabaseProvider(
|
|
process.platform === "win32" && process.env.SystemRoot !== undefined
|
|
? { SystemRoot: process.env.SystemRoot }
|
|
: {},
|
|
async (spawn) => {
|
|
ownedRoot = spawn.cwd ?? "";
|
|
const requestPath = spawn.arguments[2];
|
|
if (requestPath === undefined)
|
|
throw new Error("SQLite worker arguments missing");
|
|
const launched = await spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"--input-type=module",
|
|
"-e",
|
|
`await import(${JSON.stringify(workerUrl.href)}); process.stdout.write('ready\\n'); setInterval(() => {}, 1000);`,
|
|
"sqlite-worker-test",
|
|
requestPath,
|
|
],
|
|
});
|
|
pid = launched.process.pid;
|
|
await waitForProviderProcessReady(launched.process);
|
|
await access(`${ownedRoot}/reply.json`);
|
|
setImmediate(() => controller.abort(reason));
|
|
return launched;
|
|
},
|
|
);
|
|
const result = await provider.inspect(
|
|
{ path: database.path, table: "records" },
|
|
{ signal: controller.signal },
|
|
);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisCancelledError" },
|
|
});
|
|
if (!result.ok && result.error._tag === "AnalysisCancelledError")
|
|
expect(result.error.cause).toBe(reason);
|
|
if (pid === undefined) throw new Error("SQLite worker not acquired");
|
|
const acquiredPid = pid;
|
|
expect(() => process.kill(acquiredPid, 0)).toThrow();
|
|
await expectPrivateRootRemoved(ownedRoot);
|
|
},
|
|
);
|
|
|
|
nativeTest(
|
|
"reports the worker's V8 heap report even when its output overflows the diagnostic budget",
|
|
async () => {
|
|
const { database } = await createFixture();
|
|
const provider = new SqliteDatabaseProvider({}, (spawn) =>
|
|
spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"-e",
|
|
`process.stderr.write('FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory\\n'); process.stdout.write('x'.repeat(${String(SQLITE_DATABASE_LIMITS.diagnosticBytes + 1)})); setInterval(() => {}, 1000)`,
|
|
],
|
|
}),
|
|
);
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({ path: database.path });
|
|
if (result.ok) throw new Error("Expected heap exhaustion");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
resource: "memory",
|
|
reported_limits: { javascript_heap_bytes: 256 * 1024 * 1024 },
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
nativeTest.each([
|
|
["cancelled", "cancelled"],
|
|
["output-limit", "resource_constraint"],
|
|
["heap-diagnostic", "resource_constraint"],
|
|
] as const)(
|
|
"preserves the typed %s worker failure through retained cleanup retry",
|
|
async (scenario, code) => {
|
|
const { database } = await createFixture();
|
|
const controller = new AbortController();
|
|
const reason = { requested: "cancel retained SQLite worker" };
|
|
let cleanupAllowed = false;
|
|
let ownedRoot = "";
|
|
let launches = 0;
|
|
const provider = new SqliteDatabaseProvider({}, async (spawn) => {
|
|
launches += 1;
|
|
ownedRoot = spawn.cwd ?? "";
|
|
const script =
|
|
scenario === "heap-diagnostic"
|
|
? "process.stderr.write('JavaScript heap out of memory'); process.exitCode = 3"
|
|
: scenario === "output-limit"
|
|
? `process.stdout.write('x'.repeat(${String(SQLITE_DATABASE_LIMITS.diagnosticBytes + 1)})); setInterval(() => {}, 1000)`
|
|
: "process.stdout.write('ready\\n'); setInterval(() => {}, 1000)";
|
|
const launched = await spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: ["-e", script],
|
|
});
|
|
const cleanupOwned =
|
|
launched.cleanup ??
|
|
(() => cleanupOwnedProcessGroup(launched.ownership));
|
|
if (scenario === "cancelled") {
|
|
await waitForProviderProcessReady(launched.process);
|
|
setImmediate(() => controller.abort(reason));
|
|
}
|
|
return {
|
|
...launched,
|
|
cleanup: async () => {
|
|
const cleaned = await cleanupOwned();
|
|
if (!cleanupAllowed)
|
|
throw new Error("controlled SQLite worker cleanup refusal");
|
|
return cleaned;
|
|
},
|
|
};
|
|
});
|
|
onTestFinished(async () => {
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
});
|
|
const result = await provider.inspect(
|
|
{ path: database.path },
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected retained worker cleanup");
|
|
expect(projectAnalysisError(result.error).code).toBe("cleanup_incomplete");
|
|
const primary = result.error.cleanupPrimary;
|
|
if (primary === undefined) throw new Error("Missing typed worker failure");
|
|
expect(projectAnalysisError(primary).code).toBe(code);
|
|
if (scenario === "cancelled")
|
|
expect(result.error.cleanupPrimary?.cause).toBe(reason);
|
|
expect(await provider.inspect({ path: database.path })).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true },
|
|
});
|
|
expect(launches).toBe(1);
|
|
await access(ownedRoot);
|
|
cleanupAllowed = true;
|
|
await provider.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(projectAnalysisError(result.error).code).toBe(code);
|
|
if (scenario === "cancelled")
|
|
expect(result.error.cleanupPrimary?.cause).toBe(reason);
|
|
await expectPrivateRootRemoved(ownedRoot);
|
|
},
|
|
);
|
|
|
|
nativeTest.each([
|
|
{ encoding: "malformed UTF-8", code: "unreadable_output" },
|
|
{ encoding: "literal replacement character", code: "capability_unavailable" },
|
|
{ encoding: "UTF-8 BOM", code: "unreadable_output" },
|
|
{ encoding: "malformed JSON", code: "unreadable_output" },
|
|
] as const)(
|
|
"admits complete SQLite reply-file JSON without replacement decoding: $encoding",
|
|
async ({ encoding, code }) => {
|
|
const { database } = await createFixture();
|
|
let ownedRoot = "";
|
|
const provider = new SqliteDatabaseProvider(process.env, async (spawn) => {
|
|
ownedRoot = spawn.cwd ?? "";
|
|
const requestPath = spawn.arguments[2];
|
|
if (requestPath === undefined)
|
|
throw new Error("SQLite request path missing");
|
|
const request = JSON.parse(await readFile(requestPath, "utf8")) as {
|
|
reply_path: string;
|
|
};
|
|
const valid = Buffer.from(
|
|
JSON.stringify({
|
|
ok: false,
|
|
reason: "unavailable",
|
|
message: "recorded \ufffd diagnostic",
|
|
}),
|
|
);
|
|
let bytes = valid;
|
|
if (encoding === "malformed UTF-8") {
|
|
const replacementOffset = valid.indexOf(Buffer.from("\ufffd"));
|
|
expect(replacementOffset).toBeGreaterThanOrEqual(0);
|
|
bytes = Buffer.concat([
|
|
valid.subarray(0, replacementOffset),
|
|
Buffer.from([0xff]),
|
|
valid.subarray(replacementOffset + 3),
|
|
]);
|
|
} else if (encoding === "UTF-8 BOM") {
|
|
bytes = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), valid]);
|
|
} else if (encoding === "malformed JSON") {
|
|
bytes = Buffer.from("{ malformed reply");
|
|
}
|
|
await writeFile(request.reply_path, bytes, { flag: "wx", mode: 0o600 });
|
|
return spawnOwnedProviderProcess({
|
|
...spawn,
|
|
arguments: [
|
|
"-e",
|
|
"process.stderr.write('sqlite-reply-marker'); process.exit(0)",
|
|
],
|
|
});
|
|
});
|
|
onTestFinished(() => provider.close());
|
|
const result = await provider.inspect({ path: database.path });
|
|
if (result.ok) throw new Error("Expected worker refusal");
|
|
expect(projectAnalysisError(result.error).code).toBe(code);
|
|
expect(result.error.capturedOutput).toMatchObject({
|
|
stderr: "sqlite-reply-marker",
|
|
truncated: false,
|
|
});
|
|
if (encoding === "literal replacement character")
|
|
expect(projectAnalysisError(result.error).message).toBe(
|
|
"recorded \ufffd diagnostic",
|
|
);
|
|
else
|
|
expect(result.error.cause).toBeInstanceOf(
|
|
encoding === "malformed UTF-8" ? TypeError : SyntaxError,
|
|
);
|
|
await provider.close();
|
|
await expectPrivateRootRemoved(ownedRoot);
|
|
},
|
|
);
|