scsh 1.41.15

Scoped Skills Helper — preflight a git repo and run its scoped skills in ephemeral containers.
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
//! Setup tab payload: per-harness image + login readiness and the canonical model catalog.
//!
//! `GET /api/v1/setup` composes existing image inspect + host login preflight into a
//! browser-safe JSON shape. No secrets, no automatic model calls — probes are started
//! explicitly via `POST /api/v1/setup/tests`.

use crate::config::Harness;
use crate::json::quote;
use crate::runtime::{self, ImageStatus, LoginPreflight};

/// One curated catalog entry (primary smoke or additional built-in).
pub struct CatalogModel {
  pub id: &'static str,
  /// `"primary"` (default selected smoke) or `"builtin"` (shown, opt-in).
  pub kind: &'static str,
}

/// Canonical harness → models matrix shared with the built-in `doctor` definition.
/// Primary entries match `doctor.yml`; additional entries cover demo/builtin aliases.
pub fn catalog_models(harness: Harness) -> &'static [CatalogModel] {
  match harness {
    Harness::Opencode => &[
      CatalogModel { id: "openai/gpt-5.6-luna", kind: "primary" },
      CatalogModel { id: "openai/gpt-5.6-terra", kind: "builtin" },
    ],
    Harness::Claude => &[
      CatalogModel { id: "sonnet", kind: "primary" },
      CatalogModel { id: "claude-opus-4-8", kind: "builtin" },
      CatalogModel { id: "claude-fable-5", kind: "builtin" },
    ],
    Harness::Codex => &[
      CatalogModel { id: "gpt-5.6-luna", kind: "primary" },
      CatalogModel { id: "gpt-5.6-terra", kind: "builtin" },
      CatalogModel { id: "gpt-5.6-sol", kind: "builtin" },
      CatalogModel { id: "gpt-5.6-spark", kind: "builtin" },
    ],
    Harness::Grok => {
      &[CatalogModel { id: "grok-build", kind: "primary" }, CatalogModel { id: "grok-4.5", kind: "builtin" }]
    }
    Harness::Cursor => {
      &[CatalogModel { id: "auto", kind: "primary" }, CatalogModel { id: "composer-2.5", kind: "builtin" }]
    }
  }
}

/// Primary smoke model id for a harness (first `primary` catalog entry).
pub fn primary_model_id(harness: Harness) -> &'static str {
  catalog_models(harness)
    .iter()
    .find(|m| m.kind == "primary")
    .map(|m| m.id)
    .expect("every harness has a primary catalog model")
}

/// Validate a custom model id from the browser (no secrets, no shell metacharacters).
pub fn validate_custom_model_id(raw: &str) -> Result<String, String> {
  let id = raw.trim();
  if id.is_empty() {
    return Err("model id is empty".into());
  }
  if id.len() > 128 {
    return Err("model id is too long (max 128 characters)".into());
  }
  if id.chars().any(|c| c.is_control() || c == '\n' || c == '\r' || c == '"' || c == '\'' || c == '`' || c == '$') {
    return Err("model id has unsafe characters".into());
  }
  Ok(id.to_string())
}

fn image_status_word(img: &ImageStatus, engine_running: bool) -> &'static str {
  if !engine_running {
    // A stopped engine answers no inspect, so scsh does not know whether the image is
    // there. Saying "missing" would be a guess that reads as "click Build" — see
    // [`unknown_image_statuses`].
    "unknown"
  } else if !img.exists {
    "missing"
  } else if !img.up_to_date {
    "stale"
  } else {
    "ready"
  }
}

fn image_json(s: &ImageStatus, engine_running: bool) -> String {
  format!(
    "{{ \"name\": {}, \"tag\": {}, \"exists\": {}, \"up_to_date\": {}, \"status\": {}, \"created\": {}, \"size\": {} }}",
    quote(&s.name),
    quote(&s.tag),
    s.exists,
    s.up_to_date,
    quote(image_status_word(s, engine_running)),
    s.created.as_deref().map(quote).unwrap_or_else(|| "null".into()),
    s.size.as_deref().map(quote).unwrap_or_else(|| "null".into()),
  )
}

/// Engine liveness for the browser: is the runtime actually up, and if not, the exact
/// command that starts it. The CLI answers this before every `scsh run`
/// (`preflight_runtime_engine`); this is the same answer, shaped for the Setup tab.
fn engine_json(rt_name: &str, running: bool) -> String {
  let start = crate::ui::engine::start_command(rt_name, crate::ui::Os::current());
  format!(
    "{{ \"running\": {}, \"runtime\": {}, \"name\": {}, \"start_command\": {} }}",
    running,
    quote(rt_name),
    quote(&crate::ui::engine::display_name(rt_name)),
    start.as_deref().map(quote).unwrap_or_else(|| "null".into()),
  )
}

