MadoHub Docs

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

  1. Trigger — A connected source tile fires based on the configured trigger type (idle detection, completion, keyword match, or continuous).
  2. 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.
  3. Transform — The extracted output is transformed into a prompt for the target agent. This can use AI-powered rewriting, template wrapping, or raw passthrough.
  4. 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:

  1. 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.
  2. Read ~/.claude/sessions/{PID}.json (keyed by the agent's PID, not by session id) and extract its sessionId field.
  3. Scan every subdirectory of ~/.claude/projects/ for a file named {sessionId}.jsonl — that's the transcript.
  4. 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

TypeBehavior
on-idleWaits for a completion-keyword match, then 8 seconds of silence (IDLE_DELAY_MS). Default and most reliable.
on-completeHandled identically to on-idle: waits for the same completion-keyword match, then the same 8-second silence delay before firing.
on-keywordFires when a custom regex pattern matches in output.
alwaysStreams all new terminal output continuously.

Transform types

TypeDescription
ai-routingUses an AI model to generate a context-aware prompt for the target agent. Requires API key in settings. Supports custom templates.
prompt-wrapWraps output in a user-provided template. Variables: {output}, {round}, {maxRounds}, {source}.
full-outputSends the last 30 lines of terminal output as-is.
rawSends the last 10 lines of terminal output.
summarySends 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:

SettingDefaultRangeDescription
maxRounds51–20Maximum routing cycles before auto-deactivation.
cooldownMs3s1–30sMinimum delay between triggers.
stopKeyword—Any stringStops 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.

On this page