memd 1.9.0

Local memory CLI for AI agents with hybrid retrieval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Privacy-safe retrieval episodes and explicit task outcomes.

use std::collections::HashSet;
use std::fmt;

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use uuid::Uuid;

use crate::error::{MemdError, Result};
use crate::types::{ChunkId, TenantId};

pub const OUTCOME_POLICY_VERSION: &str = "outcome-v1";
pub const OUTCOME_HALF_LIFE_MS: i64 = 90 * 24 * 60 * 60 * 1_000;
pub const MAX_OUTCOME_ADJUSTMENT: f32 = 0.20;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RetrievalEpisodeId(Uuid);

impl RetrievalEpisodeId {
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    pub fn parse(value: &str) -> Result<Self> {
        Uuid::parse_str(value).map(Self).map_err(|error| {
            MemdError::ValidationError(format!("invalid retrieval episode id: {error}"))
        })
    }
}

impl Default for RetrievalEpisodeId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for RetrievalEpisodeId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}", self.0)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OutcomeEventId(Uuid);

impl OutcomeEventId {
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    pub fn parse(value: &str) -> Result<Self> {
        Uuid::parse_str(value).map(Self).map_err(|error| {
            MemdError::ValidationError(format!("invalid outcome event id: {error}"))
        })
    }
}

impl Default for OutcomeEventId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for OutcomeEventId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}", self.0)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RankingPolicyMode {
    Off,
    Shadow,
    Serve,
}

