Skip to content

fix(dispatcher): survive the rate limit and get its own API budget #296

Closed

Description

@Ardnived

Summary

Every dispatcher run and every cloud session draws on one GitHub REST budget: sydevs-bot's 5,000 calls an hour. On 2026-11-20 a batch of member mentions from 22:16Z grew into 486 dispatcher runs in one hour, and the budget ran out at 23:00Z. Every dispatcher run from then until the 23:10Z reset failed, because a 403 is never retried. One of them stranded the lease on sydevs/WeMeditateWeb#326. This ticket makes a rate-limited run wait for the reset, makes each run spend less, and moves the dispatcher onto a GitHub App with a budget of its own.

Approach

  1. Wait out a rate limit. installRetry (dispatcher/retry.mjs:33) already wraps every call the dispatcher makes, but retriable (retry.mjs:13) answers every 4xx at once. Add a rate-limit branch:
    • A 403 or 429 with x-ratelimit-remaining: 0 waits until x-ratelimit-reset.
    • A 403 or 429 with retry-after (a secondary limit) waits that many seconds.
    • Each waits only up to a new dispatch.githubRetry.rateLimitMaxWaitSeconds (900). A longer wait, or a 403 with neither header (a permission refusal), is thrown as today.
    • Raise timeout-minutes on resolve and act (.github/workflows/dispatcher.yml:54, :86) above that wait.
    • On 2026-11-20 the budget reset 10 minutes after it ran out, so every failed run, Fri — 0 dispatches · 0 failed · 0 anomalies #326's unlock among them, would have finished.
  2. Measure what a run spends. In the same request hook, count REST and GraphQL requests, and log api: <n> REST, <m> GraphQL at the end of each resolve run and each act leg.
  3. Cut the biggest cost, and shed load under pressure.
    • Each event run also adds up to dispatch.reconcileMaxPerEvent (10) drift targets (dispatcher/resolve.mjs:63-70), each a full gather (dispatcher/gather.mjs:218) in its own act leg, and in a burst many runs in one repo pick the same drifted items. Use step 2's counts to cut the largest cost first.
    • Whatever the measurement shows: resolve reads GET /rate_limit, which does not count against the limit. When REST remaining is under a new dispatch.rateLimitFloor (1,000), an event run returns only its own targets and logs that it shed the rest. The cron's full pass covers them.
  4. Give the dispatcher its own budget through a GitHub App.
    • sydevs is on the Free plan with 5 repos, so an App installation gets 5,000 an hour: no more than the PAT, but separate from it. Cloud sessions cannot act as an App (docs/routine-setup.md §0b). They stay sydevs-bot and keep its 5,000 to themselves.
    • Token. Each dispatcher.yml job that reads secrets.SYDEVS_BOT_PAT (lines 74, 133, 160, 204, 239, 260, 275, 405) mints an installation token with actions/create-github-app-token when the org variable SYDEVS_APP_ID and the org secret SYDEVS_APP_PRIVATE_KEY are set. When they are not, it uses the PAT. So this merges before the App exists, and the switch is a settings change either way.
    • Identity. The dispatcher's writes then come from <app-slug>[bot]. Add identity.dispatcherLogins to loop-config.json (both logins), and read it wherever the dispatcher finds its own writes: loadRecord (dispatcher/record.mjs:74), commentOnce markers, the own-unlock check, and isBot / fullTreatment (dispatcher/decide.mjs:69, :98) for a contract-sync PR the App opens. identity.expectedLogin stays the sessions' login.
    • Settle by experiment on one PR before switching on. Any call the App cannot make keeps the PAT, so that job holds both tokens:
      • whether an App's approving review satisfies the ruleset's required approval (mergePolicy.proxyApprove);
      • whether the App can edit a status comment sydevs-bot wrote;
      • whether it can write the org issue fields (Priority, Hold Until) and the board's Status;
      • whether it can push a relay branch that touches .github/workflows/;
      • whether any ruleset lists sydevs-bot as a bypass actor.
    • Setup, by a person, after the code merges. Create the App in the sydevs org with Issues, Pull requests, Contents and Workflows (read and write), org Projects (read and write) and Metadata (read). Install it on the five repos, and store SYDEVS_APP_ID and SYDEVS_APP_PRIVATE_KEY with visibility all. Document it beside SYDEVS_BOT_PAT in docs/routine-setup.md:286.
  5. Docs. Add docs/why.md sections on why a rate-limited call waits for the reset, and why the dispatcher has its own App: the budget split, and sessions cannot use one.

