Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/content/workspaces/ai-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<n>`. 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 <ref>`. 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 <noreply@anthropic.com>`. 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:<you>/<repo>`. 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
Expand Down
18 changes: 18 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <noreply@anthropic.com>` 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <id>+<login>@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."
3 changes: 3 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/gitconfig
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 11 additions & 4 deletions freeform/modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
]
}]
}
})
Expand Down
2 changes: 2 additions & 0 deletions freeform/modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions freeform/tests/validate.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <noreply@anthropic.com>` 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
Expand Down
73 changes: 73 additions & 0 deletions modules/claude-self-hosted-runner/files/claude-session-identity
Original file line number Diff line number Diff line change
@@ -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 <id>+<login>@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."
3 changes: 3 additions & 0 deletions modules/claude-self-hosted-runner/files/gitconfig
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 11 additions & 4 deletions modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
]
}]
}
})
Expand Down
2 changes: 2 additions & 0 deletions modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading