Record an agent turn as an Atomic change, or classify a clean turn. This is the function that bridges the agent world into the VCS world. It builds a `ChangeHeader`, `Provenance`, and `SessionEnvelope`, then calls the repository's `record()` method to create a proper content-addressed, hashable, pushable Atomic change. When the working copy is clean, `EmptyTurn` is NOT returned (RFC §10.2 remove
(
repo_root: &Path,
options: &TurnRecordOptions<'_>,
)
| 1644 | /// |
| 1645 | /// Returns `AgentError::RecordFailed` if the repository record operation fails. |
| 1646 | pub fn record_turn( |
| 1647 | repo_root: &Path, |
| 1648 | options: &TurnRecordOptions<'_>, |
| 1649 | ) -> AgentResult<TurnRecordResult> { |
| 1650 | let mut manifest = scope::manifest(options)?; |
| 1651 | // Step 1: Open the repository read-only for the initial status check. |
| 1652 | // This can coexist with other readers. Wait for a transient incompatible |
| 1653 | // writer before deciding whether work or untracked files exist. |
| 1654 | let mut repo = atomic_repository::Repository::open_readonly_wait( |
| 1655 | repo_root, |
| 1656 | std::time::Duration::from_secs(10), |
| 1657 | ) |
| 1658 | .map_err(|e| AgentError::RecordFailed { |
| 1659 | session_id: options.session.session_id.clone(), |
| 1660 | turn_number: options.turn_number, |
| 1661 | reason: format!("Failed to open repository (readonly): {}", e), |
| 1662 | })?; |
| 1663 | |
| 1664 | // Preserve the read-only fast path when the persisted desired view already |
| 1665 | // matches the session. Non-sandbox recording intentionally targets the |
| 1666 | // session view, so repair or align it before status. A provisioned sandbox's |
| 1667 | // persistent working-copy record is authoritative and is never repointed here. |
| 1668 | let session_view_needs_alignment = !repo.is_sandbox() |
| 1669 | && (repo.current_view() != options.session.view_name |
| 1670 | || matches!( |
| 1671 | repo.get_view_info(&options.session.view_name), |
| 1672 | Err(atomic_repository::RepositoryError::ViewNotFound { .. }) |
| 1673 | )); |
| 1674 | if session_view_needs_alignment { |
| 1675 | drop(repo); |
| 1676 | let mut repair_repo = atomic_repository::Repository::open_existing_wait( |
| 1677 | repo_root, |
| 1678 | std::time::Duration::from_secs(10), |
| 1679 | ) |
| 1680 | .map_err(|error| AgentError::RecordFailed { |
| 1681 | session_id: options.session.session_id.clone(), |
| 1682 | turn_number: options.turn_number, |
| 1683 | reason: format!("Failed to open repository for view alignment: {}", error), |
| 1684 | })?; |
| 1685 | align_or_repair_session_view(&mut repair_repo, options)?; |
| 1686 | drop(repair_repo); |
| 1687 | repo = atomic_repository::Repository::open_readonly_wait( |
| 1688 | repo_root, |
| 1689 | std::time::Duration::from_secs(10), |
| 1690 | ) |
| 1691 | .map_err(|error| AgentError::RecordFailed { |
| 1692 | session_id: options.session.session_id.clone(), |
| 1693 | turn_number: options.turn_number, |
| 1694 | reason: format!( |
| 1695 | "Failed to reopen repository after view alignment: {}", |
| 1696 | error |
| 1697 | ), |
| 1698 | })?; |
| 1699 | } |
| 1700 | |
| 1701 | let working_copy = |
| 1702 | repo.require_working_copy_id() |
| 1703 | .map_err(|error| AgentError::RecordFailed { |