Hat System & Config Research
**File:** `crates/ralph-core/src/config.rs:1229-1310`
Overview
Hat System & Config Research
HatConfig Schema
File: crates/ralph-core/src/config.rs:1229-1310
pub struct HatConfig {
pub name: String,
pub description: Option,
pub triggers: Vec,
pub publishes: Vec,
pub instructions: String,
pub extra_instructions: Vec,
pub backend: Option,
pub backend_args: Option<Vec>,
pub default_publishes: Option,
pub max_activations: Option<u32>,
pub disallowed_tools: Vec,
}
New fields needed for waves: concurrency, aggregate, isolation.
Hatless Ralph Architecture
File: crates/ralph-core/src/hatless_ralph.rs
Key insight: Ralph is always the executor. Custom hats define topology (pub/sub contracts) that Ralph uses for coordination context, but Ralph handles all iterations.
- Solo mode (no custom hats): Ralph runs directly
- Multi-hat mode: Ralph always executes, custom hats are "personas" with filtered instructions
Hat Selection & Activation
File: crates/ralph-core/src/event_loop/mod.rs:658-672
next_hat()returns first hat with pending events- In multi-hat mode, always returns "ralph"
HatRegistry::get_for_topic()matches events to hats
Prompt Building
File: crates/ralph-core/src/hatless_ralph.rs:258-309
Injection order:
- Core prompt (guardrails, orientation, scratchpad)
- Skill index
- OBJECTIVE section
- ROBOT GUIDANCE
- PENDING EVENTS (all hat events requiring handling)
- Workflow section (unless hat has custom instructions)
- HATS table (full topology for delegation)
- EVENT WRITING guide
- DONE section
HATS Table & Context Injection
File: crates/ralph-core/src/hatless_ralph.rs:596-711
Already generates a topology table:
| Hat | Triggers On | Publishes | Description |
Also generates Mermaid flowchart of event flow.
Existing downstream resolution: For each active hat's publishes, shows which downstream hats receive each event. This is exactly the mechanism needed for NL-driven wave dispatch — inject downstream hat descriptions into the dispatcher's prompt.
Hat Lifecycle
- Registration — HatRegistry loads from config
- Activation — When pending events exist, check max_activations
- Execution — Backend runs with hat-specific prompt context
- Scope enforcement — Hat can only publish declared topics; violations →
<hat_id>.scope_violation - Default publish — If hat publishes nothing, inject
default_publishesevent - Exhaustion — When activation count >= max_activations, emit
<hat_id>.exhausted
Where New Config Fields Go
- HatConfig struct —
crates/ralph-core/src/config.rs:1229-1310 - Hat proto —
crates/ralph-proto/src/hat.rs:43-63 - HatTopology —
crates/ralph-core/src/hatless_ralph.rs:101-150 - Prompt injection —
crates/ralph-core/src/hatless_ralph.rs:596-711 - Validation —
crates/ralph-core/src/config.rs:366-494(RalphConfig::validate())