scsh 1.17.4

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
//! Session index table page.

use super::escape::{collapse_slashes, encode_repo_url_path, esc, percent_decode};
use super::format::{format_duration_secs, format_relative_age, format_short_age};
use super::layout::wrap_page;
use crate::daemon::model::{sessions_for_index, Session, SessionLifecycle, Store};
use crate::daemon::paths::now_unix_secs;

/// Filtered Projects/Jobs view from `/project/…` or `/repo/…`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexFilter {
  /// Bare project name under `$SCSH_HOME/projects/<name>`.
  Project(String),
  /// Absolute repository path.
  Repo(String),
}

impl IndexFilter {
  /// Absolute repo path this filter matches (projects resolve under `projects_dir`).
  pub fn repo_path(&self) -> String {
    match self {
      Self::Project(name) => crate::daemon::paths::projects_dir().join(name).to_string_lossy().into_owned(),
      Self::Repo(path) => path.clone(),
    }
  }

  pub fn label(&self) -> String {
    match self {
      Self::Project(name) => format!("project · {name}"),
      Self::Repo(path) => path.clone(),
    }
  }
}

/// Parse `/project/…` or `/repo/…` (extra slashes allowed). `None` for bare `/project` / `/repo`.
pub fn parse_index_filter(path: &str) -> Option<IndexFilter> {
  let path = path.split('?').next().unwrap_or(path);
  if let Some(rest) = path.strip_prefix("/project") {
    let name = collapse_slashes(&percent_decode(rest)).trim_matches('/').to_string();
    if name.is_empty() || name.contains('/') {
      return None;
    }
    return Some(IndexFilter::Project(name));
  }
  if let Some(rest) = path.strip_prefix("/repo") {
    let mut p = collapse_slashes(&percent_decode(rest));
    if p.is_empty() || p == "/" {
      return None;
    }
    if !p.starts_with('/') {
      p.insert(0, '/');
    }
    return Some(IndexFilter::Repo(p));
  }
  None
}

/// Which index tab is active on first paint (path-based: `/`, `/jobs`, `/projects`, `/setup`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexTab {
  Run,
  Jobs,
  Projects,
  Setup,
}

impl IndexTab {
  /// Parse a request path (`/`, `/run`, `/jobs`, `/projects`, `/setup`, `/images`).
  pub fn from_path(path: &str) -> Option<Self> {
    let path = path.split('?').next().unwrap_or(path).trim_end_matches('/');
    let path = if path.is_empty() { "/" } else { path };
    match path {
      "/" | "/run" => Some(Self::Run),
      "/jobs" => Some(Self::Jobs),
      "/projects" => Some(Self::Projects),
      "/setup" | "/images" => Some(Self::Setup),
      _ => None,
    }
  }
}

pub fn index_page(store: &Store) -> String {
  index_page_for(store, None, IndexTab::Run)
}

pub fn index_page_with_filter(store: &Store, filter: Option<IndexFilter>) -> String {
  // Filtered /project|/repo URLs open the Projects tab.
  index_page_for(store, filter, IndexTab::Projects)
}

