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)
| 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(¬ices) { |
| 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 { |