Project setup as code: lucida plan and apply

Keep flags, metrics, experiments, cohorts and keys in a checked-in folder, preview the diff with lucida plan and converge with lucida apply.

Embed
Preview of the card
<a href="https://lucidace.com/help/plan-and-apply"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/b0f6f1c1-2eb6-40a1-b4f8-0879b35e2ac7/social-card.svg?v=3e7ae33eec8b" alt="Project setup as code: lucida plan and apply" width="600" height="315"></a>

Definitions live as YAML files, one per object, in <dir>/<kind>/*.yaml (default lucida/). The kinds are actions, cohorts, flags, insights, metrics, experiments, dashboards, alerts, keys, export-schedules, endpoints, settings, toolbar-origins and webhook-sources. Objects are matched by a stable key, never by id, so the same files apply to any project and a re-run changes nothing.

Kind

Key

flags

key

endpoints

slug

settings

section (ingest, sdk or atlas_sso)

every other kind

name

lucida pull --dir lucida             # write the project's current definitions as files
lucida plan --dir lucida             # show what apply would change; changes nothing
lucida apply --dir lucida            # create and update by key
lucida prune --dir lucida            # list objects that are not in the files (deletes nothing)
lucida prune --dir lucida --yes      # delete them

plan prints one line per change, such as update flags/nav (enabled) or create keys/ci. It names the fields that differ and never prints values. A field missing from a file is left alone, not cleared.

  • Experiments have no upsert route. apply looks the experiment up by name: it creates a missing one, and sends only the changed fields to the existing one. A started experiment refuses design changes, so those fields fail loudly instead of being rewritten. prune archives an experiment, since it cannot be deleted.

  • Metrics are replaced as a whole when any managed field differs.

  • Keys are create-only. A key whose name already exists is left untouched, whatever its file says. prune never deletes keys.

Settings, toolbar origins and webhook sources

  • settings has two fixed objects, settings/ingest.yaml (section: ingest) and settings/sdk.yaml (section: sdk). They map to PUT /v1/projects/{id}/settings/ingest and .../settings/sdk, and any key those routes accept can go in the file. apply sends only the keys you wrote. pull writes only the keys that differ from the API's defaults, so the file states your choices. prune never resets a section.

  • toolbar-origins is one object, toolbar-origins/origins.yaml: name: origins and origins: [...] (the sites the toolbar may run on). prune never deletes it.

  • webhook-sources are matched by name. A file holds name, mapping, enabled, log_payloads, log_keep and auth. A secret is never pulled. To create a source with a signing secret, put auth_secret: env:NAME in the file. apply reads the value from that environment variable when it creates the source, and fails naming the variable if it is not set. A literal value is refused. The secret is read on create only.

  • atlas_sso is a third settings section, settings/atlas_sso.yaml: section: atlas_sso, enabled: true and optionally org_id (default: the project's organization). It is organization-level, so it uses GET /v1/orgs/{id}/atlas-sso and POST .../atlas-sso/setup-link with an account key restricted to that organization that has sso:* or admin. The API cannot connect an identity provider for you, so one step stays human: while no connection is active, plan shows pending settings/atlas_sso (and --exit-code counts it as drift), and apply creates the Atlas organization if needed and prints a one-time setup link (valid 7 days) for your IT admin to connect SAML or OIDC. Once a connection is active, the plan is clean. enabled: false with an active connection is reported as pending too: the API has no way to disable SSO, so do it in Atlas. pull does not write this section.

# lucida/settings/ingest.yaml
section: ingest
server_event_consent: require_granted
capture_allowed_origins: ["https://example.com"]

Start a folder with lucida init

lucida init --framework next --consent cookiemunch --write --defs deploy/lucida --origin https://example.com

After the usual Next.js edits, this also writes the starter folder (default deploy/lucida): settings/ingest.yaml, settings/sdk.yaml and, with --origin (comma separated), toolbar-origins/origins.yaml. With --consent cookiemunch, the files set server_event_consent: require_granted and, for the SDK, consent_mode: opt_in and mask_all_inputs: true. It never overwrites a file that exists. Without --write it only lists what it would write. It ends by printing next: lucida plan --dir deploy/lucida, so check the diff, then run apply.

Does it delete, and what does plan return

  • apply never deletes. It creates and updates only. Objects that exist in the project but not in your files are left alone.

  • prune deletes, and only with --yes. Without --yes it prints the deletions it would make and changes nothing. It looks only at kinds that have a directory under --dir, and it never deletes keys.

  • plan fails on drift only when asked. It is apply --dry-run and exits 0 by default. Add --exit-code (lucida plan --dir lucida --exit-code) to exit 1 when any change is planned, so a CI job fails on drift; nothing to do exits 0.

  • pull options. --kinds flags,cohorts pulls only those kinds. Stopped experiments and revoked keys are left out unless you pass --include-stopped or --include-revoked. pull replaces the <dir>/<kind>/ folder for each kind it pulls.

  • --project picks the project; it defaults to $LC_PROJECT_ID or the login default.

  • apply stops at the first error. Changes already made stay, and a re-run converges because objects are matched by key.

  • Export schedules change in place only for name, interval and enabled. Their stream, format, destination, warehouse, chunk size and options are fixed when created.

Keys and secrets

A key file holds name, kind and scopes, never a secret. When apply creates a key, the secret comes back once. By default it prints once on stdout as new key secret (shown once) <name>=<secret>. Pass --secret-out <file> to append name=secret lines to a file created with mode 0600 instead; nothing then reaches stdout. Plan output and pulled files never contain a secret. Store it right away, because Lucida keeps only its hash. To replace a key, use lucida keys rotate.

Updated