pub fn index_page_for(store: &Store, filter: Option<IndexFilter>, tab: IndexTab) -> String {
  let port = store.port;
  let now = now_unix_secs();
  let filter_repo = filter.as_ref().map(|f| f.repo_path());
  let sessions = sessions_for_index(&store.sessions, now);
  let mut rows = String::new();
  for session in sessions {
    if let Some(ref want) = filter_repo {
      if &session.repo != want {
        continue;
      }
    }
    rows.push_str(&index_session_row(session, now));
  }
  if rows.is_empty() {
    rows = if filter.is_some() {
      "<tr><td colspan=\"7\" class=\"dim\">No jobs for this project or repository.</td></tr>\n".into()
    } else {
      "<tr><td colspan=\"7\" class=\"dim\">No jobs yet — run <code>scsh run</code> to start one.</td></tr>\n".into()
    };
  }
  let active = |want: IndexTab| if tab == want { " active" } else { "" };
  // ARIA tab pattern (server side): tablist/tab/tabpanel roles plus a roving tabindex,
  // so the markup is honest before the client script re-asserts the same state on
  // activation. New attributes stay clear of the `class= data-tab=` pair — tests and
  // the live renderer match on that exact adjacency.
  let selected = |want: IndexTab| if tab == want { "true" } else { "false" };
  let tabindex = |want: IndexTab| if tab == want { "0" } else { "-1" };
  let body = format!(
    "<nav class=\"tabs\" role=\"tablist\">\
<button id=\"tabbtn-run\" role=\"tab\" aria-selected=\"{run_s}\" aria-controls=\"tab-run\" \
tabindex=\"{run_i}\" class=\"tab{run_a}\" data-tab=\"run\">Run</button>\
<button id=\"tabbtn-jobs\" role=\"tab\" aria-selected=\"{jobs_s}\" aria-controls=\"tab-jobs\" \
tabindex=\"{jobs_i}\" class=\"tab{jobs_a}\" data-tab=\"jobs\">Jobs</button>\
<button id=\"tabbtn-projects\" role=\"tab\" aria-selected=\"{proj_s}\" aria-controls=\"tab-projects\" \
tabindex=\"{proj_i}\" class=\"tab{proj_a}\" data-tab=\"projects\">Projects</button>\
<button id=\"tabbtn-setup\" role=\"tab\" aria-selected=\"{setup_s}\" aria-controls=\"tab-setup\" \
tabindex=\"{setup_i}\" class=\"tab{setup_a}\" data-tab=\"setup\">Setup</button>\
</nav>\n\
<section class=\"tab-panel{run_p}\" id=\"tab-run\" role=\"tabpanel\" aria-labelledby=\"tabbtn-run\">\n{start}</section>\n\
<section class=\"tab-panel{jobs_p}\" id=\"tab-jobs\" role=\"tabpanel\" aria-labelledby=\"tabbtn-jobs\">\n\
<div class=\"card card--accent-left-cyan\">\n\
<p class=\"section-label\">Jobs</p>\n{harness_stops}\
<div class=\"table-scroll\"><table>\n\
<thead><tr><th>Job</th><th>Status</th><th>Started</th><th>Duration</th>\
<th>Profile</th><th>Procs</th><th>Repo</th></tr></thead>\n\
<tbody id=\"sessions-body\">\n{rows}</tbody>\n</table></div>\n\
</div>\n\
</section>\n\
<section class=\"tab-panel{proj_p}\" id=\"tab-projects\" role=\"tabpanel\" aria-labelledby=\"tabbtn-projects\">\n{dirs}</section>\n\
<section class=\"tab-panel{setup_p}\" id=\"tab-setup\" role=\"tabpanel\" aria-labelledby=\"tabbtn-setup\">\n{images}</section>\n",
    run_a = active(IndexTab::Run),
    jobs_a = active(IndexTab::Jobs),
    proj_a = active(IndexTab::Projects),
    setup_a = active(IndexTab::Setup),
    run_s = selected(IndexTab::Run),
    jobs_s = selected(IndexTab::Jobs),
    proj_s = selected(IndexTab::Projects),
    setup_s = selected(IndexTab::Setup),
    run_i = tabindex(IndexTab::Run),
    jobs_i = tabindex(IndexTab::Jobs),
    proj_i = tabindex(IndexTab::Projects),
    setup_i = tabindex(IndexTab::Setup),
    run_p = active(IndexTab::Run),
    jobs_p = active(IndexTab::Jobs),
    proj_p = active(IndexTab::Projects),
    setup_p = active(IndexTab::Setup),
    rows = rows,
    harness_stops = harness_stop_strip(store, now),
    dirs = dirs_panel(store, now, filter.as_ref()),
    start = start_panel(),
    images = images_panel()
  );
  wrap_page("scsh", port, None, "", &body)
}

