Skip to content

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

TypeScript

fix(desktop): report interrupted environment handoffs (R13-L4)

Josué37cc39b72c8b
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
index 3e00cc2f7..9824ab1ab 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
+++ b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
@@ -674,4 +674,59 @@ describe('Desktop environment REST endpoint matching', () => {
'error'
)
})
+
+ it('reports an auth transition that starts during the post-logout config read', async () => {
+ let authState = {
+ booting: false,
+ busy: false,
+ authTransitioning: false,
+ isAuthenticated: true,
+ }
+ let sessionGeneration = 0
+ let refreshCount = 0
+ let finishSecondRefresh!: () => void
+ let reportSecondRefreshStarted!: () => void
+ const secondRefreshStarted = new Promise<void>(resolve => {
+ reportSecondRefreshStarted = resolve
+ })
+ const runtimeConfigState = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ const refreshRuntimeConfigState = () => {
+ refreshCount += 1
+ if (refreshCount === 1) return Promise.resolve(runtimeConfigState)
+ return new Promise<typeof runtimeConfigState>(resolve => {
+ finishSecondRefresh = () => resolve(runtimeConfigState)
+ reportSecondRefreshStarted()
+ })
+ }
+ const logout = vi.fn(async () => {
+ authState = { ...authState, isAuthenticated: false }
+ sessionGeneration += 1
+ return sessionGeneration
+ })
+ const { handler, selectRuntimeConfig, setPendingDesktopEnvironmentSetup, setStatus } =
+ createHandler(
+ () => authState,
+ refreshRuntimeConfigState,
+ logout,
+ vi.fn(async () => {}),
+ async () => sessionGeneration
+ )
+
+ const handling = handler({
+ ...targetEnvironment,
+ externalRestApiBaseUrl: `${targetEnvironment.externalRestApiBaseUrl}/api/v1`,
+ })
+ await secondRefreshStarted
+ authState = { ...authState, busy: true, authTransitioning: true }
+ finishSecondRefresh()
+ await handling
+
+ expect(logout).toHaveBeenCalledOnce()
+ expect(selectRuntimeConfig).not.toHaveBeenCalled()
+ expect(setPendingDesktopEnvironmentSetup).toHaveBeenCalledWith(null)
+ expect(setStatus).toHaveBeenCalledWith(
+ 'Finish the current authentication action before continuing this desktop link.',
+ 'info'
+ )
+ })
})
diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
index 7503fca36..e0aa2a5fb 100644
--- a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
@@ -99,6 +99,20 @@ function isAuthenticationOperationInProgress(state: DesktopEnvironmentHandoffSta
return state.booting || state.busy || state.authTransitioning
}
+function reportAuthenticationStateChanged(
+ state: DesktopEnvironmentHandoffState,
+ setStatus: SetStatusFn
+): void {
+ if (isAuthenticationOperationInProgress(state)) {
+ setStatus(
+ 'Finish the current authentication action before continuing this desktop link.',
+ 'info'
+ )
+ return
+ }
+ setStatus('The desktop session changed while processing this link. Open it again.', 'info')
+}
+
function isActiveRestEndpointMatch(
configState: DesktopRuntimeConfigState,
externalRestApiBaseUrl: string
@@ -204,7 +218,6 @@ export function createDesktopEnvironmentSetupHandler({
)
return
}
-
if (restMatches.sameOriginDifferentEndpoint.length > 0) {
setPendingDesktopEnvironmentSetup(null)
rejectSameOriginPathConflict(setStatus)
@@ -236,7 +249,10 @@ export function createDesktopEnvironmentSetupHandler({
if (!(await ownsSessionGeneration(sessionGeneration))) return
authState = getAuthState()
- if (isAuthenticationOperationInProgress(authState) || !authState.isAuthenticated) return
+ if (isAuthenticationOperationInProgress(authState) || !authState.isAuthenticated) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
setPendingDesktopEnvironmentSetup(null)
let logoutGeneration: number | null
@@ -254,17 +270,28 @@ export function createDesktopEnvironmentSetupHandler({
if (authState.isAuthenticated && !isAuthenticationOperationInProgress(authState)) {
try {
await onSessionNeedsLoad({ preserveNav: true })
- } catch {
- // A failed logout must leave the current environment in place.
+ } catch (error) {
+ setStatus(
+ `Could not reload the current desktop session: ${error instanceof Error ? error.message : String(error)}`,
+ 'error'
+ )
}
}
+ if (authState.isAuthenticated || isAuthenticationOperationInProgress(authState)) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ } else {
+ setStatus('Could not confirm sign out before switching desktop environments.', 'error')
+ }
return
}
sessionGeneration = logoutGeneration
if (!(await ownsSessionGeneration(sessionGeneration))) return
authState = getAuthState()
- if (isAuthenticationOperationInProgress(authState)) return
+ if (isAuthenticationOperationInProgress(authState)) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
if (authState.isAuthenticated) {
try {
await onSessionNeedsLoad({ preserveNav: true })
@@ -278,7 +305,10 @@ export function createDesktopEnvironmentSetupHandler({
}
if (!(await ownsSessionGeneration(sessionGeneration))) return
authState = getAuthState()
- if (authState.isAuthenticated) return
+ if (authState.isAuthenticated) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
}
try {
configState = await refreshRuntimeConfigState()
@@ -288,7 +318,10 @@ export function createDesktopEnvironmentSetupHandler({
}
if (!(await ownsSessionGeneration(sessionGeneration))) return
authState = getAuthState()
- if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) return
+ if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
restMatches = getDesktopEnvironmentRestMatches(
configState,
linkedConfig.externalRestApiBaseUrl
@@ -341,10 +374,19 @@ export function createDesktopEnvironmentSetupHandler({
}
setPendingDesktopEnvironmentSetup(null)
const selection = await handleSelectRuntimeConfig(savedOption.id, sessionGeneration)
- if (!selection) return
+ if (!selection) {
+ setStatus(
+ 'Could not select the linked desktop environment. Try opening the link again.',
+ 'error'
+ )
+ return
+ }
if (!(await ownsSessionGeneration(selection.sessionGeneration))) return
authState = getAuthState()
- if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) return
+ if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
try {
await onSessionNeedsLoad({ preserveNav: true })
} catch (error) {
@@ -359,7 +401,10 @@ export function createDesktopEnvironmentSetupHandler({
// The backend supplies the RPC endpoint after the user confirms this REST API.
if (!(await ownsSessionGeneration(sessionGeneration))) return
authState = getAuthState()
- if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) return
+ if (isAuthenticationOperationInProgress(authState) || authState.isAuthenticated) {
+ reportAuthenticationStateChanged(authState, setStatus)
+ return
+ }
setPendingDesktopEnvironmentSetup({ ...linkedConfig, rpcProxyBaseUrl: '' })
}