tandem-automation 0.7.2

Automation model and execution primitives for Tandem
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
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ExecutionProfile {
    #[default]
    Strict,
    Guided,
    Yolo,
}

impl ExecutionProfile {
    pub fn as_str(self) -> &'static str {
        match self {
            ExecutionProfile::Strict => "strict",
            ExecutionProfile::Guided => "guided",
            ExecutionProfile::Yolo => "yolo",
        }
    }

    pub fn allows_validation_warning(self) -> bool {
        matches!(self, ExecutionProfile::Guided | ExecutionProfile::Yolo)
    }

    pub fn allows_experimental_continue(self) -> bool {
        matches!(self, ExecutionProfile::Yolo)
    }

    pub fn repair_budget_multiplier(self) -> f32 {
        match self {
            ExecutionProfile::Strict => 1.0,
            ExecutionProfile::Guided => 1.5,
            ExecutionProfile::Yolo => 2.0,
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "snake_case")]
pub enum ValidatorClass {
    MissingRequiredSection,
    WeakMarkdownStructure,
    MissingOptionalEvidence,
    ArtifactWordCountBelowMinimum,
    MissingNonconsumedWorkspaceFiles,
    RequiredSourcePathsNotRead,
    MissingRequiredArtifactPath,
    ValidatorKindSpecificSoftCheck,
    RepairBudgetExhausted,
    UnauthorizedWorkspace,
    SecretAccessDenied,
    DestructiveActionRequiresApproval,
    ExternalPublishRequiresApproval,
    TenantPolicyDenied,
    ToolUnauthorized,
    BudgetExceeded,
    KillSwitchEngaged,
    EngineLeaseExpired,
    InvalidApiToken,
    DeterministicVerificationFailed,
}

impl ValidatorClass {
    pub fn is_critical(self) -> bool {
        matches!(
            self,
            ValidatorClass::UnauthorizedWorkspace
                | ValidatorClass::SecretAccessDenied
                | ValidatorClass::DestructiveActionRequiresApproval
                | ValidatorClass::ExternalPublishRequiresApproval
                | ValidatorClass::TenantPolicyDenied
                | ValidatorClass::ToolUnauthorized
                | ValidatorClass::BudgetExceeded
                | ValidatorClass::KillSwitchEngaged
                | ValidatorClass::EngineLeaseExpired
                | ValidatorClass::InvalidApiToken
                | ValidatorClass::DeterministicVerificationFailed
        )
    }

