Merge branch 'main' into pill-run-decision
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.
|
||||
@@ -52,6 +54,9 @@ type chatShellReleaser 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, from, to 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, string) (protocol.C
|
||||
|
||||
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, 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
|
||||
}
|
||||
|
||||
@@ -100,6 +100,10 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
|
||||
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
|
||||
}
|
||||
title := ""
|
||||
directory := ""
|
||||
if request.ChatCreate != nil {
|
||||
@@ -114,13 +118,75 @@ 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 == "" {
|
||||
if bootstrap.State != "created" {
|
||||
chatCreateBootstrapError(request, session.ID, bootstrap.State, write)
|
||||
return
|
||||
}
|
||||
// The conditional transition is the durable reservation for the external
|
||||
// Create call. A duplicate request that lost it must not create an orphan.
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "created", "creating"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
return
|
||||
}
|
||||
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 err := registry.AdvanceChatCreate(ctx, request.ID, "creating", "created"); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but durable bootstrap state could not advance: " + err.Error()})
|
||||
return
|
||||
}
|
||||
bootstrap.State = "created"
|
||||
}
|
||||
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, "created", "activating"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
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, "activating", "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
|
||||
}
|
||||
bootstrap.State = "activated"
|
||||
}
|
||||
if bootstrap.State == "creating" || bootstrap.State == "activating" || bootstrap.State == "recording" {
|
||||
chatCreateBootstrapError(request, session.ID, bootstrap.State, write)
|
||||
return
|
||||
}
|
||||
if bootstrap.State == "activated" {
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "activated", "recording"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
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()})
|
||||
return
|
||||
}
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "recording", "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
|
||||
}
|
||||
} else if bootstrap.State != "recorded" {
|
||||
chatCreateBootstrapError(request, session.ID, bootstrap.State, write)
|
||||
return
|
||||
}
|
||||
session.Directory = resolved
|
||||
@@ -132,6 +198,14 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(response)
|
||||
}
|
||||
|
||||
func chatCreateConflictError(request protocol.Message, err error, write func(protocol.Message) error) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_create bootstrap ownership/state conflict: " + err.Error()})
|
||||
}
|
||||
|
||||
func chatCreateBootstrapError(request protocol.Message, sessionID, state string, write func(protocol.Message) error) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " has an unfinished manager bootstrap (" + state + "); it will not be repeated automatically"})
|
||||
}
|
||||
|
||||
// handleChatAdopt records a session that already exists on the endpoint in the
|
||||
// operator registry, using the endpoint's own directory and title verbatim, so
|
||||
// it appears in chat_sessions and can be continued. It refuses a nil broker, a
|
||||
|
||||
@@ -77,6 +77,13 @@ type fakeChat struct {
|
||||
createDirectory string
|
||||
created protocol.ChatSession
|
||||
createErr error
|
||||
createGate <-chan struct{}
|
||||
createStarted chan struct{}
|
||||
createOnce sync.Once
|
||||
activateCalls int
|
||||
activateSessionID string
|
||||
activateSkill string
|
||||
activateErr error
|
||||
sessionByIDCalls int
|
||||
sessionByID string
|
||||
sessionRecord protocol.ChatSession
|
||||
@@ -84,6 +91,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, string) error { return nil }
|
||||
|
||||
func (f *fakeChat) Prompt(context.Context, string, string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -250,12 +263,29 @@ 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) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
|
||||
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()
|
||||
f.createCalls++
|
||||
f.createTitle = title
|
||||
f.createDirectory = directory
|
||||
gate, started := f.createGate, f.createStarted
|
||||
f.mu.Unlock()
|
||||
if started != nil {
|
||||
f.createOnce.Do(func() { close(started) })
|
||||
}
|
||||
if gate != nil {
|
||||
<-gate
|
||||
}
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.createErr != nil {
|
||||
return protocol.ChatSession{}, f.createErr
|
||||
}
|
||||
@@ -1308,6 +1338,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)
|
||||
@@ -1317,6 +1353,127 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatCreateDuplicateRequestReservesExternalWork proves the conditional
|
||||
// durable transition, rather than timing or the race detector, elects exactly
|
||||
// one request to call the external Create and ActivateSkill operations.
|
||||
func TestChatCreateDuplicateRequestReservesExternalWork(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
createGate := make(chan struct{})
|
||||
broker := &fakeChat{
|
||||
created: protocol.ChatSession{ID: "ses_once"},
|
||||
createGate: createGate,
|
||||
createStarted: make(chan struct{}),
|
||||
}
|
||||
request := protocol.Message{ID: "same-request", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo"}}
|
||||
responses := make(chan protocol.Message, 2)
|
||||
run := func() {
|
||||
handleChatCreate(context.Background(), request, broker, store, func(message protocol.Message) error {
|
||||
responses <- message
|
||||
return nil
|
||||
})
|
||||
}
|
||||
go run()
|
||||
select {
|
||||
case <-broker.createStarted:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("first request did not reserve Create")
|
||||
}
|
||||
go run()
|
||||
select {
|
||||
case response := <-responses:
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "unfinished manager bootstrap") {
|
||||
t.Fatalf("losing request response = %+v", response)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("duplicate request did not finish at its durable reservation")
|
||||
}
|
||||
close(createGate)
|
||||
select {
|
||||
case response := <-responses:
|
||||
if response.Type != protocol.ChatCreate || response.ChatSession == nil || response.ChatSession.ID != "ses_once" {
|
||||
t.Fatalf("winning request response = %+v", response)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("winning request did not finish")
|
||||
}
|
||||
broker.mu.Lock()
|
||||
creates, activations := broker.createCalls, broker.activateCalls
|
||||
broker.mu.Unlock()
|
||||
if creates != 1 || activations != 1 {
|
||||
t.Fatalf("duplicate request repeated external work: creates=%d activations=%d", creates, activations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreateRequiresRequestID(t *testing.T) {
|
||||
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_new"}}
|
||||
var response protocol.Message
|
||||
handleChatCreate(context.Background(), protocol.Message{Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo"}}, broker, newOperatorStore(t), func(message protocol.Message) error {
|
||||
response = message
|
||||
return nil
|
||||
})
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "non-empty request id") {
|
||||
t.Fatalf("empty chat_create response = %+v", response)
|
||||
}
|
||||
broker.mu.Lock()
|
||||
creates := broker.createCalls
|
||||
broker.mu.Unlock()
|
||||
if creates != 0 {
|
||||
t.Fatalf("empty chat_create called broker.Create %d times", creates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSessionsAllowsEmptyRequestID(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(context.Background(), "ses_existing", "/work", "Existing", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broker := &fakeChat{sessions: []protocol.ChatSession{{ID: "ses_existing"}}}
|
||||
var response protocol.Message
|
||||
handleChatSessions(context.Background(), protocol.Message{Type: protocol.ChatSessions}, broker, nil, store, func(message protocol.Message) error {
|
||||
response = message
|
||||
return nil
|
||||
})
|
||||
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_existing" {
|
||||
t.Fatalf("empty-ID chat_sessions response = %+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
// 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