diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 8d17c20d4fd..bf55979e791 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -510,6 +510,24 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('FE-1969: naming the rebound workflow keeps its confirmed subscription', async () => { + const setup = mountFollower('wf-1') + dispatchFrame('doc_subscribed', { ok: true }) + setup.unmount() + bridgeState.current = null + const { unmount, workflowId, status } = mountFollower(null) + dispatchFrame('doc_subscribed', { ok: true }) + + workflowId.value = 'wf-1' + await nextTick() + + expect(status().connected).toBe(true) + expect(bridge().subscribe).toHaveBeenCalledOnce() + vi.advanceTimersByTime(SUBSCRIBE_CATCHUP_GRACE_MS) + expect(bridge().resubscribe).toHaveBeenCalledOnce() + unmount() + }) + it('FE-1902: a real detach clears the persisted binding and unsubscribes', async () => { const { unmount, workflowId } = mountFollower('wf-1') dispatchFrame('doc_subscribed', { ok: true }) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 6a955165bdc..3ebf3bafa69 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -843,6 +843,21 @@ function startAgentCrdtFollower( if (justActivated) applyCollected(persisted) } + type TargetSnapshot = [string | null | undefined, boolean | undefined] + const isActivationEdge = ( + active: boolean, + previous: TargetSnapshot | undefined + ): boolean => active && previous?.[1] === false + const acknowledgesFollowedDoc = ( + next: string | null, + active: boolean, + justActivated: boolean + ): boolean => + active && + !justActivated && + next !== null && + next === subscribedWorkflowId.value + const activateTarget = (next: string, justActivated: boolean): void => { initialBind = false rebindProjection(next) @@ -853,14 +868,12 @@ function startAgentCrdtFollower( watch( [workflowId, isTargetActive], - ( - [next, active], - previous: [string | null | undefined, boolean | undefined] | undefined - ) => { + ([next, active], previous: TargetSnapshot | undefined) => { // Only the inactive->active edge, and never the `immediate` first run // (`previous` is undefined there): a plain mount or retarget has no // collected changes to apply, the graph watcher covers readiness. - const justActivated = active && previous?.[1] === false + const justActivated = isActivationEdge(active, previous) + if (acknowledgesFollowedDoc(next, active, justActivated)) return lifecycle.clearForRetarget() connected.value = false pendingLiveNodeIds.clear()