/// One red "✕ stop all <harness> (n)" button per harness with running skill containers, so a
/// misbehaving harness (say, grok out of quota) can be cut across every live session at once
/// (`POST /api/v1/harness/stop`). Empty when nothing is running.
fn harness_stop_strip(store: &Store, now: u64) -> String {
  use crate::daemon::model::{ProcKind, ProcStatus};
  let mut counts: std::collections::BTreeMap<String, usize> = std::collections::BTreeMap::new();
  for s in store.sessions.values() {
    // Lifecycle, not `ended_at`: a client that died without deregistering leaves its session
    // un-ended (and its procs "running") FOREVER — those are Terminated zombies, and offering
    // to stop their long-gone containers is noise. Only genuinely live sessions count.
    if s.lifecycle_status(now) != SessionLifecycle::Running {
      continue;
    }
    for p in &s.procs {
      let live = p.status == ProcStatus::Running || p.status == ProcStatus::Waiting;
      if live && p.kind == ProcKind::Skill {
        if let Some(h) = p.harness.as_deref().filter(|h| !h.is_empty()) {
          *counts.entry(h.to_string()).or_insert(0) += 1;
        }
      }
    }
  }
  if counts.is_empty() {
    return String::new();
  }
  let mut buttons = String::new();
  for (harness, n) in &counts {
    buttons.push_str(&format!(
      "<button type=\"button\" class=\"chamfer btn btn--red btn--sm\" data-harness-stop=\"{h}\" title=\"Stop every running {h} container, in every job\"><span>✕ stop all {h} ({n})</span></button>\n",
      h = esc(harness),
    ));
  }
  format!("<div class=\"harness-stops\">{buttons}</div>\n")
}

/// The Setup panel: per-harness readiness (image + login), with the low-level image
/// inventory under a collapsed Advanced disclosure. Populated by `GET /api/v1/setup`.
///
/// First paint already lists every harness in a `checking…` state (§13: no empty limbo
/// while the runtime inspect runs). Path `/setup` (and `/images` as a compatibility alias).
fn images_panel() -> String {
  let mut cards = String::new();
  for h in crate::config::Harness::ALL {
    cards.push_str(&setup_skeleton_card(h));
  }
  let mut rows = String::new();
  rows.push_str(&images_skeleton_row("base", crate::runtime::BASE_IMAGE_TAG, true));
  for h in crate::config::Harness::ALL {
    rows.push_str(&images_skeleton_row(h.as_str(), &crate::runtime::image_tag(h), true));
  }
  format!(
    r##"<div class="card card--accent-left-orange">
<p class="section-label">Harness setup</p>
<p class="dim">Prepare and verify the agents scsh can run. Choose a runtime, build missing
images, and sign in on the host. Image freshness alone is not readiness — login is checked
separately. Model probes run only when you click <strong>Test</strong> (real provider calls).</p>
<div class="setup-toolbar">
<div id="images-runtimes" class="images-runtimes"></div>
<span id="setup-checked" class="dim"></span>
<a href="#" id="setup-refresh">refresh</a>
<button type="button" class="chamfer btn btn--cyan btn--sm" id="setup-test-all" title="One primary smoke model per ready harness"><span>Test all defaults</span></button>
</div>
<p id="setup-summary" class="setup-summary dim">checking agents…</p>
<div id="setup-cards" class="setup-cards">{cards}</div>
</div>
<div class="card card--accent-left-purple">
<p class="section-label">Images setup</p>
<p class="dim">Image tags, sizes, timestamps, base rebuilds, and force rebuilds for the
selected runtime.</p>
<div class="table-scroll"><table>
<thead><tr><th></th><th>Image</th><th>Status</th><th>Created</th><th>Size</th><th></th></tr></thead>
<tbody id="images-body">
{rows}</tbody>
</table></div>
<div class="images-controls">
<button type="button" class="chamfer btn btn--cyan btn--sm" id="images-build-selected" disabled><span>Build selected</span></button>
<button type="button" class="chamfer btn btn--orange btn--sm" id="images-build-all"><span>Build all</span></button>
<label><input type="checkbox" id="images-rebuild-base"> also rebuild the base image (--no-cache)</label>
<label><input type="checkbox" id="images-force"> force rebuild even when up to date</label>
<a href="#" id="images-refresh">refresh</a>
<span id="images-note" class="dim">checking container runtime…</span>
</div>
</div>
"##,
    cards = cards,
    rows = rows
  )
}

