Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

15.5 KB
Raw Blame
import { classifyFailure, errorFocus, looksLikeCredential, secretRefFix } from './diagnose'
import type { BuildWarning } from './build-warnings'
import { formatBytes } from './check'
import { SIZES, type Size } from './types'

/**
 * Everything an agent needs about one environment, as returned by
 * `GET /v1/apps/{app}/envs/{env}/context` and printed by `boxlite app status --details`.
 * Secret values never appear: variables that come from `[secrets]` carry `value: null`.
 */
export interface AgentContext {
  app: string
  env: string
  control_plane: string
  generated_at: string
  /** new | deploying | live | live (last deployment failed) | failed */
  status: string
  urls: { app: string | null; health: string | null; login: string | null }
  access: { level: string; source: 'manifest' | 'online' }
  environments: { name: string; status: string; url: string | null }[]
  live: { deployment: number; spec_version: number | null; live_at: string | null } | null
  /** `usage` is read live from the running Box: absent from an older control plane, null while it sleeps. */
  box: { id: string; state: string; usage?: BoxUsage | null } | null
  runtime: { image: string; size: string; build: string[] } | null
  services: AgentService[]
  schedules: AgentSchedule[]
  database: AgentDatabase | null
  volumes: { name: string; mount: string; mode: string }[]
  auth: {
    enabled: boolean
    providers: { name: string; status: string }[]
    protect: string[]
    login_url: string | null
    users: number
  }
  variables: AgentVariable[]
  /** Newest first, at most 5. */
  deployments: AgentDeployment[]
  /** Newest last: error-level lines of the last 24 h, at most 20. */
  recent_errors: { ts: string; source: string; line: string; deployment: number | null }[]
  /** What the live deployment's build warned about (npm audit, bundle size, deprecated packages). Absent from an older control plane. */
  build_warnings?: BuildWarning[]
  /** What earlier sessions on this App wrote down for the next one (`app remember "<fact>"`), newest first, at most 20. Absent from an older control plane. */
  notes?: AppNote[]
  /** Info findings acknowledged on purpose (`app ack`): out of next_actions while they stay the same. Absent from an older control plane. */
  acknowledged?: AcknowledgedFinding[]
  /** Ranked problems with fixes, worst first; empty when everything looks fine. */
  next_actions: Finding[]
}

/** The memory and disk of the whole Box (volumes are directories on its disk). */
export interface BoxUsage {
  memory_total_bytes: number
  memory_available_bytes: number
  disk_total_bytes: number
  disk_free_bytes: number
}

/** A fact or decision an agent kept for the next session (App notes). */
export interface AppNote {
  id: number
  text: string
  by: string
  created_at: string
}

export interface AcknowledgedFinding {
  id: string
  title: string
  reason: string
  by: string
  at: string
}

export interface AgentService {
  name: string
  exposed: boolean
  command: string | null
  static: string | null
  health_path: string | null
  /** From the supervisor; null before the first deploy. */
  state: string | null
  health: string | null
  restarts: number | null
  last_exit_code: number | null
  /** Resident memory of the service and its children while the Box runs; absent from an older control plane. */
  memory_bytes?: number | null
}

export interface AgentSchedule {
  name: string
  cron: string
  target: string
  next_run: string | null
  last_run: string | null
  last_status: number | null
}

export interface AgentDatabase {
  status: 'ready' | 'unreachable' | 'not_provisioned'
  branch: string | null
  error?: string
  tables: { name: string; columns: { name: string; type: string; nullable: boolean; default: string | null }[] }[]
  migrations: {
    file: string
    status: 'applied' | 'pending' | 'changed'
    deployment: number | null
    applied_at: string | null
  }[]
}

export interface AgentVariable {
  name: string
  /** null for secrets and for platform values that are themselves credentials (DATABASE_URL). */
  value: string | null
  secret: boolean
  /** `boxlite.toml`, `secret org/openai`, or `platform`. */
  source: string
  /** False when a `[secrets]` reference names a secret (App or org) that does not exist. */
  resolved: boolean
}

