aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-test / src / block_first.rs
| 1 | //! Desk blocks go first (L309, 2026-10-20). |
| 2 | //! |
| 3 | //! The PR desk (Renda) carries many PRs in one "desk block" PR and records it |
| 4 | //! in `<state>/renda-block-<repo>.json`: |
| 5 | //! `{"open": {"sha": HEAD, "carried": [PR..], "heads": {"PR": SHA}, ..}, ..}`. |
| 6 | //! One judge run on the block head stands for every carried PR, so while a |
| 7 | //! block is open the worker judges its head before any other head of that |
| 8 | //! repository and does not judge a carried PR's own head by itself. |
| 9 | //! |
| 10 | //! A state file older than `MAX_AGE` is ignored, so a desk that stopped |
| 11 | //! cannot keep PRs from being judged forever. An explicit trigger file is an |
| 12 | //! operator's order and is never skipped. |
| 13 | |
| 14 | use std::collections::{BTreeSet, HashSet, VecDeque}; |
| 15 | use std::fs; |
| 16 | use std::path::{Path, PathBuf}; |
| 17 | use std::time::{Duration, SystemTime}; |
| 18 | |
| 19 | /// A queued head: repository key, commit, and the trigger file to remove |
| 20 | /// once it has been served. |
| 21 | pub type Head = (String, String, Option<PathBuf>); |
| 22 | |
| 23 | /// Longest a block state file is believed without being rewritten. |
| 24 | pub const MAX_AGE: Duration = Duration::from_secs(24 * 3600); |
| 25 | |
| 26 | /// One open desk block. |
| 27 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 28 | pub struct Block { |
| 29 | pub repo: String, |
| 30 | pub head: String, |
| 31 | /// Heads of the PRs the block carries. |
| 32 | pub carried: BTreeSet<String>, |
| 33 | } |
| 34 | |
| 35 | /// Parse one `renda-block-<repo>.json`; None when no block is open or the |
| 36 | /// text is not what the desk writes. |
| 37 | pub fn parse(repo: &str, text: &str) -> Option<Block> { |
| 38 | let v: serde_json::Value = serde_json::from_str(text).ok()?; |
| 39 | let open = v.get("open")?; |
| 40 | let head = open.get("sha")?.as_str()?.to_ascii_lowercase(); |
| 41 | if !crate::worker::valid_sha(&head) { |
| 42 | return None; |
| 43 | } |
| 44 | let numbers: BTreeSet<u64> = open |
| 45 | .get("carried") |
| 46 | .and_then(|c| c.as_array()) |
| 47 | .map(|a| a.iter().filter_map(|n| n.as_u64()).collect()) |
| 48 | .unwrap_or_default(); |
| 49 | let heads = open.get("heads").and_then(|h| h.as_object()); |
| 50 | let carried = numbers |
| 51 | .iter() |
| 52 | .filter_map(|n| heads?.get(&n.to_string())?.as_str()) |
| 53 | .map(str::to_ascii_lowercase) |
| 54 | .filter(|s| crate::worker::valid_sha(s)) |
| 55 | .collect(); |
| 56 | Some(Block { |
| 57 | repo: repo.to_string(), |
| 58 | head, |
| 59 | carried, |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | /// The open blocks of `repos`, read from `state_root`. A file not rewritten |
| 64 | /// within `MAX_AGE` of `now` is ignored. |
| 65 | pub fn open_blocks(state_root: &Path, repos: &[String], now: SystemTime) -> Vec<Block> { |
| 66 | let mut out = Vec::new(); |
| 67 | for repo in repos { |
| 68 | if !crate::worker::valid_repo_key(repo) { |
| 69 | continue; |
| 70 | } |
| 71 | let path = state_root.join(format!("renda-block-{repo}.json")); |
| 72 | let fresh = fs::metadata(&path) |
| 73 | .and_then(|m| m.modified()) |
| 74 | .ok() |
| 75 | .and_then(|t| now.duration_since(t).ok().or(Some(Duration::ZERO))) |
| 76 | .is_some_and(|age| age <= MAX_AGE); |
| 77 | if !fresh { |
| 78 | continue; |
| 79 | } |
| 80 | if let Some(b) = fs::read_to_string(&path).ok().and_then(|t| parse(repo, &t)) { |
| 81 | out.push(b); |
| 82 | } |
| 83 | } |
| 84 | out |
| 85 | } |
| 86 | |
| 87 | /// The next head to judge. A block head not yet `tried` and without a bundle |
| 88 | /// comes first (taken from the queue when it is there, otherwise made, with |
| 89 | /// its trigger file when one exists). Otherwise the front of the queue, except |
| 90 | /// that a head with no trigger file which a block carries is dropped. |
| 91 | pub fn next_head( |
| 92 | queue: &mut VecDeque<Head>, |
| 93 | blocks: &[Block], |
| 94 | trigger_dir: &Path, |
| 95 | bundled: &dyn Fn(&str, &str) -> bool, |
| 96 | tried: &mut HashSet<(String, String)>, |
| 97 | ) -> Option<Head> { |
| 98 | for b in blocks { |
| 99 | let key = (b.repo.clone(), b.head.clone()); |
| 100 | if tried.contains(&key) || bundled(&b.repo, &b.head) { |
| 101 | continue; |
| 102 | } |
| 103 | tried.insert(key); |
| 104 | if let Some(i) = queue |
| 105 | .iter() |
| 106 | .position(|(r, s, _)| *r == b.repo && *s == b.head) |
| 107 | { |
| 108 | return queue.remove(i); |
| 109 | } |
| 110 | let trigger = trigger_dir.join(format!("{}.{}", b.repo, b.head)); |
| 111 | return Some(( |
| 112 | b.repo.clone(), |
| 113 | b.head.clone(), |
| 114 | trigger.exists().then_some(trigger), |
| 115 | )); |
| 116 | } |
| 117 | while let Some((repo, sha, trigger)) = queue.pop_front() { |
| 118 | if !tried.insert((repo.clone(), sha.clone())) { |
| 119 | // Served already this round; a leftover trigger file goes with it. |
| 120 | if let Some(f) = trigger { |
| 121 | let _ = fs::remove_file(f); |
| 122 | } |
| 123 | continue; |
| 124 | } |
| 125 | let carried = trigger.is_none() |
| 126 | && blocks |
| 127 | .iter() |
| 128 | .any(|b| b.repo == repo && b.carried.contains(&sha)); |
| 129 | if carried { |
| 130 | tried.remove(&(repo, sha)); |
| 131 | continue; |
| 132 | } |
| 133 | return Some((repo, sha, trigger)); |
| 134 | } |
| 135 | None |
| 136 | } |
| 137 | |
| 138 | #[cfg(test)] |
| 139 | mod tests { |
| 140 | use super::*; |
| 141 | |
| 142 | fn sha(c: char) -> String { |
| 143 | c.to_string().repeat(40) |
| 144 | } |
| 145 | |
| 146 | fn state(name: &str, text: &str) -> PathBuf { |
| 147 | let d = std::env::temp_dir().join(format!("block-first-{name}-{}", std::process::id())); |
| 148 | let _ = fs::remove_dir_all(&d); |
| 149 | fs::create_dir_all(d.join("trigger")).unwrap(); |
| 150 | fs::write(d.join("renda-block-r.json"), text).unwrap(); |
| 151 | d |
| 152 | } |
| 153 | |
| 154 | fn block_text(head: &str) -> String { |
| 155 | format!( |
| 156 | r#"{{"open": {{"pr": 9, "sha": "{head}", "carried": [1, 2], |
| 157 | "heads": {{"1": "{}", "2": "{}", "3": "{}"}}}}, "size": 12}}"#, |
| 158 | sha('1'), |
| 159 | sha('2'), |
| 160 | sha('3') |
| 161 | ) |
| 162 | } |
| 163 | |
| 164 | fn drain( |
| 165 | q: Vec<Head>, |
| 166 | blocks: &[Block], |
| 167 | dir: &Path, |
| 168 | bundled: &dyn Fn(&str, &str) -> bool, |
| 169 | ) -> Vec<String> { |
| 170 | let mut q: VecDeque<Head> = q.into(); |
| 171 | let mut tried = HashSet::new(); |
| 172 | let mut out = Vec::new(); |
| 173 | while let Some((_, s, _)) = next_head(&mut q, blocks, dir, bundled, &mut tried) { |
| 174 | out.push(s); |
| 175 | } |
| 176 | out |
| 177 | } |
| 178 | |
| 179 | fn head(c: char) -> Head { |
| 180 | ("r".to_string(), sha(c), None) |
| 181 | } |
| 182 | |
| 183 | #[test] |
| 184 | fn block_head_goes_first_and_carried_heads_are_skipped() { |
| 185 | let d = state("order", &block_text(&sha('b'))); |
| 186 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 187 | assert_eq!(blocks.len(), 1); |
| 188 | assert_eq!(blocks[0].carried.len(), 2); |
| 189 | // queue sorted by sha as GitHub polling leaves it: 1, 2, 3, b |
| 190 | let q = vec![head('1'), head('2'), head('3'), head('b')]; |
| 191 | let got = drain(q, &blocks, &d.join("trigger"), &|_, _| false); |
| 192 | assert_eq!(got, vec![sha('b'), sha('3')]); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn block_head_missing_from_the_queue_is_made_first() { |
| 197 | let d = state("missing", &block_text(&sha('b'))); |
| 198 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 199 | let got = drain(vec![head('3')], &blocks, &d.join("trigger"), &|_, _| false); |
| 200 | assert_eq!(got, vec![sha('b'), sha('3')]); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn judged_block_head_is_not_queued_again_and_carried_stay_skipped() { |
| 205 | let d = state("done", &block_text(&sha('b'))); |
| 206 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 207 | let b = sha('b'); |
| 208 | let got = drain( |
| 209 | vec![head('1'), head('3')], |
| 210 | &blocks, |
| 211 | &d.join("trigger"), |
| 212 | &|_, s| s == b, |
| 213 | ); |
| 214 | assert_eq!(got, vec![sha('3')]); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn a_trigger_file_for_a_carried_head_is_still_served() { |
| 219 | let d = state("trigger", &block_text(&sha('b'))); |
| 220 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 221 | let t = d.join("trigger").join(format!("r.{}", sha('1'))); |
| 222 | fs::write(&t, "").unwrap(); |
| 223 | let q = vec![("r".to_string(), sha('1'), Some(t))]; |
| 224 | let got = drain(q, &blocks, &d.join("trigger"), &|_, _| false); |
| 225 | assert_eq!(got, vec![sha('b'), sha('1')]); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn block_head_trigger_file_made_mid_round_travels_with_the_head() { |
| 230 | let d = state("mid", &block_text(&sha('b'))); |
| 231 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 232 | let t = d.join("trigger").join(format!("r.{}", sha('b'))); |
| 233 | fs::write(&t, "").unwrap(); |
| 234 | let mut q: VecDeque<Head> = VecDeque::new(); |
| 235 | let mut tried = HashSet::new(); |
| 236 | let got = next_head( |
| 237 | &mut q, |
| 238 | &blocks, |
| 239 | &d.join("trigger"), |
| 240 | &|_, _| false, |
| 241 | &mut tried, |
| 242 | ); |
| 243 | assert_eq!(got, Some(("r".to_string(), sha('b'), Some(t)))); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn no_open_block_leaves_the_order_alone() { |
| 248 | let d = state("none", r#"{"open": null, "size": 12}"#); |
| 249 | let blocks = open_blocks(&d, &["r".to_string()], SystemTime::now()); |
| 250 | assert!(blocks.is_empty()); |
| 251 | let got = drain( |
| 252 | vec![head('1'), head('b')], |
| 253 | &blocks, |
| 254 | &d.join("trigger"), |
| 255 | &|_, _| false, |
| 256 | ); |
| 257 | assert_eq!(got, vec![sha('1'), sha('b')]); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn a_stale_state_file_and_other_repos_are_ignored() { |
| 262 | let d = state("stale", &block_text(&sha('b'))); |
| 263 | let later = SystemTime::now() + MAX_AGE + Duration::from_secs(60); |
| 264 | assert!(open_blocks(&d, &["r".to_string()], later).is_empty()); |
| 265 | assert!(open_blocks(&d, &["other".to_string()], SystemTime::now()).is_empty()); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn unreadable_state_is_no_block() { |
| 270 | assert_eq!(parse("r", "not json"), None); |
| 271 | assert_eq!(parse("r", r#"{"open": {"sha": "short"}}"#), None); |
| 272 | } |
| 273 | } |