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>
This commit is contained in:
+71
-1
@@ -48,6 +48,8 @@ import { generateReply } from "./social/reply.js";
|
||||
import { createChatMemory } from "./social/memory.js";
|
||||
import { createStuckIncidentDetector } from "./stuck-incident.js";
|
||||
import { createSkillMetrics } from "./skill-metrics.js";
|
||||
import { createWorldJournal } from "./world-journal.js";
|
||||
import { createScenarioMemory, situationHash } from "./scenario-memory.js";
|
||||
|
||||
fs.mkdirSync(stateDir, { recursive: true });
|
||||
const JOINED_FLAG = path.join(stateDir, "joined-before.flag");
|
||||
@@ -74,6 +76,8 @@ let lastEscalationAt = 0;
|
||||
const noProgress = createNoProgressDetector();
|
||||
const stuckIncident = createStuckIncidentDetector();
|
||||
const skillMetrics = createSkillMetrics();
|
||||
const worldJournal = createWorldJournal();
|
||||
const scenarioMemory = createScenarioMemory();
|
||||
let lastResult = null; // { label, ok, code, detail, ts }
|
||||
let lastFailureAt = 0;
|
||||
let lastPlanReadAt = 0;
|
||||
@@ -82,6 +86,9 @@ let cachedPlanExists = false;
|
||||
const MILESTONE_CACHE_MS = 30_000;
|
||||
|
||||
// Reflex context — passed into reflex.js every tick. Mutable across ticks.
|
||||
// Memory stores (journal + memory) live here so skill code can consult
|
||||
// them directly — gather.* can preferentially target known log positions,
|
||||
// deposit-surplus can mark the chest it placed, etc.
|
||||
const reflexCtx = {
|
||||
bot: null,
|
||||
snapshot: lastSnapshot,
|
||||
@@ -93,6 +100,8 @@ const reflexCtx = {
|
||||
// Tracks repeated failure of the same labelled action — triggers a proposal.
|
||||
recentFailures: [], // [{label, detail, ts}], capped at 10
|
||||
dispatch: dispatchAction,
|
||||
journal: worldJournal,
|
||||
memory: scenarioMemory,
|
||||
};
|
||||
|
||||
let chatTimestamps = [];
|
||||
@@ -156,6 +165,45 @@ function maybeHandleAuthPrompt(text) {
|
||||
// Reflexes call this to fire an async action without blocking the tick.
|
||||
// Sets busy=true, runs fn, clears busy when done; optional onComplete callback
|
||||
// receives the action's { ok, detail } result.
|
||||
// Pull worldDelta fields written by skills (e.g. {choppedAt, logType,
|
||||
// minedAt, blockType, gotWool, baseAt, shelterAt, depositedTotal,
|
||||
// plantedAt, harvestedAt, tilledAt}) and turn them into journal lines.
|
||||
// Any unknown delta is silently skipped — skills can extend the world
|
||||
// journal without bot.js needing to know each schema.
|
||||
function recordWorldDeltaToJournal(label, res, snapshot) {
|
||||
const wd = res?.worldDelta;
|
||||
if (!wd || typeof wd !== "object") return;
|
||||
try {
|
||||
if (wd.choppedAt) worldJournal.append({ kind: "chopped", name: wd.logType ?? "log", at: wd.choppedAt });
|
||||
if (wd.minedAt) worldJournal.append({ kind: "chopped", name: wd.blockType ?? "stone", at: wd.minedAt });
|
||||
if (wd.placedAt) worldJournal.append({ kind: "placed", name: wd.placedType ?? "block", at: wd.placedAt });
|
||||
if (wd.baseAt) worldJournal.append({ kind: "base", name: "base", at: wd.baseAt });
|
||||
if (wd.shelterAt) worldJournal.append({ kind: "shelter", name: "shelter", at: wd.shelterAt });
|
||||
if (wd.plantedAt) worldJournal.append({ kind: "farm", name: "planted", at: wd.plantedAt });
|
||||
if (wd.harvestedAt) worldJournal.append({ kind: "farm", name: "harvested", at: wd.harvestedAt });
|
||||
if (wd.tilledAt) worldJournal.append({ kind: "farm", name: "tilled", at: wd.tilledAt });
|
||||
// failures: blacklisted / no_target — log a dead-end at current pos
|
||||
if (res?.code === "no_target" && snapshot?.position) {
|
||||
worldJournal.append({
|
||||
kind: "dead_end",
|
||||
name: label,
|
||||
reason: res?.detail ? String(res.detail).slice(0, 80) : "no_target",
|
||||
at: snapshot.position,
|
||||
});
|
||||
}
|
||||
if (res?.code === "silent_dig_failure" && snapshot?.position) {
|
||||
worldJournal.append({
|
||||
kind: "dead_end",
|
||||
name: label,
|
||||
reason: "silent_dig_failure",
|
||||
at: wd?.blacklisted ?? snapshot.position,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
warn("journal", `append from ${label} failed: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function dispatchAction(fn, label, opts = {}) {
|
||||
if (reflexCtx.busy) {
|
||||
warn("dispatch", `tried to dispatch ${label} while busy with ${reflexCtx.currentActionLabel}`);
|
||||
@@ -163,9 +211,14 @@ function dispatchAction(fn, label, opts = {}) {
|
||||
}
|
||||
reflexCtx.busy = true;
|
||||
reflexCtx.currentActionLabel = label;
|
||||
// Capture the situation hash BEFORE the action runs so a failure is
|
||||
// attributable to the state at dispatch time, not the state after the
|
||||
// (partial) effect.
|
||||
const startSnap = lastSnapshot;
|
||||
const startSituation = situationHash(startSnap);
|
||||
// current-task is a resume anchor — keep it small. Embedding the full
|
||||
// perception snapshot blows the file up to ~3 KB per write × every action.
|
||||
writeCurrentTask({ label, status: "in_progress", position: lastSnapshot.position });
|
||||
writeCurrentTask({ label, status: "in_progress", position: startSnap.position });
|
||||
info("dispatch", `→ ${label}`);
|
||||
Promise.resolve()
|
||||
.then(() => fn())
|
||||
@@ -184,6 +237,14 @@ function dispatchAction(fn, label, opts = {}) {
|
||||
ts: Date.now(),
|
||||
};
|
||||
skillMetrics.record(label, ok);
|
||||
scenarioMemory.record({
|
||||
skillId: label,
|
||||
situation: startSituation,
|
||||
code: lastResult.code,
|
||||
ok,
|
||||
detail: res?.detail,
|
||||
});
|
||||
recordWorldDeltaToJournal(label, res, startSnap);
|
||||
if (!ok) {
|
||||
lastFailureAt = lastResult.ts;
|
||||
recordFailure(label, res?.detail);
|
||||
@@ -210,6 +271,13 @@ function dispatchAction(fn, label, opts = {}) {
|
||||
ts: Date.now(),
|
||||
};
|
||||
skillMetrics.record(label, false);
|
||||
scenarioMemory.record({
|
||||
skillId: label,
|
||||
situation: startSituation,
|
||||
code: "threw",
|
||||
ok: false,
|
||||
detail: String(e?.message ?? e),
|
||||
});
|
||||
lastFailureAt = lastResult.ts;
|
||||
recordFailure(label, String(e?.message ?? e));
|
||||
})
|
||||
@@ -710,6 +778,8 @@ function tick() {
|
||||
snapshot: lastSnapshot,
|
||||
lastResult,
|
||||
metrics: lastSnapshot.skillMetrics,
|
||||
journalSummary: worldJournal.summary(),
|
||||
scenarioTail: scenarioMemory.recentTailFor({ n: 12 }),
|
||||
now,
|
||||
});
|
||||
if (stuck?.fire) {
|
||||
|
||||
Reference in New Issue
Block a user