MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / verify_capture

Function verify_capture

atomic-agent/src/turn/capture.rs:323–431  ·  view source on GitHub ↗
(
    sessions_dir: &Path,
    session: &AgentSession,
    turn: u32,
    expected_working_copy: &str,
    expected_parent_oid: Option<&str>,
    expected_committed_tree: Option<&str>,
    turn_starte

Source from the content-addressed store, hash-verified

321/// `--no-verify`).
322#[allow(clippy::too_many_arguments)]
323pub fn verify_capture(
324 sessions_dir: &Path,
325 session: &AgentSession,
326 turn: u32,
327 expected_working_copy: &str,
328 expected_parent_oid: Option<&str>,
329 expected_committed_tree: Option<&str>,
330 turn_started_at: i64,
331) -> AgentResult<Option<ManagedCommitCapture>> {
332 // Verify the LATEST attempt (the last commit attempt of the turn);
333 // earlier attempts stay on disk as durable evidence.
334 let Some(path) = latest_capture_file(sessions_dir, &session.session_id, turn)? else {
335 return Ok(None);
336 };
337 let bytes = std::fs::read(&path).map_err(|error| crate::error::AgentError::CaptureInvalid {
338 reason: format!("cannot read capture {}: {}", path.display(), error),
339 })?;
340 let capture = ManagedCommitCapture::from_json(&bytes)?;
341 if capture.version != CAPTURE_VERSION {
342 return Err(crate::error::AgentError::CaptureInvalid {
343 reason: format!(
344 "capture schema version {} is not supported (expected {CAPTURE_VERSION})",
345 capture.version
346 ),
347 });
348 }
349
350 // The durable session JSON is the MAC-key authority: the pre-commit hook
351 // persisted the key it signed with. Fall back to the in-memory session
352 // for freshly-created keys not yet saved.
353 let key = {
354 let store = crate::turn::session::SessionStore::new(sessions_dir)?;
355 store
356 .load(&session.session_id)?
357 .and_then(|stored| stored.mac_key)
358 .or_else(|| session.mac_key.clone())
359 };
360 let key = key.ok_or_else(|| crate::error::AgentError::CaptureInvalid {
361 reason: "session has no MAC key; capture cannot authenticate".to_string(),
362 })?;
363 if !capture.verify(&key) {
364 return Err(crate::error::AgentError::CaptureInvalid {
365 reason: "capture MAC verification failed (tampered or forged)".to_string(),
366 });
367 }
368 if capture.session_id != session.session_id {
369 return Err(crate::error::AgentError::CaptureInvalid {
370 reason: format!(
371 "capture binds session '{}' not '{}'",
372 capture.session_id, session.session_id
373 ),
374 });
375 }
376 if capture.turn != turn {
377 return Err(crate::error::AgentError::CaptureInvalid {
378 reason: format!(
379 "capture binds turn {} not {} (replayed or stale)",
380 capture.turn, turn

Calls 6

latest_capture_fileFunction · 0.85
readFunction · 0.50
loadMethod · 0.45
cloneMethod · 0.45
verifyMethod · 0.45
timestampMethod · 0.45