Files
pepa-pi-bot/runtime/skills/diagnose-scan.js
T
mayatnikovandClaude Opus 4.7 28f5d9e483 fix(perception): use numeric block ids — callback matchers silently fail under ViaBackwards
Root cause of "bot just stands still": every gather.* skill was using
bot.findBlock({ matching: (b) => names.includes(b.name) }), and under
mineflayer 1.21.4 + ViaBackwards the Block objects fed into the
callback have a wrong .name field (Block.type / numeric id is still
correct — this is mineflayer issue #2347). Every search returned null,
every skill reported "no_target", reflex looped wander → tunnel-out
forever. The bot's logs said "dispatch ok" while the operator watched
it pace in circles.

Proven live with a new diag.match skill on play.xmatic.team:
  findBlocks({matching: numericIds})              → 50 hits
  findBlocks({matching: (b) => b.name === ...})   →  0 hits  ← the bug
  findBlock({matching: (b) => b.name === ...})    → null     ← the bug
  findBlock({matching: numericIds})               → dark_oak_log @ (606,62,110)

After this fix the same bot from the same spawn dispatches gather.logs
and reaches the chop loop ("chop: dark_oak_log at 606,62,110 (tool=fists)")
instead of returning "no reachable log within 64 blocks".

Changes:
- runtime/perception.js (new): findBlocksByName / findNearestBlockByName
  centralise the numeric-id workaround for any future skill.
- runtime/actions.js: chopNearestTree, sleepInBed, placeCraftingTable now
  use perception. Also load mineflayer-tool plugin alongside collectblock
  (collectblock 1.6 hard-requires bot.tool to dispatch a dig).
- gather-stone, gather-wool, deposit-surplus rewritten to numeric-id
  search. gather-wool also loads mineflayer-tool.
- diagnose-scan.js (new): two diagnostic skills — diag.scan reports
  findBlocks counts per radius for common blocks; diag.match cross-tests
  the four matcher styles so this regression can be re-proven on demand.
- runtime/skills/index.js: registers diag.scan + diag.match.

Memory: project_findblock_callback_broken_under_viabackwards.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 13:45:29 +03:00

119 lines
4.2 KiB
JavaScript

// diag.scan / diag.match — ground-truth probes of bot.findBlock(s).
// Counts how many instances of common blocks are visible at increasing
// radii. Lets the operator (or a stuck detector) tell two failure modes
// apart:
//
// case A: grass_block.32 > 0 but oak_log.96 = 0 → world has no trees
// case B: grass_block.32 = 0 → findBlocks itself is broken (mineflayer
// issue #2347 under ViaBackwards), and
// gather.* skills will silently no-op
// forever no matter how far we walk.
//
// Writes nothing to journal. Always returns ok so it never wedges.
const TYPES = ["grass_block", "dirt", "stone", "oak_log", "birch_log", "spruce_log", "jungle_log", "coal_ore", "iron_ore"];
const RADII = [16, 32, 64, 96];
const LOG_NAMES = ["oak_log", "dark_oak_log", "spruce_log", "birch_log", "jungle_log", "acacia_log", "mangrove_log", "cherry_log", "pale_oak_log"];
export const matchSkill = Object.freeze({
id: "diag.match",
title: "Compare findBlock matcher styles for logs",
timeoutMs: 15_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
const mcData = bot.registry;
const ids = LOG_NAMES.map((n) => mcData?.blocksByName?.[n]?.id).filter((x) => typeof x === "number");
const r = 32;
// Style A: numeric matching (what diag.scan uses, and what works)
const a = (bot.findBlocks({ matching: ids, maxDistance: r, count: 50 }) || []).length;
// Style B: callback matching by .name (what chopNearestTree uses, and what fails)
const b = (bot.findBlocks({
matching: (blk) => !!blk && !!blk.position && LOG_NAMES.includes(blk.name),
maxDistance: r,
count: 50,
}) || []).length;
// Style C: callback matching by .type (numeric id) — control
const c = (bot.findBlocks({
matching: (blk) => !!blk && ids.includes(blk.type),
maxDistance: r,
count: 50,
}) || []).length;
// Style D: singular findBlock with callback matcher (what chopNearestTree literally calls)
const d = bot.findBlock({
matching: (blk) => !!blk && !!blk.position && LOG_NAMES.includes(blk.name),
maxDistance: r,
});
// Style E: singular findBlock with numeric id array
const e = bot.findBlock({ matching: ids, maxDistance: r });
return {
ok: true,
code: "match_done",
detail: {
ids,
radius: r,
A_findBlocks_numeric: a,
B_findBlocks_callback_name: b,
C_findBlocks_callback_type: c,
D_findBlock_callback_name: d ? { name: d.name, type: d.type, pos: { x: d.position.x, y: d.position.y, z: d.position.z } } : null,
E_findBlock_numeric: e ? { name: e.name, type: e.type, pos: { x: e.position.x, y: e.position.y, z: e.position.z } } : null,
},
worldDelta: null,
};
},
});
export const skill = Object.freeze({
id: "diag.scan",
title: "Scan for findBlocks visibility",
timeoutMs: 20_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
const mcData = bot.registry;
const here = bot.entity.position.clone();
const report = {};
for (const name of TYPES) {
const blk = mcData?.blocksByName?.[name];
if (!blk) {
report[name] = { absent_in_registry: true };
continue;
}
const perRadius = {};
for (const r of RADII) {
const hits = bot.findBlocks({ matching: blk.id, maxDistance: r, count: 50 }) || [];
perRadius[r] = hits.length;
}
report[name] = perRadius;
}
// Also blockAt directly underfoot and 5-block ring scan: this bypasses
// findBlocks entirely and proves whether the protocol decode is sane.
const under = bot.blockAt(here.offset(0, -1, 0));
const ring = {};
for (let dx = -5; dx <= 5; dx++) {
for (let dz = -5; dz <= 5; dz++) {
const b = bot.blockAt(here.offset(dx, -1, dz));
if (!b) continue;
ring[b.name] = (ring[b.name] || 0) + 1;
}
}
return {
ok: true,
code: "scan_done",
detail: {
pos: { x: +here.x.toFixed(1), y: +here.y.toFixed(1), z: +here.z.toFixed(1) },
under: under?.name ?? null,
ring_11x11_underfoot: ring,
findBlocks: report,
},
worldDelta: null,
};
},
});