Skip to content

feat(sts): deliver switch settings in the bundle - #13165

Open
iscekic wants to merge 7 commits into
mainfrom
igor/sts-settings
Open

iscekic wants to merge 7 commits into
mainfrom
igor/sts-settings

Conversation

@iscekic

@iscekic iscekic commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

feat(sts): deliver switch settings in the bundle

What

Decision M340: every STS-related switch moves out of wrangler vars and Vercel env vars into a
non-secret setting in Infisical (/workloads/<workload>/settings/<NAME>), delivered by STS in the
workload's bundle. This PR is the STS side plus the shared helpers. It is dormant on deploy: no
policy lists a setting and no reader calls readSwitch.

  • packages/sts-contracts: settings.ts (registry, SettingNameSchema, value schemas,
    resolveSwitch, SwitchUnavailableError, the switch_fallback marker, the two event types);
    bundle request opt-in settings: true; optional settings map on the ok answer.
  • services/sts: policy settings lists (top level and per environment, default []; load refuses
    an unregistered name, a duplicate, settings on a github-oidc policy; registrations list none),
    snapshot entry and policy.changed (addedSettings, removedSettings), settings resolution from
    the existing /workloads folder copy, ETag, bundle.settings plain column (generated migration
    drizzle.bundle/0002_*), setting.changed / setting.refused events, README "Settings
    (switches)".
  • packages/secrets-client: { settings: true } option on createWorkerSecrets /
    createHttpSecrets, setting(name, ctx) → delivered | absent | unavailable, 90-second
    revalidation for opted-in clients.
  • packages/worker-utils/read-switch: readSwitch(env, secrets, name, ctx).
  • packages/web-shared/lib/sts-switch: readSwitch(name) for Vercel (own opt-in client, built on
    first use behind the same STS_URL issuer check).

Wire order (pinned)

STS adds settings to an answer only when both the request sends settings: true and the
workload's effective policy lists at least one setting; otherwise the key is absent (never an empty
map). The response schema stays strict. Tests parse with a frozen copy of today's OkBundleSchema:
(1) an opt-in request to a workload with no settings in policy, (2) a request without opt-in to a
workload with settings in policy, and (3) the shipped policies with opt-in. The sealed row and its
parse are unchanged.

Readers (design section 6)

State Source (env) Sender / receiver (refuse)
delivered the value the value
absent (bundle loaded, no such setting) the variable through today's parser, else the default same
unavailable (no bundle after the first-load window, older than 24 h, denied, or a value this build does not know) the variable, as for absent SwitchUnavailableError (reason: 'unavailable', retryable): caller answers 503 (receiver) or fails the outbound call (sender)

An invalid variable reproduces today's parser: a fixed value where the parser had one
(BOUNCER_CLIENT_AUTH, STS_AUTH → legacy; SECRETS_COMPARE_ENABLED → false), otherwise
SwitchUnavailableError with reason: 'invalid_env' (not retryable; the caller answers it as its
configuration error today). One switch_fallback log line per isolate, setting and state.

Registry

Setting Values Default Kind onUnavailable
SECRETS_SOURCE (Workers) secrets-store, sts secrets-store source env
VERCEL_SECRETS_SOURCE (variable SECRETS_SOURCE) env, sts env source env
SECRETS_COMPARE_ENABLED false, true false source env
SESSION_INGEST_SENDER_AUTH, CODE_REVIEW_WORKER_SENDER_AUTH, O11Y_API_METRICS_SENDER_AUTH, O11Y_ALERTING_SENDER_AUTH, WEB_INTERNAL_SENDER_AUTH, SECURE_PROXY_SENDER_AUTH, GLANCEABLE_SNAPSHOT_SENDER_AUTH, BYOC_LOOKUP_SENDER_AUTH, PR_REFRESH_SENDER_AUTH, WEB_INTERNAL_AUTH, USAGE_WRITER_AUTH, USAGE_PUBLISHER_AUTH legacy, sts legacy sender refuse
BOUNCER_CLIENT_AUTH legacy, dual, sts legacy sender refuse
USAGE_RECORD_AUTH legacy, dual, sts legacy receiver refuse
ADMIN_CREDENTIAL_ACCEPT, INTERNAL_CREDENTIAL_ACCEPT, KILO_AI_GATEWAY_SECURE_PROXY_CREDENTIAL_ACCEPT disabled, legacy-only, legacy-and-sts, sts-only legacy-only receiver refuse
API_METRICS_CREDENTIAL_ACCEPT as above unset/empty → ADMIN_CREDENTIAL_ACCEPT receiver refuse
STS_AUTH (Bouncer) legacy, shadow, dual, sts legacy receiver refuse

