Skip to content

refactor(github): one github session per identity owning token, quota and transport #767

Closed

Description

@dan-petty

Problem Statement

ADR 0002 requires every roadmap job change to be made as the machine account, so a process must have exactly one GitHub identity. Today the token is found five ways, in different orders (all evidence checked at HEAD 9c879ae, branch release/v0.2.25):

  • get_github_token (config/settings.py:977-989, with _github_cli_token at :982): keyring, then DEVOPS_CLI_GITHUB_TOKEN and GITHUB_TOKEN, then config, then gh auth token. The registry entry is at security/secrets.py:418 and the provider order at :338-341. Its callers are commands/ssh.py:168 and :294, commands/repos.py:54, commands/review.py:730 and commands/analyze.py:482.
  • _resolve_github_token (commands/gh.py:131-146): three keyring names (github.token, github_token, github), then DEVOPS_CLI_GITHUB_TOKEN, GITHUB_TOKEN and GH_TOKEN, then gh auth token.
  • resolve_github_token (github/graphql.py:640-654): environment, then gh auth token. It never reads the keyring.
  • _gh_auth_token (commands/config.py:77-92): gh auth token only. config init offers to copy that token into the keyring (:233-239) or prompts for a PAT (:247-253).
  • _inject_gh_credentials (core/process.py:206-215, called by the sync runner at :248 and the async runner at :389) gives each gh subprocess GH_TOKEN, GITHUB_TOKEN or DEVOPS_CLI_GITHUB_TOKEN from the environment. It never passes the keyring token, so gh falls back to its own stored login. git subprocesses get no token at all, because DEFAULT_DENIED_ENV_PATTERNS (core/process.py:86-94) drops *TOKEN*.

Not every gh subprocess goes through run_gh. _refresh_from_github (github/rate_limiter.py:903-913) runs gh api rate_limit through run_subprocess directly. core/ does not import github/ (no devops_cli.github import under src/devops_cli/core/).

When the keyring token and gh's login differ, one GitHubClient acts as two identities. Labels and milestones go through PyGithub with the keyring token (github/client.py:41-49). Issues go through run_gh (GhCliClient, :345) with gh's login. register_key_on_github (github/ssh.py:23-35) tries gh's login first and then the keyring token. ai/review/runner.py:1924-1925 builds its GitHubClient from whatever token its caller passes, or an empty string. There are five GitHubClient( constructions: commands/repos.py:63, commands/gh.py:155, ai/review/runner.py:1925, commands/analyze.py:512, github/ssh.py:40.

Quota and caching do not know about identity:

  • GitHubRateLimiter keeps one quota file (resolve_quota_cache_path, rate_limiter.py:1313-1317, <data dir>/cache/gh_quota.json, CONST_GH_QUOTA_CACHE_FILENAME at config/constants.py:677). It also keeps one response cache, keyed only by argv (get_cached, :1204-1210; _build_cache_key, :1383). A second identity therefore reads the first identity's quota and cached responses.
  • PyGithub never updates the ledger.
  • GitHubGraphQLClient (graphql.py:657) keeps its own GraphQLTokenBucket (:225) and an ETag cache for GraphQL (RFC7234ETagCache, :135). GitHub documents conditional requests only for REST, and feat(cli): service mode for devops serve with github webhooks, a polling tick and per-repo job queues #752 checked with gh api graphql -i that GraphQL answers carry no ETag. Its only caller is GitHubClient.get_repo_overview (client.py:56), and nothing calls that. GitHubGraphQLError (exceptions/git.py:118) is raised only by this client.

The transport, error classes, Link pagination, conditional requests and the move of the roadmap store, PR diff and signing key onto the session are #983 (v0.2.28), split out of this item.

Proposed Solution

