# Router-Based Agent System - Architecture ## 🎯 Why Router Pattern? **Problem with Sequential Flow**: ❌ Agents improvise and skip steps ❌ Agents combine instructions from multiple files ❌ Agents take initiatives in wrong order ❌ Unpredictable behavior **Solution: Router Pattern**: βœ… Check condition β†’ Route to ONE file β†’ Follow ONLY that file βœ… No flow to skip βœ… No improvisation βœ… Predictable, consistent behavior --- ## πŸ”€ Router Architecture ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ USER ACTIVATES AGENT β”‚ β”‚ @freya / @saga / @idunn β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ STEP 1: Show Presentation β”‚ β”‚ β†’ freya-presentation.md β”‚ β”‚ β†’ saga-presentation.md β”‚ β”‚ β†’ idunn-presentation.md β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ STEP 2: ROUTER β”‚ β”‚ instructions.md (router only) β”‚ β”‚ β”‚ β”‚ Check conditions in order: β”‚ β”‚ A β†’ B β†’ C β†’ D β†’ E β”‚ β”‚ STOP at first match β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ COND A COND B COND C COND D COND E β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”‚Outline β”‚ β”‚ Folder β”‚ β”‚Empty β”‚ β”‚New β”‚ β”‚Unknβ”‚ β”‚Exists β”‚ β”‚Structureβ”‚ β”‚Docs β”‚ β”‚Projβ”‚ β”‚own β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”˜ β””β”€β”¬β”€β”€β”˜ β””β”€β”¬β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”‚outline- β”‚ β”‚folder- β”‚ β”‚emptyβ”‚ β”‚new-β”‚ β”‚unknβ”‚ β”‚based- β”‚ β”‚based- β”‚ β”‚proj β”‚ β”‚projβ”‚ β”‚own-β”‚ β”‚analysis β”‚ β”‚analysis β”‚ β”‚resp β”‚ β”‚respβ”‚ β”‚respβ”‚ β”‚.md β”‚ β”‚.md β”‚ β”‚.md β”‚ β”‚.md β”‚ β”‚.md β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”˜ β””β”€β”¬β”€β”€β”˜ β””β”€β”¬β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Present Status β”‚ β”‚ + Options β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ User Selects Task β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ In YOUR Domain Other Domain β”‚ β”‚ β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚Continue β”‚ β”‚agent-handoff-β”‚ β”‚in same β”‚ β”‚guide.md β”‚ β”‚convo β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Hand Over to β”‚ β”‚ Other Agent β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ“ File Structure ``` project-analysis/ β”œβ”€β”€ instructions.md ← ROUTER ONLY β”œβ”€β”€ agent-handoff-guide.md ← Handoff instructions β”œβ”€β”€ analysis-types/ ← Routed destinations β”‚ β”œβ”€β”€ outline-based-analysis.md ← FAST (Condition A) β”‚ β”œβ”€β”€ folder-based-analysis.md ← FALLBACK (Condition B) β”‚ β”œβ”€β”€ empty-project-response.md ← Quick response (Condition C) β”‚ β”œβ”€β”€ new-project-response.md ← Quick response (Condition D) β”‚ └── unknown-structure-response.md ← Quick response (Condition E) └── agent-domains/ ← Agent expertise β”œβ”€β”€ saga-domain.md ← Phases 1-2 β”œβ”€β”€ freya-domain.md ← Phases 4-5, 7 └── idunn-domain.md ← Phases 3, 6 ``` --- ## 🎯 Router Conditions **Checked in order. First match wins.** ``` A: Project Outline Exists? β†’ docs/.wds-project-outline.yaml β†’ .wds-project-outline.yaml IF YES β†’ outline-based-analysis.md (FAST!) B: Docs Folder with Structure? β†’ docs/1-*, docs/A-* folders exist IF YES β†’ folder-based-analysis.md (FALLBACK) C: Empty Docs Folder? β†’ docs/ exists but empty IF YES β†’ empty-project-response.md (QUICK) D: No Docs Folder? β†’ No docs/ folder at all IF YES β†’ new-project-response.md (QUICK) E: Unknown Structure? β†’ docs/ exists but no pattern match IF YES β†’ unknown-structure-response.md (QUICK) ``` --- ## πŸ”‘ Key Principles ### 1. Router is NOT a Flow - Router checks conditions - Router routes to ONE file - Agent follows ONLY that ONE file - No combining files ### 2. Routed Files are Complete - Each analysis file is standalone - Contains all instructions needed - Tells agent exactly what to present - No references back to router ### 3. Agent Domain Files are Reference - Loaded AFTER analysis complete - Used for generating recommendations - Lists "when to stay" vs "when to hand over" ### 4. Handoff Guide is Universal - One handoff pattern for all agents - Clear 4-step process - No copy/paste needed - Seamless agent switch --- ## βœ… Benefits of Router Pattern | Sequential Flow | Router Pattern | | ----------------- | ---------------------- | | Agent improvises | Agent follows ONE file | | Skips steps | No steps to skip | | Unpredictable | Predictable | | Takes initiatives | Follows instructions | | Combines files | Uses ONE file only | --- ## 🎨 Example: Freya Activation **User**: `@freya help me` **Router checks**: 1. Outline exists? β†’ **YES** βœ… 2. Route to: `outline-based-analysis.md` 3. **STOP** (don't check B, C, D, E) **Freya follows outline-based-analysis.md ONLY**: - Reads outline - Presents status - Shows user intentions - Suggests 2-4 options **User**: "I need technical requirements" **Freya checks**: `freya-domain.md` β†’ "Technical requirements" = Phase 3 = Idunn's domain **Freya uses**: `agent-handoff-guide.md` β†’ Hands over to Idunn seamlessly **Idunn activates automatically**: - Shows presentation - Router checks β†’ Outline exists - Routes to: `outline-based-analysis.md` - Reads SAME outline - Continues helping! --- **Router pattern = Predictable, consistent agent behavior!** 🎯