2026-10-09 00:20:46 +08:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
recordWorkflowUnknowns,
|
|
|
|
|
createWorkflowEvidence,
|
2026-10-14 08:32:00 +08:00
|
|
|
executeComposedWorkflow,
|
2026-10-09 00:20:46 +08:00
|
|
|
} from "./WorkflowEvidence.js";
|
|
|
|
|
import { InvestigationRecords } from "./investigation/InvestigationRecords.js";
|
|
|
|
|
import { UnknownRegistryError } from "../domain/unknownRegistryError.js";
|
2026-10-14 08:32:00 +08:00
|
|
|
import { err, ok } from "../domain/result.js";
|
|
|
|
|
import {
|
|
|
|
|
createAnalysisExecution,
|
|
|
|
|
type AnalysisOperationPort,
|
|
|
|
|
} from "./AnalysisProvider.js";
|
|
|
|
|
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
2026-10-11 14:56:46 +08:00
|
|
|
import { nativeApiBoundarySchema } from "../domain/native/nativeApiBoundary.js";
|
|
|
|
|
import { projectNativeApiInspection } from "./native/NativeApiInspection.js";
|
|
|
|
|
import { functionDossierSchema } from "../domain/hopperValues.js";
|
|
|
|
|
import { ghidraFunctionDossier } from "../domain/ghidraValues.fixture.js";
|
2026-10-09 00:20:46 +08:00
|
|
|
|
2026-10-14 08:32:00 +08:00
|
|
|
const provider = { id: "fixture", name: "Fixture", version: "1" };
|
|
|
|
|
|
|
|
|
|
it("keeps Evidence identity stable when dependent branches finish in opposite orders", async () => {
|
|
|
|
|
const run = async (order: readonly number[]) => {
|
|
|
|
|
const gate = () => {
|
|
|
|
|
let resolve: () => void = () => {
|
|
|
|
|
throw new Error("gate not initialized");
|
|
|
|
|
};
|
|
|
|
|
const promise = new Promise<void>((release) => {
|
|
|
|
|
resolve = release;
|
|
|
|
|
});
|
|
|
|
|
return { promise, resolve };
|
|
|
|
|
};
|
|
|
|
|
const parents = [gate(), gate()];
|
|
|
|
|
const children = [gate(), gate()];
|
|
|
|
|
const analysis: AnalysisOperationPort = {
|
|
|
|
|
execute: async (operation, parameters) => {
|
|
|
|
|
const index = Number(parameters.index);
|
|
|
|
|
if (operation === "procedure_address") {
|
|
|
|
|
await parents[index]!.promise;
|
|
|
|
|
return ok(createAnalysisExecution(String(index), provider));
|
|
|
|
|
}
|
|
|
|
|
children[index]!.resolve();
|
|
|
|
|
return ok(
|
|
|
|
|
createAnalysisExecution(String(index), provider, {
|
|
|
|
|
limitations: [
|
|
|
|
|
index === 0 ? "first child caveat" : "second child caveat",
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const composed = await executeComposedWorkflow(
|
|
|
|
|
analysis,
|
|
|
|
|
async (observed) => {
|
|
|
|
|
const branches = Promise.all(
|
|
|
|
|
[0, 1].map(async (index) => {
|
|
|
|
|
await observed.execute("procedure_address", { index });
|
|
|
|
|
const child = await observed.execute("address_name", { index });
|
|
|
|
|
if (!child.ok) throw child.error;
|
|
|
|
|
return child.value.result;
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
for (const index of order) {
|
|
|
|
|
parents[index]!.resolve();
|
|
|
|
|
await children[index]!.promise;
|
|
|
|
|
}
|
|
|
|
|
return ok(await branches);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
if (!composed.ok) throw composed.error;
|
|
|
|
|
return createWorkflowEvidence({
|
|
|
|
|
target: undefined,
|
|
|
|
|
operation: "batch_decompile",
|
|
|
|
|
parameters: {},
|
|
|
|
|
result: composed.value.result,
|
|
|
|
|
upstreamProfile: undefined,
|
|
|
|
|
upstreamLimitations: composed.value.limitations,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const left = await run([0, 1]);
|
|
|
|
|
const right = await run([1, 0]);
|
|
|
|
|
expect(left.normalized_result).toEqual(right.normalized_result);
|
|
|
|
|
expect(left.limitations).toEqual(right.limitations);
|
|
|
|
|
expect(left.evidence_id).toBe(right.evidence_id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains unique upstream limits in canonical order across concurrent observations", async () => {
|
|
|
|
|
let release: () => void = () => {
|
|
|
|
|
throw new Error("gate not initialized");
|
|
|
|
|
};
|
|
|
|
|
const gate = new Promise<void>((resolve) => {
|
|
|
|
|
release = resolve;
|
|
|
|
|
});
|
|
|
|
|
let calls = 0;
|
|
|
|
|
const options = { signal: new AbortController().signal };
|
|
|
|
|
const analysis: AnalysisOperationPort = {
|
|
|
|
|
execute: async (_, parameters, forwarded) => {
|
|
|
|
|
expect(parameters).toEqual({ marker: true });
|
|
|
|
|
expect(forwarded).toBe(options);
|
|
|
|
|
const call = calls++;
|
|
|
|
|
if (call === 0) await gate;
|
|
|
|
|
else release();
|
|
|
|
|
return ok(
|
|
|
|
|
createAnalysisExecution([], provider, {
|
|
|
|
|
limitations: call === 0 ? ["first", "shared"] : ["second", "shared"],
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await executeComposedWorkflow(analysis, async (observed) => {
|
|
|
|
|
await Promise.all([
|
|
|
|
|
observed.execute("list_strings", { marker: true }, options),
|
|
|
|
|
observed.execute("list_procedures", { marker: true }, options),
|
|
|
|
|
]);
|
|
|
|
|
return ok({ count: 0 });
|
|
|
|
|
});
|
|
|
|
|
expect(calls).toBe(2);
|
|
|
|
|
expect(result).toEqual(
|
|
|
|
|
ok({ result: { count: 0 }, limitations: ["first", "second", "shared"] }),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves a failed composed operation without an extra provider read", async () => {
|
|
|
|
|
const failure = err(new AnalysisCancelledError("list_strings"));
|
|
|
|
|
let calls = 0;
|
|
|
|
|
const result = await executeComposedWorkflow(
|
|
|
|
|
{
|
|
|
|
|
execute: async () => {
|
|
|
|
|
calls++;
|
|
|
|
|
return failure;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
async (observed) => {
|
|
|
|
|
const observedResult = await observed.execute("list_strings", {});
|
|
|
|
|
return observedResult.ok
|
|
|
|
|
? ok(observedResult.value.result)
|
|
|
|
|
: observedResult;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(result).toBe(failure);
|
|
|
|
|
expect(calls).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-09 00:20:46 +08:00
|
|
|
describe("workflow residual question retention", () => {
|
|
|
|
|
it("keeps questions linked to workflow Evidence and records repeats idempotently", () => {
|
|
|
|
|
const records = new InvestigationRecords();
|
|
|
|
|
const result = {
|
|
|
|
|
residual_unknowns: ["Which provider resolves this boundary?"],
|
|
|
|
|
};
|
|
|
|
|
const evidence = createWorkflowEvidence({
|
|
|
|
|
target: undefined,
|
|
|
|
|
operation: "inspect_native_api",
|
|
|
|
|
parameters: { procedure: "0x1000" },
|
|
|
|
|
result,
|
|
|
|
|
upstreamProfile: undefined,
|
2026-10-14 08:32:00 +08:00
|
|
|
upstreamLimitations: [],
|
2026-10-09 00:20:46 +08:00
|
|
|
});
|
|
|
|
|
expect(records.recordEvidence(evidence).ok).toBe(true);
|
|
|
|
|
const input = {
|
|
|
|
|
name: "inspect_native_api",
|
|
|
|
|
result,
|
|
|
|
|
evidenceId: evidence.evidence_id,
|
|
|
|
|
recordUnknown: (
|
|
|
|
|
unknown: Parameters<InvestigationRecords["recordUnknown"]>[0],
|
|
|
|
|
) => records.recordUnknown(unknown),
|
|
|
|
|
};
|
|
|
|
|
expect(recordWorkflowUnknowns(input).ok).toBe(true);
|
|
|
|
|
expect(recordWorkflowUnknowns(input).ok).toBe(true);
|
|
|
|
|
expect(records.listUnknowns()).toMatchObject([
|
|
|
|
|
{
|
|
|
|
|
question: result.residual_unknowns[0],
|
|
|
|
|
domain: "native-api",
|
|
|
|
|
supporting_evidence_ids: [evidence.evidence_id],
|
|
|
|
|
required_authority: "shipped-artifact",
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-11 19:58:55 +08:00
|
|
|
it.each([
|
|
|
|
|
["spaces", " Which boundary? "],
|
|
|
|
|
["line feed and tab", "\nWhich boundary?\t"],
|
|
|
|
|
["byte order mark", "\uFEFFWhich boundary?\uFEFF"],
|
|
|
|
|
])(
|
|
|
|
|
"normalizes edge whitespace from %s without changing workflow Evidence",
|
|
|
|
|
(_, question) => {
|
|
|
|
|
const records = new InvestigationRecords();
|
|
|
|
|
const result = { residual_unknowns: [question] };
|
|
|
|
|
const evidence = createWorkflowEvidence({
|
|
|
|
|
target: undefined,
|
|
|
|
|
operation: "inspect_native_api",
|
|
|
|
|
parameters: {},
|
|
|
|
|
result,
|
|
|
|
|
upstreamProfile: undefined,
|
2026-10-14 08:32:00 +08:00
|
|
|
upstreamLimitations: [],
|
2026-10-11 19:58:55 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(records.recordEvidence(evidence).ok).toBe(true);
|
|
|
|
|
expect(
|
|
|
|
|
recordWorkflowUnknowns({
|
|
|
|
|
name: "inspect_native_api",
|
|
|
|
|
result,
|
|
|
|
|
evidenceId: evidence.evidence_id,
|
|
|
|
|
recordUnknown: (unknown) => records.recordUnknown(unknown),
|
|
|
|
|
}).ok,
|
|
|
|
|
).toBe(true);
|
|
|
|
|
expect(records.listUnknowns()).toMatchObject([
|
|
|
|
|
{ question: "Which boundary?" },
|
|
|
|
|
]);
|
|
|
|
|
expect(evidence.normalized_result).toEqual(result);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("rejects questions that are empty after whitespace normalization", () => {
|
|
|
|
|
const records = new InvestigationRecords();
|
|
|
|
|
const result = { residual_unknowns: [" \n\t "] };
|
|
|
|
|
const evidence = createWorkflowEvidence({
|
|
|
|
|
target: undefined,
|
|
|
|
|
operation: "inspect_native_api",
|
|
|
|
|
parameters: {},
|
|
|
|
|
result,
|
|
|
|
|
upstreamProfile: undefined,
|
2026-10-14 08:32:00 +08:00
|
|
|
upstreamLimitations: [],
|
2026-10-11 19:58:55 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(records.recordEvidence(evidence).ok).toBe(true);
|
|
|
|
|
expect(
|
|
|
|
|
recordWorkflowUnknowns({
|
|
|
|
|
name: "inspect_native_api",
|
|
|
|
|
result,
|
|
|
|
|
evidenceId: evidence.evidence_id,
|
|
|
|
|
recordUnknown: (unknown) => records.recordUnknown(unknown),
|
|
|
|
|
}).ok,
|
|
|
|
|
).toBe(false);
|
|
|
|
|
expect(records.listUnknowns()).toEqual([]);
|
|
|
|
|
expect(evidence.normalized_result).toEqual(result);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-09 00:20:46 +08:00
|
|
|
it("preserves registry failures rather than presenting a completed workflow", () => {
|
|
|
|
|
const error = new UnknownRegistryError("limit");
|
|
|
|
|
expect(
|
|
|
|
|
recordWorkflowUnknowns({
|
|
|
|
|
name: "trace_feature",
|
|
|
|
|
result: { residual_unknowns: ["Which branch handles the request?"] },
|
|
|
|
|
evidenceId: "unretained",
|
|
|
|
|
recordUnknown: () => err(error),
|
|
|
|
|
}),
|
|
|
|
|
).toEqual(err(error));
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-10-11 14:56:46 +08:00
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
" Which boundary? ",
|
|
|
|
|
"\nWhich boundary?\t",
|
|
|
|
|
"\uFEFFWhich boundary?\uFEFF",
|
|
|
|
|
])(
|
|
|
|
|
"retains contract-accepted question %j without rewriting workflow Evidence",
|
|
|
|
|
(question) => {
|
|
|
|
|
const boundary = nativeApiBoundarySchema.parse({
|
|
|
|
|
available: false,
|
|
|
|
|
reason: "Synthetic provider unavailable boundary",
|
|
|
|
|
residual_unknowns: [question],
|
|
|
|
|
});
|
|
|
|
|
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
|
|
|
|
|
const result = projectNativeApiInspection({
|
|
|
|
|
...dossier,
|
|
|
|
|
native_api: boundary,
|
|
|
|
|
});
|
|
|
|
|
const evidence = createWorkflowEvidence({
|
|
|
|
|
target: undefined,
|
|
|
|
|
operation: "inspect_native_api",
|
|
|
|
|
parameters: { procedure: "0x1000" },
|
|
|
|
|
result,
|
|
|
|
|
upstreamProfile: undefined,
|
2026-10-14 08:32:00 +08:00
|
|
|
upstreamLimitations: [],
|
2026-10-11 14:56:46 +08:00
|
|
|
});
|
|
|
|
|
const records = new InvestigationRecords();
|
|
|
|
|
expect(records.recordEvidence(evidence).ok).toBe(true);
|
|
|
|
|
expect(
|
|
|
|
|
recordWorkflowUnknowns({
|
|
|
|
|
name: "inspect_native_api",
|
|
|
|
|
result,
|
|
|
|
|
evidenceId: evidence.evidence_id,
|
|
|
|
|
recordUnknown: (input) => records.recordUnknown(input),
|
|
|
|
|
}).ok,
|
|
|
|
|
).toBe(true);
|
|
|
|
|
expect(records.listUnknowns()).toMatchObject([
|
|
|
|
|
{ question: "Which boundary?" },
|
|
|
|
|
]);
|
|
|
|
|
expect(records.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
|
|
|
|
expect(evidence.normalized_result).toMatchObject({
|
|
|
|
|
residual_unknowns: [question],
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|