Files
rea/experiments/atomic-codemode/native-contract.mjs
T
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

90 lines
2.8 KiB
JavaScript

// Shared native identity/value contracts and bounded row/facet decoding.
import { z } from "zod";
import { FactFailure } from "./fact-error.mjs";
import { ok, err } from "../../dist/domain/result.js";
export const requireFact = (condition, reason) => {
if (!condition) throw new FactFailure("invalid_provider_output", reason);
};
export const hex = z.string().regex(/^0x(?:0|[1-9a-f][0-9a-f]{0,15})$/u);
export const ordinal = z.number().int().nonnegative().max(0x7fffffff);
export const valueId = z.string().regex(/^v[0-9a-f]{1,16}$/u);
export const operationId = z
.string()
.regex(/^0x[0-9a-f]{1,16}:[0-9a-f]{1,8}$/u);
export const value = z.strictObject({
id: valueId,
space: z.string().nullable(),
offset: hex,
size_bytes: z.number().int().positive().max(0x7fffffff),
is_constant: z.boolean(),
is_input: z.boolean(),
is_annotation: z.boolean(),
definition: operationId.nullable(),
type_name: z.string(),
});
export const rowParser = (schema, arrays) => {
const metadata = schema.omit(
Object.fromEntries(Object.keys(arrays).map((key) => [key, true])),
);
return (raw) => {
const fields = { ...raw };
for (const key of Object.keys(arrays)) delete fields[key];
const result = metadata.parse(fields);
for (const [key, parse] of Object.entries(arrays)) {
requireFact(Array.isArray(raw[key]), `Native ${key} must be an array`);
result[key] = raw[key].map(parse);
}
return result;
};
};
export const formatFailure = (error) => {
if (error instanceof FactFailure) return error;
const details =
error instanceof z.ZodError
? {
issues: error.issues.slice(0, 8).map(({ path, code, message }) => ({
path,
code,
message: message.slice(0, 160),
})),
}
: { reason: String(error.message).slice(0, 256) };
return new FactFailure(
"invalid_provider_output",
"Native IR source does not match the declared format",
details,
);
};
export const readNativeFacet = (fn, name, label, parse) => {
try {
const reason = fn[`${name}_error`];
if (reason !== undefined) {
requireFact(
fn[name] === undefined,
`Native ${label} is both present and unavailable`,
);
return err(
new FactFailure(
"capability_unavailable",
`Native ${label} facet is unavailable`,
{
native_error: reason.slice(0, 1024),
native_error_truncated: reason.length > 1024,
},
),
);
}
if (fn[name] === undefined)
return err(
new FactFailure(
"capability_unavailable",
`Native ${label} facet was not captured`,
),
);
return ok(parse(fn[name]));
} catch (error) {
return err(formatFailure(error));
}
};