Skip to content

fix(storage): prevent FTS orphans during replacement - #1435

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

Sinity merged 1 commit into
masterfrom
feature/fix/fts-delete-consistency

Conversation

@Sinity

@Sinity Sinity commented May 23, 2026

Copy link
Copy Markdown
Owner

Summary

Makes FTS correctness real across daemon catch-up/replacement. Conversation replacement now explicitly removes message/action FTS rows before deleting base rows when FTS delete triggers are missing, and daemon startup restores full FTS parity using cheap stats-table counts before the watcher proceeds.

Problem

Production drift showed messages_fts_docsize overcounting the base messages table by about 108k rows with rowids that no longer exist in messages. That shape is consistent with deleting/replacing conversation rows while FTS delete triggers are suspended or missing: the base rows disappear, but external-content FTS shadow rows remain. Per-conversation repair then cannot remove those rowids because it only sees current messages rows.

FTS must be transparently ready and correct; stale search is not an acceptable steady state.

Solution

  • Add replacement-time FTS purges for message and action-event rows when their delete triggers are absent.
  • Purges use the FTS5 external-content delete command while the old base rows still exist, so no orphan shadow rows are left behind when messages / action_events are deleted.
  • Keep normal trigger-driven deletes untouched when delete triggers are present, avoiding double deletes.
  • Startup readiness still rebuilds on FTS count mismatch, but compares messages_fts_docsize to conversation_stats instead of scanning the full messages table.

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_stats_count_mismatch tests/unit/daemon/test_daemon_cli.py::test_ensure_fts_startup_readiness_rebuilds_when_triggers_missing tests/unit/storage/test_fts_bloat_invariants.py::test_conversation_replacement_purges_fts_when_delete_triggers_missing tests/unit/storage/test_fts_bloat_invariants.py::test_messages_fts_deletion_consistency_with_external_content -q → 6 passed
  • python -m mypy polylogue/daemon/cli.py polylogue/storage/conversation_replacement.py tests/unit/daemon/test_daemon_cli.py tests/unit/storage/test_fts_bloat_invariants.py → success
  • ruff format ... && ruff check ... → all checks passed
  • devtools render-all --check → OK
  • pre-push devtools verify --quick → exit_code 0, total_duration_s 35.58

Problem: production FTS drift contained orphan docsize rows with no matching messages rows. The replacement path can delete messages while FTS delete triggers are suspended during bulk ingest, leaving external-content FTS rowids behind. Subsequent per-conversation repair only sees current message rowids and cannot delete already-orphaned rows.

What changed: conversation replacement now explicitly issues FTS5 delete commands for message and action-event rows before deleting base rows when the delete triggers are absent. Startup readiness still enforces real FTS parity, but uses conversation_stats rather than a full messages scan before rebuilding.

Verification: focused FTS replacement and daemon startup pytest nodes passed; focused mypy passed; ruff format/check passed.
@coderabbitai

coderabbitai Bot commented May 23, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, we couldn't start this review because you've used your available PR reviews for now.

Your plan currently allows 1 review/hour. Refill in 42 minutes and 2 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, 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 trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 7c9c487b-d916-48ea-96f3-5a0e14db75eb

📥 Commits

Reviewing files that changed from the base of the PR and between 4cab346 and 59cd56f.

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

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 f0219c6 into master May 23, 2026
6 checks passed
@Sinity
Sinity deleted the feature/fix/fts-delete-consistency branch May 23, 2026 12:02

@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: 59cd56f93b

ℹ️ 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/cli.py Resolved
@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.

  • 62.F029 / P1: fixed-already at the checked revision: The current FTS adapter counts searchable blocks, not all conversation messages: polylogue/storage/fts/derivation.py:166 uses blocks WHERE search_text != ''. Startup composes that adapter at polylogue/daemon/cli.py:2780, so null-text message counts cannot create the former permanent denominator mismatch.

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

@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.

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