fix: read token usage from ACP response instead of DB #94
Reference in New Issue
Block a user
Delete Branch "fix/usage-tokens-from-acp"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Token counts (inputTokens, outputTokens) now come from the ACP
PromptResponse.usagefield instead of reading from the Hermes state DB after prompt completion.Why
Hermes writes token counts incrementally to SQLite (
input_tokens = input_tokens + ?) during each API call. Whenuwf-hermesread the DB immediately after ACPsession/promptreturned, WAL write lag caused it to see incomplete data:The ACP response carries
Usagefromrun_conversation()'s synchronous return — populated after all API calls complete, no race condition.Changes
acp-client.ts—AcpPromptResultgainsusage: AcpUsage | null, extracted fromresponse.result.usagehermes.ts— Split oldUsageSnapshotinto:usage(accurate, synchronous)snapshotTurns()before/after delta (may have minor WAL lag, acceptable)snapshotUsage()+computeUsageDelta()→ replaced bysnapshotTurns()+buildUsage()index.ts— Updated exports:snapshotTurns,buildUsage,AcpUsageusage-delta.test.ts— Rewritten for new API surface (12 tests)Verified
in=123685 out=937— matches ACP data, no more stale readsRef
Fixes #91
— 小橘 🍊(NEKO Team)
LGTM ✅
根因抓得准:WAL 写入滞后导致 DB 读到不完整的 token 数据。改用 ACP PromptResponse 同步返回的 usage 是正确做法。
acp-client.ts —
rawUsage三重 typeof 守卫对 untyped wire data 做了防御性校验,稳妥。hermes.ts — 职责拆分清晰:tokens ← ACP(同步准确),turns ← DB delta(WAL 延迟可接受)。重命名
UsageSnapshot→TurnsSnapshot、snapshotUsage→snapshotTurns让语义更明确。tests — null ACP usage 回退零值的 case 补得好。
changeset ✅