feat(v0.4.1): make the Settlement Contract honest end-to-end

Patches each link of the closed-loop chain (world-truth -> invariant ->
dispatch -> worldDelta -> milestone) so the contract can no longer advance
on a lie or stall on an unreachable milestone, and surfaces the result on
the monitor.

- Unblock M6_lighting: add gather.coal producer (mirrors gather.stone,
  strict expectGain), register it, add the skill-graph node, and make
  curriculum.shelter.torch two-step (gather coal -> craft torch). craft.torch
  needed coal but no skill produced it, so the contract pinned on M6 forever
  and starved M7/M8/M9. One curriculum edit fixes both rails.
- M8 no longer falsely satisfied: build-shelter records locations.shelter
  only on real progress (setLocation moved below the no_progress guard).
- M2_bed made monotonic: sleepInBed writes locations.bed when the bot places
  its own bed, so M2 stays met after the carried bed item is consumed.
- Observability: new ink-free tui/format.js helper renders per-tick
  state / firing rail / active skill / blocked prerequisite / stall reason
  in the monitor header (fields bot.js already computes but never showed).
- Harden the valid-skill-id guardrail: skillRegistryPrompt preserves the
  "NEVER invent" footer when truncating (a growing registry was dropping it),
  and fix the stale "tech-tree > autonomous" reflex chain in the escalation
  prompt.

