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
View File
@@ -37,6 +37,7 @@ func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.Ch
return protocol.ChatSession{}, err
}
title = strings.TrimSpace(title)
c.SetTitle(ctx, id, title)
s := c.sessionFor(id, absolute)
s.mu.Lock()
s.title = title
+27
View File
@@ -102,6 +102,9 @@ type Chat struct {
mu sync.Mutex
sessions map[string]*session
closed bool
// titles holds the operator's chat titles by session id (see SetTitle), so a
// session rebuilt after an idle release keeps its name.
titles map[string]string
// quotas is the latest rate_limit_event quota per account (see quota.go).
quotaMu sync.Mutex
@@ -177,10 +180,34 @@ func (c *Chat) sessionFor(id, directory string) *session {
streamClaimed: map[int]bool{},
lastUsed: time.Now(),
}
s.title = c.titles[id]
c.sessions[id] = s
return s
}
// SetTitle records the chat's current title. The server calls it on a rename
// and when it first routes a session after a restart. A running CLI keeps the
// name it started with; the next spawn passes the new one as --name. The server
// never writes into Claude Code's transcript itself.
func (c *Chat) SetTitle(_ context.Context, id, title string) {
title = strings.TrimSpace(title)
if id == "" || title == "" {
return
}
c.mu.Lock()
if c.titles == nil {
c.titles = map[string]string{}
}
c.titles[id] = title
s := c.sessions[id]
c.mu.Unlock()
if s != nil {
s.mu.Lock()
s.title = title
s.mu.Unlock()
}
}
func (c *Chat) forget(id string) {
c.mu.Lock()
delete(c.sessions, id)
+87
View File
@@ -0,0 +1,87 @@
package claude
import (
"context"
"os"
"slices"
"testing"
)
func TestCLIArgsNameTheSessionWithItsTitle(t *testing.T) {
chat := &Chat{sessions: map[string]*session{}}
s := chat.sessionFor("id-1", "/fixture")
args, _ := s.cliArgs(false)
if slices.Contains(args, "--name") {
t.Fatalf("an untitled session must not pass --name: %q", args)
}
s.title = " Fix the\nlogin page "
args, _ = s.cliArgs(false)
if got := argValue(args, "--name"); got != "Fix the login page" {
t.Fatalf("--name = %q, want the title on one line: %q", got, args)
}
if argValue(args, "--session-id") != "id-1" {
t.Fatalf("a new session must pin its id: %q", args)
}
args, _ = s.cliArgs(true)
if argValue(args, "--resume") != "id-1" || argValue(args, "--name") != "Fix the login page" {
t.Fatalf("a resumed session must keep its id and name: %q", args)
}
}
func TestSetTitleReachesExistingAndRebuiltSessions(t *testing.T) {
ctx := context.Background()
chat := &Chat{sessions: map[string]*session{}}
// A title recorded before the session is rebuilt (a server restart) is
// applied when sessionFor creates it.
chat.SetTitle(ctx, "id-1", "From the registry")
if got := chat.sessionFor("id-1", "/fixture").title; got != "From the registry" {
t.Fatalf("rebuilt session title = %q", got)
}
// A rename updates a live session, and survives an idle release.
chat.SetTitle(ctx, "id-1", "Renamed")
if got := chat.session("id-1").title; got != "Renamed" {
t.Fatalf("live session title = %q", got)
}
chat.forget("id-1")
if got := chat.sessionFor("id-1", "/fixture").title; got != "Renamed" {
t.Fatalf("title after release = %q", got)
}
// A blank title never erases the name.
chat.SetTitle(ctx, "id-1", " ")
if got := chat.session("id-1").title; got != "Renamed" {
t.Fatalf("blank rename changed the title to %q", got)
}
}
func TestSpawnPassesTheTitleAndARenameReachesTheNextSpawn(t *testing.T) {
ctx := context.Background()
chat := testChat(t)
session := newCreatedChat(t, chat)
frames := subscribe(t, chat, session.ID)
if err := chat.Prompt(ctx, session.ID, "hello"); err != nil {
t.Fatal(err)
}
waitStepEnded(t, frames)
logPath := os.Getenv("FAKE_CLAUDE_LOG")
entries := readArgs(t, logPath)
if got := argValue(entries[len(entries)-1].Args, "--name"); got != "Fixture" {
t.Fatalf("first spawn --name = %q: %+v", got, entries)
}
chat.SetTitle(ctx, session.ID, "Renamed chat")
chat.session(session.ID).closeProcess()
if err := chat.Prompt(ctx, session.ID, "hello again"); err != nil {
t.Fatal(err)
}
waitStepEnded(t, frames)
entries = readArgs(t, logPath)
last := entries[len(entries)-1].Args
if argValue(last, "--resume") != session.ID || argValue(last, "--name") != "Renamed chat" {
t.Fatalf("respawn after rename = %q", last)
}
}
+41 -23
View File
@@ -55,6 +55,46 @@ type controlReply struct {
err string
}
// cliArgs builds the CLI arguments for one spawn and returns the permission
// mode it applied. resume continues an existing transcript; otherwise the CLI
// pins the id recorded at create time. A known chat title is passed as --name,
// which Claude Code records as the session's display name, so `claude --resume`
// in a terminal lists the chat by its title.
func (s *session) cliArgs(resume bool) ([]string, string) {
args := []string{
"-p",
"--input-format", "stream-json",
"--output-format", "stream-json",
"--verbose",
"--include-partial-messages",
"--permission-prompt-tool=stdio",
"--permission-prompts", "host",
}
if resume {
args = append(args, "--resume", s.id)
} else {
args = append(args, "--session-id", s.id)
}
s.mu.Lock()
model, mode, title := s.model, s.mode, s.title
s.mu.Unlock()
if model != "" {
args = append(args, "--model", model)
}
if mode != "" && mode != defaultMode {
args = append(args, "--permission-mode", mode)
}
if name := sessionDisplayName(title); name != "" {
args = append(args, "--name", name)
}
return args, mode
}
// sessionDisplayName folds a chat title onto one line for the CLI's session name.
func sessionDisplayName(title string) string {
return strings.Join(strings.Fields(title), " ")
}
// spawn starts the CLI in the session directory. A session whose transcript
// already exists resumes it; a new one pins the id we recorded at create time.
func (s *session) spawn(ctx context.Context) (*process, error) {
@@ -67,29 +107,7 @@ func (s *session) spawn(ctx context.Context) (*process, error) {
if info, err := os.Stat(s.directory); err != nil || !info.IsDir() {
return nil, fmt.Errorf("Claude session directory is not available: %s", s.directory)
}
args := []string{
"-p",
"--input-format", "stream-json",
"--output-format", "stream-json",
"--verbose",
"--include-partial-messages",
"--permission-prompt-tool=stdio",
"--permission-prompts", "host",
}
if findTranscriptFile(s.id) != "" {
args = append(args, "--resume", s.id)
} else {
args = append(args, "--session-id", s.id)
}
if s.model != "" {
args = append(args, "--model", s.model)
}
s.mu.Lock()
mode := s.mode
s.mu.Unlock()
if mode != "" && mode != defaultMode {
args = append(args, "--permission-mode", mode)
}
args, mode := s.cliArgs(findTranscriptFile(s.id) != "")
cmd := exec.CommandContext(s.chat.ctx, s.chat.binary, args...)
cmd.Dir = s.directory
env, notices := s.chat.processEnv(ctx, s.directory)