spawningpool-cli 0.3.0

CLI for spawningpool — create hyper-specific, 0-waste agents
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
//! Tests for [`super`]. Extracted from `main.rs` and included via
//! `#[path]` so they remain a child module with access to private items.

use std::collections::HashMap;
use std::path::PathBuf;

use spawningpool::ai::{Api, Reasoning};
use spawningpool::{ModelDef, ProviderDef, Registry, Specialist};

use crate::cli::{DefineEntity, DeleteEntity, ListKind, ShowEntity};
use crate::commands::define::{
    check_model_refs, check_specialist_refs, define, parse_list, parse_reasoning, resolve_script,
};
use crate::commands::delete::{
    affirmative, delete, referrers_of_model, referrers_of_provider, referrers_of_tool,
};
use crate::commands::list::list;
use crate::commands::show::show;
use crate::display::{available_names, onboarding_message, progress, unset_key_warnings};

/// Serializes the tests below that point `$SPAWNINGPOOL_REGISTRY` at a temp
/// file, since that env var is process-wide and tests otherwise run parallel.
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[test]
fn parse_list_splits_and_trims() {
    assert_eq!(
        parse_list(Some("a, b ,c".into())),
        vec!["a".to_string(), "b".to_string(), "c".to_string()]
    );
    assert!(parse_list(None).is_empty());
    assert!(parse_list(Some("  ,  ".into())).is_empty());
}

#[test]
fn affirmative_only_accepts_an_explicit_yes() {
    for yes in ["y", "Y", "yes", "Yes", " yes \n"] {
        assert!(affirmative(yes), "{yes:?} should confirm");
    }
    // Empty (the EOF case on a non-interactive stdin) and anything else decline.
    for no in ["", "\n", "n", "no", "yep", "1"] {
        assert!(!affirmative(no), "{no:?} should decline");
    }
}

#[test]
fn parse_reasoning_maps_levels_and_rejects_unknown() {
    assert_eq!(parse_reasoning("high"), Ok(Reasoning::High));
    assert_eq!(parse_reasoning("off"), Ok(Reasoning::Off));
    assert!(parse_reasoning("ultra").is_err());
}

