talos-core 0.10.0

Foundation types, core traits, and error definitions
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
//! Storage-neutral completion claims and independent evaluation state.
//!
//! This module defines the contract between an executor and the evaluator.  A claim is an
//! assertion that work is ready to inspect; it is never a completion authority.  Reports are
//! bound to the exact subject revision captured by the claim and derive their aggregate verdict
//! from criterion-level results.

use crate::work::{WorkIdentity, WorkKind};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use thiserror::Error;
use uuid::Uuid;

/// The exact workspace/content revision inspected by an evaluator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct WorkspaceRevision {
    /// Stable identity of the workspace/content subject.
    pub id: Uuid,
    /// Monotonic content revision (for example a commit plus dirty-tree digest revision).
    pub revision: u64,
}

/// Exact Mission, Goal and workspace subject to which a claim or report applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct EvaluationSubject {
    /// Mission identity and revision.
    pub mission: WorkIdentity,
    /// Goal identity and revision.
    pub goal: WorkIdentity,
    /// Workspace/content identity and revision.
    pub workspace: WorkspaceRevision,
}

impl EvaluationSubject {
    /// Validate that the subject contains the required Mission and Goal roles.
    pub fn validate(&self) -> Result<(), EvaluationError> {
        if self.mission.kind != WorkKind::Mission {
            return Err(EvaluationError::InvalidSubject(
                "mission identity is not a Mission",
            ));
        }
        if self.goal.kind != WorkKind::Goal {
            return Err(EvaluationError::InvalidSubject(
                "goal identity is not a Goal",
            ));
        }
        Ok(())
    }

    /// Return whether another subject is exactly the same evaluation subject.
    #[must_use]
    pub fn matches(&self, current: &Self) -> bool {
        self == current
    }
}

/// A stable reference to an artifact changed or inspected by an evaluation.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct ArtifactRef {
    /// Stable artifact identity.
    pub id: Uuid,
    /// Human-readable locator, such as a path or commit object.
    pub locator: String,
}

/// A stable reference to evidence.  Evidence is referential and never a verdict authority.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct EvidenceRef {
    /// Stable evidence identity.
    pub id: Uuid,
    /// Producer or record kind (for example `validation`).
    pub kind: String,
}

/// The semantic kind of an acceptance criterion.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CriterionKind {
    /// A user-visible behavior requirement.
    Behavior,
    /// A technical/API contract requirement.
    Technical,
    /// A machine-verifiable validation requirement.
    Validation,
    /// A documentation or operational requirement.
    Documentation,
    /// An explicitly named project-specific criterion.
    Custom(String),
}

/// One typed acceptance criterion for a Goal.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct AcceptanceCriterion {
    /// Stable criterion identity.
    pub id: Uuid,
    /// Criterion category.
    pub kind: CriterionKind,
    /// Short requirement text.
    pub statement: String,
    /// Required criteria determine the aggregate PASS/FAIL result.
    pub required: bool,
}

/// A completion assertion submitted by an executor.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct CompletionClaim {
    /// Stable claim identity.
    pub id: Uuid,
    /// Exact subject revision being claimed.
    pub subject: EvaluationSubject,
    /// Immutable acceptance criteria snapshot.
    pub criteria: Vec<AcceptanceCriterion>,
    /// Artifacts the executor says were changed.
    pub changed_artifacts: Vec<ArtifactRef>,
    /// Evidence references offered as hints to the evaluator.
    pub claimed_evidence: Vec<EvidenceRef>,
    /// Executor summary; never an independent verdict.
    pub executor_summary: String,
}

impl CompletionClaim {
    /// Construct a claim in the evaluation-pending state.
    pub fn new(
        subject: EvaluationSubject,
        criteria: Vec<AcceptanceCriterion>,
        changed_artifacts: Vec<ArtifactRef>,
        claimed_evidence: Vec<EvidenceRef>,
        executor_summary: impl Into<String>,
    ) -> Result<Self, EvaluationError> {
        subject.validate()?;
        validate_criteria(&criteria)?;
        Ok(Self {
            id: Uuid::new_v4(),
            subject,
            criteria,
            changed_artifacts,
            claimed_evidence,
            executor_summary: executor_summary.into(),
        })
    }

