fix(rectification): use candidate dates for cross-midnight dasha scoring

Add date-isolated caches and regression coverage, align scoring identity, and freeze full research reruns while preserving historical artifacts. Record unresolved cache/receipt identity and end-to-end acceptance gaps for branch review only.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
jesse-ux
2026-09-20 13:56:11 +08:00
co-authored by Claude Code
parent 03cba4780a
commit aa46da1016
49 changed files with 62080 additions and 109 deletions
+42 -8
View File
@@ -68,25 +68,33 @@ def test_shifted_window_preserves_dates_across_midnight(clock, offset):
assert metrics["delivery_width_minutes"] == 31
def test_cross_midnight_real_engine_scores_match_candidate_date_replay():
def test_cross_midnight_real_engine_scores_match_candidate_date_replay(monkeypatch):
case = json.loads(DATASET.read_text(encoding="utf-8"))["cases"][5]
request, candidates = sweep.shifted_window(case, 0, 60)
assert len({candidate.date() for candidate in candidates}) == 2
contexts = sweep.compute_candidate_static_contexts(request, candidates=candidates)
grouped = sweep.score_window(request, contexts)
calls = []
build = sweep.build_event_contribution_matrix
def traced(request, **kwargs):
calls.append(len(kwargs["static_contexts"]))
return build(request, **kwargs)
with monkeypatch.context() as patch:
patch.setattr(sweep, "build_event_contribution_matrix", traced)
native = sweep.score_window(request, contexts)
assert calls == [len(contexts)]
expected = []
for context in contexts:
dated = {**request, "birth_date": context["candidate_at"].date().isoformat()}
built = sweep.build_event_contribution_matrix(dated, static_contexts=[context])
expected.extend(sweep.score_from_matrix(dated, built))
assert grouped == expected
old_matrix = sweep.build_event_contribution_matrix(request, static_contexts=contexts)
old_rows = sweep.score_from_matrix(request, old_matrix)
assert old_rows != expected
assert native == expected
native_matrix = sweep.build_event_contribution_matrix(request, static_contexts=contexts)
native_rows = sweep.score_from_matrix(request, native_matrix)
assert native_rows == expected
def test_recorded_specification_and_all_prespecified_cells():
report = json.loads((sweep.ROOT / "docs/research/reported_offset_2026_09_20.json").read_text(encoding="utf-8"))
report = json.loads(sweep.REPORT.read_text(encoding="utf-8"))
spec = report["specification"]
assert spec["ayanamsa"] == "raman"
assert spec["node_mode"] == "mean"
@@ -98,7 +106,17 @@ def test_recorded_specification_and_all_prespecified_cells():
assert spec["evaluator_sha256"] == sweep.file_sha256(sweep.ROOT / "scripts/research/reported_offset_sweep.py")
assert spec["production_scoring_sha256"] == sweep.implementation_sha256(spec["production_scoring_files"])
assert spec["research_implementation_sha256"] == sweep.implementation_sha256(spec["research_files"])
assert spec["replay_revision"] == "candidate_date_grouped_v2"
assert spec["replay_revision"] == "native_candidate_date_v3"
frozen = json.loads(sweep.FREEZE.read_text(encoding="utf-8"))
assert report["frozen_record"] == spec == frozen
sweep.verify_frozen_record(frozen, sweep.freeze_record())
assert report["implementation_hash_matches_at_replay"] is True
assert report["dataset_hash_matches_at_replay"] is True
assert frozen["frozen_at_utc"] <= report["replay_started_at_utc"] <= report["replay_finished_at_utc"]
assert spec["official_valid_independent_blind"] is False
assert spec["official_blind_trial_count"] == 0
assert spec["results_previously_seen"] is True
assert spec["must_not_use_for_tuning"] is True
assert spec["truth_hidden_from_ranker"] is True
assert spec["is_blind_evaluation"] is False
assert report["trial_count"] == 20 * len(sweep.RADII) * len(sweep.OFFSETS)
@@ -115,3 +133,19 @@ def test_recorded_specification_and_all_prespecified_cells():
assert row["truth_in_window_rate"] == expected
assert row["delivery_coverage_rate"] <= expected
assert row["top_1_rate"] <= expected
assert report["historical_comparison"] == sweep.historical_comparison(
sweep.LEGACY_REPORT, report["trials"], ("case_ordinal", "radius_minutes", "offset_minutes"),
)
@pytest.mark.parametrize("key", ["dataset_sha256", "production_scoring_sha256", "research_implementation_sha256", "evaluator_sha256"])
def test_sweep_changed_frozen_identity_fails_before_scoring(tmp_path, monkeypatch, key):
frozen = sweep.freeze_record()
frozen[key] = "0" * 64
path = tmp_path / "bad-freeze.json"
path.write_text(json.dumps(frozen), encoding="utf-8")
def unexpected(*args, **kwargs):
raise AssertionError("must reject identity before scoring")
monkeypatch.setattr(sweep, "compute_candidate_static_contexts", unexpected)
with pytest.raises(ValueError, match=f"frozen_record_mismatch:{key}"):
sweep.run(freeze_path=path)