Skip to main content

arabica_runtime/
short_memory.rs

1use std::cmp::Reverse;
2use std::collections::{BTreeMap, HashMap, HashSet};
3
4use arabica_model::{
5    ContentBlock, MemoryBatchKey, MemoryBatchKind, MemoryLoadState, RuntimeItem, ShortMemoryEntry,
6    ShortMemoryItem, ToolCallItem, ToolResultItem,
7};
8use arabica_protocol::{Event, EventEnvelope, EventId, RunId, ToolInteractionKind};
9use serde::{Deserialize, Serialize};
10
11const BATCH_FIELD_EXCERPT_LIMIT: usize = 64;
12const BATCH_USER_MESSAGE_EXCERPT_LIMIT: usize = 256;
13const DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES: usize = 384;
14const DEFAULT_BATCH_TARGET_COMPRESSION_BPS: u16 = 6_000;
15const MISC_BUCKET_SIZE: u64 = 50;
16
17const fn default_true() -> bool {
18    true
19}
20
21const fn default_batch_key_content_limit_bytes() -> usize {
22    DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES
23}
24
25const fn default_batch_target_compression_bps() -> u16 {
26    DEFAULT_BATCH_TARGET_COMPRESSION_BPS
27}
28
29/// Small retention taxonomy used by Runtime. Protocol events remain detailed
30/// audit facts and are projected into one of these policy classes.
31#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
32#[serde(rename_all = "snake_case")]
33pub enum MemoryClass {
34    Anchor,
35    Working,
36    Recovery,
37    ToolInspection,
38    ToolMutation,
39    ToolBuild,
40    ToolDependency,
41    ToolValidation,
42    Transient,
43    Control,
44}
45
46/// Relationship metadata used for precise decay. A completion affects only an
47/// older event with the same relation key, such as one matching tool call.
48#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
49pub struct EventMemoryTraits {
50    pub class: MemoryClass,
51    pub relation_key: Option<String>,
52    pub completes_relation: bool,
53}
54
55#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
56pub struct EventTtl {
57    /// Maximum accumulated event-count decay. None means unbounded.
58    pub ttl_events: Option<u64>,
59    /// Pinned events remain visible regardless of accumulated decay.
60    pub pin: bool,
61}
62
63impl EventTtl {
64    pub const fn ttl(ttl_events: u64) -> Self {
65        Self {
66            ttl_events: Some(ttl_events),
67            pin: false,
68        }
69    }
70
71    pub const fn pinned() -> Self {
72        Self {
73            ttl_events: None,
74            pin: true,
75        }
76    }
77
78    pub const fn unbounded() -> Self {
79        Self {
80            ttl_events: None,
81            pin: false,
82        }
83    }
84}
85
86#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
87#[serde(rename_all = "snake_case")]
88pub enum DecayMatch {
89    Any,
90    CompletesRelation,
91}
92
93#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
94pub struct DecayRule {
95    pub older: MemoryClass,
96    pub newer: MemoryClass,
97    pub match_kind: DecayMatch,
98    pub cost: u64,
99}
100
101/// Declarative, replayable policy for event visibility and batch loading.
102#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
103pub struct ShortMemoryPolicy {
104    pub default_ttl_events: u64,
105    pub recency_floor: usize,
106    pub recent_turns_load_all: usize,
107    #[serde(default = "default_true")]
108    pub batch_compaction_enabled: bool,
109    #[serde(default)]
110    pub key_admission: KeyAdmissionPolicy,
111    #[serde(default = "default_batch_key_content_limit_bytes")]
112    pub batch_key_content_limit_bytes: usize,
113    #[serde(default = "default_batch_target_compression_bps")]
114    pub batch_target_compression_bps: u16,
115    pub ttl_overrides: BTreeMap<MemoryClass, EventTtl>,
116    pub decay_rules: Vec<DecayRule>,
117}
118
119/// Hard limits for historical `LOAD_KEY` materialisation. `None` is unbounded.
120/// Limits apply to UTF-8 key-content bytes, excluding full `LOAD_ALL` items.
121#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
122pub struct KeyAdmissionPolicy {
123    pub max_key_batches: Option<usize>,
124    pub max_key_content_bytes: Option<usize>,
125}
126
127impl Default for ShortMemoryPolicy {
128    fn default() -> Self {
129        let ttl_overrides = BTreeMap::from([
130            (MemoryClass::Anchor, EventTtl::pinned()),
131            (MemoryClass::Working, EventTtl::ttl(3)),
132            (MemoryClass::Recovery, EventTtl::ttl(12)),
133            (MemoryClass::ToolInspection, EventTtl::ttl(1)),
134            (MemoryClass::ToolMutation, EventTtl::ttl(8)),
135            (MemoryClass::ToolBuild, EventTtl::ttl(8)),
136            (MemoryClass::ToolDependency, EventTtl::ttl(8)),
137            (MemoryClass::ToolValidation, EventTtl::ttl(6)),
138            (MemoryClass::Transient, EventTtl::ttl(0)),
139            (MemoryClass::Control, EventTtl::ttl(0)),
140        ]);
141        let decay_rules = vec![
142            DecayRule {
143                older: MemoryClass::Working,
144                newer: MemoryClass::Working,
145                match_kind: DecayMatch::CompletesRelation,
146                cost: 3,
147            },
148            DecayRule {
149                older: MemoryClass::Working,
150                newer: MemoryClass::Recovery,
151                match_kind: DecayMatch::CompletesRelation,
152                cost: 3,
153            },
154        ];
155        Self {
156            default_ttl_events: 6,
157            recency_floor: 20,
158            recent_turns_load_all: 2,
159            batch_compaction_enabled: true,
160            key_admission: KeyAdmissionPolicy::default(),
161            batch_key_content_limit_bytes: DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES,
162            batch_target_compression_bps: DEFAULT_BATCH_TARGET_COMPRESSION_BPS,
163            ttl_overrides,
164            decay_rules,
165        }
166    }
167}
168
169impl ShortMemoryPolicy {
170    /// Keep every visible event as a full typed item. This is the B0
171    /// counterfactual used to measure TTL and BatchKey savings.
172    pub fn full_replay() -> Self {
173        Self {
174            batch_compaction_enabled: false,
175            ..Self::batch_only(usize::MAX)
176        }
177    }
178
179    /// Apply production TTL and relation decay without BatchKey compaction.
180    /// This is the B2 ablation policy.
181    pub fn ttl_only() -> Self {
182        Self {
183            batch_compaction_enabled: false,
184            ..Self::default()
185        }
186    }
187
188    /// Disable event TTL while retaining batch disclosure. This is the B3
189    /// ablation policy and is also useful for diagnostics.
190    pub fn batch_only(recent_turns_load_all: usize) -> Self {
191        let ttl_overrides = BTreeMap::from([
192            (MemoryClass::Anchor, EventTtl::unbounded()),
193            (MemoryClass::Working, EventTtl::unbounded()),
194            (MemoryClass::Recovery, EventTtl::unbounded()),
195            (MemoryClass::ToolInspection, EventTtl::unbounded()),
196            (MemoryClass::ToolMutation, EventTtl::unbounded()),
197            (MemoryClass::ToolBuild, EventTtl::unbounded()),
198            (MemoryClass::ToolDependency, EventTtl::unbounded()),
199            (MemoryClass::ToolValidation, EventTtl::unbounded()),
200            (MemoryClass::Transient, EventTtl::unbounded()),
201            (MemoryClass::Control, EventTtl::unbounded()),
202        ]);
203        Self {
204            default_ttl_events: u64::MAX,
205            recency_floor: 0,
206            recent_turns_load_all,
207            batch_compaction_enabled: true,
208            key_admission: KeyAdmissionPolicy::default(),
209            batch_key_content_limit_bytes: DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES,
210            batch_target_compression_bps: DEFAULT_BATCH_TARGET_COMPRESSION_BPS,
211            ttl_overrides,
212            decay_rules: Vec::new(),
213        }
214    }
215
216    fn ttl_for(&self, class: MemoryClass) -> EventTtl {
217        self.ttl_overrides
218            .get(&class)
219            .copied()
220            .unwrap_or_else(|| EventTtl::ttl(self.default_ttl_events))
221    }
222
223    fn decay_cost(
224        &self,
225        older: MemoryClass,
226        newer: MemoryClass,
227        match_kind: DecayMatch,
228    ) -> Option<u64> {
229        self.decay_rules
230            .iter()
231            .rev()
232            .find(|rule| {
233                rule.older == older && rule.newer == newer && rule.match_kind == match_kind
234            })
235            .map(|rule| rule.cost)
236    }
237}
238
239/// Explainable decision for one immutable source event.
240#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
241pub struct EventVisibilityDecision {
242    pub event_id: EventId,
243    pub sequence: u64,
244    pub memory_class: MemoryClass,
245    pub relation_key: Option<String>,
246    pub accumulated_decay: u64,
247    pub ttl_events: Option<u64>,
248    pub pinned: bool,
249    pub protected_by_recency_floor: bool,
250    pub visible: bool,
251}
252
253/// A stable runtime grouping over immutable events.
254#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
255pub struct EventBatch {
256    pub context_key: String,
257    pub context_kind: MemoryBatchKind,
258    pub run_id: Option<RunId>,
259    pub sequence_start: u64,
260    pub sequence_end: u64,
261    pub event_count: usize,
262    pub estimated_tokens: u64,
263    pub raw_item_bytes: usize,
264    pub key_content_budget_bytes: usize,
265    pub key_content: String,
266    pub key_content_bytes: usize,
267    pub materialized_key_bytes: usize,
268    pub key_admission_rank: Option<usize>,
269    pub key_admission: KeyAdmissionDecision,
270    pub load_state: MemoryLoadState,
271    pub events: Vec<EventEnvelope>,
272}
273
274/// Explainable outcome of applying the key budget to one batch.
275#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
276#[serde(rename_all = "snake_case")]
277pub enum KeyAdmissionDecision {
278    #[default]
279    NotCandidate,
280    Admitted,
281    KeptFullNoBenefit,
282    RejectedBatchLimit,
283    RejectedByteLimit,
284}
285
286#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
287pub struct KeyAdmissionSummary {
288    pub policy: KeyAdmissionPolicy,
289    pub candidate_batches: usize,
290    pub admitted_batches: usize,
291    pub rejected_batches: usize,
292    pub kept_full_no_benefit_batches: usize,
293    pub admitted_key_content_bytes: usize,
294}
295
296/// Complete output of short-memory materialisation. The source event slice is
297/// never modified; decisions and batches are returned for replay diagnostics.
298#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
299pub struct ShortMemoryMaterialization {
300    pub entries: Vec<ShortMemoryEntry>,
301    pub batches: Vec<EventBatch>,
302    pub visibility: Vec<EventVisibilityDecision>,
303    pub key_admission: KeyAdmissionSummary,
304}
305
306/// Pure projection of a Session's immutable event history for the next model
307/// turn. TTL and batch policies alter prompt visibility, never the audit log.
308#[derive(Clone, Copy, Debug, Default)]
309pub struct ShortMemoryProjector;
310
311impl ShortMemoryProjector {
312    pub fn classify_event(event: &Event) -> EventMemoryTraits {
313        event_memory_traits(event)
314    }
315
316    /// Project every provider-relevant event without TTL or batch compaction.
317    ///
318    /// This is the canonical event-to-item mapping used by full-replay
319    /// diagnostics and benchmark baselines. Lifecycle-only events remain in
320    /// the audit log but do not become model input items.
321    pub fn project_full(events: &[EventEnvelope]) -> Vec<ShortMemoryEntry> {
322        events
323            .iter()
324            .flat_map(event_to_short_memory_entries)
325            .collect()
326    }
327
328    /// Return event-level TTL decisions without applying batch disclosure.
329    pub fn visibility(
330        events: &[EventEnvelope],
331        policy: &ShortMemoryPolicy,
332    ) -> Vec<EventVisibilityDecision> {
333        visibility_decisions(events, policy)
334    }
335
336    pub fn project(events: &[EventEnvelope]) -> Vec<ShortMemoryEntry> {
337        Self::materialize(events, None, &ShortMemoryPolicy::default()).entries
338    }
339
340    pub fn materialize(
341        events: &[EventEnvelope],
342        current_run_id: Option<&RunId>,
343        policy: &ShortMemoryPolicy,
344    ) -> ShortMemoryMaterialization {
345        Self::materialize_with_protection(events, current_run_id, policy, true, &HashSet::new())
346    }
347
348    /// Project context before a model step inside an active run.
349    ///
350    /// Only the explicitly protected event tail is forced to `LOAD_ALL`.
351    /// Older completed batches in the same run remain eligible for TTL and
352    /// BatchKey disclosure. The protected tail is returned as canonical
353    /// projected entries; provider continuation is not a second history.
354    pub fn materialize_for_model_step(
355        events: &[EventEnvelope],
356        current_run_id: &RunId,
357        protected_event_ids: &HashSet<EventId>,
358        policy: &ShortMemoryPolicy,
359    ) -> ShortMemoryMaterialization {
360        Self::materialize_with_protection(
361            events,
362            Some(current_run_id),
363            policy,
364            false,
365            protected_event_ids,
366        )
367    }
368
369    fn materialize_with_protection(
370        events: &[EventEnvelope],
371        current_run_id: Option<&RunId>,
372        policy: &ShortMemoryPolicy,
373        protect_all_current_run_tools: bool,
374        protected_event_ids: &HashSet<EventId>,
375    ) -> ShortMemoryMaterialization {
376        // Session Management supplies inherited history before local history.
377        // Sequence values are only monotonic within one Session, so sorting a
378        // forked history by sequence would interleave unrelated timelines.
379        let visibility = Self::visibility(events, policy);
380        let visible_by_event_id: HashMap<&EventId, bool> = visibility
381            .iter()
382            .map(|decision| (&decision.event_id, decision.visible))
383            .collect();
384        let mut batches = build_batches(events);
385        assign_load_states(
386            &mut batches,
387            &visible_by_event_id,
388            current_run_id,
389            policy.recent_turns_load_all,
390            policy.batch_compaction_enabled,
391            protect_all_current_run_tools,
392            protected_event_ids,
393        );
394        if !protect_all_current_run_tools && let Some(current_run_id) = current_run_id {
395            for batch in &mut batches {
396                if batch.run_id.as_ref() == Some(current_run_id) {
397                    batch.raw_item_bytes = model_step_full_batch_item_bytes(&batch.events);
398                }
399            }
400        }
401        let key_admission = populate_key_content(&mut batches, policy);
402        let entries = materialize_entries(events, &batches);
403
404        ShortMemoryMaterialization {
405            entries,
406            batches,
407            visibility,
408            key_admission,
409        }
410    }
411}
412
413fn visibility_decisions(
414    events: &[EventEnvelope],
415    policy: &ShortMemoryPolicy,
416) -> Vec<EventVisibilityDecision> {
417    let floor_start = events.len().saturating_sub(policy.recency_floor);
418    let mut newer_class_counts = BTreeMap::<MemoryClass, u64>::new();
419    let mut newer_completion_counts = HashMap::<String, HashMap<MemoryClass, u64>>::new();
420    let mut decisions = Vec::with_capacity(events.len());
421
422    for (index, event) in events.iter().enumerate().rev() {
423        let traits = event_memory_traits(&event.event);
424        let ttl = policy.ttl_for(traits.class);
425        let mut accumulated_decay: i128 = newer_class_counts
426            .iter()
427            .map(|(newer, count)| {
428                i128::from(*count)
429                    * i128::from(
430                        policy
431                            .decay_cost(traits.class, *newer, DecayMatch::Any)
432                            .unwrap_or(1),
433                    )
434            })
435            .sum();
436        if let Some(relation_key) = &traits.relation_key
437            && let Some(completions) = newer_completion_counts.get(relation_key.as_str())
438        {
439            for (&newer_class, &completion_count) in completions {
440                if completion_count == 0 {
441                    continue;
442                }
443                let general_cost = policy
444                    .decay_cost(traits.class, newer_class, DecayMatch::Any)
445                    .unwrap_or(1);
446                let completion_cost = policy
447                    .decay_cost(traits.class, newer_class, DecayMatch::CompletesRelation)
448                    .unwrap_or(general_cost);
449                accumulated_decay += i128::from(completion_count)
450                    * (i128::from(completion_cost) - i128::from(general_cost));
451            }
452        }
453        let accumulated_decay = accumulated_decay.max(0) as u64;
454        let protected_by_recency_floor = index >= floor_start;
455        let within_ttl = ttl
456            .ttl_events
457            .is_none_or(|limit| accumulated_decay <= limit);
458        decisions.push(EventVisibilityDecision {
459            event_id: event.event_id.clone(),
460            sequence: event.sequence,
461            memory_class: traits.class,
462            relation_key: traits.relation_key.clone(),
463            accumulated_decay,
464            ttl_events: ttl.ttl_events,
465            pinned: ttl.pin,
466            protected_by_recency_floor,
467            visible: ttl.pin || protected_by_recency_floor || within_ttl,
468        });
469        *newer_class_counts.entry(traits.class).or_default() += 1;
470        if traits.completes_relation
471            && let Some(relation_key) = traits.relation_key
472        {
473            *newer_completion_counts
474                .entry(relation_key)
475                .or_default()
476                .entry(traits.class)
477                .or_default() += 1;
478        }
479    }
480
481    decisions.reverse();
482    decisions
483}
484
485fn build_batches(events: &[EventEnvelope]) -> Vec<EventBatch> {
486    let mut batches = Vec::<EventBatch>::new();
487    let mut batch_indexes = HashMap::<String, usize>::new();
488    let mut active_tool_by_run = HashMap::<String, String>::new();
489
490    for event in events {
491        // Governance is audit-only evidence, not agent memory or archive content.
492        if matches!(
493            event.event,
494            Event::ContextRunResolved { .. }
495                | Event::ContextRequestExposed { .. }
496                | Event::ContextItemUnfolded { .. }
497                | Event::ContextCallStarted { .. }
498                | Event::ContextCallRejected { .. }
499        ) {
500            continue;
501        }
502
503        let (context_key, context_kind) = batch_identity(event, &mut active_tool_by_run);
504        let index = if let Some(index) = batch_indexes.get(&context_key) {
505            *index
506        } else {
507            let index = batches.len();
508            batch_indexes.insert(context_key.clone(), index);
509            batches.push(EventBatch {
510                context_key,
511                context_kind,
512                run_id: event.run_id.clone(),
513                sequence_start: event.sequence,
514                sequence_end: event.sequence,
515                event_count: 0,
516                estimated_tokens: 0,
517                raw_item_bytes: 0,
518                key_content_budget_bytes: 0,
519                key_content: String::new(),
520                key_content_bytes: 0,
521                materialized_key_bytes: 0,
522                key_admission_rank: None,
523                key_admission: KeyAdmissionDecision::NotCandidate,
524                load_state: MemoryLoadState::NoLoad,
525                events: Vec::new(),
526            });
527            index
528        };
529        let batch = &mut batches[index];
530        batch.sequence_start = batch.sequence_start.min(event.sequence);
531        batch.sequence_end = batch.sequence_end.max(event.sequence);
532        batch.events.push(event.clone());
533    }
534
535    for batch in &mut batches {
536        batch.event_count = batch.events.len();
537        batch.estimated_tokens = estimate_tokens(&batch.events);
538        batch.raw_item_bytes = full_batch_item_bytes(&batch.events);
539    }
540    batches
541}
542
543fn populate_key_content(
544    batches: &mut [EventBatch],
545    policy: &ShortMemoryPolicy,
546) -> KeyAdmissionSummary {
547    let mut candidates = Vec::new();
548    let mut kept_full_no_benefit_batches = 0_usize;
549    for (index, batch) in batches.iter_mut().enumerate() {
550        if batch.load_state != MemoryLoadState::LoadKey {
551            continue;
552        }
553        batch.key_content_budget_bytes = policy.batch_key_content_limit_bytes.min(
554            batch
555                .raw_item_bytes
556                .saturating_mul(usize::from(policy.batch_target_compression_bps.min(10_000)))
557                / 10_000,
558        );
559        batch.key_content = build_key_content(batch, batch.key_content_budget_bytes);
560        batch.key_content_bytes = batch.key_content.len();
561        batch.materialized_key_bytes = materialized_batch_key_bytes(batch);
562        if batch.materialized_key_bytes >= batch.raw_item_bytes {
563            batch.key_admission = KeyAdmissionDecision::KeptFullNoBenefit;
564            batch.load_state = MemoryLoadState::LoadAll;
565            batch.key_content.clear();
566            kept_full_no_benefit_batches += 1;
567            continue;
568        }
569        candidates.push(index);
570    }
571    if policy.key_admission == KeyAdmissionPolicy::default() {
572        let admitted_key_content_bytes = candidates
573            .iter()
574            .copied()
575            .enumerate()
576            .map(|(rank, index)| {
577                let batch = &mut batches[index];
578                batch.key_admission_rank = Some(rank + 1);
579                batch.key_admission = KeyAdmissionDecision::Admitted;
580                batch.key_content_bytes
581            })
582            .sum();
583        return KeyAdmissionSummary {
584            policy: policy.key_admission,
585            candidate_batches: candidates.len(),
586            admitted_batches: candidates.len(),
587            rejected_batches: 0,
588            kept_full_no_benefit_batches,
589            admitted_key_content_bytes,
590        };
591    }
592    candidates.sort_by_key(|index| {
593        let batch = &batches[*index];
594        (
595            batch_evidence_priority(batch),
596            batch_kind_priority(batch.context_kind),
597            Reverse(*index),
598        )
599    });
600
601    let mut admitted_batches = 0_usize;
602    let mut admitted_key_content_bytes = 0_usize;
603    for (rank, index) in candidates.iter().copied().enumerate() {
604        let batch = &mut batches[index];
605        batch.key_admission_rank = Some(rank + 1);
606        let exceeds_batch_limit = policy
607            .key_admission
608            .max_key_batches
609            .is_some_and(|limit| admitted_batches >= limit);
610        let exceeds_byte_limit = policy
611            .key_admission
612            .max_key_content_bytes
613            .is_some_and(|limit| {
614                admitted_key_content_bytes.saturating_add(batch.key_content_bytes) > limit
615            });
616        batch.key_admission = if exceeds_batch_limit {
617            KeyAdmissionDecision::RejectedBatchLimit
618        } else if exceeds_byte_limit {
619            KeyAdmissionDecision::RejectedByteLimit
620        } else {
621            admitted_batches += 1;
622            admitted_key_content_bytes += batch.key_content_bytes;
623            KeyAdmissionDecision::Admitted
624        };
625        if batch.key_admission != KeyAdmissionDecision::Admitted {
626            batch.load_state = MemoryLoadState::NoLoad;
627            batch.key_content.clear();
628        }
629    }
630
631    KeyAdmissionSummary {
632        policy: policy.key_admission,
633        candidate_batches: candidates.len(),
634        admitted_batches,
635        rejected_batches: candidates.len().saturating_sub(admitted_batches),
636        kept_full_no_benefit_batches,
637        admitted_key_content_bytes,
638    }
639}
640
641fn batch_evidence_priority(batch: &EventBatch) -> u8 {
642    batch
643        .events
644        .iter()
645        .map(|event| match event_memory_traits(&event.event).class {
646            MemoryClass::Anchor => 0,
647            MemoryClass::Recovery
648            | MemoryClass::ToolMutation
649            | MemoryClass::ToolBuild
650            | MemoryClass::ToolDependency => 1,
651            MemoryClass::ToolValidation => 2,
652            MemoryClass::Working => 3,
653            MemoryClass::ToolInspection => 4,
654            MemoryClass::Control => 5,
655            MemoryClass::Transient => 6,
656        })
657        .min()
658        .unwrap_or(7)
659}
660
661fn batch_kind_priority(kind: MemoryBatchKind) -> u8 {
662    match kind {
663        MemoryBatchKind::Turn => 0,
664        MemoryBatchKind::Reasoning => 1,
665        MemoryBatchKind::Context => 2,
666        MemoryBatchKind::Tool => 3,
667        MemoryBatchKind::Task => 4,
668        MemoryBatchKind::Artifact => 5,
669        MemoryBatchKind::Transient => 6,
670        MemoryBatchKind::Misc => 7,
671    }
672}
673
674fn batch_identity(
675    envelope: &EventEnvelope,
676    active_tool_by_run: &mut HashMap<String, String>,
677) -> (String, MemoryBatchKind) {
678    let run = envelope
679        .run_id
680        .as_ref()
681        .map_or_else(|| "none".to_owned(), ToString::to_string);
682    match &envelope.event {
683        Event::ContextRunResolved { .. }
684        | Event::ContextRequestExposed { .. }
685        | Event::ContextItemUnfolded { .. }
686        | Event::ContextCallStarted { .. }
687        | Event::ContextCallRejected { .. } => (
688            format!("run:{run}:context-audit"),
689            MemoryBatchKind::Transient,
690        ),
691        Event::MessageAccepted { .. }
692        | Event::RunScheduled
693        | Event::RunStarted
694        | Event::RunCompleted { .. }
695        | Event::RunFailed { .. }
696        | Event::AgentProgressAdvisory { .. }
697        | Event::AgentLoopTerminated { .. }
698        | Event::TerminalControlTransition { .. }
699        | Event::RunCancelled => (format!("run:{run}:turn:1"), MemoryBatchKind::Turn),
700        Event::ModelResponseItem {
701            model_step,
702            item_index,
703            item: arabica_model::RuntimeItem::Reasoning(_),
704        } => (
705            format!("run:{run}:reasoning:{model_step}:{item_index}"),
706            MemoryBatchKind::Reasoning,
707        ),
708        Event::ModelResponseItem {
709            model_step,
710            item_index,
711            ..
712        } => (
713            format!("run:{run}:model-response:{model_step}:{item_index}"),
714            MemoryBatchKind::Transient,
715        ),
716        Event::ModelResponseCompleted { model_step, .. } => (
717            format!("run:{run}:model-response:{model_step}:completed"),
718            MemoryBatchKind::Transient,
719        ),
720        Event::ModelResponseRejected { model_step, .. } => (
721            format!("run:{run}:model-response:{model_step}:rejected"),
722            MemoryBatchKind::Transient,
723        ),
724        Event::ModelResponseNormalized { model_step, .. } => (
725            format!("run:{run}:model-response:{model_step}:normalized"),
726            MemoryBatchKind::Transient,
727        ),
728        Event::ModelRequestPrepared { model_step, .. } => (
729            format!("run:{run}:model-request:{model_step}"),
730            MemoryBatchKind::Transient,
731        ),
732        Event::ModelRouteSelected { model_step, .. }
733        | Event::ModelRouteExplained { model_step, .. }
734        | Event::ModelCallObserved { model_step, .. }
735        | Event::PlanDelegationObserved { model_step, .. } => (
736            format!("run:{run}:model-routing:{model_step}"),
737            MemoryBatchKind::Transient,
738        ),
739        Event::ToolCallRequested { call_id, .. } => {
740            active_tool_by_run.insert(run.clone(), call_id.clone());
741            (format!("run:{run}:tool:{call_id}"), MemoryBatchKind::Tool)
742        }
743        Event::ToolCallClassified { call_id, .. }
744        | Event::ToolCallPermissionRequested { call_id }
745        | Event::ToolCallPermissionResolved { call_id, .. }
746        | Event::ToolCallReused { call_id, .. }
747        | Event::ToolCallLoopBlocked { call_id, .. }
748        | Event::ToolExecutionObserved { call_id, .. } => {
749            (format!("run:{run}:tool:{call_id}"), MemoryBatchKind::Tool)
750        }
751        Event::ToolCallCompleted { call_id, .. } => {
752            if active_tool_by_run.get(&run) == Some(call_id) {
753                active_tool_by_run.remove(&run);
754            }
755            (format!("run:{run}:tool:{call_id}"), MemoryBatchKind::Tool)
756        }
757        Event::PlanUpdated { .. } => (format!("run:{run}:plan"), MemoryBatchKind::Task),
758        Event::CommandOutput { .. } => active_tool_by_run.get(&run).map_or_else(
759            || {
760                (
761                    format!(
762                        "run:{run}:transient:{}",
763                        envelope.sequence / MISC_BUCKET_SIZE
764                    ),
765                    MemoryBatchKind::Transient,
766                )
767            },
768            |call_id| (format!("run:{run}:tool:{call_id}"), MemoryBatchKind::Tool),
769        ),
770        Event::ContextRead { entry } | Event::ContextUpdated { entry } => (
771            format!("context:{}:{}", envelope.workspace_id, entry.path),
772            MemoryBatchKind::Context,
773        ),
774        Event::ContextDeleted { path } => (
775            format!("context:{}:{path}", envelope.workspace_id),
776            MemoryBatchKind::Context,
777        ),
778        Event::ContextSearchResult { .. } => (
779            format!(
780                "context:{}:search:{}",
781                envelope.workspace_id, envelope.sequence
782            ),
783            MemoryBatchKind::Context,
784        ),
785        Event::ContextDisclosureSet { .. } => (
786            format!("session:{}:control", envelope.session_id),
787            MemoryBatchKind::Misc,
788        ),
789        Event::SessionCreated { .. }
790        | Event::SessionForked { .. }
791        | Event::SessionResumed
792        | Event::SessionSuspended
793        | Event::SessionClosed => (
794            format!("session:{}:lifecycle", envelope.session_id),
795            MemoryBatchKind::Misc,
796        ),
797        Event::Error { .. } if envelope.run_id.is_some() => {
798            (format!("run:{run}:turn:1"), MemoryBatchKind::Turn)
799        }
800        Event::Error { .. } => (
801            format!(
802                "session:{}:error:{}",
803                envelope.session_id, envelope.sequence
804            ),
805            MemoryBatchKind::Misc,
806        ),
807    }
808}
809
810fn assign_load_states(
811    batches: &mut [EventBatch],
812    visible_by_event_id: &HashMap<&EventId, bool>,
813    current_run_id: Option<&RunId>,
814    recent_turn_count: usize,
815    batch_compaction_enabled: bool,
816    protect_all_current_run_tools: bool,
817    protected_event_ids: &HashSet<EventId>,
818) {
819    let turn_indexes: Vec<_> = batches
820        .iter()
821        .enumerate()
822        .filter(|(_, batch)| batch.context_kind == MemoryBatchKind::Turn)
823        .map(|(index, _)| index)
824        .collect();
825    let recent_start = turn_indexes.len().saturating_sub(recent_turn_count);
826    let recent_turn_indexes = &turn_indexes[recent_start..];
827
828    for (index, batch) in batches.iter_mut().enumerate() {
829        let has_visible_event = batch.events.iter().any(|event| {
830            visible_by_event_id
831                .get(&event.event_id)
832                .copied()
833                .unwrap_or(false)
834        });
835        let explicitly_protected = batch
836            .events
837            .iter()
838            .any(|event| protected_event_ids.contains(&event.event_id));
839        batch.load_state = if explicitly_protected {
840            MemoryLoadState::LoadAll
841        } else if !has_visible_event {
842            MemoryLoadState::NoLoad
843        } else if !batch_compaction_enabled {
844            MemoryLoadState::LoadAll
845        } else if matches!(
846            batch.context_kind,
847            MemoryBatchKind::Transient | MemoryBatchKind::Misc
848        ) {
849            MemoryLoadState::NoLoad
850        } else if recent_turn_indexes.contains(&index)
851            || (protect_all_current_run_tools
852                && batch.context_kind == MemoryBatchKind::Tool
853                && current_run_id.is_some_and(|run_id| batch.run_id.as_ref() == Some(run_id)))
854        {
855            MemoryLoadState::LoadAll
856        } else {
857            MemoryLoadState::LoadKey
858        };
859    }
860}
861
862fn materialize_entries(events: &[EventEnvelope], batches: &[EventBatch]) -> Vec<ShortMemoryEntry> {
863    let mut state_by_event_id = HashMap::<EventId, (&EventBatch, bool)>::new();
864    for batch in batches {
865        for (index, event) in batch.events.iter().enumerate() {
866            state_by_event_id.insert(event.event_id.clone(), (batch, index == 0));
867        }
868    }
869
870    let mut entries = Vec::new();
871    for event in events {
872        let Some((batch, first_in_batch)) = state_by_event_id.get(&event.event_id) else {
873            continue;
874        };
875        match batch.load_state {
876            MemoryLoadState::LoadAll => {
877                entries.extend(event_to_short_memory_entries(event));
878            }
879            MemoryLoadState::LoadKey if *first_in_batch => {
880                entries.push(ShortMemoryEntry {
881                    source_event_ids: batch
882                        .events
883                        .iter()
884                        .map(|event| event.event_id.to_string())
885                        .collect(),
886                    sequence: batch.sequence_start,
887                    item: batch_key_item(batch),
888                });
889            }
890            MemoryLoadState::LoadKey | MemoryLoadState::NoLoad => {}
891        }
892    }
893    entries
894}
895
896fn event_to_short_memory_entries(envelope: &EventEnvelope) -> Vec<ShortMemoryEntry> {
897    if let Event::ModelResponseItem { item, .. } = &envelope.event {
898        let item = match item {
899            arabica_model::RuntimeItem::Reasoning(reasoning) => {
900                Some(ShortMemoryItem::Reasoning(reasoning.clone()))
901            }
902            arabica_model::RuntimeItem::Message(message) => {
903                Some(ShortMemoryItem::ProviderMessage(message.clone()))
904            }
905            // Tool calls are projected from tool.call.requested so their
906            // execution relation remains one stable batch. That event carries
907            // the exact same provider state.
908            arabica_model::RuntimeItem::ToolCall(_) | arabica_model::RuntimeItem::ToolResult(_) => {
909                None
910            }
911        };
912        if let Some(item) = item {
913            return vec![ShortMemoryEntry {
914                source_event_ids: vec![envelope.event_id.to_string()],
915                sequence: envelope.sequence,
916                item,
917            }];
918        }
919    }
920    event_to_short_memory(envelope).into_iter().collect()
921}
922
923fn event_to_short_memory(envelope: &EventEnvelope) -> Option<ShortMemoryEntry> {
924    let item = match &envelope.event {
925        Event::MessageAccepted { content, images } => ShortMemoryItem::UserMessage {
926            content: content.clone(),
927            images: images.clone(),
928        },
929        Event::RunCompleted {
930            output: Some(content),
931        } => ShortMemoryItem::AssistantMessage {
932            content: content.clone(),
933        },
934        Event::ToolCallRequested {
935            call_id,
936            name,
937            arguments,
938            provider_state,
939        } => ShortMemoryItem::ToolCall(ToolCallItem {
940            id: Some(envelope.event_id.to_string()),
941            call_id: call_id.clone(),
942            name: name.clone(),
943            arguments: arguments.clone(),
944            provider_state: provider_state.clone(),
945        }),
946        Event::ToolCallCompleted {
947            call_id,
948            name,
949            result,
950            is_error,
951        } => ShortMemoryItem::ToolResult(ToolResultItem {
952            id: Some(envelope.event_id.to_string()),
953            call_id: call_id.clone(),
954            name: Some(name.clone()),
955            content: vec![ContentBlock::text(result.clone())],
956            is_error: *is_error,
957        }),
958        Event::CommandOutput { stream, chunk } => ShortMemoryItem::Observation {
959            content: format!("stream={stream:?}\n{chunk}"),
960        },
961        Event::RunFailed { message } | Event::Error { message, .. } => {
962            ShortMemoryItem::RunFailure {
963                message: message.clone(),
964            }
965        }
966        Event::AgentProgressAdvisory { message, .. } => ShortMemoryItem::Observation {
967            content: message.clone(),
968        },
969        Event::PlanUpdated { plan_id, entries } => ShortMemoryItem::Observation {
970            content: format!(
971                "plan_id={plan_id}\n{}",
972                serde_json::to_string(entries).unwrap_or_default()
973            ),
974        },
975        Event::RunCancelled => ShortMemoryItem::RunCancelled,
976        Event::ContextRead { entry } | Event::ContextUpdated { entry } => {
977            ShortMemoryItem::Observation {
978                content: format!("context_path={}\n{}", entry.path, entry.content),
979            }
980        }
981        Event::ContextSearchResult { entries } => ShortMemoryItem::Observation {
982            content: entries
983                .iter()
984                .map(|entry| format!("[{}]\n{}", entry.path, entry.content))
985                .collect::<Vec<_>>()
986                .join("\n\n"),
987        },
988        Event::ContextDeleted { path } => ShortMemoryItem::Observation {
989            content: format!("context_deleted={path}"),
990        },
991        Event::ContextDisclosureSet { level } => ShortMemoryItem::Observation {
992            content: format!("context_disclosure={level:?}"),
993        },
994        Event::SessionCreated { .. }
995        | Event::SessionForked { .. }
996        | Event::SessionResumed
997        | Event::SessionSuspended
998        | Event::SessionClosed
999        | Event::RunScheduled
1000        | Event::RunStarted
1001        | Event::ContextRunResolved { .. }
1002        | Event::ContextRequestExposed { .. }
1003        | Event::ContextItemUnfolded { .. }
1004        | Event::ContextCallStarted { .. }
1005        | Event::ContextCallRejected { .. }
1006        | Event::ModelRouteSelected { .. }
1007        | Event::ModelRouteExplained { .. }
1008        | Event::ToolExecutionObserved { .. }
1009        | Event::ModelCallObserved { .. }
1010        | Event::PlanDelegationObserved { .. }
1011        | Event::ModelRequestPrepared { .. }
1012        | Event::ModelResponseItem { .. }
1013        | Event::ModelResponseCompleted { .. }
1014        | Event::ModelResponseRejected { .. }
1015        | Event::ModelResponseNormalized { .. }
1016        | Event::ToolCallClassified { .. }
1017        | Event::ToolCallPermissionRequested { .. }
1018        | Event::ToolCallPermissionResolved { .. }
1019        | Event::ToolCallReused { .. }
1020        | Event::ToolCallLoopBlocked { .. }
1021        | Event::AgentLoopTerminated { .. }
1022        | Event::TerminalControlTransition { .. }
1023        | Event::RunCompleted { output: None } => return None,
1024    };
1025    Some(ShortMemoryEntry {
1026        source_event_ids: vec![envelope.event_id.to_string()],
1027        sequence: envelope.sequence,
1028        item,
1029    })
1030}
1031
1032/// Reconstruct short memory for every run OTHER than `current_run_id` by
1033/// replaying each run's own typed model exchange and tool-call Events,
1034/// instead of expanding TTL/batch-projected event batches. This is what
1035/// [`crate::HistoryProjection::ExactTranscript`] substitutes for the
1036/// `short_memory` half of [`ShortMemoryProjector::materialize_for_model_step`]'s
1037/// output; the current run's own exchange is unaffected and continues to be
1038/// reconstructed separately by `exact_run_continuation`.
1039///
1040/// Two rules apply only here, not in [`event_to_short_memory`]:
1041///
1042/// - A tool call is kept only if the SAME run also recorded its completion.
1043///   A run can end (`run.failed`, `run.cancelled`) after a call was
1044///   requested but before it completed -- for example the Runner
1045///   Environment's own hard-error path, which ends a run without appending a
1046///   matching `tool.call.completed` -- and such a call must not become an
1047///   orphaned assistant `tool_calls` entry with nothing to answer it.
1048/// - A run's `run.completed` output is represented only if that run never
1049///   recorded a `model.response.item` message. The two otherwise carry the
1050///   same text (every scripted response that populates one populates both;
1051///   see this crate's own `text_response` test helper), and only one copy
1052///   may reach the model.
1053///
1054/// `command.output` Events are never materialized here at all: expanding
1055/// them is what lets a `system` message land between an assistant
1056/// `tool_calls` message and its `tool` result under the batch/TTL
1057/// projection, which most OpenAI-compatible encoders reject.
1058pub fn exact_transcript_entries(
1059    events: &[EventEnvelope],
1060    current_run_id: &RunId,
1061) -> Vec<ShortMemoryEntry> {
1062    let mut completed_call_ids: HashMap<RunId, HashSet<String>> = HashMap::new();
1063    let mut has_message: HashMap<RunId, bool> = HashMap::new();
1064    for envelope in events {
1065        let Some(run_id) = envelope.run_id.as_ref() else {
1066            continue;
1067        };
1068        if run_id == current_run_id {
1069            continue;
1070        }
1071        match &envelope.event {
1072            Event::ToolCallCompleted { call_id, .. } => {
1073                completed_call_ids
1074                    .entry(run_id.clone())
1075                    .or_default()
1076                    .insert(call_id.clone());
1077            }
1078            Event::ModelResponseItem {
1079                item: RuntimeItem::Message(_),
1080                ..
1081            } => {
1082                has_message.insert(run_id.clone(), true);
1083            }
1084            _ => {}
1085        }
1086    }
1087
1088    let mut entries = Vec::new();
1089    for envelope in events {
1090        let Some(run_id) = envelope.run_id.as_ref() else {
1091            continue;
1092        };
1093        if run_id == current_run_id {
1094            continue;
1095        }
1096        match &envelope.event {
1097            Event::ToolCallRequested { call_id, .. } => {
1098                let kept = completed_call_ids
1099                    .get(run_id)
1100                    .is_some_and(|ids| ids.contains(call_id));
1101                if kept {
1102                    entries.extend(event_to_short_memory(envelope));
1103                }
1104            }
1105            Event::ModelResponseItem {
1106                item: RuntimeItem::Message(message),
1107                ..
1108            } => {
1109                entries.push(ShortMemoryEntry {
1110                    source_event_ids: vec![envelope.event_id.to_string()],
1111                    sequence: envelope.sequence,
1112                    item: ShortMemoryItem::ProviderMessage(message.clone()),
1113                });
1114            }
1115            Event::RunCompleted { output: Some(_) }
1116                if has_message.get(run_id).copied().unwrap_or(false) => {}
1117            Event::CommandOutput { .. } => {}
1118            _ => entries.extend(event_to_short_memory(envelope)),
1119        }
1120    }
1121    entries
1122}
1123
1124fn tool_interaction_memory_class(kind: ToolInteractionKind) -> MemoryClass {
1125    match kind {
1126        ToolInteractionKind::Plan => MemoryClass::Working,
1127        ToolInteractionKind::Inspection => MemoryClass::ToolInspection,
1128        ToolInteractionKind::Mutation | ToolInteractionKind::MutationWithValidation => {
1129            MemoryClass::ToolMutation
1130        }
1131        ToolInteractionKind::Build => MemoryClass::ToolBuild,
1132        ToolInteractionKind::Dependency => MemoryClass::ToolDependency,
1133        ToolInteractionKind::Validation => MemoryClass::ToolValidation,
1134        ToolInteractionKind::Generic => MemoryClass::Working,
1135    }
1136}
1137
1138fn event_memory_traits(event: &Event) -> EventMemoryTraits {
1139    let (class, relation_key, completes_relation) = match event {
1140        Event::SessionCreated { .. }
1141        | Event::SessionForked { .. }
1142        | Event::SessionResumed
1143        | Event::SessionSuspended
1144        | Event::SessionClosed => (MemoryClass::Control, None, false),
1145        Event::RunScheduled | Event::RunStarted | Event::RunCancelled => {
1146            (MemoryClass::Control, None, false)
1147        }
1148        Event::MessageAccepted { .. } => (MemoryClass::Anchor, None, false),
1149        Event::ModelResponseItem {
1150            item: arabica_model::RuntimeItem::Reasoning(_),
1151            ..
1152        } => (MemoryClass::Working, None, false),
1153        Event::ModelResponseItem { .. }
1154        | Event::ModelResponseCompleted { .. }
1155        | Event::ModelResponseRejected { .. }
1156        | Event::ModelResponseNormalized { .. } => (MemoryClass::Control, None, false),
1157        Event::ModelRequestPrepared { .. }
1158        | Event::ContextRunResolved { .. }
1159        | Event::ContextRequestExposed { .. }
1160        | Event::ContextItemUnfolded { .. }
1161        | Event::ContextCallStarted { .. }
1162        | Event::ContextCallRejected { .. }
1163        | Event::ModelRouteSelected { .. }
1164        | Event::ModelRouteExplained { .. }
1165        | Event::ToolExecutionObserved { .. }
1166        | Event::ModelCallObserved { .. }
1167        | Event::PlanDelegationObserved { .. } => (MemoryClass::Control, None, false),
1168        Event::ToolCallRequested { call_id, .. } => {
1169            (MemoryClass::Working, Some(format!("tool:{call_id}")), false)
1170        }
1171        Event::ToolCallClassified { call_id, kind } => (
1172            tool_interaction_memory_class(*kind),
1173            Some(format!("tool:{call_id}")),
1174            false,
1175        ),
1176        Event::PlanUpdated { .. } => (MemoryClass::Anchor, Some("agent:plan".to_owned()), false),
1177        Event::ToolCallReused { call_id, .. } | Event::ToolCallLoopBlocked { call_id, .. } => {
1178            (MemoryClass::Working, Some(format!("tool:{call_id}")), false)
1179        }
1180        Event::ToolCallPermissionRequested { call_id }
1181        | Event::ToolCallPermissionResolved { call_id, .. } => {
1182            (MemoryClass::Control, Some(format!("tool:{call_id}")), false)
1183        }
1184        Event::ToolCallCompleted {
1185            call_id,
1186            is_error: true,
1187            ..
1188        } => (MemoryClass::Recovery, Some(format!("tool:{call_id}")), true),
1189        Event::ToolCallCompleted {
1190            call_id,
1191            is_error: false,
1192            ..
1193        } => (MemoryClass::Working, Some(format!("tool:{call_id}")), true),
1194        Event::CommandOutput { .. } => (MemoryClass::Transient, None, false),
1195        Event::RunCompleted { output: Some(_) } => (MemoryClass::Anchor, None, false),
1196        Event::RunCompleted { output: None } => (MemoryClass::Control, None, false),
1197        Event::AgentProgressAdvisory { .. } => (MemoryClass::Control, None, false),
1198        Event::TerminalControlTransition { .. } => (MemoryClass::Control, None, false),
1199        Event::RunFailed { .. } | Event::AgentLoopTerminated { .. } | Event::Error { .. } => {
1200            (MemoryClass::Recovery, None, false)
1201        }
1202        Event::ContextRead { entry } => (
1203            MemoryClass::Working,
1204            Some(format!("context:{}", entry.path)),
1205            false,
1206        ),
1207        Event::ContextSearchResult { .. } => (MemoryClass::Working, None, false),
1208        Event::ContextUpdated { entry } => (
1209            MemoryClass::Anchor,
1210            Some(format!("context:{}", entry.path)),
1211            false,
1212        ),
1213        Event::ContextDeleted { path } => {
1214            (MemoryClass::Anchor, Some(format!("context:{path}")), false)
1215        }
1216        Event::ContextDisclosureSet { .. } => (MemoryClass::Control, None, false),
1217    };
1218    EventMemoryTraits {
1219        class,
1220        relation_key,
1221        completes_relation,
1222    }
1223}
1224
1225fn event_type_name(event: &Event) -> &'static str {
1226    match event {
1227        Event::ContextRunResolved { .. } => "context.run.resolved",
1228        Event::ContextRequestExposed { .. } => "context.request.exposed",
1229        Event::ContextItemUnfolded { .. } => "context.item.unfolded",
1230        Event::ContextCallStarted { .. } => "context.call.started",
1231        Event::ContextCallRejected { .. } => "context.call.rejected",
1232        Event::SessionCreated { .. } => "session.created",
1233        Event::SessionForked { .. } => "session.forked",
1234        Event::SessionResumed => "session.resumed",
1235        Event::SessionSuspended => "session.suspended",
1236        Event::SessionClosed => "session.closed",
1237        Event::RunScheduled => "run.scheduled",
1238        Event::RunStarted => "run.started",
1239        Event::MessageAccepted { .. } => "message.accepted",
1240        Event::ModelRouteSelected { .. } => "model.route.selected",
1241        Event::ModelRouteExplained { .. } => "model.route.explained",
1242        Event::ModelCallObserved { .. } => "model.call.observed",
1243        Event::ToolExecutionObserved { .. } => "tool.execution.observed",
1244        Event::ModelRequestPrepared { .. } => "model.request.prepared",
1245        Event::ModelResponseItem { .. } => "model.response.item",
1246        Event::ModelResponseCompleted { .. } => "model.response.completed",
1247        Event::ModelResponseRejected { .. } => "model.response.rejected",
1248        Event::ModelResponseNormalized { .. } => "model.response.normalized",
1249        Event::ToolCallRequested { .. } => "tool.call.requested",
1250        Event::ToolCallClassified { .. } => "tool.call.classified",
1251        Event::PlanUpdated { .. } => "plan.updated",
1252        Event::PlanDelegationObserved { .. } => "plan.delegation.observed",
1253        Event::ToolCallPermissionRequested { .. } => "tool.call.permission_requested",
1254        Event::ToolCallPermissionResolved { .. } => "tool.call.permission_resolved",
1255        Event::ToolCallReused { .. } => "tool.call.reused",
1256        Event::ToolCallLoopBlocked { .. } => "tool.call.loop_blocked",
1257        Event::ToolCallCompleted { is_error: true, .. } => "tool.call.error",
1258        Event::ToolCallCompleted {
1259            is_error: false, ..
1260        } => "tool.call.completed",
1261        Event::AgentProgressAdvisory { .. } => "agent.progress.advisory",
1262        Event::AgentLoopTerminated { .. } => "agent.loop.terminated",
1263        Event::TerminalControlTransition { .. } => "terminal.control.transition",
1264        Event::CommandOutput { .. } => "command.output",
1265        Event::RunCompleted { .. } => "run.completed",
1266        Event::RunFailed { .. } => "run.failed",
1267        Event::RunCancelled => "run.cancelled",
1268        Event::ContextRead { .. } => "context.read",
1269        Event::ContextSearchResult { .. } => "context.search.result",
1270        Event::ContextUpdated { .. } => "context.updated",
1271        Event::ContextDeleted { .. } => "context.deleted",
1272        Event::ContextDisclosureSet { .. } => "context.disclosure.set",
1273        Event::Error { .. } => "error",
1274    }
1275}
1276
1277fn estimate_tokens(events: &[EventEnvelope]) -> u64 {
1278    let characters: usize = events
1279        .iter()
1280        .map(|event| serde_json::to_string(&event.event).map_or(0, |text| text.chars().count()))
1281        .sum();
1282    characters.div_ceil(4) as u64
1283}
1284
1285fn full_batch_item_bytes(events: &[EventEnvelope]) -> usize {
1286    let items: Vec<_> = events
1287        .iter()
1288        .flat_map(event_to_short_memory_entries)
1289        .map(|entry| entry.item)
1290        .collect();
1291    serde_json::to_vec(&items).map_or(usize::MAX, |encoded| encoded.len())
1292}
1293
1294fn model_step_full_batch_item_bytes(events: &[EventEnvelope]) -> usize {
1295    let items: Vec<_> = events
1296        .iter()
1297        .flat_map(event_to_short_memory_entries)
1298        .map(|entry| entry.item)
1299        .filter(|item| {
1300            !matches!(
1301                item,
1302                ShortMemoryItem::UserMessage { .. } | ShortMemoryItem::Observation { .. }
1303            )
1304        })
1305        .collect();
1306    serde_json::to_vec(&items).map_or(usize::MAX, |encoded| encoded.len())
1307}
1308
1309fn materialized_batch_key_bytes(batch: &EventBatch) -> usize {
1310    serde_json::to_vec(&batch_key_item(batch)).map_or(usize::MAX, |encoded| encoded.len())
1311}
1312
1313fn batch_key_item(batch: &EventBatch) -> ShortMemoryItem {
1314    ShortMemoryItem::BatchKey(MemoryBatchKey {
1315        context_key: batch.context_key.clone(),
1316        context_kind: batch.context_kind,
1317        sequence_start: batch.sequence_start,
1318        sequence_end: batch.sequence_end,
1319        event_count: batch.event_count,
1320        estimated_tokens: batch.estimated_tokens,
1321        key_content: batch.key_content.clone(),
1322    })
1323}
1324
1325fn build_key_content(batch: &EventBatch, content_budget_bytes: usize) -> String {
1326    let mut type_counts = BTreeMap::<&str, usize>::new();
1327    for event in &batch.events {
1328        *type_counts
1329            .entry(event_type_name(&event.event))
1330            .or_default() += 1;
1331    }
1332    let mut lines = vec![
1333        format!(
1334            "batch={} kind={:?} seq={}-{} events={} tokens={}",
1335            truncate_chars(&batch.context_key, BATCH_FIELD_EXCERPT_LIMIT),
1336            batch.context_kind,
1337            batch.sequence_start,
1338            batch.sequence_end,
1339            batch.events.len(),
1340            batch.estimated_tokens,
1341        )
1342        .to_lowercase(),
1343        format!(
1344            "types={}",
1345            type_counts
1346                .iter()
1347                .map(|(event_type, count)| format!("{event_type}:{count}"))
1348                .collect::<Vec<_>>()
1349                .join(",")
1350        ),
1351    ];
1352    let mut semantic_events: Vec<_> = batch.events.iter().collect();
1353    if batch.context_kind == MemoryBatchKind::Tool {
1354        semantic_events
1355            .sort_by_key(|event| !matches!(event.event, Event::ToolCallCompleted { .. }));
1356    }
1357    for event in semantic_events {
1358        if let Some(line) = event_semantic_key(&event.event) {
1359            lines.push(line);
1360        }
1361    }
1362    truncate_utf8_bytes(&lines.join("\n"), content_budget_bytes)
1363}
1364
1365fn event_semantic_key(event: &Event) -> Option<String> {
1366    match event {
1367        Event::MessageAccepted { content, .. } => Some(compact_user_message(content)),
1368        Event::ModelRouteSelected { .. }
1369        | Event::ModelRouteExplained { .. }
1370        | Event::ToolExecutionObserved { .. }
1371        | Event::ModelCallObserved { .. } => None,
1372        Event::ContextRunResolved { .. } | Event::ContextRequestExposed { .. }
1373        | Event::ContextItemUnfolded { .. } | Event::ContextCallStarted { .. }
1374        | Event::ContextCallRejected { .. } => None,
1375        Event::ModelResponseItem {
1376            model_step,
1377            item_index,
1378            item,
1379        } => Some(format!(
1380            "model_response_item step={model_step} index={item_index} kind={}",
1381            match item {
1382                arabica_model::RuntimeItem::Message(_) => "message",
1383                arabica_model::RuntimeItem::ToolCall(_) => "tool_call",
1384                arabica_model::RuntimeItem::ToolResult(_) => "tool_result",
1385                arabica_model::RuntimeItem::Reasoning(_) => "reasoning",
1386            },
1387        )),
1388        Event::ModelResponseCompleted {
1389            model_step,
1390            finish_reason,
1391            usage,
1392            ..
1393        } => Some(format!(
1394            "model_response_completed step={model_step} finish={finish_reason:?} input_tokens={} output_tokens={} cached_input_tokens={}",
1395            usage.input_tokens, usage.output_tokens, usage.cached_input_tokens,
1396        )),
1397        Event::ModelResponseRejected {
1398            model_step,
1399            reason,
1400            finish_reason,
1401            tool_call_count,
1402            final_output_present,
1403        } => Some(
1404            format!(
1405                "model_response_rejected step={model_step} reason={reason:?} finish={finish_reason:?} tool_calls={tool_call_count} final_output_present={final_output_present}"
1406            )
1407            .to_lowercase(),
1408        ),
1409        Event::ModelResponseNormalized {
1410            model_step,
1411            policy,
1412            ignored_assistant_text,
1413        } => Some(
1414            format!(
1415                "model_response_normalized step={model_step} policy={policy:?} ignored_assistant_text={ignored_assistant_text}"
1416            )
1417            .to_lowercase(),
1418        ),
1419        Event::ModelRequestPrepared {
1420            model_step,
1421            request,
1422        } => Some(format!(
1423            "model_request step={model_step} model={} items={} tools={} tool_choice={:?}",
1424            request.model,
1425            request.items.len(),
1426            request.tools.len(),
1427            request.tool_choice,
1428        )),
1429        Event::RunCompleted {
1430            output: Some(content),
1431        } => Some(compact_text("assistant", content)),
1432        Event::ToolCallRequested {
1433            call_id,
1434            name,
1435            arguments,
1436            ..
1437        } => {
1438            let arguments = serde_json::to_string(arguments).unwrap_or_default();
1439            Some(format!(
1440                "tool_call name={name} call_id={call_id} {}",
1441                compact_text("arguments", &arguments)
1442            ))
1443        }
1444        Event::ToolCallClassified { call_id, kind } => {
1445            Some(format!("tool_class call_id={call_id} kind={kind:?}").to_lowercase())
1446        }
1447        Event::PlanUpdated { plan_id, entries } => Some(format!(
1448            "plan_updated id={plan_id} entries={}",
1449            entries.iter().map(|entry| entry.content.as_str()).collect::<Vec<_>>().join(" | ")
1450        )),
1451        Event::PlanDelegationObserved { model_step, model_alias, succeeded, .. } => Some(format!(
1452            "plan_delegation step={model_step} model={model_alias} succeeded={succeeded}"
1453        )),
1454        Event::ToolCallPermissionRequested { call_id } => {
1455            Some(format!("tool_permission call_id={call_id} state=requested"))
1456        }
1457        Event::ToolCallPermissionResolved {
1458            call_id,
1459            outcome,
1460            scope,
1461            source,
1462        } => Some(
1463            format!(
1464                "tool_permission call_id={call_id} outcome={outcome:?} scope={scope:?} source={source:?}"
1465            )
1466            .to_lowercase(),
1467        ),
1468        Event::ToolCallReused {
1469            call_id,
1470            source_call_id,
1471            fingerprint,
1472            repeat_count,
1473        } => Some(format!(
1474            "tool_reused call_id={call_id} source_call_id={source_call_id} fingerprint={fingerprint} repeat_count={repeat_count}"
1475        )),
1476        Event::ToolCallLoopBlocked {
1477            call_id,
1478            fingerprint,
1479            repeat_count,
1480        } => Some(format!(
1481            "tool_loop_blocked call_id={call_id} fingerprint={fingerprint} repeat_count={repeat_count}"
1482        )),
1483        Event::ToolCallCompleted {
1484            call_id,
1485            name,
1486            result,
1487            is_error,
1488        } => Some(format!(
1489            "tool_result name={name} call_id={call_id} status={} {}",
1490            if *is_error { "error" } else { "ok" },
1491            compact_text("result", result)
1492        )),
1493        Event::RunFailed { message } => Some(compact_text("run_failure", message)),
1494        Event::AgentProgressAdvisory {
1495            model_step,
1496            consecutive_no_progress_steps,
1497            message,
1498        } => Some(format!(
1499            "agent_progress_advisory step={model_step} consecutive_no_progress_steps={consecutive_no_progress_steps} {}",
1500            compact_text("message", message)
1501        )),
1502        Event::AgentLoopTerminated {
1503            model_step,
1504            reason,
1505            consecutive_no_progress_steps,
1506        } => Some(
1507            format!(
1508                "agent_loop_terminated step={model_step} reason={reason:?} consecutive_no_progress_steps={consecutive_no_progress_steps}"
1509            )
1510            .to_lowercase(),
1511        ),
1512        Event::TerminalControlTransition {
1513            model_step,
1514            policy,
1515            from,
1516            to,
1517            reason,
1518        } => Some(
1519            format!(
1520                "terminal_control_transition step={model_step} policy={policy:?} from={from:?} to={to:?} reason={reason:?}"
1521            )
1522            .to_lowercase(),
1523        ),
1524        Event::RunCancelled => Some("run_status=cancelled".to_owned()),
1525        Event::ContextRead { entry } => Some(format!(
1526            "context_read path={} {}",
1527            entry.path,
1528            compact_text("content", &entry.content)
1529        )),
1530        Event::ContextSearchResult { entries } => Some(format!(
1531            "context_search paths={}",
1532            truncate_chars(
1533                &entries
1534                    .iter()
1535                    .map(|entry| entry.path.as_str())
1536                    .collect::<Vec<_>>()
1537                    .join(","),
1538                BATCH_FIELD_EXCERPT_LIMIT,
1539            )
1540        )),
1541        Event::ContextUpdated { entry } => Some(format!(
1542            "context_updated path={} {}",
1543            entry.path,
1544            compact_text("content", &entry.content)
1545        )),
1546        Event::ContextDeleted { path } => Some(format!("context_deleted path={path}")),
1547        Event::ContextDisclosureSet { level } => {
1548            Some(format!("context_disclosure={level:?}").to_lowercase())
1549        }
1550        Event::Error { code, message } => Some(format!(
1551            "error code={code:?} {}",
1552            compact_text("message", message)
1553        )),
1554        Event::SessionCreated { .. }
1555        | Event::SessionForked { .. }
1556        | Event::SessionResumed
1557        | Event::SessionSuspended
1558        | Event::SessionClosed
1559        | Event::RunScheduled
1560        | Event::RunStarted
1561        | Event::RunCompleted { output: None }
1562        | Event::CommandOutput { .. } => None,
1563    }
1564}
1565
1566fn compact_text(label: &str, value: &str) -> String {
1567    compact_text_with_limit(label, value, BATCH_FIELD_EXCERPT_LIMIT)
1568}
1569
1570fn compact_user_message(value: &str) -> String {
1571    format!(
1572        "user_hash={} user_chars={} user_content={}",
1573        stable_fingerprint(value),
1574        value.chars().count(),
1575        truncate_chars(value, BATCH_USER_MESSAGE_EXCERPT_LIMIT)
1576    )
1577}
1578
1579fn compact_text_with_limit(label: &str, value: &str, excerpt_limit: usize) -> String {
1580    format!(
1581        "{label}_hash={} {label}_chars={} {label}_excerpt={:?}",
1582        stable_fingerprint(value),
1583        value.chars().count(),
1584        truncate_chars(value, excerpt_limit)
1585    )
1586}
1587
1588fn stable_fingerprint(value: &str) -> String {
1589    let hash = value
1590        .as_bytes()
1591        .iter()
1592        .fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1593            (hash ^ u64::from(*byte)).wrapping_mul(0x0000_0100_0000_01b3)
1594        });
1595    format!("fnv1a64:{hash:016x}")
1596}
1597
1598fn truncate_chars(value: &str, limit: usize) -> String {
1599    if value.chars().count() <= limit {
1600        return value.to_owned();
1601    }
1602    let mut truncated: String = value.chars().take(limit.saturating_sub(3)).collect();
1603    truncated.push_str("...");
1604    truncated
1605}
1606
1607fn truncate_utf8_bytes(value: &str, limit: usize) -> String {
1608    if value.len() <= limit {
1609        return value.to_owned();
1610    }
1611    let mut end = limit.min(value.len());
1612    while !value.is_char_boundary(end) {
1613        end = end.saturating_sub(1);
1614    }
1615    value[..end].to_owned()
1616}
1617
1618#[cfg(test)]
1619mod tests {
1620    use arabica_model::{
1621        MemoryLoadState, ProviderState, ReasoningItem, RuntimeItem, ShortMemoryItem,
1622    };
1623    use arabica_protocol::{
1624        CommandId, EventId, EventMetadata, OutputStream, SessionId, WorkspaceId,
1625    };
1626
1627    use super::*;
1628
1629    #[test]
1630    fn context_audit_does_not_enter_memory_batches_or_transcript() {
1631        let events = vec![
1632            envelope(
1633                1,
1634                Event::ContextRequestExposed {
1635                    model_step: 0,
1636                    decision_id: "d".into(),
1637                    context_ids: vec!["tool:private-context".into()],
1638                    folded_ids: Vec::new(),
1639                },
1640            ),
1641            envelope(
1642                2,
1643                Event::ContextCallStarted {
1644                    call_id: "c".into(),
1645                    decision_id: "d".into(),
1646                    context_id: "tool:private-context".into(),
1647                },
1648            ),
1649            envelope(
1650                3,
1651                Event::ContextCallRejected {
1652                    call_id: "x".into(),
1653                    decision_id: "d".into(),
1654                    context_id: None,
1655                },
1656            ),
1657        ];
1658        assert!(build_batches(&events).is_empty());
1659        assert!(exact_transcript_entries(&events, &RunId::new("other-run")).is_empty());
1660    }
1661
1662    fn envelope(sequence: u64, event: Event) -> EventEnvelope {
1663        envelope_for_run(sequence, "run-1", event)
1664    }
1665
1666    fn envelope_for_run(sequence: u64, run_id: &str, event: Event) -> EventEnvelope {
1667        envelope_for_session(sequence, "session-1", run_id, event)
1668    }
1669
1670    fn envelope_for_session(
1671        sequence: u64,
1672        session_id: &str,
1673        run_id: &str,
1674        event: Event,
1675    ) -> EventEnvelope {
1676        EventEnvelope::new(
1677            EventMetadata {
1678                event_id: EventId::new(format!("event-{session_id}-{sequence}")),
1679                command_id: CommandId::new("command-1"),
1680                workspace_id: WorkspaceId::new("workspace-1"),
1681                session_id: SessionId::new(session_id),
1682                run_id: Some(RunId::new(run_id)),
1683                sequence,
1684                occurred_at_ms: 0,
1685            },
1686            event,
1687        )
1688    }
1689
1690    #[test]
1691    fn reasoning_response_is_canonical_and_projects_as_its_own_batch() {
1692        let reasoning = ReasoningItem {
1693            id: Some("reasoning-1".to_owned()),
1694            summary: vec!["inspect before changing state".to_owned()],
1695            provider_state: Some(ProviderState::OpenAiChatCompletions {
1696                reasoning_content: "inspect before changing state".to_owned(),
1697            }),
1698        };
1699        let history = vec![envelope(
1700            1,
1701            Event::ModelResponseItem {
1702                model_step: 0,
1703                item_index: 0,
1704                item: RuntimeItem::Reasoning(reasoning.clone()),
1705            },
1706        )];
1707
1708        let materialization = ShortMemoryProjector::materialize(
1709            &history,
1710            Some(&RunId::new("run-1")),
1711            &ShortMemoryPolicy::full_replay(),
1712        );
1713
1714        assert_eq!(materialization.batches.len(), 1);
1715        assert_eq!(
1716            materialization.batches[0].context_kind,
1717            MemoryBatchKind::Reasoning
1718        );
1719        assert!(matches!(
1720            materialization.entries.as_slice(),
1721            [ShortMemoryEntry {
1722                item: ShortMemoryItem::Reasoning(projected),
1723                ..
1724            }] if projected == &reasoning
1725        ));
1726    }
1727
1728    #[test]
1729    fn responses_message_and_tool_state_survive_active_run_projection() {
1730        let raw_message = serde_json::json!({
1731            "id": "msg-1",
1732            "type": "message",
1733            "role": "assistant",
1734            "phase": "commentary",
1735            "content": [{"type": "output_text", "text": "I will inspect it."}]
1736        });
1737        let raw_call = serde_json::json!({
1738            "id": "fc-1",
1739            "type": "function_call",
1740            "call_id": "call-1",
1741            "name": "read_file",
1742            "arguments": "{\"path\":\"src/lib.rs\"}"
1743        });
1744        let message = arabica_model::MessageItem {
1745            id: Some("msg-1".to_owned()),
1746            role: arabica_model::RuntimeRole::Assistant,
1747            content: vec![ContentBlock::text("I will inspect it.")],
1748            provider_state: Some(ProviderState::OpenAi {
1749                item_id: Some("msg-1".to_owned()),
1750                encrypted_content: None,
1751                raw_item: Some(raw_message),
1752            }),
1753        };
1754        let call_state = ProviderState::OpenAi {
1755            item_id: Some("fc-1".to_owned()),
1756            encrypted_content: None,
1757            raw_item: Some(raw_call),
1758        };
1759        let history = vec![
1760            envelope(
1761                1,
1762                Event::ModelResponseItem {
1763                    model_step: 0,
1764                    item_index: 0,
1765                    item: RuntimeItem::Message(message.clone()),
1766                },
1767            ),
1768            envelope(
1769                2,
1770                Event::ToolCallRequested {
1771                    call_id: "call-1".to_owned(),
1772                    name: "read_file".to_owned(),
1773                    arguments: serde_json::json!({"path": "src/lib.rs"}),
1774                    provider_state: Some(call_state.clone()),
1775                },
1776            ),
1777        ];
1778
1779        let materialization = ShortMemoryProjector::materialize(
1780            &history,
1781            Some(&RunId::new("run-1")),
1782            &ShortMemoryPolicy::full_replay(),
1783        );
1784
1785        assert!(materialization.entries.iter().any(|entry| matches!(
1786            &entry.item,
1787            ShortMemoryItem::ProviderMessage(projected) if projected == &message
1788        )));
1789        assert!(materialization.entries.iter().any(|entry| matches!(
1790            &entry.item,
1791            ShortMemoryItem::ToolCall(projected)
1792                if projected.provider_state.as_ref() == Some(&call_state)
1793        )));
1794    }
1795
1796    #[test]
1797    fn detailed_protocol_events_map_to_retention_classes() {
1798        assert_eq!(
1799            ShortMemoryProjector::classify_event(&Event::MessageAccepted {
1800                images: Vec::new(),
1801                content: "remember this".to_owned(),
1802            })
1803            .class,
1804            MemoryClass::Anchor
1805        );
1806        let tool_error = ShortMemoryProjector::classify_event(&Event::ToolCallCompleted {
1807            call_id: "call-1".to_owned(),
1808            name: "read".to_owned(),
1809            result: "failed".to_owned(),
1810            is_error: true,
1811        });
1812        assert_eq!(tool_error.class, MemoryClass::Recovery);
1813        assert_eq!(tool_error.relation_key.as_deref(), Some("tool:call-1"));
1814        assert!(tool_error.completes_relation);
1815        for (kind, expected_class) in [
1816            (ToolInteractionKind::Inspection, MemoryClass::ToolInspection),
1817            (ToolInteractionKind::Mutation, MemoryClass::ToolMutation),
1818            (ToolInteractionKind::Build, MemoryClass::ToolBuild),
1819            (ToolInteractionKind::Dependency, MemoryClass::ToolDependency),
1820            (ToolInteractionKind::Validation, MemoryClass::ToolValidation),
1821            (ToolInteractionKind::Generic, MemoryClass::Working),
1822        ] {
1823            let classified = ShortMemoryProjector::classify_event(&Event::ToolCallClassified {
1824                call_id: "call-1".to_owned(),
1825                kind,
1826            });
1827            assert_eq!(classified.class, expected_class);
1828            assert_eq!(classified.relation_key.as_deref(), Some("tool:call-1"));
1829            assert!(!classified.completes_relation);
1830        }
1831        assert_eq!(
1832            ShortMemoryProjector::classify_event(&Event::CommandOutput {
1833                stream: OutputStream::Stdout,
1834                chunk: "noise".to_owned(),
1835            })
1836            .class,
1837            MemoryClass::Transient
1838        );
1839        assert_eq!(
1840            ShortMemoryProjector::classify_event(&Event::SessionClosed).class,
1841            MemoryClass::Control
1842        );
1843    }
1844
1845    #[test]
1846    fn tool_interaction_classes_have_independent_default_ttls() {
1847        let policy = ShortMemoryPolicy::default();
1848
1849        assert_eq!(
1850            policy.ttl_for(MemoryClass::ToolInspection),
1851            EventTtl::ttl(1)
1852        );
1853        assert_eq!(policy.ttl_for(MemoryClass::ToolMutation), EventTtl::ttl(8));
1854        assert_eq!(policy.ttl_for(MemoryClass::ToolBuild), EventTtl::ttl(8));
1855        assert_eq!(
1856            policy.ttl_for(MemoryClass::ToolDependency),
1857            EventTtl::ttl(8)
1858        );
1859        assert_eq!(
1860            policy.ttl_for(MemoryClass::ToolValidation),
1861            EventTtl::ttl(6)
1862        );
1863    }
1864
1865    #[test]
1866    fn ttl_uses_newer_event_decay_and_specialized_rules() {
1867        let events = vec![
1868            envelope(
1869                1,
1870                Event::ToolCallRequested {
1871                    call_id: "call-1".to_owned(),
1872                    name: "read".to_owned(),
1873                    arguments: serde_json::json!({}),
1874                    provider_state: None,
1875                },
1876            ),
1877            envelope(
1878                2,
1879                Event::ToolCallCompleted {
1880                    call_id: "call-1".to_owned(),
1881                    name: "read".to_owned(),
1882                    result: "done".to_owned(),
1883                    is_error: false,
1884                },
1885            ),
1886            envelope(
1887                3,
1888                Event::RunCompleted {
1889                    output: Some("answer".to_owned()),
1890                },
1891            ),
1892        ];
1893        let mut policy = ShortMemoryPolicy {
1894            recency_floor: 0,
1895            recent_turns_load_all: 0,
1896            ..ShortMemoryPolicy::default()
1897        };
1898        policy
1899            .ttl_overrides
1900            .insert(MemoryClass::Working, EventTtl::ttl(2));
1901
1902        let result = ShortMemoryProjector::materialize(&events, None, &policy);
1903
1904        assert_eq!(result.visibility[0].accumulated_decay, 4);
1905        assert!(!result.visibility[0].visible);
1906        assert!(result.visibility[2].visible);
1907    }
1908
1909    #[test]
1910    fn tool_completion_only_accelerates_its_matching_call() {
1911        let events = vec![
1912            envelope(
1913                1,
1914                Event::ToolCallRequested {
1915                    call_id: "call-1".to_owned(),
1916                    name: "read".to_owned(),
1917                    arguments: serde_json::json!({}),
1918                    provider_state: None,
1919                },
1920            ),
1921            envelope(
1922                2,
1923                Event::ToolCallCompleted {
1924                    call_id: "call-2".to_owned(),
1925                    name: "search".to_owned(),
1926                    result: "unrelated".to_owned(),
1927                    is_error: false,
1928                },
1929            ),
1930            envelope(
1931                3,
1932                Event::RunCompleted {
1933                    output: Some("answer".to_owned()),
1934                },
1935            ),
1936        ];
1937        let mut policy = ShortMemoryPolicy {
1938            recency_floor: 0,
1939            recent_turns_load_all: 0,
1940            ..ShortMemoryPolicy::default()
1941        };
1942        policy
1943            .ttl_overrides
1944            .insert(MemoryClass::Working, EventTtl::ttl(2));
1945
1946        let result = ShortMemoryProjector::materialize(&events, None, &policy);
1947
1948        assert_eq!(result.visibility[0].accumulated_decay, 2);
1949        assert!(result.visibility[0].visible);
1950    }
1951
1952    #[test]
1953    fn recency_floor_protects_an_expired_event_without_mutating_history() {
1954        let events = vec![
1955            envelope(
1956                1,
1957                Event::CommandOutput {
1958                    stream: OutputStream::Stdout,
1959                    chunk: "old".to_owned(),
1960                },
1961            ),
1962            envelope(
1963                2,
1964                Event::CommandOutput {
1965                    stream: OutputStream::Stdout,
1966                    chunk: "recent".to_owned(),
1967                },
1968            ),
1969        ];
1970        let original = events.clone();
1971        let mut policy = ShortMemoryPolicy {
1972            recency_floor: 1,
1973            recent_turns_load_all: 0,
1974            ..ShortMemoryPolicy::default()
1975        };
1976        policy
1977            .ttl_overrides
1978            .insert(MemoryClass::Transient, EventTtl::ttl(0));
1979
1980        let first = ShortMemoryProjector::materialize(&events, None, &policy);
1981        let second = ShortMemoryProjector::materialize(&events, None, &policy);
1982
1983        assert!(!first.visibility[0].visible);
1984        assert!(first.visibility[1].visible);
1985        assert!(first.visibility[1].protected_by_recency_floor);
1986        assert_eq!(first, second);
1987        assert_eq!(events, original);
1988    }
1989
1990    #[test]
1991    fn permission_events_join_the_tool_batch_without_reaching_the_model() {
1992        use arabica_protocol::{ToolPermissionOutcome, ToolPermissionScope, ToolPermissionSource};
1993
1994        let requested = Event::ToolCallRequested {
1995            call_id: "call-9".to_owned(),
1996            name: "edit_files".to_owned(),
1997            arguments: serde_json::json!({"edits": []}),
1998            provider_state: None,
1999        };
2000        let completed = Event::ToolCallCompleted {
2001            call_id: "call-9".to_owned(),
2002            name: "edit_files".to_owned(),
2003            result: "a.txt: 1 replacement".to_owned(),
2004            is_error: false,
2005        };
2006        let ungated = vec![
2007            envelope(1, requested.clone()),
2008            envelope(2, completed.clone()),
2009        ];
2010        let gated = vec![
2011            envelope(1, requested),
2012            envelope(
2013                2,
2014                Event::ToolCallPermissionRequested {
2015                    call_id: "call-9".to_owned(),
2016                },
2017            ),
2018            envelope(
2019                3,
2020                Event::ToolCallPermissionResolved {
2021                    call_id: "call-9".to_owned(),
2022                    outcome: ToolPermissionOutcome::Allowed,
2023                    scope: ToolPermissionScope::Once,
2024                    source: ToolPermissionSource::User,
2025                },
2026            ),
2027            envelope(4, completed),
2028        ];
2029        let project = |events: &[EventEnvelope]| {
2030            ShortMemoryProjector::materialize(
2031                events,
2032                Some(&RunId::new("run-1")),
2033                &ShortMemoryPolicy::default(),
2034            )
2035        };
2036        // Item ids are source Event ids, which encode log position. The gate's
2037        // two Events shift the completion from sequence 2 to 4, so ids differ
2038        // by construction; the invariant is about content, not position.
2039        let content = |result: &ShortMemoryMaterialization| {
2040            result
2041                .entries
2042                .iter()
2043                .map(|entry| {
2044                    let mut item = entry.item.clone();
2045                    match &mut item {
2046                        ShortMemoryItem::ToolCall(call) => call.id = None,
2047                        ShortMemoryItem::ToolResult(result) => result.id = None,
2048                        _ => {}
2049                    }
2050                    item
2051                })
2052                .collect::<Vec<_>>()
2053        };
2054
2055        let with_gate = project(&gated);
2056        // The permission exchange is part of the call it gates, so the batch
2057        // still closes on tool.call.completed.
2058        assert_eq!(with_gate.batches.len(), 1);
2059        assert_eq!(with_gate.batches[0].context_key, "run:run-1:tool:call-9");
2060        assert_eq!(with_gate.batches[0].event_count, 4);
2061        // A gate the user approved leaves what the model reads unchanged.
2062        assert_eq!(content(&with_gate), content(&project(&ungated)));
2063    }
2064
2065    #[test]
2066    fn tool_call_output_and_result_share_one_stable_batch() {
2067        let events = vec![
2068            envelope(
2069                1,
2070                Event::ToolCallRequested {
2071                    call_id: "call-7".to_owned(),
2072                    name: "write_file".to_owned(),
2073                    arguments: serde_json::json!({"path": "note.txt"}),
2074                    provider_state: None,
2075                },
2076            ),
2077            envelope(
2078                2,
2079                Event::CommandOutput {
2080                    stream: OutputStream::Stdout,
2081                    chunk: "writing".to_owned(),
2082                },
2083            ),
2084            envelope(
2085                3,
2086                Event::ToolCallCompleted {
2087                    call_id: "call-7".to_owned(),
2088                    name: "write_file".to_owned(),
2089                    result: "ok".to_owned(),
2090                    is_error: false,
2091                },
2092            ),
2093        ];
2094
2095        let result = ShortMemoryProjector::materialize(
2096            &events,
2097            Some(&RunId::new("run-1")),
2098            &ShortMemoryPolicy::default(),
2099        );
2100
2101        assert_eq!(result.batches.len(), 1);
2102        assert_eq!(result.batches[0].context_key, "run:run-1:tool:call-7");
2103        assert_eq!(result.batches[0].event_count, 3);
2104        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadAll);
2105        assert!(matches!(
2106            result.entries[0].item,
2107            ShortMemoryItem::ToolCall(_)
2108        ));
2109        assert!(matches!(
2110            result.entries[2].item,
2111            ShortMemoryItem::ToolResult(_)
2112        ));
2113    }
2114
2115    #[test]
2116    fn model_step_compacts_older_closed_tools_but_protects_the_active_tail() {
2117        let events = vec![
2118            envelope(
2119                1,
2120                Event::ToolCallRequested {
2121                    call_id: "call-old".to_owned(),
2122                    name: "read_file".to_owned(),
2123                    arguments: serde_json::json!({"path": "old.txt"}),
2124                    provider_state: None,
2125                },
2126            ),
2127            envelope(
2128                2,
2129                Event::ToolCallCompleted {
2130                    call_id: "call-old".to_owned(),
2131                    name: "read_file".to_owned(),
2132                    result: "old result ".repeat(500),
2133                    is_error: false,
2134                },
2135            ),
2136            envelope(
2137                3,
2138                Event::ToolCallRequested {
2139                    call_id: "call-active".to_owned(),
2140                    name: "read_file".to_owned(),
2141                    arguments: serde_json::json!({"path": "active.txt"}),
2142                    provider_state: None,
2143                },
2144            ),
2145            envelope(
2146                4,
2147                Event::ToolCallCompleted {
2148                    call_id: "call-active".to_owned(),
2149                    name: "read_file".to_owned(),
2150                    result: "active result ".repeat(500),
2151                    is_error: false,
2152                },
2153            ),
2154        ];
2155        let protected = HashSet::from([
2156            EventId::new("event-session-1-3"),
2157            EventId::new("event-session-1-4"),
2158        ]);
2159
2160        let result = ShortMemoryProjector::materialize_for_model_step(
2161            &events,
2162            &RunId::new("run-1"),
2163            &protected,
2164            &ShortMemoryPolicy::batch_only(0),
2165        );
2166
2167        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadKey);
2168        assert_eq!(result.batches[1].load_state, MemoryLoadState::LoadAll);
2169        assert!(matches!(
2170            result.entries[0].item,
2171            ShortMemoryItem::BatchKey(_)
2172        ));
2173        assert!(result.entries.iter().any(|entry| {
2174            matches!(
2175                &entry.item,
2176                ShortMemoryItem::ToolCall(call) if call.call_id == "call-active"
2177            )
2178        }));
2179        assert!(result.entries.iter().any(|entry| {
2180            matches!(
2181                &entry.item,
2182                ShortMemoryItem::ToolResult(result) if result.call_id == "call-active"
2183            )
2184        }));
2185    }
2186
2187    #[test]
2188    fn explicit_model_step_protection_overrides_expired_ttl() {
2189        let events = vec![
2190            envelope(
2191                1,
2192                Event::ToolCallRequested {
2193                    call_id: "call-error".to_owned(),
2194                    name: "shell".to_owned(),
2195                    arguments: serde_json::json!({"command": "pytest"}),
2196                    provider_state: None,
2197                },
2198            ),
2199            envelope(
2200                2,
2201                Event::ToolCallCompleted {
2202                    call_id: "call-error".to_owned(),
2203                    name: "shell".to_owned(),
2204                    result: "one test failed".to_owned(),
2205                    is_error: true,
2206                },
2207            ),
2208            envelope(
2209                3,
2210                Event::ToolCallRequested {
2211                    call_id: "call-new".to_owned(),
2212                    name: "shell".to_owned(),
2213                    arguments: serde_json::json!({"command": "pwd"}),
2214                    provider_state: None,
2215                },
2216            ),
2217            envelope(
2218                4,
2219                Event::ToolCallCompleted {
2220                    call_id: "call-new".to_owned(),
2221                    name: "shell".to_owned(),
2222                    result: "/workspace".to_owned(),
2223                    is_error: false,
2224                },
2225            ),
2226        ];
2227        let protected = HashSet::from([
2228            EventId::new("event-session-1-1"),
2229            EventId::new("event-session-1-2"),
2230        ]);
2231        let policy = ShortMemoryPolicy {
2232            recency_floor: 0,
2233            ttl_overrides: BTreeMap::from([
2234                (MemoryClass::Anchor, EventTtl::ttl(0)),
2235                (MemoryClass::Working, EventTtl::ttl(0)),
2236                (MemoryClass::Recovery, EventTtl::ttl(0)),
2237                (MemoryClass::Transient, EventTtl::ttl(0)),
2238                (MemoryClass::Control, EventTtl::ttl(0)),
2239            ]),
2240            ..ShortMemoryPolicy::default()
2241        };
2242
2243        let result = ShortMemoryProjector::materialize_for_model_step(
2244            &events,
2245            &RunId::new("run-1"),
2246            &protected,
2247            &policy,
2248        );
2249
2250        let error_batch = result
2251            .batches
2252            .iter()
2253            .find(|batch| batch.context_key.ends_with("call-error"))
2254            .expect("error batch exists");
2255        assert_eq!(error_batch.load_state, MemoryLoadState::LoadAll);
2256        assert!(result.entries.iter().any(|entry| {
2257            matches!(
2258                &entry.item,
2259                ShortMemoryItem::ToolResult(result)
2260                    if result.call_id == "call-error" && result.is_error
2261            )
2262        }));
2263    }
2264
2265    #[test]
2266    fn model_step_gate_uses_only_items_that_reach_the_provider() {
2267        let events = vec![
2268            envelope(
2269                1,
2270                Event::ToolCallRequested {
2271                    call_id: "call-old".to_owned(),
2272                    name: "write_file".to_owned(),
2273                    arguments: serde_json::json!({"path": "old.txt", "content": "ok"}),
2274                    provider_state: None,
2275                },
2276            ),
2277            envelope(
2278                2,
2279                Event::CommandOutput {
2280                    stream: OutputStream::Stdout,
2281                    chunk: "duplicate runner output ".repeat(500),
2282                },
2283            ),
2284            envelope(
2285                3,
2286                Event::ToolCallCompleted {
2287                    call_id: "call-old".to_owned(),
2288                    name: "write_file".to_owned(),
2289                    result: "ok".to_owned(),
2290                    is_error: false,
2291                },
2292            ),
2293        ];
2294
2295        let result = ShortMemoryProjector::materialize_for_model_step(
2296            &events,
2297            &RunId::new("run-1"),
2298            &HashSet::new(),
2299            &ShortMemoryPolicy::batch_only(0),
2300        );
2301
2302        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadAll);
2303        assert_eq!(
2304            result.batches[0].key_admission,
2305            KeyAdmissionDecision::KeptFullNoBenefit
2306        );
2307        assert!(result.batches[0].materialized_key_bytes >= result.batches[0].raw_item_bytes);
2308    }
2309
2310    #[test]
2311    fn full_replay_keeps_older_active_run_tool_batches_uncompressed() {
2312        let events = vec![
2313            envelope(
2314                1,
2315                Event::ToolCallRequested {
2316                    call_id: "call-old".to_owned(),
2317                    name: "read_file".to_owned(),
2318                    arguments: serde_json::json!({"path": "old.txt"}),
2319                    provider_state: None,
2320                },
2321            ),
2322            envelope(
2323                2,
2324                Event::ToolCallCompleted {
2325                    call_id: "call-old".to_owned(),
2326                    name: "read_file".to_owned(),
2327                    result: "old result ".repeat(500),
2328                    is_error: false,
2329                },
2330            ),
2331            envelope(
2332                3,
2333                Event::ToolCallRequested {
2334                    call_id: "call-active".to_owned(),
2335                    name: "read_file".to_owned(),
2336                    arguments: serde_json::json!({"path": "active.txt"}),
2337                    provider_state: None,
2338                },
2339            ),
2340            envelope(
2341                4,
2342                Event::ToolCallCompleted {
2343                    call_id: "call-active".to_owned(),
2344                    name: "read_file".to_owned(),
2345                    result: "active result ".repeat(500),
2346                    is_error: false,
2347                },
2348            ),
2349        ];
2350        let protected = HashSet::from([
2351            EventId::new("event-session-1-3"),
2352            EventId::new("event-session-1-4"),
2353        ]);
2354
2355        let result = ShortMemoryProjector::materialize_for_model_step(
2356            &events,
2357            &RunId::new("run-1"),
2358            &protected,
2359            &ShortMemoryPolicy::full_replay(),
2360        );
2361
2362        assert!(
2363            result
2364                .batches
2365                .iter()
2366                .all(|batch| batch.load_state == MemoryLoadState::LoadAll)
2367        );
2368        assert!(
2369            result
2370                .entries
2371                .iter()
2372                .all(|entry| !matches!(entry.item, ShortMemoryItem::BatchKey(_)))
2373        );
2374    }
2375
2376    #[test]
2377    fn recent_turns_load_all_and_older_turns_load_only_the_key() {
2378        let events = vec![
2379            envelope_for_run(
2380                1,
2381                "old",
2382                Event::MessageAccepted {
2383                    images: Vec::new(),
2384                    content: "old question ".repeat(200),
2385                },
2386            ),
2387            envelope_for_run(
2388                2,
2389                "old",
2390                Event::RunCompleted {
2391                    output: Some("old answer ".repeat(200)),
2392                },
2393            ),
2394            envelope_for_run(
2395                3,
2396                "recent",
2397                Event::MessageAccepted {
2398                    images: Vec::new(),
2399                    content: "recent question".to_owned(),
2400                },
2401            ),
2402            envelope_for_run(
2403                4,
2404                "recent",
2405                Event::RunCompleted {
2406                    output: Some("recent answer".to_owned()),
2407                },
2408            ),
2409        ];
2410        let policy = ShortMemoryPolicy {
2411            recency_floor: 0,
2412            recent_turns_load_all: 1,
2413            ..ShortMemoryPolicy::default()
2414        };
2415
2416        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2417
2418        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadKey);
2419        assert_eq!(result.batches[1].load_state, MemoryLoadState::LoadAll);
2420        assert!(!result.batches[0].key_content.is_empty());
2421        assert!(result.batches[1].key_content.is_empty());
2422        assert!(matches!(
2423            result.entries[0].item,
2424            ShortMemoryItem::BatchKey(_)
2425        ));
2426        assert!(matches!(
2427            result.entries[1].item,
2428            ShortMemoryItem::UserMessage { .. }
2429        ));
2430        assert!(matches!(
2431            result.entries[2].item,
2432            ShortMemoryItem::AssistantMessage { .. }
2433        ));
2434    }
2435
2436    #[test]
2437    fn expired_batches_are_not_loaded_but_remain_in_the_input_log() {
2438        let events = vec![
2439            envelope(
2440                1,
2441                Event::ContextRead {
2442                    entry: arabica_protocol::ContextEntry {
2443                        path: "knowledge/old".to_owned(),
2444                        content: "old payload".to_owned(),
2445                    },
2446                },
2447            ),
2448            envelope(
2449                2,
2450                Event::RunCompleted {
2451                    output: Some("answer".to_owned()),
2452                },
2453            ),
2454        ];
2455        let mut policy = ShortMemoryPolicy {
2456            recency_floor: 0,
2457            recent_turns_load_all: 0,
2458            ..ShortMemoryPolicy::default()
2459        };
2460        policy
2461            .ttl_overrides
2462            .insert(MemoryClass::Working, EventTtl::ttl(0));
2463
2464        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2465
2466        assert_eq!(result.batches[0].load_state, MemoryLoadState::NoLoad);
2467        assert_eq!(events.len(), 2);
2468        assert_eq!(events[0].sequence, 1);
2469    }
2470
2471    #[test]
2472    fn forked_histories_use_event_identity_and_preserve_supplied_order() {
2473        let events = vec![
2474            envelope_for_session(
2475                1,
2476                "parent",
2477                "parent-run",
2478                Event::MessageAccepted {
2479                    images: Vec::new(),
2480                    content: "parent fact".to_owned(),
2481                },
2482            ),
2483            envelope_for_session(
2484                1,
2485                "child",
2486                "child-run",
2487                Event::MessageAccepted {
2488                    images: Vec::new(),
2489                    content: "child fact".to_owned(),
2490                },
2491            ),
2492        ];
2493
2494        let result =
2495            ShortMemoryProjector::materialize(&events, None, &ShortMemoryPolicy::default());
2496
2497        assert_eq!(result.entries.len(), 2);
2498        assert_eq!(
2499            result.entries[0].source_event_ids,
2500            vec!["event-parent-1".to_owned()]
2501        );
2502        assert_eq!(
2503            result.entries[1].source_event_ids,
2504            vec!["event-child-1".to_owned()]
2505        );
2506    }
2507
2508    #[test]
2509    fn full_projection_reuses_runtime_mapping_without_gc_or_batch_keys() {
2510        let events = vec![
2511            envelope(1, Event::RunStarted),
2512            envelope(
2513                2,
2514                Event::MessageAccepted {
2515                    images: Vec::new(),
2516                    content: "question".to_owned(),
2517                },
2518            ),
2519            envelope(
2520                3,
2521                Event::RunCompleted {
2522                    output: Some("answer".to_owned()),
2523                },
2524            ),
2525        ];
2526
2527        let entries = ShortMemoryProjector::project_full(&events);
2528
2529        assert_eq!(entries.len(), 2);
2530        assert_eq!(entries[0].source_event_ids, vec!["event-session-1-2"]);
2531        assert_eq!(entries[1].source_event_ids, vec!["event-session-1-3"]);
2532        assert!(
2533            entries
2534                .iter()
2535                .all(|entry| !matches!(entry.item, ShortMemoryItem::BatchKey(_)))
2536        );
2537    }
2538
2539    #[test]
2540    fn key_admission_prefers_recent_batches_with_equal_evidence_value() {
2541        let events = vec![
2542            envelope_for_run(
2543                1,
2544                "old",
2545                Event::MessageAccepted {
2546                    images: Vec::new(),
2547                    content: "old question ".repeat(200),
2548                },
2549            ),
2550            envelope_for_run(
2551                2,
2552                "old",
2553                Event::RunCompleted {
2554                    output: Some("old answer ".repeat(200)),
2555                },
2556            ),
2557            envelope_for_run(
2558                3,
2559                "new",
2560                Event::MessageAccepted {
2561                    images: Vec::new(),
2562                    content: "new question ".repeat(200),
2563                },
2564            ),
2565            envelope_for_run(
2566                4,
2567                "new",
2568                Event::RunCompleted {
2569                    output: Some("new answer ".repeat(200)),
2570                },
2571            ),
2572        ];
2573        let policy = ShortMemoryPolicy {
2574            recency_floor: 0,
2575            recent_turns_load_all: 0,
2576            key_admission: KeyAdmissionPolicy {
2577                max_key_batches: Some(1),
2578                max_key_content_bytes: None,
2579            },
2580            ..ShortMemoryPolicy::default()
2581        };
2582
2583        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2584
2585        assert_eq!(result.key_admission.candidate_batches, 2);
2586        assert_eq!(result.key_admission.admitted_batches, 1);
2587        assert_eq!(result.key_admission.rejected_batches, 1);
2588        assert_eq!(result.batches[0].load_state, MemoryLoadState::NoLoad);
2589        assert_eq!(
2590            result.batches[0].key_admission,
2591            KeyAdmissionDecision::RejectedBatchLimit
2592        );
2593        assert_eq!(result.batches[1].load_state, MemoryLoadState::LoadKey);
2594        assert_eq!(
2595            result.batches[1].key_admission,
2596            KeyAdmissionDecision::Admitted
2597        );
2598        assert_eq!(result.batches[1].key_admission_rank, Some(1));
2599    }
2600
2601    #[test]
2602    fn zero_byte_budget_rejects_keys_with_an_explainable_decision() {
2603        let events = vec![envelope(
2604            1,
2605            Event::MessageAccepted {
2606                images: Vec::new(),
2607                content: "required anchor ".repeat(200),
2608            },
2609        )];
2610        let policy = ShortMemoryPolicy {
2611            recency_floor: 0,
2612            recent_turns_load_all: 0,
2613            key_admission: KeyAdmissionPolicy {
2614                max_key_batches: None,
2615                max_key_content_bytes: Some(0),
2616            },
2617            ..ShortMemoryPolicy::default()
2618        };
2619
2620        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2621
2622        assert!(result.entries.is_empty());
2623        assert_eq!(result.key_admission.candidate_batches, 1);
2624        assert_eq!(result.key_admission.admitted_key_content_bytes, 0);
2625        assert_eq!(
2626            result.batches[0].key_admission,
2627            KeyAdmissionDecision::RejectedByteLimit
2628        );
2629        assert_eq!(result.batches[0].load_state, MemoryLoadState::NoLoad);
2630        assert!(result.batches[0].key_content.is_empty());
2631        assert!(result.batches[0].key_content_bytes > 0);
2632    }
2633
2634    #[test]
2635    fn batch_key_is_a_bounded_semantic_index_not_raw_event_replay() {
2636        let large_result = "x".repeat(5_000);
2637        let events = vec![
2638            envelope(
2639                1,
2640                Event::ToolCallRequested {
2641                    call_id: "call-large".to_owned(),
2642                    name: "read_file".to_owned(),
2643                    arguments: serde_json::json!({"path": "src/large.rs"}),
2644                    provider_state: None,
2645                },
2646            ),
2647            envelope(
2648                2,
2649                Event::ToolCallCompleted {
2650                    call_id: "call-large".to_owned(),
2651                    name: "read_file".to_owned(),
2652                    result: large_result.clone(),
2653                    is_error: false,
2654                },
2655            ),
2656        ];
2657        let policy = ShortMemoryPolicy {
2658            recent_turns_load_all: 0,
2659            ..ShortMemoryPolicy::default()
2660        };
2661
2662        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2663        let key = &result.batches[0].key_content;
2664
2665        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadKey);
2666        assert!(key.len() <= DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES);
2667        assert_eq!(
2668            result.batches[0].key_content_budget_bytes,
2669            DEFAULT_BATCH_KEY_CONTENT_LIMIT_BYTES.min(
2670                result.batches[0].raw_item_bytes
2671                    * usize::from(DEFAULT_BATCH_TARGET_COMPRESSION_BPS)
2672                    / 10_000
2673            )
2674        );
2675        assert!(result.batches[0].materialized_key_bytes < result.batches[0].raw_item_bytes);
2676        assert!(key.contains("types=tool.call.completed:1,tool.call.requested:1"));
2677        assert!(key.contains("tool_call name=read_file"));
2678        assert!(key.contains("result_hash=fnv1a64:"));
2679        assert!(!key.contains(&large_result[..1_000]));
2680    }
2681
2682    #[test]
2683    fn short_structured_user_evidence_stays_full_when_key_would_expand_input() {
2684        let evidence = "Remember this evidence for the next task and reply with MEMORY_STORED.\n<memory_evidence_json>{\"key\":\"tier-b-evidence-0001\",\"value\":\"STRUCTURE_TIER_B_OK\"}</memory_evidence_json>";
2685        let events = vec![envelope(
2686            1,
2687            Event::MessageAccepted {
2688                images: Vec::new(),
2689                content: evidence.to_owned(),
2690            },
2691        )];
2692        let policy = ShortMemoryPolicy {
2693            recent_turns_load_all: 0,
2694            ..ShortMemoryPolicy::default()
2695        };
2696
2697        let result = ShortMemoryProjector::materialize(&events, None, &policy);
2698        assert_eq!(result.batches[0].load_state, MemoryLoadState::LoadAll);
2699        assert_eq!(
2700            result.batches[0].key_admission,
2701            KeyAdmissionDecision::KeptFullNoBenefit
2702        );
2703        assert_eq!(result.key_admission.kept_full_no_benefit_batches, 1);
2704        assert!(result.batches[0].materialized_key_bytes >= result.batches[0].raw_item_bytes);
2705        assert!(matches!(
2706            result.entries[0].item,
2707            ShortMemoryItem::UserMessage { .. }
2708        ));
2709    }
2710
2711    fn tool_call_and_completed(sequence: u64, run_id: &str, call_id: &str) -> Vec<EventEnvelope> {
2712        vec![
2713            envelope_for_run(
2714                sequence,
2715                run_id,
2716                Event::ToolCallRequested {
2717                    call_id: call_id.to_owned(),
2718                    name: "read_file".to_owned(),
2719                    arguments: serde_json::json!({}),
2720                    provider_state: None,
2721                },
2722            ),
2723            envelope_for_run(
2724                sequence + 1,
2725                run_id,
2726                Event::ToolCallCompleted {
2727                    call_id: call_id.to_owned(),
2728                    name: "read_file".to_owned(),
2729                    result: "contents".to_owned(),
2730                    is_error: false,
2731                },
2732            ),
2733        ]
2734    }
2735
2736    #[test]
2737    fn exact_transcript_never_materializes_command_output() {
2738        let mut events = tool_call_and_completed(1, "run-1", "call-1");
2739        events.insert(
2740            2,
2741            envelope_for_run(
2742                3,
2743                "run-1",
2744                Event::CommandOutput {
2745                    stream: OutputStream::Stdout,
2746                    chunk: "wrote 5 bytes".to_owned(),
2747                },
2748            ),
2749        );
2750        let entries = exact_transcript_entries(&events, &RunId::new("run-2"));
2751        assert!(
2752            entries
2753                .iter()
2754                .all(|entry| !matches!(entry.item, ShortMemoryItem::Observation { .. })),
2755            "command.output must never become a materialized item: {entries:?}"
2756        );
2757        assert!(matches!(entries[0].item, ShortMemoryItem::ToolCall(_)));
2758        assert!(matches!(entries[1].item, ShortMemoryItem::ToolResult(_)));
2759    }
2760
2761    #[test]
2762    fn exact_transcript_drops_a_tool_call_the_run_never_completed() {
2763        // The runner's own hard-error path ends a run without appending
2764        // tool.call.completed for the call it was executing (deliberately
2765        // not changed in T4a, since fixing it would alter the recorded
2766        // Events of runs that already failed this way). A later turn must
2767        // not resurrect that call as an orphaned entry.
2768        let events = vec![
2769            envelope_for_run(
2770                1,
2771                "run-1",
2772                Event::ToolCallRequested {
2773                    call_id: "call-1".to_owned(),
2774                    name: "shell".to_owned(),
2775                    arguments: serde_json::json!({}),
2776                    provider_state: None,
2777                },
2778            ),
2779            envelope_for_run(
2780                2,
2781                "run-1",
2782                Event::RunFailed {
2783                    message: "runner failed: disk full".to_owned(),
2784                },
2785            ),
2786        ];
2787        let entries = exact_transcript_entries(&events, &RunId::new("run-2"));
2788        assert!(
2789            entries
2790                .iter()
2791                .all(|entry| !matches!(entry.item, ShortMemoryItem::ToolCall(_))),
2792            "an uncompleted call must be dropped, not reconstructed: {entries:?}"
2793        );
2794        assert!(matches!(
2795            entries[0].item,
2796            ShortMemoryItem::RunFailure { .. }
2797        ));
2798    }
2799
2800    #[test]
2801    fn exact_transcript_keeps_a_completed_call_from_a_different_run_unaffected() {
2802        // Two past runs reusing the same call id must not cross-contaminate
2803        // each other's pairing.
2804        let mut events = tool_call_and_completed(1, "run-1", "call-1");
2805        events.push(envelope_for_run(
2806            3,
2807            "run-2",
2808            Event::ToolCallRequested {
2809                call_id: "call-1".to_owned(),
2810                name: "shell".to_owned(),
2811                arguments: serde_json::json!({}),
2812                provider_state: None,
2813            },
2814        ));
2815        events.push(envelope_for_run(4, "run-2", Event::RunCancelled));
2816        let entries = exact_transcript_entries(&events, &RunId::new("run-3"));
2817        let tool_calls = entries
2818            .iter()
2819            .filter(|entry| matches!(entry.item, ShortMemoryItem::ToolCall(_)))
2820            .count();
2821        assert_eq!(
2822            tool_calls, 1,
2823            "run-2's uncompleted call-1 must stay dropped: {entries:?}"
2824        );
2825        assert!(matches!(
2826            entries.last().unwrap().item,
2827            ShortMemoryItem::RunCancelled
2828        ));
2829    }
2830
2831    #[test]
2832    fn exact_transcript_represents_a_run_completed_message_only_once() {
2833        let events = vec![
2834            envelope_for_run(
2835                1,
2836                "run-1",
2837                Event::MessageAccepted {
2838                    images: Vec::new(),
2839                    content: "please summarize".to_owned(),
2840                },
2841            ),
2842            envelope_for_run(
2843                2,
2844                "run-1",
2845                Event::ModelResponseItem {
2846                    model_step: 0,
2847                    item_index: 0,
2848                    item: RuntimeItem::Message(arabica_model::MessageItem::text(
2849                        arabica_model::RuntimeRole::Assistant,
2850                        "here is the summary",
2851                    )),
2852                },
2853            ),
2854            envelope_for_run(
2855                3,
2856                "run-1",
2857                Event::RunCompleted {
2858                    output: Some("here is the summary".to_owned()),
2859                },
2860            ),
2861        ];
2862        let entries = exact_transcript_entries(&events, &RunId::new("run-2"));
2863        let assistant_texts = entries
2864            .iter()
2865            .filter(|entry| {
2866                matches!(
2867                    entry.item,
2868                    ShortMemoryItem::ProviderMessage(_) | ShortMemoryItem::AssistantMessage { .. }
2869                )
2870            })
2871            .count();
2872        assert_eq!(
2873            assistant_texts, 1,
2874            "the same final text must not be represented twice: {entries:?}"
2875        );
2876        assert!(matches!(
2877            entries[1].item,
2878            ShortMemoryItem::ProviderMessage(_)
2879        ));
2880    }
2881
2882    #[test]
2883    fn exact_transcript_synthesizes_the_final_message_when_none_was_ever_emitted() {
2884        // A provider that only ever populates `final_output` (no typed
2885        // response items at all -- see EchoModel) leaves no
2886        // model.response.item Message behind. That is the only record of
2887        // what the model said, and must not be dropped.
2888        let events = vec![
2889            envelope_for_run(
2890                1,
2891                "run-1",
2892                Event::MessageAccepted {
2893                    images: Vec::new(),
2894                    content: "ping".to_owned(),
2895                },
2896            ),
2897            envelope_for_run(
2898                2,
2899                "run-1",
2900                Event::RunCompleted {
2901                    output: Some("pong".to_owned()),
2902                },
2903            ),
2904        ];
2905        let entries = exact_transcript_entries(&events, &RunId::new("run-2"));
2906        assert!(entries.iter().any(|entry| matches!(
2907            &entry.item,
2908            ShortMemoryItem::AssistantMessage { content } if content == "pong"
2909        )));
2910    }
2911
2912    #[test]
2913    fn exact_transcript_drops_reasoning_and_ignores_the_current_run() {
2914        let mut events = vec![envelope_for_run(
2915            1,
2916            "run-1",
2917            Event::ModelResponseItem {
2918                model_step: 0,
2919                item_index: 0,
2920                item: RuntimeItem::Reasoning(ReasoningItem {
2921                    id: None,
2922                    summary: vec!["private chain of thought".to_owned()],
2923                    provider_state: None,
2924                }),
2925            },
2926        )];
2927        events.extend(tool_call_and_completed(2, "run-2", "call-1"));
2928        let entries = exact_transcript_entries(&events, &RunId::new("run-2"));
2929        assert!(
2930            entries.is_empty(),
2931            "reasoning from a past run must be dropped and the current run \
2932             must be skipped entirely (it is reconstructed separately by \
2933             exact_run_continuation): {entries:?}"
2934        );
2935    }
2936}