Notes

The machine that said no to my machine

Read as
working implementation · tests
Build treatmentUpdated 14 Jul 2026

Production AI systems · Guardrails

What you will leave with

Implement a permission snapshot that fails on newly granted access and on missing policy coverage.

For engineers turning a sensitive database boundary into an enforced CI gate.~4 min read
PrerequisitesNode 22+PostgreSQL + psqlVersioned migrationsVitest
01 · System view

The owned guardrail you will build

The incident’s classifier is platform-owned. This production-backed equivalent protects a database boundary by declaring what must remain denied and failing CI when reality drifts.

  1. 01Denied snapshotObjects, roles, and privileges that must stay closed.
  2. 02Migration chainBuilds the database from zero in CI.
  3. 03Privilege probeObserves SELECT, INSERT, and EXECUTE grants.
  4. 04Violation diffTreats a grant or missing object as failure.
  5. 05Required gateBlocks the change before the boundary reaches production.
02 · Implementation

Build it in sequence

01

Declare what must remain denied

The snapshot is the reviewable policy. Keep it separate from the probe so policy changes cannot hide inside implementation changes.

scripts/permission-snapshot/protected-grants.json · sanitizedjson
{
  "roles": ["anonymous", "authenticated", "public"],
  "relations": {
    "privilege": "SELECT",
    "objects": ["private.customer_events", "analytics.internal_rollup"]
  },
  "relationInserts": {
    "privilege": "INSERT",
    "objects": ["private.audit_log"]
  },
  "functions": {
    "privilege": "EXECUTE",
    "objects": ["private.refresh_internal_rollup()"]
  }
}
02

Expand policy into observable assertions

The production script creates one assertion per object, role, and privilege. It only observes; migrations remain the sole owner of GRANT and REVOKE.

scripts/permission-snapshot/check-permissions.ts · production patterntypescript
export function expandPairs(snapshot: ProtectedSnapshot): Pair[] {
  const pairs: Pair[] = []

  for (const object of snapshot.relations.objects) {
    for (const role of snapshot.roles) {
      pairs.push({ kind: 'relation', object, role, privilege: 'SELECT' })
    }
  }

  for (const object of snapshot.functions.objects) {
    for (const role of snapshot.roles) {
      pairs.push({ kind: 'function', object, role, privilege: 'EXECUTE' })
    }
  }

  return pairs
}
03

Fail on holes and on missing coverage

A granted privilege is a security hole. A missing protected object is snapshot drift. Both fail because the gate never interprets disappearance as safety.

scripts/permission-snapshot/check-permissions.ts · production patterntypescript
export function computeViolations(rows: ProbeRow[]): CheckResult {
  const holes = rows.filter(row => row.exists && row.granted)
  const missing = rows.filter(row => !row.exists)

  return {
    ok: holes.length === 0 && missing.length === 0,
    total: rows.length,
    holes,
    missing,
  }
}
04

Prove that a grant turns the build red

The real test injects an accessible protected function and asserts that the report names the exact hole.

scripts/permission-snapshot/check-permissions.test.ts · sanitizedtypescript
it('fails when a restricted role receives EXECUTE', () => {
  const result = computeViolations([{
    kind: 'function',
    object: 'private.refresh_internal_rollup()',
    role: 'public',
    privilege: 'EXECUTE',
    exists: true,
    granted: true,
  }])

  expect(result.ok).toBe(false)
  expect(formatReport(result)).toContain('HOLE')
})
05

Run it against a database rebuilt from zero

CI applies the committed schema and every migration to an ephemeral Postgres instance, then probes the resulting grants. No development or production credential is required.

.github/workflows/ci.yml · public reconstructionyaml
permission-snapshot:
  services:
    postgres:
      image: supabase/postgres:15
  steps:
    - uses: actions/checkout@v4
    - run: apply-schema-and-every-migration.sh
    - run: >-
        node --experimental-strip-types
        scripts/permission-snapshot/check-permissions.ts
        --db-url "$EPHEMERAL_DATABASE_URL"
03 · Review

Definition of done

  • The denied set is versioned separately from the probe.
  • The probe changes no grants; it only observes and asserts.
  • A newly granted SELECT, INSERT, or EXECUTE fails CI.
  • A renamed or removed protected object also fails CI.
  • The database is rebuilt from the committed migration chain.
  • Negative tests prove both a privilege hole and snapshot drift.
04 · Failure modes

What breaks and what it means

A protected object is reported missing.

Do not delete it from the snapshot just to get green. Confirm whether the migration chain creates it and whether the rename was intentional.

Development is safe but CI reports a hole.

Development may contain an out-of-band manual revoke. The from-zero chain is exposing migration drift; repair the migration, not the check.

Public discussion

Questions, corrections, and useful disagreement

Moderated on GitHub ↗

Sign in with GitHub to join the conversation. Comments are public. Article views are anonymous and counted once per browser, per article, each day.

Read next09

Adopt, adapt, skip

Triaging someone else’s 24 agent skills against our own gates led us to adopt five, adapt six, and skip thirteen. The principle that fell out: a skill teaches behaviour, while a gate enforces it.

3 depths