    /// Begin a storage-neutral evaluation state machine for this claim.
    #[must_use]
    pub fn evaluation(&self) -> Evaluation {
        Evaluation {
            claim: self.clone(),
            state: EvaluationState::Pending,
            report: None,
        }
    }
}

/// Criterion-level outcome produced by an evaluator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CriterionVerdict {
    /// Criterion is satisfied.
    Pass,
    /// Criterion is not satisfied.
    Fail,
    /// Available evidence is insufficient to decide.
    Inconclusive,
}

/// Aggregate evaluation outcome derived from required criteria.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EvaluationVerdict {
    /// Every required criterion passed.
    Pass,
    /// At least one required criterion failed.
    Fail,
    /// No required criterion failed, but at least one is inconclusive.
    Inconclusive,
}

/// Severity assigned to one evaluator finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum FindingSeverity {
    /// Informational observation.
    Info,
    /// Non-blocking concern.
    Warning,
    /// Finding that prevents a passing criterion.
    Blocking,
}

/// A bounded, criterion-linked evaluator finding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct EvaluationFinding {
    /// Stable finding identity.
    pub id: Uuid,
    /// Criterion this finding concerns, if criterion-specific.
    pub criterion_id: Option<Uuid>,
    /// Finding severity.
    pub severity: FindingSeverity,
    /// Concise explanation for rework or audit.
    pub summary: String,
    /// Referenced evidence; references do not issue the verdict.
    pub evidence: Vec<EvidenceRef>,
}

/// One evaluator result for one acceptance criterion.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct CriterionEvaluation {
    /// Criterion identity from the claim snapshot.
    pub criterion_id: Uuid,
    /// Evaluator outcome.
    pub verdict: CriterionVerdict,
    /// Evidence references supporting the outcome.
    pub evidence: Vec<EvidenceRef>,
    /// Findings associated with this criterion.
    pub finding_ids: Vec<Uuid>,
}

/// A complete criterion-granular report bound to one exact claim subject.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct EvaluationReport {
    /// Stable report identity.
    pub id: Uuid,
    /// Claim being evaluated.
    pub claim_id: Uuid,
    /// Exact subject inspected.
    pub subject: EvaluationSubject,
    /// Criterion-level outcomes.
    pub results: Vec<CriterionEvaluation>,
    /// Findings explaining outcomes.
    pub findings: Vec<EvaluationFinding>,
    /// Deterministically derived aggregate verdict.
    pub verdict: EvaluationVerdict,
}

impl EvaluationReport {
    /// Construct a report, rejecting missing, duplicate or unknown criteria and contradictions.
    pub fn new(
        claim: &CompletionClaim,
        subject: EvaluationSubject,
        results: Vec<CriterionEvaluation>,
        findings: Vec<EvaluationFinding>,
    ) -> Result<Self, EvaluationError> {
        if !claim.subject.matches(&subject) {
            return Err(EvaluationError::SubjectMismatch);
        }
        let criterion_ids: HashSet<_> = claim
            .criteria
            .iter()
            .map(|criterion| criterion.id)
            .collect();
        let mut result_ids = HashSet::new();
        if results.iter().any(|result| {
            !criterion_ids.contains(&result.criterion_id) || !result_ids.insert(result.criterion_id)
        }) {
            return Err(EvaluationError::CriterionMismatch);
        }
        if result_ids.len() != criterion_ids.len() {
            return Err(EvaluationError::CriterionMismatch);
        }
        let finding_ids: HashSet<_> = findings.iter().map(|finding| finding.id).collect();
        if finding_ids.len() != findings.len()
            || findings.iter().any(|finding| {
                finding
                    .criterion_id
                    .is_some_and(|id| !criterion_ids.contains(&id))
                    || finding.summary.trim().is_empty()
            })
            || results
                .iter()
                .flat_map(|result| result.finding_ids.iter())
                .any(|id| !finding_ids.contains(id))
        {
            return Err(EvaluationError::FindingMismatch);
        }
        let verdict = aggregate_verdict(&claim.criteria, &results);
        Ok(Self {
            id: Uuid::new_v4(),
            claim_id: claim.id,
            subject,
            results,
            findings,
            verdict,
        })
    }
}

