Sub topic 4
Scoping the Task and Defining Done
A coding agent works from the task description you give it. A vague description leaves the agent to guess the boundaries, and those guesses become edits you didn't ask for. This chapter covers how to write a task description that sets the goal, the scope, the constraints, and a check that tells you when the work is finished.
This chapter assumes you can read and write code in at least one language, use basic Git (branches, commits, diffs), and run a test suite and a linter from the command line. It also assumes you have used an AI coding assistant before.
Why scoping matters more for existing code
In a new project, a loose request produces something you can accept or reject as a whole. In existing code, a loose request produces edits scattered across files that already work. Each edit can change behavior that other code depends on.
An agent has no way to know which parts of your codebase are fragile, which are frozen by a contract with another team, or which are about to be deleted. It reads the files it can reach and acts on what it finds. Scoping is how you supply that missing knowledge.
Compare two requests:
- "Clean up the pricing module."
- "Extract the tax calculation from
format_invoiceinto a function namedcalculate_tax, and change nothing else."
The first can mean renaming variables, reformatting, changing rounding, deleting "unused" helpers, or rewriting the module. The second has one visible outcome that you can check in the diff.
The parts of a scoped task
A well-scoped task description has five parts. Not every task needs all five at full length, but each one answers a question the agent would otherwise guess at.
| Part | Question it answers | Example |
|---|---|---|
| Goal | What should be different afterward? | Extract tax logic into `calculate_tax` |
| Scope | Which files and symbols may change? | Only `pricing.py` and `tests/test_pricing.py` |
| Constraints | What must stay the same? | Public signature and return value of `format_invoice` |
| Verification | How do we know it worked? | Run `pytest tests/test_pricing.py` before and after |
| Process | How should the agent proceed? | Describe a plan, wait for approval |
Here is a task description that contains all five parts:
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.The first line is the goal. The constraints list gives the scope and the behavior that must not change. The third constraint is the verification step. The last paragraph sets the process.
State the goal as an outcome
Describe what the code should look like or do afterward, not only what to do to it. "Extract tax logic into calculate_tax" is checkable. "Improve the structure of the pricing code" is not, because two reviewers would disagree about whether it succeeded.
Name the artifacts you expect: function names, file names, module boundaries. If the agent chooses names, you will spend review time on naming instead of on behavior.
State the scope in files and symbols
List the files the agent may edit. If a change legitimately needs to reach beyond them, you want the agent to stop and ask, not to proceed. A useful sentence to add is: "If you believe another file must change, say so and wait."
Scope can also be negative. "Do not touch the legacy/ directory" is often more reliable than listing every allowed path, particularly when the allowed set is large.
State what must not change
This is the part people skip. For a refactoring, the most important constraint is that observable behavior stays the same. Spell out what counts as observable for this code:
- Public function signatures and return values.
- Exceptions raised and their messages, if callers match on them.
- Log output or metrics names, if dashboards depend on them.
- Database schema, file formats, and API payloads.
- Ordering and rounding, which are easy to alter by accident.
You do not need to list everything. List what a reasonable engineer might change without realizing it matters.
Defining done
"Done" means a set of conditions you can check, not a feeling that the agent finished. Write the conditions into the task so the agent can check them too and report the results.
A good definition of done is a list of commands and expected outcomes:
pytest tests/test_pricing.pypasses, and it passed before the change.git diff --statshows changes only in the two allowed files.calculate_taxexists andformat_invoicecalls it.- The linter reports no new warnings.
Each item is either true or false. None depends on judgment about quality.
Ask for results, not claims
An agent can write "all tests pass" without having run them, or having run only some of them. Ask it to run the commands and paste the output, or the relevant summary lines. In the example above, "report both results" asks for the before and after runs. The before run matters: if tests already fail before you start, a failing run afterward tells you nothing about the agent's change.
Check the baseline first
Before assigning the task, run the verification commands yourself on the unchanged code. Three outcomes are possible:
- Everything passes. You have a clean baseline. Any later failure is caused by the change.
- Some tests fail. Record which ones. Tell the agent they fail already and must not be "fixed" as part of this task.
- There are no tests covering the code. The definition of done has nothing to stand on. Add characterization tests first, which a later chapter covers.
The third case is the most dangerous. If nothing checks behavior, then "done" can only mean "the agent says so."
Refactoring and migration have different definitions of done
The two kinds of change need different scoping, and the difference shows up in the definition of done.
| Row | Refactoring | Migration |
|---|---|---|
| Behavior | Must not change | May change in defined ways, such as a new API |
| Done when | Existing tests pass unchanged | Old usage is gone and new usage works |
| Test edits | Suspicious, since they may hide drift | Expected where the API changed |
| Typical scope | One function, class, or module | Many files, possibly the whole repository |
For a refactoring, a diff that edits existing test assertions is a warning sign. If the agent changed what a test expects, it may have changed the behavior and adjusted the test to match. State this in the task: "Do not modify existing assertions."
For a migration, say precisely what changes. "Upgrade to library v3" is a goal, but it needs a boundary: which calls to the old API are in scope, what replaces them, and what the end state looks like. A useful end state is searchable, such as "no remaining imports of oldlib," because a search can confirm it.
Size the task to the review
The right size for one task is whatever you can review carefully. A diff of twenty lines in one file can be read in full. A diff across sixty files tends to be skimmed.
Signs a task is too large:
- The goal contains the word "and" more than once.
- You cannot list the files in scope without searching the repository.
- The definition of done has no single command that covers it.
- You would not know which part to revert if one piece went wrong.
When a task is too large, split it. Each piece should have its own goal and definition of done, and each should end in a state where the tests pass. Later chapters cover committing each piece separately and tracking a large migration across many pieces.
Common scoping mistakes
- Mixing refactoring with fixes. If the agent notices a bug while refactoring and fixes it, the diff now changes behavior and structure together, and you cannot tell which change broke a test. Tell the agent to report bugs it sees, not fix them.
- Mixing refactoring with formatting. Auto-formatting a whole file turns a ten-line change into a three-hundred-line diff. Run the formatter in a separate step, or tell the agent not to reformat.
- Leaving the scope implicit. "Update the callers" invites the agent to search and decide what counts as a caller. List them or give the search that finds them.
- Defining done as "it looks good." This gives the agent no target and gives you no way to disagree with a result.
- Skipping the plan request. For anything beyond a trivial change, asking for a plan first lets you correct the scope before any file changes. The next chapters cover this.
Putting it together
The flow below shows how scoping leads into the work. If a check fails, you return to the task description, not just to the code.
Exercise
You ask an agent to "refactor the report generator to be cleaner." It returns a diff touching eleven files, including two test files where expected values were changed. Name three problems with the task description that made this outcome likely, and rewrite the request so it fixes them.
Show answer
The description has no scope (no files listed), no behavior constraint (nothing says outputs must stay the same), and no definition of done (no command or check). A better request: "Extract the date-formatting code from generate_report in reports.py into a function format_report_date. Do not change the output of generate_report. Touch only reports.py and tests/test_reports.py, and do not modify existing assertions. Run pytest tests/test_reports.py before and after and report both results. If another file needs to change, stop and ask. Describe your plan first and wait for approval." The changed test values should also be treated as a sign that behavior drifted.
Exercise
Before assigning a refactoring, you run pytest tests/test_pricing.py and see 2 failures out of 14 tests. What should you do, and what should the task description say about them?
Show answer
Record which two tests fail and confirm they fail without any change. Then tell the agent in the task that these two tests already fail, that it must not fix or edit them as part of this task, and that done means the other 12 still pass and the same 2 fail for the same reasons. Without this, you cannot tell whether a failure after the change was caused by the agent.