Skip to content

fix(sync): link a thread's replies once per page, not once per message - #3333

Merged
leemour merged 1 commit into
mainfrom
fix/sync-thread-link-per-page
Nov 25, 2026
Merged

leemour merged 1 commit into
mainfrom
fix/sync-thread-link-per-page

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

tg-cli #917: messages sync falls from about 75 to 7 messages/s as a big group fills the store.

Why

upsertMessage ran UPDATE messages SET thread_root_id=? WHERE chat_id=? AND thread_external_id=? AND thread_root_id IS NULL for every saved message. No index covers thread_external_id, so the plan is SEARCH 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

  • linkThreadChildren runs that link once per saveMessages call, for all the page's ids (json_each). The phone projector calls it for its one message.
  • No migration. The index that removes the remaining scan, (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:

stored before after
10k 78 ms 32 ms
30k 294 ms 37 ms
60k 593 ms 42 ms
90k 919 ms —

The "after" run was on a machine at load average 25. Full write-up to follow in cli-testing.

  • New assertion in 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.ts and refill.test.ts time out locally at that load with and without this change, so CI decides.

🤖 Generated with Claude Code

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
leemour merged commit a197da5 into main Nov 25, 2026
43 checks passed
@leemour
leemour deleted the fix/sync-thread-link-per-page branch November 25, 2026 01:34
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>
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