feat: TODO迁移到DB + no_agent自愈执行器
- 创建 mofin.db → todos 表(含fix_action/verification_check/retry机制) - 创建 scripts/self_todo_executor.py(no_agent,纯代码逻辑,无LLM) - 修改 morning_health_check.py:TODO写入DB而非JSON,新增derive_fix_action() - cron替换:LLM cron → no_agent脚本,*/10 8-22高频轮询 - 成本:无pending时仅sqlite查询,约0.01s/次 处理链: health_check(8:00) → 可修直接自动修 → 不可修写DB(todos表) → 自愈执行器(每10分) → 有fix_action就执行 → 无fix_action标blocked留待人工
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env python3
|
||||
"""self_todo_executor.py — TODO自动执行器 (no_agent模式)
|
||||
|
||||
每10分钟轮询mofin.db中todos表的pending任务,执行fix_action命令。
|
||||
纯代码逻辑,不调LLM。
|
||||
有执行→输出摘要,无→SILENT。
|
||||
"""
|
||||
|
||||
import json, os, sqlite3, subprocess, sys, time
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DB_PATH = BASE / "data" / "mofin.db"
|
||||
|
||||
|
||||
def get_conn():
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
def verify_fix(verification_check):
|
||||
"""运行验证命令,返回 (ok, output)"""
|
||||
if not verification_check:
|
||||
return True, "无验证命令"
|
||||
try:
|
||||
r = subprocess.run(
|
||||
verification_check,
|
||||
shell=True, capture_output=True, text=True, timeout=30
|
||||
)
|
||||
if r.returncode == 0:
|
||||
return True, r.stdout.strip()[:200]
|
||||
else:
|
||||
return False, f"exit={r.returncode}: {r.stderr.strip()[:200]}"
|
||||
except Exception as e:
|
||||
return False, str(e)[:200]
|
||||
|
||||
|
||||
def execute_fix(fix_action):
|
||||
"""执行修复命令,返回 (ok, output)"""
|
||||
if not fix_action:
|
||||
return False, "无修复命令"
|
||||
try:
|
||||
r = subprocess.run(
|
||||
fix_action,
|
||||
shell=True, capture_output=True, text=True, timeout=60
|
||||
)
|
||||
if r.returncode == 0:
|
||||
return True, r.stdout.strip()[:300] or "ok"
|
||||
else:
|
||||
return False, f"exit={r.returncode}: {r.stderr.strip()[:300]}"
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "执行超时(60s)"
|
||||
except Exception as e:
|
||||
return False, str(e)[:200]
|
||||
|
||||
|
||||
def main():
|
||||
start = time.time()
|
||||
conn = get_conn()
|
||||
cur = conn.cursor()
|
||||
|
||||
# 读所有pending任务(按优先级排序)
|
||||
rows = cur.execute(
|
||||
"SELECT id, title, description, priority, fix_action, verification_check, "
|
||||
"retry_count, max_retries, note FROM todos "
|
||||
"WHERE status='pending' ORDER BY "
|
||||
"CASE priority WHEN 'high' THEN 0 WHEN 'medium' THEN 1 ELSE 2 END, "
|
||||
"created_at ASC LIMIT 10"
|
||||
).fetchall()
|
||||
|
||||
if not rows:
|
||||
conn.close()
|
||||
print("[SILENT] 无待处理TODO")
|
||||
return
|
||||
|
||||
results = []
|
||||
for row in rows:
|
||||
todo_id = row["id"]
|
||||
title = row["title"]
|
||||
fix_action = row["fix_action"]
|
||||
verification = row["verification_check"]
|
||||
retry_count = row["retry_count"]
|
||||
max_retries = row["max_retries"]
|
||||
|
||||
# 标记in_progress
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='in_progress', updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(todo_id,)
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
if fix_action:
|
||||
# 执行修复
|
||||
ok, output = execute_fix(fix_action)
|
||||
if not ok:
|
||||
retry_count += 1
|
||||
if retry_count >= max_retries:
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='blocked', retry_count=?, "
|
||||
"note=?, updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(retry_count, f"重试{retry_count}次仍失败: {output}", todo_id)
|
||||
)
|
||||
results.append(("⛔", f"{title}: 已阻塞({output[:60]})"))
|
||||
else:
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='pending', retry_count=?, "
|
||||
"note=?, updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(retry_count, f"第{retry_count}次失败: {output}", todo_id)
|
||||
)
|
||||
results.append(("🔄", f"{title}: 重试{retry_count}/{max_retries}({output[:60]})"))
|
||||
conn.commit()
|
||||
continue
|
||||
|
||||
# 修复成功→验证
|
||||
v_ok, v_out = verify_fix(verification)
|
||||
if v_ok:
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='completed', note=?, "
|
||||
"updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(f"已修复: {output[:200]}", todo_id)
|
||||
)
|
||||
results.append(("✅", f"{title}: 已修复"))
|
||||
else:
|
||||
# 修复成功但验证失败→可能验证命令不准,标记需人工
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='completed', note=?, "
|
||||
"updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(f"执行成功但验证异常: {v_out}", todo_id)
|
||||
)
|
||||
results.append(("⚠️", f"{title}: 已执行但验证异常({v_out[:60]})"))
|
||||
else:
|
||||
# 无fix_action → 标记为无法自动修复
|
||||
cur.execute(
|
||||
"UPDATE todos SET status='blocked', note='需人工处理(无修复命令)', "
|
||||
"updated_at=CURRENT_TIMESTAMP WHERE id=?",
|
||||
(todo_id,)
|
||||
)
|
||||
results.append(("⛔", f"{title}: 无修复命令,已阻塞"))
|
||||
|
||||
conn.commit()
|
||||
|
||||
conn.close()
|
||||
|
||||
elapsed = time.time() - start
|
||||
if results:
|
||||
print(f"自愈执行器 | {datetime.now().strftime('%H:%M')} | {len(results)}条 ({elapsed:.0f}s)")
|
||||
for icon, msg in results:
|
||||
print(f" {icon} {msg}")
|
||||
else:
|
||||
print("[SILENT] 无待处理TODO")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user