# How do we capture 20 years of undocumented behavior in a legacy codebase before a rewrite?


URL: https://getunblocked.com/answers/capture-undocumented-legacy-codebase-behavior-before-rewrite/
Published: 2026-11-20T15:30:00Z
Updated: 2026-11-15T20:38:04Z
Author: Brandon Waselnuk
Categories: Context Engine, Engineering Insights

**Short answer:** Record what the code does before anyone touches it, then find out why. Pin current behavior with golden-master tests and mirrored production traffic. Trace each odd case to the PR, ticket or chat thread behind it. Unblocked answers those why questions across 20 years of history with citations. Interview the people who remember the rest, and write an ADR for each behavior you keep.

---
Capture it in two passes before the legacy codebase rewrite starts. First record what the system does today, odd cases included, with tests and real traffic. Then find out why each odd case exists, from the pull requests, tickets and chat threads where someone once explained it. Unblocked does the second pass: you ask "why does this function round down for Canadian accounts?" and it searches 20 years of PRs, issues, docs and Slack or Teams threads, then returns one answer that links each source. **A behavior inventory** is the list both passes produce: each behavior, the test that pins it, the reason it exists, and a decision to keep or drop it. With Unblocked, the reason column fills with cited answers instead of guesses.

Here is a made-up example of three rows:

| Behavior | Pinned by | Why it exists | Decision |
| --- | --- | --- | --- |
| Invoice totals round down on the 31st | Golden-master case 214 | 2011 ticket: a bank file format limit | Drop, bank retired the format |
| Orders over 500 lines split in two | Mirrored traffic sample | Slack thread: old warehouse system timeout | Keep, ADR-007 |
| Retry three times, then email ops | Unit test | No source found | Ask in interview |


