From cba85749cc62449a4e1277fc4c0f4075ed558bb1 Mon Sep 17 00:00:00 2001 From: Randy Fay Date: Tue, 24 Nov 2026 16:56:42 +0000 Subject: [PATCH] feat(runner): commit as the session's GitHub account, not Claude MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Short Summary (TL;DR) Commits that a self-hosted runner session makes now name the GitHub account of the person who started the session. A SessionStart hook looks the account up through the session's git proxy, and `Claude ` is only the fallback. ## The Issue The runner deletes `~/.gitconfig` before every session, so the module sets the git identity in `/etc/gitconfig`. That identity is `Claude `, so every commit from a runner session names Claude, not the person who did the work. Before the runner, sessions committed with the workspace owner's own identity. ## How This PR Solves The Issue The new `claude-session-identity` script runs as a SessionStart hook, next to `claude-checkout-check` in the managed settings drop-in. The session's git proxy adds the GitHub credential of the person who started the session, so the hook asks that proxy for GitHub's `/user`, as the session's `gh` shim does. It writes `GIT_AUTHOR_*` and `GIT_COMMITTER_*` to `$CLAUDE_ENV_FILE`, which Claude Code applies to the session's Bash commands. - Name: the account's profile name, or its login. - Email: the account's public email, or `+@users.noreply.github.com`. GitHub credits both to the account. - The hook never uses the workspace's own `gh` login, because that login belongs to the workspace owner, whoever started the session. - When the lookup fails, the hook writes nothing, and commits keep the `/etc/gitconfig` identity. Other identity sources lost. The claude.ai email in the session token is a login, not a git identity, and it can be a shared role account. A key or signing setup on the workspace would let every session act as one person. Commits stay unsigned. The README and docs say to re-sign a branch on your own computer when a repository requires signed commits. ## Manual Testing Instructions Use a freeform workspace with "Run a Claude Code self-hosted runner" on, from a template pushed from this branch. 1. Restart the workspace, then start a session on claude.ai/code in its environment, with a repository attached. 2. Ask Claude to run `env | grep -E '^GIT_(AUTHOR|COMMITTER)_'`. Expect your GitHub profile name and public email, or your `users.noreply.github.com` address. 3. Ask Claude to commit a small change on a branch and push it. On GitHub, expect the commit to show your avatar and login, not Claude's. 4. In the workspace, run `grep claude-session-identity /etc/claude-code/managed-settings.d/coder-runner.json`. Expect the hook command. 5. Check the fallback: run `CLAUDE_ENV_FILE=~/tmp/env.sh GIT_CONFIG_GLOBAL=/dev/null ~/.local/bin/claude-session-identity`. Expect the "commits use the default git identity" message, and expect `~/tmp/env.sh` to stay empty. ## Automated Testing Overview `freeform/tests/validate.tftest.hcl` now checks that the startup script installs `claude-session-identity` and adds it to the SessionStart hooks. Claude Code ran `terraform fmt -recursive`, `make validate` and `make test-templates` (all pass), markdownlint on the changed docs, and `shellcheck` on the script, all on Linux. In a runner session on coder.ddev.com, Claude Code ran the script against the real proxy: it found the right account, wrote the env file, and wrote nothing without an env file or a proxy. Nobody has confirmed yet that the `$CLAUDE_ENV_FILE` exports reach Bash commands in a new runner session, so step 2 above is that check. The strict Zensical docs build was not run. ## Release/Deployment Notes Restart runner workspaces after pushing the template, so they install the hook. The hook relies on two details of Anthropic's git proxy: the `proxy-injected` placeholder token and the `ccr/ca-bundle.crt` path. If either changes, commits fall back to the Claude identity. 🤖 Developed with assistance from [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UrTUMzWrupgmEe5ZjBfY2J --- docs/content/workspaces/ai-tools.md | 1 + .../claude-self-hosted-runner/files/README.md | 18 +++++ .../files/claude-session-identity | 73 +++++++++++++++++++ .../claude-self-hosted-runner/files/gitconfig | 3 + .../modules/claude-self-hosted-runner/main.tf | 15 +++- .../claude-self-hosted-runner/outputs.tf | 2 + freeform/tests/validate.tftest.hcl | 8 ++ .../claude-self-hosted-runner/files/README.md | 18 +++++ .../files/claude-session-identity | 73 +++++++++++++++++++ .../claude-self-hosted-runner/files/gitconfig | 3 + modules/claude-self-hosted-runner/main.tf | 15 +++- modules/claude-self-hosted-runner/outputs.tf | 2 + 12 files changed, 223 insertions(+), 8 deletions(-) create mode 100755 freeform/modules/claude-self-hosted-runner/files/claude-session-identity create mode 100755 modules/claude-self-hosted-runner/files/claude-session-identity diff --git a/docs/content/workspaces/ai-tools.md b/docs/content/workspaces/ai-tools.md index 2ce0e72..647a4b3 100644 --- a/docs/content/workspaces/ai-tools.md +++ b/docs/content/workspaces/ai-tools.md @@ -122,6 +122,7 @@ On every start, the runner runs in the `tmux` session `claude-runner` (`tmux att - **Use a workspace only for the runner.** The runner rewrites `~/.gitconfig` and `~/.config/git` before every session. It can't be turned on together with remote control. - **Concurrent sessions** (1 to 4) sets how many sessions run at once. They share the workspace's Docker daemon and DDEV, and a skill installed in `~/.claude/skills/coder-shared-workspace` tells them how to stay out of each other's way. For full isolation, use several workspaces with one session each; the same environment secret works in all of them (see [Who can use the runner](#who-can-use-the-runner)). - **Checkouts are cleaned between sessions.** A runner keeps its clones under `~/workspace/runner-`. When a session ends, including when the runner releases it after 30 idle minutes, `claude-runner-sweep` cleans that runner's directory, and it runs again before each runner start. Uncommitted changes become `runner-rescue` stashes in the clone. The runner clears the stash list between sessions, so each rescue also gets a ref: list them with `git for-each-ref refs/runner-rescue` and restore one with `git stash apply `. Worktrees are removed. Extra clones a session made are deleted if everything in them is pushed, and otherwise moved to `~/.local/state/claude-runner-rescue/`. Everything the sweep does is logged in `~/.local/state/claude-runner-rescue/log`, and rescues are kept for 30 days. If a checkout is still dirty when a session starts, a hook tells Claude to stash the leftovers rather than work around them. +- **Commits name the person who started the session**, not Claude. A SessionStart hook looks up the GitHub account connected to their claude.ai account, the one the session's HTTPS pushes act as, and uses its profile name and its public email, or its `users.noreply.github.com` address when the email is private. If the lookup fails, commits are `Claude `. Commits are not signed: where a repository requires signed commits, re-sign the branch on your own computer with `git rebase --exec 'git commit --amend --no-edit -n -S' origin/main` and force-push. - **Pushing to your fork.** Runner and remote-control sessions use the workspace SSH agent: your own agent when you have one forwarded (VS Code, `coder ssh -A`), otherwise your Coder key. Add your fork as an SSH remote, such as `git@github.com:/`. If you don't forward an agent, add the key that `coder publickey` prints to your GitHub account. The runner adds GitHub's host keys to `~/.ssh/known_hosts`. SSH pushes act as the workspace owner, whoever started the session; see [Who acts on GitHub](#who-acts-on-github). ### Who acts on GitHub diff --git a/freeform/modules/claude-self-hosted-runner/files/README.md b/freeform/modules/claude-self-hosted-runner/files/README.md index 5378d78..31a2121 100644 --- a/freeform/modules/claude-self-hosted-runner/files/README.md +++ b/freeform/modules/claude-self-hosted-runner/files/README.md @@ -62,6 +62,24 @@ takes sessions from anyone who can start sessions in the environment, and runs them as you, with your SSH agent and Coder key. Don't share the environment with other people. +## Commit identity + +Commits a session makes name the GitHub account connected to the claude.ai +account that started it, the same account its HTTPS pushes act as. A +SessionStart hook, `~/.local/bin/claude-session-identity`, asks GitHub for +that account through the session's git proxy and sets `GIT_AUTHOR_*` and +`GIT_COMMITTER_*`: the account's profile name, and its public email or else +its `users.noreply.github.com` address. It never uses this workspace's own +`gh` login. If the lookup fails, commits fall back to +`Claude ` from `/etc/gitconfig`. + +Commits are not signed. If a repository requires signed commits, re-sign +the branch on your own computer and force-push: + +```bash +git rebase --exec 'git commit --amend --no-edit -n -S' origin/main +``` + ## Troubleshooting - A session fails before Claude starts, and the log shows diff --git a/freeform/modules/claude-self-hosted-runner/files/claude-session-identity b/freeform/modules/claude-self-hosted-runner/files/claude-session-identity new file mode 100755 index 0000000..8979305 --- /dev/null +++ b/freeform/modules/claude-self-hosted-runner/files/claude-session-identity @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# SessionStart hook for Claude Code self-hosted runner sessions: commits name +# the GitHub account connected to the claude.ai account that started the +# session, not Claude. +# +# With --use-anthropic-git-proxy, the session's git reaches github.com through +# a local proxy that adds that account's credential. This asks the same proxy +# for GitHub's /user, the way the session's gh shim does, and writes +# GIT_AUTHOR_* and GIT_COMMITTER_* to $CLAUDE_ENV_FILE, which Claude Code +# applies to the session's Bash commands: +# name: the account's profile name, else its login +# email: the account's public email, else +@users.noreply.github.com +# Both are tied to that GitHub account, so commits are credited to it. +# +# Never uses the machine's own gh login, which belongs to the runner's owner, +# not to whoever started the session. When the lookup fails it changes +# nothing, and commits fall back to the identity in the system git config. +# +# Nothing is signed. Sign on your own computer before opening a PR. +set -u + +log() { echo "claude-session-identity: $*; commits use the default git identity" >&2; } + +if [ -z "${CLAUDE_ENV_FILE:-}" ]; then + log "no CLAUDE_ENV_FILE" + exit 0 +fi +proxy=$(git config --global --get http.https://github.com/.proxy 2>/dev/null) +ca="${CLAUDE_CONFIG_DIR:-}/ccr/ca-bundle.crt" +if [ -z "$proxy" ] || [ ! -r "$ca" ]; then + log "no Anthropic git proxy in this session" + exit 0 +fi + +user="" +for _ in 1 2 3; do + if user=$(curl -fsS --max-time 10 -x "$proxy" --cacert "$ca" \ + -H 'Authorization: token proxy-injected' \ + -H 'Accept: application/vnd.github+json' \ + https://api.github.com/user 2>/dev/null); then + break + fi + user="" + sleep 1 +done +if [ -z "$user" ]; then + log "GitHub /user through the session proxy failed" + exit 0 +fi + +login=$(jq -r '.login // empty' <<<"$user") +id=$(jq -r '.id // empty' <<<"$user") +name=$(jq -r '.name // empty' <<<"$user") +email=$(jq -r '.email // empty' <<<"$user") +case "$id" in + '' | *[!0-9]*) + log "unexpected /user response" + exit 0 + ;; +esac +if [ -z "$login" ]; then + log "unexpected /user response" + exit 0 +fi +name="${name:-$login}" +email="${email:-$id+$login@users.noreply.github.com}" + +{ + printf 'export GIT_AUTHOR_NAME=%q GIT_AUTHOR_EMAIL=%q\n' "$name" "$email" + printf 'export GIT_COMMITTER_NAME=%q GIT_COMMITTER_EMAIL=%q\n' "$name" "$email" +} >>"$CLAUDE_ENV_FILE" +# SessionStart output reaches Claude as context. +echo "Git commits in this session are authored as $name <$email>, from the GitHub account (@$login) connected to whoever started the session." diff --git a/freeform/modules/claude-self-hosted-runner/files/gitconfig b/freeform/modules/claude-self-hosted-runner/files/gitconfig index a24da96..4d0083d 100644 --- a/freeform/modules/claude-self-hosted-runner/files/gitconfig +++ b/freeform/modules/claude-self-hosted-runner/files/gitconfig @@ -1,6 +1,9 @@ # Installed by the claude-self-hosted-runner module. The runner deletes # ~/.gitconfig and ~/.config/git before every session, so session git # settings live here instead. +# A SessionStart hook, claude-session-identity, sets GIT_AUTHOR_* and +# GIT_COMMITTER_* to the GitHub account of whoever started the session. This +# identity is only the fallback for when that lookup fails. [user] name = Claude email = noreply@anthropic.com diff --git a/freeform/modules/claude-self-hosted-runner/main.tf b/freeform/modules/claude-self-hosted-runner/main.tf index b2f0ff3..586583b 100644 --- a/freeform/modules/claude-self-hosted-runner/main.tf +++ b/freeform/modules/claude-self-hosted-runner/main.tf @@ -49,10 +49,17 @@ locals { SessionStart = [{ # Not "compact" or "clear": by then the changes are the session's own. matcher = "startup|resume" - hooks = [{ - type = "command" - command = "/home/coder/.local/bin/claude-checkout-check ${var.base_dir}" - }] + hooks = [ + { + type = "command" + command = "/home/coder/.local/bin/claude-checkout-check ${var.base_dir}" + }, + { + # Commits name the GitHub account of whoever started the session. + type = "command" + command = "/home/coder/.local/bin/claude-session-identity" + }, + ] }] } }) diff --git a/freeform/modules/claude-self-hosted-runner/outputs.tf b/freeform/modules/claude-self-hosted-runner/outputs.tf index a688eb1..b13d4ca 100644 --- a/freeform/modules/claude-self-hosted-runner/outputs.tf +++ b/freeform/modules/claude-self-hosted-runner/outputs.tf @@ -42,6 +42,8 @@ output "startup_script" { chmod +x ~/.local/bin/claude-checkout-check echo '${base64encode(file("${path.module}/files/claude-runner-sweep"))}' | base64 -d > ~/.local/bin/claude-runner-sweep chmod +x ~/.local/bin/claude-runner-sweep + echo '${base64encode(file("${path.module}/files/claude-session-identity"))}' | base64 -d > ~/.local/bin/claude-session-identity + chmod +x ~/.local/bin/claude-session-identity sudo mkdir -p /etc/claude-code/managed-settings.d echo '${base64encode(local.managed_settings)}' | base64 -d | sudo tee /etc/claude-code/managed-settings.d/coder-runner.json >/dev/null mkcert -install >/dev/null 2>&1 || true diff --git a/freeform/tests/validate.tftest.hcl b/freeform/tests/validate.tftest.hcl index ce6a3c2..bef1d4d 100644 --- a/freeform/tests/validate.tftest.hcl +++ b/freeform/tests/validate.tftest.hcl @@ -309,6 +309,14 @@ run "claude_runner_enabled" { condition = strcontains(base64decode(regex("echo '([A-Za-z0-9+/=]+)' \\| base64 -d > ~/.local/bin/claude-runner-loop", coder_agent.main.startup_script)[0]), "--hooks-dir \"$hooks\"") error_message = "each runner must get a hooks dir whose post-session hook sweeps its checkouts" } + assert { + condition = strcontains(base64decode(regex("echo '([A-Za-z0-9+/=]+)' \\| base64 -d \\| sudo tee /etc/claude-code/managed-settings.d/coder-runner.json", coder_agent.main.startup_script)[0]), "/home/coder/.local/bin/claude-session-identity") + error_message = "startup script must install the SessionStart hook that sets the commit identity to the session's GitHub account" + } + assert { + condition = strcontains(base64decode(regex("echo '([A-Za-z0-9+/=]+)' \\| base64 -d > ~/.local/bin/claude-session-identity", coder_agent.main.startup_script)[0]), "CLAUDE_ENV_FILE") + error_message = "startup script must install claude-session-identity" + } assert { condition = strcontains(base64decode(regex("echo '([A-Za-z0-9+/=]+)' \\| base64 -d > ~/.claude-runner/README.md", coder_agent.main.startup_script)[0]), "~/.claude-runner/environment-secret") error_message = "startup script must create ~/.claude-runner with a README that says where to save the secret" diff --git a/modules/claude-self-hosted-runner/files/README.md b/modules/claude-self-hosted-runner/files/README.md index 5378d78..31a2121 100644 --- a/modules/claude-self-hosted-runner/files/README.md +++ b/modules/claude-self-hosted-runner/files/README.md @@ -62,6 +62,24 @@ takes sessions from anyone who can start sessions in the environment, and runs them as you, with your SSH agent and Coder key. Don't share the environment with other people. +## Commit identity + +Commits a session makes name the GitHub account connected to the claude.ai +account that started it, the same account its HTTPS pushes act as. A +SessionStart hook, `~/.local/bin/claude-session-identity`, asks GitHub for +that account through the session's git proxy and sets `GIT_AUTHOR_*` and +`GIT_COMMITTER_*`: the account's profile name, and its public email or else +its `users.noreply.github.com` address. It never uses this workspace's own +`gh` login. If the lookup fails, commits fall back to +`Claude ` from `/etc/gitconfig`. + +Commits are not signed. If a repository requires signed commits, re-sign +the branch on your own computer and force-push: + +```bash +git rebase --exec 'git commit --amend --no-edit -n -S' origin/main +``` + ## Troubleshooting - A session fails before Claude starts, and the log shows diff --git a/modules/claude-self-hosted-runner/files/claude-session-identity b/modules/claude-self-hosted-runner/files/claude-session-identity new file mode 100755 index 0000000..8979305 --- /dev/null +++ b/modules/claude-self-hosted-runner/files/claude-session-identity @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# SessionStart hook for Claude Code self-hosted runner sessions: commits name +# the GitHub account connected to the claude.ai account that started the +# session, not Claude. +# +# With --use-anthropic-git-proxy, the session's git reaches github.com through +# a local proxy that adds that account's credential. This asks the same proxy +# for GitHub's /user, the way the session's gh shim does, and writes +# GIT_AUTHOR_* and GIT_COMMITTER_* to $CLAUDE_ENV_FILE, which Claude Code +# applies to the session's Bash commands: +# name: the account's profile name, else its login +# email: the account's public email, else +@users.noreply.github.com +# Both are tied to that GitHub account, so commits are credited to it. +# +# Never uses the machine's own gh login, which belongs to the runner's owner, +# not to whoever started the session. When the lookup fails it changes +# nothing, and commits fall back to the identity in the system git config. +# +# Nothing is signed. Sign on your own computer before opening a PR. +set -u + +log() { echo "claude-session-identity: $*; commits use the default git identity" >&2; } + +if [ -z "${CLAUDE_ENV_FILE:-}" ]; then + log "no CLAUDE_ENV_FILE" + exit 0 +fi +proxy=$(git config --global --get http.https://github.com/.proxy 2>/dev/null) +ca="${CLAUDE_CONFIG_DIR:-}/ccr/ca-bundle.crt" +if [ -z "$proxy" ] || [ ! -r "$ca" ]; then + log "no Anthropic git proxy in this session" + exit 0 +fi + +user="" +for _ in 1 2 3; do + if user=$(curl -fsS --max-time 10 -x "$proxy" --cacert "$ca" \ + -H 'Authorization: token proxy-injected' \ + -H 'Accept: application/vnd.github+json' \ + https://api.github.com/user 2>/dev/null); then + break + fi + user="" + sleep 1 +done +if [ -z "$user" ]; then + log "GitHub /user through the session proxy failed" + exit 0 +fi + +login=$(jq -r '.login // empty' <<<"$user") +id=$(jq -r '.id // empty' <<<"$user") +name=$(jq -r '.name // empty' <<<"$user") +email=$(jq -r '.email // empty' <<<"$user") +case "$id" in + '' | *[!0-9]*) + log "unexpected /user response" + exit 0 + ;; +esac +if [ -z "$login" ]; then + log "unexpected /user response" + exit 0 +fi +name="${name:-$login}" +email="${email:-$id+$login@users.noreply.github.com}" + +{ + printf 'export GIT_AUTHOR_NAME=%q GIT_AUTHOR_EMAIL=%q\n' "$name" "$email" + printf 'export GIT_COMMITTER_NAME=%q GIT_COMMITTER_EMAIL=%q\n' "$name" "$email" +} >>"$CLAUDE_ENV_FILE" +# SessionStart output reaches Claude as context. +echo "Git commits in this session are authored as $name <$email>, from the GitHub account (@$login) connected to whoever started the session." diff --git a/modules/claude-self-hosted-runner/files/gitconfig b/modules/claude-self-hosted-runner/files/gitconfig index a24da96..4d0083d 100644 --- a/modules/claude-self-hosted-runner/files/gitconfig +++ b/modules/claude-self-hosted-runner/files/gitconfig @@ -1,6 +1,9 @@ # Installed by the claude-self-hosted-runner module. The runner deletes # ~/.gitconfig and ~/.config/git before every session, so session git # settings live here instead. +# A SessionStart hook, claude-session-identity, sets GIT_AUTHOR_* and +# GIT_COMMITTER_* to the GitHub account of whoever started the session. This +# identity is only the fallback for when that lookup fails. [user] name = Claude email = noreply@anthropic.com diff --git a/modules/claude-self-hosted-runner/main.tf b/modules/claude-self-hosted-runner/main.tf index b2f0ff3..586583b 100644 --- a/modules/claude-self-hosted-runner/main.tf +++ b/modules/claude-self-hosted-runner/main.tf @@ -49,10 +49,17 @@ locals { SessionStart = [{ # Not "compact" or "clear": by then the changes are the session's own. matcher = "startup|resume" - hooks = [{ - type = "command" - command = "/home/coder/.local/bin/claude-checkout-check ${var.base_dir}" - }] + hooks = [ + { + type = "command" + command = "/home/coder/.local/bin/claude-checkout-check ${var.base_dir}" + }, + { + # Commits name the GitHub account of whoever started the session. + type = "command" + command = "/home/coder/.local/bin/claude-session-identity" + }, + ] }] } }) diff --git a/modules/claude-self-hosted-runner/outputs.tf b/modules/claude-self-hosted-runner/outputs.tf index a688eb1..b13d4ca 100644 --- a/modules/claude-self-hosted-runner/outputs.tf +++ b/modules/claude-self-hosted-runner/outputs.tf @@ -42,6 +42,8 @@ output "startup_script" { chmod +x ~/.local/bin/claude-checkout-check echo '${base64encode(file("${path.module}/files/claude-runner-sweep"))}' | base64 -d > ~/.local/bin/claude-runner-sweep chmod +x ~/.local/bin/claude-runner-sweep + echo '${base64encode(file("${path.module}/files/claude-session-identity"))}' | base64 -d > ~/.local/bin/claude-session-identity + chmod +x ~/.local/bin/claude-session-identity sudo mkdir -p /etc/claude-code/managed-settings.d echo '${base64encode(local.managed_settings)}' | base64 -d | sudo tee /etc/claude-code/managed-settings.d/coder-runner.json >/dev/null mkcert -install >/dev/null 2>&1 || true