Skip to content

fix(github): GraphQL mutations and implicit-POST gh api writes get the rate limiter's mutation pacing, decided from the request gh sends #1125

Closed

Description

@dan-petty

Problem

Lines are at 132ecc5 on release/v0.2.25. At the branch head d37120b, the cited lines of config/constants.py moved down 10 and those of config/defaults.py moved down 5; every other cited line is unchanged. Paths are under src/devops_cli/ unless they start with tests/; a bare :N is a line of github/rate_limiter.py.

run_gh gives a request GitHub's write pacing only when _is_mutation_command (:498-509, called at :1919) says it is a write. Then _calculate_target_delay (:976-981) raises its wait to at least DEFAULT_GH_MUTATION_MIN_INTERVAL_SECONDS (1.0 s, config/defaults.py:773). That interval is GitHub's advice in "Best practices for using the REST API": when making many POST, PATCH, PUT or DELETE requests, wait at least one second between each. It is the limiter's only write-specific guard against GitHub's secondary limits: 80 content-generating requests a minute and 500 an hour, counted across REST, GraphQL and the web interface, and 2,000 GraphQL points a minute, where a request with a mutation costs 5. The classifier reads argv tokens only:

  • A high-level gh command is a write when any token after the first is in CONST_GH_MUTATION_VERBS (config/constants.py:2156-2178).
  • A gh api call is a write only when it has an explicit method token of POST, PUT, PATCH or DELETE (_is_api_mutation, :484-495). It misses -X=POST, a form gh accepts.

An offline run of _is_mutation_command at 132ecc5:

Request What gh sends Answer today
api graphql -f 'query=mutation ResolveThread(...) {...}' a GraphQL mutation read
api graphql --input -, with {"query": "mutation(...) {...}"} on stdin a GraphQL mutation read
api repos/o/r/pulls -f title=t -f head=h POST (implicit) read
api repos/o/r/rulesets --input file.json POST (implicit) read
project link 3 --owner o --repo o/r a write read
issue list --label sync a read write
api -X POST repos/o/r/issues -f title=t POST write

gh's rule is in gh api --help (gh 2.102.0): "The default HTTP request method is GET normally and POST if any parameters were added. Override the method with --method." gh's source (pkg/cmd/api/api.go at tag v2.102.0, line 329) applies the same switch to --input: if !opts.RequestMethodPassed && (len(params) > 0 || opts.RequestInputFile != "") { method = "POST" }. A gh api graphql call that carries a query therefore always POSTs, and whether it writes depends on the operation in the document, which nothing reads.

These writes skip the interval today:

  • github/pr_threads.py: _run_graphql_query (:72-87) sends the addPullRequestReviewThreadReply, resolveReviewThread and unresolveReviewThread mutations (:146-180). resolve_all_pr_review_threads (:331-353, devops pr threads resolve-all) sends one resolve per thread, back to back.
  • github/projects.py: _create_project_view (:1335-1347) and _rename_default_view (:1350-1362) send createProjectV2View and updateProjectV2View.
  • roadmap/github_store.py: _graphql (:1456-1461) sends createProjectV2Field and updateProjectV2Field (:521-529) as gh api graphql --input -. The document is only in run_gh's input, which the classifier never receives. The same module's project link (:1008) is a write missing from the verb list.
  • commands/pr.py: _fallback_create_pr (:851-895) opens the pull request with gh api repos/{owner}/{repo}/pulls -f title=... -f head=... (implicit POST, argv at :876-890). Its docstring says it runs when gh pr create fails, for example on GraphQL rate limits, which is exactly when pacing matters.

There are two smaller gaps in the same path:

  • The schedule is per resource. Each recognized write sleeps at least the interval before it goes out, so the writes one caller sends in sequence are at least 1 s apart, whatever their resource. Callers on different threads of one process are serialized per target only: _acquire_locked (:1014-1042) schedules from _next_allowed_time[target], so a REST write (core) and a GraphQL write (graphql) acquired at the same moment are scheduled for the same instant. GitHub's best practices also ask for serial, not concurrent, requests. _last_mutation_epoch is set at :1036, initialized at :762, and never read.
  • No test calls _is_mutation_command or checks the interval. _is_api_mutation is exercised only through the cache predicate (test_is_cacheable_api_call_rejects_all_mutations, tests/test_github_rate_limiter.py:779). rg -n "is_mutation|mutation_min_interval|_is_api_mutation" tests finds nothing.

