Harness Engineering for Infrastructure Repos: Guardrails for Coding Agents

Harness Engineering for Infrastructure Repos: Guardrails for Coding Agents

Most writing about agent harnesses assumes you are building a web app. Infrastructure repos are different: a wrong line in a Terraform module or a Kubernetes manifest can take down production, and “the tests passed” means much less when the real test is apply. This is a practical harness for letting coding agents work on infrastructure without giving them the keys.

What a harness is (and why infra needs a stricter one)

The short definition that stuck in 2026: agent = model + harness. The harness is everything around the model: the instruction files it reads, the tools it may call, the checks that run after it acts, and the permissions that stop it. Claude Code, OpenCode, Codex and DeepSeek’s harness all ship an inner harness; you build the outer one for your repo.

For application code, the outer harness is mostly tests and linters. For infrastructure, it also has to answer: what is this change going to *do* to real systems, and who approves that?

Layer 1: instruction files the agent actually reads

Put the rules where every agent looks:

  • AGENTS.md at the repo root (OpenCode reads it, and falls back to CLAUDE.md).
  • CLAUDE.md for Claude Code, either the same content or a one-line pointer to AGENTS.md.

Keep it short and operational:

# Infra repo rules
- Never run terraform apply, kubectl apply/delete, or helm upgrade. Plan only.
- Every change needs: terraform fmt, terraform validate, tflint, and a saved plan.
- Kubernetes manifests must pass kubeconform and our OPA policies.
- Do not edit files under envs/prod/ without the words "prod change approved" in the task.
- Secrets never go in git. Use the existing External Secrets references.

If you run more than one agent, keep one source of truth; I covered that in Syncing Opencode and Claude Code skills.

Layer 2: sensors that give the agent fast feedback

Martin Fowler’s harness engineering article describes the steering loop: the agent acts, sensors report back, the agent corrects itself. For infrastructure, good sensors are cheap and deterministic:

  • terraform fmt -check and terraform validate
  • tflint and a policy checker (Checkov, Conftest/OPA)
  • kubeconform for manifests, helm template plus kubeconform for charts
  • terraform plan -out=plan.tfplan against a sandbox or read-only state

Wrap them in one command (make check) and tell the agent to run it after every change. Agents fix their own mistakes surprisingly well when the error message is precise.

Layer 3: hooks and permissions that block the dangerous verbs

Instructions are advice. Hooks and permissions are enforcement.

  • Claude Code supports hooks that run before a tool call (PreToolUse); a hook that exits with code 2 blocks the call and returns your message to the agent. Use it to block terraform apply, kubectl delete and anything touching prod credentials.
  • OpenCode has a permission system with allow, ask and deny rules using glob patterns, and parses shell commands rather than string-matching them.

A minimal Claude Code guard script:

#!/usr/bin/env bash
# .claude/hooks/block-apply.sh (PreToolUse on Bash)
cmd=$(jq -r '.tool_input.command // ""')
if echo "$cmd" | grep -Eq 'terraform +(apply|destroy)|kubectl +(apply|delete|edit)|helm +(upgrade|install|uninstall)'; then
  echo "Blocked: this repo is plan-only for agents. Produce a plan and stop." >&2
  exit 2
fi

The most dangerous credential is the one already in your shell. Run agents in a shell or container whose cloud credentials are read-only.

Layer 4: plan-only loop with human apply

The loop that works for infra:

  1. Agent edits code and runs make check until it is green.
  2. Agent runs terraform plan (read-only credentials) and summarises the diff: what is created, changed, destroyed.
  3. A human reviews the plan in the pull request.
  4. CI applies with its own credentials after approval.

The agent never holds write credentials, so the worst case is a bad pull request, not a bad night.

Harnessability: make your repo easy to steer

Some repos are easier for agents to work in than others. Small modules with typed variables, policies as code, and one obvious way to run checks make agents dramatically more reliable. Every guardrail you add for agents also helps the next human who joins the team.

FAQ

Can coding agents safely work on Terraform?

Yes, if they are plan-only: read-only credentials, sensors that validate every change, hooks that block apply and destroy, and a human approving the plan before CI applies it.

What is the difference between AGENTS.md and CLAUDE.md?

Both are instruction files. AGENTS.md is the vendor-neutral convention read by OpenCode and others; CLAUDE.md is read by Claude Code. Keep one source of truth and point the other at it.

Should the agent ever run kubectl?

Read-only commands (get, describe, logs) are useful for context. Anything that changes the cluster should go through the same review path as a human change.