/// Every image scsh knows about, in an explicitly unknown state — what the panel shows
/// when the engine is installed but stopped.
///
/// The alternative is to inspect for real, but a stopped engine fails every single
/// `image inspect`, so all of them come back `exists: false` and the tab renders a red
/// "Needs build" badge plus a Build button that cannot work. One unreachable engine is
/// the truth; N missing images is a fabrication.
fn unknown_image_statuses() -> Vec<ImageStatus> {
  let unknown =
    |name: String, tag: String| ImageStatus { name, tag, exists: false, up_to_date: false, created: None, size: None };
  let mut out = vec![unknown("base".into(), runtime::BASE_IMAGE_TAG.to_string())];
  for h in Harness::ALL {
    out.push(unknown(h.as_str().into(), runtime::image_tag(h)));
  }
  out
}

fn login_json(login: &LoginPreflight) -> String {
  format!(
    "{{ \"status\": {}, \"label\": {}, \"hint\": {} }}",
    quote(login.status),
    quote(&login.label),
    quote(&login.hint),
  )
}

fn models_json(harness: Harness) -> String {
  let primary = primary_model_id(harness);
  let parts: Vec<String> = catalog_models(harness)
    .iter()
    .map(|m| {
      format!(
        "{{ \"id\": {}, \"kind\": {}, \"primary_smoke\": {}, \"status\": \"not_tested\" }}",
        quote(m.id),
        quote(m.kind),
        m.id == primary,
      )
    })
    .collect();
  format!("[{}]", parts.join(", "))
}

/// Overall harness readiness for Setup (no end-to-end model probes yet on GET).
/// Never claims full "ready" — that requires passed model tests.
fn overall_status(image: &ImageStatus, login: &LoginPreflight, engine_running: bool) -> &'static str {
  if !engine_running {
    "unknown"
  } else if !image.exists || !image.up_to_date {
    "needs_build"
  } else if login.status != "found" {
    "needs_login"
  } else {
    "not_tested"
  }
}

fn overall_label(overall: &str) -> &'static str {
  match overall {
    "needs_build" => "Needs build",
    "needs_login" => "Needs login",
    "not_tested" => "Ready to test",
    "unknown" => "Engine stopped",
    _ => "Unknown",
  }
}

fn action_json(image: &ImageStatus, login: &LoginPreflight, overall: &str, engine_start: Option<&str>) -> String {
  // A stopped engine blocks every action on the card — building and probing both need a
  // live runtime — so the card offers the one command that unblocks them instead.
  if overall == "unknown" {
    let hint = match engine_start {
      Some(cmd) => format!("Start the container engine with `{cmd}`, then refresh."),
      None => "Start the container engine, then refresh.".to_string(),
    };
    return format!("{{ \"kind\": \"blocked\", \"label\": {}, \"hint\": {} }}", quote("Engine stopped"), quote(&hint));
  }
  match overall {
    "needs_build" => {
      let (kind, label) = if !image.exists { ("build", "Build image") } else { ("update", "Update image") };
      format!(
        "{{ \"kind\": {}, \"label\": {}, \"hint\": {} }}",
        quote(kind),
        quote(label),
        quote("Build this harness image for the selected runtime"),
      )
    }
    "needs_login" => {
      format!("{{ \"kind\": \"login\", \"label\": {}, \"hint\": {} }}", quote("Sign in on host"), quote(&login.hint),)
    }
    _ => {
      r#"{ "kind": "test", "label": "Test selected models", "hint": "Runs a real container probe for each checked model (provider calls may incur cost)" }"#
        .to_string()
    }
  }
}

