Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ debug = "line-tables-only"
# One copy, not two. Code that lives in core is used from core directly,
# never re-exported through a local shim — the envelope layout in
# tw-crypto already drifted by 61 lines once, while two copies existed.
tw-crypto = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-types = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-upstream = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-wire = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.38.0" }
tw-breaker = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-crypto = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-types = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-upstream = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }
tw-wire = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.40.0" }

# Web framework
axum = { version = "0.8", features = ["macros", "ws"] }
Expand Down Expand Up @@ -150,3 +151,4 @@ think-watch-auth = { path = "crates/auth" }
think-watch-gateway = { path = "crates/gateway" }
think-watch-mcp-gateway = { path = "crates/mcp-gateway" }


1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
tw-breaker = { workspace = true }
tw-guard = { workspace = true }
tw-crypto = { workspace = true }
axum = { workspace = true }
Expand Down
77 changes: 37 additions & 40 deletions crates/common/src/cb_registry.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
//! Process-wide circuit-breaker state registry.
//!
//! Both the AI gateway (`think-watch-gateway`) and the MCP gateway
//! (`think-watch-mcp-gateway`) write into this shared map every time a
//! circuit transitions. The dashboard handler in `think-watch-server` reads
//! a snapshot to render real-time CB state in the upstream-health panel.
//! The MCP gateway (`think-watch-mcp-gateway`) writes into this map every
//! time one of its in-process breakers transitions, and the Open listener
//! turns an opening into an audit event. The dashboard handler reads a
//! snapshot to render MCP rows.
//!
//! Living in `think-watch-common` keeps the two gateways decoupled while
//! still letting them share a single global view.
//! AI routes are not here: their breakers live in Redis, shared by every
//! replica, and the dashboard reads them from there
//! (`HealthTracker::state`). A process-local map would show each replica
//! only its own view.

use std::collections::HashMap;
use std::sync::OnceLock;
use std::sync::RwLock;

/// Public, stable representation of a circuit-breaker state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CbState {
Closed,
HalfOpen,
Open,
}
/// A breaker's state: thinkwatch-core's, the state machine every breaker
/// here runs.
use tw_breaker::State;

impl CbState {
pub fn as_str(&self) -> &'static str {
match self {
CbState::Closed => "Closed",
CbState::HalfOpen => "HalfOpen",
CbState::Open => "Open",
}
/// How the dashboard spells a state (`Closed` / `HalfOpen` / `Open`).
pub fn label(state: State) -> &'static str {
match state {
State::Closed => "Closed",
State::HalfOpen => "HalfOpen",
State::Open => "Open",
}
}

static CB_REGISTRY: OnceLock<RwLock<HashMap<String, CbState>>> = OnceLock::new();
static CB_REGISTRY: OnceLock<RwLock<HashMap<String, State>>> = OnceLock::new();

/// Signature of the Open-transition listener. Named so the static's
/// type declaration stays short and clippy::type_complexity happy.
Expand All @@ -43,7 +40,7 @@ pub type OpenListener = Box<dyn Fn(&str, &str) + Send + Sync>;
/// breakers so downstream subscribers can distinguish them.
static OPEN_LISTENER: OnceLock<OpenListener> = OnceLock::new();

fn cb_registry() -> &'static RwLock<HashMap<String, CbState>> {
fn cb_registry() -> &'static RwLock<HashMap<String, State>> {
CB_REGISTRY.get_or_init(|| RwLock::new(HashMap::new()))
}

