Chat: branching works on the session authority (bugfix, fixes #467) #469
On the session authority, /branch N can copy the whole chat when message N comes from a turn sent after the chat was opened.
rows only holds what was read on opening plus older pages (loadAllHistory doesn't fetch newer rows). So when the Nth counted row is in a turn sent since then, lastCountedRowId returns null and this falls back to newestPage()?.lastOrNull(), the newest row. Example: open a chat with 2 turns (4 counted rows), send 3 more, then /branch 6. The mutation branches through row 10, so all 10 messages are copied instead of 6. The legacy session.branch {count: 6} keeps 6.
Fix: keep counting into the newest page instead of taking its last row. Append the page's rows whose ids aren't already in rows, and find the keepth counted row across both lists. Fall back to the newest row only when keep is larger than the whole transcript, which is the case where legacy keeps everything. If the page doesn't reach far enough, return null (BRANCH_ROW_UNKNOWN). A test like aNewChatsLiveTurnBranchesThroughItsStoredRows with /branch N landing in the live turns would cover it.
No open findings. The one earlier finding (/branch N past the loaded rows copied the whole chat on the session authority) is fixed: it now counts on the rows as stored now, and anEarlierLiveTurnBranchesThroughItsOwnRows covers it. This pass found nothing new against the CLAUDE.md checklist or CONTRIBUTING.md.
[!NOTE]
🤖 Claude Fable 5.2 responding on behalf of Giton22
Fixed in 3a4b0c2b and 87270434. /branch N past the loaded rows now counts on the rows as stored now (currentRows() joins the newest 500 rows onto the loaded ones) and takes the newest row only when N is past the whole transcript. anEarlierLiveTurnBranchesThroughItsOwnRows covers /branch 1 landing in live turns.
@claude review
Giton22
Bug fix. On a gateway whose chats run on the session authority (the gateway-hosted Webapp listener), pressing Branch, or typing
/branch, failed withunknown method: session.branch — not available on this Hermes backend. The Hermes webapp could branch the same chat.Hermes didn't rename
session.branch. The session authority never served it, orsession.branch_whole. Its method table isgateway/session_controls.pyhandlers(). Branching there is abranchoperation ofsession.mutate, which is what Hermes Desktop / the webapp send (apps/desktop/src/api/canonical-protocol.ts). A standalonehermes dashboard, or any legacytui_gatewaysocket, still servessession.branch, so only the authority was affected.Fixes #467
What changed
On the session authority (
gateway.readysession_authority), a branch is now asession.mutatewith these fields:request_idexpected_revisionandexpected_generationoperation: "branch"payload:{}for the whole chat, or{through_message_id}when branching from a messageWhich row it branches through
/branch N: the row that N counts to, the same row the legacycountreaches./branch Npast the loaded rows counts on the rows as stored now, as the legacycountwould.Revision and retry
revisionis read fromsession.resume/session.create.session.infoandsession.updatedkeep it current; it only moves forward, and only for this chat's receipts.revision_conflictorstale_generationrefusal carries the current fences indata, so the branch is sent once more with those and a newrequest_id.RpcExceptionnow keeps the error'sdataso the retry can read those fences.Result
branched_session_id. The mutation returns no title, but the gateway titles the branch ("<parent> #2"). A chat opened without a title now takes the onesession.resumenames ininfo, so the branch's header shows it. Until then a stored chat without a title says "Untitled session".Other gateways
tui_gatewaykeepsession.branch_whole/session.branchexactly as before. The choice goes bysession_authority, not by trying a method and waiting for -32601.How it was proved
-
session.branch_whole,session.branchandsession.branch {count}each answer -32601 with the text from the issue.-
session.mutatewithoperation: "branch"branches: the whole chat with{}, a prefix with{through_message_id}.- A stale
expected_revisiongets4001 revision_conflictwithdata.revision/data.execution_generation. Resending with those values and a newrequest_idsucceeds.- Started a new chat, got a reply, tapped Branch under the reply, confirmed.
- The branch opened with the prompt and reply copied, and a new chat showed in the list.
- This run found a first-cut bug: a live reply was refused as "not saved". The second commit fixes that and adds a test for it.
- Reproduced: after two turns sent in one view, Branch under the first reply failed with "Couldn't find this message in the saved chat." After the fix it opens a branch with only the first turn.
- Reproduced: the branch opened as "Untitled session" while the chat list showed its title. After the fix the header shows the gateway's title.
ChatSessionTestwith the fake gateway. Eight new tests fail on master or on the first commit, and pass here. They cover:- a whole-chat branch on the authority (exact params, no
session.branch*sent);- from a reply with tool steps, from a prompt, and
/branch Nwith a hidden instruction row;- a new chat's live turn;
- the earlier of two live turns, with a note row between them, and
/branch Nlanding in live turns;- a chat opened without a title taking the one its resume names;
- the revision taken from
session.info/session.updated(another chat's receipt and an older receipt are ignored);- the retry with the refusal's fences;
- a second conflict surfacing the error.
The existing legacy branch tests still pass, and one now also checks that no
session.mutateis sent there.:desktopApp:compileKotlinJvm.Not verified
hermes dashboardsocket wasn't driven end to end. The legacy dispatcher was checked in-process: it still servessession.branchand has nosession.mutate, and the legacy tests cover that path.Reviewed with a Gemini loop in place of sol-loop: 2 rounds with Gemini 4 Neon (High). Verdict: approved.
Created with Claude Opus 5.5 in Claude Code (Orca). Reviewed and finished with Claude Fable 5.2.