harn-vm 0.10.121

Async bytecode virtual machine for the Harn programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
//! Deterministic prompt-turn recaps projected from canonical session events.
//!
//! A recap is a read-only view over [`SessionStore`]. It never writes a derived
//! record and never calls a provider. The store's signed event chain remains the
//! source of truth; this module groups those facts by the existing `run_id`,
//! `turn_id`, and `loop_checkpoint` iteration boundaries.

use std::collections::{BTreeMap, HashMap};
use std::path::Path;

use harn_session_store::{ReadRange, SessionEventKind, SessionStore, StoredEvent, MAX_READ_BATCH};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::agent_sessions::event_facts as facts;
use crate::redact::{current_policy, RedactionPolicy};

pub const SESSION_RECAP_SCHEMA_VERSION: u32 = 1;
pub const SESSION_RECAP_QUERY_METHOD: &str = "harn.session_recap.query";
pub const SESSION_RECAP_SCHEMA_ARTIFACT: &str = "schemas/session-recap-v1.schema.json";
pub const DEFAULT_RECAP_SOURCE_LIMIT: usize = 4_096;
pub const MAX_RECAP_SOURCE_LIMIT: usize = 32_768;

/// One bounded read over a durable agent session.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
pub struct SessionRecapQuery {
    #[serde(alias = "session_id")]
    pub session_id: String,
    #[serde(alias = "run_id")]
    pub run_id: Option<String>,
    #[serde(alias = "turn_id")]
    pub turn_id: Option<String>,
    #[serde(alias = "from_event_id")]
    pub from_event_id: Option<u64>,
    pub limit: Option<usize>,
}

impl SessionRecapQuery {
    pub fn for_session(session_id: impl Into<String>) -> Self {
        Self {
            session_id: session_id.into(),
            ..Self::default()
        }
    }

    fn limit(&self) -> usize {
        self.limit
            .unwrap_or(DEFAULT_RECAP_SOURCE_LIMIT)
            .clamp(1, MAX_RECAP_SOURCE_LIMIT)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SessionRecapSnapshot {
    pub schema_version: u32,
    pub session_id: String,
    pub query: SessionRecapQuery,
    pub cursor: SessionRecapCursor,
    pub coverage: SessionRecapCoverage,
    pub source: SessionRecapSource,
    pub content_hash: String,
    pub projection_hash: String,
    pub turns: Vec<PromptTurnRecap>,
    /// Explicit forward-compatible wire data. Unknown fields outside this map
    /// are not part of schema v1 and must not be written back by consumers.
    #[serde(default)]
    pub extensions: BTreeMap<String, serde_json::Value>,
}

/// Why a terminal result could not carry a deterministic recap snapshot.
///
/// The reason remains explicit so a projection failure cannot masquerade as
/// either an empty session or a successful result with no recap facts.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SessionRecapUnavailableReason {
    JournalUnavailable,
    SessionMissing,
    ProjectionFailed,
    AdmissionTerminal,
}

/// Terminal `AgentResult` projection of recap availability.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum SessionRecapAvailability {
    Available {
        snapshot: Box<SessionRecapSnapshot>,
    },
    Unavailable {
        reason: SessionRecapUnavailableReason,
    },
}

impl SessionRecapAvailability {
    pub fn available(snapshot: SessionRecapSnapshot) -> Self {
        Self::Available {
            snapshot: Box::new(snapshot),
        }
    }

