A default FTS5 tokenizer keeps issueRefund as one token, so a search for refund misses it. Index original and split identifiers and test recall.
The search tool decides what a coding agent can see
A coding agent working in a repository spends much of its time searching. Before it edits a refund handler it asks a tool for "refund", reads what comes back, and plans from there. If the tool misses a file, the agent does not know the file exists. It will not say "I could not find the gift card policy"; it will write a change that ignores the gift card policy, and the change will look complete.
Many agent harnesses back that tool with a lexical full-text index, because it is cheap, local and predictable. Lexical indexes were designed for prose. Source code names things differently: issueRefund, GiftCardRefundPolicy, open_return_case, HTTPClient, v2RefundTotal. Whether a query for "refund" finds those depends entirely on how the index splits text into tokens, and the default tokenizers of popular engines were not built with identifiers in mind.
This guide measures the problem on a small synthetic repository from Belgavi's Book Store's help desk service, shows a tokenization that fixes it, and explains how to test your own agent's search tool.
How the default tokenizer sees code
SQLite's FTS5 uses the unicode61 tokenizer by default. Its documentation describes it as treating whitespace and punctuation characters, as defined by Unicode 6.1, as separators, and folding case. That means an underscore separates tokens, so open_return_case becomes three tokens, but a camelCase name is a single token: issueRefund is indexed as issuerefund, and GiftCardRefundPolicy as giftcardrefundpolicy. Nothing in that token starts or ends at "refund".
Here is the repository, four snippets long:
| File | Content |
|---|---|
| refunds.py | def issueRefund(order_id, amount): return ledger.post(order_id, -amount) |
| returns.py | def open_return_case(case_id): queue.put(case_id) |
| gift.py | class GiftCardRefundPolicy: allow_cash = False |
| notes.md | Refund rules for returns are described in the help desk guide. |
The probe below ran on SQLite 3.49.1 with four indexing variants. Each query is a phrase query.
| Query | A: unicode61 on raw text | B: underscore kept inside tokens | C: identifiers split | D: raw plus split, query both forms |
|---|---|---|---|---|
| refund | notes.md | notes.md | gift.py, notes.md, refunds.py | all three |
| issueRefund | refunds.py | refunds.py | refunds.py | refunds.py |
| issuerefund | refunds.py | refunds.py | none | refunds.py |
| open_return_case | returns.py | returns.py | returns.py | returns.py |
| case | returns.py | none | returns.py | returns.py |
| gift card | none | none | gift.py | gift.py |
Variant A, the default, finds only the prose note for "refund". The two code files that implement refunds are invisible to the most natural query an agent would issue. Variant B, setting tokenchars '_' so that snake_case names stay whole, is a common suggestion and makes things worse: "case" no longer matches open_return_case. Variant C splits identifiers before indexing and finds everything a person would expect, but loses the exact lowercase form issuerefund, which agents do produce when they copy a name from a stack trace or a log line that folded case. Variant D indexes both forms and queries both, and is the only one with no misses on this set.
The fixture is tiny on purpose, so you can check each row by hand. The point is not a recall percentage; it is that the default misses the files that matter for the most ordinary query.
Splitting identifiers before indexing
The split is a few regular expressions applied to the text before it reaches the index, and to the query before it reaches the engine.
import re, sqlite3
def split_identifiers(text):
text = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", text) # issueRefund -> issue Refund
text = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", text) # HTTPClient -> HTTP Client
return text.replace("_", " ") # open_return_case -> open return case
def phrase(s):
return '"' + s.replace('"', '""') + '"'
DOCS = {
"refunds.py": "def issueRefund(order_id, amount): return ledger.post(order_id, -amount)",
"gift.py": "class GiftCardRefundPolicy: allow_cash = False",
"notes.md": "Refund rules for returns are described in the help desk guide.",
}
db = sqlite3.connect(":memory:")
db.execute("CREATE VIRTUAL TABLE code USING fts5(path, body)")
for path, body in DOCS.items():
db.execute("INSERT INTO code VALUES (?, ?)", (path, body + "\n" + split_identifiers(body)))
def search(q):
match = phrase(q) + " OR " + phrase(split_identifiers(q))
return sorted(r[0] for r in db.execute("SELECT path FROM code WHERE code MATCH ?", (match,)))
print(search("refund"), search("issuerefund"), search("gift card"))
# ['gift.py', 'notes.md', 'refunds.py'] ['refunds.py'] ['gift.py']Indexing both forms roughly doubles the indexed text for code files. For a repository index that is usually an acceptable price; measure it on your own corpus before assuming otherwise.
Elasticsearch and OpenSearch provide the same capability as a built-in token filter, word_delimiter_graph, with options such as split_on_case_change, split_on_numerics and preserve_original. The last one is the equivalent of variant D: it keeps the original token alongside the parts.
The choices that change results
Splitting is not one decision but several, and each changes what an agent can find. The companion lab lets you vary them on an eight-snippet corpus.
- Acronyms. A rule that only splits at a lowercase-to-uppercase boundary leaves
HTTPClientas one token, so neither "http" nor "client" finds it. A second rule that splits an uppercase run before a capitalised word turns it intoHTTP Client. The lab shows the difference on a query for "http client". - Digits. The case-change rule already turns
v2RefundTotalintov2 Refund Total. An extra digit rule producesv 2 Refund Total, which makes the bare letterva token in every versioned name. Whichever you choose, the query must go through the same rule, or a search for "v2" stops matching. - Minimum token length. Dropping one- and two-letter tokens cuts noise from loop variables but also drops
idandio, which are real words in code. - Match rule. A phrase query for "gift card" requires the words to be adjacent; an all-words query does not. Phrase matching on split identifiers is a good default because it respects the order inside a name.
Make the tool honest about misses
Even with a good index, an agent should know when search might be incomplete. Three cheap measures help:
- Return the number of matches and the query that actually ran, including the split form, so the agent can see what was searched.
- When a query returns nothing, say so explicitly and suggest the split and joined variants, rather than returning an empty list that the model may read as "no such code exists".
- Offer an exact-substring tool alongside the index, such as a grep over the working tree. Lexical indexes and substring search fail in different ways, and an agent with both can recover from either.
Verification: a recall test for the search tool
Treat the search tool as a component with its own tests. Keep a small list of query and expected-file pairs drawn from real tasks, and run it whenever the tokenizer, the index schema or the engine version changes.
| Query | Must return |
|---|---|
| refund | refunds.py, gift.py |
| gift card | gift.py |
| issuerefund | refunds.py |
| case | returns.py |
Measure recall on that list, not on generic benchmarks. A tokenizer change that improves prose search can still break the four queries above, and the agent's behaviour will degrade without any error.
Tradeoffs and failure modes
- Index size. Dual indexing stores more tokens. On code-heavy corpora that is the main cost.
- Precision. Splitting creates common short tokens such as
get,setandid. Ranking with BM25 handles most of this, but generic queries return more results. - Language-specific names. Rules tuned for camelCase and snake_case do not cover every language. Kebab-case, Lisp-style names and names with non-Latin letters need their own checks.
- Query-side drift. If the index splits identifiers and the query path does not, phrase queries for camelCase names fail. Apply the same function on both sides, and test it.
When to reach for something else
A lexical index with good tokenization answers "where is this name used" and "which files talk about refunds" well. It does not find code by behaviour: a query for "money goes back to the customer" will not find issueRefund unless those words appear nearby. Embedding search can help there, at the cost of less predictable results and harder debugging. For symbol navigation, a language server or a code intelligence index that understands definitions and references is more precise than either. Many agent harnesses end up with all three. Fixing tokenization is the cheapest of the three and usually the first to do, because without it the simplest query an agent can write silently misses the code it was asked to change.
Interactive experiment
Code search tokenization: camelCase, snake_case and dual indexing. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.