scv-tools 0.1.35

Workspace-scoped filesystem, process, skill, and agent tools for SCV
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! The native agent CLIs SCV can delegate to, one descriptor each.
//!
//! A descriptor is the whole integration: the default command line, where the
//! CLI keeps its state inside SCV's private adapter home, which inherited
//! variables it must never see, and how `scv agents login|status|logout`
//! handle it. Adding an agent means adding one entry to [`ADAPTERS`].

use std::{
    ffi::OsStr,
    path::{Path, PathBuf},
};

/// How SCV signs an agent in, inside its private adapter home.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Login {
    /// Run the CLI's own sign-in command.
    Command(&'static [&'static str]),
    /// Open the CLI interactively; `hint` names its in-app sign-in command.
    Interactive {
        args: &'static [&'static str],
        hint: &'static str,
    },
    /// Prompt for an API key and store it in the CLI's own credential file.
    ApiKey(KeyStore),
    /// Copy SCV's own configuration: `scv agents import <name>`.
    Import,
}

/// How SCV reports whether an agent is signed in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
    /// The CLI prints its own status and exits non-zero when signed out.
    Command(&'static [&'static str]),
    /// SCV inspects the CLI's credential file without printing secrets.
    Stored(KeyStore),
}

/// What a CLI prints on stdout when SCV runs it, and so how SCV reads its
/// reply, usage, and failure out of it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputFormat {
    /// Plain text: stdout is the reply.
    Text,
    /// Claude Code `--output-format stream-json --verbose`: one JSON event per
    /// line, ending with a `result` event.
    ClaudeStreamJson,
    /// `codex exec --json`: JSON events per line; SCV also passes `-o <file>`
    /// so the final message survives an unparsable stream.
    CodexJsonl,
    /// pi `--mode json`: JSON events per line; the reply is the last
    /// assistant `message_end`.
    PiJson,
}

impl OutputFormat {
    /// Arguments that select this format, placed after the fixed arguments.
    pub fn args(self) -> &'static [&'static str] {
        match self {
            Self::Text => &[],
            Self::ClaudeStreamJson => &["--output-format", "stream-json", "--verbose"],
            Self::CodexJsonl => &["--json"],
            Self::PiJson => &["--mode", "json"],
        }
    }
}

/// How SCV talks to an agent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transport {
    /// One CLI process per turn: the prompt is an argument and the reply is
    /// read from its output ([`OutputFormat`]), continued through [`Resume`].
    Process,
    /// A long-running `scv server --stdio` per conversation, driven over the
    /// SCV protocol: its tool approvals are relayed to the calling session
    /// and its events become progress.
    ScvProtocol,
}

impl Transport {
    /// Whether one child process lives for a whole conversation.
    pub fn is_live(self) -> bool {
        !matches!(self, Self::Process)
    }
}

/// How to start an agent's Agent Client Protocol (ACP) server: a long-running
/// process speaking JSON-RPC 2.0 over stdio, one conversation per ACP session.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AcpLaunch {
    /// The ACP server executable: the agent itself or its official adapter.
    pub command: &'static str,
    /// Its arguments; a `{full}` entry is replaced by `full_args` for
    /// `permissions = "full"` and dropped otherwise.
    pub args: &'static [&'static str],
    pub full_args: &'static [&'static str],
    /// The ACP session mode selected for `permissions = "full"`, for agents
    /// whose permission level is a session mode.
    pub full_mode: Option<&'static str>,
    /// Environment for the ACP server under `permissions = "full"`, for
    /// settings the server reads only from its environment.
    pub full_environment: &'static [(&'static str, &'static str)],
}

/// Expand `launch.args` for the configured permission level.
pub fn acp_args(launch: &AcpLaunch, full: bool) -> Vec<String> {
    let mut args = Vec::with_capacity(launch.args.len() + launch.full_args.len());
    for arg in launch.args {
        if *arg == "{full}" {
            if full {
                args.extend(launch.full_args.iter().map(|arg| (*arg).to_owned()));
            }
        } else {
            args.push((*arg).to_owned());
        }
    }
    args
}

