feat(runtime): early-game survival curriculum + stone/craft skills (Phase 3)

Phase 3 of plans/autonomous-survival-bot-prd.md. Gives the bot a
deterministic path from empty inventory through stone-tier tools and
basic storage, without an LLM call per tick.

New:
- runtime/curriculum.js: ordered milestone chooser
  (wood.16 → wood.planks-and-sticks → wood.tools → stone.32 →
   stone.tools → food.basic → storage.chest → shelter.torch). Each
  milestone exposes isDone(inventory, snapshot) and suggest() returning
  a { skillId } plan the scheduler can dispatch via runSkill. isDone
  uses "stage reached" escapes so progress is monotonic — crafting
  planks doesn't bounce the chooser back to "gather 16 logs".
- runtime/skills/gather-stone.js: gather.stone with pickaxe-required
  precondition, blacklist on failed paths, registry-aware matching
  (stone / cobblestone / deepslate / cobbled_deepslate / andesite /
  diorite / granite).
- runtime/skills/craft.js: factory + concrete skills for craft.planks,
  craft.sticks, craft.wooden-axe/-pickaxe/-sword, craft.stone-axe/
  -pickaxe/-sword, craft.furnace, craft.chest, craft.torch (torch
  requires coal or charcoal preflight).

Tests:
- runtime/curriculum.test.js: 14 tests covering chooser ordering,
  per-milestone skill suggestion, inventoryFull threshold, monotonic
  advancement across stage transitions.
- npm test now runs the full suite: 28/28 passing.

Wiring:
- runtime/bot.js: lastSnapshot.curriculum carries the next milestone
  + suggested skill on every tick; lastSnapshot.currentMilestone
  prefers the curriculum title over the planner.md line.
- tui/tui.tsx: milestone line shows the curriculum's suggested skill
  and an [inventory full] flag when isInventoryFull fires.

