From 1305b292902a28eeeeeb3a0cfa2162239e775ec1 Mon Sep 17 00:00:00 2001 From: oxa agent Date: Tue, 17 Nov 2026 19:53:32 +0100 Subject: [PATCH 01/24] test(work): reproduce oversized board detail query failures --- oxa/oxa-cli/tests/work_shadow.rs | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/oxa/oxa-cli/tests/work_shadow.rs b/oxa/oxa-cli/tests/work_shadow.rs index 1bd6f601..bf562c5f 100644 --- a/oxa/oxa-cli/tests/work_shadow.rs +++ b/oxa/oxa-cli/tests/work_shadow.rs @@ -41,6 +41,7 @@ fn fixture() -> (TestEnv, serde_json::Value) { let gh = env.write("tmp/gh", r#"#!/bin/sh printf 'read\n' >> gh-calls if [ -e expect-input ]; then IFS= read -r expected < expect-input; [ "$GH_TOKEN" = "$expected" ] || exit 93; fi +case " $* " in *' --include '*) printf 'HTTP/2.0 200 OK\r\n\r\n' ;; esac if [ -e oversized ]; then printf '{"data":{"repository":{"pullRequests":{"totalCount":0,"nodes":[]},"padding":"' head -c 4194305 /dev/zero | tr '\000' 'a' @@ -298,3 +299,98 @@ fn unreported_classic_and_ruleset_requirements_are_pending() { ); } } + +fn batched_fixture(count: u64, limit: usize, failure: u16) -> TestEnv { + let (env, detail) = fixture(); + let connection = serde_json::json!({"totalCount":count,"nodes":(1..=count) + .map(|number| serde_json::json!({"number":number})).collect::>()}); + fs::write(env.work().join("list.json"), connection.to_string()).unwrap(); + fs::create_dir(env.work().join("rows")).unwrap(); + for number in 1..=count { + let mut row = detail["data"]["repository"]["p42"].clone(); + row["number"] = number.into(); + fs::write(env.work().join(format!("rows/{number}.json")), row.to_string()).unwrap(); + } + fs::write(env.work().join("limit"), limit.to_string()).unwrap(); + fs::write(env.work().join("failure"), failure.to_string()).unwrap(); + let gh = env.write("tmp/gh", r#"#!/bin/sh +include=false +case " $* " in *' --include '*) include=true ;; esac +numbers=$(printf '%s\n' "$*" | grep -oE 'p[0-9]+:pullRequest\(number:[0-9]+\)' | sed -E 's/.*number:([0-9]+)\)/\1/') +set -- $numbers +printf '%s\n' "$#" >> batch-sizes +status=200 +if [ "$#" -gt "$(cat limit)" ]; then status=$(cat failure); fi +if "$include"; then printf 'HTTP/2.0 %s Synthetic\r\n\r\n' "$status"; fi +if [ "$status" != 200 ]; then + printf '{"message":"synthetic-private-response-marker"}' + exit 1 +fi +printf '{"data":{"repository":{"pullRequests":' +cat list.json +printf ',"mergeQueue":null' +for number in "$@"; do + printf ',"p%s":' "$number" + cat "rows/$number.json" +done +printf '}}}' +"#); + fs::set_permissions(gh, fs::Permissions::from_mode(0o700)).unwrap(); + env +} + +fn batch_sizes(env: &TestEnv) -> Vec { + fs::read_to_string(env.work().join("batch-sizes")) + .unwrap() + .lines() + .map(|line| line.parse().unwrap()) + .collect() +} + +#[test] +fn ninety_pr_details_are_read_in_small_batches_without_losing_rows() { + let env = batched_fixture(90, 15, 504); + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + let sizes = batch_sizes(&env); + assert_eq!(sizes[0], 0); + assert!(sizes[1..].iter().all(|size| *size > 0 && *size <= 15)); + assert_eq!(sizes.iter().sum::(), 90); + let text = fs::read_to_string(log_for(&env, "example/repo")).unwrap(); + let sample: serde_json::Value = serde_json::from_str(text.trim()).unwrap(); + let numbers: Vec<_> = sample["board"]["entries"] + .as_array() + .unwrap() + .iter() + .map(|row| row["number"].as_u64().unwrap()) + .collect(); + assert_eq!(numbers, (1..=90).collect::>()); +} + +#[test] +fn repeated_gateway_failures_retry_then_reduce_the_detail_batch() { + for status in [502, 503, 504] { + let env = batched_fixture(31, 7, status); + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + let sizes = batch_sizes(&env); + assert_eq!(&sizes[..5], &[0, 15, 15, 15, 7]); + assert!(sizes[4..].iter().all(|size| *size > 0 && *size <= 7)); + } +} + +#[test] +fn terminal_http_failures_are_bounded_named_and_do_not_publish_a_partial_board() { + for (status, attempts) in [(401, 1), (403, 1), (429, 1), (504, 3)] { + let env = batched_fixture(1, 0, status); + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(!out.status.success()); + let error = String::from_utf8_lossy(&out.stderr); + assert!(error.contains(&format!("HTTP {status}")), "{error}"); + assert!(!error.contains("synthetic")); + assert_eq!(batch_sizes(&env), [vec![0], vec![1; attempts]].concat()); + assert!(fs::read_to_string(log_for(&env, "example/repo")) + .unwrap() + .is_empty()); + } +} From 5fc27bfaf302c75726a406a9859be9914af1050e Mon Sep 17 00:00:00 2001 From: oxa agent Date: Tue, 17 Nov 2026 20:08:52 +0100 Subject: [PATCH 02/24] fix(work): bound and retry board detail batches (#1153) --- oxa/oxa-cli/src/work_board.rs | 144 +++++++++++++++++++++++++------ oxa/oxa-cli/tests/work_shadow.rs | 10 +++ 2 files changed, 126 insertions(+), 28 deletions(-) diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index 78355910..5774ee1e 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -6,7 +6,7 @@ use std::{ io::{BufRead, IsTerminal, Read}, path::Path, process::Command, - time::SystemTime, + time::{Duration, SystemTime}, }; fn token_line(input: &mut dyn BufRead) -> Result { @@ -34,9 +34,41 @@ struct Reader<'a> { token: &'a str, } +const MAX_RESPONSE: usize = 4 * 1024 * 1024; + +enum QueryError { + Http(u16), + Other(&'static str), +} +impl QueryError { + fn retryable(&self) -> bool { + matches!(self, Self::Http(502 | 503 | 504)) + } + fn message(self) -> String { + match self { + Self::Http(status) => format!("GitHub read failed (HTTP {status})"), + Self::Other(message) => message.into(), + } + } +} + impl Reader<'_> { - fn query(&self, fields: &str) -> Result { - let (owner, name) = self.repo.split_once('/').ok_or("expected OWNER/REPO")?; + fn query(&self, fields: &str) -> Result<(Value, usize), QueryError> { + let mut retries = 0; + loop { + match self.query_once(fields) { + Err(error) if error.retryable() && retries < 2 => { + std::thread::sleep(Duration::from_millis(200 << retries)); + retries += 1; + } + result => return result, + } + } + } + + fn query_once(&self, fields: &str) -> Result<(Value, usize), QueryError> { + let invalid = QueryError::Other; + let (owner, name) = self.repo.split_once('/').ok_or(invalid("expected OWNER/REPO"))?; let query = format!("query{{repository(owner:{owner:?},name:{name:?}){{{fields}}}}}"); let mut command = Command::new(self.gh); command @@ -45,26 +77,49 @@ impl Reader<'_> { .env_remove("GH_DEBUG") .args([ "api", + "--include", "--hostname", "github.com", "graphql", "-f", &format!("query={query}"), ]); - let (code, text) = crate::report_cmd::command_runner(command, Some(4 * 1024 * 1024)) - .ok_or("GitHub read failed, exceeded its byte limit or timed out")?; - if code != 0 { - return Err("GitHub read failed".into()); + let (code, text) = crate::report_cmd::command_runner(command, Some(MAX_RESPONSE)).ok_or(invalid( + "GitHub read failed, exceeded its byte limit or timed out", + ))?; + let (headers, body) = text + .split_once("\r\n\r\n") + .or_else(|| text.split_once("\n\n")) + .ok_or(invalid("GitHub command failed without an HTTP response"))?; + let status = headers + .lines() + .next() + .filter(|line| line.starts_with("HTTP/")) + .and_then(|line| line.split_whitespace().nth(1)) + .and_then(|value| value.parse::().ok()) + .filter(|value| (100..600).contains(value)) + .ok_or(invalid("invalid GitHub HTTP status"))?; + if !(200..300).contains(&status) { + return Err(QueryError::Http(status)); } - let result: Value = serde_json::from_str(&text).map_err(|_| "invalid GitHub response")?; + let result: Value = serde_json::from_str(body).map_err(|_| invalid("invalid GitHub response"))?; if result.get("errors").is_some() { - return Err("incomplete GitHub response".into()); + return Err(invalid(match result["errors"][0]["type"].as_str() { + Some("FORBIDDEN") => "GitHub GraphQL error (FORBIDDEN)", + Some("RATE_LIMITED") => "GitHub GraphQL error (RATE_LIMITED)", + Some("NOT_FOUND") => "GitHub GraphQL error (NOT_FOUND)", + _ => "GitHub GraphQL error", + })); } - result + if code != 0 { + return Err(QueryError::Http(status)); + } + let repository = result .pointer("/data/repository") .filter(|v| v.is_object()) .cloned() - .ok_or("missing GitHub repository".into()) + .ok_or(invalid("missing GitHub repository"))?; + Ok((repository, text.len())) } } @@ -105,14 +160,30 @@ fn required_contexts(base: &Value) -> Result, String> { impl GitHub for Reader<'_> { fn open_pull_requests(&mut self) -> Result, String> { - // One list plus one batched detail query, never per-PR polling. Fail on truncation. + // Small detail batches retain every field and recheck the complete open set. const LIST: &str = "pullRequests(first:100,states:OPEN){totalCount nodes{number}}"; - let list = self.query(&format!("{LIST} mergeQueue(branch:\"main\"){{id}}"))?; + let (list, _) = self + .query(&format!("{LIST} mergeQueue(branch:\"main\"){{id}}")) + .map_err(QueryError::message)?; let has_queue = list["mergeQueue"].is_object(); - let mut fields = LIST.to_owned(); - for pr in nodes(&list["pullRequests"])? { - let n = pr["number"].as_u64().ok_or("missing PR number")?; - fields.push_str(&format!( + let numbers: Vec = nodes(&list["pullRequests"])? + .iter() + .map(|pr| pr["number"].as_u64().ok_or("missing PR number")) + .collect::>()?; + let ids = |connection: &Value| -> Result, String> { + nodes(connection)? + .iter() + .map(|pr| pr["number"].as_u64().ok_or("missing PR number".into())) + .collect() + }; + let expected = ids(&list["pullRequests"])?; + let mut details = serde_json::json!({}); + let (mut offset, mut size, mut bytes) = (0, 15, 0usize); + loop { + let end = numbers.len().min(offset + size); + let mut fields = LIST.to_owned(); + for n in &numbers[offset..end] { + fields.push_str(&format!( r#"p{n}:pullRequest(number:{n}){{number headRefOid baseRefName state body isDraft reviewDecision mergeable mergeStateStatus baseRef{{refUpdateRule{{requiredStatusCheckContexts}} rules(first:100){{totalCount nodes{{type parameters{{... on RequiredStatusChecksParameters{{requiredStatusChecks{{context}}}}}}}}}}}} labels(first:100){{totalCount nodes{{name}}}} @@ -123,17 +194,34 @@ impl GitHub for Reader<'_> { ... on CheckRun{{name isRequired(pullRequestNumber:{n}) status conclusion}} ... on StatusContext{{context isRequired(pullRequestNumber:{n}) state}} }}}}}}}}}}}}}}"# - )); - } - let details = self.query(&fields)?; - let ids = |connection: &Value| -> Result, String> { - nodes(connection)? - .iter() - .map(|pr| pr["number"].as_u64().ok_or("missing PR number".into())) - .collect() - }; - if ids(&list["pullRequests"])? != ids(&details["pullRequests"])? { - return Err("open PR set changed during snapshot; retry later".into()); + )); + } + let (page, read) = match self.query(&fields) { + Ok(page) => page, + Err(error) if error.retryable() && end - offset > 1 => { + size = (end - offset) / 2; + continue; + } + Err(error) => return Err(error.message()), + }; + bytes = bytes + .checked_add(read) + .filter(|total| *total <= MAX_RESPONSE) + .ok_or("GitHub detail snapshot exceeds byte limit")?; + if ids(&page["pullRequests"])? != expected { + return Err("open PR set changed during snapshot; retry later".into()); + } + for n in &numbers[offset..end] { + let row = &page[format!("p{n}")]; + if row["number"].as_u64() != Some(*n) { + return Err("missing or mismatched PR details".into()); + } + details[format!("p{n}")] = row.clone(); + } + offset = end; + if offset == numbers.len() { + break; + } } nodes(&list["pullRequests"])? .iter() diff --git a/oxa/oxa-cli/tests/work_shadow.rs b/oxa/oxa-cli/tests/work_shadow.rs index bf562c5f..ba29edee 100644 --- a/oxa/oxa-cli/tests/work_shadow.rs +++ b/oxa/oxa-cli/tests/work_shadow.rs @@ -321,6 +321,7 @@ set -- $numbers printf '%s\n' "$#" >> batch-sizes status=200 if [ "$#" -gt "$(cat limit)" ]; then status=$(cat failure); fi +if [ -e fail-after ] && [ "$(wc -l < batch-sizes)" -gt "$(cat fail-after)" ]; then status=403; fi if "$include"; then printf 'HTTP/2.0 %s Synthetic\r\n\r\n' "$status"; fi if [ "$status" != 200 ]; then printf '{"message":"synthetic-private-response-marker"}' @@ -393,4 +394,13 @@ fn terminal_http_failures_are_bounded_named_and_do_not_publish_a_partial_board() .unwrap() .is_empty()); } + let env = batched_fixture(31, 15, 504); + fs::write(env.work().join("fail-after"), "2").unwrap(); + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(!out.status.success()); + assert!(String::from_utf8_lossy(&out.stderr).contains("HTTP 403")); + assert_eq!(batch_sizes(&env), [0, 15, 15]); + assert!(fs::read_to_string(log_for(&env, "example/repo")) + .unwrap() + .is_empty()); } From 52863c54fb91dab427af945da22d556efd6467ed Mon Sep 17 00:00:00 2001 From: oxstack-agent Date: Tue, 17 Nov 2026 20:45:14 +0100 Subject: [PATCH 03/24] feat(work): consume admitted helper packets with fenced claims (#1017) --- oxa/oxa-cli/src/lib.rs | 1 + oxa/oxa-cli/src/work_refill.rs | 159 ++++++++++++++++++++++ oxa/oxa-cli/tests/work_refill.rs | 220 +++++++++++++++++++++++++++++++ oxa/oxa-work/src/helper_queue.rs | 19 ++- 4 files changed, 398 insertions(+), 1 deletion(-) create mode 100644 oxa/oxa-cli/src/work_refill.rs create mode 100644 oxa/oxa-cli/tests/work_refill.rs diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index a5a074d4..c9be6cac 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -46,6 +46,7 @@ mod wipe_cmd; mod work_board; mod work_checks; mod work_cmd; +pub mod work_refill; pub use args::{ no_tool_alias, parse_args, usage_text, ApiKey, Args, LoginArgs, ModeArg, Subcommand, NO_TOOL_ALIASES, diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs new file mode 100644 index 00000000..c3610140 --- /dev/null +++ b/oxa/oxa-cli/src/work_refill.rs @@ -0,0 +1,159 @@ +//! Queue-only consuming adapter; registration, prepared taskfiles and activation are user-owned. +use oxa_work::board::{assignment::Worker, *}; +use oxa_work::{Result, helper_queue}; +use sha2::{Digest, Sha256}; +use std::{collections::BTreeSet, fs, path::Path}; + +/// Read-only acquisition and authoritative revision/action revalidation; no write capability. +pub trait Source: GitHub { + fn revalidate(&mut self, entry: &Entry) -> Result; +} + +/// Apply the same operator-selected item partition to BOTH PR and issue intake. +pub struct Partition { + pub modulus: u64, + pub residues: BTreeSet, +} +impl Partition { + fn contains(&self, number: u64) -> bool { + self.residues.contains(&(number % self.modulus)) + } +} +struct Partitioned<'a, S>(&'a mut S, &'a Partition); +impl GitHub for Partitioned<'_, S> { + fn snapshot(&mut self) -> Result { + let mut snapshot = self.0.snapshot()?; + snapshot.pull_requests.retain(|pr| self.1.contains(pr.number)); + snapshot.issues.retain(|issue| self.1.contains(issue.number)); + Ok(snapshot) + } +} + +/// Existing bounded taskfile bytes, frozen to the exact candidate; never a new packet builder. +pub struct Packet<'a> { + pub number: u64, + pub revision: &'a str, + pub action: Action, + pub task: &'a str, +} +pub struct Receipt { + pub number: u64, + pub key: String, + pub lease: Lease, +} + +/// One authenticated queue target. Default Policy is OFF. The pause marker persists across +/// restarts; only the operator may remove it after qualification. No workers are spawned here. +pub struct Consumer<'a> { + pub repository: &'a str, + pub queue: &'a Path, + pub pause: &'a Path, + pub claims: &'a Claims, + pub worker: &'a Worker, + pub partition: &'a Partition, + pub policy: &'a Policy, + pub ttl: u64, +} +impl Consumer<'_> { + fn paused(&self) -> Result { + match fs::symlink_metadata(self.pause) { + Ok(_) => Ok(true), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(_) => Err("cannot establish board pause state".into()), + } + } + + /// At most one task per call. Any outstanding lease, including an expired lease, keeps + /// this worker busy until fenced completion. Unknown bootstrap rows are never zero counts. + /// Audit must durably record the board before reservation/publication. It receives no packet. + pub fn once( + &self, + source: &mut impl Source, + packets: &[Packet<'_>], + now: u64, + mut audit: impl FnMut(&AssignmentBoard) -> Result<()>, + ) -> Result> { + if !self.policy.enabled || self.paused()? { + return Ok(vec![]); + } + if self.partition.modulus == 0 + || self.partition.residues.is_empty() + || self + .partition + .residues + .iter() + .any(|r| *r >= self.partition.modulus) + || self.policy.max_attempts_per_day > 3 + { + return Err("invalid board partition or attempt cap".into()); + } + let board = self.claims.assignment_board( + &mut Partitioned(source, self.partition), + self.policy, + std::slice::from_ref(self.worker), + now, + )?; + audit(&board)?; + for number in board.candidates(&self.worker.id)? { + let entry = board.board.entries.iter().find(|e| e.number == number).unwrap(); + if !matches!( + entry.action, + Action::FixFindings + | Action::FixRequiredCi + | Action::ResolveConflict + | Action::AddRemoves + | Action::BuildIssue + ) { + continue; + } + let matching: Vec<_> = packets.iter().filter(|p| p.number == number).collect(); + let [packet] = matching.as_slice() else { continue }; + if packet.revision != entry.head || packet.action != entry.action { + continue; + } + let identity = serde_json::to_vec(&(self.repository, number, &entry.head, entry.action)) + .map_err(|_| "cannot encode board queue identity")?; + let key = format!("{:x}", Sha256::digest(identity)); + let mut lease = None; + let published = helper_queue::publish_reserved( + self.queue, + &key, + packet.task, + entry.release_pinned, + self.policy, + || { + if self.paused()? { + return Ok(false); + } + let rows = self.claims.assignments()?; + // Recheck under the shared queue publication lock, including other refs. + if !rows.contains_key(&number) + || rows.values().any(|row| { + row.state + .lease + .as_ref() + .is_some_and(|l| l.holder == self.worker.id) + }) + || rows[&number].state.lease.is_some() + || !source.revalidate(entry)? + || self.paused()? + { + return Ok(false); + } + lease = self + .claims + .claim_for(entry, self.policy, self.worker, now, self.ttl)?; + Ok(lease.is_some()) + }, + )?; + if published { + return Ok(vec![Receipt { + number, + key, + lease: lease.ok_or("missing queue lease")?, + }]); + } + } + Ok(vec![]) + } +} diff --git a/oxa/oxa-cli/tests/work_refill.rs b/oxa/oxa-cli/tests/work_refill.rs new file mode 100644 index 00000000..de8d41db --- /dev/null +++ b/oxa/oxa-cli/tests/work_refill.rs @@ -0,0 +1,220 @@ +use oxa_cli::work_refill::{Consumer, Packet, Partition, Source}; +use oxa_fake::TestEnv; +use oxa_work::board::{assignment::*, *}; +use std::{collections::BTreeMap, fs, os::unix::fs::DirBuilderExt}; + +#[derive(Default)] +struct Fake { + reads: usize, + checks: usize, + stale: bool, +} +impl GitHub for Fake { + fn snapshot(&mut self) -> oxa_work::Result { + self.reads += 1; + Ok(Snapshot { + pull_requests: vec![PullRequest { + number: 45, + head: "head".into(), + release_pinned: false, + findings: true, + required_ci: RequiredCi::Passed, + mergeability: Mergeability::Mergeable, + body: String::new(), + removes_source: false, + draft: false, + reviewed: false, + }], + issues: [44, 46] + .map(|number| Issue { + number, + revision: "revision".into(), + release_pinned: false, + approved: true, + has_open_pull_request: false, + }) + .into(), + }) + } +} +impl Source for Fake { + fn revalidate(&mut self, _: &Entry) -> oxa_work::Result { + self.checks += 1; + Ok(!self.stale) + } +} +const AUTHORITY: Option = Some(ManualAuthority::Owner); +const TASK: &str = "i44 local/fake agent/issue-44 main\nSynthetic bounded existing taskfile.\n"; + +fn seed(claims: &Claims, excluded: bool) { + let incoming = BTreeMap::from([( + 44, + ImportState { + state: AttemptState::default(), + excluded, + }, + )]); + claims.import_snapshot(AUTHORITY, &incoming, 10).unwrap(); +} +fn no_task(target: Consumer<'_>, source: &mut Fake, packets: &[Packet<'_>], now: u64) { + assert!(target.once(source, packets, now, |_| Ok(())).unwrap().is_empty()); +} + +#[test] +fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { + let env = TestEnv::new(); + let root = env.tmp().join("claims"); + let queue = env.tmp().join("queue"); + let pause = env.tmp().join("paused"); + for path in [&root, &queue] { + fs::DirBuilder::new().mode(0o700).create(path).unwrap(); + } + let claims = Claims::open(&root.canonicalize().unwrap()).unwrap(); + seed(&claims, false); + let worker = Worker { + id: "oxa:node/helper".into(), + kind: WorkerKind::Oxa, + roles: [Role::Builder].into(), + skills: Default::default(), + }; + let partition = Partition { + modulus: 22, + residues: [0].into(), + }; + let off = Policy::default(); + let policy = Policy { + enabled: true, + ..Policy::default() + }; + let packets = [Packet { + number: 44, + revision: "revision", + action: Action::BuildIssue, + task: TASK, + }]; + let mut source = Fake::default(); + let target = |enabled| Consumer { + repository: "example/project", + queue: &queue, + pause: &pause, + claims: &claims, + worker: &worker, + partition: &partition, + policy: if enabled { &policy } else { &off }, + ttl: 60, + }; + assert!( + target(false) + .once(&mut source, &packets, 10, |_| panic!("off audit")) + .unwrap() + .is_empty() + ); + fs::write(&pause, b"paused").unwrap(); + // Recreating the consumer simulates service startup: the persistent marker still wins. + assert!( + target(true) + .once(&mut source, &packets, 10, |_| panic!("paused audit")) + .unwrap() + .is_empty() + ); + assert_eq!((source.reads, source.checks), (0, 0)); + fs::remove_file(&pause).unwrap(); + assert!( + target(true) + .once(&mut source, &packets, 10, |_| Err("audit failed".into())) + .is_err() + ); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + source.stale = true; + no_task(target(true), &mut source, &packets, 10); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + source.stale = false; + let receipts = target(true) + .once(&mut source, &packets, 10, |board| { + assert_eq!( + board.board.entries.iter().map(|e| e.number).collect::>(), + [44] + ); + Ok(()) + }) + .unwrap(); + assert_eq!(receipts.len(), 1); + let receipt = &receipts[0]; + assert_eq!(receipt.lease.holder, worker.id); + let name = format!("board-{}.txt", receipt.key); + assert_eq!(fs::read_to_string(queue.join(&name)).unwrap(), TASK); + fs::rename(queue.join(&name), queue.join("started").join(name)).unwrap(); + no_task(target(true), &mut source, &packets, 80); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); + assert!( + claims + .release_for(44, &worker.id, &receipt.lease, 80, "verified completion") + .unwrap() + ); + // Retained started file dedupes before a second claim, even after fenced release. + no_task(target(true), &mut source, &packets, 81); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); +} + +#[test] +fn unknown_held_owner_only_and_reviewer_work_never_publishes() { + let env = TestEnv::new(); + let root = env.tmp().join("claims"); + let queue = env.tmp().join("queue"); + let pause = env.tmp().join("paused"); + for path in [&root, &queue] { + fs::DirBuilder::new().mode(0o700).create(path).unwrap(); + } + let claims = Claims::open(&root.canonicalize().unwrap()).unwrap(); + let policy = Policy { + enabled: true, + ..Policy::default() + }; + let partition = Partition { + modulus: 22, + residues: [0].into(), + }; + let mut worker = Worker { + id: "oxa:node/helper".into(), + kind: WorkerKind::Oxa, + roles: [Role::Builder].into(), + skills: Default::default(), + }; + let packets = [Packet { + number: 44, + revision: "revision", + action: Action::BuildIssue, + task: TASK, + }]; + let mut source = Fake::default(); + for stage in 0..4 { + if stage == 1 { + seed(&claims, true); + } + if stage == 2 { + worker.roles = [Role::Reviewer].into(); + let revision = claims.assignments().unwrap()[&44].data.revision; + claims.clear_import_hold(44, AUTHORITY, revision).unwrap(); + } + if stage == 3 { + worker.roles = [Role::Builder].into(); + let revision = claims.assignments().unwrap()[&44].data.revision; + claims + .control(44, AUTHORITY, revision, Control::OwnerOnly(true)) + .unwrap(); + } + let target = Consumer { + repository: "example/project", + queue: &queue, + pause: &pause, + claims: &claims, + worker: &worker, + partition: &partition, + policy: &policy, + ttl: 60, + }; + no_task(target, &mut source, &packets, 10); + } + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + assert!(fs::read_dir(queue).unwrap().all(|e| e.unwrap().path().is_dir())); +} diff --git a/oxa/oxa-work/src/helper_queue.rs b/oxa/oxa-work/src/helper_queue.rs index ac98b9e6..b8ce8c20 100644 --- a/oxa/oxa-work/src/helper_queue.rs +++ b/oxa/oxa-work/src/helper_queue.rs @@ -1,5 +1,5 @@ //! Atomic publication only. The caller must reserve a core claim and authorize the target first. -use crate::{board::Policy, Result}; +use crate::{Result, board::Policy}; use std::fs::{self, File, OpenOptions}; use std::io::Write; use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt}; @@ -36,6 +36,20 @@ impl Drop for Staged { /// The consumer atomically moves ready files into `started` and retains them there. /// No GitHub client, worker execution or parallel lease storage exists in this publisher. pub fn publish(queue: &Path, key: &str, task: &str, pinned: bool, policy: &Policy) -> Result { + publish_reserved(queue, key, task, pinned, policy, || Ok(true)) +} + +/// Reserve under the publication lock, after packet validation and pending/started dedupe. +/// The callback must not access this queue. Errors after reservation are uncertain publication: +/// retain the reservation until the trusted completion frontend reconciles the exact lease. +pub fn publish_reserved( + queue: &Path, + key: &str, + task: &str, + pinned: bool, + policy: &Policy, + reserve: impl FnOnce() -> Result, +) -> Result { if !policy.enabled { return Ok(false); } @@ -82,6 +96,9 @@ pub fn publish(queue: &Path, key: &str, task: &str, pinned: bool, policy: &Polic } } } + if !reserve()? { + return Ok(false); + } let nonce = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_err(|_| "invalid board queue clock")? From e0746290485f7af7e90731346cc20d819b3619ba Mon Sep 17 00:00:00 2001 From: oxa agent Date: Tue, 17 Nov 2026 20:47:02 +0100 Subject: [PATCH 04/24] test(work): cover complete paginated board reads --- oxa/oxa-cli/tests/work_shadow.rs | 81 +++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/oxa/oxa-cli/tests/work_shadow.rs b/oxa/oxa-cli/tests/work_shadow.rs index 3cd00575..e8d4b8d2 100644 --- a/oxa/oxa-cli/tests/work_shadow.rs +++ b/oxa/oxa-cli/tests/work_shadow.rs @@ -60,6 +60,9 @@ esac fs::set_permissions(gh, fs::Permissions::from_mode(0o700)).unwrap(); let detail: serde_json::Value = serde_json::from_str(r#"{"data":{"repository":{"pullRequests":{"totalCount":1,"nodes":[{"number":42}]},"mergeQueue":null,"p42":{"number":42,"headRefOid":"synthetic-head","baseRefName":"main","state":"OPEN","body":"","isDraft":false,"reviewDecision":null,"mergeable":"CONFLICTING","mergeStateStatus":"CLEAN","labels":{"totalCount":0,"nodes":[]},"files":{"totalCount":0,"nodes":[]},"reviewThreads":{"totalCount":0,"nodes":[]},"latestOpinionatedReviews":{"totalCount":0,"nodes":[]},"commits":{"nodes":[{"commit":{"statusCheckRollup":null}}]}}}}}"#).unwrap(); let mut detail = detail; + detail["data"]["repository"]["pullRequests"]["nodes"][0]["headRefOid"] = "synthetic-head".into(); + detail["data"]["repository"]["pullRequests"]["pageInfo"] = + serde_json::json!({"hasNextPage":false,"endCursor":"synthetic-end"}); detail["data"]["repository"]["p42"]["baseRef"] = serde_json::json!({ "refUpdateRule":{"requiredStatusCheckContexts":["ci"]}, "rules":{"totalCount":0,"nodes":[]} @@ -307,9 +310,23 @@ fn unreported_classic_and_ruleset_requirements_are_pending() { fn batched_fixture(count: u64, limit: usize, failure: u16) -> TestEnv { let (env, detail) = fixture(); - let connection = serde_json::json!({"totalCount":count,"nodes":(1..=count) - .map(|number| serde_json::json!({"number":number})).collect::>()}); - fs::write(env.work().join("list.json"), connection.to_string()).unwrap(); + fs::create_dir(env.work().join("pages")).unwrap(); + for start in (0..count.max(1)).step_by(100) { + let end = count.min(start + 100); + let connection = serde_json::json!({"totalCount":count,"nodes":(start+1..=end) + .map(|number| serde_json::json!({"number":number,"headRefOid":"synthetic-head"})).collect::>(), + "pageInfo":{"hasNextPage":end TestEnv { let gh = env.write("tmp/gh", r#"#!/bin/sh include=false case " $* " in *' --include '*) include=true ;; esac +cursor=$(printf '%s\n' "$*" | sed -n 's/.*after:"\([^"]*\)".*/\1/p') numbers=$(printf '%s\n' "$*" | grep -oE 'p[0-9]+:pullRequest\(number:[0-9]+\)' | sed -E 's/.*number:([0-9]+)\)/\1/') set -- $numbers printf '%s\n' "$#" >> batch-sizes status=200 if [ "$#" -gt "$(cat limit)" ]; then status=$(cat failure); fi if [ -e fail-after ] && [ "$(wc -l < batch-sizes)" -gt "$(cat fail-after)" ]; then status=403; fi +if [ -e fail-page ] && [ "$cursor" = "$(cat fail-page)" ]; then status=403; fi if "$include"; then printf 'HTTP/2.0 %s Synthetic\r\n\r\n' "$status"; fi if [ "$status" != 200 ]; then printf '{"message":"synthetic-private-response-marker"}' exit 1 fi printf '{"data":{"repository":{"pullRequests":' -cat list.json +cat "pages/${cursor:-first}.json" printf ',"mergeQueue":null' for number in "$@"; do printf ',"p%s":' "$number" @@ -360,7 +379,10 @@ fn ninety_pr_details_are_read_in_small_batches_without_losing_rows() { assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); let sizes = batch_sizes(&env); assert_eq!(sizes[0], 0); - assert!(sizes[1..].iter().all(|size| *size > 0 && *size <= 15)); + assert_eq!(sizes.last(), Some(&0)); + assert!(sizes[1..sizes.len() - 1] + .iter() + .all(|size| *size > 0 && *size <= 15)); assert_eq!(sizes.iter().sum::(), 90); let text = fs::read_to_string(log_for(&env, "example/repo")).unwrap(); let sample: serde_json::Value = serde_json::from_str(text.trim()).unwrap(); @@ -381,7 +403,10 @@ fn repeated_gateway_failures_retry_then_reduce_the_detail_batch() { assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); let sizes = batch_sizes(&env); assert_eq!(&sizes[..5], &[0, 15, 15, 15, 7]); - assert!(sizes[4..].iter().all(|size| *size > 0 && *size <= 7)); + assert_eq!(sizes.last(), Some(&0)); + assert!(sizes[4..sizes.len() - 1] + .iter() + .all(|size| *size > 0 && *size <= 7)); } } @@ -410,6 +435,50 @@ fn terminal_http_failures_are_bounded_named_and_do_not_publish_a_partial_board() .is_empty()); } +#[test] +fn complete_paginated_boards_include_every_pr_up_to_the_limit() { + for count in [101, 1000] { + let env = batched_fixture(count, 15, 504); + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + let text = fs::read_to_string(log_for(&env, "example/repo")).unwrap(); + let sample: serde_json::Value = serde_json::from_str(text.trim()).unwrap(); + let numbers: Vec<_> = sample["board"]["entries"] + .as_array() + .unwrap() + .iter() + .map(|row| row["number"].as_u64().unwrap()) + .collect(); + assert_eq!(numbers, (1..=count).collect::>()); + assert_eq!(batch_sizes(&env).iter().sum::(), count as usize); + } +} + +#[test] +fn later_page_failures_and_changed_heads_never_publish_partial_boards() { + for fault in ["http", "duplicate", "head"] { + let env = batched_fixture(101, 15, 504); + if fault == "http" { + fs::write(env.work().join("fail-page"), "c100").unwrap(); + } else if fault == "duplicate" { + let path = env.work().join("pages/c100.json"); + let mut page: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + page["nodes"][0]["number"] = 1.into(); + fs::write(path, page.to_string()).unwrap(); + } else { + let path = env.work().join("rows/101.json"); + let mut row: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + row["headRefOid"] = "changed-head".into(); + fs::write(path, row.to_string()).unwrap(); + } + let out = invoke(&env, "work board --shadow --repo example/repo --once"); + assert!(!out.status.success(), "{fault}"); + assert!(fs::read_to_string(log_for(&env, "example/repo")) + .unwrap() + .is_empty()); + } +} + #[test] fn exited_github_child_cannot_escape_the_whole_read_deadline() { use std::time::{Duration, Instant}; From 1b47197347742230b663946299488663ef2cc19f Mon Sep 17 00:00:00 2001 From: oxa agent Date: Tue, 17 Nov 2026 20:50:31 +0100 Subject: [PATCH 05/24] fix(work): read and revalidate complete paginated boards (#1153) --- oxa/oxa-cli/src/work_board.rs | 96 +++++++++++++++++++++++++------- oxa/oxa-cli/tests/work_shadow.rs | 16 +++++- 2 files changed, 90 insertions(+), 22 deletions(-) diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index 1ae914e2..cdb2a989 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -35,6 +35,15 @@ struct Reader<'a> { } const MAX_RESPONSE: usize = 4 * 1024 * 1024; +const LIST: &str = "pullRequests(first:100,states:OPEN){totalCount pageInfo{hasNextPage endCursor} nodes{number headRefOid}}"; + +fn charge(bytes: &mut usize, read: usize) -> Result<(), String> { + *bytes = bytes + .checked_add(read) + .filter(|total| *total <= MAX_RESPONSE) + .ok_or("GitHub snapshot exceeds byte limit")?; + Ok(()) +} enum QueryError { Http(u16), @@ -53,6 +62,62 @@ impl QueryError { } impl Reader<'_> { + fn listing(&self, bytes: &mut usize) -> Result<(Value, Value), String> { + let (mut fields, mut rows, mut seen, mut cursors) = + (LIST.to_owned(), vec![], BTreeSet::new(), BTreeSet::new()); + let mut result = Value::Null; + loop { + let (page, read) = self + .query(&format!("{fields} mergeQueue(branch:\"main\"){{id}}")) + .map_err(QueryError::message)?; + charge(bytes, read)?; + if result.is_null() { + result = page.clone(); + } + let connection = &page["pullRequests"]; + let total = connection["totalCount"].as_u64().ok_or("missing PR count")?; + if total > 1000 + || connection["totalCount"] != result["pullRequests"]["totalCount"] + || page["mergeQueue"] != result["mergeQueue"] + { + return Err("GitHub listing exceeds limit or changed during snapshot".into()); + } + let nodes = connection["nodes"].as_array().ok_or("missing PR page")?; + if nodes.len() > 100 { + return Err("oversized PR page".into()); + } + for row in nodes { + let number = row["number"] + .as_u64() + .filter(|n| *n > 0) + .ok_or("invalid PR number")?; + if !seen.insert(number) || row["headRefOid"].as_str().is_none_or(str::is_empty) { + return Err("duplicate PR or missing head in listing".into()); + } + rows.push(row.clone()); + } + let more = connection["pageInfo"]["hasNextPage"] + .as_bool() + .ok_or("missing PR pagination")?; + if !more { + if rows.len() as u64 != total { + return Err("incomplete PR listing".into()); + } + let first = result["pullRequests"].clone(); + result["pullRequests"]["nodes"] = rows.into(); + return Ok((result, first)); + } + let cursor = connection["pageInfo"]["endCursor"] + .as_str() + .filter(|c| !c.is_empty()) + .ok_or("missing PR cursor")?; + if nodes.is_empty() || rows.len() as u64 >= total || !cursors.insert(cursor.to_owned()) { + return Err("invalid PR pagination progress".into()); + } + fields = LIST.replace("states:OPEN", &format!("states:OPEN,after:{cursor:?}")); + } + } + fn query(&self, fields: &str) -> Result<(Value, usize), QueryError> { let mut retries = 0; loop { @@ -160,25 +225,17 @@ fn required_contexts(base: &Value) -> Result, String> { impl GitHub for Reader<'_> { fn open_pull_requests(&mut self) -> Result, String> { - // Small detail batches retain every field and recheck the complete open set. - const LIST: &str = "pullRequests(first:100,states:OPEN){totalCount nodes{number}}"; - let (list, _) = self - .query(&format!("{LIST} mergeQueue(branch:\"main\"){{id}}")) - .map_err(QueryError::message)?; + // Verify the complete paginated identity set before publishing any details. + let mut bytes = 0; + let (list, first) = self.listing(&mut bytes)?; let has_queue = list["mergeQueue"].is_object(); let numbers: Vec = nodes(&list["pullRequests"])? .iter() .map(|pr| pr["number"].as_u64().ok_or("missing PR number")) .collect::>()?; - let ids = |connection: &Value| -> Result, String> { - nodes(connection)? - .iter() - .map(|pr| pr["number"].as_u64().ok_or("missing PR number".into())) - .collect() - }; - let expected = ids(&list["pullRequests"])?; + let expected: BTreeMap<_, _> = numbers.iter().zip(nodes(&list["pullRequests"])?).collect(); let mut details = serde_json::json!({}); - let (mut offset, mut size, mut bytes) = (0, 15, 0usize); + let (mut offset, mut size) = (0, 15); loop { let end = numbers.len().min(offset + size); let mut fields = LIST.to_owned(); @@ -204,16 +261,13 @@ impl GitHub for Reader<'_> { } Err(error) => return Err(error.message()), }; - bytes = bytes - .checked_add(read) - .filter(|total| *total <= MAX_RESPONSE) - .ok_or("GitHub detail snapshot exceeds byte limit")?; - if ids(&page["pullRequests"])? != expected { + charge(&mut bytes, read)?; + if page["pullRequests"] != first { return Err("open PR set changed during snapshot; retry later".into()); } for n in &numbers[offset..end] { let row = &page[format!("p{n}")]; - if row["number"].as_u64() != Some(*n) { + if row["number"].as_u64() != Some(*n) || row["headRefOid"] != expected[n]["headRefOid"] { return Err("missing or mismatched PR details".into()); } details[format!("p{n}")] = row.clone(); @@ -223,6 +277,10 @@ impl GitHub for Reader<'_> { break; } } + let (verified, _) = self.listing(&mut bytes)?; + if verified["pullRequests"] != list["pullRequests"] || verified["mergeQueue"] != list["mergeQueue"] { + return Err("PR identities changed during snapshot; retry later".into()); + } nodes(&list["pullRequests"])? .iter() .map(|pr| { diff --git a/oxa/oxa-cli/tests/work_shadow.rs b/oxa/oxa-cli/tests/work_shadow.rs index e8d4b8d2..7e71658d 100644 --- a/oxa/oxa-cli/tests/work_shadow.rs +++ b/oxa/oxa-cli/tests/work_shadow.rs @@ -456,14 +456,20 @@ fn complete_paginated_boards_include_every_pr_up_to_the_limit() { #[test] fn later_page_failures_and_changed_heads_never_publish_partial_boards() { - for fault in ["http", "duplicate", "head"] { + for fault in ["http", "late-http", "duplicate", "head", "cursor", "identity"] { let env = batched_fixture(101, 15, 504); if fault == "http" { fs::write(env.work().join("fail-page"), "c100").unwrap(); - } else if fault == "duplicate" { + } else if fault == "late-http" { + fs::write(env.work().join("fail-after"), "10").unwrap(); + } else if matches!(fault, "duplicate" | "cursor" | "identity") { let path = env.work().join("pages/c100.json"); let mut page: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); - page["nodes"][0]["number"] = 1.into(); + match fault { + "duplicate" => page["nodes"][0]["number"] = 1.into(), + "cursor" => page["pageInfo"]["hasNextPage"] = true.into(), + _ => page["nodes"][0]["headRefOid"] = serde_json::Value::Null, + } fs::write(path, page.to_string()).unwrap(); } else { let path = env.work().join("rows/101.json"); @@ -473,6 +479,10 @@ fn later_page_failures_and_changed_heads_never_publish_partial_boards() { } let out = invoke(&env, "work board --shadow --repo example/repo --once"); assert!(!out.status.success(), "{fault}"); + if fault == "late-http" { + assert!(String::from_utf8_lossy(&out.stderr).contains("HTTP 403")); + assert_eq!(batch_sizes(&env).iter().sum::(), 101); + } assert!(fs::read_to_string(log_for(&env, "example/repo")) .unwrap() .is_empty()); From 78bfb675d9f852c638f630472b3673541f7d4f0c Mon Sep 17 00:00:00 2001 From: oxa agent Date: Tue, 17 Nov 2026 20:51:29 +0100 Subject: [PATCH 06/24] refactor(work): share bounded board reader with native consumer --- oxa/oxa-cli/src/work_board.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index cdb2a989..abfa1ed5 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -27,7 +27,7 @@ fn token_line(input: &mut dyn BufRead) -> Result { Ok(token.to_owned()) } -struct Reader<'a> { +pub(crate) struct Reader<'a> { gh: &'a Path, path: &'a std::ffi::OsStr, repo: &'a str, @@ -61,15 +61,30 @@ impl QueryError { } } -impl Reader<'_> { +impl<'a> Reader<'a> { + /// Callers must enforce trust/offline policy and supply an explicit App credential. + pub(crate) fn new(gh: &'a Path, path: &'a std::ffi::OsStr, repo: &'a str, token: &'a str) -> Self { + Self { + gh, + path, + repo, + token, + } + } + + /// Fixed repository fields from native callers share the bounded, redacted transport. + /// The byte count lets callers enforce an aggregate snapshot budget across queries. + pub(crate) fn repository_read(&self, fields: &str) -> Result<(Value, usize), String> { + self.query(fields).map_err(QueryError::message) + } + fn listing(&self, bytes: &mut usize) -> Result<(Value, Value), String> { let (mut fields, mut rows, mut seen, mut cursors) = (LIST.to_owned(), vec![], BTreeSet::new(), BTreeSet::new()); let mut result = Value::Null; loop { - let (page, read) = self - .query(&format!("{fields} mergeQueue(branch:\"main\"){{id}}")) - .map_err(QueryError::message)?; + let (page, read) = + self.repository_read(&format!("{fields} mergeQueue(branch:\"main\"){{id}}"))?; charge(bytes, read)?; if result.is_null() { result = page.clone(); @@ -455,12 +470,7 @@ pub(crate) fn run( } else { inherited.ok_or("missing App token")?.to_owned() }; - let mut reader = Reader { - gh: &gh, - path: &policy.host_path, - repo, - token: &token, - }; + let mut reader = Reader::new(&gh, &policy.host_path, repo, &token); let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map_err(|_| "invalid system clock")? From 1271fbc4c093dbff3b83ca96e8c5a697222397af Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:57:45 +0100 Subject: [PATCH 07/24] test(work): reproduce worker reservation racing refill (#1017) --- oxa/oxa-cli/src/work_refill.rs | 11 +- oxa/oxa-cli/tests/work_refill.rs | 234 ++++++++++++++++--------------- 2 files changed, 123 insertions(+), 122 deletions(-) diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs index c3610140..9445bdc7 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -3,12 +3,10 @@ use oxa_work::board::{assignment::Worker, *}; use oxa_work::{Result, helper_queue}; use sha2::{Digest, Sha256}; use std::{collections::BTreeSet, fs, path::Path}; - /// Read-only acquisition and authoritative revision/action revalidation; no write capability. pub trait Source: GitHub { fn revalidate(&mut self, entry: &Entry) -> Result; } - /// Apply the same operator-selected item partition to BOTH PR and issue intake. pub struct Partition { pub modulus: u64, @@ -28,7 +26,6 @@ impl GitHub for Partitioned<'_, S> { Ok(snapshot) } } - /// Existing bounded taskfile bytes, frozen to the exact candidate; never a new packet builder. pub struct Packet<'a> { pub number: u64, @@ -42,8 +39,7 @@ pub struct Receipt { pub lease: Lease, } -/// One authenticated queue target. Default Policy is OFF. The pause marker persists across -/// restarts; only the operator may remove it after qualification. No workers are spawned here. +/// Authenticated queue target; default OFF. The operator-owned pause marker survives restarts. pub struct Consumer<'a> { pub repository: &'a str, pub queue: &'a Path, @@ -63,9 +59,8 @@ impl Consumer<'_> { } } - /// At most one task per call. Any outstanding lease, including an expired lease, keeps - /// this worker busy until fenced completion. Unknown bootstrap rows are never zero counts. - /// Audit must durably record the board before reservation/publication. It receives no packet. + /// At most one task; any lease, even expired, blocks the worker until fenced completion. + /// Unknown counts refuse. Audit durably records the board before reservation, without packets. pub fn once( &self, source: &mut impl Source, diff --git a/oxa/oxa-cli/tests/work_refill.rs b/oxa/oxa-cli/tests/work_refill.rs index de8d41db..ea212e1f 100644 --- a/oxa/oxa-cli/tests/work_refill.rs +++ b/oxa/oxa-cli/tests/work_refill.rs @@ -2,14 +2,14 @@ use oxa_cli::work_refill::{Consumer, Packet, Partition, Source}; use oxa_fake::TestEnv; use oxa_work::board::{assignment::*, *}; use std::{collections::BTreeMap, fs, os::unix::fs::DirBuilderExt}; - #[derive(Default)] -struct Fake { +struct Fake<'a> { reads: usize, checks: usize, stale: bool, + on_check: Option<(&'a Claims, &'a Worker, &'a Policy)>, } -impl GitHub for Fake { +impl GitHub for Fake<'_> { fn snapshot(&mut self) -> oxa_work::Result { self.reads += 1; Ok(Snapshot { @@ -37,100 +37,138 @@ impl GitHub for Fake { }) } } -impl Source for Fake { - fn revalidate(&mut self, _: &Entry) -> oxa_work::Result { +impl Source for Fake<'_> { + fn revalidate(&mut self, entry: &Entry) -> oxa_work::Result { self.checks += 1; + if let Some((claims, worker, policy)) = self.on_check.take() { + let mut other = entry.clone(); + other.number = 46; + assert!(claims.claim_for(&other, policy, worker, 10, 60)?.is_some()); + } Ok(!self.stale) } } const AUTHORITY: Option = Some(ManualAuthority::Owner); const TASK: &str = "i44 local/fake agent/issue-44 main\nSynthetic bounded existing taskfile.\n"; - -fn seed(claims: &Claims, excluded: bool) { - let incoming = BTreeMap::from([( - 44, - ImportState { - state: AttemptState::default(), - excluded, - }, - )]); +const PACKETS: [Packet<'static>; 1] = [Packet { + number: 44, + revision: "revision", + action: Action::BuildIssue, + task: TASK, +}]; +fn seed(claims: &Claims, number: u64, excluded: bool) { + let row = ImportState { + state: AttemptState::default(), + excluded, + }; + let incoming = BTreeMap::from([(number, row)]); claims.import_snapshot(AUTHORITY, &incoming, 10).unwrap(); } fn no_task(target: Consumer<'_>, source: &mut Fake, packets: &[Packet<'_>], now: u64) { assert!(target.once(source, packets, now, |_| Ok(())).unwrap().is_empty()); } +struct Fixture { + _env: TestEnv, + queue: std::path::PathBuf, + pause: std::path::PathBuf, + claims: Claims, + worker: Worker, + partition: Partition, + policy: Policy, +} +impl Fixture { + fn new() -> Self { + let env = TestEnv::new(); + let root = env.tmp().join("claims"); + let queue = env.tmp().join("queue"); + let pause = env.tmp().join("paused"); + for path in [&root, &queue] { + fs::DirBuilder::new().mode(0o700).create(path).unwrap(); + } + Self { + claims: Claims::open(&root.canonicalize().unwrap()).unwrap(), + _env: env, + queue, + pause, + worker: Worker { + id: "oxa:node/helper".into(), + kind: WorkerKind::Oxa, + roles: [Role::Builder].into(), + skills: Default::default(), + }, + partition: Partition { + modulus: 22, + residues: [0].into(), + }, + policy: Policy { + enabled: true, + ..Policy::default() + }, + } + } + fn target(&self) -> Consumer<'_> { + Consumer { + repository: "example/project", + queue: &self.queue, + pause: &self.pause, + claims: &self.claims, + worker: &self.worker, + partition: &self.partition, + policy: &self.policy, + ttl: 60, + } + } +} #[test] fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { - let env = TestEnv::new(); - let root = env.tmp().join("claims"); - let queue = env.tmp().join("queue"); - let pause = env.tmp().join("paused"); - for path in [&root, &queue] { - fs::DirBuilder::new().mode(0o700).create(path).unwrap(); - } - let claims = Claims::open(&root.canonicalize().unwrap()).unwrap(); - seed(&claims, false); - let worker = Worker { - id: "oxa:node/helper".into(), - kind: WorkerKind::Oxa, - roles: [Role::Builder].into(), - skills: Default::default(), - }; - let partition = Partition { - modulus: 22, - residues: [0].into(), - }; + let fixture = Fixture::new(); + let (claims, queue, pause, worker, policy) = ( + &fixture.claims, + &fixture.queue, + &fixture.pause, + &fixture.worker, + &fixture.policy, + ); + seed(claims, 44, false); let off = Policy::default(); - let policy = Policy { - enabled: true, - ..Policy::default() - }; - let packets = [Packet { - number: 44, - revision: "revision", - action: Action::BuildIssue, - task: TASK, - }]; let mut source = Fake::default(); let target = |enabled| Consumer { - repository: "example/project", - queue: &queue, - pause: &pause, - claims: &claims, - worker: &worker, - partition: &partition, - policy: if enabled { &policy } else { &off }, - ttl: 60, + policy: if enabled { policy } else { &off }, + ..fixture.target() }; - assert!( - target(false) - .once(&mut source, &packets, 10, |_| panic!("off audit")) - .unwrap() - .is_empty() - ); - fs::write(&pause, b"paused").unwrap(); - // Recreating the consumer simulates service startup: the persistent marker still wins. - assert!( - target(true) - .once(&mut source, &packets, 10, |_| panic!("paused audit")) - .unwrap() - .is_empty() - ); + for enabled in [false, true] { + if enabled { + fs::write(pause, b"paused").unwrap(); + } + let result = target(enabled).once(&mut source, &PACKETS, 10, |_| panic!("inactive audit")); + assert!(result.unwrap().is_empty()); + } assert_eq!((source.reads, source.checks), (0, 0)); - fs::remove_file(&pause).unwrap(); + fs::remove_file(pause).unwrap(); assert!( target(true) - .once(&mut source, &packets, 10, |_| Err("audit failed".into())) + .once(&mut source, &PACKETS, 10, |_| Err("audit failed".into())) .is_err() ); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); source.stale = true; - no_task(target(true), &mut source, &packets, 10); + no_task(target(true), &mut source, &PACKETS, 10); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); source.stale = false; + seed(claims, 46, false); + source.on_check = Some((claims, worker, policy)); + // Another authenticated reservation lands after the consumer's preliminary idle scan. + no_task(target(true), &mut source, &PACKETS, 10); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + let other_lease = claims.snapshot().unwrap()[&46].lease.clone().unwrap(); + assert!( + claims + .release_for(46, &worker.id, &other_lease, 10, "finished other ref") + .unwrap() + ); let receipts = target(true) - .once(&mut source, &packets, 10, |board| { + .once(&mut source, &PACKETS, 10, |board| { assert_eq!( board.board.entries.iter().map(|e| e.number).collect::>(), [44] @@ -144,7 +182,7 @@ fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { let name = format!("board-{}.txt", receipt.key); assert_eq!(fs::read_to_string(queue.join(&name)).unwrap(), TASK); fs::rename(queue.join(&name), queue.join("started").join(name)).unwrap(); - no_task(target(true), &mut source, &packets, 80); + no_task(target(true), &mut source, &PACKETS, 80); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); assert!( claims @@ -152,69 +190,37 @@ fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { .unwrap() ); // Retained started file dedupes before a second claim, even after fenced release. - no_task(target(true), &mut source, &packets, 81); + no_task(target(true), &mut source, &PACKETS, 81); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); } #[test] fn unknown_held_owner_only_and_reviewer_work_never_publishes() { - let env = TestEnv::new(); - let root = env.tmp().join("claims"); - let queue = env.tmp().join("queue"); - let pause = env.tmp().join("paused"); - for path in [&root, &queue] { - fs::DirBuilder::new().mode(0o700).create(path).unwrap(); - } - let claims = Claims::open(&root.canonicalize().unwrap()).unwrap(); - let policy = Policy { - enabled: true, - ..Policy::default() - }; - let partition = Partition { - modulus: 22, - residues: [0].into(), - }; - let mut worker = Worker { - id: "oxa:node/helper".into(), - kind: WorkerKind::Oxa, - roles: [Role::Builder].into(), - skills: Default::default(), - }; - let packets = [Packet { - number: 44, - revision: "revision", - action: Action::BuildIssue, - task: TASK, - }]; + let mut fixture = Fixture::new(); + let claims = &fixture.claims; let mut source = Fake::default(); for stage in 0..4 { if stage == 1 { - seed(&claims, true); + seed(claims, 44, true); } if stage == 2 { - worker.roles = [Role::Reviewer].into(); + fixture.worker.roles = [Role::Reviewer].into(); let revision = claims.assignments().unwrap()[&44].data.revision; claims.clear_import_hold(44, AUTHORITY, revision).unwrap(); } if stage == 3 { - worker.roles = [Role::Builder].into(); + fixture.worker.roles = [Role::Builder].into(); let revision = claims.assignments().unwrap()[&44].data.revision; claims .control(44, AUTHORITY, revision, Control::OwnerOnly(true)) .unwrap(); } - let target = Consumer { - repository: "example/project", - queue: &queue, - pause: &pause, - claims: &claims, - worker: &worker, - partition: &partition, - policy: &policy, - ttl: 60, - }; - no_task(target, &mut source, &packets, 10); + no_task(fixture.target(), &mut source, &PACKETS, 10); } assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); - assert!(fs::read_dir(queue).unwrap().all(|e| e.unwrap().path().is_dir())); + assert!( + fs::read_dir(&fixture.queue) + .unwrap() + .all(|e| e.unwrap().path().is_dir()) + ); } From 6c0d41823d94c45a18aaca6fbfd5ba82b0b397f3 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:58:02 +0100 Subject: [PATCH 08/24] wip(work): preserve queue-only refill CLI carry (#1017) --- oxa/oxa-cli/src/lead_input.rs | 6 +- oxa/oxa-cli/src/work_board.rs | 4 + oxa/oxa-cli/src/work_board/refill.rs | 285 +++++++++++++++++++++++++++ 3 files changed, 292 insertions(+), 3 deletions(-) create mode 100644 oxa/oxa-cli/src/work_board/refill.rs diff --git a/oxa/oxa-cli/src/lead_input.rs b/oxa/oxa-cli/src/lead_input.rs index 08b2daaa..f8cded4b 100644 --- a/oxa/oxa-cli/src/lead_input.rs +++ b/oxa/oxa-cli/src/lead_input.rs @@ -1,6 +1,6 @@ //! Bounded, read-only access to existing board samples and typed runner situation exports. //! These functions return facts, never raw file content or input-bearing diagnostics. -use oxa_proto::work::{Snapshot, MAX_ITEMS, MAX_SNAPSHOT_BYTES}; +use oxa_proto::work::{MAX_ITEMS, MAX_SNAPSHOT_BYTES, Snapshot}; use oxa_work::board::Board; use std::{ fs::{self, File}, @@ -14,7 +14,7 @@ type Result = std::result::Result; /// The caller supplies the owner's uid from its user configuration, never project metadata. /// Sources must have private parent directories; producers may atomically replace files. -fn open(path: &Path, uid: u32) -> Result { +pub(crate) fn open(path: &Path, uid: u32) -> Result { let parent = path.parent().ok_or("missing input directory")?; let directory = fs::symlink_metadata(parent).map_err(|_| "input directory unavailable")?; if !directory.is_dir() || directory.uid() != uid || directory.mode() & 0o077 != 0 { @@ -89,7 +89,7 @@ pub fn situation(path: &Path, uid: u32) -> Result { mod tests { use super::*; use oxa_fake::TestEnv; - use std::os::unix::fs::{symlink, PermissionsExt}; + use std::os::unix::fs::{PermissionsExt, symlink}; fn fixture(env: &TestEnv, content: &[u8]) -> (std::path::PathBuf, u32) { let path = env.agent_dir().join("inputs/board.jsonl"); diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index 1ae914e2..8c4c45d6 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -1,6 +1,7 @@ //! Explicitly opted-in, read-only GitHub sampling. Never invokes a worker or mutation. use oxa_work::board::{Board, GitHub, Mergeability, Policy, PullRequest, RequiredCi}; use serde_json::Value; +mod refill; use std::{ collections::{BTreeMap, BTreeSet}, io::{BufRead, IsTerminal, Read}, @@ -335,6 +336,9 @@ pub(crate) fn run( dirs: &oxa_config::Dirs, policy: &oxa_work::RunPolicy, ) -> Result { + if args.first().is_some_and(|arg| arg == "--refill") { + return refill::run(args, root, env, dirs, policy); + } // Existing parser declaration; migrate command/flag metadata together in #1062. let (repo, options) = match args { [shadow, flag, repo, options @ ..] if shadow == "--shadow" && flag == "--repo" => (repo, options), diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs new file mode 100644 index 00000000..5f3ef610 --- /dev/null +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -0,0 +1,285 @@ +//! Owner-selected queue frontend. Existing Reader, Claims, publisher and private-file IO only. +use super::*; +use crate::work_refill::{Consumer, Packet, Partition, Source}; +use oxa_work::board::{Action, Entry, Issue, Snapshot, assignment::Worker}; +use std::{fs, os::unix::fs::MetadataExt, path::PathBuf}; + +fn text<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> { + value[key] + .as_str() + .ok_or_else(|| "missing board input field".into()) +} +fn private(path: &Path, uid: u32) -> Result { + let mut bytes = Vec::new(); + crate::lead_input::open(path, uid)? + .take(65_537) + .read_to_end(&mut bytes) + .map_err(|_| "cannot read private board input")?; + if bytes.len() > 65_536 { + return Err("board input exceeds capacity".into()); + } + serde_json::from_slice(&bytes).map_err(|_| "invalid private board input".into()) +} + +struct Prepared { + number: u64, + revision: String, + action: Action, + branch: String, + task: String, +} +struct Input<'a> { + reader: Reader<'a>, + packets: &'a [Prepared], + intake: bool, +} +impl GitHub for Input<'_> { + fn snapshot(&mut self) -> Result { + let pull_requests = self.reader.open_pull_requests()?; + let mut fields = "pullRequests(first:100,states:OPEN){totalCount nodes{number headRefOid headRefName closingIssuesReferences(first:100){totalCount nodes{number}}}}".to_owned(); + for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { + fields.push_str(&format!( + " i{}:issue(number:{}){{number state updatedAt}}", + packet.number, packet.number + )); + } + let (metadata, _) = self.reader.query(&fields).map_err(QueryError::message)?; + let rows = nodes(&metadata["pullRequests"])?; + let heads: BTreeMap<_, _> = rows + .iter() + .map(|p| (p["number"].as_u64(), p["headRefOid"].as_str())) + .collect(); + if heads.len() != pull_requests.len() + || pull_requests + .iter() + .any(|p| heads.get(&Some(p.number)) != Some(&Some(p.head.as_str()))) + { + return Err("board membership or head changed; retry later".into()); + } + let mut closing = BTreeSet::new(); + for pr in rows { + for issue in nodes(&pr["closingIssuesReferences"])? { + closing.insert(issue["number"].as_u64().ok_or("invalid closing issue")?); + } + if let Some(packet) = self + .packets + .iter() + .find(|p| p.number == pr["number"].as_u64().unwrap_or(0)) + { + if text(pr, "headRefName")? != packet.branch { + return Err("prepared PR branch changed".into()); + } + } + } + let mut issues = vec![]; + for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { + let issue = &metadata[format!("i{}", packet.number)]; + if issue["number"].as_u64() != Some(packet.number) { + return Err("prepared issue is missing".into()); + } + issues.push(Issue { + number: packet.number, + revision: text(issue, "updatedAt")?.into(), + release_pinned: false, + approved: self.intake && pull_requests.len() < 40 && text(issue, "state")? == "OPEN", + has_open_pull_request: closing.contains(&packet.number), + }); + } + Ok(Snapshot { + pull_requests, + issues, + }) + } +} +impl Source for Input<'_> { + fn revalidate(&mut self, entry: &Entry) -> Result { + let board = Board::read( + self, + &Policy { + enabled: true, + ..Policy::default() + }, + &BTreeMap::new(), + 0, + )?; + Ok(board.entries.iter().any(|fresh| { + fresh.number == entry.number && fresh.head == entry.head && fresh.action == entry.action + })) + } +} + +pub(super) fn run( + args: &[String], + root: &Path, + env: &oxa_config::Env, + dirs: &oxa_config::Dirs, + policy: &oxa_work::RunPolicy, +) -> Result { + // #1062 owns command declarations; WorkBoard's existing Tail seam retains handler validation. + let [mode, repo_flag, repo, once, token_flag, config_flag, config] = args else { + return Err( + "usage: oxa work board --refill --repo OWNER/REPO --once --token-stdin --config PATH".into(), + ); + }; + if mode != "--refill" + || repo_flag != "--repo" + || once != "--once" + || token_flag != "--token-stdin" + || config_flag != "--config" + { + return Err("invalid board refill options".into()); + } + if repo.split('/').count() != 2 + || repo.split('/').any(|part| { + part.is_empty() + || matches!(part, "." | "..") + || !part + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b)) + }) + { + return Err("expected OWNER/REPO".into()); + } + if policy.offline { + return Err("GitHub board is unavailable in offline mode".into()); + } + if !crate::work_cmd::trusted(root, env, dirs)? { + return Err("source project is not trusted".into()); + } + let config = Path::new(config) + .canonicalize() + .map_err(|_| "board config unavailable")?; + if config.starts_with(root) { + return Err("board config must be outside the source project".into()); + } + let uid = fs::metadata(&dirs.home) + .map_err(|_| "owner home unavailable")? + .uid(); + let value = private(&config, uid)?; + if value["enabled"] != true { + return Ok(0); + } + let owner_path = |key| -> Result { + let path = PathBuf::from(text(&value, key)?); + if !path.is_absolute() || path.starts_with(root) { + return Err("board state must be outside the source project".into()); + } + Ok(path) + }; + let queue = owner_path("queue")?; + let pause = owner_path("pause")?; + match fs::symlink_metadata(&pause) { + Ok(_) => return Ok(0), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => (), + Err(_) => return Err("cannot establish board pause state".into()), + } + let claims = oxa_work::board::Claims::open(&owner_path("claims")?)?; + let worker: Worker = + serde_json::from_value(value["worker"].clone()).map_err(|_| "invalid authenticated worker export")?; + let partition = Partition { + modulus: value["partition"]["modulus"] + .as_u64() + .ok_or("missing partition modulus")?, + residues: serde_json::from_value(value["partition"]["residues"].clone()) + .map_err(|_| "invalid partition residues")?, + }; + let rows = value["packets"] + .as_array() + .filter(|rows| rows.len() <= 20) + .ok_or("expected bounded prepared packets")?; + let mut packets = vec![]; + let mut seen = BTreeSet::new(); + for row in rows { + let number = row["number"] + .as_u64() + .filter(|n| *n > 0 && seen.insert(*n)) + .ok_or("invalid or duplicate prepared ref")?; + let action: Action = + serde_json::from_value(row["action"].clone()).map_err(|_| "invalid prepared action")?; + let path = Path::new(text(row, "taskfile")?); + if !path.is_absolute() || path.starts_with(root) { + return Err("taskfile must be outside source project".into()); + } + let mut task = String::new(); + crate::lead_input::open(path, uid)? + .take(65_537) + .read_to_string(&mut task) + .map_err(|_| "cannot read prepared taskfile")?; + let header: Vec<_> = task + .lines() + .next() + .unwrap_or("") + .split_ascii_whitespace() + .collect(); + let branch = text(row, "branch")?; + if task.len() > 65_536 + || !matches!(header.len(), 3 | 4) + || header[2] != branch + || header.get(3).copied().unwrap_or("main") + != if action == Action::BuildIssue { + "main" + } else { + branch + } + { + return Err("prepared taskfile branch/base mismatch".into()); + } + packets.push(Prepared { + number, + revision: text(row, "revision")?.into(), + action, + branch: branch.into(), + task, + }); + } + let stdin = std::io::stdin(); + if stdin.is_terminal() { + return Err("App token input requires a private pipe, not a terminal".into()); + } + let token = token_line(&mut stdin.lock())?; + let gh = + crate::netguard::trusted_executable_for("gh", Some(&policy.host_path), Some(root), Some(&dirs.home)) + .ok_or("no trusted gh executable")?; + let mut source = Input { + reader: Reader { + gh: &gh, + path: &policy.host_path, + repo, + token: &token, + }, + packets: &packets, + intake: value["intake"] == true, + }; + let now = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .map_err(|_| "invalid system clock")? + .as_secs(); + let policy = Policy { + enabled: true, + ..Policy::default() + }; + let consumer = Consumer { + repository: repo, + queue: &queue, + pause: &pause, + claims: &claims, + worker: &worker, + partition: &partition, + policy: &policy, + ttl: 1800, + }; + let inputs: Vec<_> = packets + .iter() + .map(|p| Packet { + number: p.number, + revision: &p.revision, + action: p.action, + task: &p.task, + }) + .collect(); + let mut log = oxa_work::shadow::open_log(&owner_path("log")?)?; + consumer.once(&mut source, &inputs, now, |board| { + oxa_work::shadow::run(&mut log, true, || Ok(board.board.clone()), |_| {}) + })?; + Ok(0) +} From 6e3f801c30a53d6ac883ce3d0af8d68f4fdd9426 Mon Sep 17 00:00:00 2001 From: oxa-agent Date: Tue, 17 Nov 2026 20:58:45 +0100 Subject: [PATCH 09/24] feat(work): reserve idle helper claims under the shared lock (#1017) --- oxa/oxa-work/src/board/claims.rs | 5 ++ oxa/oxa-work/src/board/claims/controls.rs | 29 +++++++ oxa/oxa-work/tests/board_controls.rs | 94 +++++++++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/oxa/oxa-work/src/board/claims.rs b/oxa/oxa-work/src/board/claims.rs index 686395df..99a9ab30 100644 --- a/oxa/oxa-work/src/board/claims.rs +++ b/oxa/oxa-work/src/board/claims.rs @@ -160,6 +160,11 @@ impl Claims { Self::open(&self.root)?; let lock = io(File::open(&self.root))?; io(lock.lock())?; + self.rows_locked() + } + + /// Caller holds the directory lock used by every reservation and control update. + fn rows_locked(&self) -> Result> { let mut states = BTreeMap::new(); for entry in io(fs::read_dir(&self.root))? { let name = io(entry)?.file_name(); diff --git a/oxa/oxa-work/src/board/claims/controls.rs b/oxa/oxa-work/src/board/claims/controls.rs index eda2a454..1ad3802a 100644 --- a/oxa/oxa-work/src/board/claims/controls.rs +++ b/oxa/oxa-work/src/board/claims/controls.rs @@ -163,4 +163,33 @@ impl Claims { Ok(controls.allows(worker, entry.action)) }) } + + /// Helper admission checks idleness and reserves under the SAME directory lock. + /// Every outstanding target/worker lease, including expired work, requires fenced completion. + /// Worker identity and roles must come from the trusted frontend, as with `claim_for`. + /// Generic manual reservations retain their separate `assign`/`claim_for` semantics. + pub fn claim_idle_for( + &self, + entry: &Entry, + policy: &Policy, + worker: &Worker, + now: u64, + ttl: u64, + ) -> Result> { + self.claim_checked(entry, policy, &worker.id, (now, ttl), |row| { + if row.state.lease.is_some() + || self.rows_locked()?.values().any(|saved| { + saved + .state + .lease + .as_ref() + .is_some_and(|lease| lease.holder == worker.id) + }) + { + return Ok(false); + } + let controls = row.data.policy.get_or_insert_with(AssignmentPolicy::default); + Ok(controls.allows(worker, entry.action)) + }) + } } diff --git a/oxa/oxa-work/tests/board_controls.rs b/oxa/oxa-work/tests/board_controls.rs index 0c52d6a7..6266756e 100644 --- a/oxa/oxa-work/tests/board_controls.rs +++ b/oxa/oxa-work/tests/board_controls.rs @@ -216,6 +216,10 @@ fn typed_issue_intake_uses_one_snapshot_and_builder_authority() { worker.roles = [Role::Reviewer].into(); let denied = store.claim_for(issue, &policy(), &worker, 10, 10).unwrap(); assert!(denied.is_none()); + assert!(store + .claim_idle_for(issue, &policy(), &worker, 10, 10) + .unwrap() + .is_none()); let legacy = store.claim(issue, &policy(), &worker.id, 10, 10).unwrap(); assert!(legacy.is_none()); worker.roles = [Role::Builder].into(); @@ -294,6 +298,10 @@ fn import_resumes_without_resetting_attempts_or_admitting_preserved_jobs() { .claim_for(&issue, &policy(), &worker, 21, 10) .unwrap() .is_none()); + assert!(store + .claim_idle_for(&issue, &policy(), &worker, 21, 10) + .unwrap() + .is_none()); assert!(store .claim(&entry(), &policy(), &worker.id, 21, 10) .unwrap() @@ -528,3 +536,89 @@ fn records_without_release_metadata_remain_readable() { assert_eq!(rows[&3].state.attempts, 1); assert!(rows[&3].data.last_release.is_none()); } + +#[test] +fn idle_claims_preserve_expired_work_until_fenced_completion() { + let fixture = Fixture::new(); + let store = fixture.store(); + let worker = worker("slot-1"); + let first = entry(); + let mut next = first.clone(); + next.number = 2; + assert!(store + .claim_idle_for(&next, &Policy::default(), &worker, 10, 10) + .unwrap() + .is_none()); + assert_eq!(fs::read_dir(&fixture.0).unwrap().count(), 0); + let lease = store + .claim_for(&first, &policy(), &worker, 10, 10) + .unwrap() + .unwrap(); + // Expiry is not evidence that the existing helper actually finished. + assert!(store + .claim_idle_for(&next, &policy(), &worker, 20, 10) + .unwrap() + .is_none()); + assert_eq!(store.snapshot().unwrap()[&2].attempts, 0); + let other = crate::worker("slot-2"); + assert!(store + .claim_idle_for(&first, &policy(), &other, 20, 10) + .unwrap() + .is_none()); + let mut independent = first.clone(); + independent.number = 1; + assert!(store + .claim_idle_for(&independent, &policy(), &other, 20, 10) + .unwrap() + .is_some()); + assert!(store + .release_for(3, &worker.id, &lease, 20, "verified completion") + .unwrap()); + let next_lease = store + .claim_idle_for(&next, &policy(), &worker, 20, 10) + .unwrap() + .unwrap(); + let rows = fixture.store().assignments().unwrap(); + assert_eq!(rows[&3].state.attempts, 1); + assert_eq!(rows[&2].state.attempts, 1); + assert_eq!(rows[&2].state.lease, Some(next_lease)); + assert!(rows[&3].state.lease.is_none()); + // Losing another worker's established state must fail the complete idle check closed. + fs::remove_file(fixture.0.join("1.board-claim.json")).unwrap(); + next.number = 6; + assert!(store + .claim_idle_for(&next, &policy(), &crate::worker("slot-3"), 21, 10) + .is_err()); + assert!(!fixture.0.join("6.board-claim.json").exists()); +} + +#[test] +fn concurrent_idle_reservations_share_one_worker_fence_across_refs() { + let fixture = Fixture::new(); + let barrier = std::sync::Barrier::new(6); + let winners = std::thread::scope(|scope| { + let mut pending = vec![]; + for number in 1..=6 { + let store = fixture.store(); + let worker = worker("shared-slot"); + let mut candidate = entry(); + candidate.number = number; + let barrier = &barrier; + pending.push(scope.spawn(move || { + barrier.wait(); + store + .claim_idle_for(&candidate, &policy(), &worker, 10, 10) + .unwrap() + })); + } + pending + .into_iter() + .map(|thread| thread.join().unwrap()) + .filter(Option::is_some) + .count() + }); + assert_eq!(winners, 1); + let rows = fixture.store().assignments().unwrap(); + assert_eq!(rows.values().map(|row| row.state.attempts).sum::(), 1); + assert_eq!(rows.values().filter(|row| row.state.lease.is_some()).count(), 1); +} From bb76c0dbcd8411dd25dfec638c65ec316956cbab Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:01:06 +0100 Subject: [PATCH 10/24] test(work): drive refill startup and control-file authority (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 oxa/oxa-cli/tests/work_refill_cli.rs diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs new file mode 100644 index 00000000..edcdbd97 --- /dev/null +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -0,0 +1,92 @@ +use oxa_fake::TestEnv; +use serde_json::json; +use std::{ + fs, + io::Write, + os::unix::fs::PermissionsExt, + path::Path, + process::{Command, Stdio}, +}; + +fn invoke(env: &TestEnv, config: &Path, token: &str, offline: bool) -> std::process::Output { + let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); + command + .current_dir(env.work()) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")); + if offline { + command.arg("--offline"); + } + command + .args([ + "work", + "board", + "--refill", + "--repo", + "example/project", + "--once", + "--token-stdin", + "--config", + ]) + .arg(config) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + let mut child = command.spawn().unwrap(); + child.stdin.take().unwrap().write_all(token.as_bytes()).unwrap(); + child.wait_with_output().unwrap() +} + +#[test] +fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { + let env = TestEnv::new(); + assert!( + Command::new("git") + .args(["init", "-b", "main"]) + .current_dir(env.work()) + .output() + .unwrap() + .status + .success() + ); + oxa_perm::TrustStore::default() + .save(&env.agent_dir().join("trust.json"), &env.work(), true) + .unwrap(); + let controls = env.agent_dir().join("board-control"); + fs::create_dir(&controls).unwrap(); + fs::set_permissions(&controls, fs::Permissions::from_mode(0o700)).unwrap(); + fs::set_permissions(env.tmp(), fs::Permissions::from_mode(0o700)).unwrap(); + let config = controls.join("refill.json"); + let pause = controls.join("paused"); + let queue = controls.join("queue"); + let claims = controls.join("claims"); + for path in [&queue, &claims] { + fs::create_dir(path).unwrap(); + fs::set_permissions(path, fs::Permissions::from_mode(0o700)).unwrap(); + } + let sentinel = claims.join("preserved"); + fs::write(&sentinel, b"synthetic imported attempts and holds").unwrap(); + let write_config = |value: serde_json::Value| { + fs::write(&config, value.to_string()).unwrap(); + fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); + }; + write_config(json!({"enabled":false})); + assert!(invoke(&env, &config, "", false).status.success()); + write_config(json!({"enabled":true,"queue":queue,"pause":pause,"claims":claims})); + fs::write(&pause, b"owner paused").unwrap(); + // Separate real processes simulate a service starting again while the pause persists. + for _ in 0..2 { + assert!(invoke(&env, &config, "", false).status.success()); + } + assert_eq!( + fs::read(&sentinel).unwrap(), + b"synthetic imported attempts and holds" + ); + assert_eq!(fs::read_dir(&claims).unwrap().count(), 1); + assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); + assert_eq!(invoke(&env, &config, "", true).status.code(), Some(2)); + let outside = env.tmp().join("untrusted-control.json"); + fs::write(&outside, b"{\"enabled\":false}").unwrap(); + fs::set_permissions(&outside, fs::Permissions::from_mode(0o600)).unwrap(); + assert_eq!(invoke(&env, &outside, "", false).status.code(), Some(2)); +} From c8d62f5547f6d7950b7e6365ee5a88d46a103330 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:04:26 +0100 Subject: [PATCH 11/24] fix(work): reserve an idle helper under the shared claim lock (#1017) --- oxa/oxa-cli/src/work_refill.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs index 9445bdc7..91d1bebb 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -137,7 +137,7 @@ impl Consumer<'_> { } lease = self .claims - .claim_for(entry, self.policy, self.worker, now, self.ttl)?; + .claim_idle_for(entry, self.policy, self.worker, now, self.ttl)?; Ok(lease.is_some()) }, )?; From 812c0890b399e68450a50d025db793e50244edec Mon Sep 17 00:00:00 2001 From: oxa-agent Date: Tue, 17 Nov 2026 21:04:27 +0100 Subject: [PATCH 12/24] test(work): reject transient markers as helper bootstrap state (#1017) --- oxa/oxa-work/tests/board_controls.rs | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/oxa/oxa-work/tests/board_controls.rs b/oxa/oxa-work/tests/board_controls.rs index 6266756e..02734479 100644 --- a/oxa/oxa-work/tests/board_controls.rs +++ b/oxa/oxa-work/tests/board_controls.rs @@ -18,6 +18,23 @@ impl Fixture { fn store(&self) -> Claims { Claims::open(&self.0).unwrap() } + fn establish(&self, numbers: impl IntoIterator) { + let incoming = numbers + .into_iter() + .map(|number| { + ( + number, + ImportState { + state: AttemptState::default(), + excluded: false, + }, + ) + }) + .collect(); + self.store() + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 10) + .unwrap(); + } } impl Drop for Fixture { fn drop(&mut self) { @@ -550,6 +567,7 @@ fn idle_claims_preserve_expired_work_until_fenced_completion() { .unwrap() .is_none()); assert_eq!(fs::read_dir(&fixture.0).unwrap().count(), 0); + fixture.establish([1, 2, 6]); let lease = store .claim_for(&first, &policy(), &worker, 10, 10) .unwrap() @@ -595,6 +613,7 @@ fn idle_claims_preserve_expired_work_until_fenced_completion() { #[test] fn concurrent_idle_reservations_share_one_worker_fence_across_refs() { let fixture = Fixture::new(); + fixture.establish(1..=6); let barrier = std::sync::Barrier::new(6); let winners = std::thread::scope(|scope| { let mut pending = vec![]; @@ -622,3 +641,34 @@ fn concurrent_idle_reservations_share_one_worker_fence_across_refs() { assert_eq!(rows.values().map(|row| row.state.attempts).sum::(), 1); assert_eq!(rows.values().filter(|row| row.state.lease.is_some()).count(), 1); } + +#[test] +fn idle_claims_require_saved_state_not_a_denied_claim_marker() { + let fixture = Fixture::new(); + let store = fixture.store(); + let mut candidate = entry(); + candidate.number = 44; + candidate.action = Action::BuildIssue; + let builder = worker("slot-1"); + let reviewer = Worker { + roles: [Role::Reviewer].into(), + ..builder.clone() + }; + assert!(store + .claim_for(&candidate, &policy(), &reviewer, 10, 10) + .unwrap() + .is_none()); + assert!(fixture.0.join("44.board-claim.lock").exists()); + assert!(!fixture.0.join("44.board-claim.json").exists()); + assert!(store + .claim_idle_for(&candidate, &policy(), &builder, 10, 10) + .unwrap() + .is_none()); + assert!(!fixture.0.join("44.board-claim.json").exists()); + fixture.establish([44]); + assert!(store + .claim_idle_for(&candidate, &policy(), &builder, 10, 10) + .unwrap() + .is_some()); + assert_eq!(fixture.store().snapshot().unwrap()[&44].attempts, 1); +} From 39a50edb6438b741f71b516c90ddb2666c12deba Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:09:21 +0100 Subject: [PATCH 13/24] fix(work): bind refill authority to protected owner controls (#1017) --- oxa/oxa-cli/src/work_board/refill.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 5f3ef610..805cb55b 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -43,7 +43,7 @@ impl GitHub for Input<'_> { packet.number, packet.number )); } - let (metadata, _) = self.reader.query(&fields).map_err(QueryError::message)?; + let (metadata, _) = self.reader.repository_read(&fields)?; let rows = nodes(&metadata["pullRequests"])?; let heads: BTreeMap<_, _> = rows .iter() @@ -149,8 +149,12 @@ pub(super) fn run( let config = Path::new(config) .canonicalize() .map_err(|_| "board config unavailable")?; - if config.starts_with(root) { - return Err("board config must be outside the source project".into()); + let authority = dirs + .agent_dir + .canonicalize() + .map_err(|_| "owner controls unavailable")?; + if config.starts_with(root) || !config.starts_with(&authority) { + return Err("board config must be in protected owner controls outside the source project".into()); } let uid = fs::metadata(&dirs.home) .map_err(|_| "owner home unavailable")? @@ -196,12 +200,14 @@ pub(super) fn run( .ok_or("invalid or duplicate prepared ref")?; let action: Action = serde_json::from_value(row["action"].clone()).map_err(|_| "invalid prepared action")?; - let path = Path::new(text(row, "taskfile")?); - if !path.is_absolute() || path.starts_with(root) { - return Err("taskfile must be outside source project".into()); + let path = Path::new(text(row, "taskfile")?) + .canonicalize() + .map_err(|_| "taskfile unavailable")?; + if path.starts_with(root) || !path.starts_with(&authority) { + return Err("taskfile must be in protected owner controls outside source project".into()); } let mut task = String::new(); - crate::lead_input::open(path, uid)? + crate::lead_input::open(&path, uid)? .take(65_537) .read_to_string(&mut task) .map_err(|_| "cannot read prepared taskfile")?; @@ -241,12 +247,7 @@ pub(super) fn run( crate::netguard::trusted_executable_for("gh", Some(&policy.host_path), Some(root), Some(&dirs.home)) .ok_or("no trusted gh executable")?; let mut source = Input { - reader: Reader { - gh: &gh, - path: &policy.host_path, - repo, - token: &token, - }, + reader: Reader::new(&gh, &policy.host_path, repo, &token), packets: &packets, intake: value["intake"] == true, }; From e4ef6897dfcdf4a0b436e47e7d312559c77cc5b2 Mon Sep 17 00:00:00 2001 From: oxa-agent Date: Tue, 17 Nov 2026 21:13:30 +0100 Subject: [PATCH 14/24] fix(work): require saved bootstrap state for idle helper admission (#1017) --- oxa/oxa-work/src/board/claims/controls.rs | 4 +++- oxa/oxa-work/tests/board_controls.rs | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/oxa/oxa-work/src/board/claims/controls.rs b/oxa/oxa-work/src/board/claims/controls.rs index 1ad3802a..bf15856e 100644 --- a/oxa/oxa-work/src/board/claims/controls.rs +++ b/oxa/oxa-work/src/board/claims/controls.rs @@ -165,6 +165,7 @@ impl Claims { } /// Helper admission checks idleness and reserves under the SAME directory lock. + /// The target must have validated Saved state; transient lock markers are not bootstrap facts. /// Every outstanding target/worker lease, including expired work, requires fenced completion. /// Worker identity and roles must come from the trusted frontend, as with `claim_for`. /// Generic manual reservations retain their separate `assign`/`claim_for` semantics. @@ -177,7 +178,8 @@ impl Claims { ttl: u64, ) -> Result> { self.claim_checked(entry, policy, &worker.id, (now, ttl), |row| { - if row.state.lease.is_some() + if !self.established(entry.number)? + || row.state.lease.is_some() || self.rows_locked()?.values().any(|saved| { saved .state diff --git a/oxa/oxa-work/tests/board_controls.rs b/oxa/oxa-work/tests/board_controls.rs index 02734479..ed1d10c3 100644 --- a/oxa/oxa-work/tests/board_controls.rs +++ b/oxa/oxa-work/tests/board_controls.rs @@ -604,10 +604,11 @@ fn idle_claims_preserve_expired_work_until_fenced_completion() { // Losing another worker's established state must fail the complete idle check closed. fs::remove_file(fixture.0.join("1.board-claim.json")).unwrap(); next.number = 6; + let untouched = fs::read(fixture.0.join("6.board-claim.json")).unwrap(); assert!(store .claim_idle_for(&next, &policy(), &crate::worker("slot-3"), 21, 10) .is_err()); - assert!(!fixture.0.join("6.board-claim.json").exists()); + assert_eq!(fs::read(fixture.0.join("6.board-claim.json")).unwrap(), untouched); } #[test] @@ -666,6 +667,11 @@ fn idle_claims_require_saved_state_not_a_denied_claim_marker() { .is_none()); assert!(!fixture.0.join("44.board-claim.json").exists()); fixture.establish([44]); + assert!(store + .claim_idle_for(&candidate, &policy(), &reviewer, 10, 10) + .unwrap() + .is_none()); + assert_eq!(store.snapshot().unwrap()[&44].attempts, 0); assert!(store .claim_idle_for(&candidate, &policy(), &builder, 10, 10) .unwrap() From 5be12e834cfbc18c30d5be417151d52be34528ee Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:21:33 +0100 Subject: [PATCH 15/24] test(work): extract prepared input and reproduce partial metadata (#1017) --- oxa/oxa-cli/src/lib.rs | 1 + oxa/oxa-cli/src/work_board.rs | 1 + oxa/oxa-cli/src/work_board/refill_input.rs | 113 +++++++++++++++++++ oxa/oxa-cli/tests/work_refill_input.rs | 125 +++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 oxa/oxa-cli/src/work_board/refill_input.rs create mode 100644 oxa/oxa-cli/tests/work_refill_input.rs diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index c9be6cac..72b734ba 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -44,6 +44,7 @@ mod status; mod themes; mod wipe_cmd; mod work_board; +pub use work_board::refill_input; mod work_checks; mod work_cmd; pub mod work_refill; diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index abfa1ed5..bba7485f 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -1,4 +1,5 @@ //! Explicitly opted-in, read-only GitHub sampling. Never invokes a worker or mutation. +pub mod refill_input; use oxa_work::board::{Board, GitHub, Mergeability, Policy, PullRequest, RequiredCi}; use serde_json::Value; use std::{ diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs new file mode 100644 index 00000000..748ea6c5 --- /dev/null +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -0,0 +1,113 @@ +//! Read-only prepared-input normalization through the existing board Reader. +use super::*; +use crate::work_refill::Source; +use oxa_work::board::{Action, Entry, Issue, Snapshot}; +pub(super) fn text<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> { + value[key] + .as_str() + .ok_or_else(|| "missing board input field".into()) +} + +pub struct Prepared { + pub number: u64, + pub revision: String, + pub action: Action, + pub branch: String, + pub task: String, +} +pub struct Input<'a> { + reader: Reader<'a>, + packets: &'a [Prepared], + intake: bool, +} +impl<'a> Input<'a> { + /// Caller authorizes the source, trusted executable and App token before acquisition. + pub fn new( + gh: &'a Path, + path: &'a std::ffi::OsStr, + repo: &'a str, + token: &'a str, + packets: &'a [Prepared], + intake: bool, + ) -> Self { + Self { + reader: Reader::new(gh, path, repo, token), + packets, + intake, + } + } +} +impl GitHub for Input<'_> { + fn snapshot(&mut self) -> Result { + let pull_requests = self.reader.open_pull_requests()?; + let mut fields = "pullRequests(first:100,states:OPEN){totalCount nodes{number headRefOid headRefName closingIssuesReferences(first:100){totalCount nodes{number}}}}".to_owned(); + for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { + fields.push_str(&format!( + " i{}:issue(number:{}){{number state updatedAt}}", + packet.number, packet.number + )); + } + let (metadata, _) = self.reader.repository_read(&fields)?; + let rows = nodes(&metadata["pullRequests"])?; + let heads: BTreeMap<_, _> = rows + .iter() + .map(|p| (p["number"].as_u64(), p["headRefOid"].as_str())) + .collect(); + if heads.len() != pull_requests.len() + || pull_requests + .iter() + .any(|p| heads.get(&Some(p.number)) != Some(&Some(p.head.as_str()))) + { + return Err("board membership or head changed; retry later".into()); + } + let mut closing = BTreeSet::new(); + for pr in rows { + for issue in nodes(&pr["closingIssuesReferences"])? { + closing.insert(issue["number"].as_u64().ok_or("invalid closing issue")?); + } + if let Some(packet) = self + .packets + .iter() + .find(|p| p.number == pr["number"].as_u64().unwrap_or(0)) + { + if text(pr, "headRefName")? != packet.branch { + return Err("prepared PR branch changed".into()); + } + } + } + let mut issues = vec![]; + for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { + let issue = &metadata[format!("i{}", packet.number)]; + if issue["number"].as_u64() != Some(packet.number) { + return Err("prepared issue is missing".into()); + } + issues.push(Issue { + number: packet.number, + revision: text(issue, "updatedAt")?.into(), + release_pinned: false, + approved: self.intake && pull_requests.len() < 40 && text(issue, "state")? == "OPEN", + has_open_pull_request: closing.contains(&packet.number), + }); + } + Ok(Snapshot { + pull_requests, + issues, + }) + } +} +impl Source for Input<'_> { + fn revalidate(&mut self, entry: &Entry) -> Result { + let board = Board::read( + self, + &Policy { + enabled: true, + ..Policy::default() + }, + &BTreeMap::new(), + 0, + )?; + Ok(board.entries.iter().any(|fresh| { + fresh.number == entry.number && fresh.head == entry.head && fresh.action == entry.action + })) + } +} diff --git a/oxa/oxa-cli/tests/work_refill_input.rs b/oxa/oxa-cli/tests/work_refill_input.rs new file mode 100644 index 00000000..0213117c --- /dev/null +++ b/oxa/oxa-cli/tests/work_refill_input.rs @@ -0,0 +1,125 @@ +use oxa_cli::{ + refill_input::{Input, Prepared}, + work_refill::Source, +}; +use oxa_fake::TestEnv; +use oxa_work::board::{Action, Board, GitHub, Policy}; +use serde_json::{Value, json}; +use std::{collections::BTreeMap, fs, os::unix::fs::PermissionsExt}; + +fn write(env: &TestEnv, name: &str, value: &Value) { + fs::write(env.tmp().join(name), value.to_string()).unwrap(); +} + +fn fixture(count: u64, linked: bool) -> (TestEnv, Vec) { + let env = TestEnv::new(); + fs::set_permissions(env.tmp(), fs::Permissions::from_mode(0o700)).unwrap(); + let gh = env.write( + "tmp/gh", + r#"#!/bin/sh +[ "$GH_TOKEN" = synthetic ] || exit 93 +printf 'HTTP/2.0 200 OK\r\n\r\n' +case "$*" in + *headRefName*) case "$*" in *after:*) cat "$0.second";; *) cat "$0.first";; esac ;; + *':pullRequest('*) cat "$0.details" ;; + *) cat "$0.list" ;; +esac +"#, + ); + fs::set_permissions(gh, fs::Permissions::from_mode(0o700)).unwrap(); + let list: Vec<_> = (100..100 + count) + .map(|n| json!({"number":n,"headRefOid":format!("head-{n}")})) + .collect(); + let mut repository = json!({"mergeQueue":null,"pullRequests":{ + "totalCount":count,"nodes":list,"pageInfo":{"hasNextPage":false,"endCursor":"last"}}}); + write(&env, "gh.list", &json!({"data":{"repository":repository}})); + for n in 100..100 + count { + repository[format!("p{n}")] = json!({"number":n,"headRefOid":format!("head-{n}"), + "baseRefName":"main","state":"OPEN","body":"","isDraft":false,"reviewDecision":null, + "mergeable":"MERGEABLE","mergeStateStatus":"CLEAN", + "baseRef":{"refUpdateRule":null,"rules":{"totalCount":0,"nodes":[]}}, + "labels":{"totalCount":0,"nodes":[]},"files":{"totalCount":0,"nodes":[]}, + "reviewThreads":{"totalCount":0,"nodes":[]},"latestOpinionatedReviews":{"totalCount":0,"nodes":[]}, + "commits":{"nodes":[{"commit":{"statusCheckRollup":null}}]}}); + } + write(&env, "gh.details", &json!({"data":{"repository":repository}})); + let metadata: Vec<_> = (100..100 + count) + .map(|n| { + json!({"number":n, + "headRefOid":format!("head-{n}"),"headRefName":format!("agent/{n}"), + "closingIssuesReferences":if linked && n == 99+count { + json!({"totalCount":1,"nodes":[{"number":44}]}) + } else { json!({"totalCount":0,"nodes":[]}) }}) + }) + .collect(); + let split = metadata.len() / 2; + for (name, rows, more) in [ + ("gh.first", &metadata[..split], true), + ("gh.second", &metadata[split..], false), + ] { + write( + &env, + name, + &json!({"data":{"repository":{ + "pullRequests":{"totalCount":count,"nodes":rows,"pageInfo":{"hasNextPage":more,"endCursor":"next"}}, + "i44":{"number":44,"state":"OPEN","updatedAt":"revision"}}}}), + ); + } + let packets = vec![Prepared { + number: 44, + revision: "revision".into(), + action: Action::BuildIssue, + branch: "agent/44".into(), + task: "i44 local/fake agent/44 main\nSynthetic packet.\n".into(), + }]; + (env, packets) +} + +#[test] +fn all_metadata_pages_preserve_linked_issue_refusal_and_explicit_intake() { + let (env, packets) = fixture(2, true); + let gh = env.tmp().join("gh"); + let path = std::env::var_os("PATH").unwrap(); + let mut source = Input::new(&gh, &path, "example/project", "synthetic", &packets, true); + let snapshot = source.snapshot().unwrap(); + assert_eq!(snapshot.pull_requests.len(), 2); + assert!(snapshot.issues[0].approved); + assert!(snapshot.issues[0].has_open_pull_request); + let mut off = Input::new(&gh, &path, "example/project", "synthetic", &packets, false); + assert!(!off.snapshot().unwrap().issues[0].approved); + let mut second: Value = serde_json::from_slice(&fs::read(env.tmp().join("gh.second")).unwrap()).unwrap(); + second["data"]["repository"]["pullRequests"]["nodes"][0]["headRefOid"] = "changed-head".into(); + write(&env, "gh.second", &second); + assert!(source.snapshot().is_err()); +} + +#[test] +fn fresh_revision_and_global_open_pr_cap_gate_issue_admission() { + let (env, packets) = fixture(2, false); + let gh = env.tmp().join("gh"); + let path = std::env::var_os("PATH").unwrap(); + let mut source = Input::new(&gh, &path, "example/project", "synthetic", &packets, true); + let board = Board::read( + &mut source, + &Policy { + enabled: true, + ..Policy::default() + }, + &BTreeMap::new(), + 10, + ) + .unwrap(); + let entry = board.entries.iter().find(|row| row.number == 44).unwrap(); + assert_eq!(entry.action, Action::BuildIssue); + assert!(source.revalidate(entry).unwrap()); + let mut first: Value = serde_json::from_slice(&fs::read(env.tmp().join("gh.first")).unwrap()).unwrap(); + first["data"]["repository"]["i44"]["updatedAt"] = "changed-revision".into(); + write(&env, "gh.first", &first); + assert!(!source.revalidate(entry).unwrap()); + let (capped, packets) = fixture(40, false); + let gh = capped.tmp().join("gh"); + let mut source = Input::new(&gh, &path, "example/project", "synthetic", &packets, true); + let snapshot = source.snapshot().unwrap(); + assert_eq!(snapshot.pull_requests.len(), 40); + assert!(!snapshot.issues[0].approved); +} From 2ce523240e18ce0eccd9ce2644a35541abf01b40 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:24:54 +0100 Subject: [PATCH 16/24] test(work): drive token failure and exact-ref queue publication (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 100 ++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index edcdbd97..59fcf2dd 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -13,7 +13,11 @@ fn invoke(env: &TestEnv, config: &Path, token: &str, offline: bool) -> std::proc command .current_dir(env.work()) .env_clear() - .envs(env.envs("http://127.0.0.1:1")); + .envs(env.envs("http://127.0.0.1:1")) + .env( + "PATH", + format!("{}:{}", env.tmp().display(), std::env::var("PATH").unwrap()), + ); if offline { command.arg("--offline"); } @@ -37,8 +41,7 @@ fn invoke(env: &TestEnv, config: &Path, token: &str, offline: bool) -> std::proc child.wait_with_output().unwrap() } -#[test] -fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { +fn fixture() -> (TestEnv, std::path::PathBuf) { let env = TestEnv::new(); assert!( Command::new("git") @@ -56,6 +59,12 @@ fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { fs::create_dir(&controls).unwrap(); fs::set_permissions(&controls, fs::Permissions::from_mode(0o700)).unwrap(); fs::set_permissions(env.tmp(), fs::Permissions::from_mode(0o700)).unwrap(); + (env, controls) +} + +#[test] +fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { + let (env, controls) = fixture(); let config = controls.join("refill.json"); let pause = controls.join("paused"); let queue = controls.join("queue"); @@ -90,3 +99,88 @@ fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { fs::set_permissions(&outside, fs::Permissions::from_mode(0o600)).unwrap(); assert_eq!(invoke(&env, &outside, "", false).status.code(), Some(2)); } + +#[test] +fn private_token_and_exact_task_ref_precede_one_durable_publication() { + use oxa_work::board::{AttemptState, Claims, ImportState, assignment::ManualAuthority}; + use std::collections::BTreeMap; + let (env, controls) = fixture(); + let queue = controls.join("queue"); + let root = controls.join("claims"); + for path in [&queue, &root] { + fs::create_dir(path).unwrap(); + fs::set_permissions(path, fs::Permissions::from_mode(0o700)).unwrap(); + } + let claims = Claims::open(&root).unwrap(); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + claims + .import_snapshot( + Some(ManualAuthority::Owner), + &BTreeMap::from([( + 44, + ImportState { + state: AttemptState::default(), + excluded: false, + }, + )]), + now, + ) + .unwrap(); + let task = controls.join("task.txt"); + fs::write( + &task, + b"i46 local/fake agent/44 main\nSynthetic prepared packet.\n", + ) + .unwrap(); + fs::set_permissions(&task, fs::Permissions::from_mode(0o600)).unwrap(); + let config = controls.join("refill.json"); + fs::write(&config, json!({"enabled":true,"intake":true,"queue":queue,"claims":root, + "pause":controls.join("paused"),"log":controls.join("audit.jsonl"), + "worker":{"id":"oxa:node/helper","kind":"oxa","roles":["builder"],"skills":[]}, + "partition":{"modulus":22,"residues":[0]}, + "packets":[{"number":44,"revision":"revision","action":"build_issue","branch":"agent/44","taskfile":task}] + }).to_string()).unwrap(); + fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); + let gh = env.write( + "tmp/gh", + r#"#!/bin/sh +[ "$GH_TOKEN" = synthetic ] || exit 93 +printf 'read\n' >> "$0.calls" +printf 'HTTP/2.0 200 OK\r\n\r\n' +cat "$0.reply" +"#, + ); + fs::set_permissions(&gh, fs::Permissions::from_mode(0o700)).unwrap(); + fs::write(env.tmp().join("gh.reply"), json!({"data":{"repository":{ + "mergeQueue":null,"pullRequests":{"totalCount":0,"nodes":[],"pageInfo":{"hasNextPage":false,"endCursor":"last"}}, + "i44":{"number":44,"state":"OPEN","updatedAt":"revision"} + }}}).to_string()).unwrap(); + let missing = invoke(&env, &config, "", false); + assert_eq!(missing.status.code(), Some(2)); + assert!(!env.tmp().join("gh.calls").exists()); + let mismatch = invoke(&env, &config, "synthetic\n", false); + assert_eq!(mismatch.status.code(), Some(2)); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); + let valid = "i44 local/fake agent/44 main\nSynthetic prepared packet.\n"; + fs::write(&task, valid).unwrap(); + let result = invoke(&env, &config, "synthetic\n", false); + assert!( + result.status.success(), + "{}", + String::from_utf8_lossy(&result.stderr) + ); + let tasks: Vec<_> = fs::read_dir(&queue) + .unwrap() + .map(|entry| entry.unwrap().path()) + .filter(|path| path.is_file()) + .collect(); + assert_eq!(tasks.len(), 1); + assert_eq!(fs::read_to_string(&tasks[0]).unwrap(), valid); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); + assert!(invoke(&env, &config, "synthetic\n", false).status.success()); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); +} From ad55c29be35d1db5a4d7f841909a19d61b81d959 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:26:02 +0100 Subject: [PATCH 17/24] test(work): isolate stdin token failure from packet validation (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index 59fcf2dd..6143a729 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -14,6 +14,7 @@ fn invoke(env: &TestEnv, config: &Path, token: &str, offline: bool) -> std::proc .current_dir(env.work()) .env_clear() .envs(env.envs("http://127.0.0.1:1")) + .env("GH_TOKEN", "synthetic-cached") .env( "PATH", format!("{}:{}", env.tmp().display(), std::env::var("PATH").unwrap()), @@ -132,7 +133,7 @@ fn private_token_and_exact_task_ref_precede_one_durable_publication() { let task = controls.join("task.txt"); fs::write( &task, - b"i46 local/fake agent/44 main\nSynthetic prepared packet.\n", + b"i44 local/fake agent/44 main\nSynthetic prepared packet.\n", ) .unwrap(); fs::set_permissions(&task, fs::Permissions::from_mode(0o600)).unwrap(); @@ -160,7 +161,13 @@ cat "$0.reply" }}}).to_string()).unwrap(); let missing = invoke(&env, &config, "", false); assert_eq!(missing.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&missing.stderr).contains("App token")); assert!(!env.tmp().join("gh.calls").exists()); + fs::write( + &task, + b"i46 local/fake agent/44 main\nSynthetic prepared packet.\n", + ) + .unwrap(); let mismatch = invoke(&env, &config, "synthetic\n", false); assert_eq!(mismatch.status.code(), Some(2)); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); From 767c73ad7b8233579c4d0c9ee811f2b0f0654bc3 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:26:45 +0100 Subject: [PATCH 18/24] refactor(work): reuse the extracted prepared-input adapter (#1017) --- oxa/oxa-cli/src/work_board/refill.rs | 110 +++------------------------ 1 file changed, 11 insertions(+), 99 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 805cb55b..b2b53d83 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -1,14 +1,10 @@ //! Owner-selected queue frontend. Existing Reader, Claims, publisher and private-file IO only. +use super::refill_input::{Input, Prepared, text}; use super::*; -use crate::work_refill::{Consumer, Packet, Partition, Source}; -use oxa_work::board::{Action, Entry, Issue, Snapshot, assignment::Worker}; +use crate::work_refill::{Consumer, Packet, Partition}; +use oxa_work::board::{Action, assignment::Worker}; use std::{fs, os::unix::fs::MetadataExt, path::PathBuf}; -fn text<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> { - value[key] - .as_str() - .ok_or_else(|| "missing board input field".into()) -} fn private(path: &Path, uid: u32) -> Result { let mut bytes = Vec::new(); crate::lead_input::open(path, uid)? @@ -21,93 +17,6 @@ fn private(path: &Path, uid: u32) -> Result { serde_json::from_slice(&bytes).map_err(|_| "invalid private board input".into()) } -struct Prepared { - number: u64, - revision: String, - action: Action, - branch: String, - task: String, -} -struct Input<'a> { - reader: Reader<'a>, - packets: &'a [Prepared], - intake: bool, -} -impl GitHub for Input<'_> { - fn snapshot(&mut self) -> Result { - let pull_requests = self.reader.open_pull_requests()?; - let mut fields = "pullRequests(first:100,states:OPEN){totalCount nodes{number headRefOid headRefName closingIssuesReferences(first:100){totalCount nodes{number}}}}".to_owned(); - for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { - fields.push_str(&format!( - " i{}:issue(number:{}){{number state updatedAt}}", - packet.number, packet.number - )); - } - let (metadata, _) = self.reader.repository_read(&fields)?; - let rows = nodes(&metadata["pullRequests"])?; - let heads: BTreeMap<_, _> = rows - .iter() - .map(|p| (p["number"].as_u64(), p["headRefOid"].as_str())) - .collect(); - if heads.len() != pull_requests.len() - || pull_requests - .iter() - .any(|p| heads.get(&Some(p.number)) != Some(&Some(p.head.as_str()))) - { - return Err("board membership or head changed; retry later".into()); - } - let mut closing = BTreeSet::new(); - for pr in rows { - for issue in nodes(&pr["closingIssuesReferences"])? { - closing.insert(issue["number"].as_u64().ok_or("invalid closing issue")?); - } - if let Some(packet) = self - .packets - .iter() - .find(|p| p.number == pr["number"].as_u64().unwrap_or(0)) - { - if text(pr, "headRefName")? != packet.branch { - return Err("prepared PR branch changed".into()); - } - } - } - let mut issues = vec![]; - for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { - let issue = &metadata[format!("i{}", packet.number)]; - if issue["number"].as_u64() != Some(packet.number) { - return Err("prepared issue is missing".into()); - } - issues.push(Issue { - number: packet.number, - revision: text(issue, "updatedAt")?.into(), - release_pinned: false, - approved: self.intake && pull_requests.len() < 40 && text(issue, "state")? == "OPEN", - has_open_pull_request: closing.contains(&packet.number), - }); - } - Ok(Snapshot { - pull_requests, - issues, - }) - } -} -impl Source for Input<'_> { - fn revalidate(&mut self, entry: &Entry) -> Result { - let board = Board::read( - self, - &Policy { - enabled: true, - ..Policy::default() - }, - &BTreeMap::new(), - 0, - )?; - Ok(board.entries.iter().any(|fresh| { - fresh.number == entry.number && fresh.head == entry.head && fresh.action == entry.action - })) - } -} - pub(super) fn run( args: &[String], root: &Path, @@ -246,11 +155,14 @@ pub(super) fn run( let gh = crate::netguard::trusted_executable_for("gh", Some(&policy.host_path), Some(root), Some(&dirs.home)) .ok_or("no trusted gh executable")?; - let mut source = Input { - reader: Reader::new(&gh, &policy.host_path, repo, &token), - packets: &packets, - intake: value["intake"] == true, - }; + let mut source = Input::new( + &gh, + &policy.host_path, + repo, + &token, + &packets, + value["intake"] == true, + ); let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map_err(|_| "invalid system clock")? From d9f630b3f67e29df2268ae43b889b12f2a1ddad3 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:30:33 +0100 Subject: [PATCH 19/24] fix(work): complete and bound prepared metadata before admission (#1017) --- oxa/oxa-cli/src/work_board/refill_input.rs | 52 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index 748ea6c5..c7b4c483 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -40,20 +40,64 @@ impl<'a> Input<'a> { impl GitHub for Input<'_> { fn snapshot(&mut self) -> Result { let pull_requests = self.reader.open_pull_requests()?; - let mut fields = "pullRequests(first:100,states:OPEN){totalCount nodes{number headRefOid headRefName closingIssuesReferences(first:100){totalCount nodes{number}}}}".to_owned(); + let mut issue_fields = String::new(); for packet in self.packets.iter().filter(|p| p.action == Action::BuildIssue) { - fields.push_str(&format!( + issue_fields.push_str(&format!( " i{}:issue(number:{}){{number state updatedAt}}", packet.number, packet.number )); } - let (metadata, _) = self.reader.repository_read(&fields)?; + let (mut metadata, mut rows, mut cursor, mut cursors) = + (Value::Null, vec![], String::new(), BTreeSet::new()); + // Retained normalized facts and all metadata pages share one bounded budget. + let mut bytes = serde_json::to_vec(&pull_requests) + .map_err(|_| "cannot bound board facts")? + .len(); + loop { + let fields = format!( + "pullRequests(first:100,states:OPEN{cursor}){{totalCount nodes{{number headRefOid headRefName closingIssuesReferences(first:100){{totalCount nodes{{number}}}}}} pageInfo{{hasNextPage endCursor}}}}{issue_fields}" + ); + let (page, read) = self.reader.repository_read(&fields)?; + super::charge(&mut bytes, read)?; + let connection = &page["pullRequests"]; + if connection["totalCount"].as_u64() != Some(pull_requests.len() as u64) { + return Err("board membership changed; retry later".into()); + } + let batch = connection["nodes"].as_array().ok_or("missing metadata page")?; + if batch.len() > 100 { + return Err("oversized metadata page".into()); + } + rows.extend(batch.iter().cloned()); + if rows.len() > pull_requests.len() { + return Err("oversized metadata snapshot".into()); + } + if metadata.is_null() { + metadata = page.clone(); + } + if !connection["pageInfo"]["hasNextPage"] + .as_bool() + .ok_or("missing metadata pagination")? + { + break; + } + let next = connection["pageInfo"]["endCursor"] + .as_str() + .filter(|c| !c.is_empty()) + .ok_or("missing metadata cursor")?; + if batch.is_empty() || !cursors.insert(next.to_owned()) { + return Err("invalid metadata pagination progress".into()); + } + cursor = format!(",after:{next:?}"); + issue_fields.clear(); + } + metadata["pullRequests"]["nodes"] = rows.into(); let rows = nodes(&metadata["pullRequests"])?; let heads: BTreeMap<_, _> = rows .iter() .map(|p| (p["number"].as_u64(), p["headRefOid"].as_str())) .collect(); - if heads.len() != pull_requests.len() + if heads.len() != rows.len() + || heads.len() != pull_requests.len() || pull_requests .iter() .any(|p| heads.get(&Some(p.number)) != Some(&Some(p.head.as_str()))) From b9705e678f27fcb24d6be972ac523cbba8ef7962 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:37:26 +0100 Subject: [PATCH 20/24] fix(work): bind prepared task identity before claim publication (#1017) --- oxa/oxa-cli/src/work_board/refill.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index b2b53d83..c754c3df 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -127,8 +127,10 @@ pub(super) fn run( .split_ascii_whitespace() .collect(); let branch = text(row, "branch")?; + let task_id = format!("{}{number}", if action == Action::BuildIssue { "i" } else { "c" }); if task.len() > 65_536 || !matches!(header.len(), 3 | 4) + || header[0] != task_id || header[2] != branch || header.get(3).copied().unwrap_or("main") != if action == Action::BuildIssue { @@ -137,7 +139,7 @@ pub(super) fn run( branch } { - return Err("prepared taskfile branch/base mismatch".into()); + return Err("prepared taskfile ref/branch/base mismatch".into()); } packets.push(Prepared { number, @@ -155,14 +157,8 @@ pub(super) fn run( let gh = crate::netguard::trusted_executable_for("gh", Some(&policy.host_path), Some(root), Some(&dirs.home)) .ok_or("no trusted gh executable")?; - let mut source = Input::new( - &gh, - &policy.host_path, - repo, - &token, - &packets, - value["intake"] == true, - ); + let intake = value["intake"] == true; + let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake); let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map_err(|_| "invalid system clock")? From eb6e442e26afbdd465f50195e26cc9cc2c764270 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:15:41 +0100 Subject: [PATCH 21/24] style(work): follow the workspace formatter edition (#1017) --- oxa/oxa-cli/src/work_refill.rs | 2 +- oxa/oxa-cli/tests/work_refill.rs | 32 ++++++++++++-------------------- oxa/oxa-work/src/helper_queue.rs | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs index 91d1bebb..a204b507 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -1,6 +1,6 @@ //! Queue-only consuming adapter; registration, prepared taskfiles and activation are user-owned. use oxa_work::board::{assignment::Worker, *}; -use oxa_work::{Result, helper_queue}; +use oxa_work::{helper_queue, Result}; use sha2::{Digest, Sha256}; use std::{collections::BTreeSet, fs, path::Path}; /// Read-only acquisition and authoritative revision/action revalidation; no write capability. diff --git a/oxa/oxa-cli/tests/work_refill.rs b/oxa/oxa-cli/tests/work_refill.rs index ea212e1f..42bfc2d5 100644 --- a/oxa/oxa-cli/tests/work_refill.rs +++ b/oxa/oxa-cli/tests/work_refill.rs @@ -146,11 +146,9 @@ fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { } assert_eq!((source.reads, source.checks), (0, 0)); fs::remove_file(pause).unwrap(); - assert!( - target(true) - .once(&mut source, &PACKETS, 10, |_| Err("audit failed".into())) - .is_err() - ); + assert!(target(true) + .once(&mut source, &PACKETS, 10, |_| Err("audit failed".into())) + .is_err()); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); source.stale = true; no_task(target(true), &mut source, &PACKETS, 10); @@ -162,11 +160,9 @@ fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { no_task(target(true), &mut source, &PACKETS, 10); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); let other_lease = claims.snapshot().unwrap()[&46].lease.clone().unwrap(); - assert!( - claims - .release_for(46, &worker.id, &other_lease, 10, "finished other ref") - .unwrap() - ); + assert!(claims + .release_for(46, &worker.id, &other_lease, 10, "finished other ref") + .unwrap()); let receipts = target(true) .once(&mut source, &PACKETS, 10, |board| { assert_eq!( @@ -184,11 +180,9 @@ fn paused_partitioned_role_checked_admission_retains_one_exact_lease() { fs::rename(queue.join(&name), queue.join("started").join(name)).unwrap(); no_task(target(true), &mut source, &PACKETS, 80); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); - assert!( - claims - .release_for(44, &worker.id, &receipt.lease, 80, "verified completion") - .unwrap() - ); + assert!(claims + .release_for(44, &worker.id, &receipt.lease, 80, "verified completion") + .unwrap()); // Retained started file dedupes before a second claim, even after fenced release. no_task(target(true), &mut source, &PACKETS, 81); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); @@ -218,9 +212,7 @@ fn unknown_held_owner_only_and_reviewer_work_never_publishes() { no_task(fixture.target(), &mut source, &PACKETS, 10); } assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); - assert!( - fs::read_dir(&fixture.queue) - .unwrap() - .all(|e| e.unwrap().path().is_dir()) - ); + assert!(fs::read_dir(&fixture.queue) + .unwrap() + .all(|e| e.unwrap().path().is_dir())); } diff --git a/oxa/oxa-work/src/helper_queue.rs b/oxa/oxa-work/src/helper_queue.rs index b8ce8c20..970f4a20 100644 --- a/oxa/oxa-work/src/helper_queue.rs +++ b/oxa/oxa-work/src/helper_queue.rs @@ -1,5 +1,5 @@ //! Atomic publication only. The caller must reserve a core claim and authorize the target first. -use crate::{Result, board::Policy}; +use crate::{board::Policy, Result}; use std::fs::{self, File, OpenOptions}; use std::io::Write; use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt}; From a07d947b11db15220ed52d9ef9e764334e20c737 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:18:41 +0100 Subject: [PATCH 22/24] style(work): format source input with the workspace edition (#1017) --- oxa/oxa-cli/tests/work_refill_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oxa/oxa-cli/tests/work_refill_input.rs b/oxa/oxa-cli/tests/work_refill_input.rs index 0213117c..a6f38796 100644 --- a/oxa/oxa-cli/tests/work_refill_input.rs +++ b/oxa/oxa-cli/tests/work_refill_input.rs @@ -4,7 +4,7 @@ use oxa_cli::{ }; use oxa_fake::TestEnv; use oxa_work::board::{Action, Board, GitHub, Policy}; -use serde_json::{Value, json}; +use serde_json::{json, Value}; use std::{collections::BTreeMap, fs, os::unix::fs::PermissionsExt}; fn write(env: &TestEnv, name: &str, value: &Value) { From f30daa9107657a419718d265421e1ca3b546a9ef Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:40:14 +0100 Subject: [PATCH 23/24] test(work): refuse board state in sibling task trees --- oxa/oxa-cli/tests/work_refill_cli.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index 107ac251..b36388ea 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -188,4 +188,29 @@ cat "$0.reply" assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); assert!(invoke(&env, &config, "synthetic\n", false).status.success()); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); + let peer = env + .work() + .parent() + .unwrap() + .join(".oxa-worktrees") + .join(env.work().file_name().unwrap()) + .join("synthetic-task"); + fs::create_dir_all(&peer).unwrap(); + fs::set_permissions(&peer, fs::Permissions::from_mode(0o700)).unwrap(); + let original: serde_json::Value = serde_json::from_slice(&fs::read(&config).unwrap()).unwrap(); + for key in ["queue", "claims", "pause", "log"] { + let path = peer.join(key); + if matches!(key, "queue" | "claims") { + fs::create_dir(&path).unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap(); + } + let mut unsafe_config = original.clone(); + unsafe_config[key] = json!(path); + fs::write(&config, unsafe_config.to_string()).unwrap(); + assert_eq!( + invoke(&env, &config, "synthetic\n", false).status.code(), + Some(2), + "worker-writable {key} must be refused", + ); + } } From 774588759e4be78cfb319b5d5eb2451e13a1e986 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:49:19 +0100 Subject: [PATCH 24/24] fix(work): protect refill state with the runner storage guard --- oxa/oxa-cli/src/work_board.rs | 3 ++- oxa/oxa-cli/src/work_board/refill.rs | 5 ++--- oxa/oxa-cli/src/work_cmd.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index cfc7a256..5d1a41ba 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -412,12 +412,13 @@ impl GitHub for Reader<'_> { pub(crate) fn run( args: &[String], root: &Path, + common: &Path, env: &oxa_config::Env, dirs: &oxa_config::Dirs, policy: &oxa_work::RunPolicy, ) -> Result { if args.first().is_some_and(|arg| arg == "--refill") { - return refill::run(args, root, env, dirs, policy); + return refill::run(args, root, common, env, dirs, policy); } // Existing parser declaration; migrate command/flag metadata together in #1062. let (repo, options) = match args { diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 4d4b7bb7..44a26628 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -20,6 +20,7 @@ fn private(path: &Path, uid: u32) -> Result { pub(super) fn run( args: &[String], root: &Path, + common: &Path, env: &oxa_config::Env, dirs: &oxa_config::Dirs, policy: &oxa_work::RunPolicy, @@ -74,9 +75,7 @@ pub(super) fn run( } let owner_path = |key| -> Result { let path = PathBuf::from(text(&value, key)?); - if !path.is_absolute() || path.starts_with(root) { - return Err("board state must be outside the source project".into()); - } + crate::work_cmd::protected_storage(&path, root, common)?; Ok(path) }; let queue = owner_path("queue")?; diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 9d6cd577..cfb01194 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -17,7 +17,7 @@ pub(crate) fn trusted(cwd: &Path, env: &Env, dirs: &Dirs) -> Result Result<(), String> { +pub(crate) fn protected_storage(storage: &Path, repo: &Path, common: &Path) -> Result<(), String> { if !storage.is_absolute() || storage .components() @@ -153,7 +153,7 @@ pub(crate) fn run( }; protected_storage(&policy.storage, &root, &common)?; if args.first().is_some_and(|arg| arg == "board") { - return crate::work_board::run(&args[1..], &root, env, dirs, &policy); + return crate::work_board::run(&args[1..], &root, &common, env, dirs, &policy); } if matches!(args.first().map(String::as_str), Some("status" | "--status")) { let json = match args {