DEV Community

Darkø Tasevski
Darkø Tasevski

Posted on Originally published at darkotasevski.dev

Checking an agent's plan against the project's own gates before deciding

#ai

In the reading app I'm building, I approved a design decision that the plan review threw out a few hours later, because a pre-commit gate in the same repository already forbids it, and the same review found a second choice in that design that broke an accepted architecture decision record (ADR). The research behind the design was thorough, and it had even read some of the project's gates, but only the ones that name files the change would edit, so nothing asked what the project's gates and ADRs forbid for what each option would do. That question is cheap enough to ask before a decision reaches a person, as long as we bound how much we read, and this post is mostly about how to ask it.

Checking a key before it is saved

The app lets you bring your own API key for its AI features. There are fixed cloud providers (OpenAI, OpenRouter, Anthropic, Gemini) and custom endpoints, which in practice means something like Ollama or LM Studio on your own machine or network. Saving a fixed-cloud profile stored any non-blank key and made the profile usable without a single request to the provider, so a mistyped key looked configured and only failed at the first generation. The task was to make Save check the key with the provider first and write nothing to the Keychain if the provider refuses it.

The agent wrote a design, and a stress test of it (inversion, second-order effects and a SWOT, run as separate passes) changed a few choices before the plan was written. One of them dropped checking a custom endpoint's model list, because a local server may list a model under a different id than the one it accepts (an Ollama :latest tag, for example), but the design still checked a custom endpoint's key when it was saved.

Then a deepening pass ran four research streams over the plan, the project's past learnings, Swift and TCA correctness of every snippet, privacy and credential egress, and the providers' API docs. The provider stream found something the design hadn't accounted for, which is that OpenAI's RBAC guide treats "List models" and "Model capabilities: Request" as separate permissions. The check was going to be a GET /v1/models/{model} with the key, so a restricted key that can generate but can't read models would be refused, and the design as written would have called that a bad key and cleared it.

The agent offered me a choice, and since I ask for the ideal version first, I picked the option it presented as the ideal, which was to let such a key save with the model unchecked, telling a missing permission apart from a bad key by reading the provider's error message (community reports show a restricted key getting something like 401 … Missing scopes: api.model.read). A small matcher would read only the scope words from the message and keep no text, and it went into the plan with my approval next to it.

What the plan review found

The plan review, which runs a panel of reviewer agents over the plan, came back blocked on decisions, and two of its findings were about choices already in the design.

The approach reviewer and the security reviewer both pointed at the same required pre-commit gate, whose header starts like this:

# REQUIRED gate (the body-parse allowlist law is absolute — no carve-outs).
# Fails if any authorized-AI code reads `error.message` from a provider response
# body. A resolver's allowlist parses error.code + error.type only.
Enter fullscreen mode Exit fullscreen mode

It greps both AI directories for error.message and anything shaped like it, and when it fails it explains why, which is that a provider's error message can echo user-supplied parameters back, which for generation can be the user's prompt, and for a bad OpenAI key the message quotes part of the key itself (masked, but still). The header goes on to say that a real exception needs a new ADR and an AST gate registered for it, never a comment that waves the check through. So the matcher couldn't have been committed, and the rule it broke had been in the repo for months, for a reason that applies to a key check just as much as to generation.

The second finding was about the custom endpoints. The design still checked a custom endpoint's key at save, and the approach reviewer quoted two lines from the accepted ADR for how AI destinations work (manual profiles are what it calls the custom endpoints):

- Manual profiles may be saved inactive while offline.
…
Local Network permission is triggered only by explicit test/activation.
Enter fullscreen mode Exit fullscreen mode

A save-time check on a custom endpoint breaks both, since it needs the network to save and it would trigger the Local Network prompt on Save instead of on Test, and it adds no protection either, because a custom profile already can't be activated without a successful probe.

Neither rule was hidden, and the design had even read two of the project's gates, the ones that limit which files may send a request or build a zero-content envelope, because it had to add its new file to their allowlists, and it planned an amendment to that same ADR. What it never did was ask what the gates and the ADR forbid for what each option does, so the error.message gate, which names no file the change edits, never came up, and neither did the two lines of the ADR the design was already amending somewhere else. So the evidence behind the matcher was complete about OpenAI and silent about the repo.

What changed in the plan

In the revised plan, an OpenAI 401 or 403 is read from error.code only. An invalid_api_key code means the key is rejected and cleared, and any other refusal is treated as a permission problem, where nothing is saved, the key stays in the field and the copy names the Models permission. So a restricted key still can't be saved, which is the part of the original idea that the gate cost, and the plan says so instead of hiding it (the invalid_api_key code itself also rests on community examples and waits for a check with real keys). Gemini needed the same care, since it returns HTTP 400 both for an invalid key (INVALID_ARGUMENT) and for region or billing refusals (FAILED_PRECONDITION), so a Gemini 400 at check time keeps the key and says it may be the key or the account. The check is scoped to fixed clouds, and custom endpoints stay local-first and are measured when they are activated, as the ADR says.

Asking the repo before asking the human

The gate would have caught the matcher eventually, at the first commit, but by then it would have been written and tested, and the same question would have come back to us with a different set of facts, so the cheap point is before the decision is put to a person, when the agent claims its evidence is complete. This is the same step as reading what each option changes, pointed at the project's rules instead of its code.

My first idea for finding the gates was to grep the check scripts for the directory an option touches, since most of them name their scan roots, and in this repo that grep does list six gates, the error.message one among them. It misses a whole kind of gate, though, since the repo also has AST gates written in Swift, and fourteen of their rules apply to every package's sources through a scope set in code, where a grep for a directory never finds them. What covers both kinds is the map the project already keeps for itself, a gate catalog with one entry per gate saying what it forbids and why, and the table in the agent instructions that ties each area of the code to the ADRs to read before touching it. We search those for the option's paths and also for what the option would do, and both searches matter, because the catalog entry for the error.message gate names no directory, so only a search for error.message finds it, and that one paragraph says the rule admits no carve-outs and why, which is all the agent needed to drop the matcher before asking anyone.

A few details keep that read small and its answers trustworthy. We only run it for options that change code, configuration, a schema or a documented contract, and we read only what the search hits, which matters because the ADR in this story is several thousand lines long, so we search its decision section and amendments for the component and read the matching passages. We read every gate and ADR as it stands on the base branch, since a branch can edit either, and an option that relaxes a rule shouldn't get to approve itself. And each rule ends up in one of a handful of states, each with somewhere to go:

  • An option that keeps every rule it touches is an ordinary choice.
  • An option that breaks a gate it can't pass is dropped, but the question to the person says it was dropped, by which rule, and what it would have bought.
  • An option that passes only through the gate's exception route, a carve-out or an allowlist edit goes to the person as a proposal to relax that rule, the same way an ADR break does.
  • An option that breaks an accepted ADR is put to the person as a proposal to amend it, quoting the exact clause, since approving an amendment to one line of an ADR isn't approval to break the others.
  • A rule the agent can't judge from a plan, and a search that finds nothing, are reported as open, with what was searched, and neither counts as kept.
  • When every option breaks something, the conflict itself is the question, whether to change the requirement, the gate or the ADR.

So before a design decision goes to a person, the research needs a third source next to the outside docs and the code, which is the project's own enforcement, found from the project's own map by both paths and behaviour, read at the base branch, and recorded per option as kept, broken or open. Had that run here, we'd have been asked a smaller and more honest question, whether a restricted key should be refusable at Save, before anyone approved a matcher the repo was never going to accept.

Top comments (0)