From 4da0504254aba7609cb35996169887243005e286 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 02:10:36 +0100 Subject: [PATCH 1/2] Count every queue directory entry against a hard limit The queue scan applied MAX_QUEUE_FILES only to visible regular .json inputs; hidden, non-JSON and non-regular entries were read and skipped without limit on every watch iteration. Each entry returned by read_dir now counts against MAX_QUEUE_ENTRIES (4096) before filtering, and the scan stops with an error past it. Co-Authored-By: Claude Opus 5.5 --- oxa/oxa-work/src/queue.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-work/src/queue.rs b/oxa/oxa-work/src/queue.rs index 28a64cd9..20d5fe2f 100644 --- a/oxa/oxa-work/src/queue.rs +++ b/oxa/oxa-work/src/queue.rs @@ -144,6 +144,9 @@ fn free_space(repo: &Path, common: &Path, queue: &Path, df: &Path, policy: &RunP const MAX_INPUT_BYTES: u64 = 1024 * 1024; const MAX_INPUT_TASKS: usize = 128; const MAX_QUEUE_FILES: usize = 256; +/// Every directory entry counts, including hidden, non-JSON and non-regular ones, +/// so ignored entries cannot make each watch iteration unbounded. +const MAX_QUEUE_ENTRIES: usize = 4096; const MAX_QUEUE_BYTES: u64 = 16 * 1024 * 1024; const MAX_RETAINED_TASKS: usize = 4096; @@ -253,7 +256,12 @@ fn scan( ) -> Result<()> { let mut files = vec![]; let mut bytes = 0; - for entry in fs::read_dir(directory).map_err(|e| e.to_string())? { + for (index, entry) in fs::read_dir(directory).map_err(|e| e.to_string())?.enumerate() { + if index == MAX_QUEUE_ENTRIES { + return Err(format!( + "queue directory exceeds {MAX_QUEUE_ENTRIES} entries; move other files out of it" + )); + } let entry = match entry { Ok(entry) => entry, Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue, @@ -751,6 +759,34 @@ mod tests { assert!(inputs.tasks.is_empty()); } + #[test] + fn queue_counts_ignored_entries_against_the_directory_limit() { + let dir = crate::test_dir::PrivateDir::new("queue-entry-limit"); + for i in 0..MAX_QUEUE_ENTRIES { + let name = match i % 3 { + 0 => format!(".hidden-{i}.json"), + 1 => format!("notes-{i}.txt"), + _ => format!("dir-{i}.json"), + }; + if i % 3 == 2 { + fs::create_dir(dir.path().join(name)).unwrap(); + } else { + fs::write(dir.path().join(name), "").unwrap(); + } + } + fs::write( + dir.path().join("task.json"), + r#"[{"id":"task","branch":"agent/task","model":"fake","prompt":"Synthetic task"}]"#, + ) + .unwrap(); + let mut pending = vec![]; + let mut inputs = Inputs::default(); + let result = scan(dir.path(), &mut pending, &mut inputs, &mut |_| Ok(())); + assert!(result.unwrap_err().contains("4096 entries")); + assert!(pending.is_empty()); + assert!(inputs.tasks.is_empty()); + } + #[test] fn queue_rejects_aggregate_input_bytes_before_admission() { let dir = crate::test_dir::PrivateDir::new("queue-input-budget"); From bf2f1dad0bf177cbd29dcf343db26c4e43bd10e7 Mon Sep 17 00:00:00 2001 From: oxa agent <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 02:13:33 +0100 Subject: [PATCH 2/2] Hand a native attempt's branch to the next attempt on that branch Each native board admission gets its own task id (it includes the lease generation) and therefore its own worktree. A later attempt on the same branch could not add its worktree while the finished earlier attempt still had the branch checked out. The reviewed-PR handoff now also covers native attempts: when the branch is attached to a finished, clean native attempt's worktree in the same worktree directory, that worktree's HEAD is detached at the branch tip and the new attempt checks out the branch. The earlier worktree's files and receipt are unchanged. Co-Authored-By: Claude Opus 5.5 --- oxa/oxa-cli/tests/work_native_handoff.rs | 96 ++++++++++++++++++++++++ oxa/oxa-work/src/tree.rs | 72 ++++++++++++++---- 2 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 oxa/oxa-cli/tests/work_native_handoff.rs diff --git a/oxa/oxa-cli/tests/work_native_handoff.rs b/oxa/oxa-cli/tests/work_native_handoff.rs new file mode 100644 index 00000000..702558da --- /dev/null +++ b/oxa/oxa-cli/tests/work_native_handoff.rs @@ -0,0 +1,96 @@ +//! A later native board attempt continues the branch that a finished attempt left checked out. +#[path = "common/work_repo.rs"] +mod work_repo; +use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +use std::{path::PathBuf, process::Command}; +use work_repo::setup; + +const CHILD: &str = "OXA_NATIVE_HANDOFF_TEST_ROOT"; +const BRANCH: &str = "agent/native"; + +fn attempt(ordinal: u32) -> oxa_work::Task { + let lease = oxa_work::board::Lease { + holder: "codex:node/slot-1".into(), + expires_at: 2_000_000_000, + claimed_at: Some(1_000_000_000), + generation: (20_000, ordinal), + }; + let binding = oxa_work::board::NativeBinding::new(7, &"a".repeat(64), &lease, BRANCH).unwrap(); + let task: oxa_work::Task = serde_json::from_value(serde_json::json!({ + "id": "i7", "model": "fake", "branch": BRANCH, "base": "main", "prompt": "Synthetic native task" + })) + .unwrap(); + oxa_work::native_task(&binding, task).unwrap() +} + +fn run_attempts(root: PathBuf) { + let project = root.join("work/project"); + let policy = oxa_work::RunPolicy { + git_binary: "/usr/bin/git".into(), + host_path: std::env::var_os("PATH").unwrap(), + terminate_binary: Some("/usr/bin/kill".into()), + runner_owns_github: false, + offline: false, + storage: root.join("home/.oxa/agent/work/native-handoff"), + }; + let binary = std::path::Path::new(env!("CARGO_BIN_EXE_oxa")); + let mut records = vec![]; + for ordinal in [1, 2] { + let task = attempt(ordinal); + let plan = oxa_work::plan_task(&project, task, &policy).unwrap(); + let record = oxa_work::run_task_with_policy(&project, binary, &plan, &policy).unwrap(); + assert_eq!(record.outcome, oxa_work::Outcome::Done, "{record:?}"); + records.push(record); + } + assert_ne!(records[0].id, records[1].id); + assert_ne!(records[0].worktree, records[1].worktree); + let show = |rev: &str| { + let out = Command::new("/usr/bin/git") + .current_dir(&project) + .args(["show", rev]) + .output() + .unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + String::from_utf8(out.stdout).unwrap() + }; + assert_eq!(show(&format!("{BRANCH}:first.txt")), "first attempt"); + assert_eq!(show(&format!("{BRANCH}:second.txt")), "second attempt"); + // The finished attempt keeps its files; only its HEAD was detached. + assert!(records[0].worktree.join("first.txt").exists()); +} + +#[test] +fn a_later_native_attempt_continues_the_branch_of_a_finished_attempt() { + if let Some(root) = std::env::var_os(CHILD) { + run_attempts(PathBuf::from(root)); + return; + } + let env = TestEnv::new(); + setup(&env, "[]"); + let write = |path: &str, content: &str| { + let args = serde_json::json!({"path": path, "content": content}).to_string(); + [ + ScriptTurn::tool("write", "write", &args), + ScriptTurn::text("done"), + ] + }; + let mut turns = write("first.txt", "first attempt").to_vec(); + turns.extend(write("second.txt", "second attempt")); + let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); + let out = Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "a_later_native_attempt_continues_the_branch_of_a_finished_attempt", + ]) + .env_clear() + .envs(env.envs(&server.url())) + .env(CHILD, env.root()) + .output() + .unwrap(); + assert!( + out.status.success(), + "{} / {}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); +} diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs index ce1fd0f1..7a06dd05 100644 --- a/oxa/oxa-work/src/tree.rs +++ b/oxa/oxa-work/src/tree.rs @@ -271,12 +271,26 @@ pub(crate) fn validate(policy: &RunPolicy, repo: &Path, tree: &Path, task: &Task Ok(()) } -/// Preserve an earlier, finished round in place while releasing its branch for this round. -fn release_review_branch( +/// The earlier task that may hand its checked-out branch to this one. +#[derive(Clone, Copy)] +enum Lineage<'a> { + /// An earlier reviewed round of the same pull request; the branch is at `reviewed`. + Review(&'a str), + /// An earlier native board attempt on the same branch. Each admission has its + /// own task id, and therefore its own worktree. + Native, +} + +fn native_attempt(task: &Task) -> bool { + task.id.starts_with("board-") && task.runner_owns_github() +} + +/// Preserve an earlier, finished task in place while releasing its branch for this one. +fn release_prior_branch( repo: &Path, tree: &Path, task: &Task, - reviewed: &str, + lineage: Lineage<'_>, binary: &Path, policy: &RunPolicy, ) -> Result<()> { @@ -286,35 +300,59 @@ fn release_review_branch( repo, &["for-each-ref", "--format=%(refname)%00%(worktreepath)", &branch], ))?; + if attached.is_empty() { + // A first native attempt creates the branch. + return Ok(()); + } let path = attached .strip_prefix(&branch) .and_then(|result| result.strip_prefix('\0')) - .ok_or("cannot identify the reviewed branch's worktree")?; + .ok_or("cannot identify the task branch's worktree")?; if path.is_empty() { return Ok(()); } let previous = Path::new(path); - let blocked = "reviewed branch is already checked out outside a finished task for this pull request"; + let blocked = match lineage { + Lineage::Review(_) => { + "reviewed branch is already checked out outside a finished task for this pull request" + } + Lineage::Native => { + "task branch is already checked out outside a finished native attempt for this branch" + } + }; let prior_id = previous .file_name() .and_then(|name| name.to_str()) .ok_or(blocked)?; - let (pull_request, _) = task.id.rsplit_once('-').ok_or(blocked)?; + let same_lineage = match lineage { + Lineage::Review(_) => { + let (pull_request, _) = task.id.rsplit_once('-').ok_or(blocked)?; + prior_id.rsplit_once('-').map(|(pr, _)| pr) == Some(pull_request) + } + Lineage::Native => prior_id.starts_with("board-"), + }; if previous.parent() != tree.parent() || !super::valid_task_id(prior_id) - || prior_id.rsplit_once('-').map(|(pr, _)| pr) != Some(pull_request) + || !same_lineage || prior_id == task.id { return Err(blocked.into()); } let prior_lock = lock_task(policy, prior_id, true)?; let prior = load_task_checkpoint(policy, prior_id)?.ok_or(blocked)?; + let prior_kind = match lineage { + Lineage::Review(_) => { + prior.task.reviewed_head().is_some() + && matches!(prior.record.outcome, Outcome::Done | Outcome::NothingToDo) + } + // A retry follows any finished attempt; its saved work is on the branch. + Lineage::Native => native_attempt(&prior.task), + }; if prior.task.id != prior_id - || prior.task.reviewed_head().is_none() + || !prior_kind || prior.task.branch != task.branch || prior.record.worktree != previous || prior.record.step != Step::Finished - || !matches!(prior.record.outcome, Outcome::Done | Outcome::NothingToDo) { return Err(blocked.into()); } @@ -327,12 +365,16 @@ fn release_review_branch( "REVERT_HEAD", ] { if git_path(policy, previous, operation)?.exists() { - return Err(format!("previous PR worktree has {operation} in progress")); + return Err(format!("previous task worktree has {operation} in progress")); } } if dirty_worktree(binary, previous, repo, policy, &prior_lock)? { - return Err("previous PR worktree has uncommitted changes".into()); + return Err("previous task worktree has uncommitted changes".into()); } + let head = match lineage { + Lineage::Review(reviewed) => reviewed.to_string(), + Lineage::Native => checked(git(policy, previous, &["rev-parse", "--verify", &branch]))?, + }; // Change only the attached HEAD. The index, files, branch ref and prior receipt stay intact. checked(git( policy, @@ -343,8 +385,8 @@ fn release_review_branch( "update-ref", "--no-deref", "HEAD", - reviewed, - reviewed, + &head, + &head, ], ))?; Ok(()) @@ -369,8 +411,10 @@ pub(crate) fn prepare( return Err("local pull request branch is not at the reviewed head".into()); } if !tree.exists() { - release_review_branch(repo, tree, task, reviewed, binary, policy)?; + release_prior_branch(repo, tree, task, Lineage::Review(reviewed), binary, policy)?; } + } else if native_attempt(task) && !tree.exists() { + release_prior_branch(repo, tree, task, Lineage::Native, binary, policy)?; } let retained = tree.exists(); if !retained {