CORE LORE / WIKI
SYS DELEGATION ENGINE
Updated 3 weeks ago
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ D E L E G A T I O N E N G I N E ║
║ ✶ M U L T I - M O D E L T A S K R O U T E R ║
║ ║
╠══════════════════════════════════════════════════════════════════════════════╣
║ STATUS: LIVE (scripts only — cockpit pane removed Session 93) ║
║ VERIFIED: 2026-03-06 ║
╚══════════════════════════════════════════════════════════════════════════════╝
⫷✦🜛❂⛬🜞Ω🜚⛬❂🜛✦⫸───────────────────────────────────────────⫷✦🜛❂⛬🜞Ω🜚⛬❂🜛✦⫸
WHAT IT DOES: Multi-model routing — sends tasks to Flash/Pro/Codex based on
complexity and cost. CLI via `delegate.sh`. No cockpit pane.
────────────────────────────────────────────────────────────────────────────────
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SYSTEM GLYPH
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
GLYPH: ✶
UNICODE: U+2736 · SIX POINTED BLACK STAR
MEANING: Overmind subsystem — manages dispatch and delegation routing
WHEN TO USE: SYS docs, delegation routing references, KID tags for
Delegation Engine artifacts
TAGGING: KID:FORGE:OVERMIND:DELEGATE|V:STATUS:DATE:OWNER
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
[ ❖ ] K E Y P A T H S
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
| Component | Path |
|-----------|------|
| delegate.sh (simple wrapper) | `~/.ai_history/delegate.sh` |
| smart-delegate.sh (full router) | `~/.ai_history/smart-delegate.sh` |
| config.json (routing rules) | `~/.ai_history/config.json` |
| Daily session logs | `~/.ai_history/session_YYYY-MM-DD.json` |
| Aggregated stats | `~/.ai_history/routing_stats.json` |
| Active model indicator | `~/.ai_history/.active_model` |
| status-panel.sh | `~/.ai_history/status-panel.sh` |
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
[ ⚡ ] M O D E L R O S T E R
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
| Model | CLI flag | Use case | Input $/M | Output $/M |
|-------|----------|----------|-----------|------------|
| Gemini 3 Flash | `flash` | Default — 90% of tasks, research, coding | $0.50 | $3.00 |
| Gemini 3 Pro | `pro` | Complex reasoning, multimodal, long context | $2.00 | $12.00 |
| GPT-5.3-Codex | `codex` | Bash scripts, terminal automation, DevOps | $1.75 | $14.00 |
| Claude Sonnet 4.6 | — | Brandon's primary interface. Never delegated. | $3.00 | $15.00 |
| Claude Opus 4.6 | — | Architecture, audits, 100K+ context (via Claude Code) | $5.00 | $25.00 |
**Codex rate limit:** 45–225 messages / 5-hour window.
**Decision tree:**
```
THINKING (architecture, security, complex design) → Opus 4.6 (Claude Code)
DOING:
├── Terminal / Bash / DevOps → codex
├── Complex multi-step, long context, multimodal → pro
└── Everything else → flash ← DEFAULT
```
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
[ 🜂 ] d e l e g a t e . s h C L I
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
Simple wrapper. Two arguments: model key and prompt.
```bash
~/.ai_history/delegate.sh <flash|pro|codex> "prompt"
```
**Internals:** Routes to correct CLI → captures output + timing → estimates tokens (char count / 4) → logs to `session_YYYY-MM-DD.json` → writes `.active_model` → calls `sentinel-write.sh` (non-blocking) → prints output + summary line.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
[ ⚙ ] s m a r t - d e l e g a t e . s h ( A u t o R o u t e r )
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Autonomous — determines model from prompt content. Uses waterfall gates from `config.json`.
```bash
~/.ai_history/smart-delegate.sh "prompt"
```
**Waterfall gates (in order):**
| Gate | Trigger | Action |
|------|---------|--------|
| Safety | `rm -rf`, `DROP TABLE`, `DELETE FROM`, `git push --force`, `truncate` | Block — exit 1 |
| Complexity | `architect`, `refactor`, `debug race condition`, `security audit`, `design pattern`, `deadlock`, `concurrency` | Route to `pro` |
| Multimodal | `screenshot`, `image`, `mockup`, `ui bug`, `css layout`, `design` | Route to `pro` |
| Context size | >50,000 estimated tokens | Route to `flash` |
| Default | Everything else | Route to `flash` |
**Self-healing loop:** Execute → if invalid → retry (max 2x) → if still failing → escalate to `pro`. Retry uses original_prompt + concise delta.
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
[ 🜂 ] C O M M O N C O M M A N D S
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
```bash
# Flash — default
~/.ai_history/delegate.sh flash "search for best practices on LLM routing"
~/.ai_history/delegate.sh flash "write a Python function to parse JSON"
# Pro — complex
~/.ai_history/delegate.sh pro "architect a SQLite schema for the mission system"
# Codex — bash/terminal
~/.ai_history/delegate.sh codex "write a bash script to monitor launchd logs"
# Auto-router
~/.ai_history/smart-delegate.sh "analyze this 60k token log file"
# Direct CLI (bypasses logging)
gemini --model gemini-3-flash-preview "prompt"
gemini --model gemini-3-pro-preview "prompt"
codex exec "prompt" --ephemeral --skip-git-repo-check 2>/dev/null
# View today's session log
cat ~/.ai_history/session_$(date +%Y-%m-%d).json | jq .
# View routing stats
cat ~/.ai_history/routing_stats.json | jq .
```
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
[ 🜄 ] S E S S I O N L O G F O R M A T
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
```json
{
"timestamp": "2026-02-22T14:30:00.000Z",
"selected_model": "gemini-3-flash-preview",
"prompt": "...",
"output": "...",
"tokens_in": 120,
"tokens_out": 340,
"tokens_total": 460,
"time_ms": 2100,
"exit_code": 0
}
```
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
[ 📶 ] W E E K L Y R E V I E W T A R G E T S
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
| Model | Target % |
|-------|----------|
| Flash | 80–90% |
| Pro | 5–10% |
| Codex | 5–10% |
| Opus (Claude Code) | <5% |
Goal: <$5/day. Check Fridays.
════════════════════════════════════════════════════════════════════════════════
[ 🝓 ] G O T C H A S
════════════════════════════════════════════════════════════════════════════════
- **PATH stripped in launchd.** Any plist calling delegate.sh must hardcode `/opt/homebrew/bin/gemini` and `/opt/homebrew/bin/codex`. delegate.sh assumes both on PATH — safe from interactive shell only.
- **Codex file-write bug.** Codex uses `cat` internally for file writes (Feb 2026). smart-delegate.sh system prompt bans cat/echo for file ops. Route file-heavy tasks to Flash.
- **Token counts are estimates.** `char_count / 4`. No actual token metadata from CLI. Logged costs are approximations.
- **`set -e` kills retry logic.** delegate.sh uses `set -euo pipefail` — model failure exits immediately (no retry). Use smart-delegate.sh for retry behavior.
- **No timeout.** Neither script has a timeout wrapper. A hung model call hangs indefinitely. Wrap with `timeout 60` in automation.
- **Codex context window is 128K** (vs 1M for Flash/Pro). Route large-context tasks to Flash.
- **config.json pricing stale.** delegate.sh hardcodes codex at $5.00/$15.00 but config.json says $1.75/$14.00. config.json is canonical.
- **Security hardening.** Log files `chmod 600`, log dir `chmod 700`. Validators hard-coded (not config-driven) to prevent RCE via malicious config.
- **No cockpit pane.** MODEL STATS pane removed from FORGE_CLAUDE.kdl (Session 93). Run `status-panel.sh` standalone if needed.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
🜚 SYS_DELEGATION_ENGINE // THE FORGE // ⛬⚚⛬ THE LAW STANDS.
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