Skip to main content

How to use a coding agent in an unfamiliar codebase

SU

Suruchi Saxena

Customer Success

BU

Burhan Khalid

Customer Success

Your first ticket on a new team is to send an email when an invoice payment fails. Sounds small. The repository is three years old, runs to more than 200 files, and none of it is yours. Before you can write the handler you need to find where billing events enter the system, and work out how retries avoid sending the same customer two emails.

Your agent starts with the same gap. It’ll search faster than you can, but speed doesn’t tell it which details matter. A function marked legacy might still carry most of your production traffic, and an odd-looking test assertion might protect behavior a customer depends on. Usually nobody has written either down.

So before the agent writes any code, we have it produce evidence we can check. The agent traces the affected path and captures what the code does today in tests, then drafts what the change has to preserve. We review each step, and we hand over more of the change as the evidence holds up. Figure 1 shows how the stages feed back when something unexpected turns up.

Loading image...Diagram of three stages left to right. Learn the code: map the code path, and turn findings into tests. An arrow leads to Set the limits: write down what stays the same. An arrow leads to Make the change, for four task types: feature, bug fix, refactor, and dependency upgrade. A dashed arrow labeled check against captured behavior loops from Make the change back to Learn the code.
Figure 1. Tracing and characterization tests establish the change boundary before implementation begins. Behavior that contradicts the map sends the work back through the earlier stages.

Rewrites and automated delivery need tests first

Tracing and testing before any code can feel like a lot of ceremony for a small ticket, especially in a year when agents take on far bigger jobs. What we’re really after is automated software delivery. A software factory is one way to get there. It covers part of frontier engineering, the broader practice of building the agent setup that builds the software. The Kiro Crew team built one that merged 1,000 pull requests in a week, and its follow-up on keeping that stable is mostly about testing. The team pushed test coverage above 90 percent and spends around 20 percent of its time on infrastructure that lets it automate more.

Full rewrites are back on the table too. Dan Luu’s October case for being more ambitious with the software we build argues that many developers still carry a pre-agentic cost model. Its lead example is pgrust, a Postgres rewrite in Rust. The essay also warns that what a model can verify without help keeps falling behind what it can implement.

We agree a rewrite can be worth it now, as long as the tests exist before the agent starts. One frontier engineering principle, treat code as disposable, tells teams not to avoid a re-architecture out of sunk cost. It makes one exception, for the tests at your system’s boundaries, because they are the contract any rewrite has to satisfy.

Understanding inherited code still takes time

Code you inherited rarely comes with tests like those. A 2018 field study of 78 professional developers found they spent about 58 percent of their working time understanding programs.

Whether AI shrinks that cost is still hard to measure. In a 2025 randomized METR study, experienced developers working in repositories they already knew took 19 percent longer with the AI tools of the time, while believing they’d worked 20 percent faster. A 2026 follow-up estimated speedups of 18 percent for returning participants and 4 percent for new ones, though both intervals include no effect and METR calls its own number a lower bound while it redesigns the study. METR scopes these results tightly, and points to unfamiliar code as a setting where agents may help more.

The numbers will keep moving, but the mechanism won’t. Mature repositories carry implicit requirements and years of design decisions that live only in people’s heads. A new developer takes time to learn them. So does an agent. Until someone writes those requirements down as tests, the agent can write code that looks right and still breaks one of them. Good tests can stand in for much of that knowledge, but someone has to understand the code well enough to write them.

Trace the path that matters

Back on the payment-failure ticket, the first problem is finding the code it touches among more than 200 files you’ve never read. Ask an agent to explain the whole repository and you’ll usually get an accurate package tour and a paraphrased README, and neither answers the question behind your ticket.

We start one level above the ticket instead. Ask which packages own billing events and customer notifications, and where each one exposes its public entry points. Require a file citation for every claim, and rule out proposals at this stage.

Loading code example...

Citations make the summary checkable. Open any file it names and see whether the code says what the agent claims.

Then narrow to an existing feature that uses the same path. For the payment-failure ticket, trace an event that already sends an email.

Loading code example...

That last request checks whether the agent followed the real production path or pattern-matched its way to a plausible story. Pick a case you already know the answer to. If the explanation comes back wrong, fix the code-path map before you trust anything the agent tells you about code you can’t check yourself.

Once the map survives that check, ask how invoice.payment_failed differs from invoice.paid. The answer should separate confirmed behavior from inference and show where the path crosses into queues, databases, caches, or external services.

Turn the trace into tests

The trace records what the agent believes. Characterization tests check those beliefs against the code, capturing what the system does today, including the parts that look wrong.

Loading code example...

