mirror of
https://github.com/morluto/rea.git
synced 2026-11-14 10:43:30 +00:00
* 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.
454 lines
15 KiB
TypeScript
454 lines
15 KiB
TypeScript
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import { parseEvidence, createEvidence } from "../../src/domain/evidence.js";
|
|
import { createEvidenceBundle } from "../../src/domain/evidenceBundle.js";
|
|
import { rustBinarySchema } from "../../src/domain/rust/rustBinary.js";
|
|
import { analysisViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
|
|
import { readEvidenceBundle } from "../../src/application/EvidenceBundleFiles.js";
|
|
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
|
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
|
import {
|
|
createRustImage,
|
|
createRustHashOverflowImage,
|
|
rustDependencyPayload,
|
|
} from "../fixtures/rust/image.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
|
|
cliTest(
|
|
"keeps compiler-prefix coverage in CLI summaries until the selected budget admits every hash",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-compiler-summary-");
|
|
const path = join(root, "compiler-paths.elf");
|
|
await writeFile(path, createRustHashOverflowImage());
|
|
for (const maxHashes of [64, 65]) {
|
|
const output = join(root, `source-${String(maxHashes)}.json`);
|
|
const delivered = await cli.run({
|
|
arguments: [
|
|
"inspect-rust-binary",
|
|
path,
|
|
"--detail",
|
|
"summary",
|
|
"--evidence_output",
|
|
output,
|
|
"--max-rustc-hashes",
|
|
String(maxHashes),
|
|
"--json",
|
|
],
|
|
});
|
|
expect(delivered.exitCode, delivered.stderr).toBe(0);
|
|
const summary = analysisViewResultSchema.parse(
|
|
(delivered.json as { summary: { normalized_result: unknown } }).summary
|
|
.normalized_result,
|
|
);
|
|
if (
|
|
summary.kind !== "summary" ||
|
|
summary.parent_operation !== "inspect_rust_binary"
|
|
)
|
|
throw new Error("Expected a Rust summary");
|
|
expect(summary.summary.rustc_commit).toEqual({
|
|
evidence_kind: "inferred",
|
|
hashes_count: maxHashes,
|
|
...(maxHashes === 64
|
|
? {
|
|
coverage: {
|
|
status: "partial",
|
|
reason: "max_rustc_hashes",
|
|
effective_limit: 64,
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
expect(summary.summary.auditable_metadata?.counts.packages).toBe(2);
|
|
}
|
|
},
|
|
);
|
|
|
|
/** Both authenticated retention and portable imports query the same captured facts. */
|
|
const assertRetainedPages = async (
|
|
call: Awaited<ReturnType<typeof connectLocalToolsMcp>>["call"],
|
|
evidenceId: string,
|
|
exported: string,
|
|
) => {
|
|
for (const [filter, expected] of [
|
|
[
|
|
{ names: ["same"], versions: ["2"], fields: ["name", "version"] },
|
|
[{ index: 1, name: "same", version: "2" }],
|
|
],
|
|
[{ names: ["same"], indices: [0], fields: [] }, [{ index: 0 }]],
|
|
[{ names: ["same"], versions: [], fields: [] }, []],
|
|
] as const) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source: { kind: "retained-evidence", evidence_id: evidenceId },
|
|
view: { kind: "rust", collection: "packages", ...filter },
|
|
});
|
|
expect(reply.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({ page: { rows: expected } });
|
|
}
|
|
|
|
for (const source of [
|
|
{ kind: "retained-evidence", evidence_id: evidenceId },
|
|
{ kind: "evidence-file", path: exported },
|
|
]) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
names: ["same"],
|
|
indices: [0, 1, 99],
|
|
offset: 1,
|
|
limit: 1,
|
|
fields: [],
|
|
},
|
|
});
|
|
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
parent_evidence_id: evidenceId,
|
|
page: { rows: [{ index: 1 }] },
|
|
coverage: {
|
|
total: 2,
|
|
examined: 1,
|
|
exhausted: true,
|
|
next_offset: null,
|
|
},
|
|
});
|
|
const edgeReply = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "edges",
|
|
from_indices: [1, 3],
|
|
to_indices: [0],
|
|
fields: ["from_index", "to_index"],
|
|
},
|
|
});
|
|
expect(edgeReply.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(edgeReply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
page: {
|
|
collection: "edges",
|
|
rows: [
|
|
{ index: 1, from_index: 1, to_index: 0 },
|
|
{ index: 3, from_index: 3, to_index: 0 },
|
|
],
|
|
},
|
|
});
|
|
const empty = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
names: [],
|
|
versions: ["1"],
|
|
fields: ["name"],
|
|
},
|
|
});
|
|
expect(
|
|
parseEvidence(empty.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
page: { rows: [] },
|
|
coverage: { total: 0, exhausted: true },
|
|
});
|
|
}
|
|
};
|
|
|
|
cliTest(
|
|
"queries stable Rust packages and edges through real CLI/MCP and portable Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-views-");
|
|
const { call } = await connectLocalToolsMcp();
|
|
for (const format of ["elf", "pe", "macho"] as const) {
|
|
const path = join(root, `source.${format}`);
|
|
const output = join(root, `${format}-source.json`);
|
|
const payload = rustDependencyPayload({
|
|
packages: [
|
|
{
|
|
name: "same",
|
|
version: "1",
|
|
source: "crates.io",
|
|
dependencies: [2],
|
|
features: ["default"],
|
|
},
|
|
{
|
|
name: "same",
|
|
version: "2",
|
|
source: "git+https://upstream.example/repo",
|
|
dependencies: [0, 2],
|
|
},
|
|
{ name: "leaf", version: "1", source: "local" },
|
|
{
|
|
name: "app",
|
|
version: "1",
|
|
source: "local",
|
|
dependencies: [0, 1],
|
|
root: true,
|
|
},
|
|
],
|
|
});
|
|
await writeFile(path, createRustImage({ format, payload }));
|
|
const fullReply = await call("inspect_rust_binary", { path });
|
|
expect(fullReply.isError).not.toBe(true);
|
|
const full = parseEvidence(fullReply.structuredContent);
|
|
const report = rustBinarySchema.parse(full.normalized_result);
|
|
const explicit = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "complete",
|
|
});
|
|
expect(parseEvidence(explicit.structuredContent)).toEqual(full);
|
|
const selection = {
|
|
collection: "packages",
|
|
names: ["same"],
|
|
fields: ["name", "version", "dependencies", "features"],
|
|
};
|
|
const direct = await call("inspect_rust_binary", {
|
|
path,
|
|
view: selection,
|
|
});
|
|
expect(direct.isError, JSON.stringify(direct)).not.toBe(true);
|
|
const selected = parseEvidence(
|
|
direct.structuredContent,
|
|
).normalized_result;
|
|
expect(selected).toMatchObject({
|
|
kind: "rust",
|
|
parent_evidence_id: full.evidence_id,
|
|
page: {
|
|
collection: "packages",
|
|
rows: [
|
|
{
|
|
index: 0,
|
|
name: "same",
|
|
version: "1",
|
|
dependencies: [2],
|
|
features: ["default"],
|
|
},
|
|
{ index: 1, name: "same", version: "2", dependencies: [0, 2] },
|
|
],
|
|
},
|
|
});
|
|
const summary = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "summary",
|
|
});
|
|
expect(summary.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(summary.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
kind: "summary",
|
|
summary: {
|
|
auditable_metadata: {
|
|
counts: { packages: 4, edges: 5 },
|
|
format_version: null,
|
|
},
|
|
},
|
|
source_coverage: {
|
|
embedded_records: "complete",
|
|
runtime_dependencies: "unknown",
|
|
},
|
|
});
|
|
const delivered = await cli.run({
|
|
arguments: [
|
|
"inspect-rust-binary",
|
|
path,
|
|
"--view",
|
|
JSON.stringify(selection),
|
|
"--evidence_output",
|
|
output,
|
|
"--json",
|
|
],
|
|
});
|
|
expect(delivered.exitCode, delivered.stderr).toBe(0);
|
|
expect(delivered.json).toMatchObject({
|
|
summary: { normalized_result: selected },
|
|
});
|
|
const bundle = await readEvidenceBundle(output);
|
|
if (!bundle.ok) throw bundle.error;
|
|
expect(
|
|
bundle.value.records.find(
|
|
(row) => row.evidence_id === full.evidence_id,
|
|
),
|
|
).toEqual({ ...full, caller_input_path: path });
|
|
const exported = join(root, `${format}-record.json`);
|
|
await writeFile(exported, JSON.stringify(full));
|
|
await unlink(path);
|
|
|
|
await assertRetainedPages(call, full.evidence_id, exported);
|
|
const malformed = structuredClone(report);
|
|
malformed.auditable_metadata!.dependency_edges[0]!.to_index = 999;
|
|
if (full.subject == null)
|
|
throw new Error("Missing complete Rust subject");
|
|
const invalid = createEvidence(
|
|
{
|
|
path,
|
|
sha256: report.artifact.sha256,
|
|
format: format === "macho" ? "mach-o" : format,
|
|
...(full.subject.architecture === null
|
|
? {}
|
|
: { architecture: full.subject.architecture }),
|
|
},
|
|
full.provider,
|
|
{
|
|
operation: full.operation,
|
|
parameters: full.parameters,
|
|
result: malformed,
|
|
},
|
|
);
|
|
expect(
|
|
parseMcpToolError(
|
|
await call("inspect_analysis_view", {
|
|
source: { kind: "inline", evidence: invalid },
|
|
view: { kind: "rust", collection: "edges" },
|
|
}),
|
|
).error,
|
|
).toMatchObject({ code: "invalid_request" });
|
|
}
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"queries historical feature omissions from authenticated bundles without changing the parent",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-historical-features-");
|
|
const path = join(root, "source.elf");
|
|
await writeFile(
|
|
path,
|
|
createRustImage({
|
|
format: "elf",
|
|
payload: rustDependencyPayload({
|
|
packages: [{ name: "old", version: "1", source: "local" }],
|
|
}),
|
|
}),
|
|
);
|
|
const { call } = await connectLocalToolsMcp();
|
|
const captured = await call("inspect_rust_binary", { path });
|
|
expect(captured.isError, JSON.stringify(captured.content)).not.toBe(true);
|
|
const full = parseEvidence(captured.structuredContent);
|
|
const report = rustBinarySchema.parse(full.normalized_result);
|
|
const metadata = report.auditable_metadata;
|
|
if (metadata === null) throw new Error("Expected Rust dependency metadata");
|
|
if (full.subject == null)
|
|
throw new Error("Expected Rust artifact identity");
|
|
const historical = createEvidence(
|
|
{
|
|
path,
|
|
sha256: report.artifact.sha256,
|
|
format: "elf",
|
|
...(full.subject.architecture === null
|
|
? {}
|
|
: { architecture: full.subject.architecture }),
|
|
},
|
|
full.provider,
|
|
{
|
|
operation: full.operation,
|
|
parameters: full.parameters,
|
|
result: {
|
|
...report,
|
|
auditable_metadata: {
|
|
...metadata,
|
|
packages: metadata.packages.map(
|
|
({ features: _features, ...row }) => row,
|
|
),
|
|
},
|
|
},
|
|
},
|
|
);
|
|
const bundle = join(root, "historical-bundle.json");
|
|
const record = join(root, "historical-record.json");
|
|
await writeFile(bundle, JSON.stringify(createEvidenceBundle([historical])));
|
|
await writeFile(record, JSON.stringify(historical));
|
|
await unlink(path);
|
|
const view = { kind: "rust", collection: "packages", fields: ["features"] };
|
|
for (const source of [
|
|
{ kind: "retained-evidence", evidence_id: historical.evidence_id },
|
|
{ kind: "evidence-file", path: record },
|
|
]) {
|
|
const reply = await cli.run({
|
|
arguments: [
|
|
"inspect-analysis-view",
|
|
JSON.stringify({ source, view }),
|
|
...(source.kind === "retained-evidence"
|
|
? ["--evidence-bundle", bundle]
|
|
: []),
|
|
"--json",
|
|
],
|
|
});
|
|
expect(reply.exitCode, reply.stderr + JSON.stringify(reply.json)).toBe(0);
|
|
expect(parseEvidence(reply.json).normalized_result).toMatchObject({
|
|
parent_evidence_id: historical.evidence_id,
|
|
page: { rows: [{ index: 0, features: null }] },
|
|
});
|
|
}
|
|
const imported = await call("import_evidence_bundle", { path: bundle });
|
|
expect(imported.isError, JSON.stringify(imported.content)).not.toBe(true);
|
|
for (const fields of [["features"], [], ["features"]]) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: historical.evidence_id,
|
|
},
|
|
view: { ...view, fields },
|
|
});
|
|
expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
parent_evidence_id: historical.evidence_id,
|
|
page: {
|
|
rows: [
|
|
fields.length === 0 ? { index: 0 } : { index: 0, features: null },
|
|
],
|
|
},
|
|
});
|
|
}
|
|
expect(JSON.parse(await readFile(record, "utf8"))).toEqual(historical);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"preserves unknown Rust metadata and refuses invalid compact CLI choices before reading",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-view-missing-");
|
|
const path = join(root, "plain.elf");
|
|
await writeFile(
|
|
path,
|
|
createRustImage({ format: "elf", omitSection: true }),
|
|
);
|
|
const { call } = await connectLocalToolsMcp();
|
|
const summary = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "summary",
|
|
});
|
|
expect(summary.isError).not.toBe(true);
|
|
const view = analysisViewResultSchema.parse(
|
|
parseEvidence(summary.structuredContent).normalized_result,
|
|
);
|
|
expect(view).toMatchObject({
|
|
summary: { auditable_metadata: null },
|
|
source_coverage: {
|
|
embedded_records: "unknown",
|
|
runtime_dependencies: "unknown",
|
|
},
|
|
});
|
|
const missing = join(root, "never-created");
|
|
for (const args of [
|
|
["--detail", "summary"],
|
|
["--view", "{"],
|
|
["--detail", "complete", "--view", '{"collection":"packages"}'],
|
|
]) {
|
|
const refused = await cli.run({
|
|
arguments: ["inspect-rust-binary", missing, ...args, "--json"],
|
|
});
|
|
expect(refused.exitCode).toBe(1);
|
|
expect(refused.json).toMatchObject({ code: "invalid_request" });
|
|
expect(JSON.stringify(refused.json)).not.toContain("ENOENT");
|
|
}
|
|
expect((await readFile(path)).length).toBeGreaterThan(0);
|
|
},
|
|
);
|