VALOR0316 commited on
Commit
b68cd99
·
verified ·
1 Parent(s): 03ec0c9

uid101 clean v5.32 learned evidence router

Browse files
Files changed (2) hide show
  1. source.py +817 -139
  2. weights.bin +2 -2
source.py CHANGED
@@ -1,14 +1,16 @@
1
- """UID 101 Aegis clean v4.5 task-independent consensus candidate.
2
 
3
- Independent candidates are selected by deterministic sample, execution, and consensus evidence.
4
- Sparse or contradictory evidence escalates. The policy has no task IDs, prompt fingerprints,
5
- lookup tables, stored solutions, or benchmark-trained answer selector.
6
  """
7
 
8
  import ast
9
  import json
10
  import math
11
  import re
 
 
12
  import subprocess
13
  import sys
14
  import time
@@ -23,7 +25,7 @@ _MODELS = (
23
  "google/gemini-3.6-flash",
24
  "moonshotai/kimi-k3",
25
  )
26
- _KIND = "valor-uid101-aegis-clean-v4.5"
27
  _SAMPLE_MARK = re.compile(r"^Sample (Input|Output) (\d+)\s*$", re.M)
28
  _CODE_WORD = re.compile(r"\b(?:input|print|sys|def|import|from)\b")
29
  _NUMBER = re.compile(r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\Z")
@@ -47,21 +49,76 @@ _MUTABLE_GRAPH_RISK = re.compile(
47
  r"(?:destroy|remove|unlock|open).{0,100}(?:wall|edge|cell|node)", re.I | re.S
48
  )
49
  _LARGE_N = re.compile(
50
- r"\bN\s*(?:\\leq|<=|≤)\s*(?:[1-9]\s*(?:\\times|[x×*])\s*)?10\^5|"
 
51
  r"\bN\s*(?:\\leq|<=|≤)\s*[1-9][0-9]{5,}", re.I
52
  )
53
- _NAMED_BLOCK = re.compile(r"```(candidate|generator|reference)\s*\n(.*?)```", re.I | re.S)
 
 
 
 
 
 
 
 
 
 
 
 
54
  _FLOAT_TOL = 1e-9
55
  _CASE_TIMEOUT = 3.0
56
  _GEN_TIMEOUT = 1.0
57
- _STRESS_TIMEOUT = 2.0
58
- _STRESS_SECONDS = 18.0
59
- _STRESS_SIZES = (1, 2, 3, 5, 8, 12)
60
- _RUN_BUDGET_S = 540.0
 
 
 
 
 
61
  _EXPECTED_TASKS = 6
62
- _RESERVE_PER_TASK_S = 24.0
 
 
63
  _MIN_CALL_WINDOW_S = 12.0
64
  _DEFAULT_CALL_ESTIMATE_S = 20.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  _SOLVE = (
67
  "Derive the algorithm only from the statement and constraints. Check indexing, duplicates, "
@@ -69,6 +126,12 @@ _SOLVE = (
69
  "use local aliases with attached metadata. Before coding a greedy or compressed-state solution, "
70
  "differential-check its invariant against exhaustive tiny cases."
71
  )
 
 
 
 
 
 
72
  _PRIMARY_FORMAT = (
73
  "Return two fenced blocks and no prose. First is the complete Python 3 solution; its first "
74
  "source line must be the literal comment # input so the parser recognizes sys.stdin programs. "
@@ -104,12 +167,30 @@ _REVIEW = (
104
  "legal instances, including duplicates, ordering, and overwrites. Trace every statement sample "
105
  "through parsing and output length. Return one corrected raw Python 3 program without prose."
106
  )
 
 
 
107
  _MUST_PASS = (
108
  "Every statement sample is an executable acceptance condition. Re-derive the algorithm rather "
109
  "than fitting sample outputs, mentally execute the complete program on every stated sample, and "
110
  "return code only after all samples agree. If prior candidates share an invariant, actively seek "
111
  "a counterexample to that invariant before reusing it."
112
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  _CHALLENGE_A = (
114
  "Solve independently without copying the prior candidate. Return exactly three fenced blocks and "
115
  "no prose. The candidate block must contain a complete Python 3 solution. "
@@ -120,24 +201,155 @@ _CHALLENGE_B = (
120
  "statement. Vary ties, duplicates, boundaries, operation order, and branch combinations.\n"
121
  )
122
  _CHALLENGE_FORMAT = (
123
- "Reference must be a literal small-input solver independent of the candidate. Label the first "
124
- "fence python and make its first source line the literal comment # input, so the parser extracts "
125
- "the same candidate this agent may return.\n"
126
  "```python\n<program>\n```\n```generator\n<program>\n```"
127
- "\n```reference\n<program>\n```"
128
  )
129
  _CHALLENGE = _CHALLENGE_A + _CHALLENGE_B + _CHALLENGE_FORMAT
130
- _GENERATOR_ONLY = (
131
- "Return exactly two fenced blocks and no prose. Generator accepts integer seed and size command-"
132
- "line arguments, calls random.seed(seed), and prints one small legal input. Reference is a "
133
- "literal exhaustive solver for small legal inputs and must not use an optimized greedy invariant.\n"
134
- "```generator\n<program>\n```\n```reference\n<program>\n```"
 
 
135
  )
136
- _FLOAT_FORMAT = (
137
- "The statement permits floating-point tolerance. Print each real-valued result in fixed "
138
- "decimal notation with exactly 15 digits after the decimal point so exact-token benchmark "
139
- "harnesses retain the precision shown by standard contest reference outputs."
 
 
 
 
 
 
 
 
 
 
 
140
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
 
143
  def _load(weights):
@@ -146,25 +358,29 @@ def _load(weights):
146
  except Exception as exc:
147
  raise ValueError("UID 101 clean settings are not valid JSON") from exc
148
  required = {
149
- "schema", "kind", "primary", "challenger", "generator", "reviewer", "rescue",
 
150
  "primary_max_tokens", "challenge_max_tokens", "review_max_tokens",
 
151
  "generator_max_tokens", "rescue_max_tokens", "sample_limit", "stress_cases",
152
  "min_consensus_cases", "critic",
153
  "temperature", "primary_reasoning", "challenge_reasoning", "review_reasoning",
 
154
  "generator_reasoning", "rescue_reasoning",
155
  }
156
  if not isinstance(data, dict) or set(data) != required:
157
  raise ValueError("UID 101 clean settings have an invalid schema")
158
  if data["schema"] != 3 or data["kind"] != _KIND:
159
  raise ValueError("UID 101 clean settings do not match this source")
160
- for field in ("primary", "challenger", "generator", "reviewer", "rescue"):
 
161
  if type(data[field]) is not int or not 0 <= data[field] < len(_MODELS):
162
  raise ValueError("UID 101 clean model index is invalid")
163
  if (type(data["primary_max_tokens"]) is not int
164
  or not 4096 <= data["primary_max_tokens"] <= 32768):
165
  raise ValueError("UID 101 clean primary token limit is invalid")
166
  for field in ("challenge_max_tokens", "generator_max_tokens", "review_max_tokens",
167
- "rescue_max_tokens"):
168
  if type(data[field]) is not int or not 2048 <= data[field] <= 32768:
169
  raise ValueError("UID 101 clean fallback token limit is invalid")
170
  if type(data["sample_limit"]) is not int or not 1 <= data["sample_limit"] <= 4:
@@ -191,7 +407,8 @@ def _load(weights):
191
  if type(data["temperature"]) not in (int, float) or data["temperature"] != 0:
192
  raise ValueError("UID 101 clean temperature must be deterministic")
193
  for field in ("primary_reasoning", "challenge_reasoning", "generator_reasoning",
194
- "review_reasoning", "rescue_reasoning"):
 
195
  if data[field] not in ("none", "low", "medium", "high"):
196
  raise ValueError("UID 101 clean reasoning effort is invalid")
197
  return data
@@ -289,7 +506,12 @@ def _tokens_match(observed, expected, numeric):
289
  return True
290
 
291
 
292
- def _run_program(code, stdin_text, timeout, argv=()):
 
 
 
 
 
293
  try:
294
  run = subprocess.run(
295
  [sys.executable, "-I", "-c", code, *[str(value) for value in argv]],
@@ -301,7 +523,7 @@ def _run_program(code, stdin_text, timeout, argv=()):
301
  return run.stdout if run.returncode == 0 else None
302
 
303
 
304
- def _check(code, samples, numeric):
305
  if not code.strip():
306
  return 0, max(1, len(samples)), (
307
  samples[0][0] if samples else "", "<empty>",
@@ -318,8 +540,9 @@ def _check(code, samples, numeric):
318
  passed = 0
319
  first_bad = None
320
  for stdin, expected in samples:
321
- observed = _run_program(code, stdin, _CASE_TIMEOUT)
322
- good = observed is not None and _tokens_match(observed, expected, numeric)
 
323
  if good:
324
  passed += 1
325
  elif first_bad is None:
@@ -342,8 +565,20 @@ def _failure_note(passed, total, failure):
342
  def _named_programs(answer):
343
  programs = {}
344
  python_blocks = []
345
- for match in re.finditer(r"```([^\n`]*)\n(.*?)```", str(answer or ""), re.I | re.S):
346
- tag = match.group(1).strip().lower()
 
 
 
 
 
 
 
 
 
 
 
 
347
  lines = [line for line in match.group(2).strip().splitlines()
348
  if line.strip().lower() not in ("<program>", "program")]
349
  code = "\n".join(lines).strip()
@@ -353,7 +588,7 @@ def _named_programs(answer):
353
  python_blocks.append(code + "\n")
354
  if "candidate" not in programs:
355
  programs["candidate"] = code + "\n"
356
- elif tag in ("generator", "reference") and tag not in programs:
357
  programs[tag] = code + "\n"
358
  elif tag == "candidate" and "candidate" not in programs:
359
  # Backward-compatible parsing for stored development responses. New prompts never ask
@@ -378,7 +613,7 @@ def _plain_program_blocks(answer):
378
  return blocks
379
 
380
 
381
- def _generated_inputs(generator, case_limit):
382
  if not generator or not generator.strip():
383
  return []
384
  # A generator that ignores the supplied scale only exercises toy inputs and can hide
@@ -386,26 +621,189 @@ def _generated_inputs(generator, case_limit):
386
  direct_size = re.search(r"(?:sys\.)?argv\s*\[\s*2\s*\]", generator)
387
  sliced_args = (re.search(r"(?:sys\.)?argv\s*\[\s*1\s*:\s*\]", generator)
388
  and re.search(r"\[\s*1\s*\]", generator))
389
- if direct_size is None and not sliced_args:
390
  return []
391
  try:
392
  compile(generator, "<generator>", "exec")
393
  except (SyntaxError, ValueError, TypeError):
394
  return []
395
- deadline = time.monotonic() + _STRESS_SECONDS
 
396
  cases = []
397
- for index in range(case_limit):
 
 
 
 
 
 
 
 
 
 
 
 
 
398
  if time.monotonic() >= deadline:
399
  break
400
- size = _STRESS_SIZES[index % len(_STRESS_SIZES)]
401
- stdin = _run_program(generator, "", _GEN_TIMEOUT, (index + 1, size))
402
  if not stdin or not stdin.strip() or len(stdin) > 20000:
403
  continue
 
404
  if stdin not in cases:
405
  cases.append(stdin)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
  return cases
407
 
408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  def _quality(code):
410
  """Return a small task-independent static safety score; never infer a task identity."""
411
  try:
@@ -488,52 +886,99 @@ def _critic_score(code, sample_ratio, completion_ratio, consensus_ratio, critic)
488
  return 1.0 / (1.0 + math.exp(-max(-30.0, min(30.0, logit))))
489
 
490
 
491
- def _reference_valid(reference, samples, numeric):
492
  if not reference or not reference.strip():
493
  return False
494
  try:
495
  compile(reference, "<reference>", "exec")
496
  except (SyntaxError, ValueError, TypeError):
497
  return False
 
 
 
498
  for stdin, expected in samples:
499
- observed = _run_program(reference, stdin, _STRESS_TIMEOUT)
500
- if observed is None or not _tokens_match(observed, expected, numeric):
 
 
 
501
  return False
502
- return True
503
 
504
 
505
- def _trusted_oracle(reference_a, reference_b, samples, generated, numeric, minimum):
506
- if (not _reference_valid(reference_a, samples, numeric)
507
- or not _reference_valid(reference_b, samples, numeric)):
 
508
  return []
509
  agreed = []
510
  for stdin in generated:
511
- left = _run_program(reference_a, stdin, _STRESS_TIMEOUT)
512
- right = _run_program(reference_b, stdin, _STRESS_TIMEOUT)
513
- if left is None or right is None or not _tokens_match(left, right, numeric):
 
 
514
  return []
515
  agreed.append((stdin, left))
516
  return agreed if len(agreed) >= minimum else []
517
 
518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
  def _candidate_evidence(candidates, samples, generated, numeric, oracle_cases=(), critic=None,
520
- large_input=False):
521
  rows = []
522
  outputs = []
523
  for index, code in enumerate(candidates):
524
- passed, total, failure = _check(code, samples, numeric)
525
- values = [_run_program(code, stdin, _STRESS_TIMEOUT) for stdin in generated]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  outputs.append(values)
527
  rows.append({
528
  "index": index,
529
  "sample_passed": passed,
530
  "sample_total": total,
531
- "sample_valid": bool(code.strip() and failure is None and passed == total),
532
  "completed": sum(value is not None for value in values),
533
  "consensus": 0,
534
  "oracle": 0,
535
  "oracle_total": len(oracle_cases),
536
  "feasible": not (large_input and _has_input_scaled_nested_loop(code)),
 
 
 
 
 
537
  "critic": 0.0,
538
  "quality": _quality(code),
539
  })
@@ -562,7 +1007,7 @@ def _candidate_evidence(candidates, samples, generated, numeric, oracle_cases=()
562
  rows[candidate_index]["consensus"] += 1
563
  for row, code in zip(rows, candidates):
564
  for stdin, expected in oracle_cases:
565
- observed = _run_program(code, stdin, _STRESS_TIMEOUT)
566
  if observed is not None and _tokens_match(observed, expected, numeric):
567
  row["oracle"] += 1
568
  row["critic"] = _critic_score(
@@ -578,10 +1023,24 @@ def _candidate_evidence(candidates, samples, generated, numeric, oracle_cases=()
578
  def _best_candidate(candidates, rows):
579
  viable = [row for row in rows if row["sample_valid"]]
580
  pool = viable if viable else rows
 
 
 
 
 
 
 
 
 
 
581
  best = max(pool, key=lambda row: (
582
- row["sample_valid"], row["feasible"], row["sample_passed"],
583
  bool(row["oracle_total"] and row["oracle"] == row["oracle_total"]),
584
- row["oracle"], row["consensus"], row["critic"],
 
 
 
 
585
  row["completed"], row["quality"], -row["index"],
586
  ))
587
  return candidates[best["index"]], best
@@ -605,9 +1064,31 @@ def _disagreement_note(generated, outputs):
605
  return "\n\n".join(notes)
606
 
607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
  def build_agent(weights):
609
  config = _load(weights)
610
- state = {"started": None, "done": 0, "latency": {}}
 
611
 
612
  def invoke(call_model, model_index, prompt, max_tokens, reasoning):
613
  params = {
@@ -617,6 +1098,8 @@ def build_agent(weights):
617
  }
618
  started = time.monotonic()
619
  try:
 
 
620
  return call_model(
621
  _MODELS[model_index], [{"role": "user", "content": prompt}], params
622
  )
@@ -626,9 +1109,9 @@ def build_agent(weights):
626
  return ""
627
  finally:
628
  elapsed = time.monotonic() - started
629
- prior = state["latency"].get(model_index, _DEFAULT_CALL_ESTIMATE_S)
630
- state["latency"][model_index] = max(_DEFAULT_CALL_ESTIMATE_S,
631
- 0.7 * prior + 0.3 * elapsed)
632
 
633
  def agent(prompt, call_model):
634
  if state["started"] is None:
@@ -636,25 +1119,52 @@ def build_agent(weights):
636
  state["done"] += 1
637
  epoch_deadline = state["started"] + _RUN_BUDGET_S
638
 
639
- def room_for(model_index, multiplier=1.0):
640
  remaining = max(0, _EXPECTED_TASKS - state["done"])
641
- estimate = state["latency"].get(model_index, _DEFAULT_CALL_ESTIMATE_S)
 
 
 
 
 
 
642
  return (time.monotonic() + max(_MIN_CALL_WINDOW_S, estimate * multiplier)
643
- + _RESERVE_PER_TASK_S * remaining) < epoch_deadline
644
 
645
  original = str(prompt)
646
  if not _is_code(original):
 
647
  return invoke(
648
- call_model, config["primary"], _non_code_request(original),
649
  config["primary_max_tokens"], config["primary_reasoning"],
650
  )
651
 
 
 
 
 
 
 
 
 
 
 
 
 
652
  samples = _samples(original, config["sample_limit"])
653
  numeric = bool(_FLOAT_JUDGE.search(original))
 
654
  sequential_risk = bool(_SEQUENTIAL_RISK.search(original))
655
  mutable_graph_risk = bool(_MUTABLE_GRAPH_RISK.search(original))
656
  large_input = bool(_LARGE_N.search(original))
657
- request = original + "\n\nGeneral reliability protocol: " + _SOLVE
 
 
 
 
 
 
 
658
  if numeric:
659
  request += "\n\nNumeric-output protocol: " + _FLOAT_FORMAT
660
  if sequential_risk:
@@ -665,89 +1175,214 @@ def build_agent(weights):
665
  request += ("\n\nLarge-input protocol: Prove the asymptotic bound for the largest N. "
666
  "Reject any nested iteration whose inner bound grows with the input unless "
667
  "a strict amortized or logarithmic bound is established.")
668
- primary_answer = invoke(
669
- call_model, config["primary"], request + "\n\nOutput protocol:\n" + _PRIMARY_FORMAT,
670
- config["primary_max_tokens"],
671
- config["primary_reasoning"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
672
  )
673
  primary_programs = _named_programs(primary_answer)
674
- first = _returnable_candidate(primary_answer, primary_programs)
675
  references = []
676
  if primary_programs.get("reference"):
677
  references.append(primary_programs["reference"])
678
- challenge_request = (
679
- request + "\n\nA separate solver proposed this program:\n" + first
680
- + "\n\nIndependent challenge protocol:\n" + _CHALLENGE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681
  )
682
- challenge_answer = (invoke(
683
- call_model, config["challenger"], challenge_request,
684
- config["challenge_max_tokens"], config["challenge_reasoning"],
685
- ) if room_for(config["challenger"], 1.5) else "")
686
  programs = _named_programs(challenge_answer)
687
- second = _returnable_candidate(challenge_answer, programs)
688
  generator = programs.get("generator")
 
689
  if programs.get("reference"):
690
  references.append(programs["reference"])
691
  candidates = [first]
692
  if second.strip():
693
  candidates.append(second)
694
- generated = _generated_inputs(generator, config["stress_cases"])
695
- if not generated and room_for(config["generator"], 1.25):
 
 
 
 
 
 
696
  generator_request = original + "\n\nInput-generator protocol: " + _GENERATOR_ONLY
697
- generator_answer = invoke(
698
- call_model, config["generator"], generator_request,
699
  config["generator_max_tokens"], config["generator_reasoning"],
700
  )
701
  generated_programs = _named_programs(generator_answer)
702
  if not generated_programs:
703
  plain_blocks = _plain_program_blocks(generator_answer)
704
- if len(plain_blocks) == 2:
705
  generated_programs = {
706
  "generator": plain_blocks[0], "reference": plain_blocks[1],
707
  }
708
- generator = generated_programs.get("generator") or _source(generator_answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709
  if generated_programs.get("reference"):
710
  references.append(generated_programs["reference"])
711
- generated = _generated_inputs(generator, config["stress_cases"])
712
- oracle_cases = []
713
- for left_index in range(len(references)):
714
- for right_index in range(left_index + 1, len(references)):
715
- oracle_cases = _trusted_oracle(
716
- references[left_index], references[right_index], samples, generated,
717
- numeric, config["min_consensus_cases"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
718
  )
719
- if oracle_cases:
720
- break
721
- if oracle_cases:
722
- break
723
  rows, outputs = _candidate_evidence(
724
- candidates, samples, generated, numeric, oracle_cases, config["critic"], large_input
725
  )
726
  best, best_row = _best_candidate(candidates, rows)
 
 
727
  disagreement = _disagreement_note(generated, outputs)
728
- if (best_row["sample_valid"] and (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
729
  (oracle_cases and best_row["oracle"] == best_row["oracle_total"])
730
- or (not oracle_cases and len(candidates) >= 2
731
  and len(generated) >= config["min_consensus_cases"]
732
  and all(row["sample_valid"] for row in rows)
733
  and best_row["consensus"] == len(generated)
734
  and best_row["completed"] == len(generated)))):
735
- return best
736
 
737
  candidate_text = "\n\n".join(
738
  "Candidate " + str(index + 1) + ":\n" + code
739
  for index, code in enumerate(candidates)
740
  )
741
- review_request = (
742
- request + "\n\nIndependent review protocol: " + _REVIEW
743
- + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
744
- + "\n\nPrograms under review:\n" + candidate_text
745
- )
 
 
 
 
 
 
 
 
 
 
 
746
  if disagreement:
747
  review_request += (
748
  "\n\nThe programs disagreed on small generated inputs. These outputs are evidence "
749
  "of disagreement only; none is a trusted oracle.\n" + disagreement
750
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
  if oracle_cases:
752
  counterexamples = []
753
  for stdin, expected in oracle_cases:
@@ -765,49 +1400,92 @@ def build_agent(weights):
765
  "\n\nTwo independent small references agreed on these counterexamples:\n"
766
  + "\n\n".join(counterexamples)
767
  )
768
- third = _source(invoke(
769
- call_model, config["reviewer"], review_request, config["review_max_tokens"],
770
  config["review_reasoning"],
771
- )) if room_for(config["reviewer"], 1.5) else ""
 
772
  if third.strip():
773
  candidates.append(third)
774
- rows, outputs = _candidate_evidence(
775
- candidates, samples, generated, numeric, oracle_cases, config["critic"], large_input
776
- )
 
 
 
 
 
 
 
 
 
 
777
  best, best_row = _best_candidate(candidates, rows)
778
- if (best_row["sample_valid"] and (
 
 
 
 
 
 
 
 
779
  (oracle_cases and best_row["oracle"] == best_row["oracle_total"])
780
- or (not oracle_cases
781
  and best_row["consensus"] >= config["min_consensus_cases"]
782
- and best_row["completed"] >= config["min_consensus_cases"]))):
783
- return best
 
 
 
 
 
 
784
 
785
- # A slow emergency model cannot improve a candidate that already satisfies every
786
- # statement sample without concrete contradictory execution evidence. Preserve the best
787
- # valid candidate and reserve rescue for syntax/sample failure only.
788
- if (best_row["sample_valid"]
789
- and (not oracle_cases or best_row["oracle"] == best_row["oracle_total"])):
790
- return best
 
791
 
792
  passed, total, failure = _check(best, samples, numeric)
793
- rescue_request = (
794
- request + "\n\nEmergency independent derivation: " + _REPAIR
795
- + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
796
- + "\n\nThe deterministic selector found insufficient execution consensus. "
797
- "Do not vote between prior programs; derive and return one complete raw program."
798
- + "\n\nBest prior candidate diagnostics:\n"
799
- + _failure_note(passed, total, failure)
800
- + "\n\nBest prior candidate:\n" + best
801
- )
802
- rescued = _source(invoke(
803
- call_model, config["rescue"], rescue_request, config["rescue_max_tokens"],
 
 
 
 
 
 
 
 
 
804
  config["rescue_reasoning"],
805
- )) if room_for(config["rescue"], 1.5) else ""
806
  if rescued.strip():
807
  candidates.append(rescued)
808
- rows, _outputs = _candidate_evidence(
809
- candidates, samples, generated, numeric, oracle_cases, config["critic"], large_input
810
- )
811
- return _best_candidate(candidates, rows)[0]
 
 
 
 
 
 
 
 
812
 
813
  return agent
 
1
+ """UID 101 Aegis clean v5.32 task-independent consensus candidate.
2
 
3
+ Provider-diverse candidates are selected by literal statement samples, bounded execution,
4
+ generated counterexamples, and runtime evidence. The policy has no task IDs, prompt
5
+ fingerprints, lookup tables, stored solutions, or benchmark-trained answer selector.
6
  """
7
 
8
  import ast
9
  import json
10
  import math
11
  import re
12
+ import queue
13
+ import threading
14
  import subprocess
15
  import sys
16
  import time
 
25
  "google/gemini-3.6-flash",
26
  "moonshotai/kimi-k3",
27
  )
28
+ _KIND = "valor-uid101-aegis-clean-v5.32"
29
  _SAMPLE_MARK = re.compile(r"^Sample (Input|Output) (\d+)\s*$", re.M)
30
  _CODE_WORD = re.compile(r"\b(?:input|print|sys|def|import|from)\b")
31
  _NUMBER = re.compile(r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\Z")
 
49
  r"(?:destroy|remove|unlock|open).{0,100}(?:wall|edge|cell|node)", re.I | re.S
50
  )
51
  _LARGE_N = re.compile(
52
+ r"\bN\s*(?:\\leq|<=|≤)\s*(?:[1-9]\s*(?:\\times|[x×*])\s*)?"
53
+ r"10\^(?:[5-9]|[1-9][0-9]+)|"
54
  r"\bN\s*(?:\\leq|<=|≤)\s*[1-9][0-9]{5,}", re.I
55
  )
56
+ _MANY_OUTPUT_RISK = re.compile(
57
+ r"(?:for\s+each|for\s+every).{0,100}(?:integer|value|number)|"
58
+ r"(?:all|many).{0,80}(?:coefficients?|answers?|values?)", re.I | re.S
59
+ )
60
+ _MODULAR_RISK = re.compile(
61
+ r"(?:prime|modulo|modulus).{0,100}\bP\b|\bP\b.{0,100}(?:prime|modulo|modulus)",
62
+ re.I | re.S,
63
+ )
64
+ _COUNTING_RISK = re.compile(
65
+ r"(?:number\s+of|count).{0,120}(?:graphs?|ways?|configurations?|sequences?|subsets?)",
66
+ re.I | re.S,
67
+ )
68
+ _NAMED_BLOCK = re.compile(r"```(candidate|generator|reference|benchmark)\s*\n(.*?)```", re.I | re.S)
69
  _FLOAT_TOL = 1e-9
70
  _CASE_TIMEOUT = 3.0
71
  _GEN_TIMEOUT = 1.0
72
+ _STRESS_TIMEOUT = 3.0
73
+ _PERFORMANCE_MARGIN_TIMEOUT = 8.0
74
+ _STRESS_SECONDS = 8.0
75
+ _MAX_CODE_CALLS = 4
76
+ _EVIDENCE_BUDGET_S = 54.0
77
+ _ORACLE_BUDGET_S = 12.0
78
+ _CANDIDATE_EVIDENCE_BUDGET_S = 18.0
79
+ _STRESS_SIZES = (1, 2, 3, 5, 8, 12, 24, 48)
80
+ _RUN_BUDGET_S = 640.0
81
  _EXPECTED_TASKS = 6
82
+ _EXPECTED_CODE_TASKS = 2
83
+ _RESERVE_PER_TASK_S = 20.0
84
+ _RESERVE_PER_CODE_TASK_S = 280.0
85
  _MIN_CALL_WINDOW_S = 12.0
86
  _DEFAULT_CALL_ESTIMATE_S = 20.0
87
+ _MODEL_CALL_FLOOR_S = (60.0, 90.0, 180.0, 90.0, 150.0, 45.0, 90.0)
88
+ _HARD_CALL_TIMEOUT_S = 300.0
89
+ _RESOURCE_AUDIT = (
90
+ "Estimate maximum states, leaves, and retained objects before coding. A time-feasible "
91
+ "enumeration can still exceed memory if every leaf is stored. When only distinct "
92
+ "aggregates are required, update the final set or counter online. The largest legal input "
93
+ "must retain a substantial margin below the execution limit, not merely finish at its edge."
94
+ )
95
+ _INDEX_BOUND_AUDIT = (
96
+ "For every list or array, derive its allocated length and the maximum reachable subscript "
97
+ "over every legal input. Reject code when an index can grow faster than the allocation "
98
+ "expression or can otherwise exceed it; a sample-sized run is not boundary evidence."
99
+ )
100
+ _PERFORMANCE_AUDIT = (
101
+ "Construct a largest-constraint legal input from the statement and estimate the exact dominant "
102
+ "operation count. Reject boundary-time code. Prefer a candidate that preserves correctness "
103
+ "evidence while completing every executable scale probe with the largest timing margin."
104
+ )
105
+ _RECURSION_AUDIT = (
106
+ "Treat self-recursion inside an input-dependent loop as exponential until a strict maximum "
107
+ "leaf count proves wide margin. Another recursive branch increases that bound; sample-sized "
108
+ "completion is not largest-input performance evidence."
109
+ )
110
+ _BATCH_OUTPUT_AUDIT = (
111
+ "When a counting problem requests all answers or coefficients over a range, reject rerunning "
112
+ "the full combinatorial dynamic program independently at every interpolation point unless the "
113
+ "proved total operation count has a wide margin. Prefer propagating coefficient vectors or "
114
+ "batched transforms once through the state graph, while proving the coefficient conversion."
115
+ )
116
+ _PERFORMANCE_REPAIR = (
117
+ "The program below has the strongest independent correctness evidence but missed a strict "
118
+ "largest-input timing gate. Preserve its verified semantics and exact I/O while reducing the "
119
+ "largest legal input below 4 seconds on one CPU. Re-derive the algorithm if "
120
+ "micro-optimization cannot provide that margin. Return only complete raw Python 3 source."
121
+ )
122
 
123
  _SOLVE = (
124
  "Derive the algorithm only from the statement and constraints. Check indexing, duplicates, "
 
126
  "use local aliases with attached metadata. Before coding a greedy or compressed-state solution, "
127
  "differential-check its invariant against exhaustive tiny cases."
128
  )
129
+ _INTEGER_SAFETY = (
130
+ "Python integers are arbitrary precision. Do not narrow values, partial sums, XOR states, or "
131
+ "hash keys into a fixed-width array or mask unless every stated bound proves that no legal "
132
+ "value can be truncated or collide. Prefer built-in set/dict for exact deduplication; "
133
+ "use Python-level open addressing only with proved bounds and a faster executable benchmark."
134
+ )
135
  _PRIMARY_FORMAT = (
136
  "Return two fenced blocks and no prose. First is the complete Python 3 solution; its first "
137
  "source line must be the literal comment # input so the parser recognizes sys.stdin programs. "
 
167
  "legal instances, including duplicates, ordering, and overwrites. Trace every statement sample "
168
  "through parsing and output length. Return one corrected raw Python 3 program without prose."
169
  )
170
+ _REVIEW = (_REVIEW + " " + _PERFORMANCE_AUDIT + " " + _BATCH_OUTPUT_AUDIT
171
+ + " " + _RECURSION_AUDIT)
172
+
173
  _MUST_PASS = (
174
  "Every statement sample is an executable acceptance condition. Re-derive the algorithm rather "
175
  "than fitting sample outputs, mentally execute the complete program on every stated sample, and "
176
  "return code only after all samples agree. If prior candidates share an invariant, actively seek "
177
  "a counterexample to that invariant before reusing it."
178
  )
179
+ _EQUIVALENCE_AUDIT = (
180
+ "Audit every claimed equivalence and every necessary-or-sufficient reduction in both "
181
+ "directions. Construct the smallest legal counterexample to each unproved direction before "
182
+ "accepting a recurrence, compressed state, interpolation formula, or graph characterization. "
183
+ )
184
+ _INDEPENDENCE_AUDIT = (
185
+ "Do not reuse a shared invariant merely because several programs agree. Derive the invariant "
186
+ "from the statement, compare it with a literal small-instance enumerator when tractable, and "
187
+ "separately justify completeness, uniqueness, and the largest-input complexity bound."
188
+ )
189
+ _QUANTIFIER_AUDIT = (
190
+ "Preserve the order and scope of every universal or existential condition. Before replacing a "
191
+ "quantified condition by a familiar named property, prove both implications and enumerate the "
192
+ "smallest legal structures where the proposed property may hold but the original condition may fail."
193
+ )
194
  _CHALLENGE_A = (
195
  "Solve independently without copying the prior candidate. Return exactly three fenced blocks and "
196
  "no prose. The candidate block must contain a complete Python 3 solution. "
 
201
  "statement. Vary ties, duplicates, boundaries, operation order, and branch combinations.\n"
202
  )
203
  _CHALLENGE_FORMAT = (
204
+ "Reference is an independent literal small-input solver. Benchmark accepts seed and size and "
205
+ "prints one largest legal input, never an answer. Label the first fence python and start it with "
206
+ "the literal comment # input so parser and returned candidate match.\n"
207
  "```python\n<program>\n```\n```generator\n<program>\n```"
208
+ "\n```reference\n<program>\n```\n```benchmark\n<program>\n```"
209
  )
210
  _CHALLENGE = _CHALLENGE_A + _CHALLENGE_B + _CHALLENGE_FORMAT
211
+ _GENERATOR_ONLY = _CHALLENGE
212
+ _ARBITER_ONLY = (
213
+ "Derive a third small-input oracle independently from the statement, without reading or "
214
+ "reusing any optimized candidate invariant. Return exactly one fenced reference block and no "
215
+ "prose. The program must be a literal exhaustive solver for small legal inputs. It must cover "
216
+ "operation order, ties, duplicates, and boundary states whenever those concepts occur in the "
217
+ "statement.\n```reference\n<program>\n```"
218
  )
219
+ _FLOAT_FORMAT = "".join((
220
+ "The validator compares output tokens literally even when the statement describes a numeric "
221
+ "tolerance. Match the fixed-decimal style and number of places shown consistently by all "
222
+ "statement samples. ",
223
+ "If samples show more than 15 significant decimal digits, do not use binary float for the final "
224
+ "computation: use decimal.Decimal with sufficient guard precision, including Decimal.sqrt or an "
225
+ "equivalent high-precision derivation. ",
226
+ "Never paste sample values or special-case their inputs; the same derivation and formatting must "
227
+ "cover every legal input.",
228
+ ))
229
+
230
+
231
+ _EXPLICIT_DECIMALS = re.compile(
232
+ r"(?:exactly\s+)?\d+\s+(?:digits?\s+after\s+the\s+decimal|decimal\s+places?)",
233
+ re.I,
234
  )
235
+ _FIXED_FORMAT = re.compile(r"\.(\d+)f")
236
+
237
+
238
+ def _sample_decimal_places(statement):
239
+ places = set()
240
+ for _stdin, expected in _samples(statement, 4):
241
+ for token in str(expected).split():
242
+ if _NUMBER.fullmatch(token) and "." in token.lower() and "e" not in token.lower():
243
+ places.add(len(token.rsplit(".", 1)[1]))
244
+ return next(iter(places)) if len(places) == 1 and 1 <= next(iter(places)) <= 30 else None
245
+
246
+
247
+ class _DecimalHypotTransformer(ast.NodeTransformer):
248
+ """Promote generic Euclidean primitives when public output requires high precision."""
249
+
250
+ def __init__(self):
251
+ self.changed = False
252
+
253
+ def visit_Call(self, node):
254
+ node = self.generic_visit(node)
255
+ if (isinstance(node.func, ast.Attribute)
256
+ and isinstance(node.func.value, ast.Name)
257
+ and node.func.value.id == "math" and node.func.attr == "hypot"
258
+ and len(node.args) == 2 and not node.keywords):
259
+ left, right = node.args
260
+ def square(value):
261
+ converted = ast.Call(
262
+ func=ast.Name(id="_RouterDecimal", ctx=ast.Load()),
263
+ args=[value], keywords=[],
264
+ )
265
+ return ast.BinOp(left=converted, op=ast.Mult(), right=converted)
266
+ self.changed = True
267
+ return ast.copy_location(ast.Call(
268
+ func=ast.Attribute(
269
+ value=ast.BinOp(left=square(left), op=ast.Add(), right=square(right)),
270
+ attr="sqrt", ctx=ast.Load(),
271
+ ),
272
+ args=[], keywords=[],
273
+ ), node)
274
+ return node
275
+
276
+
277
+ class _DecimalFsumTransformer(ast.NodeTransformer):
278
+ def visit_Call(self, node):
279
+ node = self.generic_visit(node)
280
+ if (isinstance(node.func, ast.Attribute)
281
+ and isinstance(node.func.value, ast.Name)
282
+ and node.func.value.id == "math" and node.func.attr == "fsum"
283
+ and len(node.args) == 1 and not node.keywords):
284
+ return ast.copy_location(ast.Call(
285
+ func=ast.Name(id="sum", ctx=ast.Load()),
286
+ args=[node.args[0], ast.Call(
287
+ func=ast.Name(id="_RouterDecimal", ctx=ast.Load()),
288
+ args=[ast.Constant(value=0)], keywords=[],
289
+ )], keywords=[],
290
+ ), node)
291
+ return node
292
+
293
+
294
+ def _promote_decimal_hypot(value, sample_places):
295
+ if sample_places <= 15 or "Decimal" in value or "decimal" in value:
296
+ return value
297
+ try:
298
+ tree = ast.parse(value)
299
+ transformer = _DecimalHypotTransformer()
300
+ tree = transformer.visit(tree)
301
+ if not transformer.changed:
302
+ return value
303
+ tree = _DecimalFsumTransformer().visit(tree)
304
+ insertion = 1 if (tree.body and isinstance(tree.body[0], ast.Expr)
305
+ and isinstance(tree.body[0].value, ast.Constant)
306
+ and isinstance(tree.body[0].value.value, str)) else 0
307
+ while (insertion < len(tree.body) and isinstance(tree.body[insertion], ast.ImportFrom)
308
+ and tree.body[insertion].module == "__future__"):
309
+ insertion += 1
310
+ setup = ast.parse(
311
+ "from decimal import Decimal as _RouterDecimal, getcontext as _router_decimal_context\n"
312
+ + "_router_decimal_context().prec = " + str(sample_places + 20) + "\n"
313
+ ).body
314
+ tree.body[insertion:insertion] = setup
315
+ ast.fix_missing_locations(tree)
316
+ return ast.unparse(tree) + "\n"
317
+ except (SyntaxError, ValueError, TypeError):
318
+ return value
319
+
320
+
321
+ def _normalize_float_program(code, prompt):
322
+ """Apply the public sample's literal float style without task identity or stored outputs."""
323
+ value = str(code or "")
324
+ statement = str(prompt or "")
325
+ if not _FLOAT_JUDGE.search(statement) or _EXPLICIT_DECIMALS.search(statement):
326
+ return value
327
+ try:
328
+ ast.parse(value)
329
+ except (SyntaxError, ValueError, TypeError):
330
+ return value
331
+ sample_places = _sample_decimal_places(statement)
332
+ if sample_places is None:
333
+ return value
334
+ updated = _FIXED_FORMAT.sub(lambda _match: "." + str(sample_places) + "f", value)
335
+ updated = _promote_decimal_hypot(updated, sample_places)
336
+ try:
337
+ ast.parse(updated)
338
+ except (SyntaxError, ValueError, TypeError):
339
+ return value
340
+ return updated
341
+
342
+
343
+ def _finish_candidate(code, prompt):
344
+ """Reject prose, truncation, and malformed programs before any expensive evidence stage."""
345
+ value = _normalize_float_program(code, prompt)
346
+ if not str(value).strip():
347
+ return ""
348
+ try:
349
+ ast.parse(value)
350
+ except (SyntaxError, ValueError, TypeError):
351
+ return ""
352
+ return value
353
 
354
 
355
  def _load(weights):
 
358
  except Exception as exc:
359
  raise ValueError("UID 101 clean settings are not valid JSON") from exc
360
  required = {
361
+ "schema", "kind", "non_code", "primary", "challenger", "generator", "reviewer", "rescue",
362
+ "batch_primary", "batch_challenger",
363
  "primary_max_tokens", "challenge_max_tokens", "review_max_tokens",
364
+ "batch_primary_max_tokens", "batch_challenge_max_tokens",
365
  "generator_max_tokens", "rescue_max_tokens", "sample_limit", "stress_cases",
366
  "min_consensus_cases", "critic",
367
  "temperature", "primary_reasoning", "challenge_reasoning", "review_reasoning",
368
+ "batch_primary_reasoning", "batch_challenge_reasoning",
369
  "generator_reasoning", "rescue_reasoning",
370
  }
371
  if not isinstance(data, dict) or set(data) != required:
372
  raise ValueError("UID 101 clean settings have an invalid schema")
373
  if data["schema"] != 3 or data["kind"] != _KIND:
374
  raise ValueError("UID 101 clean settings do not match this source")
375
+ for field in ("non_code", "primary", "challenger", "generator", "reviewer", "rescue",
376
+ "batch_primary", "batch_challenger"):
377
  if type(data[field]) is not int or not 0 <= data[field] < len(_MODELS):
378
  raise ValueError("UID 101 clean model index is invalid")
379
  if (type(data["primary_max_tokens"]) is not int
380
  or not 4096 <= data["primary_max_tokens"] <= 32768):
381
  raise ValueError("UID 101 clean primary token limit is invalid")
382
  for field in ("challenge_max_tokens", "generator_max_tokens", "review_max_tokens",
383
+ "rescue_max_tokens", "batch_primary_max_tokens", "batch_challenge_max_tokens"):
384
  if type(data[field]) is not int or not 2048 <= data[field] <= 32768:
385
  raise ValueError("UID 101 clean fallback token limit is invalid")
386
  if type(data["sample_limit"]) is not int or not 1 <= data["sample_limit"] <= 4:
 
407
  if type(data["temperature"]) not in (int, float) or data["temperature"] != 0:
408
  raise ValueError("UID 101 clean temperature must be deterministic")
409
  for field in ("primary_reasoning", "challenge_reasoning", "generator_reasoning",
410
+ "review_reasoning", "rescue_reasoning", "batch_primary_reasoning",
411
+ "batch_challenge_reasoning"):
412
  if data[field] not in ("none", "low", "medium", "high"):
413
  raise ValueError("UID 101 clean reasoning effort is invalid")
414
  return data
 
506
  return True
507
 
508
 
509
+ def _run_program(code, stdin_text, timeout, argv=(), deadline=None):
510
+ if deadline is not None:
511
+ remaining = deadline - time.monotonic()
512
+ if remaining <= 0:
513
+ return None
514
+ timeout = min(float(timeout), max(0.01, remaining))
515
  try:
516
  run = subprocess.run(
517
  [sys.executable, "-I", "-c", code, *[str(value) for value in argv]],
 
523
  return run.stdout if run.returncode == 0 else None
524
 
525
 
526
+ def _check(code, samples, numeric, deadline=None):
527
  if not code.strip():
528
  return 0, max(1, len(samples)), (
529
  samples[0][0] if samples else "", "<empty>",
 
540
  passed = 0
541
  first_bad = None
542
  for stdin, expected in samples:
543
+ observed = _run_program(code, stdin, _CASE_TIMEOUT, deadline=deadline)
544
+ # The validator's LCB grader is literal-token exact, including numeric tasks.
545
+ good = observed is not None and str(observed).split() == str(expected).split()
546
  if good:
547
  passed += 1
548
  elif first_bad is None:
 
565
  def _named_programs(answer):
566
  programs = {}
567
  python_blocks = []
568
+ text = str(answer or "")
569
+ # Recover explicitly labelled tooling blocks first. A provider may emit an unlabelled closing
570
+ # fence after raw candidate code; generic left-to-right fence pairing can otherwise consume the
571
+ # next labelled opener as a closer and hide valid tooling.
572
+ for label in ("generator", "reference", "benchmark"):
573
+ match = re.search(r"```" + label + r"\s*\n(.*?)```", text, re.I | re.S)
574
+ if match:
575
+ code = match.group(1).strip()
576
+ if code:
577
+ programs[label] = code + "\n"
578
+ for match in re.finditer(r"```([^\n`]*)\n(.*?)```", text, re.I | re.S):
579
+ # Some providers add one extra fence marker around an otherwise correctly labelled block.
580
+ # Normalize only fence punctuation; arbitrary prose labels are never accepted as programs.
581
+ tag = match.group(1).strip().lower().strip("`")
582
  lines = [line for line in match.group(2).strip().splitlines()
583
  if line.strip().lower() not in ("<program>", "program")]
584
  code = "\n".join(lines).strip()
 
588
  python_blocks.append(code + "\n")
589
  if "candidate" not in programs:
590
  programs["candidate"] = code + "\n"
591
+ elif tag in ("generator", "reference", "benchmark") and tag not in programs:
592
  programs[tag] = code + "\n"
593
  elif tag == "candidate" and "candidate" not in programs:
594
  # Backward-compatible parsing for stored development responses. New prompts never ask
 
613
  return blocks
614
 
615
 
616
+ def _generated_inputs(generator, case_limit, require_scale=True, deadline=None):
617
  if not generator or not generator.strip():
618
  return []
619
  # A generator that ignores the supplied scale only exercises toy inputs and can hide
 
621
  direct_size = re.search(r"(?:sys\.)?argv\s*\[\s*2\s*\]", generator)
622
  sliced_args = (re.search(r"(?:sys\.)?argv\s*\[\s*1\s*:\s*\]", generator)
623
  and re.search(r"\[\s*1\s*\]", generator))
624
+ if require_scale and direct_size is None and not sliced_args:
625
  return []
626
  try:
627
  compile(generator, "<generator>", "exec")
628
  except (SyntaxError, ValueError, TypeError):
629
  return []
630
+ local_deadline = time.monotonic() + _STRESS_SECONDS
631
+ deadline = min(local_deadline, deadline) if deadline is not None else local_deadline
632
  cases = []
633
+ observations = []
634
+ if case_limit <= 1:
635
+ size_schedule = _STRESS_SIZES[:case_limit]
636
+ elif case_limit < len(_STRESS_SIZES):
637
+ last = len(_STRESS_SIZES) - 1
638
+ size_schedule = tuple(
639
+ _STRESS_SIZES[round(index * last / (case_limit - 1))]
640
+ for index in range(case_limit)
641
+ )
642
+ else:
643
+ size_schedule = tuple(
644
+ _STRESS_SIZES[index % len(_STRESS_SIZES)] for index in range(case_limit)
645
+ )
646
+ for index, size in enumerate(size_schedule):
647
  if time.monotonic() >= deadline:
648
  break
649
+ stdin = _run_program(generator, "", _GEN_TIMEOUT, (index + 1, size), deadline)
 
650
  if not stdin or not stdin.strip() or len(stdin) > 20000:
651
  continue
652
+ observations.append((size, stdin))
653
  if stdin not in cases:
654
  cases.append(stdin)
655
+ if require_scale:
656
+ small = [value for size, value in observations if size <= 5]
657
+ large = [value for size, value in observations if size >= 24]
658
+ if not small or not large:
659
+ return []
660
+
661
+ def shape(value):
662
+ tokens = str(value).split()
663
+ integers = []
664
+ for token in tokens[:32]:
665
+ try:
666
+ integers.append(abs(int(token)))
667
+ except ValueError:
668
+ integers.append(0)
669
+ return len(str(value)), len(tokens), integers
670
+
671
+ small_shapes = [shape(value) for value in small]
672
+ large_shapes = [shape(value) for value in large]
673
+ small_chars = max(row[0] for row in small_shapes)
674
+ small_tokens = max(row[1] for row in small_shapes)
675
+ grew = (max(row[0] for row in large_shapes) >= max(8, int(small_chars * 1.25))
676
+ or max(row[1] for row in large_shapes) >= max(3, int(small_tokens * 1.25)))
677
+ width = max((len(row[2]) for row in small_shapes + large_shapes), default=0)
678
+ for position in range(width):
679
+ low = max((row[2][position] for row in small_shapes
680
+ if position < len(row[2])), default=0)
681
+ high = max((row[2][position] for row in large_shapes
682
+ if position < len(row[2])), default=0)
683
+ if high > low and high >= max(2, int(low * 1.25)):
684
+ grew = True
685
+ break
686
+ if not grew:
687
+ return []
688
  return cases
689
 
690
 
691
+ def _deep_code_risk(text):
692
+ """Recognize broad derivation risk without prompt identity or stored-solution routing."""
693
+ value = str(text)
694
+ signals = sum(bool(pattern.search(value)) for pattern in (
695
+ _MANY_OUTPUT_RISK, _MODULAR_RISK, _COUNTING_RISK,
696
+ ))
697
+ return signals >= 2
698
+
699
+
700
+ def _fixed_width_integer_risk(code):
701
+ """Flag lossy Python integer storage; this is a language safety check, not task dispatch."""
702
+ try:
703
+ tree = ast.parse(code)
704
+ except (SyntaxError, ValueError, TypeError):
705
+ return False
706
+ has_fixed_array = False
707
+ has_integer_narrowing = False
708
+ for node in ast.walk(tree):
709
+ if (isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
710
+ and node.func.id == "array" and node.args
711
+ and isinstance(node.args[0], ast.Constant)
712
+ and str(node.args[0].value) in {"b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"}):
713
+ has_fixed_array = True
714
+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitAnd):
715
+ for operand in (node.left, node.right):
716
+ if (isinstance(operand, ast.Constant) and type(operand.value) is int
717
+ and operand.value >= (1 << 31) - 1
718
+ and (operand.value + 1) & operand.value == 0):
719
+ has_integer_narrowing = True
720
+ return has_fixed_array or has_integer_narrowing
721
+
722
+
723
+ def _expression_degree(node, degrees=None):
724
+ """Conservative polynomial degree used only for generic allocation/index diagnostics."""
725
+ degrees = degrees or {}
726
+ if node is None or isinstance(node, ast.Constant):
727
+ return 0
728
+ if isinstance(node, ast.Name):
729
+ return degrees.get(node.id, 1)
730
+ if isinstance(node, ast.UnaryOp):
731
+ return _expression_degree(node.operand, degrees)
732
+ if isinstance(node, ast.BinOp):
733
+ left = _expression_degree(node.left, degrees)
734
+ right = _expression_degree(node.right, degrees)
735
+ if isinstance(node.op, (ast.Add, ast.Sub, ast.Mod)):
736
+ return max(left, right)
737
+ if isinstance(node.op, ast.Mult):
738
+ return left + right
739
+ if isinstance(node.op, (ast.Div, ast.FloorDiv)):
740
+ return left
741
+ if (isinstance(node.op, ast.Pow) and isinstance(node.right, ast.Constant)
742
+ and type(node.right.value) is int and 0 <= node.right.value <= 8):
743
+ return left * node.right.value
744
+ return max(left, right)
745
+ if isinstance(node, ast.Call):
746
+ return max((_expression_degree(arg, degrees) for arg in node.args), default=1)
747
+ return max((_expression_degree(child, degrees)
748
+ for child in ast.iter_child_nodes(node)), default=0)
749
+
750
+
751
+ def _repeated_list_size(node):
752
+ if not isinstance(node, ast.BinOp) or not isinstance(node.op, ast.Mult):
753
+ return None
754
+ if isinstance(node.left, (ast.List, ast.Tuple)):
755
+ return node.right
756
+ if isinstance(node.right, (ast.List, ast.Tuple)):
757
+ return node.left
758
+ return None
759
+
760
+
761
+ def _allocation_extent(node):
762
+ repeated = _repeated_list_size(node)
763
+ if repeated is not None:
764
+ return repeated
765
+ if (isinstance(node, ast.ListComp) and len(node.generators) == 1
766
+ and isinstance(node.generators[0].iter, ast.Call)
767
+ and isinstance(node.generators[0].iter.func, ast.Name)
768
+ and node.generators[0].iter.func.id == "range"):
769
+ args = node.generators[0].iter.args
770
+ if len(args) == 1:
771
+ return args[0]
772
+ if len(args) >= 2:
773
+ return args[1]
774
+ return None
775
+
776
+
777
+ def _allocation_index_risk(code):
778
+ """Flag an index whose polynomial growth outruns its list allocation growth."""
779
+ try:
780
+ tree = ast.parse(code)
781
+ except (SyntaxError, ValueError, TypeError):
782
+ return False
783
+ allocations = {}
784
+ degrees = {}
785
+ for node in ast.walk(tree):
786
+ if isinstance(node, (ast.Assign, ast.AnnAssign)):
787
+ value = node.value
788
+ targets = node.targets if isinstance(node, ast.Assign) else [node.target]
789
+ size = _allocation_extent(value)
790
+ for target in targets:
791
+ if isinstance(target, ast.Name):
792
+ if size is None:
793
+ degrees[target.id] = _expression_degree(value, degrees)
794
+ else:
795
+ allocations[target.id] = _expression_degree(size, degrees)
796
+ for node in ast.walk(tree):
797
+ if not isinstance(node, ast.Subscript) or not isinstance(node.value, ast.Name):
798
+ continue
799
+ if node.value.id not in allocations:
800
+ continue
801
+ index = node.slice.value if isinstance(node.slice, ast.Index) else node.slice
802
+ if _expression_degree(index, degrees) > allocations[node.value.id]:
803
+ return True
804
+ return False
805
+
806
+
807
  def _quality(code):
808
  """Return a small task-independent static safety score; never infer a task identity."""
809
  try:
 
886
  return 1.0 / (1.0 + math.exp(-max(-30.0, min(30.0, logit))))
887
 
888
 
889
+ def _reference_valid(reference, samples, numeric, deadline=None):
890
  if not reference or not reference.strip():
891
  return False
892
  try:
893
  compile(reference, "<reference>", "exec")
894
  except (SyntaxError, ValueError, TypeError):
895
  return False
896
+ if not samples:
897
+ return True
898
+ completed = 0
899
  for stdin, expected in samples:
900
+ observed = _run_program(reference, stdin, _STRESS_TIMEOUT, deadline=deadline)
901
+ if observed is None:
902
+ continue
903
+ completed += 1
904
+ if not _tokens_match(observed, expected, numeric):
905
  return False
906
+ return completed > 0
907
 
908
 
909
+ def _trusted_oracle(reference_a, reference_b, samples, generated, numeric, minimum,
910
+ deadline=None):
911
+ if (not _reference_valid(reference_a, samples, numeric, deadline)
912
+ or not _reference_valid(reference_b, samples, numeric, deadline)):
913
  return []
914
  agreed = []
915
  for stdin in generated:
916
+ left = _run_program(reference_a, stdin, _STRESS_TIMEOUT, deadline=deadline)
917
+ right = _run_program(reference_b, stdin, _STRESS_TIMEOUT, deadline=deadline)
918
+ if left is None or right is None:
919
+ continue
920
+ if not _tokens_match(left, right, numeric):
921
  return []
922
  agreed.append((stdin, left))
923
  return agreed if len(agreed) >= minimum else []
924
 
925
 
926
+ def _first_trusted_oracle(references, samples, generated, numeric, minimum, deadline=None):
927
+ """Admit an oracle only when two independently returned references fully agree."""
928
+ local_deadline = time.monotonic() + _ORACLE_BUDGET_S
929
+ deadline = min(local_deadline, deadline) if deadline is not None else local_deadline
930
+ for left_index in range(len(references)):
931
+ for right_index in range(left_index + 1, len(references)):
932
+ agreed = _trusted_oracle(
933
+ references[left_index], references[right_index], samples, generated,
934
+ numeric, minimum, deadline,
935
+ )
936
+ if agreed:
937
+ return agreed
938
+ return []
939
+
940
+
941
  def _candidate_evidence(candidates, samples, generated, numeric, oracle_cases=(), critic=None,
942
+ large_input=False, performance_inputs=(), deadline=None):
943
  rows = []
944
  outputs = []
945
  for index, code in enumerate(candidates):
946
+ local_deadline = time.monotonic() + _CANDIDATE_EVIDENCE_BUDGET_S
947
+ candidate_deadline = (min(local_deadline, deadline)
948
+ if deadline is not None else local_deadline)
949
+ passed, total, failure = _check(code, samples, numeric, candidate_deadline)
950
+ sample_valid = bool(code.strip() and failure is None and passed == total)
951
+ if sample_valid:
952
+ values = [_run_program(code, stdin, _STRESS_TIMEOUT, deadline=candidate_deadline)
953
+ for stdin in generated]
954
+ performance_started = time.monotonic()
955
+ performance_values = [
956
+ _run_program(code, stdin, _PERFORMANCE_MARGIN_TIMEOUT, deadline=candidate_deadline)
957
+ for stdin in performance_inputs
958
+ ]
959
+ performance_latency = time.monotonic() - performance_started
960
+ else:
961
+ # A program that fails the statement samples can never be returned. Do not spend its
962
+ # reviewer reserve on generated or scale probes.
963
+ values = [None] * len(generated)
964
+ performance_values = [None] * len(performance_inputs)
965
+ performance_latency = 0.0
966
  outputs.append(values)
967
  rows.append({
968
  "index": index,
969
  "sample_passed": passed,
970
  "sample_total": total,
971
+ "sample_valid": sample_valid,
972
  "completed": sum(value is not None for value in values),
973
  "consensus": 0,
974
  "oracle": 0,
975
  "oracle_total": len(oracle_cases),
976
  "feasible": not (large_input and _has_input_scaled_nested_loop(code)),
977
+ "integer_safe": not _fixed_width_integer_risk(code),
978
+ "boundary_safe": not _allocation_index_risk(code),
979
+ "performance_completed": sum(value is not None for value in performance_values),
980
+ "performance_total": len(performance_inputs),
981
+ "performance_latency_s": performance_latency,
982
  "critic": 0.0,
983
  "quality": _quality(code),
984
  })
 
1007
  rows[candidate_index]["consensus"] += 1
1008
  for row, code in zip(rows, candidates):
1009
  for stdin, expected in oracle_cases:
1010
+ observed = _run_program(code, stdin, _STRESS_TIMEOUT, deadline=deadline)
1011
  if observed is not None and _tokens_match(observed, expected, numeric):
1012
  row["oracle"] += 1
1013
  row["critic"] = _critic_score(
 
1023
  def _best_candidate(candidates, rows):
1024
  viable = [row for row in rows if row["sample_valid"]]
1025
  pool = viable if viable else rows
1026
+ # Executed largest-input evidence is stronger than a conservative static hint. If at least
1027
+ # one sample-valid candidate finishes every generated performance probe inside the published
1028
+ # ten-second case contract with a two-second reserve, exclude candidates that do not.
1029
+ performance_viable = [
1030
+ row for row in pool
1031
+ if (not row.get("performance_total")
1032
+ or row.get("performance_completed") == row.get("performance_total"))
1033
+ ]
1034
+ if performance_viable:
1035
+ pool = performance_viable
1036
  best = max(pool, key=lambda row: (
1037
+ row["sample_valid"], row["sample_passed"],
1038
  bool(row["oracle_total"] and row["oracle"] == row["oracle_total"]),
1039
+ row["oracle"], row["consensus"], row.get("integer_safe", True),
1040
+ row.get("boundary_safe", True), row.get("feasible", True),
1041
+ bool(not row.get("performance_total")
1042
+ or row.get("performance_completed") == row.get("performance_total")),
1043
+ -row.get("performance_latency_s", 0.0), row["critic"],
1044
  row["completed"], row["quality"], -row["index"],
1045
  ))
1046
  return candidates[best["index"]], best
 
1064
  return "\n\n".join(notes)
1065
 
1066
 
1067
+ def _hard_bounded_call(callback, timeout=_HARD_CALL_TIMEOUT_S):
1068
+ """Return an empty response when a provider call exceeds a real wall-clock deadline."""
1069
+ result = queue.Queue(maxsize=1)
1070
+
1071
+ def run():
1072
+ try:
1073
+ result.put((True, callback()))
1074
+ except BaseException as exc:
1075
+ result.put((False, exc))
1076
+
1077
+ worker = threading.Thread(target=run, daemon=True)
1078
+ worker.start()
1079
+ worker.join(timeout)
1080
+ if worker.is_alive():
1081
+ return ""
1082
+ ok, value = result.get_nowait()
1083
+ if not ok:
1084
+ raise value
1085
+ return value
1086
+
1087
+
1088
  def build_agent(weights):
1089
  config = _load(weights)
1090
+ state = {"started": None, "done": 0, "code_done": 0,
1091
+ "reserved_code_s": 0.0, "latency": {}}
1092
 
1093
  def invoke(call_model, model_index, prompt, max_tokens, reasoning):
1094
  params = {
 
1098
  }
1099
  started = time.monotonic()
1100
  try:
1101
+ # The runtime/harness owns the single request deadline. Nesting another daemon-thread
1102
+ # timeout here leaves an uncancellable provider request alive across later tasks.
1103
  return call_model(
1104
  _MODELS[model_index], [{"role": "user", "content": prompt}], params
1105
  )
 
1109
  return ""
1110
  finally:
1111
  elapsed = time.monotonic() - started
1112
+ floor = _MODEL_CALL_FLOOR_S[model_index]
1113
+ prior = state["latency"].get(model_index, floor)
1114
+ state["latency"][model_index] = max(floor, 0.7 * prior + 0.3 * elapsed)
1115
 
1116
  def agent(prompt, call_model):
1117
  if state["started"] is None:
 
1119
  state["done"] += 1
1120
  epoch_deadline = state["started"] + _RUN_BUDGET_S
1121
 
1122
+ def room_for(model_index, multiplier=1.0, stage_cap=None):
1123
  remaining = max(0, _EXPECTED_TASKS - state["done"])
1124
+ estimate = state["latency"].get(model_index, _MODEL_CALL_FLOOR_S[model_index])
1125
+ # stage_cap used to reduce the estimate without enforcing a matching wall timeout.
1126
+ # Never admit a long provider call on that optimistic fiction.
1127
+ reserve = max(
1128
+ _RESERVE_PER_TASK_S * remaining,
1129
+ state["reserved_code_s"],
1130
+ )
1131
  return (time.monotonic() + max(_MIN_CALL_WINDOW_S, estimate * multiplier)
1132
+ + reserve) < epoch_deadline
1133
 
1134
  original = str(prompt)
1135
  if not _is_code(original):
1136
+ state["reserved_code_s"] = 0.0
1137
  return invoke(
1138
+ call_model, config["non_code"], _non_code_request(original),
1139
  config["primary_max_tokens"], config["primary_reasoning"],
1140
  )
1141
 
1142
+ state["code_done"] += 1
1143
+ remaining_code = max(0, _EXPECTED_CODE_TASKS - state["code_done"])
1144
+ state["reserved_code_s"] = _RESERVE_PER_CODE_TASK_S * remaining_code
1145
+ model_calls = 0
1146
+
1147
+ def ask(model_index, request_text, max_tokens, reasoning):
1148
+ nonlocal model_calls
1149
+ if model_calls >= _MAX_CODE_CALLS:
1150
+ return ""
1151
+ model_calls += 1
1152
+ return invoke(call_model, model_index, request_text, max_tokens, reasoning)
1153
+
1154
  samples = _samples(original, config["sample_limit"])
1155
  numeric = bool(_FLOAT_JUDGE.search(original))
1156
+ finish = lambda code: _finish_candidate(code, original)
1157
  sequential_risk = bool(_SEQUENTIAL_RISK.search(original))
1158
  mutable_graph_risk = bool(_MUTABLE_GRAPH_RISK.search(original))
1159
  large_input = bool(_LARGE_N.search(original))
1160
+ deep_risk = _deep_code_risk(original)
1161
+ batch_output_risk = bool(
1162
+ _MANY_OUTPUT_RISK.search(original) and _COUNTING_RISK.search(original)
1163
+ )
1164
+ high_risk = sequential_risk or mutable_graph_risk or large_input or deep_risk
1165
+ request = (original + "\n\nGeneral reliability protocol: " + _SOLVE
1166
+ + "\n\nResource-bound audit: " + _RESOURCE_AUDIT
1167
+ + "\n\nAllocation/index audit: " + _INDEX_BOUND_AUDIT)
1168
  if numeric:
1169
  request += "\n\nNumeric-output protocol: " + _FLOAT_FORMAT
1170
  if sequential_risk:
 
1175
  request += ("\n\nLarge-input protocol: Prove the asymptotic bound for the largest N. "
1176
  "Reject any nested iteration whose inner bound grows with the input unless "
1177
  "a strict amortized or logarithmic bound is established.")
1178
+ if deep_risk:
1179
+ request += ("\n\nLogical-equivalence audit: " + _EQUIVALENCE_AUDIT
1180
+ + _QUANTIFIER_AUDIT + _INDEPENDENCE_AUDIT)
1181
+ primary_model = config["batch_primary"] if batch_output_risk else config["primary"]
1182
+ challenger_model = config["batch_challenger"] if batch_output_risk else config["challenger"]
1183
+ primary_max_tokens = (config["batch_primary_max_tokens"] if batch_output_risk
1184
+ else config["primary_max_tokens"])
1185
+ challenge_max_tokens = (config["batch_challenge_max_tokens"] if batch_output_risk
1186
+ else config["challenge_max_tokens"])
1187
+ primary_reasoning = (config["batch_primary_reasoning"] if batch_output_risk
1188
+ else config["primary_reasoning"])
1189
+ challenge_reasoning = (config["batch_challenge_reasoning"] if batch_output_risk
1190
+ else config["challenge_reasoning"])
1191
+ if batch_output_risk:
1192
+ request += (
1193
+ "\n\nBatched-output complexity audit: " + _BATCH_OUTPUT_AUDIT
1194
+ + "\n\nExecutable performance target: construct a largest-legal-input "
1195
+ "benchmark from the statement. The returned program must preserve the derived "
1196
+ "semantics and target at most 6 seconds per official case on one CPU, leaving "
1197
+ "headroom below the 10-second validator limit."
1198
+ )
1199
+ primary_answer = ask(
1200
+ primary_model, request + "\n\nOutput protocol:\n" + _PRIMARY_FORMAT,
1201
+ primary_max_tokens, primary_reasoning,
1202
  )
1203
  primary_programs = _named_programs(primary_answer)
1204
+ first = finish(_returnable_candidate(primary_answer, primary_programs))
1205
  references = []
1206
  if primary_programs.get("reference"):
1207
  references.append(primary_programs["reference"])
1208
+ if deep_risk:
1209
+ # Do not anchor a provider-diverse solver on the primary program. Agreement after
1210
+ # seeing the same compressed-state idea is correlated evidence, not independence.
1211
+ challenge_request = (
1212
+ request + "\n\nLogical-equivalence audit: " + _EQUIVALENCE_AUDIT
1213
+ + _INDEPENDENCE_AUDIT
1214
+ + "\n\nIndependent challenge protocol:\n" + _CHALLENGE
1215
+ )
1216
+ else:
1217
+ # Preserve provider independence on ordinary tasks too: the challenger receives the
1218
+ # statement and generic audits, never the primary program.
1219
+ challenge_request = (
1220
+ request + "\n\nLogical-equivalence audit: " + _EQUIVALENCE_AUDIT
1221
+ + _INDEPENDENCE_AUDIT
1222
+ + "\n\nIndependent challenge protocol: derive from the statement before "
1223
+ "considering any other implementation.\n" + _CHALLENGE
1224
+ )
1225
+ challenge_answer = (ask(
1226
+ challenger_model, challenge_request,
1227
+ challenge_max_tokens, challenge_reasoning,
1228
+ ) if room_for(challenger_model, 1.5) else "")
1229
+ evidence_deadline = min(
1230
+ epoch_deadline - 8.0, time.monotonic() + _EVIDENCE_BUDGET_S
1231
  )
 
 
 
 
1232
  programs = _named_programs(challenge_answer)
1233
+ second = finish(_returnable_candidate(challenge_answer, programs))
1234
  generator = programs.get("generator")
1235
+ benchmark = programs.get("benchmark")
1236
  if programs.get("reference"):
1237
  references.append(programs["reference"])
1238
  candidates = [first]
1239
  if second.strip():
1240
  candidates.append(second)
1241
+ generated = _generated_inputs(
1242
+ generator, config["stress_cases"], require_scale=not deep_risk, deadline=evidence_deadline
1243
+ )
1244
+ generator_candidate_added = False
1245
+ # Preserve the final model call for an independent reviewer. A missing or malformed
1246
+ # challenge must not spend that reserve on generator-only tooling.
1247
+ if (not generated or (not benchmark and not deep_risk)) and model_calls < 2 and room_for(
1248
+ config["generator"], 1.0, 45.0):
1249
  generator_request = original + "\n\nInput-generator protocol: " + _GENERATOR_ONLY
1250
+ generator_answer = ask(
1251
+ config["generator"], generator_request,
1252
  config["generator_max_tokens"], config["generator_reasoning"],
1253
  )
1254
  generated_programs = _named_programs(generator_answer)
1255
  if not generated_programs:
1256
  plain_blocks = _plain_program_blocks(generator_answer)
1257
+ if len(plain_blocks) in (2, 3):
1258
  generated_programs = {
1259
  "generator": plain_blocks[0], "reference": plain_blocks[1],
1260
  }
1261
+ if len(plain_blocks) == 3:
1262
+ generated_programs["benchmark"] = plain_blocks[2]
1263
+ elif not plain_blocks:
1264
+ # Providers occasionally ignore a tooling-only format and return a complete
1265
+ # raw solution. It is not generator evidence, but it is still an independently
1266
+ # derived runtime candidate and must not be silently discarded.
1267
+ recovered = _source(generator_answer)
1268
+ if recovered.strip():
1269
+ candidates.append(finish(recovered))
1270
+ generator_candidate_added = True
1271
+ generated_candidate = generated_programs.get("candidate")
1272
+ if generated_candidate and generated_candidate.strip():
1273
+ candidates.append(finish(generated_candidate))
1274
+ generator_candidate_added = True
1275
+ generator = generated_programs.get("generator") or ""
1276
+ benchmark = generated_programs.get("benchmark") or benchmark
1277
  if generated_programs.get("reference"):
1278
  references.append(generated_programs["reference"])
1279
+ generated = _generated_inputs(
1280
+ generator, config["stress_cases"], require_scale=not deep_risk
1281
+ )
1282
+ # Every returned benchmark is only admission evidence and uses an eight-second
1283
+ # timeout, leaving margin under the validator's published ten-second case contract.
1284
+ performance_inputs = _generated_inputs(
1285
+ benchmark, 2, require_scale=False, deadline=evidence_deadline
1286
+ ) if benchmark else []
1287
+ oracle_cases = _first_trusted_oracle(
1288
+ references, samples, generated, numeric, config["min_consensus_cases"], evidence_deadline
1289
+ )
1290
+ # High-risk statements may have many sample-valid but semantically wrong compressed or
1291
+ # greedy programs. Ask a provider-diverse model for a third literal oracle when the first
1292
+ # two stages did not establish one. A single reference is never trusted by itself.
1293
+ if (generated and not oracle_cases and not deep_risk
1294
+ and model_calls < 2 and room_for(config["generator"], 1.5)):
1295
+ arbiter_answer = ask(
1296
+ config["generator"], original + "\n\nOracle protocol: " + _ARBITER_ONLY,
1297
+ config["generator_max_tokens"], config["generator_reasoning"],
1298
+ )
1299
+ arbiter_programs = _named_programs(arbiter_answer)
1300
+ arbiter_reference = arbiter_programs.get("reference")
1301
+ if not arbiter_reference:
1302
+ plain_blocks = _plain_program_blocks(arbiter_answer)
1303
+ if len(plain_blocks) == 1:
1304
+ arbiter_reference = plain_blocks[0]
1305
+ if (arbiter_reference and arbiter_reference.strip()
1306
+ and arbiter_reference not in references):
1307
+ references.append(arbiter_reference)
1308
+ oracle_cases = _first_trusted_oracle(
1309
+ references, samples, generated, numeric, config["min_consensus_cases"], evidence_deadline
1310
  )
 
 
 
 
1311
  rows, outputs = _candidate_evidence(
1312
+ candidates, samples, generated, numeric, oracle_cases, config["critic"], large_input, performance_inputs, evidence_deadline
1313
  )
1314
  best, best_row = _best_candidate(candidates, rows)
1315
+ # Two independent generations are normally sufficient. A third call is admitted
1316
+ # only below, when execution/oracle evidence cannot safely accept either candidate.
1317
  disagreement = _disagreement_note(generated, outputs)
1318
+ fixed_width_risk = any(_fixed_width_integer_risk(code) for code in candidates)
1319
+ boundary_risk = any(_allocation_index_risk(code) for code in candidates)
1320
+ review_required = high_risk or fixed_width_risk or boundary_risk
1321
+ performance_ok = (
1322
+ not performance_inputs
1323
+ or best_row["performance_completed"] == best_row["performance_total"]
1324
+ )
1325
+ # A generator-format violation may still yield a complete independent program.
1326
+ # On an ordinary statement, a sample-valid arbitrary-precision candidate is strictly safer
1327
+ # than a fixed-width candidate that needs collision/bound proofs. This is a generic language
1328
+ # safety fallback, not task routing or answer lookup.
1329
+ if (generator_candidate_added and not high_risk and fixed_width_risk
1330
+ and best_row["sample_valid"] and best_row.get("integer_safe", True)
1331
+ and performance_ok):
1332
+ return finish(best)
1333
+ if (best_row["sample_valid"] and performance_ok and oracle_cases
1334
+ and best_row["oracle"] == best_row["oracle_total"]
1335
+ and len(generated) >= config["min_consensus_cases"]):
1336
+ return finish(best)
1337
+ if (not review_required and best_row["sample_valid"] and performance_ok and (
1338
  (oracle_cases and best_row["oracle"] == best_row["oracle_total"])
1339
+ or (not high_risk and not oracle_cases and len(candidates) >= 2
1340
  and len(generated) >= config["min_consensus_cases"]
1341
  and all(row["sample_valid"] for row in rows)
1342
  and best_row["consensus"] == len(generated)
1343
  and best_row["completed"] == len(generated)))):
1344
+ return finish(best)
1345
 
1346
  candidate_text = "\n\n".join(
1347
  "Candidate " + str(index + 1) + ":\n" + code
1348
  for index, code in enumerate(candidates)
1349
  )
1350
+ if deep_risk:
1351
+ # Correctness evidence selects the repair seed; no task identity or stored answer is used.
1352
+ # The reviewer sees only the current statement, generic audit policy, and runtime candidate.
1353
+ review_request = (
1354
+ request + "\n\nPerformance repair protocol: " + _PERFORMANCE_REPAIR
1355
+ + "\n\nLogical-equivalence audit: " + _EQUIVALENCE_AUDIT
1356
+ + _INDEPENDENCE_AUDIT
1357
+ + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
1358
+ + "\n\nEvidence-selected program to repair:\n" + best
1359
+ )
1360
+ else:
1361
+ review_request = (
1362
+ request + "\n\nIndependent review protocol: " + _REVIEW
1363
+ + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
1364
+ + "\n\nPrograms under review:\n" + candidate_text
1365
+ )
1366
  if disagreement:
1367
  review_request += (
1368
  "\n\nThe programs disagreed on small generated inputs. These outputs are evidence "
1369
  "of disagreement only; none is a trusted oracle.\n" + disagreement
1370
  )
1371
+ narrowed = [str(index + 1) for index, code in enumerate(candidates)
1372
+ if _fixed_width_integer_risk(code)]
1373
+ if narrowed:
1374
+ review_request += (
1375
+ "\n\nStatic language-safety diagnostics: candidate(s) " + ", ".join(narrowed)
1376
+ + " use fixed-width integer storage or masking. " + _INTEGER_SAFETY
1377
+ )
1378
+ boundary_candidates = [str(index + 1) for index, code in enumerate(candidates)
1379
+ if _allocation_index_risk(code)]
1380
+ if boundary_candidates:
1381
+ review_request += (
1382
+ "\n\nStatic allocation/index diagnostics: candidate(s) "
1383
+ + ", ".join(boundary_candidates) + " contain a subscript expression whose "
1384
+ "growth outruns the tracked list allocation. " + _INDEX_BOUND_AUDIT
1385
+ )
1386
  if oracle_cases:
1387
  counterexamples = []
1388
  for stdin, expected in oracle_cases:
 
1400
  "\n\nTwo independent small references agreed on these counterexamples:\n"
1401
  + "\n\n".join(counterexamples)
1402
  )
1403
+ third = finish(_source(ask(
1404
+ config["reviewer"], review_request, config["review_max_tokens"],
1405
  config["review_reasoning"],
1406
+ ))) if room_for(config["reviewer"], 1.0, 75.0) else ""
1407
+ reviewer_row = None
1408
  if third.strip():
1409
  candidates.append(third)
1410
+ review_deadline = min(
1411
+ epoch_deadline - 8.0,
1412
+ time.monotonic() + _CANDIDATE_EVIDENCE_BUDGET_S,
1413
+ )
1414
+ third_rows, third_outputs = _candidate_evidence(
1415
+ [third], samples, generated, numeric, oracle_cases, config["critic"],
1416
+ large_input, performance_inputs, review_deadline,
1417
+ )
1418
+ if third_rows:
1419
+ third_rows[0]["index"] = len(candidates) - 1
1420
+ rows.extend(third_rows)
1421
+ outputs.extend(third_outputs)
1422
+ reviewer_row = third_rows[0]
1423
  best, best_row = _best_candidate(candidates, rows)
1424
+ if model_calls >= _MAX_CODE_CALLS:
1425
+ return finish(best)
1426
+ # A reviewer is another candidate, never an authority. The same execution,
1427
+ # oracle, scale, and learned-critic evidence selects among every generated program.
1428
+ performance_ok = (
1429
+ not performance_inputs
1430
+ or best_row["performance_completed"] == best_row["performance_total"]
1431
+ )
1432
+ if (best_row["sample_valid"] and performance_ok and (
1433
  (oracle_cases and best_row["oracle"] == best_row["oracle_total"])
1434
+ or (not high_risk and not oracle_cases
1435
  and best_row["consensus"] >= config["min_consensus_cases"]
1436
+ and best_row["completed"] >= config["min_consensus_cases"])
1437
+ or (high_risk and not oracle_cases
1438
+ and len(candidates) >= 3
1439
+ and len(generated) >= config["min_consensus_cases"]
1440
+ and sum(row["sample_valid"] for row in rows) >= 3
1441
+ and best_row["consensus"] == len(generated)
1442
+ and best_row["completed"] == len(generated)))):
1443
+ return finish(best)
1444
 
1445
+ # Preserve sample-valid answers on ordinary tasks when there is no contradictory evidence.
1446
+ # High-risk tasks may not use this sample-only escape hatch: they must establish a dual
1447
+ # oracle, unanimous three-way generated-case consensus, or continue to independent rescue.
1448
+ if (best_row["sample_valid"] and performance_ok
1449
+ and ((oracle_cases and best_row["oracle"] == best_row["oracle_total"])
1450
+ or (not high_risk and not oracle_cases))):
1451
+ return finish(best)
1452
 
1453
  passed, total, failure = _check(best, samples, numeric)
1454
+ if (performance_inputs and best_row["sample_valid"] and not performance_ok):
1455
+ rescue_request = (
1456
+ request + "\n\nSecond independent performance repair: " + _PERFORMANCE_REPAIR
1457
+ + "\n\nThe previous repair did not pass every executable largest-input "
1458
+ "probe. Preserve exact semantics; replace the algorithm when necessary."
1459
+ + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
1460
+ + "\n\nExecution-selected program to repair:\n" + best
1461
+ )
1462
+ else:
1463
+ rescue_request = (
1464
+ request + "\n\nEmergency independent derivation: " + _REPAIR
1465
+ + "\n\nStatement-sample acceptance protocol: " + _MUST_PASS
1466
+ + "\n\nThe deterministic selector found insufficient execution consensus. "
1467
+ "Do not vote between prior programs; derive and return one complete raw program."
1468
+ + "\n\nBest prior candidate diagnostics:\n"
1469
+ + _failure_note(passed, total, failure)
1470
+ + "\n\nBest prior candidate:\n" + best
1471
+ )
1472
+ rescued = finish(_source(ask(
1473
+ config["rescue"], rescue_request, config["rescue_max_tokens"],
1474
  config["rescue_reasoning"],
1475
+ ))) if room_for(config["rescue"], 1.0, 60.0) else ""
1476
  if rescued.strip():
1477
  candidates.append(rescued)
1478
+ rescue_deadline = min(
1479
+ epoch_deadline - 8.0,
1480
+ time.monotonic() + _CANDIDATE_EVIDENCE_BUDGET_S,
1481
+ )
1482
+ rescue_rows, _rescue_outputs = _candidate_evidence(
1483
+ [rescued], samples, generated, numeric, oracle_cases, config["critic"],
1484
+ large_input, performance_inputs, rescue_deadline,
1485
+ )
1486
+ if rescue_rows:
1487
+ rescue_rows[0]["index"] = len(candidates) - 1
1488
+ rows.extend(rescue_rows)
1489
+ return finish(_best_candidate(candidates, rows)[0])
1490
 
1491
  return agent
weights.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:c3c551943729277bbf380771379fe285212ffd1d198bad6177db2c60fec3885b
3
- size 1448
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:749020122e03f20a93c391fc92197188f0bf56df6e10cc63c6beb39b8e86147a
3
+ size 2183