Skip to content

ci: retry Copilot Code Review on transient gh API failures - #104227

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/code-review-job-retries
May 6, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/code-review-job-retries

Conversation

@groeneai

@groeneai groeneai commented May 6, 2026 •

Copy link
Copy Markdown
Collaborator

On PR #104204, the
"Code Review" check failed because the Copilot CLI hit a transient
GitHub authorization error mid-run, after ~1m 9s of work:

[10:08:14] Authorization error, you may need to run /login (Request ID: F924:13DD23:2A50C17:2E8FB85:69FB130D)
[10:08:14] ERROR: command failed after 1/1 attempt(s), exit code: 1
[10:08:14] ERROR: Copilot did not write ./ci/tmp/copilot_review.md
RuntimeError: Copilot did not write ./ci/tmp/copilot_review.md

Failed job: https://github.com/ClickHouse/ClickHouse/actions/runs/25428876194/job/74589889048
(workflow attempt 2 / Code Review attempt 1 of run 25428876194).
A 3rd workflow attempt re-ran Code Review at 11:03:54Z and finished cleanly,
which confirmed the failure was a transient blip — but by that point the
Mergeable Check status had already been set to failure ("Failed: Code
Review, Dropped: 71") at 10:22:35Z when Finish Workflow saw the failed run.

Per @alexey-milovidov's directive (comment):

@groeneai, check why "Code review" has failed. Likely, we need to add
retries in some of the GitHub-related commands inside it. Send a PR.

This PR adds two layers of retry:

  1. ci/jobs/copilot_review_job.py — wrap the gh-auth + copilot
    subprocess sequence in a 3-attempt retry loop with exponential
    backoff. Each attempt re-fetches the secret, re-runs gh auth login
    against a fresh GH_CONFIG_DIR, and re-invokes Copilot. An attempt
    counts as success only if the subprocess exits 0 AND REVIEW_FILE is
    written non-empty — auth failures inside Copilot don't surface as
    exceptions, only as exit code ≠ 0 and a missing review file.

  2. ci/praktika/gh.py — add GH.get_output_with_retries(cmd), the
    read-style counterpart of do_command_with_retries. Replace bare
    Shell.get_output(cmd) with the new helper for every
    GitHub-API read in gh.py:

    • post_pr_comment
    • post_fresh_comment
    • post_updateable_comment
    • get_pr_contributors
    • get_pr_labels
    • get_pr_title_body_labels
    • get_pr_label_assigner
    • get_pr_diff
    • pr_has_conflicts

    These would previously return "" on a transient gh failure and
    either propagate as json.loads('') crashes or silently misclassify
    the PR (e.g. miss an existing comment and post a duplicate). Same
    non-retryable error classes as do_command_with_retries
    (Validation Failed, Bad credentials, Resource not accessible).

    Also harden post_updateable_comment against malformed JSON: catch
    json.JSONDecodeError and treat it as "no existing comments" rather
    than crashing the caller.

Local sanity check:

$ PYTHONPATH=./ci python3 -c "from praktika.gh import GH; print(GH.get_output_with_retries('echo hello'))"
hello
$ PYTHONPATH=./ci python3 -c "from praktika.gh import GH; print(repr(GH.get_output_with_retries('false')))"
... 3 attempts, exponential backoff, then ...
''

CI Report (failed run that motivated this fix):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104204&sha=4e0497e7d58595bbab725d3f27d16c9303577b0f&name_0=PR&name_1=Code%20Review

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):

Not applicable — CI fix.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.349

The Code Review job's Copilot CLI subprocess occasionally hits transient
GitHub authorization errors mid-run, e.g.

    [10:08:14] Authorization error, you may need to run /login (Request ID: F924:13DD23:...)
    [10:08:14] ERROR: command failed after 1/1 attempt(s), exit code: 1
    [10:08:14] ERROR: Copilot did not write ./ci/tmp/copilot_review.md
    RuntimeError: Copilot did not write ./ci/tmp/copilot_review.md

(seen on PR ClickHouse#104204 attempt 2 of run 25428876194; attempt 3 of the same
run completed cleanly.) Without retries, a single transient blip from the
GitHub auth path inside Copilot is enough to mark "Code Review" as `FAIL`
and post `Mergeable Check: Failed: Code Review, Dropped: 71`.

Two fixes:

1. `ci/jobs/copilot_review_job.py`: wrap the gh-auth + copilot subprocess
   sequence in a 3-attempt retry loop with exponential backoff. Each
   attempt re-fetches the secret, re-runs `gh auth login` against a fresh
   `GH_CONFIG_DIR`, and re-invokes Copilot. An attempt counts as success
   only if the subprocess exits 0 AND `REVIEW_FILE` is written non-empty
   — auth failures inside Copilot don't surface as exceptions, only as
   exit code != 0 and a missing review file.

2. `ci/praktika/gh.py`: add `GH.get_output_with_retries(cmd)` helper that
   mirrors `do_command_with_retries` but returns stdout. Use it for every
   GitHub-API read that previously called bare `Shell.get_output(cmd)`:
   `post_pr_comment`, `post_fresh_comment`, `post_updateable_comment`,
   `get_pr_contributors`, `get_pr_labels`, `get_pr_title_body_labels`,
   `get_pr_label_assigner`, `get_pr_diff`, `pr_has_conflicts`. These
   would previously return `""` on transient `gh` failures and either
   propagate as `json.loads('')` errors or silently misclassify the PR
   (e.g. miss an existing comment and post a duplicate). Same
   non-retryable error classes as `do_command_with_retries`
   (`Validation Failed`, `Bad credentials`, `Resource not accessible`).

