Repository navigation
Expand file tree
/
Copy pathcompounding_roi.py
More file actions
314 lines (277 loc) · 9.44 KB
/
Copy pathcompounding_roi.py
File metadata and controls
314 lines (277 loc) · 9.44 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
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
"""PlayerZero steal: compounding ROI control plane. Fail closed. No SaaS."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Iterable, Mapping, Sequence
LIFECYCLE_STAGES = (
"planning",
"coding",
"review",
"docs",
"quality",
"triage",
"sre",
)
VANITY_METRICS = frozenset(
{
"pr_velocity",
"tokens_consumed",
"deploy_frequency",
"sprint_completion",
}
)
HAWK_METRICS = frozenset(
{
"wqtu",
"paywall_attempt_success",
"incident_recurrence",
"defect_escape_rate",
"mttr",
"time_to_first_hypothesis",
"escalations_per_ticket",
"engineering_capacity_reclaimed",
}
)
REQUIRED_WORLD_SOURCES = ("code", "telemetry", "deploys", "resolution_history")
CYCLE_LINKS = (
"quality_to_simulation",
"simulation_to_prevention",
"prevention_to_triage",
"triage_to_knowledge",
"knowledge_to_simulation",
)
PAYWALL_WEIGHT = 10
WQTU_WEIGHT = 5
@dataclass(frozen=True)
class ControlDecision:
action: str
ok: bool
reason: str
def _norm(value: str) -> str:
return (value or "").strip().lower()
def evaluate_lifecycle_handoff(
*,
source_stage: str,
target_stages: Iterable[str],
shared_context_id: str,
) -> ControlDecision:
source = _norm(source_stage)
targets = [_norm(item) for item in target_stages if str(item).strip()]
context = (shared_context_id or "").strip()
if source not in LIFECYCLE_STAGES:
return ControlDecision(
action="block_unknown_stage",
ok=False,
reason="handoffs must use a mapped SDLC stage",
)
known_targets = [item for item in targets if item in LIFECYCLE_STAGES]
if not known_targets or not context:
return ControlDecision(
action="block_single_player",
ok=False,
reason="intelligence reset at handoff; mice stay in their maze",
)
return ControlDecision(
action="allow_multiplayer",
ok=True,
reason="shared context id flows to at least one other stage",
)
def evaluate_hawk_metrics(*, metrics: Iterable[str]) -> ControlDecision:
names = {_norm(item).replace("-", "_") for item in metrics if str(item).strip()}
if names & HAWK_METRICS:
return ControlDecision(
action="allow_hawk_metrics",
ok=True,
reason="prevention or customer-outcome metrics are present",
)
return ControlDecision(
action="block_velocity_proxy",
ok=False,
reason="PR velocity and tokens do not prove the org is getting smarter",
)
def evaluate_prevention_recipe(
*,
resolved: bool,
pattern: str,
gate: str,
) -> ControlDecision:
if not resolved:
return ControlDecision(
action="block_unresolved",
ok=False,
reason="unresolved incidents cannot claim a prevention recipe",
)
if not (pattern or "").strip() or not (gate or "").strip():
return ControlDecision(
action="block_recovery_only",
ok=False,
reason="a local fix without a watch is recovery, not prevention",
)
return ControlDecision(
action="allow_prevention",
ok=True,
reason="escaped defect encoded as a reusable gate",
)
def evaluate_compounding_cycle(*, links: Mapping[str, object]) -> ControlDecision:
missing = [
key
for key in CYCLE_LINKS
if not bool(links.get(key))
]
if missing:
return ControlDecision(
action="block_linear_gains",
ok=False,
reason="cycle broken; single-player gains do not compound",
)
return ControlDecision(
action="allow_compounding",
ok=True,
reason="quality, simulation, prevention, triage, and knowledge feed each other",
)
def evaluate_world_model(
*,
sources: Iterable[str],
persists: bool,
) -> ControlDecision:
have = {_norm(item) for item in sources if str(item).strip()}
missing = [item for item in REQUIRED_WORLD_SOURCES if item not in have]
if missing or not persists:
return ControlDecision(
action="block_session_reset",
ok=False,
reason="session-only memory evaporates; Phase 2 needs a persistent graph",
)
return ControlDecision(
action="allow_world_model",
ok=True,
reason="code, telemetry, deploys, and resolutions persist across sessions",
)
def evaluate_judgment_handoff(
*,
root_cause: str,
blast_radius: str,
decision: str,
human_role: str,
) -> ControlDecision:
role = _norm(human_role)
cause = (root_cause or "").strip()
blast = (blast_radius or "").strip()
call = (decision or "").strip()
if role != "judgment" or not cause or cause.lower() == "unknown" or not blast or not call:
return ControlDecision(
action="block_human_cleanup",
ok=False,
reason="agents reconstruct context; humans judge, they do not mop up",
)
return ControlDecision(
action="allow_judgment",
ok=True,
reason="investigation assembled; human is spun in for the call only",
)
def rank_work(*, candidates: Sequence[Mapping[str, object]]) -> list[dict[str, object]]:
scored: list[tuple[float, dict[str, object]]] = []
for raw in candidates:
row = dict(raw)
metric = _norm(str(row.get("metric", "")))
effort = max(float(row.get("effort") or 1), 1.0)
if metric in VANITY_METRICS:
score = 0.0
else:
paywall = float(row.get("expected_paywall_attempts") or 0)
wqtu = float(row.get("expected_wqtu_delta") or 0)
score = (paywall * PAYWALL_WEIGHT + wqtu * WQTU_WEIGHT) / effort
row["score"] = score
scored.append((score, row))
scored.sort(key=lambda item: item[0], reverse=True)
return [row for _, row in scored]
def require_compounding_controls(
*,
has_lifecycle: bool,
has_metrics: bool,
has_prevention: bool,
has_cycle: bool,
has_world_model: bool,
) -> ControlDecision:
if has_lifecycle and has_metrics and has_prevention and has_cycle and has_world_model:
return ControlDecision(
action="controls_complete",
ok=True,
reason="lifecycle, hawk metrics, prevention, cycle, and world model evaluated",
)
return ControlDecision(
action="block_incomplete_controls",
ok=False,
reason="fail closed: missing lifecycle, metrics, prevention, cycle, or world model",
)
def main() -> int:
import argparse
import json
parser = argparse.ArgumentParser(description="Compounding ROI control plane (no SaaS call).")
parser.add_argument("--source-stage", default="")
parser.add_argument("--target-stages", default="")
parser.add_argument("--context-id", default="")
parser.add_argument("--metrics", default="")
parser.add_argument("--resolved", action="store_true")
parser.add_argument("--pattern", default="")
parser.add_argument("--gate", default="")
parser.add_argument("--cycle", default="")
parser.add_argument("--sources", default="")
parser.add_argument("--persists", action="store_true")
args = parser.parse_args()
payload: dict[str, object] = {}
ok = True
has_lifecycle = bool(args.source_stage or args.target_stages or args.context_id)
if has_lifecycle:
lifecycle = evaluate_lifecycle_handoff(
source_stage=args.source_stage,
target_stages=[part.strip() for part in args.target_stages.split(",") if part.strip()],
shared_context_id=args.context_id,
)
payload["lifecycle"] = lifecycle.__dict__
ok = ok and lifecycle.ok
has_metrics = bool(args.metrics)
if has_metrics:
metrics = evaluate_hawk_metrics(
metrics=[part.strip() for part in args.metrics.split(",") if part.strip()]
)
payload["metrics"] = metrics.__dict__
ok = ok and metrics.ok
has_prevention = bool(args.resolved or args.pattern or args.gate)
if has_prevention:
prevention = evaluate_prevention_recipe(
resolved=bool(args.resolved),
pattern=args.pattern,
gate=args.gate,
)
payload["prevention"] = prevention.__dict__
ok = ok and prevention.ok
has_cycle = bool(args.cycle)
if has_cycle:
links = {
key: True
for key in (part.strip() for part in args.cycle.split(",") if part.strip())
}
cycle = evaluate_compounding_cycle(links=links)
payload["cycle"] = cycle.__dict__
ok = ok and cycle.ok
has_world_model = bool(args.sources or args.persists)
if has_world_model:
world = evaluate_world_model(
sources=[part.strip() for part in args.sources.split(",") if part.strip()],
persists=bool(args.persists),
)
payload["world_model"] = world.__dict__
ok = ok and world.ok
completeness = require_compounding_controls(
has_lifecycle=has_lifecycle,
has_metrics=has_metrics,
has_prevention=has_prevention,
has_cycle=has_cycle,
has_world_model=has_world_model,
)
payload["completeness"] = completeness.__dict__
ok = ok and completeness.ok
print(json.dumps(payload, indent=2))
return 0 if ok else 2
if __name__ == "__main__":
raise SystemExit(main())