feat(runtime): skill substrate + dynamic groups + reference skills (Phase 2)
Phase 2 of plans/autonomous-survival-bot-prd.md. Establishes the
composable skill contract from PRD §5.2 and ports three reference
skills so future phases can layer survival behaviour on top instead of
adding more ad-hoc branches to reflex.js.
New: runtime/skills/
- index.js: skill registry + runSkill(id, ctx, args) wrapper. Enforces
preconditions, hard timeout, normalises {ok, code, detail, worldDelta}
on every result, runs validate() and calls recover() on failure.
Stable failure codes live in RUNNER_CODES (unknown_skill,
precondition_failed, timeout, threw, validation_failed, done).
- groups.js: registry-derived item/block sets — logs/planks/sticks/beds
derived by suffix; foods intersects a curated allowlist with the live
bot.registry; axes/pickaxes/swords scoped to whatever the connected
server's item table actually ships. Empty set instead of throwing on
missing registry, so skills can emit code:"unsupported_version".
- chop-logs.js: gather.logs reference skill (wraps chopNearestTree).
- eat.js: survive.eat (wraps eatBestFood, preconditions check carrying
edible food from the registry-derived set).
- wander.js: explore.wander (wraps wander).
- contract.test.js + groups.test.js: 14 tests covering precondition
gating, timeout firing recover(), execute exceptions, validate
flipping ok→false, dynamic group filtering across mock registries.
package.json: `npm test` runs the new contract + groups suites.
docs/runtime.md: documents the skill contract, runner, dynamic groups
and the reference skills.
Reflex.js still calls actions.js directly — wiring the scheduler to
runSkill() lands in later phases when the survival curriculum kicks in.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Tests for runtime/skills/groups.js. We synthesise fake registries that
|
||||
// stand in for what mineflayer ships via bot.registry — small enough to be
|
||||
// hand-tested across "modern" and "old" Minecraft shapes.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { logs, planks, sticks, beds, foods, axes, pickaxes, swords } from "./groups.js";
|
||||
|
||||
function makeBot({ items = [], blocks = [] } = {}) {
|
||||
const itemsByName = Object.fromEntries(items.map((name) => [name, { id: 1, name }]));
|
||||
const blocksByName = Object.fromEntries(blocks.map((name) => [name, { id: 1, name }]));
|
||||
return { registry: { itemsByName, blocksByName } };
|
||||
}
|
||||
|
||||
test("logs() picks every _log and _stem block from registry", () => {
|
||||
const bot = makeBot({
|
||||
blocks: ["oak_log", "dark_oak_log", "crimson_stem", "warped_stem", "stone", "dirt"],
|
||||
});
|
||||
const got = logs(bot);
|
||||
assert.deepEqual(
|
||||
Array.from(got).sort(),
|
||||
["crimson_stem", "dark_oak_log", "oak_log", "warped_stem"],
|
||||
);
|
||||
});
|
||||
|
||||
test("logs() returns empty when registry missing", () => {
|
||||
assert.equal(logs(null).size, 0);
|
||||
assert.equal(logs({}).size, 0);
|
||||
assert.equal(logs({ registry: {} }).size, 0);
|
||||
});
|
||||
|
||||
test("planks/sticks/beds match by suffix or exact name", () => {
|
||||
const bot = makeBot({
|
||||
items: ["oak_planks", "stick", "dirt"],
|
||||
blocks: ["red_bed", "white_bed", "stone"],
|
||||
});
|
||||
assert.deepEqual(Array.from(planks(bot)).sort(), ["oak_planks"]);
|
||||
assert.deepEqual(Array.from(sticks(bot)), ["stick"]);
|
||||
assert.deepEqual(Array.from(beds(bot)).sort(), ["red_bed", "white_bed"]);
|
||||
});
|
||||
|
||||
test("foods() intersects the allowlist with the registry", () => {
|
||||
const bot = makeBot({
|
||||
items: ["bread", "apple", "spider_eye", "rotten_flesh", "cooked_beef", "glow_berries"],
|
||||
});
|
||||
const got = foods(bot);
|
||||
// Allowlist members present in this registry only — spider_eye and
|
||||
// rotten_flesh are explicitly NOT in the allowlist and must be excluded.
|
||||
assert.deepEqual(
|
||||
Array.from(got).sort(),
|
||||
["apple", "bread", "cooked_beef", "glow_berries"],
|
||||
);
|
||||
});
|
||||
|
||||
test("foods() returns empty on missing registry", () => {
|
||||
assert.equal(foods(null).size, 0);
|
||||
});
|
||||
|
||||
test("axes/pickaxes/swords scoped to what the version actually ships", () => {
|
||||
const bot = makeBot({
|
||||
items: ["wooden_axe", "stone_axe", "iron_pickaxe", "diamond_sword"],
|
||||
});
|
||||
assert.deepEqual(Array.from(axes(bot)).sort(), ["stone_axe", "wooden_axe"]);
|
||||
assert.deepEqual(Array.from(pickaxes(bot)).sort(), ["iron_pickaxe"]);
|
||||
assert.deepEqual(Array.from(swords(bot)).sort(), ["diamond_sword"]);
|
||||
});
|
||||
Reference in New Issue
Block a user