blyte 0.87.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
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
//! Flags added lines in a unified diff that match common fallback patterns.

use serde::Serialize;
use std::path::{Component, Path};

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Finding {
    pub file: String,
    pub line: usize,
    pub pattern: &'static str,
    pub text: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FallbackRecord {
    pub file: String,
    pub line: usize,
    pub pattern: &'static str,
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verdict: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

/// Added lines of a unified diff (`git diff` output) that match a fallback pattern.
/// A line that was moved or re-indented is a candidate too: the code around it may have
/// changed, and the fresh review judges it with the rest.
pub fn added_fallbacks(diff: &str) -> Vec<Finding> {
    let lines: Vec<&str> = diff.lines().collect();

    let mut findings = Vec::new();

    let mut current_file: Option<(&str, &str)> = None; // (path, ext)
    let mut current_line: usize = 0;
    let mut in_hunk = false;
    let mut ignore_hunk = false;
    let mut after_cfg_test = false;

    for (idx, &raw_line) in lines.iter().enumerate() {
        if let Some(rest) = raw_line.strip_prefix("+++ ") {
            in_hunk = false;
            ignore_hunk = false;
            after_cfg_test = false;
            current_line = 0;
            if rest == "/dev/null" {
                current_file = None;
            } else {
                let path = rest.strip_prefix("b/").unwrap_or(rest);
                if is_test_file(path) {
                    current_file = None;
                } else {
                    let ext = Path::new(path)
                        .extension()
                        .and_then(|s| s.to_str())
                        .unwrap_or("");
                    if is_supported_ext(ext) {
                        current_file = Some((path, ext));
                    } else {
                        current_file = None;
                    }
                }
            }
            continue;
        }

        if let Some(after_at) = raw_line.strip_prefix("@@ ") {
            if let Some((coords, _)) = after_at.split_once("@@") {
                if let Some(plus_part) = coords.split_whitespace().find_map(|t| t.strip_prefix('+'))
                {
                    let c_str = plus_part.split(',').next().unwrap_or(plus_part);
                    if let Ok(c) = c_str.parse::<usize>() {
                        current_line = c;
                        in_hunk = true;
                        let is_rust = current_file.map(|(_, ext)| ext == "rs").unwrap_or(false);
                        ignore_hunk = is_rust && raw_line.contains("mod tests");
                    }
                }
            }
            continue;
        }

        let Some((file_path, ext)) = current_file else {
            continue;
        };
        if !in_hunk {
            continue;
        }

        if let Some(content) = raw_line.strip_prefix(' ') {
            current_line += 1;
            if ext == "rs" && content.contains("#[cfg(test)]") {
                after_cfg_test = true;
            }
            continue;
        }

        if raw_line.starts_with('-') {
            continue;
        }

        if let Some(content) = raw_line.strip_prefix('+') {
            let line_no = current_line;
            current_line += 1;

            if ext == "rs" && content.contains("#[cfg(test)]") {
                after_cfg_test = true;
            }

            if ignore_hunk || after_cfg_test || is_comment(content, ext) {
                continue;
            }

            let patterns = match_line(content, ext, &lines, idx);
            if !patterns.is_empty() {
                let trimmed = content.trim();
                let text: String = trimmed.chars().take(200).collect();
                for pattern in patterns {
                    findings.push(Finding {
                        file: file_path.to_string(),
                        line: line_no,
                        pattern,
                        text: text.clone(),
                    });
                }
            }
        }
    }

    findings
}

/// One `== checks` flag for unjudged findings, or `None` when there are none.
pub fn flag(findings: &[Finding]) -> Option<String> {
    flags_for_review(findings, None).into_iter().next()
}

/// Flags for fallback candidates given an optional fresh model review result.
pub fn flags_for_review(
    candidates: &[Finding],
    model_review: Option<&crate::model_review::ModelReviewResult>,
) -> Vec<String> {
    if candidates.is_empty() {
        return Vec::new();
    }
    let Some(mr) = model_review else {
        return vec![format_not_judged_why(candidates.len(), "no review")];
    };
    if mr.is_failed() {
        let why = mr.failure_detail();
        return vec![format_not_judged_why(candidates.len(), &why)];
    }

    let mut confirmed = Vec::new();
    let mut not_judged = Vec::new();
    for c in candidates {
        match mr
            .fallbacks
            .iter()
            .find(|v| v.file == c.file && v.line == c.line)
        {
            Some(v) if v.verdict == "fallback" => {
                confirmed.push(format!("{}:{}", c.file, c.line));
            }
            Some(v) if v.verdict == "not_fallback" => {}
            _ => {
                not_judged.push(format!("{}:{}", c.file, c.line));
            }
        }
    }

    let mut out = Vec::new();
    if !confirmed.is_empty() {
        out.push(format_fallback_list("fallbacks added", &confirmed));
    }
    if !not_judged.is_empty() {
        out.push(format_fallback_list(
            "fallback candidates not judged",
            &not_judged,
        ));
    }
    out
}

/// Annotate each fallback candidate with its verdict and reason when judged by a succeeded review.
pub fn annotate_fallbacks(
    candidates: &[Finding],
    model_review: Option<&crate::model_review::ModelReviewResult>,
) -> Vec<FallbackRecord> {
    let ok_review = model_review.filter(|mr| !mr.is_failed());
    candidates
        .iter()
        .map(|c| {
            let matched = ok_review.and_then(|mr| {
                mr.fallbacks
                    .iter()
                    .find(|v| v.file == c.file && v.line == c.line)
            });
            FallbackRecord {
                file: c.file.clone(),
                line: c.line,
                pattern: c.pattern,
                text: c.text.clone(),
                verdict: matched.map(|v| v.verdict.clone()),
                reason: matched.map(|v| v.reason.clone()),
            }
        })
        .collect()
}

fn format_not_judged_why(n: usize, why: &str) -> String {
    let why_one_line: String = why
        .lines()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join(" ");
    let prefix = format!("possible fallbacks added: {n} (not judged: ");
    let full = format!("{prefix}{why_one_line})");
    if full.chars().count() <= 200 {
        return full;
    }
    let avail = 200usize.saturating_sub(prefix.chars().count() + 2);
    let truncated: String = why_one_line.chars().take(avail).collect();
    format!("{prefix}{truncated}…)")
}

fn format_fallback_list(prefix: &str, items: &[String]) -> String {
    let clean: Vec<String> = items.iter().map(|s| s.replace(['\r', '\n'], " ")).collect();
    let n = clean.len();
    let head = format!("{prefix}: {n} (");
    let full = format!("{head}{})", clean.join(", "));
    if full.chars().count() <= 200 {
        return full;
    }
    for m in (1..n).rev() {
        let k = n - m;
        let cand = format!("{head}{}, … and {k} more)", clean[..m].join(", "));
        if cand.chars().count() <= 200 {
            return cand;
        }
    }
    if n > 1 {
        let suffix = format!(", … and {} more)", n - 1);
        let avail = 200usize.saturating_sub(head.chars().count() + suffix.chars().count());
        if avail > 0 {
            let first: String = clean[0].chars().take(avail).collect();
            return format!("{head}{first}{suffix}");
        }
        return sanitize_flag(format!("{head}… and {n} more)"));
    }
    let avail = 200usize.saturating_sub(head.chars().count() + 1);
    let first: String = clean[0].chars().take(avail).collect();
    format!("{head}{first})")
}

fn sanitize_flag(flag: String) -> String {
    let single_line = flag.lines().next().unwrap_or("").to_string();
    if single_line.chars().count() > 200 {
        single_line.chars().take(200).collect()
    } else {
        single_line
    }
}

fn is_supported_ext(ext: &str) -> bool {
    matches!(
        ext,
        "rs" | "py" | "js" | "jsx" | "mjs" | "ts" | "tsx" | "sh" | "bash" | "yml" | "yaml"
    )
}

fn is_test_file(path: &str) -> bool {
    let p = Path::new(path);
    for comp in p.components() {
        if let Component::Normal(os) = comp {
            if let Some(s) = os.to_str() {
                if matches!(
                    s,
                    "tests"
                        | "test"
                        | "__tests__"
                        | "benches"
                        | "examples"
                        | "fixtures"
                        | "testdata"
                ) {
                    return true;
                }
            }
        }
    }
    if let Some(file_name) = p.file_name().and_then(|s| s.to_str()) {
        if file_name.starts_with("test_") || file_name == "conftest.py" {
            return true;
        }
        if let (Some(stem), Some(ext)) = (
            p.file_stem().and_then(|s| s.to_str()),
            p.extension().and_then(|s| s.to_str()),
        ) {
            if !ext.is_empty()
                && (stem.ends_with("_test") || stem.ends_with(".test") || stem.ends_with(".spec"))
            {
                return true;
            }
        }
    }
    false
}

fn is_comment(content: &str, ext: &str) -> bool {
    let trimmed = content.trim_start();
    if trimmed.starts_with("//") || trimmed.starts_with('*') || trimmed.starts_with("--") {
        return true;
    }
    if trimmed.starts_with('#') {
        if ext == "rs" && (trimmed.starts_with("#[") || trimmed.starts_with("#!")) {
            return false;
        }
        return true;
    }
    false
}

fn match_line(content: &str, ext: &str, diff_lines: &[&str], idx: usize) -> Vec<&'static str> {
    let mut out = Vec::new();
    match ext {
        "rs" => {
            if content.contains(".unwrap_or(") {
                out.push("unwrap_or");
            }
            if content.contains(".unwrap_or_default(") {
                out.push("unwrap_or_default");
            }
            if content.contains(".unwrap_or_else(") {
                out.push("unwrap_or_else");
            }
        }
        "py" => {
            if matches_py_except_pass(content, diff_lines, idx) {
                out.push("except-pass");
            }
            if matches_py_get_default(content) {
                out.push("get-default");
            }
            if matches_py_or_literal(content) {
                out.push("or-literal");
            }
        }
        "js" | "jsx" | "mjs" | "ts" | "tsx" => {
            if content.contains("??") {
                out.push("nullish");
            }
            if matches_js_or_literal(content) {
                out.push("or-literal");
            }
            if matches_js_empty_catch(content) {
                out.push("empty-catch");
            }
        }
        "sh" | "bash" | "yml" | "yaml" => {
            if content.contains("|| true") || content.contains("|| :") {
                out.push("or-true");
            }
            if matches_shell_param_default(content) {
                out.push("param-default");
            }
        }
        _ => {}
    }
    out
}

fn matches_py_except_pass(content: &str, diff_lines: &[&str], idx: usize) -> bool {
    let trimmed = content.trim_start();
    let after_except = if let Some(rest) = trimmed.strip_prefix("except:") {
        Some(rest)
    } else if let Some(rest) = trimmed.strip_prefix("except Exception") {
        if rest.starts_with(':') || rest.starts_with(char::is_whitespace) {
            rest.split_once(':').map(|(_, r)| r)
        } else {
            None
        }
    } else if let Some(rest) = trimmed.strip_prefix("except BaseException") {
        if rest.starts_with(':') || rest.starts_with(char::is_whitespace) {
            rest.split_once(':').map(|(_, r)| r)
        } else {
            None
        }
    } else {
        None
    };

    let Some(after_colon) = after_except else {
        return false;
    };

    let inline = after_colon.split('#').next().unwrap_or(after_colon).trim();
    if !inline.is_empty() {
        return is_pass_continue_or_return(inline);
    }

    for &next_raw in &diff_lines[idx + 1..] {
        if next_raw.starts_with("+++ ")
            || next_raw.starts_with("@@ ")
            || next_raw.starts_with("diff --git ")
            || next_raw.starts_with(' ')
        {
            break;
        }
        if let Some(added) = next_raw.strip_prefix('+') {
            let added_trim = added.trim();
            if added_trim.is_empty() {
                continue;
            }
            let stmt = added_trim.split('#').next().unwrap_or(added_trim).trim();
            return is_pass_continue_or_return(stmt);
        }
    }
    false
}

fn is_pass_continue_or_return(s: &str) -> bool {
    s == "pass" || s == "continue" || s.starts_with("return")
}

fn matches_py_get_default(content: &str) -> bool {
    let bytes = content.as_bytes();
    for i in 0..bytes.len() {
        let rest = &content[i..];
        let args_start = if let Some(after) = rest.strip_prefix(".get(") {
            Some(after)
        } else if let Some(after) = rest.strip_prefix("getenv(") {
            let prev_ok = i == 0 || {
                let b = bytes[i - 1];
                !b.is_ascii_alphanumeric() && b != b'_'
            };
            if prev_ok {
                Some(after)
            } else {
                None
            }
        } else {
            None
        };
        if let Some(after_open) = args_start {
            if has_top_level_comma(after_open) {
                return true;
            }
        }
    }
    false
}

fn has_top_level_comma(s: &str) -> bool {
    let mut depth = 0usize;
    let mut in_str: Option<char> = None;
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if let Some(q) = in_str {
            if c == '\\' {
                let _ = chars.next();
            } else if c == q {
                in_str = None;
            }
            continue;
        }
        match c {
            '"' | '\'' => in_str = Some(c),
            '(' | '[' | '{' => depth += 1,
            ')' => {
                if depth == 0 {
                    return false;
                }
                depth -= 1;
            }
            ']' | '}' => {
                depth = depth.saturating_sub(1);
            }
            ',' if depth == 0 => return true,
            _ => {}
        }
    }
    false
}

