Preserve manager skill bootstrap work

This commit is contained in:
2026-10-18 15:03:22 +01:00
parent 88595ebd55
commit ab9181eee9
13 changed files with 362 additions and 4 deletions
+61
View File
@@ -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