The Drift Log · Governing agents that write code
Evaluating Agent Tool Calls Against Structural Code Invariants
23 November 2026 · 4 min read · 800 words · established

Prevent agent workspace degradation by intercepting file mutation tools with AST-level invariant checks before disk writes occur.
The failure mode of direct tool access
Most agent environments give the model direct access to file-system mutation tools. The agent decides to edit a file, calls a tool like write_file or apply_patch, and the runner executes the change directly against the workspace.
Only after the disk changes do linters, compilers, or test suites run. If the change violates an architectural rule—such as importing an internal module across a service boundary, introducing a prohibited third-party library, or adding unmanaged I/O—the mutation still lands in the working tree.
Fixing it requires a secondary cycle: running test commands, feeding compiler errors back to the model's context window, and waiting for another patch attempt. When that patch fails, the workspace state degrades further.
Post-hoc CI and traditional code review automation are the wrong control points for agents. Humans can read a pull request and understand that an entire approach violates a project invariant. Agents, when corrected purely by downstream compiler errors, often patch around the symptom rather than respecting the structural boundary.
The failure is not that the model hallucinated. The failure is that your tool layer trusted the output enough to write it to disk without checking structural invariants first.
Intercepting the tool call
Governing agent mutations requires a gate placed between the agent's decision to write and the actual mutation of the repository. Instead of giving the agent a raw file-system tool, you expose a tool that submits a structured proposal.
In placing an invariant gate in front of coding agents, the core premise is straightforward: no agent writes directly to a repository without satisfying a machine-verifiable contract.
When an agent issues a tool invocation via an MCP server or standard JSON-RPC interface, the gate intercepts the payload. Before applying a single byte, it evaluates the change against AST-level rules:
- Import boundary enforcement: Does the proposed file introduce unauthorized external dependencies or bridge private package boundaries?
- Interface conformance: Does the exported signature match the contract expected by callers without altering type definitions outside the agent's scope?
- Safety and resource constraints: Does the diff introduce unmetered filesystem calls, network sockets, or concurrency primitives that violate local execution policies?
{
"gate_verdict": "REJECTED",
"invariant": "no_raw_file_io_in_domain_layer",
"file": "src/domain/billing/processor.ts",
"violation": "Import of 'node:fs/promises' is forbidden in domain modules."
}
The gate immediately halts execution and returns a structured rejection. The agent receives precise feedback on the invariant failure before polluted state touches the branch, avoiding repetitive repair loops.
What invariant evaluation can and cannot do
Evaluating structural invariants at the tool layer is fast because it relies on static analysis and AST inspection, not full test suite execution. You can parse an AST, traverse an import graph, and verify license allowlists in single-digit milliseconds.
This approach has limits:
- It cannot prove semantic intent: A gate can verify that a function signature matches and that forbidden imports are absent, but it cannot prove the algorithm inside the function produces the correct business calculation.
- Dynamic dependencies escape static gates: If an agent uses reflection or dynamic property lookups, purely static gates cannot catch every misuse without strict runtime sandboxing.
- Latency trade-offs exist: Running a full TypeScript type-checker or whole-program analysis on every single tool call is too slow for interactive agent loops. Invariant checks at the tool boundary must remain local and structural.
Treat the build gate as an enforcement mechanism for invariants that are cheap to check and fatal to violate.
How SHPBL handles tool-level governance
At SHPBL, every capability write an agent performs passes through a Build Intent gate. The agent registers its proposal, and the invariant verification—covering structural bounds, integration contracts, and licensing rules—is resolved entirely in code before a terminal state is reached.
The catalog components are model-independent: no model runs inside the software, and runtime behavior is computed rather than generated. Access operates across four unified entry points: an MCP server for interactive agent sessions, a plain HTTP API, a typed TypeScript client (@shpbl/sdk), and an offline edition. They share the same gate, the same rules, and the same verification standards.
You can run a free repository evaluation to inspect how structured boundaries apply to your existing code.
What to do on Monday
Audit your current agent configuration:
- Remove raw write tools: Replace unrestricted
write_fileorexecute_bashcapabilities with an explicit proposal tool. - Implement an import linter on tool submission: Write a lightweight pre-write check using your language's native AST parser. If the agent adds an import that violates your boundary map, reject the call at the tool boundary.
- Return structured error objects: When a tool call is rejected by an invariant check, do not return a generic string. Return the exact rule name, the offending line, and the allowed alternative so the agent can pivot on its next token pass.
This post supports the longer argument in Placing an Invariant Gate in Front of Coding Agents.
Keep reading
Next in the log
- Placing an Invariant Gate in Front of Coding Agents
Governing coding agents requires replacing raw file-mutation tools with synchronous invariant gates that resolve Build Intent before touching disk.
- Enforcing Pre‑Commit Invariant Checks on Agent Proposals
A pre‑commit hook validates licensing, provenance, and runtime invariants before any agent‑generated write is persisted.
- Validating Licensing and Invariants at the Build Intent Gate
Intercept agent code writes with a Build Intent gate to enforce licensing budgets and structural invariants before mutation touches disk.
The Strategic Master Library · written and reviewed under the house's own epistemic rules: nothing claimed that we cannot show.