Sub topic 1
How a Coding Agent Works on Existing Code
Prerequisites
Assume you can read code, use Git branches and commits, run tests, and use a command-line linter. You should also know the basic workflow of an AI coding agent.
The Agent Loop
A coding agent works through a loop:
- Read the repository and relevant files.
- Form a plan from your request and the project context.
- Edit files.
- Run tests or other checks.
- Observe the results and revise the change.
The agent does not know your project’s intent automatically. It may infer patterns from nearby code, but you must verify those inferences.
Refactoring Versus Migration
A refactoring changes the internal structure while preserving the program’s observable behavior. For example, extracting tax logic into a function should keep the same inputs and outputs.
A migration moves the code or its dependencies to a different system, version, or architecture. A framework upgrade may require changed APIs, configuration, and behavior.
| Change | Main goal | Typical risk |
|---|---|---|
| Refactoring | Improve structure without changing behavior | Accidental behavior drift |
| Migration | Adopt a new platform or interface | Compatibility and rollout failures |
State which kind of work you want. A refactoring needs strong behavior checks. A migration also needs compatibility planning and a staged rollout.
Giving the Agent Context
Give the agent the files, conventions, constraints, and checks that matter. Mention related tests and APIs it must not change. Do not assume that “clean up this module” defines a safe boundary.
Refactor pricing.py: extract tax calculation into calculate_tax.
Keep format_invoice's public signature and return value.
Touch only pricing.py and tests/test_pricing.py.
Run pytest tests/test_pricing.py before and after editing.
First give me a plan. Wait before editing.Scoping the Task and Defining Done
A scoped task names the goal, allowed files, constraints, and definition of done. For existing code, record current behavior with characterization tests before changing it. Then ask for one small, reversible step at a time.
Define done with observable checks: tests pass, type checks pass, lint passes, the diff contains only intended edits, and the public behavior remains compatible.
Exercise
Is upgrading a library always a refactoring?
Show answer
No. An upgrade is a migration when it changes dependencies or APIs. It may include refactoring to adapt the code.