zagens-cli 0.7.1

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
//! CRAFT P1: File-based blackboard for structured inter-agent context sharing.
//!
//! The blackboard is a JSON file at `.zagens/blackboards/{task_id}.json`.
//! Each role writes its own partition on completion; the next agent in the
//! task reads the board at spawn time. No live reload — the snapshot is taken
//! once and injected into the child's assignment prompt.

use std::path::{Path, PathBuf};

use zagens_config::{
    legacy_workspace_meta_dir, workspace_meta_dir, workspace_meta_file_read,
    workspace_meta_file_write,
};

use serde_json::{Value, json};

use zagens_core::subagent::{StructuredFindings, StructuredVerdict, SubAgentType};

use super::SubAgentResult;

// ── Path helpers ──────────────────────────────────────────────

/// Validate a CRAFT blackboard task id (filename stem under `.zagens/blackboards/`).
pub fn validate_task_id(task_id: &str) -> Result<(), String> {
    if task_id.is_empty() {
        return Err("task_id 不能为空".to_string());
    }
    if task_id.contains("..") || task_id.contains('/') || task_id.contains('\\') {
        return Err("task_id 含非法路径字符".to_string());
    }
    if !task_id
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        return Err("task_id 仅允许字母、数字、_ 和 -".to_string());
    }
    Ok(())
}

fn blackboard_rel(task_id: &str) -> Result<String, String> {
    validate_task_id(task_id)?;
    Ok(format!("blackboards/{task_id}.json"))
}

fn blackboard_path_read(workspace: &Path, task_id: &str) -> Result<PathBuf, String> {
    Ok(workspace_meta_file_read(
        workspace,
        &blackboard_rel(task_id)?,
    ))
}

fn blackboard_path_write(workspace: &Path, task_id: &str) -> Result<PathBuf, String> {
    Ok(workspace_meta_file_write(
        workspace,
        &blackboard_rel(task_id)?,
    ))
}

fn ensure_dir(path: &Path) {
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
}

// ── Public API ─────────────────────────────────────────────────

/// Read the blackboard and format the relevant section as Markdown
/// for injection into the child's assignment prompt.
///
/// Returns `None` if the file doesn't exist or is unreadable.
pub fn read_blackboard_section(
    workspace: &Path,
    task_id: &str,
    agent_type: &SubAgentType,
) -> Option<String> {
    let path = blackboard_path_read(workspace, task_id).ok()?;
    let raw = std::fs::read_to_string(&path).ok()?;
    let board: Value = serde_json::from_str(&raw).ok()?;

    let mut sections: Vec<String> = Vec::new();

    match agent_type {
        SubAgentType::Implementer => {
            // Implementer needs explorer findings + reviewer blockers + verifier failures
            if let Some(s) = format_explorer_findings(&board) {
                sections.push(s);
            }
            if let Some(s) = format_reviewer_blockers(&board) {
                sections.push(s);
            }
            if let Some(s) = format_verifier_failures(&board) {
                sections.push(s);
            }
        }
        SubAgentType::Review => {
            // Reviewer needs implementer changes (to know what was modified)
            if let Some(s) = format_implementer_changes(&board) {
                sections.push(s);
            }
        }
        SubAgentType::Verifier => {
            // Verifier needs implementer changes (to know what to test)
            if let Some(s) = format_implementer_changes(&board) {
                sections.push(s);
            }
        }
        SubAgentType::Auditor => {
            if let Some(s) = format_scratchpad_mirror(&board) {
                sections.push(s);
            }
            if let Some(s) = format_reviewer_blockers(&board) {
                sections.push(s);
            }
        }
        _ => {
            // Explorer, Plan, Custom — no blackboard injection needed
        }
    }

    if sections.is_empty() {
        None
    } else {
        Some(sections.join("\n\n"))
    }
}

/// Read the full blackboard as a raw `serde_json::Value`.
/// Returns `None` when the file doesn't exist or is unparseable.
pub fn read_blackboard_raw(workspace: &Path, task_id: &str) -> Option<Value> {
    let path = blackboard_path_read(workspace, task_id).ok()?;
    let raw = std::fs::read_to_string(&path).ok()?;
    serde_json::from_str(&raw).ok()
}

