From dc2216aa278b9c31f82579bf85be1d51a8eb1d08 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:49:15 +0100 Subject: [PATCH 01/18] work: reuse bounded PR context for native packet sources (#1017) --- oxa/oxa-work/src/github.rs | 125 ++++++++++++++++++++++++++++++------- oxa/oxa-work/src/lib.rs | 5 +- 2 files changed, 106 insertions(+), 24 deletions(-) diff --git a/oxa/oxa-work/src/github.rs b/oxa/oxa-work/src/github.rs index 393badb5c..64d342b02 100644 --- a/oxa/oxa-work/src/github.rs +++ b/oxa/oxa-work/src/github.rs @@ -8,10 +8,7 @@ use serde_json::Value; use std::path::Path; use std::process::Command; -const PR_QUERY: &str = r#" -query($owner: String!, $repo: String!, $number: Int!) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $number) { +const PR_FIELDS: &str = r#" title body headRefOid headRefName isCrossRepository comments(first:100) { totalCount nodes { author { login } body } } reviews(first:100) { totalCount nodes { author { login } body state } } @@ -29,9 +26,57 @@ query($owner: String!, $repo: String!, $number: Int!) { ... on StatusContext { context state } } } } } } } +"#; + +/// Bounded repository fields for the existing GitHub reader to fetch. +pub fn pull_request_fields(number: u64) -> Result { + if number == 0 { + return Err("pull request number must be positive".into()); + } + Ok(format!("pullRequest(number:{number}) {{ {PR_FIELDS} }}")) +} + +/// Fresh, bounded context for one immutable pull request head. +pub struct PullRequestContext { + pub branch: String, + pub prompt: String, +} + +fn pull_request_identity(pr: &Value) -> Result<(&str, &str)> { + if pr["isCrossRepository"] != false { + return Err("pull request must have a branch in the source repository".into()); + } + let head = pr["headRefOid"] + .as_str() + .filter(|value| oid(value)) + .ok_or("reviewed head missing")?; + let branch = pr["headRefName"] + .as_str() + .filter(|name| !name.is_empty()) + .ok_or("pull request branch missing")?; + Ok((head, branch)) +} + +/// Validate a reader snapshot against the frozen head without transport or checkpoints. +pub fn prepare_pull_request_context( + number: u64, + expected_head: &str, + pr: &Value, +) -> Result { + if number == 0 { + return Err("pull request number must be positive".into()); + } + let (head, branch) = pull_request_identity(pr)?; + if head != expected_head { + return Err("pull request changed since the reviewed head".into()); } - } -}"#; + let mut prompt = github_runner_guidance("pull request"); + prompt.push_str(&pull_request_prompt(number, head, pr)?); + Ok(PullRequestContext { + branch: branch.into(), + prompt, + }) +} fn oid(value: &str) -> bool { value.len() == 40 && crate::board::queue::is_object_id(value) @@ -83,13 +128,18 @@ pub fn pull_request_task( if number == 0 { return Err("pull request number must be positive".into()); } + let query = format!( + "query($owner: String!, $repo: String!, $number: Int!) {{ \ + repository(owner: $owner, name: $repo) {{ \ + pullRequest(number: $number) {{ {PR_FIELDS} }} }} }}" + ); let mut command = Command::new(gh); isolate_git_environment(&mut command); command.env("PATH", &policy.host_path).current_dir(repo).args([ "api", "graphql", "-f", - &format!("query={PR_QUERY}"), + &format!("query={query}"), "-F", "owner={owner}", "-F", @@ -102,17 +152,7 @@ pub fn pull_request_task( return Err("GitHub returned an incomplete pull request snapshot".into()); } let pr = &response["data"]["repository"]["pullRequest"]; - if pr["isCrossRepository"] != false { - return Err("pull request must have a branch in the source repository".into()); - } - let head = pr["headRefOid"] - .as_str() - .filter(|value| oid(value)) - .ok_or("reviewed head missing")?; - let branch = pr["headRefName"] - .as_str() - .filter(|name| !name.is_empty()) - .ok_or("pull request branch missing")?; + let (head, branch) = pull_request_identity(pr)?; let id = format!("pr-{number}-{head}"); if let Some(saved) = load_task_checkpoint(policy, &id)? { let task = saved.task; @@ -124,14 +164,13 @@ pub fn pull_request_task( // Capture acceptance from the trusted local main lineage. The branch can // be behind main without changing the reviewed tree before the worker runs. let base = checked(git(policy, repo, &["merge-base", "refs/heads/main", head]))?; - let mut prompt = github_runner_guidance("pull request"); - prompt.push_str(&pull_request_prompt(number, head, pr)?); + let context = prepare_pull_request_context(number, head, pr)?; Ok(Task { id, model, - branch: branch.into(), + branch: context.branch, base, - prompt, + prompt: context.prompt, max_time: default_max_time(), reviewed_pull_request: None, }) @@ -304,6 +343,9 @@ mod tests { json!({ "title": "Synthetic PR", "body": "Fixture", + "headRefOid": HEAD, + "headRefName": "agent/fixture", + "isCrossRepository": false, "reviews": {"totalCount": 0, "nodes": []}, "comments": {"totalCount": 0, "nodes": []}, "reviewThreads": {"totalCount": 0, "nodes": []}, @@ -312,6 +354,42 @@ mod tests { }) } + #[test] + fn prepared_context_keeps_runner_authority_and_refreshes_same_head_data() { + let mut pr = packet(); + let context = prepare_pull_request_context(1, HEAD, &pr).unwrap(); + assert_eq!(context.branch, "agent/fixture"); + assert_eq!( + context.prompt, + github_runner_guidance("pull request") + &pull_request_prompt(1, HEAD, &pr).unwrap() + ); + pr["body"] = json!("Fresh context at the same reviewed head"); + let refreshed = prepare_pull_request_context(1, HEAD, &pr).unwrap(); + assert!(refreshed + .prompt + .contains("Fresh context at the same reviewed head")); + assert_ne!(refreshed.prompt, context.prompt); + } + + #[test] + fn prepared_context_refuses_missing_identity_foreign_or_changed_heads() { + assert!(pull_request_fields(0).is_err()); + assert!(prepare_pull_request_context(0, HEAD, &packet()).is_err()); + assert!(prepare_pull_request_context(1, &"a".repeat(40), &packet()).is_err()); + for (field, value) in [ + ("isCrossRepository", json!(true)), + ("isCrossRepository", Value::Null), + ("headRefOid", json!("a".repeat(64))), + ("headRefOid", Value::Null), + ("headRefName", json!("")), + ("headRefName", Value::Null), + ] { + let mut pr = packet(); + pr[field] = value; + assert!(prepare_pull_request_context(1, HEAD, &pr).is_err(), "{field}"); + } + } + #[test] fn clean_empty_packet_has_no_missing_data_marker() { let prompt = pull_request_prompt(1, HEAD, &packet()).unwrap(); @@ -326,6 +404,7 @@ mod tests { let mut pr = packet(); pr["commits"]["nodes"][0]["commit"]["oid"] = json!("stale-head"); assert!(pull_request_prompt(1, HEAD, &pr).is_err()); + assert!(prepare_pull_request_context(1, HEAD, &pr).is_err()); let mut pr = packet(); pr["reviews"] = json!(3); assert!(pull_request_prompt(1, HEAD, &pr).is_err()); @@ -347,7 +426,7 @@ mod tests { "nodes": [{"name": "failure".repeat(1000), "conclusion": "FAILURE"}] } }); - let prompt = pull_request_prompt(1, HEAD, &pr).unwrap(); + let prompt = prepare_pull_request_context(1, HEAD, &pr).unwrap().prompt; assert!(prompt.contains("NEED-METADATA")); assert!(prompt.contains("Ask the runner")); assert!(prompt.contains("by unknown")); diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 8fdeb6b56..82fcb2ca3 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -51,7 +51,10 @@ pub mod events; mod facts_io; pub use facts_io::require_private_directory; mod github; -pub use github::{pull_request_task, ReviewedPullRequest}; +pub use github::{ + prepare_pull_request_context, pull_request_fields, pull_request_task, PullRequestContext, + ReviewedPullRequest, +}; pub mod incidents; pub mod situation; pub mod standing; From 875a83f5218e38e57b8da4c4d99135c6e8df18b5 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:56:14 +0100 Subject: [PATCH 02/18] test(work): require live App packet generation through refill (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 80 ++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index ccdef35a0..322b8dc0f 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -102,11 +102,16 @@ fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { #[test] fn private_token_and_exact_task_ref_precede_one_durable_publication() { for pull_request in [false, true] { - durable_publication(pull_request); + durable_publication(pull_request, false); } } -fn durable_publication(pull_request: bool) { +#[test] +fn live_fix_packet_uses_current_app_context_and_the_original_durable_binding() { + durable_publication(true, true); +} + +fn durable_publication(pull_request: bool, live: bool) { use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState}; use std::collections::BTreeMap; let (env, controls) = fixture(); @@ -140,7 +145,13 @@ fn durable_publication(pull_request: bool) { } else { "revision".into() }; - let valid = if pull_request { + let live_task = format!( + "c44 local/fake agent/44 agent/44\n{}Address pull request #44 at {revision}: Synthetic live PR\n\nCurrent protected source", + oxa_work::github_runner_guidance("pull request") + ); + let valid = if live { + live_task.as_str() + } else if pull_request { "c44 local/fake agent/44 agent/44\nSynthetic prepared packet.\n" } else { "i44 local/fake agent/44 main\nSynthetic prepared packet.\n" @@ -148,12 +159,24 @@ fn durable_publication(pull_request: bool) { fs::write(&task, valid).unwrap(); fs::set_permissions(&task, fs::Permissions::from_mode(0o600)).unwrap(); let config = controls.join("refill.json"); - fs::write(&config, json!({"enabled":true,"host_id":"host-a","intake":true,"queue":queue,"claims":root, + let mut settings = json!({"enabled":true,"host_id":"host-a","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":if pull_request {"resolve_conflict"} else {"build_issue"},"branch":"agent/44","taskfile":task}] - }).to_string()).unwrap(); + }); + if live { + settings.as_object_mut().unwrap().remove("packets"); + settings["live"] = json!(true); + settings["intake"] = json!(false); + settings["model"] = json!("local/fake"); + fs::write( + &task, + b"Unused manual packet; live mode must fetch current context.", + ) + .unwrap(); + } + fs::write(&config, settings.to_string()).unwrap(); fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); let gh = env.write( "tmp/gh", @@ -174,6 +197,11 @@ cat "$0.reply" fs::write(env.tmp().join("gh.reply"), json!({"data":{"repository":{ "mergeQueue":null,"pullRequests":{"totalCount":rows.len(),"nodes":rows,"pageInfo":{"hasNextPage":false,"endCursor":"last"}}, "i44":{"number":44,"state":"OPEN","updatedAt":"revision"},"b44":null, + "pullRequest":{"title":"Synthetic live PR","body":"Current protected source", + "headRefOid":revision,"headRefName":"agent/44","isCrossRepository":false, + "comments":{"totalCount":0,"nodes":[]},"reviews":{"totalCount":0,"nodes":[]}, + "reviewThreads":{"totalCount":0,"nodes":[]},"files":{"totalCount":0,"nodes":[]}, + "commits":{"nodes":[{"commit":{"oid":revision,"statusCheckRollup":null}}]}}, "p44":{"number":44,"headRefOid":revision,"baseRefName":"main","state":"OPEN","body":"", "isDraft":false,"reviewDecision":null,"mergeable":"CONFLICTING","mergeStateStatus":"DIRTY", "baseRef":{"refUpdateRule":null,"rules":{"totalCount":0,"nodes":[]}}, @@ -183,18 +211,22 @@ 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")); + if !live { + 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); - assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); - fs::write(&task, valid).unwrap(); + if !live { + 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); + assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); + fs::write(&task, valid).unwrap(); + } let original_config = fs::read(&config).unwrap(); let mut hostless: serde_json::Value = serde_json::from_slice(&original_config).unwrap(); hostless.as_object_mut().unwrap().remove("host_id"); @@ -203,6 +235,22 @@ cat "$0.reply" assert_eq!(refused.status.code(), Some(2), "native refill requires a host"); assert!(!env.tmp().join("gh.calls").exists()); fs::write(&config, original_config).unwrap(); + if live { + let reply = env.tmp().join("gh.reply"); + let original = fs::read(&reply).unwrap(); + for (field, value) in [ + ("headRefOid", json!("b".repeat(40))), + ("isCrossRepository", json!(true)), + ] { + let mut response: serde_json::Value = serde_json::from_slice(&original).unwrap(); + response["data"]["repository"]["pullRequest"][field] = value; + fs::write(&reply, response.to_string()).unwrap(); + assert_eq!(invoke(&env, &config, "synthetic\n", false).status.code(), Some(2)); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); + } + fs::write(&reply, original).unwrap(); + } if !pull_request { let reply = env.tmp().join("gh.reply"); let original = fs::read(&reply).unwrap(); From c48d04b952320322a8b6737513cdd55fbbc20a11 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:07:40 +0100 Subject: [PATCH 03/18] work: generate current native fix packets with the App reader (#1017) --- oxa/docs/work.md | 8 ++++ oxa/oxa-cli/src/work_board/refill.rs | 33 +++++++++++--- oxa/oxa-cli/src/work_board/refill_input.rs | 51 +++++++++++++++++++++- oxa/oxa-cli/src/work_refill.rs | 43 ++++++++++-------- 4 files changed, 111 insertions(+), 24 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index f59b75fa9..a93a535db 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -325,3 +325,11 @@ its worker process exits. Problem-report workflows hold a per-user kernel lease across drafting, duplicate search, match reads and writing, so concurrent workers share one reporting lane. + +Board refill can generate current PR fix packets with `live: true` and an explicit +`model` in its protected owner config. Omit `packets` and keep issue `intake` off. +The same config supplies the registered worker, host, Claims path, queue, pause +marker and item partition. Each round reads current assignment candidates and PR +context through the explicit App credential, then uses the existing admission and +publication path. A changed head or action cannot publish the prepared packet. +This mode does not execute a worker or select a default host, model or Claims store. diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 5e325a1a2..0babaab7e 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -94,10 +94,28 @@ pub(super) fn run( 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 live = value["live"] == true; + let model = if live { + if value.get("packets").is_some() || value["intake"] == true { + return Err("live fix packets cannot use prepared packets or issue intake".into()); + } + let model = text(&value, "model")?; + if model.is_empty() || model.bytes().any(|b| b.is_ascii_whitespace() || b == 0) { + return Err("live model must be one task header field".into()); + } + Some(model) + } else { + None + }; + let rows = if live { + &[][..] + } else { + value["packets"] + .as_array() + .filter(|rows| rows.len() <= 20) + .ok_or("expected bounded prepared packets")? + .as_slice() + }; let mut packets = vec![]; let mut seen = BTreeSet::new(); for row in rows { @@ -156,7 +174,6 @@ 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 intake = value["intake"] == true; - let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map_err(|_| "invalid system clock")? @@ -165,6 +182,12 @@ pub(super) fn run( enabled: true, ..Policy::default() }; + if let Some(model) = model { + packets = Input::new(&gh, &policy.host_path, repo, &token, &[], false) + .with_host(host) + .prepare_live(&claims, &policy, &worker, &partition, now, model)?; + } + let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let consumer = Consumer { repository: repo, queue: &queue, diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index bdb7be9bc..3c74440fa 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -1,7 +1,9 @@ //! Read-only prepared-input normalization through the existing board Reader. use super::*; -use crate::work_refill::Source; -use oxa_work::board::{Action, Entry, ExistingWork, Issue, NativeBinding, Snapshot}; +use crate::work_refill::{Partition, Source}; +use oxa_work::board::{ + assignment::Worker, Action, Claims, Entry, ExistingWork, Issue, NativeBinding, Snapshot, +}; pub(super) fn text<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> { value[key] .as_str() @@ -66,6 +68,51 @@ impl<'a> Input<'a> { self.host = Some(host); self } + + /// Generate only current fix packets; Consumer still owns all admission and publication. + pub(super) fn prepare_live( + &mut self, + claims: &Claims, + policy: &Policy, + worker: &Worker, + partition: &Partition, + now: u64, + model: &str, + ) -> Result, String> { + self.host_id()?; + let board = partition.assignment_board(claims, self, policy, worker, now)?; + let mut prepared = vec![]; + for number in board.candidates(&worker.id)? { + let entry = board + .board + .entries + .iter() + .find(|entry| entry.number == number) + .unwrap(); + if !matches!( + entry.action, + Action::FixFindings | Action::FixRequiredCi | Action::ResolveConflict | Action::AddRemoves + ) { + continue; + } + let fields = oxa_work::pull_request_fields(number)?; + let (snapshot, _) = self.reader.repository_read(&fields)?; + let context = + oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; + let branch = context.branch; + prepared.push(Prepared { + number, + revision: entry.head.clone(), + action: entry.action, + task: format!("c{number} {model} {branch} {branch}\n{}", context.prompt), + branch, + }); + if prepared.len() == 20 { + break; + } + } + Ok(prepared) + } } impl GitHub for Input<'_> { fn snapshot(&mut self) -> Result { diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs index b55c34cdc..61830d1af 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -30,6 +30,29 @@ impl Partition { fn contains(&self, number: u64) -> bool { self.residues.contains(&(number % self.modulus)) } + + pub(crate) fn assignment_board( + &self, + claims: &Claims, + source: &mut impl GitHub, + policy: &Policy, + worker: &Worker, + now: u64, + ) -> Result { + if self.modulus == 0 + || self.residues.is_empty() + || self.residues.iter().any(|r| *r >= self.modulus) + || policy.max_attempts_per_day > 3 + { + return Err("invalid board partition or attempt cap".into()); + } + claims.assignment_board( + &mut Partitioned(source, self), + policy, + std::slice::from_ref(worker), + now, + ) + } } struct Partitioned<'a, S>(&'a mut S, &'a Partition); impl GitHub for Partitioned<'_, S> { @@ -89,23 +112,9 @@ impl Consumer<'_> { return Ok(vec![]); } let host = source.host_id()?.map(str::to_owned); - 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, - )?; + let board = self + .partition + .assignment_board(self.claims, source, self.policy, 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(); From bbb7531e99b1d7e5944bb2babd3fd385b526a586 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:12:24 +0100 Subject: [PATCH 04/18] work: retain runner path when preparing live packets (#1017) --- oxa/oxa-cli/src/work_board/refill.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 0babaab7e..4062df47c 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -178,14 +178,14 @@ pub(super) fn run( .duration_since(SystemTime::UNIX_EPOCH) .map_err(|_| "invalid system clock")? .as_secs(); - let policy = Policy { + let board_policy = Policy { enabled: true, ..Policy::default() }; if let Some(model) = model { packets = Input::new(&gh, &policy.host_path, repo, &token, &[], false) .with_host(host) - .prepare_live(&claims, &policy, &worker, &partition, now, model)?; + .prepare_live(&claims, &board_policy, &worker, &partition, now, model)?; } let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let consumer = Consumer { @@ -195,7 +195,7 @@ pub(super) fn run( claims: &claims, worker: &worker, partition: &partition, - policy: &policy, + policy: &board_policy, ttl: 1800, }; let inputs: Vec<_> = packets From ce237a5dfeb5a6071f58b191256bff3620a5944e Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:25:22 +0100 Subject: [PATCH 05/18] test(work): refuse live packet publication for unknown attempts (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index 322b8dc0f..e093b7b05 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -238,6 +238,29 @@ cat "$0.reply" if live { let reply = env.tmp().join("gh.reply"); let original = fs::read(&reply).unwrap(); + // Item 66 is inside the same partition but has no imported attempt row. + let mut unknown: serde_json::Value = serde_json::from_slice(&original).unwrap(); + let repository = &mut unknown["data"]["repository"]; + repository["pullRequests"]["nodes"][0]["number"] = json!(66); + repository["p66"] = repository["p44"].take(); + repository["p66"]["number"] = json!(66); + fs::write(&reply, unknown.to_string()).unwrap(); + let refused = invoke(&env, &config, "synthetic\n", false); + assert!( + refused.status.success(), + "{}", + String::from_utf8_lossy(&refused.stderr) + ); + let rows = claims.assignments().unwrap(); + assert_eq!(rows.len(), 1); + assert!( + !rows.contains_key(&66), + "unknown attempts must not become a zero-count claim" + ); + assert_eq!(rows[&44].state.attempts, 0); + assert!(fs::read_dir(&queue) + .unwrap() + .all(|entry| entry.unwrap().path().is_dir())); for (field, value) in [ ("headRefOid", json!("b".repeat(40))), ("isCrossRepository", json!(true)), @@ -247,7 +270,9 @@ cat "$0.reply" fs::write(&reply, response.to_string()).unwrap(); assert_eq!(invoke(&env, &config, "synthetic\n", false).status.code(), Some(2)); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); - assert_eq!(fs::read_dir(&queue).unwrap().count(), 0); + assert!(fs::read_dir(&queue) + .unwrap() + .all(|entry| entry.unwrap().path().is_dir())); } fs::write(&reply, original).unwrap(); } From 7c394db943f0ac237d9fe733f577905818aa7057 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:55:10 +0100 Subject: [PATCH 06/18] test(work): accept Markdown delimiters in live PR context (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index e093b7b05..b05e2f88e 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -146,7 +146,7 @@ fn durable_publication(pull_request: bool, live: bool) { "revision".into() }; let live_task = format!( - "c44 local/fake agent/44 agent/44\n{}Address pull request #44 at {revision}: Synthetic live PR\n\nCurrent protected source", + "c44 local/fake agent/44 agent/44\n{}Address pull request #44 at {revision}: Synthetic live PR\n\nCurrent protected source\n\\---\nNext section\n\nReview and conversation data\n\nConversation comment by fixture:\nFeedback\r\n\\ \t--- \t\r\nMore feedback\n\n", oxa_work::github_runner_guidance("pull request") ); let valid = if live { @@ -197,9 +197,9 @@ cat "$0.reply" fs::write(env.tmp().join("gh.reply"), json!({"data":{"repository":{ "mergeQueue":null,"pullRequests":{"totalCount":rows.len(),"nodes":rows,"pageInfo":{"hasNextPage":false,"endCursor":"last"}}, "i44":{"number":44,"state":"OPEN","updatedAt":"revision"},"b44":null, - "pullRequest":{"title":"Synthetic live PR","body":"Current protected source", + "pullRequest":{"title":"Synthetic live PR","body":"Current protected source\n---\nNext section", "headRefOid":revision,"headRefName":"agent/44","isCrossRepository":false, - "comments":{"totalCount":0,"nodes":[]},"reviews":{"totalCount":0,"nodes":[]}, + "comments":{"totalCount":1,"nodes":[{"author":{"login":"fixture"},"body":"Feedback\r\n \t--- \t\r\nMore feedback"}]},"reviews":{"totalCount":0,"nodes":[]}, "reviewThreads":{"totalCount":0,"nodes":[]},"files":{"totalCount":0,"nodes":[]}, "commits":{"nodes":[{"commit":{"oid":revision,"statusCheckRollup":null}}]}}, "p44":{"number":44,"headRefOid":revision,"baseRefName":"main","state":"OPEN","body":"", From 3e0bd8c9d08d519bbfd0ec6d81da1c64fdd58109 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:13:17 +0100 Subject: [PATCH 07/18] work: quote Markdown queue delimiters in live PR context (#1017) --- oxa/oxa-cli/src/work_board/refill_input.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index 3c74440fa..f3de9ef52 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -99,12 +99,19 @@ impl<'a> Input<'a> { let (snapshot, _) = self.reader.repository_read(&fields)?; let context = oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; + let mut prompt = String::with_capacity(context.prompt.len()); + for line in context.prompt.split_inclusive('\n') { + if line.trim() == "---" { + prompt.push('\\'); + } + prompt.push_str(line); + } let branch = context.branch; prepared.push(Prepared { number, revision: entry.head.clone(), action: entry.action, - task: format!("c{number} {model} {branch} {branch}\n{}", context.prompt), + task: format!("c{number} {model} {branch} {branch}\n{prompt}"), branch, }); if prepared.len() == 20 { From ad1037b6045edfabbd086189d03c1922ca270b9a Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:39:45 +0100 Subject: [PATCH 08/18] test(work): reach known live work past unknown and fork candidates (#1017) --- oxa/oxa-cli/tests/work_refill_cli.rs | 81 ++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index b05e2f88e..60f5195f3 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -102,16 +102,21 @@ fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { #[test] fn private_token_and_exact_task_ref_precede_one_durable_publication() { for pull_request in [false, true] { - durable_publication(pull_request, false); + durable_publication(pull_request, false, false); } } #[test] fn live_fix_packet_uses_current_app_context_and_the_original_durable_binding() { - durable_publication(true, true); + durable_publication(true, true, false); } -fn durable_publication(pull_request: bool, live: bool) { +#[test] +fn live_known_work_is_not_starved_by_twenty_unknown_candidates() { + durable_publication(true, true, true); +} + +fn durable_publication(pull_request: bool, live: bool, leading_unknowns: bool) { use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState}; use std::collections::BTreeMap; let (env, controls) = fixture(); @@ -176,6 +181,9 @@ fn durable_publication(pull_request: bool, live: bool) { ) .unwrap(); } + if leading_unknowns { + settings["partition"]["residues"] = json!((0..12).collect::>()); + } fs::write(&config, settings.to_string()).unwrap(); fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); let gh = env.write( @@ -184,6 +192,10 @@ fn durable_publication(pull_request: bool, live: bool) { [ "$GH_TOKEN" = synthetic ] || exit 93 printf 'read\n' >> "$0.calls" printf 'HTTP/2.0 200 OK\r\n\r\n' +for context in "$0.context-"*; do + number="${context##*-}" + case "$*" in *"pullRequest(number:$number) {"*) cat "$context"; exit 0 ;; esac +done cat "$0.reply" "#, ); @@ -265,10 +277,17 @@ cat "$0.reply" ("headRefOid", json!("b".repeat(40))), ("isCrossRepository", json!(true)), ] { + if leading_unknowns && field == "isCrossRepository" { + continue; + } let mut response: serde_json::Value = serde_json::from_slice(&original).unwrap(); response["data"]["repository"]["pullRequest"][field] = value; fs::write(&reply, response.to_string()).unwrap(); - assert_eq!(invoke(&env, &config, "synthetic\n", false).status.code(), Some(2)); + let expected = if field == "isCrossRepository" { 0 } else { 2 }; + assert_eq!( + invoke(&env, &config, "synthetic\n", false).status.code(), + Some(expected) + ); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); assert!(fs::read_dir(&queue) .unwrap() @@ -306,6 +325,55 @@ cat "$0.reply" } fs::write(&reply, original).unwrap(); } + if live { + let leading: Vec = if leading_unknowns { + (1..=11).chain(22..=30).collect() + } else { + claims + .import_snapshot( + Some(ManualAuthority::Owner), + &BTreeMap::from([( + 22, + ImportState { + state: AttemptState::default(), + excluded: false, + }, + )]), + now, + ) + .unwrap(); + vec![22] + }; + let reply = env.tmp().join("gh.reply"); + let mut response: serde_json::Value = serde_json::from_slice(&fs::read(&reply).unwrap()).unwrap(); + let repository = &mut response["data"]["repository"]; + let row_template = repository["pullRequests"]["nodes"][0].clone(); + let pr_template = repository["p44"].clone(); + for number in &leading { + let mut row = row_template.clone(); + row["number"] = json!(number); + row["headRefName"] = json!(format!("agent/{number}")); + repository["pullRequests"]["nodes"] + .as_array_mut() + .unwrap() + .push(row); + let mut pr = pr_template.clone(); + pr["number"] = json!(number); + repository[format!("p{number}")] = pr; + } + repository["pullRequests"]["totalCount"] = json!(leading.len() + 1); + fs::write(&reply, response.to_string()).unwrap(); + for number in leading { + let mut context = response.clone(); + context["data"]["repository"]["pullRequest"]["headRefName"] = json!(format!("agent/{number}")); + context["data"]["repository"]["pullRequest"]["isCrossRepository"] = json!(!leading_unknowns); + fs::write( + env.tmp().join(format!("gh.context-{number}")), + context.to_string(), + ) + .unwrap(); + } + } let result = invoke(&env, &config, "synthetic\n", false); assert!( result.status.success(), @@ -336,6 +404,11 @@ cat "$0.reply" format!("board-{}.txt", binding.key) ); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); + if live && !leading_unknowns { + let fork = &claims.snapshot().unwrap()[&22]; + assert_eq!(fork.attempts, 0); + assert!(fork.lease.is_none()); + } assert!(invoke(&env, &config, "synthetic\n", false).status.success()); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); if !pull_request { From fa9cc54a9c522d2a135f277c571a559307955001 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:54:55 +0100 Subject: [PATCH 09/18] fix(work): keep ineligible live candidates from blocking known work --- oxa/oxa-cli/src/work_board/refill_input.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index f3de9ef52..fa1388256 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -83,6 +83,9 @@ impl<'a> Input<'a> { let board = partition.assignment_board(claims, self, policy, worker, now)?; let mut prepared = vec![]; for number in board.candidates(&worker.id)? { + if !board.assignments.contains_key(&number) { + continue; + } let entry = board .board .entries @@ -97,6 +100,9 @@ impl<'a> Input<'a> { } let fields = oxa_work::pull_request_fields(number)?; let (snapshot, _) = self.reader.repository_read(&fields)?; + if snapshot["pullRequest"]["isCrossRepository"] == true { + continue; + } let context = oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; let mut prompt = String::with_capacity(context.prompt.len()); From a65279c9dc6beea78b5b3a3eb182c83dcfabdad5 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:05:46 +0100 Subject: [PATCH 10/18] test(board): reproduce retained-lease and branch admission starvation --- oxa/oxa-cli/tests/work_refill_cli.rs | 124 ++++++++++++++++++++------- 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index 60f5195f3..fe6f7d1d9 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -102,23 +102,43 @@ fn refill_startup_retains_pause_and_refuses_worker_writable_authority() { #[test] fn private_token_and_exact_task_ref_precede_one_durable_publication() { for pull_request in [false, true] { - durable_publication(pull_request, false, false); + durable_publication(pull_request, None); } } #[test] fn live_fix_packet_uses_current_app_context_and_the_original_durable_binding() { - durable_publication(true, true, false); + durable_publication(true, Some(Leading::Fork)); } #[test] fn live_known_work_is_not_starved_by_twenty_unknown_candidates() { - durable_publication(true, true, true); + durable_publication(true, Some(Leading::Unknown)); } -fn durable_publication(pull_request: bool, live: bool, leading_unknowns: bool) { - use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState}; +#[test] +fn live_known_work_is_not_starved_by_twenty_retained_expired_leases() { + durable_publication(true, Some(Leading::RetainedLeases)); +} + +#[test] +fn live_invalid_native_branch_is_skipped_before_reservation() { + durable_publication(true, Some(Leading::InvalidBranch)); +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Leading { + Fork, + Unknown, + RetainedLeases, + InvalidBranch, +} + +fn durable_publication(pull_request: bool, leading: Option) { + use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState, Lease}; use std::collections::BTreeMap; + let live = leading.is_some(); + let twenty = matches!(leading, Some(Leading::Unknown | Leading::RetainedLeases)); let (env, controls) = fixture(); let queue = controls.join("queue"); let root = controls.join("claims"); @@ -181,7 +201,7 @@ fn durable_publication(pull_request: bool, live: bool, leading_unknowns: bool) { ) .unwrap(); } - if leading_unknowns { + if twenty { settings["partition"]["residues"] = json!((0..12).collect::>()); } fs::write(&config, settings.to_string()).unwrap(); @@ -191,6 +211,7 @@ fn durable_publication(pull_request: bool, live: bool, leading_unknowns: bool) { r#"#!/bin/sh [ "$GH_TOKEN" = synthetic ] || exit 93 printf 'read\n' >> "$0.calls" +printf '%s\n' "$*" >> "$0.queries" printf 'HTTP/2.0 200 OK\r\n\r\n' for context in "$0.context-"*; do number="${context##*-}" @@ -277,7 +298,7 @@ cat "$0.reply" ("headRefOid", json!("b".repeat(40))), ("isCrossRepository", json!(true)), ] { - if leading_unknowns && field == "isCrossRepository" { + if twenty && field == "isCrossRepository" { continue; } let mut response: serde_json::Value = serde_json::from_slice(&original).unwrap(); @@ -325,34 +346,63 @@ cat "$0.reply" } fs::write(&reply, original).unwrap(); } + let mut preserved = BTreeMap::new(); if live { - let leading: Vec = if leading_unknowns { + let numbers: Vec = if twenty { (1..=11).chain(22..=30).collect() } else { - claims - .import_snapshot( - Some(ManualAuthority::Owner), - &BTreeMap::from([( - 22, + vec![22] + }; + if leading != Some(Leading::Unknown) { + let incoming = numbers + .iter() + .map(|number| { + let state = if leading == Some(Leading::RetainedLeases) { + AttemptState { + utc_day: now / 86_400, + attempts: 1, + lease: Some(Lease { + holder: "oxa:peer/helper".into(), + generation: (now / 86_400, 1), + claimed_at: Some(now - 60), + expires_at: now - 30, + }), + } + } else { + AttemptState::default() + }; + ( + *number, ImportState { - state: AttemptState::default(), + state, excluded: false, }, - )]), - now, - ) + ) + }) + .collect(); + claims + .import_snapshot(Some(ManualAuthority::Owner), &incoming, now) .unwrap(); - vec![22] - }; + let rows = claims.assignments().unwrap(); + for number in &numbers { + assert!(!rows[number].data.import_hold); + assert!(rows[number].data.item_owner.is_none()); + preserved.insert(*number, serde_json::to_value(&rows[number]).unwrap()); + } + } let reply = env.tmp().join("gh.reply"); let mut response: serde_json::Value = serde_json::from_slice(&fs::read(&reply).unwrap()).unwrap(); let repository = &mut response["data"]["repository"]; let row_template = repository["pullRequests"]["nodes"][0].clone(); let pr_template = repository["p44"].clone(); - for number in &leading { + for number in &numbers { let mut row = row_template.clone(); row["number"] = json!(number); - row["headRefName"] = json!(format!("agent/{number}")); + row["headRefName"] = if leading == Some(Leading::InvalidBranch) { + json!("-blocked") + } else { + json!(format!("agent/{number}")) + }; repository["pullRequests"]["nodes"] .as_array_mut() .unwrap() @@ -361,12 +411,18 @@ cat "$0.reply" pr["number"] = json!(number); repository[format!("p{number}")] = pr; } - repository["pullRequests"]["totalCount"] = json!(leading.len() + 1); + repository["pullRequests"]["totalCount"] = json!(numbers.len() + 1); fs::write(&reply, response.to_string()).unwrap(); - for number in leading { + for number in numbers { let mut context = response.clone(); - context["data"]["repository"]["pullRequest"]["headRefName"] = json!(format!("agent/{number}")); - context["data"]["repository"]["pullRequest"]["isCrossRepository"] = json!(!leading_unknowns); + context["data"]["repository"]["pullRequest"]["headRefName"] = + if leading == Some(Leading::InvalidBranch) { + json!("-blocked") + } else { + json!(format!("agent/{number}")) + }; + context["data"]["repository"]["pullRequest"]["isCrossRepository"] = + json!(leading == Some(Leading::Fork)); fs::write( env.tmp().join(format!("gh.context-{number}")), context.to_string(), @@ -404,10 +460,20 @@ cat "$0.reply" format!("board-{}.txt", binding.key) ); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); - if live && !leading_unknowns { - let fork = &claims.snapshot().unwrap()[&22]; - assert_eq!(fork.attempts, 0); - assert!(fork.lease.is_none()); + let rows = claims.assignments().unwrap(); + for (number, before) in preserved { + assert_eq!( + serde_json::to_value(&rows[&number]).unwrap(), + before, + "skipped candidates retain their original lease, attempts and assignment" + ); + if leading == Some(Leading::RetainedLeases) { + let queries = fs::read_to_string(env.tmp().join("gh.queries")).unwrap(); + assert!( + !queries.contains(&format!("pullRequest(number:{number}) {{")), + "retained leases are excluded before context acquisition and the packet cap" + ); + } } assert!(invoke(&env, &config, "synthetic\n", false).status.success()); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); From 9baf4a2bc5c3710a8ec153d5a142759799b390c6 Mon Sep 17 00:00:00 2001 From: oxa-agent Date: Wed, 18 Nov 2026 12:18:05 +0100 Subject: [PATCH 11/18] Expose the canonical native branch predicate --- oxa/oxa-work/src/board/claims/native.rs | 28 ++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/oxa/oxa-work/src/board/claims/native.rs b/oxa/oxa-work/src/board/claims/native.rs index e250b1491..6d3d49bcc 100644 --- a/oxa/oxa-work/src/board/claims/native.rs +++ b/oxa/oxa-work/src/board/claims/native.rs @@ -49,6 +49,21 @@ impl NativeBinding { .ok_or_else(|| "invalid native attempt binding".into()) } + /// Check a candidate branch before reservation, without constructing a lease. + pub fn is_valid_branch(branch: &str) -> bool { + !branch.is_empty() + && branch.len() <= 256 + && !branch.starts_with(['-', '/']) + && !branch.ends_with(['/', '.']) + && !branch.contains([' ', '~', '^', ':', '?', '*', '[', '\\']) + && !branch.contains("..") + && !branch.contains("@{") + && !branch.chars().any(char::is_control) + && branch + .split('/') + .all(|part| !part.is_empty() && !part.starts_with('.') && !part.ends_with(".lock")) + } + pub(super) fn valid(&self) -> bool { self.number > 0 && self.key.len() == 64 @@ -65,18 +80,7 @@ impl NativeBinding { self.key, self.lease.generation.0, self.lease.generation.1 ) && crate::valid_task_id(&self.native_task_id) - && !self.branch.is_empty() - && self.branch.len() <= 256 - && !self.branch.starts_with(['-', '/']) - && !self.branch.ends_with(['/', '.']) - && !self.branch.contains([' ', '~', '^', ':', '?', '*', '[', '\\']) - && !self.branch.contains("..") - && !self.branch.contains("@{") - && !self.branch.chars().any(char::is_control) - && self - .branch - .split('/') - .all(|part| !part.is_empty() && !part.starts_with('.') && !part.ends_with(".lock")) + && Self::is_valid_branch(&self.branch) && self .reviewed_head .as_deref() From b9a0ab1bf20b1651fb65a9eb1fc9806e2de25952 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:22:45 +0100 Subject: [PATCH 12/18] fix(board): exclude retained leases and invalid branches before packet admission --- oxa/oxa-cli/src/work_board/refill_input.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index fa1388256..801926d38 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -83,7 +83,11 @@ impl<'a> Input<'a> { let board = partition.assignment_board(claims, self, policy, worker, now)?; let mut prepared = vec![]; for number in board.candidates(&worker.id)? { - if !board.assignments.contains_key(&number) { + if board + .assignments + .get(&number) + .is_none_or(|row| row.state.lease.is_some()) + { continue; } let entry = board @@ -105,6 +109,9 @@ impl<'a> Input<'a> { } let context = oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; + if !NativeBinding::is_valid_branch(&context.branch) { + continue; + } let mut prompt = String::with_capacity(context.prompt.len()); for line in context.prompt.split_inclusive('\n') { if line.trim() == "---" { From 01bc9cfb518ec822ad5765ebaf76c52477c415cd Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:59:55 +0100 Subject: [PATCH 13/18] test(board): reproduce published identities starving live packet preparation --- oxa/oxa-cli/tests/work_refill_cli.rs | 78 ++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index fe6f7d1d9..a8ea5c238 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -126,19 +126,29 @@ fn live_invalid_native_branch_is_skipped_before_reservation() { durable_publication(true, Some(Leading::InvalidBranch)); } +#[test] +fn live_known_work_is_not_starved_by_twenty_published_identities() { + durable_publication(true, Some(Leading::Published)); +} + #[derive(Clone, Copy, PartialEq, Eq)] enum Leading { Fork, Unknown, RetainedLeases, InvalidBranch, + Published, } fn durable_publication(pull_request: bool, leading: Option) { use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState, Lease}; + use sha2::{Digest, Sha256}; use std::collections::BTreeMap; let live = leading.is_some(); - let twenty = matches!(leading, Some(Leading::Unknown | Leading::RetainedLeases)); + let twenty = matches!( + leading, + Some(Leading::Unknown | Leading::RetainedLeases | Leading::Published) + ); let (env, controls) = fixture(); let queue = controls.join("queue"); let root = controls.join("claims"); @@ -347,6 +357,7 @@ cat "$0.reply" fs::write(&reply, original).unwrap(); } let mut preserved = BTreeMap::new(); + let mut published = BTreeMap::new(); if live { let numbers: Vec = if twenty { (1..=11).chain(22..=30).collect() @@ -368,6 +379,12 @@ cat "$0.reply" expires_at: now - 30, }), } + } else if leading == Some(Leading::Published) { + AttemptState { + utc_day: now / 86_400, + attempts: 1, + lease: None, + } } else { AttemptState::default() }; @@ -413,7 +430,7 @@ cat "$0.reply" } repository["pullRequests"]["totalCount"] = json!(numbers.len() + 1); fs::write(&reply, response.to_string()).unwrap(); - for number in numbers { + for (index, number) in numbers.into_iter().enumerate() { let mut context = response.clone(); context["data"]["repository"]["pullRequest"]["headRefName"] = if leading == Some(Leading::InvalidBranch) { @@ -428,6 +445,50 @@ cat "$0.reply" context.to_string(), ) .unwrap(); + if leading == Some(Leading::Published) { + let identity = serde_json::to_vec(&( + "example/project", + number, + &revision, + oxa_work::board::Action::ResolveConflict, + )) + .unwrap(); + let key = format!("{:x}", Sha256::digest(identity)); + let packet = + format!("c{number} local/fake agent/{number} agent/{number}\nSynthetic prior packet.\n"); + let pinned = index % 2 == 0; + assert!(oxa_work::helper_queue::publish( + &queue, + &key, + &packet, + pinned, + &oxa_work::board::Policy { + enabled: true, + ..Default::default() + } + ) + .unwrap()); + let name = format!("{}board-{key}.txt", if pinned { "p1-" } else { "" }); + let ready = queue.join(&name); + // Retained private identities in each supported spool layout. + // These are publication facts, not execution/completion receipts. + let retained = match index % 3 { + 1 => queue.join("started").join(&name), + 2 => { + let directory = queue + .join("started") + .join(format!("native-{}", name.trim_end_matches(".txt"))); + fs::create_dir(&directory).unwrap(); + fs::set_permissions(&directory, fs::Permissions::from_mode(0o700)).unwrap(); + directory.join(format!("board-{key}-{}-1.txt", now / 86_400)) + } + _ => ready.clone(), + }; + if retained != ready { + fs::rename(&ready, &retained).unwrap(); + } + published.insert(retained.clone(), fs::read(retained).unwrap()); + } } } let result = invoke(&env, &config, "synthetic\n", false); @@ -439,7 +500,7 @@ cat "$0.reply" let tasks: Vec<_> = fs::read_dir(&queue) .unwrap() .map(|entry| entry.unwrap().path()) - .filter(|path| path.is_file()) + .filter(|path| path.is_file() && !published.contains_key(path)) .collect(); assert_eq!(tasks.len(), 1); assert_eq!(fs::read_to_string(&tasks[0]).unwrap(), valid); @@ -460,6 +521,13 @@ cat "$0.reply" format!("board-{}.txt", binding.key) ); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); + for (path, bytes) in &published { + assert_eq!( + fs::read(path).unwrap(), + *bytes, + "prior packet bytes remain untouched" + ); + } let rows = claims.assignments().unwrap(); for (number, before) in preserved { assert_eq!( @@ -467,11 +535,11 @@ cat "$0.reply" before, "skipped candidates retain their original lease, attempts and assignment" ); - if leading == Some(Leading::RetainedLeases) { + if matches!(leading, Some(Leading::RetainedLeases | Leading::Published)) { let queries = fs::read_to_string(env.tmp().join("gh.queries")).unwrap(); assert!( !queries.contains(&format!("pullRequest(number:{number}) {{")), - "retained leases are excluded before context acquisition and the packet cap" + "inadmissible identities are excluded before context acquisition and the packet cap" ); } } From f8db93e7e1ebc64c7786e27a5652fbfac01cc61f Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:11:56 +0100 Subject: [PATCH 14/18] Share read-only published packet checks with the spool publisher (#1017) --- oxa/oxa-work/src/helper_queue.rs | 71 ++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/oxa/oxa-work/src/helper_queue.rs b/oxa/oxa-work/src/helper_queue.rs index a651cf370..b38729ae4 100644 --- a/oxa/oxa-work/src/helper_queue.rs +++ b/oxa/oxa-work/src/helper_queue.rs @@ -25,6 +25,36 @@ pub(crate) fn directory(path: &Path) -> Result<()> { Ok(()) } +fn valid_key(key: &str) -> bool { + key.len() == 64 + && key + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} + +fn queue_lock(queue: &Path) -> Result { + directory(queue)?; + let lock = io(File::open(queue))?; + io(lock.lock())?; + Ok(lock) +} + +/// Check whether this key already has a pending or started packet without +/// creating any entries. Publication rechecks under its own spool lock. +pub fn is_published(queue: &Path, key: &str) -> Result { + if !valid_key(key) { + return Err("invalid board queue identity".into()); + } + let _lock = queue_lock(queue)?; + let started = queue.join("started"); + match fs::symlink_metadata(&started) { + Ok(_) => directory(&started)?, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => (), + Err(error) => return io(Err(error)), + } + is_published_locked(queue, &started, key) +} + /// `key` is the lowercase SHA-256 identity of repository, PR, head and action. /// 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. @@ -50,10 +80,7 @@ pub fn publish_reserved( .split_once('\n') .ok_or("queue task needs a header and packet")?; let fields: Vec<_> = header.split_ascii_whitespace().collect(); - if key.len() != 64 - || !key - .bytes() - .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + if !valid_key(key) || task.len() > MAX_TASK_BYTES || task.contains('\0') || body.trim().is_empty() @@ -63,9 +90,7 @@ pub fn publish_reserved( { return Err("invalid board queue identity or bounded task packet".into()); } - directory(queue)?; - let lock = io(File::open(queue))?; - io(lock.lock())?; + let lock = queue_lock(queue)?; let started = queue.join("started"); match fs::DirBuilder::new().mode(0o700).create(&started) { Ok(()) => (), @@ -73,6 +98,21 @@ pub fn publish_reserved( Err(e) => return io(Err(e)), } directory(&started)?; + if is_published_locked(queue, &started, key)? || !reserve()? { + return Ok(false); + } + let prefix = if pinned { "p1-board-" } else { "board-" }; + crate::checkpoint::write_atomic_in( + &lock, + &queue.join(format!("{prefix}{key}.txt")), + task.as_bytes(), + false, + ) + .map_err(|error| format!("board queue storage: {error}"))?; + Ok(true) +} + +fn is_published_locked(queue: &Path, started: &Path, key: &str) -> Result { for prefix in ["board-", "p1-board-"] { let name = format!("{prefix}{key}.txt"); // Check pending before started: the consumer's one-way rename cannot hide both. @@ -82,7 +122,7 @@ pub fn publish_reserved( if !metadata.is_file() || metadata.mode() & 0o077 != 0 { return Err("unsafe existing board queue item".into()); } - return Ok(false); + return Ok(true); } Err(e) if e.kind() == std::io::ErrorKind::NotFound => (), Err(e) => return io(Err(e)), @@ -92,24 +132,13 @@ pub fn publish_reserved( match fs::symlink_metadata(&native) { Ok(_) => { directory(&native)?; - return Ok(false); + return Ok(true); } Err(error) if error.kind() == std::io::ErrorKind::NotFound => (), Err(error) => return io(Err(error)), } } - if !reserve()? { - return Ok(false); - } - let prefix = if pinned { "p1-board-" } else { "board-" }; - crate::checkpoint::write_atomic_in( - &lock, - &queue.join(format!("{prefix}{key}.txt")), - task.as_bytes(), - false, - ) - .map_err(|error| format!("board queue storage: {error}"))?; - Ok(true) + Ok(false) } /// Take the admitted packet into the existing started directory. The native name From 951f7383dac82b2443ba6a7d7fcee897ab2c00eb Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:24:54 +0100 Subject: [PATCH 15/18] fix(board): exclude published identities before the live packet cap --- oxa/oxa-cli/src/work_board/refill.rs | 12 +++++------ oxa/oxa-cli/src/work_board/refill_input.rs | 24 +++++++++++++--------- oxa/oxa-cli/src/work_refill.rs | 11 +++++++--- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 91f55bd02..194fe4204 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -206,12 +206,6 @@ pub(super) fn run( max_attempts_per_day: attempt_limit(&value)?, ..Policy::default() }; - if let Some(model) = model { - packets = Input::new(&gh, &policy.host_path, repo, &token, &[], false) - .with_host(host) - .prepare_live(&claims, &board_policy, &worker, &partition, now, model)?; - } - let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let consumer = Consumer { repository: repo, queue: &queue, @@ -222,6 +216,12 @@ pub(super) fn run( policy: &board_policy, ttl: 1800, }; + if let Some(model) = model { + packets = Input::new(&gh, &policy.host_path, repo, &token, &[], false) + .with_host(host) + .prepare_live(&consumer, now, model)?; + } + let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let inputs: Vec<_> = packets .iter() .map(|p| Packet { diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index 801926d38..f48924dbf 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -1,9 +1,7 @@ //! Read-only prepared-input normalization through the existing board Reader. use super::*; -use crate::work_refill::{Partition, Source}; -use oxa_work::board::{ - assignment::Worker, Action, Claims, Entry, ExistingWork, Issue, NativeBinding, Snapshot, -}; +use crate::work_refill::{Consumer, Source}; +use oxa_work::board::{Action, Entry, ExistingWork, Issue, NativeBinding, Snapshot}; pub(super) fn text<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> { value[key] .as_str() @@ -72,17 +70,20 @@ impl<'a> Input<'a> { /// Generate only current fix packets; Consumer still owns all admission and publication. pub(super) fn prepare_live( &mut self, - claims: &Claims, - policy: &Policy, - worker: &Worker, - partition: &Partition, + consumer: &Consumer<'_>, now: u64, model: &str, ) -> Result, String> { self.host_id()?; - let board = partition.assignment_board(claims, self, policy, worker, now)?; + let board = consumer.partition.assignment_board( + consumer.claims, + self, + consumer.policy, + consumer.worker, + now, + )?; let mut prepared = vec![]; - for number in board.candidates(&worker.id)? { + for number in board.candidates(&consumer.worker.id)? { if board .assignments .get(&number) @@ -102,6 +103,9 @@ impl<'a> Input<'a> { ) { continue; } + if oxa_work::helper_queue::is_published(consumer.queue, &consumer.key(entry)?)? { + continue; + } let fields = oxa_work::pull_request_fields(number)?; let (snapshot, _) = self.reader.repository_read(&fields)?; if snapshot["pullRequest"]["isCrossRepository"] == true { diff --git a/oxa/oxa-cli/src/work_refill.rs b/oxa/oxa-cli/src/work_refill.rs index 6fd63acb4..7c3ebffbc 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -89,6 +89,13 @@ pub struct Consumer<'a> { pub ttl: u64, } impl Consumer<'_> { + /// Preparation and locked admission share this exact immutable identity. + pub(crate) fn key(&self, entry: &Entry) -> Result { + let identity = serde_json::to_vec(&(self.repository, entry.number, &entry.head, entry.action)) + .map_err(|_| "cannot encode board queue identity")?; + Ok(format!("{:x}", Sha256::digest(identity))) + } + fn paused(&self) -> Result { match fs::symlink_metadata(self.pause) { Ok(_) => Ok(true), @@ -134,9 +141,7 @@ impl Consumer<'_> { 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 key = self.key(entry)?; let mut receipt = None; let published = helper_queue::publish_reserved( self.queue, From d083ea446b6b1788c7a308da2cbc69a4730b05aa Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:33:48 +0100 Subject: [PATCH 16/18] test(board): reproduce rich fork context exhausting live refill reads --- oxa/oxa-cli/tests/work_refill_cli.rs | 42 ++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index a8ea5c238..9fc56e11f 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -111,6 +111,11 @@ fn live_fix_packet_uses_current_app_context_and_the_original_durable_binding() { durable_publication(true, Some(Leading::Fork)); } +#[test] +fn live_fork_context_cannot_exhaust_the_budget_before_eligible_work() { + durable_publication(true, Some(Leading::LargeFork)); +} + #[test] fn live_known_work_is_not_starved_by_twenty_unknown_candidates() { durable_publication(true, Some(Leading::Unknown)); @@ -134,6 +139,7 @@ fn live_known_work_is_not_starved_by_twenty_published_identities() { #[derive(Clone, Copy, PartialEq, Eq)] enum Leading { Fork, + LargeFork, Unknown, RetainedLeases, InvalidBranch, @@ -225,7 +231,12 @@ printf '%s\n' "$*" >> "$0.queries" printf 'HTTP/2.0 200 OK\r\n\r\n' for context in "$0.context-"*; do number="${context##*-}" - case "$*" in *"pullRequest(number:$number) {"*) cat "$context"; exit 0 ;; esac + case "$*" in + *"pullRequest(number:$number) { headRefOid headRefName isCrossRepository }"*) + if [ -f "$0.identity-$number" ]; then cat "$0.identity-$number"; else cat "$context"; fi + exit 0 ;; + *"pullRequest(number:$number) {"*) cat "$context"; exit 0 ;; + esac done cat "$0.reply" "#, @@ -439,7 +450,21 @@ cat "$0.reply" json!(format!("agent/{number}")) }; context["data"]["repository"]["pullRequest"]["isCrossRepository"] = - json!(leading == Some(Leading::Fork)); + json!(matches!(leading, Some(Leading::Fork | Leading::LargeFork))); + if leading == Some(Leading::LargeFork) { + let pr = &context["data"]["repository"]["pullRequest"]; + fs::write( + env.tmp().join(format!("gh.identity-{number}")), + json!({"data":{"repository":{"pullRequest":{ + "headRefOid":pr["headRefOid"],"headRefName":pr["headRefName"], + "isCrossRepository":pr["isCrossRepository"] + }}}}) + .to_string(), + ) + .unwrap(); + // An outside contributor controls rich context, not the fixed identity fields. + context["data"]["repository"]["pullRequest"]["body"] = json!("x".repeat(4 * 1024 * 1024)); + } fs::write( env.tmp().join(format!("gh.context-{number}")), context.to_string(), @@ -542,6 +567,19 @@ cat "$0.reply" "inadmissible identities are excluded before context acquisition and the packet cap" ); } + if leading == Some(Leading::LargeFork) { + let queries = fs::read_to_string(env.tmp().join("gh.queries")).unwrap(); + let identity = + format!("pullRequest(number:{number}) {{ headRefOid headRefName isCrossRepository }}"); + assert!(queries.contains(&identity), "read fixed identity fields first"); + assert!( + queries + .lines() + .filter(|line| line.contains(&format!("pullRequest(number:{number}) {{"))) + .all(|line| line.contains(&identity)), + "fork rich context must never consume the shared reader budget" + ); + } } assert!(invoke(&env, &config, "synthetic\n", false).status.success()); assert_eq!(claims.snapshot().unwrap()[&44].attempts, 1); From 36a3f99e680b8bbb0904fb2844806d6c15f95b75 Mon Sep 17 00:00:00 2001 From: "oxa-agent[bot]" <357576450+oxa-agent[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:07:15 +0100 Subject: [PATCH 17/18] Reject live refill forks before fetching rich context (#1017) --- oxa/oxa-cli/src/work_board/refill_input.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index f48924dbf..381b71ae6 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -106,6 +106,23 @@ impl<'a> Input<'a> { if oxa_work::helper_queue::is_published(consumer.queue, &consumer.key(entry)?)? { continue; } + // Fork-controlled rich text must not consume the shared snapshot budget. + let fields = + format!("pullRequest(number:{number}) {{ headRefOid headRefName isCrossRepository }}"); + let (identity, _) = self.reader.repository_read(&fields)?; + let identity = &identity["pullRequest"]; + match identity["isCrossRepository"].as_bool() { + Some(true) => continue, + Some(false) => {} + None => return Err("missing pull request repository identity".into()), + } + if identity["headRefOid"].as_str() != Some(entry.head.as_str()) { + return Err("pull request changed since the reviewed head".into()); + } + let branch = text(identity, "headRefName")?; + if !NativeBinding::is_valid_branch(branch) { + continue; + } let fields = oxa_work::pull_request_fields(number)?; let (snapshot, _) = self.reader.repository_read(&fields)?; if snapshot["pullRequest"]["isCrossRepository"] == true { @@ -113,8 +130,8 @@ impl<'a> Input<'a> { } let context = oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; - if !NativeBinding::is_valid_branch(&context.branch) { - continue; + if context.branch != branch { + return Err("pull request branch changed during context acquisition".into()); } let mut prompt = String::with_capacity(context.prompt.len()); for line in context.prompt.split_inclusive('\n') { From 58cac817940ecd592900a6078d9905066ead5680 Mon Sep 17 00:00:00 2001 From: "oxa-agent[bot]" <357576450+oxa-agent[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:47:53 +0100 Subject: [PATCH 18/18] Bound rich PR context per refill candidate (#1017) --- oxa/docs/work.md | 6 + oxa/oxa-cli/src/work_board.rs | 131 +++++++++++++++++---- oxa/oxa-cli/src/work_board/refill.rs | 14 ++- oxa/oxa-cli/src/work_board/refill_input.rs | 48 +++++--- oxa/oxa-cli/tests/work_refill_cli.rs | 54 +++++++++ oxa/oxa-work/src/lib.rs | 80 +++++++++++-- 6 files changed, 285 insertions(+), 48 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index fd16779f6..1cf4a1cbf 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -336,4 +336,10 @@ The same config supplies the registered worker, host, Claims path, queue, pause marker and item partition. Each round reads current assignment candidates and PR context through the explicit App credential, then uses the existing admission and publication path. A changed head or action cannot publish the prepared packet. +Set `pr_context_capture_bytes` in the same protected config to limit each live +PR context read. It defaults to 2 MiB within the unchanged 4 MiB round budget. +The reservation includes one byte to detect overflow. When the limit is reached, +refill logs the cause and prepares the packet with its verified identity and a +truncation notice. If the remaining round budget cannot verify a later identity, +refill keeps the packets already prepared and stops preparing further candidates. This mode does not execute a worker or select a default host, model or Claims store. diff --git a/oxa/oxa-cli/src/work_board.rs b/oxa/oxa-cli/src/work_board.rs index 1eede5ae1..8b6893d75 100644 --- a/oxa/oxa-cli/src/work_board.rs +++ b/oxa/oxa-cli/src/work_board.rs @@ -59,6 +59,7 @@ fn charge(bytes: &mut usize, read: usize) -> Result<(), String> { enum QueryError { Http(u16), + ContextLimit, Other(&'static str), } @@ -131,11 +132,23 @@ impl QueryError { fn message(self) -> String { match self { Self::Http(status) => format!("GitHub read failed (HTTP {status})"), + Self::ContextLimit => "GitHub PR context capture limit reached".into(), Self::Other(message) => message.into(), } } } +fn http_status(headers: &str) -> Result { + 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(QueryError::Other("invalid GitHub HTTP status")) +} + 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 { @@ -162,7 +175,15 @@ impl<'a> Reader<'a> { /// 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) + self.query(fields, None).map_err(QueryError::message) + } + + fn repository_context(&self, fields: &str, limit: usize) -> Result, String> { + match self.query(fields, Some(limit)) { + Ok((value, _)) => Ok(Some(value)), + Err(QueryError::ContextLimit) => Ok(None), + Err(error) => Err(error.message()), + } } fn listing(&self) -> Result<(Value, Value), SnapshotError> { @@ -209,10 +230,10 @@ impl<'a> Reader<'a> { } } - fn query(&self, fields: &str) -> Result<(Value, usize), QueryError> { + fn query(&self, fields: &str, context_limit: Option) -> Result<(Value, usize), QueryError> { let mut retries = 0; loop { - match self.query_once(fields) { + match self.query_once(fields, context_limit) { Err(error) if error.retryable() && retries < 2 => { let pause = Duration::from_millis(200 << retries); if self.time_left()? <= pause { @@ -226,16 +247,13 @@ impl<'a> Reader<'a> { } } - fn query_once(&self, fields: &str) -> Result<(Value, usize), QueryError> { + fn query_once(&self, fields: &str, context_limit: Option) -> 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 = self.api_command(); command.args(["--include", "graphql", "-f", &format!("query={query}")]); - let (remaining, duration) = self.read_budget()?; - let (code, text) = oxa_work::command_runner(command, Some(remaining), duration).ok_or(invalid( - "GitHub read failed: snapshot exceeds byte limit, capture refused or deadline reached", - ))?; + let (code, text) = self.capture_query(command, context_limit)?; // Charge captured bytes, including headers and non-2xx bodies, before interpreting them. self.charge_capture(text.len())?; let (status, body) = self.response(&text, GitHubResource::Graphql)?; @@ -262,20 +280,60 @@ impl<'a> Reader<'a> { Ok((repository, text.len())) } + fn capture_query( + &self, + command: Command, + context_limit: Option, + ) -> Result<(i32, String), QueryError> { + use oxa_work::BoundedOutput; + use std::os::unix::process::ExitStatusExt; + + let failed = QueryError::Other( + "GitHub read failed: snapshot exceeds byte limit, capture refused or deadline reached", + ); + let Some(limit) = context_limit else { + let (remaining, duration) = self.read_budget()?; + return oxa_work::command_runner(command, Some(remaining), duration).ok_or(failed); + }; + let duration = oxa_work::COMMAND_DEADLINE.min(self.time_left()?); + let reservation = limit.min(self.remaining_capture()); + if reservation < b"HTTP/1.1 200 OK\r\n".len() { + return Err(QueryError::ContextLimit); + } + match oxa_work::command_output_reserved(command, reservation, duration).map_err(|_| failed)? { + BoundedOutput::Complete(output) => Ok(( + output.status.code().unwrap_or(1), + String::from_utf8(output.stdout).map_err(|_| QueryError::Other("invalid GitHub response"))?, + )), + BoundedOutput::Limit(output) => { + self.charge_capture(output.stdout.len())?; + // Use only the transport status. A partial JSON response is never a snapshot. + let status = output + .stdout + .split_inclusive(|&byte| byte == b'\n') + .next() + .filter(|line| line.ends_with(b"\n")) + .and_then(|line| std::str::from_utf8(line).ok()) + .ok_or(QueryError::Other("missing GitHub HTTP status"))?; + let status = http_status(status)?; + if !(200..300).contains(&status) { + return Err(QueryError::Http(status)); + } + if !output.status.success() && output.status.signal() != Some(signal_hook::consts::SIGPIPE) { + return Err(QueryError::Other("GitHub context command failed")); + } + Err(QueryError::ContextLimit) + } + } + } + fn response<'b>(&self, text: &'b str, resource: GitHubResource) -> Result<(u16, &'b str), QueryError> { let invalid = QueryError::Other; 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"))?; + let status = http_status(headers)?; if let Some(quota) = &self.app_quota { let headers = if (200..300).contains(&status) { headers } else { "" }; quota.borrow_mut().observe(GitHubIdentity::App, resource, headers); @@ -284,13 +342,17 @@ impl<'a> Reader<'a> { } fn read_budget(&self) -> Result<(usize, Duration), QueryError> { - let remaining = MAX_RESPONSE - .checked_sub(self.bytes.get()) - .filter(|n| *n > 0) - .ok_or(QueryError::Other("GitHub snapshot exceeds byte limit"))?; + let remaining = self.remaining_capture(); + if remaining == 0 { + return Err(QueryError::Other("GitHub snapshot exceeds byte limit")); + } Ok((remaining, oxa_work::COMMAND_DEADLINE.min(self.time_left()?))) } + fn remaining_capture(&self) -> usize { + MAX_RESPONSE.saturating_sub(self.bytes.get()) + } + fn charge_capture(&self, bytes: usize) -> Result<(), QueryError> { let mut used = self.bytes.get(); charge(&mut used, bytes).map_err(|_| QueryError::Other("GitHub snapshot exceeds byte limit"))?; @@ -422,7 +484,7 @@ impl Reader<'_> { }}}}}}}}}}}}}}"# )); } - let (page, _) = match self.query(&fields) { + let (page, _) = match self.query(&fields, None) { Ok(page) => page, Err(error) if error.retryable() && end - offset > 1 => { size = (end - offset) / 2; @@ -735,6 +797,33 @@ mod deadline_tests { use oxa_fake::TestEnv; use std::os::unix::fs::PermissionsExt; + #[test] + fn context_capture_charges_truncation_and_preserves_http_refusal() { + let reader = Reader::new( + Path::new("/bin/false"), + std::ffi::OsStr::new("/usr/bin:/bin"), + "example/repo", + "synthetic-app-token", + ); + let mut accepted = Command::new("/bin/sh"); + accepted.args(["-c", "printf 'HTTP/2.0 200 OK\\r\\n\\r\\nOrdinary project notes'"]); + assert!(matches!( + reader.capture_query(accepted, Some(32)), + Err(QueryError::ContextLimit) + )); + assert_eq!(reader.bytes.get(), 32); + let mut refused = Command::new("/bin/sh"); + refused.args([ + "-c", + "printf 'HTTP/2.0 403 Forbidden\\r\\n\\r\\nAccess unavailable'; exit 1", + ]); + assert!(matches!( + reader.capture_query(refused, Some(32)), + Err(QueryError::Http(403)) + )); + assert_eq!(reader.bytes.get(), 64); + } + #[test] fn exhausted_budget_refuses_launch_and_remaining_time_bounds_the_child() { let test = TestEnv::new(); diff --git a/oxa/oxa-cli/src/work_board/refill.rs b/oxa/oxa-cli/src/work_board/refill.rs index 194fe4204..dea25a374 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -126,7 +126,15 @@ pub(super) fn run( if model.is_empty() || model.bytes().any(|b| b.is_ascii_whitespace() || b == 0) { return Err("live model must be one task header field".into()); } - Some(model) + let capture_bytes = match value.get("pr_context_capture_bytes") { + Some(limit) => limit + .as_u64() + .filter(|&bytes| (1..=MAX_RESPONSE as u64).contains(&bytes)) + .ok_or("pr_context_capture_bytes must be a positive integer within the snapshot budget")? + as usize, + None => MAX_RESPONSE / 2, + }; + Some((model, capture_bytes)) } else { None }; @@ -216,10 +224,10 @@ pub(super) fn run( policy: &board_policy, ttl: 1800, }; - if let Some(model) = model { + if let Some((model, capture_bytes)) = model { packets = Input::new(&gh, &policy.host_path, repo, &token, &[], false) .with_host(host) - .prepare_live(&consumer, now, model)?; + .prepare_live(&consumer, now, model, capture_bytes)?; } let mut source = Input::new(&gh, &policy.host_path, repo, &token, &packets, intake).with_host(host); let inputs: Vec<_> = packets diff --git a/oxa/oxa-cli/src/work_board/refill_input.rs b/oxa/oxa-cli/src/work_board/refill_input.rs index 381b71ae6..7731ac783 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -73,6 +73,7 @@ impl<'a> Input<'a> { consumer: &Consumer<'_>, now: u64, model: &str, + capture_bytes: usize, ) -> Result, String> { self.host_id()?; let board = consumer.partition.assignment_board( @@ -109,7 +110,10 @@ impl<'a> Input<'a> { // Fork-controlled rich text must not consume the shared snapshot budget. let fields = format!("pullRequest(number:{number}) {{ headRefOid headRefName isCrossRepository }}"); - let (identity, _) = self.reader.repository_read(&fields)?; + let Some(identity) = self.reader.repository_context(&fields, MAX_RESPONSE)? else { + eprintln!("oxa work board refill: PR #{number} identity unavailable within the remaining capture budget"); + break; + }; let identity = &identity["pullRequest"]; match identity["isCrossRepository"].as_bool() { Some(true) => continue, @@ -124,29 +128,45 @@ impl<'a> Input<'a> { continue; } let fields = oxa_work::pull_request_fields(number)?; - let (snapshot, _) = self.reader.repository_read(&fields)?; - if snapshot["pullRequest"]["isCrossRepository"] == true { - continue; - } - let context = - oxa_work::prepare_pull_request_context(number, &entry.head, &snapshot["pullRequest"])?; - if context.branch != branch { - return Err("pull request branch changed during context acquisition".into()); - } - let mut prompt = String::with_capacity(context.prompt.len()); - for line in context.prompt.split_inclusive('\n') { + let context = match self.reader.repository_context(&fields, capture_bytes)? { + Some(snapshot) => { + if snapshot["pullRequest"]["isCrossRepository"] == true { + continue; + } + let context = oxa_work::prepare_pull_request_context( + number, + &entry.head, + &snapshot["pullRequest"], + )?; + if context.branch != branch { + return Err("pull request branch changed during context acquisition".into()); + } + context.prompt + } + None => { + eprintln!("oxa work board refill: PR #{number} context truncated at the capture limit"); + format!( + "{}Address pull request #{number} at {}.\n\n\ + PR context truncated at the capture limit. Description, discussion, \ + changed files, and check details are unavailable.\n", + oxa_work::github_runner_guidance("pull request"), + entry.head, + ) + } + }; + let mut prompt = String::with_capacity(context.len()); + for line in context.split_inclusive('\n') { if line.trim() == "---" { prompt.push('\\'); } prompt.push_str(line); } - let branch = context.branch; prepared.push(Prepared { number, revision: entry.head.clone(), action: entry.action, task: format!("c{number} {model} {branch} {branch}\n{prompt}"), - branch, + branch: branch.into(), }); if prepared.len() == 20 { break; diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index 9fc56e11f..578e6a3dd 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -116,6 +116,11 @@ fn live_fork_context_cannot_exhaust_the_budget_before_eligible_work() { durable_publication(true, Some(Leading::LargeFork)); } +#[test] +fn live_context_limit_notices_truncation_and_keeps_later_work_eligible() { + durable_publication(true, Some(Leading::LimitedContext)); +} + #[test] fn live_known_work_is_not_starved_by_twenty_unknown_candidates() { durable_publication(true, Some(Leading::Unknown)); @@ -140,6 +145,7 @@ fn live_known_work_is_not_starved_by_twenty_published_identities() { enum Leading { Fork, LargeFork, + LimitedContext, Unknown, RetainedLeases, InvalidBranch, @@ -151,6 +157,7 @@ fn durable_publication(pull_request: bool, leading: Option) { use sha2::{Digest, Sha256}; use std::collections::BTreeMap; let live = leading.is_some(); + let limited = leading == Some(Leading::LimitedContext); let twenty = matches!( leading, Some(Leading::Unknown | Leading::RetainedLeases | Leading::Published) @@ -220,6 +227,9 @@ fn durable_publication(pull_request: bool, leading: Option) { if twenty { settings["partition"]["residues"] = json!((0..12).collect::>()); } + if limited { + settings["pr_context_capture_bytes"] = json!(4096); + } fs::write(&config, settings.to_string()).unwrap(); fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); let gh = env.write( @@ -465,6 +475,10 @@ cat "$0.reply" // An outside contributor controls rich context, not the fixed identity fields. context["data"]["repository"]["pullRequest"]["body"] = json!("x".repeat(4 * 1024 * 1024)); } + if limited { + context["data"]["repository"]["pullRequest"]["body"] = + json!("Additional project notes. ".repeat(200)); + } fs::write( env.tmp().join(format!("gh.context-{number}")), context.to_string(), @@ -516,6 +530,46 @@ cat "$0.reply" } } } + if limited { + let queries_path = env.tmp().join("gh.queries"); + fs::write(&queries_path, "").unwrap(); + let result = invoke(&env, &config, "synthetic\n", false); + assert!(result.status.success()); + let notice = "PR #22 context truncated at the capture limit"; + assert!(String::from_utf8_lossy(&result.stderr).contains(notice)); + let queries = fs::read_to_string(queries_path).unwrap(); + assert!(queries + .lines() + .any(|line| line.contains("pullRequest(number:44) {") + && !line.contains("headRefOid headRefName isCrossRepository"))); + let binding = claims.assignments().unwrap()[&22] + .data + .native_binding + .clone() + .unwrap(); + assert_eq!(binding.reviewed_head.as_deref(), Some(revision.as_str())); + let ready = queue.join(format!("board-{}.txt", binding.key)); + let packet = fs::read_to_string(&ready).unwrap(); + assert!(packet.starts_with("c22 local/fake agent/22 agent/22\n")); + assert!(packet.contains("PR context truncated at the capture limit")); + assert!(!packet.contains("Additional project notes")); + assert_eq!(claims.snapshot().unwrap()[&22].attempts, 1); + assert_eq!(claims.snapshot().unwrap()[&44].attempts, 0); + assert!(claims + .release_for( + 22, + &binding.lease.holder, + &binding.lease, + binding.lease.claimed_at.unwrap(), + "fixture completed", + ) + .unwrap()); + published.insert(ready, packet.into_bytes()); + preserved.insert( + 22, + serde_json::to_value(&claims.assignments().unwrap()[&22]).unwrap(), + ); + } let result = invoke(&env, &config, "synthetic\n", false); assert!( result.status.success(), diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 02cbad221..c5f2cf6a4 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -451,6 +451,24 @@ pub fn command_output_bytes( Some((out.status.code().unwrap_or(1), out.stdout)) } +pub enum BoundedOutput { + Complete(Output), + Limit(Output), +} + +/// Capture at most `reservation` bytes, including the byte that detects a limit. +/// Only callers that explicitly handle a limit can inspect the partial output. +pub fn command_output_reserved( + command: Command, + reservation: usize, + deadline: Duration, +) -> io::Result { + let limit = reservation + .checked_sub(1) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "empty command output reservation"))?; + capture_output(command, Some(limit), deadline, Stdio::null(), false) +} + /// Capture through the same deadline and byte limit while a supervised child holds a supplied stdin. pub fn command_runner_with_stdin( command: Command, @@ -465,28 +483,45 @@ pub fn command_runner_with_stdin( )) } +#[derive(Default)] +struct CapturedPipe { + bytes: Vec, + limited: bool, +} + fn read_output( pipe: impl Read + Send + 'static, limit: Option, -) -> std::thread::JoinHandle>> { +) -> std::thread::JoinHandle> { std::thread::spawn(move || { let ceiling = limit.map_or(u64::MAX, |limit| limit.saturating_add(1) as u64); let mut bytes = Vec::new(); pipe.take(ceiling).read_to_end(&mut bytes)?; - if limit.is_some_and(|limit| bytes.len() > limit) { - return Err(io::Error::other("command output exceeds limit")); - } - Ok(bytes) + let limited = limit.is_some_and(|limit| bytes.len() > limit); + Ok(CapturedPipe { bytes, limited }) }) } fn capture_command( - mut command: Command, + command: Command, limit: Option, deadline: Duration, stdin: Stdio, keep_stderr: bool, ) -> io::Result { + match capture_output(command, limit, deadline, stdin, keep_stderr)? { + BoundedOutput::Complete(output) => Ok(output), + BoundedOutput::Limit(_) => Err(io::Error::other("command output exceeds limit")), + } +} + +fn capture_output( + mut command: Command, + limit: Option, + deadline: Duration, + stdin: Stdio, + keep_stderr: bool, +) -> io::Result { let start = Instant::now(); let timeout = || io::Error::new(io::ErrorKind::TimedOut, "command timed out"); if deadline.is_zero() { @@ -527,15 +562,23 @@ fn capture_command( let _ = child.wait(); } let status = status?; - let collected = |reader: std::thread::JoinHandle>>| { + let collected = |reader: std::thread::JoinHandle>| { reader .join() .map_err(|_| io::Error::other("command output reader failed"))? }; - Ok(Output { + let stdout = collected(stdout)?; + let stderr = stderr.map(collected).transpose()?.unwrap_or_default(); + let limited = stdout.limited || stderr.limited; + let output = Output { status, - stdout: collected(stdout)?, - stderr: stderr.map(collected).transpose()?.unwrap_or_default(), + stdout: stdout.bytes, + stderr: stderr.bytes, + }; + Ok(if limited { + BoundedOutput::Limit(output) + } else { + BoundedOutput::Complete(output) }) } @@ -1371,6 +1414,23 @@ mod tests { assert_eq!(command_runner(command(), Some(2), deadline), None); } + #[test] + fn reserved_command_output_accounts_for_the_limit_detection_byte() { + let command = || { + let mut command = Command::new("/bin/sh"); + command.args(["-c", "printf abc"]); + command + }; + let complete = command_output_reserved(command(), 4, COMMAND_DEADLINE).unwrap(); + assert!(matches!(complete, BoundedOutput::Complete(output) if output.stdout == b"abc")); + let limited = command_output_reserved(command(), 3, COMMAND_DEADLINE).unwrap(); + assert!(matches!(limited, BoundedOutput::Limit(output) if output.stdout == b"abc")); + assert!(matches!( + command_output_reserved(command(), 3, Duration::ZERO), + Err(error) if error.kind() == io::ErrorKind::TimedOut + )); + } + #[test] fn command_deadline_cleans_up_a_descendant_holding_stdout() { let fixture = test_dir::PrivateDir::new("command-deadline");