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:
2026-11-27 00:43:33 +00:00
parent fd5d4b783c
commit 4eb9b2e934
5 changed files with 136 additions and 0 deletions
@@ -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)
}
}