Skip to content

feat!: tw-breaker, one circuit-breaker state machine for both gateways; hidden-text scan in tw-guard - #155

Merged
fylorn merged 1 commit into
mainfrom
feat/tw-breaker
Sep 24, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/tw-breaker

Conversation

@fylorn

@fylorn fylorn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

tw-breaker

The desktop's upstream health, the server edition's route health and its MCP breaker each had their own state machine. Their premises differ:

  • The desktop has one user in one process, and fails open.
  • The server edition's routes are replicas sharing Redis, and a tripped route is filtered out.

The machine is the same, so it is now shared:

  • Breaker is plain, serialisable data. Transitions are pure functions of it, a Policy, and a time the caller supplies. Storage is the caller's: a mutex (Breakers<K>) or a Redis key.
  • Trip is either N consecutive failures or an error rate over a window. The window is counted by whoever stores the samples; the machine only needs the tally.
  • A cooled breaker reads as half-open with nobody nudging it. This fixes a server-edition bug: a route moved from open to half-open only when a request on it completed, but an open route is never picked, so it stayed open until its Redis key expired, about 4× the cooldown.
  • Half-open admits. probes successes close it, and one failure reopens it and restarts the cooldown. A late result that arrives while open still counts.

The desktop's health runs on it with the same policy as before: 3 failures, 60 s cooldown, one probe. Its own rules stay on top: fail open when every candidate is down, and bypass the breaker for a single candidate. A half-open provider still reads "ok" on the upstreams page.

hidden → tw-guard

hidden (invisible and look-alike characters) moves from tw-scan into tw-guard, so the server edition can scan requests with it.

Kind::smuggles names the two kinds with no legitimate use in any text: tag characters and bidi overrides. Zero-width joiners build emoji, and Cyrillic is just Russian. The config scan grades by the same method.

Tests

  • 12 new state-machine tests: transitions reported once, the streak reset, cooling into half-open, probes deciding, several probes, late results, admit, error-rate thresholds, a JSON round trip, and keyed breakers on a controllable clock.
  • One new desktop health test on the clock.
  • One test for smuggles: emoji, Russian and Arabic text are not flagged.
  • cargo test --workspace, clippy and scripts/smoke.sh (55 passed, 0 failed) all pass.

Breaking: tw_gateway::health::State is now tw_breaker::State, which adds HalfOpen; tw_scan::hidden is now tw_guard::hidden.

🤖 Generated with Claude Code

@fylorn
fylorn force-pushed the feat/tw-breaker branch 2 times, most recently from 4b6dd65 to 22c0ff6 Compare September 24, 2026 02:30
…s; hidden-text scan in tw-guard

The desktop's upstream health, the server edition's route health and its
MCP breaker each carried their own state machine. Their premises differ —
one user in one process that fails open, versus replicas sharing Redis
that filter a tripped route out — but the machine is the same: closed,
open, half-open once the cooldown is over, and a probe deciding.

tw-breaker is that machine and nothing else:

- `Breaker` is plain, serialisable data; transitions are pure functions
  of it, a `Policy` and a time the caller supplies. Where it lives — a
  mutex, a Redis key — is the caller's business. `Breakers<K>` is the
  in-process keyed set.
- A `Trip` is either N consecutive failures or an error rate over a
  window; the window is counted by whoever stores the samples.
- A cooled breaker reads as half-open with nobody nudging it. The server
  edition moved a route from open to half-open only when a request on
  that route completed, and an open route is never picked — it stayed
  open until its Redis key expired.
- Half-open admits; `probes` successes close it, one failure reopens it.
  A late result while open still counts.

The desktop's `health` runs on it with the same policy (3 failures, 60 s,
one probe) and keeps its own rules on top: fail open when every
candidate is down, bypass a single candidate. A half-open provider still
reads "ok" on the upstreams page.

`hidden` (invisible and look-alike characters) moves from tw-scan into
tw-guard, for the server edition to scan requests with. `Kind::smuggles`
names the two kinds with no legitimate use in any text — tag characters
and bidi overrides; zero-width joiners make emoji and Cyrillic is
Russian. The config scan grades by it too.

BREAKING: tw_gateway::health::State is tw_breaker::State (adds
HalfOpen); tw_scan::hidden is tw_guard::hidden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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