Skip to content

refactor(cleanup): decide file keep/delete with one rule - #10906

Merged
Xuanwo merged 2 commits into
lance-format:mainfrom
wjones127:refactor/cleanup-file-verdict
Nov 15, 2026
Merged

Xuanwo merged 2 commits into
lance-format:mainfrom
wjones127:refactor/cleanup-file-verdict

Conversation

@wjones127

@wjones127 wjones127 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

cleanup_file_if_not_referenced had a separate copy of the keep/delete rule for each file kind, and the copies had drifted. This PR splits it into two steps that apply to every kind and are unit tested without an object store:

cleanup_file_if_not_referenced(path)
  ListedFile::classify(path)      -> (kind, FileReference) | None (unmanaged: keep)
  ReferencedFiles::contains(ref)  -> is_referenced / is_verified
  Verdict::decide(is_referenced, is_verified, maybe_in_progress) -> Keep | Delete { unverified }

The staging-manifest authority check is now the only per-kind special case. The new code lives in cleanup/file_verdict.rs.

This also fixes the unverified drift. A data, index, deletion or _fri file that is old and also referenced by an expiring manifest was reported as unverified: true in explain(), and the audit log recorded it as delete_unverified. It now reports as verified, which is what transaction and _blobs files already did. Deletions are unchanged: the same files get deleted either way.

Evidence

The new unit tests are tables over path × reference state × age: 27 cases for classify, 7 for decide, and 18 for key lookup. The decide::verified_old case is the regression test for the drift.

Seven integration tests only checked this rule, so they are removed:

  • cleanup_unreferenced_data_files
  • do_not_cleanup_newer_data
  • cleanup_old_index
  • clean_old_delete_files
  • dont_clean_index_data_files
  • dont_cleanup_in_progress_write
  • orphan_map_ages_out

Their keep-side checks moved into every_removed_file_kind_contributes_its_bytes, renamed to cleanup_keeps_and_removes_every_file_kind. It now asserts exact removed and surviving counts for every kind, so a mismatch between the keys manifests record and the keys classify derives from paths is still caught end to end.

Per-test time from --test-threads=1 --report-time, debug build, measured before the current-main integration:

Tests Time
Rule tests before (8 integration) 8 0.187 s
Rule tests after (1 integration + 52 unit) 53 0.075 s
Whole dataset::cleanup suite, sum of per-test times 233 → 278 8.96 s → 8.85 s

The rule tests now take 60% less time, but they were already cheap. Over the whole suite the saving is within run-to-run noise. Most of the suite's time goes to branch_lineage tests (~4.4 s), which this rule doesn't affect. Parallel wall time is unchanged at about 1.8 s.

Integration