Acceptance criteria

  • A call answered 403 or 429 with x-ratelimit-remaining: 0 waits until x-ratelimit-reset and retries, when that is within rateLimitMaxWaitSeconds. Beyond it, the error is thrown as today.
  • A 403 or 429 carrying retry-after waits that long, within the cap, and retries.
  • A 403 with neither header is thrown at once, as today.
  • resolve and act time out later than the longest wait.
  • Every resolve run and act leg logs its REST and GraphQL request counts.
  • An event run whose REST budget is under dispatch.rateLimitFloor returns only its own targets, and logs that it shed the drift targets. Schedule and workflow_dispatch runs are unchanged.
  • The largest per-leg cost step 2 measures is cut, and the PR states the counts before and after.
  • With SYDEVS_APP_ID unset, every job uses SYDEVS_BOT_PAT exactly as today. With it set, every job that used the PAT uses the App token, except a call the experiment showed the App cannot make.
  • The dispatcher reads its own status comments, markers and PRs under either login in identity.dispatcherLogins, so an item written before the switch keeps its record.
  • docs/routine-setup.md names the App, its permissions and both settings, and docs/why.md records why.

Verification checklist

  • node --test dispatcher/test/*.test.mjs passes.
  • retry.test.mjs cases with a fake sleep:
    • 403 with x-ratelimit-remaining: 0 and a reset 30 s ahead → one wait of about 30 s, then success.
    • The same with a reset 2 h ahead → thrown with no wait.
    • 429 with retry-after: 5 → one 5 s wait.
    • 403 with no rate-limit headers → thrown at once.
  • A resolve.test.mjs case: an event run with /rate_limit reporting 500 remaining yields only the event's own targets, and with 4,000 it adds drift targets as today.
  • A record.test.mjs case: loadRecord finds a status comment written by either dispatcher login, and ignores one written by a member.
  • On a test item, a dispatcher run with SYDEVS_APP_ID unset logs the PAT as its token source. After the App is set up, the next run logs the App, and the item's status comment, labels and board Status update.
  • claude plugin validate . passes.

Notes

Measured on 2026-11-20, from workflow-state.yml runs in all five repos:

Window Dispatcher runs Failed
12:00–21:59Z 0–32 an hour 0
22:00–22:59Z 486 0
23:00–23:10Z — 35, all before the 23:10:28Z reset
23:11Z onward — 0
  • The three failed runs read all say API rate limit exceeded for user ID 322523691 (sydevs-bot), x-ratelimit-resource: core.
  • One was refactor(server): a folder per route, under server/routes WeMeditateWeb#326's pull_request_target.unlabeled run (23:01:35Z), which left refs/sydevs-lease/326 until a manual sweep at 23:56Z. fix(dispatcher): reclaim dead sessions without waiting on the cron #288 now covers reading such a missed unlock on the item's next event. This ticket stops the run from failing at all.
  • Budgets, from docs.github.com ("Rate limits for the REST API"):
    • A PAT gets 5,000 an hour.
    • An App installation gets 5,000, plus 50 per repo beyond 20 repos and 50 per user beyond 20 users, up to 12,500 (15,000 on Enterprise Cloud).
    • Requests an App makes on a user's behalf share that user's limit. Cloud sessions act as sydevs-bot through the Claude account's GitHub connection, so they most likely draw on the same 5,000 as the PAT.
    • Secondary limits apply on top: 80 content-creating requests a minute, 500 an hour.
  • sydevs is on GitHub's Free plan, with 2 seats.

Related: #288.

Activity

  1. added theissue type on Nov 21, 2026
  2. Ardnived commented on Nov 21, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot implement

  3. added
    bot:working
    on Nov 21, 2026
  4. sydevs-bot commented on Nov 21, 2026

    @sydevs-bot
    Contributor

    🤖 no session running
    approved by Ardnived

    recent dispatches
    • 00:26Z implement · attempt 1 · session · done
    • 00:26Z implement · attempt 1 · session

    Written by the sydevs dispatcher. Sessions never edit this comment.

  5. moved this from Awaiting to Working in SYDevelopers Technical Roadmapon Nov 21, 2026
  6. removed
    bot:working
    on Nov 21, 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

    No labels
    No labels

    Type

    Fields

    Priority

    High

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions