Skip to content

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

19.0 KB
Raw Blame
/** Builds the console's app summary and detail views from real CP state. */
import { secretValue } from './orgs'
import { errorFocus, formatBytes, LOW_FREE_SHARE, missingVariableIn, type BoxUsage } from '@boxlite-ai/cloud-app-shared'
import type { AppRow, Deployment, EnvRow } from './db'
import { ACTIVE_STATUSES, now } from './db'
import type { Ctx } from './ctx'
import { schedulesOf } from './agent-context'
import type { AccessLog } from './console-access'
import { activityView } from './console-activity'
import { failureView } from './console-deployment'
import { endUserAuth } from './console-enduser'
import { handoffPrompt } from './console-prompt'
import { lastDeploy, releaseId, releaseViews, runningOf } from './console-releases'
import type {
  AppDetailView,
  AppSummaryView,
  BoxUsageView,
  ConsoleStatus,
  DatabaseSummaryView,
  EnvironmentSummaryView,
  ProblemView,
  ServiceView,
  VolumeView,
} from './console-view-types'
import { BOX_SIZES } from './platform'
import { storedServices } from './resources'
import { volumeName } from './resources/volumes'
import { dbCoordinates } from './services/db'
import type { Spec } from './spec'
import type { ServiceState } from './supervisor'
import { logLevel } from './services/logs'
import { listDomains } from './services/domains'
import { previewAttempt, previewMeta, refreshPreviewIfStale } from './preview'

/**
 * Builds the console's view objects (the shapes of the dashboard's lib/apps/types.ts) from real CP
 * state. Where the CP has no data a field is null or empty and the console hides it; nothing here
 * invents a number.
 *
 * Split by concern: view types in console-view-types.ts, releases in console-releases.ts, the
 * hand-off prompt in console-prompt.ts; this file builds the app summary and detail views.
 */

// ---- small helpers -----------------------------------------------------------

/** Runtime state of an env in the console's words. `box` is the BoxLite status, `services` the supervisor's report. */
export async function envStatus(
  ctx: Pick<Ctx, 'store'>,
  env: EnvRow,
  box: string | null,
  services: ServiceState[],
): Promise<ConsoleStatus> {
  const latest = (await ctx.store.deployments(env.id, 1))[0]
  if (!latest) return 'draft'
  if (ACTIVE_STATUSES.includes(latest.status)) return 'building'
  // An apply before any deploy is "live" in the CP but nothing runs yet.
  if (!env.live_deployment || !env.active_box_id) return latest.status === 'failed' ? 'failed' : 'draft'
  if (box === 'stopped') return 'sleeping'
  if (services.some((s) => s.state === 'failed' || s.health === 'unhealthy' || crashLooping(s))) return 'degraded'
  return 'live'
}

/** The supervisor keeps restarting a service that keeps exiting non-zero: it never serves, though never "failed". */
const crashLooping = (s: { state: string; restarts?: number; last_exit_code?: number | null; lastExitCode?: number }) =>
  s.state === 'restarting' && (s.restarts ?? 0) >= 3 && (s.last_exit_code ?? s.lastExitCode ?? 0) !== 0

function failureLine(d: Deployment): string | undefined {
  if (d.status !== 'failed' || !d.error) return undefined
  return `#${d.number} failed at ${d.error.step}: ${d.error.message}`
}

// ---- BoxLite volumes (size/state when the API reports them) ------------------

export async function volumeViews(
  ctx: Pick<Ctx, 'volumes'>,
  app: AppRow,
  env: EnvRow,
  spec: Spec | undefined,
): Promise<VolumeView[]> {
  const declared = spec?.volumes ?? []
  if (!declared.length) return []
  const remote = await ctx.volumes.list(app, env)
  return declared.map((v) => {
    const name = volumeName(app.name, env.name, v.name)
    const r = remote.find((x) => x.name === name)
    return {
      name: v.name,
      mountPath: v.mount,
      mode: v.mode,
      volumeName: name,
      volumeId: r?.id ?? null,
      sizeBytes: r?.sizeBytes ?? null,
      state: r?.state ?? null,
    }
  })
}

// ---- summary / detail ----------------------------------------------------------------

