Skip to content

Event State Machine

Every event in PromptJang follows a strict state machine. This ensures predictable behavior and a clear audit trail.

States

StateDescription
RECEIVEDEvent received by the API worker (transient)
QUEUEDEvent enqueued for delivery
PROCESSINGDelivery worker is attempting to forward the event
DELIVEREDTarget endpoint returned 2xx — terminal state
FAILEDTarget returned non-2xx or was unreachable
RETRYINGEvent is scheduled for retry
EXPIREDMax retries exceeded — terminal state
REPLAYEDEvent was manually replayed — terminal state

State Transitions

RECEIVED → QUEUED → PROCESSING → DELIVERED ✓
                            ├─────▶ FAILED → RETRYING → QUEUED (loop)
                            │                        └──▶ EXPIRED ✗
                            └─────▶ (any non-2xx)

Retry Behavior

When delivery fails (non-2xx response or network error):

  1. Status moves to FAILED
  2. A delivery attempt is recorded with status code, response body, and latency
  3. If retries remain: RETRYINGQUEUED (back to the queue)
  4. If max retries exceeded: EXPIRED (terminal)

Exponential Backoff

Retries use exponential backoff:

AttemptDelay
1st retry30 seconds
2nd retry60 seconds
3rd retry120 seconds
4th retry240 seconds
5th retry480 seconds (8 minutes)

Formula: delay = 30 * 2^(attempt - 1) seconds, capped at 480s.

Default max retries: 5.

Event Replay

Any event (regardless of state) can be replayed via POST /api/v1/events/:id/replay. This:

  1. Creates a new event with status QUEUED
  2. Copies the original payload from R2
  3. The original event is marked REPLAYED

Replay does not modify the original event's delivery attempts — they remain in the audit trail.

Released under the MIT License.