diff --git a/oxa/docs/work.md b/oxa/docs/work.md index 623307e6..e3b57c7a 100644 --- a/oxa/docs/work.md +++ b/oxa/docs/work.md @@ -225,3 +225,33 @@ one short line per task. 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`. 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 +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. +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. 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. +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. 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/args.rs b/oxa/oxa-cli/src/args.rs index c1a9cdfd..44e42e7d 100644 --- a/oxa/oxa-cli/src/args.rs +++ b/oxa/oxa-cli/src/args.rs @@ -281,7 +281,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 | --pr N --model MODEL | board --shadow --repo OWNER/REPO --once", + "oxa work [--rerun] [--publish] [--report-problems] | --issue N --model MODEL | --pr N --model MODEL | --status | board --shadow --repo OWNER/REPO --once", "Run tasks in isolated worktrees with external acceptance checks", ), ( diff --git a/oxa/oxa-cli/src/lib.rs b/oxa/oxa-cli/src/lib.rs index eb4d3e3a..afd91d49 100644 --- a/oxa/oxa-cli/src/lib.rs +++ b/oxa/oxa-cli/src/lib.rs @@ -55,6 +55,7 @@ pub use work_board::refill_input; mod work_checks; mod work_cmd; pub mod work_refill; +mod work_reports; pub use args::{ no_tool_alias, parse_args, usage_text, ApiKey, Args, LoginArgs, ModeArg, RegistryArgs, Subcommand, diff --git a/oxa/oxa-cli/src/report_cmd.rs b/oxa/oxa-cli/src/report_cmd.rs index 557a588d..8c95d894 100644 --- a/oxa/oxa-cli/src/report_cmd.rs +++ b/oxa/oxa-cli/src/report_cmd.rs @@ -457,11 +457,14 @@ pub(super) fn submit_with_storage( /// 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_checks.rs b/oxa/oxa-cli/src/work_checks.rs index bc5e7ba4..4c38f634 100644 --- a/oxa/oxa-cli/src/work_checks.rs +++ b/oxa/oxa-cli/src/work_checks.rs @@ -235,7 +235,9 @@ git --no-replace-objects commit -m {message} } } if failed { - err.write_all(&diagnostics).map_err(|e| e.to_string())?; + let ctx = crate::report_cmd::ScrubCtx::from_env(env, dirs); + let safe = crate::report_cmd::scrub(&String::from_utf8_lossy(&diagnostics), &ctx); + err.write_all(safe.as_bytes()).map_err(|e| e.to_string())?; } Ok(i32::from(failed)) } diff --git a/oxa/oxa-cli/src/work_cmd.rs b/oxa/oxa-cli/src/work_cmd.rs index 9162c8f4..356863d1 100644 --- a/oxa/oxa-cli/src/work_cmd.rs +++ b/oxa/oxa-cli/src/work_cmd.rs @@ -204,8 +204,28 @@ pub(crate) fn run( return Ok(0); } } - let rerun = args.last().is_some_and(|arg| arg == "--rerun"); - let task_args = if rerun { &args[..args.len() - 1] } else { args }; + let (mut rerun, mut publish, mut reports) = (false, false, false); + let mut end = args.len(); + 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; + } + if (publish || reports) + && env.pairs().iter().any(|(name, _)| { + matches!( + name.as_str(), + "OXA_CODING_AGENT" | "AI_AGENT" | "OXA_SUBAGENT_DEPTH" + ) + }) + { + return Err("publishing and problem reporting require the owner outside an agent tool".into()); + } + let task_args = &args[..end]; let is_trusted = trusted(&root, env, dirs)?; let tasks = match task_args { [file] if !file.starts_with('-') => oxa_work::read_tasks(&cwd.join(file))?, @@ -258,7 +278,12 @@ pub(crate) fn run( // Freeze every task before checkout hooks or a first worker can change // shared refs. A bad plan remains a per-task error, not a batch abort. let plans = if is_trusted { - oxa_work::plan_tasks_for_run(&root, &tasks, &policy, rerun) + let planner = if publish || reports { + oxa_work::plan_tasks_for_actions + } else { + oxa_work::plan_tasks_for_run + }; + planner(&root, &tasks, &policy, rerun) } else { tasks .iter() @@ -274,29 +299,94 @@ pub(crate) fn run( for task in planned { let record = match task { Ok(plan) => { - match oxa_work::run_task_with_observer( - &root, - &binary, - &plan, - &policy, - rerun, - &|_: &oxa_work::Record| Ok(()), - ) { - Ok(record) => record, - Err(oxa_work::RunError::Blocked(detail)) => { - let mut record = oxa_work::blocked_record(plan.task(), detail); - record.base_sha = Some(plan.base_sha().to_owned()); - record + let cached = if (publish || reports) && !rerun { + oxa_work::action_record(&policy, plan.task())? + } else { + None + }; + let mut record = if let Some(record) = cached { + record + } else { + 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)) => { + let mut record = oxa_work::blocked_record(plan.task(), detail); + record.base_sha = Some(plan.base_sha().to_owned()); + record + } + Err(oxa_work::RunError::Checkpoint(error)) => return Err(error), } - Err(oxa_work::RunError::Checkpoint(error)) => return Err(error), + }; + if publish && !record.worktree.as_os_str().is_empty() { + let gh = crate::netguard::trusted_executable_for( + "gh", + Some(policy.host_path.as_os_str()), + Some(&root), + Some(&dirs.home), + ) + .unwrap_or_else(|| PathBuf::from("/unavailable/gh")); + record = oxa_work::publish( + &root, + &binary, + plan.task(), + &policy, + &gh, + &|_: &oxa_work::Record| Ok(()), + &mut crate::report_cmd::tty_confirm(env), + )?; + } + if reports && !record.worktree.as_os_str().is_empty() { + let settings = oxa_config::Settings::load(dirs, &root, false); + let ctx = crate::report_cmd::ScrubCtx::from_env(env, dirs); + record = oxa_work::report_notes( + plan.task(), + &policy, + &|_: &oxa_work::Record| Ok(()), + &mut |note, lock, offline| { + let mut runner = |program: &str, args: &[String]| { + let executable = crate::netguard::trusted_executable_for( + program, + Some(policy.host_path.as_os_str()), + Some(&root), + Some(&dirs.home), + )?; + crate::work_reports::process( + &executable.to_string_lossy(), + args, + &root, + lock, + ) + }; + crate::work_reports::report( + note, + dirs, + &settings, + &ctx, + offline, + &mut crate::report_cmd::tty_confirm(env), + &mut runner, + ) + }, + )?; } + record } Err(record) => *record, }; if matches!( record.outcome, oxa_work::Outcome::Red | oxa_work::Outcome::Blocked | oxa_work::Outcome::Interrupted - ) { + ) || record.reports.iter().any(|report| report.disposition == "failed") + { code = 1; } serde_json::to_writer(&mut *out, &record).map_err(|e| e.to_string())?; diff --git a/oxa/oxa-cli/src/work_reports.rs b/oxa/oxa-cli/src/work_reports.rs new file mode 100644 index 00000000..cb10fe81 --- /dev/null +++ b/oxa/oxa-cli/src/work_reports.rs @@ -0,0 +1,503 @@ +//! 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, Confirm, Runner, ScrubCtx}; +use oxa_config::{Dirs, Settings}; +use oxa_work::{ProblemNote, ReportRecord}; +use serde_json::Value; +use std::{ + fs::File, + path::Path, + process::{Command, Stdio}, +}; + +pub(crate) fn process(program: &str, args: &[String], cwd: &Path, lock: &File) -> Option<(i32, String)> { + let mut command = Command::new(program); + command.args(args).current_dir(cwd); + oxa_work::command_runner_with_stdin( + command, + Some(1_048_576), + Stdio::from(lock.try_clone().ok()?), + oxa_work::COMMAND_DEADLINE, + ) +} +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 visible(text: &str) -> String { + text.chars().map(|ch| { + if (ch.is_control() && ch != '\n') + || matches!(ch, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{2028}'..='\u{202e}' | '\u{2066}'..='\u{2069}') + { 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() { + 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, + confirm: Confirm<'_>, + runner: Runner<'_>, +) -> Result { + let note = &ProblemNote { + title: visible(¬e.title), + notes: visible(¬e.notes), + keywords: note.keywords.iter().map(|query| visible(query)).collect(), + }; + 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 repo = format!("github.com/{repo}"); + 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> { + if confirm(&format!( + "Search open issues and PRs in {repo} with these exact queries:\n{}\nType yes to send: ", + note.keywords.join("\n") + )) != Some(true) + { + return Err("search requires controlling-terminal confirmation; draft retained locally".into()); + } + 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 { + 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, + &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); + 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, + &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; + + #[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(), + 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 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 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(); + 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 |_| Some(true), + &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 |_| Some(true), + &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 |_| Some(true), + &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 |_| 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 c9c6ccd4..790ec03b 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") @@ -54,15 +56,36 @@ pub fn run_observed( args: &[&str], path: Option, ) -> (serde_json::Value, Vec) { - let (mut records, requests) = run_batch(env, turns, args, path); + 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 (mut records, requests) = run_batch_with(env, turns, args, path, configure); assert_eq!(records.len(), 1, "expected one task record"); (records.remove(0), requests) } + pub fn run_batch( env: &TestEnv, turns: Vec, args: &[&str], path: Option, +) -> (Vec, Vec) { + run_batch_with(env, turns, args, path, |_| {}) +} + +fn run_batch_with( + env: &TestEnv, + turns: Vec, + args: &[&str], + path: Option, + configure: impl FnOnce(&mut Command), ) -> (Vec, Vec) { let server = FakeServer::start(turns, FakeOpts::default()).unwrap(); let mut command = Command::new(env!("CARGO_BIN_EXE_oxa")); @@ -74,9 +97,10 @@ pub fn run_batch( 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(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 00000000..6e2b78fa --- /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 new file mode 100644 index 00000000..99345bf4 --- /dev/null +++ b/oxa/oxa-cli/tests/work_publish.rs @@ -0,0 +1,789 @@ +//! 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 sha2::{Digest, Sha256}; +use std::{fs, path::PathBuf, process::Command}; +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()) + .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( + "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" +host=github.com +if test -f "$HOME/enterprise-host"; then host=code.example.invalid; fi +case "$1 $2" in + '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') + 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://%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') + 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://%s/example/project/pull/1","isDraft":%s,"headRepositoryOwner":{"login":"example"},"isCrossRepository":false}' "$host" "$draft" > "$HOME/pr-state.json" ;; + *) exit 7 ;; +esac +"##); + 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(); + } + 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 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::{ + 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()), + runner_owns_github: false, + 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 + )); + 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); + 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); + 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(); + 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", test_name]) + .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 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 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; + 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(), + &["work", "../tasks.json", "--publish"], + 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!( + 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 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"]"#); + 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"], 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() { + 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"), + "{blocked}" + ); + 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 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(); + 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!( + !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")); + 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_can_publish_after_red_remained_local() { + 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"], false, "{red}"); + assert!(red["pull_request"].is_null()); + 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 create")); +} +#[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"); +} + +#[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 = 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}"); + 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 = state_path(&env); + let mut state: serde_json::Value = serde_json::from_slice(&fs::read(&state_path).unwrap()).unwrap(); + for key in [ + "pull_request", + "draft", + "problem_notes", + "reports", + "acceptance_outcome", + ] { + state["record"].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"); +} + +#[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","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 ;; +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:?}" + ); +} diff --git a/oxa/oxa-work/src/committed.rs b/oxa/oxa-work/src/committed.rs index dcb7de45..207d2226 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, @@ -73,6 +91,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")?; @@ -83,7 +102,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)?; @@ -100,19 +119,19 @@ 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, normalize: None, }, }, 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 @@ -145,18 +164,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( @@ -167,22 +186,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<()> { @@ -367,7 +381,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/execute.rs b/oxa/oxa-work/src/execute.rs index 3db06afa..0fec3997 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), pub base_alias: Option<&'a tree::BaseAlias>, } impl Runner<'_> { @@ -77,6 +78,7 @@ impl Runner<'_> { } else { point.prompt.clone() }; + let prompt = format!("{prompt}{}", crate::notes::INSTRUCTIONS); let session = state.record.session_id.clone(); let task = state.task.clone(); let result = worker::run( @@ -112,6 +114,13 @@ impl Runner<'_> { state.record.interruption = worker.result.interruption; } state.record.worker = Some(worker.result.clone()); + for note in worker.notes.iter().filter_map(self.scrub_note) { + if state.record.problem_notes.len() < 5 + && !state.record.problem_notes.contains(¬e) + { + state.record.problem_notes.push(note); + } + } state.record.session_id = worker.session.clone(); worker.failure() } @@ -201,7 +210,7 @@ impl Runner<'_> { self.advance(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 932bda8e..4655b380 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -16,6 +16,10 @@ mod committed; pub use committed::{verify_saved_task, VerifiedTask}; mod execute; pub mod helper_queue; +mod notes; +mod publish; +pub use notes::{ProblemNote, ReportRecord}; +pub use publish::{publish, report_notes}; pub mod mirrors; pub mod shadow; mod state; @@ -90,6 +94,16 @@ pub struct Record { pub head_sha: Option, pub tree_sha: Option, pub interruption: Option, + #[serde(default)] + pub pull_request: Option, + #[serde(default)] + pub draft: bool, + #[serde(default)] + pub problem_notes: Vec, + #[serde(default)] + pub reports: Vec, + #[serde(default)] + pub acceptance_outcome: Option, } /// An immutable task/base/check snapshot, built before any task in its batch runs. @@ -121,6 +135,16 @@ pub fn plan_task(repo: &Path, task: Task, policy: &RunPolicy) -> Result 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 completed_execution(step: Step) -> bool { + matches!(step, Step::Finished | Step::Publishing | Step::Reporting) +} + +fn plan_task_mode( + repo: &Path, + task: Task, + policy: &RunPolicy, + rerun: bool, + actions: bool, +) -> Result { validate(&task)?; let Some(saved) = load_task_checkpoint(policy, &task.id)? else { return plan_task(repo, task, policy); @@ -141,8 +190,9 @@ pub fn plan_task_for_run(repo: &Path, task: Task, policy: &RunPolicy, rerun: boo if saved.task != task { 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)) + 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) { @@ -166,10 +216,29 @@ pub fn plan_tasks_for_run( tasks: &[Task], policy: &RunPolicy, rerun: bool, +) -> Vec> { + plan_tasks(repo, tasks, policy, rerun, false) +} + +pub fn plan_tasks_for_actions( + repo: &Path, + tasks: &[Task], + policy: &RunPolicy, + rerun: bool, +) -> Vec> { + plan_tasks(repo, tasks, policy, rerun, true) +} + +fn plan_tasks( + repo: &Path, + tasks: &[Task], + policy: &RunPolicy, + rerun: bool, + actions: bool, ) -> Vec> { let mut plans: Vec<_> = tasks .iter() - .map(|task| plan_task_for_run(repo, task.clone(), policy, rerun)) + .map(|task| plan_task_mode(repo, task.clone(), policy, rerun, actions)) .collect(); let mut targets = std::collections::BTreeMap::>>::new(); @@ -826,9 +895,31 @@ pub fn blocked_record(task: &Task, detail: String) -> Record { head_sha: None, tree_sha: None, interruption: None, + pull_request: None, + draft: false, + problem_notes: vec![], + reports: vec![], + acceptance_outcome: None, } } +/// Read a completed execution for explicit publication/report retries. The action +/// reacquires the task lock and validates its checkpoint before making an effect. +pub fn action_record(policy: &RunPolicy, task: &Task) -> Result> { + validate(task)?; + let Some(saved) = load_task_checkpoint(policy, &task.id)? else { + return Ok(None); + }; + if saved.task != *task { + return Err("task specification changed; use a new task id".into()); + } + Ok(matches!( + saved.record.step, + Step::Finished | Step::Publishing | Step::Reporting + ) + .then_some(saved.record)) +} + fn verify_task_storage(path: &Path) -> std::io::Result<()> { let directory = fs::OpenOptions::new() .read(true) @@ -904,6 +995,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)?; @@ -967,7 +1073,12 @@ pub fn run_task_with_observer( 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); } @@ -988,6 +1099,11 @@ pub fn run_task_with_observer( }; // 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)?; @@ -998,6 +1114,7 @@ pub fn run_task_with_observer( lock: &lock_file, writer: &mut writer, observer, + scrub_note, base_alias: planned.base_alias.as_ref(), }) .drive(&mut state) @@ -1019,6 +1136,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)?; @@ -1030,6 +1151,43 @@ 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, retry, observer) + .map_err(RunError::Checkpoint)?; + return Ok(state.record); + } + state.record.outcome = state + .record + .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) diff --git a/oxa/oxa-work/src/notes.rs b/oxa/oxa-work/src/notes.rs new file mode 100644 index 00000000..e15a44c9 --- /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(Clone, 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 00000000..5d230cc9 --- /dev/null +++ b/oxa/oxa-work/src/publish.rs @@ -0,0 +1,444 @@ +//! Explicit publishing of the accepted commit; existing queued PRs are never pushed. +use super::{checked, state, tree, CheckpointObserver, Outcome, Record, Result, RunPolicy, Task}; +use state::Step; +use std::os::unix::fs::OpenOptionsExt; +use std::{ + fs::File, + path::{Path, PathBuf}, + process::{Command, Stdio}, +}; + +fn context( + task: &Task, + policy: &RunPolicy, + observer: &dyn CheckpointObserver, +) -> Result<(state::Writer, File, state::State)> { + super::validate(task)?; + super::verify_task_storage(&policy.storage).map_err(|e| format!("task storage: {e}"))?; + let path = policy.storage.join(format!("{}.json", task.id)); + let lock = std::fs::OpenOptions::new() + .read(true) + .write(true) + .custom_flags(super::shadow::OWNER_FILE_FLAGS) + .open(path.with_extension("lock")) + .map_err(|e| e.to_string())?; + let metadata = lock + .metadata() + .map_err(|e| format!("cannot inspect task lock: {e}"))?; + // SAFETY: geteuid has no arguments or preconditions. + if !super::shadow::regular_file_owned_by(&metadata, unsafe { libc::geteuid() }) { + return Err("cannot lock task: lock must be an owner-controlled regular file".into()); + } + lock.try_lock() + .map_err(|e| format!("task is already running: {e}"))?; + let value = state::load(&path)?; + if value.task != *task { + return Err("task specification changed".into()); + } + let mut writer = state::Writer::new(path, true); + 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("GH_PROMPT_DISABLED", "1") + .current_dir(repo) + .args(args); + 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()), + } +} +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, + 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, + binary: &Path, + task: &Task, + policy: &RunPolicy, + gh_binary: &Path, + observer: &dyn CheckpointObserver, + confirm: &mut dyn FnMut(&str) -> Option, +) -> Result { + let (mut writer, lock, mut value) = context(task, policy, observer)?; + let accepted = value.record.acceptance_outcome.unwrap_or(value.record.outcome); + if !super::completed_execution(value.record.step) + || !matches!(accepted, Outcome::Done | Outcome::NothingToDo) + { + return Ok(value.record); + } + value.record.acceptance_outcome = Some(accepted); + writer.save(&mut value, Step::Publishing, observer)?; + let git = |repo: &Path, args: &[&str]| super::git(policy, repo, args); + let gh = |repo: &Path, lock: &File, args: &[&str]| gh(gh_binary, policy, repo, lock, args); + let result = (|| -> Result<(String, bool)> { + if policy.offline { + return Err("publishing is unavailable in offline mode".into()); + } + let tree = &value.record.worktree; + tree::validate(policy, repo, tree, task)?; + if super::dirty_worktree(binary, tree, repo, policy, &lock)? { + return Err("uncommitted work must be saved before publishing".into()); + } + let head = checked(git(tree, &["rev-parse", "HEAD"]))?; + 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()); + } + let remote = checked(git(repo, &["remote", "get-url", "--push", "--all", "origin"]))?; + if remote.lines().count() != 1 { + return Err("publishing requires one origin push URL".into()); + } + let repository = repository(&json(&gh( + tree, + &lock, + &["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, + &[ + "pr", + "list", + "--repo", + name, + "--state", + "open", + "--head", + &task.branch, + "--json", + "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) + || pr["isCrossRepository"].as_bool() != Some(false) + }) { + return Err("PR head repository does not match origin".into()); + } + if accepted == Outcome::NothingToDo + && prs.is_empty() + && value.record.head_sha == value.record.base_sha + { + 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 + )) != Some(true) { + return Err("publication requires confirmation through the controlling terminal".into()); + } + if remote != checked(git(repo, &["remote", "get-url", "--push", "--all", "origin"]))? + || head != checked(git(tree, &["rev-parse", "HEAD"]))? + { + 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() { + 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", + "--hostname", + &repository.host, + "-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, + &[ + "-c", + "core.hooksPath=/dev/null", + "-c", + "protocol.https.allow=always", + "-c", + "protocol.ssh.allow=always", + "-c", + "protocol.file.allow=always", + "push", + "origin", + &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"); + 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; + let body = policy.storage.join(format!("{}.pr-body.md", task.id)); + 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, + "--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(); + writer.save(&mut value, Step::Finished, observer)?; + } + Err(error) => { + value.record.outcome = Outcome::Blocked; + value.record.detail = format!("publishing: {error}"); + writer.save(&mut value, Step::Publishing, observer)?; + } + } + Ok(value.record) +} +pub fn report_notes( + task: &Task, + policy: &RunPolicy, + observer: &dyn CheckpointObserver, + reporter: &mut dyn FnMut(&super::ProblemNote, &File, bool) -> Result, +) -> 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 + .reports + .get(index) + .is_some_and(|r| !matches!(r.disposition.as_str(), "local" | "failed")) + { + continue; + } + writer.save(&mut value, reporting_step, observer)?; + let report = + reporter(&value.record.problem_notes[index], &lock, policy.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 { + value.record.reports.push(report); + } + writer.save(&mut value, reporting_step, observer)?; + } + writer.save( + &mut value, + if matches!(prior_step, Step::Saving | Step::Publishing) { + prior_step + } else { + Step::Finished + }, + observer, + )?; + Ok(value.record) +} diff --git a/oxa/oxa-work/src/state.rs b/oxa/oxa-work/src/state.rs index 9894b68b..61e38495 100644 --- a/oxa/oxa-work/src/state.rs +++ b/oxa/oxa-work/src/state.rs @@ -20,6 +20,8 @@ pub enum Step { Saving, #[default] Finished, + Publishing, + Reporting, } #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] @@ -134,7 +136,18 @@ pub(crate) fn load(path: &Path) -> Result { .get("record") .and_then(serde_json::Value::as_object) .ok_or("task checkpoint record is missing")?; - if record.len() != keys.len() || keys.iter().any(|key| !record.contains_key(*key)) { + let optional = [ + "pull_request", + "draft", + "problem_notes", + "reports", + "acceptance_outcome", + ]; + if keys.iter().any(|key| !record.contains_key(*key)) + || record + .keys() + .any(|key| !keys.contains(&key.as_str()) && !optional.contains(&key.as_str())) + { return Err("incomplete or unknown task checkpoint record fields".into()); } let state: State = serde_json::from_value(value).map_err(|e| format!("task checkpoint: {e}"))?; diff --git a/oxa/oxa-work/src/worker.rs b/oxa/oxa-work/src/worker.rs index 82af423d..4187e96a 100644 --- a/oxa/oxa-work/src/worker.rs +++ b/oxa/oxa-work/src/worker.rs @@ -63,6 +63,7 @@ pub(crate) struct Worker { pub result: WorkerResult, pub session: Option, pub diagnostic: String, + pub notes: Vec, } impl Worker { @@ -184,11 +185,12 @@ pub(crate) fn run( interruption: Some(WorkerInterruption::Deadline), tokens, }, - session: session.ok().flatten(), + session: session.ok().and_then(|(id, _)| id), diagnostic: "worker exceeded max_time and its wrap-up grace; worktree retained".into(), + notes: vec![], }); } - let session = session?; + let (session, notes) = session?; let (result, diagnostic) = terminal?; if status.code() != Some(result.exit_code) { return Err("worker exit code disagrees with its oxa_result".into()); @@ -200,11 +202,15 @@ pub(crate) fn run( result, session, diagnostic, + notes, }) }) } -fn read_session(pipe: impl Read, on_session: &mut dyn FnMut(&str) -> Result<()>) -> Result> { +fn read_session( + pipe: impl Read, + on_session: &mut dyn FnMut(&str) -> Result<()>, +) -> Result<(Option, Vec)> { let mut reader = BufReader::new(pipe); let session = line(&mut reader)?.and_then(|line| { let value: serde_json::Value = serde_json::from_slice(&line).ok()?; @@ -220,9 +226,27 @@ fn read_session(pipe: impl Read, on_session: &mut dyn FnMut(&str) -> Result<()>) }) }); let recorded = session.as_deref().map(on_session).transpose(); - std::io::copy(&mut reader, &mut std::io::sink()).map_err(|e| e.to_string())?; + let mut notes = vec![]; + while let Some(bytes) = line(&mut reader)? { + let Ok(value) = serde_json::from_slice::(&bytes) else { + continue; + }; + if value["type"] == "message_end" && value["message"]["role"] == "assistant" { + let mut text = String::new(); + if let Some(parts) = value["message"]["content"].as_array() { + for part in parts { + if part["type"] == "text" { + if let Some(value) = part["text"].as_str() { + text.push_str(value); + } + } + } + } + notes = crate::notes::parse(&text); + } + } recorded?; - Ok(session) + Ok((session, notes)) } fn read_result(pipe: impl Read) -> Result<(WorkerResult, String)> {