/// Lifecycle of an evaluation report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "kind", content = "verdict")]
pub enum EvaluationState {
    /// Claim is awaiting an independent evaluator.
    Pending,
    /// Evaluator is inspecting the exact subject.
    Evaluating,
    /// A report has been accepted for the current subject.
    Verdict(EvaluationVerdict),
    /// Subject changed after a report and the prior verdict no longer certifies it.
    Stale,
    /// Rework is required before a new claim can be submitted.
    Rework,
}

/// Storage-neutral state machine for one completion claim.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Evaluation {
    /// Immutable claim under evaluation.
    pub claim: CompletionClaim,
    /// Current lifecycle state.
    pub state: EvaluationState,
    /// Accepted report, when one exists.
    pub report: Option<EvaluationReport>,
}

impl Evaluation {
    /// Return whether this evaluation contains a validated passing report for its claim.
    #[must_use]
    pub fn has_valid_pass(&self) -> bool {
        if self.state != EvaluationState::Verdict(EvaluationVerdict::Pass) {
            return false;
        }
        let Some(report) = &self.report else {
            return false;
        };
        if report.claim_id != self.claim.id || !self.claim.subject.matches(&report.subject) {
            return false;
        }
        if report.verdict != EvaluationVerdict::Pass {
            return false;
        }
        EvaluationReport::new(
            &self.claim,
            report.subject,
            report.results.clone(),
            report.findings.clone(),
        )
        .is_ok_and(|validated| validated.verdict == report.verdict)
    }

    /// Transition `Pending` to `Evaluating`.
    pub fn begin(&mut self) -> Result<(), EvaluationError> {
        if self.state != EvaluationState::Pending {
            return Err(EvaluationError::IllegalTransition);
        }
        self.state = EvaluationState::Evaluating;
        Ok(())
    }

    /// Accept a report and transition `Evaluating` to its derived verdict.
    pub fn accept_report(&mut self, report: EvaluationReport) -> Result<(), EvaluationError> {
        if self.state != EvaluationState::Evaluating || report.claim_id != self.claim.id {
            return Err(EvaluationError::IllegalTransition);
        }
        // Reports are public and deserializable, so revalidate the complete payload at the
        // authority boundary instead of trusting a caller-provided aggregate verdict.
        if !self.claim.subject.matches(&report.subject) {
            return Err(EvaluationError::SubjectMismatch);
        }
        let validated = EvaluationReport::new(
            &self.claim,
            report.subject,
            report.results.clone(),
            report.findings.clone(),
        )?;
        if validated.verdict != report.verdict {
            return Err(EvaluationError::VerdictMismatch);
        }
        self.state = EvaluationState::Verdict(report.verdict);
        self.report = Some(report);
        Ok(())
    }

    /// Mark a verdict stale when the relevant subject revision changes.
    pub fn observe_subject(&mut self, current: EvaluationSubject) -> Result<(), EvaluationError> {
        if self.state
            != EvaluationState::Verdict(
                self.report
                    .as_ref()
                    .map_or(EvaluationVerdict::Inconclusive, |report| report.verdict),
            )
        {
            return Err(EvaluationError::IllegalTransition);
        }
        if !self.claim.subject.matches(&current) {
            self.state = EvaluationState::Stale;
        }
        Ok(())
    }

