summaryrefslogtreecommitdiff
path: root/src/gap_pipeline/paper_pipeline.py
blob: c90f3f651a2e4605604db12ce3c4b932788decb9 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""Literal five-stage GAP kernel pipeline matching the manuscript operations."""

from __future__ import annotations

import asyncio

from pydantic import BaseModel, ConfigDict, model_validator

from .clients import JsonLLM
from .kernel_models import (
    CandidateBundle,
    DiffusedProof,
    JudgeVerdict,
    KernelRunResult,
    MethodPlan,
    ProofDAG,
    RenderedVariant,
    ReplacementPlan,
    VerificationIteration,
)
from .kernel_prompts import (
    DAG_SYSTEM,
    DIFFUSION_SYSTEM,
    JUDGE_SYSTEM,
    METHOD_SYSTEM,
    RENDER_SYSTEM,
    REPLACEMENT_SYSTEM,
    dag_user,
    diffusion_user,
    judge_user,
    method_user,
    render_user,
    replacement_user,
)
from .models import CanonicalItem, ModelCallRecord
from .store import RunStore, sha256_payload


class PaperPipelineConfig(BaseModel):
    model_config = ConfigDict(extra="forbid")

    protocol_name: str = "gap-literal-five-stage-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) -> "PaperPipelineConfig":
        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 PaperKernelPipeline:
    """Execute all five paper stages as typed, separately auditable calls."""

    def __init__(
        self,
        *,
        proposer: JsonLLM,
        judges: list[JsonLLM],
        store: RunStore,
        config: PaperPipelineConfig,
    ) -> 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 construct_dag(self, item: CanonicalItem) -> ProofDAG:
        request_id = f"{item.item_id}.stage1.dag"
        dag = ProofDAG.model_validate(
            await self._call(
                self.proposer,
                request_id=request_id,
                system_prompt=DAG_SYSTEM,
                user_prompt=dag_user(item),
            )
        )
        self.store.write_stage("01_proof_dag", dag, request_id=request_id)
        return dag

    async def summarize_methods(self, dag: ProofDAG) -> MethodPlan:
        request_id = f"{self.store.item_id}.stage2.methods"
        methods = MethodPlan.model_validate(
            await self._call(
                self.proposer,
                request_id=request_id,
                system_prompt=METHOD_SYSTEM,
                user_prompt=method_user(dag),
            )
        ).validate_against(dag)
        self.store.write_stage("02_method_plan", methods, request_id=request_id)
        return methods

    async def generate_replacements(
        self,
        item: CanonicalItem,
        dag: ProofDAG,
        methods: MethodPlan,
        *,
        version: int,
        previous_replacements: ReplacementPlan | None = None,
        feedback: str = "",
    ) -> ReplacementPlan:
        request_id = f"{item.item_id}.stage3.replacement.v{version:02d}"
        replacements = ReplacementPlan.model_validate(
            await self._call(
                self.proposer,
                request_id=request_id,
                system_prompt=REPLACEMENT_SYSTEM,
                user_prompt=replacement_user(
                    item,
                    dag,
                    methods,
                    previous_replacements=previous_replacements,
                    feedback=feedback,
                ),
            )
        ).validate_against(dag)
        if previous_replacements is not None:
            replacements.validate_repair_of(previous_replacements)
        self.store.write_stage(
            f"03_replacement_v{version:02d}",
            replacements,
            request_id=request_id,
        )
        return replacements

    async def diffuse_dag(
        self,
        item: CanonicalItem,
        dag: ProofDAG,
        methods: MethodPlan,
        replacements: ReplacementPlan,
        *,
        version: int,
        previous_diffused: DiffusedProof | None = None,
        feedback: str = "",
    ) -> DiffusedProof:
        request_id = f"{item.item_id}.stage4.diffusion.v{version:02d}"
        diffused = DiffusedProof.model_validate(
            await self._call(
                self.proposer,
                request_id=request_id,
                system_prompt=DIFFUSION_SYSTEM,
                user_prompt=diffusion_user(
                    item,
                    dag,
                    methods,
                    replacements,
                    previous_diffused=previous_diffused,
                    feedback=feedback,
                ),
            )
        ).validate_against(dag, methods)
        self.store.write_stage(
            f"04_diffused_proof_v{version:02d}",
            diffused,
            request_id=request_id,
        )
        return diffused

    async def render_variant(
        self,
        dag: ProofDAG,
        replacements: ReplacementPlan,
        diffused: DiffusedProof,
        *,
        version: int,
        previous_variant: RenderedVariant | None = None,
        feedback: str = "",
    ) -> RenderedVariant:
        request_id = f"{self.store.item_id}.stage5.render.v{version:02d}"
        variant = RenderedVariant.model_validate(
            await self._call(
                self.proposer,
                request_id=request_id,
                system_prompt=RENDER_SYSTEM,
                user_prompt=render_user(
                    replacements,
                    diffused,
                    previous_variant=previous_variant,
                    feedback=feedback,
                ),
            )
        ).validate_against(dag, diffused)
        self.store.write_stage(
            f"05_rendered_variant_v{version:02d}",
            variant,
            request_id=request_id,
        )
        return variant

    async def build_bundle(
        self,
        item: CanonicalItem,
        dag: ProofDAG,
        methods: MethodPlan,
        *,
        version: int,
        previous_bundle: CandidateBundle | None = None,
        feedback: str = "",
    ) -> CandidateBundle:
        replacements = await self.generate_replacements(
            item,
            dag,
            methods,
            version=version,
            previous_replacements=(
                previous_bundle.replacement_plan
                if previous_bundle is not None
                else None
            ),
            feedback=feedback,
        )
        diffused = await self.diffuse_dag(
            item,
            dag,
            methods,
            replacements,
            version=version,
            previous_diffused=(
                previous_bundle.diffused_proof
                if previous_bundle is not None
                else None
            ),
            feedback=feedback,
        )
        variant = await self.render_variant(
            dag,
            replacements,
            diffused,
            version=version,
            previous_variant=(
                previous_bundle.variant if previous_bundle is not None else None
            ),
            feedback=feedback,
        )
        bundle = CandidateBundle(
            replacement_plan=replacements,
            diffused_proof=diffused,
            variant=variant,
        )
        if (
            previous_bundle is not None
            and sha256_payload(bundle) == sha256_payload(previous_bundle)
        ):
            raise ValueError("repair pass returned an unchanged candidate bundle")
        return bundle

    async def _judge_once(
        self,
        judge: JsonLLM,
        *,
        item: CanonicalItem,
        dag: ProofDAG,
        methods: MethodPlan,
        bundle: CandidateBundle,
        iteration: int,
        judge_id: int,
    ) -> JudgeVerdict:
        request_id = f"{item.item_id}.verify.t{iteration:02d}.j{judge_id}"
        verdict = JudgeVerdict.model_validate(
            await self._call(
                judge,
                request_id=request_id,
                system_prompt=JUDGE_SYSTEM,
                user_prompt=judge_user(
                    item,
                    methods,
                    bundle.replacement_plan,
                    bundle.variant,
                ),
            )
        )
        return verdict.validate_coverage(dag)

    @staticmethod
    def _feedback(verdicts: list[JudgeVerdict]) -> str:
        rows = []
        for index, verdict in enumerate(verdicts, start=1):
            if verdict.verdict == "reject":
                rows.append(
                    f"judge {index}: {verdict.blocking_issues}; "
                    f"suggested repair: {verdict.patch_suggestion}"
                )
        return "\n".join(rows)

    async def verify(
        self,
        item: CanonicalItem,
        dag: ProofDAG,
        methods: MethodPlan,
        initial_bundle: CandidateBundle,
    ) -> KernelRunResult:
        bundle = initial_bundle
        iterations: list[VerificationIteration] = []
        pass_streak = 0
        streak_sha: str | None = None
        repaired_from_previous = False

        for iteration in range(1, self.config.max_iterations + 1):
            bundle_sha = sha256_payload(bundle)
            candidate_sha = sha256_payload(bundle.variant)
            verdicts = list(
                await asyncio.gather(
                    *(
                        self._judge_once(
                            judge,
                            item=item,
                            dag=dag,
                            methods=methods,
                            bundle=bundle,
                            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, bundle_sha}:
                    raise AssertionError("pass streak crossed provenance versions")
                streak_sha = bundle_sha
                pass_streak += 1
            else:
                pass_streak = 0
                streak_sha = None

            record = VerificationIteration(
                iteration=iteration,
                bundle_sha256=bundle_sha,
                candidate_sha256=candidate_sha,
                verdicts=verdicts,
                unanimous=unanimous,
                pass_streak_after=pass_streak,
                repaired_from_previous=repaired_from_previous,
            )
            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",
                    proof_dag=dag,
                    method_plan=methods,
                    accepted_replacement_plan=bundle.replacement_plan,
                    accepted_diffused_proof=bundle.diffused_proof,
                    accepted_candidate=bundle.variant,
                    iterations=iterations,
                    accepted_candidate_sha256=candidate_sha,
                    accepted_bundle_sha256=bundle_sha,
                )
                self.store.write_final(result)
                return result

            if not unanimous and iteration < self.config.max_iterations:
                bundle = await self.build_bundle(
                    item,
                    dag,
                    methods,
                    version=iteration + 1,
                    previous_bundle=bundle,
                    feedback=self._feedback(verdicts),
                )
                repaired_from_previous = True
            else:
                repaired_from_previous = False

        result = KernelRunResult(
            item_id=item.item_id,
            status="rejected",
            proof_dag=dag,
            method_plan=methods,
            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)
        dag = await self.construct_dag(item)
        methods = await self.summarize_methods(dag)
        bundle = await self.build_bundle(item, dag, methods, version=1)
        return await self.verify(item, dag, methods, bundle)