Harness Engineering: Turning Agent Constraints into Executable Guardrails
This article explains Harness Engineering, the practice of wrapping coding agents in executable constraints, layered context, automated verification, and structured feedback. It breaks down the four-layer harness structure, outlines a step-by-step adoption path, and highlights common pitfalls, showing why constraint design often delivers more value than switching to a stronger model.
1. The Problem Is Not a Weak Model, but an Unconstrained Environment
Many teams fall into the same loop after adopting AI coding agents: the agent writes a version of the code, review finds it violates a project convention, so the convention is added to the documentation. The next time it hits a different pitfall, another rule is added. After dozens of rounds, the document has grown to hundreds of lines, mixing architecture standards, naming conventions, exception-handling strategies, and a list of prohibited practices, yet the agent’s error rate has not dropped significantly.
This reveals a commonly overlooked fact: rules written in natural language are “reference material” to an agent, not “enforced constraints.” A new human hire gradually internalizes organizational rules through questions, trial and error, and reminders from colleagues. Agents lack this social feedback loop and act only on the literal meaning of the prompt. Therefore, turning engineering constraints into executable, verifiable mechanisms is the prerequisite for stable agent output. This is the core proposition of Harness Engineering.
A harness can be understood as the entire “operating frame” wrapped around the model: context supply, tool interfaces, validation rules, execution sandboxes, and feedback channels. The model generates; the harness defines the boundaries and decides whether the result is acceptable. The focus of engineering effort therefore shifts from “writing better prompts” to “designing better constraints and verification.”
2. Key Signal from the Case: Delivery Pace Is Determined by Guardrails
A frequently cited industry experiment involved a few engineers who, over several months, started from an empty repository and used AI agents to build a product exceeding one million lines of code, merging roughly 1,500 change requests, at a rate of several per person per day. All code was generated by agents, with no hand-written implementation.
What matters is not the output volume but the reason for the early slowdown: the agents could code, but did not understand the dependency structure, were unclear about architecture boundaries, and had no way to verify whether their output was correct. As a result, the code they wrote “compiled but headed in the wrong direction,” and human effort was heavily consumed by review, rework, and explaining “why this cannot be done this way.” This closely resembles a new hire’s onboarding period: the capability is there, but the rules are unclear.
The three things the team did next form the skeleton of Harness Engineering:
- Turn architecture constraints into machine checks. They defined a strict one-way dependency order (types → config → data access → business services → runtime → UI) and implemented it as custom static checks. The CI pipeline directly rejects cross-layer calls, removing reliance on verbal reminders during review.
- Make context a map, not an encyclopedia. They maintained a collaboration document of about one hundred lines containing only the most critical navigation information: directory responsibilities, dependency direction, and verification entry points. Less but more accurate information makes it harder for the agent to be led astray by irrelevant details.
- Give verification authority back to the agent. Each working copy starts an independent application instance, and the agent operates pages directly through the browser debugging protocol, reads logs, and confirms whether the UI and behavior meet expectations without waiting for human inspection.
One detail is especially worth recording: the team initially spent about twenty percent of each week cleaning up “low-quality accumulation” from the agents—inconsistent style, over-abstraction, and oddly named code. This fixed overhead is the inevitable cost of lacking a constraint layer.
3. The Essence: The Four-Layer Structure of Harness Engineering
3.1 Constraint Layer: Compile Standards from Documents into Rules
The reliability of a standard depends on whether it is executable. The test is simple: can a violation fail automatically? Layered dependencies, module visibility, interface signature stability, and prohibited dependency categories are all suitable for static checks, dependency analysis, or build-time assertions.
# Illustration: dependency-direction check logic (pseudocode)ALLOWED = { "types": [], "config": ["types"], "repo": ["types","config"],"service": ["types","config","repo"], "runtime": ["service"],"ui": ["runtime"] }for edge in dependency_graph.edges():if edge.target not in ALLOWED[edge.source]:fail(f"Cross-layer dependency: {edge.source} -> {edge.target}")
The value of a rule lies in where the feedback appears: the earlier it fails, the lower the fix cost. Placing constraints before commit and in the early stages of CI lets the agent learn within seconds that “this path does not work,” rather than being told so by a human during review.
3.2 Context Layer: Layered Supply, Loaded on Demand
More context is not better; more accurate context is. A practical approach has three layers: a global map (stable directory responsibilities and dependency direction), module-local notes (the module’s interface contracts and data structures), and task-level information (relevant files, failure logs, reproduction steps). Keep the global layer short and prune it regularly, let the local layer evolve with the code, and have tools retrieve and inject the task layer at runtime. This avoids filling the prompt space with redundant information and reduces the chance of the agent “misremembering the rules.”
3.3 Verification Layer: Give the Agent Closed-Loop Judgment
An agent without verification capability can only produce code that “looks right.” Reusable verification means include type checks and unit tests, build artifact startup checks, API contract tests, end-to-end flow scripts, and running instances in an isolated environment to collect logs and page state. The key design is isolation and parallelism: provide each task with an independent working copy and an independent running instance so they do not interfere, allowing the agent to experiment boldly without polluting the main branch.
3.4 Feedback Layer: Structure the Failure Information
Agents handle structured input well. Compiler errors, test failure stack traces, static check reports, and performance threshold breaches should all be fed back as standard input rather than paraphrased by a human. Once the feedback channel is stable, the human role shifts from “correcting errors one by one” to “designing constraints and acceptance criteria.”
4. Adoption Path: From Zero to Managed Agent Output
- Automate the most painful architecture rule first. Prioritize recurring points of contention in current reviews, such as layered calls or reverse dependencies on shared modules. Keep rules few and hard, and first establish trust that “a violation means failure.”
- Build a minimal verification loop. At minimum, include type checks, unit tests, and one startup verification; when possible, add isolated instances and browser automation for self-checking UI-related changes.
- Rewrite the collaboration document as a short map. Keep it around one hundred lines, retaining only directory responsibilities, dependency direction, verification command entry points, and prohibited practices; move the rest into nearby module notes.
- Define acceptance criteria and how to handle low-quality output. Make clear that style consistency is guaranteed by formatting and static checks, and that abstraction level and naming are constrained by a review checklist, preventing the agent from substituting a “more general abstraction” for a clear implementation.
- Measure rework rate, not output volume. Track the first-pass rate of change requests, the proportion of rework caused by constraint violations, and the share of weekly time spent cleaning low-quality code, using data to judge whether the constraint layer is actually working.
5. Common Pitfalls
- Treating documentation as a constraint. Documents can guide but cannot block; a standard without automated checks is no standard at all.
- Stuffing in all knowledge at once. Context overload makes the agent ignore key clauses; a map beats an encyclopedia.
- Adding rules without verification. Rules improve the correctness rate, but verification determines the ceiling; without verification, errors surface only at a later stage.
- Ignoring cleanup cost. Low-quality output accumulates into a structural burden and must be explicitly governed in the process rather than left for “a refactor later.”
6. Conclusion
The essence of Harness Engineering is turning an organization’s engineering knowledge from human memory and verbal communication into machine-executable constraints, accessible context, and automated verification. Model capability sets the ceiling; the constraint system sets the floor. When the guardrails are clear enough, agent output can shift from “a draft that needs human backup” to “a deliverable that can be continuously merged.” For teams that want to use the engineering capabilities of platforms such as Baidu AI Cloud to carry the build, verification, and monitoring/alerting chain, completing the constraint layer and feedback loop first often yields more obvious gains than switching to a stronger model.