/// `GET /api/v1/setup` — readiness dashboard for the Setup tab.
pub fn setup_json(runtime_override: Option<&str>) -> String {
  let available = runtime::available_runtimes();
  let rt_name: String = match runtime_override.filter(|r| !r.is_empty()) {
    Some(rt) if available.contains(&rt) => rt.to_string(),
    Some(rt) => {
      return format!(
        "{{ \"error\": {} }}",
        quote(&format!("runtime '{rt}' is not installed (available: {})", available.join(", ")))
      );
    }
    None => match runtime::detect_runtime() {
      Some(rt) => rt.name,
      None => {
        return r#"{ "error": "no container runtime found (docker, podman, or Apple container)" }"#.to_string();
      }
    },
  };
  let available_json: Vec<String> = available.iter().map(|r| quote(r)).collect();
  // `available_runtimes` is a `which` on $PATH — it says the binary exists, not that the
  // engine is up. Probe liveness once here (this GET is event-driven, never polled) and,
  // when it is down, skip the inspects entirely: they would each fail slowly and lie.
  let engine_running = crate::ui::engine::is_running(&rt_name);
  let engine_start = crate::ui::engine::start_command(&rt_name, crate::ui::Os::current());
  let statuses = if engine_running { runtime::image_statuses(&rt_name) } else { unknown_image_statuses() };
  let images_json: Vec<String> = statuses.iter().map(|s| image_json(s, engine_running)).collect();
  let base = statuses.iter().find(|s| s.name == "base");

  let mut needs_build = 0u32;
  let mut needs_login = 0u32;
  let mut not_tested = 0u32;
  let mut harness_rows = Vec::new();

  for h in Harness::ALL {
    let img = statuses.iter().find(|s| s.name == h.as_str()).cloned().unwrap_or(ImageStatus {
      name: h.as_str().into(),
      tag: runtime::image_tag(h),
      exists: false,
      up_to_date: false,
      created: None,
      size: None,
    });
    let login = runtime::harness_login_preflight(h);
    let overall = overall_status(&img, &login, engine_running);
    match overall {
      "needs_build" => needs_build += 1,
      "needs_login" => needs_login += 1,
      // "unknown" counts toward nothing: with the engine down scsh cannot say what any
      // harness needs, and a zeroed summary is honest where a guessed one is not.
      "unknown" => {}
      _ => not_tested += 1,
    }
    harness_rows.push(format!(
      "{{ \"id\": {}, \"name\": {}, \"overall\": {}, \"overall_label\": {}, \"image\": {}, \"login\": {}, \"models\": {}, \"action\": {} }}",
      quote(h.as_str()),
      quote(h.display_name()),
      quote(overall),
      quote(overall_label(overall)),
      image_json(&img, engine_running),
      login_json(&login),
      models_json(h),
      action_json(&img, &login, overall, engine_start.as_deref()),
    ));
  }

  let checked_at = crate::daemon::paths::now_unix_secs();
  format!(
    "{{ \"runtime\": {}, \"available\": [{}], \"engine\": {}, \"checked_at\": {}, \"summary\": {{ \"needs_build\": {}, \"needs_login\": {}, \"not_tested\": {}, \"agents\": {} }}, \"harnesses\": [{}], \"base\": {}, \"images\": [{}] }}",
    quote(&rt_name),
    available_json.join(", "),
    engine_json(&rt_name, engine_running),
    checked_at,
    needs_build,
    needs_login,
    not_tested,
    Harness::ALL.len(),
    harness_rows.join(", "),
    base.map(|s| image_json(s, engine_running)).unwrap_or_else(|| "null".into()),
    images_json.join(", "),
  )
}

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

  #[test]
  fn setup_json_error_when_runtime_unknown() {
    let j = setup_json(Some("not-a-runtime-xyz"));
    assert!(j.contains("\"error\""), "{j}");
    assert!(j.contains("not-a-runtime-xyz"), "{j}");
  }

  fn image(exists: bool, up_to_date: bool) -> ImageStatus {
    ImageStatus { name: "claude".into(), tag: "scsh-claude:1".into(), exists, up_to_date, created: None, size: None }
  }

  fn login(status: &'static str) -> LoginPreflight {
    LoginPreflight { status, label: status.into(), hint: "sign in".into() }
  }

  /// The regression this whole path exists for: with the engine down, a ready harness must
  /// NOT read as "needs build". Every inspect fails when the engine is stopped, and the old
  /// code turned that into a red badge over a Build button that could not possibly work.
  #[test]
  fn a_stopped_engine_reads_as_unknown_not_needs_build() {
    let ready = image(true, true);
    assert_eq!(overall_status(&ready, &login("found"), true), "not_tested");
    assert_eq!(overall_status(&ready, &login("found"), false), "unknown");
    // Even a genuinely missing image is only "unknown" while the engine cannot answer.
    assert_eq!(overall_status(&image(false, false), &login("found"), false), "unknown");
    assert_eq!(overall_status(&image(false, false), &login("found"), true), "needs_build");
    assert_eq!(overall_label("unknown"), "Engine stopped");
    assert_eq!(image_status_word(&ready, false), "unknown");
    assert_eq!(image_status_word(&ready, true), "ready");
  }

  /// A blocked card offers the start command instead of a button that cannot work.
  #[test]
  fn a_stopped_engine_offers_the_start_command_and_no_actions() {
    let a = action_json(&image(true, true), &login("found"), "unknown", Some("container system start"));
    assert!(a.contains("\"kind\": \"blocked\""), "{a}");
    assert!(a.contains("container system start"), "{a}");
    // Unknown runtimes have no canned command, so the hint degrades rather than lying.
    let bare = action_json(&image(true, true), &login("found"), "unknown", None);
    assert!(bare.contains("\"kind\": \"blocked\""), "{bare}");
    assert!(!bare.contains('`'), "{bare}");
  }

  /// The placeholder inventory keeps every row visible (no empty limbo) without claiming
  /// anything about images it could not inspect.
  #[test]
  fn unknown_inventory_covers_base_and_every_harness() {
    let rows = unknown_image_statuses();
    assert_eq!(rows.len(), Harness::ALL.len() + 1);
    assert!(rows.iter().any(|s| s.name == "base"));
    for h in Harness::ALL {
      assert!(rows.iter().any(|s| s.name == h.as_str()), "{h:?}");
    }
    assert!(rows.iter().all(|s| !s.exists && !s.up_to_date && !s.tag.is_empty()));
    assert!(rows.iter().all(|s| image_status_word(s, false) == "unknown"));
  }

  /// The payload always carries an `engine` object, so the browser can tell "engine is up"
  /// from "this daemon is too old to know" (field absent) rather than guessing.
  #[test]
  fn engine_json_reports_liveness_and_the_start_command() {
    let up = engine_json("docker", true);
    assert!(up.contains("\"running\": true"), "{up}");
    assert!(up.contains("Docker"), "{up}");
    let down = engine_json("container", false);
    assert!(down.contains("\"running\": false"), "{down}");
    assert!(down.contains("Apple container"), "{down}");
    assert!(down.contains("container system start"), "{down}");
    // No canned advice for an SCSH_RUNTIME scsh does not know: null, never a wrong command.
    assert!(engine_json("nerdctl", false).contains("\"start_command\": null"));
  }

  #[test]
  fn catalog_marks_one_primary_per_harness() {
    for h in Harness::ALL {
      let cats = catalog_models(h);
      assert!(!cats.is_empty(), "{h:?}");
      assert_eq!(cats.iter().filter(|m| m.kind == "primary").count(), 1, "{h:?}");
    }
  }

  #[test]
  fn setup_orders_native_harnesses_before_opencode() {
    assert_eq!(Harness::ALL, [Harness::Claude, Harness::Codex, Harness::Grok, Harness::Cursor, Harness::Opencode]);
  }

  #[test]
  fn doctor_primaries_are_in_the_catalog() {
    let expected = [
      (Harness::Opencode, "openai/gpt-5.6-luna"),
      (Harness::Claude, "sonnet"),
      (Harness::Codex, "gpt-5.6-luna"),
      (Harness::Grok, "grok-build"),
      (Harness::Cursor, "auto"),
    ];
    for (h, id) in expected {
      assert_eq!(primary_model_id(h), id, "{h:?}");
    }
    assert!(!Harness::ALL.into_iter().flat_map(catalog_models).any(|model| model.id.contains("mini")));
  }

  #[test]
  fn codex_catalog_offers_sol_as_a_builtin() {
    assert!(catalog_models(Harness::Codex).iter().any(|m| m.id == "gpt-5.6-sol" && m.kind == "builtin"));
  }

  #[test]
  fn gorgeous_pipeline_models_are_catalog_builtins() {
    // The pipeline's Fable orchestration/fix routes and Spark reviewer lanes must be
    // offered by the Setup catalog alongside the models they run next to.
    assert!(catalog_models(Harness::Claude).iter().any(|m| m.id == "claude-fable-5" && m.kind == "builtin"));
    assert!(catalog_models(Harness::Codex).iter().any(|m| m.id == "gpt-5.6-spark" && m.kind == "builtin"));
  }

  #[test]
  fn custom_model_validation_rejects_unsafe() {
    assert!(validate_custom_model_id("").is_err());
    assert!(validate_custom_model_id("  ").is_err());
    assert!(validate_custom_model_id(&"a".repeat(200)).is_err());
    assert!(validate_custom_model_id("bad`id").is_err());
    assert_eq!(validate_custom_model_id("ok/model-1.2").unwrap(), "ok/model-1.2");
  }

  #[test]
  fn overall_label_for_untested_is_actionable() {
    assert_eq!(super::overall_label("not_tested"), "Ready to test");
  }

  #[test]
  fn login_preflight_disabled_when_opted_out() {
    std::env::set_var("SCSH_NO_CLAUDE_AUTH", "1");
    let login = runtime::harness_login_preflight(Harness::Claude);
    std::env::remove_var("SCSH_NO_CLAUDE_AUTH");
    assert_eq!(login.status, "disabled");
    assert!(login.label.contains("Disabled"));
    assert!(login.hint.contains("SCSH_NO_CLAUDE_AUTH"));
  }

  #[test]
  fn parse_setup_tests_accepts_a_batch() {
    use crate::json::{parse, Value};
    let Value::Object(obj) = parse(
      r#"{"tests":[{"harness":"claude","model":"sonnet"},{"harness":"codex","model":"gpt-5.6-luna","effort":"low"}]}"#,
    )
    .unwrap() else {
      panic!("object");
    };
    let tests = parse_setup_tests(&obj).unwrap();
    assert_eq!(tests.len(), 2);
    assert_eq!(tests[0].harness, Harness::Claude);
    assert_eq!(tests[0].model, "sonnet");
    assert_eq!(tests[1].effort.as_deref(), Some("low"));
  }

  #[test]
  fn render_setup_batch_yaml_lists_invocations() {
    let yaml = render_setup_batch_yaml(&[SetupTestRequest {
      harness: Harness::Grok,
      model: "grok-composer-2.5-fast".into(),
      effort: None,
    }]);
    assert!(yaml.contains("harness: grok"), "{yaml}");
    assert!(yaml.contains("model: grok-composer-2.5-fast"), "{yaml}");
  }
}

