File size: 31,259 Bytes
31acd63 | 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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | """Miner3-v9: fast Luna drafts with evidence-gated differential repair."""
import ast
import hashlib
import json
import os
import re
import resource
import signal
import subprocess
import sys
import tempfile
import time
from decimal import Decimal, InvalidOperation
_FORMAT = "miner3-evidence-gated-v9"
_PRIMARY = "openai/gpt-5.6-luna"
_TOOLS_MODEL = "openai/gpt-5.6-luna"
_DIVERSE = "google/gemini-3.6-flash"
_CONFIRM = "google/gemini-3.6-flash"
_EXPECTED_TASKS = 6
_OUTPUT_LIMIT = 4 * 1024 * 1024
_CASE_TIMEOUT_S = 7.0
_GENERATOR_TIMEOUT_S = 5.0
_PERF_GENERATOR_TIMEOUT_S = 8.0
_PERF_TIMEOUT_S = 7.0
_PERF_TARGET_S = 2.5
_PERF_SIZE = 200000
_MAX_EVIDENCE_INPUT = 16384
_MAX_EVIDENCE_OUTPUT = 4096
_CHILD_CPU_S = 8
_CHILD_AS_BYTES = 1 << 30
_CHILD_NPROC = 16
_CHILD_NOFILE = 32
_SIZES = (2, 3, 5, 8, 13, 21, 34, 40)
_MAX_TOOL_BYTES = 64 * 1024
_SAFE_IMPORTS = frozenset((
"array", "bisect", "collections", "copy", "dataclasses", "decimal",
"fractions", "functools", "heapq", "itertools", "math", "operator",
"random", "re", "statistics", "string", "sys", "typing",
))
_BLOCKED_NAMES = frozenset((
"__import__", "breakpoint", "compile", "delattr", "dir", "eval", "exec",
"getattr", "globals", "help", "locals", "open", "setattr", "vars",
))
_BLOCKED_ATTRIBUTES = frozenset((
"fork", "forkpty", "kill", "meta_path", "modules", "path", "path_hooks",
"popen", "posix_spawn", "setprofile", "settrace", "spawn", "system",
))
_BLOCKED_TEXT = (
"/dev", "/etc", "/proc", "/root", "/run", "openrouter", "hf_token",
"hugging_face",
"subprocess", "multiprocessing", "socket", "__import__",
)
_SAMPLE = re.compile(r"^Sample (Input|Output)\s*(\d+)\s*$", re.MULTILINE)
_FENCE = re.compile(r"```(?:python|py)?\s*\n(.*?)```", re.DOTALL | re.IGNORECASE)
_TOOLS = re.compile(r"```(oracle|generator)\s*\n(.*?)```", re.DOTALL | re.IGNORECASE)
_UNSAFE = re.compile(
r"\b(?:subprocess|multiprocessing|socket)\b|"
r"\bos\s*\.\s*(?:fork|forkpty|posix_spawn|system|popen)\b|"
r"\bpty\s*\.\s*spawn\b",
re.IGNORECASE,
)
_DRAFT_GUIDANCE = " ".join((
"Derive the algorithm from the complete specification and maximum constraints.",
"Privately construct a small direct specification and try to falsify the proposed algorithm.",
"Check ordering, multiplicity, repeated values, boundaries, state changes, and complexity.",
"Trace every published example. The execution harness compares output tokens exactly even when the problem prose grants mathematical tolerance.",
"Match the required canonical representation, precision, rounding, ordering, and separators shown by the statement and examples.",
"Do not print extra precision merely because it is available.",
"Return only one complete raw Python 3 program without Markdown, fences, or explanation.",
))
_TOOLS_GUIDANCE = " ".join((
"Work independently from the statement and do not assume any candidate implementation.",
"Write a correctness-first oracle for small legal inputs using direct simulation or exhaustive search.",
"Also write a generator accepting seed and size arguments, seeding Python random, and printing one varied legal input.",
"Systematically include legal ties and equalities, repeated values, minimum counts, endpoints, mandatory final transitions, and choices where an equal local action changes a later opportunity.",
"Vary these boundary families from the seed instead of emitting a fixed example.",
"The oracle must not reuse the efficient algorithm requested by the statement.",
"The execution harness compares output tokens exactly even when the prose permits numerical tolerance.",
"Reproduce every published output token exactly and infer one canonical representation for unseen outputs from the statement and examples without inventing extra precision.",
"Return exactly two fenced blocks named oracle and generator, with no other text.",
))
_CONFIRM_GUIDANCE = " ".join((
"Independently derive a small-input reference program from the complete statement.",
"Use direct simulation or exhaustive search rather than the intended efficient algorithm.",
"Read the original input format and print the original output format.",
"The execution harness compares output tokens exactly even when the prose permits numerical tolerance.",
"Reproduce every published output token exactly and infer one canonical representation for unseen outputs from the statement and examples without inventing extra precision.",
"Return one raw complete Python 3 program without Markdown or explanation.",
))
_REPAIR_GUIDANCE = " ".join((
"Executed evidence disproved the program.",
"Re-derive a general algorithm from the complete statement and constraints.",
"Do not patch, fingerprint, or special-case the failing input.",
"Return only one complete raw Python 3 program without Markdown, fences, or explanation.",
))
_FORMAT_REPAIR_GUIDANCE = " ".join((
"Execution shows that the algorithm is numerically acceptable but its output representation violates the exact-token judge contract.",
"Infer one general canonical precision, rounding, and formatting rule from the complete statement, examples, and independently confirmed expected tokens.",
"Do not hardcode, fingerprint, or special-case the failing input or any observed numeric value.",
"Return only one complete raw Python 3 program without Markdown, fences, or explanation.",
))
_PERFORMANCE_GUIDANCE = " ".join((
"The program is correct on checked cases but execution showed that it is too slow at large scale.",
"Replace it with an asymptotically faster general algorithm while preserving the input and output contract.",
"Use buffered input and batched output where appropriate.",
"Return only one complete raw Python 3 program without Markdown, fences, or explanation.",
))
_NUMERIC_GUIDANCE = " ".join((
"Solve in the requested units and privately verify arithmetic, signs, rounding, and boundary assumptions.",
"Put only the final numeric result on the last line.",
))
_INCONCLUSIVE = object()
def _is_code(text):
value = str(text)
return (
"Write a complete Python 3 program" in value
and "standard input" in value
and "standard output" in value
)
def _is_choice(text):
body = "\n" + str(text)
return all("\n" + letter + ")" in body for letter in "ABCD")
def _samples(prompt, maximum):
text = str(prompt).replace("\r\n", "\n").replace("\r", "\n")
marks = [(m.start(), m.end(), m.group(1), m.group(2)) for m in _SAMPLE.finditer(text)]
blocks = {}
for index, (_start, end, kind, number) in enumerate(marks):
stop = marks[index + 1][0] if index + 1 < len(marks) else len(text)
body = text[end:stop].strip("\n")
if kind == "Output":
body = body.split("\n\n", 1)[0]
blocks.setdefault(number, {})[kind] = body.strip("\n")
pairs = []
for number in sorted(blocks, key=lambda value: int(value) if value.isdigit() else value):
row = blocks[number]
if row.get("Input", "").strip() and "Output" in row:
pairs.append((row["Input"] + "\n", row["Output"]))
return pairs[:maximum]
def _program(response):
value = str(response or "")
match = _FENCE.search(value)
return (match.group(1) if match else value).strip()
def _tool_blocks(response):
value = str(response or "")
matches = _TOOLS.findall(value)
if len(matches) != 2 or _TOOLS.sub("", value).strip():
return {}
names = [name.lower() for name, _code in matches]
if sorted(names) != ["generator", "oracle"]:
return {}
blocks = {}
for name, code in matches:
encoded = code.encode("utf-8", "replace")
if not code.strip() or len(encoded) > _MAX_TOOL_BYTES:
return {}
blocks[name.lower()] = code.strip()
return blocks
def _limits(): # pragma: no cover - subprocess only
resource.setrlimit(resource.RLIMIT_CPU, (_CHILD_CPU_S, _CHILD_CPU_S))
resource.setrlimit(resource.RLIMIT_AS, (_CHILD_AS_BYTES, _CHILD_AS_BYTES))
resource.setrlimit(resource.RLIMIT_NPROC, (_CHILD_NPROC, _CHILD_NPROC))
resource.setrlimit(resource.RLIMIT_NOFILE, (_CHILD_NOFILE, _CHILD_NOFILE))
resource.setrlimit(resource.RLIMIT_FSIZE, (_OUTPUT_LIMIT, _OUTPUT_LIMIT))
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
os.setsid()
if os.geteuid() == 0:
try:
os.setgroups([])
except PermissionError:
pass
os.setgid(65534)
os.setuid(65534)
def _kill_group(process):
try:
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
except Exception: # noqa: BLE001
try:
process.kill()
except Exception: # noqa: BLE001
pass
def _safe_code(code):
value = str(code)
if not value.strip() or _UNSAFE.search(value):
return False
try:
tree = ast.parse(value)
except SyntaxError:
return False
for node in ast.walk(tree):
if isinstance(node, ast.Import):
if any(alias.name.split(".", 1)[0] not in _SAFE_IMPORTS for alias in node.names):
return False
elif isinstance(node, ast.ImportFrom):
if node.level or not node.module:
return False
if node.module.split(".", 1)[0] not in _SAFE_IMPORTS:
return False
elif isinstance(node, ast.Name):
if node.id in _BLOCKED_NAMES:
return False
if node.id.startswith("__") and node.id != "__name__":
return False
elif isinstance(node, ast.Attribute):
if node.attr in _BLOCKED_ATTRIBUTES or node.attr.startswith("_"):
return False
elif isinstance(node, ast.Constant) and isinstance(node.value, str):
lowered = node.value.lower()
if any(marker in lowered for marker in _BLOCKED_TEXT):
return False
return True
def _execute(code, stdin_text, timeout, argv=()):
if not _safe_code(code):
return "rejected", ""
path = None
output = None
process = None
try:
descriptor, path = tempfile.mkstemp(suffix=".py")
with os.fdopen(descriptor, "w") as handle:
handle.write(str(code))
os.chmod(path, 0o444)
output = tempfile.TemporaryFile()
process = subprocess.Popen(
[sys._base_executable, "-I", "-S", path, *[str(value) for value in argv]],
stdin=subprocess.PIPE,
stdout=output,
stderr=subprocess.DEVNULL,
preexec_fn=_limits,
close_fds=True,
cwd=tempfile.gettempdir(),
env={"PATH": "/usr/bin:/bin", "PYTHONIOENCODING": "utf-8"},
)
try:
process.communicate(str(stdin_text).encode("utf-8"), timeout=timeout)
except subprocess.TimeoutExpired:
_kill_group(process)
process.communicate(timeout=2)
return "timeout", ""
if process.returncode != 0:
return "exit", ""
output.seek(0)
raw = output.read(_OUTPUT_LIMIT + 1)
if len(raw) > _OUTPUT_LIMIT:
return "output_limit", ""
return "ok", raw.decode("utf-8", "replace")
except Exception: # noqa: BLE001
return "harness", ""
finally:
if process is not None and process.poll() is None:
_kill_group(process)
if output is not None:
output.close()
if path:
try:
os.unlink(path)
except OSError:
pass
def _bounded_timeout(until, maximum):
if until is None:
return maximum
return max(0.05, min(maximum, until - time.monotonic()))
def _sample_failure(answer, cases, until=None):
if not cases:
return _INCONCLUSIVE
code = _program(answer)
if not _safe_code(code):
return _INCONCLUSIVE
for stdin_text, expected in cases:
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
status, observed = _execute(
code, stdin_text, _bounded_timeout(until, _CASE_TIMEOUT_S),
)
if status in ("rejected", "harness"):
return _INCONCLUSIVE
if status != "ok" or not _same_output(observed, expected):
shown = observed.strip() if status == "ok" else "<%s>" % status
return stdin_text, shown or "<empty>", expected.strip()
return None
def _same_output(left, right):
return str(left).split() == str(right).split()
def _case_bank(
oracle, generator, rounds, until=None, seed_base=67867967, exclude=(),
):
bank = []
seen = {
hashlib.sha256(case.encode("utf-8", "replace")).digest()
for case, _wanted in exclude
}
for index in range(rounds):
if until is not None and time.monotonic() >= until:
break
status, case = _execute(
generator, "", _bounded_timeout(until, _GENERATOR_TIMEOUT_S),
argv=(seed_base + index, _SIZES[index % len(_SIZES)]),
)
encoded = case.encode("utf-8", "replace")
if status != "ok" or not case.strip() or len(encoded) > _MAX_EVIDENCE_INPUT:
continue
digest = hashlib.sha256(encoded).digest()
if digest in seen:
continue
if until is not None and time.monotonic() >= until:
break
oracle_status, wanted = _execute(
oracle, case, _bounded_timeout(until, _CASE_TIMEOUT_S),
)
if oracle_status != "ok" or not wanted.strip():
continue
if len(wanted.encode("utf-8", "replace")) > _MAX_EVIDENCE_OUTPUT:
continue
seen.add(digest)
bank.append((case, wanted))
return bank
def _counterexample(answer, bank, until=None):
code = _program(answer)
if not _safe_code(code):
return _INCONCLUSIVE
for case, wanted in bank:
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
status, observed = _execute(
code, case, _bounded_timeout(until, _CASE_TIMEOUT_S),
)
if status in ("rejected", "harness"):
return _INCONCLUSIVE
if status != "ok" or not _same_output(observed, wanted):
shown = observed.strip() if status == "ok" else "<%s>" % status
return case, shown or "<empty>", wanted.strip()
return None
def _confirms(reference, cases, mismatch, until=None):
if not reference or _sample_failure(reference, cases, until) is not None:
return False
if until is not None and time.monotonic() >= until:
return False
status, observed = _execute(
_program(reference), mismatch[0], _bounded_timeout(until, _CASE_TIMEOUT_S),
)
return status == "ok" and _same_output(observed, mismatch[2])
def _performance_issue(answer, generator, until=None):
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
status, case = _execute(
generator, "", _bounded_timeout(until, _PERF_GENERATOR_TIMEOUT_S),
argv=(104729, _PERF_SIZE),
)
if status != "ok" or not case.strip():
return None
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
started = time.monotonic()
run_status, _output = _execute(
_program(answer), case, _bounded_timeout(until, _PERF_TIMEOUT_S),
)
elapsed = time.monotonic() - started
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
if run_status == "ok" and elapsed <= _PERF_TARGET_S:
return None
return len(case.encode("utf-8", "replace")), elapsed, run_status
def _looks_large(prompt, threshold):
text = str(prompt)
for match in re.finditer(r"(?<![A-Za-z0-9_])(\d[\d,]*)(?![A-Za-z0-9_])", text):
try:
if int(match.group(1).replace(",", "")) >= threshold:
return True
except ValueError:
continue
for match in re.finditer(r"\b10\s*(?:\^|\*\*)\s*(\d{1,2})", text):
if int(match.group(1)) >= len(str(threshold)) - 1:
return True
return False
def _stated_tolerance(prompt):
text = str(prompt).lower()
if "error" not in text and "tolerance" not in text:
return None
values = []
for match in re.finditer(r"10\s*(?:\^|\*\*)?\s*\{?\s*[-−]\s*(\d{1,2})\s*\}?", text):
exponent = int(match.group(1))
if 1 <= exponent <= 18:
values.append(Decimal(10) ** -exponent)
for match in re.finditer(r"1(?:\.0+)?e-(\d{1,2})", text):
exponent = int(match.group(1))
if 1 <= exponent <= 18:
values.append(Decimal(10) ** -exponent)
return min(values) if values else None
def _same_differential_output(left, right, tolerance):
left_tokens = str(left).split()
right_tokens = str(right).split()
if left_tokens == right_tokens:
return True
if tolerance is None or len(left_tokens) != len(right_tokens):
return False
for left_token, right_token in zip(left_tokens, right_tokens):
if left_token == right_token:
continue
if not any(marker in left_token.lower() for marker in (".", "e")):
return False
if not any(marker in right_token.lower() for marker in (".", "e")):
return False
try:
left_number = Decimal(left_token)
right_number = Decimal(right_token)
except InvalidOperation:
return False
if not left_number.is_finite() or not right_number.is_finite():
return False
scale = max(Decimal(1), abs(left_number), abs(right_number))
if abs(left_number - right_number) > tolerance * scale:
return False
return True
def _differential_failure(answer, bank, tolerance, until=None):
code = _program(answer)
if not _safe_code(code):
return _INCONCLUSIVE
for case, wanted in bank:
if until is not None and time.monotonic() >= until:
return _INCONCLUSIVE
status, observed = _execute(
code, case, _bounded_timeout(until, _CASE_TIMEOUT_S),
)
if status in ("rejected", "harness"):
return _INCONCLUSIVE
if status != "ok" or not _same_differential_output(
observed, wanted, tolerance,
):
shown = observed.strip() if status == "ok" else "<%s>" % status
return case, shown or "<empty>", wanted.strip()
return None
def _differential_confirms(reference, cases, mismatch, tolerance, until=None):
if not reference or _sample_failure(reference, cases, until) is not None:
return False
if until is not None and time.monotonic() >= until:
return False
status, observed = _execute(
_program(reference), mismatch[0], _bounded_timeout(until, _CASE_TIMEOUT_S),
)
return status == "ok" and _same_differential_output(
observed, mismatch[2], tolerance,
)
def _load_policy(weights):
try:
policy = json.loads(bytes(weights).decode("utf-8"))
except Exception as exc:
raise ValueError("miner3-v9 weights are not valid JSON") from exc
expected = {
"code_call_cap": 5,
"confirm_effort": "medium",
"confirm_max_tokens": 12288,
"confirm_model": _DIVERSE,
"diverse_repair_effort": "medium",
"diverse_repair_max_tokens": 12288,
"diverse_repair_model": _DIVERSE,
"floor_call_cap": 1,
"floor_effort": "low",
"format": _FORMAT,
"future_task_reserve_s": 45,
"holdout_rounds": 8,
"local_guard_s": 55,
"max_examples": 6,
"min_holdout": 4,
"min_valid_stress": 12,
"performance_constraint_floor": 10000,
"primary_effort": "low",
"primary_max_tokens": 12288,
"primary_model": _PRIMARY,
"repair_effort": "medium",
"repair_max_tokens": 16384,
"run_call_cap": 12,
"run_deadline_s": 600,
"strategy_revision": 9,
"stress_rounds": 48,
"tools_effort": "low",
"tools_fallback": _DIVERSE,
"tools_max_tokens": 12288,
"tools_model": _TOOLS_MODEL,
}
if not isinstance(policy, dict) or policy != expected:
raise ValueError("miner3-v9 policy is malformed")
return policy
def build_agent(weights):
policy = _load_policy(weights)
started = [None]
served = [0]
total_calls = [0]
def agent(prompt, call_model):
original = str(prompt)
if started[0] is None:
started[0] = time.monotonic()
task_index = served[0]
served[0] += 1
calls = [0]
code_task = _is_code(original)
cap = policy["code_call_cap"] if code_task else policy["floor_call_cap"]
future = max(0, _EXPECTED_TASKS - task_index - 1)
task_deadline = (
started[0] + policy["run_deadline_s"]
- future * policy["future_task_reserve_s"]
)
verification_until = task_deadline - policy["local_guard_s"]
def request(model, messages, effort, tokens, window):
if calls[0] >= cap:
raise RuntimeError("miner3-v9 per-task call limit exceeded")
if total_calls[0] >= policy["run_call_cap"]:
raise RuntimeError("miner3-v9 whole-run call limit exceeded")
if time.monotonic() + window > task_deadline:
raise TimeoutError("miner3-v9 shared deadline reserve reached")
calls[0] += 1
total_calls[0] += 1
return call_model(
model, messages,
{"max_tokens": tokens, "reasoning": {"effort": effort}},
)
def one_user(content):
return [{"role": "user", "content": content}]
def confirm(mismatch, model, tolerance=None):
try:
reference = request(
model, one_user(original + "\n\n" + _CONFIRM_GUIDANCE),
policy["confirm_effort"], policy["confirm_max_tokens"], 40,
)
except Exception:
return False
if tolerance is None:
return _confirms(reference, cases, mismatch, verification_until)
return _differential_confirms(
reference, cases, mismatch, tolerance, verification_until,
)
def repair(
mismatch, model, effort, tokens, evidence_source,
guidance=_REPAIR_GUIDANCE,
):
repair_message = (
guidance
+ "\nExecuted input:\n%s\nProgram output:\n%s"
"\nTrusted expected output (%s):\n%s"
% (mismatch[0], mismatch[1], evidence_source, mismatch[2])
)
try:
return request(
model, one_user(original + "\n\n" + repair_message),
effort, tokens, 50,
)
except Exception:
return ""
if _is_choice(original):
try:
return request(
_PRIMARY, one_user(original), policy["floor_effort"],
policy["primary_max_tokens"], 25,
)
except Exception:
return ""
if not code_task:
try:
return request(
_PRIMARY, one_user(original + "\n\n" + _NUMERIC_GUIDANCE),
policy["floor_effort"], policy["primary_max_tokens"], 25,
)
except Exception:
return ""
cases = _samples(original, policy["max_examples"])
tolerance = _stated_tolerance(original)
try:
candidate = request(
_PRIMARY, one_user(original + "\n\n" + _DRAFT_GUIDANCE),
policy["primary_effort"], policy["primary_max_tokens"], 40,
)
except Exception:
return ""
sample_bad = _sample_failure(candidate, cases, verification_until)
if sample_bad is _INCONCLUSIVE:
return candidate
if sample_bad is not None:
revised = repair(
sample_bad, _PRIMARY, policy["repair_effort"],
policy["repair_max_tokens"], "published sample",
)
return (
revised
if _sample_failure(revised, cases, verification_until) is None
else candidate
)
def evidence_bank(model):
if time.monotonic() + 5.0 >= verification_until:
return "", "", []
try:
reply = request(
model, one_user(original + "\n\n" + _TOOLS_GUIDANCE),
policy["tools_effort"], policy["tools_max_tokens"], 40,
)
except Exception:
return "", "", []
blocks = _tool_blocks(reply)
oracle = blocks.get("oracle", "")
generator = blocks.get("generator", "")
if not oracle or not generator:
return "", "", []
if _sample_failure(oracle, cases, verification_until) is not None:
return "", "", []
bank = _case_bank(
oracle, generator, policy["stress_rounds"], verification_until,
)
if len(bank) < policy["min_valid_stress"]:
return "", "", []
return oracle, generator, bank
oracle, generator, bank = evidence_bank(policy["tools_model"])
fallback_used = not bank
if fallback_used:
oracle, generator, bank = evidence_bank(policy["tools_fallback"])
if not bank:
return candidate
mismatch = _differential_failure(
candidate, bank, tolerance, verification_until,
)
if mismatch is _INCONCLUSIVE:
return candidate
if mismatch is not None:
confirm_model = _PRIMARY if fallback_used else policy["confirm_model"]
if not confirm(mismatch, confirm_model, tolerance):
return candidate
repair_model = (
policy["diverse_repair_model"] if fallback_used else _PRIMARY
)
repair_effort = (
policy["diverse_repair_effort"]
if fallback_used else policy["repair_effort"]
)
repaired = repair(
mismatch, repair_model, repair_effort,
(
policy["diverse_repair_max_tokens"]
if fallback_used else policy["repair_max_tokens"]
),
"independent references",
)
if _sample_failure(repaired, cases, verification_until) is not None:
return candidate
if _differential_failure(
repaired, bank, tolerance, verification_until,
) is not None:
return candidate
holdout = _case_bank(
oracle, generator, policy["holdout_rounds"], verification_until,
seed_base=982451653, exclude=bank,
)
if len(holdout) < policy["min_holdout"]:
return candidate
if _differential_failure(
repaired, holdout, tolerance, verification_until,
) is not None:
return candidate
return repaired
if tolerance is not None:
format_mismatch = _counterexample(
candidate, bank, verification_until,
)
if format_mismatch is _INCONCLUSIVE:
return candidate
if format_mismatch is not None:
confirm_model = (
_PRIMARY if fallback_used else policy["confirm_model"]
)
if not confirm(format_mismatch, confirm_model):
return candidate
repair_model = (
policy["diverse_repair_model"]
if fallback_used else _PRIMARY
)
repair_effort = (
policy["diverse_repair_effort"]
if fallback_used else policy["repair_effort"]
)
formatted = repair(
format_mismatch, repair_model, repair_effort,
(
policy["diverse_repair_max_tokens"]
if fallback_used else policy["repair_max_tokens"]
),
"independently confirmed exact tokens",
_FORMAT_REPAIR_GUIDANCE,
)
if _sample_failure(
formatted, cases, verification_until,
) is not None:
return candidate
if _counterexample(
formatted, bank, verification_until,
) is not None:
return candidate
holdout = _case_bank(
oracle, generator, policy["holdout_rounds"],
verification_until, seed_base=982451653, exclude=bank,
)
if len(holdout) < policy["min_holdout"]:
return candidate
if _counterexample(
formatted, holdout, verification_until,
) is not None:
return candidate
return formatted
if not _looks_large(original, policy["performance_constraint_floor"]):
return candidate
issue = _performance_issue(candidate, generator, verification_until)
if issue in (None, _INCONCLUSIVE):
return candidate
bytes_count, elapsed, status = issue
performance_message = (
_PERFORMANCE_GUIDANCE
+ "\nMeasured input bytes: %d\nMeasured seconds: %.3f"
"\nExecution status: %s" % (bytes_count, elapsed, status)
)
try:
faster = request(
_PRIMARY, one_user(original + "\n\n" + performance_message),
policy["repair_effort"], policy["repair_max_tokens"], 50,
)
except Exception:
return candidate
if _sample_failure(faster, cases, verification_until) is not None:
return candidate
if _differential_failure(
faster, bank, tolerance, verification_until,
) is not None:
return candidate
holdout = _case_bank(
oracle, generator, policy["holdout_rounds"], verification_until,
seed_base=961748927, exclude=bank,
)
if len(holdout) < policy["min_holdout"]:
return candidate
if _differential_failure(
faster, holdout, tolerance, verification_until,
) is not None:
return candidate
return (
faster
if _performance_issue(faster, generator, verification_until) is None
else candidate
)
return agent
|