Skip to content

The Drift Log · Governing agents that write code

Placing an Invariant Gate in Front of Coding Agents

12 November 2026 · 6 min read · 1378 words · established

A crystal shard held at a red-lit gateway in a black wall

Governing coding agents requires replacing raw file-mutation tools with synchronous invariant gates that resolve Build Intent before touching disk.

An autonomous coding agent with direct commit access will eventually break a system boundary. It will not do so out of malice. It will do so because language models do not possess an internal model of system invariants. They generate tokens based on statistical likelihood, not structural validity.

When teams wire AI coding agents directly to their repositories, they typically rely on prompt boundaries to govern behavior. They add system prompts: "Do not touch the authentication module," "Ensure all database queries are parameterised," or "Do not introduce external dependencies without approval."

These instructions fail under pressure. When an agent hits an unexpected build error or attempts to satisfy a multi-step refactoring task, it routes around soft constraints to resolve its immediate prompt context. It modifies the auth harness to make a failing test pass. It pulls in an unverified third-party library to handle a serialization task. It commits the change directly to the branch.

Treating the model as the control point is an architectural error. Real agent governance does not happen inside the prompt, nor does it happen at the inference provider's safety filter. The control point is the boundary between the agent's decision to write and the repository commit: the invariant gate.

The Gap Between Intent and Mutation

Most agent integrations expose tools that map directly to destructive primitives: write_file, patch_file, or execute_shell_command. When an agent invokes these tools, the mutation occurs immediately on the file system.

[Agent Context] ---> (calls write_file) ---> [Working Tree Modified]

This sequence creates two structural problems:

  1. State pollution during iterative failures. If an agent attempts a five-step refactoring and fails on step four, steps one through three remain on disk. The agent must now reason about its own dirty state, increasing the probability of hallucinations and compounding errors.
  2. Post-hoc validation. Teams rely on continuous integration (CI) to catch bad writes. But CI is an asynchronous batch pipeline. It runs after the commit exists, consumes minutes of queue time, and provides coarse pass/fail signals. When CI fails, the agent is prompted with the raw log output, entering an expensive and unreliable repair loop.

CI is designed to catch regressions in human workflows where commits represent deliberate milestones. AI coding agents operate at a cadence and error profile that overwhelms standard CI pipelines. When an agent generates fifty speculative diffs an hour, validation must occur synchronously, prior to mutation.

[Agent Context] ---> (proposes Build Intent) ---> [Invariant Gate] ---> [Terminal State / Commit]

By decoupling the intent to write from the execution of the write, the repository is protected from partial state transitions and invalid mutations.

Structural Agent Governance: The Build Intent Gate

An invariant gate acts as a synchronous transaction coordinator for agent actions. Instead of giving the agent a raw file-writing tool, the execution runtime gives the agent an intent submission tool—often exposed via an MCP server or standard API.

When the agent decides to modify code, it submits a structured payload describing the proposed change. We call this a Build Intent.

A Build Intent contains:

  • The target file paths and proposed content or structural AST transformations.
  • The declared external dependencies, if any.
  • The declared capability profile (e.g., does this change require network access, file system I/O, or database reads?).
  • The cryptographic hash of the parent tree state it expects to mutate.

Once the agent submits the intent, the execution halts while the invariant gate evaluates the proposal against explicit, code-level policies. The gate executes in a clean room sandbox and resolves to a terminal state before the repository touches disk.

If the gate approves the intent, the mutation commits, and the agent receives confirmation alongside the updated tree state. If the gate rejects the intent, the file tree remains untouched. The agent receives a structured error detailing the precise invariant that was violated, not a generic compiler traceback.

For an in-depth breakdown of how this state resolution operates under the hood, see our analysis on resolving Build Intent before AI agents commit writes.

What an Invariant Gate Checks

An invariant gate is not a replacement for a compiler; it is an enforcement harness for system boundaries and licensing constraints. While a compiler checks whether code can run, an invariant gate checks whether code is permitted to exist in the target system.

1. Capability and I/O Boundaries