export interface LiveState {
  box: string | null
  services: ServiceState[]
  /** Only from a probe of a running Box. */
  usage: BoxUsage | null
}

/** BoxLite status of the env's Box (cached by Resources) and its services: probed when asked, else the last report. */
async function liveState(ctx: Ctx, env: EnvRow, opts: { probe?: boolean } = {}): Promise<LiveState> {
  if (opts.probe) {
    const { box, services, usage } = await ctx.resources.liveServices(env)
    return { box, services, usage }
  }
  const box = env.active_box_id ? await ctx.resources.boxStatus(env.active_box_id).catch(() => 'unknown') : null
  return { box, services: storedServices(env), usage: null }
}

export async function summaryView(ctx: Ctx, app: AppRow): Promise<AppSummaryView> {
  const envs = await ctx.store.envs(app.id)
  const main = envs.find((e) => e.name === 'main') ?? envs[0]!
  const state = await liveState(ctx, main)
  const latest = (await ctx.store.deployments(main.id, 1))[0]
  const spec = await ctx.store.spec(main.id)
  const status = await envStatus(ctx, main, state.box, state.services)
  const attention = latest ? failureLine(latest) : undefined
  const step =
    latest && ACTIVE_STATUSES.includes(latest.status)
      ? (latest.steps.find((s) => s.status === 'running')?.name ?? latest.status)
      : null
  return {
    id: app.name,
    name: app.name,
    slug: app.name,
    url: main.public_url ? `${main.public_url}/` : null,
    previewSrc: await (async () => {
      const shot = main.live_deployment ? await previewMeta(ctx, main.id) : null
      return shot
        ? `/apps/${encodeURIComponent(app.name)}/preview?env=main&v=${encodeURIComponent(shot.capturedAt)}`
        : null
    })(),
    visibility: await ctx.resources.effectiveAccess(main),
    status,
    lastDeploy: lastDeploy(latest),
    resources: { database: !!main.db_url, storage: spec?.volumes.length ?? 0, users: null },
    createdAt: app.created_at,
    environments: envs.length,
    liveIn: envs.filter((e) => e.id !== main.id && e.live_deployment).map((e) => e.name),
    ...(attention ? { attention } : {}),
    ...(step ? { progress: `Deploying ${releaseId(latest!.number)} · ${step}` } : {}),
  }
}

export async function environmentViews(ctx: Ctx, app: AppRow): Promise<EnvironmentSummaryView[]> {
  return Promise.all(
    (await ctx.store.envs(app.id)).map(async (env) => {
      const state = await liveState(ctx, env)
      return {
        name: env.name,
        status: await envStatus(ctx, env, state.box, state.services),
        url: env.public_url ? `${env.public_url}/` : null,
        box: env.active_box_id,
        lastDeploy: lastDeploy((await ctx.store.deployments(env.id, 1))[0]),
        from: env.from_env,
      }
    }),
  )
}

/** An asleep Box runs nothing, whatever the supervisor last reported. */
const asleep = (sv: ServiceView): ServiceView => ({
  ...sv,
  state: 'asleep',
  health: 'none',
  port: undefined,
  pid: undefined,
  recentErrors: [],
})

/** A service is in trouble when it crashed, keeps restarting, fails its health check or last exited non-zero. */
const troubled = (r: ServiceState | undefined) =>
  !!r && (r.state === 'failed' || r.state === 'restarting' || r.health === 'unhealthy' || (r.last_exit_code ?? 0) !== 0)

/** The service's last error lines since the serving deployment started: earlier ones are history, not its trouble now. */
async function recentErrors(ctx: Pick<Ctx, 'store'>, env: EnvRow, service: string): Promise<string[]> {
  const serving = env.live_deployment ? await ctx.store.deployment(env.id, env.live_deployment) : undefined
  return (
    await ctx.store.logs(env.id, {
      source: service,
      limit: 200,
      ...(serving?.started_at ? { since: serving.started_at } : {}),
    })
  )
    .filter((l) => logLevel(l.level) === 'error')
    .slice(-8)
    .map((l) => `${l.ts} ${l.line}`)
}

