fix: integrate cumulative ACP usage snapshots and deltas - #662
Conversation
Model: gpt-6
Update Core, Grok, and DSH references; preserve accounting deltas at the client boundary and coalesce cumulative snapshots. Replace the superseded queue-only research and delivery contract. Model: gpt-6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c1e0ff80a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -1 +1 @@ | |||
| Subproject commit ce194fd6766292df0726680ccbb3a1737c609b5c | |||
| Subproject commit ee3a7044328641bfc02fe262b5ad5df033d250cd | |||
There was a problem hiding this comment.
Route DeepSeek usage through the consumer
This gitlink adds DSH usage emission, but builtin DeepSeek sessions still discard every update in MessageHandler.handleUsageUpdate: that method accepts only isManagedBuiltinAgentType(meta.agentType), while packages/shared/src/ai.ts deliberately excludes deepseek from the managed-runtime list and its test explicitly asserts that predicate is false. Consequently none of the new DSH token or cost snapshots can reach UsageTrackingService or the persistence endpoint; widen the usage-attribution path and its CliType typing to include builtin DeepSeek.
Useful? React with 👍 / 👎.
Preserve optional delta coverage for all builtin providers and document accounting lifetime and normalization gaps. Model: gpt-6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1e314e377d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -1 +1 @@ | |||
| Subproject commit c962338e3e6e68858e0bf92e9671a84b9a07e055 | |||
| Subproject commit 3a397a79724ebff6e74488fe3cf20bbef5800624 | |||
There was a problem hiding this comment.
Give restarted Grok accounting a fresh identity
When a Grok session is resumed after a CLI/adapter restart, this pin starts its cumulative modelUsage from a fresh process baseline while Lody confirms and persists the same ACP session ID in session-manager.ts; the PR's own reproduction in .agents/notes/proposed/bug-fix/2026-09-12-grok-token-accounting.md records that a same-key total of 1000 followed by a fresh lifetime's 200 produces no increment. Since UsageTrackingService also keys delivery by that unchanged acpSessionId, post-restart usage below the previous total is silently lost. Restore the cumulative baseline or supply a distinct accounting-lifetime identity before emitting restarted snapshots.
AGENTS.md reference: AGENTS.md:L85-L87
Useful? React with 👍 / 👎.
Model: gpt-6
Model: gpt-6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b5028cb0e4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| (base.usage.cacheCreationInputTokens ?? 0) + (delta.usage.cacheCreationInputTokens ?? 0), | ||
| reasoningOutputTokens: | ||
| (base.usage.reasoningOutputTokens ?? 0) + (delta.usage.reasoningOutputTokens ?? 0), | ||
| webSearchRequests: (base.usage.webSearchRequests ?? 0) + (delta.usage.webSearchRequests ?? 0), |
There was a problem hiding this comment.
Omit unsupported web-search counts from aggregate usage
Whenever legacy Codex accounting crosses a compaction boundary, this merge unconditionally adds webSearchRequests—usually as 0—to the top-level usage object. The public upsertSessionUsageFromCli contract in packages/cloud-api/src/index.ts lines 711–719 does not permit that field (it is permitted only within modelUsage), so the mutation is rejected and every subsequent post-compaction snapshot remains stuck in the retry queue instead of being persisted. Keep this field out of aggregate usage unless the endpoint contract is extended.
AGENTS.md reference: AGENTS.md:L83-L88
Useful? React with 👍 / 👎.
Related issue
Supersedes the closed implementation in #661.
Problem / pressure
Grok reports per-prompt model totals while accounting consumers require cumulative snapshots. Queueing every prompt does not resolve that mismatch. DSH previously did not report token/USD accounting.
Summary
Visual explanation
Before / after
Dependencies and rollout
Dependency PRs (publish Core before adapters):
Publish Core first, then release/rebuild Grok and DSH, then ship the consuming application. Do not merge this as a claim that dependencies are already published.
Test plan
Context handoff
Instructions for reviewing agents
Authoring context
Independent audit follow-up
Updated in this PR, with Core #9 and DSH #16 updated in place. Additional provider source dependencies:
Repairs: real deepseek-official pricing route; Codex disjoint buckets, cache writes, unattributed cumulative accounting and compaction retention; Claude thinking/unknown-cost/cancelled-result handling and delta; Kimi serialized success-only delta baseline; CLI acknowledged legacy compaction offsets and unknown-cost preservation; Core empty aggregate cost omitted.
Current targeted verification supersedes earlier counts above: Core 3, DSH 16, Codex usage 9, Claude usage 10, Kimi projection 7, CLI delivery 15 (60 passing synthetic tests). Narrow actual Kimi emission methods also pass failure/retry checks. Core/DSH builds and Codex bundle pass; docs/public/platform boundary checks pass. Root check stops at Claude mid_conv_system SDK union mismatch; root format stops at cloud-api missing Prettier. Full Codex typecheck has harness Vitest mock signature incompatibilities. Full Kimi session regression added but not run in complete engine workspace.
Not resolved / not ready to claim complete conformance: same-ID resume/reset needs durable baseline or a consumer accounting-lifetime protocol. Private backend changes require separate approval. Kimi managed artifact remains unchanged and must be released later. Codex USD stays unknown without native per-model attribution. No package publication, paid runtime calls, production rollout, or historical repair. The owning bilingual Note and draft Spec contain evidence and limitations.
Core 0.1.5 publication follow-up
Core 0.1.5 is now verified available on npm. All five provider manifests pin 0.1.5: Grok/DSH already did; Codex/Claude/Kimi manifests and standalone lockfiles are now updated, and this PR pins those source commits. Root workspace links intentionally remain workspace references.
Codex/Claude npm ci dry-run passes; published tarball exports and empty-aggregate cost behavior verified. Kimi lock diff independently reviewed; frozen lock validation blocked by Node below >=24.15.0. Root check/format remain blocked by missing workspace dependencies. No adapter package or Kimi managed artifact release performed.