Formal Verification Gates for AI Coding Loops
TL;DR Highlight
Moving rules from prompts into compiler-enforced types makes AI coding loops structurally harder to break.
Who Should Read
A backend developer whose AI-generated code keeps forgetting multi-tenant authorization checks.
Core Mechanics
- Prompt rules are unstable
- Putting rules in CLAUDE.md or system prompts makes them behavioral gates that rely on the model remembering them.
- The author says models follow them often enough to be useful but fail often enough to make the arrangement unstable.
- Structural gates like compilers refuse bad code
- Compilers, type checkers, test runners, linters, and proof checkers give concrete answers about the artifact.
- The idea is to move rules out of the model's instruction space into the substrate it writes against.
- Shen specs generate guard types
- You write rules in Shen, a small statically typed Lisp, and shengen lowers them into guard types in Go or TypeScript.
- The model never needs to know Shen; the code just has to compile and pass the gates.
- The auth chain is enforced by types
- jwt-token → authenticated-user → tenant-access → resource-access each require the previous proof.
- In Go, fields are unexported and the constructor is the only path, and it refuses isMember == false.
- Passing raw values fails the build
- If the agent skips the chain and passes a raw string, the build fails before a binary exists.
- The membership DB query is concentrated at the TenantAccess construction boundary instead of scattered across handlers.
- The sb loop feeds gate failures into the next prompt
- The default set has five gates: shengen, test, build, shen tc, and tcb audit.
- Failures become concrete context in the next prompt, which is the backpressure.
- The author names the limits
- Writing a spec is not free, and the spec can be wrong, the generator can drift, and tests can miss cases.
- The claim is that bypassing by accident is practically impossible, not that bypassing is impossible.
Community Reactions
- Critique of the JWT rules
- A commenter argued the jwt-token rule only checks the string is non-empty, not parsing, expiry, or signature.
- They said if the AI can call the constructor directly with true, it can 'prove' anything, which seems backwards.
- They shared that they named such constructors newUnverified and scrutinized only the callers, such as JWT parsers and DB lookups.
- Judgment moves rather than disappears
- One commenter said the compiler enforces what you encoded, not what you meant.
- They saw it as forcing human judgment up front into type definitions, which are reviewable and permanent unlike prompt rules.
- Why not Rust, Lean, or Liquid Haskell?
- A commenter asked what this adds over Rust constructors, private fields, and newtypes.
- Another suggested Liquid Haskell or Lean to put invariants directly in the program instead of a sidecar.
- A Lean user said that requiring proofs in type signatures stopped agents from doing something else, but the gap to real projects in other languages is frustrating.
- Experience returning to deterministic tools
- A DevOps commenter said their team moved from heavy LLM use to building deterministic tools usable by both humans and LLMs.
- They reported scripts ran in under 30 seconds while LLM confabulating took 2-3 minutes.
- Another commenter argued backpressure is a rate-control signal, and what's needed is an error-control signal telling upstream what downstream detects.
How to Apply
- Consolidate authorization checks into one constructor
- Pick one handler area where permission ifs are scattered across endpoints.
- In Go, create a TenantAccess type with unexported fields so only the constructor validates membership.
- Change handlers to accept this type instead of a string.
- Name unverified constructors and limit callers
- Name any constructor that accepts a caller-asserted value like newUnverified.
- Restrict its callers to JWT parsers and specific DB lookups and review only those closely.
- Fix the gate order in your agent loop
- Run build, test, and lint in a fixed order each iteration and paste failures verbatim into the next prompt.
- To try the tool, run sb init then sb loop and switch harnesses with RALPH_HARNESS.
- Add an audit gate for hand edits
- Put an audit script in CI that rejects manual edits to generated guard code.
- Encode just the single most important invariant first and expand later.
Try It
# Paste into Claude Code or ChatGPT (a simple prompt version of the article's method, Go)
Refactor the {{handler code}} below.
Rules:
1. Do not keep tenant membership checks in handlers; put them only in the constructor NewTenantAccess(principal, tenantID, isMember) (TenantAccess, error).
2. Make the TenantAccess struct fields lowercase (unexported) and remove any other way to create the value.
3. Handler function parameters must take the TenantAccess type, not a string.
4. If isMember is false, the constructor returns an error.
5. Gather the DB lookup that computes isMember into one function that calls the constructor, named so callers are easy to find.
After the change, run go build ./... and go test ./..., and if they fail, read the errors and fix them yourself.
{{handler code}}Terminology
Related Papers
Frostbite: A new CPU architecture discovered, designed and verified by AI
AI agents designed a CPU from scratch, claiming 4.59x perf/W over a traditional core
Restricting AI agents in mobile operating systems platforms from capturing data
Signal now blocks OS-level AI agents from reading its screen on iOS 27 and Android
Actions Speak Louder than Words: Stateful Privacy Sniffing of LLM Agents via Behavioral Covert Channels in Web Interactions
Agents' link choices alone let attackers rebuild user queries at F1 0.89; SOR cuts it to 0.39.
Tracking and Limiting Unsupported Claim Propagation in Multi-Agent LLM Systems: A Provenance-Aware Framework
Passing claims with source, evidence and history kept equivalence at 5 agent stages at 96% versus 31%.
LLM assisted reverse engineering
Pointing an agent at binaries with skills and MD notes made years-long hacks feasible
Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code
Injecting per-turn complexity reports into coding agents cut code erosion without changing correctness
Why is Gemini 4 Carbon taking so long?
Carbon took nearly an hour on bug fixes that Astra and Opus finished in minutes, mostly from extra unrequested work.
Weight-Space Programming builds neural networks like a 1990s researcher
LLM hand-wrote MNIST weights to 91.3%, using 1990s-style techniques