| 1025 | /// Attaching is idempotent: re-attaching the same evidence is a no-op. |
| 1026 | #[allow(clippy::too_many_arguments)] |
| 1027 | pub fn attach_turn_boundary( |
| 1028 | &mut self, |
| 1029 | session_id: &str, |
| 1030 | provenance_hash: &crate::types::Hash, |
| 1031 | boundary_start: &crate::change::session::TurnBoundary, |
| 1032 | boundary_end: &crate::change::session::TurnBoundary, |
| 1033 | outcome: &crate::change::session::ManagedTurnOutcome, |
| 1034 | ) -> PristineResult<bool> { |
| 1035 | use crate::change::session::{ |
| 1036 | encode_session_turn_key, session_turn_namespace, SessionTurn, |
| 1037 | }; |
| 1038 | |
| 1039 | let mut turns = self.txn.open_table(SESSION_TURNS)?; |
| 1040 | let namespace = session_turn_namespace(session_id); |
| 1041 | let scan_start = encode_session_turn_key(namespace, 0); |
| 1042 | let scan_end = encode_session_turn_key(namespace, u32::MAX); |
| 1043 | |
| 1044 | let mut matches: Vec<([u8; 40], SessionTurn)> = Vec::new(); |
| 1045 | for row in turns.range::<&[u8; 40]>(&scan_start..=&scan_end)? { |
| 1046 | let (key, value) = row?; |
| 1047 | let turn = SessionTurn::from_bytes(value.value()).map_err(|e| { |
| 1048 | PristineError::Serialization { |
| 1049 | message: format!("session turn decode: {}", e), |
| 1050 | } |
| 1051 | })?; |
| 1052 | if turn.session_id == session_id && turn.provenance_hash == *provenance_hash { |
| 1053 | matches.push((*key.value(), turn)); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | let Some((key, mut turn)) = matches.into_iter().next() else { |
| 1058 | return Ok(false); |
| 1059 | }; |
| 1060 | |
| 1061 | let unchanged = turn.boundary_start.as_ref() == Some(boundary_start) |
| 1062 | && turn.boundary_end.as_ref() == Some(boundary_end) |
| 1063 | && turn.outcome.as_ref() == Some(outcome); |
| 1064 | if unchanged { |
| 1065 | return Ok(true); |
| 1066 | } |
| 1067 | |
| 1068 | turn.boundary_start = Some(boundary_start.clone()); |
| 1069 | turn.boundary_end = Some(boundary_end.clone()); |
| 1070 | turn.outcome = Some(outcome.clone()); |
| 1071 | turns.insert(&key, turn.to_bytes().as_slice())?; |
| 1072 | drop(turns); |
| 1073 | Ok(true) |
| 1074 | } |
| 1075 | |
| 1076 | /// Store an immutable session manifest and advance its convenience head. |
| 1077 | pub fn save_session_manifest( |