Skip to content

A long code-search query keeps its informative terms - #1664

Closed
zlareb1 wants to merge 8 commits into
mainfrom
fix/code-search-long-query-terms
Closed

zlareb1 wants to merge 8 commits into
mainfrom
fix/code-search-long-query-terms

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

code.search on a long query, such as a pasted issue or a whole prompt, now searches its lexical leg with the query's identifiers and the words it repeats, whatever the query opens with. It no longer keeps the rarest words of the body, the pieces of links, hashes and UUIDs, or the English function words of its prose.

Why

The lexical leg scores a chunk by the idf of the query lexemes it holds, over at most 32 lexemes. A query past code_search_query_max_tokens (48) kept its identifiers in reading order and then its rarest words. On 29 public issues (24 of them past the threshold) that choice is the wrong way round:

Lexeme of the issue Count In the chunk of the definition to change Named half Seeded half
Word said once, in under 1 chunk per 1,000 175 1% 1% 1%
Word said once, in 1 to 10 chunks per 1,000 389 17% 15% 20%
Word said once, in 10 to 125 chunks per 1,000 360 36% 34% 39%
Word said twice or more, in under 1 chunk per 1,000 49 14% 12% 16%
Word said twice or more, in 1 to 10 chunks per 1,000 148 33% 38% 28%
Word said twice or more, in 10 to 125 chunks per 1,000 324 50% 46% 56%
Identifier, said once 130 19% 14% 26%
Identifier, said twice or more 94 35% 32% 41%
Bare number 70 1% 0% 5%
Lexeme the index does not contain 134 0% 0% 0%
Word in more than 125 chunks per 1,000, said once 57 61% 53% 74%
Word in more than 125 chunks per 1,000, said twice or more 99 85% 85% 85%
Function word outside the first 77, in at most 125 chunks per 1,000 101 20% 25% 12%

The word rows count every word outside the 77 function words of the earlier revisions, on the 24 issues past the threshold. A word in more than one chunk in eight is in the gold chunk because it is in most chunks; it is ranked last for what it costs at the gate (under Tests), not for being off subject.

The same words by where they stand:

