Pipeline System

The pipeline system provides a hook-based processing framework that wraps every stage of the agent turn lifecycle. Core and plugin hooks execute at 12 distinct pipeline stages with configurable priority, timeout protection, and rich modification capabilities.

Architecture

┌──────────────────────────────────────────────────────────┐
│                    Pipeline System                         │
│                                                           │
│  12 Pipeline Stages (in order):                            │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐               │
│  │pre-assess│→│post-assess│→│pre-reason │→ ...            │
│  └──────────┘  └──────────┘  └──────────┘               │
│       │              │              │                      │
│       ▼              ▼              ▼                      │
│  ┌────────────────────────────────────────────┐          │
│  │          Registered Hooks (prioritized)     │          │
│  │  Core Hooks + Plugin Hooks                  │          │
│  └────────────────────────────────────────────┘          │
└──────────────────────────────────────────────────────────┘

12 Pipeline Stages

StageWhenPurpose
pre-assessBefore assessmentPre-filter, context preparation
post-assessAfter assessmentValidate assessment output
pre-reasonBefore reasoningModify reasoning context
post-reasonAfter reasoningValidate reasoning output
pre-toolBefore tool executionTool call filtering
post-toolAfter tool executionTool result processing
pre-llmBefore LLM callModify LLM request
post-llmAfter LLM callProcess LLM response
pre-reflectBefore reflectionModify reflection context
post-reflectAfter reflectionProcess reflection output
pre-outputBefore final outputModify user-facing response
post-outputAfter final outputPost-response side effects

Hook Interface

interface PipelineHook {
  name: string;
  stages: PipelineStage[];
  priority: number;
  async: boolean;
  disableable: boolean;
  run(ctx: PipelineContext): Promise<HookResult>;
}

interface HookResult {
  abort?: boolean;
  modifyInput?: string;
  modifyLLMResponse?: string;
  modifyOutput?: string;
  injectMessages?: Message[];
  sideEffects?: SideEffect[];
}

Built-in Core Hooks

HookStageDescription
Content Safetypre-llmPrompt injection detection, PII redaction
Output Truncationpost-llmTruncate responses at 8K tokens
Summarizationpre-llmAuto-compact context at 80K tokens
Loop Detectionpost-toolDetect >5 same-file edits, escalate
Token Budgetpre-llmEnforce per-turn token limits
Reflection Injectionpre-llmInject reflection patterns into context
MQM Routingpre-llmRoute model selection via Quartermaster
Validationpre-toolRun custom validation plugins

Hook Registration

Hooks can be registered by:

  • Core system on startup
  • Plugins via the plugin manager
  • Custom scripts via the hooks API

Each hook has a priority (0–100) determining execution order within a stage. Hooks have timeout protection (5s sync, 15s async) to prevent pipeline stalls.

Pipeline Context

Hooks receive a read-only PipelineContext with:

  • sessionId, turnId — Current session/turn identifiers
  • tokensUsed, costUsd — Current consumption metrics
  • toolCallsMade — Count of tool calls this turn
  • setState(key, value) — Mutate pipeline state

See also: Agent Loop, Plugin System