Speed up rectification scoring without changing results.

Cache work that depends only on the candidate minute, drop unread response fields, and reject windows above 241 candidates before any chart is built. Eleven canonical scenario hashes match 77057a819. Sealed and reported-offset replays match the 2026-10-08 records (changed_trial_count 8 and 581) and are stored as scoring_perf_2026_10_13.

Co-Authored-By: Grok (agent loop)
This commit is contained in:
jesse-ux
2026-10-14 02:06:14 +08:00
co-authored by Grok (agent loop)
parent 74384be024
commit 8e0503f9c0
27 changed files with 45977 additions and 92 deletions
+60 -9
View File
@@ -4,7 +4,9 @@ from typing import Any, Mapping, Sequence
from uuid import NAMESPACE_URL, uuid5
from scripts.active_rectification_events import build_candidate_result_summary
from scripts.offline_compute_mixins import BadRequest
from scripts.rectification.candidate_feature_service import build_candidate_feature_snapshot
from scripts.rectification.candidate_window import enumerate_candidate_window
from scripts.rectification.contracts import (
EVENT_CONTRACT_VERSION,
RectificationRequest,
@@ -195,6 +197,35 @@ def _rectification_report(
}
MAX_CANDIDATES_PER_REQUEST = 241
class TooManyCandidates(BadRequest):
"""Rejected before any chart is computed.
The API server maps every BadRequest to HTTP 400 with error_code
ERR_BAD_REQUEST and this task must not edit that server. The token
too_many_candidates lives in the message, with the cap and the count.
"""
code = "too_many_candidates"
def __init__(self, count: int) -> None:
self.count = int(count)
self.limit = MAX_CANDIDATES_PER_REQUEST
super().__init__(f"too_many_candidates: limit={self.limit} count={self.count}")
def _candidate_count(request: RectificationRequest) -> int:
return len(enumerate_candidate_window(request))
def _reject_if_too_many_candidates(request: RectificationRequest) -> None:
count = _candidate_count(request)
if count > MAX_CANDIDATES_PER_REQUEST:
raise TooManyCandidates(count)
def engine_scoring_versions() -> dict[str, str]:
"""Identity fields also returned by `/api/rectification/v5/score`, without scoring."""
return {
@@ -219,7 +250,14 @@ def candidate_features(request: RectificationRequest) -> dict[str, Any]:
}
def score_candidates(request: RectificationRequest) -> dict[str, Any]:
def score_candidates(
request: RectificationRequest,
*,
include_internal: bool = False,
enforce_candidate_cap: bool = True,
) -> dict[str, Any]:
if enforce_candidate_cap:
_reject_if_too_many_candidates(request)
scoring_request = scoreable_request(request)
built = build_event_contribution_matrix(scoring_request)
rows = score_from_matrix(scoring_request, built)
@@ -272,7 +310,7 @@ def score_candidates(request: RectificationRequest) -> dict[str, Any]:
)
rectification_report["next_step_codes"] = candidate_summary["next_step_codes"]
candidate_summary["report"] = rectification_report
return {
payload = {
"result_id": result_id,
"algorithm_version": ALGORITHM_VERSION,
"event_contract_version": EVENT_CONTRACT_VERSION,
@@ -286,11 +324,9 @@ def score_candidates(request: RectificationRequest) -> dict[str, Any]:
"conflicting_event_ids": [item["event_id"] for item in row["evidence"] if item["points"] < 0],
} for row in rows],
"candidate_decisions": candidate_decisions,
"candidate_decision_receipt": decision_receipt,
"decision_receipt": decision_receipt,
"execution_ledger_version": EXECUTION_LEDGER_VERSION,
"execution_ledger": execution_ledger,
"event_contribution_matrix": built["matrix"],
"candidate_feature_snapshot": build_candidate_feature_snapshot(
scoring_request, spec_hash, built.get("static_contexts")
),
@@ -317,10 +353,20 @@ def score_candidates(request: RectificationRequest) -> dict[str, Any]:
"margin_percent": decision_receipt["margin_percent"],
"can_confirm_exact_minute": decision_receipt["confirmation_allowed"],
}
if include_internal:
payload["candidate_decision_receipt"] = decision_receipt
payload["event_contribution_matrix"] = built["matrix"]
return payload
def diagnostics(request: RectificationRequest) -> dict[str, Any]:
scored = score_candidates(request)
def diagnostics(
request: RectificationRequest,
*,
enforce_candidate_cap: bool = True,
) -> dict[str, Any]:
if enforce_candidate_cap:
_reject_if_too_many_candidates(request)
scored = score_candidates(request, enforce_candidate_cap=enforce_candidate_cap)
return {
"result_id": scored["result_id"],
"algorithm_version": scored["algorithm_version"],
@@ -328,7 +374,6 @@ def diagnostics(request: RectificationRequest) -> dict[str, Any]:
"decision_policy_version": scored["decision_policy_version"],
"calculation_spec_hash": scored["calculation_spec_hash"],
"candidate_decisions": scored["candidate_decisions"],
"candidate_decision_receipt": scored["candidate_decision_receipt"],
"decision_receipt": scored["decision_receipt"],
"execution_ledger_version": scored["execution_ledger_version"],
"execution_ledger": scored["execution_ledger"],
@@ -460,7 +505,11 @@ def _normalize_relative_support(raw: Sequence[float]) -> list[float]:
return shares
def block_scan(request: RectificationRequest) -> dict[str, Any]:
def block_scan(
request: RectificationRequest,
*,
enforce_candidate_cap: bool = True,
) -> dict[str, Any]:
"""Aggregate event scores into declared periods or caller-supplied sub-blocks."""
requested_step = request.get("minute_step")
if isinstance(requested_step, int) and requested_step > 1:
@@ -472,7 +521,9 @@ def block_scan(request: RectificationRequest) -> dict[str, Any]:
if step <= 1:
step = 10
scoring_request = {**request, "minute_step": step}
scored = score_candidates(scoring_request)
if enforce_candidate_cap:
_reject_if_too_many_candidates(scoring_request)
scored = score_candidates(scoring_request, enforce_candidate_cap=enforce_candidate_cap)
events_by_id = {
str(event.get("id") or ""): event
for event in request.get("events") or []