Skip to content

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

3.6 KB
Raw Blame
/**
 * Acknowledging an info finding of `app context` on purpose through the environment’s findings API:
 * a public site the user asked for, a variable that only looks like a credential. Only `info`
 * findings qualify (warnings and errors need a fix), and an acknowledgement covers that exact
 * finding: when its title changes (another variable, another provider), it comes back.
 */
import type { AcknowledgedFinding, AgentContext, Finding } from '@boxlite-ai/cloud-app-shared'
import type { AppRow, EnvRow } from '../db'
import { now } from '../db'
import type { Ctx } from '../ctx'
import { fail } from '../errors'

interface AckRow {
  finding_id: string
  title: string
  reason: string
  by: string
  created_at: string
}

/** Splits the findings into what still needs attention and what was acknowledged as it stands. */
export async function applyAcks(
  ctx: Pick<Ctx, 'store'>,
  env: EnvRow,
  findings: Finding[],
): Promise<{ next_actions: Finding[]; acknowledged: AcknowledgedFinding[] }> {
  const acks = new Map(
    (
      await ctx.store.all<AckRow>(
        'SELECT finding_id, title, reason, by, created_at FROM finding_acks WHERE env_id = ?',
        env.id,
      )
    ).map((a) => [a.finding_id, a]),
  )
  const next_actions: Finding[] = []
  const acknowledged: AcknowledgedFinding[] = []
  for (const f of findings) {
    const a = acks.get(f.id)
    if (a && a.title === f.title && f.severity === 'info')
      acknowledged.push({ id: f.id, title: f.title, reason: a.reason, by: a.by, at: a.created_at })
    else next_actions.push(f)
  }
  return { next_actions, acknowledged }
}

export async function ackFinding(
  ctx: Ctx,
  app: AppRow,
  env: EnvRow,
  current: AgentContext,
  id: string,
  reason: string,
  actor: string,
): Promise<AcknowledgedFinding> {
  const why = reason.trim()
  if (!why)
    throw fail(
      'invalid_request',
      'say why it is fine as it is',
      'send a JSON reason, for example {"reason":"the user asked for a public site"}',
    )
  const f = current.next_actions.find((x) => x.id === id)
  if (!f) {
    const ids = current.next_actions.map((x) => x.id).join(', ') || 'none'
    throw fail(
      'not_found',
      `no finding "${id}" in ${env.name} now`,
      `\`boxlite app status --details\` lists them (now: ${ids})`,
    )
  }
  if (f.severity !== 'info')
    throw fail(
      'invalid_request',
      `"${id}" is a${f.severity === 'error' ? 'n error' : ' warning'}; only info findings can be acknowledged`,
      f.fix,
    )
  const at = now()
  await ctx.store.run(
    `INSERT INTO finding_acks (env_id, finding_id, title, reason, by, created_at) VALUES (?, ?, ?, ?, ?, ?)
     ON CONFLICT (env_id, finding_id) DO UPDATE SET title = excluded.title, reason = excluded.reason, by = excluded.by, created_at = excluded.created_at`,
    env.id,
    id,
    f.title,
    why.slice(0, 500),
    actor,
    at,
  )
  await ctx.store.recordActivity(app.id, env.name, actor, 'finding.ack', { id, reason: why.slice(0, 200) })
  return { id, title: f.title, reason: why.slice(0, 500), by: actor, at }
}

export async function unackFinding(
  ctx: Ctx,
  app: AppRow,
  env: EnvRow,
  id: string,
  actor: string,
): Promise<{ unacknowledged: string }> {
  if (
    !Number((await ctx.store.run('DELETE FROM finding_acks WHERE env_id = ? AND finding_id = ?', env.id, id)).changes)
  ) {
    throw fail(
      'not_found',
      `"${id}" is not acknowledged in ${env.name}`,
      '`boxlite app status --details` lists the acknowledged findings',
    )
  }
  await ctx.store.recordActivity(app.id, env.name, actor, 'finding.unack', { id })
  return { unacknowledged: id }
}