fn setup_skeleton_card(h: crate::config::Harness) -> String {
  format!(
    r#"<article class="setup-card" data-harness="{id}" data-pending="1">
<header class="setup-card-head">
<strong class="setup-card-name">{name}</strong>
<span class="chamfer session-status checking"><span>checking…</span></span>
</header>
<div class="setup-card-layers">
<div><span class="setup-layer-label">Image</span> <span class="setup-layer-value dim">checking…</span></div>
<div><span class="setup-layer-label">Login</span> <span class="setup-layer-value dim">checking…</span></div>
</div>
<ul class="setup-models dim"><li class="setup-models-hint">Check models, then <strong>Test selected</strong></li></ul>
<div class="setup-card-actions"><span class="dim setup-next">Real container probe — may incur provider cost</span></div>
</article>
"#,
    id = esc(h.as_str()),
    name = esc(h.display_name()),
  )
}

/// One known-image row in its first observable state: present, status `checking…`.
fn images_skeleton_row(name: &str, tag: &str, selectable: bool) -> String {
  let checkbox = if selectable {
    format!(r#"<input type="checkbox" class="image-select" value="{name}" disabled>"#, name = esc(name))
  } else {
    String::new()
  };
  format!(
    r#"<tr data-image="{name}" data-pending="1"><td class="image-select-cell">{checkbox}</td><td><code>{tag}</code></td><td class="image-status-cell"><span class="chamfer session-status checking"><span>checking…</span></span></td><td class="dim image-created-cell">—</td><td class="dim image-size-cell">—</td><td class="image-action-cell"></td></tr>
"#,
    name = esc(name),
    tag = esc(tag),
    checkbox = checkbox
  )
}

/// The Run tab: open a git repo (POST `/api/v1/repos/open`, which reports whether it is
/// runnable and why not), pick a harness definition, fill the rendered param form, and start a
/// job (POST `/api/v1/jobs/start`, deep-linking to the spawned session). Start is disabled until
/// the repo is runnable. Default landing tab on the index page.
fn start_panel() -> &'static str {
  r##"<div class="card card--accent-left-green">
<p class="section-label">Run</p>
<p class="dim">Open a git repository — an absolute path, or the bare name of a project under
<code>~/.scsh/projects/</code> — to configure and start a harness-definition job in it; the
daemon runs it just like <code>scsh run</code>. The repo must be committed, clean, and have a
gitignored scratch dir (<code>tmp/</code> or <code>.harness/tmp</code>). One job per repository at a time.</p>
<p class="dim">Or <strong>create a new project</strong>: a fresh git repository under
<code>~/.scsh/projects/&lt;name&gt;</code>, born runnable (its first commit gitignores
<code>/tmp</code>) — tests and demos start right here, no terminal needed.</p>
<div class="start-controls">
<div class="chamfer input-wrap">
<input class="input" type="text" id="repo-path" placeholder="/path/to/a/git/repo, or a project name (type, paste, or Pick…)">
</div>
<div class="start-actions">
<button type="button" class="chamfer btn btn--purple btn--sm" id="repo-pick"><span>Pick…</span></button>
<button type="button" class="chamfer btn btn--cyan btn--sm" id="repo-open"><span>Open</span></button>
</div>
<span id="repo-note" class="dim"></span>
</div>
<div class="start-controls">
<div class="chamfer input-wrap">
<input class="input" type="text" id="project-name" placeholder="project name (letters, digits, - or _; no dots/slashes)" autocomplete="off" spellcheck="false">
</div>
<div class="start-actions">
<button type="button" class="chamfer btn btn--green btn--sm" id="project-create"><span>New project</span></button>
</div>
</div>
<div id="repo-blockers" class="blockers" hidden></div>
<div id="defs-panel" hidden>
<p class="section-label">Definitions</p>
<p class="dim">Harness definitions in <code id="open-repo-path"></code> — pick one to configure and start.</p>
<div id="defs-list"></div>
<div id="def-form"></div>
</div>
</div>
"##
}

