2026-09-03 22:36:02 +02:00
|
|
|
|
"""Candidate selection and safety-gated regression execution."""
|
2026-09-03 22:12:45 +02:00
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
2026-09-03 22:36:02 +02:00
|
|
|
|
from collections.abc import Mapping, Sequence
|
2026-09-03 22:48:10 +02:00
|
|
|
|
from pathlib import Path
|
2026-09-03 22:12:45 +02:00
|
|
|
|
|
2026-09-03 22:36:02 +02:00
|
|
|
|
from ..complexity.ledger import hotspot_key, select_forced_candidates
|
|
|
|
|
|
from ..complexity.map import Hotspot
|
2026-09-03 22:48:10 +02:00
|
|
|
|
from ..config import SandboxConfig
|
2026-09-03 22:36:02 +02:00
|
|
|
|
from ..findings import StaticFinding
|
2026-09-03 22:12:45 +02:00
|
|
|
|
from ..sandbox import SandboxJob, SandboxResult, SandboxRunner
|
2026-09-03 22:48:10 +02:00
|
|
|
|
from .recipes import recipe_for
|
2026-09-03 22:12:45 +02:00
|
|
|
|
from .safety import UnsafeSnippetError, check_snippet_safety
|
2026-09-03 22:59:01 +02:00
|
|
|
|
from .verdict import (
|
|
|
|
|
|
INCONCLUSIVE,
|
|
|
|
|
|
MISSING_RECIPE,
|
|
|
|
|
|
NOT_TRIGGERABLE,
|
|
|
|
|
|
SAFETY_REJECTED,
|
|
|
|
|
|
TRIGGERABLE,
|
|
|
|
|
|
RegressionVerdict,
|
|
|
|
|
|
verdict_from_result,
|
|
|
|
|
|
)
|
2026-09-03 22:12:45 +02:00
|
|
|
|
|
2026-09-03 22:59:01 +02:00
|
|
|
|
__all__ = [
|
|
|
|
|
|
"INCONCLUSIVE",
|
|
|
|
|
|
"MISSING_RECIPE",
|
|
|
|
|
|
"NOT_TRIGGERABLE",
|
|
|
|
|
|
"SAFETY_REJECTED",
|
|
|
|
|
|
"TRIGGERABLE",
|
|
|
|
|
|
"RegressionRunner",
|
|
|
|
|
|
"RegressionVerdict",
|
|
|
|
|
|
"select_candidates",
|
|
|
|
|
|
]
|
2026-09-03 22:12:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-03 22:36:02 +02:00
|
|
|
|
def select_candidates(
|
|
|
|
|
|
hotspots: Sequence[Hotspot],
|
|
|
|
|
|
findings: Sequence[StaticFinding],
|
|
|
|
|
|
*,
|
|
|
|
|
|
max_candidates: int,
|
|
|
|
|
|
policy_severity_by_id: Mapping[str, object] | None = None,
|
|
|
|
|
|
rule_cwe: Mapping[str, str] | None = None,
|
|
|
|
|
|
) -> tuple[Hotspot, ...]:
|
|
|
|
|
|
"""Select min(max_candidates, ranked hotspots) ∪ sev-5 reachable inventory.
|
|
|
|
|
|
|
|
|
|
|
|
The cap applies to the ranked hotspot slice. Forced policy-severity-5
|
|
|
|
|
|
reachable (or inferred-file-surface) inventory stays in the set even when a
|
|
|
|
|
|
ledger overlay demoted its hotspot out of that slice.
|
|
|
|
|
|
"""
|
|
|
|
|
|
ranked = tuple(sorted(hotspots, key=lambda item: (-item.score, item.path, item.function_name or "")))
|
|
|
|
|
|
selected = list(ranked[: max(max_candidates, 0)])
|
|
|
|
|
|
seen = {hotspot_key(item.path, item.function_name) for item in selected}
|
|
|
|
|
|
extras: list[Hotspot] = []
|
|
|
|
|
|
for finding in select_forced_candidates(findings, policy_severity_by_id or {}, rule_cwe=rule_cwe):
|
|
|
|
|
|
hotspot = _hotspot_for_finding(finding, ranked) or _hotspot_from_forced_finding(finding)
|
|
|
|
|
|
key = hotspot_key(hotspot.path, hotspot.function_name)
|
|
|
|
|
|
if key in seen:
|
|
|
|
|
|
continue
|
|
|
|
|
|
seen.add(key)
|
|
|
|
|
|
extras.append(hotspot)
|
|
|
|
|
|
return tuple(selected + extras)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _hotspot_for_finding(finding: StaticFinding, hotspots: Sequence[Hotspot]) -> Hotspot | None:
|
|
|
|
|
|
for hotspot in hotspots:
|
|
|
|
|
|
if finding.finding_id in hotspot.inventory_finding_ids:
|
|
|
|
|
|
return hotspot
|
|
|
|
|
|
key = hotspot_key(finding.path, finding.function_name)
|
|
|
|
|
|
for hotspot in hotspots:
|
|
|
|
|
|
if hotspot_key(hotspot.path, hotspot.function_name) == key:
|
|
|
|
|
|
return hotspot
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _hotspot_from_forced_finding(finding: StaticFinding) -> Hotspot:
|
|
|
|
|
|
return Hotspot(
|
|
|
|
|
|
path=finding.path,
|
|
|
|
|
|
function_name=finding.function_name,
|
|
|
|
|
|
score=0.0,
|
|
|
|
|
|
band="low",
|
|
|
|
|
|
signals={},
|
|
|
|
|
|
rationale="forced reachable severity-5 inventory",
|
|
|
|
|
|
test_hint=None,
|
|
|
|
|
|
inventory_finding_ids=(finding.finding_id,),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-03 22:12:45 +02:00
|
|
|
|
class RegressionRunner:
|
|
|
|
|
|
"""Run a snippet in the sandbox only after the structural safety check passes."""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, sandbox: SandboxRunner) -> None:
|
|
|
|
|
|
self._sandbox = sandbox
|
|
|
|
|
|
|
2026-09-03 22:48:10 +02:00
|
|
|
|
def run_recipe(
|
|
|
|
|
|
self,
|
|
|
|
|
|
language: str,
|
|
|
|
|
|
snippet: str,
|
|
|
|
|
|
image: str,
|
|
|
|
|
|
sandbox_limits: SandboxConfig,
|
|
|
|
|
|
*,
|
|
|
|
|
|
repo_root: Path,
|
|
|
|
|
|
) -> RegressionVerdict:
|
|
|
|
|
|
job = recipe_for(language, snippet, image, sandbox_limits, repo_root=repo_root)
|
|
|
|
|
|
if job is None:
|
2026-09-03 22:59:01 +02:00
|
|
|
|
return verdict_from_result(recipe_missing=True)
|
2026-09-03 22:48:10 +02:00
|
|
|
|
return self.run_snippet(snippet, job)
|
|
|
|
|
|
|
|
|
|
|
|
def run_snippet(self, snippet: str, job: SandboxJob | None) -> RegressionVerdict:
|
|
|
|
|
|
if job is None:
|
2026-09-03 22:59:01 +02:00
|
|
|
|
return verdict_from_result(recipe_missing=True)
|
2026-09-03 22:12:45 +02:00
|
|
|
|
try:
|
|
|
|
|
|
check_snippet_safety(snippet)
|
|
|
|
|
|
except UnsafeSnippetError:
|
2026-09-03 22:59:01 +02:00
|
|
|
|
return verdict_from_result(safety_rejected=True)
|
2026-09-03 22:12:45 +02:00
|
|
|
|
return _verdict_from_result(self._sandbox.run(job))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _verdict_from_result(result: SandboxResult) -> RegressionVerdict:
|
2026-09-03 22:59:01 +02:00
|
|
|
|
return verdict_from_result(result)
|