codewhale-tui 0.8.65

Terminal UI for open-source and open-weight coding models
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
//! Typed task-spec loading, artifact refs, deterministic scorers, and receipts.

#![allow(dead_code)]

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use chrono::{SecondsFormat, Utc};
use codewhale_protocol::fleet::*;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use super::ledger::FleetLedger;

const MAX_SCORER_READ_BYTES: u64 = 1_000_000;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FleetTaskSpecDocument {
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub labels: BTreeMap<String, String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub security_policy: Option<FleetSecurityPolicy>,
    #[serde(default, alias = "worker_specs")]
    pub workers: Vec<FleetWorkerSpec>,
    #[serde(default)]
    pub tasks: Vec<FleetTaskSpec>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
enum FleetTaskSpecFile {
    Document(FleetTaskSpecDocument),
    Tasks(Vec<FleetTaskSpec>),
    Single(Box<FleetTaskSpec>),
}

impl FleetTaskSpecFile {
    fn into_document(self, fallback_name: String) -> FleetTaskSpecDocument {
        match self {
            Self::Document(mut doc) => {
                if doc.name.as_deref().is_none_or(str::is_empty) {
                    doc.name = Some(fallback_name);
                }
                doc
            }
            Self::Tasks(tasks) => FleetTaskSpecDocument {
                name: Some(fallback_name),
                labels: BTreeMap::new(),
                security_policy: None,
                workers: Vec::new(),
                tasks,
            },
            Self::Single(task) => FleetTaskSpecDocument {
                name: Some(fallback_name),
                labels: BTreeMap::new(),
                security_policy: None,
                workers: Vec::new(),
                tasks: vec![*task],
            },
        }
    }
}

#[derive(Debug, Clone)]
pub struct FleetTaskVerificationInput {
    pub run_id: FleetRunId,
    pub task_id: String,
    pub worker_id: String,
    pub exit_code: Option<i32>,
    pub artifacts: Vec<FleetArtifactRef>,
    /// Resolved-route snapshot to persist on the receipt (#3154).
    pub resolved_route: Option<FleetResolvedRoute>,
}

#[derive(Debug, Clone)]
pub struct FleetTaskVerification {
    pub result: FleetTaskResult,
    pub failure_kind: Option<FleetTaskFailureKind>,
    pub score: FleetScore,
    pub evidence: Vec<String>,
}

pub fn load_task_spec_document(path: &Path) -> Result<FleetTaskSpecDocument> {
    let raw = std::fs::read_to_string(path)
        .with_context(|| format!("reading fleet task spec {}", path.display()))?;
    let fallback_name = path
        .file_stem()
        .and_then(|s| s.to_str())
        .filter(|s| !s.is_empty())
        .unwrap_or("fleet-run")
        .to_string();
    let parsed = match path.extension().and_then(|s| s.to_str()) {
        Some("toml") => toml::from_str::<FleetTaskSpecFile>(&raw)
            .with_context(|| format!("parsing TOML fleet task spec {}", path.display()))?,
        _ => serde_json::from_str::<FleetTaskSpecFile>(&raw)
            .with_context(|| format!("parsing JSON fleet task spec {}", path.display()))?,
    };
    let doc = parsed.into_document(fallback_name);
    validate_task_spec_document(&doc)?;
    Ok(doc)
}

pub fn validate_task_spec_document(doc: &FleetTaskSpecDocument) -> Result<()> {
    if doc.tasks.is_empty() {
        bail!("fleet task spec must include at least one task");
    }
    let mut ids = BTreeSet::new();
    for task in &doc.tasks {
        if task.id.trim().is_empty() {
            bail!("fleet task id cannot be empty");
        }
        if !ids.insert(task.id.clone()) {
            bail!("duplicate fleet task id {}", task.id);
        }
        if task.name.trim().is_empty() {
            bail!("fleet task {} name cannot be empty", task.id);
        }
        if task.instructions.trim().is_empty() {
            bail!("fleet task {} instructions cannot be empty", task.id);
        }
        if let Some(objective) = &task.objective
            && objective.trim().is_empty()
        {
            bail!("fleet task {} objective cannot be empty", task.id);
        }
        validate_worker_profile(&task.id, task.worker.as_ref())?;
        validate_tags(&task.id, &task.tags)?;
        validate_workspace_requirements(task)?;
    }
    Ok(())
}

fn validate_worker_profile(task_id: &str, worker: Option<&FleetTaskWorkerProfile>) -> Result<()> {
    let Some(worker) = worker else {
        return Ok(());
    };
    validate_worker_token(
        task_id,
        "worker.agent_profile",
        worker.agent_profile.as_deref(),
    )?;
    validate_worker_token(task_id, "worker.loadout", worker.loadout.as_deref())?;
    validate_worker_token(task_id, "worker.model_class", worker.model_class.as_deref())?;
    validate_worker_model(task_id, worker.model.as_deref())?;
    Ok(())
}

fn validate_worker_token(task_id: &str, field: &str, value: Option<&str>) -> Result<()> {
    let Some(value) = value else {
        return Ok(());
    };
    let trimmed = value.trim();
    if trimmed.is_empty() {
        bail!("fleet task {task_id} {field} cannot be empty");
    }
    if trimmed != value || !trimmed.chars().all(is_worker_token_char) {
        bail!(
            "fleet task {task_id} {field} must be a simple token, not a path or provider/model id"
        );
    }
    Ok(())
}

fn is_worker_token_char(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.')
}

fn validate_worker_model(task_id: &str, value: Option<&str>) -> Result<()> {
    let Some(value) = value else {
        return Ok(());
    };
    let trimmed = value.trim();
    if trimmed.is_empty() {
        bail!("fleet task {task_id} worker.model cannot be empty");
    }
    if trimmed != value
        || !trimmed
            .chars()
            .all(|ch| ch.is_ascii_graphic() && !matches!(ch, '=' | '\'' | '"'))
    {
        bail!(
            "fleet task {task_id} worker.model must be a visible model id without whitespace or secrets"
        );
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn write_fleet_artifact_ref(
    workspace: &Path,
    run_id: &FleetRunId,
    task_id: &str,
    worker_id: &str,
    kind: FleetArtifactKind,
    filename: &str,
    contents: &[u8],
    mime_type: Option<&str>,
) -> Result<FleetArtifactRef> {
    let rel_path = PathBuf::from(".codewhale")
        .join("fleet")
        .join(safe_path_segment(&run_id.0))
        .join(safe_path_segment(task_id))
        .join(safe_path_segment(worker_id))
        .join(safe_path_segment(filename));
    let abs_path = workspace.join(&rel_path);
    if let Some(parent) = abs_path.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("creating fleet artifact dir {}", parent.display()))?;
    }
    std::fs::write(&abs_path, contents)
        .with_context(|| format!("writing fleet artifact {}", abs_path.display()))?;
    let digest = Sha256::digest(contents);
    Ok(FleetArtifactRef {
        kind,
        path: rel_path,
        checksum: Some(format!("sha256:{digest:x}")),
        mime_type: mime_type.map(str::to_string),
        size_bytes: Some(contents.len() as u64),
    })
}

pub fn verify_task_result(
    workspace: &Path,
    task: &FleetTaskSpec,
    input: &FleetTaskVerificationInput,
) -> FleetTaskVerification {
    match &task.scorer {
        Some(FleetScorerSpec::ExitCode) => verify_exit_code(input.exit_code),
        Some(FleetScorerSpec::FileExists { path }) => verify_file_exists(workspace, path),
        Some(FleetScorerSpec::RegexMatch { path, pattern }) => {
            verify_regex_match(workspace, path, pattern)
        }
        Some(FleetScorerSpec::JsonPath { path, expression }) => {
            verify_json_path(workspace, path, expression)
        }
        Some(FleetScorerSpec::Command { command, .. }) => partial(
            format!("external scorer command configured: {command}"),
            "run the configured scorer command to finalize this receipt",
        ),
        Some(FleetScorerSpec::CodeWhaleVerifierPrompt { .. }) => partial(
            "CodeWhale verifier prompt configured",
            "run a verifier prompt pass to finalize this receipt",
        ),
        Some(FleetScorerSpec::Manual) => partial(
            "manual scorer configured",
            "manual verification is required to finalize this receipt",
        ),
        None if !has_verifiable_artifact(input) => partial(
            "no scorer configured and no verifiable artifacts recorded",
            "worker exited successfully but produced no verifiable output",
        ),
        None => partial(
            "no scorer configured",
            "task has artifacts but no deterministic scorer",
        ),
    }
}

pub fn record_verification_receipt(
    ledger: &FleetLedger,
    workspace: &Path,
    input: &FleetTaskVerificationInput,
    verification: FleetTaskVerification,
) -> Result<FleetReceipt> {
    let evidence = json!({
        "run_id": input.run_id.0.clone(),
        "task_id": input.task_id.clone(),
        "worker_id": input.worker_id.clone(),
        "result": verification.result.clone(),
        "failure_kind": verification.failure_kind.clone(),
        "score": verification.score.clone(),
        "evidence": verification.evidence.clone(),
        "artifacts": input.artifacts.clone(),
    });
    let bytes =
        serde_json::to_vec_pretty(&evidence).context("serializing fleet receipt evidence")?;
    let receipt_artifact = write_fleet_artifact_ref(
        workspace,
        &input.run_id,
        &input.task_id,
        &input.worker_id,
        FleetArtifactKind::Receipt,
        "verification-receipt.json",
        &bytes,
        Some("application/json"),
    )?;
    let mut artifacts = input.artifacts.clone();
    artifacts.push(receipt_artifact);
    let receipt = FleetReceipt {
        run_id: input.run_id.clone(),
        task_id: input.task_id.clone(),
        worker_id: input.worker_id.clone(),
        completed_at: timestamp(),
        result: verification.result,
        failure_kind: verification.failure_kind,
        artifacts,
        score: Some(verification.score),
        resolved_route: input.resolved_route.clone(),
    };
    ledger.record_receipt(receipt.clone())?;
    Ok(receipt)
}

fn validate_tags(task_id: &str, tags: &[String]) -> Result<()> {
    let mut seen = BTreeSet::new();
    for tag in tags {
        if tag.trim().is_empty() {
            bail!("fleet task {task_id} tag cannot be empty");
        }
        if !seen.insert(tag) {
            bail!("fleet task {task_id} has duplicate tag {tag}");
        }
    }
    Ok(())
}

fn validate_workspace_requirements(task: &FleetTaskSpec) -> Result<()> {
    let Some(workspace) = &task.workspace else {
        return Ok(());
    };
    let env = workspace.environment.as_ref();
    for name in env
        .into_iter()
        .flat_map(|env| env.required.iter().chain(env.allowlist.iter()))
    {
        if name.trim().is_empty() {
            bail!(
                "fleet task {} environment variable name cannot be empty",
                task.id
            );
        }
    }
    Ok(())
}

fn verify_exit_code(exit_code: Option<i32>) -> FleetTaskVerification {
    match exit_code {
        Some(0) => pass("exit_code=0"),
        Some(code) => fail(
            FleetTaskFailureKind::Task,
            0.0,
            format!("exit_code={code}"),
            "worker task exited unsuccessfully",
        ),
        None => fail(
            FleetTaskFailureKind::Transport,
            0.0,
            "missing exit code",
            "worker transport did not report a process result",
        ),
    }
}

fn verify_file_exists(workspace: &Path, path: &Path) -> FleetTaskVerification {
    let abs_path = resolve_workspace_path(workspace, path);
    if abs_path.is_file() {
        pass(format!("file exists: {}", path.display()))
    } else {
        fail(
            FleetTaskFailureKind::Task,
            0.0,
            format!("missing file: {}", path.display()),
            "expected artifact file was not produced",
        )
    }
}

fn verify_regex_match(workspace: &Path, path: &Path, pattern: &str) -> FleetTaskVerification {
    let regex = match Regex::new(pattern) {
        Ok(regex) => regex,
        Err(err) => {
            return fail(
                FleetTaskFailureKind::Verifier,
                0.0,
                format!("invalid regex: {err}"),
                "regex scorer could not be compiled",
            );
        }
    };
    let contents = match read_bounded_to_string(workspace, path) {
        Ok(contents) => contents,
        Err(err) => {
            return fail(
                err.failure_kind,
                0.0,
                err.evidence,
                "regex scorer could not read bounded evidence",
            );
        }
    };
    if regex.is_match(&contents) {
        pass(format!("regex matched {}: {pattern}", path.display()))
    } else {
        fail(
            FleetTaskFailureKind::Task,
            0.0,
            format!("regex did not match {}: {pattern}", path.display()),
            "worker output did not satisfy the regex scorer",
        )
    }
}

fn verify_json_path(workspace: &Path, path: &Path, expression: &str) -> FleetTaskVerification {
    let Some(segments) = json_path_segments(expression) else {
        return fail(
            FleetTaskFailureKind::Verifier,
            0.0,
            format!("unsupported JSON path expression: {expression}"),
            "json_path scorer supports $.field or .field paths",
        );
    };
    let contents = match read_bounded_to_string(workspace, path) {
        Ok(contents) => contents,
        Err(err) => {
            return fail(
                err.failure_kind,
                0.0,
                err.evidence,
                "json_path scorer could not read bounded evidence",
            );
        }
    };
    let value: Value = match serde_json::from_str(&contents) {
        Ok(value) => value,
        Err(err) => {
            return fail(
                FleetTaskFailureKind::Task,
                0.0,
                format!("invalid JSON in {}: {err}", path.display()),
                "worker artifact was not valid JSON",
            );
        }
    };
    match json_path_lookup(&value, &segments) {
        Some(found) if json_truthy(found) => pass(format!(
            "json_path matched {}: {expression}",
            path.display()
        )),
        _ => fail(
            FleetTaskFailureKind::Task,
            0.0,
            format!(
                "json_path missing or false in {}: {expression}",
                path.display()
            ),
            "worker JSON artifact did not satisfy the scorer",
        ),
    }
}

fn pass(evidence: impl Into<String>) -> FleetTaskVerification {
    let evidence = evidence.into();
    FleetTaskVerification {
        result: FleetTaskResult::Pass,
        failure_kind: None,
        score: FleetScore {
            value: 1.0,
            max: Some(1.0),
            notes: Some(evidence.clone()),
        },
        evidence: vec![evidence],
    }
}

fn partial(evidence: impl Into<String>, notes: impl Into<String>) -> FleetTaskVerification {
    let evidence = evidence.into();
    let notes = notes.into();
    FleetTaskVerification {
        result: FleetTaskResult::Partial,
        failure_kind: None,
        score: FleetScore {
            value: 0.5,
            max: Some(1.0),
            notes: Some(notes),
        },
        evidence: vec![evidence],
    }
}

fn fail(
    failure_kind: FleetTaskFailureKind,
    value: f64,
    evidence: impl Into<String>,
    notes: impl Into<String>,
) -> FleetTaskVerification {
    let evidence = evidence.into();
    FleetTaskVerification {
        result: FleetTaskResult::Fail,
        failure_kind: Some(failure_kind),
        score: FleetScore {
            value,
            max: Some(1.0),
            notes: Some(notes.into()),
        },
        evidence: vec![evidence],
    }
}

fn has_verifiable_artifact(input: &FleetTaskVerificationInput) -> bool {
    input.artifacts.iter().any(|artifact| {
        !matches!(
            artifact.kind,
            FleetArtifactKind::Log | FleetArtifactKind::Receipt
        )
    })
}

#[derive(Debug)]
struct EvidenceReadError {
    failure_kind: FleetTaskFailureKind,
    evidence: String,
}

fn read_bounded_to_string(
    workspace: &Path,
    path: &Path,
) -> std::result::Result<String, EvidenceReadError> {
    let abs_path = resolve_workspace_path(workspace, path);
    let metadata = std::fs::metadata(&abs_path).map_err(|err| EvidenceReadError {
        failure_kind: if err.kind() == std::io::ErrorKind::NotFound {
            FleetTaskFailureKind::Task
        } else {
            FleetTaskFailureKind::Verifier
        },
        evidence: format!("cannot read {}: {err}", path.display()),
    })?;
    if metadata.len() > MAX_SCORER_READ_BYTES {
        return Err(EvidenceReadError {
            failure_kind: FleetTaskFailureKind::Verifier,
            evidence: format!(
                "refusing to read oversized evidence {}: {} bytes",
                path.display(),
                metadata.len()
            ),
        });
    }
    std::fs::read_to_string(&abs_path).map_err(|err| EvidenceReadError {
        failure_kind: FleetTaskFailureKind::Verifier,
        evidence: format!("cannot decode {} as UTF-8: {err}", path.display()),
    })
}

fn resolve_workspace_path(workspace: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        workspace.join(path)
    }
}

fn json_path_segments(expression: &str) -> Option<Vec<&str>> {
    let trimmed = expression.trim();
    let path = trimmed
        .strip_prefix("$.")
        .or_else(|| trimmed.strip_prefix('.'))?;
    if path.is_empty() {
        return None;
    }
    let segments: Vec<_> = path.split('.').collect();
    if segments.iter().any(|segment| segment.is_empty()) {
        return None;
    }
    Some(segments)
}

fn json_path_lookup<'a>(value: &'a Value, segments: &[&str]) -> Option<&'a Value> {
    let mut current = value;
    for segment in segments {
        current = current.as_object()?.get(*segment)?;
    }
    Some(current)
}

fn json_truthy(value: &Value) -> bool {
    !matches!(value, Value::Null | Value::Bool(false))
}

fn timestamp() -> String {
    Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true)
}

fn safe_path_segment(value: &str) -> String {
    value
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.') {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use tempfile::TempDir;

    fn task(id: &str, scorer: Option<FleetScorerSpec>) -> FleetTaskSpec {
        FleetTaskSpec {
            id: id.to_string(),
            name: id.to_string(),
            description: None,
            objective: Some(format!("Verify {id}")),
            instructions: format!("do {id}"),
            worker: Some(FleetTaskWorkerProfile {
                agent_profile: None,
                role: Some("reviewer".to_string()),
                loadout: None,
                model_class: None,
                model: None,
                tool_profile: Some("read-only".to_string()),
                tools: vec!["git".to_string()],
                capabilities: vec!["rust".to_string()],
            }),
            workspace: Some(FleetWorkspaceRequirements {
                root: Some(PathBuf::from(".")),
                required_files: vec![PathBuf::from("Cargo.toml")],
                writable_paths: vec![PathBuf::from(".codewhale/fleet")],
                environment: Some(FleetEnvironmentRequirements {
                    required: vec!["PATH".to_string()],
                    allowlist: vec!["RUST_LOG".to_string()],
                }),
            }),
            input_files: vec![PathBuf::from("Cargo.toml")],
            context: vec!["fleet verifier test".to_string()],
            budget: Some(FleetTaskBudget {
                max_tokens: Some(4000),
                max_tool_calls: Some(12),
                max_seconds: Some(120),
            }),
            expected_artifacts: vec![FleetArtifactKind::Log, FleetArtifactKind::Receipt],
            scorer,
            retry_policy: Some(FleetRetryPolicy::default()),
            alert_policy: None,
            timeout_seconds: Some(120),
            tags: vec!["review".to_string()],
            metadata: BTreeMap::new(),
        }
    }

    #[test]
    fn fleet_task_spec_document_parses_multi_task_verified_shape() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("fleet-tasks.json");
        let doc = json!({
            "name": "release triage",
            "labels": {"milestone": "v0.8.60"},
            "tasks": [
                task("release-notes", Some(FleetScorerSpec::ExitCode)),
                task("risk-review", Some(FleetScorerSpec::Manual))
            ]
        });
        std::fs::write(&path, serde_json::to_string_pretty(&doc).unwrap()).unwrap();

        let parsed = load_task_spec_document(&path).unwrap();

        assert_eq!(parsed.name.as_deref(), Some("release triage"));
        assert_eq!(parsed.tasks.len(), 2);
        assert_eq!(
            parsed.tasks[0].objective.as_deref(),
            Some("Verify release-notes")
        );
        assert_eq!(
            parsed.tasks[0].worker.as_ref().unwrap().role.as_deref(),
            Some("reviewer")
        );
        assert_eq!(parsed.tasks[1].tags, vec!["review"]);
    }

    #[test]
    fn fleet_task_spec_document_parses_worker_profile_loadout_intent() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("fleet-profile-task.json");
        let doc = json!({
            "name": "profile loadout smoke",
            "tasks": [{
                "id": "review",
                "name": "review",
                "instructions": "review the patch",
                "worker": {
                    "profile": "adversarial_reviewer",
                    "role": "reviewer",
                    "loadout": "auto",
                    "model_class": "balanced",
                    "model": "deepseek-v4-pro",
                    "tool_profile": "read-only",
                    "tools": ["read_file", "grep_files"],
                    "capabilities": ["rust"]
                }
            }]
        });
        std::fs::write(&path, serde_json::to_string_pretty(&doc).unwrap()).unwrap();

        let parsed = load_task_spec_document(&path).unwrap();
        let worker = parsed.tasks[0].worker.as_ref().unwrap();

        assert_eq!(
            worker.agent_profile.as_deref(),
            Some("adversarial_reviewer")
        );
        assert_eq!(worker.role.as_deref(), Some("reviewer"));
        assert_eq!(worker.loadout.as_deref(), Some("auto"));
        assert_eq!(worker.model_class.as_deref(), Some("balanced"));
        assert_eq!(worker.model.as_deref(), Some("deepseek-v4-pro"));
        assert_eq!(worker.tool_profile.as_deref(), Some("read-only"));
    }

    #[test]
    fn fleet_task_spec_rejects_unsafe_worker_profile_intent_tokens() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("unsafe-profile-task.json");
        let doc = json!({
            "tasks": [{
                "id": "review",
                "name": "review",
                "instructions": "review the patch",
                "worker": {
                    "profile": "../secrets",
                    "loadout": "openrouter/deepseek",
                    "model_class": "",
                    "model": "deepseek/deepseek-v4-pro"
                }
            }]
        });
        std::fs::write(&path, serde_json::to_string_pretty(&doc).unwrap()).unwrap();

        let err = load_task_spec_document(&path).unwrap_err().to_string();

        assert!(
            err.contains("worker.agent_profile must be a simple token"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn fleet_task_spec_rejects_secret_like_worker_model() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("unsafe-worker-model.json");
        let doc = json!({
            "tasks": [{
                "id": "review",
                "name": "review",
                "instructions": "review the patch",
                "worker": {
                    "model": "deepseek-v4-pro api_key=secret"
                }
            }]
        });
        std::fs::write(&path, serde_json::to_string_pretty(&doc).unwrap()).unwrap();

        let err = load_task_spec_document(&path).unwrap_err().to_string();

        assert!(
            err.contains("worker.model must be a visible model id"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn fleet_task_spec_artifact_refs_are_bounded_paths() {
        let tmp = TempDir::new().unwrap();
        let artifact = write_fleet_artifact_ref(
            tmp.path(),
            &FleetRunId::from("run-1"),
            "task-a",
            "worker-1",
            FleetArtifactKind::Log,
            "worker.log",
            b"this is artifact content",
            Some("text/plain"),
        )
        .unwrap();

        let json = serde_json::to_string(&artifact).unwrap();
        assert!(!json.contains("this is artifact content"));
        assert!(json.contains("worker.log"));
        assert_eq!(artifact.size_bytes, Some(24));
        assert!(artifact.checksum.as_deref().unwrap().starts_with("sha256:"));
        assert!(tmp.path().join(&artifact.path).exists());
    }

    #[test]
    fn fleet_task_spec_scorers_record_pass_fail_partial_evidence() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("result.txt"), "status=ok\n").unwrap();
        std::fs::write(tmp.path().join("result.json"), r#"{"status":"ok"}"#).unwrap();
        let input = FleetTaskVerificationInput {
            run_id: FleetRunId::from("run-1"),
            task_id: "task-a".to_string(),
            worker_id: "worker-1".to_string(),
            exit_code: Some(0),
            artifacts: vec![],
            resolved_route: None,
        };

        let pass = verify_task_result(
            tmp.path(),
            &task("exit", Some(FleetScorerSpec::ExitCode)),
            &input,
        );
        assert_eq!(pass.result, FleetTaskResult::Pass);
        assert_eq!(pass.failure_kind, None);

        let regex = verify_task_result(
            tmp.path(),
            &task(
                "regex",
                Some(FleetScorerSpec::RegexMatch {
                    path: PathBuf::from("result.txt"),
                    pattern: "status=ok".to_string(),
                }),
            ),
            &input,
        );
        assert_eq!(regex.result, FleetTaskResult::Pass);

        let json_path = verify_task_result(
            tmp.path(),
            &task(
                "json",
                Some(FleetScorerSpec::JsonPath {
                    path: PathBuf::from("result.json"),
                    expression: "$.status".to_string(),
                }),
            ),
            &input,
        );
        assert_eq!(json_path.result, FleetTaskResult::Pass);

        let manual = verify_task_result(
            tmp.path(),
            &task("manual", Some(FleetScorerSpec::Manual)),
            &input,
        );
        assert_eq!(manual.result, FleetTaskResult::Partial);

        let no_scorer_empty = verify_task_result(tmp.path(), &task("unscored", None), &input);
        assert_eq!(no_scorer_empty.result, FleetTaskResult::Partial);
        assert!(
            no_scorer_empty
                .score
                .notes
                .as_deref()
                .unwrap_or_default()
                .contains("no verifiable output")
        );

        let failed = verify_task_result(
            tmp.path(),
            &task(
                "missing",
                Some(FleetScorerSpec::FileExists {
                    path: PathBuf::from("missing.txt"),
                }),
            ),
            &input,
        );
        assert_eq!(failed.result, FleetTaskResult::Fail);
        assert_eq!(failed.failure_kind, Some(FleetTaskFailureKind::Task));

        let verifier_failed = verify_task_result(
            tmp.path(),
            &task(
                "bad-regex",
                Some(FleetScorerSpec::RegexMatch {
                    path: PathBuf::from("result.txt"),
                    pattern: "[".to_string(),
                }),
            ),
            &input,
        );
        assert_eq!(verifier_failed.result, FleetTaskResult::Fail);
        assert_eq!(
            verifier_failed.failure_kind,
            Some(FleetTaskFailureKind::Verifier)
        );
    }

    #[test]
    fn fleet_task_spec_receipt_records_artifacts_scores_and_failure_kind() {
        let tmp = TempDir::new().unwrap();
        let ledger = FleetLedger::open(tmp.path()).unwrap();
        let log = write_fleet_artifact_ref(
            tmp.path(),
            &FleetRunId::from("run-1"),
            "task-a",
            "worker-1",
            FleetArtifactKind::Log,
            "worker.log",
            b"exit_code=1",
            Some("text/plain"),
        )
        .unwrap();
        let input = FleetTaskVerificationInput {
            run_id: FleetRunId::from("run-1"),
            task_id: "task-a".to_string(),
            worker_id: "worker-1".to_string(),
            exit_code: Some(1),
            artifacts: vec![log],
            resolved_route: None,
        };
        let verification = verify_task_result(
            tmp.path(),
            &task("task-a", Some(FleetScorerSpec::ExitCode)),
            &input,
        );

        let receipt =
            record_verification_receipt(&ledger, tmp.path(), &input, verification).unwrap();

        assert_eq!(receipt.result, FleetTaskResult::Fail);
        assert_eq!(receipt.failure_kind, Some(FleetTaskFailureKind::Task));
        assert_eq!(receipt.artifacts.len(), 2);
        assert!(matches!(
            receipt.artifacts.last().unwrap().kind,
            FleetArtifactKind::Receipt
        ));
        let state = ledger.rebuild_state().unwrap();
        assert_eq!(
            state.receipts["run-1:task-a"].failure_kind,
            Some(FleetTaskFailureKind::Task)
        );
    }
}