diff --git a/Cargo.lock b/Cargo.lock index 5ca40e2..700d231 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,7 +302,7 @@ dependencies = [ [[package]] name = "github" -version = "0.13.1" +version = "0.14.0" dependencies = [ "anyhow", "pointman-extension", diff --git a/Cargo.toml b/Cargo.toml index 3848c4b..78cf04d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "github" -version = "0.13.1" +version = "0.14.0" edition = "2021" description = "GitHub on Pointman's nodes: gh on each machine, and agents' runs acting on GitHub as Pointman App." license = "MIT" diff --git a/README.md b/README.md index d25304c..5d55765 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,23 @@ Pointman itself. |---|---|---| | `cli` | empty | GitHub's CLI, when it isn't on the node's PATH or in /opt/homebrew/bin or /usr/local/bin; empty, the extension installs its own | | `coauthors` | `{}` | Pointman handles mapped to `Name ` commit authors | -| `default_author` | empty | A handle from `coauthors` to credit on automatic runs too | - -To keep an owner's commit credit through agent handoffs, CI reports and timer wakes, set -`default_author = "james"` alongside their entry in `coauthors`. Commits then name that person as author, -the App as committer, and `Pointman ` as co-author. A person directly starting a run -takes precedence; an unmapped person stays App-only. An unreadable run-person file also stays App-only. -Leaving `default_author` empty keeps automatic runs App-only. +| `default_author` | empty | Who is credited in runs no person started: a handle from `coauthors`, `app` for no one, or empty for this machine's own GitHub account | +| `client_coauthors` | `{claude = "Claude "}` | Agent clients (by name) credited as co-authors after Pointman | + +In runs no person started (agent handoffs, CI reports, timer wakes), an agent's commit names an author from +`default_author`: a handle from `coauthors`, `app` for App-only, or, left empty, this machine's own GitHub +account (the account its `gh` is signed in to, as `Name `, checked every +5 minutes and never with the App's token). The App is the committer and `Pointman ` the +co-author. An account that can't be checked (signed out, an error) is dropped at once, and one not checked +for 15 minutes isn't used, so those commits stay App-only. + +A person directly starting a run is credited only with an entry in `coauthors`; a person who isn't in it +stays App-only, never someone else, the machine's own account included. An unreadable run-person file, an +unreadable or malformed `coauthors`, or an entry that isn't one `Name ` also stays App-only. + +The run's client is a further co-author on those commits: the node names it in the run's `MB_RUN_CLIENT` +(`claude`, `codex`…), and `client_coauthors` gives its `Name `. A client that isn't listed gets no +line. Agents never write these lines themselves. A pull request has one creator: the account whose token opens it. PRs opened with the App still show the App as creator; the author and co-author above appear on their commits. Existing commits are unchanged. diff --git a/SKILL.md b/SKILL.md index 1459e8d..7c974b9 100644 --- a/SKILL.md +++ b/SKILL.md @@ -5,11 +5,13 @@ description: Writing on GitHub (pull requests, issues, comments, reviews, commit # Writing on GitHub as Pointman App -The extension may also have a `default_author` configured for automatic runs: their commits credit -that person with Pointman as co-author, including after agent handoffs and timer wakes. A directly -named person takes precedence. PRs themselves still have the App as their single creator. +Your commits are credited to a person, with Pointman and your client (Claude, for example) as co-authors: +the person whose post started the run, when the extension's `coauthors` names them; in runs no person +started (agent handoffs, timer wakes), the machine's person (its own GitHub account, or a configured +`default_author`). Anyone else's commits stay the App's. PRs themselves still have the App as +their single creator. -In your runs, `gh` and git act on GitHub as **Pointman App**: PRs, issues, comments, reviews, pushes and commits all show as the App's bot (`[bot]`). The exception is a commit in a run a person started, or one with a configured default_author: it is authored as that person, with the App as committer and `Co-authored-by: Pointman`, added by the `git` the extension gives you. Don't write that line or set the author yourself. The extension gives each command a short-lived token for the repo's owner, so you never sign in, set a token, or change git's name and email. +In your runs, `gh` and git act on GitHub as **Pointman App**: PRs, issues, comments, reviews, pushes and commits all show as the App's bot (`[bot]`). The exception is a commit someone is credited on (above): it is authored as that person, with the App as committer and `Co-authored-by` lines for Pointman and your client, added by the `git` the extension gives you. Don't write those lines or set the author yourself. The extension gives each command a short-lived token for the repo's owner, so you never sign in, set a token, or change git's name and email. Where the App can't act (it isn't installed on the repo's account, or GitHub refuses it), `gh` and git use this machine's own `gh` login instead, and say so in a line starting `github:`. What you do then shows on GitHub as that login's person, not the bot. diff --git a/extension.toml b/extension.toml index 955b350..b365651 100644 --- a/extension.toml +++ b/extension.toml @@ -5,7 +5,7 @@ [extension] id = "github" name = "GitHub" -version = "0.13.1" +version = "0.14.0" publisher = "aurelio" summary = "GitHub's CLI on this machine, and agents' runs acting on GitHub as Pointman App, else as the machine's gh login." node = ">=0.4.4" @@ -51,7 +51,7 @@ description = "GitHub's CLI, when it isn't on the node's PATH or in /opt/homebre [settings.properties.coauthors] type = "object" default = {} -description = "People credited on the commits agents make in a chat with them, by Pointman handle: {\"james\": \"James Briggs <35938317+jamescalam@users.noreply.github.com>\"}. The person is the commit's author, Pointman App its committer, and Pointman (noreply@pointman.ai) its Co-authored-by. Runs no person started stay App-only unless default_author is configured." +description = "People credited on the commits agents make in a chat with them, by Pointman handle: {\"james\": \"James Briggs <35938317+jamescalam@users.noreply.github.com>\"}. The person is the commit's author, Pointman App its committer, and Pointman (noreply@pointman.ai) its Co-authored-by. A person who starts a run and isn't in it stays App-only, never someone else; runs no person started follow default_author." [settings.properties.coauthors.additionalProperties] type = "string" @@ -59,4 +59,12 @@ type = "string" [settings.properties.default_author] type = "string" default = "" -description = "Pointman handle from coauthors to credit on commits in automatic runs too (for example james). Empty keeps automatic commits App-only. A person who directly starts a run takes precedence; an unmapped person stays App-only. PRs are still opened by the App." +description = "Who is credited on commits in runs no person started (an agent's mention, a timer, a workflow): a Pointman handle from coauthors (for example james), app for no one (App-only), or empty for this machine's own GitHub account (its gh login). A person who directly starts a run takes precedence. PRs are still opened by the App." + +[settings.properties.client_coauthors] +type = "object" +default = { claude = "Claude " } +description = "The agent's client credited as a co-author after Pointman, by the client's name (claude, codex, gemini…), on the commits a person is credited on. A client not in it gets no line." + +[settings.properties.client_coauthors.additionalProperties] +type = "string" diff --git a/src/app.rs b/src/app.rs index b470bf4..cd017f3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -388,20 +388,22 @@ fn write_script(dir: &Path, name: &str, text: &str) -> anyhow::Result { Ok(path) } -/// Writes the runs' `gh` and `git` (see `shim` and `git_shim`), the people who are credited on their commits, and -/// where GitHub's CLI is. +/// Writes the runs' `gh` and `git` (see `shim` and `git_shim`), the people and clients who are credited on their +/// commits, and where GitHub's CLI is. pub fn write_shim( exe: &Path, state: &Path, gh: Option<&Path>, coauthors: &serde_json::Value, default_author: &str, + client_coauthors: &serde_json::Value, ) -> anyhow::Result { let dir = shim_dir(state); std::fs::create_dir_all(&dir)?; write_script(&dir, "git", &git_shim(exe, state))?; write_script(&dir, "coauthors.json", &coauthors.to_string())?; write_script(&dir, "default-author", default_author)?; + write_script(&dir, "client-coauthors.json", &client_coauthors.to_string())?; match gh { Some(gh) => { write_script(&state.join("app"), "cli", &gh.display().to_string())?; @@ -422,21 +424,125 @@ pub fn git_shim(exe: &Path, state: &Path) -> String { ) } -/// `Name ` for a person's handle, as the extension's `coauthors` setting names them. +/// `default_author`'s value that keeps commits in runs no person started the App's alone. +pub const APP_ONLY_AUTHOR: &str = "app"; + +/// Where this machine's own GitHub account is kept, as `Name ` (`machine_author`). +pub fn machine_author_file(state: &Path) -> PathBuf { + shim_dir(state).join("machine-author") +} + +/// This machine's own GitHub account as a commit's author: `gh api user`'s name (else login), with GitHub's +/// noreply address for it, so the commit shows on that account without its real email. +pub fn machine_author(user: &serde_json::Value) -> Option { + let login = user["login"].as_str()?.trim(); + let id = user["id"].as_u64()?; + let name = user["name"].as_str().map(str::trim).filter(|n| !n.is_empty()).unwrap_or(login); + let ok = |s: &str| !s.is_empty() && !s.contains(['\n', '<', '>']); + (ok(login) && ok(name)).then(|| format!("{name} <{id}+{login}@users.noreply.github.com>")) +} + +/// How long a kept machine account is believed (`refresh_machine_author` keeps it fresh well within this): past it, a +/// machine whose extension stopped asking credits no one, rather than whoever was signed in last. +pub const MACHINE_AUTHOR_FOR: std::time::Duration = std::time::Duration::from_secs(900); + +/// Asks this machine's own `gh` login who it is (`gh api user`, with no token from the environment, so never the +/// App's) and keeps the answer where the runs' git reads it (`machine_author_file`). Anything else (no `gh`, signed +/// out, an error, a reply that isn't one account) removes what was kept, so an account that can't be verified is +/// never credited; the error says why. +pub fn refresh_machine_author(state: &Path, gh: Option<&Path>) -> anyhow::Result { + match ask_machine_author(gh) { + Ok(who) => keep_machine_author(state, &who).map(|()| who), + Err(e) => match forget_machine_author(state) { + Ok(()) => Err(e), + Err(rm) => Err(e.context(format!("and the account kept before couldn't be removed: {rm:#}"))), + }, + } +} + +fn ask_machine_author(gh: Option<&Path>) -> anyhow::Result { + let gh = gh.context("there's no gh on this machine")?; + let out = Command::new(gh) + .args(["api", "user"]) + .env_remove("GH_TOKEN") + .env_remove("GITHUB_TOKEN") + .env_remove("GH_ENTERPRISE_TOKEN") + .env_remove("GITHUB_ENTERPRISE_TOKEN") + .stdin(Stdio::null()) + .output() + .context("couldn't run gh api user")?; + if !out.status.success() { + anyhow::bail!("gh api user: {}", String::from_utf8_lossy(&out.stderr).trim()); + } + let user: Value = serde_json::from_slice(&out.stdout).context("gh api user's answer isn't JSON")?; + machine_author(&user).context("gh api user's answer has no usable login and id") +} + +/// Keeps `who` in `machine_author_file`, written every time so its age says when it was last verified. +fn keep_machine_author(state: &Path, who: &str) -> anyhow::Result<()> { + let dir = shim_dir(state); + std::fs::create_dir_all(&dir)?; + let mut f = tempfile::NamedTempFile::new_in(&dir)?; + f.write_all(who.as_bytes())?; + f.persist(machine_author_file(state))?; + Ok(()) +} + +fn forget_machine_author(state: &Path) -> std::io::Result<()> { + match std::fs::remove_file(machine_author_file(state)) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } +} + +/// This machine's own GitHub account, when one was verified less than `MACHINE_AUTHOR_FOR` ago. +fn machine_identity(state: &Path) -> Option { + let path = machine_author_file(state); + // a time in the future (a clock moved back) isn't a recent check either + let age = std::fs::metadata(&path).ok()?.modified().ok()?.elapsed().ok()?; + if age >= MACHINE_AUTHOR_FOR { + return None; + } + let who = std::fs::read_to_string(&path).ok()?; + let who = who.trim(); + (split_identity(who).is_some() && !who.contains('\n')).then(|| who.to_string()) +} + +/// `Name ` for the person credited on a commit in this run (`person`: whose post started it, "" for none): +/// - a person: their entry in the `coauthors` setting, else no one, so the commit stays the App's. An unmapped +/// person never becomes someone else, the machine's person included; +/// - a run no person started (an agent's mention, a timer, a workflow): `default_author`'s entry in `coauthors` when +/// it names a handle, no one for `app`, else this machine's own GitHub account (`machine_identity`). +/// +/// It fails closed: an unreadable or malformed `coauthors` or `default_author`, or an entry that isn't one +/// `Name `, credits no one. fn coauthor(state: &Path, person: &str) -> Option { - // Only an automatic run may use the configured fallback. An unmapped person must never - // silently become someone else. - let fallback; - let person = if person.is_empty() { - fallback = std::fs::read_to_string(shim_dir(state).join("default-author")).ok()?; - fallback.trim() - } else { - person - }; let text = std::fs::read_to_string(shim_dir(state).join("coauthors.json")).ok()?; + let v: Value = serde_json::from_str(&text).ok()?; + let people = v.as_object()?; + let mapped = |handle: &str| { + let who = people.get(handle)?.as_str()?.trim(); + (!handle.is_empty() && split_identity(who).is_some() && !who.contains('\n')).then(|| who.to_string()) + }; + if !person.is_empty() { + return mapped(person); + } + let default = std::fs::read_to_string(shim_dir(state).join("default-author")).ok()?; + match default.trim() { + "" => machine_identity(state), + APP_ONLY_AUTHOR => None, + handle => mapped(handle), + } +} + +/// `Name ` for the client a run uses (`MB_RUN_CLIENT`: claude, codex…), as the `client_coauthors` setting +/// names it; none for a client it doesn't name. +pub fn client_coauthor(state: &Path, client: &str) -> Option { + let client = client.trim(); + let text = std::fs::read_to_string(shim_dir(state).join("client-coauthors.json")).ok()?; let v: serde_json::Value = serde_json::from_str(&text).ok()?; - let who = v.get(person)?.as_str()?.trim(); - (!person.is_empty() && !who.is_empty() && !who.contains('\n')).then(|| who.to_string()) + let who = v.get(client)?.as_str()?.trim(); + (!client.is_empty() && split_identity(who).is_some() && !who.contains('\n')).then(|| who.to_string()) } /// Where git's subcommand is in its arguments, past the options that come before it. @@ -469,15 +575,20 @@ pub fn talks_to_github(args: &[String]) -> bool { /// (the App's bot email shows as `pointman-app[bot]`), so this email is one no account has, and GitHub shows the name. pub const COAUTHOR: &str = "Pointman "; -/// git's arguments with Pointman credited as co-author (`COAUTHOR`), when this is a `commit` with a directly named person -/// or an explicitly configured default author, they have an entry (they become the author, see `git_command`) and the App commits (`bot`); else the -/// arguments as they are. -pub fn git_args(state: &Path, person: &str, bot: &str, args: &[String]) -> Vec { +/// git's arguments with Pointman credited as co-author (`COAUTHOR`), and after it the run's client (`client`, from +/// `client_coauthor`), when this is a `commit` someone is credited on (`coauthor`: they become the author, see +/// `git_command`) and the App commits (`bot`); else the arguments as they are. +pub fn git_args(state: &Path, person: &str, client: Option<&str>, bot: &str, args: &[String]) -> Vec { let mut out = args.to_vec(); if let (Some(at), Some(_), false) = (commit_at(args), coauthor(state, person), bot.is_empty()) { - let trailer = format!("Co-authored-by: {COAUTHOR}"); - if !args[at..].iter().any(|a| a == &format!("--trailer={trailer}")) { - out.splice(at + 1..at + 1, ["--trailer".to_string(), trailer]); + let trailers: Vec = + std::iter::once(COAUTHOR).chain(client).map(|who| format!("Co-authored-by: {who}")).collect(); + let mut place = at + 1; + for trailer in trailers { + if !args[at..].iter().any(|a| a == &format!("--trailer={trailer}")) { + out.splice(place..place, ["--trailer".to_string(), trailer]); + place += 2; + } } } out @@ -520,7 +631,12 @@ pub fn git_command(state: &Path, args: &[String], login: Login) -> anyhow::Resul (Ok(n), Ok(e)) if !n.is_empty() && !e.is_empty() => format!("{n} <{e}>"), _ => String::new(), }; - let args = person.as_deref().map(|person| git_args(state, person, &bot, args)).unwrap_or_else(|| args.to_vec()); + // the run's client as a co-author too, when the node says which it is (MB_RUN_CLIENT) and it's named + let client = std::env::var("MB_RUN_CLIENT").ok().and_then(|c| client_coauthor(state, &c)); + let args = person + .as_deref() + .map(|person| git_args(state, person, client.as_deref(), &bot, args)) + .unwrap_or_else(|| args.to_vec()); let exe = std::env::current_exe().context("where this extension's binary is")?; let mut cmd = Command::new(git); if let (Some(at), Some(who)) = (commit_at(&args), person.as_deref().and_then(|p| coauthor(state, p))) { @@ -810,11 +926,12 @@ mod tests { let who = "James <1+jamescalam@users.noreply.github.com>"; let dir = state_with(json!({"james": who})); let bot = "pointman-app[bot] <1+pointman-app[bot]@users.noreply.github.com>"; - let got = git_args(dir.path(), "james", bot, &args("-C /r commit -m hi")); + let got = git_args(dir.path(), "james", None, bot, &args("-C /r commit -m hi")); let trailer = "Co-authored-by: Pointman "; assert_eq!(got, argv(&["-C", "/r", "commit", "--trailer", trailer, "-m", "hi"])); // asked again with the same trailer, it isn't added twice - let again = git_args(dir.path(), "james", bot, &argv(&["commit", &format!("--trailer={trailer}"), "-m", "hi"])); + let again = + git_args(dir.path(), "james", None, bot, &argv(&["commit", &format!("--trailer={trailer}"), "-m", "hi"])); assert_eq!(again, argv(&["commit", &format!("--trailer={trailer}"), "-m", "hi"])); assert_eq!(split_identity(who), Some(("James", "1+jamescalam@users.noreply.github.com"))); } @@ -825,10 +942,10 @@ mod tests { for (person, a) in [("", "commit -m x"), ("scene", "commit -m x"), ("james", "push origin main"), ("james", "log commit")] { - assert_eq!(git_args(dir.path(), person, "bot ", &args(a)), args(a), "{person} {a}"); + assert_eq!(git_args(dir.path(), person, None, "bot ", &args(a)), args(a), "{person} {a}"); } let none = tempfile::tempdir().unwrap(); - assert_eq!(git_args(none.path(), "james", "bot ", &args("commit -m x")), args("commit -m x")); + assert_eq!(git_args(none.path(), "james", None, "bot ", &args("commit -m x")), args("commit -m x")); } #[test] @@ -838,21 +955,171 @@ mod tests { let dir = state_with(json!({"james": who, "sam": other})); let file = shim_dir(dir.path()).join("default-author"); let command = args("commit -m continued"); - assert_eq!(git_args(dir.path(), "", "bot ", &command), command); + assert_eq!(git_args(dir.path(), "", None, "bot ", &command), command); std::fs::write(&file, "james").unwrap(); assert_eq!(coauthor(dir.path(), "").as_deref(), Some(who)); assert_eq!(coauthor(dir.path(), "sam").as_deref(), Some(other)); assert_eq!(coauthor(dir.path(), "unknown"), None); - assert!(git_args(dir.path(), "", "bot ", &command).contains(&format!("Co-authored-by: {COAUTHOR}"))); - assert_eq!(git_args(dir.path(), "unknown", "bot ", &command), command); - assert_eq!(git_args(dir.path(), "", "", &command), command); - assert_eq!(git_args(dir.path(), "", "bot ", &args("push origin main")), args("push origin main")); + assert!(git_args(dir.path(), "", None, "bot ", &command).contains(&format!("Co-authored-by: {COAUTHOR}"))); + assert_eq!(git_args(dir.path(), "unknown", None, "bot ", &command), command); + assert_eq!(git_args(dir.path(), "", None, "", &command), command); + assert_eq!(git_args(dir.path(), "", None, "bot ", &args("push origin main")), args("push origin main")); std::fs::write(&file, "unknown").unwrap(); assert_eq!(coauthor(dir.path(), ""), None); std::fs::write(&file, "").unwrap(); + assert_eq!(coauthor(dir.path(), ""), None, "no machine account known"); + } + + /// The runs' folder as write_shim leaves it: `coauthors.json` and `default-author` as given (None: no file), and + /// this machine's own account when one was verified. + fn state_of(coauthors: Option<&str>, default: Option<&str>, machine: Option<&str>) -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + let shims = shim_dir(dir.path()); + std::fs::create_dir_all(&shims).unwrap(); + for (name, text) in [("coauthors.json", coauthors), ("default-author", default)] { + if let Some(text) = text { + std::fs::write(shims.join(name), text).unwrap(); + } + } + if let Some(who) = machine { + keep_machine_author(dir.path(), who).unwrap(); + } + dir + } + + #[test] + fn with_nothing_configured_the_machines_own_account_is_the_author() { + let me = "James Briggs <35938317+jamescalam@users.noreply.github.com>"; + let user = json!({"login": "jamescalam", "id": 35938317, "name": "James Briggs"}); + assert_eq!(machine_author(&user).as_deref(), Some(me)); + assert_eq!( + machine_author(&json!({"login": "sam", "id": 2, "name": null})).as_deref(), + Some("sam <2+sam@users.noreply.github.com>") + ); + assert_eq!(machine_author(&json!({"login": "x = (Option<&'a str>, Option<&'a str>, Option<&'a str>, [Option<&'a str>; 3]); + #[rustfmt::skip] + let rows: [Row; 13] = [ + // nothing configured: runs no person started credit the machine's account; a person's run that + // isn't mapped stays the App's, the machine's person included (EXT-148) + (Some("{}"), Some(""), Some(me), [Some(me), None, None]), + // the machine's account unknown (no gh, signed out): no one + (Some("{}"), Some(""), None, [None, None, None]), + // `app`: runs no person started are the App's + (Some("{}"), Some(APP_ONLY_AUTHOR), Some(me), [None, None, None]), + // someone mapped: they're credited in their own runs, an unmapped person never becomes anyone + (Some(&with_sam), Some(""), Some(me), [Some(me), None, Some(sam)]), + (Some(&with_sam), Some("sam"), Some(me), [Some(sam), None, Some(sam)]), + // default_author names someone not mapped: no one, not the machine's account + (Some(&with_sam), Some("james"), Some(me), [None, None, Some(sam)]), + // a mapping that isn't one `Name `: no one, not the machine's account + (Some(&bad_sam), Some(""), Some(me), [Some(me), None, None]), + (Some(&bad_sam), Some("sam"), Some(me), [None, None, None]), + // coauthors missing or malformed: fail closed, everywhere + (None, Some(""), Some(me), [None, None, None]), + (Some("not json"), Some(""), Some(me), [None, None, None]), + (Some("null"), Some(""), Some(me), [None, None, None]), + (Some(r#"["sam"]"#), Some(""), Some(me), [None, None, None]), + // default-author missing: fail closed for runs no person started + (Some(&with_sam), None, Some(me), [None, None, Some(sam)]), + ]; + for (coauthors, default, machine, want) in rows { + let dir = state_of(coauthors, default, machine); + for (person, want) in ["", "james", "sam"].into_iter().zip(want) { + let row = format!("{coauthors:?} {default:?} {machine:?}, person {person:?}"); + assert_eq!(coauthor(dir.path(), person).as_deref(), want, "{row}"); + // and the commit's trailer only when someone is credited + let command = args("commit -m x"); + let got = git_args(dir.path(), person, None, "bot ", &command); + assert_eq!(got != command, want.is_some(), "{row}"); + } + } + // a machine file that isn't one identity is no one + let dir = state_of(Some("{}"), Some(""), Some("not an identity")); assert_eq!(coauthor(dir.path(), ""), None); } + #[cfg(unix)] + #[test] + fn the_machines_account_is_forgotten_once_it_cant_be_verified() { + use std::os::unix::fs::PermissionsExt; + let me = "James Briggs <35938317+jamescalam@users.noreply.github.com>"; + let dir = state_of(Some("{}"), Some(""), None); + let gh = dir.path().join("gh"); + let answer = |script: &str| { + std::fs::write(&gh, format!("#!/bin/sh\n{script}\n")).unwrap(); + std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap(); + }; + let signed_in = r#"echo '{"login": "jamescalam", "id": 35938317, "name": "James Briggs"}'"#; + // each failure after a success: the account kept before is gone, and no one is credited + for (why, fails) in [ + ("signed out", "echo 'You are not logged into any GitHub hosts.' >&2; exit 4"), + ("an error", "echo 'HTTP 403: API rate limit exceeded' >&2; exit 1"), + ("not JSON", "echo ''"), + ("no login", r#"echo '{"message": "Bad credentials"}'"#), + ("a login that isn't one", r#"echo '{"login": "x>y", "id": 3}'"#), + ] { + answer(signed_in); + assert_eq!(refresh_machine_author(dir.path(), Some(&gh)).unwrap(), me, "{why}"); + assert_eq!(coauthor(dir.path(), "").as_deref(), Some(me), "{why}"); + answer(fails); + assert!(refresh_machine_author(dir.path(), Some(&gh)).is_err(), "{why}"); + assert!(!machine_author_file(dir.path()).exists(), "{why}"); + assert_eq!(coauthor(dir.path(), ""), None, "{why}"); + } + // no gh at all, and a gh that isn't there + answer(signed_in); + refresh_machine_author(dir.path(), Some(&gh)).unwrap(); + assert!(refresh_machine_author(dir.path(), None).is_err()); + assert_eq!(coauthor(dir.path(), ""), None); + refresh_machine_author(dir.path(), Some(&gh)).unwrap(); + assert!(refresh_machine_author(dir.path(), Some(&dir.path().join("missing"))).is_err()); + assert_eq!(coauthor(dir.path(), ""), None); + // one kept longer than MACHINE_AUTHOR_FOR without being verified again (the extension stopped asking, the + // machine restarted): no one + refresh_machine_author(dir.path(), Some(&gh)).unwrap(); + assert_eq!(coauthor(dir.path(), "").as_deref(), Some(me)); + let old = SystemTime::now() - MACHINE_AUTHOR_FOR - std::time::Duration::from_secs(1); + std::fs::File::options().write(true).open(machine_author_file(dir.path())).unwrap().set_modified(old).unwrap(); + assert_eq!(coauthor(dir.path(), ""), None); + // and a fresh check believes it again + refresh_machine_author(dir.path(), Some(&gh)).unwrap(); + assert_eq!(coauthor(dir.path(), "").as_deref(), Some(me)); + } + + #[test] + fn the_runs_client_is_a_co_author_after_pointman() { + let who = "James <1+j@users.noreply.github.com>"; + let dir = state_with(json!({"james": who})); + let clients = json!({"claude": "Claude ", "bad": "no email"}); + std::fs::write(shim_dir(dir.path()).join("client-coauthors.json"), clients.to_string()).unwrap(); + let claude = client_coauthor(dir.path(), "claude"); + assert_eq!(claude.as_deref(), Some("Claude ")); + assert_eq!(client_coauthor(dir.path(), "codex"), None); + assert_eq!(client_coauthor(dir.path(), "bad"), None); + assert_eq!(client_coauthor(dir.path(), ""), None); + let ours = "Co-authored-by: Pointman "; + let theirs = "Co-authored-by: Claude "; + let got = git_args(dir.path(), "james", claude.as_deref(), "bot ", &args("commit -m hi")); + assert_eq!(got, argv(&["commit", "--trailer", ours, "--trailer", theirs, "-m", "hi"])); + // neither twice, and nothing on a commit nobody is credited on + let again = argv(&["commit", &format!("--trailer={theirs}"), "-m", "hi"]); + let got = git_args(dir.path(), "james", claude.as_deref(), "bot ", &again); + assert_eq!(got, argv(&["commit", "--trailer", ours, &format!("--trailer={theirs}"), "-m", "hi"])); + assert_eq!( + git_args(dir.path(), "scene", claude.as_deref(), "bot ", &args("commit -m x")), + args("commit -m x") + ); + // and no client file at all: the client is no one + assert_eq!(client_coauthor(tempfile::tempdir().unwrap().path(), "claude"), None); + } + fn args(s: &str) -> Vec { s.split_whitespace().map(str::to_string).collect() } @@ -1171,9 +1438,9 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let gh = dir.path().join("gh"); std::fs::write(&gh, "").unwrap(); - write_shim(Path::new("/e/github"), dir.path(), Some(&gh), &json!({}), "").unwrap(); + write_shim(Path::new("/e/github"), dir.path(), Some(&gh), &json!({}), "", &json!({})).unwrap(); assert_eq!(machine_cli(dir.path()), Some(gh)); - write_shim(Path::new("/e/github"), dir.path(), None, &json!({}), "").unwrap(); + write_shim(Path::new("/e/github"), dir.path(), None, &json!({}), "", &json!({})).unwrap(); assert_eq!(machine_cli(dir.path()), None); } diff --git a/src/main.rs b/src/main.rs index 0d0c772..e6f8d3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,11 +102,35 @@ fn write_shim(h: &Handle) -> anyhow::Result<()> { cli(h).as_deref(), &h.settings()["coauthors"], h.settings()["default_author"].as_str().unwrap_or_default(), + &h.settings()["client_coauthors"], ) .context("couldn't write the runs' gh")?; Ok(()) } +/// How often this machine's own GitHub account is asked for again (`keep_machine_author`), well within +/// app::MACHINE_AUTHOR_FOR, the time after which an account that wasn't asked for again credits no one. +const MACHINE_AUTHOR_EVERY: Duration = Duration::from_secs(300); + +/// This machine's own GitHub account, for the runs' git to credit in runs no person started (app::coauthor): asked of +/// this machine's own `gh` login when `asked` is over MACHINE_AUTHOR_EVERY ago. One that can't be verified now (signed +/// out, an error) is removed, so its agents' commits stay the App's until it can be. +fn keep_machine_author(h: &Handle, asked: &mut Option, had: &mut Option) { + if asked.is_some_and(|at| at.elapsed() < MACHINE_AUTHOR_EVERY) { + return; + } + *asked = Some(Instant::now()); + let got = app::refresh_machine_author(&state(h), cli(h).as_deref()); + // said once each time it changes, not every few minutes + if *had != Some(got.is_ok()) { + match &got { + Ok(who) => h.log(format!("commits in runs no person started credit this machine's own account, {who}")), + Err(e) => h.log(format!("this machine's own GitHub account isn't known, so no one is credited: {e:#}")), + } + } + *had = Some(got.is_ok()); +} + /// What the board said, for runs to read: its App and where it's installed, and a fresh token for each of those. fn keep_tokens(h: &Handle, app: Option<&App>) { let state = state(h); @@ -200,10 +224,12 @@ fn main() -> anyhow::Result<()> { } // the runs' gh, and what the board says about Pointman App with its tokens, kept fresh for runs (MB-707) let (h, b) = (h.clone(), b.clone()); + let (mut asked, mut had) = (None, None); std::thread::spawn(move || loop { if let Err(e) = write_shim(&h) { h.log(format!("{e:#}")); } + keep_machine_author(&h, &mut asked, &mut had); if let Some(said) = check_app(&h, &b, false) { keep_tokens(&h, said.as_ref()); } diff --git a/tests/github.rs b/tests/github.rs index ab91546..c49d200 100644 --- a/tests/github.rs +++ b/tests/github.rs @@ -128,7 +128,8 @@ fn configured_author_survives_automatic_wakes_but_never_replaces_another_person( let node = StandIn::new(env!("CARGO_MANIFEST_DIR")) .binary(env!("CARGO_BIN_EXE_github")) .settings(json!({"cli": fake_gh().to_string_lossy(), "default_author": "james", - "coauthors": {"james": "James ", "sam": "Sam "}})) + "coauthors": {"james": "James ", "sam": "Sam "}, + "client_coauthors": {"claude": "Claude "}})) .method("core.call", board) .start() .unwrap(); @@ -145,6 +146,7 @@ fn configured_author_survives_automatic_wakes_but_never_replaces_another_person( command .env("PATH", std::env::join_paths([shims.as_path(), dir.path(), std::path::Path::new("/bin")]).unwrap()) .env("MB_RUN_PERSON", person) + .env("MB_RUN_CLIENT", "claude") .env_remove("MB_RUN_PERSON_FILE"); if let Some(file) = file { command.env("MB_RUN_PERSON_FILE", file); @@ -160,6 +162,8 @@ fn configured_author_survives_automatic_wakes_but_never_replaces_another_person( assert!(output.contains("author:James "), "{output}"); assert!(output.contains("committer:pointman-app[bot]"), "{output}"); assert!(output.contains("arg:Co-authored-by: Pointman "), "{output}"); + // the run's client after Pointman (EXT-148) + assert!(output.contains("arg:Co-authored-by: Claude "), "{output}"); } assert!(run("sam", None).contains("author:Sam ")); for output in [run("unknown", None), run("james", Some(&dir.path().join("missing")))] {