Skip to content

feat(api): every closed set is an enum; credential and sign-in reasons are coded - #176

Merged
fylorn merged 1 commit into
mainfrom
feat/api-more-enums-coded-credentials
Sep 24, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/api-more-enums-coded-credentials

Conversation

@fylorn

@fylorn fylorn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #174, which did the first eight sets. This covers the rest of the closed-set strings in tw-api and makes the credential and sign-in errors coded all the way through.

Closed sets → enums (same wire words)

New slug_enum! types, exported to TypeScript as string-literal unions: Billing, Protocol, Dialect, ProxyKind, OnProxyFail, ModelSource, Health, BreakerState, ProxyState, AuthState, FailureSource, GuardMode, SecurityOutcome, ContentMatch, SecretKind, HiddenKind, LoginStatus, ChatgptLoginMode, ZaiFamily, ConfigOrigin, ConfigStage, PricingSource, ProbeClass, ProbeMode, ConditionField, SetField, ServeSkip, DryRunOutcome, RuleVerdict, RuleEffect, ScanSource, Verification, FieldOp, McpOp (and DryRunResult.strategy uses GroupKind).

  • Internal enums map to them through exhaustive From impls (tw-config/src/wire.rs, tw-gateway/src/wire.rs), so a new variant on either side fails to compile.
  • The request store keeps the words in its columns and parses them back with a checked reader; RequestRow.billing / TurnRow.billing / SecurityEvent.action are typed.
  • A value outside a set is now a request-body error (422 control.request_rejected), so the hand-written checks go away: security.unknown_mode, security.unknown_match, control.unsupported_value, control.unsupported_action, control.unknown_signin_mode, control.unknown_account_family, control.rule.unknown_condition.
  • slug_enum! also gives each type Display and comparison with its wire word.

Left as strings on purpose: names and ids the user or an upstream chooses (proxy names, client ids, rule/category names in SecurityRuleView.kind, QuotaWindow.window / status, ResetCreditView fields), auth_header (a header name), DryRunRequest.intent (empty means a real request).

Credential and sign-in reasons → Msg

  • OauthError has a code per variant (gw.oauth.{unreachable,status,no_token,expired}, and gw.oauth.backoff.{unreachable,status,no_token} for a backoff by cause); ${ENV} failures have config.secret.{env_missing,unterminated,empty_name} (replacing the passthrough config.credential.env); RotateError has config.rotate.*.
  • headers_for / oauth_token_for / headers_after_401 return Msg.
  • New Msg::in_context(name, value, lead): keeps the reason's code, adds the context (upstream, proxy, sign-in step) as an argument and prefixes the English. It replaces the {detail} wrapper codes gw.credentials.failed, control.credentials_failed, gw.models.credentials, control.provider_test.credentials, gw.config.proxy_password, l1.config.proxy_password.
  • Now Msg: CredentialRotated.detail (control.rotation.written, gw.oauth.rotation_{queue_full,no_manager}, or the write-back error's own code), CredentialExpired.detail, OAuthView.failure, ChatgptLoginStatus.error / ZaiLoginStatus.error / LoginFinished.error (control.chatgpt_login.*, gw.chatgpt.token_*, control.zai_login.*, Z.ai call failures keep their control.account_service_* codes with a step argument), ConfigRejected.message, RuleTrace.error.

CONTROL_API_VERSION 14 → 15. msg-codes.txt updated.

Local: fmt, clippy (--all-targets and --lib), workspace tests, and the ts steps (clippy, tests, export, tsc --strict).

🤖 Generated with Claude Code

…s are coded

The rest of the fixed-vocabulary `String` fields in tw-api are enums now,
with the same wire words (serde renames; ts-rs exports string-literal
unions): Billing, Protocol, Dialect, ProxyKind, OnProxyFail, ModelSource,
Health, BreakerState, ProxyState, AuthState, FailureSource, GuardMode,
SecurityOutcome, ContentMatch, SecretKind, HiddenKind, LoginStatus,
ChatgptLoginMode, ZaiFamily, ConfigOrigin, ConfigStage, PricingSource,
ProbeClass, ProbeMode, ConditionField, SetField, ServeSkip, DryRunOutcome,
RuleVerdict, RuleEffect, ScanSource, Verification, FieldOp, McpOp.
`slug_enum!` also gives them Display and comparison with their word.
Internal enums map to them through exhaustive `From` impls (tw-config's
and tw-gateway's `wire` modules). A value outside a set is now a
request-body error (422 control.request_rejected), so the hand-written
"unknown value" checks and their codes are gone.

Credential errors are coded end to end. OauthError has a code per variant
(gw.oauth.*, backoff per cause), `${ENV}` failures have config.secret.*,
RotateError has its own codes, and headers_for / oauth_token_for /
headers_after_401 return `Msg`. Where a reason sits inside a larger
context (which upstream, which proxy, which sign-in step), the new
`Msg::in_context` keeps the reason's code and adds the context as an
argument, instead of wrapping English text in a `{detail}` code; the
gw.credentials.failed / control.credentials_failed / gw.models.credentials
/ control.provider_test.credentials / *.proxy_password wrappers are gone.
CredentialRotated/CredentialExpired.detail, OAuthView.failure, the
ChatGPT and Z.ai sign-in errors (status and event), ConfigRejected.message
and RuleTrace.error are `Msg`.

CONTROL_API_VERSION is 15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 136c3b7 into main Sep 24, 2026
3 checks passed
@fylorn
fylorn deleted the feat/api-more-enums-coded-credentials branch September 24, 2026 10:30
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