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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand, ValueEnum};
use crate::mode::Mode;
/// Mode selection for which AI tool is calling rippy.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ModeArg {
Claude,
Gemini,
Cursor,
Codex,
}
impl ModeArg {
const fn to_mode(self) -> Mode {
match self {
Self::Claude => Mode::Claude,
Self::Gemini => Mode::Gemini,
Self::Cursor => Mode::Cursor,
Self::Codex => Mode::Codex,
}
}
}
/// A shell command safety hook for AI coding tools.
#[derive(Parser, Debug)]
#[command(
name = "rippy",
version,
about,
after_help = "\
Reads a JSON hook payload from stdin and writes a verdict to stdout.\n\n\
Exit codes: 0 = allow, 2 = ask/deny, 1 = error\n\n\
Get started with a safety package:\n \
rippy init # interactive package selection\n \
rippy init --package develop # skip the prompt\n \
rippy profile list # see available packages\n\n\
Packages: review (full supervision), develop (balanced), autopilot (maximum autonomy)\n\n\
Example hook usage:\n \
echo '{\"tool_name\":\"Bash\",\"tool_input\":{\"command\":\"git status\"}}' | rippy --mode claude"
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
#[command(flatten)]
pub hook_args: HookArgs,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Configure rippy as the permission engine for another tool
Setup(SetupArgs),
/// Convert a .rippy config file to .rippy.toml format
Migrate(MigrateArgs),
/// Show configured rules and trace command decisions
Inspect(InspectArgs),
/// Show aggregate decision tracking statistics
Stats(StatsArgs),
/// Add an allow rule to the config
Allow(RuleArgs),
/// Add a deny rule to the config
Deny(RuleArgs),
/// Add an ask rule to the config
Ask(RuleArgs),
/// Analyze tracking data and suggest config rules
Suggest(SuggestArgs),
/// Initialize config with a safety package (review, develop, or autopilot)
Init(InitArgs),
/// Discover flag aliases from command --help output
Discover(DiscoverArgs),
/// Manage trust for project-level config files
Trust(TrustArgs),
/// Trace the full decision path for a command
Debug(DebugArgs),
/// List safe commands, handlers, or effective rules
List(ListArgs),
/// Manage safety packages (review, develop, autopilot)
Profile(ProfileArgs),
}
#[derive(Args, Debug)]
pub struct ListArgs {
#[command(subcommand)]
pub target: ListTarget,
}
#[derive(Args, Debug)]
pub struct ProfileArgs {
#[command(subcommand)]
pub target: ProfileTarget,
}
#[derive(Subcommand, Debug)]
pub enum ProfileTarget {
/// List available safety packages
List {
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Show details of a safety package
Show {
/// Package name (review, develop, autopilot)
name: String,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Activate a safety package
Set {
/// Package name (review, develop, autopilot)
name: String,
/// Write to project config instead of global
#[arg(long)]
project: bool,
},
}
#[derive(Subcommand, Debug)]
pub enum ListTarget {
/// Show all auto-approved safe commands
Safe,
/// Show all commands with dedicated handlers
Handlers,
/// Show effective rules from all config sources
Rules(ListRulesArgs),
}
#[derive(Args, Debug)]
pub struct ListRulesArgs {
/// Filter rules by pattern
#[arg(long)]
pub filter: Option<String>,
}
#[derive(Args, Debug)]
pub struct DiscoverArgs {
/// Command and optional subcommand (e.g. "git push")
pub args: Vec<String>,
/// Re-discover all previously cached commands
#[arg(long)]
pub all: bool,
/// Output in JSON format
#[arg(long)]
pub json: bool,
}
#[derive(Args, Debug)]
pub struct InitArgs {
/// Write to global config (~/.rippy/config.toml) instead of project .rippy.toml
#[arg(long)]
pub global: bool,
/// Print stdlib to stdout instead of writing to file
#[arg(long)]
pub stdout: bool,
/// Safety package to activate (review, develop, autopilot).
/// Prompts interactively if omitted.
#[arg(long)]
pub package: Option<String>,
}
#[derive(Args, Debug)]
pub struct StatsArgs {
/// Time filter, e.g. "7d", "30d", "1h", "30m"
#[arg(long)]
pub since: Option<String>,
/// Output in JSON format
#[arg(long)]
pub json: bool,
/// Override tracking database path
#[arg(long)]
pub db: Option<PathBuf>,
}
#[derive(Args, Debug)]
pub struct RuleArgs {
/// Pattern to match (e.g. "git push *")
pub pattern: String,
/// Optional rejection/guidance message
pub message: Option<String>,
/// Write to global config (~/.rippy/config.toml) instead of project .rippy.toml
#[arg(long)]
pub global: bool,
}
#[derive(Args, Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct SuggestArgs {
/// Generate patterns from a command string instead of analyzing the DB
#[arg(long)]
pub from_command: Option<String>,
/// Time filter, e.g. "7d", "30d", "1h", "30m"
#[arg(long)]
pub since: Option<String>,
/// Output in JSON format
#[arg(long)]
pub json: bool,
/// Override tracking database path
#[arg(long)]
pub db: Option<PathBuf>,
/// Apply all suggestions to config
#[arg(long)]
pub apply: bool,
/// Write to global config (~/.rippy/config.toml) instead of project .rippy.toml
#[arg(long)]
pub global: bool,
/// Minimum number of occurrences to generate a suggestion
#[arg(long, default_value = "3")]
pub min_count: i64,
/// Use Claude Code session files (default if sessions exist, use --db to override)
#[arg(long)]
pub sessions: bool,
/// Analyze a specific session JSONL file
#[arg(long)]
pub session_file: Option<PathBuf>,
/// Audit mode: classify commands against current config
#[arg(long)]
pub audit: bool,
}
#[derive(Args, Debug)]
pub struct InspectArgs {
/// Command to trace through the decision pipeline (omit to list all rules)
pub command: Option<String>,
/// Output in JSON format
#[arg(long)]
pub json: bool,
/// Override config file path
#[arg(long, env = "RIPPY_CONFIG")]
pub config: Option<PathBuf>,
}
/// Arguments for `rippy debug` — trace the decision path for a command.
#[derive(Args, Debug)]
pub struct DebugArgs {
/// The shell command to trace (e.g. "git push --force")
pub command: String,
/// Output in JSON format
#[arg(long)]
pub json: bool,
/// Override config file path
#[arg(long, env = "RIPPY_CONFIG")]
pub config: Option<PathBuf>,
}
#[derive(Args, Debug)]
pub struct MigrateArgs {
/// Path to the config file to convert (defaults to .rippy in current directory)
pub path: Option<PathBuf>,
/// Write to stdout instead of creating .rippy.toml
#[arg(long)]
pub stdout: bool,
}
#[derive(Args, Debug)]
pub struct SetupArgs {
#[command(subcommand)]
pub target: SetupTarget,
}
#[derive(Subcommand, Debug)]
pub enum SetupTarget {
/// Configure tokf to use rippy as its external permission engine
Tokf(TokfSetupArgs),
/// Install rippy as a direct hook for Claude Code
ClaudeCode(DirectHookArgs),
/// Install rippy as a direct hook for Gemini CLI
Gemini(DirectHookArgs),
/// Install rippy as a direct hook for Cursor
Cursor(DirectHookArgs),
}
#[derive(Args, Debug)]
pub struct DirectHookArgs {
/// Install at user level (~/.claude/ etc.) instead of project level (.claude/ etc.)
#[arg(long)]
pub global: bool,
}
#[derive(Args, Debug)]
pub struct TokfSetupArgs {
/// Install at user level (~/.config/tokf/) instead of project level (.tokf/)
#[arg(long)]
pub global: bool,
/// Also install tokf hooks for these AI tools (comma-separated).
/// Supported: claude-code, opencode, codex, gemini-cli, cursor, cline,
/// windsurf, copilot, aider
#[arg(long, value_delimiter = ',')]
pub install_hooks: Vec<String>,
/// Install tokf hooks for all supported AI tools
#[arg(long)]
pub all_hooks: bool,
}
/// Arguments for `rippy trust` — manage project config trust.
#[derive(Args, Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct TrustArgs {
/// Remove trust for the current project config
#[arg(long)]
pub revoke: bool,
/// Show trust status without modifying
#[arg(long)]
pub status: bool,
/// List all trusted project configs
#[arg(long)]
pub list: bool,
/// Trust without interactive confirmation
#[arg(long, short = 'y')]
pub yes: bool,
}
/// Hook-mode arguments (the original rippy behavior).
#[derive(Args, Debug)]
pub struct HookArgs {
/// Force a specific AI tool mode
#[arg(long, value_enum)]
pub mode: Option<ModeArg>,
/// Override config file path (also reads `RIPPY_CONFIG` / `DIPPY_CONFIG` env vars)
#[arg(long, env = "RIPPY_CONFIG")]
pub config: Option<PathBuf>,
/// Remote mode (container/SSH context — skip local path validation)
#[arg(long)]
pub remote: bool,
/// Print decision trace to stderr for debugging
#[arg(long, short = 'v')]
pub verbose: bool,
}
impl HookArgs {
/// Return the explicitly forced mode, if any.
#[must_use]
pub fn forced_mode(&self) -> Option<Mode> {
self.mode.map(ModeArg::to_mode)
}
/// Resolve the config path: CLI flag > `RIPPY_CONFIG` > `DIPPY_CONFIG` env var.
#[must_use]
pub fn config_path(&self) -> Option<PathBuf> {
self.config
.clone()
.or_else(|| std::env::var_os("DIPPY_CONFIG").map(PathBuf::from))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn forced_mode_claude() {
let args = HookArgs {
mode: Some(ModeArg::Claude),
config: None,
remote: false,
verbose: false,
};
assert_eq!(args.forced_mode(), Some(Mode::Claude));
}
#[test]
fn no_forced_mode() {
let args = HookArgs {
mode: None,
config: None,
remote: false,
verbose: false,
};
assert_eq!(args.forced_mode(), None);
}
}