flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/crates/runner/src/main.rs

264 lines10,837 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
67pub(crate) fn env(name: &str) -> Result<String> {
68 std::env::var(name).with_context(|| format!("{name} is not set"))
69}
70
71/// Runs git and returns its trimmed output, failing on a non-zero exit.
72pub(crate) fn git(dir: &Path, args: &[&str]) -> Result<String> {
73 let output = Command::new("git")
74 .current_dir(dir)
75 .args(args)
76 .output()
77 .context("could not run git")?;
78 if !output.status.success() {
79 bail!(
80 "git {} failed: {}",
81 args.first().unwrap_or(&""),
82 String::from_utf8_lossy(&output.stderr).trim()
83 );
84 }
85 Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
86}
87
88/// A git option that authenticates one command. The credential is passed
89/// per command and never written to the clone's config or its remote URL,
90/// where the agent would find it.
91pub(crate) fn auth_option(user: &str, token: &str) -> String {
92 let credentials = STANDARD.encode(format!("{user}:{token}"));
93 format!("http.extraHeader=Authorization: Basic {credentials}")
94}
95
96/// Clones the fork, runs the agent on `PROMPT`, commits and pushes what it
97/// did, and returns its closing summary.
98pub(crate) fn run(reporter: &mut Reporter) -> Result<String> {
99 let mut prompt = env("PROMPT")?;
100 let remote = env("GIT_REMOTE")?;
101 let auth = auth_option(&env("G1T_USER")?, &env("G1T_TOKEN")?);
102 let workdir = Path::new(WORKDIR);
103
104 if let Ok(model) = std::env::var("AGENT_MODEL_NAME") {
105 reporter.record(Entry::new("note", &format!("Running on {model}.")));
106 }
107 reporter.record(Entry::new("prompt", &prompt));
108 reporter.flush();
109
110 std::fs::create_dir_all("/work")?;
111 clone::clone(Path::new("/work"), &auth, &[], &remote, WORKDIR).context("could not clone the pull request's fork")?;
112 git(workdir, &["config", "user.name", "g1t agent"])?;
113 git(workdir, &["config", "user.email", "agent@g1t.sh"])?;
114 let branch = git(workdir, &["rev-parse", "--abbrev-ref", "HEAD"])?;
115 let start = git(workdir, &["rev-parse", "HEAD"]).unwrap_or_default();
116
117 // Sent back to work that is already open: start from where the branch it
118 // will land on is now, so what passes here passes there too.
119 if let (Ok(upstream), Ok(upstream_branch)) = (env("UPSTREAM_REMOTE"), env("UPSTREAM_BRANCH")) {
120 clone::fetch(workdir, &auth, &upstream, &upstream_branch).context("could not fetch the branch this will land on")?;
121 // Shallow: deep enough to tell whether it is behind, and to merge.
122 clone::share_history(workdir, &auth, &[("origin", branch.as_str()), (upstream.as_str(), upstream_branch.as_str())], "HEAD", "FETCH_HEAD")?;
123 let behind = Command::new("git")
124 .current_dir(workdir)
125 .args(["merge-base", "--is-ancestor", "FETCH_HEAD", "HEAD"])
126 .status()
127 .is_ok_and(|status| !status.success());
128 if behind {
129 let message = format!("Catch up with {upstream_branch}");
130 let merged = Command::new("git")
131 .current_dir(workdir)
132 .args(["merge", "--quiet", "--no-edit", "-m", &message, "FETCH_HEAD"])
133 .status()
134 .is_ok_and(|status| status.success());
135 if merged {
136 reporter.record(Entry::new(
137 "note",
138 &format!("Merged in the latest {upstream_branch} before starting."),
139 ));
140 } else {
141 let files = git(workdir, &["diff", "--name-only", "--diff-filter=U"])?;
142 let files: Vec<&str> = files.lines().collect();
143 reporter.record(Entry::new(
144 "note",
145 &format!(
146 "Merged in the latest {upstream_branch} before starting; {} conflict.",
147 files.join(", ")
148 ),
149 ));
150 prompt.push_str(&format!(
151 "\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.",
152 files.join(", ")
153 ));
154 }
155 reporter.flush();
156 }
157 }
158
159 // The agent is asked what it learned, which goes to memory, and how sure
160 // it is of its change, which g1t weighs with what it observes. Neither
161 // stays in the summary.
162 let asked = confidence::ask(&learned::ask(&prompt));
163 let summary = confidence::finish(learned::finish(harness::run_claude(workdir, &asked, reporter)?));
164
165 // Commit whatever the agent left in the working tree.
166 if !git(workdir, &["status", "--porcelain"])?.is_empty() {
167 let message = std::env::var("COMMIT_MESSAGE").unwrap_or_else(|_| "Agent changes".into());
168 git(workdir, &["add", "--all"])?;
169 git(workdir, &["commit", "--quiet", "--message", &message])?;
170 }
171 let head = git(workdir, &["rev-parse", "HEAD"])?;
172 if head == start {
173 // An agent woken to answer usually only answers.
174 if std::env::var("MODE").as_deref() == Ok("answer") {
175 return Ok(summary);
176 }
177 bail!("the agent finished without changing anything");
178 }
179 git(
180 workdir,
181 &[
182 "-c",
183 &auth,
184 "push",
185 "--quiet",
186 "origin",
187 &format!("HEAD:{branch}"),
188 ],
189 )
190 .context("could not push the pull request's commits")?;
191 reporter.record(Entry::new(
192 "note",
193 &format!("Pushed {}.", &head[..head.len().min(12)]),
194 ));
195 Ok(summary)
196}
197
198fn main() {
199 // A self-hosted runner's commands; the modes below are what it, and
200 // g1t's sandboxes, run work with.
201 let args: Vec<String> = std::env::args().skip(1).collect();
202 if selfhosted::is_command(&args) {
203 std::process::exit(selfhosted::main(args));
204 }
205 // A guarded sandbox's HTTPS is re-signed on its way out: trust that
206 // before anything is fetched. The guard hook runs before every tool
207 // call, so it skips this.
208 if std::env::var("MODE").as_deref() == Ok("guard") {
209 std::process::exit(guard::hook_main());
210 }
211 guard::trust_egress_ca();
212 // Watches for mining for as long as the sandbox runs (abuse.rs). Not in
213 // the hooks the harness runs after every tool call.
214 if std::env::var("MODE").as_deref() != Ok("steer") {
215 abuse::watch();
216 }
217 // The same image does the other jobs a sandbox is started for.
218 match std::env::var("MODE").as_deref() {
219 Ok("actions") => std::process::exit(actions::main()),
220 Ok("bump") => std::process::exit(bump::main()),
221 Ok("checks") => std::process::exit(checks::main()),
222 Ok("deploy") => std::process::exit(deploy::main()),
223 Ok("update") => std::process::exit(update::main()),
224 Ok("review") => std::process::exit(review::main()),
225 Ok("revise") => std::process::exit(revise::main()),
226 Ok("answer") => std::process::exit(revise::answer()),
227 Ok("plan") => std::process::exit(plan::main()),
228 Ok("reply") => std::process::exit(reply::main()),
229 Ok("queue") => std::process::exit(queue::main()),
230 Ok("mergecheck") => std::process::exit(mergecheck::main()),
231 Ok("steer") => std::process::exit(steer::main()),
232 _ => {}
233 }
234 let mut reporter = match Reporter::from_env() {
235 Ok(reporter) => reporter,
236 Err(error) => {
237 eprintln!("g1t-runner: {error:#}");
238 std::process::exit(2);
239 }
240 };
241 match run(&mut reporter) {
242 Ok(summary) => {
243 reporter.flush();
244 if let Err(error) = reporter.ready(&summary) {
245 eprintln!("g1t-runner: could not mark the pull request ready: {error:#}");
246 std::process::exit(1);
247 }
248 }
249 Err(error) => {
250 eprintln!("g1t-runner: {error:#}");
251 // Stopped at a cap: like a person's stop, the pull request is
252 // left open for a person, not closed.
253 if guard::is_halt(&error) {
254 reporter.record(Entry::new("note", &format!("g1t stopped the agent: {error:#}.")));
255 reporter.flush();
256 std::process::exit(1);
257 }
258 reporter.record(Entry::new("note", &format!("The run failed: {error:#}")));
259 reporter.flush();
260 let _ = reporter.close();
261 std::process::exit(1);
262 }
263 }
264}