Skip to content

fix(daemon): compact superseded raw snapshots - #1483

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/raw-retention-compaction
May 24, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/raw-retention-compaction

Conversation

@Sinity

@Sinity Sinity commented May 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds bounded cleanup for superseded live raw payload snapshots and stops future append fragments from becoming durable conversation/provider-event raw provenance. This keeps active session files from multiplying tiny append blobs and historical full snapshots far beyond the actual source workload.

Closes #1482
Ref #818

Problem

The live daemon had stopped broad catch-up churn, but the archive still showed raw/blob amplification: one active 106 MB Codex JSONL path had accumulated 1,472 raw rows and about 6.8 GB of blob references. A target-specific live preview found about 3,932 superseded raw snapshots totaling roughly 27.7 GB of referenced bytes.

The first implementation attempt also exposed a startup hazard: building provider_events(raw_id) as a runtime index on an existing 1.7M-event archive can block too long, so that index must be fresh-schema-only unless an operator explicitly chooses offline cleanup.

Solution

  • Add polylogue.storage.raw_retention to plan and execute conservative raw snapshot cleanup: source file must still exist, current conversation raw references are preserved, newest full snapshots and newest append fragments are retained, active leases are skipped, and provider-event raw links are nulled only for explicit cleanup candidates.
  • Add superseded_raw_snapshots as a polylogue doctor --cleanup --target ... target.
  • Keep explicit-target doctor maintenance in probe mode unless --deep is requested, so a single-target preview does not run broad expensive readiness scans.
  • Change append-only conversation writes so transient append fragments do not replace conversations.raw_id and new provider events do not point at append raw blobs.
  • Add daemon-side bounded compaction for raw rows acquired after the daemon process starts, avoiding broad scans and avoiding the historical provider-event-link backlog.
  • Split blob cleanup helpers out of storage/repair.py to keep file budgets clean.
  • Add the optional provider_events(raw_id) index to fresh schema DDL only; existing archives do not build it during ordinary startup.

Verification

  • pytest -q tests/unit/storage/test_raw_retention.py tests/unit/storage/test_repair_blobs.py tests/unit/sources/test_live_watcher.py --tb=short → 64 passed in 7.26s
  • pytest -q tests/unit/core/test_maintenance_targets.py tests/unit/devtools/test_artifact_graph.py tests/unit/devtools/test_scenario_coverage.py tests/unit/pipeline/test_ingest_batch.py tests/unit/storage/test_raw_retention.py tests/unit/storage/test_repair_blobs.py tests/unit/sources/test_live_watcher.py tests/unit/cli/test_check.py --tb=short → 143 passed in 8.96s
  • POLYLOGUE_FORCE_PLAIN=1 timeout 30 polylogue doctor --cleanup --target superseded_raw_snapshots --preview --format json → reported 3,932 candidate snapshots / 27,736,902,614 referenced bytes in under 1s
  • devtools render-all --check && devtools verify-file-budgets → pass
  • devtools verify --json --skip-slow → pass, exit_code: 0, total_duration_s: 46.64
  • pre-push devtools verify --quick → pass, exit_code: 0, total_duration_s: 33.66

Summary by CodeRabbit

Release Notes

  • New Features

    • Added superseded_raw_snapshots maintenance target to clean up redundant raw snapshot data.
    • Added orphaned_blobs maintenance target to detect and remove unreferenced blob files.
    • Live batch processor now performs background compaction of superseded raw snapshots during file ingestion.
  • Performance

    • Added database index to improve lookup performance for provider events.
  • Documentation

    • Updated CLI reference documentation for new maintenance targets.
    • Updated test quality and topology documentation.

Review Change Stack

Live append ingest was durably retaining every historical raw payload snapshot for active session files. On the live archive, one 106 MB Codex JSONL path had accumulated roughly 1,472 raw rows and 6.8 GB of blob references; repo-wide preview found about 27.7 GB of superseded snapshots.\n\nThis changes append writes so transient append fragments are not recorded as the durable conversation/provider-event raw provenance, adds bounded daemon-side compaction for new append snapshots, and exposes an explicit doctor cleanup target for older superseded snapshots whose source files still exist. Targeted doctor maintenance now keeps explicit-target previews in probe mode unless --deep is requested, avoiding broad expensive readiness scans when the operator only asked for one cleanup target.\n\nFresh schemas include a provider_events(raw_id) index for future archives, but existing archives do not build that index on ordinary startup.\n\nRef #1482
@coderabbitai

coderabbitai Bot commented May 24, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR implements a new canonical maintenance target superseded_raw_snapshots for cleaning up redundant live raw payload snapshots from the database and blob store, with bounded integration into the live ingest daemon and refactored blob repair utilities.

Changes

Superseded Raw Snapshot Cleanup Feature

Layer / File(s) Summary
Maintenance target registration and readiness probe
polylogue/maintenance/targets.py, polylogue/cli/shared/check_workflow.py, docs/cli-reference.md
Registers superseded_raw_snapshots target with destructive cleanup and archive-readiness gating; adjusts readiness probing to enable explicit target verification when cleanup targets are specified without deep checking.
Raw snapshot retention candidate selection and cleanup
polylogue/storage/raw_retention.py
Core retention logic: ranks raw snapshots by acquisition time per source path, filters to exclude live conversation/artifact/lease references, supports configurable retention thresholds for full vs append snapshots, and orchestrates multi-path cleanup with aggregated byte-level and error tracking.
Blob repair module and repair system integration
polylogue/storage/blob_repair.py, polylogue/storage/repair.py, polylogue/storage/sqlite/schema_ddl_provider_events.py
Extracts blob orphan counting/cleanup into dedicated blob_repair module; integrates both blob and raw-snapshot cleanup into the maintenance repair dispatch system via _PREVIEW_HANDLERS and _REPAIR_HANDLERS; adds idx_provider_events_raw_id partial index for efficient raw-id lookups.
Ingest batch hash and tuple helpers extraction
polylogue/pipeline/services/ingest_batch/_append_helpers.py, polylogue/pipeline/services/ingest_batch/_core.py
Extracts SHA-256 hash-composition and tuple-normalization utilities into dedicated helpers module; refactors _core to import and use these helpers for merged hash computation and conversation/provider-event tuple normalization before upsert.
Live batch processor raw snapshot compaction trigger
polylogue/sources/live/batch.py
Initializes per-process baseline timestamp during construction; triggers bounded raw-snapshot cleanup on successful ingest operations via background thread, using per-path limits and the baseline timestamp as acquisition-time lower bound.
Generated documentation, topology metadata, and test snapshots
docs/topology-status.md, docs/verification-catalog.md, docs/test-quality-workflows.md, docs/plans/topology-target.yaml, tests/unit/cli/__snapshots__/test_terminal_snapshots.ambr
Auto-generated documentation updates reflecting new maintenance target and module additions; CLI snapshot reflects wrapped help text for the expanded maintenance target list.
Test expectations and raw retention validation
tests/unit/core/test_maintenance_targets.py, tests/unit/devtools/test_artifact_graph.py, tests/unit/devtools/test_scenario_coverage.py, tests/unit/sources/test_live_watcher.py, tests/unit/storage/test_raw_retention.py
Updates test expectations for maintenance target catalog; adds comprehensive coverage for raw snapshot cleanup (candidate selection, dry-run vs real deletion, reference preservation, provider-event link clearing, and schema index validation); adds small timing fix to watcher test.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Sinity/polylogue#1428: Both PRs modify blob repair and orphan cleanup behavior in polylogue/storage/repair.py, with this PR extracting blob repair into a separate module while that PR adjusted dry-run counting.

Suggested labels

type:test, area:pipeline, area:storage, area:schema, area:cli


🐰 Raw snapshots trim with care,
Cleanup paths now walk the fair,
Blobs and tubes dance hand in hand,
Live ingests compact, compact, stand! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.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 PR title 'fix(daemon): compact superseded raw snapshots' directly describes the main implementation: adding bounded cleanup for superseded raw snapshots in the daemon.
Description check ✅ Passed The PR description follows the template with complete Summary, Problem, Solution, Verification, and Changelog sections, including specific test results and commands.
Linked Issues check ✅ Passed The PR successfully implements all acceptance criteria from #1482: adds raw_retention module for cleanup, registers superseded_raw_snapshots doctor target, modifies append logic to prevent append fragments from becoming durable raw provenance, adds daemon-side bounded compaction, and includes comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes directly support the core objective of cleaning superseded raw snapshots. The helper extraction to blob_repair.py, new raw_retention module, append logic modifications, and test updates all align with issue #1482 requirements.

✏️ 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/raw-retention-compaction

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: a018a81581

ℹ️ 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/cli/shared/check_workflow.py Resolved
@Sinity
Sinity merged commit 64bca08 into master May 24, 2026
6 of 7 checks passed
@Sinity
Sinity deleted the feature/fix/raw-retention-compaction branch May 24, 2026 08: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.

  • 61.F135 / P2: stale at the checked revision: The current check command is a read-only archive/runtime/schema health check at polylogue/cli/commands/check.py:41. Its CheckCommandOptions and option declarations no longer expose the target/preview repair workflow, so explicit-target previews cannot report the former probe-only false-zero repair counts through this command.

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.

Compact superseded live raw snapshots

1 participant