Acceptance criteria

  • One classifier decides read or write from the request gh will send. run_gh passes it input, plus cwd for relative paths. For gh api:
    • The method is the explicit -X/--method value in any form gh accepts (-X POST, -XPOST, -X=POST, --method POST, --method=POST, any case).
    • With no explicit method, it is POST when any -f/--raw-field/-F/--field (attached forms included) or --input is given, and GET otherwise.
    • A REST request is a write when its method is POST, PUT, PATCH or DELETE.
    • A graphql request is a write when the operation it runs is a mutation. The document comes from the query field (-f query=..., -F query=..., or -F query=@<file> and -F query=@-, which gh reads from a file or stdin; -f never reads files) or from the query key of the --input body (stdin or a file). The operationName field or body key selects among several operations.
  • A table-driven test in tests/test_github_rate_limiter.py covers at least these cases:
    • every row of the table above, with the corrected answer;
    • a mutation after a # comment, and a mutation after a fragment definition;
    • a { ... } shorthand query (read);
    • a two-operation document whose operationName selects the query (read) or the mutation (write);
    • -F query=@<file> with a mutation in a temporary file (write), and -f query=@<file>, whose file is not read because gh sends the value as a literal string;
    • --method GET search/issues -f q=x and -XGET (read);
    • -H 'Accept: ...' with no field (read);
    • a document that does not parse, an --input body that is not JSON, an unreadable @file and an argv that does not parse (all writes).
  • High-level gh commands are classified by the verb in the verb position, not by any token. gh project link is a write, and gh issue list --label sync is a read. Every high-level read used in src today stays a read: issue list, label list, pr checks|diff|list|view, project field-list|item-list|list, release list|view, repo view, run list|view. Every high-level write used in src today stays a write, for example issue close|create, label create, pr close|create|edit|ready, project create|field-create|field-delete|item-add|item-create|item-delete|item-edit and release edit.
  • The interval holds across resources. With the clock frozen and time.sleep patched, a REST write and a GraphQL write acquired at the same instant are scheduled at least mutation_min_interval apart; today both are scheduled for the same instant. A GraphQL query acquired at that instant is paced by its quota alone. _last_mutation_epoch is either read by the scheduler or removed.
  • The call sites get the interval. The subprocess runner is patched and GitHubRateLimiter.acquire is spied. These acquire with is_mutation=True: resolve_all_pr_review_threads over three threads (tests/test_github_pr_threads.py), _fallback_create_pr (tests/test_pr_cmd.py), _create_project_view (tests/test_github_projects.py) and the roadmap store's field creation through run_gh (tests/test_roadmap_github_store.py). The threads' list query acquires with is_mutation=False.
  • Caching is unchanged. test_should_cache_predicates (tests/test_github_rate_limiter.py:273) and test_is_cacheable_api_call_rejects_all_mutations (:779) pass unedited.
  • GraphQL documents are read with graphql-core (see Key questions). It is exact-pinned in [project].dependencies like the other runtime dependencies, and uv.lock is updated. It is imported only when a graphql request is classified: python -X importtime -c "import devops_cli.github.rate_limiter" lists no graphql module.
  • The tests run offline. They start no real gh, open no socket (prevent_external_network_calls, tests/conftest.py:99) and do not really sleep. Each new test takes under 1 s (pytest --durations=0), and uv run devops ci passes within its 5-minute budget.
  • changelog.d/<issue>.md exists, CHANGELOG.md and docs/ROADMAP.md are untouched, and devops pr check-readiness passes.
  • Pending a person: on a throwaway pull request with four replied review threads, in a repository they own, time uv run devops pr threads resolve-all <number> -R <owner>/<repo> resolves all four and takes at least 3 s, the floor that 1 s between four writes sets.

Key questions

  • Should the classifier detect writes from the request, or should callers declare them? Proposed: detect them from the request. fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintained ratelimit package is removed #1118 sketches the other way: REST callers add --method POST, and GraphQL callers set a new run_gh keyword that is threaded through the roadmap store's GhRunner protocol. A declaration is the step every call site above already missed. devops gh api (commands/gh.py:1285-1297) passes user arguments that no caller can declare. The document is already in argv or in input, both of which run_gh holds, so no signature changes. That keeps the call sites that 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 rewrite free of a new keyword, and refactor(github): the github session owns transport, ledger, error classes and link pagination #983 keeps run_gh's call sites on their argv. Callers may still spell out --method POST for readability, but correctness no longer depends on it.
  • Should the GraphQL document be read with graphql-core or with a prefix check? Proposed: graphql-core, using graphql.parse and graphql.utilities.get_operation_ast. It is the reference Python port of GraphQL.js; 3.3.0 was released on 2026-09-27, lists Python 3.14 and has no dependencies. A "starts with mutation" check misreads a mutation that follows a comment or a fragment, and a multi-operation document selected by operationName. It is also the kind of hand-written string check the project replaces with libraries. If the dependency is declined, no string check replaces it: the GraphQL callers in src declare their writes the way fix(github): gh calls no longer stall silently for up to a minute behind a fixed 60-call window, and the unmaintained ratelimit package is removed #1118 sketches, and an undeclared gh api graphql request, such as one from devops gh api, counts as a write. Pending a person: approval of the new runtime dependency.
  • How should gh's argv be parsed? Proposed: with the standard library's argparse, declaring every flag gh api --help lists and using parse_known_args for the rest. No library parses gh's flags. On Python 3.14, argparse accepts the attached, = and combined forms that gh's flag parser accepts (-XPOST, -X=POST, --method=POST, -fkey=v, -iXPOST). The parser is built with allow_abbrev=False, because gh accepts no abbreviated long flags, and exit_on_error=False, so a malformed argv raises argparse.ArgumentError instead of exiting. An argv that fails to parse is a write.
  • Which way do uncertain cases fall? Proposed: they count as writes. A false write costs at most one second. A false read sends an unpaced write toward a secondary limit, and the backoff after that is 60 s to 300 s (DEFAULT_GH_SECONDARY_RATE_WAIT, DEFAULT_GH_SECONDARY_MAX_CAP, config/defaults.py:774-775). For high-level commands this means replacing the write-verb list with a read-verb set: view, list, status, checks, diff, show, item-list and field-list, which _is_cacheable_cli_read already uses (:1345-1359), plus the search group. Every other verb is a write. gh auth stays exempt from pacing (_is_rate_limit_exempt, :1440-1447), so auth token and auth status pay nothing. The known commands that change side are project link and commands with a verb-like option value.
  • Should the interval span resources and processes? Proposed: it should span resources, but not processes in this item. GitHub's advice covers mutative requests whichever API they use, its content-creation limits count REST and GraphQL together, and _last_mutation_epoch exists for this purpose but is never read. Sequential callers already get the spacing, because each write sleeps first; the gap is concurrent callers in one process. Cross-process spacing is a separate gap: the per-target schedule is not persisted either (_persist_to_disk_locked, :800-810). A shared write clock belongs with the per-identity session of 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.
  • Should the limiter count GitHub's secondary points or the hourly content-creation limit? Proposed: no. The 1 s interval keeps one process at 60 writes a minute, under the 80-a-minute limit. Counting points or the hourly 500 belongs to refactor(github): the github session owns transport, ledger, error classes and link pagination #983's ledger, if the throttle counters (feat(github): github api request and throttle counters for the devops-cli dashboard #814) ever show secondary-limit hits.
  • Is this a release fix? Proposed: no. No secondary-limit incident has been traced to these writes. The roadmap tooling places it.

Strategic alignment

Amended 2026-10-03: board reads stay within GitHub's GraphQL budget

The owner asked (2026-10-03) to "ensure that we aren't handling old closed items during these github iterations … to help manage the gh api limits". Measured the same day:

  • Two devops roadmap migrate attempts used 4,971 of the 5,000-point hourly GraphQL budget in about five minutes, roughly 2,500 points each. Board 2 holds 931 items: 662 issues and 269 pull requests, 651 of them closed.

  • The store reads the board with gh project item-list --limit N (roadmap/github_store.py:1308-1327), which fetches every field of every item and applies no filter. ProjectV2.items counts archived items by default: 932 in total, 931 with archivedStates: [NOT_ARCHIVED], and 280 with query: "is:open". Archiving therefore does not shrink the read.

  • gh api rate_limit (REST) reported 4,899 GraphQL points left while GraphQL's own rateLimit{remaining} was 17.

  • Every board read passes a Projects filter at the source, such as gh project item-list --query "is:open is:issue" or items(query:, archivedStates: [NOT_ARCHIVED]). Each job declares the filter it needs; migrate's one-time reads may include closed items. The fail-closed count check compares against the filtered total, and no full listing is filtered by hand.

  • Before a board read, a run asks GraphQL for rateLimit{remaining} and stops with a clear message, spending nothing, when the remaining budget is below the read's measured cost.

  • A count that changes during a read is re-read once before the run fails closed. A count of 931 against 932 stopped a migrate run on 2026-10-03.

  • A test pins the GraphQL cost of a reprioritize run against a fixture board of 931 items, 280 of them open.

  • Archived items never fail the read. gh project item-list leaves archived items out, but the total the store checks against counts them. On 2026-10-04 one archived item made migrate stop with "Read 930 of 931 board feat(release): v0.1.8 #2 items" twice. The check compares the read against the total for the same filter (archivedStates: [NOT_ARCHIVED] or the same --query), and a test archives one fixture item. The board's auto-archive workflow (is:closed updated:<@today-1w) stays off until this lands.

Amended 2026-10-04: the limiter charges GraphQL points, and dry runs are cheap

The owner (2026-10-04): "if you used request quota, then it's not really a dry run … I bet the dry run even bypasses the rate limit".

  • The limiter counts calls, not points. The roadmap store sends every call through run_gh (roadmap/github_store.py:669), and run_gh charges 1 per gh invocation (github/rate_limiter.py:1561, _post_process_run(..., cost=1)). gh project item-list --limit N is one invocation, but it pages the board internally with GraphQL queries that request every field of every item. devops roadmap reprioritize --dry-run spent 2,025 GraphQL points on 2026-10-04 (5,000 before, 2,975 after) while the limiter recorded 1. The limiter cannot see the cost, so it cannot pace or refuse it.

  • A dry run costs as much as a real run. It writes nothing but reads the whole board.

  • Board and other GraphQL reads go through the store's own paged GraphQL query, not gh project item-list. Each page requests rateLimit { cost remaining resetAt }, charges the limiter the reported cost, and stops before a page when remaining is below a budget floor (a named constant).

  • run_gh charges GraphQL commands by points where the response reports them. A gh command that can't report its cost (for example gh project …) is not used for reads that page.

  • A dry run makes no request at all (feat(cli): reachable --dry-run previews for delegated commands #412, owner's definition of 2026-10-04). Every real run, and every read-only preview (--plan, feat(cli): reachable --dry-run previews for delegated commands #412), ends with one line giving the GraphQL points spent and the points left, read from GraphQL itself.

  • A test with a fake runner pins the charged cost of a fixture board read and the refusal below the floor.

Activity

  1. added
    type/bug
    scope/github
    priority/p2-medium
    on Oct 3, 2026
  2. added this to the v0.2.26 milestone on Oct 4, 2026
  3. added
    priority/p1-high
    and removed
    priority/p2-medium
    on Oct 4, 2026
  4. dan-petty commented on Oct 4, 2026

    @dan-petty
    OwnerAuthor

    Closed by #1166, merged into release/v0.2.26.

    GitHub only acts on closing keywords when a pull request merges into the default branch, so this was closed by devops gh issues close-merged.

  5. added a commit that references this issue on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/githubtype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions