opboxDocs
Sign inBook a demo
DocsAutonomous WorkerAI - Agent

AI Agent - Autonomous Worker

The Opbox AI Agent is a server-side autonomous task processor. It picks up tasks from the AgentTask queue, runs an agentic tool-calling loop, and writes the result back. Two execution paths compete for the same queue - whichever claims first wins.

Architecture

PathRuntimeHow it works
In-process workerInside the main appCalls the resolved provider directly (Anthropic Messages API for provider=anthropic; OpenAI-compatible /v1/chat/completions for provider=openclaw with the opbox-specific headers + optional CF Access pair). Tool execution runs in-process. Credentials resolve through BYOK Credentials, which honours the OpenClaw-takes-precedence rule. OpenAI direct provider is not yet supported by the worker.
Agent BridgeSeparate Node.js processSpawns Claude Code CLI as a child process. Communicates via stdin/stdout NDJSON. Connects back to Opbox via MCP. See Agent Bridge.

Both paths use the same AgentTask queue, the same atomic-claim lease state machine, the same autonomy enforcement, the same cost ledger. The only difference is which process runs the agentic loop.

Task Lifecycle

PENDING → CLAIMED → PROCESSING → DONE
                              ↘ FAILED / TIMED_OUT / CANCELLED
StateMeaning
PENDINGTask created, awaiting a worker.
CLAIMEDA worker has claimed the task; lease (30 min) is held.
PROCESSINGThe agentic loop is running.
DONELoop completed successfully. Result written.
FAILEDLoop errored or budget gate rejected.
TIMED_OUTTerminal timeout after the retry/wall-clock policy is exceeded.
CANCELLEDCancelled by an admin via the UI/API.

Trigger Sources

Tasks are created automatically by fire-and-forget triggers in existing routes. The sourceType field records which trigger fired:

TriggersourceType
Step assignment to agentASSIGNMENT
@-mention of agent in commentMENTION
Auto-assignment on step entryASSIGNMENT
Webhook from external system (pipeline ingest)WEBHOOK
Cron-driven recurring taskCRON
Self-spawned (depth ≥ 1) within an agentic loopSELF
Manual creation via POST /api/agent-tasksMANUAL
Chat-initiated (chat-to-agent handoff)CHAT

A trigger only fires when the agent member is the assignee or mentioned party.

AgentTask Schema

FieldTypeDescription
idstringTask ID
workspaceIdstringWorkspace scope
agentUserIdstringThe Opbox Agent member identity
createdByIdstringThe user who triggered the task (or null for cron/webhook)
title / promptstringHuman-readable label and the actual instruction
statusenumSee lifecycle above
priorityenumLOW / NORMAL / HIGH
resultstring?Final output (rendered as a comment on the source matter step)
errorMessagestring?On FAILED / TIMED_OUT
progressTextstring?Live progress (updated by the bridge during execution)
costUsd / durationMs / toolCallCountmetricsCaptured on completion
sourceType / sourceId / sourceMatterId / sourceStepIdlinkageWhat triggered the task
depth / parentTaskIdint / string?Self-spawning depth (max 5)
claimTokenstring?SHA-256 hash of the active claim token - required for progress/complete calls
claimedAt / startedAt / completedAt / timeoutAttimestampsLifecycle markers

Bridge API (MCP API Key Auth)

These endpoints are auth via Authorization: Bearer cp_live_... + X-MCP-Client header (same as MCP routes). No session cookies.

EndpointMethodPurpose
/api/agent-tasks/[taskId]/claimPOSTAtomic claim. Returns plaintext claimToken + task details including autonomy level.
/api/agent-tasks/[taskId]/progressPATCHUpdate progressText. Requires claimToken.
/api/agent-tasks/[taskId]/completePATCHTransition to DONE/FAILED. Accepts result, costUsd, durationMs, toolCallCount. Runs shared post-completion side effects.

Admin API (Session Auth)

EndpointMethodPurpose
/api/agent-tasksGETList tasks (pagination, status filter). ADMIN/OWNER.
/api/agent-tasksPOSTCreate a manual task. ADMIN/OWNER + CSRF.
/api/agent-tasks/[taskId]GETFull task details.
/api/agent-tasks/[taskId]PATCHCancel (PENDING/CLAIMED/PROCESSING only). ADMIN/OWNER + CSRF.
/api/agent-tasks/[taskId]/retryPOSTRetry FAILED/TIMED_OUT. Creates a new task with parentTaskId. ADMIN/OWNER + CSRF.

UI Surface

  • /agent-tasks page under Automations - paginated list with status filters and detail drawer.
  • AutomationsSidebar.tsx - live queue counter and recent-tasks drawer.

Atomic Claim Lease

Both worker paths claim a task through the same atomic claim primitive. The lease lasts 30 minutes. If the worker crashes mid-task, the lease expires and the task becomes claimable again for a fresh/resume claim.

agent_queue_extend (MCP tool) lets a worker push the lease forward when it knows it'll need longer. Useful for doc-generation runs that legitimately take 5+ minutes.

Adaptive Briefing

agent_queue_get_briefing (MCP tool) returns a context-shaped briefing for the claimed task. For doc-gen tasks, the briefing includes:

  • Matter payload (summarised)
  • Step config and current data
  • Coverage gate result (PASS/WARN/FAIL)
  • Detected variables / sections / tables on the template

The agent uses the briefing to decide how to proceed without separate tool calls for each piece of context.

Security Controls

ControlDetail
Default autonomy levelL0 (read-only). Both in-process worker and MCP claim endpoint default to 0 unless the workspace agent config explicitly raises it.
Autonomy enforcementA single autonomy enforcer runs before every tool call on both paths.
Injection scanningPrompt-injection scanning runs on tool results, page-context payloads, MCP bridge results, and manual task prompts.
Task depth limitMaximum self-spawning depth: 5. Prevents runaway loops.
Task lease30 minutes; expired leases are claimable again.
Concurrency cap (in-process)3 concurrent tasks.
Result size cap50KB per tool result.
Iteration cap (in-process)25 agentic-loop iterations.
API key role gatePOST /api/agent/api-keys requires ADMIN/OWNER. Autonomy 0-3 is validated on input.
Rate-limit asymmetryMCP bridge enforces per-tool-tier + per-key global limits. In-process worker uses iteration cap and task timeout only.

Cost Tracking

Every model call inside the loop writes one AiCostLedger row (operationType: "agent"). The total per-task cost is summed at completion and stored in AgentTask.costUsd. The keySource follows the BYOK resolver chain - if the workspace has an org-level Anthropic key, the cost is attributed to ORG_KEY; if it has a workspace override, WORKSPACE_KEY.

Per-task budget gating runs before the loop starts. If the workspace is over its hard cap, the task fails immediately with BUDGET_EXCEEDED.

MCP Tool Surface for the Queue

Five MCP tools cover the queue lifecycle: agent_queue_list, agent_queue_claim, agent_queue_get_briefing, agent_queue_release, and agent_queue_extend. These let an external bridge (or any MCP client with adequate autonomy) drain the queue from outside the main app.

See Also

We use cookies

Strictly necessary cookies keep you signed in and protect requests. We also use optional cookies for preferences and (when enabled) analytics. Learn more.