Mongo Collection Architecture for Poller-Based Certificate Workflow
1. Purpose
This document defines the MongoDB data model for LitmusChaos certificate generation using a server-side poller orchestration pattern.
Design goals:
- Support one experiment with multiple runs.
- Keep UI decoupled from certifier APIs.
- Keep MongoDB as the single source of truth.
- Ensure deterministic transitions and idempotent orchestration.
2. Core Modeling Decision
Do not model certificate workflow as one document per experiment only.
Use a two-tier model:
- Run-level workflow records for bucketing lifecycle per run.
- Experiment-level aggregation/certificate records across runs.
This prevents overwrites when runs execute in parallel.
3. Collections
3.1 certificate_experiments (parent summary)
One document per project+experiment.
{
"_id": "ObjectId",
"projectId": "proj-1",
"agentId": "flash-agent",
"agentName": "Flash Agent v1.0",
"experimentId": "exp-123",
"status": "RUNS_IN_PROGRESS",
"expectedRuns": 30,
"runCounts": {
"total": 12,
"running": 2,
"completed": 9,
"failed": 1
},
"aggregationPolicy": {
"mode": "ALL_RUNS_COMPLETED"
},
"activeAggregationVersion": 2,
"latestCertificate": {
"status": "EXPERIMENT_CERTIFICATE_READY",
"certificationId": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"pdfStatus": "NOT_FETCHED",
"generatedAt": "2026-04-27T11:00:00Z"
},
"createdAt": "2026-04-27T09:00:00Z",
"updatedAt": "2026-04-27T11:00:00Z"
}
Indexes:
- Unique:
{ projectId: 1, experimentId: 1 } - Non-unique:
{ projectId: 1, agentId: 1, status: 1 } - Non-unique:
{ status: 1, updatedAt: -1 } - Non-unique:
{ "latestCertificate.status": 1, updatedAt: -1 }
3.2 certificate_run_workflows (run-level state)
One document per experiment run.
{
"_id": "ObjectId",
"projectId": "proj-1",
"agentId": "flash-agent",
"experimentId": "exp-123",
"experimentRunId": "run-0268",
"runSequence": 7,
"status": "BUCKETING_RUNNING",
"bucketing": {
"taskId": "1546dfab-0e7b-4879-9c30-61654f32a2b6",
"pollUrl": "/api/v1/tasks?experiment_id=exp-123&experiment_run_id=run-0268",
"lastPolledAt": "2026-04-27T10:10:00Z",
"nextPollAt": "2026-04-27T10:10:15Z",
"attempt": 4,
"startedAt": "2026-04-27T10:00:05Z",
"completedAt": null
},
"result": {
"metricsReady": false,
"metricsLocation": null
},
"error": {
"code": null,
"reason": null,
"retryable": null,
"lastFailedAt": null
},
"createdAt": "2026-04-27T10:00:00Z",
"updatedAt": "2026-04-27T10:10:00Z",
"version": 9
}
Indexes:
- Unique:
{ projectId: 1, agentId: 1, experimentId: 1, experimentRunId: 1 } - Unique sparse:
{ "bucketing.taskId": 1 } - Non-unique:
{ status: 1, "bucketing.nextPollAt": 1 } - Non-unique:
{ agentId: 1, experimentId: 1, status: 1 } - Non-unique:
{ updatedAt: -1 }
3.3 certificate_aggregation_workflows (experiment-level aggregation/cert)
One document per aggregation version.
{
"_id": "ObjectId",
"projectId": "proj-1",
"agentId": "flash-agent",
"agentName": "Flash Agent v1.0",
"experimentId": "exp-123",
"aggregationVersion": 2,
"status": "AGGREGATION_RUNNING",
"inputSnapshot": {
"runIds": ["run-001", "run-002", "run-003"],
"successfulRuns": 3,
"failedRuns": 0
},
"aggregation": {
"certTaskId": "agg-456",
"pollUrl": "/api/v1/cert-tasks/agg-456",
"lastPolledAt": "2026-04-27T10:30:00Z",
"nextPollAt": "2026-04-27T10:30:20Z",
"attempt": 3,
"startedAt": "2026-04-27T10:20:05Z",
"completedAt": null
},
"certificate": {
"certificationId": null,
"checksumSha256": null,
"generatedAt": null,
"expiresAt": null
},
"error": {
"code": null,
"reason": null,
"retryable": null,
"lastFailedAt": null
},
"createdAt": "2026-04-27T10:20:00Z",
"updatedAt": "2026-04-27T10:30:00Z",
"version": 6
}
Indexes:
- Unique:
{ projectId: 1, agentId: 1, experimentId: 1, aggregationVersion: 1 } - Unique sparse:
{ "aggregation.certTaskId": 1 } - Non-unique:
{ status: 1, "aggregation.nextPollAt": 1 } - Non-unique:
{ agentId: 1, experimentId: 1, createdAt: -1 }
4. Status Models
4.1 Run Workflow Status
BUCKETING_INITIATEDBUCKETING_TRIGGEREDBUCKETING_RUNNINGBUCKETING_FAILEDBUCKETING_COMPLETED
4.2 Aggregation Workflow Status
AGGREGATION_TRIGGEREDAGGREGATION_RUNNINGAGGREGATION_COMPLETEDAGGREGATION_FAILED
4.3 Experiment Summary Status
RUNS_IN_PROGRESSREADY_FOR_AGGREGATIONAGGREGATION_IN_PROGRESSEXPERIMENT_CERTIFICATE_READY
5. Transition Rules
Run transitions:
BUCKETING_INITIATED -> BUCKETING_TRIGGEREDBUCKETING_TRIGGERED -> BUCKETING_RUNNINGBUCKETING_RUNNING -> BUCKETING_COMPLETEDBUCKETING_RUNNING -> BUCKETING_FAILED
Aggregation transitions:
AGGREGATION_TRIGGERED -> AGGREGATION_RUNNINGAGGREGATION_RUNNING -> AGGREGATION_COMPLETEDAGGREGATION_RUNNING -> AGGREGATION_FAILED
Reject any invalid transition.
6. Trigger and Polling Logic
6.1 On Experiment Run Completion
- Upsert into
certificate_run_workflowsby(projectId, experimentId, experimentRunId). - If first time, set
status=BUCKETING_TRIGGERED. - Call bucketing API once using
agent_id,experiment_id, andrun_idfrom the workflow doc. - Save
bucketing.taskId,bucketing.pollUrl, setstatus=BUCKETING_RUNNING.
6.2 Bucketing Poller
Poll query target:
certificate_run_workflowswherestatus=BUCKETING_RUNNINGandbucketing.nextPollAt <= now.
Result handling:
RUNNING: updatelastPolledAt, compute next poll.FAILED: setstatus=BUCKETING_FAILED, set error fields.COMPLETED: setstatus=BUCKETING_COMPLETED,result.metricsReady=true.
6.3 Aggregation Trigger Gate
Use certificate_experiments.aggregationPolicy:
ALL_RUNS_COMPLETED
When gate passes:
- Create new
certificate_aggregation_workflowsdoc with nextaggregationVersion. - Call aggregation API using
agent_id,agent_name, andexperiment_id, then setAGGREGATION_TRIGGERED -> AGGREGATION_RUNNINGafter API accept. - Update parent
certificate_experiments.status=AGGREGATION_IN_PROGRESS.
Gate condition for ALL_RUNS_COMPLETED:
- all expected experiment runs are finished
- all corresponding bucketing workflows are in terminal state
- terminal bucketing states are
BUCKETING_COMPLETEDandBUCKETING_FAILED
This means aggregation starts only after the full experiment execution is over and no bucketing workflow is still pending or running.
6.4 Aggregation Poller
Poll query target:
certificate_aggregation_workflowswherestatus=AGGREGATION_RUNNINGandaggregation.nextPollAt <= now.
Result handling:
RUNNING: continue polling.FAILED: setAGGREGATION_FAILED, store error.COMPLETED: setAGGREGATION_COMPLETED, storecertificate.certificationIdand metadata.
Then update parent certificate_experiments.latestCertificate and set parent status to EXPERIMENT_CERTIFICATE_READY.
7. Idempotency and Concurrency
Bucketing API Submission:
- Run init idempotency via unique key
(projectId, agentId, experimentId, experimentRunId)— ensures workflow doc created once. - Before calling API: check if
bucketing.taskIdis already set. If yes, skip submission (already sent). - Call
POST /api/v1/bucketing-extractionwith(agent_id, experiment_id, run_id)from the workflow doc. - On 202 response: store returned
task_id, setstatus=BUCKETING_RUNNING. - On 409 response (TASK_ALREADY_ACTIVE): the API already has this task. Extract
task_idfrom response, store it, continue polling. - On other errors: set
status=BUCKETING_FAILED, store error reason.
Aggregation API Submission: Same pattern — use unique constraint (projectId, agentId, experimentId, aggregationVersion) and call POST /api/v1/aggregation-certification with agent_id, agent_name, and experiment_id.
Concurrency for pollers:
- For single-replica polling, no additional lock collection is required.
8. Retry and Backoff
Recommended defaults:
- Initial poll interval: 10-15 seconds.
- Backoff: 15s, 30s, 60s, 120s max.
- Max poll retries before failure: 20.
- API trigger retries (transient errors): 5.
Store retry metadata in each workflow doc.
9. Data Retention and Security
- Never persist certifier secrets.
- Redact sensitive payload fields before saving snapshots.
- Keep workflow docs per compliance window (example: 180 days).
- Use TTL for events if needed (example: 90 days).
10. UI Read Pattern
UI should query GraphQL only:
- Parent experiment certificate summary from
certificate_experiments. - Per-run progress from
certificate_run_workflows. - Latest
certificationIdfrom parent or latest aggregation doc.
PDF download should be done via a separate external API using certificationId.
UI should not call certifier APIs directly.
11. Why This Schema Works for Multi-Run Experiments
- Each run has isolated state and external task id.
- Experiment-level summary remains stable and query-friendly.
- Aggregation is versioned and repeatable when new runs arrive.
- Polling scales safely with indexes and lock support.
- Mongo remains the authoritative source for UI and backend orchestration.
12. API Flow Implementation
From UI to AgentCert GraphQL server:
- After each experiment run completes, trigger a call to the GraphQL backend at
/certification-generation. - The backend executes the certifier sequence in order:
bucketing-extractionpoll-bucketing-extractionaggregate-certificationpoll-aggregate-certification
- Each step updates the MongoDB collections defined in this document.
Experiment history page behavior:
- When the user opens experiment history, backend checks experiment summary status.
- If status is
EXPERIMENT_CERTIFICATE_READY, return a flagtrueto UI. - If status is not
EXPERIMENT_CERTIFICATE_READY, return a flagfalse. - UI enables the download certificate link only when the flag is
true; otherwise keep it disabled.
12.1 Flow Diagram
flowchart TD
A[Experiment run completed] --> B[UI calls GraphQL /certification-generation]
B --> C[Upsert run workflow in Mongo]
C --> D[Call certifier: bucketing-extraction]
D --> E{Bucketing API response}
E -->|202 or 409| F[Save task id and set BUCKETING_RUNNING]
E -->|Error| G[Set BUCKETING_FAILED and store error]
F --> H[Poll bucketing task]
H --> I{Bucketing poll status}
I -->|RUNNING| H
I -->|COMPLETED| J[Set BUCKETING_COMPLETED]
I -->|FAILED| G
J --> K[Evaluate gate: ALL_RUNS_COMPLETED]
G --> K
K --> L{All runs finished and all bucketing terminal?}
L -->|No| M[Wait for remaining runs]
M --> K
L -->|Yes| N[Create aggregation workflow version N+1]
N --> O[Call certifier: aggregate-certification]
O --> P{Aggregation API response}
P -->|202 or 409| Q[Save cert task id and set AGGREGATION_RUNNING]
P -->|Error| R[Set AGGREGATION_FAILED and store error]
Q --> S[Poll aggregation task]
S --> T{Aggregation poll status}
T -->|RUNNING| S
T -->|FAILED| R
T -->|COMPLETED| U[Set AGGREGATION_COMPLETED]
U --> V[Update parent experiment status to EXPERIMENT_CERTIFICATE_READY]
V --> W[Save latestCertificate.certificationId]
X[User opens experiment history page] --> Y[GraphQL reads experiment summary from Mongo]
Y --> Z{status == EXPERIMENT_CERTIFICATE_READY?}
Z -->|Yes| AA[Return ready=true and enable download link]
Z -->|No| AB[Return ready=false and keep download disabled]
13. GraphQL to Certifier API Data Mapping
This section defines exactly how AgentCert GraphQL reads data from MongoDB, calls certifier APIs, and persists responses back to MongoDB.
13.1 Trigger: GraphQL /certification-generation
Inputs to GraphQL resolver (from UI/event):
projectIdagentIdexperimentIdexperimentRunId
GraphQL initial Mongo reads:
certificate_experimentsby(projectId, experimentId)certificate_run_workflowsby(projectId, agentId, experimentId, experimentRunId)
If run doc does not exist, GraphQL upserts certificate_run_workflows with:
status=BUCKETING_INITIATEDthenBUCKETING_TRIGGEREDagentId,experimentId,experimentRunIdcreatedAt,updatedAt
13.2 POST /api/v1/bucketing-extraction
Mongo source fields to build request payload:
| Request Field | Value Source |
|---|---|
agent_id | certificate_run_workflows.agentId |
experiment_id | certificate_run_workflows.experimentId |
run_id | certificate_run_workflows.experimentRunId |
trace_source | GraphQL config source (project/server settings or runtime input) |
llm_batch_size | GraphQL config/default |
storage_config | GraphQL config/default |
Response handling and Mongo updates:
202 Accepted: savebucketing.taskId,bucketing.pollUrl, setstatus=BUCKETING_RUNNING, updatebucketing.startedAt.409 TASK_ALREADY_ACTIVE: extract existingtask_id, save it tobucketing.taskId, keep/setstatus=BUCKETING_RUNNING.422/500/other error: setstatus=BUCKETING_FAILED, store error payload inerrorfields.
13.3 GET /api/v1/tasks
GraphQL poll request mapping:
| Query Param | Value Source |
|---|---|
experiment_id | certificate_run_workflows.experimentId |
experiment_run_id | certificate_run_workflows.experimentRunId |
Poll selection in Mongo:
certificate_run_workflowswherestatus=BUCKETING_RUNNINGandbucketing.nextPollAt <= now
Poll response to Mongo mapping:
RUNNING: updatebucketing.lastPolledAt, incrementbucketing.attempt, compute and setbucketing.nextPollAt.COMPLETED: setstatus=BUCKETING_COMPLETED, setbucketing.completedAt, setresult.metricsReady=true, optionally store metrics location metadata.FAILED: setstatus=BUCKETING_FAILED, seterror.code/reason/retryable/lastFailedAt.
13.4 Aggregation Gate Evaluation
GraphQL reads Mongo before triggering aggregation:
- Parent:
certificate_experimentsby(projectId, experimentId) - Runs: all
certificate_run_workflowsfor(agentId, experimentId)
Gate condition (ALL_RUNS_COMPLETED):
- all expected runs are finished
- all run workflows are terminal (
BUCKETING_COMPLETEDorBUCKETING_FAILED)
If passed:
- create next
certificate_aggregation_workflowswithaggregationVersion = activeAggregationVersion + 1 - set aggregation status to
AGGREGATION_TRIGGERED - set parent
certificate_experiments.status=AGGREGATION_IN_PROGRESS
13.5 POST /api/v1/aggregation-certification
Mongo source fields to build request payload:
| Request Field | Value Source |
|---|---|
agent_id | certificate_aggregation_workflows.agentId |
agent_name | certificate_aggregation_workflows.agentName |
experiment_id | certificate_aggregation_workflows.experimentId |
certification_run_id | derived by GraphQL (for example v<aggregationVersion> or release tag) |
runs_per_fault | from certificate_experiments.expectedRuns or GraphQL config |
storage_config | GraphQL config/default (type=local) |
Response handling and Mongo updates:
202 Accepted: saveaggregation.certTaskId,aggregation.pollUrl, setstatus=AGGREGATION_RUNNING, setaggregation.startedAt.409 TASK_ALREADY_ACTIVE: extract existingcert_task_id, save it, keep/setstatus=AGGREGATION_RUNNING.400/422/500/other error: setstatus=AGGREGATION_FAILED, store error payload.
13.6 GET /api/v1/cert-tasks
GraphQL poll request mapping:
| Query Param | Value Source |
|---|---|
experiment_id | certificate_aggregation_workflows.experimentId |
Poll selection in Mongo:
certificate_aggregation_workflowswherestatus=AGGREGATION_RUNNINGandaggregation.nextPollAt <= now
Poll response to Mongo mapping:
RUNNING: updateaggregation.lastPolledAt, incrementaggregation.attempt, compute and setaggregation.nextPollAt.COMPLETED: setstatus=AGGREGATION_COMPLETED, setaggregation.completedAt, storecertificate.certificationIdfrom responseresult.certification_id, update checksums/metadata if available.FAILED: setstatus=AGGREGATION_FAILED, store normalized error fields.
Parent finalization after aggregation completion:
- update
certificate_experiments.activeAggregationVersion - set
certificate_experiments.status=EXPERIMENT_CERTIFICATE_READY - update
certificate_experiments.latestCertificatewith:status=EXPERIMENT_CERTIFICATE_READYcertificationIdgeneratedAtpdfStatus=NOT_FETCHED
13.7 Experiment History Read Path
GraphQL query for experiment history reads certificate_experiments and returns:
ready=trueifstatus=EXPERIMENT_CERTIFICATE_READYready=falseotherwisecertificationIdwhen available
UI behavior:
- if
ready=true, enable certificate download link - if
ready=false, keep download link disabled
14. Certificate PDF Download
After aggregation completes successfully, the certificationId stored on certificate_experiments.latestCertificate.certificationId is the handle the certifier uses to serve the rendered PDF. The PDF itself is not stored in MongoDB — it is fetched on demand from the certifier service.
14.1 PDF Status Lifecycle
The latestCertificate.pdfStatus field tracks the local view of the PDF artifact. Allowed values:
| Status | Meaning |
|---|---|
NOT_FETCHED | Certificate is ready but no one has requested the PDF yet (default after aggregation completes). |
FETCHING | A download request is in flight to the certifier. |
FETCHED | PDF has been streamed to the user at least once. |
FETCH_FAILED | Last download attempt returned a non-2xx response. |
Transitions are written by the GraphQL PDF endpoint (section 14.3):
NOT_FETCHED -> FETCHING -> FETCHEDNOT_FETCHED -> FETCHING -> FETCH_FAILEDFETCH_FAILED -> FETCHING -> ...(retry)
14.2 Certifier PDF Endpoint
GET {CERTIFIER_BASE_URL}/api/v1/certifications/{certification_id}/pdf
Accept: application/pdf
Response (200): Content-Type: application/pdf, body is the binary PDF.
Response (404): certification id not found or not yet ready. Response (5xx): transient certifier error — eligible for retry.
14.3 GraphQL/REST Surface for the UI
The UI never calls the certifier directly. The graphql-server exposes a binary REST route that proxies the PDF and updates the status fields:
GET /api/certification/{projectId}/{experimentId}/certificate.pdf
Authorization: <session JWT>
Server behavior:
- Read
certificate_experimentsby(projectId, experimentId). - If
status != EXPERIMENT_CERTIFICATE_READYorlatestCertificate.certificationIdis empty → return409 Conflictwith body{"error":"certificate not ready"}. - Atomically
$set latestCertificate.pdfStatus="FETCHING"andlatestCertificate.lastFetchAt = now(). GETthe certifier PDF endpoint above with the storedcertificationId.- On 2xx:
- stream
Content-Type: application/pdfdirectly to the HTTP client, - set
pdfStatus="FETCHED"and incrementlatestCertificate.fetchCount.
- stream
- On 4xx/5xx:
- set
pdfStatus="FETCH_FAILED"and writelatestCertificate.lastFetchError, - return the upstream status code with a normalized JSON error body.
- set
14.4 Mongo Field Additions on latestCertificate
"latestCertificate": {
"status": "EXPERIMENT_CERTIFICATE_READY",
"certificationId": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"pdfStatus": "FETCHED",
"generatedAt": "2026-04-27T11:00:00Z",
"lastFetchAt": "2026-04-27T11:42:13Z",
"lastFetchError": null,
"fetchCount": 3
}
lastFetchAt, lastFetchError, and fetchCount are written only by the PDF route described in 14.3.
14.5 UI Behavior for PDF Download
- UI calls
getCertificationStatus; ifready=truethe download button is enabled. - Clicking the button issues a regular browser GET to
/api/certification/{projectId}/{experimentId}/certificate.pdf. - The browser receives the binary stream and saves/opens it.
- On error response, UI shows the normalized error message and keeps the button enabled for retry.
14.6 Idempotency, Caching, and Security
- The PDF endpoint is idempotent — the certifier produces the same PDF for a given
certificationId. Repeated downloads are safe. - The graphql-server does not cache the PDF binary on disk; each request pulls fresh bytes from the certifier. This avoids stale artifacts and keeps the server stateless.
- Authorization is enforced by the existing
Authorizedmiddleware (same JWT used elsewhere); only members ofprojectIdmay download. - Audit: every PDF fetch increments
fetchCountand updateslastFetchAt, providing a rudimentary access trail without storing PII.
14.7 Flow Diagram
flowchart LR
U[User clicks Download] --> R[GET /api/certification/.../certificate.pdf]
R --> M{status == EXPERIMENT_CERTIFICATE_READY?}
M -->|No| E1[409 certificate not ready]
M -->|Yes| S1[Set pdfStatus=FETCHING]
S1 --> C[GET certifier /api/v1/certifications/{id}/pdf]
C --> D{Upstream status}
D -->|2xx| OK[Stream PDF to user, set pdfStatus=FETCHED, fetchCount++]
D -->|4xx/5xx| F[Set pdfStatus=FETCH_FAILED, store lastFetchError]
F --> ER[Return normalized error JSON]
Certifier API Endpoint Configuration Locations
To connect AgentCert to the Certifier API and PDF service (local, Azure, or any remote deployment), set these two environment variables everywhere the orchestrator or PDF proxy runs:
CERTIFIER_BASE_URL— main Certifier API endpoint (e.g. https://your-azure-certifier-host)CERTIFICATE_PDF_BASE_URL— PDF download endpoint (e.g. https://your-azure-certifier-host or https://your-azure-pdf-host)
Where to set them
1. Local/Scripted Startup (recommended for dev and most deployments)
- Edit your
.envfile at the repo root:CERTIFIER_BASE_URL=https://your-azure-certifier-hostCERTIFICATE_PDF_BASE_URL=https://your-azure-pdf-host
- Pass this file to the startup script:
bash scripts/azure_build/start-agentcert-v2.sh --deploy --env-file $(pwd)/.env --paths-file $(pwd)/build-paths.env
Note: The only supported host-process startup script is
start-agentcert-v2.sh --deploy(without--deployit only builds images locally; the Kubernetes stack is installed with./scripts/setup.sh --deploy). If you see references tostart-agentcert.shin older docs or comments, usestart-agentcert-v2.shinstead. Both required environment variables must be set in the.envfile you provide to this script.
2. Kubernetes Deployment (using provided manifests)
- Edit all three manifest files:
AgentCert/chaoscenter/manifests/litmus-getting-started.yamlAgentCert/chaoscenter/manifests/litmus-installation.yamlAgentCert/chaoscenter/manifests/litmus-without-resources.yaml
- In each, update both keys in the
ConfigMapdata block:CERTIFIER_BASE_URL: "https://your-azure-certifier-host" CERTIFICATE_PDF_BASE_URL: "https://your-azure-pdf-host"
3. Local GraphQL Server Direct Run (rare, for advanced dev only)
- Edit
AgentCert/chaoscenter/graphql/server/.env:CERTIFIER_BASE_URL=https://your-azure-certifier-hostCERTIFICATE_PDF_BASE_URL=https://your-azure-pdf-host
Do NOT edit code defaults in variables.go — always override via env/config.
Reference: Where localhost:8088/8089 appear in the repo
- scripts/azure_build/start-agentcert-v2.sh: CERTIFIER_BASE_URL and CERTIFICATE_PDF_BASE_URL defaults
- .env.example: CERTIFIER_BASE_URL and CERTIFICATE_PDF_BASE_URL example values
- AgentCert/chaoscenter/graphql/server/utils/variables.go: Go config defaults
- AgentCert/chaoscenter/graphql/server/.env: local dev config
- AgentCert/chaoscenter/manifests/litmus-*.yaml: K8s ConfigMap values
- Docs/examples: AgentCert/docs/next-step.md, AgentCert/docs/new_requirement.md