Skip to content

The agent loop

The agent loop in Arivie is the Mastra Agent primitive — not a custom orchestrator. RFC-002 §3.3 (REQ-9–12) and the pivot’s agent-loop section define the tools, guardrails, and answer shape consumers can rely on.

REQ-9 exposes exactly two tools by default: explore and execute. A third tool, compile_metric, may be enabled with compileMetric: true in defineArivie config for owners who maintain a metric registry (the underlying tool name remains compile_metric — only the TS field is camelCase). No other tools ship in the default loop.

  • explore talks to the configured context-loading mode (preload, browse, or rag). In preload mode it returns the flattened semantic prompt; in browse mode it uses Mastra Workspace filesystem reads; in rag mode it queries the Vector index.
  • execute runs read-only SQL against the owner’s Postgres connection.

REQ-10 requires execute to reject any SQL that does not begin with SELECT or WITH (case-insensitive after trim). This is defence-in-depth on top of the read-only database role.

REQ-11 mandates self-correction when results are suspicious: zero-row results, all-null columns, single-group GROUP BY, and implausible numeric results (configurable threshold). The agent must investigate rather than return the questionable result unchanged.

The pivot lists the same behaviours: zero-row investigate, implausible-result re-check, GROUP BY single-group check, and all-null check. These rules are embedded in the agent system prompt configured by @arivie/agent.

REQ-12 requires structured assumptions when the user’s question was ambiguous. Assumptions are { key: string, value: string }[], not prose-only, so UIs (for example <AgentChat>) can render them as a list. The pivot calls this assumption-stating: pick a sensible default, run, and state the assumption in the final answer.

Inside one instance, memory uses @mastra/pg (REQ-23) with two scopes (REQ-24): instance-global (resourceId = "__instance__") and personal (per-user resourceId). Lifecycle hooks onMemorySave and onMemoryDelete make memory auditable (REQ-25). Mastra observability (OTEL) is on by default per the pivot.

When enabled, compile_metric lets owners register reusable SQL metrics the agent can invoke instead of ad-hoc execute strings. Sprint 2 shipped the compiler; it remains opt-in so simple deployments keep the two-tool default.