Repository navigation
refactor(agent): unify chat and headless token accumulation - #860
Merged
Merged
Conversation
Route the sync AgentService.Run path through storeIterationMetrics so both chat (streaming) and headless share one accumulation path: tokenizer polyfill, per-request ChatMetrics, and the conversation repository's session sink that /cost reads. Headless drops its four inline counters and ad-hoc AddTokenUsage goroutine; emitSessionStats now reads GetSessionTokens/GetSessionCostStats. --no-save disables repo auto-save so accumulation stays in-memory only. Closes #835
edenreich
commented
Jul 11, 2026
Co-authored-by: Eden Reich <[email protected]>
Contributor
|
🎉 This PR is included in version 0.142.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #835
What
Routes the sync
AgentService.Runpath (headless) through the samestoreIterationMetricsaccumulator the streaming (chat) path already uses, and deletes the headless duplicates. The shared sink was already there — the conversation repository'sAddTokenUsage/GetSessionTokens/GetSessionCostStats(what/costreads); the root cause was thatRunskipped it.internal/agent/agent.go:Runnow callsstoreIterationMetrics(tokenizer polyfill included) and returns the effective usage inChatSyncResponse.Usage, so per-message output matches what was accumulated.cmd/agent.go: drops the four inline counters, the ad-hocAddTokenUsagegoroutine, and the directPricingServicecost recomputation;emitSessionStatsreads the repo's session token and cost stats.--no-savenow callsSetAutoSave(false)so accumulation stays in-memory only.internal/services/persistent_conversation.go:SetAutoSavetakes the metadata mutex (one-line hardening).TestSyncAndStreamAccumulateIdenticalSessionTokens: both paths against the mock gateway'susage-reportscenario accumulate identical session totals (142 tokens).Acceptance criteria (#835)
usage-report, 142 tokens)/costand the headless final stats read from the same source (the conversation repo session stats)Intended behavior changes
session_statsis now session/conversation-scoped (same numbers/costshows) instead of run-scoped. Fresh sessions (CI, subagents, scheduler/heartbeat) are unchanged; resumed sessions (--session-idunder channels) now include restored history; a mid-run rollover resets totals with the conversation. No programmatic consumer parses these values (agentrunner ignoressession_statslines).requests; the polyfill now counts it with estimated tokens — chat parity, the point of the change.--no-save+ resumed session + mid-run rollover:StartNewConversationsaves explicitly (ignores auto-save), and the persisted metadata's token stats now include the--no-saverun's usage. Pre-existing save path, marginally widened.Verification
task test,task test:e2e(race),task lintall green.INFER_GATEWAY_MOCK=true infer agent "report your usage" --no-saveemitssession_statswith 142+15 tokens and repo-computed cost; storage untouched under--no-save; save-enabled run persists the same accumulated stats.no docs ticket: internal-only refactor