mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
refactor(sse): harden SSE with queue-based writer, backpressure, and directory-aware batching
- Replace promise-chaining SSE writer with queue-based pump with MAX_QUEUED_FRAMES backpressure - Make frontend parts batcher directory-aware with (sessionID, directory) composite key - Add session content versioning for reliable content change detection - Add TOOLS_CACHEBUST arg for selective Docker cache invalidation - Add docker-upgrade.sh modes (cached/tools/full) - Fix plugin install timeout and spawn error handling - Fix assistant session endpoint path and ordering params
This commit is contained in:
1 parent
fdeffeaab7
commit
12b7387e11
25 files changed
+1632
-181
No files matched your search
@@ -35,6 +35,8 @@ export class OpenCodeEventStream {
|
||||
private clientId: string | null = null
|
||||
private lastEventAt: number | null = null
|
||||
private watchdogTimer: ReturnType<typeof setInterval> | null = null
|
||||
private upstreamConnectedCount: number | null = null
|
||||
private upstreamTotalCount: number | null = null
|
||||
|
||||
constructor(options: OpenCodeEventStreamOptions = {}) {
|
||||
this.transport = options.transport ?? createBrowserEventStreamTransport()
|
||||
@@ -46,8 +48,7 @@ export class OpenCodeEventStream {
|
||||
onStatusChange?: EventStreamStatusHandler
|
||||
onHealthChange?: (state: EventStreamHealthState) => void
|
||||
}): GlobalMonitorSubscription {
|
||||
const id = this.addSubscriber(input.onEvent, input.onStatusChange, input.onHealthChange)
|
||||
this.updateSubscriberDirectories(id, input.directories)
|
||||
const id = this.addSubscriber(input.onEvent, input.onStatusChange, input.onHealthChange, input.directories)
|
||||
|
||||
return {
|
||||
updateDirectories: (directories) => this.updateSubscriberDirectories(id, directories),
|
||||
@@ -65,14 +66,31 @@ export class OpenCodeEventStream {
|
||||
onEvent: OpenCodeEventHandler,
|
||||
onStatusChange?: EventStreamStatusHandler,
|
||||
onHealthChange?: (state: EventStreamHealthState) => void,
|
||||
directories: string[] = [],
|
||||
): string {
|
||||
const id = `sub_${++this.subscriberIdCounter}`
|
||||
|
||||
const initialDirectories = new Set(directories.filter(Boolean))
|
||||
const newDirectories: string[] = []
|
||||
|
||||
for (const directory of initialDirectories) {
|
||||
const currentCount = this.directoryRefCounts.get(directory) ?? 0
|
||||
this.directoryRefCounts.set(directory, currentCount + 1)
|
||||
if (currentCount === 0) {
|
||||
if (this.clientId && this.connected) {
|
||||
newDirectories.push(directory)
|
||||
} else {
|
||||
this.pendingDirectories.add(directory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.subscribers.set(id, {
|
||||
id,
|
||||
onEvent,
|
||||
onStatusChange,
|
||||
onHealthChange,
|
||||
directories: new Set(),
|
||||
directories: initialDirectories,
|
||||
})
|
||||
|
||||
onStatusChange?.(this.connected)
|
||||
@@ -80,6 +98,8 @@ export class OpenCodeEventStream {
|
||||
|
||||
if (this.subscribers.size === 1) {
|
||||
this.connect()
|
||||
} else if (newDirectories.length > 0) {
|
||||
void this.subscribeToRemoteDirectories(newDirectories)
|
||||
}
|
||||
|
||||
return id
|
||||
@@ -186,6 +206,8 @@ export class OpenCodeEventStream {
|
||||
private handleError(): void {
|
||||
this.connected = false
|
||||
this.clientId = null
|
||||
this.upstreamConnectedCount = null
|
||||
this.upstreamTotalCount = null
|
||||
this.stopWatchdog()
|
||||
this.lastEventAt = null
|
||||
|
||||
@@ -205,7 +227,7 @@ export class OpenCodeEventStream {
|
||||
private handleMessage(data: string): void {
|
||||
try {
|
||||
this.markActivity()
|
||||
this.broadcast(JSON.parse(data))
|
||||
this.broadcast(flattenEventEnvelope(JSON.parse(data)))
|
||||
} catch {
|
||||
this.markActivity()
|
||||
}
|
||||
@@ -213,10 +235,16 @@ export class OpenCodeEventStream {
|
||||
|
||||
private handleConnected(data: string): void {
|
||||
try {
|
||||
const parsed = JSON.parse(data) as { clientId?: unknown }
|
||||
const parsed = JSON.parse(data) as { clientId?: unknown; connected?: unknown; total?: unknown }
|
||||
if (typeof parsed.clientId === 'string') {
|
||||
this.clientId = parsed.clientId
|
||||
}
|
||||
if (typeof parsed.connected === 'number') {
|
||||
this.upstreamConnectedCount = parsed.connected
|
||||
}
|
||||
if (typeof parsed.total === 'number') {
|
||||
this.upstreamTotalCount = parsed.total
|
||||
}
|
||||
} catch {
|
||||
this.clientId = null
|
||||
}
|
||||
@@ -239,6 +267,8 @@ export class OpenCodeEventStream {
|
||||
this.connection = null
|
||||
this.connected = false
|
||||
this.clientId = null
|
||||
this.upstreamConnectedCount = null
|
||||
this.upstreamTotalCount = null
|
||||
this.lastEventAt = null
|
||||
this.pendingDirectories.clear()
|
||||
this.notifyHealth()
|
||||
@@ -263,6 +293,8 @@ export class OpenCodeEventStream {
|
||||
this.connection = null
|
||||
this.connected = false
|
||||
this.clientId = null
|
||||
this.upstreamConnectedCount = null
|
||||
this.upstreamTotalCount = null
|
||||
this.lastEventAt = null
|
||||
this.pendingDirectories = new Set(this.directoryRefCounts.keys())
|
||||
this.notifyStatusChange(false)
|
||||
@@ -308,9 +340,10 @@ export class OpenCodeEventStream {
|
||||
|
||||
private buildHealth(): EventStreamHealthState {
|
||||
const isStalled = this.connected && this.lastEventAt != null && Date.now() - this.lastEventAt > STALL_THRESHOLD_MS
|
||||
const upstreamDisconnected = this.upstreamTotalCount != null && this.upstreamTotalCount > 0 && this.upstreamConnectedCount === 0
|
||||
return {
|
||||
isConnected: this.connected,
|
||||
isHealthy: this.connected && this.lastEventAt != null && !isStalled,
|
||||
isHealthy: this.connected && this.lastEventAt != null && !isStalled && !upstreamDisconnected,
|
||||
lastEventAt: this.lastEventAt,
|
||||
isStalled,
|
||||
}
|
||||
@@ -381,4 +414,18 @@ export class OpenCodeEventStream {
|
||||
}
|
||||
}
|
||||
|
||||
function flattenEventEnvelope(parsed: unknown): unknown {
|
||||
if (
|
||||
parsed !== null &&
|
||||
typeof parsed === 'object' &&
|
||||
'payload' in parsed &&
|
||||
(parsed as { payload: unknown }).payload !== null &&
|
||||
typeof (parsed as { payload: unknown }).payload === 'object'
|
||||
) {
|
||||
const { payload, directory } = parsed as { payload: object; directory?: unknown }
|
||||
return { ...payload, directory }
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
export const openCodeEventStream = new OpenCodeEventStream()
|
||||
Reference in new issue
Block a user