claude-smart 0.2.9

Cross-platform Claude Code smart session manager
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
//! Shell completion generation.
//!
//! This module uses `clap` **only** for generating completions (`csm completions
//! {zsh|bash|pwsh}`). It does not touch `csm`'s own argv — that is handled by
//! the hand-rolled parser in `cli/parser.rs`.
//!
//! The `CsmCompletionsApp` clap tree mirrors the full subcommand surface defined
//! in `main.rs`'s dispatch table. It is NEVER used to parse real argv; it exists
//! solely as a metadata source for `clap_complete::generate`.

use clap::CommandFactory;
use clap_complete::Shell;

// ─── Clap model (completions-only) ────────────────────────────────────────────
//
// Each subcommand's options are defined here so completions include the flags.
// These mirrors the hand-rolled parser in `cli/parser.rs`; keeping them in sync
// is a best-effort doc aid, not a correctness requirement (the real parser is
// authoritative).

/// Clap-derived struct used exclusively for `csm completions` — never for
/// parsing `csm run` arguments.
#[derive(clap::Parser)]
#[command(
    name = "csm",
    about = "Cross-platform Claude Code smart session manager",
    long_about = "csm — the claude-smart session launcher. Wraps `claude` with \
                  smart session selection, account auto-switching, and \
                  limit-detection relaunch."
)]
pub struct CsmCompletionsApp {
    #[command(subcommand)]
    pub command: CompletionsSubcmd,
}

#[derive(clap::Subcommand)]
pub enum CompletionsSubcmd {
    /// Launch claude (default subcommand when no subcommand is given).
    #[command(name = "run")]
    Run {
        /// Force interactive TTY mode.
        #[arg(short = 'i', long)]
        interactive: bool,
        /// Continue the newest free session.
        #[arg(short = 'c', long)]
        continue_: bool,
        /// Force an account pick even if the current account is healthy.
        #[arg(short = 'A', long = "pick-account")]
        pick_account: bool,
        /// Suppress automatic account picking.
        #[arg(long)]
        no_pick: bool,
        /// Resume a specific session by UUID or title alias.
        #[arg(short = 'r', long, value_name = "ID_OR_ALIAS")]
        resume: Option<String>,
        /// Override `--permission-mode` (forwarded to claude).
        #[arg(long, value_name = "MODE")]
        permission_mode: Option<String>,
        /// Override `--effort` (forwarded to claude).
        #[arg(long, value_name = "LEVEL")]
        effort: Option<String>,
        /// Override `--model` (forwarded to claude).
        #[arg(long, value_name = "MODEL")]
        model: Option<String>,
        /// Explicit session id (forwarded to claude as --session-id).
        #[arg(long, value_name = "UUID")]
        session_id: Option<String>,
        /// Pin a specific Claude profile (skips account picking).
        #[arg(long, value_name = "PROFILE")]
        profile: Option<String>,
        /// Extra arguments forwarded verbatim to claude (after `--`).
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        passthru: Vec<String>,
    },

    /// Stop/SubagentStop/SessionEnd hook (reads event JSON from stdin).
    #[command(name = "hook")]
    Hook {
        /// Profile directory that owns this hook instance (CLAUDE_CONFIG_DIR for this profile).
        #[arg(long, value_name = "DIR")]
        owner: Option<String>,
    },

    /// Claude-as profile switcher + registry manager (binary half; shim evals
    /// the eval-class output, calls management verbs directly).
    ///
    /// Eval-class ops (after `--`, with `--eval --shell`): `<profile>`, `-`,
    /// `-g <profile>`, `resync`, `status`.
    /// Management verbs (direct, no `--eval`): `list`, `add <name> [<dir>]`,
    /// `set <name> <dir>`, `remove|rm <name>`, `use <name>`, `edit`.
    #[command(name = "cas")]
    Cas {
        /// Emit the eval-able export line (required for profile switching).
        #[arg(long)]
        eval: bool,
        /// Shell dialect for the export line (zsh|bash|pwsh).
        #[arg(long, value_name = "SHELL")]
        shell: Option<String>,
        /// Print the resolved default CLAUDE_CONFIG_DIR and exit (floor SSOT).
        #[arg(long)]
        print_default_dir: bool,
        /// Operation and its arguments (after `--`, or a management verb).
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        op_args: Vec<String>,
    },

