This closes the learning loop. Lessons in knowledge.db now actually
influence behaviour:
- runtime/coach/advice.js: consult({plannedSkillId, snapshot}) reads
knowledge.topAdvice() and returns 'override' / 'avoid' / 'proceed'.
When a lesson says "avoid <skill>" with prefer="survive.flee" (etc.),
the dispatcher swaps in the alternative.
- runtime/reflex.js:
* curriculumReflex now consults advice before dispatch; on 'avoid'
backs off the planned skill + sets wander hint; on 'override'
dispatches the lesson's preferred alternative.
* defendReflex (dist≤4 melee branch) consults advice too — so a
creeper at 4m honours the starter rule "attack creeper → flee".
Failure outcomes feed back via markApplied so confidence stays
grounded.
SAFE_OVERRIDES whitelist contains only known runSkill targets
(survive.flee, survive.sleep, survive.eat, recovery.tunnel-out,
explore.far/wander, village.build-shelter); unknown prefers fall back
to plain 'avoid'.
7 advice tests; total suite 237 green.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
98 lines
3.0 KiB
JavaScript
98 lines
3.0 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
import { initKnowledge, record } from "../knowledge/index.js";
|
|
import { __resetForTests, isAvailable, closeStore } from "../knowledge/store.js";
|
|
import { consult, reportOutcome, __testing } from "./advice.js";
|
|
|
|
const { SAFE_OVERRIDES } = __testing;
|
|
|
|
async function bootstrap() {
|
|
__resetForTests();
|
|
const tmp = mkdtempSync(join(tmpdir(), "pepa-advice-test-"));
|
|
await initKnowledge({ stateDir: tmp });
|
|
return tmp;
|
|
}
|
|
|
|
function cleanup(tmp) {
|
|
closeStore();
|
|
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
|
}
|
|
|
|
test("consult: returns proceed when knowledge disabled", () => {
|
|
__resetForTests();
|
|
const res = consult({ plannedSkillId: "gather.logs", snapshot: {} });
|
|
assert.equal(res.action, "proceed");
|
|
});
|
|
|
|
test("consult: returns proceed when no relevant lesson", async () => {
|
|
const tmp = await bootstrap();
|
|
if (!isAvailable()) { cleanup(tmp); return; }
|
|
const res = consult({ plannedSkillId: "gather.unknown-skill", snapshot: {} });
|
|
assert.equal(res.action, "proceed");
|
|
cleanup(tmp);
|
|
});
|
|
|
|
test("consult: starter creeper rule routes attack → survive.flee", async () => {
|
|
const tmp = await bootstrap();
|
|
if (!isAvailable()) { cleanup(tmp); return; }
|
|
const res = consult({
|
|
plannedSkillId: "attack creeper",
|
|
snapshot: { closestHostile: { name: "creeper", distance: 4 } },
|
|
});
|
|
assert.equal(res.action, "override");
|
|
assert.equal(res.overrideSkillId, "survive.flee");
|
|
assert.ok(res.lessonId);
|
|
assert.ok(res.lesson);
|
|
cleanup(tmp);
|
|
});
|
|
|
|
test("consult: avoid lesson without prefer → 'avoid' action", async () => {
|
|
const tmp = await bootstrap();
|
|
if (!isAvailable()) { cleanup(tmp); return; }
|
|
record({
|
|
text: "Don't gather.stone — confirmed flaky.",
|
|
category: "pathing",
|
|
triggerSkill: "gather.stone",
|
|
avoidSkill: "gather.stone",
|
|
preferSkill: null,
|
|
confidence: 0.9,
|
|
source: "test",
|
|
});
|
|
const res = consult({ plannedSkillId: "gather.stone", snapshot: {} });
|
|
assert.equal(res.action, "avoid");
|
|
assert.ok(res.lessonId);
|
|
cleanup(tmp);
|
|
});
|
|
|
|
test("consult: prefer outside SAFE_OVERRIDES set → falls to avoid", async () => {
|
|
const tmp = await bootstrap();
|
|
if (!isAvailable()) { cleanup(tmp); return; }
|
|
record({
|
|
text: "test fallback",
|
|
category: "combat",
|
|
triggerSkill: "gather.logs",
|
|
avoidSkill: "gather.logs",
|
|
preferSkill: "non.standard.skill",
|
|
confidence: 0.9,
|
|
source: "test",
|
|
});
|
|
const res = consult({ plannedSkillId: "gather.logs", snapshot: {} });
|
|
assert.equal(res.action, "avoid", "unsafe prefer falls back to avoid, not override");
|
|
cleanup(tmp);
|
|
});
|
|
|
|
test("reportOutcome: no-op without lessonId", () => {
|
|
reportOutcome({ lessonId: null });
|
|
assert.ok(true);
|
|
});
|
|
|
|
test("SAFE_OVERRIDES: only contains known reflex skills", () => {
|
|
for (const id of SAFE_OVERRIDES) {
|
|
assert.ok(typeof id === "string" && id.includes("."), `${id} looks like a real skill id`);
|
|
}
|
|
});
|