Skip to content

Fix a Node.js circuit breaker state machine (rolling window, half-open concurrency, timeouts, exact transition events)

Exact prompt

# Task: fix the circuit breaker state machine `breaker.js` (Node 20, CommonJS, no dependencies) wraps calls to a flaky service. Make `CircuitBreaker` implement exactly this state machine (keep the constructor options and export): - **closed**: `call(...args)` invokes `fn(...args)` and resolves/rejects with its result. A synchronous throw from `fn` behaves like a rejection. A failure is a rejection for which `isFailure(err)` returns true (others are passed through to the caller but otherwise treated like a success: not counted). Failures are counted in a **rolling window**: only failures whose time (`now()` when the call settled) is within the last `windowMs` (i.e. `t > now - windowMs`) count; successes do **not** reset the count. When the count reaches `failureThreshold`, the breaker goes **open** (`openedAt = now()`). - **open**: every call rejects immediately with an `Error` whose `code` is `'EOPEN'`, without invoking `fn`, until `now() - openedAt >= cooldownMs`; at that point the breaker is **half_open** (the `state` getter must already report `'half_open'` then, even before a call). - **half_open**: at most `halfOpenMax` trial calls may be in flight at once; extra calls reject with `EOPEN` without invoking `fn`. Any trial failure -> **open** again (new `openedAt`), and the in-flight results don't change state further. After `successThreshold` successful trials in a row -> **closed** with failure history cleared. - `timeoutMs` > 0: a call not settled within `timeoutMs` real milliseconds rejects with an `Error` with `code` `'ETIMEOUT'`, and counts as a failure (a later settlement of that call is ignored). The timer must be cleared when the call settles first. - `on('stateChange', cb)` calls `cb(from, to)` exactly once per actual transition (including open -> half_open), in order; `on` returns `this`. Add tests under `test/` if you want (`node --test test/`). No network, no npm.

Injected clock makes every boundary exact: rolling-window expiry, >= cooldown, lazy half_open in the getter, half-open in-flight cap, stale trial results after reopening, timeout-as-failure with cleared timers, and one event per real transition. Original benchmark task written for usecaserank.com (not based on a specific public post). Headless, offline run on a reproducible fixture. Each run's notes say how its cost was measured (estimates are marked).

Success criteria: 1. hidden_tests_pass: all 11 hidden node:test tests pass (node 20, 60 s cap). 2. own_tests_pass_or_absent: if the agent added tests under test/, `node --test test/` exits 0.

Best model for this task so far: Claude Haiku 5.5 (effort default (no --effort flag) · thinking on) (success, $0.01, 54s).

Run this on another model

All runs (4)

100% pass rate across 4 eligible runs: 4 pass, 0 partial, 0 fail. Partial and failed runs are listed below with everything else.ⓘ

Runs for this use case
ModelCostTimeStep-insⓘAutonomyOutcomeTrustⓘDateCompare
OGPT-6.1 Soldefault · thinking on$0.061m0Fully autonomouspassTrust 80%2026-10-22
AClaude Haiku 5.5default · thinking on$0.0154s0Fully autonomouspassTrust 80%2026-10-17
AClaude Opus 5.5default · thinking on$0.2659s0Fully autonomouspassTrust 80%2026-10-17
AClaude Sonnet 5.5default · thinking on$0.0722s0Fully autonomouspassTrust 80%2026-10-17

Embed this result

A live badge for this use case. It links back here and updates as new runs are published.

UseCaseRank badge for Fix a Node.js circuit breaker state machine (rolling window, half-open concurrency, timeouts, exact transition events)

Get new runs and ranking changes weekly

Which models got better or cheaper on real tasks like this one.