Files
pepa-pi-bot/runtime/stuck-incident.js
T
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

142 lines
5.0 KiB
JavaScript

// Stuck-incident detector. Today's failure-tracker in bot.js fires
// proposals on repeated *exception-class* failures (TypeError, timeout).
// This module fires on "no-progress" stagnation — the bot is healthy and
// the reflex loop hasn't crashed, but a single reason code (e.g.
// no_food_source, planner_empty) keeps coming back tick after tick.
//
// When the same reason persists past STUCK_THRESHOLD_MS we build a
// proposal body summarising the situation, including:
// - the no-progress reason
// - the active milestone + suggested skill
// - a slim snapshot
// - the last skill result
// - per-skill success/failure metrics (if available)
// - the allowed edit scope ("runtime/skills/<skill>.js" plus tests)
//
// The caller (bot.js) is responsible for actually writing the proposal —
// this module just produces the body string when the threshold trips.
const STUCK_THRESHOLD_MS = 5 * 60 * 1000;
const COOLDOWN_MS = 30 * 60 * 1000;
export function createStuckIncidentDetector({ thresholdMs = STUCK_THRESHOLD_MS, cooldownMs = COOLDOWN_MS } = {}) {
let currentReason = null;
let firstSeenAt = 0;
let lastFiredAt = 0;
function reset() {
currentReason = null;
firstSeenAt = 0;
}
// Returns { fire: true, body, kind, editScope } when an incident should
// be filed this tick; null otherwise.
function check({ snapshot, lastResult, metrics, journalSummary, scenarioTail, now = Date.now() }) {
const reason = snapshot?.noProgressReason ?? null;
if (!reason) {
reset();
return null;
}
if (reason !== currentReason) {
currentReason = reason;
firstSeenAt = now;
return null;
}
if (now - firstSeenAt < thresholdMs) return null;
if (lastFiredAt > 0 && now - lastFiredAt < cooldownMs) return null;
lastFiredAt = now;
const milestone = snapshot?.curriculum?.milestone;
const suggested = snapshot?.curriculum?.plan?.skillId;
const slim = {
runtimeState: snapshot.runtimeState,
noProgressReason: reason,
milestone: milestone?.title ?? null,
suggestedSkill: suggested ?? null,
position: snapshot.position,
health: snapshot.health,
food: snapshot.food,
isDay: snapshot.isDay,
inventoryCounts: Object.keys(snapshot.inventory ?? {}).length,
closestHostile: snapshot.closestHostile,
};
const editScope = suggested
? [`runtime/skills/${suggested.replace(/\./g, "-")}.js`, `runtime/skills/`]
: ["runtime/skills/", "runtime/"];
const metricsLine = metrics && Object.keys(metrics).length
? Object.entries(metrics)
.map(([id, m]) => `${id}: ok=${m.ok} fail=${m.fail}`)
.join(", ")
: "(none)";
const journalLine = journalSummary
? `byKind=${JSON.stringify(journalSummary.byKind ?? {})} buckets=${journalSummary.totalBuckets ?? 0}`
: "(no journal)";
const scenarioLines = Array.isArray(scenarioTail) && scenarioTail.length
? scenarioTail.map((e) =>
`- \`${e.skillId}\` ${e.ok ? "OK" : "FAIL"} code=${e.code ?? "?"} ${e.detail ? `(${e.detail.slice?.(0, 120) ?? ""})` : ""} situ=${e.situationHashShort ?? "?"}`,
).join("\n")
: "_(no scenario memory recorded yet)_";
const body = [
`# Stuck on \`${reason}\``,
"",
`The runtime has reported the same no-progress reason for >${Math.round(thresholdMs / 60000)} min without a productive action.`,
"",
"## Current state",
"",
"```json",
JSON.stringify(slim, null, 2),
"```",
"",
"## Last action result",
"",
lastResult
? `\`${lastResult.label}\` → ${lastResult.code ?? (lastResult.ok ? "ok" : "fail")}${lastResult.detail ? ` (${JSON.stringify(lastResult.detail).slice(0, 200)})` : ""}`
: "_(none recorded)_",
"",
"## Skill metrics so far (this process lifetime)",
"",
metricsLine,
"",
"## World journal (what we have discovered so far)",
"",
journalLine,
"",
"## Recent scenario memory (last attempts, what worked / failed in similar situations)",
"",
scenarioLines,
"",
"## Suggested fix",
"",
suggested
? `Improve \`${suggested}\` so the bot can clear the \`${reason}\` blocker, OR teach a NEW skill that handles this kind of situation if no single edit fixes it. Touch only the listed files (the test files under runtime/**/*.test.js are auto-allowed). Use the scenario-memory entries above to avoid re-introducing patterns that already failed.`
: `The curriculum has no suggested skill for this state. Either teach the curriculum a new milestone OR add a recovery skill that turns this reason code into a productive action. The scenario memory above shows what's been tried.`,
"",
"## Edit scope (auto-patch must obey this)",
"",
editScope.map((p) => `- ${p}`).join("\n"),
"",
"## Forbidden",
"",
"- Don't touch `.env`, `state/`, `extensions/`, `tui/` unless the scope above includes them.",
"- Don't add new npm dependencies.",
"- Don't change git history (no `--amend`, no `git reset --hard`).",
"",
].join("\n");
return {
fire: true,
kind: `stuck-${reason}`,
summary: `stuck on ${reason}${milestone ? ` (milestone: ${milestone.title})` : ""}`,
body,
editScope,
};
}
return { check, reset };
}