Skip to content

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

TypeScript

fix(desktop-app): preserve team transition ordering

Josué3eb008e1da2d
diff --git a/desktop-app/src/__tests__/appService.nativeCommit.test.ts b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
index cbd92e282..e68121a8f 100644
--- a/desktop-app/src/__tests__/appService.nativeCommit.test.ts
+++ b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
@@ -3,7 +3,7 @@ import {
cleanupNativeCommitTestHarness,
createNativeCommitTestHarness,
deferred,
-} from './appService.nativeCommitTestHarness.js'
+} from '../../testSupport/appService.nativeCommitTestHarness.js'
afterEach(cleanupNativeCommitTestHarness)
diff --git a/desktop-app/src/__tests__/appService.nativeLogoutRecovery.test.ts b/desktop-app/src/__tests__/appService.nativeLogoutRecovery.test.ts
index b6e0c11cd..623baae2e 100644
--- a/desktop-app/src/__tests__/appService.nativeLogoutRecovery.test.ts
+++ b/desktop-app/src/__tests__/appService.nativeLogoutRecovery.test.ts
@@ -3,7 +3,7 @@ import {
cleanupNativeCommitTestHarness,
createNativeCommitTestHarness,
deferred,
-} from './appService.nativeCommitTestHarness.js'
+} from '../../testSupport/appService.nativeCommitTestHarness.js'
afterEach(cleanupNativeCommitTestHarness)
diff --git a/desktop-app/src/__tests__/appService.prewarm.test.ts b/desktop-app/src/__tests__/appService.prewarm.test.ts
index 2fb75eeaa..1fc76dc11 100644
--- a/desktop-app/src/__tests__/appService.prewarm.test.ts
+++ b/desktop-app/src/__tests__/appService.prewarm.test.ts
@@ -402,7 +402,9 @@ describe('AppService.prewarmHost — bounded wake re-emission', () => {
return operation
})
await operationStarted
- expect(switchSessionToTeam).toHaveBeenCalledWith('team-2', expect.any(String))
+ expect(switchSessionToTeam).toHaveBeenCalledWith('team-2', expect.any(String), {
+ commitOwnerHeld: true,
+ })
expect(
(
svc as unknown as { prewarmAttemptAtByHostRef: Map<string, number> }
@@ -414,7 +416,9 @@ describe('AppService.prewarmHost — bounded wake re-emission', () => {
finishOperation()
await hop
- expect(switchSessionToTeam).toHaveBeenCalledWith('team-1', expect.any(String))
+ expect(switchSessionToTeam).toHaveBeenCalledWith('team-1', expect.any(String), {
+ commitOwnerHeld: true,
+ })
await expect(svc.prewarmHost('chatllm')).resolves.toEqual({
requested: false,
skipped: 'cooldown',
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index a2b34a7a9..103299dc3 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -1151,7 +1151,17 @@ export class AppService {
return this.me.teamId || ''
}
- private async switchSessionToTeam(teamId: string, token = this.requireSessionToken()) {
+ private switchSessionToTeam(
+ teamId: string,
+ token?: string,
+ options: { commitOwnerHeld?: boolean } = {}
+ ): Promise<string> {
+ const run = () =>
+ this.switchSessionToTeamWithCommitOwner(teamId, token ?? this.requireSessionToken())
+ return options.commitOwnerHeld ? run() : this.withNativeAuthEnvironmentCommit(run)
+ }
+
+ private async switchSessionToTeamWithCommitOwner(teamId: string, token: string) {
const releaseTransientHop = this.enterGfsTransientTeamHop()
const targetTeamId = String(teamId || '').trim()
try {
@@ -1274,7 +1284,9 @@ export class AppService {
try {
if (shouldSwitch) {
- activeToken = await this.switchSessionToTeam(targetTeamId, originalToken)
+ activeToken = await this.switchSessionToTeam(targetTeamId, originalToken, {
+ commitOwnerHeld: true,
+ })
}
try {
@@ -1285,7 +1297,9 @@ export class AppService {
} finally {
if (shouldRestore) {
try {
- await this.switchSessionToTeam(originalTeamId, this.requireSessionToken())
+ await this.switchSessionToTeam(originalTeamId, this.requireSessionToken(), {
+ commitOwnerHeld: true,
+ })
restoredOriginalTeam = true
} catch (restoreError) {
if (!operationError) throw restoreError
@@ -3687,10 +3701,6 @@ export class AppService {
}
async switchTeam(teamId: string): Promise<SessionState> {
- return this.withNativeAuthEnvironmentCommit(() => this.switchTeamWithCommitOwner(teamId))
- }
-
- private async switchTeamWithCommitOwner(teamId: string): Promise<SessionState> {
const targetTeamId = String(teamId || '').trim()
if (!targetTeamId) throw new Error('teamId is required')
// Explicit team changes fence opportunistic wake immediately. A failed
@@ -3714,39 +3724,48 @@ export class AppService {
// NOTE: only the user-initiated `switchTeam` closes streams — the transient
// per-operation team hops in `runWithTeamContext`/`switchSessionToTeam` (e.g.
// minting a cross-team RPC token to decide an approval) must NOT.
- await this.switchSessionToTeam(targetTeamId)
- if (!this.me) throw new Error('Team switch ended without an authenticated session')
- // A deliberate user switch is the only team-context boundary that fences
- // in-flight uploads. Transient runWithTeamContext hops intentionally use
- // switchSessionToTeam directly and must leave those jobs untouched.
- try {
- await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
- } catch (error) {
- // The replacement session is already installed at this point. If the
- // auth-boundary state write fails, do not leave that token/me paired
- // with a blocked or stale GFS scope. Cleanup remains inside the outer
- // transient-hop gate, and the original persistence error is preserved.
- this.clearAuthenticatedSessionState()
+ const switchedToken = await this.switchSessionToTeam(targetTeamId)
+ const switchedMe = this.me
+ const switchedGeneration = this.sessionGeneration
+ const switchedEnvironment = this.captureAuthEnvironmentBinding()
+ return await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(switchedGeneration)
+ this.assertAuthEnvironmentBinding(switchedEnvironment)
+ if (this.sessionToken !== switchedToken || !switchedMe || this.me !== switchedMe) {
+ throw new Error('stale_auth_epoch: authenticated team scope changed before activation')
+ }
+ // A deliberate user switch is the only team-context boundary that fences
+ // in-flight uploads. Transient runWithTeamContext hops intentionally use
+ // switchSessionToTeam directly and must leave those jobs untouched.
try {
- await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
- legacyEnvKeys: getActiveLegacyEnvKeys(),
- })
- } catch (clearError) {
- console.warn(
- '[AppService] Failed to clear a partially switched team session:',
- clearError
- )
+ await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
+ } catch (error) {
+ // The replacement session is already installed at this point. If the
+ // auth-boundary state write fails, do not leave that token/me paired
+ // with a blocked or stale GFS scope. Cleanup remains inside the outer
+ // transient-hop gate, and the original persistence error is preserved.
+ this.clearAuthenticatedSessionState()
+ try {
+ await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
+ legacyEnvKeys: getActiveLegacyEnvKeys(),
+ })
+ } catch (clearError) {
+ console.warn(
+ '[AppService] Failed to clear a partially switched team session:',
+ clearError
+ )
+ }
+ throw error
}
- throw error
- }
- this.activateGfsAuthScope()
- this.updateEntityChangeSessionToken(this.sessionToken)
- this.stopAllStreams()
- // Grants are keyed by userId, not by team, so they carry over — but every
- // cached org/agents/contexts answer is now about the wrong team. Drop the
- // cache and tell mounted plugins to refetch.
- tryGetPluginSdkRuntime()?.notifySessionChanged(true)
- return { authenticated: true, me: this.me }
+ this.activateGfsAuthScope()
+ this.updateEntityChangeSessionToken(this.sessionToken)
+ this.stopAllStreams()
+ // Grants are keyed by userId, not by team, so they carry over — but every
+ // cached org/agents/contexts answer is now about the wrong team. Drop the
+ // cache and tell mounted plugins to refetch.
+ tryGetPluginSdkRuntime()?.notifySessionChanged(true)
+ return { authenticated: true, me: this.me }
+ })
} finally {
releasePrewarm()
releaseTransientHop()
diff --git a/desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts b/desktop-app/testSupport/appService.nativeCommitTestHarness.ts
similarity index 97%
rename from desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts
rename to desktop-app/testSupport/appService.nativeCommitTestHarness.ts
index cd1520f87..534e83f46 100644
--- a/desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts
+++ b/desktop-app/testSupport/appService.nativeCommitTestHarness.ts
@@ -3,7 +3,7 @@ import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
-vi.mock('../chatStoreBinding.js', () => ({
+vi.mock('../src/chatStoreBinding.js', () => ({
bindChatStoreForUser: vi.fn(),
unbindChatStore: vi.fn(),
__setChatStoreBaseDirForTests: vi.fn(),
@@ -72,8 +72,8 @@ export async function createNativeCommitTestHarness() {
}))
const [{ AppService }, runtimeConfig] = await Promise.all([
- import('../appService.js'),
- import('../config.js'),
+ import('../src/appService.js'),
+ import('../src/config.js'),
])
const restA = 'https://api-a.example.test'
const restB = 'https://api-b.example.test'