Skip to content
mentoor

Topic 2 of 5 · 4 min read

Recovering intent from history and tickets

Goal: Use history and tickets to explain a strange piece of legacy code before deciding whether an agent may change it.

Before you let an agent change strange code, find out why it is strange. Old code often looks wrong because it fixed a bug that nobody remembers. The history usually still knows.

Check yourself · 1 of 3

A function has an odd special case for one country. What should you do before an agent removes it?

Choose one answer

Why it matters

An agent sees only the code in front of it. A line like if (country == "NZ") total += 0.01 looks like a hack, and a model asked to "clean up this function" may remove it with a polite explanation. The line may exist because a real customer was charged the wrong tax in 2019. The reason lives in the history, not the file.

How to do it

  1. Find the commit. git blame shows which commit last touched each line. Use git log -S "NZ" -- path/to/file to find the commit that added a string, and git blame -w -C to look past whitespace changes and moves.
  2. Read the message and the diff. Look at what else changed in that commit, especially tests.
  3. Follow the link. Commit messages and pull requests often carry a ticket id. Open it. The customer report, the date and the discussion are the real explanation.
  4. Check old docs and comments. They may be out of date. Treat them as leads, not facts.
  5. Let the agent help. Paste the blame output, commit messages and ticket text into the agent and ask: "What problem was this line solving? What would break if it were removed? What is still unknown?"
  6. Record the answer. Write the reason as a short comment or in the test, so the next person does not repeat the hunt.

Before and after

Weak prompt: "Simplify calcTotal and remove anything unnecessary."

Better prompt:

Here is `calcTotal`, the git blame for lines 40-52, and the three commit
messages and ticket texts they point to. For each odd branch, say what
problem it solved and what would fail if it were removed. Mark anything
you cannot explain from this evidence. Do not change any code yet.

What goes wrong

  • Squashed history. One commit called "big merge" explains nothing. Look at the pull request, or ask who was on the team.
  • Blame points at a reformat. The last commit to touch a line may only have fixed indentation. Use -w, -C, or git log -S to go further back.
  • The agent invents a reason. Given only code, a model will write a convincing history. Give it evidence and ask it to say "unknown".
  • Following a dead ticket link. If the ticket is gone, mark the code as "intent unknown" and pin its behaviour with a test before touching it.

Examples

The one-cent branch

git log -S "NZ" finds a 2019 commit titled "Fix NZ rounding, see BILL-212". The ticket shows a customer was undercharged one cent on every invoice because of a rounding rule. The branch is not a hack. The agent is told to leave it and a test is named nz_rounding_adds_one_cent_BILL_212.

Blame hits a reformat

git blame says a line came from "Run prettier on repo". Re-running with git blame -w -C -C shows the real author commit from three years earlier, which links to the ticket you need.

Key points

  • Odd-looking legacy code often fixes a real bug; find the reason before you change it.
  • Use git blame, git log -S and the linked ticket to find the commit and the problem it solved.
  • Give the agent the history as evidence, and ask it to mark what it cannot explain.
  • If the intent stays unknown, pin the behaviour with a test instead of deleting the code.
  • Write the discovered reason down as a comment or test name so the hunt is not repeated.

Common mistakes

  • Asking the agent to clean up odd code without any history.
  • Trusting a code comment over the commit and ticket it was written against.
  • Stopping at the reformat commit that blame shows first.
  • Learning the reason and not recording it anywhere.

Check yourself · 2 of 3

git blame shows a line was last changed by a commit named "Run formatter on repo". What helps you find the real origin?

Choose one answer

Check yourself · 3 of 3

You give an agent only the code of an odd function and ask why it exists. What is the main risk?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic