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
|
"""Prompt-faithful kernel generation and the J=5, K=2, T=15 loop."""
from __future__ import annotations
import asyncio
from pydantic import BaseModel, ConfigDict, model_validator
from .clients import JsonLLM
from .models import (
CanonicalItem,
IterationRecord,
JudgeVerdict,
KernelCandidate,
KernelPlan,
KernelRunResult,
ModelCallRecord,
)
from .prompts import (
FIX_SYSTEM_PROMPT,
JUDGE_SYSTEM_PROMPT,
KERNEL_GENERATE_SYSTEM,
KERNEL_PLAN_SYSTEM,
fix_user,
judge_user,
kernel_generate_user,
kernel_plan_user,
)
from .store import RunStore, sha256_payload
class PipelineConfig(BaseModel):
model_config = ConfigDict(extra="forbid")
protocol_name: str = "gap-J5-K2-T15"
proposer_model: str
judge_model: str
judge_count: int = 5
streak_length: int = 2
max_iterations: int = 15
@model_validator(mode="after")
def enforce_protocol(self) -> "PipelineConfig":
if (self.judge_count, self.streak_length, self.max_iterations) != (5, 2, 15):
raise ValueError("the GAP protocol is fixed at J=5, K=2, T=15")
return self
class KernelPipeline:
def __init__(
self,
*,
proposer: JsonLLM,
judges: list[JsonLLM],
store: RunStore,
config: PipelineConfig,
) -> None:
if len(judges) != config.judge_count:
raise ValueError(
f"expected {config.judge_count} judges, received {len(judges)}"
)
self.proposer = proposer
self.judges = judges
self.store = store
self.config = config
async def _call(
self,
client: JsonLLM,
*,
request_id: str,
system_prompt: str,
user_prompt: str,
) -> dict:
response = await client.generate_json(
system_prompt=system_prompt,
user_prompt=user_prompt,
request_id=request_id,
)
self.store.write_call(
ModelCallRecord(
request_id=request_id,
model=response.model,
system_prompt=system_prompt,
user_prompt=user_prompt,
response_data=response.data,
raw_text=response.raw_text,
provider_response_id=response.response_id,
usage=response.usage,
)
)
return response.data
async def extract_plan(self, item: CanonicalItem) -> KernelPlan:
request_id = f"{item.item_id}.plan"
payload = await self._call(
self.proposer,
request_id=request_id,
system_prompt=KERNEL_PLAN_SYSTEM,
user_prompt=kernel_plan_user(item),
)
plan = KernelPlan.model_validate(payload)
nodes = [
{
"node_id": f"n{index}",
"method_label": step,
"dependencies": [] if index == 1 else [f"n{index - 1}"],
}
for index, step in enumerate(plan.core_steps, start=1)
]
self.store.write_stage(
"01_proof_dag",
{
"nodes": nodes,
"terminal_node_id": nodes[-1]["node_id"],
"construction": "ordered core_steps returned verbatim by Prompt-A",
},
request_id=request_id,
)
self.store.write_stage(
"02_method_plan",
{"method_labels": plan.core_steps},
request_id=request_id,
)
self.store.write_stage(
"03_mutable_slots",
{
key: value.model_dump(mode="json")
for key, value in plan.mutable_slots.items()
},
request_id=request_id,
)
return plan
async def generate_candidate(
self,
item: CanonicalItem,
plan: KernelPlan,
) -> KernelCandidate:
request_id = f"{item.item_id}.candidate"
payload = await self._call(
self.proposer,
request_id=request_id,
system_prompt=KERNEL_GENERATE_SYSTEM,
user_prompt=kernel_generate_user(item, plan),
)
candidate = KernelCandidate.model_validate(payload)
self.store.write_stage(
"04_regenerated_proof",
{"solution": candidate.solution},
request_id=request_id,
)
self.store.write_stage(
"05_variant_question",
{"question": candidate.question},
request_id=request_id,
)
return candidate
async def _judge_once(
self,
judge: JsonLLM,
*,
item: CanonicalItem,
plan: KernelPlan,
candidate: KernelCandidate,
iteration: int,
judge_id: int,
) -> JudgeVerdict:
request_id = f"{item.item_id}.verify.t{iteration:02d}.j{judge_id}"
payload = await self._call(
judge,
request_id=request_id,
system_prompt=JUDGE_SYSTEM_PROMPT,
user_prompt=judge_user(item, plan, candidate),
)
return JudgeVerdict.model_validate(payload)
async def _repair(
self,
*,
item: CanonicalItem,
candidate: KernelCandidate,
verdicts: list[JudgeVerdict],
iteration: int,
) -> KernelCandidate:
problem_issues: list[str] = []
solution_issues: list[str] = []
for verdict in verdicts:
if verdict.verdict == "reject":
problem_issues.append(verdict.blocking_issues)
solution_issues.append(
verdict.patch_suggestion or verdict.blocking_issues
)
request_id = f"{item.item_id}.repair.after_t{iteration:02d}"
payload = await self._call(
self.proposer,
request_id=request_id,
system_prompt=FIX_SYSTEM_PROMPT,
user_prompt=fix_user(
item,
candidate,
problem_issues="; ".join(problem_issues),
solution_issues="; ".join(solution_issues),
),
)
repaired = KernelCandidate(
question=str(payload["corrected_question"]),
solution=str(payload["corrected_solution"]),
)
self.store.write_stage(
f"repair_after_{iteration:02d}",
repaired,
request_id=request_id,
)
return repaired
async def verify_candidate(
self,
*,
item: CanonicalItem,
plan: KernelPlan,
candidate: KernelCandidate,
) -> KernelRunResult:
current = candidate
iterations: list[IterationRecord] = []
pass_streak = 0
streak_sha: str | None = None
for iteration in range(1, self.config.max_iterations + 1):
current_sha = sha256_payload(current)
verdicts = list(
await asyncio.gather(
*(
self._judge_once(
judge,
item=item,
plan=plan,
candidate=current,
iteration=iteration,
judge_id=judge_id,
)
for judge_id, judge in enumerate(self.judges, start=1)
)
)
)
unanimous = all(verdict.verdict == "accept" for verdict in verdicts)
if unanimous:
if streak_sha not in {None, current_sha}:
raise AssertionError("pass streak crossed candidate versions")
streak_sha = current_sha
pass_streak += 1
else:
pass_streak = 0
streak_sha = None
record = IterationRecord(
iteration=iteration,
candidate_sha256=current_sha,
verdicts=verdicts,
unanimous=unanimous,
pass_streak_after=pass_streak,
)
iterations.append(record)
self.store.write_iteration(iteration, record)
if pass_streak == self.config.streak_length:
result = KernelRunResult(
item_id=item.item_id,
status="accepted",
plan=plan,
accepted_candidate=current,
iterations=iterations,
accepted_candidate_sha256=current_sha,
)
self.store.write_final(result)
return result
if not unanimous and iteration < self.config.max_iterations:
current = await self._repair(
item=item,
candidate=current,
verdicts=verdicts,
iteration=iteration,
)
result = KernelRunResult(
item_id=item.item_id,
status="rejected",
plan=plan,
iterations=iterations,
rejection_reason="no two consecutive unanimous rounds within T=15",
)
self.store.write_final(result)
return result
async def run(self, item: CanonicalItem) -> KernelRunResult:
self.store.write_input(item)
self.store.write_config(self.config)
plan = await self.extract_plan(item)
candidate = await self.generate_candidate(item, plan)
return await self.verify_candidate(
item=item,
plan=plan,
candidate=candidate,
)
|