initiative-api

module
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT

README

initiative-api

License Issues PRs Coverage

Go API behind Initiative!, the SweetRPG initiative tracker. All tracker rules live here; clients are presentation only (PADR-0033, proposed). Design and task list: openspec/changes/initiative-api in sweetrpg/platform.

Status: skeleton. Config validation, logging, tracing, and /status/* are in place; auth, data layer, and domain routes are not.

Running

cp .env.example .env
make run
Variable Default Notes
BIND_ADDRESS :8000 host:port
ENV dev
LOG_LEVEL WARN DEBUG, INFO, WARN, ERROR
OTEL_EXPORTER_OTLP_ENDPOINT unset absolute URL of the trace collector
AUTH_API_URL required auth-api base URL; tokens are verified via /authz/check
USERS_API_URL required users-api base URL; resolves the caller to users._id
ENTITLEMENTS_API_URL required entitlements-api base URL; the caller's bearer is forwarded on checks
DB_OPTS / DB_URI query driver timeouts added serverSelectionTimeoutMS=5000, connectTimeoutMS=5000 and socketTimeoutMS=30000 are added when the connection settings do not set them
ALLOWED_ORIGINS unset comma-separated; unset disables CORS, * is rejected
SESSION_MEMBERS_MAX 20 maximum of a session's member limit; an entitled members_per_session above it has no effect; positive integer
SESSION_DEVICES_MAX 2 most connections one member (the GM included) may hold to a session stream; a further one is refused with 403 device_limit_reached; positive integer
REQUEST_TIMEOUT 10s deadline for every request except the event stream upgrade; past it a store call fails and the answer is 503 timeout with Retry-After; 1s-1m
STREAM_ALLOWED_ORIGINS unset comma-separated host patterns a browser Origin may match to open an event stream (no Origin is always allowed; * is rejected); CORS ALLOWED_ORIGINS still applies first, so a cross-origin stream client must be in both lists
STREAM_TICKET_TTL, STREAM_MAX_AGE, STREAM_HEARTBEAT 30s, 1h, 20s stream ticket lifetime, maximum connection age, and heartbeat (application heartbeat and protocol ping interval); Go durations: ticket TTL 1s-2m, max age 1m-24h, heartbeat 1s-1m
STREAM_RECHECK_INTERVAL 5s how often an open session stream re-reads its member's record and a credential-backed stream re-checks its credential; 1s-1m, never above STREAM_HEARTBEAT. Revocation bound, when the eviction signal is lost: a removed, demoted or blocked member's stream closes (4002) within this interval plus the 2s check timeout (7s by default) when the client keeps up, plus up to 12s (a heartbeat write the client takes 10s to accept, then a 2s seat renewal) if its socket is slow (19s). If the store cannot answer, the stream closes at the third failed re-check, within 3 x (interval + 2s) (21s, 33s with a slow client); a connect (which re-reads the member's record) is refused while the store is down. A store slower than the 2s check timeout for three re-checks in a row (about 15s) closes healthy streams with 4002: fail-closed by design, and the client reconnects once the store answers. User-level streams have no membership re-check by design: each notice they receive is checked for membership. Derived in code by StreamOptions.RevocationBound, StalledBound, FaultBound and StalledFaultBound
CREDENTIAL_EVENTS_MAX_DISCONNECT 10m how long NATS may be unreachable before integration credentials answer 503 (the rest of the API keeps serving); they reopen after the consumer reconnects and catches up; 30s-1h
STREAM_CONCURRENT_MAX, STREAM_CREDENTIAL_MAX, STREAM_MAX_PER_POD 10, 3, 1000 most event streams one user may hold across all pods (429 too_many_streams with Retry-After), most one integration credential may (its own count; it never uses up its owner's), and most one pod serves (close code 1013, ticket not spent)
STREAM_LEASE_TTL, STREAM_GRACE 60s, 30s how long a stream's seat lasts without a renewal (at least twice STREAM_HEARTBEAT), and how long a dropped client keeps it for a reconnect; seats are compared across pods by their clocks, so keep node clocks in sync. If the seat store is down, new streams get 503 streams_unavailable and open streams stay up (seats lapse after the TTL)
OWNER_ENTITLEMENTS_MAX_AGE 168h how long a recorded owner entitlement serves integration credentials; positive Go duration
REDIS_HOST, REDIS_PORT, REDIS_PASS REDIS_HOST required rate-limit counters; the service stops at startup without REDIS_HOST
RATE_LIMIT_STANDARD, RATE_LIMIT_STANDARD_WINDOW_SECONDS, RATE_LIMIT_CHEAP, RATE_LIMIT_CHEAP_WINDOW_SECONDS api-core defaults per-client budgets (PADR-0036)

Invalid values stop the process at startup with every problem listed.

Endpoints

  • GET /status/ping: shallow liveness and readiness probe.
  • GET /status/health: deep dependency check (database); 503 while one is down. Not used by the probes (PADR-0017).
  • GET /themes, GET /themes/packaged, GET /themes/{id}, POST /themes, PATCH /themes/{id} (body carries revision), DELETE /themes/{id}?revision=N. Custom themes take the lowest free slot below initiative.theme_slots; packaged themes (pkg- IDs) are read-only. Errors are {code, message, correlation_id, key?, limit?}.
  • PUT /entitlements/active/{themes|rosters|encounters|credentials} (body ids): choose which items stay active when a limit is below the number owned. Over-limit items go inactive (inactive_at), never deleted: readable and deletable, not editable (403 item_inactive), and an inactive credential stops authenticating. Limits are re-read on the owner's own requests (at most every 30 s).
  • Without the initiative.game_systems_extra flag, encounters using a game system other than No System are locked (locked_at): readable and deletable, not editable or runnable (403 entitlement_required), and unlocked unchanged when the flag returns. Creating or switching to a gated system without the flag gets the same error.
  • POST /credentials (body label, scopes; the secret is returned once), GET /credentials, POST /credentials/{id}/revoke. Integrations send the secret as Authorization: Bearer ic_...; it acts as its owner with the user role, read allows GET and write allows other methods. Credentials cannot reach /credentials, /entitlements or /themes. A deny of this service or a removed role in auth-api ends the owner's credentials (PADR-0039) and then every open stream of that user: all sessions, user-level and private, closed on the pod that consumes the event at once and on the others as their fan-out tail delivers the row (milliseconds), over the same path as session evictions; nothing is remembered, so a user granted access again simply reconnects. If a fan-out row is lost (a full eviction queue, logged at error level) the user's session and user-level streams stay open until STREAM_MAX_AGE (they are still members, so the re-check does not end them), though every reconnect is refused by auth-api. The service consumes the AUTH_EVENTS stream (NATS_URL, optional NATS_DURABLE_NAME, NATS_CREDS or NATS_USER/NATS_PASSWORD). Credentials answer 503 credentials_unavailable until the consumer has caught up, and stay off if NATS_URL is unset or the consumer fell behind the stream's retention. POST /admin/credentials/reopen (admin bearer only) clears that close and logs an audit entry; the gate records the actor. Encounter events are also published to the INITIATIVE_EVENTS stream on initiative.events.<entity>.<action> (NATS_URL, same login), fail-open: a broker outage drops the event with a log line and never fails the write. Nats-Msg-Id is the event id. Addressed notices are also published, one event per unmuted addressee, on initiative.user.<user_id>.<type> (turn.yours stays two tokens: initiative.user.u1.turn.yours), with user_id in the envelope, for a future push service. Administrator routes (admin bearer only, no credentials): GET /admin/credentials[?owner=] lists any owner's credentials (never the secret or hash); DELETE /admin/credentials/{id} with {"reason": "..."} removes one for good, at once; GET /admin/credentials/deletions[?owner=] lists the audit entries (paged with limit and cursor). A 500 deletion_incomplete means retry the same request. The entry (administrator, owner, credential id, label, scopes, reason, time) goes to integration_credential_deletions before the credential is removed, and has no update or delete path. Credentials.Live(id) is what an open stream re-checks. Delete class (PADR-0027): integration_credentials are soft delete for the owner's own removal (revoke) and hard delete only through the administrator route above.
  • Sessions (user bearer only, no integration credentials): POST /sessions, GET /sessions, GET|DELETE /sessions/{id} (delete is the GM's, with ?revision=), POST /sessions/{id}/leave. Members: GET /sessions/{id}/members, PATCH|DELETE /sessions/{id}/members/{user}. Invites: POST|GET /sessions/{id}/invites (the code is returned once, only its hash is stored), DELETE /sessions/{id}/invites/{invite} revokes. Joining: POST /sessions/join {code} makes a pending request that grants nothing; GET /sessions/{id}/requests, POST .../requests/{id}/approve {role} or /reject {reason}. Blocks: GET|POST /sessions/{id}/blocks, DELETE .../blocks/{user}. Notifications: turn.yours, turn.next and the change-request events (change_request.submitted to the GM and co-GMs, .approved / .rejected to the submitter) are delivered only to the addressee's open streams of that session. GET|PUT /sessions/{id}/notification-preferences {"muted": [...]} mutes types per member; state events are unaffected. Connect limit: a member's connections to a session stream share one seat in a per-session bucket, counted across pods in the lease collection. The session holds at most the member limit of distinct non-GM members (the owner's recorded initiative.members_per_session, clamped to SESSION_MEMBERS_MAX, the same number as the roster; the GM holds no member seat), so a member's second device takes no further seat, and a connect by a member who would be one too many is refused with 403 entitlement_limit_reached (key and limit in the body) with the roster untouched. One member, the GM included, may hold at most SESSION_DEVICES_MAX (default 2) connections to a session; a further one is refused with 403 device_limit_reached (key devices_per_member and limit in the body). A client that reconnects while the server still holds its old connection counts as a further device, so a member at the limit can get this code until the old connection times out; clients retry with backoff on it. Seats lapse after STREAM_LEASE_TTL unless renewed (a crashed pod's connections stop counting), and a dropped client that reconnects with the same client_id within STREAM_GRACE takes its seat back even if others filled the session. Nothing ends a live connection because the limit fell. User-level stream: POST /streams/tickets {"scope":"user"} (no session or encounter), then GET /streams?ticket=, or GET /streams?scope=user with the bearer. It carries only the addressed events of every session the user still belongs to (checked per delivery), no encounter state; integration credentials cannot open it. Roles are gm (the creator, exactly one), co_gm, player, observer; domain.Allowed is the one role table. Membership collections are the source of truth for who holds a role. Membership, invite, request and block records and the session are all soft-deleted (deleted_at, deleted_by; PADR-0027), and a live marker lets the unique indexes ignore deleted rows. Limits follow the GM: creating a session checks initiative.sessions_per_gm (403 entitlement_limit_reached, 503 when entitlements are down); sessions beyond a reduced limit go inactive (readable, deletable, leavable; no invites, approvals, or joins; 403 item_inactive). Each GM request re-reads initiative.members_per_session with the GM's token and records it on the session; member checks use that record only (503 owner_entitlements_pending until one exists).
  • GET /combatants?ref_key=&ref_value=: the caller's combatants whose external_refs match.

OpenFGA session tuples are rebuilt from the membership collection by cmd/reconciler (a CronJob every 5 minutes running as the initiative-reconciler user with the openfga-writer role; PADR-0041 covers how it signs in). For each session it reads the tuples first and then that session's current members, so a grant or revoke between the two reads is at worst corrected by the next pass (PADR-0040). After the tuple pass it closes session mints older than 15 minutes whose session is missing or soft-deleted in Mongo (auth-api GET /internal/openfga/session-mints, paged by after/next, then POST .../{id}/close); a failed sessions lookup skips that batch until the next pass. A live session with no live GM record is soft-deleted as the system, with its membership records (records first, session last, so a failed pass is retried), and its mint closed. Nothing in this repo removes a live GM record today, so this only reaps double-failure orphans (Open stored the session but neither its GM record nor its own removal) and sessions an operator stripped of a GM; the latter lose their co_gm and player data 15 minutes later. One extra $in query per mint page finds them, covered by the partial session_user index (session_id prefix, live exists). A pass stops after removing 10 and fails the job once (a wrong database or an empty members collection would otherwise wipe every old session; earlier pages may already have removed up to 10). A session younger than 15 minutes, or one that gained a GM, is left alone; a failed query leaves live sessions alone and fails the job. The manifests are in kubernetes/overlays/dev/reconciler.yaml; reconciler-alert.yaml alerts when no run has succeeded in 15 minutes (token expired, job failing, or CronJob suspended).

The reconciler signs in to auth-api as an Auth0 machine-to-machine client: RECONCILER_CLIENT_ID, RECONCILER_CLIENT_SECRET, RECONCILER_TOKEN_URL (https) and RECONCILER_AUDIENCE fetch a client-credentials token, cached in Redis as reconciler:token until 120 seconds before it expires. A 401 from auth-api clears the cache, fetches once more and retries once. Exit 1 is a failed pass or an Auth0 or Redis failure, 2 missing or invalid configuration, 3 auth-api rejected a token. A 401 on a token fetched in the same run sets reconciler:rejected for an hour so a persistent 401 cannot spend the Auth0 token allowance; fix the cause, then DEL reconciler:rejected in the initiative cache to retry at once.

A failed Auth0 token request (429, 5xx, 401/403 from Auth0, network error, malformed answer) backs off further fetches: 5, 10, 20, 40, then 60 minutes (or Auth0's Retry-After on a 429 when longer, at most 60), tracked in reconciler:auth0-failures (2 hour TTL, cleared by a successful fetch) and reconciler:auth0-backoff. While it exists a run fails with exit 1 without calling Auth0, but a cached token is still used. After the cause is fixed, DEL reconciler:auth0-backoff reconciler:auth0-failures to retry at once.

Requests are rate limited per client (PADR-0036): the X-API-Key header, else the client IP, and an ic_ integration credential counts as its own client. Over the budget is 429 with Retry-After; if Redis is down, /status/* still answers and every other route returns 503.

Every response carries X-Correlation-ID (the caller's value, or a generated one), also written to the access log as correlation_id.

Checks

make lint test

Directories

Path Synopsis
cmd
initiative-api command
Command initiative-api serves the Initiative! API.
Command initiative-api serves the Initiative! API.
reconciler command
Command reconciler runs one OpenFGA tuple reconciliation pass and exits (PADR-0035, PADR-0038).
Command reconciler runs one OpenFGA tuple reconciliation pass and exits (PADR-0035, PADR-0038).
seed command
Command seed applies the checked-in game-system definitions to the configured database.
Command seed applies the checked-in game-system definitions to the configured database.
Package config loads and validates the service configuration from the environment.
Package config loads and validates the service configuration from the environment.
Package constants holds service-wide names and environment variable keys.
Package constants holds service-wide names and environment variable keys.
Package data is initiative-api's data layer.
Package data is initiative-api's data layer.
Package domain holds initiative-api's rules over the owner-scoped data layer: rosters and characters, and moving cast between rosters and encounters.
Package domain holds initiative-api's rules over the owner-scoped data layer: rosters and characters, and moving cast between rosters and encounters.
Package entitlements is the client for entitlements-api's check routes (docs/entitlements-check-contract.md in the platform repo, PADR-0034).
Package entitlements is the client for entitlements-api's check routes (docs/entitlements-check-contract.md in the platform repo, PADR-0034).
Package events consumes auth-api's change events from the platform bus (PADR-0039): a deny or a role removal ends the subject's integration credentials.
Package events consumes auth-api's change events from the platform bus (PADR-0039): a deny or a role removal ends the subject's integration credentials.
stream
Package stream turns committed encounter changes into the events subscribers receive.
Package stream turns committed encounter changes into the events subscribers receive.
Package reconcile rebuilds the OpenFGA session tuples from the membership collection, which is the source of truth (PADR-0035).
Package reconcile rebuilds the OpenFGA session tuples from the membership collection, which is the source of truth (PADR-0035).
Package relations is the client for auth-api's POST /authz/check-relation (PADR-0035).
Package relations is the client for auth-api's POST /authz/check-relation (PADR-0035).
Package seed loads the versioned game-system definitions checked into this repo.
Package seed loads the versioned game-system definitions checked into this repo.
Package server wires the HTTP routes and middleware.
Package server wires the HTTP routes and middleware.
Package startup holds the retry that process start-up uses against its dependencies.
Package startup holds the retry that process start-up uses against its dependencies.
Package store is the narrow MongoDB surface the data layer needs.
Package store is the narrow MongoDB surface the data layer needs.
fake
Package fake is an in-memory store.Collection for tests.
Package fake is an in-memory store.Collection for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL