Repository navigation
Description
Problem
Lines cited at dcafadf. A bare :N is a line of src/devops_cli/github/rate_limiter.py. Other paths are under src/devops_cli/ unless they start with tests/ or docs/, name a top-level file, or start with fastmcp/, anyio/, ratelimit/ or aiolimiter/, which are inside those locked packages.
At 132ecc5 these cited lines moved, and every other citation is unchanged: config/defaults.py:764 is 773, config/constants.py:2156-2177 is 2156-2178, README.md:364 is 365, docs/CLI_REFERENCE.md:6038 is 6055, ai/mcp/server.py:1678 and :2136 are 1701 and 2159, commands/pr.py:874-889 is 876-891, and in roadmap/github_store.py :91 is 109, :346-354 is 521-529 and :936 is 1458.
run_gh passes every gh process it starts through a burst ceiling built on ratelimit (github/rate_limiter.py:31 and :59-83, called at :1546, :1642 and :1750). The ratelimit==2.2.1 pin (pyproject.toml:43) has been abandoned upstream:
- Its last PyPI release, 2.2.1, was uploaded 2018-12-17 (
uv.lock:2808-2812). The tomasbasham/ratelimit repository has had commits since then (the latest on its default branch is from 2025-05-31) but no release. - It ships only an sdist, with no wheel (
uv.lock:2812). It is one of two sdist-only packages inuv.lock; the other is actionlint-py, in thedevgroup (pyproject.toml:74). Auv syncwith an empty cache builds it from source. - It ships no type information. The module therefore carries
# type: ignore[import-untyped](:31) and twountyped-decoratorignores (:62-63), and each call site wraps its result in acast(:1544,:1640,:1748). Those three casts are the only uses of thecastimported at:27.
What the ceiling does:
limits(calls=60, period=60)undersleep_and_retryis a fixed window of 60 calls per 60 s. It is held in memory per process, and its first window opens when the module is imported. The 61st gh start in a window sleeps until the window ends. In an offline probe (patched runner, no gh, no network), the 61st call asked for a sleep of 59.1–59.8 s, the rest of the window that had opened at import.- That sleep is silent. Nothing logs it, and
record_throttle(:812-817) does not count it, sodevops gh rate-limit(commands/gh.py:1489-1490) never shows it. The ledger logs its own pauses (:1005-1009). - It does not bound bursts: when a window closes after 60 starts, the next 60 can follow at once.
- It counts calls the ledger exempts by design:
gh auth,version,helpandgh api rate_limit(config/constants.py:871-879,github/rate_limiter.py:1440-1447). It also counts every retry. - For a short-lived command it is per invocation. Each
devopsrun starts a fresh window, so it never limited callers that run many short commands. The state shared across processes is the ledger's quota file, which is guarded by a file lock (:213-267,:983-1012). - The MCP server is different: one long-lived process runs many commands, so the window carries across them:
- 36 MCP tools and 4 resources run
devops gh …ordevops pr …(ai/mcp/server.py, for examplegh_issue_listatai/mcp/server.py:1678andpr_thread_resolveatai/mcp/server.py:2136). They go through_run_mcp_cmd(ai/mcp/server.py:64-95), which hands them toInProcessDispatcher.dispatchand from there to Typer'sCliRunner, inside the server process (ai/mcp/dispatcher.py:186-251). - FastMCP 3.4.7 starts each incoming request in its own task (
fastmcp/server/low_level.py:257-268). It runs sync tools in anyio worker threads (fastmcp/utilities/async_utils.py:9,34), at most 40 at once by anyio's default (anyio/_backends/_asyncio.py:3162, anyio 4.15.1). gh calls from parallel tool calls can therefore overlap. - The ceiling caps the whole server at 60 gh starts per window, and it stalls whichever tool call makes the 61st start.
- 36 MCP tools and 4 resources run
- It contradicts the module's stated contract, "No initial quotas, no hardcoded default windows, default request rate, or bursting" (
:3-6). The 60/60 is a literal, and no setting changes it.
What already paces gh without the ceiling:
-
GitHubRateLimiter.acquire(:983-1012) spaces every non-exempt request by time until reset divided by remaining requests for its resource (:935-974,calculate_request_delayat:412-434). With a full 5,000-request core quota and the reset 60 minutes away, requests are 0.72 s apart. -
That spacing holds across threads in one process.
_acquire_locked(:1014-1042) books each start after the previous booking for its resource (:1023-1033) while holdingself._lock, and the sleep happens outside the lock (:1004-1010). In an offline probe, two threads released together, each callingrun_gh, slept 0.72 s and 1.44 s (5,000 remaining, reset 3,600 s away). In the MCP server, this is what spaces gh starts once the ceiling is gone. Secondary-limit answers get the backoff below, andrecord_throttlecounts them. -
It waits at least
DEFAULT_GH_MUTATION_MIN_INTERVAL_SECONDS = 1.0(config/defaults.py:764) before a mutation (:979-980). GitHub's "Best practices for using the REST API" page asks for "at least one second between each" mutative request. The wait applies only to writes that_is_mutation_command(:498-509) recognizes: a mutation verb such ascreateorresolve(config/constants.py:2156-2177), or an explicit-X/--methodwith POST, PUT, PATCH or DELETE (:484-495). An offline probe confirmed that it misses two kinds of write:- GraphQL mutations sent with
gh api graphql. These are the thread reply, resolve and unresolve (github/pr_threads.py:146-179, sent atgithub/pr_threads.py:260-295);resolve_all_pr_review_threads(github/pr_threads.py:331-354) runs the resolve once per thread. They also include the view writes ingithub/projects.py:1335-1362and the field writes inroadmap/github_store.py:346-354, sent atroadmap/github_store.py:936. - REST writes that rely on gh's documented default of POST when fields are given, such as pull request creation in
commands/pr.py:874-889.
For these writes the ceiling was the only per-process cap, at 60 per window. After this item they are paced by quota spacing alone.
- GraphQL mutations sent with
-
After a rate-limit error it returns a server
Retry-Afterunchanged, waits for the reset when the primary quota is exhausted, and otherwise waits a 60 s floor plus tenacity'swait_random_exponential(:646-716,:1178-1202,:1778-1803, from feat(cli): correct secondary-limit backoff and retry-after handling inrun_gh#705). This is what the same page's "Handle rate limit errors appropriately" section prescribes. -
GitHub's documented secondary limits ("Rate limits for the REST API"), set against a serial client:
- No more than 100 concurrent requests. A serial client makes one at a time, and the MCP server makes at most 40.
- 900 points a minute per REST endpoint and 2,000 for GraphQL, where a read costs 1 point and a write 5. A serial reader would need a read every 67 ms to reach 900. On the development container,
gh --versionalone took 0.10–0.17 s (median 0.11 s over 20 runs, gh 2.102.0, load1 about 11 on 16 CPUs). Earlier runs on the same container took 0.21–0.44 s and 0.31–0.83 s. A serial caller therefore starts at most about 600 gh processes a minute before any network round trip. Recognized writes are at least 1 s apart: at most 60 a minute, or 300 points. - No more than 90 s of CPU time per 60 s of real time, which GitHub says can be estimated from total response time. A serial client's response times cannot add up to more than 60 s a minute.
- No more than 80 content-generating requests a minute. Recognized writes stay at 60 or fewer.
- No more than 500 content-generating requests an hour. Neither the ceiling (up to 3,600 calls an hour) nor the ledger (a 1 s interval allows 3,600 an hour) bounds this, so removing the ceiling changes nothing here. A hit gets the secondary backoff.
-
19 modules use
run_gh: 18 import it, andcommands/release.pyloads it lazily (commands/release.py:47). None of them starts a thread pool or task group. The oneasyncio.to_threadroute,run_gh_async(:1944-1980), has no caller.
The ceiling binds in two cases:
- A single command makes more than 60 gh calls in a minute while the ledger's spacing is short (plenty of quota, reset close). It stalls the command silently, though GitHub's limits would not have been reached.
- In the MCP server, tool calls together pass 60 gh starts in a window. It stalls one tool call silently, whichever made the 61st start.
The existing dependencies cover these jobs:
- aiolimiter 1.3.0 (
pyproject.toml:44) is async-only:AsyncLimiter.acquireis a coroutine tied to an event loop (aiolimiter/leakybucket.py:129). Its only use is the hardcodedAsyncLimiter(max_rate=60, time_period=60)inget_async_limiter(:775-780), which serves onlyrun_gh_async. - tenacity 9.1.4 retries after a failure and already supplies the backoff above. It has no limiter that acts before a request.
- PyGithub 2.10.0 throttles its own requests by default:
seconds_between_requests=0.25,seconds_between_writes=1.0, andretry=GithubRetry()with a 60 s secondary wait.GitHubClientbuilds it with those defaults (github/client.py:48). This covers only calls made through PyGithub, which never passed the ceiling, and it cannot pace gh processes. - GitHub's
x-ratelimit-*andRetry-Afterheaders already feed the ledger when gh prints them (:588-643), and they feed the backoff (:609-621).
Nothing new is needed to replace the ceiling. Once it is deleted, gh is paced by the ledger and the backoff, which together follow GitHub's documented guidance for every write the ledger recognizes.
Out of scope:
- The module's hand-written pagination and parsing. That covers
_run_gh_paginatedand its helpers (:1499-1669), header-line andRetry-Afterparsing (:565-621), JSON extraction from mixed output (:388-409) and rate-limit message matching (:1164-1176). It is a separate, later family: refactor(github): the github session owns transport, ledger, error classes and link pagination #983 replaces the page loop withLinkpagination and the string matching with one classifier. - Recognizing the writes
_is_mutation_commandmisses (see Key questions).
Acceptance criteria
-
Pending a person: the owner confirms the two key questions marked (owner decision) before work starts. The criteria below assume the proposed answers.
-
ratelimitis gone. Its line atpyproject.toml:43and itsuv.lockentries (uv.lock:664,uv.lock:730,uv.lock:2808-2812) are removed viauv lock.rg -n "ratelimit import|import ratelimit|_burst_protected_subprocess|_BURST_LIMIT_DECORATOR" src testsprints nothing. -
The three call sites (
:1546,:1642,:1750) callrun_subprocess(core/process.py:218-231, which returnsCompletedProcess[str]) with the same arguments and nocast, andcastleaves thetypingimport at:27. Thetype: ignorecomments at:31and:62-63are removed, and mypy passes with no new ignore. -
No gh start loses pacing. Every non-exempt start still passes
GitHubRateLimiter.acquire. That includes the--paginatefallback with no endpoint (:1639-1645), which today passes only the ceiling. Test: with a patched runner, that fallback callsacquireonce before starting gh. -
No minute-long stall. A unit test in
tests/test_github_rate_limiter.py:- sets
update_quota("core", remaining=5000, limit=5000, reset_epoch=time.time() + 60), replacing the autouse fixture's 3,600 s (tests/test_github_rate_limiter.py:31-40); - patches
run_subprocesswith a runner that returns success; - patches
time.sleepwith a fake that records each request and raisesAssertionErroron any request of 1 s or more; - sends 61
run_ghreads and asserts 61 runner calls.
Before the change the test fails at once: the first call over 60 in ratelimit's window makes
sleep_and_retryask the fake for the rest of that window (59.1–59.8 s, measured). After the change it passes, and the largest recorded sleep is the ledger's own spacing, 60 s / 5,000, accumulated over 61 calls (0.67–0.72 s in the probes, under the 1 s limit). The setup matters:- With the fixture's 3,600 s and a clock that the fake sleep does not advance, the ledger's bookings add up to 44 s, and 60 of the 61 sleeps are 1 s or more. Use the 60 s reset above, or a fake
time.timethat each recorded sleep advances (largest sleep 0.72 s). - Patching
time.monotonichas no effect before the change, because ratelimit binds it at import (ratelimit/decorators.py:11, used as the default clock atratelimit/decorators.py:17). - A recording sleep that never raises makes
sleep_and_retry(ratelimit/decorators.py:115) retry without end.
- sets
-
Parallel callers stay spaced. In a second test, two threads released together by a
threading.Barriereach callrun_ghonce, with the fixture's quota (5,000 remaining, reset 3,600 s away) and a recordingtime.sleep. The test asserts that the larger recorded sleep exceeds the smaller by at least 0.5 s (0.72 s and 1.44 s in the probe). The test passes before and after the change. It pins the cross-thread spacing that the MCP server relies on once the ceiling is gone. -
The existing pacing,
Retry-After, secondary-floor and mutation-interval tests intests/test_github_rate_limiter.pypass unchanged. -
The sentences that call
run_gh's pacing a token bucket are changed to describe what remains: quota-derived spacing, the mutation interval and the backoff. They are atlang/en/help.py:746,commands/gh.py:1284,AGENTS.md:79andAGENTS.md:369.README.md:364,docs/commands/gh.md:9anddocs/CLI_REFERENCE.md:6038repeat the help string.uv run devops docs generaterewrites them, anduv run devops docs checkpasses.run_gh_async's docstring (:1959) goes away with refactor(github): the github session owns transport, ledger, error classes and link pagination #983. -
The end of the silent stall of up to a minute in gh commands under
### Fixed. -
Gate:
- The tests are offline and start no gh. The 61-call loop ran in 0.02–0.08 s when measured, and the new tests take well under 1 s together. No gate time is added.
- The full suite at 132ecc5 (dcafadf plus feat(github): current release admission, cap, descoping and stall rules (
devops roadmap reprioritize) #740, which leavesgithub/rate_limiter.pyunchanged) sends 48 calls through the ceiling: 8,382 tests on 16 xdist workers, at most 15 calls in one worker. The ceiling never raised, so the gate never reached it. uv run devops cipasses.uv syncinstalls one package fewer and builds one sdist fewer.
Key questions
-
What replaces the ceiling? Proposed: nothing new. The ledger's quota spacing and mutation interval, together with the
Retry-After, primary and secondary backoff, are GitHub's documented guidance, and they already run on every non-exempt gh start. No dependency is added. -
Should a fixed per-minute ceiling be kept anyway? (owner decision) Proposed: no. If one is wanted, the ledger already has the setting:
GitHubRateLimiter(min_interval=...)(:731, applied at:974), set from a newDEFAULT_GH_*constant inget_github_rate_limiter(:1327-1335). At 1.0 s it would give today's 60 calls a minute as even spacing, with no burst and no new code path. Because the ledger is shared by every thread in a process, it would also space the MCP server's parallel tool calls. The cost is up to 1 s per call in every command. -
Does the MCP server need a cap of its own once the ceiling is gone? Proposed: not in this item. Its tool calls share one ledger, which books gh starts in sequence across threads, and anyio runs at most 40 tool calls at once, below GitHub's 100 concurrent requests. GitHub's best-practice page asks clients to send requests serially. When tool calls overlap, neither the ceiling nor the ledger does that: both act on starts, not on requests in flight. If the throttle counters show secondary-limit hits from the server, the
min_intervalsetting above is the first step. -
Do the writes
_is_mutation_commandmisses lose protection? (owner decision) Proposed: they lose the ceiling's cap of 60 per window per process, and nothing else. They never had the 1 s interval, so the gap is in mutation detection and predates this item. A loop such asresolve_all_pr_review_threadsover more than 80 threads can now pass GitHub's 80 content-generating requests a minute, if round trips are short and the quota spacing is small. The secondary-limit answer then gets the backoff and is counted. Today the ceiling stalls such a loop silently at its 61st start. Closing the gap is proposed as a separate item:- REST writes need no new detection. gh documents POST as the default when fields are given, so the callers that rely on it, such as
commands/pr.py:874-889, pass--method POSTexplicitly, which_is_mutation_commandalready recognizes (:484-495). - GraphQL mutations need the caller to say it sends one, through a
run_ghkeyword androadmap/github_store.py'sGhRunnerprotocol (roadmap/github_store.py:91). That touchesgithub/pr_threads.py,github/projects.pyandroadmap/github_store.py, which refactor(github): one github session per identity owning token, quota and transport #767 and refactor(github): the github session owns transport, ledger, error classes and link pagination #983 also rewrite.
This item does not depend on that one.
- REST writes need no new detection. gh documents POST as the default when fields are given, so the callers that rely on it, such as
-
Why not pyrate-limiter, as feat(cli): correct secondary-limit backoff and retry-after handling in
run_gh#705's roadmap entry proposed? Proposed: that P2 follow-up was never filed. It wanted one ceiling to replace bothratelimitandrun_gh_async'sAsyncLimiter. refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletesrun_gh_async, which has no caller, so only the synchronous side is left, and the ledger already paces it. pyrate-limiter 4.5.0 (2026-08-30) and limits 5.8.0 (2026-02-05) are the maintained candidates with synchronous support. One of them is worth adopting only if the throttle counters later show secondary-limit hits that a cross-process ceiling would prevent. Adopting one now would add a dependency to enforce a limit that GitHub does not set. -
Why not aiolimiter, tenacity or PyGithub's throttling? Proposed: none of them does this job. aiolimiter is async-only, and
run_ghis called synchronously from 19 modules (18 import it;commands/release.pyloads it lazily). tenacity reacts to failures and already supplies the backoff. PyGithub paces only its own HTTP requests. -
Do
run_gh_asyncand itsAsyncLimiter(60, 60)go in this item? Proposed: no. refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletesrun_gh_async(its criterion "run_gh_asyncis deleted"), and that removes aiolimiter's only import insrc. Limiting this item toratelimitkeeps the change to one module, the dependency manifests and the wording fix. -
How does this item interact with refactor(github): one github session per identity owning token, quota and transport #767, refactor(github): the github session owns transport, ledger, error classes and link pagination #983, feat(cli): extend the ruff rule set to the correctness families #420 and fix(deps): pyproject.toml declares every package src imports, and a deptry step in devops ci keeps it that way #1120? Proposed: it depends on none of them, and none depends on it.
- This change touches
:27,:31,:59-83,:1639-1645and the three call sites ingithub/rate_limiter.py. refactor(github): one github session per identity owning token, quota and transport #767, refactor(github): the github session owns transport, ledger, error classes and link pagination #983 and feat(cli): extend the ruff rule set to the correctness families #420 also edit that module; feat(cli): extend the ruff rule set to the correctness families #420's lines are:218and:1953. Whichever item lands later rebases those lines. If this item lands first, refactor(github): the github session owns transport, ledger, error classes and link pagination #983's new page loop can callrun_subprocessdirectly. - fix(deps): pyproject.toml declares every package src imports, and a deptry step in devops ci keeps it that way #1120 also edits
[project].dependenciesanduv.lock, and adds a deptry step todevops ci. Whichever of the two lands second re-runsuv lock. This item removes the import and the pin in the same change, so deptry reports neither DEP002 nor DEP001 forratelimit.
- This change touches
Strategic alignment
- AGENTS.md:25: "Avoid custom workarounds when standard library or established open-source tools exist." This item removes an unmaintained, untyped, sdist-only dependency. It adds no dependency and no hand-written limiter.
- P2: it produces no wrong result, but gh commands, and tool calls in the MCP server, can stall silently for up to a minute, and the dependency gets no releases.
- Proposed milestone: backlog (P2). A person plans it on the board. Landing it before refactor(github): the github session owns transport, ledger, error classes and link pagination #983 leaves refactor(github): the github session owns transport, ledger, error classes and link pagination #983 fewer lines to rebase.
- Related:
- feat(cli): correct secondary-limit backoff and retry-after handling in
run_gh#705 delivered the backoff that makes the ceiling unnecessary; this item is its unfiled P2 follow-up. - feat(cli): outward-facing capability & upstream maturity survey #425's roadmap extension listed "drop
ratelimitor seam a maintained limiter" among its survey-free fixes. - refactor(github): the github session owns transport, ledger, error classes and link pagination #983 deletes
run_gh_asyncand owns the pagination and parsing family. - refactor(github): one github session per identity owning token, quota and transport #767 and feat(cli): extend the ruff rule set to the correctness families #420 edit the same module.
- fix(deps): pyproject.toml declares every package src imports, and a deptry step in devops ci keeps it that way #1120 gates
[project].dependencieswith deptry and edits the same manifests. - feat(cli): service mode for
devops servewith github webhooks, a polling tick and per-repo job queues #752's planned service mode runs per-repo job threads that callrun_ghin one process. The ledger's cross-thread spacing, which this item's second test pins, applies to it the same way. - feat(github): github api request and throttle counters for the devops-cli dashboard #814 reads the ledger's request and throttle counters. After this item, every wait in
run_ghgoes through the ledger: a logged pause or a counted backoff.
- feat(cli): correct secondary-limit backoff and retry-after handling in
Update 2026-10-08: refinement
The pin and import remain (pyproject.toml:46, github/rate_limiter.py:35, :73-101). #1125 already treats GraphQL mutations and implicit POSTs as writes (rate_limiter.py:2099), so drop the missed-writes question; only the fixed-ceiling question waits on the owner.
-
test_paginated_read_fails_closed_when_exceeding_page_cap(tests/test_github_rate_limiter.py:804) runs under 1 s alone and in the gate (54-105 s today).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
dan-petty commentedon Oct 8, 2026
Evidence, 2026-10-08: the
ratelimitceiling of 60 calls per 60 s trips inside the fulldevops cirun.tests/test_github_rate_limiter.py::test_paginated_read_fails_closed_when_exceeding_page_cap(#1034) spun for 60.10 s in one gate run: pytest took 5m 21s and the gate 5m 53s, against the 5-minute budget. Run as a single file, it spins for 104.66 s at 3f1935a. Any test that patchestime.sleepto a no-op or a recorder, including #1364's boundary tests, busy-waits until the window ends once its xdist worker has made 60 gh calls in 60 s, and an exact sleep-list assertion would then fail. Removing the ceiling removes both the delay and the flake.dan-petty commentedon Oct 8, 2026
Evidence for this item's gate cost, from #1360's delivery (2026-10-08):
tests/test_github_rate_limiter.py::test_paginated_read_fails_closed_when_exceeding_page_cap(added by #1034, #1406) takes about 54 s when run alone. It already patchestime.sleepand sets the page cap to 2, so the time probably goes to the fixed 60-call window this item removes, waited on through a clock the test doesn't control. That one test accounts for much ofdevops ci's overrun of its 5-minute budget (CI gate speed is P0). Add to this item's criteria: that test's call phase is under 1 s. If this item isn't scheduled soon, the test should get a controlled clock first. Folded here instead of a new issue because the agent filing quota (#1153) is spent.dan-petty commentedon Oct 8, 2026
Linked: #983, #1415 and #1441. Pacing and quota accounting miss what GitHub charges; #983's session rewrites the same module, and each fix stays independent. Found by the 2026-10-08 backlog review.
chore(main): release 0.4.2 (#2191)