Skip to content

feat(nix): mint the agents' GitHub App token in dotfiles - #532

Merged
furedea merged 13 commits into
mainfrom
feat/mint-agent-github-token
Dec 1, 2026
Merged

furedea merged 13 commits into
mainfrom
feat/mint-agent-github-token

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

概要

furedea/agent-harness#264 でエージェント用 GitHub トークンの発行機能が agent-harness から削除されたため,発行を dotfiles に移した.

  • scripts/github-app-token/mint_github_app_token.sh(新規): openssl,curl,jq でトークンを発行する.jq は既存の依存.
    • App の鍵で RS256 の JWT に署名する.
    • GET /app/installations で App のインストールが 1 つだけであることを確かめ,POST /app/installations/{id}/access_tokens でトークンを発行する.
    • <設定ディレクトリ>/hosts.yml を一時ファイルからの rename で mode 0600 で書く.設定ディレクトリは 0700.config.yml がなければ version: "1" を書く.
    • 失敗したときは今の hosts.yml をそのまま残す.
    • JWT とトークンは標準入力で curl に渡すので,argv にも出力にも出ない.
  • 2 回前のトークンの失効: 置き換える hosts.yml を同じディレクトリの hosts.previous.yml(mode 0600,一時ファイルからの rename)に残す.新しい hosts.yml に置き換えた後,hosts.previous.yml にあったトークン(2 回前の実行で発行したもの)を,そのトークン自身で DELETE /installation/token して失効させる.
    • 有効なトークンは最大 2 つになる.実行の直前に始まった gh run watch のような長い gh も,その次の実行まで(少なくとも 15 分)動き続ける.
    • 漏れたトークンは最大 30 分で失効する(もともと 1 時間で期限切れ).
    • 1 回目と 2 回目の実行は何も失効させない.
    • 発行に失敗したときは hosts.yml も hosts.previous.yml も変えず,何も失効させない.
    • 401 は期限切れなので何も出さない.
    • ほかの失敗はトークンを含めずにログへ出し,実行は成功として終える.
  • 権限の絞り込み: 権限の指定を必須にし,指定がなければ usage で失敗する.App の全権限を持つトークンは発行されない.
  • nix/home/default.nix:
    • programs.agent-harness.github.agentToken を削除した.
    • launchd エージェント agent-github-token を両ホストに追加した(RunAtLoad,15 分ごとに実行,ログは ~/Library/Logs/agent-github-token.log).
    • App ID,鍵のパス,設定ディレクトリ ~/.config/agent-harness/gh は旧版と同じ.
    • Claude の env と Codex の shell_environment_policy.set に GH_CONFIG_DIR を設定した.source の設定は ~ を展開しないので,dotfiles が絶対パスで渡す.
  • flake.lock: agent-harness を 249aeac(0.10.0,feat: render MCP servers declared once in the source agent-harness#268 を含む)に,agent-config を 4ea2075(0.6.0,feat(settings)!: declare the harness protection and the agent GitHub policy agent-config#286 を含む)に一緒に更新した.
    • feat: render MCP servers declared once in the source agent-harness#268 は,ソースの mcp_servers.json と Home Manager の mcpServers に宣言した MCP サーバーを,各プロバイダーの設定に生成する.
    • dotfiles と agent-config は MCP サーバーを宣言していないので,生成されるのはサーバーを含まない空のファイルだけ.darwin-rebuild 後に Mac に現れるのは次の 2 つ.
      • ~/.gemini/config/mcp_config.json: 新しい activation agentHarnessAntigravityMcpServers が,なければ作る.
      • ~/.gemini/config/.agent-harness-managed.json: そのサイドカー({"mcpServers": []}).
    • 既存の ~/.codex/.agent-harness-managed.json に空の mcp_servers が加わる.Codex の config.toml に [mcp_servers] は増えず,Claude の skills ツリーのファイル構成も変わらない.Devin は有効にしていないので ~/.config/devin には何も書かれない.
    • agent-config 側の差分は 0.6.0 のリリースと依存の更新(hypothesis,ruff).
  • nix/checks.nix:
    • 新しい agent-harness は composedSettings を公開しないので,モジュールと同じ評価(source の設定に Home Manager の settings を recursiveUpdate で重ねる)で生成後の設定を組み立てるようにした.
    • 契約を追加した.
      • 旧 github オプションが削除済みであること
      • 両ホストのジョブの引数(App ID,鍵,設定ディレクトリ,権限)
      • 実行間隔が 1 時間の有効期限の 1/3 未満であること
      • Claude と Codex の GH_CONFIG_DIR
      • 組み込んだスクリプトが実行できること(darwinOnly)
    • Claude,Codex,Antigravity のそれぞれで鍵が読み取り拒否されていることを確かめる契約を追加した.
      • Claude: Read(...) と denyRead の両方
      • Codex: default_permissions のプロファイルの filesystem に "deny"
      • Antigravity: read_file(...)
      • 鍵そのものに加え,~/.ssh などの親ディレクトリの拒否も鍵を含むものとして扱う.
      • 設定を生成するプロバイダーが増えたら失敗する契約もある.
  • ADR-0055 を追加し,README を更新した.

Closes #531

権限の選び方

GET /apps/furedea-agent で調べた App の権限は actions:read,contents:write,issues:write,metadata:read,pull_requests:write だった.エージェントはどれも使うので,5 つすべてを明示して要求する.

権限 使い道
actions:read CI の確認
contents:write push
issues:write Issue
pull_requests:write PR
metadata:read 常に必要

絞り込みの効果は,後から App に権限を足しても,dotfiles に書き足すまでエージェントには渡らないこと.リポジトリは絞らなかった.インストールが所有者の全リポジトリを対象にしていて,エージェントは複数のリポジトリにまたがって作業するため.

マージ後の手順(各 Mac で)

トークンは表示しない.

  1. sudo darwin-rebuild switch --flake ~/ghq/github.com/furedea/dotfiles#<host> で反映する.
  2. launchd エージェントが動いたことを確かめる.
    • launchctl print gui/$(id -u)/org.nix-community.home.agent-github-token | grep -E 'state|last exit code' で last exit code = 0 になっている.
    • ~/Library/Logs/agent-github-token.log にエラーがない.
  3. エージェントの身元で gh が動くことを確かめる.
    • GH_CONFIG_DIR=~/.config/agent-harness/gh gh api /installation/repositories --jq .total_count が数を返す.
    • stat -f %Lp ~/.config/agent-harness/gh/hosts.yml が 600 を返す.
  4. 新しく起動した Claude Code または Codex のセッションで gh pr list --repo furedea/dotfiles が動く.
  5. 旧版の状態ファイル ~/.config/agent-harness/gh/agent_harness_token.json は不要なので消してよい.

検証

  • bats tests/github-app-token: 22 件すべて成功.スクリプトを作る前,失効と権限を足す前,2 回前の失効に変える前の各段階で,テストが先に失敗することを確認した.
  • nix flake check: 成功(all checks passed!).
    • lock 更新前は次の 4 件が失敗し,更新後に通ることを確認した.
      • agent-harness-github-option-removed(旧 agent-harness-has-no-github-option)
      • github-app-key-unreadable-by-claude,-codex,-antigravity
    • GH_CONFIG_DIR の 2 件は,dotfiles で設定した時点で通った.
  • bats --recursive tests(137 件): 今回の変更による失敗はなし.ローカルのサンドボックスで zsh の ssh-agent の 8 件と nvim の lspmux の 1 件が失敗したが,原因は Unix ソケットの listen と ~/.local/state/nvim への書き込みの拒否で,今回の変更とは関係ない.
  • shellcheck,shfmt,lefthook,commitlint: 成功.
  • Nix のチェック agent-github-token-script(darwinOnly): bats の仕様を,launchd ジョブと同じ固定ツール(nix/agent_github_token.nix,curl のみ偽物)で実行し,22 件すべて成功.
  • CI: codex-review と,それに連動する all-green 以外はすべて成功.codex-review は利用上限で実行されていない(Codex のコメントで確認).
  • 実際の鍵とトークンは,読み取りも表示もしていない.テストは使い捨ての鍵と偽の curl を使う.

レビュー対応

  • P1(Claude): launchd ジョブの TOOLS_PATH に sed がなく,2 回目以降の実行が exit 127 で失敗してトークンが更新されなくなる.
    • 29e11c7 で修正した.前のトークンは bash の正規表現で読むようにした.
    • bats の仕様をジョブと同じ固定ツールで実行する Nix チェックを足した.修正前のスクリプトで,このチェックが sed: command not found で失敗することを確認した.
  • P2(Claude): 前のトークンをすぐ失効させるため,次の実行より長く動く gh(gh run watch など)が 401 になる.
    • 所有者の判断で,2 回前のトークンを失効させる方式に変えた(ae6e9e1).有効なトークンは最大 2 つ,漏れたトークンは最大 30 分で失効する.
    • ADR-0055 の Decision と Consequences を更新した(b4fecc3).
  • P3(Claude): Claude Code の Read(/path) はプロジェクトからの相対パスとして読まれるので,鍵の契約が保護にならないエントリーを通しうる.
    • 398e377 で修正した.Read( の絶対パスは // で始まるものだけを数える.
  • P3(Claude,変更なし): revoke の対象が hosts.yml の値をそのまま使う点.指摘自身が問題なしとしている.エージェントがそのディレクトリに書けても,送られるのはエージェントがすでに持つトークンだけ.
  • P3(Claude,変更なし): checks は agent-harness の合成を再現していて,生成結果そのものは読んでいない.生成結果を読むには評価時のビルド(IFD)が要るので,今の形にとどめた.agent-harness の合成方法が変わったときは見直す.
  • 最終の再レビュー(Claude): P0〜P2 の指摘なし.未解決のスレッドなし.
  • 2 回前の失効と lock 更新後の再レビュー(Claude): P0〜P2 の指摘なし.P3 として,PR 本文の失効方式と lock のリビジョンが古いと指摘された.push 直後に本文を更新しており,どちらも反映済み.
  • Codex: 利用上限のため 2 回とも実行されていない.
  • CI の bats で,GNU の stat -f によって mode の確認がずれていた.テストの補助関数で stat -c を先に試すように直した.

計画からの逸脱

  • JSON の読み取りに既存の jq を使った.インストール一覧の応答には入れ子の id があり,sed では確実に読めないため.
  • 期限を追跡せず,毎回発行する.状態は前の hosts.yml だけで済む代わりに,1 台あたり毎時 4 回の発行と失効になる(ADR-0055).
  • nix/checks.nix で agent-harness の合成を再現した.新しい agent-harness が composedSettings を公開しなくなったため.

🤖 Generated with Claude Code

@furedea

furedea commented Nov 28, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-12-01T03:52:13.952039Z
We couldn’t complete this review. Comment @codex review to try again.
775a3d0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Comment thread nix/home/default.nix Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Review summary

One blocking finding.

  • P1 (nix/home/default.nix): the launchd wrapper limits PATH to coreutils, curl, gnugrep, jq and openssl, but the script calls sed in current_token. The first run succeeds because no hosts.yml exists yet. Every later run exits 127 before hosts.yml is rewritten, so the one-hour token expires and is never refreshed. The bats tests and the --help Nix check both miss it, because the tests keep the host PATH in TOOLS_PATH.

I found nothing else worth flagging. The permission scoping, atomic 0600 write, stdin-passed credentials and the key-deny checks look right.

@furedea

furedea commented Nov 29, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

Copy link
Copy Markdown
Contributor

Review summary: no P0–P2 findings.

I read the full diff (the mint script, the Bats suite and helper, the Nix wiring, nix/checks.nix, ADR-0055 and the README) and found nothing that should block the merge. I did not run the test suite or nix flake check.

What I checked:

  • Script error handling: set -euo pipefail and inherit_errexit stop a failed openssl, curl or jq inside $(...) from producing a partial JWT or token. The usage guard short-circuits before $1 is read, so set -u is safe with fewer than 4 arguments.
  • Ordering: the previous token is read from hosts.yml before the new file is written, and revoked only after the mv. The mv replaces the file atomically, and the temp-file trap covers an early exit. Revoke failures are logged without the token and do not fail the run.
  • Secrets: the JWT and tokens go to curl through --header @-, so they stay out of argv. The tests check this for both the JWT and the revoked token.
  • Permissions: the permission arguments are validated against ^[a-z_]+=(read|write|admin)$ before they reach jq. A missing permission list fails with usage, so a token never gets every App permission.
  • Nix wiring: the script is embedded with writeShellScript and gets TOOLS_PATH from the shared tool list. The job arguments, the interval, and GH_CONFIG_DIR for Claude and Codex are asserted per host. The new script check is inside darwinOnly, which avoids the #!/usr/bin/env bash shebang problem in the Linux Nix sandbox.
  • Key-deny contracts: the logic that decides whether a deny entry covers the key handles the ~/, // and /** spellings. It also treats a parent-directory deny as covering the key.

P3 notes, not inline comments:

  • Revoking the previous token on every 15-minute run means any process that captured the token earlier fails at the next rotation instead of after an hour. Examples are GH_TOKEN=$(gh auth token), a token embedded in a remote URL, or a long git push that straddles the swap. The ADR already records the 4/hour mint-and-revoke cost, but this effect on in-flight users isn't spelled out. If agents are never expected to cache the token, nothing needs to change.
  • The agent-github-token log (~/Library/Logs/agent-github-token.log) has no rotation. It only grows on errors, so this is minor.

@furedea

furedea commented Nov 29, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

Copy link
Copy Markdown
Contributor

Review summary

Read-only review of the new mint_github_app_token.sh, the launchd job, the nix/checks.nix contracts and the Bats specification. I did not run the tests.

Findings

  • P2: Revoking the previous token right after rotation caps the life of an in-memory gh token at 15 minutes, which affects long-running gh run watch and polling commands. Details are in the inline comment.

Checked, no problem found

  • Error handling under set -euo pipefail and inherit_errexit: a failed list, mint or JWT step leaves the existing hosts.yml untouched, and the temp file is cleaned up.
  • Credentials stay out of argv, because headers go to curl on stdin.
  • hosts.yml is written with mode 0600 and the config dir is 0700.
  • The TOOLS_PATH tool set covers every external command the script uses. The new Nix check runs the Bats spec against that pinned set.
  • The key-deny contracts for Claude, Codex and Antigravity look sound.

No P0 or P1 findings.

@furedea

furedea commented Nov 29, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

Copy link
Copy Markdown
Contributor

Review summary

No P0 or P1 findings, and no inline comments. I read the whole diff and did not run the Bats suite or the Nix checks. The review is by reading only.

What I checked

  • mint_github_app_token.sh:
    • The strict-mode and inherit_errexit handling is correct.
    • The JWT is built correctly: base64url, 60 s backdate, 9 min lifetime.
    • The JWT and token only travel to curl on stdin (--header @-), so neither shows up in argv.
    • hosts.yml is written to a temporary file, then renamed, with a 0700 directory and 0600 file. A failed mint leaves the old file in place.
    • The previous token is revoked only after the new hosts.yml is in place. Revoke failures are logged and do not fail the run.
    • The usage check rejects a missing or malformed permission list, so a token with all of the App's permissions cannot be minted.
  • nix/home/default.nix: the launchd job's tools come from a pinned TOOLS_PATH. builtins.readFile of the script inside writeShellScript is safe, because the file's contents are not Nix-interpolated and its shebang becomes a comment.
  • Tests: the Bats suite covers revoke ordering, the 401 and failure paths, permission scoping, and no secrets in argv or output. A second check runs the same suite with PATH limited to the job's tool set, which catches a missing tool before launchd does.

Residual notes, none blocking

  • P3: denyCoversGithubAppKey in nix/checks.nix treats a single-leading-slash Read(/abs/path) entry as an absolute path. I believe Claude Code reads that as project-relative, and only // is absolute. If so, the check could pass on a deny rule that doesn't protect the key. The current source config probably uses ~/ or //, so I'm not flagging it inline. I didn't verify the Claude Code semantics.
  • P3: the revoke step sends whatever oauth_token is in $CONFIG_DIR/hosts.yml to api.github.com as a bearer credential. This is only a concern if the agent sandbox can write that directory, and then the worst case is one request to GitHub with a token the agent already holds. It's acceptable as is.
  • The ADR already documents the trade-off that a long-running gh process loses its token at the next run.

@furedea

furedea commented Nov 29, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

Copy link
Copy Markdown
Contributor

Review summary

No P0–P2 findings. I read the full diff: the mint script, its Bats spec and helpers, the launchd job, the Nix checks, the ADR, and the lock bump. I did not run the tests or nix flake check.

Checked and found sound:

  • Secrets out of argv and output. The JWT and tokens go to curl through --header @- on stdin. The revoke call does the same.
  • Atomic write. hosts.yml is written through mktemp in the 0700 config dir and renamed into place. The EXIT trap cleans up the temp file on failure, and a failed mint leaves the current file untouched.
  • Revoke ordering. The previous token is read before the write and revoked only after the new hosts.yml is in place. 401 is treated as expired, and other revoke failures are logged without failing the run.
  • TOOLS_PATH coverage. The pinned set (coreutils, curl, gnugrep, jq, openssl) covers every external command the script uses. The new agent-github-token-script check runs the Bats spec with the same fixed tools, which guards against the earlier sed exit-127 bug.
  • Permissions. The permission list is required and validated, so a token cannot get all of the App's permissions by omission.
  • Refresh interval. The 15-minute interval is under a third of the 1-hour token lifetime.

P3 (optional, no change needed to merge): nix/checks.nix now rebuilds the Claude, Codex and Antigravity settings itself with recursiveUpdate over the source files. The github-app-key-unreadable-by-* contracts therefore check that reconstruction, not what agent-harness renders. If agent-harness later merges lists differently or adds an overlay, these key-protection checks could pass or fail without reflecting the real output. The github-app-key-covers-every-rendered-provider check only covers the case where a new provider appears. Re-verify against the rendered files if agent-harness's composition changes.

Known trade-off, already recorded in ADR-0055: a long-running gh process that outlives the next 15-minute run gets 401 after its token is revoked.

🤖 Generated with Claude Code

furedea
furedea previously approved these changes Dec 1, 2026
furedea and others added 10 commits December 1, 2026 11:40
Sign an RS256 App JWT, mint a token for the App's only installation,
and replace the gh hosts.yml in the given config dir atomically with
mode 0600, keeping the current file on any failure.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…harness

Add the agent-github-token launchd agent on both Macs, keeping the App ID,
key path and gh config dir, and drop programs.agent-harness.github, which
agent-harness no longer provides. Record the ownership move in ADR-0055.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Request the token with an explicit permission list from the launchd job,
so permissions later added to the App do not reach agents, and revoke the
token of the previous hosts.yml once the new one is in place so at most
one agent token is live. A 401 on revoke means it already expired; other
failures are logged without any token.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude, Codex and Antigravity must each deny reading the GitHub App key,
and a newly rendered provider fails the check until it is covered.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude Code env and Codex shell_environment_policy.set do not expand ~,
so the agent source cannot name the config dir; dotfiles sets it next to
NVIM_LOG_FILE.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
agent-harness 2008c43 drops the github option and composedSettings;
agent-config ac335be (furedea/agent-config#286) denies reading ~/.ssh
for every provider. The checks now compose the provider settings the way
the agent-harness module does, and the old option counts as removed when
reading it fails.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The launchd job's TOOLS_PATH has no sed, so every run after the first
exited 127 before replacing hosts.yml. Read the previous token with bash
instead, and run the Bats specification in a Nix check with only the
job's pinned tools so a missing tool fails there.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… key

Claude Code reads Read(/path) relative to the project, so such an entry
does not protect the key and must not satisfy the contract.

Refs #531

Co-Authored-By: Claude Opus 5.5 <[email protected]>
furedea and others added 2 commits December 1, 2026 11:42
A gh process keeps the token it read at start, so revoking the previous
token cut off a gh run watch that began just before a run. Keeping the
replaced hosts.yml as hosts.previous.yml and revoking the token it held
lets such a process run until the run after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
agent-harness 0.10.0 renders MCP servers declared once in the source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@furedea

furedea commented Dec 1, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review summary

I found no P0, P1 or P2 issues, so I posted no inline comments.

I read the whole diff and traced these paths:

  • Failure ordering in mint_github_app_token.sh. The new token is minted before any file changes. hosts.previous.yml and hosts.yml are replaced by temp-file rename. The revoke only runs after the new hosts.yml is in place. A failed mint or revoke leaves the current config intact, and the tests cover this.
  • Revoke guard. It skips the DELETE when the oldest token equals the previous or new token. That covers the case where a half-written run leaves hosts.previous.yml equal to hosts.yml.
  • Secrets. The JWT and tokens go to curl through --header @-, so they stay out of argv and output. File modes are 0600 and the config dir is 0700.
  • Job tools. nix/agent_github_token.nix provides every external tool the script calls: coreutils, curl, grep, jq and openssl. The agent-github-token-script check runs the bats spec with only those tools, which guards against the sed regression fixed earlier.
  • Key-deny contracts in nix/checks.nix. The matching can only fail closed. A glob or an unrecognised spelling makes the check fail instead of passing silently.

P3 (not tied to a line): the PR description is stale in two places.

  • It says the previous token is revoked immediately, so there is always one valid token. The code, ADR-0055 and README instead revoke the token from two runs ago, so up to two tokens are live (a leaked one for up to about 30 minutes).
  • It names agent-harness 2008c43 and agent-config ac335be. The flake.lock diff pins 249aeac and 4ea2075.

Please update the description so it matches what merges. The owner-facing trade-off is described differently in the description and the ADR.

I did not run the bats suite or nix flake check. This review was read-only.

@furedea
furedea merged commit c933982 into main Dec 1, 2026
21 of 23 checks passed
@furedea
furedea deleted the feat/mint-agent-github-token branch December 1, 2026 08:06
furedea added a commit to furedea/agent-config that referenced this pull request Dec 1, 2026
…guard rely on it (#290)

エージェント用の gh 設定ディレクトリ `~/.config/agent-harness/gh` を全 provider
で書込み禁止にし,gh ガードはそれを根拠に「literal な `--repo` の読み取り + 同じ行の別コマンド」で確認を求めなくなる.
宣言は `harness_roots.json` を一般化した `write_protected_paths.json`(各項目に
`purpose`)に移した.
判断事項: マージ前に下の「dotfiles が満たすべきこと」と「マージ後の確認」.

Refs #289(#282 の続き,#181 の暫定段階)

```mermaid
flowchart LR
  D[write_protected_paths.json] --> C[Claude: Edit deny + sandbox denyWrite]
  D --> X[Codex guarded: read]
  D --> A[Antigravity: write_file deny]
  D --> F[guard_file.py harness: 全 provider]
  D --> G[gh guard]
  G -->|GH_CONFIG_DIR と config.yml / hosts.yml の実体が gh-config 宣言内| R[--repo 読み取りは行の他コマンドで ask しない]
```

| 状況 | 変更前 | 変更後 |
| --- | --- | --- |
| Edit/Write で `~/.config/agent-harness/gh/config.yml` | 通る | 拒否(「ホストの
dotfiles で変える」と案内) |
| `git fetch -q origin && gh issue list --repo o/r`(保護された
`GH_CONFIG_DIR`,Claude / Codex) | ask | allow |
| 同上,Antigravity / Devin / dsh / Hermes / Pi | ask | ask(シェルの書込みを防ぐ
sandbox 規則がない) |
| 同上,`GH_CONFIG_DIR` 未設定・相対・存在しない・宣言外・設定ファイルが宣言外へ link・宣言パス自体が link |
ask | ask |
| 行に `export GH_HOST=…` / `HOME=… gh` / `XDG_CONFIG_HOME=… gh` / `export
HTTPS_PROXY=…` | ask | ask |
| `--repo` なしの読み取り + 別コマンド | ask | ask |

## 検証

- [x] `uv run pytest -q tests`: 6658 passed, 2 skipped(git-lfs と gofmt
が未インストール)
- [x] `ruff check` / `ruff format --check` / `ty check` / `vulture` /
`deptry` / `ls_lint` / `adr-check`: 通過
- [x] Red を確認: 新しいテストは宣言ファイル未作成・新形式未対応・`changes_directory` による ask
で失敗し,実装後に通過
- [x] rebase 後(ruff 0.17.6)に関連テスト 187 件と lint を再実行して通過
- [x] review 対応(1d974ef): claude-review P2「宣言内でも存在しない `GH_CONFIG_DIR`
を信頼する」を修正.Red(新テスト失敗)→ Green,関連 57 件通過
- [x] review 対応(8ea5997): claude-review P2「`harness` 用の宣言(`~/.hermes`
など)の中の `GH_CONFIG_DIR` も信頼する」を修正.hook しか持たない provider
ではシェルがルートに書けるため,`gh-config` 用の宣言だけを信頼する.Red → Green
- [x] review 対応(b277803): claude-review P2「コマンド sandbox のない provider
にも緩和が効く」を修正.Claude(`provider` なし)と Codex に限定.Red(5 provider が allow)→
Green,全テスト 6666 passed
- [x] bootstrap: 外部ダウンロードの失敗(curl の終了コード 22).ユーザーの rerun で通過
- [x] jev-lint(advisory): テストで繰り返していたコマンド文字列を定数にした.`check(mode: str)` と
`AGENT_GH_POLICY` の文字列は既存のコードで今回の変更範囲ではなく,`entry without a path` は外側の
`invalid write protection policy: <path>` が操作を示すので変えていない
- [ ] codex-review: Codex の usage limit で review を完了できず失敗(コード起因ではない)

## dotfiles が満たすべきこと(#532 で既に満たす)

- `GH_CONFIG_DIR` を **絶対パス** `$HOME/.config/agent-harness/gh` として Claude
の `settings.env` と Codex の `shell_environment_policy.set` に渡す.
- そのディレクトリ自体と `~/.config`,`~/.config/agent-harness` を symlink
にしない(実体が宣言の外にあると gh ガードは ask に戻る).`config.yml` / `hosts.yml`
を置くなら実体もこのディレクトリ内に置く.

## マージ後の確認(ユーザー)

- [ ] agent-harness でレンダリングした `~/.claude/settings.json` に
`Edit(~/.config/agent-harness/gh/**)` と `sandbox.filesystem.denyWrite` の
`~/.config/agent-harness/gh` があること,Codex の `config.toml` に
`"~/.config/agent-harness/gh" = "read"` があること.
- [ ] Claude で `~/.config/agent-harness/gh/config.yml` への Write
が拒否されること.sandbox の Bash で `touch ~/.config/agent-harness/gh/x` が失敗すること.
- [ ] Claude で `git fetch -q origin && gh pr view <n> --repo
furedea/agent-config` が確認なしで動くこと.
- [ ] Codex の hook に `GH_CONFIG_DIR` が渡らない場合,その行は従来どおり ask
になる(安全側).渡したい場合は dotfiles 側で hook の環境にも設定する.

<details><summary>設計・証拠・範囲</summary>

- 宣言: `agents/hooks/rules/write_protected_paths.json`(旧
`harness_roots.json`).`purpose` は `harness` と `gh-config`.テスト用の上書き変数は
`AGENT_WRITE_PROTECTED_PATHS`(旧
`AGENT_HARNESS_ROOTS_POLICY`).別ファイルを並べず一般化したのは,provider 設定・file guard・gh
guard が同じ問い(エージェントがそのパスを書けるか)を読むため.`guard_file.py harness` のモード名は
hooks.json と全 adapter が参照するので据え置いた.
- gh ガードは hook
がユーザー権限で動くため書込み可否を自分で確かめられない.宣言を信頼する根拠は,`tests/test_harness_protection.py`
が宣言と各 provider の設定を照合すること.作業ディレクトリと `$TMPDIR` は許可リストに関係なく書けるので,Claude の
`denyWrite` と Codex の `read` も足した.
- gh を逸らせる他の経路(`_read_host_problem` の docstring と
`tests/hooks/test_gh_protected_config.py`):
- `GH_HOST`,`GH_REPO`,`GH_CONFIG_DIR`,`HOME`,`XDG_CONFIG_HOME`,proxy,CA
bundle は環境変数で,行の代入・`export` は既存の `changes_environment` で ask.
- alias,pager,`http_unix_socket` は `config.yml` / `hosts.yml`
にあり保護ディレクトリ内.
  - `--repo` があれば作業ディレクトリのリモートと git 設定は読まない.
  - extension は組み込みコマンドを上書きできず,state / cache ディレクトリはホストを選ばない.
- PATH への gh / git の仕込みは `_stand_in_problem` が独立に判定する.既存テスト `cp x
later/git; gh pr list -R …` は PATH に `later`
がなく実際には仕込めていなかった(`changes_directory` だけで ask していた)ので,PATH
に相対エントリを足して本当に仕込める形にし,gh も対象に加えた.
- 既存の #282 テスト(`test_gh_line_asks.py`)は「gh の設定が保護されていない」前提を fixture
で明示した.保護された `GH_CONFIG_DIR` の環境で実行すると allow になっていたため.
- DESIGN.md / DESIGN_ja.md の「agent-harness が `denyWrite`
を置き換えるので未適用」という注記は,agent-harness#213 が 2026-11-11 に close
され,インストール済み設定にソースの項目が入っていることを確認して直した.
- review の補足(finding ではない): `$HOME` が symlink の配下にあるホストでは `holds()`
が一致せず常に ask になる.安全側なので変えていない.
- ADR-0032 を追加し,ADR-0031 を `partly-superseded-by` にした.
- 範囲外: トークン発行と dotfiles の変更(furedea/dotfiles#532),GitHub
Gateway(#181),マージ.

</details>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: furedea <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

エージェントの GitHub App トークンを dotfiles のスクリプトで発行する

1 participant