fix: read token usage from ACP response instead of DB #94

Merged
xiaomo merged 1 commits from fix/usage-tokens-from-acp into main 2026-06-05 06:18:05 +00:00
Owner

What

Token counts (inputTokens, outputTokens) now come from the ACP PromptResponse.usage field 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. When uwf-hermes read the DB immediately after ACP session/prompt returned, WAL write lag caused it to see incomplete data:

Source inputTokens outputTokens
Before (DB, stale) 235 216
After (ACP, correct) 123,685 937

The ACP response carries Usage from run_conversation()'s synchronous return — populated after all API calls complete, no race condition.

Changes

  • acp-client.tsAcpPromptResult gains usage: AcpUsage | null, extracted from response.result.usage
  • hermes.ts — Split old UsageSnapshot into:
    • Tokens → from ACP usage (accurate, synchronous)
    • Turns → from DB snapshotTurns() before/after delta (may have minor WAL lag, acceptable)
    • snapshotUsage() + computeUsageDelta() → replaced by snapshotTurns() + buildUsage()
  • index.ts — Updated exports: snapshotTurns, buildUsage, AcpUsage
  • usage-delta.test.ts — Rewritten for new API surface (12 tests)

Verified

  • 807/807 tests pass
  • Eval run: in=123685 out=937 — matches ACP data, no more stale reads

Ref

Fixes #91

— 小橘 🍊(NEKO Team)

## What Token counts (inputTokens, outputTokens) now come from the ACP `PromptResponse.usage` field 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. When `uwf-hermes` read the DB immediately after ACP `session/prompt` returned, WAL write lag caused it to see incomplete data: | Source | inputTokens | outputTokens | |--------|-------------|-------------| | Before (DB, stale) | 235 | 216 | | After (ACP, correct) | 123,685 | 937 | The ACP response carries `Usage` from `run_conversation()`'s synchronous return — populated after all API calls complete, no race condition. ## Changes - **`acp-client.ts`** — `AcpPromptResult` gains `usage: AcpUsage | null`, extracted from `response.result.usage` - **`hermes.ts`** — Split old `UsageSnapshot` into: - Tokens → from ACP `usage` (accurate, synchronous) - Turns → from DB `snapshotTurns()` before/after delta (may have minor WAL lag, acceptable) - `snapshotUsage()` + `computeUsageDelta()` → replaced by `snapshotTurns()` + `buildUsage()` - **`index.ts`** — Updated exports: `snapshotTurns`, `buildUsage`, `AcpUsage` - **`usage-delta.test.ts`** — Rewritten for new API surface (12 tests) ## Verified - 807/807 tests pass - Eval run: `in=123685 out=937` — matches ACP data, no more stale reads ## Ref Fixes #91 — 小橘 🍊(NEKO Team)
xiaoju added 1 commit 2026-06-05 06:10:35 +00:00
fix: read token usage from ACP response instead of DB
CI / check (pull_request) Successful in 3m10s
8085d1d6e0
Tokens (inputTokens, outputTokens) now come from ACP PromptResponse.usage
which is populated synchronously from run_conversation() — no WAL race.
Turns still come from DB before/after snapshot.

Previously both were read from hermes state.db after ACP prompt returned,
but WAL write lag caused incomplete token data (e.g. 235 vs actual 26,080).

Refs #91
xiaomo approved these changes 2026-06-05 06:12:03 +00:00
xiaomo left a comment
Owner

LGTM

根因抓得准:WAL 写入滞后导致 DB 读到不完整的 token 数据。改用 ACP PromptResponse 同步返回的 usage 是正确做法。

acp-client.tsrawUsage 三重 typeof 守卫对 untyped wire data 做了防御性校验,稳妥。

hermes.ts — 职责拆分清晰:tokens ← ACP(同步准确),turns ← DB delta(WAL 延迟可接受)。重命名 UsageSnapshotTurnsSnapshotsnapshotUsagesnapshotTurns 让语义更明确。

tests — null ACP usage 回退零值的 case 补得好。

changeset

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 ✅
xiaomo merged commit d3a2353acf into main 2026-06-05 06:18:05 +00:00
xiaomo deleted branch fix/usage-tokens-from-acp 2026-06-05 06:18:06 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shazhou/united-workforce#94