AI Autopilot (Workflows + Queues Foundation)
This document describes the initial implementation for durable DM AI execution with retries, recovery, and timeline visibility.
What Is Implemented
- Durable AI job tables:
ai_jobsai_job_events
- Gateway enqueue endpoint:
POST /api/gateway/dm-autopilot
- Executor endpoint:
POST /api/ai/jobs/execute
- Watchdog sweep endpoint:
POST /api/ai/jobs/sweep
- Timeline lookup endpoint:
GET /api/ai/jobs/:jobId:jobIdcan be numeric DB id orcorrelation_id
- Dedicated queue consumer worker scaffold:
orchestrator-worker/
Feature Flags and Secrets
Pages/Gateway env vars:
DM_AUTOPILOT_ENABLED=trueAI_AUTOPILOT_INTERNAL_KEY=...AI_RETRY_ASSESSOR_MODE=bounded(orllmfor bounded LLM stub mode)
Worker env vars:
SPACEBOT_API_BASE(Pages URL)AI_AUTOPILOT_INTERNAL_KEY(same as Pages)
Queue Binding
Pages producer binding is expected as:
AI_AUTOPILOT_QUEUEbound to queuespacebot-ai-autopilot
The consumer is configured in orchestrator-worker/wrangler.toml.
Request Flow
- Gateway receives DM and checks manager access.
- If local-runner dispatch does not take over and
DM_AUTOPILOT_ENABLED=true, gateway callsPOST /api/gateway/dm-autopilot. - Job is persisted as
pending+ timeline eventjob.queued. - Job message is sent to queue.
- Orchestrator worker consumes queue and calls
POST /api/ai/jobs/execute. - Executor claims job atomically (
pending -> running), runsgenerateChatResponse, then DMs user. - On transient failure, bounded retry decision schedules
next_retry_at+job.retry_scheduled. - On terminal failure, job is marked
failed_terminaland user receives failure DM. - Watchdog endpoint recovers stale running jobs and requeues due pending jobs.
Bounded Intelligent Retry
The policy hook is in src/lib/ai/retry-policy.ts.
- Classifies error into bounded classes.
- Applies strict max-attempt and max wall-clock constraints.
- Computes bounded exponential backoff with jitter.
- Optional
llmmode currently uses a bounded stub that can be replaced later.
User Visibility
- Gateway enqueue reply includes correlation id.
- Timeline endpoint returns full state and ordered events.
- Every execution stage appends explicit events (
queued,running,executing,delivering,retry_scheduled,completed,failed_terminal).
Rollout Sequence
- Run migration
0048_ai_orchestration_jobs.sql. - Deploy Pages changes.
- Deploy orchestrator worker and queue bindings.
- Set
AI_AUTOPILOT_INTERNAL_KEYin both services. - Enable
DM_AUTOPILOT_ENABLED=true.
Current Scope Notes
- This is a production-focused foundation, not the final Workflow graph.
- Existing local-runner DM/screenshot path is preserved and still executes first.
- Workflow-native branching/checkpointing can now be layered onto this durable contract.