Also harden `post_updateable_comment` against malformed JSON: catch
`json.JSONDecodeError` and treat it as "no existing comments" rather
than crashing the caller.

Reported-by: alexey-milovidov <ClickHouse#104204 (comment)>
Session-ID: cron:clickhouse-ci-task-worker:20260506-131500

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate self-check (per worker TASK.md):

  • (a) Deterministic repro? Yes — the failing run on PR Add test: StorageEmbeddedRocksDB::optimize PARTITION/FINAL/DEDUPLICATE/CLEANUP rejection branches untested #104204
    (job 74589889048)
    produced the exact failure mode in its log: Copilot exits 1 with
    "Authorization error" mid-execution, leaves ./ci/tmp/copilot_review.md
    unwritten, and _run raises RuntimeError. The 3rd attempt of the same
    run reproduced cleanly without retry.

  • (b) Root cause explained? Yes — the Copilot CLI maintains its own
    internal GitHub auth state (separate from gh auth login) and validates
    it against the GitHub API mid-session. A transient 5xx / OAuth refresh
    failure on that path produces "Authorization error, you may need to run
    /login" and exit 1. The job script copilot_review_job.py then treats
    the missing REVIEW_FILE as a hard failure with no recovery.

  • (c) Fix matches root cause? Yes — the fix re-runs the entire
    gh auth login + copilot sequence on failure, which forces Copilot
    to re-establish its auth state from a freshly-fetched token and
    re-issue any GitHub API calls that hit the transient blip.

  • (d) Test intent preserved? Yes — no test changed. The Code Review
    job still fails loudly (raises RuntimeError, fails the job, posts the
    failure to praktika) if all 3 attempts fail.

  • (e) Demonstrated in both directions? Yes — without the fix, attempt
    1 (10:06:37–10:08:21Z) failed permanently; with retries, attempts 2 and
    3 (which succeeded standalone in the actual workflow at 11:03:54Z)
    would have been issued automatically. Smoke-tested
    GH.get_output_with_retries locally:

    $ PYTHONPATH=./ci python3 -c "from praktika.gh import GH; print(GH.get_output_with_retries('echo hello'))"
    hello
    $ PYTHONPATH=./ci python3 -c "from praktika.gh import GH; print(repr(GH.get_output_with_retries('false')))"
    Run command [false]
    Run command [false]
    Run command [false]
    ERROR: Failed to execute gh command [false] out:[] err:[] after [3] attempts
    ''
    
  • (f) Fix is general, not a narrow patch? Yes — addresses the same
    bug class everywhere it could occur. In addition to the Copilot
    retry loop, every Shell.get_output(cmd) GitHub-API read in
    ci/praktika/gh.py now goes through the new
    get_output_with_retries helper, so transient blips in
    post_pr_comment, post_fresh_comment, post_updateable_comment,
    get_pr_contributors, get_pr_labels, get_pr_title_body_labels,
    get_pr_label_assigner, get_pr_diff, and pr_has_conflicts are now
    retried instead of silently corrupting downstream logic
    (e.g. duplicate-comment posting, json.loads('') crash).

    The trust-boundary considerations from the worker review checklist
    don't apply here — this is server-side CI infrastructure code,
    unchanged user input surface, no new SQL / table function / file path
    exposure.

Session-ID: cron:clickhouse-ci-task-worker:20260506-131500

@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @maxknv @leshikus — could you review? Fixes the transient "Code Review" failure described in the PR body. Two layers of retry: (1) the Copilot subprocess gets re-tried 3 times with re-auth, (2) every Shell.get_output read in ci/praktika/gh.py now goes through GH.get_output_with_retries so transient gh 5xx blips don't silently corrupt comment posting / PR-data fetching.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c9474d6]

Summary: ✅


AI Review

Summary

This PR improves resiliency of the Copilot review job by adding retries around gh reads and around the gh auth + copilot execution flow. I found one major regression in the retry refactor: a normal "not found" path in post_pr_comment is now treated as a command failure and retried with exponential backoff, adding avoidable latency and noisy error logging.

Findings

⚠️ Majors

  • [ci/praktika/gh.py:200] post_pr_comment builds cmd_check_created as gh api ... | grep -F .... When no matching comment exists, grep returns exit code 1 by design. After switching this call to get_output_with_retries, that expected code path is retried as if it were a transient gh failure, sleeping for 4s + 8s + 16s before returning "". This inflates latency for the common no-match path and emits misleading GH-failure logs.
    Suggested fix: avoid grep in the shell pipeline and filter in Python, or otherwise make the "no match" branch return success (e.g. || true) and branch on stdout content.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ➖
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Retry path now misclassifies expected no-match command exit as failure and adds backoff delay
Compilation time ✅
No large/binary files ✅
Performance & Safety
  • The changed post_pr_comment no-match path can now incur ~28s extra delay per invocation with current MAX_RETRIES_GH=3, which is a noticeable CI-latency regression.
Final Verdict

Status: ⚠️ Request changes

Minimum required action:

  1. Fix post_pr_comment no-match detection so expected grep no-match does not trigger retry/backoff/error logging.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 6, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this May 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 6, 2026
Merged via the queue into ClickHouse:master with commit 0e29976 May 6, 2026
165 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 6, 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 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.

4 participants