diff --git a/src/renderer/src/components/native-chat/drafts/native-chat-draft-position-retirement.ts b/src/renderer/src/components/native-chat/drafts/native-chat-draft-position-retirement.ts deleted file mode 100644 index a7ff1a5f476..00000000000 --- a/src/renderer/src/components/native-chat/drafts/native-chat-draft-position-retirement.ts +++ /dev/null @@ -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 - ) - } - } -} diff --git a/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.test.ts b/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.test.ts new file mode 100644 index 00000000000..29fadb996f2 --- /dev/null +++ b/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.test.ts @@ -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 }) +}) diff --git a/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.ts b/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.ts new file mode 100644 index 00000000000..9d5a7a1606e --- /dev/null +++ b/src/renderer/src/components/native-chat/session/native-chat-removed-tab-retirement.ts @@ -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) + } + } +} diff --git a/src/renderer/src/components/native-chat/subagents/native-chat-open-subagent.ts b/src/renderer/src/components/native-chat/subagents/native-chat-open-subagent.ts index 2b4e664150d..b3fbaa33eff 100644 --- a/src/renderer/src/components/native-chat/subagents/native-chat-open-subagent.ts +++ b/src/renderer/src/components/native-chat/subagents/native-chat-open-subagent.ts @@ -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, diff --git a/src/renderer/src/runtime/web-session-tabs-sync/store-patch.ts b/src/renderer/src/runtime/web-session-tabs-sync/store-patch.ts index 2fd734daa09..3cb63d5fc28 100644 --- a/src/renderer/src/runtime/web-session-tabs-sync/store-patch.ts +++ b/src/renderer/src/runtime/web-session-tabs-sync/store-patch.ts @@ -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] ?? [] ) diff --git a/src/renderer/src/store/slices/tabs/moved-tab-selection.ts b/src/renderer/src/store/slices/tabs/moved-tab-selection.ts index 63b6c1edabd..a5c13380086 100644 --- a/src/renderer/src/store/slices/tabs/moved-tab-selection.ts +++ b/src/renderer/src/store/slices/tabs/moved-tab-selection.ts @@ -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] ?? [] ) diff --git a/src/renderer/src/store/slices/tabs/tabs-close-actions.ts b/src/renderer/src/store/slices/tabs/tabs-close-actions.ts index da70642c918..f9699f56efc 100644 --- a/src/renderer/src/store/slices/tabs/tabs-close-actions.ts +++ b/src/renderer/src/store/slices/tabs/tabs-close-actions.ts @@ -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 })