Embed Facts in the PI narrative prompt and teach the bind-mounted runner to write assistant text into missing reasoning targets, matching pi-native.
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
import { strict as assert } from "node:assert";
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve, dirname } from "node:path";
|
|
import {
|
|
stripThinkBlocks,
|
|
extractAssistantText,
|
|
materializeReasoningTargets,
|
|
} from "./reasoning-materialize.mjs";
|
|
|
|
assert.equal(stripThinkBlocks("<think>secret</think>\n{\"a\":1}"), "{\"a\":1}");
|
|
assert.equal(
|
|
extractAssistantText({
|
|
type: "message_end",
|
|
message: { role: "assistant", content: [{ type: "text", text: "<think>x</think>\n{\"ok\":true}" }] },
|
|
}),
|
|
"<think>x</think>\n{\"ok\":true}",
|
|
);
|
|
assert.equal(extractAssistantText({ type: "message_end", message: { role: "user", content: [{ type: "text", text: "no" }] } }), "");
|
|
|
|
const dir = mkdtempSync(join(tmpdir(), "pi-reason-mat-"));
|
|
try {
|
|
const written = materializeReasoningTargets({
|
|
workspace: dir,
|
|
targets: ["narrative.md"],
|
|
assistantText: "<think>ignore</think>\n{\"schema_kind\":\"change_report_narrative_v1\"}",
|
|
existsSync,
|
|
readFileSync,
|
|
writeFileSync,
|
|
mkdirSync,
|
|
resolve,
|
|
dirname,
|
|
});
|
|
assert.deepEqual(written, ["narrative.md"]);
|
|
assert.equal(readFileSync(join(dir, "narrative.md"), "utf8"), "{\"schema_kind\":\"change_report_narrative_v1\"}");
|
|
|
|
// Existing non-empty target must not be overwritten.
|
|
writeFileSync(join(dir, "keep.md"), "kept", "utf8");
|
|
const again = materializeReasoningTargets({
|
|
workspace: dir,
|
|
targets: ["keep.md"],
|
|
assistantText: "overwrite?",
|
|
existsSync,
|
|
readFileSync,
|
|
writeFileSync,
|
|
mkdirSync,
|
|
resolve,
|
|
dirname,
|
|
});
|
|
assert.deepEqual(again, []);
|
|
assert.equal(readFileSync(join(dir, "keep.md"), "utf8"), "kept");
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
|
|
console.log("reasoning-materialize.test.mjs PASS");
|