brain: Preserve queued context changes
02d8e58bb7e339acce308ddc459225b9c2d2028c
Keep one generation-tagged desired context change while an async refetch is in flight. Start it only after applying the current result so repeated expansion input is not lost. Assisted-by: Codex:gpt-5.6
1 parent
82295dab
crates/brain/src/session.rs
+28 -20
| 357 | 357 | } |
|
| 358 | 358 | TerminalEvent::Highlight(generation, HighlightOutput::Context(update)) => { |
|
| 359 | 359 | if generation != self.generation { |
|
| 360 | 360 | return Ok(Update::Continue); |
|
| 361 | 361 | } |
|
| 362 | - | // A missing update still completes the in-flight request. |
|
| 363 | - | self.context_fetch = ContextFetch::Idle; |
|
| 364 | - | if let Some(update) = update { |
|
| 362 | + | let redraw = if let Some(update) = update { |
|
| 365 | 363 | let area = viewer_area(self.terminal.area()); |
|
| 366 | 364 | if let Some(loaded) = &mut self.commits[self.current].loaded { |
|
| 367 | 365 | loaded.apply_context_update(update, &self.brain, area)?; |
|
| 368 | 366 | } |
|
| 369 | 367 | self.refresh_visibility()?; |
|
| 370 | - | return Ok(Update::Redraw); |
|
| 368 | + | true |
|
| 369 | + | } else { |
|
| 370 | + | false |
|
| 371 | + | }; |
|
| 372 | + | let desired = self.context_fetch.complete(generation); |
|
| 373 | + | if let Some(change) = desired { |
|
| 374 | + | self.request_context(change); |
|
| 371 | 375 | } |
|
| 372 | - | Ok(Update::Continue) |
|
| 376 | + | Ok(if redraw { |
|
| 377 | + | Update::Redraw |
|
| 378 | + | } else { |
|
| 379 | + | Update::Continue |
|
| 380 | + | }) |
|
| 373 | 381 | } |
|
| 374 | 382 | } |
|
| 375 | 383 | } |
|
| 376 | 384 | ||
| 377 | 385 | /// Dispatches one input event, handling review keys before the viewer. |
| 1099 | 1107 | Ok(()) |
|
| 1100 | 1108 | } |
|
| 1101 | 1109 | ||
| 1102 | 1110 | /// Requests context refetch work for one context change. |
|
| 1103 | 1111 | /// |
|
| 1104 | - | /// Only one context request is in flight per active commit. Requests are |
|
| 1105 | - | /// tied to the current highlighter generation by `activate`, so stale |
|
| 1106 | - | /// context updates are ignored in [`Session::update`]. |
|
| 1112 | + | /// One request runs per active generation. A change received while it runs |
|
| 1113 | + | /// becomes a bounded desired follow-up, which starts after the viewer |
|
| 1114 | + | /// applies the current result. |
|
| 1107 | 1115 | fn request_context(&mut self, change: ContextChange) { |
|
| 1108 | - | if self.context_fetch == ContextFetch::Pending { |
|
| 1116 | + | let generation = self.generation; |
|
| 1117 | + | if !self.context_fetch.request(generation, change) { |
|
| 1109 | 1118 | return; |
|
| 1110 | 1119 | } |
|
| 1111 | - | let Some(loaded) = &mut self.commits[self.current].loaded else { |
|
| 1112 | - | return; |
|
| 1113 | - | }; |
|
| 1114 | - | let Some(request) = loaded.viewer.context_request(change) else { |
|
| 1115 | - | return; |
|
| 1116 | - | }; |
|
| 1117 | - | let queued = self |
|
| 1118 | - | .highlighter |
|
| 1120 | + | let request = self.commits[self.current] |
|
| 1121 | + | .loaded |
|
| 1119 | 1122 | .as_ref() |
|
| 1120 | - | .is_some_and(|highlighter| highlighter.context(request)); |
|
| 1121 | - | if queued { |
|
| 1122 | - | self.context_fetch = ContextFetch::Pending; |
|
| 1123 | + | .and_then(|loaded| loaded.viewer.context_request(change)); |
|
| 1124 | + | let queued = request.is_some_and(|request| { |
|
| 1125 | + | self.highlighter |
|
| 1126 | + | .as_ref() |
|
| 1127 | + | .is_some_and(|highlighter| highlighter.context(request)) |
|
| 1128 | + | }); |
|
| 1129 | + | if !queued { |
|
| 1130 | + | self.context_fetch.cancel(generation); |
|
| 1123 | 1131 | } |
|
| 1124 | 1132 | } |
|
| 1125 | 1133 | ||
| 1126 | 1134 | /// Draws one terminal frame without scheduling highlight work. |
|
| 1127 | 1135 | fn draw_frame(&mut self) -> io::Result<()> { |
crates/brain/src/session/event.rs
+76 -2
| 13 | 13 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
|
| 14 | 14 | pub(super) enum ContextFetch { |
|
| 15 | 15 | /// No context request is currently running. |
|
| 16 | 16 | #[default] |
|
| 17 | 17 | Idle, |
|
| 18 | - | /// A context request is currently running. |
|
| 19 | - | Pending, |
|
| 18 | + | /// A context request and at most one desired follow-up are pending. |
|
| 19 | + | Pending { |
|
| 20 | + | /// Highlighter generation that owns the request. |
|
| 21 | + | generation: u64, |
|
| 22 | + | /// Most recent context change requested while the worker was busy. |
|
| 23 | + | desired: Option<ContextChange>, |
|
| 24 | + | }, |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | impl ContextFetch { |
|
| 28 | + | /// Records a context change, returning whether a worker request should start. |
|
| 29 | + | pub(super) fn request(&mut self, generation: u64, change: ContextChange) -> bool { |
|
| 30 | + | match self { |
|
| 31 | + | Self::Pending { |
|
| 32 | + | generation: pending, |
|
| 33 | + | desired, |
|
| 34 | + | } if *pending == generation => { |
|
| 35 | + | *desired = Some(change); |
|
| 36 | + | false |
|
| 37 | + | } |
|
| 38 | + | _ => { |
|
| 39 | + | *self = Self::Pending { |
|
| 40 | + | generation, |
|
| 41 | + | desired: None, |
|
| 42 | + | }; |
|
| 43 | + | true |
|
| 44 | + | } |
|
| 45 | + | } |
|
| 46 | + | } |
|
| 47 | + | ||
| 48 | + | /// Completes this generation's request and returns its desired follow-up. |
|
| 49 | + | pub(super) fn complete(&mut self, generation: u64) -> Option<ContextChange> { |
|
| 50 | + | match *self { |
|
| 51 | + | Self::Pending { |
|
| 52 | + | generation: pending, |
|
| 53 | + | desired, |
|
| 54 | + | } if pending == generation => { |
|
| 55 | + | *self = Self::Idle; |
|
| 56 | + | desired |
|
| 57 | + | } |
|
| 58 | + | _ => None, |
|
| 59 | + | } |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Cancels this generation's state when its worker rejects the request. |
|
| 63 | + | pub(super) fn cancel(&mut self, generation: u64) { |
|
| 64 | + | if matches!( |
|
| 65 | + | self, |
|
| 66 | + | Self::Pending { |
|
| 67 | + | generation: pending, |
|
| 68 | + | .. |
|
| 69 | + | } if *pending == generation |
|
| 70 | + | ) { |
|
| 71 | + | *self = Self::Idle; |
|
| 72 | + | } |
|
| 73 | + | } |
|
| 20 | 74 | } |
|
| 21 | 75 | ||
| 22 | 76 | /// Result of applying one terminal or worker event. |
|
| 23 | 77 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 24 | 78 | pub(super) enum Update { |
| 123 | 177 | ||
| 124 | 178 | let error = result.unwrap_err(); |
|
| 125 | 179 | assert_eq!(error.kind(), io::ErrorKind::Other); |
|
| 126 | 180 | assert_eq!(error.to_string(), "terminal input failed"); |
|
| 127 | 181 | } |
|
| 182 | + | ||
| 183 | + | #[test] |
|
| 184 | + | fn context_fetch_preserves_two_increases() { |
|
| 185 | + | let generation = 7; |
|
| 186 | + | let mut fetch = ContextFetch::default(); |
|
| 187 | + | let mut context_lines = 3; |
|
| 188 | + | ||
| 189 | + | assert!(fetch.request(generation, ContextChange::Increase)); |
|
| 190 | + | assert!(!fetch.request(generation, ContextChange::Increase)); |
|
| 191 | + | assert_eq!(fetch.complete(generation - 1), None); |
|
| 192 | + | ||
| 193 | + | context_lines += 1; |
|
| 194 | + | let desired = fetch.complete(generation).unwrap(); |
|
| 195 | + | assert_eq!(desired, ContextChange::Increase); |
|
| 196 | + | assert!(fetch.request(generation, desired)); |
|
| 197 | + | context_lines += 1; |
|
| 198 | + | assert_eq!(fetch.complete(generation), None); |
|
| 199 | + | ||
| 200 | + | assert_eq!(context_lines, 5); |
|
| 201 | + | } |
|
| 128 | 202 | } |