lean-ctx 3.12.1

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// SPDX-License-Identifier: Apache-2.0
use lean_ctx_hooks::hooks::to_bash_compatible_path;

pub(crate) fn quiet_enabled() -> bool {
    lean_ctx::core::runtime_flags::quiet_enabled()
}

macro_rules! qprintln {
    ($($t:tt)*) => {
        if !quiet_enabled() {
            println!($($t)*);
        }
    };
}

pub fn cmd_init(args: &[String]) {
    // Safety (#476 class, #1849): asking about init must never *run* it —
    // `init --agent claude --help` used to write the agent's rules file. The
    // guard sits here rather than in the dispatcher so every entry point
    // (`cmd_init_quiet`, `doctor --fix`) inherits it.
    if args.iter().any(|a| a == "--help" || a == "-h") {
        print_init_help();
        return;
    }
    // Opt-in value surfaces: each is its own step, never part of the alias setup.
    if let Some(enable) = opt_in_flag(args, "--prompt") {
        let binary = lean_ctx::core::portable_binary::stable_shell_binary(
            &lean_ctx::core::portable_binary::resolve_portable_binary(),
        );
        super::prompt_init::cmd_init_prompt(enable, &binary);
        return;
    }
    if let Some(enable) = opt_in_flag(args, "--git-trailer") {
        super::git_trailer::cmd_init_git_trailer(enable);
        return;
    }
    let global = args.iter().any(|a| a == "--global" || a == "-g");
    let project = args.iter().any(|a| a == "--project");
    let dry_run = args.iter().any(|a| a == "--dry-run");
    let no_hook = args.iter().any(|a| a == "--no-shell-hook")
        || lean_ctx::core::config::Config::load().shell_hook_disabled_effective();

    let explicit_mode = args
        .windows(2)
        .find(|w| w[0] == "--mode")
        .and_then(|w| lean_ctx_hooks::hooks::HookMode::from_str_loose(&w[1]));
    let governor_arm = args.windows(2).find(|w| w[0] == "--mode").and_then(|w| {
        lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm::from_mode(&w[1])
    });

    if args.windows(2).any(|w| w[0] == "--mode")
        && explicit_mode.is_none()
        && governor_arm.is_none()
    {
        let bad = args
            .windows(2)
            .find(|w| w[0] == "--mode")
            .map_or("?", |w| w[1].as_str());
        eprintln!(
            "Unknown hook mode: '{bad}'. Valid: mcp, hybrid, replace, governor, governor-mcp"
        );
        std::process::exit(1);
    }

    let agents: Vec<&str> = args
        .windows(2)
        .filter(|w| w[0] == "--agent")
        .map(|w| w[1].as_str())
        .collect();

    // `--dry-run` with `--agent` must write nothing: the agent and governor
    // paths below run before the shell-hook dry run (reimplements community
    // PR #2169 by ZacKienzle2).
    if dry_run && !agents.is_empty() {
        let mode = args
            .windows(2)
            .find(|w| w[0] == "--mode")
            .map_or("default", |w| w[1].as_str());
        qprintln!("\nlean-ctx init --dry-run\n");
        qprintln!("  Would configure: {} (mode: {mode})", agents.join(", "));
        qprintln!("\n  Run without --dry-run to apply.");
        return;
    }

    if let Some(arm) = governor_arm {
        init_governor(&agents, arm);
        return;
    }
    // An explicit standard mode is a pinned choice (no later update switches
    // it to the governor) and switches a governor install back first.
    if let Some(mode) = explicit_mode
        && let Some(home) = lean_ctx::core::home::resolve_home_dir()
    {
        let mode = mode.to_string().to_ascii_lowercase();
        for agent in &agents {
            let Some(host) = lean_ctx_hooks::hooks::governor_default::host(agent) else {
                continue;
            };
            lean_ctx_hooks::hooks::governor_default::pin(host, Some(&mode));
            let reverted = if host.key == "claude" {
                lean_ctx_hooks::hooks::agents::claude_governor::revert(&home)
            } else {
                lean_ctx_hooks::hooks::agents::governor_hosts::governor_available(agent)
                    .is_some_and(|spec| (spec.revert)(&home))
            };
            if reverted {
                qprintln!("  ✓ governor setup reverted for {}", host.name);
            }
        }
    }

    if !agents.is_empty() {
        let cwd = std::env::current_dir().unwrap_or_default();
        for agent_name in &agents {
            if explicit_mode.is_none() && init_governor_default(agent_name) {
                continue;
            }
            let mode = explicit_mode
                .unwrap_or_else(|| lean_ctx_hooks::hooks::recommend_hook_mode(agent_name));
            let result = crate::setup::setup_single_agent(agent_name, global, mode);
            for name in &result.rules.injected {
                qprintln!("  ✓ {name} rules injected");
            }
            for name in &result.rules.updated {
                qprintln!("  ✓ {name} rules updated");
            }
            for name in &result.rules.already {
                qprintln!("  ✓ {name} rules up-to-date");
            }
            if result.skill_installed {
                qprintln!("  ✓ SKILL.md installed for {agent_name}");
            }
            if result.mcp_skipped {
                qprintln!("  • MCP registration skipped for {agent_name} (auto_update_mcp=false)");
            }
            for e in &result.errors {
                eprintln!("  ✗ {agent_name}: {e}");
            }
            if result.errors.is_empty()
                && let Err(e) = crate::setup::agent_selection::remember_connected_agent(agent_name)
            {
                eprintln!("  ✗ {agent_name}: {e}");
            }
            if agent_name.eq_ignore_ascii_case("hermes") {
                qprintln!("\n  Beyond MCP, lean-ctx can be Hermes' active context engine");
                qprintln!("  (replaces the built-in ContextCompressor). Install the plugin from");
                qprintln!("  integrations/hermes-lean-ctx (scripts/install.sh), then set");
                qprintln!("  context.engine: \"lean-ctx\" in ~/.hermes/config.yaml.");
            }
            if project {
                lean_ctx_hooks::hooks::install_agent_project_hooks(agent_name, &cwd);
            }
        }
        if !global {
            lean_ctx_hooks::hooks::install_project_rules_for_agents(&agents);
        }
        qprintln!("\nRun 'lean-ctx cockpit' after using some commands to see your savings.");
        return;
    }

    let eval_shell = args
        .iter()
        .find(|a| matches!(a.as_str(), "bash" | "zsh" | "fish" | "powershell" | "pwsh"));
    if let Some(shell) = eval_shell
        && !global
    {
        super::shell_init::print_hook_stdout(shell);
        return;
    }

    let shell_name = std::env::var("SHELL").unwrap_or_default();
    let is_zsh = shell_name.contains("zsh");
    let is_fish = shell_name.contains("fish");
    let is_powershell = cfg!(windows) && shell_name.is_empty();

    // Shell hooks outlive the build that wrote them: embed the stable PATH
    // entry, not a versioned package-manager directory (#1851).
    let binary = lean_ctx::core::portable_binary::stable_shell_binary(
        &lean_ctx::core::portable_binary::resolve_portable_binary(),
    );

    if dry_run {
        let rc = if is_powershell {
            dirs::home_dir().map_or_else(
                || "PowerShell profile".to_string(),
                |h| {
                    lean_ctx::shell::platform::resolve_powershell_profile_path(&h)
                        .to_string_lossy()
                        .into_owned()
                },
            )
        } else if is_fish {
            "~/.config/fish/config.fish".to_string()
        } else if is_zsh {
            "~/.zshrc".to_string()
        } else {
            "~/.bashrc".to_string()
        };
        qprintln!("\nlean-ctx init --dry-run\n");
        qprintln!("  Would modify:  {rc}");
        qprintln!("  Would backup:  {rc}.lean-ctx.bak");
        qprintln!("  Would alias:   git npm pnpm yarn cargo docker docker-compose kubectl");
        qprintln!("                 gh pip pip3 ruff go golangci-lint eslint prettier tsc");
        qprintln!("                 curl wget php composer (24 commands + k)");
        let data_dir = lean_ctx::core::data_dir::lean_ctx_data_dir().map_or_else(
            |_| "~/.config/lean-ctx/".to_string(),
            |p| p.to_string_lossy().to_string(),
        );
        qprintln!("  Would create:  {data_dir}");
        qprintln!("  Binary:        {binary}");
        qprintln!("\n  Safety: aliases auto-fallback to original command if lean-ctx is removed.");
        qprintln!("\n  Run without --dry-run to apply.");
        return;
    }

    if no_hook {
        qprintln!("Shell hook disabled (--no-shell-hook or shell_hook_disabled config).");
        qprintln!("MCP tools remain active. Set LEAN_CTX_NO_HOOK=1 to disable at runtime.");
    } else if is_powershell {
        super::shell_init::init_powershell(&binary);
    } else {
        let bash_binary = to_bash_compatible_path(&binary);
        if is_fish {
            super::shell_init::init_fish(&bash_binary);
        } else {
            super::shell_init::init_posix(is_zsh, &bash_binary);
        }
    }

    if let Ok(lean_dir) = lean_ctx::core::data_dir::lean_ctx_data_dir()
        && !lean_dir.exists()
    {
        let _ = std::fs::create_dir_all(&lean_dir);
        qprintln!("Created {}", lean_dir.display());
    }

    let rc = if is_powershell {
        "$PROFILE"
    } else if is_fish {
        "config.fish"
    } else if is_zsh {
        ".zshrc"
    } else {
        ".bashrc"
    };

    qprintln!("\nlean-ctx init complete (24 aliases installed)");
    qprintln!();
    qprintln!("  Disable temporarily:  lean-ctx-off");
    qprintln!("  Re-enable:            lean-ctx-on");
    qprintln!("  Check status:         lean-ctx-status");
    qprintln!("  Full uninstall:       lean-ctx uninstall");
    qprintln!("  Diagnose issues:      lean-ctx doctor");
    qprintln!("  Preview changes:      lean-ctx init --global --dry-run");
    qprintln!();
    if is_powershell {
        qprintln!("  Restart PowerShell or run: . {rc}");
    } else {
        qprintln!("  Restart your shell or run: source ~/{rc}");
    }
    qprintln!();
    qprintln!("For AI tool integration: lean-ctx init --agent <tool> [--mode <mode>]");
    qprintln!("  Supported: aider, amazonq, amp, antigravity, antigravity-cli, augment,");
    qprintln!(
        "    claude, cline, codewhale, codex, commandcode, continue, copilot, crush, cursor, emacs, gemini,"
    );
    qprintln!("    grok, hermes, jetbrains, kiro, neovim, omp, openclaw, opencode, pi,");
    qprintln!("    qoder, qodercli, qoderwork, qwen, roo, sublime, trae, verdent, vscode,");
    qprintln!("    windsurf, zed");
    qprintln!("  Modes: mcp, hybrid, replace  (auto-detected per agent, override with --mode)");
    qprintln!("  Claude Code defaults to governor (no MCP server); MCP tools: --mode hybrid");
}

