Files
ea4f16a0da feat(runtime): scheduler-via-runSkill + Pi banter escalation + base-site (follow-ups) (#20)
Three closures of remaining PRD follow-ups, one merge:

1. Reflex scheduler now drives behaviour from the curriculum.
   - reflex.js: replaced ad-hoc techTreeReflex + autonomousReflex with
     curriculumReflex that dispatches the skill suggested by
     snapshot.curriculum.plan via runSkill. Per-skill backoff for
     missing_tool / missing_material / no_target / no_food_source /
     unsupported_version. recover() hint with `{hint:"wander"}` swaps
     the next tick to wander for 60 s.
   - Chain is now: defend > eat > sleep > curriculum > idle.
   - reflex.test.js: 11 new tests covering busy/disconnected,
     defend/eat preemption, dispatch by id, unknown-skill fallback,
     per-skill + wander-hint backoffs, onComplete updating backoff.

2. Pi escalation for ADDRESSED_BANTER with hard rate limit.
   - bot.js: when generateReply returns {escalate:true}, spawn askPi
     with bot state + last 5 lines from that speaker (redacted via
     chatMemory). Reply capped at 200 chars, sent as one chat line.
   - Rate cap: 6 calls/hour, 90 s min gap. Suppressed escalations
     log once and silently drop.

3. Phase 4 substrate.
   - runtime/locations.js: atomic JSON store
     (state/<host>/locations.json) with setLocation / getLocation /
     nearestLocation / removeLocation; 6 tests.
   - runtime/base-site.js: scoreCurrentPosition(bot) + pure scoreSite
     bundle (wood / stone / water / flatness / no-players /
     no-foreign-builds, owned-blocks excluded from claim penalty);
     6 tests.
   - runtime/skills/choose-base.js: village.choose-base skill — scores
     the current spot, writes locations.base if score ≥ 8, otherwise
     returns code:"too_weak" with a wander recover hint.
   - curriculum.js: new final milestone village.base-site fires
     village.choose-base until a base location exists.
   - bot.js: stamps snapshot.locations from listLocations() each tick
     so the curriculum can read it without coupling to disk.

docs/runtime.md updated with three new sections.
npm test now 116/116.

Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 10:46:14 +03:00

94 lines
2.4 KiB
JavaScript

// Named locations persisted under state/<host>/locations.json. The bot
// records places it cares about: "base", "wood-spot", "stone-spot",
// "wheat-farm", "chest-1". The format is a flat dict keyed by name; the
// value carries the integer block coordinates, an optional radius (for
// "the wood-spot is somewhere in this 16-block square"), the dimension
// and a free-form note for diary readability.
//
// All writes are sync — the file is small (a few dozen entries at most).
// We write atomically (tmp + rename) so a crash mid-write doesn't leave
// a half-written JSON.
import fs from "node:fs";
import path from "node:path";
import { stateDir } from "./config.js";
const LOCATIONS_PATH = path.join(stateDir, "locations.json");
function ensureDir() {
try { fs.mkdirSync(stateDir, { recursive: true }); } catch {}
}
function loadAll() {
try {
const raw = fs.readFileSync(LOCATIONS_PATH, "utf8").trim();
if (!raw) return {};
const parsed = JSON.parse(raw);
return parsed && typeof parsed === "object" ? parsed : {};
} catch (e) {
if (e.code === "ENOENT") return {};
return {};
}
}
function saveAll(map) {
ensureDir();
const tmp = `${LOCATIONS_PATH}.tmp`;
fs.writeFileSync(tmp, JSON.stringify(map, null, 2));
fs.renameSync(tmp, LOCATIONS_PATH);
}
export function listLocations() {
return loadAll();
}
export function getLocation(name) {
const all = loadAll();
return all[name] ?? null;
}
export function setLocation(name, {
x, y, z,
dimension = "overworld",
radius = 0,
note = "",
}) {
if (!name) throw new Error("setLocation: name required");
if (typeof x !== "number" || typeof y !== "number" || typeof z !== "number") {
throw new Error("setLocation: x/y/z must be numbers");
}
const all = loadAll();
all[name] = {
x: Math.round(x),
y: Math.round(y),
z: Math.round(z),
dimension,
radius,
note,
ts: new Date().toISOString(),
};
saveAll(all);
return all[name];
}
export function removeLocation(name) {
const all = loadAll();
if (!(name in all)) return false;
delete all[name];
saveAll(all);
return true;
}
// Find the named location closest to (x,y,z) — useful for "go back to
// base" when there are multiple shelters.
export function nearestLocation({ x, z }) {
const all = loadAll();
let best = null;
for (const [name, loc] of Object.entries(all)) {
if (typeof loc?.x !== "number") continue;
const d = Math.hypot(loc.x - x, loc.z - z);
if (!best || d < best.d) best = { name, loc, d };
}
return best;
}