Files
pepa-pi-bot/runtime/goal/storyline.js
T
mayatnikovandClaude Opus 4.7 08ac2e1bc1 feat(v0.3.1): real survival patterns — biome-aware scout, wedge-relocate, escape-pit-safe
Operator reported the bot wandered the same 50×50 patch for 2 hours
without making any progress toward food. Diagnosis showed three root
causes; this commit addresses all five open improvement_requests
the LLM (postmortem + tuner) flagged automatically.

Research basis (`Voyager`, `Plan4MC`, `GITM`, `Mindcraft`):
  - Coverage / commit-to-cardinal exploration when local scan fails
  - Biome-aware strategy switching using a static affordance table
  - Wedge detector above the skill layer that triggers RELOCATE not
    RETRY (per-skill stuck checks reset on re-entry — useless)
  - Time-in-region bbox heuristic + need-duration AND skill-cycle gate

Concrete changes:

1. `runtime/goal/storyline.js`
   - orient_self.completed: added timeout fallback (HP=full + session
     >120s → done) so barren biomes don't block the bot on step 1.
     Closes improvement #2 'Нет навыка оценки когда сменить район'.
   - first_food.suggestSkill: now picks survive.scout-food (new) when
     no passive mob is nearby; falls back to survive.acquire-food only
     when something is in immediate range.

2. `runtime/biome-affordances.js` (new)
   - Static table: 40+ biomes → {has_passive_mobs, has_trees,
     has_water, has_crops, livable}.
   - Unknown biomes return optimistic defaults to avoid regressions.
   - Closes improvement #1 'Нет навыка целевого поиска еды по биому'.

3. `runtime/skills/scout-food.js` (new — survive.scout-food)
   - Tiered strategy: biome check → scan 32 → scan 64 → commit a
     cardinal for 200 blocks rescanning every 16. On cardinal
     exhaustion, returns code:"exhausted" so the curriculum can
     escalate to village.relocate.
   - In barren biomes (desert/ocean/snowy_plains) the scan is
     SKIPPED — bot walks straight toward the nearest neighbour
     biome that affords passive mobs (8-direction biome probe at
     radius 64).

4. `runtime/awareness/wedge-detector.js` (new)
   - Rolling 10-min position bbox tracker. observe() called every
     tick; isWedged() returns true when bbox<50 AND active need
     unmet >5min AND skill cycles ≥3.
   - markRelocationStarted() suppresses further wedge firings
     until the bot has displaced ≥200b — prevents stack overflow
     of relocate calls.
   - Lives ABOVE the skill layer (in runtime/reflex.js), because
     any per-skill stuck check resets on re-entry.

5. `runtime/skills/relocate.js` (new — village.relocate)
   - 300-block walk in least-recently-used cardinal (per-incident
     memory in ctx.recentRelocations).
   - Re-paths every 32 blocks, soft-tolerates pathfinder failures
     (3 consecutive throws → exit with code:"stuck_in_place").
   - Closes improvement #2 + #4 ('low success rate trigger').

6. `runtime/skills/escape-pit-safe.js` (new — recovery.escape-pit-safe)
   - Surveys 4 cardinals AND ceiling height before committing.
     Picks the direction with most open blocks (≥3, no lava).
     Falls through to pillar-up only if ceiling clear ≥4b. Returns
     code:"no_strategy" if both blocked so curriculum can escalate
     to relocate.
   - Closes improvement #3 'Нет навыка для безопасного выхода'.

7. `runtime/reflex.js`
   - Wedge detector wired before manifesto/storyline. If wedge.wedged
     is true, dispatches village.relocate directly and returns —
     bypasses every other branch.
   - ctx.disableWedge flag for tests.

8. `runtime/coach/advisor-trigger.js`
   - LLM provider outage backoff: 3 consecutive http_400 / timeout /
     network_error → suppress advisor for 10 min. Today's TimeWeb
     gpt-5.4-mini was 400'ing for an hour straight; we were spending
     trigger budget on dead calls. Closes improvement implicit gap
     in #5.

9. `runtime/bot.js`
   - Tracks botSpawnedAt; snapshot._sessionMs exposed for storyline
     orient_self timeout fallback.

