910028a75c
Read a bounded newest-first page of eight assistants (4 MiB, no cursor traversal), selecting context and completed generation rate independently from the latest valid measurement matching Session.Info.model provider/id. Require assistant model identity; never pair an old model's measurements with the selected model's window. Context remains unknown across a newer compaction until a subsequent prompt reports usage; the last completed matching rate may survive compaction. Unknown model metadata is cached for 15 seconds instead of five minutes, while known capacities retain their five-minute cache. Provenance remains canonical V2 Model.Ref, TokenUsage and request-created/streamed/completed fields cited in chat_usage.go. No live endpoint access, inference or service changes. Isolated HTTP regressions cover in-flight/null/empty newest tokens with prior known metrics, post-compaction boundaries, independent prompt/rate selection, switched/missing model identity, duplicate known-zero measurements, over-limit page rejection and metadata recovery. Checked full internal/opencode suite, focused usage race suite, go vet ./internal/opencode and git diff --check.
241 lines
7.9 KiB
Go
241 lines
7.9 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// Provenance: OpenCode V2 4617210822bdbb31e5749751afbfb4584ccedac8,
|
|
// packages/schema/src/token-usage.ts, session-event.ts and
|
|
// packages/core/src/session/{usage,projector,message-updater}.ts, runner/step.ts.
|
|
// UsageUpdated is a cumulative projection, not a delta. Native output is visible
|
|
// output (reasoning is separate); input excludes cache. Never price locally.
|
|
// message-updater sets created from Step.Started.started (request dispatch),
|
|
// streamed at Step.Streamed (provider body exhausted), completed after tools.
|
|
// Thus created->streamed is generation incl. provider latency, NOT tool time.
|
|
type usageTokens struct {
|
|
Input *int64 `json:"input"`
|
|
Output *int64 `json:"output"`
|
|
Reasoning *int64 `json:"reasoning"`
|
|
Cache struct {
|
|
Read *int64 `json:"read"`
|
|
Write *int64 `json:"write"`
|
|
} `json:"cache"`
|
|
}
|
|
|
|
func tokenSum(values ...*int64) *int64 {
|
|
var total int64
|
|
for _, value := range values {
|
|
if value == nil || *value < 0 || *value > math.MaxInt64-total {
|
|
return nil
|
|
}
|
|
total += *value
|
|
}
|
|
return &total
|
|
}
|
|
|
|
func decodeUsageSnapshot(data []byte) *protocol.ChatUsage {
|
|
var record struct {
|
|
Tokens usageTokens `json:"tokens"`
|
|
Cost *float64 `json:"cost"`
|
|
Input int `json:"inputTokens"`
|
|
Output int `json:"outputTokens"`
|
|
}
|
|
if json.Unmarshal(data, &record) != nil {
|
|
return &protocol.ChatUsage{Scope: "session"}
|
|
}
|
|
u := &protocol.ChatUsage{Scope: "session", InputTokens: record.Input, OutputTokens: record.Output}
|
|
if record.Tokens.Input != nil {
|
|
u.InputTokens = int(*record.Tokens.Input)
|
|
}
|
|
if record.Tokens.Output != nil {
|
|
u.OutputTokens = int(*record.Tokens.Output)
|
|
}
|
|
u.TotalTokens = tokenSum(record.Tokens.Input, record.Tokens.Output, record.Tokens.Reasoning, record.Tokens.Cache.Read, record.Tokens.Cache.Write)
|
|
if record.Cost != nil && *record.Cost >= 0 && !math.IsNaN(*record.Cost) && !math.IsInf(*record.Cost, 0) {
|
|
u.CostUSD = record.Cost
|
|
u.Cost = *record.Cost
|
|
}
|
|
return u
|
|
}
|
|
|
|
type usageModelCache struct {
|
|
limit *int64
|
|
at time.Time
|
|
}
|
|
|
|
const usageAssistantLimit = 8
|
|
|
|
type usageMessage struct {
|
|
Type string `json:"type"`
|
|
Tokens usageTokens `json:"tokens"`
|
|
Model struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"providerID"`
|
|
} `json:"model"`
|
|
Time struct {
|
|
Created *float64 `json:"created"`
|
|
Streamed *float64 `json:"streamed"`
|
|
Completed *float64 `json:"completed"`
|
|
} `json:"time"`
|
|
}
|
|
|
|
// Usage reads one session, at most eight recent assistants and one compaction.
|
|
// No unbounded history, CLI export, model/provider creation or inference. Missing
|
|
// fields remain unknown. After compaction an old prompt's usage is not current.
|
|
// An in-flight assistant need not have tokens yet: context and rate independently
|
|
// retain the latest reported measurement for the selected model within this page.
|
|
func (c *Chat) Usage(ctx context.Context, id string) (*protocol.ChatUsage, error) {
|
|
if !ValidSessionID(id) {
|
|
return nil, fmt.Errorf("invalid OpenCode session id")
|
|
}
|
|
if c.conn.Backend == "v1" {
|
|
return &protocol.ChatUsage{Scope: "session"}, nil
|
|
}
|
|
data, err := c.call(ctx, "get", "/api/session/"+id, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var session struct {
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(data, &session) != nil || len(session.Data) == 0 {
|
|
return nil, fmt.Errorf("invalid OpenCode usage session")
|
|
}
|
|
u := decodeUsageSnapshot(session.Data)
|
|
var info struct {
|
|
ID string `json:"id"`
|
|
Model struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"providerID"`
|
|
} `json:"model"`
|
|
Location struct {
|
|
Directory string `json:"directory"`
|
|
} `json:"location"`
|
|
}
|
|
if json.Unmarshal(session.Data, &info) != nil || info.ID != id {
|
|
return nil, fmt.Errorf("invalid OpenCode usage session")
|
|
}
|
|
u.ContextWindow = c.usageModelLimit(ctx, info.Location.Directory, info.Model.Provider, info.Model.ID)
|
|
recent := func(kind string, limit int) ([]usageMessage, bool) {
|
|
body, e := c.usageRead(ctx, fmt.Sprintf("/api/session/%s/message?type=%s&order=desc&limit=%d", id, kind, limit), c.conn.Directory, 4<<20)
|
|
if e != nil {
|
|
return nil, false
|
|
}
|
|
var page struct {
|
|
Data []usageMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(body, &page) != nil || page.Data == nil || len(page.Data) > limit {
|
|
return nil, false
|
|
}
|
|
for _, message := range page.Data {
|
|
if message.Type != kind {
|
|
return nil, false
|
|
}
|
|
}
|
|
return page.Data, true
|
|
}
|
|
assistants, _ := recent("assistant", usageAssistantLimit)
|
|
if len(assistants) == 0 || info.Model.ID == "" || info.Model.Provider == "" {
|
|
return u, nil
|
|
}
|
|
compactions, boundaryKnown := recent("compaction", 1)
|
|
// Failure to establish the compaction boundary must not revive old context.
|
|
// Empty compaction pages are valid, unlike failed/malformed reads.
|
|
for _, a := range assistants {
|
|
// Model.Ref is required by V2. Missing identity is not evidence that an
|
|
// old model's prompt/rate belongs with the newly selected capacity.
|
|
// Variants may change between calls; these remain last-reported figures,
|
|
// not a prediction or an instantaneous rate for the in-flight variant.
|
|
if a.Model.ID != info.Model.ID || a.Model.Provider != info.Model.Provider {
|
|
continue
|
|
}
|
|
if u.ContextTokens == nil && boundaryKnown && a.Time.Created != nil && (len(compactions) == 0 || (compactions[0].Time.Created != nil && *compactions[0].Time.Created < *a.Time.Created)) {
|
|
u.ContextTokens = tokenSum(a.Tokens.Input, a.Tokens.Cache.Read, a.Tokens.Cache.Write)
|
|
}
|
|
if u.OutputTokensPerSecond == nil && a.Time.Completed != nil && a.Time.Created != nil && a.Time.Streamed != nil && *a.Time.Created >= 0 && *a.Time.Streamed > *a.Time.Created && *a.Time.Completed >= *a.Time.Streamed && a.Tokens.Output != nil && *a.Tokens.Output >= 0 {
|
|
rate := float64(*a.Tokens.Output) * 1000 / (*a.Time.Streamed - *a.Time.Created)
|
|
if !math.IsNaN(rate) && !math.IsInf(rate, 0) {
|
|
u.OutputTokensPerSecond = &rate
|
|
u.RateBasis = "generation"
|
|
}
|
|
}
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
// Reuses the authenticated, location-scoped transport, with an explicit bounded
|
|
// allowance for large model catalogs. It never changes the shared connection's
|
|
// directory or stage export/format pin. Metadata is cached by catalog id AND
|
|
// provider AND location, not the upstream modelID (aliases can differ).
|
|
func (c *Chat) usageRead(ctx context.Context, path, directory string, budget int64) ([]byte, error) {
|
|
c.mu.Lock()
|
|
conn := *c.conn
|
|
c.mu.Unlock()
|
|
conn.Directory = directory
|
|
body, err := conn.HistoryNative(ctx, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer body.Close()
|
|
data, err := io.ReadAll(io.LimitReader(body, budget+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(len(data)) > budget {
|
|
return nil, fmt.Errorf("OpenCode metrics read exceeds budget")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (c *Chat) usageModelLimit(ctx context.Context, directory, provider, id string) *int64 {
|
|
if directory == "" || provider == "" || id == "" {
|
|
return nil
|
|
}
|
|
key := directory + "\x00" + provider + "\x00" + id
|
|
c.usageMu.Lock()
|
|
cached, ok := c.usageModels[key]
|
|
c.usageMu.Unlock()
|
|
ttl := 5 * time.Minute
|
|
if cached.limit == nil {
|
|
ttl = 15 * time.Second
|
|
}
|
|
if ok && time.Since(cached.at) < ttl {
|
|
return cached.limit
|
|
}
|
|
var limit *int64
|
|
data, err := c.usageRead(ctx, "/api/model", directory, 16<<20)
|
|
if err == nil {
|
|
var catalog struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"providerID"`
|
|
Limit struct {
|
|
Context *int64 `json:"context"`
|
|
} `json:"limit"`
|
|
} `json:"data"`
|
|
}
|
|
if json.Unmarshal(data, &catalog) == nil {
|
|
for _, m := range catalog.Data {
|
|
if m.ID == id && m.Provider == provider && m.Limit.Context != nil && *m.Limit.Context >= 0 {
|
|
limit = m.Limit.Context
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
c.usageMu.Lock()
|
|
if c.usageModels == nil || len(c.usageModels) >= 128 {
|
|
c.usageModels = make(map[string]usageModelCache)
|
|
}
|
|
c.usageModels[key] = usageModelCache{limit: limit, at: time.Now()}
|
|
c.usageMu.Unlock()
|
|
return limit
|
|
}
|