Harness Engineering. Part 3: Tell the agent when it's wrong
This is the third post in the Harness Engineering series. If you missed the previous ones, start with Part 1: Evolution of AI-assisted workflows and Part 2: The human is the loop.
In the previous part, we ended with a simple harness that allows us to run longer sessions. Now, let’s discuss what it takes to delegate more complex tasks to it, and how we can let the agent run unsupervised. Or simply: how does the agent find out it’s wrong when you stop watching every edit?
Stage 2: One project, one branch, no IDE
You stop telling the agent what to do and start telling it where to go.
In Stage 2, prompts become goal-based.
The agent runs longer, and you check the result instead of every edit. As we said in Part 1, that only works if the environment can tell the agent when it’s wrong, so that it can self-correct and adjust.
Stage 2 brings a long list of challenges: reliable feedback, automatic context management, multi-model flows, sub-agents, conventions, permissions. We start with feedback because everything else depends on it. An agent that can’t tell a good result from a bad one will confidently finish the wrong task, no matter how good its context is.
Instructions are not feedback
In Stage 1, we focused our attention on the context: instructions, skills, explicit references. Context tells the agent what should happen. It doesn’t tell the agent what did happen.
When you were watching every step, you were the feedback. You saw the failing build, the broken page, the wrong behaviour and messed up conventions, and you said so. Once you step back, someone else has to do that job.
The rule of thumb:
If a requirement can be checked by a tool, don’t put it in the instructions. Turn it into a check.
A failing check is a much stronger signal than a sentence the model may or may not follow. It’s also cheaper: it doesn’t sit in the context window of every session.
What good feedback looks like
Not every check is equally useful to an agent. Good feedback is:
- Deterministic. The same code gives the same result. A flaky test teaches the agent to ignore failures.
- Fast. The agent runs it many times per task. A check that takes 20 minutes will be skipped or run once at the end.
- Specific. It names the file, the line and the rule. “Build failed” is much less useful than
orders.ts:42 - Property 'total' does not exist on type 'Order'. - Local. The agent can run it in its own environment, not only in CI after a push.
- Consistent with CI. If the local check passes and CI fails, the agent learned the wrong thing.
The feedback layers
Think of checks as a layered cake: cheap and narrow at the bottom, expensive and broad at the top.
Formatters and linters
The cheapest signal and the easiest win. They catch style issues and a surprising number of real bugs, and many of them can fix the problem automatically.
This is also where a lot of instructions can be retired. Instead of “Never import from internal/ outside of the package” in AGENTS.md, add a lint rule:
// eslint.config.js
export default [
{
rules: {
"no-restricted-imports": ["error", { patterns: ["**/internal/**"] }],
},
},
];
Now the convention is enforced in every session, and the error message tells the agent exactly what to fix.
Type checks and builds
Type checkers and compilers verify that the pieces fit together. For an agent that just edited five files, this is the first real confirmation that the change at least compiles. I know: most of the time, agents operate in an environment where they have access to the compiler, but you’d be surprised.
Unit tests
Tests are where you encode behavior and expectations. Low test coverage or a soup of meaningless low-quality tests make the agent’s work harder. On the other hand, high-quality tests that codify defined contracts help any system gather quick feedback and keep these contracts unbroken.
Agents are good at writing tests when given some guidance; they are also good at finding low-quality tests, make sure to revisit your test suite every now and then.
Integration, end-to-end and acceptance tests
Slower and broader. They catch the problems unit tests miss: wiring, configuration, data access, contracts between services. They are usually too slow for every edit, so the agent should run the relevant subset while iterating and the full suite before it calls the task done. But they still should be:
- easy to execute
- available in the environment where the agent operates
The second bullet point is important. When we isolate the agent in a sandbox (cloud or local) we need to make an effort and provide it with options to verify its own work before handing it over to the human.
Architecture and invariants
When reviewing many examples of successful agentic sessions and comparing them with the sessions that disappoint developers, I notice one pattern: codebases with clear and consistent architecture are better suited for agents.
LLMs are good at catching patterns and following them. They are also trained on and understand standard patterns and conventions. Which means two things:
- we need to make sure agents do not ruin good architecture
- we can use agents to validate, fix and improve legacy codebases to make them more agent-friendly
Stopping the agent when it takes shortcuts is crucial. Whenever you see that - codify the convention, make it a test, linter rule, part of review process. Whatever works and whatever is possible in your case.
Let the agent see what it built
For UI work, passing tests don’t mean the page looks right. Browser tools like Playwright let the agent open the page, click through the flow and check the result. The Playwright MCP server gives the agent the same capability interactively during a session.
For non-web UI projects we also need to give an agent a way to receive visual feedback. Be it a tool that allows it to interact with a phone emulator or access to the native desktop app interface. As much as it seems hard to do, the investments pay off.
An agent that can look at its own work catches the obvious visual mistakes before you do.
Make the loop easy to run
Having checks is not enough. The agent has to know about them, be able to run them and understand the output.
One command. Give the agent a single entry point that runs the validation steps in order and stops at the first failure.
{
"scripts": {
"check": "npm run lint && npm run typecheck && npm run test"
}
}
Tell the agent when to use it. This is one of the few things that can belong in the repository instructions:
Run
npm run checkafter every change. A task is not done until it passes.
Make failures readable. Long stack traces and thousands of lines of logs pollute context and bury the actual error. Depending on the toolchain we may want to tune the verbosity level. It’s something that we need to be looking at and adjusting when needed.
Make the environment ready. The agent can’t run tests if dependencies are missing or the database isn’t there. Locally, that’s a setup script. For Copilot cloud agent running on GitHub Actions, .github/workflows/copilot-setup-steps.yml preinstalls tools and dependencies before the agent starts working.
Reading failures and iterating
With feedback in place, the session becomes a loop: change → check → read the failure → fix → check again. A few things keep that loop healthy.
- Fix the cause, not the check. A common failure mode is an agent that makes the check pass by changing the check: skipping a test, loosening an assertion, adding a lint suppression.
- Know when to stop. If the same check keeps failing after several attempts, the agent is probably missing context or the task is wrong. It’s better to stop and report than to keep guessing.
- Treat flaky checks as bugs. A test that fails randomly trains the agent, and you, to ignore red. Fix it or quarantine it.
- Enforce checks when needed. With hooks, we can force certain checks to run in a deterministic way.
A short checklist
- Every convention that a tool can check is a check, not an instruction.
- The agent has one command to run the checks, and the instructions tell it when to run it.
- Checks are fast, deterministic and produce readable failures.
- Local checks match CI.
- The agent’s environment can run the checks without a human preparing it.
- Weakening tests or lint rules is called out in the instructions and in review.
Approvals
When we have our goal in mind - sessions that call for our attention only when a decision should be made - we have to minimize the noise. Manually approving tool calls becomes dangerous: you lose focus, you stop paying attention, you blindly hit “yes”. This defeats the purpose of approval checks.
Instead, we need to restrict the environment and limit the approvals as much as we can. In some cases we need to stick with a combination of auto-approval (LLM based risk assessment) and hard guards (hooks or/and harness-based deny lists). In other cases, we may afford “yolo” mode if the environment allows us.
It’s not possible to block every possible dangerous request, that means we need to isolate our agents:
- built-in sandboxes
- docker-based environments (dev containers)
- microVMs
Placing the agent where it cannot do any harm (or where the blast radius is acceptable) is a safer approach and it unlocks unattended sessions that we aim for.
Context management
When we no longer manage context manually, we need to rely on built-in capabilities.
Providing access to the information needed
Having a curated list of tools that the agent can use to search for information removes the burden of us providing it directly. This includes:
- documentation reachable by the agent
- MCP-servers relevant to the task
- local cli tools with reasonable level of access permissions
Context window hygiene
Longer sessions hit the context window size limit at some point. Modern harnesses deal with that reasonably well by running autocompaction when it’s time. But compaction means some information is removed from the context. It’s ok when it’s not needed anymore and less fun when something crucial was there.
We can encourage the agent to use sub-agents (sessions that run in their own context) and offload noisy parts of the workflow there. For example: research tasks that may load many lines of information, analyze it and provide compact gists – there is no need to do that in the main session, it only needs the result. A perfect candidate for a sub-agentic run.
This way we can reduce token pressure on the main session and avoid compaction.
Whenever compaction is inevitable, the outer-loop harness should let the agent recover missing bits of information: plans and to-do lists, docs and MCP servers to the rescue.
Multi-model flows
We know that not every task needs the latest frontier model with the higher reasoning effort level. This already means we should be using different models for our tasks. But, since we’re talking about building the reliable long-running sessions that can verify their own work, we also can look at model choice from that point of view.
A cheaper model may benefit from higher-tier model’s opinion.
- A pattern where the main session may run a sub-agent with a more capable model to validate the plan, the work in-progress or the result may produce better results at lower price.
Cross-checking work by calling out a model from another vendor may cover some blind spots and biases
/rubber-duck- built-in skill in Copilot will do exactly that
Elevating the model or effort level when needed
- when the agent gets stuck or lost we can pass the control to a more powerful model
Codifying these workflows manually, or by using smart model routers like Copilot Fusion, we can give our agents a way to deliver quality work at lower cost.
Combining these techniques with purpose-scoped code, security and requirements review agents, we can automate validation of guard rails (quality and security checks) with end state verification (checking if the implementation matches expected end-state and covers all requirements).
Memory
Whenever our agents run, they make discoveries about the codebase and environment. As LLMs are stateless these findings disappear when you discard the session.
This calls for something more durable. Typically that’s called Agent Memory. Be it a set of md files in the repository and instructions to maintain them, or something more sophisticated, such as GitHub Copilot Memory (cloud-based collection of memories shared and maintained by local, cloud, code review agents and shared across the team), all improve the session quality.
Memories must be created and invalidated automatically, this prevents our instruction files from growing and ensures that outdated facts are removed instead of confusing the next agent.
Harness engineering is a process
I don’t think it’s possible to set these guardrails right in one go and never look back. Your codebase evolves, environment is not fixed in stone and conventions change.
Reflection on the past sessions should become a frequent exercise. If we start treating agent misses as incidents, we need to review why the problem happened, think about how we can prevent it from happening in the future (you know, just like we deal with production incidents), but we also need to verify if old checks remain valid. Outdated instructions and tests that enforce conventions that are no longer applicable may not prevent the agent from getting its job done, but would waste our tokens and make sessions unnecessarily longer.
Setting the goal
Once we have strong enough harness (a collection of tools for the agent to validate its own work and a set of guardrails that prevent the agent from making mistakes) we may start prompting our agent with goals instead of detailed plans.
Goals should be both well scoped and verifiable. Basically, by explaining the desired end-state of the system and providing steps to verify that state against clear expectations (acceptance criteria) we can send the agent into autonomous mode and let it reach the goal without our supervision.
What’s next in the series
With a harness that can verify its own work, a single session can run unattended for much longer. While one agent works, we wait. The idle time is not ideal, we can use that time to build more!
In the next part, we’ll look at running sessions in parallel, at the problems that come with it and tools that can address these problems.
Stay tuned!
Further reading
- GitHub Docs: Configure the development environment for agents on GitHub Actions
- Playwright: Getting started
- Playwright MCP server
- GitHub Docs: About GitHub Copilot Memory
- GitHub Docs: About GitHub Copilot code review
- GitHub Docs: About GitHub Copilot Fusion
Disclosure
I’m employed by GitHub at the time of writing this post. All opinions are my own.