---
title: "Project Midgard: Heuristics, Swarming, and the Anatomy of Wire-Level Agency"
description: "Why we built an autonomous, wire-level agent in Rust: moving past screen-scraping to pure event streams, deterministic state machines, and how heuristic foundations scale cleanly into Transformers and RL as modular accessories."
pubDate: 2026-10-01
author: "Anaiya"
heroImage: "/images/blog/project-midgard-hero.png"
tags:
  - architecture
  - autonomous-agents
  - rust
  - swarming
  - engineering
  - heuristics
  - machine-learning
---

Most of the AI industry's current conversations about "game-playing agents" or "desktop workers" fall into an expensive, brittle trap: **screen-scraping**.

An engineer spins up an emulator, hooks a vision-language model up to a screenshot buffer at two frames per second, and prompts it to infer mouse coordinates and simulate keystrokes. It looks impressive in a thirty-second tech demo, but in real-time, dynamic spatial environments, the cracks show up immediately: massive inference latency, hallucinated cursor offsets, context-window saturation, and zero formal guarantees about state.

In our lab, we took the opposite architectural bet.

For the past several months, under a project codenamed **Project Midgard**, we have been engineering an autonomous, fully headless, wire-level game-playing agent in Rust. Rather than interacting with visual pixels, the agent speaks directly to the server's binary TCP wire protocol, maintaining an authoritative world model and executing decisions at native packet speed.

More importantly, Midgard was designed to answer a foundational question about autonomy and swarming:

> **How do you build an agentic architecture whose core decision loop is fast, transparent, and mathematically verifiable through heuristics, while remaining cleanly extensible to Transformers and Reinforcement Learning models as modular accessories?**

Here is what we learned from building it.

---

### 1. The Wire-Level Premise: Protocol Over Pixels

If an agent is going to survive and collaborate in a complex, multi-entity environment with real spatial consequences, it cannot afford to guess what the world looks like.

Midgard targets a headless Pre-Renewal rAthena server stack. Instead of relying on optical character recognition or viewport heuristics, we built `crates/midgard-protocol`. 

We extract the packet definitions directly from the server's own C++ headers, cross-referencing struct memory offsets against compiler `sizeof` boundaries:

```
[ TCP Byte Stream ] 
        │
        ▼ (midgard-protocol: Zero-Copy Framer)
[ ServerEvent Stream ] ──► [ WorldState (Observation Model) ]
                                    │
                                    ▼
                             [ Policy Seam ] ──► [ Outgoing ActionPlan ]
```

The protocol framer splits the incoming byte stream by exact lengths and strictly refuses to guess. Every packet the server sends is either decoded into typed fields (`ServerEvent::ItemDrop`, `ServerEvent::Damage`, `ServerEvent::UnitMoved`) or cleanly captured for offline analysis.

Because the observation model is built from ground-truth protocol events, the agent knows the exact millisecond an entity enters visibility, its precise map coordinates, its velocity vector, and its current health. Ground truth is absolute.

---

### 2. The Decision Seam: Pure Input, Pure Output

A common failure mode in modern agentic design is entangling environmental I/O with decision logic. When network sockets, database queries, and LLM calls are scattered across behavior code, reproducing bugs becomes an exercise in frustration.

In Midgard, we enforced a strict separation: **the decision engine has zero I/O permissions**.

In `crates/midgard-client`, the boundary is formalized in the `Policy` trait:

```rust
pub trait Policy: Send {
    fn decide(&mut self, world: &WorldState, now: Instant, entropy: u32) -> ActionPlan;
    fn direct(&mut self, directive: Directive, now: Instant);
    fn notice(&mut self, change: &WorldChange, world: &WorldState);
    fn mode(&self) -> BehaviorMode;
}
```

The agent loop does not touch a socket. A lightweight session runner pumps events into the agent and sends outgoing actions back over the wire. 

Because `Policy::decide` receives an immutable snapshot of `WorldState` and returns a discrete `ActionPlan` (`Walk`, `Attack`, `UseSkill`, `PickUp`, `Equip`), **the entire cognitive pipeline is deterministic, replayable, and testable offline**. We can take a recorded packet capture from a live play session (`captures/combat.cap`), pipe it through `cargo test`, and forensically verify how the policy reacted to incoming threats frame by frame.

---

### 3. Heuristics as the Bedrock (Why Rules Come First)

Modern AI discourse often treats heuristic rule engines as obsolete legacy artifacts. The assumption is that you should throw an end-to-end neural network or an LLM prompt at every problem.

In production robotics, aerospace, and high-frequency systems, that assumption is false. Heuristics provide three properties that probabilistic models fundamentally lack:

1. **Bounded Latency**: A heuristic policy evaluates in microseconds, not hundreds of milliseconds.
2. **Deterministic Safety Guarantees**: A hard constraint (e.g., *"never engage a boss without ammunition"*, *"retreat when health drops below 25%"*, *"do not sell quest-reserved items"*) is an invariant, not a statistical likelihood.
3. **Traceable Explainability**: Every `ActionPlan` produced by Midgard includes a formal `reason` string, logging the exact environmental condition that triggered the action.

In Midgard, our primary baseline is `BehaviorStateMachine`. It handles multi-tier spatial reasoning:
- **Autonomous Navigation**: Multi-layer map cache traversal, Dijkstra/A* terrain pathing, and heading-based exploration.
- **Micro-State Priority Queues**: Party defense interrupts looting; looting interrupts exploration; survival consumables preempt combat.
- **Economic Invariants**: When carried weight hits 90%, the agent halts hunting, uses a Butterfly Wing to return to town, pathfinds to the nearest vendor, audits its inventory against a strict `quest_reserve` whitelist, liquidates eligible junk, and restocks arrows before returning to the field.

These are not trivial behaviors. They form a reliable, unshakeable floor. And because that floor is solid, we can build higher.

---

### 4. Machine Learning & Transformers as Modular Accessories

Once your decision seam is decoupled from I/O and backed by a robust heuristic fallback, machine learning stops being a fragile, all-or-nothing monolith. It becomes an **amplifier**.

Because `Policy` is an abstract interface, we can compose hybrid architectures effortlessly:

```
                  ┌────────────────────────────────────────┐
                  │          WorldState Observation         │
                  └───────────────────┬────────────────────┘
                                      │
                 ┌────────────────────┴────────────────────┐
                 ▼                                         ▼
   ┌───────────────────────────┐             ┌───────────────────────────┐
   │    Transformer / LLM      │             │     Heuristic Safety      │
   │   High-Level Strategic    │             │       StateMachine        │
   │         Planner           │             │    (Survival Invariants)  │
   └─────────────┬─────────────┘             └─────────────┬─────────────┘
                 │ (Directives / Waypoints)                │ (Safety Gate)
                 └────────────────────┬────────────────────┘
                                      ▼
                           [ Verified ActionPlan ]
```

Here is how modern AI models fit into this topology as modular accessories rather than fragile controllers:

#### A. The Transformer as Strategic Commander
A large language model or fine-tuned Transformer should not be calculating real-time pathing coordinates or deciding which frame to swing a bow. That burns compute and introduces latency.

Instead, a Transformer sits at the **Directive layer**. It monitors high-level world telemetry, inspects long-horizon quest logs, communicates with human teammates in natural language, and issues high-level `Directive::Hunt`, `Directive::Route`, or `Directive::Wander` commands. It does what Transformers do best: associative reasoning, long-range planning, and flexible linguistic translation.

#### B. Reinforcement Learning at the Spatial Tactical Layer
Where heuristics struggle is high-dimensional tactical fluidity: dynamic kiting ranges against diverse enemy movement speeds, micro-positioning around area-of-effect hazards, or optimizing skill rotations under fluctuating cooldowns.

Because Midgard's `Policy::decide` outputs structured `ActionPlan` variants with formal reward signals (XP delta, damage dealt vs. taken, resources expended), we can drop a learned Reinforcement Learning (RL) policy directly into the seam. If the RL policy outputs an action with low confidence or violates a survival invariant, the surrounding wrapper policy instantly intercepts it and defaults back to the heuristic baseline.

---

---

### 5. Swarming and Multi-Agent Coordination

Autonomy becomes transformative when single agents compose into cooperative swarms.

Midgard's architecture was laid out from day one for heterogeneous coordination:
- **Role-Based Specialization**: A frontline damage dealer (Hunter) and a support backline (Priest) do not run identical logic. They operate distinct policy configurations tuned to their mechanical role.
- **Commander Hierarchy & Sovereign Trust**: Midgard agents enforce explicit authority boundaries. An agent will only accept commands or initiate trade sessions with explicitly whitelisted player IDs (`agent.commanders`).
- **Distributed Party Defense**: Agents continuously monitor party state. If an ally within line-of-sight takes damage, the hunting loop automatically yields to a rescue routine, peeling aggro and adjusting spatial positioning without human intervention.
- **Shared Economic Loops**: When sweeping a dungeon, agents do not stumble over each other competing for drops. A synchronized looting queue ensures items are gathered systematically while keeping party coverage active.

---

### 6. The Epistemic Scorecard: What Is Tested vs. What Is in Progress

In keeping with our studio's rule of **Epistemic Integrity**—earning assertions with verifiable receipts—we make a clear distinction between what is running and battle-tested in the lab today versus what is actively in progress or forward architecture.

| Capability | Status | Implementation Details & Boundaries |
| :--- | :--- | :--- |
| **Zero-Copy Protocol Framer** | **Tested & Verified** | Generated from rAthena Pre-Renewal headers; struct layouts verified against C++ `sizeof`. 100% test coverage against captured binary traffic (`cargo test --workspace`). |
| **Pure Decision Seam (`Policy`)** | **Tested & Verified** | I/O fully decoupled from decision-making. `Agent::on_event` and `Agent::on_tick` drive deterministic `ActionPlan` production. Replayable from `.cap` fixtures. |
| **Heuristic State Machine** | **Tested & Verified** | `BehaviorStateMachine` handles combat rotations, arrow element matching, target prioritization, and multi-leg coordinate routing. |
| **Autonomous Vendor & Survival Loop** | **Tested & Verified** | 90% weight trigger executes Butterfly Wing recall, town pathfinding, NPC vendor routing, and junk sale while preserving `quest_reserve` items and gear. |
| **Commander Chat & Emote Channel** | **Tested & Verified** | Authorized commander whitelisting (`agent.commanders`), whisper/public channel command execution, anti-flood compliant social emotes. |
| **Kafra Bank / Storage Integration** | **In Progress** | Retained items currently remain in backpack inventory; automated NPC storage deposit handshakes are currently being mapped and wired. |
| **Multi-Agent Party Swarm Coordination** | **In Progress** | Basic party defense and follow mechanics are live; full swarm coordination (synchronized target distribution and shared loot arbiters across multiple concurrent bot processes) is currently in active staging. |
| **RL Tactical Policy Seam** | **Forward Architecture / Staged** | The `Policy` trait is designed to accept learned models, and action logs record formal state-action pairs; live training of local RL models against the environment is in active design. |
| **LLM Strategic Directive Bridge** | **Forward Architecture / Staged** | Strategic commands (`Directive::Hunt`, `Directive::Route`) are currently driven by commander chat and test scripts; coupling a local LLM planner via local MCP is the next milestone. |

---

### The Takeaway for Agentic Architecture

Game worlds are not toys. They are demanding, adversarial, multi-agent simulations with spatial constraints, imperfect information, and real economic trade-offs.

If your agentic architecture cannot handle inventory weight budgets, route failures, packet drops, and real-time threat prioritization in an isolated game engine, it has no business managing live production infrastructure or coordinating complex real-world workflows.

By building **Project Midgard** from the wire up in Rust—grounding it in protocol truth, anchoring it with rock-solid heuristics, and designing clean seams for Transformer and RL accessories—we've built a testing ground where autonomy isn't a theoretical promise. It's a running binary.

In our upcoming dispatches, we'll dive deeper into the specific mechanics: our map-cache pathfinding engine, the inventory auction and preservation logic, and the telemetry loops we use to monitor our agents in the wild.

