How TokenCap Picks Files for a Task: Rare Words, Four Fields, Exact Identifiers

2026-12-02来源 dev.toEnglish

A TokenCap task pack starts with a ranking. tokencap ask "<task>" scores every file in the repository's graph, and only the top of that list competes for the token budget. If the ranking is wrong, the packer can only fill the budget with the wrong files.

That ranking was rewritten. The code is in src/retrieve/rank.js and src/context/retrieveFiles.js. This post covers how it works now and what it still cannot do.

What the old ranking did

The old score was a sum of bonuses. A query keyword anywhere in the path added 5. A matching cluster added 3. Risk added up to 2, a recent change 1.5, high impact 1. A BM25 score over the path and the file's identifiers was added on top, with every query word counted the same.

Two things followed from that. Path matching was a substring test, so the keyword "is" matched registry. And a file with no connection to the task could still rank, because risk and recent-change bonuses did not depend on the task.

I checked both rankings on a five-file toy repository: a shortcodes registry, its renderer, a database connector, a parser, and an auth session file marked as changed, high-impact and CRITICAL. For the task "allShortcodes includes drafts", the old ranking returned one file:

4.5 src/auth/session.js recently-changed, high-impact, risk:CRITICAL 

The rewritten ranking returned the files the task is about (scores from when it was written):

10 src/shortcodes/registry.js matches:allshortcodes+shortcod+draft, direct-dependency 8.4 src/shortcodes/render.js matches:allshortcodes+shortcod, direct-dependency 1.3 src/util/parse.js direct-dependency 

A toy proves nothing about real repositories. It does show the two failure modes clearly.

Rare words count more

Each query term gets an inverse document frequency weight:

idf(term) = log(1 + (files - matching + 0.5) / (matching + 0.5)) 

A word that appears in most files of the repository adds little. A word that appears in three adds a lot. The final score is also scaled by how much of the task's rare-word weight a file covers, from 0.4 for almost none to 1.0 for all of it. A file that matches one of three rare words is scored down.

Four fields per file

Each term is checked against four fields:

Field Weight
File name 3.0
Declared symbols (functions, classes, consts, defs) 1.5
Directory 1.0
Full text, BM25-saturated 1.0

A term that appears 40 times in a file does not count 40 times more than a term that appears once; BM25 saturation flattens repeat counts.

Stemming, variants and a few synonyms

A small suffix stemmer runs on both the query and the files:

cookie, cookies -> cooki validate, validating -> validat 

Words where one extends the other, like "config" and "configuration", match at 0.6 weight. Five fixed synonym groups match at 0.5: auth/login/session/token, user/account/profile, payment/billing/checkout, database/db/storage, and test/spec/e2e.

Identifiers, paths and stack frames are matched exactly

When a task names an identifier, that is the most precise signal it carries. TokenCap pulls out every token with two or more parts and builds the spellings it may take in source:

allShortcodes -> allshortcodes, all_shortcodes, all-shortcodes DATABASE_URL -> databaseurl, database_url, database-url 

Each file whose text or path contains one of those spellings gets an extra 4.0 times that identifier's rarity. Plain words are not treated as identifiers.

A file path in the task is looked up directly and outweighs every other field. A stack frame is looked up the same way, and its line is shown first. A name declared in only a few files points straight at those files.

Signals that only adjust

Risk, impact and recent change are still useful for ordering files that are already relevant. They are multipliers that apply only when a file's task score is above zero:

recently changed x1.15 cluster match x1.10 memory match x1.05 high impact x1.05 HIGH/CRITICAL risk x1.05 test files x0.60 (unless the task is about tests) generated, lockfiles x0.30 

How files without a matching word get in

Three routes are tied to the task's best matches. Graph proximity: the 25 best matches pass 15% of their score to the files they import and the files that import them, and a neighbour never outranks the match it came from. Co-change: files that git history shows changing together with the best matches can join. Commit history: files of past commits whose messages match the task can join too.

One retrieval everywhere, with reasons

tokencap ask "<task>", the companion bridge and --adaptive build the same pack through one pipeline, and the MCP tool tokencap_files uses the same ranking. The CLI and your agent agree on file order.

Each file in a pack records how it was included (whole, span, outline, summary or reference), what it cost, and why it was chosen. tokencap ask prints the files shown as code and up to five left-out files with the reason: out of budget, over the file cap, or below relevance. For the full explanation:

tokencap ask "allShortcodes includes drafts" --json 

What the benchmark shows, and the limits

On benchmarks/fair/, 100 real commits from nine repositories, the task pack shows 89% of the needed files as code, holds 74% of the lines each commit edited, and uses a median 18.4x fewer tokens than a keyword grep. On the held-out tasks: 88.9%, 74.5% and 18.0x. Before this ranking, the first three repositories in that benchmark scored 25%. The benchmark calls no model, so it measures retrieval and packing, not answers.

Limits worth knowing:

  • Reading every matching file still finds more: 96%.
  • Files over 300,000 bytes are ranked by path and symbols only, and repositories with more than 6,000 graph files skip full-text matching, which also limits identifier matching to paths.
  • The stemmer strips suffixes. It does not know that "ran" is "run".
  • The synonym list is five groups, not a thesaurus.
  • Benchmark tasks are commit subjects, which are terser than what people usually type.

More: tokencap.vansharora.app

原文:https://www.a8q9.cn/en/article/how-tokencap-picks-files-for-a-task-rare-words-four-fields-exact-identifiers-86f1ee.html

相关文章