/// One requested model probe from `POST /api/v1/setup/tests`.
#[derive(Clone, Debug)]
pub struct SetupTestRequest {
  pub harness: Harness,
  pub model: String,
  pub effort: Option<String>,
}

// Prefix only — each probe run scaffolds `smoketest-<nonce>` under ~/.scsh/projects.
const SETUP_TESTS_PROJECT: &str = "smoketest";
// Runs and their result files are named `smoketest-<harness>-<model>` after this def.
const SETUP_BATCH_DEF: &str = "smoketest";
const MAX_SETUP_TESTS: usize = 10;

/// Parse and validate the `tests` array from a setup-tests POST body.
pub fn parse_setup_tests(obj: &[(String, crate::json::Value)]) -> Result<Vec<SetupTestRequest>, String> {
  use crate::json::Value;
  let Some((_, Value::Array(arr))) = obj.iter().find(|(k, _)| k == "tests") else {
    return Err("give a non-empty tests array".into());
  };
  if arr.is_empty() {
    return Err("give a non-empty tests array".into());
  }
  if arr.len() > MAX_SETUP_TESTS {
    return Err(format!("at most {MAX_SETUP_TESTS} model tests per request"));
  }
  let mut out = Vec::new();
  let mut seen = std::collections::BTreeSet::new();
  for item in arr {
    let Value::Object(fields) = item else {
      return Err("each test must be an object".into());
    };
    let harness_s = fields
      .iter()
      .find(|(k, _)| k == "harness")
      .and_then(|(_, v)| match v {
        Value::String(s) => Some(s.as_str()),
        _ => None,
      })
      .ok_or_else(|| "each test needs a harness".to_string())?;
    let harness = Harness::parse(harness_s).ok_or_else(|| format!("unknown harness '{harness_s}'"))?;
    let model_raw = fields
      .iter()
      .find(|(k, _)| k == "model")
      .and_then(|(_, v)| match v {
        Value::String(s) => Some(s.as_str()),
        _ => None,
      })
      .ok_or_else(|| "each test needs a model".to_string())?;
    let model = validate_custom_model_id(model_raw)?;
    let effort = fields.iter().find(|(k, _)| k == "effort").and_then(|(_, v)| match v {
      Value::String(s) if !s.trim().is_empty() => Some(s.trim().to_string()),
      _ => None,
    });
    if let Some(ref e) = effort {
      let allowed = harness.effort_levels();
      if !allowed.is_empty() && !allowed.contains(&e.as_str()) {
        return Err(format!("effort '{e}' is not valid for {}", harness.as_str()));
      }
    }
    let key = format!("{}::{}", harness.as_str(), model);
    if !seen.insert(key) {
      return Err(format!("duplicate test for {} / {model}", harness.as_str()));
    }
    out.push(SetupTestRequest { harness, model, effort });
  }
  Ok(out)
}

