Skip to content

feat(agent): add circuit breaker (#345) - #416

Merged
Abidoyesimze merged 2 commits into
Neurowealth:mainfrom
BernardOnuh:feat/345-agent-circuit-breaker
Sep 2, 2026
Merged

feat(agent): add circuit breaker (#345)#416
Abidoyesimze merged 2 commits into
Neurowealth:mainfrom
BernardOnuh:feat/345-agent-circuit-breaker

Conversation

@BernardOnuh

Copy link
Copy Markdown
Contributor

Summary

Implements the Agent Circuit Breaker (#345): halts agent-initiated rebalancing when the market, a protocol, or a user shows risk. Withdrawals are untouched (acceptance criterion).

Changes

  • Pure trip rules (src/agent/breakerRules.ts): abnormal-loss drawdown, stablecoin de-peg, rebalance oscillation, stale APY — all deterministic, DB/clock-free.
  • Pure state machine (src/agent/breakerState.ts): CLOSED → OPEN → HALF_OPEN → CLOSED with cooldown backoff and manual trip/reset.
  • Integration (src/agent/breakerService.ts): measurement loads, per-tick evaluation, fail-closed halting, operator alerts, user events, sync status surfaces.
  • Loop wiring: GLOBAL > PROTOCOL > USER blocking precedent, BLOCKED decision records, blockedProtocols target exclusion, GET /agent/status + per-user breaker status.
  • Admin endpoints: list / manual trip / reset with audit logging (requireAdminScope('agent')).
  • Observability: agent_breaker_state gauge + agent_breaker_trips_total counter, Prometheus alert rules, RUNBOOK + OBSERVABILITY docs.

De-peg feed gap (documented, see RUNBOOK §9)

The de-peg rule is a pure consumer of a stablecoin spot price. No live feed exists in this codebase yet (the fee oracle publishes fees only; routing.ts is a stub), so getStablecoinPrice() returns null (fails safe — never trips) and the rule is off by default (BREAKER_DEPEG_ENABLED=false). getStablecoinPrice() is the single integration point to wire a feed later.

Verification

  • pnpm/npm: typecheck, lint, prettier all pass
  • 1695 tests pass, incl. 5 new suites: rules, state machine, service status/privacy, loop blocking, target exclusion
  • Privacy: per-user breaker status never leaks another user's/protocol's breaker
  • Replay/determinism: pure modules kept DB/clock free

Closes #345

Add an agent circuit breaker that halts agent-initiated rebalancing when
the market, a protocol, or a user shows risk. Withdrawals are untouched.

- Pure trip rules (breakerRules.ts): abnormal-loss drawdown, stablecoin
  de-peg (fails safe on a missing feed), rebalance oscillation, stale APY.
- Pure CLOSED/OPEN/HALF_OPEN state machine (breakerState.ts) with cooldown
  backoff and manual trip/reset.
- Integration (breakerService.ts): measurement loads, per-tick evaluation,
  fail-closed halting, operator alerts, user events, status surfaces.
- Loop wiring: GLOBAL > PROTOCOL > USER blocking, BLOCKED decisions,
  blockedProtocols target exclusion, agent status + per-user status route.
- Admin endpoints: list/trip/reset breakers with audit logging.
- Prometheus gauge + counters and alert rules; RUNBOOK + OBSERVABILITY docs.
- Tests: 5 new suites (rules, state machine, service status/privacy,
  loop blocking, target exclusion) + harness update.

De-peg is a pure rule consuming a price input; no live feed exists yet so
getStablecoinPrice() returns null (fails safe) and the rule stays off by
default (BREAKER_DEPEG_ENABLED=false).
@drips-wave

drips-wave Bot commented Sep 1, 2026

Copy link
Copy Markdown

@BernardOnuh Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Abidoyesimze
Abidoyesimze merged commit e844b75 into Neurowealth:main Sep 2, 2026
8 checks passed
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.

Agent Circuit Breaker on Abnormal Loss, De-Peg & Oscillation

3 participants