Chat stack: groups of open chats, synced with the open-chat set

A chat dropped onto the middle of another makes a named group (asked at
once, "Group N" by default); onto a group it joins, a group dropped onto
a chat takes it in, groups merge. The top level shows each group as a
small pile with its name and count; a tap opens it in place under a
header that leads back (also the scrim, Back and Escape). In a group,
dropping a chat on the header leaves the group; a group of one
dissolves. The close button ungroups a dropped group with Undo. Rename,
Ungroup, Close all (with Undo), Move to group and Remove from group are
in the group menu and as semantic actions.

Each open-chat entry carries its group name (protocol field "group",
omitted when ungrouped; older clients ignore it), stored and pushed by
the server like the order; names are trimmed, printable, at most 80
characters. Needs a server restart to keep groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 22:18:44 +00:00
parent 130c1e6708
commit efaa0d9fca
30 changed files with 2399 additions and 308 deletions
+22
View File
@@ -7,6 +7,8 @@ import (
"fmt"
"strings"
"time"
"unicode"
"unicode/utf8"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -35,11 +37,31 @@ func validateOpenWorkspace(entries []protocol.ChatOpenWorkspaceEntry) error {
if _, ok := seen[id]; ok {
return fmt.Errorf("duplicate open chat session id %q", id)
}
if !validOpenWorkspaceGroup(entry.Group) {
return fmt.Errorf("invalid open chat group name")
}
seen[id] = struct{}{}
}
return nil
}
// validOpenWorkspaceGroup accepts an empty name (no group) or a trimmed,
// printable one of at most ChatOpenWorkspaceGroupLimit characters.
func validOpenWorkspaceGroup(name string) bool {
if name == "" {
return true
}
if strings.TrimSpace(name) != name || !utf8.ValidString(name) || utf8.RuneCountInString(name) > protocol.ChatOpenWorkspaceGroupLimit {
return false
}
for _, r := range name {
if unicode.IsControl(r) {
return false
}
}
return true
}
func decodeOpenWorkspace(raw string) ([]protocol.ChatOpenWorkspaceEntry, error) {
var entries []protocol.ChatOpenWorkspaceEntry
if err := json.Unmarshal([]byte(raw), &entries); err != nil {
@@ -3,6 +3,7 @@ package pipeline
import (
"context"
"fmt"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
@@ -127,3 +128,46 @@ func TestChatOpenWorkspaceReplacesARestartedChatInPlace(t *testing.T) {
t.Fatalf("replace onto an open chat = %+v changed=%v err=%v", merged, changed, err)
}
}
// TestChatOpenWorkspaceKeepsGroups pins that chat groups (a name on each
// grouped entry) are stored, compared and restored with the entries, that a
// group-only change stores a new revision, that a restarted chat keeps its
// group, and that malformed names are refused.
func TestChatOpenWorkspaceKeepsGroups(t *testing.T) {
path := t.TempDir() + "/workspace.db"
ctx := context.Background()
store := openWorkspaceStore(t, path)
entries := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a", Group: "Work"}, {SessionID: "ses_b", Group: "Work"}, {SessionID: "ses_c"}}
snapshot, changed, err := store.SaveChatOpenWorkspace(ctx, "default", entries, false, 0)
if err != nil || !changed || snapshot.Entries[1].Group != "Work" {
t.Fatalf("save: %+v changed=%v err=%v", snapshot, changed, err)
}
renamed := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a", Group: "Personal"}, {SessionID: "ses_b", Group: "Personal"}, {SessionID: "ses_c"}}
snapshot, changed, err = store.SaveChatOpenWorkspace(ctx, "default", renamed, false, 1)
if err != nil || !changed || snapshot.Revision != 2 {
t.Fatalf("a group-only change was not stored: %+v changed=%v err=%v", snapshot, changed, err)
}
if _, changed, err = store.SaveChatOpenWorkspace(ctx, "default", renamed, false, 2); err != nil || changed {
t.Fatalf("an identical write stored a revision: changed=%v err=%v", changed, err)
}
moved, _, err := store.ReplaceChatOpenWorkspaceSession(ctx, "default", "ses_b", "ses_new")
if err != nil || moved.Entries[1].SessionID != "ses_new" || moved.Entries[1].Group != "Personal" {
t.Fatalf("restarted chat lost its group: %+v err=%v", moved, err)
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
store = openWorkspaceStore(t, path)
defer store.Close()
loaded, err := store.ChatOpenWorkspace(ctx, "default")
if err != nil || loaded.Entries[0].Group != "Personal" || loaded.Entries[2].Group != "" {
t.Fatalf("groups did not survive restart: %+v err=%v", loaded, err)
}
long := strings.Repeat("g", protocol.ChatOpenWorkspaceGroupLimit+1)
for _, name := range []string{" Work", "Work\n", long} {
bad := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a", Group: name}}
if _, _, err := store.SaveChatOpenWorkspace(ctx, "default", bad, false, loaded.Revision); err == nil {
t.Fatalf("group name %q was accepted", name)
}
}
}
+7
View File
@@ -5,8 +5,15 @@ package protocol
type ChatOpenWorkspaceEntry struct {
SessionID string `json:"session_id"`
Floating bool `json:"floating,omitempty"`
// Group names the chat group this chat belongs to in the phone's chat
// stack; entries sharing a name form one group, ordered as the entries
// are. Empty for an ungrouped chat. Older clients ignore it.
Group string `json:"group,omitempty"`
}
// ChatOpenWorkspaceGroupLimit bounds a group name, in characters.
const ChatOpenWorkspaceGroupLimit = 80
// ChatOpenWorkspaceRequest replaces the open-chat set. A chat_open_workspace
// without a request only reads the current snapshot.
type ChatOpenWorkspaceRequest struct {
+1 -1
View File
@@ -140,7 +140,7 @@ func validChatWorkspaceEntries(ctx context.Context, request protocol.ChatOpenWor
return nil, err
}
if open {
valid = append(valid, protocol.ChatOpenWorkspaceEntry{SessionID: id, Floating: entry.Floating})
valid = append(valid, protocol.ChatOpenWorkspaceEntry{SessionID: id, Floating: entry.Floating, Group: entry.Group})
}
}
return valid, nil
@@ -193,3 +193,30 @@ func TestChatOpenWorkspaceFollowsARestartedChat(t *testing.T) {
t.Fatalf("stored workspace after /new = %+v", read)
}
}
// TestChatOpenWorkspaceSyncsGroups pins that a chat's group travels with its
// entry: stored on a write, pushed to the other connection and read back.
func TestChatOpenWorkspaceSyncsGroups(t *testing.T) {
store := newOperatorStore(t)
ctx, _, conns := dialChatConns(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 2)
for _, id := range []string{"ses_a", "ses_b", "ses_c"} {
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_a", Group: "Work"},
protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b", Group: "Work"},
protocol.ChatOpenWorkspaceEntry{SessionID: "ses_c"}))
if reply := readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace).ChatOpenWorkspace; reply.Revision != 1 || reply.Entries[1].Group != "Work" {
t.Fatalf("grouped write reply = %+v", reply)
}
push := readWorkspacePush(t, ctx, conns[1], 1)
if push.Entries[0].Group != "Work" || push.Entries[1].Group != "Work" || push.Entries[2].Group != "" {
t.Fatalf("grouped push = %+v", push)
}
writeChat(t, ctx, conns[1], workspaceWrite("w2", 1, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_a", Group: " bad"}))
if failed := readUntil(t, ctx, conns[1], protocol.Error); failed.Error == "" {
t.Fatal("a malformed group name was accepted")
}
}