/// Scaffold a fresh `~/.scsh/projects/smoketest-<nonce>` project; write the batch def YAML.
pub fn prepare_setup_batch(tests: &[SetupTestRequest]) -> Result<std::path::PathBuf, String> {
  let root = ensure_setup_tests_project()?;
  let harness_dir = root.join(".harness");
  std::fs::create_dir_all(&harness_dir).map_err(|e| format!("could not create .harness/: {e}"))?;
  let yaml = render_setup_batch_yaml(tests);
  let path = harness_dir.join(format!("{SETUP_BATCH_DEF}.yml"));
  std::fs::write(&path, yaml).map_err(|e| format!("could not write smoketest def: {e}"))?;
  Ok(root)
}

fn ensure_setup_tests_project() -> Result<std::path::PathBuf, String> {
  let projects = crate::daemon::paths::projects_dir();
  std::fs::create_dir_all(&projects).map_err(|e| format!("could not create {}: {e}", projects.display()))?;
  // One fresh project per probe run. A fixed directory name could collide with a real
  // project the user created, and the one-job-per-repository guard would serialize
  // concurrent Setup probes behind it. The scaffolds are tiny (one gitignore commit).
  let path = projects.join(format!("{SETUP_TESTS_PROJECT}-{}", crate::runtime::random_nonce_6()));
  if path.exists() {
    let _ = std::fs::remove_dir_all(&path);
  }
  // Reuse the same scaffold as Projects → New project.
  scaffold_setup_tests_project(&path)?;
  Ok(path)
}

