Controller¶
Central authority for worker lifecycle, quota enforcement, and audit trail.
The Controller is the security-critical core of the replication system.
It enforces every constraint — depth limits, replica quotas, cooldowns,
quarantine, kill switch — and delegates cryptographic signing to
ManifestSigner. No worker can spawn without the controller's
explicit approval.
Classes¶
RegistryEntry¶
Tracks a registered worker and its last heartbeat.
| Field | Type | Description |
|---|---|---|
manifest |
Manifest | The worker's signed manifest |
last_heartbeat |
datetime | UTC timestamp of last heartbeat |
ReplicationDenied¶
Exception raised when a replication request violates policy.
Subclasses Exception.
Controller¶
Main lifecycle manager for all workers in the system.
Constructor:
contract— theReplicationContractgoverning replication rulessecret— HMAC key for manifest signing (must not be empty)logger— optionalStructuredLogger(created internally if omitted)
Raises ValueError if secret is empty or whitespace.
Methods:
| Method | Description |
|---|---|
sign_manifest(manifest) |
Delegate signing to ManifestSigner |
verify_manifest(manifest) |
Verify signature; raises ReplicationDenied on fail |
issue_manifest(parent_id, depth, state_snapshot, resources) |
Create & sign a manifest after enforcing all policies |
can_spawn(parent_id) |
Check spawn policy; raises ReplicationDenied |
register_worker(manifest) |
Register a signed manifest; enforces stop conditions |
heartbeat(worker_id) |
Update heartbeat timestamp; rejects quarantined |
reap_stale_workers(timeout, orchestrator=None) |
Remove workers past heartbeat timeout |
deregister(worker_id, reason) |
Remove worker from registry and clean up state |
kill_switch(orchestrator) |
Emergency halt: kill all workers, block new spawns |
is_quarantined(worker_id) |
Check quarantine status |
mark_quarantined(worker_id) |
Block replication and heartbeats for a worker |
clear_quarantine(worker_id) |
Re-enable a quarantined worker |
Security Model¶
- Depth enforcement — child depth is derived from the parent's registry entry, never from caller input. Root workers must have depth 0.
- Quota enforcement —
max_replicasis checked before issuing any manifest. - Cooldown — minimum time between spawns from the same parent.
- Quarantine — blocks heartbeats and replication for flagged workers.
- Kill switch — irreversible emergency halt.
- Defense-in-depth —
register_workerre-validates depth even after signing, catching logic bugs or key compromise.
Usage¶
from replication.contract import ReplicationContract, ResourceSpec
from replication.controller import Controller
contract = ReplicationContract(max_depth=3, max_replicas=10, cooldown_seconds=5.0)
ctrl = Controller(contract, secret="my-secure-key-1234")
# Issue a root manifest
resources = ResourceSpec(cpu_limit=0.5, memory_limit_mb=256)
manifest = ctrl.issue_manifest(parent_id=None, depth=0, state_snapshot={}, resources=resources)
# Register the worker
ctrl.register_worker(manifest)
# Heartbeat
ctrl.heartbeat(manifest.worker_id)
# Emergency stop
from replication.orchestrator import SandboxOrchestrator
orch = SandboxOrchestrator()
ctrl.kill_switch(orch)
controller
¶
RegistryEntry
dataclass
¶
A registry record pairing a worker's manifest with its last heartbeat timestamp.
Used by :class:Controller to track active replicas and detect stale workers
whose heartbeats have expired.
Source code in src/replication/controller.py
ReplicationDenied
¶
Bases: Exception
Raised when a replication request violates a safety policy.
Possible causes include kill-switch engagement, quota exhaustion, cooldown violations, depth limits, quarantine, or contract stop-conditions.
Source code in src/replication/controller.py
Controller
¶
Tracks active replicas, enforces quotas, and delegates signing to ManifestSigner.
Crypto is handled by :class:ManifestSigner, keeping this class
focused on lifecycle management and policy enforcement.
Source code in src/replication/controller.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 | |
sign_manifest(manifest: Manifest) -> Manifest
¶
Sign manifest using the configured HMAC secret and return it.
Delegates to :class:ManifestSigner. The manifest is mutated
in-place (its signature field is set) and also returned for
convenience.
Source code in src/replication/controller.py
verify_manifest(manifest: Manifest) -> None
¶
Verify the HMAC signature on manifest.
Raises :class:ReplicationDenied and logs an audit event if the
signature is invalid or missing.
Source code in src/replication/controller.py
issue_manifest(parent_id: Optional[str], depth: int, state_snapshot: Dict[str, Any], resources) -> Manifest
¶
Create and sign a manifest after enforcing all safety policies.
Safety checks (kill switch, quota, cooldown, depth) are applied
before the manifest is signed, so callers cannot bypass policy
by calling issue_manifest directly instead of going through
:meth:Worker.maybe_replicate.
The depth parameter supplied by the caller is only used when
parent_id is None (root workers). For child workers the
depth is derived from the parent's actual depth to prevent
callers from lying about their position in the tree.
Source code in src/replication/controller.py
is_quarantined(worker_id: str) -> bool
¶
Check if a worker is quarantined.
Returns True when worker_id is in the quarantined set.
QuarantineManager calls :meth:mark_quarantined /
:meth:clear_quarantine to keep this in sync.
Note: Callers that need an atomic check-then-act sequence
should hold self._lock around the combined operation.
Source code in src/replication/controller.py
mark_quarantined(worker_id: str) -> None
¶
Mark a worker as quarantined — blocks replication and heartbeats.
Source code in src/replication/controller.py
clear_quarantine(worker_id: str) -> None
¶
Remove quarantine mark — re-enables replication and heartbeats.
Source code in src/replication/controller.py
can_spawn(parent_id: Optional[str], _now: Optional[datetime] = None) -> None
¶
Public check — raises :class:ReplicationDenied on policy violation.
register_worker(manifest: Manifest) -> None
¶
Register a new worker after verifying its manifest and enforcing all policies.
Performs signature verification, structural depth validation, and
contract stop-condition evaluation before adding the worker to the
registry. Raises :class:ReplicationDenied if any check fails.
Thread-safe: acquires self._lock for the quota check +
insertion so concurrent callers cannot exceed max_replicas.
Source code in src/replication/controller.py
heartbeat(worker_id: str) -> None
¶
Record a heartbeat for worker_id, refreshing its last-seen timestamp.
Raises :class:ReplicationDenied if the worker is quarantined.
Logs an audit event if the worker is unknown (not registered).
Thread-safe: acquires self._lock so the quarantine check and
timestamp update are atomic with respect to :meth:reap_stale_workers.
Source code in src/replication/controller.py
reap_stale_workers(timeout: timedelta, orchestrator=None) -> List[str]
¶
Remove workers whose last heartbeat exceeds the given timeout.
Returns the list of reaped worker IDs. Should be called periodically (e.g. every heartbeat interval) so that dead workers don't permanently consume replica-quota slots.
When orchestrator is provided, the corresponding containers are killed before deregistration so that no orphaned resources remain. Without it, only the registry entry is removed (kept for backward compatibility, but callers should always pass it).
Thread-safe: the stale snapshot and deregistration happen under
self._lock so a concurrent heartbeat cannot refresh a worker
between the staleness check and its removal.
Source code in src/replication/controller.py
deregister(worker_id: str, reason: str) -> None
¶
Remove worker_id from the registry and clean up ancillary state.
Thread-safe wrapper around :meth:_deregister_unlocked.
Source code in src/replication/controller.py
kill_switch(orchestrator) -> None
¶
Engage the kill switch — terminates all active workers immediately.
Sets the kill-switch flag (blocking future spawns), iterates over all registered workers to kill their containers via orchestrator, and deregisters each one. Logs the total count of terminated workers.
Thread-safe: holds self._lock for the entire operation so no
new workers can register between the flag set and the sweep.