//! Git operations owned by agentd. //! //! Invariants: //! * agentd only ever runs git in the shared bare **mirror** (`git -C //! ...`). It never runs git inside a sandbox-controlled repo. //! * All invocations use argv (no shell) with timeouts. //! * The push token travels via `GIT_CONFIG_COUNT/KEY_0/VALUE_0` //! (`http.extraHeader = Authorization: Bearer `), never in the URL, //! argv, or logs. //! * On startup the mirror is created if missing (`clone --bare`), then //! pinned with `gc.auto 0` + `gc.pruneExpire never` (slots reference it //! via alternates — pruning would corrupt them) and made group/world //! readable so the builder uid can fetch from it. use std::path::Path; use std::process::Stdio; use std::time::Duration; const GIT_TIMEOUT: Duration = Duration::from_secs(120); const FETCH_TIMEOUT: Duration = Duration::from_secs(300); const PUSH_TIMEOUT: Duration = Duration::from_secs(300); /// Error type that keeps command output but guarantees no secret is /// embedded: callers must pass redacted output (the token is never placed in /// argv, so `stderr` cannot contain it; still, [`redact`] is applied). #[derive(Debug)] pub struct GitError(pub String); impl std::fmt::Display for GitError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } async fn run_git( dir: Option<&Path>, args: &[&str], extra_env: &[(&str, &str)], timeout: Duration, ) -> Result { let mut cmd = tokio::process::Command::new("git"); if let Some(d) = dir { cmd.arg("-C").arg(d); } // No pager, no interactive prompts, no sandbox repo config leakage. cmd.env("GIT_PAGER", "cat") .env("GIT_TERMINAL_PROMPT", "0") .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .args(args); for (k, v) in extra_env { cmd.env(k, v); } // Kill the whole process group on timeout: `kill_on_drop` only kills the // direct child, so spawn detached via `pre_exec`-less approach — use // `Command::kill_on_drop` plus rely on git's own children exiting when // the parent dies. (No shell is ever involved.) let child = cmd .kill_on_drop(true) .spawn() .map_err(|e| GitError(format!("spawn git {}: {e}", args.join(" "))))?; let out = tokio::time::timeout(timeout, child.wait_with_output()) .await .map_err(|_| GitError(format!("git {} timed out after {timeout:?}", args.join(" "))))? .map_err(|e| GitError(format!("git {}: {e}", args.join(" "))))?; if !out.status.success() { let stderr = String::from_utf8_lossy(&out.stderr); return Err(GitError(format!( "git {} failed: {}", args.join(" "), stderr.trim() ))); } String::from_utf8(out.stdout).map_err(|e| GitError(format!("git output not UTF-8: {e}"))) } /// Ensure the bare mirror exists and is fetch-ready. `repo_url` is the /// upstream (e.g. `https://git.maproj.org/Modern-Apps.git`). The umask is /// relaxed while creating so the builder uid can read the new files. pub async fn ensure_mirror(mirror: &Path, repo_url: &str) -> Result<(), GitError> { if mirror.join("HEAD").exists() || mirror.join("config").exists() { return Ok(()); } if let Some(parent) = mirror.parent() { std::fs::create_dir_all(parent) .map_err(|e| GitError(format!("create mirror parent: {e}")))?; } // 022 umask → files 644, dirs 755: world-readable for the builder uid. #[cfg(unix)] let prev = libc_umask(0o022); let r = run_git( None, &["clone", "--bare", repo_url, &mirror.to_string_lossy()], &[], FETCH_TIMEOUT, ) .await; #[cfg(unix)] libc_umask(prev); r.map(|_| ())?; // Pin GC off: slots use this repo as an alternates source. run_git( Some(mirror), &["config", "gc.auto", "0"], &[], GIT_TIMEOUT, ) .await?; run_git( Some(mirror), &["config", "gc.pruneExpire", "never"], &[], GIT_TIMEOUT, ) .await?; Ok(()) } #[cfg(unix)] fn libc_umask(mask: u32) -> u32 { // Minimal FFI to avoid a libc dependency. extern "C" { fn umask(mask: u32) -> u32; } unsafe { umask(mask) } } /// Refresh the mirror's `main` from upstream before each task. pub async fn fetch_main(mirror: &Path) -> Result<(), GitError> { run_git( Some(mirror), &["fetch", "origin", "+refs/heads/main:refs/heads/main"], &[], FETCH_TIMEOUT, ) .await .map(|_| ()) } /// Base SHA the task branch starts from (mirror's `main`). pub async fn main_sha(mirror: &Path) -> Result { let sha = run_git(Some(mirror), &["rev-parse", "refs/heads/main"], &[], GIT_TIMEOUT).await?; Ok(sha.trim().to_string()) } /// Push one local ref to the upstream branch with the scoped token via /// `http.extraHeader` (token in process env, never URL/argv/logs). /// Replaces the old `push_branch` (`refs/agent-out/*`): the review gate /// pushes `refs/agent-wip/` directly. pub async fn push_ref( mirror: &Path, repo_url: &str, src: &str, dst: &str, token: &str, ) -> Result<(), GitError> { let header_value = format!("Authorization: Bearer {token}"); // Env values are not argv; redact defensively anyway on error paths. let r = run_git( Some(mirror), &["push", repo_url, &format!("+{src}:{dst}")], &[ ("GIT_CONFIG_COUNT", "1"), ("GIT_CONFIG_KEY_0", "http.extraHeader"), ("GIT_CONFIG_VALUE_0", &header_value), ], PUSH_TIMEOUT, ) .await; match r { Ok(_) => Ok(()), Err(GitError(msg)) => Err(GitError(crate::agent::scrub_secrets(&format!( "push {dst}: {msg}" )))), } } /// Import a downloaded slot bundle as the task's WIP ref /// (`refs/agent-wip/`), returning the WIP commit sha. Same verify-then- /// fetch discipline as the old task-bundle import: a corrupt bundle fails, a /// missing prerequisite does not. /// /// `branch` is the task's slot branch (the ref the bundle carries). pub async fn import_wip_bundle( mirror: &Path, bundle_file: &Path, branch: &str, task_id: i64, ) -> Result { let dst = format!("refs/agent-wip/{task_id}"); let verify_err = run_git( Some(mirror), &["bundle", "verify", &bundle_file.to_string_lossy()], &[], GIT_TIMEOUT, ) .await .err() .map(|e| e.0); if let Err(e) = run_git( Some(mirror), &[ "fetch", &bundle_file.to_string_lossy(), &format!("+refs/heads/{branch}:{dst}"), ], &[], FETCH_TIMEOUT, ) .await { if let Some(v) = verify_err { return Err(GitError(format!("wip bundle import failed ({e}); verify: {v}"))); } return Err(GitError(format!("wip bundle import: {e}"))); } let sha = run_git(Some(mirror), &["rev-parse", &dst], &[], GIT_TIMEOUT).await?; Ok(sha.trim().to_string()) } /// Does the WIP ref exist in the mirror, and what sha does it resolve to? pub async fn wip_ref_sha(mirror: &Path, task_id: i64) -> Result, GitError> { let dst = format!("refs/agent-wip/{task_id}"); match run_git(Some(mirror), &["rev-parse", "--verify", &dst], &[], GIT_TIMEOUT).await { Ok(sha) => Ok(Some(sha.trim().to_string())), Err(_) => Ok(None), } } /// Delete the local WIP ref (parked with no changes, discard, or after the /// work moved elsewhere). Missing ref is fine. pub async fn delete_wip_ref(mirror: &Path, task_id: i64) -> Result<(), GitError> { let dst = format!("refs/agent-wip/{task_id}"); match run_git( Some(mirror), &["update-ref", "-d", &dst], &[], GIT_TIMEOUT, ) .await { Ok(_) => Ok(()), Err(e) => { // Tolerate "not found" (already deleted / never created). if wip_ref_sha(mirror, task_id).await?.is_none() { Ok(()) } else { Err(e) } } } }