    /// Return a failed/inconclusive evaluation to the executor for rework.
    pub fn request_rework(&mut self) -> Result<(), EvaluationError> {
        match self.state {
            EvaluationState::Verdict(EvaluationVerdict::Fail | EvaluationVerdict::Inconclusive)
            | EvaluationState::Stale => {
                self.state = EvaluationState::Rework;
                Ok(())
            }
            _ => Err(EvaluationError::IllegalTransition),
        }
    }
}

/// Errors raised while constructing or advancing the evaluation contract.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum EvaluationError {
    /// Mission/Goal roles or another subject invariant is invalid.
    #[error("invalid evaluation subject: {0}")]
    InvalidSubject(&'static str),
    /// Criteria are empty, duplicated or malformed.
    #[error("invalid acceptance criteria")]
    InvalidCriteria,
    /// A report does not exactly match the claim's criterion set.
    #[error("evaluation criteria do not exactly match the claim")]
    CriterionMismatch,
    /// A report finding is duplicated, unknown or malformed.
    #[error("evaluation findings do not match the report")]
    FindingMismatch,
    /// A report was produced for a different subject revision.
    #[error("evaluation subject revision does not match the claim")]
    SubjectMismatch,
    /// An operation is not legal from the current lifecycle state.
    #[error("illegal evaluation state transition")]
    IllegalTransition,
    /// A report's caller-provided aggregate verdict disagrees with its criterion results.
    #[error("evaluation verdict contradicts criterion results")]
    VerdictMismatch,
}

fn validate_criteria(criteria: &[AcceptanceCriterion]) -> Result<(), EvaluationError> {
    let mut ids = HashSet::new();
    if criteria.is_empty()
        || criteria
            .iter()
            .any(|criterion| criterion.statement.trim().is_empty() || !ids.insert(criterion.id))
    {
        return Err(EvaluationError::InvalidCriteria);
    }
    Ok(())
}

