cleanup: 执行器失败带完整上下文调gateway API

- 升降级信息含完整description(体检原因/分类/详情)
- 含尝试的修复命令和失败输出
- gateway API是新session,有完整上下文才能分析
This commit is contained in:
知微
2026-06-24 21:41:14 +08:00
parent 526fcc4412
commit 629f154829
+34 -22
View File
@@ -2,7 +2,7 @@
"""self_todo_executor.py — TODO自动执行器 (no_agent模式)
每10分钟轮询pending TODOs,执行fix_action。
成功→completed。失败→直接调Hermes Gateway让知微处理。
成功→completed。失败→调gateway API,带完整上下文让知微处理。
"""
import json, sqlite3, subprocess, time, urllib.request
@@ -13,7 +13,6 @@ 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():
@@ -22,7 +21,7 @@ def main():
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT id, title, fix_action FROM todos WHERE status='pending' "
"SELECT id, title, description, 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()
@@ -35,13 +34,15 @@ def main():
for row in rows:
tid = row["id"]
title = row["title"]
desc = row["description"] or ""
fix = row["fix_action"]
conn.execute("UPDATE todos SET status='in_progress' WHERE id=?", (tid,))
conn.commit()
if not fix:
msg = f"[自愈执行器] 需处理: {title} (无自动修复方案)"
# 无修复方案 → 带完整描述调gateway
context = f"[自愈执行器] 系统体检发现以下问题,无自动修复方案,需分析处理。\n\n问题: {title}\n\n详情:\n{desc}".strip()
else:
# 执行修复命令
try:
@@ -52,35 +53,46 @@ def main():
conn.commit()
print(f"{title}: 已修复")
continue
output = r.stderr.strip()[:200] or r.stdout.strip()[:200]
output = r.stderr.strip()[:500] or r.stdout.strip()[:500]
except subprocess.TimeoutExpired:
output = "执行超时"
output = "执行超时(30s)"
except Exception as e:
output = str(e)[:200]
msg = f"需处理: {title} (尝试失败: {output})"
# 修复失败→发到核心群让知微处理(知微在群里,我收到了就处理)
context = (
f"[自愈执行器] 尝试自动修复失败,需知微分析处理。\n\n"
f"问题: {title}\n\n"
f"详情:\n{desc}\n\n"
f"尝试的修复命令: {fix}\n"
f"失败输出: {output}\n\n"
f"请分析失败原因并完成修复,完成后标记TODO #{tid} 为 completed。"
)
# 调gateway让知微处理(带完整上下文)
payload = json.dumps({
"model": "default",
"messages": [{"role": "user", "content": context}],
"max_tokens": 1000,
}).encode()
req = urllib.request.Request(GATEWAY_URL, data=payload,
headers={"Content-Type": "application/json",
"Authorization": f"Bearer {GATEWAY_KEY}"})
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]})")
resp = urllib.request.urlopen(req, timeout=120)
reply = json.loads(resp.read())
result = reply["choices"][0]["message"]["content"][:500]
conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
(f"知微已处理: {result[:200]}", tid))
print(f" 🔶 {title}: 已处理")
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]})")
(f"调用知微失败: {str(e)[:100]},下次再试", tid))
print(f" ⚠️ {title}: 调用失败,下次再试")
conn.commit()
conn.close()
if rows:
print(f"自愈执行器 | {datetime.now().strftime('%H:%M')} | {len(rows)}条 ({time.time()-start:.0f}s)")
else: