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 commands;
mod filters;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "lowfat", version)]
#[command(about = "Token-aware command filter for LLM environments")]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
/// Command to filter (e.g., lowfat git status)
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
}
#[derive(Subcommand)]
enum Commands {
// ── primary inspection commands ───────────────────────────────
/// Show current configuration, active filters, and pipelines
#[command(after_help = "\
Examples:
lowfat info # status badge + active filter list
lowfat info git # pipeline applied to `git`
lowfat info --config # full resolved config (paths, level, env)")]
Info {
/// Show pipeline for this command (e.g., git, docker)
cmd: Option<String>,
/// Show full resolved config instead of the default view
#[arg(long)]
config: bool,
},
/// Show token savings, or recent plugin executions with --audit
Stats {
/// Show recent plugin executions instead of savings summary
#[arg(long)]
audit: bool,
/// Number of audit entries (only with --audit)
#[arg(long, default_value = "20")]
audit_limit: usize,
},
/// Local usage history (powers plugin candidate ranking)
History {
/// Number of rows to show (bare form only — equivalent to `candidates --limit`)
#[arg(long, default_value = "20")]
limit: usize,
/// Include trivia rows (bare form only — equivalent to `candidates --all`)
#[arg(long)]
all: bool,
#[command(subcommand)]
action: Option<HistoryAction>,
},
// ── runtime / config ──────────────────────────────────────────
/// Get or set intensity level
Level {
/// Level to set (lite, full, ultra)
value: Option<String>,
},
// ── integrations ──────────────────────────────────────────────
/// Claude Code PreToolUse hook (reads JSON from stdin)
Hook,
/// Print shell init script for eval
ShellInit {
/// Shell type (bash, zsh, fish)
#[arg(default_value = "zsh")]
shell: String,
},
/// Manage plugins
Plugin {
#[command(subcommand)]
action: PluginAction,
},
/// Run a .lf rule file against stdin (standalone testing)
#[command(after_help = "\
Examples:
cat sample.txt | lowfat filter cargo.lf --sub=build --level=ultra
cat sample.txt | lowfat filter --explain git.lf --sub=diff > /tmp/out
lowfat filter foo.lf --sub=status --args=\"--porcelain\" < input.txt")]
Filter {
/// Path to the .lf file
path: String,
/// Subcommand context (sets $sub for the rule)
#[arg(long, default_value = "")]
sub: String,
/// Intensity level
#[arg(long, default_value = "full")]
level: String,
/// Whitespace-separated args (sets $args)
#[arg(long, default_value = "")]
args: String,
/// Print per-stage diagnostics to stderr
#[arg(long)]
explain: bool,
},
// ── hidden backward-compat aliases ────────────────────────────
// Old inspection commands keep working but are hidden from help.
// Slated for removal one release after .lf migration.
#[command(hide = true)]
Config,
#[command(hide = true)]
Filters {
/// Print only command names (one per line), for shell-init
#[arg(long)]
commands: bool,
},
#[command(hide = true)]
Gain,
#[command(hide = true)]
Status,
#[command(hide = true)]
Pipeline {
/// Command to show pipeline for (e.g., git)
cmd: String,
},
#[command(hide = true)]
Audit {
#[arg(default_value = "20")]
limit: usize,
},
}
#[derive(Subcommand)]
enum HistoryAction {
/// Rank command usage as plugin candidates
Candidates {
/// Number of rows to show
#[arg(default_value = "20")]
limit: usize,
/// Include trivia rows (avg raw <50 tok or <2 runs)
#[arg(long)]
all: bool,
},
/// Export all invocation rows as JSON to stdout (for backup / analysis)
Export,
/// Selectively delete invocation rows (does not touch lifetime gain totals)
#[command(after_help = "\
Examples:
lowfat history prune # default: --older-than 90d
lowfat history prune --older-than 30d # 30d, 2w, 3m suffixes accepted
lowfat history prune --below 2 # drop groups with fewer than 2 runs
lowfat history prune --kept-by-plugin # drop groups already covered by a plugin
lowfat history prune --all # wipe all invocation rows
lowfat history prune --dry-run [...] # preview without deleting")]
Prune {
/// Drop rows older than this duration (e.g. 30d, 2w, 3m). Default if no
/// other criterion is given: 90d.
#[arg(long, value_name = "DURATION")]
older_than: Option<String>,
/// Drop (command, subcommand) groups with fewer than N runs
#[arg(long, value_name = "N")]
below: Option<u64>,
/// Drop groups where every run was already handled by a plugin
#[arg(long)]
kept_by_plugin: bool,
/// Wipe all invocation rows
#[arg(long)]
all: bool,
/// Report what would be removed without deleting
#[arg(long)]
dry_run: bool,
},
}
#[derive(Subcommand)]
enum PluginAction {
/// List community plugins
List,
/// Check plugin dependencies
Doctor,
/// Show plugin info
Info { name: String },
/// Trust a plugin (allow execution)
Trust { name: String },
/// Revoke trust for a plugin
Untrust { name: String },
/// Benchmark a plugin against its samples
Bench { name: String },
/// Scaffold a new plugin
#[command(after_help = "\
Examples:
lowfat plugin new cargo # creates cargo-compact plugin
lowfat plugin new kubectl # creates kubectl-compact plugin
lowfat plugin new eslint -n eslint-filter # custom plugin name")]
New {
/// Command to intercept (e.g., cargo)
command: String,
/// Plugin name override (default: <command>-compact)
#[arg(short, long)]
name: Option<String>,
},
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
// ── new consolidated inspection commands ─────────────────
Some(Commands::Info { cmd, config }) => {
commands::info::run(cmd.as_deref(), config)
}
Some(Commands::Stats { audit, audit_limit }) => {
commands::stats::run(audit, audit_limit)
}
// ── kept ─────────────────────────────────────────────────
Some(Commands::History { limit, all, action }) => match action {
Some(HistoryAction::Candidates { limit, all }) => commands::candidates::run(limit, all),
Some(HistoryAction::Export) => commands::history_export::run(),
Some(HistoryAction::Prune {
older_than,
below,
kept_by_plugin,
all,
dry_run,
}) => commands::history_prune::run(commands::history_prune::PruneOpts {
older_than,
below,
kept_by_plugin,
all,
dry_run,
}),
None => commands::candidates::run(limit, all),
},
Some(Commands::Level { value }) => commands::level::run(value.as_deref()),
Some(Commands::Hook) => commands::hook::run(),
Some(Commands::ShellInit { shell }) => commands::shell_init::run(&shell),
Some(Commands::Filter {
path,
sub,
level,
args,
explain,
}) => commands::filter::run(&path, &sub, &level, &args, explain),
Some(Commands::Plugin { action }) => match action {
PluginAction::List => commands::plugin::list(),
PluginAction::Doctor => commands::plugin::doctor(),
PluginAction::Info { name } => commands::plugin::info(&name),
PluginAction::Trust { name } => commands::plugin::trust(&name),
PluginAction::Untrust { name } => commands::plugin::untrust(&name),
PluginAction::Bench { name } => commands::plugin::bench(&name),
PluginAction::New { command, name } => {
let plugin_name = name.unwrap_or_else(|| format!("{command}-compact"));
commands::plugin::new_plugin(&plugin_name, &command)
}
},
// ── hidden backward-compat aliases route to new code ─────
Some(Commands::Config) => commands::info::run(None, true),
Some(Commands::Status) => commands::info::run(None, false),
Some(Commands::Pipeline { cmd }) => commands::info::run(Some(&cmd), false),
Some(Commands::Filters { commands: cmds_only }) => {
// `--commands` is consumed by shell-init scripts; preserve its
// raw one-per-line output. Bare form is just a view of `info`.
if cmds_only {
commands::filters::run(true)
} else {
commands::info::run(None, false)
}
}
Some(Commands::Gain) => commands::stats::run(false, 20),
Some(Commands::Audit { limit }) => commands::stats::run(true, limit),
None => {
if cli.args.is_empty() {
commands::help::run();
Ok(())
} else {
let exit_code = commands::run::run(&cli.args);
std::process::exit(exit_code);
}
}
};
if let Err(e) = result {
eprintln!("lowfat: {e}");
std::process::exit(1);
}
}