Added with their reader PRs (not on main at this base): NOTIFICATIONS_SENDER_AUTH,
NOTIFICATIONS_LOW_BALANCE_SENDER_AUTH, SECURITY_AUTO_ANALYSIS_SENDER_AUTH, SECURITY_SYNC_SENDER_AUTH,
SESSION_SYNC_SENDER_AUTH, MODEL_EVAL_INGEST_SENDER_AUTH, WEBHOOK_AGENT_INGEST_SENDER_AUTH,
AUTO_ROUTING_DECIDE_SENDER_AUTH, GATEWAY_ROUTING_TABLES_SENDER_AUTH, AUTO_ROUTING_ADMIN_SENDER_AUTH,
AUTO_ROUTING_BENCHMARK_ADMIN_SENDER_AUTH, WEB_ROUTING_TABLES_SENDER_AUTH,
BENCHMARK_ROUTING_TABLES_SENDER_AUTH, INTERNAL_ROUTES_CREDENTIAL_ACCEPT.

Hot paths and limits (design section 3) this touches

  • Read secrets (hot path) and Secret reads (limit): an opted-in client revalidates every 90 s
    instead of 240 s, about 2.7 times the revalidation calls of that client. Each is an ETag check that
    the bundle object answers from storage with a 304, one delivery_counter upsert, and no audit
    row. Clients without opt-in are unchanged. Settings ride in the existing recursive /workloads
    read: no additional Infisical call. Dormant on deploy: no client opts in.
  • Write the audit row: unchanged count; bundle.delivered gains one list attribute only on an
    answer that carries settings.
  • STS audit relay: the refresher appends one event per settings change or refused entry version
    (operator writes in Infisical, not request-driven).

Per request

Request Network calls Storage writes Audit events
Bundle read, ETag matches (304) caller → STS → bundle object (unchanged) 1 delivery_counter upsert (unchanged) none (unchanged)
Bundle read, full body unchanged unchanged 1 bundle.delivered (unchanged), with settings: ['NAME:version'] only when settings are sent
Refresher push (per refresh, per workload) unchanged the bundle row write as today; when the policy lists settings, the settings column in the same statement none per push
Refresher folder read with a settings change unchanged (no new Infisical call) the folder row (unchanged) 1 setting.changed per changed setting, 1 setting.refused per refused entry version
readSwitch none of its own; reads the held bundle (a revalidation when due, as above) none none (one switch_fallback log line per isolate, setting and state)

Dormancy proof

$ git grep -n -E "^\s*settings:" -- 'services/sts/policy/*.yaml'      # exit 1: no policy lists a setting
$ git grep -n -E "readSwitch\(" -- '*.ts' '*.tsx' ':!**/*.test.ts' \
    ':!packages/worker-utils/src/read-switch.ts' ':!packages/web-shared/src/lib/sts-switch.ts'   # exit 1: no reader

test/worker/settings.test.ts "lists no setting in any shipped policy" pins the first. A push for a
policy without settings carries no settings field and writes a null column; every answer's shape
and ETag are what they are today.

Rollback

  • Before any reader opts in (this PR alone): revert/redeploy the previous STS. The new column is
    ignored by the old code; old code rewriting a row leaves the column, and the new code serves the
    column only while its stored ETag equals the row's, so nothing stale is ever served after a roll
    forward. Pushes of this release for policies without settings parse under the old strict schema.
  • After a reader opts in: this STS release is the floor. An older STS refuses
    settings: true (400 invalid_request / refused RPC). The client treats that as unavailable,
    keeps its held bundle, and never drops the opt-in; senders and receivers then refuse once their
    held bundle ages out. Roll STS forward instead, or first revert the readers.
  • A switch itself rolls back with an Infisical write (or deleting the setting: absent → today's
    variable) and reaches the workload within about 2.5 minutes with the webhook, or about 5.5 minutes
    on the 4-minute cadence. Which case holds in production is checked in the first policy PR.

Verification

Run on a disposable gcloud VM (n2-standard-8), branch at a006ca38e6, base ad4b27a7b9:

Check Result
pnpm install --frozen-lockfile exit 0 (lockfile: @kilocode/sts-contracts moves to worker-utils dependencies)
drizzle-kit generate --config drizzle.bundle.config.ts (services/sts) exit 0, 0002_cynical_ogun.sql: ALTER TABLE `bundle` ADD `settings` text;
packages/sts-contracts vitest 7 files, 184 tests passed
packages/secrets-client vitest 6 files, 58 tests passed
packages/worker-utils vitest 76 files, 1650 tests passed
services/sts vitest (workers pool) 28 files, 994 tests passed
services/sts vitest (node config) 13 files, 124 tests passed
web-shared jest (sts-switch, sts-secret-transport, sts-secret-reader) 3 suites, 68 tests passed
typecheck: sts-contracts, secrets-client, worker-utils, services/sts, web-shared, apps/web, apps/ai-gateway all exit 0
oxlint (changed packages) 0 errors
pnpm format:check exit 0
Mutation: drop the opt-in gate in BundleDO settings.test.ts fails (1 of 14)
Mutation: drop the policy gate in BundleDO settings.test.ts fails (1 of 14)

The branch merges cleanly onto the current main (git merge-tree exit 0), and every registered
switch and its parser are unchanged there.

Registry of STS-delivered switch settings (M340), the bundle request opt-in and optional settings map, the secrets-client setting() accessor with 90-second revalidation for opted-in clients, and readSwitch for Workers and Vercel. Dormant: no reader calls readSwitch.
Policy settings lists, settings resolution from the existing /workloads read, the ETag, a plain bundle.settings column (generated migration), setting.changed and setting.refused events, and the README. Settings ride only on an answer to a client that asks for a workload whose policy lists one; no shipped policy lists a setting, so the release is dormant.
Keep known settings readable across registry rollbacks and distinguish invalid or unconfirmed values from absence. Age settings by manager freshness without changing the legacy secret cache.
Commit setting audit evidence with the manager snapshot. Prove rollback and retry after a SQLite audit append fails, without publishing the new setting before the evidence is stored.
Distinguish a fresh, explicitly absent setting from a missing or stale grant, and let activation pin the reader to required grants without changing the opt-out protocol.
Authenticate the plain settings map against its row ETag, commit new policy grants with policy.changed before delivery, and keep unregistered manager keys out of refusal telemetry.
Check the target environment’s committed policy snapshot before serving or revalidating opted-in settings, even from a warm signed row. Scope settings audit diffs to the served environment.
Give Vercel deployments a single lazy reader with fixed grant pins, and cover warm omission, unaudited re-grants, environment expansion, and signed-column repair. Fix the exact type and formatting failures.
Use the checked workload policy for settings admission and restore the throttled, off-path alarm-only wake for warm opted-in bundles.
Cover alarm loss and rejected admission without a 304 or a lost secret bundle, and document the activation and rollback floor for both Durable Object versions.
- Replace the direct `expect(stub.committedSettings(...)).rejects.toThrow()` with an
  in-object `runInDurableObject` catch that asserts the genuine `SyntaxError` from the
  truncated JSON column.
- The direct RPC rejection also escaped as an unhandled error in the test worker even
  while every assertion passed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant