mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(background-activity): address review on resync scope and stale subscribe responses
A directory subscribed by the stream but neither registered as a repo nor remembered as a session directory was reconciled on subscribe but skipped on resync, so a missed shell exit could leave the indicator stale. Pass the stream's subscribed directories to onResync and reconcile their pending actions and running shells alongside the tracked ones. Subscribe responses that arrive after the connection was replaced or the directory was unsubscribed could also apply snapshots to state the client no longer owns. Ignore responses from a superseded connection and report only directories still subscribed. The stale-indicator e2e test now waits for the successful unsubscribe response and asserts the subscribe responses succeeded instead of sleeping.
This commit is contained in:
1 parent
74a2f6fb6e
commit
1b2665bd2b
5 files changed
+131
-26
No files matched your search
@@ -9,6 +9,23 @@ class RejectingSubscribeTransport extends TestEventStreamTransport {
|
||||
}
|
||||
}
|
||||
|
||||
class DeferredSubscribeTransport extends TestEventStreamTransport {
|
||||
private resolvers: Array<(value: boolean) => void> = []
|
||||
|
||||
override post(path: string, body: unknown): Promise<boolean> {
|
||||
this.posts.push({ path, body })
|
||||
return new Promise<boolean>((resolve) => {
|
||||
this.resolvers.push(resolve)
|
||||
})
|
||||
}
|
||||
|
||||
resolvePosts(success = true): void {
|
||||
const resolvers = this.resolvers
|
||||
this.resolvers = []
|
||||
resolvers.forEach((resolve) => resolve(success))
|
||||
}
|
||||
}
|
||||
|
||||
describe('OpenCodeEventStream', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
@@ -176,6 +193,7 @@ describe('OpenCodeEventStream', () => {
|
||||
transport.resync()
|
||||
|
||||
expect(onResync).toHaveBeenCalledTimes(1)
|
||||
expect(onResync).toHaveBeenCalledWith(['/repo'])
|
||||
})
|
||||
|
||||
it('reports visibility through the transport adapter', async () => {
|
||||
@@ -242,4 +260,51 @@ describe('OpenCodeEventStream', () => {
|
||||
|
||||
expect(onDirectoriesSubscribed).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('ignores a subscribe response that resolves after the stream reconnected', async () => {
|
||||
const transport = new DeferredSubscribeTransport()
|
||||
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(transport.posts).toContainEqual({
|
||||
path: '/api/sse/subscribe',
|
||||
body: { clientId: 'client-1', directories: ['/repo'] },
|
||||
})
|
||||
|
||||
transport.fail()
|
||||
transport.openConnection()
|
||||
transport.connected('client-2')
|
||||
|
||||
transport.resolvePosts()
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(onDirectoriesSubscribed).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not notify a directory removed while its subscribe request is in flight', async () => {
|
||||
const transport = new DeferredSubscribeTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
const onDirectoriesSubscribed = vi.fn()
|
||||
|
||||
const subscription = stream.subscribeGlobalMonitor({
|
||||
directories: ['/repo'],
|
||||
onEvent: vi.fn(),
|
||||
onDirectoriesSubscribed,
|
||||
})
|
||||
transport.openConnection()
|
||||
transport.connected('client-1')
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
subscription.updateDirectories([])
|
||||
|
||||
transport.resolvePosts()
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
|
||||
expect(onDirectoriesSubscribed).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -14,7 +14,7 @@ interface Subscriber {
|
||||
onEvent: OpenCodeEventHandler
|
||||
onStatusChange?: EventStreamStatusHandler
|
||||
onHealthChange?: (state: EventStreamHealthState) => void
|
||||
onResync?: () => void
|
||||
onResync?: (subscribedDirectories: string[]) => void
|
||||
onDirectoriesSubscribed?: (directories: string[]) => void
|
||||
directories: Set<string>
|
||||
}
|
||||
@@ -24,7 +24,7 @@ interface SubscriberInput {
|
||||
onStatusChange?: EventStreamStatusHandler
|
||||
onHealthChange?: (state: EventStreamHealthState) => void
|
||||
directories: string[]
|
||||
onResync?: () => void
|
||||
onResync?: (subscribedDirectories: string[]) => void
|
||||
onDirectoriesSubscribed?: (directories: string[]) => void
|
||||
}
|
||||
|
||||
@@ -400,9 +400,10 @@ export class OpenCodeEventStream {
|
||||
|
||||
private handleResync(): void {
|
||||
this.markActivity()
|
||||
const directories = Array.from(this.directoryRefCounts.keys())
|
||||
this.subscribers.forEach((subscriber) => {
|
||||
try {
|
||||
subscriber.onResync?.()
|
||||
subscriber.onResync?.(directories)
|
||||
} catch {
|
||||
void 0
|
||||
}
|
||||
@@ -422,18 +423,25 @@ export class OpenCodeEventStream {
|
||||
private async subscribeToRemoteDirectories(directories: string[]): Promise<void> {
|
||||
if (!this.clientId || directories.length === 0) return
|
||||
|
||||
const clientId = this.clientId
|
||||
|
||||
const success = await this.transport.post('/api/sse/subscribe', {
|
||||
clientId: this.clientId,
|
||||
clientId,
|
||||
directories,
|
||||
})
|
||||
|
||||
if (this.clientId !== clientId) return
|
||||
|
||||
if (!success) {
|
||||
directories.forEach((directory) => this.pendingDirectories.add(directory))
|
||||
this.reconnect()
|
||||
return
|
||||
}
|
||||
|
||||
this.notifyDirectoriesSubscribed(directories)
|
||||
const subscribedDirectories = directories.filter((directory) => this.directoryRefCounts.has(directory))
|
||||
if (subscribedDirectories.length === 0) return
|
||||
|
||||
this.notifyDirectoriesSubscribed(subscribedDirectories)
|
||||
}
|
||||
|
||||
private notifyDirectoriesSubscribed(directories: string[]): void {
|
||||
|
||||
Reference in new issue
Block a user