Skip to content

Harden agent rules: repo-scoping + never-fabricate GitHub ids + never-suppress-write-output (stray cross-repo comment incident) #366

Closed

Description

@ptr727

Summary

An autonomous Claude Code PR-review loop posted a stray comment to a stranger's
repository under the account owner's identity, caused by a fabricated (hallucinated)
GitHub GraphQL node id
. This issue documents the incident, the root cause, and a set of
hardened agent rules that should be folded into the standard AGENTS.md (expanding the
existing "PR Review Etiquette" section) so every project inherits them.

What happened

While an agent was autonomously driving a Copilot PR-review cycle on the owner's own repo
(ptr727/PlexCleaner PR #869 — commit → push → reply → resolve → re-request → poll), it
emitted a throwaway GraphQL mutation with a hand-fabricated thread node id and a literal
body of placeholder, with output suppressed:

gh api graphql -f query='mutation($t: ID!, $b: String!){
  addPullRequestReviewThreadReply(input:{pullRequestReviewThreadId:$t, body:$b}){ comment{ id } } }' \
  -F t="PRRT_kwDODvuuzM6SFvx0" -F b="placeholder" >/dev/null 2>&1 || true

The id was never returned by any query in the session — the model invented it. GitHub
resolves node ids globally, so the fabricated id resolved to a real CodeRabbit review
thread on an unrelated third-party repo (mankatcheung/job-finder PR #15). The mutation
succeeded, posting placeholder as the owner. Because the call's output was suppressed
with >/dev/null 2>&1 || true, the agent assumed it had "failed harmlessly" and moved on —
the failure was only ever in the agent's assumption, never in the API call.

The stray comment has since been deleted, and an audit (GitHub event feed + full local
session-log grep) confirmed it was a single, isolated occurrence.

Root cause

  1. Fabricated GitHub id. The agent hand-typed a PRRT_… literal instead of capturing it
    from a live query. Node ids resolve globally, so a guessed id can silently land on a real
    object in a completely different repo/owner.
  2. Suppressed write output. >/dev/null 2>&1 || true on a mutation hid the result, so a
    "success" was misread as a harmless failure.
  3. No repo-scoping guardrail. Nothing constrained the agent to the project's own repo, so
    a cross-repo write was possible at all.

Proposed rules (added to the owner's global ~/.claude/CLAUDE.md, proposed for AGENTS.md)

These were added verbatim to global guidance and are proposed as an expansion of the standard
AGENTS.md:

🚫 Never interact with a repo other than the current project's own repo.
The only repo an agent may write to is the repo of the project it is working in (its
configured origin). "Write" = anything that mutates GitHub or acts as the user: PR/issue/
review comments and replies, thread resolves, review requests, gh pr/gh issue
create/merge/close, pushes, gh api mutations, or any -X POST/PUT/PATCH/DELETE. Reading
public data from other repos is fine. Touching any other repo — even another repo owned
by the same user — requires explicit, per-repo permission from the human first, in the
current conversation. No "harmless" test calls, no exceptions.

Never fabricate or guess a GitHub id. Every node id / numeric id passed to a write
(PRRT_… thread ids, PR_…, comment/review ids, BOT_…) MUST come from a live query in
the same session, captured into a variable and used directly. Never hand-type a literal
id, never pattern-guess one, never reuse one from memory or an example. GitHub resolves ids
globally, so a guessed id can silently land on a real object in a different repo.

Never suppress the output of a write. Do not append >/dev/null 2>&1, || true, or
|| echo … to a mutation/gh api write. Show and confirm its result. A write that appears
to fail must be verified, never assumed to have failed harmlessly — the failure may be
on the agent's end while the call actually succeeded remotely.

The runbook's own reply-thread example was also corrected: it previously modeled the footgun
(-F t="PRRT_..." literal) and now demonstrates capturing $TID from a live
reviewThreads(first:100) query instead.

Ask

Fold these three rules into the standard AGENTS.md (as an expansion of the "PR Review
Etiquette" section) so all downstream projects inherit the repo-scoping guardrail, the
never-fabricate-ids rule, and the never-suppress-write-output rule.

Activity

  1. ptr727 commented on Jul 20, 2026

    @ptr727
    OwnerAuthor

    Follow-up: credential-layer defense-in-depth (token scoping)

    The agent rule above is the primary control. This note captures the optional
    credential-layer hardening discussed after the incident, so it can be referenced from
    AGENTS.md as defense-in-depth. None of this replaces the rule — it only caps what a future
    rule violation could physically reach.

    Why the credential mattered here

    The incident write succeeded because the account's active token is an OAuth token
    (gho_…, from gh auth login) carrying the classic repo scope. Classic repo is
    all-or-nothing: it can write to every repo the account can reach, including arbitrary
    public third-party repos. That is what let a fabricated node id land on a stranger's repo.

    Key constraint: no GitHub token can be scoped to "only the current project." The tightest
    a token expresses is a specific list of repositories. So token scoping complements, never
    replaces, the repo-scoping agent rule.

    Option 1 — fine-grained PAT limited to owned repos

    Replace/augment the classic-scope token with a fine-grained PAT (github_pat_…) whose
    repository access is "All repositories owned by ". It then structurally cannot
    write to a repo you don't own
    (hard 403), while still covering the full multi-repo
    workflow across your own repos. Grant only the permissions the runbook uses: Metadata (RO,
    mandatory), Contents (RW, pushes), Pull requests (RW, review comments/threads/requests),
    Issues (RW), plus Actions (RW) / Workflows (RW) / Administration (RW) only if the project
    dispatches workflows, edits .github/workflows/*, or manages rulesets.

    Trade-offs: fine-grained PATs expire (~1 yr, must renew); org repos need the org to enable +
    approve fine-grained PATs; occasional API-coverage gaps vs classic — validate the review
    runbook once before trusting it.

    Option 2 — directory-scoped active token (for people who work on own + 3rd-party repos on one machine)

    gh has no native per-directory auth (its stored token is global), but gh honors
    GH_TOKEN/GITHUB_TOKEN over the stored token, so "which token is active" can be made to
    depend on the working directory:

    • direnv — a per-directory .envrc that export GH_TOKEN=…. Scopes your interactive
      shell
      . Caveat: direnv hooks interactive shells; an agent's non-interactive Bash
      subshell may not trigger the auto-load, so this protects the human but not necessarily the
      agent.
    • Claude Code per-project env (recommended for the agent) — because Claude Code settings
      are per-project, a git-ignored .claude/settings.local.json can inject a repo-scoped token
      for that directory's sessions:
      { "env": { "GH_TOKEN": "github_pat_scoped_to_this_projects_repos..." } }
      The Bash tool inherits it, so blast radius per directory = exactly what that directory's
      token can reach
      . Own-repo checkouts get an owned-repos PAT; a third-party checkout gets a
      PAT scoped to just that repo (or a read-only token). This directly neutralizes the incident:
      a fabricated id in one project's session would 403 on any repo outside that token's scope.

    Secret hygiene: both approaches put a token in a file — keep it in .claude/settings.local.json
    / .envrc (git-ignored), never in committed settings.json; a keyring/pass-backed fetch is
    cleaner if plaintext-on-disk is unacceptable.

    Suggested AGENTS.md phrasing

    Least-privilege credentials (defense-in-depth). No token can be scoped to a single repo
    automatically, so the repo-scoping rule above is the primary guard. Where practical,
    additionally run agents with a fine-grained PAT limited to owned repositories (blocks
    writes to repos you don't own at the credential layer), and/or a per-project GH_TOKEN
    via git-ignored .claude/settings.local.json so an agent's blast radius is capped to the
    repos its project's token can reach.

  2. ptr727 commented on Jul 20, 2026

    @ptr727
    OwnerAuthor

    Addressed on develop via #367 (8cef2f6). All three root causes now have a behavioral rule, and the first three also have a mechanical guard in the PreToolUse hook:

    1. Fabricated id — carried rule "Never fabricate, guess, or reuse an identifier passed to a write", plus the hook denies a GraphQL mutation that passes a literal PR_/PRRT_/BOT_… node id in a -F value instead of a $variable.
    2. Suppressed write output — carried rule "A write is never a probe, and a write's output is never suppressed", plus the hook denies a gh write whose output is discarded or forced to success (>/dev/null, 2>/dev/null, &>/dev/null, || true, || :, || echo).
    3. No repo-scoping — carried rule "Write only to the current project's own repository", plus the hook denies a gh write with an explicit -R / repos/<owner>/<repo> target outside the checkout's origin.

    Three layers, by design: the carried AGENTS.md section (fleet), the per-machine ~/.claude/CLAUDE.md section (every session, including pre-sync and ad-hoc work, tracked in #365), and the PreToolUse hook (mechanical, and it fires even in autonomous / bypass-permissions sessions — the incident's exact condition). Prose is paired with the hook deliberately: prose is the layer that already failed here (the incident happened under a Verification Discipline section).

    The hook is precision-leaning — it denies the specific dangerous shapes with high confidence rather than gating every write, and it does not false-deny a suppression token merely quoted inside a --body/--title. It was driven through an adversarial review that caught two real enforcement gaps before merge (a || : tail that never matched, and a quoted -R "owner/repo" that bypassed the cross-origin check); both are fixed and covered by self-tests.

    Remaining optional defense-in-depth: a fine-grained PAT scoped to owned repos (a hard 403 on any non-owned repo). Kept out-of-band as per-machine credential setup, not a carried rule.

    Holding open until the develop → main promotion and first downstream propagation.

  3. ptr727 commented on Jul 23, 2026

    @ptr727
    OwnerAuthor

    Resolved. The three-layer defense shipped in #367 - the carried AGENTS.md rules, the per-machine ~/.claude/CLAUDE.md section, and the PreToolUse gh-write-guard hook - is on main and propagated to the first downstream repo (Financial-Modeling). The optional fine-grained-PAT credential hardening remains out-of-band per the decision. Per-box installer rollout continues in #365. Closing.

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions