diff --git a/api.py b/api.py index 773a592..b859e3f 100644 --- a/api.py +++ b/api.py @@ -8,6 +8,7 @@ cron 的 docker exec 构建/出计划照旧,互不影响。局域网内部服 GET /plan?date=2026-07-30 指定日期 GET /plan?format=md Markdown 原文(浏览器直接可读) GET /plan/dates 可用日期列表 + GET /plan/check?code=SZ300693 已发布完整计划的逐票核验(不裁剪、不重装配) POST /plan/refresh?date=...&key=... 按 XXL 鉴权排入计划修订,不在请求中重装配 统一任务调度平台(XXL-JOB)触发入口挂在 /api/v1/xxl/*(见 xxl.py,2026-08-03): @@ -20,7 +21,7 @@ import threading import pandas as pd from fastapi import FastAPI, HTTPException, Request -from fastapi.responses import PlainTextResponse +from fastapi.responses import PlainTextResponse, JSONResponse import card import sources @@ -170,7 +171,18 @@ def get_plan(request: Request, date: str | None = None, format: str = "json", if format == "md": return PlainTextResponse(plan.render_md(data), media_type="text/markdown; charset=utf-8") - return data + # 同步路由的工作线程完成编码,避免大计划编码阻塞逐票检查与其他请求。 + return JSONResponse(content=data) + + +@app.get('/plan/check') +def check_plan_stock(code: str): + """采纳专用逐票读取。只读已发布计划,不装配、不查研究库。""" + from common import to_prefix + try: + return JSONResponse(content=plan_versions.stock_check(config.PLAN_SNAPSHOT_DIR, to_prefix(code))) + except (OSError, ValueError, RuntimeError) as e: + raise HTTPException(status_code=503, detail=str(e)) @app.post("/plan/refresh") diff --git a/docs/复盘决定台账.md b/docs/复盘决定台账.md index e8e8c40..e1bfe16 100644 --- a/docs/复盘决定台账.md +++ b/docs/复盘决定台账.md @@ -796,3 +796,12 @@ - **复核日期。** 本轮54项专项、46批本地及候选镜像禁网回归,155模拟部署后按正常调度及自然用户行为核验。188不操作,不制造交易,不额外等待五日。 - **077部署判收。** 功能1df17e6于2026-10-20 11:35:28在155四容器生效。54专项、46批本地及候选镜像禁网回归通过。11:35:55参数与持仓、批次、执行进度逐项相同,对账正常,QMT在线。登录后右侧页面显示正常且零错误。当前无待确认提议,自然采纳未判收。不制造交易或增加五日观察。 + + +## 078 · 2026-10-21 · 采纳逐票核验与确认落库完整性 + +- **日期。** 2026-10-21,北京时间。 +- **改动。** 采纳与后台复核使用只读逐票接口。整榜响应在线程内编码。短暂取数失败拒绝本次采纳并保留准备结果,恢复后原内容不重算。普通建仓的确认状态、动作审计和指令同事务保存,重复确认返回原指令。页面仅在内容标识变化时要求重新确认。 +- **依据。** 盛弘股份09:45请求整份计划超过3秒。155实测响应3093481字节、5.481秒。后台相同取数也超时,建议在准备中与可确认间反复切换。代码补查发现确认先更新状态、再分开写审计与指令,存在中断缺口。 +- **预期。** 逐票请求与完整发布版本一致,不因名次裁剪误撤下,不用旧缓存绕过新研究。确认仍核对当日有效期、资料与固定数量执行条件。故障不产生指令,重试不重复交易。现有交易时段、择时、仓位阈值不变。 +- **复核日期。** 今日桥接口回归、PMS端到端SQL事务测试和47批禁网回归,候选镜像验证后仅部署155。188不动,不代用户采纳。自然用户操作的证据单列。 diff --git a/plan_versions.py b/plan_versions.py index 714610b..5822d79 100644 --- a/plan_versions.py +++ b/plan_versions.py @@ -231,3 +231,38 @@ def publish(root, plan, inputs, params, *, cutoff_at, reason): atomic(morning, {'id': history(root, day)[0]['decision']['id']}) health(root, day, pending_codes=[], error=None, decision_id=did) return p + + +def stock_check(root, code): + """只读完整发布文件的一只股票。不存在与取数失败严格区分;不按展示名次裁剪。""" + if not re.fullmatch(r'(SH|SZ|BJ)\d{6}', code): + raise ValueError('股票代码无效') + for _ in range(3): + day = latest_date(root) + if not day: + raise FileNotFoundError('无已发布计划') + path = Path(root) / f'plan_{day}.json' + health_path = Path(root) / 'versions' / day / 'health.json' + def identity(p): + try: + s = p.stat() + return (s.st_dev, s.st_ino, s.st_size, s.st_mtime_ns, s.st_ctime_ns) + except FileNotFoundError: + return None + before = (identity(path), identity(health_path)) + published = load(root, day) + state = health(root, day) + if (day != latest_date(root) or before != (identity(path), identity(health_path))): + continue + if (published.get('date') != day or not (published.get('decision') or {}).get('id') + or not all(isinstance(published.get(b), list) for b in ('main', 'observe'))): + raise ValueError('完整发布计划不可核验') + rows = {b: [r for r in published[b] if r.get('code') == code] for b in ('main', 'observe')} + count = sum(map(len, rows.values())) + if count > 1: + raise ValueError('完整计划出现重复股票') + return {**{k: published.get(k) for k in ('date', 'schema_version', 'decision', 'traceability', + 'plan_version', 'generated_at', 'rank_axis', 'gate_on')}, + **rows, 'check_code': code, 'found': bool(count), 'refresh_status': state, + 'plan_source': 'snapshot', 'snapshot_generated_at': published.get('generated_at')} + raise RuntimeError('计划正在切换,请稍后重试') diff --git a/test_api_support.py b/test_api_support.py index 80bb10b..719f0b5 100644 --- a/test_api_support.py +++ b/test_api_support.py @@ -24,4 +24,5 @@ except ImportError: sys.modules['fastapi'] = fa resp = types.ModuleType('fastapi.responses') resp.PlainTextResponse = lambda content, **kw: content + resp.JSONResponse = lambda content, **kw: content sys.modules['fastapi.responses'] = resp diff --git a/test_daily_plan_contracts.py b/test_daily_plan_contracts.py index 732af17..0059105 100644 --- a/test_daily_plan_contracts.py +++ b/test_daily_plan_contracts.py @@ -10,7 +10,7 @@ import test_api_support # noqa: F401 def main(): suite = unittest.TestSuite() - for name in ('test_plan_versions', 'test_plan_reviews_live', 'test_plan_review_source'): + for name in ('test_plan_versions', 'test_plan_reviews_live', 'test_plan_review_source', 'test_plan_stock_check'): suite.addTests(unittest.defaultTestLoader.loadTestsFromModule(importlib.import_module(name))) for name in ('test_plan_cache', 'test_plan_snapshot', 'test_plan_rank_axis', 'test_plan_quality_rank', 'test_plan_verdict', 'test_plan_logic_state', diff --git a/test_plan_reviews_live.py b/test_plan_reviews_live.py index 9517498..de34c01 100644 --- a/test_plan_reviews_live.py +++ b/test_plan_reviews_live.py @@ -45,5 +45,25 @@ class PublishedQueriesTest(unittest.TestCase): api._published_stock_rows(['600001.SH'], '2000-01-01', None) + def test_http_shapes_preserve_full_and_single_stock_contracts(self): + import json + from types import SimpleNamespace + def decoded(response): + return json.loads(response.body) if hasattr(response, 'body') else response + with tempfile.TemporaryDirectory() as root, patch.object(config, 'PLAN_SNAPSHOT_DIR', root), \ + patch.object(sources, 'company_reviews', side_effect=AssertionError('查询不得取新研究')), \ + patch.object(plan, 'collect', side_effect=AssertionError('查询不得装配')): + p = pv.publish(root, fixture.sample(), {'x': 1}, {}, cutoff_at=fixture.CUT, reason='晨版') + code = p['main'][0]['code'] + one = decoded(api.check_plan_stock(code)) + full = decoded(api.get_plan(SimpleNamespace(client=SimpleNamespace(host='offline')), + date=fixture.DAY, top=300, obs_top=100, theme_cap=0)) + self.assertEqual(one['decision'], full['decision']) + self.assertEqual(one['main'][0]['company_review'], full['main'][0]['company_review']) + self.assertEqual(one['main'][0]['research_rank'], full['main'][0]['research_rank']) + self.assertEqual(len(one['main']) + len(one['observe']), 1) + self.assertNotIn('candidates', one) + + if __name__ == '__main__': unittest.main() diff --git a/test_plan_stock_check.py b/test_plan_stock_check.py new file mode 100644 index 0000000..191bf95 --- /dev/null +++ b/test_plan_stock_check.py @@ -0,0 +1,53 @@ +"""离线:python -m unittest test_plan_stock_check。无库、无模型、无网络。""" +import copy +import tempfile +import unittest +from unittest.mock import patch +from pathlib import Path +import plan_versions as pv + +class StockCheckTest(unittest.TestCase): + def setUp(self): + self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup);self.root=self.tmp.name + self.day='2026-10-20';self.code='SZ300693' + self.plan={'date':self.day,'schema_version':2,'decision':{'id':self.day+'_'+'a'*64}, + 'main':[{'code':self.code,'rank':501,'research_rank':501,'score':230,'tier':'强传导', + 'company_review':{'thesis':'原研究'},'risk':[]}], 'observe':[]} + self.path=Path(self.root)/('plan_'+self.day+'.json');pv.atomic(self.path,self.plan) + def test_row_beyond_display_cutoff_is_returned_unchanged(self): + r=pv.stock_check(self.root,self.code) + self.assertEqual(r['main'],self.plan['main']);self.assertTrue(r['found']) + self.assertEqual(r['decision'],self.plan['decision']) + def test_missing_is_explicit_and_no_fake_candidate(self): + r=pv.stock_check(self.root,'SZ300001');self.assertFalse(r['found']);self.assertEqual(r['main'],[]) + def test_pending_state_has_no_stale_cache(self): + pv.stock_check(self.root,self.code) + pv.health(self.root,self.day,pending_codes=[self.code],error='source') + self.assertEqual(pv.stock_check(self.root,self.code)['refresh_status']['pending_codes'],[self.code]) + def test_atomic_new_version_is_visible(self): + old=pv.stock_check(self.root,self.code) + self.plan['decision']['id']=self.day+'_'+'b'*64;self.plan['main'][0]['risk']=['changed'] + pv.atomic(self.path,self.plan) + new=pv.stock_check(self.root,self.code) + self.assertNotEqual(new['decision'],old['decision']);self.assertEqual(old['main'][0]['risk'],[]) + def test_change_during_read_retries_instead_of_mixing(self): + original=pv.load;calls=[] + def racing(*a,**kw): + r=original(*a,**kw);calls.append(1) + if len(calls)==1: + self.plan['decision']['id']=self.day+'_'+'b'*64;pv.atomic(self.path,self.plan) + return r + with patch.object(pv,'load',side_effect=racing):r=pv.stock_check(self.root,self.code) + self.assertEqual(len(calls),2);self.assertEqual(r['decision'],self.plan['decision']) + def test_corrupt_missing_legacy_duplicate_fail_closed(self): + for bad in ({},dict(self.plan,decision=None),dict(self.plan,main=None), + dict(self.plan,observe=copy.deepcopy(self.plan['main']))): + pv.atomic(self.path,bad) + with self.assertRaises(ValueError):pv.stock_check(self.root,self.code) + self.path.unlink() + with self.assertRaises(FileNotFoundError):pv.stock_check(self.root,self.code) + def test_invalid_code_rejected(self): + for c in ('../secret','300693',''): + with self.assertRaises(ValueError):pv.stock_check(self.root,c) + +if __name__=='__main__':unittest.main()