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
16 changes: 12 additions & 4 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ For a machine that must not get the full wrapper. It resolves its own path the s

Every `lib/*.sh` file assumes `logging.sh` is already sourced. `permissions.sh` and `path-security.sh` are foundational — other modules call their functions.

- **`credentials.sh`** — fetches `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain (service `op-service-account-claude-automation`) and `GH_TOKEN` from the Automation vault via that service account, retrying `op read` with exponential backoff. Runs automatically as a side effect of being sourced (not invoked as a discrete step later in the flow). Both are exported into the wrapper process and inherited by its children, including the agent's own shell — they are absent from the user's login shell but visible to anything the wrapper launches. If the vault fetch fails, `GH_TOKEN` is set to a deliberately invalid sentinel so `gh` fails closed, rather than left unset (which would let `gh` fall back to the keyring OAuth token and its `admin:org` scope). Must be sourced before `secrets-loader.sh`, which depends on `OP_SERVICE_ACCOUNT_TOKEN`.
- **`credentials.sh`** — fetches `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain (service `op-service-account-claude-automation`) and `GH_TOKEN` from the Automation vault via that service account, retrying `op read` with exponential backoff. Runs automatically as a side effect of being sourced (not invoked as a discrete step later in the flow). Both are exported into the wrapper process and inherited by its children, including the agent's own shell — they are absent from the user's login shell but visible to anything the wrapper launches. If the vault fetch fails, `GH_TOKEN` is set to a deliberately invalid sentinel so `gh` fails closed, rather than left unset (which would let `gh` fall back to the keyring OAuth token and its `admin:org` scope). GitHub tokens are loaded only on arich-mac; see "Personal GitHub tokens: arich-mac only" below. Must be sourced before `secrets-loader.sh`, which depends on `OP_SERVICE_ACCOUNT_TOKEN`.
- **`secrets-loader.sh`** — discovers secrets files at two levels (project `.claude/secrets.op`, local `.claude/secrets.local.op`), validates permissions/paths, runs `op inject` to resolve `op://Automation/...` references against the Automation vault via service account. Invoked explicitly as `init_secrets_loader` and `inject_secrets` (see execution flow above).
- **`launch-dir-check.sh`** — `check_launch_dir` warns to stderr (never blocks) when CWD is exactly `$HOME` or `$HOME/Developer` — not subdirectories, which are legitimate git repos — since per-project secrets/pre-launch hooks are skipped there and an unrelated MCP server surface from `~/.claude/.claude.json` loads instead; recommends `--add-dir` for multi-repo work
- **`binary-discovery.sh`** — finds the real `claude` binary in `$PATH` excluding the wrapper itself, validates it isn't world-writable
Expand All @@ -84,6 +84,14 @@ The wrapper execs `claude` through `caffeinate -is`, so the system does not idle

`-i` (prevent idle sleep) holds on battery as well as AC; `-s` is AC-only.

#### Personal GitHub tokens: arich-mac only

Andrew's rule (2026-11-09): no GitHub operation by an agent may act as a person. On any host whose `hostname -s` is not `arich-mac` (any case, exact match), `credentials.sh` loads no `GH_TOKEN`, `GH_TOKEN_NOS` or `GH_TOKEN_TWM`, and `creds_enforce_app_only_gh` removes `GH_TOKEN_NOS`, `GH_TOKEN_TWM`, `GH_TOKEN_SWM`, `GITHUB_TOKEN` and `CLAUDE_WRAPPER_VAULT_VARS`. `bin/claude-wrapper` calls it again just before `exec`, because `inject_secrets` (a `secrets.op` can name `GH_TOKEN`) and the pre-launch hook run after `credentials.sh`. Agents there act on GitHub only as the GitHub Apps, minted per call by the dotfiles gh wrapper.

`GH_TOKEN` is set to the fetch-failed sentinel, not unset: an unset `GH_TOKEN` lets gh and the gh wrapper fall back to the keyring login, which acts as Andrew. The gh wrapper replaces `GH_TOKEN` with the App token on App-routed calls, so those still work.

