lean-ctx-protocol 0.4.0

Open wire contracts for lean-ctx: events, observations, assignments, evidence gaps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// SPDX-License-Identifier: Apache-2.0
//! Neutral, content-free evidence about how context representation strategies
//! performed on completed tasks.
//!
//! The Engine observes and aggregates; it does not decide. A consumer that
//! learns a policy from this evidence lives outside this crate. Records carry
//! bounded enums and counts only — never prompts, source text, paths or
//! repository names. Anything the Engine did not measure is marked
//! `Unmeasured`, never reported as zero.

use serde::{Deserialize, Serialize};

use crate::common::ValidationError;
use crate::context_gateway::{QualityRecoveryV1, RetentionCountsV1};

pub const CONTEXT_POLICY_EVIDENCE_VERSION: u32 = 1;
/// Upper bound on records in one evidence document.
pub const MAX_EVIDENCE_RECORDS: usize = 4_096;

/// The task class the planner assigned.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkloadTaskClassV1 {
    BugFix,
    Refactor,
    TestAddition,
    Documentation,
    Investigation,
}

/// Dominant language of the planned file context, by extension.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkloadLanguageV1 {
    Rust,
    Python,
    TypeScript,
    JavaScript,
    Go,
    Java,
    C,
    Cpp,
    CSharp,
    Swift,
    Kotlin,
    Ruby,
    Php,
    Shell,
    Other,
    /// No file object was planned.
    None,
}

/// Planned context budget, bucketed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkloadSizeV1 {
    /// ≤ 2k tokens.
    Tiny,
    /// ≤ 8k tokens.
    Small,
    /// ≤ 32k tokens.
    Medium,
    /// ≤ 128k tokens.
    Large,
    VeryLarge,
}

impl WorkloadSizeV1 {
    #[must_use]
    pub fn from_budget_tokens(tokens: u64) -> Self {
        match tokens {
            0..=2_048 => Self::Tiny,
            2_049..=8_192 => Self::Small,
            8_193..=32_768 => Self::Medium,
            32_769..=131_072 => Self::Large,
            _ => Self::VeryLarge,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkloadV1 {
    pub task_class: WorkloadTaskClassV1,
    pub language: WorkloadLanguageV1,
    pub size: WorkloadSizeV1,
}

/// Context quality measured on the task's deliveries, summed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "state", deny_unknown_fields)]
pub enum QualityEvidenceV1 {
    Measured {
        /// Critical probes only: losing an important fact is not a quality failure.
        retention: RetentionCountsV1,
        recovery: QualityRecoveryV1,
    },
    Unmeasured,
}

/// Whether a strategy change was checked for security regressions. The Engine
/// cannot attribute one today, so it reports `Unmeasured`; a consumer must treat
/// that as ineligible for promotion, never as "no regression".
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "state", deny_unknown_fields)]
pub enum SecurityEvidenceV1 {
    Measured { regressions: u64 },
    Unmeasured,
}

/// The read strategy the planner chose, as a closed set. Configured mode
/// strings are free text and could name a project; anything outside the known
/// modes is reported as `Other`, never passed through.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadStrategyV1 {
    Full,
    Map,
    Signatures,
    Aggressive,
    Entropy,
    Task,
    Reference,
    Diff,
    Lines,
    Auto,
    Other,
}

impl ReadStrategyV1 {
    /// Classify an Engine read mode; `lines:N-M` is `Lines`.
    #[must_use]
    pub fn from_mode(mode: &str) -> Self {
        match mode {
            "full" => Self::Full,
            "map" => Self::Map,
            "signatures" => Self::Signatures,
            "aggressive" => Self::Aggressive,
            "entropy" => Self::Entropy,
            "task" => Self::Task,
            "reference" => Self::Reference,
            "diff" => Self::Diff,
            "auto" => Self::Auto,
            _ if mode == "lines" || mode.starts_with("lines:") => Self::Lines,
            _ => Self::Other,
        }
    }

    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Full => "full",
            Self::Map => "map",
            Self::Signatures => "signatures",
            Self::Aggressive => "aggressive",
            Self::Entropy => "entropy",
            Self::Task => "task",
            Self::Reference => "reference",
            Self::Diff => "diff",
            Self::Lines => "lines",
            Self::Auto => "auto",
            Self::Other => "other",
        }
    }
}

#[allow(clippy::trivially_copy_pass_by_ref)] // serde's skip_serializing_if signature
fn is_zero(value: &u64) -> bool {
    *value == 0
}

/// Outcomes of one read strategy on one workload, on one UTC day.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StrategyOutcomeRecordV1 {
    pub workload: WorkloadV1,
    pub strategy: ReadStrategyV1,
    /// Days since the Unix epoch (UTC) on which the outcomes were observed,
    /// so a consumer can weight evidence by its real age.
    pub observed_day: u64,
    /// Tasks with a terminal outcome; Unknown outcomes are never counted.
    pub samples: u64,
    pub accepted: u64,
    pub rejected: u64,
    /// Tasks where the user explicitly overrode the planner's mode.
    pub explicit_overrides: u64,
    /// Tasks whose deliveries were all verified; only these contribute tokens.
    pub token_samples: u64,
    /// Tasks whose runtime signals are fully attributed; the counts below are
    /// tasks among them with at least one such event. Zero counts are left
    /// out on the wire, so consumers of the first schema keep parsing.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub signal_samples: u64,
    /// Full re-read of a file read compressed in the task.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub bounce_tasks: u64,
    /// Expansion of a file handle read compressed in the task.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub expand_tasks: u64,
    /// Failed edit after a compressed read in the task.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub edit_failure_tasks: u64,
    pub tokens_original: u64,
    pub tokens_delivered: u64,
    pub quality: QualityEvidenceV1,
    pub security: SecurityEvidenceV1,
}

impl StrategyOutcomeRecordV1 {
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.samples == 0 {
            return Err(ValidationError::new("record without samples"));
        }
        if self.accepted.checked_add(self.rejected) != Some(self.samples) {
            return Err(ValidationError::new(
                "accepted + rejected must equal samples",
            ));
        }
        if self.explicit_overrides > self.samples {
            return Err(ValidationError::new("more overrides than samples"));
        }
        if self.token_samples > self.samples {
            return Err(ValidationError::new("more token samples than samples"));
        }
        if self.signal_samples > self.samples
            || [
                self.bounce_tasks,
                self.expand_tasks,
                self.edit_failure_tasks,
            ]
            .iter()
            .any(|count| *count > self.signal_samples)
        {
            return Err(ValidationError::new(
                "signal counts exceed their attributed tasks",
            ));
        }
        if self.token_samples == 0 && (self.tokens_original > 0 || self.tokens_delivered > 0) {
            return Err(ValidationError::new("tokens without token samples"));
        }
        if self.tokens_delivered > self.tokens_original {
            return Err(ValidationError::new("delivered tokens exceed original"));
        }
        if let QualityEvidenceV1::Measured { recovery, .. } = &self.quality
            && (recovery.handles_verified > recovery.handles_emitted
                || recovery.failures > recovery.handles_emitted
                || recovery.critical_failures > recovery.failures)
        {
            return Err(ValidationError::new("impossible recovery counts"));
        }
        Ok(())
    }

    fn key(&self) -> (&WorkloadV1, ReadStrategyV1, u64) {
        (&self.workload, self.strategy, self.observed_day)
    }
}

/// Verdict of a paired task evaluation (`lean-ctx eval frontier`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvaluationVerdictV1 {
    Improved,
    NonInferior,
    Regressed,
    Underpowered,
}

/// A paired task evaluation of one read strategy against the unranked raw
/// baseline on a declared suite. Suite-level, not per workload; no suite name
/// or task content. Deltas are in thousandths of the suite's score scale.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StrategyEvaluationV1 {
    pub strategy: ReadStrategyV1,
    pub evidence_tier: crate::context_gateway::QualityEvidenceTierV1,
    pub verdict: EvaluationVerdictV1,
    pub pairs: u64,
    pub powered: bool,
    pub delta_milli: i64,
    pub ci_low_milli: i64,
    pub ci_high_milli: i64,
    pub margin_milli: i64,
}

impl StrategyEvaluationV1 {
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.strategy == ReadStrategyV1::Other {
            return Err(ValidationError::new("an evaluation names a known strategy"));
        }
        if self.ci_low_milli > self.ci_high_milli || self.margin_milli < 0 {
            return Err(ValidationError::new("impossible evaluation interval"));
        }
        if self.powered && self.pairs == 0 {
            return Err(ValidationError::new("a powered evaluation has pairs"));
        }
        Ok(())
    }
}

