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>
59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { createWorldJournal, _internal } from "./world-journal.js";
|
|
|
|
function tag() { return `__t_${Date.now()}_${Math.floor(Math.random() * 1e6)}`; }
|
|
|
|
test("cellOf buckets by GRID_CELL", () => {
|
|
assert.equal(_internal.cellOf(0, 0), "0,0");
|
|
assert.equal(_internal.cellOf(15, 15), "0,0");
|
|
assert.equal(_internal.cellOf(16, 16), "1,1");
|
|
assert.equal(_internal.cellOf(-1, -1), "-1,-1");
|
|
});
|
|
|
|
test("append + nearest returns the entry we just stored", () => {
|
|
const j = createWorldJournal();
|
|
const name = tag();
|
|
j.append({ kind: "chopped", name, at: { x: 100, y: 64, z: 200 } });
|
|
const got = j.nearest({ kind: "chopped", x: 100, z: 200, radius: 16, limit: 5 });
|
|
const ours = got.find((e) => e.name === name);
|
|
assert.ok(ours, "expected to find our own entry back");
|
|
assert.equal(ours.at.x, 100);
|
|
});
|
|
|
|
test("nearest ranks by distance and respects radius", () => {
|
|
const j = createWorldJournal();
|
|
const t = tag();
|
|
j.append({ kind: "stone", name: t + "_far", at: { x: 100, y: 64, z: 100 } });
|
|
j.append({ kind: "stone", name: t + "_near", at: { x: 5, y: 64, z: 5 } });
|
|
const got = j.nearest({ kind: "stone", x: 0, z: 0, radius: 50, limit: 5 });
|
|
const near = got.find((e) => e.name === t + "_near");
|
|
const far = got.find((e) => e.name === t + "_far");
|
|
assert.ok(near);
|
|
assert.equal(far, undefined, "far entry > radius should be excluded");
|
|
});
|
|
|
|
test("leanestQuadrant returns the quadrant with fewest entries", () => {
|
|
const j = createWorldJournal();
|
|
const t = tag();
|
|
for (let i = 0; i < 5; i++) {
|
|
j.append({ kind: "dead_end", name: t, at: { x: 10 + i, y: 64, z: -10 - i } }); // NE
|
|
}
|
|
for (let i = 0; i < 2; i++) {
|
|
j.append({ kind: "dead_end", name: t, at: { x: 10 + i, y: 64, z: 10 + i } }); // SE
|
|
}
|
|
const { best, counts } = j.leanestQuadrant({ x: 0, z: 0, radius: 64 });
|
|
assert.ok(["SW", "NW"].includes(best), `expected unused quadrant, got ${best} counts=${JSON.stringify(counts)}`);
|
|
});
|
|
|
|
test("summary lists per-kind totals", () => {
|
|
const j = createWorldJournal();
|
|
j.append({ kind: "chopped", name: "oak_log", at: { x: 1, y: 1, z: 1 } });
|
|
j.append({ kind: "chopped", name: "oak_log", at: { x: 2, y: 1, z: 2 } });
|
|
j.append({ kind: "shelter", name: "shelter", at: { x: 0, y: 1, z: 0 } });
|
|
const s = j.summary();
|
|
assert.ok(s.byKind.chopped >= 2);
|
|
assert.ok(s.byKind.shelter >= 1);
|
|
});
|