pub fn cmd_init_quiet(args: &[String]) {
    let _quiet_guard = lean_ctx::core::runtime_flags::scoped_quiet();
    cmd_init(args);
}

/// `--flag` → enable, `--flag off` / `--flag=off` → disable, absent → `None`.
fn opt_in_flag(args: &[String], flag: &str) -> Option<bool> {
    let pos = args
        .iter()
        .position(|a| a == flag || a.starts_with(&format!("{flag}=")))?;
    let value = args[pos]
        .strip_prefix(&format!("{flag}="))
        .or_else(|| args.get(pos + 1).map(String::as_str));
    Some(!matches!(value, Some("off" | "false" | "0")))
}

fn is_claude_agent(agent: &str) -> bool {
    matches!(
        agent.to_ascii_lowercase().as_str(),
        "claude" | "claude-code"
    )
}

/// `init --agent <host> --mode governor|governor-mcp`: the zero-overhead
/// governor profile (`governor-mcp` is Claude Code only).
fn init_governor(
    agents: &[&str],
    arm: lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm,
) {
    let Some(home) = lean_ctx::core::home::resolve_home_dir() else {
        eprintln!("  ✗ cannot resolve the home directory");
        std::process::exit(1);
    };
    if agents.is_empty() {
        eprintln!("  ✗ --mode governor needs --agent <host>");
        std::process::exit(1);
    }
    for agent in agents {
        if !is_claude_agent(agent) {
            init_host_governor(agent, arm, &home);
            continue;
        }
        lean_ctx_hooks::hooks::agents::claude_governor::install(&home, arm);
        if let Some(host) = lean_ctx_hooks::hooks::governor_default::host(agent) {
            lean_ctx_hooks::hooks::governor_default::pin(host, None);
        }
        qprintln!("  ✓ Claude Code governor setup installed (revert: --mode hybrid)");
        if let Err(e) = crate::setup::agent_selection::remember_connected_agent(agent) {
            eprintln!("  ✗ {agent}: {e}");
        }
    }
}

