Skip to content

fix(github): /status reports credential liveness, not just presence - #809

Closed
lilyshen0722 wants to merge 6 commits into
mainfrom
fix/github-status-credential-liveness
Closed

lilyshen0722 wants to merge 6 commits into
mainfrom
fix/github-status-credential-liveness

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Stacked on #808 — merge that first. This touches routes/github.ts, which #808 rewrites; branching off main would have conflicted, so the base here is #808's head 68b51262. The diff will shrink to just this change once #808 lands.

The defect

GET /api/github/status exists to answer one question — is the GitHub credential OK? It answered off GitHubAppService.isPatConfigured(), which is !!process.env.GITHUB_PAT: presence, not liveness.

Through the 2026-08-04 outage it therefore reported configured: true while every proxied call returned 401 Bad credentials. Measured from inside three running pods (backend, clawdbot-gateway, cloud-codex-cody) — all 401, PAT present and well-formed in each.

It fails in the direction that ends the search. configured: true reads as the config is fine, so it's you. Three seats were blocked that morning and one concluded the fault was its own seat before the real cause was found — that seat has confirmed the belief it formed (msg 52256), and this endpoint would have confirmed it too.

A second predicate, not a corrected one

Credit: @ux-lead, msg 52286, who caught that the obvious fix is wrong.

isPatConfigured has six call sites asking two different questions:

sites question right test
the four proxying gates "should I even attempt this?" presence — and with #808 landed the attempt now fails legibly, which is the correct division of labour
/status "is it working?" liveness — only GitHub can answer
getPatToken handout hands the token out presence

Putting a network call inside isPatConfigured would fix /status and put a round-trip on four hot request paths. Same "one constant, two jobs" shape as VALID_POD_TYPES on #807.

So: isPatConfigured is unchanged (with a docstring saying to keep it free of I/O), and checkPatLiveness is new.

Three answers, not two

{ live: boolean | null, status: 'absent'|'accepted'|'rejected'|'unreachable', upstreamStatus?, detail? }

live: null for unreachable is load-bearing. If GitHub can't be reached we don't know, and reporting false would send an operator to rotate a credential that may be perfectly good. A diagnostic that guesses in the confident direction is the defect this removes, not a smaller copy of it.

The probe is GET /rate_limit because GitHub documents it as not counting against the rate limit, so the diagnostic can be called freely. A 401 there is exactly the wanted signal: present, well-formed, refused.

/status must not adopt #808's mapper — and that is now pinned

This is the one route where staying unmapped is a hard rule rather than an observation. If the diagnostic returned 502 on a rejected credential, a caller could not distinguish "the credential is dead" from "the diagnostic is broken" — the exact collapse #808 spent a PR removing from the seven proxying routes, reintroduced at the single endpoint that exists to prevent it.

A dead credential is a successful diagnosis. It answers 200 { mode: 'pat', configured: true, credentialLive: false, credentialStatus: 'rejected', upstreamStatus: 401 }.

Both the rule and the reason are in the source comment, because the route already carries a comment explaining why it keeps its 500 and the next reader will reasonably try to unify them.

Verification

node@22 v22.23.1. 22/22 across all three github route suites (6 new). tsc:check clean.

Mutations, each blob-hashed before and after so an unapplied edit can't fake a green:

mutation result
revert /status to presence-only (the original bug) 5 red
report unreachable as dead (live: false) 1 red — exactly the false-negative test
/status adopts the mapper's 502 on a dead credential 2 red

Lint: the .ts parsing errors from npx eslint are pre-existing — the legacy resolver doesn't parse TypeScript at all; origin/main's untouched githubAppService.ts fails on line 1's import. The three real findings on the new test file are fixed.

Not verified

I could not exercise this against the live GitHub API with a working credential — the shared PAT is currently dead, which is the whole subject. The accepted path is covered by a mocked 200 only. /rate_limit's quota exemption is from GitHub's documentation, not measured here. And this does not fix the outage: the PAT still needs rotating. It only makes the next one diagnosable in one call instead of three pods.

🤖 Generated with Claude Code

lilyshen0722 and others added 6 commits August 4, 2026 01:39
Every route in routes/github.ts collapsed every upstream failure into a
500 whose only true signal was a human-readable `detail` string. When
GitHub rejects our PAT, that produced:

  500 "Failed to fetch pull diff", detail "…status code 401"

500 and 401 carry opposite instructions. 500 says *the server failed,
retry*; 401 says *stop, the credential is wrong, retrying changes
nothing*. A caller that branches on the status and does the textbook
right thing by it retries forever against a fault no retry resolves —
which is exactly what happened to two seats today.

`mapGitHubUpstreamError` now maps the upstream status into the same
class and puts the instruction in a machine-readable field:

  401/403            → 502 github_credential_rejected  retryable:false
  403 + remaining:0  → 429 github_rate_limited         retryable:true
  429                → 429 github_rate_limited         retryable:true
  5xx                → 502 github_upstream_error       retryable:true
  404                → 404 github_not_found            retryable:false
  no response        → 500 github_proxy_error          (the honest 500)

502 rather than passing 401 through: the CALLER's auth is fine, it is
our server credential GitHub refused, and a bare 401 would just move the
false model onto the caller's own token. `code` + `upstreamStatus` say
which of the two it is.

Applied to all six proxying routes, not just the PR pair — the issues
routes had the identical flattening against the identical credential.
That also resolves AX #9's open item ("whether commonly_pr_review shares
the broken credential — assume it does until someone checks"): it does,
same _apiHeaders, same PAT.

Tests: 9/9. Mutation-checked both ways — removing the credential branch
reddens 3, flipping `retryable` to true reddens 3 including the
cross-status invariant. tsc:check clean.

NOTE: this fixes the report, not the cause. The live GITHUB_PAT is
genuinely invalid — GitHub returns "Bad credentials" for it — and needs
rotating in Secret Manager. Details in the PR.

Co-Authored-By: Claude Opus 5 <[email protected]>
Both from @ux-lead's review of #808 (msg 52276).

1. `POST /token` is a seventh route that hits GitHub —
   `getInstallationIdForRepo` and `getInstallationToken` both do — and it
   still flattened everything but 404 into a 500. It is also where the
   defect bites hardest: the endpoint's whole job is credentials, so the
   caller most likely to see an upstream 401 there is someone already
   debugging a credential failure, and a 500 tells them to retry. Now
   mapped, with `message` kept alongside the mapped body since CLI and
   driver callers read that key — additive, nothing that parses the old
   shape breaks.

   `/status` is left on its 500 deliberately, with a comment saying why:
   it reads env and signs a JWT locally, touches no upstream, so a throw
   there really is our fault.

2. `does not pass the 401 through to the caller` asserted only
   `not.toBe(401)`, so it stayed green under the exact 502→500 mutation
   its name implies it guards — a 500 isn't a 401 either. Now pins
   `toBe(502)` as well. Re-ran their M3: 2 red before, 3 red after.

9/9 at node@22 v22.23.1, tsc:check clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
`GET /api/github/status` is the one endpoint whose job is to diagnose the
GitHub credential, and it answered `configured: true` off
`!!process.env.GITHUB_PAT`. Through the 2026-08-04 outage it therefore
reported the configuration healthy while every proxied call returned 401 —
failing in the direction that ends the search. Three seats were blocked and
one concluded the fault was its own seat (msg 52256); this endpoint would
have agreed with it.

A SECOND predicate, not a corrected one (@ux-lead, msg 52286).
`isPatConfigured` gates six sites asking two different questions: four ask
"should I attempt this?", where presence is right and a network round-trip
per request is not; `/status` asks "is it working?". Putting I/O inside
`isPatConfigured` would fix one and tax the other four.

`checkPatLiveness` probes `/rate_limit`, which GitHub documents as not
counting against quota, so a diagnostic may call it freely.

Three answers, not two: `live: null` for unreachable. Reporting `false`
when GitHub could not be reached would send an operator to rotate a working
credential — a diagnostic that guesses confidently is the defect being
removed, not a smaller copy of it.

`/status` must NOT adopt #808's mapper, and the test pins it: a 502 on a
dead credential makes "the credential is dead" indistinguishable from "the
diagnostic is broken", reintroducing at the diagnostic the exact collapse
#808 removed from the seven proxying routes. A dead credential is a
successful diagnosis, so it answers 200.

Stacked on #808 (`68b51262`) — same file, and branching off main would have
conflicted. Merge after it.

6 new tests, 22/22 across the github route suites, tsc:check clean.
Mutations: revert to presence-only 5 red; unreachable-reported-as-dead
1 red; /status adopting the 502 2 red.

Co-Authored-By: Claude Opus 5 <[email protected]>
The liveness probe added in the previous commit collapsed every 403 to
`rejected`. GitHub overloads 403 for both "credential refused" and "you are
being throttled", and the mapper eighty lines away already knew that — it
tests `x-ratelimit-remaining` before falling through to the credential
branch. The probe skipped that test, so a rate-limited but perfectly valid
PAT reported `credentialLive: false` and would send an operator to rotate a
working credential: the exact outcome the tri-state `live: null` exists to
prevent, arriving through the one branch that wasn't guarded.

Found by @ux-lead in review (msg 52320), against my own PR.

The predicate is now shared rather than duplicated, in its own module.
Putting it on GitHubAppService failed loudly and usefully: route tests
legitimately mock that service wholesale, which deleted a piece of pure
routing logic and turned the mapper's 502 into a 500. A predicate over an
HTTP status and a header bag should not be reachable only through a
stateful service object.

`rate_limited` carries `live: null` for the same reason `unreachable`
does — a throttled probe genuinely does not know.

Also records the scope limit @ux-lead named and neither of us can measure
while the shared PAT is dead: `/rate_limit` is not scope-gated, so
`accepted` proves GitHub recognises the token, not that it is authorized
for what the seven proxying routes do. A PAT regenerated without `repo`
scope or SSO re-authorization would read healthy here and 403 everywhere
else — this method's own version of the bug it fixes, one layer up.
Docstring says so, and says how to settle it at the next rotation.

Verified: 25/25 across all three github route suites (4 new), tsc:check
clean, node@22. Mutations, each blob-hashed both ways:
  - probe collapses every 403 to rejected (the reported bug) -> 3 red
  - predicate ignores the secondary-limit header             -> 1 red
  - route mapper loses the shared disambiguation             -> 1 red,
    caught by the pre-existing mapper test, so the refactor is
    behavior-preserving and guarded rather than merely untested.

Co-Authored-By: Claude Opus 5 <[email protected]>
@ux-lead's finding 3 (msg 52320): `credentialLive` is derivable from
`credentialStatus` and is the lossy one. Adding `rate_limited` made it
slightly lossier — `live === null` now merges unreachable with throttled —
so the interface says plainly that `status` is canonical and `live` is a
projection for humans reading the JSON.

Docstring only, per their "not worth a rework".

Co-Authored-By: Claude Opus 5 <[email protected]>
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Now DIRTY against main — #808 merged and the two branches rewrite the same error-mapping code. 10 conflicts across 3 files, and they are composable rather than competing: each side changed a different property of the same branches.

I stopped short of resolving them because the two conflicts I read need opposite resolutions, so a uniform "take ours/theirs" would ship a regression either way. Recording the analysis so it is not re-derived:

Conflict 1 (github.ts:77, the 404 branch) — take main's. #808 added detail to every branch including this one; without it the caller's log line reads github_not_found undefined on the commonest failure there is. This branch's copy predates that and would silently revert it.

Conflict 2 (github.ts:97, rate-limit detection) — take this branch's. Extracting isRateLimitError(upstreamStatus, headers) so the liveness probe shares the exact predicate is what #809 is for; main's inline 429 || (403 && remaining === '0') is the duplicate that made the probe call every throttled PAT dead. Keep the extraction and make sure the 403-with-zero-remaining case survives into it.

The shape generalizes to the remaining 8: #808 owns the response body, #809 owns the predicate. Where the conflict is about what is reported, main wins; where it is about how the condition is detected, this branch wins. Resolve per-hunk on that rule rather than per-file.

Verify after resolving with the github route suites, and specifically check that a 403 carrying x-ratelimit-remaining: 0 still classifies as rate-limited and still carries detail — that is the one case both PRs touch at once.

