Skip to content

fix(daemon): reduce large ingest FTS amplification - #1453

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/large-ingest-fts-amplification
May 23, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/large-ingest-fts-amplification

Conversation

@Sinity

@Sinity Sinity commented May 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Reduce the remaining live catch-up write amplification for huge single-session files. Live append/full-ingest paths now avoid redundant message FTS repair when active triggers already maintained FTS rows, while large full-ingest batches deliberately suspend triggers inside the ingest transaction and repair the changed conversations once before commit.

Problem

After #1452 bounded catch-up into file chunks, live validation still showed a single 221 MB Codex session with 31,417 messages spending 314 seconds in slow_write, driving severe host IO pressure. The batch was bounded, but the single-conversation write path was still monolithic and FTS-heavy.

The code also had a latent suspended-trigger transaction bug: _process_ingest_batch_sync dropped FTS triggers before BEGIN IMMEDIATE, so a suspended bulk path could fail with sqlite3.OperationalError: cannot start a transaction within a transaction.

Solution

  • Add a repair_message_fts switch to the canonical archive write effects path, matching the existing repair_action_fts control.
  • Keep conservative defaults for ordinary archive writes.
  • Make live append ingest opt out of redundant message/action FTS repair because FTS triggers are active for those row writes.
  • Make live full ingest suspend FTS triggers for raw records at or above 32 MiB, then force message/action FTS repair once before commit.
  • Move sync trigger suspension inside BEGIN IMMEDIATE and add mark_stale=False for transaction-scoped suspension so an already-ready freshness ledger is not falsely degraded by a transaction that repairs before commit.

Synthetic local probe on a temporary archive with a 12k-message conversation:

  • active triggers + post-write message repair: 0.292s
  • suspended triggers + one message repair: 0.143s

This does not claim the production 31k-message case is solved completely; it removes a known FTS write-amplification layer and fixes the transaction bug. The next live daemon run should verify residual parse/write/insight cost against the real 221 MB session.

Verification

  • pytest tests/unit/archive/test_write_gateway.py tests/unit/sources/test_live_batch_support.py tests/unit/pipeline/test_ingest_batch.py tests/unit/storage/test_fts_trigger_lifecycle.py tests/unit/storage/test_fts_bloat_invariants.py -q → 51 passed in 1.16s
  • python -m mypy polylogue/archive/write_effects.py polylogue/pipeline/services/ingest_batch/_core.py polylogue/sources/live/batch.py polylogue/sources/live/append_ingest.py polylogue/storage/fts/fts_lifecycle.py tests/unit/archive/test_write_gateway.py tests/unit/sources/test_live_batch_support.py → Success: no issues found in 7 source files
  • devtools verify --quick → PASS, exit_code: 0, total_duration_s: 34.63
  • pre-push devtools verify --quick → PASS, exit_code: 0, total_duration_s: 35.15

devtools verify reached pytest-testmon and was stopped after 785.8s because the affected selector expanded to effectively the full serial suite (9297 JUnit testcases recorded, failures=0, skipped=2) and was still running at 99%. That behavior is the existing verification-efficiency problem being tracked separately; I did not restart another broad pytest loop for this small branch.

Ref #1447.

Summary by CodeRabbit

  • Performance Improvements

    • Optimized full-text search maintenance during large file imports to reduce processing overhead.
  • Bug Fixes

    • Enhanced consistency in search index repair sequencing during data ingestion operations.
  • Tests

    • Added tests to verify correct behavior for skip-repair scenarios and large file import optimization.

Review Change Stack

Large live catch-up files were still paying per-row FTS trigger work and, for some paths, post-write conversation repair over the same changed rows. That kept one 31k-message Codex session in a long SQLite write window and continued to drive heavy IO pressure after file-batch chunking landed.

This keeps the conservative archive write default, but lets live append/full-ingest paths distinguish trigger-maintained rows from explicit repair. Large full-ingest batches now suspend FTS triggers inside the ingest transaction, write rows, restore triggers, and repair the changed conversations once before commit. The suspended path no longer starts outside BEGIN, avoiding the existing transaction error and keeping rollback atomic.

Verification: pytest tests/unit/archive/test_write_gateway.py tests/unit/sources/test_live_batch_support.py tests/unit/pipeline/test_ingest_batch.py tests/unit/storage/test_fts_trigger_lifecycle.py tests/unit/storage/test_fts_bloat_invariants.py -q; python -m mypy polylogue/archive/write_effects.py polylogue/pipeline/services/ingest_batch/_core.py polylogue/sources/live/batch.py polylogue/sources/live/append_ingest.py polylogue/storage/fts/fts_lifecycle.py tests/unit/archive/test_write_gateway.py tests/unit/sources/test_live_batch_support.py; devtools verify --quick.
@coderabbitai

coderabbitai Bot commented May 23, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR adds independent control of full-text search repair behavior across the ingestion pipeline. FTS trigger suspension now supports optional stale-marking, archive write effects split message and action FTS repair into separate gates, ingest core orchestrates repairs based on suspension state, and full/append ingests use distinct strategies for when to repair.

Changes

FTS Repair Control During Ingestion

Layer / File(s) Summary
FTS trigger suspension with optional stale-marking
polylogue/storage/fts/fts_lifecycle.py
suspend_fts_triggers_async and suspend_fts_triggers_sync accept a new mark_stale kw-only parameter (default True). When disabled, the stale-marking step is skipped, allowing callers to defer state changes during large transactions.
Archive write effects independent repair gates
polylogue/archive/write_effects.py, tests/unit/archive/test_write_gateway.py
commit_archive_write_effects splits message and action FTS repair into two independent conditionals, each gated by both sorted IDs and its respective repair flag. A new test verifies repair calls are skipped when both flags are disabled.
Ingest core FTS repair orchestration
polylogue/pipeline/services/ingest_batch/_core.py
_process_ingest_batch_sync and _commit_sync_ingest_side_effects gain repair_message_fts parameter. When suspend_fts_triggers is active, both repair flags are forced to true to ensure indexes are restored post-transaction. Transaction ordering is changed to begin BEGIN IMMEDIATE before suspending FTS triggers, and suspension uses mark_stale=False.
Full ingest large-file FTS suspension strategy
polylogue/sources/live/batch.py, tests/unit/sources/test_live_batch_support.py
Full ingests compute suspend_fts_triggers based on a 32 MB blob-size threshold. When any record exceeds the threshold, triggers are suspended and repairs are forced post-commit, avoiding index thrashing. A test verifies suspension and repairs are triggered for large single-file ingests.
Append ingest message repair disablement
polylogue/sources/live/append_ingest.py, tests/unit/sources/test_live_batch_support.py
Append ingests explicitly pass repair_message_fts=False to _process_ingest_batch_sync. An updated test verifies both message and action repair flags are set to False in append-ingest side effects.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Sinity/polylogue#1362: Both PRs modify _process_ingest_batch_sync and FTS trigger/repair orchestration in the ingest core, with this PR adding independent message/action repair gating.
  • Sinity/polylogue#882: Both PRs adjust FTS trigger suspension and repair sequencing around the database commit in polylogue/pipeline/services/ingest_batch/_core.py.

Poem

🐰 A rabbit hops through FTS trees,
Repair gates now dance with ease,
Large files suspend, then fix anew,
While appends skip what they need not do—
Full-text search, now finely tuned!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 41.18% 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): reduce large ingest FTS amplification' is concise and directly summarizes the primary change: reducing FTS write amplification in large live ingest operations.
Description check ✅ Passed The pull request description thoroughly covers all required template sections: Summary (with why it matters), Problem (business case and transaction bug), Solution (detailed approach with specific changes), Verification (exact commands and results), Changelog (no user-visible changes), and Risks (acknowledged next steps).
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.

✏️ 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/large-ingest-fts-amplification

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.

@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: f75e3e001e

ℹ️ 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/sources/live/batch.py Resolved
@Sinity
Sinity merged commit 6c826cf into master May 23, 2026
5 of 6 checks passed
@Sinity
Sinity deleted the feature/fix/large-ingest-fts-amplification branch May 23, 2026 21:42
@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.

  • 70.F049 / P1: stale at the checked revision: The removed session-work-event and work-thread FTS surfaces are no longer part of the suspended trigger set. polylogue/storage/fts/fts_lifecycle.py:81 defines the entire set as the block-backed message triggers, and the bulk-ingest recovery calls restore_fts_triggers_sync at polylogue/pipeline/services/ingest_batch/_core.py:3728.

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