/// List all task_ids that have a blackboard file under the given workspace.
pub fn list_blackboard_tasks(workspace: &Path) -> Vec<String> {
    let mut tasks = std::collections::BTreeSet::new();
    for root in [
        workspace_meta_dir(workspace).join("blackboards"),
        legacy_workspace_meta_dir(workspace).join("blackboards"),
    ] {
        let Ok(dir) = std::fs::read_dir(&root) else {
            continue;
        };
        for entry in dir.filter_map(Result::ok) {
            let name = entry.file_name().to_string_lossy().into_owned();
            if let Some(task_id) = name.strip_suffix(".json") {
                tasks.insert(task_id.to_string());
            }
        }
    }
    tasks.into_iter().collect()
}

/// Write one partition to the blackboard for the given agent type.
///
/// Reads the existing board (or starts fresh), updates the matching
/// partition key, and writes back atomically.
pub fn write_blackboard_partition(
    workspace: &Path,
    task_id: &str,
    agent_type: &SubAgentType,
    result: &SubAgentResult,
) {
    let Ok(path) = blackboard_path_write(workspace, task_id) else {
        return;
    };
    ensure_dir(&path);

    // Read existing board first — needed by Implementer for round tracking
    let existing_raw = std::fs::read_to_string(&path).unwrap_or_default();

    let (partition_key, partition_data) = match agent_type {
        SubAgentType::Explore => {
            // Explorer: write findings from structured verdict (P1+)
            // CRAFT V2: also extract coverage metadata from the output
            let mut partition = json!({
                "findings": build_explorer_findings(result),
                "impact_summary": extract_impact_summary(result),
                "files_examined": extract_files_examined(result),
                "coverage_confidence": extract_coverage_confidence(result),
            });
            if let Some(findings) = &result.structured_findings
                && let Value::Object(ref mut map) = partition
            {
                map.insert(
                    "structured_findings".to_string(),
                    serde_json::to_value(findings).unwrap_or(Value::Null),
                );
            }
            ("explorer", partition)
        }
        SubAgentType::Implementer => {
            // CRAFT V2: append current round to rounds[] history
            (
                "implementer",
                build_implementer_rounds(result, &existing_raw, workspace),
            )
        }
        SubAgentType::Review => {
            let mut partition = json!({
                "verdict": result.structured_verdict.as_ref()
                    .map(|v| serde_json::to_value(&v.verdict).unwrap_or(json!("PASS")))
                    .unwrap_or(json!("PASS")),
                "blockers": result.structured_verdict.as_ref()
                    .map(|v| &v.items)
                    .unwrap_or(&vec![]),
            });
            if let Some(verdict) = &result.structured_verdict
                && let Value::Object(ref mut map) = partition
            {
                map.insert(
                    "structured_verdict".to_string(),
                    serde_json::to_value(verdict).unwrap_or(Value::Null),
                );
            }
            ("reviewer", partition)
        }
        SubAgentType::Verifier => (
            "verifier",
            json!({
                "verdict": result.structured_verdict.as_ref()
                    .map(|v| serde_json::to_value(&v.verdict).unwrap_or(json!("PASS")))
                    .unwrap_or(json!("PASS")),
                "failures": build_verifier_failures(result),
                "summary": extract_verifier_summary(result),
            }),
        ),
        SubAgentType::Auditor => (
            "auditor",
            json!({
                "verdict": result.structured_verdict.as_ref()
                    .map(|v| serde_json::to_value(&v.verdict).unwrap_or(json!("FAIL")))
                    .unwrap_or(json!("FAIL")),
                "details": extract_auditor_details(result),
            }),
        ),
        _ => return, // General / Plan / Custom — no blackboard write
    };

    // Merge → write atomically (existing_raw already read above)
    let mut board: Value = if existing_raw.trim().is_empty() {
        json!({
            "schema_version": 1,
            "task_id": task_id,
        })
    } else {
        serde_json::from_str(&existing_raw).unwrap_or(json!({
            "schema_version": 1,
            "task_id": task_id,
        }))
    };

    if let Value::Object(ref mut map) = board {
        map.insert(partition_key.to_string(), partition_data);
    }

    let payload = serde_json::to_string_pretty(&board).unwrap_or_default();
    let tmp_path = path.with_extension("tmp");
    let _ = std::fs::write(&tmp_path, &payload);
    let _ = std::fs::rename(&tmp_path, &path);
}

/// Read structured audit findings written by an Explore agent.
pub fn read_structured_findings_from_blackboard(
    workspace: &Path,
    task_id: &str,
    agent_type: &SubAgentType,
) -> Option<StructuredFindings> {
    if !matches!(agent_type, SubAgentType::Explore) {
        return None;
    }
    let board = read_blackboard_raw(workspace, task_id)?;
    board
        .get("explorer")?
        .get("structured_findings")
        .and_then(|v| serde_json::from_value(v.clone()).ok())
}

/// Read structured CRAFT verdict written by a Review agent.
pub fn read_structured_verdict_from_blackboard(
    workspace: &Path,
    task_id: &str,
    agent_type: &SubAgentType,
) -> Option<StructuredVerdict> {
    if !matches!(agent_type, SubAgentType::Review) {
        return None;
    }
    let board = read_blackboard_raw(workspace, task_id)?;
    board
        .get("reviewer")?
        .get("structured_verdict")
        .and_then(|v| serde_json::from_value(v.clone()).ok())
}

/// BLOCKER-severity items from the latest CRAFT review blackboard partition.
#[must_use]
pub fn read_reviewer_blocker_items(
    workspace: &Path,
    task_id: &str,
) -> Vec<zagens_core::subagent::VerdictItem> {
    read_structured_verdict_from_blackboard(workspace, task_id, &SubAgentType::Review)
        .map(|v| v.items)
        .unwrap_or_default()
        .into_iter()
        .filter(|item| item.severity.eq_ignore_ascii_case("BLOCKER"))
        .collect()
}

// ── Markdown formatters (read side) ────────────────────────────

fn format_explorer_findings(board: &Value) -> Option<String> {
    let findings = board.get("explorer")?.get("findings")?.as_array()?;
    if findings.is_empty() {
        return None;
    }
    let mut lines = vec!["### Explorer findings".to_string()];
    for f in findings {
        let file = f.get("file").and_then(|v| v.as_str()).unwrap_or("?");
        let concern = f.get("concern").and_then(|v| v.as_str()).unwrap_or("?");
        let severity = f.get("severity").and_then(|v| v.as_str()).unwrap_or("?");
        let suggestion = f
            .get("suggestion")
            .and_then(|v| v.as_str())
            .map(|s| format!("{s}"))
            .unwrap_or_default();
        lines.push(format!("- [{severity}] `{file}` — {concern}{suggestion}"));
    }
    Some(lines.join("\n"))
}

fn format_reviewer_blockers(board: &Value) -> Option<String> {
    let blockers = board.get("reviewer")?.get("blockers")?.as_array()?;
    if blockers.is_empty() {
        return None;
    }
    let mut lines = vec!["### Reviewer blockers".to_string()];
    for b in blockers {
        let id = b.get("id").and_then(|v| v.as_str()).unwrap_or("?");
        let file = b.get("file").and_then(|v| v.as_str()).unwrap_or("?");
        let line = b
            .get("line")
            .and_then(|v| v.as_u64())
            .map(|l| format!(":{l}"))
            .unwrap_or_default();
        let desc = b.get("description").and_then(|v| v.as_str()).unwrap_or("?");
        lines.push(format!("- [{id}] `{file}{line}` — {desc}"));
    }
    Some(lines.join("\n"))
}

fn format_verifier_failures(board: &Value) -> Option<String> {
    let failures = board.get("verifier")?.get("failures")?.as_array()?;
    if failures.is_empty() {
        return None;
    }
    let mut lines = vec!["### Verifier failures".to_string()];
    for f in failures {
        let file = f.get("file").and_then(|v| v.as_str()).unwrap_or("?");
        let line = f
            .get("line")
            .and_then(|v| v.as_u64())
            .map(|l| format!(":{l}"))
            .unwrap_or_default();
        let observed = f.get("observed").and_then(|v| v.as_str()).unwrap_or("?");
        let hypothesis = f
            .get("hypothesis")
            .and_then(|v| v.as_str())
            .filter(|s| !s.is_empty())
            .map(|h| format!(" (hypothesis: {h})"))
            .unwrap_or_default();
        lines.push(format!("- `{file}{line}` — {observed}{hypothesis}"));
    }
    Some(lines.join("\n"))
}

