Repository navigation
fix(daemon): avoid FTS startup scans (#845) - #858
Conversation
Problem: daemon startup counted rows directly from the FTS5 virtual table before reporting liveness. On production archives that becomes an O(index) scan and can burn gigabytes of reads before convergence can resume. What changed: reuse the existing FTS docsize-backed status helper for startup gap checks and for post-rebuild logging, and add a daemon CLI regression test that rejects COUNT(*) on messages_fts while still proving rebuild is scheduled from cheap counts. Ref #845
|
Warning Rate limit exceeded
To continue reviewing without waiting, purchase usage credits in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR updates the daemon’s startup FTS readiness check to avoid expensive COUNT(*) scans over the FTS5 virtual table by using the existing docsize-backed FTS status helper, keeping daemon startup responsive on large archives.
Changes:
- Replace startup
COUNT(*) FROM messages_ftscalls withfts_index_status_sync()(docsize-based counting) in_ensure_fts_startup_readiness. - Add a focused unit test to ensure the startup check never issues
COUNT(*) FROM messages_ftsand still schedules rebuilds based on bounded counts.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
polylogue/daemon/cli.py |
Switch startup FTS coverage counting to fts_index_status_sync() and add a small helper to read the returned count safely. |
tests/unit/daemon/test_daemon_cli.py |
Add a unit test asserting the startup readiness check uses docsize counts and avoids COUNT(*) FROM messages_fts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| conn = open_connection(db, timeout=10.0) | ||
| total = conn.execute("SELECT COUNT(*) FROM messages").fetchone()[0] | ||
| if total == 0: | ||
| conn.close() | ||
| return | ||
| fts_count = conn.execute("SELECT COUNT(*) FROM messages_fts").fetchone()[0] | ||
| from polylogue.storage.fts.fts_lifecycle import fts_index_status_sync | ||
|
|
||
| fts_status = fts_index_status_sync(conn) | ||
| fts_count = _fts_status_count(fts_status) | ||
| gap = total - fts_count |
| def execute(self, sql: str, _params: object = ()) -> FakeCursor: | ||
| query = " ".join(sql.split()) | ||
| self.queries.append(query) | ||
| if query == "SELECT COUNT(*) FROM messages": | ||
| return FakeCursor((3,)) | ||
| if query == "SELECT name FROM sqlite_master WHERE type='table' AND name='messages_fts'": | ||
| return FakeCursor(("messages_fts",)) | ||
| if query == "SELECT COUNT(*) FROM messages_fts_docsize": | ||
| count = self.doc_counts.pop(0) if self.doc_counts else 3 | ||
| return FakeCursor((count,)) | ||
| if query == "SELECT name FROM sqlite_master WHERE type='table' AND name='action_events_fts'": | ||
| return FakeCursor(None) | ||
| raise AssertionError(f"unexpected query: {query}") |
Production deployment of #854 showed `polylogued` blocked before liveness inside `_ensure_fts_startup_readiness`. A py-spy trace showed the daemon inside the startup FTS check while `PRAGMA user_version` reported the archive had not yet reached later convergence/schema work; multiple GB had already been read because the check used `COUNT(*) FROM messages_fts`, which is O(N) on FTS5 virtual tables (the repo already documents that pattern). Follow-up to PR #854 after deployment exposed the regression. - `polylogue/daemon/cli.py`: replaces the direct `COUNT(*) FROM messages_fts` calls with the existing `fts_index_status_sync()` helper that counts `messages_fts_docsize` for both the initial gap check and the post-rebuild logging, keeping daemon startup bounded enough to report liveness before live convergence work resumes on large archives. - `tests/unit/daemon/test_daemon_cli.py`: focused test proves the startup check schedules a rebuild from cheap counts and never issues `COUNT(*) FROM messages_fts`. Refs #845
Closes the #817 FTS-bloat slice by removing four `session_*` FTS virtual tables and their twelve sync triggers that never had a live consumer after the session-insight rewrite. The tables shadowed `messages_fts` semantics on the session-profile rows, doubled write load on every profile rebuild, and contributed indirectly to the FTS startup-scan symptoms reported in #845/#858. - `polylogue/storage/sqlite/schema_ddl_insight_profiles.py`: removes the four FTS virtual tables and their triggers. - `storage/derived/derived_status.py`: removes the readiness shape that referenced the dropped tables. - `tests/unit/cli/test_insights.py`, `tests/unit/storage/test_backend.py`: tests updated to the new insight-profile shape. Net effect across the wider PR diff: 14 files +94/-396 lines. Refs #817
Production deployment of #854 showed `polylogued` blocked before liveness inside `_ensure_fts_startup_readiness`. A py-spy trace showed the daemon inside the startup FTS check while `PRAGMA user_version` reported the archive had not yet reached later convergence/schema work; multiple GB had already been read because the check used `COUNT(*) FROM messages_fts`, which is O(N) on FTS5 virtual tables (the repo already documents that pattern). Follow-up to PR #854 after deployment exposed the regression. - `polylogue/daemon/cli.py`: replaces the direct `COUNT(*) FROM messages_fts` calls with the existing `fts_index_status_sync()` helper that counts `messages_fts_docsize` for both the initial gap check and the post-rebuild logging, keeping daemon startup bounded enough to report liveness before live convergence work resumes on large archives. - `tests/unit/daemon/test_daemon_cli.py`: focused test proves the startup check schedules a rebuild from cheap counts and never issues `COUNT(*) FROM messages_fts`. Refs #845
Closes the #817 FTS-bloat slice by removing four `session_*` FTS virtual tables and their twelve sync triggers that never had a live consumer after the session-insight rewrite. The tables shadowed `messages_fts` semantics on the session-profile rows, doubled write load on every profile rebuild, and contributed indirectly to the FTS startup-scan symptoms reported in #845/#858. - `polylogue/storage/sqlite/schema_ddl_insight_profiles.py`: removes the four FTS virtual tables and their triggers. - `storage/derived/derived_status.py`: removes the readiness shape that referenced the dropped tables. - `tests/unit/cli/test_insights.py`, `tests/unit/storage/test_backend.py`: tests updated to the new insight-profile shape. Net effect across the wider PR diff: 14 files +94/-396 lines. Refs #817
Summary
Replace the daemon startup FTS coverage check's direct
COUNT(*) FROM messages_ftscalls with the existing docsize-backed FTS status helper. This keeps daemon startup bounded enough to report liveness before live convergence work resumes on large archives.Problem
Production deployment of #854 showed
polyloguedblocked before liveness in_ensure_fts_startup_readiness.py-spyshowed the daemon inside the startup FTS check, and the process had already read multiple GB whilePRAGMA user_versionstill showed the archive had not reached the later convergence/schema work. The repo already documents thatCOUNT(*)on FTS5 virtual tables is O(N); daemon startup had reintroduced that pattern.Solution
polylogue/daemon/cli.pynow usesfts_index_status_sync(), which countsmessages_fts_docsize, for both the initial gap check and post-rebuild logging. A focused daemon CLI test proves the startup check schedules a rebuild from cheap counts and never issuesCOUNT(*) FROM messages_fts.Verification
ruff format --check polylogue/daemon/cli.py tests/unit/daemon/test_daemon_cli.py->2 files already formattedruff check polylogue/daemon/cli.py tests/unit/daemon/test_daemon_cli.py->All checks passed!mypy --strict polylogue/daemon/cli.py tests/unit/daemon/test_daemon_cli.py->Success: no issues found in 2 source filespytest tests/unit/daemon/test_daemon_cli.py -q->12 passed in 11.41sdevtools verify->verify: all checks passedgit pushpre-push quick gate ->verify: all checks passedRef #845.