d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 1 | use matrix_sdk::{ |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 2 | Room, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 3 | event_cache::{EventCacheError, HistoryGapFillOutcome, HistoryGapStatus}, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 4 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 5 | use ruma::{OwnedRoomId, OwnedTransactionId}; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 6 | use tokio::sync::watch; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 7 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 8 | use super::{ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 9 | executor::{JoinError, JoinHandle, spawn}, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 10 | timeline::SessionContext, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 11 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 12 | use crate::{ |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 13 | CommandOutput, Error, ErrorKind, Event, HistoryContinuity, HistoryRepairCommand, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 14 | HistoryRepairPhase, HistoryRepairReport, HistoryRepairStop, RecentHistoryState, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 15 | transport::Responder, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 16 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 17 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 18 | const MAX_GAP_PAGES: u32 = 4; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 19 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 20 | struct Running { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 21 | task: JoinHandle<Finished>, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 22 | cancel: watch::Sender<bool>, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 23 | responders: Vec<PendingResponder>, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 24 | gap_id: Option<OwnedTransactionId>, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 25 | closing: bool, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 26 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 27 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 28 | struct PendingResponder { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 29 | responder: Responder, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 30 | cancel_ack: bool, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 31 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 32 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 33 | pub(super) enum Outcome { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 34 | Recent(RecentHistoryState), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 35 | Fill(HistoryRepairReport), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 36 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 37 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 38 | pub(super) struct Completion { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 39 | pub(super) outcome: Outcome, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 40 | /// history went into the cache, so the room timeline has to show it before anyone hears |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 41 | pub(super) committed: bool, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 42 | pub(super) closing: bool, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 43 | pub(super) result: Result<CommandOutput, Error>, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 44 | responders: Vec<PendingResponder>, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 45 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 46 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 47 | impl Completion { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 48 | /// the room timeline couldn't show what was committed |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 49 | pub(super) fn unpublished(&mut self, error: Error) { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 50 | match &mut self.outcome { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 51 | Outcome::Recent(state) => *state = RecentHistoryState::Failed(error.message.clone()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 52 | Outcome::Fill(report) => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 53 | report.phase = HistoryRepairPhase::Paused; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 54 | report.failure = Some(error.clone()); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 55 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 56 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 57 | self.result = Err(error); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 58 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 59 | |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 60 | pub(super) fn resolve( |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 61 | self, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 62 | result: Result<CommandOutput, Error>, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 63 | cancellation: Result<(), Error>, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 64 | ) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 65 | for responder in self.responders { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 66 | if responder.cancel_ack { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 67 | responder.responder.done(cancellation.clone()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 68 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 69 | responder.responder.send(result.clone()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 70 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 71 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 72 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 73 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 74 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 75 | enum Stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 76 | Done, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 77 | Cancelled, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 78 | Failed(Error), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 79 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 80 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 81 | struct Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 82 | stopped: Stopped, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 83 | committed: bool, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 84 | report: Option<HistoryRepairReport>, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 85 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 86 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 87 | pub(super) struct RepairController { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 88 | room: Room, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 89 | context: SessionContext, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 90 | running: Option<Running>, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 91 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 92 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 93 | impl RepairController { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 94 | pub(super) fn new(room: Room, context: SessionContext) -> Self { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 95 | Self { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 96 | room, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 97 | context, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 98 | running: None, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 99 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 100 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 101 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 102 | pub(super) fn is_running(&self) -> bool { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 103 | self.running.is_some() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 104 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 105 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 106 | pub(super) async fn start_recent(&mut self) { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 107 | self.start_recent_with(None).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 108 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 109 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 110 | pub(super) async fn execute(&mut self, command: HistoryRepairCommand, reply: Responder) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 111 | match command { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 112 | HistoryRepairCommand::Cancel { .. } => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 113 | if let Some(running) = self.running.as_mut() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 114 | running.responders.push(PendingResponder { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 115 | responder: reply, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 116 | cancel_ack: true, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 117 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 118 | running.cancel.send_replace(true); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 119 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 120 | reply.done(Ok(())); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 121 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 122 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 123 | HistoryRepairCommand::RetryRecent { .. } => self.start_recent_with(Some(reply)).await, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 124 | HistoryRepairCommand::FillGap { gap_id, .. } => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 125 | self.start_gap_fill(gap_id, reply).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 126 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 127 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 128 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 129 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 130 | async fn start_recent_with(&mut self, responder: Option<Responder>) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 131 | if let Some(running) = &mut self.running { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 132 | if let Some(responder) = responder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 133 | if running.gap_id.is_none() { |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 134 | running.responders.push(PendingResponder { |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 135 | responder, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 136 | cancel_ack: false, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 137 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 138 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 139 | responder.done(Err(room_busy())); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 140 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 141 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 142 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 143 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 144 | let event = Event::RecentHistoryChanged { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 145 | room_id: self.room.room_id().to_owned(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 146 | state: RecentHistoryState::Loading, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 147 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 148 | if self.context.events.send(event).await.is_err() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 149 | if let Some(responder) = responder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 150 | responder.done(Err(Error::interrupted())); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 151 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 152 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 153 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 154 | let (cancel, receiver) = watch::channel(false); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 155 | let room = self.room.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 156 | let task = spawn(async move { load_recent(room, receiver).await }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 157 | self.running = Some(Running { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 158 | task, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 159 | cancel, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 160 | responders: responder |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 161 | .map(|responder| PendingResponder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 162 | responder, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 163 | cancel_ack: false, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 164 | }) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 165 | .into_iter() |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 166 | .collect(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 167 | gap_id: None, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 168 | closing: false, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 169 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 170 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 171 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 172 | async fn start_gap_fill(&mut self, gap_id: OwnedTransactionId, responder: Responder) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 173 | if self.is_running() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 174 | responder.done(Err(room_busy())); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 175 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 176 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 177 | let room = self.room.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 178 | let progress = report(self.room.room_id().to_owned(), gap_id.clone()); |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 179 | if self |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 180 | .context |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 181 | .events |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 182 | .send(Event::HistoryRepairChanged(progress.clone())) |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 183 | .await |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 184 | .is_err() |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 185 | { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 186 | responder.done(Err(Error::interrupted())); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 187 | return; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 188 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 189 | let (cancel, receiver) = watch::channel(false); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 190 | let task = spawn(async move { fill_gap(room, progress, receiver).await }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 191 | self.running = Some(Running { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 192 | task, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 193 | cancel, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 194 | responders: vec![PendingResponder { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 195 | responder, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 196 | cancel_ack: false, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 197 | }], |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 198 | gap_id: Some(gap_id), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 199 | closing: false, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 200 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 201 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 202 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 203 | pub(super) async fn next_completion(&mut self) -> Option<Completion> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 204 | let running = self.running.as_mut()?; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 205 | let result = (&mut running.task).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 206 | Some(self.finish_task(result)) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 207 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 208 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 209 | pub(super) async fn shutdown(&mut self) -> Option<Completion> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 210 | let running = self.running.as_mut()?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 211 | running.closing = true; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 212 | running.cancel.send_replace(true); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 213 | let result = (&mut running.task).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 214 | Some(self.finish_task(result)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 215 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 216 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 217 | fn finish_task(&mut self, result: Result<Finished, JoinError>) -> Completion { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 218 | let running = self.running.take().expect("repair task exists"); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 219 | let finished = result.unwrap_or(Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 220 | stopped: Stopped::Failed(Error::interrupted()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 221 | committed: false, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 222 | report: None, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 223 | }); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 224 | let result = match &finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 225 | Stopped::Done => Ok(CommandOutput::Done), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 226 | Stopped::Cancelled => Err(Error::interrupted()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 227 | Stopped::Failed(error) => Err(error.clone()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 228 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 229 | let outcome = match running.gap_id { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 230 | None => Outcome::Recent(match finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 231 | _ if running.closing => RecentHistoryState::Idle, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 232 | Stopped::Done => RecentHistoryState::Loaded, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 233 | Stopped::Cancelled => RecentHistoryState::Cancelled, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 234 | Stopped::Failed(error) => RecentHistoryState::Failed(error.message), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 235 | }), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 236 | Some(gap_id) => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 237 | let mut report = finished |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 238 | .report |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 239 | .unwrap_or_else(|| report(self.room.room_id().to_owned(), gap_id)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 240 | match finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 241 | Stopped::Done => {} |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 242 | Stopped::Cancelled => report.stop = Some(HistoryRepairStop::Cancelled), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 243 | Stopped::Failed(error) => report.failure = Some(error), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 244 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 245 | report.phase = if report.stop.is_none() && report.failure.is_none() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 246 | HistoryRepairPhase::Ready |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 247 | } else { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 248 | HistoryRepairPhase::Paused |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 249 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 250 | Outcome::Fill(report) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 251 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 252 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 253 | Completion { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 254 | outcome, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 255 | committed: finished.committed, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 256 | closing: running.closing, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 257 | result, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 258 | responders: running.responders, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 259 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 260 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 261 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 262 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 263 | async fn load_recent(room: Room, mut cancel: watch::Receiver<bool>) -> Finished { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 264 | let outcome = tokio::select! { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 265 | biased; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 266 | _ = cancel.changed() => None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 267 | result = async { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 268 | let (cache, _handles) = room.event_cache().await?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 269 | cache.load_recent_history().await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 270 | } => Some(result), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 271 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 272 | let stopped = match outcome { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 273 | None => Stopped::Cancelled, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 274 | Some(Ok(_)) => Stopped::Done, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 275 | Some(Err(error)) => Stopped::Failed(cache_error(error)), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 276 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 277 | Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 278 | committed: matches!(stopped, Stopped::Done), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 279 | stopped, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 280 | report: None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 281 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 282 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 283 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 284 | async fn fill_gap( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 285 | room: Room, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 286 | mut report: HistoryRepairReport, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 287 | mut cancel: watch::Receiver<bool>, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 288 | ) -> Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 289 | let mut committed = false; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 290 | let stopped = async { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 291 | let (cache, _handles) = room.event_cache().await.map_err(cache_error)?; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 292 | for _ in 0..MAX_GAP_PAGES { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 293 | let next = tokio::select! { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 294 | biased; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 295 | _ = cancel.changed() => return Ok(Stopped::Cancelled), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 296 | result = cache.fill_history_gap_once(report.gap_id.clone()) => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 297 | result.map_err(cache_error)? |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 298 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 299 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 300 | let (gap, events) = match next { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 301 | HistoryGapFillOutcome::NotFound => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 302 | return Err(Error::new( |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 303 | ErrorKind::EventNotFound, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 304 | "history gap was not found", |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 305 | )); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 306 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 307 | HistoryGapFillOutcome::Busy => return Err(room_busy()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 308 | HistoryGapFillOutcome::Updated { gap, events } => (gap, events), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 309 | }; |
06c73391cbae | atagen | 2026-10-25 10:26 UTC | 310 | // the sdk answers an already unavailable gap without asking the server, and a joined |
06c73391cbae | atagen | 2026-10-25 10:26 UTC | 311 | // one with NotFound. an empty first page that hits the end looks the same, uncounted |
924b7c011591 | atagen | 2026-10-25 10:28 UTC | 312 | let settled_before = |
924b7c011591 | atagen | 2026-10-25 10:28 UTC | 313 | report.pages == 0 && events == 0 && gap.status == HistoryGapStatus::Unavailable; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 314 | if !settled_before { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 315 | report.pages += 1; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 316 | committed = true; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 317 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 318 | report.events = report |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 319 | .events |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 320 | .saturating_add(u32::try_from(events).unwrap_or(u32::MAX)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 321 | match gap.status { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 322 | HistoryGapStatus::Joined => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 323 | report.continuity = HistoryContinuity::Verified; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 324 | return Ok(Stopped::Done); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 325 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 326 | HistoryGapStatus::Unavailable => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 327 | report.continuity = HistoryContinuity::AccessibleStart; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 328 | report.stop = Some(HistoryRepairStop::HistoryUnavailable); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 329 | return Ok(Stopped::Done); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 330 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 331 | HistoryGapStatus::Paused => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 332 | report.stop = Some(HistoryRepairStop::CursorStalled); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 333 | return Ok(Stopped::Done); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 334 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 335 | HistoryGapStatus::Failed { message } => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 336 | return Err(Error::new(ErrorKind::Network, message)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 337 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 338 | HistoryGapStatus::Idle => {} |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 339 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 340 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 341 | report.stop = Some(HistoryRepairStop::Budget); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 342 | Ok(Stopped::Done) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 343 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 344 | .await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 345 | .unwrap_or_else(Stopped::Failed); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 346 | Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 347 | stopped, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 348 | committed, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 349 | report: Some(report), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 350 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 351 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 352 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 353 | fn report(room_id: OwnedRoomId, gap_id: OwnedTransactionId) -> HistoryRepairReport { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 354 | HistoryRepairReport { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 355 | room_id, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 356 | gap_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 357 | pages: 0, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 358 | events: 0, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 359 | phase: HistoryRepairPhase::Bridging, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 360 | continuity: HistoryContinuity::Unresolved, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 361 | stop: None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 362 | failure: None, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 363 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 364 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 365 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 366 | fn cache_error(source: EventCacheError) -> Error { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 367 | let kind = match &source { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 368 | EventCacheError::PaginationError(error) => crate::error::classify_sdk_error(error), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 369 | EventCacheError::InitialPaginationError(_) => ErrorKind::Network, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 370 | EventCacheError::RoomNotFound { .. } => ErrorKind::RoomUnknown, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 371 | EventCacheError::ClientDropped => ErrorKind::Interrupted, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 372 | _ => ErrorKind::Store, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 373 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 374 | Error::with_source(kind, "room history operation failed", source) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 375 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 376 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 377 | fn room_busy() -> Error { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 378 | Error::new( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 379 | ErrorKind::RoomBusy, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 380 | "room history operation is already running", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 381 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 382 | } |