Spaces:
Sleeping
Sleeping
File size: 14,540 Bytes
8c75600 361f093 8c75600 361f093 8c75600 361f093 8c75600 361f093 8c75600 361f093 8c75600 | 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 | """Deterministic step-by-step playthrough for all LeanMigrate tasks."""
from __future__ import annotations
import argparse
import sys
from collections.abc import Sequence
from pathlib import Path
from rich.columns import Columns
from rich.console import Console
from rich.panel import Panel
from rich.rule import Rule
from rich.syntax import Syntax
from rich.text import Text
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from lean_migrate.env.models import ( # noqa: E402 # type: ignore
AnalyzeDepsAction,
InspectAction,
RunTestsAction,
SubmitAction,
)
from lean_migrate.env.grader import clamp_open_unit # noqa: E402 # type: ignore
from lean_migrate.env.target_snippets import ( # noqa: E402 # type: ignore
TASK_TARGET_SNIPPETS,
build_submission_bundle,
)
from lean_migrate.env.tasks import get_task, list_tasks # noqa: E402 # type: ignore
from lean_migrate.env.verification_ir import ( # noqa: E402 # type: ignore
BehaviorSummary,
VerificationIRResult,
build_verification_ir,
)
from lean_migrate.server.lean_migrate_environment import ( # noqa: E402 # type: ignore
LeanMigrateEnvironment,
)
TASK_IDS = tuple(task_info["task_id"] for task_info in list_tasks())
console = Console()
def _short_feedback(text: str | None, max_lines: int = 12) -> str:
if not text:
return ""
lines = text.strip().splitlines()
if len(lines) <= max_lines:
return "\n".join(lines)
return "\n".join(lines[:max_lines] + ["..."])
def _short_digest(text: str | None, width: int = 16) -> str:
if not text:
return "(n/a)"
if len(text) <= width:
return text
return f"{text[:width]}..."
def _syntax_renderable(code: str, language: str) -> Syntax | Text:
try:
return Syntax(code, language, theme="monokai", line_numbers=False)
except Exception:
return Text(code)
def _print_code_preview(title: str, code: str, language: str) -> None:
console.print(
Panel(
_syntax_renderable(code, language),
title=f"[bold green]{title}[/]",
border_style="green",
)
)
def _summary_text(label: str, summary: BehaviorSummary | None) -> Text:
if summary is None:
return Text("(unavailable)", style="dim")
rendered = Text()
rendered.append(f"{label}\n", style="bold cyan")
for field_name, value in [
("task", summary.task_id),
("function", summary.function_name),
("language", summary.target_language),
("arity", str(summary.arity)),
("samples", str(summary.sample_count)),
("passed", str(summary.passed_count)),
("digest", _short_digest(summary.behavior_digest)),
]:
rendered.append(f" {field_name}: ", style="bold cyan")
rendered.append(f"{value}\n")
return rendered
def _print_ir_preview(
task_id: str,
function_name: str,
target_language: str,
backend_name: str,
ir_result: VerificationIRResult,
) -> None:
status_style = "green" if ir_result.ready else "red"
summary = Text()
summary.append("backend: ", style="bold cyan")
summary.append(f"{backend_name}\n")
summary.append("path: ", style="bold cyan")
summary.append(f"{target_language} -> IR -> Lean\n")
summary.append("ready: ", style="bold cyan")
summary.append(f"{str(ir_result.ready).lower()}\n")
if ir_result.provenance is not None:
summary.append("parser: ", style="bold cyan")
summary.append(f"{ir_result.provenance.parser}\n")
summary.append("signature: ", style="bold cyan")
summary.append(
f"{ir_result.provenance.normalized_signature or '(unavailable)'}\n"
)
summary.append("arity: ", style="bold cyan")
summary.append(
f"{ir_result.provenance.arity if ir_result.provenance.arity is not None else '(n/a)'}\n"
)
summary.append("body hash: ", style="bold cyan")
summary.append(f"{_short_digest(ir_result.provenance.normalized_body_hash)}\n")
summary.append("source digest: ", style="bold cyan")
summary.append(f"{_short_digest(ir_result.provenance.source_digest)}\n")
if ir_result.run_result is not None:
summary.append("samples: ", style="bold cyan")
summary.append(
f"{ir_result.run_result.tests_passed}/{ir_result.run_result.tests_total}\n"
)
if ir_result.run_result.case_results is not None:
summary.append("case traces: ", style="bold cyan")
summary.append(f"{len(ir_result.run_result.case_results)}\n")
if ir_result.submission_summary is not None:
summary.append("behavior digest: ", style="bold cyan")
summary.append(
f"{_short_digest(ir_result.submission_summary.behavior_digest)}\n"
)
console.print(
Panel(
summary,
title=f"[bold magenta]IR PREVIEW[/] {task_id}:{function_name}",
border_style=status_style,
)
)
if ir_result.feedback:
console.print(
Panel(
Text(ir_result.feedback),
title="[bold magenta]IR TRACE[/]",
border_style=status_style,
)
)
if (
ir_result.submission_summary is not None
and ir_result.expected_summary is not None
):
console.print(
Columns(
[
Panel(
_summary_text(
"Submission summary", ir_result.submission_summary
),
border_style="cyan",
title="[bold cyan]SUBMISSION[/]",
),
Panel(
_summary_text("Expected summary", ir_result.expected_summary),
border_style="green",
title="[bold green]EXPECTED[/]",
),
],
expand=True,
)
)
if ir_result.lean_code:
_print_code_preview("GENERATED LEAN", ir_result.lean_code, "lean")
def _print_step_input(action_label: str, **fields: str | None) -> None:
rows = []
for field_name, value in fields.items():
if value is None:
continue
if "\n" in value:
rows.append(
Text.assemble(
(f"{field_name}\n", "bold cyan"),
(value.rstrip(), ""),
)
)
else:
rows.append(Text.assemble((f"{field_name}: ", "bold cyan"), value))
body = Text()
for index, row in enumerate(rows):
body.append_text(row)
if index != len(rows) - 1:
body.append("\n\n")
console.print(
Panel(
body if rows else Text("(no inputs)", style="dim"),
title=f"[bold blue]INPUT[/] {action_label}",
border_style="blue",
)
)
def _print_observation(action_label: str, observation) -> None:
reward = clamp_open_unit(float(observation.reward or 0.0))
status_style = (
"green"
if observation.reward_details
and observation.reward_details.feedback.startswith("VERIFIED")
else "yellow"
)
summary = Text.assemble(
("reward=", "bold cyan"),
f"{reward:.3f} ",
("progress=", "bold cyan"),
f"{clamp_open_unit(float(observation.progress)):.3f} ",
("done=", "bold cyan"),
str(observation.done).lower(),
)
console.print(
Panel(
summary,
title=f"[bold {status_style}]ACTION[/] {action_label}",
border_style=status_style,
)
)
feedback = _short_feedback(getattr(observation.reward_details, "feedback", None))
if feedback:
if "def " in feedback or feedback.lstrip().startswith(
("function ", "from ", "import ")
):
syntax_language = (
"python"
if feedback.lstrip().startswith(("def ", "from ", "import "))
else "javascript"
)
feedback_renderable = Syntax(
feedback, syntax_language, theme="monokai", line_numbers=False
)
else:
feedback_renderable = Text(feedback)
console.print(
Panel(
feedback_renderable,
title="[bold magenta]FEEDBACK[/]",
border_style="magenta",
)
)
def _assert_verified(task_id: str, function_name: str, observation) -> None:
if function_name not in observation.verified:
raise RuntimeError(
f"submit failed for {task_id}:{function_name}; "
f"verified={observation.verified!r}"
)
def _run_task(task_id: str, verbose: bool = True) -> float:
task = get_task(task_id)
env = LeanMigrateEnvironment()
observation = env.reset(task_id=task_id)
backend_name = type(env._state.backend).__name__
verified_target_snippets: dict[str, str] = {}
target_snippets = TASK_TARGET_SNIPPETS[task_id]
if verbose:
console.print(Rule(f"[bold white]{task.display_name}[/] [dim]({task_id})[/]"))
console.print(
Panel(
Text.assemble(
("Episode: ", "bold cyan"),
observation.episode_id,
"\n",
("Order: ", "bold cyan"),
", ".join(task.topo_order),
),
border_style="white",
title="[bold white]TASK[/]",
)
)
for function_name in task.topo_order:
function_spec = task.get_function(function_name)
if function_spec is None:
continue
if verbose:
console.print(Rule(f"[bold yellow]STEP[/] [white]{function_name}[/]"))
_print_step_input(f"inspect({function_name})", function_name=function_name)
observation = env.step(
InspectAction(type="inspect", function_name=function_name)
)
if verbose:
_print_observation(f"inspect({function_name})", observation)
observation = env.step(
AnalyzeDepsAction(type="analyze_deps", function_name=function_name)
)
if verbose:
_print_observation(f"analyze_deps({function_name})", observation)
target_code = None
if not function_spec.is_proof_required:
target_code = build_submission_bundle(
task,
function_name,
verified_target_snippets,
target_snippets[function_name],
)
if verbose:
_print_step_input(
f"run_tests({function_name})",
function_name=function_name,
candidate_code=target_code,
)
observation = env.step(
RunTestsAction(
type="run_tests",
function_name=function_name,
candidate_code=target_code,
)
)
if verbose:
_print_observation(f"run_tests({function_name})", observation)
if verbose:
_print_step_input(
f"submit_proof({function_name})"
if function_spec.is_proof_required
else f"submit_target_code({function_name})",
function_name=function_name,
target_code=target_code,
lean_proof=function_spec.lean_fragment
if function_spec.is_proof_required
else None,
)
if function_spec.is_proof_required:
_print_code_preview(
"LEAN PROOF PREVIEW",
function_spec.lean_fragment or "",
"lean",
)
else:
ir_result = build_verification_ir(
task, function_spec, target_code or ""
)
_print_ir_preview(
task_id,
function_name,
task.target_language,
backend_name,
ir_result,
)
submit_action = SubmitAction(
type="submit",
function_name=function_name,
target_code=target_code,
lean_proof=function_spec.lean_fragment
if function_spec.is_proof_required
else None,
)
observation = env.step(submit_action)
if verbose:
_print_observation(
f"submit_proof({function_name})"
if function_spec.is_proof_required
else f"submit({function_name})",
observation,
)
_assert_verified(task_id, function_name, observation)
if not function_spec.is_proof_required:
verified_target_snippets[function_name] = target_snippets[function_name]
if verbose:
console.print(
Panel(
Text.assemble(
("Completed: ", "bold green"),
task_id,
"\n",
("progress=", "bold cyan"),
f"{clamp_open_unit(float(observation.progress)):.3f}",
),
border_style="green",
)
)
return clamp_open_unit(float(observation.progress))
def main(argv: Sequence[str] | None = None) -> None:
parser = argparse.ArgumentParser(
description="Play all LeanMigrate tasks one step at a time."
)
parser.add_argument(
"--tasks",
nargs="+",
default=TASK_IDS,
help="Task ids to play in order.",
)
parser.add_argument(
"--quiet",
action="store_true",
help="Reduce output to per-task summaries.",
)
args = parser.parse_args(argv)
scores: dict[str, float] = {}
for task_id in args.tasks:
scores[task_id] = _run_task(task_id, verbose=not args.quiet)
console.print(Rule("[bold white]PLAYTHROUGH RESULTS[/]"))
overall = sum(scores.values()) / len(scores) if scores else 0.0
for task_id, score in scores.items():
console.print(f"[bold cyan]{task_id:<20}[/] {score:.3f}")
console.print(f"[bold cyan]{'overall':<20}[/] {overall:.3f}")
if __name__ == "__main__":
main()
|