Agent Routing
Automatically pass outputs between AI agents with intelligent transformation.
Agent routing is MadoHub's core feature. When one agent completes a task, MadoHub can automatically extract the result, transform it into an actionable prompt, and deliver it to another agent.
How it works
- Trigger — A connected source tile fires based on the configured trigger type (idle detection, completion, keyword match, or continuous).
- Extract — MadoHub reads the agent's last response. For Claude Code, this uses JSONL session files for clean extraction. For other agents, it reads terminal output directly.
- Transform — The extracted output is transformed into a prompt for the target agent. This can use AI-powered rewriting, template wrapping, or raw passthrough.
- Deliver — The transformed prompt is typed into the target terminal with human-like delays (50–80ms per line), then submitted with the agent-appropriate Enter sequence.
JSONL session resolution
For Claude Code, MadoHub extracts clean responses by reading session log files:
- Find the terminal's tmux pane, then walk the process tree (
ps -eo pid=,ppid=,comm=) breadth-first to the first descendant whose command name contains claude/codex/amp/opencode — that PID is the agent process. - Read
~/.claude/sessions/{PID}.json(keyed by the agent's PID, not by session id) and extract itssessionIdfield. - Scan every subdirectory of
~/.claude/projects/for a file named{sessionId}.jsonl— that's the transcript. - Parse the JSONL and extract the last assistant message.
This gives much cleaner content than raw terminal scraping, avoiding ANSI codes and formatting artifacts.
Separately, cwd_to_project_dir() maps a tile's working directory to a project directory by replacing / with -, falling back to a substring search when no exact match exists.
Trigger types
| Type | Behavior |
|---|---|
| on-idle | Waits for a completion-keyword match, then 8 seconds of silence (IDLE_DELAY_MS). Default and most reliable. |
| on-complete | Handled identically to on-idle: waits for the same completion-keyword match, then the same 8-second silence delay before firing. |
| on-keyword | Fires when a custom regex pattern matches in output. |
| always | Streams all new terminal output continuously. |
Transform types
| Type | Description |
|---|---|
| ai-routing | Uses an AI model to generate a context-aware prompt for the target agent. Requires API key in settings. Supports custom templates. |
| prompt-wrap | Wraps output in a user-provided template. Variables: {output}, {round}, {maxRounds}, {source}. |
| full-output | Sends the last 30 lines of terminal output as-is. |
| raw | Sends the last 10 lines of terminal output. |
| summary | Sends an AI-generated summary of the output. |
Routing dispatch defaults to the madoagent path (also selectable: legacy, shadow).
Content delivery
MadoHub types content into the target terminal line by line with 50–80ms delays to simulate human input. The Enter key behavior is agent-specific:
- Claude Code, Codex, Amp, OpenCode: Double carriage return (
\r\r) to submit. Codex has an 800ms pre-delay with 200ms between the two presses; the other agents use a 500ms pre-delay. - Shell: Single newline (
\n) to submit.
Loop protection
Routing connections have built-in safeguards to prevent infinite loops:
| Setting | Default | Range | Description |
|---|---|---|---|
| maxRounds | 5 | 1–20 | Maximum routing cycles before auto-deactivation. |
| cooldownMs | 3s | 1–30s | Minimum delay between triggers. |
| stopKeyword | — | Any string | Stops routing when detected (e.g., "LGTM"). |
Example workflow
Terminal 1 (Claude Code) Terminal 2 (Claude Code)
┌──────────────────────┐ → ┌──────────────────────┐
│ "Refactor auth module"│ │ Receives: │
│ │ │ "Review the refactor │
│ ✓ Done — 3 files │ │ in src/auth/..." │
└──────────────────────┘ └──────────────────────┘Agent 1 refactors the code. On completion, MadoHub reads the JSONL response, uses AI routing to generate a review prompt with the specific files and changes, and delivers it to Agent 2.