Skip to content

fix(daemon): avoid FTS startup scans (#845) - #858

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/daemon-fts-startup
May 6, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/daemon-fts-startup

Conversation

@Sinity

@Sinity Sinity commented May 6, 2026

Copy link
Copy Markdown
Owner

Summary

Replace the daemon startup FTS coverage check's direct COUNT(*) FROM messages_fts calls 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 polylogued blocked before liveness in _ensure_fts_startup_readiness. py-spy showed the daemon inside the startup FTS check, and the process had already read multiple GB while PRAGMA user_version still showed the archive had not reached the later convergence/schema work. The repo already documents that COUNT(*) on FTS5 virtual tables is O(N); daemon startup had reintroduced that pattern.

Solution

polylogue/daemon/cli.py now uses fts_index_status_sync(), which counts messages_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 issues COUNT(*) FROM messages_fts.

Verification

  • ruff format --check polylogue/daemon/cli.py tests/unit/daemon/test_daemon_cli.py -> 2 files already formatted
  • ruff 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 files
  • pytest tests/unit/daemon/test_daemon_cli.py -q -> 12 passed in 11.41s
  • devtools verify -> verify: all checks passed
  • git push pre-push quick gate -> verify: all checks passed

Ref #845.

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
Copilot AI review requested due to automatic review settings May 6, 2026 22:11
@Sinity
Sinity merged commit fbf1197 into master May 6, 2026
3 checks passed
@Sinity
Sinity deleted the feature/fix/daemon-fts-startup branch May 6, 2026 22:11
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 32 minutes and 46 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8c218801-f78b-4dfd-8ed6-e01b8b6b35bf

📥 Commits

Reviewing files that changed from the base of the PR and between a6c48e0 and 456af93.

📒 Files selected for processing (2)
  • polylogue/daemon/cli.py
  • tests/unit/daemon/test_daemon_cli.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/daemon-fts-startup

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_fts calls with fts_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_fts and 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.

Comment thread polylogue/daemon/cli.py
Comment on lines 86 to 95
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
Comment on lines +202 to +214
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}")
Sinity added a commit that referenced this pull request May 7, 2026
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
Sinity added a commit that referenced this pull request May 7, 2026
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
Sinity added a commit that referenced this pull request May 7, 2026
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
Sinity added a commit that referenced this pull request May 7, 2026
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
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