Expand All @@ -63,22 +60,22 @@ where
/// either "ai" or "mcp" — used by the Open listener to tag emitted
/// audit events. When the transition is Closed→Open or HalfOpen→Open
/// the listener (if installed) fires once.
pub fn record_cb_with_kind(key: &str, state: CbState, kind: &str) {
pub fn record_cb_with_kind(key: &str, state: State, kind: &str) {
let prev = if let Ok(mut m) = cb_registry().write() {
m.insert(key.to_string(), state)
} else {
None
};
if state == CbState::Open
&& prev != Some(CbState::Open)
if state == State::Open
&& prev != Some(State::Open)
&& let Some(listener) = OPEN_LISTENER.get()
{
listener(key, kind);
}
}

/// Snapshot the current state of every key the registry has seen.
pub fn snapshot_cb_states() -> HashMap<String, CbState> {
pub fn snapshot_cb_states() -> HashMap<String, State> {
cb_registry().read().map(|m| m.clone()).unwrap_or_default()
}

Expand Down Expand Up @@ -127,10 +124,10 @@ mod tests {
let _g = serial_lock().lock().unwrap();
let _ = drain_captured(); // discard residue from earlier tests

record_cb_with_kind("test-fires-once", CbState::Closed, "ai");
record_cb_with_kind("test-fires-once", CbState::Open, "ai");
record_cb_with_kind("test-fires-once", CbState::Open, "ai");
record_cb_with_kind("test-fires-once", CbState::Open, "ai");
record_cb_with_kind("test-fires-once", State::Closed, "ai");
record_cb_with_kind("test-fires-once", State::Open, "ai");
record_cb_with_kind("test-fires-once", State::Open, "ai");
record_cb_with_kind("test-fires-once", State::Open, "ai");

let calls = drain_captured();
let calls_for_key: Vec<_> = calls
Expand All @@ -150,9 +147,9 @@ mod tests {
let _g = serial_lock().lock().unwrap();
let _ = drain_captured();

record_cb_with_kind("test-re-fires", CbState::Open, "mcp");
record_cb_with_kind("test-re-fires", CbState::Closed, "mcp");
record_cb_with_kind("test-re-fires", CbState::Open, "mcp");
record_cb_with_kind("test-re-fires", State::Open, "mcp");
record_cb_with_kind("test-re-fires", State::Closed, "mcp");
record_cb_with_kind("test-re-fires", State::Open, "mcp");

let calls = drain_captured();
let calls_for_key: Vec<_> = calls.iter().filter(|(k, _)| k == "test-re-fires").collect();
Expand All @@ -169,8 +166,8 @@ mod tests {
let _g = serial_lock().lock().unwrap();
let _ = drain_captured();

record_cb_with_kind("test-no-half-open", CbState::Closed, "ai");
record_cb_with_kind("test-no-half-open", CbState::HalfOpen, "ai");
record_cb_with_kind("test-no-half-open", State::Closed, "ai");
record_cb_with_kind("test-no-half-open", State::HalfOpen, "ai");

let calls = drain_captured();
assert!(
Expand All @@ -183,12 +180,12 @@ mod tests {
fn snapshot_reflects_latest_state_for_each_key() {
let _g = serial_lock().lock().unwrap();

record_cb_with_kind("snapshot-test-1", CbState::Closed, "ai");
record_cb_with_kind("snapshot-test-2", CbState::Open, "mcp");
record_cb_with_kind("snapshot-test-1", CbState::HalfOpen, "ai");
record_cb_with_kind("snapshot-test-1", State::Closed, "ai");
record_cb_with_kind("snapshot-test-2", State::Open, "mcp");
record_cb_with_kind("snapshot-test-1", State::HalfOpen, "ai");

let snap = snapshot_cb_states();
assert_eq!(snap.get("snapshot-test-1"), Some(&CbState::HalfOpen));
assert_eq!(snap.get("snapshot-test-2"), Some(&CbState::Open));
assert_eq!(snap.get("snapshot-test-1"), Some(&State::HalfOpen));
assert_eq!(snap.get("snapshot-test-2"), Some(&State::Open));
}
}
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
tw-breaker = { workspace = true }
tw-guard = { workspace = true }
tw-types = { workspace = true }
tw-dialect = { workspace = true }
Expand Down
Loading