    pub fn is_relaxable_in(self, profile: ExecutionProfile) -> bool {
        if self.is_critical() {
            return false;
        }
        match (self, profile) {
            (_, ExecutionProfile::Strict) => false,
            (
                ValidatorClass::MissingRequiredSection
                | ValidatorClass::WeakMarkdownStructure
                | ValidatorClass::MissingOptionalEvidence
                | ValidatorClass::ArtifactWordCountBelowMinimum
                | ValidatorClass::MissingNonconsumedWorkspaceFiles,
                ExecutionProfile::Guided | ExecutionProfile::Yolo,
            ) => true,
            (
                ValidatorClass::MissingRequiredArtifactPath
                | ValidatorClass::ValidatorKindSpecificSoftCheck
                | ValidatorClass::RepairBudgetExhausted,
                ExecutionProfile::Yolo,
            ) => true,
            _ => false,
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            ValidatorClass::MissingRequiredSection => "missing_required_section",
            ValidatorClass::WeakMarkdownStructure => "weak_markdown_structure",
            ValidatorClass::MissingOptionalEvidence => "missing_optional_evidence",
            ValidatorClass::ArtifactWordCountBelowMinimum => "artifact_word_count_below_minimum",
            ValidatorClass::MissingNonconsumedWorkspaceFiles => {
                "missing_nonconsumed_workspace_files"
            }
            ValidatorClass::RequiredSourcePathsNotRead => "required_source_paths_not_read",
            ValidatorClass::MissingRequiredArtifactPath => "missing_required_artifact_path",
            ValidatorClass::ValidatorKindSpecificSoftCheck => "validator_kind_specific_soft_check",
            ValidatorClass::RepairBudgetExhausted => "repair_budget_exhausted",
            ValidatorClass::UnauthorizedWorkspace => "unauthorized_workspace",
            ValidatorClass::SecretAccessDenied => "secret_access_denied",
            ValidatorClass::DestructiveActionRequiresApproval => {
                "destructive_action_requires_approval"
            }
            ValidatorClass::ExternalPublishRequiresApproval => "external_publish_requires_approval",
            ValidatorClass::TenantPolicyDenied => "tenant_policy_denied",
            ValidatorClass::ToolUnauthorized => "tool_unauthorized",
            ValidatorClass::BudgetExceeded => "budget_exceeded",
            ValidatorClass::KillSwitchEngaged => "kill_switch_engaged",
            ValidatorClass::EngineLeaseExpired => "engine_lease_expired",
            ValidatorClass::InvalidApiToken => "invalid_api_token",
            ValidatorClass::DeterministicVerificationFailed => "deterministic_verification_failed",
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum ValidationOutcome {
    Passed,
    Warning,
    Experimental,
    Blocked,
}

impl ValidationOutcome {
    pub fn as_str(self) -> &'static str {
        match self {
            ValidationOutcome::Passed => "passed",
            ValidationOutcome::Warning => "warning",
            ValidationOutcome::Experimental => "experimental",
            ValidationOutcome::Blocked => "blocked",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RelaxedValidatorClass {
    pub class: ValidatorClass,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    pub original_outcome: ValidationOutcome,
    pub effective_outcome: ValidationOutcome,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProfileValidationDecision {
    pub profile: ExecutionProfile,
    pub original_outcome: ValidationOutcome,
    pub effective_outcome: ValidationOutcome,
    pub should_block: bool,
    pub experimental: bool,
    pub relaxed_classes: Vec<RelaxedValidatorClass>,
}

impl ProfileValidationDecision {
    pub fn passthrough(profile: ExecutionProfile, outcome: ValidationOutcome) -> Self {
        ProfileValidationDecision {
            profile,
            original_outcome: outcome,
            effective_outcome: outcome,
            should_block: matches!(outcome, ValidationOutcome::Blocked),
            experimental: false,
            relaxed_classes: Vec::new(),
        }
    }
}

/// Single chokepoint: given a non-pass validator outcome and the validator
/// classes that triggered it, decide what the run/node should actually see
/// under the active profile. All profile-driven downgrades MUST flow through
/// this function — see `docs/internal/execution-profiles/PROPOSAL.md`
/// "Executor Chokepoint Invariant".
pub fn decide_profile_validation(
    profile: ExecutionProfile,
    original_outcome: ValidationOutcome,
    classes: &[(ValidatorClass, Option<String>)],
    tenant_relaxation_denylist: &[ValidatorClass],
) -> ProfileValidationDecision {
    if matches!(
        original_outcome,
        ValidationOutcome::Passed | ValidationOutcome::Warning
    ) {
        return ProfileValidationDecision::passthrough(profile, original_outcome);
    }

    if classes.is_empty() {
        return ProfileValidationDecision::passthrough(profile, original_outcome);
    }

    let any_critical = classes.iter().any(|(class, _)| class.is_critical());
    if any_critical {
        return ProfileValidationDecision::passthrough(profile, ValidationOutcome::Blocked);
    }

    let any_tenant_denied = classes
        .iter()
        .any(|(class, _)| tenant_relaxation_denylist.contains(class));
    if any_tenant_denied {
        return ProfileValidationDecision::passthrough(profile, ValidationOutcome::Blocked);
    }

    let all_relaxable = classes
        .iter()
        .all(|(class, _)| class.is_relaxable_in(profile));
    if !all_relaxable {
        return ProfileValidationDecision::passthrough(profile, ValidationOutcome::Blocked);
    }

    let effective_outcome = match profile {
        ExecutionProfile::Strict => ValidationOutcome::Blocked,
        ExecutionProfile::Guided => ValidationOutcome::Warning,
        ExecutionProfile::Yolo => ValidationOutcome::Experimental,
    };

    let relaxed_classes = classes
        .iter()
        .map(|(class, detail)| RelaxedValidatorClass {
            class: *class,
            detail: detail.clone(),
            original_outcome,
            effective_outcome,
        })
        .collect();

    ProfileValidationDecision {
        profile,
        original_outcome,
        effective_outcome,
        should_block: matches!(effective_outcome, ValidationOutcome::Blocked),
        experimental: matches!(effective_outcome, ValidationOutcome::Experimental),
        relaxed_classes,
    }
}

/// Marks an output as carrying experimental input taint when one or more
/// upstream node outputs are themselves experimental. Pure metadata: writes
/// `artifact_validation.experimental = true` and
/// `artifact_validation.tainted_inputs = [upstream_node_id, ...]` without
/// touching `output.status`. Returns `true` when taint was applied.
///
/// Rationale (PROPOSAL.md "Experimental Propagation"): a downstream node's
/// own validation may pass even when its inputs were accepted under a
/// relaxed profile. Without taint propagation the run-level
/// "experimental" flag would silently disappear at the first cleanly-passing
/// downstream step. Propagating taint keeps receipts honest and lets
/// `run_completed` consumers filter experimental runs.
pub fn propagate_experimental_input_taint<'a, I>(output: &mut Value, upstream_outputs: I) -> bool
where
    I: IntoIterator<Item = (&'a str, &'a Value)>,
{
    let tainted: Vec<String> = upstream_outputs
        .into_iter()
        .filter_map(|(node_id, upstream_output)| {
            let is_experimental = upstream_output
                .get("artifact_validation")
                .and_then(|av| av.get("experimental"))
                .and_then(Value::as_bool)
                .unwrap_or(false);
            if is_experimental {
                Some(node_id.to_string())
            } else {
                None
            }
        })
        .collect();
    if tainted.is_empty() {
        return false;
    }

    let object = match output.as_object_mut() {
        Some(map) => map,
        None => return false,
    };
    if !object.contains_key("artifact_validation") {
        object.insert(
            "artifact_validation".to_string(),
            Value::Object(serde_json::Map::new()),
        );
    }
    let validation = object
        .get_mut("artifact_validation")
        .and_then(Value::as_object_mut)
        .expect("artifact_validation present (just inserted if missing)");

    let already_experimental = validation
        .get("experimental")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    validation.insert("experimental".to_string(), json!(true));
    validation
        .entry("tainted_inputs".to_string())
        .or_insert_with(|| Value::Array(Vec::new()));
    if let Some(arr) = validation
        .get_mut("tainted_inputs")
        .and_then(Value::as_array_mut)
    {
        for node_id in tainted {
            let already_listed = arr.iter().any(|value| value.as_str() == Some(&node_id));
            if !already_listed {
                arr.push(json!(node_id));
            }
        }
    }
    !already_experimental
}

/// Parses a string into an `ExecutionProfile`, accepting the same
/// snake_case wire form as serde plus a few common aliases. Trims and
/// lowercases the input. Empty strings and unknown values return `None`.
///
/// Used for parsing operator-supplied tenant-default settings (e.g.
/// `TANDEM_DEFAULT_EXECUTION_PROFILE` env var) without forcing operators
/// to remember exact casing.
pub fn parse_execution_profile_str(raw: &str) -> Option<ExecutionProfile> {
    let normalized = raw.trim().to_ascii_lowercase();
    match normalized.as_str() {
        "strict" => Some(ExecutionProfile::Strict),
        "guided" | "assisted" | "warn" => Some(ExecutionProfile::Guided),
        "yolo" | "exploratory" | "lenient" | "permissive" => Some(ExecutionProfile::Yolo),
        _ => None,
    }
}

/// Reads the tenant-level default execution profile from the
/// `TANDEM_DEFAULT_EXECUTION_PROFILE` environment variable. Returns
/// `None` when the variable is unset, empty, or names an unknown value
/// (operators get safe Strict fallback rather than a panic on typos).
///
/// Run-creation paths consult this before falling back to the system
/// default of Guided, so the precedence chain is:
///   run override → workflow policy → tenant default → Guided.
pub fn tenant_default_execution_profile_from_env() -> Option<ExecutionProfile> {
    std::env::var("TANDEM_DEFAULT_EXECUTION_PROFILE")
        .ok()
        .as_deref()
        .and_then(parse_execution_profile_str)
}

/// Parses a comma-separated list of validator class names into the
/// `ValidatorClass` taxonomy. Trims and lowercases each entry; unknown
/// entries are silently skipped (operators get a safe under-restriction
/// fallback rather than a panic on typos). Recognized inputs match the
/// canonical `as_str` form, e.g. `missing_required_section`,
/// `weak_markdown_structure`, `repair_budget_exhausted`.
pub fn parse_validator_class_list(raw: &str) -> Vec<ValidatorClass> {
    raw.split(',')
        .filter_map(|item| {
            let normalized = item.trim().to_ascii_lowercase();
            match normalized.as_str() {
                "missing_required_section" => Some(ValidatorClass::MissingRequiredSection),
                "weak_markdown_structure" => Some(ValidatorClass::WeakMarkdownStructure),
                "missing_optional_evidence" => Some(ValidatorClass::MissingOptionalEvidence),
                "artifact_word_count_below_minimum" => {
                    Some(ValidatorClass::ArtifactWordCountBelowMinimum)
                }
                "missing_nonconsumed_workspace_files" => {
                    Some(ValidatorClass::MissingNonconsumedWorkspaceFiles)
                }
                "missing_required_artifact_path" => {
                    Some(ValidatorClass::MissingRequiredArtifactPath)
                }
                "validator_kind_specific_soft_check" => {
                    Some(ValidatorClass::ValidatorKindSpecificSoftCheck)
                }
                "repair_budget_exhausted" => Some(ValidatorClass::RepairBudgetExhausted),
                _ => None,
            }
        })
        .collect()
}

/// Reads the tenant-level relaxation denylist from the
/// `TANDEM_RELAXATION_DENYLIST` environment variable. Returns the list
/// of `ValidatorClass` values that should NEVER be relaxed under any
/// profile, even when the chokepoint would otherwise allow them.
///
/// Operators set this to insist that specific validator classes always
/// block (e.g. `missing_required_artifact_path,repair_budget_exhausted`)
/// while still benefiting from the rest of the relaxation set under
/// Guided/Lenient. Empty/unset returns an empty Vec — no classes are
/// denied beyond the always-critical hard set.
pub fn tenant_relaxation_denylist_from_env() -> Vec<ValidatorClass> {
    std::env::var("TANDEM_RELAXATION_DENYLIST")
        .ok()
        .as_deref()
        .map(parse_validator_class_list)
        .unwrap_or_default()
}

/// Human-applied accept/reject signal on a relaxed (Guided/Lenient) artifact.
///
/// Together with `relaxed_validator_classes`, this is the input to the
/// graduation loop: classes whose accept-rate is high enough over a rolling
/// window can be promoted from "experimental" to "supported", or moved
/// from Lenient into Guided. `Unmarked` is the default — it represents
/// "no human has reviewed this yet" rather than a neutral verdict, and
/// must not be confused with `Accepted`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum HumanDisposition {
    #[default]
    Unmarked,
    Accepted,
    Rejected,
    ReRanStrict,
}

impl HumanDisposition {
    pub fn as_str(self) -> &'static str {
        match self {
            HumanDisposition::Unmarked => "unmarked",
            HumanDisposition::Accepted => "accepted",
            HumanDisposition::Rejected => "rejected",
            HumanDisposition::ReRanStrict => "re_ran_strict",
        }
    }
}

/// Parses a human-disposition string from API/UI input. Accepts the canonical
/// snake_case form plus a few operator-friendly aliases (`approve`/`reject`/
/// `rerun`). Whitespace and case are normalized. Unknown strings return
/// `None` — callers should reject those rather than silently coercing.
pub fn parse_human_disposition_str(raw: &str) -> Option<HumanDisposition> {
    let normalized = raw.trim().to_ascii_lowercase();
    match normalized.as_str() {
        "unmarked" | "" | "none" | "clear" => Some(HumanDisposition::Unmarked),
        "accepted" | "accept" | "approve" | "approved" | "ok" => Some(HumanDisposition::Accepted),
        "rejected" | "reject" | "deny" | "denied" | "fail" => Some(HumanDisposition::Rejected),
        "re_ran_strict" | "rerun_strict" | "rerun-strict" | "rerun" | "re_ran" => {
            Some(HumanDisposition::ReRanStrict)
        }
        _ => None,
    }
}

/// Writes `human_disposition` into `output["artifact_validation"]`. Returns
/// `true` when the value was newly set or changed; `false` when the key
/// already held the same disposition. Creates an empty `artifact_validation`
/// object if one is not yet present, so dispositions can be set on outputs
/// that did not go through the relaxation chokepoint (e.g. Strict runs the
/// human still wants to comment on).
pub fn set_human_disposition_on_output(output: &mut Value, disposition: HumanDisposition) -> bool {
    let object = match output.as_object_mut() {
        Some(map) => map,
        None => return false,
    };
    if !object.contains_key("artifact_validation") {
        object.insert(
            "artifact_validation".to_string(),
            Value::Object(serde_json::Map::new()),
        );
    }
    let validation = match object
        .get_mut("artifact_validation")
        .and_then(Value::as_object_mut)
    {
        Some(map) => map,
        None => return false,
    };
    let previous = validation
        .get("human_disposition")
        .and_then(Value::as_str)
        .map(str::to_string);
    let next = disposition.as_str().to_string();
    if previous.as_deref() == Some(next.as_str()) {
        return false;
    }
    validation.insert("human_disposition".to_string(), json!(next));
    true
}

/// Per-class accept/reject counts for graduation telemetry.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct DispositionCounts {
    #[serde(default)]
    pub accepted: u64,
    #[serde(default)]
    pub rejected: u64,
    #[serde(default)]
    pub re_ran_strict: u64,
    #[serde(default)]
    pub unmarked: u64,
}

impl DispositionCounts {
    pub fn record(&mut self, disposition: HumanDisposition) {
        match disposition {
            HumanDisposition::Accepted => self.accepted = self.accepted.saturating_add(1),
            HumanDisposition::Rejected => self.rejected = self.rejected.saturating_add(1),
            HumanDisposition::ReRanStrict => {
                self.re_ran_strict = self.re_ran_strict.saturating_add(1)
            }
            HumanDisposition::Unmarked => self.unmarked = self.unmarked.saturating_add(1),
        }
    }

    pub fn total(&self) -> u64 {
        self.accepted
            .saturating_add(self.rejected)
            .saturating_add(self.re_ran_strict)
            .saturating_add(self.unmarked)
    }

    /// Accept rate over reviewed dispositions (excludes `unmarked`). Returns
    /// `None` when no humans have reviewed any outputs in the bucket — the
    /// dashboard should render that as "insufficient signal" rather than 0%.
    pub fn accept_rate(&self) -> Option<f32> {
        let reviewed = self
            .accepted
            .saturating_add(self.rejected)
            .saturating_add(self.re_ran_strict);
        if reviewed == 0 {
            return None;
        }
        Some(self.accepted as f32 / reviewed as f32)
    }
}

/// Aggregate result of walking a slice of run records: per-`ValidatorClass`
/// disposition counts plus a few totals. Intended for the read-only
/// graduation summary endpoint and any future per-class graduation
/// dashboard. Pure — does not touch state.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct ValidatorClassDispositionSummary {
    #[serde(default)]
    pub total_outputs_scanned: u64,
    #[serde(default)]
    pub total_relaxed_outputs: u64,
    #[serde(default)]
    pub by_class: std::collections::BTreeMap<ValidatorClass, DispositionCounts>,
}

/// Walk a slice of node outputs and attribute each output's
/// `human_disposition` (defaulting to `unmarked`) to **every** validator
/// class listed under `relaxed_validator_classes` for that output. Outputs
/// without `relaxed_validator_classes` are not included — they were not
/// relaxed under a profile and therefore have nothing to graduate.
///
/// Pure — does not touch state. The HTTP handler that surfaces this
/// aggregate is responsible for filtering runs by time window and
/// flattening the per-run `node_outputs` into the iterator.
pub fn aggregate_human_dispositions_by_class<'a, I>(outputs: I) -> ValidatorClassDispositionSummary
where
    I: IntoIterator<Item = &'a Value>,
{
    let mut summary = ValidatorClassDispositionSummary::default();
    for output in outputs {
        summary.total_outputs_scanned = summary.total_outputs_scanned.saturating_add(1);
        let validation = match output.get("artifact_validation") {
            Some(value) => value,
            None => continue,
        };
        let relaxed = match validation
            .get("relaxed_validator_classes")
            .and_then(Value::as_array)
        {
            Some(value) if !value.is_empty() => value,
            _ => continue,
        };
        summary.total_relaxed_outputs = summary.total_relaxed_outputs.saturating_add(1);
        let disposition = validation
            .get("human_disposition")
            .and_then(Value::as_str)
            .and_then(parse_human_disposition_str)
            .unwrap_or(HumanDisposition::Unmarked);
        for entry in relaxed {
            let class_name = entry
                .as_str()
                .or_else(|| entry.get("class").and_then(Value::as_str));
            let class_name = match class_name {
                Some(name) => name,
                None => continue,
            };
            if let Some(class) = parse_validator_class_list(class_name).into_iter().next() {
                summary
                    .by_class
                    .entry(class)
                    .or_default()
                    .record(disposition);
            }
        }
    }
    summary
}

/// Profile-aware repair budget multiplier, bounded above by global caps in
/// `AutomationExecutionPolicy`. Returns the effective number of repair
/// attempts allowed for the given declared budget under `profile`.
pub fn effective_repair_budget(declared: u32, profile: ExecutionProfile) -> u32 {
    let multiplier = profile.repair_budget_multiplier();
    let scaled = (declared as f32 * multiplier).ceil();
    scaled.clamp(0.0, u32::MAX as f32) as u32
}

/// Classifies a validator's `unmet_requirements` string into a
/// `ValidatorClass`. Returns `None` for strings that have not yet been
/// taxonomized — those default to "blocking, never relaxable" so behavior
/// stays Strict-equivalent until the class is explicitly added.
pub fn classify_unmet_requirement(raw: &str) -> Option<ValidatorClass> {
    let key = raw
        .split([':', '|'])
        .next()
        .map(str::trim)
        .unwrap_or(raw)
        .trim();
    match key {
        "missing_required_section" | "missing_section" | "section_missing" => {
            Some(ValidatorClass::MissingRequiredSection)
        }
        "weak_markdown_structure"
        | "weak_structure"
        | "weak_markdown"
        | "markdown_structure_missing" => Some(ValidatorClass::WeakMarkdownStructure),
        "missing_optional_evidence"
        | "missing_evidence_optional"
        | "editorial_substance_missing" => Some(ValidatorClass::MissingOptionalEvidence),
        "artifact_word_count_below_minimum" | "artifact_too_short" => {
            Some(ValidatorClass::ArtifactWordCountBelowMinimum)
        }
        "missing_nonconsumed_workspace_files" | "missing_optional_workspace_files" => {
            Some(ValidatorClass::MissingNonconsumedWorkspaceFiles)
        }
        "required_source_paths_not_read" | "required_source_read_paths_not_read" => {
            Some(ValidatorClass::RequiredSourcePathsNotRead)
        }
        "missing_required_artifact_path" | "missing_artifact_path" => {
            Some(ValidatorClass::MissingRequiredArtifactPath)
        }
        "validator_kind_specific_soft_check" | "soft_validator_check" => {
            Some(ValidatorClass::ValidatorKindSpecificSoftCheck)
        }
        "repair_budget_exhausted" => Some(ValidatorClass::RepairBudgetExhausted),
        "unauthorized_workspace" | "workspace_unauthorized" => {
            Some(ValidatorClass::UnauthorizedWorkspace)
        }
        "secret_access_denied" => Some(ValidatorClass::SecretAccessDenied),
        "destructive_action_requires_approval" | "destructive_requires_approval" => {
            Some(ValidatorClass::DestructiveActionRequiresApproval)
        }
        "external_publish_requires_approval" => {
            Some(ValidatorClass::ExternalPublishRequiresApproval)
        }
        "tenant_policy_denied" | "policy_denied" => Some(ValidatorClass::TenantPolicyDenied),
        "tool_unauthorized" | "unauthorized_tool" => Some(ValidatorClass::ToolUnauthorized),
        "budget_exceeded" => Some(ValidatorClass::BudgetExceeded),
        "kill_switch_engaged" => Some(ValidatorClass::KillSwitchEngaged),
        "engine_lease_expired" => Some(ValidatorClass::EngineLeaseExpired),
        "invalid_api_token" => Some(ValidatorClass::InvalidApiToken),
        "deterministic_verification_failed" | "code_patch_apply_failed" => {
            Some(ValidatorClass::DeterministicVerificationFailed)
        }
        _ => None,
    }
}

/// Augments a node `output` JSON value with profile-aware relaxation
/// metadata AND rewrites the executor's blocking signals when the active
/// profile would relax all of its unmet requirements.
///
/// On a successful relaxation, this function writes telemetry into
/// `output["artifact_validation"]` (`relaxed_validator_classes`,
/// `effective_outcome`, `original_validator_outcome`, `execution_profile`,
/// optional `requested_execution_profile`, `experimental`, and
/// `original_status`) AND downgrades the executor-facing fields so the run
/// continues:
///
/// - `output["status"]` becomes `completed_with_warnings` (Guided) or
///   `completed` (Lenient; experimental-flagged via `artifact_validation`).
/// - `output["failure_kind"]` is cleared if it was validation-related.
/// - `output["blocked_reason"]` is cleared.
/// - `artifact_validation.warning_count` is set to the count of relaxed
///   classes so `automation_output_has_warnings` returns true.
///
/// Strict runs and runs whose unmet requirements include any critical or
/// not-yet-classified class are returned untouched.
///
/// Returns `true` when relaxation occurred.
pub fn augment_output_with_profile_relaxation(
    output: &mut Value,
    profile: ExecutionProfile,
    requested_profile: Option<ExecutionProfile>,
    tenant_relaxation_denylist: &[ValidatorClass],
) -> bool {
    let object = match output.as_object_mut() {
        Some(map) => map,
        None => return false,
    };
    let raw_unmet = object
        .get("artifact_validation")
        .and_then(|value| value.get("unmet_requirements"))
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_default();
    if raw_unmet.is_empty() {
        return false;
    }
    let mut classes: Vec<(ValidatorClass, Option<String>)> = Vec::new();
    let mut had_unclassified = false;
    for entry in &raw_unmet {
        let raw = match entry.as_str() {
            Some(value) => value.trim(),
            None => continue,
        };
        match classify_unmet_requirement(raw) {
            Some(class) => {
                let detail = raw
                    .split_once([':', '|'])
                    .map(|(_, tail)| tail)
                    .map(|tail| tail.trim().to_string())
                    .filter(|value| !value.is_empty());
                classes.push((class, detail));
            }
            None => {
                had_unclassified = true;
            }
        }
    }

    let original_outcome = ValidationOutcome::Blocked;
    let decision = decide_profile_validation(
        profile,
        original_outcome,
        &classes,
        tenant_relaxation_denylist,
    );
    let augmented = !decision.relaxed_classes.is_empty()
        && !matches!(decision.effective_outcome, ValidationOutcome::Blocked);
    if !augmented {
        return false;
    }
    if had_unclassified {
        // Conservative: if any unmet requirement is not yet classified, keep
        // Strict-equivalent behavior even when others would relax.
        return false;
    }

    let original_status = object
        .get("status")
        .and_then(Value::as_str)
        .map(str::to_string);
    let original_failure_kind = object
        .get("failure_kind")
        .and_then(Value::as_str)
        .map(str::to_string);

    // Downgrade executor-facing blocking signals so the run continues.
    let new_status = match decision.effective_outcome {
        ValidationOutcome::Warning => "completed_with_warnings",
        ValidationOutcome::Experimental => "completed",
        // Defensive: by construction `effective_outcome` is non-blocking here.
        ValidationOutcome::Passed | ValidationOutcome::Blocked => "completed",
    };
    object.insert("status".to_string(), json!(new_status));
    let validation_failure_kinds = matches!(
        original_failure_kind.as_deref(),
        Some("validation_error") | Some("verification_failed") | Some("artifact_rejected")
    );
    if validation_failure_kinds {
        object.insert("failure_kind".to_string(), Value::Null);
    }
    if matches!(
        object.get("blocked_reason").and_then(Value::as_str),
        Some(text) if !text.is_empty()
    ) {
        object.insert("blocked_reason".to_string(), Value::Null);
    }

    let validation = object
        .get_mut("artifact_validation")
        .and_then(Value::as_object_mut)
        .expect("artifact_validation present (checked above)");
    validation.insert(
        "relaxed_validator_classes".to_string(),
        serde_json::to_value(&decision.relaxed_classes).unwrap_or(Value::Null),
    );
    validation.insert(
        "effective_outcome".to_string(),
        json!(decision.effective_outcome.as_str()),
    );
    validation.insert(
        "original_validator_outcome".to_string(),
        json!(original_outcome.as_str()),
    );
    validation.insert("execution_profile".to_string(), json!(profile.as_str()));
    if let Some(req) = requested_profile {
        validation.insert(
            "requested_execution_profile".to_string(),
            json!(req.as_str()),
        );
    }
    if decision.experimental {
        validation.insert("experimental".to_string(), json!(true));
    }
    if let Some(prev) = original_status {
        validation.insert("original_status".to_string(), json!(prev));
    }
    if let Some(prev) = original_failure_kind {
        validation.insert("original_failure_kind".to_string(), json!(prev));
    }
    let warning_count = decision.relaxed_classes.len() as u64;
    validation.insert("warning_count".to_string(), json!(warning_count));
    true
}