fn format_implementer_changes(board: &Value) -> Option<String> {
    let rounds = board.get("implementer")?.get("rounds")?.as_array()?;
    if rounds.is_empty() {
        return None;
    }
    let mut lines = vec!["### Implementer changes".to_string()];
    for round in rounds {
        let changes = round.get("changes")?.as_array()?;
        for c in changes {
            let file = c.get("file").and_then(|v| v.as_str()).unwrap_or("?");
            let intent = c.get("intent").and_then(|v| v.as_str()).unwrap_or("?");
            lines.push(format!("- `{file}` — {intent}"));
        }
    }
    // Append factual symbol index change record (not model-narrated).
    if let Some(round) = rounds.last()
        && let Some(sc) = round.get("symbol_changes")
        && !sc.is_null()
    {
        lines.push(String::new());
        lines.push("### Symbol index changes (factual)".to_string());
        if let Some(added) = sc.get("added").and_then(|v| v.as_array()) {
            for a in added {
                if let Some(s) = a.as_str() {
                    lines.push(format!("- added: `{s}`"));
                }
            }
        }
        if let Some(removed) = sc.get("removed").and_then(|v| v.as_array()) {
            for r in removed {
                if let Some(s) = r.as_str() {
                    lines.push(format!("- removed: `{s}`"));
                }
            }
        }
        if let Some(modified) = sc.get("modified").and_then(|v| v.as_array()) {
            for m in modified {
                if let Some(s) = m.as_str() {
                    lines.push(format!("- modified: `{s}`"));
                }
            }
        }
    }
    Some(lines.join("\n"))
}

// ── Data extractors (write side) ───────────────────────────────

fn build_explorer_findings(result: &SubAgentResult) -> Value {
    if let Some(f) = &result.structured_findings {
        let items: Vec<Value> = f
            .items
            .iter()
            .map(|item| {
                json!({
                    "file": item.file,
                    "line": item.line,
                    "line_end": item.line_end,
                    "concern": item.claim,
                    "severity": item.severity,
                    "evidence": item.evidence,
                    "kind": item.kind,
                })
            })
            .collect();
        return json!(items);
    }
    // Fallback: CRAFT structured_verdict (legacy explore output)
    match &result.structured_verdict {
        Some(v) => {
            let items: Vec<Value> = v
                .items
                .iter()
                .map(|item| {
                    json!({
                        "file": item.file,
                        "concern": item.description,
                        "severity": item.severity,
                        "suggestion": item.suggestion,
                    })
                })
                .collect();
            json!(items)
        }
        None => json!([]),
    }
}

fn extract_impact_summary(result: &SubAgentResult) -> String {
    result
        .structured_verdict
        .as_ref()
        .and_then(|v| v.summary.as_deref())
        .unwrap_or("")
        .to_string()
}

fn build_verifier_failures(result: &SubAgentResult) -> Value {
    let Some(v) = &result.structured_verdict else {
        return json!([]);
    };
    if !matches!(
        v.verdict,
        super::VerdictLevel::Fail | super::VerdictLevel::Blocker
    ) {
        return json!([]);
    }
    let failures: Vec<Value> = v
        .items
        .iter()
        .map(|item| {
            json!({
                "observed": item.description,
                "hypothesis": item.suggestion,
                "file": item.file,
                "line": item.line,
                "rule": item.rule,
            })
        })
        .collect();
    json!(failures)
}

fn extract_verifier_summary(result: &SubAgentResult) -> String {
    result
        .structured_verdict
        .as_ref()
        .and_then(|v| v.summary.as_deref())
        .unwrap_or(result.result.as_deref().unwrap_or(""))
        .to_string()
}

/// Extract audit failure details from the Auditor's structured_verdict.
/// Falls back to scanning the result text for "FAIL" markers when no
/// structured_verdict is present (graceful degradation for path B).
fn extract_auditor_details(result: &SubAgentResult) -> Value {
    if let Some(v) = &result.structured_verdict {
        json!(v.items)
    } else if let Some(text) = &result.result {
        // Fallback: scan for FAIL markers in the text output
        let details: Vec<Value> = text
            .lines()
            .filter(|l| l.contains("缺失:") || l.contains("FAIL"))
            .map(|l| json!({"detail": l.trim().to_string()}))
            .collect();
        json!(details)
    } else {
        json!([])
    }
}