export interface AgentDeployment {
  number: number
  kind: string
  status: string
  message: string | null
  created_at: string
  finished_at: string | null
  spec_version: number | null
  error: { step: string; message: string; log_tail?: string[] } | null
}

export type Severity = 'error' | 'warn' | 'info'

export interface Finding {
  /** Stable id, e.g. `deploy_failed`, `crash_loop`, `auth_not_ready`. */
  id: string
  severity: Severity
  title: string
  /** A concrete command or file edit. */
  fix: string
  detail?: string
  evidence?: string[]
}

const RANK: Record<Severity, number> = { error: 0, warn: 1, info: 2 }

export function rankFindings(findings: Finding[]): Finding[] {
  return findings
    .map((f, i) => ({ f, i }))
    .sort((a, b) => RANK[a.f.severity] - RANK[b.f.severity] || a.i - b.i)
    .map((x) => x.f)
}

/** Restarts above this count mean the service is crash-looping rather than recovering. */
export const CRASH_LOOP_RESTARTS = 3

/** Problems visible from the control plane's side of one environment. */
export function contextFindings(c: Omit<AgentContext, 'next_actions'>): Finding[] {
  const out: Finding[] = []
  const latest = c.deployments[0]
  const exposed = c.services.find((s) => s.exposed)

  if (!latest) {
    out.push({
      id: 'not_deployed',
      severity: 'info',
      title: `${c.env} has never been deployed`,
      fix: 'run `boxlite app deploy --check`, then `boxlite app deploy -m "first version"`',
    })
  } else if (latest.status === 'failed' && latest.error) {
    const d = classifyFailure({
      step: latest.error.step,
      message: latest.error.message,
      log_tail: latest.error.log_tail,
      deployment: latest.number,
      service: exposed
        ? { name: exposed.name, command: exposed.command ?? undefined, health: exposed.health_path ?? undefined }
        : undefined,
    })
    out.push({
      id: 'deploy_failed',
      severity: 'error',
      title: `deployment #${latest.number} failed at ${latest.error.step}: ${d.summary}`,
      fix: d.fix,
      detail: c.live ? `#${c.live.deployment} is still serving` : 'nothing is serving yet',
      evidence: d.evidence,
    })
  } else if (['queued', 'uploading', 'building', 'migrating', 'starting'].includes(latest.status)) {
    out.push({
      id: 'deploy_in_progress',
      severity: 'info',
      title: `deployment #${latest.number} is ${latest.status}`,
      fix: `follow it: \`boxlite app logs --deployment ${latest.number} --follow\``,
    })
  }

  for (const s of c.services) {
    const errs = c.recent_errors
      .filter((e) => e.source === s.name)
      .slice(-5)
      .map((e) => e.line)
    if ((s.restarts ?? 0) > CRASH_LOOP_RESTARTS) {
      // The same classifier as a failed deploy: a missing variable or module is named with its fix.
      const own = c.recent_errors.filter((e) => e.source === s.name).map((e) => e.line)
      const tail = c.recent_errors
        .filter((e) => e.source === s.name || e.source === 'system')
        .map((e) => `[${e.source}] ${e.line}`)
      const d = classifyFailure({
        step: 'start',
        message: '',
        log_tail: tail,
        service: { name: s.name, command: s.command ?? undefined },
      })
      const named = d.cause === 'missing_variable' || d.cause === 'module_not_found'
      out.push({
        id: 'crash_loop',
        severity: 'error',
        title: named
          ? `service ${s.name} keeps crashing: ${d.summary}`
          : `service ${s.name} restarted ${s.restarts} times${s.last_exit_code != null ? ` (last exit code ${s.last_exit_code})` : ''}`,
        fix: named
          ? d.fix
          : `read its stderr: \`boxlite app logs --source ${s.name} --level error --since 1h\`, fix the crash, then \`boxlite app deploy\``,
        evidence: errorFocus(own, 5),
      })
    } else if (s.state === 'failed' || s.state === 'stopped') {
      out.push({
        id: 'service_down',
        severity: 'error',
        title: `service ${s.name} is ${s.state}`,
        fix: `\`boxlite api POST /apps/${c.app}/envs/${c.env}/services/${s.name}/restart\`; if it stops again, \`boxlite app logs --source ${s.name} --since 1h\``,
        evidence: errs,
      })
    }
  }

  if (c.database) {
    if (c.database.status === 'unreachable') {
      out.push({
        id: 'db_unreachable',
        severity: 'error',
        title: 'the database does not answer',
        fix: `retry \`boxlite api GET /apps/${c.app}/envs/${c.env}/db\` in a minute and report any persistent error`,
        detail: c.database.error,
      })
    }
    const changed = c.database.migrations.filter((m) => m.status === 'changed')
    for (const m of changed) {
      out.push({
        id: 'migration_changed',
        severity: 'error',
        title: `${m.file} differs from the version applied in #${m.deployment ?? '?'}`,
        fix: `restore the applied contents of ${m.file} and add the change as a new file: \`boxlite app psql --migration <name>\``,
      })
    }
    const pending = c.database.migrations.filter((m) => m.status === 'pending')
    if (pending.length) {
      out.push({
        id: 'pending_migrations',
        severity: 'warn',
        title: `${pending.length} migration(s) not applied: ${pending.map((m) => m.file).join(', ')}`,
        fix: '`boxlite app deploy --config-only --yes` (or `boxlite app deploy`) runs them',
      })
    }
  }

  if (c.auth.enabled) {
    const ready = c.auth.providers.filter((p) => p.status === 'ready')
    // email needs the App's own mail sender, which the agent sets up; the OAuth ones need the platform admin.
    if (c.auth.providers.some((p) => p.name === 'email' && p.status !== 'ready')) {
      out.push({
        id: 'email_sender_missing',
        severity: ready.length ? 'warn' : 'error',
        title: 'email sign-in has no mail sender: visitors cannot get codes',
        fix: 'ask the user for the SMTP account and configure and test the mail sender in the Dashboard’s Authentication tab',
      })
    }
    const missing = c.auth.providers.filter((p) => p.status !== 'ready' && p.name !== 'email').map((p) => p.name)
    if (!ready.length && missing.length)
      out.push({
        id: 'auth_not_ready',
        severity: 'error',
        title: `sign-in is on but no provider is configured on this platform (${missing.join(', ')})`,
        fix: 'ask the platform admin to configure the OAuth credentials; test the sign-in flow in the verification environment',
      })
    else if (missing.length)
      out.push({
        id: 'auth_partial',
        severity: 'info',
        title: `sign-in: ${missing.join(', ')} not configured on this platform; ${ready.map((p) => p.name).join(', ')} works`,
        fix: `remove ${missing.join(', ')} from [auth].providers in boxlite.toml, or leave it: the login page only offers ready providers`,
      })
  }

  const unresolved = c.variables.filter((v) => v.secret && !v.resolved)
  if (unresolved.length) {
    out.push({
      id: 'secret_missing',
      severity: 'error',
      title: `secret(s) not set on the platform: ${unresolved.map((v) => `${v.name} (${v.source})`).join(', ')}`,
      fix: 'for a missing App secret, pipe the value into `boxlite app vars set VARIABLE --secret` and deploy the new reference; ask an org admin to create a missing org secret',
    })
  }

  // Advisor: a credential kept as a plain variable is readable by anyone who can open the console or the CLI.
  const plain = c.variables.filter((v) => !v.secret && looksLikeCredential(v.name))
  if (plain.length) {
    const names = plain.map((v) => v.name)
    out.push({
      id: 'plain_credential',
      severity: 'info',
      title: `${names.join(', ')} ${names.length === 1 ? 'looks like a credential but is a plain variable' : 'look like credentials but are plain variables'}, readable in the console and \`boxlite app vars list\``,
      fix: names.map((n) => `${n}: ${secretRefFix(n)}`).join('; '),
    })
  }

  const recent = c.recent_errors.filter((e) => Date.parse(c.generated_at) - Date.parse(e.ts) < 3_600_000)
  if (recent.length && !out.some((f) => f.id === 'crash_loop')) {
    const sources = [...new Set(recent.map((e) => e.source))]
    out.push({
      id: 'recent_errors',
      severity: 'warn',
      title: `${recent.length} error line(s) in the last hour from ${sources.join(', ')}`,
      fix: `\`boxlite app logs --level error --since 1h\``,
      evidence: recent.slice(-5).map((e) => `[${e.source}] ${e.line}`),
    })
  }

  // The app runs, and its build still left something to review: shown beside it, never as a failure.
  for (const w of c.build_warnings ?? []) {
    out.push({
      id: `build_${w.id}`,
      severity: w.id === 'npm_audit' && /high|critical/i.test(w.title) ? 'warn' : 'info',
      title: w.title,
      fix: w.fix,
      evidence: w.evidence,
    })
  }

  for (const f of usageFindings(c)) out.push(f)

  if (c.box?.state === 'stopped')
    out.push({
      id: 'box_asleep',
      severity: 'info',
      title: 'the app is asleep (it stops after 15 min without traffic)',
      fix: 'nothing to do: the next request wakes it in a few seconds',
    })
  if (c.access.level === 'public' && !c.auth.enabled)
    out.push({
      id: 'public_anonymous',
      severity: 'info',
      title: 'the app is public and has no sign-in: anyone with the URL can use it',
      fix: 'keep it only if the user asked for a public site; otherwise select private access in the Dashboard',
    })
  return rankFindings(out)
}

/** Below this share free, the Box is close enough to its memory or disk limit to say so. */
export const LOW_FREE_SHARE = 0.1

/** The Box near its memory or disk limit, with the service using the most memory and what to change. */
function usageFindings(c: Omit<AgentContext, 'next_actions'>): Finding[] {
  const u = c.box?.usage
  if (!u) return []
  const out: Finding[] = []
  if (u.memory_total_bytes > 0 && u.memory_available_bytes < u.memory_total_bytes * LOW_FREE_SHARE) {
    const top = [...c.services]
      .filter((s) => s.memory_bytes != null)
      .sort((a, b) => b.memory_bytes! - a.memory_bytes!)[0]
    const sizes = Object.keys(SIZES) as Size[]
    const size = c.runtime?.size as Size | undefined
    const bigger = size ? sizes[sizes.indexOf(size) + 1] : undefined
    out.push({
      id: 'memory_low',
      severity: 'warn',
      title: `the Box is nearly out of memory: ${formatBytes(u.memory_available_bytes)} free of ${formatBytes(u.memory_total_bytes)}${top ? `; ${top.name} uses ${formatBytes(top.memory_bytes!)}` : ''}`,
      fix: bigger
        ? `set \`size = "${bigger}"\` under [runtime] in boxlite.toml (${SIZES[bigger].memory_mib} MiB), then \`boxlite app deploy\`; or lower the memory use of ${top?.name ?? 'the app'}`
        : `lower the memory use of ${top?.name ?? 'the app'} (${size ?? 'this'} is the largest size): cache less in process, stream large files, cap worker counts`,
      detail: 'a service that runs out of memory is killed (exit 137) and restarted',
    })
  }
  if (u.disk_total_bytes > 0 && u.disk_free_bytes < u.disk_total_bytes * LOW_FREE_SHARE) {
    out.push({
      id: 'disk_low',
      severity: 'warn',
      title: `the Box disk is nearly full: ${formatBytes(u.disk_free_bytes)} free of ${formatBytes(u.disk_total_bytes)}`,
      fix: 'open the Volumes tab in the Dashboard, inspect large files and remove unneeded data; check which service writes it',
      detail: 'writes fail once it is full; volumes and the last 3 releases share this disk',
    })
  }
  return out
}