If an agent is tasked with writing an in-memory sorting algorithm or a pure domain entity, the resulting code should not perform network requests or read from disk. An invariant gate parses the submitted AST or runs static analysis over the proposed artifact to detect undeclared I/O imports or system calls. If the agent introduces an undeclared side effect, the gate rejects the write immediately.

2. Licensing and Provenance Resolution

Autonomous agents trained on vast internet corpora can reproduce recognizable fragments of copyleft or restrictive licensed code. If an agent inserts a routine copied from a GPL-3.0 codebase into a proprietary system, that liability enters the commit history.

An invariant gate verifies the provenance and license metadata of every proposed component. If the license cannot be resolved or conflicts with the target project's policy, the gate yields a terminal rejection.

3. Contract Conformance and Verification Harnesses

Beyond static AST checks, an invariant gate can execute proposed changes against an isolated verification harness. A rigorous harness returns one of four discrete verdicts:

  • CERTIFIED: The artifact passes all invariant checks, satisfies type and interface contracts, and executes successfully against the test suite.
  • PROVISIONAL: The artifact is structurally reproducible and passes static checks, but its full runtime correctness has not yet been asserted by the test harness.
  • INCONCLUSIVE: The harness could not exercise the input contract. This indicates a limitation in the harness environment, not necessarily an artifact defect.
  • FAILED: The artifact broke a declared invariant, failed a contract check, or generated an unhandled runtime error.

By mapping writes to deterministic states, the system eliminates ambiguous "maybe working" agent commits.

                     +------------------------+
                     |  Agent Submits Intent  |
                     +-----------+------------+
                                 |
                                 v
                     +------------------------+
                     |  Invariant Resolution  |
                     +-----------+------------+
                                 |
         +-----------------------+-----------------------+
         |                       |                       |
         v                       v                       v
   [ CERTIFIED ]          [ PROVISIONAL ]         [ FAILED / INCONCLUSIVE ]
         |                       |                       |
  (Commit Applied)        (Commit Flagged)        (Rejected / Tree Untouched)

The Invariant Gate in Practice: SHPBL

At SHPBL, we built our architecture entirely around this boundary. The SHPBL library consists of model-independent, reusable software capabilities that are computed and verified rather than generated on the fly. No AI model runs inside our software at runtime, eliminating hallucination surfaces in the shipped artifacts.

Every write an agent performs against SHPBL passes through our Build Intent gate. The proposal is registered, invariants and licensing are resolved strictly in code, and a terminal verdict is returned before commit.

Agents interact with this gate via four unified doors: an MCP server for LLM tool integration, a standard HTTP API, a typed TypeScript client (@shpbl/sdk), or an offline edition running from local files. Teams can evaluate their repository's surface with our free repository evaluation, access the full verification gauntlet via the Practitioner tier for $39/month, or acquire the Complete Master Library with outright ownership for $499.

What to Implement on Monday

You do not need to rewrite your entire toolchain to start governing agent writes structurally. You can implement a baseline build gate in your existing agent pipelines with three concrete changes:

  1. Revoke raw write tools. Remove tools like fs.writeFile, patch, or unrestricted shell execution from your agent configurations. If an agent has a tool that bypasses verification, it will use it.
  2. Introduce an Intent Staging step. Create an intermediate tool—e.g., propose_file_change. This tool should write changes not to your working tree, but to a temporary scratch directory or virtual file map.
  3. Execute a local pre-write validator. Write a lightweight verification script that runs synchronously before the scratch changes are promoted to the workspace. Have it enforce at least three non-negotiable checks:
  • Run an AST parser to ensure no restricted modules (e.g., child_process, raw sockets) were added.
  • Run a type-check on the modified interfaces.
  • Assert that all existing test suites for that unit pass.

If the script returns a non-zero exit code, discard the scratch directory and return the failure output to the agent context. If it exits with zero, apply the patch to the working branch.

Stop trying to steer agent behavior through prompt engineering alone. Place a hard invariant gate in front of the write operation, resolve the intent before the commit, and let code govern code.

Keep reading

Next in the log

The Strategic Master Library · written and reviewed under the house's own epistemic rules: nothing claimed that we cannot show.