Harness Engineering: Making Coding Agents Reliable on Long-Running Tasks
Coding agents fail on long tasks not because the underlying model is weak, but because the runtime system wrapping it lacks structure. This article explains Harness engineering—the discipline of designing the system that controls what the model sees, what it is allowed to do, how results flow back, and when a task is declared complete. It covers the execution loop, a pagination example, two core criteria (resumable state and independent evidence of completion), layered implementations for long tasks, matching complexity to task size, the relationship to prompt and context engineering, and a practical checklist for putting it all into production.
1. What a Harness Is: The Runtime System Around the Model
A large language model is a stateless generator with no execution capability. It is good at reasoning and generating, but it cannot read files, run tests, or remember where it left off on its own. What actually makes an agent “keep working” is the runtime system wrapped around the model—the harness. It determines what information the model sees, which operations it is allowed to perform, how execution results are fed back, and what criteria establish that the task is done. Designing and improving this system is Harness engineering.
A responsibility table helps clarify the boundary:
| Responsibility | Owner | Description |
|---|---|---|
| Reasoning and generation | Model | Decides the next step, how to change code |
| Information assembly | Runtime system | Task description, environment facts, history and progress |
| Actual execution | Runtime system | File reads/writes, command execution, test invocation |
| Boundaries and permissions | Runtime system | Path whitelists, interception of dangerous operations |
| State persistence | Runtime system | Cross-session progress and pending items |
| Stop conditions | Runtime system | When to continue, retry, escalate, or terminate |
The point of this table: if any responsibility has no owner, the task fails at that step. No matter how strong the model is, it cannot replace the enforced constraints of the outer program.
2. The Skeleton of One Execution Loop
Abstracting one execution into a loop reveals the inputs and outputs of each step:
loop:1. Assemble context: task goal + environment facts + prior results + last progress2. Model outputs a structured action request (read / edit / execute / query)3. Outer-layer validation: is the path within allowed scope, are parameters valid, is it high-risk4. Outer-layer execution: real reads/writes and command invocation, capture exit code and output5. Feed results back: return diffs, stdout, and error stacks to the model6. State decision: continue / retry / report blocked / terminate
The critical constraint is step 3. “Do not access this directory” cannot rely on a line in the prompt asking the model to comply—prompts are suggestions; validators are boundaries. Likewise, the quality of the information fed back in step 5 directly determines the quality of the model’s next decision. Returning only “test failed” without the failing case and assertion diff leaves the model guessing blindly.
3. Example Scenario: Adding Pagination to an Order Query API
Suppose the task is “add pagination to the order query API.” This is an illustrative scenario, not a real customer case, but it is enough to expose the actual workload.
The model needs up-front information: how the repository is started and built, the API’s existing parameter and response conventions, which files are within the editable scope, and how tests are executed. The tool capabilities it needs include: reading files, precise edits, running tests, and viewing diffs. When a test fails, the error must be sent back for further handling rather than immediately terminating the task as failed.
If the task stretches long or gets interrupted, progress must be preserved: which changes are complete, which cases still fail, and what the next step is—so the next run can resume precisely.
4. Two Core Criteria
Two things matter most in understanding Harness engineering.
First: can the task state be resumed? The test is not “did we write logs,” but “given a brand-new session, can we recover from any interruption point without repeating or missing work?” Resumability requires state that is structured and machine-verifiable, not a natural-language summary.
{"task": "orders query pagination","done": ["add page/size params", "update response meta"],"pending": ["boundary cases", "permission regression"],"failing_tests": ["test_orders_pagination_boundary"],"next_step": "fix boundary handling for size=0"}
Second: is there independent evidence of completion? “I have finished” is just model-generated text; it is not evidence. Real acceptance must check default pagination behavior, boundary parameters (page 0 or negative, oversized page size), whether permission filtering still works, and whether the relevant regression tests are all green. What specifically gets checked is determined by the API contract, not by the model’s self-report.
There is also an easily overlooked anti-cheating constraint: the model must not “prove” success by deleting or rewriting failing tests. Changes to test files must therefore be reviewed separately, or test files must be placed outside protected paths.
5. A Layered Implementation for Long Tasks
Common industry approaches to long tasks fall roughly into four layers—a useful reference, but not every task should follow them blindly:
- Initialization: prepare environment dependencies and produce a checkable feature list as the acceptance baseline.
- Incremental progress: break the list into small batches, one session per batch, to avoid unbounded context growth.
- Progress recording: maintain a structured progress file tracking completed items, pending items, and failing cases.
- Fact-based resumption: use version-control history and test results as an objective source of truth, cross-checked against the progress file to prevent records drifting from reality.
6. Scale Should Match the Task
A heavier harness is not a better harness.
- Single-step, tool-like tasks: a clear interface definition plus a result check is usually enough; adding progress files and recovery machinery is pure overhead.
- Multi-hour, cross-session software changes: resumption, recovery, and independent acceptance become necessities.
Complexity should be proportional to task duration and recoverability requirements—this is the first principle of harness design.
7. Relationship to Prompt Engineering and Context Engineering
The three overlap but carry different responsibilities: prompts define what to do right now; context engineering organizes what should be visible right now; the harness is additionally responsible for tool execution, persistent state, and stop conditions. Different projects may name these modules entirely differently—what matters is that every responsibility has a clear owner.
8. Putting Repository Convention Files to Work
A conventions file at the repository root can document the repo structure, development conventions, and test commands—but it is only a “declaration.” Writing “tests must pass before commit” in a file does not mean tests were actually run. To make it effective, you need three more things: an executable path that invokes the tests, decision logic that checks the results, and a handling strategy when things fail (block the commit or send it back to the model for repair).
Convention file (declaration layer)↓ read by the runtime systemExecution path (invocation layer): build → lint → test↓Result evaluation (evidence layer): exit code + test report↓Failure handling (feedback layer): feed back errors, forbid declaring completion
Missing any layer reduces the conventions to documentation decoration.
9. An Implementation Checklist
- Are permission boundaries enforced by program, not by prompt?
- Do tool results fed back to the model include sufficient error detail?
- Can execution resume from structured progress after an interruption?
- Is completion judged on independent evidence?
- Are test and acceptance files protected from being “edited green”?
- Does harness complexity match the task’s scale?
- Does every responsibility have a clear owner?
When deploying to the cloud, a container platform can host the execution environment, object storage can persist progress state, and logging and monitoring services can observe the execution pipeline—these generic capabilities are sufficient to support a recoverable, auditable runtime system.
10. Common Pitfalls
First, treating the harness as just the prompt, leaving boundaries and state unowned. Second, over-engineering—applying full recovery machinery to single-step tasks. Third, accepting the model’s self-report as the acceptance verdict. Fourth, letting progress records drift from the actual code state, causing duplicated changes on resume.
11. Summary
The essence of Harness engineering is converting “can the model do this?” into “can the system reliably get it done?” By holding onto the two main threads—resumable state and independent evidence of completion—sizing complexity to the task, and assigning a clear owner to every responsibility, you can keep coding agents reliable across long-running tasks.
