112 lines
5.6 KiB
Python
112 lines
5.6 KiB
Python
"""Global profile comparison followed by full-source family verification."""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
|
|
from suite_contract import Problem, encoded, validate_partition
|
|
from suite_policy import BASE_NAME_LIMIT, invocation, validate_natural
|
|
from suite_profiles import batches, profile_call, profile_source, size_metadata, validate_profiles
|
|
|
|
STRATEGY_REVISION = "suite-adaptive-selection-v1-20260930"
|
|
MAX_SOURCE_BATCHES = 16
|
|
MAX_LOGICAL_CALLS = 64
|
|
FALLBACK_ERRORS = {"pass_generation_retries_exhausted", "pass_turn_limit_exhausted",
|
|
"pass_output_limit", "pass_context_limit", "pass_capacity", "pass_request_too_large",
|
|
"pass_provider_transient_failure", "pass_timeout"}
|
|
|
|
|
|
def selection(source):
|
|
"""Use content-free deterministic risk thresholds before any paid inference."""
|
|
sizes = size_metadata(source)
|
|
reasons = []
|
|
if len(source["cases"]) >= 250:
|
|
reasons.append("case_count_at_least_250")
|
|
if sizes["source_bytes"] >= 256 * 1024:
|
|
reasons.append("source_bytes_at_least_262144")
|
|
if any(sizes[f]["p95"] >= 8192 for f in ("description", "preconditions", "success_criteria")):
|
|
reasons.append("p95_field_bytes_at_least_8192")
|
|
return {"execution_mode": "hierarchical" if reasons else "direct", "strategy_reasons": reasons,
|
|
"strategy_revision": STRATEGY_REVISION, "source_size_metadata": sizes}
|
|
|
|
|
|
def minimal_partition(value):
|
|
"""Keep exact memberships; the complete profiles supply mechanism evidence."""
|
|
return {"groups": [{"members": g["members"]} for g in value["groups"]]}
|
|
|
|
|
|
def source_batches(source, partition):
|
|
"""Reopen intact candidate families, keeping all member source fields."""
|
|
by_alias = {c["alias"]: c for c in source["cases"]}
|
|
packed, current = [], []
|
|
for group in sorted(partition["groups"], key=lambda g: sorted(g["members"])):
|
|
candidate = current + [group]
|
|
records = [by_alias[a] for g in candidate for a in g["members"]]
|
|
if current and (len(records) > 80 or len(encoded(records)) > 96 * 1024):
|
|
packed.append(current)
|
|
current = []
|
|
current.append(group)
|
|
if current:
|
|
packed.append(current)
|
|
if len(packed) > MAX_SOURCE_BATCHES:
|
|
raise Problem("hierarchical_source_capacity", 422, batch_count=len(packed))
|
|
result = []
|
|
for groups in packed:
|
|
aliases = {a for g in groups for a in g["members"]}
|
|
subset = {**source, "cases": [c for c in source["cases"] if c["alias"] in aliases]}
|
|
result.append((subset, {"candidate_partition": {"groups": groups}}))
|
|
return result
|
|
|
|
|
|
def validate_source(value, source, context):
|
|
"""Require original evidence and forbid accidental cross-candidate review drift."""
|
|
validate_natural(value, source)
|
|
originals = [set(g["members"]) for g in context["candidate_partition"]["groups"]]
|
|
if any(sum(set(g["members"]) <= members for members in originals) != 1 for g in value["groups"]):
|
|
raise Problem("source_review_crossed_family_boundary", 502)
|
|
|
|
|
|
def execute(workflow, source, capacity):
|
|
"""Independent global perspectives never inherit profile-processing boundaries."""
|
|
workflow.execution_mode = "hierarchical"
|
|
workflow.max_calls = MAX_LOGICAL_CALLS
|
|
pieces = batches(source)
|
|
workflow.profile_batch_count = len(pieces)
|
|
calls = [profile_call(piece, i) for i, piece in enumerate(pieces, 1)]
|
|
for piece, call in zip(pieces, calls):
|
|
capacity(call, workflow.provider, len(piece["cases"]))
|
|
profiles = {}
|
|
source_floor = 60 * max(1, math.ceil(len(source["cases"])/80),
|
|
math.ceil(len(encoded(source["cases"]))/98304))
|
|
for i, (piece, call) in enumerate(zip(pieces, calls)):
|
|
workflow.future_seconds = (len(pieces)-i-1)*30 + 5*workflow.global_floor + source_floor
|
|
value = workflow.call("implementation_profiles", piece, custom_call=call,
|
|
validator=lambda v, p=piece: validate_profiles(v, p))
|
|
profiles.update(value["profiles"])
|
|
compact = profile_source(source, profiles)
|
|
# Each proposal sees the entire profile set in an independent order. No
|
|
# processing batch partition or prior proposal is supplied to proposal B.
|
|
workflow.future_seconds = 4*workflow.global_floor + source_floor
|
|
a = workflow.call("profile_proposal_a", compact)
|
|
from suite_multipass import ordered_request
|
|
workflow.future_seconds = 3*workflow.global_floor + source_floor
|
|
b = workflow.call("profile_proposal_b", ordered_request(compact, True))
|
|
workflow.future_seconds = 2*workflow.global_floor + source_floor
|
|
provisional = workflow.call("profile_reconciliation", compact,
|
|
{"proposal_a": minimal_partition(a), "proposal_b": minimal_partition(b)})
|
|
workflow.cross_batch_review_count += 1
|
|
checks = source_batches(source, provisional)
|
|
# Every full-source verification is admitted before any such call launches.
|
|
for piece, context in checks:
|
|
capacity(invocation("source_review", piece, context), workflow.provider,
|
|
len(piece["cases"]), len(context["candidate_partition"]["groups"]))
|
|
confirmed = {"groups": []}
|
|
for i, (piece, context) in enumerate(checks):
|
|
workflow.future_seconds = 2*workflow.global_floor + (len(checks)-i-1)*60
|
|
value = workflow.call("source_review", piece, context,
|
|
len(context["candidate_partition"]["groups"]),
|
|
validator=lambda v, p=piece, c=context: validate_source(v, p, c))
|
|
confirmed["groups"].extend(value["groups"])
|
|
validate_natural(confirmed, source)
|
|
workflow.source_review_batch_count = len(checks)
|
|
return a, b, confirmed
|