P0 (correctness):
1. PEPA_HEADLESS=1 guard in extensions/mineflayer-bridge.ts. When `pi -p`
spawns a subprocess (banter, coach, planner, reflect, auto-patch), the
bridge no longer attempts a second MC connect — the hybrid runtime
already owns the nickname. runtime/pi-bridge.js sets the env var on
every spawn. Root cause of the "two pepa_bot's racing for the slot"
bug seen in reply-pi stderr.
2. Test state isolation in runtime/config.js. When running under the node
test runner (detected via execArgv/argv) — or when PEPA_STATE_DIR is
set — stateDir redirects to /tmp/pepa-test-state-<pid>/. log.js,
scenario-memory, world-journal, and knowledge.db all follow.
`npm test` no longer pollutes live scenarios.jsonl, world-journal.jsonl,
or daily log files. Verified empirically: post-fix run added 0 test
rows to the live scenarios file. Cleaned ~550 historical test rows
from live state in the same change.
3. defendReflex outcome reporting (runtime/reflex.js). Previously a
creeper-rule override marked the lesson succeeded=false BEFORE the
flee skill returned. Now dispatchDefendFlee accepts {lessonId} and
the onComplete fires reportAdviceOutcome with the actual flee result.
4. Mode-name → skill-id translation in runtime/coach/advice.js. Pi-coach
occasionally returns prefer_skill values that are mode names
("night_shelter", "self_preservation", "hunger"). normalisePreferSkill
maps these to SAFE_OVERRIDES entries before dispatch. Also handles
"tunnel-out", "survive_flee", "survive flee" shapes.
New behavior:
5. Self-reflection loop (runtime/coach/reflect.js). Every 30 min, the
bot asks Pi: "Are you making progress, or stuck in a loop? What
should you do differently?" Pi answers with a verdict
(progress/loop/recovering/idle/emergency), summary, next-action, and
0-N new lessons. The reflection is written to
state/<host>/reflections/<ts>.md and lessons land in the DB with
source="pi-reflect". Rate-limited to 2 calls/hour. Wired through
bot.js with the existing askPi + lastSnapshot accessor.
Tests: 246/246 green (+9 new: 6 advice mode-name + 4 reflect).
Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
143 lines
5.0 KiB
JavaScript
143 lines
5.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, MODE_TO_SKILL, normalisePreferSkill } = __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`);
|
|
}
|
|
});
|
|
|
|
test("normalisePreferSkill: mode names translate to skill ids", () => {
|
|
assert.equal(normalisePreferSkill("self_preservation"), "survive.flee");
|
|
assert.equal(normalisePreferSkill("night_shelter"), "survive.sleep");
|
|
assert.equal(normalisePreferSkill("hunger"), "survive.eat");
|
|
assert.equal(normalisePreferSkill("shelter"), "village.build-shelter");
|
|
assert.equal(normalisePreferSkill("flee"), "survive.flee");
|
|
assert.equal(normalisePreferSkill("eat"), "survive.eat");
|
|
assert.equal(normalisePreferSkill("tunnel-out"), "recovery.tunnel-out");
|
|
assert.equal(normalisePreferSkill("tunnel_out"), "recovery.tunnel-out");
|
|
});
|
|
|
|
test("normalisePreferSkill: 'survive_flee' shape gets translated to dot form", () => {
|
|
assert.equal(normalisePreferSkill("survive_flee"), "survive.flee");
|
|
assert.equal(normalisePreferSkill("survive sleep"), "survive.sleep");
|
|
});
|
|
|
|
test("normalisePreferSkill: passes through known dot-form skills unchanged", () => {
|
|
assert.equal(normalisePreferSkill("survive.flee"), "survive.flee");
|
|
assert.equal(normalisePreferSkill("explore.far"), "explore.far");
|
|
});
|
|
|
|
test("normalisePreferSkill: unknown values returned as-is", () => {
|
|
assert.equal(normalisePreferSkill("some.unknown.skill"), "some.unknown.skill");
|
|
assert.equal(normalisePreferSkill(null), null);
|
|
assert.equal(normalisePreferSkill(""), "");
|
|
});
|
|
|
|
test("consult: Pi-style mode-name prefer is normalised to override target", async () => {
|
|
const tmp = await bootstrap();
|
|
if (!isAvailable()) { cleanup(tmp); return; }
|
|
const { id } = (await import("../knowledge/index.js")).record({
|
|
text: "After a death at night, prefer shelter.",
|
|
category: "survival",
|
|
triggerSkill: "gather.logs",
|
|
avoidSkill: "gather.logs",
|
|
preferSkill: "night_shelter", // Pi gave a mode name, not a skill id
|
|
confidence: 0.9,
|
|
source: "test",
|
|
});
|
|
const res = consult({ plannedSkillId: "gather.logs", snapshot: {} });
|
|
assert.equal(res.action, "override");
|
|
assert.equal(res.overrideSkillId, "survive.sleep", "night_shelter mapped to survive.sleep");
|
|
cleanup(tmp);
|
|
});
|