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,7 @@
---
bump: patch
---

### Fixed

- `babysit_gh.py` counts the GitHub requests of every call that gives no header block (a binary artifact download, a subcommand other than `gh api`, a bare `--paginate`). In a job that names its token, such a call runs with `GH_DEBUG=1`, each request to a GitHub API URL becomes a `request <route>` line in the token's api-budget file, and stderr comes back without the debug lines, as `api_budget_traced` does for the shell wrapper. The job's api-calls notice no longer leaves those requests out.
2 changes: 1 addition & 1 deletion .github/workflows/pr-review-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ jobs:
if: always()
continue-on-error: true
timeout-minutes: 1
uses: melodic-software/ci-workflows/.github/actions/report-api-budget@2476d5dde8979eab34374150b7e4b0a2b99f4e88 # 2476d5d 2026-10-29
uses: melodic-software/ci-workflows/.github/actions/report-api-budget@09cf6f2ecd22f7188e62552dea9af48adbd23bb1 # 09cf6f2 2026-11-03
with:
tokens: github-token

Expand Down
54 changes: 52 additions & 2 deletions plugins/source-control/skills/babysit-prs/scripts/babysit_gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,27 @@ def _send(
kind = gh_call_kind(args, env_overrides)
_gh_call_counts[kind] = _gh_call_counts.get(kind, 0) + 1
include = not binary and _includes_headers(args)
token = _api_budget_token(env_overrides)
traced = guarded and not include and token is not None
proc = run_command(
["gh", "api", "-i", *args[1:]] if include else ["gh", *args],
allowed_executables=("gh",),
timeout_seconds=(
timeout_seconds if timeout_seconds is not None else _default_timeout_seconds
),
check=False,
env_overrides=env_overrides,
env_overrides={**(env_overrides or {}), "GH_DEBUG": "1"}
if traced
else env_overrides,
binary=binary,
)
status, headers = None, {}
if include:
status, headers, proc.stdout = _split_headers(proc.stdout)
token = _api_budget_token(env_overrides)
if token and status is not None:
_api_budget_write(token, status, headers, api_route(_endpoint(args)))
elif traced and token:
proc.stderr = _api_budget_trace(token, proc.stderr)
if guarded:
_guard_response(limits_pool, args, proc, status, headers, binary=binary)
return proc, headers
Expand Down Expand Up @@ -976,6 +981,51 @@ def _api_budget_line(
return f"{status} {' '.join(values)} {route}\n"


# api-budget.bash's api_budget_traced port. A call that gives no header block
# (a binary download, a subcommand other than `api`, a bare --paginate) runs
# with GH_DEBUG=1, which logs only head lines per request, never headers or
# bodies. Each request to a GitHub API URL (a redirect to a download host is
# not one) becomes a `request <route>` line in the token's file, so the job's
# api-calls notice counts it; the debug lines are then dropped from stderr.
_TRACE_API_URL = re.compile(r"https://((api|uploads)\.github\.com|[^/]+/api)/")
_TRACE_DROP = re.compile(r"\* (Using proxy|Skipping TLS verification): |\[git[] ]")


def _api_budget_trace(token: str, stderr: str) -> str:
"""stderr without what GH_DEBUG=1 adds, after appending one `request`
line per API request to the token's file. Never raises on the write."""
kept: list[str] = []
routes: list[str] = []
open_requests = 0
last = ""
for line in stderr.splitlines():
if line.startswith("* Request at "):
open_requests += 1
elif line.startswith("* Request took "):
open_requests = max(open_requests - 1, 0)
elif line.startswith("* Request to "):
parts = line.split()
if len(parts) >= 4 and _TRACE_API_URL.match(parts[3]):
routes.append(api_route(parts[3]))
elif _TRACE_DROP.match(line) or (open_requests and line.startswith("* ")):
pass
elif line.startswith("lookup ") and last.startswith("error connecting to "):
last = ""
else:
kept.append(line)
last = line
if routes:
try:
path = os.path.join(os.environ["RUNNER_TEMP"], "api-budget")
os.makedirs(path, exist_ok=True)
with open(os.path.join(path, token), "a", encoding="utf-8") as handle:
handle.writelines(f"request {route}\n" for route in routes)
except OSError:
pass
text = "\n".join(kept)
return text + "\n" if text and stderr.endswith("\n") else text


# A reading's route, for the job-end api-routes notice: rate-guard.mjs's
# routeOf, whose REST_PATH_WORDS this list is (test_babysit_gh_api_budget.py
# checks both against rate-guard.vectors.json's routes). A URL on a host other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ def test_a_call_with_its_own_token_reads_into_the_override_name(self) -> None:
)
self.assertFalse(pathlib.Path(self.dir, "api-budget", "lanes-app").exists())

def test_without_a_token_name_or_for_a_non_api_call_nothing_is_read(self) -> None:
def test_without_a_token_name_or_for_a_non_api_call_no_reading_is_written(
self,
) -> None:
# Every `api` call but a paginated one still runs with -i, for the
# guard, and a `--paginate --slurp` read goes page by page with -i
# (`_run_pages`); only a named token writes a reading.
# (`_run_pages`); only a named token writes a reading. A non-api call
# writes `request` lines only for the trace its stderr carries, none here.
self.run_gh(
"{}", ["api", "x"], env={"RUNNER_TEMP": self.dir, "API_BUDGET_TOKEN": ""}
)
Expand Down Expand Up @@ -194,6 +197,86 @@ def test_without_a_token_name_or_for_a_non_api_call_nothing_is_read(self) -> Non
self.assertEqual(self.calls[-1], ["gh", "api", "rate_limit"])
self.assertFalse(pathlib.Path(self.dir, "api-budget").exists())

def test_a_call_without_a_header_block_counts_each_api_request_it_traces(
self,
) -> None:
# api-budget.bash's api_budget_traced: GH_DEBUG=1 head lines become
# `request <route>` lines, a redirect to a download host is not one,
# and stderr comes back without what the debug level added.
trace = (
"* Request at 2026-11-04 04:00:00\n"
"* Request to https://api.github.com/repos/o/r/actions/runs/7/artifacts?per_page=100\n"
"* Request took 80ms\n"
"* Request at 2026-11-04 04:00:01\n"
"* Request to https://api.github.com/repos/o/r/actions/artifacts/9/zip\n"
"* Request took 90ms\n"
"* Request at 2026-11-04 04:00:02\n"
"* Request to https://productionresultssa1.blob.core.windows.net/x\n"
"* Request took 30ms\n"
"[git remote -v]\n"
"error connecting to api.github.com\n"
"lookup api.github.com: no such host\n"
"real error\n"
)
seen: list[dict[str, Any]] = []

def fake(argv: list[str], **kwargs: Any) -> subprocess.CompletedProcess[str]:
seen.append({"argv": argv, "env": kwargs.get("env_overrides")})
return subprocess.CompletedProcess(argv, 1, "", trace)

env = {"RUNNER_TEMP": self.dir, "API_BUDGET_TOKEN": "github-token"}
with (
mock.patch.dict(os.environ, env, clear=False),
mock.patch.object(gh, "run_command", fake),
):
proc = gh._run_gh(
["run", "download", "7", "-n", "a"], timeout_seconds=None, check=False
)
binary = gh._run_gh(
["api", "repos/o/r/actions/artifacts/9/zip"],
timeout_seconds=None,
check=False,
binary=True,
)
self.assertEqual(seen[0]["env"], {"GH_DEBUG": "1"})
self.assertEqual(
seen[1]["argv"], ["gh", "api", "repos/o/r/actions/artifacts/9/zip"]
)
self.assertEqual(
proc.stderr, "error connecting to api.github.com\nreal error\n"
)
self.assertEqual(binary.stderr, proc.stderr)
self.assertEqual(
self.readings("github-token"),
(
"request repos/{repo}/actions/runs/{n}/artifacts\n"
"request repos/{repo}/actions/artifacts/{n}/{x}\n"
)
* 2,
)

def test_without_a_token_name_a_header_less_call_runs_untraced(self) -> None:
seen: list[Any] = []

def fake(argv: list[str], **kwargs: Any) -> subprocess.CompletedProcess[str]:
seen.append(kwargs.get("env_overrides"))
return subprocess.CompletedProcess(
argv, 0, "", "* Request to https://api.github.com/x\n"
)

with (
mock.patch.dict(
os.environ,
{"RUNNER_TEMP": self.dir, "API_BUDGET_TOKEN": ""},
clear=False,
),
mock.patch.object(gh, "run_command", fake),
):
proc = gh._run_gh(["pr", "view", "1"], timeout_seconds=None, check=False)
self.assertEqual(seen, [None])
self.assertEqual(proc.stderr, "* Request to https://api.github.com/x\n")
self.assertFalse(pathlib.Path(self.dir, "api-budget").exists())

def test_each_page_of_a_paged_read_is_a_reading_under_its_route(self) -> None:
env = {"RUNNER_TEMP": self.dir, "API_BUDGET_TOKEN": "github-token"}
page = HEAD.format(status=200, remaining=9, used=1).replace(
Expand Down
Loading