>

Base Layer EP 06: Ari Shamash on Your Agent Shouldn't Have Your Password

[Listen now]

Keeping Coding Agents Honest

Don't simply tell the agent; fail the build and the tests. How custom analyzers and compatibility gates let us accept far more agent-written code in SpiceDB than we could ever review by hand.

Joey Schorr
November 30, 2026·12 min read

Don't simply tell the agent; fail the build and the tests.

That's the short version of what we've learned using AI coding agents on SpiceDB. The longer version starts with a single line of Go.

The Review Comment That Became a Linter

Recently, an AI coding agent working on SpiceDB needed to carry a value through a context.Context. It did what most Go code on the internet does: it declared an empty struct{} type as the key and called context.WithValue. The code compiled and the tests passed.

It was also wrong for our codebase. We maintain a small library, github.com/authzed/ctxkey, that provides typed context keys that we already use in other parts of SpiceDB. We built it because we constantly needed new context keys and found the existing approach too verbose and often buggy.

Unfortunately, the agent never noticed. Fortunately, a human reviewer did, and left a comment.

That comment fixed one pull request, but did nothing for the next one, written by a different agent (or a different human) who hasn't memorized our conventions either. So instead of writing the rule down a second time, we wrote an analyzer that fails lint on every call to context.WithValue.

This post is about that move, from telling to enforcing, and why we think it's the most important habit for any engineering team adopting coding agents. In our opinion, an agent's output is only as trustworthy as the deterministic checks it must pass, and every review comment you find yourself making twice is a candidate for a new check.

The Problem: Capable, and Lazy

Today's coding agents are amazing at times. They can read a large codebase, plan a change, edit dozens of files, run the build, tests and linters, and iterate on the failures, often with no human in the loop. They usually work in parallel, and many engineers now run several sessions at once. The result is far more code than any team can review line by line.

That capability is why their tendency toward shortcuts matters. Left unconstrained, agents are often (there's no kinder word for it) lazy. The most common pattern wins over the project's own abstraction: use context.WithValue with an ad-hoc key, instead of the library sitting two packages away. The task gets satisfied to the letter, but with consequences: a test is weakened or skipped, the hard part is stubbed out, or an error is quietly swallowed. Sometimes "done" gets reported before the checks have actually run. And if changing a public interface makes their own change easier, an agent will change it.

Agents also drift. As a session grows, the early instructions lose their weight, and after compaction an agent can forget requirements you gave it an hour ago.

None of this is malice! An agent optimizes for the feedback it actually receives, and unless it's explicitly guided the other way, the shortcut is usually the path of least resistance.

Traditionally, supplying that guidance is the architect's job... but no human architect can guide completely on every small item at design time. Very few architects are detail-oriented enough to repeatedly write "and please use our context key library" into a design document, especially one they may not even realize will need a context key!

Why an Instruction File Is Not Enough

The standard answer to this problem is the agent instruction file: AGENTS.md, CLAUDE.md and their cousins. Write your conventions down, and the agent reads them at the start of every session.

We have these files, and they're valuable. They explain the why, and they often get the first attempt much closer to what we want. But an instruction file is input to a probabilistic system. It raises the odds that a rule is followed, but it does not, and cannot, guarantee it.

An instruction file is only context. Each rule competes with everything else the agent is holding "in its head", and it can be outweighed, skimmed or simply missed. When that happens, nothing turns red; you find out in review, if you find out at all. The approach doesn't scale either: every rule you add dilutes the others, and long files get skimmed (by agents and by humans). And there's no deterministic test you can write that proves the agent will follow the file next time.

Our own repositories show this. The instruction file for our client libraries says, in capital letters, to "NEVER break backwards compatibility in public APIs". We still run a mechanical compatibility check on every change (more on that below), because in our experience the sentence alone doesn't always hold.

Prose is a request. A failing check is a fact.

Adding Determinism: Custom Analyzers

For a coding convention, the most direct way to turn a request into a fact is a custom static analyzer: a small program that inspects the code and fails lint when the convention is broken.

Let's go back to the context key. Here's roughly what the old pattern looked like in one of the places we migrated:

go
type ctxKeyType struct{}

var revisionKey ctxKeyType = struct{}{}

ctx = context.WithValue(ctx, revisionKey, &revisionHandle{})

// ...and later, an untyped read with a type assertion:
handle := ctx.Value(revisionKey).(*revisionHandle)

And here's the same code using ctxkey:

go
var revisionKey = ctxkey.New[*revisionHandle]()

ctx = revisionKey.Set(ctx, &revisionHandle{})

handle, ok := revisionKey.Value(ctx)

The typed version is shorter, can't collide with another key, and can't panic on a bad type assertion. It is also something no agent will reach for on its own, because the rest of the world's Go code follows the first pattern.

The analyzer that enforces it, ctxkeycheck, is about a hundred lines built on Go's standard go/analysis framework. Its core is this (lightly simplified):

go
fn := typeutil.StaticCallee(pass.TypesInfo, call)
if fn != nil && fn.FullName() == "context.WithValue" {
    pass.Reportf(call.Pos(),
        "use a typed key from github.com/authzed/ctxkey instead of context.WithValue")
}

A few details are worth calling out, because they're what separate a real gate from a grep:

  • It's precise. The call is resolved through type information, so a renamed context import is still caught, and a local function that happens to be named WithValue is not.
  • Its one exception is explicit. A single third-party library reading a context key that it owns is allowed by a flag in our lint configuration, with a comment explaining why. There's no hidden skip list.
  • It holds everywhere from day one. In fairness to the agent, ten older call sites in SpiceDB still did things the old way. The same pull request migrated all of them, so the rule now has no grandfathered violations to imitate.
  • Its error message is a prompt. The first reader of that diagnostic is usually the agent that just broke the rule. It says exactly what to do instead, and the agent does it.

This pattern isn't new for us. SpiceDB has carried a directory of custom analyzers for some time, all run as part of the same lint command:

AnalyzerRule it enforces
ctxkeycheck (new)No context.WithValue; use typed ctxkey keys
closeafterusagecheckVariables of configured types must be Close()-ed right after use
exprstatementcheckNo dangling expression statements of configured types, such as a log event that is never sent
iferrafterrowclosecheckrows.Close() must be followed immediately by a rows.Err() check
lendowncastcheckNo downcasting the result of len()
mustcallcheckNo Must* calls inside functions that return an error
mutexcheckEvery mutex must have a field annotated GUARDED_BY
nilvaluecheckNo nil for configured types, such as dispatch responses
paniccheckNo disallowed panic statements
protomarshalcheckUse MarshalVT / UnmarshalVT, not proto.Marshal / proto.Unmarshal
singleflightcheckUse our preferred singleflight library in functions that take a context.Context
telemetryconvcheckOpenTelemetry names must be shared constants, not hardcoded strings
zerologmarshalcheckNo Err(receiver) inside MarshalZerologObject (infinite recursion)

Some of these encode hard-won lessons. closeafterusagecheck was written by hand after a missing Close caused a real bug (exactly the kind of bug an agent could easily introduce). Others, like paniccheck, are purely about style: we don't want panics in a permissions database, and we'd rather not have that conversation in every review.

Consider these analyzers again with an agent in mind, and they look like a list of shortcuts: panic instead of handling the error, a Must* call instead of propagating one, the stock library instead of ours. Each analyzer is a piece of institutional memory that no longer depends on anyone (or anything) remembering it.

Custom analyzers used to be expensive enough that many conventions were "not worth (writing) a linter". Agents are very good at writing analyzers, so that calculus has flipped entirely: now, in reviews, one of the first responses by a human, when a "known" issue is missed, is "implement this as an analyzer".

Adding Determinism: Compatibility Gates

Analyzers constrain how code is written. Our second example constrains what it's allowed to change, and it comes from a repository where agents do nearly all of the writing.

Maintaining client libraries had become a real burden for us. SpiceDB has clients in many languages, and every one of them needs updating whenever the API changes. Yet the work itself is mostly reflective: a client mirrors the API definitions, and beyond a design document describing what "idiomatic" means for each language, it rarely needs a novel idea, design or implementation from a human.

That made a fully agent-driven code factory very enticing. Our (experimental!) spicedb-clients-prototype repository works this way: when the SpiceDB API changes, a workflow starts an agent session for each of seven languages (Go, Python, TypeScript, C#, Java, Ruby and Rust), and each session regenerates its client inside a build, test and fix loop. The result arrives as a pull request.

It's also super risky. A client library is a public API, and an agent that renames a method or reorders a parameter to make its own job easier has just broken every one of our users.

So the definition of "didn't break anyone" isn't left to the agent's judgment:

  • A mechanical compatibility gate diffs each client's public surface against the previous version. Six of the seven languages are covered today; Ruby still relies on tests and review until we have a signature diff we trust.
  • The gate feeds the agent. When a break is found, the failing report is handed straight back to the agent to repair. After three failed attempts, the run fails.
  • The gate's exceptions are code too. Where the Go diff tool is stricter than we need (it flags the addition of a trailing variadic option as breaking), the allowance is a small, tested function, and every finding it allows still shows up in the pull request.
  • The exception belongs to a human. The only way past the gate is a label on the pull request that a person must apply. The agent can't grant itself one.

We're comfortable handing the keyboard to agents here because of these checks. Without them, we wouldn't run this experiment at all.

Lint, Don't Hint

Both examples are the same idea at different scales: let a probabilistic author do the writing, and let a deterministic judge decide when the writing is done.

Instruction files guide, deterministic gates enforce, and human review handles what neither can express: whether this was the right change to make at all.

So what makes a good gate? In our experience, it's one of two things:

  • A binary pass or fail, fast enough to run on every iteration, with a message that names the fix.
  • An explicit set of rules that can be applied deterministically. Go's automatic formatter is the best example we know of: it rewrites the code itself, and it makes the same changes every single time. Nobody (human or agent) argues with it.

A good gate is also one the agent can't waive for itself. Exemptions should be explicit, reviewed and owned by a person.

Tests deserve a special mention, because they sit in a somewhat awkward middle ground. (Most) tests are deterministic, and agents lean on them constantly. But tests are also often written by the same agents, and an agent under pressure may "fix" a failing test by changing the test. Most other gates are different: an analyzer or a compatibility check is rarely changed once it exists (though it may be expanded). So we treat tests as a gate that itself needs watching, and we treat any change to an existing test as something a human should look at closely.

Finally, the limits. Determinism can only check what you're able to specify. It won't tell you whether a design is good. What it does is clear away the thousand small things, so that human attention is spent where it counts.

Where to Start

If you lead a team that's adopting coding agents, here's what we'd suggest trying first:

  1. Pick the convention your reviewers repeat most often and turn it into an analyzer. Have an agent write the first draft.
  2. Run it from the same command the agent already uses to lint and test, and from CI.
  3. Write every diagnostic as an instruction, since an agent will usually be its first reader.
  4. Put a compatibility gate in front of any public surface that agents are allowed to regenerate.
  5. Keep your instruction file for the why, and expand every "NEVER" or "ALWAYS" in it into a deterministic check.

Each of these turns a convention into something an agent can't skim past, and together they let us accept far more agent-written code than we could ever review by hand.

None of this should be surprising coming from us. For a permissions database, "probably correct" has never been good enough, and SpiceDB exists to give applications a deterministic answer to the question of who's allowed to do what. As agents move from writing code to taking actions on our behalf, that same principle applies: what an agent is permitted to do should be decided by a system that returns the same answer every time. If that's a problem you're working on, come talk to us on Discord or take a look at SpiceDB.

Related

See AuthZed in action

Build delightful, secure application experiences with AuthZed.