Claude chats: pass the chat title as --name so terminal resume lists them by name

Spawns pass the session title with --name (Claude Code records it as the
transcript's custom-title). A rename tells the harness the new title for its
next spawn, and the first routing of a session after a restart hands it the
registry title.
This commit is contained in:
2026-11-03 12:31:31 +00:00
parent d34185b56c
commit a12a7b3d66
8 changed files with 279 additions and 27 deletions
+1 -1
View File
@@ -208,7 +208,7 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
handleChatColor(ctx, request, registry, hub.changes(), clientID, write)
return true
case protocol.ChatRename:
handleChatRename(ctx, request, registry, hub.changes(), clientID, write)
handleChatRename(ctx, request, broker, registry, hub.changes(), clientID, write)
return true
case protocol.ChatExport:
handleChatExport(ctx, request, broker, registry, write)
+31 -2
View File
@@ -273,13 +273,22 @@ func (m *chatManager) brokerFor(ctx context.Context, sessionID string) (chatBrok
if adapter == nil {
return nil, errors.New("chat backend " + backend + " is not available")
}
if hinter, ok := adapter.(chatDirectoryHinter); ok && m.registry != nil {
hinter, hintsDirectory := adapter.(chatDirectoryHinter)
titler, takesTitle := adapter.(chatTitleSetter)
if (hintsDirectory || takesTitle) && m.registry != nil {
m.mu.Lock()
done := m.hinted[sessionID]
m.mu.Unlock()
if !done {
if row, found, err := m.registry.OperatorSession(ctx, sessionID); err == nil && found {
hinter.HintDirectory(ctx, sessionID, row.Directory)
if hintsDirectory {
hinter.HintDirectory(ctx, sessionID, row.Directory)
}
// The registry holds the operator's title, renames included, so
// a session rebuilt after a restart keeps its name.
if takesTitle && row.Title != "" {
titler.SetTitle(ctx, sessionID, row.Title)
}
m.mu.Lock()
if m.hinted == nil {
m.hinted = map[string]bool{}
@@ -292,6 +301,26 @@ func (m *chatManager) brokerFor(ctx context.Context, sessionID string) (chatBrok
return adapter, nil
}
// chatTitleSetter is a harness that names its own sessions from the operator's
// chat title (Claude Code passes it as --name on the next spawn). SetTitle
// records the title in memory only and does no I/O.
type chatTitleSetter interface {
SetTitle(ctx context.Context, sessionID, title string)
}
// SetTitle hands a renamed chat's title to its harness when the harness keeps
// one. The registry stays the source of truth; this only refreshes the name the
// harness shows outside the app.
func (m *chatManager) SetTitle(ctx context.Context, sessionID, title string) {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return
}
if titler, ok := adapter.(chatTitleSetter); ok {
titler.SetTitle(ctx, sessionID, title)
}
}
// chatDirectoryHinter is a harness that needs a session's recorded directory
// to find it after a restart (Claude Code keeps a transcript in the
// directory's own CLAUDE_CONFIG_DIR). HintDirectory is cheap after the first
+6 -1
View File
@@ -18,7 +18,7 @@ const chatTitleLimit = 200
// browser and device lists the same name. The OpenCode session, its transcript
// and its generated title are untouched, and a session that is not an operator
// chat is refused before anything is written.
func handleChatRename(ctx context.Context, request protocol.Message, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
func handleChatRename(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
@@ -48,5 +48,10 @@ func handleChatRename(ctx context.Context, request protocol.Message, registry op
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// A harness that names its own sessions (Claude Code's --name) takes the
// new title for its next start; the registry row above stays authoritative.
if titler, ok := broker.(chatTitleSetter); ok {
titler.SetTitle(ctx, rename.SessionID, title)
}
echoOperatorSession(ctx, request, rename.SessionID, protocol.ChatRename, registry, changes, clientID, write)
}
+85
View File
@@ -0,0 +1,85 @@
package server
import (
"context"
"path/filepath"
"sync"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// titledChat records the titles the server hands to a harness that names its
// own sessions (Claude Code's --name).
type titledChat struct {
*fakeChat
titleMu sync.Mutex
titles []string
}
func (c *titledChat) SetTitle(_ context.Context, _, title string) {
c.titleMu.Lock()
c.titles = append(c.titles, title)
c.titleMu.Unlock()
}
func (c *titledChat) lastTitle() string {
c.titleMu.Lock()
defer c.titleMu.Unlock()
if len(c.titles) == 0 {
return ""
}
return c.titles[len(c.titles)-1]
}
func TestChatRenameReachesTheHarnessAndARestartLearnsTheRegistryTitle(t *testing.T) {
ctx := context.Background()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db"))
if err != nil {
t.Fatal(err)
}
id := "01900000-0000-7000-8000-000000000003"
if err := store.RecordOperatorSession(ctx, id, "/fixture", "Original", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := store.SetOperatorSessionBackend(ctx, id, "claude"); err != nil {
t.Fatal(err)
}
// First routing after a restart hands the registry title to the harness.
fake := &titledChat{fakeChat: scriptedChat()}
manager := &chatManager{claude: fake, registry: store, cache: map[string]string{}}
if _, err := manager.brokerFor(ctx, id); err != nil {
t.Fatal(err)
}
if got := fake.lastTitle(); got != "Original" {
t.Fatalf("routed session title = %q, want the registry title", got)
}
var reply protocol.Message
capture := func(msg protocol.Message) error { reply = msg; return nil }
handleChatRename(ctx, protocol.Message{ID: "rename", ChatRename: &protocol.ChatRenameRequest{SessionID: id, Title: " Renamed "}}, manager, store, nil, "client", capture)
if reply.Type != protocol.ChatRename || reply.ChatSession == nil || reply.ChatSession.Title != "Renamed" {
t.Fatalf("rename reply = %+v", reply)
}
if got := fake.lastTitle(); got != "Renamed" {
t.Fatalf("harness title after rename = %q", got)
}
// A restarted server learns the renamed title from the registry.
restarted := &titledChat{fakeChat: scriptedChat()}
manager = &chatManager{claude: restarted, registry: store, cache: map[string]string{}}
if _, err := manager.brokerFor(ctx, id); err != nil {
t.Fatal(err)
}
if got := restarted.lastTitle(); got != "Renamed" {
t.Fatalf("title after restart = %q", got)
}
// A refused rename never reaches the harness.
handleChatRename(ctx, protocol.Message{ID: "blank", ChatRename: &protocol.ChatRenameRequest{SessionID: id, Title: " "}}, manager, store, nil, "client", capture)
if reply.Type != protocol.Error || restarted.lastTitle() != "Renamed" {
t.Fatalf("blank rename = %+v, harness title %q", reply, restarted.lastTitle())
}
}