Skip to content

feat(service): Cache GitHub issue and PR state in the brain from webhooks #6013

Done

Description

@dan-petty

Problem

The Service polls GitHub for state that GitHub already pushes as webhooks. Its 4000-request hourly budget binds all day (693 holds in 24 hours on 2026-11-16, #6012), and about 1700 requests an hour come from loops that name no row; about 170 an hour go to each of 6 Service repositories that have little work. Each row reads the same issue and pull request data again (labels, body, state, checks, mergeable state), so the same object is fetched several times between two changes.

Owner, 2026-11-16: "that's why I want more webhook drive workflows over polling workflows and the project brain to cache gh isssue data when it is updated, which could save some repeated gh requests".

Expected behavior

Related

Alternatives check of 2026-11-17: rejected.
Rejected by ADR 0023. A newer fact can change an ADR.
Tool: karpetrosyan/hishel (https://github.com/karpetrosyan/hishel), BSD-3-Clause, last activity 2026-11-10, maintained.
Reason: hishel caches HTTP answers with ETag revalidation and pluggable stores, covering only the conditional reconcile; it has no webhook writes, GitHub object schema or per-row read-through, and ADR 0023 rejected it for token-private GitHub answers.

Covers:

  • HTTP response cache with stored ETag validators
  • If-None-Match requests answered by 304 with no body
  • Pluggable storage backends such as Redis

Lacks:

  • No webhook payload to cached object write
  • No GitHub issue, pull request or check schema
  • No per-row read-through, reconcile schedule or cache counters

Proposed design

Problem statement

The Service polls GitHub for issue, pull request and check state that GitHub already pushes as webhooks. The 4000-request hourly budget binds all day, and rows read the same object again. This change adds one project-brain cache entry for each GitHub object that a row reads, scoped to its Service repository. Webhooks write the entry, rows read it first, and a slow reconcile with conditional requests repairs missed events. A row falls back to GitHub when Valkey is offline.

Acceptance criteria

Requirement: Brain caches row GitHub objects

The project brain SHALL hold one cache entry for each issue, pull request and head check state that a Service row reads, scoped to its Service repository.

Scenario: A row reads a cached issue

  • WHEN A row asks the brain for an issue that a fake webhook has already cached.
  • THEN The brain returns the title, body, labels, state and update time of that issue, and the fake GitHub client receives no request.

Requirement: Webhook writes cached issue and wakes row

A subscribed webhook event MUST write its object into the brain from the event payload before the Service wakes the lane of the job that needs it.

Scenario: A labeled issue event updates the cache and wakes the row

  • WHEN The webhook router receives an issues event with action labeled for a cached issue.
  • THEN The fake brain store holds the new label set with the event time, and the fake lane trigger records one wake for the due row.

Requirement: Webhook writes cached pull request

A pull_request webhook event MUST write the pull request head, base, draft state and labels into the brain.

Scenario: A pull request event updates the cache

  • WHEN The webhook router receives a pull_request event with action synchronize for a cached pull request.
  • THEN The fake brain store holds the new head SHA, base and draft state from the event payload, and the fake GitHub client receives no request.

Requirement: Brain caches head check state

The project brain SHALL hold the check state of each pull request head that a row reads.

Scenario: A row reads cached check state

  • WHEN A row asks the brain for the check state of a head that a fake reconcile has already cached.
  • THEN The brain returns the check suite and check run states, and the fake GitHub client receives no request.

Requirement: Row reads cache before GitHub

Each row MUST read the brain before GitHub and MUST read GitHub only on a cache miss, for a write, or when the cached object is older than one hour.

Scenario: A miss reads GitHub once and fills the cache

  • WHEN A row reads an object that the fake brain does not hold.
  • THEN The row reads the fake GitHub once, writes the object into the brain, and counts one cache miss.

Requirement: Service write updates cache

A write that the Service makes to GitHub MUST write the same object into the brain.

Scenario: A label write fills the cache

  • WHEN The Service adds a label to an issue through the fake GitHub client.
  • THEN The fake brain holds the new label set before the fake webhook for that label is delivered.

Requirement: Row falls back to GitHub when Valkey is offline

When Valkey is offline, a row MUST read GitHub as today and MUST NOT fail its round on the cache.

Scenario: The brain store refuses a connection

  • WHEN The fake brain store raises a connection error during a row round.
  • THEN The row reads the fake GitHub, the round returns its normal result, and the Service logs and counts the cache outage.

Requirement: Reconcile reads each object at most hourly

The reconcile poll MUST read each cached object at most once an hour with a conditional request.

Scenario: A second reconcile in the same hour skips the object

  • WHEN The reconcile poll runs twice within one hour for the same cached object.
  • THEN The second run sends no GitHub request for that object.

Requirement: A 304 leaves the cache unchanged

A 304 answer MUST leave the cached object unchanged and MUST NOT spend GitHub's primary rate limit.

Scenario: A 304 answer keeps the stored object

  • WHEN The reconcile poll sends If-None-Match with the stored validator and the fake GitHub transport answers 304.
  • THEN The cached object stays as it is, the fake ledger records the attempt in the Service's own window and no spent primary-limit unit.

Requirement: Cache hit and miss counters

The Service MUST count cache hits and cache misses for each row and MUST count the GitHub requests of each row.

Scenario: A cache hit is counted

  • WHEN A row reads a cached object that the brain holds and that is not stale.
  • THEN devops_cli_brain_cache_total{row="refine",result="hit"} increases by one, and the GitHub request counter of that row does not change.

Requirement: Brain cache keys follow the brain key form

Each brain cache key MUST be roadmap:v1:brain-<kind>:<owner>/<name>[:<id>], built by RepoRef.valkey_key, with no empty part and no glob character.

Scenario: A key names its repository and object

  • WHEN The brain builds a key for an issue of a Service repository.
  • THEN The key is roadmap:v1:brain-issue:<owner>/<name>:<number>, and RepoRef.valkey_patterns finds it and no key of another repository.

Requirement: Quiet Service repository stays inside request budget

A Service repository with little work MUST cost fewer than 20 GitHub requests an hour.

Scenario: One simulated hour of a quiet repository

  • WHEN A test runs the rows of a quiet Service repository for one simulated hour with a fake clock and a fake GitHub client that counts requests.
  • THEN The fake GitHub client records fewer than 20 requests for that repository.

Existing Tool Considered

The alternatives check in the issue body (2026-11-17) records a rejected verdict for hishel (https://github.com/karpetrosyan/hishel) under ADR 0023. The block lists what hishel covers and lacks. This proposal does not adopt hishel; it uses the project brain in Valkey for storage and adds the webhook-to-object write, the GitHub object schema, the per-row read-through, the reconcile schedule and the cache counters. The project builds that code itself: about 40,000 tokens to build it, about 15,000 tokens a month to keep and fix it, and about 3 days of elapsed work.

Key questions

  • Where does the brain keep the cache of GitHub objects?: In valkey-roadmap, under the brain key form roadmap:v1:brain-<kind>:<owner>/<name>[:<id>] that RepoRef.valkey_key builds, with a TTL on each key. (best practice) [sources: CONTEXT.md, docs/adr/0023-the-project-brain-is-a-derived-index-in-two-cache-tiers-and-two-scheduled-runs-keep-it.md, src/devops_cli/github/repo_ref.py, docs/adr/0022-one-repository-reference-names-the-objects-labels-and-keys-of-each-repository.md, docs/adr/0021-the-roadmap-service-is-a-webhook-router-cluster-pods-run-the-roadmap-jobs-and-valkey-holds-their-shared-state.md]
    • Chosen: valkey-roadmap under the brain key form. The brain holds the caches of GitHub answers. ADR 0023 names the brain keys, and RepoRef.valkey_key is the recorded builder of a repository-scoped key.
    • Rejected: valkey-devops under valkey: cache keys. No record builds a repository-scoped valkey: key. The instance prefix is the namespace, and valkey-devops keys start with valkey:.
    • Rejected: A JSON file in the Service home. Each lane and each pod needs the same object, and a file has one writer on one host.
    • Rejected: GitHub itself. That is the poll that this item removes.
  • Which route ingests the webhook events into the cache?: The existing webhook router and its roadmap consumer. The consumer's events set is the subscription, and each repository's hook must name the events it sends. Add no second route. (best practice) [sources: docs/adr/0009-one-webhook-router-delivers-each-event-to-the-consumers-subscribed-to-it.md, docs/CLUSTER.md]
    • Chosen: Extend the existing router and roadmap consumer. ADR 0009 builds one router with one consumer for each subscriber. The roadmap consumer already takes issues, pull_request, milestone and release.
    • Rejected: A second webhook route for the cache. ADR 0009 rejects a second route. The existing receiver is extended instead.
  • Does the Service still poll GitHub for the cached objects?: Yes. The poll becomes a slow reconcile with conditional requests and repairs a missed or redelivered event. The sweep keeps its 5-minute schedule for the due marks. (best practice) [sources: docs/adr/0017-each-subscribed-webhook-event-runs-the-update-scoped-to-it.md, docs/adr/0021-the-roadmap-service-is-a-webhook-router-cluster-pods-run-the-roadmap-jobs-and-valkey-holds-their-shared-state.md]
    • Chosen: Keep a slow reconcile poll with conditional requests. ADR 0017 keeps the poll as the reconcile, and GitHub never retries a failed delivery.
    • Rejected: Webhooks only. A missed delivery would never repair, and the cache would stay wrong.
  • How old may a cached object be before a row reads GitHub again?: One hour, the interval of the reconcile. (best practice) [sources: docs/adr/0023-the-project-brain-is-a-derived-index-in-two-cache-tiers-and-two-scheduled-runs-keep-it.md, docs/adr/0017-each-subscribed-webhook-event-runs-the-update-scoped-to-it.md, docs/adr/0021-the-roadmap-service-is-a-webhook-router-cluster-pods-run-the-roadmap-jobs-and-valkey-holds-their-shared-state.md]
    • Chosen: One hour, the reconcile interval. The reconcile reads each entry at most once an hour, so the row and the reconcile do not read the same object twice in one hour. The six quiet repositories stay inside the 20-request target.
    • Rejected: The row interval of the due table. A short row would read GitHub again on each round and spend the budget that this item saves.
    • Rejected: 24 hours, the bound of the listing snapshot. ADR 0023 drops a listing copy whose last full read is older than 24 hours. A missed webhook would leave an object wrong for a day.
    • Rejected: No limit. A missed webhook would leave the entry wrong with no repair.
  • Which cache key scheme do the brain entries use?: The brain key form of ADR 0022 section 5 as ADR 0023 section 3 restates it: roadmap:v1:brain-<kind>:<owner>/<name>[:<id>], built by RepoRef.valkey_key (for example brain-issue, brain-pr, brain-check). The repository holds no text of docs(adr): Valkey keys are coordination keys or cache keys #5245, so the key rule in force is the ADR and RepoRef. (best practice) [sources: src/devops_cli/github/repo_ref.py, docs/adr/0022-one-repository-reference-names-the-objects-labels-and-keys-of-each-repository.md, docs/adr/0023-the-project-brain-is-a-derived-index-in-two-cache-tiers-and-two-scheduled-runs-keep-it.md, openspec/specs/repository-names/spec.md]
    • Chosen: The brain key form of ADR 0022 section 5. It is the recorded key form of valkey-roadmap. RepoRef builds it, and the repository part holds the only /, so the two patterns of a repository find its entries and no other's.
    • Rejected: A new scheme for the brain. Two schemes for one store make the keys of the same object differ, and no record builds a repository-scoped valkey: key.
  • Which names do the cache counters use?: devops_cli_brain_cache_total counts a hit or a miss for each row, beside the existing per-row GitHub request counter devops_cli_github_requests_total. (best practice) [sources: docs/CLUSTER.md]
    • Chosen: Follow the existing counter naming with a row label. The Service already counts GitHub requests per row under devops_cli_github_requests_total, and the same row label keeps the two series together.
    • Rejected: A separate metrics namespace. A second naming rule for the same Service makes the dashboards harder to read.
  • Which webhook events update the cache?: The first version writes the cache from issues and pull_request events. label is an action of issues, not an event. issue_comment, pull_request_review, check_suite, check_run, status and push have no roadmap consumer subscription today, and each repository's hook does not send them; their cache writes are deferred to a new issue. Check state is still read on a miss and by the hourly reconcile. (best practice) [sources: docs/adr/0009-one-webhook-router-delivers-each-event-to-the-consumers-subscribed-to-it.md, docs/CLUSTER.md, src/devops_cli/config/constants.py]
    • Chosen: Write from issues and pull_request now, and defer the other events. The roadmap consumer already takes issues and pull_request. The other events need a consumer subscription and a person to change each repository hook.
    • Rejected: Add all listed events in this change. No subscriber exists for the check, comment and push events, and a person must change each repository hook. This would block the core cache.
  • Where does the reconcile keep the validator for its conditional request?: With the object in its brain entry (the ETag and the update time), and the reconcile sends it as If-None-Match. Until the two-token probe of ADR 0021 section 6 passes, the reconcile reads that object in full once an hour. (best practice) [sources: docs/adr/0023-the-project-brain-is-a-derived-index-in-two-cache-tiers-and-two-scheduled-runs-keep-it.md, docs/adr/0021-the-roadmap-service-is-a-webhook-router-cluster-pods-run-the-roadmap-jobs-and-valkey-holds-their-shared-state.md, src/devops_cli/github/session_transport.py, docs/adr/0006-github-session-requests-go-through-gidgethub.md]
    • Chosen: The validator goes in the brain entry with the object. The gidgethub ETag store is in process and dies with the pod, so a restarted pod would read every object in full. The entry's validator survives the pod.
    • Rejected: The in-process gidgethub store alone. A pod restart loses every validator, and the first reconcile after a restart reads each object in full.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Moved to new items

#6630

Activity

  1. added
    type/feature
    source/agent
    status/new
    on Nov 16, 2026
  2. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. The Service hits GitHub rate limits 693 times per day, causing job holds and degraded throughput.
    • Related: perf(roadmap): share the listing snapshot and tool facts in Valkey #4668, which asks for a different change. Both reduce counted GitHub requests via Valkey, but #4668 shares listing snapshots and PyPI facts while the candidate caches issue and PR state updated by webhooks.
  3. added
    effort/high
    value/high
    scope/service
    priority/p1-high
    on Nov 16, 2026
  4. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Owner question: Who adds the events to each repository's webhook? (a person decides)

    Options

    1. Decide the question: The job refines the item with your answer.
    2. Leave it to the job: The job records the best-practice answer and goes on.

    Recommendation: Decide the question: only a person can answer it.

    Evidence

  5. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (6)

    Tools considered:

    Searches run:

    • hishel HTTP cache conditional requests Python, results: 5
    • open source cache GitHub issue and pull request state from webhooks, results: 5
    • hishel pypi caching library github karpetrosyan, results: 5
    • webhook driven GitHub API cache Valkey Redis reduce rate limit, results: 5
    • hishel documentation storage backends redis s3 rfc 9111, results: 5
    • hishel, results: 5

    Sources:

  6. devops-cli-service commented on Nov 20, 2026

    @devops-cli-service
    Contributor

    Owner question: See the first point of Open questions in the section Proposed design of the body.

    Options

    1. Decide the question: The job refines the item with your answer.
    2. Leave it to the job: The job records the best-practice answer and goes on.

    Recommendation: Decide the question: only a person can answer it.

    Evidence

  7. added
    status/ready
    and removed
    status/new
    on Nov 21, 2026
  8. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See Moved to new items in the section Proposed design of the body.

  9. added
    owner/service
    on Nov 21, 2026
  10. 6 remaining items

  11. added
    status/planned
    and removed
    status/ready
    on Nov 21, 2026
  12. added
    owner/agent
    status/in-progress
    and removed
    status/planned
    on Nov 24, 2026
  13. added
    status/in-review
    and removed
    status/in-progress
    on Nov 24, 2026
  14. removed
    status/in-review
    owner/agent
    on Nov 24, 2026
  15. devops-cli-service commented on Nov 24, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-24 13:45 UTC

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

    effort/highpriority/p1-highscope/servicesource/agenttype/featurevalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions