Skip to content

fix(daemon): bound blob cleanup and append planning - #1428

Merged
Sinity merged 3 commits into
masterfrom
fix/status-large-archive-timeout
May 23, 2026
Merged

Sinity merged 3 commits into
masterfrom
fix/status-large-archive-timeout

Conversation

@Sinity

@Sinity Sinity commented May 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes two production blockers discovered on the live archive: orphaned blob cleanup was advertised but not executable through the resumable maintenance path, and live append planning could retain large JSONL tails in daemon memory before ingestion.

Problem

The live blob store is 422.10 GiB while raw_conversations references only 31.44 GiB. A filesystem/DB comparison found 20,242 orphan blob files totaling 419,141,710,487 bytes, with no pending blob leases. The normal polylogue maintenance run --target orphaned_blobs --dry-run path failed as an unsupported replay target, and the older dry-run accounting only measured the bounded orphan sample.

Separately, LiveBatchProcessor._append_plan read from cursor offset to EOF and retained every append payload before processing. A stale cursor on a large Claude/Codex JSONL session could allocate hundreds of MiB to GiB per file and multiply across a batch, matching the daemon RSS ballooning reports.

The branch also includes the preceding status-timeout fix/docs so polylogue status remains usable on large archives.

Solution

  • Wire archive cleanup targets into polylogue.maintenance.replay, including orphaned_blobs.
  • Make orphaned blob dry-run and live cleanup account the full orphan set instead of the sample.
  • Use read-only DB connections for blob cleanup reference reads and maintenance planning reads.
  • Add a direct exact maintenance plan --target orphaned_blobs path that reports the real reclaim count/bytes without broad status scans.
  • Make shallow archive-cleanup preview explicitly bounded/truncated instead of accidentally running expensive scans.
  • Fix maintenance gc-history to read config.db_path (polylogue.db) instead of the stale archive.db path.
  • Cap live append plans at 16 MiB; larger tails fall back to the existing streaming full-ingest path.

Verification

  • pytest tests/unit/storage/test_repair_blobs.py tests/unit/maintenance/test_idempotency.py tests/unit/maintenance/test_preview.py tests/unit/sources/test_live_watcher.py -q --tb=short
  • pytest tests/unit/sources/test_live_batch_support.py -q --tb=short
  • python -m mypy polylogue/maintenance polylogue/storage/repair.py polylogue/sources/live tests/unit/storage/test_repair_blobs.py tests/unit/sources/test_live_watcher.py tests/unit/maintenance/test_preview.py tests/unit/maintenance/test_idempotency.py
  • devtools verify --quick → exit_code 0, total_duration_s 36.96 locally; pre-push rerun exit_code 0, total_duration_s 34.78.
  • Live exact dry-run: polylogue maintenance run --target orphaned_blobs --dry-run --output-format json reported 20,242 orphan blobs and 419,141,710,487 bytes.
  • Live exact plan: polylogue maintenance plan --target orphaned_blobs --output-format json completed in ~2.6s with the same count.
  • Live shallow preview: polylogue maintenance preview --scope archive_cleanup --shallow --output-format json completed in ~3.0s and reports truncated rows.

Operational note

This PR does not delete the live orphan blobs. Reclaiming the 419,141,710,487 bytes requires an explicit destructive maintenance run after review/deploy:

polylogue maintenance run --target orphaned_blobs

Summary by CodeRabbit

Release Notes

  • New Features

    • Added replay support for orphaned blobs, messages, content blocks, empty conversations, and attachments.
    • Implemented shallow vs full modes for archive-cleanup orphan counting in previews.
    • Added 16MB size limit for streaming ingestion payloads.
  • Bug Fixes

    • Increased CLI daemon health check timeout to 30s for better performance with larger archives.
    • Orphaned blob cleanup now reports complete orphan set instead of sample.
  • Documentation

    • Updated verification catalog reference for maintenance status command.

Review Change Stack

Sinity added 3 commits May 23, 2026 01:13
Problem: polylogue status used a 5s daemon timeout and treated any timeout as daemon absence. On the live workstation archive, the daemon status payload can take around 20s after a rebuild, so the CLI incorrectly reported no daemon while /api/status eventually returned a healthy payload.\n\nWhat changed: keep the fast bare-command probe at 1s, but raise the explicit status command timeout to 30s so operator-requested status can wait for the full archive health payload.\n\nVerification: python import check for _FULL_TIMEOUT_S == 30.0; pytest tests/unit/cli/test_json_envelope_contract.py::TestStatusJsonContract -q. Broad tests/unit/cli remains red from unrelated existing help snapshot/plain-output/cursor failures on this branch.
Document why the bare command keeps a 1s daemon probe while explicit status waits up to 30s for large archive health payloads.\n\nVerification: python import check for status timeout constants; pytest tests/unit/cli/test_json_envelope_contract.py::TestStatusJsonContract -q.
Problem: the live archive had 20,242 unreferenced blob files consuming 419,141,710,487 bytes, but the advertised orphaned_blobs maintenance target was not wired into the resumable executor and dry-runs only accounted the bounded sample. Live append planning also read every byte from a stale cursor offset to EOF and retained all append payloads before ingestion, which could balloon daemon RSS on large JSONL sessions.\n\nWhat changed: wire archive cleanup targets into maintenance replay, make orphaned blob dry-runs account the full orphan set, use read-only connections for planning/cleanup reads, add a direct exact orphaned_blobs planning path, make shallow archive cleanup preview explicitly truncated, and cap append plans at 16 MiB so large tails use the existing streaming full-ingest path.\n\nVerification: pytest tests/unit/storage/test_repair_blobs.py tests/unit/maintenance/test_idempotency.py tests/unit/maintenance/test_preview.py tests/unit/sources/test_live_watcher.py -q --tb=short; pytest tests/unit/sources/test_live_batch_support.py -q --tb=short; python -m mypy polylogue/maintenance polylogue/storage/repair.py polylogue/sources/live tests/unit/storage/test_repair_blobs.py tests/unit/sources/test_live_watcher.py tests/unit/maintenance/test_preview.py tests/unit/maintenance/test_idempotency.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 implements deterministic orphan blob repair across the maintenance system, enforces payload limits for live session ingestion, and refines CLI database path handling. The changes integrate orphaned blob cleanup into planning and preview flows with shallow/full staleness modes, extend replay dispatch for multiple repair targets, and add comprehensive test coverage.

Changes

Orphaned blobs maintenance support

Layer / File(s) Summary
Exact orphan set computation
polylogue/storage/repair.py
Introduces _orphaned_blob_hashes helper to compute the full set of orphan blobs by comparing blob store contents against database-referenced IDs. Updates repair_orphaned_blobs to use this exact set (not bounded samples) for accurate dry-run and live cleanup accounting.
Backfill planner orphaned_blobs support
polylogue/maintenance/planner.py
preview_backfill adds orphaned_blobs special-case handling with dry-run execution, computes include_expensive flag from resolved targets, and passes it to archive debt collection. Both preview and execute functions switch to open_read_connection for read-only archive DB access.
Preview staleness inventory shallow/full modes
polylogue/maintenance/preview.py
Adds include_expensive parameter to _archive_cleanup_items; when disabled, emits truncated zero-count rows for expensive orphan scopes; when enabled, counts orphaned_blobs via count_orphaned_blobs_sync. Generalizes connection handling in staleness_inventory and conditionally collects derived model statuses.
Replay dispatcher target support
polylogue/maintenance/replay.py
Extends _REPLAY_DISPATCH to wire repair functions for orphaned_messages, orphaned_content_blocks, empty_conversations, orphaned_attachments, and orphaned_blobs, advertising them as supported replay targets.
Orphaned blobs validation tests
tests/unit/storage/test_repair_blobs.py, tests/unit/maintenance/test_idempotency.py, tests/unit/maintenance/test_preview.py
Adds unit tests verifying exact orphan set behavior in dry-run and live modes, updates replay target contract to require orphaned_blobs, and extends preview model handling for orphaned scope baseline reasons.

Live ingestion append plan payload limit

Layer / File(s) Summary
Append plan payload constant and enforcement
polylogue/sources/live/batch_support.py, polylogue/sources/live/batch.py
Defines _MAX_APPEND_PLAN_PAYLOAD_BYTES (16 MiB) and adds guard in _append_plan to return None when tail byte delta exceeds limit, preventing oversized incremental reads during live ingestion.
Append plan boundary tests
tests/unit/sources/test_live_batch_support.py
New test constructs JSONL session file with appended tail exceeding size limit and verifies _append_plan rejection behavior.

CLI and configuration refinements

Layer / File(s) Summary
Config DB path and status timeout
polylogue/cli/commands/maintenance.py, polylogue/cli/commands/status.py, docs/verification-catalog.md
Maintenance command uses config.db_path instead of constructing it locally. Status command increases full-probe HTTP timeout from 5.0 to 30.0 seconds with clarifying comments. Catalog line reference updated.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Sinity/polylogue#1427: Modifies the same polylogue/cli/commands/status.py timeout increase from 5.0 to 30.0 for daemon-health HTTP requests.
  • Sinity/polylogue#879: Introduces the underlying BlobStore orphan detection APIs and initially registers the orphaned_blobs maintenance target that this PR fully integrates into planning, preview, and replay flows.
  • Sinity/polylogue#1002: Adds comprehensive integration and property tests covering the orphaned-blobs repair lifecycle and catalog dispatch that complement this PR's production changes.

Poem

🐰 Blobs once orphaned, now they're found,
Exact counts scattered all around,
Shallow checks and deep dives too,
Planning payloads—size limits new,
Maintenance flows run clean and true! 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% 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 accurately and concisely summarizes the main changes: fixing blob cleanup execution and bounding append planning, which are the two primary production issues addressed.
Description check ✅ Passed The description comprehensively covers all required template sections: Summary, Problem, Solution, Verification, Changelog, and Risks/Follow-ups. All sections are well-detailed and aligned with the actual changes.
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 fix/status-large-archive-timeout

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 1110956 into master May 23, 2026
6 of 7 checks passed
@Sinity
Sinity deleted the fix/status-large-archive-timeout branch May 23, 2026 07:55

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

ℹ️ 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/maintenance/planner.py Resolved
Comment thread polylogue/maintenance/replay.py Resolved
@Sinity

Sinity commented Sep 30, 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.

@Sinity

Sinity commented Sep 30, 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