Files
rea/experiments/atomic-codemode/instruction-facts.test.mjs
T
N0zoM1z0 4f13bd6733 experiment(re)!: separate IR origins from machine instruction facts
A real model conflated an optimized CFG edge with the bytes at its instruction origin. Make provenance explicit and add bounded, batched instruction windows from the existing Kuna CLI, preserving exact bytes, per-window failures and Evidence. Record the task-cost comparison without treating the incomplete machine-path answer as an equal-quality win.

Verified 33 focused tests, targeted lint, real stock/patched MCP decoding with mixed success, and a real 4096-row capacity run under the resource observer.

BREAKING CHANGE: Experimental operation.address/addresses and block.start become origin_address/origin_addresses and origin_start. Printed CFG target_address becomes target_origin; historical source captures are unchanged.

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 04:51:55 +00:00

93 lines
2.6 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { parseInstructions } from "./instruction-facts.mjs";
const document = (address = "0xd97e") => ({
binary: "/private/input",
kind: "code",
start_hex: address,
end_hex: `0x${(BigInt(address) + 6n).toString(16)}`,
count: 1,
bytes: 6,
truncated: true,
notes: ["selected window"],
instructions: [
{
address_hex: address,
size: 6,
bytes: "0f87e9080000",
mnemonic: "JA",
operands: "0xe26d",
text: "JA 0xe26d",
},
],
});
const parse = (doc, address = doc.start_hex) =>
parseInstructions(JSON.stringify(doc), "/private/input", {
address,
count: 1,
});
test("instruction facts preserve exact bytes/addresses and do not substitute an optimized CFG target", () => {
const result = parse(document());
assert.equal(result.items[0].address, "0xd97e");
assert.equal(result.items[0].text, "JA 0xe26d");
assert.equal(result.items[0].bytes, "0f87e9080000");
assert.equal(result.items[0].status, "decoded");
assert.deepEqual(result.coverage.notes, ["selected window"]);
const high = document("0x20000000000001");
high.instructions[0].address = Number(BigInt(high.start_hex));
assert.equal(parse(high).items[0].address, "0x20000000000001");
});
test("unknown bytes remain undecoded and projection retains byte-range identity", () => {
const doc = document();
doc.instructions[0].mnemonic = ".byte";
const result = parseInstructions(
JSON.stringify(doc),
"/private/input",
{ address: "0xd97e", count: 1 },
["mnemonic"],
);
assert.deepEqual(result.items[0], {
address: "0xd97e",
size: 6,
status: "undecoded",
mnemonic: ".byte",
});
});
test("wrong target/window, gaps, extent and byte-size contradictions fail at decoding boundary", () => {
for (const mutate of [
(doc) => {
doc.binary = "/other/input";
},
(doc) => {
doc.instructions[0].address_hex = "0xd97f";
},
(doc) => {
doc.instructions[0].size = 5;
},
(doc) => {
doc.end_hex = "0xd985";
},
(doc) => {
doc.count = 2;
},
]) {
const doc = document();
mutate(doc);
assert.throws(
() => parse(doc),
(error) => error.code === "invalid_provider_output",
);
}
assert.throws(() => parse(document(), "0xd970"), /window/u);
});
test("decoded-row capacity rejects oversized malformed arrays before expanding errors", () => {
const doc = document();
doc.instructions = Array.from({ length: 4097 }, () => ({}));
assert.throws(() => parse(doc), /capacity/u);
});