diff --git a/src/quant_studio/decision_summary.py b/src/quant_studio/decision_summary.py new file mode 100644 index 0000000..e5a447c --- /dev/null +++ b/src/quant_studio/decision_summary.py @@ -0,0 +1,76 @@ +"""Read evidence status without converting successful runs into research gates.""" + +import json +from html import escape + +from quant_studio import QuantStudioError +from quant_studio.result_integrity import verify_result +from quant_studio.run_record import load_run_result +from quant_studio.runner import safe_run_file + +OBJECTIVE_LABELS = { + "meets_objective": "目标点估计满足", + "failed": "目标未满足", + "insufficient_evidence": "目标证据不足", +} + + +def evidence_summary(directory): + result = load_run_result(directory) + if result.get("status") != "succeeded": + return ["未完成或失败的实验;不计入验收"], [] + try: + verify_result(directory, result) + objectives, provisional = [], False + for name in sorted(result["output_hashes"]): + if not name.endswith("/result.json"): + continue + value = json.loads(safe_run_file(directory, name).read_text("utf-8")) + if value.get("schema_version") != "quant.research-result/v1": + continue + provisional |= value.get("evidence_state") == "provisional" + provisional |= value.get("provisional") is True + evaluation = value.get("objective_evaluation") or {} + label = OBJECTIVE_LABELS.get(evaluation.get("status"), "目标未评审") + objectives.append(f"{value.get('candidate', name)}:{label}") + provisional |= result.get("evidence_state") == "provisional" + provisional |= result.get("provisional") is True + label = ( + "临时证据(provisional);不计入 A/B/C 验收" + if provisional + else "产物完整性已核验;经济有效性及未来门槛另行评审" + ) + return [label], objectives + except (OSError, ValueError, TypeError, AttributeError, QuantStudioError): + return ["证据未核验;不计入验收"], [] + + +def gate_a_summary(account, daily): + if not account: + return "未关联模拟账户" + if not daily or not daily.get("operations"): + return "当前原生运行状态未核实" + native = daily.get("account") or {} + if any( + native.get(key) != account.get(key) + for key in ("account_id", "definition_sha256") + ): + return "当前运行账户与决策所关联的定义不一致" + acceptance = daily["operations"].get("acceptance") or {} + count = acceptance.get("longest_consecutive") + if type(count) is not int or count < 0: + return "自然交易日凭证未核实" + checked = daily.get("checked_at") or "时间未提供" + return f"最长连续自然成功 {count}/6;核验时间 {checked};不代表策略有效" + + +def summary_body(root, record, daily): + review = record.get("next_review_date") or "尚未安排" + body = f"
下次复核:{escape(review)}
" + body += f"Gate A:{escape(gate_a_summary(record.get('account'), daily))}
" + for ref in record["evidence"]: + labels, objectives = evidence_summary(root / ref["run_id"]) + text = ";".join(labels + (objectives or ["未提供原生目标评审结果"])) + body += f"实验 {escape(ref['run_id'])}:{escape(text)}
" + body += "Gate B/C 不由运行成功或点估计满足自动判定。
" + return body diff --git a/src/quant_studio/findings.py b/src/quant_studio/findings.py index e2d01db..6d8fa97 100644 --- a/src/quant_studio/findings.py +++ b/src/quant_studio/findings.py @@ -3,6 +3,7 @@ from html import escape from quant_studio import QuantStudioError +from quant_studio.decision_summary import summary_body from quant_studio.projects import ProjectStore from quant_studio.research_cards import ACTIONS, ROLES, VERDICTS, DecisionStore @@ -23,6 +24,7 @@ def findings_body(root, query): """ projects, decisions = ProjectStore(root), DecisionStore(root) + daily, daily_read = None, False count = 0 for current in projects.list(): records = decisions.list(current["id"]) @@ -44,6 +46,15 @@ def findings_body(root, query): for ref in record["evidence"] ) href = f"/projects/{project['id']}?revision={project['revision']}" + if record.get("account") and not daily_read: + from quant_studio.etf_daily import inspect_daily + + daily_read = True + try: + daily = inspect_daily() + except (QuantStudioError, OSError, ValueError): + daily = None + summary = summary_body(projects.runs_root, record, daily) body += f'''{VERDICTS[record["verdict"]]} · {ACTIONS[record["action"]]}
@@ -51,6 +62,7 @@ def findings_body(root, query):{ROLES[card["sample_role"]]} · {escape(card["start"])} 至 {escape(card["end"])}
否定条件:{escape(card["rejection_rule"])}
结论依据:{escape(record["rationale"])}
+{summary}记录时间:{escape(record["recorded_at"])}
尚无符合条件的研究结论。
引用实验:
''' @@ -116,6 +117,7 @@ def card_post(root, identifier, revision, action, data): values.update( candidate=field(data, "candidate", ""), account_id=field(data, "account_id", ""), + next_review_date=field(data, "next_review_date", ""), evidence=data.pop("evidence", []), ) if data: diff --git a/src/quant_studio/research_cards.py b/src/quant_studio/research_cards.py index 7592b88..c50c762 100644 --- a/src/quant_studio/research_cards.py +++ b/src/quant_studio/research_cards.py @@ -87,6 +87,7 @@ def append( evidence, candidate="", account_id="", + next_review_date="", ): project = self.projects.get(project_id) if project["revision"] != revision: @@ -95,6 +96,15 @@ def append( raise QuantStudioError("请先登记研究卡") if verdict not in VERDICTS or action not in ACTIONS: raise QuantStudioError("研究结论或后续行动无效") + if not isinstance(next_review_date, str): + raise QuantStudioError("下次复核日期须为 YYYY-MM-DD") + if next_review_date: + try: + parsed = date.fromisoformat(next_review_date) + if parsed.isoformat() != next_review_date: + raise ValueError("noncanonical date") + except (ValueError, TypeError) as exc: + raise QuantStudioError("下次复核日期须为 YYYY-MM-DD") from exc if ( not isinstance(rationale, str) or not rationale.strip() @@ -173,6 +183,7 @@ def append( "evidence": refs, "candidate": candidate, "account": account, + "next_review_date": next_review_date, } record["sha256"] = digest(record) directory = self.root / project_id / "decisions" diff --git a/tests/test_decision_summary.py b/tests/test_decision_summary.py new file mode 100644 index 0000000..7853062 --- /dev/null +++ b/tests/test_decision_summary.py @@ -0,0 +1,57 @@ +import hashlib +import json + +import pytest + +from quant_studio.decision_summary import evidence_summary, gate_a_summary + + +@pytest.mark.parametrize("provisional", [True, False]) +def test_native_point_estimate_never_grants_research_acceptance(tmp_path, provisional): + path = tmp_path / "strategy-output/family/base/result.json" + path.parent.mkdir(parents=True) + path.write_text( + json.dumps( + { + "schema_version": "quant.research-result/v1", + "candidate": "base", + "provisional": provisional, + "objective_evaluation": {"status": "meets_objective"}, + } + ) + ) + record = { + "status": "succeeded", + "output_hashes": { + path.relative_to(tmp_path).as_posix(): hashlib.sha256( + path.read_bytes() + ).hexdigest() + }, + } + (tmp_path / "result.json").write_text(json.dumps(record)) + labels, objectives = evidence_summary(tmp_path) + assert objectives == ["base:目标点估计满足"] + assert ("provisional" in labels[0]) == provisional + assert "不计入" in labels[0] if provisional else "另行评审" in labels[0] + path.write_text(path.read_text() + " ") + assert evidence_summary(tmp_path) == (["证据未核验;不计入验收"], []) + + +def test_unverified_success_does_not_become_validated_evidence(tmp_path): + (tmp_path / "result.json").write_text(json.dumps({"status": "succeeded"})) + assert evidence_summary(tmp_path) == (["证据未核验;不计入验收"], []) + + +def test_gate_a_is_bound_to_account_definition_and_native_counter(): + account = {"account_id": "old", "definition_sha256": "frozen"} + native = { + "account": dict(account), + "checked_at": "2026-10-22T12:00:00+08:00", + "operations": {"acceptance": {"longest_consecutive": 0}}, + } + assert "0/6" in gate_a_summary(account, native) + native["operations"]["acceptance"]["longest_consecutive"] = True + assert "未核实" in gate_a_summary(account, native) + native["account"]["definition_sha256"] = "replacement" + assert "不一致" in gate_a_summary(account, native) + assert "未核实" in gate_a_summary(account, None) diff --git a/tests/test_findings.py b/tests/test_findings.py index 1382344..8bcba74 100644 --- a/tests/test_findings.py +++ b/tests/test_findings.py @@ -32,6 +32,7 @@ def test_findings_retain_rejected_hypothesis_revision_and_check_evidence(tmp_pat action="stop_research", rationale="negative evidence", evidence=[result.run_id], + next_review_date="2026-10-29", ) store.save( "new title", @@ -45,6 +46,8 @@ def test_findings_retain_rejected_hypothesis_revision_and_check_evidence(tmp_pat assert "old hypothesis" in page and "new hypothesis" not in page assert "<study>" in page and "