loops: backend routes + repo + cron scheduler + HMAC webhook
Third commit of the Research + Loops arc. Lights up loops as durable
recurring topology executions:
GET /api/loops list workspace's loops
POST /api/loops create — returns webhook_token +
signing_key ONCE when webhook trigger
is enabled; never exposed again
GET /api/loops/:id detail
PATCH /api/loops/:id update definition
DELETE /api/loops/:id delete
POST /api/loops/:id/run trigger one iteration NOW
POST /api/loops/:id/enable set enabled=true
POST /api/loops/:id/disable set enabled=false
POST /webhooks/loops/:token public; HMAC-SHA256-verified
Scheduler (cm_runtime::spawn_loop_scheduler) wakes every 10s, queries the
partial index on (next_fire_at) for due loops, enqueues one topology_runs
row per fire with loop_id + iteration + parent_run_id chained back to the
previous iteration. Uses croner via the existing scheduling::next_occurrence
helper. Missed windows fire ONCE and skip the backlog — next_fire_at is
always computed strictly AFTER now(), so a late scheduler doesn't drain a
buildup.
Webhook signatures follow the same pattern as the Stripe billing webhook
(HMAC-SHA256 with constant-time hex compare). Token + signing key are
24-byte OS-RNG values; the URL uses base64-url for the token, and the
signing key is base64-std. Both surface exactly once at create time.
All three fire paths (scheduler, immediate-run, webhook) funnel through
`cm_db::repo::loops::enqueue_iteration` so the invariants stay in one
place. `iters` repeat policy is enforced by the scheduler tick; `until`
and `on_completion` land with the orchestrator hook in commit 4.
Adds cm-llm as a direct cm-api dep, getrandom for the webhook material
generator, and wires the scheduler spawn into the server binary alongside
the resume sweeper and outbox drainer.
This commit is contained in:
@@ -274,6 +274,9 @@ async fn run() -> Result<(), String> {
|
||||
// Outbound-email delivery: drains the §15-gated `outbox` over SMTP. Inert
|
||||
// until CLAWMATES_SMTP_* is set, so it ships safely before credentials exist.
|
||||
cm_runtime::spawn_drainer(pool.clone(), std::time::Duration::from_secs(10));
|
||||
// Loop scheduler: fires cron-triggered loop iterations. Missed windows
|
||||
// fire ONCE and skip the backlog (see cm_runtime::loops for details).
|
||||
cm_runtime::spawn_loop_scheduler(pool.clone(), std::time::Duration::from_secs(10));
|
||||
// Expiry/retention sweep: expires stale auth/oauth rows and prunes old
|
||||
// journal/audit rows hourly so unbounded tables don't accumulate.
|
||||
cm_api::cleanup_sweeper::spawn(pool.clone(), std::time::Duration::from_secs(3600));
|
||||
|
||||
Reference in New Issue
Block a user