fn scaffold_setup_tests_project(path: &std::path::Path) -> Result<(), String> {
  let git = |args: &[&str]| -> Result<(), String> {
    let out = crate::git_command()
      .arg("-C")
      .arg(path)
      .args(args)
      .output()
      .map_err(|e| format!("git {}: {e}", args.first().unwrap_or(&"")))?;
    if out.status.success() {
      Ok(())
    } else {
      Err(format!("git {} failed: {}", args.first().unwrap_or(&""), String::from_utf8_lossy(&out.stderr).trim()))
    }
  };
  std::fs::create_dir(path).map_err(|e| format!("could not create {}: {e}", path.display()))?;
  git(&["init", "-q"])?;
  std::fs::write(path.join(".gitignore"), "# scsh scratch — results, logs, cache. Never tracked.\n/tmp\n.harness/\n")
    .map_err(|e| format!("could not write .gitignore: {e}"))?;
  std::fs::create_dir_all(path.join("tmp")).map_err(|e| format!("could not create tmp/: {e}"))?;
  git(&["add", ".gitignore"])?;
  git(&[
    "-c",
    &format!("user.name={}", crate::SCSH_COMMIT_NAME),
    "-c",
    &format!("user.email={}", crate::SCSH_COMMIT_EMAIL),
    "commit",
    "-qm",
    "Init smoketest project.",
  ])
}

fn render_setup_batch_yaml(tests: &[SetupTestRequest]) -> String {
  let mut out = String::from(
    r#"# Generated by the Setup tab — do not edit by hand.
description: "Setup model probes from the session browser."
task: |
  Connectivity probe. Write the JSON object
  {"ok": true, "model": "<the model id you are running as>",
   "message": "connectivity ok - responding as <the model id you are running as>"}
  to the file named by the SCSH_RESULT environment variable, then stop. Do nothing else.
invocations:
"#,
  );
  for (i, t) in tests.iter().enumerate() {
    let route = format!(
      "{}-{}",
      t.harness.as_str(),
      t.model
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '-' })
        .collect::<String>()
    );
    let route = if route.is_empty() { format!("t{i}") } else { route };
    out.push_str(&format!(
      "  {route}:\n    harness: {}\n    model: {}\n",
      t.harness.as_str(),
      yaml_escape_scalar(&t.model)
    ));
    if let Some(ref e) = t.effort {
      out.push_str(&format!("    effort: {e}\n"));
    } else if t.harness == Harness::Codex {
      out.push_str("    effort: low\n");
    }
  }
  out
}

fn yaml_escape_scalar(s: &str) -> String {
  if s.chars().any(|c| c.is_whitespace() || ":#{}[],&*?|>!%@`'\"".contains(c)) {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
  } else {
    s.to_string()
  }
}

pub fn setup_batch_def_name() -> &'static str {
  SETUP_BATCH_DEF
}