Skip to content

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

TypeScript

fix(desktop): r14-l1 make rpc discovery awaits explicit

Josué9b1a56a328f0
diff --git a/desktop-app/src/__tests__/appService.runtimeConfigResolution.test.ts b/desktop-app/src/__tests__/appService.runtimeConfigResolution.test.ts
new file mode 100644
index 000000000..96317254a
--- /dev/null
+++ b/desktop-app/src/__tests__/appService.runtimeConfigResolution.test.ts
@@ -0,0 +1,82 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import {
+ cleanupNativeCommitTestHarness,
+ createNativeCommitTestHarness,
+ deferred,
+} from '../../testSupport/appService.nativeCommitTestHarness.js'
+
+afterEach(cleanupNativeCommitTestHarness)
+
+type DesktopEnvironmentDiscovery = {
+ externalRestApiBaseUrl: string
+ rpcProxyBaseUrl: string
+ appName: string
+}
+
+describe('AppService runtime config discovery ownership', () => {
+ it('shares a pending discovery for the same profile and REST endpoint', async () => {
+ const { service, runtimeConfig, restA } = await createNativeCommitTestHarness()
+ await runtimeConfig.saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: '',
+ appName: 'Environment A',
+ })
+
+ const discovery = deferred<DesktopEnvironmentDiscovery>()
+ const discoveryStarted = deferred<void>()
+ const getDesktopEnvironment = vi.fn(() => {
+ discoveryStarted.resolve()
+ return discovery.promise
+ })
+ const app = service as unknown as {
+ authClient: { getDesktopEnvironment: ReturnType<typeof vi.fn> }
+ resolveRuntimeConfigIfNeeded: () => Promise<void>
+ }
+ app.authClient = { getDesktopEnvironment }
+
+ const first = app.resolveRuntimeConfigIfNeeded()
+ await discoveryStarted.promise
+ const second = app.resolveRuntimeConfigIfNeeded()
+ await new Promise(resolve => setTimeout(resolve, 0))
+ expect(getDesktopEnvironment).toHaveBeenCalledOnce()
+
+ discovery.resolve({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: 'https://rpc-discovered.example.test',
+ appName: 'Environment A',
+ })
+ await Promise.all([first, second])
+
+ expect(runtimeConfig.config.rpcProxyBaseUrl).toBe('https://rpc-discovered.example.test')
+ })
+
+ it('clears a rejected discovery entry so the next call can retry', async () => {
+ const { service, runtimeConfig, restA } = await createNativeCommitTestHarness()
+ await runtimeConfig.saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: '',
+ appName: 'Environment A',
+ })
+
+ const getDesktopEnvironment = vi
+ .fn()
+ .mockRejectedValueOnce(new Error('temporary discovery failure'))
+ .mockResolvedValue({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: 'https://rpc-discovered.example.test',
+ appName: 'Environment A',
+ })
+ const app = service as unknown as {
+ authClient: { getDesktopEnvironment: ReturnType<typeof vi.fn> }
+ resolveRuntimeConfigIfNeeded: () => Promise<void>
+ }
+ app.authClient = { getDesktopEnvironment }
+
+ await expect(app.resolveRuntimeConfigIfNeeded()).rejects.toThrow('temporary discovery failure')
+
+ await expect(app.resolveRuntimeConfigIfNeeded()).resolves.toBeUndefined()
+
+ expect(getDesktopEnvironment).toHaveBeenCalledTimes(2)
+ expect(runtimeConfig.config.rpcProxyBaseUrl).toBe('https://rpc-discovered.example.test')
+ })
+})
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 3f5d56253..ee223f329 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -892,7 +892,7 @@ export class AppService {
private teamDirectoryCache: TeamDirectoryResult | null = null
private teamContextQueue: Promise<void> = Promise.resolve()
private restoreSavedSessionInFlight: Promise<SessionState> | null = null
- private runtimeConfigResolutionInFlight = new Map<string, Promise<void>>()
+ private runtimeConfigResolutionInFlight = new Map<string, { promise: Promise<void> }>()
private savedSessionRestoreAttemptedEnvKey: string | null = null
private savedSessionRestoreAttemptedAtMs = 0
private interactiveLoginAttempts = 0
@@ -2378,12 +2378,15 @@ export class AppService {
request.externalRestApiBaseUrl,
])
const inFlight = this.runtimeConfigResolutionInFlight.get(resolutionKey)
- if (inFlight) return inFlight
+ if (inFlight) {
+ await inFlight.promise
+ return
+ }
- const resolution = this.resolveRuntimeConfigRequest(request)
+ const resolution = { promise: this.resolveRuntimeConfigRequest(request) }
this.runtimeConfigResolutionInFlight.set(resolutionKey, resolution)
try {
- await resolution
+ await resolution.promise
} finally {
if (this.runtimeConfigResolutionInFlight.get(resolutionKey) === resolution) {
this.runtimeConfigResolutionInFlight.delete(resolutionKey)