Skip to content

feat(core): record title and compaction usage in session totals - #37441

Merged
rekram1-node merged 1 commit into
v2from
side-inference-usage
Jul 17, 2026
Merged

rekram1-node merged 1 commit into
v2from
side-inference-usage

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Problem

Title generation and compaction make billable LLM requests, but their reported tokens and cost were discarded. Session totals only included normal assistant steps. (V1 counted compaction usage through step-finish parts but silently dropped title usage; V2 dropped both.)

Implementation

  • Extracted shared token normalization and cost calculation into session/usage.ts (SessionUsage.tokens, calculateCost, record, add), deduplicating the copies in runner/llm.ts and publish-llm-event.ts.
  • Captured provider usage from the title and compaction streams, accumulating across step-finish events with per-step tier costing, and flushing on both completion and interrupt.
  • Added a durable session.usage.recorded event for side inferences, mirroring how session.step.ended carries usage for normal steps. Durability is required: session totals are a projection of the durable log and are rebuilt through replay/sync, so a direct write would be lost.
  • Projected the event into session token/cost totals via the existing applyUsage fold and wired it into the aggregate session.usage.updated broadcast.
  • Kept the event internal-durable: it is in SessionEvent.DurableDefinitions (replay/sync manifest and the experimental durable session log contract) but excluded from SessionEvent.Definitions, so it never reaches the public event stream or manifest. Clients observe usage only through the aggregate session.usage.updated.
  • Regenerated public client types: SessionUsageRecorded is absent from V2Event and present only in the durable-log SessionEventDurable union.

Testing

  • Title and compaction tests now assert projected session totals, cost, and durable event ordering (Started → UsageRecorded → Ended).
  • Manifest test pins the internal/public split: present in EventManifest.Durable, absent from Definitions/ServerDefinitions/Latest.
  • Ran core session/event/log suites, full schema suite, and typechecks across core, schema, protocol, client, and server.

@rekram1-node
rekram1-node force-pushed the side-inference-usage branch from fb9915d to 5c9a90e Compare July 17, 2026 04:34
@rekram1-node
rekram1-node force-pushed the side-inference-usage branch from 5c9a90e to 0c9352d Compare July 17, 2026 05:08
@rekram1-node
rekram1-node merged commit 73567f3 into v2 Jul 17, 2026
8 checks passed
@rekram1-node
rekram1-node deleted the side-inference-usage branch July 17, 2026 05:10
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant