Repository navigation
fix(storage): FTS trigger commit ordering + SIGKILL safety (#1242) - #1362
Conversation
Two related bugs documented in #1242: Bug A — commit ordering. _process_ingest_batch_sync ran _commit_sync_ingest_side_effects in a `finally` block, AFTER the row commit completed. If the post-commit FTS repair raised, data was already durable but the FTS index drifted silently. Worse, the finally also ran after a rollback, silently restoring triggers on top of nothing. The fix moves the side-effects call into the try block, immediately after _flush_ingest_results, so trigger restore + FTS repair + commit land as one effects window through the archive write gateway. On exception the row writes are rolled back AND triggers are explicitly restored before re-raising, so the database is never left in the SIGKILL-style drift state from this code path. Removed the redundant restore + commit from the old _commit_ingest_results (renamed to _flush_ingest_results to match its new role). Bug B — SIGKILL during bulk suspend. A SIGKILL between suspend_fts_triggers_sync (DDL → auto-commits the drop) and the matching restore leaves all six FTS triggers absent across process death; subsequent writes silently bypass the FTS index. The daemon startup readiness check (_ensure_fts_startup_readiness) previously only rebuilt when the FTS table was missing or the docsize shadow was empty; it did not detect the SIGKILL-drift signature, where triggers are gone but the index still has stale rows from before the kill. The check now explicitly enumerates the six expected triggers and, on detected drift, restores them and rebuilds the FTS index in one startup-time recovery window. Regression tests in tests/unit/storage/test_fts_trigger_lifecycle.py: - test_ingest_side_effects_run_in_try_block_not_finally: AST-level structural assertion that _process_ingest_batch_sync calls the side-effects helper inside the try block and NOT in the finally block. Pins the structural fix against accidental reversion. - test_sigkill_mid_bulk_recovery_via_daemon_startup: simulates the SIGKILL-during-suspend signature (drop + commit + die), runs _ensure_fts_startup_readiness in a fresh "process", and asserts both trigger restore and FTS index rebuild happened. Closes #1242. Ref #817.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR fixes two related FTS (Full-Text Search) safety bugs: incorrect commit ordering in ingest batch transactions and undetected FTS trigger loss after SIGKILL-style terminations. The ingest batch refactoring moves FTS side effects before connection release to keep them atomic, while daemon startup now detects and restores missing triggers that may have been suspended before an interruption. ChangesFTS Trigger Safety & Recovery
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
✨ 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: 7042b2522d
ℹ️ 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".
|
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
Fix two related FTS trigger lifecycle bugs from #1242 (slice C of #817):
the post-write side effects ran AFTER the row commit in a
finallyblock, and a SIGKILL between trigger-suspend and trigger-restore left
the daemon with no FTS triggers across restarts.
Problem
Bug A — commit ordering.
_process_ingest_batch_syncran_commit_sync_ingest_side_effectsin afinallyblock, so thepost-commit FTS repair landed in a separate transaction AFTER the row
commit. If the repair raised, data was already durable but the FTS
index drifted silently. The same
finallyalso fired after aconn.rollback(), silently restoring triggers on top of nothing andmasking the original exception in some cases.
Bug B — SIGKILL during bulk suspend.
suspend_fts_triggers_syncisDROP TRIGGER ..., which is DDL andauto-commits the drop. A SIGKILL between the drop and the matching
restore_fts_triggers_syncleaves all six FTS triggers absent acrossprocess death, and subsequent writes silently bypass the FTS index.
The daemon startup readiness check rebuilt only when the FTS table was
missing or the docsize shadow was empty — it did not detect this
"triggers gone, stale rows still present" drift signature.
Solution
polylogue/pipeline/services/ingest_batch/_core.py:_commit_ingest_results→_flush_ingest_results; it nowonly flushes pending conversation entries and does not commit.
The earlier in-function
restore_fts_triggers_sync+conn.commit()is removed because the gateway path below performs both atomically.
_process_ingest_batch_sync, moved_commit_sync_ingest_side_effectsout of thefinallyblock andinto the try body, immediately after
_flush_ingest_results.This collapses trigger restore + per-conversation FTS repair +
commit into one effects window through
ArchiveWriteGateway.commit_write_sync→commit_archive_write_effects.restore_fts_triggers_sync+conn.commit()before re-raising, sothe database is never left in the SIGKILL-drift state from this
code path.
polylogue/daemon/cli.py:_ensure_fts_startup_readinessnow enumerates the six canonicalFTS triggers and, on detected drift (the SIGKILL-during-suspend
signature), restores triggers and rebuilds the FTS index in one
startup-time recovery window. Existing FTS-table-missing and
empty-while-has-messages branches are preserved.
sqlite3to a module-level import and replaced theinline
__import__("contextlib")workaround with the module-levelcontextlibthat already exists.tests/unit/storage/test_fts_trigger_lifecycle.py:test_ingest_side_effects_run_in_try_block_not_finally— AST-levelstructural assertion that pins the commit-ordering fix against
accidental reversion.
test_sigkill_mid_bulk_recovery_via_daemon_startup— simulatesdrop + commit + die, runs the daemon startup readiness check in a
fresh process, asserts trigger restore and FTS rebuild happened.
tests/unit/daemon/test_daemon_cli.py:FakeCursor.fetchall()andhandle the new
sqlite_master WHERE type='trigger'probe with allsix expected triggers present (steady-state path).
Verification
Closes #1242. Ref #817.
Summary by CodeRabbit
Bug Fixes
Tests