    /// Profile registry (human-facing noun-verb front over the cas handlers).
    #[command(name = "profiles")]
    Profiles {
        #[command(subcommand)]
        verb: Option<ProfilesVerb>,
    },

    /// csm's own global config (~/.config/claude-smart/config.json).
    #[command(name = "config")]
    Config {
        #[command(subcommand)]
        verb: Option<ConfigVerb>,
    },

    /// Multi-profile usage table (registry ∪ hub), offline-aware.
    #[command(name = "usage")]
    Usage {
        /// Emit the joined registry∪hub view as JSON.
        #[arg(long)]
        json: bool,
        /// Read only the local cache (no network).
        #[arg(long)]
        no_fetch: bool,
    },

    /// Pick the best account to launch under.
    #[command(name = "pick-account")]
    PickAccount {
        /// Current profile name (to optionally exclude from candidates).
        #[arg(value_name = "CURRENT")]
        current: Option<String>,
        /// Include the current profile in scoring; return empty on no-op.
        #[arg(long)]
        include_current: bool,
    },

    /// Scan a directory for Claude Code sessions and print TSV rows.
    #[command(name = "scan")]
    Scan {
        /// Working directory to scan (defaults to current directory).
        #[arg(value_name = "CWD")]
        cwd: Option<String>,
    },

    /// Discover and kill orphan processes left behind by a csm-managed claude session.
    #[command(name = "reap")]
    Reap {
        /// List candidates and exit without a picker or any kill.
        #[arg(long)]
        dry_run: bool,
        /// Send SIGTERM instead of the default SIGKILL (POSIX; ignored on Windows).
        #[arg(long)]
        term: bool,
        /// Inspect every csm-managed session (the default scope).
        #[arg(long)]
        all: bool,
        /// Inspect a single session by id.
        #[arg(long, value_name = "SID")]
        session: Option<String>,
    },

    /// Print session/week usage percentages for a profile.
    #[command(name = "current-usage")]
    CurrentUsage {
        /// Profile name to query.
        #[arg(value_name = "PROFILE")]
        profile: String,
    },

    /// Read/write/merge session sidecar state.
    #[command(name = "sidecar")]
    Sidecar {
        /// Operation: read | write | merge | flags
        #[arg(value_name = "OP")]
        op: String,
        /// Session UUID.
        #[arg(value_name = "SID")]
        sid: String,
        /// Key=value pairs to write/merge (for write/merge operations).
        #[arg(value_name = "KEY=VALUE")]
        kv_args: Vec<String>,
    },

    /// Print `<profile>@<host>` for shell prompt integration.
    #[command(name = "statusline")]
    Statusline,

    /// Emit shell completions for the given shell to stdout.
    #[command(name = "completions")]
    Completions {
        /// Target shell.
        shell: Shell,
    },

    /// Print a fresh lowercase UUID v4 (used as --session-id on cold launch).
    #[command(name = "newuuid")]
    Newuuid,
}

/// `csm profiles <verb>` — registry management verbs.
#[derive(clap::Subcommand)]
pub enum ProfilesVerb {
    /// List configured profiles (current/default marked).
    #[command(name = "list")]
    List,
    /// Register a new profile (dir defaults to ~/.claude.<name>).
    #[command(name = "add")]
    Add {
        /// Profile name.
        name: String,
        /// Config dir (optional; defaults to ~/.claude.<name>).
        dir: Option<String>,
    },
    /// Register/overwrite a profile's config dir.
    #[command(name = "set")]
    Set {
        /// Profile name.
        name: String,
        /// Config dir.
        dir: String,
    },
    /// Unregister a profile (refused if it is the default).
    #[command(name = "rm", alias = "remove")]
    Rm {
        /// Profile name.
        name: String,
    },
    /// Set the machine default profile (state file + platform floor).
    #[command(name = "use")]
    Use {
        /// Profile name.
        name: String,
    },
    /// Interactive editor (TTY).
    #[command(name = "edit")]
    Edit,
    /// Print a profile's config dir (default profile when omitted).
    #[command(name = "dir")]
    Dir {
        /// Profile name (default profile when omitted).
        name: Option<String>,
    },
}

