fix: auto-approve phase 3 rewrite

This commit is contained in:
User
2026-07-14 13:51:39 +02:00
parent c7d586aaf7
commit 1b1554b353
5 changed files with 169 additions and 13 deletions
@@ -0,0 +1,83 @@
const test = require("node:test");
const assert = require("node:assert");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { createFakePi } = require("./fake_pi_runtime.js");
globalThis.require = require;
const installGatePromise = import("../../tht-gate.js").then((m) => m.default);
function setupFakeTht(t, phase) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fake-tht-rewrite-"));
const log = path.join(dir, "calls.log");
fs.writeFileSync(log, "");
const bin = path.join(dir, "tht");
fs.writeFileSync(
bin,
`#!/bin/bash
echo "$@" >> "${log}"
case "$1 $2" in
"phase show") echo "Fase corrente: ${phase}" ;;
"phase meta") echo '{"max_phase":8,"phases":[{"num":3,"id":"F3","emits":["question_rewritten"]}]}' ;;
*) echo "OK" ;;
esac
`,
{ mode: 0o755 },
);
const oldPath = process.env.PATH;
process.env.PATH = `${dir}:${oldPath}`;
t.after(() => {
process.env.PATH = oldPath;
fs.rmSync(dir, { recursive: true, force: true });
});
return { dir, calls: () => fs.readFileSync(log, "utf8").trim().split("\n").filter(Boolean) };
}
test("rewrite question completes F3 without a reviewer widget", async (t) => {
const fake = setupFakeTht(t, 3);
const installGate = await installGatePromise;
const { pi, ctx, tools } = createFakePi();
ctx.cwd = fake.dir;
installGate(pi);
const { def } = tools.get("rewrite_question");
const result = await def.execute(
"rewrite-1",
{ session: "s1", question: "pazienti con ablazione", assumptions: ["anno 2025"] },
null,
null,
ctx,
);
assert.deepEqual(fake.calls(), [
"phase show --session s1",
"session set-question s1 --question pazienti con ablazione --assumption anno 2025",
"decision add --session s1 --type question_rewritten --subject domanda --detail pazienti con ablazione",
"phase advance --session s1",
"phase meta --json",
]);
assert.equal(ctx.uiCalls.length, 0);
assert.match(result.content[0].text, /Fase 3 completata/);
});
test("rewrite question refuses calls outside F3", async (t) => {
const fake = setupFakeTht(t, 4);
const installGate = await installGatePromise;
const { pi, ctx, tools } = createFakePi();
ctx.cwd = fake.dir;
installGate(pi);
const { def } = tools.get("rewrite_question");
const result = await def.execute(
"rewrite-2",
{ session: "s1", question: "pazienti con ablazione" },
null,
null,
ctx,
);
assert.deepEqual(fake.calls(), ["phase show --session s1"]);
assert.match(result.content[0].text, /solo in Fase 3/);
});