Files
pepa-pi-bot/runtime/scenario-memory.test.js
mayatnikovandClaude Opus 4.7 d960db4819 feat(runtime): persistent memory — world-journal + scenario-memory
Closes a structural gap: the bot now actually REMEMBERS what it
discovered and what it tried. Two stores live under state/<host>/ and
are wired in automatically.

runtime/world-journal.js
- Append-only JSONL of discovered points (chopped, placed, base,
  shelter, farm, dead_end). Indexed by 16-block spatial grid; O(neighbors)
  nearest() lookups; 6 h age prune; 10k line ceiling with trim.
- leanestQuadrant({x,z}) reports the quadrant the bot has the FEWEST
  markers in — used by explore.far to circle rather than retread.
- summary() exposed for the stuck-incident proposal body.

runtime/scenario-memory.js
- Sliding window of (skillId, situationHash, code, ok, detail) tuples.
- situationHash() is a coarse fingerprint (16x8x16 cell + day/night +
  food/hp bucket + inv key set + closest hostile). So "same kind of
  place + same kind of state" matches.
- shouldSkip({skillId, situation}) → true after ≥3 failures within 30
  min UNLESS a more-recent success in the same situation un-locks it.
- recentTailFor() exposed for the stuck-incident body.

Wiring (runtime/bot.js):
- dispatchAction captures situationHash BEFORE the action runs and
  records (skillId, situation, code, ok) after — failures are attributed
  to the dispatch-time state, not the partial-effect state.
- worldDelta fields (choppedAt, minedAt, placedAt, baseAt, shelterAt,
  plantedAt, harvestedAt, tilledAt) auto-flow into the journal.
- no_target + silent_dig_failure also write dead_end markers.

Scheduler / skills now consume memory:
- reflex.js curriculum reflex calls memory.shouldSkip — if the same
  (skill, situation) failed 3+ times recently, auto-converts to a
  wander hint so the bot leaves and tries elsewhere.
- explore.far calls journal.leanestQuadrant when multiple cardinal
  directions are walkable and prefers the less-explored one.
- gather.logs walks to the nearest known "chopped" bucket within 96
  blocks before falling through to findBlock — chunks with confirmed
  trees are more likely to yield another.

stuck-incident body now includes journal byKind + last 12 scenario
entries so Pi can write a structural fix, not just a guard clause.

Architecturally: this is the foundation for "bot rewrites itself".
The proposals Pi now receives carry real signal about what was tried
and what's around, instead of a single snapshot in isolation.

10 new tests (world-journal × 5, scenario-memory × 5). npm test 134/134.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 12:20:03 +03:00

70 lines
2.6 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createScenarioMemory, situationHash } from "./scenario-memory.js";
function snap(extras = {}) {
return {
connected: true,
position: { x: 100, y: 64, z: -200 },
isDay: true,
food: 18,
health: 20,
inventory: {},
closestHostile: null,
...extras,
};
}
test("situationHash is stable for the same situation, different for different cell", () => {
const a = situationHash(snap());
const b = situationHash(snap());
assert.equal(a, b);
const c = situationHash(snap({ position: { x: 200, y: 64, z: -200 } }));
assert.notEqual(a, c);
});
test("situationHash changes with day/night, food, health bucket, hostile", () => {
const base = snap();
const night = situationHash({ ...base, isDay: false });
const lowHp = situationHash({ ...base, health: 5 });
const hostile = situationHash({ ...base, closestHostile: { name: "zombie", distance: 5 } });
assert.notEqual(situationHash(base), night);
assert.notEqual(situationHash(base), lowHp);
assert.notEqual(situationHash(base), hostile);
});
test("shouldSkip flips after N failures in the same situation", () => {
const m = createScenarioMemory({ failureThreshold: 3, windowMs: 60_000 });
const sit = "x|y|z|d|F|H|host:-|inv:-";
const skillId = "test.always-fails-" + Date.now();
assert.equal(m.shouldSkip({ skillId, situation: sit }), false);
for (let i = 0; i < 3; i++) {
m.record({ skillId, situation: sit, code: "no_target", ok: false, detail: "no" });
}
assert.equal(m.shouldSkip({ skillId, situation: sit }), true);
});
test("a recent success in the same situation un-locks the skill", () => {
const m = createScenarioMemory({ failureThreshold: 2, windowMs: 60_000 });
const sit = "site_alpha";
const skillId = "test.flaky-" + Date.now();
m.record({ skillId, situation: sit, code: "fail", ok: false });
m.record({ skillId, situation: sit, code: "fail", ok: false });
assert.equal(m.shouldSkip({ skillId, situation: sit }), true);
m.record({ skillId, situation: sit, code: "done", ok: true });
assert.equal(m.shouldSkip({ skillId, situation: sit }), false);
});
test("recentTailFor returns most-recent entries with situation hash short form", () => {
const m = createScenarioMemory();
const skillId = "test.tail-" + Date.now();
for (let i = 0; i < 5; i++) {
m.record({ skillId, situation: `s${i}`, code: i % 2 ? "ok" : "no_target", ok: i % 2 === 1 });
}
const tail = m.recentTailFor({ skillId, n: 3 });
assert.equal(tail.length, 3);
assert.equal(tail[2].skillId, skillId);
assert.ok(typeof tail[0].situationHashShort === "string");
});