codewhale-tui 0.9.10

Terminal UI 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
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
use super::*;
use clap::Parser as _;

#[test]
fn default_probe_request_is_fully_offline() {
    let request = DoctorProbeRequest::default();
    assert!(!request.should_check_updates());
    assert!(!request.should_probe_api(false));
    assert!(!request.should_probe_api(true));
    assert!(!request.should_probe_mcp());
    assert!(!request.should_probe_search());
}

fn ds4_config() -> crate::config::Config {
    let mut providers = crate::config::ProvidersConfig::default();
    providers.custom.insert(
        "ds4".to_string(),
        crate::config::ProviderConfig {
            kind: Some("openai-compatible".to_string()),
            base_url: Some("http://127.0.0.1:8000/v1".to_string()),
            model: Some("deepseek-v4-flash".to_string()),
            auth_mode: Some("none".to_string()),
            ..Default::default()
        },
    );
    crate::config::Config {
        provider: Some("ds4".to_string()),
        providers: Some(providers),
        ..Default::default()
    }
}

#[test]
fn recognizes_only_the_explicit_keyless_local_ds4_route() {
    let ds4 = ds4_config();
    assert!(is_keyless_ds4_route(&ds4));

    let mut hosted = ds4.clone();
    hosted
        .providers
        .as_mut()
        .and_then(|providers| providers.custom.get_mut("ds4"))
        .expect("DS4 config")
        .base_url = Some("https://ds4.example/v1".to_string());
    assert!(!is_keyless_ds4_route(&hosted));
}

#[test]
fn ds4_probe_error_reports_status_without_response_body() {
    let mut config = ds4_config();
    config
        .providers
        .as_mut()
        .and_then(|providers| providers.custom.get_mut("ds4"))
        .expect("DS4 config")
        .base_url = Some("http://user:secret@127.0.0.1:8000/v1?token=secret".to_string());
    let message = ds4_probe_error(
        &config,
        "Failed to list models: HTTP 404: secret backend response",
    );

    assert!(message.contains("HTTP 404"));
    assert!(!message.contains("secret backend response"));
    assert!(!message.contains("secret"));
}

#[test]
fn update_renderer_omits_untrusted_release_tags_and_errors() {
    let release_sentinel = "v9.9.9?token=doctor-update-sentinel";
    let error_sentinel = "https://user:doctor-update-error-sentinel@example.test/path";

    let metadata = doctor_update_report("0.9.3", Ok::<String, ()>(release_sentinel.to_string()));
    let transport = doctor_update_report("0.9.3", Err(error_sentinel.to_string()));
    let rendered = [
        doctor_update_report_lines(&metadata).join("\n"),
        doctor_update_report_lines(&transport).join("\n"),
    ]
    .join("\n");

    assert_eq!(metadata, DoctorUpdateReport::ReleaseMetadataInvalid);
    assert_eq!(transport, DoctorUpdateReport::ReleaseCheckFailed);
    assert!(!rendered.contains(release_sentinel));
    assert!(!rendered.contains(error_sentinel));
    assert!(rendered.contains("details omitted"));
}

#[test]
fn update_renderer_canonicalizes_safe_release_tags() {
    let report = doctor_update_report("0.9.3", Ok::<String, ()>(" v0.9.4 ".to_string()));
    assert_eq!(
        doctor_update_report_lines(&report),
        vec![
            "latest: v0.9.4".to_string(),
            "Update available. Run `codewhale update` to install.".to_string(),
        ]
    );
}

#[test]
fn live_probe_flags_open_only_their_owned_boundary() {
    let update = DoctorProbeRequest {
        check_updates: true,
        ..DoctorProbeRequest::default()
    };
    assert!(update.should_check_updates());
    assert!(!update.should_probe_api(false));
    assert!(!update.should_probe_api(true));

    let hosted = DoctorProbeRequest {
        probe_api: true,
        ..DoctorProbeRequest::default()
    };
    assert!(hosted.should_probe_api(false));
    assert!(!hosted.should_probe_api(true));

    let local = DoctorProbeRequest {
        probe_local: true,
        ..DoctorProbeRequest::default()
    };
    assert!(!local.should_probe_api(false));
    assert!(local.should_probe_api(true));

    let search = DoctorProbeRequest {
        probe_search: true,
        ..DoctorProbeRequest::default()
    };
    assert!(search.should_probe_search());
    assert!(!search.should_check_updates());
    assert!(!search.should_probe_api(false));
    assert!(!search.should_probe_mcp());
}

