feat: integrate minute rectification safeguards

This commit is contained in:
Jesse_Chen
2026-07-22 09:58:32 +08:00
parent d5453681ad
commit d7d9703364
16 changed files with 639 additions and 36 deletions
+32 -4
View File
@@ -11,6 +11,14 @@ from datetime import datetime, timedelta
from pathlib import Path
from typing import Any
try:
from scripts.rectification_input_contract import (
candidate_input_fingerprint,
stability_probe_contract,
)
except ModuleNotFoundError: # pragma: no cover - direct script execution
from rectification_input_contract import candidate_input_fingerprint, stability_probe_contract
ROOT = Path(__file__).resolve().parents[1]
ENGINE = ROOT / "scripts" / "jyotish_engine.py"
@@ -60,6 +68,7 @@ def scan_candidate_times(payload: dict[str, Any], *, uncertainty_minutes: int =
rows.append({
"time": moment.strftime("%Y-%m-%d %H:%M"),
"offset_minutes": offset,
"input_fingerprint": candidate_input_fingerprint(point),
"d1_ascendant": asc.get("sign"),
"d1_degree_in_sign": asc.get("degree_in_sign"),
"divisional_ascendants": divisional,
@@ -68,14 +77,22 @@ def scan_candidate_times(payload: dict[str, Any], *, uncertainty_minutes: int =
unavailable_vargas = [varga.upper() for varga in _VARGAS if all(row["divisional_ascendants"][varga.upper()] is None for row in rows)]
supported_vargas = [varga.lower() for varga in _VARGAS if varga.upper() not in unavailable_vargas]
modal = Counter(signatures).most_common(1)[0][0]
for row, signature in zip(rows, signatures):
row["sensitivity_count"] = sum(left != right for left, right in zip(signature, modal))
for row, signature in zip(rows, signatures, strict=True):
row["sensitivity_count"] = sum(
left != right for left, right in zip(signature, modal, strict=True)
)
row["sensitive_layers"] = [
name for name, current, typical in zip(("D1", "D4", "D9", "D10", "D24", "D30"), signature, modal)
name
for name, current, typical in zip(
("D1", "D4", "D9", "D10", "D24", "D30"),
signature,
modal,
strict=True,
)
if current != typical
]
transitions = []
for previous, current in zip(rows, rows[1:]):
for previous, current in zip(rows, rows[1:], strict=False):
changed = [name for name in ("d1_ascendant", "divisional_ascendants") if previous[name] != current[name]]
if changed:
transitions.append({"between": [previous["time"], current["time"]], "changed": changed})
@@ -88,6 +105,17 @@ def scan_candidate_times(payload: dict[str, Any], *, uncertainty_minutes: int =
"uncertainty_minutes": uncertainty_minutes,
"step_minutes": step_minutes,
"rows": rows,
"input_contract": {
"version": "rectification-input-v1",
"center_input_fingerprint": candidate_input_fingerprint(payload),
"settings": {
"ayanamsa": str(payload.get("ayanamsa") or "lahiri").strip().lower(),
"node_mode": str(
payload.get("node_mode", payload.get("nodeMode", "mean"))
).strip().lower(),
},
},
"stability_contract": stability_probe_contract(payload),
"transitions": transitions,
"supported_vargas": [varga.upper() for varga in supported_vargas],
"unavailable_vargas": unavailable_vargas,
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""Append one independently reviewed case to a non-production v4 intake queue."""
from __future__ import annotations
import argparse
import json
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
try:
from scripts.minute_rectification_holdout_validator import case_errors
except ModuleNotFoundError: # pragma: no cover - direct script execution
from minute_rectification_holdout_validator import case_errors
INTAKE_SCHEMA_VERSION = "minute-rectification-holdout-v4-intake"
DEFAULT_INTAKE = (
Path(__file__).resolve().parents[1]
/ "references"
/ "real_case_calibration"
/ "minute_rectification_holdout_v4_intake.json"
)
def append_case(path: Path, case: dict[str, Any]) -> dict[str, Any]:
data = json.loads(path.read_text(encoding="utf-8"))
if data.get("schema_version") != INTAKE_SCHEMA_VERSION:
return {"appended": False, "errors": ["intake_schema_required"]}
gate = data.get("minimum_gate") if isinstance(data.get("minimum_gate"), dict) else {}
cases = data.get("cases") if isinstance(data.get("cases"), list) else []
case_id = str(case.get("case_id") or "").strip()
if not case_id:
return {"appended": False, "errors": ["missing_case_id"]}
if any(isinstance(existing, dict) and existing.get("case_id") == case_id for existing in cases):
return {"appended": False, "errors": ["duplicate_case_id"]}
errors = case_errors(case, gate, require_review_safeguards=True)
if errors:
return {"appended": False, "errors": errors}
cases.append({**case, "ingested_at": datetime.now(UTC).isoformat().replace("+00:00", "Z")})
data["cases"] = cases
data["status"] = "collecting_independently_reviewed_cases"
data["production_tuning_allowed"] = False
data["verified_minute_claim_allowed"] = False
path.write_text(json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8")
return {
"appended": True,
"errors": [],
"case_count": len(cases),
"verified_minute_claim_allowed": False,
}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("manifest", nargs="?", type=Path, default=DEFAULT_INTAKE)
parser.add_argument("--case-json", required=True)
args = parser.parse_args()
result = append_case(args.manifest, json.loads(args.case_json))
print(json.dumps(result, ensure_ascii=False, indent=2, sort_keys=True))
return 0 if result["appended"] else 1
if __name__ == "__main__":
raise SystemExit(main())
@@ -13,6 +13,7 @@ DEFAULT_MANIFEST = ROOT / "references" / "real_case_calibration" / "minute_recti
SUPPORTED_SCHEMA_VERSIONS = {
"minute-rectification-holdout-v2",
"minute-rectification-holdout-v3",
"minute-rectification-holdout-v4",
}
ALLOWED_DOMAINS = {
"education", "relocation", "relationship", "career", "finance", "health_pressure",
@@ -142,6 +143,71 @@ def _case_errors(case: Any, gate: dict[str, Any]) -> list[str]:
return errors
def _review_safeguard_errors(case: Any, gate: dict[str, Any]) -> list[str]:
"""Validate safeguards required for newly admitted v4 holdout cases."""
if not isinstance(case, dict):
return ["case_must_be_object"]
errors: list[str] = []
if not isinstance(case.get("adjudicator"), str) or not case.get("adjudicator", "").strip():
errors.append("missing_independent_adjudicator")
if case.get("independent_human_reviewed") is not True:
errors.append("independent_review_not_attested")
if case.get("frozen_before_scoring") is not True:
errors.append("case_not_frozen_before_scoring")
events = case.get("events") if isinstance(case.get("events"), list) else []
day_precision_count = sum(
isinstance(event, dict)
and event.get("precision") == "day"
and _parse_event_date(event.get("date"), event.get("precision")) is not None
for event in events
)
if day_precision_count < int(gate.get("day_precision_events_per_case", 3)):
errors.append("insufficient_day_precision_events")
offsets = case.get("false_minute_offsets") if isinstance(case.get("false_minute_offsets"), list) else []
commitments = (
case.get("false_minute_commitments")
if isinstance(case.get("false_minute_commitments"), list)
else []
)
committed_offsets: list[int] = []
hashes: list[str] = []
for item in commitments:
if not isinstance(item, dict):
errors.append("invalid_false_minute_commitment")
continue
offset = item.get("offset_minutes")
commitment_hash = item.get("commitment_hash")
if not isinstance(offset, int) or offset == 0:
errors.append("invalid_false_minute_commitment_offset")
else:
committed_offsets.append(offset)
if (
not isinstance(commitment_hash, str)
or len(commitment_hash) != 64
or any(character not in "0123456789abcdef" for character in commitment_hash.lower())
):
errors.append("invalid_false_minute_commitment_hash")
else:
hashes.append(commitment_hash.lower())
if any(key in item for key in ("candidate_minute", "published_minute", "birth_time")):
errors.append("false_minute_commitment_leaks_time")
if sorted(committed_offsets) != sorted(offsets):
errors.append("false_minute_commitments_do_not_match_offsets")
if len(hashes) != len(set(hashes)):
errors.append("duplicate_false_minute_commitment_hash")
return errors
def case_errors(case: Any, gate: dict[str, Any], *, require_review_safeguards: bool = False) -> list[str]:
"""Public case-level validator shared by frozen manifests and intake tooling."""
errors = _case_errors(case, gate)
if require_review_safeguards:
errors.extend(_review_safeguard_errors(case, gate))
return sorted(set(errors))
def validate(manifest_path: Path = DEFAULT_MANIFEST) -> dict[str, Any]:
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
gate = manifest.get("minimum_gate") if isinstance(manifest.get("minimum_gate"), dict) else {}
@@ -154,7 +220,10 @@ def validate(manifest_path: Path = DEFAULT_MANIFEST) -> dict[str, Any]:
if manifest.get("frozen_before_replay") is not True:
manifest_errors.append("benchmark_not_frozen_before_replay")
if (
manifest.get("schema_version") == "minute-rectification-holdout-v3"
manifest.get("schema_version") in {
"minute-rectification-holdout-v3",
"minute-rectification-holdout-v4",
}
and manifest.get("source_audit_status") != "passed_before_freeze"
):
manifest_errors.append("source_content_audit_not_passed_before_freeze")
@@ -165,8 +234,9 @@ def validate(manifest_path: Path = DEFAULT_MANIFEST) -> dict[str, Any]:
seen_ids: set[str] = set()
invalid_details: list[dict[str, Any]] = []
valid_cases = 0
require_review_safeguards = manifest.get("schema_version") == "minute-rectification-holdout-v4"
for case in cases:
errors = _case_errors(case, gate)
errors = case_errors(case, gate, require_review_safeguards=require_review_safeguards)
case_id = case.get("case_id") if isinstance(case, dict) else "non_object_case"
if isinstance(case_id, str) and case_id in seen_ids:
errors.append("duplicate_case_id")
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""Audit reusable public AA cases before admitting them to a minute holdout."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_SOURCES = (
ROOT / "references" / "real_case_calibration" / "replay_manifest.json",
ROOT / "references" / "real_case_calibration" / "replay_manifest_holdout_v2.json",
ROOT / "references" / "real_case_calibration" / "replay_manifest_probe3_v2.json",
ROOT / "references" / "real_case_calibration" / "public_context_manifest.json",
)
def _events(case: dict[str, Any]) -> list[dict[str, Any]]:
raw = case.get("events") if isinstance(case.get("events"), list) else case.get("event_outcomes")
return [event for event in raw or [] if isinstance(event, dict)]
def _birth_source(subject: dict[str, Any]) -> dict[str, Any]:
if isinstance(subject.get("birth_source"), dict):
return subject["birth_source"]
birth = subject.get("birth") if isinstance(subject.get("birth"), dict) else {}
return birth.get("source") if isinstance(birth.get("source"), dict) else {}
def _is_aa(source: dict[str, Any]) -> bool:
return source.get("time_accuracy_rating") == "AA" or source.get("rodden_rating") == "AA"
def build_source_audit(paths: list[Path] | tuple[Path, ...] = DEFAULT_SOURCES) -> dict[str, Any]:
entries: dict[str, dict[str, Any]] = {}
for path in paths:
data = json.loads(path.read_text(encoding="utf-8"))
for case in data.get("cases", []):
if not isinstance(case, dict):
continue
subject = case.get("subject") if isinstance(case.get("subject"), dict) else case
source = _birth_source(subject)
source_url = str(source.get("url") or "")
if not _is_aa(source) or not source_url:
continue
label = str(subject.get("name") or subject.get("subject_label") or case.get("case_id") or "unnamed")
entry = entries.setdefault(source_url, {
"subject": label,
"birth_source_url": source_url,
"case_ids": [],
"day_precision_event_dates": set(),
})
entry["case_ids"].append(str(case.get("case_id") or label))
for event in _events(case):
event_date = str(event.get("event_date") or event.get("date") or "")
if len(event_date) == 10:
entry["day_precision_event_dates"].add(event_date)
cases = []
for entry in entries.values():
event_count = len(entry["day_precision_event_dates"])
cases.append({
"subject": entry["subject"],
"birth_source_url": entry["birth_source_url"],
"case_ids": sorted(set(entry["case_ids"])),
"existing_day_precision_event_count": event_count,
"additional_day_precision_events_required": max(0, 3 - event_count),
"review_and_commitment_controls_required": True,
})
cases.sort(key=lambda case: (case["additional_day_precision_events_required"], case["subject"]))
return {
"scope": "minute_rectification_public_aa_source_audit",
"public_aa_case_count": len(cases),
"minimum_public_aa_cases": 20,
"additional_public_aa_cases_required": max(0, 20 - len(cases)),
"cases": cases,
"production_tuning_allowed": False,
"boundary": (
"Source discovery is not holdout validation. Every promoted case still requires "
"independent review, day-precision events and committed false-minute controls."
),
}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("sources", nargs="*", type=Path, default=list(DEFAULT_SOURCES))
parser.add_argument("--output", type=Path)
args = parser.parse_args()
result = build_source_audit(args.sources)
text = json.dumps(result, ensure_ascii=False, indent=2, sort_keys=True)
if args.output:
args.output.write_text(text + "\n", encoding="utf-8")
print(text)
return 0
if __name__ == "__main__":
raise SystemExit(main())
+113
View File
@@ -0,0 +1,113 @@
"""Stable, privacy-safe identities for birth-time rectification evidence."""
from __future__ import annotations
import hashlib
import json
from datetime import datetime, timedelta
from typing import Any
INPUT_CONTRACT_VERSION = "rectification-input-v1"
REQUIRED_FIELDS = ("year", "month", "day", "hour", "minute", "lat", "lon", "tz")
STABILITY_OFFSETS = (-5, -2, -1, 1, 2, 5)
def _canonical_json(value: Any) -> str:
return json.dumps(value, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
def canonical_birth_input(case: dict[str, Any]) -> dict[str, Any]:
"""Normalize only calculation-bearing fields using deployed defaults."""
missing = [field for field in REQUIRED_FIELDS if case.get(field) is None]
if missing:
raise ValueError(f"missing rectification input fields: {', '.join(missing)}")
year, month, day = int(case["year"]), int(case["month"]), int(case["day"])
hour, minute, second = int(case["hour"]), int(case["minute"]), int(case.get("second", 0))
datetime(year, month, day, hour, minute, second)
node_mode = str(case.get("node_mode", case.get("nodeMode", "mean"))).strip().lower()
if node_mode not in {"mean", "true"}:
raise ValueError("node_mode must be mean or true")
return {
"year": year,
"month": month,
"day": day,
"hour": hour,
"minute": minute,
"second": second,
"lat": float(case["lat"]),
"lon": float(case["lon"]),
"tz": float(case["tz"]),
"ayanamsa": str(case.get("ayanamsa", "lahiri")).strip().lower(),
"node_mode": node_mode,
}
def candidate_input_fingerprint(case: dict[str, Any]) -> str:
payload = {
"schema_version": INPUT_CONTRACT_VERSION,
"calculation_input": canonical_birth_input(case),
}
return hashlib.sha256(_canonical_json(payload).encode("utf-8")).hexdigest()
def stability_probe_contract(case: dict[str, Any]) -> dict[str, Any]:
"""Materialize adjacent-minute identities without claiming that they passed."""
baseline = canonical_birth_input(case)
center = datetime(
baseline["year"],
baseline["month"],
baseline["day"],
baseline["hour"],
baseline["minute"],
baseline["second"],
)
probes = []
for offset in STABILITY_OFFSETS:
moment = center + timedelta(minutes=offset)
probe = {
**baseline,
"year": moment.year,
"month": moment.month,
"day": moment.day,
"hour": moment.hour,
"minute": moment.minute,
"second": moment.second,
}
probes.append({
"offset_minutes": offset,
"input_fingerprint": candidate_input_fingerprint(probe),
})
return {
"scope": "candidate_minute_stability_contract",
"status": "pending_score_comparison",
"baseline_input_fingerprint": candidate_input_fingerprint(baseline),
"probes": probes,
"minute_confirmation_allowed": False,
"blocker": "public_blind_minute_holdout_not_closed",
"boundary": (
"Probe identities are reproducible inputs, not evidence that a minute passed "
"stability or outcome validation."
),
}
def _semantic_normalize(value: Any, *, parent_key: str | None = None) -> Any:
if isinstance(value, dict):
return {
key: _semantic_normalize(item, parent_key=key)
for key, item in sorted(value.items())
}
if isinstance(value, list):
normalized = [_semantic_normalize(item, parent_key=parent_key) for item in value]
if parent_key in {"gives", "receives"}:
return sorted(normalized, key=_canonical_json)
return normalized
return value
def semantic_evidence_hash(value: Any) -> str:
"""Hash known order-insensitive evidence while raw artifact hashes remain intact."""
normalized = _semantic_normalize(value)
return hashlib.sha256(_canonical_json(normalized).encode("utf-8")).hexdigest()
+17 -21
View File
@@ -1,9 +1,7 @@
"""Build a privacy-safe, request-level three-engine rectification parity packet."""
from __future__ import annotations
import hashlib
import importlib
import json
import sys
from datetime import datetime
from pathlib import Path
@@ -11,6 +9,19 @@ from typing import Any
from domain_calculation_service import compute_chart
try:
from scripts.rectification_input_contract import (
candidate_input_fingerprint,
canonical_birth_input,
stability_probe_contract,
)
except ModuleNotFoundError: # pragma: no cover - direct script execution
from rectification_input_contract import (
candidate_input_fingerprint,
canonical_birth_input,
stability_probe_contract,
)
ROOT = Path(__file__).resolve().parents[1]
JYOTISHGANIT_ROOT = ROOT / "references" / "open_source_sources" / "jyotishganit"
PLANETS = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn")
@@ -19,29 +30,12 @@ SIGNS = ("Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpi
def canonical_case_input(case: dict[str, Any]) -> dict[str, Any]:
"""Normalize only calculation-bearing fields before hashing or engine dispatch."""
required = ("year", "month", "day", "hour", "minute", "lat", "lon", "tz")
missing = [key for key in required if key not in case]
if missing:
raise ValueError("case is missing required birth fields")
return {
"year": int(case["year"]),
"month": int(case["month"]),
"day": int(case["day"]),
"hour": int(case["hour"]),
"minute": int(case["minute"]),
"second": int(case.get("second", 0)),
"lat": float(case["lat"]),
"lon": float(case["lon"]),
"tz": float(case["tz"]),
"ayanamsa": str(case.get("ayanamsa", "lahiri")).strip().lower(),
"node_mode": str(case.get("node_mode", case.get("nodeMode", "mean"))).strip().lower(),
}
return canonical_birth_input(case)
def case_hash(case: dict[str, Any]) -> str:
"""Stable identity for evidence correlation; never exposes birth data."""
payload = json.dumps(canonical_case_input(case), sort_keys=True, ensure_ascii=True, separators=(",", ":"))
return hashlib.sha256(payload.encode()).hexdigest()
return candidate_input_fingerprint(case)
def _local_d1(case: dict[str, Any]) -> dict[str, str]:
@@ -140,6 +134,8 @@ def build_packet(
return {
"scope": "request_level_three_engine_d1_parity",
"case_hash": case_hash(case),
"input_contract_hash": candidate_input_fingerprint(case),
"stability_contract": stability_probe_contract(case),
"engine_status": engine_status,
"match_count": sum(row["status"] == "match" for row in rows),
"mismatch_count": sum(row["status"] == "mismatch" for row in rows),
+7 -6
View File
@@ -14,9 +14,10 @@ ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from benchmarks.jyotish.scripts.run_pyjhora_compare import build_pyjhora_sample
from benchmarks.jyotish.scripts.run_skill_baseline import run_sample
from scripts.three_engine_parity_runner import _capture_jyotishganit_raw
from benchmarks.jyotish.scripts.run_pyjhora_compare import build_pyjhora_sample # noqa: E402
from benchmarks.jyotish.scripts.run_skill_baseline import run_sample # noqa: E402
from scripts.rectification_input_contract import semantic_evidence_hash # noqa: E402
from scripts.three_engine_parity_runner import _capture_jyotishganit_raw # noqa: E402
ORACLE = ROOT / "references" / "oracle"
ARTIFACTS = ORACLE / "artifacts"
@@ -146,9 +147,9 @@ def build() -> dict[str, Any]:
manifest = {
"case_id": "steve_jobs_public_1955_lahiri", "birth_data_policy": "public_case_only", "blocked_reason": "none",
"engines": {
"VedAstro": {"status": "official_verified", "official_raw_response_path": "artifacts/" + ved_path.name, "artifact_hash": _sha(ved_path), "settings": ved["settings"]},
"PyJHora_JHora": {"status": "imported", "raw_output_path": "artifacts/" + py_path.name, "artifact_hash": _sha(py_path), "settings": pyjhora["settings"]},
"jyotishganit": {"status": "imported", "raw_output_path": "artifacts/" + jy_path.name, "artifact_hash": _sha(jy_path), "settings": {"ayanamsa": jyotish["ayanamsa"]}},
"VedAstro": {"status": "official_verified", "official_raw_response_path": "artifacts/" + ved_path.name, "artifact_hash": _sha(ved_path), "semantic_hash": semantic_evidence_hash(ved), "settings": ved["settings"]},
"PyJHora_JHora": {"status": "imported", "raw_output_path": "artifacts/" + py_path.name, "artifact_hash": _sha(py_path), "semantic_hash": semantic_evidence_hash(pyjhora), "settings": pyjhora["settings"]},
"jyotishganit": {"status": "imported", "raw_output_path": "artifacts/" + jy_path.name, "artifact_hash": _sha(jy_path), "semantic_hash": semantic_evidence_hash(jyotish), "settings": {"ayanamsa": jyotish["ayanamsa"]}},
},
"comparison_rows": rows,
"method_arbitration": {