Merged main at 57bd7ff59 with no conflicts, so the rule runs on top of the landed bulk-delete path (#10787), transaction and deletion orphan discovery (#10899) and the foreign-root fixture (#11095); their code and tests are unchanged. On the merged tree cargo fmt --all --check, cargo check --workspace --tests --benches --profile ci --locked, cargo clippy --all --tests --benches --profile ci --locked -- -D warnings and cargo test --workspace --profile ci --locked (23,129 passed, 0 failed) pass, as do the 52 rule cases in isolation and cleanup_keeps_and_removes_every_file_kind with its exact per-kind counts. The timings above were not re-measured after the merge.

Merge Risk

Door: Two-way. No format or API change. But a mistake in this code would delete live files, and deleted files can't be recovered.
Blast Radius: cleanup. Only the unverified flag in explain() and the audit-log mode change.

Fixes #10903

🤖 Generated with Claude Code

Split `cleanup_file_if_not_referenced` into path classification and one
verdict rule shared by every file kind, and unit test both without an
object store. A verified file that is also old is now reported as
verified for data, index, deletion and row-map files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wjones127
wjones127 marked this pull request as ready for review November 10, 2026 18:03

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 10, 2026
…-verdict

Integrate the single keep/delete rule with main's bulk deletes (lance-format#10787), transaction and deletion orphan discovery (lance-format#10899) and the foreign-root fixture (lance-format#11095).
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 15, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Gate recommendation: approve.

No blocking findings. The shared rule fixes the unverified classification while preserving retained-file and in-progress-write protections through the current listing and bulk-delete paths.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 15, 2026
@Xuanwo
Xuanwo merged commit 1e6aa0b into lance-format:main Nov 15, 2026
43 of 44 checks passed
Xuanwo added a commit that referenced this pull request Nov 15, 2026
## Summary

Cleanup decided whether a version expires in several places in
`CleanupTask` (`run` mutating `policy.before_timestamp`,
`cleanup_candidates`, `process_manifest_file`, `bucket_survivors` in
`CleanupInspection`, `CleanupPolicy::proven_expired`). The only way to
test retention semantics was to write real versions to a real dataset.
This PR moves that decision into `cleanup/expiry.rs`, which does no I/O:

```text
VersionExpiry::new(legacy filters, retention rules, now, thinning anchor, read_version)
    .with_tagged(..).with_branch_held(..)
  classify(version, timestamp)  -> Retained | TaggedOld | Expired | Thinned(candidate)
  is_old(version, timestamp)    -> used to pick referenced branches to cascade into
  is_expired_by_version(version) -> the delete_unverified fast path (was proven_expired)
ThinningSurvivors<T>::offer(candidate, T) / into_survivors()
```

`CleanupTask` still does the I/O: it resolves the anchor from the
version hint (now `resolve_thinning_anchor`), lists manifests, and
re-reads one survivor per bucket. Behavior is unchanged.

Answers to the issue's open questions:
- Expiry can't be decided before reading the manifest, because the
timestamp is in the manifest. Survivors are still re-read once, which
keeps memory bounded by bucket count.
- Legacy filters stay a separate input. Internally, `before_timestamp`
and `RemoveAll` combine into one cutoff, so `run` no longer mutates the
policy.
- No rename of the second `CleanupPolicy`; that's a public API change
and can be a separate PR.

## Evidence

Rule semantics (ladder, independent bands, per-period buckets, hint
anchoring, `RemoveAll` vs. samples, filters, tags, branch-held and
detached versions, the version-only fast path, out-of-range ages) are
now covered by 29 synchronous tests in `expiry::tests`, which also check
that survivors do not depend on manifest listing order. The retention
integration matrices drop from 32 cases to 8, keeping one case for each
I/O path they exercise (config parsing, hint reads, pinned snapshots,
tag/filter wiring, `delete_unverified`).
`cleanup_retention_bands_have_independent_buckets` is now only a unit
test.

Cleanup test runtime (`dataset::cleanup::`, `--test-threads=1`, debug
build, 3 runs each on the same machine, both on `1b89c0041`):

| | tests | wall time | retention + expiry tests |
|---|---|---|---|
| main | 256 | 8.83–8.89 s | 1.86 s (73 tests) |
| this PR | 255 | 7.82–8.25 s | 0.72 s (73 tests, 25 of them ~0 ms) |

Most of what's left in the cleanup suite is branch-lineage and
managed-blob I/O, which this PR doesn't touch.

Those timings are a historical measurement on `1b89c0041`; they were not
re-run on the integrated tree.

Merged with `main` at `1e6aa0b`, which includes the `file_verdict`
extraction from #10906, so both modules coexist with their `Verdict`
types aliased at the integration boundary. The expiry, retention and
`file_verdict` suites plus the workspace check, format and clippy gates
pass on the integrated tree.

## Merge Risk

**Door:** One-way in effect. This is an internal refactor with no API or
format change, but it sits on the path that deletes data, so a semantic
slip would delete versions.
**Blast Radius:** cleanup (auto-cleanup, `cleanup_old_versions`,
retention policies, branch cascade).

Fixes #10904

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Xuanwo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: consolidate cleanup file keep/delete decision into one rule

2 participants