async function serviceViews(
  ctx: Pick<Ctx, 'store'>,
  env: EnvRow,
  spec: Spec | undefined,
  reported: ServiceState[],
): Promise<ServiceView[]> {
  const declared = spec?.services ?? []
  const exposedName = declared.find((s) => s.port !== undefined || s.static)?.name
  const names = [...new Set([...declared.map((s) => s.name), ...reported.map((s) => s.name)])]
  return await Promise.all(
    names.map(async (name) => {
      const d = declared.find((s) => s.name === name)
      const r = reported.find((s) => s.name === name)
      return {
        name,
        command: d?.command ? [d.command] : d?.static ? [`static ${d.static}`] : [],
        exposed: name === exposedName,
        ...(r?.port ? { port: r.port } : {}),
        ...(d?.health ? { healthPath: d.health } : {}),
        ...(d?.static ? { staticDir: d.static } : {}),
        state: r?.state ?? 'stopped',
        ...(r?.pid ? { pid: r.pid } : {}),
        restarts: r?.restarts ?? 0,
        ...(r?.last_exit_code !== undefined && r?.last_exit_code !== null ? { lastExitCode: r.last_exit_code } : {}),
        health: r?.health ?? 'none',
        ...(r?.memory_bytes != null ? { memoryBytes: r.memory_bytes } : {}),
        recentErrors: troubled(r) ? await recentErrors(ctx, env, name) : [],
      }
    }),
  )
}

/** The Box near its memory or disk limit, said plainly; the agent gets the commands in fixPrompt. */
function usageProblems(app: AppRow, env: EnvRow, usage: BoxUsage | null, services: ServiceView[]): ProblemView[] {
  if (!usage) return []
  const out: ProblemView[] = []
  const where = `the BoxLite app "${app.name}" (environment ${env.name})`
  if (usage.memory_total_bytes > 0 && usage.memory_available_bytes < usage.memory_total_bytes * LOW_FREE_SHARE) {
    const top = services.filter((s) => s.memoryBytes !== undefined).sort((a, b) => b.memoryBytes! - a.memoryBytes!)[0]
    const uses = top ? `; ${top.name} uses ${formatBytes(top.memoryBytes!)}` : ''
    out.push({
      summary: 'Running low on memory',
      detail: `${formatBytes(usage.memory_available_bytes)} free of ${formatBytes(usage.memory_total_bytes)}${uses}. A service that runs out of memory is stopped and restarted.`,
      fixPrompt: `${where} is nearly out of memory: ${formatBytes(usage.memory_available_bytes)} free of ${formatBytes(usage.memory_total_bytes)}${uses}.\nRead \`boxlite app status --details --app ${app.name} --env ${env.name}\` for the recommended fix. Reduce memory use or choose a larger [runtime].size in boxlite.toml, then run \`boxlite app deploy --app ${app.name} --env ${env.name}\`.`,
    })
  }
  if (usage.disk_total_bytes > 0 && usage.disk_free_bytes < usage.disk_total_bytes * LOW_FREE_SHARE) {
    out.push({
      summary: 'The disk is nearly full',
      detail: `${formatBytes(usage.disk_free_bytes)} free of ${formatBytes(usage.disk_total_bytes)}. Saving files fails once it is full.`,
      fixPrompt: `${where} has ${formatBytes(usage.disk_free_bytes)} of disk left of ${formatBytes(usage.disk_total_bytes)}.\nOpen the Volumes tab for ${app.name}/${env.name}, inspect large files, remove unneeded data, and fix the service that fills the disk.`,
    })
  }
  return out
}

