Skip to content

CI: do not authenticate an unused robot token in the Codex code review - #115232

Merged
maxknv merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-codex-review-unused-robot-auth
Aug 24, 2026
Merged

maxknv merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-codex-review-unused-robot-auth

Conversation

@groeneai

@groeneai groeneai commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

Related: #113059

The Code Review job died on gh auth login for 10 pull requests in the 24 h to
2026-08-17, losing the automated review for those heads. A fork PR cannot re-run it,
so recovery needs a fresh push.

ERROR: Codex review failed after 3 attempts: CalledProcessError:
       Command '['gh', 'auth', 'login', '--with-token']' returned non-zero exit status 1.

_run_codex_once authenticated a robot token before every attempt, under
check=True, but the Codex backend never uses it: enable_gh_auth=True has the
runner authenticate the app up front, and the prompt runs every agent gh call with
GH_CONFIG_DIR unset. Across the 25 job logs that reached the agent,
246 of 246 executed gh calls unset it and none used the scoped robot directory, so
that login could only fail the job. In those same logs
/ci/robot-ch-test-poll-copilot answered HTTP 401: Bad credentials on 27 of 27
attempts, the oldest on 2026-08-03, and /ci/robot-ch-test-poll-1-copilot never did.
Two robots
over three attempts rotate as [X, Y, X], so the healthy one gets a single attempt
when the shuffle puts the dead one first, and one transient GraphQL 503 there fails
the job.

This removes that login and its temporary GH_CONFIG_DIR from the Codex attempt, so
it runs on the app identity it already used and retries serve agent failures.
_run_copilot_once keeps its login, which the Copilot CLI does use.

New ci/tests/test_copilot_review_codex_auth.py: 5 tests fail on the current code
with the error above and pass here; the 2 pinning unchanged behaviour were verified
by mutating the code they pin. No secret, process or sleep involved.

Out of scope: /ci/robot-ch-test-poll-copilot still needs rotating operationally
(--copilot would still need it); and 8 further failures that window are a separate
defect, gh api -X PATCH on issues/comments/<id> returning 503 after its retries.


Workflow [PR]
Sync PR [sync-upstream/pr/115232]

Version info

  • Merged into: 26.9.1.36 (included in 26.9 and later)

The Code Review job died on `gh auth login` for 10 pull requests in the 24 h to
2026-08-17, losing the automated review for those heads:

    ERROR: Codex review failed after 3 attempts: CalledProcessError:
           Command '['gh', 'auth', 'login', '--with-token']' returned non-zero exit status 1.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113059&sha=83188f6442a4f2d7e4376efe54c2bf3bad32dd50&name_0=PR&name_1=Code%20Review
https://s3.amazonaws.com/clickhouse-test-reports/PRs/113059/83188f6442a4f2d7e4376efe54c2bf3bad32dd50/code_review/job.log
ClickHouse#113059

_run_codex_once authenticated a robot token before every attempt, under
check=True, so any rejection raised before the agent started. The Codex backend
does not use that token. The job sets enable_gh_auth=True, so the runner mints a
GitHub App token into the ambient gh config before the script runs, and the
prompt has the agent prefix every gh call with GH_PREFIX = "env -u
GH_CONFIG_DIR", discarding the scoped robot directory. Over all 25 job logs that
reached the agent, 246 of 246 executed gh calls unset it and none used the robot
directory, and every HTTP 401 in those logs belongs to the robot login rather
than to an agent call. So that login could only ever fail the job.

/ci/robot-ch-test-poll-copilot has answered HTTP 401: Bad credentials on 27 of 27
observed attempts, with no success, since 2026-08-03, while
/ci/robot-ch-test-poll-1-copilot never returned a 401: one dead credential, not a
window-wide outage. Two robots over three attempts rotate as [X, Y, X], so the
healthy robot gets one attempt of three when the shuffle puts the dead one first,
and a single transient GraphQL 503 on that attempt then fails the job. The
rotation had been silently absorbing a permanently broken credential for two
weeks.

Remove the login and its temporary GH_CONFIG_DIR from the Codex attempt, so it
runs on the app identity it was already using and the retry budget is spent on
agent failures. _run_copilot_once keeps its login: the Copilot CLI authenticates
to GitHub with that token directly. Rotating the dead token remains an
operational matter, which the Codex path no longer depends on.

The new test file covers both: five cases fail on the previous code with the
error above, and two pin behaviour that must not change, each verified by
mutating the code it pins. One of those pins is the enable_gh_auth flag the
Codex path now depends on, which lives in ci/defs/job_configs.py rather than in
the job, so nothing else in the suite would notice its removal.
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review

An independent cold review plus a second model reviewed this change before it was opened. Verdicts
below; findings marked AGREE were fixed in this PR.

# Severity Finding Verdict
1 ⚠️ Major The suite mocks the whole boundary and calls _run directly, so nothing pins enable_gh_auth=True. After this change that flag is the only thing authenticating the Codex path, and deleting it would leave all the new tests green while the agent ran unauthenticated. AGREE - added a config-level pin asserting the Code Review job keeps enable_gh_auth=True, mutation-validated by flipping the flag and confirming the test reddens.
1b ⚠️ Major That pin covers only half the invariant. The credential has to be reachable with GH_CONFIG_DIR unset, which holds natively but not in a container: the container path passes the app config solely as GH_CONFIG_DIR=/ghconfig, and every agent command unsets that variable. Setting run_in_docker left all seven tests green. AGREE - the same test now also pins run_in_docker == "", with the coupling explained in its docstring, mutation-validated in both directions.
1c 💡 Nit Alternatively, support and test the containerized credential path. DISAGREE - nothing asks this job to run in a container, and making the agent work under /ghconfig would mean changing the env -u GH_CONFIG_DIR prefix this change deliberately leaves alone. The pin is the half that fails closed.
2 💡 Nit The stated parent-failure count was wrong: five of the new tests fail on the previous code, not four. test_codex_retry_budget_is_spent_on_agent_failures also reddens there, because it requires zero gh auth login calls and the previous code authenticates before every attempt. AGREE - corrected in the description and the commit message.
3 💡 Nit The HTTP 401 count cited the wrong log corpus: 21 was measured over an earlier subset, while the sentence names all 25 logs that reached the agent. AGREE - re-measured per occurrence over the full corpus: 27 of 27 for /ci/robot-ch-test-poll-copilot, 0 of 34 for /ci/robot-ch-test-poll-1-copilot. Description and commit message updated.
4 💡 Nit _run still copies and shuffles ROBOT_NAMES on every attempt even though the Codex path now ignores the value. DISAGREE - _run is shared with _run_copilot_once, which does consume the rotation. Removing it needs either a duplicated retry loop or a backend special-case inside it, both larger than this deletion. Cost is one shuffle of a two-element list per job run.
5 💡 Nit The module docstring still names /ci/robot-ch-test-poll-copilot as the Copilot backend's credential, the same token measured dead here. DISAGREE - the line is unchanged by this PR and still accurate: Copilot does authenticate a robot token from ROBOT_NAMES. Naming the other token would relocate the staleness, and a token's health is operational state that source text should not track.
6 💡 Nit Dropping GH_CONFIG_DIR from the codex exec environment removes an implicit fallback: an agent gh call that omitted the env -u GH_CONFIG_DIR prefix used to run as the robot and now runs as the app. DISAGREE - the fallback was incidental and provably unused. All 246 executed agent gh calls across the logs already unset the directory, and every bot comment on the affected pull requests was authored by the app, none by the robot account. The app is this job's intended identity: _post_review already posts the summary as the app with no scoped environment. Sandbox read scope is unchanged.

Verification behind the change: the premise that the Codex backend never uses the robot token was
re-derived two independent ways, from the job logs (246 of 246 executed agent gh calls unset
GH_CONFIG_DIR, none used the scoped directory) and from GitHub itself (no review or issue comment
on the affected pull requests was ever authored by the robot account). Test liveness was traced per
test rather than assumed, and every test that passes on both trees was validated by mutating the
code it pins.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, two levels, no randomness or network. pytest ci/tests/test_copilot_review_codex_auth.py fails 5 of 7 on the current code; a fake-codex/always-failing-gh end-to-end run of _run_codex_once raises the production string verbatim.
b Root cause explained? Yes. _run_codex_once validated a robot token under check=True before every attempt, but the Codex backend runs on the app token the runner mints (enable_gh_auth=True) and unsets GH_CONFIG_DIR on every agent gh call, so the login could only fail the job; one robot has 401'd on 27 of 27 observed attempts since 2026-08-03 while the other never returned a 401, and the [X, Y, X] rotation leaves the healthy robot one attempt of three.
c Fix matches root cause? Yes: it deletes the credential validation that cannot help, rather than hardening it. An earlier design that added retries and negative caching there was discarded for that reason. No widened bound, no extra attempts, no defensive guard.
d Test intent preserved / new tests added? No test weakened or removed. 7 new tests: 5 red-to-green, plus 2 pinning unchanged behaviour so a later cleanup cannot silently strip the login the Copilot backend does need, nor the job definition this path now relies on. Every pinned property was proven live by mutating it: the Copilot robot login, MAX_ATTEMPTS, enable_gh_auth=True, and run_in_docker="", each reddening only its own assertion.
e Both directions demonstrated? Yes. Unit: 5 failed / 2 passed before, 7 passed after. End-to-end: Codex review failed after 3 attempts: CalledProcessError: ... ['gh','auth','login','--with-token'] before, review file written with GH_CONFIG_DIR unset after.
f Fix is general across code paths? Invariant: every identity an attempt validates is one that attempt uses. _run_copilot_once (token genuinely used by the Copilot CLI), _gh_auth_with_robot_token (kept for it), _post_review (already ambient), codex login --with-api-key (a different credential, actually consumed) all reviewed and correctly unchanged. The only other gh auth login --with-token calls are in gh_auth.py, already retried; revert_ci_regressions.py scopes GH_CONFIG_DIR for the opposite purpose (credential denial, enable_gh_auth=False). The dependency this moves out of the file has two carriers, and both are pinned: the flag that mints the credential, and the native execution that makes it reachable to the agent.
g Fix generalizes across inputs (params/datatypes/wrappers)? Not a data path: no types, wrappers or boundary values involved. The input axis is which robot the rotation picks, and the Codex path is now independent of all of them; the first test asserts success when every robot fails, harsher than the observed one-dead-robot case.
h Backward compatible? (maintainer-approved exception only) Yes. CI-internal only: no setting, default, format or user-visible behaviour, so no SettingsChangesHistory.cpp surface. Both robot secrets stay referenced, so no provisioning change.
i Invariants and contracts preserved? _run_codex_once keeps its arity so the shared retry loop drives both backends identically; CODEX_HOME scoping and its ./ci/tmp placement are unchanged, so the API key still never reaches global runner state; the TemporaryDirectory still cleans up on every path including exceptions; _drop_stale_review_file still runs first; the success contract (exit 0 and non-empty review file) is untouched. The dependency this moves out of the file, the app token the runner mints, is pinned from the workflow in full: the job must both request it (enable_gh_auth=True) and run natively (run_in_docker=""), since a container receives that token only as GH_CONFIG_DIR=/ghconfig, which every agent command unsets.

ci/tests/ full suite: 717 passed, 2 failed. Both pre-existing in test_max_failures_exit_code.py, failing identically on an unpatched tree and untouched by this diff.

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b12c594]

Summary: ✅


AI Review

Summary

This PR removes the unused robot-token gh auth login from the Codex review path and adds focused tests that pin the current PR workflow auth shape. I did not find correctness, safety, or regression issues in the current code; the change is consistent with how enable_gh_auth=True, native execution, and GH_PREFIX already interact in the Code Review job.

Final Verdict

✅ No blocking or major issues found.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano @maxknv, could you review this? The Codex backend ran gh auth login with a robot token it never uses (every agent gh call is prefixed env -u GH_CONFIG_DIR, and enable_gh_auth=True already mints the app credential it actually runs on), so one permanently dead robot secret was failing the job outright; this removes that login from the Codex attempt and leaves the Copilot path, which does use the token, alone.

@maxknv
maxknv added this pull request to the merge queue Aug 24, 2026
Merged via the queue into ClickHouse:master with commit b969510 Aug 24, 2026
540 of 545 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect manual approve Manual approve required to run CI pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants