Guard chat bootstrap transitions
Use conditional durable phase transitions and affected-row checks so only the request that reserves each phase can create or activate a manager chat session. Record duplicate ownership conflicts without repeating external work, and cover the reservation and restart state in pipeline and server tests. Checked packaged templates and authoring skill references: the existing task commit already packages the dedicated manager skill and installer inventory; this transition-race fix requires no further template or reference update. Shared documentation remains untouched; the task note is docs/notes/manager-skill-bootstrap.md.
This commit is contained in:
@@ -76,6 +76,9 @@ 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
|
||||
@@ -90,8 +93,8 @@ type fakeChat struct {
|
||||
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) 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()
|
||||
@@ -268,10 +271,19 @@ func (f *fakeChat) ActivateSkill(_ context.Context, sessionID, skill string) err
|
||||
|
||||
func (f *fakeChat) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
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
|
||||
}
|
||||
@@ -1388,6 +1400,91 @@ func TestChatCreateRetryReusesDurableBootstrap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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