Repository navigation
perf(daemon): bound startup FTS checks - #1434
Conversation
Problem: daemon restart was paying full-archive FTS count scans before writing its pidfile, which left status reporting stale pidfiles and kept the host in IO pressure during ordinary restarts. The idle convergence loop also repeated global FTS count/rebuild checks instead of relying on per-source convergence debt. What changed: acquire the pidfile before startup FTS readiness, replace startup FTS parity counts with bounded existence probes, use conversation_stats for daemon FTS status source counts, and remove global FTS rebuild scans from the periodic idle convergence loop. Per-source and per-conversation convergence repair remains in the daemon path. Verification: ruff format/check on touched files; focused daemon pytest nodes; focused mypy on touched files.
📝 WalkthroughWalkthroughThis PR optimizes FTS readiness monitoring by replacing expensive full-table COUNT(*) scans with lightweight presence checks and stats-based accounting. Startup readiness detection now uses bounded LIMIT 1 queries; message indexable counts come from conversation_stats aggregates; the readiness check is repositioned after pidfile acquisition; and periodic maintenance shifts to convergence debt retry logic. ChangesLightweight FTS Readiness and Indexing Monitoring
🎯 3 (Moderate) | ⏱️ ~18 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bf0d8538af
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/unit/daemon/test_daemon_status.py`:
- Around line 314-327: The test currently only asserts a negative condition on
the traced SQL queries which will vacuously pass if no queries were captured;
update the test around traced_connect/queries/_fts_readiness_info to first
assert that queries captured is non-empty (e.g., assert queries) and then
perform normalized matching (lowercase and strip whitespace) when checking that
no query contains the pattern "count(*) from messages where"; locate the
traced_connect helper and the queries list used in the with-patch block and add
the positive assertion plus use normalized query strings for the negative
membership check against the COUNT(*) pattern.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: ad1bfafc-e123-4e62-a778-b1ffd62f8bc2
📒 Files selected for processing (4)
polylogue/daemon/cli.pypolylogue/daemon/fts_status.pytests/unit/daemon/test_daemon_cli.pytests/unit/daemon/test_daemon_status.py
| def traced_connect(*args: Any, **kwargs: Any) -> sqlite3.Connection: | ||
| conn = original_connect(*args, **kwargs) | ||
| conn.set_trace_callback(queries.append) | ||
| return cast(sqlite3.Connection, conn) | ||
|
|
||
| with ( | ||
| patch("polylogue.daemon.status.db_path", return_value=db), | ||
| patch("polylogue.storage.sqlite.connection_profile.sqlite3.connect", side_effect=traced_connect), | ||
| ): | ||
| readiness = status_module._fts_readiness_info() | ||
|
|
||
| assert readiness["message_indexable_count"] == 5 | ||
| assert readiness["message_indexed_count"] == 5 | ||
| assert all("COUNT(*) FROM messages WHERE" not in query for query in queries) |
There was a problem hiding this comment.
Strengthen the SQL-trace assertion to avoid vacuous pass.
If trace capture ever breaks, the current negative assertion still passes with an empty queries list. Add a positive capture assertion and normalize SQL before matching.
Proposed fix
def test_daemon_status_fts_readiness_uses_stats_not_message_scan(tmp_path: Path) -> None:
@@
with (
patch("polylogue.daemon.status.db_path", return_value=db),
patch("polylogue.storage.sqlite.connection_profile.sqlite3.connect", side_effect=traced_connect),
):
readiness = status_module._fts_readiness_info()
assert readiness["message_indexable_count"] == 5
assert readiness["message_indexed_count"] == 5
- assert all("COUNT(*) FROM messages WHERE" not in query for query in queries)
+ assert queries
+ normalized_queries = [" ".join(query.upper().split()) for query in queries]
+ assert any("FROM CONVERSATION_STATS" in query for query in normalized_queries)
+ assert all("COUNT(*) FROM MESSAGES WHERE" not in query for query in normalized_queries)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@tests/unit/daemon/test_daemon_status.py` around lines 314 - 327, The test
currently only asserts a negative condition on the traced SQL queries which will
vacuously pass if no queries were captured; update the test around
traced_connect/queries/_fts_readiness_info to first assert that queries captured
is non-empty (e.g., assert queries) and then perform normalized matching
(lowercase and strip whitespace) when checking that no query contains the
pattern "count(*) from messages where"; locate the traced_connect helper and the
queries list used in the with-patch block and add the positive assertion plus
use normalized query strings for the negative membership check against the
COUNT(*) pattern.
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
Bounds daemon restart/status FTS probes so ordinary restarts do not scan the full
messagestable or rebuild FTS from the idle convergence loop. The daemon now writes its pidfile before startup FTS readiness and uses existence probes for startup health; status derives source message counts fromconversation_statsinstead ofCOUNT(*) FROM messages WHERE text IS NOT NULL.Problem
While deploying #1433 to the real archive,
polyloguedspent about 59 seconds before logging the FTS mismatch and about 10 minutes rebuilding FTS before the watcher started. During that window it had no fresh pidfile, sopolylogue statusreported a stale pidfile for the old process. The expensive startup path was caused by full-table FTS parity counts on a multi-million-row archive, and the idle convergence loop still had another global count/rebuild path.Solution
SELECT 1 ... LIMIT 1probes that only rebuild the historical empty-index case._periodic_convergence_check; per-source and per-conversation convergence repair remains intact.conversation_statsfor daemon FTS status source counts so status avoids scanningmessages.Verification
pytest tests/unit/daemon/test_daemon_cli.py::test_ensure_fts_startup_readiness_uses_bounded_probes tests/unit/daemon/test_daemon_cli.py::test_ensure_fts_startup_readiness_rebuilds_empty_fts tests/unit/daemon/test_daemon_cli.py::test_ensure_fts_startup_readiness_rebuilds_when_triggers_missing tests/unit/daemon/test_daemon_status.py::test_daemon_status_fts_readiness_uses_stats_not_message_scan tests/unit/daemon/test_daemon_status.py::test_daemon_status_fts_readiness_uses_lightweight_table_probe -q→ 5 passedpython -m mypy polylogue/daemon/cli.py polylogue/daemon/fts_status.py tests/unit/daemon/test_daemon_cli.py tests/unit/daemon/test_daemon_status.py→ successruff format ... && ruff check ...→ all checks passeddevtools render-all --check→ OKdevtools verify --quick→ exit_code 0, total_duration_s 34.75Summary by CodeRabbit