Skip to content

bug: global GitHub PAT rotation never reaches agents — propagation skips agents without .env and never re-templates the git remote #1967

Closed

Description

@vybe

Summary

Rotating the global GitHub PAT in Settings does not reach running agents. The #211 auto-propagation reports success while updating nothing, and even on its success path it never repairs git authentication. Agents that use the global PAT keep authenticating with the revoked token — git fetch/push and the gh CLI all fail — until each container is manually restarted. Nothing in the UI signals this, so the rotation looks clean.

Observed on a fleet where a rotation left every global-PAT agent authenticating with a token that returns HTTP 401, for as long as the containers stayed up (11–13 days).

Component

Backend / Settings + GitHub PAT propagation (github_pat_propagation_service)

Priority

P1 — the documented purpose of #211 ("agents pick up the new token without a restart") is non-functional in the common configuration, and the failure is silent.

Error

No exception is raised. The endpoint returns a success payload in which nothing was updated:

{
  "success": true,
  "masked": "ghp_...[REDACTED]",
  "propagation": {
    "total_running": 12,
    "updated": [],
    "skipped": [
      {"agent_name": "[AGENT]", "status": "skipped_no_pat"},
      {"agent_name": "[AGENT]", "status": "skipped_per_agent_pat"}
    ],
    "failed": []
  }
}

Downstream symptom inside an affected agent:

remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/[ORG]/[REPO].git/'

Location

  • File: src/backend/services/github_pat_propagation_service.py
  • Functions: propagate_github_pat(), _propagate_to_agent(), _apply_pat_to_env()
  • Caller: src/backend/routers/settings.py:712 (update_github_pat)

Root Cause

Three independent gaps, the first two of which each fully defeat the rotation.

1. The global path requires a pre-existing .env that most agents never have.

_propagate_to_agent() calls _apply_pat_to_env(..., add_if_missing=False). That returns skipped_no_pat when /home/developer/.env is absent, or present but without a GITHUB_PAT line:

if env_content is None:
    if not add_if_missing:
        return "skipped_no_pat"
elif not _env_has_github_pat(env_content) and not add_if_missing:
    return "skipped_no_pat"

Agents provisioned from a GitHub template ship a .env.example and no .env, so every such agent skips. The per-agent path (propagate_pat_to_single_agent, #1264) passes add_if_missing=True and does not have this problem — the asymmetry appears unintentional.

2. The global path never re-templates the git remote.

Clones are created as https://oauth2:<PAT>@github.com/<org>/<repo>.git, and that URL is persisted in .git/config on the agent's workspace volume. Git authenticates from the URL, not from GITHUB_PAT. propagate_github_pat() only rewrites .env; it never calls git_service.update_remote_pat().

The service's own docstring for the per-agent path states this explicitly:

The live git process authenticates from the remote URL (not the env var), so remote_updated is the load-bearing part of the immediate fix; the .env write only takes effect on the next restart.

So even if gap #1 were fixed, a global rotation still would not restore git access until a restart. Only propagate_pat_to_single_agent() calls update_remote_pat().

3. The propagation result is never surfaced.

update_github_pat returns a populated propagation object, but no caller displays it. A rotation in which 100% of agents were skipped is visually identical to one that succeeded.

Secondary observation

An agent whose per-agent PAT happens to hold a stale copy of the old global token is skipped as skipped_per_agent_pat on every future rotation, and its dead token is re-injected on every recreate. There is no signal that a stored per-agent PAT has been revoked. Worth considering a validity check or a "stale per-agent PAT" warning.

Note on the existing convergence path

check_github_pat_env_matches() (services/agent_service/helpers.py:549) does force a container recreate on the next start when the baked GITHUB_PAT env differs from the effective PAT, and docker/base-image/startup.sh then unconditionally runs git remote set-url origin with the fresh token. That path is correct — it just only runs on a cold start, so long-lived agents are the exposure window. The hot-propagation in #211 exists precisely to close that window, and currently does not.

Reproduction Steps

  1. Create an agent from a GitHub template so it is cloned over HTTPS with the global PAT embedded in the remote URL. Confirm the agent has no /home/developer/.env (only .env.example).
  2. Leave the agent running.
  3. In Settings, replace the global GitHub PAT with a different valid token.
  4. Inspect the API response — propagation.updated is empty, all agents report skipped_no_pat.
  5. In the container, printenv GITHUB_PAT and git remote get-url origin both still contain the old token.
  6. Revoke the old token; any git fetch/push or gh call from the agent now fails, with no indication in the UI.

Suggested Fix

a. Do not require a pre-existing .env on the global path. Either pass add_if_missing=True, or gate on "the agent has a git config" rather than "the agent already has a GITHUB_PAT line":

status = await _apply_pat_to_env(
    client, agent_name, base_url, new_pat, add_if_missing=True
)

b. Re-template the live remote on the global path too, mirroring propagate_pat_to_single_agent:

git_config = db.get_git_config(agent_name)
if git_config and git_config.github_repo:
    await git_service.update_remote_pat(
        agent_name, new_pat, git_config.github_repo
    )

Factoring the shared ".env write + remote rewrite" body out of propagate_pat_to_single_agent and reusing it for both paths would remove the asymmetry at its source.

c. Surface the propagation result in the Settings UI — at minimum a warning when updated is empty while total_running > 0, and a per-agent breakdown of skipped/failed.

Environment

  • Trinity version: 9f766d11
  • Database backend: PostgreSQL
  • OS: Ubuntu 22.04

Related

Activity

  1. added
    priority-p1
    type-bug
    theme-infrastructure
    status-ready
    on Aug 3, 2026
  2. added
    complexity-medium
    on Aug 4, 2026
  3. added
    status-in-progress
    and removed
    status-ready
    on Aug 4, 2026
  4. self-assigned this
    on Aug 4, 2026
  5. dolho commented on Aug 4, 2026

    @dolho
    Contributor

    /claim — picking this up. Branch: fix/1967-global-pat-propagation, off dev.

  6. github-actions commented on Aug 4, 2026

    @github-actions

    @dolho This issue is already assigned to dolho. Use /unclaim first if you want to take over.

  7. dolho commented on Aug 4, 2026

    @dolho
    Contributor

    PR: #1979

    Two notes on scope:

    • Gap 3 is already fixed on dev — Settings.vue renders the full updated/failed/skipped breakdown with per-agent reasons. This issue was filed against 9f766d11. One residual remained and is fixed in the PR: 0 of N applied rendered success-green whenever nothing outright failed, so a rotation that reached nothing looked identical to one that worked.
    • I did not take add_if_missing=True unconditionally. It would inject the global token into every running container including agents that never touched GitHub — the module's own docstring says the original gate existed to prevent exactly that. Used the alternative this issue also offers (gate on the git config), which targets precisely the skipped population while leaving non-GitHub agents alone. Details and the before/after table are in the PR.

    The secondary observation (a per-agent PAT holding a stale copy of the old global token, skipped forever and re-injected on every recreate) is not addressed here — detecting it needs a validity probe against the GitHub API per agent, which brings network calls, rate limits and a new failure mode into a rotation path. That is a separate feature; worth its own issue rather than being folded in silently.

  8. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 4, 2026
  9. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-mediumpriority-p1theme-infrastructuretype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions