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
mod backup;
mod claude_json;
mod doctor;
mod git;
mod memory;
mod orphans;
mod output;
mod paths;
mod process;
mod prune;
mod secrets;
mod show;
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,
}
#[derive(Parser)]
#[command(
name = "midden",
about = "Resolve, audit, and clean coding-agent context and accumulated 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,
},
}
fn main() -> ExitCode {
let cli = Cli::parse();
match cli.color {
ColorMode::Always => control::set_override(true),
ColorMode::Never => control::set_override(false),
ColorMode::Auto => {
if cli.json {
control::set_override(false);
}
}
}
let env = paths::Env::new(cli.config.clone(), cli.claude_home.clone())
.with_codex_home(cli.codex_home.clone());
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) => {
if cli.json {
let err = serde_json::json!({ "error": format!("{e:#}") });
eprintln!("{err}");
} else {
eprintln!("error: {e:#}");
}
ExitCode::from(2)
}
}
}