| 1 | //! Runs a coding agent on one attempt and reports back to g1t. |
| 2 | //! |
| 3 | //! This is the program a hosted sandbox starts. It clones the attempt's |
| 4 | //! fork, runs the agent harness headless, streams what the agent does into |
| 5 | //! the attempt's session as it happens, pushes the result and submits the |
| 6 | //! attempt. It talks to g1t only through the public API and git, exactly |
| 7 | //! as an agent on someone's own machine would. |
| 8 | //! |
| 9 | //! Configuration comes from the environment: |
| 10 | //! |
| 11 | //! - `G1T_API`, `G1T_TOKEN`, `G1T_USER`: where and who to report as. |
| 12 | //! - `ATTEMPT_ID`, `GIT_REMOTE`: the attempt and its fork. |
| 13 | //! - `PROMPT`: what the agent is asked to do. |
| 14 | //! - `COMMIT_MESSAGE`: used if the agent leaves changes uncommitted. |
| 15 | //! - `ANTHROPIC_API_KEY`: read by the harness itself. |
| 16 | |
| 17 | mod harness; |
| 18 | mod report; |
| 19 | |
| 20 | use std::path::Path; |
| 21 | use std::process::Command; |
| 22 | |
| 23 | use anyhow::{Context, Result, bail}; |
| 24 | use base64::Engine; |
| 25 | use base64::engine::general_purpose::STANDARD; |
| 26 | |
| 27 | use report::{Entry, Reporter}; |
| 28 | |
| 29 | const WORKDIR: &str = "/work/repo"; |
| 30 | |
| 31 | fn env(name: &str) -> Result<String> { |
| 32 | std::env::var(name).with_context(|| format!("{name} is not set")) |
| 33 | } |
| 34 | |
| 35 | /// Runs git and returns its trimmed output, failing on a non-zero exit. |
| 36 | fn git(dir: &Path, args: &[&str]) -> Result<String> { |
| 37 | let output = Command::new("git") |
| 38 | .current_dir(dir) |
| 39 | .args(args) |
| 40 | .output() |
| 41 | .context("could not run git")?; |
| 42 | if !output.status.success() { |
| 43 | bail!( |
| 44 | "git {} failed: {}", |
| 45 | args.first().unwrap_or(&""), |
| 46 | String::from_utf8_lossy(&output.stderr).trim() |
| 47 | ); |
| 48 | } |
| 49 | Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned()) |
| 50 | } |
| 51 | |
| 52 | /// A git option that authenticates one command. The credential is passed |
| 53 | /// per command and never written to the clone's config or its remote URL, |
| 54 | /// where the agent would find it. |
| 55 | fn auth_option(user: &str, token: &str) -> String { |
| 56 | let credentials = STANDARD.encode(format!("{user}:{token}")); |
| 57 | format!("http.extraHeader=Authorization: Basic {credentials}") |
| 58 | } |
| 59 | |
| 60 | fn run(reporter: &mut Reporter) -> Result<String> { |
| 61 | let prompt = env("PROMPT")?; |
| 62 | let remote = env("GIT_REMOTE")?; |
| 63 | let auth = auth_option(&env("G1T_USER")?, &env("G1T_TOKEN")?); |
| 64 | let workdir = Path::new(WORKDIR); |
| 65 | |
| 66 | reporter.record(Entry::new("prompt", &prompt)); |
| 67 | reporter.flush(); |
| 68 | |
| 69 | std::fs::create_dir_all("/work")?; |
| 70 | git( |
| 71 | Path::new("/work"), |
| 72 | &["-c", &auth, "clone", "--quiet", &remote, WORKDIR], |
| 73 | ) |
| 74 | .context("could not clone the attempt's fork")?; |
| 75 | git(workdir, &["config", "user.name", "g1t agent"])?; |
| 76 | git(workdir, &["config", "user.email", "agent@g1t.sh"])?; |
| 77 | let branch = git(workdir, &["rev-parse", "--abbrev-ref", "HEAD"])?; |
| 78 | let start = git(workdir, &["rev-parse", "HEAD"]).unwrap_or_default(); |
| 79 | |
| 80 | let summary = harness::run_claude(workdir, &prompt, reporter)?; |
| 81 | |
| 82 | // Commit whatever the agent left in the working tree. |
| 83 | if !git(workdir, &["status", "--porcelain"])?.is_empty() { |
| 84 | let message = std::env::var("COMMIT_MESSAGE").unwrap_or_else(|_| "Agent changes".into()); |
| 85 | git(workdir, &["add", "--all"])?; |
| 86 | git(workdir, &["commit", "--quiet", "--message", &message])?; |
| 87 | } |
| 88 | let head = git(workdir, &["rev-parse", "HEAD"])?; |
| 89 | if head == start { |
| 90 | bail!("the agent finished without changing anything"); |
| 91 | } |
| 92 | git( |
| 93 | workdir, |
| 94 | &[ |
| 95 | "-c", |
| 96 | &auth, |
| 97 | "push", |
| 98 | "--quiet", |
| 99 | "origin", |
| 100 | &format!("HEAD:{branch}"), |
| 101 | ], |
| 102 | ) |
| 103 | .context("could not push the attempt's commits")?; |
| 104 | reporter.record(Entry::new( |
| 105 | "note", |
| 106 | &format!( |
| 107 | "Pushed {} to the attempt's fork.", |
| 108 | &head[..head.len().min(12)] |
| 109 | ), |
| 110 | )); |
| 111 | Ok(summary) |
| 112 | } |
| 113 | |
| 114 | fn main() { |
| 115 | let mut reporter = match Reporter::from_env() { |
| 116 | Ok(reporter) => reporter, |
| 117 | Err(error) => { |
| 118 | eprintln!("g1t-runner: {error:#}"); |
| 119 | std::process::exit(2); |
| 120 | } |
| 121 | }; |
| 122 | match run(&mut reporter) { |
| 123 | Ok(summary) => { |
| 124 | reporter.flush(); |
| 125 | if let Err(error) = reporter.submit(&summary) { |
| 126 | eprintln!("g1t-runner: could not submit: {error:#}"); |
| 127 | std::process::exit(1); |
| 128 | } |
| 129 | } |
| 130 | Err(error) => { |
| 131 | eprintln!("g1t-runner: {error:#}"); |
| 132 | reporter.record(Entry::new("note", &format!("The run failed: {error:#}"))); |
| 133 | reporter.flush(); |
| 134 | let _ = reporter.abandon(); |
| 135 | std::process::exit(1); |
| 136 | } |
| 137 | } |
| 138 | } |