Take the pending shim-watch notice for a session, if any (CB-13D). Consuming claims the notice atomically first (review R6): the pending file is renamed to a per-consumer claim name before it is read, so a producer that publishes a *newer* notice after the claim still finds its file at the pending path and is delivered on the next take — the old read-then-unlink order destroyed exactly that newer
(&self, session_id: &str)
| 1229 | /// different session (or of an unknown version) is refused rather than |
| 1230 | /// delivered. |
| 1231 | pub fn take_watch_notice(&self, session_id: &str) -> AgentResult<Option<WatchNotice>> { |
| 1232 | validate_session_id(session_id)?; |
| 1233 | // CB-13D ::24 R6 exactly-once replay: a claim file left behind by a |
| 1234 | // CRASHED consumer (its writer pid no longer exists) is replayed |
| 1235 | // before claiming new work — the claimed notice was consumed from |
| 1236 | // the pending slot but never delivered. A claim whose writer is |
| 1237 | // still alive belongs to a live consumer mid-delivery and is never |
| 1238 | // stolen (a live consumer holds it between the claim rename and |
| 1239 | // the consume remove). |
| 1240 | if let Some(notice) = self.replay_crashed_claim(session_id)? { |
| 1241 | return Ok(Some(notice)); |
| 1242 | } |
| 1243 | let path = self.watch_notice_path(session_id); |
| 1244 | let claim = path.with_extension(format!( |
| 1245 | "json.claim.{}.{}", |
| 1246 | std::process::id(), |
| 1247 | WATCH_NOTICE_SEQUENCE.fetch_add(1, Ordering::Relaxed) |
| 1248 | )); |
| 1249 | // Atomically claim whatever is pending right now. `NotFound` means |
| 1250 | // no notice is pending; any claim error after this point leaves the |
| 1251 | // pending file alone (the notice is not consumed). |
| 1252 | if let Err(error) = std::fs::rename(&path, &claim) { |
| 1253 | if error.kind() == std::io::ErrorKind::NotFound { |
| 1254 | return Ok(None); |
| 1255 | } |
| 1256 | return Err(AgentError::SessionLoadFailed { |
| 1257 | session_id: session_id.to_string(), |
| 1258 | reason: format!("cannot claim watch notice: {}", error), |
| 1259 | }); |
| 1260 | } |
| 1261 | let bytes = match std::fs::read(&claim) { |
| 1262 | Ok(bytes) => bytes, |
| 1263 | Err(error) => { |
| 1264 | let _ = std::fs::remove_file(&claim); |
| 1265 | return Err(AgentError::SessionLoadFailed { |
| 1266 | session_id: session_id.to_string(), |
| 1267 | reason: format!("cannot read watch notice: {}", error), |
| 1268 | }); |
| 1269 | } |
| 1270 | }; |
| 1271 | if let Err(error) = std::fs::remove_file(&claim) { |
| 1272 | return Err(AgentError::SessionLoadFailed { |
| 1273 | session_id: session_id.to_string(), |
| 1274 | reason: format!("cannot consume watch notice: {}", error), |
| 1275 | }); |
| 1276 | } |
| 1277 | let notice: WatchNotice = serde_json::from_slice(&bytes)?; |
| 1278 | if notice.record_type != WATCH_NOTICE_RECORD_TYPE |
| 1279 | || notice.version != WATCH_NOTICE_VERSION |
| 1280 | || notice.session_id != session_id |
| 1281 | { |
| 1282 | return Err(AgentError::SessionLoadFailed { |
| 1283 | session_id: session_id.to_string(), |
| 1284 | reason: "watch notice does not match its session identity".to_string(), |
| 1285 | }); |
| 1286 | } |
| 1287 | Ok(Some(notice)) |
| 1288 | } |