Skip to content

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

TypeScript
32.5 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import fs from 'node:fs/promises'
import { createRequire } from 'node:module'
import os from 'node:os'
import path from 'node:path'
import { buildGfsFileReference, classifyBytes } from '@clerum/gfs-interaction-policy'
import { AppService } from '../appService.js'
import { __setChatStoreBaseDirForTests } from '../chatStoreBinding.js'
import { ApiError } from '../httpClient.js'
import type { HostMessageAttachment } from '../types.js'

const { declaredHeaderPngOfSize, jpegOfSize } = createRequire(import.meta.url)(
  '../../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'
) as {
  declaredHeaderPngOfSize: (targetBytes: number) => Buffer
  jpegOfSize: (targetBytes: number, width?: number, height?: number) => Buffer
}

const MIB = 1024 * 1024
const MESSAGE_SCOPES = ['host:message:invoke', 'host:task:read', 'host:wake:write'] as const

function imageAttachment(
  id: string,
  sizeBytes: number,
  mimeType: HostMessageAttachment['mimeType'] = 'image/png'
): HostMessageAttachment {
  const bytes =
    mimeType === 'image/jpeg' ? jpegOfSize(sizeBytes) : declaredHeaderPngOfSize(sizeBytes)
  return {
    id,
    kind: 'image',
    mimeType,
    encoding: 'base64',
    dataBase64: bytes.toString('base64'),
    filename: `${id}.${mimeType === 'image/jpeg' ? 'jpg' : 'png'}`,
  }
}

