Skip to content

Serve cached gh credentials to remote sends while refreshing in the background - #5258

Merged
ymichael merged 1 commit into
mainfrom
bb/stop-gh-auth-lookups-on-the-send-path-for-remote-thr_bnzqa2ntm8
Oct 9, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/stop-gh-auth-lookups-on-the-send-path-for-remote-thr_bnzqa2ntm8

Conversation

@ymichael

@ymichael ymichael commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Every send to a thread on a non-server host assembles that host's environment, which forwards the server's gh login (GH_TOKEN, git author/committer identity). Those credentials were memoized for 60 s (5 s when empty) and then refreshed inline, so the first send after expiry blocked on gh auth token and gh api user — 350–820 ms of network-bound latency on the send path.

What changed

  • apps/server/src/services/lib/async-ttl-memo.ts: new opt-in staleWhileRevalidate option. Once a key has loaded, an expired value is returned immediately while a single background lookup replaces it; a failed background lookup leaves the stale value in place and the next call retries. Lookup start/settle logic is extracted into a shared start() helper. Expired entries are not pruned when the option is on (they are what gets served). Other memo callers are unaffected.
  • apps/server/src/services/machines/git-credentials.ts: the credential cache enables staleWhileRevalidate. resolveGitCredentials (send path) serves cached credentials instantly; machineGitHealth (Settings health check) still forces a blocking refresh.

Trade-off: after a token rotation or gh auth login/logout, the first send after expiry still sees the previous result; the next one sees the refreshed value.

No wire changes.

How you verified

  • New async-ttl-memo test: expired value is served, only one background lookup runs, a rejected lookup keeps the stale value and is retried, success replaces it, and a forced refresh blocks and is shared with concurrent callers.
  • Updated git-credentials tests for the serve-stale-then-refresh behavior.
  • pnpm exec turbo run typecheck lint --filter=@bb/server and the two test files pass.

AGENT GENERATED

🤖 Generated with Claude Code

…ackground

Every send to a non-server host assembles the host environment, which
forwards the server's gh login (GH_TOKEN and git identity) to that machine.
The credentials were memoized for 60 seconds and then refreshed inline, so the
first send after expiry waited on `gh auth token` and `gh api user` (350-820 ms
of network-bound latency).

The memo now supports stale-while-revalidate: once loaded, an expired value is
returned immediately while one background lookup replaces it. Settings health
checks still force a blocking refresh, and host connection already warms the
cache through the machine environment sync.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 60eed05 into main Oct 9, 2026
28 checks passed
@ymichael
ymichael deleted the bb/stop-gh-auth-lookups-on-the-send-path-for-remote-thr_bnzqa2ntm8 branch October 9, 2026 17:11
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