Files
pepa-pi-bot/runtime/goal/storyline.test.js
T
mayatnikovandClaude Opus 4.7 7f545723b5 feat(v0.3.1): storyline — canonical Minecraft survival quest
The bot has been stuck in a loop for two days:
  acquire-food (fail: no nearby food) → explore.far → pillar-up (fail) → repeat

Diagnosis: manifesto + LLM advisor both correctly identify "you need
food" but neither expresses *what concretely to do next*. Manifesto is
a priority ladder (need-detection), not a narrative arc.

This commit adds the missing narrative layer — an ordered list of
operational steps that mirror the vanilla Minecraft survival path:

  1. orient_self      — Понять где я
  2. first_wood       — Собрать 8 поленьев
  3. crafting_basics  — Сделать верстак и палки
  4. first_tools      — Деревянные орудия
  5. first_food       — Найти первую еду
  6. shelter_minimal  — Простой шелтер с кроватью
  7. stone_tier       — Каменные орудия
  8. food_security    — Запас еды на 16+
  9. iron_age         — Железо и печь
  10. settle_base     — Постоянная база
  11. village_grow    — Развивать деревню (ongoing)

Each step has:
  - completed(snapshot) → bool — detects achievement from snapshot
  - suggestSkill(snapshot) → { skillId, args? } — concrete next dispatch
  - emergencyPause(snapshot) → bool — defers to manifesto L0 alive
    emergencies (low HP near hostile, lava under foot, food = 0)
  - narration_ru — chat-friendly Russian one-liner spoken on entry

Components:

- runtime/goal/storyline.js — 11-step canonical quest catalogue
- runtime/goal/state.js — pickCurrentStep(snapshot) walks the list,
  returns first non-completed step + its suggestion. 3s cache.
  Validates suggestSkill's skillId against the live registry.
- runtime/reflex.js — curriculumReflex dispatch priority is now:
    1. manifesto (L0 alive emergencies always win)
    2. storyline (concrete operational subgoal)
    3. curriculum plan (legacy fallback)
  Tests pass ctx.disableStoryline=true for isolation.
- runtime/bot.js — snapshot.storyStep populated each tick so
  chatter/advisor/reflect observers see the same view.
