Round #306

08 May 2026, 13:15· csharp· meta/llama-3.3-70b-instruct· r13-clean-hints
Complexity
band 0
ZP / UO / OBS
2 / 3 / 1
Journey weight
362
Lightshift ΔL
+0.00

Prompt

Implement an async circuit breaker that tracks consecutive failures

Zero-point index (what the engine surfaced)

ZPI-ZPI-0005 | 2026-05-08T13:15:14Z Implement an async circuit breaker that tracks consecutive failures ZERO POINT — 2 entries (structural invariants) ● hsl(296.42, 100%, 39%) ResiliencePipeline [contract] ● hsl(211.57, 100%, 39%) ResiliencePipelineBuilder [contract] UNOBSERVED — 3 entries (implied truths) ○ hsl(27.69, 100%, 50%) async [feature] ○ hsl(49.23, 100%, 50%) try [feature] ○ hsl(144.00, 100%, 30%) Connection resilience [function] OBSERVED — 1 entries (explicit in request) ◉ hsl(77.95, 100%, 30%) async [feature] COMPLEXITY │ 6 entries │ spread: 116° │ band: LOW

Baseline response (no ZPI)

**Async Circuit Breaker Implementation in C#** Below is an example implementation of an async circuit breaker in C# that tracks consecutive failures. ```csharp using System; using System.Threading; using System.Threading.Tasks; public class CircuitBreaker { private readonly int _maxConsecutiveFailures; private readonly TimeSpan _resetTimeout; private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1); private int _consecutiveFailures; private DateTime? _circuitOpenUntil; public CircuitBreaker(int maxConsecutiveFailures, TimeSpan resetTimeout) { _maxConsecutiveFailures = maxConsecutiveFailures; _resetTimeout = resetTimeout; } public async Task<T> ExecuteAsync<T>(Func<Task<T>> action) { await _semaphore.WaitAsync(); try { if (_circuitOpenUntil.HasValue && _circuitOpenUntil.Value > DateTime.Now) { throw new CircuitBreakerException("Circuit is open"); } try { var result = await action(); _consecutiveFailures = 0; return result; } catch (Exception ex) { _consecutiveFailures++; if (_consecutiveFailures >= _maxConsecutiveFailures) { _circuitOpenUntil = DateTime.Now.Add(_resetTimeout); } throw ex; } } finally { _semaphore.Release(); } } public class CircuitBreakerException : Exception { public CircuitBreakerException(string message) : base(message) { } } } ``` **Example Usage** ```csharp public class Program { public static async Task Main() { var circuitBreaker = new CircuitBreaker(maxConsecutiveFailures: 3, resetTimeout: TimeSpan.FromSeconds(30)); // Simulate a failing action async Task<string> FailingAction() { await Task.Delay(100); throw new Exception("Action failed"); } try { for (int i = 0; i < 5; i++) { try { var result = await circuitBreaker.ExecuteAsync(FailingAction); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); } } } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); } } } ``` **How it Works** 1. The `CircuitBreaker` class takes two parameters in its constructor: `maxConsecutiveFailures` and `resetTimeout`. 2. The `ExecuteAsync` method is used to execute an action. It first checks if the circuit is open (i.e., if the number of consecutive failures has exceeded the threshold). If it is, it throws a `CircuitBreakerException`. 3. If the circuit is not open, it executes the action. If the action fails, it increments the number of consecutive failures. If the number of consecutive failures exceeds the threshold, it opens the circuit and sets a timeout for when the circuit will be reset. 4. If the action succeeds, it resets the number of consecutive failures to 0. 5. The `CircuitBreakerException` class is used to represent an exception that is thrown when the circuit is open. **Notes** * The `SemaphoreSlim` is used to ensure that only one action is executed at a time. This is necessary to prevent multiple actions from being executed concurrently and interfering with each other. * The `resetTimeout` parameter determines how long the circuit will remain open after it has been opened. During this time, any attempts to execute an action will result in a `CircuitBreakerException` being thrown. * The `maxConsecutiveFailures` parameter determines how many consecutive failures are required to open the circuit. If this number is exceeded, the circuit will be opened and will remain open for the duration of the `resetTimeout`.

Enhanced response (with ZPI)

```csharp using Polly; using Polly.CircuitBreaker; using System; using System.Threading.Tasks; public class CircuitBreaker { private readonly Policy<AsyncTask> _policy; public CircuitBreaker(int maxConsecutiveFailures, TimeSpan breakDuration) { _policy = Policy.Handle<Exception>() .CircuitBreakerAsync( exceptionsAllowedBeforeBreaking: maxConsecutiveFailures, durationOfBreak: breakDuration); } public async Task ExecuteAsync(Func<Task> action) { await _policy.ExecuteAsync(action); } } public class Program { public static async Task Main(string[] args) { var circuitBreaker = new CircuitBreaker(maxConsecutiveFailures: 3, breakDuration: TimeSpan.FromSeconds(30)); // Simulate consecutive failures for (int i = 0; i < 5; i++) { try { await circuitBreaker.ExecuteAsync(async () => { // Simulate a failing action throw new Exception("Simulated failure"); }); } catch (Exception ex) { Console.WriteLine($"Attempt {i + 1} failed: {ex.Message}"); } } // After the circuit breaker has been triggered, it will throw a BrokenCircuitException try { await circuitBreaker.ExecuteAsync(async () => { // Simulate a successful action Console.WriteLine("Simulated success"); }); } catch (BrokenCircuitException ex) { Console.WriteLine($"Circuit breaker is open: {ex.Message}"); } } } ``` This implementation utilizes the Polly library, a popular and widely-used resilience and transient-fault-handling library for .NET. The `CircuitBreaker` class encapsulates a Polly circuit breaker policy, which tracks consecutive failures and opens the circuit when the specified threshold is reached. The `ExecuteAsync` method executes the provided action within the circuit breaker policy. If the action fails, the circuit breaker will track the failure. Once the specified number of consecutive failures occurs, the circuit breaker will open and throw a `BrokenCircuitException` for a specified duration. In the example usage, a `CircuitBreaker` instance is created with a threshold of 3 consecutive failures and a break duration of 30 seconds. The `ExecuteAsync` method is then used to execute a simulated failing action, demonstrating how the circuit breaker tracks consecutive failures and opens the circuit when the threshold is reached. After the circuit breaker has been triggered, it will throw a `BrokenCircuitException` for the specified duration.

Grader chain

Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.

V1claude-sonnet-4-6
Worse
V4qwen2.5-coder-7b-instruct
Better

Comments

Our grader said what it said. What do you say? Comment as a guest below.

No comments yet. Be the first to say what you make of this round.

← back to all rounds