Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): r8-l12 fence stream during environment changes

Josué7281d1e084a6
diff --git a/desktop-app/src/__tests__/appService.entityChanges.test.ts b/desktop-app/src/__tests__/appService.entityChanges.test.ts
index 6b69b6d5e..f99bdfa62 100644
--- a/desktop-app/src/__tests__/appService.entityChanges.test.ts
+++ b/desktop-app/src/__tests__/appService.entityChanges.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import { AppService } from '../appService.js'
+import { config } from '../config.js'
import { ApiError } from '../httpClient.js'
import type { EntityChangeStreamEvent } from '../types.js'
@@ -157,6 +158,59 @@ describe('AppService entity-change fan-out', () => {
})
describe('AppService.startEntityChangeStream session expiry', () => {
+ it('does not retry an old committed token against a changing environment', async () => {
+ vi.useFakeTimers()
+ vi.spyOn(Math, 'random').mockReturnValue(0)
+ const originalBaseUrl = config.externalRestApiBaseUrl
+ const environmentA = 'https://environment-a.example'
+ const environmentB = 'https://environment-b.example'
+ config.externalRestApiBaseUrl = environmentA
+ const service = new AppService() as any
+ service.sessionToken = 'environment-a-committed-token'
+ service.me = { id: 'user-1', teamId: 'team-1' }
+ setSyntheticSessionToken(service, 'environment-a-committed-token')
+ service.beginPrewarmAuthTransition = () => () => undefined
+ service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn().mockResolvedValue(undefined)
+ service.activateGfsAuthScope = vi.fn()
+ service.stopAllStreams = vi.fn()
+ service.tokenStore = { clearSessionToken: vi.fn().mockResolvedValue(undefined) }
+ const opened: Array<{ token: string; baseUrl: string }> = []
+ service.authClient = {
+ openEntityChangeStream: vi.fn(async (token: string) => {
+ opened.push({ token, baseUrl: config.externalRestApiBaseUrl })
+ throw new Error('temporary connection failure')
+ }),
+ }
+
+ let finishPersistence!: () => void
+ try {
+ service.startEntityChangeStream('stream-1', 7, vi.fn())
+ await flushAsyncWork()
+ expect(opened).toEqual([{ token: 'environment-a-committed-token', baseUrl: environmentA }])
+
+ const switching = service.applyRuntimeEnvironmentChange(async () => {
+ config.externalRestApiBaseUrl = environmentB
+ await new Promise<void>(resolve => {
+ finishPersistence = resolve
+ })
+ })
+ await flushAsyncWork()
+ await vi.advanceTimersByTimeAsync(1_000)
+ await flushAsyncWork()
+
+ expect(opened).toHaveLength(1)
+ finishPersistence()
+ await switching
+ expect(service.sessionToken).toBeNull()
+ expect(opened).toHaveLength(1)
+ } finally {
+ config.externalRestApiBaseUrl = originalBaseUrl
+ service.stopEntityChangeStream('stream-1', 7)
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+ }
+ })
+
it('does not reset reconnect backoff on synthetic transport-open callbacks', async () => {
vi.useFakeTimers()
vi.spyOn(Math, 'random').mockReturnValue(0)
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 1d336b8cc..f21e933b0 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -959,6 +959,7 @@ export class AppService {
private entityChangeConnectionStop: ((opts?: { silent?: boolean }) => void) | null = null
private entityChangeSessionToken: string | null = null
private entityChangeSessionGeneration = 0
+ private entityChangeEnvironmentSwitching = false
private entityChangeSessionExpiryDeferred = false
private entityChangeCursor: string | null = null
private progressStreams = new Map<
@@ -1667,12 +1668,14 @@ export class AppService {
}
private async applyRuntimeEnvironmentChange(operation: () => Promise<void>): Promise<void> {
+ const oldEnvKey = getActiveEnvKey()
+ const oldBaseUrl = normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
+ const oldLegacyEnvKeys = getActiveLegacyEnvKeys()
const releasePrewarm = this.beginPrewarmAuthTransition()
try {
- const oldEnvKey = getActiveEnvKey()
- const oldBaseUrl = normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
- const oldLegacyEnvKeys = getActiveLegacyEnvKeys()
const hadAuthenticatedScope = Boolean(this.sessionToken && this.me)
+ this.entityChangeEnvironmentSwitching = true
+ this.entityChangeConnectionStop?.({ silent: true })
// Invalidate a restore that may still be awaiting keychain/getMe before it
// can bind an old-environment token to the newly selected runtime.
this.sessionGeneration += 1
@@ -1680,7 +1683,11 @@ export class AppService {
try {
await operation()
} catch (error) {
- if (hadAuthenticatedScope && this.sessionToken && this.me) this.activateGfsAuthScope()
+ const environmentUnchanged =
+ getActiveEnvKey() === oldEnvKey &&
+ normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) === oldBaseUrl
+ if (!environmentUnchanged) this.clearAuthenticatedSessionState()
+ else if (hadAuthenticatedScope && this.sessionToken && this.me) this.activateGfsAuthScope()
throw error
}
const boundaryChanged =
@@ -1693,6 +1700,13 @@ export class AppService {
this.activateGfsAuthScope()
}
} finally {
+ this.entityChangeEnvironmentSwitching = false
+ if (
+ getActiveEnvKey() === oldEnvKey &&
+ normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) === oldBaseUrl
+ ) {
+ this.ensureEntityChangeConnection()
+ }
releasePrewarm()
}
}
@@ -2938,6 +2952,7 @@ export class AppService {
}
private ensureEntityChangeConnection(): void {
+ if (this.entityChangeEnvironmentSwitching) return
if (this.entityChangeConnectionStop || this.entityChangeSubscribers.size === 0) return
if (
!this.entityChangeSessionToken &&