One GitHub session per process. It has one identity and owns the token and the quota. #983 gives it the transport.

  • Identity: on the first GitHub call, gh auth token runs once and the result is kept. That command already applies GH_TOKEN, then GITHUB_TOKEN, then gh's stored login (checked with gh 2.102.0). session.login (GET /user) is fetched the first time it is read, then cached.
  • Pin: both subprocess runners in core/process.py give every gh and git subprocess GH_TOKEN set to that token and no GITHUB_TOKEN. This replaces _inject_gh_credentials, so it covers gh calls that bypass run_gh, such as _refresh_from_github. The lookup and the pin live in core/, because core/ does not import github/. The session in github/ reads its token from there.
  • gh auth is exempt: gh auth subcommands get the ambient GH_TOKEN and GITHUB_TOKEN unchanged, and never a token the pin added.
  • Per-identity quota and cache: GitHubRateLimiter's quota file and response cache live in a directory per identity under the existing cache dir (<data dir>/cache/, CONST_CACHE_DIR_NAME at config/constants.py:200). The directory is named by the first 16 hex characters of the token's SHA-256. No path, and no log line the ledger writes, contains a token or a login. This directory is where refactor(github): the github session owns transport, ledger, error classes and link pagination #983 later keeps its ETag store.
  • PyGithub: stays for now, and GitHubClient is built only from the session's token, in one place. refactor(github): pull request, repository and ssh key calls move from pygithub onto the github session #891 (backlog) removes PyGithub.
  • Removed without shims:
    • the token code: get_github_token with _github_cli_token (config/settings.py:977-989), commands/gh.py::_resolve_github_token, graphql.py::resolve_github_token, commands/config.py::_gh_auth_token, and config init's token import and PAT prompt (commands/config.py:233-253);
    • core/process.py::_inject_gh_credentials;
    • the github.token option (config/options.py:5, :125, :222, keyring key github_token at :237, registry entry security/secrets.py:418, env spec config/env.py:13, :103, :230-231, the config show row at commands/config.py:119, the special case at :320 and the audit-keys dry-run list at :546) and DEVOPS_CLI_GITHUB_TOKEN, with their exports from config/__init__.py (:362, :409, :447, :818, :829, :891);
    • GitHubGraphQLClient, GraphQLTokenBucket, RFC7234ETagCache, GitHubClient.get_repo_overview and .graphql, with their exports from github/__init__.py (:29, :34, :36, :39, :144, :153, :172, :231), and GitHubGraphQLError (exceptions/git.py:118, exported at exceptions/__init__.py:56 and :145), which only that client raises. verify_webhook_signature and WebhookEventDispatcher (graphql.py:278, :295) stay for feat(cli): service mode for devops serve with github webhooks, a polling tick and per-repo job queues #752.
  • Docs and messages:
    • docs/ENV_VARS.md, docs/CLI_REFERENCE.md and docs/commands/config.md are regenerated with uv run devops docs generate --sync-readme (the command devops ci runs at commands/ci.py:361). The example key in lang/en/help.py:1109 changes, which fixes CLI_REFERENCE.md:2260 and config.md:123; the DEVOPS_CLI_GITHUB_TOKEN row at ENV_VARS.md:58 goes with the env spec.
    • README.md (:17, :60) stops naming github.token.
    • The messages at lang/en/messages.py:47, :208-210 and :292-293, commands/repos.py:57 and github/ssh.py:34 say to run gh auth login.
    • The knowledge-base pages that teach github.token, the github_token keyring key, DEVOPS_CLI_GITHUB_TOKEN or get_github_token are corrected: devops_cli/configuration_and_settings.md (:55, :115, :121), devops_cli/libraries/keyring.md (:52-64), devops_cli/libraries/pygithub.md (:38, :75), devops_cli/tasks/github_project_management.md (:177), it_domains/tools/keyring.md (:11-14, :26, :44-50, :75), it_domains/tools/github_cli.md (:11, :72) and it_domains/topics/zero_trust_security_and_compliance.md (:42).
    • The devcontainer gh-token move (commands/devcontainer.py:1449) drops DEVOPS_CLI_GITHUB_TOKEN from its env check. It runs only gh auth subcommands, which are exempt from the pin.

Scope

  • New: a session module in github/ and the token lookup and pin in core/process.py.
  • Changed: core/process.py, github/rate_limiter.py, github/client.py, github/ssh.py, github/graphql.py, github/__init__.py, exceptions/git.py and exceptions/__init__.py (the GitHubGraphQLError removal only), commands/gh.py, commands/config.py, commands/repos.py, commands/devcontainer.py, ai/review/runner.py, config/settings.py, config/env.py, config/options.py, config/__init__.py, security/secrets.py, lang/en/messages.py, lang/en/help.py, tests/conftest.py, README.md, three generated docs, seven knowledge-base pages.
  • Tests: the 19 test files that name a removed symbol (96 matches; tests/test_github_graphql.py alone has 33 across 20 tests and is deleted with the client; tests/test_config_commands.py 11, tests/test_github_client.py 9, tests/test_config_cmd.py 7, tests/test_subprocess_env_boundary.py 6) are rewritten or removed; new tests follow the acceptance criteria below.
  • Constraints:
    • No GitHub API request is made as an identity other than the session's.
    • No new dependencies (httpx2==2.13.0 at pyproject.toml:19 provides MockTransport).
    • No aliases or shims for removed names.
    • New tests use httpx2.MockTransport (as tests/web_fakes.py::StubWeb does) and a patched subprocess runner, never touch the network, and take under 1 s each. They use structural tuple assertions until test(cli): retire the tuple-assert mandate and detect opaque tuple asserts and presence-only tests #851 settles that.
    • A changelog fragment changelog.d/767.md; CHANGELOG.md and docs/ROADMAP.md are not edited.

Key Questions

