Files
Jyotisha/scripts/report_render/birth_precision_window.py
T
ClaudeandClaude Fable 5.2 def4c986bb
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m31s
Independent Staging Quality Gate / validate-python (push) Successful in 7m0s
Independent Staging Quality Gate / publish (push) Successful in 3m34s
fix: plain advice, plain distances and dates, no grade letters in the annual report, named people in synastry analysis (BUG-2034)
Reader-layer leftovers from the round-eight staging retest (astrologer's
2026-11-19 layering rule; owner's no-hedging rule).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-19 18:29:42 +08:00

133 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Birth-time tolerance used by the personal report's evidence grading."""
from datetime import datetime
from math import isfinite
import re
# BUG-2013 (占星师 2026-11-19 第 5 条): 前后各 1 分钟是产品设定的敏感性检查范围,不是出生时间的
# 真实误差。填写到分钟不等于误差就是 ±1 分钟:四舍五入记录通常是 ±30 秒,截去秒数是那一分钟到下一分钟
# 之前,「大约某分钟」要另外记录。这三种记录方式目前没有分别建模(待办),数值不变。
CONFIRMED_TOLERANCE_MINUTES = 1.0
TOLERANCE_KIND = "product_sensitivity_range"
DEFAULT_TOLERANCE_MINUTES = 30.0
_WINDOW_EDGE_TOLERANCE_MINUTES = 0.01
WINDOW_POLICY = "birth_record_window_v1"
DEFAULT_WINDOW_SOURCE = "default_unknown_30"
DEFAULT_WINDOW_SCENARIO = "default_window_scenario"
DEFAULT_WINDOW_DISCLOSURE_ZH = "没有填写误差,这只是试算,不能确认稳定"
DEFAULT_WINDOW_DISCLOSURE_EN = "No margin was supplied. This trial does not establish stability."
_DECLARED_SOURCES = {"user_stated", "shichen", "period", "rectified_range"}
_MAX_SIDE_MINUTES = 720
_LOCAL_AT = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2})?\Z")
def _valid_side(value: object) -> bool:
return type(value) in (float, int) and 0 <= value <= _MAX_SIDE_MINUTES and isfinite(value)
def _local_at(value: object) -> bool:
if not isinstance(value, str) or not _LOCAL_AT.fullmatch(value):
return False
try:
datetime.fromisoformat(value)
except ValueError:
return False
return True
def _window_metadata(value: dict) -> dict:
metadata = {}
if _local_at(value.get("start_at")) and _local_at(value.get("end_at")):
metadata.update(start_at=value["start_at"], end_at=value["end_at"])
intervals = value.get("intervals")
if isinstance(intervals, list) and all(
isinstance(part, dict) and set(part) == {"start_at", "end_at"}
and _local_at(part["start_at"]) and _local_at(part["end_at"])
for part in intervals
):
metadata["intervals"] = intervals
# BUG-2013: charts whose ascendant changes across a line the user took a side on (chart-choice
# `chosen_lines`), and the subset whose other side was actually computed (BUG-2018; absent = none).
for key in ("resolved_line_charts", "both_versions_domains", "chosen_line_charts",
"chosen_line_charts_other_side_computed"):
entries = value.get(key)
if isinstance(entries, list) and all(isinstance(entry, str) for entry in entries):
metadata[key] = entries
return metadata
def report_birth_window(value: object, *, minute_confirmed: bool) -> dict:
supplied = value if isinstance(value, dict) else {}
if minute_confirmed:
tolerance = CONFIRMED_TOLERANCE_MINUTES
metadata = _window_metadata(supplied) if supplied.get("source") in ("rectified_range", "confirmed_minute") else {}
return {"before_minutes": tolerance, "after_minutes": tolerance, "source": "confirmed_minute", **metadata}
before, after, source = supplied.get("before_minutes"), supplied.get("after_minutes"), supplied.get("source")
if not (isinstance(source, str) and source in _DECLARED_SOURCES
and _valid_side(before) and _valid_side(after) and (before or after)):
before = after = DEFAULT_TOLERANCE_MINUTES
source = "default_unknown_30"
result = {"before_minutes": before, "after_minutes": after, "source": source}
if source == "rectified_range":
result.update(_window_metadata(supplied))
if source in ("user_stated", "default_unknown_30") and supplied.get("rectified_range_rejected") == "clock_outside_chosen_side":
result["rectified_range_rejected"] = "clock_outside_chosen_side"
return result
def crosses_birth_window(before: float | None, after: float | None, window: dict) -> bool:
"""Check for a crossing in either direction, keeping the confirmed-minute rule inclusive."""
before_limit, after_limit = window["before_minutes"], window["after_minutes"]
if window.get("source") == "confirmed_minute":
return (before is not None and before <= before_limit
or after is not None and after <= after_limit)
return (before is not None and before < before_limit - _WINDOW_EDGE_TOLERANCE_MINUTES
or after is not None and after < after_limit - _WINDOW_EDGE_TOLERANCE_MINUTES)
def plain_minutes_zh(minutes: float) -> str:
"""A distance in birth time as a reader would say it: 「5 分 1 秒」, 「18 秒」, 「19 分钟」.
BUG-2034 (复测第八轮): 「提前 5.017 分钟」「前 71.517 分钟」 reached the report. Whole minutes keep
their old wording; a fraction is written as minutes and seconds. The stored numbers are unchanged.
"""
seconds = int(round(float(minutes) * 60))
whole, rest = divmod(seconds, 60)
if rest == 0:
return f"{whole} 分钟"
return f"{whole} 分 {rest} 秒" if whole else f"{rest} 秒"
def birth_window_label_zh(window: dict) -> str:
source = window["source"]
before, after = window["before_minutes"], window["after_minutes"]
if source == "confirmed_minute":
# 原值: 「已确认到分钟,按前后各 1 分钟核对」
# 新值: 写明 1 分钟是产品的敏感性检查范围
# 原因: BUG-2013,占星师 2026-11-19 第 5 条:时间显示精度不等于出生时间准确度
return f"按填写的分钟计算;另按前后各 {before:g} 分钟做敏感性检查,这不是出生时间的误差范围"
if source == "default_unknown_30":
return f"没有填写误差,按前后各 {before:g} 分钟试算"
label = "按填写时给的范围"
if source == "rectified_range":
label = "按生时纠正选定的区间"
if "start_at" in window and "end_at" in window:
label += f" {window['start_at'][11:16]}–{window['end_at'][11:16]}"
# 原值: 「(前 6.5 分钟、后 1.7 分钟)」;新值: 「(前 6 分 30 秒、后 1 分 42 秒)」;整分钟写法不变
# 原因: BUG-2034,读者看到的距离不写带小数的分钟
return f"{label}(前 {plain_minutes_zh(before)}、后 {plain_minutes_zh(after)})"
def birth_window_disclosure(window: dict, language: str = "zh") -> str:
if language == "en":
before, after = window["before_minutes"], window["after_minutes"]
if window["source"] == DEFAULT_WINDOW_SOURCE:
return f"Trial calculation from {before:g} minutes earlier to {after:g} minutes later. {DEFAULT_WINDOW_DISCLOSURE_EN}"
source = {"rectified_range": "Rectified interval", "confirmed_minute": "Sensitivity check around the entered minute"}.get(
window["source"], "Declared birth-time range"
)
return f"{source}: {before:g} minutes earlier to {after:g} minutes later."
label = birth_window_label_zh(window)
return f"{label}。{DEFAULT_WINDOW_DISCLOSURE_ZH}。" if window["source"] == DEFAULT_WINDOW_SOURCE else label