From 3936b70cee6f783bec8f0670c6e8a982e299ad38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:18:50 +0000 Subject: [PATCH 01/26] Register oxa-work crate for issue #333 --- oxa/Cargo.lock | 9 +++++++++ oxa/Cargo.toml | 3 ++- oxa/oxa-cli/Cargo.toml | 1 + oxa/oxa-work/Cargo.toml | 14 ++++++++++++++ oxa/oxa-work/src/lib.rs | 1 + 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 oxa/oxa-work/Cargo.toml create mode 100644 oxa/oxa-work/src/lib.rs diff --git a/oxa/Cargo.lock b/oxa/Cargo.lock index 485000b98..c5f1ba2d8 100644 --- a/oxa/Cargo.lock +++ b/oxa/Cargo.lock @@ -431,6 +431,7 @@ dependencies = [ "oxa-tools", "oxa-tui", "oxa-web", + "oxa-work", "regex", "serde_json", "sha2", @@ -694,6 +695,14 @@ dependencies = [ "ureq", ] +[[package]] +name = "oxa-work" +version = "0.2.0" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "parking_lot" version = "0.12.5" diff --git a/oxa/Cargo.toml b/oxa/Cargo.toml index b4a683d90..27d254cff 100644 --- a/oxa/Cargo.toml +++ b/oxa/Cargo.toml @@ -12,7 +12,7 @@ members = [ "oxa-core", "oxa-tui", "oxa-interactive", "oxa-rpc", "oxa-export", - "oxa-cli", + "oxa-cli", "oxa-work", "oxa-fake", "oxa-parity", ] @@ -45,6 +45,7 @@ oxa-interactive = { path = "oxa-interactive" } oxa-rpc = { path = "oxa-rpc" } oxa-export = { path = "oxa-export" } oxa-cli = { path = "oxa-cli" } +oxa-work = { path = "oxa-work" } oxa-fake = { path = "oxa-fake" } # external (plan sec. 4); no async runtime anywhere serde = { version = "1", features = ["derive"] } diff --git a/oxa/oxa-cli/Cargo.toml b/oxa/oxa-cli/Cargo.toml index f962b13ad..a6085a4db 100644 --- a/oxa/oxa-cli/Cargo.toml +++ b/oxa/oxa-cli/Cargo.toml @@ -17,6 +17,7 @@ name = "oxa" path = "src/main.rs" [dependencies] +oxa-work.workspace = true oxa-proto.workspace = true oxa-engine.workspace = true oxa-session.workspace = true diff --git a/oxa/oxa-work/Cargo.toml b/oxa/oxa-work/Cargo.toml new file mode 100644 index 000000000..6097461ce --- /dev/null +++ b/oxa/oxa-work/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "oxa-work" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +serde.workspace = true +serde_json.workspace = true + +[lints] +workspace = true diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs new file mode 100644 index 000000000..90f7f11b9 --- /dev/null +++ b/oxa/oxa-work/src/lib.rs @@ -0,0 +1 @@ +//! Unattended task runner. From fb25b362d9be2fe7c59d4ce3f12e9ce17e50124c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:23:27 +0000 Subject: [PATCH 02/26] Add opt-in work task runner with external acceptance checks (#333) --- oxa/docs/work.md | 33 +++++ oxa/oxa-cli/src/args.rs | 7 + oxa/oxa-cli/src/dispatch.rs | 1 + oxa/oxa-cli/src/lib.rs | 1 + oxa/oxa-cli/src/work_cmd.rs | 39 ++++++ oxa/oxa-cli/tests/work_cli.rs | 190 ++++++++++++++++++++++++++ oxa/oxa-work/src/lib.rs | 250 +++++++++++++++++++++++++++++++++- 7 files changed, 520 insertions(+), 1 deletion(-) create mode 100644 oxa/docs/work.md create mode 100644 oxa/oxa-cli/src/work_cmd.rs create mode 100644 oxa/oxa-cli/tests/work_cli.rs diff --git a/oxa/docs/work.md b/oxa/docs/work.md new file mode 100644 index 000000000..4bee57969 --- /dev/null +++ b/oxa/docs/work.md @@ -0,0 +1,33 @@ +# Unattended work + +`oxa work tasks.json` opts into sequential tasks in separate git worktrees. +Run it inside the project repository. The JSON file is an array: + +```json +[{"id":"docs-fix","model":"your-model","branch":"agent/docs-fix","base":"main","prompt":"Fix documentation links."}] +``` + +`base` defaults to `main`. IDs contain letters, digits, hyphens or underscores. +IDs and branches must be unique; branches must be new. For a GitHub issue use +`oxa work --issue 123 --model your-model`. This explicitly invokes your installed, +authenticated `gh` CLI to fetch the issue title and body. + +Each task runs a child `oxa -p` with its model. Normal agent permission, trust and +sandbox rules apply. Configure project trust before unattended use. The runner +executes the base version of `.oxa/settings.json`'s `done` commands outside the +agent after it exits, even without edits. These are trusted project shell commands: +invoking `work` authorizes their execution as your user. `in subdirectory: command` +runs in that worktree subdirectory; escaping it with the directory is rejected. +Agent-internal done checks may also run when the project is trusted. + +Failed acceptance output goes to one additional child run with the original prompt, +then acceptance runs once more. JSON lines report `done`, `nothing_to_do` (no +commits or file changes), `red` (checks still fail), or `blocked` (setup or agent +execution failed). Exit codes are 0 for success, 1 if any task is red or blocked, +and 2 for invalid input or runner errors. + +Worktrees and uncommitted changes remain under the common git directory's +`oxa-work/`. Result receipts are beside them at `.json`. No automatic +cleanup, commit, push, PR creation or continuation is implemented yet. Inspect +retained work before removing it with `git worktree remove`. Review task prompts +and bases before running them. diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index d62d19272..ecf283560 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -14,6 +14,8 @@ pub enum ModeArg { /// A subcommand (first positional word). #[derive(Debug, Clone, PartialEq, Eq)] pub enum Subcommand { + /// `oxa work | --issue N --model MODEL`. + Work(Vec), /// `oxa mcp add|remove|list|login|logout ..`: the words after `mcp`. Mcp(Vec), /// `oxa install|remove|update|list ..`: the verb and the words after it. @@ -174,6 +176,10 @@ const COMMANDS: &[(&str, &str)] = &[ "oxa report-issue ", "Draft a scrubbed ox-llm issue; submit posts it only after you type yes", ), + ( + "oxa work | --issue N --model MODEL", + "Run tasks in isolated worktrees with external acceptance checks", + ), ( "oxa wipe --session | --all [--yes] [--engine]", "Delete one session (or every session) and its spill files; --engine also drops the engine's state", @@ -340,6 +346,7 @@ fn subcommand(word: &str, rest: &[String]) -> Result, OxaErro "identity" => Subcommand::Identity(rest.to_vec()), "report-issue" => Subcommand::ReportIssue(rest.to_vec()), "wipe" => Subcommand::Wipe(rest.to_vec()), + "work" => Subcommand::Work(rest.to_vec()), "auth" => return Err(OxaError::unsupported("the auth command")), // pi's provider sign-in (`pi login [--api-key] [--no-browser]`, `pi logout`). "login" | "logout" => { diff --git a/oxa/oxa-cli/src/dispatch.rs b/oxa/oxa-cli/src/dispatch.rs index 101f58fe4..4bdc4b7b0 100644 --- a/oxa/oxa-cli/src/dispatch.rs +++ b/oxa/oxa-cli/src/dispatch.rs @@ -125,6 +125,7 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { Err(e) => return report(&mut *stderr, &e), }; let result = match sub { + Subcommand::Work(words) => return crate::work_cmd::run(&words, &cwd, &mut *stdout, &mut *stderr), Subcommand::Mcp(words) => run_mcp(&words, &dirs, &cwd, &mut *stdout, &mut *stderr), Subcommand::Pkg(verb, words) => run_pkg(&verb, &words, &dirs, &cwd, &mut *stdout, &mut *stderr), Subcommand::Config(words) => run_config(&words, &dirs, &cwd, &mut *stdout), diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index b7096b398..f5cc30f16 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -31,6 +31,7 @@ mod report_cmd; mod sandbox_layout; mod themes; mod wipe_cmd; +mod work_cmd; pub use args::{parse_args, usage_text, ApiKey, Args, ModeArg, Subcommand}; pub use config_cmd::run_config; diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs new file mode 100644 index 000000000..3d8769c5e --- /dev/null +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -0,0 +1,39 @@ +use std::io::Write; +use std::path::{Path, PathBuf}; + +pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dyn Write) -> i32 { + let result = (|| -> Result { + let tasks = match args { + [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, + [flag, number, model_flag, model] if flag == "--issue" && model_flag == "--model" => { + vec![oxa_work::issue_task( + cwd, + number.parse().map_err(|_| "invalid issue number")?, + model.clone(), + )?] + } + _ => return Err("usage: oxa work | --issue N --model MODEL".into()), + }; + let binary: PathBuf = std::env::current_exe().map_err(|e| e.to_string())?; + let mut code = 0; + for task in tasks { + let record = oxa_work::run_task(cwd, &binary, &task)?; + if matches!( + record.outcome, + oxa_work::Outcome::Red | oxa_work::Outcome::Blocked + ) { + code = 1; + } + serde_json::to_writer(&mut *out, &record).map_err(|e| e.to_string())?; + writeln!(out).map_err(|e| e.to_string())?; + } + Ok(code) + })(); + match result { + Ok(code) => code, + Err(e) => { + let _ = writeln!(err, "oxa work: {e}"); + 2 + } + } +} diff --git a/oxa/oxa-cli/tests/work_cli.rs b/oxa/oxa-cli/tests/work_cli.rs new file mode 100644 index 000000000..fa28e7b54 --- /dev/null +++ b/oxa/oxa-cli/tests/work_cli.rs @@ -0,0 +1,190 @@ +//! Real child oxa with fake engine and local bare git remote; no network. +use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +use std::{path::Path, process::Command}; +fn git(env: &TestEnv, dir: &Path, args: &[&str]) { + let out = Command::new("git") + .args(args) + .current_dir(dir) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") + .output() + .unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); +} +fn setup(env: &TestEnv, done: &str) { + git(env, &env.work(), &["init", "--bare", "remote.git"]); + git(env, &env.work(), &["init", "-b", "main", "project"]); + let project = env.work().join("project"); + env.write( + "work/project/.oxa/settings.json", + &format!(r#"{{"done":{done}}}"#), + ); + git(env, &project, &["add", "."]); + git(env, &project, &["commit", "-m", "fixture"]); + git(env, &project, &["remote", "add", "origin", "../remote.git"]); + git(env, &project, &["push", "origin", "main"]); + env.write( + "work/tasks.json", + r#"[{"id":"test","branch":"agent/test","model":"fake","prompt":"Finish task"}]"#, + ); +} +fn run(env: &TestEnv, turns: Vec) -> serde_json::Value { + run_args(env, turns, &["work", "../tasks.json"], None) +} +fn run_args(env: &TestEnv, turns: Vec, args: &[&str], path: Option) -> serde_json::Value { + let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); + let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); + command + .args(args) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs(&server.url())); + if let Some(path) = path { + command.env("PATH", path); + } + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + tx.send(command.output().unwrap()).unwrap(); + }); + let out = rx + .recv_timeout(std::time::Duration::from_secs(90)) + .expect("runner finished"); + serde_json::from_slice(&out.stdout).unwrap_or_else(|e| { + panic!( + "{e}: {} / {}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ) + }) +} +#[test] +fn external_checks_run_without_edits_and_retain_worktree() { + let env = TestEnv::new(); + setup(&env, r#"["printf checked > receipt"]"#); + let record = run(&env, vec![ScriptTurn::text("finished")]); + assert_eq!(record["outcome"], "done", "{record}"); + let tree = Path::new(record["worktree"].as_str().unwrap()); + assert_eq!(std::fs::read_to_string(tree.join("receipt")).unwrap(), "checked"); + assert!(env.work().join("project/.git/oxa-work/test.json").exists()); +} +#[test] +fn clean_task_is_nothing_to_do() { + let env = TestEnv::new(); + setup(&env, "[]"); + assert_eq!( + run(&env, vec![ScriptTurn::text("nothing")])["outcome"], + "nothing_to_do" + ); +} +#[test] +fn failed_checks_get_exactly_one_retry() { + let env = TestEnv::new(); + setup( + &env, + r#"["printf attempt >> attempts; echo external-check; exit 1"]"#, + ); + let record = run( + &env, + vec![ScriptTurn::text("finished"), ScriptTurn::text("cannot repair")], + ); + assert_eq!(record["outcome"], "red", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("external-check")); + let tree = Path::new(record["worktree"].as_str().unwrap()); + assert_eq!( + std::fs::read_to_string(tree.join("attempts")).unwrap(), + "attemptattempt" + ); +} +#[test] +fn retry_can_make_acceptance_green() { + let env = TestEnv::new(); + setup( + &env, + r#"["if test -f once; then true; else touch once; exit 1; fi"]"#, + ); + let record = run( + &env, + vec![ScriptTurn::text("finished"), ScriptTurn::text("retry")], + ); + assert_eq!(record["outcome"], "done", "{record}"); +} + +#[test] +fn agent_edits_stay_in_worktree() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always"}"#, + ); + let record = run( + &env, + vec![ + ScriptTurn::tool("edit", "write", r#"{"path":"answer.txt","content":"finished"}"#), + ScriptTurn::text("done"), + ], + ); + assert_eq!(record["outcome"], "done", "{record}"); + let tree = Path::new(record["worktree"].as_str().unwrap()); + assert_eq!( + std::fs::read_to_string(tree.join("answer.txt")).unwrap(), + "finished" + ); + assert!(!env.work().join("project/answer.txt").exists()); +} +#[test] +fn invalid_task_file_is_rejected_before_creating_worktree() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write( + "work/tasks.json", + r#"[{"id":"../escape","branch":"agent/test","model":"fake","prompt":"go"}]"#, + ); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + assert_eq!(out.status.code(), Some(2)); + assert!(!env.work().join("project/.git/oxa-work").exists()); +} + +#[test] +fn github_issue_input_uses_mock_gh() { + use std::os::unix::fs::PermissionsExt; + let env = TestEnv::new(); + setup(&env, "[]"); + let gh = env.write( + "tmp/gh", + r##"#!/bin/sh +[ "$*" = 'issue view 42 --json title,body' ] || exit 9 +printf '%s' '{"title":"Synthetic task","body":"Fix the test fixture"}' +"##, + ); + std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap(); + let path = format!("{}:{}", env.tmp().display(), std::env::var("PATH").unwrap()); + let record = run_args( + &env, + vec![ScriptTurn::text("done")], + &["work", "--issue", "42", "--model", "fake"], + Some(path), + ); + assert_eq!(record["id"], "issue-42"); + assert_eq!(record["branch"], "agent/42-work"); + assert_eq!(record["outcome"], "nothing_to_do", "{record}"); +} +#[test] +fn setup_failure_records_blocked_and_keeps_existing_branch() { + let env = TestEnv::new(); + setup(&env, "[]"); + git(&env, &env.work().join("project"), &["branch", "agent/test"]); + let record = run(&env, vec![]); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("already exists")); +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 90f7f11b9..5f1dbd487 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -1 +1,249 @@ -//! Unattended task runner. +//! Opt-in, sequential unattended task execution. Worktrees are retained on every outcome. +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output, Stdio}; + +pub type Result = std::result::Result; + +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Task { + pub id: String, + pub model: String, + pub branch: String, + #[serde(default = "default_base")] + pub base: String, + pub prompt: String, +} +fn default_base() -> String { + "main".into() +} + +#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum Outcome { + Done, + NothingToDo, + Red, + Blocked, +} + +#[derive(Debug, Serialize)] +pub struct Record { + pub id: String, + pub branch: String, + pub worktree: PathBuf, + pub outcome: Outcome, + pub detail: String, +} + +fn output(mut command: Command) -> Result { + command.stdin(Stdio::null()).output().map_err(|e| e.to_string()) +} +fn checked(command: Command) -> Result { + let out = output(command)?; + if !out.status.success() { + return Err(String::from_utf8_lossy(&out.stderr).into_owned()); + } + Ok(String::from_utf8_lossy(&out.stdout).trim().into()) +} +fn git(repo: &Path, args: &[&str]) -> Command { + let mut c = Command::new("git"); + c.current_dir(repo).args(args); + c +} + +pub fn read_tasks(path: &Path) -> Result> { + let tasks: Vec = serde_json::from_slice(&fs::read(path).map_err(|e| e.to_string())?) + .map_err(|e| format!("task file: {e}"))?; + if tasks.is_empty() { + return Err("task file is empty".into()); + } + let mut ids = std::collections::HashSet::new(); + let mut branches = std::collections::HashSet::new(); + for task in &tasks { + validate(task)?; + if !ids.insert(&task.id) || !branches.insert(&task.branch) { + return Err("duplicate task id or branch".into()); + } + } + Ok(tasks) +} +fn validate(task: &Task) -> Result<()> { + if task.id.is_empty() + || !task + .id + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_".contains(&b)) + { + return Err("task id must contain only letters, digits, - or _".into()); + } + if task.model.trim().is_empty() || task.prompt.trim().is_empty() { + return Err("model and prompt are required".into()); + } + for reference in [&task.branch, &task.base] { + if reference.is_empty() || reference.starts_with('-') { + return Err("invalid branch or base".into()); + } + } + Ok(()) +} + +/// Issue text is data, passed as one prompt argument; gh never invokes a shell. +pub fn issue_task(repo: &Path, number: u64, model: String) -> Result { + if number == 0 { + return Err("issue number must be positive".into()); + } + let mut c = Command::new("gh"); + c.current_dir(repo) + .args(["issue", "view", &number.to_string(), "--json", "title,body"]); + let text = checked(c)?; + let issue: serde_json::Value = serde_json::from_str(&text).map_err(|e| e.to_string())?; + let title = issue["title"].as_str().ok_or("issue title missing")?; + let body = issue["body"].as_str().ok_or("issue body missing")?; + Ok(Task { + id: format!("issue-{number}"), + model, + branch: format!("agent/{number}-work"), + base: default_base(), + prompt: format!("Implement issue #{number}: {title}\n\n{body}"), + }) +} + +/// Acceptance commands are frozen from the base before the agent can edit settings. +fn done_commands(root: &Path) -> Result> { + let path = root.join(".oxa/settings.json"); + let bytes = match fs::read(path) { + Ok(bytes) => bytes, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(vec![]), + Err(e) => return Err(e.to_string()), + }; + let value: serde_json::Value = serde_json::from_slice(&bytes).map_err(|e| e.to_string())?; + match value.get("done") { + None => Ok(vec![]), + Some(value) => serde_json::from_value(value.clone()).map_err(|e| format!("done commands: {e}")), + } +} +fn acceptance(root: &Path, commands: &[String]) -> Result { + let mut failures = String::new(); + for entry in commands { + let (dir, shell) = match entry.strip_prefix("in ").and_then(|s| s.split_once(':')) { + Some((dir, cmd)) => (root.join(dir.trim()), cmd.trim()), + None => (root.to_path_buf(), entry.as_str()), + }; + let dir = dir.canonicalize().map_err(|e| e.to_string())?; + if !dir.starts_with(root) { + return Err("done directory escapes worktree".into()); + } + let mut c = Command::new("sh"); + c.current_dir(dir).args(["-c", shell]); + let out = output(c)?; + if !out.status.success() { + failures.push_str(&format!( + "{entry}: {}\n{}\n{}\n", + out.status, + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + )); + } + } + Ok(failures) +} +fn agent(binary: &Path, root: &Path, task: &Task, prompt: &str) -> Result<()> { + let mut c = Command::new(binary); + c.current_dir(root) + .args(["-p", "--model", &task.model, "--no-session", "--", prompt]); + checked(c).map(|_| ()) +} + +struct TaskLock(PathBuf); +impl Drop for TaskLock { + fn drop(&mut self) { + let _ = fs::remove_file(&self.0); + } +} + +pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { + validate(task)?; + let root = checked(git(repo, &["rev-parse", "--show-toplevel"]))?; + let root = PathBuf::from(root); + checked(git(&root, &["check-ref-format", "--branch", &task.branch]))?; + let common = checked(git( + &root, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?; + let storage = PathBuf::from(common).join("oxa-work"); + fs::create_dir_all(&storage).map_err(|e| e.to_string())?; + let lock_path = storage.join(format!("{}.lock", task.id)); + let _lock_file = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&lock_path) + .map_err(|e| format!("cannot lock task {}: {e}", task.id))?; + let _lock = TaskLock(lock_path); + let receipt = storage.join(format!("{}.json", task.id)); + if receipt.exists() { + return Err("task already has a receipt; retained for inspection".into()); + } + let tree = storage.join(&task.id); + let mut record = Record { + id: task.id.clone(), + branch: task.branch.clone(), + worktree: tree.clone(), + outcome: Outcome::Blocked, + detail: String::new(), + }; + let result = (|| -> Result { + if tree.exists() { + return Err("task worktree already exists; retained for inspection".into()); + } + let tree_arg = tree.to_str().ok_or("non-UTF8 worktree path")?; + checked(git( + &root, + &["worktree", "add", "-b", &task.branch, tree_arg, &task.base], + ))?; + let tree = tree.canonicalize().map_err(|e| e.to_string())?; + let initial = checked(git(&tree, &["rev-parse", "HEAD"]))?; + let commands = done_commands(&tree)?; + let first_agent = agent(binary, &tree, task, &task.prompt); + let failures = acceptance(&tree, &commands)?; + if !failures.is_empty() { + let repair_agent = agent( + binary, + &tree, + task, + &format!( + "{}\n\nExternal acceptance checks failed. Repair once:\n{failures}", + task.prompt + ), + ); + let failures = acceptance(&tree, &commands)?; + if !failures.is_empty() { + record.detail = failures; + return Ok(Outcome::Red); + } + repair_agent?; + } else { + first_agent?; + } + let head = checked(git(&tree, &["rev-parse", "HEAD"]))?; + let dirty = checked(git(&tree, &["status", "--porcelain"]))?; + Ok(if head == initial && dirty.is_empty() { + Outcome::NothingToDo + } else { + Outcome::Done + }) + })(); + match result { + Ok(outcome) => record.outcome = outcome, + Err(e) => record.detail = e, + } + // Separate receipts avoid overwriting another task's result. Never remove its worktree. + fs::write( + receipt, + serde_json::to_vec_pretty(&record).map_err(|e| e.to_string())?, + ) + .map_err(|e| e.to_string())?; + Ok(record) +} From cdfab279d60b4ae005dcc413507d9bac49a32948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:48:01 +0000 Subject: [PATCH 03/26] Fix sandbox commits and offline issue lookup in work runner --- oxa/docs/work.md | 9 ++-- oxa/oxa-cli/src/work_cmd.rs | 3 ++ oxa/oxa-cli/tests/work_cli.rs | 80 +++++++++++++++++++++++++++++++++++ oxa/oxa-work/src/lib.rs | 18 +++++++- 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 4bee57969..ed74c8ab8 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -10,7 +10,8 @@ Run it inside the project repository. The JSON file is an array: `base` defaults to `main`. IDs contain letters, digits, hyphens or underscores. IDs and branches must be unique; branches must be new. For a GitHub issue use `oxa work --issue 123 --model your-model`. This explicitly invokes your installed, -authenticated `gh` CLI to fetch the issue title and body. +authenticated `gh` CLI to fetch the issue title and body. Issue lookup is refused +when the environment or user settings enable offline mode. Each task runs a child `oxa -p` with its model. Normal agent permission, trust and sandbox rules apply. Configure project trust before unattended use. The runner @@ -26,8 +27,10 @@ commits or file changes), `red` (checks still fail), or `blocked` (setup or agen execution failed). Exit codes are 0 for success, 1 if any task is red or blocked, and 2 for invalid input or runner errors. -Worktrees and uncommitted changes remain under the common git directory's -`oxa-work/`. Result receipts are beside them at `.json`. No automatic +Worktrees and uncommitted changes remain in `.oxa-worktrees//` +beside the repository (or the separate git directory). Result receipts live inside +the common git directory at `oxa-work/.json`. Worktrees stay outside that git +directory so the normal sandbox can bind git objects and refs for commits. No automatic cleanup, commit, push, PR creation or continuation is implemented yet. Inspect retained work before removing it with `git worktree remove`. Review task prompts and bases before running them. diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 3d8769c5e..af2d67257 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -6,6 +6,9 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy let tasks = match args { [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, [flag, number, model_flag, model] if flag == "--issue" && model_flag == "--model" => { + if oxa_proto::offline() { + return Err("GitHub issue lookup is unavailable in offline mode".into()); + } vec![oxa_work::issue_task( cwd, number.parse().map_err(|_| "invalid issue number")?, diff --git a/oxa/oxa-cli/tests/work_cli.rs b/oxa/oxa-cli/tests/work_cli.rs index fa28e7b54..ec6279eef 100644 --- a/oxa/oxa-cli/tests/work_cli.rs +++ b/oxa/oxa-cli/tests/work_cli.rs @@ -188,3 +188,83 @@ fn setup_failure_records_blocked_and_keeps_existing_branch() { assert_eq!(record["outcome"], "blocked", "{record}"); assert!(record["detail"].as_str().unwrap().contains("already exists")); } + +#[test] +fn child_can_create_a_commit_with_the_real_sandbox() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always"}"#, + ); + let record = run( + &env, + vec![ + ScriptTurn::tool("write", "write", r#"{"path":"answer.txt","content":"committed"}"#), + ScriptTurn::tool( + "commit", + "bash", + r#"{"command":"git add answer.txt && git -c user.name=Test -c user.email=test@example.invalid commit -m task"}"#, + ), + ScriptTurn::text("done"), + ], + ); + assert_eq!(record["outcome"], "done", "{record}"); + let tree = Path::new(record["worktree"].as_str().unwrap()); + let status = Command::new("git") + .args(["status", "--porcelain"]) + .current_dir(tree) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + assert!(status.status.success()); + assert!( + status.stdout.is_empty(), + "commit must persist: {}", + String::from_utf8_lossy(&status.stdout) + ); + let subject = Command::new("git") + .args(["log", "-1", "--format=%s"]) + .current_dir(tree) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + assert_eq!(String::from_utf8_lossy(&subject.stdout).trim(), "task"); +} +#[test] +fn offline_issue_input_never_spawns_gh() { + use std::os::unix::fs::PermissionsExt; + for setting in [false, true] { + let env = TestEnv::new(); + setup(&env, "[]"); + let gh = env.write( + "tmp/gh", + "#!/bin/sh\nprintf invoked > \"$HOME/gh-invoked\"\nexit 7\n", + ); + std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap(); + if setting { + env.write("home/.oxa/agent/settings.json", r#"{"oxa":{"offline":true}}"#); + } + let mut cmd = Command::new(env!("CARGO_BIN_EXE_oxa")); + cmd.args(["work", "--issue", "42", "--model", "fake"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .env( + "PATH", + format!("{}:{}", env.tmp().display(), std::env::var("PATH").unwrap()), + ); + if !setting { + cmd.env("OXA_OFFLINE", "1"); + } + let out = cmd.output().unwrap(); + assert_eq!(out.status.code(), Some(2)); + assert!( + !env.home().join("gh-invoked").exists(), + "offline lookup ran gh, setting={setting}" + ); + assert!(String::from_utf8_lossy(&out.stderr).contains("offline")); + } +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 5f1dbd487..4ea8a1551 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -173,7 +173,8 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { &root, &["rev-parse", "--path-format=absolute", "--git-common-dir"], ))?; - let storage = PathBuf::from(common).join("oxa-work"); + let common = PathBuf::from(common); + let storage = common.join("oxa-work"); fs::create_dir_all(&storage).map_err(|e| e.to_string())?; let lock_path = storage.join(format!("{}.lock", task.id)); let _lock_file = fs::OpenOptions::new() @@ -186,7 +187,20 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { if receipt.exists() { return Err("task already has a receipt; retained for inspection".into()); } - let tree = storage.join(&task.id); + // The common git directory must not be an ancestor of the child cwd: the sandbox + // deliberately refuses writable git binds with that layout. + let repo_dir = if common.file_name().is_some_and(|name| name == ".git") { + common.parent().ok_or("git directory has no parent")? + } else { + &common + }; + let parent = repo_dir + .parent() + .ok_or("repository has no parent for worktrees")?; + let name = repo_dir.file_name().ok_or("repository has no directory name")?; + let trees = parent.join(".oxa-worktrees").join(name); + fs::create_dir_all(&trees).map_err(|e| e.to_string())?; + let tree = trees.join(&task.id); let mut record = Record { id: task.id.clone(), branch: task.branch.clone(), From c86fa8b9bf3df4ae49612e9458f2fa51aad354bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:50:25 +0000 Subject: [PATCH 04/26] Continue work branches with ordinary base merges (#333) --- oxa/docs/work.md | 16 ++- oxa/oxa-cli/tests/common/work.rs | 79 +++++++++++ oxa/oxa-cli/tests/work_cli.rs | 75 ++-------- oxa/oxa-cli/tests/work_continue.rs | 215 +++++++++++++++++++++++++++++ oxa/oxa-work/src/lib.rs | 43 ++---- oxa/oxa-work/src/tree.rs | 147 ++++++++++++++++++++ 6 files changed, 477 insertions(+), 98 deletions(-) create mode 100644 oxa/oxa-cli/tests/common/work.rs create mode 100644 oxa/oxa-cli/tests/work_continue.rs create mode 100644 oxa/oxa-work/src/tree.rs diff --git a/oxa/docs/work.md b/oxa/docs/work.md index ed74c8ab8..0772b2b76 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -8,7 +8,9 @@ Run it inside the project repository. The JSON file is an array: ``` `base` defaults to `main`. IDs contain letters, digits, hyphens or underscores. -IDs and branches must be unique; branches must be new. For a GitHub issue use +IDs and branches must be unique within one file. A new branch starts from `base`; +an existing branch is continued with a normal merge of that base before the agent +starts. No remote fetch happens automatically. For a GitHub issue use `oxa work --issue 123 --model your-model`. This explicitly invokes your installed, authenticated `gh` CLI to fetch the issue title and body. Issue lookup is refused when the environment or user settings enable offline mode. @@ -20,6 +22,8 @@ agent after it exits, even without edits. These are trusted project shell comman invoking `work` authorizes their execution as your user. `in subdirectory: command` runs in that worktree subdirectory; escaping it with the directory is rejected. Agent-internal done checks may also run when the project is trusted. +On continued branches, acceptance commands are read from the captured base commit, +even when the task branch has changed its own settings. Failed acceptance output goes to one additional child run with the original prompt, then acceptance runs once more. JSON lines report `done`, `nothing_to_do` (no @@ -31,6 +35,14 @@ Worktrees and uncommitted changes remain in `.oxa-worktrees//` beside the repository (or the separate git directory). Result receipts live inside the common git directory at `oxa-work/.json`. Worktrees stay outside that git directory so the normal sandbox can bind git objects and refs for commits. No automatic -cleanup, commit, push, PR creation or continuation is implemented yet. Inspect +cleanup, commit, push or PR creation is implemented yet. + +To continue a task, run the same file again. The retained receipt must match its +ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere +is blocked. Uncommitted changes are retained and blocked until you commit them. +Merge conflicts are passed to the agent with the merge still in progress, the +conflicted file list and merge output. An unfinished merge can be continued on a +later run. The agent must finish the merge commit and keep the task branch selected; +unresolved or aborted base merges remain blocked even when checks would pass. Inspect retained work before removing it with `git worktree remove`. Review task prompts and bases before running them. diff --git a/oxa/oxa-cli/tests/common/work.rs b/oxa/oxa-cli/tests/common/work.rs new file mode 100644 index 000000000..44062a99f --- /dev/null +++ b/oxa/oxa-cli/tests/common/work.rs @@ -0,0 +1,79 @@ +use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +use std::{path::Path, process::Command}; +pub fn git(env: &TestEnv, dir: &Path, args: &[&str]) -> String { + let out = Command::new("git") + .args(args) + .current_dir(dir) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") + .output() + .unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + String::from_utf8_lossy(&out.stdout).trim().into() +} +pub fn setup(env: &TestEnv, done: &str) { + git(env, &env.work(), &["init", "--bare", "remote.git"]); + git(env, &env.work(), &["init", "-b", "main", "project"]); + let project = env.work().join("project"); + git(env, &project, &["config", "user.name", "Test"]); + git(env, &project, &["config", "user.email", "test@example.invalid"]); + env.write( + "work/project/.oxa/settings.json", + &format!(r#"{{"done":{done}}}"#), + ); + git(env, &project, &["add", "."]); + git(env, &project, &["commit", "-m", "fixture"]); + git(env, &project, &["remote", "add", "origin", "../remote.git"]); + git(env, &project, &["push", "origin", "main"]); + env.write( + "work/tasks.json", + r#"[{"id":"test","branch":"agent/test","model":"fake","prompt":"Finish task"}]"#, + ); +} +pub fn run(env: &TestEnv, turns: Vec) -> serde_json::Value { + run_args(env, turns, &["work", "../tasks.json"], None) +} +pub fn run_args( + env: &TestEnv, + turns: Vec, + args: &[&str], + path: Option, +) -> serde_json::Value { + run_observed(env, turns, args, path).0 +} +pub fn run_observed( + env: &TestEnv, + turns: Vec, + args: &[&str], + path: Option, +) -> (serde_json::Value, Vec) { + let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); + let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); + command + .args(args) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs(&server.url())); + if let Some(path) = path { + command.env("PATH", path); + } + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + tx.send(command.output().unwrap()).unwrap(); + }); + let out = rx + .recv_timeout(std::time::Duration::from_secs(90)) + .expect("runner finished"); + let record = serde_json::from_slice(&out.stdout).unwrap_or_else(|e| { + panic!( + "{e}: {} / {}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ) + }); + (record, server.requests()) +} diff --git a/oxa/oxa-cli/tests/work_cli.rs b/oxa/oxa-cli/tests/work_cli.rs index ec6279eef..52dc5f2ee 100644 --- a/oxa/oxa-cli/tests/work_cli.rs +++ b/oxa/oxa-cli/tests/work_cli.rs @@ -1,66 +1,9 @@ //! Real child oxa with fake engine and local bare git remote; no network. -use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +#[path = "common/work.rs"] +mod work; +use oxa_fake::{ScriptTurn, TestEnv}; use std::{path::Path, process::Command}; -fn git(env: &TestEnv, dir: &Path, args: &[&str]) { - let out = Command::new("git") - .args(args) - .current_dir(dir) - .env_clear() - .envs(env.envs("http://127.0.0.1:1")) - .env("GIT_AUTHOR_NAME", "Test") - .env("GIT_AUTHOR_EMAIL", "test@example.invalid") - .env("GIT_COMMITTER_NAME", "Test") - .env("GIT_COMMITTER_EMAIL", "test@example.invalid") - .output() - .unwrap(); - assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); -} -fn setup(env: &TestEnv, done: &str) { - git(env, &env.work(), &["init", "--bare", "remote.git"]); - git(env, &env.work(), &["init", "-b", "main", "project"]); - let project = env.work().join("project"); - env.write( - "work/project/.oxa/settings.json", - &format!(r#"{{"done":{done}}}"#), - ); - git(env, &project, &["add", "."]); - git(env, &project, &["commit", "-m", "fixture"]); - git(env, &project, &["remote", "add", "origin", "../remote.git"]); - git(env, &project, &["push", "origin", "main"]); - env.write( - "work/tasks.json", - r#"[{"id":"test","branch":"agent/test","model":"fake","prompt":"Finish task"}]"#, - ); -} -fn run(env: &TestEnv, turns: Vec) -> serde_json::Value { - run_args(env, turns, &["work", "../tasks.json"], None) -} -fn run_args(env: &TestEnv, turns: Vec, args: &[&str], path: Option) -> serde_json::Value { - let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); - let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); - command - .args(args) - .current_dir(env.work().join("project")) - .env_clear() - .envs(env.envs(&server.url())); - if let Some(path) = path { - command.env("PATH", path); - } - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - tx.send(command.output().unwrap()).unwrap(); - }); - let out = rx - .recv_timeout(std::time::Duration::from_secs(90)) - .expect("runner finished"); - serde_json::from_slice(&out.stdout).unwrap_or_else(|e| { - panic!( - "{e}: {} / {}", - String::from_utf8_lossy(&out.stdout), - String::from_utf8_lossy(&out.stderr) - ) - }) -} +use work::{git, run, run_args, setup}; #[test] fn external_checks_run_without_edits_and_retain_worktree() { let env = TestEnv::new(); @@ -180,13 +123,17 @@ printf '%s' '{"title":"Synthetic task","body":"Fix the test fixture"}' assert_eq!(record["outcome"], "nothing_to_do", "{record}"); } #[test] -fn setup_failure_records_blocked_and_keeps_existing_branch() { +fn branch_checked_out_elsewhere_is_blocked() { let env = TestEnv::new(); setup(&env, "[]"); - git(&env, &env.work().join("project"), &["branch", "agent/test"]); + git( + &env, + &env.work().join("project"), + &["worktree", "add", "-b", "agent/test", "../other"], + ); let record = run(&env, vec![]); assert_eq!(record["outcome"], "blocked", "{record}"); - assert!(record["detail"].as_str().unwrap().contains("already exists")); + assert!(record["detail"].as_str().unwrap().contains("other")); } #[test] diff --git a/oxa/oxa-cli/tests/work_continue.rs b/oxa/oxa-cli/tests/work_continue.rs new file mode 100644 index 000000000..5c0c244a2 --- /dev/null +++ b/oxa/oxa-cli/tests/work_continue.rs @@ -0,0 +1,215 @@ +//! Continued branches merge the base before the real child agent starts. +#[path = "common/work.rs"] +mod work; +use oxa_fake::{ScriptTurn, TestEnv}; +use std::path::{Path, PathBuf}; +use work::{git, run, run_observed, setup}; + +fn project(env: &TestEnv) -> PathBuf { + env.work().join("project") +} +fn commit_file(env: &TestEnv, file: &str, content: &str) { + env.write(&format!("work/project/{file}"), content); + git(env, &project(env), &["add", file]); + git(env, &project(env), &["commit", "-m", "fixture update"]); +} +fn diverge(env: &TestEnv, conflict: bool) { + setup(env, "[]"); + commit_file(env, "shared.txt", "original\n"); + git(env, &project(env), &["checkout", "-b", "agent/test"]); + commit_file(env, "shared.txt", "task version\n"); + git(env, &project(env), &["checkout", "main"]); + commit_file( + env, + if conflict { "shared.txt" } else { "base.txt" }, + "base version\n", + ); +} +fn tree(record: &serde_json::Value) -> &Path { + Path::new(record["worktree"].as_str().unwrap()) +} +fn merge_head(env: &TestEnv, root: &Path) -> PathBuf { + PathBuf::from(git( + env, + root, + &["rev-parse", "--path-format=absolute", "--git-path", "MERGE_HEAD"], + )) +} +fn resolve_turns() -> Vec { + vec![ + ScriptTurn::tool( + "resolve", + "write", + r#"{"path":"shared.txt","content":"resolved\n"}"#, + ), + ScriptTurn::tool( + "commit", + "bash", + r#"{"command":"git add shared.txt && git commit --no-edit"}"#, + ), + ScriptTurn::text("merge finished"), + ] +} +fn trust(env: &TestEnv) { + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always"}"#, + ); +} +#[test] +fn existing_branch_fast_forwards_to_base_before_agent() { + let env = TestEnv::new(); + setup(&env, "[]"); + git(&env, &project(&env), &["branch", "agent/test"]); + commit_file(&env, "base.txt", "from main\n"); + let record = run(&env, vec![ScriptTurn::text("nothing")]); + assert_eq!(record["outcome"], "nothing_to_do", "{record}"); + assert_eq!( + std::fs::read_to_string(tree(&record).join("base.txt")).unwrap(), + "from main\n" + ); + git( + &env, + &project(&env), + &["merge-base", "--is-ancestor", "main", "agent/test"], + ); +} +#[test] +fn divergent_branch_gets_normal_merge_without_losing_task_commit() { + let env = TestEnv::new(); + diverge(&env, false); + let record = run(&env, vec![ScriptTurn::text("nothing")]); + assert_eq!(record["outcome"], "nothing_to_do", "{record}"); + assert_eq!( + git(&env, tree(&record), &["rev-list", "--parents", "-n", "1", "HEAD"]) + .split_whitespace() + .count(), + 3 + ); + assert_eq!( + std::fs::read_to_string(tree(&record).join("shared.txt")).unwrap(), + "task version\n" + ); + assert!(tree(&record).join("base.txt").exists()); +} +#[test] +fn conflicts_reach_agent_with_merge_in_progress_and_stay_blocked_if_unresolved() { + let env = TestEnv::new(); + diverge(&env, true); + let (record, requests) = run_observed( + &env, + vec![ScriptTurn::text("cannot resolve")], + &["work", "../tasks.json"], + None, + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("merge")); + assert!(merge_head(&env, tree(&record)).exists()); + assert!(std::fs::read_to_string(tree(&record).join("shared.txt")) + .unwrap() + .contains("<<<<<<<")); + let user = requests + .iter() + .flat_map(|r| &r.messages) + .filter_map(|m| m.content.as_deref()) + .collect::>() + .join("\n"); + assert!( + user.contains("Finish task") && user.contains("merge in progress") && user.contains("shared.txt"), + "{user}" + ); +} +#[test] +fn agent_can_resolve_and_commit_merge_inside_real_sandbox() { + let env = TestEnv::new(); + diverge(&env, true); + trust(&env); + let record = run(&env, resolve_turns()); + assert_eq!(record["outcome"], "done", "{record}"); + assert!(!merge_head(&env, tree(&record)).exists()); + assert_eq!( + std::fs::read_to_string(tree(&record).join("shared.txt")).unwrap(), + "resolved\n" + ); + assert_eq!( + std::fs::read_to_string(project(&env).join("shared.txt")).unwrap(), + "base version\n" + ); +} +#[test] +fn unresolved_merge_can_be_continued_without_resetting_it() { + let env = TestEnv::new(); + diverge(&env, true); + trust(&env); + let first = run(&env, vec![ScriptTurn::text("blocked")]); + assert_eq!(first["outcome"], "blocked", "{first}"); + let second = run(&env, resolve_turns()); + assert_eq!(second["outcome"], "done", "{second}"); + assert!(!merge_head(&env, tree(&second)).exists()); +} +#[test] +fn dirty_retained_worktree_is_blocked_without_dropping_edits() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let first = run( + &env, + vec![ + ScriptTurn::tool("write", "write", r#"{"path":"notes.txt","content":"keep me"}"#), + ScriptTurn::text("done"), + ], + ); + assert_eq!(first["outcome"], "done", "{first}"); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(requests.is_empty()); + assert_eq!( + std::fs::read_to_string(tree(&record).join("notes.txt")).unwrap(), + "keep me" + ); +} +#[test] +fn continued_branch_cannot_replace_base_acceptance_commands() { + let env = TestEnv::new(); + setup(&env, r#"["echo base-acceptance; exit 1"]"#); + git(&env, &project(&env), &["checkout", "-b", "agent/test"]); + commit_file(&env, ".oxa/settings.json", r#"{"done":[]}"#); + git(&env, &project(&env), &["checkout", "main"]); + let record = run( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("still done")], + ); + assert_eq!(record["outcome"], "red", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("base-acceptance")); +} + +#[test] +fn aborting_merge_cannot_report_green() { + let env = TestEnv::new(); + diverge(&env, true); + trust(&env); + let record = run( + &env, + vec![ + ScriptTurn::tool("abort", "bash", r#"{"command":"git merge --abort"}"#), + ScriptTurn::text("done"), + ], + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("base is not merged")); +} +#[test] +fn changing_branch_cannot_report_green_for_task_branch() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let record = run( + &env, + vec![ + ScriptTurn::tool("switch", "bash", r#"{"command":"git checkout -b alternate"}"#), + ScriptTurn::text("done"), + ], + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("different branch")); +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 4ea8a1551..02e9d52e9 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -1,4 +1,5 @@ //! Opt-in, sequential unattended task execution. Worktrees are retained on every outcome. +mod tree; use serde::{Deserialize, Serialize}; use std::fs; use std::path::{Path, PathBuf}; @@ -20,7 +21,7 @@ fn default_base() -> String { "main".into() } -#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Outcome { Done, @@ -29,7 +30,7 @@ pub enum Outcome { Blocked, } -#[derive(Debug, Serialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct Record { pub id: String, pub branch: String, @@ -111,20 +112,6 @@ pub fn issue_task(repo: &Path, number: u64, model: String) -> Result { }) } -/// Acceptance commands are frozen from the base before the agent can edit settings. -fn done_commands(root: &Path) -> Result> { - let path = root.join(".oxa/settings.json"); - let bytes = match fs::read(path) { - Ok(bytes) => bytes, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(vec![]), - Err(e) => return Err(e.to_string()), - }; - let value: serde_json::Value = serde_json::from_slice(&bytes).map_err(|e| e.to_string())?; - match value.get("done") { - None => Ok(vec![]), - Some(value) => serde_json::from_value(value.clone()).map_err(|e| format!("done commands: {e}")), - } -} fn acceptance(root: &Path, commands: &[String]) -> Result { let mut failures = String::new(); for entry in commands { @@ -184,9 +171,6 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { .map_err(|e| format!("cannot lock task {}: {e}", task.id))?; let _lock = TaskLock(lock_path); let receipt = storage.join(format!("{}.json", task.id)); - if receipt.exists() { - return Err("task already has a receipt; retained for inspection".into()); - } // The common git directory must not be an ancestor of the child cwd: the sandbox // deliberately refuses writable git binds with that layout. let repo_dir = if common.file_name().is_some_and(|name| name == ".git") { @@ -201,6 +185,7 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { let trees = parent.join(".oxa-worktrees").join(name); fs::create_dir_all(&trees).map_err(|e| e.to_string())?; let tree = trees.join(&task.id); + tree::check_receipt(&receipt, &tree, task)?; let mut record = Record { id: task.id.clone(), branch: task.branch.clone(), @@ -209,18 +194,11 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { detail: String::new(), }; let result = (|| -> Result { - if tree.exists() { - return Err("task worktree already exists; retained for inspection".into()); - } - let tree_arg = tree.to_str().ok_or("non-UTF8 worktree path")?; - checked(git( - &root, - &["worktree", "add", "-b", &task.branch, tree_arg, &task.base], - ))?; + let prepared = tree::prepare(&root, &tree, task)?; let tree = tree.canonicalize().map_err(|e| e.to_string())?; - let initial = checked(git(&tree, &["rev-parse", "HEAD"]))?; - let commands = done_commands(&tree)?; - let first_agent = agent(binary, &tree, task, &task.prompt); + let commands = tree::done_from_base(&tree, &prepared.base)?; + let first_agent = agent(binary, &tree, task, &prepared.prompt); + tree::ensure_merged(&tree, &prepared.base, &task.branch)?; let failures = acceptance(&tree, &commands)?; if !failures.is_empty() { let repair_agent = agent( @@ -229,9 +207,10 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { task, &format!( "{}\n\nExternal acceptance checks failed. Repair once:\n{failures}", - task.prompt + prepared.prompt ), ); + tree::ensure_merged(&tree, &prepared.base, &task.branch)?; let failures = acceptance(&tree, &commands)?; if !failures.is_empty() { record.detail = failures; @@ -243,7 +222,7 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { } let head = checked(git(&tree, &["rev-parse", "HEAD"]))?; let dirty = checked(git(&tree, &["status", "--porcelain"]))?; - Ok(if head == initial && dirty.is_empty() { + Ok(if head == prepared.initial && dirty.is_empty() { Outcome::NothingToDo } else { Outcome::Done diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs new file mode 100644 index 000000000..48c8e5561 --- /dev/null +++ b/oxa/oxa-work/src/tree.rs @@ -0,0 +1,147 @@ +//! Worktree continuation uses ordinary merges; no reset, stash or force checkout. +use super::{checked, git, output, Result, Task}; +use std::fs; +use std::path::{Path, PathBuf}; + +pub(crate) struct Prepared { + pub initial: String, + pub base: String, + pub prompt: String, +} +fn git_path(tree: &Path, name: &str) -> Result { + checked(git( + tree, + &["rev-parse", "--path-format=absolute", "--git-path", name], + )) + .map(PathBuf::from) +} +fn merging(tree: &Path) -> Result { + Ok(git_path(tree, "MERGE_HEAD")?.exists()) +} +fn merge_prompt(tree: &Path, task: &Task, output: &str) -> Result { + let paths = checked(git(tree, &["diff", "--name-only", "--diff-filter=U"]))?; + Ok(format!("{}\n\nThere is a merge in progress from base {}. Resolve the conflicts, stage the resolutions, and finish the merge commit before completing the task. Preserve the task's existing work.\nConflicted paths:\n{paths}\nMerge output:\n{output}", task.prompt, task.base)) +} +pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result { + let base = checked(git( + repo, + &["rev-parse", "--verify", &format!("{}^{{commit}}", task.base)], + ))?; + if !tree.exists() { + let branch = format!("refs/heads/{}", task.branch); + let exists = output(git(repo, &["show-ref", "--verify", "--quiet", &branch]))?; + let tree_arg = tree.to_str().ok_or("non-UTF8 worktree path")?; + match exists.status.code() { + Some(0) => { + checked(git(repo, &["worktree", "add", tree_arg, &task.branch]))?; + } + Some(1) => { + checked(git( + repo, + &["worktree", "add", "-b", &task.branch, tree_arg, &base], + ))?; + } + _ => return Err(String::from_utf8_lossy(&exists.stderr).into()), + } + } + let canonical = tree.canonicalize().map_err(|e| e.to_string())?; + let actual = PathBuf::from(checked(git(tree, &["rev-parse", "--show-toplevel"]))?); + let common = |p: &Path| -> Result { + PathBuf::from(checked(git( + p, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?) + .canonicalize() + .map_err(|e| e.to_string()) + }; + if actual.canonicalize().map_err(|e| e.to_string())? != canonical || common(tree)? != common(repo)? { + return Err("retained directory is not this repository's task worktree".into()); + } + let branch = checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))?; + if branch != format!("refs/heads/{}", task.branch) { + return Err("retained worktree is on a different branch".into()); + } + for operation in ["rebase-merge", "rebase-apply", "CHERRY_PICK_HEAD", "REVERT_HEAD"] { + if git_path(tree, operation)?.exists() { + return Err(format!("retained worktree has {operation} in progress")); + } + } + let initial = checked(git(tree, &["rev-parse", "HEAD"]))?; + if merging(tree)? { + return Ok(Prepared { + initial, + base, + prompt: merge_prompt(tree, task, "Resuming the retained merge.")?, + }); + } + if !checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + return Err("retained worktree has uncommitted changes; commit them before continuing".into()); + } + let merged = output(git(tree, &["merge", "--no-edit", &base]))?; + if !merged.status.success() { + if !merging(tree)? { + return Err(format!( + "base merge failed: {}\n{}", + String::from_utf8_lossy(&merged.stdout), + String::from_utf8_lossy(&merged.stderr) + )); + } + return Ok(Prepared { + initial, + base, + prompt: merge_prompt( + tree, + task, + &format!( + "{}\n{}", + String::from_utf8_lossy(&merged.stdout), + String::from_utf8_lossy(&merged.stderr) + ), + )?, + }); + } + // A clean base-only merge is preparation, not new task work. + Ok(Prepared { + initial: checked(git(tree, &["rev-parse", "HEAD"]))?, + base, + prompt: task.prompt.clone(), + }) +} +pub(crate) fn ensure_merged(tree: &Path, base: &str, branch: &str) -> Result<()> { + if checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))? != format!("refs/heads/{branch}") { + return Err("agent left the worktree on a different branch".into()); + } + if merging(tree)? || !checked(git(tree, &["diff", "--name-only", "--diff-filter=U"]))?.is_empty() { + return Err("merge remains in progress; retained for resolution".into()); + } + if !output(git(tree, &["merge-base", "--is-ancestor", base, "HEAD"]))? + .status + .success() + { + return Err("base is not merged into the task branch; retained for continuation".into()); + } + Ok(()) +} +/// Read acceptance settings from the captured base commit, even on an older task branch. +pub(crate) fn done_from_base(repo: &Path, base: &str) -> Result> { + let path = ".oxa/settings.json"; + if checked(git(repo, &["ls-tree", "--name-only", base, "--", path]))?.is_empty() { + return Ok(vec![]); + } + let text = checked(git(repo, &["show", &format!("{base}:{path}")]))?; + let value: serde_json::Value = serde_json::from_str(&text).map_err(|e| e.to_string())?; + match value.get("done") { + None => Ok(vec![]), + Some(value) => serde_json::from_value(value.clone()).map_err(|e| format!("done commands: {e}")), + } +} +pub(crate) fn check_receipt(receipt: &Path, tree: &Path, task: &Task) -> Result<()> { + if receipt.exists() { + let record: super::Record = serde_json::from_slice(&fs::read(receipt).map_err(|e| e.to_string())?) + .map_err(|e| format!("retained receipt: {e}"))?; + if record.id != task.id || record.branch != task.branch || record.worktree != tree { + return Err("retained receipt belongs to a different task branch or worktree".into()); + } + } + Ok(()) +} From d05391082f1edb8a76d76664f90912743853b2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:56:43 +0000 Subject: [PATCH 05/26] Include the current base when resuming an older merge --- oxa/oxa-cli/tests/work_continue.rs | 39 ++++++++++++++++++++++++++++++ oxa/oxa-work/src/tree.rs | 12 +++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/oxa/oxa-cli/tests/work_continue.rs b/oxa/oxa-cli/tests/work_continue.rs index 5c0c244a2..bc0c252d4 100644 --- a/oxa/oxa-cli/tests/work_continue.rs +++ b/oxa/oxa-cli/tests/work_continue.rs @@ -213,3 +213,42 @@ fn changing_branch_cannot_report_green_for_task_branch() { assert_eq!(record["outcome"], "blocked", "{record}"); assert!(record["detail"].as_str().unwrap().contains("different branch")); } + +#[test] +fn resumed_old_merge_is_told_to_integrate_the_newly_captured_base() { + let env = TestEnv::new(); + diverge(&env, true); + trust(&env); + let first = run(&env, vec![ScriptTurn::text("blocked")]); + assert_eq!(first["outcome"], "blocked", "{first}"); + let old_base = git(&env, &project(&env), &["rev-parse", "main"]); + commit_file(&env, "new-base.txt", "new base commit\n"); + let new_base = git(&env, &project(&env), &["rev-parse", "main"]); + let mut turns = resolve_turns(); + turns.insert( + 2, + ScriptTurn::tool( + "latest", + "bash", + &serde_json::json!({"command": format!("git merge --no-edit {new_base}")}).to_string(), + ), + ); + let (record, requests) = run_observed(&env, turns, &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "done", "{record}"); + let messages = requests + .iter() + .flat_map(|r| &r.messages) + .filter_map(|m| m.content.as_deref()) + .collect::>() + .join("\n"); + assert!( + messages.contains(&old_base) && messages.contains(&format!("git merge --no-edit {new_base}")), + "{messages}" + ); + assert!(tree(&record).join("new-base.txt").exists()); + git( + &env, + &project(&env), + &["merge-base", "--is-ancestor", &new_base, "agent/test"], + ); +} diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs index 48c8e5561..122fe61a7 100644 --- a/oxa/oxa-work/src/tree.rs +++ b/oxa/oxa-work/src/tree.rs @@ -18,9 +18,10 @@ fn git_path(tree: &Path, name: &str) -> Result { fn merging(tree: &Path) -> Result { Ok(git_path(tree, "MERGE_HEAD")?.exists()) } -fn merge_prompt(tree: &Path, task: &Task, output: &str) -> Result { +fn merge_prompt(tree: &Path, task: &Task, base: &str, output: &str) -> Result { let paths = checked(git(tree, &["diff", "--name-only", "--diff-filter=U"]))?; - Ok(format!("{}\n\nThere is a merge in progress from base {}. Resolve the conflicts, stage the resolutions, and finish the merge commit before completing the task. Preserve the task's existing work.\nConflicted paths:\n{paths}\nMerge output:\n{output}", task.prompt, task.base)) + let pending = checked(git(tree, &["rev-parse", "MERGE_HEAD"]))?; + Ok(format!("{}\n\nThere is a merge in progress from base {} at commit {pending}. The current base for this run is {base}. Resolve the conflicts, stage the resolutions, and finish the merge commit before completing the task. Then run `git merge --no-edit {base}` to integrate the captured current base as well, resolving and committing any further conflicts. Preserve the task's existing work.\nConflicted paths:\n{paths}\nMerge output:\n{output}", task.prompt, task.base)) } pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result { let base = checked(git( @@ -70,8 +71,8 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result if merging(tree)? { return Ok(Prepared { initial, - base, - prompt: merge_prompt(tree, task, "Resuming the retained merge.")?, + base: base.clone(), + prompt: merge_prompt(tree, task, &base, "Resuming the retained merge.")?, }); } if !checked(git(tree, &["status", "--porcelain"]))?.is_empty() { @@ -88,10 +89,11 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result } return Ok(Prepared { initial, - base, + base: base.clone(), prompt: merge_prompt( tree, task, + &base, &format!( "{}\n{}", String::from_utf8_lossy(&merged.stdout), From 1e3917272e6b67660b34ae7f9795a55cad9b3c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:39:53 +0000 Subject: [PATCH 06/26] Preserve offline policy and continue work batches after task failures --- oxa/docs/work.md | 9 +++- oxa/oxa-cli/src/args.rs | 21 +++++---- oxa/oxa-cli/src/work_cmd.rs | 3 +- oxa/oxa-cli/tests/work_cli.rs | 82 +++++++++++++++++++++++++++++++++++ oxa/oxa-work/src/lib.rs | 37 +++++++++++----- 5 files changed, 130 insertions(+), 22 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index ed74c8ab8..a04b88a26 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -13,7 +13,8 @@ IDs and branches must be unique; branches must be new. For a GitHub issue use authenticated `gh` CLI to fetch the issue title and body. Issue lookup is refused when the environment or user settings enable offline mode. -Each task runs a child `oxa -p` with its model. Normal agent permission, trust and +Each task runs a child `oxa -p` with its model. The installed offline policy, +including a parent `--offline` flag, is passed to every child. Normal agent permission, trust and sandbox rules apply. Configure project trust before unattended use. The runner executes the base version of `.oxa/settings.json`'s `done` commands outside the agent after it exits, even without edits. These are trusted project shell commands: @@ -25,7 +26,11 @@ Failed acceptance output goes to one additional child run with the original prom then acceptance runs once more. JSON lines report `done`, `nothing_to_do` (no commits or file changes), `red` (checks still fail), or `blocked` (setup or agent execution failed). Exit codes are 0 for success, 1 if any task is red or blocked, -and 2 for invalid input or runner errors. +and 2 for invalid input or output errors. A task setup failure produces a blocked +JSON line and does not skip later tasks. If a task cannot be locked or already has +a receipt, its existing state is preserved; a pre-worktree failure has an empty +`worktree` in the result. Locks use kernel ownership, so an abandoned lock file +after process death does not permanently block a task. Worktrees and uncommitted changes remain in `.oxa-worktrees//` beside the repository (or the separate git directory). Result receipts live inside diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index ecf283560..b4a6942ed 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -458,19 +458,24 @@ fn positional(a: &mut Args, word: &str) { /// `unsupported_in_oxa`. pub fn parse_args(argv: &[String], ext_flags: &[FlagSpec]) -> Result { let mut a = Args::default(); - if let Some(first) = argv.first() { - if !first.starts_with('-') && !first.starts_with('@') { - if let Some(sub) = subcommand(first, &argv[1..])? { - a.subcommand = Some(sub); - return Ok(a); - } - } - } let mut i = 0; let mut stop = false; while i < argv.len() { let arg = argv[i].as_str(); i += 1; + if !stop + && !a.print + && a.mode.is_none() + && !arg.starts_with('-') + && !arg.starts_with('@') + && a.messages.is_empty() + && a.files.is_empty() + { + if let Some(sub) = subcommand(arg, &argv[i..])? { + a.subcommand = Some(sub); + break; + } + } if stop || !arg.starts_with('-') { positional(&mut a, arg); continue; diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index af2d67257..4afd4b9d1 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -20,7 +20,8 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy let binary: PathBuf = std::env::current_exe().map_err(|e| e.to_string())?; let mut code = 0; for task in tasks { - let record = oxa_work::run_task(cwd, &binary, &task)?; + let record = oxa_work::run_task_with_policy(cwd, &binary, &task, oxa_proto::offline()) + .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); if matches!( record.outcome, oxa_work::Outcome::Red | oxa_work::Outcome::Blocked diff --git a/oxa/oxa-cli/tests/work_cli.rs b/oxa/oxa-cli/tests/work_cli.rs index ec6279eef..3cc488928 100644 --- a/oxa/oxa-cli/tests/work_cli.rs +++ b/oxa/oxa-cli/tests/work_cli.rs @@ -268,3 +268,85 @@ fn offline_issue_input_never_spawns_gh() { assert!(String::from_utf8_lossy(&out.stderr).contains("offline")); } } + +#[test] +fn command_line_offline_policy_reaches_child_agent() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write( + "work/tasks.json", + r#"[{"id":"test","branch":"agent/test","model":"cloud-model","prompt":"go"}]"#, + ); + let server = FakeServer::start( + vec![ScriptTurn::text("done")], + FakeOpts { + model: "cloud-model".into(), + remote: Some("openrouter"), + models: vec![oxa_fake::FakeModel { + alias: "cloud-model".into(), + cap: 64000, + max_output: Some(4096), + caps: None, + }], + ..FakeOpts::default() + }, + ) + .unwrap(); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["--offline", "work", "../tasks.json"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs(&server.url())) + .output() + .unwrap(); + let record: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!( + record["detail"].as_str().unwrap().contains("offline mode is on"), + "{record}" + ); + assert!( + server.requests().is_empty(), + "no chat reached even the mock remote" + ); +} +#[test] +fn batch_reports_one_task_error_and_runs_later_tasks() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write("work/project/.git/oxa-work/test.json", "{}"); + env.write("work/tasks.json", r#"[{"id":"test","branch":"agent/test","model":"fake","prompt":"go"},{"id":"next","branch":"agent/next","model":"fake","prompt":"go"}]"#); + let server = FakeServer::start(vec![ScriptTurn::text("done")], FakeOpts::default()).unwrap(); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs(&server.url())) + .output() + .unwrap(); + assert_eq!( + out.status.code(), + Some(1), + "{}", + String::from_utf8_lossy(&out.stderr) + ); + let rows: Vec = String::from_utf8_lossy(&out.stdout) + .lines() + .map(|line| serde_json::from_str(line).unwrap()) + .collect(); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0]["outcome"], "blocked"); + assert_eq!(rows[1]["outcome"], "nothing_to_do"); + assert_eq!( + std::fs::read_to_string(env.work().join("project/.git/oxa-work/test.json")).unwrap(), + "{}" + ); +} +#[test] +fn abandoned_lock_file_does_not_block_a_task() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write("work/project/.git/oxa-work/test.lock", ""); + let record = run(&env, vec![ScriptTurn::text("done")]); + assert_eq!(record["outcome"], "nothing_to_do", "{record}"); +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 4ea8a1551..2a862c1e6 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -150,21 +150,30 @@ fn acceptance(root: &Path, commands: &[String]) -> Result { } Ok(failures) } -fn agent(binary: &Path, root: &Path, task: &Task, prompt: &str) -> Result<()> { +fn agent(binary: &Path, root: &Path, task: &Task, prompt: &str, offline: bool) -> Result<()> { let mut c = Command::new(binary); c.current_dir(root) - .args(["-p", "--model", &task.model, "--no-session", "--", prompt]); + .args(["-p", "--model", &task.model, "--no-session"]); + if offline { + c.arg("--offline"); + } + c.args(["--", prompt]); checked(c).map(|_| ()) } -struct TaskLock(PathBuf); -impl Drop for TaskLock { - fn drop(&mut self) { - let _ = fs::remove_file(&self.0); +pub fn blocked_record(task: &Task, detail: String) -> Record { + Record { + id: task.id.clone(), + branch: task.branch.clone(), + worktree: PathBuf::new(), + outcome: Outcome::Blocked, + detail, } } - pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { + run_task_with_policy(repo, binary, task, false) +} +pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { validate(task)?; let root = checked(git(repo, &["rev-parse", "--show-toplevel"]))?; let root = PathBuf::from(root); @@ -177,12 +186,17 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { let storage = common.join("oxa-work"); fs::create_dir_all(&storage).map_err(|e| e.to_string())?; let lock_path = storage.join(format!("{}.lock", task.id)); - let _lock_file = fs::OpenOptions::new() + let lock_file = fs::OpenOptions::new() .write(true) - .create_new(true) + .create(true) + .truncate(false) .open(&lock_path) .map_err(|e| format!("cannot lock task {}: {e}", task.id))?; - let _lock = TaskLock(lock_path); + lock_file + .try_lock() + .map_err(|e| format!("task {} is already running: {e}", task.id))?; + // Keep the inode so a crashed runner leaves no stale ownership and concurrent + // runners cannot acquire locks on two different files with the same name. let receipt = storage.join(format!("{}.json", task.id)); if receipt.exists() { return Err("task already has a receipt; retained for inspection".into()); @@ -220,7 +234,7 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { let tree = tree.canonicalize().map_err(|e| e.to_string())?; let initial = checked(git(&tree, &["rev-parse", "HEAD"]))?; let commands = done_commands(&tree)?; - let first_agent = agent(binary, &tree, task, &task.prompt); + let first_agent = agent(binary, &tree, task, &task.prompt, offline); let failures = acceptance(&tree, &commands)?; if !failures.is_empty() { let repair_agent = agent( @@ -231,6 +245,7 @@ pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { "{}\n\nExternal acceptance checks failed. Repair once:\n{failures}", task.prompt ), + offline, ); let failures = acceptance(&tree, &commands)?; if !failures.is_empty() { From 77100aa4ab4b65d096885590ec99e5250cb819a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:47:42 +0000 Subject: [PATCH 07/26] Checkpoint work tasks and save unfinished work as commits (#333) --- oxa/docs/work.md | 38 ++++-- oxa/oxa-cli/src/args.rs | 2 +- oxa/oxa-cli/src/work_cmd.rs | 27 +++- oxa/oxa-cli/tests/work_continue.rs | 5 +- oxa/oxa-cli/tests/work_state.rs | 210 +++++++++++++++++++++++++++++ oxa/oxa-work/src/execute.rs | 124 +++++++++++++++++ oxa/oxa-work/src/lib.rs | 186 +++++++++++++++---------- oxa/oxa-work/src/state.rs | 94 +++++++++++++ oxa/oxa-work/src/tree.rs | 51 +++---- 9 files changed, 622 insertions(+), 115 deletions(-) create mode 100644 oxa/oxa-cli/tests/work_state.rs create mode 100644 oxa/oxa-work/src/execute.rs create mode 100644 oxa/oxa-work/src/state.rs diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 7983dd97d..896fc3792 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -1,6 +1,7 @@ # Unattended work `oxa work tasks.json` opts into sequential tasks in separate git worktrees. +Completed tasks are skipped on restart; add `--rerun` to deliberately run them again. Run it inside the project repository. The JSON file is an array: ```json @@ -16,8 +17,8 @@ authenticated `gh` CLI to fetch the issue title and body. Issue lookup is refuse when the environment or user settings enable offline mode. Each task runs a child `oxa -p` with its model. The installed offline policy, -including a parent `--offline` flag, is passed to every child. Normal agent permission, trust and -sandbox rules apply. Configure project trust before unattended use. The runner +including a parent `--offline` flag, is passed to every child. Normal agent +permission, trust and sandbox rules apply. Configure project trust before unattended use. The runner executes the base version of `.oxa/settings.json`'s `done` commands outside the agent after it exits, even without edits. These are trusted project shell commands: invoking `work` authorizes their execution as your user. `in subdirectory: command` @@ -34,20 +35,41 @@ and 2 for invalid input or output errors. A task setup failure produces a blocke JSON line and does not skip later tasks. If a task cannot be locked or already has a receipt, its existing state is preserved; a pre-worktree failure has an empty `worktree` in the result. Locks use kernel ownership, so an abandoned lock file -after process death does not permanently block a task. +after process death does not permanently block a task. A surviving child keeps the +lock until it exits, preventing overlap with a restarted runner. Worktrees and uncommitted changes remain in `.oxa-worktrees//` beside the repository (or the separate git directory). Result receipts live inside the common git directory at `oxa-work/.json`. Worktrees stay outside that git -directory so the normal sandbox can bind git objects and refs for commits. No automatic -cleanup, commit, push or PR creation is implemented yet. +directory so the normal sandbox can bind git objects and refs for commits. +Uncommitted task work, including red work, is saved as a commit with the repository's +configured identity. Commit hooks run normally. A missing identity or rejected commit +blocks the task and retains every file. Unresolved conflicts remain in progress for +the agent; they are never staged as a fabricated resolution. There is no automatic +cleanup, push or PR creation yet. -To continue a task, run the same file again. The retained receipt must match its -ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere -is blocked. Uncommitted changes are retained and blocked until you commit them. +To continue an interrupted, red or blocked task, run the same file again. Use +`--rerun` to continue a completed task after its base changes. The retained receipt +must match its ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere +is blocked. Existing dirty work is saved when possible before a later retry. Merge conflicts are passed to the agent with the merge still in progress, the conflicted file list and merge output. An unfinished merge can be continued on a later run. The agent must finish the merge commit and keep the task branch selected; unresolved or aborted base merges remain blocked even when checks would pass. Inspect retained work before removing it with `git worktree remove`. Review task prompts and bases before running them. + +`oxa work --status` prints one line per recorded task, sorted by ID: + +```text +docs-fix done 1800000000 finished +``` + +Columns are task ID, state, last step-progress time (Unix seconds), and current step. +Running tasks report steps such as `agent`, `acceptance`, `repair` or `saving`. +The state file is replaced atomically with owner-only permissions at each transition. +It records the task specification, captured base/checks, repair output and current +step. Reusing an ID with a different specification is refused; use a new ID. +Restart continues the saved step. An agent or check interrupted within its step +may run again; tasks and checks should tolerate that. Checkpoints after an agent +step avoid replaying it when only acceptance or saving remains. diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index b4a6942ed..ec2fe79aa 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -177,7 +177,7 @@ const COMMANDS: &[(&str, &str)] = &[ "Draft a scrubbed ox-llm issue; submit posts it only after you type yes", ), ( - "oxa work | --issue N --model MODEL", + "oxa work [--rerun] | --issue N --model MODEL | --status", "Run tasks in isolated worktrees with external acceptance checks", ), ( diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 4afd4b9d1..6978aa9f6 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -3,6 +3,24 @@ use std::path::{Path, PathBuf}; pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dyn Write) -> i32 { let result = (|| -> Result { + if args == ["--status"] { + for record in oxa_work::status(cwd)? { + let state = serde_json::to_value(record.outcome).map_err(|e| e.to_string())?; + let step = serde_json::to_value(record.step).map_err(|e| e.to_string())?; + writeln!( + out, + "{} {} {} {}", + record.id, + state.as_str().unwrap_or("unknown"), + record.progress_at, + step.as_str().unwrap_or("unknown") + ) + .map_err(|e| e.to_string())?; + } + return Ok(0); + } + let rerun = args.last().is_some_and(|s| s == "--rerun"); + let args = if rerun { &args[..args.len() - 1] } else { args }; let tasks = match args { [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, [flag, number, model_flag, model] if flag == "--issue" && model_flag == "--model" => { @@ -15,12 +33,17 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy model.clone(), )?] } - _ => return Err("usage: oxa work | --issue N --model MODEL".into()), + _ => { + return Err( + "usage: oxa work [--rerun] | --issue N --model MODEL [--rerun] | --status" + .into(), + ) + } }; let binary: PathBuf = std::env::current_exe().map_err(|e| e.to_string())?; let mut code = 0; for task in tasks { - let record = oxa_work::run_task_with_policy(cwd, &binary, &task, oxa_proto::offline()) + let record = oxa_work::run_task_with_options(cwd, &binary, &task, rerun, oxa_proto::offline()) .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); if matches!( record.outcome, diff --git a/oxa/oxa-cli/tests/work_continue.rs b/oxa/oxa-cli/tests/work_continue.rs index bc0c252d4..6e437bdf5 100644 --- a/oxa/oxa-cli/tests/work_continue.rs +++ b/oxa/oxa-cli/tests/work_continue.rs @@ -148,7 +148,7 @@ fn unresolved_merge_can_be_continued_without_resetting_it() { assert!(!merge_head(&env, tree(&second)).exists()); } #[test] -fn dirty_retained_worktree_is_blocked_without_dropping_edits() { +fn completed_work_is_saved_and_not_replayed() { let env = TestEnv::new(); setup(&env, "[]"); trust(&env); @@ -161,8 +161,9 @@ fn dirty_retained_worktree_is_blocked_without_dropping_edits() { ); assert_eq!(first["outcome"], "done", "{first}"); let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); - assert_eq!(record["outcome"], "blocked", "{record}"); + assert_eq!(record["outcome"], "done", "{record}"); assert!(requests.is_empty()); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); assert_eq!( std::fs::read_to_string(tree(&record).join("notes.txt")).unwrap(), "keep me" diff --git a/oxa/oxa-cli/tests/work_state.rs b/oxa/oxa-cli/tests/work_state.rs new file mode 100644 index 000000000..c5464e2fd --- /dev/null +++ b/oxa/oxa-cli/tests/work_state.rs @@ -0,0 +1,210 @@ +//! Durable outcomes through the real runner, fake engine and local bare remote. +#[path = "common/work.rs"] +mod work; +use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +use std::{ + fs, + path::{Path, PathBuf}, + process::{Command, Stdio}, + time::{Duration, Instant}, +}; +use work::{git, run, run_args, run_observed, setup}; +fn project(env: &TestEnv) -> PathBuf { + env.work().join("project") +} +fn receipt(env: &TestEnv) -> PathBuf { + project(env).join(".git/oxa-work/test.json") +} +fn tree(record: &serde_json::Value) -> &Path { + Path::new(record["worktree"].as_str().unwrap()) +} +fn trust(env: &TestEnv) { + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always"}"#, + ); +} +fn write_turn() -> ScriptTurn { + ScriptTurn::tool("write", "write", r#"{"path":"answer.txt","content":"retained"}"#) +} +fn load(env: &TestEnv) -> serde_json::Value { + serde_json::from_slice(&fs::read(receipt(env)).unwrap()).unwrap() +} +fn replace_state(env: &TestEnv, value: &serde_json::Value) { + fs::write(receipt(env), serde_json::to_vec(value).unwrap()).unwrap(); +} +#[test] +fn uncommitted_success_is_saved_as_a_commit() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let record = run(&env, vec![write_turn(), ScriptTurn::text("done")]); + assert_eq!(record["outcome"], "done", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:answer.txt"]), "retained"); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); + use std::os::unix::fs::PermissionsExt; + assert_eq!( + fs::metadata(receipt(&env)).unwrap().permissions().mode() & 0o077, + 0 + ); + assert_eq!(record["step"], "finished"); + assert!(record["progress_at"].as_u64().unwrap() > 0); +} +#[test] +fn red_work_is_saved_too() { + let env = TestEnv::new(); + setup(&env, r#"["printf failed > failure.txt; exit 1"]"#); + let record = run( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("cannot repair")], + ); + assert_eq!(record["outcome"], "red", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:failure.txt"]), "failed"); + assert!(git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); +} +#[test] +fn completed_task_is_skipped_unless_rerun_is_explicit() { + let env = TestEnv::new(); + setup(&env, "[]"); + let first = run(&env, vec![ScriptTurn::text("done")]); + let (again, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(first, again); + assert!(requests.is_empty()); + let repeated = run_args( + &env, + vec![ScriptTurn::text("repeated")], + &["work", "../tasks.json", "--rerun"], + None, + ); + assert_eq!(repeated["outcome"], "nothing_to_do", "{repeated}"); +} +#[test] +fn restart_at_saving_preserves_outcome_without_replaying_agent() { + let env = TestEnv::new(); + setup(&env, "[]"); + let first = run(&env, vec![ScriptTurn::text("done")]); + fs::write(tree(&first).join("recovered.txt"), "recovered").unwrap(); + let mut state = load(&env); + state["step"] = "saving".into(); + state["outcome"] = "red".into(); + state["detail"] = "previous acceptance failed".into(); + replace_state(&env, &state); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "red"); + assert!(requests.is_empty()); + assert_eq!( + git(&env, tree(&record), &["show", "HEAD:recovered.txt"]), + "recovered" + ); +} +#[test] +fn restart_at_acceptance_keeps_captured_checks_and_base() { + let env = TestEnv::new(); + setup(&env, r#"["printf ran >> attempts"]"#); + let first = run(&env, vec![ScriptTurn::text("done")]); + env.write("work/project/.oxa/settings.json", r#"{"done":["exit 7"]}"#); + git(&env, &project(&env), &["add", ".oxa/settings.json"]); + git(&env, &project(&env), &["commit", "-m", "advance base checks"]); + let mut state = load(&env); + state["step"] = "acceptance".into(); + state["outcome"] = "running".into(); + replace_state(&env, &state); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json"], None); + assert_eq!(record["outcome"], "done", "{record}"); + assert!(requests.is_empty()); + assert_eq!( + fs::read_to_string(tree(&first).join("attempts")).unwrap(), + "ranran" + ); +} +#[test] +fn status_reports_state_progress_time_and_current_step() { + let env = TestEnv::new(); + setup(&env, "[]"); + let record = run(&env, vec![ScriptTurn::text("done")]); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "--status"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + assert!(out.status.success()); + assert_eq!( + String::from_utf8_lossy(&out.stdout).trim(), + format!("test nothing_to_do {} finished", record["progress_at"]) + ); +} +#[test] +fn failed_auto_commit_retains_every_file_and_blocks() { + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + // A user hook refuses the automatic commit. The runner must not bypass it. + use std::os::unix::fs::PermissionsExt; + let hook = env.write("work/project/.git/hooks/pre-commit", "#!/bin/sh\nexit 1\n"); + fs::set_permissions(hook, fs::Permissions::from_mode(0o755)).unwrap(); + let record = run(&env, vec![write_turn(), ScriptTurn::text("done")]); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert_eq!( + fs::read_to_string(tree(&record).join("answer.txt")).unwrap(), + "retained" + ); + assert!(!git(&env, tree(&record), &["status", "--porcelain"]).is_empty()); +} +fn until(mut predicate: impl FnMut() -> bool) { + let deadline = Instant::now() + Duration::from_secs(15); + while !predicate() { + assert!(Instant::now() < deadline, "timed out"); + std::thread::sleep(Duration::from_millis(10)); + } +} +#[test] +fn child_keeps_lock_after_runner_is_killed_then_task_restarts() { + let env = TestEnv::new(); + setup(&env, "[]"); + let server = FakeServer::start( + vec![ScriptTurn::text("first")], + FakeOpts { + hold_headers_ms: 1500, + ..FakeOpts::default() + }, + ) + .unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs(&server.url())) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + until(|| !server.requests().is_empty()); + assert_eq!(load(&env)["step"], "agent"); + child.kill().unwrap(); + child.wait().unwrap(); + let blocked = run(&env, vec![]); + assert_eq!(blocked["outcome"], "blocked", "{blocked}"); + assert!( + blocked["detail"].as_str().unwrap().contains("already running"), + "{blocked}" + ); + assert_eq!( + load(&env)["step"], + "agent", + "busy retry must not clobber the checkpoint" + ); + let lock_path = project(&env).join(".git/oxa-work/test.lock"); + until(|| { + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .open(&lock_path) + .unwrap(); + file.try_lock().is_ok() + }); + let restarted = run(&env, vec![ScriptTurn::text("resumed")]); + assert_eq!(restarted["outcome"], "nothing_to_do", "{restarted}"); + assert_eq!(restarted["step"], "finished"); +} diff --git a/oxa/oxa-work/src/execute.rs b/oxa/oxa-work/src/execute.rs new file mode 100644 index 000000000..115ec7317 --- /dev/null +++ b/oxa/oxa-work/src/execute.rs @@ -0,0 +1,124 @@ +use super::{acceptance, agent, checked, git, state, tree, Outcome, Result, Task}; +use state::{Checkpoint, State, Step}; +use std::fs::File; +use std::path::Path; + +fn complete(tree: &Path, state: &mut State) -> Result<()> { + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + if let Some(error) = &point.agent_error { + return Err(error.clone()); + } + let head = checked(git(tree, &["rev-parse", "HEAD"]))?; + let dirty = checked(git(tree, &["status", "--porcelain"]))?; + state.record.outcome = if head == point.initial && dirty.is_empty() { + Outcome::NothingToDo + } else { + Outcome::Done + }; + Ok(()) +} +pub(crate) fn execute( + repo: &Path, + binary: &Path, + task: &Task, + path: &Path, + state: &mut State, + lock: &File, +) -> Result<()> { + let tree = state.record.worktree.clone(); + loop { + match state.record.step { + Step::Preparing => { + let prepared = tree::prepare(repo, &tree, task)?; + let commands = tree::done_from_base(&tree, &prepared.base)?; + state.checkpoint = Some(Checkpoint { + initial: prepared.initial, + base: prepared.base, + prompt: prepared.prompt, + commands, + agent_error: None, + failures: String::new(), + }); + state::save(path, state, Step::Agent)?; + } + Step::Agent | Step::Repair => { + tree::validate(repo, &tree, task)?; + let step = state.record.step; + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + let prompt = if step == Step::Repair { + format!( + "{}\n\nExternal acceptance checks failed. Repair once:\n{}", + point.prompt, point.failures + ) + } else { + point.prompt.clone() + }; + let result = agent(binary, &tree, task, &prompt, lock, state.offline); + tree::ensure_merged(&tree, &point.base, &task.branch)?; + state + .checkpoint + .as_mut() + .ok_or("missing task checkpoint")? + .agent_error = result.err(); + state::save( + path, + state, + if step == Step::Agent { + Step::Acceptance + } else { + Step::Recheck + }, + )?; + } + Step::Acceptance | Step::Recheck => { + tree::validate(repo, &tree, task)?; + let point = state.checkpoint.as_ref().ok_or("missing task checkpoint")?; + tree::ensure_merged(&tree, &point.base, &task.branch)?; + let failures = acceptance( + &tree.canonicalize().map_err(|e| e.to_string())?, + &point.commands, + lock, + state.offline, + )?; + if failures.is_empty() { + complete(&tree, state)?; + state::save(path, state, Step::Saving)?; + } else if state.record.step == Step::Acceptance { + state + .checkpoint + .as_mut() + .ok_or("missing task checkpoint")? + .failures = failures; + state::save(path, state, Step::Repair)?; + } else { + state.record.outcome = Outcome::Red; + state.record.detail = failures; + state::save(path, state, Step::Saving)?; + } + } + Step::Saving | Step::Finished => return Ok(()), + } + } +} +/// Save all staged, unstaged and untracked files. Conflicts are kept in progress for the agent. +pub(crate) fn save_work(repo: &Path, tree: &Path, task: &Task) -> Result<()> { + if !tree.exists() { + return Ok(()); + } + tree::validate(repo, tree, task)?; + if tree::merging(tree)? || !checked(git(tree, &["diff", "--name-only", "--diff-filter=U"]))?.is_empty() { + return Err("unresolved merge retained in progress; files were not discarded".into()); + } + if checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + return Ok(()); + } + checked(git(tree, &["add", "--all"]))?; + checked(git( + tree, + &["commit", "-m", &format!("oxa work: save task {}", task.id)], + ))?; + if !checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + return Err("uncommitted changes remain after saving; retained for inspection".into()); + } + Ok(()) +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index abe10485b..0ddd10e97 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -1,13 +1,16 @@ //! Opt-in, sequential unattended task execution. Worktrees are retained on every outcome. +mod execute; +mod state; mod tree; use serde::{Deserialize, Serialize}; +pub use state::Step; use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; pub type Result = std::result::Result; -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct Task { pub id: String, @@ -24,6 +27,7 @@ fn default_base() -> String { #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Outcome { + Running, Done, NothingToDo, Red, @@ -37,13 +41,17 @@ pub struct Record { pub worktree: PathBuf, pub outcome: Outcome, pub detail: String, + pub step: Step, + pub progress_at: u64, } fn output(mut command: Command) -> Result { command.stdin(Stdio::null()).output().map_err(|e| e.to_string()) } fn checked(command: Command) -> Result { - let out = output(command)?; + checked_output(output(command)?) +} +fn checked_output(out: Output) -> Result { if !out.status.success() { return Err(String::from_utf8_lossy(&out.stderr).into_owned()); } @@ -112,7 +120,7 @@ pub fn issue_task(repo: &Path, number: u64, model: String) -> Result { }) } -fn acceptance(root: &Path, commands: &[String]) -> Result { +fn acceptance(root: &Path, commands: &[String], lock: &fs::File, offline: bool) -> Result { let mut failures = String::new(); for entry in commands { let (dir, shell) = match entry.strip_prefix("in ").and_then(|s| s.split_once(':')) { @@ -124,8 +132,13 @@ fn acceptance(root: &Path, commands: &[String]) -> Result { return Err("done directory escapes worktree".into()); } let mut c = Command::new("sh"); - c.current_dir(dir).args(["-c", shell]); - let out = output(c)?; + c.current_dir(dir) + .args(["-c", shell]) + .stdin(lock.try_clone().map_err(|e| e.to_string())?); + if offline { + c.env("OXA_OFFLINE", "1"); + } + let out = c.output().map_err(|e| e.to_string())?; if !out.status.success() { failures.push_str(&format!( "{entry}: {}\n{}\n{}\n", @@ -137,15 +150,25 @@ fn acceptance(root: &Path, commands: &[String]) -> Result { } Ok(failures) } -fn agent(binary: &Path, root: &Path, task: &Task, prompt: &str, offline: bool) -> Result<()> { +fn agent( + binary: &Path, + root: &Path, + task: &Task, + prompt: &str, + lock: &fs::File, + offline: bool, +) -> Result<()> { let mut c = Command::new(binary); c.current_dir(root) .args(["-p", "--model", &task.model, "--no-session"]); if offline { c.arg("--offline"); } - c.args(["--", prompt]); - checked(c).map(|_| ()) + // Stdin was null for unattended runs. An empty locked file still supplies EOF, + // and fd 0 keeps the shared kernel lock alive if the runner dies before its child. + c.args(["--", prompt]) + .stdin(lock.try_clone().map_err(|e| e.to_string())?); + checked_output(c.output().map_err(|e| e.to_string())?).map(|_| ()) } pub fn blocked_record(task: &Task, detail: String) -> Record { @@ -155,12 +178,23 @@ pub fn blocked_record(task: &Task, detail: String) -> Record { worktree: PathBuf::new(), outcome: Outcome::Blocked, detail, + step: Step::Finished, + progress_at: state::now(), } } +pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { + run_task_with_options(repo, binary, task, false, offline) +} pub fn run_task(repo: &Path, binary: &Path, task: &Task) -> Result { - run_task_with_policy(repo, binary, task, false) + run_task_with_options(repo, binary, task, false, false) } -pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { +pub fn run_task_with_options( + repo: &Path, + binary: &Path, + task: &Task, + rerun: bool, + offline: bool, +) -> Result { validate(task)?; let root = checked(git(repo, &["rev-parse", "--show-toplevel"]))?; let root = PathBuf::from(root); @@ -173,17 +207,7 @@ pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bo let storage = common.join("oxa-work"); fs::create_dir_all(&storage).map_err(|e| e.to_string())?; let lock_path = storage.join(format!("{}.lock", task.id)); - let lock_file = fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(false) - .open(&lock_path) - .map_err(|e| format!("cannot lock task {}: {e}", task.id))?; - lock_file - .try_lock() - .map_err(|e| format!("task {} is already running: {e}", task.id))?; - // Keep the inode so a crashed runner leaves no stale ownership and concurrent - // runners cannot acquire locks on two different files with the same name. + let lock = state::lock(&lock_path)?; let receipt = storage.join(format!("{}.json", task.id)); // The common git directory must not be an ancestor of the child cwd: the sandbox // deliberately refuses writable git binds with that layout. @@ -199,59 +223,75 @@ pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bo let trees = parent.join(".oxa-worktrees").join(name); fs::create_dir_all(&trees).map_err(|e| e.to_string())?; let tree = trees.join(&task.id); - tree::check_receipt(&receipt, &tree, task)?; - let mut record = Record { - id: task.id.clone(), - branch: task.branch.clone(), - worktree: tree.clone(), - outcome: Outcome::Blocked, - detail: String::new(), - }; - let result = (|| -> Result { - let prepared = tree::prepare(&root, &tree, task)?; - let tree = tree.canonicalize().map_err(|e| e.to_string())?; - let commands = tree::done_from_base(&tree, &prepared.base)?; - let first_agent = agent(binary, &tree, task, &prepared.prompt, offline); - tree::ensure_merged(&tree, &prepared.base, &task.branch)?; - let failures = acceptance(&tree, &commands)?; - if !failures.is_empty() { - let repair_agent = agent( - binary, - &tree, - task, - &format!( - "{}\n\nExternal acceptance checks failed. Repair once:\n{failures}", - prepared.prompt - ), - offline, - ); - tree::ensure_merged(&tree, &prepared.base, &task.branch)?; - let failures = acceptance(&tree, &commands)?; - if !failures.is_empty() { - record.detail = failures; - return Ok(Outcome::Red); - } - repair_agent?; - } else { - first_agent?; + let mut state = if receipt.exists() { + let old = state::read(&receipt)?; + if old.task != *task + || old.record.worktree != tree + || old.record.id != task.id + || old.record.branch != task.branch + { + return Err("task specification changed; use a new task id".into()); } - let head = checked(git(&tree, &["rev-parse", "HEAD"]))?; - let dirty = checked(git(&tree, &["status", "--porcelain"]))?; - Ok(if head == prepared.initial && dirty.is_empty() { - Outcome::NothingToDo + if old.record.step != Step::Finished { + old + } else if !rerun && matches!(old.record.outcome, Outcome::Done | Outcome::NothingToDo) { + return Ok(old.record); } else { - Outcome::Done - }) - })(); - match result { - Ok(outcome) => record.outcome = outcome, - Err(e) => record.detail = e, + new_state(task, tree, offline || old.offline) + } + } else { + new_state(task, tree, offline) + }; + state.offline |= offline; + let step = state.record.step; + state::save(&receipt, &mut state, step)?; + if let Err(error) = execute::execute(&root, binary, task, &receipt, &mut state, &lock) { + state.record.outcome = Outcome::Blocked; + state.record.detail = error; + } + state::save(&receipt, &mut state, Step::Saving)?; + if let Err(error) = execute::save_work(&root, &state.record.worktree, task) { + state.record.outcome = Outcome::Blocked; + if !state.record.detail.is_empty() { + state.record.detail.push('\n'); + } + state.record.detail.push_str(&format!("save: {error}")); + } + state::save(&receipt, &mut state, Step::Finished)?; + Ok(state.record) +} +fn new_state(task: &Task, worktree: PathBuf, offline: bool) -> state::State { + state::State { + record: Record { + id: task.id.clone(), + branch: task.branch.clone(), + worktree, + outcome: Outcome::Running, + detail: String::new(), + step: Step::Preparing, + progress_at: state::now(), + }, + task: task.clone(), + checkpoint: None, + offline, + } +} +pub fn status(repo: &Path) -> Result> { + let common = checked(git( + repo, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?; + let directory = PathBuf::from(common).join("oxa-work"); + if !directory.exists() { + return Ok(vec![]); + } + let mut records = vec![]; + for entry in fs::read_dir(directory).map_err(|e| e.to_string())? { + let path = entry.map_err(|e| e.to_string())?.path(); + if path.extension().is_some_and(|s| s == "json") { + records.push(state::read(&path)?.record); + } } - // Separate receipts avoid overwriting another task's result. Never remove its worktree. - fs::write( - receipt, - serde_json::to_vec_pretty(&record).map_err(|e| e.to_string())?, - ) - .map_err(|e| e.to_string())?; - Ok(record) + records.sort_by(|a, b| a.id.cmp(&b.id)); + Ok(records) } diff --git a/oxa/oxa-work/src/state.rs b/oxa/oxa-work/src/state.rs new file mode 100644 index 000000000..ff0b433e9 --- /dev/null +++ b/oxa/oxa-work/src/state.rs @@ -0,0 +1,94 @@ +//! Durable step checkpoints and a kernel lock that recovers after process death. +use super::{Record, Result, Task}; +use serde::{Deserialize, Serialize}; +use std::fs::{self, File, OpenOptions}; +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::Path; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum Step { + Preparing, + Agent, + Acceptance, + Repair, + Recheck, + Saving, + Finished, +} + +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct Checkpoint { + pub initial: String, + pub base: String, + pub prompt: String, + pub commands: Vec, + pub agent_error: Option, + pub failures: String, +} +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct State { + #[serde(flatten)] + pub record: Record, + pub task: Task, + pub checkpoint: Option, + pub offline: bool, +} +pub(crate) fn now() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +} +pub(crate) fn lock(path: &Path) -> Result { + let file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .mode(0o600) + .open(path) + .map_err(|e| e.to_string())?; + file.try_lock() + .map_err(|e| format!("task is already running: {e}"))?; + file.set_len(0).map_err(|e| e.to_string())?; + // Keep the inode: removing a lock file permits two processes to lock different inodes. + Ok(file) +} +pub(crate) fn read(path: &Path) -> Result { + serde_json::from_slice(&fs::read(path).map_err(|e| e.to_string())?) + .map_err(|e| format!("task state: {e}")) +} +pub(crate) fn save(path: &Path, state: &mut State, step: Step) -> Result<()> { + state.record.step = step; + state.record.progress_at = now(); + let temporary = path.with_extension(format!( + "tmp-{}-{}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() + )); + let result = (|| -> Result<()> { + let mut file = OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(&temporary) + .map_err(|e| e.to_string())?; + file.write_all(&serde_json::to_vec_pretty(state).map_err(|e| e.to_string())?) + .map_err(|e| e.to_string())?; + file.sync_all().map_err(|e| e.to_string())?; + fs::rename(&temporary, path).map_err(|e| e.to_string())?; + File::open(path.parent().ok_or("state has no parent")?) + .and_then(|p| p.sync_all()) + .map_err(|e| e.to_string()) + })(); + if result.is_err() { + let _ = fs::remove_file(temporary); + } + result +} diff --git a/oxa/oxa-work/src/tree.rs b/oxa/oxa-work/src/tree.rs index 122fe61a7..341282286 100644 --- a/oxa/oxa-work/src/tree.rs +++ b/oxa/oxa-work/src/tree.rs @@ -1,6 +1,5 @@ //! Worktree continuation uses ordinary merges; no reset, stash or force checkout. use super::{checked, git, output, Result, Task}; -use std::fs; use std::path::{Path, PathBuf}; pub(crate) struct Prepared { @@ -15,7 +14,7 @@ fn git_path(tree: &Path, name: &str) -> Result { )) .map(PathBuf::from) } -fn merging(tree: &Path) -> Result { +pub(crate) fn merging(tree: &Path) -> Result { Ok(git_path(tree, "MERGE_HEAD")?.exists()) } fn merge_prompt(tree: &Path, task: &Task, base: &str, output: &str) -> Result { @@ -45,23 +44,7 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result _ => return Err(String::from_utf8_lossy(&exists.stderr).into()), } } - let canonical = tree.canonicalize().map_err(|e| e.to_string())?; - let actual = PathBuf::from(checked(git(tree, &["rev-parse", "--show-toplevel"]))?); - let common = |p: &Path| -> Result { - PathBuf::from(checked(git( - p, - &["rev-parse", "--path-format=absolute", "--git-common-dir"], - ))?) - .canonicalize() - .map_err(|e| e.to_string()) - }; - if actual.canonicalize().map_err(|e| e.to_string())? != canonical || common(tree)? != common(repo)? { - return Err("retained directory is not this repository's task worktree".into()); - } - let branch = checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))?; - if branch != format!("refs/heads/{}", task.branch) { - return Err("retained worktree is on a different branch".into()); - } + validate(repo, tree, task)?; for operation in ["rebase-merge", "rebase-apply", "CHERRY_PICK_HEAD", "REVERT_HEAD"] { if git_path(tree, operation)?.exists() { return Err(format!("retained worktree has {operation} in progress")); @@ -109,6 +92,26 @@ pub(crate) fn prepare(repo: &Path, tree: &Path, task: &Task) -> Result prompt: task.prompt.clone(), }) } +pub(crate) fn validate(repo: &Path, tree: &Path, task: &Task) -> Result<()> { + let canonical = tree.canonicalize().map_err(|e| e.to_string())?; + let actual = PathBuf::from(checked(git(tree, &["rev-parse", "--show-toplevel"]))?); + let common = |p: &Path| -> Result { + PathBuf::from(checked(git( + p, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?) + .canonicalize() + .map_err(|e| e.to_string()) + }; + if actual.canonicalize().map_err(|e| e.to_string())? != canonical || common(tree)? != common(repo)? { + return Err("retained directory is not this repository's task worktree".into()); + } + let branch = checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))?; + if branch != format!("refs/heads/{}", task.branch) { + return Err("retained worktree is on a different branch".into()); + } + Ok(()) +} pub(crate) fn ensure_merged(tree: &Path, base: &str, branch: &str) -> Result<()> { if checked(git(tree, &["symbolic-ref", "--quiet", "HEAD"]))? != format!("refs/heads/{branch}") { return Err("agent left the worktree on a different branch".into()); @@ -137,13 +140,3 @@ pub(crate) fn done_from_base(repo: &Path, base: &str) -> Result> { Some(value) => serde_json::from_value(value.clone()).map_err(|e| format!("done commands: {e}")), } } -pub(crate) fn check_receipt(receipt: &Path, tree: &Path, task: &Task) -> Result<()> { - if receipt.exists() { - let record: super::Record = serde_json::from_slice(&fs::read(receipt).map_err(|e| e.to_string())?) - .map_err(|e| format!("retained receipt: {e}"))?; - if record.id != task.id || record.branch != task.branch || record.worktree != tree { - return Err("retained receipt belongs to a different task branch or worktree".into()); - } - } - Ok(()) -} From f6321b0c5ee42508cc0795930060dc07b72c0fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:03:57 +0000 Subject: [PATCH 08/26] Keep task ownership throughout saving and detect interrupted status --- oxa/docs/work.md | 3 ++ oxa/oxa-cli/tests/work_state.rs | 80 +++++++++++++++++++++++++++++++++ oxa/oxa-work/src/execute.rs | 20 +++++---- oxa/oxa-work/src/lib.rs | 29 +++++++++++- 4 files changed, 121 insertions(+), 11 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 896fc3792..d6089db74 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -73,3 +73,6 @@ step. Reusing an ID with a different specification is refused; use a new ID. Restart continues the saved step. An agent or check interrupted within its step may run again; tasks and checks should tolerate that. Checkpoints after an agent step avoid replaying it when only acceptance or saving remains. + +Status probes the task lock. An interrupted phase with no surviving worker is shown +as blocked; the checkpoint stays intact for the next task-file invocation. diff --git a/oxa/oxa-cli/tests/work_state.rs b/oxa/oxa-cli/tests/work_state.rs index c5464e2fd..8101df4f5 100644 --- a/oxa/oxa-cli/tests/work_state.rs +++ b/oxa/oxa-cli/tests/work_state.rs @@ -208,3 +208,83 @@ fn child_keeps_lock_after_runner_is_killed_then_task_restarts() { assert_eq!(restarted["outcome"], "nothing_to_do", "{restarted}"); assert_eq!(restarted["step"], "finished"); } + +#[test] +fn saving_git_keeps_lock_after_runner_dies_during_commit_hook() { + use std::os::unix::fs::PermissionsExt; + struct Release(PathBuf); + impl Drop for Release { + fn drop(&mut self) { + let _ = fs::write(&self.0, "release"); + } + } + let env = TestEnv::new(); + setup(&env, "[]"); + trust(&env); + let release = Release(env.home().join("release-hook")); + let hook = env.write( + "work/project/.git/hooks/pre-commit", + r#"#!/bin/sh +touch "$HOME/hook-started" +attempt=0 +while ! test -f "$HOME/release-hook"; do + attempt=$((attempt + 1)) + if test "$attempt" -ge 500; then exit 9; fi + sleep 0.01 +done +"#, + ); + fs::set_permissions(hook, fs::Permissions::from_mode(0o755)).unwrap(); + let server = + FakeServer::start(vec![write_turn(), ScriptTurn::text("done")], FakeOpts::default()).unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs(&server.url())) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + until(|| env.home().join("hook-started").exists()); + child.kill().unwrap(); + child.wait().unwrap(); + let blocked = run(&env, vec![]); + assert!( + blocked["detail"].as_str().unwrap().contains("already running"), + "{blocked}" + ); + assert_eq!(load(&env)["step"], "saving"); + fs::write(&release.0, "release").unwrap(); + let path = project(&env).join(".git/oxa-work/test.lock"); + until(|| { + let file = fs::OpenOptions::new().read(true).write(true).open(&path).unwrap(); + file.try_lock().is_ok() + }); + let record = run(&env, vec![]); + assert_eq!(record["outcome"], "done", "{record}"); + assert_eq!(git(&env, tree(&record), &["show", "HEAD:answer.txt"]), "retained"); +} +#[test] +fn status_reports_an_unlocked_interrupted_phase_as_blocked_without_mutating_state() { + let env = TestEnv::new(); + setup(&env, "[]"); + run(&env, vec![ScriptTurn::text("done")]); + let mut state = load(&env); + state["step"] = "agent".into(); + state["outcome"] = "running".into(); + replace_state(&env, &state); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "--status"]) + .current_dir(project(&env)) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .output() + .unwrap(); + let text = String::from_utf8_lossy(&out.stdout); + assert!( + text.starts_with("test blocked ") && text.trim().ends_with("agent"), + "{text}" + ); + assert_eq!(load(&env), state); +} diff --git a/oxa/oxa-work/src/execute.rs b/oxa/oxa-work/src/execute.rs index 115ec7317..a6e08fd12 100644 --- a/oxa/oxa-work/src/execute.rs +++ b/oxa/oxa-work/src/execute.rs @@ -101,23 +101,25 @@ pub(crate) fn execute( } } /// Save all staged, unstaged and untracked files. Conflicts are kept in progress for the agent. -pub(crate) fn save_work(repo: &Path, tree: &Path, task: &Task) -> Result<()> { +pub(crate) fn save_work(repo: &Path, tree: &Path, task: &Task, lock: &File) -> Result<()> { if !tree.exists() { return Ok(()); } tree::validate(repo, tree, task)?; - if tree::merging(tree)? || !checked(git(tree, &["diff", "--name-only", "--diff-filter=U"]))?.is_empty() { + let run = |args: &[&str]| -> Result { + let mut command = git(tree, args); + command.stdin(lock.try_clone().map_err(|e| e.to_string())?); + super::checked_output(command.output().map_err(|e| e.to_string())?) + }; + if tree::merging(tree)? || !run(&["diff", "--name-only", "--diff-filter=U"])?.is_empty() { return Err("unresolved merge retained in progress; files were not discarded".into()); } - if checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + if run(&["status", "--porcelain"])?.is_empty() { return Ok(()); } - checked(git(tree, &["add", "--all"]))?; - checked(git( - tree, - &["commit", "-m", &format!("oxa work: save task {}", task.id)], - ))?; - if !checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + run(&["add", "--all"])?; + run(&["commit", "-m", &format!("oxa work: save task {}", task.id)])?; + if !run(&["status", "--porcelain"])?.is_empty() { return Err("uncommitted changes remain after saving; retained for inspection".into()); } Ok(()) diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 0ddd10e97..1883032d2 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -250,7 +250,7 @@ pub fn run_task_with_options( state.record.detail = error; } state::save(&receipt, &mut state, Step::Saving)?; - if let Err(error) = execute::save_work(&root, &state.record.worktree, task) { + if let Err(error) = execute::save_work(&root, &state.record.worktree, task, &lock) { state.record.outcome = Outcome::Blocked; if !state.record.detail.is_empty() { state.record.detail.push('\n'); @@ -289,7 +289,32 @@ pub fn status(repo: &Path) -> Result> { for entry in fs::read_dir(directory).map_err(|e| e.to_string())? { let path = entry.map_err(|e| e.to_string())?.path(); if path.extension().is_some_and(|s| s == "json") { - records.push(state::read(&path)?.record); + let mut value = state::read(&path)?; + if value.record.step != Step::Finished { + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .open(path.with_extension("lock")); + let busy = match file { + Ok(file) => match file.try_lock() { + Ok(()) => { + value = state::read(&path)?; + false + } + Err(std::fs::TryLockError::WouldBlock) => true, + Err(error) => return Err(error.to_string()), + }, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => false, + Err(error) => return Err(error.to_string()), + }; + if value.record.step != Step::Finished { + value.record.outcome = if busy { Outcome::Running } else { Outcome::Blocked }; + if !busy { + value.record.detail = "task was interrupted; rerun its task file to resume".into(); + } + } + } + records.push(value.record); } } records.sort_by(|a, b| a.id.cmp(&b.id)); From 232eb0efc75474912093db5f9c4734195fa8eb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:11:09 +0000 Subject: [PATCH 09/26] Publish accepted work and file scrubbed duplicate-checked problem notes (#333) --- oxa/docs/work.md | 27 ++- oxa/oxa-cli/src/args.rs | 2 +- oxa/oxa-cli/src/dispatch.rs | 4 +- oxa/oxa-cli/src/lib.rs | 1 + oxa/oxa-cli/src/work_cmd.rs | 56 ++++- oxa/oxa-cli/src/work_reports.rs | 380 ++++++++++++++++++++++++++++++ oxa/oxa-cli/tests/work_publish.rs | 256 ++++++++++++++++++++ oxa/oxa-work/src/execute.rs | 19 +- oxa/oxa-work/src/lib.rs | 43 +++- oxa/oxa-work/src/notes.rs | 51 ++++ oxa/oxa-work/src/publish.rs | 221 +++++++++++++++++ oxa/oxa-work/src/state.rs | 3 + 12 files changed, 1047 insertions(+), 16 deletions(-) create mode 100644 oxa/oxa-cli/src/work_reports.rs create mode 100644 oxa/oxa-cli/tests/work_publish.rs create mode 100644 oxa/oxa-work/src/notes.rs create mode 100644 oxa/oxa-work/src/publish.rs diff --git a/oxa/docs/work.md b/oxa/docs/work.md index d6089db74..14a62278c 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -46,7 +46,7 @@ Uncommitted task work, including red work, is saved as a commit with the reposit configured identity. Commit hooks run normally. A missing identity or rejected commit blocks the task and retains every file. Unresolved conflicts remain in progress for the agent; they are never staged as a fabricated resolution. There is no automatic -cleanup, push or PR creation yet. +cleanup. Publication is explicitly opt-in as described below. To continue an interrupted, red or blocked task, run the same file again. Use `--rerun` to continue a completed task after its base changes. The retained receipt @@ -76,3 +76,28 @@ step avoid replaying it when only acceptance or saving remains. Status probes the task lock. An interrupted phase with no surviving worker is shown as blocked; the checkpoint stays intact for the next task-file invocation. + + +`oxa work tasks.json --publish` pushes the accepted, saved commit to `origin` and +creates or updates its PR through `gh`. Red acceptance creates a draft; green +acceptance makes the PR ready. Nothing-to-do and blocked tasks do not create PRs. +The runner checks existing PR queue membership before every push and refuses a +queued PR; use a new task branch for follow-up work. It refuses branch changes +made after acceptance until `--rerun` checks them. PR bodies include task ID and +acceptance state, without raw prompts, transcripts or failed-check output. +A publication failure keeps a publication checkpoint; another `--publish` retries +without replaying the agent. Offline mode refuses network publication. + +`--report-problems` independently authorizes filing the task's own structured +problem notes using the `report-issue` workflow. This flag must be launched by the +user outside an agent tool. Normal `oxa report-issue submit` still requires its +terminal confirmation. Reports use the user setting `oxa.reportIssue.repo`; +project settings cannot choose a destination. Notes, titles and queries pass +through the existing report scrubber. The runner performs two or three symptom +queries against open issues and PRs and reads the top matches before writing. +It comments only new notes on a matching issue, links an existing fixing PR, or +files a new report with related links. Search failure, offline mode or missing +report configuration keeps a local scrubbed draft and records its path/detail. +A later explicit `--report-problems` retries local reports without replaying the +agent. No notes are filed unless this flag is present; model output and acceptance +logs are never treated as report bodies. diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index ec2fe79aa..345b5700c 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -177,7 +177,7 @@ const COMMANDS: &[(&str, &str)] = &[ "Draft a scrubbed ox-llm issue; submit posts it only after you type yes", ), ( - "oxa work [--rerun] | --issue N --model MODEL | --status", + "oxa work [--rerun] [--publish] [--report-problems] | --status", "Run tasks in isolated worktrees with external acceptance checks", ), ( diff --git a/oxa/oxa-cli/src/dispatch.rs b/oxa/oxa-cli/src/dispatch.rs index 4ef83d386..052ba1b9a 100644 --- a/oxa/oxa-cli/src/dispatch.rs +++ b/oxa/oxa-cli/src/dispatch.rs @@ -125,7 +125,9 @@ pub fn run(argv: &[String], env: &Env, io: Stdio) -> i32 { Err(e) => return report(&mut *stderr, &e), }; let result = match sub { - Subcommand::Work(words) => return crate::work_cmd::run(&words, &cwd, &mut *stdout, &mut *stderr), + Subcommand::Work(words) => { + return crate::work_cmd::run(&words, &cwd, &dirs, env, &mut *stdout, &mut *stderr) + } Subcommand::Mcp(words) => run_mcp(&words, &dirs, &cwd, &mut *stdout, &mut *stderr), Subcommand::Pkg(verb, words) => run_pkg(&verb, &words, &dirs, &cwd, &mut *stdout, &mut *stderr), Subcommand::Config(words) => run_config(&words, &dirs, &cwd, &mut *stdout), diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index f5cc30f16..afd53316a 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -32,6 +32,7 @@ mod sandbox_layout; mod themes; mod wipe_cmd; mod work_cmd; +mod work_reports; pub use args::{parse_args, usage_text, ApiKey, Args, ModeArg, Subcommand}; pub use config_cmd::run_config; diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 6978aa9f6..0ac619a6b 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -1,7 +1,14 @@ use std::io::Write; use std::path::{Path, PathBuf}; -pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dyn Write) -> i32 { +pub(crate) fn run( + args: &[String], + cwd: &Path, + dirs: &oxa_config::Dirs, + env: &oxa_config::Env, + out: &mut dyn Write, + err: &mut dyn Write, +) -> i32 { let result = (|| -> Result { if args == ["--status"] { for record in oxa_work::status(cwd)? { @@ -19,8 +26,21 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy } return Ok(0); } - let rerun = args.last().is_some_and(|s| s == "--rerun"); - let args = if rerun { &args[..args.len() - 1] } else { args }; + let mut end = args.len(); + let (mut rerun, mut publish, mut reports) = (false, false, false); + while let Some(flag) = args.get(end.wrapping_sub(1)) { + match flag.as_str() { + "--rerun" => rerun = true, + "--publish" => publish = true, + "--report-problems" => reports = true, + _ => break, + } + end -= 1; + } + let args = &args[..end]; + if (publish || reports) && env.get("OXA_CODING_AGENT").is_some() { + return Err("publishing and problem reporting must be explicitly launched by the user outside an agent tool".into()); + } let tasks = match args { [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, [flag, number, model_flag, model] if flag == "--issue" && model_flag == "--model" => { @@ -35,7 +55,7 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy } _ => { return Err( - "usage: oxa work [--rerun] | --issue N --model MODEL [--rerun] | --status" + "usage: oxa work | --issue N --model MODEL | --status; task runs accept --rerun, --publish, --report-problems" .into(), ) } @@ -43,8 +63,32 @@ pub(crate) fn run(args: &[String], cwd: &Path, out: &mut dyn Write, err: &mut dy let binary: PathBuf = std::env::current_exe().map_err(|e| e.to_string())?; let mut code = 0; for task in tasks { - let record = oxa_work::run_task_with_options(cwd, &binary, &task, rerun, oxa_proto::offline()) - .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); + let mut record = + oxa_work::run_task_with_options(cwd, &binary, &task, rerun, oxa_proto::offline()) + .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); + if publish && !record.worktree.as_os_str().is_empty() { + record = oxa_work::publish(cwd, &task, oxa_proto::offline()) + .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); + } + if reports && !record.worktree.as_os_str().is_empty() { + let settings = oxa_config::Settings::load(dirs, cwd, false); + let ctx = crate::report_cmd::ScrubCtx::from_env(env, dirs); + record = + oxa_work::report_notes(cwd, &task, oxa_proto::offline(), &mut |note, lock, offline| { + let mut runner = |program: &str, args: &[String]| { + crate::work_reports::process(program, args, cwd, lock) + }; + crate::work_reports::report( + note, + dirs, + &settings, + &ctx, + offline || oxa_proto::offline(), + &mut runner, + ) + }) + .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); + } if matches!( record.outcome, oxa_work::Outcome::Red | oxa_work::Outcome::Blocked diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs new file mode 100644 index 000000000..d5cd7e763 --- /dev/null +++ b/oxa/oxa-cli/src/work_reports.rs @@ -0,0 +1,380 @@ +//! The report-issue workflow for an explicit user-launched --report-problems run. +//! Normal `report-issue submit` retains its controlling-terminal confirmation. +use crate::report_cmd::{clean_notes, report_repo, save_draft, scrub, Runner, ScrubCtx}; +use oxa_config::{Dirs, Settings}; +use oxa_work::{ProblemNote, ReportRecord}; +use serde_json::Value; +use std::{ + fs::File, + io::Read, + path::Path, + process::{Command, Stdio}, + time::{Duration, Instant}, +}; + +pub(crate) fn process(program: &str, args: &[String], cwd: &Path, lock: &File) -> Option<(i32, String)> { + let mut child = Command::new(program) + .args(args) + .current_dir(cwd) + .stdin(Stdio::from(lock.try_clone().ok()?)) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .ok()?; + let mut stdout = child.stdout.take()?; + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let mut out = String::new(); + let result = (&mut stdout).take(1_048_576).read_to_string(&mut out); + let _ = tx.send(result.ok().map(|_| out)); + }); + let start = Instant::now(); + loop { + match child.try_wait() { + Ok(Some(status)) => { + return Some(( + status.code().unwrap_or(1), + rx.recv_timeout(Duration::from_secs(2)).ok()??, + )) + } + Ok(None) if start.elapsed() < Duration::from_secs(30) => { + std::thread::sleep(Duration::from_millis(20)) + } + _ => { + let _ = child.kill(); + let _ = child.wait(); + return None; + } + } + } +} +fn call(runner: Runner<'_>, args: &[String]) -> Result { + match runner("gh", args) { + Some((0, text)) => Ok(text), + _ => Err("GitHub search or write failed; the local draft is retained".into()), + } +} +fn words(items: &[&str]) -> Vec { + items.iter().map(|s| s.to_string()).collect() +} +fn matches(note: &ProblemNote, candidate: &Value) -> bool { + let title = candidate["title"].as_str().unwrap_or_default().to_lowercase(); + if title == note.title.to_lowercase() { + return true; + } + let all = format!( + "{title} {}", + candidate["body"].as_str().unwrap_or_default().to_lowercase() + ); + let terms: std::collections::HashSet<_> = note + .keywords + .iter() + .flat_map(|q| q.split_whitespace()) + .filter(|word| word.len() >= 2) + .map(str::to_lowercase) + .collect(); + terms.len() >= 3 && terms.iter().all(|word| all.contains(word)) +} +pub(crate) fn report( + note: &ProblemNote, + dirs: &Dirs, + settings: &Settings, + ctx: &ScrubCtx, + offline: bool, + runner: Runner<'_>, +) -> Result { + let (draft, title, body) = + save_draft(¬e.title, ¬e.notes, dirs, ctx, runner).map_err(|e| e.to_string())?; + let mut result = ReportRecord { + disposition: "local".into(), + draft, + url: None, + detail: String::new(), + }; + if offline { + result.detail = "offline: report retained locally".into(); + return Ok(result); + } + let Some(repo) = report_repo(settings) else { + result.detail = "issue reporting is not configured; draft retained locally".into(); + return Ok(result); + }; + let note = ProblemNote { + title, + notes: clean_notes(¬e.notes, ctx), + keywords: note.keywords.iter().map(|q| scrub(q, ctx)).collect(), + }; + if note.notes.is_empty() + || !(2..=3).contains(¬e.keywords.len()) + || note.keywords.iter().any(|q| q.trim().is_empty()) + { + result.detail = "report has no safe symptom text or queries; draft retained locally".into(); + return Ok(result); + } + let attempt = (|| -> Result<(), String> { + let mut candidates = vec![]; + for query in ¬e.keywords { + for kind in ["issue", "pr"] { + let text = call( + runner, + &words(&[ + kind, + "list", + "--repo", + &repo, + "--state", + "open", + "--search", + query, + "--limit", + "5", + "--json", + "number,title,url", + ]), + )?; + let rows: Vec = + serde_json::from_str(&text).map_err(|_| "invalid GitHub search response".to_string())?; + for row in rows { + let number = row["number"].as_u64().ok_or("search match has no number")?; + if !candidates.contains(&(kind, number)) { + candidates.push((kind, number)); + } + } + } + } + // Read all top matches before any write, including PRs which may already fix it. + let mut read = vec![]; + for (kind, number) in candidates { + let fields = if kind == "issue" { + "title,body,url,comments" + } else { + "title,body,url" + }; + let text = call( + runner, + &words(&[ + kind, + "view", + &number.to_string(), + "--repo", + &repo, + "--json", + fields, + ]), + )?; + let value: Value = + serde_json::from_str(&text).map_err(|_| "invalid GitHub match response".to_string())?; + if !value.is_object() + || value["title"].as_str().is_none() + || value["body"].as_str().is_none() + || value["url"].as_str().is_none() + { + return Err("incomplete GitHub match; draft retained locally".into()); + } + read.push((kind, number, value)); + } + if let Some((_, _, value)) = read + .iter() + .find(|(kind, _, v)| *kind == "pr" && matches(¬e, v)) + { + result.disposition = "existing_pr".into(); + result.url = value["url"].as_str().map(str::to_string); + return Ok(()); + } + let body_path = result.draft.with_extension("work-body.md"); + if let Some((_, number, value)) = read + .iter() + .find(|(kind, _, v)| *kind == "issue" && matches(¬e, v)) + { + let already = value["body"].as_str().unwrap_or_default().contains(¬e.notes) + || value["comments"].as_array().is_some_and(|rows| { + rows.iter() + .any(|c| c["body"].as_str().unwrap_or_default().contains(¬e.notes)) + }); + if !already { + oxa_resources::write_atomic(&body_path, note.notes.as_bytes()).map_err(|e| e.to_string())?; + call( + runner, + &words(&[ + "issue", + "comment", + &number.to_string(), + "--repo", + &repo, + "--body-file", + &body_path.to_string_lossy(), + ]), + )?; + } + result.disposition = "existing_issue".into(); + result.url = value["url"].as_str().map(str::to_string); + } else { + let links = read + .iter() + .filter_map(|(_, _, v)| v["url"].as_str()) + .collect::>() + .join("\n"); + let body = if links.is_empty() { + body.clone() + } else { + format!("{body}\n\nRelated open reports (different symptoms):\n{links}\n") + }; + let body = scrub(&body, ctx); + oxa_resources::write_atomic(&body_path, body.as_bytes()).map_err(|e| e.to_string())?; + let url = call( + runner, + &words(&[ + "issue", + "create", + "--repo", + &repo, + "--title", + ¬e.title, + "--body-file", + &body_path.to_string_lossy(), + ]), + )?; + result.disposition = "filed".into(); + result.url = Some(url.trim().into()); + } + let _ = std::fs::remove_file(body_path); + Ok(()) + })(); + if let Err(error) = attempt { + result.detail = error; + } + Ok(result) +} + +#[cfg(test)] +mod tests { + use super::*; + use oxa_fake::TestEnv; + fn note() -> ProblemNote { + ProblemNote { + title: "oxa work commit is read only".into(), + notes: "Another synthetic reproduction of a read only git commit.".into(), + keywords: vec!["oxa work".into(), "read only git".into()], + } + } + fn settings() -> Settings { + Settings::from_values( + serde_json::json!({"oxa":{"reportIssue":{"repo":"example/project"}}}), + serde_json::json!({}), + ) + } + #[test] + fn matching_issue_is_commented_after_all_queries_and_reads() { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut log = vec![]; + let mut runner = |program: &str, args: &[String]| { + if program != "gh" { + return Some((0, "{}".into())); + } + log.push(args.to_vec()); + match (args[0].as_str(), args[1].as_str()) { + ("issue", "list") => Some((0, r#"[{"number":7,"title":"oxa work commit is read only"}]"#.into())), + ("pr", "list") => Some((0, "[]".into())), + ("issue", "view") => Some((0, r#"{"title":"oxa work commit is read only","body":"Original symptom","url":"https://github.com/example/project/issues/7","comments":[]}"#.into())), + ("issue", "comment") => { let body = std::fs::read_to_string(args.last().unwrap()).unwrap(); assert_eq!(body, note().notes); Some((0, "commented".into())) }, + _ => panic!("unexpected write: {args:?}"), + } + }; + let result = report( + ¬e(), + &dirs, + &settings(), + &ScrubCtx::default(), + false, + &mut runner, + ) + .unwrap(); + assert_eq!(result.disposition, "existing_issue"); + assert_eq!(log.iter().filter(|a| a[1] == "list").count(), 4); + assert_eq!(log.last().unwrap()[1], "comment"); + } + #[test] + fn failed_search_and_offline_never_file_blind() { + for offline in [false, true] { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut gh_calls = 0; + let mut runner = |program: &str, _: &[String]| { + if program == "gh" { + gh_calls += 1; + None + } else { + Some((0, "{}".into())) + } + }; + let result = report( + ¬e(), + &dirs, + &settings(), + &ScrubCtx::default(), + offline, + &mut runner, + ) + .unwrap(); + assert_eq!(result.disposition, "local"); + assert!(result.draft.exists()); + assert!(!result.detail.is_empty()); + assert_eq!(gh_calls, if offline { 0 } else { 1 }); + } + } + #[test] + fn an_open_fixing_pr_prevents_issue_or_comment_writes() { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut runner = |program: &str, args: &[String]| { + if program != "gh" { + return Some((0, "{}".into())); + } + if args[1] == "list" { + return Some(( + 0, + if args[0] == "pr" { + r#"[{"number":9}]"# + } else { + "[]" + } + .into(), + )); + } + assert_eq!(args[1], "view"); + Some((0, r#"{"title":"oxa work commit is read only","body":"Fixed by this PR","url":"https://github.com/example/project/pull/9"}"#.into())) + }; + let result = report( + ¬e(), + &dirs, + &settings(), + &ScrubCtx::default(), + false, + &mut runner, + ) + .unwrap(); + assert_eq!(result.disposition, "existing_pr"); + } + #[test] + fn new_report_is_scrubbed_and_related_reports_are_linked() { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut n = note(); + n.notes = "Synthetic reproduction token=example at /home/synthetic/project with user@example.invalid" + .into(); + let mut runner = |program: &str, args: &[String]| { + if program != "gh" { + return Some((0, "{}".into())); + } + match args[1].as_str() { + "list" => Some((0, if args[0] == "issue" { r#"[{"number":8}]"# } else { "[]" }.into())), + "view" => Some((0, r#"{"title":"Different symptom","body":"Unrelated reproduction","url":"https://github.com/example/project/issues/8","comments":[]}"#.into())), + "create" => { let body = std::fs::read_to_string(args.last().unwrap()).unwrap(); assert!(!body.contains("token=example") && !body.contains("/home/synthetic") && !body.contains("user@example.invalid")); assert!(body.contains("issues/8")); Some((0, "https://github.com/example/project/issues/10".into())) }, + _ => panic!("unexpected command"), + } + }; + let result = report(&n, &dirs, &settings(), &ScrubCtx::default(), false, &mut runner).unwrap(); + assert_eq!(result.disposition, "filed"); + } +} diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs new file mode 100644 index 000000000..ff0c2ae78 --- /dev/null +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -0,0 +1,256 @@ +//! Local bare pushes and fake gh, including draft updates and merge-queue protection. +#[path = "common/work.rs"] +mod work; +use oxa_fake::{ScriptTurn, TestEnv}; +use std::{fs, path::PathBuf, process::Command}; +use work::{git, run, run_args, setup}; +fn configure(env: &TestEnv) -> String { + use std::os::unix::fs::PermissionsExt; + env.write( + "home/.oxa/agent/settings.json", + r#"{"defaultProjectTrust":"always","oxa":{"reportIssue":{"repo":"example/project"}}}"#, + ); + let gh = env.write("tmp/gh", r##"#!/bin/sh +printf '%s\n' "$*" >> "$HOME/gh-calls" +case "$1 $2" in + 'repo view') printf '%s' '{"nameWithOwner":"example/project"}' ;; + 'pr list') if test -f "$HOME/pr-state.json"; then printf '['; cat "$HOME/pr-state.json"; printf ']'; else printf '[]'; fi ;; + 'api graphql') if test -f "$HOME/queued"; then printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":{"id":"queue"}}}}}'; else printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":null}}}}'; fi ;; + 'pr create') + draft=false + body_next=false + for arg in "$@"; do + if test "$body_next" = true; then cat "$arg" > "$HOME/published-body"; body_next=false; fi + if test "$arg" = '--draft'; then draft=true; fi + if test "$arg" = '--body-file'; then body_next=true; fi + done + printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s}' "$draft" > "$HOME/pr-state.json" + printf '%s' 'https://github.com/example/project/pull/1' ;; + 'pr edit') : ;; + 'pr ready') + draft=false + for arg in "$@"; do if test "$arg" = '--undo'; then draft=true; fi; done + printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s}' "$draft" > "$HOME/pr-state.json" ;; + *) exit 7 ;; +esac +"##); + let git_wrapper = env.write("tmp/git", "#!/bin/sh\nif test \"$1\" = push; then printf 'push\\n' >> \"$HOME/push-calls\"; fi\nexec /usr/bin/git \"$@\"\n"); + let controller = env.write("tmp/oxc", "#!/bin/sh\nprintf '{}'\n"); + for file in [gh, git_wrapper, controller] { + fs::set_permissions(file, fs::Permissions::from_mode(0o755)).unwrap(); + } + format!("{}:{}", env.tmp().display(), std::env::var("PATH").unwrap()) +} +fn edits() -> Vec { + vec![ + ScriptTurn::tool("write", "write", r#"{"path":"answer.txt","content":"published"}"#), + ScriptTurn::text("done"), + ] +} +#[test] +fn publishes_local_branch_and_updates_same_pr_on_restart() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let first = run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(first["outcome"], "done", "{first}"); + assert_eq!(first["draft"], false); + assert_eq!(first["pull_request"], "https://github.com/example/project/pull/1"); + assert_eq!( + git( + &env, + &env.work(), + &[ + "--git-dir", + "remote.git", + "show", + "refs/heads/agent/test:answer.txt" + ] + ), + "published" + ); + let again = run_args(&env, vec![], &["work", "../tasks.json", "--publish"], Some(path)); + assert_eq!(again["outcome"], "done", "{again}"); + let calls = fs::read_to_string(env.home().join("gh-calls")).unwrap(); + assert_eq!(calls.lines().filter(|l| l.starts_with("pr create")).count(), 1); + assert_eq!(calls.lines().filter(|l| l.starts_with("pr edit")).count(), 1); + let body = fs::read_to_string(env.home().join("published-body")).unwrap(); + assert!(!body.contains("Finish task")); +} +#[test] +fn red_checks_create_a_draft_pr() { + let env = TestEnv::new(); + setup(&env, r#"["printf red > failed-check; exit 1"]"#); + let path = configure(&env); + let record = run_args( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("still red")], + &["work", "../tasks.json", "--publish"], + Some(path), + ); + assert_eq!(record["outcome"], "red", "{record}"); + assert_eq!(record["draft"], true); + assert!(record["pull_request"].is_string()); +} +#[test] +fn queued_pr_is_never_pushed_and_publication_retries_without_agent_replay() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + env.write("home/queued", "yes"); + let blocked = run_args( + &env, + vec![], + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(blocked["outcome"], "blocked", "{blocked}"); + assert!(blocked["detail"].as_str().unwrap().contains("merge queue")); + assert_eq!( + fs::read_to_string(env.home().join("push-calls")) + .unwrap() + .lines() + .count(), + 1 + ); + fs::remove_file(env.home().join("queued")).unwrap(); + let retried = run_args(&env, vec![], &["work", "../tasks.json", "--publish"], Some(path)); + assert_eq!(retried["outcome"], "done", "{retried}"); +} +#[test] +fn offline_publication_never_starts_gh_or_push() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let record = run_args( + &env, + edits(), + &["--offline", "work", "../tasks.json", "--publish"], + Some(path), + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("offline")); + assert!(!env.home().join("gh-calls").exists()); + assert!(!env.home().join("push-calls").exists()); +} +#[test] +fn branch_drift_after_acceptance_cannot_publish_unchecked_commits() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let done = run(&env, edits()); + let tree = PathBuf::from(done["worktree"].as_str().unwrap()); + fs::write(tree.join("later.txt"), "unchecked").unwrap(); + git(&env, &tree, &["add", "."]); + git(&env, &tree, &["commit", "-m", "later commit"]); + let record = run_args(&env, vec![], &["work", "../tasks.json", "--publish"], Some(path)); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"] + .as_str() + .unwrap() + .contains("changed after acceptance")); + assert!(!env.home().join("push-calls").exists()); +} +#[test] +fn own_problem_notes_are_captured_and_failed_search_keeps_a_local_draft() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let text = "done [{\"title\":\"oxa work symptom\",\"notes\":\"Synthetic reproduction token=example\",\"keywords\":[\"oxa work\",\"synthetic symptom\"]}]"; + let record = run_args( + &env, + vec![ScriptTurn::text(text)], + &["work", "../tasks.json", "--report-problems"], + Some(path), + ); + assert_eq!(record["problem_notes"].as_array().unwrap().len(), 1); + assert_eq!(record["reports"][0]["disposition"], "local"); + let draft = fs::read_to_string(record["reports"][0]["draft"].as_str().unwrap()).unwrap(); + assert!(!draft.contains("token=example")); + let calls = fs::read_to_string(env.home().join("gh-calls")).unwrap(); + assert!(!calls.contains("issue create")); +} +#[test] +fn a_model_tool_cannot_opt_itself_into_reporting_or_publishing() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json", "--report-problems"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .env("OXA_CODING_AGENT", "1") + .env("PATH", path) + .output() + .unwrap(); + assert_eq!(out.status.code(), Some(2)); + assert!(!env.home().join("gh-calls").exists()); +} + +#[test] +fn green_rerun_marks_an_existing_red_draft_ready() { + let env = TestEnv::new(); + setup( + &env, + r#"["test -f fixed || { printf red > failed-check; exit 1; }"]"#, + ); + let path = configure(&env); + let red = run_args( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("still red")], + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(red["draft"], true, "{red}"); + let green = run_args( + &env, + vec![ + ScriptTurn::tool("fix", "write", r#"{"path":"fixed","content":"fixed"}"#), + ScriptTurn::text("done"), + ], + &["work", "../tasks.json", "--rerun", "--publish"], + Some(path), + ); + assert_eq!(green["outcome"], "done", "{green}"); + assert_eq!(green["draft"], false); + assert!(fs::read_to_string(env.home().join("gh-calls")) + .unwrap() + .contains("pr ready")); +} +#[test] +fn a_local_report_retries_without_replaying_the_completed_task() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let text = "[{\"title\":\"oxa work symptom\",\"notes\":\"Synthetic reproduction\",\"keywords\":[\"oxa work\",\"synthetic symptom\"]}]"; + let first = run_args( + &env, + vec![ScriptTurn::text(text)], + &["work", "../tasks.json", "--report-problems"], + Some(path.clone()), + ); + assert_eq!(first["reports"][0]["disposition"], "local"); + let gh = env.tmp().join("gh"); + let script = fs::read_to_string(&gh).unwrap().replace(" *) exit 7 ;;", " 'issue list') printf '[]' ;;\n 'issue create') printf '%s' 'https://github.com/example/project/issues/2' ;;\n *) exit 7 ;;"); + fs::write(gh, script).unwrap(); + let retry = run_args( + &env, + vec![], + &["work", "../tasks.json", "--report-problems"], + Some(path), + ); + assert_eq!(retry["reports"][0]["disposition"], "filed", "{retry}"); + assert_eq!(retry["step"], "finished"); +} diff --git a/oxa/oxa-work/src/execute.rs b/oxa/oxa-work/src/execute.rs index a6e08fd12..a62807e77 100644 --- a/oxa/oxa-work/src/execute.rs +++ b/oxa/oxa-work/src/execute.rs @@ -34,7 +34,7 @@ pub(crate) fn execute( state.checkpoint = Some(Checkpoint { initial: prepared.initial, base: prepared.base, - prompt: prepared.prompt, + prompt: prepared.prompt + super::notes::INSTRUCTIONS, commands, agent_error: None, failures: String::new(), @@ -54,12 +54,25 @@ pub(crate) fn execute( point.prompt.clone() }; let result = agent(binary, &tree, task, &prompt, lock, state.offline); + let error = match result { + Ok(reply) => { + for note in reply.notes { + if state.record.problem_notes.len() < 5 + && !state.record.problem_notes.contains(¬e) + { + state.record.problem_notes.push(note); + } + } + reply.error + } + Err(error) => Some(error), + }; tree::ensure_merged(&tree, &point.base, &task.branch)?; state .checkpoint .as_mut() .ok_or("missing task checkpoint")? - .agent_error = result.err(); + .agent_error = error; state::save( path, state, @@ -96,7 +109,7 @@ pub(crate) fn execute( state::save(path, state, Step::Saving)?; } } - Step::Saving | Step::Finished => return Ok(()), + Step::Saving | Step::Finished | Step::Publishing | Step::Reporting => return Ok(()), } } } diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 1883032d2..b29965cac 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -1,5 +1,9 @@ //! Opt-in, sequential unattended task execution. Worktrees are retained on every outcome. mod execute; +mod notes; +mod publish; +pub use notes::{ProblemNote, ReportRecord}; +pub use publish::{publish, report_notes}; mod state; mod tree; use serde::{Deserialize, Serialize}; @@ -43,6 +47,11 @@ pub struct Record { pub detail: String, pub step: Step, pub progress_at: u64, + pub head: Option, + pub pull_request: Option, + pub draft: bool, + pub problem_notes: Vec, + pub reports: Vec, } fn output(mut command: Command) -> Result { @@ -157,7 +166,7 @@ fn agent( prompt: &str, lock: &fs::File, offline: bool, -) -> Result<()> { +) -> Result { let mut c = Command::new(binary); c.current_dir(root) .args(["-p", "--model", &task.model, "--no-session"]); @@ -168,9 +177,18 @@ fn agent( // and fd 0 keeps the shared kernel lock alive if the runner dies before its child. c.args(["--", prompt]) .stdin(lock.try_clone().map_err(|e| e.to_string())?); - checked_output(c.output().map_err(|e| e.to_string())?).map(|_| ()) + let output = c.output().map_err(|e| e.to_string())?; + let notes = notes::parse(&String::from_utf8_lossy(&output.stdout)); + Ok(AgentReply { + notes, + error: checked_output(output).err(), + }) } +struct AgentReply { + notes: Vec, + error: Option, +} pub fn blocked_record(task: &Task, detail: String) -> Record { Record { id: task.id.clone(), @@ -180,6 +198,11 @@ pub fn blocked_record(task: &Task, detail: String) -> Record { detail, step: Step::Finished, progress_at: state::now(), + head: None, + pull_request: None, + draft: false, + problem_notes: vec![], + reports: vec![], } } pub fn run_task_with_policy(repo: &Path, binary: &Path, task: &Task, offline: bool) -> Result { @@ -232,7 +255,10 @@ pub fn run_task_with_options( { return Err("task specification changed; use a new task id".into()); } - if old.record.step != Step::Finished { + if matches!(old.record.step, Step::Publishing | Step::Reporting) && !rerun { + return Ok(old.record); + } + if old.record.step != Step::Finished && !rerun { old } else if !rerun && matches!(old.record.outcome, Outcome::Done | Outcome::NothingToDo) { return Ok(old.record); @@ -242,7 +268,7 @@ pub fn run_task_with_options( } else { new_state(task, tree, offline) }; - state.offline |= offline; + state.offline = offline; let step = state.record.step; state::save(&receipt, &mut state, step)?; if let Err(error) = execute::execute(&root, binary, task, &receipt, &mut state, &lock) { @@ -257,6 +283,9 @@ pub fn run_task_with_options( } state.record.detail.push_str(&format!("save: {error}")); } + if state.record.worktree.exists() && state.record.outcome != Outcome::Blocked { + state.record.head = Some(checked(git(&state.record.worktree, &["rev-parse", "HEAD"]))?); + } state::save(&receipt, &mut state, Step::Finished)?; Ok(state.record) } @@ -270,10 +299,16 @@ fn new_state(task: &Task, worktree: PathBuf, offline: bool) -> state::State { detail: String::new(), step: Step::Preparing, progress_at: state::now(), + head: None, + pull_request: None, + draft: false, + problem_notes: vec![], + reports: vec![], }, task: task.clone(), checkpoint: None, offline, + acceptance_outcome: None, } } pub fn status(repo: &Path) -> Result> { diff --git a/oxa/oxa-work/src/notes.rs b/oxa/oxa-work/src/notes.rs new file mode 100644 index 000000000..7e09e9beb --- /dev/null +++ b/oxa/oxa-work/src/notes.rs @@ -0,0 +1,51 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] +pub struct ProblemNote { + pub title: String, + pub notes: String, + pub keywords: Vec, +} +#[derive(Debug, Deserialize, Serialize)] +pub struct ReportRecord { + pub disposition: String, + pub draft: PathBuf, + pub url: Option, + pub detail: String, +} +pub(crate) const INSTRUCTIONS: &str = "\n\nIf you encountered a problem with oxa, oxc or the engine, follow the report-issue skill's privacy rules. Do not post it yourself. End your final reply with [{\"title\":\"component and symptom\",\"notes\":\"your own brief reproduction, expected and actual behavior\",\"keywords\":[\"component symptom\",\"command error\"]}]. Supply two or three distinct symptom keyword queries. Never include prompts, transcripts, secrets, personal data or file contents. Omit the block when there is no problem to report. The runner only files notes when its user explicitly requests --report-problems."; +pub(crate) fn parse(output: &str) -> Vec { + let Some((_, rest)) = output.rsplit_once("") else { + return vec![]; + }; + let Some((json, _)) = rest.split_once("") else { + return vec![]; + }; + if json.len() > 32_000 { + return vec![]; + } + let Ok(notes) = serde_json::from_str::>(json) else { + return vec![]; + }; + notes + .into_iter() + .take(5) + .filter(|n| { + !n.title.trim().is_empty() + && n.title.chars().count() <= 200 + && !n.notes.trim().is_empty() + && n.notes.chars().count() <= 2000 + && (2..=3).contains(&n.keywords.len()) + && n.keywords + .iter() + .all(|k| !k.trim().is_empty() && k.chars().count() <= 100) + && n.keywords + .iter() + .map(|q| q.trim().to_lowercase()) + .collect::>() + .len() + == n.keywords.len() + }) + .collect() +} diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs new file mode 100644 index 000000000..ee0fe06ba --- /dev/null +++ b/oxa/oxa-work/src/publish.rs @@ -0,0 +1,221 @@ +//! Explicit publishing of the accepted commit; existing queued PRs are never pushed. +use super::{checked, git, state, tree, Outcome, Record, Result, Task}; +use state::Step; +use std::{ + fs::File, + path::{Path, PathBuf}, + process::Command, +}; + +fn context(repo: &Path, task: &Task) -> Result<(PathBuf, File, state::State)> { + let common = PathBuf::from(checked(git( + repo, + &["rev-parse", "--path-format=absolute", "--git-common-dir"], + ))?); + let path = common.join("oxa-work").join(format!("{}.json", task.id)); + let lock = state::lock(&path.with_extension("lock"))?; + let value = state::read(&path)?; + if value.task != *task { + return Err("task specification changed".into()); + } + Ok((path, lock, value)) +} +fn gh(repo: &Path, lock: &File, args: &[&str]) -> Result { + let mut command = Command::new("gh"); + command.current_dir(repo).args(args); + command.stdin(lock.try_clone().map_err(|e| e.to_string())?); + super::checked_output(command.output().map_err(|e| e.to_string())?) +} +fn json(text: &str) -> Result { + serde_json::from_str(text).map_err(|e| e.to_string()) +} +fn pr_base(repo: &Path, task: &Task) -> Result { + let reference = checked(git(repo, &["rev-parse", "--symbolic-full-name", &task.base]))?; + reference + .strip_prefix("refs/heads/") + .or_else(|| reference.strip_prefix("refs/remotes/origin/")) + .filter(|s| !s.is_empty()) + .map(str::to_string) + .ok_or_else(|| "publishing requires a named local or origin base branch".into()) +} +pub fn publish(repo: &Path, task: &Task, offline: bool) -> Result { + let (path, lock, mut value) = context(repo, task)?; + let accepted = value.acceptance_outcome.unwrap_or(value.record.outcome); + if !matches!(accepted, Outcome::Done | Outcome::Red) { + return Ok(value.record); + } + value.acceptance_outcome = Some(accepted); + value.offline = offline; + state::save(&path, &mut value, Step::Publishing)?; + let result = (|| -> Result<(String, bool)> { + if offline { + return Err("publishing is unavailable in offline mode".into()); + } + let tree = &value.record.worktree; + tree::validate(repo, tree, task)?; + if !checked(git(tree, &["status", "--porcelain"]))?.is_empty() { + return Err("uncommitted work must be saved before publishing".into()); + } + let head = checked(git(tree, &["rev-parse", "HEAD"]))?; + if value.record.head.as_deref() != Some(head.as_str()) { + return Err("task branch changed after acceptance; use --rerun before publishing".into()); + } + let base = pr_base(tree, task)?; + let repository = json(&gh(tree, &lock, &["repo", "view", "--json", "nameWithOwner"])?)?; + let name = repository["nameWithOwner"] + .as_str() + .ok_or("repository name missing")?; + let (owner, repo_name) = name.split_once('/').ok_or("invalid repository name")?; + let prs = json(&gh( + tree, + &lock, + &[ + "pr", + "list", + "--repo", + name, + "--state", + "open", + "--head", + &task.branch, + "--json", + "number,url,isDraft", + ], + )?)?; + let prs = prs.as_array().ok_or("invalid PR list")?; + if prs.len() > 1 { + return Err("multiple open PRs match the task branch".into()); + } + if let Some(pr) = prs.first() { + let number = pr["number"].as_u64().ok_or("PR number missing")?.to_string(); + let query = "query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){mergeQueueEntry{id}}}}"; + let data = json(&gh( + tree, + &lock, + &[ + "api", + "graphql", + "-f", + &format!("query={query}"), + "-F", + &format!("owner={owner}"), + "-F", + &format!("name={repo_name}"), + "-F", + &format!("number={number}"), + ], + )?)?; + let pull = &data["data"]["repository"]["pullRequest"]; + if !pull.is_object() || pull.get("mergeQueueEntry").is_none() { + return Err("cannot determine whether PR is in the merge queue".into()); + } + if !pull["mergeQueueEntry"].is_null() { + return Err("PR is in the merge queue; use a new task branch for follow-up work".into()); + } + } + let mut push = git( + repo, + &["push", "origin", &format!("{head}:refs/heads/{}", task.branch)], + ); + push.env("GIT_TERMINAL_PROMPT", "0") + .stdin(lock.try_clone().map_err(|e| e.to_string())?); + super::checked_output(push.output().map_err(|e| e.to_string())?)?; + let draft = accepted == Outcome::Red; + let body = path.with_extension("pr-body.md"); + let text = format!("Task `{}` executed by `oxa work`.\n\nAcceptance result: `{}`.\n\nThe runner retained the task worktree and saved its changes.\n", task.id, if draft { "red" } else { "done" }); + std::fs::write(&body, text).map_err(|e| e.to_string())?; + let body_arg = body.to_str().ok_or("non-UTF8 PR body path")?; + let result = if let Some(pr) = prs.first() { + let number = pr["number"].as_u64().ok_or("PR number missing")?.to_string(); + gh( + tree, + &lock, + &["pr", "edit", &number, "--repo", name, "--body-file", body_arg], + )?; + let was_draft = pr["isDraft"].as_bool().ok_or("PR draft state missing")?; + if was_draft != draft { + let mut args = vec!["pr", "ready", &number, "--repo", name]; + if draft { + args.push("--undo"); + } + gh(tree, &lock, &args)?; + } + pr["url"].as_str().ok_or("PR URL missing")?.to_string() + } else { + let title = format!("oxa work: {}", task.id); + let mut args = vec![ + "pr", + "create", + "--repo", + name, + "--head", + &task.branch, + "--base", + &base, + "--title", + &title, + "--body-file", + body_arg, + ]; + if draft { + args.push("--draft"); + } + gh(tree, &lock, &args)? + }; + let _ = std::fs::remove_file(body); + Ok((result.trim().to_string(), draft)) + })(); + match result { + Ok((url, draft)) => { + value.record.pull_request = Some(url); + value.record.draft = draft; + value.record.outcome = accepted; + value.record.detail.clear(); + state::save(&path, &mut value, Step::Finished)?; + } + Err(error) => { + value.record.outcome = Outcome::Blocked; + value.record.detail = format!("publishing: {error}"); + state::save(&path, &mut value, Step::Publishing)?; + } + } + Ok(value.record) +} +pub fn report_notes( + repo: &Path, + task: &Task, + offline: bool, + reporter: &mut dyn FnMut(&super::ProblemNote, &File, bool) -> Result, +) -> Result { + let (path, lock, mut value) = context(repo, task)?; + value.offline = offline; + let prior_step = value.record.step; + for index in 0..value.record.problem_notes.len() { + if value + .record + .reports + .get(index) + .is_some_and(|r| r.disposition != "local") + { + continue; + } + state::save(&path, &mut value, Step::Reporting)?; + let report = reporter(&value.record.problem_notes[index], &lock, value.offline)?; + if index < value.record.reports.len() { + value.record.reports[index] = report; + } else { + value.record.reports.push(report); + } + state::save(&path, &mut value, Step::Reporting)?; + } + state::save( + &path, + &mut value, + if prior_step == Step::Publishing { + Step::Publishing + } else { + Step::Finished + }, + )?; + Ok(value.record) +} diff --git a/oxa/oxa-work/src/state.rs b/oxa/oxa-work/src/state.rs index ff0b433e9..81a86825d 100644 --- a/oxa/oxa-work/src/state.rs +++ b/oxa/oxa-work/src/state.rs @@ -17,6 +17,8 @@ pub enum Step { Recheck, Saving, Finished, + Publishing, + Reporting, } #[derive(Debug, Deserialize, Serialize)] @@ -35,6 +37,7 @@ pub(crate) struct State { pub task: Task, pub checkpoint: Option, pub offline: bool, + pub acceptance_outcome: Option, } pub(crate) fn now() -> u64 { SystemTime::now() From 8bf1f38bbcd28502a7327ed4deadd18698a6ff3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:22:36 +0000 Subject: [PATCH 10/26] Preserve execution privacy and finish failed reporting checkpoints --- oxa/docs/work.md | 7 +- oxa/oxa-cli/src/work_cmd.rs | 5 +- oxa/oxa-cli/tests/work_publish.rs | 115 ++++++++++++++++++++++++++++++ oxa/oxa-work/src/lib.rs | 14 +++- oxa/oxa-work/src/publish.rs | 13 ++-- oxa/oxa-work/src/state.rs | 1 + 6 files changed, 144 insertions(+), 11 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 14a62278c..90786b4ab 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -85,6 +85,8 @@ The runner checks existing PR queue membership before every push and refuses a queued PR; use a new task branch for follow-up work. It refuses branch changes made after acceptance until `--rerun` checks them. PR bodies include task ID and acceptance state, without raw prompts, transcripts or failed-check output. +Execution preserves a saved offline restriction across restarts; later explicit +publication uses the current network policy without changing that restriction. A publication failure keeps a publication checkpoint; another `--publish` retries without replaying the agent. Offline mode refuses network publication. @@ -99,5 +101,6 @@ It comments only new notes on a matching issue, links an existing fixing PR, or files a new report with related links. Search failure, offline mode or missing report configuration keeps a local scrubbed draft and records its path/detail. A later explicit `--report-problems` retries local reports without replaying the -agent. No notes are filed unless this flag is present; model output and acceptance -logs are never treated as report bodies. +agent. A draft-write failure records a failed report and finishes the task checkpoint; +`--report-problems` can retry it. Reports contain only scrubbed structured symptom +summaries, without raw assistant output or acceptance logs. diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 0ac619a6b..8f6306b9b 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -89,10 +89,7 @@ pub(crate) fn run( }) .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); } - if matches!( - record.outcome, - oxa_work::Outcome::Red | oxa_work::Outcome::Blocked - ) { + if record.failed() { code = 1; } serde_json::to_writer(&mut *out, &record).map_err(|e| e.to_string())?; diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index ff0c2ae78..c1ae16f20 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -254,3 +254,118 @@ fn a_local_report_retries_without_replaying_the_completed_task() { assert_eq!(retry["reports"][0]["disposition"], "filed", "{retry}"); assert_eq!(retry["step"], "finished"); } + +#[test] +fn unfinished_execution_keeps_its_saved_offline_restriction() { + use oxa_fake::{FakeModel, FakeOpts, FakeServer}; + let env = TestEnv::new(); + setup(&env, "[]"); + run_args( + &env, + vec![ScriptTurn::text("done")], + &["--offline", "work", "../tasks.json"], + None, + ); + let state_path = env.work().join("project/.git/oxa-work/test.json"); + let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + state["step"] = "agent".into(); + state["outcome"] = "running".into(); + fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); + let server = FakeServer::start( + vec![ScriptTurn::text("network was allowed")], + FakeOpts { + remote: Some("openrouter"), + models: vec![FakeModel { + alias: "fake-model".into(), + cap: 64000, + max_output: Some(4096), + caps: None, + }], + ..FakeOpts::default() + }, + ) + .unwrap(); + let out = Command::new(env!("CARGO_BIN_EXE_oxa")) + .args(["work", "../tasks.json"]) + .current_dir(env.work().join("project")) + .env_clear() + .envs(env.envs(&server.url())) + .output() + .unwrap(); + let record: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(record["detail"].as_str().unwrap().contains("offline")); + assert!(server.requests().is_empty()); +} +#[test] +fn later_publication_and_reporting_do_not_erase_execution_policy() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + run_args( + &env, + edits(), + &["--offline", "work", "../tasks.json"], + Some(path.clone()), + ); + let state_path = env.work().join("project/.git/oxa-work/test.json"); + for flag in ["--publish", "--report-problems"] { + let record = run_args(&env, vec![], &["work", "../tasks.json", flag], Some(path.clone())); + assert_eq!(record["outcome"], "done", "{record}"); + let state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + assert_eq!(state["offline"], true, "{flag} erased execution policy"); + } +} +#[test] +fn optional_publication_metadata_defaults_to_inactive() { + let env = TestEnv::new(); + setup(&env, "[]"); + run(&env, vec![ScriptTurn::text("done")]); + let state_path = env.work().join("project/.git/oxa-work/test.json"); + let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + for key in [ + "head", + "pull_request", + "draft", + "problem_notes", + "reports", + "acceptance_outcome", + ] { + state.as_object_mut().unwrap().remove(key); + } + fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); + let record = run(&env, vec![]); + assert_eq!(record["outcome"], "nothing_to_do", "{record}"); + assert_eq!(record["draft"], false); + assert_eq!(record["reports"], serde_json::json!([])); +} +#[test] +fn failed_draft_creation_does_not_strand_reporting_checkpoint() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + env.write( + "home/.oxa/agent/issue-drafts", + "a file blocks the draft directory", + ); + let text="[{\"title\":\"oxa work symptom\",\"notes\":\"Synthetic reproduction\",\"keywords\":[\"oxa work\",\"synthetic symptom\"]}]"; + let first = run_args( + &env, + vec![ScriptTurn::text(text)], + &["work", "../tasks.json", "--report-problems"], + Some(path.clone()), + ); + assert_eq!(first["step"], "finished", "{first}"); + assert_eq!(first["reports"][0]["disposition"], "failed"); + let plain = run_args(&env, vec![], &["work", "../tasks.json"], Some(path.clone())); + assert_eq!(plain["step"], "finished"); + fs::remove_file(env.home().join(".oxa/agent/issue-drafts")).unwrap(); + let retry = run_args( + &env, + vec![], + &["work", "../tasks.json", "--report-problems"], + Some(path), + ); + assert_eq!(retry["reports"][0]["disposition"], "local"); + assert_eq!(retry["step"], "finished"); +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index b29965cac..794d485f3 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -47,13 +47,25 @@ pub struct Record { pub detail: String, pub step: Step, pub progress_at: u64, + #[serde(default)] pub head: Option, + #[serde(default)] pub pull_request: Option, + #[serde(default)] pub draft: bool, + #[serde(default)] pub problem_notes: Vec, + #[serde(default)] pub reports: Vec, } +impl Record { + pub fn failed(&self) -> bool { + matches!(self.outcome, Outcome::Red | Outcome::Blocked) + || self.reports.iter().any(|r| r.disposition == "failed") + } +} + fn output(mut command: Command) -> Result { command.stdin(Stdio::null()).output().map_err(|e| e.to_string()) } @@ -268,7 +280,7 @@ pub fn run_task_with_options( } else { new_state(task, tree, offline) }; - state.offline = offline; + state.offline |= offline; let step = state.record.step; state::save(&receipt, &mut state, step)?; if let Err(error) = execute::execute(&root, binary, task, &receipt, &mut state, &lock) { diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index ee0fe06ba..1e8eecf6f 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -45,7 +45,6 @@ pub fn publish(repo: &Path, task: &Task, offline: bool) -> Result { return Ok(value.record); } value.acceptance_outcome = Some(accepted); - value.offline = offline; state::save(&path, &mut value, Step::Publishing)?; let result = (|| -> Result<(String, bool)> { if offline { @@ -188,19 +187,25 @@ pub fn report_notes( reporter: &mut dyn FnMut(&super::ProblemNote, &File, bool) -> Result, ) -> Result { let (path, lock, mut value) = context(repo, task)?; - value.offline = offline; let prior_step = value.record.step; for index in 0..value.record.problem_notes.len() { if value .record .reports .get(index) - .is_some_and(|r| r.disposition != "local") + .is_some_and(|r| !matches!(r.disposition.as_str(), "local" | "failed")) { continue; } state::save(&path, &mut value, Step::Reporting)?; - let report = reporter(&value.record.problem_notes[index], &lock, value.offline)?; + let report = reporter(&value.record.problem_notes[index], &lock, offline).unwrap_or_else(|detail| { + super::ReportRecord { + disposition: "failed".into(), + draft: PathBuf::new(), + url: None, + detail, + } + }); if index < value.record.reports.len() { value.record.reports[index] = report; } else { diff --git a/oxa/oxa-work/src/state.rs b/oxa/oxa-work/src/state.rs index 81a86825d..fae423fc1 100644 --- a/oxa/oxa-work/src/state.rs +++ b/oxa/oxa-work/src/state.rs @@ -37,6 +37,7 @@ pub(crate) struct State { pub task: Task, pub checkpoint: Option, pub offline: bool, + #[serde(default)] pub acceptance_outcome: Option, } pub(crate) fn now() -> u64 { From d7ab5cfa31ae22b3ef4da2bc606e5cd2c151c74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nordstr=C3=B6m?= <13300531+n0rpan@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:54:33 +0000 Subject: [PATCH 11/26] Keep prompt parsing stable and retain agent failure causes (#333) --- oxa/oxa-cli/src/args.rs | 9 +---- oxa/oxa-cli/tests/c12_argument_parsing.rs | 47 +++++++++++++++++++++++ oxa/oxa-cli/tests/work_cli.rs | 15 ++++++++ oxa/oxa-work/src/lib.rs | 3 ++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/oxa/oxa-cli/src/args.rs b/oxa/oxa-cli/src/args.rs index b4a6942ed..0ca62ae99 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -463,14 +463,7 @@ pub fn parse_args(argv: &[String], ext_flags: &[FlagSpec]) -> Result Date: Sun, 15 Nov 2026 18:03:19 +0000 Subject: [PATCH 12/26] Require terminal approval for public work actions and retain interrupted checkpoints (#333) --- oxa/docs/work.md | 10 ++- oxa/oxa-cli/src/report_cmd.rs | 5 +- oxa/oxa-cli/src/work_cmd.rs | 10 ++- oxa/oxa-cli/src/work_reports.rs | 31 +++++++++- oxa/oxa-cli/tests/common/work.rs | 4 +- oxa/oxa-cli/tests/common/work_confirm.rs | 66 ++++++++++++++++++++ oxa/oxa-cli/tests/work_publish.rs | 77 +++++++++++++++++++++++- oxa/oxa-work/src/lib.rs | 5 +- oxa/oxa-work/src/publish.rs | 13 +++- 9 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 oxa/oxa-cli/tests/common/work_confirm.rs diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 90786b4ab..797a3d991 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -49,7 +49,8 @@ the agent; they are never staged as a fabricated resolution. There is no automat cleanup. Publication is explicitly opt-in as described below. To continue an interrupted, red or blocked task, run the same file again. Use -`--rerun` to continue a completed task after its base changes. The retained receipt +`--rerun` to continue a completed task after its base changes. Interrupted execution +always resumes its captured base, checks and progress, even with `--rerun`. The retained receipt must match its ID, branch and worktree. A clean worktree is reused; a branch checked out elsewhere is blocked. Existing dirty work is saved when possible before a later retry. Merge conflicts are passed to the agent with the merge still in progress, the @@ -89,11 +90,14 @@ Execution preserves a saved offline restriction across restarts; later explicit publication uses the current network policy without changing that restriction. A publication failure keeps a publication checkpoint; another `--publish` retries without replaying the agent. Offline mode refuses network publication. +Before each push, the runner asks through the controlling terminal for approval +of the accepted commit, branch and PR destination. Piped input cannot approve it. `--report-problems` independently authorizes filing the task's own structured problem notes using the `report-issue` workflow. This flag must be launched by the -user outside an agent tool. Normal `oxa report-issue submit` still requires its -terminal confirmation. Reports use the user setting `oxa.reportIssue.repo`; +user outside an agent tool. Each new report or comment shows its exact cleaned +text and requires controlling-terminal confirmation, as `oxa report-issue submit` +does. Without confirmation it stays a local draft. Reports use the user setting `oxa.reportIssue.repo`; project settings cannot choose a destination. Notes, titles and queries pass through the existing report scrubber. The runner performs two or three symptom queries against open issues and PRs and reads the top matches before writing. diff --git a/oxa/oxa-cli/src/report_cmd.rs b/oxa/oxa-cli/src/report_cmd.rs index 77ca53fee..3c48281f9 100644 --- a/oxa/oxa-cli/src/report_cmd.rs +++ b/oxa/oxa-cli/src/report_cmd.rs @@ -404,11 +404,14 @@ pub fn submit( /// The real confirmation: the question and the answer go through `/dev/tty` (never stdin, so /// piped input cannot confirm). `None` without a controlling terminal, or when oxa's own tools /// run this (`OXA_CODING_AGENT` is set: the model must not confirm for the user). -fn tty_confirm(env: &Env) -> impl FnMut(&str) -> Option + '_ { +pub(crate) fn tty_confirm(env: &Env) -> impl FnMut(&str) -> Option + '_ { move |question: &str| { if env.get("OXA_CODING_AGENT").is_some() { return None; } + // Keep concurrent workers' questions and answers together. + static TERMINAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let _terminal = TERMINAL.lock().ok()?; let mut tty = std::fs::OpenOptions::new() .read(true) .write(true) diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 8f6306b9b..48d9706bf 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -67,8 +67,13 @@ pub(crate) fn run( oxa_work::run_task_with_options(cwd, &binary, &task, rerun, oxa_proto::offline()) .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); if publish && !record.worktree.as_os_str().is_empty() { - record = oxa_work::publish(cwd, &task, oxa_proto::offline()) - .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); + record = oxa_work::publish( + cwd, + &task, + oxa_proto::offline(), + &mut crate::report_cmd::tty_confirm(env), + ) + .unwrap_or_else(|detail| oxa_work::blocked_record(&task, detail)); } if reports && !record.worktree.as_os_str().is_empty() { let settings = oxa_config::Settings::load(dirs, cwd, false); @@ -84,6 +89,7 @@ pub(crate) fn run( &settings, &ctx, offline || oxa_proto::offline(), + &mut crate::report_cmd::tty_confirm(env), &mut runner, ) }) diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index d5cd7e763..d7fca26b2 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -1,6 +1,6 @@ //! The report-issue workflow for an explicit user-launched --report-problems run. //! Normal `report-issue submit` retains its controlling-terminal confirmation. -use crate::report_cmd::{clean_notes, report_repo, save_draft, scrub, Runner, ScrubCtx}; +use crate::report_cmd::{clean_notes, report_repo, save_draft, scrub, Confirm, Runner, ScrubCtx}; use oxa_config::{Dirs, Settings}; use oxa_work::{ProblemNote, ReportRecord}; use serde_json::Value; @@ -81,6 +81,7 @@ pub(crate) fn report( settings: &Settings, ctx: &ScrubCtx, offline: bool, + confirm: Confirm<'_>, runner: Runner<'_>, ) -> Result { let (draft, title, body) = @@ -192,6 +193,13 @@ pub(crate) fn report( .any(|c| c["body"].as_str().unwrap_or_default().contains(¬e.notes)) }); if !already { + if confirm(&format!( + "Comment on {repo}#{number}:\n\n{}\n\nType yes to post these notes: ", + note.notes + )) != Some(true) + { + return Err("problem report requires confirmation through the controlling terminal; draft retained locally".into()); + } oxa_resources::write_atomic(&body_path, note.notes.as_bytes()).map_err(|e| e.to_string())?; call( runner, @@ -220,6 +228,13 @@ pub(crate) fn report( format!("{body}\n\nRelated open reports (different symptoms):\n{links}\n") }; let body = scrub(&body, ctx); + if confirm(&format!( + "Create issue in {repo}:\nTitle: {}\n\n{body}\n\nType yes to post this report: ", + note.title + )) != Some(true) + { + return Err("problem report requires confirmation through the controlling terminal; draft retained locally".into()); + } oxa_resources::write_atomic(&body_path, body.as_bytes()).map_err(|e| e.to_string())?; let url = call( runner, @@ -287,6 +302,7 @@ mod tests { &settings(), &ScrubCtx::default(), false, + &mut |_| Some(true), &mut runner, ) .unwrap(); @@ -314,6 +330,7 @@ mod tests { &settings(), &ScrubCtx::default(), offline, + &mut |_| Some(true), &mut runner, ) .unwrap(); @@ -351,6 +368,7 @@ mod tests { &settings(), &ScrubCtx::default(), false, + &mut |_| Some(true), &mut runner, ) .unwrap(); @@ -374,7 +392,16 @@ mod tests { _ => panic!("unexpected command"), } }; - let result = report(&n, &dirs, &settings(), &ScrubCtx::default(), false, &mut runner).unwrap(); + let result = report( + &n, + &dirs, + &settings(), + &ScrubCtx::default(), + false, + &mut |_| Some(true), + &mut runner, + ) + .unwrap(); assert_eq!(result.disposition, "filed"); } } diff --git a/oxa/oxa-cli/tests/common/work.rs b/oxa/oxa-cli/tests/common/work.rs index 44062a99f..ebc82f69d 100644 --- a/oxa/oxa-cli/tests/common/work.rs +++ b/oxa/oxa-cli/tests/common/work.rs @@ -1,4 +1,6 @@ use oxa_fake::{FakeOpts, FakeServer, ScriptTurn, TestEnv}; +#[path = "work_confirm.rs"] +mod confirm; use std::{path::Path, process::Command}; pub fn git(env: &TestEnv, dir: &Path, args: &[&str]) -> String { let out = Command::new("git") @@ -63,7 +65,7 @@ pub fn run_observed( } let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { - tx.send(command.output().unwrap()).unwrap(); + tx.send(confirm::output(command)).unwrap(); }); let out = rx .recv_timeout(std::time::Duration::from_secs(90)) diff --git a/oxa/oxa-cli/tests/common/work_confirm.rs b/oxa/oxa-cli/tests/common/work_confirm.rs new file mode 100644 index 000000000..6e2b78fa2 --- /dev/null +++ b/oxa/oxa-cli/tests/common/work_confirm.rs @@ -0,0 +1,66 @@ +//! Real controlling-terminal approvals for publication integration tests. +use std::{ + io::Write, + process::{Command, Output, Stdio}, + sync::atomic::{AtomicU64, Ordering}, +}; + +pub fn output(mut command: Command) -> Output { + if !command + .get_args() + .any(|a| a == "--publish" || a == "--report-problems") + { + return command.output().unwrap(); + } + static NEXT: AtomicU64 = AtomicU64::new(0); + let tmp = command + .get_envs() + .find_map(|(k, v)| (k == "TMPDIR").then_some(v).flatten()) + .unwrap(); + let prefix = std::path::Path::new(tmp).join(format!( + "approval-{}-{}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); + let stdout = prefix.with_extension("out"); + let stderr = prefix.with_extension("err"); + let quote = |s: &std::ffi::OsStr| format!("'{}'", s.to_string_lossy().replace('\'', "'\\''")); + let words = std::iter::once(command.get_program()) + .chain(command.get_args()) + .map(quote) + .collect::>() + .join(" "); + let shell = format!( + "{words} >{} 2>{}", + quote(stdout.as_os_str()), + quote(stderr.as_os_str()) + ); + let mut terminal = Command::new("script"); + terminal.args(["-qefc", &shell, "/dev/null"]).env_clear(); + for (key, value) in command.get_envs() { + if let Some(value) = value { + terminal.env(key, value); + } + } + if let Some(cwd) = command.get_current_dir() { + terminal.current_dir(cwd); + } + let mut child = terminal + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .expect("script provides a real controlling terminal"); + child + .stdin + .take() + .unwrap() + .write_all("yes\n".repeat(32).as_bytes()) + .unwrap(); + let mut out = child.wait_with_output().unwrap(); + out.stdout = std::fs::read(&stdout).unwrap_or_default(); + out.stderr.extend(std::fs::read(&stderr).unwrap_or_default()); + let _ = std::fs::remove_file(stdout); + let _ = std::fs::remove_file(stderr); + out +} diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index c1ae16f20..cd38c3091 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -3,7 +3,7 @@ mod work; use oxa_fake::{ScriptTurn, TestEnv}; use std::{fs, path::PathBuf, process::Command}; -use work::{git, run, run_args, setup}; +use work::{git, run, run_args, run_observed, setup}; fn configure(env: &TestEnv) -> String { use std::os::unix::fs::PermissionsExt; env.write( @@ -369,3 +369,78 @@ fn failed_draft_creation_does_not_strand_reporting_checkpoint() { assert_eq!(retry["reports"][0]["disposition"], "local"); assert_eq!(retry["step"], "finished"); } + +#[test] +fn unsetting_the_agent_marker_and_piping_yes_cannot_authorize_public_writes() { + use std::io::Write; + use std::process::Stdio; + let mut unauthorized = vec![]; + for flag in ["--publish", "--report-problems"] { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + env.write("tmp/gh", r##"#!/bin/sh +case "$1 $2" in + 'repo view') printf '%s' '{"nameWithOwner":"example/project"}' ;; + 'pr list'|'issue list') printf '[]' ;; + 'pr create'|'issue create'|'issue comment') touch "$HOME/public-write"; printf '%s' 'https://github.com/example/project/issues/1' ;; + *) exit 7 ;; +esac +"##); + let text = "[{\"title\":\"oxa work symptom\",\"notes\":\"Synthetic reproduction\",\"keywords\":[\"oxa work\",\"synthetic symptom\"]}]"; + let mut turns = edits(); + turns.pop(); + turns.push(ScriptTurn::text(text)); + assert_eq!(run(&env, turns)["outcome"], "done"); + // A fresh session has no controlling terminal. Removing the marker and + // supplying yes on stdin must not stand in for a person's confirmation. + let mut child = Command::new("setsid") + .env_clear() + .envs(env.envs("http://127.0.0.1:1")) + .env("PATH", path) + .env_remove("OXA_CODING_AGENT") + .args(["--wait", env!("CARGO_BIN_EXE_oxa"), "work", "../tasks.json", flag]) + .current_dir(env.work().join("project")) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child.stdin.take().unwrap().write_all(b"yes\n").unwrap(); + let out = child.wait_with_output().unwrap(); + assert!(out.status.code().is_some()); + if env.home().join("public-write").exists() || env.home().join("push-calls").exists() { + unauthorized.push(flag); + } + } + assert!( + unauthorized.is_empty(), + "public writes without confirmation: {unauthorized:?}" + ); +} + +#[test] +fn rerun_resumes_interrupted_execution_without_discarding_its_checkpoint() { + for step in ["acceptance", "recheck", "saving"] { + let env = TestEnv::new(); + setup(&env, r#"["printf checked >> checkpoint-checks"]"#); + let first = run(&env, edits()); + let state_path = env.work().join("project/.git/oxa-work/test.json"); + let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + state["step"] = step.into(); + state["outcome"] = if step == "saving" { "done" } else { "running" }.into(); + state["pull_request"] = "https://github.com/example/project/pull/1".into(); + state["problem_notes"] = serde_json::json!([{"title":"Synthetic problem","notes":"Keep these notes","keywords":["synthetic problem","keep notes"]}]); + state["reports"] = serde_json::json!([{"draft":"report.md","disposition":"existing_issue","url":"https://github.com/example/project/issues/1","detail":""}]); + fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); + let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json", "--rerun"], None); + assert_eq!(record["outcome"], "done", "{step}: {record}"); + assert!(requests.is_empty(), "{step}: agent was replayed"); + for key in ["pull_request", "problem_notes", "reports"] { + assert_eq!(record[key], state[key], "{step}: {key}"); + } + let after: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + assert_eq!(after["checkpoint"], state["checkpoint"], "{step}"); + assert_eq!(record["worktree"], first["worktree"]); + } +} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 794d485f3..730439711 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -270,7 +270,10 @@ pub fn run_task_with_options( if matches!(old.record.step, Step::Publishing | Step::Reporting) && !rerun { return Ok(old.record); } - if old.record.step != Step::Finished && !rerun { + if !matches!( + old.record.step, + Step::Finished | Step::Publishing | Step::Reporting + ) { old } else if !rerun && matches!(old.record.outcome, Outcome::Done | Outcome::NothingToDo) { return Ok(old.record); diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index 1e8eecf6f..04fdefec6 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -38,7 +38,12 @@ fn pr_base(repo: &Path, task: &Task) -> Result { .map(str::to_string) .ok_or_else(|| "publishing requires a named local or origin base branch".into()) } -pub fn publish(repo: &Path, task: &Task, offline: bool) -> Result { +pub fn publish( + repo: &Path, + task: &Task, + offline: bool, + confirm: &mut dyn FnMut(&str) -> Option, +) -> Result { let (path, lock, mut value) = context(repo, task)?; let accepted = value.acceptance_outcome.unwrap_or(value.record.outcome); if !matches!(accepted, Outcome::Done | Outcome::Red) { @@ -112,6 +117,12 @@ pub fn publish(repo: &Path, task: &Task, offline: bool) -> Result { return Err("PR is in the merge queue; use a new task branch for follow-up work".into()); } } + if confirm(&format!( + "Push accepted commit {head} as {} to origin and create/update its PR in {name} (base {base})? Type yes to publish: ", + task.branch + )) != Some(true) { + return Err("publication requires confirmation through the controlling terminal".into()); + } let mut push = git( repo, &["push", "origin", &format!("{head}:refs/heads/{}", task.branch)], From a8e2efe27e65e75eb8bc2a3c31754a847bb1a808 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:59:03 +0100 Subject: [PATCH 13/26] fix(work): preserve action and save retries on current checkpoints --- oxa/oxa-cli/src/work_cmd.rs | 7 +- oxa/oxa-cli/src/work_reports.rs | 33 ++++++++++ oxa/oxa-cli/tests/work_publish.rs | 105 +++++++----------------------- oxa/oxa-work/src/lib.rs | 32 ++++++++- 4 files changed, 94 insertions(+), 83 deletions(-) diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index b46a41a23..5747cefcb 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -185,7 +185,12 @@ pub(crate) fn run( .into_iter() .map(|task| { if is_trusted { - oxa_work::plan_task_for_run(&root, task.clone(), &policy, rerun) + let planner = if publish || reports { + oxa_work::plan_task_for_actions + } else { + oxa_work::plan_task_for_run + }; + planner(&root, task.clone(), &policy, rerun) .map_err(|detail| Box::new(oxa_work::blocked_record(&task, detail))) } else { Err(Box::new(oxa_work::blocked_record( diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index 7c5929632..6fff5ec63 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -298,6 +298,39 @@ mod tests { ) } #[test] + fn declining_visible_queries_prevents_any_github_request() { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut value = note(); + value.keywords[0] = "component\u{1b}[2Jsymptom\u{200f}".into(); + let mut calls = 0; + let mut approved = false; + let result = report( + &value, + &dirs, + &settings(), + &ScrubCtx::default(), + false, + &mut |prompt| { + assert!(!prompt.contains('\u{1b}') && !prompt.contains('\u{200f}')); + assert!(prompt.contains("\\u{1b}") && prompt.contains("\\u{200f}")); + approved = true; + Some(false) + }, + &mut |program, _| { + if program == "gh" { + calls += 1; + } + Some((0, "[]".into())) + }, + ) + .unwrap(); + assert!(approved); + assert_eq!(calls, 0); + assert_eq!(result.disposition, "local"); + assert!(result.draft.exists()); + } + #[test] fn matching_issue_is_commented_after_all_queries_and_reads() { let env = TestEnv::new(); let dirs = Dirs::under(env.root()); diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index cd38c3091..b7ef61657 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -2,8 +2,17 @@ #[path = "common/work.rs"] mod work; use oxa_fake::{ScriptTurn, TestEnv}; +use sha2::{Digest, Sha256}; use std::{fs, path::PathBuf, process::Command}; use work::{git, run, run_args, run_observed, setup}; +fn state_path(env: &TestEnv) -> PathBuf { + let common = env.work().join("project/.git").canonicalize().unwrap(); + let key: String = Sha256::digest(common.as_os_str().as_encoded_bytes()) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect(); + env.agent_dir().join("work").join(key).join("test.json") +} fn configure(env: &TestEnv) -> String { use std::os::unix::fs::PermissionsExt; env.write( @@ -24,17 +33,17 @@ case "$1 $2" in if test "$arg" = '--draft'; then draft=true; fi if test "$arg" = '--body-file'; then body_next=true; fi done - printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s}' "$draft" > "$HOME/pr-state.json" + printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$draft" > "$HOME/pr-state.json" printf '%s' 'https://github.com/example/project/pull/1' ;; 'pr edit') : ;; 'pr ready') draft=false for arg in "$@"; do if test "$arg" = '--undo'; then draft=true; fi; done - printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s}' "$draft" > "$HOME/pr-state.json" ;; + printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$draft" > "$HOME/pr-state.json" ;; *) exit 7 ;; esac "##); - let git_wrapper = env.write("tmp/git", "#!/bin/sh\nif test \"$1\" = push; then printf 'push\\n' >> \"$HOME/push-calls\"; fi\nexec /usr/bin/git \"$@\"\n"); + let git_wrapper = env.write("tmp/git", "#!/bin/sh\nfor arg do if test \"$arg\" = push; then printf 'push\\n' >> \"$HOME/push-calls\"; fi; done\nexec /usr/bin/git \"$@\"\n"); let controller = env.write("tmp/oxc", "#!/bin/sh\nprintf '{}'\n"); for file in [gh, git_wrapper, controller] { fs::set_permissions(file, fs::Permissions::from_mode(0o755)).unwrap(); @@ -83,7 +92,7 @@ fn publishes_local_branch_and_updates_same_pr_on_restart() { assert!(!body.contains("Finish task")); } #[test] -fn red_checks_create_a_draft_pr() { +fn red_checks_never_push_or_create_a_pr() { let env = TestEnv::new(); setup(&env, r#"["printf red > failed-check; exit 1"]"#); let path = configure(&env); @@ -94,8 +103,10 @@ fn red_checks_create_a_draft_pr() { Some(path), ); assert_eq!(record["outcome"], "red", "{record}"); - assert_eq!(record["draft"], true); - assert!(record["pull_request"].is_string()); + assert_eq!(record["draft"], false); + assert!(record["pull_request"].is_null()); + assert!(!env.home().join("gh-calls").exists()); + assert!(!env.home().join("push-calls").exists()); } #[test] fn queued_pr_is_never_pushed_and_publication_retries_without_agent_replay() { @@ -200,7 +211,7 @@ fn a_model_tool_cannot_opt_itself_into_reporting_or_publishing() { } #[test] -fn green_rerun_marks_an_existing_red_draft_ready() { +fn green_rerun_can_publish_after_red_remained_local() { let env = TestEnv::new(); setup( &env, @@ -213,7 +224,8 @@ fn green_rerun_marks_an_existing_red_draft_ready() { &["work", "../tasks.json", "--publish"], Some(path.clone()), ); - assert_eq!(red["draft"], true, "{red}"); + assert_eq!(red["draft"], false, "{red}"); + assert!(red["pull_request"].is_null()); let green = run_args( &env, vec![ @@ -227,7 +239,7 @@ fn green_rerun_marks_an_existing_red_draft_ready() { assert_eq!(green["draft"], false); assert!(fs::read_to_string(env.home().join("gh-calls")) .unwrap() - .contains("pr ready")); + .contains("pr create")); } #[test] fn a_local_report_retries_without_replaying_the_completed_task() { @@ -255,48 +267,6 @@ fn a_local_report_retries_without_replaying_the_completed_task() { assert_eq!(retry["step"], "finished"); } -#[test] -fn unfinished_execution_keeps_its_saved_offline_restriction() { - use oxa_fake::{FakeModel, FakeOpts, FakeServer}; - let env = TestEnv::new(); - setup(&env, "[]"); - run_args( - &env, - vec![ScriptTurn::text("done")], - &["--offline", "work", "../tasks.json"], - None, - ); - let state_path = env.work().join("project/.git/oxa-work/test.json"); - let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); - state["step"] = "agent".into(); - state["outcome"] = "running".into(); - fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); - let server = FakeServer::start( - vec![ScriptTurn::text("network was allowed")], - FakeOpts { - remote: Some("openrouter"), - models: vec![FakeModel { - alias: "fake-model".into(), - cap: 64000, - max_output: Some(4096), - caps: None, - }], - ..FakeOpts::default() - }, - ) - .unwrap(); - let out = Command::new(env!("CARGO_BIN_EXE_oxa")) - .args(["work", "../tasks.json"]) - .current_dir(env.work().join("project")) - .env_clear() - .envs(env.envs(&server.url())) - .output() - .unwrap(); - let record: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); - assert_eq!(record["outcome"], "blocked", "{record}"); - assert!(record["detail"].as_str().unwrap().contains("offline")); - assert!(server.requests().is_empty()); -} #[test] fn later_publication_and_reporting_do_not_erase_execution_policy() { let env = TestEnv::new(); @@ -308,7 +278,7 @@ fn later_publication_and_reporting_do_not_erase_execution_policy() { &["--offline", "work", "../tasks.json"], Some(path.clone()), ); - let state_path = env.work().join("project/.git/oxa-work/test.json"); + let state_path = state_path(&env); for flag in ["--publish", "--report-problems"] { let record = run_args(&env, vec![], &["work", "../tasks.json", flag], Some(path.clone())); assert_eq!(record["outcome"], "done", "{record}"); @@ -321,17 +291,16 @@ fn optional_publication_metadata_defaults_to_inactive() { let env = TestEnv::new(); setup(&env, "[]"); run(&env, vec![ScriptTurn::text("done")]); - let state_path = env.work().join("project/.git/oxa-work/test.json"); + let state_path = state_path(&env); let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); for key in [ - "head", "pull_request", "draft", "problem_notes", "reports", "acceptance_outcome", ] { - state.as_object_mut().unwrap().remove(key); + state["record"].as_object_mut().unwrap().remove(key); } fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); let record = run(&env, vec![]); @@ -418,29 +387,3 @@ esac "public writes without confirmation: {unauthorized:?}" ); } - -#[test] -fn rerun_resumes_interrupted_execution_without_discarding_its_checkpoint() { - for step in ["acceptance", "recheck", "saving"] { - let env = TestEnv::new(); - setup(&env, r#"["printf checked >> checkpoint-checks"]"#); - let first = run(&env, edits()); - let state_path = env.work().join("project/.git/oxa-work/test.json"); - let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); - state["step"] = step.into(); - state["outcome"] = if step == "saving" { "done" } else { "running" }.into(); - state["pull_request"] = "https://github.com/example/project/pull/1".into(); - state["problem_notes"] = serde_json::json!([{"title":"Synthetic problem","notes":"Keep these notes","keywords":["synthetic problem","keep notes"]}]); - state["reports"] = serde_json::json!([{"draft":"report.md","disposition":"existing_issue","url":"https://github.com/example/project/issues/1","detail":""}]); - fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap(); - let (record, requests) = run_observed(&env, vec![], &["work", "../tasks.json", "--rerun"], None); - assert_eq!(record["outcome"], "done", "{step}: {record}"); - assert!(requests.is_empty(), "{step}: agent was replayed"); - for key in ["pull_request", "problem_notes", "reports"] { - assert_eq!(record[key], state[key], "{step}: {key}"); - } - let after: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); - assert_eq!(after["checkpoint"], state["checkpoint"], "{step}"); - assert_eq!(record["worktree"], first["worktree"]); - } -} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index cdf682c72..9e9496294 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -131,6 +131,27 @@ pub fn plan_task(repo: &Path, task: Task, policy: &RunPolicy) -> Result Result { + plan_task_mode(repo, task, policy, rerun, false) +} + +/// Explicit actions reuse completed execution inputs, including red results, +/// without treating a report/publication retry as another model attempt. +pub fn plan_task_for_actions( + repo: &Path, + task: Task, + policy: &RunPolicy, + rerun: bool, +) -> Result { + plan_task_mode(repo, task, policy, rerun, true) +} + +fn plan_task_mode( + repo: &Path, + task: Task, + policy: &RunPolicy, + rerun: bool, + actions: bool, +) -> Result { validate(&task)?; let path = policy.storage.join(format!("{}.json", task.id)); match fs::symlink_metadata(&path) { @@ -146,7 +167,7 @@ pub fn plan_task_for_run(repo: &Path, task: Task, policy: &RunPolicy, rerun: boo return Err("task specification changed; use a new task id".into()); } if saved.record.step != Step::Finished - || (!rerun && matches!(saved.record.outcome, Outcome::Done | Outcome::NothingToDo)) + || (!rerun && (actions || matches!(saved.record.outcome, Outcome::Done | Outcome::NothingToDo))) { let execution = saved.execution.ok_or("missing task execution checkpoint")?; if saved.record.base_sha.as_deref() != Some(&execution.base) { @@ -652,6 +673,10 @@ pub fn run_task_with_observer( state.record.detail = error; } } + state + .record + .acceptance_outcome + .get_or_insert(state.record.outcome); writer .save(&mut state, Step::Saving, observer) .map_err(RunError::Checkpoint)?; @@ -663,7 +688,12 @@ pub fn run_task_with_observer( state.record.detail.push('\n'); } state.record.detail.push_str(&format!("save: {error}")); + writer + .save(&mut state, Step::Saving, observer) + .map_err(RunError::Checkpoint)?; + return Ok(state.record); } + state.record.outcome = state.record.acceptance_outcome.unwrap_or(state.record.outcome); writer .save(&mut state, Step::Finished, observer) .map_err(RunError::Checkpoint)?; From 494cfff64081f2279aab57fc203371f0f6892a79 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:14:35 +0100 Subject: [PATCH 14/26] fix(work): clear completed save outcome before later execution --- oxa/oxa-cli/tests/work_publish.rs | 7 +++++-- oxa/oxa-work/src/lib.rs | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index b7ef61657..d3aa1d52c 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -4,7 +4,7 @@ mod work; use oxa_fake::{ScriptTurn, TestEnv}; use sha2::{Digest, Sha256}; use std::{fs, path::PathBuf, process::Command}; -use work::{git, run, run_args, run_observed, setup}; +use work::{git, run, run_args, setup}; fn state_path(env: &TestEnv) -> PathBuf { let common = env.work().join("project/.git").canonicalize().unwrap(); let key: String = Sha256::digest(common.as_os_str().as_encoded_bytes()) @@ -127,7 +127,10 @@ fn queued_pr_is_never_pushed_and_publication_retries_without_agent_replay() { Some(path.clone()), ); assert_eq!(blocked["outcome"], "blocked", "{blocked}"); - assert!(blocked["detail"].as_str().unwrap().contains("merge queue")); + assert!( + blocked["detail"].as_str().unwrap().contains("merge queue"), + "{blocked}" + ); assert_eq!( fs::read_to_string(env.home().join("push-calls")) .unwrap() diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 9e9496294..2b4b7f4a3 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -688,12 +688,25 @@ pub fn run_task_with_observer( state.record.detail.push('\n'); } state.record.detail.push_str(&format!("save: {error}")); + let retry = if matches!( + state.record.acceptance_outcome, + Some(Outcome::Done | Outcome::NothingToDo | Outcome::Red) + ) { + Step::Saving + } else { + // An unfinished merge/worker still needs another execution attempt. + Step::Finished + }; writer - .save(&mut state, Step::Saving, observer) + .save(&mut state, retry, observer) .map_err(RunError::Checkpoint)?; return Ok(state.record); } - state.record.outcome = state.record.acceptance_outcome.unwrap_or(state.record.outcome); + state.record.outcome = state + .record + .acceptance_outcome + .take() + .unwrap_or(state.record.outcome); writer .save(&mut state, Step::Finished, observer) .map_err(RunError::Checkpoint)?; From 02d68898879bf17733586eef580b177541299a1b Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:26:56 +0100 Subject: [PATCH 15/26] fix(work): authorize only confirmed publication transports --- oxa/oxa-cli/tests/work_publish.rs | 92 +++++++++++++++++++++++++++++++ oxa/oxa-work/src/publish.rs | 5 ++ 2 files changed, 97 insertions(+) diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index d3aa1d52c..f37576a30 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -57,10 +57,101 @@ fn edits() -> Vec { ] } #[test] +fn a_real_save_failure_retries_without_another_worker() { + const CHILD: &str = "OXA_SAVE_RECOVERY_TEST_ROOT"; + if let Some(root) = std::env::var_os(CHILD) { + use std::sync::{ + atomic::{AtomicBool, Ordering}, + Mutex, + }; + let root = PathBuf::from(root); + let project = root.join("work/project"); + let task = oxa_work::read_tasks(&root.join("work/tasks.json")) + .unwrap() + .remove(0); + 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()), + offline: false, + storage: root.join("home/.oxa/agent/work/save-recovery"), + }; + let binary = std::path::Path::new(env!("CARGO_BIN_EXE_oxa")); + let injected = AtomicBool::new(false); + let lock_path = Mutex::new(None); + let observe = |record: &oxa_work::Record| { + if record.step == oxa_work::Step::Saving && !injected.swap(true, Ordering::SeqCst) { + let out = Command::new("/usr/bin/git") + .current_dir(&record.worktree) + .args(["rev-parse", "--path-format=absolute", "--git-path", "index"]) + .output() + .map_err(|error| error.to_string())?; + if !out.status.success() { + return Err("cannot locate synthetic index".into()); + } + let path = PathBuf::from(format!("{}.lock", String::from_utf8_lossy(&out.stdout).trim())); + fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&path) + .map_err(|error| error.to_string())?; + *lock_path.lock().unwrap() = Some(path); + } + Ok(()) + }; + let plan = oxa_work::plan_task(&project, task.clone(), &policy).unwrap(); + let first = + oxa_work::run_task_with_observer(&project, binary, &plan, &policy, false, &observe).unwrap(); + assert_eq!(first.outcome, oxa_work::Outcome::Blocked, "{first:?}"); + assert_eq!(first.step, oxa_work::Step::Saving, "{first:?}"); + assert!(matches!( + first.worker.as_ref().unwrap().outcome, + oxa_work::WorkerOutcome::Done + )); + fs::remove_file(lock_path.into_inner().unwrap().unwrap()).unwrap(); + let plan = oxa_work::plan_task_for_run(&project, task, &policy, false).unwrap(); + let retry = oxa_work::run_task_with_policy(&project, binary, &plan, &policy).unwrap(); + assert_eq!(retry.outcome, oxa_work::Outcome::Done, "{retry:?}"); + assert_eq!(retry.step, oxa_work::Step::Finished); + assert_eq!(retry.session_id, first.session_id); + let saved = Command::new("/usr/bin/git") + .current_dir(&retry.worktree) + .args(["show", "HEAD:answer.txt"]) + .output() + .unwrap(); + assert!(saved.status.success()); + assert_eq!(String::from_utf8(saved.stdout).unwrap(), "published"); + return; + } + let env = TestEnv::new(); + setup(&env, "[]"); + let server = oxa_fake::FakeServer::start(edits(), oxa_fake::FakeOpts::default()).unwrap(); + let out = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "a_real_save_failure_retries_without_another_worker"]) + .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) + ); + assert_eq!(server.requests().len(), 2, "save retry must not call a model"); +} +#[test] fn publishes_local_branch_and_updates_same_pr_on_restart() { + use std::os::unix::fs::PermissionsExt; let env = TestEnv::new(); setup(&env, "[]"); let path = configure(&env); + let hook = env.write( + "work/project/.git/hooks/pre-push", + "#!/bin/sh\ntouch \"$HOME/publish-hook-ran\"\n", + ); + fs::set_permissions(hook, fs::Permissions::from_mode(0o755)).unwrap(); let first = run_args( &env, edits(), @@ -68,6 +159,7 @@ fn publishes_local_branch_and_updates_same_pr_on_restart() { Some(path.clone()), ); assert_eq!(first["outcome"], "done", "{first}"); + assert!(!env.home().join("publish-hook-ran").exists()); assert_eq!(first["draft"], false); assert_eq!(first["pull_request"], "https://github.com/example/project/pull/1"); assert_eq!( diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index 46adc889a..2b6635700 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -186,6 +186,8 @@ pub fn publish( let mut push = git( repo, &[ + "-c", + "core.hooksPath=/dev/null", "-c", "protocol.https.allow=always", "-c", @@ -198,6 +200,9 @@ pub fn publish( ], ); push.env("GIT_TERMINAL_PROMPT", "0") + // Metadata probes deny every transport. Only this explicit, + // confirmed publication may use the normal GitHub/file transports. + .env("GIT_ALLOW_PROTOCOL", "https:ssh:file") .stdin(lock.try_clone().map_err(|e| e.to_string())?); let pushed = push.output().map_err(|e| e.to_string())?; if !pushed.status.success() { From 1b9e38a19a1fd9cfff289018b5a799c1a264f81c Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:03:28 +0100 Subject: [PATCH 16/26] test(work): expose note privacy and publication metadata regressions --- oxa/oxa-cli/tests/work_publish.rs | 90 +++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index f37576a30..f0badf211 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -21,8 +21,10 @@ fn configure(env: &TestEnv) -> String { ); let gh = env.write("tmp/gh", r##"#!/bin/sh printf '%s\n' "$*" >> "$HOME/gh-calls" +host=github.com +if test -f "$HOME/enterprise-host"; then host=code.example.invalid; fi case "$1 $2" in - 'repo view') printf '%s' '{"nameWithOwner":"example/project"}' ;; + 'repo view') printf '{"nameWithOwner":"example/project","url":"https://%s/example/project"}' "$host" ;; 'pr list') if test -f "$HOME/pr-state.json"; then printf '['; cat "$HOME/pr-state.json"; printf ']'; else printf '[]'; fi ;; 'api graphql') if test -f "$HOME/queued"; then printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":{"id":"queue"}}}}}'; else printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":null}}}}'; fi ;; 'pr create') @@ -33,13 +35,19 @@ case "$1 $2" in if test "$arg" = '--draft'; then draft=true; fi if test "$arg" = '--body-file'; then body_next=true; fi done - printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$draft" > "$HOME/pr-state.json" - printf '%s' 'https://github.com/example/project/pull/1' ;; - 'pr edit') : ;; + printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$host" "$draft" > "$HOME/pr-state.json" + printf 'https://%s/example/project/pull/1' "$host" ;; + 'pr view') jq -Rs '{body:.}' "$HOME/published-body" ;; + 'pr edit') + body_next=false + for arg in "$@"; do + if test "$body_next" = true; then cat "$arg" > "$HOME/published-body"; body_next=false; fi + if test "$arg" = '--body-file'; then body_next=true; fi + done ;; 'pr ready') draft=false for arg in "$@"; do if test "$arg" = '--undo'; then draft=true; fi; done - printf '{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$draft" > "$HOME/pr-state.json" ;; + printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$host" "$draft" > "$HOME/pr-state.json" ;; *) exit 7 ;; esac "##); @@ -184,6 +192,68 @@ fn publishes_local_branch_and_updates_same_pr_on_restart() { assert!(!body.contains("Finish task")); } #[test] +fn publication_keeps_the_origin_host_for_prs_and_graphql() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + env.write("home/enterprise-host", "synthetic"); + let first = run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(first["outcome"], "done", "{first}"); + let again = run_args(&env, vec![], &["work", "../tasks.json", "--publish"], Some(path)); + assert_eq!(again["outcome"], "done", "{again}"); + let calls = fs::read_to_string(env.home().join("gh-calls")).unwrap(); + assert!( + calls.contains("pr list --repo code.example.invalid/example/project"), + "{calls}" + ); + assert!( + calls.contains("pr create --repo code.example.invalid/example/project"), + "{calls}" + ); + assert!( + calls.contains("api graphql --hostname code.example.invalid"), + "{calls}" + ); +} + +#[test] +fn rerun_updates_managed_metadata_without_erasing_owner_body() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let first = run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(first["outcome"], "done", "{first}"); + let body_path = env.home().join("published-body"); + let body = + fs::read_to_string(&body_path).unwrap() + "\nOwner what/why and validated controls stay here.\n"; + fs::write(&body_path, body).unwrap(); + let next = run_args( + &env, + vec![ + ScriptTurn::tool("update", "write", r#"{"path":"answer.txt","content":"updated"}"#), + ScriptTurn::text("done"), + ], + &["work", "../tasks.json", "--rerun", "--publish"], + Some(path), + ); + assert_eq!(next["outcome"], "done", "{next}"); + assert_ne!(next["head_sha"], first["head_sha"]); + let body = fs::read_to_string(body_path).unwrap(); + assert!(body.contains("Owner what/why and validated controls stay here.")); + assert!(body.contains(next["head_sha"].as_str().unwrap()), "{body}"); + assert!(!body.contains(first["head_sha"].as_str().unwrap()), "{body}"); +} +#[test] fn red_checks_never_push_or_create_a_pr() { let env = TestEnv::new(); setup(&env, r#"["printf red > failed-check; exit 1"]"#); @@ -281,6 +351,16 @@ fn own_problem_notes_are_captured_and_failed_search_keeps_a_local_draft() { Some(path), ); assert_eq!(record["problem_notes"].as_array().unwrap().len(), 1); + assert!( + !serde_json::to_string(&record).unwrap().contains("token=example"), + "unscrubbed record output" + ); + assert!( + !fs::read_to_string(state_path(&env)) + .unwrap() + .contains("token=example"), + "unscrubbed checkpoint" + ); assert_eq!(record["reports"][0]["disposition"], "local"); let draft = fs::read_to_string(record["reports"][0]["draft"].as_str().unwrap()).unwrap(); assert!(!draft.contains("token=example")); From a7db5ecb4995c32b764e28f2fe27338bd6d12373 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:14:44 +0100 Subject: [PATCH 17/26] fix(work): sanitize notes and retain publication host and owner body --- oxa/oxa-cli/src/work_cmd.rs | 4 +- oxa/oxa-cli/src/work_reports.rs | 22 +++++++ oxa/oxa-cli/tests/work_publish.rs | 2 +- oxa/oxa-work/src/execute.rs | 7 ++- oxa/oxa-work/src/lib.rs | 16 +++++ oxa/oxa-work/src/publish.rs | 97 +++++++++++++++++++++++++++---- 6 files changed, 133 insertions(+), 15 deletions(-) diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 5747cefcb..7aabc060b 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -212,13 +212,15 @@ pub(crate) fn run( let mut record = if let Some(record) = cached { record } else { - match oxa_work::run_task_with_observer( + let ctx = crate::report_cmd::ScrubCtx::from_env(env, dirs); + match oxa_work::run_task_with_notes( &root, &binary, &plan, &policy, rerun, &|_: &oxa_work::Record| Ok(()), + &|note| crate::work_reports::scrub_note(note, &ctx), ) { Ok(record) => record, Err(oxa_work::RunError::Blocked(detail)) => { diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index 6fff5ec63..6259e38d4 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -64,6 +64,28 @@ fn visible(text: &str) -> String { { ch.escape_unicode().to_string() } else { ch.to_string() } }).collect() } +pub(crate) fn scrub_note(note: &ProblemNote, ctx: &ScrubCtx) -> Option { + let note = ProblemNote { + title: visible(&scrub(¬e.title, ctx)).chars().take(200).collect(), + notes: visible(&clean_notes(¬e.notes, ctx)) + .chars() + .take(2000) + .collect(), + keywords: note + .keywords + .iter() + .map(|query| visible(&scrub(query, ctx)).chars().take(100).collect()) + .collect(), + }; + (!note.title.trim().is_empty() + && !note.notes.trim().is_empty() + && (2..=3).contains(¬e.keywords.len()) + && note + .keywords + .iter() + .all(|query: &String| !query.trim().is_empty())) + .then_some(note) +} fn matches(note: &ProblemNote, candidate: &Value) -> bool { let title = candidate["title"].as_str().unwrap_or_default().to_lowercase(); if title == note.title.to_lowercase() { diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index f0badf211..9f4acb85d 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -525,7 +525,7 @@ fn unsetting_the_agent_marker_and_piping_yes_cannot_authorize_public_writes() { let path = configure(&env); env.write("tmp/gh", r##"#!/bin/sh case "$1 $2" in - 'repo view') printf '%s' '{"nameWithOwner":"example/project"}' ;; + 'repo view') printf '%s' '{"nameWithOwner":"example/project","url":"https://github.com/example/project"}' ;; 'pr list'|'issue list') printf '[]' ;; 'pr create'|'issue create'|'issue comment') touch "$HOME/public-write"; printf '%s' 'https://github.com/example/project/issues/1' ;; *) exit 7 ;; diff --git a/oxa/oxa-work/src/execute.rs b/oxa/oxa-work/src/execute.rs index 3921a11da..5e440a38e 100644 --- a/oxa/oxa-work/src/execute.rs +++ b/oxa/oxa-work/src/execute.rs @@ -12,6 +12,7 @@ pub(crate) struct Runner<'a> { pub lock: &'a File, pub writer: &'a mut Writer, pub observer: &'a dyn CheckpointObserver, + pub scrub_note: &'a (dyn Fn(&crate::ProblemNote) -> Option + Sync), } impl Runner<'_> { fn advance(&mut self, state: &mut State, step: Step) -> std::result::Result<(), RunError> { @@ -103,11 +104,11 @@ impl Runner<'_> { state.record.interruption = worker.result.interruption; } state.record.worker = Some(worker.result.clone()); - for note in &worker.notes { + for note in worker.notes.iter().filter_map(self.scrub_note) { if state.record.problem_notes.len() < 5 - && !state.record.problem_notes.contains(note) + && !state.record.problem_notes.contains(¬e) { - state.record.problem_notes.push(note.clone()); + state.record.problem_notes.push(note); } } state.record.session_id = worker.session.clone(); diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 2b4b7f4a3..4946658d3 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -550,6 +550,21 @@ pub fn run_task_with_observer( policy: &RunPolicy, rerun: bool, observer: &dyn CheckpointObserver, +) -> std::result::Result { + run_task_with_notes(repo, binary, planned, policy, rerun, observer, &|_| None) +} + +/// The trusted frontend supplies its privacy scrubber before any worker note +/// enters a checkpoint, observation or emitted record. Other callers discard +/// notes by default rather than accidentally publishing model-supplied text. +pub fn run_task_with_notes( + repo: &Path, + binary: &Path, + planned: &PlannedTask, + policy: &RunPolicy, + rerun: bool, + observer: &dyn CheckpointObserver, + scrub_note: &(dyn Fn(&ProblemNote) -> Option + Sync), ) -> std::result::Result { let task = &planned.task; validate(task)?; @@ -653,6 +668,7 @@ pub fn run_task_with_observer( lock: &lock_file, writer: &mut writer, observer, + scrub_note, }) .drive(&mut state) { diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index 2b6635700..fddec2c25 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -50,6 +50,61 @@ fn gh(binary: &Path, policy: &RunPolicy, repo: &Path, lock: &File, args: &[&str] fn json(text: &str) -> Result { serde_json::from_str(text).map_err(|e| e.to_string()) } +struct Repository { + qualified: String, + host: String, + owner: String, + name: String, +} +fn repository(value: &serde_json::Value) -> Result { + let identity = value["nameWithOwner"] + .as_str() + .ok_or("repository identity missing")?; + let (owner, name) = identity.split_once('/').ok_or("invalid repository identity")?; + let url = value["url"].as_str().ok_or("repository URL missing")?; + let (host, path) = url + .strip_prefix("https://") + .and_then(|url| url.split_once('/')) + .ok_or("repository URL must identify its HTTPS host")?; + if owner.is_empty() + || name.is_empty() + || name.contains('/') + || path.trim_end_matches('/') != identity + || host.is_empty() + || !host + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || b".-:".contains(&byte)) + { + return Err("repository URL disagrees with its identity".into()); + } + Ok(Repository { + qualified: format!("{host}/{identity}"), + host: host.into(), + owner: owner.into(), + name: name.into(), + }) +} +const BODY_BEGIN: &str = ""; +const BODY_END: &str = ""; +fn body(task: &Task, head: &str, existing: Option<&str>) -> Result { + let section = format!("{BODY_BEGIN}\nAccepted commit: `{head}`\nLocal acceptance: passed\nCI: not yet qualified for this commit\n{BODY_END}"); + let default = format!("T2 — task `{}` executed by `oxa work`.\n\nWhat/why: explicitly requested task; review its changes against the base.\n\nChanges: accepted worktree retained.\n\nRemoves: inspect the diff before promotion; no deletion clearance is inferred.\n\nValidation: frozen local acceptance passed; CI qualification is separate.\n", task.id); + let original = existing.unwrap_or(&default); + match ( + original.matches(BODY_BEGIN).count(), + original.matches(BODY_END).count(), + ) { + (0, 0) => Ok(format!("{original}\n\n{section}\n")), + (1, 1) => { + let (before, rest) = original + .split_once(BODY_BEGIN) + .ok_or("invalid managed PR section")?; + let (_, after) = rest.split_once(BODY_END).ok_or("invalid managed PR section")?; + Ok(format!("{before}{section}{after}")) + } + _ => Err("ambiguous managed PR section; preserve the owner description".into()), + } +} fn pr_base(repo: &Path, task: &Task, policy: &RunPolicy) -> Result { let reference = checked(super::git( policy, @@ -102,15 +157,14 @@ pub fn publish( if remote.lines().count() != 1 { return Err("publishing requires one origin push URL".into()); } - let repository = json(&gh( + let repository = repository(&json(&gh( tree, &lock, - &["repo", "view", &remote, "--json", "nameWithOwner"], - )?)?; - let name = repository["nameWithOwner"] - .as_str() - .ok_or("repository name missing")?; - let (owner, repo_name) = name.split_once('/').ok_or("invalid repository name")?; + &["repo", "view", &remote, "--json", "nameWithOwner,url"], + )?)?)?; + let name = repository.qualified.as_str(); + let owner = repository.owner.as_str(); + let repo_name = repository.name.as_str(); let prs = json(&gh( tree, &lock, @@ -143,6 +197,18 @@ pub fn publish( { return Err("nothing to publish".into()); } + let existing_body = if let Some(pr) = prs.first() { + let number = pr["number"].as_u64().ok_or("PR number missing")?.to_string(); + let value = json(&gh( + tree, + &lock, + &["pr", "view", &number, "--repo", name, "--json", "body"], + )?)?; + Some(value["body"].as_str().ok_or("PR body missing")?.to_owned()) + } else { + None + }; + let text = body(task, &head, existing_body.as_deref())?; if confirm(&format!( "Push accepted commit {head} as {} to origin and create/update its PR in {name} (base {base})? Type yes to publish: ", task.branch @@ -165,6 +231,8 @@ pub fn publish( &[ "api", "graphql", + "--hostname", + &repository.host, "-f", &format!("query={query}"), "-F", @@ -210,15 +278,24 @@ pub fn publish( } let draft = false; let body = policy.storage.join(format!("{}.pr-body.md", task.id)); - let text = format!("T2 — task `{}` executed by `oxa work`.\n\nWhat/why: explicitly requested task; review its changes against the base.\n\nChanges: accepted commit `{head}`; worktree retained.\n\nRemoves: inspect the diff before promotion; no deletion clearance is inferred.\n\nValidation: frozen local acceptance passed. CI: not yet qualified.\n", task.id); - std::fs::write(&body, text).map_err(|e| e.to_string())?; + crate::checkpoint::write_atomic(&body, text.as_bytes(), body.exists())?; let body_arg = body.to_str().ok_or("non-UTF8 PR body path")?; let result = if let Some(pr) = prs.first() { let number = pr["number"].as_u64().ok_or("PR number missing")?.to_string(); gh( tree, &lock, - &["pr", "edit", &number, "--repo", name, "--base", &base], + &[ + "pr", + "edit", + &number, + "--repo", + name, + "--base", + &base, + "--body-file", + body_arg, + ], )?; let was_draft = pr["isDraft"].as_bool().ok_or("PR draft state missing")?; if was_draft != draft { From ff82d247a1a5587e252a5f63716a6e06c32a3504 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:38:58 +0100 Subject: [PATCH 18/26] test(work): reporting must retain a pending accepted save --- oxa/oxa-cli/tests/work_publish.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index 9f4acb85d..8096b4588 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -116,6 +116,13 @@ fn a_real_save_failure_retries_without_another_worker() { first.worker.as_ref().unwrap().outcome, oxa_work::WorkerOutcome::Done )); + let reported = + oxa_work::report_notes(&task, &policy, &|_: &oxa_work::Record| Ok(()), &mut |_, _, _| { + panic!("no notes were emitted by this worker") + }) + .unwrap(); + assert_eq!(reported.step, oxa_work::Step::Saving, "{reported:?}"); + assert_eq!(reported.session_id, first.session_id); fs::remove_file(lock_path.into_inner().unwrap().unwrap()).unwrap(); let plan = oxa_work::plan_task_for_run(&project, task, &policy, false).unwrap(); let retry = oxa_work::run_task_with_policy(&project, binary, &plan, &policy).unwrap(); From 2b6a47b349b1b6446ec4d66d180d88e573bfcf30 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:47:43 +0100 Subject: [PATCH 19/26] fix(work): preserve pending save through report checkpoints --- oxa/oxa-work/src/publish.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index fddec2c25..138af3225 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -354,6 +354,12 @@ pub fn report_notes( ) -> Result { let (mut writer, lock, mut value) = context(task, policy, observer)?; let prior_step = value.record.step; + // Reporting is ancillary: a failed save/publication remains recoverable even + // if reporting is interrupted between durable report receipts. + let reporting_step = match prior_step { + Step::Saving | Step::Publishing => prior_step, + _ => Step::Reporting, + }; for index in 0..value.record.problem_notes.len() { if value .record @@ -363,7 +369,7 @@ pub fn report_notes( { continue; } - writer.save(&mut value, Step::Reporting, observer)?; + writer.save(&mut value, reporting_step, observer)?; let report = reporter(&value.record.problem_notes[index], &lock, policy.offline).unwrap_or_else(|detail| { super::ReportRecord { @@ -378,12 +384,12 @@ pub fn report_notes( } else { value.record.reports.push(report); } - writer.save(&mut value, Step::Reporting, observer)?; + writer.save(&mut value, reporting_step, observer)?; } writer.save( &mut value, - if prior_step == Step::Publishing { - Step::Publishing + if matches!(prior_step, Step::Saving | Step::Publishing) { + prior_step } else { Step::Finished }, From 6936ee2efdca287980cfdb8ecdb764a8a03682e5 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:19:39 +0100 Subject: [PATCH 20/26] test(work): reject publication recovery acceptance bypasses --- oxa/oxa-cli/tests/work_publish.rs | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index 8096b4588..ca9eeebb2 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -327,6 +327,70 @@ fn offline_publication_never_starts_gh_or_push() { assert!(!env.home().join("gh-calls").exists()); assert!(!env.home().join("push-calls").exists()); } +#[test] +fn publication_drift_requires_fresh_checks_on_explicit_rerun() { + let env = TestEnv::new(); + setup(&env, r#"["test ! -f later.txt"]"#); + let path = configure(&env); + let done = run(&env, edits()); + let tree = PathBuf::from(done["worktree"].as_str().unwrap()); + fs::write(tree.join("later.txt"), "unchecked\n").unwrap(); + git(&env, &tree, &["add", "."]); + git( + &env, + &tree, + &["commit", "-m", "Synthetic acceptance-breaking drift"], + ); + let blocked = run_args( + &env, + vec![], + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(blocked["step"], "publishing", "{blocked}"); + let (retry, requests) = work::run_observed( + &env, + vec![ScriptTurn::text("done"), ScriptTurn::text("done")], + &["work", "../tasks.json", "--rerun", "--publish"], + Some(path), + ); + assert_eq!(retry["outcome"], "red", "{retry}"); + assert_eq!( + requests.len(), + 2, + "fresh execution plus the existing repair attempt" + ); + assert!(!env.home().join("push-calls").exists()); +} + +#[test] +fn ordinary_run_cannot_reseal_a_failed_publication_checkpoint() { + let env = TestEnv::new(); + setup(&env, "[]"); + let path = configure(&env); + let done = run(&env, edits()); + let tree = PathBuf::from(done["worktree"].as_str().unwrap()); + fs::write(tree.join("later.txt"), "unchecked\n").unwrap(); + git(&env, &tree, &["add", "."]); + git(&env, &tree, &["commit", "-m", "Synthetic publication drift"]); + let blocked = run_args( + &env, + vec![], + &["work", "../tasks.json", "--publish"], + Some(path.clone()), + ); + assert_eq!(blocked["step"], "publishing", "{blocked}"); + let (retry, requests) = work::run_observed(&env, vec![], &["work", "../tasks.json"], Some(path.clone())); + assert_eq!( + retry["head_sha"], done["head_sha"], + "a normal run cannot certify a new head" + ); + assert!(requests.is_empty()); + let retry = run_args(&env, vec![], &["work", "../tasks.json", "--publish"], Some(path)); + assert_eq!(retry["outcome"], "blocked", "{retry}"); + assert!(!env.home().join("push-calls").exists()); +} + #[test] fn branch_drift_after_acceptance_cannot_publish_unchecked_commits() { let env = TestEnv::new(); From 95c55cba9ed3f422ed055d7b364bd625ea2133a6 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:30:33 +0100 Subject: [PATCH 21/26] fix(work): revalidate terminal action checkpoints on explicit rerun --- oxa/oxa-work/src/lib.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index f1a0b1834..476d479f5 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -152,6 +152,10 @@ pub fn plan_task_for_actions( plan_task_mode(repo, task, policy, rerun, true) } +fn completed_execution(step: Step) -> bool { + matches!(step, Step::Finished | Step::Publishing | Step::Reporting) +} + fn plan_task_mode( repo: &Path, task: Task, @@ -173,8 +177,9 @@ fn plan_task_mode( if saved.task != task { return Err("task specification changed; use a new task id".into()); } - if saved.record.step != Step::Finished - || (!rerun && (actions || matches!(saved.record.outcome, Outcome::Done | Outcome::NothingToDo))) + if !(rerun && completed_execution(saved.record.step)) + && (saved.record.step != Step::Finished + || (!rerun && (actions || matches!(saved.record.outcome, Outcome::Done | Outcome::NothingToDo)))) { let execution = saved.execution.ok_or("missing task execution checkpoint")?; if saved.record.base_sha.as_deref() != Some(&execution.base) { @@ -645,7 +650,12 @@ pub fn run_task_with_notes( return Err("task specification changed; use a new task id".into()); } writer.replay(&old, observer).map_err(RunError::Checkpoint)?; - if old.record.step == Step::Finished { + // Action recovery cannot turn a changed HEAD into fresh acceptance. + // Only an explicit rerun starts a new execution for these checkpoints. + if !rerun && matches!(old.record.step, Step::Publishing | Step::Reporting) { + return Ok(old.record); + } + if completed_execution(old.record.step) { if !rerun && matches!(old.record.outcome, Outcome::Done | Outcome::NothingToDo) { return Ok(old.record); } From 08c2c9b2803efb15487dda862056494037158202 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:10:41 +0100 Subject: [PATCH 22/26] test(work): expose stale save and publication verification failures --- oxa/oxa-cli/src/work_reports.rs | 9 +++ oxa/oxa-cli/tests/work_publish.rs | 95 ++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index 6259e38d4..f3c33831c 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -306,6 +306,15 @@ pub(crate) fn report( mod tests { use super::*; use oxa_fake::TestEnv; + + #[test] + fn locked_report_command_refuses_oversized_stdout() { + let env = TestEnv::new(); + let lock = File::open("/dev/null").unwrap(); + let args = words(&["-c", "1048577", "/dev/zero"]); + assert!(process("/usr/bin/head", &args, &env.work(), &lock).is_none()); + } + fn note() -> ProblemNote { ProblemNote { title: "oxa work commit is read only".into(), diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index ca9eeebb2..6a6eac141 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -66,6 +66,36 @@ fn edits() -> Vec { } #[test] fn a_real_save_failure_retries_without_another_worker() { + exercise_save_recovery( + "a_real_save_failure_retries_without_another_worker", + SaveRecovery::Unchanged, + ); +} + +#[test] +fn publication_does_not_replace_a_pending_save_checkpoint() { + exercise_save_recovery( + "publication_does_not_replace_a_pending_save_checkpoint", + SaveRecovery::PendingPublish, + ); +} + +#[test] +fn an_edit_after_acceptance_cannot_reuse_the_pending_save_verdict() { + exercise_save_recovery( + "an_edit_after_acceptance_cannot_reuse_the_pending_save_verdict", + SaveRecovery::Edited, + ); +} + +#[derive(PartialEq, Eq)] +enum SaveRecovery { + Unchanged, + PendingPublish, + Edited, +} + +fn exercise_save_recovery(test_name: &str, case: SaveRecovery) { const CHILD: &str = "OXA_SAVE_RECOVERY_TEST_ROOT"; if let Some(root) = std::env::var_os(CHILD) { use std::sync::{ @@ -123,9 +153,37 @@ fn a_real_save_failure_retries_without_another_worker() { .unwrap(); assert_eq!(reported.step, oxa_work::Step::Saving, "{reported:?}"); assert_eq!(reported.session_id, first.session_id); + if case == SaveRecovery::PendingPublish { + let published = oxa_work::publish( + &project, + binary, + &task, + &policy, + std::path::Path::new("/unavailable/gh"), + &|_: &oxa_work::Record| Ok(()), + &mut |_| panic!("a pending save cannot request publication approval"), + ) + .unwrap(); + assert_eq!(published.step, oxa_work::Step::Saving, "{published:?}"); + assert_eq!(published.checkpoint_revision, reported.checkpoint_revision); + } fs::remove_file(lock_path.into_inner().unwrap().unwrap()).unwrap(); + if case == SaveRecovery::Edited { + fs::write(first.worktree.join("answer.txt"), "edited after acceptance").unwrap(); + } let plan = oxa_work::plan_task_for_run(&project, task, &policy, false).unwrap(); let retry = oxa_work::run_task_with_policy(&project, binary, &plan, &policy).unwrap(); + if case == SaveRecovery::Edited { + assert!( + matches!(retry.outcome, oxa_work::Outcome::Red | oxa_work::Outcome::Blocked), + "{retry:?}" + ); + assert_eq!( + fs::read_to_string(first.worktree.join("answer.txt")).unwrap(), + "edited after acceptance" + ); + return; + } assert_eq!(retry.outcome, oxa_work::Outcome::Done, "{retry:?}"); assert_eq!(retry.step, oxa_work::Step::Finished); assert_eq!(retry.session_id, first.session_id); @@ -139,10 +197,15 @@ fn a_real_save_failure_retries_without_another_worker() { return; } let env = TestEnv::new(); - setup(&env, "[]"); + let checks = if case == SaveRecovery::Edited { + r#"["grep -Fx published answer.txt"]"# + } else { + "[]" + }; + setup(&env, checks); let server = oxa_fake::FakeServer::start(edits(), oxa_fake::FakeOpts::default()).unwrap(); let out = Command::new(std::env::current_exe().unwrap()) - .args(["--exact", "a_real_save_failure_retries_without_another_worker"]) + .args(["--exact", test_name]) .env_clear() .envs(env.envs(&server.url())) .env(CHILD, env.root()) @@ -156,6 +219,34 @@ fn a_real_save_failure_retries_without_another_worker() { ); assert_eq!(server.requests().len(), 2, "save retry must not call a model"); } + +#[test] +fn publishing_requires_acceptance_without_ignored_helpers() { + let env = TestEnv::new(); + setup(&env, r#"["sh helper.sh"]"#); + let repo = env.work().join("project"); + env.write("work/project/.gitignore", "helper.sh\n"); + git(&env, &repo, &["add", ".gitignore"]); + git(&env, &repo, &["commit", "-m", "Ignore synthetic local helper"]); + let mut turns = edits(); + turns.insert( + 0, + ScriptTurn::tool("helper", "write", r#"{"path":"helper.sh","content":"exit 0\n"}"#), + ); + let saved = run_args( + &env, + turns, + &["work", "../tasks.json", "--publish"], + Some(configure(&env)), + ); + assert_eq!(saved["outcome"], "blocked", "{saved}"); + assert!(saved["detail"] + .as_str() + .unwrap() + .contains("clean committed acceptance")); + assert!(!env.home().join("push-calls").exists()); + assert!(!env.home().join("gh-calls").exists()); +} #[test] fn publishes_local_branch_and_updates_same_pr_on_restart() { use std::os::unix::fs::PermissionsExt; From 8ac1cc9754adefef06bc0094c12f6fd55f153222 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:21:36 +0100 Subject: [PATCH 23/26] Verify committed publication and recovered saves --- oxa/oxa-cli/src/report_cmd.rs | 9 +++++-- oxa/oxa-cli/src/work_reports.rs | 43 +++++------------------------ oxa/oxa-work/src/committed.rs | 48 +++++++++++++++++++++------------ oxa/oxa-work/src/lib.rs | 24 +++++++++++++++++ oxa/oxa-work/src/publish.rs | 11 +++++++- 5 files changed, 79 insertions(+), 56 deletions(-) diff --git a/oxa/oxa-cli/src/report_cmd.rs b/oxa/oxa-cli/src/report_cmd.rs index 30ab4339d..a8df98b8e 100644 --- a/oxa/oxa-cli/src/report_cmd.rs +++ b/oxa/oxa-cli/src/report_cmd.rs @@ -456,12 +456,17 @@ system-wide or put it in a trusted PATH entry outside the project (for example ~ ) } -pub(crate) fn command_runner( +pub(crate) fn command_runner(command: std::process::Command, limit: Option) -> Option<(i32, String)> { + command_runner_with_stdin(command, limit, std::process::Stdio::null()) +} + +pub(crate) fn command_runner_with_stdin( mut command: std::process::Command, limit: Option, + stdin: std::process::Stdio, ) -> Option<(i32, String)> { let mut child = command - .stdin(std::process::Stdio::null()) + .stdin(stdin) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::null()) .spawn() diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index f3c33831c..38b50e275 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -6,47 +6,18 @@ use oxa_work::{ProblemNote, ReportRecord}; use serde_json::Value; use std::{ fs::File, - io::Read, path::Path, process::{Command, Stdio}, - time::{Duration, Instant}, }; pub(crate) fn process(program: &str, args: &[String], cwd: &Path, lock: &File) -> Option<(i32, String)> { - let mut child = Command::new(program) - .args(args) - .current_dir(cwd) - .stdin(Stdio::from(lock.try_clone().ok()?)) - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .spawn() - .ok()?; - let mut stdout = child.stdout.take()?; - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let mut out = String::new(); - let result = (&mut stdout).take(1_048_576).read_to_string(&mut out); - let _ = tx.send(result.ok().map(|_| out)); - }); - let start = Instant::now(); - loop { - match child.try_wait() { - Ok(Some(status)) => { - return Some(( - status.code().unwrap_or(1), - rx.recv_timeout(Duration::from_secs(2)).ok()??, - )) - } - Ok(None) if start.elapsed() < Duration::from_secs(30) => { - std::thread::sleep(Duration::from_millis(20)) - } - _ => { - let _ = child.kill(); - let _ = child.wait(); - return None; - } - } - } + let mut command = Command::new(program); + command.args(args).current_dir(cwd); + crate::report_cmd::command_runner_with_stdin( + command, + Some(1_048_576), + Stdio::from(lock.try_clone().ok()?), + ) } fn call(runner: Runner<'_>, args: &[String]) -> Result { match runner("gh", args) { diff --git a/oxa/oxa-work/src/committed.rs b/oxa/oxa-work/src/committed.rs index e9200cc6a..c30c54718 100644 --- a/oxa/oxa-work/src/committed.rs +++ b/oxa/oxa-work/src/committed.rs @@ -57,13 +57,31 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st .map_err(|_| "cannot open saved task lock")?; lock.try_lock().map_err(|_| "task is already running")?; let saved = state::load(&policy.storage.join(format!("{id}.json")))?; - let record = saved.record; + let record = &saved.record; if saved.task.id != id || record.step != Step::Finished || !matches!(record.outcome, Outcome::Done | Outcome::NothingToDo) { return Err("only finished successful work can be verified for publication".into()); } + let export = verify_commit(repo, binary, policy, &saved, &lock)?; + Ok(VerifiedTask { + record: saved.record, + export, + _lock: lock, + checked_at_ms: crate::state::now_ms(), + }) +} + +/// Check the saved commit while the caller holds the task lock. +pub(crate) fn verify_commit( + repo: &Path, + binary: &Path, + policy: &RunPolicy, + saved: &state::State, + lock: &File, +) -> Result { + let record = &saved.record; // An explicit later online publication never relaxes the saved check policy. let verification_policy = RunPolicy { offline: saved.offline || policy.offline, @@ -72,6 +90,7 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st let policy = &verification_policy; let execution = saved .execution + .as_ref() .ok_or("saved acceptance commands are unavailable")?; let head = record.head_sha.as_deref().ok_or("saved head is unavailable")?; let tree_id = record.tree_sha.as_deref().ok_or("saved tree is unavailable")?; @@ -82,7 +101,7 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st tree::validate(policy, repo, &record.worktree, &saved.task)?; tree::ensure_merged(policy, &record.worktree, base, &saved.task.branch)?; same_saved_commit(policy, &record.worktree, head, tree_id)?; - if dirty_worktree(binary, &record.worktree, repo, policy, &lock)? { + if dirty_worktree(binary, &record.worktree, repo, policy, lock)? { return Err("saved task has uncommitted changes".into()); } let export = Export::create(policy, &record.worktree, head, tree_id, base)?; @@ -99,18 +118,18 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st SupervisorRequest { source: repo.into(), operation: SupervisorOperation::Checkout { - branch: saved.task.branch, + branch: saved.task.branch.clone(), create: false, }, }, policy, - &lock, + lock, )?; if !checkout.status.success() { return Err("clean acceptance checkout failed; saved work is retained".into()); } same_saved_commit(policy, &export.tree, head, tree_id)?; - if dirty_worktree(binary, &export.tree, repo, policy, &lock)? { + if dirty_worktree(binary, &export.tree, repo, policy, lock)? { return Err("clean acceptance checkout is dirty".into()); } // This fresh index comes from the verified commit and lives outside both @@ -143,18 +162,18 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st SupervisorRequest { source: repo.into(), operation: SupervisorOperation::CommittedChecks { - commands: execution.commands, + commands: execution.commands.clone(), index: index.clone(), }, }, policy, - &lock, + lock, )?)?; if !failed.is_empty() { return Err(format!("clean committed acceptance failed: {failed}")); } same_saved_commit(policy, &export.tree, head, tree_id)?; - if dirty_worktree(binary, &export.tree, repo, policy, &lock)? { + if dirty_worktree(binary, &export.tree, repo, policy, lock)? { return Err("acceptance changed committed source files".into()); } let inspected = supervise( @@ -165,22 +184,17 @@ pub fn verify_saved_task(repo: &Path, binary: &Path, policy: &RunPolicy, id: &st operation: SupervisorOperation::InspectCommitted { index }, }, policy, - &lock, + lock, )?; if !inspected.status.success() { return Err("acceptance changed committed source files or could not be inspected".into()); } export.verify(policy, head, base)?; same_saved_commit(policy, &record.worktree, head, tree_id)?; - if dirty_worktree(binary, &record.worktree, repo, policy, &lock)? { + if dirty_worktree(binary, &record.worktree, repo, policy, lock)? { return Err("saved work changed during committed acceptance".into()); } - Ok(VerifiedTask { - record, - export, - _lock: lock, - checked_at_ms: crate::state::now_ms(), - }) + Ok(export) } fn same_saved_commit(policy: &RunPolicy, tree: &Path, head: &str, tree_id: &str) -> Result<()> { @@ -365,7 +379,7 @@ impl Export { Ok(export) } - fn verify(&self, policy: &RunPolicy, head: &str, base: &str) -> Result<()> { + pub(crate) fn verify(&self, policy: &RunPolicy, head: &str, base: &str) -> Result<()> { match (fs::read(self.store.join("shallow")), &self.shallow) { (Ok(actual), Some(expected)) if actual == *expected => {} (Err(error), None) if error.kind() == std::io::ErrorKind::NotFound => {} diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 8c7eaf673..506f8e490 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -694,6 +694,11 @@ pub fn run_task_with_notes( }; // Replaying the prior checkpoint precedes this first new checkpoint or action. let step = state.record.step; + let recover_accepted_save = step == Step::Saving + && matches!( + state.record.acceptance_outcome.unwrap_or(state.record.outcome), + Outcome::Done | Outcome::NothingToDo + ); writer .save(&mut state, step, observer) .map_err(RunError::Checkpoint)?; @@ -759,6 +764,25 @@ pub fn run_task_with_notes( .acceptance_outcome .take() .unwrap_or(state.record.outcome); + if recover_accepted_save { + // A failed or interrupted save can leave a worktree that changed since + // acceptance. Qualify the exact recovered commit before restoring success. + match committed::verify_commit(&root, binary, &policy, &state, &lock_file) { + Ok(_) => { + let initial = state.execution.as_ref().and_then(|point| point.initial.as_ref()); + state.record.outcome = if state.record.head_sha.as_ref() == initial { + Outcome::NothingToDo + } else { + Outcome::Done + }; + state.record.detail.clear(); + } + Err(error) => { + state.record.outcome = Outcome::Blocked; + state.record.detail = format!("save recovery: {error}"); + } + } + } writer .save(&mut state, Step::Finished, observer) .map_err(RunError::Checkpoint)?; diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index 138af3225..7afe9c460 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -129,7 +129,9 @@ pub fn publish( ) -> Result { let (mut writer, lock, mut value) = context(task, policy, observer)?; let accepted = value.record.acceptance_outcome.unwrap_or(value.record.outcome); - if !matches!(accepted, Outcome::Done | Outcome::NothingToDo) { + if !super::completed_execution(value.record.step) + || !matches!(accepted, Outcome::Done | Outcome::NothingToDo) + { return Ok(value.record); } value.record.acceptance_outcome = Some(accepted); @@ -149,6 +151,12 @@ pub fn publish( if value.record.head_sha.as_deref() != Some(head.as_str()) { return Err("task branch changed after acceptance; use --rerun before publishing".into()); } + let proof = super::committed::verify_commit(repo, binary, policy, &value, &lock)?; + let accepted_base = value + .record + .base_sha + .as_deref() + .ok_or("saved base is unavailable")?; let base = pr_base(tree, task, policy)?; if task.branch == base { return Err("task branch must differ from its base branch".into()); @@ -220,6 +228,7 @@ pub fn publish( { return Err("publication destination or task head changed during confirmation".into()); } + proof.verify(policy, &head, accepted_base)?; // Confirmation may remain open indefinitely: queue admission is checked // only after it returns, immediately before the push. if let Some(pr) = prs.first() { From 72025f394a764e7318c1902620f220dcf3809afc Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:58:23 +0100 Subject: [PATCH 24/26] test(work): cover publication subprocess and destination guards --- oxa/oxa-cli/tests/common/work.rs | 10 +++++ oxa/oxa-cli/tests/work_publish.rs | 64 ++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/oxa/oxa-cli/tests/common/work.rs b/oxa/oxa-cli/tests/common/work.rs index 6465f48c5..30e5ef9db 100644 --- a/oxa/oxa-cli/tests/common/work.rs +++ b/oxa/oxa-cli/tests/common/work.rs @@ -55,6 +55,15 @@ pub fn run_observed( turns: Vec, args: &[&str], path: Option, +) -> (serde_json::Value, Vec) { + run_observed_with(env, turns, args, path, |_| {}) +} +pub fn run_observed_with( + env: &TestEnv, + turns: Vec, + args: &[&str], + path: Option, + configure: impl FnOnce(&mut Command), ) -> (serde_json::Value, Vec) { let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); @@ -66,6 +75,7 @@ pub fn run_observed( if let Some(path) = path { command.env("PATH", path); } + configure(&mut command); let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { tx.send(confirm::output(command)).unwrap(); diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index 6a6eac141..ce9e2df80 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -24,7 +24,12 @@ printf '%s\n' "$*" >> "$HOME/gh-calls" host=github.com if test -f "$HOME/enterprise-host"; then host=code.example.invalid; fi case "$1 $2" in - 'repo view') printf '{"nameWithOwner":"example/project","url":"https://%s/example/project"}' "$host" ;; + 'repo view') + if test "${GH_ENTERPRISE_TOKEN+x}" = x || test "${GITHUB_ENTERPRISE_TOKEN+x}" = x; then + touch "$HOME/ambient-publication-auth" + fi + printf '{"nameWithOwner":"example/project","url":"https://%s/example/project"}' "$host" + if test -f "$HOME/oversized-repo-response"; then head -c 1048576 /dev/zero | tr '\000' ' '; fi ;; 'pr list') if test -f "$HOME/pr-state.json"; then printf '['; cat "$HOME/pr-state.json"; printf ']'; else printf '[]'; fi ;; 'api graphql') if test -f "$HOME/queued"; then printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":{"id":"queue"}}}}}'; else printf '%s' '{"data":{"repository":{"pullRequest":{"mergeQueueEntry":null}}}}'; fi ;; 'pr create') @@ -247,6 +252,63 @@ fn publishing_requires_acceptance_without_ignored_helpers() { assert!(!env.home().join("push-calls").exists()); assert!(!env.home().join("gh-calls").exists()); } +#[test] +fn publication_refuses_oversized_repository_metadata_before_push() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write("home/oversized-repo-response", ""); + let record = run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(configure(&env)), + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + assert!(env.home().join("gh-calls").exists()); + assert!(!env.home().join("push-calls").exists()); +} + +#[test] +fn publication_does_not_forward_ambient_enterprise_credentials_to_origin() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write("home/enterprise-host", ""); + let (record, _) = work::run_observed_with( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(configure(&env)), + |command| { + command.env("GH_ENTERPRISE_TOKEN", "synthetic-fixture"); + command.env("GITHUB_ENTERPRISE_TOKEN", "synthetic-fixture"); + }, + ); + assert_eq!(record["outcome"], "done", "{record}"); + assert!(env.home().join("push-calls").exists()); + assert!(!env.home().join("ambient-publication-auth").exists()); +} + +#[test] +fn publication_refuses_a_same_owner_fork_before_push() { + let env = TestEnv::new(); + setup(&env, "[]"); + env.write( + "home/pr-state.json", + r#"{"number":1,"url":"https://github.com/example/project/pull/1","isDraft":false,"headRepositoryOwner":{"login":"example"},"isCrossRepository":true}"#, + ); + env.write("home/published-body", "Synthetic fork"); + let record = run_args( + &env, + edits(), + &["work", "../tasks.json", "--publish"], + Some(configure(&env)), + ); + assert_eq!(record["outcome"], "blocked", "{record}"); + let calls = fs::read_to_string(env.home().join("gh-calls")).unwrap(); + assert!(calls.contains("pr list")); + assert!(!env.home().join("push-calls").exists()); +} + #[test] fn publishes_local_branch_and_updates_same_pr_on_restart() { use std::os::unix::fs::PermissionsExt; From 72a5d6e380f405609dbc6321ff9e377314d93fbd Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:02:00 +0100 Subject: [PATCH 25/26] test(work): assert the confirmed report host is used --- oxa/oxa-cli/src/work_reports.rs | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index 8a914d26e..6029cb170 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -333,6 +333,46 @@ mod tests { assert_eq!(result.disposition, "local"); assert!(result.draft.exists()); } + #[test] + fn report_confirms_and_targets_the_same_qualified_repository() { + let env = TestEnv::new(); + let dirs = Dirs::under(env.root()); + let mut prompts = Vec::new(); + let mut calls = Vec::new(); + let result = report( + ¬e(), + &dirs, + &settings(), + &ScrubCtx::default(), + false, + &mut |prompt| { + prompts.push(prompt.to_owned()); + Some(true) + }, + &mut |program, args| { + if program != "gh" { + return Some((0, "{}".into())); + } + calls.push(args.to_vec()); + match args[1].as_str() { + "list" => Some((0, "[]".into())), + "create" => Some((0, "https://github.com/example/project/issues/10".into())), + _ => panic!("unexpected report request: {args:?}"), + } + }, + ) + .unwrap(); + assert_eq!(result.disposition, "filed"); + assert_eq!(prompts.len(), 2); + assert!(prompts + .iter() + .all(|prompt| prompt.contains("github.com/example/project"))); + assert_eq!(calls.len(), 5); + assert!(calls.iter().all(|args| args + .windows(2) + .any(|pair| { pair[0] == "--repo" && pair[1] == "github.com/example/project" }))); + } + #[test] fn matching_issue_is_commented_after_all_queries_and_reads() { let env = TestEnv::new(); From 373b8bdfb9f149fc798c46ec02e29b984f19d1de Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:03:38 +0100 Subject: [PATCH 26/26] Bound publication commands and bind confirmed destinations --- oxa/docs/work.md | 3 +- oxa/oxa-cli/src/work_reports.rs | 1 + oxa/oxa-cli/tests/work_publish.rs | 4 +- oxa/oxa-work/src/publish.rs | 62 ++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 95ea8facb..7ee628202 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -200,7 +200,8 @@ The [checkpoint integration contract](work-state.md) defines the runner's public event hook. Event/situation/standing stores and their CLI adapters are a separate integration; this slice does not infer facts from worker prose or raw logs. `oxa work tasks.json --publish` pushes the accepted, saved commit to `origin` and -creates or updates its PR through `gh`. Red acceptance never pushes; green +creates or updates its PR through `gh`. Sign in with `gh auth login --hostname HOST` +for the host in `origin`. Publication uses that host's saved login. Red acceptance never pushes; green acceptance makes the PR ready. Blocked tasks do not create PRs. Drift-only accepted reruns may update an existing PR. The runner checks existing PR queue membership before every push and refuses a queued PR; use a new task branch for follow-up work. It refuses branch changes diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs index 6029cb170..cb10fe81b 100644 --- a/oxa/oxa-cli/src/work_reports.rs +++ b/oxa/oxa-cli/src/work_reports.rs @@ -106,6 +106,7 @@ pub(crate) fn report( result.detail = "issue reporting is not configured; draft retained locally".into(); return Ok(result); }; + let repo = format!("github.com/{repo}"); let note = ProblemNote { title, notes: clean_notes(¬e.notes, ctx), diff --git a/oxa/oxa-cli/tests/work_publish.rs b/oxa/oxa-cli/tests/work_publish.rs index ce9e2df80..512d5d871 100644 --- a/oxa/oxa-cli/tests/work_publish.rs +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -40,7 +40,7 @@ case "$1 $2" in if test "$arg" = '--draft'; then draft=true; fi if test "$arg" = '--body-file'; then body_next=true; fi done - printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$host" "$draft" > "$HOME/pr-state.json" + printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"},"isCrossRepository":false}' "$host" "$draft" > "$HOME/pr-state.json" printf 'https://%s/example/project/pull/1' "$host" ;; 'pr view') jq -Rs '{body:.}' "$HOME/published-body" ;; 'pr edit') @@ -52,7 +52,7 @@ case "$1 $2" in 'pr ready') draft=false for arg in "$@"; do if test "$arg" = '--undo'; then draft=true; fi; done - printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"}}' "$host" "$draft" > "$HOME/pr-state.json" ;; + printf '{"number":1,"url":"https://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"},"isCrossRepository":false}' "$host" "$draft" > "$HOME/pr-state.json" ;; *) exit 7 ;; esac "##); diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index a490e58c7..5d230cc97 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -5,7 +5,7 @@ use std::os::unix::fs::OpenOptionsExt; use std::{ fs::File, path::{Path, PathBuf}, - process::Command, + process::{Command, Stdio}, }; fn context( @@ -39,21 +39,41 @@ fn context( writer.replay(&value, observer)?; Ok((writer, lock, value)) } +const MAX_PUBLICATION_RESPONSE_BYTES: usize = 1024 * 1024; + +fn isolate_github_environment(command: &mut Command) { + // The destination comes from origin. Let the CLI choose its stored + // login for that host, independently of host-agnostic shell overrides. + for key in [ + "GH_HOST", + "GH_REPO", + "GH_TOKEN", + "GITHUB_TOKEN", + "GH_ENTERPRISE_TOKEN", + "GITHUB_ENTERPRISE_TOKEN", + ] { + command.env_remove(key); + } +} + fn gh(binary: &Path, policy: &RunPolicy, repo: &Path, lock: &File, args: &[&str]) -> Result { let mut command = Command::new(binary); super::isolate_git_environment(&mut command); + isolate_github_environment(&mut command); command .env("PATH", &policy.host_path) - .env_remove("GH_REPO") - .env_remove("GH_HOST") + .env("GH_PROMPT_DISABLED", "1") .current_dir(repo) .args(args); - command.stdin(lock.try_clone().map_err(|e| e.to_string())?); - let output = command.output().map_err(|e| e.to_string())?; - if !output.status.success() { - return Err("GitHub publication request failed".into()); + match super::command_runner_with_stdin( + command, + Some(MAX_PUBLICATION_RESPONSE_BYTES), + Stdio::from(lock.try_clone().map_err(|e| e.to_string())?), + super::COMMAND_DEADLINE, + ) { + Some((0, text)) => Ok(text.trim().into()), + _ => Err("GitHub publication request failed".into()), } - Ok(String::from_utf8_lossy(&output.stdout).trim().into()) } fn json(text: &str) -> Result { serde_json::from_str(text).map_err(|e| e.to_string()) @@ -194,17 +214,17 @@ pub fn publish( "--head", &task.branch, "--json", - "number,url,isDraft,headRepositoryOwner", + "number,url,isDraft,headRepositoryOwner,isCrossRepository", ], )?)?; let prs = prs.as_array().ok_or("invalid PR list")?; if prs.len() > 1 { return Err("multiple open PRs match the task branch".into()); } - if prs - .first() - .is_some_and(|pr| pr["headRepositoryOwner"]["login"].as_str() != Some(owner)) - { + if prs.first().is_some_and(|pr| { + pr["headRepositoryOwner"]["login"].as_str() != Some(owner) + || pr["isCrossRepository"].as_bool() != Some(false) + }) { return Err("PR head repository does not match origin".into()); } if accepted == Outcome::NothingToDo @@ -284,13 +304,21 @@ pub fn publish( &format!("{head}:refs/heads/{}", task.branch), ], ); + isolate_github_environment(&mut push); push.env("GIT_TERMINAL_PROMPT", "0") + .env("GH_PROMPT_DISABLED", "1") // Metadata probes deny every transport. Only this explicit, // confirmed publication may use the normal GitHub/file transports. - .env("GIT_ALLOW_PROTOCOL", "https:ssh:file") - .stdin(lock.try_clone().map_err(|e| e.to_string())?); - let pushed = push.output().map_err(|e| e.to_string())?; - if !pushed.status.success() { + .env("GIT_ALLOW_PROTOCOL", "https:ssh:file"); + if !matches!( + super::command_runner_with_stdin( + push, + Some(MAX_PUBLICATION_RESPONSE_BYTES), + Stdio::from(lock.try_clone().map_err(|e| e.to_string())?), + super::COMMAND_DEADLINE, + ), + Some((0, _)) + ) { return Err("accepted commit push failed".into()); } let draft = false;