fix: hold the upstream resync until the OpenCode proxy accepts requests (OM-179)

This commit is contained in:
Chris Scott committed 2026-11-03 00:06:39 -05:00
1 parent 9fca96ecea
commit 3e6ebbd1dc
6 files changed
+103 -1

No files matched your search

@@ -303,6 +303,7 @@ class OpenCodeServerManager {
private openCodeClient: OpenCodeClient | null = null
private sandboxEnforced: boolean = false
private lifecycleInitialized: boolean = false
private readonly lifecycleOpenedListeners = new Set<() => void>()
private markerRefreshTimer: ReturnType<typeof setInterval> | null = null
private processGeneration = 0
private intentionalStop: { pid: number; generation: number } | null = null
@@ -1037,7 +1038,22 @@ class OpenCodeServerManager {
}
setLifecycleInitialized(initialized: boolean): void {
const opened = initialized && !this.lifecycleInitialized
this.lifecycleInitialized = initialized
if (!opened) return
this.lifecycleOpenedListeners.forEach((listener) => {
try {
listener()
} catch (error) {
logger.error('OpenCode lifecycle listener failed:', error)
}
})
}
/** Calls `listener` each time the lifecycle gate opens, i.e. the proxy starts accepting requests. */
onLifecycleInitialized(listener: () => void): () => void {
this.lifecycleOpenedListeners.add(listener)
return () => { this.lifecycleOpenedListeners.delete(listener) }
}
isLifecycleInitialized(): boolean {
+25 -1
View File
@@ -65,6 +65,8 @@ class SSEAggregator {
private openCodeClient: Pick<OpenCodeClient, 'api'> | null = null
private scheduledSessionsResolver: (() => ScheduledSessionRef[]) | null = null
private remoteReplayHandler: RemoteReplayHandler | null = null
private isResyncGateOpen: () => boolean = () => true
private resyncPending = false
private readonly buildReplayEvent: ReplayEventBuilder = createReplayEventBuilder('ocm_replay_')
private constructor() {}
@@ -81,6 +83,22 @@ class SSEAggregator {
this.remoteReplayHandler = handler
}
/**
* Holds the upstream `resync` while the gate is closed, so clients do not re-read OpenCode
* state through the proxy before it accepts requests. Call {@link releasePendingResync} when
* the gate opens.
*/
setResyncGate(isOpen: () => boolean): void {
this.isResyncGateOpen = isOpen
}
/** Broadcasts a `resync` held by the gate while the upstream stream is still connected. */
releasePendingResync(): void {
if (!this.resyncPending || !this.upstreamConnected) return
this.resyncPending = false
this.broadcastResync()
}
reconnect(): void {
if (!this.started) return
logger.info('SSE forcing upstream reconnect (auth changed)')
@@ -113,6 +131,7 @@ class SSEAggregator {
},
onDisconnect: (error) => {
this.upstreamConnected = false
this.resyncPending = false
logger.warn(`SSE upstream disconnected: ${String(error)}`)
},
})
@@ -357,7 +376,11 @@ class SSEAggregator {
this.upstreamConnected = true
this.everConnected = true
this.notifyUpstreamConnected()
this.broadcastResync()
if (this.isResyncGateOpen()) {
this.broadcastResync()
} else {
this.resyncPending = true
}
if (wasConnectedBefore) {
void this.replaySessionStatusesForTrackedDirectories()
}
@@ -675,6 +698,7 @@ class SSEAggregator {
this.subscription?.close()
this.subscription = null
this.upstreamConnected = false
this.resyncPending = false
this.activeSessions.clear()
this.sessionDirectories.clear()