From d079dc62239953ea59d573fa9030d871c6e2f0fb Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 23:36:40 +0800 Subject: [PATCH] =?UTF-8?q?usage:=20an=20OpenCode=20session's=20row=20show?= =?UTF-8?q?s=20the=20effort=20and=20the=20model=20it=20asked=20for=20(#680?= =?UTF-8?q?,=20Sun1090:=20=E6=95=B0=E6=8D=AE=E4=B8=8D=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E6=8E=A8=E7=90=86=E5=BC=BA=E5=BA=A6=E6=98=AF=E6=9C=89?= =?UTF-8?q?=E7=9A=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OpenCode rows read from its own data (opencode.db or storage/) had 请求模型 and 推理强度 as —. OpenCode keeps both on each reply (packages/schema/src/v1/session.ts, AssistantMessage): variant, the effort picked in its model menu for the prompt (high, max, low, xhigh, none, thinking … as the provider's variants name them; prompt.ts copies lastUser.model.variant onto every reply), and modelID, the model the reply was sent to. OpenCode 2 (session_message) keeps the variant in model.variant (Model.Ref). It records no model the response named, so modelID is what was asked for, as a Codex rollout's model is. Now a call's Effort is the reply's variant ("default" or none is no effort picked); a compaction in OpenCode 2 takes the effort last replied with, as it takes the model. In the ledger an OpenCode call is told as a Codex one: requested = its model, served left empty rather than claimed. The call cache moves to calls-v5 so calls read before carry the effort. TestOpenCodeCallsJSON/DB/V2 (fixtures with variant high, default, xhigh and none) and TestOpenCodeSessionCallIsWhatItAskedFor fail on the old code and pass. go vet ./internal/..., builds (nogui, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried against a real OpenCode database with variants (this machine's OpenCode data predates them). --- internal/sessions/call_cache.go | 3 ++- internal/sessions/calls_opencode.go | 5 +++++ internal/sessions/calls_opencode_test.go | 18 ++++++++++------ internal/sessions/opencode.go | 15 ++++++++----- internal/sessions/opencode_pi_test.go | 2 +- .../msg_0001.json | 3 ++- .../msg_0002.json | 1 + internal/usage/ledger.go | 7 ++++--- internal/usage/session_provider_test.go | 21 +++++++++++++++++++ 9 files changed, 58 insertions(+), 17 deletions(-) diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 443945cb3..47823d019 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -21,7 +21,8 @@ import ( // v3 persists Codex cumulative counters explicitly; v2 shards can contain // inflated deltas after a reload, so rebuild their derived rows from source. // v4: a Codex call's input no longer holds what it wrote to the cache (#589). -const callCacheVersion = "calls-v4" +// v5: an OpenCode call keeps the effort its prompt asked for (#680). +const callCacheVersion = "calls-v5" const maxKeptCalls = 131072 const maxKeptFiles = 64 diff --git a/internal/sessions/calls_opencode.go b/internal/sessions/calls_opencode.go index 187c976d9..0d4f090e1 100644 --- a/internal/sessions/calls_opencode.go +++ b/internal/sessions/calls_opencode.go @@ -218,6 +218,11 @@ func ocCall(st *callFile, m ocMessage, dir string) (Call, bool) { if m.Path != nil && m.Path.Cwd != "" { c.Cwd = st.str(m.Path.Cwd) } + // the effort picked for the prompt in OpenCode's model menu, which each + // of its replies carries; "default" is none picked (#680) + if m.Variant != "" && m.Variant != "default" { + c.Effort = st.str(m.Variant) + } if !done.IsZero() && !created.IsZero() && done.After(created) { c.Millis = done.Sub(created).Milliseconds() } diff --git a/internal/sessions/calls_opencode_test.go b/internal/sessions/calls_opencode_test.go index 3c2261b7c..c778905db 100644 --- a/internal/sessions/calls_opencode_test.go +++ b/internal/sessions/calls_opencode_test.go @@ -38,9 +38,13 @@ func checkOCCalls(t *testing.T, cs map[string]Call) { !a.Time.Equal(at("2026-09-26T10:00:30Z")) { t.Fatalf("astra: %+v", a) } + // the effort picked in OpenCode's model menu, its reply's variant (#680) + if a.Effort != "high" { + t.Fatalf("astra's effort %q, want high", a.Effort) + } o, ok := cs["msg_0004"] if !ok || o.Model != "claude-opus-5-5" || o.Upstream != "anthropic" || o.Session != ocMain || - o.Tokens != (Tokens{40, 60, 700, 300}) || o.Millis != 30000 { + o.Tokens != (Tokens{40, 60, 700, 300}) || o.Millis != 30000 || o.Effort != "" { t.Fatalf("the subagent's opus: %+v", o) } if len(cs) != 2 { @@ -81,11 +85,12 @@ func TestOpenCodeCallsDB(t *testing.T) { // a failed reply is a failed call; one only stopped is none ocInsert(t, db, ocChild, "msg_0005", 1790416990000, 1790417000000, - `{"id":"msg_0005","role":"assistant","modelID":"claude-opus-5-5","providerID":"anthropic","tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"error":{"name":"APIError","data":{"message":"Overloaded","statusCode":529}},"time":{"created":1790416990000,"completed":1790417000000}}`) + `{"id":"msg_0005","role":"assistant","modelID":"claude-opus-5-5","providerID":"anthropic","variant":"default","tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"error":{"name":"APIError","data":{"message":"Overloaded","statusCode":529}},"time":{"created":1790416990000,"completed":1790417000000}}`) ocInsert(t, db, ocChild, "msg_0006", 1790417010000, 1790417020000, `{"id":"msg_0006","role":"assistant","modelID":"claude-opus-5-5","providerID":"anthropic","tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"error":{"name":"MessageAbortedError","data":{"message":"aborted"}},"time":{"created":1790417010000}}`) cs := ocCalls(t) - if e := cs["msg_0005"]; e.Error != "APIError" || e.ErrorText != "Overloaded" || e.Session != ocMain { + // "default" is no effort picked + if e := cs["msg_0005"]; e.Error != "APIError" || e.ErrorText != "Overloaded" || e.Session != ocMain || e.Effort != "" { t.Fatalf("a failed reply: %+v", e) } if _, ok := cs["msg_0006"]; ok { @@ -105,7 +110,7 @@ func TestOpenCodeCallsV2(t *testing.T) { } // and one after OpenCode's own reply is OpenCode's if _, err := db.Exec(`INSERT INTO session_message VALUES ('msg_0007', ?, 'assistant', 10, 1790417020000, 1790417030000, ?), ('msg_0008', ?, 'compaction', 11, 1790417040000, 1790417050000, ?)`, - ocMain, `{"model":{"id":"big-pickle","providerID":"opencode"},"content":[],"cost":0,"tokens":{"input":7,"output":3,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417020000,"completed":1790417030000}}`, + ocMain, `{"model":{"id":"big-pickle","providerID":"opencode","variant":"xhigh"},"content":[],"cost":0,"tokens":{"input":7,"output":3,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417020000,"completed":1790417030000}}`, ocMain, `{"status":"completed","reason":"auto","cost":0,"tokens":{"input":11,"output":2,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417040000}}`); err != nil { t.Fatal(err) } @@ -113,10 +118,11 @@ func TestOpenCodeCallsV2(t *testing.T) { if _, ok := cs["msg_0005"]; ok { t.Fatalf("a compaction through magpie counted: %+v", cs["msg_0005"]) } - if c := cs["msg_0007"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{7, 3, 0, 0}) { + if c := cs["msg_0007"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{7, 3, 0, 0}) || c.Effort != "xhigh" { t.Fatalf("an OpenCode Zen reply: %+v", c) } - if c := cs["msg_0008"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{11, 2, 0, 0}) { + // a compaction is made with the model and effort last replied with + if c := cs["msg_0008"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{11, 2, 0, 0}) || c.Effort != "xhigh" { t.Fatalf("its compaction: %+v", c) } } diff --git a/internal/sessions/opencode.go b/internal/sessions/opencode.go index 8d970c3d3..dd941c988 100644 --- a/internal/sessions/opencode.go +++ b/internal/sessions/opencode.go @@ -84,6 +84,10 @@ type ocMessage struct { ModelID string `json:"modelID"` // the provider the reply came from: magpie's gateway is "magpie" ProviderID string `json:"providerID,omitempty"` + // a reply's variant: the reasoning effort picked in OpenCode's model + // menu for its prompt (high, max, low …; "default" or none when none + // was). OpenCode 2 keeps it in model.variant (#680) + Variant string `json:"variant,omitempty"` // what ended the reply, when something did: {name, data {message}}, or // in OpenCode 2 {type, message} Error json.RawMessage `json:"error,omitempty"` @@ -316,7 +320,7 @@ func (s ocV2DB) messages(sid string) [][]byte { } defer rows.Close() var out [][]byte - last, lastProvider := "", "" + last, lastProvider, lastVariant := "", "", "" for rows.Next() { var id, typ string var b []byte @@ -328,20 +332,21 @@ func (s ocV2DB) messages(sid string) [][]byte { Model *struct { ID string `json:"id"` ProviderID string `json:"providerID"` + Variant string `json:"variant"` } `json:"model"` } if json.Unmarshal(b, &m) != nil || json.Unmarshal(b, &v) != nil { continue } - m.ID, m.Role, m.ModelID, m.ProviderID = id, "assistant", last, lastProvider + m.ID, m.Role, m.ModelID, m.ProviderID, m.Variant = id, "assistant", last, lastProvider, lastVariant if v.Model != nil && v.Model.ID != "" { - m.ModelID, m.ProviderID = v.Model.ID, v.Model.ProviderID + m.ModelID, m.ProviderID, m.Variant = v.Model.ID, v.Model.ProviderID, v.Model.Variant } switch typ { case "user": - m.Role, m.ModelID, m.ProviderID, m.Tokens = "user", "", "", nil + m.Role, m.ModelID, m.ProviderID, m.Variant, m.Tokens = "user", "", "", "", nil case "assistant": - last, lastProvider = m.ModelID, m.ProviderID + last, lastProvider, lastVariant = m.ModelID, m.ProviderID, m.Variant } if o, err := json.Marshal(m); err == nil { out = append(out, o) diff --git a/internal/sessions/opencode_pi_test.go b/internal/sessions/opencode_pi_test.go index 725cb3fdb..f8a8334fd 100644 --- a/internal/sessions/opencode_pi_test.go +++ b/internal/sessions/opencode_pi_test.go @@ -314,7 +314,7 @@ func ocMakeV2(t *testing.T, path string) *sql.DB { for i, m := range []struct{ sid, id, typ, data string }{ {ocMain, "msg_0001", "user", `{"text":"Why does the login test\nflake?","time":{"created":1790416805000}}`}, {ocMain, "msg_0001s", "synthetic", `{"text":"Called the Read tool with the following input","time":{"created":1790416805000}}`}, - {ocMain, "msg_0002", "assistant", `{"agent":"build","model":{"id":"gpt-6-astra","providerID":"openai","variant":"default"},"content":[{"type":"reasoning","text":"…"},{"type":"text","text":"Let me look."}],"cost":0.01,"tokens":{"input":1000,"output":100,"reasoning":20,"cache":{"read":5000,"write":0}},"time":{"created":1790416806000,"completed":1790416830000}}`}, + {ocMain, "msg_0002", "assistant", `{"agent":"build","model":{"id":"gpt-6-astra","providerID":"openai","variant":"high"},"content":[{"type":"reasoning","text":"…"},{"type":"text","text":"Let me look."}],"cost":0.01,"tokens":{"input":1000,"output":100,"reasoning":20,"cache":{"read":5000,"write":0}},"time":{"created":1790416806000,"completed":1790416830000}}`}, {ocMain, "msg_0003", "assistant", `{"agent":"build","model":{"id":"codex/gpt-6-luna","providerID":"magpie","variant":"default"},"content":[{"type":"text","text":"It races the clock."}],"cost":0,"tokens":{"input":300,"output":50,"reasoning":10,"cache":{"read":2000,"write":0}},"time":{"created":1790416890000,"completed":1790416980000}}`}, {ocMain, "msg_0003i", "idle", `{"outcome":"succeeded","time":{"created":1790416980000}}`}, {ocChild, "msg_0004", "assistant", `{"agent":"general","model":{"id":"claude-opus-5-5","providerID":"anthropic"},"content":[],"cost":0,"tokens":{"input":40,"output":60,"reasoning":0,"cache":{"read":700,"write":300}},"time":{"created":1790416860000,"completed":1790416890000}}`}, diff --git a/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0001.json b/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0001.json index 22ef9b28a..6cf151450 100644 --- a/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0001.json +++ b/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0001.json @@ -8,6 +8,7 @@ "agent": "build", "model": { "providerID": "openai", - "modelID": "gpt-6-astra" + "modelID": "gpt-6-astra", + "variant": "high" } } diff --git a/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0002.json b/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0002.json index b1dca2a86..ca2491397 100644 --- a/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0002.json +++ b/internal/sessions/testdata/opencode/storage/message/ses_1a2b3c4d5e6fAAAAAAAAAAAAAA/msg_0002.json @@ -25,5 +25,6 @@ "created": 1790416806000, "completed": 1790416830000 }, + "variant": "high", "finish": "tool-calls" } diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index 5be91cc17..affca5f65 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -224,13 +224,14 @@ const UnknownProvider = "session-unknown" // unknown, and what the file doesn't say is left out. Nothing // stood between the agent and the vendor, so what was sent is what the agent // asked for. Claude Code's file names the model it asked for (as it runs) and -// the one the API answered with, which is the served model; Codex's names the -// one it asked for. +// the one the API answered with, which is the served model; Codex's and +// OpenCode's name only the one they asked for (OpenCode's reply keeps the +// model it was sent to, never the one the response named) (#680). func logRecord(c sessions.Call) Record { r := Record{Time: c.Time, Agent: c.Agent, Provider: UnknownProvider, Model: c.Model, Served: c.Model, Requested: c.Requested, Input: c.Input, Output: c.Output, CacheRead: c.CacheRead, CacheWrite: c.CacheWrite, Reasoning: c.Reasoning, Effort: c.Effort, Millis: c.Millis, TTFT: c.TTFT, Session: c.Session, RequestID: c.RequestID, Error: c.ErrorText, ErrType: c.Error} - if c.Agent == "codex" { + if c.Agent == "codex" || c.Agent == "opencode" { r.Requested, r.Served = c.Model, "" } if r.Requested != "" { diff --git a/internal/usage/session_provider_test.go b/internal/usage/session_provider_test.go index 1bbe74f9c..4ee00a12b 100644 --- a/internal/usage/session_provider_test.go +++ b/internal/usage/session_provider_test.go @@ -270,3 +270,24 @@ func TestLedgerKeepsLocalAccountsSeparateFromGatewayProviders(t *testing.T) { t.Fatalf("local identity was lost or guessed: %+v", rows) } } + +// An OpenCode reply names only the model it was sent to, and the effort +// picked for its prompt (#680): the row's requested model and effort, not a +// served model the file never saw. +func TestOpenCodeSessionCallIsWhatItAskedFor(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming")) + t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local")) + logs := []sessions.Call{{Time: time.Now(), Agent: "opencode", Model: "space-bunny-free", Upstream: "opencode", Effort: "high", + Tokens: sessions.Tokens{Input: 10, Output: 2}}} + rows, _, _, _ := ledgerWith(time.Time{}, Filter{}, nil, logs) + if len(rows) != 1 { + t.Fatalf("rows %+v", rows) + } + if r := rows[0]; r.Requested != "space-bunny-free" || r.Model != "space-bunny-free" || r.Served != "" || r.Effort != "high" { + t.Fatalf("an OpenCode call: requested %q model %q served %q effort %q", r.Requested, r.Model, r.Served, r.Effort) + } +}