Files
alan 83b60990d2 feat(codex): retain native session usage snapshots for chat
Verify typed ThreadTokenUsage/TokenUsageBreakdown/Thread/Turn against openai/codex@6fb1c3903d. Use native totalTokens and latest-call context tokens without re-adding cached/reasoning subsets. Replay bounded per-connection snapshots and clear on room/source release.

Cost stays unavailable: no subscription USD usage is reported by this notification. Throughput stays unavailable even with native Turn.durationMs because neither last-call nor cumulative-session output is a compatible turn-specific numerator. Thread readback exposes no token usage; no extra reads, resume, or live provider calls added.

Checks: go test -race ./internal/codex; go vet ./internal/codex; go test ./internal/server ./internal/protocol; git diff --check.
2026-10-21 00:07:12 +01:00

114 lines
3.8 KiB
Go

package codex
import (
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Native types verified against openai/codex@6fb1c3903d42e3cc7da2c89e3a045fe0ce137af4:
// codex-rs/app-server-protocol/schema/typescript/v2/{ThreadTokenUsage,
// TokenUsageBreakdown,ThreadTokenUsageUpdatedNotification,Thread,Turn}.ts.
// Pointers deliberately tolerate older/partial payloads without inventing zeros.
type tokenUsageBreakdown struct {
TotalTokens *int64 `json:"totalTokens"`
InputTokens *int64 `json:"inputTokens"`
CachedInputTokens *int64 `json:"cachedInputTokens"`
CacheWriteInputTokens *int64 `json:"cacheWriteInputTokens"`
OutputTokens *int64 `json:"outputTokens"`
ReasoningOutputTokens *int64 `json:"reasoningOutputTokens"`
}
type threadTokenUsage struct {
Total tokenUsageBreakdown `json:"total"`
Last tokenUsageBreakdown `json:"last"`
ModelContextWindow *int64 `json:"modelContextWindow"`
}
func nonnegativeUsage(value *int64) *int64 {
if value == nil || *value < 0 {
return nil
}
return value
}
func (c *Chat) receiveUsage(raw json.RawMessage) {
var p struct {
ThreadID string `json:"threadId"`
TurnID string `json:"turnId"`
Usage *threadTokenUsage `json:"tokenUsage"`
}
if json.Unmarshal(raw, &p) != nil || p.ThreadID == "" || p.Usage == nil {
return
}
u := &protocol.ChatUsage{
Scope: "session", TotalTokens: nonnegativeUsage(p.Usage.Total.TotalTokens),
ContextTokens: nonnegativeUsage(p.Usage.Last.TotalTokens),
ContextWindow: nonnegativeUsage(p.Usage.ModelContextWindow),
}
if n := nonnegativeUsage(p.Usage.Total.InputTokens); n != nil {
u.InputTokens = int(*n)
}
if n := nonnegativeUsage(p.Usage.Total.OutputTokens); n != nil {
u.OutputTokens = int(*n)
}
// Native totalTokens is authoritative. Cached input and reasoning output
// are subsets, not additional usage. Last is the latest model call, not
// the whole turn: see TokenUsageInfo::append_last_usage in protocol.rs.
// Turn.durationMs cannot be paired with last.outputTokens (multi-call
// turns), or session outputTokens. No compatible turn total is exposed.
// Cost is not reported here either; subscription cost is not zero USD.
// Thread readback has no usage field, so do not add RPCs or resume a
// thread merely to fetch counters; reattach replays retained notifications.
c.emit(protocol.ChatEventFrame{SessionID: p.ThreadID, NativeTurnID: p.TurnID, Kind: protocol.ChatEventUsage, Usage: u})
}
const retainedUsageLimit = 256
type usageSnapshot struct {
frame protocol.ChatEventFrame
order uint64
}
// Called under mu. Each update replaces the full session snapshot, including
// unavailable fields. Native usage never modifies starting/turns/busy state.
func (c *Chat) retainUsageLocked(frame protocol.ChatEventFrame) bool {
if c.closed || c.connectionErr != nil || frame.SessionID == "" || frame.Usage == nil {
return false
}
if frame.NativeTurnID != "" {
current := c.turns[frame.SessionID]
if current == "" {
current = c.endedTurns[frame.SessionID]
}
if current != "" && current != frame.NativeTurnID {
return false
}
}
if c.usage == nil {
c.usage = map[string]usageSnapshot{}
}
c.usageOrder++
c.usage[frame.SessionID] = usageSnapshot{frame: frame, order: c.usageOrder}
if len(c.usage) > retainedUsageLimit {
var oldest string
var order uint64
for id, snapshot := range c.usage {
if oldest == "" || snapshot.order < order {
oldest, order = id, snapshot.order
}
}
delete(c.usage, oldest)
}
return true
}
// ReleaseShells is the existing room/source ownership-release hook. Retain a
// snapshot across ordinary stream reattach, but not after the room is released.
func (c *Chat) ReleaseShells(sessionID string) {
c.mu.Lock()
delete(c.commands, sessionID)
delete(c.usage, sessionID)
c.mu.Unlock()
}