Skip to content

docs(storage): document blob GC lease design as kept-by-design (#1000) - #1015

Merged
Sinity merged 1 commit into
masterfrom
feature/chore/blob-gc-lease-audit-1000
May 11, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/chore/blob-gc-lease-audit-1000

Conversation

@Sinity

@Sinity Sinity commented May 11, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes #1000. Audit-only PR. Confirms the lease tables (pending_blob_refs, gc_generations) are still load-bearing and documents why they coexist with the snapshot-based _still_referenced check.

Audit outcome

Issue #1000 asked whether the lease design has been superseded by the snapshot check. Tracing every external caller of acquire_blob_leases / release_operation_leases shows:

  • polylogue/archive/write_effects.py::commit_archive_write_effects is the only external caller. It acquires leases on a separate immediate-commit connection before the main archive write transaction commits, then releases them after the commit.
  • run_blob_gc enforces both _still_referenced (snapshot reference check against raw_conversations) and _has_active_lease (in-flight operation check against pending_blob_refs).

The two checks cover different windows: the snapshot check covers blobs already referenced by a committed archive row; the lease check covers blobs the write path has materialized but has not yet inserted into raw_conversations. Removing the lease tables would re-open the exact race the design was added to close.

Solution

No behavioural changes. The audit was the entire scope; no half-removal — either keep the tables and document them, or remove them. This PR keeps and documents.

Verification

  • nix develop -c devtools verify --quick → all checks passed.

Summary by CodeRabbit

  • Documentation
    • Added comprehensive documentation describing internal garbage-collection concurrency models and mechanisms. Updated related docstrings for improved clarity.

Review Change Stack

## Audit outcome

Issue #1000 asked whether the lease tables (``pending_blob_refs``,
``gc_generations``) are still load-bearing or whether the snapshot-based
``repair_orphaned_blobs`` path has fully replaced them.

Audit result: **leases are still load-bearing and intentionally
retained.** Confirmed by tracing every caller of ``acquire_blob_leases``
/ ``release_operation_leases``:

- ``polylogue/archive/write_effects.py:commit_archive_write_effects``
  acquires leases on a separate immediate-commit connection *before*
  the main archive write transaction commits, then releases them
  *after* the commit. This is the only call site outside ``blob_gc.py``
  itself.
- ``run_blob_gc`` in ``polylogue/storage/blob_gc.py`` enforces both
  ``_still_referenced`` (snapshot reference check against
  ``raw_conversations``) **and** ``_has_active_lease`` (in-flight
  operation check against ``pending_blob_refs``).

The two checks cover different windows:

1. The snapshot check protects blobs already referenced by a committed
   archive row.
2. The lease check protects blobs the write path has materialized but
   has not yet inserted into ``raw_conversations``.

Removing the lease tables would re-open the exact race the design was
added to close: a GC pass running between blob materialization and the
corresponding ``raw_conversations`` write would misclassify the blob
as orphan and delete it.

## Documentation changes

- ``docs/internals.md`` — added a "GC concurrency model — leases plus
  snapshot reference check" section under Blob Store Model that names
  the two invariants, calls out ``commit_archive_write_effects`` as
  the sole external caller, and explains why removal would reintroduce
  the race.
- ``polylogue/storage/blob_gc.py::acquire_blob_leases`` — docstring now
  cites #1000 and points at ``docs/internals.md`` so the rationale
  lives next to the code.
- ``AGENTS.md`` — auto-rendered.

No behavioural changes. The audit was the entire scope.

Closes #1000

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 11, 2026 00:40
@coderabbitai

coderabbitai Bot commented May 11, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR documents the blob garbage-collection concurrency model across three files: a new subsection in internals documentation explains the two-part safety checks (DB snapshot reference plus pending lease check) and why lease tables are retained; the generated AGENTS manifest is updated to reflect this documentation; and the acquire_blob_leases function docstring is expanded to reference the full concurrency contract.

Changes

Blob GC Concurrency Model Documentation

Layer / File(s) Summary
Core GC concurrency model documentation
docs/internals.md
New "GC concurrency model — leases plus snapshot reference check" subsection describes the two-part safety invariants (DB snapshot reference query plus pending lease check), the purpose of lease tables to cover the blob-acquire → DB-commit window, how the lease lifecycle is exercised via commit_archive_write_effects, acquire_blob_leases, and release_operation_leases, and how gc_generations plus age thresholds provide defense-in-depth. Duplicate "Known issues" GC bullet removed.
Generated manifest sync
AGENTS.md
Documentation subsection added to reflect the GC concurrency model from internals docs, with "Known issues" GC bullet repositioned to follow the new model section for consistency.
Function docstring contract clarification
polylogue/storage/blob_gc.py
acquire_blob_leases docstring expanded to document the lease safety contract, explain the ingest-to-DB-commit window being protected, clarify why pending_blob_refs is retained alongside DB checks, and reference the full concurrency model in docs/internals.md.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

A rabbit's tale of clarity, hopped with care,
Leases and snapshots, a safety pair,
No races now, no blobs left to decay,
The docs shine bright—the design's here to stay! 🐰📚

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: documenting blob GC lease design as kept-by-design, with a reference to the audit issue #1000.
Description check ✅ Passed The description follows the template well, with Summary, Problem (Audit outcome), Solution, and Verification sections; it lacks explicit mention of Changelog and Risks/Follow-ups but these omissions are justified for documentation-only work.
Linked Issues check ✅ Passed The PR fully addresses all acceptance criteria from #1000: audits callers of acquire_blob_leases/release_operation_leases, documents the concurrency model in docs/internals.md, and keeps lease tables while documenting their role.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to documentation of existing lease design and docstring clarification; no behavioral or logic changes are introduced.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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/chore/blob-gc-lease-audit-1000

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/internals.md`:
- Around line 175-179: The docs claim GC uses "previous generation plus
MIN_AGE_S" gating but the implementation in run_blob_gc() only applies a fixed
MIN_AGE_S filter; pick one consistent approach and update accordingly: either
(A) change run_blob_gc() to additionally consult gc_generations (compare each
candidate blob's generation timestamp or generation id against
gc_generations[shard] + MIN_AGE_S) so the code enforces the doc's
generation-based age gate, or (B) update the docs to state that eligibility is
determined solely by MIN_AGE_S and remove the "previous generation" wording;
reference gc_generations, MIN_AGE_S, and run_blob_gc() to locate the relevant
logic and make the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e3d37825-9851-4dc0-aa2e-3322f6b8b3b4

📥 Commits

Reviewing files that changed from the base of the PR and between 50f23b3 and ff0e384.

📒 Files selected for processing (3)
  • AGENTS.md
  • docs/internals.md
  • polylogue/storage/blob_gc.py

Comment thread docs/internals.md
Comment on lines +175 to +179
`gc_generations` tracks the high-water mark of completed GC runs. The
"defense-in-depth" age check requires candidate blobs to be older than
the previous generation plus `MIN_AGE_S` so that a brand-new blob is
never considered for deletion within the same GC run cycle that
created it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Generation-based age-gating claim does not match current GC implementation.

The new section says eligibility is based on “previous generation plus MIN_AGE_S”, but run_blob_gc() currently filters only by a fixed MIN_AGE_S threshold and does not use generation timestamps for candidate age decisions. Please align docs and code so the audited contract is exact.

Proposed doc-only alignment (if behavior is unchanged)
-`gc_generations` tracks the high-water mark of completed GC runs. The
-"defense-in-depth" age check requires candidate blobs to be older than
-the previous generation plus `MIN_AGE_S` so that a brand-new blob is
-never considered for deletion within the same GC run cycle that
-created it.
+`gc_generations` tracks completed GC runs. Candidate selection currently
+applies a fixed minimum age threshold (`MIN_AGE_S`) before lease/reference
+checks. If generation-relative age gating is introduced, this section should
+be updated to describe that behavior explicitly.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
`gc_generations` tracks the high-water mark of completed GC runs. The
"defense-in-depth" age check requires candidate blobs to be older than
the previous generation plus `MIN_AGE_S` so that a brand-new blob is
never considered for deletion within the same GC run cycle that
created it.
`gc_generations` tracks completed GC runs. Candidate selection currently
applies a fixed minimum age threshold (`MIN_AGE_S`) before lease/reference
checks. If generation-relative age gating is introduced, this section should
be updated to describe that behavior explicitly.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/internals.md` around lines 175 - 179, The docs claim GC uses "previous
generation plus MIN_AGE_S" gating but the implementation in run_blob_gc() only
applies a fixed MIN_AGE_S filter; pick one consistent approach and update
accordingly: either (A) change run_blob_gc() to additionally consult
gc_generations (compare each candidate blob's generation timestamp or generation
id against gc_generations[shard] + MIN_AGE_S) so the code enforces the doc's
generation-based age gate, or (B) update the docs to state that eligibility is
determined solely by MIN_AGE_S and remove the "previous generation" wording;
reference gc_generations, MIN_AGE_S, and run_blob_gc() to locate the relevant
logic and make the change.

Copilot AI 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.

Pull request overview

This audit-only PR documents why blob-GC lease tables (pending_blob_refs, gc_generations) remain necessary alongside the snapshot-based reference check, and adds a pointer from acquire_blob_leases() to the new internal documentation.

Changes:

  • Expanded acquire_blob_leases() docstring to capture the rationale (Issue #1000) and link to internal docs.
  • Added a new “GC concurrency model — leases plus snapshot reference check” section to docs/internals.md.
  • Re-rendered the same documentation into AGENTS.md.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
polylogue/storage/blob_gc.py Docstring update explaining the lease-vs-snapshot concurrency window and linking to internals docs.
docs/internals.md New internal documentation section describing GC safety invariants and why leases are retained.
AGENTS.md Auto-rendered copy of the updated internals documentation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/internals.md
Comment on lines +175 to +179
`gc_generations` tracks the high-water mark of completed GC runs. The
"defense-in-depth" age check requires candidate blobs to be older than
the previous generation plus `MIN_AGE_S` so that a brand-new blob is
never considered for deletion within the same GC run cycle that
created it.
Comment thread AGENTS.md
Comment on lines +853 to +856
"defense-in-depth" age check requires candidate blobs to be older than
the previous generation plus `MIN_AGE_S` so that a brand-new blob is
never considered for deletion within the same GC run cycle that
created it.
Comment thread docs/internals.md
Comment on lines +148 to +151
1. **DB reference check** — `_still_referenced` queries
`raw_conversations` for the blob's `raw_id`. If a raw record points at
the blob, GC skips it. This is the snapshot/mark-and-sweep view of
"this blob is in active use right now."
Comment thread AGENTS.md
Comment on lines +825 to +828
1. **DB reference check** — `_still_referenced` queries
`raw_conversations` for the blob's `raw_id`. If a raw record points at
the blob, GC skips it. This is the snapshot/mark-and-sweep view of
"this blob is in active use right now."
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.

chore(storage): audit blob GC lease design (pending_blob_refs / gc_generations) for retention

2 participants