Routing's By session headed each session "Claude Code · folder · title",
and with no title only the folder and id ("路由这里的会话名只能看到目录名和
会话ID吗"). Now the head is the agent and the session's title on one
line (whitespace folded), with its folder after it in a quieter .dir; with
no title the folder stands in, and with neither the id. The tooltip keeps
the title, the folder and the id.
The title comes from what magpie already reads, no second reader:
sessions.Titles (the agent's own title, else its first prompt) and Codex's
name index, and where both find nothing, the first prompt the gateway
recorded for that session (sessions.GatewayTitler, the title the Sessions
page shows for it), looked up under the agent the page names and the one
the gateway records it under (usage.AgentOf). A Codex thread Codex never
named now goes to sessions.Titles as a codex: key, so its rollout's first
prompt names it. withGatewayTitles and codexReadableTitles in trace.go.
Nothing is read that the Sessions page doesn't show already, and nothing
leaves the local GUI.
Tests: TestSessionTitlesFallBackToFirstPrompt (new): an unnamed Codex
thread gets its first prompt, a named one keeps its name, an unknown id
stays out, a pi session known only to the gateway gets its first words.
routing-session-folder.test.cjs follows the new head: title then .dir,
the first prompt on one line, the folder said once, the id with neither,
nothing clipped (chromium and webkit, en zh zh-TW ja de, 1100 and 420px).
Run on mote via mote-run, at origin/main e7b0c398e plus this change:
- Whole Routing GUI set (routing-*, panel-routing*) + i18n-keys,
chromium and webkit: 2583 pass, 35 fail. Every red is a known one:
rule-remove (#3096), rule-drag (#3789), row-swap (#3799),
order-unknown (#4627), rules-line (#3593), many-keys, steady,
steps-cap and story-shrinks (#5088), and under the load of another
session's whole GUI suite (load ~200) routing-credits webkit ja 420
(#5161) and routing-flood webkit en (#2215). Run again: credits 20/20
and flood 12/12 here; order-unknown's webkit de 420 red the same at
e7b0c398e alone.
- routing-session-folder: 20/20 (both engines, 5 languages, 2 widths).
- go test ./internal/gui: TestSessionTitlesFallBackToFirstPrompt ok;
red only TestCoreScriptBudget (1534001 > 1533802, the same at
e7b0c398e, #5139: this change adds nothing to the core scripts) and
TestMainWindowCorners (the same at e7b0c398e, #3949).
- Rebased onto origin/main 1a67ca614 (its routing client.js changes
merged clean), at this commit: routing-session-folder 20/20,
routing-session-title 40/40, routing-kept-again 20/20,
usage-compact-counts 30/30, routing-live-calm 2/2, i18n-keys 4/4, and
go test ./internal/gui -run 'TestSession|TestCoreScriptBudget' ok
(the budget passes at this base). The whole Routing set was not run
again here. Then onto 0f1b40bbc (routing client.js and css again,
merged clean): routing-session-folder 20/20, routing-live-bytes
20/20, routing-live-calm 2/2, i18n-keys 4/4.
Broken by hand:
- withGatewayTitles a no-op: TestSessionTitlesFallBackToFirstPrompt red
("titles = map[]").
- client.js back to the old head: routing-session-folder red in every
engine, language and width.
Not tried: the real app with real Codex, Claude Code and gateway-only
sessions (fixtures only), the Linux WebKitGTK app, Windows.
125 lines
5.5 KiB
Go
125 lines
5.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The Routing page names a Claude Code session by the title in its own
|
|
// file, as the Sessions page does, not by its UUID (#1293). A session with
|
|
// no file here, or another agent's id, gets no name.
|
|
func TestSessionTitlesNameOtherAgents(t *testing.T) {
|
|
sandboxHome(t)
|
|
claude := t.TempDir()
|
|
t.Setenv("CLAUDE_CONFIG_DIR", claude)
|
|
t.Setenv("CODEX_HOME", t.TempDir())
|
|
t.Cleanup(sessions.Reset)
|
|
sessions.Reset()
|
|
const id = "578bd3d1-834b-43fb-b40c-ed9d17ad7731"
|
|
dir := filepath.Join(claude, "projects", "-work-app")
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lines := []string{
|
|
`{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"把路由页的会话名显示出来"},"uuid":"u1","timestamp":"2026-10-08T10:00:01.000Z","cwd":"/work/app","sessionId":"` + id + `"}`,
|
|
`{"parentUuid":"u1","isSidechain":false,"message":{"model":"claude-opus-5-5","id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"好"}],"usage":{"input_tokens":10,"output_tokens":5}},"requestId":"req_1","type":"assistant","uuid":"a1","timestamp":"2026-10-08T10:00:05.000Z","cwd":"/work/app","sessionId":"` + id + `"}`,
|
|
`{"type":"ai-title","aiTitle":"路由会话名","sessionId":"` + id + `"}`,
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, id+".jsonl"), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
traceRoutes(mux)
|
|
body := `{"ids":[],"routeIds":[],"sessions":["claude:` + id + `","claude:00000000-0000-0000-0000-000000000000","pi:` + id + `"]}`
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/gateway/session-titles", strings.NewReader(body)))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var out struct {
|
|
Titles map[string]string `json:"titles"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]string{"claude:" + id: "路由会话名"}
|
|
if len(out.Titles) != len(want) || out.Titles["claude:"+id] != want["claude:"+id] {
|
|
t.Fatalf("titles = %v, want %v (body %s)", out.Titles, want, w.Body)
|
|
}
|
|
}
|
|
|
|
// flandern1211_58540 on Discord: Routing headed a session by its folder
|
|
// and id ("路由这里的会话名只能看到目录名和会话ID吗"). A Codex thread Codex
|
|
// never named is headed by its first prompt, as the Sessions page heads
|
|
// it, and a session whose files aren't on this computer by the first words
|
|
// the gateway recorded of it. A named thread keeps its name, and a session
|
|
// with neither keeps no title.
|
|
func TestSessionTitlesFallBackToFirstPrompt(t *testing.T) {
|
|
sandboxHome(t)
|
|
t.Setenv("CLAUDE_CONFIG_DIR", t.TempDir())
|
|
codex := t.TempDir()
|
|
t.Setenv("CODEX_HOME", codex)
|
|
t.Cleanup(sessions.Reset)
|
|
sessions.Reset()
|
|
const unnamed, named = "019b0001-aaaa-7bbb-8ccc-000000000001", "019b0001-aaaa-7bbb-8ccc-000000000002"
|
|
day := filepath.Join(codex, "sessions", "2026", "11", "04")
|
|
if err := os.MkdirAll(day, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{unnamed, named} {
|
|
lines := []string{
|
|
`{"timestamp":"2026-11-04T10:00:00.000Z","type":"session_meta","payload":{"id":"` + id + `","timestamp":"2026-11-04T10:00:00.000Z","cwd":"/work/app","originator":"codex_cli_rs","cli_version":"0.99.0","source":"cli","model_provider":"openai"}}`,
|
|
`{"timestamp":"2026-11-04T10:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"把路由页的会话名\n显示出来"}]}}`,
|
|
`{"timestamp":"2026-11-04T10:00:01.100Z","type":"event_msg","payload":{"type":"user_message","message":"把路由页的会话名\n显示出来","images":[]}}`,
|
|
}
|
|
if err := os.WriteFile(filepath.Join(day, "rollout-2026-11-04T10-00-00-"+id+".jsonl"), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(codex, "session_index.jsonl"), []byte(`{"id":"`+named+`","thread_name":"路由会话名","updated_at":"2026-11-04T10:01:00Z"}`+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := sessions.SetGatewayRecording(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := sessions.SaveGatewayTurn(sessions.GatewayTurn{Agent: "pi", Session: "ses_remote", Time: time.Now().UTC(),
|
|
Input: []sessions.Part{{Role: "user", Kind: "text", Text: "Deploy the staging server"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
traceRoutes(mux)
|
|
ask := func(body string, out any) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/gateway/session-titles", strings.NewReader(body)))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var groups struct {
|
|
Names map[string]string `json:"names"`
|
|
}
|
|
ask(`{"ids":["`+unnamed+`","`+named+`","019b0001-aaaa-7bbb-8ccc-00000000dead"],"groups":true,"routeIds":[]}`, &groups)
|
|
if groups.Names[unnamed] == "" || !strings.HasPrefix(groups.Names[unnamed], "把路由页的会话名") || groups.Names[named] != "路由会话名" || len(groups.Names) != 2 {
|
|
t.Fatalf("Codex names = %q", groups.Names)
|
|
}
|
|
var others struct {
|
|
Titles map[string]string `json:"titles"`
|
|
}
|
|
ask(`{"ids":[],"routeIds":[],"sessions":["pi:ses_remote","pi:ses_none","claude:ses_remote"]}`, &others)
|
|
if others.Titles["pi:ses_remote"] != "Deploy the staging server" || len(others.Titles) != 1 {
|
|
t.Fatalf("titles = %q", others.Titles)
|
|
}
|
|
}
|