Files
rea/scripts/lib/kuna-reference-verifier.mjs
T
c9f9b83c6f feat(kuna): add bounded call relationships and exact xrefs
Add session-cached call relationships and exact incoming xrefs using a body-free graph and bounded native queries. Reuse existing graph/path tools, preserve coverage and exact u64 identities, and retain native cleanup owners and admission leases.

Validated with 136 tests, real Linux Kuna workflows on SQLite/zlib and source-owned fixtures, independent native xref comparisons, resource measurements, CI and review.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-10-15 11:12:47 +00:00

110 lines
3.3 KiB
JavaScript

import assert from "node:assert/strict";
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import { combineFunctionRelationshipCoverage } from "../../dist/domain/functionCallCoverage.js";
const entries = async (path) => {
try {
return (await readdir(path)).sort();
} catch (cause) {
if (cause.code === "ENOENT") return [];
throw cause;
}
};
/** Verify selected public reference answers without requesting source text. */
export const verifyKunaReferences = async ({
call,
cacheRoot,
name,
address,
expectedCaller,
nativeIncoming,
}) => {
const before = await Promise.all([
entries(join(cacheRoot, "projects")),
entries(join(cacheRoot, "staging")),
]);
const callers = await call("procedure_callers", { procedure: address });
const callees = await call("procedure_callees", { procedure: address });
const incoming = await call("xrefs", { address });
for (const [label, result] of [
["callers", callers],
["callees", callees],
["xrefs", incoming],
]) {
assert.ok(
Array.isArray(result.value),
`${name} ${label} omitted its address list`,
);
assert.equal(
new Set(result.value).size,
result.value.length,
`${label} repeats an address`,
);
assert.ok(
result.value.every((value) =>
/^0x(?:0|[1-9a-f][0-9a-f]{0,15})$/u.test(value),
),
`${label} returned a noncanonical u64 address`,
);
assert.equal(result.structured.provider.id, "kuna");
const source = result.structured.raw_result.references;
assert.match(source.sha256, /^[0-9a-f]{64}$/u);
assert.ok(source.bytes > 0);
}
let composed;
if (expectedCaller !== undefined) {
assert.ok(
callers.value.includes(expectedCaller),
`${name} lost its source-owned caller`,
);
assert.ok(
incoming.value.length > 0,
`${name} lost its source-owned call site`,
);
assert.deepEqual(
[...incoming.value].sort(),
await nativeIncoming(address),
`${name} incoming references differ from exact-target native sites`,
);
const graph = await call("get_call_graph", {
address: expectedCaller,
direction: "forward",
});
assert.ok(graph.value.graph["0"][0].calls.includes(address));
const path = await call("trace_call_path", {
start: expectedCaller,
goal: address,
direction: "forward",
});
assert.equal(path.value.goal_status, "reached");
composed = { graph_ms: graph.ms, path_ms: path.ms };
}
assert.deepEqual(
await Promise.all([
entries(join(cacheRoot, "projects")),
entries(join(cacheRoot, "staging")),
]),
before,
"A reference-only workflow started a C project export",
);
const repeated = await call("procedure_callees", { procedure: address });
assert.deepEqual(repeated.value, callees.value);
return {
callers: callers.value.length,
callees: callees.value.length,
xrefs: incoming.value.length,
first_graph_ms: callers.ms,
warm_graph_ms: repeated.ms,
xrefs_ms: incoming.ms,
response_bytes: callers.bytes + callees.bytes + incoming.bytes,
relationship_coverage: combineFunctionRelationshipCoverage(
callers.structured.relationship_coverage,
callees.structured.relationship_coverage,
),
...composed,
};
};