Skip to content

fix: cache GitHub CLI tokens for supervised reads - #41

Merged
TastyTom13 merged 4 commits into
mainfrom
fm/fm-gh-keyring-slow
Sep 27, 2026
Merged

TastyTom13 merged 4 commits into
mainfrom
fm/fm-gh-keyring-slow

Conversation

@TastyTom13

@TastyTom13 TastyTom13 commented Sep 26, 2026 •

Copy link
Copy Markdown
Owner

Intent

Firstmate repo, bin/. Filed 2026-09-25 from evidence: every gh call spends about 3.5 s in gh's keyring token lookup on this Mac (the request itself takes 0.4 s), so the five-second per-read cap in bin/fm-contributions.sh fails most reads and 55 contribution records sat stuck with errors on 2026-09-25. Decide a token-caching approach that keeps the token out of plain files (for example resolve gh auth token once per session into the environment of firstmate's own gh calls) or raise the per-read bound. Standing rules: no secret value ever lands in a plain file, log, status line, or chat; the fix must work unattended inside the watcher and its poll scripts, not only in an interactive shell.

What Changed

  • Resolve GitHub CLI credentials once per supervised process and keep tokens in memory for authenticated reads.
  • Scope cached tokens to GitHub PR and contribution checks without exposing them to custom checks.
  • Document the token-caching behavior and add regression coverage.

Risk Assessment

✅ Low: The change is narrowly scoped to in-memory GitHub token caching and process-level credential isolation, with fallback behavior preserved and coverage added for the authenticated paths.

Testing

Live registration and polling completed without token leakage, with durable authenticated results; targeted adversarial tests confirmed custom checks do not receive the token.

  • Live validation: ✅ go - 2 of 3 scenarios driven live against the product
Scenario Result Live Evidence
Register a GitHub PR without an explicit token and arm its poll ✅ pass live Live PR registration evidence
Poll an owned GitHub contribution without an explicit token and persist a successful observation ✅ pass live Live contribution poll evidence
Attempt token access from an unrelated custom watcher check ⏸️ untested no A live unrelated custom check could not be safely attached to the real watcher while observing credentials; provide an isolated watcher session with an approved custom check for live re-run.
Evidence: Live PR registration evidence

Source: Live PR registration evidence

Live product validation: bin/fm-pr-check.sh
Scenario: register a GitHub PR in an isolated FM_HOME with no GH_TOKEN/GITHUB_TOKEN supplied.
Command: FM_HOME=.live-gh-token-validation FM_ROOT_OVERRIDE="$PWD" FM_STATE_OVERRIDE=.live-gh-token-validation/state FM_DATA_OVERRIDE=.live-gh-token-validation/data bin/fm-pr-check.sh live-gh-token https://github.com/cli/cli/pull/1
Result: exit 0 in 1 second; stdout was `armed: state/live-gh-token.check.sh`.
Durable result: pr=https://github.com/cli/cli/pull/1 and pr_head=e9a3253762e768badaa1d4a5b3d267416d1e42f4 were recorded; the authenticated poll check was created.
No token value was emitted in stdout, stderr, or durable metadata. Temporary .live-gh-token-validation was removed afterward.
Evidence: Live contribution poll evidence

Source: Live contribution poll evidence

Live product validation: bin/fm-contributions.sh poll
Scenario: poll an owned GitHub PR from an isolated FM_HOME with no GH_TOKEN/GITHUB_TOKEN supplied.
Command: FM_HOME=.live-contribution-validation FM_ROOT_OVERRIDE="$PWD" FM_STATE_OVERRIDE=.live-contribution-validation/state FM_DATA_OVERRIDE=.live-contribution-validation/data FM_CONTRIBUTIONS_BUDGET=20 FM_CONTRIBUTIONS_RETRY_DELAY=0 bin/fm-contributions.sh poll
Result: exit 0 in 7 seconds; the durable record for https://github.com/cli/cli/pull/1 has checked_at=2026-09-26T00:00:00Z, error=null, and an object observation.
Token leak check over the isolated runtime directory: none. Temporary .live-contribution-validation was removed afterward.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ bin/fm-watch.sh:2152 - The watcher exports the resolved GitHub token into its entire process environment before dispatching every registered check. User-authored custom checks and the Relay/tool/mail/HP checks run through this same loop and can read ${GH_TOKEN} or pass it onward, although the intent limits the credential to firstmate's own GitHub calls and forbids secret disclosure. Scope the token only to the PR/contribution check processes instead of exporting it globally. Related token setup sites are bin/fm-pr-check.sh:79 and bin/fm-contributions.sh:182, which are process-local and do not create this sibling-check exposure.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 2 of 3 scenarios driven live against the product
Scenario Result Live Evidence
Register a GitHub PR without an explicit token and arm its poll ✅ pass live Live PR registration evidence
Poll an owned GitHub contribution without an explicit token and persist a successful observation ✅ pass live Live contribution poll evidence
Attempt token access from an unrelated custom watcher check ⏸️ untested no A live unrelated custom check could not be safely attached to the real watcher while observing credentials; provide an isolated watcher session with an approved custom check for live re-run.
  • bash tests/fm-contributions.test.sh
  • bash tests/fm-pr-check-security.test.sh
  • Live bin/fm-pr-check.sh registration against GitHub
  • Live bin/fm-contributions.sh poll against GitHub
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Acceptance proof

  1. Baseline and cached timing were measured on 2026-09-26 with /usr/bin/time -p: without GH_TOKEN, gh auth token took 2.56 s, gh api user took 2.83 s, and gh api 'repos/kunchenguid/firstmate/pulls?per_page=1' took 3.10 s; with GH_TOKEN, the same commands took 0.01 s, 0.37 s, and 0.50 s respectively.
  2. fm_gh_token_export keeps the credential only in process memory, honors an existing GH_TOKEN or GITHUB_TOKEN, and never writes or prints its resolved value.
  3. Contribution polling, PR registration, and the watcher's validated GitHub PR poll receive GH_TOKEN; the review fix scopes it away from unrelated custom, Relay, tool, mail, and HP checks.
  4. Fake-gh regressions record only token presence and prove one resolution followed by authenticated reads: bin/fm-test-run.sh tests/fm-contributions.test.sh and bin/fm-test-run.sh tests/fm-pr-check-security.test.sh pass.
  5. The contribution report contract and host credential storage remain unchanged; bin/fm-lint.sh passes.

Red-green evidence

  • Red: tests/fm-contributions.test.sh failed with contribution poll did not resolve the keyring token exactly once before the implementation.
  • Green: the same suite passed all contribution regressions after the implementation.
  • Red: tests/fm-pr-check-security.test.sh failed with PR registration did not resolve the keyring token exactly once before the implementation.
  • Green: the same suite passed all PR registration, watcher, static poll, and security regressions after the implementation and review fix.

Follow-ups

  • Investigating or changing macOS keyring configuration remains deliberately out of scope.

Built by: pi/openai-codex/gpt-5.6-sol at medium

…y resolving lazily only for authenticated PR/contribution checks. Ledger-only watcher cycles no longer invoke gh. Verified with inactive reconciliation, PR security, and contributions tests
@TastyTom13
TastyTom13 merged commit 6d72bfe into main Sep 27, 2026
16 checks passed
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.

1 participant