#[test]
fn cli_defaults_doctor_offline_and_keeps_json_incompatible_with_live_flags() {
    let cli =
        crate::Cli::try_parse_from(["codewhale-tui", "doctor"]).expect("parse default doctor");
    let Some(crate::Commands::Doctor(args)) = cli.command else {
        panic!("expected doctor command");
    };
    assert!(!args.check_updates);
    assert!(!args.probe_api);
    assert!(!args.probe_local);
    assert!(!args.probe_mcp);
    assert!(!args.probe_search);

    let cli = crate::Cli::try_parse_from(["codewhale-tui", "doctor", "--probe-search"])
        .expect("parse search probe");
    let Some(crate::Commands::Doctor(args)) = cli.command else {
        panic!("expected doctor command");
    };
    assert!(args.probe_search);
    assert!(!args.probe_api);
    assert!(!args.probe_local);
    assert!(!args.probe_mcp);

    for output_flag in ["--json", "--context-json"] {
        for live_flag in [
            "--check-updates",
            "--probe-api",
            "--probe-local",
            "--probe-mcp",
            "--probe-search",
        ] {
            assert!(
                crate::Cli::try_parse_from(["codewhale-tui", "doctor", output_flag, live_flag,])
                    .is_err(),
                "{output_flag} unexpectedly accepted live flag {live_flag}"
            );
        }
    }
}

#[tokio::test]
async fn search_probe_counts_any_http_response_as_transport_only() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    Mock::given(method("HEAD"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(401))
        .expect(1)
        .mount(&server)
        .await;
    let config = crate::config::Config {
        search: Some(crate::config::SearchConfig {
            provider: Some(crate::config::SearchProvider::DuckDuckGo),
            base_url: Some(server.uri()),
            api_key: None,
        }),
        ..Default::default()
    };

    let report = doctor_search_probe(
        &config,
        DoctorProbeRequest {
            probe_search: true,
            ..DoctorProbeRequest::default()
        },
    )
    .await;
    let lines = doctor_search_probe_lines(&report).join("\n");

    assert!(matches!(
        report,
        DoctorSearchProbeReport::Reachable { status: 401, .. }
    ));
    assert!(lines.contains("responded (HTTP 401)"), "{lines}");
    assert!(lines.contains("Transport only"), "{lines}");
    assert!(lines.contains("authentication and search results were not tested"));
    assert!(!lines.contains("search successful"));
    let requests = server.received_requests().await.expect("recorded probe");
    assert_eq!(requests.len(), 1);
    assert!(requests[0].url.query().is_none());
    assert!(requests[0].body.is_empty());
    assert!(requests[0].headers.get("authorization").is_none());
    assert!(requests[0].headers.get("x-api-key").is_none());
    assert!(requests[0].headers.get("cookie").is_none());
}

#[tokio::test]
async fn search_probe_does_not_follow_redirects() {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    let server = MockServer::start().await;
    Mock::given(method("HEAD"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(302).insert_header("Location", "/redirected"))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("HEAD"))
        .and(path("/redirected"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    let config = crate::config::Config {
        search: Some(crate::config::SearchConfig {
            provider: Some(crate::config::SearchProvider::Searxng),
            base_url: Some(server.uri()),
            api_key: None,
        }),
        ..Default::default()
    };

    let report = doctor_search_probe(
        &config,
        DoctorProbeRequest {
            probe_search: true,
            ..DoctorProbeRequest::default()
        },
    )
    .await;

    assert!(matches!(
        report,
        DoctorSearchProbeReport::Reachable { status: 302, .. }
    ));
}

#[tokio::test]
async fn search_probe_respects_network_policy_without_contacting_the_authority() {
    let config = crate::config::Config {
        search: Some(crate::config::SearchConfig {
            provider: Some(crate::config::SearchProvider::Searxng),
            base_url: Some("https://search.example/private?token=secret".to_string()),
            api_key: None,
        }),
        network: Some(crate::config::NetworkPolicyToml {
            default: "allow".to_string(),
            deny: vec!["search.example".to_string()],
            ..Default::default()
        }),
        ..Default::default()
    };

    let report = doctor_search_probe(
        &config,
        DoctorProbeRequest {
            probe_search: true,
            ..DoctorProbeRequest::default()
        },
    )
    .await;
    let lines = doctor_search_probe_lines(&report).join("\n");

    assert_eq!(
        report,
        DoctorSearchProbeReport::PolicyDenied {
            authority: "https://search.example".to_string()
        }
    );
    assert!(!lines.contains("private"));
    assert!(!lines.contains("token"));
    assert!(!lines.contains("secret"));
}

#[tokio::test]
async fn search_probe_skips_the_default_provider_when_web_search_is_disabled() {
    let mut config = crate::config::Config::default();
    config
        .set_feature("web_search", false)
        .expect("known feature");

    let report = doctor_search_probe(
        &config,
        DoctorProbeRequest {
            probe_search: true,
            ..DoctorProbeRequest::default()
        },
    )
    .await;

    assert_eq!(report, DoctorSearchProbeReport::FeatureDisabled);
    assert!(
        doctor_search_probe_lines(&report)
            .join("\n")
            .contains("web_search feature is disabled")
    );
}

#[test]
fn explicit_codewhale_home_owns_every_default_user_path() {
    let _lock = crate::test_support::lock_test_env();
    let temp = tempfile::tempdir().expect("temp home");
    let home = temp.path().join("isolated-codewhale-home");
    let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.as_os_str());
    let _config = crate::test_support::EnvVarGuard::remove("CODEWHALE_CONFIG_PATH");
    let _legacy_config = crate::test_support::EnvVarGuard::remove("DEEPSEEK_CONFIG_PATH");
    let _automations = crate::test_support::EnvVarGuard::remove("CODEWHALE_AUTOMATIONS_DIR");
    let _legacy_automations = crate::test_support::EnvVarGuard::remove("DEEPSEEK_AUTOMATIONS_DIR");
    let _tasks = crate::test_support::EnvVarGuard::remove("CODEWHALE_TASKS_DIR");
    let _legacy_tasks = crate::test_support::EnvVarGuard::remove("DEEPSEEK_TASKS_DIR");
    let _runtime = crate::test_support::EnvVarGuard::remove("CODEWHALE_RUNTIME_DIR");
    let _legacy_runtime = crate::test_support::EnvVarGuard::remove("DEEPSEEK_RUNTIME_DIR");

    let report = DoctorPathReport::resolve(None).expect("resolve doctor paths");
    let task_manager_root = crate::task_manager::default_tasks_dir();
    let runtime_config = crate::runtime_threads::RuntimeThreadManagerConfig::from_task_data_dir(
        task_manager_root.clone(),
    );
    let (secrets, legacy_secrets) =
        codewhale_secrets::FileKeyringStore::default_paths_read_only().expect("secret paths");

    assert_eq!(report.home, home);
    assert_eq!(report.config, home.join("config.toml"));
    assert_eq!(report.settings, home.join("settings.toml"));
    assert_eq!(report.sessions, home.join("sessions"));
    assert_eq!(report.logs, home.join("logs"));
    assert_eq!(report.automations, home.join("automations"));
    assert_eq!(report.task_manager_root, task_manager_root);
    assert_eq!(report.task_manager_tasks, task_manager_root.join("tasks"));
    assert_eq!(
        report.task_manager_artifacts,
        task_manager_root.join("artifacts")
    );
    assert_eq!(report.runtime_store, runtime_config.data_dir);
    assert_eq!(
        report.runtime_events,
        runtime_config.data_dir.join("events")
    );
    assert_eq!(
        report.personal_fleet_definitions,
        crate::fleet::exact::personal_fleet_definitions_dir().expect("personal fleets")
    );
    assert_eq!(
        report.personal_fleet_agents,
        crate::fleet::profile::personal_agent_profile_dir().expect("personal agents")
    );
    assert_eq!(report.secrets, secrets);
    assert_eq!(legacy_secrets, None);
    assert_eq!(report.entries().len(), 14);
    let json = serde_json::to_value(&report).expect("serialize path snapshot");
    for (label, path) in report.entries() {
        assert_eq!(
            json[label].as_str(),
            Some(path.to_string_lossy().as_ref()),
            "human and JSON path snapshots diverged for {label}"
        );
    }
    assert!(
        !home.exists(),
        "path reporting must not create the configured home"
    );
}

#[test]
fn explicit_relative_config_matches_the_canonical_loader_path() {
    let relative = Path::new("fixtures/relative-doctor-config.toml");
    let expected = codewhale_config::resolve_config_path(Some(relative.to_path_buf()))
        .expect("canonical config path");

    let report = DoctorPathReport::resolve(Some(relative)).expect("resolve doctor paths");

    assert_eq!(report.config, expected);
    assert!(report.config.is_absolute());
}

#[test]
fn path_report_json_contains_no_secret_file_contents() {
    let _lock = crate::test_support::lock_test_env();
    let temp = tempfile::tempdir().expect("temp home");
    let home = temp.path().join("isolated-codewhale-home");
    let secret_path = home.join("secrets").join("secrets.json");
    std::fs::create_dir_all(secret_path.parent().unwrap()).expect("secret dir fixture");
    let sentinel = "doctor-path-report-secret-sentinel";
    std::fs::write(&secret_path, sentinel).expect("secret fixture");
    let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.as_os_str());

    let report = DoctorPathReport::resolve(None).expect("resolve doctor paths");
    let json = serde_json::to_string(&report).expect("serialize path report");

    assert!(!json.contains(sentinel));
    assert_eq!(std::fs::read_to_string(secret_path).unwrap(), sentinel);
}

