Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r8-l11 reconnect half-open entity streams
Josué5b06c5b1cf54
diff --git a/desktop-app/src/__tests__/appService.entityChanges.test.ts b/desktop-app/src/__tests__/appService.entityChanges.test.tsindex 5ad0cded7..9311a88ef 100644--- a/desktop-app/src/__tests__/appService.entityChanges.test.ts+++ b/desktop-app/src/__tests__/appService.entityChanges.test.ts@@ -258,6 +258,58 @@ describe('AppService.startEntityChangeStream session expiry', () => {}})+ it('reconnects a half-open stream after the idle deadline without invalidating entities', async () => {+ vi.useFakeTimers()+ vi.spyOn(Math, 'random').mockReturnValue(0)+ const service = new AppService() as any+ setSyntheticSessionToken(service, 'session-token')+ const attempts: Array<{+ onEvent: (event: EntityChangeStreamEvent) => void+ signal: AbortSignal+ }> = []+ service.authClient = {+ openEntityChangeStream: vi.fn((_token, _cursor, onEvent, signal) => {+ attempts.push({ onEvent, signal })+ return new Promise<void>(resolve => {+ signal.addEventListener('abort', () => resolve(), { once: true })+ })+ }),+ }+ const events: EntityChangeStreamEvent[] = []++ try {+ service.startEntityChangeStream('stream-1', 7, (event: EntityChangeStreamEvent) => {+ events.push(event)+ })+ await flushAsyncWork()+ expect(attempts).toHaveLength(1)++ await vi.advanceTimersByTimeAsync(100_000)+ expect(attempts[0]?.signal.aborted).toBe(false)+ attempts[0]?.onEvent({+ schemaVersion: 1,+ type: 'heartbeat',+ cursor: '00000000-0000-0000-0000-000000000000',+ observedAt: '2026-09-30T00:00:00.000Z',+ })+ await vi.advanceTimersByTimeAsync(129_999)+ expect(attempts[0]?.signal.aborted).toBe(false)+ attempts[0]?.onEvent({ type: 'open' })+ await vi.advanceTimersByTimeAsync(1)+ expect(attempts[0]?.signal.aborted).toBe(true)+ expect(events.map(event => event.type)).not.toContain('scope.invalidated')+ expect(events.map(event => event.type)).not.toContain('resync_required')++ await vi.advanceTimersByTimeAsync(1_000)+ await flushAsyncWork()+ expect(attempts).toHaveLength(2)+ } finally {+ service.stopEntityChangeStream('stream-1', 7)+ vi.useRealTimers()+ vi.restoreAllMocks()+ }+ })+it('turns an initial 401 into a terminal session-expired frame without reconnecting', async () => {const service = new AppService() as anysetSyntheticSessionToken(service, 'session-token')diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex f20d321c0..5ece11616 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -94,6 +94,9 @@ import {// makes the failure feel slower than it already did.const BACKEND_PROBE_TIMEOUT_MS = 1500const ENTITY_CHANGE_RETRY_AFTER_CAP_MS = 5 * 60 * 1000+// Two maximum server heartbeat intervals plus its maximum poll delay. This+// matches the operator-stream liveness watchdog and bounds half-open sockets.+const ENTITY_CHANGE_STREAM_IDLE_TIMEOUT_MS = 2 * (60_000 + 5_000)const HOST_WAKE_SCOPE: RpcScope = 'host:wake:write'const PROFILE_UI_BASE_URL_ORIGIN_ERROR =@@ -2968,16 +2971,29 @@ export class AppService {let closed = falselet abortController: AbortController | null = nulllet retryTimer: NodeJS.Timeout | null = null+ let idleTimer: NodeJS.Timeout | null = nulllet backoffMs = 1000let serverRetryAfterMs: number | undefinedconst clearRetry = () => {if (retryTimer) clearTimeout(retryTimer)retryTimer = null}+ const clearIdleTimer = () => {+ if (idleTimer) clearTimeout(idleTimer)+ idleTimer = null+ }+ const armIdleTimer = (attemptController: AbortController) => {+ clearIdleTimer()+ idleTimer = setTimeout(() => {+ idleTimer = null+ if (!closed && abortController === attemptController) attemptController.abort()+ }, ENTITY_CHANGE_STREAM_IDLE_TIMEOUT_MS)+ }const stop = (opts?: { silent?: boolean }) => {if (closed) returnclosed = trueclearRetry()+ clearIdleTimer()abortController?.abort()if (this.entityChangeConnectionStop === stop) this.entityChangeConnectionStop = nullif (!opts?.silent) this.emitEntityChangeEvent({ type: 'closed' })@@ -2989,7 +3005,9 @@ export class AppService {const connectionToken = this.entityChangeSessionTokenconst connectionGeneration = this.entityChangeSessionGenerationif (!connectionToken) return- abortController = new AbortController()+ const attemptController = new AbortController()+ abortController = attemptController+ armIdleTimer(attemptController)try {await this.authClient.openEntityChangeStream(connectionToken,@@ -3002,6 +3020,7 @@ export class AppService {event.type === 'resync_required' ||event.type === 'stream.closing') {+ armIdleTimer(attemptController)// Transport-open is synthetic: it proves only that fetch// returned headers. Reset retry pressure only after the server// produces a validated schema-v1 frame.@@ -3013,7 +3032,7 @@ export class AppService {}this.emitEntityChangeEvent(event)},- abortController.signal+ attemptController.signal)} catch (error) {if (!closed) {@@ -3032,6 +3051,7 @@ export class AppService {}} finally {if (closed) return+ clearIdleTimer()clearRetry()const localBackoffMs = backoffMs + Math.floor(Math.random() * Math.max(1, backoffMs * 0.2))const delay = Math.max(localBackoffMs, serverRetryAfterMs ?? 0)diff --git a/docs/architecture/live-entity-change-consumer-semantics.md b/docs/architecture/live-entity-change-consumer-semantics.mdindex bdc66f396..47b287946 100644--- a/docs/architecture/live-entity-change-consumer-semantics.md+++ b/docs/architecture/live-entity-change-consumer-semantics.md@@ -7,6 +7,7 @@ may coalesce intermediate changes while disconnected or under load.| Input | Desktop | Control UI | Required visible result || --- | --- | --- | --- || `heartbeat` | Update transport liveness only. | Update transport liveness only. | No query, list, selection, dialog, or preview mutation. |+| Silent/half-open stream | Abort an idle connection after 130 seconds without a validated server frame, then use bounded reconnect/backoff. | Use the same established 130-second operator watchdog. | Watchdog is transport-only; synthetic open does not reset it and expiry never invalidates entity state. || User `scope.invalidated` | Softly revalidate active GFS state and open resources. | Use soft semantics if received. | Keep loaded pages and preview bytes until authoritative reads establish a change or denial. || Operator `scope.invalidated` | Use the soft path if received. | Coalesce a bounded burst into one active and one trailing authoritative pass. | Preserve rows, selection, dialogs, errors, and unchanged previews while reads run. || User navigation or pagination during refresh | Foreground work wins; background work cannot replace its destination. | Bind results to location, operation, cursor, and generation. Defer a pending scope refresh across load-more. | A stale page never appears in another folder; the pending refresh eventually runs. |