import { type PolicyRules, type PolicyDecision } from './policy.js'; /** * Multi-Agent Budget Pool — fleet-level budget management. * * Problem: When you have 10 agents sharing a $1000/day budget, you need: * 1. Centralized pool with per-agent allocation * 2. Real-time rebalancing (idle agents release budget to active ones) * 3. Cost-center accounting (who spent what, against which budget line) * 4. Pool-level analytics for finance teams * * This is game-theoretic budget allocation, not just counting. * * Allocation strategies: * - EQUAL: every agent gets pool / N * - WEIGHTED: agents get proportional shares (e.g. Agent A: 40%, Agent B: 60%) * - PRIORITY: high-priority agents can borrow from low-priority ones * * Rebalancing: * When an agent hits its limit, the pool checks if any agent has surplus * (spent < 50% of allocation AND has been idle for > idleThresholdMs). * If so, surplus is redistributed to the requesting agent. This is * cooperative game theory: agents implicitly cooperate by releasing * unused budget. */ /** Configuration for a single agent within a pool. */ export interface PoolAgentConfig { /** Unique identifier for this agent. */ id: string; /** Weight for WEIGHTED allocation (higher = more budget). Default: 1 */ weight?: number; /** Priority level for PRIORITY strategy (higher = can borrow more). Default: 1 */ priority?: number; /** Optional label for cost-center reporting. */ costCenter?: string; /** Per-agent policy overrides (merged with pool default policy). */ policyOverrides?: Partial; } /** Allocation strategy. */ export type AllocationStrategy = 'equal' | 'weighted' | 'priority'; /** Pool configuration. */ export interface BudgetPoolConfig { /** Total pool budget (dollars). */ total: number; /** Allocation strategy. Default: 'equal' */ strategy?: AllocationStrategy; /** Agents in the pool. */ agents: PoolAgentConfig[]; /** How long an agent must be idle before its surplus can be rebalanced (ms). Default: 300000 (5 min) */ idleThresholdMs?: number; /** Minimum surplus ratio before budget can be reclaimed (0-1). Default: 0.5 (agent used < 50%) */ surplusThreshold?: number; /** Default policy rules applied to all agents (can be overridden per-agent). */ defaultPolicy?: PolicyRules; } /** Pool-level analytics for finance reporting. */ export interface PoolAnalytics { /** Total pool budget */ totalBudget: number; /** Total spent across all agents */ totalSpent: number; /** Total remaining across all agents */ totalRemaining: number; /** Pool utilization percentage (0-1) */ utilization: number; /** Per-agent breakdown */ agents: AgentAnalytics[]; /** Spend by cost center */ byCostCenter: Record; /** Number of rebalancing events that occurred */ rebalanceCount: number; } /** Per-agent analytics within the pool. */ export interface AgentAnalytics { id: string; costCenter: string; allocated: number; spent: number; remaining: number; utilization: number; transactionCount: number; lastActivityAt: number; isIdle: boolean; topEndpoints: { url: string; spent: number; }[]; } /** Result of a pool budget check. */ export interface PoolBudgetDecision { allowed: boolean; reason?: string; /** If allowed, which agent's budget is being debited. */ agentId?: string; /** Remaining allocation for this agent after this spend. */ remainingAfter?: number; /** Whether rebalancing was triggered to allow this. */ rebalanced?: boolean; } /** * Multi-agent shared budget pool. * * Usage: * const pool = new BudgetPool({ * total: 1000, * strategy: 'weighted', * agents: [ * { id: 'researcher', weight: 3, costCenter: 'R&D' }, * { id: 'support-bot', weight: 1, costCenter: 'Support' }, * ], * }); * * // Before an agent pays: * const decision = pool.check('researcher', 2.50); * if (decision.allowed) { * // ... execute payment ... * pool.record('researcher', 2.50, 'https://api.data.com/prices'); * } */ export declare class BudgetPool { private agents; private totalBudget; private strategy; private idleThresholdMs; private surplusThreshold; private rebalanceCount; private defaultPolicy; constructor(config: BudgetPoolConfig); /** * Check if an agent can spend a given amount. * If the agent is over budget, attempts rebalancing from idle agents. */ check(agentId: string, amount: number): PoolBudgetDecision; /** * Record a successful spend against an agent's budget. */ record(agentId: string, amount: number, endpoint?: string): void; /** * Get the effective policy for an agent (default + overrides merged). * Overrides take precedence: if an agent specifies maxPerRequest, * it replaces the org default for that field. */ effectivePolicy(agentId: string): PolicyRules; /** * Check a payment against the agent's effective policy (org default + overrides). */ checkPolicy(agentId: string, params: { url: string; amount: number; currency: string; network: string; }): PolicyDecision; /** * Update the default policy for all agents. * Existing per-agent overrides are preserved. */ updateDefaultPolicy(rules: PolicyRules): void; /** * Get pool-level analytics for finance reporting. */ analytics(): PoolAnalytics; /** * Manually add a new agent to the pool with a specific allocation. * Does NOT reallocate existing agents — takes from unallocated pool. */ addAgent(config: PoolAgentConfig, allocation: number): void; /** * Get a specific agent's state. */ agentStatus(agentId: string): AgentAnalytics | null; /** * Try to rebalance budget from idle/surplus agents to a requesting agent. * * This is the cooperative game theory bit: * - Agents don't compete for budget — they implicitly cooperate * - An agent is a "donor" if: (a) it has surplus, (b) it's been idle * - PRIORITY strategy: higher priority agents can borrow from all lower ones * - EQUAL/WEIGHTED: only borrow from agents with surplus > threshold */ private tryRebalance; } //# sourceMappingURL=pool.d.ts.map