chat: separate create operation identity
Use an explicit chat_create operation_id as the durable bootstrap key while retaining envelope IDs for reply correlation. Legacy requests receive fresh server-scoped identities, so independent cli-1/android-1 requests do not collide; clients that retry retain one operation_id. Update Android creation state and repository calls to preserve an operation identity across retries, and cover durable reconnect retries, duplicate barriers, changed input, legacy correlation reuse, and protocol serialization. Checked packaged templates: none describe chat_create. Checked packaged authoring-skill references: the existing description is stale, but shared references and README must not be edited by an item; docs/notes/bootstrap-create-identity.md records the required docs-fold update and rollout compatibility.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
@@ -72,6 +74,8 @@ type operatorRegistry interface {
|
||||
ReopenOperatorSession(ctx context.Context, sessionID string) error
|
||||
}
|
||||
|
||||
var chatTokenSequence atomic.Uint64
|
||||
|
||||
// chatClientToken returns an unguessable per-connection identity. Its only role
|
||||
// is to key a connection's chat-room subscriptions and to exclude the sender from
|
||||
// its own fan-out; it carries no ownership. A fresh token per connection means a
|
||||
@@ -79,7 +83,7 @@ type operatorRegistry interface {
|
||||
func chatClientToken() string {
|
||||
var b [8]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "conn-unknown"
|
||||
return "conn-fallback-" + strconv.FormatUint(chatTokenSequence.Add(1), 10)
|
||||
}
|
||||
return "conn-" + hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
@@ -100,15 +100,20 @@ 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 := ""
|
||||
operationID := ""
|
||||
if request.ChatCreate != nil {
|
||||
title = strings.TrimSpace(request.ChatCreate.Title)
|
||||
directory = strings.TrimSpace(request.ChatCreate.Directory)
|
||||
operationID = strings.TrimSpace(request.ChatCreate.OperationID)
|
||||
}
|
||||
// Legacy clients did not carry a durable create identity. Give each received
|
||||
// request its own server-generated scope so their connection-local envelope
|
||||
// IDs (for example cli-1) cannot collide globally. Such requests cannot be
|
||||
// retried exactly once across reconnects because they supplied no key.
|
||||
if operationID == "" {
|
||||
operationID = chatClientToken()
|
||||
}
|
||||
if title == "" {
|
||||
title = "New chat"
|
||||
@@ -118,7 +123,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
bootstrap, err := registry.BeginChatCreate(ctx, request.ID, title, resolved)
|
||||
bootstrap, err := registry.BeginChatCreate(ctx, operationID, title, resolved)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
@@ -131,7 +136,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
}
|
||||
// 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 {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "created", "creating"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
return
|
||||
}
|
||||
@@ -140,11 +145,11 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if err := registry.SetChatCreateSession(ctx, request.ID, session.ID); err != nil {
|
||||
if err := registry.SetChatCreateSession(ctx, operationID, 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 {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "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
|
||||
}
|
||||
@@ -154,7 +159,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
// 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 {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "created", "activating"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
return
|
||||
}
|
||||
@@ -162,7 +167,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = 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 {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "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
|
||||
}
|
||||
@@ -173,7 +178,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
return
|
||||
}
|
||||
if bootstrap.State == "activated" {
|
||||
if err := registry.AdvanceChatCreate(ctx, request.ID, "activated", "recording"); err != nil {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "activated", "recording"); err != nil {
|
||||
chatCreateConflictError(request, err, write)
|
||||
return
|
||||
}
|
||||
@@ -181,7 +186,7 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
|
||||
_ = 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 {
|
||||
if err := registry.AdvanceChatCreate(ctx, operationID, "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
|
||||
}
|
||||
|
||||
@@ -1373,7 +1373,7 @@ func TestChatCreateReportsManagerBootstrapFailure(t *testing.T) {
|
||||
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"}}
|
||||
request := protocol.Message{ID: "create-once", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo", OperationID: "operation-once"}}
|
||||
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 })
|
||||
@@ -1389,6 +1389,51 @@ func TestChatCreateRetryReusesDurableBootstrap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatCreateOperationSurvivesLostResponseAndStoreReopen verifies that the
|
||||
// durable create identity, rather than the connection-local reply ID, resumes a
|
||||
// completed create after a client lost its reply and the server reopened its DB.
|
||||
func TestChatCreateOperationSurvivesLostResponseAndStoreReopen(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "operator.db")
|
||||
store, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_once"}}
|
||||
handleChatCreate(ctx, protocol.Message{
|
||||
ID: "cli-1", Type: protocol.ChatCreate,
|
||||
ChatCreate: &protocol.ChatCreateRequest{Title: "demo", OperationID: "operation-once"},
|
||||
}, broker, store, func(protocol.Message) error {
|
||||
return nil // Simulate a reply lost after all durable checkpoints completed.
|
||||
})
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reopened, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
|
||||
var response protocol.Message
|
||||
handleChatCreate(ctx, protocol.Message{
|
||||
ID: "cli-2", Type: protocol.ChatCreate,
|
||||
ChatCreate: &protocol.ChatCreateRequest{Title: "demo", OperationID: "operation-once"},
|
||||
}, broker, reopened, func(message protocol.Message) error {
|
||||
response = message
|
||||
return nil
|
||||
})
|
||||
if response.Type != protocol.ChatCreate || response.ID != "cli-2" || response.ChatSession == nil || response.ChatSession.ID != "ses_once" {
|
||||
t.Fatalf("reopened retry response = %#v", response)
|
||||
}
|
||||
broker.mu.Lock()
|
||||
creates, activations := broker.createCalls, broker.activateCalls
|
||||
broker.mu.Unlock()
|
||||
if creates != 1 || activations != 1 {
|
||||
t.Fatalf("reopened 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.
|
||||
@@ -1400,7 +1445,7 @@ func TestChatCreateDuplicateRequestReservesExternalWork(t *testing.T) {
|
||||
createGate: createGate,
|
||||
createStarted: make(chan struct{}),
|
||||
}
|
||||
request := protocol.Message{ID: "same-request", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo"}}
|
||||
request := protocol.Message{ID: "same-request", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "demo", OperationID: "operation-once"}}
|
||||
responses := make(chan protocol.Message, 2)
|
||||
run := func() {
|
||||
handleChatCreate(context.Background(), request, broker, store, func(message protocol.Message) error {
|
||||
@@ -1440,21 +1485,48 @@ func TestChatCreateDuplicateRequestReservesExternalWork(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreateRequiresRequestID(t *testing.T) {
|
||||
func TestChatCreateLegacyRequestWithoutOperationIDUsesFreshScope(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)
|
||||
store := newOperatorStore(t)
|
||||
for _, request := range []protocol.Message{
|
||||
{ID: "cli-1", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "first"}},
|
||||
{ID: "cli-1", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "second"}},
|
||||
} {
|
||||
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 {
|
||||
t.Fatalf("legacy 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)
|
||||
if creates != 2 {
|
||||
t.Fatalf("legacy creates = %d, want 2", creates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreateOperationIDRejectsChangedInputBeforeExternalWork(t *testing.T) {
|
||||
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_new"}}
|
||||
store := newOperatorStore(t)
|
||||
first := protocol.Message{ID: "cli-1", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "first", OperationID: "operation"}}
|
||||
handleChatCreate(context.Background(), first, broker, store, func(protocol.Message) error { return nil })
|
||||
var response protocol.Message
|
||||
handleChatCreate(context.Background(), protocol.Message{ID: "cli-1", Type: protocol.ChatCreate, ChatCreate: &protocol.ChatCreateRequest{Title: "second", OperationID: "operation"}}, broker, store, func(message protocol.Message) error {
|
||||
response = message
|
||||
return nil
|
||||
})
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "operation id was reused with different input") {
|
||||
t.Fatalf("changed operation response = %+v", response)
|
||||
}
|
||||
broker.mu.Lock()
|
||||
creates, activations := broker.createCalls, broker.activateCalls
|
||||
broker.mu.Unlock()
|
||||
if creates != 1 || activations != 1 {
|
||||
t.Fatalf("changed operation repeated external work: creates=%d activations=%d", creates, activations)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user