7dc97a49ee
Keep representative-time cards off while method coverage is still open. New cases bind Skill 10.0.9; existing 10.0.8 packages stay hashed. Co-authored-by: Cursor <cursoragent@cursor.com>
123 lines
5.9 KiB
Python
123 lines
5.9 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from uuid import NAMESPACE_URL, uuid5
|
|
|
|
from scripts.rectification.candidate_feature_service import build_candidate_feature_snapshot
|
|
from scripts.rectification.contracts import EVENT_CONTRACT_VERSION, RectificationRequest
|
|
from scripts.rectification.decision_policy import (
|
|
EXECUTION_LEDGER_VERSION,
|
|
POLICY_VERSION,
|
|
build_candidate_decisions,
|
|
build_decision_receipt,
|
|
build_execution_ledger,
|
|
)
|
|
from scripts.rectification.diagnostics_service import run_diagnostics
|
|
from scripts.rectification.scoring_service import (
|
|
ALGORITHM_VERSION,
|
|
build_event_contribution_matrix,
|
|
calculation_spec,
|
|
score_from_matrix,
|
|
scoreable_request,
|
|
sha256,
|
|
)
|
|
|
|
|
|
def candidate_features(request: RectificationRequest) -> dict[str, Any]:
|
|
spec = calculation_spec(request)
|
|
spec_hash = sha256(spec)
|
|
scoring_request = scoreable_request(request)
|
|
return {
|
|
"algorithm_version": ALGORITHM_VERSION,
|
|
"event_contract_version": EVENT_CONTRACT_VERSION,
|
|
"decision_policy_version": POLICY_VERSION,
|
|
"calculation_spec": spec,
|
|
"calculation_spec_hash": spec_hash,
|
|
"candidate_feature_snapshot": build_candidate_feature_snapshot(scoring_request, spec_hash),
|
|
"can_confirm_exact_minute": False,
|
|
}
|
|
|
|
|
|
def score_candidates(request: RectificationRequest) -> dict[str, Any]:
|
|
scoring_request = scoreable_request(request)
|
|
built = build_event_contribution_matrix(scoring_request)
|
|
rows = score_from_matrix(scoring_request, built)
|
|
spec = calculation_spec(request)
|
|
spec_hash = sha256(spec)
|
|
diagnostic_values = run_diagnostics(scoring_request, rows, built)
|
|
fingerprint = sha256(request)
|
|
result_id = str(uuid5(NAMESPACE_URL, f"{ALGORITHM_VERSION}:{fingerprint}"))
|
|
candidate_decisions = build_candidate_decisions(rows, result_id=result_id)
|
|
decision_receipt = build_decision_receipt(request, candidate_decisions, built, diagnostic_values)
|
|
execution_ledger = build_execution_ledger(request, built, diagnostic_values, candidate_decisions)
|
|
representative = candidate_decisions[0] if candidate_decisions else None
|
|
return {
|
|
"result_id": result_id,
|
|
"algorithm_version": ALGORITHM_VERSION,
|
|
"event_contract_version": EVENT_CONTRACT_VERSION,
|
|
"decision_policy_version": POLICY_VERSION,
|
|
"calculation_spec": spec,
|
|
"calculation_spec_hash": spec_hash,
|
|
"candidate_scores": [{
|
|
"time": row["time"],
|
|
"score": row["score"],
|
|
"supporting_event_ids": [item["event_id"] for item in row["evidence"] if item["points"] > 0],
|
|
"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")
|
|
),
|
|
"diagnostics": diagnostic_values,
|
|
"robustness": {
|
|
"neighbor_support_minutes": diagnostic_values.get("neighbor_support_minutes", 0),
|
|
"leave_one_out_retention_rate": diagnostic_values.get("leave_one_event_out_retention_rate", 0),
|
|
"leave_one_domain_out_retention_rate": diagnostic_values.get("leave_one_domain_out_retention_rate", 0),
|
|
"date_sensitivity_retention_rate": diagnostic_values.get("date_sensitivity_retention_rate", 0),
|
|
},
|
|
"missing_layers": built["missing_layers"],
|
|
"display_allowed": decision_receipt["display_allowed"],
|
|
"selection_allowed": decision_receipt["selection_allowed"],
|
|
"acceptance_allowed": decision_receipt["acceptance_allowed"],
|
|
"propose_allowed": decision_receipt["propose_allowed"],
|
|
"confirmation_allowed": decision_receipt["confirmation_allowed"],
|
|
"representative_candidate_id": representative["candidate_id"] if representative else None,
|
|
"representative_time": representative["time"] if representative else None,
|
|
"overall_confidence": decision_receipt["overall_confidence"],
|
|
"margin_percent": decision_receipt["margin_percent"],
|
|
"can_confirm_exact_minute": decision_receipt["confirmation_allowed"],
|
|
}
|
|
|
|
|
|
def diagnostics(request: RectificationRequest) -> dict[str, Any]:
|
|
scored = score_candidates(request)
|
|
return {
|
|
"result_id": scored["result_id"],
|
|
"algorithm_version": scored["algorithm_version"],
|
|
"event_contract_version": scored["event_contract_version"],
|
|
"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"],
|
|
"diagnostics": scored["diagnostics"],
|
|
"missing_layers": scored["missing_layers"],
|
|
"display_allowed": scored["display_allowed"],
|
|
"selection_allowed": scored["selection_allowed"],
|
|
"acceptance_allowed": scored["acceptance_allowed"],
|
|
"propose_allowed": scored["propose_allowed"],
|
|
"confirmation_allowed": scored["confirmation_allowed"],
|
|
"representative_candidate_id": scored["representative_candidate_id"],
|
|
"representative_time": scored["representative_time"],
|
|
"overall_confidence": scored["overall_confidence"],
|
|
"margin_percent": scored["margin_percent"],
|
|
"can_confirm_exact_minute": scored["confirmation_allowed"],
|
|
}
|