Skip to content

feat: scheduled leaf dispatch (Archetype C cron trigger on-ramp) - #13

Merged
pdettori merged 12 commits into
mainfrom
feat/scheduled-leaf-dispatch
Jun 28, 2026
Merged

pdettori merged 12 commits into
mainfrom
feat/scheduled-leaf-dispatch

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

Adds the Archetype C trigger on-ramp: a Kubernetes CronJob becomes the start signal that dispatches a static, config-defined list of leaf sessions onto the unchanged async path (POST /run-leaf {async:true}). No external orchestrator process needs to be running — the schedule fires a thin cron-dispatch pod that enqueues the work and exits; the existing leaf-queue → KEDA ScaledJob → runLeaf → done-marker pipeline does the rest.

Design: docs/specs/2026-06-28-scheduled-leaf-dispatch-design.md · Plan: docs/superpowers/plans/2026-06-28-scheduled-leaf-dispatch.md

What's added

  • cron-dispatch.ts — pure logic (applyFire/dispatchAll/loadConfig/exitCodeFor, fully unit-tested with an injected post) + a guarded entrypoint that POSTs each templated envelope {async:true} to the in-cluster Knative service.
  • deploy/knative/leaf-cron.yaml — one manifest, two docs: the leaf-cron-config ConfigMap (item list with the __FIRE__ template token) + the leaf-cron CronJob (concurrencyPolicy: Forbid, fire id via the job-name downward-API label, reuses the serverless-harness SA + harness image — no new RBAC).
  • deploy/knative/leaf-cron-smoke.sh — gated (CRON_LIVE_SMOKE=1) live gate.

Idempotency

The fire id = the dispatcher's Kubernetes Job name (stable across a Job's pod retries, unique per fire). It's substituted into sessionId and resultRef, so each scheduled fire is a fresh run (fire-stamped result dir) while a retried dispatcher re-POSTs identical sessionIds → idempotent resume/overwrite (no duplicate runs).

Notable fix (approved scope change)

The live gate revealed the fire-stamped result dir /work/nightly/<fire>/results/ was never created — the async path only worked because its orchestrator pre-created the dir, and cron has no orchestrator (and the fire id is dynamic). Fixed by adding mkdirSync(dirname(...), {recursive:true}) to the result write (run-leaf.ts) and the marker write (done-marker.ts) — a no-op when the dir exists, so the sync/async paths are unaffected. This also hardens orchestrator-less async use. (This touched runLeaf, overriding the plan's "unchanged" constraint — an explicit, approved decision.)

Testing

  • Unit (vitest, green): knative-server 26 (incl. cron-dispatch 11), harness 82 (incl. a new done-marker missing-parent test).
  • Live gate CRON_LIVE_SMOKE=1 deploy/knative/leaf-cron-smoke.sh on Kind — all 3 claims PASS: CronJob present; a kubectl create job --from=cronjob/leaf-cron fire dispatches the list → all leaves complete with correct verdicts under the fire-stamped dir; idempotent retry (re-firing the same id re-runs the dispatcher to completion, reuses the same dir, markers still done).

Follow-up (non-blocking)

  • Align server.ts's isMainModule guard to the exact-match idiom used in cron-dispatch.ts (cross-package consistency; the new code is already correct).

Assisted-By: Claude Code

Paolo Dettori added 11 commits June 28, 2026 08:44
…l (single kubectl apply)

Signed-off-by: Paolo Dettori <[email protected]>
…chestrator to pre-create them)

Signed-off-by: Paolo Dettori <[email protected]>

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review (automated): clean, well-structured PR — additive feature with good test coverage (unit + live gate). One suggestion below on pod security posture; no blocking issues.

Areas reviewed: TypeScript, Shell, YAML, Docs
Commits: 11, all DCO signed-off
CI: passing

restartPolicy: Never
serviceAccountName: serverless-harness
containers:
- name: cron-dispatch

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Missing securityContext on the container/pod spec. Consider adding:

securityContext:
  runAsNonRoot: true
  allowPrivilegeEscalation: false
  readOnlyRootFilesystem: true

This dispatcher only does HTTP POSTs — it doesn't need write access or elevated privileges.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in e961a5f (live-gate re-verified, 3/3 PASS). Added a pod + container securityContext:

  • runAsNonRoot: true + runAsUser: 65532 — the harness image (node:22-alpine) has no USER so it runs as root; runAsNonRoot alone would refuse to start, hence the explicit nonroot uid.
  • allowPrivilegeEscalation: false, capabilities.drop: [ALL], seccompProfile: RuntimeDefault.
  • readOnlyRootFilesystem: true + a writable /tmp emptyDir — node/tsx need a writable temp dir, so the read-only rootfs needs that one carve-out.

The dispatcher only reads the mounted config and makes HTTP POSTs, so it runs fine fully locked down. (Left the leaf-worker/service pods as-is for now — same hardening there is a sensible follow-up.)

…v-esc, read-only rootfs, drop caps)

Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 41ea458 into main Jun 28, 2026
1 check passed
@pdettori
pdettori deleted the feat/scheduled-leaf-dispatch branch June 28, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant