Sub topic 4
Scoping the Task and Defining Done
This chapter assumes you can read Python, use Git branches and diffs, and run a test suite from the command line. It also assumes you have used a coding agent before. Here you will learn how to turn a vague request into a task the agent can finish and you can check.
Why scope matters
A coding agent works in a loop: it reads files, edits them, runs commands, and looks at the results. It stops when it believes the task is done. If you give it no definition of done, it decides for itself. That is how a request to "clean up pricing.py" turns into a rewrite of three modules.
A scoped task removes that guesswork. It names five things:
- The goal, in one sentence.
- The files in scope.
- The behavior that must not change.
- A way to verify the result.
- A request to plan before editing.
A scoped task description
Compare a vague request with a scoped one.
Vague:
Clean up the invoice code and make the tax part nicer.Scoped:
Refactor pricing.py: extract the tax calculation from format_invoice into its own function named calculate_tax.
Constraints:
- Do not change the public signature or the return value of format_invoice.
- Touch only pricing.py and tests/test_pricing.py.
- Run `pytest tests/test_pricing.py` before and after the change and report both results.
First describe your plan in a few bullet points. Wait for my approval before editing.Each part of the scoped version does a job:
| Part | Text in the example | What it prevents |
|---|---|---|
| Goal | Extract `calculate_tax` | Open-ended "improvements" |
| Files in scope | Only `pricing.py` and its test | Edits to unrelated modules |
| Unchanged behavior | Same signature and return value | Silent API changes |
| Verification | Run `pytest` before and after | Claims of success with no evidence |
| Plan first | Wait for approval | Surprises after the edits are made |
Running the tests before the change matters. It gives you a baseline. If a test already fails, you learn that before the agent touches anything, and you will not blame the agent for it later.
What "done" looks like
Done should be something you can observe, not a feeling. Good criteria are checks a command can answer:
pytest tests/test_pricing.pypasses, with the same number of tests as before.git diff --statlists only the files you named.format_invoicehas the same signature as before.- No new dependencies appear in the project's dependency file.
For the example task, the result should look like this:
TAX_RATE = 0.2
def calculate_tax(subtotal):
return round(subtotal * TAX_RATE, 2)
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = calculate_tax(subtotal)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}The structure changed. The inputs and outputs did not. That second property is the one your definition of done must protect.
Common scoping mistakes
- Goal and method mixed together. "Make it faster and also rename everything" is two tasks. Split them.
- No boundary. Without a file list, the agent may fix "related" code it finds along the way.
- Unverifiable criteria. "Looks cleaner" cannot be checked. "Tests pass and the diff touches two files" can.
- Forgetting what must not change. Agents tend to preserve what you mention. State the public signature, the return shape, and any output format you rely on.
Exercise
Exercise
A teammate writes this request for an agent: "Update the date handling in the app to be more modern." Rewrite it as a scoped task. Include the goal, the files in scope, one behavior that must not change, a verification step, and a plan-first request. Assume the code is in utils/dates.py and its tests are in tests/test_dates.py.
Show answer
One acceptable answer: "Refactor utils/dates.py: replace the manual string slicing in parsedate with datetime.strptime. Constraints: do not change the signature of parsedate or the values it returns for valid input; touch only utils/dates.py and tests/testdates.py. Run `pytest tests/testdates.py` before and after the change and report both results. First describe your plan in a few bullet points and wait for my approval before editing." The answer is correct if it has a single concrete goal, a named file list, a stated unchanged behavior, a runnable verification command, and a plan-first request.