- runtime/coach/fast-advisor.js — buildUserPrompt now embeds the
  current step + its suggested skill, so LLM advice is anchored
  ("step 5 first_food, storyline wants survive.acquire-food, but
  recent dispatches show it's failing — try explore.far + scout").
- runtime/coach/advisor-trigger.js — forwards ctx.storyStep into
  advise() and logs step id at trigger time.
- runtime/coach/reflect.js — reflection prompt includes storyline
  progress so 30-min self-assessment is anchored.
- runtime/persona/chatter.js — narrates step.narration_ru on
  transition. Rate-limited via existing maybeNarrateRaw().

New operator CLI:

- scripts/show-story.js — fetches the live snapshot via IPC sock and
  prints step progress with ✓/→/ markers, current skill, inventory.
  Falls back to --plain catalogue view when bot offline.

Token cost impact: ~+30 input tokens per advise() call (one extra
line in user prompt). Trivial vs the value of grounding LLM advice
in a concrete narrative.

Operator usage:

  node scripts/show-story.js          # live progress + which step + why
  node scripts/show-story.js --plain  # static catalogue of all 11 steps

Tests: 376 green (was 360, +16 storyline tests).

Also in this branch (already committed): dev/v0.3.1/PRD.md —
LLM prompt cost optimization design doc.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 20:36:17 +03:00

166 lines
5.7 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { STORYLINE, getStep, __testing } from "./storyline.js";
import { pickCurrentStep, progressSummary, _resetForTest } from "./state.js";
function snap(overrides = {}) {
return {
connected: true,
health: 20,
food: 20,
hasFood: false,
inventory: {},
equipment: { hand: null, head: null, torso: null, legs: null, feet: null },
nearbyBlocks: {},
hazards: { footBlock: "grass_block", belowBlock: "dirt", headBlock: "air" },
isDay: true,
hostileCount: 0,
closestHostile: null,
_sessionMs: 60_000,
...overrides,
};
}
test("STORYLINE: 11 steps, all have id/title/narration/completed/suggestSkill", () => {
assert.equal(STORYLINE.length, 11);
for (const s of STORYLINE) {
assert.ok(s.id, `step missing id`);
assert.ok(s.title);
assert.ok(s.narration_ru);
assert.equal(typeof s.completed, "function");
assert.equal(typeof s.suggestSkill, "function");
}
// Ids unique
const ids = STORYLINE.map((s) => s.id);
assert.equal(new Set(ids).size, ids.length);
});
test("getStep: lookup by id", () => {
assert.equal(getStep("first_wood").title, "Собрать 8 поленьев");
assert.equal(getStep("does-not-exist"), null);
});
test("emergencyPause: low hp + close hostile → true", () => {
const { emergencyPause } = __testing;
assert.equal(emergencyPause(snap({ health: 4, closestHostile: { name: "zombie", distance: 3 } })), true);
assert.equal(emergencyPause(snap()), false);
assert.equal(emergencyPause(snap({ hazards: { footBlock: "lava" } })), true);
assert.equal(emergencyPause(snap({ food: 0 })), true);
});
test("step first_wood: completed when ≥8 logs", () => {
const s = getStep("first_wood");
assert.equal(s.completed(snap()), false);
assert.equal(s.completed(snap({ inventory: { oak_log: 8 } })), true);
assert.equal(s.completed(snap({ inventory: { oak_log: 4, birch_log: 4 } })), true);
});
test("step first_wood: suggest gather.logs if trees nearby, explore.far otherwise", () => {
const s = getStep("first_wood");
assert.equal(s.suggestSkill(snap({ nearbyBlocks: { logs: 5 } })).skillId, "gather.logs");
assert.equal(s.suggestSkill(snap()).skillId, "explore.far");
});
test("step first_tools: requires all three wood tools", () => {
const s = getStep("first_tools");
assert.equal(s.completed(snap({ inventory: { wooden_pickaxe: 1 } })), false);
assert.equal(s.completed(snap({ inventory: { wooden_pickaxe: 1, wooden_axe: 1 } })), false);
assert.equal(s.completed(snap({ inventory: { wooden_pickaxe: 1, wooden_axe: 1, wooden_sword: 1 } })), true);
// Higher tier also counts
assert.equal(s.completed(snap({ inventory: { stone_pickaxe: 1, stone_axe: 1, stone_sword: 1 } })), true);
});
test("step first_food: completed at ≥2 food items", () => {
const s = getStep("first_food");
assert.equal(s.completed(snap()), false);
assert.equal(s.completed(snap({ inventory: { bread: 2 } })), true);
});
test("step shelter_minimal: completed when bed placed nearby", () => {
const s = getStep("shelter_minimal");
assert.equal(s.completed(snap()), false);
assert.equal(s.completed(snap({ nearbyBlocks: { beds: 1 } })), true);
});
test("step stone_tier: needs cobblestone first", () => {
const s = getStep("stone_tier");
assert.equal(s.completed(snap()), false);
assert.equal(s.suggestSkill(snap()).skillId, "gather.stone");
assert.equal(s.suggestSkill(snap({ inventory: { cobblestone: 6, stick: 4 } })).skillId, "craft.stone-pickaxe");
});
test("village_grow: never auto-completes (ongoing)", () => {
const s = getStep("village_grow");
assert.equal(s.completed(snap({ inventory: { iron_pickaxe: 1, diamond_pickaxe: 1 } })), false);
});
test("pickCurrentStep: disconnected → null", () => {
_resetForTest();
assert.equal(pickCurrentStep({ connected: false }), null);
});
test("pickCurrentStep: fresh spawn → first non-completed step", () => {
_resetForTest();
const s = snap({ _sessionMs: 5_000, nearbyBlocks: {} });
const r = pickCurrentStep(s);
assert.ok(r);
// orient_self is the first; with no nearby blocks and short session,
// completed() returns false → picked.
assert.equal(r.step.id, "orient_self");
assert.equal(r.index, 0);
});
test("pickCurrentStep: bot with 8+ logs → first_wood done, picks crafting_basics", () => {
_resetForTest();
const r = pickCurrentStep(snap({
_sessionMs: 60_000,
nearbyBlocks: { logs: 3 },
inventory: { oak_log: 10 },
}));
assert.ok(r);
assert.equal(r.step.id, "crafting_basics");
assert.equal(r.completedSteps, 2, "orient_self + first_wood done");
});
test("pickCurrentStep: emergency pauses suggestion", () => {
_resetForTest();
const r = pickCurrentStep(snap({
health: 4,
closestHostile: { name: "zombie", distance: 3 },
nearbyBlocks: { logs: 2 },
}));
assert.ok(r);
assert.equal(r.emergency, true);
assert.equal(r.suggestion, null, "no concrete suggestion while emergency holds");
});
test("pickCurrentStep: rejects unknown skill ids from suggestSkill", () => {
_resetForTest();
// Inject a synthetic step with bogus skill — but STORYLINE is frozen,
// so we just verify that real ids are valid (sanity check).
const r = pickCurrentStep(snap({
_sessionMs: 60_000,
nearbyBlocks: { logs: 2 },
}));
if (r?.suggestion?.skillId) {
// All real STORYLINE skill ids should be registered.
// (skill-registry imports a frozen list of skills/index.js.)
assert.ok(r.suggestion.skillId.includes("."), "skill id is namespaced");
}
});
test("progressSummary: formats step n/N + skill + emergency tag", () => {
_resetForTest();
const s1 = progressSummary(snap({ _sessionMs: 5_000 }));
assert.match(s1, /step 1\/11/);
assert.match(s1, /orient_self/);
_resetForTest();
const s2 = progressSummary(snap({
health: 3,
closestHostile: { name: "creeper", distance: 2 },
}));
assert.match(s2, /EMERGENCY/);
});