1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Standalone cleanup CLI for v1 CacheManifest registries.
//!
//! Phase 2 of #228 (#231). This binary does not require the broker or
//! originating daemons to be running.
use std::process::ExitCode;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use running_process::broker::manifest;
use running_process::cleanup::{
actions_json, instances, list, parse_duration_secs, prune, uninstall, verify_artifacts,
verify_basic,
};
#[derive(Parser)]
#[command(
name = "running-process-cleanup",
about = "Inspect and clean running-process v1 CacheManifest registries"
)]
struct Cli {
/// Override the central manifest registry directory.
#[arg(long, global = true)]
registry_dir: Option<std::path::PathBuf>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// List manifests in the central registry.
List {
/// Emit JSON.
#[arg(long)]
json: bool,
},
/// Prune dormant or explicitly-selected cache roots.
Prune {
/// Select manifests dormant for this duration, e.g. 30d, 12h.
#[arg(long)]
dormant_after: Option<String>,
/// Keep current manifests that have a daemon process recorded.
#[arg(long)]
keep_current: bool,
/// Keep the N most recently-active versions per service.
#[arg(long)]
keep_last: Option<usize>,
/// Restrict pruning to a single service.
#[arg(long)]
service: Option<String>,
/// Restrict pruning to a single service version.
#[arg(long)]
version: Option<String>,
/// Actually delete selected roots. Omit for dry-run.
#[arg(long)]
confirm: bool,
/// Emit JSON.
#[arg(long)]
json: bool,
},
/// Uninstall one service's manifest-declared cache roots.
Uninstall {
/// Service name to uninstall.
service: String,
/// Preserve CACHE_CONFIG roots.
#[arg(long)]
keep_config: bool,
/// Actually delete selected roots. Omit for dry-run.
#[arg(long)]
confirm: bool,
/// Emit JSON.
#[arg(long)]
json: bool,
},
/// Registry consistency verification plus exhaustive daemon-artifact
/// reconciliation (#391): socket, pid file, .servicedef files, SQLite
/// registry (incl. WAL/SHM), log files, emergency reserve, shadow dir.
/// Read-only: nothing is deleted.
Verify {
/// Emit JSON.
#[arg(long)]
json: bool,
/// Reconcile the artifacts of this scope hash instead of the
/// global daemon scope.
#[arg(long)]
scope_hash: Option<String>,
},
/// Enumerate visible broker instances.
Instances {
/// Placeholder for Phase 4 broker status aggregation.
#[arg(long)]
status: bool,
/// Emit JSON.
#[arg(long)]
json: bool,
},
}
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(err) => {
eprintln!("error: {err:#}");
ExitCode::from(1)
}
}
}
fn run() -> Result<()> {
let cli = Cli::parse();
let registry_dir = cli
.registry_dir
.unwrap_or_else(manifest::central_registry_dir);
match cli.command {
Commands::List { json } => {
let manifests = list::list(®istry_dir);
if json {
println!("{}", list::render_json(&manifests));
} else if manifests.is_empty() {
println!("no manifests found in {}", registry_dir.display());
} else {
for manifest in manifests {
println!(
"{} {} roots={} last_active_unix_ms={}",
manifest.service_name,
manifest.service_version,
manifest.roots.len(),
manifest.last_active_unix_ms
);
}
}
}
Commands::Prune {
dormant_after,
keep_current,
keep_last,
service,
version,
confirm,
json,
} => {
let dormant_after_secs = dormant_after
.as_deref()
.map(parse_duration_secs)
.transpose()
.context("invalid --dormant-after")?;
let options = prune::PruneOptions {
dormant_after_secs,
keep_current,
keep_last,
service,
version,
confirm,
};
let actions = prune::run(®istry_dir, &options)?;
if json {
println!("{}", actions_json(1, &actions));
} else {
print_actions(&actions, confirm);
}
}
Commands::Uninstall {
service,
keep_config,
confirm,
json,
} => {
let actions = uninstall::run(®istry_dir, &service, keep_config, confirm)?;
if json {
println!("{}", actions_json(1, &actions));
} else {
print_actions(&actions, confirm);
}
}
Commands::Verify { json, scope_hash } => {
let report = verify_basic::run(®istry_dir);
let artifact_paths =
verify_artifacts::ArtifactPaths::from_environment(scope_hash.as_deref());
let artifacts = verify_artifacts::run(&artifact_paths);
if json {
// Additive extension of the frozen verify JSON shape: the
// registry document gains an `artifacts` object (#391).
let mut document: serde_json::Value =
serde_json::from_str(&verify_basic::render_json(&report))
.context("internal: verify JSON did not round-trip")?;
document["artifacts"] = artifacts.to_json_value();
println!("{document}");
} else {
if report.findings.is_empty() {
println!("verified {} manifest(s); no findings", report.scanned);
} else {
for finding in &report.findings {
println!(
"{}: {}: {}",
finding.severity,
finding.path.display(),
finding.message
);
}
}
print!("{}", artifacts.render_text());
}
if artifacts.exit_code() != 0 {
anyhow::bail!("artifact verification could not inspect every location");
}
}
Commands::Instances { status, json } => {
let found = instances::list();
if json {
println!("{}", instances::render_json(&found));
} else if found.is_empty() {
if status {
println!(
"no broker instances found; status aggregation requires Phase 4 broker"
);
} else {
println!("no broker instances found");
}
} else {
for instance in found {
println!("{}", instance.path);
}
}
}
}
Ok(())
}
fn print_actions(actions: &[running_process::cleanup::CleanupAction], confirm: bool) {
if actions.is_empty() {
println!("no matching cache roots");
return;
}
for action in actions {
let verb = if action.skipped {
"skip"
} else if confirm {
"deleted"
} else {
"would delete"
};
if let Some(reason) = &action.skip_reason {
println!("{verb}: {} ({reason})", action.path.display());
} else {
println!("{verb}: {}", action.path.display());
}
}
}