Show recorded OpenCode models in the chat list
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# OpenCode chat list model reporting
|
||||
|
||||
OpenCode's session summary and detail can omit `model` even after an assistant
|
||||
reply recorded one. The live operator session's detail omitted it; a read-only
|
||||
GET of its newest assistant message returned `deepseek-v4.1-flash` with provider
|
||||
`opencode-go`. The Flutter list already renders a supplied model correctly.
|
||||
|
||||
The server now resolves missing models after filtering to registered operator
|
||||
sessions. It prefers an explicit selected model in session detail, then reads
|
||||
a bounded recent assistant page. No configured/global default is invented for
|
||||
an empty chat. Existing listed models and other harnesses are preserved.
|
||||
|
||||
The adapter reuses its authenticated transport and correct operator directory.
|
||||
Reads are capped at 1 MiB; V2 requests one assistant, descending, and native V1
|
||||
requests eight recent messages. A backend ignoring page bounds is rejected.
|
||||
Successful and empty results are cached for 30 seconds, invalidated by an
|
||||
upstream update timestamp or an accepted model switch. The cache is bounded to
|
||||
128 entries. All optional metadata reads share a two-second list budget; failure
|
||||
is logged and leaves the chat visible with its model unknown. Pipeline and
|
||||
foreign histories are never fetched for this enrichment.
|
||||
|
||||
Validation: go build ./..., go vet ./..., and go test ./... pass. Focused race
|
||||
checks cover V1/V2 payloads, current-model preference, cache refresh/expiry,
|
||||
switch invalidation, empty chats, oversized/malformed responses, operator-only
|
||||
filtering, and metadata failures. Live checks read only detail/reply metadata;
|
||||
no prompt, chat mutation, shared-service restart or server deployment occurred.
|
||||
|
||||
Server deployment remains pending. internal/server/AGENTS.md reserves deployed
|
||||
server changes to the operator; tests used local fixtures only. No Flutter or
|
||||
web build is required for this server-only fix.
|
||||
@@ -42,11 +42,13 @@ type Chat struct {
|
||||
// operation and status on the shared connection, so two calls must not
|
||||
// overlap; the permission poller runs alongside the read loop's
|
||||
// prompt/interrupt/reply calls.
|
||||
mu sync.Mutex
|
||||
usageMu sync.Mutex
|
||||
usageModels map[string]usageModelCache
|
||||
modeMu sync.Mutex
|
||||
agents map[string]string
|
||||
mu sync.Mutex
|
||||
usageMu sync.Mutex
|
||||
usageModels map[string]usageModelCache
|
||||
modeMu sync.Mutex
|
||||
agents map[string]string
|
||||
modelMu sync.Mutex
|
||||
sessionModels map[string]sessionModelCache
|
||||
}
|
||||
|
||||
// NewChat brokers chat for one owned OpenCode endpoint. The connection must be
|
||||
@@ -234,6 +236,9 @@ func (c *Chat) SwitchModel(ctx context.Context, id, provider, model string) erro
|
||||
}
|
||||
return err
|
||||
}
|
||||
c.modelMu.Lock()
|
||||
delete(c.sessionModels, id)
|
||||
c.modelMu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type sessionModelCache struct {
|
||||
updated int64
|
||||
at time.Time
|
||||
provider, model string
|
||||
}
|
||||
|
||||
// ResolveSessionModel fills a missing list model from authoritative detail or
|
||||
// recent reply metadata. The caller filters operator sessions first: never
|
||||
// expand every pipeline/stage session into individual history requests.
|
||||
func (c *Chat) ResolveSessionModel(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
if session.Model != "" {
|
||||
return session, nil
|
||||
}
|
||||
if !ValidSessionID(session.ID) {
|
||||
return session, fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
c.modelMu.Lock()
|
||||
defer c.modelMu.Unlock()
|
||||
if cached, ok := c.sessionModels[session.ID]; ok && cached.updated == session.UpdatedAt && time.Since(cached.at) < 30*time.Second {
|
||||
session.Model, session.ModelProvider = cached.model, cached.provider
|
||||
return session, nil
|
||||
}
|
||||
// Detail may carry a selected model omitted by a compact list projection.
|
||||
prefix := "/api/session/"
|
||||
if c.conn.Backend == "v1" {
|
||||
prefix = "/session/"
|
||||
}
|
||||
data, err := c.usageRead(ctx, prefix+session.ID, session.Directory, 1<<20)
|
||||
if err != nil {
|
||||
return session, err
|
||||
}
|
||||
record, err := chatSessionRecord(data)
|
||||
if err != nil || record.ID != session.ID {
|
||||
return session, fmt.Errorf("invalid OpenCode model session")
|
||||
}
|
||||
model, provider := record.Model.ID, record.Model.ProviderID
|
||||
if model == "" {
|
||||
model, provider, err = c.latestSessionModel(ctx, prefix, session)
|
||||
if err != nil {
|
||||
return session, err
|
||||
}
|
||||
}
|
||||
if c.sessionModels == nil || len(c.sessionModels) >= 128 {
|
||||
c.sessionModels = make(map[string]sessionModelCache)
|
||||
}
|
||||
c.sessionModels[session.ID] = sessionModelCache{session.UpdatedAt, time.Now(), provider, model}
|
||||
session.Model, session.ModelProvider = model, provider
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Both request and response are bounded. Never download a full transcript or
|
||||
// CLI export just to label a row, including when an old endpoint ignores limit.
|
||||
func (c *Chat) latestSessionModel(ctx context.Context, prefix string, session protocol.ChatSession) (string, string, error) {
|
||||
limit, query := 1, "?type=assistant&order=desc&limit=1"
|
||||
if c.conn.Backend == "v1" {
|
||||
limit, query = 8, "?limit=8"
|
||||
}
|
||||
data, err := c.usageRead(ctx, prefix+session.ID+"/message"+query, session.Directory, 1<<20)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
var rows []struct {
|
||||
Type string `json:"type"`
|
||||
Model nativeChatModel `json:"model"`
|
||||
Time struct {
|
||||
Created int64 `json:"created"`
|
||||
} `json:"time"`
|
||||
Info struct {
|
||||
Role string `json:"role"`
|
||||
Model string `json:"modelID"`
|
||||
Provider string `json:"providerID"`
|
||||
Time struct {
|
||||
Created int64 `json:"created"`
|
||||
} `json:"time"`
|
||||
} `json:"info"`
|
||||
}
|
||||
var envelope struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if json.Unmarshal(data, &envelope) == nil && envelope.Data != nil {
|
||||
data = envelope.Data
|
||||
}
|
||||
if err := json.Unmarshal(data, &rows); err != nil || len(rows) > limit {
|
||||
return "", "", fmt.Errorf("invalid bounded OpenCode model history")
|
||||
}
|
||||
var model, provider string
|
||||
var newest int64 = -1
|
||||
for _, row := range rows {
|
||||
role, id, source, created := row.Type, row.Model.ID, row.Model.ProviderID, row.Time.Created
|
||||
if row.Info.Role != "" {
|
||||
role, id, source, created = row.Info.Role, row.Info.Model, row.Info.Provider, row.Info.Time.Created
|
||||
}
|
||||
if role == "assistant" && strings.TrimSpace(id) != "" && created >= newest {
|
||||
model, provider, newest = id, source, created
|
||||
}
|
||||
}
|
||||
return model, provider, nil
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestResolveSessionModel(t *testing.T) {
|
||||
for _, backend := range []string{"v1", "v2"} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
var reads atomic.Int32
|
||||
prefix := "/api/session/ses_operator"
|
||||
if backend == "v1" {
|
||||
prefix = "/session/ses_operator"
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
reads.Add(1)
|
||||
if r.URL.Query().Get("directory") != "/operator" {
|
||||
t.Errorf("wrong directory: %s", r.URL)
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case prefix:
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
case prefix + "/message":
|
||||
if backend == "v1" {
|
||||
if r.URL.Query().Get("limit") != "8" {
|
||||
t.Errorf("unbounded request: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `[{"info":{"role":"assistant","modelID":"old","time":{"created":1}}},{"info":{"role":"assistant","modelID":"deepseek-v4.1-flash","providerID":"opencode-go","time":{"created":2}}},{"info":{"role":"user","modelID":"ignored","time":{"created":3}}}]`)
|
||||
} else {
|
||||
if r.URL.Query().Get("limit") != "1" || r.URL.Query().Get("order") != "desc" || r.URL.Query().Get("type") != "assistant" {
|
||||
t.Errorf("unbounded request: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"time":{"created":2}}]}`)
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected read: %s", r.URL)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: backend, URL: srv.URL, Directory: "/wrong"})
|
||||
row := protocol.ChatSession{ID: "ses_operator", Directory: "/operator", UpdatedAt: 10}
|
||||
for range 2 {
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "deepseek-v4.1-flash" || got.ModelProvider != "opencode-go" {
|
||||
t.Fatalf("resolved: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
if reads.Load() != 2 {
|
||||
t.Fatalf("cache missed: %d reads", reads.Load())
|
||||
}
|
||||
row.UpdatedAt++
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reads.Load() != 4 {
|
||||
t.Fatalf("changed session was cached: %d reads", reads.Load())
|
||||
}
|
||||
cached := chat.sessionModels[row.ID]
|
||||
cached.at = time.Now().Add(-time.Minute)
|
||||
chat.sessionModels[row.ID] = cached
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reads.Load() != 6 {
|
||||
t.Fatalf("expired model was cached: %d reads", reads.Load())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelPrefersSelectedModel(t *testing.T) {
|
||||
var reads atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
reads.Add(1)
|
||||
if r.URL.Path != "/api/session/ses_operator" {
|
||||
t.Errorf("unnecessary history read: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator","model":{"id":"selected","providerID":"provider"}}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
row := protocol.ChatSession{ID: "ses_operator"}
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "selected" || got.ModelProvider != "provider" {
|
||||
t.Fatalf("selected: %+v %v", got, err)
|
||||
}
|
||||
row.Model = "already-listed"
|
||||
got, err = chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "already-listed" || reads.Load() != 1 {
|
||||
t.Fatalf("listed: %+v %v reads=%d", got, err, reads.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelRejectsUnboundedOrInvalidMetadata(t *testing.T) {
|
||||
for _, body := range []string{`{"data":[{},{}]}`, strings.Repeat(" ", 1<<20) + `[]`, `garbage`} {
|
||||
t.Run(fmt.Sprint(len(body)), func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.HasSuffix(r.URL.Path, "/message") {
|
||||
fmt.Fprint(w, body)
|
||||
} else {
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
got, err := chat.ResolveSessionModel(context.Background(), protocol.ChatSession{ID: "ses_operator"})
|
||||
if err == nil || got.Model != "" {
|
||||
t.Fatalf("unverified model: %+v %v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelDoesNotInventModelForEmptyChat(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.HasSuffix(r.URL.Path, "/message") {
|
||||
fmt.Fprint(w, `{"data":[]}`)
|
||||
} else {
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_empty"}}`)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
got, err := chat.ResolveSessionModel(context.Background(), protocol.ChatSession{ID: "ses_empty"})
|
||||
if err != nil || got.Model != "" || got.ModelProvider != "" {
|
||||
t.Fatalf("empty session: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSwitchModelInvalidatesListModelCache(t *testing.T) {
|
||||
var switched atomic.Bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_operator/model" {
|
||||
switched.Store(true)
|
||||
fmt.Fprint(w, `{}`)
|
||||
return
|
||||
}
|
||||
model := "original"
|
||||
if switched.Load() {
|
||||
model = "selected"
|
||||
}
|
||||
fmt.Fprintf(w, `{"data":{"id":"ses_operator","model":{"id":%q,"providerID":"provider"}}}`, model)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
row := protocol.ChatSession{ID: "ses_operator"}
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SwitchModel(context.Background(), row.ID, "provider", "selected"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "selected" {
|
||||
t.Fatalf("model switch kept stale row: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type chatSessionModelResolver interface {
|
||||
ResolveSessionModel(context.Context, protocol.ChatSession) (protocol.ChatSession, error)
|
||||
}
|
||||
|
||||
func (m *chatManager) ResolveSessionModel(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
if session.Model != "" || (session.Backend != "" && session.Backend != "opencode") {
|
||||
return session, nil
|
||||
}
|
||||
if resolver, ok := m.opencode.(chatSessionModelResolver); ok {
|
||||
return resolver.ResolveSessionModel(ctx, session)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestChatListResolvesOnlyListedOperatorModels(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var detailReads, historyReads int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/session":
|
||||
fmt.Fprint(w, `{"data":[{"id":"ses_operator","time":{"updated":12}},{"id":"ses_pipeline"},{"id":"ses_foreign"}]}`)
|
||||
case "/api/session/ses_operator":
|
||||
detailReads++
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
case "/api/session/ses_operator/message":
|
||||
historyReads++
|
||||
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"}}]}`)
|
||||
default:
|
||||
t.Errorf("read unlisted history: %s", r.URL)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
broker := NewChatManager(ctx, opencode.NewChat(&opencode.Connection{Backend: "v2", URL: srv.URL}), store)
|
||||
for range 2 {
|
||||
var reply protocol.Message
|
||||
handleChatSessions(ctx, protocol.Message{ID: "list"}, broker, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 {
|
||||
t.Fatalf("list: %+v", reply)
|
||||
}
|
||||
row := reply.ChatSessions[0]
|
||||
if row.ID != "ses_operator" || row.Model != "deepseek-v4.1-flash" || row.ModelProvider != "opencode-go" {
|
||||
t.Fatalf("model: %+v", row)
|
||||
}
|
||||
}
|
||||
if detailReads != 1 || historyReads != 1 {
|
||||
t.Fatalf("metadata was not cached: detail=%d history=%d", detailReads, historyReads)
|
||||
}
|
||||
}
|
||||
|
||||
type unavailableSessionModel struct{ *fakeChat }
|
||||
|
||||
func (b unavailableSessionModel) ResolveSessionModel(_ context.Context, row protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
return row, errors.New("metadata unavailable")
|
||||
}
|
||||
|
||||
func TestChatListSurvivesUnavailableModelMetadata(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := scriptedChat()
|
||||
fake.sessions = []protocol.ChatSession{{ID: "ses_operator"}}
|
||||
var reply protocol.Message
|
||||
handleChatSessions(ctx, protocol.Message{ID: "list"}, unavailableSessionModel{fake}, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 || reply.ChatSessions[0].Model != "" {
|
||||
t.Fatalf("unavailable metadata hid or invented a model: %+v", reply)
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -61,6 +63,10 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
present[session.ID] = session
|
||||
}
|
||||
}
|
||||
// All optional model lookups share one budget, so a slow metadata endpoint
|
||||
// cannot add one full timeout per row to the ordinary list request.
|
||||
modelCtx, cancelModels := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancelModels()
|
||||
for _, operator := range operatorSessions {
|
||||
session, ok := present[operator.ID]
|
||||
if !ok {
|
||||
@@ -72,6 +78,15 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
session.Directory = operator.Directory
|
||||
session.Tag = operator.Tag
|
||||
session.Backend = operator.Backend
|
||||
if resolver, ok := broker.(chatSessionModelResolver); ok && session.Model == "" && modelCtx.Err() == nil {
|
||||
resolved, err := resolver.ResolveSessionModel(modelCtx, session)
|
||||
if err != nil {
|
||||
// Metadata failure must not hide an otherwise usable chat.
|
||||
log.Printf("chat model metadata %s: %v", session.ID, err)
|
||||
} else {
|
||||
session = resolved
|
||||
}
|
||||
}
|
||||
session.CreatedAt = operator.CreatedAt
|
||||
session.UpdatedAt = operator.UpdatedAt
|
||||
// The closed state comes from the registry, so every client sees the same
|
||||
|
||||
Reference in New Issue
Block a user