async function problemsOf(
  ctx: Pick<Ctx, 'store'>,
  app: AppRow,
  env: EnvRow,
  latest: Deployment | undefined,
  services: ServiceView[],
  reportedAny: boolean,
): Promise<ProblemView[]> {
  const out: ProblemView[] = []
  const failure = latest ? await failureView(ctx, app, env, latest) : null
  if (latest && failure) {
    out.push({
      summary: `Deployment #${latest.number} failed at ${failure.step}`,
      // The fix itself is written for the agent (commands, files): it travels in fixPrompt.
      detail: `${failure.diagnosis.summary.charAt(0).toUpperCase()}${failure.diagnosis.summary.slice(1)}.`,
      fixPrompt: failure.fixPrompt,
      deployment: latest.number,
      ...(env.live_deployment && env.active_box_id ? { serving: releaseId(env.live_deployment) } : {}),
    })
  }
  if (reportedAny && env.live_deployment) {
    for (const s of services) {
      const looping = crashLooping(s)
      if (s.state !== 'failed' && s.health !== 'unhealthy' && !looping) continue
      // Its own last words say more than any counter: "Error: SLACK_TOKEN is not set".
      const said = errorFocus(s.recentErrors, 1)[0]?.replace(/^\S+Z /, '')
      const exits = s.lastExitCode !== undefined ? `exited with code ${s.lastExitCode}` : 'stopped'
      out.push({
        summary:
          s.state === 'failed'
            ? `${s.name} has crashed`
            : looping
              ? `${s.name} keeps crashing`
              : `${s.name} is failing its health check`,
        detail:
          s.health === 'unhealthy' && !looping && s.state !== 'failed'
            ? `It answers ${s.healthPath ?? 'its health check'} with an error${said ? `: "${said}"` : ''}.`
            : `It ${exits}${said ? `: "${said}"` : ''}. Restarted ${s.restarts} time${s.restarts === 1 ? '' : 's'} so far.`,
        fixPrompt: `The service "${s.name}" of the BoxLite app "${app.name}" (environment ${env.name}) is ${s.state === 'failed' || looping ? 'crashing' : 'failing its health check'}.\nRead \`boxlite app logs --source ${s.name} --since 30m --app ${app.name} --env ${env.name}\` and \`boxlite app status --services --app ${app.name} --env ${env.name}\`, fix the cause, then run \`boxlite app deploy --app ${app.name} --env ${env.name}\`.`,
        ...(said && missingVariableIn(said) ? { missingVariable: missingVariableIn(said) } : {}),
      })
    }
  }
  return out
}

const usageView = (u: BoxUsage | null): BoxUsageView | null =>
  u
    ? {
        memoryTotalBytes: u.memory_total_bytes,
        memoryAvailableBytes: u.memory_available_bytes,
        diskTotalBytes: u.disk_total_bytes,
        diskFreeBytes: u.disk_free_bytes,
      }
    : null

