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>
This commit is contained in:
2026-05-27 18:44:30 +03:00
co-authored by Claude Opus 4.7
parent 602060d857
commit bc381b2a4b
9 changed files with 499 additions and 9 deletions
+28 -2
View File
@@ -23,14 +23,33 @@ 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;
}
/**
@@ -69,6 +88,11 @@ export async function advise({
_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 };
@@ -93,6 +117,7 @@ export async function advise({
rationale,
raw: parsed,
latencyMs: res.latencyMs,
usage: res.usage,
};
}
info("advisor", `switch_skill → ${skillId} (${rationale.slice(0, 80)})`);
@@ -103,15 +128,16 @@ export async function advise({
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 };
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 };
return { ok: false, code: "bad_action", detail: action || "missing", raw: parsed, latencyMs: res.latencyMs, usage: res.usage };
}
function buildSystemPrompt() {