← Back to Pull requests

Chat: branching works on the session authority (bugfix, fixes #467) #469

Merged
Giton22 merged 7 files (393 lines) on Nov 22, 2026 20:12 UTC
View on GitHub
Giton22 Giton22 commented on Nov 22, 2026 19:33 UTC
Author

Bug fix. On a gateway whose chats run on the session authority (the gateway-hosted Webapp listener), pressing Branch, or typing /branch, failed with unknown 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, or session.branch_whole. Its method table is gateway/session_controls.py handlers(). Branching there is a branch operation of session.mutate, which is what Hermes Desktop / the webapp send (apps/desktop/src/api/canonical-protocol.ts). A standalone hermes dashboard, or any legacy tui_gateway socket, still serves session.branch, so only the authority was affected.

Fixes #467

What changed


On the session authority (gateway.ready session_authority), a branch is now a session.mutate with these fields:
  • request_id

  • expected_revision and expected_generation

  • operation: "branch"

  • payload: {} for the whole chat, or {through_message_id} when branching from a message
  • Which row it branches through

  • From a message: the newest stored row the message reaches. For a reply split by tool calls, that is its last step.

  • /branch N: the row that N counts to, the same row the legacy count reaches.

  • A message shown from the live stream (a turn sent since the chat was opened) isn't in the loaded rows. Its row is found among the rows stored since: the live prompts pair, in order, with the stored prompts, and a reply reaches up to the next prompt's row. A prompt with its own row id uses that.

  • /branch N past the loaded rows counts on the rows as stored now, as the legacy count would.
  • Revision and retry

  • The session's revision is read from session.resume / session.create. session.info and session.updated keep it current; it only moves forward, and only for this chat's receipts.

  • A revision_conflict or stale_generation refusal carries the current fences in data, so the branch is sent once more with those and a new request_id.

  • A second such refusal shows "The chat changed on the gateway while branching. Try again."

  • RpcException now keeps the error's data so the retry can read those fences.
  • Result

  • The new chat is 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 one session.resume names in info, so the branch's header shows it. Until then a stored chat without a title says "Untitled session".
  • Other gateways

  • The standalone dashboard and legacy tui_gateway keep session.branch_whole / session.branch exactly as before. The choice goes by session_authority, not by trying a method and waiting for -32601.
  • How it was proved


  • Before (request level, Hermes v0.23.3 gateway in WSL with a fake model, Webapp listener on :9120):

  • - session.branch_whole, session.branch and session.branch {count} each answer -32601 with the text from the issue.
    - session.mutate with operation: "branch" branches: the whole chat with {}, a prefix with {through_message_id}.
    - A stale expected_revision gets 4001 revision_conflict with data.revision / data.execution_generation. Resending with those values and a new request_id succeeds.
  • After (emulator, Kyllene Dev from this branch on the same v0.23.3 gateway):

  • - 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.
  • Review pass (emulator, same gateway):

  • - 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.
  • Tests: ChatSessionTest with 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 N with a hidden instruction row;
    - a new chat's live turn;
    - the earlier of two live turns, with a note row between them, and /branch N landing 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.mutate is sent there.

  • The full CI command from CONTRIBUTING.md passes, plus :desktopApp:compileKotlinJvm.
  • Not verified


  • Only Hermes v0.23.3 was run. Older authority versions (v0.22.1 and later) are inferred from source.

  • The standalone hermes dashboard socket wasn't driven end to end. The legacy dispatcher was checked in-process: it still serves session.branch and has no session.mutate, and the legacy tests cover that path.

  • The retry was tested in unit tests and by hand-built requests. A real concurrent edit racing a branch on a device was not tested.

  • The fake model makes no tool calls, so the tool-step boundary is covered by unit tests only.

  • A live message can't be placed when a prompt shown never reached the gateway, or when a new chat has 500 or more rows that were never read. It shows "Couldn't find this message in the saved chat. Open the chat again, then branch from it." instead of branching at a guess.

  • Branching from a message that a compaction archived wasn't tested; the gateway refuses a row that is no longer active.

  • Not tested on a physical phone, at 360 dp or in dark theme. There is no UI change apart from that message and the header's title.
  • 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.

    Diff: 393 lines (+368 / -25) across 7 files Reviewer: Giton22 Review duration: 39m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.85) × (1 − Review Confidence (0.81)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.85 · 393 lines diff across 7 files
    0.85
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 4 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    81%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.358
    Self Review self_review
    0.140
    Sensitive Paths sensitive_paths
    0.085
    Real PR Conversation Timeline
    5 items
    github-actions[bot] github-actions[bot] commented on code on Nov 22, 2026 19:34 UTC
    Code Review
    shared/core/src/commonMain/kotlin/dev/hermeskotlin/core/chat/ChatSession.kt:1
    @@ -1333,7 +1343,8 @@ class ChatSession(
                 if (!loadAllHistory()) return null
                 withHiddenRows(it)
             }
    -        return branchKeeping(keep)
    +        // Past the loaded rows (turns of ours since they were read): the newest stored row.
    +        return branchKeeping(keep) { keep?.let { lastCountedRowId(it) ?: newestPage()?.lastOrNull()?.id } }

    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.

    github-actions[bot] github-actions[bot] commented on Nov 22, 2026 19:34 UTC
    Bot

    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.

    Giton22 Giton22 commented on code on Nov 22, 2026 20:03 UTC
    Code Review
    shared/core/src/commonMain/kotlin/dev/hermeskotlin/core/chat/ChatSession.kt:1
    @@ -1333,7 +1343,8 @@ class ChatSession(
                 if (!loadAllHistory()) return null
                 withHiddenRows(it)
             }
    -        return branchKeeping(keep)
    +        // Past the loaded rows (turns of ours since they were read): the newest stored row.
    +        return branchKeeping(keep) { keep?.let { lastCountedRowId(it) ?: newestPage()?.lastOrNull()?.id } }

    [!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.

    Giton22 Giton22 commented on Nov 22, 2026 20:09 UTC
    Author

    @claude review

    Reviewers
    Assignees
    Giton22 Giton22
    Labels
    risk: low confidence: 81.3% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 84.7% (HIGH) · Higher = larger blast radius
    84.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 81.3% · Lower = rushed or shallow review
    81.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.8% · Safe to merge
    15.8%