describe('AppService.invokeHostMessage', () => {
  let chatStoreBaseDir: string

  beforeEach(async () => {
    chatStoreBaseDir = await fs.mkdtemp(path.join(os.tmpdir(), 'clerum-app-service-rpc-'))
    __setChatStoreBaseDirForTests(chatStoreBaseDir)
  })

  afterEach(async () => {
    __setChatStoreBaseDirForTests(null)
    await fs.rm(chatStoreBaseDir, { recursive: true, force: true })
  })

  it('forces desktop host messages onto the authenticated rpc envelope', async () => {
    const service = new AppService() as any
    service.sessionToken = 'session-token'
    service.me = {
      id: '00000000-0000-4000-8000-000000000001',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: '00000000-0000-4000-8000-0000000000aa',
      teamName: 'Test Team',
      role: 'member',
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }

    await service.invokeHostMessage(
      'chatllm',
      {
        content: 'What workflow recipes can I trigger and what inputs do they require?',
        channelType: 'slack',
        model: 'vision-model',
        modelSelectionRevision: 7,
        imageModel: { provider: 'untrusted', model: 'untrusted' },
        channelId: 'attacker-channel',
        hostRef: 'attacker-host',
        sender: 'attacker-controlled-user',
        metadata: {
          teamId: 'attacker-controlled-team',
          targetUserId: '00000000-0000-4000-8000-000000000999',
          outputOverrides: { path: '/tmp/untrusted' },
        },
        targetUserId: '00000000-0000-4000-8000-000000000999',
        outputOverrides: { path: '/tmp/untrusted' },
      },
      ['chatllm'],
      { async: true }
    )

    expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)
    const forwarded = service.rpcClient.invokeHostMessage.mock.calls[0][2]
    expect(forwarded).toEqual({
      content: 'What workflow recipes can I trigger and what inputs do they require?',
      channelType: 'rpc',
      model: 'vision-model',
      modelSelectionRevision: 7,
      channelId: 'chatllm',
      hostRef: 'chatllm',
      sender: '00000000-0000-4000-8000-000000000001',
      metadata: { teamId: '00000000-0000-4000-8000-0000000000aa' },
      threadId: undefined,
      attachments: undefined,
    })
    expect(service.rpcClient.invokeHostMessage.mock.calls[0][3]).toEqual({ async: true })
  })

  it('forwards a 5 MiB JPEG without letting the payload choose identity, host, or scopes', async () => {
    const service = new AppService() as any
    service.sessionToken = 'session-token'
    service.me = {
      id: '00000000-0000-4000-8000-000000000001',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: '00000000-0000-4000-8000-0000000000aa',
      teamName: 'Test Team',
      role: 'member',
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }
    const attachment = imageAttachment('photo', 5 * MIB, 'image/jpeg')

    await service.invokeHostMessage(
      'chatllm',
      {
        content: 'look',
        channelType: 'slack',
        channelId: 'attacker-channel',
        hostRef: 'attacker-host',
        sender: 'attacker-controlled-user',
        metadata: { teamId: 'attacker-controlled-team' },
        attachments: [attachment],
      },
      ['chatllm']
    )

    expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(
      'session-token',
      [...MESSAGE_SCOPES],
      ['chatllm']
    )
    expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)
    expect(service.rpcClient.invokeHostMessage.mock.calls[0][0]).toBe('rpc-test-token')
    expect(service.rpcClient.invokeHostMessage.mock.calls[0][1]).toBe('chatllm')
    const forwarded = service.rpcClient.invokeHostMessage.mock.calls[0][2]
    expect(forwarded).toEqual({
      content: 'look',
      channelType: 'rpc',
      channelId: 'chatllm',
      hostRef: 'chatllm',
      sender: '00000000-0000-4000-8000-000000000001',
      metadata: { teamId: '00000000-0000-4000-8000-0000000000aa' },
      threadId: undefined,
      attachments: [attachment],
    })
    expect(Buffer.from(forwarded.attachments[0].dataBase64, 'base64')).toHaveLength(5 * MIB)
  })

  it('forwards a 10 MiB PNG + 5 MiB JPEG on the same authenticated envelope', async () => {
    const service = new AppService() as any
    service.sessionToken = 'session-token'
    service.me = {
      id: '00000000-0000-4000-8000-000000000001',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: '00000000-0000-4000-8000-0000000000aa',
      teamName: 'Test Team',
      role: 'member',
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }
    const attachments = [
      imageAttachment('ten', 10 * MIB),
      imageAttachment('five', 5 * MIB, 'image/jpeg'),
    ]

    await service.invokeHostMessage(
      'chatllm',
      {
        content: 'look',
        channelType: 'slack',
        hostRef: 'attacker-host',
        sender: 'attacker-controlled-user',
        attachments,
      },
      ['chatllm']
    )

    expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(
      'session-token',
      [...MESSAGE_SCOPES],
      ['chatllm']
    )
    const forwarded = service.rpcClient.invokeHostMessage.mock.calls[0][2]
    expect(forwarded.channelType).toBe('rpc')
    expect(forwarded.sender).toBe('00000000-0000-4000-8000-000000000001')
    expect(forwarded.hostRef).toBe('chatllm')
    expect(forwarded.channelId).toBe('chatllm')
    expect(forwarded.attachments).toEqual(attachments)
    expect(Buffer.from(forwarded.attachments[0].dataBase64, 'base64')).toHaveLength(10 * MIB)
    expect(Buffer.from(forwarded.attachments[1].dataBase64, 'base64')).toHaveLength(5 * MIB)
  })

  it('rejects malformed attachments before issuing an RPC token', async () => {
    const service = new AppService() as any
    service.sessionToken = 'test-token-session'
    service.me = {
      id: '00000000-0000-4000-8000-000000000001',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: '00000000-0000-4000-8000-0000000000aa',
      teamName: 'Test Team',
      role: 'member',
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'test-token-rpc' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }
    const issueToken = vi.spyOn(service, 'issueRpcTokenForHostRefs')

    await expect(
      service.invokeHostMessage('chatllm', { content: 'hello', attachments: {} }, ['chatllm'])
    ).rejects.toThrow('Attachments must be a list.')
    expect(issueToken).not.toHaveBeenCalled()
    expect(service.rpcTokenManager.getOrIssue).not.toHaveBeenCalled()
    expect(service.rpcClient.invokeHostMessage).not.toHaveBeenCalled()

    // Positive control: the same call with a well-formed list issues the token
    // and reaches the Host, so the checks above are not vacuous.
    await service.invokeHostMessage('chatllm', { content: 'hello', attachments: [] }, ['chatllm'])
    expect(issueToken).toHaveBeenCalledTimes(1)
    expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalled()
    expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)
  })

  describe('structured file references (#666)', () => {
    const RID = '1234567890abcdef1234567890abcdef'

    function fileReference() {
      const built = buildGfsFileReference({
        drive: 'main',
        resourceId: RID,
        gfsUri: `gfs://main/${RID}`,
        version: 3,
        name: 'plan.md',
        declaredMediaType: null,
        byteLength: 120,
        classification: classifyBytes({
          bytes: new Uint8Array(0),
          totalByteLength: 120,
          declaredMediaType: null,
          filename: 'plan.md',
        }),
      })
      if (!built.ok) throw new Error(`fixture reference invalid: ${built.code}`)
      return built.value
    }

    function serviceWithRpc() {
      const service = new AppService() as any
      service.sessionToken = 'session-token'
      service.me = {
        id: '00000000-0000-4000-8000-000000000001',
        email: '[email protected]',
        name: 'Test User',
        picture: null,
        teamId: '00000000-0000-4000-8000-0000000000aa',
        teamName: 'Test Team',
        role: 'member',
      }
      service.rpcTokenManager = {
        getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
        clear: vi.fn(),
      }
      service.rpcClient = {
        invokeHostMessage: vi.fn().mockResolvedValue({ success: true, status: 'pending' }),
      }
      return service
    }

    it('forwards the references on the authenticated envelope', async () => {
      const service = serviceWithRpc()
      const reference = fileReference()

      await service.invokeHostMessage(
        'chatllm',
        { content: 'Summarize the plan', sender: 'attacker', fileReferences: [reference] },
        ['chatllm']
      )

      expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)
      expect(service.rpcClient.invokeHostMessage.mock.calls[0][2]).toEqual({
        content: 'Summarize the plan',
        channelType: 'rpc',
        channelId: 'chatllm',
        hostRef: 'chatllm',
        sender: '00000000-0000-4000-8000-000000000001',
        metadata: { teamId: '00000000-0000-4000-8000-0000000000aa' },
        threadId: undefined,
        attachments: undefined,
        fileReferences: [reference],
      })
    })

    it('rejects non-list references before issuing an RPC token', async () => {
      const service = serviceWithRpc()
      const issueToken = vi.spyOn(service, 'issueRpcTokenForHostRefs')

      await expect(
        service.invokeHostMessage('chatllm', { content: 'hello', fileReferences: {} }, ['chatllm'])
      ).rejects.toThrow('File references must be a list.')
      expect(issueToken).toHaveBeenCalledTimes(0)
      expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(0)

      // Positive control: a list issues the token and reaches the Host.
      await service.invokeHostMessage('chatllm', { content: 'hello', fileReferences: [] }, [
        'chatllm',
      ])
      expect(issueToken).toHaveBeenCalledTimes(1)
      expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)
    })
  })

  it('switches to a matching directory team before issuing RPC tokens for teamless sessions', async () => {
    const service = new AppService() as any
    service.desktopGfsUploadStatePath = vi
      .fn()
      .mockResolvedValue(path.join(chatStoreBaseDir, 'gfs-upload-state.json'))
    service.sessionToken = 'teamless-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: null,
      teamName: null,
      role: 'member',
    }
    const teamMe = {
      ...service.me,
      teamId: 'team-1',
      teamName: 'Team One',
    }
    service.accessCatalog = {
      userId: 'user-1',
      teamId: null,
      userContextIds: [],
      userAgentNames: ['pro-agent'],
      teamContextIds: [],
      teamAgentNames: [],
      contextIds: [],
      agentNames: ['pro-agent'],
      mcpServersByAgent: { 'pro-agent': [] },
      agentContextByName: { 'pro-agent': null },
    }
    service.teamDirectoryCache = {
      currentTeamId: 'team-1',
      items: [
        {
          team: { id: 'team-1', name: 'Team One', role: 'member' },
          members: [],
          contextIds: [],
          agentNames: ['pro-agent'],
        },
      ],
    }
    service.authClient = {
      getMe: vi.fn().mockResolvedValueOnce(service.me).mockResolvedValueOnce(teamMe),
      switchTeam: vi.fn().mockResolvedValue({
        token: 'team-test-token',
        team: { id: 'team-1', name: 'Team One', role: 'member' },
      }),
    }
    service.tokenStore = {
      setSessionToken: vi.fn(),
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }

    await expect(
      service.invokeHostMessage('pro-agent', { content: 'hello' }, ['pro-agent'])
    ).resolves.toEqual({ success: true, response: 'ok' })

    expect(service.authClient.switchTeam).toHaveBeenCalledWith('teamless-token', 'team-1')
    expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(
      'team-test-token',
      ['host:message:invoke', 'host:task:read', 'host:wake:write'],
      ['pro-agent']
    )
  })

  it('does not fence GFS uploads during a transient cross-team operation hop', async () => {
    const service = new AppService() as any
    service.desktopGfsUploadStatePath = vi
      .fn()
      .mockResolvedValue(path.join(chatStoreBaseDir, 'gfs-upload-state.json'))
    service.sessionToken = 'teamless-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    const uploadJob = { suspendForAuth: vi.fn() }
    service.gfsUploadJobs = new Map([
      [
        'upload-1',
        {
          job: uploadJob,
          promise: Promise.resolve(undefined),
          scope: {
            ownerId: 'user-1',
            teamId: 'team-a',
            environmentKey: 'test-env',
            baseUrl: 'https://external.example',
            drive: 'main',
            authEpoch: 1,
          },
        },
      ],
    ])
    service.gfsPendingUploadJobs = new Set()
    service.gfsPendingLegacyUploads = new Set()
    service.authClient = {
      getMe: vi
        .fn()
        .mockResolvedValueOnce({ ...service.me, teamId: 'team-b', teamName: 'Team B' })
        .mockResolvedValueOnce({ ...service.me }),
      switchTeam: vi.fn(async (_token: string, teamId: string) => ({
        token: `${teamId}-test-token`,
        team: { id: teamId, name: teamId === 'team-b' ? 'Team B' : 'Team A', role: 'member' },
      })),
    }
    service.tokenStore = { setSessionToken: vi.fn() }

    await expect(
      service.runWithTeamContext('team-b', async (token: string) => token)
    ).resolves.toBe('team-b-test-token')

    expect(uploadJob.suspendForAuth).not.toHaveBeenCalled()
    expect(service.gfsUploadJobs).toHaveProperty('size', 1)
    expect(service.me.teamId).toBe('team-a')
    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(1, 'teamless-token', 'team-b')
    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(2, 'team-b-test-token', 'team-a')
  })

  it('blocks new GFS dispatches while a transient team token is installed', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.gfsDispatchBlocked = false
    service.authClient = {
      getMe: vi
        .fn()
        .mockResolvedValueOnce({ ...service.me, teamId: 'team-b', teamName: 'Team B' })
        .mockResolvedValueOnce(service.me),
      switchTeam: vi.fn(async (_token: string, teamId: string) => ({
        token: `${teamId}-test-token`,
        team: { id: teamId, name: teamId, role: 'member' },
      })),
    }
    service.tokenStore = { setSessionToken: vi.fn() }

    await expect(
      service.runWithTeamContext('team-b', async () => {
        await expect(
          service.startGfsFileUpload('parent-a', 'upload.bin', 'upload.bin')
        ).rejects.toThrow('GFS upload dispatch is unavailable during a transient team hop')
        return 'team-b-operation'
      })
    ).resolves.toBe('team-b-operation')

    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(1, 'team-a-token', 'team-b')
    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(2, 'team-b-test-token', 'team-a')
    expect(service.gfsTransientTeamHopDepth).toBe(0)
  })

  it('keeps existing GFS controls usable through the real team-hop lifecycle', async () => {
    const service = new AppService() as any
    const statePath = path.join(chatStoreBaseDir, 'gfs-upload-state.json')
    const uploadId = '92929292-9292-4292-8292-929292929292'
    const session = {
      uploadId,
      drive: 'main',
      operation: 'create',
      expectedBytes: 4,
      partBytes: 4,
      partCount: 1,
      state: 'uploading',
      contiguousBytes: 0,
      committedBytes: 0,
      committedPartCount: 0,
      activePartCount: 0,
      expiresAt: new Date(Date.now() + 60_000).toISOString(),
    }
    const scope = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
      drive: 'main',
      authEpoch: 1,
    }
    await fs.writeFile(
      statePath,
      JSON.stringify({
        version: 2,
        records: [
          {
            version: 2,
            uploadId,
            filePath: '/tmp/payload.bin',
            fileName: 'payload.bin',
            fileSize: 4,
            target: { operation: 'create', parentRid: 'parent' },
            name: 'payload.bin',
            session,
            scope,
            status: 'active',
            updatedAt: new Date().toISOString(),
          },
        ],
        quarantined: [],
      })
    )
    service.desktopGfsUploadStatePath = vi.fn().mockResolvedValue(statePath)
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsAuthEpoch = 1
    service.gfsDispatchBlocked = false
    service.gfsScopeIdentity = { ...scope, authEpoch: undefined }
    const job = {
      snapshot: vi.fn(() => ({ state: 'uploading', session, uploadedBytes: 0, totalBytes: 4 })),
      pause: vi.fn(async () => ({ ...session, state: 'paused' })),
      resume: vi.fn(async () => ({ ...session, state: 'uploading' })),
      cancel: vi.fn(async () => undefined),
    }
    service.gfsUploadJobs = new Map([[uploadId, { job, promise: Promise.resolve(session), scope }]])
    service.authClient = {
      getMe: vi
        .fn()
        .mockResolvedValueOnce({ ...service.me, teamId: 'team-b', teamName: 'Team B' })
        .mockResolvedValueOnce(service.me),
      switchTeam: vi.fn(async (_token: string, teamId: string) => ({
        token: `${teamId}-test-token`,
        team: { id: teamId, name: teamId, role: 'member' },
      })),
    }
    service.tokenStore = { setSessionToken: vi.fn() }

    await service.runWithTeamContext('team-b', async () => {
      await expect(service.getGfsUploadSnapshot(uploadId, 'main')).resolves.toMatchObject({
        state: 'uploading',
      })
      await expect(service.listGfsUploadSessions('main')).resolves.toHaveLength(1)
      await expect(service.pauseGfsUpload(uploadId, 'main')).resolves.toMatchObject({
        state: 'paused',
      })
      await expect(service.resumeGfsUpload(uploadId, 'main')).resolves.toMatchObject({
        state: 'uploading',
      })
      await expect(service.cancelGfsUpload(uploadId, 'main')).resolves.toBeUndefined()
    })

    expect(job.snapshot).toHaveBeenCalledTimes(1)
    expect(job.pause).toHaveBeenCalledTimes(1)
    expect(job.resume).toHaveBeenCalledTimes(1)
    expect(job.cancel).toHaveBeenCalledTimes(1)
    expect(service.me.teamId).toBe('team-a')
    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(1, 'team-a-token', 'team-b')
    expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(2, 'team-b-test-token', 'team-a')
  })

  it('fences the old GFS scope if a replacement team token cannot be refreshed', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.authClient = {
      switchTeam: vi.fn().mockResolvedValue({
        token: 'team-b-test-token',
        team: { id: 'team-b', name: 'Team B', role: 'member' },
      }),
      getMe: vi.fn().mockRejectedValue(new Error('replacement session rejected')),
    }
    service.tokenStore = { setSessionToken: vi.fn(), clearSessionToken: vi.fn() }
    service.rpcTokenManager = { clear: vi.fn() }
    service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn().mockResolvedValue(undefined)
    service.clearAuthenticatedSessionState = vi.fn()

    await expect(service.switchSessionToTeam('team-b', 'team-a-token')).rejects.toThrow(
      'replacement session rejected'
    )

    expect(service.suspendDesktopGfsUploadsForAuthBoundary).toHaveBeenCalledWith({
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    })
    expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
  })

  it('clears and revokes a replacement token even when GFS fence persistence fails', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.authClient = {
      switchTeam: vi.fn().mockResolvedValue({
        token: 'team-b-test-token',
        team: { id: 'team-b', name: 'Team B', role: 'member' },
      }),
      getMe: vi.fn().mockRejectedValue(new Error('replacement session rejected')),
    }
    service.tokenStore = {
      setSessionToken: vi.fn(),
      clearSessionToken: vi.fn(),
    }
    service.rpcTokenManager = { clear: vi.fn() }
    service.suspendDesktopGfsUploadsForAuthBoundary = vi
      .fn()
      .mockRejectedValue(new Error('state file is read-only'))
    service.clearAuthenticatedSessionState = vi.fn()

    await expect(service.switchSessionToTeam('team-b', 'team-a-token')).rejects.toThrow(
      'replacement session rejected'
    )

    expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
    expect(service.tokenStore.clearSessionToken).toHaveBeenCalledWith(expect.any(String), {
      legacyEnvKeys: expect.any(Array),
    })
  })

  it('fences GFS uploads only after an explicit switchTeam succeeds', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.gfsDispatchBlocked = false
    service.authClient = {
      switchTeam: vi.fn().mockResolvedValue({
        token: 'team-b-test-token',
        team: { id: 'team-b', name: 'Team B', role: 'member' },
      }),
      getMe: vi.fn().mockResolvedValue({ ...service.me, teamId: 'team-b', teamName: 'Team B' }),
    }
    service.tokenStore = { setSessionToken: vi.fn() }
    service.rpcTokenManager = { clear: vi.fn() }
    service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn(async () => {
      await expect(
        service.startGfsFileUpload('parent-a', 'upload.bin', 'upload.bin')
      ).rejects.toThrow('GFS upload dispatch is unavailable during a transient team hop')
    })
    service.activateGfsAuthScope = vi.fn()
    service.stopAllStreams = vi.fn()
    service.gfsUploadJobs = new Map([['upload-1', { job: { suspendForAuth: vi.fn() } }]])

    await expect(service.switchTeam('team-b')).resolves.toMatchObject({
      authenticated: true,
      me: { teamId: 'team-b' },
    })

    expect(service.suspendDesktopGfsUploadsForAuthBoundary).toHaveBeenCalledWith({
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    })
    expect(
      service.suspendDesktopGfsUploadsForAuthBoundary.mock.invocationCallOrder[0]
    ).toBeGreaterThan(service.authClient.switchTeam.mock.invocationCallOrder[0])
  })

  it('clears the replacement team session when explicit GFS fence persistence fails', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.gfsDispatchBlocked = false
    service.authClient = {
      switchTeam: vi.fn().mockResolvedValue({
        token: 'team-b-test-token',
        team: { id: 'team-b', name: 'Team B', role: 'member' },
      }),
      getMe: vi.fn().mockResolvedValue({ ...service.me, teamId: 'team-b', teamName: 'Team B' }),
    }
    service.tokenStore = {
      setSessionToken: vi.fn(),
      clearSessionToken: vi.fn(),
    }
    service.rpcTokenManager = { clear: vi.fn() }
    service.suspendDesktopGfsUploadsForAuthBoundary = vi
      .fn()
      .mockRejectedValue(new Error('state file is read-only'))
    const clearAuthenticatedSessionState = service.clearAuthenticatedSessionState.bind(service)
    let depthDuringCleanup = 0
    service.clearAuthenticatedSessionState = vi.fn(() => {
      depthDuringCleanup = service.gfsTransientTeamHopDepth
      clearAuthenticatedSessionState()
    })

    await expect(service.switchTeam('team-b')).rejects.toThrow('state file is read-only')

    expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
    expect(service.tokenStore.clearSessionToken).toHaveBeenCalledWith(expect.any(String), {
      legacyEnvKeys: expect.any(Array),
    })
    expect(depthDuringCleanup).toBeGreaterThan(0)
    expect(service.sessionToken).toBeNull()
    expect(service.me).toBeNull()
    expect(service.gfsScopeIdentity).toBeNull()
    expect(service.gfsDispatchBlocked).toBe(true)
    expect(service.gfsTransientTeamHopDepth).toBe(0)
  })

  it('does not fence GFS uploads when an explicit switchTeam is rejected', async () => {
    const service = new AppService() as any
    service.sessionToken = 'team-a-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: 'team-a',
      teamName: 'Team A',
      role: 'member',
    }
    service.gfsScopeIdentity = {
      ownerId: 'user-1',
      teamId: 'team-a',
      environmentKey: 'test-env',
      baseUrl: 'https://external.example',
    }
    service.authClient = {
      switchTeam: vi.fn().mockRejectedValue(new Error('team switch rejected')),
    }
    service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn()

    await expect(service.switchTeam('team-b')).rejects.toThrow('team switch rejected')
    expect(service.suspendDesktopGfsUploadsForAuthBoundary).not.toHaveBeenCalled()
  })

  it('uses the teamless session when a directly granted agent needs an RPC token', async () => {
    const service = new AppService() as any
    service.sessionToken = 'teamless-token'
    service.me = {
      id: 'user-1',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: null,
      teamName: null,
      role: 'member',
    }
    service.accessCatalog = {
      userId: 'user-1',
      teamId: null,
      userContextIds: [],
      userAgentNames: ['pro-agent'],
      teamContextIds: [],
      teamAgentNames: [],
      contextIds: [],
      agentNames: ['pro-agent'],
      mcpServersByAgent: { 'pro-agent': [] },
      agentContextByName: { 'pro-agent': null },
    }
    service.authClient = {
      getMe: vi.fn().mockResolvedValue(service.me),
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({
        token: 'user-rpc-test-token',
        accessScope: 'user',
        teamId: null,
      }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),
    }

    await expect(
      service.invokeHostMessage('pro-agent', { content: 'hello' }, ['pro-agent'])
    ).resolves.toEqual({ success: true, response: 'ok' })
    expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(
      'teamless-token',
      ['host:message:invoke', 'host:task:read', 'host:wake:write'],
      ['pro-agent']
    )
    expect(service.rpcClient.invokeHostMessage).toHaveBeenCalled()
  })

  it('uses the access catalog response without probing host status', async () => {
    const service = new AppService() as any
    service.sessionToken = 'session-token'
    service.me = {
      id: '00000000-0000-4000-8000-000000000001',
      email: '[email protected]',
      name: 'Test User',
      picture: null,
      teamId: '00000000-0000-4000-8000-0000000000aa',
      teamName: 'Test Team',
      role: 'member',
    }
    service.authClient = {
      getMe: vi.fn().mockResolvedValue(service.me),
      getMyContexts: vi.fn().mockResolvedValue({ contextIds: [] }),
      getMyAgents: vi.fn().mockResolvedValue({
        agentNames: ['allowed-agent', 'denied-agent'],
        agents: [
          { name: 'allowed-agent', mcpServers: [] },
          { name: 'denied-agent', mcpServers: [] },
        ],
      }),
      getTeamContexts: vi.fn().mockResolvedValue({ contextIds: [] }),
      getTeamAgents: vi.fn().mockResolvedValue({ agentNames: [], agents: [] }),
    }
    service.rpcTokenManager = {
      getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),
      clear: vi.fn(),
    }
    service.rpcClient = {
      getHostStatus: vi.fn((_token: string, hostRef: string) => {
        if (hostRef === 'denied-agent') {
          throw new ApiError(
            '403 Forbidden: Forbidden: user cannot access this host',
            403,
            '{"error":"Forbidden: user cannot access this host"}'
          )
        }
        return Promise.resolve({ queue: { pending: 0, processing: 0 } })
      }),
    }

    const catalog = await service.refreshAccessCatalog()

    expect(catalog.agentNames).toEqual(['allowed-agent', 'denied-agent'])
    expect(catalog.userAgentNames).toEqual(['allowed-agent', 'denied-agent'])
    expect(catalog.mcpServersByAgent).toEqual({ 'allowed-agent': [], 'denied-agent': [] })
    expect(service.rpcTokenManager.clear).toHaveBeenCalledTimes(1)
    expect(service.rpcClient.getHostStatus).not.toHaveBeenCalled()
  })
})