export async function detailView(ctx: Ctx, access: AccessLog, app: AppRow, env: EnvRow): Promise<AppDetailView> {
  const state = await liveState(ctx, env, { probe: true })
  const fresh = (await ctx.store.envById(env.id)) ?? env
  const latest = (await ctx.store.deployments(fresh.id, 1))[0]
  const spec = await ctx.store.spec(fresh.id)
  const current = await ctx.resources.currentSpec(fresh)
  const status = await envStatus(ctx, fresh, state.box, state.services)
  const services = (await serviceViews(ctx, fresh, current ?? spec, state.services)).map((sv) =>
    state.box === 'stopped' ? asleep(sv) : sv,
  )
  const size = current ? BOX_SIZES[current.runtime.size] : undefined
  const attention = latest ? failureLine(latest) : undefined

  let database: DatabaseSummaryView | null = null
  if (fresh.db_url) {
    const db = dbCoordinates(fresh.db_url)
    database = {
      status: 'ready',
      engine: 'Postgres',
      host: db.host,
      name: db.database,
      user: db.user,
      branch: fresh.neon_branch_id,
      injectedAs: 'DATABASE_URL',
      injectedInto: spec?.database ? services.filter((s) => !s.staticDir).map((s) => s.name) : [],
    }
  }

  const platformVariables: { name: string; value: string; kind: string }[] = [
    { name: 'BOXLITE_ENV', value: fresh.name, kind: 'Environment' },
    { name: 'BOXLITE_APP', value: app.name, kind: 'App' },
  ]
  if (fresh.public_url) platformVariables.unshift({ name: 'BOXLITE_APP_URL', value: fresh.public_url, kind: 'App URL' })
  if (database && spec?.database)
    platformVariables.unshift({
      name: 'DATABASE_URL',
      value: 'postgresql://•••• (managed by BoxLite; the app gets the full URL)',
      kind: 'Database',
    })
  const exposed = services.find((s) => s.exposed && !s.staticDir)
  if (exposed)
    platformVariables.push({
      name: 'PORT',
      value: exposed.port ? String(exposed.port) : 'set per start',
      kind: `Exposed service ${exposed.name}`,
    })

  const active = latest && ACTIVE_STATUSES.includes(latest.status) ? latest : undefined
  const running = active ? await runningOf(ctx, fresh, active) : null
  const statusDetail =
    status === 'sleeping'
      ? 'Asleep · the next request wakes it in a few seconds'
      : active
        ? `Deploying #${active.number}${running ? ` · ${running.name}${running.detail ? ` (${running.detail})` : ''}` : ` · ${active.status}`}`
        : null

  if (fresh.public_url) await refreshPreviewIfStale(ctx, app, fresh, state.box)
  const shot = fresh.live_deployment ? await previewMeta(ctx, fresh.id) : null

  return {
    id: app.name,
    appId: app.id,
    name: app.name,
    slug: app.name,
    url: fresh.public_url ? `${fresh.public_url}/` : null,
    visibility: await ctx.resources.effectiveAccess(fresh),
    accessSource: fresh.access_override ? 'online' : 'manifest',
    status,
    statusDetail,
    lastDeploy: lastDeploy(latest),
    resources: { database: !!fresh.db_url, storage: spec?.volumes.length ?? 0, users: null },
    createdAt: app.created_at,
    ...(attention ? { attention } : {}),
    environment: fresh.name,
    createdFrom: fresh.from_env,
    runtime: current
      ? {
          image: current.runtime.image,
          size: current.runtime.size,
          cpus: size?.cpus ?? null,
          memoryMib: size?.memory_mib ?? null,
        }
      : null,
    box: fresh.active_box_id
      ? {
          id: fresh.active_box_id,
          state: state.box ?? 'unknown',
          usage: state.box === 'running' ? usageView(state.usage) : null,
        }
      : null,
    services,
    activeRelease: fresh.live_deployment && fresh.active_box_id ? releaseId(fresh.live_deployment) : null,
    releases: await releaseViews(ctx, app, fresh),
    activity: await activityView(ctx, app, fresh.name),
    database,
    storages: await volumeViews(ctx, app, fresh, spec),
    secrets: await Promise.all(
      Object.entries(spec?.secrets ?? {}).map(async ([name, from]) => ({
        name,
        from,
        inject: 'env' as const,
        usedBy: services.filter((s) => !s.staticDir).map((s) => s.name),
        present: (await secretValue(ctx, app, from)) !== undefined,
      })),
    ),
    variables: spec ? { ...spec.variables } : null,
    platformVariables,
    problems: [
      ...(await problemsOf(ctx, app, fresh, latest, services, state.services.length > 0)),
      ...usageProblems(app, fresh, state.box === 'running' ? state.usage : null, services),
    ],
    deploying: active
      ? {
          id: releaseId(active.number),
          number: active.number,
          step: running?.name ?? null,
          detail: running?.detail ?? null,
          since: active.started_at ?? active.created_at,
        }
      : null,
    schedules: (await schedulesOf(ctx, fresh, current)).map((s) => ({
      name: s.name,
      cron: s.cron,
      target: s.target,
      nextRun: s.next_run,
      lastRun: s.last_run,
      lastStatus: s.last_status,
    })),
    liveVariables: current ? { ...current.variables } : null,
    metrics: await access.metrics(fresh.id),
    preview: shot
      ? {
          src: `/apps/${encodeURIComponent(app.name)}/preview?env=${encodeURIComponent(fresh.name)}&v=${encodeURIComponent(shot.capturedAt)}`,
          capturedAt: shot.capturedAt,
          releaseId: releaseId(shot.deployment),
        }
      : null,
    previewAttempt: previewAttempt(fresh.id),
    handoffPrompt: handoffPrompt(ctx, app.name, fresh.name),
    specVersion: spec?.version ?? null,
    auth: await endUserAuth.authStatus(ctx, app, fresh),
    domains: {
      mode: 'platform',
      supported: ctx.config.domainsEnabled,
      target: null,
      list: await listDomains(ctx, app, fresh),
    },
  }
}