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

Method replay_crashed_claim

atomic-agent/src/turn/session.rs:1293–1360  ·  view source on GitHub ↗

Replay notices a crashed consumer claimed but never delivered (CB-13D ::24 R6): scan the notices directory for claim files whose writer pid is gone, deliver the oldest one, and remove it.

(&self, session_id: &str)

Source from the content-addressed store, hash-verified

1291 /// (CB-13D ::24 R6): scan the notices directory for claim files whose
1292 /// writer pid is gone, deliver the oldest one, and remove it.
1293 fn replay_crashed_claim(&self, session_id: &str) -> AgentResult<Option<WatchNotice>> {
1294 let notices = self.sessions_dir.join(WATCH_NOTICES_DIRECTORY);
1295 let entries = match std::fs::read_dir(&notices) {
1296 Ok(entries) => entries,
1297 Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
1298 Err(error) => {
1299 return Err(AgentError::SessionLoadFailed {
1300 session_id: session_id.to_string(),
1301 reason: format!("cannot scan watch notice claims: {}", error),
1302 })
1303 }
1304 };
1305 let prefix = format!("{session_id}.json.claim.");
1306 let mut candidates: Vec<(u64, std::path::PathBuf)> = Vec::new();
1307 for entry in entries {
1308 let entry = match entry {
1309 Ok(entry) => entry,
1310 // CB-13D ::24 R2: an unreadable entry fails the scan
1311 // fail-closed instead of silently narrowing the namespace.
1312 Err(error) => {
1313 return Err(AgentError::SessionLoadFailed {
1314 session_id: session_id.to_string(),
1315 reason: format!("cannot read watch notice claim entry: {}", error),
1316 })
1317 }
1318 };
1319 let name = entry.file_name().to_string_lossy().to_string();
1320 let Some(rest) = name.strip_prefix(&prefix) else {
1321 continue;
1322 };
1323 let Some(pid_text) = rest.split('.').next() else {
1324 continue;
1325 };
1326 let Ok(pid) = pid_text.parse::<u64>() else {
1327 continue;
1328 };
1329 if pid == std::process::id() as u64 || pid_alive(pid) {
1330 continue;
1331 }
1332 candidates.push((pid, entry.path()));
1333 }
1334 candidates.sort();
1335 let Some((_, claim)) = candidates.first() else {
1336 return Ok(None);
1337 };
1338 let bytes = std::fs::read(claim).map_err(|error| AgentError::SessionLoadFailed {
1339 session_id: session_id.to_string(),
1340 reason: format!(
1341 "cannot read a crashed consumer's watch notice claim: {}",
1342 error
1343 ),
1344 })?;
1345 let notice: WatchNotice = serde_json::from_slice(&bytes)?;
1346 if notice.record_type != WATCH_NOTICE_RECORD_TYPE
1347 || notice.version != WATCH_NOTICE_VERSION
1348 || notice.session_id != session_id
1349 {
1350 return Err(AgentError::SessionLoadFailed {

Callers 1

take_watch_noticeMethod · 0.80

Calls 8

pid_aliveFunction · 0.85
sortMethod · 0.80
readFunction · 0.50
kindMethod · 0.45
file_nameMethod · 0.45
nextMethod · 0.45
pushMethod · 0.45
pathMethod · 0.45

Tested by

no test coverage detected