// ── Phase C3: scratchpad mirror partition ───────────────────────

/// Write read-only scratchpad stats before `agent_spawn(type=auditor)` (§6.12.6).
pub fn write_scratchpad_mirror(
    task_id: &str,
    workspace: &Path,
    run_id: &str,
    config: &crate::scratchpad::ScratchpadConfig,
) {
    let Some(store) = crate::scratchpad::try_open_store(workspace, Some(run_id), None, None) else {
        return;
    };
    let Ok(inventory) = store.read_inventory() else {
        return;
    };
    let Ok(notes) = store.read_notes() else {
        return;
    };
    let stats = crate::scratchpad::compute_coverage_stats(&inventory, &notes, config);
    let superseded = crate::scratchpad::compute_superseded_ids(&notes);
    let high_note_ids: Vec<String> = notes
        .iter()
        .filter(|n| {
            crate::scratchpad::is_verified_finding(n, &superseded)
                && crate::scratchpad::is_high_severity(n.severity.as_deref())
        })
        .map(|n| n.id.clone())
        .collect();

    let partition = json!({
        "run_id": store.run_id(),
        "path": crate::scratchpad::display_run_path(store.run_id()),
        "areas_done": stats.areas_accounted,
        "areas_total": stats.areas_total,
        "findings_verified": stats.verified_findings,
        "high_note_ids": high_note_ids,
    });
    merge_board_partition(workspace, task_id, "scratchpad", partition);
}

fn merge_board_partition(
    workspace: &Path,
    task_id: &str,
    partition_key: &str,
    partition_data: Value,
) {
    let Ok(path) = blackboard_path_write(workspace, task_id) else {
        return;
    };
    ensure_dir(&path);
    let existing_raw = std::fs::read_to_string(&path).unwrap_or_default();
    let mut board: Value = if existing_raw.trim().is_empty() {
        json!({
            "schema_version": 1,
            "task_id": task_id,
        })
    } else {
        serde_json::from_str(&existing_raw).unwrap_or(json!({
            "schema_version": 1,
            "task_id": task_id,
        }))
    };
    if let Value::Object(ref mut map) = board {
        map.insert(partition_key.to_string(), partition_data);
    }
    let payload = serde_json::to_string_pretty(&board).unwrap_or_default();
    let tmp_path = path.with_extension("tmp");
    let _ = std::fs::write(&tmp_path, &payload);
    let _ = std::fs::rename(&tmp_path, &path);
}

fn format_scratchpad_mirror(board: &Value) -> Option<String> {
    let sp = board.get("scratchpad")?;
    let run_id = sp.get("run_id")?.as_str()?;
    let path = sp.get("path").and_then(|v| v.as_str()).unwrap_or("?");
    let done = sp.get("areas_done").and_then(|v| v.as_u64()).unwrap_or(0);
    let total = sp.get("areas_total").and_then(|v| v.as_u64()).unwrap_or(0);
    let verified = sp
        .get("findings_verified")
        .and_then(|v| v.as_u64())
        .unwrap_or(0);
    let highs = sp
        .get("high_note_ids")
        .and_then(|v| v.as_array())
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        })
        .unwrap_or_default();
    Some(format!(
        "### Scratchpad mirror\n- run_id: `{run_id}`\n- path: `{path}`\n- areas accounted: {done}/{total}\n- verified findings: {verified}\n- high_note_ids: [{highs}]"
    ))
}

