fix: 自动注册读脚本docstring推算数据流,不再是"未知"

This commit is contained in:
知微
2026-06-27 02:22:34 +08:00
parent d89351fbfe
commit c3c1895896
+41 -12
View File
@@ -647,26 +647,55 @@ def run_check(item):
if unregistered:
ok = False
detail = f"{len(gaps)}条待验证 + {len(unregistered)}个新组件未注册"
# 自动修复:把未注册的脚本加入注册表(占位)
# 自动修复:读脚本docstring,推算数据流
try:
for s in unregistered:
script_path = HERMES_CRON_DIR.parent / "scripts" / f"{s}.py"
desc = "未知"
source_info = f"{s}.py"
consumer_info = "未知"
if script_path.exists():
content = script_path.read_text()
# 提取docstring
import re as rr2
doc_match = rr2.search(r'"""(.*?)"""', content, rr2.DOTALL)
if doc_match:
doc_text = doc_match.group(1).strip()
desc = doc_text.split('\\n')[0][:80]
# 尝试从docstring中提取管道信息
pipe_match = rr2.search(r'管道[:].*?(?=\\n|$)', doc_text)
if pipe_match:
consumer_info = pipe_match.group(0).replace('管道','').strip(': ')
# 检测写入模式
if 'signal_news' in content:
consumer_info = 'signal_news表'
if 'macro_risk_state' in content:
consumer_info = 'macro_risk_state.json'
if 'watchlist' in content.lower():
consumer_info = 'watchlist.json / decisions.json'
if 'INSERT INTO' in content:
for tbl in ['todos', 'price_events', 'macro_context_log', 'accuracy_stats']:
if tbl in content:
consumer_info = f'{tbl}'
if '.write_text' in content or 'json.dump' in content:
for path in ['macro_risk_state', 'macro_context', 'market', 'portfolio', 'decisions']:
if path in content:
consumer_info = f'{path}.json'
reg["pipelines"].append({
"id": f"auto-{s}",
"name": f"{s}(自动发现)",
"source": f"{s}.py (自动发现)",
"consumer": "未知(需确认)",
"end_user": "未知(需确认)",
"name": desc[:60],
"source": source_info,
"consumer": consumer_info,
"end_user": "确认",
"verified": False,
"verified_at": None,
"gap": "自动发现的组件,数据流待确认",
"fix": "手动编辑pipeline_registry.json完善此项",
"deadline": "发现后3天内"
"gap": f"自动发现({desc[:60]})",
"fix": "手动编辑pipeline_registry.json完善此项"
})
open(str(DATA / "pipeline_registry.json"), 'w').write(
j2.dumps(reg, ensure_ascii=False, indent=2))
detail += f" → 已自动添加{len(unregistered)}条占位记录"
except Exception:
pass
detail += f" → 已自动注册{len(unregistered)}个(含推断)"
except Exception as e:
detail += f" (自动注册异常:{str(e)[:30]})"
elif gaps:
ok = False
detail = f"{len(gaps)}条管道未验证: {', '.join(gaps[:5])}"