Tests: 494/494 passing; new build-shelter / invariants / format suites plus
skill-graph, curriculum, skill-registry cases. lint-patch clean. Deferred
audit findings recorded in plans/v0.4.0-vnext.md (local).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-29 12:33:45 +03:00
co-authored by Claude Opus 4.8
parent 5613f75a77
commit cd63edfeaf
17 changed files with 590 additions and 15 deletions
+107
View File
@@ -0,0 +1,107 @@
// Pure, ink-free helpers for the monitor header.
//
// bot.js stamps a rich observability bundle onto every STATUS snapshot
// (runtimeState, lastReflex.name, activeSkill, noProgressReason, reflexPaused)
// and the GoalManager annotates contract.suggestedSkill.blockedBy — but none
// of it was rendered, so the operator could not answer "what is the bot doing,
// why, and what is blocked?" from the screen. These helpers assemble that into
// colour-tagged segments. Kept in a plain .js module (no React/ink import) so
// `node --test` can unit-test the formatting without booting the TUI.
//
// A "segment" is { text, color?, dimColor?, bold? }. The monitor maps each to
// an <ink Text>; tests join `.text` and assert on substrings + segment colour.
const STATE_COLORS = Object.freeze({
emergency: "red",
recovering: "yellow",
working: "green",
planning: "magenta",
social: "cyan",
idle: "gray",
});
// computeState can return any of the STATES strings; unknown/new states render
// in a neutral colour rather than being blanked out.
export function stateColor(state) {
if (!state) return "gray";
return STATE_COLORS[String(state).toLowerCase()] ?? "white";
}
// blockedBy is an array of { item, min, have } | { tool } OBJECTS (from
// goal-manager.js via skill-graph.prerequisitesMet) — NOT strings. Map to the
// missing names so we never render "[object Object]". Returns null when there
// is nothing blocked.
export function formatBlockedBy(blockedBy) {
if (!Array.isArray(blockedBy) || blockedBy.length === 0) return null;
const names = blockedBy
.map((b) => {
const name = b?.item ?? b?.tool;
if (!name) return null;
return b?.min && b.min > 1 ? `${name}×${b.min}` : name;
})
.filter(Boolean);
return names.length ? names.join("+") : null;
}
// Build the "what / why / blocked / stalled" status line for the header.
// Reads only fields already present on the broadcast snapshot.
export function formatStatusSegments(snapshot = {}) {
const segs = [];
const push = (text, opts = {}) => {
if (text != null && text !== "") segs.push({ text, ...opts });
};
const state = snapshot.runtimeState;
if (state) push(String(state), { bold: true, color: stateColor(state) });
if (snapshot.reflexPaused === true) push(" PAUSED", { bold: true, color: "red" });
// why — which rail fired this tick (mode:…, curriculum, defend, …)
const why = snapshot.lastReflex?.name;
if (why) {
push(" via ", { dimColor: true });
push(String(why), { color: "white" });
}
// doing — the skill currently/last dispatched
const doing = snapshot.activeSkill;
if (doing) {
push(" · ", { dimColor: true });
push(String(doing), { color: "cyan" });
}
// blocked — the suggested skill cannot run because a prerequisite is
// missing (e.g. craft.torch needs coal). This is the one that made the
// M6-torch/coal stall invisible.
const sg = snapshot.contract?.suggestedSkill;
const blocked = formatBlockedBy(sg?.blockedBy);
if (blocked) {
push(" · ", { dimColor: true });
if (sg?.skillId) push(String(sg.skillId), { color: "red" });
push(" needs ", { dimColor: true });
push(blocked, { color: "red", bold: true });
}
// urgent preemption (food jumping the contract queue)
const reason = snapshot.contract?.reason;
if (typeof reason === "string" && reason.startsWith("urgent")) {
push(" · ", { dimColor: true });
push(reason, { color: "yellow" });
}
// stalled — no observable world change for a while
const np = snapshot.noProgressReason;
if (np) {
push(" · stalled:", { dimColor: true });
push(String(np), { color: "yellow" });
}
return segs;
}
// Convenience for tests / plain-text surfaces.
export function statusText(snapshot) {
return formatStatusSegments(snapshot)
.map((s) => s.text)
.join("");
}
+67
View File
@@ -0,0 +1,67 @@
// Pure formatting helpers for the monitor header. These assemble the per-tick
// "what / why / blocked / stalled" line from snapshot fields the bot already
// broadcasts, so the operator can see why the bot is (not) acting. Tested off
// ink — no React/render involved.
import { test } from "node:test";
import assert from "node:assert/strict";
import { formatStatusSegments, statusText, formatBlockedBy, stateColor } from "./format.js";
test("statusText surfaces state, firing rail, active skill and stall reason", () => {
const txt = statusText({
runtimeState: "idle",
lastReflex: { name: "curriculum" },
activeSkill: "gather.logs",
noProgressReason: "waiting_for_day",
});
assert.match(txt, /idle/);
assert.match(txt, /curriculum/);
assert.match(txt, /gather\.logs/);
assert.match(txt, /waiting_for_day/);
});
test("reflexPaused renders a PAUSED marker", () => {
assert.match(statusText({ runtimeState: "working", reflexPaused: true }), /PAUSED/);
assert.doesNotMatch(statusText({ runtimeState: "working", reflexPaused: false }), /PAUSED/);
});
test("blocked prerequisite is shown by name, never as [object Object]", () => {
const snap = {
runtimeState: "working",
contract: { suggestedSkill: { skillId: "craft.torch", blockedBy: [{ item: "coal", min: 1, have: 0 }] } },
};
const txt = statusText(snap);
assert.match(txt, /craft\.torch/);
assert.match(txt, /coal/);
assert.doesNotMatch(txt, /\[object Object\]/);
});
test("urgent contract reason (food preemption) is surfaced", () => {
const txt = statusText({
runtimeState: "working",
contract: { reason: "urgent:M4_food_security(100) preempts M1_wood_tools" },
});
assert.match(txt, /urgent/);
});
test("formatBlockedBy maps {item,min} and {tool} objects, null when empty", () => {
assert.equal(formatBlockedBy([]), null);
assert.equal(formatBlockedBy(undefined), null);
assert.equal(formatBlockedBy([{ item: "planks", min: 8, have: 0 }]), "planks×8");
assert.equal(formatBlockedBy([{ tool: "pickaxe" }]), "pickaxe");
assert.equal(formatBlockedBy([{ item: "coal", min: 1 }, { item: "stick", min: 1 }]), "coal+stick");
});
test("stateColor maps known states and defaults unknown ones to a visible colour", () => {
assert.equal(stateColor("emergency"), "red");
assert.equal(stateColor("idle"), "gray");
assert.equal(stateColor("planning"), "magenta");
// unknown / future states render in white rather than being blanked out
assert.equal(stateColor("teleporting"), "white");
assert.equal(stateColor(undefined), "gray");
});
test("empty snapshot yields no segments (header row collapses)", () => {
assert.equal(formatStatusSegments({}).length, 0);
});
+16
View File
@@ -24,6 +24,7 @@
import React, { useEffect, useReducer, useState } from "react";
import { render, Box, Text, useApp, useInput, useStdout } from "ink";
import { createIpcClient } from "./ipc-client.js";
import { formatStatusSegments } from "./format.js";
import { EVENT_TYPES } from "../runtime/ipc-protocol.js";
import { stateDir } from "../runtime/config.js";
import { initKnowledge, isAvailable as knowledgeReady, recentRecommendations, listImprovements, recommendationStats } from "../runtime/knowledge/index.js";
@@ -215,6 +216,21 @@ function StatusHeader({ snapshot, connectedIpc, width, startedAt }: { snapshot:
</>
) : null}
</Box>
{/* What / why / blocked / stalled — per-tick observability the bot
already computes (runtimeState, lastReflex, activeSkill, blockedBy,
noProgressReason); assembled by the pure formatStatusSegments helper
so it stays unit-testable off ink. */}
{(() => {
const segs = formatStatusSegments(snapshot);
if (segs.length === 0) return null;
return (
<Box>
{segs.map((s, i) => (
<Text key={i} color={s.color} dimColor={s.dimColor} bold={s.bold}>{s.text}</Text>
))}
</Box>
);
})()}
</Box>
);
}