Files
MoFin/scripts/self_todo_executor.py
T
知微 526fcc4412 fix: 执行器失败直接发核心群,不走API
- 执行器修不了→hermes send到核心群,知微在群里收到就处理
- Dad也在核心群,能看到
- 不发gateway API(上下文隔离,Dad看不到)
- 无fix_action也走同一路径
2026-06-24 21:38:36 +08:00

92 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""self_todo_executor.py — TODO自动执行器 (no_agent模式)
每10分钟轮询pending TODOs,执行fix_action。
成功→completed。失败→直接调Hermes Gateway让知微处理。
"""
import json, sqlite3, subprocess, time, urllib.request
from pathlib import Path
from datetime import datetime
BASE = Path("/home/hmo/MoFin")
DB_PATH = BASE / "data" / "mofin.db"
GATEWAY_URL = "http://localhost:8643/v1/chat/completions"
GATEWAY_KEY = "hermes123"
COREGROUP = "coregroup@conference.yoin.fun"
def main():
start = time.time()
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT id, title, fix_action FROM todos WHERE status='pending' "
"ORDER BY CASE priority WHEN 'high' THEN 0 WHEN 'medium' THEN 1 ELSE 2 END, "
"created_at ASC LIMIT 3"
).fetchall()
if not rows:
conn.close()
print("[SILENT] 无待处理TODO")
return
for row in rows:
tid = row["id"]
title = row["title"]
fix = row["fix_action"]
conn.execute("UPDATE todos SET status='in_progress' WHERE id=?", (tid,))
conn.commit()
if not fix:
msg = f"[自愈执行器] 需处理: {title} (无自动修复方案)"
else:
# 执行修复命令
try:
r = subprocess.run(fix, shell=True, capture_output=True, text=True, timeout=30)
if r.returncode == 0:
conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
(f"已修复: {r.stdout.strip()[:200]}", tid))
conn.commit()
print(f"{title}: 已修复")
continue
output = r.stderr.strip()[:200] or r.stdout.strip()[:200]
except subprocess.TimeoutExpired:
output = "执行超时"
except Exception as e:
output = str(e)[:200]
msg = f"需处理: {title} (尝试失败: {output})"
# 修复失败→发到核心群让知微处理(知微在群里,我收到了就处理)
try:
# 用hermes send发到核心群
send_cmd = ["hermes", "send", "--to", f"xmpp:{COREGROUP}", msg]
r = subprocess.run(send_cmd, capture_output=True, text=True, timeout=30)
if r.returncode == 0:
conn.execute("UPDATE todos SET status='completed', note='已发到核心群通知知微' WHERE id=?",
(tid,))
print(f" 🔶 {title}: 已发到核心群")
else:
conn.execute("UPDATE todos SET status='pending', note=? WHERE id=?",
(f"发核心群失败: {r.stderr[:100]}", tid))
print(f" ⚠️ {title}: 发核心群失败({r.stderr[:60]})")
except Exception as e:
conn.execute("UPDATE todos SET status='pending', note=? WHERE id=?",
(f"发核心群异常: {str(e)[:100]}", tid))
print(f" ⚠️ {title}: 发核心群异常({str(e)[:60]})")
conn.commit()
conn.close()
if rows:
print(f"自愈执行器 | {datetime.now().strftime('%H:%M')} | {len(rows)}条 ({time.time()-start:.0f}s)")
else:
print("[SILENT] 无待处理TODO")
if __name__ == "__main__":
main()