feat(chat): persist safe Codex next-turn model selection
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type selectedModelChat struct {
|
||||
*fakeChat
|
||||
model, nativeID string
|
||||
overrides int
|
||||
}
|
||||
|
||||
func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, nativeID, model string) error {
|
||||
c.model, c.nativeID = model, nativeID
|
||||
c.overrides++
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "chat.db")
|
||||
store, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id := "01900000-0000-7000-8000-000000000001"
|
||||
if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetOperatorSessionBackend(ctx, id, "codex"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := &selectedModelChat{fakeChat: scriptedChat()}
|
||||
fake.models = []protocol.ChatModel{{ID: "fixture-model", Provider: "fixture-provider"}}
|
||||
fake.sessionRecord = protocol.ChatSession{ID: id, Backend: "codex", Model: "fixture-current", ModelProvider: "fixture-provider"}
|
||||
fake.sessionFound = true
|
||||
fake.sessions = []protocol.ChatSession{fake.sessionRecord}
|
||||
manager := &chatManager{codex: fake, registry: store, cache: map[string]string{}}
|
||||
var reply protocol.Message
|
||||
handleChatModelSwitch(ctx, protocol.Message{ID: "choose", ModelRequest: &protocol.ChatModelRequest{SessionID: id, Provider: "fixture-provider", Model: "fixture-model"}}, manager, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.ChatModelSelect || reply.ModelRequest.Selection != "next_turn" {
|
||||
t.Fatalf("selection reply = %+v", reply)
|
||||
}
|
||||
if fake.overrides != 0 {
|
||||
t.Fatal("selection started a turn")
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err = pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
// A new manager has neither backend nor model cached in memory.
|
||||
manager = &chatManager{codex: fake, registry: store, cache: map[string]string{}}
|
||||
session, found, err := manager.Session(ctx, id)
|
||||
if err != nil || !found || session.Model != "fixture-model" || session.ModelSelection != "next_turn" {
|
||||
t.Fatalf("restarted session = %+v, %v", session, err)
|
||||
}
|
||||
list, err := manager.Sessions(ctx)
|
||||
if err != nil || len(list) != 1 || list[0].Model != "fixture-model" || list[0].ModelSelection != "next_turn" {
|
||||
t.Fatalf("restarted list = %+v, %v", list, err)
|
||||
}
|
||||
if err := manager.PromptWithMessageID(ctx, id, "hello", "msg_native"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.model != "fixture-model" || fake.nativeID != "msg_native" {
|
||||
t.Fatalf("override = %s/%s", fake.model, fake.nativeID)
|
||||
}
|
||||
if err := manager.Prompt(ctx, id, "again"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.overrides != 2 {
|
||||
t.Fatal("plain prompt bypassed saved choice")
|
||||
}
|
||||
if err := manager.PromptWithMessageIDFiles(ctx, id, "no attachments", "msg_empty", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.overrides != 3 || fake.nativeID != "msg_empty" {
|
||||
t.Fatal("empty attachments bypassed saved choice")
|
||||
}
|
||||
fake.sessionRecord.ModelProvider = "different-provider"
|
||||
if err := manager.Prompt(ctx, id, "must not send"); err == nil {
|
||||
t.Fatal("saved model sent to a different provider")
|
||||
}
|
||||
if fake.overrides != 3 {
|
||||
t.Fatal("mismatched provider started a turn")
|
||||
}
|
||||
// A failed durable save must never acknowledge the next-turn selection.
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reply = protocol.Message{}
|
||||
handleChatModelSwitch(ctx, protocol.Message{ID: "failed-save", ModelRequest: &protocol.ChatModelRequest{SessionID: id, Provider: "fixture-provider", Model: "fixture-model"}}, manager, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.Error || reply.ModelRequest != nil {
|
||||
t.Fatalf("failed save acknowledged: %+v", reply)
|
||||
}
|
||||
}
|
||||
@@ -119,11 +119,7 @@ func (m *chatManager) Create(ctx context.Context, title, directory string) (prot
|
||||
}
|
||||
|
||||
func (m *chatManager) Prompt(ctx context.Context, sessionID, text string) error {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return adapter.Prompt(ctx, sessionID, text)
|
||||
return m.PromptWithMessageID(ctx, sessionID, text, "")
|
||||
}
|
||||
|
||||
func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, messageID string) error {
|
||||
@@ -131,6 +127,28 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
provider, model, err := m.chatModelSelection(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if model != "" {
|
||||
session, found, err := adapter.Session(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found || session.ModelProvider != provider {
|
||||
return errors.New("saved model provider no longer matches the Codex thread; select a model again")
|
||||
}
|
||||
withModel, ok := adapter.(interface {
|
||||
PromptWithMessageIDModel(context.Context, string, string, string, string) error
|
||||
})
|
||||
if !ok {
|
||||
return errors.New("this Codex adapter cannot apply the saved next-turn model")
|
||||
}
|
||||
return withModel.PromptWithMessageIDModel(ctx, sessionID, text, messageID, model)
|
||||
}
|
||||
}
|
||||
if withID, ok := adapter.(chatPromptWithMessageID); ok {
|
||||
return withID.PromptWithMessageID(ctx, sessionID, text, messageID)
|
||||
}
|
||||
@@ -140,6 +158,9 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
// PromptWithMessageIDFiles forwards a prompt with the private local files the
|
||||
// server wrote for browser attachments. Never silently discard files.
|
||||
func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error {
|
||||
if len(files) == 0 {
|
||||
return m.PromptWithMessageID(ctx, sessionID, text, messageID)
|
||||
}
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -147,10 +168,7 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
|
||||
if withFiles, ok := adapter.(chatPromptWithFiles); ok {
|
||||
return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files)
|
||||
}
|
||||
if len(files) > 0 {
|
||||
return errChatAttachmentsUnsupported
|
||||
}
|
||||
return m.PromptWithMessageID(ctx, sessionID, text, messageID)
|
||||
return errChatAttachmentsUnsupported
|
||||
}
|
||||
|
||||
var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments")
|
||||
@@ -204,9 +222,53 @@ func (m *chatManager) SwitchModel(ctx context.Context, sessionID, provider, mode
|
||||
if !ok {
|
||||
return errors.New("this chat harness does not support changing models")
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
registry, ok := m.registry.(chatModelSelectionRegistry)
|
||||
if !ok {
|
||||
return errors.New("durable chat model selection is not configured")
|
||||
}
|
||||
if err := modeler.SwitchModel(ctx, sessionID, provider, model); err != nil {
|
||||
return err
|
||||
}
|
||||
return registry.SetChatModelSelection(ctx, sessionID, provider, model)
|
||||
}
|
||||
return modeler.SwitchModel(ctx, sessionID, provider, model)
|
||||
}
|
||||
|
||||
type chatModelSelectionRegistry interface {
|
||||
SetChatModelSelection(context.Context, string, string, string) error
|
||||
ChatModelSelection(context.Context, string) (string, string, error)
|
||||
}
|
||||
|
||||
func (m *chatManager) ModelSelectionMode(ctx context.Context, id string) string {
|
||||
if m.backendFor(ctx, id) == codex.Backend {
|
||||
return "next_turn"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *chatManager) chatModelSelection(ctx context.Context, id string) (string, string, error) {
|
||||
if registry, ok := m.registry.(chatModelSelectionRegistry); ok {
|
||||
return registry.ChatModelSelection(ctx, id)
|
||||
}
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
func (m *chatManager) withModelSelection(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
if session.Backend != codex.Backend {
|
||||
return session, nil
|
||||
}
|
||||
provider, model, err := m.chatModelSelection(ctx, session.ID)
|
||||
if err != nil {
|
||||
return session, err
|
||||
}
|
||||
if model != "" {
|
||||
session.ModelProvider, session.Model = provider, model
|
||||
session.ModelSelection = "next_turn"
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (m *chatManager) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
@@ -270,6 +332,13 @@ func (m *chatManager) Sessions(ctx context.Context) ([]protocol.ChatSession, err
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range sessions {
|
||||
var err error
|
||||
sessions[i], err = m.withModelSelection(ctx, sessions[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
@@ -278,7 +347,12 @@ func (m *chatManager) Session(ctx context.Context, sessionID string) (protocol.C
|
||||
if err != nil {
|
||||
return protocol.ChatSession{}, false, err
|
||||
}
|
||||
return adapter.Session(ctx, sessionID)
|
||||
session, found, err := adapter.Session(ctx, sessionID)
|
||||
if err != nil || !found {
|
||||
return session, found, err
|
||||
}
|
||||
session, err = m.withModelSelection(ctx, session)
|
||||
return session, found, err
|
||||
}
|
||||
|
||||
func (m *chatManager) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) {
|
||||
|
||||
@@ -166,7 +166,14 @@ func handleChatModelSwitch(ctx context.Context, request protocol.Message, broker
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatModelSelect, ID: request.ID, ModelRequest: body})
|
||||
reply := *body
|
||||
reply.Selection = ""
|
||||
if reporter, ok := broker.(interface {
|
||||
ModelSelectionMode(context.Context, string) string
|
||||
}); ok {
|
||||
reply.Selection = reporter.ModelSelectionMode(ctx, body.SessionID)
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatModelSelect, ID: request.ID, ModelRequest: &reply})
|
||||
}
|
||||
|
||||
// handleChatSubscribe attaches one connection to a session's shared stream so it
|
||||
|
||||
Reference in New Issue
Block a user