/// The "Projects" tab: every job the daemon knows about, grouped by the repository it runs
/// in (plus repositories opened from the UI that have no jobs yet). Rendered server-side so
/// the tab is populated on first paint; `renderRepoJobs` in the client JS re-renders the
/// same markup from live tick snapshots — keep the two byte-identical.
fn dirs_panel(store: &Store, now: u64, filter: Option<&IndexFilter>) -> String {
  let banner = match filter {
    Some(f) => format!(
      "<p class=\"filter-banner\" data-repo-filter=\"{path}\">Showing <strong>{label}</strong> · \
<a class=\"filter-clear\" href=\"/projects\">Show all</a></p>\n",
      path = esc(&f.repo_path()),
      label = esc(&f.label()),
    ),
    None => String::new(),
  };
  format!(
    r##"<div class="card card--accent-left-magenta">
<p class="section-label">Projects</p>
{banner}<p class="dim">Current jobs, grouped by where they run: a project under <code>~/.scsh/projects/</code> shows its
name; anything else shows its repository path. Click a name to filter.</p>
<div class="table-scroll"><table>
<thead><tr><th>Project / repository</th><th>Jobs</th></tr></thead>
<tbody id="repos-body">{rows}</tbody>
</table></div>
</div>
{internal}
"##,
    banner = banner,
    rows = repo_jobs_rows(store, now, filter),
    internal = internal_panel(store, now, filter),
  )
}

/// Projects → Internal: synthetic-repo sessions (`(image builds)`, `(internal)`), grouped by
/// profile. Hidden when filtering to a real project/repo or when none exist. Mirrored by
/// `renderInternalJobs` in the client JS.
fn internal_panel(store: &Store, now: u64, filter: Option<&IndexFilter>) -> String {
  if filter.is_some() {
    return String::new();
  }
  let mut jobs: Vec<&Session> = store.sessions.values().filter(|s| is_internal_repo(&s.repo)).collect();
  if jobs.is_empty() {
    return String::new();
  }
  let activity = |s: &Session| s.ended_at.unwrap_or(s.started_at);
  let mut groups: std::collections::BTreeMap<&str, Vec<&Session>> = std::collections::BTreeMap::new();
  for s in jobs.drain(..) {
    groups.entry(s.profile.as_deref().unwrap_or("default")).or_default().push(s);
  }
  let mut ordered: Vec<(&str, Vec<&Session>)> = groups.into_iter().collect();
  for (_, g) in &mut ordered {
    g.sort_by_key(|s| {
      (std::cmp::Reverse(s.lifecycle_status(now) == SessionLifecycle::Running), std::cmp::Reverse(activity(s)))
    });
  }
  ordered.sort_by_key(|(_, g)| {
    (
      std::cmp::Reverse(g.iter().any(|s| s.lifecycle_status(now) == SessionLifecycle::Running)),
      std::cmp::Reverse(g.iter().map(|s| activity(s)).max().unwrap_or(0)),
    )
  });
  let body = ordered
    .iter()
    .map(|(task, g)| {
      let links = g
        .iter()
        .map(|s| {
          let lc = s.lifecycle_status(now);
          format!(
            "<div class=\"repo-job\"><span class=\"chamfer session-status {cls}\"><span>{label}</span></span> <a class=\"job-id\" href=\"/job/{id}\">{id}</a> <span class=\"dim\">{age}</span></div>",
            id = esc(&s.id),
            cls = lc.css_class(),
            label = lc.label(),
            age = format_short_age(now.saturating_sub(activity(s))),
          )
        })
        .collect::<Vec<_>>()
        .join("");
      format!(
        "<div class=\"repo-jobgroup\"><span class=\"repo-jobgroup-name\">{task}</span>{links}</div>",
        task = esc(task),
        links = links,
      )
    })
    .collect::<Vec<_>>()
    .join("");
  format!(
    r##"<div class="card card--accent-left-purple" id="internal-jobs-card">
<p class="section-label">Internal</p>
<p class="dim">System jobs — image builds and annotate catch-up — not tied to a project or repository.</p>
<div id="internal-body">{body}</div>
</div>
"##,
    body = body,
  )
}

fn is_internal_repo(repo: &str) -> bool {
  repo == crate::daemon::server::IMAGE_BUILDS_REPO || repo == crate::daemon::server::INTERNAL_REPO
}

