diff --git a/CHANGELOG.md b/CHANGELOG.md index ba29e2d8..902e3892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 印度占星 Skill 更新日志 +## 2026-11-16 — 生时纠正复跑逐臂核对并补齐 P0 证据 + +- 新增完整四臂比较、历史范围读回回归与只读聚合统计。占星 Skill 版本不变(BUG-1925)。 + ## 2026-11-16 — 合盘分析逻辑不再夹英文 - 分析逻辑里的相位名、盘名、运限主星名统一写中文,引号用「」;现实建议先写要做的事。占星 Skill 版本不变(BUG-1927)。 diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index d1761a39..74c26318 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -25978,6 +25978,17 @@ - 关联记录:BUG-1923、`references/product_runtime_contract.md` §R、`deploy/README.md`。 - 修复版本:`claude/raman-house-texts-1116`。 +## BUG-1925 | 生时纠正复跑的逐字段差异与缺臂假通过 + +- 状态:fixed-pending-verify(待独立验收)。 +- 首次发现 / 最近更新:2026-11-16 / 2026-11-16。 +- 触发与现象:占星师复验 #279 包时,四臂汇总、答案、命中和最终区间一致,严格比较在年龄切片时长失败。包装器按候选输出行数截取归档,缺组、空组或少行也可能通过。 +- 根因:时间边界由浮点运限换算到微秒,再相减形成时长。固定反例能重现端点微秒差。包装器缺少独立的文件、行数和案例偏差网格合同。 +- 修复:新增严格四臂键集与汇总比较器和独立烟测合同,半毫秒容差仅用于时长叶字段。余下 P0 证据记录到清单,并以合成数据核对旧请求拒绝、读回和聚合。 +- 验证与防复发:`tests/test_rectification_replay_compare_20261116.py` 覆盖缺、空、短、多、重键与字段边界。全量 2 464 行和独立 32 行烟测均已重跑。新增前端证据测试同一旧请求的顺序/并行拒绝,以及档案读回与历史保留。 +- 关联记录:#279、#292、BUG-1890、BUG-1896。 +- 修复版本:`codex/rect-p0-evidence-20261116`。证据见 [P0 清单](research/rect-p0-evidence-20261116.md) 和 [进度](tasks/PROGRESS-rect-p0-evidence-20261116.md)。 + ## BUG-1927 | 合盘「分析逻辑」里还夹着英文相位名、D1、「北交点Rahu」和单引号 - 状态:fixed-pending-verify。 diff --git a/docs/operations/rect-p0-aggregate-20261116.sql b/docs/operations/rect-p0-aggregate-20261116.sql new file mode 100644 index 00000000..703f4293 --- /dev/null +++ b/docs/operations/rect-p0-aggregate-20261116.sql @@ -0,0 +1,106 @@ +-- Rectification P0 aggregate v1, 2026-11-16. +-- Public business schema through staging 20b02e0a. The Supabase migration source +-- and self-hosted PostgreSQL 17 use the same qualified business tables. +-- Run under the owner's approved read-only reporting identity. One result set: +-- metric, adoption_source, contract, source, rule, report_type, count_display. +-- Every returned count below five is "<5". Only fixed vocabulary is emitted. +-- Scope is profiles with an active accepted/confirmed time. "Retired" describes +-- the current stored source. Reports are ready rows on those profiles, not a +-- claim that each report was originally generated under that adoption. +-- There is no profile-bound history for browser Markdown downloads. +-- Legacy standalone synastry rows have no reliable subject binding and are omitted. + +begin transaction isolation level repeatable read read only; + +with active_subjects as ( + select p.id as owner_id, null::uuid as subject_id, p.active_birth_provenance as provenance, + p.active_birth_date, p.active_birth_time + from public.profiles p + where p.active_birth_time is not null and p.birth_time_status in ('accepted', 'confirmed') + union all + select p.user_id, p.id, p.active_birth_provenance, p.active_birth_date, p.active_birth_time + from public.chart_profiles p + where p.role = 'other' and p.active_birth_time is not null + and p.birth_time_status in ('accepted', 'confirmed') +), attributes as ( + select a.owner_id, a.subject_id, a.provenance ->> 'contract' as contract, + a.provenance ->> 'source' as source, a.provenance ->> 'rule' as rule, + a.provenance ->> 'retired_reason' as retired_reason, + coalesce(a.provenance ->> 'decided_by', r.detail #>> '{decision,decided_by}') as decided_by, + r.detail #>> '{provenance,contract}' as receipt_contract, + coalesce(r.detail #>> '{decision,rule}', r.rule) as receipt_rule, + a.provenance -> 'rounds' as rounds + from active_subjects a + left join public.chart_choice_records r + on r.id = case when a.provenance ->> 'record_id' ~* '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$' + then (a.provenance ->> 'record_id')::uuid end + and r.user_id = a.owner_id and r.chart_profile_id is not distinct from a.subject_id + and r.saved_date = a.active_birth_date and r.saved_time = a.active_birth_time +), classified as ( + select *, + case + when contract = 'segment-v1' or source = 'server_segment_midpoint' then 'segment-v1' + when source = 'user_choice_from_answers' + or (contract = 'chart-choice-v2' and rule = 'chart_answers') then 'old_questionnaire' + when coalesce(receipt_contract, contract) = 'chart-choice-v1' + and coalesce(receipt_rule, rule) = 'evidence' and decided_by = 'event' then 'old_event' + when coalesce(receipt_rule, rule) = 'user_override' then 'explicit_manual_choice' + when contract = 'chart-choice-v2' and jsonb_typeof(rounds) = 'array' and rounds <> '[]'::jsonb + then 'issue_229_chart_side' + when source in ('reported_time_default', 'window_midpoint_default') then 'declared_default' + else 'unknown' + end as category + from attributes +), safe_groups as ( + select category, + case when contract in ('segment-v1', 'chart-choice-v1', 'chart-choice-v2', 'agentic-rectification-v1') + then contract when contract is null then 'missing' else 'other' end as contract, + case when source in ('user_choice', 'user_choice_from_answers', 'server_segment_midpoint', + 'reported_time_default', 'window_midpoint_default', 'certain') + then source when source is null then 'missing' else 'other' end as source, + case when rule in ('chart_answers', 'evidence', 'user_override', 'reused_record', 'reported_time_default', + 'window_midpoint_default', 'majority', 'tie_keeps_reported', 'strong_override', + 'reported_kept', 'stopped', 'round_limit', 'no_line_in_range') + then rule when rule is null then 'missing' else 'other' end as rule + from classified +), retired as ( + select owner_id, subject_id + from classified + where retired_reason = 'BUG-1890' or category in ('old_questionnaire', 'old_event', 'segment-v1') +), categories(category) as ( + values ('old_questionnaire'), ('old_event'), ('segment-v1'), ('explicit_manual_choice'), + ('issue_229_chart_side'), ('declared_default'), ('unknown') +), report_types(report_type) as ( + values ('personal_full'), ('annual_full'), ('synastry_full') +), scoped_reports as ( + select r.report_type + from public.personal_reports r + where r.status = 'ready' and r.report_type in (select report_type from report_types) + and exists ( + select 1 from retired s + where s.owner_id = r.user_id + and (s.subject_id is not distinct from r.chart_profile_id + or (r.report_type = 'synastry_full' and r.partner_chart_profile_id = s.subject_id)) + ) +), counts as ( + select 'provenance' as metric, null::text as adoption_source, contract, source, rule, + null::text as report_type, count(*) as n + from safe_groups + group by contract, source, rule + union all + select 'category', c.category, null, null, null, null, count(s.category) + from categories c left join safe_groups s using (category) + group by c.category + union all + select 'active_retired', null, null, null, null, null, count(*) from retired + union all + select 'ready_reports_on_retired', null, null, null, null, t.report_type, count(r.report_type) + from report_types t left join scoped_reports r using (report_type) + group by t.report_type +) +select metric, adoption_source, contract, source, rule, report_type, + case when n < 5 then '<5' else n::text end as count_display +from counts +order by metric, adoption_source, contract, source, rule, report_type; + +commit; diff --git a/docs/research/rect-p0-evidence-20261116.json b/docs/research/rect-p0-evidence-20261116.json new file mode 100644 index 00000000..3fe0e47d --- /dev/null +++ b/docs/research/rect-p0-evidence-20261116.json @@ -0,0 +1,266 @@ +{ + "schema": "rectification_p0_evidence.v1", + "issue": 294, + "bug": "BUG-1925", + "revisions": { + "staging": "20b02e0a1fbf96e9d7f0b616a0d380a2840e414b", + "evidence_implementation": "e10018eff9c06ef67e880c7cc307f708186e7697", + "issue_279": { + "accepted": "7a491ac16", + "behavior": "773a7f97d" + }, + "issue_292": { + "merge": "feb336bad672a17166b9aa435837c59e371d63db", + "behavior": "51c84cb4d" + }, + "chart_choice_commits_after_279": [ + "51c84cb4d" + ], + "chart_choice_commits_after_292": [], + "frozen_engine": "845854ff79ac72c931daa4e34f97f7084e8f35d4", + "archive_runner": "e6d2d7a4f" + }, + "evidence": { + "rejected_requests": { + "path": "frontend/tests/rectification-p0-evidence-20261116.test.ts", + "test": "legacy choice requests reject individually, on repeat and concurrently without storage writes", + "http_status": 400, + "codes": { + "useAnswerWindow": "answer_window_disabled", + "overrideSide": "questionnaire_paused", + "resumeRecordId": "questionnaire_paused", + "resolved_nonempty": "questionnaire_paused", + "chart_answers": "questionnaire_paused" + }, + "synthetic_storage_reads_match": true, + "storage_and_engine_calls": 0 + }, + "refused_history_reuse": { + "function": "reuseChartChoiceRecord", + "source": "frontend/src/lib/chart-choice/server.ts", + "test": "frontend/tests/database-chart-choice-records-1654.test.ts", + "historic_binding_test": "frontend/tests/retired-birth-window-1890.test.ts" + }, + "six_entry_reads": { + "path": "frontend/tests/rectification-p0-evidence-20261116.test.ts", + "test": "a retired profile reaches new chart and report readers with its declaration and leaves history intact", + "history_sha256_unchanged": true, + "readers": { + "long_report": "buildLongformBirthPayload", + "annual": "annualBirthTime + annualBirthWindowBody", + "chat_card": "prepareConsultationRoute", + "chart": "prepareChartViewProfile", + "home": "readBirthRow + globalBirthProfileFromAccountRow + declaredBirthWindow", + "export": "buildReportExportBlocks + joinExportBlocks; synthetic rendered snapshot, no profile lookup" + } + }, + "midpoint": { + "chart": { + "birthTimeStatus": "reported", + "explicit_midpoint_field": null + }, + "home": { + "birthTimeStatus": "reported", + "explicit_midpoint_field": null + }, + "long_report": { + "birth_time_accuracy": "provisional", + "reading_minute_confirmed": false, + "time_source": "period_only", + "bounds": "birth_time_window" + }, + "annual": { + "rectification": "none", + "bounds": "birth_window" + }, + "chat": { + "mode": "declared_birth_window", + "bounds": [ + "rangeStart", + "rangeEnd" + ], + "single_chart": null + }, + "export": { + "explicit_midpoint_field": null, + "source": "stored_snapshot" + } + }, + "abstention": { + "archive_test": "ReplayControlsTest.test_stop_after_excluding_truth_reverts_to_original", + "runtime_test": "tests/test_chart_choice.py::test_adaptive_preserves_the_reported_clock_when_screening_excludes_it", + "synthetic_receipt": { + "stop_reason": "no_answer_can_improve_separation", + "set_changed": true, + "reverted_to_original": true, + "apply_allowed": false + } + }, + "simulation": { + "path": "references/real_case_calibration/rect_repro_20261114/test_replay.py", + "tests": [ + "test_scoring_truth_never_reaches_the_event_answer", + "test_event_selection_is_stable_when_options_are_reordered", + "test_empty_birthmarks_are_unknown", + "test_graduation_employment_and_relocation_share_one_fact" + ], + "independent_fact_count_deduplicated": true, + "engine_scoring_uses_original_events": true + }, + "source_audit": { + "path": "frontend/src/lib/retired-birth-window.ts", + "source_chain_fields": [ + "contract", + "source", + "rule", + "decided_by", + "record_id" + ], + "independent_birth_record_verification": null, + "manual_and_229_behavior": "retained", + "unmatched_provenance_behavior": "retained_or_unknown", + "old_evidence_behavior": "original_declaration_if_retired" + }, + "snapshots": { + "longform": { + "table": "personal_report_sections", + "section_id": "longform-calculation-snapshot-v1", + "lookup": [ + "user_id", + "request_id", + "section_id" + ], + "body_hash": "contentSha256", + "document_birth_status": "birthTimeStatus", + "separate_snapshot_rectified_flag": null + }, + "annual": { + "table": "personal_report_sections", + "section_id": "annual-report-snapshot", + "lookup": [ + "user_id", + "request_id", + "section_id" + ] + }, + "export": "stored report blocks via browser; no profile-bound export ledger" + }, + "aggregate": { + "path": "docs/operations/rect-p0-aggregate-20261116.sql", + "isolated_validation": "PostgreSQL 17 with synthetic rows, offline container", + "production_executed": false, + "small_cell_value": "<5", + "counts_without_profile_binding": [ + "old_standalone_synastry", + "browser_export" + ] + } + }, + "validation": { + "replay_archive_rows": 2464, + "rows_per_arm": 616, + "smoke_total": 32, + "frozen_hash_checks_passed": true, + "windows_rank_audit": { + "rows": 2464, + "near_tie_limit_seconds": 0.001, + "full": {"exact_duration_ties": 377118, "nonzero_near_ties": 0, "shortest_nonzero_gap_seconds": 0.005841}, + "without_pd": {"exact_duration_ties": 377333, "nonzero_near_ties": 0, "shortest_nonzero_gap_seconds": 0.811482}, + "without_narayana": {"exact_duration_ties": 508898, "nonzero_near_ties": 0, "shortest_nonzero_gap_seconds": 0.005841}, + "without_functional": {"shares_rank_with": "full"} + }, + "windows": { + "python": "3.11.7", + "all_fields_exact": true, + "summary_exact": true, + "compared_duration_count": [ + 9827, + 9544, + 10208, + 9827 + ], + "different_duration_count": [ + 0, + 0, + 0, + 0 + ], + "max_duration_delta_seconds": 0 + }, + "linux": { + "python": "3.11.18", + "all_fields_exact": false, + "summary_exact": true, + "smoke_exact": true, + "tolerant_comparison_passed": true, + "rows": 2464, + "duration_values": 39406, + "different_duration_values": 763, + "max_duration_delta_seconds": 8.8e-05, + "arms": { + "full": { + "rows": 616, + "values": 9827, + "different": 181, + "median_nonzero_seconds": "0.000003", + "max_seconds": "0.000088", + "nonzero_bins": { + "up_to_1us": 61, + "up_to_10us": 64, + "up_to_100us": 56 + } + }, + "without_pd": { + "rows": 616, + "values": 9544, + "different": 178, + "median_nonzero_seconds": "0.000002", + "max_seconds": "0.000059", + "nonzero_bins": { + "up_to_1us": 80, + "up_to_10us": 55, + "up_to_100us": 43 + } + }, + "without_narayana": { + "rows": 616, + "values": 10208, + "different": 223, + "median_nonzero_seconds": "0.000002", + "max_seconds": "0.000088", + "nonzero_bins": { + "up_to_1us": 89, + "up_to_10us": 75, + "up_to_100us": 59 + } + }, + "without_functional": { + "rows": 616, + "values": 9827, + "different": 181, + "median_nonzero_seconds": "0.000003", + "max_seconds": "0.000088", + "nonzero_bins": { + "up_to_1us": 61, + "up_to_10us": 64, + "up_to_100us": 56 + } + } + } + }, + "duration_contract": { + "path": "questions[].age_option_ranks[].time_slice_seconds", + "comparison": "absolute", + "max_tolerance_seconds": 0.0005, + "counterexample_delta_microseconds": 1 + } + }, + "missing_evidence": [ + "first divergent operation in the earlier cross-platform replay", + "independent verification of declared birth records", + "dedicated midpoint marker on chart, home, and export surfaces", + "live profile read during Markdown export", + "profile-linked export counts and counts of unbound legacy synastry snapshots", + "production aggregate measurements" + ] +} diff --git a/docs/research/rect-p0-evidence-20261116.md b/docs/research/rect-p0-evidence-20261116.md new file mode 100644 index 00000000..ba910e7b --- /dev/null +++ b/docs/research/rect-p0-evidence-20261116.md @@ -0,0 +1,43 @@ +# 生时纠正 P0 证据包(#294,BUG-1925) + +证据所用的 staging 基线为 `20b02e0a1fbf96e9d7f0b616a0d380a2840e414b`。本单的测试、比较器和聚合 SQL 位于 `e10018eff9c06ef67e880c7cc307f708186e7697`。机器可读版本见 [JSON](rect-p0-evidence-20261116.json)。 + +| 项目 | 提交与复验证据 | +| --- | --- | +| 版本 | #279 合入链的冻结说明提交 `7a491ac16`,业务改动 `773a7f97d`。#292 合入 `feb336bad672a17166b9aa435837c59e371d63db`。对 `frontend/src/components/chart-choice`、`frontend/src/app/api/chart-choice`、`frontend/src/lib/chart-choice`、`frontend/src/lib/retired-birth-window.ts` 和 `scripts/chart_choice` 执行路径限定的 `git log`。从 #279 到基线的非合并变更为 `51c84cb4d`。从 #292 到基线该路径集无变更。复验回放使用单独的冻结引擎 `845854ff`。 | +| 禁用采用与直接请求 | #279 在 `adopt/route.ts` 拒绝 `useAnswerWindow`,返回 `answer_window_disabled`。#292 在 `adopt` 和 `packet` 拒绝选边、续答、非空 `resolved` 与 `chart_answers`,返回 `questionnaire_paused`。本单运行真实路由处理器。逐载荷核对 400 和完整 code,两次顺序请求与两次并行请求的结果相同。合成存储读回哈希保持一致,存储访问与引擎调用计数均为零。 | +| 停用记录再采用 | #279 的 `reuseChartChoiceRecord` 先读取当前主体历史,并拒绝 `historicChoiceRetired` 判定的记录。既有 `retired-birth-window-1890.test.ts` 的 `old event-choice history requires the active record and exact saved second` 核对主体、日期与秒。另见 `database-chart-choice-records-1654.test.ts` 的旧事件拒绝和手选复用断言。 | +| 撤回旧窄范围及记录保留 | 本单对同一份虚构旧答题采纳档案分别调用 `buildLongformBirthPayload`、`annualBirthTime`、`prepareConsultationRoute`、`prepareChartViewProfile` 和首页账号投影(`readBirthRow`、`globalBirthProfileFromAccountRow`、`declaredBirthWindow`)。以原范围构造的合成报告快照通过实际 Markdown 导出构建器。六个入口的断言共用这份档案,历史对象前后 SHA-256 相同。导出本身读取已呈现的报告,实时档案读取证据为无。 | +| 范围中点 | 虚构档案仅填跨午夜范围。星盘与首页的 `birthTimeStatus` 为 `reported`,显式中点标记为无。长报告保持 `reading_minute_confirmed=false`、`birth_time_accuracy=provisional`、`time_source=period_only`,并携带双侧 20 分钟。年运的 `birth_window` 带双侧分钟数与 `rectification=none`。聊天的 `declared_birth_window` 模式交付原范围 `rangeStart/rangeEnd`,单点盘为空。导出沿用快照,独立的中点标记为无。 | +| 先缩窄后弃权 | #279 的冻结复跑测试 `test_stop_after_excluding_truth_reverts_to_original` 断言保存范围恢复原值。网站端 `tests/test_chart_choice.py::test_adaptive_preserves_the_reported_clock_when_screening_excludes_it` 校验原时刻投影。示例回执见下。 | +| 模拟隔离 | #279 的 `references/real_case_calibration/rect_repro_20261114/test_replay.py` 保留四个反例。真值替换 `test_scoring_truth_never_reaches_the_event_answer`,选项重排 `test_event_selection_is_stable_when_options_are_reordered`,未知胎记 `test_empty_birthmarks_are_unknown`,关联事件链 `test_graduation_employment_and_relocation_share_one_fact`。独立事实计数去重与评分去重属于不同层。评分沿用原事件输入。 | + +缩窄后弃权的合成回执如下,`saved_segments` 按测试断言等于原窗口。 + +```json +{ + "stop_reason": "no_answer_can_improve_separation", + "set_changed": true, + "reverted_to_original": true, + "apply_allowed": false +} +``` + +## 手选来源和已生成报告 + +- 医院记录、证件、自报时间与自报范围保留各自的声明来源。数据库有独立出生记录核验证据的字段或复验结果为无。显式 `user_override` 和 #229 的分盘选边仍按原规则读取采用记录。旧问卷、旧事件及 `segment-v1` 在识别出来源时撤回窄范围。旧事件历史匹配要求同一主体、日期和秒级时刻。缺来源或收据无法匹配时,读路径无法证明是否来自推断。分盘选边和来源不明的推断均有可能继续作为现行时刻。这是和占星师要求按来源链隔离仍有的差距。 +- 长报告读取 `personal_reports.report_document` 与 `personal_report_sections` 中 `longform-calculation-snapshot-v1`。该缓存按拥有者、`request_id`、`section_id` 查找,校验报告号、请求号和 Markdown 内容 SHA-256。报告主体可持久化 `birthTimeStatus`。计算快照本体无单独的「已校正」布尔字段,导出沿用已有文档及快照。旧快照从未重新核对的新出生输入为无。 +- 年运按同一报告拥有者和请求号读取 `annual-report-snapshot`;预览输入携带出生范围。合盘已有报告使用报告绑定的双方快照和分节。浏览器下载 Markdown 使用现有快照块,按档案关联的下载计数为无。 + +## 复验命令 + +```text +cd frontend +node node_modules/tsx/dist/cli.mjs --test tests/rectification-p0-evidence-20261116.test.ts tests/retired-birth-window-1890.test.ts tests/chart-choice-pause-20261115.test.tsx +cd .. +python -m pytest -q tests/test_chart_choice.py tests/test_rectification_replay_compare_20261116.py references/real_case_calibration/rect_repro_20261114/test_replay.py +``` + +四臂全量共 2 464 行。Windows 与归档精确一致。Linux 的排序、选题、分差、答案、命中、区间和汇总精确一致,切片时长有 763 处差异,最大 88 微秒。半毫秒容差仅用于切片时长的比较。两端独立生成的 32 行烟测都精确通过。 + +复跑与时长见 [四臂复跑比较](../../references/real_case_calibration/rect_repro_20261116/)。聚合统计见 [只读 SQL](../operations/rect-p0-aggregate-20261116.sql) 与 [执行记录](../tasks/PROGRESS-rect-p0-evidence-20261116.md)。生产统计由负责人按受控流程执行。 diff --git a/docs/research/rect-repro-20261114.md b/docs/research/rect-repro-20261114.md index a96f6283..f1d67b94 100644 --- a/docs/research/rect-repro-20261114.md +++ b/docs/research/rect-repro-20261114.md @@ -89,6 +89,8 @@ ## #279 引擎复跑补包 +复跑包装器的校验口径为 136 项校验,73 个不同文件。[#294 的严格比较器](../../references/real_case_calibration/rect_repro_20261116/) 另行核对完整的四臂键集和独立烟测。 + 取源提交为 `845854ff79ac72c931daa4e34f97f7084e8f35d4`。从 `results/SHA256SUMS` 筛选 `scripts/` 下的 60 个 Python 模块。用上述提交的 Git blob 提取,逐项核对 SHA256,并保留路径。另核对清单里的 3 个 `.se1` 文件。提交中的相同星历路径为 `references/open_source_sources/vedic-astro-skills/antigravity/skills/vedic-calculator/scripts/ephe/`。当前 staging 在年运、咨询和合盘模块上已有其他提交,复验不得把这些版本混入。 在干净检出根目录解压源码补包和原 #272 归档,保留 `scripts/`、`references/real_case_calibration/` 和星历原目录。用 Python 3.11。需要显式设置 `PYTHONPATH` 时加入检出根和 `scripts/`,Windows 用分号,POSIX 用冒号。先执行: diff --git a/docs/tasks/PROGRESS-rect-p0-evidence-20261116.md b/docs/tasks/PROGRESS-rect-p0-evidence-20261116.md new file mode 100644 index 00000000..688a8899 --- /dev/null +++ b/docs/tasks/PROGRESS-rect-p0-evidence-20261116.md @@ -0,0 +1,52 @@ +# 生时纠正 P0 证据补包执行记录(2026-11-16) + +- 工单 #294,BUG-1925。开工基线 `20b02e0a1fbf96e9d7f0b616a0d380a2840e414b`,任务书 `e334c9cb72041738b6affd84169aeaf82044877e`。防复发核对见 BUG-1890、BUG-1896。 + +## 时长与复跑 + +- 选择字段精度约定。实际字段是 `chart_questions._ranked_ages` 对两个已经定位的时间边界取差。正整数微秒可以精确表示两个端点的差,但端点在转成 `datetime` 前已受浮点运算影响。把时长换为整数微秒无法修复已产生的端点差。调整运限的端点算法会影响评分链,留给更大的验证范围。 +- 固定反例见 `duration_counterexample.py`。相同的数学式在 `98.25` 上以两种顺序加两个 `2^-47`。进入当前 Narayana `_boundaries` 日数转换后,同一切片得到 `31557600.0` 与 `31557600.000001` 秒,相差 1 微秒。反例定位已知的浮点传播机制。实际两台机器的第一处不同运算尚无单步追踪证据。 +- 比较器只对叶字段 `questions[].age_option_ranks[].time_slice_seconds` 允许 0.0005 秒绝对差。该值把回信中的约 0.000458 秒上限上取到半毫秒。所有其他字段和完整汇总保持精确比较。选题、年龄组、分差与答案的改变会使比较失败。排序器沿用原时长,不消费比较容差。 +- Windows Python 3.11.7 按原 #272 冻结引擎、脚本和依赖重跑。全量 2 464 行,四臂各 616;汇总和全部字段精确一致。四臂时长字段分别比对 9 827、9 544、10 208、9 827 个,共 39 406 个,差异数和最大差均为零。`--exact` 通过。独立 32 行烟测同样通过。原归档的四臂、散列及汇总校验通过。 +- Linux Python 3.11.18 在隔离 Docker 环境重跑同一冻结源与依赖。四臂共 2 464 行,汇总、选题、年龄组及其顺序、分差、答案、命中和最终区间全部精确一致。39 406 个时长值中有 763 个差异,最大 88 微秒,半毫秒比较通过。严格比较在首个非零时长差(3 微秒)退出。烟测的 32 行与汇总精确一致。 + +| 四臂 | 时长差异数 | 非零差中位数(微秒) | 最大差(微秒) | 不超过 1 微秒 | 大于 1 至 10 微秒 | 大于 10 至 100 微秒 | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| full | 181 | 3 | 88 | 61 | 64 | 56 | +| without_pd | 178 | 2 | 59 | 80 | 55 | 43 | +| without_narayana | 223 | 2 | 88 | 89 | 75 | 59 | +| without_functional | 181 | 3 | 88 | 61 | 64 | 56 | + +- 排序审计随冻结回放检查各年龄排名表的等分相邻项。两边的时长各移动半毫秒时,非并列项最多缩小 1 毫秒的间距。Windows 全量在此范围内的非零近并列为零,最小非零间距为 0.005841 秒。观察到的精确并列仍按既有年龄组键解开,Windows 回放的获选年龄组及题序与归档精确一致。 + +| 排名计算 | 等分相邻项 | 时长精确并列 | 非零最短时长差(秒) | +| --- | ---: | ---: | ---: | +| 完整 | 987 536 | 377 118 | 0.005841 | +| 去 PD | 750 504 | 377 333 | 0.811482 | +| 去 Narayana | 990 615 | 508 898 | 0.005841 | +| 去功能吉凶 | 复用完整组排名缓存 | 复用完整组排名缓存 | 复用完整组排名缓存 | + +## 合成统计 + +- [只读 SQL](../operations/rect-p0-aggregate-20261116.sql) 根据两个自报主体表的当前采用凭据和同主体历史分类,将未识别的旧值映射到固定类别。长报告、年运与合盘按与停用档案关联的 ready 记录计数,合盘两侧同时命中只计一次。旧独立合盘若无可核验的档案外键,其关联统计为无。导出是浏览器文件操作,档案绑定的次数为无。 +- 仓库的部署说明确认现行生产数据在自托管 PostgreSQL 17,Supabase 为迁移来源。两者在业务迁移版本达到同一口径后共用这份 SQL。生产和历史库均留给负责人执行。 +- 本机以 Docker 隔离 PostgreSQL 17,容器无网络。合成档案包括五条旧答题、旧事件复用、已停用段、手选、分盘选边、声明默认值、不可识别值和同一拥有者的第二档案。生成态报告被排除。以下样例用竖线分列。 + +```text +active_retired||||||8 +category|old_questionnaire|||||5 +category|old_event|||||<5 +ready_reports_on_retired|||||annual_full|<5 +ready_reports_on_retired|||||personal_full|5 +ready_reports_on_retired|||||synastry_full|5 +``` + +## 验证 + +- 前端定向 48 项通过。Python 定向 69 项通过,包含 #279 复跑包测试与比较器回归。现有测试断言未改动。 +- 额外在本机隔离 PostgreSQL 上运行 `database-chart-choice-records-1654.test.ts`,两项通过,覆盖旧停用记录拒绝复用和允许的手选历史复用。 +- `tsc --noEmit` 通过,lint 0 错误。基线与候选生产构建均通过,`/`、`/rectification` 静态,首页 39 个入口 JS/CSS 资产 gzip 共 616 927 字节,变化 0%。 +- Windows 全量前端基线 3 894 项(3 811 通过),候选 3 896 项(3 813 通过)。双方均为相同的 46 项失败、37 项跳过、0 项取消。逐项对照没有新增失败或消失的用例。 +- Windows Python 快速门执行到 pytest,1 506 项通过、2 项跳过、29 个子测试通过、4 项失败。其中三项 Kaala 负时间戳失败在开工基线复现。余下一项咨询分层现有输出对照在全量中出现 VedAstro 可用源差异,开工基线的 13 项单文件复验通过,候选的同项单跑通过。快门整体仍为失败。 +- 对 staging `85e762ee` 和本单证据用例的组合复验,前端 14 项、Python 65 项通过,生产构建通过且首页保持静态。 +- Windows 预检验证远端、Python 和适配器,碎片审计与预检定向测试达到 90 秒和 45 秒的既有时间限制。 diff --git a/docs/tasks/TASK-rect-p0-evidence-20261116.md b/docs/tasks/TASK-rect-p0-evidence-20261116.md new file mode 100644 index 00000000..2a2f9e03 --- /dev/null +++ b/docs/tasks/TASK-rect-p0-evidence-20261116.md @@ -0,0 +1,82 @@ +# 生时纠正:P0 证据包与复跑包两处修正(2026-11-16,Claude) + +Bug 编号:BUG-1925。 + +## 背景 + +占星师 11-15 复核了 #279 的引擎复跑包并回信(产品负责人 11-16 转来): + +- 2,464 条全部重现,汇总、最终区间、命中、答案一致。 +- 严格逐行比较没过。唯一差异字段是 `questions[].age_option_ranks[].time_slice_seconds`,共 8,803 处,最大差约 0.000458 秒。 +- 包装器的比较有假通过缺口:只遍历实际输出的文件,按新文件行数截取归档,缺组、空组或少行都可能通过。 +- 包装器文案写「136 个文件」,实际是 136 次哈希核对、73 个不同文件。 +- P0 不关闭:那份包冻结的是 `845854ff`,不含 P0 代码。她要一份单独版本化的 P0 小型证据包。 + +本单交付这份证据包里能在仓内完成的部分,并修掉复跑包的两处问题。原 #272 归档和冻结脚本**不改**,新东西放新目录。 + +## 不做的事 + +- 不改 `references/real_case_calibration/rect_repro_20261114/results/` 下任何文件,不改该目录已归档的 `run.py`、`verify.py`、`test_replay.py` 的冻结版本散列所对应的内容。需要新行为就在新目录新建文件。 +- 不重开问卷,不改 `RECT_QUESTIONNAIRE_ENABLED`。 +- 不改引擎评分。时长字段的源头修复只允许改那一处时长的算法,且要先交最小反例。 +- 不查询任何含真实用户资料的数据库,不导出用户明细。 +- 不碰 `scripts/report_render/`、合盘、咨询卡。 + +## 一、时长字段的最小反例和修复 + +1. 定位 `time_slice_seconds` 的来源(复跑脚本写出的是 `selected[1]`,往上追到引擎里年龄段切片时长的计算)。 +2. 写一个最小反例:固定输入,说明在什么条件下这个值会随平台、Python 版本或累加顺序变化(例如 `timedelta.total_seconds()` 与浮点累加、切片求和顺序)。反例要能在一台机器上稳定展示差异的来源,不能只写「跨平台有差异」。 +3. 二选一,并在 PROGRESS 写明理由: + - **源头修复**:把时长改成整数微秒或由端点差直接得出,使结果不依赖累加顺序。附测试。修复后在新目录重跑一遍四臂全量,报告与原归档逐字段比较的结果:除这个字段外应全部一致;这个字段给出差异分布。 + - **字段精度约定**:如果证明差异只来自不可避免的浮点表示,给出事先论证的容差(依据是什么、为什么这个量级不会改变任何排序或选题),比较器只对这一个字段按容差比较。不允许全局四舍五入。 +4. 不管选哪种,都要回答:这个差异在本批数据上有没有改变过任何一次排序或选题?有边界上的并列就列出来。 + +## 二、严格比较器 + +在新目录(建议 `references/real_case_calibration/rect_repro_20261116/`)新建完整比较脚本,要求: + +- 固定四臂:`full`、`without_pd`、`without_narayana`、`without_functional`。缺任何一臂即失败。 +- 每臂恰好 616 行;键 `(case_id, offset_minutes)` 恰好是 77 × 8 且唯一。 +- 全量逐字段比较,不按新文件行数截取归档。汇总文件同样比较。 +- smoke 有单独的 32 行合同,不能拿 smoke 的前缀比较冒充全量。 +- 测试覆盖:缺一臂、空臂、少一行、重复键、多一行,各自必须失败。 + +文案改为「136 项校验,73 个不同文件」(在新目录的说明里写,原包不动;`docs/research/rect-repro-20261114.md` 里对应句子可以改)。 + +## 三、P0 证据清单 + +新建 `docs/research/rect-p0-evidence-20261116.md` 和同目录一份机器可读的 `rect-p0-evidence-20261116.json`。逐项写明「证据是什么、在哪个提交、怎么复验」。没有证据的项写「无」,不要写成已完成。 + +| 项目 | 要给的证据 | +|---|---| +| 版本 | #279 合入提交 `7a491ac1`、#292 合入提交 `feb336ba`,以及这两处之后 staging 上涉及 `chart-choice`、`retired-birth-window` 的提交列表 | +| 禁用采用、拒绝直接请求、旧记录再采用 | 对应测试的文件名和用例名。**补测试**:旧客户端载荷(`useAnswerWindow`、`overrideSide`、`resumeRecordId`、非空 `resolved`、`chart_answers`)逐个被拒绝且返回明确 code;拒绝前后对存储的读回一致(无写入断言);同一请求重复发两次、并发发两次,结果相同且无写入 | +| 旧窄范围撤回、原记录留存 | **补一个六入口合成夹具测试**:同一份合成档案(旧答题采纳来源)分别经过长报告、年运、聊天取数卡、星盘、导出、首页的读取函数,断言每个入口拿到的都是原始时间和原范围,且历史记录原值未变(前后哈希相同)。哪个入口没有可单测的读取函数,就写明并给出替代证据 | +| 没填时间用范围中点 | 断言各入口把它标为代表时刻(字段名以现有实现为准,没有就在清单里写「无此字段」并说明现状),且原范围一路传到报告和应期的消费者,没有变成单点 | +| 先缩窄后弃权恢复原范围 | 对应测试源码位置、命令、结果;`stop_reason`、`set_changed`、`reverted_to_original`、`apply_allowed` 四个字段的一条示例回执 | +| 模拟器四个隔离反例 | 真值替换、选项重排、未知胎记、关联事件链四个测试的文件名和用例名。#279 已有的直接引用;缺的补上。清单里写明:独立事实计数去重不等于评分已去重 | +| 手动选择和分盘选边 | 按来源链判定的现状:有独立出生记录来源的、由旧问卷或事件或选边得出的、来源不明的,各自现在怎么读。只描述现状并指出与她裁定不一致的地方,**不改行为** | +| 已生成报告 | 现状描述:旧报告快照和导出读的是什么,有没有「已校正」状态字段,缓存键是什么。只盘点,不改 | + +## 四、生产聚合统计的脚本(只写脚本,不执行) + +写一份只出聚合数字的 SQL(放 `docs/operations/rect-p0-aggregate-20261116.sql`),由产品负责人在受控环境执行。要求: + +- 只有 `count`,按来源分组;不选出任何出生资料、标识、邮箱、正文。任何一组数量小于 5 时输出「<5」。 +- 要数的东西: + - 档案采纳来源的分布(`contract`、`source`、`rule` 的组合); + - 属于「旧答题」「旧事件采纳」「segment-v1」「明确手选」「#229 分盘选边」「来源不明」各有多少; + - 当前采纳来自已停用来源的档案数; + - 在这些档案上已生成的长报告、年运、合盘、导出各有多少(按报告类型)。 +- 文件头写明:口径说明、脚本版本、适用的表结构版本、输出格式。生产库现在是 Supabase,目标库是自托管 PostgreSQL,两边表名不同的话各给一份。 +- 在隔离测试库上用合成数据跑通,附输出样例。 + +## 验收 + +- **测试:** 上面写了「补」的测试全部新增并通过;现有 chart-choice 前端测试、`tests/test_chart_choice.py`、复跑包自带测试不受影响。 +- **门禁:** Python quick gate、`tsc`、`npx eslint .` 0 errors。跑全量前端测试,失败项与开工基线逐项对照。 +- **文档:** + - `docs/BUG_HISTORY.md` 新增 BUG-1925(只追加这一节):现象(严格比较未过、比较器可假通过)、根因、修复、验证、防复发,关联 BUG-1890、BUG-1896; + - CHANGELOG 一行; + - 写 PROGRESS,既有断言有改动的用「原值 / 新值 / 原因」列出。 +- **回报里必须写明:** 时长字段选了哪种处理及反例;四臂重跑与原归档的比较结果;证据清单里哪些项是「无」。 diff --git a/frontend/tests/rectification-p0-evidence-20261116.test.ts b/frontend/tests/rectification-p0-evidence-20261116.test.ts new file mode 100644 index 00000000..bee372c6 --- /dev/null +++ b/frontend/tests/rectification-p0-evidence-20261116.test.ts @@ -0,0 +1,169 @@ +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import test from "node:test"; + +import { annualBirthTime, annualBirthWindowBody } from "../src/lib/annual-birth-time.ts"; +import { prepareChartViewProfile } from "../src/lib/chart-view-profile-read.ts"; +import { prepareConsultationRoute } from "../src/lib/consultation-route-service.ts"; +import { declaredBirthWindow } from "../src/lib/lagna-precheck.ts"; +import { buildLongformBirthPayload } from "../src/lib/personal-report-longform-birth.ts"; +import { resolveReportBirthClock } from "../src/lib/personal-report-route-core.ts"; +import { buildReportExportBlocks, joinExportBlocks } from "../src/lib/report-export-blocks.ts"; +import { readBirthRow } from "../src/lib/retired-birth-window.ts"; +import { globalBirthProfileFromAccountRow } from "../src/lib/server-owned-birth-profile.ts"; + +const profile = Object.freeze({ + name: "Fictional profile", birth_date: "2000-01-01", latitude: 0, longitude: 0, timezone_offset: 0, + birth_place_label: "Fictional place", birth_time: "06:05:10", birth_time_status: "accepted", + active_birth_time: "06:05:10", active_birth_date: "2000-01-01", active_birth_timezone_offset: 1, + active_birth_provenance: Object.freeze({ contract: "chart-choice-v2", source: "user_choice_from_answers", record_id: "fictional-record" }), +}); +const history = Object.freeze({ + records: [{ id: "fictional-record", saved_time: "06:05:10", detail: { window: ["06:00", "06:10"] } }], + current_record_id: "fictional-record", +}); +const reportOptions = { + today: "2026-11-16", targetYear: 2026, birthTimeAccuracy: "confirmed", readingMinuteConfirmed: true, + candidateRange: { start_time: "06:00", end_time: "06:10" }, +} as const; + +function checksum(value: unknown): string { + return createHash("sha256").update(JSON.stringify(value)).digest("hex"); +} + +test("legacy choice requests reject individually, on repeat and concurrently without storage writes", () => { + const program = ` + import assert from "node:assert/strict"; + import { createHash } from "node:crypto"; + import { mock } from "node:test"; + const store = { profile: { active_birth_time: "06:05:10" }, history: { saved_time: "06:05:10" } }; + const readBack = () => createHash("sha256").update(JSON.stringify(store)).digest("hex"); + const before = readBack(); + const calls = { storage: 0, engine: 0 }; + function neverWrite() { + calls.storage++; + throw new Error("rejected requests must never reach storage"); + } + globalThis.fetch = async () => { + calls.engine++; + throw new Error("rejected requests must never reach the engine"); + }; + mock.module("server-only", { namedExports: {} }); + mock.module("@/lib/supabase/server", { namedExports: { + createServerSupabaseClient: async () => ({ + auth: { getUser: async () => ({ data: { user: { id: "fictional-owner" } } }) }, + from: neverWrite, rpc: neverWrite, + }), + }}); + mock.module("@/lib/supabase/admin", { namedExports: { createAdminSupabaseClient: neverWrite } }); + mock.module("@/lib/product-access", { namedExports: { isProductEnabled: async () => true } }); + mock.module("@/lib/request-rate-limit", { namedExports: { consumeUserRequestRateLimit: () => ({ok:true}) } }); + const packet = (await import("./src/app/api/chart-choice/packet/route.ts")).POST; + const adopt = (await import("./src/app/api/chart-choice/adopt/route.ts")).POST; + async function send(handler, body) { + const request = new Request("http://localhost/api/chart-choice", { method:"POST", body: JSON.stringify(body) }); + const response = await handler(request); + return [response.status, (await response.json()).code]; + } + const cases = [ + [adopt, {useAnswerWindow: true}, "answer_window_disabled"], + ...[adopt, packet].flatMap(handler => [ + {overrideSide:"before"}, {resumeRecordId:"fictional-record"}, + {resolved:[{line_at:"2000-01-01T06:05:00", chosen_side:"after"}]}, {chart_answers:{}}, + ].map(body => [handler, body, "questionnaire_paused"])), + ]; + for (const [handler, body, code] of cases) { + const expected = [400, code]; + assert.deepEqual(await send(handler, body), expected); + assert.deepEqual(await send(handler, body), expected); + assert.deepEqual(await Promise.all([send(handler, body), send(handler, body)]), [expected, expected]); + assert.equal(readBack(), before); + } + assert.deepEqual(calls, { storage: 0, engine: 0 }); + `; + const result = spawnSync(process.execPath, [ + "--experimental-test-module-mocks", "--import", "tsx", "--input-type=module", "--eval", program, + ], { cwd: new URL("..", import.meta.url), encoding: "utf8", timeout: 30_000 }); + assert.ifError(result.error); + assert.equal(result.status, 0, result.stderr || result.stdout); +}); + +test("a retired profile reaches new chart and report readers with its declaration and leaves history intact", async () => { + const rangeCases = [{ + row: { + ...profile, reported_birth_time: "05:40", birth_time_source: "approximate", + declared_window_start: "05:30", declared_window_end: "06:00", + }, + clock: "05:40", clockParts: [5, 40], halfWidths: [10, 20], reportWindowSource: "user_stated", + consultationMode: "unverified_birth_time", + interval: { start_at: "2000-01-01T05:30", end_at: "2000-01-01T06:00" }, + }, { + row: { + ...profile, reported_birth_time: null, birth_time_source: "period_only", birth_time_period: "custom_range", + declared_window_start: "23:50", declared_window_end: "00:30", + }, + clock: "00:10", clockParts: [0, 10], halfWidths: [20, 20], reportWindowSource: "period", + consultationMode: "declared_birth_window", + interval: { start_at: "2000-01-01T23:50", end_at: "2000-01-02T00:30" }, + }]; + + for (const { row, clock, clockParts, halfWidths, reportWindowSource, consultationMode, interval } of rangeCases) { + const stored = { profile: row, history }; + const before = checksum(stored); + const chart = await prepareChartViewProfile({ row, resolveTimezone: async (value) => value }); + assert.ok(chart.ok); + assert.equal(chart.profile.time, clock); + assert.equal(chart.profile.birthTimeStatus, "reported"); + assert.equal(chart.profile.timezoneOffset, 0); + + const accountRead = await readBirthRow(row, null, "fictional-owner", "self"); + const home = globalBirthProfileFromAccountRow(accountRead); + assert.equal(home.time, clock); + assert.equal(home.birthTimeStatus, "reported"); + const homeWindow = declaredBirthWindow(accountRead); + assert.deepEqual([homeWindow?.start_at, homeWindow?.end_at], [interval.start_at, interval.end_at]); + + const report = buildLongformBirthPayload(row, reportOptions); + assert.ok(report); + assert.deepEqual([report.hour, report.minute], clockParts); + assert.deepEqual(report.birth_time_window, { + before_minutes: halfWidths[0], after_minutes: halfWidths[1], source: reportWindowSource, + }); + assert.equal(report.reading_minute_confirmed, false); + assert.equal(report.birth_time_accuracy, "provisional"); + assert.equal(report.time_source, row.birth_time_source); + // Export has no profile lookup. Its input is the already rendered snapshot. + const snapshot = Object.freeze({ + markdown: `## 出生资料\n\n${clock} · ${row.declared_window_start}–${row.declared_window_end}\n`, + }); + const rendered = joinExportBlocks(buildReportExportBlocks({ markdown: snapshot.markdown })); + assert.ok(rendered.includes(`${clock} · ${row.declared_window_start}–${row.declared_window_end}`)); + assert.ok(!rendered.includes(profile.active_birth_time)); + + const annualClock = resolveReportBirthClock(row); + assert.deepEqual([annualClock?.clock.hour, annualClock?.clock.minute], clockParts); + assert.deepEqual(annualBirthWindowBody(annualBirthTime(row)), { + known: true, before_minutes: halfWidths[0], after_minutes: halfWidths[1], + source: row.birth_time_source, rectification: "none", + }); + + const chat = await prepareConsultationRoute({ + userId: "fictional-owner", mode: "verified_chart", loadProfile: async () => row, reserve: async () => null, + loadCandidateRange: async () => assert.fail("retired range must not be loaded"), + }); + assert.equal(chat.consultationMode, consultationMode); + if (row.reported_birth_time === null) { + assert.equal(chat.serverChart, null); + const windowInput = chat.declaredWindow?.toolInput; + assert.deepEqual([windowInput?.rangeStart, windowInput?.rangeEnd], [row.declared_window_start, row.declared_window_end]); + assert.equal(chat.declaredWindow?.truth.wrapsMidnight, true); + } else { + assert.deepEqual([chat.serverChart?.toolInput.hour, chat.serverChart?.toolInput.minute], clockParts); + assert.deepEqual(chat.serverChart?.toolInput.candidate_range, { + start_time: row.declared_window_start, end_time: row.declared_window_end, candidate_intervals: [interval], + }); + } + assert.equal(checksum(stored), before); + } +}); diff --git a/references/real_case_calibration/rect_repro_20261116/README.md b/references/real_case_calibration/rect_repro_20261116/README.md new file mode 100644 index 00000000..22a4f591 --- /dev/null +++ b/references/real_case_calibration/rect_repro_20261116/README.md @@ -0,0 +1,17 @@ +# #272 四臂复跑比较与切片时长审计 + +原归档和冻结脚本保留在 `rect_repro_20261114/`。运行前,用原归档的 `SHA256SUMS` 核对 #272 运行时的脚本、数据、星历和结果。补包文案的口径是 **136 项校验,73 个不同文件**。同一文件在原归档和补包两侧各校验一次时,两次都进入校验项数。 + +用 Git 提取冻结网站提交 `845854ff79ac72c931daa4e34f97f7084e8f35d4`。在独立检出中按原路径放入原归档脚本(仓内 Git 对象 `e6d2d7a4f` 的 `references/real_case_calibration/rect_repro_20261114/`)。使用 Python 3.11 和冻结依赖 `pyswisseph==2.10.3.2`、`tzdata==2026.2`。保持源文件 LF。以下路径示例中的 REPLAY_OUTPUT 和 FROZEN_RUN 指各自的本地路径。 + +```text +python FROZEN_RUN --output REPLAY_OUTPUT --workers 8 +python references/real_case_calibration/rect_repro_20261116/compare.py references/real_case_calibration/rect_repro_20261114/results REPLAY_OUTPUT --mode full +python references/real_case_calibration/rect_repro_20261116/audit_ranking.py FROZEN_RUN references/real_case_calibration/rect_repro_20261114/results --workers 8 +python references/real_case_calibration/rect_repro_20261116/duration_counterexample.py +``` + +- 全量逐臂检查 616 行。键固定为冻结数据集的 77 个案例乘八种偏差,重复键、缺臂、多行和少行均失败。完整汇总逐字段比较。 +- 烟测用单独的输出目录和 `--smoke` 重跑,再显式指定比较器 `--mode smoke`。固定第一个冻结案例的八种偏差和四臂,共 32 行。Windows 和 Linux 用 #272 脚本生成的烟测汇总一致,脚本用这份结果的固定 SHA-256 核对完整烟测汇总。 +- 只有 `questions[].age_option_ranks[].time_slice_seconds` 允许绝对差不超过 0.0005 秒。加 `--exact` 可核对该字段的严格数值相等。题序、年龄排序的获选项与分差、答案、命中和区间仍逐字段相等。 +- 排序审计保留运行时排序。统计已形成的各年龄排名表中相同分差的相邻项,分别报告精确并列和相差不足或等于 1 毫秒的非零差。1 毫秒是相邻两时长各允许 0.5 毫秒漂移时的保守界。只输出聚合指标。 diff --git a/references/real_case_calibration/rect_repro_20261116/audit_ranking.py b/references/real_case_calibration/rect_repro_20261116/audit_ranking.py new file mode 100644 index 00000000..4bf1c0c0 --- /dev/null +++ b/references/real_case_calibration/rect_repro_20261116/audit_ranking.py @@ -0,0 +1,117 @@ +"""Measure near ties in the frozen rank lists without exporting row-level data.""" + +from __future__ import annotations + +import argparse +from collections import Counter +from concurrent.futures import ProcessPoolExecutor +from decimal import Decimal +import hashlib +import importlib.util +import json +from pathlib import Path +import sys + +from compare import ARMS, DURATION_TOLERANCE, OFFSETS + +FROZEN_ENGINE = "845854ff79ac72c931daa4e34f97f7084e8f35d4" +RANK_GAP_LIMIT = 2 * DURATION_TOLERANCE +RANK_ARMS = tuple(arm for arm in ARMS if arm != "without_functional") +replay = None +counts = Counter() +minimum = {} + + +def load_replay(script): + spec = importlib.util.spec_from_file_location("frozen_rect_replay", script) + if spec is None or spec.loader is None: + raise ValueError("could not load the archived replay") + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + if module.WEBSITE != FROZEN_ENGINE or module.ARMS != ARMS or module.OFFSETS != OFFSETS: + raise ValueError("unexpected frozen replay contract") + return module + + +def init_worker(script, expected_hash): + global replay + if hashlib.sha256(script.read_bytes()).hexdigest() != expected_hash: + raise ValueError("unexpected replay script digest") + replay = load_replay(script) + original = replay.BASE_RANKS + + def audited(*args): + groups = original(*args) + arm = replay.ACTIVE_ARM + for group in groups: + counts[arm, "groups"] += 1 + for before, after in zip(group, group[1:]): + if before[0] != after[0]: + continue + counts[arm, "equal_score_pairs"] += 1 + gap = Decimal(str(before[1])) - Decimal(str(after[1])) + if gap == 0: + counts[arm, "exact_duration_ties"] += 1 + else: + minimum[arm] = min(minimum.get(arm, gap), gap) + if gap <= RANK_GAP_LIMIT: + counts[arm, "nonzero_near_ties"] += 1 + return groups + + replay.BASE_RANKS = audited + replay.install_worker_cache() + + +def audit_case(item): + counts.clear() + minimum.clear() + rows = replay.run_case(item) + return len(rows), dict(counts), dict(minimum) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("frozen_run", type=Path) + parser.add_argument("archive", type=Path, help="directory containing the original SHA256SUMS") + parser.add_argument("--workers", type=int, default=4) + args = parser.parse_args() + script = args.frozen_run.resolve() + archived = args.archive / "SHA256SUMS" + label = "references/real_case_calibration/rect_repro_20261114/run.py" + matches = [line.split(" ", 1)[0] for line in archived.read_text(encoding="utf-8").splitlines() + if line.endswith(" " + label)] + if len(matches) != 1 or hashlib.sha256(script.read_bytes()).hexdigest() != matches[0]: + parser.error("use the original #272 replay script matching the archive") + frozen = load_replay(script) + frozen.source_files() + if frozen.sha(frozen.DATA) != frozen.DATA_SHA256: + parser.error("frozen dataset digest mismatch") + source = frozen.read(frozen.DATA) + cases = source["cases"] + if len(cases) != 77: + parser.error("expected the full frozen dataset") + profile = (source["ayanamsa"], source["node_mode"]) + total = Counter() + shortest = {} + rows = 0 + with ProcessPoolExecutor(args.workers, initializer=init_worker, initargs=(script, matches[0])) as pool: + inputs = ((case, profile, ARMS, OFFSETS) for case in cases) + for count, result, gaps in pool.map(audit_case, inputs): + rows += count + total.update(result) + for arm, gap in gaps.items(): + shortest[arm] = min(shortest.get(arm, gap), gap) + fields = ("groups", "equal_score_pairs", "exact_duration_ties", "nonzero_near_ties") + stats = { + arm: {**{field: total[arm, field] for field in fields}, + "shortest_nonzero_gap_seconds": str(shortest[arm]) if arm in shortest else None} + for arm in RANK_ARMS + } + # The frozen replay gives both arms the same cache key and rank calculation. + stats["without_functional"] = {"shares_rank_with": "full"} + print(json.dumps({"rows": rows, "rank_gap_limit_seconds": str(RANK_GAP_LIMIT), "arms": stats}, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/references/real_case_calibration/rect_repro_20261116/compare.py b/references/real_case_calibration/rect_repro_20261116/compare.py new file mode 100644 index 00000000..c01c2229 --- /dev/null +++ b/references/real_case_calibration/rect_repro_20261116/compare.py @@ -0,0 +1,178 @@ +"""Compare complete #272 replay receipts without producing individual data.""" + +from __future__ import annotations + +import argparse +from collections import Counter +from decimal import Decimal +import gzip +import hashlib +import json +from pathlib import Path +from statistics import median + +ARMS = ("full", "without_pd", "without_narayana", "without_functional") +OFFSETS = (-10, -5, -2, -1, 1, 2, 5, 10) +DATA = Path(__file__).resolve().parent.parent / "minute_rectification_holdout_v5.json" +DATA_SHA256 = "ab365a3e3f4953d4b538f2c0889dc98ab89444a28340c3a7b29e2a864cdd687a" +SMOKE_SUMMARY_SHA256 = "cce60cd86e3b493167db35644a993ac9cd041588f589008fda09eeaaa41bc117" +DURATION_TOLERANCE = Decimal("0.0005") + + +def unique_fields(pairs): + result = {} + for key, value in pairs: + if key in result: + raise ValueError("duplicate JSON object field") + result[key] = value + return result + + +def invalid_constant(_): + raise ValueError("non-finite JSON number") + + +def parse_json(data): + return json.loads(data, parse_float=Decimal, parse_constant=invalid_constant, object_pairs_hook=unique_fields) + + +def expected_keys(data, *, smoke): + raw = data.read_bytes() + if hashlib.sha256(raw).hexdigest() != DATA_SHA256: + raise ValueError("frozen dataset digest mismatch") + cases = [entry["case_id"] for entry in parse_json(raw)["cases"]] + if len(cases) != 77 or len(set(cases)) != 77: + raise ValueError("expected 77 unique frozen cases") + return [(case, offset) for case in (cases[:1] if smoke else cases) for offset in OFFSETS] + + +def read_arm(folder, arm, expected): + with gzip.open(folder / f"{arm}.jsonl.gz", "rt", encoding="utf-8") as source: + rows = {} + for line in source: + if len(rows) == len(expected): + raise ValueError(f"{arm}: too many rows") + row = parse_json(line) + if not isinstance(row, dict) or row.get("arm") != arm: + raise ValueError(f"{arm}: wrong arm value") + case = row.get("case_id") + offset = row.get("offset_minutes") + if not isinstance(case, str) or type(offset) is not int: + raise ValueError(f"{arm}: ill-typed case/offset key") + if (case, offset) in rows: + raise ValueError(f"{arm}: duplicate case/offset key") + rows[case, offset] = row + if rows.keys() != expected: + raise ValueError(f"{arm}: expected {len(expected)} rows on the frozen case/offset grid, got {len(rows)}") + return rows + + +def is_duration(path): + return (len(path) == 5 and path[0] == "questions" and isinstance(path[1], int) + and path[2] == "age_option_ranks" and isinstance(path[3], int) and path[4] == "time_slice_seconds") + + +def duration_number(value): + return type(value) in (int, Decimal) and value > 0 + + +def compare_value(archived, replayed, path, durations, tolerance): + if durations is not None and is_duration(path): + if not duration_number(archived) or not duration_number(replayed): + raise ValueError("duration must be a finite positive JSON number") + gap = abs(Decimal(archived) - Decimal(replayed)) + if gap > tolerance: + raise ValueError(f"duration drift {gap} exceeds {tolerance} seconds") + durations.append(gap) + return + if type(archived) is not type(replayed): + raise ValueError(f"field type differs at {path}") + if isinstance(archived, dict): + if archived.keys() != replayed.keys(): + raise ValueError(f"object fields differ at {path}") + for key in archived: + compare_value(archived[key], replayed[key], (*path, key), durations, tolerance) + elif isinstance(archived, list): + if len(archived) != len(replayed): + raise ValueError(f"array length differs at {path}") + for index, (old, new) in enumerate(zip(archived, replayed, strict=True)): + compare_value(old, new, (*path, index), durations, tolerance) + elif archived != replayed: + raise ValueError(f"field value differs at {path}") + + +def duration_stats(durations): + nonzero = sorted(gap for gap in durations if gap) + bins = Counter( + "up_to_1us" if gap <= Decimal("0.000001") + else "up_to_10us" if gap <= Decimal("0.000010") + else "up_to_100us" if gap <= Decimal("0.000100") + else "over_100us" + for gap in nonzero + ) + return { + "values": len(durations), + "different": len(nonzero), + "median_nonzero_seconds": str(median(nonzero)) if nonzero else "0", + "max_seconds": str(nonzero[-1]) if nonzero else "0", + "nonzero_bins": dict(bins), + } + + +def compare(archive, replay, *, mode, data=DATA, tolerance=DURATION_TOLERANCE): + if mode not in {"full", "smoke"}: + raise ValueError("expected the full or smoke contract") + smoke = mode == "smoke" + archive_order = expected_keys(data, smoke=False) + replay_order = expected_keys(data, smoke=True) if smoke else archive_order + replay_keys = set(replay_order) + archive_keys = set(archive_order) + results = {} + for arm in ARMS: + old = read_arm(archive, arm, archive_keys) + new = read_arm(replay, arm, replay_keys) + durations = [] + for index, key in enumerate(replay_order, 1): + try: + compare_value(old[key], new[key], (), durations, tolerance) + except ValueError as error: + raise ValueError(f"{arm}, frozen row {index}: {error}") from None + results[arm] = {"rows": len(new), **duration_stats(durations)} + summary_raw = (replay / "summary.json").read_bytes() + summary = parse_json(summary_raw) + if smoke: + if summary.get("persons") != 1 or tuple(summary.get("offsets_minutes", ())) != OFFSETS: + raise ValueError("smoke summary must describe exactly one case and all eight offsets") + if set(summary.get("arms", {})) != set(ARMS): + raise ValueError("smoke summary missing a fixed arm") + for arm in summary["arms"].values(): + if arm["all"]["trials"] != len(OFFSETS) or set(arm["per_offset"]) != set(map(str, OFFSETS)): + raise ValueError("smoke summary trial count differs from its eight-offset contract") + if any(group["trials"] != 1 for group in arm["per_offset"].values()): + raise ValueError("smoke summary must report one trial per offset") + if hashlib.sha256(summary_raw).hexdigest() != SMOKE_SUMMARY_SHA256: + raise ValueError("smoke summary differs from the frozen 32-row result") + else: + compare_value(parse_json((archive / "summary.json").read_bytes()), summary, ("summary",), None, Decimal(0)) + return {"mode": mode, "rows": sum(arm["rows"] for arm in results.values()), + "summary": "exact_frozen_smoke" if smoke else "exact", + "duration_tolerance_seconds": str(tolerance), "arms": results} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("archive", type=Path) + parser.add_argument("replay", type=Path) + parser.add_argument("--mode", choices=("full", "smoke"), required=True) + parser.add_argument("--exact", action="store_true", help="require exact durations as well") + args = parser.parse_args() + try: + result = compare(args.archive, args.replay, mode=args.mode, + tolerance=Decimal(0) if args.exact else DURATION_TOLERANCE) + except (OSError, ValueError, KeyError, TypeError) as error: + parser.exit(1, f"Replay comparison failed: {error}\n") + print(json.dumps(result, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/references/real_case_calibration/rect_repro_20261116/duration_counterexample.py b/references/real_case_calibration/rect_repro_20261116/duration_counterexample.py new file mode 100644 index 00000000..1336fcfa --- /dev/null +++ b/references/real_case_calibration/rect_repro_20261116/duration_counterexample.py @@ -0,0 +1,29 @@ +"""A one-microsecond change at the existing Narayana boundary conversion.""" + +from datetime import datetime, timedelta +import json +from pathlib import Path +import sys + + +def example(): + root = Path(__file__).resolve().parents[3] + sys.path.insert(0, str(root)) + from scripts.chart_choice.interview import _boundaries + + birth = datetime(2000, 1, 1) + age = 98.25 + twice = 2.0 ** -47 + neighboring_ages = ((age + twice) + twice, age + (twice + twice)) + limit = birth + timedelta(days=365.25 * 140) + lower = birth + timedelta(days=(age - 1) * 365.25) + durations = [] + for candidate in neighboring_ages: + row = {"at": birth, "timeline": [], "narayana": ([{"start_age": candidate, "end_age": candidate + 1}], {})} + upper = min(_boundaries(row, birth, limit)) + durations.append((upper - lower).total_seconds()) + return {"seconds": durations, "delta_microseconds": round((durations[1] - durations[0]) * 1_000_000)} + + +if __name__ == "__main__": + print(json.dumps(example())) diff --git a/tests/test_rectification_replay_compare_20261116.py b/tests/test_rectification_replay_compare_20261116.py new file mode 100644 index 00000000..2d4447da --- /dev/null +++ b/tests/test_rectification_replay_compare_20261116.py @@ -0,0 +1,139 @@ +import gzip +import hashlib +import importlib.util +import json +from decimal import Decimal +from pathlib import Path + +import pytest + + +def load_replay_tool(name): + root = Path(__file__).resolve().parents[1] + path = root / "references/real_case_calibration/rect_repro_20261116" / f"{name}.py" + spec = importlib.util.spec_from_file_location(name, path) + assert spec and spec.loader + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +compare = load_replay_tool("compare") + + +def test_duration_drift_begins_at_boundary_conversion(): + trace = load_replay_tool("duration_counterexample").example() + assert trace == {"seconds": [31557600.0, 31557600.000001], "delta_microseconds": 1} + + +def dump_arm(folder, arm, rows): + with gzip.open(folder / f"{arm}.jsonl.gz", "wt", encoding="utf-8") as stream: + for row in rows: + stream.write(json.dumps(row) + "\n") + + +@pytest.fixture +def receipts(tmp_path, monkeypatch): + cases = [f"synthetic-{index}" for index in range(77)] + data = tmp_path / "synthetic.json" + data.write_text(json.dumps({"cases": [{"case_id": case} for case in cases]}), encoding="utf-8") + monkeypatch.setattr(compare, "DATA_SHA256", hashlib.sha256(data.read_bytes()).hexdigest()) + archive, replay = tmp_path / "archive", tmp_path / "replay" + for folder in (archive, replay): + folder.mkdir() + (folder / "summary.json").write_text('{"fake": {"trials": 2464}}', encoding="utf-8") + + groups = { + arm: [ + { + "arm": arm, "case_id": case, "offset_minutes": offset, + "questions": [{"age_option_ranks": [{"time_slice_seconds": 120.0, "ages": [20, 21]}]}], + } + for case in cases for offset in compare.OFFSETS + ] + for arm in compare.ARMS + } + for arm, rows in groups.items(): + dump_arm(archive, arm, rows) + dump_arm(replay, arm, rows) + return archive, replay, data, groups + + +@pytest.mark.parametrize( + ("change", "error"), + [ + ("missing", "No such file"), + ("empty", "expected 616"), + ("short", "expected 616"), + ("duplicate", "duplicate case/offset"), + ("extra", "too many rows"), + ("wrong_grid", "frozen case/offset grid"), + ], +) +def test_frozen_grid_rejects_missing_or_malformed_arm(receipts, change, error): + archive, replay, data, groups = receipts + arm = "without_pd" + rows = groups[arm] + if change == "missing": + (replay / f"{arm}.jsonl.gz").unlink() + else: + replacements = { + "empty": [], + "short": rows[:-1], + "duplicate": [*rows[:-1], rows[0]], + "extra": [*rows, {**rows[-1], "case_id": "outside-grid"}], + "wrong_grid": [*rows[:-1], {**rows[-1], "case_id": "outside-grid"}], + } + dump_arm(replay, arm, replacements[change]) + with pytest.raises((OSError, ValueError), match=error): + compare.compare(archive, replay, mode="full", data=data) + + +def test_duration_tolerance_is_local_and_configurable(receipts): + archive, replay, data, groups = receipts + rows = groups["full"] + rank = rows[0]["questions"][0]["age_option_ranks"][0] + rank["time_slice_seconds"] += 0.0005 + dump_arm(replay, "full", rows) + outcome = compare.compare(archive, replay, mode="full", data=data) + assert outcome["rows"] == 2464 and outcome["summary"] == "exact" + assert outcome["arms"]["full"]["different"] == 1 + with pytest.raises(ValueError, match="duration drift"): + compare.compare(archive, replay, mode="full", data=data, tolerance=Decimal(0)) + rank["time_slice_seconds"] = 120.000501 + dump_arm(replay, "full", rows) + with pytest.raises(ValueError, match="duration drift"): + compare.compare(archive, replay, mode="full", data=data) + rank["time_slice_seconds"] = 120.0 + rank["ages"][1] = 22 + dump_arm(replay, "full", rows) + with pytest.raises(ValueError, match="field value differs"): + compare.compare(archive, replay, mode="full", data=data) + + +def test_full_summary_has_no_duration_exception(receipts): + archive, replay, data, _ = receipts + summary = replay / "summary.json" + summary.write_text('{"fake": {"trials": 2463}}', encoding="utf-8") + with pytest.raises(ValueError, match="field value differs.*summary"): + compare.compare(archive, replay, mode="full", data=data) + + +def test_smoke_is_a_separate_exact_grid(receipts, monkeypatch): + archive, replay, data, groups = receipts + for arm, rows in groups.items(): + dump_arm(replay, arm, rows[:8]) + smoke_arm = {"all": {"trials": 8}, "per_offset": {offset: {"trials": 1} for offset in compare.OFFSETS}} + summary = {"persons": 1, "offsets_minutes": compare.OFFSETS, "arms": dict.fromkeys(compare.ARMS, smoke_arm)} + summary_path = replay / "summary.json" + summary_path.write_text(json.dumps(summary), encoding="utf-8") + monkeypatch.setattr(compare, "SMOKE_SUMMARY_SHA256", hashlib.sha256(summary_path.read_bytes()).hexdigest()) + assert compare.compare(archive, replay, mode="smoke", data=data)["rows"] == 32 + with pytest.raises(ValueError, match="expected 616"): + compare.compare(archive, replay, mode="full", data=data) + summary_path.write_text(json.dumps({**summary, "unexpected": False}), encoding="utf-8") + with pytest.raises(ValueError, match="smoke summary differs"): + compare.compare(archive, replay, mode="smoke", data=data) + dump_arm(replay, "full", groups["full"][8:16]) + with pytest.raises(ValueError, match="frozen case/offset grid"): + compare.compare(archive, replay, mode="smoke", data=data)