Skip to content

search: searching every chat reads a trigram index, about 70x faster on 50,000 messages, and finds Привет, École and Über - #477

Merged
SiddDevZ merged 14 commits into
DuskBotAI:devfrom
Yabuku-xD:message-search
Dec 3, 2026

Conversation

@Yabuku-xD

@Yabuku-xD Yabuku-xD commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Searching every chat read every message: lower(text) LIKE '%needle%' over the whole messages table. On pnpm bench:persistence (50,000 messages) that is about 80 ms per search, and the server stalls for that long unless the search worker runs it.

The same query also missed words with a capital letter outside A-Z. SQLite's lower() and LIKE fold only ASCII, while the query is lowercased in JavaScript, so on dev a search for Привет finds nothing in a chat that says "Привет, команда". "École" and "Über" behave the same way, as does any Cyrillic, Greek or accented capital.

This PR:

  • adds messages_search, a contentless FTS5 trigram index (detail=none) of each text message's text and each activity's tool name. It narrows a search of 3 or more characters to rows that hold its trigrams. The search query's own comparison still decides every match, so the index never adds a result;
  • lowercases rows with the same toLowerCase() as the query (a deterministic SQL function on the search connection). ASCII-only values keep SQLite's lower(), which gives the same result for them;
  • asks the index for each 3-character window in every way it can be written. SQLite's case folding is older than JavaScript's: it has no pairs for Cherokee, Georgian Mtavruli, Osage, Adlam or the letters added since Unicode 7, and it keeps İ apart from i. A query that relied on it missed 1,395 of 9,234 checks over every cased letter;
  • makes the team-backup snapshot drop messages_search before VACUUM. A contentless index keeps a deleted row's trigrams until its segments merge, and the snapshot comment says no search index may still hold a private conversation's words. The index is built again from the remaining rows when the database next opens;
  • moves the recall and search index setup into server/message-text-index.ts, because message-db.ts is at its size ratchet. This is a move only, with no change.

A search in one conversation does not use the index: its rows are already indexed by thread.

Numbers

pnpm bench:persistence (50,000 messages, an absent needle), Apple Silicon, Node 26.13.1, SQLite 3.54.1, two serial runs each. The machine had other load (load average about 5), so read these as ranges:

dev this PR
search p95, 10 and 50 sessions 72.8-77.9 ms 0.95-1.2 ms
search p95, 1 session (holds the first search) 74-77 ms 5.8-6.2 ms
event loop max, with searches 76-96 ms 5.9-29 ms
message write p95 0.10-0.13 ms 0.19-0.21 ms
1,000 writes, 50 sessions 166-169 ms 238-246 ms

The index cannot help a needle that most messages hold. Here is my own script on the same machine: 50 threads × 1,000 messages of about 1 KB, every message made from the same 20 words, median of 15:

needle dev, all chats this PR, all chats one conversation, dev → PR
absent 63-66 ms 0.5-0.6 ms 1.7 → 2.1 ms
zq (under 3 characters, no index) 45 ms 60 ms 1.3 → 1.6 ms
alpha (in every message) 81 ms 105 ms 0.7 → 0.7 ms
Foxtrot Golf (in most messages) 67-68 ms 102-104 ms 0.8 → 0.8 ms

So a search for a word in almost every message, across all chats, is about 30-50% slower. A word in few messages is about 100x faster.

Other costs:

  • messages.db grows about 15% (52.4 MB to 60.5 MB on synthetic text).
  • The first start after the update builds the index once: about 0.6 s per 50,000 messages.
  • The first search in a process builds a case table from the first two Unicode planes: about 4 ms.
  • A row with non-ASCII text pays for one JavaScript toLowerCase() when SQLite's lower() does not already match.

How it was checked

  • server/message-search-query.test.ts:
    • a property test (400 runs per CI pass) checks that for any messages, any later inserts, deletes and edits, and any needle, search returns exactly the rows whose toLowerCase() text holds the lowercased needle. Its alphabet has Cyrillic, Greek sigmas, İ ı, ß ẞ, DŽ Dž dž, the Kelvin and Ångström signs and fi. I ran it 20,000 times locally;
    • an exhaustive test puts every letter that Unicode gives a lower case at each of the 3 places in a window, and requires a hit through the index.
  • I made each part fail on purpose: a case-sensitive index, no case variants, and no İ→i form. Each one fails the tests. The new tests also fail on the version before the case fix.
  • workspace-backup-team-private.test.ts: the snapshot holds no messages_search table or trigger. A database opened from it builds the index again and finds the team's words but not the private ones. Without the drop, this test fails.
  • Upgrade: dev's message-db.ts wrote a database, then this branch opened it. The index and triggers were created. In-process, worker and thread-scoped searches all find "Привет", "привет" and "École", which dev does not find in the same file.
  • Fixture: control-dusk launch, new-bot, two send + wait turns with the fake engine, then GET /api/search (the worker path). "Привет", "ПРИВЕТ" and "école" find the Russian message, "quarterly" finds the English one, and "absent words" finds nothing.
  • pnpm typecheck and pnpm lint pass. The CI source guards pass, including file-size-ratchet.
  • 159 test files under server/ that touch messages, search, backups, routes, email, stores or memory: 158 pass, 1 skipped (1,855 tests). On this Mac the whole vitest run did not finish: it takes hours on the default single worker, and the Vitest pool crashed with EPIPE at 8 workers. CI's shards cover the rest.
  • pnpm typecheck, pnpm lint and pnpm test pass (typecheck and lint pass; for pnpm test, see the line above)
  • New behaviour has a test
  • No secrets, personal data or local paths in the diff or screenshots

No new runtime dependency. fast-check is a dev dependency for the property test. The property-tests PR from this fork also adds it, so whichever merges second needs a one-line rebase of package.json and pnpm-lock.yaml.

Not in this PR: ensureRecallIndex creates messages_fts and runs its rebuild as two separate statements. If the process stops between them, later starts skip the rebuild. I found this by reading the code, not by running it, and left it for a separate change.

This branch also carries the commits of #483: the fix for two tests red on dev (the find_tool ranking and the guest skill card), the flaky-test retry list for tests that failed at random in CI on Dec 1, and the link chaos fix, so this PR's CI can pass. They drop out of the diff once #483 merges.

…ge, about 70x faster on 50,000 messages

Searching all chats ran lower(text) LIKE '%needle%' over every row: 78 ms p95 in bench:persistence (50,000 messages),
blocking the server for that long unless the worker ran it. A contentless FTS5 trigram index (detail=none) of each text
message's text and each activity's tool name now narrows a needle of three or more characters to the rows that hold all
of its trigrams; the same LIKE still decides every match, so results do not change. A property test checks that against
a brute-force reading for any messages, edits, deletions and needle.

bench:persistence, search p95: 76-81 ms -> 0.43-0.62 ms; event loop max with searches: 82-87 ms -> 3-15 ms.
Costs: a message write's p95 0.12 -> 0.20 ms; the index adds about 15% to messages.db; the first start after the update
builds it once (about 0.6 s per 50,000 messages). A needle found in most messages, or under three characters, still
reads every candidate row.
…ривет, École, Über)

SQLite's lower() and LIKE fold ASCII letters only, while the query is lowercased in JavaScript, so a search for
"Привет" never found a message saying "Привет": the needle became "привет" and the row kept its capital П. Rows are
now lowercased by the same toLowerCase() as the query (a deterministic SQL function on the search's connection) and
matched with instr.

The trigram index folds case itself, so it still narrows a search; a needle holding U+0307 (from "İ", which lowercases
to i + U+0307) reads every row, since no indexed text holds that pair. The property test now uses an alphabet with
Cyrillic, Greek sigmas, İ/ı, ß/ẞ, DŽ/Dž/dž, the Kelvin and Ångström signs and a ligature, against toLowerCase().includes.

A needle under three characters, or found in most messages, still reads every candidate: that path now costs 17-34%
more (35 -> 47 ms for a two-letter needle on 50,000 messages).
…itten, checked for every cased letter

SQLite's FTS5 case folding is older than JavaScript's toLowerCase(): it has no pairs for Cherokee, Georgian Mtavruli,
Adlam, Osage and the Greek and Cyrillic letters added since Unicode 7, and keeps İ apart from i, so a needle folded by
one could miss rows folded by the other (1,395 of 9,234 checks missed). A search now asks the index for each
three-character window written any way that lowercases to it (İ standing for an i that ends a window, Σ for ς), so
the index only has to apply the same tokenizer to both sides. A test runs every letter Unicode gives a lower case at
each place in a window. The index triggers read a tool name only from valid JSON, so one damaged row cannot stop the
database from opening.
…no trigram of a private conversation

A contentless FTS5 index keeps a deleted row's trigrams until its segments merge, and VACUUM does not merge them, so a
Team Cloud export that removed a member's private conversations still held trigrams of their words. The snapshot now
drops the index and its triggers; the database builds it again from the rows left when it next opens.
… text is lowercased by SQLite

A search in one conversation asked the trigram index for every conversation's candidates first: 1 ms became 20-44 ms
for words most messages hold. It reads the conversation's rows by thread again. SQLite's lower() agrees with
toLowerCase() on ASCII text, so only a value with other characters calls the JavaScript lowercase.
…e-text-index.ts

message-db.ts is at its size ratchet; the FTS5 setup moves out unchanged.
…instead of 30 ms on the first search

No letter past U+1FFFF has a lower case (the last is U+1E921); a test fails if a later Unicode adds one.
… and the guest skill card test stages four kinds of work

bacb532 left two tests red on dev. Its new skill_manage description dropped the word reusable, so find_tool ranked
skills_list first for "save this procedure as a reusable skill"; the description says reusable SKILL.md again (a skill
is recurring work, which that change keeps). Its duplicate check turns away a create that covers an existing skill, and
the guest skill card test staged four skills with one description, the same as an earlier test's: each now has its own
kind of work.

(cherry picked from commit 78889fc)
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
Yabuku-xD added a commit to Yabuku-xD/duskbot that referenced this pull request Dec 1, 2026
…list's two retries

Fifteen tests failed in CI on Dec 1 in runs of main, a release and PRs DuskBotAI#477-DuskBotAI#483 whose changes do not reach them, a
different set each run, and pass locally. Each entry names the runs it failed in. vps-routing's 'reuses the existing
VPS with $settings' failed too (PR DuskBotAI#477, run 46623618361) but is left out: its name is made per case, and the list
matches names written in the file.

(cherry picked from commit 54f5c51)
…s 60 servers link from their own addresses

Every link chaos run since the per-address limit on new links (adc99d7, Nov 14) was cancelled at CI's 60-minute
limit, on main pushes, the weekly run and every PR that touches the link service: the scenarios finished in about five
minutes and printed their table, but a socket or timer a scenario left open held the process. The run now exits once its
results are written. The storm's 60 servers set up their links over one connection, so the node's front gave them one
address, which the limit refused after 30 (Too many new links from here); each now uses its own connection, as 60
machines would. Locally, --quick passes all 12 scenarios and exits in 5.6 minutes.

(cherry picked from commit 83ab615)
…y list's retries

chat-mcp-tools' shutdown verdict, memory-order and the encrypted workspace restore failed in PR DuskBotAI#479's run 46740020170,
whose change does not reach them, and pass locally.

(cherry picked from commit c47197f)
…os job installs no packages

The job runs `node apps/link-service/chaos/run.mjs` straight after checkout, so the undici import failed
with ERR_MODULE_NOT_FOUND before any scenario ran.
…ds nothing for 8 s, as the app does

src/lib/live-events.ts gives up on a dial that sends nothing in LIVE_EVENTS_OPEN_MS (8 s) and dials again. The probe
dropped a quiet stream only after it had sent something, so one silent dial after node-5xx's 502s held it down until
the relay closed it, about 90 s, and failed the scenario in CI on five of seven runs.
@SiddDevZ
SiddDevZ merged commit d5946b5 into DuskBotAI:dev Dec 3, 2026
31 of 33 checks passed
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.

2 participants