#[test]
fn human_and_json_backend_reports_never_include_secret_file_contents() {
    let _lock = crate::test_support::lock_test_env();
    let temp = tempfile::tempdir().expect("temp home");
    let home = temp.path().join("isolated-codewhale-home");
    let secret_path = home.join("secrets").join("secrets.json");
    std::fs::create_dir_all(secret_path.parent().unwrap()).expect("secret dir fixture");
    let sentinel = "doctor-render-secret-sentinel";
    std::fs::write(&secret_path, format!("not-json:{sentinel}")).expect("secret fixture");
    let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.as_os_str());
    let _backend = crate::test_support::EnvVarGuard::set("CODEWHALE_SECRET_BACKEND", "file");

    let diagnostic = codewhale_secrets::diagnose_secret_backend();
    let human = secret_backend_human_lines(&diagnostic).join("\n");
    let json = serde_json::to_string(&diagnostic).expect("serialize backend diagnostic");

    assert!(!human.contains(sentinel));
    assert!(!json.contains(sentinel));
    assert_eq!(
        std::fs::read_to_string(secret_path).unwrap(),
        format!("not-json:{sentinel}")
    );
}

#[test]
fn structural_url_authority_omits_every_secret_capable_component() {
    let sentinels = [
        "URL-USER-SENTINEL",
        "URL-PASSWORD-SENTINEL",
        "URL-PATH-SENTINEL",
        "URL-QUERY-KEY-SENTINEL",
        "URL-QUERY-VALUE-SENTINEL",
        "URL-FRAGMENT-SENTINEL",
    ];
    let raw = format!(
        "https://{}:{}@example.invalid:8443/{}/child?{}={}#{}",
        sentinels[0], sentinels[1], sentinels[2], sentinels[3], sentinels[4], sentinels[5]
    );

    let authority = structural_url_authority(&raw);

    assert_eq!(authority, "https://example.invalid:8443");
    for sentinel in sentinels {
        assert!(!authority.contains(sentinel));
    }
}

#[test]
fn credential_shaped_config_values_are_flagged_by_key_name_only() {
    // Fixture tokens stay low-entropy on purpose: realistic random strings
    // trip secret scanners (GitGuardian flagged the originals as live credentials).
    let raw = r#"
# comment with sk-not-a-real-line
model = "deepseek-v4-flash"
base_url = "https://api.moonshot.ai/kimi-code/v1"
chatgpt_access_token = "eyJ0000000000000000000000000"
moonshot_api_key = "[redacted]"
provider_api_key = "sk-test0000000000000000"
workspace_token_note = "short"
random_id = "0123456789abcdef0123456789abcdef"
"#;
    let flagged = super::config_credential_shaped_keys(raw);
    assert_eq!(flagged, vec!["chatgpt_access_token", "provider_api_key"]);
}

#[test]
fn credential_scan_ignores_urls_models_and_redacted_entries() {
    let raw = r#"
model = "kimi-k3-instruct-preview-2026"
endpoint = "https://example.com/v1?key=nope"
api_key = "[redacted]"
"#;
    assert!(super::config_credential_shaped_keys(raw).is_empty());
}