blyte 0.34.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
551
552
553
554
use std::path::Path;
use std::process::Command;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Concern {
    pub file: String,
    pub line: Option<u64>,
    pub severity: String,
    pub text: String,
}

pub const SYSTEM_PROMPT: &str = "\
You are a senior software engineer performing a first review of a code diff against a task specification.
Review the diff against the task. Raise only concrete problems a reviewer must look at:
- the task's requirements not met
- a behaviour or edge case the task names but the code or tests miss
- changes outside what the task asks for
- tests that could pass with a wrong implementation
- risky commands or deleted tests

Do not make style comments.
Raise at most 10 concerns.
Answer with only a JSON array of objects with the following schema:
[{\"file\": \"<path>\", \"line\": <line number or null>, \"severity\": \"high\" | \"medium\" | \"low\", \"text\": \"<description>\"}]
Return [] when there is nothing to raise. Do not include any text or markdown outside the JSON array.";

/// System prompt + user message. The user message holds, in this order: the task text
/// (under "## Task"), `git diff --stat`, and the full diff (under "## Diff"), the diff cut to
/// 200_000 characters with a final line "[diff truncated]" if longer.
pub fn build_request(task: &str, stat: &str, diff: &str) -> blyte_provider::Request {
    let diff_text = if diff.chars().count() > 200_000 {
        let cutoff = diff
            .char_indices()
            .nth(200_000)
            .map(|(i, _)| i)
            .unwrap_or(diff.len());
        let mut s = diff[..cutoff].to_string();
        if !s.ends_with('\n') {
            s.push('\n');
        }
        s.push_str("[diff truncated]\n");
        s
    } else {
        diff.to_string()
    };

    let mut user_msg = String::new();
    user_msg.push_str("## Task\n");
    user_msg.push_str(task);
    if !user_msg.ends_with('\n') {
        user_msg.push('\n');
    }
    user_msg.push('\n');

    let stat_trimmed = stat.trim();
    if !stat_trimmed.is_empty() {
        user_msg.push_str(stat_trimmed);
        user_msg.push_str("\n\n");
    }

    user_msg.push_str("## Diff\n");
    user_msg.push_str(&diff_text);
    if !user_msg.ends_with('\n') {
        user_msg.push('\n');
    }

    blyte_provider::Request {
        system: SYSTEM_PROMPT.to_string(),
        messages: vec![blyte_provider::Message::User(user_msg)],
        tools: Vec::new(),
        max_output_tokens: Some(32_000),
    }
}

/// Parse the model's text. It is asked for a JSON array of
/// {"file","line","severity","text"} with severity one of "high","medium","low",
/// and `[]` when there is nothing to raise. Accept the array bare or inside a ```json fence,
/// with surrounding prose. Unknown severities become "low"; entries without "text" are dropped.
/// Empty or whitespace-only text → Err("empty response").
/// Unparseable text → Err(String) with the first 200 characters of the text.
pub fn parse(text: &str) -> Result<Vec<Concern>, String> {
    if text.trim().is_empty() {
        return Err("empty response".to_string());
    }
    let candidate = extract_json_candidate(text);
    let value: serde_json::Value = match candidate {
        Some(s) => match serde_json::from_str(s) {
            Ok(v) => v,
            Err(_) => {
                if let (Some(start), Some(end)) = (text.find('['), text.rfind(']')) {
                    if end >= start {
                        match serde_json::from_str(&text[start..=end]) {
                            Ok(v) => v,
                            Err(_) => return Err(first_200(text)),
                        }
                    } else {
                        return Err(first_200(text));
                    }
                } else {
                    return Err(first_200(text));
                }
            }
        },
        None => return Err(first_200(text)),
    };

    let array = match value {
        serde_json::Value::Array(arr) => arr,
        serde_json::Value::Object(mut map) => {
            if let Some(serde_json::Value::Array(arr)) = map.remove("concerns") {
                arr
            } else {
                return Err(first_200(text));
            }
        }
        _ => return Err(first_200(text)),
    };

    let mut concerns = Vec::new();
    for item in array {
        let Some(obj) = item.as_object() else {
            continue;
        };
        let text_val = match obj.get("text").and_then(|t| t.as_str()) {
            Some(t) => t.to_string(),
            None => continue,
        };
        let file = obj
            .get("file")
            .and_then(|f| f.as_str())
            .unwrap_or("")
            .to_string();
        let line = obj.get("line").and_then(|l| {
            l.as_u64()
                .or_else(|| l.as_str().and_then(|s| s.parse::<u64>().ok()))
        });
        let severity = match obj.get("severity").and_then(|s| s.as_str()) {
            Some(s) if s.eq_ignore_ascii_case("high") => "high".to_string(),
            Some(s) if s.eq_ignore_ascii_case("medium") => "medium".to_string(),
            Some(s) if s.eq_ignore_ascii_case("low") => "low".to_string(),
            _ => "low".to_string(),
        };
        concerns.push(Concern {
            file,
            line,
            severity,
            text: text_val,
        });
    }

    Ok(concerns)
}

fn first_200(text: &str) -> String {
    text.chars().take(200).collect()
}

fn extract_json_candidate(text: &str) -> Option<&str> {
    if let Some(start) = text.find("```json") {
        let content_start = start + 7;
        if let Some(end) = text[content_start..].find("```") {
            return Some(text[content_start..content_start + end].trim());
        }
    }
    if let Some(start) = text.find("```") {
        let after_fence = &text[start + 3..];
        let content_start = if let Some(nl) = after_fence.find('\n') {
            nl + 1
        } else {
            0
        };
        if let Some(end) = after_fence[content_start..].find("```") {
            return Some(after_fence[content_start..content_start + end].trim());
        }
    }
    if let Some(start) = text.find('[') {
        if let Some(end) = text.rfind(']') {
            if end >= start {
                return Some(&text[start..=end]);
            }
        }
    }
    None
}

fn severity_rank(sev: &str) -> u8 {
    match sev.to_ascii_lowercase().as_str() {
        "high" => 0,
        "medium" => 1,
        "low" => 2,
        _ => 3,
    }
}

pub fn parse_error_message(err: &str, finish: Option<&str>) -> String {
    match finish {
        Some(reason) if !reason.is_empty() => format!("{err} (finish reason: {reason})"),
        _ => err.to_string(),
    }
}

pub fn sort_concerns(concerns: &mut [Concern]) {
    concerns.sort_by_key(|c| severity_rank(&c.severity));
}

pub fn format_concern(c: &Concern) -> String {
    if let Some(line) = c.line {
        format!("[{}] {}:{} {}", c.severity, c.file, line, c.text)
    } else {
        format!("[{}] {} {}", c.severity, c.file, c.text)
    }
}

pub fn run(root: &Path, id: &str, base: &str, wt: &Path) {
    let start_path = root.join(".wt").join(format!("{id}.start.json"));
    let task_text = match read_task_text(root, &start_path) {
        Some(t) => t,
        None => {
            println!("== model review");
            println!("skipped: task file not found");
            return;
        }
    };

    let stat_out = Command::new("git")
        .args(["diff", "--stat", &format!("{base}..HEAD")])
        .current_dir(wt)
        .output()
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .unwrap_or_default();

    let diff_out = Command::new("git")
        .args(["diff", &format!("{base}..HEAD")])
        .current_dir(wt)
        .output()
        .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
        .unwrap_or_default();

    if let Some(env_path) = crate::env_file::default_path() {
        let _ = crate::env_file::load(&env_path);
    }

    let repo_config = match crate::config::load_with_fallback(root) {
        Ok(Some((cfg, _))) => Some(cfg),
        Ok(None) => None,
        Err(e) => {
            println!("== model review");
            println!("error: {e}");
            return;
        }
    };
    let dispatch_cfg = repo_config.map(|c| c.dispatch).unwrap_or_default();
    let model = dispatch_cfg.model.as_deref();

    // One call over a whole diff at high effort can take minutes; allow 10 unless the user set a limit.
    if std::env::var_os("BLYTE_MODEL_TIMEOUT_SECS").is_none() {
        std::env::set_var("BLYTE_MODEL_TIMEOUT_SECS", "600");
    }
    let provider = match blyte_provider::resolve_provider(model) {
        Ok(p) => p,
        Err(blyte_provider::ProviderError::MissingKey(key)) => {
            println!("== model review");
            println!("skipped: no API key (set {key})");
            return;
        }
        Err(e) => {
            println!("== model review");
            println!("error: {e}");
            return;
        }
    };
    let model_id = provider.model_id();

    let req = build_request(&task_text, &stat_out, &diff_out);
    let resp = match provider.complete(&req) {
        Ok(r) => r,
        Err(e) => {
            println!("== model review");
            println!("error: {e}");
            save_review(root, id, &model_id, None, Vec::new(), Some(e.to_string()));
            return;
        }
    };

    let cost_usd = blyte_provider::pricing(&model_id).map(|p| p.cost(&resp.usage));

    let concerns = match parse(&resp.text) {
        Ok(mut c) => {
            sort_concerns(&mut c);
            c
        }
        Err(e) => {
            let err_msg = parse_error_message(&e, resp.finish_reason.as_deref());
            println!("== model review");
            println!("error: {err_msg}");
            save_review(root, id, &model_id, cost_usd, Vec::new(), Some(err_msg));
            return;
        }
    };

    let cost_str = match cost_usd {
        Some(c) => format!("${c:.4}"),
        None => "?".to_string(),
    };
    println!("== model review (trial: {model_id}, {cost_str})");
    if concerns.is_empty() {
        println!("nothing raised");
    } else {
        for c in &concerns {
            println!("{}", format_concern(c));
        }
    }

    save_review(root, id, &model_id, cost_usd, concerns, None);
}

fn read_task_text(root: &Path, start_path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(start_path).ok()?;
    let val: serde_json::Value = serde_json::from_str(&content).ok()?;
    let task_file_str = val.get("task_file")?.as_str()?;
    let task_path = Path::new(task_file_str);
    if task_path.is_absolute() {
        std::fs::read_to_string(task_path).ok()
    } else {
        std::fs::read_to_string(root.join(task_path))
            .or_else(|_| std::fs::read_to_string(task_path))
            .ok()
    }
}

fn save_review(
    root: &Path,
    id: &str,
    model: &str,
    cost_usd: Option<f64>,
    concerns: Vec<Concern>,
    error: Option<String>,
) {
    let wt_dir = root.join(".wt");
    let _ = std::fs::create_dir_all(&wt_dir);
    let review_path = wt_dir.join(format!("{id}.model-review.json"));
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let record = serde_json::json!({
        "model": model,
        "cost_usd": cost_usd,
        "concerns": concerns,
        "ts": ts,
        "error": error,
    });
    if let Ok(json_str) = serde_json::to_string_pretty(&record) {
        let _ = std::fs::write(&review_path, json_str + "\n");
    }
}

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

    #[test]
    fn test_parse_empty() {
        let res = parse("[]").expect("parse should succeed");
        assert!(res.is_empty());
    }

    #[test]
    fn test_parse_empty_or_whitespace() {
        assert_eq!(parse(""), Err("empty response".to_string()));
        assert_eq!(parse("  \n"), Err("empty response".to_string()));
    }

    #[test]
    fn test_parse_error_message() {
        assert_eq!(
            parse_error_message("empty response", Some("MAX_TOKENS")),
            "empty response (finish reason: MAX_TOKENS)"
        );
        assert_eq!(
            parse_error_message("empty response", None),
            "empty response"
        );
    }

    #[test]
    fn test_parse_fenced_with_prose() {
        let input = r#"
Here is the review of the diff against the task:
```json
[
  {
    "file": "crates/blyte-cli/src/foo.rs",
    "line": 42,
    "severity": "high",
    "text": "Task requires error handling here"
  },
  {
    "file": "crates/blyte-cli/src/bar.rs",
    "line": 100,
    "severity": "medium",
    "text": "Missing edge case check"
  }
]
```
Overall looks okay otherwise.
"#;
        let concerns = parse(input).expect("parse should succeed");
        assert_eq!(concerns.len(), 2);
        assert_eq!(concerns[0].file, "crates/blyte-cli/src/foo.rs");
        assert_eq!(concerns[0].line, Some(42));
        assert_eq!(concerns[0].severity, "high");
        assert_eq!(concerns[0].text, "Task requires error handling here");
        assert_eq!(concerns[1].file, "crates/blyte-cli/src/bar.rs");
        assert_eq!(concerns[1].line, Some(100));
        assert_eq!(concerns[1].severity, "medium");
        assert_eq!(concerns[1].text, "Missing edge case check");
    }

    #[test]
    fn test_parse_normalization_and_filtering() {
        let input = r#"[
            {
                "file": "crates/a.rs",
                "line": null,
                "severity": "critical",
                "text": "A severe issue"
            },
            {
                "file": "crates/b.rs",
                "line": 10,
                "severity": "high"
            }
        ]"#;
        let concerns = parse(input).expect("parse should succeed");
        assert_eq!(concerns.len(), 1);
        assert_eq!(concerns[0].file, "crates/a.rs");
        assert_eq!(concerns[0].line, None);
        assert_eq!(concerns[0].severity, "low");
        assert_eq!(concerns[0].text, "A severe issue");
    }

    #[test]
    fn test_parse_not_json() {
        let err = parse("not json").expect_err("should return Err");
        assert!(err.contains("not json"));
    }

    #[test]
    fn test_parse_long_unparseable_truncated_to_200() {
        let input = "not json ".repeat(30);
        assert!(input.chars().count() > 200);
        let err = parse(&input).expect_err("should return Err");
        assert_eq!(err.chars().count(), 200);
        assert!(input.starts_with(&err));
    }

    #[test]
    fn test_build_request_diff_truncation() {
        let task = "Implement feature X with full test coverage.";
        let stat = " src/main.rs | 100 ++\n 1 file changed";
        let diff = "a".repeat(300_000);

        let req = build_request(task, stat, &diff);
        assert_eq!(req.max_output_tokens, Some(32_000));
        assert!(req.tools.is_empty(), "tools should be empty");
        assert_eq!(req.messages.len(), 1);

        let blyte_provider::Message::User(ref msg) = req.messages[0] else {
            panic!("expected user message");
        };

        assert!(
            msg.contains("[diff truncated]"),
            "must contain [diff truncated]"
        );
        assert!(msg.len() < 210_000, "user message length was {}", msg.len());

        let task_pos = msg.find(task).expect("task text missing");
        let diff_pos = msg.find("## Diff").expect("diff header missing");
        assert!(task_pos < diff_pos, "task text must appear before diff");
    }

    #[test]
    fn test_build_request_max_output_tokens() {
        let req = build_request("task", "stat", "diff");
        assert_eq!(req.max_output_tokens, Some(32_000));
    }

    #[test]
    fn test_concerns_sort_and_format() {
        let mut concerns = vec![
            Concern {
                file: "low.rs".to_string(),
                line: Some(1),
                severity: "low".to_string(),
                text: "low issue".to_string(),
            },
            Concern {
                file: "high.rs".to_string(),
                line: None,
                severity: "high".to_string(),
                text: "high issue".to_string(),
            },
            Concern {
                file: "med.rs".to_string(),
                line: Some(2),
                severity: "medium".to_string(),
                text: "medium issue".to_string(),
            },
        ];

        sort_concerns(&mut concerns);

        assert_eq!(concerns[0].severity, "high");
        assert_eq!(concerns[1].severity, "medium");
        assert_eq!(concerns[2].severity, "low");

        assert_eq!(format_concern(&concerns[0]), "[high] high.rs high issue");
        assert_eq!(
            format_concern(&concerns[1]),
            "[medium] med.rs:2 medium issue"
        );
        assert_eq!(format_concern(&concerns[2]), "[low] low.rs:1 low issue");
    }

    #[test]
    fn test_save_review_record() {
        let dir = tempfile::tempdir().unwrap();
        let concerns = vec![Concern {
            file: "foo.rs".to_string(),
            line: Some(5),
            severity: "high".to_string(),
            text: "problem".to_string(),
        }];
        save_review(
            dir.path(),
            "T1",
            "gemini/gemini-3.8-flash",
            Some(0.0012),
            concerns,
            None,
        );
        let path = dir.path().join(".wt/T1.model-review.json");
        assert!(path.is_file());
        let content = std::fs::read_to_string(&path).unwrap();
        let val: serde_json::Value = serde_json::from_str(&content).unwrap();
        assert_eq!(val["model"], "gemini/gemini-3.8-flash");
        assert_eq!(val["cost_usd"], 0.0012);
        assert_eq!(val["concerns"].as_array().unwrap().len(), 1);
        assert!(val["error"].is_null());
        assert!(val["ts"].as_u64().unwrap() > 0);
    }
}