Whether the observed HEAD move is explained by an anchored journal record. Review R4: the advisory journal is untrusted input. A record explains a transition only when it was written for THIS worktree (the real journal writes the canonical worktree root) and inside the turn window (a stale or future-dated row is replay evidence, not an explanation). Anything else is ignored and the transition sta
(
repo_root: &Path,
start_oid: Option<&str>,
end_oid: Option<&str>,
not_before_unix: i64,
)
| 302 | /// or future-dated row is replay evidence, not an explanation). Anything else |
| 303 | /// is ignored and the transition stays unexplained. |
| 304 | fn checkout_journaled( |
| 305 | repo_root: &Path, |
| 306 | start_oid: Option<&str>, |
| 307 | end_oid: Option<&str>, |
| 308 | not_before_unix: i64, |
| 309 | ) -> bool { |
| 310 | let (Some(start_oid), Some(end_oid)) = (start_oid, end_oid) else { |
| 311 | return false; |
| 312 | }; |
| 313 | let journal = repo_root |
| 314 | .join(".atomic") |
| 315 | .join("bridge") |
| 316 | .join("git-events.jsonl"); |
| 317 | let Ok(bytes) = std::fs::read(&journal) else { |
| 318 | return false; |
| 319 | }; |
| 320 | // Bound the scan: the journal is append-only; a recent tail is enough. |
| 321 | let tail_start = bytes.len().saturating_sub(256 * 1024); |
| 322 | let tail = &bytes[tail_start..]; |
| 323 | let worktree_root = |
| 324 | std::fs::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf()); |
| 325 | let now_unix = chrono::Utc::now().timestamp(); |
| 326 | let mut records = 0usize; |
| 327 | for line in tail.split(|byte| *byte == b'\n') { |
| 328 | if records >= 512 { |
| 329 | break; |
| 330 | } |
| 331 | let Ok(record) = serde_json::from_slice::<serde_json::Value>(line) else { |
| 332 | continue; |
| 333 | }; |
| 334 | if record.get("record_type").and_then(|value| value.as_str()) != Some("post-checkout") { |
| 335 | continue; |
| 336 | } |
| 337 | records += 1; |
| 338 | let old_head = record.get("old_head").and_then(|value| value.as_str()); |
| 339 | let new_head = record.get("new_head").and_then(|value| value.as_str()); |
| 340 | if old_head != Some(start_oid) || new_head != Some(end_oid) { |
| 341 | continue; |
| 342 | } |
| 343 | // Worktree binding: the row must name this worktree (review R4: |
| 344 | // "worktree /" in the executed probe is a fabricated row). |
| 345 | let recorded_root = record |
| 346 | .get("worktree_root") |
| 347 | .and_then(|value| value.as_str()) |
| 348 | .map(std::path::PathBuf::from) |
| 349 | .map(|path| std::fs::canonicalize(&path).unwrap_or(path)); |
| 350 | if recorded_root.as_deref() != Some(worktree_root.as_path()) { |
| 351 | continue; |
| 352 | } |
| 353 | // Turn-window anchoring: the row must have been written inside the |
| 354 | // window, with a generous same-machine clock skew on both sides. |
| 355 | let recorded_at = record |
| 356 | .get("recorded_at") |
| 357 | .and_then(|value| value.as_str()) |
| 358 | .and_then(|value| chrono::DateTime::parse_from_rfc3339(value).ok()) |
| 359 | .map(|time| time.timestamp()); |
| 360 | let Some(recorded_unix) = recorded_at else { |
| 361 | continue; |
no test coverage detected