<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Blog for Heimdall]]></title><description><![CDATA[Blog for Heimdall]]></description><link>https://wertter.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 23:41:46 GMT</lastBuildDate><atom:link href="https://wertter.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Heimdall - your PR buddy]]></title><description><![CDATA[Every open-source maintainer knows this rhythm. A pull request opens. Someone asks whether the fix actually fixes anything. Someone else points at a…]]></description><link>https://wertter.hashnode.dev/heimdall-your-pr-buddy</link><guid isPermaLink="true">https://wertter.hashnode.dev/heimdall-your-pr-buddy</guid><dc:creator><![CDATA[Orange Cat]]></dc:creator><pubDate>Sat, 07 Nov 2026 17:50:26 GMT</pubDate><content:encoded><![CDATA[<h1>The Problem with PR Review: Why We Built Heimdall</h1>
<p>Every open-source maintainer knows this rhythm. A pull request opens. Someone asks whether the fix actually fixes anything. Someone else points at a missing test, a missing label, an issue that was never linked. The thread grows. The maintainer repeats the same checklist. And somewhere between the third reminder and the fifth rebase, a bad fix gets merged because nobody had the energy to re-verify it after the last push.</p>
<p>AI coding tools have made this worse, not better. It is cheaper than ever to generate a diff that looks plausible and fails in exactly the way a tired reviewer will not notice. Letting an agent post to GitHub unsupervised is a supply-chain incident waiting to happen.</p>
<p><strong>Heimdall is built for the gap between "the diff looks fine" and "I am confident enough to comment on it."</strong></p>
<hr />
<h2>The Origin Story</h2>
<p>We didn't set out to build a PR triage tool. We set out to solve our own problem: the maintainer burnout that comes from being the bottleneck on every decision.</p>
<p>The breaking point came when we realized that the "AI revolution" in code review was mostly generating more noise. Tools that auto-comment on PRs without human oversight create a false sense of security. A bot saying "LGTM" on a diff it doesn't actually understand is worse than no review at all.</p>
<p>We wanted something different: <strong>agents that do the grunt work, but humans who keep the authority.</strong></p>
<hr />
<h2>What We Actually Built</h2>
<p>Heimdall runs inside a <strong>Mattermost + Switch AI</strong> room. Three agents — <strong>Huginn</strong> the reproducer, <strong>Loki</strong> the sceptic, and <strong>Heimdall</strong> the desk — test and question every PR. Nothing reaches GitHub until a maintainer approves the exact saved draft text, verified by SHA-256 hash.</p>
<p>The rule that shapes everything: <strong>agents may think, test, and draft; only an approval with a matching hash lets Gjallarhorn write to GitHub.</strong></p>
<hr />
<h2>The Five Problems We Solved</h2>
<table>
<thead>
<tr>
<th>Problem</th>
<th>Our Answer</th>
</tr>
</thead>
<tbody><tr>
<td><strong>"Does this fix actually work?"</strong></td>
<td>Huginn runs the repo's own test command against the PR head <em>and</em> the base commit, then reports paired results. Verdicts rest on saved run data, not vibes.</td>
</tr>
<tr>
<td><strong>Reviews that never finish</strong></td>
<td>Loki scans the changed lines, cites findings by <code>file:line</code>, caps at five, and refuses anything that isn't a concrete defect introduced by the diff.</td>
</tr>
<tr>
<td><strong>The repeating maintainer checklist</strong></td>
<td>Intake checks linked issues, assignees, required labels, open-PR limits, CLA, and CI once, in plain code, and posts the result to the triage room.</td>
</tr>
<tr>
<td><strong>Unsupervised AI posting to GitHub</strong></td>
<td>Gjallarhorn — the only writer to GitHub — posts only when an approval row exists whose <code>draft_hash</code> matches the exact text being posted. No approval, no comment. No hash match, no comment.</td>
</tr>
<tr>
<td><strong>Reproduction you cannot trust</strong></td>
<td>Huginn's GitHub Actions workflow runs with <code>contents: read</code>, no secrets, and the test command passed through <code>env</code> — never interpolated into a shell script.</td>
</tr>
</tbody></table>
<hr />
<h2>How a PR Moves Through the System</h2>
<p>Every pull request flows through the same stages. Only <code>triage</code> changes a PR's state, through one pure transition function: <code>triage.domain.next_state(...)</code>.</p>
<p><strong>1. Webhook Intake</strong> — GitHub delivers events. Signatures verified, deliveries deduplicated on <code>X-GitHub-Delivery</code>.</p>
<p><strong>2. Intake — Plain-Code Rule Checks</strong> — Six checks (linked issue, labels, assignee, CLA, CI, open-PR limit) run against the PR head. Results stored per rule per commit.</p>
<p><strong>3. Reproduction — Huginn</strong> — Runs the validated test command with and without the fix on GitHub Actions (read-only, no secrets, max 3 attempts). Submits a verdict: <code>reproduced</code>, <code>not_covered</code>, <code>setup_failed</code>, or <code>inconclusive</code>.</p>
<p><strong>4. Review — Loki</strong> — Reviews the current diff with read-only tools. Up to 5 findings, each cited <code>file:line</code>. Tone filter strips guesses about how code was written.</p>
<p><strong>5. Desk — Heimdall</strong> — Summarizes intake, reproduction, and review into a Switch card. Renders draft replies to maintainer commands (<code>/heimdall approve|revise|fix|retry</code>). Hashes every draft with SHA-256.</p>
<p><strong>6. Approval</strong> — Maintainer reacts 👍 to a draft. Approval records the exact draft hash. Mechanical tasks (lint, typo, missing import) use <code>approver = "policy:auto_suggest"</code> — still through an approval row, still hash-bound.</p>
<p><strong>7. Publish — Gjallarhorn</strong> — Delivers the GitHub comment only while the stored <code>draft_hash</code> still matches. If the draft was revised, the hash mismatches and nothing posts. Every approval appends an audit row.</p>
<hr />
<h2>The Three Agents: Specialized, Not General</h2>
<table>
<thead>
<tr>
<th>Agent</th>
<th>Role</th>
<th>Output</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Huginn</strong></td>
<td>Reproducer</td>
<td>Paired test runs + <code>HuginnVerdict</code></td>
</tr>
<tr>
<td><strong>Loki</strong></td>
<td>Sceptic</td>
<td>Up to 5 findings cited <code>file:line</code>, or clean pass</td>
</tr>
<tr>
<td><strong>Heimdall</strong></td>
<td>Desk</td>
<td>Switch card, draft replies, mechanical tasks, command parsing</td>
</tr>
</tbody></table>
<p>All three run on the same agentic runtime (<code>agents/_runtime.py</code>). The model chooses steps; the runtime enforces tool allow-lists, JSON-schema validation, secret scanning, tone filtering, context budgets, turn/token limits, and a single <code>submit_*</code> exit. Agents never import a vendor LLM SDK — the provider is config (<code>LLM_DEFAULT_*</code> falling back per agent via <code>LLM_HUGINN_*</code>, <code>LLM_LOKI_*</code>, <code>LLM_HEIMDALL_*</code>), never a code change.</p>
<hr />
<h2>Why Mattermost + Switch AI?</h2>
<p>We didn't choose Switch because it's trendy. We chose it because it's the only platform that treats AI agents as <strong>first-class teammates in a chat room you own</strong>.</p>
<p><strong>What Heimdall implements on Switch:</strong></p>
<ol>
<li><strong>Three agents as room participants</strong> — Each registers via the agent protocol (WebSocket), gets its own identity and API key, posts threaded replies, responds to mentions (<code>@loki review this</code>).</li>
<li><strong>One thread per PR</strong> — The triage card is the root; intake checklist, Huginn's verdict, Loki's findings, Heimdall's draft all reply in that thread. Context stays in one place.</li>
<li><strong>Built-in auth, authorization, approval cards</strong> — <code>apps.bridge.setup</code> handles agent registration, addressing policies (only triage room members can mention agents), Mattermost↔Switch room linkage, and native approval cards for the 👍 flow.</li>
<li><strong>Separation: agents vs. infrastructure</strong> — Agents run as independent processes with their own WebSocket. Worker posts via transactional outbox using agent identities. Bridge (Bifröst) relays reactions back. A compromised agent can't post as another; outbox guarantees idempotent delivery.</li>
<li><strong>Provider-agnostic AI</strong> — Heimdall's agents use an <code>AiAdapter</code> (config via <code>LLM_DEFAULT_*</code> / <code>LLM_&lt;AGENT&gt;_*</code>). Switch ships no models — you bring your own (Claude Code, Codex, OpenCode, any OpenAI-compatible API). Switching models = config change, not code change.</li>
<li><strong>Self-hosted, no vendor lock-in</strong> — Mattermost + Switch run on your infrastructure (Docker Compose, Postgres). No SaaS dependency. You own the conversation history, agent keys, and audit trail.</li>
<li><strong>Proven pattern</strong> — The Switch AI <a href="https://docs.switchagents.ai/switch-rooms/use-cases/open-source-contribution">"Open source: review a contribution" use case</a> describes this exact pattern. The <a href="https://dev.to/yash_thokal_a1899d36b0351/i-gave-my-github-repo-an-ai-triage-team-that-cant-do-anything-without-my-yes-1h6n">Flip the Switch hackathon</a> produced "Maintainer Desk" and "Issue Desk" projects implementing the same human-in-the-loop triage pattern.</li>
</ol>
<hr />
<h2>The Architecture Philosophy: Boring Infrastructure, Sharp Boundaries</h2>
<p>We didn't invent new patterns. We composed proven ones:</p>
<ul>
<li><strong>Module borders</strong> — Import only through <code>api.py</code>. Never reach into another module's <code>models.py</code>.</li>
<li><strong>Pure domain</strong> — <code>domain.py</code> takes values, returns values. No FastAPI, SQLModel, githubkit, httpx, or adapters.</li>
<li><strong>Ports and adapters</strong> — Services depend on Protocols in <code>ports.py</code>. Wiring happens in <code>apps/*/main.py</code>.</li>
<li><strong>One transaction per use case</strong> — Commit everything or nothing.</li>
<li><strong>Outbox for side effects</strong> — Never call GitHub or Switch from a service. Write an outbox row; the sender delivers with an idempotency key.</li>
<li><strong>Jobs in Postgres</strong> — Procrastinate jobs enqueued inside the same transaction as the state change.</li>
<li><strong>State machine owns PR state</strong> — Only <code>triage</code> changes <code>prs.state</code>, only through <code>triage.domain.next_state(...)</code>.</li>
<li><strong>Tenancy everywhere</strong> — Every table has <code>installation_id</code>. Every query filters on it. No exceptions.</li>
</ul>
<p>This isn't clever. It's discipline. It means the logic stays testable, the outside world stays replaceable, and the system doesn't surprise you at 2 AM.</p>
<hr />
<h2>Architecture: How the Pieces Fit Together</h2>
<p>At a high level, Heimdall is a <strong>state machine wrapped around three agents</strong>, with Postgres as the source of truth and an outbox guarding every external write.</p>
<pre><code class="language-markdown">flowchart LR
    GH[GitHub PR Events] --&gt; API[Webhook API
    Verify + Persist]
    API --&gt; DB[(Postgres
    Events + Job Queue)]
    DB --&gt; TRIAGE[Triage State Machine
    next_state(...)]
    TRIAGE --&gt; OUT[Transactional Outbox
    Idempotency Keys]
    OUT --&gt; SWITCH[Switch Room
    Thread per PR]
    TRIAGE --&gt; HUGINN[Huginn Reproducer
    GitHub Actions]
    HUGINN --&gt; DB
    TRIAGE --&gt; LOKI[Loki Reviewer
    Read-Only Diff]
    LOKI --&gt; DB
    HUGINN --&gt; DESK[Heimdall Desk
    Card + Draft]
    LOKI --&gt; DESK
    DESK --&gt; OUT
    OUT --&gt;|Approval| APPROVAL[Approval Row
    draft_hash]
    APPROVAL --&gt; GJALLARHORN[Gjallarhorn
    Only GitHub Writer]
    GJALLARHORN --&gt; GH
    DB --&gt; AUDIT[(Audit Log
    Every Decision)]
</code></pre>
<h3>The Flow in Plain English</h3>
<p><strong>1. GitHub knocks</strong> — A webhook arrives. The API verifies the signature, deduplicates on <code>X-GitHub-Delivery</code>, and writes the event to Postgres in the same transaction that queues triage handling.</p>
<p><strong>2. Triage decides</strong> — The state machine (<code>triage.domain.next_state</code>) is the only thing that can change a PR's state. It moves <code>received -&gt; checking</code> and enqueues Huginn and Loki in parallel. On failure, it moves to <code>intake_failed</code>.</p>
<p><strong>3. Agents work</strong> — Huginn runs your test command on the PR head and base commit (GitHub Actions, read-only, no secrets). Loki reviews the diff with read-only tools. Both write results back to Postgres.</p>
<p><strong>4. Desk assembles</strong> — The Heimdall agent reads the stored results, builds a Switch card, renders a draft, and hashes it with SHA-256. The draft sits in the outbox, waiting.</p>
<p><strong>5. Human approves</strong> — You react 👍 in the Switch thread. The Bridge (Bifröst) records an approval row with the exact <code>draft_hash</code>.</p>
<p><strong>6. Gjallarhorn delivers</strong> — The only component that writes to GitHub. It re-checks the hash matches <em>at post time</em>, then posts the exact approved text. If you edited the draft, the hash mismatches and nothing posts.</p>
<p><strong>7. Everything audited</strong> — Every approval, auto-suggest, repro dispatch appends an audit row in the same transaction. You can reconstruct any decision.</p>
<h3>Why This Architecture?</h3>
<table>
<thead>
<tr>
<th>Principle</th>
<th>What It Buys Us</th>
</tr>
</thead>
<tbody><tr>
<td><strong>State machine owns state</strong></td>
<td>No surprise transitions; PR state is predictable and testable</td>
</tr>
<tr>
<td><strong>Outbox for side effects</strong></td>
<td>No duplicate GitHub comments; no lost Switch posts; crash-safe</td>
</tr>
<tr>
<td><strong>Pure domain, no I/O</strong></td>
<td>Unit tests run in milliseconds; no mocks needed</td>
</tr>
<tr>
<td><strong>Ports + adapters</strong></td>
<td>Swap GitHub for GitLab, Switch for Slack, Postgres for SQLite in tests</td>
</tr>
<tr>
<td><strong>Tenancy everywhere</strong></td>
<td>Multi-installation from day one; no data leakage</td>
</tr>
<tr>
<td><strong>One transaction per use case</strong></td>
<td>Atomicity by default; no partial states</td>
</tr>
<tr>
<td><strong>Agents only</strong> <code>submit_*</code></td>
<td>Runtime enforces tool allow-lists, budgets, tone filters; agents can't go rogue</td>
</tr>
</tbody></table>
<p>This architecture isn't exciting. That's the point. It's boring infrastructure that does one thing well: <strong>guarantee that nothing reaches GitHub without a maintainer's explicit, hash-verified approval.</strong></p>
<hr />
<h2>Security by Design, Not Bolted On</h2>
<ul>
<li><strong>PR content is untrusted data</strong> — Titles, bodies, diffs, comments, test output wrapped as <code>&lt;pr_data&gt;</code> data. Never followed as instructions. Never passed to a shell.</li>
<li><strong>Only Gjallarhorn writes to GitHub</strong> — Only against an approval row whose hash matches at posting time.</li>
<li><strong>Mechanical tasks still go through approval</strong> — <code>auto_suggest</code> uses <code>approver = "policy:auto_suggest"</code> but still requires a hash-matched approval row.</li>
<li><strong>No</strong> <code>pull_request_target</code> — Repro workflows get <code>contents: read</code> only. Test command via <code>env</code>, never shell interpolation.</li>
<li><strong>Tokens, keys, diffs never logged</strong> — Only IDs.</li>
</ul>
<hr />
<h2>The Best Use Case: Hash-Bound PR Triage with Human-in-the-Loop</h2>
<p>This is what Heimdall exists for: <strong>end-to-end PR triage where AI does the work but humans keep the authority — enforced cryptographically.</strong></p>
<h3>The Flow</h3>
<ol>
<li><strong>Intake</strong> (plain code) checks linked issue, labels, assignee, CLA, CI — results posted to the PR thread</li>
<li><strong>Huginn</strong> runs the repo's test command on PR head + base commit on GitHub Actions (read-only, no secrets) → paired verdict</li>
<li><strong>Loki</strong> reviews the diff with read-only tools → up to 5 findings, each cited <code>file:line</code></li>
<li><strong>Heimdall (Desk)</strong> builds a summary card + draft reply, hashes the draft with SHA-256</li>
<li><strong>Maintainer</strong> reacts 👍 to the draft in Switch → approval row recorded with <code>draft_hash</code></li>
<li><strong>Gjallarhorn</strong> (the only GitHub writer) re-checks the hash matches <em>at posting time</em> → posts comment</li>
</ol>
<h3>Why This Works</h3>
<table>
<thead>
<tr>
<th>Property</th>
<th>How It's Achieved</th>
</tr>
</thead>
<tbody><tr>
<td><strong>No unsupervised AI writes</strong></td>
<td>Only Gjallarhorn writes; only with matching <code>draft_hash</code></td>
</tr>
<tr>
<td><strong>Maintainer keeps final say</strong></td>
<td>👍 on exact draft text; edited draft = hash mismatch = no post</td>
</tr>
<tr>
<td><strong>Audit trail</strong></td>
<td>Every approval, auto-suggest, repro dispatch logged in same transaction</td>
</tr>
<tr>
<td><strong>Reproducible verification</strong></td>
<td>Huginn's paired runs (with/without fix) stored as artifacts</td>
</tr>
<tr>
<td><strong>Grounded reviews</strong></td>
<td>Loki's findings cite <code>file:line</code>; tone filter removes speculation</td>
</tr>
<tr>
<td><strong>Fits your workflow</strong></td>
<td>Runs in your Mattermost/Switch, uses your GitHub App, your test command</td>
</tr>
</tbody></table>
<h3>What Makes It Unique</h3>
<ul>
<li><strong>Hash-binding at post time, not draft time</strong> — A revised draft can never slip out under an old approval</li>
<li><strong>Auto-suggest still goes through approval</strong> — Mechanical fixes require a hash-matched approval row</li>
<li><strong>Reproduction is isolated</strong> — GitHub Actions gets <code>contents: read</code> only, test command via <code>env</code>, no <code>pull_request_target</code></li>
<li><strong>Tenancy everywhere</strong> — Every table has <code>installation_id</code>; every query filters on it; no cross-repo leakage</li>
</ul>
<p>This use case — <strong>AI-assisted triage with cryptographic human approval</strong> — is what Heimdall was built for. Everything else (module architecture, outbox, state machine, agent runtime) exists to make this flow reliable, auditable, and secure.</p>
<hr />
<h2>Try It Locally</h2>
<pre><code class="language-bash"># 1. Install and migrate
uv sync
docker compose up -d postgres
uv run alembic upgrade head

# 2. Configure
cp .env.example .env
# Fill in HEIMDALL_GITHUB_APP_ID, HEIMDALL_GITHUB_WEBHOOK_SECRET,
# HEIMDALL_SWITCH_URL, SMEE_URL and the LLM defaults.
# Put the GitHub App private key at secrets/github-app.pem.

# 3. Set up Switch rooms and tokens (safe to re-run)
docker compose up -d
uv run python -m apps.bridge.setup

# 4. Start the full stack
docker compose --profile app --profile tunnel up -d --build
</code></pre>
<p>Install the GitHub App on a repo with Actions enabled, subscribe to <code>pull_request</code>, <code>workflow_run</code>, <code>check_suite</code>, <code>installation</code>, <code>installation_repositories</code>, then open a PR and watch its thread appear in the <code>triage</code> room.</p>
<p><strong>Fast tests (no DB, no network):</strong></p>
<pre><code class="language-bash">uv run pytest tests/unit
uv run ruff check . &amp;&amp; uv run ruff format --check .
uv run pyright
</code></pre>
<hr />
<h2>The Philosophy</h2>
<p>We didn't build Heimdall because we wanted more AI in the loop. We built it because we wanted <em>less</em> — less noise, less repetition, less "looks good to me" on code that doesn't work.</p>
<p>The agents do the grunt work: they run the tests, they read the diff, they cite the lines. The maintainer does the one thing only a maintainer can do: <strong>decide</strong>.</p>
<p>If that sounds like the workflow you've been missing, <a href="docs/index.md">read the docs</a> or <a href="README.md#quickstart">jump straight to the quickstart</a>.</p>
<hr />
<p><em>Heimdall — the gatekeeper who never sleeps, and never lets a bad commit through without a witness.</em></p>
]]></content:encoded></item></channel></rss>