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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
mod backup;
mod claude_json;
mod doctor;
mod git;
mod memory;
mod orphans;
mod output;
mod paths;
mod process;
mod prune;
mod safe_io;
mod secrets;
mod show;
mod terminal;
mod transcripts;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use colored::control;
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Clone, Copy, PartialEq, clap::ValueEnum)]
enum ColorMode {
Always,
Auto,
Never,
}
fn color_override(mode: ColorMode, json: bool) -> Option<bool> {
match mode {
ColorMode::Always => Some(true),
ColorMode::Never => Some(false),
ColorMode::Auto if json => Some(false),
ColorMode::Auto => None,
}
}
#[derive(Parser)]
#[command(
name = "midden",
about = "Resolve, audit, visualize, and clean coding-agent context and state",
version,
propagate_version = true
)]
struct Cli {
/// When to use colors: auto, always, never
#[arg(long, default_value = "auto", global = true)]
color: ColorMode,
/// Output as JSON
#[arg(long, global = true)]
json: bool,
/// Path to `~/.claude.json` (default: $HOME/.claude.json)
#[arg(long, global = true, value_name = "PATH")]
config: Option<PathBuf>,
/// Path to the Claude user-scope directory (default: $HOME/.claude)
#[arg(long, global = true, value_name = "PATH")]
claude_home: Option<PathBuf>,
/// Path to the Codex user-scope directory (default: $CODEX_HOME or $HOME/.codex)
#[arg(long, global = true, value_name = "PATH")]
codex_home: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Garbage-collect dead `projects` entries from ~/.claude.json
Prune {
/// Actually remove entries (default is a dry run)
#[arg(long)]
apply: bool,
/// Also garbage-collect orphaned transcript artifacts under ~/.claude/projects/
#[arg(long)]
transcripts: bool,
/// Only consider entries under a .claude/worktrees/ path
#[arg(long = "worktrees-only")]
worktrees_only: bool,
/// Write even if a `claude` process appears to be running
#[arg(long)]
force: bool,
},
/// Hygiene and audit lint for Claude Code configuration
Doctor {
/// Target directory (resolves project-scope state for this path)
#[arg(default_value = ".")]
path: PathBuf,
/// Auto-resolve findings marked auto-fixable
#[arg(long)]
fix: bool,
/// Write even if a `claude` process appears to be running
#[arg(long)]
force: bool,
/// Unmask secret values in the output (dangerous)
#[arg(long = "show-secrets")]
show_secrets: bool,
},
/// Resolve every config surface for a target directory with provenance
Show {
/// Target directory
#[arg(default_value = ".")]
path: PathBuf,
/// Unmask secret values in the output (dangerous)
#[arg(long = "show-secrets")]
show_secrets: bool,
},
/// Inspect and manage persistent agent memory
Memory {
#[command(subcommand)]
command: MemoryCommand,
},
/// Generate shell completions
Completions {
/// Shell to generate completions for
shell: Shell,
},
}
#[derive(Subcommand)]
enum MemoryCommand {
/// Show Codex and Claude memory sources for a target directory
Show {
/// Target directory
#[arg(default_value = ".")]
path: PathBuf,
/// Provider to inspect
#[arg(long, value_enum, default_value = "all")]
provider: memory::ProviderFilter,
/// Include unrelated and unassociated memory sources
#[arg(long)]
all: bool,
},
}
/// Restore the default SIGPIPE disposition. The Rust runtime ignores SIGPIPE,
/// so `midden ... | head` would otherwise panic with a broken-pipe I/O error
/// once the reader closes; dying of SIGPIPE like any other Unix filter is the
/// contract downstream tools expect.
#[cfg(unix)]
#[expect(
unsafe_code,
reason = "restoring the default SIGPIPE disposition requires libc::signal"
)]
fn reset_sigpipe() {
// SAFETY: installing SIG_DFL registers no handler code, and this runs
// before any thread or I/O exists.
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
}
#[cfg(not(unix))]
fn reset_sigpipe() {}
fn emit_error(error: &str, json: bool) {
let safe = secrets::mask_free_text(error);
if json {
eprintln!("{}", serde_json::json!({ "error": safe }));
} else {
eprintln!("error: {}", terminal::escape(&safe));
}
}
fn main() -> ExitCode {
reset_sigpipe();
let cli = Cli::parse();
if let Some(enabled) = color_override(cli.color, cli.json) {
control::set_override(enabled);
}
let env = match paths::Env::try_new(
cli.config.clone(),
cli.claude_home.clone(),
cli.codex_home.clone(),
) {
Ok(env) => env,
Err(error) => {
emit_error(&error.to_string(), cli.json);
return ExitCode::from(2);
}
};
let result = match cli.command {
Command::Prune {
apply,
transcripts,
worktrees_only,
force,
} => prune::run(
&env,
prune::Options {
apply,
transcripts,
worktrees_only,
force,
json: cli.json,
},
),
Command::Doctor {
ref path,
fix,
force,
show_secrets,
} => doctor::run(
&env,
doctor::Options {
path: path.clone(),
fix,
force,
show_secrets,
json: cli.json,
},
),
Command::Show {
ref path,
show_secrets,
} => show::run(
&env,
show::Options {
path: path.clone(),
show_secrets,
json: cli.json,
},
),
Command::Memory {
command:
MemoryCommand::Show {
ref path,
provider,
all,
},
} => memory::run_show(
&env,
memory::ShowOptions {
path: path.clone(),
provider,
include_unassociated: all,
json: cli.json,
},
),
Command::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "midden", &mut std::io::stdout());
return ExitCode::SUCCESS;
}
};
match result {
Ok(code) => code,
Err(e) => {
emit_error(&format!("{e:#}"), cli.json);
ExitCode::from(2)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_disables_color_in_auto_mode() {
assert_eq!(color_override(ColorMode::Auto, true), Some(false));
assert_eq!(color_override(ColorMode::Auto, false), None);
assert_eq!(color_override(ColorMode::Always, true), Some(true));
assert_eq!(color_override(ColorMode::Never, false), Some(false));
}
}