Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-25
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Design

## Decisions
- Extract `_failed_head_in_backoff(ctx: _WatchContext, *, pr_number: int, head_sha: str, base_ref: str) -> bool` in `src/devops_cli/roadmap/watch_row.py` to test whether `ctx.recorded_pulls[pr_number]` is a `FAILED` entry with matching `head` and `base` and `ctx.current_time < datetime.fromisoformat(entry.next_read_at)`, recording `pr_number` in `ctx.failed_heads`.
- Call `_failed_head_in_backoff` inside `_process_rest_pulls` after resolving `(item, is_plan)` and before `read_check_runs`; when true, increment `ctx.watched_count += 1` and `continue` so zero check-run requests run and `WatchRoundResult.watched` / `WatchRoundResult.failed_heads` keep their GraphQL semantics.
- Reuse `_failed_head_in_backoff` in `_evaluate_pull` right after `ctx.watched_count += 1` so both paths share one predicate.

## Rejected alternatives
- **Keep backoff check only in `_evaluate_pull`**: `read_check_runs` still fires before `_evaluate_pull` is called in `_process_rest_pulls`.
- **Pass a lazy check-run reader into `_evaluate_pull`**: complicates `_evaluate_pull` and its callers for no gain over a shared pre-check helper.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Proposal: #6004

## Why
During a GraphQL quota hold, `_process_rest_pulls` in `src/devops_cli/roadmap/watch_row.py` calls `read_check_runs` before checking the 30-minute failed-head backoff recorded in the watch ledger. As a result, failed heads inside their backoff window trigger a REST check-runs request on every watch tick.

## What Changes
- Extract a shared `_failed_head_in_backoff` helper in `src/devops_cli/roadmap/watch_row.py`.
- Check the backoff before calling `read_check_runs` in `_process_rest_pulls`, appending to `ctx.failed_heads` and incrementing `ctx.watched_count` just as `_evaluate_pull` does, and skipping `read_check_runs`.
- Reuse `_failed_head_in_backoff` in `_evaluate_pull` so GraphQL and REST share one backoff rule.
- Add a unit test in `tests/roadmap/test_roadmap_watch_row.py` proving zero check-run reads when a failed head is in backoff during a GraphQL quota hold.

## Capabilities
- `roadmap-watch-row`: Enforce failed-head backoff before REST `read_check_runs` calls during GraphQL quota holds.

## Impact
- Prevents wasted GitHub REST API requests during GraphQL quota holds when failed PR heads are already backed off for 30 minutes.

## Feasibility
- Confirmed on `origin/main` (`4046839ec`): `src/devops_cli/roadmap/watch_row.py:851` invokes `read_check_runs(owner, name, pull_status.head_sha)` before `_evaluate_pull` checks `entry.verdict == PRVerdict.FAILED.value` (`lines 404-411`). `openspec/specs/roadmap-watch-row/spec.md:23` already specifies zero check re-reads within 30 minutes unless head or base moves.

## Existing Tool Considered
No external library governs internal `WatchLedger` backoff timing or REST fallback ordering (alternatives check verdict `none`). Reuses the existing backoff check via one helper in `watch_row.py`; ~25 lines, ~30 minutes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## ADDED Requirements

### Requirement: REST fallback tests backoff before reading check runs
In the REST fallback path (`_process_rest_pulls`), `watch` SHALL test whether the pull request head and base match an active failed-head backoff entry in the watch ledger before calling `read_check_runs`, skipping check-run requests and recording the pull request in `failed_heads` while counting it in `watched`.

#### Scenario: Failed head in backoff under GraphQL hold triggers zero check-run reads
- **WHEN** GraphQL quota is on hold, `watch_round` falls back to REST for an open pull request whose ledger entry holds verdict `FAILED`, the same head SHA and base branch, and `next_read_at` in the future
- **THEN** zero REST requests are sent to `/check-runs`, the result includes the pull request number in `failed_heads`, and `watched` counts it
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tasks

## 1. Behavior

- [x] 1.1 Add failing test in `tests/roadmap/test_roadmap_watch_row.py` proving zero `/check-runs` requests for a backed-off failed head in REST fallback
- Verification: `$ uv run devops ci test tests/roadmap/test_roadmap_watch_row.py`
- [x] 1.2 Extract `_failed_head_in_backoff` in `src/devops_cli/roadmap/watch_row.py` and guard `read_check_runs` in `_process_rest_pulls`
- Verification: `$ uv run devops ci test tests/roadmap/test_roadmap_watch_row.py`

## 2. Documents

- [x] 2.1 Validate OpenSpec artifacts against `roadmap-watch-row` spec
- Verification: `$ uv run devops openspec check`
7 changes: 7 additions & 0 deletions openspec/specs/roadmap-watch-row/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,10 @@ A delivery draft is the draft of `implement.pr` that the ready path takes, with
#### Scenario: A round with no numbers reads the listing
- **WHEN** `watch_round` is called with no pull numbers
- **THEN** it reads the open pull requests with one GraphQL query, as the poll does

### Requirement: REST fallback tests backoff before reading check runs
In the REST fallback path (`_process_rest_pulls`), `watch` SHALL test whether the pull request head and base match an active failed-head backoff entry in the watch ledger before calling `read_check_runs`, skipping check-run requests and recording the pull request in `failed_heads` while counting it in `watched`.

#### Scenario: Failed head in backoff under GraphQL hold triggers zero check-run reads
- **WHEN** GraphQL quota is on hold, `watch_round` falls back to REST for an open pull request whose ledger entry holds verdict `FAILED`, the same head SHA and base branch, and `next_read_at` in the future
- **THEN** zero REST requests are sent to `/check-runs`, the result includes the pull request number in `failed_heads`, and `watched` counts it
36 changes: 28 additions & 8 deletions src/devops_cli/roadmap/watch_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,28 @@ class _WatchContext:
watched_count: int = 0