// ── Tests ──────────────────────────────────────────────────────

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

    fn test_workspace() -> PathBuf {
        std::env::temp_dir().join(format!("deepseek-blackboard-test-{}", std::process::id()))
    }

    #[test]
    fn test_blackboard_path_write_contains_task_id() {
        let ws = test_workspace();
        let path = blackboard_path_write(&ws, "bugfix-001").expect("valid task id");
        let s = path.to_string_lossy();
        assert!(
            s.contains("bugfix-001"),
            "path should contain task id, got: {s}"
        );
        assert!(s.ends_with(".json"), "path should end with .json, got: {s}");
        assert!(
            s.contains(&ws.to_string_lossy().to_string()),
            "path should be under workspace"
        );
    }

    #[test]
    fn test_blackboard_path_write_rejects_traversal() {
        let ws = test_workspace();
        assert!(blackboard_path_write(&ws, "/tmp/evil").is_err());
        assert!(blackboard_path_write(&ws, "..\\escape").is_err());
    }

    #[test]
    fn test_read_blackboard_returns_none_for_missing_file() {
        let ws = test_workspace();
        let result =
            read_blackboard_section(&ws, "nonexistent-task-99999", &SubAgentType::Implementer);
        assert!(result.is_none(), "missing file should return None");
    }

    #[test]
    fn test_list_and_read_blackboard_raw() {
        let ws = test_workspace();
        let task_id = "list-test-001";
        let path = blackboard_path_write(&ws, task_id).expect("valid task id");
        let _ = std::fs::remove_file(&path);
        assert!(list_blackboard_tasks(&ws).is_empty());

        if let Some(parent) = path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let board = json!({"schema_version": 1, "task_id": task_id});
        std::fs::write(&path, serde_json::to_string_pretty(&board).unwrap()).unwrap();

        let tasks = list_blackboard_tasks(&ws);
        assert!(tasks.contains(&task_id.to_string()));

        let raw = read_blackboard_raw(&ws, task_id).expect("should read board");
        assert_eq!(raw["task_id"], task_id);

        let _ = std::fs::remove_file(&path);
    }

    // ── CRAFT P1 integration tests ──────────────────────────

    use crate::tools::subagent::{
        StructuredVerdict, SubAgentAssignment, SubAgentResult, SubAgentStatus, SubAgentType as SAT,
        VerdictItem, VerdictLevel,
    };

    #[test]
    fn test_write_and_read_explorer_findings() {
        let ws = test_workspace();
        let task_id = "test-001";
        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));

        // Simulate Explorer completion with structured_verdict
        let verdict = StructuredVerdict {
            verdict: VerdictLevel::Pass,
            items: vec![
                VerdictItem {
                    severity: "high".into(),
                    file: "auth/login.rs".into(),
                    line: Some(42),
                    description: "token uses standard RNG instead of CSPRNG".into(),
                    rule: Some("TOKEN_INSECURE_RNG".into()),
                    suggestion: Some("replace with OsRng".into()),
                },
                VerdictItem {
                    severity: "medium".into(),
                    file: "auth/session.rs".into(),
                    line: None,
                    description: "session timeout is hardcoded".into(),
                    rule: None,
                    suggestion: Some("make configurable".into()),
                },
            ],
            summary: Some("Two risks in auth module".into()),
        };

        let result = SubAgentResult {
            agent_id: "agent_test".into(),
            agent_type: SAT::Explore,
            assignment: SubAgentAssignment::new("explore auth".into(), None),
            model: "deepseek-v4-flash".into(),
            nickname: None,
            status: SubAgentStatus::Completed,
            result: Some("found two risks".into()),
            steps_taken: 5,
            duration_ms: 1000,
            from_prior_session: false,
            structured_verdict: Some(verdict),
            structured_findings: None,
            completion_reason: None,
            max_steps: 100,
            step_timeout_ms: 600_000,
            structured_findings_parse_failure: None,
            scratchpad_run_id: None,
            parent_thread_id: None,
            progress_status: None,
            stuck_suspected: false,
            idle_ms: 0,
        };

        // Write to blackboard
        write_blackboard_partition(&ws, task_id, &SAT::Explore, &result);

        // Read back as Implementer
        let section = read_blackboard_section(&ws, task_id, &SAT::Implementer)
            .expect("should read back explorer findings for implementer");

        assert!(
            section.contains("### Explorer findings"),
            "section: {section}"
        );
        assert!(section.contains("auth/login.rs"), "section: {section}");
        assert!(
            section.contains("token uses standard RNG"),
            "section: {section}"
        );
        assert!(section.contains("replace with OsRng"), "section: {section}");
        assert!(section.contains("auth/session.rs"), "section: {section}");
        assert!(section.contains("session timeout"), "section: {section}");

        // Clean up
        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));
    }

    #[test]
    fn test_write_and_read_roundtrip_multiple_roles() {
        let ws = test_workspace();
        let task_id = "test-002";
        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));

        // Write explorer findings
        let explorer_result = SubAgentResult {
            agent_id: "e1".into(),
            agent_type: SAT::Explore,
            assignment: SubAgentAssignment::new("explore".into(), None),
            model: "deepseek-v4-flash".into(),
            nickname: None,
            status: SubAgentStatus::Completed,
            result: Some("done".into()),
            steps_taken: 1,
            duration_ms: 100,
            from_prior_session: false,
            structured_verdict: Some(StructuredVerdict {
                verdict: VerdictLevel::Pass,
                items: vec![VerdictItem {
                    severity: "high".into(),
                    file: "src/main.rs".into(),
                    line: Some(10),
                    description: "unsafe usage".into(),
                    rule: None,
                    suggestion: None,
                }],
                summary: Some("one risk".into()),
            }),
            structured_findings: None,
            completion_reason: None,
            max_steps: 100,
            step_timeout_ms: 600_000,
            structured_findings_parse_failure: None,
            scratchpad_run_id: None,
            parent_thread_id: None,
            progress_status: None,
            stuck_suspected: false,
            idle_ms: 0,
        };
        write_blackboard_partition(&ws, task_id, &SAT::Explore, &explorer_result);

        // Write reviewer blockers
        let reviewer_result = SubAgentResult {
            agent_id: "r1".into(),
            agent_type: SAT::Review,
            assignment: SubAgentAssignment::new("review".into(), None),
            model: "deepseek-v4-flash".into(),
            nickname: None,
            status: SubAgentStatus::Completed,
            result: Some("blocker found".into()),
            steps_taken: 2,
            duration_ms: 200,
            from_prior_session: false,
            structured_verdict: Some(StructuredVerdict {
                verdict: VerdictLevel::Blocker,
                items: vec![VerdictItem {
                    severity: "BLOCKER".into(),
                    file: "src/main.rs".into(),
                    line: Some(15),
                    description: "missing null check".into(),
                    rule: Some("NULL_SAFETY".into()),
                    suggestion: Some("add if guard".into()),
                }],
                summary: Some("one blocker".into()),
            }),
            structured_findings: None,
            completion_reason: None,
            max_steps: 100,
            step_timeout_ms: 600_000,
            structured_findings_parse_failure: None,
            scratchpad_run_id: None,
            parent_thread_id: None,
            progress_status: None,
            stuck_suspected: false,
            idle_ms: 0,
        };
        write_blackboard_partition(&ws, task_id, &SAT::Review, &reviewer_result);

        // Read back as Implementer — should see BOTH
        let section = read_blackboard_section(&ws, task_id, &SAT::Implementer)
            .expect("should read both sections");

        assert!(
            section.contains("### Explorer findings"),
            "section: {section}"
        );
        assert!(section.contains("unsafe usage"), "section: {section}");
        assert!(
            section.contains("### Reviewer blockers"),
            "section: {section}"
        );
        assert!(section.contains("missing null check"), "section: {section}");

        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));
    }

    #[test]
    fn test_verifier_failures_injected_for_implementer() {
        let ws = test_workspace();
        let task_id = "test-verifier-003";
        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));

        let verifier_result = SubAgentResult {
            agent_id: "v1".into(),
            agent_type: SAT::Verifier,
            assignment: SubAgentAssignment::new("verify".into(), None),
            model: "deepseek-v4-flash".into(),
            nickname: None,
            status: SubAgentStatus::Completed,
            result: Some("tests failed".into()),
            steps_taken: 1,
            duration_ms: 100,
            from_prior_session: false,
            structured_verdict: Some(StructuredVerdict {
                verdict: VerdictLevel::Fail,
                items: vec![VerdictItem {
                    severity: "FAIL".into(),
                    file: "src/lib.rs".into(),
                    line: Some(99),
                    description: "assertion failed".into(),
                    rule: None,
                    suggestion: Some("fix test setup".into()),
                }],
                summary: Some("one failure".into()),
            }),
            structured_findings: None,
            completion_reason: None,
            max_steps: 100,
            step_timeout_ms: 600_000,
            structured_findings_parse_failure: None,
            scratchpad_run_id: None,
            parent_thread_id: None,
            progress_status: None,
            stuck_suspected: false,
            idle_ms: 0,
        };
        write_blackboard_partition(&ws, task_id, &SAT::Verifier, &verifier_result);

        let section = read_blackboard_section(&ws, task_id, &SAT::Implementer)
            .expect("implementer should read verifier failures");
        assert!(section.contains("### Verifier failures"), "{section}");
        assert!(section.contains("assertion failed"), "{section}");
        assert!(section.contains("fix test setup"), "{section}");

        let _ = std::fs::remove_file(blackboard_path_write(&ws, task_id).expect("valid task id"));
    }
}

