Write the capture for one turn of one session, signing it first. Review R9: attempts are create-only. A retried commit within the same turn writes `turn-N.attempt-2.json` and so on instead of overwriting the previous attempt's evidence; earlier attempts stay durable for review.
(
sessions_dir: &Path,
session: &mut AgentSession,
turn: u32,
working_copy: String,
token: &atomic_repository::GitObservationToken,
)
| 265 | /// turn writes `turn-N.attempt-2.json` and so on instead of overwriting the |
| 266 | /// previous attempt's evidence; earlier attempts stay durable for review. |
| 267 | pub fn write_capture( |
| 268 | sessions_dir: &Path, |
| 269 | session: &mut AgentSession, |
| 270 | turn: u32, |
| 271 | working_copy: String, |
| 272 | token: &atomic_repository::GitObservationToken, |
| 273 | ) -> AgentResult<PathBuf> { |
| 274 | let key = session.ensure_mac_key(); |
| 275 | let mut capture = build_capture(session, turn, working_copy, token); |
| 276 | capture.sign(&key); |
| 277 | let dir = capture_dir(sessions_dir, &session.session_id)?; |
| 278 | std::fs::create_dir_all(&dir)?; |
| 279 | // Find the first attempt slot that does not exist yet, exclusively. |
| 280 | for attempt in 1..=64u32 { |
| 281 | let path = attempt_capture_path(sessions_dir, &session.session_id, turn, attempt)?; |
| 282 | match std::fs::OpenOptions::new() |
| 283 | .write(true) |
| 284 | .create_new(true) |
| 285 | .open(&path) |
| 286 | { |
| 287 | Ok(mut file) => { |
| 288 | file.write_all(&capture.to_json()?)?; |
| 289 | file.sync_all()?; |
| 290 | return Ok(path); |
| 291 | } |
| 292 | Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, |
| 293 | Err(error) => return Err(error.into()), |
| 294 | } |
| 295 | } |
| 296 | Err(crate::error::AgentError::RecordFailed { |
| 297 | session_id: session.session_id.clone(), |
| 298 | turn_number: turn, |
| 299 | reason: "too many commit attempts without a turn boundary (64)".to_string(), |
| 300 | }) |
| 301 | } |
| 302 | |
| 303 | /// Load and fully verify the capture for one turn of one session. |
| 304 | /// |