/// `init --agent <host>` without `--mode` on a host with a governor: apply the
/// default (new install or once-per-version migration). Returns whether the
/// host is handled; `false` (pinned, or switched back this version) runs the
/// standard setup.
fn init_governor_default(agent: &str) -> bool {
    use lean_ctx_hooks::hooks::governor_default::{Outcome, apply_default, host};
    let (Some(host), Some(home)) = (host(agent), lean_ctx::core::home::resolve_home_dir()) else {
        return false;
    };
    let outcome = apply_default(host, &home);
    if outcome.ran_steps() {
        let _ = crate::setup::agent_selection::remember_connected_agent(host.key);
    }
    if let Some(notice) = outcome.notice(host) {
        qprintln!("  ✓ {notice}");
    } else if outcome == Outcome::AlreadyGovernor {
        (host.refresh)(&home);
        qprintln!("  ✓ {} governor setup up to date", host.name);
    }
    !matches!(outcome, Outcome::Pinned(_) | Outcome::SkippedThisVersion)
}

/// `init --agent <host> --mode governor` for a host other than Claude Code.
fn init_host_governor(
    agent: &str,
    arm: lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm,
    home: &std::path::Path,
) {
    use lean_ctx_hooks::hooks::agents::{claude_governor::GovernorArm, governor_hosts};
    let Some(spec) = governor_hosts::governor_available(agent) else {
        eprintln!("  • {agent}: no tool hooks LeanCTX can govern; keeps the MCP setup");
        return;
    };
    if arm == GovernorArm::McpDeferred {
        eprintln!("  • {agent}: governor-mcp is Claude Code only; use --mode governor");
        return;
    }
    governor_hosts::install(spec, home);
    qprintln!(
        "  ✓ {} {} setup installed (switch back: {})",
        spec.name,
        spec.class.label(),
        spec.switch_back
    );
    if let Err(e) = crate::setup::agent_selection::remember_connected_agent(agent) {
        eprintln!("  ✗ {agent}: {e}");
    }
}

/// Help for `lean-ctx init`. Printed for `--help`/`-h`, whatever else is on
/// the line, and never followed by an init.
fn print_init_help() {
    println!("Usage: lean-ctx init [options]");
    println!("       lean-ctx init <bash|zsh|fish|powershell|pwsh>");
    println!();
    println!("Installs the shell aliases, or connects an AI tool with --agent.");
    println!("The second form prints the shell hook to stdout for `eval` and writes nothing.");
    println!();
    println!("Options:");
    println!("  --global, -g        Install the shell aliases into your shell profile");
    println!("  --agent <tool>      Configure an AI tool: MCP, hooks and rules (repeatable)");
    println!("  --mode <mode>       Hook mode for --agent: mcp, hybrid or replace");
    println!("                      (auto-detected per agent when omitted). Claude Code,");
    println!("                      CodeBuddy, Cursor, Codex, Gemini and Copilot CLI default");
    println!("                      to governor (native tools + hooks, no MCP); an explicit");
    println!("                      mode there is kept by later updates (Claude Code also:");
    println!("                      governor-mcp)");
    println!("  --project           With --agent: also install project-local hooks");
    println!("  --no-shell-hook     Skip the shell aliases; MCP tools stay active");
    println!("  --dry-run           Show what the shell setup would change, change nothing");
    println!("  --prompt [off]      Show what lean-ctx did in your shell prompt (zsh, bash,");
    println!("                      fish; prints a module for Starship). `off` removes it");
    println!("  --git-trailer [off] Add a `lean-ctx:` trailer to this repo's commit messages.");
    println!("                      `off` removes the hook");
    println!("  --help, -h          Show this help (never runs init)");
    println!();
    println!("Examples:");
    println!("  lean-ctx init --global --dry-run");
    println!("  lean-ctx init --global");
    println!("  lean-ctx init --prompt");
    println!("  lean-ctx init --agent claude");
    println!("  lean-ctx init --agent codex --mode hybrid");
}

#[cfg(test)]
mod tests {
    use super::opt_in_flag;

    fn args(a: &[&str]) -> Vec<String> {
        a.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn opt_in_flags_parse_on_and_off() {
        assert_eq!(opt_in_flag(&args(&["--prompt"]), "--prompt"), Some(true));
        assert_eq!(
            opt_in_flag(&args(&["--prompt", "off"]), "--prompt"),
            Some(false)
        );
        assert_eq!(
            opt_in_flag(&args(&["--prompt=false"]), "--prompt"),
            Some(false)
        );
        assert_eq!(
            opt_in_flag(&args(&["--git-trailer", "0"]), "--git-trailer"),
            Some(false)
        );
        assert_eq!(opt_in_flag(&args(&["--global"]), "--prompt"), None);
        // `--prompt-foo` is not `--prompt`.
        assert_eq!(opt_in_flag(&args(&["--prompt-foo"]), "--prompt"), None);
    }

    /// `init --mode hybrid` after a governor migration restores the standard
    /// setup and pins it, so the next update leaves it alone.
    #[test]
    fn revert_restores_the_standard_setup_pinned() {
        use lean_ctx::test_env;
        use lean_ctx_hooks::hooks::agents::claude_governor;
        use lean_ctx_hooks::hooks::governor_default::{Outcome, apply_default, host, pinned_mode};

        let _iso = lean_ctx::core::data_dir::isolated_data_dir();
        let home = tempfile::tempdir().unwrap();
        let prev_home = std::env::var_os("HOME");
        test_env::set_var("HOME", home.path());
        test_env::set_var("LEAN_CTX_HOOK_BINARY", "lean-ctx");
        for var in ["CLAUDE_CONFIG_DIR", "LEAN_CTX_HOOK_MODE", "LEAN_CTX_QUIET"] {
            test_env::remove_var(var);
        }
        let h = home.path();
        let read = |p: &str| std::fs::read_to_string(h.join(p)).unwrap_or_default();
        let mcp = || serde_json::from_str::<serde_json::Value>(&read(".claude.json")).unwrap();
        // A pre-governor standard install: MCP entry and rules block below
        // the user's own text.
        std::fs::create_dir_all(h.join(".claude")).unwrap();
        std::fs::write(
            h.join(".claude.json"),
            r#"{"projects":{"/x":{}},"mcpServers":{"lean-ctx":{"command":"lean-ctx","alwaysLoad":true},"other":{"command":"o"}}}"#,
        )
        .unwrap();
        std::fs::write(
            h.join(".claude/CLAUDE.md"),
            "# My notes\nkeep me\n\n<!-- lean-ctx -->\nstandard block: use ctx_read\n<!-- /lean-ctx -->\n",
        )
        .unwrap();

        let claude = host("claude").unwrap();
        assert!(matches!(apply_default(claude, h), Outcome::Migrated(_)));
        super::cmd_init_quiet(&args(&[
            "--agent", "claude", "--mode", "hybrid", "--global",
        ]));

        assert!(!claude_governor::is_installed(h));
        let settings = read(".claude/settings.json");
        assert!(settings.contains("lean-ctx hook rewrite"), "{settings}");
        assert!(!settings.contains("hook govern"), "{settings}");
        assert!(mcp().pointer("/mcpServers/lean-ctx").is_some(), "MCP back");
        assert!(read(".claude/CLAUDE.md").starts_with("# My notes\nkeep me\n"));
        assert_eq!(pinned_mode(claude).as_deref(), Some("hybrid"));
        // The next update leaves the pinned choice alone.
        let before = (read(".claude/settings.json"), read(".claude/CLAUDE.md"));
        assert_eq!(apply_default(claude, h), Outcome::Pinned("hybrid".into()));
        assert_eq!(
            (read(".claude/settings.json"), read(".claude/CLAUDE.md")),
            before
        );

        test_env::remove_var("LEAN_CTX_HOOK_BINARY");
        match prev_home {
            Some(p) => test_env::set_var("HOME", p),
            None => test_env::remove_var("HOME"),
        }
    }
}