Skip to main content

Agent Runs

An agent run executes a configured agent headlessly — no chat UI, no human at the keyboard. Runs are the programmatic core of NexSpace Headless AI: the same durable path backs API-initiated runs, automations (schedule/event triggers), and the nexspace agents run CLI command. Every run requires the agents:run scope on the API key.

Lifecycle

Runs are persisted durably (headless_agent_runs) — state survives server restarts and is readable across instances. Orphaned runs are reconciled and old runs are pruned on a schedule.

Start a run

A second run started while one is already executing for the same agent returns 409 AGENT_BUSY — the one-run-per-agent guard prevents overlapping runs.

Run identity

Each run acts as runAsUserId, which drives tool scope and RBAC — a run can never do more than that user is permitted. It defaults to the caller; binding a different user is restricted to internal operators (otherwise 403 RUN_AS_FORBIDDEN).

Poll, list, and cancel

Stream a run

Stream run events as they happen over Server-Sent Events:
Each SSE frame carries a monotonic id. If the connection drops, reconnect with a Last-Event-ID header to replay the frames you missed — the same resumable transport described in MCP. The CLI does this for you: nexspace agents run 42 --output-format stream-json.

Approvals

When a run invokes a tool that requires approval (see autonomy), it transitions to pending_approval and exposes pendingApprovalIds instead of executing unattended. List the pending actions, then approve or reject — approving resumes the agent loop, it does not merely run the one tool.

Autonomy

An agent’s autonomy setting decides what happens on a risky write:
  • require_approval — pause at pending_approval and wait for a human (or an approver workflow wired to the webhook below).
  • auto_execute — perform the write unattended (use only when the writes are safe to run without review).

Lifecycle webhooks

Subscribe to run lifecycle events instead of polling (see Webhooks):
  • agent_run.pending_approval
  • agent_run.completed
  • agent_run.failed

See also

  • Automations — fire runs on a schedule or business event.
  • MCP — the tool layer runs invoke.
  • CLI commandsnexspace agents run.