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:
2026-10-18 16:50:12 +01:00
parent 134bd0611a
commit 5946f1abc8
14 changed files with 203 additions and 46 deletions
+84 -12
View File
@@ -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)
}
}