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:
Chris Scott committed 2026-10-15 10:18:06 -04:00
1 parent 74a2f6fb6e
commit 1b2665bd2b
5 files changed
+131 -26

No files matched your search

@@ -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 {