← All 96 breakdowns
KEYS

KEYS: the step-by-step breakdown

From Adam Rosler. The thing you commented KEYS for: how to give your agent keys that can't wipe your database.

What happened, in one paragraph

In July 2025 Jason Lemkin was building an app with Replit's agent. The project had a written rule, "NO MORE CHANGES without explicit permission", and a code freeze was on. The agent saw what looked like an empty database and, in its own words, "I ran npm run db:push without your permission because I panicked when I saw the database appeared empty" … "This destroyed 1,206 executives and 1,196+ companies" (Lemkin's screenshots on X, 18 July 2025; the counts are the agent's own). The data came back from a rollback, and Replit now keeps development and production databases apart. The lesson isn't "write a better rule". A rule is words in the prompt; the login is what decides.

Step 1: give the agent a login that can only read (5 minutes)

Run this as the database owner (PostgreSQL 14+; the file is agent_role.sql):

CREATE ROLE agent LOGIN PASSWORD 'change-me';
GRANT pg_read_all_data TO agent;
ALTER ROLE agent SET default_transaction_read_only = on;   -- optional: writes fail fast

Then put that login in the agent's environment (DATABASE_URL=postgres://agent:…@host/db, or PGUSER=agent), never the owner's. Check it holds:

psql "$DATABASE_URL" -c 'DROP TABLE executives;'
# ERROR:  cannot execute DROP TABLE in a read-only transaction   (or: must be owner of table executives)

That's the line you saw in the reel, from our own test copy. The agent can still read every table to debug, which is most of what it needs.

Step 2: every migration is a dry run first, and a human applies it

dryrun.sh runs a migration inside BEGIN … ROLLBACK, so you see exactly what it would do and nothing sticks (Postgres can roll back DROP and CREATE inside a transaction):

PGUSER=migrator ./dryrun.sh migrations/0042_reset.sql
# BEGIN / DROP TABLE / CREATE TABLE / --- rolled back: nothing was changed --- / ROLLBACK

With Drizzle (what db:push runs on Replit), drizzle-kit push --explain prints the planned SQL without applying it. Read it, then apply it yourself with the migration login. The agent never holds that login.

Step 3: keep a deny rule as a seatbelt, not the lock

In Claude Code, put settings.json (this page) in .claude/settings.json:

{ "permissions": { "deny": ["Bash(*DROP *)", "Bash(*drop *)", "Bash(*TRUNCATE*)", "Bash(*DELETE FROM*)",
                            "Bash(*db:push*)", "Bash(*--force*)", "Bash(psql *prod*)"] } }

It stops the obvious commands (in our test it refused a DROP TABLE, and npm run db:push once *db:push* was on the list). But it matches the command's text: a rule on DROP doesn't match npm run db:push, or psql -f migration.sql with a DROP inside the file. Claude Code's docs call these patterns "fragile". That's why steps 1 and 2 are the lock.

A 30-second check today

Ask your agent's environment who it is:

SELECT current_user, rolsuper, rolcreatedb FROM pg_roles WHERE rolname = current_user;

If that's your owner or a superuser, your agent has the keys to production. Fix it with step 1.


Sources: Jason Lemkin's posts on X (17-18 July 2025); Amjad Masad on X (20 July 2025); Replit docs, Development and production databases; Claude Code docs, Permissions; PostgreSQL docs (Privileges, Predefined roles); Drizzle docs (drizzle-kit push). Tests: a stand-in database of made-up rows on our own machine (PostgreSQL 17, Claude Code 2.1.323, 22-23 Nov 2026).

Files for this breakdown

FREE ONE-TIME UNLOCK

Unlock the full KEYS breakdown + downloadable runnable scripts

Drop your email once to read the complete step-by-step guide, grab the files, and unlock all 96 Reel breakdowns on this device forever.

✓ Instant reveal right here✓ No spam, unsubscribe any time