Reflex.js still calls actions.js directly; wiring the scheduler to
runSkill(plan.skillId, …) lands in Phase 4.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 22:23:05 +03:00
co-authored by Claude Opus 4.7
parent 4b7541435d
commit 1bdc7465ac
9 changed files with 785 additions and 3 deletions
+267
View File
@@ -0,0 +1,267 @@
// craft.* — thin skill wrappers around the existing actions.js crafting
// primitives. Each one is generated by makeCraftSkill so adding a new
// craftable is one entry in this file rather than a new module.
//
// All wood/stone crafts that need a workbench reuse actions.placeCraftingTable
// internally: the skill's preconditions guarantee the raw materials, then
// execute() defers to the actions.js implementation that handles
// table-placement, recipe lookup and bot.craft().
import {
craftPlanks,
craftSticks,
craftWoodenAxe,
craftWoodenPickaxe,
craftWoodenSword,
placeCraftingTable,
inv as invHelpers,
} from "../actions.js";
function totalPlanks(bot) {
return invHelpers.getAnyPlanksCount(bot);
}
function totalSticks(bot) {
return invHelpers.getItemCount(bot, "stick");
}
function totalLogs(bot) {
return invHelpers.getAnyLogCount(bot);
}
function count(bot, name) {
return invHelpers.getItemCount(bot, name);
}
function withTimeout(promise, ms, label) {
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms / 1000}s`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
function asResult(res, doneCode = "done") {
if (res?.ok) {
return { ok: true, code: doneCode, detail: res.detail, worldDelta: { crafted: res.detail?.item ?? doneCode } };
}
const msg = String(res?.detail ?? "");
const code = msg.includes("no recipe")
? "no_recipe"
: msg.includes("timed out")
? "timeout"
: "failed";
return { ok: false, code, detail: res?.detail, worldDelta: null };
}
// --- direct wrappers around existing actions ---------------------------------
export const craftPlanksSkill = Object.freeze({
id: "craft.planks",
title: "Craft planks from logs",
timeoutMs: 20_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalLogs(ctx.bot) < 1) return { ok: false, code: "missing_material", detail: "no log in inventory" };
return { ok: true };
},
async execute(ctx, args = {}) {
return asResult(await craftPlanks(ctx.bot, args.count ?? 4));
},
});
export const craftSticksSkill = Object.freeze({
id: "craft.sticks",
title: "Craft sticks from planks",
timeoutMs: 20_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalPlanks(ctx.bot) < 2) return { ok: false, code: "missing_material", detail: "need 2 planks" };
return { ok: true };
},
async execute(ctx, args = {}) {
return asResult(await craftSticks(ctx.bot, args.count ?? 4));
},
});
export const craftWoodenAxeSkill = Object.freeze({
id: "craft.wooden-axe",
title: "Craft wooden axe",
timeoutMs: 30_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalPlanks(ctx.bot) < 3) return { ok: false, code: "missing_material", detail: "need 3 planks" };
if (totalSticks(ctx.bot) < 2) return { ok: false, code: "missing_material", detail: "need 2 sticks" };
return { ok: true };
},
async execute(ctx) {
return asResult(await craftWoodenAxe(ctx.bot));
},
});
export const craftWoodenPickaxeSkill = Object.freeze({
id: "craft.wooden-pickaxe",
title: "Craft wooden pickaxe",
timeoutMs: 30_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalPlanks(ctx.bot) < 3) return { ok: false, code: "missing_material", detail: "need 3 planks" };
if (totalSticks(ctx.bot) < 2) return { ok: false, code: "missing_material", detail: "need 2 sticks" };
return { ok: true };
},
async execute(ctx) {
return asResult(await craftWoodenPickaxe(ctx.bot));
},
});
export const craftWoodenSwordSkill = Object.freeze({
id: "craft.wooden-sword",
title: "Craft wooden sword",
timeoutMs: 30_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalPlanks(ctx.bot) < 2) return { ok: false, code: "missing_material", detail: "need 2 planks" };
if (totalSticks(ctx.bot) < 1) return { ok: false, code: "missing_material", detail: "need 1 stick" };
return { ok: true };
},
async execute(ctx) {
return asResult(await craftWoodenSword(ctx.bot));
},
});
// --- generic crafting (stone tools, furnace, chest, torch) -------------------
// Stone-tier and storage crafts share the same lookup-recipe-then-call-bot.craft
// shape as the wooden tools, but with different precondition counts and item
// names. We define a tiny factory.
//
// `requires` is a map from item name to required count. `needsTable: true`
// means we place / reuse a crafting table first (via actions.placeCraftingTable).
function makeRecipeSkill({ id, title, item, requires, needsTable, doneCode = "done" }) {
return Object.freeze({
id,
title,
timeoutMs: 30_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
for (const [name, n] of Object.entries(requires)) {
const have = name === "planks"
? totalPlanks(ctx.bot)
: count(ctx.bot, name);
if (have < n) {
return { ok: false, code: "missing_material", detail: `need ${n} ${name} (have ${have})` };
}
}
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
let tableBlock = null;
if (needsTable) {
const tableRes = await placeCraftingTable(bot);
if (!tableRes.ok) {
const msg = String(tableRes.detail ?? "");
const code = msg.includes("timed out") ? "timeout" : "missing_table";
return { ok: false, code, detail: tableRes.detail, worldDelta: null };
}
tableBlock = tableRes.block;
}
const reg = bot?.registry;
const itemId = reg?.itemsByName?.[item]?.id;
if (itemId == null) {
return { ok: false, code: "unsupported_version", detail: `unknown item ${item}`, worldDelta: null };
}
const recipes = bot.recipesFor(itemId, null, 1, tableBlock);
const recipe = recipes[0];
if (!recipe) {
return { ok: false, code: "no_recipe", detail: `no recipe for ${item}`, worldDelta: null };
}
try {
await withTimeout(bot.craft(recipe, 1, tableBlock), 15_000, `craft(${item})`);
return { ok: true, code: doneCode, detail: { item }, worldDelta: { crafted: item } };
} catch (e) {
const msg = String(e?.message ?? "");
const code = msg.includes("timed out") ? "timeout" : "failed";
return { ok: false, code, detail: e.message, worldDelta: null };
}
},
validate(ctx, result) {
return result.ok && !!result.worldDelta?.crafted;
},
});
}
export const craftStoneAxeSkill = makeRecipeSkill({
id: "craft.stone-axe",
title: "Craft stone axe",
item: "stone_axe",
requires: { cobblestone: 3, stick: 2 },
needsTable: true,
});
export const craftStonePickaxeSkill = makeRecipeSkill({
id: "craft.stone-pickaxe",
title: "Craft stone pickaxe",
item: "stone_pickaxe",
requires: { cobblestone: 3, stick: 2 },
needsTable: true,
});
export const craftStoneSwordSkill = makeRecipeSkill({
id: "craft.stone-sword",
title: "Craft stone sword",
item: "stone_sword",
requires: { cobblestone: 2, stick: 1 },
needsTable: true,
});
export const craftFurnaceSkill = makeRecipeSkill({
id: "craft.furnace",
title: "Craft furnace",
item: "furnace",
requires: { cobblestone: 8 },
needsTable: true,
});
export const craftChestSkill = makeRecipeSkill({
id: "craft.chest",
title: "Craft chest",
item: "chest",
requires: { planks: 8 },
needsTable: true,
});
// Torch: 1 stick + 1 coal (or charcoal) → 4 torches. We accept either
// fuel via precondition shortcut: if no coal AND no charcoal, fail with
// missing_material so the curriculum surfaces the blocker rather than
// silently fail at recipe lookup.
export const craftTorchSkill = Object.freeze({
id: "craft.torch",
title: "Craft torches",
timeoutMs: 20_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (totalSticks(ctx.bot) < 1) return { ok: false, code: "missing_material", detail: "need 1 stick" };
if (count(ctx.bot, "coal") < 1 && count(ctx.bot, "charcoal") < 1) {
return { ok: false, code: "missing_material", detail: "need 1 coal or charcoal" };
}
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
const reg = bot?.registry;
const itemId = reg?.itemsByName?.["torch"]?.id;
if (itemId == null) {
return { ok: false, code: "unsupported_version", detail: "no torch in registry", worldDelta: null };
}
const recipes = bot.recipesFor(itemId, null, 1, null);
const recipe = recipes[0];
if (!recipe) {
return { ok: false, code: "no_recipe", detail: "no recipe for torch (need coal+stick)", worldDelta: null };
}
try {
await withTimeout(bot.craft(recipe, 1, null), 15_000, "craft(torch)");
return { ok: true, code: "done", detail: { item: "torch" }, worldDelta: { crafted: "torch" } };
} catch (e) {
return { ok: false, code: "failed", detail: e.message, worldDelta: null };
}
},
});
+128
View File
@@ -0,0 +1,128 @@
// gather.stone — find a nearby stone/cobble/deepslate block, equip a
// pickaxe (best available), path to it and mine it. Stone-tier mining
// needs at least a wooden pickaxe — the preconditions enforce that.
import pathfinderPkg from "mineflayer-pathfinder";
const { pathfinder, goals, Movements } = pathfinderPkg;
import { pickaxes } from "./groups.js";
import { info, warn } from "../log.js";
const STONE_NAMES = ["stone", "cobblestone", "deepslate", "cobbled_deepslate", "andesite", "diorite", "granite"];
let pluginLoaded = new WeakSet();
function ensurePathfinder(bot) {
if (pluginLoaded.has(bot)) return;
bot.loadPlugin(pathfinder);
pluginLoaded.add(bot);
}
function setMovementsForGather(bot) {
const m = new Movements(bot);
m.canDig = true;
m.allow1by1towers = false;
bot.pathfinder.setMovements(m);
}
const PICKAXE_PRIORITY = ["netherite_pickaxe", "diamond_pickaxe", "iron_pickaxe", "stone_pickaxe", "wooden_pickaxe"];
async function equipBestPickaxe(bot) {
for (const name of PICKAXE_PRIORITY) {
const item = bot.inventory.items().find((i) => i.name === name);
if (item) {
try {
await bot.equip(item, "hand");
return name;
} catch {}
}
}
return null;
}
function withTimeout(promise, ms, label) {
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms / 1000}s`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
const stoneBlacklist = new WeakMap();
const BLACKLIST_TTL_MS = 5 * 60_000;
function getBlacklist(bot) {
let m = stoneBlacklist.get(bot);
if (!m) {
m = new Map();
stoneBlacklist.set(bot, m);
}
const now = Date.now();
for (const [k, exp] of m) if (exp < now) m.delete(k);
return m;
}
export const skill = Object.freeze({
id: "gather.stone",
title: "Gather cobblestone",
timeoutMs: 90_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
const available = pickaxes(ctx.bot);
if (available.size === 0) {
return { ok: false, code: "unsupported_version", detail: "no pickaxes in registry" };
}
const owned = ctx.bot.inventory.items().some((i) => available.has(i.name));
if (!owned) {
return { ok: false, code: "missing_tool", detail: "no pickaxe in inventory" };
}
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
const blacklist = getBlacklist(bot);
const target = bot.findBlock({
matching: (b) => {
if (!b || !b.position || !STONE_NAMES.includes(b.name)) return false;
const key = `${b.position.x},${b.position.y},${b.position.z}`;
return !blacklist.has(key);
},
maxDistance: 32,
});
if (!target) {
return { ok: false, code: "no_target", detail: "no reachable stone within 32 blocks", worldDelta: null };
}
ensurePathfinder(bot);
setMovementsForGather(bot);
const pickaxe = await equipBestPickaxe(bot);
info("action", `gather.stone: ${target.name} at ${target.position.x},${target.position.y},${target.position.z} (tool=${pickaxe ?? "fists"})`);
try {
await withTimeout(
bot.pathfinder.goto(new goals.GoalGetToBlock(target.position.x, target.position.y, target.position.z)),
45_000,
"pathToStone",
);
await withTimeout(bot.dig(target), 30_000, "digStone");
await new Promise((r) => setTimeout(r, 1200));
return {
ok: true,
code: "done",
detail: { blockType: target.name, at: target.position },
worldDelta: { minedAt: target.position, blockType: target.name },
};
} catch (e) {
warn("action", `gather.stone failed: ${e.message}`);
const key = `${target.position.x},${target.position.y},${target.position.z}`;
blacklist.set(key, Date.now() + BLACKLIST_TTL_MS);
const msg = String(e?.message ?? "");
const code = msg.includes("timed out") ? "timeout" : "failed";
return { ok: false, code, detail: e.message, worldDelta: null };
}
},
validate(ctx, result) {
return result.ok && !!result.worldDelta?.blockType;
},
recover(ctx, result) {
if (result.code === "no_target") return { hint: "wander", reason: "no stone within 32 blocks" };
return null;
},
});
+26
View File
@@ -25,6 +25,20 @@ import { info, warn } from "../log.js";
import { skill as chopLogs } from "./chop-logs.js";
import { skill as eat } from "./eat.js";
import { skill as wander } from "./wander.js";
import { skill as gatherStone } from "./gather-stone.js";
import {
craftPlanksSkill,
craftSticksSkill,
craftWoodenAxeSkill,
craftWoodenPickaxeSkill,
craftWoodenSwordSkill,
craftStoneAxeSkill,
craftStonePickaxeSkill,
craftStoneSwordSkill,
craftFurnaceSkill,
craftChestSkill,
craftTorchSkill,
} from "./craft.js";
const SKILLS = new Map();
@@ -40,6 +54,18 @@ function register(skill) {
register(chopLogs);
register(eat);
register(wander);
register(gatherStone);
register(craftPlanksSkill);
register(craftSticksSkill);
register(craftWoodenAxeSkill);
register(craftWoodenPickaxeSkill);
register(craftWoodenSwordSkill);
register(craftStoneAxeSkill);
register(craftStonePickaxeSkill);
register(craftStoneSwordSkill);
register(craftFurnaceSkill);
register(craftChestSkill);
register(craftTorchSkill);
export function listSkills() {
return Array.from(SKILLS.values()).map((s) => ({