/// Rows of the Projects table. Within a repository the jobs are grouped by the task they
/// ran (the workflow/profile name), running groups above finished ones, newest first, each
/// job with a compact age stamp. Mirrored by `renderRepoJobs` in the client JS
/// (`client_js.rs`) — keep the markup identical.
fn repo_jobs_rows(store: &Store, now: u64, filter: Option<&IndexFilter>) -> String {
  let filter_repo = filter.map(|f| f.repo_path());
  let mut by_repo: std::collections::BTreeMap<&str, Vec<&Session>> = std::collections::BTreeMap::new();
  for path in store.open_repos.keys() {
    if filter_repo.as_ref().is_some_and(|want| path != want) {
      continue;
    }
    by_repo.entry(path).or_default();
  }
  for s in store.sessions.values() {
    if s.repo.is_empty() || is_internal_repo(&s.repo) {
      continue;
    }
    if filter_repo.as_ref().is_some_and(|want| &s.repo != want) {
      continue;
    }
    by_repo.entry(&s.repo).or_default().push(s);
  }
  if by_repo.is_empty() {
    return if filter.is_some() {
      "<tr><td colspan=\"2\" class=\"dim\">No jobs for this project or repository.</td></tr>".into()
    } else {
      "<tr><td colspan=\"2\" class=\"dim\">No jobs yet — open or create a project under Run.</td></tr>".to_string()
    };
  }
  // A job's "activity" moment: when it finished, or when it started if still going.
  let activity = |s: &Session| s.ended_at.unwrap_or(s.started_at);
  let projects_root = format!("{}/", crate::daemon::paths::projects_dir().display());
  let mut rows = String::new();
  for (repo, jobs) in by_repo {
    let cells = if jobs.is_empty() {
      "<span class=\"dim\">no jobs yet</span>".to_string()
    } else {
      let mut groups: std::collections::BTreeMap<&str, Vec<&Session>> = std::collections::BTreeMap::new();
      for s in jobs {
        groups.entry(s.profile.as_deref().unwrap_or("default")).or_default().push(s);
      }
      let mut ordered: Vec<(&str, Vec<&Session>)> = groups.into_iter().collect();
      for (_, g) in &mut ordered {
        g.sort_by_key(|s| {
          (std::cmp::Reverse(s.lifecycle_status(now) == SessionLifecycle::Running), std::cmp::Reverse(activity(s)))
        });
      }
      // Groups with something running come first, then by most recent activity.
      ordered.sort_by_key(|(_, g)| {
        (
          std::cmp::Reverse(g.iter().any(|s| s.lifecycle_status(now) == SessionLifecycle::Running)),
          std::cmp::Reverse(g.iter().map(|s| activity(s)).max().unwrap_or(0)),
        )
      });
      ordered
        .iter()
        .map(|(task, g)| {
          let links = g
            .iter()
            .map(|s| {
              let lc = s.lifecycle_status(now);
              format!(
                "<div class=\"repo-job\"><span class=\"chamfer session-status {cls}\"><span>{label}</span></span> <a class=\"job-id\" href=\"/job/{id}\">{id}</a> <span class=\"dim\">{age}</span></div>",
                id = esc(&s.id),
                cls = lc.css_class(),
                label = lc.label(),
                age = format_short_age(now.saturating_sub(activity(s))),
              )
            })
            .collect::<Vec<_>>()
            .join("");
          format!(
            "<div class=\"repo-jobgroup\"><span class=\"repo-jobgroup-name\">{task}</span>{links}</div>",
            task = esc(task),
            links = links,
          )
        })
        .collect::<Vec<_>>()
        .join("")
    };
    let href = repo_filter_href(repo, &projects_root);
    rows.push_str(&format!(
      "<tr data-repo=\"{repo}\"><td class=\"repo-path\" title=\"{repo}\"><a class=\"repo-filter-link\" href=\"{href}\">{label}</a></td><td>{cells}</td></tr>",
      repo = esc(repo),
      href = esc(&href),
      label = esc(&repo_display_label(repo, &projects_root)),
      cells = cells,
    ));
  }
  rows
}

/// `/project/<name>` for scsh projects, `/repo/<abs-path>` for everything else.
fn repo_filter_href(repo: &str, projects_root: &str) -> String {
  match repo.strip_prefix(projects_root) {
    Some(name) if !name.is_empty() && !name.contains('/') => format!("/project/{name}"),
    _ => format!("/repo{}", encode_repo_url_path(repo)),
  }
}

/// A repo under `~/.scsh/projects/` displays as `project · <name>`; anything else shows its
/// full path. Mirrors `repoLabel` inside `renderRepoJobs` in the client JS.
fn repo_display_label(repo: &str, projects_root: &str) -> String {
  match repo.strip_prefix(projects_root) {
    Some(name) => format!("project · {name}"),
    None => repo.to_string(),
  }
}

