merge: land chat-adopt-claude
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// adoptingChat finds the session on another harness, as the chat manager does
|
||||
// for a Claude Code or Codex session id.
|
||||
type adoptingChat struct {
|
||||
*fakeChat
|
||||
backend string
|
||||
asked []string
|
||||
}
|
||||
|
||||
func (a *adoptingChat) AdoptSession(_ context.Context, id, backend string) (protocol.ChatSession, string, bool, error) {
|
||||
a.asked = append(a.asked, backend)
|
||||
return protocol.ChatSession{ID: id, Directory: "/work/claude"}, a.backend, true, nil
|
||||
}
|
||||
|
||||
func TestChatAdoptRecordsTheOwningHarness(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newOperatorStore(t)
|
||||
broker := &adoptingChat{fakeChat: scriptedChat(), backend: "claude"}
|
||||
_, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
|
||||
const id = "27ca4504-744a-472b-938b-ab497c02aba7"
|
||||
request := protocol.ChatAdoptRequest{SessionID: id, Title: "Flutter port"}
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &request}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatAdopt || response.ChatSession == nil {
|
||||
t.Fatalf("chat_adopt response = %+v", response)
|
||||
}
|
||||
if got := response.ChatSession; got.Backend != "claude" || got.Title != "Flutter port" || got.Directory != "/work/claude" {
|
||||
t.Fatalf("adopted session = %+v", got)
|
||||
}
|
||||
if len(broker.asked) != 1 || broker.asked[0] != "" {
|
||||
t.Fatalf("harness asked = %q, want one search across harnesses", broker.asked)
|
||||
}
|
||||
row, found, err := store.OperatorSession(ctx, id)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("registry row = %v %v", found, err)
|
||||
}
|
||||
if row.Backend != "claude" || row.Title != "Flutter port" || row.Directory != "/work/claude" {
|
||||
t.Fatalf("registry row = %+v", row)
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,45 @@ func (m *chatManager) Backends() []string {
|
||||
return backends
|
||||
}
|
||||
|
||||
// AdoptSession finds an existing session on [backend], or on the first
|
||||
// harness that knows it when [backend] is blank (OpenCode, Claude Code, Codex),
|
||||
// and remembers which harness owns it.
|
||||
func (m *chatManager) AdoptSession(ctx context.Context, sessionID, backend string) (protocol.ChatSession, string, bool, error) {
|
||||
candidates := []string{backend}
|
||||
if backend == "" {
|
||||
candidates = []string{"opencode", claude.Backend, codex.Backend}
|
||||
}
|
||||
var firstErr error
|
||||
for _, name := range candidates {
|
||||
adapter := m.adapter(name)
|
||||
if adapter == nil {
|
||||
if backend != "" {
|
||||
return protocol.ChatSession{}, "", false, errors.New("chat backend " + backend + " is not available")
|
||||
}
|
||||
continue
|
||||
}
|
||||
session, found, err := adapter.Session(ctx, sessionID)
|
||||
if err != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
if name == "" {
|
||||
name = "opencode"
|
||||
}
|
||||
session.Backend = name
|
||||
m.mu.Lock()
|
||||
m.cache[sessionID] = name
|
||||
m.mu.Unlock()
|
||||
return session, name, true, nil
|
||||
}
|
||||
return protocol.ChatSession{}, "", false, firstErr
|
||||
}
|
||||
|
||||
func (m *chatManager) adapter(backend string) chatBroker {
|
||||
switch backend {
|
||||
case "", "opencode":
|
||||
|
||||
@@ -273,6 +273,11 @@ func chatCreateBootstrapError(request protocol.Message, sessionID, state string,
|
||||
// an endpoint record with an empty directory is refused rather than substituting
|
||||
// the server's working directory or the operator home. The response echoes the
|
||||
// adopted session and its registry row like chat_create.
|
||||
// chatAdopter finds a session on whichever harness owns it.
|
||||
type chatAdopter interface {
|
||||
AdoptSession(ctx context.Context, sessionID, backend string) (protocol.ChatSession, string, bool, error)
|
||||
}
|
||||
|
||||
func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
|
||||
if broker == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
||||
@@ -288,18 +293,36 @@ func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatB
|
||||
}
|
||||
id := strings.TrimSpace(request.ChatAdopt.SessionID)
|
||||
if !validChatSessionID(id) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
||||
return
|
||||
}
|
||||
session, found, err := broker.Session(ctx, id)
|
||||
requested := strings.TrimSpace(request.ChatAdopt.Backend)
|
||||
backend := "opencode"
|
||||
var session protocol.ChatSession
|
||||
var found bool
|
||||
var err error
|
||||
if adopter, ok := broker.(chatAdopter); ok {
|
||||
session, backend, found, err = adopter.AdoptSession(ctx, id, requested)
|
||||
} else if requested != "" && requested != "opencode" {
|
||||
err = errors.New("chat backend " + requested + " is not available")
|
||||
} else {
|
||||
session, found, err = broker.Session(ctx, id)
|
||||
}
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " was not found on the OpenCode endpoint"})
|
||||
where := "any chat harness"
|
||||
if requested != "" {
|
||||
where = requested
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " was not found on " + where})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(session.Title) == "" {
|
||||
session.Title = strings.TrimSpace(request.ChatAdopt.Title)
|
||||
}
|
||||
directory := strings.TrimSpace(session.Directory)
|
||||
if directory == "" {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " has no directory on the OpenCode endpoint"})
|
||||
@@ -309,6 +332,13 @@ func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatB
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was found but could not be recorded: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if backend != "" && backend != "opencode" {
|
||||
if err := registry.SetOperatorSessionBackend(ctx, session.ID, backend); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was recorded but its harness could not be: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
session.Backend = backend
|
||||
session.Directory = directory
|
||||
session.Tag = protocol.ChatSessionTagOperator
|
||||
response := protocol.Message{Type: protocol.ChatAdopt, ID: request.ID, ChatSession: &session}
|
||||
|
||||
Reference in New Issue
Block a user