Open chats: the server moves a restarted chat (/new) to its new session
The server-stored open-chat set kept a /new'd chat's old session unless the restarting device's own write landed, so another device (the phone) came back to the old conversation. chat_new_session now replaces the session in the stored set in place (position and floating kept) and pushes the new revision to every connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -177,3 +177,47 @@ func (s *Store) RemoveChatOpenWorkspaceSessions(ctx context.Context, scope strin
|
||||
}
|
||||
return next, true, nil
|
||||
}
|
||||
|
||||
// ReplaceChatOpenWorkspaceSession moves an open chat to its new session id
|
||||
// (the composer's /new) in place, keeping its position and floating flag. If
|
||||
// the new id is already open, the old entry is dropped instead. Nothing is
|
||||
// stored when the old id is not open.
|
||||
func (s *Store) ReplaceChatOpenWorkspaceSession(ctx context.Context, scope, oldID, newID string) (snapshot protocol.ChatOpenWorkspaceSnapshot, changed bool, err error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return snapshot, false, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
current, err := readOpenWorkspace(ctx, tx, scope)
|
||||
if err != nil || current.Revision == 0 || oldID == newID {
|
||||
return current, false, err
|
||||
}
|
||||
present := false
|
||||
for _, entry := range current.Entries {
|
||||
if entry.SessionID == newID {
|
||||
present = true
|
||||
}
|
||||
}
|
||||
next := make([]protocol.ChatOpenWorkspaceEntry, 0, len(current.Entries))
|
||||
for _, entry := range current.Entries {
|
||||
if entry.SessionID == oldID {
|
||||
changed = true
|
||||
if present {
|
||||
continue
|
||||
}
|
||||
entry.SessionID = newID
|
||||
}
|
||||
next = append(next, entry)
|
||||
}
|
||||
if !changed {
|
||||
return current, false, nil
|
||||
}
|
||||
snapshot = protocol.ChatOpenWorkspaceSnapshot{Revision: current.Revision + 1, Entries: next}
|
||||
if err := writeOpenWorkspace(ctx, tx, scope, snapshot.Revision, next); err != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, false, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, false, err
|
||||
}
|
||||
return snapshot, true, nil
|
||||
}
|
||||
|
||||
@@ -103,3 +103,27 @@ func TestChatOpenWorkspaceRejectsDuplicatesAndBounds(t *testing.T) {
|
||||
t.Fatal("oversized workspace accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatOpenWorkspaceReplacesARestartedChatInPlace(t *testing.T) {
|
||||
store := openWorkspaceStore(t, t.TempDir()+"/workspace.db")
|
||||
defer store.Close()
|
||||
ctx := context.Background()
|
||||
entries := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a"}, {SessionID: "ses_b", Floating: true}, {SessionID: "ses_c"}}
|
||||
if _, _, err := store.SaveChatOpenWorkspace(ctx, "default", entries, false, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
moved, changed, err := store.ReplaceChatOpenWorkspaceSession(ctx, "default", "ses_b", "ses_new")
|
||||
if err != nil || !changed || moved.Revision != 2 {
|
||||
t.Fatalf("replace = %+v changed=%v err=%v", moved, changed, err)
|
||||
}
|
||||
if got := moved.Entries; len(got) != 3 || got[1].SessionID != "ses_new" || !got[1].Floating || got[0].SessionID != "ses_a" || got[2].SessionID != "ses_c" {
|
||||
t.Fatalf("entries after replace = %+v", got)
|
||||
}
|
||||
if _, changed, _ := store.ReplaceChatOpenWorkspaceSession(ctx, "default", "ses_gone", "ses_x"); changed {
|
||||
t.Fatal("replacing a chat that is not open stored a revision")
|
||||
}
|
||||
merged, changed, err := store.ReplaceChatOpenWorkspaceSession(ctx, "default", "ses_a", "ses_c")
|
||||
if err != nil || !changed || len(merged.Entries) != 2 || merged.Entries[0].SessionID != "ses_new" || merged.Entries[1].SessionID != "ses_c" {
|
||||
t.Fatalf("replace onto an open chat = %+v changed=%v err=%v", merged, changed, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +108,7 @@ func handleChatNewSession(ctx context.Context, request protocol.Message, broker
|
||||
fail("session " + created.ID + " was started but the chat could not move to it: " + err.Error())
|
||||
return
|
||||
}
|
||||
replaceInChatWorkspace(ctx, registry, hub.changes(), oldID, created.ID)
|
||||
row, found, err := registry.OperatorSession(ctx, created.ID)
|
||||
if err != nil || !found {
|
||||
fail("session " + created.ID + " was recorded but could not be read back")
|
||||
|
||||
@@ -20,6 +20,7 @@ type chatOpenWorkspaceStore interface {
|
||||
ChatOpenWorkspace(context.Context, string) (protocol.ChatOpenWorkspaceSnapshot, error)
|
||||
SaveChatOpenWorkspace(ctx context.Context, scope string, entries []protocol.ChatOpenWorkspaceEntry, seed bool, baseRevision int64) (protocol.ChatOpenWorkspaceSnapshot, bool, error)
|
||||
RemoveChatOpenWorkspaceSessions(ctx context.Context, scope string, sessionIDs []string) (protocol.ChatOpenWorkspaceSnapshot, bool, error)
|
||||
ReplaceChatOpenWorkspaceSession(ctx context.Context, scope, oldID, newID string) (protocol.ChatOpenWorkspaceSnapshot, bool, error)
|
||||
}
|
||||
|
||||
func openWorkspaceStoreOf(registry operatorRegistry) chatOpenWorkspaceStore {
|
||||
@@ -101,6 +102,24 @@ func dropFromChatWorkspace(ctx context.Context, registry operatorRegistry, chang
|
||||
}
|
||||
}
|
||||
|
||||
// replaceInChatWorkspace moves a chat that /new restarted to its new session
|
||||
// in the open-chat set, so every device opens the new session (not the old
|
||||
// one) even if the restarting device's own write never lands.
|
||||
func replaceInChatWorkspace(ctx context.Context, registry operatorRegistry, changes *chatSessionChanges, oldID, newID string) {
|
||||
store := openWorkspaceStoreOf(registry)
|
||||
if store == nil {
|
||||
return
|
||||
}
|
||||
snapshot, changed, err := store.ReplaceChatOpenWorkspaceSession(ctx, chatWorkspaceScope, oldID, newID)
|
||||
if err != nil {
|
||||
log.Printf("move restarted chat %s to %s in open workspace: %v", oldID, newID, err)
|
||||
return
|
||||
}
|
||||
if changed {
|
||||
changes.broadcastWorkspace("", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
// validChatWorkspaceEntries checks a client's snapshot and keeps only its open
|
||||
// operator chats. Malformed or duplicate ids are refused; closed or unknown
|
||||
// chats are dropped, so a stale client can never restore them.
|
||||
|
||||
@@ -145,3 +145,51 @@ func TestChatOpenWorkspacePrunesOnConnectForEveryone(t *testing.T) {
|
||||
t.Fatalf("catch-up push = %v", workspaceIDs(push))
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatOpenWorkspaceFollowsARestartedChat pins that /new moves the open
|
||||
// chat to its new session on the server and pushes that to every connection,
|
||||
// so another device reopens the new session even if the restarting device
|
||||
// never writes its own workspace.
|
||||
func TestChatOpenWorkspaceFollowsARestartedChat(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
chat := scriptedChat()
|
||||
chat.created = protocol.ChatSession{ID: "ses_fresh", Title: "Feature"}
|
||||
chat.sessionFound = true
|
||||
chat.sessions = []protocol.ChatSession{{ID: "ses_old"}, {ID: "ses_b"}, {ID: "ses_fresh"}}
|
||||
ctx, _, conns := dialChatConns(t, Options{Chat: chat, OperatorRegistry: store}, 2)
|
||||
for _, id := range []string{"ses_old", "ses_b"} {
|
||||
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_old", Floating: true}))
|
||||
if got := readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 1 {
|
||||
t.Fatalf("first write reply = %+v", got)
|
||||
}
|
||||
readWorkspacePush(t, ctx, conns[1], 1)
|
||||
|
||||
writeChat(t, ctx, conns[0], chatNewSessionRequest("n1", "ses_old"))
|
||||
// The push is asynchronous, so it may reach the restarting connection
|
||||
// before or after its reply.
|
||||
var replied bool
|
||||
var pushed *protocol.ChatOpenWorkspaceSnapshot
|
||||
for !replied || pushed == nil {
|
||||
message := readMessage(t, ctx, conns[0])
|
||||
switch {
|
||||
case message.Type == protocol.ChatNewSession && message.ID == "n1":
|
||||
replied = true
|
||||
case message.Type == protocol.ChatOpenWorkspaceChanged && message.ChatOpenWorkspace != nil && message.ChatOpenWorkspace.Revision == 2:
|
||||
pushed = message.ChatOpenWorkspace
|
||||
}
|
||||
}
|
||||
if !sameIDs(workspaceIDs(pushed), "ses_b", "ses_fresh*") {
|
||||
t.Fatalf("restarting connection after /new = %v", workspaceIDs(pushed))
|
||||
}
|
||||
if push := readWorkspacePush(t, ctx, conns[1], 2); !sameIDs(workspaceIDs(push), "ses_b", "ses_fresh*") {
|
||||
t.Fatalf("other connection after /new = %v", workspaceIDs(push))
|
||||
}
|
||||
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatOpenWorkspace, ID: "r1"})
|
||||
if read := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; read.Revision != 2 || !sameIDs(workspaceIDs(read), "ses_b", "ses_fresh*") {
|
||||
t.Fatalf("stored workspace after /new = %+v", read)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user