Skip to content

perf(daemon): update append stats incrementally - #1470

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/incremental-append-stats
May 24, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/incremental-append-stats

Conversation

@Sinity

@Sinity Sinity commented May 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Update daemon append writes so conversation stats are maintained from changed tail-message deltas instead of recounting every message in the conversation after each append.

Problem

After #1469 was deployed, live catch-up still produced severe physical reads even though source payload reads were tiny, FTS freshness rows were ready, and hot insight rebuilds were being deferred. The remaining broad read was the append write path: _append_conversation always called _upsert_stats_from_messages, which scans the full messages set for the active conversation after every tiny append.

Live evidence from the deployed daemon:

  • catch-up scanned 11886 files and needed 309 files / 508.6 MB;
  • FTS freshness rows were ready for the live surfaces;
  • /proc/$pid/io over 10s still showed 1,440,432,128 read bytes;
  • recent attempts recorded tiny source_payload_read_bytes while input_bytes repeated huge source-file sizes.

Solution

  • Add polylogue/pipeline/services/ingest_batch/_append_stats.py for append-only stats helpers.
  • Reuse existing message signatures to compare content hashes and carry old stat counters.
  • Apply deltas only for changed tail messages during append writes.
  • Fall back to the full recount only when the conversation_stats row is missing, including unchanged append replays that need stats repair.
  • Keep non-append writes on the existing full stats path.

Ref #1447

Verification

  • pytest -q tests/unit/pipeline/test_ingest_append_replay.py tests/unit/pipeline/test_ingest_batch.py -> 29 passed in 0.24s
  • mypy polylogue/pipeline/services/ingest_batch/_core.py polylogue/pipeline/services/ingest_batch/_append_stats.py tests/unit/pipeline/test_ingest_append_replay.py -> Success: no issues found in 3 source files
  • devtools verify --quick -> exit_code 0, total_duration_s 38.25
  • pre-push devtools verify --quick -> exit_code 0, total_duration_s 36.11

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved conversation statistics calculation during batch append operations to accurately handle incremental updates.
  • Tests

    • Added regression tests to verify proper statistics handling in append-mode operations and recovery of missing statistics during batch processing.
  • Documentation

    • Updated internal topology documentation to reflect structural changes.

Review Change Stack

Live catch-up after the FTS hot-path fix still showed severe physical reads while source payload reads stayed tiny and FTS freshness rows were ready. The remaining append path always ran a full conversation_stats recount after each tiny live tail append, forcing large active conversations back through a broad messages scan.\n\nMove append stats maintenance into a focused helper that reuses existing message signatures, applies deltas for changed tail messages, and falls back to the full recount only when the stats row is missing. Preserve missing-stats repair for unchanged append replays while keeping non-append writes on the existing full path.\n\nFocused verification:\n- pytest -q tests/unit/pipeline/test_ingest_append_replay.py tests/unit/pipeline/test_ingest_batch.py -> 29 passed\n- mypy polylogue/pipeline/services/ingest_batch/_core.py polylogue/pipeline/services/ingest_batch/_append_stats.py tests/unit/pipeline/test_ingest_append_replay.py -> success\n- devtools verify --quick -> exit_code 0\n\nRef #1447
@coderabbitai

coderabbitai Bot commented May 24, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

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: 9020a769-1c80-4158-ade1-d1eebc687cf8

📥 Commits

Reviewing files that changed from the base of the PR and between 1478cc1 and 65d916c.

📒 Files selected for processing (5)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/pipeline/services/ingest_batch/_append_stats.py
  • polylogue/pipeline/services/ingest_batch/_core.py
  • tests/unit/pipeline/test_ingest_append_replay.py

📝 Walkthrough

Walkthrough

This PR refactors append-mode conversation statistics updates in the ingest batch pipeline. A new _append_stats module extracts message signature detection and delta-based stats computation; _core.py is refactored to use these helpers, and tests validate incremental updates and missing-stats repair scenarios.

Changes

Append-mode Conversation Stats Refactoring

Layer / File(s) Summary
New append_stats module with signature and delta helpers
polylogue/pipeline/services/ingest_batch/_append_stats.py
existing_message_signatures queries per-message stats (content hash, word count, flags) in chunked IN (...) queries; upsert_stats_for_append either invokes a full-recount callback when conversation_stats is missing or computes per-counter deltas from changed_messages vs existing_messages and applies them via additive UPDATE.
Core append logic delegates to append_stats helpers
polylogue/pipeline/services/ingest_batch/_core.py
Imports the new helpers, removes local _existing_message_hashes, refactors changed_messages detection to use existing_message_signatures, and updates both the early-return and post-append stats paths to call upsert_stats_for_append instead of inline logic.
Test coverage for incremental stats and repair scenarios
tests/unit/pipeline/test_ingest_append_replay.py
Extends the existing append-only replay test to query and assert conversation_stats values. Adds two new regression tests: one verifies that append-mode replay updates stats incrementally without triggering full recounts (via monkeypatch assertion), and another verifies that missing stats rows are repaired on replay.
Topology documentation updates
docs/plans/topology-target.yaml, docs/topology-status.md
Registers the new _append_stats.py module in the topology target and updates generated status counts.

🎯 3 (Moderate) | ⏱️ ~25 minutes


🐰 Stats now flow like gentle streams,
Deltas dance where once rebounded dreams,
No full recount in append's way,
Just whispered changes, light as day! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 'perf(daemon): update append stats incrementally' clearly and concisely summarizes the main change: updating daemon append statistics incrementally rather than recounting all messages.
Description check ✅ Passed The pull request description includes all required sections from the template (Summary, Problem, Solution, Verification) with clear, detailed explanations of the changes, motivation, and how they were tested.
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/incremental-append-stats

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 010fe6f into master May 24, 2026
6 of 7 checks passed
@Sinity
Sinity deleted the feature/fix/incremental-append-stats branch May 24, 2026 04: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

Here are some automated review suggestions for this pull request.

Reviewed commit: 65d916cb4f

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

Sinity added a commit that referenced this pull request May 24, 2026
Summary

Skip post-write message/action FTS repair when live FTS triggers were already present for the write.

Problem

After #1470 and #1471 were deployed, live validation still showed `polylogued` reading about `2.34 GB` over 10 seconds with IO full avg10 around `20%`, while logs showed hot insight rebuilds were deferred and append writes were small.

The remaining broad path was `commit_archive_write_effects`: it always called `repair_message_fts_index_sync` and `repair_action_fts_index_sync` for changed conversations. On normal live writes the FTS triggers already maintain those rows, so the repair did a redundant delete/reinsert of the whole conversation FTS surface after every append.

Solution

- Record whether message/action FTS triggers were present before write side effects run.
- Always call `ensure_fts_triggers_sync` to restore missing triggers.
- If triggers were already live, trust them and skip the redundant repair.
- If triggers were missing or suspended, keep the existing targeted repair path after trigger restoration.
- Add tests for both paths.

This keeps the hard FTS freshness contract: live-triggered writes stay fresh immediately, and missing/suspended triggers still force repair.

Ref #1447

Verification

- `pytest -q tests/unit/archive/test_write_gateway.py tests/unit/pipeline/test_ingest_append_replay.py` -> `9 passed in 0.20s`
- `mypy polylogue/archive/write_effects.py tests/unit/archive/test_write_gateway.py` -> `Success: no issues found in 2 source files`
- `devtools verify --quick` -> `exit_code 0`, `total_duration_s 39.71`
- pre-push `devtools verify --quick` -> `exit_code 0`, `total_duration_s 34.62`
@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