fix(runtime): unstick scheduler + chop + sleep + bed/shelter/farm skills
Recovers the bot from the live-server symptoms reported 2026-05-26: 1) constant supervisor reconnects, 2) chop "clicks once and stops", 3) sleep does nothing without a bed and so blocks night-skipping for other players, 4) curriculum reflex always fell through to wander. Supervisor (#38): - runtime/watch-filter.js: pure predicate excluding *.test.js + the supervisor itself; recursive:true so skills/ + social/ edits also restart. Burned a working main once when test files counted toward the rollback threshold. - runtime/supervisor.js: watch-triggered restarts no longer count toward the crash-loop rollback path. Watcher is now recursive. Chop / mine (#39): - runtime/actions.js + runtime/skills/gather-stone.js: replaced raw pathfinder.goto + bot.dig with mineflayer-collectblock's bot.collectBlock.collect — handles approach, repositioning, LoS, dig and pickup as one primitive. Old version "swung once" because GoalGetToBlock often parked the bot in leaves above the log. Sleep + bed (#40): - runtime/actions.js: sleepInBed now ALSO places a carried bed on solid ground next to the bot and sleeps on it. Critical so the bot stops blocking player night-skipping the moment it owns a bed. Bed pipeline (#41): - runtime/skills/gather-wool.js: gather.wool skill — mines wool block if any nearby, otherwise shears or attacks the nearest sheep. - runtime/skills/craft.js: craftBedSkill (any colour the bot has ≥3 wool of, plus 3 planks, plus a table). - runtime/curriculum.js: new milestone survive.bed sits between wood.tools and stone.32 so the bot gets a bed BEFORE everything else. Test fixture updated to include a red_bed in post-survive.bed stages. Village / shelter / wheat (#42, #43): - runtime/skills/build-shelter.js: village.build-shelter — real 3×3×3 resumable hut blueprint around the recorded base, places one block per loop, idempotent so an interrupted build resumes correctly, marks each placed block in the owned-blocks ledger. - runtime/skills/deposit-surplus.js: village.deposit-surplus opens the nearest chest and transfers surplus stacks while keeping a reserve of tools/food/bed. - runtime/skills/farm-wheat.js: farm.wheat does one step per call (till adjacent-to-water grass, plant seeds, or harvest ripe wheat). - runtime/curriculum.js: village.shelter milestone after base-site. Scheduler glitch (root of "always wander"): - runtime/bot.js: curriculum + locations are now computed BEFORE runTick. Previously they were stamped AFTER, so reflex.js saw snapshot.curriculum=undefined every tick and fell through to the wander fallback. Verified live: scheduler now dispatches gather.logs/gather.stone/craft.* by id via runSkill. Eat-spam: - runtime/reflex.js: eatReflex now checks inventory for actual food and updates lastEatAt on EVERY dispatch (not only successes), so a failed eat respects the 5 s cooldown instead of firing every tick. npm test 123/123. Validated live on play.xmatic.team (curriculum dispatched gather.logs via runSkill, recover hint switched to wander when no log in range). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+34
-25
@@ -17,6 +17,7 @@ import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { stateDir } from "./config.js";
|
||||
import { isWatchableJs } from "./watch-filter.js";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
@@ -124,32 +125,40 @@ function spawnChild() {
|
||||
child.on("exit", (code, signal) => {
|
||||
console.log(`[supervisor] child exited code=${code} signal=${signal}`);
|
||||
const wantsRestart = code === RELOAD_EXIT_CODE || restartingDueToWatch;
|
||||
// Capture BEFORE clearing — watch-triggered restarts are intentional
|
||||
// and must not be counted toward the crash-loop rollback threshold.
|
||||
const isWatchRestart = restartingDueToWatch;
|
||||
restartingDueToWatch = false;
|
||||
if (!wantsRestart) {
|
||||
// Clean exit (SIGINT/SIGTERM bubble) or crash — don't relaunch.
|
||||
process.exit(code ?? 0);
|
||||
}
|
||||
// Rate-limit restarts so a crash loop doesn't burn CPU.
|
||||
const now = nowMs();
|
||||
restartTimestamps.push(now);
|
||||
while (restartTimestamps.length && now - restartTimestamps[0] > 60_000) restartTimestamps.shift();
|
||||
if (restartTimestamps.length > MAX_RESTARTS_PER_MINUTE) {
|
||||
// Crash loop. If the last commit is young AND touched runtime/, it
|
||||
// probably broke us — roll it back and try once more.
|
||||
const ageMs = lastCommitAgeMs();
|
||||
if (
|
||||
ageMs < ROLLBACK_FRESHNESS_MS &&
|
||||
lastCommitTouchedRuntime() &&
|
||||
rollbackCount < MAX_ROLLBACKS &&
|
||||
rollbackLastCommit()
|
||||
) {
|
||||
console.log(`[supervisor] auto-rollback ${rollbackCount}/${MAX_ROLLBACKS} applied; restart counters reset`);
|
||||
restartTimestamps.length = 0;
|
||||
setTimeout(spawnChild, 500);
|
||||
return;
|
||||
// Rate-limit restarts so a crash loop doesn't burn CPU. Watch-triggered
|
||||
// restarts don't count — burned a working main once (2026-05-26) when
|
||||
// edits to runtime/*.test.js looked like a crash loop and rolled back
|
||||
// the scheduler PR.
|
||||
if (!isWatchRestart) {
|
||||
const now = nowMs();
|
||||
restartTimestamps.push(now);
|
||||
while (restartTimestamps.length && now - restartTimestamps[0] > 60_000) restartTimestamps.shift();
|
||||
if (restartTimestamps.length > MAX_RESTARTS_PER_MINUTE) {
|
||||
// Crash loop. If the last commit is young AND touched runtime/, it
|
||||
// probably broke us — roll it back and try once more.
|
||||
const ageMs = lastCommitAgeMs();
|
||||
if (
|
||||
ageMs < ROLLBACK_FRESHNESS_MS &&
|
||||
lastCommitTouchedRuntime() &&
|
||||
rollbackCount < MAX_ROLLBACKS &&
|
||||
rollbackLastCommit()
|
||||
) {
|
||||
console.log(`[supervisor] auto-rollback ${rollbackCount}/${MAX_ROLLBACKS} applied; restart counters reset`);
|
||||
restartTimestamps.length = 0;
|
||||
setTimeout(spawnChild, 500);
|
||||
return;
|
||||
}
|
||||
console.error(`[supervisor] too many restarts (${restartTimestamps.length} in 60s) — giving up`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.error(`[supervisor] too many restarts (${restartTimestamps.length} in 60s) — giving up`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`[supervisor] restarting in 500ms…`);
|
||||
setTimeout(spawnChild, 500);
|
||||
@@ -163,11 +172,11 @@ function spawnChild() {
|
||||
|
||||
let debounceTimer = null;
|
||||
function watchRuntime() {
|
||||
const watcher = fs.watch(RUNTIME_DIR, { recursive: false }, (eventType, filename) => {
|
||||
if (!filename || !filename.endsWith(".js")) return;
|
||||
// supervisor.js itself is excluded — restarting THIS process from
|
||||
// inside itself would require a separate exec, which we don't do.
|
||||
if (filename === "supervisor.js") return;
|
||||
// recursive:true so edits to runtime/skills/*.js and runtime/social/*.js
|
||||
// also restart the child. macOS + Linux support recursive fs.watch on
|
||||
// Node 20+.
|
||||
const watcher = fs.watch(RUNTIME_DIR, { recursive: true }, (eventType, filename) => {
|
||||
if (!isWatchableJs(filename)) return;
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
debounceTimer = setTimeout(() => {
|
||||
console.log(`[supervisor] ${filename} changed — restarting child`);
|
||||
|
||||
Reference in New Issue
Block a user