Skip to content

The agent that grows with you

fix(desktop): pin a branch child's owning socket for the tile's lifetime

Zeus-Deus0cb3e4cfd814
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 8699682830f..5ea19942020 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
@@ -2012,6 +2012,19 @@ export function useSessionActions({
const effectiveBranchMessages = responseBranchMessages.length ? responseBranchMessages : branchMessages
const routedSessionId = branched.stored_session_id ?? branched.session_id
const preview = effectiveBranchMessages.map(({ content }) => content).find(Boolean) ?? null
+
+ // Record the exact owner and pin its socket THE MOMENT the create
+ // returns, before the optimistic row / tile publication can lose a
+ // race with the gateway pruner. A draft branch child exists only as a
+ // runtime on the owning backend (the stored row lands on first turn),
+ // so a prune in this gap orphan-reaps it and the tile enters the
+ // resume→reclaim flicker loop (#93892 shape). Mirrors the two routed
+ // creates at the top of this file.
+ if (ownerRoute) {
+ setSessionOwnerHint(routedSessionId, ownerRoute)
+ holdSessionOwnerUntilForeground(routedSessionId, ownerRoute)
+ }
+
// Draft until submit: nest under the parent at the parent's recency so it
// doesn't bubble to the top until a real message lands (backend persists
// + auto-names it then). The selected row survives refreshes (sessionsToKeep).
@@ -2068,7 +2081,17 @@ export function useSessionActions({
if (parentStoredId !== null && selectedStoredSessionIdRef.current === parentStoredId) {
await resumeSession(routedSessionId)
} else {
- openSessionTile(routedSessionId, 'center')
+ // Carry the exact owner onto the tile: its persisted ownerRoute is
+ // what pins the owning backend's socket in the gateway keep-set
+ // (openTileGatewayScopes) for the tile's whole lifetime. Without it
+ // a remote-owned branch child's tile pinned nothing, the pruner
+ // closed the owner socket, the backend reaped the draft runtime,
+ // and the tile looped resume→reclaim until the storm breaker
+ // latched "Couldn't open this session".
+ openSessionTile(routedSessionId, 'center', undefined, null, {
+ ownerRoute,
+ workspaceMode: 'sessions'
+ })
patchSessionTile(routedSessionId, { runtimeId: branched.session_id })
revealTreePane(`session-tile:${routedSessionId}`)
}
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
index 9dcbf9740e2..c343701c180 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
@@ -38,7 +38,7 @@ import {
setSessions,
setSessionsLoading
} from '@/store/session'
-import { $workingSessionIds, getRecentlySettledSessionIds } from '@/store/session-states'
+import { $sessionTiles, $workingSessionIds, getRecentlySettledSessionIds } from '@/store/session-states'
import { refreshCronJobs as refreshCronJobsStore } from '../../cron/cron-actions'
@@ -82,6 +82,15 @@ function sessionsToKeep(scope?: string): Set<string> {
...getRecentlySettledSessionIds()
])
+ // Open tiles are user-visible state exactly like the selected row: a branch
+ // child is a DRAFT until its first real turn, so the aggregator can't return
+ // it — without this the next background refresh silently dropped the
+ // optimistic `draft: branch #N` row while its tab was open, and the sidebar
+ // showed no trace of the branch until first send.
+ for (const tile of $sessionTiles.get()) {
+ keep.add(tile.storedSessionId)
+ }
+
const active = $selectedStoredSessionId.get()
if (active) {
diff --git a/apps/desktop/src/store/session-states-foreground-scopes.test.ts b/apps/desktop/src/store/session-states-foreground-scopes.test.ts
index 97ce973a144..2518a9fce4c 100644
--- a/apps/desktop/src/store/session-states-foreground-scopes.test.ts
+++ b/apps/desktop/src/store/session-states-foreground-scopes.test.ts
@@ -69,6 +69,25 @@ describe('foregroundSessionScopes: owner hold across the create → foreground g
expect(foregroundSessionScopes()).toEqual(new Set())
})
+ it('does not retire a hold for a tile that carries neither the route nor a scoped runtime', () => {
+ const pandora = { connectionId: '100-125-133-71-9119', mode: 'remote' as const, profile: 'default' }
+
+ holdSessionOwnerUntilForeground('stored-branch', pandora)
+ // The pre-fix branch path: openSessionTile with no workspaceScope mints a
+ // route-less tile. It pins nothing, so its mere existence must not retire
+ // the hold — that reopened the create→foreground gap and the pruner closed
+ // the owner socket under the draft runtime (resume→reclaim loop, #93892).
+ $sessionTiles.set([{ storedSessionId: 'stored-branch' }])
+ expect(foregroundSessionScopes()).toEqual(new Set(['conn:100-125-133-71-9119::default']))
+
+ // Once the tile actually names the owner (persisted route), it covers the
+ // hold and the hold retires for good.
+ $sessionTiles.set([{ ownerRoute: pandora, storedSessionId: 'stored-branch' }])
+ expect(foregroundSessionScopes()).toEqual(new Set(['conn:100-125-133-71-9119::default']))
+ $sessionTiles.set([])
+ expect(foregroundSessionScopes()).toEqual(new Set())
+ })
+
it('is released explicitly by the caller (failed create / drift close) and expires on its own', () => {
vi.useFakeTimers()
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index 4e2152a108f..03179c05b83 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -200,6 +200,32 @@ describe('resetTileRuntimeBindings', () => {
expect(invalidateRuntimeBindings).toHaveBeenCalledWith(new Set(['stored-barry-sibling-bot', 'stored-work-bot']))
})
+ it('keeps an owner-routed SESSIONS tile (branch child) bound across an unrelated reconnect', () => {
+ const invalidateRuntimeBindings = vi.fn()
+ setSessionTileDelegate({ invalidateRuntimeBindings } as unknown as SessionTileDelegate)
+ $sessionTiles.set([
+ {
+ ownerRoute: { connectionId: '100-125-133-71-9119', mode: 'remote', profile: 'default' },
+ runtimeId: 'runtime-branch-live',
+ storedSessionId: 'stored-branch-child',
+ workspaceMode: 'sessions'
+ }
+ ])
+
+ // A flapping sibling connection reconnects; the branch child's runtime
+ // lives on its parent's backend and must keep its binding — dropping it
+ // re-arms the tile's resume, and repeated sibling flaps latch the
+ // resume-storm error card over a healthy session.
+ resetTileRuntimeBindings({ connectionId: 'other-ssh-source', profile: 'default' })
+
+ expect($sessionTiles.get()[0]?.runtimeId).toBe('runtime-branch-live')
+ expect(invalidateRuntimeBindings).toHaveBeenCalledWith(new Set(['stored-branch-child']))
+
+ // Its OWN connection reconnecting still drops the binding for re-resume.
+ resetTileRuntimeBindings({ connectionId: '100-125-133-71-9119', profile: 'default' })
+ expect($sessionTiles.get()[0]?.runtimeId).toBeUndefined()
+ })
+
it('unknown restarted identity preserves only Bot runtimes owned by provably-live connections', () => {
// Legacy remote primary: no registry connectionId to scope by. The dead
// owner can't be named, so keep only owners we know are alive elsewhere —
@@ -244,6 +270,27 @@ describe('SessionTile workspace scope', () => {
$sessionTiles.set([])
})
+ it('persists a sessions-mode owner route so a branch child tile pins its owning socket', () => {
+ const ownerRoute = { connectionId: '100-125-133-71-9119', mode: 'remote' as const, profile: 'default' }
+
+ openSessionTile('branch-child', 'center', undefined, null, { ownerRoute, workspaceMode: 'sessions' })
+
+ expect($sessionTiles.get()).toEqual([
+ expect.objectContaining({ ownerRoute, storedSessionId: 'branch-child', workspaceMode: 'sessions' })
+ ])
+ })
+
+ it('keeps an existing sessions-mode owner route on a route-less re-scope', () => {
+ const ownerRoute = { connectionId: '100-125-133-71-9119', mode: 'remote' as const, profile: 'default' }
+
+ openSessionTile('branch-child', 'center', undefined, null, { ownerRoute, workspaceMode: 'sessions' })
+ // A plain sidebar re-open routes through setSessionTileWorkspaceScope with
+ // no route — absence of information, not a revocation.
+ setSessionTileWorkspaceScope('branch-child', { workspaceMode: 'sessions' })
+
+ expect($sessionTiles.get()).toEqual([expect.objectContaining({ ownerRoute, storedSessionId: 'branch-child' })])
+ })
+
it('stores an exact Bot owner and keeps it through placement patches', () => {
const ownerRoute = {
connectionId: 'connection-a',
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index d3402dc369a..56494073f40 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -1122,7 +1122,12 @@ export function setSessionTileWorkspaceScope(storedSessionId: string, scope: Ses
const tile = $sessionTiles.get().find(candidate => candidate.storedSessionId === storedSessionId)
const workspaceOwnerKey = scope.workspaceMode === 'bots' ? scope.workspaceOwnerKey : undefined
- const ownerRoute = scope.workspaceMode === 'bots' ? scope.ownerRoute : undefined
+ // Sessions-mode re-opens (sidebar click on an already-tiled session) pass no
+ // route; that is absence of information, not a revocation — keep the exact
+ // owner the tile was opened with (a branch child's parent connection) so a
+ // plain re-open can't unpin the owning socket. Bot scopes stay authoritative
+ // both ways: they always name their route explicitly.
+ const ownerRoute = scope.workspaceMode === 'bots' ? scope.ownerRoute : (scope.ownerRoute ?? tile?.ownerRoute)
const workspaceTabTitle = scope.workspaceMode === 'bots' ? scope.workspaceTabTitle : undefined
if (
@@ -1206,8 +1211,15 @@ export function resetTileRuntimeBindings(
const preservedStoredIds = new Set(
tiles
.filter(
+ // Any tile with an EXACT owner route — bot tabs always, and a
+ // sessions tile whose opener stamped one (a branch child on its
+ // parent's connection). Its runtime lives on that owner's socket,
+ // not the ambient gateway, so an unrelated connection's reconnect
+ // must not drop the binding: each drop re-arms the tile's resume,
+ // and a flapping sibling connection turns that into 4+ re-resumes
+ // inside the storm window — latching the "keeps losing its backend
+ // runtime" card over a session that is actually healthy.
tile =>
- tile.workspaceMode === 'bots' &&
Boolean(tile.ownerRoute?.connectionId) &&
(!(reconnected || liveConnectionIds) || !belongsToReconnectedRuntime(tile))
)
@@ -1386,7 +1398,15 @@ export function openSessionTile(
anchor: dock,
before,
dir,
- ownerRoute: workspaceScope.workspaceMode === 'bots' ? workspaceScope.ownerRoute : undefined,
+ // The owner route pins the owning backend's socket in the gateway
+ // keep-set (openTileGatewayScopes / foregroundSessionScopes) for as
+ // long as the tile is open. Bot tabs always carry one; a sessions-mode
+ // tile carries one when its opener knows the exact owner — e.g. a
+ // branch child created on its parent's owning connection, whose
+ // draft runtime is otherwise orphan-reaped the moment the pruner
+ // closes the unpinned socket (the resume/reclaim flicker loop,
+ // #93892 shape).
+ ownerRoute: workspaceScope.ownerRoute,
storedSessionId,
workspaceMode: workspaceScope.workspaceMode,
workspaceOwnerKey,