| 908 | } |
| 909 | |
| 910 | pub(crate) fn record_pre_commit(root: &Path) -> CliResult<()> { |
| 911 | let root = canonical_root(root)?; |
| 912 | let sessions_dir = match atomic_repository::Repository::canonical_dot_dir(&root) { |
| 913 | Ok(dot_dir) => dot_dir.join("sessions"), |
| 914 | Err(_) => return Ok(()), // not an Atomic repository — nothing to capture |
| 915 | }; |
| 916 | |
| 917 | // One git observation covers every active session's capture. |
| 918 | let mut token = match atomic_repository::observe_git_metadata(&root) { |
| 919 | Ok(observation) => observation.token(), |
| 920 | Err(error) => { |
| 921 | print_warning(&format!( |
| 922 | "pre-commit capture skipped: git observation failed: {error}" |
| 923 | )); |
| 924 | return Ok(()); |
| 925 | } |
| 926 | }; |
| 927 | |
| 928 | let working_copy = match atomic_repository::Repository::open_readonly(&root) |
| 929 | .and_then(|repo| repo.require_working_copy_id().map(|id| id.to_string())) |
| 930 | { |
| 931 | Ok(working_copy) => working_copy, |
| 932 | Err(_) => return Ok(()), // no working-copy identity — nothing managed to capture |
| 933 | }; |
| 934 | |
| 935 | let store = match atomic_agent::turn::session::SessionStore::new(&sessions_dir) { |
| 936 | Ok(store) => store, |
| 937 | Err(_) => return Ok(()), |
| 938 | }; |
| 939 | |
| 940 | let active = match store.active_sessions() { |
| 941 | Ok(active) => active, |
| 942 | Err(_) => return Ok(()), |
| 943 | }; |
| 944 | if active.is_empty() { |
| 945 | return Ok(()); |
| 946 | } |
| 947 | // `git commit -a`, `git commit <paths>` and `--only` build the commit |
| 948 | // from a temporary index that Git names in GIT_INDEX_FILE; the primary |
| 949 | // index still holds the pre-commit stage. The capture must bind the tree |
| 950 | // Git is about to commit, or the turn-end consumer can never prove it. |
| 951 | // Only computed when a session is there to capture for. |
| 952 | if let Some(tree) = commit_index_tree(&root) { |
| 953 | token.index_tree = Some(tree); |
| 954 | } |
| 955 | |
| 956 | for mut session in active { |
| 957 | let turn = session.turn_count + 1; |
| 958 | match atomic_agent::turn::capture::write_capture( |
| 959 | &sessions_dir, |
| 960 | &mut session, |
| 961 | turn, |
| 962 | working_copy.clone(), |
| 963 | &token, |
| 964 | ) { |
| 965 | Ok(path) => { |
| 966 | // Persist the MAC key generated for the capture, or the |
| 967 | // turn-end consumer cannot authenticate the evidence. |