/// `csm config <verb>` — global config verbs.
#[derive(clap::Subcommand)]
pub enum ConfigVerb {
    /// Print the config JSON (bare `csm config` ≡ show).
    #[command(name = "show")]
    Show,
    /// Print the resolved value of a config key.
    #[command(name = "get")]
    Get {
        /// Config key (currently: launch-command).
        key: String,
    },
    /// Set a config key. e.g. `set launch-command happy`.
    #[command(name = "set")]
    Set {
        /// Config key (currently: launch-command).
        key: String,
        /// Value tokens (the launch command argv).
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        value: Vec<String>,
    },
    /// Clear a config key (revert to default). e.g. `unset launch-command`.
    #[command(name = "unset")]
    Unset {
        /// Config key (currently: launch-command).
        key: String,
    },
}

// ─── generate ─────────────────────────────────────────────────────────────────

/// Generate completions for `shell` and write them to `out`.
///
/// Uses `CsmCompletionsApp` as the command metadata source. The `CsmCompletionsApp`
/// tree is intentionally kept in sync with `main.rs`'s dispatch table so
/// completions include all subcommands and their options.
pub fn generate(shell: Shell, out: &mut impl std::io::Write) {
    let mut cmd = CsmCompletionsApp::command();
    clap_complete::generate(shell, &mut cmd, "csm", out);
}

// ─── tests ────────────────────────────────────────────────────────────────────

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

    // ── completions output is non-empty for all shells ────────────────────────

    #[test]
    fn generate_zsh_completions_is_non_empty() {
        let mut buf = Vec::new();
        generate(Shell::Zsh, &mut buf);
        assert!(!buf.is_empty(), "zsh completions should not be empty");
    }

    #[test]
    fn generate_bash_completions_is_non_empty() {
        let mut buf = Vec::new();
        generate(Shell::Bash, &mut buf);
        assert!(!buf.is_empty(), "bash completions should not be empty");
    }

    #[test]
    fn generate_powershell_completions_is_non_empty() {
        let mut buf = Vec::new();
        generate(Shell::PowerShell, &mut buf);
        assert!(
            !buf.is_empty(),
            "powershell completions should not be empty"
        );
    }

    // ── completions include known subcommand names ────────────────────────────

    #[test]
    fn zsh_completions_mention_run_subcommand() {
        let mut buf = Vec::new();
        generate(Shell::Zsh, &mut buf);
        let out = String::from_utf8_lossy(&buf);
        assert!(
            out.contains("run") || out.contains("csm"),
            "zsh completions should reference the run subcommand or binary name"
        );
    }

    #[test]
    fn bash_completions_mention_hook_subcommand() {
        let mut buf = Vec::new();
        generate(Shell::Bash, &mut buf);
        let out = String::from_utf8_lossy(&buf);
        assert!(
            out.contains("hook"),
            "bash completions should mention 'hook' subcommand"
        );
    }

    #[test]
    fn zsh_completions_mention_completions_subcommand() {
        let mut buf = Vec::new();
        generate(Shell::Zsh, &mut buf);
        let out = String::from_utf8_lossy(&buf);
        assert!(
            out.contains("completions"),
            "zsh completions should mention 'completions' subcommand"
        );
    }

    // ── full subcommand surface is represented ────────────────────────────────

    #[test]
    fn zsh_completions_include_all_subcommands() {
        let mut buf = Vec::new();
        generate(Shell::Zsh, &mut buf);
        let out = String::from_utf8_lossy(&buf);
        // All subcommands from the dispatch table.
        for sub in &[
            "run",
            "hook",
            "profiles",
            "config",
            "usage",
            "cas",
            "pick-account",
            "scan",
            "reap",
            "current-usage",
            "sidecar",
            "statusline",
            "completions",
            "newuuid",
        ] {
            assert!(
                out.contains(sub),
                "zsh completions missing subcommand {sub:?}"
            );
        }
    }

    // ── generate is idempotent (called twice produces the same output) ────────

    #[test]
    fn generate_is_idempotent() {
        let mut buf1 = Vec::new();
        let mut buf2 = Vec::new();
        generate(Shell::Zsh, &mut buf1);
        generate(Shell::Zsh, &mut buf2);
        assert_eq!(
            buf1, buf2,
            "repeated generate calls must produce identical output"
        );
    }
}