Sub topic 4
Telling the agent to work test-first
This chapter assumes you can write and run unit tests, read a Git diff, and have used a coding agent. A coding agent reads files, edits them, runs commands, and reads the output. By default it often writes the implementation first and tests afterward, if at all. To get test-first work, you have to ask for it, and you have to be specific.
Why a vague request fails
"Use TDD" leaves the agent to guess what that means. It may write the implementation and tests in one step, or skip running the tests. It may also change a test when the test gets in its way. An instruction should describe the order of actions and name what the agent must show you.
A good instruction covers four things:
- Order: the test comes before the code.
- Evidence: the agent runs the test and shows the output at each step.
- Scope: one behavior per cycle, with the smallest change that passes.
- Boundaries: which files the agent may not change, and which shortcuts are forbidden.
A working agreement
Most agents read an instruction file from the repository, such as AGENTS.md or a tool-specific rules file. The file name depends on your tool. Put the workflow there so it applies to every session.
## Working agreement for this repository
1. Work test-first. Write one failing test for the requested behavior and run it.
2. Show the failure output and confirm it fails because the behavior is missing, not because of a typo or import error.
3. Write the smallest change that makes that test pass, then run the full test suite.
4. Refactor only while all tests are green, and run the suite again afterwards.
5. Do not edit, skip, or delete existing tests to make them pass. If a test seems wrong, stop and explain why.
6. Do not hardcode expected values from tests into the implementation.Each rule maps to a later part of the cycle. Rules 1 and 2 cover the red phase. Rule 3 covers green. Rule 4 covers refactoring. Rules 5 and 6 guard against the failure modes described in later chapters.
Giving the agent a feedback command
The agent needs one command that reports pass or fail. Put it in the instruction file so the agent doesn't guess.
python -m pytest -qThe agent reads this output to decide its next step. A traceback tells it which assertion failed and what values it saw. A short, quiet output such as -q keeps the signal readable. If your suite is slow, also give a command that runs one test file, so each cycle stays fast.
The prompt for one cycle
The instruction file sets the rules. The prompt for a single task sets the behavior. Keep it narrow.
Add a `slugify(text)` function in `slugify_util.py`.
Start with only this behavior: it lowercases the text and joins words
with hyphens, so "Hello World" becomes "hello-world".
Write the failing test first, run it, and show me the output. Stop
there and wait for my go-ahead before implementing.The last sentence matters. Stopping after red gives you a checkpoint. You can confirm the test asserts the right thing and fails for the right reason before any code exists. Once you are comfortable with an agent, you can let it continue through green on its own and review the whole cycle at the end.
sequenceDiagram<br>
Common mistakes in instructions
- Asking for tests and code together. "Implement X with tests" lets the agent write both at once, so the test never fails first.
- Listing no stop point. Without one, the agent may build several behaviors in one pass and hand you a large diff.
- Forbidding nothing. If the rules don't say tests are off limits, a failing test looks to the agent like something to fix.
Exercise
You tell an agent "Implement slugify using TDD." It returns an implementation and three passing tests in one step. Name two things missing from your instruction that would have prevented this, and say what you would add.
Show answer
The instruction gave no order and no evidence requirement. Add "write one failing test first, run it, and show the failure output" to fix the order. Also add a scope limit, such as "one behavior per cycle, then stop," so the agent doesn't produce several tests and the implementation together. A rule against editing existing tests is also worth adding.