fn matches_py_or_literal(content: &str) -> bool {
    let mut search = content;
    while let Some(pos) = search.find(" or ") {
        let after = &search[pos + 4..];
        let after_trimmed = after.trim_start_matches([' ', '\t']);
        if let Some(c) = after_trimmed.chars().next() {
            if matches!(c, '"' | '\'' | '[' | '{' | '(' | '0'..='9') {
                return true;
            }
        }
        search = after;
    }
    false
}

fn matches_js_or_literal(content: &str) -> bool {
    let mut search = content;
    while let Some(pos) = search.find("||") {
        let after = &search[pos + 2..];
        let after_trimmed = after.trim_start_matches([' ', '\t']);
        if let Some(c) = after_trimmed.chars().next() {
            if matches!(c, '"' | '\'' | '`' | '[' | '{' | '0'..='9') {
                return true;
            }
        }
        search = after;
    }
    false
}

fn matches_js_empty_catch(content: &str) -> bool {
    let bytes = content.as_bytes();
    for i in 0..bytes.len() {
        let Some(after_catch) = content[i..].strip_prefix("catch") else {
            continue;
        };
        let prev_ok = i == 0 || {
            let b = bytes[i - 1];
            !b.is_ascii_alphanumeric() && b != b'_' && b != b'$'
        };
        let next_ok = after_catch
            .chars()
            .next()
            .map(|c| !c.is_ascii_alphanumeric() && c != '_' && c != '$')
            .unwrap_or(false);
        if !prev_ok || !next_ok {
            continue;
        }
        let mut rest = after_catch.trim_start();
        if let Some(after_open_paren) = rest.strip_prefix('(') {
            let mut depth = 1usize;
            let mut end_idx = None;
            for (idx, ch) in after_open_paren.char_indices() {
                match ch {
                    '(' => depth += 1,
                    ')' => {
                        depth -= 1;
                        if depth == 0 {
                            end_idx = Some(idx + 1);
                            break;
                        }
                    }
                    _ => {}
                }
            }
            let Some(end) = end_idx else {
                continue;
            };
            rest = after_open_paren[end..].trim_start();
        }
        if let Some(after_brace) = rest.strip_prefix('{') {
            if after_brace == "}"
                || after_brace == " }"
                || after_brace.trim_start().starts_with('}')
            {
                return true;
            }
        }
    }
    false
}

fn matches_shell_param_default(content: &str) -> bool {
    let mut search = content;
    while let Some(pos) = search.find("${") {
        let after = &search[pos + 2..];
        let inside = after.split('}').next().unwrap_or(after);
        if inside.contains(":-") {
            return true;
        }
        search = after;
    }
    false
}

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

    #[test]
    fn flags_for_review_rejects_malformed_result_without_unknown_error_placeholder() {
        let candidates = vec![Finding {
            file: "src/lib.rs".to_string(),
            line: 10,
            pattern: "unwrap_or",
            text: "x.unwrap_or(0)".to_string(),
        }];
        let malformed = ModelReviewResult {
            status: "error".to_string(),
            model: Some("test-model".to_string()),
            cost_usd: None,
            concerns: Vec::new(),
            fallbacks: Vec::new(),
            error: None,
        };
        let flags = flags_for_review(&candidates, Some(&malformed));
        assert_eq!(flags.len(), 1);
        assert!(!flags[0].contains("unknown error"), "flag={}", flags[0]);
        assert!(!flags[0].contains('?'), "flag={}", flags[0]);
        assert!(flags[0].contains("test-model"), "flag={}", flags[0]);
    }
}