    pub fn unavailable(reason: SessionRecapUnavailableReason) -> Self {
        Self::Unavailable { reason }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
pub struct SessionRecapCursor {
    pub last_event_id: Option<u64>,
    pub next_event_id: Option<u64>,
}

/// Visible instrument shape for a bounded projection.
///
/// `scanned` counts canonical source rows read. `matched` counts rows that
/// contributed a recap fact. `pending` counts rows beyond this bounded read.
/// `unassigned` counts recognized facts lacking the identities or iteration
/// boundary needed to place them without guessing.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
pub struct SessionRecapCoverage {
    pub scanned: usize,
    pub matched: usize,
    pub pending: usize,
    pub unassigned: usize,
    pub truncated: bool,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
pub struct SessionRecapSource {
    pub first_event_id: Option<u64>,
    pub last_event_id: Option<u64>,
    pub events: Vec<SessionRecapSourceEvent>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SessionRecapSourceEvent {
    pub event_id: u64,
    pub record_hash: String,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapCompletionState {
    Open,
    Complete,
    Incomplete,
    Unassigned,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PromptTurnRecap {
    pub turn_id: String,
    pub run_id: String,
    pub state: RecapCompletionState,
    pub prompts: Vec<RecapTextFact>,
    pub iterations: Vec<IterationRecap>,
    pub terminal: Option<RecapTerminalFact>,
    pub source_event_ids: Vec<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct IterationRecap {
    /// `None` is an explicit unassigned segment for a recognized fact that
    /// arrived outside durable iteration checkpoints. It is not a new persisted
    /// identity and never aliases a real iteration number.
    pub iteration: Option<i64>,
    pub state: RecapCompletionState,
    pub assistant_text: Vec<RecapTextFact>,
    pub tools: Vec<RecapToolExchange>,
    pub plans: Vec<RecapPlanFact>,
    pub progress: Vec<RecapProgressFact>,
    pub source_event_ids: Vec<u64>,
    #[serde(skip)]
    started: bool,
    #[serde(skip)]
    ended: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapTextFact {
    pub text: String,
    pub source_event_id: u64,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapToolState {
    Open,
    Completed,
    Failed,
    Incomplete,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapToolExchange {
    pub tool_call_id: String,
    pub tool_name: Option<String>,
    pub state: RecapToolState,
    pub call_observed: bool,
    pub result_observed: bool,
    pub input: Option<serde_json::Value>,
    pub output: Option<serde_json::Value>,
    pub verification: Option<RecapVerificationFact>,
    pub source_event_ids: Vec<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapVerificationFact {
    pub schema: String,
    pub status: RecapVerificationStatus,
    pub verified_paths: Vec<String>,
    pub source_event_id: u64,
}

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

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapPlanFact {
    pub document_id: String,
    pub revision_id: String,
    pub title: String,
    pub summary: String,
    pub steps: Vec<RecapPlanStep>,
    pub event: Option<RecapPlanEventFact>,
    pub source_event_id: u64,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapPlanStepStatus {
    Pending,
    InProgress,
    Completed,
    Blocked,
    Cancelled,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapPlanStep {
    pub id: String,
    pub content: String,
    pub status: RecapPlanStepStatus,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapPlanEventKind {
    Created,
    Updated,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapPlanEventFact {
    pub kind: RecapPlanEventKind,
    pub event_id: String,
    pub input_revision_id: Option<String>,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapProgressStatus {
    Pending,
    InProgress,
    Completed,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RecapProgressPriority {
    High,
    Medium,
    Low,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapProgressEntry {
    pub content: String,
    pub status: RecapProgressStatus,
    pub priority: Option<RecapProgressPriority>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapProgressFact {
    pub message: Option<String>,
    pub entries: Vec<RecapProgressEntry>,
    pub replace: bool,
    pub source_event_id: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct RecapTerminalFact {
    pub state: RecapCompletionState,
    pub final_status: Option<String>,
    pub stop_reason: Option<String>,
    pub kind: Option<String>,
    pub owner: Option<String>,
    pub reason: Option<String>,
    pub source_event_id: u64,
}

#[derive(Debug)]
pub struct SessionRecapError(String);

impl std::fmt::Display for SessionRecapError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl std::error::Error for SessionRecapError {}

/// Project one canonical session through the deterministic recap interface.
///
/// `None` means the session does not exist. An existing session with no events
/// returns `Some` with zero scanned rows and zero turns.
pub async fn query_session_recap(
    store: &dyn SessionStore,
    query: SessionRecapQuery,
) -> Result<Option<SessionRecapSnapshot>, SessionRecapError> {
    if query.session_id.trim().is_empty() {
        return Err(SessionRecapError(
            "session recap requires a non-empty session_id".to_string(),
        ));
    }
    let meta = match store.describe(&query.session_id).await {
        Ok(meta) => meta,
        Err(harn_session_store::StoreError::NotFound(_)) => return Ok(None),
        Err(error) => return Err(SessionRecapError(error.to_string())),
    };
    let source_events = read_source_events(store, &query).await?;
    let last_event_id = source_events.last().map(|event| event.event_id);
    let next_event_id = last_event_id
        .filter(|last| meta.last_event_id.is_some_and(|tail| tail > *last))
        .map(|last| last.saturating_add(1));
    let pending = last_event_id
        .zip(meta.last_event_id)
        .map(|(last, tail)| tail.saturating_sub(last) as usize)
        .unwrap_or(0);
    let policy = current_policy();
    let mut projector = RecapProjector::new(&query, &policy);
    for event in &source_events {
        projector.absorb(event);
    }
    let (turns, matched, unassigned) = projector.finish();
    let source = SessionRecapSource {
        first_event_id: source_events.first().map(|event| event.event_id),
        last_event_id,
        events: source_events
            .iter()
            .map(|event| SessionRecapSourceEvent {
                event_id: event.event_id,
                record_hash: event.source_record_hash().to_string(),
            })
            .collect(),
    };
    let content_hash = sha256_canonical(&source);
    let cursor = SessionRecapCursor {
        last_event_id,
        next_event_id,
    };
    let coverage = SessionRecapCoverage {
        scanned: source_events.len(),
        matched,
        pending,
        unassigned,
        truncated: next_event_id.is_some(),
    };
    let projection_hash = recap_projection_hash(
        &query.session_id,
        &query,
        &cursor,
        &coverage,
        &content_hash,
        &turns,
    );
    Ok(Some(SessionRecapSnapshot {
        schema_version: SESSION_RECAP_SCHEMA_VERSION,
        session_id: query.session_id.clone(),
        query,
        cursor,
        coverage,
        source,
        content_hash,
        projection_hash,
        turns,
        extensions: BTreeMap::new(),
    }))
}

/// Query the canonical store rooted at a project without inventing a second
/// persistence path. `None` means either the store or the named session does
/// not exist; an existing empty session returns an empty snapshot.
pub async fn query_persisted_session_recap(
    project_root: &Path,
    query: SessionRecapQuery,
) -> Result<Option<SessionRecapSnapshot>, SessionRecapError> {
    let Some(store) = crate::stdlib::session_store::open_existing_canonical_store(project_root)
        .map_err(|error| SessionRecapError(error.to_string()))?
    else {
        return Ok(None);
    };
    query_session_recap(&store, query).await
}

mod schema;
pub use schema::session_recap_json_schema;

async fn read_source_events(
    store: &dyn SessionStore,
    query: &SessionRecapQuery,
) -> Result<Vec<StoredEvent>, SessionRecapError> {
    let mut events = Vec::with_capacity(query.limit().min(MAX_READ_BATCH));
    let mut from = query.from_event_id;
    while events.len() < query.limit() {
        let remaining = query.limit() - events.len();
        let page = store
            .read(
                &query.session_id,
                ReadRange {
                    from_event_id: from,
                    limit: Some(remaining.min(MAX_READ_BATCH)),
                    ..ReadRange::default()
                },
            )
            .await
            .map_err(|error| SessionRecapError(error.to_string()))?;
        events.extend(page.events);
        let Some(next) = page.next_cursor else {
            break;
        };
        if events.len() >= query.limit() {
            break;
        }
        from = Some(next);
    }
    Ok(events)
}

struct TurnDraft {
    recap: PromptTurnRecap,
    iteration_positions: HashMap<Option<i64>, usize>,
    current_iteration: Option<i64>,
}

struct RecapProjector<'a> {
    query: &'a SessionRecapQuery,
    policy: &'a RedactionPolicy,
    turns: Vec<TurnDraft>,
    turn_positions: HashMap<(String, String), usize>,
    matched: usize,
    unassigned: usize,
}

impl<'a> RecapProjector<'a> {
    fn new(query: &'a SessionRecapQuery, policy: &'a RedactionPolicy) -> Self {
        Self {
            query,
            policy,
            turns: Vec::new(),
            turn_positions: HashMap::new(),
            matched: 0,
            unassigned: 0,
        }
    }

    fn absorb(&mut self, event: &StoredEvent) {
        if !is_recap_event(event) {
            return;
        }
        let run_id = event.headers.get("run_id").cloned();
        let turn_id = event.headers.get("turn_id").cloned();
        if run_id.is_none() || turn_id.is_none() {
            self.unassigned += 1;
            return;
        }
        if self
            .query
            .run_id
            .as_ref()
            .is_some_and(|expected| run_id.as_ref() != Some(expected))
            || self
                .query
                .turn_id
                .as_ref()
                .is_some_and(|expected| turn_id.as_ref() != Some(expected))
        {
            return;
        }
        let (Some(run_id), Some(turn_id)) = (run_id, turn_id) else {
            self.unassigned += 1;
            return;
        };
        let turn_index = self.turn_index(run_id, turn_id);
        self.turns[turn_index]
            .recap
            .source_event_ids
            .push(event.event_id);

        if is_checkpoint(event) {
            self.absorb_checkpoint(turn_index, event);
        } else if matches!(event.kind, SessionEventKind::Message) {
            self.absorb_message(turn_index, event);
        } else if matches!(event.kind, SessionEventKind::ToolCall) {
            self.absorb_tool_call(turn_index, event);
        } else if matches!(event.kind, SessionEventKind::ToolResult) {
            self.absorb_tool_result(turn_index, event);
        } else if matches!(event.kind, SessionEventKind::Plan) {
            self.absorb_plan(turn_index, event);
        } else if event.kind.discriminator() == "progress_reported" {
            self.absorb_progress(turn_index, event);
        } else if event.kind.discriminator() == "agent_run_terminal" {
            self.absorb_terminal(turn_index, event);
        }
    }

    fn turn_index(&mut self, run_id: String, turn_id: String) -> usize {
        let key = (run_id.clone(), turn_id.clone());
        if let Some(index) = self.turn_positions.get(&key) {
            return *index;
        }
        let index = self.turns.len();
        self.turns.push(TurnDraft {
            recap: PromptTurnRecap {
                turn_id,
                run_id,
                state: RecapCompletionState::Open,
                prompts: Vec::new(),
                iterations: Vec::new(),
                terminal: None,
                source_event_ids: Vec::new(),
            },
            iteration_positions: HashMap::new(),
            current_iteration: None,
        });
        self.turn_positions.insert(key, index);
        index
    }

    fn iteration_index(&mut self, turn_index: usize, iteration: Option<i64>) -> usize {
        if let Some(index) = self.turns[turn_index].iteration_positions.get(&iteration) {
            return *index;
        }
        let index = self.turns[turn_index].recap.iterations.len();
        self.turns[turn_index]
            .recap
            .iterations
            .push(IterationRecap {
                iteration,
                state: if iteration.is_some() {
                    RecapCompletionState::Open
                } else {
                    RecapCompletionState::Unassigned
                },
                assistant_text: Vec::new(),
                tools: Vec::new(),
                plans: Vec::new(),
                progress: Vec::new(),
                source_event_ids: Vec::new(),
                started: false,
                ended: false,
            });
        self.turns[turn_index]
            .iteration_positions
            .insert(iteration, index);
        index
    }

    fn current_segment(&mut self, turn_index: usize) -> usize {
        let iteration = self.turns[turn_index].current_iteration;
        if iteration.is_none() {
            self.unassigned += 1;
        }
        self.iteration_index(turn_index, iteration)
    }

    fn mark_segment_event(&mut self, turn_index: usize, segment_index: usize, event_id: u64) {
        self.turns[turn_index].recap.iterations[segment_index]
            .source_event_ids
            .push(event_id);
        self.matched += 1;
    }

    fn absorb_checkpoint(&mut self, turn_index: usize, event: &StoredEvent) {
        let kind = facts::string_at(&event.payload, facts::CHECKPOINT_KIND);
        let iteration = facts::i64_at(&event.payload, facts::ITERATION);
        let Some(iteration) = iteration else {
            self.unassigned += 1;
            return;
        };
        let segment_index = self.iteration_index(turn_index, Some(iteration));
        let segment = &mut self.turns[turn_index].recap.iterations[segment_index];
        segment.source_event_ids.push(event.event_id);
        match kind.as_deref() {
            Some("iteration_start") => {
                segment.started = true;
                self.turns[turn_index].current_iteration = Some(iteration);
                self.matched += 1;
            }
            Some("iteration_end") => {
                segment.ended = true;
                if self.turns[turn_index].current_iteration == Some(iteration) {
                    self.turns[turn_index].current_iteration = None;
                }
                self.matched += 1;
            }
            _ => self.unassigned += 1,
        }
    }

    fn absorb_message(&mut self, turn_index: usize, event: &StoredEvent) {
        let role = facts::semantic_string(&event.payload, &facts::ROLE);
        let visibility = facts::string_at(&event.payload, facts::VISIBILITY);
        let Some(text) = facts::semantic_string(&event.payload, &facts::TEXT) else {
            return;
        };
        let text = self.policy.redact_string(&text).into_owned();
        match role.as_deref() {
            Some("user") if visibility.as_deref().is_none_or(|value| value == "public") => {
                self.turns[turn_index].recap.prompts.push(RecapTextFact {
                    text,
                    source_event_id: event.event_id,
                });
                self.matched += 1;
            }
            Some("assistant") if visibility.as_deref() == Some("public") => {
                let segment_index = self.current_segment(turn_index);
                self.turns[turn_index].recap.iterations[segment_index]
                    .assistant_text
                    .push(RecapTextFact {
                        text,
                        source_event_id: event.event_id,
                    });
                self.mark_segment_event(turn_index, segment_index, event.event_id);
            }
            _ => {}
        }
    }

    fn absorb_tool_call(&mut self, turn_index: usize, event: &StoredEvent) {
        let segment_index = self.current_segment(turn_index);
        let Some(tool_call_id) = tool_call_id(event) else {
            self.unassigned += 1;
            return;
        };
        let input = facts::semantic_value(&event.payload, &facts::TOOL_INPUT_ANY)
            .map(|value| self.policy.redact_json(&value));
        let tool = RecapToolExchange {
            tool_call_id,
            tool_name: facts::semantic_string(&event.payload, &facts::TOOL_NAME_ANY),
            state: RecapToolState::Open,
            call_observed: true,
            result_observed: false,
            input,
            output: None,
            verification: None,
            source_event_ids: vec![event.event_id],
        };
        upsert_tool(
            &mut self.turns[turn_index].recap.iterations,
            segment_index,
            tool,
        );
        self.mark_segment_event(turn_index, segment_index, event.event_id);
    }

    fn absorb_tool_result(&mut self, turn_index: usize, event: &StoredEvent) {
        let Some(tool_call_id) = tool_call_id(event) else {
            self.unassigned += 1;
            return;
        };
        let existing = find_tool(&self.turns[turn_index].recap.iterations, &tool_call_id);
        let segment_index = existing
            .map(|(segment, _)| segment)
            .unwrap_or_else(|| self.current_segment(turn_index));
        let output = facts::semantic_value(&event.payload, &facts::TOOL_OUTPUT_ANY)
            .map(|value| self.policy.redact_json(&value));
        let failed = facts::bool_at_any(&event.payload, &facts::TOOL_IS_ERROR_ANY);
        let verification = verification_fact(event, self.policy);
        let result = RecapToolExchange {
            tool_call_id,
            tool_name: facts::semantic_string(&event.payload, &facts::TOOL_NAME_ANY),
            state: if failed {
                RecapToolState::Failed
            } else {
                RecapToolState::Completed
            },
            call_observed: false,
            result_observed: true,
            input: None,
            output,
            verification,
            source_event_ids: vec![event.event_id],
        };
        upsert_tool(
            &mut self.turns[turn_index].recap.iterations,
            segment_index,
            result,
        );
        self.mark_segment_event(turn_index, segment_index, event.event_id);
    }

    fn absorb_plan(&mut self, turn_index: usize, event: &StoredEvent) {
        let Some(document_value) = event.payload.pointer(facts::PLAN_DOCUMENT).cloned() else {
            self.unassigned += 1;
            return;
        };
        let Ok(document) = serde_json::from_value::<crate::llm::plan::PlanDocument>(document_value)
        else {
            self.unassigned += 1;
            return;
        };
        if document.validate().is_err() {
            self.unassigned += 1;
            return;
        }
        let plan_event = match event.payload.pointer(facts::PLAN_DOCUMENT_EVENT) {
            Some(value) => {
                let Ok(plan_event) =
                    serde_json::from_value::<crate::llm::plan::PlanDocumentEvent>(value.clone())
                else {
                    self.unassigned += 1;
                    return;
                };
                if plan_event.document() != &document || plan_event.document().validate().is_err() {
                    self.unassigned += 1;
                    return;
                }
                Some(match plan_event {
                    crate::llm::plan::PlanDocumentEvent::Created { event_id, .. } => {
                        RecapPlanEventFact {
                            kind: RecapPlanEventKind::Created,
                            event_id,
                            input_revision_id: None,
                        }
                    }
                    crate::llm::plan::PlanDocumentEvent::Updated {
                        event_id,
                        input_revision_id,
                        ..
                    } => RecapPlanEventFact {
                        kind: RecapPlanEventKind::Updated,
                        event_id,
                        input_revision_id: Some(input_revision_id),
                    },
                })
            }
            None => None,
        };
        let plan = &document.current_revision.plan;
        let document_id = document.document_id.clone();
        let revision_id = document.current_revision.revision_id.clone();
        let title = self.policy.redact_string(&plan.title).into_owned();
        let summary = self.policy.redact_string(&plan.summary).into_owned();
        let steps = plan
            .steps
            .iter()
            .map(|step| RecapPlanStep {
                id: step.id.clone(),
                content: self.policy.redact_string(&step.content).into_owned(),
                status: recap_plan_step_status(&step.status),
            })
            .collect();
        let segment_index = self.current_segment(turn_index);
        self.turns[turn_index].recap.iterations[segment_index]
            .plans
            .push(RecapPlanFact {
                document_id,
                revision_id,
                title,
                summary,
                steps,
                event: plan_event,
                source_event_id: event.event_id,
            });
        self.mark_segment_event(turn_index, segment_index, event.event_id);
    }

    fn absorb_progress(&mut self, turn_index: usize, event: &StoredEvent) {
        let metadata = event
            .payload
            .pointer(facts::TRANSCRIPT_METADATA)
            .unwrap_or(&serde_json::Value::Null);
        let message = metadata
            .get("message")
            .and_then(serde_json::Value::as_str)
            .map(|value| self.policy.redact_string(value).into_owned());
        let entries = metadata
            .get("entries")
            .and_then(serde_json::Value::as_array)
            .map(|entries| {
                entries
                    .iter()
                    .map(|entry| progress_entry(entry, self.policy))
                    .collect::<Result<Vec<_>, _>>()
            })
            .transpose();
        let Ok(entries) = entries else {
            self.unassigned += 1;
            return;
        };
        let entries = entries.unwrap_or_default();
        if message.is_none() && entries.is_empty() {
            self.unassigned += 1;
            return;
        }
        let segment_index = self.current_segment(turn_index);
        self.turns[turn_index].recap.iterations[segment_index]
            .progress
            .push(RecapProgressFact {
                message,
                entries,
                replace: metadata
                    .get("replace")
                    .and_then(serde_json::Value::as_bool)
                    .unwrap_or(true),
                source_event_id: event.event_id,
            });
        self.mark_segment_event(turn_index, segment_index, event.event_id);
    }

    fn absorb_terminal(&mut self, turn_index: usize, event: &StoredEvent) {
        let kind = facts::string_at(&event.payload, facts::TERMINAL_KIND);
        let owner = facts::string_at(&event.payload, facts::TERMINAL_OWNER);
        let state = if kind.is_some() && owner.is_some() {
            RecapCompletionState::Complete
        } else {
            RecapCompletionState::Incomplete
        };
        let redact =
            |value: Option<String>| value.map(|text| self.policy.redact_string(&text).into_owned());
        self.turns[turn_index].recap.terminal = Some(RecapTerminalFact {
            state,
            final_status: redact(facts::string_at(&event.payload, facts::FINAL_STATUS)),
            stop_reason: redact(facts::string_at(&event.payload, facts::STOP_REASON)),
            kind: redact(kind),
            owner: redact(owner),
            reason: redact(facts::string_at(&event.payload, facts::TERMINAL_REASON)),
            source_event_id: event.event_id,
        });
        self.matched += 1;
    }

    fn finish(mut self) -> (Vec<PromptTurnRecap>, usize, usize) {
        for turn in &mut self.turns {
            let has_terminal = turn.recap.terminal.is_some();
            let mut incomplete = turn
                .recap
                .terminal
                .as_ref()
                .is_some_and(|terminal| terminal.state == RecapCompletionState::Incomplete);
            for iteration in &mut turn.recap.iterations {
                if iteration.iteration.is_none() {
                    iteration.state = RecapCompletionState::Unassigned;
                    incomplete = true;
                } else if iteration.started && iteration.ended {
                    iteration.state = RecapCompletionState::Complete;
                } else {
                    iteration.state = RecapCompletionState::Incomplete;
                    incomplete = true;
                }
                for tool in &mut iteration.tools {
                    if tool.call_observed && tool.result_observed {
                        // Preserve the result-owned completed/failed state.
                    } else {
                        tool.state = RecapToolState::Incomplete;
                        incomplete = true;
                    }
                }
            }
            turn.recap.state = if incomplete {
                RecapCompletionState::Incomplete
            } else if has_terminal {
                RecapCompletionState::Complete
            } else if turn.recap.iterations.is_empty() {
                RecapCompletionState::Open
            } else {
                RecapCompletionState::Incomplete
            };
        }
        (
            self.turns.into_iter().map(|turn| turn.recap).collect(),
            self.matched,
            self.unassigned,
        )
    }
}

fn is_recap_event(event: &StoredEvent) -> bool {
    matches!(
        event.kind,
        SessionEventKind::Message
            | SessionEventKind::ToolCall
            | SessionEventKind::ToolResult
            | SessionEventKind::Plan
    ) || matches!(
        event.kind.discriminator(),
        "loop_checkpoint" | "progress_reported" | "agent_run_terminal"
    )
}

fn is_checkpoint(event: &StoredEvent) -> bool {
    event.kind.discriminator() == "loop_checkpoint"
}

fn tool_call_id(event: &StoredEvent) -> Option<String> {
    event
        .headers
        .get("tool_call_id")
        .cloned()
        .or_else(|| facts::string_at(&event.payload, facts::TOOL_CALL_ID))
        .or_else(|| {
            event
                .payload
                .pointer(facts::TOOL_RESULT_FACT_CALL_ID)
                .and_then(serde_json::Value::as_str)
                .map(str::to_string)
        })
}

fn find_tool(iterations: &[IterationRecap], call_id: &str) -> Option<(usize, usize)> {
    iterations
        .iter()
        .enumerate()
        .find_map(|(segment, iteration)| {
            iteration
                .tools
                .iter()
                .position(|tool| tool.tool_call_id == call_id)
                .map(|tool| (segment, tool))
        })
}

fn upsert_tool(
    iterations: &mut [IterationRecap],
    segment_index: usize,
    incoming: RecapToolExchange,
) {
    if let Some((existing_segment, existing_tool)) = find_tool(iterations, &incoming.tool_call_id) {
        let tool = &mut iterations[existing_segment].tools[existing_tool];
        if incoming.tool_name.is_some() {
            tool.tool_name = incoming.tool_name;
        }
        tool.call_observed |= incoming.call_observed;
        tool.result_observed |= incoming.result_observed;
        if incoming.input.is_some() {
            tool.input = incoming.input;
        }
        if incoming.output.is_some() {
            tool.output = incoming.output;
        }
        if incoming.verification.is_some() {
            tool.verification = incoming.verification;
        }
        if incoming.result_observed {
            tool.state = incoming.state;
        }
        tool.source_event_ids.extend(incoming.source_event_ids);
        return;
    }
    iterations[segment_index].tools.push(incoming);
}

fn recap_plan_step_status(status: &crate::llm::plan::PlanStepStatus) -> RecapPlanStepStatus {
    match status {
        crate::llm::plan::PlanStepStatus::Pending => RecapPlanStepStatus::Pending,
        crate::llm::plan::PlanStepStatus::InProgress => RecapPlanStepStatus::InProgress,
        crate::llm::plan::PlanStepStatus::Completed => RecapPlanStepStatus::Completed,
        crate::llm::plan::PlanStepStatus::Blocked => RecapPlanStepStatus::Blocked,
        crate::llm::plan::PlanStepStatus::Cancelled => RecapPlanStepStatus::Cancelled,
    }
}

fn progress_entry(
    value: &serde_json::Value,
    policy: &RedactionPolicy,
) -> Result<RecapProgressEntry, ()> {
    let content = value
        .get("content")
        .and_then(serde_json::Value::as_str)
        .filter(|content| !content.trim().is_empty())
        .ok_or(())?;
    let status = match value.get("status").and_then(serde_json::Value::as_str) {
        Some("pending") => RecapProgressStatus::Pending,
        Some("in_progress") => RecapProgressStatus::InProgress,
        Some("completed") => RecapProgressStatus::Completed,
        _ => return Err(()),
    };
    let priority = match value.get("priority") {
        None | Some(serde_json::Value::Null) => None,
        Some(serde_json::Value::String(priority)) => Some(match priority.as_str() {
            "high" => RecapProgressPriority::High,
            "medium" => RecapProgressPriority::Medium,
            "low" => RecapProgressPriority::Low,
            _ => return Err(()),
        }),
        _ => return Err(()),
    };
    Ok(RecapProgressEntry {
        content: policy.redact_string(content).into_owned(),
        status,
        priority,
    })
}

fn verification_fact(
    event: &StoredEvent,
    policy: &RedactionPolicy,
) -> Option<RecapVerificationFact> {
    let value = event.payload.pointer(facts::TOOL_VERIFICATION)?;
    let schema = value.get("schema")?.as_str()?;
    let status = value.get("status")?.as_str()?;
    if schema != "harn.agent_tool_postcondition.v1" || status != "passed" {
        return None;
    }
    let verified_paths = value
        .get("verified_paths")?
        .as_array()?
        .iter()
        .map(serde_json::Value::as_str)
        .collect::<Option<Vec<_>>>()?
        .into_iter()
        .map(|path| policy.redact_string(path).into_owned())
        .collect();
    Some(RecapVerificationFact {
        schema: schema.to_string(),
        status: RecapVerificationStatus::Passed,
        verified_paths,
        source_event_id: event.event_id,
    })
}

/// Hash the Harn-owned deterministic projection fields.
///
/// `extensions` are deliberately outside this base hash. A decorative
/// extension must carry its own binding to this projection hash before a
/// consumer treats the extension as content-bound evidence.
fn recap_projection_hash(
    session_id: &str,
    query: &SessionRecapQuery,
    cursor: &SessionRecapCursor,
    coverage: &SessionRecapCoverage,
    content_hash: &str,
    turns: &[PromptTurnRecap],
) -> String {
    sha256_canonical(&serde_json::json!({
        "schema_version": SESSION_RECAP_SCHEMA_VERSION,
        "session_id": session_id,
        "query": query,
        "cursor": cursor,
        "coverage": coverage,
        "content_hash": content_hash,
        "turns": turns,
    }))
}

fn sha256_canonical<T: Serialize>(value: &T) -> String {
    let json = serde_json::to_value(value).expect("recap contract must serialize");
    format!(
        "sha256:{}",
        hex::encode(Sha256::digest(crate::canonical_json::to_vec(&json)))
    )
}

#[cfg(test)]
#[path = "session_recap_tests.rs"]
mod tests;