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>
This commit is contained in:
2026-05-27 19:12:48 +03:00
co-authored by Claude Opus 4.7
parent bc381b2a4b
commit 0d97ccccfa
18 changed files with 1188 additions and 197 deletions
+60
View File
@@ -179,3 +179,63 @@ CREATE TABLE IF NOT EXISTS code_changes (
outcome TEXT, -- 'applied'|'rolled_back'|'rejected'
notes TEXT
);
----------------------------------------------------------------------
-- Advisor recommendations (v0.3.0+ fast LLM trail)
-- Every time runtime/coach/advisor-trigger.js asks the fast LLM and
-- the answer is cached on ctx, we write a row here. When the reflex
-- consumes the recommendation and dispatches, we attach the dispatch
-- result later via outcome_ok / outcome_code. The history is the
-- ground truth for trigger-tuner.js stats and for the operator's
-- "what is the LLM suggesting and is it actually helping" question.
----------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS advisor_recommendations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL,
trigger_reason TEXT NOT NULL, -- 'wedged_*', 'repeat_*', 'preempt_retry_*', 'emergency_*'
planned_skill TEXT, -- what manifesto/curriculum was about to dispatch
recommended_skill TEXT, -- what the LLM said to do instead
action TEXT NOT NULL, -- 'switch_skill' | 'continue' | 'wait'
rationale TEXT,
active_need TEXT, -- 'L2 tools_wood' etc.
tokens_in INTEGER,
tokens_out INTEGER,
latency_ms INTEGER,
applied INTEGER NOT NULL DEFAULT 0, -- 1 if reflex actually dispatched recommended_skill
outcome_ok INTEGER, -- NULL until dispatch finishes
outcome_code TEXT,
outcome_at INTEGER
);
CREATE INDEX IF NOT EXISTS idx_advisor_ts ON advisor_recommendations(ts);
CREATE INDEX IF NOT EXISTS idx_advisor_trigger ON advisor_recommendations(trigger_reason);
CREATE INDEX IF NOT EXISTS idx_advisor_outcome ON advisor_recommendations(outcome_ok);
----------------------------------------------------------------------
-- Improvement requests (v0.3.0+)
-- The LLM (postmortem / reflect / advisor) can flag situations where
-- the bot lacked the right skill or feature. Instead of trying to
-- self-patch (which we explicitly disabled), it writes an entry here.
-- The operator reads `scripts/list-improvements.js` and decides what
-- to implement. Implemented entries get marked so the bot stops
-- re-flagging the same gap.
----------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS improvement_requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL,
source TEXT NOT NULL, -- 'postmortem'|'reflect'|'advisor'|'tuner'|'manual'
category TEXT, -- 'skill'|'tuning'|'perception'|'planning'|'social'|'other'
title TEXT NOT NULL,
description TEXT,
context TEXT, -- JSON: position, snapshot tail, related lesson ids
priority INTEGER NOT NULL DEFAULT 3, -- 1..5 (1=urgent, 5=nice-to-have)
status TEXT NOT NULL DEFAULT 'open', -- 'open'|'in_progress'|'implemented'|'rejected'|'duplicate'
duplicate_of INTEGER, -- another row id if dup
votes INTEGER NOT NULL DEFAULT 1, -- bumped each time the bot re-flags same gap
implemented_at INTEGER,
notes TEXT,
FOREIGN KEY (duplicate_of) REFERENCES improvement_requests(id)
);
CREATE INDEX IF NOT EXISTS idx_improvements_status ON improvement_requests(status);
CREATE INDEX IF NOT EXISTS idx_improvements_priority ON improvement_requests(priority);
CREATE INDEX IF NOT EXISTS idx_improvements_source ON improvement_requests(source);
CREATE INDEX IF NOT EXISTS idx_improvements_ts ON improvement_requests(ts);