Skip to content

feat(api): closed sets are enums; attempt, model-list and probe reasons are coded - #174

Merged
fylorn merged 1 commit into
mainfrom
feat/api-closed-sets-coded-reasons
Sep 24, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/api-closed-sets-coded-reasons

Conversation

@fylorn

@fylorn fylorn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Found while the desktop UI adopted the generated types: several fields are String on the wire but only ever carry a fixed set of words, so the UI narrows them by hand; and three reasons reach the Chinese UI as English sentences because they are plain strings, not Msg.

Closed sets → enums (same wire words)

A small slug_enum! macro in tw-api declares each set once: serde renames keep the wire strings, ts-rs exports a string-literal union, and slug() / from_slug() / ALL serve storage and path building.

Enum Fields
Guard SecurityEventView.guard, SecurityEventsQuery.guard
GroupKind GroupView.kind, GroupInput.kind
ModelListStatus ProviderView.model_status, ProviderModelsView.status
TakesEffect DetectedClient / KeySynced / AdoptResponse .takes_effect
AttemptOutcome AttemptView.outcome
RuleAction SecurityRuleView.action / default_action, ActionSave.action, CustomRuleSave.action, SecurityTestHit.action, Event::ContentMatched.action, Event::ToolCallFlagged.action
ScanLevel / FindingLevel ScanFinding.level / FindingView.level
L1Step / L1Peer / L1SkipReason L1Stage.step / .peer, L1Skip.reason (tw-gateway's l1 uses these instead of its own copies)

tw-control's private Guard enum is replaced by tw_api::Guard; engine/adopt/scan enums map to the contract ones through exhaustive matches.

Reasons → Msg

  • AttemptView.error: the per-hop English sentence is gone; a failed hop carries the same coded message the hop reports to the client (gw.route.protocol_mismatch, gw.convert.failed, gw.credentials.failed, gw.upstream.*, …). WebSocket hops too.
  • ProviderView.model_error / ProviderModelsView.error: new gw.models.{credentials,check_failed,no_endpoint,unrecognized,empty}; a failed probe passes its own code through.
  • ProviderTestResult.error: new gw.probe.{timeout,connect,key_rejected,request_failed} (reqwest's text is the detail argument), control.provider_test.{oauth_unsaved,credentials}; config and credential errors pass their existing coded Msg.

Removed: control.group.unknown_strategy (an unknown strategy is now a request-body error). msg-codes.txt updated.

Versions

  • CONTROL_API_VERSION 13 → 14 (three fields changed from string to object).
  • Request store SCHEMA 18 → 19: the stored routing JSON carries Msg errors now.

Tests: a wire round-trip test for every enum; model-list, provider-test, security and WebSocket tests assert codes instead of English text. cargo fmt, clippy (--all-targets and --lib), workspace tests, and the ts feature steps (clippy, tests, export, tsc --strict) pass locally.

🤖 Generated with Claude Code

…ns are coded

Fields whose value comes from a fixed set were `String` on the wire, so
the desktop UI had to narrow them by hand. They are enums now, with the
same wire words, and export to TypeScript as string-literal unions:

- Guard: SecurityEventView.guard, SecurityEventsQuery.guard
- GroupKind: GroupView.kind, GroupInput.kind
- ModelListStatus: ProviderView.model_status, ProviderModelsView.status
- TakesEffect: DetectedClient/KeySynced/AdoptResponse.takes_effect
- AttemptOutcome: AttemptView.outcome
- RuleAction: SecurityRuleView.action/default_action, ActionSave.action,
  CustomRuleSave.action, SecurityTestHit.action, and the action of the
  ContentMatched / ToolCallFlagged events
- ScanLevel: ScanFinding.level; FindingLevel: FindingView.level
- L1Step / L1Peer / L1SkipReason: L1Stage.step/peer, L1Skip.reason
  (tw-gateway's l1 now uses these instead of its own copies)

Three reasons that reached the UI as English strings are `Msg` now:

- AttemptView.error is the same coded message the hop reports to the
  client (the separate English sentence per hop is gone)
- ProviderView.model_error / ProviderModelsView.error: new gw.models.*
  codes; failures from the probe carry its gw.probe.* code
- ProviderTestResult.error: new gw.probe.* codes for timeout, connect,
  rejected key and other request failures (reqwest's text as `detail`),
  and control.provider_test.* for the OAuth cases

An unknown group strategy is now a request-body error, so
control.group.unknown_strategy is gone. CONTROL_API_VERSION is 14; the
stored routing JSON changed shape, so the request store SCHEMA is 19.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the feat/api-closed-sets-coded-reasons branch from 6383a65 to 4ada23f Compare September 24, 2026 09:32
@fylorn
fylorn merged commit 2505e23 into main Sep 24, 2026
3 checks passed
@fylorn
fylorn deleted the feat/api-closed-sets-coded-reasons branch September 24, 2026 09:42
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