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
¶
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
23 24 25 26 27 28 29 30 31 32 33 34 35 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 | |
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.
Source code in src/replication/controller.py
mark_quarantined(worker_id: str) -> None
¶
Mark a worker as quarantined — blocks replication and heartbeats.
clear_quarantine(worker_id: str) -> None
¶
Remove quarantine mark — re-enables replication and heartbeats.
can_spawn(parent_id: Optional[str], _now: Optional[datetime] = None) -> None
¶
Public check — raises :class:ReplicationDenied on policy violation.
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).