/// Aggregated evidence of one scope (tenant/project), sorted by
/// (workload, strategy, day) so equal evidence serializes to equal bytes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextPolicyEvidenceV1 {
    pub schema_version: u32,
    pub records: Vec<StrategyOutcomeRecordV1>,
    /// The newest task evaluation per strategy, sorted by strategy. Absent
    /// when none was run; an absent evaluation is never a passing one.
    /// Evaluations run on a declared suite, not in a scope: the same
    /// machine-wide results are attached to every scope's evidence.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub evaluations: Vec<StrategyEvaluationV1>,
}

impl ContextPolicyEvidenceV1 {
    /// The evidence in the first schema's shape (no runtime signals, no
    /// evaluations), for consumers that reject unknown fields.
    #[must_use]
    pub fn without_v1_1_fields(&self) -> Self {
        let mut legacy = self.clone();
        legacy.evaluations.clear();
        for record in &mut legacy.records {
            record.signal_samples = 0;
            record.bounce_tasks = 0;
            record.expand_tasks = 0;
            record.edit_failure_tasks = 0;
        }
        legacy
    }

    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.schema_version != CONTEXT_POLICY_EVIDENCE_VERSION {
            return Err(ValidationError::new("unsupported evidence version"));
        }
        if self.records.len() > MAX_EVIDENCE_RECORDS {
            return Err(ValidationError::new("too many evidence records"));
        }
        for record in &self.records {
            record.validate()?;
        }
        if !self
            .records
            .windows(2)
            .all(|pair| pair[0].key() < pair[1].key())
        {
            return Err(ValidationError::new(
                "records must be strictly sorted by workload, strategy and day",
            ));
        }
        for evaluation in &self.evaluations {
            evaluation.validate()?;
        }
        if !self
            .evaluations
            .windows(2)
            .all(|pair| pair[0].strategy < pair[1].strategy)
        {
            return Err(ValidationError::new(
                "evaluations must be one per strategy, sorted",
            ));
        }
        Ok(())
    }
}

/// Schema version of [`ContextPolicyV1`].
pub const CONTEXT_POLICY_VERSION: u32 = 1;
/// Upper bound on entries in one policy.
pub const MAX_POLICY_ENTRIES: usize = 4_096;

/// One workload's read strategy in a promoted policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextPolicyEntryV1 {
    pub workload: WorkloadV1,
    pub strategy: ReadStrategyV1,
}

/// A promoted read-strategy policy in the Engine's vocabulary. Whoever
/// learned it keeps its own artifact; `learner_digest` names that artifact
/// so a later rollback can be traced to it. Entries are sorted by workload,
/// one per workload, and only name strategies the Engine can apply.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextPolicyV1 {
    pub schema_version: u32,
    pub version: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_version: Option<u64>,
    pub entries: Vec<ContextPolicyEntryV1>,
    /// Lowercase hex digest of the learner's artifact (opaque to the Engine).
    pub learner_digest: String,
}

