Repository navigation
feat(search): find other word forms with messages search --forms - #216
neoromantic merged 22 commits into
Conversation
…unning it (neoromantic#92) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…romantic#92) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…neoromantic#92) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oromantic#92) Co-Authored-By: Claude Opus 5.5 <[email protected]>
… add the stemmers' notice Co-Authored-By: Claude Fable 5.2 <[email protected]>
Plain queries expand to the index's forms of each word. A query with FTS5 syntax or --phrase is matched as written, as before, so the two refusals of the opt-in flag are gone. The answer has forms (null when the query was not expanded) and filters.exact. BREAKING CHANGE: tg messages search returns more results for plain words; pass --exact for the previous behaviour. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The forms come from the whole cache's vocabulary, so listing them could show words of chats outside the allowlist. Checking each form against the allowed chats would read every message with that word, so expansion is off while a list is set, as with --exact. Co-Authored-By: Claude Fable 5.2 <[email protected]>
2ms stemmed to 2m, so a search for milliseconds also matched minutes. A word with a digit or a letter of another script is now matched as typed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The vocabulary lookup reads the folded word, which can be another index word than the one the tokenizer makes of the typed word (a letter typed with a combining mark), so the exact match dropped out. Every group now starts with the word as typed, and the cap of 30 keeps a place for it. Co-Authored-By: Claude Fable 5.2 <[email protected]>
No other word stems to one letter, so scanning every index word with that first letter only cost time. Two-letter words keep the scan: its stems to it, and её reads as ее. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Two things from a second review that are your call:
|
…arch-word-forms # Conflicts: # CHANGELOG.md
…arch-word-forms # Conflicts: # CHANGELOG.md
neoromantic
left a comment
There was a problem hiding this comment.
Let's go with the opt-in version for #92. The Russian recall gain is worth shipping, and the precision and two-word latency results support keeping today's behavior for existing callers. Please bring back tg messages search --forms and keep plain searches exact by default, as in 17b5bd7. Please update the help, docs, changelog and tests to describe that behavior.
The allowedChats behavior from your second review looks right. Please keep expansion off when an allowlist is active, including with --forms, and include the stemmer NOTICE with the release binaries and the Homebrew install. Once those changes are on the branch, we can review the updated head and run CI.
…arch-word-forms # Conflicts: # CHANGELOG.md # src/commands/messages/search.ts # src/db/messages-search.ts Co-Authored-By: Claude Fable 5.2 <[email protected]>
The FTS5 match built for count, the untyped-match check and the search that joins transcript hits passed only the phrase option, so it dropped the word forms: --count disagreed with the results, and forms stopped working once a transcript matched. Co-Authored-By: Claude Fable 5.2 <[email protected]>
findQueryForms read only message_search_vocab, so a form that occurs only in a voice or video note transcript was never searched for. A second fts5vocab table, transcript_search_vocab, is read together with it, summing doc per term. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Plain tg messages search matches each word as typed, as 0.16.0 does. --forms turns word forms on and is refused with FTS5 syntax or --phrase, as in 17b5bd7; --exact and filters.exact are gone, filters.forms is back. While allowedChats is set, --forms doesn't expand and forms is null. The help, docs, skill, changelog and storage.md §9 record the opt-in choice made in review. Co-Authored-By: Claude Fable 5.2 <[email protected]>
A word whose stem has one or two letters (ok, не) scanned every index word that starts with its first letter, and a word with no stem (2026) scanned on its prefix only to keep itself. Both are now exact lookups, the first with е and ё both, so --forms no longer reads a large part of the vocabulary before such a search. it no longer finds its. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ormula The release workflow copies NOTICE into dist, checksums it with the binaries and uploads it to the GitHub Release. The Homebrew formula takes it as a resource and installs it in the formula's prefix; tap.sh waits for it to download before the audit. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Done as you asked, on 3434504:
The description is rewritten for the opt-in version. CI is green. |
What and why
tg messages search --formsalso matches the other forms of each plain word that occur in the index:квартираfindsквартируandквартирах,meetingfindsmeetings. Without the flag search is what 0.16.0 does: each word as typed. This is #92, with the benchmark it asks for. Refs #92The need: the index has no stemming, so a Russian word finds about a third of the messages that contain some form of it, and a two-word Russian query about a ninth. A caller has to know to write
квартир*.Without the flag (unchanged; real output of the e2e environment, trimmed to the fields that matter)
With
--formsThe benchmark (
bun run bench:search, defined indocs/storage.md§9 before any candidate ran)Judge: a dictionary lemmatizer for each language, independent of the candidates; a result is right when it holds a form of the query word's lemma. Corpus: 50,000 public conversational sentences per language, fetched at run time. 385 Russian and 400 English single-word queries in four frequency bands, 200 two-word queries per language, paired bootstrap intervals.
prefix=index 1.8–2.1× the size; loses the newest-first walk of #118The same run on a real 62,000-message cache (read-only, aggregates only) agrees: Russian 0.33–0.37 found with exact search, 0.74–0.76 with light stems at 0.93 correct; two-word Russian queries with no result fall from 60% to 36%.
Stems in the index and expansion of the query gave identical results (the same matches by construction), so this PR expands the query and leaves the index alone.
Opt-in, as reviewed
No candidate met the bar fixed in advance (Russian recall up by at least 0.15 and precision down by at most 0.05): light stems gain 0.34 and lose 0.059. An earlier head of this PR made forms the default anyway; per your review of 2026-11-22 it is a flag again, as in 17b5bd7, and plain searches stay exact for existing callers.
About half of the wrong matches are different words with one stem (
брат/брать,лет/лето); the unit tests list them as known limits, anddocs/search.mdstates them.How it works
message_search_vocabandtranscript_search_vocab, persistentfts5vocabtables over the message and transcript indexes, and the FTS5 expression becomes anANDofORgroups. The tables are created with the schema if missing, have no storage of their own, survive a rebuild of the index, and are readable onquery_onlyconnections such astg sql. Older tg versions and other SQLite clients read and write the cache as before.src/utils/word-stems.ts: Russian noun and adjective endings, English plurals,ёread asе. They are ports;NOTICEcarries the attribution. The benchmark imports the same module, so it measures what ships.--formswith FTS5 syntax (a quoted phrase,word*,OR,sender:,chat:) or with--phraseisINVALID_ARGSwith a suggestion, as in 17b5bd7: forms beside aNOT, a phrase orsender:would change what the query means.allowedChats. While an allowlist is set,--formsmatches words as typed andformsisnull: the forms come from the whole cache and would show words of other chats.ok,не) and a word with no stem (2026,2ms) are looked up as typed, without scanning the vocabulary. Soitdoes not findits.cut: true).forms(per query word, the words searched for;nullwithout--formsor withallowedChats) andfilters.forms.nextcarries--forms.--snippethighlights every form. Filters,--countand both sorts work with it.ё/е: with--forms,ещеfindsещёand the reverse; without it they are different letters, as before.NOTICEin the releaseNOTICEwas only in the npm tarball. Nowrelease.ymlcopies it intodist, adds it tochecksums.txt(6 lines instead of 5) and uploads it to the GitHub Release next to the binaries;formula.shreads its checksum and adds aresource "notice"that the formula installs into its prefix;tap.shwaits for the asset.docs/releasing.mdanddocs/build-distribution.mdfollow.Not run: the release workflow itself, the upload,
brew audit --strict --onlineandbrew install(no release has the asset yet). Checked locally:sh -non both scripts; a dry run offormula.shon made-up binaries plus the realNOTICE(checksums verify, 6 lines);formula.shrefuses achecksums.txtwithoutNOTICE;ruby -candbrew styleon the generated formula. Please look at that part with the eyes of whoever runs the next release.Other decisions you may want to change: the cap of 30;
formsalways present in the output.Left out, on purpose: typo correction (measured in the same benchmark: a fallback when a query finds nothing passes its bar; I'll send it separately), phrases of word forms (they need stems in the index), keyboard-layout and transliteration rewrites.
The benchmark vendors the Snowball stemmers under
scripts/snowball/(BSD-3,COPYINGincluded) only to measure the aggressive option; nothing insrc/uses them. Say if you'd rather the benchmark fetch them at run time.How I checked it
E2E through the real CLI on a made-up cache (10 tests in
src/test/messages-search-forms.e2e.test.ts): Russian and English forms found with--formsand not without;ё/е; a word with no forms; FTS5 syntax and--phrasematched as written without the flag and refused with it; chat, sender, date and deleted filters;allowedChatswith--forms; relevance sort, count and snippets;nextwith and without--forms.Unit tests beside the code: the stemmers (with the wrongly merged pairs as known limits), the expansion (range scan, stem filter, cap, absent word,
ё/е, case and diacritics, exact lookups for short and stemless words counted by database calls, the tables surviving a rebuild and read on aquery_onlyconnection), the query builder.Read-only on a real cache with an earlier head: one word found 146 messages with forms and 19 as typed (7 forms); a two-word query 4 and 1; the existing cache got
message_search_vocabon open.On the current head (3434504):
bun run checkclean,bun run test1361 pass,bun run test:e2e448 pass, 0 fail.Not verified: Linux and Windows (the SQLite bundled there), query time on a cache of a million messages, and the release steps listed above.
Tests pass locally and use made-up data only
README, docs and affected demos follow the product content standard (
docs/search.md,docs/cli-commands.md,docs/database-schema.md,docs/storage.md,docs/releasing.md,docs/build-distribution.md, README, skill, CHANGELOG)Title follows the repo's commit style: Conventional Commits (
feat: …,fix: …) unlessAGENTS.mdsays otherwiseUpdate: review of 2026-11-22 applied (3434504)
--formsis back and plain searches are exact, as in 17b5bd7;--exactandfilters.exactare gone; withallowedChatsset--formsdoes not expand. Help, docs, skill, changelog and tests follow. The title lost its!: nothing changes for existing callers.NOTICEships with the release binaries and the Homebrew formula (section above).The updates below are the history of the branch while forms were the default; where they mention
--exactor the default, the sections above are current.Update: second code review (617c9fe)
A second, independent review of this branch. Each point below was first shown by a failing test, then fixed.
allowedChats:formslisted words from the whole account's index, so with an allowlist it showed words that occur only in chats outside it. While an allowlist is set, words are now matched as typed andformsisnull, as with--exact. Reporting only the forms that occur in allowed chats would need, for each form, a match filtered by chat, which reads every message with that word when it occurs only elsewhere. Your call if you prefer that cost over losing word forms under an allowlist.2mswas read as2m.itsanditare forms of each other).NOTICEis in the npm tarball. The binary release and the Homebrew formula ship only the executable; the notice for the ported stemmers would need to be a release asset the formula installs..github/is untouched.After these commits:
bun run checkclean,bun run test1216 pass,bun run test:e2e347 pass, 0 fail.Update:
mainmerged in (649eef9)mainup to #220 merged in, no conflicts and no changes to this branch's code.After the merge:
bun run checkclean,bun run test1227 pass,bun run test:e2e348 pass, 0 fail.Update:
mainmerged in (853a651)mainup to 0.15.1 and #232 merged in. One conflict:CHANGELOG.md(the entry of this PR now sits under the new## [Unreleased]). No code change.After the merge:
bun run checkclean,bun run test1239 pass,bun run test:e2e374 pass, 0 fail.Update:
mainmerged in (ac85803)mainup to 0.15.2 (#234) merged in. One conflict:CHANGELOG.md(the entry of this PR stays under## [Unreleased], above the new 0.15.2 section). No code change in this PR.After the merge:
bun run checkclean,bun run test1239 pass,bun run test:e2e374 pass, 0 fail.Update:
mainmerged in (392c8d8), d9113f5Conflicts in
CHANGELOG.md,src/commands/messages/search.tsandsrc/db/messages-search.ts: the forms are now worked out beforereadSearchPageand passed through it, andmain's--type,typesPending,suggestionandtranscriptMatchoutput is kept.Two things were wrong once this was combined with transcript search on
main, each fixed with a test that fails without it:count, the untyped-match check and a search with transcript hits built their match without the forms, so--countgave 1 where the list had 4 results. They now get the same query as the list.transcript_search_vocab, is read together withmessage_search_vocab;docs/search.md,docs/database-schema.mdand the schema description follow.Checks:
bun run checkclean, unit 1361 pass, e2e 447 pass, 0 fail.Known and not changed here: a two-letter word or a number still scans the vocabulary by a one- or two-character prefix before the search, and the description of
message_search_vocabintg sql print-schemastill names--forms, which is now the default with--exactto turn it off.🤖 Generated with Claude Code