Skip to content

fix(github-app): refresh the agent token early and often - #164

Merged
furedea merged 4 commits into
mainfrom
fix/refresh-token-early
Oct 20, 2026
Merged

furedea merged 4 commits into
mainfrom
fix/refresh-token-early

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

概要

  • インストールトークンの寿命は 60 分なのに,launchd ジョブは 50 分ごとにしか発行しないため,スリープや launchd の遅延が 10 分あると失効していた(2026-10-20 に 14:35 発行のトークンが 15:35 に失効し,15:25 の実行は起きなかった).
  • github-app-token refresh は,次の条件をすべて満たすときだけ発行せずに成功終了する.--force を付けると常に発行する.
    • 状態ファイルを読め,前回の更新が失敗していない
    • 記録された発行元が設定と一致する(App ID,インストールの選び方,設定した場合はインストール ID)
    • hosts.yml を読め,その oauth_token の SHA-256 が記録と一致する
    • 残りが 20 分以上ある
  • インストールの選び方は「設定した ID(configured)」か「App の唯一のインストールを解決(only_installation)」として記録する.ずっと未設定ならそのまま使い続ける.設定済みから未設定へ,またはその逆に変えたら発行し直すので,未設定側では唯一のインストールを解決し直し,インストールが複数あれば失敗する.
  • 状態ファイルにはトークン本体ではなく SHA-256(既存依存の ring で計算)だけを記録する.発行元と指紋がない旧形式の記録は 1 回だけ発行し直す.
  • status は hosts.yml がない場合,壊れている場合,記録と違うトークンを持つ場合,旧形式の記録の場合も失敗を報告する.
  • launchd エージェントと systemd タイマーは 5 分ごとに確認する.発行の判断は Rust 側にあるので,確認を増やしても発行は約 40 分ごとで,GitHub API の呼び出しはほとんど増えない.
  • 決めてほしいこと:なし.
flowchart LR
  A[5 分ごと / ログイン時] --> B{--force?}
  B -->|yes| M[発行]
  B -->|no| C{状態を読める かつ 前回失敗なし}
  C -->|no| M
  C -->|yes| D{発行元が App ID・選び方・インストール ID に一致}
  D -->|no| M
  D -->|yes| E{hosts.yml のトークンが記録した指紋と一致}
  E -->|no| M
  E -->|yes| F{残り 20 分以上}
  F -->|no| M
  F -->|yes| S[発行せず成功終了]
Loading
ファイル 変更
src/github_app.rs MINT_BELOW_REMAINING_SECS(20 分)と RefreshRequest.force を追加.発行元,hosts.yml の指紋,残り時間がすべて揃うときだけスキップし,status で hosts.yml の欠落・不一致・旧形式の記録を報告
src/github_app/token_files.rs 記録に issuer(app_id,installation_id,selection)と token_sha256 を追加し,hosts.yml と照合する hosts_token を追加
src/cli.rs refresh --force を追加
nix/agent_github_app_token_module.nix StartInterval を 300 秒,systemd の OnCalendar を *:0/5 に変更し,コメントで「5 分ごとに確認し 20 分未満で発行」を説明
tests/agent_github_app_token.nix 3000 秒 / *:0/50 の固定値を 300 秒 / *:0/5 に更新
README.md,README_ja.md ジョブの動作,--force,status の失敗条件を記載
docs/adr/0021_refresh_the_agent_token_from_a_github_app.md 元の決定文は変えず,末尾に ## Amendment (2026-10-20) を追記

受け入れ条件

  • 残り 20 分以上で他の条件も満たすなら発行しない(境界の残りちょうど 20 分を含む)
  • 残り 20 分未満,状態ファイルなし,状態ファイル破損,前回失敗のときは発行する
  • hosts.yml がない,壊れている,別のトークンを持つときは発行し,status が失敗を報告する
  • App ID またはインストール ID が変わったときは発行する
  • インストール ID を設定済みから未設定へ,またはその逆に変えたときは発行し直し,未設定でインストールが複数あれば失敗する
  • インストール ID がずっと未設定なら記録済みのトークンを使い続ける
  • 発行元と指紋のない旧形式の記録では 1 回だけ発行し,その後はスキップする
  • --force は残り時間に関係なく発行する
  • launchd と systemd が 5 分ごとに確認する
  • ADR-0021 に変更を追記する
  • 実機で darwin-rebuild switch 後,ログに 5 分ごとの実行が残り,トークンが失効しない
検証結果
  • 時刻と GitHubAppApi を差し替えるテストを先に追加し,red を確認してから実装した.
    • 初回:スキップのテストが red
    • Codex レビュー 1 回目:hosts.yml の欠落・破損・不一致,App 切り替え,インストール切り替え,旧形式の記録の 6 件が red
    • Codex レビュー 2 回目:Some(42) → None でインストールが複数なら失敗,Some(42) → None で唯一のインストール 43 なら発行,None → Some(42) で発行の 3 件が red
  • cargo fmt --check,cargo clippy --all-targets -- -D warnings:ok
  • cargo test:全件 ok(lib 114 件を含む)
  • nix flake check:all checks passed(Nix テストは実装前に red を確認)
  • pre-commit(statix,deadnix,ls-lint,nixfmt,fmt,clippy)と commitlint:ok

補足

  • CHANGELOG は release-please が生成するため手で編集していない.
  • ADR-0021 は新しい ADR を起こさず,末尾の Amendment 節で今回の変更を記録した.このリポジトリには ADR 内に追記する前例がないため,日付付きの見出しにした.

Closes #163
Refs furedea/dotfiles#241

🤖 Generated with Claude Code

furedea and others added 4 commits October 20, 2026 16:00
Installation tokens live 60 minutes, and the refresh job ran every 50
minutes, so a 10-minute launchd deferral or sleep let the token lapse.

`github-app-token refresh` now keeps a token with at least 20 minutes
left and mints only when less remains, the status file is missing or
corrupt, or the last refresh failed. `--force` always mints. The
launchd agent and systemd timer check every 5 minutes.

Closes #163
Refs furedea/dotfiles#241

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The skip trusted only the state JSON, so a missing, corrupt, or replaced
hosts.yml, or a changed App or installation, kept a wrong token for tens
of minutes.

The state now records the issuing App and installation and a SHA-256 of
the token. Refresh keeps the token only when both match, and status
reports a missing or mismatched hosts.yml. Records written by older
releases lack these fields, so they mint once.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A token minted for a configured installation was kept after the ID was
unset, so refresh never checked whether the App had several
installations.

The issuer now records whether the installation was configured or
resolved as the App's only one, and refresh keeps a token only when
the selection mode matches too.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@furedea
furedea merged commit 46a25fb into main Oct 20, 2026
11 checks passed
@furedea
furedea deleted the fix/refresh-token-early branch October 20, 2026 07:23
furedea added a commit that referenced this pull request Oct 21, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.9.5](agent-harness-v0.9.4...agent-harness-v0.9.5)
(2026-10-20)


### Bug Fixes

* **github-app:** refresh the agent token early and often
([#164](#164))
([46a25fb](46a25fb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

fix(github-app-token): refresh early and often so the agent token never lapses

1 participant