forked from coredipper/operon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_governed_release_train.py
More file actions
184 lines (152 loc) · 5.31 KB
/
24_governed_release_train.py
File metadata and controls
184 lines (152 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/usr/bin/env python3
"""
Example 24: Governed Release Train
==================================
Demonstrates a release controller that combines:
- QuorumSensing for multi-agent consensus
- CoherentFeedForwardLoop for two-key gating
- NegativeFeedbackLoop to adjust strictness based on error rate
- CoordinationSystem for resource-aware execution
- Membrane filtering and Lysosome cleanup
Run:
python examples/24_governed_release_train.py
"""
from __future__ import annotations
from operon_ai import (
ATP_Store,
CoherentFeedForwardLoop,
GateLogic,
Lysosome,
Membrane,
NegativeFeedbackLoop,
QuorumSensing,
Signal,
ThreatLevel,
VotingStrategy,
Waste,
WasteType,
)
from operon_ai.coordination import CoordinationSystem
def clamp(value: float, min_value: float, max_value: float) -> float:
return max(min_value, min(max_value, value))
def run_release_step(step_name: str, release: dict) -> dict:
prompt = release["prompt"].lower()
if step_name == "deploy" and "refactor" in prompt:
raise RuntimeError("Canary failed")
if step_name == "monitor" and release["observed_error_rate"] > 0.15:
return {"status": "alert", "reason": "error rate high"}
return {"status": "ok", "step": step_name, "release_id": release["id"]}
def validate_step(result: dict) -> bool:
return isinstance(result, dict) and result.get("status") == "ok"
def main() -> None:
print("=" * 70)
print("Governed Release Train")
print("=" * 70)
budget = ATP_Store(budget=300, silent=True)
membrane = Membrane(threshold=ThreatLevel.DANGEROUS, silent=True)
quorum = QuorumSensing(
n_agents=5,
budget=budget,
strategy=VotingStrategy.MAJORITY,
threshold=0.6,
silent=True,
)
cffl = CoherentFeedForwardLoop(
budget=budget,
gate_logic=GateLogic.AND,
enable_cache=False,
silent=True,
)
feedback = NegativeFeedbackLoop(
setpoint=0.1,
gain=0.4,
damping=0.1,
min_correction=0.02,
max_correction=0.2,
silent=True,
)
coordination = CoordinationSystem()
lysosome = Lysosome(silent=True)
for resource in ("build_cluster", "deploy_slot", "metrics_channel"):
coordination.register_resource(resource)
release_queue = [
{
"id": "rel-101",
"prompt": "Deploy checkout cache warmup",
"observed_error_rate": 0.05,
},
{
"id": "rel-102",
"prompt": "Deploy auth service refactor",
"observed_error_rate": 0.18,
},
{
"id": "rel-103",
"prompt": "Hotfix: patch token validation",
"observed_error_rate": 0.12,
},
]
threshold = 0.6
for release in release_queue:
print("-" * 60)
print(f"Release {release['id']}: {release['prompt']}")
filter_result = membrane.filter(Signal(content=release["prompt"]))
if not filter_result.allowed:
print("Blocked by membrane:", filter_result.threat_level.name)
continue
correction = feedback.measure(release["observed_error_rate"])
threshold = clamp(threshold - correction, 0.4, 0.9)
quorum.set_strategy(VotingStrategy.MAJORITY, threshold=threshold)
vote_result = quorum.run_vote(release["prompt"])
print(
f"Quorum: permits={vote_result.permit_votes}, blocks={vote_result.block_votes}, "
f"threshold={vote_result.threshold_used:.2f}"
)
if not vote_result.reached:
print("Quorum blocked release")
continue
gate_result = cffl.run(release["prompt"])
if gate_result.blocked:
print("CFFL blocked release:", gate_result.block_reason)
continue
steps = [
("build", "build_cluster"),
("deploy", "deploy_slot"),
("monitor", "metrics_channel"),
]
for step_name, resource in steps:
if not budget.consume(12, f"{release['id']}:{step_name}"):
print("Insufficient ATP for", step_name)
break
result = coordination.execute_operation(
operation_id=f"{release['id']}-{step_name}",
agent_id="release_train",
work_fn=lambda s=step_name, r=release: run_release_step(s, r),
resources=[resource],
validate_fn=validate_step,
priority=1,
)
if not result.success:
lysosome.ingest(
Waste(
WasteType.FAILED_OPERATION,
{
"release": release["id"],
"step": step_name,
"error": result.error,
},
source="release_train",
)
)
print(f"Step failed: {step_name} -> {result.error}")
break
print(f"Step ok: {step_name}")
else:
print("Release completed")
digest = lysosome.digest()
print("=" * 70)
print(f"Lysosome digested: {digest.disposed}, recycled: {len(digest.recycled)}")
print(f"Remaining ATP: {budget.atp}")
print("=" * 70)
if __name__ == "__main__":
main()