Skip to content

fix(daemon): bound FTS startup probes - #1443

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

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

Conversation

@Sinity

@Sinity Sinity commented May 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Keeps polylogued startup bounded by removing exact message FTS shadow-table counts and gap joins from the pre-listen readiness path. Startup still repairs cheap structural hazards: missing FTS tables, missing triggers, and completely empty message FTS when messages exist.

Problem

Live deployment of #1441 showed the daemon still scanning messages_fts_docsize before binding HTTP sockets. On the production archive, SELECT COUNT(*) FROM messages_fts_docsize took about 19s, the join probes exceeded 30s before manual termination, and the systemd service read ~860MB over 5s while reaching a 6G cgroup memory peak. That made restarts unobservable and hostile to the host.

Closes #1442.

Solution

  • Remove the pre-listen exact count and join-based gap repair from polylogue.daemon.cli._ensure_fts_startup_readiness.
  • Preserve startup recovery for cheap structural failures only.
  • Extend daemon CLI tests so the startup path rejects COUNT(*) FROM messages_fts_docsize and FTS docsize join probes.
  • Guard faulthandler.enable() for Click test streams that do not expose fileno().

Verification

  • pytest tests/unit/daemon/test_daemon_cli.py tests/unit/daemon/test_fts_trigger_drift.py tests/unit/daemon/test_fts_global_repair.py tests/unit/daemon/test_health_contract.py tests/unit/daemon/test_daemon_status.py tests/unit/storage/test_fts_trigger_lifecycle.py tests/unit/storage/test_fts_repair_sql.py tests/unit/storage/test_fts5.py tests/unit/storage/test_fts_bloat_invariants.py tests/unit/storage/test_archive_search_contracts.py -q → 154 passed in 3.80s
  • devtools verify --quick → exit_code 0, total_duration_s 45.42
  • pre-push devtools verify --quick → exit_code 0, total_duration_s 35.25

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced daemon startup fault handling with improved error tolerance for edge cases.
  • Refactor

    • Streamlined FTS health verification logic during daemon initialization for better performance.
    • Simplified startup readiness checks to reduce unnecessary workload during initialization.

Review Change Stack

Problem: live deployment showed polylogued still scanned messages_fts_docsize before binding health sockets. On the production archive a docsize count took about 19s and join probes ran beyond 30s, pushing the service into D-state and a 6G cgroup peak.\n\nWhat changed: the daemon startup path now only performs cheap structural recovery: missing FTS table, missing triggers, or completely empty message FTS while messages exist. Exact FTS freshness scans remain outside the pre-listen startup path. The run command also tolerates test/runner stderr streams without a fileno when enabling faulthandler.\n\nVerification: pytest tests/unit/daemon/test_daemon_cli.py tests/unit/daemon/test_fts_trigger_drift.py tests/unit/daemon/test_fts_global_repair.py tests/unit/daemon/test_health_contract.py tests/unit/daemon/test_daemon_status.py tests/unit/storage/test_fts_trigger_lifecycle.py tests/unit/storage/test_fts_repair_sql.py tests/unit/storage/test_fts5.py tests/unit/storage/test_fts_bloat_invariants.py tests/unit/storage/test_archive_search_contracts.py -q; devtools verify --quick.\n\nRef #1442
@coderabbitai

coderabbitai Bot commented May 23, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: bb181782-f7bb-4c46-849a-a7e2bf32c9ee

📥 Commits

Reviewing files that changed from the base of the PR and between 81024be and bc47214.

📒 Files selected for processing (2)
  • polylogue/daemon/cli.py
  • tests/unit/daemon/test_daemon_cli.py

📝 Walkthrough

Walkthrough

Daemon FTS startup is simplified to avoid expensive pre-listen shadow-table scans. The targeted structural mismatch repair logic and its heavy COUNT(*) queries are removed, keeping only cheap structural checks (missing tables, missing triggers, empty FTS). A new faulthandler safe-initialization helper prevents errors when stderr lacks a real file descriptor. Tests verify that the removed expensive query patterns are no longer executed.

Changes

FTS Startup Bounded Repair and Faulthandler Safe Initialization

Layer / File(s) Summary
FTS Startup Repair Simplification
polylogue/daemon/cli.py
Removes _STARTUP_FTS_TARGETED_REPAIR_LIMIT constant and three repair helpers (_missing_message_fts_conversation_ids_sync, _excess_message_fts_row_count_sync, _repair_message_fts_gap_sync). Eliminates the structural mismatch branch from _ensure_fts_startup_readiness() that compared conversation_stats vs messages_fts_docsize row counts. Updates docstring to emphasize bounded freshness checks and avoidance of heavy shadow-table workloads before health sockets are available.
Faulthandler Safe Initialization
polylogue/daemon/cli.py
Adds _enable_faulthandler_if_supported() helper that wraps faulthandler.enable() with error suppression for environments where stderr lacks a real file descriptor. Updates run_command to use the safe helper instead of unconditional enablement.
Test Alignment for FTS Startup Changes
tests/unit/daemon/test_daemon_cli.py
Removes unused Sequence import. Removes mock handlers from three test cases (test_ensure_fts_startup_readiness_bounded_probes, test_ensure_fts_startup_readiness_rebuilds_empty_fts, test_ensure_fts_startup_readiness_rebuilds_when_triggers_missing) for queries that are no longer executed: table/virtual-table existence checks via IN ('table', 'virtual table'), conversation_stats aggregation, and COUNT(*) FROM messages_fts_docsize. Adds assertions verifying that COUNT(*) and LEFT JOIN messages_fts_docsize patterns do not appear in startup execution paths.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Possibly related issues

  • fix(daemon): keep FTS startup repair bounded before serving #1442: This PR directly implements the acceptance criteria of issue #1442 by removing exact full-table FTS docsize counts and join scans from the pre-listen daemon startup path, keeping only cheap structural checks, and adding regression coverage to verify that startup does not issue expensive COUNT(*) or gap-detection queries.

Possibly related PRs

  • Sinity/polylogue#1434: Modifies _ensure_fts_startup_readiness() and its tests to replace unbounded COUNT(*)/row-count parity checks with bounded presence/probe-based SQL patterns.
  • Sinity/polylogue#1438: Updates FTS readiness/probing logic across daemon startup and polylogue status paths to avoid expensive COUNT(*)-style queries on messages_fts_docsize.
  • Sinity/polylogue#1431: Modifies daemon FTS readiness SQL probing to switch from counting the virtual messages_fts table toward messages_fts_docsize and updates tests for new query patterns.

Poem

🐰 The daemon startup hops with glee,
No heavy shadows counting free—
Just cheap checks, quick and light,
Health sockets fast in sight!
Fault-safe paws now guide the way,
Bounded bounces save the day. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 21.43% 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 'fix(daemon): bound FTS startup probes' clearly and concisely summarizes the main change—preventing expensive FTS probes from running during daemon startup.
Description check ✅ Passed The PR description follows the template with all required sections (Summary, Problem, Solution, Verification). It clearly explains what changed and why, and includes exact test commands and verification results.
Linked Issues check ✅ Passed Code changes directly address all key objectives from issue #1442: remove COUNT(*) from messages_fts_docsize and join-based gap repairs from pre-listen startup, retain cheap structural recovery, add test assertions rejecting expensive queries [#1442].
Out of Scope Changes check ✅ Passed Changes are scoped: CLI faulthandler guard is a complementary fix for Click test streams; test updates align with startup behavior changes; no unrelated modifications detected.

✏️ 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/fix/bounded-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.

@Sinity
Sinity merged commit 9b9e105 into master May 23, 2026
5 of 6 checks passed
@Sinity
Sinity deleted the feature/fix/bounded-fts-startup branch May 23, 2026 16:10

@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

restore_fts_triggers_sync(conn)
ensure_fts_index_sync(conn)
has_indexable_messages = bool(conn.execute("SELECT 1 FROM messages WHERE text IS NOT NULL LIMIT 1").fetchone())
has_indexed_messages = bool(conn.execute("SELECT 1 FROM messages_fts_docsize LIMIT 1").fetchone())

P1 Badge Rebuild index when trigger drift is detected

When startup detects missing FTS sync triggers, this path restores trigger DDL but performs no freshness repair unless the entire message FTS is empty. In the documented SIGKILL-during-bulk-suspend scenario, rows can be written while triggers are absent, leaving partial gaps in messages_fts_docsize; with a non-empty index, has_indexed_messages stays true and the daemon commits/serves stale search results indefinitely until an explicit repair is run. The removed mismatch reconciliation made this case self-healing, so this is a correctness regression for real archives that hit trigger drift.

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

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.

fix(daemon): keep FTS startup repair bounded before serving

1 participant