Skip to content

The agent that grows with you

fix(desktop): a selected-message branch sends its durable boundary row

Teknium15bfec0b3fe8
diff --git a/apps/desktop/src/api/canonical-protocol.test.ts b/apps/desktop/src/api/canonical-protocol.test.ts
index d94f89f1242..1e3bda29d84 100644
--- a/apps/desktop/src/api/canonical-protocol.test.ts
+++ b/apps/desktop/src/api/canonical-protocol.test.ts
@@ -226,3 +226,16 @@ test('the composer model picker travels as the canonical model mutation, not an
expect(protocol.wire('config.set', protocol.prepare('config.set', { session_id: 's', key: 'busy', value: 'queue' }))).toBe('config.set')
expect(protocol.wire('config.set', protocol.prepare('config.set', { session_id: 's', key: 'model', value: 'gpt-5 --global' }))).toBe('config.set')
})
+
+test('a message-level branch carries its durable boundary row; a whole-chat branch copies everything', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 3, execution_generation: 1 })
+ const selected = protocol.prepare('session.branch', { session_id: 's', idempotency_key: 'k', count: 2, through_message_id: 41 })
+ expect(protocol.wire('session.branch', selected)).toBe('session.mutate')
+ expect(selected).toMatchObject({ operation: 'branch', payload: { through_message_id: 41 } })
+ // A lost-response retry presents the identical mutation (the boundary is part of its identity).
+ expect(protocol.prepare('session.branch', { session_id: 's', idempotency_key: 'k', count: 2, through_message_id: 41 })).toEqual(selected)
+ // A selected message with no durable row yet must never fall back to a whole-history copy.
+ expect(() => protocol.prepare('session.branch', { session_id: 's', count: 2 })).toThrow(/not saved yet/)
+ expect(protocol.prepare('session.branch', { session_id: 's' })).toMatchObject({ payload: {} })
+})
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 50eb51e68da..5e18a3610df 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -76,6 +76,18 @@ export function siblingRoute(profile: unknown): string | null {
return typeof profile === 'string' && profile && profile !== 'default' ? profile : null
}
+// A message-level branch (`count` = the selected prefix) keeps rows through `through_message_id`.
+// Without that durable id the authority would copy the whole history, so it refuses instead.
+function branchBoundary(params: Record<string, unknown>): Record<string, unknown> {
+ const through = params.through_message_id
+
+ if (typeof through === 'number' && Number.isInteger(through) && through > 0) { return { through_message_id: through } }
+
+ if (params.count !== undefined) { throw new Error('This message is not saved yet; wait for it to settle, then branch from it again') }
+
+ return {}
+}
+
const BRANCH_METHODS = new Set(['session.branch', 'session.branch_stored', 'session.branch_whole'])
const MUTATION_METHODS = new Set(['session.title', 'session.archive', 'session.compress', ...BRANCH_METHODS])
@@ -186,7 +198,7 @@ export class CanonicalDesktopProtocol {
// Branch and compress fence the execution generation like the slash directives.
const fenced = ({
- 'session.branch': () => ({ operation: 'branch', payload: {} }),
+ 'session.branch': () => ({ operation: 'branch', payload: branchBoundary(params) }),
'session.compress': () => ({ operation: 'compress', payload: params.focus_topic ? { focus: String(params.focus_topic) } : {} })
} as Record<string, () => { operation: string; payload: Record<string, unknown> }>)[method]?.()
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.test.ts
new file mode 100644
index 00000000000..435aa804837
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.test.ts
@@ -0,0 +1,25 @@
+import { expect, test } from 'vitest'
+
+import { toChatMessages } from '@/lib/chat-messages/hydration'
+
+import { branchThroughRowId } from './branch-boundary'
+import { selectBranchMessages } from './utils'
+
+test('branching from a folded assistant bubble keeps through its last durable row, not its first', () => {
+ const messages = toChatMessages([
+ { row_id: 10, role: 'user', content: 'first question', timestamp: 1 },
+ { row_id: 11, role: 'assistant', content: '', tool_calls: [{ id: 'c1', type: 'function', function: { name: 'terminal', arguments: '{}' } }], timestamp: 2 },
+ { row_id: 12, role: 'tool', content: 'ran', tool_call_id: 'c1', timestamp: 3 },
+ { row_id: 13, role: 'assistant', content: 'first answer', timestamp: 4 },
+ { row_id: 14, role: 'user', content: 'later question the branch must leave out', timestamp: 5 },
+ { row_id: 15, role: 'assistant', content: 'later answer', timestamp: 6 }
+ ] as never)
+
+ const answer = messages.find(message => message.role === 'assistant' && JSON.stringify(message.parts).includes('first answer'))!
+ const branch = selectBranchMessages(messages, messages, answer.id)
+
+ expect(branch.map(message => message.content)).toEqual(['first question', 'first answer'])
+ expect(branchThroughRowId(branch)).toBe(13)
+ // An optimistic bubble with no durable row has no boundary (the adapter refuses it).
+ expect(branchThroughRowId([{ content: 'x', role: 'user', source: { id: 'local', role: 'user', parts: [] } }])).toBeUndefined()
+})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.ts b/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.ts
new file mode 100644
index 00000000000..1f4d5b52e4e
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/branch-boundary.ts
@@ -0,0 +1,23 @@
+import type { BranchMessage } from './utils'
+
+/**
+ * The durable row a message-level branch keeps through: the highest row id the selected bubble
+ * shows. An assistant bubble folds several rows (tool-call turns, then its answer), and each
+ * text part keeps the row it came from, so the bubble's own `rowId` (its FIRST row) would cut the
+ * answer off. The authority pulls in the tool results that directly answer an assistant boundary.
+ * Undefined when the bubble has no durable row yet (an optimistic, unsettled message).
+ */
+export function branchThroughRowId(messages: readonly BranchMessage[]): number | undefined {
+ const source = messages.at(-1)?.source
+
+ if (!source) {
+ return undefined
+ }
+
+ const ids = [
+ source.rowId,
+ ...source.parts.map(part => (part.type === 'text' ? part.sourceRowId : undefined))
+ ].filter((id): id is number => typeof id === 'number' && Number.isInteger(id) && id > 0)
+
+ return ids.length ? Math.max(...ids) : undefined
+}
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 6feb81e756e..1e208d4c8ec 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -159,6 +159,7 @@ import {
} from '../session-context-drift'
import { singleFlightSessionResume } from '../use-prompt-actions/single-flight-resume'
+import { branchThroughRowId } from './branch-boundary'
import { branchCreateKey } from './branch-create-key'
import { sessionCreateOverrideParams, type SessionCreateOverrides, type SessionSeedMessage } from './create-overrides'
import { markSessionCreatedThisRun, sessionCreatedThisRun } from './created-this-run'
@@ -865,6 +866,7 @@ export function useSessionActions({
if (createIntentRef.current === createIntent) {
createIntentRef.current = null
}
+
activeSessionIdRef.current = created.session_id
selectedStoredSessionIdRef.current = stored
ensureSessionState(created.session_id, stored)
@@ -2737,7 +2739,7 @@ export function useSessionActions({
// Stable per-attempt key: a lost-response retry of session.branch /
// session.branch_whole returns the SAME child (#65410).
idempotency_key: key,
- ...(branchCount !== undefined ? { count: branchCount } : {})
+ ...(branchCount !== undefined ? { count: branchCount, through_message_id: branchThroughRowId(branchMessages) } : {})
}
const createParams = {