AI and MCP

Scheduled AI tasks

Bind a natural-language question + persona playbook to a schedule + delivery channel. Server ticks every 60s, runs the same BYOK plan-and-ground path /api/ai/chat uses, delivers via Slack.

What scheduled AI tasks are

A scheduled AI task binds a natural-language prompt + a persona playbook to a schedule + a delivery destination. It answers the “send me weekly ARR every Monday to Slack” use case, grounded through the semantic layer instead of a hand-written SQL string.

The schedule grammar matches the one Goni pipelines use (interval, 5-field cron, @-descriptors, CRON_TZ-prefixed cron, Quartz L). A background sweeper ticks every 60s, enumerates due tasks across every tenant, runs each one through the same plan-and-ground path /api/ai/chat uses, formats the response, and posts it to the delivery destination.

Endpoints on this page

  • GET /api/ai/tasks - tenant-scoped list
  • GET /api/ai/tasks/{id} - single fetch (cross-tenant probe returns 404)
  • POST /api/ai/tasks - create
  • PUT /api/ai/tasks/{id} - update (secret preserved on redacted round-trip)
  • DELETE /api/ai/tasks/{id} - delete
  • POST /api/ai/tasks/{id}/run-now - force-fire once, ignoring schedule

REST endpoints

MethodPathAuthResponse
GET/api/ai/tasksSession or JWT + allow_save{tasks: [...], count}
GET/api/ai/tasks/{id}Session or JWT + allow_savetask object or 404
POST/api/ai/tasksSession or JWT + allow_save201 + task object
PUT/api/ai/tasks/{id}Session or JWT + allow_save200 + task object
DELETE/api/ai/tasks/{id}Session or JWT + allow_save{deleted: id}
POST/api/ai/tasks/{id}/run-nowSession or JWT + allow_save{task_id, status, detail}

Every mutation gates on AllowSave (editor role). A scheduled task can page an on-call rotation or spend the tenant’s BYOK LLM budget, both editor-level concerns.

Scheduled task shape

Wire shape (JSON, redacted on reads):

{
  "id": "at-9c1f0b2e4d80",
  "tenant_id": "acme",
  "owner": "analyst@example.com",
  "name": "Weekly ARR to #metrics",
  "playbook_id": "data-analyst",
  "question": "Give me weekly active_arr by country",
  "schedule": "CRON_TZ=Europe/Berlin 0 9 * * MON",
  "enabled": true,
  "delivery_channel": "slack_webhook",
  "delivery_target": "https://hooks.slack.com/services/T000/B000/xxx",
  "delivery_secret": "***",
  "created_at": "2026-08-18T09:12:04Z",
  "updated_at": "2026-08-18T09:12:04Z",
  "last_run_at": "2026-08-18T09:12:34Z",
  "last_run_status": "ok",
  "last_run_detail": "*Weekly ARR to #metrics*\nQuestion: Give me weekly active_arr by country\nMetric: `active_arr`\nBy: country\nGrain: week\n"
}

Field notes:

  • id server-minted, at- prefix + 12 hex chars.
  • owner is the JWT-authenticated user who created the task; used as the audit-log user attribution on every dispatch.
  • playbook_id empty picks the registry default (data-analyst). An mcp_only playbook is refused at dispatch time (scheduled tasks emit text summaries; only scope: chat playbooks are eligible).
  • schedule grammar reference below.
  • enabled: false pauses dispatch without deleting the task.
  • delivery_channel is slack_webhook.
  • delivery_target is the destination Slack incoming-webhook URL.
  • delivery_secret is an optional HMAC-SHA256 key for signed webhook bodies. Reads always redact it to "***"; PUT preserves the on-disk value when the request body omits delivery_secret or sends it empty, so a redacted round-trip cannot silently wipe the key.
  • last_run_at | last_run_status | last_run_detail are stamped after every dispatch, scheduled or run-now. last_run_status is one of ok | clarify | error | skipped; last_run_detail is truncated at 4000 chars so a monster LLM response or CH stack trace does not bloat the YAML.

Schedule grammar

schedule accepts the same shapes Goni pipelines accept. It is validated at PUT/POST time so a typo cannot land silently on disk and no-op every tick. Alerts and scheduled reports use a simpler interval-only grammar (see Alerts and scheduled reports).

ShapeExampleMeaning
Interval5m, 15m, 1h, 6h, 1dFires every N units
Cron (5-field)30 2 * * *Airflow-style, UTC by default
Cron @-descriptor@daily, @hourly, @every 5mStandard crontab macros
Cron with timezoneCRON_TZ=Europe/Berlin 0 9 * * MON-FRIIANA zone name
Quartz L / L-N0 23 L * *Last day of month at 23:00

@reboot is rejected. Quartz W and # are not implemented. See Pipelines: Schedule grammar for the full reference and the /api/schedule/preview endpoint.

Delivery channels

Channeldelivery_target
slack_webhookSlack incoming-webhook URL