fn extract_files_examined(result: &SubAgentResult) -> Value {
    // Parse "## Coverage Report" section from result text
    let text = result.result.as_deref().unwrap_or("");
    let marker = "## Coverage Report";
    if let Some(after) = text.find(marker) {
        let section = &text[after + marker.len()..];
        // Extract "Files examined:" bullet list
        let mut files: Vec<String> = Vec::new();
        let mut in_files = false;
        for line in section.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("- **Files examined**")
                || trimmed.starts_with("- Files examined")
            {
                in_files = true;
                continue;
            }
            if in_files {
                if trimmed.starts_with("- ") && !trimmed.starts_with("- **") {
                    let path = trimmed.trim_start_matches("- ").trim();
                    if !path.is_empty() {
                        files.push(path.to_string());
                    }
                } else if trimmed.starts_with("- **")
                    || trimmed.starts_with("##")
                    || trimmed.is_empty()
                {
                    break;
                }
            }
        }
        json!(files)
    } else {
        json!([])
    }
}

fn extract_coverage_confidence(result: &SubAgentResult) -> Value {
    let text = result.result.as_deref().unwrap_or("");
    let re = regex::Regex::new(r"(?im)^-?\s*\*\*Confidence\*\*:\s*(high|medium|low)").ok();
    if let Some(re) = re
        && let Some(cap) = re.captures(text)
        && let Some(m) = cap.get(1)
    {
        return json!(m.as_str());
    }
    json!("unknown")
}

