| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | /// A conservative resource claim calculated before a tool may execute. |
| 6 | /// |
| 7 | /// The prepared-call seam records these claims before authority checks and the |
| 8 | /// product scheduler uses them to keep parallel batches non-conflicting. |
| 9 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 10 | #[serde(rename_all = "snake_case")] |
| 11 | pub enum ResourceClaim { |
| 12 | ReadOnly, |
| 13 | WorkspaceRead, |
| 14 | ReadPath(PathBuf), |
| 15 | WritePath(PathBuf), |
| 16 | ReadTree(PathBuf), |
| 17 | WriteTree(PathBuf), |
| 18 | Terminal(String), |
| 19 | ToolCatalogWrite, |
| 20 | GlobalExclusive, |
| 21 | } |
| 22 | |
| 23 | impl ResourceClaim { |
| 24 | #[must_use] |
| 25 | pub fn conflicts_with(&self, other: &Self) -> bool { |
| 26 | use ResourceClaim::{ |
| 27 | GlobalExclusive, ReadOnly, ReadPath, ReadTree, Terminal, ToolCatalogWrite, |
| 28 | WorkspaceRead, WritePath, WriteTree, |
| 29 | }; |
| 30 | |
| 31 | match (self, other) { |
| 32 | (GlobalExclusive, _) | (_, GlobalExclusive) => true, |
| 33 | (ReadOnly, _) | (_, ReadOnly) => false, |
| 34 | (WorkspaceRead, WritePath(_) | WriteTree(_)) |
| 35 | | (WritePath(_) | WriteTree(_), WorkspaceRead) => true, |
| 36 | (ToolCatalogWrite, ToolCatalogWrite) => false, |
| 37 | (Terminal(left), Terminal(right)) => left == right, |
| 38 | (ReadPath(_), ReadPath(_)) => false, |
| 39 | (ReadPath(left), WritePath(right)) | (WritePath(right), ReadPath(left)) => { |
| 40 | left == right |
| 41 | } |
| 42 | (WritePath(left), WritePath(right)) => left == right, |
| 43 | (ReadTree(_), ReadTree(_)) => false, |
| 44 | (ReadTree(_), ReadPath(_)) | (ReadPath(_), ReadTree(_)) => false, |
| 45 | (ReadTree(tree), WritePath(path)) | (WritePath(path), ReadTree(tree)) => { |
| 46 | path.starts_with(tree) |
| 47 | } |
| 48 | (WriteTree(tree), ReadPath(path)) | (ReadPath(path), WriteTree(tree)) => { |
| 49 | path.starts_with(tree) |
| 50 | } |
| 51 | (WriteTree(tree), WritePath(path)) | (WritePath(path), WriteTree(tree)) => { |
| 52 | path.starts_with(tree) |
| 53 | } |
| 54 | (ReadTree(left), WriteTree(right)) | (WriteTree(right), ReadTree(left)) => { |
| 55 | trees_overlap(left, right) |
| 56 | } |
| 57 | (WriteTree(left), WriteTree(right)) => trees_overlap(left, right), |
| 58 | _ => false, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn trees_overlap(left: &Path, right: &Path) -> bool { |
| 64 | left.starts_with(right) || right.starts_with(left) |
| 65 | } |
| 66 | |
| 67 | /// Build deterministic parallel batches. Items with no conflicting resource |
| 68 | /// claims share a batch; conflicting items retain their original order. |
| 69 | #[must_use] |
| 70 | pub fn schedule_non_conflicting<T>(items: Vec<(T, Vec<ResourceClaim>)>) -> Vec<Vec<T>> { |
| 71 | let mut batches = Vec::new(); |
| 72 | let mut batch = Vec::new(); |
| 73 | let mut batch_claims = Vec::new(); |
| 74 | |
| 75 | for (item, claims) in items { |
| 76 | let global_barrier = !batch.is_empty() |
| 77 | && (claims.contains(&ResourceClaim::GlobalExclusive) |
| 78 | || batch_claims.contains(&ResourceClaim::GlobalExclusive)); |
| 79 | let conflicts = global_barrier |
| 80 | || claims.iter().any(|claim| { |
| 81 | batch_claims |
| 82 | .iter() |
| 83 | .any(|existing| claim.conflicts_with(existing)) |
| 84 | }); |
| 85 | if conflicts && !batch.is_empty() { |
| 86 | batches.push(std::mem::take(&mut batch)); |
| 87 | batch_claims.clear(); |
| 88 | } |
| 89 | |
| 90 | batch.push(item); |
| 91 | batch_claims.extend(claims); |
| 92 | } |
| 93 | |
| 94 | if !batch.is_empty() { |
| 95 | batches.push(batch); |
| 96 | } |
| 97 | |
| 98 | batches |
| 99 | } |
| 100 | |
| 101 | #[cfg(test)] |
| 102 | mod tests { |
| 103 | use super::*; |
| 104 | |
| 105 | #[test] |
| 106 | fn two_reads_share_a_batch_but_write_is_ordered() { |
| 107 | let path = PathBuf::from("src/lib.rs"); |
| 108 | let batches = schedule_non_conflicting(vec![ |
| 109 | ("read-a", vec![ResourceClaim::ReadPath(path.clone())]), |
| 110 | ("read-b", vec![ResourceClaim::ReadPath(path.clone())]), |
| 111 | ("write", vec![ResourceClaim::WritePath(path)]), |
| 112 | ]); |
| 113 | assert_eq!(batches, vec![vec!["read-a", "read-b"], vec!["write"]]); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn unrelated_writes_can_run_together() { |
| 118 | let batches = schedule_non_conflicting(vec![ |
| 119 | ("a", vec![ResourceClaim::WritePath(PathBuf::from("a.rs"))]), |
| 120 | ("b", vec![ResourceClaim::WritePath(PathBuf::from("b.rs"))]), |
| 121 | ]); |
| 122 | assert_eq!(batches, vec![vec!["a", "b"]]); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn intervening_conflict_preserves_contiguous_order() { |
| 127 | let path = PathBuf::from("src/lib.rs"); |
| 128 | let batches = schedule_non_conflicting(vec![ |
| 129 | ("read-before", vec![ResourceClaim::ReadPath(path.clone())]), |
| 130 | ("write", vec![ResourceClaim::WritePath(path.clone())]), |
| 131 | ("read-after", vec![ResourceClaim::ReadPath(path)]), |
| 132 | ]); |
| 133 | assert_eq!( |
| 134 | batches, |
| 135 | vec![vec!["read-before"], vec!["write"], vec!["read-after"]] |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn tree_claims_conflict_only_when_a_write_scope_overlaps() { |
| 141 | let src = PathBuf::from("workspace/src"); |
| 142 | let nested = PathBuf::from("workspace/src/nested"); |
| 143 | let source_file = PathBuf::from("workspace/src/lib.rs"); |
| 144 | let test_file = PathBuf::from("workspace/tests/test.rs"); |
| 145 | |
| 146 | assert!( |
| 147 | !ResourceClaim::ReadTree(src.clone()) |
| 148 | .conflicts_with(&ResourceClaim::ReadPath(source_file.clone())) |
| 149 | ); |
| 150 | assert!( |
| 151 | ResourceClaim::ReadTree(src.clone()) |
| 152 | .conflicts_with(&ResourceClaim::WritePath(source_file.clone())) |
| 153 | ); |
| 154 | assert!( |
| 155 | !ResourceClaim::ReadTree(src.clone()) |
| 156 | .conflicts_with(&ResourceClaim::WritePath(test_file)) |
| 157 | ); |
| 158 | assert!( |
| 159 | ResourceClaim::WriteTree(src.clone()) |
| 160 | .conflicts_with(&ResourceClaim::ReadPath(source_file)) |
| 161 | ); |
| 162 | assert!(ResourceClaim::WriteTree(src).conflicts_with(&ResourceClaim::ReadTree(nested))); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn global_exclusive_stays_a_singleton_barrier() { |
| 167 | let batches = schedule_non_conflicting(vec![ |
| 168 | ("before", Vec::new()), |
| 169 | ("global", vec![ResourceClaim::GlobalExclusive]), |
| 170 | ("after", Vec::new()), |
| 171 | ]); |
| 172 | assert_eq!(batches, vec![vec!["before"], vec!["global"], vec!["after"]]); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn global_exclusive_conflicts_with_every_claim() { |
| 177 | for claim in [ |
| 178 | ResourceClaim::ReadOnly, |
| 179 | ResourceClaim::WorkspaceRead, |
| 180 | ResourceClaim::ReadPath(PathBuf::from("src/lib.rs")), |
| 181 | ResourceClaim::Terminal("shell-1".to_string()), |
| 182 | ResourceClaim::ToolCatalogWrite, |
| 183 | ResourceClaim::GlobalExclusive, |
| 184 | ] { |
| 185 | assert!(ResourceClaim::GlobalExclusive.conflicts_with(&claim)); |
| 186 | assert!(claim.conflicts_with(&ResourceClaim::GlobalExclusive)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn readonly_workspace_read_and_catalog_claims_schedule_with_write_barriers() { |
| 192 | let batches = schedule_non_conflicting(vec![ |
| 193 | ("read", vec![ResourceClaim::ReadPath(PathBuf::from("a.rs"))]), |
| 194 | ("git_status", vec![ResourceClaim::WorkspaceRead]), |
| 195 | ("bash_ro", vec![ResourceClaim::WorkspaceRead]), |
| 196 | ("load_skill", vec![ResourceClaim::ReadOnly]), |
| 197 | ("tool_search_1", vec![ResourceClaim::ToolCatalogWrite]), |
| 198 | ("grep", vec![ResourceClaim::ReadTree(PathBuf::from("src"))]), |
| 199 | ("tool_search_2", vec![ResourceClaim::ToolCatalogWrite]), |
| 200 | ( |
| 201 | "edit", |
| 202 | vec![ResourceClaim::WritePath(PathBuf::from("b.rs"))], |
| 203 | ), |
| 204 | ("git_diff", vec![ResourceClaim::WorkspaceRead]), |
| 205 | ("mutating_bash", vec![ResourceClaim::GlobalExclusive]), |
| 206 | ("session_search", vec![ResourceClaim::ReadOnly]), |
| 207 | ]); |
| 208 | assert_eq!( |
| 209 | batches, |
| 210 | vec![ |
| 211 | vec![ |
| 212 | "read", |
| 213 | "git_status", |
| 214 | "bash_ro", |
| 215 | "load_skill", |
| 216 | "tool_search_1", |
| 217 | "grep", |
| 218 | "tool_search_2" |
| 219 | ], |
| 220 | vec!["edit"], |
| 221 | vec!["git_diff"], |
| 222 | vec!["mutating_bash"], |
| 223 | vec!["session_search"], |
| 224 | ] |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn workspace_read_conflicts_with_writes_but_not_reads() { |
| 230 | assert!( |
| 231 | !ResourceClaim::WorkspaceRead |
| 232 | .conflicts_with(&ResourceClaim::ReadPath(PathBuf::from("a.rs"))) |
| 233 | ); |
| 234 | assert!( |
| 235 | !ResourceClaim::WorkspaceRead |
| 236 | .conflicts_with(&ResourceClaim::ReadTree(PathBuf::from("src"))) |
| 237 | ); |
| 238 | assert!(!ResourceClaim::WorkspaceRead.conflicts_with(&ResourceClaim::ReadOnly)); |
| 239 | assert!(!ResourceClaim::WorkspaceRead.conflicts_with(&ResourceClaim::WorkspaceRead)); |
| 240 | assert!(!ResourceClaim::WorkspaceRead.conflicts_with(&ResourceClaim::ToolCatalogWrite)); |
| 241 | assert!( |
| 242 | ResourceClaim::WorkspaceRead |
| 243 | .conflicts_with(&ResourceClaim::WritePath(PathBuf::from("a.rs"))) |
| 244 | ); |
| 245 | assert!( |
| 246 | ResourceClaim::WritePath(PathBuf::from("a.rs")) |
| 247 | .conflicts_with(&ResourceClaim::WorkspaceRead) |
| 248 | ); |
| 249 | assert!( |
| 250 | ResourceClaim::WorkspaceRead |
| 251 | .conflicts_with(&ResourceClaim::WriteTree(PathBuf::from("src"))) |
| 252 | ); |
| 253 | assert!( |
| 254 | ResourceClaim::WriteTree(PathBuf::from("src")) |
| 255 | .conflicts_with(&ResourceClaim::WorkspaceRead) |
| 256 | ); |
| 257 | } |
| 258 | } |
| 259 |