Sub topic 8
Keeping Each Cycle Small
This chapter assumes you can write and run unit tests with a framework such as pytest, read a Git diff, and have used a coding agent. It covers why one behavior per cycle matters when an agent writes the code, how to size a step, and what goes wrong when steps grow.
Why size matters more with an agent
A person doing TDD feels the pain of a big step right away. The test stays red for a long time, and the next failure is hard to trace. An agent does not feel that pain. Given a large request, it will produce a large change with confidence, and the result is hard for you to check.
Small steps help in four ways:
- Diagnosis. When the suite goes red after a small change, the cause is in a few lines. After a large change, the cause could be anywhere.
- Review. You can check a ten-line diff against one test in under a minute. A two-hundred-line diff tends to get skimmed.
- Fewer shortcuts. An agent facing one narrow failing test has little room to hardcode values or rewrite unrelated code. Wide tasks give it more room to cut corners.
- Cheap recovery. If a cycle goes wrong, you discard a few lines and ask again. You do not untangle an hour of work.
A cycle is one pass through red, green, and refactor for one behavior. The goal is a cycle small enough that you can say in one sentence what changed and why.
What counts as one behavior
One behavior is one observable rule that you can state as "given this input, the result is that." It is not one function, one file, or one ticket.
For the slugify example from earlier chapters, the full requirement might read "turn a title into a URL slug." That is a feature, not a behavior. It breaks down like this:
- Lowercase the text and join words with hyphens.
- Remove punctuation.
- Collapse repeated separators into one hyphen.
Each item gets its own test and its own cycle. Here is the first test again, followed by the two that came in later cycles:
# test_slugify.py
from slugify_util import slugify
def test_lowercases_and_joins_words_with_hyphens():
assert slugify("Hello World") == "hello-world"
def test_strips_punctuation():
assert slugify("Hello, World!") == "hello-world"
def test_collapses_repeated_separators():
assert slugify("a -- b") == "a-b"A useful check is the "and" test. If you describe the test as "it lowercases and strips punctuation," it covers two behaviors. Split it.
Signs a step is too big
- The test name needs the word "and."
- The test has several unrelated assertions, so one failure could mean several different things.
- The agent needs to touch more than one or two functions to turn the test green.
- The diff is longer than you can read in a couple of minutes.
- You cannot predict, before running it, what the failure message will say.
The last sign is the most reliable. In the red phase you should know why the test will fail. If you cannot predict it, the step is not yet well understood.
The shape of one small cycle
Every cycle starts and ends on a green suite. That gives you a known good state to return to. If anything in a cycle goes wrong, you reset to the last commit.
Worked example: growing slugify one step at a time
Each step below shows what you ask the agent and what you check.
Cycle 1: lowercase and hyphens
Request: "Add the test test_lowercases_and_joins_words_with_hyphens. Run it and show me the failure. Do not implement anything yet."
The expected failure is an import error or a missing function, because slugify_util.py does not exist. That is a valid red for the very first cycle, since the behavior is absent. Then ask for the smallest change:
# slugify_util.py
def slugify(text):
return text.lower().replace(" ", "-")This passes. It is deliberately naive. Handling punctuation now would be a second behavior in the same cycle, so you leave it for later. Run the suite and commit.
Cycle 2: punctuation
Add only test_strips_punctuation. Run it. The simple implementation returns "hello,-world!", so the assertion fails because of missing behavior, which is the right reason. Ask the agent to make that one test pass without breaking the first.
Cycle 3: repeated separators
Add only test_collapses_repeated_separators. The input "a -- b" fails against the earlier code. Once the test is green, the refactor step has something real to do. A regular expression that extracts runs of letters and digits handles all three tests at once:
# slugify_util.py
import re
def slugify(text):
words = re.findall(r"[a-z0-9]+", text.lower())
return "-".join(words)The refactor happened under green tests, so the three tests prove the behavior did not change. Run the suite one more time and commit.
Telling the agent the step size
Agents tend to run ahead. Ask for a large feature and they will write the tests, the implementation, and some extras in one go. State the limit in your request or in the agent's instruction file. For example:
## Step size
- Add exactly one new test per cycle, then stop and show the failing output.
- Do not implement behavior that no failing test requires.
- Do not change more than one function per cycle unless a refactor step needs it.
- After the suite is green, stop and wait for review before starting the next behavior.The last rule matters most. Without a stop point, the agent moves to the next behavior by itself and your review checkpoint disappears.
Pausing at the red step
A common routine is to split the agent's work into two requests. First: "Write the failing test only, and run it." You read the failure and decide whether it is the right one. Second: "Now make it pass with the smallest change." This gives you a decision point exactly where a wrong test would otherwise get baked in.
Edge cases
The first test needs scaffolding
The very first cycle in a new project often needs a file, a module, or a configuration. That setup is not a behavior. Do it as its own small step, with a trivial passing or skipped placeholder, and commit it separately. Then start behavior cycles.
Some behaviors cannot be split
Occasionally a rule cannot be built in halves. For example, a function that must handle both the empty string and a normal string may need one shape of code to satisfy both. When this happens, pick the simplest case first and let the next test force the generalization. Writing a test for the empty string first is often the way in:
def test_empty_string_gives_empty_slug():
assert slugify("") == ""A green test that was never red
If you write a test and it passes immediately, do not move on. Either the behavior already existed, or the test does not check what you think. Both are worth knowing before you continue. An agent may also report "test passes" without having shown a red run, so ask to see the failing output each time.
The refactor is large
A refactor is also a step and should be small. Rename, extract, or simplify one thing, run the suite, and repeat. If a refactor needs several tests to be edited, it is not a refactor. It is a behavior change and needs its own cycle.
Changes you did not ask for
Agents sometimes fix a nearby issue while passing the test: reformatting a file, renaming a variable, adding a parameter. Each one widens the diff. Either ask for these to be removed or put them in a separate commit, so the cycle's commit contains only the change its test requires.
Step size and the cost of feedback
Small steps mean more runs of the test suite. That is only practical when the suite is fast. Run a single test file or a single test while you work, then the full suite before each commit:
python -m pytest -q test_slugify.py::test_strips_punctuation
python -m pytest -qIf the full suite takes minutes, long cycles start to feel attractive, and step size grows. Fix the slow suite before you give in to larger steps.
A summary table
| Question | Small-step answer |
|---|---|
| How many new tests per cycle? | One |
| How many behaviors per test? | One |
| When does the agent stop? | When the suite is green and the diff is ready for review |
| What gets committed? | One verified cycle |
| What if a cycle goes wrong? | Reset to the last commit and retry with a smaller step |
Common errors
- Batching tests. Asking for five tests and then an implementation. The first green run then covers five behaviors and the agent may build a general solution that you have not reviewed.
- Skipping the commit. Doing three cycles and committing once. You lose the ability to revert one cycle.
- Letting the step grow when stuck. When a cycle is hard, the instinct is to ask for more. The better move is a smaller test that isolates the difficulty.
- Accepting extra code. The agent adds a branch that no test requires. Untested code is a liability, so ask for it to be removed.
Exercise
You ask an agent to "add a slugify test for accents and punctuation and non-ASCII letters, then implement it." The agent returns a 90-line diff with four new tests. Name two problems with this request and describe how you would restructure it into cycles.
Show answer
The request bundles at least three behaviors (accents, punctuation, non-ASCII letters) and asks for tests and implementation together, so you cannot confirm each test fails for the right reason and the diff is too large to review well. Restructure it as separate cycles. In each one, ask for a single failing test, run it and check the failure, request the smallest change that passes, run the full suite, review the small diff, and commit. Handle punctuation first, then accents, then other non-ASCII letters, with a commit after each.
Exercise
A new test you wrote passes the first time you run it, before any implementation change. What should you do, and why?
Show answer
Do not continue. Check whether the behavior already exists in the code or whether the test fails to check what you intended, for example because of a wrong assertion or the wrong input. A test has value as a specification only if you have seen it fail for the right reason. Fix or replace the test until you see a meaningful red.