Skip to content

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

TypeScript
4.1 KB
Raw Blame
/**
 * #666 — the per-message gate between the Host's GFS token and file
 * reference resolution.
 */
import { describe, expect, it, vi } from 'vitest'
import type { GfsToolScopeInspection } from '../../internalTools/gfsClient'
import { createFileReferenceGfsGate } from '../fileReferenceGfsGate'
import type { FileReferenceGfscClient } from '../fileReferenceResolver'

function gate(inspection: GfsToolScopeInspection, approvalTools?: Record<string, boolean>) {
  const client: FileReferenceGfscClient = { resolve: vi.fn() }
  const logger = { warn: vi.fn(), error: vi.fn() }
  const inspectScopes = vi.fn(() => inspection)
  const access = createFileReferenceGfsGate({
    inspectScopes,
    approvalTools: () => approvalTools,
    client,
    logger,
  })
  return { access, client, logger, inspectScopes }
}

describe('createFileReferenceGfsGate (#666)', () => {
  it('returns the client for a token with gfs.read, and logs nothing', () => {
    const { access, client, logger, inspectScopes } = gate({
      status: 'ok',
      scopes: new Set(['gfs.read']),
    })
    expect(access()).toEqual({ status: 'available', client })
    // Witness: the token was inspected for this message.
    expect(inspectScopes).toHaveBeenCalledTimes(1)
    expect(logger.warn).not.toHaveBeenCalled()
    expect(logger.error).not.toHaveBeenCalled()
  })

  it.each([
    ['no token', { status: 'not_configured' } as const, 'not_configured'],
    [
      'a scope outside the allowlist',
      { status: 'scope_outside_allowlist' } as const,
      'scope_outside_allowlist',
    ],
    [
      'gfs.write only',
      { status: 'ok', scopes: new Set(['gfs.write'] as const) } as const,
      'missing_gfs_read',
    ],
    ['no scopes', { status: 'ok', scopes: new Set<never>() } as const, 'missing_gfs_read'],
  ])('answers unsupported for %s and warns with reason %s', (_label, inspection, reason) => {
    const { access, logger } = gate(inspection)
    expect(access()).toEqual({ status: 'unsupported' })
    expect(logger.warn).toHaveBeenCalledTimes(1)
    expect(logger.warn).toHaveBeenCalledWith(
      { event: 'file_reference_gfs_unavailable', reason },
      'Host runtime event'
    )
    expect(logger.error).not.toHaveBeenCalled()
  })

  it.each(['clerum__gfs_stat', 'clerum__gfs_resolve', 'clerum__gfs_read'])(
    'answers unsupported when approval.tools forces %s to approval, before asking gfsc',
    tool => {
      const { access, logger, inspectScopes } = gate(
        { status: 'ok', scopes: new Set(['gfs.read']) },
        { [tool]: true }
      )
      expect(access()).toEqual({ status: 'unsupported' })
      expect(logger.warn).toHaveBeenCalledWith(
        { event: 'file_reference_gfs_unavailable', reason: 'read_tool_requires_approval' },
        'Host runtime event'
      )
      // Witness: the decision came from the approval override, not the token.
      expect(inspectScopes).not.toHaveBeenCalled()
    }
  )

  it.each([
    ['an override that waives approval', { clerum__gfs_read: false }],
    ['an override on a tool admission does not use', { clerum__gfs_write: true }],
    ['no overrides at all', undefined],
  ])('keeps the client available with %s', (_label, approvalTools) => {
    const { access, client, logger, inspectScopes } = gate(
      { status: 'ok', scopes: new Set(['gfs.read']) },
      approvalTools
    )
    expect(access()).toEqual({ status: 'available', client })
    // Witness: the token was inspected, so the approval check let the message through.
    expect(inspectScopes).toHaveBeenCalledTimes(1)
    expect(logger.warn).not.toHaveBeenCalled()
  })

  it.each([
    ['token_unreadable', 'TokenReadError'],
    ['token_undecodable', 'TokenDecodeError'],
  ] as const)('refuses on %s with %s and logs an error', (status, errorClass) => {
    const { access, logger } = gate({ status })
    expect(access()).toEqual({ status: 'credentials_failed', errorClass })
    expect(logger.error).toHaveBeenCalledTimes(1)
    expect(logger.error).toHaveBeenCalledWith(
      { event: 'file_reference_gfs_unavailable', reason: status },
      'Host runtime event'
    )
    expect(logger.warn).not.toHaveBeenCalled()
  })
})