impl ContextPolicyV1 {
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.schema_version != CONTEXT_POLICY_VERSION {
            return Err(ValidationError::new("unsupported policy version"));
        }
        if self.entries.len() > MAX_POLICY_ENTRIES {
            return Err(ValidationError::new("too many policy entries"));
        }
        if self
            .parent_version
            .is_some_and(|parent| parent >= self.version)
        {
            return Err(ValidationError::new(
                "a policy must be newer than its parent",
            ));
        }
        if self.entries.iter().any(|entry| {
            matches!(
                entry.strategy,
                ReadStrategyV1::Other | ReadStrategyV1::Auto | ReadStrategyV1::Lines
            )
        }) {
            return Err(ValidationError::new(
                "a policy names only strategies the planner can apply",
            ));
        }
        if !self
            .entries
            .windows(2)
            .all(|pair| pair[0].workload < pair[1].workload)
        {
            return Err(ValidationError::new(
                "policy entries must be one per workload, sorted",
            ));
        }
        if self.learner_digest.len() != 64
            || !self
                .learner_digest
                .bytes()
                .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
        {
            return Err(ValidationError::new("invalid learner digest"));
        }
        Ok(())
    }

    /// The strategy this policy assigns to `workload`, if any.
    #[must_use]
    pub fn strategy_for(&self, workload: &WorkloadV1) -> Option<ReadStrategyV1> {
        self.entries
            .binary_search_by(|entry| entry.workload.cmp(workload))
            .ok()
            .map(|index| self.entries[index].strategy)
    }
}

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

    fn record(strategy: ReadStrategyV1, day: u64) -> StrategyOutcomeRecordV1 {
        StrategyOutcomeRecordV1 {
            workload: WorkloadV1 {
                task_class: WorkloadTaskClassV1::BugFix,
                language: WorkloadLanguageV1::Rust,
                size: WorkloadSizeV1::Tiny,
            },
            strategy,
            observed_day: day,
            samples: 3,
            accepted: 2,
            rejected: 1,
            explicit_overrides: 0,
            token_samples: 3,
            signal_samples: 0,
            bounce_tasks: 0,
            expand_tasks: 0,
            edit_failure_tasks: 0,
            tokens_original: 900,
            tokens_delivered: 300,
            quality: QualityEvidenceV1::Unmeasured,
            security: SecurityEvidenceV1::Unmeasured,
        }
    }

    #[test]
    fn budget_buckets_have_inclusive_upper_bounds() {
        assert_eq!(WorkloadSizeV1::from_budget_tokens(0), WorkloadSizeV1::Tiny);
        assert_eq!(
            WorkloadSizeV1::from_budget_tokens(2_048),
            WorkloadSizeV1::Tiny
        );
        assert_eq!(
            WorkloadSizeV1::from_budget_tokens(2_049),
            WorkloadSizeV1::Small
        );
        assert_eq!(
            WorkloadSizeV1::from_budget_tokens(131_072),
            WorkloadSizeV1::Large
        );
        assert_eq!(
            WorkloadSizeV1::from_budget_tokens(131_073),
            WorkloadSizeV1::VeryLarge
        );
    }

    #[test]
    fn modes_map_to_a_closed_set_and_never_pass_text_through() {
        assert_eq!(ReadStrategyV1::from_mode("map"), ReadStrategyV1::Map);
        assert_eq!(
            ReadStrategyV1::from_mode("lines:10-20"),
            ReadStrategyV1::Lines
        );
        assert_eq!(
            ReadStrategyV1::from_mode("internal-payments"),
            ReadStrategyV1::Other
        );
        let json = serde_json::to_string(&ReadStrategyV1::from_mode("internal-payments"))
            .expect("serializes");
        assert_eq!(json, r#""other""#);
        for strategy in [
            ReadStrategyV1::Full,
            ReadStrategyV1::Lines,
            ReadStrategyV1::Other,
        ] {
            assert_eq!(
                serde_json::to_value(strategy).expect("serializes"),
                strategy.as_str()
            );
        }
    }

    #[test]
    fn evidence_must_be_consistent_and_sorted() {
        let mut evidence = ContextPolicyEvidenceV1 {
            schema_version: CONTEXT_POLICY_EVIDENCE_VERSION,
            records: vec![
                record(ReadStrategyV1::Full, 20_000),
                record(ReadStrategyV1::Full, 20_001),
                record(ReadStrategyV1::Map, 19_000),
            ],
            evaluations: Vec::new(),
        };
        let evaluation = |strategy| StrategyEvaluationV1 {
            strategy,
            evidence_tier: crate::context_gateway::QualityEvidenceTierV1::LiveTaskEvaluation,
            verdict: EvaluationVerdictV1::NonInferior,
            pairs: 30,
            powered: true,
            delta_milli: 5,
            ci_low_milli: -10,
            ci_high_milli: 20,
            margin_milli: 30,
        };
        evidence.evaluations = vec![
            evaluation(ReadStrategyV1::Full),
            evaluation(ReadStrategyV1::Map),
        ];
        assert!(evidence.validate().is_ok());
        evidence.evaluations.swap(0, 1);
        assert!(evidence.validate().is_err(), "evaluations unsorted");
        evidence.evaluations = vec![evaluation(ReadStrategyV1::Other)];
        assert!(
            evidence.validate().is_err(),
            "an unknown strategy is no evaluation"
        );
        evidence.evaluations.clear();
        assert!(evidence.validate().is_ok());
        evidence.records.swap(0, 1);
        assert!(evidence.validate().is_err(), "days unsorted");
        evidence.records.swap(0, 1);
        evidence.records[0].rejected = 2;
        assert!(evidence.validate().is_err(), "outcomes do not add up");
        evidence.records[0].rejected = 1;
        evidence.records[0].token_samples = 0;
        assert!(evidence.validate().is_err(), "tokens without token samples");
        evidence.records[0].token_samples = 4;
        assert!(
            evidence.validate().is_err(),
            "more token samples than samples"
        );
        evidence.records[0].token_samples = 3;
        evidence.records[0].quality = QualityEvidenceV1::Measured {
            retention: RetentionCountsV1 {
                retained: 1,
                recoverable: 0,
                lost: 0,
            },
            recovery: QualityRecoveryV1 {
                handles_emitted: 1,
                handles_verified: 1,
                failures: 0,
                critical_failures: 1,
            },
        };
        assert!(evidence.validate().is_err(), "impossible recovery counts");
    }

    #[test]
    fn unmeasured_is_explicit_on_the_wire() {
        let json = serde_json::to_string(&record(ReadStrategyV1::Map, 1)).expect("round trip");
        assert!(
            json.contains(r#""quality":{"state":"unmeasured"}"#),
            "{json}"
        );
        assert!(
            json.contains(r#""security":{"state":"unmeasured"}"#),
            "{json}"
        );
        let back: StrategyOutcomeRecordV1 = serde_json::from_str(&json).expect("round trip");
        assert_eq!(back, record(ReadStrategyV1::Map, 1));

        // Zero signal counts stay off the wire, so first-schema consumers
        // (which reject unknown fields) keep parsing; non-zero ones appear.
        assert!(!json.contains("signal_samples"), "{json}");
        let mut signalled = record(ReadStrategyV1::Map, 1);
        signalled.signal_samples = 2;
        signalled.bounce_tasks = 1;
        let evidence = ContextPolicyEvidenceV1 {
            schema_version: CONTEXT_POLICY_EVIDENCE_VERSION,
            records: vec![signalled],
            evaluations: vec![StrategyEvaluationV1 {
                strategy: ReadStrategyV1::Map,
                evidence_tier: crate::context_gateway::QualityEvidenceTierV1::LiveTaskEvaluation,
                verdict: EvaluationVerdictV1::NonInferior,
                pairs: 10,
                powered: true,
                delta_milli: 0,
                ci_low_milli: -5,
                ci_high_milli: 5,
                margin_milli: 30,
            }],
        };
        let current = serde_json::to_string(&evidence).expect("json");
        assert!(current.contains("\"bounce_tasks\":1") && current.contains("evaluations"));
        let legacy = serde_json::to_string(&evidence.without_v1_1_fields()).expect("json");
        assert!(
            !legacy.contains("signal_samples")
                && !legacy.contains("bounce_tasks")
                && !legacy.contains("evaluations"),
            "{legacy}"
        );
    }

    #[test]
    fn policies_name_only_applicable_strategies_once_per_workload() {
        let workload = |size| WorkloadV1 {
            task_class: WorkloadTaskClassV1::BugFix,
            language: WorkloadLanguageV1::Rust,
            size,
        };
        let mut policy = ContextPolicyV1 {
            schema_version: CONTEXT_POLICY_VERSION,
            version: 2,
            parent_version: Some(1),
            entries: vec![
                ContextPolicyEntryV1 {
                    workload: workload(WorkloadSizeV1::Tiny),
                    strategy: ReadStrategyV1::Full,
                },
                ContextPolicyEntryV1 {
                    workload: workload(WorkloadSizeV1::Large),
                    strategy: ReadStrategyV1::Map,
                },
            ],
            learner_digest: "a".repeat(64),
        };
        policy.validate().expect("valid policy");
        assert_eq!(
            policy.strategy_for(&workload(WorkloadSizeV1::Large)),
            Some(ReadStrategyV1::Map)
        );
        assert_eq!(policy.strategy_for(&workload(WorkloadSizeV1::Medium)), None);

        let mut invalid = policy.clone();
        invalid.entries[1].strategy = ReadStrategyV1::Auto;
        assert!(
            invalid.validate().is_err(),
            "auto is not a learned strategy"
        );
        let mut invalid = policy.clone();
        invalid.entries.swap(0, 1);
        assert!(invalid.validate().is_err(), "unsorted entries");
        let mut invalid = policy.clone();
        invalid.parent_version = Some(2);
        assert!(invalid.validate().is_err(), "parent not older");
        policy.learner_digest = "A".repeat(64);
        assert!(policy.validate().is_err(), "uppercase digest");
    }
}