Review the differences first. Each one is either a mistake in the map or an undocumented rule in the implementation. When the surprise turns out to be real, you have a question for the code owner. Is this a bug, or a contract somebody depends on?

Ask the agent to separate what it observed from what it inferred. It might observe that preference checks run before template selection, while only inferring that retries deduplicate on the billing event ID. That second claim needs another test or an answer from an owner.

If the agent can’t test a path at a stable interface, look for hidden side effects or dependencies constructed inside the function under test. Code that’s hard to test at its interface is also risky to change, so hand over less autonomy there.

Now say payment-failure emails start arriving twice because the billing provider retries the event. The agent already flagged deduplication as an inference rather than an observation, so you know where to look and what to reproduce.

Loading code example...

Write the reproduction at a stable interface and the fix won’t break it. Reach into private state and you’ll usually be rewriting the test along with the code it was supposed to protect.

Define the change boundary

Characterization tests protect behavior, not scope. An agent can rename types, reshape an interface, and touch files you never mentioned while every test stays green. Those edits still carry risk. Your characterization tests cover only the path you traced, and every extra change makes the real one harder to review. Write the limits down before you ask for code.

Loading code example...

The rule about existing tests is there because an agent optimizing for a green suite will happily rewrite an assertion that encoded intended behavior. Agents also tend to do more than you asked. They’re nearly twice as likely as humans to add defensive error handling, and without proper constraints they drift further from the original intent the longer you talk to them.

For bugs, a Bugfix Spec generates this boundary for you, splitting current, expected, and unchanged behavior, then deriving tests from each. The hand-written version above is the general case for everything else.

Set autonomy according to evidence and reach

How closely we supervise an agent comes down to two things, evidence and reach.

Evidence is what you’ve built to check the agent against, meaning the trace, the characterization tests, and a failing reproduction if the ticket is a bug. When evidence is thin, add a test or ask an owner before implementation starts.

Reach is how far a wrong change travels. A private template resolver doesn’t go far. A mistake in a shared event schema or a database migration spreads to callers you didn’t edit and data that’s already stored. Adding tests won’t make that reach any smaller.

TaskEvidence to create firstReach of a wrong changeHow to run the agent
Feature following an existing patternClosest working example and relevant testsContained if the extension point holdsAutonomous after boundary approval
Bug in unfamiliar codeFailing test through a stable interfaceDepends on whether the path is sharedSupervised until the cause is confirmed
Behavior-preserving refactorTests at the module boundaryContained behind a private interfaceAutonomous in small tested steps
Dependency upgradeCall-site inventory and migration notesRepository-wideAutonomous by package, with changed defaults reviewed
Public API, migration, security, or concurrency changeTraced callers and an owner-reviewed designWide, reaching callers and stored dataSupervised throughout

By this measure, a large mechanical change with a complete inventory is often safer to delegate than a one-line edit in shared code nobody has traced.

Where this method stops helping

Some questions still need an owner. An agent can report that most modules use constructor injection and three don’t. Why those three are different is a Chesterton’s fence question, and usually only someone who knows the history can answer it. The agent will also miss ordering and side effects that unit tests never expose. Record the owner’s answer in steering, and add integration tests at the nearest stable interface when those details matter.

The changes in the last row of the table stay supervised even with good evidence. A security bug usually depends on input nobody wrote a test for, and a race depends on timing the suite doesn’t reproduce.

For these, the trace and tests make your review faster, but someone who owns the system still makes the call. Trace the callers, write the reproduction, then ask that owner to read the plan while it’s still a plan.

Keep the useful evidence

Before you open the pull request, keep the characterization tests that protect intended behavior. Drop the exploratory ones, and delete any steering you wrote only for this ticket once the work ships.

Kiro hooks can run affected tests whenever the agent saves a matching file. Use a command that already passes locally and in continuous integration.

Package the prompts and the boundary template as a Kiro skill. Skills bundle instructions with templates and load when a task matches their description. They follow the open Agent Skills standard, so the same package also works in Kiro Crew. Keep repository conventions in steering or AGENTS.md, and turn on memory for smaller discoveries, such as tests that only pass after a local service starts.

Try it on a real ticket

Pick a ticket in a module you don’t know well, as long as it has tests you can run, and scope the agent to that module. Send the trace prompt before you ask for any code. Check what comes back against one behavior you already understand, then get the important assumptions into tests.

For a ticket bigger than one session, run the skill’s steps as a Kiro workflow, which trades more tokens for a fresh session at each step. The bundled investigate recipe does a read-only pass in the background and returns a cited report for the next step to build on. Long test loops can run in a cloud session instead of on your laptop, unless they need local services or step-by-step approval.

If this works in your repository, or fails in an interesting way, tell us on the Kiro Discord.

Sources