Acceptance Criteria

  • One identity. With gh auth token scripted to print token A, a path makes one GitHubClient.get_pr_diff call, two run_gh calls and one _refresh_from_github call. The diff request sends Authorization: Bearer A through GitHubClient.get_pr_diff (refactor(github): the github session owns transport, ledger, error classes and link pagination #983 moves it onto session.rest()). All three gh subprocesses get GH_TOKEN=A and no GITHUB_TOKEN. gh auth token runs once. Test: a unit test using httpx2.MockTransport and a patched subprocess runner, next to tests/test_github_client.py::test_get_pr_diff_normal_and_redirect (:248).

  • Environment tokens are honoured. With GH_TOKEN=B in the process environment, the identity lookup passes GH_TOKEN and GITHUB_TOKEN through to gh auth token, and the session's token is B. Test: a unit test that checks the env handed to the subprocess.

  • No fallback. When gh auth token fails, the first call raises the unauthenticated error and no other gh subprocess runs. Test: a unit test.

  • gh auth is exempt. With no GH_TOKEN or GITHUB_TOKEN in the environment and token A pinned, gh auth login, status, refresh, token, switch, logout and setup-git get no GH_TOKEN. With GH_TOKEN=B in the environment, they get B unchanged. Test: a parametrized unit test.

  • git gets the pin. A git push argv run through run_subprocess gets GH_TOKEN=A. Test: a unit test in tests/test_subprocess_env_boundary.py, replacing test_run_subprocess_forwards_tokens_to_gh (:198) and test_run_subprocess_forwards_explicit_env_devops_token (:215).

  • Quota and cache per identity. Sessions for tokens A and B write separate quota files and response caches. A cached gh api read made as A is not served to B. No ledger or cache path contains a token or a login, and neither does any log line the ledger writes. Test: a unit test with a temporary data dir in tests/test_github_rate_limiter.py.

  • Removed names are gone. This command prints nothing; the reviewer runs it, and a unit test asserts that github.token is not a config option:

    rg -n "resolve_github_token|get_github_token|_github_cli_token|_gh_auth_token|_inject_gh_credentials|GitHubGraphQLClient|GraphQLTokenBucket|RFC7234ETagCache|GitHubGraphQLError|get_repo_overview|DEVOPS_CLI_GITHUB_TOKEN|github\.token" src tests README.md docs/ENV_VARS.md docs/CLI_REFERENCE.md docs/commands/config.md

  • PyGithub is built from the session. rg -n "GitHubClient\(" src shows a single construction, in the session module, which passes the session's token. A unit test checks that the client used by repos, review (including ai/review/runner.py), analyze, ssh and commands/gh.py::_get_github_client carries the session's token.

  • Docs regenerated. uv run devops docs generate --sync-readme leaves the tree unchanged, which the docs check of uv run devops ci (commands/ci.py:443-447) verifies; tests/test_docs.py:94 no longer expects DEVOPS_CLI_GITHUB_TOKEN.

  • Gate. uv run devops ci passes within its 5-minute budget. Each new test runs in under 1 s under pytest --durations=0. No new test opens a socket, which prevent_external_network_calls (tests/conftest.py:99) enforces, and no test runs a real gh.

  • Pending a person (live GitHub, read-only), in a terminal logged in to gh, with no GH_TOKEN set:

    • uv run devops gh issues list -R dan-petty/devops-cli prints the open issues.
    • uv run devops gh milestones list -R dan-petty/devops-cli prints the milestones.
    • GH_TOKEN=not-a-real-token uv run devops gh issues list -R dan-petty/devops-cli exits non-zero with the unauthenticated error and prints no issues.

Out of Scope

Dependencies

Activity

  1. added this to the v0.2.26 milestone on Oct 1, 2026
  2. added
    type/refactor
    scope/github
    priority/p1-high
    on Oct 1, 2026
  3. added a commit that references this issue on Oct 1, 2026
  4. dan-petty commented on Oct 3, 2026

    @dan-petty
    OwnerAuthor

    #741 (now the in-cluster runtime) lands before this item and is no longer a dependent; #1083 and #981 are. The machine account's token reaches the Secret from gh's keyring (gh auth token --user), so no devops keyring copy returns.

  5. dan-petty commented on Oct 4, 2026

    @dan-petty
    OwnerAuthor

    Owner decision (2026-10-04) on the amended bad-token check: accepted as delivered.

    • With GH_TOKEN=not-a-real-token, gh auth token prints the token it is given, so the lookup succeeds.
    • GitHub's 401 to the first call then comes back from the quota refresh as Failed to refresh rate limits from GitHub API: HTTP 401: Bad credentials: exit 1, no traceback, no issues printed.

    Turning that into the unauthenticated error is now part of #983. The amendment adds the quota refresh as a classifier site, a unit test and a CLI test, and a person check that replaces this one.

  6. dan-petty commented on Oct 4, 2026

    @dan-petty
    OwnerAuthor

    Closed by #1160, 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.

  7. 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/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions