Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(relay): the unread-dot signature hook follows a reconnect that leaves its signature unchanged

reviewd798acb05630
diff --git a/src/renderer/session/agent-status-lookup.test.tsx b/src/renderer/session/agent-status-lookup.test.tsx
index bef81823b..ba8863c6b 100644
--- a/src/renderer/session/agent-status-lookup.test.tsx
+++ b/src/renderer/session/agent-status-lookup.test.tsx
@@ -13,6 +13,7 @@ import { resolve } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { NodeTerminalApi, Project } from '@shared/types'
import {
+ agentStatusForProject,
bindProjectToSession,
createSession,
disposeSession,
@@ -140,6 +141,42 @@ describe('useAgentStatusLookup', () => {
expect(host.textContent).toBe('unread')
expect(renders).toBe(settled + 1)
})
+ it('useAgentStatusSig re-subscribes after a reconnect that leaves the signature unchanged', () => {
+ // The common case: a team tab with nothing unread reconnects ('read' -> 'read'), or an offline
+ // remote tab binds. The signature does not move, so nothing re-renders, and the hook must still
+ // follow the project to its NEW store — a re-subscribe that waited for a render never happened,
+ // and the tab dot stayed dark for every later unread.
+ let renders = 0
+ function Probe() {
+ renders++
+ const sig = useAgentStatusSig([TEAM_PROJECT], (lookup) => (lookup(TEAM_PROJECT, NODE)?.unread ? 'unread' : 'read'))
+ return <span>{sig}</span>
+ }
+ // Unbound remote tab: the offline session's empty store.
+ act(() => root.render(<Probe />))
+ expect(host.textContent).toBe('read')
+
+ // It binds: same signature, no re-render — and the relay store is the one listened to.
+ let first = ''
+ act(() => {
+ first = joinTeam()
+ })
+ expect(host.textContent).toBe('read')
+ act(() => relayStore(first).getState().markUnread(NODE))
+ expect(host.textContent).toBe('unread')
+ act(() => relayStore(first).getState().clearUnread(NODE))
+ expect(host.textContent).toBe('read')
+
+ // A reconnect: a new session (new api, new store) takes the tab, the old one is disposed.
+ const settled = renders
+ act(() => {
+ joinTeam()
+ disposeSession(first)
+ })
+ expect(renders, 'an unchanged signature re-renders nothing').toBe(settled)
+ act(() => agentStatusForProject(TEAM_PROJECT).store.getState().markUnread(NODE))
+ expect(host.textContent).toBe('unread')
+ })
})
describe('unreadSigFor', () => {
diff --git a/src/renderer/session/agent-status-lookup.ts b/src/renderer/session/agent-status-lookup.ts
index 874bcefb5..3e7a33c44 100644
--- a/src/renderer/session/agent-status-lookup.ts
+++ b/src/renderer/session/agent-status-lookup.ts
@@ -68,39 +68,61 @@ function sameKey(a: readonly unknown[], b: readonly unknown[]): boolean {
return true
}
+/** The distinct stores `projectIds` resolve to right now, and a key for that set. */
+function resolveDistinct(projectIds: readonly string[]): { stores: StatusStore[]; key: string } {
+ const stores = [...new Set(projectIds.map(storeFor))]
+ return { stores, key: stores.map(storeId).join(',') }
+}
+
/**
* The shared source behind both hooks: one subscription to every DISTINCT store the listed
* projects resolve to plus the session registry signal, and a snapshot lookup memoized on the
* tuple (project ids, registry version, each project's `byId` reference).
*
- * Re-resolution: a registry change (a reconnect swaps a project's session, an unbind sends it to
- * the offline session) notifies the subscriber; the snapshot re-resolves and changes, the caller
- * re-renders, the render resolves the NEW store set, and its changed key re-subscribes. A
- * resolution that moves only because the projects store changed (`remote` read by
- * `agentStatusForProject`) is picked up the same way on the caller's next render — every caller
- * holds the projects it passes, so it re-renders on them.
+ * Re-subscription happens on the SUBSCRIBE side, never by waiting for a render: a registry change
+ * (a reconnect swaps a project's session, a bind takes a remote tab off the offline session, an
+ * unbind sends it back) re-resolves the store set inside the registry listener, moves the store
+ * subscriptions to the new set, and only then notifies. `useAgentStatusSig` depends on that: a
+ * reconnect of a tab with nothing unread leaves its signature unchanged, so React re-renders
+ * nothing, and a re-subscribe that waited for the render would have kept listening to the dead
+ * store. A resolution that moves only because the projects store changed (`remote` read by
+ * `agentStatusForProject`) is picked up on the caller's next render instead — every caller holds
+ * the projects it passes, so it re-renders on them, and the render's store-set key re-subscribes.
*/
function useLookupSource(projectIds: readonly string[]): {
subscribe: (onChange: () => void) => () => void
getLookup: () => AgentStatusLookup
} {
- const distinct = [...new Set(projectIds.map(storeFor))]
- const storesKey = distinct.map(storeId).join(',')
- // The callback closes over THIS render's distinct stores; its identity changes exactly when the
- // set does, which is what makes React re-subscribe.
+ const idsRef = useRef(projectIds)
+ idsRef.current = projectIds
+ // The render's view of the store set: its key changes the callback (and so re-subscribes) when a
+ // render resolves a different set than the one subscribed — the projects-store case above.
+ const storesKey = resolveDistinct(projectIds).key
const subscribe = useCallback(
(onChange: () => void) => {
- const offs = distinct.map((store) => store.subscribe(onChange))
- offs.push(subscribeSessionRegistry(onChange))
+ let attachedKey: string | null = null
+ let offStores: Array<() => void> = []
+ const attach = (): void => {
+ const { stores, key } = resolveDistinct(idsRef.current)
+ if (key === attachedKey) return
+ for (const off of offStores) off()
+ offStores = stores.map((store) => store.subscribe(onChange))
+ attachedKey = key
+ }
+ attach()
+ const offRegistry = subscribeSessionRegistry(() => {
+ attach()
+ onChange()
+ })
return () => {
- for (const off of offs) off()
+ offRegistry()
+ for (const off of offStores) off()
+ offStores = []
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[storesKey]
)
- const idsRef = useRef(projectIds)
- idsRef.current = projectIds
const cache = useRef<Snapshot | null>(null)
const getLookup = useCallback((): AgentStatusLookup => {
const ids = idsRef.current