Word (in at most 1 chunk in 8, not a function word) Count In the chunk of the definition to change
Said once, in the first 16 lexemes 122 23%
Said once, after them 802 21%
Said twice or more, in the first 16 lexemes 122 57%
Said twice or more, after them 399 37%
  • Rarest picks the worst class first. A word that is rare in code is rare because it is not code vocabulary (culprit, duck, a reporter's name).
  • Each off-subject lexeme costs rank. The score is a sum over presence with no length term, so a lexeme the gold chunk lacks only adds other chunks to the 200-row pool.
  • Repetition marks the subject. At every frequency the gold chunk holds a repeated word more often than a word said once (14% against 1%, 33% against 17%, 50% against 36%), and a repeated identifier more often than one said once (35% against 19%).
  • Position alone does not. A word said once is no more likely to be on subject in the first 16 lexemes than after them. A repeated word is, and a rule on repetition keeps it wherever it stands.
  • Hash pieces passed the noise filter. The filter ran on tokens after the tokenizer split a hash at its letter-digit edges, so eed, fd and dae read as rare words.
  • Identifiers were cut by position. In a traceback-heavy issue the first 32 identifiers took every slot, and the innermost frame, which names the function, came later.
  • Function words are repeated by grammar, not by subject. The function words outside the first 77 (also, only, all, doesn) occur 101 times in these issues as a word rare enough to keep. The gold chunk holds them 20% of the time, the rate of a word said once (21%), and 8 of the 14 that held a slot on the third revision held it because the issue repeats them.
  • A function word written as code is a name. only(), .all(), returns None and the WHERE block name code. On the long issues 10 function words that at most one chunk in eight holds are written that way, and the gold chunk holds 9 of them; it holds 40% of the 391 written only as prose (43% of 205 on the named half, 35% of 186 on the seeded half). Nine of the ten are on the named half and one on the seeded half, so this rule rests on how code is written and on the unit tests, not on these counts.

The rule now, for a query past the threshold:

  • Removed before tokenizing: URLs, UUIDs, and hashes: a word of seven or more hex characters with two or more digits in which letters and digits meet at two or more places. A short code (F401, e164), a word and a number (ed25519) and a word around one digit (face2face) are terms.
  • Kept: the whole forms of its identifiers, and for half the list its words: those it says more than once first, then the others, each in reading order. A word of four or more hex characters in which letters and digits meet at two or more places (7c05, ff00aa: a group of an id, a colour) is a word, not an identifier. A word and a number (F401, c8) is an identifier, as before.
  • A file path counts for its file: the directories of a path (/usr/lib/python3.11/site-packages/django/forms/formsets.py, D:\app\models\query.py, db/models/query.py:12) are not counted as said, so the frames of a traceback do not make them words the query repeats; a lexeme only they hold is a word said once, and the file's own name is counted as written. Names joined by / or \ are a path when they start at a root or end in a file name with an extension: request/response, and/or and 1/8 are counted as words. A full stop after the last name ends the sentence and is not part of the name, so in db/models/query.py. is a path.
  • A function word counts where it is written as code: called (only(), read off an object (qs.only), alone in backticks, or capitalised inside a sentence (returns None, the WHERE block). Its uses in prose and inside an identifier (order_by) are not counted. The list is the closed classes by class (determiners, pronouns, auxiliaries and modals, prepositions, conjunctions, question and place words, negation and the focus particles) and the halves the tokenizer leaves of contractions (doesn, ve, ll): 200 lexemes, written from the grammar before any run.
  • Past 32: the words it repeats, then the identifiers' whole forms, the rarest first, then the words it says once.
  • Only when a slot is left, in this order: a word that more than one chunk in eight holds, a function word the query never writes as code, a bare number, and a lexeme the index does not contain.
  • extra_terms: a caller's terms are not words of the query, so they are neither stripped nor filtered nor counted as said. They take the slots the query leaves, the whole forms of their identifiers before the lexemes only they hold, the rarest first. The query's words are chosen from the query alone, so the terms of the query that the index contains are the same with and without them. The concept leg passes its term list this way, so a long list is cut to its rarest tokens, as before this change.
  • Only addresses and hashes: they stay as the terms, so the leg still runs.

Designs measured and dropped:

  • The opening as the rule (the first revision of this pull request: the first 16 lexemes and the identifiers). It assumes the query opens with its subject. With one greeting sentence before each issue it found the gold file in the top 10 for 12 of 29 on the hybrid request, against 13 before the change and 18 with the rule above; with a three-heading issue form before each issue, 6 against 7 and 10. The full comparison is under Tests.
  • Letting the words take the slots the identifiers leave free. In the lexical leg's own 200-row pool the gold file was in the top 10 for 6 of 24 issues as written, against 12 with the words held to half the list, and for 7 of 28 against 12 after a greeting. Both halves of the set lose.
  • A caller's terms ahead of everything (the second revision). Six proposed names took the identifier slots: with six names unrelated to the issue the gold file was in the leg's top 10 for 1 of 29 issues, against 8 before the change and 9 now.
  • A caller's terms kept only as whole names, their parts treated as words of the query. With six names from the layers most of the issues are about, the gold file was in the leg's top 10 for 10 of 27, against 13 with the names kept whole and in parts; with six unrelated names, 13 of 29 against 9 (measured on the third revision). The parts are what match when a proposed name is close to the code's name and not equal to it, and under the threshold a caller's term is already asked for whole and in parts, so the rule above keeps one meaning for a caller's term at every length.
  • Removing a common word from the list (the second revision). In a namespace of fewer than 8 chunks every word is in more than one chunk in eight, the list was empty and the lexical leg returned nothing. A common word now ranks last among the words.
  • A caller's term taking a word out of the query's words (the third revision). A lexeme of a proposed name that the query also says (query, set, sql, a proposed Paginator) left the query's words and ranked with the pieces of the names, rarest first, and the counts of what the query says included the names. With the six related names 7 of the 24 long issues lost a word of their own list (5 of 11 named in advance, 2 of 13 seeded), and for 6 of them the gold chunk holds a lost word; with the unrelated names 13 lost error, a piece of one name. None loses a word now (table under Tests).
  • A hash as any seven hex characters with two digits (the third revision). It removed ed25519 from a question about a key type. On these issues, in any of the four forms under Tests, the two rules remove the same words.
  • A function word never a word to keep (the fourth revision). A query about only(), or one that says a call returns None, lost that word however often it said it, and a long query of function words alone was asked with no term, so its lexical leg returned nothing. Against the third revision's 77 words it moved 0 cells up and 4 down on the seeded half as written. A function word now counts where it is written as code and fills a slot otherwise (table under Tests).
  • A caller's identifiers beside the query's (the third and fourth revisions). The proposed names shared the identifiers' slots, the rarest first, so with six names 3 and 4 of the issues were asked with fewer of their own indexed identifiers than fit in 16 slots. The query's terms now go first and none loses one. The cost is on the proposed names: a query that fills the list alone is asked without them, and 6 and 7 issues are asked with fewer of them than the index contains, against 5 and 5 on the fourth revision and 3 and 3 before the change (table under Tests).
  • A word said once ahead of the identifiers past 32 (through the fourth revision). With 16 kept words and more than 16 identifiers, the identifiers were cut to the rarest 16 while every word said once kept its slot.
  • The directories of a path as words (through the fifth revision). Each frame of a traceback repeats them, so on a seven-frame traceback usr, lib, python, site, packages and two package directories took 7 of the 16 word slots and the lexical-only request lost the file the trace ends in from the top 10 (control under Tests).
  • A group of an id as an identifier (through the fourth revision). 7c05 and e89b of a UUID split at their letter-digit edges like an identifier and were always kept. On these issues, as written, after a greeting, after a form and as one line, removing UUIDs and reading such a group as a word changes no list; one control under Tests has three UUIDs.

Scope and risk

  • Touches: server/code/domain/index/query_trim.py (the plan and the selection), the one call in the lexical leg and the one in the concept leg. select_rarest is replaced by select_terms.
  • Leaves alone: queries at or under the threshold (same statements, same embedding text), the vector leg's text, the SQL, the index, the scorer, the test-intent rule and the symbol route.
  • Known cost: a subject the query states once, as a plain word, is asked for only while the words' half of the list has room, the earlier first. A subject word that more than one chunk in eight holds is asked for only when fewer than 16 other words are left. A prose query that repeats nothing is therefore asked with its first 16 content words, and a preamble of that many takes the list (one control under Tests). A function word that is also a name in code is asked for as a word only where the query writes it as code: an issue that says aggregate over window functions twice loses over, and with it one request (issue G under Tests). A capitalised function word after a heading that lost its line break, or in a title in title case, reads as code and takes a word slot: one issue's one-line request gains when after its Description heading, and no cell moves. Repetition marks a format as well as a subject: each frame of a Python traceback repeats File and line, and they take two word slots. On a seven-frame traceback written for it the lexical-only request returns the file the trace ends in second, behind the function of the trace's fourth frame, where the old selection returns it first; the hybrid request returns it first on both builds (control under Tests). A route written like a path (/admin/login/) is read as one, so its words are not counted as repeated. A query that fills the list alone is asked without a caller's terms. A hash spelled as one run of letters and one of digits reads as a name and stays a term: about one seven-character hash in eight, one twelve-character hash in a hundred; a short id of that shape (ff0000, a456) is kept as an identifier, as a code (F401) is. Eight of the 29 issues are worse on one or two requests each (listed under Tests), 6 of the 11 cells on the request that excludes tests. On the seeded half two top-10 file counts are one below the old selection: the lexical-only request (9 against 11) and the request that excludes tests (12 against 13).
  • The two shares are round values, not swept ones: words take half the list, and a word in more than one chunk in eight ranks last. Their neighbours are under Tests. A quarter of the list for words does as well as a half or better at the top 10 on both halves of the set; it was not adopted because nothing but these 29 issues supports it, and a query with no identifier would then be asked with 8 words.
  • Gate width: the list's median share of a namespace's chunks that pass the text-index gate rises from 33% to 42% (p90 69% to 65%, maximum 99% to 82%); the statement reads the same rows and takes less time and memory (under Tests). Most of the width comes from the parts of snake-case identifiers, which the gate splits on both builds. A long query with fewer than 16 other words is asked with its common words and then its function words: no request of the 29 issues in any of the three forms is one, and on one written for it the share is 90%, against 8% on the old selection, and the statement takes 69 ms against 111 ms (under Tests).
  • Not measured: a real embedding model, a language other than Python, a query in a language other than English, a namespace larger than 77,000 chunks or smaller than 1,769 (the small-namespace rule is covered by unit tests only), terms proposed by a real model (the two lists under Tests were written by hand before the run), terms a caller adds through the harness's requests (the tables with a caller's terms replay the leg's statement), and the cost of this revision's lists (the timings under Tests are from the second revision).

Compatibility, rollout and rollback

  • Wire: no op or field changes. retrieval_work.query_trimmed_tokens_used and the query_trimmed(...) part of score_explanation can now report fewer than 32.
  • Operator steps: none. No setting, default, migration or re-index. The description of code_search_query_max_tokens changed, and its three generated files with it.
  • Rollback: a revert restores the old selection and leaves nothing behind.

Tests

Measurement: 29 public issues of two open-source Python web frameworks, each with the commit that fixed it. 14 were named before any measurement and 15 were drawn with a fixed seed. Each base commit is indexed whole into its own namespace (64,674 to 76,607 chunks, and one of 1,769), tests and docs included. A request is POST /v1/code.search with the issue text, top_k: 10, fresh: true and legs as named. "File" is the rank of the first changed file among distinct files; "def" is the rank of the first unit that covers a changed line. Cells are issues hit, before to after. The harness and its index are not in the repository; three runs of each build return identical answers.

The vector leg ran on a deterministic n-gram stand-in, not a provider model, so the hybrid rows are the lexical leg plus vector noise. The lexical-only rows do not depend on it.

The issues as written (each opens with its title):

Request (n=29) file@1 file@3 file@5 file@10 def@1 def@3 def@5 def@10
Issue, hybrid 9 to 10 11 to 14 13 to 15 13 to 17 4 to 6 6 to 9 8 to 9 8 to 12
Issue, lexical only 9 to 11 9 to 14 12 to 15 15 to 15 5 to 8 5 to 10 6 to 11 9 to 12
Issue as one line, first 2,000 characters, top 3 (the prompt hook's request) 9 to 10 10 to 13 n/a n/a 5 to 7 7 to 10 n/a n/a
Issue, hybrid, tests and docs excluded by glob 10 to 13 16 to 20 17 to 21 21 to 22 6 to 5 8 to 11 9 to 12 11 to 13
code.context_pack, first 500 characters 4 to 7 9 to 14 12 to 16 12 to 16 4 to 5 6 to 7 6 to 7 6 to 7
First line only, hybrid (under the threshold) 13 19 23 25 4 9 10 11
First line only, lexical (under the threshold) 11 18 21 24 8 11 11 14
Issue, vector only 6 8 8 9 2 3 3 3

The last three rows return identical answers before and after, as do the five issues under the threshold.

One greeting sentence of 19 lexemes before each issue:

Request (n=29) file@1 file@3 file@5 file@10 def@1 def@3 def@5 def@10
Issue, hybrid 6 to 10 11 to 15 12 to 17 13 to 18 3 to 7 5 to 10 6 to 10 6 to 13
Issue, lexical only 7 to 13 10 to 17 11 to 17 11 to 17 5 to 9 5 to 11 6 to 11 6 to 13
Issue as one line, top 3 6 to 10 10 to 15 n/a n/a 4 to 8 6 to 11 n/a n/a
Issue, hybrid, tests and docs excluded by glob 9 to 14 16 to 20 17 to 22 18 to 23 5 to 7 10 to 12 10 to 13 10 to 15
code.context_pack, first 500 characters 6 to 8 8 to 13 9 to 16 10 to 16 1 to 4 3 to 8 4 to 10 4 to 10

An issue form before each issue (three headings outside the fixed heading list, a checkbox line and an environment line):

Request (n=29) file@1 file@3 file@5 file@10 def@1 def@3 def@5 def@10
Issue, hybrid 2 to 1 4 to 2 7 to 6 7 to 10 0 to 0 1 to 2 4 to 5 5 to 8
Issue, lexical only 5 to 9 5 to 10 5 to 11 7 to 12 3 to 7 3 to 7 3 to 8 5 to 9
Issue as one line, top 3 1 to 1 2 to 2 n/a n/a 0 to 0 1 to 2 n/a n/a
Issue, hybrid, tests and docs excluded by glob 9 to 13 13 to 19 14 to 21 16 to 21 5 to 6 9 to 11 9 to 13 10 to 14
code.context_pack, first 500 characters 1 to 2 1 to 3 1 to 3 1 to 3 0 to 1 0 to 2 0 to 2 0 to 2

Under the form the stand-in vector leg finds the gold file for 2 of 29, and the top ranks of the hybrid rows follow it.

Each half of the set, before to after:

Half of the set Query Issue hybrid file@10 def@10 Issue lexical file@10 def@10 One-line top 3 file@3 def@3
Named in advance (14) As written 6 to 7 3 to 4 4 to 6 2 to 4 4 to 5 3 to 4
Seeded draw (15) As written 7 to 10 5 to 8 11 to 9 7 to 8 6 to 8 4 to 6
Named in advance (14) After a greeting 6 to 7 3 to 4 4 to 7 2 to 4 4 to 6 3 to 4
Seeded draw (15) After a greeting 7 to 11 3 to 9 7 to 10 4 to 9 6 to 9 3 to 7
Named in advance (14) After a form 4 to 4 3 to 3 3 to 4 2 to 2 2 to 2 1 to 2
Seeded draw (15) After a form 3 to 6 2 to 5 4 to 8 3 to 7 0 to 0 0 to 0

The opening rule this pull request first carried, as before / opening rule / now:

Query Issue hybrid file@10 def@10 Issue lexical file@10 def@10 code.context_pack file@10
As written 13 / 20 / 17 8 / 12 / 12 15 / 20 / 15 9 / 12 / 12 12 / 21 / 16
After a greeting 13 / 12 / 18 6 / 4 / 13 11 / 12 / 17 6 / 6 / 13 10 / 9 / 16
After a form 7 / 6 / 10 5 / 4 / 8 7 / 5 / 12 5 / 3 / 9 1 / 2 / 3

The opening rule finds three more files in the top 10 on the issues as written, which all open with their title, and falls below the old selection when they do not.

The tables above are from this revision, run lq-terms-n-53cfaabb of the harness on the pushed commit, with a greeting run and a form run of the same commit. Against the fifth revision (run lq-terms-m-6898d4a0), which counted the directories of a path as words, the lists of 2 of the 24 long issues as written differ and 3 of 232 cells move, two down and one up. On the seeded half the lexical-only request loses the file of issue F (rank 4 to absent: its list drops home, dev, a user name and sql for fetch, compiler, results and value, and its top 10 holds tests and the files of the trace's outer frames), and the same issue's definition moves from rank 3 to 1 with tests excluded. On the named half code.context_pack loses the file of the only() issue (rank 4 to absent, as before the change: two directory words of the reporter's own paths leave its list for __init__, the name of a file in its trace, and fetch). After a greeting the two cells of issue F move the same way, and after a form none. The leg-level tables below (the leg's own pool, a caller's terms) and the function-word table are from the fifth revision's lists.

The fifth revision against the fourth: the fourth revision's tables came from a run made before its commit; a run of that commit (lq-head-8f86ba24) returns the same answers, 0 of 232 issue-and-request cells different. Against it the fifth revision's lists of the 24 long issues as written differ on 3, each by one function word the issue writes as code (WHERE, only(), None), and 4 of 232 cells move, all up: one on the named half (code.context_pack for the only() issue, file rank 6 to 4) and three on the seeded half (the definition of the None issue: rank 9 to 5 on the lexical request, absent to 7 on the hybrid one, 10 to 5 with tests excluded). After a greeting 2 cells move, one up and one down, both on the named half, and after a form none (against the fourth revision's greeting and form runs, which were made before its commit and not repeated).

How a function word is treated, against the third revision (77 words, never a word to keep), as cells better / worse on the five issue requests:

Query Function words Named in advance (14) Seeded draw (15)
As written The class, never kept (fourth revision) 3 / 1 0 / 4
As written The class, counted where written as code (now) 3 / 1 3 / 1
After a greeting The class, never kept 5 / 1 8 / 2
After a greeting The class, counted where written as code 4 / 1 8 / 2
After a form The class, never kept 0 / 0 1 / 0
After a form The class, counted where written as code 0 / 0 1 / 0

The seeded cell that is worse as written is issue G below, which says over twice in prose. The named one is the only() issue on code.context_pack (file rank 3 to 4; 6 on the fourth revision). In the leg's own pool the class and no list at all differ by at most two issues per half at the top 10 and the top 50, for files and for definitions, in each form: as file top 10 / top 50 with the class against without, 4 / 7 against 4 / 6 (named) and 8 / 11 against 7 / 11 (seeded) as written, 5 / 7 against 4 / 7 and 8 / 13 against 6 / 12 after a greeting, 3 / 7 against 4 / 7 and 8 / 11 against 7 / 11 after a form. What the class changes most is the gate: its median share is 42%, 36% and 35% with the class and 43%, 43% and 40% without.

Issues that got worse on the issues as written, as file/def ranks with - for not in the top 10 (11 of 145 issue-and-request cells are worse and 38 better: 4 and 12 of 70 on the named half, 7 and 26 of 75 on the seeded half):

Issue Request Before After Cause
A Hybrid, tests excluded 1/1 5/10 annotations, subquery, columns are said once; the words the issue repeats come from a pasted test. Hybrid and lexical are unchanged.
C Hybrid, tests excluded 1/1 2/2 One rank.
D Hybrid 4/5 6/8 The repeated words are from pasted model code. Lexical stays 1/1; with tests excluded, 1/1 to 1/3.
E Lexical only 5/- -/- local and take are said once; the repeated words are from pasted model code. With tests excluded, 7/- to -/-.
F Lexical only 5/8 -/- timedelta and converters are said once, in traceback lines, and the top 10 holds the files of the trace's outer frames. With tests excluded, 1/7 to 1/1.
G Lexical only 7/- -/- aggregates and contain are said once, and over, which the issue says twice in prose (aggregate over window functions), is a function word it never writes as code. With tests excluded, 9/- to -/-.
H Hybrid 1/- 3/- Lexical gains the definition, 1/- to 1/3.
I Hybrid, tests excluded 9/10 1/- The file moves to rank 1 and the definition leaves the top 10. Hybrid, -/- to 3/3; lexical, 6/7 to 3/3.

In the lexical leg's own 200-row pool (the leg's statement replayed with each list, issues past the threshold), as before / opening rule / now:

Query n File in the top 10 File in the top 50 Definition in the top 10
As written 24 8 / 12 / 12 13 / 18 / 18 5 / 6 / 6
After a greeting 28 8 / 5 / 13 14 / 8 / 20 5 / 5 / 8
After a form 29 6 / 5 / 11 15 / 6 / 18 5 / 5 / 6
Without its first line 23 8 / 8 / 11 15 / 16 / 17 5 / 6 / 5

Six terms added by the caller to each issue (extra_terms, what expand and code.ask pass), in the leg's own 200-row pool, as before / fourth revision / now. The first list is six names from the query and admin layers, which most of the issues are about; the second is six names from the request, template, mail and static-file layers. Both were written before the first run and are the same for every issue. "Loses an identifier" counts issues whose list holds fewer of the query's own indexed identifiers than fit in 16 slots (identifiers the index lacks are not counted, which an earlier table did). "Loses a term" counts issues whose list lacks an indexed term that the same build's list without the caller's terms holds. "A name not asked" counts issues whose list holds fewer of the proposed names than the index contains.

Caller's terms Half of the set n File in the top 10 File in the top 50 Definition in the top 10 Definition in the top 50 Loses an identifier Loses a term A name not asked Gate share, median
Related layers All 27 14 / 12 / 13 20 / 23 / 23 7 / 9 / 9 10 / 16 / 16 1 / 3 / 0 n/a / 3 / 0 3 / 5 / 6 44% / 52% / 52%
Related layers Named in advance 13 6 / 6 / 6 10 / 12 / 12 3 / 4 / 4 4 / 9 / 9 0 / 1 / 0 n/a / 1 / 0 2 / 3 / 3 48% / 55% / 55%
Related layers Seeded draw 14 8 / 6 / 7 10 / 11 / 11 4 / 5 / 5 6 / 7 / 7 1 / 2 / 0 n/a / 2 / 0 1 / 2 / 3 36% / 48% / 49%
Unrelated layers All 29 8 / 9 / 9 16 / 21 / 21 5 / 6 / 6 8 / 11 / 11 1 / 4 / 0 n/a / 4 / 0 3 / 5 / 7 40% / 45% / 45%
Unrelated layers Named in advance 14 3 / 4 / 4 7 / 9 / 9 3 / 3 / 3 3 / 5 / 5 0 / 2 / 0 n/a / 2 / 0 2 / 3 / 4 44% / 47% / 47%
Unrelated layers Seeded draw 15 5 / 5 / 5 9 / 12 / 12 2 / 3 / 3 5 / 6 / 6 1 / 2 / 0 n/a / 2 / 0 1 / 2 / 3 31% / 41% / 41%
  • What the query keeps: with the related names 3 issues on the fourth revision were asked with fewer of their own identifiers than fit. They keep them now. Two of the three name 18 and 26 identifiers the index contains, fill the list alone and are asked with none and one of the six names; their gold file is first on every build. The third moves from rank 15 to 10 (8 before the change).
  • The seeded half against the old selection, related names: it is 7 against 8 at file top 10. Three seeded issues leave the top 10 and two enter. All three have the gold file in the top 10 with their own list (ranks 5, 9 and 9) and lose it when the six names are added (42, 18 and 11): the gold chunk holds 0, 0 and 2 of the names' lexemes on their lists, so each adds other chunks. The old selection asked for 6 and 7 of those lexemes on the first two, against 13 and 8 now, because it cut every piece that is not rare. The two that enter (35 to 5 and 12 to 2) are issues whose gold chunk holds 9 of 12 and 5 of 15.
  • Against the old selection, file in the top 10: with the related names five issues leave it (two named, three seeded) and four enter (two and two); with the unrelated names two leave (both seeded) and three enter. The old selection kept every identifier in reading order and then the rarest parts of the proposed names.
  • Gate share with a caller's terms: p90 72% / 61% / 61% and maximum 99% / 82% / 82% with the related names; p90 73% / 64% / 64% and maximum 99% / 82% / 82% with the unrelated ones.

Cost over the 29 issues as written, two passes on each build in one session, in milliseconds (pass 1, pass 2), measured on the second revision. The fourth revision's lists for these requests differ from its lists by one or two words on 11 of the 24 long issues, and this revision's from the fourth's by one word on 3; neither was timed:

Request Wall median Wall p95 Lexical leg median Lexical leg p95
Issue, hybrid 347, 421 to 333, 335 477, 497 to 451, 470 205, 235 to 195, 186 296, 334 to 286, 299
Issue, lexical only 285, 277 to 260, 252 325, 370 to 332, 318 177, 177 to 163, 141 225, 244 to 231, 191
Issue as one line, top 3 394, 388 to 341, 345 502, 478 to 493, 449 204, 205 to 173, 172 279, 291 to 261, 240
First line only, hybrid (unchanged code) 266, 259 to 275, 241 344, 429 to 380, 356 122, 97 to 101, 99 209, 175 to 199, 191
First line only, lexical (unchanged code) 195, 203 to 189, 184 260, 265 to 322, 265 97, 100 to 95, 93 168, 138 to 143, 149

The unchanged rows show the run-to-run spread. In system.query_log the lexical statement (232 per pass) reads the same rows (median 79,968 before, 79,813 after) in less time (median 136 ms to 118 ms, p95 288 ms to 226 ms) with less memory (median 54 MB to 42 MB). The list's median length falls from 32 to 24.5 lexemes.

Share of a namespace's chunks that pass the text-index gate (hasAnyTokens on the list), as before / opening rule / now:

Query Median p90 Maximum
As written 33% / 52% / 42% 69% / 75% / 65% 99% / 95% / 82%
After a greeting 30% / 43% / 36% 69% / 70% / 65% 99% / 82% / 82%
After a form 28% / 45% / 35% 69% / 71% / 65% 99% / 82% / 82%

In a replay of the rule on the issues as written, the median with a common word ranked like any other word is 62%, and without the function-word list 43%. The largest namespace here holds 76,607 chunks, and on it the statement reads every row whatever the gate admits; a larger namespace was not measured.

The neighbours of the two shares, in the leg's own pool, as file in the top 10 / top 50 and definition in the top 10 / top 50. Neither value was chosen from this table:

Query Value Named in advance: file definition Seeded draw: file definition Gate share, median
As written (11 and 13) Words take 1/4 of the list 5 / 6 4 / 6 8 / 10 5 / 8 35%
As written Words take 1/2 (the value) 4 / 7 3 / 6 8 / 11 3 / 6 42%
As written Words take 3/4 3 / 6 3 / 4 4 / 10 2 / 6 42%
As written Words take every free slot 1 / 5 1 / 3 2 / 9 1 / 3 42%
After a greeting (13 and 15) Words take 1/4 5 / 8 4 / 6 10 / 12 7 / 10
After a greeting Words take 1/2 (the value) 5 / 7 4 / 6 8 / 13 4 / 8
After a greeting Words take 3/4 3 / 8 3 / 4 6 / 12 3 / 6
After a greeting Words take every free slot 1 / 5 1 / 2 3 / 10 2 / 5
As written Common past 1 chunk in 16 3 / 8 3 / 6 7 / 10 3 / 7 38%
As written Common past 1 in 8 (the value) 4 / 7 3 / 6 8 / 11 3 / 6 42%
As written Common past 1 in 4 5 / 6 3 / 6 8 / 10 4 / 7 51%
As written No common rule 5 / 6 4 / 6 8 / 10 4 / 7 62%
After a greeting Common past 1 in 16 3 / 7 3 / 6 7 / 12 5 / 9
After a greeting Common past 1 in 8 (the value) 5 / 7 4 / 6 8 / 13 4 / 8
After a greeting Common past 1 in 4 5 / 7 3 / 6 8 / 12 5 / 9
After a greeting No common rule 5 / 7 4 / 6 8 / 12 5 / 9
  • The words' share: more than half the list for words loses files in the top 10 on both halves and in both forms. A quarter does as well as a half or better at the top 10 and one issue worse per half at the top 50 as written.
  • The common rule: retrieval moves by at most two issues per half in either direction across the four values. What the rule changes is the gate: the median share grows from 38% to 62% as it is loosened.

Controls, the same request to both builds on one index:

  • Identical answers, scores included: an identifier, a dotted identifier, a short question, a short request for tests (9 test units in the top 10 on both), and a 35-lexeme question between the list size and the threshold.
  • A long request for tests of one method: the same two test units are in the top 10 on both builds. The method the request names moves from absent to rank 4, under its class at rank 1.
  • A 54-lexeme prose question with no identifier, on one line and again after a greeting line: both builds answer. The function that computes the result list moves from absent to rank 1 in both forms. The question says many to many, and many is not asked for.
  • The same subject as prose that repeats three words, after a 19-lexeme greeting, and after 13 content words of prompt context: neither build returns that function in the top 10; after the greeting another unit of the files that hold the answer is at rank 9, where the old selection had none. After the greeting the list is 6 greeting words and 10 subject words; after the prompt context it is 14 context words and the 2 words the question repeats. This is the known cost above.
  • A question of only links, numbers and words shaped like deadbeef07: both builds answer. The list is deadbeef, 15 numbers and 16 of those words, which now read as names the index does not contain; the old selection asked with 32 link and hash fragments.
  • A question about two lint codes the index does not contain: both codes are in the list, after its 16 words.
  • A 79-lexeme question of function words and 13 other words, 8 of them in more than one chunk in eight (leg-level): the list is the 13 and the first three function words (gate share 90%, statement median 69 ms over five runs); the old selection asked with 32 function words (8%, 111 ms). Both return 200 rows.
  • A 53-lexeme prose question with 34 content words, 6 of them common (leg-level): the third revision, the fourth and this one choose the same 16 words, none of them common.
  • A long question about only(), written as code and again as prose, under one sentence of prose: written as code its list holds only in the place of its sixteenth word; in prose its list is the fourth revision's. Neither build returns the method's file in the top 10 in either form: the sentence takes ten of the sixteen word slots, the known cost above.
  • A long question that says twice that a call returns None: none is in the list in the place of a word said once. Neither build returns the method's file in the top 10.
  • A seven-frame traceback under one sentence (83 lexemes): on the hybrid request the file the trace ends in is first on both builds. On the lexical-only request it is first before the change and second now, behind changelist_view, the function of the trace's fourth frame; the fifth revision did not return it in the top 10. In the leg's own pool the file is at rank 4 with the old list, 20 with the fifth revision's and 2 with the new one: the directories of the paths held 7 of the 16 word slots, and the list now holds kwargs, options, view, valid, formsets, errors and full in their place, beside File and line. The lexical-only request is therefore one rank worse than before the change on this control, and that difference is known.
  • A prose question with three UUIDs, a span id 7c05 and a colour ff00aa: the fourth revision's list held 13 pieces of the ids among its 28 terms; the list is now 16 words of the prose. A unit of one of the three files that hold the answer is at rank 2 on the hybrid and the lexical-only request, and none is in the top 10 before the change.
  • A long question of function words alone: the fourth revision's list is empty, so its lexical leg had nothing to ask with; the list is now the first 16 of them, and both requests answer.

Other checks:

  • ruff format --check, ruff check, the function-length gate, the mypy gate, the secret gate and every generator's --check pass. The mypy gate's only errors here are five in server/llm/vertex_auth.py from a library this machine lacks; CI has it.
  • The unit tests cover is_hash, is_id_piece, strip_non_terms, term_counts, select_terms, the plan, and the service seam: a greeting or a form before a prose question keeps the subject words in the scan; a question of only links and hashes still has terms; a short code is a term, and so is a word and a number such as ed25519; a UUID leaves no term and a group of an id is not an identifier; a long concept term list is cut to its rarest tokens; a long question with six proposed names keeps its own identifiers and the proposed names in the scan, through code_search.search; a long question whose proposed names repeat three of its words, with more lexemes than slots, is asked with its own list whole and in order beside the names, through code_search.search; the question's known terms are the same with and without a caller's terms at five list sizes, and a question that fills the list alone keeps its identifiers; a function word counts where it is called, read off an object, alone in backticks or capitalised inside a sentence, and not in prose, at a sentence's start or inside an identifier; a long question whose subject is only() or None is asked with that word, and the same question in prose is not; a function word of each class and the half of a contraction waits for every other word; a question of function words alone still has a list; past the list's size a word said once goes after the identifiers and a repeated one before them; a seven-frame traceback keeps the names its frames call, the subject its sentence states and the names of its files, and gives no word slot to the directories of its paths; the directories of a path are not counted when it starts at a root, follows a drive or is relative with an extension, also at the end of a sentence, a directory word the prose also says is counted where the prose says it, and request/response, a fraction and a date are counted as words; a namespace of five chunks and a question whose every word is common still give a list that holds the subject words; a caller's term shaped like a hash stays a term and changes neither the question's identifiers nor its counts. Eleven of the changed tests fail on the fourth revision's source (checked by calling the test file against it), and the traceback test and the path test fail on the fifth revision's. Eleven linear-time cases cover the module's reads of request text, three of them for paths. I ran the test bodies by direct call; the suite itself runs in CI.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The agent measured the before and after tables above on its own index, read the per-issue rank changes, and ran the gates listed.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code


Note

Medium Risk
Changes lexical token selection for over-threshold queries—a core retrieval path—with nuanced heuristics (function words, paths, hashes), though short queries and wire/API stay unchanged and rollback is a revert.

Overview
Long code.search queries (above code_search_query_max_tokens) no longer gate the lexical leg on the rarest tokens in reading order. The trim plan in query_trim.py now strips URLs, UUIDs, and commit hashes before tokenizing, scores how often each lexeme appears (with English function words counting only when written as code, and traceback path directories excluded), and select_terms fills at most 32 slots with identifier whole forms plus about half the list for repeated subject words first.

Call sites (lexical.py, concept expansion in cards.py) switch from select_rarest to select_terms and pass richer TrimPlan fields (said, own, named) for caller extra_terms. Docs, schema, and settings text for code_search_query_max_tokens describe the new behavior; query_trimmed_tokens_used may report fewer than 32. Unit tests are expanded heavily around the new rules and the search seam.

Reviewed by Cursor Bugbot for commit 6f9ae68. Bugbot is set up for automated code reviews on this repo. Configure here.

The lexical leg of code.search scores a chunk by the idf of the query
lexemes it holds, over at most 32 lexemes. A query past the token
threshold kept its identifiers in reading order and then its rarest
words. Rarest selects what the code to change is least likely to hold:
hash pieces and route words of a link, names the corpus does not
contain, and prose that is rare in code because it is not code
vocabulary. Each such lexeme only adds other chunks to the pool.

A long query now keeps its opening (its first lexemes, half the list)
and the whole forms of its identifiers, rarest first when they exceed
the list. URLs, bare numbers and hash-like words are removed before the
tokenizer splits them, and the prose after the opening is not kept.
A query under the threshold is unchanged.

Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 28, 2026 2:30am UTC
loom-telemetry Ready Ready Preview Nov 28, 2026 2:30am UTC

Request Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/code/domain/index/query_trim.py Resolved
…it repeats

The opening of a long query is not always its subject: after a greeting or a
template the first 16 lexemes held none of it, and prose after them was never
eligible. The lexical list is now the identifiers and, for half the list, the
words: those the query says more than once first, then the others in reading
order. An English function word, and a word more than an eighth of the chunks
hold, is not kept, so they no longer reach the text-index gate.

A hash is stripped only at seven or more hex characters with digits among
letters, so a short code (F401, e164) is a term and a number ranks last. A
query of only addresses and hashes keeps them as its terms. The concept leg
passes its terms as chosen terms, so a long list is cut to its rarest, not to
its first.

Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/code/domain/index/query_trim.py Resolved
zlareb1 and others added 2 commits November 28, 2026 03:39
…s and is never left without words

The terms a caller adds to a long question ranked above its words and its
identifiers, so six proposed names took every slot the question's own
identifiers had. They now rank after the identifiers' whole forms, the
rarest first.

A word that more than one chunk in eight holds was removed from the list.
In a namespace of a few chunks that removed every word, and the lexical leg
returned nothing. Such a word now ranks after the other words and fills a
slot they leave free, as a number and an unknown lexeme do.

Only the question is stripped of links and hashes, so a caller's term of
that shape stays a term.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
zlareb1 and others added 3 commits November 28, 2026 04:40
A lexeme of a caller's term that the question also says left the question's
words and ranked with the pieces of the proposed names, the rarest first, so
a subject word that many chunks hold was cut once the list was full. The
words are now chosen from the question alone, with its own counts, and a
caller's terms only add to the list: their identifiers beside the
question's, the lexemes only they hold after them.

The function words are the closed classes of English, listed by class, with
the halves the tokenizer leaves of contractions.

A hash is hex in which letters and digits meet at two or more places, so a
word and a number such as ed25519 stays a term.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
A function word was never a word to keep, so a question about `only()`
or one that says a call returns `None` lost that word, and a question of
function words alone was asked with nothing. A function word now counts
where the question writes it as code: called, read off an object, alone
in backticks, or capitalised inside a sentence. One it never writes that
way fills a slot when no other word is left.

Past the list's size the question's identifiers go before the words it
says once, and a caller's terms take the slots the question leaves, so
its known terms are the same with and without them.

A UUID is removed whole like a hash, and a hex word in which letters and
digits alternate (a group of an id, a colour) is a word, not an
identifier that is always kept.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…h as repeated words

Each frame of a traceback repeats the directories of its path, so `usr`, `lib`,
`python`, `site`, `packages` and the package directories counted as words the query
repeats and took 7 of its 16 word slots. On a seven-frame traceback the lexical-only
request lost the file the trace ends in, which the old selection returned first.

A path's directories say where a file is, so `term_counts` does not count them: a
lexeme only they hold is said once, and the file's own name is counted as written.
A run of names joined by `/` or `\` is a path when it starts at a root or ends in a
file name with an extension, so `request/response`, `and/or` and `1/8` are counted
as words.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 53cfaab. Configure here.

Comment thread server/code/domain/index/query_trim.py Resolved
…entence as a path

The last name of a path took the full stop that ends its sentence, so a relative
path there (`in db/models/query.py.`) showed no extension and was read as words
set side by side: its directories counted as repeated and could take word slots.
The extension is now looked for before the name's trailing full stops.
`request/response.` and `and/or.` are still counted as words.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1

zlareb1 commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Closing without merging. Measured with a real embedding model on 29 public issues, this change is within one instance of zero in every hybrid cell (gold file in the top 10: 25 before, 25 with this change alone), and combined with the two search changes already on main it moves a pasted traceback's own file from rank 1 to rank 3 on the default hybrid request. The gains reported in this PR were measured with a placeholder embedder, where the lexical list carried the whole ranking. The follow-up is the weight of the lexical list in fusion for long queries, which the same measurement shows matters more.

@zlareb1 zlareb1 closed this Nov 28, 2026
@zlareb1
zlareb1 deleted the fix/code-search-long-query-terms branch November 28, 2026 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant