cms-ai-forge¶
The forge is a standalone task-processor that generates every entity a
CMS-backed website needs -- site settings, logo/favicon, taxonomy, style,
template, page types, articles, images, and SEO metadata -- and publishes them
into the CMS. It is a driver of the cms-api admin block, never a direct CMS
database writer: the only write path is cms-api's /admin/v1 surface over HTTP,
authenticated as a forge service account. The forge owns its own MySQL database
for its job/task state.
Epic AISD-148 (see Architecture for the full design and delivery phasing). This page is the operational overview; the detailed pages are:
- Job intents -- every intent the control plane accepts.
- Generators & providers -- the generation core.
- Configuration -- real vs stub providers,
account_id, env vars. - Data-fidelity fixes -- the AISD-170 correctness guarantees.
- Deployment -- how the forge ships and how to check it.
- Worker memory -- what bounds RSS during a large import, how to profile it, and why the host watchdog was removed.
- Postman / API collection -- an importable request-per-intent collection.
Components¶
| Layer | Module | Role |
|---|---|---|
| Control plane | control_plane/ |
FastAPI: POST /jobs, GET /jobs/{id}, SSE events, task retry |
| Planner | workers/planner.py |
Expand a job's intent + spec into child tasks |
| Workers | workers/ |
Celery on RabbitMQ; one queue per entity type; each worker advances one state-machine step |
| Generation core | generation/generators/, generation/providers/, generation/sources/ |
Per-entity generators, text/image providers, article-source strategies |
| Publishing | publishing/cms_publisher.py, publishing/media_publisher.py |
The only outbound writes -- cms-api admin API + cms-media S3 |
| Datastore | datastore/ |
Forge-owned MySQL (jobs, tasks, transitions, events, ledger, assets) |
| State machine | state_machine/ |
Guarded transitions, append-only transition log, retry beat |
Request flow¶
A job is submitted, planned into tasks, and each task walks a persisted state machine until published. The state lives in MySQL (not worker memory), so a crashed/redeployed worker resumes exactly where it stopped, and re-runs are idempotent.
CmsPublisher writes to cms-api /admin/v1; MediaPublisher writes to cms-media
S3. The registry lookup is what makes planning data-driven: the planner resolves
the job's intent to an ordered step list and stamps each task's _step_slug,
and the executor reads that step's schema/prompt/provider back from the registry.
The full schema and the resolve contract are documented in the
cms-db action registry page.
Control-plane API¶
| Method | Path | Purpose |
|---|---|---|
POST |
/jobs |
Submit a GenerationRequest; returns {job_id, state} (201). Honors idempotency_key -- a resubmit with the same key returns the existing job. |
POST |
/migrate_from_wp |
Whole-site WordPress import by website name -- builds a wp_site_import job (AISD-250). See wp_site_import. |
GET |
/jobs/{job_id} |
Job + its task tree (state, attempts, result_ref, error per task). |
GET |
/jobs |
List jobs newest-first, keyset-paginated, with per-state task counts. Filters: site_id, site_ref, intent, state (repeatable), submitted_by, created_after, created_before, limit, cursor. |
GET |
/jobs/{job_id}/tasks/{task_id} |
One task plus its transition log, its events, and the registry step that produced it. |
POST |
/jobs/{job_id}/tasks/{task_id}/retry |
Operator re-drive: reset a dead/failed task to pending and re-enqueue it. |
POST |
/jobs/{job_id}/retry-failed |
Re-drive every failed/dead task in the job; reports requeued vs skipped. |
POST |
/jobs/{job_id}/cancel |
Stop a non-terminal job and every continuation descending from it. Idempotent. |
POST |
/jobs/{job_id}/tasks/{task_id}/regenerate |
Re-generate a terminal task's entity as a new single-entity job; returns the new job_id. |
GET |
/jobs/{job_id}/events |
Server-Sent-Events stream of ai_generation_events progress rows until the job is terminal. |
GET |
/api/status |
Health: reports DB + broker reachability, always 200. The only unauthenticated route. |
Auth: a service credential on every route (AISD-317)¶
Every control-plane route requires a bearer token:
A missing or malformed header is 401 missing_credential; an unknown token is
401 invalid_credential. If the service has no credential configured at all it
answers 503 control_plane_credential_not_configured rather than serving the
route open -- a deploy that lost its secret must break loudly, not quietly go back
to accepting anonymous traffic.
GET /api/status is deliberately outside the gate: CodeDeploy's ValidateService
hook, the Compose healthcheck and hq.deploy check cms-ai-forge all poll it, and a
health check that needs a secret is a health check that rots. It reports only db
and broker reachability, and must not grow while it is public.
/openapi.json, /docs and /redoc are gated too on deployed environments -- the
schema is a request-shaped map of a service that holds a cms-api service account and
provider API keys. They stay open under ENVIRONMENT=local / test.
The gate is applied at mount level in forge.main.create_app(), so a router
added later is protected by construction rather than by remembering;
tests/test_control_plane_auth.py asserts every non-health route carries it.
Callers are services, not people. The credential authorises the caller process
(a dashboard's server-side hop, Postman, an operator's curl); per-user attribution
belongs on the surface that has users, and the job row already records
submitted_by. Named consumer keys can be added alongside the primary one, so one
consumer can be rotated or revoked without disturbing the others.
Provisioning: Deployment -> the control-plane credential.
Job & task states¶
Job: received → planning → planned → running → {done | partial | failed}, plus
cancelled from any non-terminal state (POST /jobs/{id}/cancel, AISD-318).
cancelled is deliberately not folded into partial: "finished with failures" and
"an operator stopped it" are different facts, and a reader who cannot tell them apart
chases a phantom failure. It has no outgoing edges -- what happens after a cancel
is a fresh submission, not a resurrection.
partial and failed do have one outgoing edge, back to running, taken only by an
operator bulk re-drive (POST /jobs/{id}/retry-failed). Without it a re-driven job
would sit at partial while its tasks worked, and maybe_finalize_job -- which only
acts on a running job -- would never re-finalize it.
Task: pending → context_ready → generated → validated → assets_ready →
published → done, with failed → retrying (bounded, exponential backoff) and
dead at attempts ≥ max_attempts. assets_ready applies only to entities that
spawn media (articles, logos); text-only entities go validated → published.
Every transition is a row-locked guarded UPDATE
(... WHERE id = :id AND state = :expected_from): a duplicate/redelivered
message loses the race and no-ops. External effects are idempotent too --
deterministic S3 keys, and content upsert is guarded by (site_id, slug) plus a
ai_content_ledger dedup -- so a re-run makes no duplicate content.
Idempotency & resume¶
- Whole-job: an
idempotency_keyon the request dedupes resubmits. - Content: the
ai_content_ledger(dedup by(site_ref, title_hash)/ slug) plus the slug-matched, cursor-pagedGET /admin/v1/contentlookup mean a re-run never double-creates. - Mid-article crash: guarded transitions + a children-pending gate let a worker killed mid-article resume to completion.