If `hostname` is missing or fails, the host counts as personal. There is no env-var override; tests put a `hostname` stub on `PATH`. The section below applies to arich-mac only.

#### GitHub token selection

A fine-grained PAT is bound to exactly one GitHub resource owner at creation and cannot be repointed, so one token cannot cover a personal account and two orgs. `credentials.sh` derives the owner from the launch directory's `origin` remote and selects the matching vault item:
Expand Down Expand Up @@ -114,7 +122,7 @@ All secret files (`.op` files) must be owner-only permissions (no group/world).
| `.claude/secrets.op` | Per-project 1Password secrets (committed); references `op://Automation/...` |
| `.claude/secrets.local.op` | Per-project local overrides (gitignored) |

`GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from the Automation vault item matching the launch directory's GitHub owner (see "GitHub token selection" above) — not from a flat file, and not sourced by shell startup.
On arich-mac, `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from the Automation vault item matching the launch directory's GitHub owner (see "GitHub token selection" above) — not from a flat file, and not sourced by shell startup. On other hosts it is only the invalid sentinel.

## Headroom Learned Patterns

Expand Down Expand Up @@ -142,9 +150,9 @@ All secret files (`.op` files) must be owner-only permissions (no group/world).

*~500 tokens/session saved*

- `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from one of two Automation vault items chosen by the launch directory's GitHub owner (`CCCLI-NOS`, or `GitHub - CCCLI` for everything else); no flat token files exist
- On arich-mac only, `GH_TOKEN` is fetched by the wrapper itself, via `lib/credentials.sh`, from one of two Automation vault items chosen by the launch directory's GitHub owner (`CCCLI-NOS`, or `GitHub - CCCLI` for everything else); no flat token files exist. Other hosts get no personal GitHub token
- Per-project secrets live in `.claude/secrets.op` (committed) referencing `op://Automation/...`; resolved by `secrets-loader.sh` via `OP_SERVICE_ACCOUNT_TOKEN` (no TouchID)
- The global `~/.config/claude-code/secrets.op` no longer exists
- `secrets-loader.sh` still loads a global `~/.config/claude-code/secrets.op` when one exists, and at least one personal Mac has one that sets `GH_TOKEN`; off arich-mac the pre-exec strip removes it
- `opp <args>` runs `op` as your personal account (unsets service account token for that subprocess); needed for Personal vault access (e.g. prep-airdrop.sh)

### Pre-commit Hooks
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ cat ~/.ssh/id_ed25519_claude_code.pub

#### GitHub Token

This applies only on the company Mac, arich-mac. On any other host the wrapper
loads no personal GitHub token and removes inherited ones; `GH_TOKEN` holds
only an invalid sentinel, and agents act on GitHub as the GitHub Apps. See
`docs/SECRETS.md`.

