fix(native-chat): forget a chat tab's open subagent when the tab closes (#42254)

The pane-to-subagent map only dropped an entry on going back to the main
agent, so a tab closed with a subagent open kept its entry for the
renderer's life. The removed-chat-tab seam that retires draft positions
now retires those entries too.

(cherry picked from commit 8e522d68ac)
This commit is contained in:
Kelvin Amoaba
2026-11-13 20:54:00 -08:00
committed by Brennan Benson
parent 6c49de1986
commit cdcafcd8e2
7 changed files with 82 additions and 35 deletions
@@ -1,29 +0,0 @@
import type { Tab } from '../../../../../shared/tab-types'
import { retireNativeChatDraftPosition } from './native-chat-composer-draft-memory'
import { readNativeChatComposerDraft } from './native-chat-composer-draft-store'
import { structuredAgentSessionDraftScopeKey } from './native-chat-draft-scope'
/** A destination's death retires only its unsatisfied UI position, never its ordinary content. */
export function retireRemovedNativeChatDraftPositions(
before: readonly Tab[],
after: readonly Tab[]
): void {
if (before === after) {
return
}
const remaining = new Map(after.map((tab) => [tab.id, tab]))
for (const tab of before) {
const next = remaining.get(tab.id)
if (
tab.contentType === 'agent-session' &&
(next?.contentType !== tab.contentType ||
next.entityId !== tab.entityId ||
next.executionHostId !== tab.executionHostId)
) {
retireNativeChatDraftPosition(
readNativeChatComposerDraft(structuredAgentSessionDraftScopeKey(tab.entityId)),
tab.id
)
}
}
}
@@ -0,0 +1,34 @@
// @vitest-environment happy-dom
import { act, renderHook } from '@testing-library/react'
import { expect, it } from 'vitest'
import { makeUnifiedTab } from '../../../store/slices/tabs/tab-group.test-fixture'
import {
openNativeChatSubagent,
useNativeChatOpenSubagent
} from '../subagents/native-chat-open-subagent'
import { retireRemovedNativeChatTabs } from './native-chat-removed-tab-retirement'
const chat = (id: string) =>
makeUnifiedTab({
id,
worktreeId: 'wt-1',
groupId: 'group-1',
entityId: `session-${id}`,
contentType: 'agent-session'
})
it('drops a closed chat tab’s open subagent and keeps the other tabs’', () => {
const closed = chat('tab-closed')
const kept = chat('tab-kept')
act(() => {
openNativeChatSubagent('tab-closed:pane', 'agent-1')
openNativeChatSubagent('tab-kept:pane', 'agent-2')
})
const closedView = renderHook(() => useNativeChatOpenSubagent('tab-closed:pane'))
const keptView = renderHook(() => useNativeChatOpenSubagent('tab-kept:pane'))
act(() => retireRemovedNativeChatTabs([closed, kept], [kept]))
expect(closedView.result.current).toBeNull()
expect(keptView.result.current).toEqual({ agentId: 'agent-2', parentId: null })
})
@@ -0,0 +1,28 @@
import type { Tab } from '../../../../../shared/tab-types'
import { retireNativeChatDraftPosition } from '../drafts/native-chat-composer-draft-memory'
import { readNativeChatComposerDraft } from '../drafts/native-chat-composer-draft-store'
import { structuredAgentSessionDraftScopeKey } from '../drafts/native-chat-draft-scope'
import { retireNativeChatSubagentViews } from '../subagents/native-chat-open-subagent'
/** A chat tab's death retires its pane-only UI: the draft's unsatisfied position (never the draft) and any open subagent view. */
export function retireRemovedNativeChatTabs(before: readonly Tab[], after: readonly Tab[]): void {
if (before === after) {
return
}
const remaining = new Map(after.map((tab) => [tab.id, tab]))
for (const tab of before) {
const next = remaining.get(tab.id)
if (
tab.contentType === 'agent-session' &&
(next?.contentType !== tab.contentType ||
next.entityId !== tab.entityId ||
next.executionHostId !== tab.executionHostId)
) {
retireNativeChatDraftPosition(
readNativeChatComposerDraft(structuredAgentSessionDraftScopeKey(tab.entityId)),
tab.id
)
retireNativeChatSubagentViews(tab.id)
}
}
}
@@ -41,6 +41,20 @@ export function openNativeChatSubagent(paneKey: string, agentId: string | null):
publish()
}
/** A closed or replaced chat tab takes its panes' open subagents with it. */
export function retireNativeChatSubagentViews(tabId: string): void {
let retired = false
for (const paneKey of openByPane.keys()) {
if (paneKey.startsWith(`${tabId}:`)) {
openByPane.delete(paneKey)
retired = true
}
}
if (retired) {
publish()
}
}
export function noteNativeChatSubagentParent(
paneKey: string,
agentId: string,
@@ -1,4 +1,4 @@
import { retireRemovedNativeChatDraftPositions } from '@/components/native-chat/drafts/native-chat-draft-position-retirement'
import { retireRemovedNativeChatTabs } from '@/components/native-chat/session/native-chat-removed-tab-retirement'
import {
markStructuredAgentSessionLaunchesPublished,
publishedStructuredSessions
@@ -199,7 +199,7 @@ export function applyWebSessionTabsStorePatch(
}
}
for (const key of new Set(affectedWorkspaces)) {
retireRemovedNativeChatDraftPositions(
retireRemovedNativeChatTabs(
state.unifiedTabsByWorktree[key] ?? [],
(patch.unifiedTabsByWorktree ?? state.unifiedTabsByWorktree)[key] ?? []
)
@@ -1,4 +1,4 @@
import { retireRemovedNativeChatDraftPositions } from '@/components/native-chat/drafts/native-chat-draft-position-retirement'
import { retireRemovedNativeChatTabs } from '@/components/native-chat/session/native-chat-removed-tab-retirement'
// Select moved tabs with the frame showing the move (L120); a later reply may already be superseded by another drag.
import type { AppState } from '../../types'
@@ -95,7 +95,7 @@ export function showFramePatch(
const before = store.getState()
const shown = withMovedTabSelections(before, patch)
for (const key of rebuiltWorkspaces) {
retireRemovedNativeChatDraftPositions(
retireRemovedNativeChatTabs(
before.unifiedTabsByWorktree[key] ?? [],
(shown.patch.unifiedTabsByWorktree ?? before.unifiedTabsByWorktree)[key] ?? []
)
@@ -4,7 +4,7 @@ import {
emptiedWorktreeLandingPatch,
landOnEmptiedWorktree
} from './tabs-empty-workspace'
import { retireRemovedNativeChatDraftPositions } from '@/components/native-chat/drafts/native-chat-draft-position-retirement'
import { retireRemovedNativeChatTabs } from '@/components/native-chat/session/native-chat-removed-tab-retirement'
import { retireStructuredAgentSessionReadOwner } from '@/components/native-chat/session/structured/read/structured-agent-session-read-owner-registry'
import type { TabsSlice, TabsSliceGet, TabsSliceSet } from './tabs-slice-contract'
import { collapseWorktreeGroupLayout } from '../../../../../shared/workspace-layout/tab-groups/tab-group-layout-tree'
@@ -127,7 +127,7 @@ export function createTabsCloseActions(
set((current) => {
const nextTabs = (current.unifiedTabsByWorktree[worktreeId] ?? []).filter((item) => {
if (item.id === tabId) {
retireRemovedNativeChatDraftPositions([item], [])
retireRemovedNativeChatTabs([item], [])
}
return item.id !== tabId
})