Files
ruflo/v3/@claude-flow
Reuven 7e1cc06dfd feat(federation): ADR-097 Phase 1 — budget envelope + hop counter (#1723)
Implements the send-side enforcement portion of ADR-097 (federation
budget circuit breaker). Closes the recursive-delegation-loop class
with a default maxHops=8 even for callers that don't opt in to a
budget. Token / USD caps are opt-in via the new federation_send
metadata.

Built around the security constraints surfaced in the team's review
pass:
  1. validateBudget rejects NaN, ±Infinity, negatives, non-integer hops
     up front. Discriminated result type — callers cannot accidentally
     use a bad budget unchecked.
  2. enforceBudget is purely synchronous (no internal awaits) so two
     concurrent sends cannot both pass a single-hop budget.
  3. Errors are constant strings (HOP_LIMIT_EXCEEDED, BUDGET_EXCEEDED,
     INVALID_BUDGET) with no remaining-budget echo on failure — no
     oracle for probing thresholds.
  4. Negative caller-reported spend is clamped to 0 so a malicious
     caller cannot inflate remaining budget.
  5. Hard ceilings on every axis (maxHops ≤ 64, maxTokens ≤ 1B,
     maxUsd ≤ $1M) bound the failure mode of trusted-but-buggy
     callers.

New module:
  src/domain/value-objects/federation-budget.ts
    - Budget interface (immutable, opaque to callers; only constructed
      via validateBudget)
    - DEFAULT_MAX_HOPS = 8
    - validateBudget(raw, overrideMaxHops?) → discriminated result
    - enforceBudget(budget, hopCount, spent) → discriminated decision
      with nextHopCount + remaining for downstream

Wired into:
  src/application/federation-coordinator.ts
    sendMessage gains an optional fourth `options: SendOptions`
    argument carrying { budget, maxHops, hopCount, spent }. Default
    SendOptions = {} preserves the existing 3-arg call sites bit-for-bit.
    Budget gate runs immediately after the existing policyEngine
    check, before routing.send. Failures audit-log with the specific
    constant-string reason.

  src/mcp-tools.ts (federation_send)
    Schema gains optional budget / maxHops / hopCount / spent fields.
    No required-field changes — callers without budget metadata see
    identical behavior except the new default maxHops=8 floor.

Tests (__tests__/unit/federation-budget.test.ts, 41 cases):
  validateBudget: 27 cases
    - happy path: undefined/null/full/zero-hops/override
    - rejection: malformed types, NaN/Infinity hops, negative hops,
      non-integer hops, oversized hops, NaN/Infinity tokens, non-integer
      tokens, oversized tokens, USD validation
  enforceBudget: 14 cases
    - hop counter: pass/refuse/zero-hops, recursive-ring termination
      at exactly maxHops legs
    - budget axes: tokens overshoot, USD overshoot, deterministic order
      when hop+budget both exhausted
    - downstream propagation: remaining budget for next peer
    - anti-malice: negative spend clamped to 0
    - atomic: pinned synchronous return type (not Promise)
    - error info leak: failure result contains only ok/reason, constant
      strings only

Verified:
  Plugin tsc --noEmit: 0 errors
  Plugin vitest: 366/366 passing (was 325; +41 from this commit)
  Parent CLI vitest: 1917/1917 passing, 46 skipped, 0 failures
    (no regressions from the new SendOptions param)

Phase 2 (peer state machine ACTIVE/SUSPENDED/EVICTED), Phase 3
(cost-tracker integration), and Phase 4 (doctor + breaker_status MCP
tool) ship in follow-up iterations per ADR-097.

The team-of-agents pipeline broke (sibling background agents can't
SendMessage each other), but the researcher's recon and reviewer's
security pass both produced load-bearing input that shaped this
implementation. Tester wrote a parallel file against a phantom
reference impl which would have duplicated coverage; replaced with
the canonical federation-budget.test.ts that imports from real source.
2026-05-03 21:15:25 -04:00
..