slack_webhook formats the LLM response as a short Slack-friendly text block (name, question, grounded metric, dimensions, time grain, optional filters, explanation) and POSTs it via the same signed-delivery path pipelines use. The same SSRF guard, shared HTTP client, and HMAC signing apply verbatim, so private-IP or metadata-endpoint URLs are refused the same way.

Scheduler cadence

One leader-elected sweeper, 60s tick, per-task 90s dispatch timeout.

Per tick:

  1. The sweeper reads every scheduled-task YAML file across every tenant.
  2. For each enabled task, the shared schedule parser plus a due-at check against last_run_at decides due-ness.
  3. Each due task is dispatched sequentially. The dispatch mirrors /api/ai/chat end-to-end: the tenant’s BYOK API key is decrypted, the hourly token budget is checked (and the tick is skipped if the window is exhausted), the playbook is resolved (unknown id or mcp_only scope refuses), the catalog is built from the compiled semantic project, and the plan runs under the same 0.2 temperature the chat handler uses.
  4. The rendered text summary is delivered to the configured channel.
  5. last_run_at | last_run_status | last_run_detail are written back to the YAML file atomically (temp write + rename).
  6. The audit log records action=ai.task.run with an origin derived from the playbook id, so /audit groups scheduled dispatches under the same ai:<playbook> bucket as their interactive-chat siblings.

Because the scheduler holds no state across restarts, a process bounce resumes without duplication: the on-disk last_run_at timestamp is the durable cadence anchor.

Sequential per-task dispatch (not fan-out) is deliberate. A 60s tick with one-at-a-time dispatch caps throughput at roughly one call per second in the worst case, well under any BYOK provider’s default rate limit.

The fresh-task-fires-first-tick invariant

A newly-created task has last_run_at IS NULL. The shared due-at check treats a zero anchor as “due now”, so a fresh task fires on the very first sweeper pass after creation instead of sitting idle for one full cron period. This matches pipeline, alert, and report “never ran = due now” convention, and it is pinned by a CI test so a regression that silently delays every new task by one full period fails at build time.

Force-fire with run-now

POST /api/ai/tasks/{id}/run-now dispatches the task once, ignoring the schedule. Useful for verifying the delivery destination without waiting for the next cron boundary. Runs synchronously so the response body carries the outcome, and records it on the task like a scheduled run:

{
  "task_id": "at-9c1f0b2e4d80",
  "status": "ok",
  "detail": "*Weekly ARR to #metrics*\nQuestion: Give me weekly active_arr by country\nMetric: `active_arr`\n..."
}

Same auth (allow_save), same audit-log row as a scheduled dispatch, with trigger: "manual" in the detail payload.

End-to-end example

Create a task that fires every Monday at 09:00 Berlin time to a Slack channel:

curl -sS -X POST https://your-goni-host/api/ai/tasks \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GONI_PAT" \
  -d '{
        "name": "Weekly ARR to #metrics",
        "playbook_id": "data-analyst",
        "question": "Give me weekly active_arr by country",
        "schedule": "CRON_TZ=Europe/Berlin 0 9 * * MON",
        "enabled": true,
        "delivery_channel": "slack_webhook",
        "delivery_target": "https://hooks.slack.com/services/T000/B000/xxx"
      }'

Fetch it a minute later to confirm the first tick populated last_run_*:

curl -sS https://your-goni-host/api/ai/tasks/at-9c1f0b2e4d80 \
  -H "Authorization: Bearer $GONI_PAT"

Force a delivery for testing without waiting for the next cron window:

curl -sS -X POST https://your-goni-host/api/ai/tasks/at-9c1f0b2e4d80/run-now \
  -H "Authorization: Bearer $GONI_PAT"

RBAC and audit

Every mutation gates on AllowSave (editor role). Scheduled tasks page on-call rotations and spend BYOK budget, so a viewer session cannot create, edit, delete, or run-now a task.

Every dispatch appends an audit row with action=ai.task.run and origin=ai:<persona> (for example ai:data-analyst, ai:data-engineer, ai:data-architect, ai:diagnostic). Filtering the /audit UI by origin=ai:* therefore surfaces both scheduled dispatches and their interactive-chat siblings in one bucket. See Audit and lineage.

BYOK token budget interaction

Each dispatch consumes tokens against the tenant’s hourly BYOK budget, the same window /api/ai/chat and /api/ask use. When the budget is exhausted, the sweeper stamps last_run_status: "skipped" with a detail like hourly token budget exhausted - resets in 42m0s and moves on. Nothing is delivered.

A task on 5m cadence spending against a starter plan (200,000 tokens/hr) will run roughly 288 dispatches per day. Before scheduling aggressive cadences, peek the current window with:

curl -sS https://your-goni-host/api/ai/budget \
  -H "Authorization: Bearer $GONI_PAT"

See Ask: per-tenant hourly token budget for plan tiers and reset semantics.

Last updated