From f594b452e3b8fd57ab6f547d1aa7611d1b1e097f Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 13:45:02 +0800 Subject: [PATCH] sessions: a Claude fork after a compaction is a branch under its session, read from the start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit neo_o11 (Discord, after v0.1.1809): one conversation still showed as ~9 rows with the same title, and opening one showed "Session start" and a single turn. Cause, reproduced with real Claude Code 2.1.312 in a sandbox CLAUDE_CONFIG_DIR: the reporter's session was compacted, then forked several times (headless --fork-session per task, or /branch). Each fork starts at the compact boundary and summary, and copies the session's last reply under the same uuid. So every fork holds the original's Leaf, and ccCopies hid the original as a fork's copy, though it alone holds the conversation before the compaction. Each fork had a Leaf of its own and stayed a row, with the copied title. Opening a fork read its own file: the summary and one turn. Fix: - cc_copies.go: ccFamily returns copies and branches. A file is a copy of another only when that one holds its Leaf and its Root too, so a file with what came before the other's start is never hidden. The members of a family left visible pick a head (holds the other's Root, then named in the other's forkedFrom, then the earlier start, then the lower key); the rest are branches of it. - sessions.go, manage.go: a branch has role "branch" and parent_session = its head, in List, Find/get and ListAgent. - transcript.go: a branch's transcript is its head up to the fork point (the last line of the head holding one of the branch's uuids), then the branch's own lines. Search still reads its file. - stats.go: a branch counts only its own lines, so tokens are not counted twice. - GUI sessions plugin: the fold under a session names branches apart ("2 branches · 1 subagent"), in en/zh/zh-TW/ja/de. - Fixtures in testdata/claude-forks/tasks*.jsonl are trimmed from real 2.1.312 output: a session compacted once, two --fork-session task forks and a /branch copy with forkedFrom. - docs/subsystems/gateway-sessions.md: copies and branches. Run: - go test ./internal/sessions -run 'TestClaude|TestQoder|TestMergeExternal' on mote, at e15b2277c before the rebase: ok. Red on the old code (cc_copies/sessions/transcript/stats restored from HEAD~): 5 tests failed (the original hidden, only forks as rows; a branch's prompts starting at the summary). - Broken by hand, each red: dropping the Root rule (3 compacted-fork tests), ignoring forkedFrom (TestClaudeBranchNamesItsSession), turning branchTranscript off (3 tests), counting a branch whole in stats (90 tokens, not 80). - GOOS=linux (-tags nogui), windows and darwin go vet, at e15b2277c: ok. - Full go test ./... -timeout 45m at this head (71124a2d7 on c1660481c), on the Linux box (mote was at load 300 with six suites): every package ok, sessions 144s, gateway 987s, except: ., internal/cli and internal/gui didn't build (no gtk4 there), ok again with -tags nogui; internal/agent's TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir and TestCursorFolderFromTheEditorIsNotTheCLI, the first three red the same at origin/main c1660481c there, all four in #4033 (#3548, #3141). Nothing here touches internal/agent. - GUI on the Windows box, both engines, every language: sessions-subagents + i18n-keys 40/44, the 4 reds 5s nav-click and goto timeouts in WebKit; WebKit alone again 20/20. Every session*.test.cjs file (32): 381/399. Its six red files run again at origin/main c1660481c and here, one at a time: calendar-fill zh red on both (#1314), sessions-today WebKit zh red here only, the reads.n race of #1315; the rest green on both. Not tried: get()'s path for a session beyond the listing's 200 has no test of its own; the TUI and CLI lists show a branch as a row of its own, not nested; an uncompacted fork that went on has no real forkedFrom fixture (TestClaudeBranchNamesItsSession injects one). --- docs/subsystems/gateway-sessions.md | 34 ++- .../gui/assets/plugins/sessions/client.js | 14 +- internal/gui/assets/plugins/sessions/i18n.js | 8 + .../gui/tests/sessions-subagents.test.cjs | 17 +- internal/sessions/cc_copies.go | 120 ++++++++-- internal/sessions/cc_copies_test.go | 216 +++++++++++++----- internal/sessions/manage.go | 3 +- internal/sessions/sessions.go | 37 ++- internal/sessions/stats.go | 12 +- .../testdata/claude-forks/tasks-branch.jsonl | 44 ++++ .../testdata/claude-forks/tasks-fork-2.jsonl | 31 +++ .../testdata/claude-forks/tasks-fork.jsonl | 31 +++ .../testdata/claude-forks/tasks.jsonl | 63 +++++ internal/sessions/transcript.go | 61 +++++ 14 files changed, 613 insertions(+), 78 deletions(-) create mode 100644 internal/sessions/testdata/claude-forks/tasks-branch.jsonl create mode 100644 internal/sessions/testdata/claude-forks/tasks-fork-2.jsonl create mode 100644 internal/sessions/testdata/claude-forks/tasks-fork.jsonl create mode 100644 internal/sessions/testdata/claude-forks/tasks.jsonl diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index b5545d109..56f330133 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -664,7 +664,35 @@ establishes them, `parent_session` and `root_session`. A child whose root is not known keeps an empty `root_session`; Codex entries without explicit identity metadata, gateway-only entries and conflicting identities set `role` to `unknown` and do not acquire a guessed parent or root. These fields are omitted -for other agents. This is Codex-specific (#1591). +for other agents, except a Claude-format branch (below). This is Codex-specific +(#1591). + +Claude Code copies a conversation into a new session file when it forks one +(`--resume --fork-session`, the Agent SDK's `forkSession`, `/branch`): +every message again, under the same uuids, with the new session id; after a +compaction the copy starts at the compaction's summary +([`cc_copies.go`](../../internal/sessions/cc_copies.go), `ccFamily`). Files +are grouped by their first message and their compactions' summaries. A file +whose last message and first message another file of the group holds is a +copy: not listed, its id in `Copies`, counted in the stats only for what the +keeper doesn't hold (`ccRest`). A file that holds what came before the +other's start (the compaction the fork started after) is never that one's +copy. What is left of the group went on apart and is one conversation: the +head is the file that holds the others' first message as they don't hold +its, else the one a `/branch` names in `forkedFrom`, else the earliest, then +the lowest key. Each other one is a branch: listed with `role` `branch` and +`parent_session` the head's id, its transcript (`TranscriptOf`, Markdown +export) is the head's from its start up to the last message the two share, +then the branch's own lines (`branchTranscript`), and the stats count only +its own lines. A search reads the branch's own file. Neo (#feedback, after +v0.1.1809): a client that ran each task in a fork of a compacted session +showed a row per task, each opening on the summary and that one task, while +the session itself was hidden as a fork's copy. See +`TestClaudeForksAfterCompactAreBranches`, +`TestClaudeSlashBranchAfterCompactIsABranch`, +`TestClaudeBranchNamesItsSession` and +`TestClaudeCopyBeforeACompactionIsCounted`; the fixtures in +`testdata/claude-forks` are Claude Code 2.1.312's own files. A session whose agent keeps its subagents' conversations beside its own lists them in `subagents` on the sessions API (`/api/sessions`, `one`, `manage`; #2261, @@ -721,7 +749,9 @@ kind, set in under the subagent that started it, and a click shows its conversation (`sub=`). A Codex thread whose `parent_session` chain reaches a listed session is drawn, as a session with its own details, Resume and Delete, under its parent in the same fold; a loop of parents, a chain deeper -than eight or a parent not listed leaves it on top. The folder's count and +than eight or a parent not listed leaves it on top. A Claude branch is drawn +under its session the same way, and the fold names them apart ("2 +branches · 1 subagent"). The folder's count and checkbox cover the sessions under others. See `sessions-subagents.test.cjs`. Codex keeps the existing session-title precedence. Only when that leaves the diff --git a/internal/gui/assets/plugins/sessions/client.js b/internal/gui/assets/plugins/sessions/client.js index 7a474edc5..491a6fb03 100644 --- a/internal/gui/assets/plugins/sessions/client.js +++ b/internal/gui/assets/plugins/sessions/client.js @@ -691,7 +691,8 @@ magpie.plugin("sessions", (ctx) => { } // nest finds the sessions shown that are under another one shown: a - // Codex thread whose parent_session is, by a chain with no loop in it + // Codex thread, or a Claude branch, whose parent_session is, by a chain + // with no loop in it function nest(list) { const byId = new Map(list.map((s) => [s.id, s])); kidsOf = new Map(); @@ -938,18 +939,23 @@ magpie.plugin("sessions", (ctx) => { // subagentsBox is the fold under a session with its subagents: those // its agent started inside it, each opened to its conversation, and the - // Codex threads it started, drawn as sessions + // Codex threads it started, drawn as sessions; and the Claude sessions + // forked from it that went on (role "branch"), named apart function subagentsBox(s, kids, depth) { const box = el("div", "sm-subs"); const k = s.agent + "/" + s.id; const subs = subTree(s.subagents || []); - const n = kids.length + subs.length; + const branches = kids.filter((c) => c.role === "branch").length; + const n = kids.length + subs.length - branches; + const words = []; + if (branches) words.push(t(branches === 1 ? "{n} branch" : "{n} branches", { n: branches })); + if (n || !branches) words.push(t(n === 1 ? "{n} subagent" : "{n} subagents", { n })); const draw = () => { const open = subsOpen.has(k) || (!!query.trim() && kids.length > 0); const fold = el("button", "fold sm-subs-fold"); fold.type = "button"; fold.setAttribute("aria-expanded", String(open)); - fold.append(svg(CHEV_R, 10, 1.6), el("span", "", t(n === 1 ? "{n} subagent" : "{n} subagents", { n }))); + fold.append(svg(CHEV_R, 10, 1.6), el("span", "", words.join(" · "))); fold.onclick = (e) => { e.stopPropagation(); if (subsOpen.has(k)) subsOpen.delete(k); else subsOpen.add(k); diff --git a/internal/gui/assets/plugins/sessions/i18n.js b/internal/gui/assets/plugins/sessions/i18n.js index eb2c1a038..6f29e932a 100644 --- a/internal/gui/assets/plugins/sessions/i18n.js +++ b/internal/gui/assets/plugins/sessions/i18n.js @@ -7,6 +7,8 @@ magpie.strings({ "Delete all sessions": "删除全部会话", "{n} subagent": "{n} 个子代理", "{n} subagents": "{n} 个子代理", + "{n} branch": "{n} 个分支", + "{n} branches": "{n} 个分支", "Relocate project": "迁移项目会话", "New absolute project path": "新的项目绝对路径", "Preview relocation": "预览迁移", @@ -84,6 +86,8 @@ magpie.strings({ "Delete all sessions": "刪除全部會話", "{n} subagent": "{n} 個子代理", "{n} subagents": "{n} 個子代理", + "{n} branch": "{n} 個分支", + "{n} branches": "{n} 個分支", "Relocate project": "遷移專案會話", "New absolute project path": "新的專案絕對路徑", "Preview relocation": "預覽遷移", @@ -161,6 +165,8 @@ magpie.strings({ "Delete all sessions": "すべてのセッションを削除", "{n} subagent": "{n} 個のサブエージェント", "{n} subagents": "{n} 個のサブエージェント", + "{n} branch": "{n} 個のブランチ", + "{n} branches": "{n} 個のブランチ", "Relocate project": "プロジェクトを移動", "New absolute project path": "新しいプロジェクトの絶対パス", "Preview relocation": "移動をプレビュー", @@ -238,6 +244,8 @@ magpie.strings({ "Delete all sessions": "Alle Sitzungen löschen", "{n} subagent": "{n} Subagent", "{n} subagents": "{n} Subagenten", + "{n} branch": "{n} Zweig", + "{n} branches": "{n} Zweige", "Relocate project": "Projekt verschieben", "New absolute project path": "Neuer absoluter Projektpfad", "Preview relocation": "Verschieben prüfen", diff --git a/internal/gui/tests/sessions-subagents.test.cjs b/internal/gui/tests/sessions-subagents.test.cjs index 43b329952..a6b5575b6 100644 --- a/internal/gui/tests/sessions-subagents.test.cjs +++ b/internal/gui/tests/sessions-subagents.test.cjs @@ -9,6 +9,8 @@ // parent_session is listed is drawn under that session, a session of its // own still (its details open where it is), and only main sessions are on // top; a loop of parents, or a parent not listed, leaves a thread on top. +// A Claude session forked from another that went on (role "branch") sits +// under it the same way, its fold naming them branches. // No click moves the page, nothing has a left-border accent, no