diff --git a/CHANGELOG.md b/CHANGELOG.md index 8738cb0f..fb06cee8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 印度占星 Skill 更新日志 +## 2026-11-17 · 生时纠正自选优先保存最近的分钟 + +- 自选后优先采用离填写时间最近、与换座线至少相隔 60 秒的段内整分钟。占星 Skill 版本不变(BUG-1952)。 + ## 2026-11-16 · 生时纠正说明六张盘的变化并开放用户自选 - 结果页展示各盘换座的分界,可自己选线前、线后或不确定,采用后可恢复填写的时间。占星 Skill 版本不变(BUG-1936)。 diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 44ea16fc..4ce39ce3 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -26239,3 +26239,14 @@ - 验证与防复发:`frontend/tests/report-polling-contract.test.ts` 更新文案断言(写明原值、新值、原因)并新增一条,钉住超时页的轮询和「仍在生成不切回进度页」的判断。未在真实浏览器里等满 8 分钟验证。 - 关联记录:BUG-1941。 - 修复版本:`claude/report-timeout-poll-1950`,待 staging 实测。 + +## BUG-1952 | 生时纠正自选保存的时刻离填写时间过远 + +- 状态:fixed-pending-verify(待独立验收)。 +- 首次发现 / 最近更新:2026-11-17 / 2026-11-17。 +- 触发与现象:用户自选线前或线后,填写时间落在选定段外。保存时取接近段中间的代表分钟,造成不必要的时刻偏移。 +- 根因:BUG-1936 自选保存沿用段中点算法。原回归验证返回值落在选定段并保留原申报,未验证区间内到填写时间的最短距离。 +- 修复:保留段内填写时间。段外选离填写时间最近的整分钟,换座线一侧至少留 60 秒。短段沿用原回退,「不确定」的另一版复用同一规则。 +- 验证与防复发:合成用例覆盖线前线后、整分钟边界、原始范围边界和短段回退。原生引擎分别重算两侧六盘,并验证重复请求与另一版采用时刻。见 [#306 进度](tasks/PROGRESS-rect-self-choice-nearest-minute-20261117.md)。 +- 关联记录:#306、#301、BUG-1936、BUG-1872。 +- 修复版本:`codex/rect-self-choice-nearest-minute-20261117`,待独立验收。 diff --git a/docs/tasks/PROGRESS-rect-self-choice-nearest-minute-20261117.md b/docs/tasks/PROGRESS-rect-self-choice-nearest-minute-20261117.md new file mode 100644 index 00000000..e4ad0deb --- /dev/null +++ b/docs/tasks/PROGRESS-rect-self-choice-nearest-minute-20261117.md @@ -0,0 +1,28 @@ +# #306 生时纠正自选采用最近整分钟 + +实现基于 `origin/staging` 的 `41357404f`。任务书背景例的 10:01 与详细规则冲突。按详细规则,10:00:30 换座后第一个符合 60 秒间隔的整分钟是 10:02。 + +原始范围传给自选时间计算,用于识别由换座线形成的段边界。选线前时,段尾是分界线前一秒,间隔仍按实际分界线计算。填写时间在段内时直接保留。短段沿用代表分钟和秒级回退。 + +## 断言与夹具更新 + +| 项目 | 原值 | 新值 | 原因 | +| --- | --- | --- | --- | +| Python 两次选边的保存和前端确认按钮 | 04:55 | 04:58 | 最近的合格整分钟 | +| 前端夹具的第一轮线前预览 | 04:44 | 04:58 | 采用预览段内最近的合格整分钟 | +| 第一轮线前预览的 D9、D10 显示 | 天秤、双子 | 天蝎、巨蟹 | 在新的预览时刻从引擎重算 | +| 「不确定」另一版 | 早于线时(夹具 04:55) | 04:58 | 断言最近的合格整分钟,并验证另一侧六盘 | +| 跨午夜短段 | 落在段内 | 沿用原回退的中点秒值 | 锁定短段的实际回退结果 | + +合成用例覆盖自选预览和确认保存。前端夹具从同一份虚构输入的原生引擎输出刷新。 + +## 验证 + +- 前端自选回归通过(4 项,0 失败、0 取消)。前端全量 ESLint 为 0 错误,既有告警 63 条。 +- Linux 容器通过 `python -m pytest -q -o addopts= tests/test_chart_choice.py`(57 项)和 `python scripts/run_quality_gate.py --profile quick --skip-frontend-runtime`。 +- 仓库隐私标记测试通过(62 项)。 +- Windows 开工预检确认 Gitea 远端和外部引擎适配器可用。碎片审计在重试时仍触及子进程的 120 秒上限,相应四项预检测试未完成。见预检台账 ERR-006、ERR-015。 + +## 已知边界 + +新规则在用户再次自选并保存时生效。存量档案与结果保留原来的采用时刻。界面文案、接口字段沿用既有合同。 diff --git a/docs/tasks/TASK-rect-self-choice-nearest-minute-20261117.md b/docs/tasks/TASK-rect-self-choice-nearest-minute-20261117.md new file mode 100644 index 00000000..a7592dd2 --- /dev/null +++ b/docs/tasks/TASK-rect-self-choice-nearest-minute-20261117.md @@ -0,0 +1,51 @@ +# 生时纠正自选:保存时间取离所填时间最近的那一分钟(2026-11-17,Claude) + +Bug 编号:BUG-1952。关联 BUG-1936(#301)。 + +## 背景 + +#301 上线后,用户自己选了线前或线后、而所填时间不在选定的那一段里时,保存的时间取那一段的代表分钟(接近那一段的中间)。例如填 10:00,线在 10:00:30,选线后,那一段是 10:00:30 到 10:14:59,保存成 10:07。保存时间离所填时间差了 7 分钟,Vimshottari 各级起止日期因此移动几周。 + +产品负责人 11-17 的决定:这种情况保存时间改成**选定那一段里离所填时间最近的整分钟**。上例应保存 10:01。 + +## 只改这一处 + +`scripts/chart_choice/service.py::_self_choice_at(segment, reported)`: + +1. 所填时间在选定段内:保持所填时间不变(现状,不改)。 +2. 所填时间不在选定段内:取段内离所填时间最近的整分钟(秒为 0),并且这个整分钟离**换座线形成的段边界**不少于 `EDGE_GUARD_SECONDS`(现值 60 秒,沿用 `scripts/chart_choice/boundaries.py` 的常量,不改它)。段边界是用户原始范围的起止而不是换座线时,那一侧不需要留这个间隔。 + - 例:所填 10:00:00,线 10:00:30,选线后 → 10:01:00 离线只有 30 秒,不够;取 10:02:00。 + - 例:所填 05:00:00,线 04:59:43,选线前 → 段尾是 04:59:42,04:59:00 离线 43 秒,不够;取 04:58:00。 + - 例:所填 10:00:00,线 10:05:10,选线后 → 取 10:07:00(10:06:00 离线 50 秒,不够)。 +3. 段太短,找不到满足第 2 条的整分钟:维持现在的回退(`representative_minute`,再不行 `representative_time`),不新写规则。 + +「不确定」时另一版的时间(`other_saved_at`)走同一个函数,自然一起变,不单独处理。 + +不改 `representative_minute`、`representative_time` 本身(旧流程还在用),不改前端文案,不改接口字段,不迁移已保存的记录(已经自选过的用户重新选一次才会变)。这一点写进 PROGRESS 的「已知边界」。 + +实现时注意:`_self_choice_at` 现在只拿到段的起止,分不清哪一侧是换座线、哪一侧是原始范围边界。把这个信息传进来(例如传入原始范围,或传入两侧是否为线的标记),不要靠猜。 + +## 测试 + +`tests/test_chart_choice.py` 增加(用合成出生输入,不用真人资料): + +- 上面三个例子各一条,断言保存时间。 +- 所填时间在选定段内:保存时间等于所填时间。 +- 选定段一侧是原始范围边界:那一侧不留 60 秒间隔。 +- 段太短没有合格整分钟:结果与改动前相同。 +- 保存时间上重算六张盘的上升星座,与所选那一侧的星座一致(防止落到线的另一边)。 +- 「不确定」:`other_saved_at` 落在另一侧,且同样满足上述规则。 +- 同一请求重复两次结果相同。 + +`frontend/tests/chart-choice-self-choice-20261116.test.tsx` 里若有写死旧代表分钟的断言,改成新值并在 PROGRESS 逐条说明;不删别的断言。 + +## 文档 + +- `docs/BUG_HISTORY.md` 新增 BUG-1952,关联 BUG-1936(只在自己那一节内编辑;不写姓名、出生资料、邮箱、ID)。 +- `CHANGELOG.md` 一行;`docs/tasks/PROGRESS-rect-self-choice-nearest-minute-20261117.md`。 + +## 验收 + +- `PATH=/bin:$PATH python scripts/run_quality_gate.py --profile quick --skip-frontend-runtime` 通过; +- `tests/test_chart_choice.py` 全部通过;`cd frontend && npx tsx --test tests/chart-choice-self-choice-20261116.test.tsx` 通过;`npx eslint .` 0 错误; +- diff 只含 `scripts/chart_choice/service.py`、上述测试和文档。 diff --git a/frontend/tests/chart-choice-self-choice-20261116.test.tsx b/frontend/tests/chart-choice-self-choice-20261116.test.tsx index fc712e05..5471f06e 100644 --- a/frontend/tests/chart-choice-self-choice-20261116.test.tsx +++ b/frontend/tests/chart-choice-self-choice-20261116.test.tsx @@ -42,7 +42,7 @@ test("self-choice result describes the six charts from real transitions and only test("self-choice page selects each line once, stops or keeps narrowing, and offers restore on readback", async () => { for (const scenario of [ - { actions: ["线前", "线后"], previews: [fixtures.before, fixtures.finished], end: fixtures.finished, time: "04:55" }, + { actions: ["线前", "线后"], previews: [fixtures.before, fixtures.finished], end: fixtures.finished, time: "04:58" }, { actions: ["线后", "就到这里"], previews: [fixtures.after], end: fixtures.after, time: "05:00" }, { actions: ["不确定"], previews: [fixtures.unsure], end: fixtures.unsure, time: "05:00" }, ]) { diff --git a/frontend/tests/fixtures/chart-choice-self-choice-20261116.json b/frontend/tests/fixtures/chart-choice-self-choice-20261116.json index 14feaf1e..2480c4ee 100644 --- a/frontend/tests/fixtures/chart-choice-self-choice-20261116.json +++ b/frontend/tests/fixtures/chart-choice-self-choice-20261116.json @@ -747,7 +747,7 @@ }, "before": { "decision": { - "saved_at": "1997-08-08T04:44", + "saved_at": "1997-08-08T04:58", "segment": { "start_at": "1997-08-08T04:30:00", "end_at": "1997-08-08T04:59:42" @@ -766,13 +766,13 @@ }, { "chart": "D9", - "sign": "天秤", + "sign": "天蝎", "decided_by": null, "flips_at": "1997-08-08T04:35:08" }, { "chart": "D10", - "sign": "双子", + "sign": "巨蟹", "decided_by": null, "flips_at": "1997-08-08T04:30:14" }, @@ -1066,7 +1066,7 @@ }, "finished": { "decision": { - "saved_at": "1997-08-08T04:55", + "saved_at": "1997-08-08T04:58", "segment": { "start_at": "1997-08-08T04:51:30", "end_at": "1997-08-08T04:59:42" @@ -1213,7 +1213,7 @@ "change" ], "chosen_side": "after", - "other_saved_at": "1997-08-08T04:55" + "other_saved_at": "1997-08-08T04:58" } }, "minuteDistance": { diff --git a/scripts/chart_choice/service.py b/scripts/chart_choice/service.py index 877c5b22..dbb69073 100644 --- a/scripts/chart_choice/service.py +++ b/scripts/chart_choice/service.py @@ -14,6 +14,7 @@ from typing import Any, Final, TypeAlias from scripts.chart_choice import adaptive, chart_questions, interview from scripts.chart_choice.boundaries import ( + EDGE_GUARD_SECONDS, LEGACY_DIVISIONS, MAX_WINDOW_MINUTES, ChartChoiceInputError, @@ -614,10 +615,22 @@ def _adaptive(body: dict, req: ChartRequest, base: dict, segment: Segment, repor return {**packet, "status": "complete", "saved_at": decision["saved_at"], "decision": decision} -def _self_choice_at(segment: Segment, reported: datetime) -> datetime: +def _self_choice_at(segment: Segment, reported: datetime, original: Segment) -> datetime: lo, hi = segment if lo <= reported <= hi: return reported + + guard = timedelta(seconds=EDGE_GUARD_SECONDS) + guarded_lo = lo + guard if lo != original[0] else lo + # A segment before a line ends one second before the line itself. + guarded_hi = hi + ONE_SECOND - guard if hi != original[1] else hi + first = guarded_lo.replace(second=0, microsecond=0) + if first < guarded_lo: + first += timedelta(minutes=1) + last = guarded_hi.replace(second=0, microsecond=0) + if first <= last: + return first if reported < lo else last + minute = representative_minute((lo, hi + ONE_SECOND), reported) return minute if lo <= minute <= hi else representative_time(segment, reported) @@ -638,7 +651,8 @@ def _self_choice( choices = body.get("choices") if not isinstance(choices, list): raise ChartChoiceInputError("choices must be a list") - all_lines = _self_choice_lines(req, *segment) + original = segment + all_lines = _self_choice_lines(req, *original) if not all_lines: raise ChartChoiceInputError("no self-choice line in the window") @@ -659,7 +673,7 @@ def _self_choice( lines = [line for line in lines if segment[0] < line.at <= segment[1]] next_line = nearest_line(lines, reported) - saved = _self_choice_at(segment, reported) + saved = _self_choice_at(segment, reported, original) resolved = {"saved_at": _saved_at(saved), "segment": _segment(*segment), "choices": choices, "key_charts": _key_chart_rows(req, saved, lines, []), "line_summary": _line_summary(lines, reported), "both_versions": []} @@ -668,7 +682,7 @@ def _self_choice( preceding = max((line.at for line in lines if line.at < unsure.at), default=segment[0]) following = min((line.at for line in lines if line.at > unsure.at), default=segment[1] + ONE_SECOND) other = (unsure.at, following - ONE_SECOND) if chosen == "before" else (preceding, unsure.at - ONE_SECOND) - resolved.update(chosen_side=chosen, other_saved_at=_saved_at(_self_choice_at(other, reported)), + resolved.update(chosen_side=chosen, other_saved_at=_saved_at(_self_choice_at(other, reported, original)), both_versions=[DOMAIN[key]["key"] for key in KEY_CHARTS if key in unsure.flips]) elif choices: resolved["chosen_side"] = choices[-1]["side"] diff --git a/tests/test_chart_choice.py b/tests/test_chart_choice.py index 5bc1bf05..a7ca32e0 100644 --- a/tests/test_chart_choice.py +++ b/tests/test_chart_choice.py @@ -16,6 +16,7 @@ from scripts.chart_choice.boundaries import ( # noqa: E402 ChartRequest, LEGACY_DIVISIONS, Line, + ascendant_longitude, find_lines, nearest_line, representative_minute, @@ -25,7 +26,8 @@ from scripts.chart_choice.boundaries import ( # noqa: E402 ) from scripts.chart_choice import adaptive, astrologer, chart_questions, interview # noqa: E402 from scripts.chart_choice.decision import decide, decide_with_evidence, reported_is_neutral # noqa: E402 -from scripts.chart_choice.service import build_packet, decide_packet # noqa: E402 +from scripts.chart_choice.readings import SIGN_CN # noqa: E402 +from scripts.chart_choice.service import _self_choice_at, build_packet, decide_packet # noqa: E402 # Fictional reference input (not a user record): mid-latitude north China, early morning. BODY = { @@ -733,8 +735,23 @@ def test_self_choice_replays_real_lines_without_scoring(monkeypatch): decided = decide_packet({**body, "choices": choices, "saved_at": BODY["end_at"], "segment": {"start_at": BODY["start_at"], "end_at": BODY["end_at"]}}) assert decided == decide_packet({**body, "choices": choices}) - assert decided["decision"]["saved_at"].endswith("04:55") - assert decided["decision"]["choices"] == choices + assert decided["decision"]["saved_at"].endswith("04:58") + decision = decided["decision"] + saved = datetime.fromisoformat(decision["saved_at"]) + selected_side = datetime.fromisoformat(first["at"]) - timedelta(seconds=1) + charts = (1, 9, 10, 2, 6, 8) + expected_signs = signature(ascendant_longitude(REQ, selected_side), charts) + assert signature(ascendant_longitude(REQ, saved), charts) == expected_signs + assert {row["chart"]: row["sign"] for row in decision["key_charts"]} == { + chart: SIGN_CN[sign] for chart, sign in expected_signs.items() + } + assert decision["choices"] == choices + picked_after = decide_packet({**body, "reported_at": "1997-08-08T04:59", + "choices": [pick(first, "after")]})["decision"] + assert picked_after["saved_at"].endswith("05:01") + assert signature(ascendant_longitude(REQ, datetime.fromisoformat(picked_after["saved_at"])), charts) == ( + signature(ascendant_longitude(REQ, datetime.fromisoformat(first["at"])), charts) + ) assert decide_packet({**body, "choices": choices[:1]}) == {"decision": before["decision"]} assert "votes" not in decided["decision"] @@ -743,7 +760,10 @@ def test_self_choice_replays_real_lines_without_scoring(monkeypatch): assert unsure["decision"]["segment"] == initial["decision"]["segment"] assert unsure["decision"]["saved_at"] == BODY["reported_at"] assert unsure["decision"]["chosen_side"] == "after" - assert unsure["decision"]["other_saved_at"] < first["at"] + assert unsure["decision"]["other_saved_at"].endswith("04:58") + other_saved = datetime.fromisoformat(unsure["decision"]["other_saved_at"]) + assert datetime.fromisoformat(first["at"]) - other_saved >= timedelta(seconds=60) + assert signature(ascendant_longitude(REQ, other_saved), charts) == expected_signs assert unsure["decision"]["both_versions"] == ["career", "money", "pressure", "change"] with pytest.raises(ChartChoiceInputError, match="next available"): decide_packet({**body, "choices": [pick(before["next"], "after")]}) @@ -764,9 +784,26 @@ def test_self_choice_preserves_boundary_second_when_no_whole_minute_fits(): assert after["saved_at"].endswith("05:00") assert after["segment"]["start_at"] == first["at"] - from scripts.chart_choice.service import _self_choice_at - lo = datetime(1997, 8, 8, 23, 59, 43) hi = lo + timedelta(seconds=10) - assert lo <= _self_choice_at((lo, hi), lo + timedelta(minutes=2)) <= hi - assert _self_choice_at((lo, lo), hi) == lo + original = (lo - timedelta(minutes=3), hi + timedelta(minutes=3)) + assert _self_choice_at((lo, hi), lo + timedelta(minutes=2), original) == lo + timedelta(seconds=5) + assert _self_choice_at((lo, lo), hi, original) == lo + + +@pytest.mark.parametrize(("segment", "original", "reported", "expected"), [ + (("10:00:30", "10:14:59"), ("09:45:00", "10:15:00"), "10:00:00", "10:02:00"), + (("04:45:00", "04:59:42"), ("04:45:00", "05:15:00"), "05:00:00", "04:58:00"), + (("10:05:10", "10:14:59"), ("09:45:00", "10:15:00"), "10:00:00", "10:07:00"), + (("10:00:30", "10:14:59"), ("09:45:00", "10:15:00"), "10:01:00", "10:01:00"), + (("10:00:30", "10:02:00"), ("09:45:00", "10:02:00"), "10:00:00", "10:02:00"), + (("09:45:00", "10:00:59"), ("09:45:00", "10:15:00"), "10:02:00", "10:00:00"), +], ids=[ + "just-after-line", "just-before-line", "later-line", "keep-reported", "original-end", + "exactly-sixty-seconds-before-line", +]) +def test_self_choice_nearest_guarded_minute(segment, original, reported, expected): + def at(clock): + return datetime.fromisoformat("2000-01-01T" + clock) + + assert _self_choice_at(tuple(map(at, segment)), at(reported), tuple(map(at, original))) == at(expected)