Files
rea/experiments/atomic-codemode/native-ir-contract.mjs
T
N0zoM1z0 f9f9dc209d feat(experiment): retain call facts beyond full graph capacity
Separate native call and boundary-value ownership from the bounded recovered graph. Validate facets independently and preserve shared identities, source evidence, and explicit partial coverage. Pin the SQLite boundary task and oracle before paired real-model trials.

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-16 09:46:42 +00:00

187 lines
5.3 KiB
JavaScript

// Native JSON decoding and resource admission; row-wise validation bounds errors.
import { z } from "zod";
import { FactFailure } from "./fact-error.mjs";
import { NATIVE_OUTPUT_BYTES } from "./limits.mjs";
import { readCallFacet } from "./native-call-contract.mjs";
import {
hex,
ordinal,
valueId,
operationId,
value,
rowParser,
requireFact,
formatFailure,
readNativeFacet,
} from "./native-contract.mjs";
const edgeId = z.string().regex(/^\d+:\d+$/u);
const edge = z.strictObject({
id: edgeId,
target: ordinal,
index: ordinal,
polarity: z.enum(["true", "false"]).nullable(),
condition_value: valueId.nullable(),
condition_test: z.enum(["eq-zero", "ne-zero"]).nullable(),
});
const operation = z.strictObject({
id: operationId,
address: hex,
block: ordinal,
opcode: z.string(),
statement: z.string(),
is_dead: z.boolean(),
inputs: z.array(
z.strictObject({
input_index: ordinal,
value_id: valueId.nullable(),
phi_predecessor: ordinal.nullable(),
phi_predecessor_edge: edgeId.nullable(),
}),
),
output: valueId.nullable(),
});
const factsSchema = z.strictObject({
schema_version: z.literal(3),
blocks: z
.array(
z.strictObject({
id: ordinal,
start: hex,
operations: z.array(operationId),
predecessors: z.array(ordinal),
successors: z.array(edge),
}),
)
.min(1)
.max(12000),
operations: z.array(operation).max(12000),
values: z.array(value).max(36000),
coverage: z.strictObject({
scope: z.string(),
machine_semantic_equivalence: z.literal("unknown"),
indirect_branch_operations: z.array(operationId),
omitted_definition_ids: z.array(operationId),
issues: z.array(z.string()),
}),
});
const documentSchema = z.object({
binary: z.string(),
count: z.literal(1),
language: z.string(),
error: z.null(),
functions: z
.array(
z.object({
address_hex: hex,
code: z.string(),
error: z.null(),
atomic_facts: z.unknown().optional(),
atomic_facts_error: z.string().optional(),
atomic_calls: z.unknown().optional(),
atomic_calls_error: z.string().optional(),
}),
)
.length(1),
});
// Parse one row at a time: a bad provider must not expand thousands of malformed
// records into a giant Zod issue list before the caller can reject the source.
const nativeBlock = factsSchema.shape.blocks.element;
const parseBlock = rowParser(nativeBlock, {
operations: (id) => operationId.parse(id),
predecessors: (id) => ordinal.parse(id),
successors: (row) => edge.parse(row),
});
const parseOperation = rowParser(operation, {
inputs: (row) => operation.shape.inputs.element.parse(row),
});
const parseFacts = rowParser(factsSchema, {
blocks: parseBlock,
operations: parseOperation,
values: (row) => value.parse(row),
});
function preflight(facts) {
requireFact(
facts && typeof facts === "object",
"Native atomic facts facet is unavailable; inspect the retained source for its reason and C",
);
requireFact(
facts.schema_version === 3,
"Unsupported native atomic facts schema version; expected 3",
);
requireFact(
facts.blocks?.length > 0,
"Native recovered graph is unavailable",
);
for (const [key, limit] of [
["blocks", 12000],
["operations", 12000],
["values", 36000],
])
requireFact(
Array.isArray(facts[key]) && facts[key].length <= limit,
`Native ${key} exceeds resource capacity or has invalid shape`,
);
const inputs = facts.operations.reduce(
(sum, op) => sum + (Array.isArray(op?.inputs) ? op.inputs.length : 0),
0,
);
const members = facts.blocks.reduce(
(sum, block) =>
sum + (Array.isArray(block?.operations) ? block.operations.length : 0),
0,
);
const edges = facts.blocks.reduce(
(sum, block) =>
sum +
(Array.isArray(block?.successors) ? block.successors.length : 0) +
(Array.isArray(block?.predecessors) ? block.predecessors.length : 0),
0,
);
requireFact(
inputs <= 50000 && members <= 12000 && edges <= 100000,
"Native relationships exceed resource capacity",
);
}
const readGraph = (fn) =>
readNativeFacet(fn, "atomic_facts", "recovered IR", (raw) => {
preflight(raw);
return parseFacts(raw);
});
export function parseNativeFunction(text, entry, expectedInput) {
try {
requireFact(
Buffer.byteLength(text) <= NATIVE_OUTPUT_BYTES,
"Native JSON source exceeds transport budget",
);
const raw = JSON.parse(text);
requireFact(
Array.isArray(raw?.functions) && raw.functions.length === 1,
"Native source must describe exactly one function",
);
const doc = documentSchema.parse(raw),
fn = doc.functions[0];
if (expectedInput !== undefined && doc.binary !== expectedInput)
throw new FactFailure(
"subject_mismatch",
"Native source names a different command input",
{ expected: expectedInput, observed: doc.binary },
);
if (fn.address_hex !== entry)
throw new FactFailure(
"subject_mismatch",
"Native function entry differs from requested subject",
);
return {
code: fn.code,
language: doc.language,
facts: readGraph(fn),
calls: readCallFacet(fn),
};
} catch (error) {
throw formatFailure(error);
}
}