AAnvio
Workflows & Automation

Automation Engine

Configuration-driven cron, event, goal, and workflow triggers that run a blueprint, agent, hook, or learning-loop action — no code changes required.

6 min read

The Automation Engine executes recurring and event-driven work without manual intervention. Every automation is a single YAML file under workspace/automations/, parsed by AutomationRegistry and run by AutomationEngine (packages/automation/src/automation-engine.ts).

Trigger types

automationTriggerSchema (packages/core/src/schemas/automation.schema.ts) is a discriminated union of four trigger types:

TypeFieldsFires when
cronschedule, timezone (default UTC), catchUp, maxCatchUpThe cron expression matches the current minute
eventevent, optional filter (key/value match)A matching event is published on the event bus
goalevent: goal.created | goal.completed | goal.progress.updated, optional goalSlugA Goal fires that lifecycle event
workflowevent: workflow.completed, optional workflowIdA Workflow run completes

The schema does not include a webhook trigger type. If you're looking for inbound-webhook-triggered automation, it isn't implemented yet — only cron, event, goal, and workflow exist in automationTriggerSchema.

Action types

automationActionSchema is a discriminated union of five action types:

TypeFieldsBehavior
blueprintblueprint, inputsRuns a Blueprint via BlueprintExecutor.run()
agentagent, inputRuns a named agent directly with a fixed input string
hookhookExecutes a local hook script with the event payload
batchblueprint, optional inputPathNot implemented — ActionExecutor.executeOnce() throws Batch action requires Phase C (U13)
learningoperation: summarize_sessionsCalls into the Learning Loop's summarizeStaleSessions()

Not yet shipped

type: batch automations are defined in the schema but always fail at execution time with Batch action requires Phase C (U13) (packages/automation/src/action-executor.ts). Don't wire a production automation to a batch action yet.

Three real examples from workspace/automations/

Cron + blueprint (enabled)

# workspace/automations/daily-summary.yaml
apiVersion: anvio.io/v1
kind: Automation
metadata:
  slug: daily-summary
  enabled: true
spec:
  description: Summarize notifications every morning
  trigger:
    type: cron
    schedule: "0 8 * * *"
    timezone: UTC
  action:
    type: blueprint
    blueprint: daily-summary
    inputs:
      userId: local-user
      channel: cli
  retry:
    maxAttempts: 2
    backoff: exponential

Cron in a named timezone, with failure notification (enabled)

# workspace/automations/weekly-architecture-review.yaml
# Runs every Monday at 09:00 (Asia/Jakarta).
# Test now: anvio automation run weekly-architecture-review --force
apiVersion: anvio.io/v1
kind: Automation
metadata:
  slug: weekly-architecture-review
  enabled: true
spec:
  description: Weekly architecture review of the Anvio monorepo
  trigger:
    type: cron
    schedule: "0 9 * * 1"
    timezone: Asia/Jakarta
  action:
    type: blueprint
    blueprint: architecture-review
    inputs:
      scope: monorepo
      userId: local-user
      channel: cli
  retry:
    maxAttempts: 2
    backoff: exponential
  onFailure:
    notify: true

This automation's action.blueprint: architecture-review points at the real workspace/blueprints/architecture-review.yaml blueprint — a complete, working example of a scheduled automation driving a blueprint end to end.

Learning-loop action (disabled by default)

# workspace/automations/session-memory-summarize.yaml
apiVersion: anvio.io/v1
kind: Automation
metadata:
  slug: session-memory-summarize
  enabled: false
spec:
  description: Periodic LLM/rule-based session summarization for cross-session recall
  trigger:
    type: cron
    schedule: "0 */6 * * *"
    timezone: UTC
  action:
    type: learning
    operation: summarize_sessions
  retry:
    maxAttempts: 2
    backoff: exponential

This one is enabled: false in the repo — a template you switch on rather than a live automation.

Architecture

Rendering diagram…

AutomationEngine.start() loads every automation, hands cron-triggered ones to an internal CronScheduler, and subscribes event- and goal-triggered ones directly to the event bus (subscribeCore). workflow-triggered automations rely on the AUTOMATION_COMPLETED → WORKFLOW_COMPLETED republish that happens after every successful automation run (see Events below) rather than a dedicated workflow subscription path in this class.

Cron scheduling — real implementation details

The cron scheduler is a hand-written parser (packages/automation/src/cron-parser.ts), not node-cron. It supports standard 5-field cron syntax: exact values, *, ranges (1-5), lists (1,3,5), steps (*/15), and day-of-week names (MON-SUN).

// packages/automation/src/automation-engine.ts — CronScheduler.tick()
async tick(now = new Date()): Promise<string[]> {
  for (const automation of this.automations) {
    if (!cronMatches(trigger.schedule, now)) continue;
    // skip if already run in this exact minute
    if (state.lastRunAt && sameMinute(state.lastRunAt, now)) continue;
    await this.onTrigger(automation);
  }
}

CronScheduler.start() ticks once immediately, then every 60 seconds (setInterval). Each tick compares now against every enabled cron automation and fires any whose schedule matches the current minute and that haven't already run in that same minute — this is what makes the scheduler safe to restart without duplicate-firing mid-minute.

The timezone field is part of the automation schema and defaults to UTC, but cronMatches() (packages/automation/src/cron-parser.ts) evaluates the cron expression against the JavaScript Date object's local time fields (getHours(), getMinutes(), etc.) with no timezone conversion applied. If your process runs in a different timezone than spec.trigger.timezone declares, verify actual firing behavior before relying on it for anything time-sensitive.

TODO: confirm whether timezone conversion is applied elsewhere in the call chain (e.g. at the automation-loading layer) before treating this as a hard limitation — it was not found in cron-parser.ts or CronScheduler.tick() during this review.

catchUp and maxCatchUp are present in the schema (for skipping or replaying missed runs after downtime) but no catch-up logic was found wired into CronScheduler — treat them as forward-looking schema fields until verified against a newer build.

Failure handling & retry

ActionExecutor.execute() (packages/automation/src/action-executor.ts) retries the action up to spec.retry.maxAttempts times:

  • backoff: exponential (default) — waits 2^(attempt-1) * 1000 ms between attempts
  • backoff: fixed — waits a flat 1000ms

On final failure, the automation's run is recorded as failed in state and an AUTOMATION_FAILED event is published with the error. onFailure.notify: true (see weekly-architecture-review above) signals the harness to notify; onFailure.hook can point at a local script.

State persistence

FilesystemAutomationStateStore (packages/automation/src/filesystem-state-store.ts) writes one JSON file per automation to workspace/automations/_state/{slug}.json:

{
  "slug": "daily-summary",
  "lastRunAt": "2026-07-03T08:00:00.000Z",
  "lastStatus": "completed",
  "runCount": 42
}

This is what makes the scheduler restart-safe — on restart, CronScheduler reloads this state and won't re-fire an automation that already ran in the current minute.

Events

EventPublished when
AUTOMATION_STARTEDBefore an automation's action executes
AUTOMATION_COMPLETEDAfter a successful run — also re-publishes WORKFLOW_COMPLETED with { workflowId: slug, status: 'completed' }
AUTOMATION_FAILEDAfter all retries are exhausted

CLI

anvio automation run daily-summary            # respects enabled flag
anvio automation run daily-summary --force     # ignore enabled: false
anvio cron list
anvio cron next-runs --limit 5

The CLI exposes automation and cron as separate top-level command groups (cmdAutomation, cmdCron in apps/cli/src/main.ts). Verify exact subcommand flags for anvio cron add/anvio cron remove against the CLI source before publishing them as guaranteed syntax — this page only lists the commands confirmed to exist as a registered case in main.ts, not every flag combination.

Edit on GitHub

Last updated on

On this page