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

79 lines
2.1 KiB
JavaScript

// locations.js writes to the real state/<host>/ on disk via the
// project's config.stateDir, so these tests run end-to-end against the
// active dev state dir. We pick obviously-fake location names with a
// timestamp suffix and clean up after ourselves so we never leave junk
// in the real state.
import { test } from "node:test";
import assert from "node:assert/strict";
import { setLocation, getLocation, listLocations, removeLocation, nearestLocation } from "./locations.js";
function tag() {
return `__test_${Date.now()}_${Math.floor(Math.random() * 1e6)}`;
}
test("setLocation + getLocation round-trip", () => {
const name = tag();
try {
const stored = setLocation(name, { x: 100, y: 64, z: -200, note: "smoke" });
assert.equal(stored.x, 100);
assert.equal(stored.note, "smoke");
const got = getLocation(name);
assert.equal(got.x, 100);
assert.equal(got.z, -200);
} finally {
removeLocation(name);
}
});
test("setLocation rounds floats", () => {
const name = tag();
try {
const stored = setLocation(name, { x: 100.4, y: 64.7, z: -200.5 });
assert.equal(stored.x, 100);
assert.equal(stored.y, 65);
assert.equal(stored.z, -200);
} finally {
removeLocation(name);
}
});
test("setLocation rejects missing coords", () => {
assert.throws(() => setLocation("bad", { x: 1, y: 2 }), /x\/y\/z/);
assert.throws(() => setLocation(null, { x: 0, y: 0, z: 0 }), /name required/);
});
test("removeLocation returns false when absent", () => {
assert.equal(removeLocation("__definitely_not_set"), false);
});
test("listLocations includes everything we put in", () => {
const a = tag();
const b = tag();
try {
setLocation(a, { x: 1, y: 64, z: 1 });
setLocation(b, { x: 2, y: 64, z: 2 });
const all = listLocations();
assert.ok(all[a]);
assert.ok(all[b]);
} finally {
removeLocation(a);
removeLocation(b);
}
});
test("nearestLocation picks the closer of two", () => {
const a = tag();
const b = tag();
try {
setLocation(a, { x: 0, y: 64, z: 0 });
setLocation(b, { x: 100, y: 64, z: 100 });
const near = nearestLocation({ x: 5, z: 5 });
assert.equal(near.name, a);
} finally {
removeLocation(a);
removeLocation(b);
}
});