fn build_implementer_rounds(
    result: &SubAgentResult,
    existing_raw: &str,
    workspace: &Path,
) -> Value {
    // Read existing rounds, append a new one
    let mut existing_rounds: Vec<Value> = if existing_raw.trim().is_empty() {
        Vec::new()
    } else {
        serde_json::from_str::<Value>(existing_raw)
            .ok()
            .and_then(|v| v.get("implementer").cloned())
            .and_then(|v| v.get("rounds").cloned())
            .and_then(|v| v.as_array().cloned())
            .unwrap_or_default()
    };

    let round_num = existing_rounds.len() + 1;
    let changes = extract_changes_from_result(result);
    let symbol_changes = read_symbol_changes(workspace);

    let new_round = json!({
        "round": round_num,
        "changes": changes,
        "symbol_changes": symbol_changes,
    });

    existing_rounds.push(new_round);
    json!(existing_rounds)
}

fn read_symbol_changes(workspace: &Path) -> Value {
    let path = workspace_meta_file_read(workspace, ".symbols_changes.json");
    std::fs::read_to_string(&path)
        .ok()
        .and_then(|raw| serde_json::from_str(&raw).ok())
        .unwrap_or(json!(null))
}

fn extract_changes_from_result(result: &SubAgentResult) -> Value {
    // Extract changed files from result text (look for path-like references)
    let text = result.result.as_deref().unwrap_or("");
    let re = regex::Regex::new(r"(?m)^\s*(?:Modified|Changed|Added|Edited):\s*(.+)$").ok();
    let mut files: Vec<Value> = Vec::new();
    if let Some(re) = re {
        for cap in re.captures_iter(text) {
            if let Some(m) = cap.get(1) {
                files.push(json!(m.as_str().trim()));
            }
        }
    }
    if files.is_empty() {
        // Fallback: look for file paths in the result
        let path_re = regex::Regex::new(r"`(crates/\S+\.(?:rs|toml|ts|tsx|js|json|md))`").ok();
        if let Some(re) = path_re {
            for cap in re.captures_iter(text) {
                if let Some(m) = cap.get(1) {
                    let path = m.as_str().to_string();
                    if !files.iter().any(|v| v.as_str() == Some(&path)) {
                        files.push(json!(path));
                    }
                }
            }
        }
    }
    json!(files)
}