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.
170 lines
5.4 KiB
JavaScript
170 lines
5.4 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { realpath } from "node:fs/promises";
|
|
import { parseArgs } from "node:util";
|
|
|
|
import { readStableArtifact } from "../../../dist/artifacts/readStableArtifact.js";
|
|
import { ArtifactReaderFailure } from "../../../dist/artifacts/ArtifactReader.js";
|
|
import {
|
|
ArtifactFileCleanupFailure,
|
|
artifactFileCompletion,
|
|
artifactFilePrimaryFacts,
|
|
} from "../../../dist/artifacts/ArtifactFileOperation.js";
|
|
|
|
import { reconcileJavaScriptRuntimeEvidence } from "../../../dist/application/javascript/JavaScriptRuntimeReconciliationService.js";
|
|
import { analyzeJavaScriptApplicationWithOwnedScope } from "../../lib/javascript-application-analysis.mjs";
|
|
import { withVerifierArtifactScope } from "../../lib/verifier-artifact-scope.mjs";
|
|
import {
|
|
completeVerifierRun,
|
|
createVerifierRun,
|
|
} from "../../lib/verifier-run.mjs";
|
|
|
|
const verifierRun = createVerifierRun();
|
|
|
|
const options = parseArgs({
|
|
options: {
|
|
application: { type: "string" },
|
|
cache: { type: "string", multiple: true, default: [] },
|
|
assets: { type: "string", multiple: true, default: [] },
|
|
"runtime-evidence": { type: "string", multiple: true, default: [] },
|
|
mapping: { type: "string", multiple: true, default: [] },
|
|
},
|
|
strict: true,
|
|
allowPositionals: false,
|
|
}).values;
|
|
|
|
if (options.application === undefined)
|
|
throw new Error("--application must name an operator-provided app/ASAR path");
|
|
if (options["runtime-evidence"].length === 0)
|
|
throw new Error(
|
|
"At least one --runtime-evidence path from inspect_web_page or inspect_electron_page is required",
|
|
);
|
|
|
|
const requestedLayers = [
|
|
{ role: "application", path: options.application },
|
|
...options.cache.map((path) => ({ role: "cache", path })),
|
|
...options.assets.map((path) => ({ role: "assets", path })),
|
|
];
|
|
const staticLayers = [];
|
|
for (const layer of requestedLayers) {
|
|
const analyzed = await analyzeJavaScriptApplicationWithOwnedScope({
|
|
input_path: layer.path,
|
|
format: "auto",
|
|
});
|
|
staticLayers.push({
|
|
role: layer.role,
|
|
analysis: analyzed,
|
|
runtime_mappings: [],
|
|
});
|
|
}
|
|
for (const encoded of options.mapping) {
|
|
const mapping = parseMapping(encoded, staticLayers.length);
|
|
staticLayers[mapping.layer].runtime_mappings.push(mapping.value);
|
|
}
|
|
const runtimeObservations = [];
|
|
for (const path of options["runtime-evidence"]) {
|
|
const observations = await readEvidenceFile(path);
|
|
for (const observation of observations) runtimeObservations.push(observation);
|
|
}
|
|
const reconciled = reconcileJavaScriptRuntimeEvidence({
|
|
static_layers: staticLayers,
|
|
runtime_observations: runtimeObservations,
|
|
});
|
|
if (!reconciled.ok) throw reconciled.error;
|
|
const result = reconciled.value.evidence.normalized_result;
|
|
|
|
process.stdout.write(
|
|
`${JSON.stringify({
|
|
verifier_run: await completeVerifierRun(verifierRun),
|
|
reconciliation_id: result.reconciliation_id,
|
|
evidence_id: reconciled.value.evidence.evidence_id,
|
|
graph_id: result.graph.graph_id,
|
|
layers: result.static_layers.map((layer) => ({
|
|
role: layer.role,
|
|
evidence_id: layer.evidence_id,
|
|
graph_id: layer.graph_id,
|
|
root_artifact_sha256: layer.root_artifact_sha256,
|
|
input_path: layer.input_path,
|
|
})),
|
|
runtime_captures: result.runtime_captures.map((capture) => ({
|
|
evidence_id: capture.evidence_id,
|
|
capture_sha256: capture.capture_sha256,
|
|
target_location: capture.target_location,
|
|
})),
|
|
summary: result.summary,
|
|
coverage: result.coverage,
|
|
unmatched_reasons: counted(
|
|
result.reconciliations
|
|
.filter(({ status }) => status !== "matched")
|
|
.map(({ reason }) => reason),
|
|
),
|
|
static_load_states: counted(
|
|
result.static_load_states.map(({ status }) => status),
|
|
),
|
|
verified: true,
|
|
})}\n`,
|
|
);
|
|
|
|
async function readEvidenceFile(path) {
|
|
let outcome;
|
|
try {
|
|
outcome = {
|
|
ok: true,
|
|
value: await withVerifierArtifactScope(async (scope) =>
|
|
readStableArtifact(
|
|
await realpath(path),
|
|
64 * 1024 * 1024,
|
|
undefined,
|
|
undefined,
|
|
scope,
|
|
),
|
|
),
|
|
};
|
|
} catch (cause) {
|
|
outcome = { ok: false, cause };
|
|
}
|
|
if (!outcome.ok && outcome.cause instanceof ArtifactFileCleanupFailure) {
|
|
const completion = artifactFileCompletion(outcome.cause);
|
|
outcome =
|
|
completion === undefined
|
|
? { ok: false, cause: artifactFilePrimaryFacts(outcome.cause).cause }
|
|
: { ok: true, value: completion.value };
|
|
}
|
|
if (!outcome.ok) {
|
|
const cause = outcome.cause;
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "limit")
|
|
throw new Error(`Runtime Evidence exceeds 64 MiB: ${path}`, { cause });
|
|
throw cause;
|
|
}
|
|
const value = JSON.parse(outcome.value.bytes.toString("utf8"));
|
|
return Array.isArray(value) ? value : [value];
|
|
}
|
|
|
|
function parseMapping(encoded, layerCount) {
|
|
const parsed = JSON.parse(encoded);
|
|
if (
|
|
typeof parsed !== "object" ||
|
|
parsed === null ||
|
|
!Number.isInteger(parsed.layer) ||
|
|
parsed.layer < 0 ||
|
|
parsed.layer >= layerCount ||
|
|
(parsed.kind !== "file-root" && parsed.kind !== "url-prefix")
|
|
)
|
|
throw new Error(
|
|
"--mapping must be JSON with a valid layer index and file-root or url-prefix kind",
|
|
);
|
|
const { layer, ...value } = parsed;
|
|
return { layer, value };
|
|
}
|
|
|
|
function counted(values) {
|
|
return Object.fromEntries(
|
|
[...new Set(values)]
|
|
.sort()
|
|
.map((value) => [
|
|
value,
|
|
values.filter((candidate) => candidate === value).length,
|
|
]),
|
|
);
|
|
}
|