impl RankingPolicyMode {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Off => "off",
            Self::Shadow => "shadow",
            Self::Serve => "serve",
        }
    }

    pub fn parse(value: &str) -> Result<Self> {
        match value {
            "off" => Ok(Self::Off),
            "shadow" => Ok(Self::Shadow),
            "serve" => Ok(Self::Serve),
            other => Err(MemdError::ValidationError(format!(
                "invalid ranking policy mode: {other}"
            ))),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
// Outcome states may grow as the evidence model does. Keep downstream matches
// forward-compatible instead of turning each wire addition into a major bump.
#[non_exhaustive]
pub enum OutcomeKind {
    Passed,
    Accepted,
    /// A retrieved chunk was reused in a later action, without evidence that
    /// the action or task succeeded. This audit signal never affects ranking.
    ObservedUsed,
    Corrected,
    Failed,
    Abandoned,
    /// The verifier produced no verdict: it crashed, timed out, or returned
    /// something unparseable. Distinct from `Failed`, which asserts the task
    /// was verified and did not succeed.
    ///
    /// Without this state a caller whose verifier broke has only `Failed` to
    /// report, and `Failed` penalises the chunks that were retrieved. That
    /// turns verifier flakiness into permanent negative priors on memories
    /// that may be perfectly good. Recording the event keeps the failure
    /// visible and attributable while crediting nothing in either direction.
    VerifierError,
}

impl OutcomeKind {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Passed => "passed",
            Self::Accepted => "accepted",
            Self::ObservedUsed => "observed_used",
            Self::Corrected => "corrected",
            Self::Failed => "failed",
            Self::Abandoned => "abandoned",
            Self::VerifierError => "verifier_error",
        }
    }

    pub fn parse(value: &str) -> Result<Self> {
        match value {
            "passed" => Ok(Self::Passed),
            "accepted" => Ok(Self::Accepted),
            "observed_used" => Ok(Self::ObservedUsed),
            "corrected" => Ok(Self::Corrected),
            "failed" => Ok(Self::Failed),
            "abandoned" => Ok(Self::Abandoned),
            "verifier_error" => Ok(Self::VerifierError),
            other => Err(MemdError::ValidationError(format!(
                "invalid retrieval outcome: {other}"
            ))),
        }
    }

    pub const fn credits_used(self) -> bool {
        matches!(self, Self::Passed | Self::Accepted)
    }

    pub const fn credits_harmful(self) -> bool {
        matches!(self, Self::Corrected | Self::Failed)
    }

    /// True when the event records that verification did not happen, rather
    /// than a verified result. Never eligible for ranking in either direction.
    pub const fn is_verifier_error(self) -> bool {
        matches!(self, Self::VerifierError)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutcomeVerifier {
    User,
    AutomatedTest,
    ExternalTool,
    TaskSystem,
    AgentSelfReport,
}

impl OutcomeVerifier {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::AutomatedTest => "automated_test",
            Self::ExternalTool => "external_tool",
            Self::TaskSystem => "task_system",
            Self::AgentSelfReport => "agent_self_report",
        }
    }

    pub fn parse(value: &str) -> Result<Self> {
        match value {
            "user" => Ok(Self::User),
            "automated_test" => Ok(Self::AutomatedTest),
            "external_tool" => Ok(Self::ExternalTool),
            "task_system" => Ok(Self::TaskSystem),
            "agent_self_report" => Ok(Self::AgentSelfReport),
            other => Err(MemdError::ValidationError(format!(
                "invalid outcome verifier: {other}"
            ))),
        }
    }

    pub const fn is_ranking_eligible(self) -> bool {
        !matches!(self, Self::AgentSelfReport)
    }

    pub const fn requires_evidence(self) -> bool {
        matches!(
            self,
            Self::AutomatedTest | Self::ExternalTool | Self::TaskSystem
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetrievalEpisode {
    pub episode_id: RetrievalEpisodeId,
    pub tenant_id: TenantId,
    pub project_id: Option<String>,
    pub query_hash: String,
    pub query_mode: String,
    pub requested_k: usize,
    pub fetched_k: usize,
    pub rendered_k: usize,
    pub policy_version: String,
    pub policy_mode: RankingPolicyMode,
    pub task_id: Option<String>,
    pub thread_id: Option<String>,
    pub created_at_ms: i64,
    pub expires_at_ms: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RetrievalEpisodeItem {
    pub episode_id: RetrievalEpisodeId,
    pub chunk_id: ChunkId,
    pub origin_tenant_id: TenantId,
    pub origin_project_id: Option<String>,
    pub original_rank: usize,
    pub original_score: f32,
    pub lane_scores_json: String,
    pub outcome_adjustment: f32,
    pub served_rank: Option<usize>,
    pub shadow_rank: Option<usize>,
    pub rendered: bool,
    pub source_dedup_group: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutcomeEvent {
    pub event_id: OutcomeEventId,
    pub episode_id: RetrievalEpisodeId,
    pub outcome: OutcomeKind,
    pub verifier: OutcomeVerifier,
    pub used_chunk_ids: Vec<ChunkId>,
    pub harmful_chunk_ids: Vec<ChunkId>,
    pub evidence_reference: Option<String>,
    pub ranking_eligible: bool,
    pub timestamp_ms: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutcomePrior {
    pub chunk_id: ChunkId,
    pub eligible_episode_count: usize,
    pub positive_weight: f32,
    pub negative_weight: f32,
    pub last_outcome_at_ms: i64,
}

impl OutcomePrior {
    pub fn new(chunk_id: ChunkId) -> Self {
        Self {
            chunk_id,
            eligible_episode_count: 0,
            positive_weight: 0.0,
            negative_weight: 0.0,
            last_outcome_at_ms: 0,
        }
    }

    pub fn add(&mut self, positive: bool, weight: f32, timestamp_ms: i64) {
        self.eligible_episode_count = self.eligible_episode_count.saturating_add(1);
        if positive {
            self.positive_weight += weight;
        } else {
            self.negative_weight += weight;
        }
        self.last_outcome_at_ms = self.last_outcome_at_ms.max(timestamp_ms);
    }

    /// Bayesian shrinkage keeps one outcome from dominating semantic score;
    /// repeated independent episodes asymptotically approach the hard cap.
    pub fn bounded_adjustment(&self) -> f32 {
        let evidence = self.positive_weight + self.negative_weight;
        if evidence <= f32::EPSILON {
            return 0.0;
        }
        ((self.positive_weight - self.negative_weight) / (evidence + 4.0) * MAX_OUTCOME_ADJUSTMENT)
            .clamp(-MAX_OUTCOME_ADJUSTMENT, MAX_OUTCOME_ADJUSTMENT)
    }
}

pub fn decayed_outcome_weight(timestamp_ms: i64, now_ms: i64) -> f32 {
    let age_ms = now_ms.saturating_sub(timestamp_ms).max(0) as f64;
    2.0_f64.powf(-age_ms / OUTCOME_HALF_LIFE_MS as f64) as f32
}

impl OutcomeEvent {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        episode_id: RetrievalEpisodeId,
        outcome: OutcomeKind,
        verifier: OutcomeVerifier,
        used_chunk_ids: Vec<ChunkId>,
        harmful_chunk_ids: Vec<ChunkId>,
        evidence_reference: Option<String>,
        timestamp_ms: i64,
    ) -> Self {
        let ranking_eligible = expected_ranking_eligibility(
            outcome,
            verifier,
            !used_chunk_ids.is_empty(),
            !harmful_chunk_ids.is_empty(),
            evidence_reference.as_deref(),
        );
        Self {
            event_id: OutcomeEventId::new(),
            episode_id,
            outcome,
            verifier,
            used_chunk_ids,
            harmful_chunk_ids,
            evidence_reference,
            ranking_eligible,
            timestamp_ms,
        }
    }

    /// Whether this event may contribute to ranking priors.
    ///
    /// The legacy scanner wrote passive reuse as eligible `accepted` events.
    /// Keep those rows for audit history while excluding their exact
    /// provenance signature from aggregation.
    pub(crate) fn contributes_to_ranking(&self) -> bool {
        self.ranking_eligible
            && !is_legacy_codex_scanner_outcome(
                self.outcome,
                self.verifier,
                self.evidence_reference.as_deref(),
            )
    }
}

fn expected_ranking_eligibility(
    outcome: OutcomeKind,
    verifier: OutcomeVerifier,
    has_used_chunks: bool,
    has_harmful_chunks: bool,
    evidence_reference: Option<&str>,
) -> bool {
    verifier.is_ranking_eligible()
        && !is_legacy_codex_scanner_outcome(outcome, verifier, evidence_reference)
        && ((outcome.credits_used() && has_used_chunks)
            || (outcome.credits_harmful() && has_harmful_chunks))
}

/// Identify events written by the legacy passive Codex scanner.
///
/// Match the full old signature so a user-verified `accepted` event, or an
/// external verifier with another `codex:` reference convention, keeps its
/// normal semantics.
pub(crate) fn is_legacy_codex_scanner_outcome(
    outcome: OutcomeKind,
    verifier: OutcomeVerifier,
    evidence_reference: Option<&str>,
) -> bool {
    if outcome != OutcomeKind::Accepted || verifier != OutcomeVerifier::ExternalTool {
        return false;
    }
    let Some(reference) = evidence_reference.and_then(|reference| reference.strip_prefix("codex:"))
    else {
        return false;
    };
    let Some((session, line)) = reference.rsplit_once(':') else {
        return false;
    };
    !session.is_empty()
        && session.ends_with(".jsonl")
        && !session.contains('/')
        && !session.contains('\\')
        && line.parse::<usize>().is_ok_and(|line| line > 0)
}

pub fn stable_query_hash(query: &str) -> String {
    let canonical = query.split_whitespace().collect::<Vec<_>>().join(" ");
    format!("{:x}", Sha256::digest(canonical.as_bytes()))
}

pub fn validate_retrieval_episode(
    episode: &RetrievalEpisode,
    items: &[RetrievalEpisodeItem],
) -> Result<()> {
    if episode.query_hash.len() != 64
        || !episode
            .query_hash
            .bytes()
            .all(|byte| byte.is_ascii_hexdigit())
    {
        return Err(MemdError::ValidationError(
            "retrieval episode query hash must be a SHA-256 hex digest".to_string(),
        ));
    }
    if episode.query_mode.trim().is_empty() || episode.policy_version.trim().is_empty() {
        return Err(MemdError::ValidationError(
            "retrieval episode mode and policy version must not be empty".to_string(),
        ));
    }
    for (name, value) in [
        ("task_id", episode.task_id.as_deref()),
        ("thread_id", episode.thread_id.as_deref()),
    ] {
        if let Some(value) = value {
            if value.trim().is_empty() || value.len() > 512 {
                return Err(MemdError::ValidationError(format!(
                    "retrieval episode {name} must contain 1 to 512 bytes"
                )));
            }
        }
    }
    if episode.expires_at_ms <= episode.created_at_ms {
        return Err(MemdError::ValidationError(
            "retrieval episode expiry must follow creation".to_string(),
        ));
    }
    if episode.fetched_k != items.len() {
        return Err(MemdError::ValidationError(format!(
            "retrieval episode fetched_k {} does not match {} candidate items",
            episode.fetched_k,
            items.len()
        )));
    }
    if episode.requested_k > 200 || episode.fetched_k > 200 || episode.rendered_k > 200 {
        return Err(MemdError::ValidationError(
            "retrieval episode exceeds bounded k limits".to_string(),
        ));
    }
    if episode.rendered_k != items.iter().filter(|item| item.rendered).count() {
        return Err(MemdError::ValidationError(
            "retrieval episode rendered_k does not match rendered items".to_string(),
        ));
    }

    let mut chunk_ids = HashSet::new();
    let mut original_ranks = HashSet::new();
    let mut served_ranks = HashSet::new();
    let mut shadow_ranks = HashSet::new();
    for item in items {
        if item.episode_id != episode.episode_id {
            return Err(MemdError::ValidationError(
                "retrieval episode item references a different episode".to_string(),
            ));
        }
        if !chunk_ids.insert(item.chunk_id.clone()) || !original_ranks.insert(item.original_rank) {
            return Err(MemdError::ValidationError(
                "retrieval episode candidate IDs and original ranks must be unique".to_string(),
            ));
        }
        if !item.original_score.is_finite() || !item.outcome_adjustment.is_finite() {
            return Err(MemdError::ValidationError(
                "retrieval episode scores must be finite".to_string(),
            ));
        }
        if item.lane_scores_json.len() > 4096
            || !matches!(
                serde_json::from_str::<serde_json::Value>(&item.lane_scores_json),
                Ok(serde_json::Value::Object(_))
            )
        {
            return Err(MemdError::ValidationError(
                "retrieval episode lane scores must be a bounded JSON object".to_string(),
            ));
        }
        if let Some(rank) = item.served_rank {
            if !served_ranks.insert(rank) {
                return Err(MemdError::ValidationError(
                    "retrieval episode served ranks must be unique".to_string(),
                ));
            }
        }
        if let Some(rank) = item.shadow_rank {
            if !shadow_ranks.insert(rank) {
                return Err(MemdError::ValidationError(
                    "retrieval episode shadow ranks must be unique".to_string(),
                ));
            }
        }
        if item.rendered && item.served_rank.is_none() {
            return Err(MemdError::ValidationError(
                "a rendered retrieval item must have a served rank".to_string(),
            ));
        }
    }
    Ok(())
}

/// Replace the served/rendered projection after a caller-side reranker or
/// packer changes the final output. Candidate identity and shadow ranks remain
/// immutable; only the observable served projection is rewritten.
pub fn apply_rendered_order(
    episode: &mut RetrievalEpisode,
    items: &mut [RetrievalEpisodeItem],
    rendered_chunk_ids: &[ChunkId],
) -> Result<()> {
    if rendered_chunk_ids.len() > episode.requested_k {
        return Err(MemdError::ValidationError(format!(
            "rendered retrieval count {} exceeds requested k {}",
            rendered_chunk_ids.len(),
            episode.requested_k
        )));
    }
    let mut requested = HashSet::new();
    for chunk_id in rendered_chunk_ids {
        if !requested.insert(chunk_id.clone()) {
            return Err(MemdError::ValidationError(
                "rendered retrieval order contains duplicate chunk IDs".to_string(),
            ));
        }
        if !items.iter().any(|item| &item.chunk_id == chunk_id) {
            return Err(MemdError::ValidationError(format!(
                "rendered chunk {chunk_id} was not in the retrieval candidate pool"
            )));
        }
    }
    for item in items.iter_mut() {
        item.served_rank = None;
        item.rendered = false;
    }
    for (rank, chunk_id) in rendered_chunk_ids.iter().enumerate() {
        let item = items
            .iter_mut()
            .find(|item| &item.chunk_id == chunk_id)
            .expect("candidate membership checked above");
        item.served_rank = Some(rank);
        item.rendered = true;
    }
    episode.rendered_k = rendered_chunk_ids.len();
    validate_retrieval_episode(episode, items)
}

pub fn validate_outcome_event(
    tenant_id: &TenantId,
    episode: &RetrievalEpisode,
    items: &[RetrievalEpisodeItem],
    event: &OutcomeEvent,
) -> Result<()> {
    if &episode.tenant_id != tenant_id || event.episode_id != episode.episode_id {
        return Err(MemdError::ValidationError(
            "outcome tenant or episode does not match the retrieval episode".to_string(),
        ));
    }
    if event.timestamp_ms < episode.created_at_ms || event.timestamp_ms > episode.expires_at_ms {
        return Err(MemdError::ValidationError(
            "outcome timestamp falls outside the retrieval episode retention window".to_string(),
        ));
    }
    if event.verifier.requires_evidence()
        && event
            .evidence_reference
            .as_deref()
            .is_none_or(|reference| reference.trim().is_empty())
    {
        return Err(MemdError::ValidationError(format!(
            "{} outcomes require an evidence reference",
            event.verifier.as_str()
        )));
    }
    if event
        .evidence_reference
        .as_ref()
        .is_some_and(|reference| reference.len() > 2048)
    {
        return Err(MemdError::ValidationError(
            "outcome evidence reference exceeds 2048 bytes".to_string(),
        ));
    }

    let rendered = items
        .iter()
        .filter(|item| item.rendered)
        .map(|item| item.chunk_id.clone())
        .collect::<HashSet<_>>();
    let used = event.used_chunk_ids.iter().cloned().collect::<HashSet<_>>();
    let harmful = event
        .harmful_chunk_ids
        .iter()
        .cloned()
        .collect::<HashSet<_>>();
    if used.len() != event.used_chunk_ids.len() || harmful.len() != event.harmful_chunk_ids.len() {
        return Err(MemdError::ValidationError(
            "outcome chunk IDs must not contain duplicates".to_string(),
        ));
    }
    if used.iter().any(|chunk_id| harmful.contains(chunk_id)) {
        return Err(MemdError::ValidationError(
            "outcome used and harmful chunk IDs must not overlap".to_string(),
        ));
    }
    if used
        .iter()
        .chain(harmful.iter())
        .any(|chunk_id| !rendered.contains(chunk_id))
    {
        return Err(MemdError::ValidationError(
            "outcome attribution is limited to chunks rendered in the episode".to_string(),
        ));
    }
    let expected_eligible = expected_ranking_eligibility(
        event.outcome,
        event.verifier,
        !used.is_empty(),
        !harmful.is_empty(),
        event.evidence_reference.as_deref(),
    );
    if event.ranking_eligible != expected_eligible {
        return Err(MemdError::ValidationError(
            "outcome ranking eligibility does not match its verifier and attribution".to_string(),
        ));
    }
    Ok(())
}

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

    #[test]
    fn query_hash_is_stable_and_whitespace_canonical() {
        assert_eq!(stable_query_hash("a  b"), stable_query_hash("a b"));
        assert_eq!(stable_query_hash("A b"), stable_query_hash("A b"));
        assert_ne!(stable_query_hash("A b"), stable_query_hash("a b"));
    }

    #[test]
    fn agent_self_reports_never_become_ranking_eligible() {
        let event = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::Passed,
            OutcomeVerifier::AgentSelfReport,
            vec![ChunkId::new()],
            Vec::new(),
            None,
            1,
        );
        assert!(!event.ranking_eligible);
    }

    #[test]
    fn observed_use_and_legacy_scanner_events_never_credit_success() {
        assert_eq!(OutcomeKind::ObservedUsed.as_str(), "observed_used");
        assert_eq!(
            OutcomeKind::parse("observed_used").unwrap(),
            OutcomeKind::ObservedUsed
        );
        assert_eq!(
            serde_json::to_string(&OutcomeKind::ObservedUsed).unwrap(),
            "\"observed_used\""
        );
        assert!(!OutcomeKind::ObservedUsed.credits_used());
        assert!(!OutcomeKind::ObservedUsed.credits_harmful());

        let chunk_id = ChunkId::new();
        let observed = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::ObservedUsed,
            OutcomeVerifier::ExternalTool,
            vec![chunk_id.clone()],
            Vec::new(),
            Some("codex:rollout-2026-09-15.jsonl:42".to_string()),
            1,
        );
        assert!(!observed.ranking_eligible);

        let legacy = OutcomeEvent {
            event_id: OutcomeEventId::new(),
            episode_id: RetrievalEpisodeId::new(),
            outcome: OutcomeKind::Accepted,
            verifier: OutcomeVerifier::ExternalTool,
            used_chunk_ids: vec![chunk_id.clone()],
            harmful_chunk_ids: Vec::new(),
            evidence_reference: Some("codex:rollout-2026-09-15.jsonl:42".to_string()),
            ranking_eligible: true,
            timestamp_ms: 1,
        };
        assert!(!legacy.contributes_to_ranking());

        let verified = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::Accepted,
            OutcomeVerifier::ExternalTool,
            vec![chunk_id],
            Vec::new(),
            Some("ci://verified-acceptance/42".to_string()),
            1,
        );
        assert!(verified.ranking_eligible);
        assert!(verified.contributes_to_ranking());
    }

    #[test]
    fn corrected_requires_harmful_attribution_and_abandoned_is_never_eligible() {
        let chunk_id = ChunkId::new();
        let corrected = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::Corrected,
            OutcomeVerifier::User,
            Vec::new(),
            vec![chunk_id.clone()],
            None,
            1,
        );
        assert!(corrected.ranking_eligible);

        let corrected_without_attribution = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::Corrected,
            OutcomeVerifier::User,
            Vec::new(),
            Vec::new(),
            None,
            1,
        );
        assert!(!corrected_without_attribution.ranking_eligible);

        let abandoned = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::Abandoned,
            OutcomeVerifier::User,
            vec![chunk_id],
            Vec::new(),
            None,
            1,
        );
        assert!(!abandoned.ranking_eligible);
    }

    /// A verifier that produced no verdict must move nothing. Reporting the
    /// broken run as `failed` is what this state exists to stop, so it is
    /// checked against full attribution in both directions and against the
    /// verifier type that would otherwise carry the most weight.
    #[test]
    fn verifier_error_credits_nothing_in_either_direction() {
        assert!(!OutcomeKind::VerifierError.credits_used());
        assert!(!OutcomeKind::VerifierError.credits_harmful());
        assert!(OutcomeKind::VerifierError.is_verifier_error());
        assert_eq!(
            OutcomeKind::parse("verifier_error").unwrap(),
            OutcomeKind::VerifierError
        );
        assert_eq!(OutcomeKind::VerifierError.as_str(), "verifier_error");

        let used = ChunkId::new();
        let harmful = ChunkId::new();
        let event = OutcomeEvent::new(
            RetrievalEpisodeId::new(),
            OutcomeKind::VerifierError,
            OutcomeVerifier::AutomatedTest,
            vec![used],
            vec![harmful],
            Some("ci://run/1".to_string()),
            1,
        );
        assert!(
            !event.ranking_eligible,
            "a verifier that produced no verdict must not credit or penalise anything"
        );
    }

    #[test]
    fn retrieval_episode_linkage_ids_are_bounded() {
        let episode_id = RetrievalEpisodeId::new();
        let episode = RetrievalEpisode {
            episode_id: episode_id.clone(),
            tenant_id: TenantId::new("linkage_test").unwrap(),
            project_id: None,
            query_hash: stable_query_hash("bounded linkage"),
            query_mode: "generic".to_string(),
            requested_k: 0,
            fetched_k: 0,
            rendered_k: 0,
            policy_version: OUTCOME_POLICY_VERSION.to_string(),
            policy_mode: RankingPolicyMode::Off,
            task_id: Some("x".repeat(513)),
            thread_id: None,
            created_at_ms: 1,
            expires_at_ms: 2,
        };
        let error = validate_retrieval_episode(&episode, &[]).unwrap_err();
        assert!(error
            .to_string()
            .contains("task_id must contain 1 to 512 bytes"));
    }
}