g1t/crates/runner/src/main.rs

268 lines11,097 bytesCodeBlame
1//! Runs a coding agent on one pull request and reports back to g1t.
2//!
3//! This is the program a hosted sandbox starts. It clones the pull
4//! request's fork, runs the agent harness headless, streams what the agent
5//! does into the pull request's session as it happens, pushes the result
6//! and marks the pull request ready for review. It talks to g1t only through the public API and git, exactly
7//! as an agent on someone's own machine would.
8//!
9//! `MODE` selects another job instead: `checks` runs acceptance checks,
10//! `update` brings a pull request up to date with its target branch,
11//! `review` has an agent review one, and `revise` sends the author back to
12//! address what the checks or a review found, `plan` turns an outcome
13//! into issues, `queue` builds and checks a state of the merge queue,
14//! `mergecheck` finds out whether a pull request merges cleanly,
15//! `actions` runs one job of a GitHub Actions workflow, and `bump` makes a
16//! security update: one package raised in its lockfiles, pushed as g1t.
17//! See the modules of those names.
18//!
19//! Configuration comes from the environment:
20//!
21//! - `G1T_API`, `G1T_TOKEN`, `G1T_USER`: where and who to report as.
22//! - `G1T_REPO`, `PULL_NUMBER`, `GIT_REMOTE`: the pull request and its fork.
23//! - `PROMPT`: what the agent is asked to do.
24//! - `COMMIT_MESSAGE`: used if the agent leaves changes uncommitted.
25//! - `ANTHROPIC_API_KEY`: read by the harness itself.
26//!
27//! Every mode runs with the mining watch in `abuse`: a sandbox that looks
28//! like it is mining stops itself and exits with `abuse::EXIT_CODE`.
29//!
30//! Given a command instead (`register`, `run`, `service`, `remove`,
31//! `update`, `version`), it is a self-hosted runner on someone's own
32//! machine, which runs work in these modes: see `selfhosted`.
33
34mod abuse;
35mod actions;
36mod bump;
37mod checks;
38mod clone;
39mod confidence;
40mod deploy;
41mod guard;
42mod harness;
43mod learned;
44mod mergecheck;
45mod plan;
46mod progress;
47mod queue;
48mod reply;
49mod report;
50mod review;
51mod revise;
52mod selfhosted;
53mod steer;
54mod update;
55
56use std::path::Path;
57use std::process::Command;
58
59use anyhow::{Context, Result, bail};
60use base64::Engine;
61use base64::engine::general_purpose::STANDARD;
62
63use report::{Entry, Reporter};
64
65pub(crate) const WORKDIR: &str = "/work/repo";
66/// The name and address on every commit g1t makes here, whether its agent
67/// or g1t itself: `g1t_contracts::system::{USERNAME, EMAIL}`.
68pub(crate) const AUTHOR_NAME: &str = "g1t";
69pub(crate) const AUTHOR_EMAIL: &str = "g1t@users.noreply.g1t.sh";
70
71pub(crate) fn env(name: &str) -> Result<String> {
72 std::env::var(name).with_context(|| format!("{name} is not set"))
73}
74
75/// Runs git and returns its trimmed output, failing on a non-zero exit.
76pub(crate) fn git(dir: &Path, args: &[&str]) -> Result<String> {
77 let output = Command::new("git")
78 .current_dir(dir)
79 .args(args)
80 .output()
81 .context("could not run git")?;
82 if !output.status.success() {
83 bail!(
84 "git {} failed: {}",
85 args.first().unwrap_or(&""),
86 String::from_utf8_lossy(&output.stderr).trim()
87 );
88 }
89 Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
90}
91
92/// A git option that authenticates one command. The credential is passed
93/// per command and never written to the clone's config or its remote URL,
94/// where the agent would find it.
95pub(crate) fn auth_option(user: &str, token: &str) -> String {
96 let credentials = STANDARD.encode(format!("{user}:{token}"));
97 format!("http.extraHeader=Authorization: Basic {credentials}")
98}
99
100/// Clones the fork, runs the agent on `PROMPT`, commits and pushes what it
101/// did, and returns its closing summary.
102pub(crate) fn run(reporter: &mut Reporter) -> Result<String> {
103 let mut prompt = env("PROMPT")?;
104 let remote = env("GIT_REMOTE")?;
105 let auth = auth_option(&env("G1T_USER")?, &env("G1T_TOKEN")?);
106 let workdir = Path::new(WORKDIR);
107
108 if let Ok(model) = std::env::var("AGENT_MODEL_NAME") {
109 reporter.record(Entry::new("note", &format!("Running on {model}.")));
110 }
111 reporter.record(Entry::new("prompt", &prompt));
112 reporter.flush();
113
114 std::fs::create_dir_all("/work")?;
115 clone::clone(Path::new("/work"), &auth, &[], &remote, WORKDIR).context("could not clone the pull request's fork")?;
116 git(workdir, &["config", "user.name", crate::AUTHOR_NAME])?;
117 git(workdir, &["config", "user.email", crate::AUTHOR_EMAIL])?;
118 let branch = git(workdir, &["rev-parse", "--abbrev-ref", "HEAD"])?;
119 let start = git(workdir, &["rev-parse", "HEAD"]).unwrap_or_default();
120
121 // Sent back to work that is already open: start from where the branch it
122 // will land on is now, so what passes here passes there too.
123 if let (Ok(upstream), Ok(upstream_branch)) = (env("UPSTREAM_REMOTE"), env("UPSTREAM_BRANCH")) {
124 clone::fetch(workdir, &auth, &upstream, &upstream_branch).context("could not fetch the branch this will land on")?;
125 // Shallow: deep enough to tell whether it is behind, and to merge.
126 clone::share_history(workdir, &auth, &[("origin", branch.as_str()), (upstream.as_str(), upstream_branch.as_str())], "HEAD", "FETCH_HEAD")?;
127 let behind = Command::new("git")
128 .current_dir(workdir)
129 .args(["merge-base", "--is-ancestor", "FETCH_HEAD", "HEAD"])
130 .status()
131 .is_ok_and(|status| !status.success());
132 if behind {
133 let message = format!("Catch up with {upstream_branch}");
134 let merged = Command::new("git")
135 .current_dir(workdir)
136 .args(["merge", "--quiet", "--no-edit", "-m", &message, "FETCH_HEAD"])
137 .status()
138 .is_ok_and(|status| status.success());
139 if merged {
140 reporter.record(Entry::new(
141 "note",
142 &format!("Merged in the latest {upstream_branch} before starting."),
143 ));
144 } else {
145 let files = git(workdir, &["diff", "--name-only", "--diff-filter=U"])?;
146 let files: Vec<&str> = files.lines().collect();
147 reporter.record(Entry::new(
148 "note",
149 &format!(
150 "Merged in the latest {upstream_branch} before starting; {} conflict.",
151 files.join(", ")
152 ),
153 ));
154 prompt.push_str(&format!(
155 "\n\nBefore you started, the latest {upstream_branch} was merged into this branch, and these files conflict: {}. Resolve the conflicts first, keeping what both sides meant, then address the points above. Leave no conflict markers.",
156 files.join(", ")
157 ));
158 }
159 reporter.flush();
160 }
161 }
162
163 // The agent is asked what it learned, which goes to memory, and how sure
164 // it is of its change, which g1t weighs with what it observes. Neither
165 // stays in the summary.
166 let asked = confidence::ask(&learned::ask(&prompt));
167 let summary = confidence::finish(learned::finish(harness::run_claude(workdir, &asked, reporter)?));
168
169 // Commit whatever the agent left in the working tree.
170 if !git(workdir, &["status", "--porcelain"])?.is_empty() {
171 let message = std::env::var("COMMIT_MESSAGE").unwrap_or_else(|_| "Agent changes".into());
172 git(workdir, &["add", "--all"])?;
173 git(workdir, &["commit", "--quiet", "--message", &message])?;
174 }
175 let head = git(workdir, &["rev-parse", "HEAD"])?;
176 if head == start {
177 // An agent woken to answer usually only answers.
178 if std::env::var("MODE").as_deref() == Ok("answer") {
179 return Ok(summary);
180 }
181 bail!("the agent finished without changing anything");
182 }
183 git(
184 workdir,
185 &[
186 "-c",
187 &auth,
188 "push",
189 "--quiet",
190 "origin",
191 &format!("HEAD:{branch}"),
192 ],
193 )
194 .context("could not push the pull request's commits")?;
195 reporter.record(Entry::new(
196 "note",
197 &format!("Pushed {}.", &head[..head.len().min(12)]),
198 ));
199 Ok(summary)
200}
201
202fn main() {
203 // A self-hosted runner's commands; the modes below are what it, and
204 // g1t's sandboxes, run work with.
205 let args: Vec<String> = std::env::args().skip(1).collect();
206 if selfhosted::is_command(&args) {
207 std::process::exit(selfhosted::main(args));
208 }
209 // A guarded sandbox's HTTPS is re-signed on its way out: trust that
210 // before anything is fetched. The guard hook runs before every tool
211 // call, so it skips this.
212 if std::env::var("MODE").as_deref() == Ok("guard") {
213 std::process::exit(guard::hook_main());
214 }
215 guard::trust_egress_ca();
216 // Watches for mining for as long as the sandbox runs (abuse.rs). Not in
217 // the hooks the harness runs after every tool call.
218 if std::env::var("MODE").as_deref() != Ok("steer") {
219 abuse::watch();
220 }
221 // The same image does the other jobs a sandbox is started for.
222 match std::env::var("MODE").as_deref() {
223 Ok("actions") => std::process::exit(actions::main()),
224 Ok("bump") => std::process::exit(bump::main()),
225 Ok("checks") => std::process::exit(checks::main()),
226 Ok("deploy") => std::process::exit(deploy::main()),
227 Ok("update") => std::process::exit(update::main()),
228 Ok("review") => std::process::exit(review::main()),
229 Ok("revise") => std::process::exit(revise::main()),
230 Ok("answer") => std::process::exit(revise::answer()),
231 Ok("plan") => std::process::exit(plan::main()),
232 Ok("reply") => std::process::exit(reply::main()),
233 Ok("queue") => std::process::exit(queue::main()),
234 Ok("mergecheck") => std::process::exit(mergecheck::main()),
235 Ok("steer") => std::process::exit(steer::main()),
236 _ => {}
237 }
238 let mut reporter = match Reporter::from_env() {
239 Ok(reporter) => reporter,
240 Err(error) => {
241 eprintln!("g1t-runner: {error:#}");
242 std::process::exit(2);
243 }
244 };
245 match run(&mut reporter) {
246 Ok(summary) => {
247 reporter.flush();
248 if let Err(error) = reporter.ready(&summary) {
249 eprintln!("g1t-runner: could not mark the pull request ready: {error:#}");
250 std::process::exit(1);
251 }
252 }
253 Err(error) => {
254 eprintln!("g1t-runner: {error:#}");
255 // Stopped at a cap: like a person's stop, the pull request is
256 // left open for a person, not closed.
257 if guard::is_halt(&error) {
258 reporter.record(Entry::new("note", &format!("g1t stopped the agent: {error:#}.")));
259 reporter.flush();
260 std::process::exit(1);
261 }
262 reporter.record(Entry::new("note", &format!("The run failed: {error:#}")));
263 reporter.flush();
264 let _ = reporter.close();
265 std::process::exit(1);
266 }
267 }
268}