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: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
// survive.eat — consume the best available food. Picks from the registry-
|
|
// derived foods() set rather than a hard-coded list, so a 1.20 server
|
|
// without "glow_berries" won't trip the skill.
|
|
|
|
import { eatBestFood } from "../actions.js";
|
|
import { foods } from "./groups.js";
|
|
|
|
export const skill = Object.freeze({
|
|
id: "survive.eat",
|
|
title: "Eat best food",
|
|
timeoutMs: 20_000,
|
|
preconditions(ctx) {
|
|
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
|
|
const food = (ctx.snapshot?.food ?? 20);
|
|
if (food >= 18) return { ok: false, code: "not_hungry", detail: `food=${food}` };
|
|
const allowed = foods(ctx.bot);
|
|
if (allowed.size === 0) {
|
|
return { ok: false, code: "unsupported_version", detail: "no foods in registry" };
|
|
}
|
|
const inv = ctx.snapshot?.inventory ?? {};
|
|
const carrying = Object.keys(inv).some((name) => allowed.has(name));
|
|
if (!carrying) return { ok: false, code: "no_food_source", detail: "no edible item in inventory" };
|
|
return { ok: true };
|
|
},
|
|
async execute(ctx) {
|
|
const res = await eatBestFood(ctx.bot);
|
|
if (res.ok) {
|
|
return {
|
|
ok: true,
|
|
code: "done",
|
|
detail: res.detail,
|
|
worldDelta: { ate: res.detail?.ate ?? null },
|
|
};
|
|
}
|
|
const msg = String(res.detail ?? "");
|
|
const code = msg.includes("no food in inventory")
|
|
? "no_food_source"
|
|
: msg.includes("timed out")
|
|
? "timeout"
|
|
: "failed";
|
|
return { ok: false, code, detail: res.detail, worldDelta: null };
|
|
},
|
|
validate(ctx, result) {
|
|
return result.ok && !!result.worldDelta?.ate;
|
|
},
|
|
});
|