mkit_cli/commands/gc.rs
1//! `mkit gc` — reclaim unreachable objects (mark-and-sweep prune).
2//!
3//! Under the repo lock: expire the recovery log, compute the live-object
4//! keep-set (every object reachable from the retention roots — refs,
5//! stash, in-progress op state, attestations, and the recovery log), then
6//! delete unreachable objects that are older than the grace window.
7//!
8//! Safety: the live set is computed **before** anything is deleted and
9//! the whole run is **fail-closed** — a missing/corrupt root, a malformed
10//! ref, or the reachability cap aborts with nothing removed (see
11//! `mkit_core::ops::gc`). Unreachable objects younger than the grace
12//! window (default 14 days) are kept as a belt-and-suspenders against
13//! objects written just before a reference that points at them. Use
14//! `--dry-run` to preview, and `--grace-secs 0` to prune every
15//! unreachable object regardless of age.
16//!
17//! Concurrency: gc holds the repo lock for its whole run, and the
18//! root-publishing paths now take the same lock around their object-write +
19//! ref/attestation-publish window — `tag` (annotated/signed), `fetch` /
20//! `pull`, and `attest` (#267) — so they are serialized against gc. The
21//! grace window remains the belt-and-suspenders net (like Git's default
22//! `gc.pruneExpire`, vs `prune --expire=now`): `--grace-secs 0` bypasses it
23//! and prints a warning, but with the publishers now locked it is safe even
24//! under concurrency.
25
26use std::io::Write;
27use std::time::{SystemTime, UNIX_EPOCH};
28
29use clap::Parser;
30use mkit_core::ops::recovery::{self, RetentionPolicy};
31use mkit_core::ops::run_gc;
32use mkit_core::store::ObjectStore;
33
34use crate::clap_shim;
35use crate::exit;
36
37/// Default object grace window: 14 days, matching Git's `gc.pruneExpire`.
38const DEFAULT_GRACE_SECS: u64 = 14 * 24 * 60 * 60;
39
40#[derive(Debug, Parser)]
41#[command(
42 name = "mkit gc",
43 about = "Reclaim unreachable objects (delete unreachable objects older than the grace window)."
44)]
45struct GcOpts {
46 /// Show what would be pruned without deleting anything.
47 #[arg(short = 'n', long = "dry-run")]
48 dry_run: bool,
49
50 /// Keep unreachable objects younger than this many seconds (default
51 /// 14 days). `0` prunes every unreachable object, but bypasses the
52 /// grace window that protects in-flight objects — only safe when no
53 /// other mkit process is operating on the repo.
54 #[arg(long = "grace-secs", value_name = "SECS", default_value_t = DEFAULT_GRACE_SECS)]
55 grace_secs: u64,
56}
57
58#[must_use]
59pub fn run(args: &[String]) -> u8 {
60 let opts = match clap_shim::parse::<GcOpts>("mkit gc", args) {
61 Ok(o) => o,
62 Err(code) => return code,
63 };
64 let cwd = match std::env::current_dir() {
65 Ok(p) => p,
66 Err(e) => return emit_err(&format!("cwd: {e}"), exit::NOINPUT),
67 };
68 let layout = match super::resolve_layout(&cwd) {
69 Ok(layout) => layout,
70 Err(code) => return code,
71 };
72
73 let store = match ObjectStore::open(&layout) {
74 Ok(s) => s,
75 Err(e) => return emit_err(&format!("not a mkit repo: {e}"), exit::GENERAL_ERROR),
76 };
77
78 // Hold EVERY worktree's lock for the whole run (#493 Phase 3) —
79 // the shared lock spanning trees. Root collection unions each
80 // tree's HEAD/index/op-state, so gc must serialize against
81 // worktree/index-mutating commands in ALL trees, not just the
82 // invoking one, plus other gc runs. Acquisition order is
83 // deterministic (main first, then registry ids ascending, from
84 // `all_state_layouts`) so concurrent multi-lock takers cannot
85 // deadlock. It still does NOT serialize against the non-worktree
86 // root publishers (`tag`, `fetch`, `attest`) — those don't take
87 // this lock (#267); the grace window protects their in-flight
88 // objects, exactly as in the single-tree case.
89 // Registry lock FIRST (global lock order: worktrees.lock before
90 // any per-tree worktree.lock, see SPEC-WORKTREE §4.3): freezes the
91 // worktree set for the whole run, so a `worktree add` cannot
92 // register a fresh tree — and start staging into it — between
93 // enumeration and the sweep.
94 let _registry_lock = match super::acquire_worktrees_registry_lock(&layout) {
95 Ok(l) => l,
96 Err(code) => return code,
97 };
98 let state_layouts = match mkit_core::layout::all_state_layouts(&layout) {
99 Ok(l) => l,
100 Err(e) => return super::error(&format!("worktree registry: {e}"), exit::DATAERR),
101 };
102 let mut locks = Vec::with_capacity(state_layouts.len());
103 for tree in &state_layouts {
104 match super::acquire_worktree_lock(tree) {
105 Ok(l) => locks.push(l),
106 Err(code) => return code,
107 }
108 }
109 let _locks = locks;
110
111 let now = SystemTime::now()
112 .duration_since(UNIX_EPOCH)
113 .map_or(0, |d| d.as_secs());
114
115 if opts.grace_secs == 0 && !opts.dry_run {
116 let mut stderr = std::io::stderr().lock();
117 let _ = writeln!(
118 stderr,
119 "warning: --grace-secs 0 prunes every unreachable object, bypassing the grace window; \
120 ensure no other mkit process is operating on this repo"
121 );
122 }
123
124 // Expire stale recovery entries first so they stop pinning objects;
125 // abort on error (fail closed — don't prune against a half-expired log).
126 // A dry run must not mutate state, so it only *counts* what would
127 // expire (and therefore reports a conservative prune set, since those
128 // soon-to-expire commits are still pinned during the preview).
129 let policy = RetentionPolicy::default();
130 let expired = if opts.dry_run {
131 match recovery::would_expire(&layout, now, &policy) {
132 Ok(n) => n,
133 Err(e) => return emit_err(&format!("recovery log: {e}"), exit::GENERAL_ERROR),
134 }
135 } else {
136 match recovery::expire(&layout, now, &policy) {
137 Ok(n) => n,
138 Err(e) => return emit_err(&format!("expire recovery log: {e}"), exit::CANTCREAT),
139 }
140 };
141
142 let report = match run_gc(&store, &layout, now, opts.grace_secs, opts.dry_run) {
143 Ok(r) => r,
144 Err(e) => return emit_err(&format!("gc: {e}"), exit::GENERAL_ERROR),
145 };
146
147 let mut stderr = std::io::stderr().lock();
148 let (prune_verb, expire_verb) = if report.dry_run {
149 ("would prune", "would expire")
150 } else {
151 ("pruned", "expired")
152 };
153 let _ = writeln!(
154 stderr,
155 "gc{}: {prune_verb} {} object(s), {} bytes; scanned {}, live {}, kept-recent {}; {expire_verb} {} recovery entr{}",
156 if report.dry_run { " (dry run)" } else { "" },
157 report.pruned,
158 report.bytes_reclaimed,
159 report.scanned,
160 report.live,
161 report.kept_recent,
162 expired,
163 if expired == 1 { "y" } else { "ies" },
164 );
165 exit::OK
166}
167
168use super::error as emit_err;