def _failed_head_in_backoff(
ctx: _WatchContext,
pr_number: int,
head_sha: str,
base_ref: str,
) -> bool:
"""Count and record `pr_number` and return True when its failed head is in backoff (#6004)."""
entry = ctx.recorded_pulls.get(pr_number)
if (
entry is not None
and entry.verdict == PRVerdict.FAILED.value
and entry.head == head_sha
and entry.base == base_ref
and entry.next_read_at
and ctx.current_time < datetime.fromisoformat(entry.next_read_at)
):
ctx.watched_count += 1
ctx.failed_heads.append(pr_number)
return True
return False


def _evaluate_pull(
ctx: _WatchContext,
*,
Expand All @@ -398,17 +420,12 @@ def _evaluate_pull(
) -> None:
"""Evaluate one roadmap pull request and apply lifecycle steps. `labels` are the labels of the
pull request: a verdict label other than `verdict/mergeable` holds the ready mark (#6024)."""
if _failed_head_in_backoff(ctx, pr_number, head_sha, base_ref):
return

ctx.watched_count += 1
has_live_unit = job_holds(item) or bool(item.job_record.get(StageRecordKey.JOB))

entry = ctx.recorded_pulls.get(pr_number)
if entry is not None and entry.verdict == PRVerdict.FAILED.value:
if entry.head == head_sha and entry.base == base_ref:
if entry.next_read_at:
next_time = datetime.fromisoformat(entry.next_read_at)
if ctx.current_time < next_time:
ctx.failed_heads.append(pr_number)
return

if mergeable is False and not is_draft and not is_plan:
ctx.total_conflicted += 1
Expand Down Expand Up @@ -848,6 +865,9 @@ def _process_rest_pulls(
continue
item, is_plan = resolved

if _failed_head_in_backoff(ctx, pr_number, pull_status.head_sha, pull_status.base_ref):
continue

checks = read_check_runs(owner, name, pull_status.head_sha)
pull_author = getattr(pull_status, "author", "") or ""

Expand Down
81 changes: 81 additions & 0 deletions tests/roadmap/test_roadmap_watch_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,3 +1337,84 @@ def test_watch_row_rest_fallback_and_closed_stage_plans(
get_github_rate_limiter().update_quota(
"graphql", remaining=5000, limit=5000, reset_epoch=time.time() + 600.0
)


def test_rest_fallback_checks_failed_head_backoff_before_reading_check_runs(
store: InMemoryRoadmapStore,
github_api: GitHubApiFake,
tmp_path: Path,
) -> None:
"""In REST fallback under GraphQL quota hold, a failed head inside its 30-minute backoff makes
zero REST check-run requests (#6004)."""
import time

from devops_cli.github.rate_limiter import get_github_rate_limiter

get_github_rate_limiter().update_quota(
"graphql", remaining=0, limit=5000, reset_epoch=time.time() + 600.0
)
try:
issue_num = store.seed_issue("Delivery Issue", fields={ItemField.PRIORITY: "P1-High"})
pr_num = store.seed_pull("feat: deliv", head_ref="feat/deliv")
pr_payload = {
"number": pr_num,
"state": "open",
"merged": False,
"draft": False,
"body": f"Closes #{issue_num}",
"head": {"sha": HEAD_FAIL, "ref": "feat/deliv"},
"base": {"ref": "main", "repo": {"default_branch": "main"}},
}
check_runs_payload = {
"check_runs": [
{"name": "Tests & Coverage", "status": "completed", "conclusion": "failure"}
]
}
github_api.route("GET", f"/repos/{REPO}/pulls/{pr_num}", github_json(pr_payload))
github_api.route(
"GET",
f"/repos/{REPO}/commits/{HEAD_FAIL}/check-runs?per_page=100",
github_json(check_runs_payload),
)
ledger_path = tmp_path / CONST_ROADMAP_WATCH_LEDGER_FILENAME

res1 = watch_round(store, repo=REPO, ledger_path=ledger_path, now=NOW)
tick1_check_runs = [r for r in github_api.sent if r.url.path.endswith("/check-runs")]
assert (res1.watched, res1.failed_heads, len(tick1_check_runs)) == (1, (pr_num,), 1)

github_api.route("GET", f"/repos/{REPO}/pulls/{pr_num}", github_json(pr_payload))
github_api.route(
"GET",
f"/repos/{REPO}/commits/{HEAD_FAIL}/check-runs?per_page=100",
github_json(check_runs_payload),
)
res2 = watch_round(
store, repo=REPO, ledger_path=ledger_path, now=NOW + timedelta(minutes=10)
)
all_check_runs = [r for r in github_api.sent if r.url.path.endswith("/check-runs")]

assert (res2.watched, res2.failed_heads, len(all_check_runs) - len(tick1_check_runs)) == (
1,
(pr_num,),
0,
)

github_api.route("GET", f"/repos/{REPO}/pulls/{pr_num}", github_json(pr_payload))
github_api.route(
"GET",
f"/repos/{REPO}/commits/{HEAD_FAIL}/check-runs?per_page=100",
github_json(check_runs_payload),
)
res3 = watch_round(
store, repo=REPO, ledger_path=ledger_path, now=NOW + timedelta(minutes=31)
)
tick3_check_runs = [r for r in github_api.sent if r.url.path.endswith("/check-runs")]
assert (res3.watched, res3.failed_heads, len(tick3_check_runs) - len(all_check_runs)) == (
1,
(pr_num,),
1,
)
finally:
get_github_rate_limiter().update_quota(
"graphql", remaining=5000, limit=5000, reset_epoch=time.time() + 600.0
)