Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,32 @@ import type { GraphOperation } from './graphOperations'
import type { BatchOutcome, OpSenderDeps } from './opSender'

const bridgeState = vi.hoisted(() => {
const liveSubscriptions = new Map<object, Set<string>>()
let maxLiveSubscriptions = 0
const countLiveSubscriptions = () =>
[...liveSubscriptions.values()].reduce(
(count, workflows) => count + workflows.size,
0
)

class FakeBridge extends EventTarget {
subscribe = vi.fn()
unsubscribe = vi.fn()
subscribe = vi.fn((workflowId: string) => {
// A bridge retargets its single subscription; concurrency is measured
// across bridge instances, not across its historical workflow ids.
liveSubscriptions.set(this, new Set([workflowId]))
maxLiveSubscriptions = Math.max(
maxLiveSubscriptions,
countLiveSubscriptions()
)
})
unsubscribe = vi.fn(() => {
liveSubscriptions.delete(this)
})
resubscribe = vi.fn()
reconcile = vi.fn()
destroy = vi.fn()
destroy = vi.fn(() => {
liveSubscriptions.delete(this)
})
Comment on lines +30 to +55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The live-subscription tracker has no failure case of its own. FE-2158 runs in order (unmount happens before the remount), so two bridges can only be subscribed at once if teardown skips bridge.destroy(). Two existing tests already fail on that. When I deleted () => bridge.destroy(), from the teardown list, "starts on flag delivery and stops synchronously on revocation" (line 343) and "tears down totally on unmount" (line 2232) both failed, with or without line 560. The fake also cannot represent one bridge subscribed to wf-a and wf-b, because subscribe replaces the bridge's entry, so every Set here holds at most one id. Making the fake's destroy inert does fail FE-2158, but that shows the tracker works, not that the composable does.

This restores the base FakeBridge. The matching edits are on lines 68-75 and 275 and inside the comment on 540-560 (line 560). They also remove the Map<object, Set<string>>, the arrow countLiveSubscriptions, and the comment that explains both.

Suggested change
const liveSubscriptions = new Map<object, Set<string>>()
let maxLiveSubscriptions = 0
const countLiveSubscriptions = () =>
[...liveSubscriptions.values()].reduce(
(count, workflows) => count + workflows.size,
0
)
class FakeBridge extends EventTarget {
subscribe = vi.fn()
unsubscribe = vi.fn()
subscribe = vi.fn((workflowId: string) => {
// A bridge retargets its single subscription; concurrency is measured
// across bridge instances, not across its historical workflow ids.
liveSubscriptions.set(this, new Set([workflowId]))
maxLiveSubscriptions = Math.max(
maxLiveSubscriptions,
countLiveSubscriptions()
)
})
unsubscribe = vi.fn(() => {
liveSubscriptions.delete(this)
})
resubscribe = vi.fn()
reconcile = vi.fn()
destroy = vi.fn()
destroy = vi.fn(() => {
liveSubscriptions.delete(this)
})
class FakeBridge extends EventTarget {
subscribe = vi.fn()
unsubscribe = vi.fn()
resubscribe = vi.fn()
reconcile = vi.fn()
destroy = vi.fn()

sendHumanOps = vi.fn()
subscribedWorkflowId: string | null = 'wf-1'
lastSequence = 41
Expand All @@ -45,7 +65,14 @@ const bridgeState = vi.hoisted(() => {
}
return {
FakeBridge,
current: null as InstanceType<typeof FakeBridge> | null
current: null as InstanceType<typeof FakeBridge> | null,
get maxLiveSubscriptions() {
return maxLiveSubscriptions
},
resetLiveSubscriptions() {
liveSubscriptions.clear()
maxLiveSubscriptions = 0
}
Comment on lines +68 to +75

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Part of removing the live-subscription tracker (comment on lines 30-55). Only line 275 and line 560 read these members.

Suggested change
current: null as InstanceType<typeof FakeBridge> | null,
get maxLiveSubscriptions() {
return maxLiveSubscriptions
},
resetLiveSubscriptions() {
liveSubscriptions.clear()
maxLiveSubscriptions = 0
}
current: null as InstanceType<typeof FakeBridge> | null

}
})

Expand Down Expand Up @@ -245,6 +272,7 @@ describe('useAgentCrdtFollower', () => {
useAgentPanelStore().enabled = true
sessionStorage.clear()
bridgeState.current = null
bridgeState.resetLiveSubscriptions()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Part of removing the live-subscription tracker (comment on lines 30-55).

Suggested change
bridgeState.resetLiveSubscriptions()

clientState.transport = null
projectionState.applyFrame
.mockReset()
Expand Down Expand Up @@ -480,6 +508,58 @@ describe('useAgentCrdtFollower', () => {
})
})

it('FE-2158: one follower retargets across a tab switch and releases across remount', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Once the unmount and remount phase goes (comment on lines 540-560), this test covers one thing no other test does: a follower whose tab went inactive on wf-a retargets to wf-b when wf-b becomes bound and active. The name should say that. The flag-revocation and total-teardown tests already cover "releases across remount".

Suggested change
it('FE-2158: one follower retargets across a tab switch and releases across remount', async () => {
it('FE-2158: retargets from a tab that went inactive to a newly active workflow', async () => {

// AgentPanelRoot.vue creates a SINGLE follower and drives it with the
// reactive boundWorkflowId / isBoundWorkflowActive. Exercising the switch
// as two mounted followers would leave a broken direct A-to-B retarget
// green, because no one follower would ever change workflow.
Comment on lines +512 to +515

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This comment argues for the test's design. That belongs in the PR description, and the code below doesn't need it. It also names AgentPanelRoot.vue refs that can be renamed without this test failing.

Suggested change
// AgentPanelRoot.vue creates a SINGLE follower and drives it with the
// reactive boundWorkflowId / isBoundWorkflowActive. Exercising the switch
// as two mounted followers would leave a broken direct A-to-B retarget
// green, because no one follower would ever change workflow.

const { unmount, workflowId, isTargetActive, status } =
mountFollower('wf-a')
expect(bridge().subscribe).toHaveBeenLastCalledWith('wf-a')
Comment on lines +516 to +518

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Line 518 repeats "subscribes immediately to a bound workflow and reports it in status" (line 406), and both go through the same immediate watcher. status is no longer read once the counter assertions at 531-532 and 538 go (suggestions below), so apply this together with those.

Suggested change
const { unmount, workflowId, isTargetActive, status } =
mountFollower('wf-a')
expect(bridge().subscribe).toHaveBeenLastCalledWith('wf-a')
const { unmount, workflowId, isTargetActive } = mountFollower('wf-a')


isTargetActive.value = false
await nextTick()

workflowId.value = 'wf-b'
isTargetActive.value = true
await nextTick()
expect(bridge().subscribe).toHaveBeenLastCalledWith('wf-b')
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// A frame for the workflow this follower just left must be counted and
// dropped, never applied to the newly bound graph.
Comment on lines +528 to +529

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The assertions already say this. It also describes a path that only exists with the fake. The real LayoutFollowerBridge drops a wf-a doc_update before re-dispatching it once its sent workflow is wf-b (layoutFollowerBridge.ts:267), so the composable never counts it in production. The test reaches the composable's own isCurrentWorkflow filter only because FakeBridge re-dispatches every frame.

Suggested change
// A frame for the workflow this follower just left must be counted and
// dropped, never applied to the newly bound graph.

dispatchFrame('doc_update', { workflowId: 'wf-a', seq: 99 })
expect(status().outcomes.applied).toBe(0)
expect(status().outcomes.skipped).toBeGreaterThan(0)
expect(projectionState.applyFrame).not.toHaveBeenCalled()

Comment on lines +531 to +534

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: None of these lines add anything once line 538 asserts which frame was applied (suggestion there). applied only increments after projection.applyFrame returns applied: true (useAgentCrdtFollower.ts:477-478), so 531 restates 533. skipped > 0 is a loose bound on a count that is exactly 1, and "counts received and skipped for a frame from an unsubscribed workflow" (line 956) already pins skip counting. Line 533 follows from toHaveBeenCalledExactlyOnceWith after both frames. Deleting the blank line puts the two dispatches together.

Suggested change
expect(status().outcomes.applied).toBe(0)
expect(status().outcomes.skipped).toBeGreaterThan(0)
expect(projectionState.applyFrame).not.toHaveBeenCalled()

// Control: the retarget really did bind B, so the filter above
// discriminated on workflow id rather than having gone silent.
Comment on lines +535 to +536

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: With one assertion on the applied frame (suggestion below), the code itself makes the control explicit.

Suggested change
// Control: the retarget really did bind B, so the filter above
// discriminated on workflow id rather than having gone silent.

dispatchFrame('doc_update', { workflowId: 'wf-b', seq: 1 })
expect(status().outcomes.applied).toBe(1)
expect(projectionState.applyFrame).toHaveBeenCalledTimes(1)
Comment on lines +538 to +539

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Both lines count the same event, and neither checks which frame was applied. I made rebindProjection return early once bound, which leaves the projection on wf-a. The wf-a frame then applies, wf-b is skipped, and these two lines still pass. Today only 531 and 533 catch that. One assertion on the frame catches it by itself. With every suggestion in this review applied, the file passes 89/89, and this test is the only one that fails for the three retarget mutations in the review body.

Suggested change
expect(status().outcomes.applied).toBe(1)
expect(projectionState.applyFrame).toHaveBeenCalledTimes(1)
expect(projectionState.applyFrame).toHaveBeenCalledExactlyOnceWith({
workflowId: 'wf-b',
seq: 1
})


const staleBridge = bridge()
unmount()
await nextTick()
expect(staleBridge.unsubscribe).toHaveBeenCalled()

staleBridge.dispatchEvent(
new CustomEvent('doc_update', {
detail: { workflowId: 'wf-b', seq: 2 }
})
)
expect(projectionState.applyFrame).toHaveBeenCalledTimes(1)

const remounted = mountFollower('wf-b')
expect(bridge()).not.toBe(staleBridge)
expect(bridge().subscribe).toHaveBeenCalledExactlyOnceWith('wf-b')

dispatchFrame('doc_update', { workflowId: 'wf-b', seq: 2 })
expect(projectionState.applyFrame).toHaveBeenCalledTimes(2)
remounted.unmount()
expect(bridgeState.maxLiveSubscriptions).toBeLessThanOrEqual(1)
Comment on lines +540 to +560

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Existing tests already catch everything this unmount and remount phase catches, and line 544 checks a different event than its position suggests.

  • Line 544: teardown never calls unsubscribe(). onScopeDispose calls bridge.destroy() (useAgentCrdtFollower.ts:819). The call this line sees comes from the inactive step at 520-521, through holdOpsForInactiveTab (useAgentCrdtFollower.ts:713). Deleting that unsubscribe() fails line 544. Deleting teardown's bridge.destroy() leaves it green.
  • Lines 546-558: removing the doc_update listener cleanup also fails "starts on flag delivery and stops synchronously on revocation" (line 362). That test already sends a late frame to the destroyed bridge and asserts that a fresh bridge subscribes exactly once (364-366). Component unmount and flag revocation both end in the same scope.stop().
  • Line 543: teardown is synchronous, so the await nextTick() waits for nothing. Line 554 follows from 555, because the old bridge's subscribe ran twice. Line 560 is the tracker assertion (comment on lines 30-55), and toBeLessThanOrEqual(1) would also pass at 0.
Suggested change
const staleBridge = bridge()
unmount()
await nextTick()
expect(staleBridge.unsubscribe).toHaveBeenCalled()
staleBridge.dispatchEvent(
new CustomEvent('doc_update', {
detail: { workflowId: 'wf-b', seq: 2 }
})
)
expect(projectionState.applyFrame).toHaveBeenCalledTimes(1)
const remounted = mountFollower('wf-b')
expect(bridge()).not.toBe(staleBridge)
expect(bridge().subscribe).toHaveBeenCalledExactlyOnceWith('wf-b')
dispatchFrame('doc_update', { workflowId: 'wf-b', seq: 2 })
expect(projectionState.applyFrame).toHaveBeenCalledTimes(2)
remounted.unmount()
expect(bridgeState.maxLiveSubscriptions).toBeLessThanOrEqual(1)
unmount()

})

it('FE-1902: persists a binding only once the server confirms it', () => {
const { unmount } = mountFollower('wf-1')
expect(persistedRecord()).toBeNull()
Expand Down
Loading