Skip to content

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

TypeScript

fix(desktop-app): centralize REST handoff precedence (R17-L10)

Josué24250d8f7cb4
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentRestMatch.property.test.ts b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentRestMatch.property.test.ts
new file mode 100644
index 000000000..9be791817
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentRestMatch.property.test.ts
@@ -0,0 +1,109 @@
+import { describe, expect, it } from 'vitest'
+import fc from 'fast-check'
+import {
+ desktopRestEndpointOrigin,
+ sameDesktopRestEndpoint,
+} from '../../../../../src/desktopEnvironmentUrl'
+import type {
+ DesktopRuntimeConfigOption,
+ DesktopRuntimeConfigState,
+} from '../../../../../src/types'
+import { resolveDesktopEnvironmentRestMatch } from '../desktopEnvironmentHandoff'
+
+const LINK_REST = 'https://api.example.test/api/v1'
+const REST_ENDPOINTS = [
+ LINK_REST,
+ `${LINK_REST}/`,
+ `${LINK_REST}?tenant=blue`,
+ 'https://api.example.test/api/v2',
+ 'https://other.example.test/api/v1',
+]
+
+const profilesArbitrary = fc
+ .array(
+ fc.record({
+ endpointIndex: fc.integer({ min: 0, max: REST_ENDPOINTS.length - 1 }),
+ localhost: fc.boolean(),
+ }),
+ { maxLength: 8 }
+ )
+ .map(profiles =>
+ profiles.map(
+ ({ endpointIndex, localhost }, index): DesktopRuntimeConfigOption => ({
+ id: `profile-${index}`,
+ label: `Profile ${index}`,
+ source: localhost ? 'localhost' : 'file',
+ configPath: null,
+ externalRestApiBaseUrl: REST_ENDPOINTS[endpointIndex]!,
+ rpcProxyBaseUrl: 'https://rpc.example.test',
+ appName: `Profile ${index}`,
+ })
+ )
+ )
+
+function configState(
+ options: DesktopRuntimeConfigOption[],
+ activeExactMatch: boolean
+): DesktopRuntimeConfigState {
+ return {
+ configured: true,
+ isLocalhost: false,
+ selectorVisible: true,
+ activeOptionId: activeExactMatch ? (options[0]?.id ?? null) : null,
+ currentConfig: {
+ externalRestApiBaseUrl: activeExactMatch ? LINK_REST : 'https://current.example.test/api/v1',
+ rpcProxyBaseUrl: 'https://current-rpc.example.test',
+ },
+ envKey: 'current-000000000000',
+ storagePath: '/profiles/current.json',
+ options,
+ }
+}
+
+describe('desktop REST handoff match precedence', () => {
+ it('always applies the documented precedence to generated saved-profile lists', () => {
+ fc.assert(
+ fc.property(profilesArbitrary, fc.boolean(), (options, activeExactMatch) => {
+ const state = configState(options, activeExactMatch)
+ const linkOrigin = desktopRestEndpointOrigin(LINK_REST)
+ const localhost = options.find(
+ option =>
+ option.source === 'localhost' &&
+ desktopRestEndpointOrigin(option.externalRestApiBaseUrl) === linkOrigin
+ )
+ const active =
+ state.configured &&
+ sameDesktopRestEndpoint(state.currentConfig!.externalRestApiBaseUrl, LINK_REST)
+ const saved = options.filter(
+ option =>
+ option.source !== 'localhost' &&
+ sameDesktopRestEndpoint(option.externalRestApiBaseUrl, LINK_REST)
+ )
+ const sameOriginDifferentEndpoint = options.some(
+ option =>
+ option.source !== 'localhost' &&
+ desktopRestEndpointOrigin(option.externalRestApiBaseUrl) === linkOrigin &&
+ !sameDesktopRestEndpoint(option.externalRestApiBaseUrl, LINK_REST)
+ )
+ const expectedKind = localhost
+ ? 'localhost'
+ : active
+ ? 'active'
+ : saved.length > 1
+ ? 'ambiguous'
+ : saved.length === 1
+ ? 'saved'
+ : sameOriginDifferentEndpoint
+ ? 'path-conflict'
+ : 'setup'
+
+ const decision = resolveDesktopEnvironmentRestMatch(state, LINK_REST)
+
+ expect(decision.kind).toBe(expectedKind)
+ if (decision.kind === 'localhost') expect(decision.option.id).toBe(localhost?.id)
+ if (decision.kind === 'saved') expect(decision.option.id).toBe(saved[0]?.id)
+ }),
+ { numRuns: 20000 }
+ )
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
index b87a1700c..28bb58739 100644
--- a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
@@ -6,6 +6,7 @@ import {
import type {
DesktopRuntimeConfig,
DesktopRuntimeConfigHandoffSelection,
+ DesktopRuntimeConfigOption,
DesktopRuntimeConfigState,
} from '../../../../src/types'
import type { DesktopEnvironmentSwitchConfirmation, SetStatusFn } from './types'
@@ -73,7 +74,7 @@ function isLocalhostOption(option: DesktopRuntimeConfigState['options'][number])
return option.source === 'localhost' || option.id === LOCALHOST_OPTION_ID
}
-export function getDesktopEnvironmentRestMatches(
+function getDesktopEnvironmentRestMatches(
configState: DesktopRuntimeConfigState,
externalRestApiBaseUrl: string
) {
@@ -96,6 +97,28 @@ export function getDesktopEnvironmentRestMatches(
}
}
+type DesktopEnvironmentRestMatchDecision =
+ | { kind: 'localhost'; option: DesktopRuntimeConfigOption }
+ | { kind: 'active' }
+ | { kind: 'ambiguous' }
+ | { kind: 'saved'; option: DesktopRuntimeConfigOption }
+ | { kind: 'path-conflict' }
+ | { kind: 'setup' }
+
+export function resolveDesktopEnvironmentRestMatch(
+ configState: DesktopRuntimeConfigState,
+ externalRestApiBaseUrl: string
+): DesktopEnvironmentRestMatchDecision {
+ const matches = getDesktopEnvironmentRestMatches(configState, externalRestApiBaseUrl)
+ if (matches.localhost) return { kind: 'localhost', option: matches.localhost }
+ if (matches.active) return { kind: 'active' }
+ if (matches.saved.length > 1) return { kind: 'ambiguous' }
+ const [savedOption] = matches.saved
+ if (savedOption) return { kind: 'saved', option: savedOption }
+ if (matches.sameOriginDifferentEndpoint.length > 0) return { kind: 'path-conflict' }
+ return { kind: 'setup' }
+}
+
function isAuthenticationOperationInProgress(state: DesktopEnvironmentHandoffState): boolean {
return state.booting || state.busy || state.authTransitioning
}
@@ -206,11 +229,11 @@ export function createDesktopEnvironmentSetupHandler({
return
}
- let restMatches = getDesktopEnvironmentRestMatches(
+ let restMatch = resolveDesktopEnvironmentRestMatch(
configState,
linkedConfig.externalRestApiBaseUrl
)
- if (restMatches.localhost) {
+ if (restMatch.kind === 'localhost') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected: the Localhost environment cannot be opened from a link.',
@@ -219,18 +242,14 @@ export function createDesktopEnvironmentSetupHandler({
return
}
authState = getAuthState()
- let activeRestEndpointMatches = restMatches.active
- if (
- !activeRestEndpointMatches &&
- restMatches.saved.length === 0 &&
- restMatches.sameOriginDifferentEndpoint.length > 0
- ) {
+ let activeRestEndpointMatches = restMatch.kind === 'active'
+ if (restMatch.kind === 'path-conflict') {
setPendingDesktopEnvironmentSetup(null)
rejectSameOriginPathConflict(setStatus)
return
}
- if (restMatches.saved.length > 1 && !activeRestEndpointMatches) {
+ if (restMatch.kind === 'ambiguous') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected because multiple saved environments use this REST API.',
@@ -319,11 +338,11 @@ export function createDesktopEnvironmentSetupHandler({
reportAuthenticationStateChanged(authState, setStatus)
return
}
- restMatches = getDesktopEnvironmentRestMatches(
+ restMatch = resolveDesktopEnvironmentRestMatch(
configState,
linkedConfig.externalRestApiBaseUrl
)
- if (restMatches.localhost) {
+ if (restMatch.kind === 'localhost') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected: the Localhost environment cannot be opened from a link.',
@@ -331,17 +350,13 @@ export function createDesktopEnvironmentSetupHandler({
)
return
}
- activeRestEndpointMatches = restMatches.active
- if (
- !activeRestEndpointMatches &&
- restMatches.saved.length === 0 &&
- restMatches.sameOriginDifferentEndpoint.length > 0
- ) {
+ activeRestEndpointMatches = restMatch.kind === 'active'
+ if (restMatch.kind === 'path-conflict') {
setPendingDesktopEnvironmentSetup(null)
rejectSameOriginPathConflict(setStatus)
return
}
- if (restMatches.saved.length > 1 && !activeRestEndpointMatches) {
+ if (restMatch.kind === 'ambiguous') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected because multiple saved environments use this REST API.',
@@ -363,13 +378,8 @@ export function createDesktopEnvironmentSetupHandler({
return
}
- if (restMatches.saved.length === 1) {
- const [savedOption] = restMatches.saved
- if (!savedOption) {
- setPendingDesktopEnvironmentSetup(null)
- setStatus('Could not resolve the saved desktop environment.', 'error')
- return
- }
+ if (restMatch.kind === 'saved') {
+ const savedOption = restMatch.option
setPendingDesktopEnvironmentSetup(null)
const selection = await handleSelectRuntimeConfig(savedOption.id, sessionGeneration)
if (!selection) return
diff --git a/desktop-app/ui/src/hooks/domain/useAuthController.ts b/desktop-app/ui/src/hooks/domain/useAuthController.ts
index 7e4c91079..859b83412 100644
--- a/desktop-app/ui/src/hooks/domain/useAuthController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAuthController.ts
@@ -13,7 +13,7 @@ import type {
import type { Tone } from '../../uiTypes'
import {
createDesktopEnvironmentSetupHandler,
- getDesktopEnvironmentRestMatches,
+ resolveDesktopEnvironmentRestMatch,
} from './desktopEnvironmentHandoff'
import type { DesktopEnvironmentSwitchConfirmation, SetStatusFn } from './types'
@@ -625,12 +625,12 @@ export function useAuthController({
return
}
- const restMatches = getDesktopEnvironmentRestMatches(
+ const restMatch = resolveDesktopEnvironmentRestMatch(
currentConfigState,
nextConfig.externalRestApiBaseUrl
)
- if (restMatches.localhost) {
+ if (restMatch.kind === 'localhost') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected: the Localhost environment cannot be opened from a link.',
@@ -639,13 +639,13 @@ export function useAuthController({
return
}
- if (restMatches.active) {
+ if (restMatch.kind === 'active') {
setPendingDesktopEnvironmentSetup(null)
setStatus('This link points to the active Evenfire Desktop environment.', 'success')
return
}
- if (restMatches.saved.length === 0 && restMatches.sameOriginDifferentEndpoint.length > 0) {
+ if (restMatch.kind === 'path-conflict') {
setPendingDesktopEnvironmentSetup(null)
setStatus(
'Desktop setup link rejected because this REST host is already saved with a different API endpoint.',
@@ -654,23 +654,17 @@ export function useAuthController({
return
}
- if (restMatches.saved.length > 0) {
- if (restMatches.saved.length > 1) {
- setPendingDesktopEnvironmentSetup(null)
- setStatus(
- 'Desktop setup link rejected because multiple saved environments use this REST API.',
- 'error'
- )
- return
- }
-
- const [savedOption] = restMatches.saved
- if (!savedOption) {
- setPendingDesktopEnvironmentSetup(null)
- setStatus('Could not resolve the saved desktop environment.', 'error')
- return
- }
+ if (restMatch.kind === 'ambiguous') {
+ setPendingDesktopEnvironmentSetup(null)
+ setStatus(
+ 'Desktop setup link rejected because multiple saved environments use this REST API.',
+ 'error'
+ )
+ return
+ }
+ if (restMatch.kind === 'saved') {
+ const savedOption = restMatch.option
const selection = await handleSelectRuntimeConfigForHandoff(
savedOption.id,
expectedSessionGeneration
@@ -709,10 +703,13 @@ export function useAuthController({
const state = await handleSaveRuntimeConfig(nextConfig, expectedSessionGeneration)
if (!state) return
const selectedOption = state.options.find(option => option.id === state.activeOptionId)
- const selectedRestMatches = getDesktopEnvironmentRestMatches(
+ const selectedRestMatch = resolveDesktopEnvironmentRestMatch(
state,
nextConfig.externalRestApiBaseUrl
- ).saved.some(option => option.id === state.activeOptionId)
+ )
+ const selectedRestMatches =
+ selectedRestMatch.kind === 'active' ||
+ (selectedRestMatch.kind === 'saved' && selectedRestMatch.option.id === state.activeOptionId)
if (!selectedOption || !selectedRestMatches || !selectedOption.rpcProxyBaseUrl.trim()) {
setPendingDesktopEnvironmentSetup(null)
setStatus(