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

Function persist_first_refusal

atomic-cli/src/commands/agent/lifecycle.rs:918–968  ·  view source on GitHub ↗
(
    dir: &Path,
    run_id: &str,
    candidate: ManagedBoundaryRefusal,
)

Source from the content-addressed store, hash-verified

916}
917
918fn persist_first_refusal(
919 dir: &Path,
920 run_id: &str,
921 candidate: ManagedBoundaryRefusal,
922) -> CliResult<ManagedBoundaryRefusal> {
923 validate_run_id(run_id)?;
924 fs::create_dir_all(dir)?;
925 let path = lifecycle_refusal_path(dir, run_id);
926 if let Some(existing) = load_first_refusal(dir, run_id)? {
927 return Ok(existing);
928 }
929
930 let temp = dir.join(format!(".{}.{}.refusal.tmp", run_id, uuid::Uuid::new_v4()));
931 let data = serde_json::to_vec_pretty(&candidate).map_err(|error| {
932 CliError::Internal(anyhow!(
933 "failed to serialize managed lifecycle refusal: {}",
934 error,
935 ))
936 })?;
937 {
938 // Write and fsync through one writable handle: on Windows,
939 // `sync_all` on a read-only handle is refused (access denied).
940 use std::io::Write as _;
941 let mut file = fs::File::create(&temp)?;
942 file.write_all(&data)?;
943 file.sync_all()?;
944 }
945
946 let won = match fs::hard_link(&temp, &path) {
947 Ok(()) => true,
948 Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => false,
949 Err(error) => {
950 let _ = fs::remove_file(&temp);
951 return Err(CliError::Io(error));
952 }
953 };
954 let _ = fs::remove_file(&temp);
955
956 if won {
957 #[cfg(unix)]
958 fs::File::open(dir)?.sync_all()?;
959 Ok(candidate)
960 } else {
961 load_first_refusal(dir, run_id)?.ok_or_else(|| {
962 CliError::Internal(anyhow!(
963 "managed lifecycle refusal '{}' disappeared after concurrent creation",
964 run_id,
965 ))
966 })
967 }
968}
969
970fn sessions_for_run(dot_dir: &Path, run_id: &str) -> Vec<AgentSession> {
971 let Ok(store) = SessionStore::new(dot_dir.join("sessions")) else {

Calls 6

validate_run_idFunction · 0.85
lifecycle_refusal_pathFunction · 0.85
load_first_refusalFunction · 0.85
openFunction · 0.85
createFunction · 0.50
kindMethod · 0.45