fn write_script(body: &str) -> PathBuf {
    use std::os::unix::fs::PermissionsExt;
    let path = std::env::temp_dir().join(format!(
        "sp_cli_tool_{}_{}.sh",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::write(&path, body).unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
    path
}

fn restore_registry_env(saved: Option<std::ffi::OsString>) {
    match saved {
        Some(v) => std::env::set_var("SPAWNINGPOOL_REGISTRY", v),
        None => std::env::remove_var("SPAWNINGPOOL_REGISTRY"),
    }
}

#[test]
fn define_list_show_and_delete_round_trip_through_the_store() {
    let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let saved = std::env::var_os("SPAWNINGPOOL_REGISTRY");
    let dir = std::env::temp_dir().join(format!("sp_cli_define_{}", std::process::id()));
    let path = dir.join("registry.json");
    std::env::set_var("SPAWNINGPOOL_REGISTRY", &path);

    define(DefineEntity::Provider {
        name: "anthropic".into(),
        api: "anthropic-messages".into(),
        base_url: "https://api.anthropic.com".into(),
        api_key_env: Some("ANTHROPIC_API_KEY".into()),
        constrained_decoding: false,
    })
    .unwrap();

    // The provider is persisted and reloads from disk.
    assert!(spawningpool::store::load()
        .unwrap()
        .providers
        .contains_key("anthropic"));
    // Listing succeeds against the populated registry. Driven on a local
    // runtime rather than `#[tokio::test]` so the env-serializing guard is
    // never held across an await point.
    tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(list(ListKind::Providers))
        .unwrap();
    // Showing a defined entity succeeds; an absent one errors.
    show(ShowEntity::Provider {
        name: "anthropic".into(),
    })
    .unwrap();
    let err = show(ShowEntity::Provider {
        name: "ghost".into(),
    })
    .unwrap_err();
    assert!(err.contains("no such"));

    // Deleting it removes it. `yes` skips the interactive confirmation.
    delete(
        DeleteEntity::Provider {
            name: "anthropic".into(),
        },
        true,
    )
    .unwrap();
    assert!(!spawningpool::store::load()
        .unwrap()
        .providers
        .contains_key("anthropic"));

    // Deleting something absent is an error.
    let err = delete(
        DeleteEntity::Provider {
            name: "ghost".into(),
        },
        true,
    )
    .unwrap_err();
    assert!(err.contains("no such"));

    std::fs::remove_dir_all(&dir).ok();
    restore_registry_env(saved);
}

#[test]
fn define_specialist_rejects_tools_and_constraint_together() {
    let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let saved = std::env::var_os("SPAWNINGPOOL_REGISTRY");
    let dir = std::env::temp_dir().join(format!("sp_cli_val_{}", std::process::id()));
    let path = dir.join("registry.json");
    std::env::set_var("SPAWNINGPOOL_REGISTRY", &path);

    let err = define(DefineEntity::Specialist {
        name: "bad".into(),
        provider: "p".into(),
        model: "m".into(),
        system_prompt: "s".into(),
        tools: Some("a,b".into()),
        constraint: Some("a".into()),
        reasoning: "off".into(),
        stream: false,
    })
    .unwrap_err();
    assert!(err.contains("tools and a constraint"));

    std::fs::remove_dir_all(&dir).ok();
    restore_registry_env(saved);
}

fn populated_registry() -> Registry {
    let mut registry = Registry::default();
    registry.providers.insert(
        "anthropic".into(),
        ProviderDef {
            name: "anthropic".into(),
            api: spawningpool::ai::Api::AnthropicMessages,
            base_url: "https://api.anthropic.com".into(),
            api_key_env: Some("ANTHROPIC_API_KEY".into()),
            constrained_decoding: false,
        },
    );
    registry.models.insert(
        "claude".into(),
        ModelDef {
            id: "claude".into(),
            name: "Claude".into(),
            provider: "anthropic".into(),
            max_tokens: 1024,
            context_window: 200_000,
        },
    );
    registry
}

/// A temp tools folder containing a single executable `ping` script, plus the
/// folder path. The caller removes the folder when done.
fn tools_dir_with_ping() -> PathBuf {
    use std::os::unix::fs::PermissionsExt;
    let dir = std::env::temp_dir().join(format!(
        "sp_cli_tools_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let script = dir.join("ping");
    std::fs::write(&script, "#!/bin/sh\n# desc: Ping\necho hi\n").unwrap();
    std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
    dir
}

fn specialist_ref(
    provider: &str,
    model: &str,
    tools: Vec<String>,
    constraint: Option<String>,
) -> Specialist {
    Specialist {
        name: "spec".into(),
        provider: provider.into(),
        model: model.into(),
        system_prompt: "s".into(),
        tools,
        constraint,
        reasoning: Reasoning::Off,
        stream: false,
    }
}

#[test]
fn available_names_lists_sorted_or_notes_emptiness() {
    let mut map: HashMap<String, u8> = HashMap::new();
    assert_eq!(available_names(&map), "(none defined yet)");
    map.insert("b".into(), 0);
    map.insert("a".into(), 0);
    assert_eq!(available_names(&map), "a, b");
}

#[test]
fn check_specialist_refs_passes_when_all_present() {
    let registry = populated_registry();
    let dir = tools_dir_with_ping();
    let spec = specialist_ref("anthropic", "claude", vec!["ping".into()], None);
    let result = check_specialist_refs(&registry, &spec, &dir);
    std::fs::remove_dir_all(&dir).ok();
    assert!(result.is_ok());
}

#[test]
fn check_specialist_refs_reports_missing_provider_model_and_tool() {
    let registry = populated_registry();
    let dir = tools_dir_with_ping();

    let err = check_specialist_refs(
        &registry,
        &specialist_ref("ghost", "claude", vec![], None),
        &dir,
    )
    .unwrap_err();
    assert!(err.contains("references provider 'ghost'"));
    assert!(err.contains("spawningpool define provider ghost"));

    let err = check_specialist_refs(
        &registry,
        &specialist_ref("anthropic", "nope", vec![], None),
        &dir,
    )
    .unwrap_err();
    assert!(err.contains("references model 'nope'"));
    assert!(err.contains("spawningpool define model nope"));

    let err = check_specialist_refs(
        &registry,
        &specialist_ref("anthropic", "claude", vec!["absent".into()], None),
        &dir,
    )
    .unwrap_err();
    std::fs::remove_dir_all(&dir).ok();
    assert!(err.contains("references tool 'absent'"));
    assert!(err.contains("spawningpool define tool absent"));
}

#[test]
fn check_specialist_refs_validates_the_constrained_tool() {
    let registry = populated_registry();
    let dir = tools_dir_with_ping();
    // A constraint names a tool too, so an undefined forced tool is caught.
    let spec = specialist_ref("anthropic", "claude", vec![], Some("absent".into()));
    let err = check_specialist_refs(&registry, &spec, &dir).unwrap_err();
    std::fs::remove_dir_all(&dir).ok();
    assert!(err.contains("references tool 'absent'"));
}

#[test]
fn check_model_refs_requires_a_defined_provider() {
    let registry = populated_registry();
    let ok = ModelDef {
        id: "m".into(),
        name: "m".into(),
        provider: "anthropic".into(),
        max_tokens: 1,
        context_window: 1,
    };
    assert!(check_model_refs(&registry, &ok).is_ok());

    let bad = ModelDef {
        provider: "ghost".into(),
        ..ok
    };
    let err = check_model_refs(&registry, &bad).unwrap_err();
    assert!(err.contains("references provider 'ghost'"));
    assert!(err.contains("spawningpool define provider ghost"));
}

#[test]
fn referrers_find_entities_pointing_at_a_target() {
    let mut registry = populated_registry();
    registry.specialists.insert(
        "spec".into(),
        specialist_ref("anthropic", "claude", vec!["ping".into()], None),
    );

    // A provider is referenced by both the specialist and the model under it.
    assert_eq!(
        referrers_of_provider(&registry, "anthropic"),
        vec![
            "specialist 'spec'".to_string(),
            "model 'claude'".to_string()
        ]
    );
    assert_eq!(
        referrers_of_model(&registry, "claude"),
        vec!["specialist 'spec'".to_string()]
    );
    assert_eq!(
        referrers_of_tool(&registry, "ping"),
        vec!["specialist 'spec'".to_string()]
    );

    // An unreferenced name has no referrers.
    assert!(referrers_of_provider(&registry, "openai").is_empty());
}

#[test]
fn referrers_of_tool_includes_a_constrained_tool() {
    let mut registry = populated_registry();
    registry.specialists.insert(
        "spec".into(),
        specialist_ref("anthropic", "claude", vec![], Some("ping".into())),
    );
    assert_eq!(
        referrers_of_tool(&registry, "ping"),
        vec!["specialist 'spec'".to_string()]
    );
}

#[test]
fn onboarding_message_walks_the_progression() {
    // Empty registry: step 1, both provider examples.
    let empty = Registry::default();
    let msg = onboarding_message(&empty);
    assert!(msg.contains("[1/4]"));
    assert!(msg.contains("spawningpool define provider anthropic"));
    assert!(msg.contains("spawningpool define provider lmstudio"));

    // Provider only: step 2, points at the real provider.
    let mut reg = Registry::default();
    reg.providers.insert(
        "anthropic".into(),
        ProviderDef {
            name: "anthropic".into(),
            api: Api::AnthropicMessages,
            base_url: "https://api.anthropic.com".into(),
            api_key_env: Some("ANTHROPIC_API_KEY".into()),
            constrained_decoding: false,
        },
    );
    let msg = onboarding_message(&reg);
    assert!(msg.contains("[2/4]"));
    assert!(msg.contains("spawningpool define model"));
    // Anthropic has no discovery endpoint, so don't offer --remote.
    assert!(!msg.contains("--remote"));

    // Model present: step 3, example uses the real provider/model.
    reg.models.insert(
        "claude".into(),
        ModelDef {
            id: "claude".into(),
            name: "Claude".into(),
            provider: "anthropic".into(),
            max_tokens: 1024,
            context_window: 200_000,
        },
    );
    let msg = onboarding_message(&reg);
    assert!(msg.contains("[3/4]"));
    assert!(msg.contains("--provider anthropic --model claude"));

    // Specialist present: step 4, run command names the real specialist.
    reg.specialists.insert(
        "summarizer".into(),
        specialist_ref("anthropic", "claude", vec![], None),
    );
    let msg = onboarding_message(&reg);
    assert!(msg.contains("[4/4]"));
    assert!(msg.contains("spawningpool run specialist spec"));
}

#[test]
fn no_models_state_offers_discovery_for_openai_providers() {
    let mut reg = Registry::default();
    reg.providers.insert(
        "lmstudio".into(),
        ProviderDef {
            name: "lmstudio".into(),
            api: Api::OpenAiCompletions,
            base_url: "http://localhost:1234/v1".into(),
            api_key_env: None,
            constrained_decoding: false,
        },
    );
    let msg = onboarding_message(&reg);
    assert!(msg.contains("spawningpool list models --remote"));
}

#[test]
fn unset_key_warnings_flags_only_missing_env_vars() {
    let reg = populated_registry();
    // The anthropic provider wants ANTHROPIC_API_KEY.
    let warnings = unset_key_warnings(&reg, |_| false);
    assert_eq!(warnings.len(), 1);
    assert!(warnings[0].contains("ANTHROPIC_API_KEY"));
    // When it's set, nothing to warn about.
    assert!(unset_key_warnings(&reg, |_| true).is_empty());
}

#[test]
fn progress_checks_completed_rungs() {
    assert!(progress(0).starts_with("  [1/4]"));
    assert!(progress(3).contains("specialist \u{2713}"));
    assert!(progress(3).starts_with("  [4/4]"));
}

#[test]
fn resolve_script_returns_absolute_path_for_executable() {
    let script = write_script("#!/bin/sh\necho hi\n");
    let resolved = resolve_script(&script).unwrap();
    std::fs::remove_file(&script).ok();
    assert!(resolved.is_absolute());
}

#[test]
fn resolve_script_rejects_non_executable_with_chmod_hint() {
    use std::os::unix::fs::PermissionsExt;
    let path = std::env::temp_dir().join(format!(
        "sp_noexec_{}_{}.sh",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::write(&path, "#!/bin/sh\necho hi\n").unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
    let err = resolve_script(&path).unwrap_err();
    std::fs::remove_file(&path).ok();
    assert!(err.contains("isn't executable"));
    assert!(err.contains("chmod +x"));
}