D4: sync-on-read entry point for read (non-edit) tools. NEVER blocks. If read-refresh is enabled and the read cooldown has elapsed since the last background spawn, this `compare_exchange`s [`background_refresh_running`](Self::background_refresh_running) to `true` and spawns a detached refresh, then returns immediately so the caller serves the current answer with zero added latency. The *next read
(&self, cg: &Arc<TraceDecay>)
| 1340 | /// `background_refresh_running` flag, and the underlying cross-process |
| 1341 | /// sync lock. At most one refresh runs at a time. |
| 1342 | fn maybe_spawn_read_refresh(&self, cg: &Arc<TraceDecay>) { |
| 1343 | if !self.sync_config.read_refresh { |
| 1344 | return; |
| 1345 | } |
| 1346 | // A checkout racing this call would diff the new branch against the |
| 1347 | // old branch's DB; `tools/call` reopens onto the live branch before |
| 1348 | // dispatch, so this only fires on an in-flight race. Skip it — the |
| 1349 | // next call runs on the reopened snapshot. |
| 1350 | if cg.branch_drifted() { |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | let now = crate::tracedecay::current_timestamp(); |
| 1355 | let cooldown = self.sync_config.read_cooldown_secs as i64; |
| 1356 | let previous = self.last_background_refresh_at.load(Ordering::Acquire); |
| 1357 | if previous != 0 && now.saturating_sub(previous) < cooldown { |
| 1358 | return; |
| 1359 | } |
| 1360 | // Reserve the cooldown slot. If another read call won the race, bail. |
| 1361 | if self |
| 1362 | .last_background_refresh_at |
| 1363 | .compare_exchange(previous, now, Ordering::AcqRel, Ordering::Acquire) |
| 1364 | .is_err() |
| 1365 | { |
| 1366 | return; |
| 1367 | } |
| 1368 | // Reserve the single-flight slot. If a refresh is already running |
| 1369 | // (e.g. a slow prior spawn that outlived its cooldown), don't stack. |
| 1370 | if self |
| 1371 | .background_refresh_running |
| 1372 | .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) |
| 1373 | .is_err() |
| 1374 | { |
| 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | self.spawn_read_refresh_task(cg, self.sync_config.full_sync_escalation_files); |
| 1379 | } |
| 1380 | |
| 1381 | /// Spawns the detached D4 refresh task. The task owns cheap `Arc` clones |
| 1382 | /// of the background-refresh flag, the completion stamp, and the shared |