Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 114 additions & 3 deletions src/workbench/extensions/agent/AgentPanelRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3942,6 +3942,38 @@ describe('AgentPanelRoot lifecycle', () => {
expect(urls.some((url) => url.endsWith('/cancel'))).toBe(false)
})

it('invalidates queued active tabs before detaching the doc op minter', async () => {
const tab = addTab('workflows/current.json')
workflowStore.activeWorkflow = tab
useAgentWorkflowTabBindingStore().bind('wf-42', tab.path)
vi.stubGlobal(
'fetch',
vi.fn(async () => json(202, ack('wf-42')))
)
vi.mocked(attachDocOpMinter).mockImplementationOnce((deps) => {
docOpMinterDeps.current = deps
return fromPartial<DocOpMinter>({
detach: vi.fn(() => {
ws.emit('agent_active_tab', {
workflow_id: 'wf-detach',
name: 'Detach delivery',
thread_id: 'th-1'
})
})
})
})
const panel = renderWithSelectedTarget()
await sendFromComposer('work here')

panel.unmount()

await vi.waitFor(() =>
expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-detach')).toBe(
'workflows/Detach delivery.json'
)
)
})

it('releases the minimap graph-activity layer even when another teardown step throws', () => {
const errorHandler = vi.fn()
const first = render(AgentPanelRoot, {
Expand All @@ -3955,11 +3987,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()

Expand All @@ -3971,6 +4009,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<DocOpMinter>({
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<DocOpMinter>({
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(() => {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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')
Expand Down
112 changes: 88 additions & 24 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,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<Record<string, () => undefined>>
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
): 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 {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
// 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()
Expand Down Expand Up @@ -1074,28 +1110,50 @@ async function openNewAgentTab(

void refreshCloudWorkflowIds()
onBeforeUnmount(() => {
++activeTabGeneration
releaseCoachCompletionWaiters()
docOpMinter.detach()
restoreOpMinter.detach()
if (
(coachDeferredBy.value === null || !agentPanelStore.isVisible) &&
composerStore.submission?.id === consentHeldSubmissionId
)
composerStore.invalidateSubmission()
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: () => {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
conversationStore.setCanvasSyncGate(
() => false,
() => 0
)
}
})
})

const history = useAgentChatHistoryStore()
Expand Down Expand Up @@ -1438,7 +1496,13 @@ const attachment = useAttachment({
remove: composerStore.removeAttachment
})

onBeforeUnmount(() => attachment.cancelAllUploads())
onBeforeUnmount(() =>
runPanelTeardown({
cancelAllUploads: () => {
attachment.cancelAllUploads()
}
})
)

function onAttach(): void {
exitNodeSelectionMode()
Expand Down
Loading