Tests: 396 green (was 378, +18):
  - runtime/biome-affordances.test.js — 8 tests
  - runtime/awareness/wedge-detector.test.js — 9 tests
  - runtime/goal/storyline.test.js — 1 new test (orient_self timeout)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 23:01:39 +03:00

340 lines
12 KiB
JavaScript

// Storyline — canonical Minecraft survival quest the bot lives inside.
//
// Why this exists (rationale, 2026-05-27 evening):
//
// After v0.3.0 went live the bot got stuck in a loop:
// acquire-food (fail: no nearby food) → explore.far → pillar-up (fail) → repeat
//
// Manifesto + LLM advisor both correctly say "you need food" but
// neither expresses *what concretely to do next*: scout 64 blocks N
// for cows; chop oak nearby; place a crafting table. The bot has no
// narrative arc, just a priority ranking of unsatisfied needs.
//
// Storyline fixes this by laying down the classic vanilla Minecraft
// survival path as an ordered list of *concrete* steps. Each step
// owns:
// - id, title, narration_ru (chat-friendly Russian one-liner)
// - completed(snapshot) → bool — detects if this step's goal has
// been achieved purely from snapshot
// - suggestSkill(snapshot) → { skillId, args? } | null — the
// concrete next dispatch for the step's pursuit
// - emergencyPause(snapshot) → bool — true if a higher-priority
// condition (low HP near hostile, lava under foot, etc.) means we
// should drop story progression for a tick
//
// The runtime/goal/state.js picker walks the list and returns the
// first non-completed step, with its suggestSkill. That feeds into:
// - reflex.js dispatch picking (storyline overrides curriculum, but
// manifesto L0 alive emergencies still win)
// - persona/chatter.js — narrates step start in MC chat
// - coach/fast-advisor.js — user prompt includes current step so
// the LLM advice is anchored in the actual narrative
// - postmortem / reflect — the LLM can flag missing skills using
// the current step as concrete context
//
// Storyline order mirrors manifesto levels but is more *operational*:
// where manifesto says "L2 tools_wood satisfied if you have a wood
// pickaxe", storyline says "step first_tools: place crafting table,
// craft wooden pickaxe + axe + sword, with these specific subgoals."
const PICKAXE_WOOD = new Set(["wooden_pickaxe", "stone_pickaxe", "iron_pickaxe", "diamond_pickaxe", "netherite_pickaxe"]);
const AXE_WOOD = new Set(["wooden_axe", "stone_axe", "iron_axe", "diamond_axe", "netherite_axe"]);
const SWORD_WOOD = new Set(["wooden_sword", "stone_sword", "iron_sword", "diamond_sword", "netherite_sword"]);
const PICKAXE_STONE = new Set(["stone_pickaxe", "iron_pickaxe", "diamond_pickaxe", "netherite_pickaxe"]);
const BED_ITEMS = [
"white_bed", "orange_bed", "magenta_bed", "light_blue_bed", "yellow_bed",
"lime_bed", "pink_bed", "gray_bed", "light_gray_bed", "cyan_bed",
"purple_bed", "blue_bed", "brown_bed", "green_bed", "red_bed", "black_bed",
];
const FOOD_ITEMS = [
"bread", "cooked_beef", "cooked_porkchop", "cooked_chicken", "cooked_mutton",
"cooked_rabbit", "cooked_cod", "cooked_salmon", "baked_potato",
"apple", "carrot", "potato", "beetroot", "melon_slice", "sweet_berries",
"golden_apple", "golden_carrot",
];
function hasSetItem(inv, set) {
if (!inv) return false;
for (const name of Object.keys(inv)) {
if (set.has(name) && inv[name] > 0) return true;
}
return false;
}
function hasAny(inv, names) {
if (!inv) return false;
for (const n of names) if ((inv[n] ?? 0) > 0) return true;
return false;
}
function countAny(inv, names) {
if (!inv) return 0;
let total = 0;
for (const n of names) total += inv[n] ?? 0;
return total;
}
function countLogs(inv) {
if (!inv) return 0;
let total = 0;
for (const [name, count] of Object.entries(inv)) {
if (name.endsWith("_log")) total += count;
}
return total;
}
function countPlanks(inv) {
if (!inv) return 0;
let total = 0;
for (const [name, count] of Object.entries(inv)) {
if (name.endsWith("_planks")) total += count;
}
return total;
}
function emergencyPause(snap) {
if (!snap?.connected) return false;
const hp = snap.health ?? 20;
const food = snap.food ?? 20;
const hostile = snap.closestHostile;
if (hp <= 5) return true;
if (food <= 0) return true;
if (hostile && (hostile.distance ?? Infinity) <= 5 && hp <= 12) return true;
if (snap.hazards?.footBlock === "lava") return true;
return false;
}
// ---------------------------------------------------------------------------
export const STORYLINE = Object.freeze([
{
id: "orient_self",
title: "Понять где я",
narration_ru: "Где я? Осмотрюсь и оценю место.",
completed(snap) {
if (!snap?.connected) return false;
const hp = snap.health ?? 20;
// Two completion paths: (a) classic — full HP + saw tangible
// blocks within 16 blocks. (b) timeout — HP=full + session
// >120s. Path (b) exists because in desert/ocean biomes the
// scan radius might never see logs/stone/crops/beds, and we
// were getting stuck on step 1 for hours.
if (hp < 18) return false;
const sawBlocks = (snap.nearbyBlocks?.logs ?? 0)
+ (snap.nearbyBlocks?.stone ?? 0)
+ (snap.nearbyBlocks?.crops ?? 0)
+ (snap.nearbyBlocks?.beds ?? 0)
> 0;
if (sawBlocks) return true;
// Fallback: settled for long enough → call orient done and let
// later steps drive forward into the biome.
const sessionMs = snap._sessionMs ?? 0;
return sessionMs > 120_000;
},
suggestSkill(snap) {
// Look around — wander a bit to get a snapshot of what's nearby.
return { skillId: "explore.wander", args: { radius: 12 } };
},
emergencyPause,
},
{
id: "first_wood",
title: "Собрать 8 поленьев",
narration_ru: "Цель: 8 поленьев. Иду рубить ближайшие деревья.",
completed(snap) {
return countLogs(snap?.inventory) >= 8;
},
suggestSkill(snap) {
const trees = snap?.nearbyBlocks?.logs ?? 0;
if (trees > 0) return { skillId: "gather.logs" };
// No tree in sight — scout further. In a biome with no trees
// (desert, ocean) the bot must commit to a long heading; the
// curriculum's wedge detector (v0.3.1+) elevates this to
// village.relocate after a few cycles.
return { skillId: "explore.far", args: { searchFor: "logs" } };
},
emergencyPause,
},
{
id: "crafting_basics",
title: "Сделать верстак и палки",
narration_ru: "Делаю верстак и палки — без них ничего не скрафтить.",
completed(snap) {
const inv = snap?.inventory ?? {};
return (inv.crafting_table ?? 0) > 0
&& (inv.stick ?? 0) >= 2
&& countPlanks(inv) >= 4;
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if (countPlanks(inv) < 4) return { skillId: "craft.planks" };
if ((inv.stick ?? 0) < 2) return { skillId: "craft.sticks" };
// We have raw materials, need to *place* a crafting table for tools.
// (No place-table skill yet — flagged as improvement_request elsewhere.)
return { skillId: "craft.sticks" };
},
emergencyPause,
},
{
id: "first_tools",
title: "Деревянные орудия",
narration_ru: "Крафчу деревянный пикакс, топор и меч.",
completed(snap) {
const inv = snap?.inventory ?? {};
return hasSetItem(inv, PICKAXE_WOOD)
&& hasSetItem(inv, AXE_WOOD)
&& hasSetItem(inv, SWORD_WOOD);
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if (!hasSetItem(inv, PICKAXE_WOOD)) return { skillId: "craft.wooden-pickaxe" };
if (!hasSetItem(inv, AXE_WOOD)) return { skillId: "craft.wooden-axe" };
if (!hasSetItem(inv, SWORD_WOOD)) return { skillId: "craft.wooden-sword" };
return null;
},
emergencyPause,
},
{
id: "first_food",
title: "Найти первую еду",
narration_ru: "Нужна еда — ищу корову, курицу или ягоды.",
completed(snap) {
return countAny(snap?.inventory, FOOD_ITEMS) >= 2;
},
suggestSkill(snap) {
// Two-tier strategy:
// - If a passive food mob is visible nearby (≤24 blocks in
// snapshot), kill it locally with acquire-food.
// - Otherwise scout-food does long-range biome-aware search.
// It commits to a cardinal for ~200 blocks, rescans, and
// on biome boundary detection heads toward food-capable
// terrain.
const hasPassiveNearby = (snap?.nearbyEntities?.passives?.length ?? 0) > 0;
if (hasPassiveNearby) return { skillId: "survive.acquire-food" };
return { skillId: "survive.scout-food" };
},
emergencyPause,
},
{
id: "shelter_minimal",
title: "Простой шелтер с кроватью",
narration_ru: "Поставлю кровать и стены — пережить ночь.",
completed(snap) {
return (snap?.nearbyBlocks?.beds ?? 0) > 0;
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if (!hasAny(inv, BED_ITEMS)) {
const wool = countAny(inv, [
"white_wool", "orange_wool", "magenta_wool", "light_blue_wool",
"yellow_wool", "lime_wool", "pink_wool", "gray_wool",
"light_gray_wool", "cyan_wool", "purple_wool", "blue_wool",
"brown_wool", "green_wool", "red_wool", "black_wool",
]);
if (wool >= 3 && countPlanks(inv) >= 3) return { skillId: "craft.bed" };
if (wool < 3) return { skillId: "gather.wool" };
}
return { skillId: "village.build-shelter" };
},
emergencyPause,
},
{
id: "stone_tier",
title: "Каменные орудия",
narration_ru: "Шахта по камню — нужен каменный сет.",
completed(snap) {
return hasSetItem(snap?.inventory, PICKAXE_STONE);
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
const cobble = inv.cobblestone ?? 0;
if (cobble < 4) return { skillId: "gather.stone" };
if ((inv.stick ?? 0) < 2) return { skillId: "craft.sticks" };
if (!hasSetItem(inv, PICKAXE_STONE)) return { skillId: "craft.stone-pickaxe" };
if (!hasAny(inv, ["stone_axe"])) return { skillId: "craft.stone-axe" };
return { skillId: "craft.stone-sword" };
},
emergencyPause,
},
{
id: "food_security",
title: "Запас еды на 16+",
narration_ru: "Делаю ферму или загон — еды должно быть с запасом.",
completed(snap) {
return countAny(snap?.inventory, FOOD_ITEMS) >= 16;
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if ((inv.wheat_seeds ?? 0) > 0 && (snap?.nearbyBlocks?.crops ?? 0) > 0) {
return { skillId: "farm.wheat" };
}
return { skillId: "survive.acquire-food" };
},
emergencyPause,
},
{
id: "iron_age",
title: "Железо и печь",
narration_ru: "Иду за железом — пора в шахту глубже.",
completed(snap) {
const inv = snap?.inventory ?? {};
return (inv.iron_ingot ?? 0) >= 3 || (inv.iron_pickaxe ?? 0) > 0;
},
suggestSkill(snap) {
// No iron-specific gather skill yet — operator-facing improvement.
return { skillId: "gather.stone" };
},
emergencyPause,
},
{
id: "settle_base",
title: "Постоянная база",
narration_ru: "Выбираю место под деревню — нужно нормальное основание.",
completed(snap) {
const nb = snap?.nearbyBlocks ?? {};
return (nb.beds ?? 0) >= 1 && (nb.storage ?? 0) >= 1;
},
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if ((inv.chest ?? 0) === 0 && countPlanks(inv) >= 8) return { skillId: "craft.chest" };
if ((inv.chest ?? 0) > 0) return { skillId: "village.place-chest" };
return { skillId: "village.choose-base" };
},
emergencyPause,
},
{
id: "village_grow",
title: "Развивать деревню",
narration_ru: "Стою на ногах — теперь строю по плану деревни.",
completed() { return false; }, // ongoing — never auto-completes
suggestSkill(snap) {
const inv = snap?.inventory ?? {};
if ((inv.chest ?? 0) > 0 && countAny(inv, FOOD_ITEMS) > 0) {
return { skillId: "village.deposit-surplus" };
}
return { skillId: "village.build-shelter" };
},
emergencyPause,
},
]);
export function getStep(id) {
return STORYLINE.find((s) => s.id === id) ?? null;
}
// Test exports
export const __testing = {
countLogs, countPlanks, countAny, hasAny, hasSetItem,
FOOD_ITEMS, BED_ITEMS, emergencyPause,
};