The wrapper fetches `GH_TOKEN` itself at launch, via `lib/credentials.sh`. It
reads `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain, then resolves a token
from the 1Password Automation vault. No shell-startup setup is required — if
Expand Down
4 changes: 4 additions & 0 deletions bin/claude-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ if secrets_available; then
fi
fi

# Off arich-mac, remove any personal GitHub token that secrets.op or the
# pre-launch hook exported after credentials.sh ran (see that file).
creds_enforce_app_only_gh

if secrets_available; then
debug_log "Executing: ${CAFF_BIN} -is ${CLAUDE_BIN} [${#RC_ARGS[@]} injected args + ${#} args] with secrets loaded"
else
Expand Down
13 changes: 12 additions & 1 deletion docs/SECRETS.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ DEBUG_MODE=true

### GitHub Token Integration

Only on the company Mac, arich-mac (`hostname -s`, any case), does the wrapper
load a personal GitHub token. On every other host it loads no `GH_TOKEN`,
`GH_TOKEN_NOS` or `GH_TOKEN_TWM`, and it removes those, `GH_TOKEN_SWM` and
`GITHUB_TOKEN` if the session inherits them. It removes them again just
before it starts Claude, so a `secrets.op` entry or a pre-launch hook cannot
put one back. `GH_TOKEN` is left as the invalid sentinel
`invalid-cccli-token-vault-fetch-failed`, not unset: with no `GH_TOKEN`, gh
falls back to the keyring login, which acts as Andrew. Agents there reach
GitHub only as the GitHub Apps, which the dotfiles gh wrapper mints per call.
The rest of this section describes arich-mac.

`GH_TOKEN` is fetched by the wrapper itself at launch, via `lib/credentials.sh`.
It reads `OP_SERVICE_ACCOUNT_TOKEN` from the macOS Keychain, then resolves the
token from the 1Password Automation vault:
Expand Down Expand Up @@ -344,7 +355,7 @@ Output shows which files loaded in order. Last file wins for duplicate variables
| `GIT_COMMITTER_NAME` | `Claude Code Bot` | Git committer |
| `GIT_COMMITTER_EMAIL` | `claude-code@smartwatermelon.github` | Git committer email |
| `GIT_SSH_COMMAND` | `ssh -i ~/.ssh/id_ed25519_claude_code ...` | SSH key for git |
| `GH_TOKEN` | Fetched by wrapper from 1Password Automation vault via `lib/credentials.sh` | GitHub CLI token |
| `GH_TOKEN` | arich-mac: fetched by wrapper from 1Password Automation vault via `lib/credentials.sh`. Other hosts: the invalid sentinel only | GitHub CLI token |

### Loaded from secrets.op

Expand Down
28 changes: 26 additions & 2 deletions lib/credentials.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# rather than left unset, so gh fails closed instead of falling back to the
# keyring OAuth token (which holds admin:org). See _load_gh_token.
#
# GitHub tokens load only on arich-mac; elsewhere see creds_enforce_app_only_gh.
#
# Requires: lib/logging.sh must be sourced first.
# Must be sourced before lib/secrets-loader.sh (which needs OP_SERVICE_ACCOUNT_TOKEN).

Expand Down Expand Up @@ -62,6 +64,14 @@ _CREDS_HAS_TIMEOUT=false
command -v timeout &>/dev/null && _CREDS_HAS_TIMEOUT=true
readonly _CREDS_HAS_TIMEOUT

# Personal GitHub tokens: arich-mac only (CLAUDE.md). hostname is on PATH
# for test stubs; no env override. Unreadable means not arich-mac.
_CREDS_PERSONAL_GH_ALLOWED=false
if [[ "$(hostname -s 2>/dev/null || true)" =~ ^[Aa][Rr][Ii][Cc][Hh]-[Mm][Aa][Cc]$ ]]; then
_CREDS_PERSONAL_GH_ALLOWED=true
fi
readonly _CREDS_PERSONAL_GH_ALLOWED

# =========================================================
# SERVICE ACCOUNT TOKEN
# =========================================================
Expand Down Expand Up @@ -311,11 +321,25 @@ _load_owner_gh_tokens() {
debug_log "Per-owner tokens available: ${loaded}/${#specs[@]}"
}

# Off arich-mac, remove personal GitHub tokens. bin/claude-wrapper calls it
# again before exec: secrets.op and pre-launch run after this file.
creds_enforce_app_only_gh() {
"${_CREDS_PERSONAL_GH_ALLOWED}" && return 0
unset GH_TOKEN_NOS GH_TOKEN_TWM GH_TOKEN_SWM GITHUB_TOKEN "${_CREDS_VAULT_MARKER_VAR}"
# Sentinel, not unset: unset lets gh use the keyring login (Andrew).
export GH_TOKEN="${_CREDS_GH_TOKEN_FETCH_FAILED}"
}

# =========================================================
# MAIN
# =========================================================
_load_service_account_token
_load_gh_token
_load_owner_gh_tokens
if "${_CREDS_PERSONAL_GH_ALLOWED}"; then
_load_gh_token
_load_owner_gh_tokens
else
debug_log "Not arich-mac: no personal GitHub token loaded; gh acts only as the GitHub Apps"
fi
creds_enforce_app_only_gh
# An inherited GH_TOKEN_SWM (an older parent session) must not reach this one.
unset GH_TOKEN_SWM
Loading
Loading