blyte 0.94.0

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
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
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
//! `blyte dashboard` collects running and recent jobs from blyte's own files and serves them
//! locally (2026-09-30).

use std::io::{BufRead, BufReader, Read, Write};
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use blyte_cli::dashboard::{collect, collect_jobs, route, Sources};

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

fn write(p: &Path, text: &str) {
    std::fs::create_dir_all(p.parent().unwrap()).unwrap();
    std::fs::write(p, text).unwrap();
}

fn job<'a>(v: &'a serde_json::Value, key: &str) -> &'a serde_json::Value {
    v["jobs"]
        .as_array()
        .unwrap()
        .iter()
        .find(|j| j["key"] == key)
        .unwrap_or_else(|| panic!("no job {key} in {v}"))
}

/// A root with dispatch-layout jobs (repo-a) and a runs dir (repo-b).
fn fixture(dir: &Path) -> Sources {
    let me = std::process::id();
    let t = now();
    let wt = dir.join("repos/repo-a/.wt");
    write(&dir.join("tasks/t1.md"), "# Add slugify\n\nMore detail.\n");
    write(
        &wt.join("T1.start.json"),
        &format!(
            r#"{{"id":"T1","pid":{me},"started_at":{},"task_file":"{}","gate":"true","tags":["planner: claude"]}}"#,
            t - 120,
            dir.join("tasks/t1.md").display()
        ),
    );
    write(
        &wt.join("T1.log"),
        "[Turn 3] \n● bash cargo test -> exit 0\n⏳ Gate running (attempt 1)\n",
    );

    write(
        &wt.join("T2.start.json"),
        &format!(
            r#"{{"id":"T2","pid":999999,"started_at":{},"task_file":"/nonexistent","gate":"true"}}"#,
            t - 900
        ),
    );
    write(
        &wt.join("T2.log"),
        "[Turn 10] \n✓ Gate passed (attempt 1)\n",
    );
    write(
        &wt.join("T2.report.json"),
        r#"{"outcome":"verified","cost_usd":0.5,"turns":10,"gate_attempts":1,"model_id":"gemini/gemini-3.8-flash","gate_failures":[],"duration_seconds":301.4,"final_message":"Done: added the flag.\nMore."}"#,
    );
    write(
        &wt.join("T2.json"),
        r#"{"commits":[{"sha":"abc12345","subject":"feat: add the flag"}],"changed_files":["a.rs","b.rs"],"worktree":"/w/T2"}"#,
    );

    write(
        &wt.join("T3.start.json"),
        &format!(
            r#"{{"id":"T3","pid":999999,"started_at":{},"task_file":"/nonexistent","gate":"true"}}"#,
            t - 600
        ),
    );
    write(
        &wt.join("T3.log"),
        "[Turn 2] \n● read src/lib.rs -> 40 lines\n",
    );

    let runs = dir.join("runs/repo-b");
    write(
        &runs.join("R1.log"),
        "[Turn 7] \n● edit src/x.py -> edited\n",
    );
    write(&runs.join("R1.pid"), &me.to_string());
    write(&runs.join("R1.prompt"), "You are implementing one task.\n\nPlan: /p/docs/plans/2026-09-30-thing.md\nTask: \"Task 1: Foo the bar\" ONLY.\n");
    write(&runs.join("R1.tags.json"), r#"["A/B: R1"]"#);
    write(
        &runs.join("R1.verdict.json"),
        r#"{"verdict":"accepted","notes":"fine"}"#,
    );

    write(&runs.join("OLD.log"), "[Turn 1] \n");
    write(
        &runs.join("OLD.report.json"),
        r#"{"outcome":"verified","cost_usd":9.0}"#,
    );
    let old = SystemTime::now() - Duration::from_secs(3 * 86400);
    for f in ["OLD.log", "OLD.report.json"] {
        std::fs::File::options()
            .write(true)
            .open(runs.join(f))
            .unwrap()
            .set_modified(old)
            .unwrap();
    }

    Sources {
        roots: vec![dir.join("repos")],
        runs_dirs: vec![dir.join("runs")],
        hours: 24,
    }
}

#[test]
fn running_jobs_show_their_progress_and_tags() {
    let d = tempfile::tempdir().unwrap();
    let v = collect(&fixture(d.path()), now());
    let t1 = job(&v, "repo-a/T1");
    assert_eq!(t1["status"], "running");
    assert_eq!(t1["gate_running"], true);
    assert_eq!(t1["turns"], 3);
    assert_eq!(t1["last_tool"], "bash cargo test -> exit 0");
    assert_eq!(t1["title"], "Add slugify");
    assert!(t1["elapsed_s"].as_u64().unwrap() >= 120);
    let tags: Vec<&str> = t1["tags"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t.as_str().unwrap())
        .collect();
    assert!(
        tags.contains(&"repo: repo-a") && tags.contains(&"planner: claude"),
        "{tags:?}"
    );

    let r1 = job(&v, "repo-b/R1");
    assert_eq!(r1["status"], "running");
    assert_eq!(r1["gate_running"], false);
    assert_eq!(r1["title"], "Task 1: Foo the bar");
    assert_eq!(r1["plan"], "/p/docs/plans/2026-09-30-thing.md");
    assert_eq!(r1["verdict"], "accepted");
    let tags: Vec<&str> = r1["tags"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t.as_str().unwrap())
        .collect();
    assert!(
        tags.contains(&"A/B: R1") && tags.contains(&"plan: 2026-09-30-thing"),
        "{tags:?}"
    );
}

#[test]
fn finished_and_stopped_jobs_are_reported_and_old_ones_dropped() {
    let d = tempfile::tempdir().unwrap();
    let v = collect(&fixture(d.path()), now());
    let t2 = job(&v, "repo-a/T2");
    assert_eq!(t2["status"], "verified");
    assert_eq!(t2["cost_usd"], 0.5);
    assert_eq!(t2["model"], "gemini/gemini-3.8-flash");
    assert_eq!(t2["elapsed_s"], 301);
    assert_eq!(t2["now"], "Done: added the flag.");
    assert_eq!(t2["commits"].as_array().unwrap().len(), 1);
    assert_eq!(t2["changed_files"].as_array().unwrap().len(), 2);
    assert_eq!(job(&v, "repo-a/T3")["status"], "stopped");
    assert!(v["jobs"]
        .as_array()
        .unwrap()
        .iter()
        .all(|j| j["key"] != "repo-b/OLD"));

    let order: Vec<&str> = v["jobs"]
        .as_array()
        .unwrap()
        .iter()
        .map(|j| j["key"].as_str().unwrap())
        .collect();
    assert_eq!(
        &order[..2]
            .iter()
            .filter(|k| ["repo-a/T1", "repo-b/R1"].contains(k))
            .count(),
        &2,
        "running first: {order:?}"
    );

    let s = &v["summary"];
    assert_eq!(s["running"], 2);
    assert_eq!(s["repos"], 2);
    assert_eq!(s["passed_24h"], 1);
    assert_eq!(s["cost_24h_usd"], 0.5);
}

#[test]
fn routes_serve_the_page_the_jobs_and_nothing_else() {
    let d = tempfile::tempdir().unwrap();
    let src = fixture(d.path());
    let (code, ctype, body) = route("/", &src);
    assert_eq!(code, 200);
    assert!(ctype.starts_with("text/html"));
    assert!(String::from_utf8_lossy(&body).contains("<html"));
    let (code, ctype, body) = route("/api/jobs", &src);
    assert_eq!((code, ctype), (200, "application/json"));
    let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert!(v["jobs"].is_array() && v["summary"].is_object());
    assert_eq!(route("/etc/passwd", &src).0, 404);
}

#[test]
fn the_server_listens_on_localhost_and_answers() {
    let d = tempfile::tempdir().unwrap();
    fixture(d.path());
    let mut child = Command::new(env!("CARGO_BIN_EXE_blyte"))
        .args(["dashboard", "--port", "0", "--root"])
        .arg(d.path().join("repos"))
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();
    let mut line = String::new();
    BufReader::new(child.stdout.take().unwrap())
        .read_line(&mut line)
        .unwrap();
    let url = line
        .trim()
        .strip_prefix("blyte dashboard: http://")
        .unwrap_or_else(|| panic!("{line}"));
    assert!(url.starts_with("127.0.0.1:"), "{url}");
    let mut s = std::net::TcpStream::connect(url).unwrap();
    s.write_all(b"GET /api/jobs HTTP/1.1\r\nHost: x\r\n\r\n")
        .unwrap();
    let mut resp = String::new();
    s.read_to_string(&mut resp).unwrap();
    child.kill().unwrap();
    let _ = child.wait();
    assert!(resp.starts_with("HTTP/1.1 200"), "{resp}");
    assert!(resp.contains("\"jobs\""), "{resp}");
}

#[test]
fn dispatch_tags_are_accepted() {
    let out = Command::new(env!("CARGO_BIN_EXE_blyte"))
        .args(["dispatch", "--help"])
        .output()
        .unwrap();
    assert!(String::from_utf8_lossy(&out.stdout).contains("--tag"));
}

#[test]
fn corrupt_report_records_problem_and_keeps_other_runs() {
    let d = tempfile::tempdir().unwrap();
    let wt = d.path().join("repos").join("alpha").join(".wt");
    std::fs::create_dir_all(&wt).unwrap();
    let t = now();

    std::fs::write(
        wt.join("X.start.json"),
        serde_json::json!({
            "id": "X",
            "started_at": t - 60,
            "pid": 999_999
        })
        .to_string(),
    )
    .unwrap();
    let bad_report = wt.join("X.report.json");
    std::fs::write(&bad_report, "not json").unwrap();

    let bare_bad_report = wt.join("BARE.report.json");
    std::fs::write(&bare_bad_report, "not json").unwrap();

    std::fs::write(
        wt.join("Y.start.json"),
        serde_json::json!({
            "id": "Y",
            "started_at": t - 50,
            "pid": 999_998
        })
        .to_string(),
    )
    .unwrap();
    std::fs::write(
        wt.join("Y.report.json"),
        serde_json::json!({
            "outcome": "accepted",
            "turns": 2,
            "gate_attempts": 0
        })
        .to_string(),
    )
    .unwrap();

    let src = Sources {
        roots: vec![d.path().join("repos")],
        runs_dirs: vec![],
        hours: 24,
    };
    let jobs = collect_jobs(&src, t);
    let by_id: std::collections::HashMap<_, _> =
        jobs.into_iter().map(|j| (j.id.clone(), j)).collect();

    assert!(
        by_id.contains_key("Y"),
        "other run Y should still be listed"
    );
    assert!(by_id["Y"].problems.is_empty(), "{:?}", by_id["Y"].problems);

    let x = by_id
        .get("X")
        .expect("corrupt run X should still be listed");
    assert_ne!(x.status, "stopped");
    assert!(
        x.problems
            .iter()
            .any(|p| p.contains(&bad_report.display().to_string())),
        "expected problem naming {}, got {:?}",
        bad_report.display(),
        x.problems
    );

    let bare = by_id
        .get("BARE")
        .expect("bare corrupt report BARE should still be listed");
    assert!(
        bare.problems
            .iter()
            .any(|p| p.contains(&bare_bad_report.display().to_string())),
        "expected problem naming {}, got {:?}",
        bare_bad_report.display(),
        bare.problems
    );
}

#[test]
fn report_without_outcome_records_problem_and_is_not_unknown() {
    let d = tempfile::tempdir().unwrap();
    let wt = d.path().join("repos").join("alpha").join(".wt");
    std::fs::create_dir_all(&wt).unwrap();
    let t = now();

    std::fs::write(
        wt.join("Z.start.json"),
        serde_json::json!({
            "id": "Z",
            "started_at": t - 60,
            "pid": 999_999
        })
        .to_string(),
    )
    .unwrap();
    let report = wt.join("Z.report.json");
    std::fs::write(
        &report,
        serde_json::json!({
            "turns": 4,
            "gate_failures": ["ok", 42]
        })
        .to_string(),
    )
    .unwrap();

    let src = Sources {
        roots: vec![d.path().join("repos")],
        runs_dirs: vec![],
        hours: 24,
    };
    let jobs = collect_jobs(&src, t);
    let z = jobs.iter().find(|j| j.id == "Z").expect("job Z missing");
    assert_ne!(z.status, "unknown");
    assert!(
        z.problems
            .iter()
            .any(|p| p.contains(&report.display().to_string()) && p.contains("outcome")),
        "expected problem naming {} and 'outcome', got {:?}",
        report.display(),
        z.problems
    );
    assert!(
        z.problems
            .iter()
            .any(|p| p.contains(&report.display().to_string()) && p.contains("gate_failures")),
        "expected problem for non-string gate_failures entry, got {:?}",
        z.problems
    );
}

#[test]
fn corrupt_run_artifacts_record_problems() {
    let d = tempfile::tempdir().unwrap();
    let wt = d.path().join("repos").join("alpha").join(".wt");
    std::fs::create_dir_all(&wt).unwrap();

    let bad_start = wt.join("S1.start.json");
    std::fs::write(&bad_start, "{not valid json").unwrap();
    let bad_verdict = wt.join("S1.verdict.json");
    std::fs::write(&bad_verdict, "corrupt verdict").unwrap();
    let bad_log = wt.join("S1.log");
    std::fs::write(&bad_log, [0xff_u8, 0xfe, 0xfd]).unwrap();
    std::fs::write(
        wt.join("S1.report.json"),
        serde_json::json!({"outcome": "accepted"}).to_string(),
    )
    .unwrap();

    let src = Sources {
        roots: vec![d.path().join("repos")],
        runs_dirs: vec![],
        hours: 24,
    };
    let jobs = collect_jobs(&src, now());
    let by_id: std::collections::HashMap<_, _> =
        jobs.into_iter().map(|j| (j.id.clone(), j)).collect();

    let s1 = by_id.get("S1").expect("job S1 missing");
    assert!(
        s1.problems
            .iter()
            .any(|p| p.contains(&bad_start.display().to_string())),
        "missing start.json problem: {:?}",
        s1.problems
    );
    assert!(
        s1.problems
            .iter()
            .any(|p| p.contains(&bad_verdict.display().to_string())),
        "missing verdict.json problem: {:?}",
        s1.problems
    );
    assert!(
        s1.problems
            .iter()
            .any(|p| p.contains(&bad_log.display().to_string())),
        "missing log problem: {:?}",
        s1.problems
    );
}

#[test]
fn dashboard_html_renders_job_problems() {
    let html = include_str!("../assets/dashboard/index.html");
    assert!(html.contains("job.problems"));
    assert!(html.contains("row-problems"));
}

#[test]
fn report_missing_or_invalid_turns_and_gate_attempts_record_problems() {
    let d = tempfile::tempdir().unwrap();
    let wt = d.path().join("repos").join("alpha").join(".wt");
    std::fs::create_dir_all(&wt).unwrap();
    let t = now();

    std::fs::write(
        wt.join("M1.start.json"),
        serde_json::json!({
            "id": "M1",
            "started_at": t - 60,
            "pid": 999_999
        })
        .to_string(),
    )
    .unwrap();
    std::fs::write(wt.join("M1.log"), "[Turn 7] \n✓ Gate passed (attempt 1)\n").unwrap();
    let missing_report = wt.join("M1.report.json");
    std::fs::write(
        &missing_report,
        serde_json::json!({
            "outcome": "verified"
        })
        .to_string(),
    )
    .unwrap();

    std::fs::write(
        wt.join("M2.start.json"),
        serde_json::json!({
            "id": "M2",
            "started_at": t - 50,
            "pid": 999_998
        })
        .to_string(),
    )
    .unwrap();
    std::fs::write(wt.join("M2.log"), "[Turn 5] \n✓ Gate passed (attempt 1)\n").unwrap();
    let invalid_report = wt.join("M2.report.json");
    std::fs::write(
        &invalid_report,
        serde_json::json!({
            "outcome": "verified",
            "turns": "five",
            "gate_attempts": -1
        })
        .to_string(),
    )
    .unwrap();

    let src = Sources {
        roots: vec![d.path().join("repos")],
        runs_dirs: vec![],
        hours: 24,
    };
    let jobs = collect_jobs(&src, t);
    let by_id: std::collections::HashMap<_, _> =
        jobs.into_iter().map(|j| (j.id.clone(), j)).collect();

    let m1 = by_id.get("M1").expect("job M1 missing");
    assert_eq!(m1.turns, 0);
    assert_eq!(m1.gate_attempts, 0);
    assert!(
        m1.problems
            .iter()
            .any(|p| p.contains(&missing_report.display().to_string()) && p.contains("turns")),
        "expected missing turns problem for {}, got {:?}",
        missing_report.display(),
        m1.problems
    );
    assert!(
        m1.problems
            .iter()
            .any(|p| p.contains(&missing_report.display().to_string())
                && p.contains("gate_attempts")),
        "expected missing gate_attempts problem for {}, got {:?}",
        missing_report.display(),
        m1.problems
    );

    let m2 = by_id.get("M2").expect("job M2 missing");
    assert_eq!(m2.turns, 0);
    assert_eq!(m2.gate_attempts, 0);
    assert!(
        m2.problems
            .iter()
            .any(|p| p.contains(&invalid_report.display().to_string()) && p.contains("turns")),
        "expected invalid turns problem for {}, got {:?}",
        invalid_report.display(),
        m2.problems
    );
    assert!(
        m2.problems
            .iter()
            .any(|p| p.contains(&invalid_report.display().to_string())
                && p.contains("gate_attempts")),
        "expected invalid gate_attempts problem for {}, got {:?}",
        invalid_report.display(),
        m2.problems
    );
}