| 888 | } |
| 889 | |
| 890 | pub(crate) fn record_pre_commit(root: &Path) -> CliResult<()> { |
| 891 | let root = canonical_root(root)?; |
| 892 | let sessions_dir = match atomic_repository::Repository::canonical_dot_dir(&root) { |
| 893 | Ok(dot_dir) => dot_dir.join("sessions"), |
| 894 | Err(_) => return Ok(()), // not an Atomic repository — nothing to capture |
| 895 | }; |
| 896 | |
| 897 | // One git observation covers every active session's capture. |
| 898 | let token = match atomic_repository::observe_git_metadata(&root) { |
| 899 | Ok(observation) => observation.token(), |
| 900 | Err(error) => { |
| 901 | print_warning(&format!( |
| 902 | "pre-commit capture skipped: git observation failed: {error}" |
| 903 | )); |
| 904 | return Ok(()); |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | let working_copy = match atomic_repository::Repository::open_readonly(&root) |
| 909 | .and_then(|repo| repo.require_working_copy_id().map(|id| id.to_string())) |
| 910 | { |
| 911 | Ok(working_copy) => working_copy, |
| 912 | Err(_) => return Ok(()), // no working-copy identity — nothing managed to capture |
| 913 | }; |
| 914 | |
| 915 | let store = match atomic_agent::turn::session::SessionStore::new(&sessions_dir) { |
| 916 | Ok(store) => store, |
| 917 | Err(_) => return Ok(()), |
| 918 | }; |
| 919 | |
| 920 | let active = match store.active_sessions() { |
| 921 | Ok(active) => active, |
| 922 | Err(_) => return Ok(()), |
| 923 | }; |
| 924 | |
| 925 | for mut session in active { |
| 926 | let turn = session.turn_count + 1; |
| 927 | match atomic_agent::turn::capture::write_capture( |
| 928 | &sessions_dir, |
| 929 | &mut session, |
| 930 | turn, |
| 931 | working_copy.clone(), |
| 932 | &token, |
| 933 | ) { |
| 934 | Ok(path) => { |
| 935 | // Persist the MAC key generated for the capture, or the |
| 936 | // turn-end consumer cannot authenticate the evidence. |
| 937 | let _ = store.save(&session); |
| 938 | log::debug!("wrote commit-time capture {}", path.display()); |
| 939 | } |
| 940 | Err(error) => { |
| 941 | print_warning(&format!( |
| 942 | "pre-commit capture for session {} turn {} failed (advisory): {error}", |
| 943 | session.session_id, turn |
| 944 | )); |
| 945 | } |
| 946 | } |
| 947 | } |