/// How a CLI continues an earlier conversation. `{session}` in any argument
/// is replaced by the conversation's vendor session ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Resume {
    /// Every call starts a fresh conversation.
    Unsupported,
    Supported {
        /// Starts a conversation under an ID SCV chooses. Empty when the CLI
        /// picks its own ID and reports it in its output (Codex).
        start: &'static [&'static str],
        /// Placed right after the fixed arguments when continuing: a
        /// subcommand such as Codex's `exec resume`.
        subcommand: &'static [&'static str],
        /// Options that continue the conversation.
        options: &'static [&'static str],
        /// Placed immediately before the prompt when continuing, for a CLI
        /// that takes the session ID as a positional argument.
        positional: &'static [&'static str],
    },
}

impl Resume {
    pub fn is_supported(self) -> bool {
        matches!(self, Self::Supported { .. })
    }

    /// Whether SCV chooses the vendor session ID when a conversation starts.
    pub fn assigns_id(self) -> bool {
        matches!(self, Self::Supported { start, .. } if !start.is_empty())
    }
}

/// Where a CLI keeps conversation transcripts inside its adapter home:
/// files with `extension` anywhere below `dir`, named after their session ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConversationFiles {
    pub dir: &'static str,
    pub extension: &'static str,
}

/// How SCV condenses a CLI's own status output. The raw output names the
/// account (an email) or part of a key, so it is never printed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusSummary {
    /// `claude auth status` JSON: `loggedIn`, `authMethod`, `subscriptionType`.
    ClaudeJson,
    /// `codex login status` text: "Logged in using an API key" or "ChatGPT".
    CodexText,
    /// The exit status alone.
    ExitStatus,
}

/// How SCV signs an agent out.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Logout {
    Command(&'static [&'static str]),
    /// SCV removes the credentials it can see in the CLI's own files.
    Stored(KeyStore),
}

/// A CLI's native credential file, relative to the adapter home.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyStore {
    /// Grok: sign-ins from `grok login` in `auth` (a JSON object of entries),
    /// or an API key in the `config` profile of its default model.
    Grok {
        auth: &'static str,
        config: &'static str,
    },
    /// DeepSeek Harness `.credentials.yaml`, holding `refs.<variable>`.
    DshRefs {
        path: &'static str,
        variable: &'static str,
    },
    /// pi's agent directory: `auth.json`, plus the SCV-configured
    /// OpenAI-compatible endpoint in `models.json` and `settings.json`.
    Pi { dir: &'static str },
    /// A nested SCV's own `config.toml`, holding the provider copied from the
    /// user's SCV by `scv agents import scv`.
    Scv { config: &'static str },
}

#[derive(Debug, Clone, Copy)]
pub struct AdapterDescriptor {
    /// Short name: the tool is `agent_<name>` and the home `adapters/<name>`.
    pub name: &'static str,
    /// Product name for messages.
    pub product: &'static str,
    pub command: &'static str,
    pub args: &'static [&'static str],
    /// Placed immediately before the prompt, for CLIs whose prompt is a flag
    /// value (`grok -p <prompt>`).
    pub prompt_args: &'static [&'static str],
    pub model_args: &'static [&'static str],
    pub effort_args: &'static [&'static str],
    /// Describes the `model` argument for the calling model.
    pub model_hint: &'static str,
    /// Variables pointing the CLI's state into the adapter home, as paths
    /// relative to it (`""` is the home itself).
    pub home_environment: &'static [(&'static str, &'static str)],
    /// Fixed variables for every delegated run.
    pub fixed_environment: &'static [(&'static str, &'static str)],
    /// Credential, endpoint, and state-location variables no delegated agent
    /// inherits. A trailing `*` matches a prefix.
    pub removed_environment: &'static [&'static str],
    /// Added after `args` when `[agents.<name>] permissions = "full"`: the
    /// CLI's own switches that turn off its approval prompts and sandbox and
    /// enable web search where the CLI gates it. Empty when the CLI has no
    /// permission system of its own.
    pub full_permission_args: &'static [&'static str],
    /// Variables set for `permissions = "full"`, for CLIs configured that way.
    pub full_permission_environment: &'static [(&'static str, &'static str)],
    /// Per-user install directories searched before `PATH`, relative to the
    /// user's home, as a login shell orders them. A user service's `PATH`
    /// omits them, so without this the daemon would miss or pick a different
    /// install than the user's shell.
    pub search_dirs: &'static [&'static str],
    pub login: Login,
    pub status: Status,
    /// How a [`Status::Command`] result is summarized.
    pub status_summary: StatusSummary,
    pub logout: Logout,
    /// What the CLI prints when SCV delegates to it.
    pub output: OutputFormat,
    /// How SCV continues a conversation with it, when it can.
    pub resume: Resume,
    /// Transcripts `scv agents gc` may remove; `None` when unknown.
    pub conversation_files: Option<ConversationFiles>,
    /// How SCV talks to the agent.
    pub transport: Transport,
    /// Its ACP server, when it has a verified one. With `[agents.<name>]
    /// transport = "auto"` SCV prefers it over [`Transport::Process`] once the
    /// command is installed.
    pub acp: Option<AcpLaunch>,
}