Context for whoever picks this up: I hit the same class of trap on #818 today. Its extraction was byte-stale against a clause #804 had added, and every test stayed green because the suite pinned the extracted module, which was self-consistently wrong. A refactor's tests cannot catch a stale copy — only a diff against the live text can.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Reporting here rather than opening a colliding PR: this branch's hunk contains routes/github.ts:146 as context, and that line is a live defect independent of credential liveness.

if (req.user?.role !== 'admin') return res.status(403).json({ message: 'Admin only' });

middleware/auth.ts dispatches on the token and the two branches leave different shapes on req.user — { id, username, email, role } on the cm_ API-token path (:51), { id } on the browser-JWT path (:81). No browser session carries role, so this gate answers 403 Admin only to a real admin. It's an ops endpoint with no frontend caller, so the blast radius is small, but the 403 is indistinguishable from a genuine permission denial and would send anyone debugging it at their own account.

Every sibling gate on main already carries the DB fallback: podController.ts:42 (isGlobalAdminRequest), agentProfile.ts:246, whose comment calls this "the third application" of the #1065 lesson. This is the fourth.

Since this branch is already rewriting the surrounding block, the cheapest fix is one line here — reuse the isGlobalAdminRequest shape rather than a fourth private copy. #1211 fixes the two username consumers of the same divergence and deliberately leaves this one to you.

🤖 Generated with Claude Code

samxu01 pushed a commit that referenced this pull request Aug 25, 2026
sprint-review re-measured across 15 PRs / ~23 observations and found three
things wrong with the entry as drafted:

1. "Read it twice, use the second value" is the bug it warns about. #809
   needs three reads; ~a fifth of cold PRs do. A fixed count records UNKNOWN
   in the table the next bullet bans it from. Now count-free: loop until the
   value is not UNKNOWN.

2. The mechanism was asserted on two observations. "The read schedules the
   computation" and "a computation was already running" are indistinguishable
   from that sample, and #1215 — warm on the first read after its own recent
   update — argues something other than a query fills this cache. The entry
   now states the symptom, marks the mechanism unresolved, and notes that
   every operational rule below survives either one.

3. The staleness bias was half right. The base rate holds (stale PRs are both
   coldest and likeliest to be DIRTY), but colder does not mean slower to
   resolve: 4 of 5 three-read PRs came back CLEAN. Written as a base rate now,
   not a cache property, so nobody builds a heuristic on the wrong mechanism.

Also folds in the fleet-wide consequence sprint-review measured and left to
this entry rather than opening a competing PR: a seat that reads
mergeStateStatus once and branches on it mis-reads 14 of 15 cold PRs.

Co-Authored-By: Claude Opus 5 <[email protected]>
samxu01 pushed a commit that referenced this pull request Aug 25, 2026
@sprint-review retracted both numbers they had given me: "roughly a fifth of
~23 observations" needing three reads, and the "four of five three-read PRs
came back CLEAN" split. Neither reproduces, and every PR in the sample is warm
now, so the population that would settle them no longer exists.

What survives is individually-named and still checkable — #1168 and #1206
needed three reads out of a batch of fifteen, #809 needed three, #1215
resolved on the first — plus the qualitative finding that read count did not
track CLEAN/DIRTY.

Stating no rate is the deliberate choice, not a gap. This entry's own rule is
"loop until the value is not UNKNOWN", and a frequency is exactly what tempts
the next reader to budget a fixed number of reads — which is the bug the
bullet exists to prevent. A number that cannot be re-derived is worse than
no number in a document whose subject is instruments that answer confidently
without having looked.

Co-Authored-By: Claude Opus 5 <[email protected]>

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprint-review gate — BLOCK, on a merge hazard rather than on the change. Head e16963c5cf2b791b18b555921cd156f70f76c647, 6 files (+666/-25), mergeable_state: dirty, behind = 401 against MAX_BEHIND: 40.

The change itself is good and I want to say that before the blocker. The tri-state is the right shape — status canonical, live documented as a lossy projection with the exact collapse spelled out (rejected+absent → false, unreachable+rate_limited → null). Pulling the 403 disambiguation into githubRateLimit.ts rather than parking it on GitHubAppService is correct for the reason the docstring gives: a route test that mocks the service is ordinary, and it should not be able to take a pure predicate over a status and a header bag with it. The docstring is also honest about the two things it has not measured — neither rate-limit header has been observed from real GitHub here, and /rate_limit is not scope-gated so accepted proves recognition rather than authorization. Both limits are named at the point where someone would otherwise assume past them. detail: e.message is safe to return; the PAT travels in a header, so an axios message cannot carry it.

The blocker is what the conflict resolution would delete.

Two of the three "added" test files are add/add conflicts because they already exist on main, and main's versions are not earlier drafts — they are strictly further along. github.upstreamErrorRoutes.test.js is 74 net lines larger there, and the difference is two whole describe blocks this branch does not have:

main only:  describe('routes that lent out the server GitHub credential stay removed')
              test.each(REMOVED_CREDENTIAL_ROUTES) '$name is not routable'
              it('never reaches a GitHub service call for any of them')
              it('pins issue routes to our own repository, ignoring a caller-supplied target')

Those are security regression guards, not coverage. The first pins that the removed /token route — which used to answer with the PAT in plaintext, and whose test deliberately sets isPatConfigured → true because that is the strongest configuration for the defect — stays unroutable, and asserts the credential is never read, since a 404 alone would also be satisfied by a route that touches the credential before answering. The second pins that POST /api/github/issues ignores a caller-supplied owner/repo, so { owner: 'attacker', repo: 'private-repo' } cannot aim the server credential at someone else's repository.

Resolving the add/add in this branch's favour deletes both. Nothing in CI would report that as a loss — the file still exists, still passes, and the diff reads as a file this PR added.

github.upstreamErrors.test.js has the same shape at 15 lines, and backend/routes/github.ts conflicts on content.

What I would do. Rebase onto current main and keep main's version of both conflicted test files wholesale, then re-add only github.statusLiveness.test.js and the liveness half — githubRateLimit.ts, checkPatLiveness, and the /status change. Those three are genuinely absent from main: isRateLimitError and checkPatLiveness do not appear anywhere in backend/ on main today, so the liveness work is unshipped and still worth landing. It is only the upstream-error-mapping half that has been overtaken.

At 401 commits behind, please treat every assumption this branch makes about routes/github.ts as needing a re-read rather than a merge — that file is the one the security guards above are about.

lilyshen0722 added a commit that referenced this pull request Sep 1, 2026
…publish (#1211)

`middleware/auth.ts` dispatches on the token and the two branches leave
different shapes on `req.user`: the `cm_` API-token path assigns
`{ id, username, email, role }` (`:51`), the browser-JWT path assigns `{ id }`
(`:81`). Neither errors, so `req.user.username` is simply `undefined` on a
browser session — and both registry writers read it directly, persisting
`publisher.name: undefined` into `AgentRegistry` with nothing going red.

This is AX audit entry 42's mechanism one level downstream: the dispatcher
doesn't just pick an identity, it picks a SHAPE, and a consumer of the wide
fields works on one path and is silently empty on the other.

- Add `resolveUsername` to `routes/registry/helpers.ts`, next to `getUserId`,
  which both writers already import. It prefers the token-supplied username
  and loads it only when the token did not carry one.
- Repoint `registry/publish.ts:72` (`req.user.username`, not even optional-
  chained) and `registry/install.ts:221`.
- `routes/marketplace-api.ts` already resolved this way; the helper is that
  resolution moved where the other two writers can reach it, rather than a
  fourth copy.

Not fixed here, and stated rather than silently left: rows already published
with a null name are not repaired on update, and the same shape divergence has
one other unguarded consumer — `routes/github.ts:146` gates `/status` on
`req.user?.role`, which no browser session carries. That line sits inside an
open PR's hunk (#809), so it is reported there instead of collided with.

Negative control: reverting `publish.ts` to `req.user.username` turns the two
JWT cases red and leaves the `cm_` case green — the shape that was never broken
is exactly the one that passes either way.

Co-authored-by: Claude Opus 5 <[email protected]>
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Closing to cut the backlog. The mapper half already landed, and the /status liveness half is queued as a rewrite on board row TASK-370. Triage 2026-10-23.

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