-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplan_research.py
More file actions
287 lines (241 loc) · 8.61 KB
/
Copy pathplan_research.py
File metadata and controls
287 lines (241 loc) · 8.61 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
"""A governed incident-research plan with visible parallel decomposition.
``plan_incident`` demonstrates refusal and replanning with a short chain. This
registry demonstrates the topology a real investigation needs: four evidence
collectors fan out from ``START``, join at correlation, fork into hypothesis
and impact analysis, then fan in to one final report.
Every kind writes one distinct state field. That makes parallel updates safe
without reducers and lets ``Materializer`` enforce the boundary independently
of the node body. ``page_oncall`` is registered so a planner can propose it,
but the default edge policy denies every transition into it.
"""
from __future__ import annotations
import json
from collections.abc import Callable
from typing import Any
from pydantic import BaseModel
from grapharc.harness.permissions import Decision
from grapharc.observe.trace import TraceRecorder
from grapharc.planner import (
AdmissionChecker,
AdmissionLimits,
CostEstimate,
EdgePolicy,
EdgeRule,
GovernedLoop,
LoopLimits,
Materializer,
NodeBuild,
NodeRegistry,
NodeSpec,
PlannerNode,
)
from grapharc.runtime.budget import Budget
from grapharc.runtime.graph import END, START
class ResearchState(BaseModel):
"""One independent field per investigative kind, plus the operator's goal."""
goal: str = ""
incident_logs: str = ""
service_metrics: str = ""
recent_deployments: str = ""
support_tickets: str = ""
correlation: str = ""
hypothesis: str = ""
impact: str = ""
report: str = ""
oncall_page: str = ""
def _pull_logs(state: ResearchState) -> dict[str, str]:
return {"incident_logs": f"logs: checkout timeouts increased for {state.goal}"}
def _pull_metrics(state: ResearchState) -> dict[str, str]:
return {"service_metrics": "metrics: p95 latency and error rate rose together"}
def _pull_deploys(state: ResearchState) -> dict[str, str]:
return {"recent_deployments": "deploys: checkout-api release preceded the increase"}
def _pull_tickets(state: ResearchState) -> dict[str, str]:
return {"support_tickets": "tickets: customers report payment confirmation timeouts"}
def _correlate(state: ResearchState) -> dict[str, str]:
evidence = (
state.incident_logs,
state.service_metrics,
state.recent_deployments,
state.support_tickets,
)
return {"correlation": "correlation: " + " | ".join(evidence)}
def _test_hypothesis(state: ResearchState) -> dict[str, str]:
return {
"hypothesis": (
"hypothesis: the checkout-api release caused the latency regression; "
f"evidence considered: {state.correlation}"
)
}
def _estimate_impact(state: ResearchState) -> dict[str, str]:
return {
"impact": (
"impact: checkout completion is degraded for affected customers; "
f"ticket signal: {state.support_tickets}"
)
}
def _write_report(state: ResearchState) -> dict[str, str]:
return {
"report": (
f"final report: {state.hypothesis}. {state.impact}. "
"Recommended next step: verify a rollback in a controlled environment."
)
}
def _page_oncall(state: ResearchState) -> dict[str, str]:
return {"oncall_page": f"page requested for: {state.goal}"}
_BODIES: dict[str, Callable[[ResearchState], dict[str, str]]] = {
"pull_logs": _pull_logs,
"pull_metrics": _pull_metrics,
"pull_deploys": _pull_deploys,
"pull_tickets": _pull_tickets,
"correlate": _correlate,
"test_hypothesis": _test_hypothesis,
"estimate_impact": _estimate_impact,
"write_report": _write_report,
"page_oncall": _page_oncall,
}
WRITES: dict[str, set[str]] = {
"pull_logs": {"incident_logs"},
"pull_metrics": {"service_metrics"},
"pull_deploys": {"recent_deployments"},
"pull_tickets": {"support_tickets"},
"correlate": {"correlation"},
"test_hypothesis": {"hypothesis"},
"estimate_impact": {"impact"},
"write_report": {"report"},
"page_oncall": {"oncall_page"},
}
_DESCRIPTIONS = {
"pull_logs": "collect relevant application logs",
"pull_metrics": "collect service latency and error metrics",
"pull_deploys": "collect recent deployment history",
"pull_tickets": "collect related customer-support reports",
"correlate": "join the four evidence streams and correlate their timing",
"test_hypothesis": "test the most likely causal hypothesis",
"estimate_impact": "estimate customer and service impact",
"write_report": "fan in the analyses and write the final incident report",
"page_oncall": "page the human on-call responder",
}
def _factory(build: NodeBuild) -> Callable[[ResearchState], dict[str, str]]:
body = _BODIES[build.kind]
body.writes = WRITES[build.kind] # type: ignore[attr-defined]
return body
def build_registry() -> NodeRegistry:
"""Nine bounded kinds; a proposal can name them but cannot supply bodies."""
return NodeRegistry(
NodeSpec(
name=kind,
description=description,
factory=_factory,
worst_case=CostEstimate(iterations=1, tokens=200),
)
for kind, description in _DESCRIPTIONS.items()
)
def default_edge_policy() -> EdgePolicy:
"""Keep paging visible to the planner while making it unreachable."""
return EdgePolicy(
rules=(
EdgeRule(
action=Decision.DENY,
target="page_oncall",
reason="paging a human requires a separate approval path",
),
EdgeRule(action=Decision.ALLOW),
)
)
def _reply(nodes: list[str], edges: list[tuple[str, str]], rationale: str) -> str:
return json.dumps(
{
"nodes": [{"name": name} for name in nodes],
"edges": [{"source": source, "target": target} for source, target in edges],
"rationale": rationale,
}
)
def scripted_planner_replies() -> list[str]:
"""Refuse paging, then run a deterministic parallel research topology."""
collectors = ["pull_logs", "pull_metrics", "pull_deploys", "pull_tickets"]
research_nodes = [
*collectors,
"correlate",
"test_hypothesis",
"estimate_impact",
"write_report",
]
research_edges = [
*((START, kind) for kind in collectors),
*((kind, "correlate") for kind in collectors),
("correlate", "test_hypothesis"),
("correlate", "estimate_impact"),
("test_hypothesis", "write_report"),
("estimate_impact", "write_report"),
("write_report", END),
]
return [
_reply(
["page_oncall"],
[(START, "page_oncall"), ("page_oncall", END)],
"page a human immediately",
),
_reply(
research_nodes,
research_edges,
"collect evidence in parallel, join it, analyse in parallel, then report",
),
]
def build_loop(
model: Any,
*,
edge_policy: EdgePolicy | None = None,
node_policy: Any = None,
trace: TraceRecorder | None = None,
budget: Budget | None = None,
limits: LoopLimits | None = None,
registry: NodeRegistry | None = None,
state_schema: type[BaseModel] | None = None,
writes: dict[str, set[str]] | None = None,
approval: Any = None,
) -> GovernedLoop:
"""Assemble the research loop from operator-owned policy and bodies."""
registry = registry or build_registry()
registry.freeze()
edge_policy = edge_policy or default_edge_policy()
return GovernedLoop(
planner=PlannerNode(
model,
name="research",
catalog=registry.catalog(),
edge_policy=edge_policy,
node_policy=node_policy,
trace=trace,
),
checker=AdmissionChecker(
registry=registry,
edge_policy=edge_policy,
node_policy=node_policy,
trace=trace,
limits=AdmissionLimits(require_entry=True),
),
materializer=Materializer(
registry=registry,
state_schema=state_schema or ResearchState,
writes=writes if writes is not None else WRITES,
trace=trace,
),
budget=budget,
limits=limits,
trace=trace,
name="research_loop",
goal_reached=lambda state: bool(getattr(state, "report", "").strip()),
approval=approval,
)
STATE_SCHEMA = ResearchState
MUTATING_KINDS = ("page_oncall",)
__all__ = [
"MUTATING_KINDS",
"STATE_SCHEMA",
"WRITES",
"ResearchState",
"build_loop",
"build_registry",
"default_edge_policy",
"scripted_planner_replies",
]