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 | |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 106 | /// a room with retained events already shows the live end, and a limited sync has put its |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 107 | /// own gap there, so only an empty room walks back from the live end. otherwise the newest |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 108 | /// gap gets one page, so a limited sync that skipped nothing leaves no gap behind |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 109 | pub(super) async fn start_recent(&mut self) { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 110 | let retained = async { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 111 | let (cache, _handles) = self.room.event_cache().await?; |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 112 | if !cache.has_retained_events().await? { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 113 | return Ok(None); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 114 | } |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 115 | let gaps = cache.history_gaps().await?; |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 116 | Ok::<_, EventCacheError>(Some(gaps.into_iter().rev().find(|gap| { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 117 | matches!( |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 118 | gap.status, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 119 | HistoryGapStatus::Idle | HistoryGapStatus::Failed { .. } |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 120 | ) |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 121 | }))) |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 122 | } |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 123 | .await; |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 124 | match retained { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 125 | Ok(Some(Some(gap))) => self.start_gap_fill(gap.id, None, 1, false).await, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 126 | Ok(Some(None)) => {} |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 127 | Ok(None) | Err(_) => self.start_recent_with(None).await, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 128 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 129 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 130 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 131 | pub(super) async fn execute(&mut self, command: HistoryRepairCommand, reply: Responder) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 132 | match command { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 133 | HistoryRepairCommand::Cancel { .. } => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 134 | if let Some(running) = self.running.as_mut() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 135 | running.responders.push(PendingResponder { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 136 | responder: reply, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 137 | cancel_ack: true, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 138 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 139 | running.cancel.send_replace(true); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 140 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 141 | reply.done(Ok(())); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 142 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 143 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 144 | HistoryRepairCommand::RetryRecent { .. } => self.start_recent_with(Some(reply)).await, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 145 | HistoryRepairCommand::FillGap { gap_id, .. } => { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 146 | self.start_gap_fill(gap_id, Some(reply), MAX_GAP_PAGES, true) |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 147 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 148 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 149 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 150 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 151 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 152 | async fn start_recent_with(&mut self, responder: Option<Responder>) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 153 | if let Some(running) = &mut self.running { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 154 | if let Some(responder) = responder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 155 | if running.gap_id.is_none() { |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 156 | running.responders.push(PendingResponder { |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 157 | responder, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 158 | cancel_ack: false, |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 159 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 160 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 161 | responder.done(Err(room_busy())); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 162 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 163 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 164 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 165 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 166 | let event = Event::RecentHistoryChanged { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 167 | room_id: self.room.room_id().to_owned(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 168 | state: RecentHistoryState::Loading, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 169 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 170 | if self.context.events.send(event).await.is_err() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 171 | if let Some(responder) = responder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 172 | responder.done(Err(Error::interrupted())); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 173 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 174 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 175 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 176 | let (cancel, receiver) = watch::channel(false); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 177 | let room = self.room.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 178 | let task = spawn(async move { load_recent(room, receiver).await }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 179 | self.running = Some(Running { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 180 | task, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 181 | cancel, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 182 | responders: responder |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 183 | .map(|responder| PendingResponder { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 184 | responder, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 185 | cancel_ack: false, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 186 | }) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 187 | .into_iter() |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 188 | .collect(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 189 | gap_id: None, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 190 | closing: false, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 191 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 192 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 193 | |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 194 | async fn start_gap_fill( |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 195 | &mut self, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 196 | gap_id: OwnedTransactionId, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 197 | responder: Option<Responder>, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 198 | pages: u32, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 199 | spend_budget: bool, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 200 | ) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 201 | if self.is_running() { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 202 | if let Some(responder) = responder { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 203 | responder.done(Err(room_busy())); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 204 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 205 | return; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 206 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 207 | let room = self.room.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 208 | let progress = report(self.room.room_id().to_owned(), gap_id.clone()); |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 209 | if self |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 210 | .context |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 211 | .events |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 212 | .send(Event::HistoryRepairChanged(progress.clone())) |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 213 | .await |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 214 | .is_err() |
9eabb6b7587d | atagen | 2026-10-25 07:08 UTC | 215 | { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 216 | if let Some(responder) = responder { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 217 | responder.done(Err(Error::interrupted())); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 218 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 219 | return; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 220 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 221 | let (cancel, receiver) = watch::channel(false); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 222 | let task = |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 223 | spawn(async move { fill_gap(room, progress, receiver, pages, spend_budget).await }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 224 | self.running = Some(Running { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 225 | task, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 226 | cancel, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 227 | responders: responder |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 228 | .map(|responder| PendingResponder { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 229 | responder, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 230 | cancel_ack: false, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 231 | }) |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 232 | .into_iter() |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 233 | .collect(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 234 | gap_id: Some(gap_id), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 235 | closing: false, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 236 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 237 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 238 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 239 | pub(super) async fn next_completion(&mut self) -> Option<Completion> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 240 | let running = self.running.as_mut()?; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 241 | let result = (&mut running.task).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 242 | Some(self.finish_task(result)) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 243 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 244 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 245 | pub(super) async fn shutdown(&mut self) -> Option<Completion> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 246 | let running = self.running.as_mut()?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 247 | running.closing = true; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 248 | running.cancel.send_replace(true); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 249 | let result = (&mut running.task).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 250 | Some(self.finish_task(result)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 251 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 252 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 253 | fn finish_task(&mut self, result: Result<Finished, JoinError>) -> Completion { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 254 | let running = self.running.take().expect("repair task exists"); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 255 | let finished = result.unwrap_or(Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 256 | stopped: Stopped::Failed(Error::interrupted()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 257 | committed: false, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 258 | report: None, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 259 | }); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 260 | let result = match &finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 261 | Stopped::Done => Ok(CommandOutput::Done), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 262 | Stopped::Cancelled => Err(Error::interrupted()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 263 | Stopped::Failed(error) => Err(error.clone()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 264 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 265 | let outcome = match running.gap_id { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 266 | None => Outcome::Recent(match finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 267 | _ if running.closing => RecentHistoryState::Idle, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 268 | Stopped::Done => RecentHistoryState::Loaded, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 269 | Stopped::Cancelled => RecentHistoryState::Cancelled, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 270 | Stopped::Failed(error) => RecentHistoryState::Failed(error.message), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 271 | }), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 272 | Some(gap_id) => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 273 | let mut report = finished |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 274 | .report |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 275 | .unwrap_or_else(|| report(self.room.room_id().to_owned(), gap_id)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 276 | match finished.stopped { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 277 | Stopped::Done => {} |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 278 | Stopped::Cancelled => report.stop = Some(HistoryRepairStop::Cancelled), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 279 | Stopped::Failed(error) => report.failure = Some(error), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 280 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 281 | report.phase = if report.stop.is_none() && report.failure.is_none() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 282 | HistoryRepairPhase::Ready |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 283 | } else { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 284 | HistoryRepairPhase::Paused |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 285 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 286 | Outcome::Fill(report) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 287 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 288 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 289 | Completion { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 290 | outcome, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 291 | committed: finished.committed, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 292 | closing: running.closing, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 293 | result, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 294 | responders: running.responders, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 295 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 296 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 297 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 298 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 299 | async fn load_recent(room: Room, mut cancel: watch::Receiver<bool>) -> Finished { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 300 | let outcome = tokio::select! { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 301 | biased; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 302 | _ = cancel.changed() => None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 303 | result = async { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 304 | let (cache, _handles) = room.event_cache().await?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 305 | cache.load_recent_history().await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 306 | } => Some(result), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 307 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 308 | let stopped = match outcome { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 309 | None => Stopped::Cancelled, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 310 | Some(Ok(_)) => Stopped::Done, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 311 | Some(Err(error)) => Stopped::Failed(cache_error(error)), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 312 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 313 | Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 314 | committed: matches!(stopped, Stopped::Done), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 315 | stopped, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 316 | report: None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 317 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 318 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 319 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 320 | async fn fill_gap( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 321 | room: Room, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 322 | mut report: HistoryRepairReport, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 323 | mut cancel: watch::Receiver<bool>, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 324 | pages: u32, |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 325 | spend_budget: bool, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 326 | ) -> Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 327 | let mut committed = false; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 328 | let stopped = async { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 329 | let (cache, _handles) = room.event_cache().await.map_err(cache_error)?; |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 330 | for _ in 0..pages { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 331 | let next = tokio::select! { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 332 | biased; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 333 | _ = cancel.changed() => return Ok(Stopped::Cancelled), |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 334 | result = cache.fill_history_gap_once(report.gap_id.clone()) => match result { |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 335 | Ok(next) => next, |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 336 | Err(error) => { |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 337 | committed |= matches!(error, EventCacheError::PaginationError(_)); |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 338 | return Err(cache_error(error)); |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 339 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 340 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 341 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 342 | let (gap, events) = match next { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 343 | HistoryGapFillOutcome::NotFound => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 344 | return Err(Error::new( |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 345 | ErrorKind::EventNotFound, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 346 | "history gap was not found", |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 347 | )); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 348 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 349 | HistoryGapFillOutcome::Busy => return Err(room_busy()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 350 | HistoryGapFillOutcome::Updated { gap, events } => (gap, events), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 351 | }; |
06c73391cbae | atagen | 2026-10-25 10:26 UTC | 352 | // the sdk answers an already unavailable gap without asking the server, and a joined |
06c73391cbae | atagen | 2026-10-25 10:26 UTC | 353 | // one with NotFound. an empty first page that hits the end looks the same, uncounted |
924b7c011591 | atagen | 2026-10-25 10:28 UTC | 354 | let settled_before = |
924b7c011591 | atagen | 2026-10-25 10:28 UTC | 355 | report.pages == 0 && events == 0 && gap.status == HistoryGapStatus::Unavailable; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 356 | if !settled_before { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 357 | report.pages += 1; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 358 | committed = true; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 359 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 360 | report.events = report |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 361 | .events |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 362 | .saturating_add(u32::try_from(events).unwrap_or(u32::MAX)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 363 | match gap.status { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 364 | HistoryGapStatus::Joined => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 365 | report.continuity = HistoryContinuity::Verified; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 366 | return Ok(Stopped::Done); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 367 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 368 | HistoryGapStatus::Unavailable => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 369 | report.continuity = HistoryContinuity::AccessibleStart; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 370 | report.stop = Some(HistoryRepairStop::HistoryUnavailable); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 371 | return Ok(Stopped::Done); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 372 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 373 | HistoryGapStatus::Paused => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 374 | report.stop = Some(HistoryRepairStop::CursorStalled); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 375 | return Ok(Stopped::Done); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 376 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 377 | HistoryGapStatus::Failed { message } => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 378 | return Err(Error::new(ErrorKind::Network, message)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 379 | } |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 380 | HistoryGapStatus::Idle | HistoryGapStatus::BudgetSpent => {} |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 381 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 382 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 383 | report.stop = Some(HistoryRepairStop::Budget); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 384 | if spend_budget { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 385 | if let Err(error) = cache.spend_history_budget(report.gap_id.clone()).await { |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 386 | tracing::debug!(%error, "marking a history gap's spent budget failed"); |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 387 | } |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 388 | committed = true; |
417da9e719bc | atagen | 2026-10-26 02:40 UTC | 389 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 390 | Ok(Stopped::Done) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 391 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 392 | .await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 393 | .unwrap_or_else(Stopped::Failed); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 394 | Finished { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 395 | stopped, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 396 | committed, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 397 | report: Some(report), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 398 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 399 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 400 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 401 | fn report(room_id: OwnedRoomId, gap_id: OwnedTransactionId) -> HistoryRepairReport { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 402 | HistoryRepairReport { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 403 | room_id, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 404 | gap_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 405 | pages: 0, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 406 | events: 0, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 407 | phase: HistoryRepairPhase::Bridging, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 408 | continuity: HistoryContinuity::Unresolved, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 409 | stop: None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 410 | failure: None, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 411 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 412 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 413 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 414 | fn cache_error(source: EventCacheError) -> Error { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 415 | let kind = match &source { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 416 | EventCacheError::PaginationError(error) => crate::error::classify_sdk_error(error), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 417 | EventCacheError::InitialPaginationError(_) => ErrorKind::Network, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 418 | EventCacheError::RoomNotFound { .. } => ErrorKind::RoomUnknown, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 419 | EventCacheError::ClientDropped => ErrorKind::Interrupted, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 420 | _ => ErrorKind::Store, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 421 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 422 | Error::with_source(kind, "room history operation failed", source) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 423 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 424 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 425 | fn room_busy() -> Error { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 426 | Error::new( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 427 | ErrorKind::RoomBusy, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 428 | "room history operation is already running", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 429 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 430 | } |