diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 896589fb42d..c11130b5524 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -4340,11 +4340,17 @@ describe('AgentPanelRoot lifecycle', () => { }) first.unmount() - expect(errorHandler).toHaveBeenCalledWith( + // Contained and reported, not escaped: a step that throws must not reach + // Vue's error handling, which re-throws out of `invokeArrayFns` and would + // abandon the remaining hooks and the rest of `unmountComponent`. + expect(reportError).toHaveBeenCalledWith( expect.objectContaining({ message: 'teardown failed' }), - expect.anything(), - 'beforeUnmount hook' + expect.objectContaining({ + errorType: 'failure_tearing_down_agent_panel', + tags: { step: 'clearCreatingTab' } + }) ) + expect(errorHandler).not.toHaveBeenCalled() renderWithSelectedTarget().unmount() @@ -4356,6 +4362,79 @@ describe('AgentPanelRoot lifecycle', () => { ) }) + it('resets the canvas sync gate when an earlier teardown step throws', () => { + const errorHandler = vi.fn() + vi.mocked(attachDocOpMinter).mockImplementationOnce((deps) => { + docOpMinterDeps.current = deps + return fromPartial({ + detach: vi.fn(() => { + throw new Error('detach failed') + }) + }) + }) + const panel = render(AgentPanelRoot, { + global: { plugins: [i18n], config: { errorHandler } } + }) + const setCanvasSyncGate = vi.spyOn( + useAgentConversationStore(), + 'setCanvasSyncGate' + ) + // Cleared so the only recorded call is the teardown reset, not this + // instance's own live gate registered while it was mounted. + setCanvasSyncGate.mockClear() + + panel.unmount() + + // PM-1575: the gate is reset to the always-safe default even though a + // step three places ahead of it threw. + expect(setCanvasSyncGate).toHaveBeenCalledOnce() + const [gate, outcomeCount] = setCanvasSyncGate.mock.lastCall ?? [] + expect(gate?.()).toBe(false) + expect(outcomeCount?.()).toBe(0) + expect(reportError).toHaveBeenCalledWith( + expect.objectContaining({ message: 'detach failed' }), + expect.objectContaining({ + errorType: 'failure_tearing_down_agent_panel', + tags: { step: 'detachDocOpMinter' } + }) + ) + expect(errorHandler).not.toHaveBeenCalled() + }) + + it('resets the canvas sync gate when reporting a failed teardown step throws', () => { + const errorHandler = vi.fn() + vi.mocked(attachDocOpMinter).mockImplementationOnce((deps) => { + docOpMinterDeps.current = deps + return fromPartial({ + detach: vi.fn(() => { + throw new Error('detach failed') + }) + }) + }) + const panel = render(AgentPanelRoot, { + global: { plugins: [i18n], config: { errorHandler } } + }) + const setCanvasSyncGate = vi.spyOn( + useAgentConversationStore(), + 'setCanvasSyncGate' + ) + setCanvasSyncGate.mockClear() + // The reporter is the one part of the loop outside its own try/catch; a + // telemetry sink torn down ahead of the panel must not take the remaining + // releases with it. + vi.mocked(reportError).mockImplementationOnce(() => { + throw new Error('reporter failed') + }) + + panel.unmount() + + expect(setCanvasSyncGate).toHaveBeenCalledOnce() + const [gate, outcomeCount] = setCanvasSyncGate.mock.lastCall ?? [] + expect(gate?.()).toBe(false) + expect(outcomeCount?.()).toBe(0) + expect(errorHandler).not.toHaveBeenCalled() + }) + it('does not claim the minimap graph-activity layer when setup throws', () => { vi.mocked(useFreeUsePlacement).mockImplementationOnce(() => { throw new Error('setup failed') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index ef8bdccdabd..46310f3dd0e 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -438,7 +438,43 @@ onMounted(() => { agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity') syncAgentMinimapLayer(graphActivity.state) }) -onBeforeUnmount(() => agentMinimapLayer?.dispose()) +// Teardown must be total. Nothing is re-thrown: an error escaping an unmount +// hook reaches Vue's logError, which re-throws outside production builds (this +// app registers no app.config.errorHandler) and aborts both the steps after it +// and the rest of unmountComponent -- stranding this panel's later releases on +// singletons that outlive it, such as the PM-1575 canvas-sync gate below. +// +// Steps are keyed rather than positional for two reasons: the key names the +// failure in telemetry, which is now the only signal that a release was +// skipped, and insertion order is the run order (no key is integer-like, so +// Object.entries order is specified). They return `undefined` rather than +// `void` so an async step -- whose rejection would escape the catch below +// while still looking contained -- fails to compile instead. +function runPanelTeardown( + steps: Readonly undefined>> +): void { + for (const [step, release] of Object.entries(steps)) { + try { + release() + } catch (error) { + try { + reportError(error, { + errorType: 'failure_tearing_down_agent_panel', + tags: { step } + }) + } catch { + // A reporter that throws must not abort the teardown it reports on. + } + } + } +} +onBeforeUnmount(() => + runPanelTeardown({ + disposeMinimapLayer: () => { + agentMinimapLayer?.dispose() + } + }) +) const { accepted: consentAccepted } = storeToRefs(useAgentConsentStore()) const { withConsent } = useAgentConsent() const workspaceStore = useTeamWorkspaceStore() @@ -1187,28 +1223,50 @@ async function onAnswerAsk( void refreshCloudWorkflowIds() onBeforeUnmount(() => { - ++activeTabGeneration - releaseCoachCompletionWaiters() - if ( - (coachDeferredBy.value === null || !agentPanelStore.isVisible) && - composerStore.submission?.id === consentHeldSubmissionId - ) - composerStore.invalidateSubmission() - docOpMinter.detach() - restoreOpMinter.detach() - exitNodeSelectionMode() - stop() - tabActivity.setEditing(null) - tabActivity.setCreating(false) - // PM-1575: the store singleton outlives this component. Without resetting - // the gate here, a remount's own setCanvasSyncGate() call is the only - // thing standing between the old (now torn-down) follower's gate and a - // turn resumed in the meantime reading it -- reset to the always-safe - // default instead of leaving whatever this instance last set. - conversationStore.setCanvasSyncGate( - () => false, - () => 0 - ) + runPanelTeardown({ + invalidateActiveTabGeneration: () => { + ++activeTabGeneration + }, + releaseCoachCompletionWaiters: () => { + releaseCoachCompletionWaiters() + }, + invalidateConsentHeldSubmission: () => { + if ( + (coachDeferredBy.value === null || !agentPanelStore.isVisible) && + composerStore.submission?.id === consentHeldSubmissionId + ) + composerStore.invalidateSubmission() + }, + detachDocOpMinter: () => { + docOpMinter.detach() + }, + detachRestoreOpMinter: () => { + restoreOpMinter.detach() + }, + exitNodeSelectionMode: () => { + exitNodeSelectionMode() + }, + stopSession: () => { + stop() + }, + clearEditingTab: () => { + tabActivity.setEditing(null) + }, + clearCreatingTab: () => { + tabActivity.setCreating(false) + }, + // PM-1575: the store singleton outlives this component. Without resetting + // the gate here, a remount's own setCanvasSyncGate() call is the only + // thing standing between the old (now torn-down) follower's gate and a + // turn resumed in the meantime reading it -- reset to the always-safe + // default instead of leaving whatever this instance last set. + resetCanvasSyncGate: () => { + conversationStore.setCanvasSyncGate( + () => false, + () => 0 + ) + } + }) }) const { copy } = useClipboard({ legacy: true }) @@ -1583,7 +1641,13 @@ const attachment = useAttachment({ remove: composerStore.removeAttachment }) -onBeforeUnmount(() => attachment.cancelAllUploads()) +onBeforeUnmount(() => + runPanelTeardown({ + cancelAllUploads: () => { + attachment.cancelAllUploads() + } + }) +) function onAttach(): void { exitNodeSelectionMode()