diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 64028ec6d..1cf4a1cbf 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -329,3 +329,17 @@ A saved worker result keeps its launch identity across checkpoint replay. 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. +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 6f5827be9..dea25a374 100644 --- a/oxa/oxa-cli/src/work_board/refill.rs +++ b/oxa/oxa-cli/src/work_board/refill.rs @@ -117,10 +117,36 @@ pub(super) fn run( let claims = oxa_work::board::Claims::open(&owner_path("claims")?)?; let (worker, host) = crate::work_cmd::registered_native_worker(&value)?; let partition = partition(&value)?; - 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()); + } + 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 + }; + 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 { @@ -179,12 +205,11 @@ 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")? .as_secs(); - let policy = Policy { + let board_policy = Policy { enabled: true, max_attempts_per_day: attempt_limit(&value)?, ..Policy::default() @@ -196,9 +221,15 @@ pub(super) fn run( claims: &claims, worker: &worker, partition: &partition, - policy: &policy, + policy: &board_policy, ttl: 1800, }; + 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, capture_bytes)?; + } + 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 bdb7be9bc..7731ac783 100644 --- a/oxa/oxa-cli/src/work_board/refill_input.rs +++ b/oxa/oxa-cli/src/work_board/refill_input.rs @@ -1,6 +1,6 @@ //! Read-only prepared-input normalization through the existing board Reader. use super::*; -use crate::work_refill::Source; +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] @@ -66,6 +66,114 @@ 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, + consumer: &Consumer<'_>, + now: u64, + model: &str, + capture_bytes: usize, + ) -> Result, String> { + self.host_id()?; + let board = consumer.partition.assignment_board( + consumer.claims, + self, + consumer.policy, + consumer.worker, + now, + )?; + let mut prepared = vec![]; + for number in board.candidates(&consumer.worker.id)? { + if board + .assignments + .get(&number) + .is_none_or(|row| row.state.lease.is_some()) + { + continue; + } + 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; + } + 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 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, + 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 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); + } + prepared.push(Prepared { + number, + revision: entry.head.clone(), + action: entry.action, + task: format!("c{number} {model} {branch} {branch}\n{prompt}"), + branch: branch.into(), + }); + 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 f1d4760fd..7c3ebffbc 100644 --- a/oxa/oxa-cli/src/work_refill.rs +++ b/oxa/oxa-cli/src/work_refill.rs @@ -31,6 +31,23 @@ impl Partition { 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 { + self.validate()?; + claims.assignment_board( + &mut Partitioned(source, self), + policy, + std::slice::from_ref(worker), + now, + ) + } + pub(crate) fn validate(&self) -> Result<()> { if self.modulus == 0 || self.residues.is_empty() || self.residues.iter().any(|r| *r >= self.modulus) { return Err("invalid board partition".into()); @@ -72,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), @@ -96,13 +120,9 @@ impl Consumer<'_> { return Ok(vec![]); } let host = source.host_id()?.map(str::to_owned); - self.partition.validate()?; - 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(); @@ -121,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, diff --git a/oxa/oxa-cli/tests/work_refill_cli.rs b/oxa/oxa-cli/tests/work_refill_cli.rs index ccdef35a0..578e6a3dd 100644 --- a/oxa/oxa-cli/tests/work_refill_cli.rs +++ b/oxa/oxa-cli/tests/work_refill_cli.rs @@ -102,13 +102,66 @@ 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, None); } } -fn durable_publication(pull_request: bool) { - use oxa_work::board::{assignment::ManualAuthority, AttemptState, Claims, ImportState}; +#[test] +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_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)); +} + +#[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)); +} + +#[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, + LargeFork, + LimitedContext, + 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 limited = leading == Some(Leading::LimitedContext); + 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"); @@ -140,7 +193,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\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 { + 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,19 +207,47 @@ 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(); + } + 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( "tmp/gh", 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##*-}" + 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" "#, ); @@ -174,6 +261,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\n---\nNext section", + "headRefOid":revision,"headRefName":"agent/44","isCrossRepository":false, + "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":"", "isDraft":false,"reviewDecision":null,"mergeable":"CONFLICTING","mergeStateStatus":"DIRTY", "baseRef":{"refUpdateRule":null,"rules":{"totalCount":0,"nodes":[]}}, @@ -183,18 +275,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 +299,54 @@ 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(); + // 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)), + ] { + if twenty && 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(); + 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() + .all(|entry| entry.unwrap().path().is_dir())); + } + fs::write(&reply, original).unwrap(); + } if !pull_request { let reply = env.tmp().join("gh.reply"); let original = fs::read(&reply).unwrap(); @@ -233,6 +377,199 @@ 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() + } else { + 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 if leading == Some(Leading::Published) { + AttemptState { + utc_day: now / 86_400, + attempts: 1, + lease: None, + } + } else { + AttemptState::default() + }; + ( + *number, + ImportState { + state, + excluded: false, + }, + ) + }) + .collect(); + claims + .import_snapshot(Some(ManualAuthority::Owner), &incoming, now) + .unwrap(); + 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 &numbers { + let mut row = row_template.clone(); + row["number"] = json!(number); + row["headRefName"] = if leading == Some(Leading::InvalidBranch) { + json!("-blocked") + } else { + 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!(numbers.len() + 1); + fs::write(&reply, response.to_string()).unwrap(); + for (index, number) in numbers.into_iter().enumerate() { + let mut context = response.clone(); + context["data"]["repository"]["pullRequest"]["headRefName"] = + if leading == Some(Leading::InvalidBranch) { + json!("-blocked") + } else { + json!(format!("agent/{number}")) + }; + context["data"]["repository"]["pullRequest"]["isCrossRepository"] = + 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)); + } + 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(), + ) + .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()); + } + } + } + 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(), @@ -242,7 +579,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); @@ -263,6 +600,41 @@ 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!( + serde_json::to_value(&rows[&number]).unwrap(), + before, + "skipped candidates retain their original lease, attempts and assignment" + ); + 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}) {{")), + "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); if !pull_request { 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() 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 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");