codewhale-cli 0.9.4

Agentic terminal facade for open-source and open-weight coding models
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
//! `model resolve` must report the route the runtime would actually take.
//!
//! Regression coverage for #4832, where a Z.ai config reported
//! `provider: deepseek` because the subcommand read only the CLI flags and
//! never consulted the resolved runtime. A diagnostic that confidently
//! reports the wrong provider is worse than one that reports nothing, so
//! every provider is asserted here rather than DeepSeek alone.

use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use std::process::Command;

use tempfile::TempDir;

/// Run `model resolve` against a sealed HOME containing `config`.
///
/// `env_clear` plus a temporary HOME keeps this off the real
/// `~/.codewhale/config.toml`; the suite has written to real user state before
/// (#4831) and this test must never be the one that does it again.
fn resolve_with_config(config: &str, args: &[&str]) -> BTreeMap<String, String> {
    let fixture = TempDir::new().expect("fixture root");
    let home = fixture.path().join("sealed-home");
    fs::create_dir_all(home.join(".codewhale")).expect("sealed config dir");
    fs::write(home.join(".codewhale").join("config.toml"), config).expect("seed config");

    let mut command = Command::new(codewhale_binary());
    command.arg("model").arg("resolve").args(args);
    let output = command
        .env_clear()
        .env("HOME", &home)
        .env("USERPROFILE", &home)
        .env("CODEWHALE_HOME", home.join(".codewhale"))
        .env("CODEWHALE_SECRET_BACKEND", "file")
        .output()
        .expect("run model resolve");

    assert!(
        output.status.success(),
        "model resolve {args:?} failed\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    String::from_utf8_lossy(&output.stdout)
        .lines()
        .filter_map(|line| line.split_once(": "))
        .map(|(key, value)| (key.trim().to_string(), value.trim().to_string()))
        .collect()
}

#[test]
fn resolve_reports_the_configured_provider_not_a_deepseek_fallback() {
    let report = resolve_with_config(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &[],
    );

    assert_eq!(
        report.get("provider").map(String::as_str),
        Some("zai"),
        "configured provider must survive to the diagnostic: {report:?}"
    );
    assert_eq!(
        report.get("provider_source").map(String::as_str),
        Some("config"),
        "provenance must name the config file: {report:?}"
    );
}

#[test]
fn resolve_reports_a_provider_scoped_model_as_explicitly_configured() {
    let report = resolve_with_config(
        "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\nmodel = \"kimi-k3-turbo\"\n",
        &[],
    );

    assert_eq!(report.get("provider").map(String::as_str), Some("moonshot"));
    assert_eq!(
        report.get("requested").map(String::as_str),
        Some("kimi-k3-turbo"),
        "a configured model is a request, not a fallback: {report:?}"
    );
    assert_eq!(
        report.get("used_fallback").map(String::as_str),
        Some("false"),
        "{report:?}"
    );
    assert_eq!(
        report.get("model_source").map(String::as_str),
        Some("config [providers.*].model"),
        "{report:?}"
    );
}

#[test]
fn resolve_admits_when_nothing_was_configured() {
    // The honest answer to "what did the user ask for" is "nothing". The
    // built-in default may still be shown, but it must be labelled as ours.
    let report = resolve_with_config("", &[]);

    assert_eq!(
        report.get("requested").map(String::as_str),
        Some(""),
        "an unconfigured model must not be presented as a request: {report:?}"
    );
    assert_eq!(
        report.get("used_fallback").map(String::as_str),
        Some("true"),
        "{report:?}"
    );
    assert_eq!(
        report.get("model_source").map(String::as_str),
        Some("provider default"),
        "{report:?}"
    );
}

#[test]
fn an_explicit_model_argument_still_answers_the_hypothetical() {
    // Naming a model asks "what would this resolve to", which must keep
    // working even when the configured provider is something else.
    let report = resolve_with_config(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &["deepseek-v4-flash"],
    );

    assert_eq!(
        report.get("requested").map(String::as_str),
        Some("deepseek-v4-flash"),
        "{report:?}"
    );
    assert_eq!(
        report.get("model_source").map(String::as_str),
        Some("argument"),
        "{report:?}"
    );
    assert_eq!(
        report.get("used_fallback").map(String::as_str),
        Some("false"),
        "{report:?}"
    );
}

#[test]
fn an_explicit_provider_flag_is_reported_as_the_source() {
    let report = resolve_with_config(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &["--provider", "moonshot"],
    );

    assert_eq!(report.get("provider").map(String::as_str), Some("moonshot"));
    assert_eq!(
        report.get("provider_source").map(String::as_str),
        Some("--provider"),
        "{report:?}"
    );
}

/// Run `model resolve` with global flags placed before the subcommand, which
/// is where `--provider` / `--model` actually go.
fn resolve_with_global_flags(
    config: &str,
    global: &[&str],
    args: &[&str],
) -> BTreeMap<String, String> {
    let fixture = TempDir::new().expect("fixture root");
    let home = fixture.path().join("sealed-home");
    fs::create_dir_all(home.join(".codewhale")).expect("sealed config dir");
    fs::write(home.join(".codewhale").join("config.toml"), config).expect("seed config");

    let mut command = Command::new(codewhale_binary());
    command.args(global).arg("model").arg("resolve").args(args);
    let output = command
        .env_clear()
        .env("HOME", &home)
        .env("USERPROFILE", &home)
        .env("CODEWHALE_HOME", home.join(".codewhale"))
        .env("CODEWHALE_SECRET_BACKEND", "file")
        .output()
        .expect("run model resolve");

    assert!(
        output.status.success(),
        "model resolve {global:?} {args:?} failed\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    String::from_utf8_lossy(&output.stdout)
        .lines()
        .filter_map(|line| line.split_once(": "))
        .map(|(key, value)| (key.trim().to_string(), value.trim().to_string()))
        .collect()
}

/// v0.9.1 kimi-k3 dogfood report: `codewhale --provider moonshot --model kimi-k3 model resolve`
/// reported `kimi-k2.7-code`. The top-level flags are the route this process
/// is on, not a hypothetical, so the diagnostic has to answer with the runtime
/// resolution instead of re-deriving a registry default and ignoring `--model`.
#[test]
fn top_level_provider_and_model_flags_report_the_runtime_route() {
    let report = resolve_with_global_flags(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &["--provider", "moonshot", "--model", "kimi-k3"],
        &[],
    );

    assert_eq!(report.get("provider").map(String::as_str), Some("moonshot"));
    assert_eq!(
        report.get("resolved").map(String::as_str),
        Some("kimi-k3"),
        "the diagnostic must not contradict the model the run will use: {report:?}"
    );
    assert_eq!(
        report.get("requested").map(String::as_str),
        Some("kimi-k3"),
        "{report:?}"
    );
    assert_eq!(
        report.get("used_fallback").map(String::as_str),
        Some("false"),
        "{report:?}"
    );
    assert_eq!(
        report.get("model_source").map(String::as_str),
        Some("--model"),
        "{report:?}"
    );
}

/// Moonshot ships `kimi-k3` on the direct platform API and `k3` on the Kimi
/// Code coding-plan API. Both must resolve, and neither may be answered by
/// another provider's identically named model (OpenCode Go also serves a
/// `kimi-k3`).
#[test]
fn moonshot_k3_products_resolve_without_crossing_providers() {
    for model in ["kimi-k3", "k3"] {
        let report = resolve_with_global_flags(
            "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\n",
            &[],
            &[model, "--provider", "moonshot"],
        );

        assert_eq!(
            report.get("provider").map(String::as_str),
            Some("moonshot"),
            "a Moonshot question must not be answered by another provider: {report:?}"
        );
        assert_eq!(
            report.get("resolved").map(String::as_str),
            Some(model),
            "{report:?}"
        );
        assert_eq!(
            report.get("used_fallback").map(String::as_str),
            Some("false"),
            "{report:?}"
        );
    }
}

/// An id the selected provider cannot serve must be reported as a fallback,
/// never as if the request had been honoured.
#[test]
fn an_unservable_model_on_the_selected_provider_is_reported_as_a_fallback() {
    let report = resolve_with_global_flags(
        "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\n",
        &[],
        &["glm-5.2", "--provider", "moonshot"],
    );

    assert_eq!(report.get("provider").map(String::as_str), Some("moonshot"));
    assert_eq!(
        report.get("used_fallback").map(String::as_str),
        Some("true"),
        "an unservable id must not be presented as an honoured request: {report:?}"
    );
}

/// Adding a model to the catalog must make it servable on the provider that
/// carries it and nowhere else. `glm-5.3` was added as a peer of `glm-5.2`, so
/// it has to answer on Z.ai without a fallback while a Moonshot-scoped question
/// still refuses it — the same cross-provider boundary the `glm-5.2` case above
/// pins, asserted on the newest sibling so the boundary cannot rot as the
/// family grows.
#[test]
fn a_new_glm_sibling_is_servable_on_zai_but_not_on_moonshot() {
    let served = resolve_with_global_flags(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &[],
        &["glm-5.3", "--provider", "zai"],
    );

    assert_eq!(served.get("provider").map(String::as_str), Some("zai"));
    assert_eq!(
        served.get("resolved").map(String::as_str),
        Some("GLM-5.3"),
        "a catalogued model must resolve to itself, not to the provider default: {served:?}"
    );
    assert_eq!(
        served.get("used_fallback").map(String::as_str),
        Some("false"),
        "a model the provider serves must not be reported as a fallback: {served:?}"
    );

    let refused = resolve_with_global_flags(
        "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\n",
        &[],
        &["glm-5.3", "--provider", "moonshot"],
    );

    assert_eq!(
        refused.get("provider").map(String::as_str),
        Some("moonshot")
    );
    assert_eq!(
        refused.get("used_fallback").map(String::as_str),
        Some("true"),
        "a Z.ai id must not be presented as honoured by Moonshot: {refused:?}"
    );
    let resolved = refused
        .get("resolved")
        .map(String::as_str)
        .unwrap_or_default();
    assert!(
        !resolved.to_ascii_lowercase().contains("glm"),
        "a provider that cannot serve GLM must not be handed a fabricated GLM id: {refused:?}"
    );
}

/// The OpenRouter sibling carries a different wire id (`z-ai/glm-5.3`) than the
/// direct Z.ai row (`GLM-5.3`), so the bare family alias has to be rewritten
/// per provider rather than passed through. This pins the OpenRouter half of
/// that rewrite, which the Z.ai case above cannot observe, and pins that adding
/// the sibling left the OpenRouter default alone.
#[test]
fn the_openrouter_glm_sibling_resolves_to_its_own_gateway_wire_id() {
    let served = resolve_with_global_flags(
        "provider = \"openrouter\"\n\n[providers.openrouter]\napi_key = \"k\"\n",
        &[],
        &["glm-5.3", "--provider", "openrouter"],
    );

    assert_eq!(
        served.get("provider").map(String::as_str),
        Some("openrouter")
    );
    assert_eq!(
        served.get("resolved").map(String::as_str),
        Some("z-ai/glm-5.3"),
        "the bare alias must be rewritten to the OpenRouter wire id, not passed through: {served:?}"
    );
    assert_eq!(
        served.get("used_fallback").map(String::as_str),
        Some("false"),
        "a gateway row the provider serves must not be reported as a fallback: {served:?}"
    );

    let default_route = resolve_with_config(
        "provider = \"openrouter\"\n\n[providers.openrouter]\napi_key = \"k\"\n",
        &[],
    );
    let resolved = default_route
        .get("resolved")
        .map(String::as_str)
        .unwrap_or_default();
    assert!(
        !resolved.to_ascii_lowercase().contains("glm"),
        "adding a GLM sibling must not make GLM the OpenRouter default: {default_route:?}"
    );
}

/// Adding a sibling must not move anyone's route. A Z.ai config that names no
/// model still has to land on `GLM-5.2`: the newer `glm-5.3` is catalogued but
/// deliberately not the default, and this is the surface where that would
/// silently change under a user.
#[test]
fn adding_a_glm_sibling_leaves_the_zai_default_route_untouched() {
    let report = resolve_with_config(
        "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n",
        &[],
    );

    assert_eq!(
        report.get("resolved").map(String::as_str),
        Some("GLM-5.2"),
        "the Z.ai default must stay GLM-5.2 after a newer sibling is added: {report:?}"
    );
    assert_eq!(
        report.get("model_source").map(String::as_str),
        Some("provider default"),
        "{report:?}"
    );
}

fn codewhale_binary() -> PathBuf {
    if let Some(path) = option_env!("CARGO_BIN_EXE_codewhale") {
        return PathBuf::from(path);
    }
    if let Ok(path) = std::env::var("CARGO_BIN_EXE_codewhale") {
        return PathBuf::from(path);
    }

    let mut path = std::env::current_exe().expect("current test executable path");
    path.pop();
    if path.ends_with("deps") {
        path.pop();
    }
    path.push(format!("codewhale{}", std::env::consts::EXE_SUFFIX));
    path
}