v0.3.0: Maslow + Awareness — self-learning bot with needs ladder, event-driven reflex, and TimeWeb fast advisor (#27)
* v0.3.0-rc.1: live skill registry + fast advisor scaffold
Roots out the v0.2.x failure mode: Pi-extracted lessons routinely named
hallucinated skill ids (relocate.surface, choose.safe.surface,
survive.shelter, gather.visible_log, …). All 47 Pi-lessons in the live DB
had applied_count=0 because normalisePreferSkill couldn't find them.
Fix:
1. runtime/skill-registry.js — single source of truth derived from
skills/index.js. Exports listSkillIds, isRegistered, and a
prompt-ready block (skillRegistryPrompt) grouped by namespace.
2. Pi prompts (coach/postmortem, coach/reflect) embed the live registry
with a "USE ONLY THESE, never invent" instruction. Lessons are
filtered at write-time too — anything not in the registry and not a
known mode name gets dropped.
3. coach/advice.js — normalisePreferSkill now returns null for unknown
ids, hardening consult() against any hallucinations that slip
through. Warn-logged for visibility.
Also lays the LLM substrate for the rest of v0.3.0:
- runtime/llm/provider.js — OpenAI-compatible chat client. Configured
via PEPA_FAST_LLM_{BASE_URL,API_KEY,MODEL,TIMEOUT_MS}. Safe no-op
unless API_KEY is set. Supports JSON-mode.
- runtime/coach/fast-advisor.js — tactical advisor tier (scaffold).
Exposes advise() that asks the fast LLM what to do RIGHT NOW when
the reflex is wedged/stuck. Rejects hallucinated skill ids using the
registry. Rate-limited 6/h, 30s cooldown. Not auto-triggered yet —
wired into reflex in rc.3 (awareness layer).
Tests: 279 green (+24 vs rc.3): 5 registry, 9 provider, 10 advisor.
See dev/v0.3.0/PLAN.md for the full iteration design (manifesto needs
ladder, event-driven awareness, skill pre-emption) and STATUS.md for
shipped/pending tracking.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* v0.3.0-rc.2: manifesto / needs ladder L0-L10
Adds an explicit hierarchical needs catalogue that the reflex consults
on every tick. The bot now pursues tangible intermediate goals (food,
wood tools, shelter, stone tools, ...) instead of inheriting whatever
the curriculum thought was "next".
Ladder:
L0 alive HP>5, food>0, not in lava, not panic-near hostile
L1 food ≥6 food items in inventory (or sated + any food)
L2 tools_wood wooden_pickaxe + wooden_axe + wooden_sword
L3 shelter_basic bed placed nearby or in inventory
L4 tools_stone stone-tier triplet
L5 armor_basic any chestplate (pursue=null until craft.leather-*
lands; ladder gracefully skips)
L6 food_security ≥16 food items
L7 tools_iron iron-tier triplet (pursue=gather.stone for now)
L8 armor_iron iron chestplate (pursue=null for now)
L9 village_seed bed + chest in nearby blocks
L10 village_full never detected, falls through to curriculum
Each need has detect(snapshot) → bool and pursue(snapshot) →
{skillId, args} | null. The ladder picks the LOWEST unsatisfied
pursuable need. Needs whose pursue is null get recorded as
blockedNeeds and the walk continues — no stalling on missing skills.
Wired into curriculumReflex: manifesto takes precedence over
curriculum.plan when it has a concrete suggestion. Tests can pass
ctx.disableManifesto=true to exercise the curriculum branch
in isolation (existing reflex tests keep passing this way).
Pi self-reflection prompt now includes
"activeNeed (Maslow ladder L0-L10): L2 tools_wood → gather.logs"
so Pi advises at the right level instead of giving generic guidance.
skillId returned by pursue() is validated against the live registry
(rc.1 plumbing) — manifesto cannot accidentally dispatch a
hallucinated skill name.
Tests: 315 green (was 279 on rc.1, +36 new):
- runtime/manifesto/needs.test.js — 24 tests (per-need detect/pursue,
helper sums)
- runtime/manifesto/state.test.js — 10 tests (ladder walk, hostile
takeover at L0, armor skipping, caching)
- runtime/reflex.test.js — 2 integration tests (manifesto overrides
curriculum plan; well-fed bot pursues tools_stone)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* v0.3.0-rc.3: event-driven awareness + skill pre-emption
Adds a reactive layer on top of the polling reflex. The bot now
notices environmental shocks (forced moves, HP plunges, hostile
spawns) within ~100ms instead of waiting for the next DISPATCH tick,
and the in-flight skill is preempted so the next reflex cycle can
re-plan against the current world state.
This is the rc that wires the "rc.1 plumbing + rc.2 manifesto" into
a feedback loop:
- awareness fires preempt → dispatch aborts
- reflex tick re-evaluates → manifesto walks the ladder
- new dispatch picks the right skill for the new world state
Pieces:
- runtime/awareness/events.js (new) — bot.on listeners:
- move: single-tick Δposition ≥ 5 blocks → forced_move flag + preempt
- health: HP drop ≥ 2 → health_plunge flag + preempt
- entitySpawn: hostile mob within 12 blocks → hostile_added + preempt
- blockUpdate: nearby block change → env_changed flag (no preempt,
throttled 800ms; otherwise gather skills would self-preempt
every dig)
- runtime/skills/index.js — RUNNER_CODES.PREEMPTED + raceWithAbort()
wraps every execute() against ctx.abortSignal. Existing skills get
preemption for free; they don't have to check the signal manually.
- runtime/bot.js:
- dispatchAction creates a fresh AbortController per dispatch and
stores it on reflexCtx.currentAbort
- attachAwareness fires controller.abort() when something disrupts
the active skill; runSkill returns code: "preempted" and the
reflex moves on
- reflexCtx.lastPreempt records the most recent shock
Tests: 332 green (was 315 on rc.2, +17 new):
- runtime/awareness/events.test.js — 12 tests (each event type +
thresholds + throttling + passive-mob filter)
- runtime/skills/contract.test.js — 3 abortSignal tests
(mid-flight, pre-armed, clean signal)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore(.env): add PEPA_FAST_LLM_* placeholders for v0.3.0 fast advisor
Empty values keep the fast-advisor tier disabled (safe no-op). Fill
in BASE_URL + API_KEY + MODEL to enable. TimeWeb-style endpoint
example included.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore(v0.3.0): rename fast-LLM env vars to TIMEWEB_* (match other projects)
Aligns with the user's other repos (proso) which use TIMEWEB_API_GROK /
TIMEWEB_URL_GROK. Single naming convention across projects avoids the
'which env var was it for this repo' mental tax.
PEPA_FAST_LLM_BASE_URL → TIMEWEB_BASE_URL
PEPA_FAST_LLM_API_KEY → TIMEWEB_API_KEY
PEPA_FAST_LLM_MODEL → TIMEWEB_MODEL
PEPA_FAST_LLM_TIMEOUT_MS → TIMEWEB_TIMEOUT_MS
Provider still works with any OpenAI-compatible endpoint — TimeWeb is
the default but the variable name doesn't lock us in. Tests + docs +
.env / .env.example updated.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(scripts): TimeWeb smoke test + bump default LLM timeout to 20s
scripts/check-timeweb.js — three probes: plain text, JSON mode, full
fast-advisor stack (registry injection + skill validation). Loads .env,
prints {ok, latency, reply preview} for each. Doesn't touch bot state.
Bumped DEFAULT_TIMEOUT_MS 8s → 20s in runtime/llm/provider.js. TimeWeb's
hosted agent endpoint takes 5-15s for the fast-advisor prompt
(registry block + snapshot context), so 8s was producing spurious
timeouts. OpenAI direct returns much faster; env var TIMEWEB_TIMEOUT_MS
overrides if needed.
Smoke verified live (PR #27 branch):
probe 1: 6.3s, plain prompt → "pepa hears you"
probe 2: 5.4s, JSON mode → {"alive":true,"name":"pepa"}
probe 3: 14.9s, advise() → action=switch_skill, skill=recovery.tunnel-out
(correct registered skill, sensible rationale — registry
injection successfully prevents hallucination)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(v0.3.0): auto-trigger fast-advisor + token usage tracking
Closes the awareness → LLM → action loop that the rc.1/2/3 sequence
left as a followup. When the bot is wedged, looping, or just suffered
a preempt-then-retry, the reflex fires advise() in the background;
when the recommendation lands it overrides the next dispatch.
Async by design: advise() takes 5-15s on TimeWeb's hosted endpoint —
too slow for a synchronous reflex tick. tickAdvisor() is fire-and-
forget, the result lands on ctx.advisorRecommendation, and the *next*
tick reads and consumes it. Recommendations age out after 60s.
Components:
- runtime/coach/advisor-trigger.js — policy + async fire path
- tickAdvisor(ctx, {plannedSkillId}) checks three triggers:
1. wedged > 60s (no significant move)
2. last 4+ dispatches are the same skill AND it's planned again
3. preempt within last 30s + same skill being retried
- 90s trigger cooldown, single-in-flight guard
- consumeFreshRecommendation(ctx) reads/clears the cache
- runtime/reflex.js — curriculumReflex calls tickAdvisor() every tick
and consumes a fresh recommendation BEFORE dispatching. ctx flag
disableAdvisor=true for tests.
- runtime/bot.js — dispatchAction maintains a rolling 8-slot
reflexCtx.recentSkillIds for the loop-detection trigger.
Token usage:
- runtime/llm/provider.js — normaliseUsage() reads OpenAI/TimeWeb-
style {prompt_tokens, completion_tokens, total_tokens} from the
response. Returned on every complete() result and logged at info
level as "in=Nt/out=Mt".
- runtime/coach/fast-advisor.js — getUsageSnapshot() aggregates
total tokens across all calls in the session.
Measured on live TimeWeb endpoint (gpt-5.4-mini agent):
per call: ~705 input + 45 output = ~750 tokens
rate limit: 6 calls/hour
worst case at full budget: ~108K tokens/day
estimated cost (OpenAI gpt-5-mini reference price): ~$0.60/month
Well within any reasonable budget — model can run hot 24/7.
Smoke verified: scripts/check-timeweb.js probe 4 produces
trigger fired: true (wedged_90s)
recommendation: recovery.tunnel-out
rationale: "Stuck wedged for 90s; exploration is failing."
latency: 5302ms
Tests: 345 green (was 332, +13 advisor-trigger).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(v0.3.0): paradigm shift — TimeWeb-only LLM + persistent advisor trail + improvement queue
This is the rc.4 batch the user requested:
1. Emergency triggers (low HP + close hostile, lava-under-foot)
bypass the long cooldown so the LLM is consulted BEFORE the bot
dies, not after.
2. Active manifesto need is now included in the advisor user prompt
— the LLM picks suggestions that satisfy the bot's current
concrete need (L2 tools_wood → "gather logs nearby" not
"explore further").
3. Every advisor recommendation is persisted to SQLite
(advisor_recommendations table) with full token usage. The
reflex marks 'applied=1' when it dispatches and updates
outcome_ok/code when the dispatch completes. Ground truth for
"is the LLM actually helping" lives in the DB, not in logs.
4. Pi CLI is OUT of every background loop. coach/postmortem and
coach/reflect now go through the same TimeWeb endpoint
fast-advisor uses, via the shared coach/llm-call.js helper.
Pi is reserved for manual operator commands.
5. The LLM (postmortem, reflect, advisor) can flag "structural
gaps" — missing skills/features the operator should implement.
These land in the new improvement_requests table. Dedup by
title bumps `votes` instead of inserting duplicates so the
queue doesn't bloat. Operator views via
`node scripts/list-improvements.js`.
6. A deterministic trigger-tuner runs hourly: reads 24h of
recommendation stats, flags triggers whose success rate is
below 25% (sample ≥ 5) or whose prompts are expensive (>1000
input tokens) with mediocre payoff. Improvements get
source="tuner", category="tuning". No LLM call.
New files:
runtime/coach/llm-call.js — askAnalytical() helper
runtime/coach/trigger-tuner.js — stats → improvements
runtime/coach/trigger-tuner.test.js
scripts/list-improvements.js — operator CLI
Schema additions:
advisor_recommendations: id, ts, trigger_reason, planned_skill,
recommended_skill, action, rationale, active_need, tokens_in,
tokens_out, latency_ms, applied, outcome_ok, outcome_code, outcome_at
improvement_requests: id, ts, source, category, title, description,
context, priority, status, duplicate_of, votes, implemented_at, notes
Renamed env-var consumers:
Pi-coach drainOnce({ askPi }) → drainOnce({ askAnalyticalFn? })
Pi-reflect runOnce({ askPi }) → runOnce({ askAnalyticalFn? })
bot.js attachCoach/attachReflect no longer pass askPi
attachTuner() added to bot.js spawn handler
lessons.source 'pi-coach' → 'timeweb-coach'
lessons.source 'pi-reflect' → 'timeweb-reflect'
Token cost measured live:
~705 input + 45 output = ~750 total per advisor call
worst case @ 6 calls/hour rate cap = ~108K tokens/day
OpenAI gpt-5-mini reference price: ~$0.60/month
Operator usage:
node scripts/list-improvements.js # open queue
node scripts/list-improvements.js --stats # advisor performance
node scripts/list-improvements.js --done 17 "shipped in 0.3.1"
node scripts/list-improvements.js --reject 18 "duplicate"
Tests: 360 green (was 332, +28 new).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Yuriy Mayatnikov <mayatnikov@me.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit was merged in pull request #27.
This commit is contained in:
+14
-6
@@ -10,7 +10,8 @@
|
||||
// recall → behavioural change. Without this, the DB is just a log.
|
||||
|
||||
import { isAvailable as knowledgeAvailable, topAdvice, markApplied } from "../knowledge/index.js";
|
||||
import { info } from "../log.js";
|
||||
import { isRegistered } from "../skill-registry.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
// Skills we will not blindly swap into — they require their own
|
||||
// preconditions (e.g. survive.flee needs a known threat direction).
|
||||
@@ -47,15 +48,19 @@ const MODE_TO_SKILL = Object.freeze({
|
||||
});
|
||||
|
||||
function normalisePreferSkill(raw) {
|
||||
if (!raw || typeof raw !== "string") return raw;
|
||||
if (SAFE_OVERRIDES.has(raw)) return raw;
|
||||
if (!raw || typeof raw !== "string") return null;
|
||||
if (SAFE_OVERRIDES.has(raw) && isRegistered(raw)) return raw;
|
||||
const lower = raw.toLowerCase().trim();
|
||||
if (MODE_TO_SKILL[lower]) return MODE_TO_SKILL[lower];
|
||||
// Pi sometimes writes "survive_flee" or "survive flee"; normalise.
|
||||
const dot = lower.replace(/[_\s]+/g, ".");
|
||||
if (SAFE_OVERRIDES.has(dot)) return dot;
|
||||
if (SAFE_OVERRIDES.has(dot) && isRegistered(dot)) return dot;
|
||||
if (MODE_TO_SKILL[dot]) return MODE_TO_SKILL[dot];
|
||||
return raw;
|
||||
// Anything else (Pi hallucinated names like "relocate.surface",
|
||||
// "choose.safe.surface", "survive.shelter", "gather.visible_log") —
|
||||
// hard reject. We'd rather fall through to 'avoid' / 'proceed' than
|
||||
// dispatch a nonexistent skill.
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +86,7 @@ export function consult({ plannedSkillId, snapshot } = {}) {
|
||||
// avoid_skill matches?
|
||||
if (advice.avoid && advice.avoid === plannedSkillId) {
|
||||
const normalisedPrefer = normalisePreferSkill(advice.prefer);
|
||||
if (normalisedPrefer && SAFE_OVERRIDES.has(normalisedPrefer)) {
|
||||
if (normalisedPrefer && SAFE_OVERRIDES.has(normalisedPrefer) && isRegistered(normalisedPrefer)) {
|
||||
if (normalisedPrefer !== advice.prefer) {
|
||||
info("coach", `advice: normalised prefer "${advice.prefer}" → "${normalisedPrefer}"`);
|
||||
}
|
||||
@@ -93,6 +98,9 @@ export function consult({ plannedSkillId, snapshot } = {}) {
|
||||
lesson: advice.lesson,
|
||||
};
|
||||
}
|
||||
if (advice.prefer && !normalisedPrefer) {
|
||||
warn("coach", `advice: rejected hallucinated prefer_skill "${advice.prefer}" (lesson #${advice.lessonId})`);
|
||||
}
|
||||
info("coach", `advice: avoid ${plannedSkillId} (lesson #${advice.lessonId})`);
|
||||
return { action: "avoid", lessonId: advice.lessonId, lesson: advice.lesson };
|
||||
}
|
||||
|
||||
@@ -117,10 +117,16 @@ test("normalisePreferSkill: passes through known dot-form skills unchanged", ()
|
||||
assert.equal(normalisePreferSkill("explore.far"), "explore.far");
|
||||
});
|
||||
|
||||
test("normalisePreferSkill: unknown values returned as-is", () => {
|
||||
assert.equal(normalisePreferSkill("some.unknown.skill"), "some.unknown.skill");
|
||||
test("normalisePreferSkill: unknown values rejected (returns null)", () => {
|
||||
// v0.3.0-rc.1: anything not in the live registry and not a known mode
|
||||
// name is rejected outright. We'd rather fall through to 'avoid' than
|
||||
// dispatch a hallucinated skill id.
|
||||
assert.equal(normalisePreferSkill("some.unknown.skill"), null);
|
||||
assert.equal(normalisePreferSkill("relocate.surface"), null);
|
||||
assert.equal(normalisePreferSkill("choose.safe.surface"), null);
|
||||
assert.equal(normalisePreferSkill("survive.shelter"), null);
|
||||
assert.equal(normalisePreferSkill(null), null);
|
||||
assert.equal(normalisePreferSkill(""), "");
|
||||
assert.equal(normalisePreferSkill(""), null);
|
||||
});
|
||||
|
||||
test("consult: Pi-style mode-name prefer is normalised to override target", async () => {
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
// Auto-trigger policy for the fast tactical advisor.
|
||||
//
|
||||
// The advisor is too slow for synchronous use inside a reflex tick
|
||||
// (5-15s via TimeWeb's hosted agent endpoint). The strategy here is
|
||||
// asynchronous: when conditions warrant tactical advice, fire-and-forget
|
||||
// an advise() call; when the result eventually arrives, cache it on
|
||||
// ctx.advisorRecommendation. The next reflex tick reads that cache and
|
||||
// can substitute the recommended skill before dispatching.
|
||||
//
|
||||
// Triggers (any one, AND-ed with the not-recently-asked cooldown):
|
||||
//
|
||||
// 1. Wedged > 60s — bot's position hasn't shifted ≥16 blocks in over
|
||||
// a minute (already tracked by reflex.js as lastSignificantMoveAt)
|
||||
// 2. Last 4+ dispatches were the same skill — clear loop signal
|
||||
// 3. Last awareness preempt was very recent AND followed by same
|
||||
// skill being dispatched again — env-shock-blind retry
|
||||
//
|
||||
// The recommendation has a TTL (60s). After that it's stale and the
|
||||
// reflex falls back to manifesto / curriculum. This keeps the system
|
||||
// reactive — advice ages out, fresh data drives fresh advice.
|
||||
|
||||
import { advise, isAvailable as advisorAvailable } from "./fast-advisor.js";
|
||||
import { isRegistered } from "../skill-registry.js";
|
||||
import { insertRecommendation } from "../knowledge/index.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
const TRIGGER_COOLDOWN_MS = 90_000;
|
||||
const RECOMMENDATION_TTL_MS = 60_000;
|
||||
const WEDGED_THRESHOLD_MS = 60_000;
|
||||
const REPEAT_THRESHOLD = 4;
|
||||
const PREEMPT_WINDOW_MS = 30_000;
|
||||
// Emergency triggers — bypass cooldown because waiting another 90s
|
||||
// when the bot is about to die is not useful.
|
||||
const EMERGENCY_HP = 6;
|
||||
const EMERGENCY_HOSTILE_DIST = 8;
|
||||
const EMERGENCY_COOLDOWN_MS = 20_000;
|
||||
|
||||
let _lastTriggerAt = 0;
|
||||
let _inFlight = false;
|
||||
|
||||
export function _resetForTest() {
|
||||
_lastTriggerAt = 0;
|
||||
_inFlight = false;
|
||||
}
|
||||
|
||||
export function getTriggerState() {
|
||||
return {
|
||||
lastTriggerAt: _lastTriggerAt,
|
||||
inFlight: _inFlight,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* tickAdvisor(ctx) → maybe-fires advise() in background.
|
||||
*
|
||||
* Called from reflex AFTER it has chosen a plannedSkillId but BEFORE
|
||||
* dispatching. Does NOT block — the in-flight call resolves later and
|
||||
* writes ctx.advisorRecommendation. The caller decides whether to
|
||||
* consume a fresh recommendation on this tick or wait for the next.
|
||||
*/
|
||||
export function tickAdvisor(ctx, { plannedSkillId } = {}) {
|
||||
if (!advisorAvailable()) return { fired: false, reason: "disabled" };
|
||||
if (_inFlight) return { fired: false, reason: "in_flight" };
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Drop a recommendation that's already aged out.
|
||||
if (ctx.advisorRecommendation && now - ctx.advisorRecommendation.at > RECOMMENDATION_TTL_MS) {
|
||||
ctx.advisorRecommendation = null;
|
||||
}
|
||||
|
||||
const reason = detectTrigger(ctx, now, plannedSkillId);
|
||||
if (!reason) return { fired: false, reason: "no_trigger" };
|
||||
|
||||
// Emergency triggers use a much shorter cooldown — waiting 90s with
|
||||
// HP=4 and a creeper at 3 blocks is exactly when we MUST hit the LLM.
|
||||
const isEmergency = reason.startsWith("emergency_");
|
||||
const cooldownMs = isEmergency ? EMERGENCY_COOLDOWN_MS : TRIGGER_COOLDOWN_MS;
|
||||
if (now - _lastTriggerAt < cooldownMs) {
|
||||
return { fired: false, reason: "cooldown" };
|
||||
}
|
||||
|
||||
_lastTriggerAt = now;
|
||||
_inFlight = true;
|
||||
const snapshot = ctx.snapshot ?? null;
|
||||
const recentSkillIds = (ctx.recentSkillIds ?? []).slice(-8);
|
||||
const activeNeed = ctx.activeNeed ?? null;
|
||||
|
||||
info("advisor-trigger", `firing because ${reason} (planned=${plannedSkillId ?? "?"}, need=${activeNeed?.need?.id ?? "?"})`);
|
||||
// Fire-and-forget. The promise's resolution writes ctx.advisorRecommendation.
|
||||
advise({ snapshot, reason, recentSkillIds, lessonsTail: ctx.recentLessons ?? [], activeNeed, force: true })
|
||||
.then((result) => {
|
||||
_inFlight = false;
|
||||
const needLabel = activeNeed
|
||||
? `L${activeNeed.need.level} ${activeNeed.need.id}`
|
||||
: null;
|
||||
if (result.ok && result.action === "switch_skill" && isRegistered(result.skillId)) {
|
||||
const recId = insertRecommendation({
|
||||
triggerReason: reason,
|
||||
plannedSkill: plannedSkillId ?? null,
|
||||
recommendedSkill: result.skillId,
|
||||
action: "switch_skill",
|
||||
rationale: result.rationale,
|
||||
activeNeed: needLabel,
|
||||
tokensIn: result.usage?.in,
|
||||
tokensOut: result.usage?.out,
|
||||
latencyMs: result.latencyMs,
|
||||
});
|
||||
ctx.advisorRecommendation = {
|
||||
id: recId,
|
||||
at: Date.now(),
|
||||
skillId: result.skillId,
|
||||
action: "switch_skill",
|
||||
rationale: result.rationale,
|
||||
triggerReason: reason,
|
||||
latencyMs: result.latencyMs,
|
||||
usage: result.usage ?? null,
|
||||
};
|
||||
info("advisor-trigger", `recommendation cached: ${result.skillId} (${result.latencyMs}ms, in=${result.usage?.in ?? "?"}t/out=${result.usage?.out ?? "?"}t, db=${recId ?? "-"})`);
|
||||
} else if (result.ok && (result.action === "wait" || result.action === "continue")) {
|
||||
const recId = insertRecommendation({
|
||||
triggerReason: reason,
|
||||
plannedSkill: plannedSkillId ?? null,
|
||||
recommendedSkill: null,
|
||||
action: result.action,
|
||||
rationale: result.rationale,
|
||||
activeNeed: needLabel,
|
||||
tokensIn: result.usage?.in,
|
||||
tokensOut: result.usage?.out,
|
||||
latencyMs: result.latencyMs,
|
||||
});
|
||||
ctx.advisorRecommendation = {
|
||||
id: recId,
|
||||
at: Date.now(),
|
||||
action: result.action,
|
||||
rationale: result.rationale,
|
||||
triggerReason: reason,
|
||||
latencyMs: result.latencyMs,
|
||||
usage: result.usage ?? null,
|
||||
};
|
||||
info("advisor-trigger", `recommendation: ${result.action} (${result.latencyMs}ms)`);
|
||||
} else if (!result.ok) {
|
||||
warn("advisor-trigger", `advise failed: ${result.code} (${result.detail})`);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
_inFlight = false;
|
||||
warn("advisor-trigger", `advise threw: ${e?.message ?? e}`);
|
||||
});
|
||||
|
||||
return { fired: true, reason };
|
||||
}
|
||||
|
||||
function detectTrigger(ctx, now, plannedSkillId) {
|
||||
const snap = ctx.snapshot ?? {};
|
||||
|
||||
// 0. EMERGENCY: low HP + hostile near — call BEFORE the bot dies.
|
||||
// Checked first so reason string starts with "emergency_" → bypasses
|
||||
// the long trigger cooldown via the caller's isEmergency check.
|
||||
const hp = snap.health ?? 20;
|
||||
const hostile = snap.closestHostile;
|
||||
if (hp <= EMERGENCY_HP && hostile && (hostile.distance ?? Infinity) <= EMERGENCY_HOSTILE_DIST) {
|
||||
return `emergency_hp${Math.round(hp)}_${hostile.name ?? "hostile"}@${Math.round(hostile.distance)}`;
|
||||
}
|
||||
// 0b. EMERGENCY: drowning / lava / lethal fluid
|
||||
if (snap.hazards?.footBlock === "lava") {
|
||||
return "emergency_lava";
|
||||
}
|
||||
|
||||
// 1. Wedged > threshold
|
||||
if (ctx.lastSignificantMoveAt && (now - ctx.lastSignificantMoveAt) > WEDGED_THRESHOLD_MS) {
|
||||
return `wedged_${Math.round((now - ctx.lastSignificantMoveAt) / 1000)}s`;
|
||||
}
|
||||
// 2. Repeat-skill loop
|
||||
const recent = ctx.recentSkillIds ?? [];
|
||||
if (recent.length >= REPEAT_THRESHOLD) {
|
||||
const tail = recent.slice(-REPEAT_THRESHOLD);
|
||||
const allSame = tail.every((id) => id === tail[0]);
|
||||
if (allSame && plannedSkillId === tail[0]) {
|
||||
return `repeat_${REPEAT_THRESHOLD}_${tail[0]}`;
|
||||
}
|
||||
}
|
||||
// 3. Recent preempt followed by same skill again
|
||||
if (ctx.lastPreempt && now - ctx.lastPreempt.at < PREEMPT_WINDOW_MS) {
|
||||
const lastDispatched = recent[recent.length - 1];
|
||||
if (lastDispatched && lastDispatched === plannedSkillId) {
|
||||
return `preempt_retry_${ctx.lastPreempt.reason}`;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* consumeFreshRecommendation(ctx) → { skillId, action, rationale } | null
|
||||
*
|
||||
* Returns a recommendation if one is currently cached and fresh, and
|
||||
* clears it so subsequent ticks don't re-apply the same advice.
|
||||
*/
|
||||
export function consumeFreshRecommendation(ctx) {
|
||||
const rec = ctx.advisorRecommendation;
|
||||
if (!rec) return null;
|
||||
if (Date.now() - rec.at > RECOMMENDATION_TTL_MS) {
|
||||
ctx.advisorRecommendation = null;
|
||||
return null;
|
||||
}
|
||||
if (rec.action !== "switch_skill" || !rec.skillId) {
|
||||
// 'continue' / 'wait' don't replace dispatch; surface for telemetry only
|
||||
return null;
|
||||
}
|
||||
ctx.advisorRecommendation = null;
|
||||
return rec;
|
||||
}
|
||||
|
||||
// Test exports
|
||||
export const __testing = {
|
||||
TRIGGER_COOLDOWN_MS, RECOMMENDATION_TTL_MS, WEDGED_THRESHOLD_MS,
|
||||
REPEAT_THRESHOLD, PREEMPT_WINDOW_MS, detectTrigger,
|
||||
};
|
||||
@@ -0,0 +1,244 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
tickAdvisor,
|
||||
consumeFreshRecommendation,
|
||||
getTriggerState,
|
||||
_resetForTest,
|
||||
__testing,
|
||||
} from "./advisor-trigger.js";
|
||||
import { _resetForTest as resetAdvisor } from "./fast-advisor.js";
|
||||
|
||||
const { detectTrigger, WEDGED_THRESHOLD_MS, REPEAT_THRESHOLD, PREEMPT_WINDOW_MS, RECOMMENDATION_TTL_MS } = __testing;
|
||||
|
||||
const API_KEY = "TIMEWEB_API_KEY";
|
||||
const MODEL = "TIMEWEB_MODEL";
|
||||
|
||||
function withEnv(env, fn) {
|
||||
const prev = {};
|
||||
for (const k of Object.keys(env)) {
|
||||
prev[k] = process.env[k];
|
||||
if (env[k] === undefined) delete process.env[k];
|
||||
else process.env[k] = env[k];
|
||||
}
|
||||
return Promise.resolve(fn()).finally(() => {
|
||||
for (const [k, v] of Object.entries(prev)) {
|
||||
if (v === undefined) delete process.env[k];
|
||||
else process.env[k] = v;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function stubFetch(reply, latency = 0) {
|
||||
const orig = globalThis.fetch;
|
||||
globalThis.fetch = async () => {
|
||||
if (latency) await new Promise((r) => setTimeout(r, latency));
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
choices: [{ message: { content: typeof reply === "string" ? reply : JSON.stringify(reply) } }],
|
||||
usage: { prompt_tokens: 1500, completion_tokens: 40, total_tokens: 1540 },
|
||||
}),
|
||||
};
|
||||
};
|
||||
return () => { globalThis.fetch = orig; };
|
||||
}
|
||||
|
||||
test("detectTrigger: returns null when nothing matches", () => {
|
||||
const r = detectTrigger({ recentSkillIds: [] }, Date.now(), "gather.logs");
|
||||
assert.equal(r, null);
|
||||
});
|
||||
|
||||
test("detectTrigger: low HP + hostile near → emergency_hp", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger({
|
||||
recentSkillIds: [],
|
||||
snapshot: { health: 4, closestHostile: { name: "creeper", distance: 3 } },
|
||||
}, now, "gather.logs");
|
||||
assert.match(r, /^emergency_hp4_creeper@3/);
|
||||
});
|
||||
|
||||
test("detectTrigger: foot in lava → emergency_lava", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger({
|
||||
recentSkillIds: [],
|
||||
snapshot: { health: 18, hazards: { footBlock: "lava" } },
|
||||
}, now, "explore.far");
|
||||
assert.equal(r, "emergency_lava");
|
||||
});
|
||||
|
||||
test("detectTrigger: emergency wins over wedged when both present", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger({
|
||||
recentSkillIds: [],
|
||||
snapshot: { health: 4, closestHostile: { name: "skeleton", distance: 5 } },
|
||||
lastSignificantMoveAt: now - 120_000,
|
||||
}, now, "x");
|
||||
assert.match(r, /^emergency_/);
|
||||
});
|
||||
|
||||
test("detectTrigger: wedged > 60s fires", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger(
|
||||
{ recentSkillIds: ["x"], lastSignificantMoveAt: now - WEDGED_THRESHOLD_MS - 5000 },
|
||||
now,
|
||||
"explore.far",
|
||||
);
|
||||
assert.match(r, /^wedged_\d+s/);
|
||||
});
|
||||
|
||||
test("detectTrigger: 4 same dispatches in row + same planned → repeat", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger(
|
||||
{ recentSkillIds: ["explore.far", "explore.far", "explore.far", "explore.far"] },
|
||||
now,
|
||||
"explore.far",
|
||||
);
|
||||
assert.match(r, /^repeat_4_explore\.far/);
|
||||
});
|
||||
|
||||
test("detectTrigger: same skill repeated but planned is different → no repeat trigger", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger(
|
||||
{ recentSkillIds: ["explore.far", "explore.far", "explore.far", "explore.far"] },
|
||||
now,
|
||||
"gather.logs",
|
||||
);
|
||||
assert.equal(r, null);
|
||||
});
|
||||
|
||||
test("detectTrigger: recent preempt + same skill re-planned → preempt_retry", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger(
|
||||
{
|
||||
recentSkillIds: ["gather.logs"],
|
||||
lastPreempt: { at: now - 5000, reason: "forced_move" },
|
||||
},
|
||||
now,
|
||||
"gather.logs",
|
||||
);
|
||||
assert.equal(r, "preempt_retry_forced_move");
|
||||
});
|
||||
|
||||
test("detectTrigger: old preempt (> window) does not trigger", () => {
|
||||
const now = Date.now();
|
||||
const r = detectTrigger(
|
||||
{
|
||||
recentSkillIds: ["gather.logs"],
|
||||
lastPreempt: { at: now - PREEMPT_WINDOW_MS - 5000, reason: "forced_move" },
|
||||
},
|
||||
now,
|
||||
"gather.logs",
|
||||
);
|
||||
assert.equal(r, null);
|
||||
});
|
||||
|
||||
test("tickAdvisor: disabled when TIMEWEB_API_KEY missing", async () => {
|
||||
await withEnv({ [API_KEY]: undefined }, () => {
|
||||
_resetForTest();
|
||||
const r = tickAdvisor({ recentSkillIds: [] }, { plannedSkillId: "x" });
|
||||
assert.equal(r.fired, false);
|
||||
assert.equal(r.reason, "disabled");
|
||||
});
|
||||
});
|
||||
|
||||
test("tickAdvisor: no_trigger when ctx has nothing interesting", async () => {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, () => {
|
||||
_resetForTest();
|
||||
resetAdvisor();
|
||||
const r = tickAdvisor({ recentSkillIds: [] }, { plannedSkillId: "gather.logs" });
|
||||
assert.equal(r.fired, false);
|
||||
assert.equal(r.reason, "no_trigger");
|
||||
});
|
||||
});
|
||||
|
||||
test("tickAdvisor: fires on wedged trigger and caches recommendation", async () => {
|
||||
const restore = stubFetch({
|
||||
action: "switch_skill",
|
||||
skill_id: "survive.flee",
|
||||
rationale: "Wedged here, retreat instead.",
|
||||
});
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
resetAdvisor();
|
||||
const ctx = {
|
||||
recentSkillIds: ["explore.far"],
|
||||
lastSignificantMoveAt: Date.now() - 120_000,
|
||||
};
|
||||
const r = tickAdvisor(ctx, { plannedSkillId: "explore.far" });
|
||||
assert.equal(r.fired, true);
|
||||
assert.match(r.reason, /^wedged_/);
|
||||
assert.equal(getTriggerState().inFlight, true);
|
||||
|
||||
// Wait for the in-flight promise to settle.
|
||||
await new Promise((res) => setTimeout(res, 20));
|
||||
assert.equal(getTriggerState().inFlight, false);
|
||||
assert.ok(ctx.advisorRecommendation, "recommendation cached");
|
||||
assert.equal(ctx.advisorRecommendation.skillId, "survive.flee");
|
||||
assert.equal(ctx.advisorRecommendation.usage.total, 1540);
|
||||
});
|
||||
} finally { restore(); }
|
||||
});
|
||||
|
||||
test("tickAdvisor: cooldown blocks second trigger right after", async () => {
|
||||
const restore = stubFetch({ action: "continue", rationale: "ok" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
resetAdvisor();
|
||||
const ctx = {
|
||||
recentSkillIds: ["explore.far"],
|
||||
lastSignificantMoveAt: Date.now() - 120_000,
|
||||
};
|
||||
const r1 = tickAdvisor(ctx, { plannedSkillId: "explore.far" });
|
||||
assert.equal(r1.fired, true);
|
||||
await new Promise((res) => setTimeout(res, 20));
|
||||
const r2 = tickAdvisor(ctx, { plannedSkillId: "explore.far" });
|
||||
assert.equal(r2.fired, false);
|
||||
assert.equal(r2.reason, "cooldown");
|
||||
});
|
||||
} finally { restore(); }
|
||||
});
|
||||
|
||||
test("consumeFreshRecommendation: returns + clears switch_skill recommendation", () => {
|
||||
_resetForTest();
|
||||
const ctx = {
|
||||
advisorRecommendation: {
|
||||
at: Date.now(),
|
||||
action: "switch_skill",
|
||||
skillId: "survive.flee",
|
||||
rationale: "x",
|
||||
},
|
||||
};
|
||||
const r = consumeFreshRecommendation(ctx);
|
||||
assert.ok(r);
|
||||
assert.equal(r.skillId, "survive.flee");
|
||||
assert.equal(ctx.advisorRecommendation, null);
|
||||
});
|
||||
|
||||
test("consumeFreshRecommendation: stale (> TTL) recommendation dropped", () => {
|
||||
_resetForTest();
|
||||
const ctx = {
|
||||
advisorRecommendation: {
|
||||
at: Date.now() - RECOMMENDATION_TTL_MS - 1000,
|
||||
action: "switch_skill",
|
||||
skillId: "survive.flee",
|
||||
},
|
||||
};
|
||||
const r = consumeFreshRecommendation(ctx);
|
||||
assert.equal(r, null);
|
||||
assert.equal(ctx.advisorRecommendation, null);
|
||||
});
|
||||
|
||||
test("consumeFreshRecommendation: continue/wait recommendations are not consumed for skill swap", () => {
|
||||
_resetForTest();
|
||||
const ctx = {
|
||||
advisorRecommendation: { at: Date.now(), action: "continue", rationale: "ok" },
|
||||
};
|
||||
const r = consumeFreshRecommendation(ctx);
|
||||
assert.equal(r, null);
|
||||
// stays cached for telemetry
|
||||
assert.ok(ctx.advisorRecommendation);
|
||||
});
|
||||
@@ -0,0 +1,203 @@
|
||||
// Fast tactical advisor — second LLM tier, parallel to Pi.
|
||||
//
|
||||
// Pi (the CLI coach) is great for deep post-mortems and 30-min reflection,
|
||||
// but it's slow (5-15s) and rate-limited. When the reflex detects the bot
|
||||
// is wedged, stuck, or just took an environment shock (forced teleport,
|
||||
// HP plunge, hostile spawn), we want a sub-2-second "what do I do?"
|
||||
// answer from a cheap, hosted model. That's this module.
|
||||
//
|
||||
// In rc.1 this is a scaffold: complete() + advise() + rate-limiting +
|
||||
// integration tests, but no auto-trigger from the reflex yet. rc.3 wires
|
||||
// the trigger paths (awareness layer) into here.
|
||||
//
|
||||
// The advisor MUST return a JSON shape whose `prefer_skill` field is a
|
||||
// real, registered skill id — anything else is rejected. The system
|
||||
// prompt embeds the live registry so the model has the source of truth.
|
||||
|
||||
import { complete, isAvailable as llmAvailable } from "../llm/provider.js";
|
||||
import { isRegistered, skillRegistryPrompt } from "../skill-registry.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
const HOURLY_BUDGET = 6;
|
||||
const COOLDOWN_MS = 30_000;
|
||||
|
||||
let _callTimes = [];
|
||||
let _lastCallAt = 0;
|
||||
let _tokensIn = 0;
|
||||
let _tokensOut = 0;
|
||||
let _calls = 0;
|
||||
|
||||
export function isAvailable() {
|
||||
return llmAvailable();
|
||||
}
|
||||
|
||||
export function getUsageSnapshot() {
|
||||
const now = Date.now();
|
||||
const hourAgo = now - 3600_000;
|
||||
const recentCalls = _callTimes.filter((t) => t > hourAgo).length;
|
||||
return {
|
||||
callsLastHour: recentCalls,
|
||||
callsTotal: _calls,
|
||||
tokensInTotal: _tokensIn,
|
||||
tokensOutTotal: _tokensOut,
|
||||
hourlyBudget: HOURLY_BUDGET,
|
||||
};
|
||||
}
|
||||
|
||||
export function _resetForTest() {
|
||||
_callTimes = [];
|
||||
_lastCallAt = 0;
|
||||
_tokensIn = 0;
|
||||
_tokensOut = 0;
|
||||
_calls = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* advise({ snapshot, reason, recentSkillIds, lessonsTail }) →
|
||||
* { ok: true, action: 'switch_skill'|'continue'|'wait', skillId?, rationale, raw, latencyMs }
|
||||
* | { ok: false, code, detail, latencyMs }
|
||||
*
|
||||
* `reason` is a free-text trigger ("wedged_60s", "forced_move",
|
||||
* "hp_plunge", "stuck_3_dispatches"). It goes verbatim into the prompt
|
||||
* so the model can tailor its advice.
|
||||
*/
|
||||
export async function advise({
|
||||
snapshot,
|
||||
reason = "unknown",
|
||||
recentSkillIds = [],
|
||||
lessonsTail = [],
|
||||
activeNeed = null,
|
||||
force = false,
|
||||
} = {}) {
|
||||
if (!isAvailable()) {
|
||||
return { ok: false, code: "not_configured", detail: "set TIMEWEB_API_KEY", latencyMs: 0 };
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
_callTimes = _callTimes.filter((t) => t > now - 3600_000);
|
||||
if (!force && _callTimes.length >= HOURLY_BUDGET) {
|
||||
return { ok: false, code: "budget_exhausted", detail: `${_callTimes.length}/${HOURLY_BUDGET} per hour`, latencyMs: 0 };
|
||||
}
|
||||
if (!force && now - _lastCallAt < COOLDOWN_MS) {
|
||||
return { ok: false, code: "cooldown", detail: `${Math.round((COOLDOWN_MS - (now - _lastCallAt)) / 1000)}s`, latencyMs: 0 };
|
||||
}
|
||||
|
||||
const system = buildSystemPrompt();
|
||||
const user = buildUserPrompt({ snapshot, reason, recentSkillIds, lessonsTail, activeNeed });
|
||||
|
||||
_callTimes.push(now);
|
||||
_lastCallAt = now;
|
||||
|
||||
const res = await complete({ system, user, json: true });
|
||||
_calls += 1;
|
||||
if (res.usage) {
|
||||
_tokensIn += res.usage.in;
|
||||
_tokensOut += res.usage.out;
|
||||
}
|
||||
if (!res.ok) {
|
||||
warn("advisor", `complete failed: ${res.code} (${res.detail})`);
|
||||
return { ok: false, code: res.code, detail: res.detail, latencyMs: res.latencyMs };
|
||||
}
|
||||
|
||||
const parsed = res.text;
|
||||
if (!parsed || typeof parsed !== "object") {
|
||||
return { ok: false, code: "bad_shape", detail: "no object in reply", latencyMs: res.latencyMs };
|
||||
}
|
||||
|
||||
const action = String(parsed.action ?? "").toLowerCase();
|
||||
const skillId = parsed.skill_id ?? parsed.prefer_skill ?? null;
|
||||
const rationale = parsed.rationale ?? parsed.reason ?? "";
|
||||
|
||||
if (action === "switch_skill") {
|
||||
if (!skillId || !isRegistered(skillId)) {
|
||||
warn("advisor", `rejected hallucinated skill "${skillId}"`);
|
||||
return {
|
||||
ok: false,
|
||||
code: "hallucinated_skill",
|
||||
detail: skillId ?? "(null)",
|
||||
rationale,
|
||||
raw: parsed,
|
||||
latencyMs: res.latencyMs,
|
||||
usage: res.usage,
|
||||
};
|
||||
}
|
||||
info("advisor", `switch_skill → ${skillId} (${rationale.slice(0, 80)})`);
|
||||
return {
|
||||
ok: true,
|
||||
action: "switch_skill",
|
||||
skillId,
|
||||
rationale,
|
||||
raw: parsed,
|
||||
latencyMs: res.latencyMs,
|
||||
usage: res.usage,
|
||||
};
|
||||
}
|
||||
|
||||
if (action === "continue" || action === "wait") {
|
||||
info("advisor", `${action} (${rationale.slice(0, 80)})`);
|
||||
return { ok: true, action, rationale, raw: parsed, latencyMs: res.latencyMs, usage: res.usage };
|
||||
}
|
||||
|
||||
return { ok: false, code: "bad_action", detail: action || "missing", raw: parsed, latencyMs: res.latencyMs, usage: res.usage };
|
||||
}
|
||||
|
||||
function buildSystemPrompt() {
|
||||
return [
|
||||
"You are the tactical advisor for pepa, an autonomous Minecraft survival bot.",
|
||||
"You are called when the bot's reflex layer detects something wrong (wedged, stuck,",
|
||||
"forced move, HP plunge). Your job: produce a single fast decision.",
|
||||
"",
|
||||
"Reply STRICTLY with a JSON object:",
|
||||
'{',
|
||||
' "action": "switch_skill" | "continue" | "wait",',
|
||||
' "skill_id": "<registered skill id or null>",',
|
||||
' "rationale": "<≤25 words explaining why>"',
|
||||
'}',
|
||||
"",
|
||||
"Rules:",
|
||||
'- "switch_skill" REQUIRES skill_id to be one of the registered ids below.',
|
||||
'- "continue" means current skill is fine, just give it more time.',
|
||||
'- "wait" means stop dispatching for ~10s (e.g. waiting for night to pass).',
|
||||
'- If unsure, return "continue".',
|
||||
"",
|
||||
skillRegistryPrompt({ limit: 1800 }),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function buildUserPrompt({ snapshot, reason, recentSkillIds, lessonsTail, activeNeed }) {
|
||||
const pos = snapshot?.position;
|
||||
const inv = snapshot?.inventory ? Object.keys(snapshot.inventory).slice(0, 10).join(", ") : "(empty)";
|
||||
const recent = (recentSkillIds ?? []).slice(-8).join(" → ") || "(none)";
|
||||
const lessons = (lessonsTail ?? []).slice(0, 4).map((l) => ` - ${l.text ?? l}`).join("\n");
|
||||
const needLine = activeNeed
|
||||
? `L${activeNeed.need.level} ${activeNeed.need.id} (${activeNeed.need.title}) — manifesto wants ${activeNeed.skillId}`
|
||||
: "(no active need)";
|
||||
const hostile = snapshot?.closestHostile
|
||||
? `${snapshot.closestHostile.name}@${snapshot.closestHostile.distance}b`
|
||||
: "(none)";
|
||||
|
||||
return [
|
||||
`Trigger: ${reason}`,
|
||||
`Position: ${pos ? `(${Math.round(pos.x)}, ${Math.round(pos.y)}, ${Math.round(pos.z)})` : "?"}`,
|
||||
`HP: ${snapshot?.health ?? "?"} food: ${snapshot?.food ?? "?"} day: ${snapshot?.isDay ? "yes" : "no"}`,
|
||||
`Active need (Maslow ladder): ${needLine}`,
|
||||
`Closest hostile: ${hostile}`,
|
||||
`Active skill: ${snapshot?.activeSkill ?? "(idle)"}`,
|
||||
`Recent dispatches: ${recent}`,
|
||||
`Inventory keys: ${inv}`,
|
||||
`Nearby threats: ${formatThreats(snapshot?.threats)}`,
|
||||
`No-progress reason: ${snapshot?.noProgressReason ?? "(none)"}`,
|
||||
"",
|
||||
lessons ? `Relevant lessons:\n${lessons}\n` : "",
|
||||
"What should the bot do RIGHT NOW? Return the JSON decision.",
|
||||
"Prefer a skill that helps satisfy the active need unless an emergency forces another action.",
|
||||
].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
function formatThreats(threats) {
|
||||
if (!Array.isArray(threats) || threats.length === 0) return "(none)";
|
||||
return threats.slice(0, 3).map((t) => `${t.name ?? "?"}@${Math.round(t.distance ?? 0)}m`).join(", ");
|
||||
}
|
||||
|
||||
// Test exports
|
||||
export const __testing = { buildSystemPrompt, buildUserPrompt, formatThreats, HOURLY_BUDGET, COOLDOWN_MS };
|
||||
@@ -0,0 +1,164 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { advise, isAvailable, _resetForTest, __testing } from "./fast-advisor.js";
|
||||
|
||||
const API_KEY = "TIMEWEB_API_KEY";
|
||||
const MODEL = "TIMEWEB_MODEL";
|
||||
const BASE = "TIMEWEB_BASE_URL";
|
||||
|
||||
function withEnv(env, fn) {
|
||||
const prev = {};
|
||||
for (const k of Object.keys(env)) {
|
||||
prev[k] = process.env[k];
|
||||
if (env[k] === undefined) delete process.env[k];
|
||||
else process.env[k] = env[k];
|
||||
}
|
||||
return Promise.resolve(fn()).finally(() => {
|
||||
for (const [k, v] of Object.entries(prev)) {
|
||||
if (v === undefined) delete process.env[k];
|
||||
else process.env[k] = v;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function stubFetch(reply) {
|
||||
const calls = [];
|
||||
const orig = globalThis.fetch;
|
||||
globalThis.fetch = async (url, opts) => {
|
||||
calls.push({ url, opts });
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
choices: [{ message: { content: typeof reply === "string" ? reply : JSON.stringify(reply) } }],
|
||||
}),
|
||||
};
|
||||
};
|
||||
return { calls, restore() { globalThis.fetch = orig; } };
|
||||
}
|
||||
|
||||
test("advise: not_configured without API key", async () => {
|
||||
await withEnv({ [API_KEY]: undefined }, async () => {
|
||||
_resetForTest();
|
||||
const res = await advise({ reason: "stuck" });
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.code, "not_configured");
|
||||
assert.match(res.detail, /TIMEWEB_API_KEY/);
|
||||
});
|
||||
});
|
||||
|
||||
test("advise: accepts a registered skill", async () => {
|
||||
const f = stubFetch({ action: "switch_skill", skill_id: "survive.flee", rationale: "creeper close" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m", [BASE]: "https://x/v1" }, async () => {
|
||||
_resetForTest();
|
||||
const res = await advise({
|
||||
snapshot: { health: 10, food: 18, isDay: true, position: { x: 1, y: 64, z: 1 } },
|
||||
reason: "wedged_60s",
|
||||
recentSkillIds: ["explore.far", "explore.far", "explore.far"],
|
||||
force: true,
|
||||
});
|
||||
assert.equal(res.ok, true);
|
||||
assert.equal(res.action, "switch_skill");
|
||||
assert.equal(res.skillId, "survive.flee");
|
||||
assert.match(res.rationale, /creeper/);
|
||||
// system prompt should mention the live registry
|
||||
const sent = JSON.parse(f.calls[0].opts.body);
|
||||
assert.match(sent.messages[0].content, /Valid skill ids/);
|
||||
assert.match(sent.messages[0].content, /survive\.flee/);
|
||||
assert.match(sent.messages[1].content, /wedged_60s/);
|
||||
});
|
||||
} finally { f.restore(); }
|
||||
});
|
||||
|
||||
test("advise: rejects hallucinated skill id with code=hallucinated_skill", async () => {
|
||||
const f = stubFetch({ action: "switch_skill", skill_id: "relocate.surface", rationale: "fresh spot" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
const res = await advise({ reason: "loop", force: true });
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.code, "hallucinated_skill");
|
||||
assert.equal(res.detail, "relocate.surface");
|
||||
});
|
||||
} finally { f.restore(); }
|
||||
});
|
||||
|
||||
test("advise: accepts 'continue' and 'wait' without skill_id", async () => {
|
||||
const f = stubFetch({ action: "continue", rationale: "skill is making slow progress" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
const res = await advise({ reason: "tick", force: true });
|
||||
assert.equal(res.ok, true);
|
||||
assert.equal(res.action, "continue");
|
||||
});
|
||||
} finally { f.restore(); }
|
||||
});
|
||||
|
||||
test("advise: rate-limit cooldown blocks rapid calls", async () => {
|
||||
const f = stubFetch({ action: "continue", rationale: "ok" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
const r1 = await advise({ reason: "x" });
|
||||
assert.equal(r1.ok, true);
|
||||
const r2 = await advise({ reason: "y" });
|
||||
assert.equal(r2.ok, false);
|
||||
assert.equal(r2.code, "cooldown");
|
||||
});
|
||||
} finally { f.restore(); }
|
||||
});
|
||||
|
||||
test("advise: hourly budget enforced with force=true override", async () => {
|
||||
const f = stubFetch({ action: "continue", rationale: "ok" });
|
||||
try {
|
||||
await withEnv({ [API_KEY]: "k", [MODEL]: "m" }, async () => {
|
||||
_resetForTest();
|
||||
for (let i = 0; i < __testing.HOURLY_BUDGET; i++) {
|
||||
await advise({ reason: `t${i}`, force: true });
|
||||
}
|
||||
const over = await advise({ reason: "over" });
|
||||
assert.equal(over.ok, false);
|
||||
assert.equal(over.code, "budget_exhausted");
|
||||
});
|
||||
} finally { f.restore(); }
|
||||
});
|
||||
|
||||
test("buildSystemPrompt: contains registry block and JSON schema", () => {
|
||||
const sys = __testing.buildSystemPrompt();
|
||||
assert.match(sys, /switch_skill/);
|
||||
assert.match(sys, /Valid skill ids/);
|
||||
assert.match(sys, /survive\.flee/);
|
||||
});
|
||||
|
||||
test("buildUserPrompt: includes trigger and recent skills", () => {
|
||||
const u = __testing.buildUserPrompt({
|
||||
snapshot: { health: 4, food: 3, isDay: false, position: { x: 10, y: 65, z: 10 }, activeSkill: "explore.far" },
|
||||
reason: "hp_plunge",
|
||||
recentSkillIds: ["explore.far", "explore.far"],
|
||||
lessonsTail: [{ text: "do not fight at night" }],
|
||||
});
|
||||
assert.match(u, /hp_plunge/);
|
||||
assert.match(u, /HP: 4/);
|
||||
assert.match(u, /Recent dispatches: explore\.far → explore\.far/);
|
||||
assert.match(u, /do not fight at night/);
|
||||
});
|
||||
|
||||
test("formatThreats: empty / formatted", () => {
|
||||
assert.equal(__testing.formatThreats(undefined), "(none)");
|
||||
assert.equal(__testing.formatThreats([]), "(none)");
|
||||
assert.equal(
|
||||
__testing.formatThreats([{ name: "zombie", distance: 4.3 }, { name: "creeper", distance: 7 }]),
|
||||
"zombie@4m, creeper@7m",
|
||||
);
|
||||
});
|
||||
|
||||
test("isAvailable mirrors provider availability", async () => {
|
||||
await withEnv({ [API_KEY]: undefined }, async () => {
|
||||
assert.equal(isAvailable(), false);
|
||||
});
|
||||
await withEnv({ [API_KEY]: "k" }, async () => {
|
||||
assert.equal(isAvailable(), true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
// Shared helper for the slow-analytical coach loops (postmortem.js,
|
||||
// reflect.js). Replaces the old askPi-based subprocess path with a
|
||||
// direct TimeWeb / OpenAI-compatible HTTP call.
|
||||
//
|
||||
// Why this split: postmortem and reflect each took 5-15s via Pi CLI
|
||||
// (with its own subprocess + auth + sometimes a fresh MC connection)
|
||||
// and were rate-limited by the subscription. Now they take 5-15s via
|
||||
// the same TimeWeb endpoint the fast-advisor uses — but they're
|
||||
// analytical, NOT tactical, so they ask for a different prompt shape
|
||||
// and a longer reply.
|
||||
//
|
||||
// The Pi CLI is no longer driven from background timers. It remains
|
||||
// available for manual operator commands.
|
||||
|
||||
import { complete } from "../llm/provider.js";
|
||||
import { warn } from "../log.js";
|
||||
|
||||
const ANALYTICAL_TIMEOUT_MS = 30_000;
|
||||
|
||||
/**
|
||||
* askAnalytical({ system, user, json }) → text|object|null
|
||||
*
|
||||
* Higher-timeout, lower-temperature companion to fast-advisor's
|
||||
* complete(). Returns just the parsed text/object on success or null
|
||||
* on failure (so callers can keep their old "no reply" branch).
|
||||
*
|
||||
* Token usage is logged via the underlying provider — no extra
|
||||
* accounting here.
|
||||
*/
|
||||
export async function askAnalytical({ system, user, json = true, timeoutMs } = {}) {
|
||||
const res = await complete({
|
||||
system,
|
||||
user,
|
||||
json,
|
||||
timeoutMs: timeoutMs ?? ANALYTICAL_TIMEOUT_MS,
|
||||
});
|
||||
if (!res.ok) {
|
||||
warn("coach-llm", `analytical call failed: ${res.code} (${res.detail})`);
|
||||
return null;
|
||||
}
|
||||
return res.text;
|
||||
}
|
||||
|
||||
export { ANALYTICAL_TIMEOUT_MS };
|
||||
+94
-59
@@ -25,18 +25,22 @@ import {
|
||||
record as recordLesson,
|
||||
poiNearby,
|
||||
recordPOI,
|
||||
createImprovementRequest,
|
||||
} from "../knowledge/index.js";
|
||||
import { isRegistered, skillRegistryPrompt } from "../skill-registry.js";
|
||||
import { isAvailable as llmAvailable } from "../llm/provider.js";
|
||||
import { askAnalytical } from "./llm-call.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
const COACH_INTERVAL_MS = 5 * 60 * 1000; // 5 min between coach passes
|
||||
const COACH_BATCH_MAX = 8; // up to 8 deaths per Pi call
|
||||
const COACH_PI_BUDGET_PER_HOUR = 3; // ≤ 3 Pi calls/hour
|
||||
const COACH_BATCH_MAX = 8; // up to 8 deaths per LLM call
|
||||
const COACH_BUDGET_PER_HOUR = 3; // ≤ 3 analytical LLM calls/hour
|
||||
const COACH_COOLDOWN_MS = 12 * 60 * 1000; // 12 min between calls
|
||||
const RECENT_CHAT_TAIL = 6;
|
||||
const SCENARIO_TAIL = 12;
|
||||
|
||||
let _attached = null;
|
||||
let _piCallTimes = [];
|
||||
let _llmCallTimes = [];
|
||||
let _coachTimer = null;
|
||||
let _lastInventory = null;
|
||||
|
||||
@@ -75,17 +79,18 @@ export function attach(bot, ctx = {}) {
|
||||
}
|
||||
});
|
||||
|
||||
// Start the periodic Pi-coach drain loop.
|
||||
if (ctx.askPi && !_coachTimer) {
|
||||
// v0.3.0 — postmortem analysis runs through TimeWeb (the fast LLM
|
||||
// provider). Pi CLI no longer drives this loop. The drain timer
|
||||
// fires regardless of whether TimeWeb is configured; drainOnce()
|
||||
// short-circuits when the LLM is unavailable.
|
||||
if (!_coachTimer) {
|
||||
_coachTimer = setInterval(() => {
|
||||
drainOnce({ askPi: ctx.askPi, stateDir: ctx.stateDir }).catch((e) =>
|
||||
drainOnce({ stateDir: ctx.stateDir }).catch((e) =>
|
||||
warn("coach", `drain error: ${e?.message ?? e}`),
|
||||
);
|
||||
}, COACH_INTERVAL_MS);
|
||||
_coachTimer.unref?.();
|
||||
info("coach", `attached; drain every ${COACH_INTERVAL_MS / 1000}s`);
|
||||
} else {
|
||||
info("coach", "attached; Pi not provided, deaths captured without postmortem analysis");
|
||||
info("coach", `attached; drain every ${COACH_INTERVAL_MS / 1000}s${llmAvailable() ? " (TimeWeb)" : " (LLM disabled — deaths captured only)"}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,54 +253,65 @@ function readJournalNearby(stateDir, pos, radius) {
|
||||
* Rate-limited: at most COACH_PI_BUDGET_PER_HOUR calls/hour, with
|
||||
* COACH_COOLDOWN_MS gap between calls.
|
||||
*/
|
||||
export async function drainOnce({ askPi, stateDir, force = false } = {}) {
|
||||
export async function drainOnce({ stateDir, force = false, askAnalyticalFn = askAnalytical } = {}) {
|
||||
if (!knowledgeAvailable()) return { ok: false, reason: "knowledge unavailable" };
|
||||
if (!askPi) return { ok: false, reason: "no askPi" };
|
||||
if (!llmAvailable()) return { ok: false, reason: "llm not configured" };
|
||||
|
||||
const now = Date.now();
|
||||
const hourAgo = now - 60 * 60 * 1000;
|
||||
_piCallTimes = _piCallTimes.filter((t) => t > hourAgo);
|
||||
if (!force && _piCallTimes.length >= COACH_PI_BUDGET_PER_HOUR) {
|
||||
return { ok: false, reason: "hourly budget exhausted", calls: _piCallTimes.length };
|
||||
_llmCallTimes = _llmCallTimes.filter((t) => t > hourAgo);
|
||||
if (!force && _llmCallTimes.length >= COACH_BUDGET_PER_HOUR) {
|
||||
return { ok: false, reason: "hourly budget exhausted", calls: _llmCallTimes.length };
|
||||
}
|
||||
if (!force && _piCallTimes.length > 0 && now - _piCallTimes[_piCallTimes.length - 1] < COACH_COOLDOWN_MS) {
|
||||
if (!force && _llmCallTimes.length > 0 && now - _llmCallTimes[_llmCallTimes.length - 1] < COACH_COOLDOWN_MS) {
|
||||
return { ok: false, reason: "cooldown" };
|
||||
}
|
||||
|
||||
const pending = unanalysedDeaths({ limit: COACH_BATCH_MAX });
|
||||
if (pending.length === 0) return { ok: true, analysed: 0 };
|
||||
|
||||
const prompt = buildPrompt(pending);
|
||||
_piCallTimes.push(now);
|
||||
const { system, user } = buildPrompt(pending);
|
||||
_llmCallTimes.push(now);
|
||||
|
||||
const reply = await askPiOnce({ askPi, prompt });
|
||||
if (!reply) return { ok: false, reason: "no reply" };
|
||||
|
||||
const parsed = extractJson(reply);
|
||||
if (!parsed) {
|
||||
warn("coach", "Pi reply was not parseable JSON");
|
||||
return { ok: false, reason: "bad reply" };
|
||||
}
|
||||
const parsed = await askAnalyticalFn({ system, user, json: true });
|
||||
if (!parsed) return { ok: false, reason: "no reply" };
|
||||
const reply = typeof parsed === "string" ? parsed : JSON.stringify(parsed);
|
||||
|
||||
let lessonsCount = 0;
|
||||
let rejectedPreferCount = 0;
|
||||
for (const item of asArray(parsed.lessons ?? parsed)) {
|
||||
if (!item || !item.lesson) continue;
|
||||
// Skill ids referenced by Pi must be in the live registry.
|
||||
// Mode names (e.g. "night_shelter") are tolerated at write time and
|
||||
// translated at consult time by advice.js#normalisePreferSkill.
|
||||
let preferSkill = item.prefer_skill ?? null;
|
||||
if (preferSkill && !isRegistered(preferSkill) && !isLikelyModeName(preferSkill)) {
|
||||
rejectedPreferCount += 1;
|
||||
preferSkill = null;
|
||||
}
|
||||
let avoidSkill = item.avoid_skill ?? null;
|
||||
if (avoidSkill && !isRegistered(avoidSkill) && !isLikelyModeName(avoidSkill)) {
|
||||
avoidSkill = null;
|
||||
}
|
||||
recordLesson({
|
||||
text: item.lesson,
|
||||
category: item.category ?? "survival",
|
||||
triggerSkill: item.trigger_skill ?? null,
|
||||
triggerHostile: item.trigger_hostile ?? null,
|
||||
triggerSituation: item.trigger_situation ?? null,
|
||||
avoidSkill: item.avoid_skill ?? null,
|
||||
preferSkill: item.prefer_skill ?? null,
|
||||
avoidSkill,
|
||||
preferSkill,
|
||||
confidence: clamp(Number(item.confidence) || 0.6, 0.1, 0.95),
|
||||
source: "pi-coach",
|
||||
sourceRef: item.source_ref ?? null,
|
||||
});
|
||||
lessonsCount += 1;
|
||||
}
|
||||
if (rejectedPreferCount > 0) {
|
||||
warn("coach", `dropped prefer_skill from ${rejectedPreferCount} lessons (not in registry)`);
|
||||
}
|
||||
|
||||
// Write one postmortem per death; if Pi grouped them, share the same lesson.
|
||||
// Write one postmortem per death; if grouped, share the same lesson.
|
||||
const groupLesson = parsed.lessons?.[0]?.lesson ?? parsed.lesson ?? null;
|
||||
for (const d of pending) {
|
||||
insertPostmortem({
|
||||
@@ -304,13 +320,42 @@ export async function drainOnce({ askPi, stateDir, force = false } = {}) {
|
||||
lesson: groupLesson,
|
||||
nextAction: parsed.next_action ?? null,
|
||||
rawResponse: reply.slice(0, 4000),
|
||||
source: "pi",
|
||||
source: "timeweb",
|
||||
});
|
||||
markDeathAnalysed(d.id);
|
||||
}
|
||||
|
||||
info("coach", `drain: analysed ${pending.length} deaths → ${lessonsCount} lessons`);
|
||||
return { ok: true, analysed: pending.length, lessons: lessonsCount };
|
||||
// v0.3.0 — record any improvement requests the LLM flagged. The
|
||||
// LLM is encouraged to do this when the deaths point to a missing
|
||||
// skill or feature; the operator reads scripts/list-improvements.js
|
||||
// and decides what to implement.
|
||||
let improvementsCount = 0;
|
||||
for (const imp of asArray(parsed.improvements ?? [])) {
|
||||
if (!imp?.title) continue;
|
||||
createImprovementRequest({
|
||||
source: "postmortem",
|
||||
category: imp.category ?? "skill",
|
||||
title: String(imp.title).slice(0, 120),
|
||||
description: imp.description ?? null,
|
||||
context: { death_ids: pending.map((d) => d.id), cause: parsed.cause },
|
||||
priority: imp.priority ?? 3,
|
||||
});
|
||||
improvementsCount += 1;
|
||||
}
|
||||
|
||||
info("coach", `drain: analysed ${pending.length} deaths → ${lessonsCount} lessons, ${improvementsCount} improvement requests`);
|
||||
return { ok: true, analysed: pending.length, lessons: lessonsCount, improvements: improvementsCount };
|
||||
}
|
||||
|
||||
// Mode names from runtime/modes.js (advice.js#MODE_TO_SKILL) — we accept
|
||||
// these at write time because advice.js maps them to real skills at consult.
|
||||
const KNOWN_MODE_NAMES = new Set([
|
||||
"self_preservation", "night_shelter", "hunger", "shelter",
|
||||
"flee", "sleep", "eat", "tunnel_out", "tunnel-out", "explore", "wander",
|
||||
]);
|
||||
function isLikelyModeName(s) {
|
||||
if (!s || typeof s !== "string") return false;
|
||||
return KNOWN_MODE_NAMES.has(s.toLowerCase().trim());
|
||||
}
|
||||
|
||||
function buildPrompt(deaths) {
|
||||
@@ -329,45 +374,35 @@ function buildPrompt(deaths) {
|
||||
].filter(Boolean).join("\n");
|
||||
}).join("\n\n");
|
||||
|
||||
return [
|
||||
const system = [
|
||||
"You are reviewing recent deaths of an autonomous Minecraft survival bot (pepa).",
|
||||
"The bot is trying to gather wood, craft tools, build a small village, and survive nights.",
|
||||
"It's currently dying repeatedly. Your job: extract 1-3 short, generalised lessons it can apply on respawn.",
|
||||
"Your job: extract 1-3 short, generalised lessons + flag any missing-skill gaps.",
|
||||
"",
|
||||
"DEATHS:",
|
||||
summary,
|
||||
skillRegistryPrompt({ limit: 1800 }),
|
||||
"",
|
||||
"Reply with ONE JSON object (no prose, no markdown fences):",
|
||||
"Reply with ONE JSON object (no markdown fences):",
|
||||
'{ "cause": "<short>", "next_action": "<one-sentence directive>",',
|
||||
' "lessons": [',
|
||||
' { "lesson": "...", "category": "combat|pathing|crafting|survival|social",',
|
||||
' "trigger_skill": "<skill id or null>",',
|
||||
' "trigger_hostile": "<mob name or null>",',
|
||||
' "avoid_skill": "<skill to NOT dispatch or null>",',
|
||||
' "prefer_skill": "<alternative skill or null>",',
|
||||
' "confidence": 0.7 }',
|
||||
' ] }',
|
||||
' "avoid_skill": "<registered skill id to NOT dispatch, or null>",',
|
||||
' "prefer_skill": "<registered skill id to use instead, or null>",',
|
||||
' "confidence": 0.7 } ],',
|
||||
' "improvements": [',
|
||||
' { "title": "<≤80 chars: what skill/feature is missing>",',
|
||||
' "description": "<why current registry doesn\'t cover this; concrete example>",',
|
||||
' "category": "skill|tuning|perception|planning|social|other",',
|
||||
' "priority": 1 } ] }',
|
||||
"",
|
||||
"Keep each lesson under 30 words. Be specific (e.g., \"attack creeper with fists\" rather than \"don't fight\").",
|
||||
"Keep each lesson under 30 words. Be specific.",
|
||||
"CRITICAL: avoid_skill and prefer_skill MUST be one of the registered ids above, or null.",
|
||||
"Use 'improvements' ONLY when a death is plausibly caused by the bot lacking a skill that doesn't exist in the registry (e.g. 'no skill to craft iron armor'). Skip it otherwise.",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function askPiOnce({ askPi, prompt }) {
|
||||
return new Promise((resolve) => {
|
||||
let buf = "";
|
||||
try {
|
||||
askPi({
|
||||
prompt,
|
||||
onChunk: ({ stream, text }) => {
|
||||
if (stream === "stdout") buf += text;
|
||||
},
|
||||
onDone: () => resolve(buf),
|
||||
});
|
||||
} catch (e) {
|
||||
warn("coach", `askPi failed: ${e?.message ?? e}`);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
const user = `DEATHS:\n${summary}`;
|
||||
return { system, user };
|
||||
}
|
||||
|
||||
function extractJson(text) {
|
||||
@@ -390,4 +425,4 @@ function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)); }
|
||||
function safeParse(s) { try { return JSON.parse(s); } catch { return null; } }
|
||||
|
||||
// Test-only exports
|
||||
export const __testing = { captureDeath, buildPrompt, extractJson, inferCause };
|
||||
export const __testing = { captureDeath, buildPrompt, extractJson, inferCause, isLikelyModeName, KNOWN_MODE_NAMES };
|
||||
|
||||
@@ -39,16 +39,18 @@ test("extractJson: tolerates fences and surrounding text", () => {
|
||||
assert.equal(extractJson(""), null);
|
||||
});
|
||||
|
||||
test("buildPrompt: includes all death rows and JSON schema hint", () => {
|
||||
test("buildPrompt: returns {system, user}, includes all death rows + improvements schema", () => {
|
||||
const rows = [
|
||||
{ id: 1, ts: Date.now(), x: 100, y: 64, z: 200, cause: "hostile", hostile: "creeper", last_skill: "gather.logs", last_skill_code: "timeout", food_at_death: 14, context_blob: JSON.stringify({ recentScenarios: [{ skillId: "gather.logs", code: "timeout" }] }) },
|
||||
{ id: 2, ts: Date.now(), x: 102, y: 64, z: 201, cause: "hostile", hostile: "creeper", last_skill: "explore.far", last_skill_code: "done", food_at_death: 12, context_blob: null },
|
||||
];
|
||||
const prompt = buildPrompt(rows);
|
||||
assert.match(prompt, /death id=1/);
|
||||
assert.match(prompt, /death id=2/);
|
||||
assert.match(prompt, /creeper/);
|
||||
assert.match(prompt, /Reply with ONE JSON object/);
|
||||
const { system, user } = buildPrompt(rows);
|
||||
assert.match(user, /death id=1/);
|
||||
assert.match(user, /death id=2/);
|
||||
assert.match(user, /creeper/);
|
||||
assert.match(system, /Reply with ONE JSON object/);
|
||||
assert.match(system, /improvements/);
|
||||
assert.match(system, /Valid skill ids/);
|
||||
});
|
||||
|
||||
test("captureDeath: builds a row with context blob and inferred cause", () => {
|
||||
@@ -88,7 +90,7 @@ test("attach + emit('death'): inserts row in knowledge DB", async () => {
|
||||
rmSync(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("drainOnce: respects budget and parses Pi reply", async () => {
|
||||
test("drainOnce: respects budget and parses analytical LLM reply (incl. improvements)", async () => {
|
||||
const stateDir = mkdtempSync(join(tmpdir(), "pepa-coach-test-"));
|
||||
__resetForTests();
|
||||
await initKnowledge({ stateDir });
|
||||
@@ -105,7 +107,13 @@ test("drainOnce: respects budget and parses Pi reply", async () => {
|
||||
|
||||
const lessonsBefore = recall({ category: "combat" }).length;
|
||||
|
||||
const fakeReply = JSON.stringify({
|
||||
// TimeWeb path needs env vars to satisfy the llmAvailable check.
|
||||
const prevKey = process.env.TIMEWEB_API_KEY;
|
||||
const prevModel = process.env.TIMEWEB_MODEL;
|
||||
process.env.TIMEWEB_API_KEY = "test-key";
|
||||
process.env.TIMEWEB_MODEL = "test-model";
|
||||
|
||||
const fakeReply = {
|
||||
cause: "creeper_explosion_unarmed",
|
||||
next_action: "shelter at dusk",
|
||||
lessons: [{
|
||||
@@ -116,16 +124,23 @@ test("drainOnce: respects budget and parses Pi reply", async () => {
|
||||
prefer_skill: "survive.flee",
|
||||
confidence: 0.85,
|
||||
}],
|
||||
});
|
||||
const askPi = ({ onChunk, onDone }) => {
|
||||
onChunk({ stream: "stdout", text: fakeReply });
|
||||
onDone({ code: 0 });
|
||||
improvements: [
|
||||
{ title: "Add craft.shield skill", description: "No skill to craft a shield when creepers are around.", category: "skill", priority: 2 },
|
||||
],
|
||||
};
|
||||
const askAnalyticalFn = async () => fakeReply;
|
||||
|
||||
const result = await drainOnce({ stateDir, force: true, askAnalyticalFn });
|
||||
|
||||
if (prevKey === undefined) delete process.env.TIMEWEB_API_KEY;
|
||||
else process.env.TIMEWEB_API_KEY = prevKey;
|
||||
if (prevModel === undefined) delete process.env.TIMEWEB_MODEL;
|
||||
else process.env.TIMEWEB_MODEL = prevModel;
|
||||
|
||||
const result = await drainOnce({ askPi, stateDir, force: true });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.analysed, 1);
|
||||
assert.equal(result.lessons, 1);
|
||||
assert.equal(result.improvements, 1);
|
||||
|
||||
const after = recall({ hostile: "creeper", category: "combat" });
|
||||
assert.ok(after.length > lessonsBefore, "new lesson recorded");
|
||||
@@ -137,18 +152,20 @@ test("drainOnce: respects budget and parses Pi reply", async () => {
|
||||
rmSync(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("drainOnce: empty queue → ok with 0 analysed", async () => {
|
||||
test("drainOnce: skipped when LLM not configured", async () => {
|
||||
const stateDir = mkdtempSync(join(tmpdir(), "pepa-coach-test-"));
|
||||
__resetForTests();
|
||||
await initKnowledge({ stateDir });
|
||||
if (!isAvailable()) {
|
||||
assert.ok(true);
|
||||
rmSync(stateDir, { recursive: true, force: true });
|
||||
return;
|
||||
}
|
||||
const result = await drainOnce({ askPi: () => {}, stateDir, force: true });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.analysed, 0);
|
||||
const prevKey = process.env.TIMEWEB_API_KEY;
|
||||
delete process.env.TIMEWEB_API_KEY;
|
||||
const result = await drainOnce({ stateDir, force: true });
|
||||
if (prevKey !== undefined) process.env.TIMEWEB_API_KEY = prevKey;
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.reason, "llm not configured");
|
||||
closeStore();
|
||||
rmSync(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
+108
-66
@@ -15,34 +15,49 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { isAvailable as knowledgeAvailable, record as recordLesson } from "../knowledge/index.js";
|
||||
import { isAvailable as knowledgeAvailable, record as recordLesson, createImprovementRequest } from "../knowledge/index.js";
|
||||
import { isRegistered, skillRegistryPrompt } from "../skill-registry.js";
|
||||
import { pickActiveNeed } from "../manifesto/state.js";
|
||||
import { isAvailable as llmAvailable } from "../llm/provider.js";
|
||||
import { askAnalytical } from "./llm-call.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
// Mode-name allow-list, mirrors postmortem.js (advice.js maps them to
|
||||
// real skills at consult-time). Anything else is hallucination → dropped.
|
||||
const KNOWN_MODE_NAMES = new Set([
|
||||
"self_preservation", "night_shelter", "hunger", "shelter",
|
||||
"flee", "sleep", "eat", "tunnel_out", "tunnel-out", "explore", "wander",
|
||||
]);
|
||||
function isLikelyModeName(s) {
|
||||
if (!s || typeof s !== "string") return false;
|
||||
return KNOWN_MODE_NAMES.has(s.toLowerCase().trim());
|
||||
}
|
||||
|
||||
const DEFAULT_INTERVAL_MS = 30 * 60 * 1000;
|
||||
const HOURLY_BUDGET = 2;
|
||||
const HISTORY_TAIL_LINES = 80;
|
||||
|
||||
let _attached = null;
|
||||
let _timer = null;
|
||||
let _piCallTimes = [];
|
||||
let _llmCallTimes = [];
|
||||
|
||||
export function attach({ bot, stateDir, askPi, getSnapshot, intervalMs = DEFAULT_INTERVAL_MS } = {}) {
|
||||
export function attach({ bot, stateDir, getSnapshot, intervalMs = DEFAULT_INTERVAL_MS } = {}) {
|
||||
if (_attached) {
|
||||
warn("reflect", "attach called twice; ignoring");
|
||||
return;
|
||||
}
|
||||
if (!stateDir || !askPi || !getSnapshot) {
|
||||
info("reflect", "attach: missing stateDir/askPi/getSnapshot — disabled");
|
||||
if (!stateDir || !getSnapshot) {
|
||||
info("reflect", "attach: missing stateDir/getSnapshot — disabled");
|
||||
return;
|
||||
}
|
||||
_attached = { bot, stateDir, askPi, getSnapshot };
|
||||
_attached = { bot, stateDir, getSnapshot };
|
||||
_timer = setInterval(() => {
|
||||
runOnce({ stateDir, askPi, getSnapshot }).catch((e) =>
|
||||
runOnce({ stateDir, getSnapshot }).catch((e) =>
|
||||
warn("reflect", `tick err: ${e?.message ?? e}`),
|
||||
);
|
||||
}, intervalMs);
|
||||
_timer.unref?.();
|
||||
info("reflect", `attached; self-assess every ${Math.round(intervalMs / 60000)} min`);
|
||||
info("reflect", `attached; self-assess every ${Math.round(intervalMs / 60000)} min${llmAvailable() ? " (TimeWeb)" : " (LLM disabled — will skip)"}`);
|
||||
}
|
||||
|
||||
export function detach() {
|
||||
@@ -51,12 +66,13 @@ export function detach() {
|
||||
_attached = null;
|
||||
}
|
||||
|
||||
export async function runOnce({ stateDir, askPi, getSnapshot, force = false } = {}) {
|
||||
export async function runOnce({ stateDir, getSnapshot, force = false, askAnalyticalFn = askAnalytical } = {}) {
|
||||
const now = Date.now();
|
||||
const hourAgo = now - 3600_000;
|
||||
_piCallTimes = _piCallTimes.filter((t) => t > hourAgo);
|
||||
if (!force && _piCallTimes.length >= HOURLY_BUDGET) {
|
||||
return { ok: false, reason: "budget exhausted", calls: _piCallTimes.length };
|
||||
_llmCallTimes = _llmCallTimes.filter((t) => t > hourAgo);
|
||||
if (!llmAvailable()) return { ok: false, reason: "llm not configured" };
|
||||
if (!force && _llmCallTimes.length >= HOURLY_BUDGET) {
|
||||
return { ok: false, reason: "budget exhausted", calls: _llmCallTimes.length };
|
||||
}
|
||||
|
||||
const snap = getSnapshot();
|
||||
@@ -64,36 +80,64 @@ export async function runOnce({ stateDir, askPi, getSnapshot, force = false } =
|
||||
const scenarios = readScenarioTail(stateDir);
|
||||
const diary = readDiaryTail(stateDir);
|
||||
const plan = readPlan(stateDir);
|
||||
const activeNeed = pickActiveNeed(snap);
|
||||
|
||||
const prompt = buildPrompt({ snap, journal, scenarios, diary, plan });
|
||||
_piCallTimes.push(now);
|
||||
const { system, user } = buildPrompt({ snap, journal, scenarios, diary, plan, activeNeed });
|
||||
_llmCallTimes.push(now);
|
||||
|
||||
const reply = await askPiOnce({ askPi, prompt });
|
||||
if (!reply) return { ok: false, reason: "no reply" };
|
||||
|
||||
const parsed = parseReply(reply);
|
||||
if (!parsed) {
|
||||
warn("reflect", "Pi reply not parseable as JSON");
|
||||
return { ok: false, reason: "bad reply", raw: reply.slice(0, 200) };
|
||||
}
|
||||
const parsed = await askAnalyticalFn({ system, user, json: true });
|
||||
if (!parsed || typeof parsed !== "object") return { ok: false, reason: "no reply" };
|
||||
const reply = JSON.stringify(parsed);
|
||||
|
||||
const path = writeReflection(stateDir, parsed, reply);
|
||||
let rejectedPrefer = 0;
|
||||
for (const l of asArray(parsed.lessons)) {
|
||||
if (!l?.lesson) continue;
|
||||
let preferSkill = l.prefer_skill ?? null;
|
||||
if (preferSkill && !isRegistered(preferSkill) && !isLikelyModeName(preferSkill)) {
|
||||
rejectedPrefer += 1;
|
||||
preferSkill = null;
|
||||
}
|
||||
let avoidSkill = l.avoid_skill ?? null;
|
||||
if (avoidSkill && !isRegistered(avoidSkill) && !isLikelyModeName(avoidSkill)) {
|
||||
avoidSkill = null;
|
||||
}
|
||||
recordLesson({
|
||||
text: l.lesson,
|
||||
category: l.category ?? "self-improve",
|
||||
triggerSkill: l.trigger_skill ?? null,
|
||||
triggerHostile: l.trigger_hostile ?? null,
|
||||
avoidSkill: l.avoid_skill ?? null,
|
||||
preferSkill: l.prefer_skill ?? null,
|
||||
avoidSkill,
|
||||
preferSkill,
|
||||
confidence: clamp(Number(l.confidence) || 0.5, 0.1, 0.9),
|
||||
source: "pi-reflect",
|
||||
source: "timeweb-reflect",
|
||||
sourceRef: path,
|
||||
});
|
||||
}
|
||||
info("reflect", `verdict=${parsed.verdict ?? "?"} ${parsed.summary?.slice(0, 80) ?? ""} (${path ?? "no file"})`);
|
||||
return { ok: true, verdict: parsed.verdict, summary: parsed.summary, lessons: parsed.lessons ?? [] };
|
||||
if (rejectedPrefer > 0) {
|
||||
warn("reflect", `dropped prefer_skill from ${rejectedPrefer} reflection lessons (not in registry)`);
|
||||
}
|
||||
|
||||
// v0.3.0 — improvement requests from reflection. The LLM is asked
|
||||
// to flag missing skills/features when self-reflection reveals a
|
||||
// systemic gap (e.g. "I keep failing iron tools because there's no
|
||||
// craft.iron-pickaxe skill").
|
||||
let improvementsCount = 0;
|
||||
for (const imp of asArray(parsed.improvements ?? [])) {
|
||||
if (!imp?.title) continue;
|
||||
createImprovementRequest({
|
||||
source: "reflect",
|
||||
category: imp.category ?? "skill",
|
||||
title: String(imp.title).slice(0, 120),
|
||||
description: imp.description ?? null,
|
||||
context: { verdict: parsed.verdict, reflection_path: path },
|
||||
priority: imp.priority ?? 3,
|
||||
});
|
||||
improvementsCount += 1;
|
||||
}
|
||||
|
||||
info("reflect", `verdict=${parsed.verdict ?? "?"} ${parsed.summary?.slice(0, 80) ?? ""} lessons=${(parsed.lessons ?? []).length} improvements=${improvementsCount} (${path ?? "no file"})`);
|
||||
return { ok: true, verdict: parsed.verdict, summary: parsed.summary, lessons: parsed.lessons ?? [], improvements: improvementsCount };
|
||||
}
|
||||
|
||||
function readJournalTail(stateDir) {
|
||||
@@ -128,19 +172,51 @@ function readPlan(stateDir) {
|
||||
try { return readFileSync(f, "utf8"); } catch { return ""; }
|
||||
}
|
||||
|
||||
function buildPrompt({ snap, journal, scenarios, diary, plan }) {
|
||||
function buildPrompt({ snap, journal, scenarios, diary, plan, activeNeed }) {
|
||||
const pos = snap?.position;
|
||||
const inv = snap?.inventory ? Object.keys(snap.inventory).slice(0, 12).join(", ") : "(empty)";
|
||||
const lastResult = snap?.lastResult ? JSON.stringify(snap.lastResult).slice(0, 200) : "(none)";
|
||||
return [
|
||||
const needLine = activeNeed
|
||||
? `L${activeNeed.need.level} ${activeNeed.need.id} → ${activeNeed.skillId} (${activeNeed.need.title})`
|
||||
: "(satisfied through L10 / no active need)";
|
||||
|
||||
const system = [
|
||||
"You are pepa, an autonomous Minecraft survival bot, reflecting on your own progress.",
|
||||
"Look at the last ~30 minutes of activity below. Answer honestly: are you actually making progress, or stuck in a loop?",
|
||||
"Answer honestly: are you making progress, stuck in a loop, or facing a structural gap?",
|
||||
"",
|
||||
skillRegistryPrompt({ limit: 1800 }),
|
||||
"",
|
||||
"Reply with ONE JSON object (no markdown fences, no prose):",
|
||||
'{',
|
||||
' "verdict": "progress" | "loop" | "recovering" | "idle" | "emergency",',
|
||||
' "summary": "<2-3 sentence honest assessment in Russian>",',
|
||||
' "next_action": "<one-sentence directive>",',
|
||||
' "lessons": [',
|
||||
' { "lesson": "<≤30 words, generalised rule>",',
|
||||
' "category": "combat|pathing|crafting|survival|self-improve",',
|
||||
' "trigger_skill": "<skill id or null>",',
|
||||
' "trigger_hostile": "<mob name or null>",',
|
||||
' "avoid_skill": "<registered skill id or null>",',
|
||||
' "prefer_skill": "<registered skill id or null>",',
|
||||
' "confidence": 0.6 } ],',
|
||||
' "improvements": [',
|
||||
' { "title": "<≤80 chars: structural gap (e.g. \'No craft.iron-pickaxe skill\')>",',
|
||||
' "description": "<concrete example showing why no registered skill helps>",',
|
||||
' "category": "skill|tuning|perception|planning|social|other",',
|
||||
' "priority": 1 } ]',
|
||||
'}',
|
||||
"",
|
||||
"CRITICAL: avoid_skill and prefer_skill MUST be one of the registered ids above, or null.",
|
||||
"Use 'improvements' ONLY when you identify a structural gap — a missing skill or feature that would unblock a class of situations. Skip it otherwise.",
|
||||
].join("\n");
|
||||
|
||||
const user = [
|
||||
"## Current state",
|
||||
`- position: ${pos ? `(${Math.round(pos.x)}, ${Math.round(pos.y)}, ${Math.round(pos.z)})` : "?"}`,
|
||||
`- hp: ${snap?.health ?? "?"} food: ${snap?.food ?? "?"} day: ${snap?.isDay ? "yes" : "no"}`,
|
||||
`- runtimeState: ${snap?.runtimeState ?? "?"}`,
|
||||
`- activeSkill: ${snap?.activeSkill ?? "(idle)"}`,
|
||||
`- activeNeed (Maslow ladder L0-L10): ${needLine}`,
|
||||
`- currentMilestone: ${snap?.currentMilestone ?? "?"}`,
|
||||
`- noProgressReason: ${snap?.noProgressReason ?? "(none)"}`,
|
||||
`- lastResult: ${lastResult}`,
|
||||
@@ -165,27 +241,9 @@ function buildPrompt({ snap, journal, scenarios, diary, plan }) {
|
||||
"```",
|
||||
journal.slice(-20).join("\n"),
|
||||
"```",
|
||||
"",
|
||||
"Reply with ONE JSON object (no markdown fences, no prose):",
|
||||
'{',
|
||||
' "verdict": "progress" | "loop" | "recovering" | "idle" | "emergency",',
|
||||
' "summary": "<2-3 sentence honest assessment in Russian>",',
|
||||
' "next_action": "<one-sentence directive for what to do next>",',
|
||||
' "lessons": [',
|
||||
' { "lesson": "<≤30 words, generalised rule>",',
|
||||
' "category": "combat|pathing|crafting|survival|self-improve",',
|
||||
' "trigger_skill": "<skill id or null>",',
|
||||
' "trigger_hostile": "<mob name or null>",',
|
||||
' "avoid_skill": "<skill to avoid or null>",',
|
||||
' "prefer_skill": "<alternative skill id or null>",',
|
||||
' "confidence": 0.6 }',
|
||||
' ]',
|
||||
'}',
|
||||
"",
|
||||
"If you're clearly in a loop (same activity, no inventory growth, same position), say so honestly.",
|
||||
"If you're stuck in a bad terrain (deep pit, hostile-rich area), recommend choosing a new base.",
|
||||
"Lessons should be SHORT and ACTIONABLE. Don't repeat lessons the dispatcher already learned.",
|
||||
].join("\n");
|
||||
|
||||
return { system, user };
|
||||
}
|
||||
|
||||
function parseReply(text) {
|
||||
@@ -241,22 +299,6 @@ function writeReflection(stateDir, parsed, raw) {
|
||||
}
|
||||
}
|
||||
|
||||
function askPiOnce({ askPi, prompt }) {
|
||||
return new Promise((res) => {
|
||||
let buf = "";
|
||||
try {
|
||||
askPi({
|
||||
prompt,
|
||||
onChunk: ({ stream, text }) => { if (stream === "stdout") buf += text; },
|
||||
onDone: () => res(buf),
|
||||
});
|
||||
} catch (e) {
|
||||
warn("reflect", `askPi: ${e?.message ?? e}`);
|
||||
res(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function asArray(v) { return Array.isArray(v) ? v : v ? [v] : []; }
|
||||
function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)); }
|
||||
|
||||
|
||||
@@ -4,14 +4,27 @@ import { mkdtempSync, rmSync, readdirSync, existsSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { initKnowledge, recall } from "../knowledge/index.js";
|
||||
import { initKnowledge, recall, listImprovements } from "../knowledge/index.js";
|
||||
import { closeStore, __resetForTests, isAvailable } from "../knowledge/store.js";
|
||||
import { runOnce, __testing } from "./reflect.js";
|
||||
|
||||
const { buildPrompt, parseReply } = __testing;
|
||||
|
||||
test("buildPrompt: includes runtime state + plan + diary", () => {
|
||||
const p = buildPrompt({
|
||||
function withTimeWebEnv(fn) {
|
||||
const prevKey = process.env.TIMEWEB_API_KEY;
|
||||
const prevModel = process.env.TIMEWEB_MODEL;
|
||||
process.env.TIMEWEB_API_KEY = "test-key";
|
||||
process.env.TIMEWEB_MODEL = "test-model";
|
||||
return Promise.resolve(fn()).finally(() => {
|
||||
if (prevKey === undefined) delete process.env.TIMEWEB_API_KEY;
|
||||
else process.env.TIMEWEB_API_KEY = prevKey;
|
||||
if (prevModel === undefined) delete process.env.TIMEWEB_MODEL;
|
||||
else process.env.TIMEWEB_MODEL = prevModel;
|
||||
});
|
||||
}
|
||||
|
||||
test("buildPrompt: returns {system, user} with state, plan, diary, improvement schema", () => {
|
||||
const { system, user } = buildPrompt({
|
||||
snap: {
|
||||
position: { x: 600, y: 64, z: 200 },
|
||||
health: 4, food: 6, isDay: false,
|
||||
@@ -26,15 +39,17 @@ test("buildPrompt: includes runtime state + plan + diary", () => {
|
||||
scenarios: ['{"skillId":"explore.far","code":"wedged"}'],
|
||||
diary: "13:00 spawned\n13:05 died",
|
||||
plan: "1. Gather 16 logs\n2. Craft pickaxe",
|
||||
activeNeed: null,
|
||||
});
|
||||
assert.match(p, /position: \(600, 64, 200\)/);
|
||||
assert.match(p, /hp: 4 food: 6/);
|
||||
assert.match(p, /emergency/);
|
||||
assert.match(p, /Gather 16 logs/);
|
||||
assert.match(p, /Reply with ONE JSON object/);
|
||||
assert.match(user, /position: \(600, 64, 200\)/);
|
||||
assert.match(user, /hp: 4 food: 6/);
|
||||
assert.match(user, /emergency/);
|
||||
assert.match(user, /Gather 16 logs/);
|
||||
assert.match(system, /Reply with ONE JSON object/);
|
||||
assert.match(system, /improvements/);
|
||||
});
|
||||
|
||||
test("parseReply: extracts JSON from various Pi outputs", () => {
|
||||
test("parseReply: extracts JSON from various LLM outputs", () => {
|
||||
assert.deepEqual(parseReply('{"verdict":"loop","summary":"stuck"}'), { verdict: "loop", summary: "stuck" });
|
||||
assert.deepEqual(parseReply('```json\n{"verdict":"progress"}\n```'), { verdict: "progress" });
|
||||
const longReply = 'I see... your situation. Here is my JSON:\n{"verdict":"emergency","summary":"hp critical","lessons":[]}\nDone.';
|
||||
@@ -43,7 +58,7 @@ test("parseReply: extracts JSON from various Pi outputs", () => {
|
||||
assert.equal(parseReply(""), null);
|
||||
});
|
||||
|
||||
test("runOnce: writes reflection file + records lessons", async () => {
|
||||
test("runOnce: writes reflection file + records lessons + improvement requests", async () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "pepa-reflect-test-"));
|
||||
__resetForTests();
|
||||
await initKnowledge({ stateDir: tmp });
|
||||
@@ -52,39 +67,64 @@ test("runOnce: writes reflection file + records lessons", async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const fakeReply = JSON.stringify({
|
||||
verdict: "loop",
|
||||
summary: "Бот ходит по кругу, ничего не добывает.",
|
||||
next_action: "выбрать новое место под базу",
|
||||
lessons: [{
|
||||
lesson: "В этой точке постоянные смерти — искать новое место.",
|
||||
category: "survival",
|
||||
prefer_skill: "village.choose-base",
|
||||
confidence: 0.7,
|
||||
}],
|
||||
});
|
||||
const askPi = ({ onChunk, onDone }) => {
|
||||
onChunk({ stream: "stdout", text: fakeReply });
|
||||
onDone({ code: 0 });
|
||||
};
|
||||
const getSnapshot = () => ({
|
||||
position: { x: 0, y: 64, z: 0 },
|
||||
health: 8, food: 10, isDay: true,
|
||||
runtimeState: "working",
|
||||
inventory: {},
|
||||
await withTimeWebEnv(async () => {
|
||||
const fakeReply = {
|
||||
verdict: "loop",
|
||||
summary: "Бот ходит по кругу, ничего не добывает.",
|
||||
next_action: "выбрать новое место под базу",
|
||||
lessons: [{
|
||||
lesson: "В этой точке постоянные смерти — искать новое место.",
|
||||
category: "survival",
|
||||
prefer_skill: "village.choose-base",
|
||||
confidence: 0.7,
|
||||
}],
|
||||
improvements: [
|
||||
{ title: "Add craft.iron-pickaxe skill", description: "Bot mines iron but cannot craft a tier-3 pickaxe.", category: "skill", priority: 2 },
|
||||
],
|
||||
};
|
||||
const askAnalyticalFn = async () => fakeReply;
|
||||
const getSnapshot = () => ({
|
||||
position: { x: 0, y: 64, z: 0 },
|
||||
health: 8, food: 10, isDay: true,
|
||||
runtimeState: "working",
|
||||
inventory: {},
|
||||
});
|
||||
|
||||
const result = await runOnce({ stateDir: tmp, getSnapshot, force: true, askAnalyticalFn });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.verdict, "loop");
|
||||
assert.equal(result.improvements, 1);
|
||||
|
||||
const reflectionsDir = join(tmp, "reflections");
|
||||
assert.ok(existsSync(reflectionsDir));
|
||||
const files = readdirSync(reflectionsDir);
|
||||
assert.ok(files.length >= 1, `expected ≥1 reflection file, got ${files.length}`);
|
||||
|
||||
const lessons = recall({ category: "survival" });
|
||||
assert.ok(lessons.some((l) => l.source === "timeweb-reflect"), "lesson recorded with source=timeweb-reflect");
|
||||
|
||||
const improvements = listImprovements({ source: "reflect" });
|
||||
assert.ok(improvements.some((r) => r.title === "Add craft.iron-pickaxe skill"));
|
||||
});
|
||||
|
||||
const result = await runOnce({ stateDir: tmp, askPi, getSnapshot, force: true });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.verdict, "loop");
|
||||
closeStore();
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
});
|
||||
|
||||
const reflectionsDir = join(tmp, "reflections");
|
||||
assert.ok(existsSync(reflectionsDir));
|
||||
const files = readdirSync(reflectionsDir);
|
||||
assert.ok(files.length >= 1, `expected ≥1 reflection file, got ${files.length}`);
|
||||
|
||||
const lessons = recall({ category: "survival" });
|
||||
assert.ok(lessons.some((l) => l.source === "pi-reflect"), "lesson recorded with source=pi-reflect");
|
||||
test("runOnce: skipped when LLM not configured", async () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "pepa-reflect-test-"));
|
||||
__resetForTests();
|
||||
await initKnowledge({ stateDir: tmp });
|
||||
if (!isAvailable()) {
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
return;
|
||||
}
|
||||
const prevKey = process.env.TIMEWEB_API_KEY;
|
||||
delete process.env.TIMEWEB_API_KEY;
|
||||
const res = await runOnce({ stateDir: tmp, getSnapshot: () => ({}), force: true });
|
||||
if (prevKey !== undefined) process.env.TIMEWEB_API_KEY = prevKey;
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.reason, "llm not configured");
|
||||
|
||||
closeStore();
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
@@ -98,15 +138,15 @@ test("runOnce: budget exhausted → ok=false", async () => {
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
return;
|
||||
}
|
||||
const askPi = ({ onDone }) => onDone({ code: 0 });
|
||||
const getSnapshot = () => ({});
|
||||
// Fire 2 forced calls to exhaust budget; 3rd without force should fail.
|
||||
await runOnce({ stateDir: tmp, askPi, getSnapshot, force: true });
|
||||
await runOnce({ stateDir: tmp, askPi, getSnapshot, force: true });
|
||||
const res = await runOnce({ stateDir: tmp, askPi, getSnapshot, force: false });
|
||||
assert.equal(res.ok, false);
|
||||
assert.match(res.reason ?? "", /budget|reply/);
|
||||
|
||||
await withTimeWebEnv(async () => {
|
||||
const askAnalyticalFn = async () => ({ verdict: "ok" });
|
||||
const getSnapshot = () => ({});
|
||||
await runOnce({ stateDir: tmp, getSnapshot, force: true, askAnalyticalFn });
|
||||
await runOnce({ stateDir: tmp, getSnapshot, force: true, askAnalyticalFn });
|
||||
const res = await runOnce({ stateDir: tmp, getSnapshot, force: false, askAnalyticalFn });
|
||||
assert.equal(res.ok, false);
|
||||
assert.match(res.reason ?? "", /budget|reply/);
|
||||
});
|
||||
closeStore();
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// Trigger tuner — periodic statistical sanity-check over the
|
||||
// advisor_recommendations table.
|
||||
//
|
||||
// Replaces the old Pi-reflect "analyse your own pattern" loop with a
|
||||
// deterministic local computation: no LLM call, no subscription, just
|
||||
// SQL. Every TUNE_INTERVAL_MS the tuner reads the last 24h of
|
||||
// recommendations, groups by trigger_reason, and flags two failure
|
||||
// modes as improvement_requests for the operator:
|
||||
//
|
||||
// 1. low-success trigger: a trigger that fires often (≥ MIN_SAMPLE)
|
||||
// but lands a successful outcome < SUCCESS_FLOOR of the time.
|
||||
// The threshold probably needs tuning, or the prompt isn't giving
|
||||
// the LLM the right hint.
|
||||
// 2. expensive trigger: trigger averages > EXPENSIVE_TOKENS input
|
||||
// tokens but its success rate is mediocre. Could mean the prompt
|
||||
// includes context the LLM doesn't actually use.
|
||||
//
|
||||
// The tuner deduplicates via createImprovementRequest's votes mechanism
|
||||
// — re-flagging the same gap just bumps the counter, not the row count.
|
||||
|
||||
import {
|
||||
isAvailable as knowledgeAvailable,
|
||||
recommendationStats,
|
||||
createImprovementRequest,
|
||||
} from "../knowledge/index.js";
|
||||
import { info, warn } from "../log.js";
|
||||
|
||||
const TUNE_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
|
||||
const MIN_SAMPLE = 5;
|
||||
const SUCCESS_FLOOR = 0.25;
|
||||
const EXPENSIVE_TOKENS = 1000;
|
||||
const EXPENSIVE_SUCCESS_CEILING = 0.5;
|
||||
|
||||
let _timer = null;
|
||||
|
||||
export function attach({ intervalMs = TUNE_INTERVAL_MS } = {}) {
|
||||
if (_timer) {
|
||||
warn("tuner", "attach called twice; ignoring");
|
||||
return;
|
||||
}
|
||||
_timer = setInterval(() => {
|
||||
runOnce().catch((e) => warn("tuner", `tick err: ${e?.message ?? e}`));
|
||||
}, intervalMs);
|
||||
_timer.unref?.();
|
||||
info("tuner", `attached; tune every ${Math.round(intervalMs / 60000)} min`);
|
||||
}
|
||||
|
||||
export function detach() {
|
||||
if (_timer) clearInterval(_timer);
|
||||
_timer = null;
|
||||
}
|
||||
|
||||
export function runOnce({ stats = null } = {}) {
|
||||
if (!knowledgeAvailable()) return { ok: false, reason: "knowledge unavailable" };
|
||||
|
||||
const rows = stats ?? recommendationStats({ sinceHours: 24 });
|
||||
if (!rows.length) return { ok: true, flagged: 0, reason: "no data" };
|
||||
|
||||
const flagged = [];
|
||||
for (const row of rows) {
|
||||
const sample = (row.applied ?? 0);
|
||||
if (sample < MIN_SAMPLE) continue;
|
||||
const succ = row.succeeded ?? 0;
|
||||
const successRate = sample === 0 ? 0 : succ / sample;
|
||||
|
||||
// 1. Low success → tune the trigger
|
||||
if (successRate < SUCCESS_FLOOR) {
|
||||
const title = `Trigger "${row.trigger_reason}" has low success rate`;
|
||||
createImprovementRequest({
|
||||
source: "tuner",
|
||||
category: "tuning",
|
||||
title,
|
||||
description: `Over the last 24h, ${sample} applied recommendations from trigger ${row.trigger_reason} produced only ${succ} successful outcomes (${(successRate * 100).toFixed(0)}%). Consider tightening the trigger condition, improving the prompt, or adjusting the threshold.`,
|
||||
context: { stats: row },
|
||||
priority: 2,
|
||||
});
|
||||
flagged.push({ kind: "low_success", trigger: row.trigger_reason, sample, succ });
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2. Expensive prompt with mediocre payoff
|
||||
const avgIn = row.avg_in ?? 0;
|
||||
if (avgIn > EXPENSIVE_TOKENS && successRate < EXPENSIVE_SUCCESS_CEILING) {
|
||||
const title = `Trigger "${row.trigger_reason}" prompt is expensive`;
|
||||
createImprovementRequest({
|
||||
source: "tuner",
|
||||
category: "tuning",
|
||||
title,
|
||||
description: `Trigger ${row.trigger_reason} averages ${Math.round(avgIn)} input tokens but lands successful outcomes only ${(successRate * 100).toFixed(0)}% of the time (${succ}/${sample}). The prompt may include context the model doesn't use — consider trimming.`,
|
||||
context: { stats: row },
|
||||
priority: 4,
|
||||
});
|
||||
flagged.push({ kind: "expensive_prompt", trigger: row.trigger_reason, avgIn });
|
||||
}
|
||||
}
|
||||
if (flagged.length > 0) {
|
||||
info("tuner", `flagged ${flagged.length} improvement(s) from ${rows.length} trigger group(s)`);
|
||||
}
|
||||
return { ok: true, flagged: flagged.length, items: flagged, groups: rows.length };
|
||||
}
|
||||
|
||||
// Test exports
|
||||
export const __testing = { TUNE_INTERVAL_MS, MIN_SAMPLE, SUCCESS_FLOOR, EXPENSIVE_TOKENS };
|
||||
@@ -0,0 +1,98 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { initKnowledge, isAvailable, listImprovements } from "../knowledge/index.js";
|
||||
import { closeStore, __resetForTests } from "../knowledge/store.js";
|
||||
import { runOnce, __testing } from "./trigger-tuner.js";
|
||||
|
||||
const { MIN_SAMPLE } = __testing;
|
||||
|
||||
async function bootstrap() {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "pepa-tuner-test-"));
|
||||
__resetForTests();
|
||||
await initKnowledge({ stateDir: tmp });
|
||||
return tmp;
|
||||
}
|
||||
|
||||
function cleanup(tmp) {
|
||||
closeStore();
|
||||
try { rmSync(tmp, { recursive: true, force: true }); } catch {}
|
||||
}
|
||||
|
||||
test("runOnce: empty stats → ok with 0 flagged", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const r = runOnce({ stats: [] });
|
||||
assert.equal(r.ok, true);
|
||||
assert.equal(r.flagged, 0);
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("runOnce: ignores small samples (below MIN_SAMPLE)", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const stats = [
|
||||
{ trigger_reason: "wedged_60s", total: 2, applied: 2, succeeded: 0, failed: 2, avg_in: 700, avg_out: 40, avg_latency_ms: 5000 },
|
||||
];
|
||||
const r = runOnce({ stats });
|
||||
assert.equal(r.flagged, 0, "applied=2 is below MIN_SAMPLE; skipped");
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("runOnce: flags low success-rate trigger as improvement", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const stats = [
|
||||
{ trigger_reason: "wedged_60s", total: 10, applied: 10, succeeded: 1, failed: 9, avg_in: 700, avg_out: 40, avg_latency_ms: 5000 },
|
||||
];
|
||||
const r = runOnce({ stats });
|
||||
assert.equal(r.flagged, 1);
|
||||
const requests = listImprovements({ source: "tuner" });
|
||||
assert.ok(requests.some((req) => req.title.includes("wedged_60s") && req.title.includes("low success")));
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("runOnce: flags expensive prompt with mediocre payoff", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const stats = [
|
||||
{ trigger_reason: "repeat_4_explore.far", total: 10, applied: 10, succeeded: 4, failed: 6, avg_in: 1500, avg_out: 50, avg_latency_ms: 7000 },
|
||||
];
|
||||
const r = runOnce({ stats });
|
||||
assert.equal(r.flagged, 1);
|
||||
const requests = listImprovements({ source: "tuner", category: "tuning" });
|
||||
assert.ok(requests.some((req) => req.title.includes("expensive")));
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("runOnce: healthy trigger does NOT get flagged", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const stats = [
|
||||
{ trigger_reason: "emergency_hp4_creeper@3", total: 8, applied: 8, succeeded: 7, failed: 1, avg_in: 700, avg_out: 40, avg_latency_ms: 5000 },
|
||||
];
|
||||
const r = runOnce({ stats });
|
||||
assert.equal(r.flagged, 0);
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("runOnce: re-running with same low-success stats bumps votes, not row count", async () => {
|
||||
const tmp = await bootstrap();
|
||||
if (!isAvailable()) { cleanup(tmp); return; }
|
||||
const stats = [
|
||||
{ trigger_reason: "wedged_unique_label", total: 10, applied: 10, succeeded: 1, failed: 9, avg_in: 700, avg_out: 40, avg_latency_ms: 5000 },
|
||||
];
|
||||
runOnce({ stats });
|
||||
runOnce({ stats });
|
||||
const requests = listImprovements({ source: "tuner" }).filter((r) => r.title.includes("wedged_unique_label"));
|
||||
assert.equal(requests.length, 1, "single row for the same title");
|
||||
assert.ok(requests[0].votes >= 2, "votes bumped on re-flagging");
|
||||
cleanup(tmp);
|
||||
});
|
||||
|
||||
test("MIN_SAMPLE constant is reasonable", () => {
|
||||
assert.ok(MIN_SAMPLE >= 3 && MIN_SAMPLE <= 10);
|
||||
});
|
||||
Reference in New Issue
Block a user