AAnvio
Reference ArchiveSource Docs

Event Hooks

Event-driven hook system for extending Anvio behavior without modifying core code.

3 min read

This is a migrated reference document from the original Anvio source repo. Content is preserved as-is; canonical documentation lives in the sections above in the sidebar.

Original path: docs/31-event-hooks.md

Event-driven hook system for extending Anvio behavior without modifying core code.

Hook Events

HookTriggerPayload
onSessionStartSession created\{ sessionId, agentId, userId \}
onSessionEndSession closed\{ sessionId, reason \}
onGoalCreatedGoal created\{ goalSlug, spec \}
onGoalCompletedGoal completed\{ goalSlug \}
onTaskAssignedKanban task assigned\{ taskId, assignee \}
onTaskCompletedTask moved to done\{ taskId \}
onToolExecutedTool invocation\{ sessionId, toolName, status \}
onWorkflowCompletedBlueprint/automation done\{ workflowId, status \}
onSoulEvolvedSoul updated\{ soulSlug, changes \}
onAutomationFailedAutomation error\{ slug, error \}

Hook Types

TypeDescriptionExample
Local ScriptShell/Node scripthooks/on-session-start.sh
MCP IntegrationCall MCP toolmcp://slack/post-message
WebhookHTTP POSThttps://api.example.com/hooks

File Layout

workspace/hooks/
  hooks.yaml               # Hook registry
  on-session-start.sh
  on-goal-completed.js
  on-tool-executed/
    audit-log.sh

Hook Registry Schema

apiVersion: anvio.io/v1
kind: HookRegistry
spec:
  hooks:
    - event: onSessionStart
      handlers:
        - type: script
          path: hooks/on-session-start.sh
          timeoutMs: 5000
        - type: mcp
          server: slack
          tool: post_message
          args:
            channel: "#anvio"
            text: "Session started: {{sessionId}}"

    - event: onGoalCompleted
      handlers:
        - type: webhook
          url: https://hooks.example.com/goals
          method: POST
          headers:
            Authorization: Bearer ${WEBHOOK_TOKEN}
          body:
            goal: "{{goalSlug}}"
            completedAt: "{{timestamp}}"

    - event: onToolExecuted
      handlers:
        - type: script
          path: hooks/audit-tool.sh
          filter:
            toolName: filesystem.write

Architecture

Rendering diagram…

Sequence: Tool Executed Hook

Rendering diagram…

Handler Contract

Script Handlers

  • Receive JSON payload via stdin
  • Exit 0 on success, non-zero on failure
  • Timeout enforced by timeoutMs
#!/bin/bash
# hooks/on-session-start.sh
payload=$(cat)
session_id=$(echo "$payload" | jq -r '.sessionId')
echo "[$(date)] Session started: $session_id" >> workspace/audit/sessions.log

MCP Handlers

  • Reference server and tool from workspace/mcp/servers.yaml
  • Template variables in args: \{\{sessionId\}\}, \{\{goalSlug\}\}, etc.

Webhook Handlers

  • Retry with exponential backoff (configurable)
  • Sign payloads optionally via HMAC

Filtering

Handlers may include filters:

filter:
  toolName: filesystem.write
  agentId: software-engineer
  column: done

Extension Guide

Status (2026-10-25): items below that mention a plugin or plugin manifest describe design intent. Anvio has no plugin system (ROADMAP T-058); extending this subsystem today means changing the package code.

  1. Add hooks to workspace/hooks/hooks.yaml
  2. Create handler scripts with executable permissions
  3. Register custom events via plugin (extends EventSubjects)

Operational Runbook

ScenarioAction
Disable hookComment out in hooks.yaml
Debug handleranvio hooks test onSessionStart --dry-run
View hook logsworkspace/audit/hooks/
Handler timeoutIncrease timeoutMs or optimize script

Package Boundaries

  • Schema: packages/core/src/schemas/hook.schema.ts
  • Engine: packages/hooks/src/hook-engine.ts
  • Dispatcher: packages/hooks/src/handlers/\{script,mcp,webhook\}.ts

Relationship to Event Bus

Hooks subscribe to existing EventSubjects in packages/events/src/types.ts. New subjects will be added for goal, task, soul, and automation events.

Edit on GitHub

Last updated on

On this page