/// Directories every adapter searches before `PATH`, relative to the user's home.
const USER_BIN_DIRS: &[&str] = &[".local/bin"];

/// Removed from every agent regardless of adapter: SCV's own selectors and
/// cloud keys that name no single agent. Any variable ending in `_API_KEY`
/// is removed as well.
const COMMON_REMOVED_ENVIRONMENT: &[&str] = &[
    "SCV_CONFIG",
    "SCV_MODEL",
    "SCV_PROVIDER",
    "SCV_BASE_URL",
    "SCV_API_KEY_ENV",
    "GEMINI_API_KEY",
    "GOOGLE_API_KEY",
    "AZURE_OPENAI_API_KEY",
    "AZURE_OPENAI_ENDPOINT",
];

const PI_STORE: KeyStore = KeyStore::Pi { dir: ".pi/agent" };
const SCV_STORE: KeyStore = KeyStore::Scv {
    config: "config.toml",
};
const DSH_STORE: KeyStore = KeyStore::DshRefs {
    path: ".dsh/.credentials.yaml",
    variable: "DEEPSEEK_API_KEY",
};

pub const ADAPTERS: &[AdapterDescriptor] = &[
    AdapterDescriptor {
        name: "claude",
        product: "Claude Code",
        command: "claude",
        args: &["-p"],
        prompt_args: &[],
        model_args: &["--model", "{model}"],
        effort_args: &["--effort", "{effort}"],
        model_hint: "Claude model alias or ID, such as sonnet or opus.",
        home_environment: &[],
        fixed_environment: &[],
        removed_environment: &[
            "ANTHROPIC_API_KEY",
            "ANTHROPIC_BASE_URL",
            "ANTHROPIC_AUTH_TOKEN",
            "CLAUDE_CODE_OAUTH_TOKEN",
            "CLAUDE_CONFIG_DIR",
        ],
        // Also allows WebSearch and WebFetch without prompting.
        full_permission_args: &["--permission-mode", "bypassPermissions"],
        full_permission_environment: &[],
        search_dirs: &[],
        login: Login::Command(&["auth", "login"]),
        status: Status::Command(&["auth", "status"]),
        status_summary: StatusSummary::ClaudeJson,
        logout: Logout::Command(&["auth", "logout"]),
        output: OutputFormat::ClaudeStreamJson,
        // `--resume` in print mode keeps the original session ID.
        resume: Resume::Supported {
            start: &["--session-id", "{session}"],
            subcommand: &[],
            options: &["--resume", "{session}"],
            positional: &[],
        },
        conversation_files: Some(ConversationFiles {
            dir: ".claude/projects",
            extension: "jsonl",
        }),
        transport: Transport::Process,
        // The official adapter from the ACP organisation (npm
        // @agentclientprotocol/claude-agent-acp), on the Claude Agent SDK.
        acp: Some(AcpLaunch {
            command: "claude-agent-acp",
            args: &[],
            full_args: &[],
            full_mode: Some("bypassPermissions"),
            full_environment: &[],
        }),
    },
    AdapterDescriptor {
        name: "codex",
        product: "Codex",
        command: "codex",
        args: &["exec"],
        prompt_args: &[],
        model_args: &["-m", "{model}"],
        effort_args: &["-c", "model_reasoning_effort=\"{effort}\""],
        model_hint: "OpenAI model ID from the Codex configuration; not a Claude alias.",
        home_environment: &[("CODEX_HOME", "")],
        fixed_environment: &[],
        removed_environment: &[
            "OPENAI_API_KEY",
            "OPENAI_BASE_URL",
            "OPENAI_ORG_ID",
            "OPENAI_PROJECT_ID",
            "CODEX_API_KEY",
            "CODEX_BASE_URL",
            "CODEX_CONFIG",
        ],
        // `codex exec` has no `--search`; `web_search = "live"` is its config form.
        full_permission_args: &[
            "--dangerously-bypass-approvals-and-sandbox",
            "-c",
            "web_search=\"live\"",
        ],
        full_permission_environment: &[],
        search_dirs: &[],
        login: Login::Command(&["login"]),
        status: Status::Command(&["login", "status"]),
        status_summary: StatusSummary::CodexText,
        logout: Logout::Command(&["logout"]),
        output: OutputFormat::CodexJsonl,
        // The thread ID arrives in `thread.started`; `exec resume` takes it
        // as a positional argument before the prompt.
        resume: Resume::Supported {
            start: &[],
            subcommand: &["resume"],
            options: &[],
            positional: &["{session}"],
        },
        conversation_files: Some(ConversationFiles {
            dir: "sessions",
            extension: "jsonl",
        }),
        transport: Transport::Process,
        // The official adapter from the ACP organisation (npm
        // @agentclientprotocol/codex-acp). It reads `$CODEX_HOME/config.toml`
        // and takes no `-c` overrides; `CODEX_CONFIG` is its JSON form of
        // them, merged into every session, so full permissions keep live web
        // search without rewriting the imported config.
        acp: Some(AcpLaunch {
            command: "codex-acp",
            args: &[],
            full_args: &[],
            full_mode: Some("agent-full-access"),
            full_environment: &[("CODEX_CONFIG", r#"{"web_search":"live"}"#)],
        }),
    },
    AdapterDescriptor {
        name: "grok",
        product: "Grok Build",
        command: "grok",
        args: &[],
        prompt_args: &["-p"],
        model_args: &["-m", "{model}"],
        effort_args: &["--reasoning-effort", "{effort}"],
        model_hint: "xAI Grok model ID, such as grok-4.7.",
        home_environment: &[("GROK_HOME", ".grok")],
        fixed_environment: &[("GROK_DISABLE_AUTOUPDATER", "1")],
        removed_environment: &["GROK_*", "XAI_API_KEY"],
        // Web search is on unless `--disable-web-search` is passed.
        full_permission_args: &["--always-approve"],
        full_permission_environment: &[],
        search_dirs: &[".grok/bin"],
        login: Login::Command(&["login"]),
        status: Status::Stored(KeyStore::Grok {
            auth: ".grok/auth.json",
            config: ".grok/config.toml",
        }),
        status_summary: StatusSummary::ExitStatus,
        logout: Logout::Command(&["logout"]),
        // `--output-format json` exists but its success shape is unverified here.
        output: OutputFormat::Text,
        // Grok documents `--session-id` and `--resume`, but they cannot be
        // verified while it is signed out here.
        resume: Resume::Unsupported,
        conversation_files: None,
        transport: Transport::Process,
        // Native: `grok agent [options] stdio`; options precede the mode.
        acp: Some(AcpLaunch {
            command: "grok",
            args: &["agent", "{full}", "stdio"],
            full_args: &["--always-approve"],
            full_mode: None,
            full_environment: &[],
        }),
    },
    AdapterDescriptor {
        name: "dsh",
        product: "DeepSeek Harness",
        command: "dsh",
        args: &["--profile", "headless"],
        prompt_args: &[],
        model_args: &[],
        effort_args: &[],
        model_hint: "Model ID in the form this agent's CLI accepts.",
        home_environment: &[("DSH_HOME", ".dsh")],
        fixed_environment: &[],
        removed_environment: &["DSH_*", "DEEPSEEK_API_KEY", "DEEPSEEK_BASE_URL"],
        // Bypasses its file sandbox and sets its approval policy to `never`.
        full_permission_args: &[],
        full_permission_environment: &[("DSH_PERMISSION_MODE", "danger-full-access")],
        search_dirs: &[],
        login: Login::ApiKey(DSH_STORE),
        status: Status::Stored(DSH_STORE),
        status_summary: StatusSummary::ExitStatus,
        logout: Logout::Stored(DSH_STORE),
        output: OutputFormat::Text,
        // Only its interactive profile documents `--resume`.
        resume: Resume::Unsupported,
        conversation_files: None,
        transport: Transport::Process,
        // Native: the shipped `acp` profile. `permissions = "full"` is the
        // `DSH_PERMISSION_MODE` variable above.
        acp: Some(AcpLaunch {
            command: "dsh",
            args: &["--profile", "acp"],
            full_args: &[],
            full_mode: None,
            full_environment: &[],
        }),
    },
    AdapterDescriptor {
        name: "pi",
        product: "pi",
        command: "pi",
        args: &["-p"],
        prompt_args: &[],
        model_args: &["--model", "{model}"],
        effort_args: &["--thinking", "{effort}"],
        model_hint: "pi model pattern or provider/id; the SCV-configured endpoint is provider scv.",
        home_environment: &[("PI_CODING_AGENT_DIR", ".pi/agent")],
        fixed_environment: &[],
        removed_environment: &["PI_*"],
        // pi has no approval prompts or sandbox, and no built-in web search.
        full_permission_args: &[],
        full_permission_environment: &[],
        search_dirs: &[],
        login: Login::Interactive {
            args: &[],
            hint: "run /login and choose a provider, then /quit",
        },
        status: Status::Stored(PI_STORE),
        status_summary: StatusSummary::ExitStatus,
        logout: Logout::Stored(PI_STORE),
        output: OutputFormat::PiJson,
        // `--session-id` uses the exact project session, creating it if missing.
        resume: Resume::Supported {
            start: &["--session-id", "{session}"],
            subcommand: &[],
            options: &["--session-id", "{session}"],
            positional: &[],
        },
        conversation_files: Some(ConversationFiles {
            dir: ".pi/agent/sessions",
            extension: "jsonl",
        }),
        transport: Transport::Process,
        // Only a community ACP adapter exists.
        acp: None,
    },
    AdapterDescriptor {
        name: "scv",
        product: "SCV",
        command: "scv",
        args: &["server", "--stdio"],
        prompt_args: &[],
        // A model is chosen per conversation through `session.start`.
        model_args: &[],
        effort_args: &[],
        model_hint: "Model ID for the nested SCV's provider; applies to a new conversation only.",
        // `SCV_HOME` already points at the adapter home, where the nested
        // SCV keeps its config, skills, and its own delegations.
        home_environment: &[],
        fixed_environment: &[],
        removed_environment: &[],
        // Its tool approvals are relayed to the calling session instead.
        full_permission_args: &[],
        full_permission_environment: &[],
        // Where `cargo install` puts `scv`; a user service's PATH omits it.
        search_dirs: &[".cargo/bin"],
        login: Login::Import,
        status: Status::Stored(SCV_STORE),
        status_summary: StatusSummary::ExitStatus,
        logout: Logout::Stored(SCV_STORE),
        output: OutputFormat::Text,
        resume: Resume::Unsupported,
        conversation_files: None,
        transport: Transport::ScvProtocol,
        acp: None,
    },
];

/// The descriptor for `name`, such as `"codex"`.
pub fn adapter(name: &str) -> Option<&'static AdapterDescriptor> {
    ADAPTERS.iter().find(|adapter| adapter.name == name)
}

/// Whether a delegated agent must not inherit `variable`: SCV's selectors,
/// any `*_API_KEY`, and every adapter's credential and state variables, so
/// no agent sees another's credentials either.
pub fn is_removed_agent_variable(variable: &OsStr) -> bool {
    let Some(variable) = variable.to_str() else {
        return false;
    };
    variable.ends_with("_API_KEY")
        || COMMON_REMOVED_ENVIRONMENT.contains(&variable)
        || ADAPTERS
            .iter()
            .flat_map(|adapter| adapter.removed_environment)
            .any(|rule| match rule.strip_suffix('*') {
                Some(prefix) => variable.starts_with(prefix),
                None => variable == *rule,
            })
}

/// One line describing a CLI's own status result without echoing it: the raw
/// output names the signed-in account or part of a key.
pub fn summarize_status(summary: StatusSummary, succeeded: bool, output: &str) -> String {
    let signed_out = "not signed in".to_owned();
    match summary {
        StatusSummary::ClaudeJson => {
            // The first JSON value; anything after it (such as stderr) is ignored.
            let first = serde_json::Deserializer::from_str(output)
                .into_iter::<serde_json::Value>()
                .next();
            let Some(Ok(value)) = first else {
                return if succeeded {
                    "signed in".into()
                } else {
                    signed_out
                };
            };
            if value.get("loggedIn").and_then(serde_json::Value::as_bool) != Some(true) {
                return signed_out;
            }
            let method = match value.get("authMethod").and_then(serde_json::Value::as_str) {
                Some("claude.ai") => "Claude account",
                Some("api_key" | "apiKey" | "console") => "API key",
                Some("oauth_token" | "oauthToken") => "OAuth token",
                _ => "other method",
            };
            match value
                .get("subscriptionType")
                .and_then(serde_json::Value::as_str)
                .filter(|plan| ["free", "pro", "max", "team", "enterprise"].contains(plan))
            {
                Some(plan) => format!("signed in ({method}, {plan})"),
                None => format!("signed in ({method})"),
            }
        }
        StatusSummary::CodexText => {
            let lower = output.to_ascii_lowercase();
            if !succeeded || lower.contains("not logged in") {
                signed_out
            } else if lower.contains("api key") {
                "signed in (API key)".into()
            } else if lower.contains("chatgpt") {
                "signed in (ChatGPT account)".into()
            } else {
                "signed in".into()
            }
        }
        StatusSummary::ExitStatus => {
            if succeeded {
                "signed in".into()
            } else {
                signed_out
            }
        }
    }
}

/// Resolve `command` in the per-user `search_dirs`, then on `PATH`. A command
/// containing a path separator is used as given.
pub fn resolve_agent_executable(command: &str, search_dirs: &[PathBuf]) -> Option<PathBuf> {
    if command.contains('/') {
        let path = Path::new(command);
        return path.is_file().then(|| path.to_path_buf());
    }
    std::env::join_paths(search_dirs)
        .ok()
        .and_then(|dirs| {
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/"));
            which::which_in(command, Some(dirs), cwd).ok()
        })
        .or_else(|| which::which(command).ok())
}

/// Absolute per-user search directories for `adapter` under `home`.
pub fn adapter_search_dirs(adapter: &AdapterDescriptor, home: &Path) -> Vec<PathBuf> {
    adapter
        .search_dirs
        .iter()
        .chain(USER_BIN_DIRS)
        .map(|dir| home.join(dir))
        .collect()
}

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

    #[test]
    fn descriptors_are_unique_and_self_consistent() {
        let mut names: Vec<_> = ADAPTERS.iter().map(|adapter| adapter.name).collect();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), ADAPTERS.len());
        for adapter in ADAPTERS {
            assert!(
                adapter.model_args.is_empty()
                    || adapter.model_args.iter().any(|arg| arg.contains("{model}")),
                "{}",
                adapter.name
            );
            assert!(
                adapter.effort_args.is_empty()
                    || adapter
                        .effort_args
                        .iter()
                        .any(|arg| arg.contains("{effort}")),
                "{}",
                adapter.name
            );
            if let Resume::Supported {
                start,
                subcommand,
                options,
                positional,
            } = adapter.resume
            {
                let names_session =
                    |args: &[&str]| args.iter().any(|arg| arg.contains("{session}"));
                assert!(start.is_empty() || names_session(start), "{}", adapter.name);
                assert!(
                    names_session(options) || names_session(positional),
                    "{}",
                    adapter.name
                );
                assert!(!names_session(subcommand), "{}", adapter.name);
                assert!(adapter.conversation_files.is_some(), "{}", adapter.name);
            }
            // Anything SCV sets must survive the removal pass.
            for (variable, _) in adapter
                .home_environment
                .iter()
                .chain(adapter.fixed_environment)
            {
                assert!(!variable.ends_with("_API_KEY"), "{variable}");
            }
            // Stored credentials live inside the directory SCV points the CLI at.
            for store in [
                match adapter.status {
                    Status::Stored(store) => Some(store),
                    Status::Command(_) => None,
                },
                match adapter.logout {
                    Logout::Stored(store) => Some(store),
                    Logout::Command(_) => None,
                },
                match adapter.login {
                    Login::ApiKey(store) => Some(store),
                    _ => None,
                },
            ]
            .into_iter()
            .flatten()
            {
                let paths = match store {
                    KeyStore::Grok { auth, config } => vec![auth, config],
                    KeyStore::DshRefs { path, .. } => vec![path],
                    KeyStore::Pi { dir } => vec![dir],
                    // The nested SCV's `SCV_HOME` is the adapter home itself.
                    KeyStore::Scv { .. } => vec![],
                };
                for path in paths {
                    assert!(
                        adapter
                            .home_environment
                            .iter()
                            .any(|(_, home)| !home.is_empty() && path.starts_with(home)),
                        "{}: {path}",
                        adapter.name
                    );
                }
            }
        }
    }

    #[test]
    fn status_summaries_never_echo_accounts_or_keys() {
        let claude = r#"{"loggedIn":true,"authMethod":"claude.ai","email":"me@example.com","orgName":"me@example.com's Organization","subscriptionType":"max"}"#;
        assert_eq!(
            summarize_status(StatusSummary::ClaudeJson, true, claude),
            "signed in (Claude account, max)"
        );
        assert_eq!(
            summarize_status(
                StatusSummary::ClaudeJson,
                true,
                r#"{"loggedIn":true,"authMethod":"api_key","subscriptionType":"me@example.com"}"#
            ),
            "signed in (API key)"
        );
        assert_eq!(
            summarize_status(StatusSummary::ClaudeJson, false, r#"{"loggedIn":false}"#),
            "not signed in"
        );
        assert_eq!(
            summarize_status(
                StatusSummary::ClaudeJson,
                true,
                "{\"loggedIn\":true,\"authMethod\":\"claude.ai\"}\n\nsome stderr"
            ),
            "signed in (Claude account)"
        );
        assert_eq!(
            summarize_status(
                StatusSummary::CodexText,
                true,
                "Logged in using an API key - sk-proj-***abcd"
            ),
            "signed in (API key)"
        );
        assert_eq!(
            summarize_status(StatusSummary::CodexText, true, "Logged in using ChatGPT"),
            "signed in (ChatGPT account)"
        );
        assert_eq!(
            summarize_status(StatusSummary::CodexText, false, "Not logged in"),
            "not signed in"
        );
        for adapter in ADAPTERS {
            if let Status::Command(_) = adapter.status {
                assert_ne!(
                    adapter.status_summary,
                    StatusSummary::ExitStatus,
                    "{}",
                    adapter.name
                );
            }
        }
    }

    #[test]
    fn removal_covers_every_adapter_and_generic_api_keys() {
        for removed in [
            "OPENAI_API_KEY",
            "CLAUDE_CONFIG_DIR",
            "GROK_HOME",
            "GROK_AUTH",
            "XAI_API_KEY",
            "DSH_HOME",
            "DSH_PERMISSION_MODE",
            "DEEPSEEK_BASE_URL",
            "PI_CODING_AGENT_DIR",
            "OPENROUTER_API_KEY",
            "SCV_CONFIG",
        ] {
            assert!(is_removed_agent_variable(OsStr::new(removed)), "{removed}");
        }
        for kept in ["PATH", "HOME", "LANG", "GH_TOKEN", "GROKKING", "PIPX_HOME"] {
            assert!(!is_removed_agent_variable(OsStr::new(kept)), "{kept}");
        }
    }

    #[test]
    fn executables_resolve_from_per_user_directories_before_path() {
        let dir = tempfile::tempdir().unwrap();
        let bin = dir.path().join(".grok/bin");
        std::fs::create_dir_all(&bin).unwrap();
        let name = "scv-test-agent-only-in-home";
        let executable = bin.join(name);
        std::fs::write(&executable, "#!/bin/sh\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        let grok = adapter("grok").unwrap();
        let dirs = adapter_search_dirs(grok, dir.path());
        assert!(dirs.contains(&dir.path().join(".local/bin")));
        assert_eq!(
            resolve_agent_executable(name, &dirs),
            Some(executable.clone())
        );
        assert_eq!(resolve_agent_executable(name, &[]), None);
        // A per-user install wins over the same command on PATH.
        let shadow = bin.join("sh");
        std::fs::write(&shadow, "#!/bin/sh\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&shadow, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        assert_eq!(resolve_agent_executable("sh", &dirs), Some(shadow));
        assert!(resolve_agent_executable("sh", &[]).is_some());
        assert_eq!(
            resolve_agent_executable(executable.to_str().unwrap(), &[]),
            Some(executable)
        );
    }
}