5 min read

Search Order Matters

Agents have four ways to find things here. Each answers a different question, and the code graph has no idea what's old unless you tell it.
Search Order Matters
Four ways to find something. Try them in order.

Four ways to find something, and which one to try first

💡
TL;DR
- Agents have four ways to find things here: the knowledge search, a code graph, grep, and live data. Each one answers a different kind of question.
- The code graph spans every repo at once, because a graph per repo can't show how the repos connect, and the connections are where things break.
- The graph has no idea what's old. Anything retired has to be kept out of it on purpose, or it gets quoted as current truth.
- The worst failure was coverage that quietly rotted, because missing coverage looks exactly like healthy coverage.

The last post was about getting agents to use a search index at all. This one is about what comes after that: once an agent has several ways to find something, which one should it reach for first?

It sounds like a small question. It decides whether an answer costs a few hundred tokens or tens of thousands, and whether the answer is right.


Four tools, four questions

Here's the order the rules file gives every session, and why.

  1. Knowledge search for intent. How does this work, what was the plan, what did we decide, what does the vendor's documentation say. Cited snippets from docs, decision records, runbooks.
  2. The code graph for structure. What calls this, what depends on it, how do these two systems connect, what breaks if I change it. Answers with a file and a line number.
  3. Grep and Read for exact text. When you already know the file, or you're about to change it.
  4. Live data tools for current state. What's actually in the system right now, as opposed to what the docs say should be there.

The mistake I kept seeing wasn't using the wrong tool. It was using one tool for all four questions. Grep will happily answer "why is this built this way" by returning every line that contains the word "why."


One graph, not seven

The code graph comes from an open-source tool called graphify. It reads the code, builds a graph of what calls and imports what, and lets you query it: explain a concept, find the path between two things, list what a symbol affects.

The obvious way to set it up is one graph per repo. I didn't, on purpose.

Our systems talk to each other. An API serves data that other apps read, and sync jobs feed it from systems we don't own. When something breaks, it tends to break at a seam between two repos. A graph per repo can't see the seams. So there's one combined graph over every repo cloned on the VM.

The code layer is built locally and costs nothing. On top of that is a "why" layer: design rationale and concepts pulled out of the docs by a language model, linked back to the code they explain. That pass costs tokens, so it ran once and is cached. Every rebuild carries it forward.

Learned the hard way: it carries it forward from the existing graph file, not from the cache. Delete the graph file to "start fresh," rebuild, and you get a perfectly healthy-looking graph with no "why" in it at all.


Keeping it fresh without thinking about it

A graph that's a week old is worse than no graph, because it's confidently wrong.

So every repo has a post-commit hook that rebuilds the graph in the background after each commit. It's detached and locked, so it never slows a commit down and two rebuilds can't fight. The expensive "why" layer gets re-merged, not recomputed.

And there's a nudge. When an agent reaches for Grep or Glob inside the repos folder, a hook adds one line to its context: the graph exists, here's how to query it. It never blocks anything. It just puts the better option in front of the agent at the moment it's about to pick the worse one. Same lesson as the search index: reachable beats good.


The graph doesn't know what's old

Here's the part that matters most, and it's not technical.

The graph has no concept of time. A design doc from two years ago, superseded twice since, is just as true to it as the one written last week. Ask it how something works, and it might answer from the retired version with full confidence.

So retired material has to be kept out on purpose:

  • An ignore file lists legacy folders and old snapshots, so they're never indexed.
  • The repos follow a rule: when a file is superseded, move it to a dated archive folder, or put a "superseded by" line at the very top if it can't move.
  • Agents are told that a result from an archive path is history, not current truth.

People do this too. Most shared drives are full of documents that were true once. The difference is that a new hire might notice the date on the file. The graph won't.


The rot nobody saw

The first version of the graph covered the seven repos I'd cloned at the time. The post-commit hook was installed on those seven.

Then more repos showed up. Some got cloned and were in the graph, but had no hook, so they only got refreshed when one of the original seven happened to have a commit. A stretch where only a newer repo saw work left the graph stale for it, with no signal anywhere. Other repos existed in the org but were never cloned at all, so they weren't in the graph, and nothing said so.

Both failures look exactly like success. The graph answers. It just answers without part of the codebase.

Now a daily job closes both gaps. It lists every active repo in the GitHub org, clones any that are missing, reinstalls the hook on every repo, and rebuilds once if anything new arrived. It skips archived repos, because the graph can't tell frozen history from current code, and it leaves alone any hook it didn't install and logs it.

Coverage heals itself now. It's the same lesson as the notebook sync: if missing data looks like healthy data, something has to check for the difference specifically.


Where I Landed

  • Every session gets the same order: knowledge search, then the graph, then grep, with live data for current state.
  • One graph covers every repo, rebuilt after every commit, with the "why" layer carried forward.
  • Retired files are kept out of the graph by an ignore list and an archive rule, because the graph can't tell old from new.
  • A daily job makes sure every repo in the org is cloned, hooked, and in the graph.
  • New docs still need a fresh "why" pass by hand. Code stays current on its own. Rationale doesn't.

There's a safety check in the tool that refuses a rebuild if the graph would shrink. It's there to catch a broken build that silently drops half the nodes.

It also fires when you delete dead code on purpose, so a real cleanup needs:

` --force `

I spent months building things that notice when something goes missing. Throwing code away is the one thing I have to insist on.

Next: the night shift.