Files
pepa-pi-bot/runtime/curriculum.js
T
ae7b4d89cb feat(runtime): early-game survival curriculum + stone/craft skills (Phase 3) (#15)
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: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 22:23:33 +03:00

166 lines
5.7 KiB
JavaScript

// Deterministic early-game survival curriculum. Mirrors PRD §6 FR4:
// the bot should be able to progress from empty inventory to wooden tools,
// to a stone tier + furnace + chest, without an LLM call per tick.
//
// Each milestone exposes:
// id - stable string, used for diary/snapshot
// title - human label, used by TUI
// isDone - (inventoryCounts, snapshot) => boolean
// suggest - (inventoryCounts, snapshot) => skill plan
// where a plan is { skillId, args? } or null if the
// milestone requires a precondition the scheduler must
// set up via an earlier milestone.
//
// The chooser walks milestones in order and returns the first one whose
// `isDone` returns false. That milestone is "current". Its `suggest()`
// tells the scheduler what skill to dispatch right now.
//
// The curriculum is pure: it never touches the bot, the world or the
// state-store. The scheduler is responsible for dispatch, retries and
// no-progress handling.
const INVENTORY_FULL_DISTINCT_STACKS = 32;
function totalLogs(inv) {
return Object.entries(inv ?? {})
.filter(([k]) => k.endsWith("_log") || k.endsWith("_stem"))
.reduce((sum, [, n]) => sum + n, 0);
}
function totalPlanks(inv) {
return Object.entries(inv ?? {})
.filter(([k]) => k.endsWith("_planks"))
.reduce((sum, [, n]) => sum + n, 0);
}
function totalCobble(inv) {
return (inv?.cobblestone ?? 0) + (inv?.cobbled_deepslate ?? 0);
}
function has(inv, name, n = 1) {
return (inv?.[name] ?? 0) >= n;
}
function hasAny(inv, names) {
return names.some((n) => (inv?.[n] ?? 0) > 0);
}
const WOODEN_TOOLS = ["wooden_axe", "wooden_pickaxe", "wooden_sword"];
const STONE_TOOLS = ["stone_axe", "stone_pickaxe", "stone_sword"];
// A "stage reached" predicate: once the bot has wooden tools, wood.16 is
// implicitly considered done even if the log stack is now empty (the bot
// burned through them to craft planks/sticks/tools). Without this, the
// curriculum oscillates: chop → craft → "oh, I have no logs again, go
// chop". Each milestone keeps its raw-resource predicate AND an
// "advanced past this tier" escape so the chooser monotonically advances.
function hasWoodenTier(inv) {
return WOODEN_TOOLS.some((n) => has(inv, n));
}
function hasStoneTier(inv) {
return STONE_TOOLS.some((n) => has(inv, n));
}
const MILESTONES = [
{
id: "wood.16",
title: "Gather 16 logs",
isDone: (inv) => totalLogs(inv) >= 16 || hasWoodenTier(inv),
suggest: () => ({ skillId: "gather.logs" }),
},
{
id: "wood.planks-and-sticks",
title: "Craft 4 planks and 4 sticks",
isDone: (inv) =>
(totalPlanks(inv) >= 4 && has(inv, "stick", 4)) || hasWoodenTier(inv),
suggest: (inv) => {
if (totalPlanks(inv) < 4) return { skillId: "craft.planks" };
return { skillId: "craft.sticks" };
},
},
{
id: "wood.tools",
title: "Craft wooden axe, pickaxe, sword",
isDone: (inv) => WOODEN_TOOLS.every((n) => has(inv, n)) || hasStoneTier(inv),
suggest: (inv) => {
if (!has(inv, "wooden_axe")) return { skillId: "craft.wooden-axe" };
if (!has(inv, "wooden_pickaxe")) return { skillId: "craft.wooden-pickaxe" };
if (!has(inv, "wooden_sword")) return { skillId: "craft.wooden-sword" };
return null;
},
},
{
id: "stone.32",
title: "Gather 32 cobblestone",
isDone: (inv) => totalCobble(inv) >= 32 || hasStoneTier(inv),
suggest: () => ({ skillId: "gather.stone" }),
},
{
id: "stone.tools",
title: "Craft stone axe, pickaxe, sword and a furnace",
isDone: (inv) => STONE_TOOLS.every((n) => has(inv, n)) && has(inv, "furnace"),
suggest: (inv) => {
if (!has(inv, "stone_axe")) return { skillId: "craft.stone-axe" };
if (!has(inv, "stone_pickaxe")) return { skillId: "craft.stone-pickaxe" };
if (!has(inv, "stone_sword")) return { skillId: "craft.stone-sword" };
if (!has(inv, "furnace")) return { skillId: "craft.furnace" };
return null;
},
},
{
id: "food.basic",
title: "Secure a basic food source",
isDone: (inv, snap) => {
const carrying = ["bread", "cooked_beef", "cooked_chicken", "cooked_porkchop", "apple", "carrot", "potato", "baked_potato"].some(
(n) => has(inv, n),
);
return carrying || (snap?.food ?? 20) >= 18;
},
suggest: () => ({ skillId: "survive.eat" }), // best-effort; richer "find food" skill lands later
},
{
id: "storage.chest",
title: "Place a personal chest",
isDone: (inv) => has(inv, "chest"),
suggest: () => ({ skillId: "craft.chest" }),
},
{
id: "shelter.torch",
title: "Have torches on hand for the perimeter",
isDone: (inv) => has(inv, "torch", 4),
suggest: () => ({ skillId: "craft.torch" }),
},
];
export function isInventoryFull(snapshot) {
const distinct = Object.keys(snapshot?.inventory ?? {}).length;
return distinct >= INVENTORY_FULL_DISTINCT_STACKS;
}
// Walk the curriculum and return the first uncompleted milestone, plus
// the suggested skill for it. If the inventory is full, the scheduler may
// choose to insert a deposit/drop step before continuing; we mark that
// in the result rather than skipping the milestone so the TUI can show
// the right blocker.
export function nextMilestone(snapshot) {
const inv = snapshot?.inventory ?? {};
for (const m of MILESTONES) {
if (m.isDone(inv, snapshot)) continue;
const plan = m.suggest(inv, snapshot);
return {
milestone: { id: m.id, title: m.title },
plan,
inventoryFull: isInventoryFull(snapshot),
};
}
return null; // every milestone done — bot has reached the end of the
// deterministic curriculum and base/village logic takes over (Phase 4).
}
export function listMilestones() {
return MILESTONES.map((m) => ({ id: m.id, title: m.title }));
}
// Exposed for tests.
export const _internal = { totalLogs, totalPlanks, totalCobble, has, hasAny, MILESTONES };