Files
reverse-engineering-anything/src/application/javascript/JavaScriptRuntimeReconciliationService.test.ts
T
morluto 035d876d1f fix(lifecycle): supervise assetutil, keep cleanup-time interruptions, and cover browser-transport handshakes (#11502)
* test(lifecycle): cover browser-transport handshakes and cleanup retry

Close six provider lifecycle gaps: Electron CDP handshake, cleanup and retry; V8 inspector handshake; web-module-trace handshake and retry.

* test(lifecycle): assert retained cleanup finishes before the next acquisition

Hold the retried browser and Electron transport close and check that no replacement launch or connect starts until it completes. Track gated upgrade sockets in the fake CDP browser so close() destroys them.

* test(lifecycle): release held retry gates and join gated work on failure

Teardown for the resolver and Electron retry tests releases the held close and awaits the gated request even when an assertion fails while it is held.

* test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion (#11512)

* test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion

Close six provider lifecycle gaps: SQLite and JavaScript worker launch, JavaScript worker handshake, assetutil launch, and qlog and mitmproxy decoding through the owned decoder child.

* test(lifecycle): run the worker relaunch in a built project and check exits before close

The relaunch starts the built worker entry, so it moves to tests/boundary/javascript. The handshake tests now check that the stand-in child has exited when analyze() returns, before close() runs.

* test(lifecycle): cover in-process cancellation and PE certificate completion (#11524)

* test(lifecycle): cover in-process cancellation and PE certificate completion

Close six provider lifecycle gaps: caller cancellation for the managed, Harmony, JavaScript workflow, runtime reconciliation and V8 code cache providers, and PE certificate completion (an existing CLI/MCP regression). Two Apple application gaps remain (#11509).

* test(lifecycle): compare the whole retained V8 code cache observation

* test(javascript): hold the startup deadline clock until the stand-in worker is running

* test(javascript): restore real timers in a void teardown

* test(lifecycle): join the held Electron request on failure, refuse unmodeled Inspector commands, drop a wording assertion

* fix(apple): run assetutil under owned process supervision and retain unproven cleanup (#11586)

* fix(process): report a cancellation or deadline during refused final cleanup as that interruption

* fix(apple): prepare ownership before the assetutil deadline, keep per-stream allowances, join interrupted-cleanup tests

* fix(artifacts): classify assetutil launch failures through the shared launch owner

After #11615, owned-command launch failures are one launch-failed outcome
classified by the shared owner. The assetutil dialect keeps only its
unavailable report (launchUnavailable) and names its provider; host
capacity and E2BIG become resource constraints.

* fix(artifacts): keep a completed assetutil run's output on its cleanup-only report

When assetutil exits 0 but its process cleanup is unproven, the cleanup
error is the primary report. retainOwnedCommandCleanup now accepts the
completed run's captured output for such a caller, so CLI and MCP keep the
retained output beside the cleanup owner.
2026-11-15 20:58:47 -05:00

250 lines
8.1 KiB
TypeScript

import { unknownTarget } from "../../domain/targetIdentity.js";
import { expect, it } from "vitest";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import {
createEvidence,
parseEvidence,
type Evidence,
evidenceSubjectInput,
} from "../../domain/evidence.js";
import { JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE } from "../../contracts/javascript/javascriptRuntimeReconciliationExample.js";
import { javascriptRuntimeReconciliationResultSchema } from "../../domain/javascript/javascriptRuntimeReconciliationSchemas.js";
import { electronPageInspectionSchema } from "../../domain/javascript/electronObservation.js";
import {
electronRuntimeEvidence,
SOURCE,
} from "../../../tests/fixtures/javascriptRuntimeReconciliation.js";
import { reconcileJavaScriptRuntimeEvidence } from "./JavaScriptRuntimeReconciliationService.js";
const reconcile = (observations: readonly Evidence[]) => {
const result = reconcileJavaScriptRuntimeEvidence({
static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers,
runtime_observations: observations,
});
if (!result.ok) throw result.error;
const evidence = parseEvidence(result.value.evidence);
return {
evidence,
sources: result.value.sources,
result: javascriptRuntimeReconciliationResultSchema.parse(
evidence.normalized_result,
),
};
};
it("stops resolving retained reconciliation sources when the caller cancels mid-request", () => {
const sources = [
JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers[0]!.analysis,
electronRuntimeEvidence("/synthetic", SOURCE),
];
const controller = new AbortController();
const reason = { request: "cancel runtime reconciliation" };
const looked: string[] = [];
const result = reconcileJavaScriptRuntimeEvidence(
{
static_layers: [
{
analysis: {
kind: "retained-evidence",
evidence_id: sources[0]!.evidence_id,
},
},
],
runtime_observations: [
{ kind: "retained-evidence", evidence_id: sources[1]!.evidence_id },
],
},
(id) => {
looked.push(id);
controller.abort(reason);
return sources.find(({ evidence_id }) => evidence_id === id);
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected cancellation");
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(reason);
expect(looked).toHaveLength(1);
});
it("keeps equal runtime payloads at distinct selected endpoints separate through service graph Evidence", () => {
const first = electronRuntimeEvidence("/synthetic", SOURCE);
const second = createEvidence(
evidenceSubjectInput(first.subject),
first.provider,
{
predicateType: first.predicate_type,
operation: first.operation,
parameters: {
...first.parameters,
cdp_endpoint: "http://127.0.0.1:9444",
},
confidence: first.confidence,
authority: first.authority,
result: first.normalized_result,
},
);
const observed = reconcile([first, second]);
expect(observed.result.graph.coverage).toMatchObject({
status: "complete",
truncated: false,
omitted_count: 0,
});
const runtime = observed.result.graph.nodes.filter(
({ identity }) => identity.strategy === "runtime-instance",
);
expect(runtime).toHaveLength(8);
expect(new Set(runtime.map(({ node_id }) => node_id)).size).toBe(8);
expect(
new Set(
runtime.map(({ identity }) =>
identity.strategy === "runtime-instance" ? identity.capture_sha256 : "",
),
).size,
).toBe(1);
for (const node of runtime) {
if (node.identity.strategy !== "runtime-instance")
throw new Error("Expected runtime instance");
expect([first.evidence_id, second.evidence_id]).toContain(
node.identity.capture_evidence_id,
);
for (const observation of node.observations)
expect(observation.evidence.evidence_ids).toContain(
node.identity.capture_evidence_id,
);
}
expect(observed.evidence.parameters.runtime_evidence_ids).toEqual([
first.evidence_id,
second.evidence_id,
]);
expect(observed.sources).toContainEqual(first);
expect(observed.sources).toContainEqual(second);
});
it("preserves attach-limited frame and worker coverage through service graph Evidence", () => {
const first = electronRuntimeEvidence("/synthetic", SOURCE);
const inspection = electronPageInspectionSchema.parse(
first.normalized_result,
);
const capture = createEvidence(
evidenceSubjectInput(first.subject),
first.provider,
{
predicateType: first.predicate_type,
operation: first.operation,
parameters: first.parameters,
confidence: first.confidence,
authority: first.authority,
result: {
...inspection,
completeness: {
...inspection.completeness,
status: "attach_limited",
conditions: ["attach_limited"],
attach_limited_sections: ["frames", "workers"],
},
},
},
);
const { result } = reconcile([capture]);
const runtime = result.graph.nodes.filter(
({ identity }) => identity.strategy === "runtime-instance",
);
expect(runtime).toHaveLength(4);
for (const node of runtime) {
const partial = node.kind === "frame" || node.kind === "worker";
for (const observation of node.observations)
expect(observation.evidence.coverage).toMatchObject({
status: partial ? "partial" : "complete",
truncated: false,
omitted_count: partial ? null : 0,
});
for (const edge of result.graph.edges.filter(
({ target_node_id }) => target_node_id === node.node_id,
))
if (edge.evidence.authority === "passive-cdp-runtime")
expect(edge.evidence.coverage).toMatchObject({
status: partial ? "partial" : "complete",
truncated: false,
omitted_count: partial ? null : 0,
});
}
expect(result.graph.coverage).toMatchObject({
status: "partial",
truncated: false,
omitted_count: null,
});
});
it("preserves unresolved Inspector target location reason through service Evidence", () => {
const evidence = createEvidence(
unknownTarget("not-observed"),
{
id: "rea-v8-inspector",
name: "REA passive Node/Electron V8 Inspector provider",
version: "1",
},
{
predicateType: "rea.javascript-runtime-observation",
operation: "observe_javascript_runtime",
parameters: {
inspector_endpoint: "http://127.0.0.1:9229",
target_id: "node-main",
observation_ms: 0,
},
authority: "external-service",
confidence: "observed",
result: {
runtime: {
product: "node/fixture",
protocol_version: "1.1",
v8_version: null,
},
target: {
target_id: "node-main",
protocol_type: "node",
attached: null,
location: {
kind: "unresolved",
reported_url: "file:///operator-selected.js",
reason: "location-authorization-not-attempted",
},
runtime_kind: "unknown",
runtime_kind_authority: "not-declared",
},
capture: {
observation_ms: 0,
events_observed: 0,
events_retained: 0,
events_dropped: 0,
metadata_bytes_retained: 0,
truncated: false,
truncation_reasons: [],
},
scripts: {
items: [],
observed_total: 0,
parse_failed_total: 0,
excluded: { unsupported_location: 0, invalid_protocol_value: 0 },
},
execution_contexts: [],
directly_observed: [],
unavailable_without_instrumentation: [],
unknowns: [],
limitations: [],
},
},
);
const projected = reconcile([evidence]);
expect(
projected.result.graph.nodes.find(
({ kind, identity }) =>
kind === "target" && identity.strategy === "runtime-instance",
)?.observations[0]?.properties,
).toMatchObject({
reported_url: "file:///operator-selected.js",
location_kind: "unresolved",
location_reason: "location-authorization-not-attempted",
});
});