Still deciding whether to rewrite at all? Start with our answer on [rewriting legacy code for agents or adding a context layer](https://getunblocked.com/answers/rewrite-legacy-code-for-agents-or-add-context-layer/). This page assumes the rewrite is going ahead.

## How do you pin down what the legacy code does today?

Record its outputs before you judge them. **A golden-master test** is a test that runs the old code on many inputs, saves what comes out, and fails if anything changes. The ApprovalTests project describes it as "taking a snapshot of the results, and confirming that they have not changed" ([ApprovalTests](https://approvaltests.com/)). You are not deciding whether the output is right. You are writing down what it is, so the new system has a target.

Made-up inputs only cover the cases you thought of. Twenty years of behavior hides in the cases nobody thought of, so use real traffic too. **Traffic mirroring** is copying live production requests to a second service without affecting the first. Istio's docs call the copies "fire and forget," with responses discarded ([Istio traffic mirroring](https://istio.io/latest/docs/tasks/traffic-management/mirroring/)). Point the mirror at a recorder now, and at the new system later. Batch jobs need the same treatment, such as a month of real input files saved with their outputs. Strip personal data before any of it becomes a fixture. Every surprising output becomes a row in the inventory.

## How do you find out why each odd behavior exists?

The code shows the rule, and the history shows the reason. Start with git. `git log -L` will "trace the evolution of the line range" or of a named function in a file ([git-log docs](https://git-scm.com/docs/git-log)). That lists the commits behind the strange branch, often with a ticket number in the message.

Over 20 years the trail breaks. For example, the tracker moved twice, the PR links to a dead wiki, and the real explanation sits in a chat thread. Following that by hand for a few hundred rows can take weeks.

A context engine shortens it. Unblocked's `context_research` tool runs "multi-source research across code, PRs, docs, issues, and messaging" and "returns a synthesized answer with citations" ([Unblocked MCP docs](https://docs.getunblocked.com/unblocked-mcp/mcp-overview)). You ask from Claude Code, Cursor, Codex, Windsurf, GitHub Copilot in VS Code or JetBrains, Slack or the web. With Data Shield on for a source, answers "only contain source material that the user posing the question is authorized to access" ([Data Shield docs](https://docs.getunblocked.com/team-settings/data-shield)). Paste the answer into the reason column and open the links to check it. Our post on [why was this built this way](https://getunblocked.com/blog/why-was-this-built-this-way/) covers this history in depth.

## What do you ask the people who remember?

Some reasons were never written down. They were said in a meeting or agreed on a call. For those, interview people and use the inventory as the agenda.

A specific list works better than "tell us about billing." Bring the rows whose reason column is still empty. Talk to the longest-serving engineer, the support lead, and the finance or operations person who uses the output. For each row, ask three things. Who depends on this? What breaks if it changes? Is it still needed? Write the answer and the person's name in the row.

Do this early. The people who know are often near the end of their tenure, which is part of why the rewrite is happening. When they leave, agents and new hires start [repeating approaches the team already rejected](https://getunblocked.com/blog/claude-code-institutional-memory/).

## How do you record the behaviors you keep?

Write an architecture decision record, so the next rewrite does not start from zero. Microsoft's guidance says each entry "captures the context, justifications, and implications of a decision," including options ruled out ([Microsoft Learn](https://learn.microsoft.com/en-us/azure/well-architected/architect-role/architecture-decision-record)). For older systems it says records "should be retroactively generated based on known past decisions." The inventory is that list of past decisions.

Keep each record short: one per kept behavior, or per group. Link the golden-master case, the source that explains it, and the person who confirmed it. Dropped behaviors get a record too, with who signed off. That is the first thing anyone asks when a customer notices the change.

## Where does AI help, and where does it make things up?

AI is good at the reading. A July 2026 ACL industry paper ran an AI pipeline over an internal financial-industry corpus of more than 3.4 million lines of COBOL, comments included. It reported "93% agreement with expert-authored business rules" and cut documentation effort by "approximately 70%" ([Kalohia, ACL 2026](https://aclanthology.org/2026.acl-industry.4/)). The pipeline held the model to a fixed output schema and kept each rule traceable to its code.

Read the other side too. By our arithmetic, 93% agreement leaves about 7 rules in 100 that did not match the experts, and you will not know which ones without checking. AI also cannot recover a reason nobody recorded. Ask a model why, with no PR or thread behind it, and you get a likely-sounding story. Treat any reason without a citation as a guess and send it to an interview.

Do not let the model grade its own port either. A May 2026 study ran 1,980 modernization calls across 11 production models, with a sample size of 60 snippets of legacy Python 2 code. On snippets with subtle semantic traps, behavior drifted in 39.7% of attempts, against 7.0% on plain control code. The producing model then endorsed 31.7% of those drifted versions on review ([Purnachandra Reddy et al., arXiv](https://arxiv.org/abs/2605.21537)). Anthropic's docs make the same point: "Claude stops when the work looks done" ([Claude Code best practices](https://code.claude.com/docs/en/best-practices)). The golden-master suite is the independent check. For what to generate from the code itself, see our answer on [AI documentation for a large codebase](https://getunblocked.com/answers/ai-generate-documentation-for-large-codebase/). For the migration after capture, see [refactoring legacy code with AI agents](https://getunblocked.com/blog/refactoring-legacy-code/) and the [legacy code modernization guide](https://getunblocked.com/blog/legacy-code-modernization/).

## Read more

- [Why Was This Built This Way? The Second Codebase Your AI Agents Can't Read](https://getunblocked.com/blog/why-was-this-built-this-way/)
- [AI-Powered Legacy Code Modernization: The Complete Guide (2026)](https://getunblocked.com/blog/legacy-code-modernization/)

## Related answers

- [What enterprise search works for a 50 to 300 person engineering team when Glean feels like too much?](https://getunblocked.com/answers/enterprise-search-mid-size-engineering-team/)
- [How do we cut ramp-up time for engineers joining a large existing codebase?](https://getunblocked.com/answers/cut-ramp-up-time-large-codebase/)
- [How do I give Claude Code docs for our internal SDK that isn't on the public internet?](https://getunblocked.com/answers/claude-code-docs-internal-sdk/)
- [Which AI assistants answer engineering questions in Slack with links to their sources?](https://getunblocked.com/answers/ai-assistant-engineering-questions-slack-sources/)
