Skip to content

perf(daemon): avoid virtual FTS status counts - #1431

Merged
Sinity merged 2 commits into
masterfrom
feature/perf/status-catchup-followup
May 23, 2026
Merged

Sinity merged 2 commits into
masterfrom
feature/perf/status-catchup-followup

Conversation

@Sinity

@Sinity Sinity commented May 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Use FTS5 docsize shadow tables for daemon status, health, and convergence readiness counts, and fix startup trigger-drift recovery so missing FTS sync triggers cause a rebuild instead of restore-only.

Problem

After deploying #1430, polylogue --plain status still timed out against the real archive. The direct CLI fallback had already been moved to messages_fts_docsize, but /api/status still called daemon health, whose medium FTS readiness check used COUNT(*) FROM messages_fts. The same virtual-table count pattern also remained in periodic convergence and global FTS-stage fallback paths.

Live inspection also showed all six production FTS sync triggers were absent and docsize counts had drifted above source table counts. Startup intended to repair this, but it called ensure_fts_index_sync() before checking for missing triggers; that recreated triggers and erased the evidence before the rebuild branch ran.

Solution

polylogue/daemon/health.py now uses messages_fts_docsize for FTS row counts and treats a missing docsize table as degraded. polylogue/daemon/convergence_stages.py and polylogue/daemon/cli.py use the same shadow-table count for global FTS readiness/rebuild checks.

Startup readiness now checks for missing FTS triggers before calling ensure_fts_index_sync(). When trigger drift is found, it restores triggers and rebuilds FTS in the same startup pass.

Verification

  • pytest tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_convergence_stages.py -q --tb=short → 35 passed
  • 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_health_check_paths.py tests/unit/daemon/test_convergence_stages.py -q --tb=short → 38 passed
  • mypy polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py → Success
  • ruff format --check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py → passed
  • ruff check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py → passed
  • timeout 10 python - <<'PY' ... daemon_status_payload() ... PY against the real archive → returned successfully
  • pre-push devtools verify --quick → pass, 35.11s on latest commit

Use FTS5 docsize shadow tables for daemon health and convergence readiness counts so production status paths do not scan the virtual FTS table on multi-million-message archives.\n\nVerification:\n- pytest tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_convergence_stages.py -q --tb=short\n- mypy polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py\n- ruff format --check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py\n- ruff check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py\n- timeout 10 python - <<'PY' ... daemon_status_payload() ... PY
@coderabbitai

coderabbitai Bot commented May 23, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR changes how the daemon measures FTS (Full-Text Search) index coverage across convergence stages, CLI periodic checks, and health monitoring. Instead of counting rows in FTS virtual tables (messages_fts, action_events_fts), the code now counts rows in FTS docsize metadata tables (messages_fts_docsize, action_events_fts_docsize), with a new helper function enabling safe fallback to zero when those metadata tables are absent.

Changes

FTS docsize measurement across daemon checks

Layer / File(s) Summary
FTS docsize helper and convergence stage integration
polylogue/daemon/convergence_stages.py
A new _fts_doc_count(conn, table) helper safely counts rows from FTS docsize tables when they exist, returning 0 otherwise. The FTS stage's coverage check and post-rebuild verification both use this helper to measure indexed completeness via messages_fts_docsize and action_events_fts_docsize instead of the FTS virtual tables.
CLI periodic convergence check update
polylogue/daemon/cli.py
The periodic convergence checker now counts messages_fts_docsize rows to compute FTS coverage, falling back to 0 when the docsize table is absent.
Health readiness check refactored for docsize tables
polylogue/daemon/health.py
The FTS readiness health check consolidates table-existence detection via sqlite_master and derives the FTS indexed count from messages_fts_docsize when present. Missing-table alert logic now distinguishes missing messages_fts_docsize from other missing FTS tables.
Test validating docsize table usage in health checks
tests/unit/daemon/test_health_check_paths.py
A new test monkeypatches SQLite connection to verify the FTS readiness check counts from messages_fts_docsize instead of directly querying virtual table messages_fts. The test fails if the forbidden COUNT(*) from messages_fts pattern appears.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Sinity/polylogue#1035: Both PRs update polylogue/daemon/convergence_stages.py to change how the daemon reasons about FTS state for action events—main PR switches coverage/recheck counts to *_fts_docsize, while the retrieved PR switches the repair decision probe from action_events_fts to the indexed action_events base table.

Poem

🐰 Docsize tables, oh so wise,
Metadata gardens, a pleasant surprise,
Virtual tables fade from view,
FTS coverage shines brand new! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: replacing direct FTS virtual table counts with docsize shadow table counts for performance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description comprehensively covers all required template sections with clear context, implementation details, and verification results.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/perf/status-catchup-followup

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.

Check for missing FTS triggers before ensure_fts_index_sync recreates them. Otherwise startup can restore the trigger DDL but skip the rebuild needed after writes landed while sync triggers were absent.\n\nVerification:\n- 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_health_check_paths.py tests/unit/daemon/test_convergence_stages.py -q --tb=short\n- mypy polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py\n- ruff format --check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py\n- ruff check polylogue/daemon/health.py polylogue/daemon/convergence_stages.py polylogue/daemon/cli.py tests/unit/daemon/test_health_check_paths.py tests/unit/daemon/test_daemon_cli.py
@Sinity
Sinity merged commit a148aee into master May 23, 2026
5 checks passed
@Sinity
Sinity deleted the feature/perf/status-catchup-followup branch May 23, 2026 10:40

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dfc1ecc2e0

ℹ️ 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".

Comment thread polylogue/daemon/convergence_stages.py Resolved
Sinity added a commit that referenced this pull request May 23, 2026
Summary

Fix daemon FTS readiness and repair when FTS docsize shadow tables contain orphan rows, not only missing rows. Startup, periodic convergence, and daemon status now treat source/docsize count inequality in either direction as drift.

Problem

The live archive after #1431 had restored triggers but still showed docsize overcounts: messages_fts_docsize and action_events_fts_docsize exceeded their source tables by hundreds of thousands of rows. The old repair predicates only rebuilt on undercounts, so overcount corruption was reported as healthy.

Solution

The daemon now compares message FTS against indexable message rows and action FTS against action events, rebuilding on mismatch. FTS status projection moved into a dedicated module with explicit count and coverage fields, and CLI status renders those fields instead of inferring a missing percentage. A regression test recreates orphan docsize rows by deleting source rows while the delete trigger is absent.

Verification

- pytest focused daemon/status suite: 27 passed
- mypy focused daemon/status suite: success
- ruff format/check focused files: passed
- devtools verify-file-budgets: clean
- devtools render-all --check: passed
- devtools verify-topology: passed
- pre-push devtools verify --quick: passed, 36.52s
- PR checks: CodeQL, containers, Pages Preview, GitGuardian passed
@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

  • 63.F046 / P2: stale at the checked revision: The former action-event FTS convergence branch has been retired with that separate search surface. Current FTS composition uses the searchable-block adapter, whose only expected surface is messages_fts at polylogue/storage/fts/derivation.py:170-178; it does not condition action repair on an action_events_fts_docsize table.

Only the rows listed above are dispositioned. Held findings remain open.

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