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/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"); diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs index 42fb6721..77ac89ed 100644 --- a/oxa/oxa-work/src/tree.rs +++ b/oxa/oxa-work/src/tree.rs @@ -328,12 +328,26 @@ fn published_head(policy: &RunPolicy, repo: &Path, branch: &str, base: &str) -> Ok(Some(tip.clone())) } -/// 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<()> { @@ -343,35 +357,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()); } @@ -384,12 +422,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, @@ -400,8 +442,8 @@ fn release_review_branch( "update-ref", "--no-deref", "HEAD", - reviewed, - reviewed, + &head, + &head, ], ))?; Ok(()) @@ -426,8 +468,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 {