test(runtime): consolidate Inspector and reconciliation coverage

This commit is contained in:
morluto committed 2026-11-17 23:02:01 -05:00
1 parent 025df6ec30
commit 69e263b0b1
8 files changed
+1269 -1250

No files matched your search

@@ -1,6 +1,7 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { javascriptRuntimeReconciliationResultSchema } from "../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js";
import { nodeReconciliationFixture } from "../../fixtures/nodeRuntimeReconciliation.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -28,13 +29,39 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
expect(response.isError, JSON.stringify(response)).not.toBe(true);
document = response.structuredContent;
}
const evidence = parseEvidence(document);
expect(evidence.operation).toBe("reconcile_javascript_runtime");
const result = javascriptRuntimeReconciliationResultSchema.parse(
(document as { normalized_result: unknown }).normalized_result,
evidence.normalized_result,
);
const appEvidenceId = input.static_layers[0]?.analysis.evidence_id;
const runtimeEvidenceId = input.runtime_observations[0]?.evidence_id;
const asset = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.some(
({ properties }) => properties.path === "renderer.js",
),
);
const [capture] = result.runtime_captures;
if (asset === undefined || capture === undefined)
throw new Error("Missing renderer asset or runtime capture");
expect(result.evidence_links).toEqual(
[appEvidenceId, runtimeEvidenceId].sort(),
);
expect(capture).toMatchObject({
evidence_id: runtimeEvidenceId,
kind: "v8-inspector",
scripts: 0,
});
expect(result.reconciliations).toEqual([
expect.objectContaining({
entity_kind: "target",
status: "matched",
basis: "artifact-path",
static_node_id: asset.node_id,
runtime_node_id: capture.target_node_id,
runtime_evidence_id: runtimeEvidenceId,
reason: "unique-location-match",
confidence: "medium",
candidate_static_count: 1,
@@ -47,6 +74,24 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
});
expect(
result.graph.edges.filter(({ relation }) => relation === "observed_as"),
).toHaveLength(1);
).toMatchObject([
{
source_node_id: asset.node_id,
target_node_id: capture.target_node_id,
evidence: {
state: "inferred",
evidence_ids: [appEvidenceId, runtimeEvidenceId].sort(),
},
},
]);
expect(
result.static_load_states.find(
({ static_node_id }) => static_node_id === asset.node_id,
),
).toMatchObject({
status: "unknown",
runtime_node_ids: [],
reason: "static-or-runtime-coverage-incomplete",
});
},
);
@@ -93,9 +93,10 @@ const wideRequest = (count: number) => {
};
};
processTest.for([1_000, 16_000])(
"retains every static load state for %s added assets through the compiled CLI",
async (count, { processes }) => {
processTest(
"retains every static load state for a wide graph through the compiled CLI",
async ({ processes }) => {
const count = 16_000;
const root = await createTestTempDirectory("rea-runtime-wide-states-");
const path = join(root, "request.json");
const { input, assetIds, analysisId } = wideRequest(count);
@@ -30,82 +30,80 @@ const readInput = (
return result.value;
};
const collisionInput = async (
alternatives: number,
explicitMapping: boolean,
) => {
it("retains the mapped disagreement despite equal bytes at other locations", async () => {
const root = await applicationFixture();
if (alternatives > 0) await writeFile(join(root, "other.js"), CAPTURED);
const live = explicitMapping
? await createTestTempDirectory("rea-runtime-content-live-")
: root;
const live = await createTestTempDirectory("rea-runtime-content-live-");
const extra = await createTestTempDirectory("rea-runtime-content-extra-");
await writeFile(join(extra, "app.js"), CAPTURED);
return readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(root),
runtime_mappings: explicitMapping
? [{ kind: "file-root", root: live, artifact_prefix: "" }]
: [],
},
...(alternatives > 1
? [{ role: "assets", analysis: await analyzeFixture(extra) }]
: []),
],
runtime_observations: [
electronRuntimeEvidence(live, CAPTURED, { includeWorker: false }),
],
await Promise.all([
writeFile(join(root, "other.js"), CAPTURED),
writeFile(join(extra, "app.js"), CAPTURED),
]);
const [application, assets] = await Promise.all([
analyzeFixture(root),
analyzeFixture(extra),
]);
const captures = [root, live].map((path, index) =>
electronRuntimeEvidence(path, CAPTURED, {
includeWorker: false,
targetId: `mismatch-${index}`,
}),
);
};
it.each([
[0, false],
[1, false],
[2, false],
[0, true],
[1, true],
[2, true],
] as const)(
"preserves mapped disagreement with %i other content matches (explicit mapping: %s)",
async (alternatives, explicitMapping) => {
const parsed = await collisionInput(alternatives, explicitMapping);
const result = reconcileJavaScriptRuntime(parsed);
const result = reconcileJavaScriptRuntime(
readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: application,
runtime_mappings: [
{ kind: "file-root", root: live, artifact_prefix: "" },
],
},
{ role: "assets", analysis: assets },
],
runtime_observations: captures,
}),
),
);
const layer = result.static_layers.find(({ role }) => role === "application");
const node = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.length === 1 &&
observations.some(({ properties }) => properties.path === "app.js"),
);
if (layer === undefined || node === undefined)
throw new Error("Missing fixture application asset");
for (const [index, basis] of [
"artifact-path",
"operator-file-mapping",
].entries()) {
const script = result.reconciliations.find(
({ entity_kind }) => entity_kind === "script",
({ entity_kind, runtime_evidence_id }) =>
entity_kind === "script" &&
runtime_evidence_id === captures[index]?.evidence_id,
);
expect(script).toMatchObject({
status: "unmatched",
basis: explicitMapping ? "operator-file-mapping" : "artifact-path",
basis,
confidence: "high",
reason: "captured-content-disagrees-with-static-location",
static_layer_id: null,
static_node_id: null,
candidate_static_count: 1,
candidate_static_nodes: [
{ static_layer_id: layer.layer_id, static_node_id: node.node_id },
],
});
const candidate = script?.candidate_static_nodes[0];
const selected = result.graph.nodes.find(
({ node_id }) => node_id === candidate?.static_node_id,
);
expect(selected?.observations).toEqual(
expect.arrayContaining([
expect.objectContaining({
properties: expect.objectContaining({ path: "app.js" }),
}),
]),
);
expect(
result.graph.edges.some(
(edge) =>
edge.relation === "observed_as" &&
edge.target_node_id === script?.runtime_node_id,
({ relation, target_node_id }) =>
relation === "observed_as" &&
target_node_id === script?.runtime_node_id,
),
).toBe(false);
},
);
}
});
it("restricts content-and-location ambiguity to mapped candidates", async () => {
const application = await applicationFixture();
@@ -139,46 +137,70 @@ it("restricts content-and-location ambiguity to mapped candidates", async () =>
reason: "ambiguous-static-candidates",
candidate_static_count: 2,
});
const unmapped = result.static_layers.find(({ role }) => role === "assets");
expect(
script?.candidate_static_nodes.map(
({ static_layer_id }) => static_layer_id,
),
).not.toContain(unmapped?.layer_id);
const app = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.some(({ properties }) => properties.path === "app.js"),
);
if (app === undefined)
throw new Error("Missing fixture shared application asset");
expect(script?.candidate_static_nodes).toEqual(
result.static_layers
.filter(({ role }) => role === "application" || role === "cache")
.map(({ layer_id }) => ({
static_layer_id: layer_id,
static_node_id: app.node_id,
})),
);
});
it.each([false, true])(
"retains digest-only matching without a mapped candidate (mapping: %s)",
async (mapped) => {
const root = await applicationFixture();
await writeFile(join(root, "other.js"), CAPTURED);
const live = await createTestTempDirectory(
"rea-runtime-content-unlocated-",
);
const result = reconcileJavaScriptRuntime(
readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(root),
runtime_mappings: mapped
? [{ kind: "file-root", root: live, artifact_prefix: "" }]
: [],
},
],
runtime_observations: [
electronRuntimeEvidence(live, CAPTURED, {
scriptFile: "absent.js",
includeWorker: false,
}),
],
}),
),
);
it("matches exact bytes when either a mapped or unmapped location has no candidate", async () => {
const root = await applicationFixture();
await writeFile(join(root, "other.js"), CAPTURED);
const mapped = await createTestTempDirectory(
"rea-runtime-content-unlocated-",
);
const unmapped = await createTestTempDirectory(
"rea-runtime-content-unmapped-",
);
const analysis = await analyzeFixture(root);
const captures = [mapped, unmapped].map((path, index) =>
electronRuntimeEvidence(path, CAPTURED, {
scriptFile: "absent.js",
includeWorker: false,
targetId: `digest-only-${index}`,
}),
);
const result = reconcileJavaScriptRuntime(
readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis,
runtime_mappings: [
{ kind: "file-root", root: mapped, artifact_prefix: "" },
],
},
],
runtime_observations: captures,
}),
),
);
const expected = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.some(({ properties }) => properties.path === "other.js"),
);
if (expected === undefined) throw new Error("Missing fixture digest match");
const scripts = result.reconciliations.filter(
({ entity_kind }) => entity_kind === "script",
);
expect(scripts).toHaveLength(2);
for (const { evidence_id } of captures)
expect(
result.reconciliations.find(
({ entity_kind }) => entity_kind === "script",
scripts.find(
({ runtime_evidence_id }) => runtime_evidence_id === evidence_id,
),
).toMatchObject({
status: "matched",
@@ -186,12 +208,30 @@ it.each([false, true])(
confidence: "high",
reason: "unique-content-match",
candidate_static_count: 1,
static_node_id: expected.node_id,
});
},
);
});
it("keeps unavailable mapped digests unknown instead of borrowing unrelated matches", async () => {
const parsed = await collisionInput(1, true);
const root = await applicationFixture();
await writeFile(join(root, "other.js"), CAPTURED);
const live = await createTestTempDirectory("rea-runtime-content-unknown-");
const parsed = readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(root),
runtime_mappings: [
{ kind: "file-root", root: live, artifact_prefix: "" },
],
},
],
runtime_observations: [
electronRuntimeEvidence(live, CAPTURED, { includeWorker: false }),
],
}),
);
const candidates = collectStaticRuntimeCandidates(parsed.layers).map(
(candidate) =>
candidate.paths.includes("app.js") && candidate.category === "javascript"
@@ -241,201 +281,147 @@ it("keeps unavailable mapped digests unknown instead of borrowing unrelated matc
const UNRELATED = "export const unrelated = 17;\n";
type LayerSetup = {
readonly root: string;
readonly mapping: "none" | "explicit";
};
/** Reconcile one captured script, located at absent.js under live, against layers. */
const reconcileCapturedScript = async (input: {
readonly layers: readonly LayerSetup[];
readonly live: string;
readonly source: string;
readonly includeWorker?: boolean;
}) => {
it("reports absence only where a layer authorizes the capture location", async () => {
const [application, cache, mappedApplication, mappedCache, unmapped] =
await Promise.all([
applicationFixture(),
applicationFixture(),
createTestTempDirectory("rea-runtime-application-mapped-"),
createTestTempDirectory("rea-runtime-cache-mapped-"),
createTestTempDirectory("rea-runtime-unmapped-"),
]);
const [applicationEvidence, cacheEvidence] = await Promise.all([
analyzeFixture(application),
analyzeFixture(cache),
]);
const captures = [application, mappedApplication, mappedCache, unmapped].map(
(path, index) =>
electronRuntimeEvidence(path, UNRELATED, {
scriptFile: "absent.js",
includeWorker: index === 3,
targetId: `no-match-${index}`,
}),
);
const result = reconcileJavaScriptRuntime(
readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: await Promise.all(
input.layers.map(async ({ root, mapping }, index) => ({
role: (["application", "cache", "assets"] as const)[index],
analysis: await analyzeFixture(root),
runtime_mappings:
mapping === "explicit"
? [{ kind: "file-root", root: input.live, artifact_prefix: "" }]
: [],
})),
),
runtime_observations: [
electronRuntimeEvidence(input.live, input.source, {
scriptFile: "absent.js",
includeWorker: input.includeWorker ?? false,
}),
],
}),
),
);
const item = (kind: string) =>
result.reconciliations.find(({ entity_kind }) => entity_kind === kind);
return { result, script: item("script"), worker: item("worker") };
};
it("keeps an unmapped captured script without a content match unknown", async () => {
const root = await applicationFixture();
const live = await createTestTempDirectory("rea-runtime-unmapped-");
const { result, script, worker } = await reconcileCapturedScript({
layers: [{ root, mapping: "none" }],
live,
source: UNRELATED,
includeWorker: true,
});
const unknown = {
status: "unknown",
basis: "none",
confidence: "unknown",
reason: "no-authorized-location-mapping",
static_layer_id: null,
static_node_id: null,
candidate_static_count: 0,
};
expect(script).toMatchObject(unknown);
// A source-free worker at an unmapped location shares the same rule.
expect(worker).toMatchObject(unknown);
expect(result.summary).toMatchObject({ matched: 0, unmatched: 0 });
expect(
result.graph.edges.some(({ relation }) => relation === "observed_as"),
).toBe(false);
});
it.each(["explicit", "implicit-root"] as const)(
"reports a mapped captured script without a content match as unmatched (%s)",
async (mapping) => {
const root = await applicationFixture();
const live =
mapping === "explicit"
? await createTestTempDirectory("rea-runtime-mapped-absent-")
: root;
const { result, script } = await reconcileCapturedScript({
layers: [{ root, mapping: mapping === "explicit" ? "explicit" : "none" }],
live,
source: UNRELATED,
});
expect(script).toMatchObject({
status: "unmatched",
basis: "none",
confidence: "high",
reason: "no-static-content-match",
candidate_static_count: 0,
});
expect(result.summary.unmatched).toBe(1);
},
);
it.each([
["only the second", ["none", "explicit"], "unmatched"],
["only the first", ["explicit", "none"], "unmatched"],
["neither", ["none", "none"], "unknown"],
] as const)(
"classifies an unmatched captured script when %s of two layers maps it",
async (_label, mappings, status) => {
const live = await createTestTempDirectory("rea-runtime-layer-mapping-");
const { script } = await reconcileCapturedScript({
layers: await Promise.all(
mappings.map(async (mapping) => ({
root: await applicationFixture(),
mapping,
})),
),
live,
source: UNRELATED,
});
expect(script).toMatchObject({
status,
reason:
status === "unknown"
? "no-authorized-location-mapping"
: "no-static-content-match",
});
},
);
it.each([
[1, "matched", "unique-content-match"],
[2, "ambiguous", "ambiguous-static-candidates"],
] as const)(
"keeps unmapped content-only correspondence with %i digest candidates",
async (copies, status, reason) => {
const live = await createTestTempDirectory("rea-runtime-unmapped-content-");
const { result, script } = await reconcileCapturedScript({
layers: await Promise.all(
Array.from({ length: copies }, async () => {
const root = await applicationFixture();
await writeFile(join(root, "other.js"), CAPTURED);
return { root, mapping: "none" as const };
}),
),
live,
source: CAPTURED,
});
expect(script).toMatchObject({
status,
basis: copies === 1 ? "content-sha256" : "none",
reason,
candidate_static_count: copies,
});
expect(result.summary.unmatched).toBe(0);
},
);
it.each([
["https://example.test/assets/absent.js", "unmatched"],
["https://other.test/assets/absent.js", "unknown"],
] as const)(
"applies URL-prefix mapping presence to captured script %s",
async (url, status) => {
const root = await applicationFixture();
const parsed = readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(root),
analysis: applicationEvidence,
runtime_mappings: [
{
kind: "url-prefix",
prefix: "https://example.test/assets/",
kind: "file-root",
root: mappedApplication,
artifact_prefix: "",
},
],
},
{
role: "cache",
analysis: cacheEvidence,
runtime_mappings: [
{ kind: "file-root", root: mappedCache, artifact_prefix: "" },
],
},
],
runtime_observations: [
electronRuntimeEvidence(root, UNRELATED, { includeWorker: false }),
],
runtime_observations: captures,
}),
),
);
const unmatched = {
status: "unmatched",
basis: "none",
confidence: "high",
reason: "no-static-content-match",
static_layer_id: null,
static_node_id: null,
candidate_static_count: 0,
};
const unknown = {
...unmatched,
status: "unknown",
confidence: "unknown",
reason: "no-authorized-location-mapping",
};
const code = result.reconciliations.filter(
({ entity_kind }) => entity_kind === "script" || entity_kind === "worker",
);
expect(code).toHaveLength(5);
for (const [index, { evidence_id }] of captures.entries()) {
const rows = code.filter(
({ runtime_evidence_id }) => runtime_evidence_id === evidence_id,
);
// Model a URL-located capture at the matcher boundary; browser and
// Inspector projections supply URL locations to the same classifier.
const entities = projectRuntimeCaptures(parsed.captures).entities.map(
(entity) =>
entity.kind === "script"
? { ...entity, location: { kind: "url" as const, value: url } }
: entity,
);
expect(
rows.find(({ entity_kind }) => entity_kind === "script"),
).toMatchObject(index < 3 ? unmatched : unknown);
if (index === 3)
expect(
rows.find(({ entity_kind }) => entity_kind === "worker"),
).toMatchObject(unknown);
}
const runtimeIds = new Set(
code.map(({ runtime_node_id }) => runtime_node_id),
);
expect(
result.graph.edges.some(
({ relation, target_node_id }) =>
relation === "observed_as" && runtimeIds.has(target_node_id),
),
).toBe(false);
});
it("applies URL-prefix presence to captured scripts independently of origin lookalikes", async () => {
const root = await applicationFixture();
const parsed = readInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(root),
runtime_mappings: [
{
kind: "url-prefix",
prefix: "https://example.test/assets/",
artifact_prefix: "",
},
],
},
],
runtime_observations: [
electronRuntimeEvidence(root, UNRELATED, { includeWorker: false }),
],
}),
);
const { entities } = projectRuntimeCaptures(parsed.captures);
const candidates = collectStaticRuntimeCandidates(parsed.layers);
// Browser and Inspector projections supply URLs to this same matcher.
for (const [url, status, confidence, reason] of [
[
"https://example.test/assets/absent.js",
"unmatched",
"high",
"no-static-content-match",
],
[
"https://other.test/assets/absent.js",
"unknown",
"unknown",
"no-authorized-location-mapping",
],
] as const) {
const result = reconcileRuntimeEntities({
entities,
candidates: collectStaticRuntimeCandidates(parsed.layers),
entities: entities.map((entity) =>
entity.kind === "script"
? { ...entity, location: { kind: "url", value: url } }
: entity,
),
candidates,
layers: parsed.layers,
});
expect(
result.items.find(({ entity_kind }) => entity_kind === "script"),
).toMatchObject({
status,
confidence: status === "unknown" ? "unknown" : "high",
reason:
status === "unknown"
? "no-authorized-location-mapping"
: "no-static-content-match",
});
},
);
url,
).toMatchObject({ status, confidence, reason, candidate_static_count: 0 });
}
});
@@ -3,35 +3,47 @@ import { reconcileJavaScriptRuntimeEvidence } from "../../../src/application/jav
import { javascriptRuntimeReconciliationResultSchema } from "../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js";
import { nodeReconciliationFixture } from "../../fixtures/nodeRuntimeReconciliation.js";
test.each([false, true])(
"matches a synthetic Node entry target without inventing script presence: %s",
(scriptObserved) => {
const reconciled = reconcileJavaScriptRuntimeEvidence(
nodeReconciliationFixture(scriptObserved),
);
if (!reconciled.ok) throw reconciled.error;
const result = javascriptRuntimeReconciliationResultSchema.parse(
reconciled.value.evidence.normalized_result,
);
const target = result.reconciliations.find(
({ entity_kind }) => entity_kind === "target",
);
expect(target).toMatchObject({
status: "matched",
basis: "artifact-path",
confidence: "medium",
reason: "unique-location-match",
candidate_static_count: 1,
});
expect(
result.graph.nodes.find(
({ node_id }) => node_id === target?.static_node_id,
)?.kind,
).toBe("javascript-asset");
expect(result.summary).toMatchObject({
runtime_scripts: scriptObserved ? 1 : 0,
static_loaded: scriptObserved ? 1 : 0,
static_not_observed: 0,
});
},
);
test("attributes loaded script state to the reported Node script", () => {
const reconciled = reconcileJavaScriptRuntimeEvidence(
nodeReconciliationFixture(true),
);
if (!reconciled.ok) throw reconciled.error;
const result = javascriptRuntimeReconciliationResultSchema.parse(
reconciled.value.evidence.normalized_result,
);
const asset = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.some(({ properties }) => properties.path === "renderer.js"),
);
if (asset === undefined) throw new Error("Missing fixture renderer asset");
const target = result.reconciliations.find(
({ entity_kind }) => entity_kind === "target",
);
const script = result.reconciliations.find(
({ entity_kind }) => entity_kind === "script",
);
expect(target).toMatchObject({
status: "matched",
static_node_id: asset.node_id,
});
expect(script).toMatchObject({
status: "matched",
static_node_id: asset.node_id,
});
expect(script?.runtime_node_id).not.toBe(target?.runtime_node_id);
expect(
result.static_load_states.find(
({ static_node_id }) => static_node_id === asset.node_id,
),
).toMatchObject({
status: "loaded",
reason: "runtime-script-correspondence",
runtime_node_ids: [script?.runtime_node_id],
});
expect(result.summary).toMatchObject({
runtime_scripts: 1,
static_loaded: 1,
static_not_observed: 0,
});
});
@@ -7,10 +7,7 @@ import { describe, expect, test } from "vitest";
import { createJavaScriptRuntimeObservationEvidence } from "../../../src/application/javascript/JavaScriptRuntimeObservationEvidence.js";
import { createBrowserEvidence } from "../../../src/application/BrowserEvidence.js";
import {
listJavaScriptRuntimeTargets,
observeJavaScriptRuntime,
} from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js";
import { observeJavaScriptRuntime } from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js";
import { reconcileJavaScriptRuntimeEvidence } from "../../../src/application/javascript/JavaScriptRuntimeReconciliationService.js";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { V8_INSPECTOR_PROVIDER_IDENTITY } from "../../../src/inspector/providerIdentity.js";
@@ -240,83 +237,71 @@ describe("Inspector execution-context lifecycle metadata", () => {
});
});
describe("Inspector execution-context ID validation", () => {
test.each([
["malformed", "2", "1"],
["valid", 2, 1],
] as const)(
"accounts for %s context IDs in lifecycle events",
async (mode, createdId, destroyedId) => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
runtimeEvents: [
{
method: "Runtime.executionContextCreated",
params: { context: { id: 1, name: "main", origin: "" } },
},
{
method: "Runtime.executionContextCreated",
params: { context: { id: createdId, name: "worker", origin: "" } },
},
{
method: "Runtime.executionContextDestroyed",
params: { executionContextId: destroyedId },
},
],
scriptEvents: [],
});
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
if (!result.ok) throw result.error;
const invalidContextUnknowns = result.value.unknowns.filter((text) =>
text.includes("malformed execution-context ID"),
);
expect(result.value.capture).toMatchObject({
events_observed: 3,
events_retained: 3,
events_dropped: 0,
truncated: false,
});
if (mode === "malformed") {
expect(result.value.execution_contexts).toEqual([
{
context_key: "context_0",
execution_context_id: 1,
state: "created",
name: "main",
origin: "",
},
]);
expect(invalidContextUnknowns).toEqual([
"2 execution-context lifecycle events with a missing or malformed execution-context ID were excluded; the affected contexts' lifecycle state is unknown.",
]);
return;
}
expect(result.value.execution_contexts).toEqual([
{
context_key: "context_0",
execution_context_id: 1,
state: "destroyed",
name: "main",
origin: "",
},
{
context_key: "context_1",
execution_context_id: 2,
state: "created",
name: "worker",
origin: "",
},
]);
expect(invalidContextUnknowns).toEqual([]);
} finally {
await fake.close();
}
},
);
test("counts malformed lifecycle events without altering valid Inspector contexts", async () => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
runtimeEvents: [
{
method: "Runtime.executionContextCreated",
params: { context: { id: 1, name: "main", origin: "" } },
},
{
method: "Runtime.executionContextCreated",
params: { context: { id: "2", name: "bad", origin: "" } },
},
{
method: "Runtime.executionContextDestroyed",
params: { executionContextId: "1" },
},
{
method: "Runtime.executionContextCreated",
params: { context: { id: 2, name: "worker", origin: "" } },
},
{
method: "Runtime.executionContextDestroyed",
params: { executionContextId: 2 },
},
],
scriptEvents: [],
});
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
if (!result.ok) throw result.error;
expect(result.value.capture).toMatchObject({
events_observed: 5,
events_retained: 5,
events_dropped: 0,
truncated: false,
});
expect(result.value.execution_contexts).toEqual([
{
context_key: "context_0",
execution_context_id: 1,
state: "created",
name: "main",
origin: "",
},
{
context_key: "context_1",
execution_context_id: 2,
state: "destroyed",
name: "worker",
origin: "",
},
]);
expect(
result.value.unknowns.filter((text) =>
text.includes("malformed execution-context ID"),
),
).toEqual([
"2 execution-context lifecycle events with a missing or malformed execution-context ID were excluded; the affected contexts' lifecycle state is unknown.",
]);
} finally {
await fake.close();
}
});
test("rejects unknown runtime observation fields", () => {
@@ -329,10 +314,12 @@ test("rejects unknown runtime observation fields", () => {
).toBe(false);
});
test("returns every target exposed by the selected Inspector endpoint inline", async () => {
test("lists all connectable targets on the selected endpoint across directories", async () => {
const fixture = await runtimeFixture();
const outside = await temporaryFile("outside.js");
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
additionalTargetUrl: pathToFileURL(outside).href,
additionalTargetCount: 205,
});
try {
@@ -340,8 +327,22 @@ test("returns every target exposed by the selected Inspector endpoint inline", a
inspector_endpoint: fake.endpoint,
});
if (!listed.ok) throw listed.error;
expect(listed.value.targets).toHaveLength(206);
expect(listed.value.targets[0]?.target_id).toBe(fake.targetId);
const targets = listed.value.targets;
expect(targets).toHaveLength(207);
expect(new Set(targets.map(({ target_id }) => target_id)).size).toBe(207);
expect(
targets.find(({ target_id }) => target_id === fake.targetId)?.location,
).toMatchObject({ kind: "file", file_path: fixture.entry });
expect(
targets.find(
({ target_id }) => target_id === "00000000-0000-4000-8000-000000000002",
)?.location,
).toMatchObject({ kind: "file", file_path: outside });
expect(
targets.find(
({ target_id }) => target_id === "00000000-0000-4000-8000-000000000207",
)?.location,
).toMatchObject({ kind: "file", file_path: fixture.entry });
} finally {
await fake.close();
}
@@ -409,27 +410,6 @@ test("rejects a V8 target explicitly marked attached", async () => {
}
});
test("includes targets outside the old caller root filter", async () => {
const fixture = await runtimeFixture();
const outside = await temporaryFile("outside.js");
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
additionalTargetUrl: pathToFileURL(outside).href,
});
try {
const result = await new V8InspectorProvider().listTargets({
inspector_endpoint: fake.endpoint,
});
if (!result.ok) throw result.error;
expect(result.value.targets).toHaveLength(2);
expect(result.value.targets.map(({ location }) => location)).toContainEqual(
expect.objectContaining({ file_path: outside }),
);
} finally {
await fake.close();
}
});
test("captures complete metadata with two enable commands", async () => {
const fixture = await runtimeFixture();
const outside = await temporaryFile("secret.js");
@@ -499,131 +479,109 @@ test("excludes an Electron main target that reports only bare file://", async ()
});
test.each([
["node", "node"],
["electron-main", "node"],
["electron-preload", "page"],
["electron-renderer", "page"],
{
targetType: "node",
allowed: ["node", "electron-main"],
refused: ["electron-preload", "electron-renderer"],
},
{
targetType: "page",
allowed: ["electron-preload", "electron-renderer"],
refused: ["node", "electron-main"],
},
] as const)(
"admits declared %s only on its protocol target family",
async (runtimeKind, targetType) => {
"admits only compatible declared roles for a $targetType Inspector target",
async ({ targetType, allowed, refused }) => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
targetType,
});
const provider = new V8InspectorProvider();
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, runtimeKind),
);
expect(result.ok).toBe(true);
for (const kind of refused) {
const result = await provider.observe(
observeInput(fake.endpoint, fake.targetId, kind),
);
expect(result, kind).toMatchObject({
ok: false,
error: { reason: "target_not_allowed" },
});
}
expect(fake.commands).toEqual([]);
for (const kind of allowed) {
const result = await provider.observe(
observeInput(fake.endpoint, fake.targetId, kind),
);
if (!result.ok) throw result.error;
expect(result.value.target).toMatchObject({
protocol_type: targetType,
runtime_kind: kind,
runtime_kind_authority: "caller-declared-unverified",
});
}
if (targetType === "page") {
const input = observeInput(fake.endpoint, fake.targetId, "node");
delete input.runtime_kind;
const result = await provider.observe(input);
if (!result.ok) throw result.error;
expect(result.value.target).toMatchObject({
protocol_type: "page",
runtime_kind: "unknown",
runtime_kind_authority: "not-declared",
});
}
} finally {
await provider.close();
await fake.close();
}
},
);
describe("complete Inspector script hashes", () => {
test("keeps more than one hundred long script hashes distinct", async () => {
const fixture = await runtimeFixture();
const url = pathToFileURL(fixture.entry).href;
const prefix = "h".repeat(512);
const scriptHashes = Array.from(
{ length: 101 },
(_, index) => `${prefix}${String(index)}`,
test("retains 2,005 authorized scripts with complete URL and hash metadata", async () => {
const shortUrl = "https://example.test/shared.js";
const longUrl = `https://example.test/${"a".repeat(20_000)}`;
const prefix = "h".repeat(512);
const scriptHashes = Array.from({ length: 2_005 }, (_, index) =>
index < 101 ? `${prefix}${index}` : `hash-${index}`,
);
const scriptUrls = scriptHashes.map((_, index) =>
index === 2_004 ? longUrl : shortUrl,
);
const fake = await startFakeV8Inspector({
targetUrl: longUrl,
scriptUrls,
scriptHashes,
});
try {
const result = await new V8InspectorProvider().observe({
...observeInput(fake.endpoint, fake.targetId, "node"),
observation_ms: 1_000,
});
if (!result.ok) throw result.error;
expect(result.value.capture).toMatchObject({
truncated: false,
truncation_reasons: [],
events_dropped: 0,
});
expect(result.value.target.location).toMatchObject({
kind: "url",
sanitized_url: longUrl,
});
const items = result.value.scripts.items;
expect(items.map(({ cdp_hash }) => cdp_hash).sort()).toEqual(
[...scriptHashes].sort(),
);
const fake = await startFakeV8Inspector({
targetUrl: url,
scriptUrls: scriptHashes.map(() => url),
scriptHashes,
expect(
items.find(({ cdp_hash }) => cdp_hash === "hash-2004")?.location,
).toMatchObject({
kind: "url",
origin: "https://example.test",
sanitized_url: longUrl,
});
try {
const result = await new V8InspectorProvider().observe({
...observeInput(fake.endpoint, fake.targetId, "node"),
observation_ms: 1_000,
});
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(scriptHashes.length);
expect(
new Set(result.value.scripts.items.map(({ cdp_hash }) => cdp_hash)),
).toEqual(new Set(scriptHashes));
} finally {
await fake.close();
}
});
});
describe("undeclared V8 runtime role", () => {
test("observes a target without inventing a runtime role", async () => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
targetType: "page",
});
try {
const input = observeInput(fake.endpoint, fake.targetId, "node");
delete input.runtime_kind;
const result = await new V8InspectorProvider().observe(input);
if (!result.ok) throw result.error;
expect(result.value.target).toMatchObject({
protocol_type: "page",
runtime_kind: "unknown",
runtime_kind_authority: "not-declared",
});
} finally {
await fake.close();
}
});
});
describe("V8 Inspector script retention", () => {
test("retains an authorized script location of more than 20,000 characters", async () => {
const longUrl = `https://example.test/${"a".repeat(20_000)}`;
const fake = await startFakeV8Inspector({
targetUrl: longUrl,
scriptUrls: [longUrl],
});
try {
const input = {
...observeInput(fake.endpoint, fake.targetId, "node"),
};
const result = await new V8InspectorProvider().observe(input);
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(1);
expect(result.value.scripts.items[0]?.location).toMatchObject({
kind: "url",
origin: "https://example.test",
sanitized_url: longUrl,
});
} finally {
await fake.close();
}
});
test("returns every authorized script of a target with 2,005 scripts", async () => {
const fixture = await runtimeFixture();
const scriptUrls = [
pathToFileURL(fixture.entry).href,
...Array.from({ length: 2_004 }, () => pathToFileURL(fixture.entry).href),
];
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
scriptUrls,
});
try {
const input = {
...observeInput(fake.endpoint, fake.targetId, "node"),
observation_ms: 1_000,
};
const result = await new V8InspectorProvider().observe(input);
if (!result.ok) throw result.error;
expect(result.value.capture.truncated).toBe(false);
expect(result.value.capture.truncation_reasons).toEqual([]);
expect(result.value.capture.events_dropped).toBe(0);
expect(result.value.scripts.items).toHaveLength(scriptUrls.length);
} finally {
await fake.close();
}
});
} finally {
await fake.close();
}
});
describe("bounded V8 Inspector capture", () => {
@@ -657,7 +615,15 @@ describe("bounded V8 Inspector capture", () => {
expect(result.value.capture.events_observed).toBeGreaterThan(
result.value.capture.events_retained,
);
expect(result.value.execution_contexts).toHaveLength(1);
expect(result.value.execution_contexts).toMatchObject([
{ execution_context_id: 1, state: "destroyed" },
]);
const retainedHashes = result.value.scripts.items.map(
({ cdp_hash }) => cdp_hash,
);
expect(retainedHashes).toContain(scriptHashes[0]);
expect(retainedHashes).not.toContain(scriptHashes.at(-1));
expect(result.value.scripts.observed_total).toBe(scriptHashes.length);
} finally {
await fake.close();
}
@@ -702,53 +668,14 @@ describe("passive V8 Inspector evidence", () => {
new V8InspectorProvider(),
input,
);
expect(first.ok).toBe(true);
expect(second.ok).toBe(true);
if (!first.ok || !second.ok) return;
if (!first.ok) throw first.error;
if (!second.ok) throw second.error;
expect(second.value.evidence_id).toBe(first.value.evidence_id);
const listed = await listJavaScriptRuntimeTargets(
new V8InspectorProvider(),
{
inspector_endpoint: fake.endpoint,
},
);
expect(listed.ok).toBe(true);
} finally {
await fake.close();
}
});
test("reconciles V8 script presence with static Application Graph Evidence", () => {
const root = "/Applications/Example.app/Contents/Resources/app";
const observation = runtimeObservation(
`${root}/index.html`,
`${root}/renderer.js`,
);
const runtimeEvidence = createJavaScriptRuntimeObservationEvidence(
"observe_javascript_runtime",
{
inspector_endpoint: "http://127.0.0.1:9229",
target_id: "example-v8-target",
runtime_kind: "electron-main",
observation_ms: 100,
},
observation,
V8_INSPECTOR_PROVIDER_IDENTITY,
);
const reconciled = reconcileJavaScriptRuntimeEvidence({
static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers,
runtime_observations: [runtimeEvidence],
});
if (!reconciled.ok) throw reconciled.error;
const result = javascriptRuntimeReconciliationResultSchema.parse(
reconciled.value.evidence.normalized_result,
);
expect(result.runtime_captures[0]?.kind).toBe("v8-inspector");
expect(result.summary.runtime_scripts).toBe(1);
expect(result.summary.matched).toBeGreaterThan(0);
});
test("keeps unverified partial script URLs unresolved during reconciliation", () => {
const rawUrl = "file:///tmp/not-verified.js?caller-value=kept";
const resolvedObservation = runtimeObservation(
@@ -9,7 +9,7 @@ import { Ajv2020 } from "ajv/dist/2020.js";
import { afterEach, describe, expect, test } from "vitest";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { observeJavaScriptRuntime } from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { V8InspectorProvider } from "../../../src/inspector/V8InspectorProvider.js";
import {
javascriptRuntimeObservationSchema,
@@ -126,7 +126,6 @@ function registerRuntimeParameterContract(
],
});
}
await client.ping();
});
}
@@ -162,6 +161,9 @@ function registerRuntimeMetadataContract(): void {
const unknowns = repeated(101);
const limitations = repeated(101);
const truncationReasons = repeated(21);
const directlyObserved = repeated(21);
const unavailableWithoutInstrumentation = repeated(21);
expect(
javascriptRuntimeObservationSchema.parse({
runtime: {
@@ -184,7 +186,7 @@ function registerRuntimeMetadataContract(): void {
events_dropped: 0,
metadata_bytes_retained: 0,
truncated: false,
truncation_reasons: repeated(21),
truncation_reasons: truncationReasons,
},
scripts: {
items: [],
@@ -196,12 +198,21 @@ function registerRuntimeMetadataContract(): void {
},
},
execution_contexts: [],
directly_observed: repeated(21),
unavailable_without_instrumentation: repeated(21),
directly_observed: directlyObserved,
unavailable_without_instrumentation: unavailableWithoutInstrumentation,
unknowns,
limitations,
}),
).toMatchObject({ unknowns, limitations });
).toMatchObject({
capture: {
observation_ms: 10_001,
truncation_reasons: truncationReasons,
},
directly_observed: directlyObserved,
unavailable_without_instrumentation: unavailableWithoutInstrumentation,
unknowns,
limitations,
});
});
}
@@ -270,28 +281,22 @@ function registerRetainedRuntimeEvidence(
},
},
});
const evidenceId = evidenceIdFrom(observedRuntime.structuredContent);
const evidence = parseEvidence(observedRuntime.structuredContent);
expect(evidence).toMatchObject({
operation: "observe_javascript_runtime",
parameters: {
inspector_endpoint: inspector.endpoint,
target_id: inspector.targetId,
runtime_kind: "node",
observation_ms: 10,
},
});
const bundle = await client.callTool({
name: "get_evidence_bundle",
arguments: {},
});
expect(bundle.isError).not.toBe(true);
expect(parseMcpEvidenceBundle(bundle).records).toContainEqual(
observedRuntime.structuredContent,
);
expect(observedRuntime.structuredContent).toMatchObject({
evidence_id: evidenceId,
operation: "observe_javascript_runtime",
parameters: expect.any(Object),
});
expect(new Set(inspector.commands.map(({ method }) => method))).toEqual(
new Set(["Runtime.enable", "Debugger.enable"]),
);
const provider = new V8InspectorProvider();
resources.push(provider);
const direct = await observeDirectly(inspector, provider);
expect(direct.ok, direct.ok ? undefined : direct.error.message).toBe(true);
if (direct.ok) expect(evidenceId).toBe(direct.value.evidence_id);
expect(parseMcpEvidenceBundle(bundle).records).toContainEqual(evidence);
});
}
@@ -347,26 +352,3 @@ const createObservationClient = async (
await client.connect(clientTransport);
return { entry, inspector, client };
};
const observeDirectly = (
inspector: Awaited<ReturnType<typeof startFakeV8Inspector>>,
provider: V8InspectorProvider,
) =>
observeJavaScriptRuntime(
provider,
observeJavaScriptRuntimeInputSchema.parse({
inspector_endpoint: inspector.endpoint,
target_id: inspector.targetId,
runtime_kind: "node",
observation_ms: 10,
}),
);
const evidenceIdFrom = (value: unknown): string => {
if (typeof value !== "object" || value === null)
throw new TypeError("Missing structured result");
const evidenceId = Reflect.get(value, "evidence_id");
if (typeof evidenceId !== "string")
throw new TypeError("Missing Evidence ID");
return evidenceId;
};
@@ -40,48 +40,7 @@ afterEach(async () => {
);
});
it("keeps byte-identical cross-layer candidates explicitly ambiguous", async () => {
const application = await applicationFixture();
const assets = await createTestTempDirectory("rea-runtime-assets-static-");
const runtime = await createTestTempDirectory("rea-runtime-assets-live-");
temporary.push(application, assets, runtime);
await Promise.all([
writeFile(join(assets, "app.js"), SOURCE),
writeFile(join(runtime, "app.js"), SOURCE),
writeFile(join(runtime, "index.html"), "<script></script>"),
]);
const result = reconcileInput({
static_layers: [
{
role: "application",
analysis: await analyzeFixture(application),
},
{ role: "assets", analysis: await analyzeFixture(assets) },
],
runtime_observations: [
electronRuntimeEvidence(runtime, SOURCE, { includeWorker: false }),
],
});
const script = result.reconciliations.find(
({ entity_kind: kind }) => kind === "script",
);
expect(script).toMatchObject({
status: "ambiguous",
reason: "ambiguous-static-candidates",
candidate_static_count: 2,
});
expect(script?.candidate_static_nodes).toHaveLength(2);
expect(
new Set(
script?.candidate_static_nodes.map(
({ static_layer_id: layerId }) => layerId,
),
).size,
).toBe(2);
});
it("retains all runtime entities and static load states without projection caps", async () => {
it("retains each capture and both runtime instances of a loaded asset", async () => {
const fixture = await applicationFixture();
temporary.push(fixture);
const staticEvidence = await analyzeFixture(fixture);
@@ -101,10 +60,40 @@ it("retains all runtime entities and static load states without projection caps"
runtime_observations: runtimeObservations,
});
expect(result.runtime_captures).toHaveLength(2);
expect(
new Set(result.runtime_captures.map(({ target_node_id: id }) => id)).size,
).toBe(2);
result.runtime_captures
.map(({ evidence_id, target_key }) => [evidence_id, target_key])
.sort(),
).toEqual(
[
[runtimeObservations[0]?.evidence_id, "target-one"],
[runtimeObservations[1]?.evidence_id, "target-two"],
].sort(),
);
const app = result.graph.nodes.find(
({ kind, observations }) =>
kind === "javascript-asset" &&
observations.some(({ properties }) => properties.path === "app.js"),
);
if (app === undefined) throw new Error("Missing fixture application asset");
const scripts = result.reconciliations.filter(
({ entity_kind }) => entity_kind === "script",
);
expect(scripts).toMatchObject([
{ status: "matched", static_node_id: app.node_id },
{ status: "matched", static_node_id: app.node_id },
]);
expect(scripts[0]?.runtime_node_id).not.toBe(scripts[1]?.runtime_node_id);
expect(
result.static_load_states.find(
({ static_node_id }) => static_node_id === app.node_id,
),
).toMatchObject({
status: "loaded",
runtime_node_ids: scripts
.map(({ runtime_node_id }) => runtime_node_id)
.sort(),
});
expect(result.summary).toMatchObject({
runtime_targets: 2,
runtime_frames: 2,
@@ -117,50 +106,6 @@ it("retains all runtime entities and static load states without projection caps"
omitted_reconciliation_items: 0,
omitted_static_load_states: 0,
});
expect(result.static_load_states).toHaveLength(
result.summary.static_loaded +
result.summary.static_resident +
result.summary.static_not_observed +
result.summary.static_unknown,
);
});
it("rejects runtime source bytes whose Evidence omits source-capture approval", async () => {
const fixture = await applicationFixture();
temporary.push(fixture);
const staticEvidence = await analyzeFixture(fixture);
const contradictoryRuntime = electronRuntimeEvidence(fixture, SOURCE, {
includeWorker: false,
sourceIncluded: false,
});
const result = readRuntimeReconciliationInput(
reconcileJavaScriptRuntimeInputSchema.parse({
static_layers: [{ role: "application", analysis: staticEvidence }],
runtime_observations: [contradictoryRuntime],
}),
);
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisInputError",
operation: "reconcile_javascript_runtime",
issues: [
{
path: [
"runtime_observations",
0,
"parameters",
"include_script_sources",
],
reason: "invalid_value",
message: expect.stringMatching(
/source.*without source-capture selection/i,
),
},
],
},
});
});
it("keeps graph omission counts unknown when a runtime section is unavailable", async () => {