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
|
"""Prompts for the paper's explicit five-stage kernel implementation.
The consolidated Prompt-A/Prompt-B interface remains byte-pinned in
``prompts.py``. The default pipeline exposes the manuscript's five operations
as separately validated artifacts and uses its Appendix F.3 judge prompt
verbatim.
"""
from __future__ import annotations
import json
from .kernel_models import (
DiffusedProof,
MethodPlan,
ProofDAG,
ReplacementPlan,
)
from .models import CanonicalItem
from .prompts import JUDGE_SYSTEM_PROMPT, JUDGE_USER_TEMPLATE
DAG_SYSTEM = "You are a rigorous competition-math proof analyst."
DAG_USER = """Parse the official solution into a concrete proof DAG.
Each node must be an intermediate mathematical claim, not a method name.
Each dependency must be a local entailment used to establish that node.
List nodes in topological order with IDs n1, n2, ... . Every node must
contribute to one terminal node.
Return JSON only:
{{"nodes":[{{"node_id":"n1","claim":"...","dependencies":[]}}],
"terminal_node_id":"nN"}}
ORIGINAL PROBLEM:
<<<{question}>>>
OFFICIAL SOLUTION:
<<<{solution}>>>"""
METHOD_SYSTEM = "You abstract concrete proofs into content-free method plans."
METHOD_USER = """For every concrete proof-DAG node, provide one content-free
method label. Preserve node IDs and order exactly. A method label describes the
operation, not the source constants, variable names, or final numerical answer.
Return JSON only:
{{"nodes":[{{"node_id":"n1","method_label":"..."}}]}}
PROOF DAG:
{dag}"""
REPLACEMENT_SYSTEM = "You design guarded, proof-plan-preserving math replacements."
REPLACEMENT_USER = """Choose one or more substantive numerical or structural
replacements that create a genuinely new problem while preserving the supplied
method plan.
For every change:
- target one of the disclosed source leaf DAG nodes;
- state that exact source DAG node;
- record the original and replacement values explicitly;
- state a mathematical guard condition derived from the original problem;
- explain why the replacement satisfies the guard.
The list must be closed: stages 4 and 5 may introduce no mathematical change
that is not declared here. Variable renaming alone is not a kernel change.
{repair_context}
Return JSON only:
{{"changes":[
{{"slot_id":"slot1","source_node_id":"n1","description":"...",
"original_value":"...","replacement_value":"...",
"guard_condition":"...","guard_justification":"..."}}
],
"closure_statement":"All intended mathematical changes are listed above."}}
ORIGINAL PROBLEM:
<<<{question}>>>
OFFICIAL SOLUTION:
<<<{solution}>>>
CONCRETE PROOF DAG:
{dag}
SOURCE LEAF NODE IDS:
{leaf_node_ids}
CONTENT-FREE METHOD PLAN:
{methods}"""
DIFFUSION_SYSTEM = "You re-instantiate a proof DAG under declared guarded replacements."
DIFFUSION_USER = """Propagate only the declared replacements through the proof
DAG, node by node. Return exactly one row for every source node, preserving its
ID, dependencies, and method label. Do not introduce undeclared constants,
objects, assumptions, reductions, or proof methods.
{repair_context}
Return JSON only:
{{"nodes":[
{{"node_id":"n1","dependencies":[],"method_label":"...",
"instantiated_claim":"...","justification":"..."}}
],
"terminal_node_id":"nN",
"terminal_answer":"..."}}
ORIGINAL PROBLEM:
<<<{question}>>>
SOURCE PROOF DAG:
{dag}
METHOD PLAN:
{methods}
DECLARED REPLACEMENTS:
{replacements}"""
RENDER_SYSTEM = "You render a verified proof DAG into a self-contained Putnam problem."
RENDER_USER = """Render the diffused proof into one complete problem statement
and one complete solution.
{repair_context}
Requirements:
- the question must be fully determined by the diffused terminal claim;
- the solution must follow the diffused nodes in order and visibly mark each
paragraph with [n1], [n2], ...;
- node_order must equal exactly {node_order};
- do not add any mathematical change beyond the declared replacements;
- copy the terminal answer exactly.
Return JSON only:
{{"question":"...","solution":"...","node_order":{node_order},
"terminal_answer":"..."}}
DECLARED REPLACEMENTS:
{replacements}
DIFFUSED PROOF:
{diffused}"""
JUDGE_SYSTEM = JUDGE_SYSTEM_PROMPT
def _dump(value: object) -> str:
if hasattr(value, "model_dump"):
value = value.model_dump(mode="json")
return json.dumps(value, ensure_ascii=False, indent=2)
def dag_user(item: CanonicalItem) -> str:
return DAG_USER.format(question=item.problem, solution=item.solution)
def method_user(dag: ProofDAG) -> str:
return METHOD_USER.format(dag=_dump(dag))
def replacement_user(
item: CanonicalItem,
dag: ProofDAG,
methods: MethodPlan,
*,
previous_replacements: ReplacementPlan | None = None,
feedback: str = "",
) -> str:
repair_context = (
"This is the initial replacement proposal."
if previous_replacements is None
else (
"This is a repair pass. Preserve the previous slot IDs, source leaf "
"nodes, and accepted changes unless the verification feedback "
"identifies them as the blocking issue. Make only the smallest "
"necessary correction.\n\nPREVIOUS REPLACEMENT PLAN:\n"
f"{_dump(previous_replacements)}\n\nVERIFICATION FEEDBACK:\n"
f"{feedback or 'No additional textual feedback was supplied.'}"
)
)
return REPLACEMENT_USER.format(
repair_context=repair_context,
question=item.problem,
solution=item.solution,
dag=_dump(dag),
leaf_node_ids=json.dumps(dag.leaf_node_ids(), ensure_ascii=False),
methods=_dump(methods),
)
def diffusion_user(
item: CanonicalItem,
dag: ProofDAG,
methods: MethodPlan,
replacements: ReplacementPlan,
*,
previous_diffused: DiffusedProof | None = None,
feedback: str = "",
) -> str:
repair_context = (
"This is the initial DAG diffusion."
if previous_diffused is None
else (
"This is a repair pass. Apply only corrections required by the "
"verification feedback; preserve every unaffected node.\n\n"
f"PREVIOUS DIFFUSED PROOF:\n{_dump(previous_diffused)}\n\n"
f"VERIFICATION FEEDBACK:\n"
f"{feedback or 'No additional textual feedback was supplied.'}"
)
)
return DIFFUSION_USER.format(
repair_context=repair_context,
question=item.problem,
dag=_dump(dag),
methods=_dump(methods),
replacements=_dump(replacements),
)
def render_user(
replacements: ReplacementPlan,
diffused: DiffusedProof,
*,
previous_variant: object | None = None,
feedback: str = "",
) -> str:
repair_context = (
"This is the initial rendering."
if previous_variant is None
else (
"This is a repair pass. Preserve all unaffected wording and apply "
"only corrections required by the verification feedback.\n\n"
f"PREVIOUS RENDERED VARIANT:\n{_dump(previous_variant)}\n\n"
f"VERIFICATION FEEDBACK:\n"
f"{feedback or 'No additional textual feedback was supplied.'}"
)
)
return RENDER_USER.format(
repair_context=repair_context,
node_order=json.dumps(
[node.node_id for node in diffused.nodes],
ensure_ascii=False,
),
replacements=_dump(replacements),
diffused=_dump(diffused),
)
def judge_user(
item: CanonicalItem,
methods: MethodPlan,
replacements: ReplacementPlan,
variant: object,
) -> str:
variant_payload = (
variant.model_dump(mode="json")
if hasattr(variant, "model_dump")
else variant
)
return JUDGE_USER_TEMPLATE.format(
original_problem=item.problem,
original_solution=item.solution,
method_labels=json.dumps(
[
f"{node.node_id}: {node.method_label}"
for node in methods.nodes
],
ensure_ascii=False,
),
slot_replacement=_dump(replacements),
candidate_problem=str(variant_payload["question"]),
candidate_proof=str(variant_payload["solution"]),
)
|