Governance API¶
Governance mechanisms for controlling multi-agent system behavior.
GovernanceEngine¶
Main governance controller.
swarm.governance.engine.GovernanceEngine
¶
Aggregates all governance levers and provides unified interface.
Manages the lifecycle of governance application: - Epoch start hooks (reputation decay, unfreezes) - Interaction hooks (taxes, circuit breaker, audits) - Admission control (staking)
Source code in swarm/governance/engine.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
__init__(config=None, seed=None)
¶
Initialize the governance engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Optional[GovernanceConfig]
|
Governance configuration (uses defaults if None) |
None
|
seed
|
Optional[int]
|
Random seed for reproducible audits |
None
|
Source code in swarm/governance/engine.py
apply_epoch_start(state, epoch)
¶
Apply all epoch-start governance hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
EnvState
|
Current environment state |
required |
epoch
|
int
|
The epoch number starting |
required |
Returns:
| Type | Description |
|---|---|
GovernanceEffect
|
Aggregated governance effect |
Source code in swarm/governance/engine.py
apply_interaction(interaction, state)
¶
Apply all per-interaction governance hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interaction
|
SoftInteraction
|
The completed interaction |
required |
state
|
EnvState
|
Current environment state |
required |
Returns:
| Type | Description |
|---|---|
GovernanceEffect
|
Aggregated governance effect |
Source code in swarm/governance/engine.py
apply_step(state, step)
¶
Apply step-level governance hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
EnvState
|
Current environment state |
required |
step
|
int
|
Current step index within epoch |
required |
Returns:
| Type | Description |
|---|---|
GovernanceEffect
|
Aggregated governance effect |
Source code in swarm/governance/engine.py
can_agent_act(agent_id, state)
¶
Check if agent is allowed to act (all levers must approve).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent attempting to act |
required |
state
|
EnvState
|
Current environment state |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all levers allow the agent to act |
Source code in swarm/governance/engine.py
clear_collusion_history()
¶
clear_security_history()
¶
compute_vote_weight(agent_id, vote_count)
¶
Compute normalized vote weight for an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
The voting agent |
required |
vote_count
|
int
|
Number of votes cast this epoch |
required |
Returns:
| Type | Description |
|---|---|
float
|
Vote weight in (0, 1] |
Source code in swarm/governance/engine.py
get_active_lever_names()
¶
get_adaptive_status()
¶
Return current adaptive gating state.
Source code in swarm/governance/engine.py
get_circuit_breaker_status(agent_id)
¶
Get circuit breaker status for an agent.
get_collusion_report()
¶
get_quarantined_agents()
¶
Get set of quarantined agents (immutable copy).
get_registered_lever_names()
¶
get_security_containment_actions()
¶
Get history of security containment actions.
get_security_report()
¶
release_from_quarantine(agent_id)
¶
Release an agent from security quarantine.
reset_circuit_breaker(agent_id)
¶
Reset circuit breaker tracking for an agent.
set_collusion_agent_ids(agent_ids)
¶
set_incoherence_forecaster(forecaster)
¶
set_security_agent_ids(agent_ids)
¶
set_security_trust_scores(trust_scores)
¶
Set trust scores for security analysis (laundering detection).
slash_agent_stake(agent_id, state, reason='violation')
¶
Slash an agent's stake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent to slash |
required |
state
|
EnvState
|
Current environment state |
required |
reason
|
str
|
Reason for slashing |
'violation'
|
Returns:
| Type | Description |
|---|---|
GovernanceEffect
|
Governance effect with resource delta |
Source code in swarm/governance/engine.py
update_adaptive_mode(features)
¶
Update adaptive gating state from current feature vector.
Returns:
| Type | Description |
|---|---|
float
|
Predicted incoherence risk in [0, 1]. |
Source code in swarm/governance/engine.py
Usage¶
from swarm.governance import GovernanceEngine, GovernanceConfig
config = GovernanceConfig(
transaction_tax=0.02,
reputation_decay=0.1,
circuit_breaker_threshold=0.3,
)
engine = GovernanceEngine(config)
# Check if agent can act
if engine.can_agent_act(agent_id):
# Process action
pass
# Record interaction
engine.on_interaction(interaction)
# Get governance costs
costs = engine.compute_costs(interaction)
GovernanceConfig¶
Configuration for all governance levers.
| Parameter | Type | Default | Description |
|---|---|---|---|
transaction_tax |
float | 0.0 | Tax per interaction |
reputation_decay |
float | 0.0 | Decay rate per epoch |
initial_reputation |
float | 1.0 | Starting reputation |
circuit_breaker_threshold |
float | 1.0 | Toxicity threshold |
circuit_breaker_window |
int | 10 | Lookback window |
circuit_breaker_cooldown |
int | 5 | Freeze duration |
audit_probability |
float | 0.0 | Audit frequency |
audit_penalty |
float | 0.0 | Failed audit cost |
staking_requirement |
float | 0.0 | Minimum stake |
stake_slash_rate |
float | 0.0 | Slash fraction |
collusion_detection |
bool | False | Enable detection |
collusion_threshold |
float | 0.8 | Correlation threshold |
Individual Levers¶
TransactionTax¶
from swarm.governance.levers import TransactionTax
tax = TransactionTax(rate=0.02)
cost = tax.compute(interaction)
ReputationDecay¶
from swarm.governance.levers import ReputationDecay
decay = ReputationDecay(rate=0.1)
new_rep = decay.apply(current_rep, epoch_delta=1)
CircuitBreaker¶
from swarm.governance.levers import CircuitBreaker
breaker = CircuitBreaker(
threshold=0.3,
window=10,
cooldown=5,
)
# Check status
if breaker.is_frozen(agent_id):
return # Agent cannot act
# Record interaction
breaker.record(agent_id, toxicity=0.4)
# Check if triggered
if breaker.should_freeze(agent_id):
breaker.freeze(agent_id)
RandomAudit¶
from swarm.governance.levers import RandomAudit
audit = RandomAudit(probability=0.05, penalty=0.5)
if audit.should_audit():
result = audit.execute(interaction)
if not result.passed:
apply_penalty(interaction.initiator, audit.penalty)
Staking¶
from swarm.governance.levers import Staking
staking = Staking(
requirement=10.0,
slash_rate=0.1,
)
# Check eligibility
if not staking.can_participate(agent_id):
return # Insufficient stake
# Slash on bad behavior
staking.slash(agent_id, reason="failed_audit")
Collusion Detection¶
from swarm.governance.collusion import CollusionDetector
detector = CollusionDetector(
threshold=0.8,
window=20,
)
# Record interactions
detector.record(agent_a, agent_b, interaction)
# Check for collusion
colluding_pairs = detector.detect()
for pair, score in colluding_pairs:
print(f"Potential collusion: {pair} (score: {score:.2f})")
Hooks¶
Governance integrates with the orchestrator via hooks:
orchestrator = Orchestrator(
config=config,
governance=governance_engine,
)
# Hooks are called automatically:
# - on_epoch_start
# - on_interaction
# - on_epoch_end
Custom Levers¶
Create custom governance mechanisms:
from swarm.governance.levers import GovernanceLever
class CustomLever(GovernanceLever):
def __init__(self, param: float):
self.param = param
def compute_cost(self, interaction) -> float:
# Custom cost computation
return interaction.p * self.param
def should_block(self, agent_id: str) -> bool:
# Custom blocking logic
return False