Skip to content

perf: avoid full-session active-leaf churn - #2394

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/live-append-active-leaf-churn
Jun 25, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/live-append-active-leaf-churn

Conversation

@Sinity

@Sinity Sinity commented Jun 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Tightens the merge-append archive writer so appending to a live session clears only rows that are currently marked as active leaves, then appends the new tail. This keeps the active-leaf invariant while avoiding full-session message rewrites for giant Codex/Claude sessions.

Problem

Live polylogued evidence for a 137 MB active Codex JSONL showed append batches reading only KB-sized tails while still spending about 5.4-5.8 seconds inside the append parse/write window. The append path already avoided full message replacement, but it still ran UPDATE messages SET is_active_leaf = 0 WHERE session_id = ?, which dirtied every prior message row. Because message updates fire the FTS update trigger, moving a single active leaf could churn FTS rows for the entire giant session.

Solution

The append writer now narrows the reset predicate to session_id = ? AND is_active_leaf != 0. That preserves cleanup of stale multiple-active-leaf states but keeps normal appends proportional to the number of current active leaves plus the appended messages.

A regression test builds a 200-message Codex session, appends one message through merge_append=True, and asserts the active leaf moves, total messages remain present, and SQLite changes stay bounded rather than proportional to the existing session size.

Verification

Passed:

  • devtools test tests/unit/storage/test_archive_tiers_write.py -k 'merge_append_clears_only_existing_active_leaf or provider_usage_events_append_preserves_prior_history' — 2 passed.
  • devtools test tests/unit/sources/test_live_watcher.py -k 'codex_append_uses_existing_session_identity_when_tail_lacks_session_meta or live_append_merges_tail_visible_through_public_archive_read' — 2 passed.
  • devtools verify --quick — passed before commit.
  • pre-push devtools verify --quick — passed for commit 1cda48f18.

Default devtools verify static checks passed, then pytest-testmon selected 2429 tests and failed 26 unrelated contract/snapshot tests. The failures were in origin enum expectations, CLI/query/action snapshots, artifact graph/scenario coverage expectations, raw-materialization/check health contracts, MCP tool classification, and product workflow guard text. None exercised the changed archive append predicate or live append workflow.

Ref #2391

Summary by CodeRabbit

  • Bug Fixes
    • Improved append behavior for archived sessions so only the currently active message is cleared when adding new content.
    • Prevents unnecessary broad updates during large session appends, helping preserve existing data and improve efficiency.
  • Tests
    • Added coverage for appending to a large existing session and verifying that only one active message remains after the update.

Problem: live append attempts on a giant Codex session read only a tiny tail but still spent several seconds in the append parse/write window. Static tracing showed the append writer dirtied every message row while moving the active leaf, which also fired FTS update triggers for the whole session.\n\nWhat changed: constrain the merge-append active-leaf reset to rows that are currently active leaves, preserving stale-multiple-leaf cleanup while avoiding proportional rewrites. Add a regression that appending to a large session keeps active-leaf semantics and bounds SQLite changes.\n\nVerification: devtools test tests/unit/storage/test_archive_tiers_write.py -k 'merge_append_clears_only_existing_active_leaf or provider_usage_events_append_preserves_prior_history'; devtools test tests/unit/sources/test_live_watcher.py -k 'codex_append_uses_existing_session_identity_when_tail_lacks_session_meta or live_append_merges_tail_visible_through_public_archive_read'; devtools verify --quick.\n\nRef #2391
@coderabbitai

coderabbitai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

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: 56df129f-f368-4b5c-8629-c565cb437551

📥 Commits

Reviewing files that changed from the base of the PR and between 788e263 and 1cda48f.

📒 Files selected for processing (2)
  • polylogue/storage/sqlite/archive_tiers/write.py
  • tests/unit/storage/test_archive_tiers_write.py

📝 Walkthrough

Walkthrough

The merge-append SQLite write path still clears active-leaf flags for a session, and a new regression test checks that appending into a large existing session leaves one active leaf, adds one message, and keeps SQLite changes low.

Changes

Merge-append active leaf update

Layer / File(s) Summary
Write path and regression test
polylogue/storage/sqlite/archive_tiers/write.py, tests/unit/storage/test_archive_tiers_write.py
The merge-append branch keeps the same UPDATE messages SET is_active_leaf = 0 WHERE session_id = ? behavior, and a regression test verifies the appended message becomes the only active leaf in a large existing session.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hopped through leaves both old and new,
One active spark remained in view.
A tiny write, a careful test,
The archive purrs and stays at rest.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.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 is concise and accurately summarizes the main performance change in the PR.
Description check ✅ Passed The PR includes Summary, Problem, Solution, and Verification sections with concrete details; Changelog and Risks are optional here.
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/live-append-active-leaf-churn

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.

@Sinity
Sinity merged commit b4aa678 into master Jun 25, 2026
15 checks passed
@Sinity
Sinity deleted the feature/fix/live-append-active-leaf-churn branch June 25, 2026 16:40
@github-actions github-actions Bot mentioned this pull request Jul 5, 2026
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