# GitHub Event-Driven Agents
GitHub is a **webhook-push** channel: there is no long-polling worker. Every GitHub App / repository delivery lands at `POST /api/webhooks/github`, where it is HMAC-verified, fan-out'd to one `InboundMessage` per matching custom-agent binding, and shipped to the rest of DeerFlow through the same `ChannelManager` that handles Feishu/Slack/Telegram. For the high-level orientation, see [channel guidance](../app/channels/AGENTS.md#github-channel).
This guide covers setup and the architecture of that pipeline:
- Per-agent bindings (`config.yaml` → `github:` block)
- Webhook → fan-out → `InboundMessage` dispatch
- Author authorization (`allow_authors` and `allow_author_associations`)
- Mention-handle precedence for `require_mention` triggers
- `preferred_thread_id = UUID5(repo, number, agent_name)` thread determinism
- [Configuring GitHub tools for webhook runs](#github-tool-setup)
- `ConflictError` (HTTP 409) thread-create race recovery
- Why **outbound is log-only**
- Follow-up buffering while busy (issue #4121): buffer → watch → drain, and the single-process scope limit
## Overview
GitHub bindings are declared **per custom agent** in `users/{owner_user_id}/agents/{agent_name}/config.yaml` under a `github:` block. The global `config.yaml` `channels.github` block is intentionally minimal — only the operator kill-switch (`enabled`) and `default_mention_login` live there. Everything that identifies "which agent handles which repo" lives next to the agent that owns it.
```mermaid
graph LR
classDef operator fill:#D8CFC4,stroke:#6E6259,color:#2F2A26
classDef agent fill:#E5D2C4,stroke:#806A5B,color:#30251E
classDef route fill:#C9D7D2,stroke:#5D706A,color:#21302C
OperatorYaml["config.yaml
channels.github:
enabled: true
default_mention_login"]:::operator
AgentYaml["agents/{name}/config.yaml
github:
installation_id
bot_login
bindings: [{repo, triggers}]"]:::agent
Registry["build_github_agent_registry()
(store-signature cached, asyncio.to_thread)"]:::route
Webhook["POST /api/webhooks/github
(HMAC verify)"]:::route
OperatorYaml --> Registry
AgentYaml --> Registry
Registry --> Webhook
```
Registry cache invalidation uses the configured agent store's opaque signature.
The file backend derives it from agent config mtimes; the database backend uses
a deterministic digest of the stored owner, name, config, and soul, so an update
still invalidates the registry when two writes share the same timestamp.
Each agent binding lists the **events it cares about** under `triggers:`. Events absent from `triggers:` are not delivered to that agent — the dispatcher never loads the agent for them. `DEFAULT_TRIGGERS` only supplies **field-level defaults** (e.g. `require_mention: true`) for events a binding did declare; it is no longer an enablement list.
## Webhook → Fan-out → Dispatch
The webhook handler stays cheap — no LangGraph calls — so GitHub's 10-second delivery timeout is never at risk. Verification, fan-out, and the bus publish are all in-process and bounded.
```mermaid
sequenceDiagram
autonumber
participant GH as GitHub
participant Router as POST /api/webhooks/github
(github_webhooks.py)
participant Disp as fanout_event()
(github/dispatcher.py)
participant Reg as build_github_agent_registry
participant Trg as event_should_fire
participant Bus as MessageBus
participant Mgr as ChannelManager
participant Client as langgraph_sdk client
participant Gateway as Gateway
/api/webhooks/github
GH->>Router: delivery (event, delivery_id, payload,
X-Hub-Signature-256, X-GitHub-Event)
Router->>Router: _verify_signature()
hmac.compare_digest(sha256, secret)
Router->>Disp: fanout_event(bus, event, delivery_id, payload,
operator_default_mention_login)
Disp->>Reg: build_github_agent_registry() (to_thread)
Reg-->>Disp: agents bound to (repo, event)
loop each matched agent
Disp->>Disp: _is_self_event(sender.login)?
alt self event
Disp-->>Disp: skipped (self_event)
else trigger filter
Disp->>Trg: event_should_fire(event, payload, trigger, default_mention_login)
Trg-->>Disp: (fire, reason)
opt fire
Disp->>Disp: build_prompt() + resolve_thread_id()
UUID5(repo, number, agent)
Disp->>Bus: publish_inbound(InboundMessage(
channel=github, chat_id=repo,
topic_id="{number}:{agent}",
owner_user_id=match.user_id,
metadata.agent_name=agent,
metadata.preferred_thread_id=...,
metadata.github={...}))
end
end
end
Disp-->>Router: summary {matched, fired, skipped}
Router-->>GH: 200 OK
Note over Bus,Gateway: Bus consumer side
Bus->>Mgr: msg = get_inbound()
Mgr->>Client: client.threads.create(thread_id=preferred_thread_id)
Client->>Gateway: threads.create (with owner headers)
Gateway-->>Client: thread_id (or 409)
Mgr->>Client: runs.create() [fire_and_forget=True]
Client->>Gateway: start run
Gateway-->>Client: pending
Note over Mgr: Manager returns immediately.
The agent can use configured GitHub tools.
```
## `preferred_thread_id = UUID5(...)` Thread Determinism
`resolve_thread_id(repo, issue_or_pr_number, agent_name)` builds a deterministic LangGraph thread id so a `(repo, PR/issue number)` always lands on the same thread — even after a store wipe, even across gateway replicas (same UUID5 namespace).
```mermaid
graph LR
classDef input fill:#D8CFC4,stroke:#6E6259,color:#2F2A26
classDef hash fill:#D7D3E8,stroke:#6B6680,color:#29263A
classDef thread fill:#C9D7D2,stroke:#5D706A,color:#21302C
Repo["repo
owner/name"]:::input
Number["issue/PR number
int"]:::input
Agent["agent_name
[A-Za-z0-9-]+"]:::input
Seed["seed = '{repo}#{number}:{agent}'"]:::hash
UUID5["uuid.uuid5(
GITHUB_THREAD_NAMESPACE,
seed)"]:::hash
Thread["thread_id
(same across replicas + restarts)"]:::thread
Repo --> Seed
Number --> Seed
Agent --> Seed
Seed --> UUID5 --> Thread
```
Different agents on the same PR (coder + reviewer) **deliberately** get different thread ids — `agent_name` is part of the seed. Sharing a thread would couple their message histories and checkpoints, and `multitask_strategy="reject"` would silently drop one run on every dual-mention. Each agent owns its own thread; cross-agent coordination flows through GitHub (PR comments, review threads), the source of truth humans see.
`ChannelStore` uses `topic_id = f"{number}:{agent_name}"` as its cache key, so each agent's cached mapping is independent — a coder's mapping is invisible to a reviewer on the same PR.
## Mention-handle Precedence
For bindings that declare `require_mention: true` on a given event, the dispatcher must resolve **which** mention login gates the trigger. The precedence chain is:
```mermaid
graph LR
classDef step fill:#C9D7D2,stroke:#5D706A,color:#21302C
classDef fallback fill:#D7D3E8,stroke:#6B6680,color:#29263A
A["1. trigger.mention_login
(per-event override)"]:::step
B["2. github.bot_login
(agent's App identity)"]:::step
C["3. channels.github.default_mention_login
(operator-wide default)"]:::step
D["4. agent.name
(last-resort fallback)"]:::fallback
Use["effective @mention"]:::step
A -->|"non-empty"| Use
B -->|"non-empty"| Use
C -->|"non-empty"| Use
D --> Use
```
Whitespace-only values at every level are treated as unset, so the chain falls through cleanly. The `_is_self_event` gate uses the same precedence (with the agent's whole `bindings[*].triggers[*].mention_login` aggregated across all bindings, plus an `agent.name` fallback) so the self-loop gate and the mention gate stay coherent.
## Author Authorization
Webhook HMAC verification only proves that a delivery came from GitHub; it does not authorize the GitHub account that triggered the event. Because each matched agent executes in its DeerFlow owner's bucket (`owner_user_id`), `event_should_fire()` enforces a fail-closed author gate on every user-authored event (`issue_comment`, `pull_request_review_comment`, `pull_request_review`, `issues`, `pull_request`):
- **`allow_authors`** (default `[]`): GitHub logins (case-insensitive) explicitly authorized to trigger the agent. Use this for specific humans or third-party bots (for example `Copilot` or `coderabbitai[bot]`).
- **`allow_authors_bypass_mention`** (default `true`): Controls whether `allow_authors` matches skip `require_mention`. Set to `false` when using `allow_authors` strictly as an authorization allowlist that must still `@mention` the bot.
- **`allow_author_associations`** (default `["OWNER", "MEMBER", "COLLABORATOR"]`): GitHub `author_association` values authorized when the actor is not listed in `allow_authors`. Association-authorized actors remain subject to `require_mention` when enabled.
- **Sender binding**: Every user-authored delivery must carry a valid `sender.login`. Because GitHub attaches `author_association` to the authored object (`comment`, `issue`, `pull_request`, `review`), the dispatcher accepts that association only when `sender.login` matches the object's `user.login` (case-insensitively), failing closed when `sender` is missing or `null` and preventing another actor on edit or update deliveries from inheriting the original author's association.
- **Fail-closed default**: External associations (`NONE`, `CONTRIBUTOR`, `FIRST_TIME_CONTRIBUTOR`, `FIRST_TIMER`, `MANNEQUIN`) and missing or unattributable associations are rejected with `unauthorized_author` even when the body mentions `@bot`.
- **Stricter scoping in large organizations**: On organization-owned repositories, GitHub reports `MEMBER` (not `OWNER`) for internal org admins and maintainers, while `COLLABORATOR` only covers outside collaborators. When org-wide read-only members should not trigger the agent, set `allow_author_associations: []` paired with an explicit `allow_authors` list (and `allow_authors_bypass_mention: false` if `@mention` should still be required), adding `"COLLABORATOR"` only when outside collaborators should also be permitted.
## GitHub Tool Setup
Webhook delivery starts the agent independently of its GitHub tool settings.
`GITHUB_WEBHOOK_SECRET` verifies incoming deliveries. For repository operations,
the [official GitHub MCP Server](https://github.com/github/github-mcp-server) can
authenticate as a GitHub App installation in `stdio` mode. It obtains and
refreshes installation tokens without an interactive login. Git and the GitHub
CLI use their own authentication settings.
### Configure the GitHub App and webhook
1. [Register a GitHub App](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/registering-a-github-app)
or reuse an existing App. Install it on the repositories the agent should
access. For the read-only tools below, grant read access to **Contents**,
**Issues**, and **Pull requests**.
2. In the App settings, set the webhook URL to
`https:///api/webhooks/github` and subscribe to
[**Issue comment**](https://docs.github.com/en/webhooks/webhook-events-and-payloads#issue_comment)
events to trigger on issue and PR conversation comments. Set the same webhook
secret in GitHub and in the Gateway's `GITHUB_WEBHOOK_SECRET` environment
variable. Set `channels.github.enabled: true` in the deployment `config.yaml`.
### Connect the MCP server
Install [GitHub MCP Server v2.2.0](https://github.com/github/github-mcp-server/releases/tag/v2.2.0)
on the Gateway. In the Gateway environment, set `GITHUB_APP_ID` to the App ID
from its settings and `GITHUB_APP_INSTALLATION_ID` to the installation ID
(`installation.id` in a GitHub App webhook payload). Generate a private key in
the App settings and make its PEM file readable by the MCP process. For Docker,
provide the Linux binary inside the Gateway container and mount the PEM as a
read-only secret. Keep the key out of source control and outside agent-accessible
mounts.
Merge the `github-app` entry into `mcpServers` in
[`extensions_config.json`](MCP_SERVER.md#setup). Adjust the executable and PEM
paths for the Gateway environment.
```json
{
"mcpServers": {
"github-app": {
"enabled": true,
"type": "stdio",
"command": "/usr/local/bin/github-mcp-server",
"args": [
"stdio",
"--read-only",
"--tools=issue_read,pull_request_read,get_file_contents"
],
"env": {
"GITHUB_APP_ID": "$GITHUB_APP_ID",
"GITHUB_APP_INSTALLATION_ID": "$GITHUB_APP_INSTALLATION_ID",
"GITHUB_APP_PRIVATE_KEY_PATH": "/run/secrets/github-app.pem"
}
}
}
}
```
This MCP server uses a deployment-wide credential. Use it on a trusted Gateway
and scope repository access through the App installation and its permissions.
See the [MCP server's GitHub App authentication guide](https://github.com/github/github-mcp-server/blob/v2.2.0/docs/github-app-auth.md)
for installation-token errors and GitHub Enterprise configuration.
### Bind and verify the agent
Add or update the `github` block in the custom agent's `config.yaml`. Set
`bot_login` to the App slug without `[bot]` and substitute the repository and App
slug below.
```yaml
github:
bot_login: example-review-app
bindings:
- repo: your-org/your-repo
triggers:
issue_comment:
actions: [created]
require_mention: true
```
Restart the Gateway. In Docker, recreate the Gateway container to apply new
environment variables and mounts. In the custom agent's **Plugins and skills**
settings, select `github-app`. On a test PR in the bound repository, post a
conversation comment such as `@example-review-app summarize this pull request`.
Verify that GitHub's Recent Deliveries response lists the agent under
`dispatch.fired_agents` and that the DeerFlow run successfully calls
`pull_request_read`. This checks both the webhook dispatch and the
installation's repository access.
To let the bot post conversation comments, grant the App **Issues: Read and
write** and approve the updated installation permissions if GitHub prompts you.
Add `add_issue_comment` to `--tools`, remove `--read-only`, and restart the
Gateway. The agent must call this tool to post a comment. Its final response is
logged by the GitHub channel. See [GitHub's comment permissions](https://docs.github.com/en/rest/issues/comments#create-an-issue-comment).
## Thread-create Race Recovery
Two webhook deliveries for the same `(repo, number)` can land within milliseconds of each other and race on `threads.create(thread_id=preferred_thread_id)`. The recovery is narrow by design.
```mermaid
sequenceDiagram
autonumber
participant Mgr1 as ChannelManager
(delivery 1)
participant Mgr2 as ChannelManager
(delivery 2)
participant Client as langgraph_sdk client
participant Gateway as Gateway
threads.create
par concurrent deliveries
Mgr1->>Client: client.threads.create(thread_id=preferred)
Client->>Gateway: POST /threads {thread_id}
and
Mgr2->>Client: client.threads.create(thread_id=preferred)
Client->>Gateway: POST /threads {thread_id}
end
Gateway-->>Client: 200 OK (first writer wins)
Gateway-->>Client: 409 ConflictError (second writer)
Client-->>Mgr1: thread (created)
Client-->>Mgr2: ConflictError
Note over Mgr2: Recovery branch
Mgr2->>Client: threads.get(preferred_thread_id)
alt existing
Client-->>Mgr2: thread
Mgr2->>Mgr2: _store_thread_id(msg, preferred_thread_id)
Mgr2-->>Mgr2: reuse deterministic id
else also missing
Client-->>Mgr2: error
Mgr2->>Mgr2: raise (do NOT cache the mapping)
end
```
The recovery is **narrow**: only `langgraph_sdk.errors.ConflictError` (HTTP 409) is treated as a concurrent-create collision. Any other failure (transient DB outage, network error, 5xx) propagates so the delivery can fail/retry rather than silently caching `preferred_thread_id` into the store and mapping every future webhook on this issue/PR to a thread that was never created (every later run would 404 forever with no retry path).
The follow-up `threads.get(preferred_thread_id)` is itself verified before caching — if it also rejects, the store underneath is in an inconsistent state and the failure surfaces.
## Follow-up Buffering While Busy (#4121)
A **different** conflict from the one above: not two deliveries racing to *create* a thread, but a new comment arriving while a *run* is already active on an existing thread. `runs.create()` raises `ConflictError` for that too, and until this fix the manager only logged it and replied with `THREAD_BUSY_MESSAGE` — invisible to the commenter, since `GitHubChannel.send()` is log-only (see below). The comment looked silently ignored.
```mermaid
sequenceDiagram
autonumber
participant C1 as Comment 1
participant C2 as Comment 2 (while busy)
participant Mgr as ChannelManager
participant Client as langgraph_sdk client
participant SB as StreamBridge
C1->>Mgr: InboundMessage
Mgr->>Client: runs.create() [fire_and_forget]
Client-->>Mgr: {run_id: run-1, status: pending}
Mgr->>SB: subscribe(run-1) (background watcher)
C2->>Mgr: InboundMessage (same thread_id)
Mgr->>Client: runs.create()
Client-->>Mgr: 409 ConflictError
Mgr->>Mgr: _buffer_followup(thread_id, msg)
(deduped by delivery_id, capped at 20)
Mgr-->>C2: THREAD_BUSY_MESSAGE (log-only on GitHub)
Note over SB: run-1 completes
SB-->>Mgr: END_SENTINEL
Mgr->>Mgr: _drain_followups_for_thread()
(pop up to 10, oldest first)
Mgr->>Client: runs.create() with input
Client-->>Mgr: {run_id: run-2, status: pending}
Mgr->>SB: subscribe(run-2) (watch again — chains if >10 queued)
```
Key properties:
- **Dedupe**: buffering keys on the GitHub webhook delivery id (falling back to the generic provider-message-id metadata keys, mirroring `_inbound_dedupe_key`), so a redelivered webhook for a comment already buffered is a no-op rather than a duplicate entry.
- **Cap**: 20 entries per thread. Overflow drops the *oldest* buffered entry (not the newest) with a WARNING log — recent activity is a more useful signal than the stalest queued comment once a thread is deep enough in the backlog to hit the cap. No reaction/acknowledgment is sent on drop; see the reactions note below.
- **Batching**: a drain coalesces at most 10 entries into one `` input block. A backlog deeper than 10 is not force-fit into a single turn — the drained run is itself watched, so its own completion triggers another drain cycle for the remainder.
- **Drain-conflict edge case**: if the drain's own `runs.create()` also hits `ConflictError` — e.g. a manual Web UI turn or a scheduled run raced onto the same thread — the popped batch is requeued (not lost, not retried in a tight loop). It is picked up again the next time this manager successfully creates *and watches* a run on that thread, which is guaranteed to eventually happen once whatever is occupying the thread finishes and any subsequent trigger succeeds.
- **Reactions are out of scope for this slice.** GitHub's reaction API (`eyes`/`confused` acknowledgment) has no existing integration in this codebase and needs its own design pass; buffered comments are coalesced silently, with no per-comment acknowledgment.
- **Config**: gated behind `ChannelRunPolicy.buffer_followups_on_busy` (default `False`); GitHub's own registration in `app/gateway/github/run_policy.py` opts in, since it is exactly the fire_and_forget + log-only-send channel this was designed for. Any other channel that adopts `fire_and_forget=True` in the future keeps the old silent-drop-with-log behavior unless it opts in too.
- **Plumbing**: the watcher subscribes to the Gateway's `StreamBridge` singleton (`app.state.stream_bridge`), which `ChannelManager` previously had no way to reach — every other consumer gets it via `get_stream_bridge(request)`, which needs an HTTP `Request` the bus-consumer loop doesn't have. It is threaded as a zero-arg closure from `app.py`'s lifespan (`get_stream_bridge=lambda: getattr(app.state, "stream_bridge", None)`) through `start_channel_service()` → `ChannelService.__init__` → `ChannelManager.__init__`, mirroring the existing `launch_run=lambda **kwargs: launch_scheduled_thread_run(app=app, **kwargs)` closure already used for `ScheduledTaskService` in the same lifespan function.
- **Single-process scope (known limitation)**: the buffer and watcher tasks live in one `ChannelManager` instance's process memory. Under `GATEWAY_WORKERS>1` or a multi-pod deployment, a follow-up comment that a load balancer or webhook fan-out routes to a *different* worker than the one that created the busy run will not see that worker's buffer. This mirrors the cross-pod gap already documented for issue #4120 (IM leader election or a shared buffer store would close it) and is deliberately deferred — single-process/single-pod deployments, the safe default, are unaffected.
## Outbound is Log-only
```mermaid
graph LR
classDef agent fill:#E5D2C4,stroke:#806A5B,color:#30251E
classDef send fill:#C9D7D2,stroke:#5D706A,color:#21302C
classDef gh fill:#D7D3E8,stroke:#6B6680,color:#29263A
Run["GitHub agent run"]:::agent
GitHubTool["Configured GitHub tool"]:::gh
Issue["GitHub issue / PR"]:::gh
Channel["GitHubChannel.send()
(log-only)"]:::send
Log["gateway.log
(INFO line)"]:::send
Run -->|"mid-run intentional writeback"| GitHubTool --> Issue
Run -->|"final assistant message"| Channel --> Log
```
A configured GitHub tool can post comments or open pull requests during a run. The channel logs the final assistant message at INFO.
Why:
- **Multiple agents can bind the same event.** coder + reviewer on a mention would each auto-post a reply, producing two replies per mention even when only one had useful work. Each agent chooses whether to post during its run.
- **The agent often wants to post intermediate updates** (an issue comment linking the PR, a sub-issue comment, a PR description edit). The auto-post-the-final-message contract didn't model that and forced the final message to play double duty.
- **The dispatcher's per-agent `_is_self_event` gate** already prevents comments the agent posts from looping the webhook back into a new run for the same agent.
This is also why the GitHub channel registers `ChannelRunPolicy.fire_and_forget=True`: the manager calls `runs.create()` and returns once the run is `pending`, no outbound ferrying, no SDK 300s `httpx.ReadTimeout` on a legitimate long coder run.
## Cross-references
- [Channel guidance](../app/channels/AGENTS.md#github-channel)
- [IM_CHANNEL_CONNECTIONS.md](IM_CHANNEL_CONNECTIONS.md) — interactive IM channels (Telegram/Slack/etc.) for the full `_handle_chat` and owner-scoped file storage flow
- `app/gateway/github/dispatcher.py` — `fanout_event`, `_is_self_event`, mention precedence chain
- `app/channels/manager.py` — `_buffer_followup`, `_drain_followups_for_thread`, `_watch_run_and_drain_followups` (follow-up buffering while busy, issue #4121)
- `app/gateway/github/identity.py` — `resolve_thread_id` (UUID5), `extract_target`
- `app/gateway/github/triggers.py` — `event_should_fire`, `DEFAULT_TRIGGERS`
- `app/gateway/github/run_policy.py` — `register_policy`
- `app/gateway/routers/github_webhooks.py` — HMAC verify, route mount predicate
- `app/channels/github.py` — `GitHubChannel` (log-only outbound)