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 10 distinct pipeline stages with configurable priority, timeout protection, and rich modification capabilities.

Architecture

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

10 Pipeline Stages

StageWhenPurpose
pre-assessBefore assessmentPre-filter, context preparation
post-assessAfter assessmentValidate assessment output
pre-reasonBefore reasoningModify reasoning context / LLM request
post-reasonAfter reasoningValidate reasoning / process LLM response
pre-toolBefore tool executionTool call filtering
post-toolAfter tool executionTool result processing
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

HookStagePriorityDescription
@cortex/injection-guardpre-reason5Prompt injection detection
@cortex/model-quartermasterpre-reason, post-reason5MQM intelligent model selection
@cortex/quartermasterpre-tool, post-tool6Tool orchestration learning
@cortex/summarizationpre-reason8Context compaction at 80K token threshold
@cortex/content-safetypre-output10Content filtering before output
@cortex/loop-detectionpre-tool12Per-file edit tracking (warns after 5+ edits)
@cortex/tool-output-sandboxpost-tool15Large output capture to session storage
@cortex/pre-completion-checklistpost-reason20Build-Verify-Fix enforcement
@cortex/audit-logpost-output150Session/turn logging (non-disableable)
@cortex/cost-trackerpost-tool, post-output200Token/cost metrics

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