-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_ngram_searcher.py
More file actions
675 lines (557 loc) Β· 31.4 KB
/
clean_ngram_searcher.py
File metadata and controls
675 lines (557 loc) Β· 31.4 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env python3
"""
Clean N-gram Searcher for Pickle Mode
Simplified searcher specifically designed for the pickle-based pipeline.
No complex memory management, checkpoints, or server restart handling.
Pure search logic with clean state management.
"""
import json
import time
from enum import Enum
from datetime import datetime
from typing import Dict, List, Optional, Set, Tuple, Callable
from dataclasses import dataclass, field
import psutil
from proofstep_lean_integration import MinimalLeanProofStepIntegrator
from theorem_sourcing_interface import get_related_theorems
from llm_pruner import LLMPruner, PruningDecision
# Import unified type definitions
from ngram_types import HolePickleInfo
@dataclass(frozen=True)
class ProofStateSignature:
"""Represents a unique signature of a proof state (hypothesis + goal combination)"""
hypothesis_hash: str
goal_hash: str
def __hash__(self):
return hash((self.hypothesis_hash, self.goal_hash))
def __eq__(self, other):
if not isinstance(other, ProofStateSignature):
return False
return (self.hypothesis_hash == other.hypothesis_hash and
self.goal_hash == other.goal_hash)
class NodeStatus(Enum):
"""Simple node status"""
PENDING = "pending"
SUCCESS = "success"
ERROR = "error"
INCOMPLETE = "incomplete"
@dataclass
class SearchNode:
"""Simple search node"""
node_id: str
tactic_sequence: List[str]
parent_id: Optional[str]
status: NodeStatus = NodeStatus.PENDING
children: Set[str] = field(default_factory=set)
depth: int = 0
proof_state_id: Optional[int] = None
error_message: Optional[str] = None
remaining_goals: int = 0
llm_pruned: bool = False # Track if this node was pruned by LLM
state_duplicate_pruned: bool = False # Track if this node was pruned by state duplication
def get_last_tactic(self) -> Optional[str]:
return self.tactic_sequence[-1] if self.tactic_sequence else None
def is_terminal(self) -> bool:
"""Check if this node should not be expanded further"""
return (self.status in [NodeStatus.SUCCESS, NodeStatus.ERROR] or
self.get_last_tactic() in ['linarith', 'nlinarith', 'omega'] or
self.llm_pruned or
self.state_duplicate_pruned)
@dataclass
class SearchResults:
"""Search results for a hole"""
hole_id: str
successful_paths: List[List[str]]
total_nodes: int
search_time_seconds: float
nodes_by_status: Dict[str, int]
max_depth_reached: int
class CleanNgramSearcher:
"""Clean n-gram searcher for pickle mode"""
def __init__(self, max_depth: int = 2, stop_on_first_success: bool = True,
theorem_query_func: Optional[Callable[[str, str, str], List[str]]] = None,
enable_llm_pruning: bool = False):
self.max_depth = max_depth
self.stop_on_first_success = stop_on_first_success
self.enable_llm_pruning = enable_llm_pruning
# Simple tactic list
self.available_tactics = ["norm_num", "linarith", "nlinarith", "omega", "ring", "ring_nf", "simp", "simpa", "field_simp", "positivity", "norm_cast"]
self.terminal_tactics = {'linarith', 'nlinarith', 'omega'}
# Theorem query function for dynamic rw[theorem] tactics
# it seems that we use self._default_theorem_query for now
self.theorem_query_func = theorem_query_func or self._default_theorem_query
# LLM Pruner (initialized only if enabled)
self.llm_pruner: Optional[LLMPruner] = None
if self.enable_llm_pruning:
self.llm_pruner = LLMPruner()
# Current search state
self.nodes: Dict[str, SearchNode] = {}
self.successful_paths: List[List[str]] = []
self.lean_integrator: Optional[MinimalLeanProofStepIntegrator] = None
# State deduplication for current hole (reset for each hole)
self.visited_states: Set[ProofStateSignature] = set()
# Detailed search log
self.explored_nodes_log: List[Dict] = []
# Memory management now handled transparently by ProofStateCache, removed complex server restart logic
def _default_theorem_query(self, dataset: str, problem_id: str, hole_id: str) -> List[str]:
"""Default theorem query function - uses real theorem sourcing interface with fallback"""
theorems = get_related_theorems(dataset, problem_id, hole_id)
return theorems
def get_available_tactics_for_hole(self, dataset: str, problem_id: str, hole_id: str) -> List[str]:
"""Get all available tactics for a specific hole, including dynamic rw[theorem] tactics"""
# Start with base tactics
tactics = self.available_tactics.copy()
# Add dynamic rw[theorem] tactics
available_theorems = self.theorem_query_func(dataset, problem_id, hole_id)
for theorem in available_theorems:
tactics.append(f"rw[{theorem}]")
return tactics
def _ensure_node_state_is_valid(self, node: SearchNode):
"""Ensure node state is valid - now handled transparently by ProofStateCache"""
if node.proof_state_id is None:
raise RuntimeError(f"Node {node.node_id} has no proof state ID")
# ProofStateCache will automatically handle state availability checking and recovery
# We only need to ensure the state ID exists
return
def search_hole(self, pickle_info: HolePickleInfo, initial_proof_state_id: int, loader_func=None,
dataset: str = "unknown", problem_id: str = "unknown") -> Tuple[List[List[str]], Dict]:
"""Search for solutions to a specific hole"""
print(f" π Starting n-gram search for {pickle_info.hole_id} (PS_{initial_proof_state_id})")
start_time = time.time()
# Reset state for this hole
self.nodes.clear()
self.successful_paths.clear()
self.visited_states.clear() # Reset state deduplication for this hole
print(f" π Reset visited states for hole {pickle_info.hole_id}")
# Initialize results_dict early so it can be returned at any time
results_dict = {
'hole_id': pickle_info.hole_id,
'successful_paths': self.successful_paths.copy(),
'total_nodes': 0, # Will be updated before final return
'search_time_seconds': 0.0, # Will be updated before final return
'nodes_by_status': {}, # Will be updated before final return
'max_depth_reached': 0,
'search_completed': False,
'stop_on_first_success': self.stop_on_first_success,
'explored_nodes_log': [] # Initialize empty, will be populated
}
# Create root node - get initial goal count
root_id = f"{pickle_info.hole_id}_root"
initial_goals = 1 # Default assumption for a hole
# Try to get actual goal count from Lean server
if self.lean_integrator and self.lean_integrator.lean_server:
try:
from lean_interact import ProofStateInfo
state_info = self.lean_integrator.lean_server.run(ProofStateInfo(proof_state=initial_proof_state_id))
if hasattr(state_info, 'goals'):
initial_goals = len(state_info.goals)
print(f" π Root node has {initial_goals} goals")
except Exception as e:
print(f" β οΈ Could not get initial goal count: {e}")
root_node = SearchNode(
node_id=root_id,
tactic_sequence=[],
parent_id=None,
depth=0,
proof_state_id=initial_proof_state_id,
status=NodeStatus.PENDING,
remaining_goals=initial_goals
)
self.nodes[root_id] = root_node
self.explored_nodes_log.append({
'node_id': root_node.node_id,
'tactic_sequence': root_node.tactic_sequence,
'depth': root_node.depth,
'status': root_node.status.value
})
# Record initial state in visited states to avoid revisiting it
if not self._is_state_visited(initial_proof_state_id):
print(f" π Recorded initial state for hole {pickle_info.hole_id}")
else:
print(f" β οΈ Initial state already visited (unexpected)")
# Search iteratively by depth
nodes_to_process = [root_node]
current_depth = 0
while nodes_to_process and current_depth < self.max_depth:
print(f" π Depth {current_depth}: processing {len(nodes_to_process)} nodes")
# Check for global success before processing current depth's nodes
if self.stop_on_first_success and self.successful_paths:
print(f" π Stopping early (success found at previous depth)")
break
next_nodes = []
for node in nodes_to_process:
# Ensure node state is valid (now handled transparently by cache)
self._ensure_node_state_is_valid(node)
# If node is terminal, skip
if node.is_terminal():
continue
children = self._expand_node(node, pickle_info.hole_id, dataset, problem_id)
next_nodes.extend(children)
# Check for success immediately
for child in children:
if child.status == NodeStatus.SUCCESS:
self.successful_paths.append(child.tactic_sequence.copy())
print(f" π Success: {' ; '.join(child.tactic_sequence)}")
# If stop_on_first_success, immediately break all loops and return
if self.stop_on_first_success:
print(f" π Stopping early (first success found at current depth)")
# Populate final stats before returning including LLM and state deduplication stats
llm_pruned_count = sum(1 for n in self.nodes.values() if n.llm_pruned)
state_duplicate_pruned_count = sum(1 for n in self.nodes.values() if n.state_duplicate_pruned)
llm_stats = {}
if self.enable_llm_pruning and self.llm_pruner:
llm_stats = self.llm_pruner.get_pruning_stats()
results_dict['successful_paths'] = self.successful_paths.copy()
results_dict['total_nodes'] = len(self.nodes)
results_dict['search_time_seconds'] = time.time() - start_time
results_dict['nodes_by_status'] = {s.value: sum(1 for n in self.nodes.values() if n.status == s) for s in NodeStatus}
results_dict['max_depth_reached'] = max(n.depth for n in self.nodes.values()) if self.nodes else 0
results_dict['search_completed'] = True
results_dict['explored_nodes_log'] = self.explored_nodes_log
results_dict['llm_pruning_enabled'] = self.enable_llm_pruning
results_dict['llm_pruned_nodes'] = llm_pruned_count
results_dict['llm_pruning_stats'] = llm_stats
results_dict['state_deduplication_enabled'] = True
results_dict['state_duplicate_pruned_nodes'] = state_duplicate_pruned_count
results_dict['total_visited_states'] = len(self.visited_states)
return self.successful_paths.copy(), results_dict
# If stop_on_first_success and a path was just found, break outer 'for node in nodes_to_process' loop
if self.stop_on_first_success and self.successful_paths:
break
# If stop_on_first_success and a path was found in this depth, break main 'while' loop
if self.stop_on_first_success and self.successful_paths:
break
# Prepare for next depth
nodes_to_process = [n for n in next_nodes if not n.is_terminal()]
current_depth += 1
# Generate results (if not already returned due to stop_on_first_success)
search_time = time.time() - start_time
# Count nodes by status
status_counts = {}
max_depth_reached = 0
llm_pruned_count = 0
state_duplicate_pruned_count = 0
for node in self.nodes.values():
status = node.status.value
status_counts[status] = status_counts.get(status, 0) + 1
max_depth_reached = max(max_depth_reached, node.depth)
if node.llm_pruned:
llm_pruned_count += 1
if node.state_duplicate_pruned:
state_duplicate_pruned_count += 1
# Add LLM pruning statistics
llm_stats = {}
if self.enable_llm_pruning and self.llm_pruner:
llm_stats = self.llm_pruner.get_pruning_stats()
results_dict.update({
'successful_paths': self.successful_paths.copy(),
'total_nodes': len(self.nodes),
'search_time_seconds': search_time,
'nodes_by_status': status_counts,
'max_depth_reached': max_depth_reached,
'search_completed': True,
'explored_nodes_log': self.explored_nodes_log,
'llm_pruning_enabled': self.enable_llm_pruning,
'llm_pruned_nodes': llm_pruned_count,
'llm_pruning_stats': llm_stats,
'state_deduplication_enabled': True,
'state_duplicate_pruned_nodes': state_duplicate_pruned_count,
'total_visited_states': len(self.visited_states)
})
print(f" π Search complete: {len(self.successful_paths)} paths, {len(self.nodes)} nodes, {search_time:.2f}s")
print(f" π State deduplication: {state_duplicate_pruned_count} nodes pruned, {len(self.visited_states)} unique states")
if self.enable_llm_pruning and llm_pruned_count > 0:
print(f" π€ LLM pruning: {llm_pruned_count} nodes pruned")
if llm_stats:
print(f" π€ LLM stats: {llm_stats['total_queries']} queries, {llm_stats['prune_rate']:.2%} prune rate")
return self.successful_paths.copy(), results_dict
def _get_proof_state_signature(self, proof_state_id: int) -> Optional[ProofStateSignature]:
"""Extract a signature from a proof state for deduplication"""
if not self.lean_integrator or not self.lean_integrator.lean_server or proof_state_id is None:
return None
try:
# Use ProofStep with skip to get current state information
from lean_interact import ProofStep
response = self.lean_integrator.lean_server.run(ProofStep(
proof_state=proof_state_id,
tactic="skip" # No-op tactic to get current state
))
# Extract goals and hypotheses information
if hasattr(response, 'goals') and response.goals:
# Create a simplified hash of the goals and hypotheses
goals_str = ""
for i, goal in enumerate(response.goals[:3]): # Limit to first 3 goals for performance
goal_str = str(goal)
# Extract hypothesis and goal parts
if "β’" in goal_str:
hypothesis_part, goal_part = goal_str.split("β’", 1)
goals_str += f"G{i}[H:{hash(hypothesis_part.strip())}|G:{hash(goal_part.strip())}]"
else:
goals_str += f"G{i}[{hash(goal_str)}]"
# Create signature with combined information
import hashlib
combined_hash = hashlib.md5(goals_str.encode()).hexdigest()[:16]
return ProofStateSignature(
hypothesis_hash=combined_hash[:8],
goal_hash=combined_hash[8:]
)
else:
# No goals means proof is complete
return ProofStateSignature(hypothesis_hash="complete", goal_hash="complete")
except Exception as e:
# If we can't get signature, don't block the search
print(f" β οΈ Failed to get proof state signature: {e}")
return None
def _is_state_visited(self, proof_state_id: int) -> bool:
"""Check if a proof state has been visited before"""
signature = self._get_proof_state_signature(proof_state_id)
if signature is None:
return False # If we can't get signature, allow the search to continue
if signature in self.visited_states:
return True
# Add to visited states
self.visited_states.add(signature)
return False
def _expand_node(self, node: SearchNode, hole_id: str, dataset: str = "unknown", problem_id: str = "unknown") -> List[SearchNode]:
"""Expand a node by trying all available tactics including dynamic rw[theorem] tactics"""
if node.depth >= self.max_depth:
return []
children = []
# Get dynamic tactics for this specific hole
available_tactics = self.get_available_tactics_for_hole(dataset, problem_id, hole_id)
for tactic in available_tactics:
child_sequence = node.tactic_sequence + [tactic]
child_id = f"{hole_id}_{'_'.join(child_sequence)}"
if child_id in self.nodes:
continue
child_node = SearchNode(
node_id=child_id,
tactic_sequence=child_sequence,
parent_id=node.node_id,
depth=node.depth + 1
)
# Execute just the new tactic on the parent's state
if node.proof_state_id is not None:
self._execute_single_tactic(child_node, node.proof_state_id)
else:
child_node.status = NodeStatus.ERROR
child_node.error_message = "Parent node has no proof state"
# LLM Pruning: Check if we should prune incomplete nodes (skip if already pruned by state deduplication)
if (self.enable_llm_pruning and
self.llm_pruner and
child_node.status == NodeStatus.INCOMPLETE and
not child_node.state_duplicate_pruned and # Short circuit: skip LLM if already pruned by state deduplication
node.proof_state_id is not None):
try:
print(f" π€ LLM pruning check for {child_node.node_id}...")
# Get detailed goal information for better LLM decisions
def get_goal_details(proof_state_id):
if self.lean_integrator and self.lean_integrator.lean_server and proof_state_id is not None:
try:
# Execute a dummy tactic to get goal information
from lean_interact import ProofStep
response = self.lean_integrator.lean_server.run(ProofStep(
proof_state=proof_state_id,
tactic="skip" # No-op tactic to just get the current state
))
if hasattr(response, 'goals'):
goals = response.goals
if goals:
# Truncate each goal for LLM readability
goal_summaries = []
for i, goal in enumerate(goals[:2]): # Max 2 goals
goal_str = str(goal)
goal_summaries.append(f"Goal {i+1}: {goal_str}")
return f"{len(goals)} goals:\n" + "\n".join(goal_summaries)
else:
return "0 goals (solved)"
except Exception as e:
# If skip fails, goal might be unsolved, try a safer approach
return f"Could not access goal details: {str(e)[:100]}"
return "No goal information available"
# Prepare detailed state information for LLM
pre_goal_details = get_goal_details(node.proof_state_id)
post_goal_details = get_goal_details(child_node.proof_state_id)
pre_state_info = {
"remaining_goals": node.remaining_goals,
"goals_summary": f"BEFORE:\n{pre_goal_details}"
}
post_state_info = {
"remaining_goals": child_node.remaining_goals,
"goals_summary": f"AFTER '{tactic}':\n{post_goal_details}"
}
# Get LLM decision
decision = self.llm_pruner.should_prune_node(
tactic_sequence=child_node.tactic_sequence,
pre_state_info=pre_state_info,
post_state_info=post_state_info,
hole_id=hole_id
)
if decision.should_prune:
print(f" π€ LLM PRUNE: {child_node.node_id} - {decision.reasoning}")
child_node.llm_pruned = True
else:
print(f" π€ LLM CONTINUE: {child_node.node_id} - {decision.reasoning}")
except Exception as e:
print(f" β LLM pruning failed for {child_node.node_id}: {e}")
# Continue without pruning if LLM fails
self.nodes[child_id] = child_node
node.children.add(child_id)
children.append(child_node)
self.explored_nodes_log.append({
'node_id': child_node.node_id,
'tactic_sequence': child_node.tactic_sequence,
'depth': child_node.depth,
'status': child_node.status.value,
'llm_pruned': child_node.llm_pruned,
'state_duplicate_pruned': child_node.state_duplicate_pruned
}) # Collect data for logging
# If we just found a success and we are stopping on first success, break early.
if child_node.status == NodeStatus.SUCCESS and self.stop_on_first_success:
print(f" π _expand_node stopping early: {child_node.node_id} is SUCCESS.")
break # Break out of the inner loop (for tactic in self.available_tactics)
return children
def _execute_single_tactic(self, node: SearchNode, parent_proof_state_id: int):
"""Executes the last tactic of a node's sequence and updates the node."""
if not self.lean_integrator or not self.lean_integrator.lean_server:
node.status = NodeStatus.ERROR
node.error_message = "No Lean server available"
return
tactic_to_execute = node.get_last_tactic()
if not tactic_to_execute:
node.status = NodeStatus.ERROR
node.error_message = "Node has no tactic to execute."
return
try:
from lean_interact import ProofStep
response = self.lean_integrator.lean_server.run(ProofStep(
proof_state=parent_proof_state_id,
tactic=tactic_to_execute
))
from lean_interact.interface import LeanError
if isinstance(response, LeanError):
node.status = NodeStatus.ERROR
node.error_message = str(response.message)
elif hasattr(response, 'goals') and hasattr(response, 'proof_state'):
if len(response.goals) == 0:
node.status = NodeStatus.SUCCESS
node.proof_state_id = None # No need for proof state after success
node.remaining_goals = 0
else:
node.status = NodeStatus.INCOMPLETE
node.proof_state_id = response.proof_state
node.remaining_goals = len(response.goals)
# State deduplication check: If this state has been visited before, prune this node
if self._is_state_visited(response.proof_state):
print(f" π State deduplication: {node.node_id} leads to visited state, pruning")
node.state_duplicate_pruned = True
node.status = NodeStatus.INCOMPLETE # Keep as incomplete but mark as pruned
else:
node.status = NodeStatus.ERROR
node.error_message = "Invalid response from Lean server"
except Exception as e:
node.status = NodeStatus.ERROR
node.error_message = f"Execution failed: {str(e)}"
# State now managed by ProofStateCache, no need to manually track server generation
def _execute_tactic_sequence(self, node: SearchNode, base_proof_state_id: int):
"""
DEPRECATED but kept for reference.
Executes a full tactic sequence. Inefficient for expansion.
"""
if not self.lean_integrator or not self.lean_integrator.lean_server:
node.status = NodeStatus.ERROR
node.error_message = "No Lean server available"
return
try:
# For executing a sequence, we must start from the root proof state
# and apply each tactic sequentially.
current_proof_state_id = base_proof_state_id
for i in range(len(node.tactic_sequence)):
tactic = node.tactic_sequence[i]
# Execute single tactic on current proof state
try:
from lean_interact import ProofStep
response = self.lean_integrator.lean_server.run(ProofStep(
proof_state=current_proof_state_id,
tactic=tactic
))
# Check response
if hasattr(response, 'error') and response.error:
node.status = NodeStatus.ERROR
node.error_message = str(response.error)
return
# Check if goals are solved
if hasattr(response, 'goals'):
if len(response.goals) == 0:
# All goals solved - success!
node.status = NodeStatus.SUCCESS
node.remaining_goals = 0
if hasattr(response, 'proof_state'):
node.proof_state_id = response.proof_state
# If this is the last tactic, we are done with the node.
# If not, it means an intermediate tactic solved the goal, which is also a success for the sequence.
return
else:
# Still have goals, continue to next tactic in the sequence
node.remaining_goals = len(response.goals)
if hasattr(response, 'proof_state'):
current_proof_state_id = response.proof_state
# Don't set the node's proof_state_id until the full sequence is run
else:
# If there's no next proof state, the sequence is broken
node.status = NodeStatus.ERROR
node.error_message = "No proof_state in response to continue sequence."
return
else:
node.status = NodeStatus.ERROR
node.error_message = "No goals information in response"
return
except Exception as tactic_error:
node.status = NodeStatus.ERROR
node.error_message = f"Tactic '{tactic}' failed: {str(tactic_error)}"
return
# If we get here, the full sequence executed but goals remain
node.status = NodeStatus.INCOMPLETE
node.proof_state_id = current_proof_state_id # Final state after full sequence
except Exception as e:
node.status = NodeStatus.ERROR
node.error_message = f"Execution failed: {str(e)}"
def demo_clean_searcher():
"""Demo the clean searcher"""
print("π Clean N-gram Searcher Demo")
print("=" * 40)
# Demo without LLM pruning
searcher = CleanNgramSearcher(max_depth=2, stop_on_first_success=True, enable_llm_pruning=False)
print("β
Clean searcher initialized")
print(f" Max depth: {searcher.max_depth}")
print(f" Stop on first success: {searcher.stop_on_first_success}")
print(f" LLM pruning: {searcher.enable_llm_pruning}")
print(f" Base tactics: {len(searcher.available_tactics)}")
print(f" Terminal tactics: {searcher.terminal_tactics}")
# Demo dynamic tactics
demo_tactics = searcher.get_available_tactics_for_hole("putnam", "putnam_1986_b6", "hole_4")
rw_tactics = [t for t in demo_tactics if t.startswith('rw[')]
print(f" Dynamic rw[theorem] tactics: {rw_tactics}")
print(f" Total tactics with dynamic: {len(demo_tactics)}")
# Demo with LLM pruning
try:
searcher_llm = CleanNgramSearcher(max_depth=2, stop_on_first_success=True, enable_llm_pruning=True)
print(f"\nπ€ LLM-enhanced searcher initialized")
print(f" LLM pruning: {searcher_llm.enable_llm_pruning}")
print(f" LLM pruner model: {searcher_llm.llm_pruner.model if searcher_llm.llm_pruner else 'N/A'}")
except Exception as e:
print(f"\nβ LLM searcher failed to initialize: {e}")
print(" Make sure OPENROUTER_API_KEY is set")
print(f"\nπ‘ Usage:")
print(f" searcher = CleanNgramSearcher(enable_llm_pruning=True)")
print(f" searcher.lean_integrator = integrator")
print(f" paths, results = searcher.search_hole(pickle_info, 42, None, 'dataset', 'problem')")
print(f"\nπ― Key Features:")
print(f" - Dynamic rw[theorem] tactic generation")
print(f" - Context-aware theorem selection")
print(f" - LLM-based intelligent pruning")
print(f" - Complete state isolation between holes")
print(f" - Automatic cleanup after each hole")
print(f" - Pure pickle-based approach")
if __name__ == "__main__":
demo_clean_searcher()