How to Give a Python Coding Agent Safe GitHub Push Access
A coding agent that opens branches and pull requests has a bigger blast radius than one that just comments on issues. How to scope its GitHub write access from Python and block direct pushes to protected branches.
Key takeaways
- -A coding agent needs write access to repository content, not just issues and comments, which calls for a tighter allowlist than a triage agent.
- -Discover exact method IDs with swy discover and swy info scoped to the github project rather than hardcoding IDs that may not match your installed catalog.
- -Python's runtime-py SDK follows the same exec(method_id, params/body) model as the JavaScript runtime.
- -A deny policy on the branch and commit methods, not the pull-request method, is what actually stops a direct push to a protected branch.
- -This builds on, and doesn't repeat, the token-scoping and rate-limit advice in our general GitHub agent guide.
Most GitHub-agent setups assume an agent that reads issues, triages them, and leaves comments. A coding agent is a different animal: it needs to touch repository content itself, opening branches, proposing changes, and raising pull requests, which is a meaningfully larger blast radius than posting a comment. This post is about wiring that up safely from Python, specifically, not about GitHub agents in general; our earlier post on connecting GitHub to an AI agent covers token scoping and rate limits that still apply here and aren't repeated.
What this covers: setting up a Python-driven coding agent's GitHub access through Swytchcode's runtime-py SDK, discovering the exact write methods your installed catalog exposes rather than guessing at method names, and writing a policy that blocks the one action a coding agent should never be allowed to do unsupervised: pushing straight to a protected branch.
Why a coding agent needs a different setup than an issue-triage agent
An agent that reads issues and leaves comments has a narrow failure mode: a wrong comment is embarrassing, not destructive. A coding agent that can create branches, commit file changes, and open pull requests has write access to the actual content of your repository. The risk isn't just "the agent does the wrong thing", it's "the agent does the wrong thing to code that then gets merged and shipped." That calls for two things an issue-triage setup doesn't need as urgently: a tight allowlist of exactly which write operations are permitted, and a policy that keeps anything touching a protected branch out of the agent's reach entirely.
Discover the exact methods your catalog exposes
Swytchcode's method catalog changes over time, and canonical method IDs for creating a branch ref, committing a file change, or opening a pull request are specific enough that hardcoding them from memory risks being wrong for your installed version. The right first step, every time, is to ask the catalog directly rather than guessing:
swy get github
swy auth connect github
# Search by what you want to do, scoped to the github project so you don't
# accidentally match a similarly-worded method on a different integration
swy discover "open a pull request" --project github
swy discover "commit a file change to a branch" --project github
swy discover "create a new branch" --project github
# Once you have a candidate ID, inspect its exact inputs and outputs
swy info <canonical_id_from_discover>Run `swy info` on whatever `swy discover` returns before you write a line of Python against it. The inputs matter here specifically, since a commit-content method typically wants the file's new content and the branch ref, not a diff, and a pull-request method wants the head and base branch names exactly as GitHub stores them.
Allow only what the agent actually needs
Add each method individually, by its real ID from `swy discover`, rather than allowing an entire provider's method set. For a coding agent that opens pull requests against a feature branch, that's typically three or four methods: creating a branch ref, committing a file change, opening the pull request, and posting a comment on it. Two of those, posting a comment and creating an issue to flag a problem, use verified canonical IDs already documented for Swytchcode's GitHub integration:
swy add method <branch_create_method_from_discover>
swy add method <commit_content_method_from_discover>
swy add method <pull_request_create_method_from_discover>
swy add method github.repo.comments.create.1
swy add method github.repo.issues.createOnly operations added this way can run. If the agent's reasoning tries to call something that was never added, the call fails before it reaches GitHub, which means a mis-picked or overly broad action is caught by the allowlist, not by hoping the model stays within bounds.
The Python side
Python's runtime-py SDK follows the same model as the JavaScript runtime shown throughout Swytchcode's docs: every call is a method ID plus the inputs that method's `swy info` output describes, nothing else. Check runtime-py's own documentation for the exact current import path for your installed version; the call shape underneath is this, written conceptually against the JavaScript SDK's documented pattern, which runtime-py mirrors:
# JavaScript (@swytchcode/runtime), the fully documented reference pattern
import { exec } from "@swytchcode/runtime";
// params -> query/path params, body -> request body
const branch = await exec("<branch_create_method>", {
body: { ref: "refs/heads/agent/fix-null-check", sha: baseCommitSha }
});
const commit = await exec("<commit_content_method>", {
params: { path: "src/validate.py" },
body: { message: "Fix null check in validate()", content: newFileContentBase64, branch: "agent/fix-null-check" }
});
const pr = await exec("<pull_request_create_method>", {
body: { title: "Fix null check in validate()", head: "agent/fix-null-check", base: "main", body: prDescription }
});Each call checks the method is on the allowlist, runs matching policies, injects the stored GitHub credential, retries on rate limits with backoff, and returns parsed JSON, or raises on failure, exactly the same for a Python caller using runtime-py as for the JavaScript example above. No API key handling and no manual retry loop in your agent code either way.
The one policy that matters most: block direct pushes to protected branches
Everything above already keeps the agent to a narrow, explicit set of operations. The policy layer is for the one rule that's easy to get wrong if it's left to code review alone: nothing the agent does should be able to touch a protected branch directly, only open a pull request against it.
{
"id": "no-direct-protected-branch-writes",
"target": ["<branch_create_method>", "<commit_content_method>"],
"when": { "field": "branch", "operator": "in", "value": ["main", "master", "production"] },
"action": { "type": "DENY", "message": "Agents may not write directly to a protected branch" }
}This is a deny, not a hold-for-approval, because there's no legitimate case where an agent should be waiting on a human to approve a direct push to main; the pull request is the approval gate. Save `REQUIRES_APPROVAL` for the pull-request step itself if you want a person to sign off before it opens, not for the branch-level write.
Things we got wrong the first time
- We assumed one allowlisted "create or update file" method would cover both new files and edits. Depending on your catalog version, creating a file and updating an existing one can be separate methods with different required fields (an update typically needs the current file's blob SHA to avoid overwriting someone else's concurrent change); check both with `swy info` rather than assuming one method handles both cases.
- We pointed the policy at the pull-request method instead of the branch and commit methods. That let the agent commit directly to a protected branch as long as it never opened a PR from it, which defeats the point. The deny has to sit on the write methods themselves, not on the PR step.
- We didn't scope swy discover to the project. A plain-English search for "create a branch" without --project github can return a similarly worded method from a different connected integration. Always scope discovery to the provider you're working with.
FAQ
Does this replace the token-scoping advice in the general GitHub agent guide?
No, it builds on it. Scoping a fine-grained token to specific repositories and permissions, covered in our earlier "How to Connect GitHub to an AI Agent" post, still applies here; this post adds the allowlist and policy layer specific to an agent that writes code rather than just comments on it.
Why not hardcode the exact method IDs for branch creation and commits?
Because Swytchcode's method catalog is versioned and can change, and a wrong hardcoded ID fails in a way that's harder to debug than a discovery step that always reflects what's actually installed. Run `swy discover` and `swy info` against your own catalog rather than copying an ID from a blog post.
Can the agent merge its own pull request?
Only if you allow a merge method and don't gate it behind an approval policy. For most teams, the safer default is to allowlist opening the PR and leave merging to a human, or to a separate CI check, rather than giving the agent a merge method at all.
What happens if the agent's commit call times out partway through?
If the method is called with an idempotency key, Swytchcode retries the call rather than risking a duplicate commit or a half-applied change; without one, a POST or PATCH is not automatically retried. Set a key on write calls in this flow for exactly that reason.
Swytchcode resources
- How to Connect GitHub to an AI Agent (2026): https://www.swytchcode.com/content/connect-github-to-an-ai-agent
- GitHub Copilot Suggests Methods That Don't Exist: How to Catch Hallucinated APIs: https://www.swytchcode.com/content/github-copilot-hallucinated-api-methods
- How to build a GitHub AI Agent: https://www.swytchcode.com/blogs/how-to-build-a-github-ai-agent
- How to set up policy guardrails for AI agents: https://www.swytchcode.com/blogs/how-to-set-up-policy-guardrails-for-ai-agents-with-swytchcode
- Idempotency explained: https://www.swytchcode.com/blogs/idempotency-explained-how-swytchcode-prevents-duplicate-charges-and-duplicate-emails
- Swytchcode pricing: https://www.swytchcode.com/pricing
More content
API Governance Tools That Block a Pull Request When a Policy Check Fails
Spectral, Redocly CLI, and vacuum all fail a PR's CI check when an OpenAPI spec breaks a governance rule. How the gate is actually wired up, and why it's a different layer from Swytchcode's own runtime policy checks.
API Governance Tools With a Free Tier: What Small Teams Can Actually Use
Most API governance comparisons assume an enterprise budget. Here's what's genuinely free with no catch, what's gated behind a paid plan, and the realistic free stack a small team can actually run.
API Spend Caps for AI Agents: Capping Cost Before the Invoice Does
Most teams find out an agent overspent on a metered API when the invoice arrives. An argument-level policy that caps the metric a provider actually bills on, checked before the call goes out, catches it in the moment instead.
