Repository navigation
fix(sync): link a thread's replies once per page, not once per message - #3333
Merged
Merged
Conversation
Saving a message also pointed replies saved before their thread root at it. That UPDATE filters on thread_external_id, which no index covers, so SQLite read the whole chat for every message. A page of 100 took 78 ms to save at 10k stored messages and 919 ms at 90k, and sync fell from about 75 to 7 messages/s (tg-cli #917). linkThreadChildren now runs once per saveMessages call for all the page's ids. It is still one chat scan per page; the index on (chat_id, thread_external_id) that removes the scan goes into store version 73 with 1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leemour
added a commit
to WireCatLabs/cli-testing
that referenced
this pull request
Nov 25, 2026
Benchmark and results for tg-cli [#917](WireCatLabs/tg-cli#917), the reason for cli-core [#3333](WireCatLabs/cli-core#3333). Synthetic data only. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
leemour
added a commit
that referenced
this pull request
Nov 25, 2026
Asked by tg-cli (#917, cli-core #3333): linking replies to their thread runs once per page with WHERE chat_id = ? AND thread_root_id IS NULL AND thread_external_id IN (...), a scan of the chat without this index. With it, a page saved in 19 to 25 ms up to 40,000 messages in that session's benchmark. It goes inside v73, which no release carries yet, so stores take one migration for 1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task
tg-cli #917:
messages syncfalls from about 75 to 7 messages/s as a big group fills the store.Why
upsertMessageranUPDATE messages SET thread_root_id=? WHERE chat_id=? AND thread_external_id=? AND thread_root_id IS NULLfor every saved message. No index coversthread_external_id, so the plan isSEARCH messages USING INDEX messages_by_time (chat_id=?): a scan of the whole chat per message. The CPU profile put 85% of the save time in this SQL.What
linkThreadChildrenruns that link once persaveMessagescall, for all the page's ids (json_each). The phone projector calls it for its one message.(chat_id, thread_external_id) WHERE thread_root_id IS NULL AND thread_external_id IS NOT NULL, goes into store version 73 with 1.0 (feat!: cli-core 1.0.0-rc.0, the stable API, store version 73 and the 1.0 fixes #3288), agreed with that session.Testing
Benchmark: one chat, 100-message pages saved top-down through
saveMessages+markRange, database on tmpfs. Save time per page:The "after" run was on a machine at load average 25. Full write-up to follow in cli-testing.
src/store/messaging.test.ts: a reply and its root saved in one page are linked.vitest run src/store/messaging.test.ts: passes.batches.test.tsandrefill.test.tstime out locally at that load with and without this change, so CI decides.🤖 Generated with Claude Code