Preserve manager skill bootstrap work
This commit is contained in:
@@ -25,6 +25,8 @@ type chatBroker interface {
|
||||
Sessions(ctx context.Context) ([]protocol.ChatSession, error)
|
||||
// Create creates one OpenCode session in the given directory and returns it.
|
||||
Create(ctx context.Context, title, directory string) (protocol.ChatSession, error)
|
||||
// ActivateSkill explicitly attaches a registered OpenCode skill before a chat can receive its first prompt.
|
||||
ActivateSkill(ctx context.Context, sessionID, skill string) error
|
||||
// Session returns the endpoint's own record of one existing session so an
|
||||
// adopted session carries the endpoint's directory and title; found is false
|
||||
// when the endpoint does not list sessionID.
|
||||
@@ -40,6 +42,9 @@ type chatBroker interface {
|
||||
// ownership (SessionOwner/run_session_owners) that settles a run: chat_sessions
|
||||
// lists operator sessions only and never consults stage ownership.
|
||||
type operatorRegistry interface {
|
||||
BeginChatCreate(ctx context.Context, requestID, title, directory string) (pipeline.ChatCreateBootstrap, error)
|
||||
SetChatCreateSession(ctx context.Context, requestID, sessionID string) error
|
||||
AdvanceChatCreate(ctx context.Context, requestID, state string) error
|
||||
RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error
|
||||
TouchOperatorSession(ctx context.Context, sessionID string) error
|
||||
OperatorSessions(ctx context.Context) ([]pipeline.OperatorSession, error)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
@@ -74,6 +75,14 @@ func (c *channelChat) Messages(context.Context, string, int) ([]protocol.ChatMes
|
||||
|
||||
func (c *channelChat) Sessions(context.Context) ([]protocol.ChatSession, error) { return nil, nil }
|
||||
|
||||
func (c *channelChat) BeginChatCreate(context.Context, string, string, string) (pipeline.ChatCreateBootstrap, error) {
|
||||
return pipeline.ChatCreateBootstrap{State: "created"}, nil
|
||||
}
|
||||
func (c *channelChat) SetChatCreateSession(context.Context, string, string) error { return nil }
|
||||
func (c *channelChat) AdvanceChatCreate(context.Context, string, string) error { return nil }
|
||||
|
||||
func (c *channelChat) ActivateSkill(context.Context, string, string) error { return nil }
|
||||
|
||||
func (c *channelChat) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
|
||||
return protocol.ChatSession{ID: "ses_created", Title: title, Directory: directory}, nil
|
||||
}
|
||||
|
||||
@@ -37,6 +37,10 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
_ = write(response)
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(request.ID) == "" {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_create requires a non-empty request id for retry-safe creation"})
|
||||
return
|
||||
}
|
||||
operatorSessions, err := registry.OperatorSessions(ctx)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
@@ -114,15 +118,54 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
session, err := broker.Create(ctx, title, resolved)
|
||||
bootstrap, err := registry.BeginChatCreate(ctx, request.ID, title, resolved)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
|
||||
session := protocol.ChatSession{ID: bootstrap.SessionID, Title: title, Directory: resolved}
|
||||
if session.ID == "" {
|
||||
session, err = broker.Create(ctx, title, resolved)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if err := registry.SetChatCreateSession(ctx, request.ID, session.ID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but durable bootstrap state could not be saved: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
if bootstrap.State == "created" {
|
||||
// Reserve the activation before calling the external service. If the
|
||||
// process dies after the call but before the success checkpoint, a retry
|
||||
// must report the ambiguous operation rather than append the skill twice.
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "activating"); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " could not reserve manager bootstrap: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if err := broker.ActivateSkill(ctx, session.ID, "pipelines-manager"); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but manager bootstrap failed while activating pipelines-manager: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "activated"); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " activated but durable bootstrap state could not be saved: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
if bootstrap.State == "activating" {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " has an unfinished manager bootstrap; activation will not be repeated automatically"})
|
||||
return
|
||||
}
|
||||
if bootstrap.State != "recorded" {
|
||||
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "recorded"); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was recorded but durable bootstrap state could not be saved: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
session.Directory = resolved
|
||||
session.Tag = protocol.ChatSessionTagOperator
|
||||
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -71,6 +72,10 @@ type fakeChat struct {
|
||||
createDirectory string
|
||||
created protocol.ChatSession
|
||||
createErr error
|
||||
activateCalls int
|
||||
activateSessionID string
|
||||
activateSkill string
|
||||
activateErr error
|
||||
sessionByIDCalls int
|
||||
sessionByID string
|
||||
sessionRecord protocol.ChatSession
|
||||
@@ -78,6 +83,12 @@ type fakeChat struct {
|
||||
sessionByIDErr error
|
||||
}
|
||||
|
||||
func (f *fakeChat) BeginChatCreate(context.Context, string, string, string) (pipeline.ChatCreateBootstrap, error) {
|
||||
return pipeline.ChatCreateBootstrap{State: "created"}, nil
|
||||
}
|
||||
func (f *fakeChat) SetChatCreateSession(context.Context, string, string) error { return nil }
|
||||
func (f *fakeChat) AdvanceChatCreate(context.Context, string, string) error { return nil }
|
||||
|
||||
func (f *fakeChat) Prompt(context.Context, string, string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -196,6 +207,14 @@ func (f *fakeChat) sessionQuery() int {
|
||||
// Create records the title and directory the handler forwarded and returns the
|
||||
// seeded created session. A blank created id defaults to "ses_created" so a test
|
||||
// only asserts what it cares about.
|
||||
func (f *fakeChat) ActivateSkill(_ context.Context, sessionID, skill string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.activateCalls++
|
||||
f.activateSessionID, f.activateSkill = sessionID, skill
|
||||
return f.activateErr
|
||||
}
|
||||
|
||||
func (f *fakeChat) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -1226,6 +1245,12 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
|
||||
if calls, title, directory := broker.createQuery(); calls != 1 || title != "demo" || directory != dir {
|
||||
t.Fatalf("broker.Create = %d/%q/%q, want 1/demo/%q", calls, title, directory, dir)
|
||||
}
|
||||
broker.mu.Lock()
|
||||
activateCalls, activateSession, activateSkill := broker.activateCalls, broker.activateSessionID, broker.activateSkill
|
||||
broker.mu.Unlock()
|
||||
if activateCalls != 1 || activateSession != "ses_new" || activateSkill != "pipelines-manager" {
|
||||
t.Fatalf("manager bootstrap = %d/%q/%q", activateCalls, activateSession, activateSkill)
|
||||
}
|
||||
recorded, err := store.OperatorSessions(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -1235,6 +1260,42 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreateReportsManagerBootstrapFailure(t *testing.T) {
|
||||
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_new"}, activateErr: errors.New("skill not found")}
|
||||
store := newOperatorStore(t)
|
||||
request := protocol.Message{ID: "create", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{}}
|
||||
var response protocol.Message
|
||||
handleChatCreate(context.Background(), request, broker, store, func(message protocol.Message) error {
|
||||
response = message
|
||||
return nil
|
||||
})
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "manager bootstrap failed") || !strings.Contains(response.Error, "skill not found") {
|
||||
t.Fatalf("response = %#v", response)
|
||||
}
|
||||
if _, err := store.OperatorSessions(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreateRetryReusesDurableBootstrap(t *testing.T) {
|
||||
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_once"}}
|
||||
store := newOperatorStore(t)
|
||||
request := protocol.Message{ID: "create-once", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo"}}
|
||||
for i := 0; i < 2; i++ {
|
||||
var response protocol.Message
|
||||
handleChatCreate(context.Background(), request, broker, store, func(message protocol.Message) error { response = message; return nil })
|
||||
if response.Type != protocol.ChatCreate || response.ChatSession == nil || response.ChatSession.ID != "ses_once" {
|
||||
t.Fatalf("retry %d response = %#v", i, response)
|
||||
}
|
||||
}
|
||||
broker.mu.Lock()
|
||||
creates, activations := broker.createCalls, broker.activateCalls
|
||||
broker.mu.Unlock()
|
||||
if creates != 1 || activations != 1 {
|
||||
t.Fatalf("retry repeated external work: creates=%d activations=%d", creates, activations)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatCreateDirectoryResolution pins directory defaulting and rejection: an
|
||||
// omitted/empty/"~"/"~/..." directory resolves to the operator's home (never the
|
||||
// server's working directory), an explicit existing directory is recorded
|
||||
|
||||
Reference in New Issue
Block a user