Keep last reported OpenCode metrics while the newest assistant is in flight

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.
This commit is contained in:
2026-10-21 00:21:05 +01:00
parent ce3482b057
commit 910028a75c
2 changed files with 197 additions and 26 deletions
+44 -23
View File
@@ -69,19 +69,28 @@ type usageModelCache struct {
limit *int64
at time.Time
}
const usageAssistantLimit = 8
type usageMessage struct {
Type string `json:"type"`
Tokens usageTokens `json:"tokens"`
Time struct {
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 at most one session, one latest assistant and one latest compaction.
// 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")
@@ -114,40 +123,48 @@ func (c *Chat) Usage(ctx context.Context, id string) (*protocol.ChatUsage, error
return nil, fmt.Errorf("invalid OpenCode usage session")
}
u.ContextWindow = c.usageModelLimit(ctx, info.Location.Directory, info.Model.Provider, info.Model.ID)
latest := func(kind string) (*usageMessage, bool) {
body, e := c.usageRead(ctx, "/api/session/"+id+"/message?type="+kind+"&order=desc&limit=1", c.conn.Directory, 4<<20)
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) > 1 {
if json.Unmarshal(body, &page) != nil || page.Data == nil || len(page.Data) > limit {
return nil, false
}
if len(page.Data) == 0 {
return nil, true
for _, message := range page.Data {
if message.Type != kind {
return nil, false
}
}
if page.Data[0].Type != kind {
return nil, false
}
return &page.Data[0], true
return page.Data, true
}
a, _ := latest("assistant")
if a == nil {
assistants, _ := recent("assistant", usageAssistantLimit)
if len(assistants) == 0 || info.Model.ID == "" || info.Model.Provider == "" {
return u, nil
}
compaction, boundaryKnown := latest("compaction")
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.
if boundaryKnown && a.Time.Created != nil && (compaction == nil || (compaction.Time.Created != nil && *compaction.Time.Created < *a.Time.Created)) {
u.ContextTokens = tokenSum(a.Tokens.Input, a.Tokens.Cache.Read, a.Tokens.Cache.Write)
}
if 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"
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
@@ -185,7 +202,11 @@ func (c *Chat) usageModelLimit(ctx context.Context, directory, provider, id stri
c.usageMu.Lock()
cached, ok := c.usageModels[key]
c.usageMu.Unlock()
if ok && time.Since(cached.at) < 5*time.Minute {
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