fn index_session_row(session: &Session, now: u64) -> String {
  let lifecycle = session.lifecycle_status(now);
  let id = esc(&session.id);
  let profile = esc(session.profile.as_deref().unwrap_or("default"));
  let n_procs = session.procs.len();
  let status = format!(
    r#"<span class="chamfer session-status {}"><span>{}</span></span>"#,
    lifecycle.css_class(),
    esc(lifecycle.label())
  );
  let started_rel = format_relative_age(now.saturating_sub(session.started_at));
  let started = format!(
    "<span class=\"session-started\" data-started=\"{}\">\
<span class=\"session-started-abs\">…</span><br>\
<span class=\"dim session-started-rel\">{}</span></span>",
    session.started_at,
    esc(&started_rel)
  );
  let duration = index_duration_label(session, now, lifecycle);
  format!(
    "<tr data-session-id=\"{id}\"><td><a class=\"job-id\" href=\"/job/{id}\">{id}</a></td>\
<td class=\"session-status-cell\">{status}</td>\
<td class=\"session-started-cell\">{started}</td>\
<td class=\"session-duration-cell\">{duration}</td>\
<td>{profile}</td><td class=\"session-procs-cell\">{chips}<span class=\"chip-count\" data-tip=\"{n_procs} run{plural} in this job\">{n_procs}</span></td><td class=\"dim repo-path\">{repo}</td></tr>\n",
    id = id,
    status = status,
    started = started,
    duration = esc(&duration),
    profile = profile,
    chips = harness_chips_html(session),
    n_procs = n_procs,
    plural = if n_procs == 1 { "" } else { "s" },
    repo = esc(&session.repo),
  )
}

/// Colored single-letter harness chips, one per skill proc — C in Anthropic orange is claude,
/// C in green is codex, C in violet is cursor — so a row shows at a glance what it runs and
/// what is still running (finished chips dim out). The tooltip is two lines — `harness ·
/// skill` then the status; a running chip instead carries `data-tip-running` (its start
/// time), from which the tip module composes a live-ticking "running for …" line without
/// churning the markup every second. Mirrored byte-for-byte by `harnessChipsHtml` in the
/// client JS (`client_js.rs`).
fn harness_chips_html(session: &Session) -> String {
  use crate::daemon::model::{ProcKind, ProcStatus};
  let mut chips = String::new();
  for p in &session.procs {
    if p.kind != ProcKind::Skill {
      continue;
    }
    let Some(h) = p.harness.as_deref().filter(|h| !h.is_empty()) else {
      continue;
    };
    let letter = h.chars().next().map(|c| c.to_ascii_uppercase()).unwrap_or('?');
    let done = matches!(p.status, ProcStatus::Ok | ProcStatus::Fail | ProcStatus::Skipped);
    let skill = p.skill_name.as_deref().unwrap_or(&p.label);
    let base = format!("{h} · {skill}");
    let (tip, running_attr) = match p.status {
      ProcStatus::Running => match p.started_at {
        Some(t) => (base, format!(" data-tip-running=\"{t}\"")),
        None => (format!("{base}\nrunning"), String::new()),
      },
      ProcStatus::Waiting => (format!("{base}\nwaiting"), String::new()),
      ProcStatus::Ok => (format!("{base}\ndone"), String::new()),
      ProcStatus::Fail => (format!("{base}\nfailed"), String::new()),
      ProcStatus::Skipped => (format!("{base}\nskipped"), String::new()),
    };
    chips.push_str(&format!(
      "<span class=\"hchip hchip--{h}{done}\" data-tip=\"{tip}\"{running_attr}>{letter}</span>",
      h = esc(h),
      done = if done { " hchip--done" } else { "" },
      tip = esc(&tip),
    ));
  }
  chips
}

fn index_duration_label(session: &Session, now: u64, lifecycle: SessionLifecycle) -> String {
  match session.duration_secs(now) {
    Some(secs) if lifecycle == SessionLifecycle::Running => format!("{} so far", format_duration_secs(secs)),
    Some(secs) => format_duration_secs(secs),
    None => "".to_string(),
  }
}