fix(v0.3.1): sated bot stops chasing food + perf-leak + fuzzy improvement dedup

Third day of "bot just walks back and forth burning tokens". Root
causes were mechanical, not the manifesto:

1. SATED BOT CHASING FOOD (the big one)
   Bot had food=17 (nearly full) but storyline first_food + manifesto
   L1 required 2+ food ITEMS in inventory, so it looped scout-food /
   acquire-food for hours instead of working. Now both treat a hunger
   bar >= 14 (SATED_FOOD) as satisfied even with empty food inventory —
   a full bot chops wood / makes tools and grabs food opportunistically,
   only hard-pursuing food when actually hungry (< 14).
   manifesto/needs.js foodDetect + goal/storyline.js first_food.completed.

2. perf_hooks MEMORY LEAK (overnight OOM suspect)
   "MaxPerformanceEntryBufferExceededWarning: 1,000,001 measure entries".
   mineflayer/pathfinder emit perf marks we never consume. Added a
   60s reaper in bot.js (performance.clearMeasures/clearMarks). unref'd.

3. IMPROVEMENT QUEUE SELF-DUPLICATING
   The LLM re-filed closed gaps with reworded titles (#5/#8/#9 were
   dupes of implemented #1/#2/#3). Exact-title dedup missed them.
   Replaced with token-set fuzzy match (isDuplicateTitle): jaccard>=0.75
   OR >=3 shared meaningful tokens with jaccard>=0.5. Also: a re-filed
   gap that's already implemented/rejected is NOT resurrected as a new
   open row. Cleared all 5 open requests (now genuinely implemented).

Also confirmed (no change needed):
- canDig=true is a DELIBERATE codebase-wide choice ("without it the bot
  gets permanently stuck", actions.js). The stale memory recommending
  canDig=false is updated. ViaBackwards dig works partially (dug:1
  moved:1.8 observed); false would trap the bot in every pit.
- scout-food already has blind/tunnel fallback + 12s step timeout
  (operator's earlier edits) so trapped-pathfinder degrades instead of
  hanging 30s.

Tests: 407 green (was 404). Updated needs/state/storyline tests for the
SATED_FOOD threshold; added fuzzy-dedup + tokenize/jaccard tests.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 09:34:42 +03:00
co-authored by Claude Opus 4.7
parent 1cf60f81e9
commit 502d7ebae4
7 changed files with 153 additions and 25 deletions
+19
View File
@@ -946,6 +946,25 @@ function tick() {
function startTickLoop() { function startTickLoop() {
if (tickTimer) clearInterval(tickTimer); if (tickTimer) clearInterval(tickTimer);
tickTimer = setInterval(tick, config.tickIntervalMs); tickTimer = setInterval(tick, config.tickIntervalMs);
startPerfBufferReaper();
}
// mineflayer + mineflayer-pathfinder emit performance.mark/measure
// entries that accumulate in the global perf_hooks buffer with no
// upper bound. Over a multi-hour run this grew past 1,000,000 entries
// ("MaxPerformanceEntryBufferExceededWarning") and is a prime suspect
// for the overnight OOM. We don't consume those entries, so clear the
// buffer on a slow interval.
let perfReaperTimer = null;
function startPerfBufferReaper() {
if (perfReaperTimer) return;
perfReaperTimer = setInterval(() => {
try {
performance.clearMeasures?.();
performance.clearMarks?.();
} catch {}
}, 60_000);
perfReaperTimer.unref?.();
} }
// ---- IPC commands ---------------------------------------------------------- // ---- IPC commands ----------------------------------------------------------
+9 -8
View File
@@ -232,16 +232,17 @@ export const STORYLINE = Object.freeze([
title: "Найти первую еду", title: "Найти первую еду",
narration_ru: "Нужна еда — ищу корову, курицу или ягоды.", narration_ru: "Нужна еда — ищу корову, курицу или ягоды.",
completed(snap) { completed(snap) {
return countAny(snap?.inventory, FOOD_ITEMS) >= 2; // Done if we have a stock (≥2 food items) OR the hunger bar is
// comfortable (≥14). A sated bot should be chopping wood, not
// chasing a chicken it doesn't need — it'll grab food
// opportunistically when one wanders close.
if (countAny(snap?.inventory, FOOD_ITEMS) >= 2) return true;
if ((snap?.food ?? 20) >= 14) return true;
return false;
}, },
suggestSkill(snap) { suggestSkill(snap) {
// Two-tier strategy: // Only reached when the bot is actually hungry (food < 14) and
// - If a passive food mob is visible nearby (≤24 blocks in // has no stock. Local mob → acquire-food; else long-range scout.
// 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.
if (hasLocalFoodMob(snap)) return { skillId: "survive.acquire-food" }; if (hasLocalFoodMob(snap)) return { skillId: "survive.acquire-food" };
return { skillId: "survive.scout-food" }; return { skillId: "survive.scout-food" };
}, },
+7 -3
View File
@@ -73,10 +73,14 @@ test("step first_tools: requires all three wood tools", () => {
assert.equal(s.completed(snap({ inventory: { stone_pickaxe: 1, stone_axe: 1, stone_sword: 1 } })), true); 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", () => { test("step first_food: completed at ≥2 food items, OR when sated (food≥14)", () => {
const s = getStep("first_food"); const s = getStep("first_food");
assert.equal(s.completed(snap()), false); // hungry + no food → not done
assert.equal(s.completed(snap({ inventory: { bread: 2 } })), true); assert.equal(s.completed(snap({ food: 8 })), false);
// hungry + 2 food items → done (have a stock)
assert.equal(s.completed(snap({ food: 8, inventory: { bread: 2 } })), true);
// sated (food≥14) + no food → done (don't chase food while full)
assert.equal(s.completed(snap({ food: 17 })), true);
}); });
test("step first_food: local hunt only for edible passive mobs within acquire range", () => { test("step first_food: local hunt only for edible passive mobs within acquire range", () => {
+63 -10
View File
@@ -334,16 +334,28 @@ export function createImprovementRequest({
} = {}) { } = {}) {
if (!_isAvailable() || !title) return null; if (!_isAvailable() || !title) return null;
try { try {
// Dedup: if an open request with same title (case-insensitive) exists, // Fuzzy dedup: the LLM re-files the same gap with reworded titles
// bump its votes instead of inserting a new row. // ("целевого поиска еды" vs "целевого дальнего поиска еды по биому").
const dup = _getStore().prepare(` // Exact-title match misses these, so we compare normalized token
SELECT id, votes FROM improvement_requests // sets against ALL recent requests (any status — a closed/
WHERE LOWER(title) = LOWER(?) AND status = 'open' // implemented one shouldn't reappear as a fresh open row). On a
ORDER BY ts DESC LIMIT 1 // strong overlap we bump votes (if still open) and return, instead
`).get(title); // of inserting a near-duplicate.
if (dup) { const incoming = tokenize(title);
_getStore().prepare(`UPDATE improvement_requests SET votes = votes + 1 WHERE id = ?`).run(dup.id); const candidates = _getStore().prepare(`
return dup.id; SELECT id, title, status, votes FROM improvement_requests
ORDER BY ts DESC LIMIT 60
`).all();
for (const c of candidates) {
if (isDuplicateTitle(incoming, tokenize(c.title))) {
// Re-flagged gap. If it's still open, count the vote. If it
// was implemented/rejected, do NOT resurrect it — just
// return its id so the caller treats it as "already known".
if (c.status === "open") {
_getStore().prepare(`UPDATE improvement_requests SET votes = votes + 1 WHERE id = ?`).run(c.id);
}
return c.id;
}
} }
const res = _getStore().prepare(` const res = _getStore().prepare(`
INSERT INTO improvement_requests INSERT INTO improvement_requests
@@ -412,7 +424,48 @@ export function markImprovementStatus(id, { status, notes } = {}) {
function clamp(n, lo, hi) { return Math.max(lo, Math.min(hi, Number(n) || lo)); } function clamp(n, lo, hi) { return Math.max(lo, Math.min(hi, Number(n) || lo)); }
// Normalize a title into a set of meaningful tokens for fuzzy dedup.
// Lowercase, strip punctuation, drop short / stop words (RU + EN) that
// carry no signal ("нет", "навыка", "для", "the", "a", …).
const _STOP = new Set([
"нет", "навык", "навыка", "для", "из", "по", "в", "на", "и", "с", "к",
"когда", "нужно", "это", "что", "the", "a", "an", "to", "of", "for",
"no", "skill", "has", "is", "low", "rate",
]);
function tokenize(s) {
if (!s || typeof s !== "string") return new Set();
const words = s.toLowerCase()
.replace(/["'`(),.:;!?\/\\-]+/g, " ")
.split(/\s+/)
.filter((w) => w.length >= 3 && !_STOP.has(w));
return new Set(words);
}
function jaccard(a, b) {
if (a.size === 0 || b.size === 0) return 0;
let inter = 0;
for (const w of a) if (b.has(w)) inter++;
return inter / (a.size + b.size - inter);
}
function intersectionSize(a, b) {
let n = 0;
for (const w of a) if (b.has(w)) n++;
return n;
}
// Two titles are "the same gap" when they either overlap very strongly
// (jaccard ≥ 0.75) OR share at least 3 meaningful tokens with moderate
// overlap (≥ 0.5). The 3-token floor stops short titles with one or two
// generic words in common ("low-prio thing" vs "high-prio thing") from
// false-matching, while still catching reworded long titles.
function isDuplicateTitle(a, b) {
const j = jaccard(a, b);
if (j >= 0.75) return true;
return intersectionSize(a, b) >= 3 && j >= 0.5;
}
function safeParse(s) { function safeParse(s) {
if (!s) return null; if (!s) return null;
try { return JSON.parse(s); } catch { return null; } try { return JSON.parse(s); } catch { return null; }
} }
// Test exports
export const __testing = { tokenize, jaccard, isDuplicateTitle };
+36
View File
@@ -320,6 +320,42 @@ test("improvement requests: create, dedup-by-title bumps votes, list filters", a
assert.ok(!stillOpen.some((r) => r.id === id1)); assert.ok(!stillOpen.some((r) => r.id === id1));
}); });
test("improvement requests: fuzzy dedup catches reworded titles + won't resurrect closed ones", async () => {
await bootstrap();
if (!isAvailable()) return;
const a = createImprovementRequest({
source: "postmortem", category: "skill",
title: "Нет навыка целевого поиска еды по биому", priority: 1,
});
assert.ok(a);
// Reworded near-duplicate — should map to the SAME row, bump votes.
const b = createImprovementRequest({
source: "reflect", category: "skill",
title: "Нет навыка целевого дальнего поиска еды по биому", priority: 1,
});
assert.equal(b, a, "reworded title deduped to original");
// Mark it implemented, then the LLM re-files the same gap reworded
// again — must NOT create a fresh open row (no resurrection).
markImprovementStatus(a, { status: "implemented", notes: "scout-food" });
const c = createImprovementRequest({
source: "reflect", category: "skill",
title: "Нужен навык дальнего поиска еды по биому", priority: 1,
});
assert.equal(c, a, "re-filed closed gap returns existing id, no new row");
const open = listImprovements({ status: "open" });
assert.ok(!open.some((r) => r.id === a), "closed gap stays closed");
});
test("tokenize / jaccard: similarity helpers", async () => {
const { tokenize, jaccard } = (await import("./index.js")).__testing;
const t1 = tokenize("Нет навыка целевого поиска еды по биому");
const t2 = tokenize("Нет навыка целевого дальнего поиска еды по биому");
assert.ok(jaccard(t1, t2) >= 0.6, `expected ≥0.6, got ${jaccard(t1, t2)}`);
const t3 = tokenize("Trigger wedged_96s has low success rate");
assert.ok(jaccard(t1, t3) < 0.3, "unrelated titles score low");
});
test("improvement requests: priority and status ordering", async () => { test("improvement requests: priority and status ordering", async () => {
await bootstrap(); await bootstrap();
if (!isAvailable()) return; if (!isAvailable()) return;
+10 -1
View File
@@ -122,9 +122,18 @@ function alivePursue(s) {
return null; return null;
} }
// SATED_FOOD — above this hunger level the bot is NOT hungry; chasing
// food (scout/acquire) is wasted motion. The bot should keep working
// toward wood/tools/shelter and pick up food opportunistically. It
// only becomes a hard need again when the bar drops below this.
const SATED_FOOD = 14;
function foodDetect(s) { function foodDetect(s) {
if (!s?.connected) return true; if (!s?.connected) return true;
if ((s.food ?? 20) >= 18 && countAny(s.inventory, FOOD_ITEMS) >= 1) return true; // Comfortable hunger bar → treat L1 as satisfied even with an empty
// food inventory. (Was: required 6+ food items, which made a sated
// bot loop scout/acquire-food for hours doing nothing else.)
if ((s.food ?? 20) >= SATED_FOOD) return true;
return countAny(s.inventory, FOOD_ITEMS) >= 6; return countAny(s.inventory, FOOD_ITEMS) >= 6;
} }
+9 -3
View File
@@ -33,13 +33,19 @@ test("pickActiveNeed: fresh spawn → L0 alive if zero food", () => {
assert.equal(a.skillId, "survive.scout-food"); assert.equal(a.skillId, "survive.scout-food");
}); });
test("pickActiveNeed: hp ok, no food in inventory → L1 food (scout)", () => { test("pickActiveNeed: hungry (food<14), no food in inventory → L1 food (scout)", () => {
_resetForTest(); _resetForTest();
const a = pickActiveNeed(snap()); const a = pickActiveNeed(snap({ food: 8 }));
assert.equal(a.need.id, "food"); assert.equal(a.need.id, "food");
assert.equal(a.skillId, "survive.scout-food"); assert.equal(a.skillId, "survive.scout-food");
}); });
test("pickActiveNeed: sated (food=20) + empty inventory → skips L1, goes to L2 tools_wood", () => {
_resetForTest();
const a = pickActiveNeed(snap());
assert.equal(a.need.id, "tools_wood", "sated bot works toward tools, not chasing food");
});
test("pickActiveNeed: food covered → L2 tools_wood (gather logs)", () => { test("pickActiveNeed: food covered → L2 tools_wood (gather logs)", () => {
_resetForTest(); _resetForTest();
const a = pickActiveNeed(snap({ inventory: { bread: 8 } })); const a = pickActiveNeed(snap({ inventory: { bread: 8 } }));
@@ -103,7 +109,7 @@ test("pickActiveNeed: hostile imminent + low HP → L0 takes over", () => {
test("describeActiveNeed: returns 'L<n> <id> → <skill>'", () => { test("describeActiveNeed: returns 'L<n> <id> → <skill>'", () => {
_resetForTest(); _resetForTest();
const s = describeActiveNeed(snap()); const s = describeActiveNeed(snap({ food: 8 }));
assert.match(s, /^L1 food → /); assert.match(s, /^L1 food → /);
}); });