fix: 代码级强制约束——LLM看到数据前必须同步重评(stale/无策略), 不等TODO异步
mofin_collect.py步骤0改造: stale(>4h未更新) → 立即调reassess_with_context()强制重评, 不靠TODO异步 无策略(holding_strategies无记录) → 立即调reassess_with_context()创建策略 只有重评成功后才允许LLM给出建议
This commit is contained in:
+45
-17
@@ -62,33 +62,61 @@ try:
|
||||
hours_stale = 999
|
||||
|
||||
if hours_stale > 4:
|
||||
# 策略过期→创建TODO触发重评
|
||||
stale_count += 1
|
||||
todo_sql = """
|
||||
INSERT OR IGNORE INTO todos
|
||||
(title, code, fix_action, source, priority, status, created_at)
|
||||
VALUES (?, ?, 'reassess_strategy', 'freshness_check', 'high', 'pending', ?)
|
||||
"""
|
||||
cur.execute(todo_sql, (
|
||||
f"策略过期需重评: {code} {name} ({hours_stale:.0f}h未更新)",
|
||||
code,
|
||||
now.isoformat()
|
||||
))
|
||||
print(f" ⚠️ STALE {code} {name}: {hours_stale:.0f}h未更新 → 已创建重评TODO", flush=True)
|
||||
# 强制代码级约束:立即执行重评,不等TODO异步处理
|
||||
# 在LLM看到数据前,策略必须是新鲜的
|
||||
try:
|
||||
from strategy_lifecycle import reassess_with_context
|
||||
result = reassess_with_context(
|
||||
code, name, r["price"],
|
||||
r.get("cost", 0), r.get("shares", 0),
|
||||
r["action"] or ""
|
||||
)
|
||||
if result and result.get("action"):
|
||||
print(f" 🔄 FORCE_REASSESS {code} {name}: {hours_stale:.0f}h过期→已立即重评→{result['action'][:60]}", flush=True)
|
||||
else:
|
||||
print(f" ⚠️ FORCE_REASSESS {code} {name}: 重评返回空结果", flush=True)
|
||||
except Exception as e:
|
||||
print(f" ❌ FORCE_REASSESS {code} {name} 失败: {e}", flush=True)
|
||||
# 创建TODO作为兜底
|
||||
todo_sql = """
|
||||
INSERT OR IGNORE INTO todos
|
||||
(title, code, fix_action, source, priority, status, created_at)
|
||||
VALUES (?, ?, 'reassess_strategy', 'freshness_check', 'high', 'pending', ?)
|
||||
"""
|
||||
cur.execute(todo_sql, (
|
||||
f"策略过期需重评: {code} {name} ({hours_stale:.0f}h未更新)",
|
||||
code,
|
||||
now.isoformat()
|
||||
))
|
||||
print(f" ⚠️ STALE {code} {name}: {hours_stale:.0f}h未更新 → 已强制重评", flush=True)
|
||||
else:
|
||||
fresh_count += 1
|
||||
print(f" ✅ FRESH {code} {name}: {hours_stale:.1f}h前更新", flush=True)
|
||||
else:
|
||||
no_strategy_count += 1
|
||||
print(f" ❌ NO_STRATEGY {code} {name}: 无策略记录", flush=True)
|
||||
|
||||
# 无策略→立即执行重评创建策略(代码级约束:不允许无策略就输出建议)
|
||||
try:
|
||||
from strategy_lifecycle import reassess_with_context
|
||||
result = reassess_with_context(
|
||||
code, name, r["price"],
|
||||
r.get("cost", 0), r.get("shares", 0),
|
||||
""
|
||||
)
|
||||
if result and result.get("action"):
|
||||
print(f" 🔄 CREATE_STRATEGY {code} {name}: 无策略→已创建→{result['action'][:60]}", flush=True)
|
||||
else:
|
||||
print(f" ⚠️ CREATE_STRATEGY {code} {name}: 重评返回空", flush=True)
|
||||
except Exception as e:
|
||||
print(f" ❌ CREATE_STRATEGY {code} {name} 失败: {e}", flush=True)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
total = len(rows)
|
||||
print(f"策略检查完成: {total}只持仓, {fresh_count}新鲜, {stale_count}过期(已创TODO), {no_strategy_count}无策略", flush=True)
|
||||
print(f"策略检查完成: {total}只持仓, {fresh_count}新鲜, {stale_count}过期(已强制重评), {no_strategy_count}无策略(已创建)", flush=True)
|
||||
if stale_count > 0 or no_strategy_count > 0:
|
||||
print(f"⚠️ LLM注意: 有{stale_count + no_strategy_count}只持仓策略不是最新状态, 禁止对这些股给出操作建议", flush=True)
|
||||
print(f"⚠️ 重评完成: {stale_count + no_strategy_count}只已强制刷新, LLM可基于最新策略给出建议", flush=True)
|
||||
except Exception as e:
|
||||
print(f"WARN: strategy_freshness_check跳过 ({e})", flush=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user