Files
reverse-engineering-anything/tests/boundary/process/javascriptVerifierBoundedEvidence.test.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* refactor: prune internal seams and preserve configuration ownership

* fix(setup): keep YAML aliases and inherited registration settings on refresh

- Reuse an unchanged YAML collection only while every alias in it still
  resolves after the edit; otherwise rebuild it so the alias becomes its
  value. A sibling anchor replaced or moved in the same refresh no longer
  leaves an unresolved alias in Hermes or Goose output.
- Keep a connection selector whose existing value already chooses stdio
  (for example an inherited Hermes transport: stdio) instead of superseding
  it; remote selectors are still replaced. Explicit stdio selectors are
  therefore no longer rewritten (Kimi expectation updated).
- When the whole Hermes registration is inherited through a merge,
  override it with that merge and write only the changed fields.

* fix(setup): edit registration fields whose YAML key is an alias

A key written as an alias to a string scalar (for example *field with
field: &field env) was treated as a source-only key, so any change to
its value was refused in Hermes and Goose. Resolve such keys to their
string, keep the aliased spelling while its anchor survives the edit,
and write the plain key when the same edit rebuilds the anchor.

* test(process): bound cleanup-boundary waits and always release gates

Both cancellation tests awaited a bare started promise and released
their gates only on success, so a missed boundary waited for the outer
deadline and could leave cleanup parked. Use awaitAcknowledgement and
release and join the cleanup in finally.

* fix(setup): repair only aliases an edit leaves unresolved

Replace the pre-edit alias survival guess, which expanded aliases to
sibling anchors that survive unchanged, with an exact repair: record
what each alias in the replaced value resolves to, write the edit, then
replace only aliases left without a preceding anchor by that value.

- Keep an aliased key's source position, so its anchor stays ahead of
  later aliases.
- Treat an aliased key as its string only when no other key in the map
  resolves to the same string; an ambiguous one stays guarded.
- Override an inherited Hermes registration only when the requested
  entry keeps every inherited field, so an explicit replacement can drop
  remote selectors.

* fix(setup): repair aliases whose binding an edit changes, keeping comments

An alias whose own anchor was removed could resolve to an earlier anchor
with the same name and silently change value. Record the node each alias
resolved to and repair any alias that no longer resolves to that node.
Carry the alias's comments and spacing onto the repaired value.

* test(setup): split the Hermes anchor and merge refresh cases

* fix(setup): refuse removing an overridden key its merge would restore

The merge-retention check skipped keys the mapping also overrides, so
dropping such a key (for example a remote url during an explicit stdio
replacement) let the retained merge restore it. Check removal before
skipping overridden keys, so the edit is refused with the existing
message instead of producing a registration that cannot be applied.
2026-11-14 04:08:37 -05:00

146 lines
4.6 KiB
TypeScript

import { execFile } from "node:child_process";
import { realpath, truncate, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
applicationFixture,
electronRuntimeEvidence,
SOURCE,
} from "../../fixtures/javascriptRuntimeReconciliation.js";
const execute = promisify(execFile);
it.each(["once", "always", "physical"] as const)(
"retries the exact Evidence descriptor after close denial: %s",
async (denial) => {
const root = await createTestTempDirectory("rea-verifier-evidence-close-");
const evidence = join(root, "evidence.json");
const preload = join(root, "instrument.mjs");
const application = await applicationFixture();
await writeFile(
evidence,
JSON.stringify(electronRuntimeEvidence(application, SOURCE)),
);
const selected = await realpath(evidence);
await writeFile(
preload,
`
import fs from 'node:fs';
import { syncBuiltinESMExports } from 'node:module';
const open = fs.promises.open;
let attempts = 0;
fs.promises.open = async function(path, ...args) {
const handle = await open.call(this, path, ...args);
if (String(path) === ${JSON.stringify(selected)}) {
const close = handle.close.bind(handle);
if (${JSON.stringify(denial)} === 'physical')
handle.read = async () => { throw new Error('authored Evidence read failure'); };
handle.close = async () => {
attempts++;
if (${JSON.stringify(denial)} === 'always' || attempts === 1)
throw new Error('authored Evidence close denial');
return close();
};
}
return handle;
};
syncBuiltinESMExports();
process.on('exit', () => fs.writeSync(2, 'Evidence close attempts: ' + attempts + '\\n'));
`,
);
const invocation = execute(
process.execPath,
[
"--import",
preload,
"scripts/verify/javascript/runtime-observation.mjs",
"--application",
application,
"--runtime-evidence",
evidence,
],
{ timeout: 20_000 },
);
if (denial === "once") {
const result = await invocation;
expect(JSON.parse(result.stdout)).toMatchObject({ verified: true });
expect(result.stderr).toContain("Evidence close attempts: 2");
expect(result.stderr).not.toContain("authored Evidence close denial");
return;
}
const result: unknown = await invocation.then(
() => {
throw new Error("Expected Evidence failure");
},
(error: unknown) => error,
);
expect(result).toMatchObject({
stderr: expect.stringContaining("Evidence close attempts: 2"),
});
if (
!(result instanceof Error) ||
!("stderr" in result) ||
typeof result.stderr !== "string"
)
throw new Error("Missing verifier diagnostics");
if (denial === "always") {
expect(result.stderr).toContain("authored Evidence close denial");
expect(result.stderr).toContain("Verifier cleanup failed");
expect(result.stderr).toContain("cleanupLeases");
} else {
expect(result.stderr).toContain("authored Evidence read failure");
expect(result.stderr).not.toContain("authored Evidence close denial");
expect(result.stderr).not.toContain("Verifier cleanup failed");
}
},
);
it("refuses oversized Evidence before a whole-file read", async () => {
const root = await createTestTempDirectory("rea-verifier-evidence-limit-");
const evidence = join(root, "evidence.json");
const preload = join(root, "instrument.mjs");
await writeFile(evidence, "");
await truncate(evidence, 64 * 1024 * 1024 + 1);
const selected = await realpath(evidence);
await writeFile(
preload,
`
import fs from 'node:fs';
import { syncBuiltinESMExports } from 'node:module';
const readFile = fs.promises.readFile;
fs.promises.readFile = function(path, ...args) {
if (String(path) === ${JSON.stringify(selected)}) throw new Error('oversized whole-file read');
return readFile.call(this, path, ...args);
};
syncBuiltinESMExports();
`,
);
const application = await applicationFixture();
const result = await execute(
process.execPath,
[
"--import",
preload,
"scripts/verify/javascript/runtime-observation.mjs",
"--application",
application,
"--runtime-evidence",
evidence,
],
{ timeout: 20_000 },
).then(
() => {
throw new Error("Expected oversized Evidence rejection");
},
(error: unknown) => error,
);
expect(result).toMatchObject({
stderr: expect.stringContaining(
`Runtime Evidence exceeds 64 MiB: ${evidence}`,
),
});
});