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:
+14
-3
@@ -141,8 +141,19 @@ export async function complete({
|
||||
}
|
||||
}
|
||||
|
||||
info("llm", `${useModel} ok (${latencyMs}ms, ${text.length}ch)`);
|
||||
return { ok: true, text: parsed, raw: text, latencyMs };
|
||||
// usage shape per OpenAI / TimeWeb / most compat endpoints:
|
||||
// { prompt_tokens, completion_tokens, total_tokens }
|
||||
const usage = normaliseUsage(payload?.usage);
|
||||
info("llm", `${useModel} ok (${latencyMs}ms, ${text.length}ch, in=${usage.in}/out=${usage.out}t)`);
|
||||
return { ok: true, text: parsed, raw: text, latencyMs, usage };
|
||||
}
|
||||
|
||||
function normaliseUsage(u) {
|
||||
if (!u || typeof u !== "object") return { in: 0, out: 0, total: 0 };
|
||||
const inT = Number(u.prompt_tokens ?? u.input_tokens ?? 0) || 0;
|
||||
const outT = Number(u.completion_tokens ?? u.output_tokens ?? 0) || 0;
|
||||
const total = Number(u.total_tokens ?? inT + outT) || (inT + outT);
|
||||
return { in: inT, out: outT, total };
|
||||
}
|
||||
|
||||
function tryParseJson(text) {
|
||||
@@ -155,4 +166,4 @@ function tryParseJson(text) {
|
||||
}
|
||||
|
||||
// Test exports
|
||||
export const __testing = { ENV, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, tryParseJson };
|
||||
export const __testing = { ENV, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, tryParseJson, normaliseUsage };
|
||||
|
||||
Reference in New Issue
Block a user