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:
+20
-2
@@ -26,6 +26,7 @@ import {
|
||||
} from "./actions.js";
|
||||
import { runSkill, getSkill } from "./skills/index.js";
|
||||
import { consult as consultAdvice, reportOutcome as reportAdviceOutcome } from "./coach/advice.js";
|
||||
import { tickAdvisor, consumeFreshRecommendation } from "./coach/advisor-trigger.js";
|
||||
import { pickActiveNeed } from "./manifesto/state.js";
|
||||
import { situationHash } from "./scenario-memory.js";
|
||||
import { tickModes } from "./modes.js";
|
||||
@@ -530,8 +531,25 @@ function curriculumReflex(ctx) {
|
||||
|
||||
// Pick what to dispatch: manifesto wins over curriculum plan because
|
||||
// it expresses concrete needs rather than abstract "next milestone".
|
||||
const skillId = manifestoSkillId ?? plan.skillId;
|
||||
const skillSource = manifestoSkillId ? `manifesto:${activeNeed.need.id}` : "curriculum";
|
||||
let skillId = manifestoSkillId ?? plan.skillId;
|
||||
let skillSource = manifestoSkillId ? `manifesto:${activeNeed.need.id}` : "curriculum";
|
||||
|
||||
// v0.3.0 fast-advisor: if a fresh recommendation is sitting on ctx
|
||||
// (the result of a previous tick's async advise() call), use it.
|
||||
// This is the closing of the awareness → LLM → action loop.
|
||||
if (!ctx.disableAdvisor) {
|
||||
const rec = consumeFreshRecommendation(ctx);
|
||||
if (rec && rec.skillId) {
|
||||
info(REFLEX_LOG, `advisor override: ${skillId} → ${rec.skillId} (${rec.triggerReason}, ${rec.rationale?.slice(0, 60)})`);
|
||||
skillId = rec.skillId;
|
||||
skillSource = `advisor:${rec.triggerReason}`;
|
||||
}
|
||||
// Always fire-and-forget another advise() if triggers fire — the
|
||||
// result lands on a future tick. tickAdvisor handles its own
|
||||
// cooldown / in-flight checks so this is safe to call every tick.
|
||||
tickAdvisor(ctx, { plannedSkillId: skillId });
|
||||
}
|
||||
|
||||
const skill = getSkill(skillId);
|
||||
if (!skill) {
|
||||
// Suggested a skill that isn't registered yet — fall back
|
||||
|
||||
Reference in New Issue
Block a user