fix(background-activity): reconcile shells for directories subscribed by the stream

The background activity indicator went stale for sessions living in a
directory that is not a registered repo. Running shells were only synced
for repo directories derived from the repos query, so session directories
subscribed over the event stream were never reconciled, leaving the
indicator visible after the shell had exited.

Report the directories actually subscribed (mid-connection updates and
pending directories flushed after connect) from the event stream through
onDirectoriesSubscribed, and reconcile running shells and pending
permissions/forms for them in the event context.
This commit is contained in:
Chris Scott committed 2026-10-15 13:00:08 +00:00
1 parent 59ffe6c586
commit 37b4ab89f0
5 files changed
+232 -29

No files matched your search

@@ -2,6 +2,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { OpenCodeEventStream, TestEventStreamTransport } from '..'
import type { EventStreamHealthState } from '..'
class RejectingSubscribeTransport extends TestEventStreamTransport {
override async post(path: string, body: unknown): Promise<boolean> {
this.posts.push({ path, body })
return false
}
}
describe('OpenCodeEventStream', () => {
beforeEach(() => {
vi.useFakeTimers()
@@ -186,4 +193,53 @@ describe('OpenCodeEventStream', () => {
body: { clientId: 'client-1', visible: true, activeSessionId: 'session-1' },
})
})
it('notifies the global monitor with directories subscribed mid-connection', async () => {
const transport = new TestEventStreamTransport()
const stream = new OpenCodeEventStream({ transport })
const onDirectoriesSubscribed = vi.fn()
const subscription = stream.subscribeGlobalMonitor({
directories: ['/repo-a'],
onEvent: vi.fn(),
onDirectoriesSubscribed,
})
transport.openConnection()
transport.connected('client-1')
await vi.advanceTimersByTimeAsync(0)
onDirectoriesSubscribed.mockClear()
subscription.updateDirectories(['/repo-a', '/repo-b'])
await vi.advanceTimersByTimeAsync(0)
expect(onDirectoriesSubscribed).toHaveBeenCalledWith(['/repo-b'])
})
it('notifies the global monitor for pending directories flushed after connect', async () => {
const transport = new TestEventStreamTransport()
const stream = new OpenCodeEventStream({ transport })
const onDirectoriesSubscribed = vi.fn()
stream.subscribeGlobalMonitor({ directories: ['/repo'], onEvent: vi.fn(), onDirectoriesSubscribed })
expect(onDirectoriesSubscribed).not.toHaveBeenCalled()
transport.openConnection()
transport.connected('client-1')
await vi.advanceTimersByTimeAsync(0)
expect(onDirectoriesSubscribed).toHaveBeenCalledWith(['/repo'])
})
it('does not notify the global monitor when the subscribe request fails', async () => {
const transport = new RejectingSubscribeTransport()
const stream = new OpenCodeEventStream({ transport })
const onDirectoriesSubscribed = vi.fn()
stream.subscribeGlobalMonitor({ directories: ['/repo'], onEvent: vi.fn(), onDirectoriesSubscribed })
transport.openConnection()
transport.connected('client-1')
await vi.advanceTimersByTimeAsync(0)
expect(onDirectoriesSubscribed).not.toHaveBeenCalled()
})
})