skip to content
jaeaeichsysdes

Design OpsPilot (Human-in-the-Loop Runner)

Durable AI incident & migration runner for 10,000+ concurrent parked workflows: Temporal Signal + 24h stale-approval guard, RBAC optimistic-locked approvals, idempotent K8s/Cloud steps, and Saga rollback.

The whiteboard

Trace the 4 flows below to see <500ms webhook ingestion, AI plan generation, zero-RAM human approval parking, and Saga rollback. Builds on Idempotency & Outbox and Concurrency & Failure Modes.

whiteboard · press 1–5 to trace flows

Ingress, RBAC Approval & Status ReadsDurable Workflow Engine (10K+ Parked Workflows)Human-in-the-Loop Approval & Saga Executionstart / signalplanaudit + statusask approvalrun approvedapplyon failAlerts / SlackPD · Datadogapprove clickGatewayauth · RBACrate limitIncident APIPOST /incidentsPOST /approveIncident PGincidents · plansaudit_logs (append)Redis + Statuswrite-throughGET /statusTemporal Serverstart · signal24h stale timerActivity Historyevent replayzero lost stateTemporal Workerevicts RAM parkedidempotent actsAI Agent / LLMpropose plan +inverse rollbackFCM → Slack / Mailplan diff + button<3s ack on clickPlan Executionstep-by-stepkey=wf_id:stepK8s / Cloud APImutating opsidempotentSaga Rollbackon step fail:reverse compens.

Full OpsPilot: fast webhook ingress → Temporal AI planning → zero-RAM 24h Slack approval → idempotent K8s execution + Saga rollback10K+ parked workflows · consistency >> availability

Scale & APIs

10,000+

concurrent parked workflows (hours–days)

< 500 ms

webhook ingest ack · <3s Slack click ack

Saga

reverse compensation on mid-plan failure

POST /incidents -> 202 { incident_id } (starts Temporal workflow)
GET /incidents/{id}/status -> plan, step-by-step statuses, logs (Redis)
POST /incidents/{id}/plans/{plan_id}/approve -> 200 OK (RBAC + audit_logs + Temporal Signal)
Postgres (incident DB):
incidents (id, tenant_id, fired_by, status, created_at)
plans (id, incident_id, steps_json, status, expires_at)
audit_logs (id, tenant_id, incident_id, actor_id, action, command_payload, outcome, ts)

How it works

1. Fast ingress, RBAC approval & status reads

  • Webhook ingestion (< 500ms): POST /incidents persists the incident in incident DB (Postgres) and calls temporal.start_workflow(incident_id) — returning 202 immediately before the AI agent starts investigating.
  • Double-click-proof approval (< 100ms Slack ack): POST /incidents/{id}/plans/{plan_id}/approve verifies RBAC, transitions the plan with a Postgres unique constraint / optimistic lock (WHERE status = 'pending' AND expires_at > now()), appends an immutable row to audit_logs, and fires temporal.signal(workflow_id, "approval").
  • Fast status UI: GET /incidents/{id}/status reads from Redis (write-through from incident DB).

2. Durable orchestration (Temporal + Activity History)

  • Why Temporal: Replaces a fragile hand-rolled Postgres + CDC + Kafka + Scheduler + DLQ state machine. Temporal persists every Activity result in Activity History so worker restarts or deploys replay and resume without re-running completed mutations.
  • Step 1 — make_plan (Activity): Calls the black-box AI agent / LLM to investigate and output an ordered plan separating read_only diagnostics from mutating steps, plus each mutating step’s inverse compensation command.
  • Step 2 — write_status (Activity): Saves the proposed plan to incident DB (write-through Redis) and appends a proposed record to audit_logs.

3. Human-in-the-loop parking & stale-approval guard

  • Zero-RAM parking: Fires a notification Activity (FCM → Slack / Email with plan diff + approve button), then parks on wait_for_signal("approval", timeout="24h"). The workflow evicts itself from worker RAM so 10,000+ parked workflows cost near-zero compute.
  • Stale-plan auto-cancel: If the 24h Timer fires before approval, the workflow marks the plan expired in Postgres and exits cleanly — preventing an engineer from accidentally clicking a 2-day-old Slack button after production state has drifted.

4. Idempotent execution, Saga rollback & immutable audit

  • Per-step idempotency: Every mutating Activity passes a deterministic idempotency key (workflow_id:step_idx) to the target K8s / Cloud API so network retries never apply a destructive change twice.
  • Saga compensating rollback: If step 3 of 5 fails after retries, the workflow iterates backward through completed mutating steps (step 2 → step 1), executing their inverse compensation commands, updating status, and appending rolled_back entries to audit_logs.
  • Append-only compliance: audit_logs is strictly append-only (never UPDATE or DELETE), recording proposed, approved, executed, and rolled_back with actor and exact command payload.

lesson

Consistency beats availability here: Even though Slack clicks must feel fast (< 3s), production mutations must never double-fire. Always enforce an atomic Postgres state transition (pending → approved) before sending the Temporal Signal, and guard every downstream cloud call with workflow_id:step_idx.

Check yourself

interactive · stress-test OpsPilot against concurrency, crash & rollback probes

01 · How do 10,000 workflows wait 24 hours without exhausting worker RAM?

10K+ parked workflows · hours/days wait · worker restarts

An on-call migration triggers 10,000 tenant remediation plans, all waiting for human approval in Slack. Why doesn't the Temporal Worker fleet run out of memory or threads?

02 · Two engineers click Approve in Slack at the same millisecond

consistency >> availability · concurrent clicks · audit trail

Both Alice and Bob click Approve on the same incident plan at t=0. How do you guarantee the mutating remediation plan executes strictly once and attributes the approval cleanly?

03 · Step 3 of a 5-step production migration fails mid-way

external K8s/Cloud APIs · partial failure · durable rollback

Steps 1 and 2 already mutated Kubernetes and AWS resources; step 3 fails with a non-retryable error. Why use a Saga rollback instead of a single distributed 2PC database transaction?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme