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:
@@ -69,19 +69,28 @@ type usageModelCache struct {
|
|||||||
limit *int64
|
limit *int64
|
||||||
at time.Time
|
at time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const usageAssistantLimit = 8
|
||||||
|
|
||||||
type usageMessage struct {
|
type usageMessage struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Tokens usageTokens `json:"tokens"`
|
Tokens usageTokens `json:"tokens"`
|
||||||
Time struct {
|
Model struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Provider string `json:"providerID"`
|
||||||
|
} `json:"model"`
|
||||||
|
Time struct {
|
||||||
Created *float64 `json:"created"`
|
Created *float64 `json:"created"`
|
||||||
Streamed *float64 `json:"streamed"`
|
Streamed *float64 `json:"streamed"`
|
||||||
Completed *float64 `json:"completed"`
|
Completed *float64 `json:"completed"`
|
||||||
} `json:"time"`
|
} `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
|
// No unbounded history, CLI export, model/provider creation or inference. Missing
|
||||||
// fields remain unknown. After compaction an old prompt's usage is not current.
|
// 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) {
|
func (c *Chat) Usage(ctx context.Context, id string) (*protocol.ChatUsage, error) {
|
||||||
if !ValidSessionID(id) {
|
if !ValidSessionID(id) {
|
||||||
return nil, fmt.Errorf("invalid OpenCode session 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")
|
return nil, fmt.Errorf("invalid OpenCode usage session")
|
||||||
}
|
}
|
||||||
u.ContextWindow = c.usageModelLimit(ctx, info.Location.Directory, info.Model.Provider, info.Model.ID)
|
u.ContextWindow = c.usageModelLimit(ctx, info.Location.Directory, info.Model.Provider, info.Model.ID)
|
||||||
latest := func(kind string) (*usageMessage, bool) {
|
recent := func(kind string, limit int) ([]usageMessage, bool) {
|
||||||
body, e := c.usageRead(ctx, "/api/session/"+id+"/message?type="+kind+"&order=desc&limit=1", c.conn.Directory, 4<<20)
|
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 {
|
if e != nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
var page struct {
|
var page struct {
|
||||||
Data []usageMessage `json:"data"`
|
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
|
return nil, false
|
||||||
}
|
}
|
||||||
if len(page.Data) == 0 {
|
for _, message := range page.Data {
|
||||||
return nil, true
|
if message.Type != kind {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if page.Data[0].Type != kind {
|
return page.Data, true
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
return &page.Data[0], true
|
|
||||||
}
|
}
|
||||||
a, _ := latest("assistant")
|
assistants, _ := recent("assistant", usageAssistantLimit)
|
||||||
if a == nil {
|
if len(assistants) == 0 || info.Model.ID == "" || info.Model.Provider == "" {
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
compaction, boundaryKnown := latest("compaction")
|
compactions, boundaryKnown := recent("compaction", 1)
|
||||||
// Failure to establish the compaction boundary must not revive old context.
|
// Failure to establish the compaction boundary must not revive old context.
|
||||||
// Empty compaction pages are valid, unlike failed/malformed reads.
|
// 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)) {
|
for _, a := range assistants {
|
||||||
u.ContextTokens = tokenSum(a.Tokens.Input, a.Tokens.Cache.Read, a.Tokens.Cache.Write)
|
// Model.Ref is required by V2. Missing identity is not evidence that an
|
||||||
}
|
// old model's prompt/rate belongs with the newly selected capacity.
|
||||||
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 {
|
// Variants may change between calls; these remain last-reported figures,
|
||||||
rate := float64(*a.Tokens.Output) * 1000 / (*a.Time.Streamed - *a.Time.Created)
|
// not a prediction or an instantaneous rate for the in-flight variant.
|
||||||
if !math.IsNaN(rate) && !math.IsInf(rate, 0) {
|
if a.Model.ID != info.Model.ID || a.Model.Provider != info.Model.Provider {
|
||||||
u.OutputTokensPerSecond = &rate
|
continue
|
||||||
u.RateBasis = "generation"
|
}
|
||||||
|
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
|
return u, nil
|
||||||
@@ -185,7 +202,11 @@ func (c *Chat) usageModelLimit(ctx context.Context, directory, provider, id stri
|
|||||||
c.usageMu.Lock()
|
c.usageMu.Lock()
|
||||||
cached, ok := c.usageModels[key]
|
cached, ok := c.usageModels[key]
|
||||||
c.usageMu.Unlock()
|
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
|
return cached.limit
|
||||||
}
|
}
|
||||||
var limit *int64
|
var limit *int64
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package opencode
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -79,11 +80,15 @@ func TestUsageReadbackNativeProvenanceAndModelCache(t *testing.T) {
|
|||||||
// another provider's otherwise-identical id.
|
// another provider's otherwise-identical id.
|
||||||
fmt.Fprintf(w, `{"padding":%q,"data":[{"id":"alias-a","modelID":"upstream","providerID":"wrong","limit":{"context":1}},{"id":"alias-a","modelID":"upstream","providerID":"p","limit":{"context":200000}},{"id":"alias-b","modelID":"upstream","providerID":"p","limit":{"context":300000}}]}`, strings.Repeat("x", MaxResponse+10))
|
fmt.Fprintf(w, `{"padding":%q,"data":[{"id":"alias-a","modelID":"upstream","providerID":"wrong","limit":{"context":1}},{"id":"alias-a","modelID":"upstream","providerID":"p","limit":{"context":200000}},{"id":"alias-b","modelID":"upstream","providerID":"p","limit":{"context":300000}}]}`, strings.Repeat("x", MaxResponse+10))
|
||||||
case "/api/session/ses_usage/message":
|
case "/api/session/ses_usage/message":
|
||||||
if r.URL.Query().Get("limit") != "1" || r.URL.Query().Get("order") != "desc" {
|
limit := "1"
|
||||||
|
if r.URL.Query().Get("type") == "assistant" {
|
||||||
|
limit = "8"
|
||||||
|
}
|
||||||
|
if r.URL.Query().Get("limit") != limit || r.URL.Query().Get("order") != "desc" {
|
||||||
t.Errorf("unbounded metrics history %s", r.URL.RawQuery)
|
t.Errorf("unbounded metrics history %s", r.URL.RawQuery)
|
||||||
}
|
}
|
||||||
if r.URL.Query().Get("type") == "assistant" {
|
if r.URL.Query().Get("type") == "assistant" {
|
||||||
fmt.Fprint(w, `{"data":[{"type":"assistant","time":{"created":1000,"streamed":3000,"completed":23000},`+usageFixtureTokens+`}]}`)
|
fmt.Fprintf(w, `{"data":[{"type":"assistant","model":{"id":%q,"providerID":"p"},"time":{"created":1000,"streamed":3000,"completed":23000},`+usageFixtureTokens+`}]}`, model)
|
||||||
} else if missingCompaction {
|
} else if missingCompaction {
|
||||||
w.WriteHeader(503)
|
w.WriteHeader(503)
|
||||||
} else if compaction {
|
} else if compaction {
|
||||||
@@ -168,6 +173,9 @@ func TestUsagePartialFieldsAndTimingNeverInvented(t *testing.T) {
|
|||||||
{"zero-native-output", `"tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}}`, `"created":1000,"streamed":3000,"completed":5000`, `"model":{"id":"m","providerID":"p"},"location":{"directory":"/actual"},`, usageInt(0), usageInt(0), usageFloat(0)},
|
{"zero-native-output", `"tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}}`, `"created":1000,"streamed":3000,"completed":5000`, `"model":{"id":"m","providerID":"p"},"location":{"directory":"/actual"},`, usageInt(0), usageInt(0), usageFloat(0)},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if tc.model == "" {
|
||||||
|
tc.model = `"model":{"id":"m","providerID":"p"},`
|
||||||
|
}
|
||||||
reads := 0
|
reads := 0
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
reads++
|
reads++
|
||||||
@@ -186,7 +194,7 @@ func TestUsagePartialFieldsAndTimingNeverInvented(t *testing.T) {
|
|||||||
if tokens != "" {
|
if tokens != "" {
|
||||||
tokens = "," + tokens
|
tokens = "," + tokens
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, `{"data":[{"type":"assistant","time":{`+tc.timing+`}`+tokens+`}]}`)
|
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"m","providerID":"p"},"time":{`+tc.timing+`}`+tokens+`}]}`)
|
||||||
default:
|
default:
|
||||||
t.Errorf("unexpected request %s", r.URL.Path)
|
t.Errorf("unexpected request %s", r.URL.Path)
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
@@ -270,3 +278,145 @@ func TestUsageStreamIdleAttachAndSettlement(t *testing.T) {
|
|||||||
t.Fatalf("idle/settlement snapshots=%v", totals)
|
t.Fatalf("idle/settlement snapshots=%v", totals)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUsageRecentAssistantsRetainLastMatchingMeasurements(t *testing.T) {
|
||||||
|
const selected = `"model":{"id":"m","providerID":"p"}`
|
||||||
|
const knownTokens = `"tokens":{"input":996,"output":71,"reasoning":0,"cache":{"read":479616,"write":0}}`
|
||||||
|
const knownTime = `"created":1792538187795,"streamed":1792538196498,"completed":1792538197150`
|
||||||
|
assistant := func(model, timing, tokens string) string {
|
||||||
|
if model != "" {
|
||||||
|
model += ","
|
||||||
|
}
|
||||||
|
return `{"type":"assistant",` + model + `"time":{` + timing + `},` + tokens + `}`
|
||||||
|
}
|
||||||
|
known := assistant(selected, knownTime, knownTokens)
|
||||||
|
inflight := assistant(selected, `"created":1792538200000,"streamed":1792538201000`, `"tokens":null`)
|
||||||
|
zero := assistant(selected, `"created":1792538198000,"streamed":1792538199000,"completed":1792538199500`, `"tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}}`)
|
||||||
|
rate := 71.0 / 8.703
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name, sessionModel, messages, compactions string
|
||||||
|
context *int64
|
||||||
|
rate *float64
|
||||||
|
}{
|
||||||
|
{"inflight-previous-known", selected, inflight + "," + known, `[]`, usageInt(480612), &rate},
|
||||||
|
{"empty-newest-previous-known", selected, assistant(selected, `"created":1792538200000`, `"tokens":{}`) + "," + known, `[]`, usageInt(480612), &rate},
|
||||||
|
{"post-compaction-no-new-prompt", selected, inflight + "," + known, `[{"type":"compaction","time":{"created":1792538198000}}]`, nil, &rate},
|
||||||
|
// Context and rate are independently last-reported, not necessarily the
|
||||||
|
// same assistant: the newest prompt can report tokens before completion.
|
||||||
|
{"new-prompt-after-compaction", selected, assistant(selected, `"created":1792538200000`, knownTokens) + "," + known, `[{"type":"compaction","time":{"created":1792538198000}}]`, usageInt(480612), &rate},
|
||||||
|
{"switched-model", `"model":{"id":"new","providerID":"p"}`, inflight + "," + known, `[]`, nil, nil},
|
||||||
|
{"wrong-provider", `"model":{"id":"m","providerID":"other"}`, known, `[]`, nil, nil},
|
||||||
|
{"missing-assistant-model", selected, assistant("", knownTime, knownTokens), `[]`, nil, nil},
|
||||||
|
{"missing-session-model", ``, known, `[]`, nil, nil},
|
||||||
|
{"duplicate-zero-is-known", selected, inflight + "," + zero + "," + zero + "," + known, `[]`, usageInt(0), usageFloat(0)},
|
||||||
|
{"reject-over-limit", selected, strings.TrimSuffix(strings.Repeat(known+",", usageAssistantLimit+1), ","), `[]`, nil, nil},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
reads := 0
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
reads++
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if r.Method != "GET" {
|
||||||
|
t.Errorf("unexpected write %s", r.Method)
|
||||||
|
w.WriteHeader(405)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/session/ses_usage":
|
||||||
|
model := tc.sessionModel
|
||||||
|
if model != "" {
|
||||||
|
model += ","
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, `{"data":{"id":"ses_usage",`+model+`"location":{"directory":"/scope"},"cost":0,`+usageFixtureTokens+`}}`)
|
||||||
|
case "/api/model":
|
||||||
|
fmt.Fprint(w, `{"data":[{"id":"m","providerID":"p","limit":{"context":1000000}},{"id":"new","providerID":"p","limit":{"context":2000000}}]}`)
|
||||||
|
case "/api/session/ses_usage/message":
|
||||||
|
if r.URL.Query().Get("order") != "desc" || r.URL.Query().Get("cursor") != "" {
|
||||||
|
t.Errorf("unexpected history request %s", r.URL.RawQuery)
|
||||||
|
}
|
||||||
|
switch r.URL.Query().Get("type") {
|
||||||
|
case "assistant":
|
||||||
|
if r.URL.Query().Get("limit") != "8" {
|
||||||
|
t.Errorf("unbounded assistant request %s", r.URL.RawQuery)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, `{"data":[`+tc.messages+`]}`)
|
||||||
|
case "compaction":
|
||||||
|
if r.URL.Query().Get("limit") != "1" {
|
||||||
|
t.Errorf("unbounded compaction request %s", r.URL.RawQuery)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, `{"data":`+tc.compactions+`}`)
|
||||||
|
default:
|
||||||
|
t.Errorf("unexpected history type %s", r.URL.RawQuery)
|
||||||
|
w.WriteHeader(404)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t.Errorf("unexpected path %s", r.URL.Path)
|
||||||
|
w.WriteHeader(404)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
chat := NewChat(&Connection{Backend: "v2", URL: server.URL})
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
u, err := chat.Usage(context.Background(), "ses_usage")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assertUsageInt(t, u.ContextTokens, tc.context)
|
||||||
|
assertUsageInt(t, u.TotalTokens, usageInt(185))
|
||||||
|
if (u.OutputTokensPerSecond == nil) != (tc.rate == nil) || tc.rate != nil && math.Abs(*u.OutputTokensPerSecond-*tc.rate) > 1e-9 {
|
||||||
|
t.Fatalf("rate=%v want=%v", u.OutputTokensPerSecond, tc.rate)
|
||||||
|
}
|
||||||
|
if tc.rate != nil && u.RateBasis != "generation" || tc.rate == nil && u.RateBasis != "" {
|
||||||
|
t.Fatalf("basis=%q", u.RateBasis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if reads > 7 {
|
||||||
|
t.Fatalf("unbounded two-read burst=%d", reads)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsageUnknownModelMetadataRetriesSoonerThanKnownMetadata(t *testing.T) {
|
||||||
|
reads := 0
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
reads++
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if reads == 1 {
|
||||||
|
w.WriteHeader(503)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, `{"data":[{"id":"m","providerID":"p","limit":{"context":1000000}}]}`)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
chat := NewChat(&Connection{Backend: "v2", URL: server.URL})
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
assertUsageInt(t, chat.usageModelLimit(context.Background(), "/scope", "p", "m"), nil)
|
||||||
|
}
|
||||||
|
if reads != 1 {
|
||||||
|
t.Fatalf("failure not briefly cached: %d", reads)
|
||||||
|
}
|
||||||
|
key := "/scope\x00p\x00m"
|
||||||
|
age := func(d time.Duration) {
|
||||||
|
chat.usageMu.Lock()
|
||||||
|
entry := chat.usageModels[key]
|
||||||
|
entry.at = time.Now().Add(-d)
|
||||||
|
chat.usageModels[key] = entry
|
||||||
|
chat.usageMu.Unlock()
|
||||||
|
}
|
||||||
|
age(16 * time.Second)
|
||||||
|
assertUsageInt(t, chat.usageModelLimit(context.Background(), "/scope", "p", "m"), usageInt(1000000))
|
||||||
|
if reads != 2 {
|
||||||
|
t.Fatalf("unknown metadata did not recover: %d", reads)
|
||||||
|
}
|
||||||
|
age(16 * time.Second)
|
||||||
|
assertUsageInt(t, chat.usageModelLimit(context.Background(), "/scope", "p", "m"), usageInt(1000000))
|
||||||
|
if reads != 2 {
|
||||||
|
t.Fatalf("known metadata lost five-minute cache: %d", reads)
|
||||||
|
}
|
||||||
|
age(6 * time.Minute)
|
||||||
|
assertUsageInt(t, chat.usageModelLimit(context.Background(), "/scope", "p", "m"), usageInt(1000000))
|
||||||
|
if reads != 3 {
|
||||||
|
t.Fatalf("known metadata did not refresh: %d", reads)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user