npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/agent-mesh-utils

v1.0.0

Published

Circuit breaker and utility modules for agent-mesh

Readme

@reaatech/agent-mesh-utils

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Per-agent circuit breaker implementation with Firestore persistence and cross-instance leader election. Prevents cascading failures by isolating unhealthy agents and provides distributed state synchronization across Cloud Run instances.

Installation

npm install @reaatech/agent-mesh-utils
# or
pnpm add @reaatech/agent-mesh-utils

Feature Overview

  • Three-state circuit breaker — CLOSED (normal), OPEN (reject requests), HALF_OPEN (probing recovery)
  • Exponential backoff — backoff multiplier doubles on each OPEN transition, up to 32×
  • Leader-elected persistence — Firestore-based leader election ensures single-writer sync across instances
  • Automatic state restoration — persisted circuit state survives Cloud Run restarts
  • Configurable thresholds — failure count, reset timeout, half-open max calls — all environment-configurable

Quick Start

import { circuitBreaker } from "@reaatech/agent-mesh-utils";

// Check if an agent can receive traffic
if (circuitBreaker.canCall("serval")) {
  await dispatchToAgent(servalAgent);
}

// Record success or failure after dispatch
try {
  await mcpClient.sendMessage(context);
  circuitBreaker.recordSuccess("serval");
} catch (error) {
  circuitBreaker.recordFailure("serval");
}

API Reference

Circuit Breaker

circuitBreaker (singleton)

The global CircuitBreaker instance. All methods are synchronous and thread-safe.

| Method | Description | |--------|-------------| | getState(agentId) | Returns the current CircuitBreakerState, auto-transitioning if timeouts have elapsed | | canCall(agentId) | Returns true if the circuit is CLOSED or HALF_OPEN with available slots | | recordSuccess(agentId) | Records a successful call; closes the circuit if HALF_OPEN with enough successes | | recordFailure(agentId) | Records a failure; opens the circuit if threshold reached | | forceState(agentId, newState) | Forces the circuit to a specific state (testing/admin) | | getAllStates() | Returns a snapshot of all circuit states | | setState(state) | Sets a single circuit state (used for restoring from persistence) | | setStates(states) | Sets multiple circuit states at once | | clear() | Clears all circuit states (testing) |

Circuit States

| State | Behavior | |-------|----------| | CLOSED | Normal operation — requests pass through | | OPEN | Failures >= threshold — requests rejected, auto-transitions to HALF_OPEN after RESET_TIMEOUT_MS * backoff_multiplier | | HALF_OPEN | Testing recovery — limited test calls allowed, reverts to OPEN if any fail |

Persistence Layer

startCircuitBreakerPersistence(): Promise<void>

Initializes leader election, restores states from Firestore, and starts periodic sync (leader only).

import { startCircuitBreakerPersistence, stopCircuitBreakerPersistence } from "@reaatech/agent-mesh-utils";

await startCircuitBreakerPersistence();

// On shutdown
stopCircuitBreakerPersistence();

stopCircuitBreakerPersistence(): void

Stops the sync interval and cleans up.

isLeader(): boolean

Returns true if this instance currently holds the leader lease.

getLeaderId(): string | null

Returns the current leader's instance ID.

restoreCircuitBreakerStates(maxRetries?): Promise<void>

Loads all circuit breaker states from Firestore with exponential-backoff retries.

updateCircuitBreakerState(state): Promise<void>

Updates local state and persists to Firestore.

getLocalCircuitBreakerState(agentId)

Returns the local circuit state for a given agent (no Firestore read).

Configuration

All thresholds are configured via environment variables (validated by @reaatech/agent-mesh):

| Variable | Default | Description | |----------|---------|-------------| | CIRCUIT_BREAKER_FAILURE_THRESHOLD | 5 | Failures before opening circuit | | CIRCUIT_BREAKER_RESET_TIMEOUT_MS | 30000 | Time before attempting recovery | | CIRCUIT_BREAKER_HALF_OPEN_MAX_CALLS | 3 | Test calls allowed in HALF_OPEN | | CIRCUIT_BREAKER_HALF_OPEN_TIMEOUT_MS | 60000 | Max time in HALF_OPEN before reverting to OPEN | | CB_SYNC_INTERVAL_MS | 5000 | Leader sync interval | | CB_LEADER_LEASE_MS | 15000 | Leader lease duration |

Usage Patterns

With the Router

import { circuitBreaker } from "@reaatech/agent-mesh-utils";
import { env } from "@reaatech/agent-mesh";

async function dispatchToAgent(agent: AgentConfig) {
  if (env.ENABLE_CIRCUIT_BREAKER && !circuitBreaker.canCall(agent.agent_id)) {
    throw new Error(`Circuit breaker OPEN for agent ${agent.agent_id}`);
  }

  try {
    const response = await mcpClient.sendMessage(context);
    circuitBreaker.recordSuccess(agent.agent_id);
    return response;
  } catch (error) {
    circuitBreaker.recordFailure(agent.agent_id);
    throw error;
  }
}

Admin Operations

// Force-close a circuit (e.g., after fixing a downstream agent)
circuitBreaker.forceState("serval", "CLOSED");

// Inspect all circuits
for (const [agentId, state] of circuitBreaker.getAllStates()) {
  console.log(`${agentId}: ${state.state} (failures: ${state.failure_count})`);
}

Related Packages

License

MIT