fn aggregate_verdict(
    criteria: &[AcceptanceCriterion],
    results: &[CriterionEvaluation],
) -> EvaluationVerdict {
    let outcomes: HashMap<_, _> = results
        .iter()
        .map(|result| (result.criterion_id, result.verdict))
        .collect();
    let mut required = criteria.iter().filter(|criterion| criterion.required);
    if required
        .clone()
        .any(|criterion| outcomes.get(&criterion.id) == Some(&CriterionVerdict::Fail))
    {
        EvaluationVerdict::Fail
    } else if required
        .any(|criterion| outcomes.get(&criterion.id) == Some(&CriterionVerdict::Inconclusive))
    {
        EvaluationVerdict::Inconclusive
    } else {
        EvaluationVerdict::Pass
    }
}

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

    fn subject() -> EvaluationSubject {
        EvaluationSubject {
            mission: WorkIdentity {
                id: Uuid::new_v4(),
                kind: WorkKind::Mission,
                revision: 1,
            },
            goal: WorkIdentity {
                id: Uuid::new_v4(),
                kind: WorkKind::Goal,
                revision: 2,
            },
            workspace: WorkspaceRevision {
                id: Uuid::new_v4(),
                revision: 3,
            },
        }
    }

    fn criterion(required: bool) -> AcceptanceCriterion {
        AcceptanceCriterion {
            id: Uuid::new_v4(),
            kind: CriterionKind::Behavior,
            statement: "works".into(),
            required,
        }
    }

    fn result(id: Uuid, verdict: CriterionVerdict) -> CriterionEvaluation {
        CriterionEvaluation {
            criterion_id: id,
            verdict,
            evidence: vec![],
            finding_ids: vec![],
        }
    }

    #[test]
    fn state_machine_rejects_self_certification_and_stales_on_revision() {
        let claim = CompletionClaim::new(subject(), vec![criterion(true)], vec![], vec![], "done")
            .expect("valid test fixture");
        let mut evaluation = claim.evaluation();
        assert_eq!(evaluation.state, EvaluationState::Pending);
        let report = EvaluationReport::new(
            &claim,
            claim.subject,
            vec![result(claim.criteria[0].id, CriterionVerdict::Pass)],
            vec![],
        )
        .expect("valid test fixture");
        assert_eq!(
            evaluation.accept_report(report),
            Err(EvaluationError::IllegalTransition)
        );
        evaluation.begin().expect("valid test fixture");
        let report = EvaluationReport::new(
            &claim,
            claim.subject,
            vec![result(claim.criteria[0].id, CriterionVerdict::Pass)],
            vec![],
        )
        .expect("valid test fixture");
        evaluation
            .accept_report(report)
            .expect("valid test fixture");
        let mut changed = claim.subject;
        changed.goal.revision += 1;
        evaluation
            .observe_subject(changed)
            .expect("valid test fixture");
        assert_eq!(evaluation.state, EvaluationState::Stale);
        evaluation.request_rework().expect("valid test fixture");
        assert_eq!(evaluation.state, EvaluationState::Rework);
    }

    #[test]
    fn locale_is_not_an_evaluation_subject_component() {
        let a = subject();
        let b = a;
        assert!(a.matches(&b));
    }

    #[test]
    fn aggregate_requires_required_criteria_only() {
        let required = criterion(true);
        let optional = criterion(false);
        let claim = CompletionClaim::new(
            subject(),
            vec![required.clone(), optional.clone()],
            vec![],
            vec![],
            "done",
        )
        .expect("valid test fixture");
        let report = EvaluationReport::new(
            &claim,
            claim.subject,
            vec![
                result(required.id, CriterionVerdict::Pass),
                result(optional.id, CriterionVerdict::Fail),
            ],
            vec![],
        )
        .expect("valid test fixture");
        assert_eq!(report.verdict, EvaluationVerdict::Pass);
    }

    #[test]
    fn report_rejects_duplicates_and_subject_mismatch() {
        let c = criterion(true);
        let claim = CompletionClaim::new(subject(), vec![c.clone()], vec![], vec![], "done")
            .expect("valid test fixture");
        assert_eq!(
            EvaluationReport::new(
                &claim,
                subject(),
                vec![result(c.id, CriterionVerdict::Pass)],
                vec![]
            ),
            Err(EvaluationError::SubjectMismatch)
        );
        assert_eq!(
            EvaluationReport::new(
                &claim,
                claim.subject,
                vec![
                    result(c.id, CriterionVerdict::Pass),
                    result(c.id, CriterionVerdict::Pass)
                ],
                vec![]
            ),
            Err(EvaluationError::CriterionMismatch)
        );
    }

    #[test]
    fn accepting_a_forged_report_revalidates_subject_and_verdict() {
        let c = criterion(true);
        let claim = CompletionClaim::new(subject(), vec![c.clone()], vec![], vec![], "done")
            .expect("valid test claim");
        let mut evaluation = claim.evaluation();
        evaluation
            .begin()
            .expect("pending claim can begin evaluation");
        let valid = EvaluationReport::new(
            &claim,
            claim.subject,
            vec![result(c.id, CriterionVerdict::Pass)],
            vec![],
        )
        .expect("valid test report");
        let forged = EvaluationReport {
            subject: {
                let mut subject = claim.subject;
                subject.goal.revision += 1;
                subject
            },
            verdict: EvaluationVerdict::Fail,
            ..valid
        };
        assert_eq!(
            evaluation.accept_report(forged),
            Err(EvaluationError::SubjectMismatch)
        );
        assert_eq!(evaluation.state, EvaluationState::Evaluating);

        let valid = EvaluationReport::new(
            &claim,
            claim.subject,
            vec![result(c.id, CriterionVerdict::Pass)],
            vec![],
        )
        .expect("valid test report");
        let forged = EvaluationReport {
            verdict: EvaluationVerdict::Fail,
            ..valid
        };
        assert_eq!(
            evaluation.accept_report(forged),
            Err(EvaluationError::VerdictMismatch)
        );
        assert_eq!(evaluation.state, EvaluationState::Evaluating);
    }
}