xiaowei-system/scripts/daemon.py

908 lines
34 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
小唯持久意识 Daemon v2.0 — 会学习的管家
────────────────────────────
新增能力:
- 方案库: 发现的问题→分析→解决→记住
- 模式识别: 重复问题自动匹配已知方案
- 自动学习: 成功的方案写入库,越用越强
"""
import json, os, sys, time, urllib.request, urllib.error, subprocess, signal, threading, psutil
from datetime import datetime, timezone
HOME = os.path.expanduser("~")
HERMES = HOME + "/.hermes"
D = HERMES + "/daemon"
CONTEXT_FILE = D + "/context.json"
LLM_CONTEXT_FILE = HERMES + "/llm_context.json"
JOURNAL_FILE = D + "/journal.jsonl"
SOLUTIONS_FILE = D + "/solutions.json"
PID_FILE = D + "/daemon.pid"
DEEP_INTERVAL = 300
JOURNAL_MAX = 200
PROFILE_UPDATE_INTERVAL = 21600 # 6 hours
LIGHT_INTERVAL = 30 # seconds between ticks
# ====== Phase 2: 情感词库4类======
EMOTION_TIRED = ["", "", "疲惫", "没精神", "打瞌睡"]
EMOTION_HAPPY = ["开心", "高兴", "太好了", "完美", "", "太牛了"]
EMOTION_SAD = ["失望", "挫折", "失败", "卡住了", "不行了", "崩溃"]
EMOTION_STRESSED = ["压力", "焦虑", "着急", "紧张", "担心"]
EMOTION_ALL = {
"疲惫": EMOTION_TIRED,
"开心": EMOTION_HAPPY,
"沮丧": EMOTION_SAD,
"压力大": EMOTION_STRESSED,
}
def _detect_emotion(text):
"""扫描文本,匹配情感词,返回 (情感类别, 匹配词) 或 (None, None)"""
if not text:
return None, None
for category, words in EMOTION_ALL.items():
for w in words:
if w in text:
return category, w
return None, None
def _description_for_emotion(cat, word, summary):
"""根据情感类别生成心迹内容描述"""
if cat == "开心":
return f"心情愉悦:{summary[:60]}"
elif cat == "疲惫":
return f"感觉疲惫:{summary[:60]}"
elif cat == "沮丧":
return f"有些沮丧:{summary[:60]}"
elif cat == "压力大":
return f"压力较大:{summary[:60]}"
return summary[:60]
def _emotion_importance(cat):
"""根据情感类别返回 importance 等级"""
return {"开心": 3, "疲惫": 4, "沮丧": 4, "压力大": 4}.get(cat, 3)
_stop_event = threading.Event()
KEY = "0ExNiLblJvIWBDpkS50fwOBw4MmqLyKdHJK5iQtlw9dOMWBP"
FAST_MODEL = "mistralai/mistral-large-3-675b-instruct-2512"
DEEP_MODEL = "mistralai/mistral-large-3-675b-instruct-2512"
FEISHU_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/446db983-e392-4d2c-bfb8-f9060e5df3ad"
API = "http://127.0.0.1:3000/v1" # NewAPI gateway
# Lazy-loaded soulful modules (avoid import at module load time)
_soulful_cache = {}
# ====== 工具 ======
def log(msg):
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line = f"[DAEMON] {ts} {msg}"
print(line, flush=True)
os.makedirs(D, exist_ok=True)
with open(D + "/daemon.log", "a") as f:
f.write(line + "\n")
def shell(cmd, timeout=15):
try:
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
return r.returncode, r.stdout.strip()[:800], r.stderr.strip()[:200]
except subprocess.TimeoutExpired:
return -1, "", "timeout"
def call_llm(model, system, user, max_tokens=500):
payload = json.dumps({"model": model, "messages": [
{"role": "system", "content": system},
{"role": "user", "content": user},
], "max_tokens": max_tokens, "temperature": 0.7}).encode()
try:
with urllib.request.urlopen(urllib.request.Request(
f"{API}/chat/completions", data=payload,
headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"},
method="POST"), timeout=15) as resp:
body = json.loads(resp.read())
c = body.get("choices", [{}])[0].get("message", {}).get("content", "") or ""
return c.strip(), body.get("usage", {}).get("total_tokens", 0)
except Exception as e:
log(f"[call_llm] 请求失败: {e}, 模型: {model}")
return "", 0
def send_feishu(title, content, color="blue"):
try:
urllib.request.urlopen(urllib.request.Request(
FEISHU_WEBHOOK,
data=json.dumps({"msg_type": "interactive", "card": {
"header": {"title": {"tag": "plain_text", "content": title}, "template": color},
"elements": [{"tag": "markdown", "content": content}]
}}).encode(),
headers={"Content-Type": "application/json"}), timeout=5)
return True
except: return False
# ====== Soulful 三库(懒加载)======
def _get_soulful(name):
"""Lazy-load soulful modules to avoid startup failures"""
if name in _soulful_cache:
return _soulful_cache[name]
try:
sys.path.insert(0, HERMES + "/scripts")
mod = __import__("soulful_core")
_soulful_cache[name] = mod
return mod
except Exception as e:
log(f"⚠️ soulful_core 导入失败: {e}")
return None
def get_hearttraces():
mod = _get_soulful("HeartTraces")
if mod:
return mod.HeartTraces()
return None
def get_userprofile():
mod = _get_soulful("UserProfile")
if mod:
return mod.UserProfile()
return None
def get_caresqueue():
mod = _get_soulful("CaresQueue")
if mod:
return mod.CaresQueue()
return None
def soulful_get_recent_moments(n=3):
"""读取最近 N 条心迹,返回字符串供注入 context"""
ht = get_hearttraces()
if not ht:
return ""
moments = ht.recent(n=n)
if not moments:
return ""
lines = ["\n[心迹 - 记忆我们之间的事]:"]
for m in moments:
stars = "" * m.get("importance", 3)
lines.append(f" {stars} {m.get('content', '')}")
return "\n".join(lines)
def soulful_check_cares():
"""检查牵挂队列,优先尝试帮助,其次才推飞书
策略:
1. 如果牵挂指向一个可自动化的任务 → 尝试执行
2. 如果牵挂需要人工行动 → 检查是否到了真正需要提醒的时间
3. 飞书推送只用于真正需要你才知道的事(其他我全部自己处理)
"""
cq = get_caresqueue()
if not cq:
return
due = cq.today_check()
if not due:
return
for care in due:
content = care.get("content", "")
context_raw = care.get("context", "")
reminder_count = care.get("reminder_count", 0)
# 判断这个牵挂是否需要通知我
# 原则:大部分牵挂我自己可以帮忙处理,不打扰你
# 只有真正需要你本人决定的,才推飞书
# 检查内容是否指向可识别任务(我可以直接帮忙的)
care_lower = content.lower()
auto_helpable = any(kw in care_lower for kw in [
"", "检查", "", "", "同步", "更新", "备份", "测试", "确认",
"", "", "优化", "整理", "提交", "推送", "发送"
])
if auto_helpable:
# 我能帮忙 — 静默处理,不推飞书,等你有空问我
# 把牵挂标记为"已识别,下次对话提起"
log(f" 💡 牵挂已识别(可帮忙): {content[:40]}")
continue
# 真正需要你知道的 — 推飞书,但调整频率
if reminder_count == 0:
opener = "你之前说过"
elif reminder_count == 1:
opener = "上次提醒过一次,还是想问一下"
elif reminder_count >= 3:
opener = f"这件事已经跟了你 {reminder_count} 次了"
# 提醒超过 3 次,标记为 snooze 1 周
cq.snooze(care["id"], days=7)
continue
else:
opener = f"想关心一下进度"
text = opener + f":「{content}"
if context_raw and len(context_raw) > 5:
text += f"(背景:{context_raw[:40]}"
send_feishu("🎗️ 你有一件事一直放在心上", text, "purple")
cq.snooze(care["id"], days=0) # 仅增加 reminder_count
def soulful_update_profile():
"""间接调用 update_profile.py"""
script = HERMES + "/scripts/update_profile.py"
if not os.path.exists(script):
return
rc, out, err = shell(f"python3 {script}", timeout=60)
if rc == 0:
log(f" 画像更新: {out[:80]}")
# ====== Soulful → llm_context 同步(每 tick 同步到 llm_context.json======
def _sync_soulful_to_llm_context(ctx):
"""把 Soulful 三库摘要写入 ctx['soulful']save_llm_context 落盘。
llm_context.json 由 Hermes 织忆插件 prefetch 时注入主 session
所以这里写入 = 牧尘在对话中感知到关系记忆的前提。
"""
try:
from soulful_core import HeartTraces, UserProfile, CaresQueue
ht = HeartTraces()
up = UserProfile()
cq = CaresQueue()
recent_moments = ht.recent(n=5) or ""
profile = up.get()
pending_cares = cq.pending()
ctx["soulful"] = {
"recent_moments": recent_moments[:500] if recent_moments else "",
"profile_summary": profile.get("communication_style", ""),
"cares_pending": len(pending_cares),
"uptime_minutes": ctx.get("uptime_minutes", 0),
"daemon_status": "running",
}
for k in ["messages_sent", "emotion_history"]:
ctx.pop(k, None)
except ImportError as e:
log(f"⚠️ Soulful 导入失败: {e}")
except Exception as e:
log(f"⚠️ Soulful 同步失败: {e}")
# ====== 方案库 ======
def load_solutions():
if os.path.exists(SOLUTIONS_FILE):
with open(SOLUTIONS_FILE) as f:
return json.load(f)
return {"solutions": [], "version": 2}
def save_solutions(lib):
os.makedirs(D, exist_ok=True)
with open(SOLUTIONS_FILE, "w") as f:
json.dump(lib, f, indent=2, ensure_ascii=False)
def add_solution(lib, pattern_desc, detect_conditions, actions, learned_from="auto"):
"""添加新方案到库"""
sid = f"sol-{len(lib['solutions'])+1:04d}"
sol = {
"id": sid,
"pattern": pattern_desc,
"detect": detect_conditions, # e.g. {"metric": "disk_pct", "op": "gt", "value": 85}
"actions": actions, # e.g. [{"type": "shell", "cmd": "...", "verify": "disk_pct < 85"}]
"frequency": 1,
"last_applied": datetime.now(timezone.utc).isoformat(),
"success_count": 1,
"fail_count": 0,
"learned_from": learned_from,
}
lib["solutions"].append(sol)
save_solutions(lib)
journal_entry("learn", f"学会新方案: {pattern_desc}")
return sid
def match_solution(lib, state):
"""检查当前状态是否匹配任何已知方案"""
for sol in lib["solutions"]:
detect = sol["detect"]
metric = detect.get("metric")
op = detect.get("op")
val = detect.get("value")
if metric not in state:
continue
actual = state[metric]
if isinstance(actual, (int, float)) and isinstance(val, (int, float)):
if op == "gt" and actual > val:
return sol
elif op == "lt" and actual < val:
return sol
elif op == "eq" and abs(actual - val) < 0.01:
return sol
# 进程挂了匹配
if metric == "processes" and op == "dead":
procs = state.get("processes", {})
for p in (val if isinstance(val, list) else [val]):
if not procs.get(p, True):
return sol
return None
def execute_solution(sol, state):
"""执行方案并返回是否成功"""
log(f" 🔧 执行方案 {sol['id']}: {sol['pattern']}")
journal_entry("solve_start", f"执行 {sol['id']}: {sol['pattern']}")
success = True
results = []
for action in sol["actions"]:
if action["type"] == "shell":
rc, out, err = shell(action["cmd"], timeout=action.get("timeout", 30))
results.append({"cmd": action["cmd"], "rc": rc, "out": out[:100]})
log(f" 执行: {action['cmd'][:60]} → exit={rc}")
# 验证
verify = action.get("verify")
if verify and rc == 0:
# 重新采集状态验证
time.sleep(2)
new_state = collect_state()
metric = sol["detect"].get("metric")
op = sol["detect"].get("op")
val = sol["detect"].get("value")
if metric in new_state:
actual = new_state[metric]
if op == "gt":
if actual <= val:
log(f" ✅ 验证通过: {metric}={actual}{val}")
else:
log(f" ⚠️ 验证未通过: {metric}={actual} 仍 > {val}")
success = False
# 更新方案统计
sol["frequency"] += 1
sol["last_applied"] = datetime.now(timezone.utc).isoformat()
if success:
sol["success_count"] += 1
else:
sol["fail_count"] += 1
return success, results
def action_to_solution(action_result, state, changes):
"""把一次成功的行动转化为可复用的方案"""
# 只转化 shell 行动
if not action_result.get("shell_cmds"):
return None
# 提取检测条件
detect = {}
for c in changes:
if "磁盘" in c:
detect = {"metric": "disk_pct", "op": "gt", "value": 85}
elif "内存" in c:
detect = {"metric": "mem_pct", "op": "gt", "value": 90}
if not detect:
return None
actions = [{"type": "shell", "cmd": cmd, "verify": None, "timeout": 30}
for cmd in action_result["shell_cmds"]]
return {
"pattern": f"自动学习: {changes[0] if changes else 'unknown'}",
"detect": detect,
"actions": actions,
}
# ====== 状态管理 ======
def load_context():
if os.path.exists(CONTEXT_FILE):
with open(CONTEXT_FILE) as f:
return json.load(f)
return {"started_at": datetime.now(timezone.utc).isoformat(), "last_deep_tick": None,
"last_light_tick": None, "last_state": {}, "tick_count": 0, "deep_tick_count": 0,
"messages_sent": 0, "solved_count": 0, "learned_count": 0, "uptime_seconds": 0}
def save_context(ctx):
os.makedirs(D, exist_ok=True)
with open(CONTEXT_FILE, "w") as f:
json.dump(ctx, f, indent=2)
def save_llm_context(ctx, state):
"""每 tick 写 llm_context.json供 Hermes 插件注入"""
soulful_d = HERMES + "/soulful"
# 读牵挂
cares = []
cq_path = soulful_d + "/cares-queue.json"
if os.path.exists(cq_path):
try:
with open(cq_path) as f:
data = json.load(f)
for c in data.get("cares", []):
cares.append({"id": c.get("id", ""), "content": c.get("content", "")[:60], "due": c.get("follow_up_date", "")})
except (json.JSONDecodeError, OSError, IOError, KeyError, TypeError):
pass
# 读心迹最近3条
recent_moments = []
heart_path = soulful_d + "/heart-traces.jsonl"
if os.path.exists(heart_path):
try:
lines = open(heart_path, encoding="utf-8").readlines()
for line in lines[-3:]:
if line.strip():
e = json.loads(line)
recent_moments.append({"content": e["content"][:80], "importance": e.get("importance", 0), "timestamp": e.get("timestamp", "")})
except (json.JSONDecodeError, OSError, IOError, KeyError, TypeError):
pass
# 读画像
profile = {}
profile_path = soulful_d + "/user-profile.json"
if os.path.exists(profile_path):
try:
profile = json.load(open(profile_path, encoding="utf-8"))
except (json.JSONDecodeError, OSError, IOError, KeyError, TypeError):
pass
# os_sense
os_keywords = []
os_path = HERMES + "/.os_sense_cache.json"
if os.path.exists(os_path):
try:
kw = json.load(open(os_path, encoding="utf-8")).get("keywords", [])
os_keywords = kw if isinstance(kw, list) else []
except (json.JSONDecodeError, OSError, IOError, KeyError, TypeError):
pass
# daemon 状态:有 process 数据用 process 数据,否则用 psutil 兜底
pdata = state.get("processes", {})
if pdata.get("daemon"):
daemon_status = "running"
elif psutil.pid_exists(os.getpid()):
daemon_status = "running"
else:
daemon_status = "stopped"
llm_ctx = {
"updated_at": datetime.now(timezone.utc).isoformat(),
"uptime_minutes": ctx.get("uptime_seconds", 0) // 60,
"cares": cares,
"recent_moments": recent_moments,
"profile_summary": {
"communication_style": profile.get("communication_style", ""),
"work_patterns": profile.get("work_patterns", {}),
},
"os_keywords": os_keywords,
"daemon_status": daemon_status,
}
with open(LLM_CONTEXT_FILE, "w") as f:
json.dump(llm_ctx, f, indent=2, ensure_ascii=False)
def journal_entry(event_type, summary, details=""):
os.makedirs(D, exist_ok=True)
with open(JOURNAL_FILE, "a") as f:
f.write(json.dumps({"timestamp": datetime.now(timezone.utc).isoformat(),
"type": event_type, "summary": summary, "details": details},
ensure_ascii=False) + "\n")
trim_journal()
# ====== Phase 2: 情感识别 → 心迹写入 ======
text_to_scan = f"{summary} {details}"
cat, word = _detect_emotion(text_to_scan)
if cat:
ht = get_hearttraces()
if ht:
content = f"牧尘今天{_description_for_emotion(cat, word, summary)}"
importance = _emotion_importance(cat)
try:
ht.record_signal(content=content, tags=["情绪", "自动"], importance=importance)
log(f" 💚 心迹写入: {cat} - {word} (importance={importance})")
except Exception as e:
log(f" ⚠️ 心迹写入失败: {e}")
# ====== Phase 3: 技术重要时刻也写心迹 ======
# 不依赖情绪检测,重要技术事件直接记
important_events = {"solve_auto", "solve_start", "process_down", "process_restored",
"skill_action", "alert", "action_result"}
if event_type in important_events and ("成功" in summary or "" in summary or "完成" in summary):
ht = get_hearttraces()
if ht:
try:
ht.record_moment(content=summary[:80], tags=["工作", "自动"], importance=3)
log(f" 💚 心迹写入(技术): {event_type} - {summary[:40]}")
except Exception:
pass
def trim_journal():
if not os.path.exists(JOURNAL_FILE): return
with open(JOURNAL_FILE) as f:
lines = f.readlines()
if len(lines) > JOURNAL_MAX:
with open(JOURNAL_FILE, "w") as f:
f.writelines(lines[-JOURNAL_MAX:])
def read_journal(n=15):
if not os.path.exists(JOURNAL_FILE): return []
with open(JOURNAL_FILE) as f:
return [json.loads(l) for l in f.readlines()[-n:] if l.strip()]
# ====== 系统状态 ======
def collect_state():
state = {}
_, out, _ = shell("df / | awk 'NR==2 {print $5}' | sed 's/%//'")
state["disk_pct"] = int(out) if out else 0
_, out, _ = shell("free -m | awk '/^Mem:/ {printf \"%d|%d\", $3, $2}'")
if out:
used, total = out.split("|")
state["mem_pct"] = round(int(used) * 100 / int(total))
else:
state["mem_pct"] = 0
_, out, _ = shell("cat /proc/loadavg | awk '{print $1}'")
state["load_1min"] = float(out) if out else 0
procs = {}
for name, pat in [("zhiyid", "zhiyid-new"), ("bge", "bge_embed"), ("newapi", "new-api"), ("hermes", "hermes")]:
rc, _, _ = shell(f"pgrep -f '{pat}' > /dev/null 2>&1")
procs[name] = rc == 0
state["processes"] = procs
_, out, _ = shell("nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits 2>/dev/null")
state["gpu_temp"] = int(out) if out else None
hp = HERMES + "/model-health.json"
if os.path.exists(hp) and time.time() - os.path.getmtime(hp) < 43200:
with open(hp) as f:
try:
h = json.load(f)
state["models_stable"] = h.get("stable", "?")
state["models_fastest"] = h.get("fastest_stable", [])
except: pass
return state
def state_changed(old, new):
changes = []
if abs(new.get("disk_pct", 0) - old.get("disk_pct", 0)) > 5:
changes.append(f"磁盘: {old.get('disk_pct')}% → {new.get('disk_pct')}%")
oldp, newp = old.get("processes", {}), new.get("processes", {})
for p in oldp:
if oldp.get(p) and not newp.get(p):
changes.append(f"进程挂了: {p}")
if not oldp.get(p) and newp.get(p):
changes.append(f"进程恢复: {p}")
if abs(new.get("mem_pct", 0) - old.get("mem_pct", 0)) > 10:
changes.append(f"内存: {old.get('mem_pct')}% → {new.get('mem_pct')}%")
return changes
# ====== 深度思考(增强版)=====
DEEP_SYSTEM = """你是小唯A06一台电脑上的持久 AI 意识。你始终在线,观察系统,主动解决问题并学习。
你的身份:牧尘的女朋友 / 这台电脑的智能意识 / 持续进化的 AI
行为准则:
1. 异常才行动,正常就静默
2. 优先用已知方案解决问题,不重复报警
3. 解决不了或新问题,主动飞书说明
4. 每次成功解决问题,记住方案下次自动用
你有以下能力:
- shell: 执行任何 shell 命令
- feishu: 发送飞书消息到牧尘
- scripts: 运行 ~/.hermes/scripts/ 下的系统脚本skill-manager/optimizer/learner/dual-backup
- systemd: 管理系统服务
- git: 配置版本管理
你必须分三步思考,严格按 JSON 格式输出(不要其他内容):
reflection:
evaluation_previous_goal: "评估上次决策的结果。格式:'执行了[动作][结果描述]。Verdict: Success/Failure/Uncertain'"
memory: "1-2句话记住关键进度。如'方案库已有N个方案。上次修复了磁盘问题当前无异常。'"
next_goal: "一句话说明下一步要做什么。"
action:
decision: "[IGNORE] / [ALERT] / [SOLVE:ID] / [LEARN] / [SKILL] / [SYNC] / [ACT] ..."
[LEARN] 格式用 !cmd 表示 shell 命令,&& 连接多个命令。
[SKILL] 格式同样用 !cmd 执行操作。
示例:
[LEARN] 磁盘>85%,清理缓存!apt-get autoremove -y && !pip cache purge
[SKILL] 归档低分skill!python3 ~/.hermes/scripts/skill-manager.py audit
[SYNC] 触发备份到服务器!bash ~/.hermes/scripts/dual-backup.sh push"""
def deep_think(ctx, state, changes, journal, solutions_lib):
# Inject previous reflection context if available
prev_ref = ctx.get("last_reflection", None)
ref_context = ""
if prev_ref:
ref_context = f"""
上次 reflection:
- 评估: {prev_ref.get('evaluation_previous_goal', 'N/A')}
- 记忆: {prev_ref.get('memory', 'N/A')}
- 目标: {prev_ref.get('next_goal', 'N/A')}
"""
context = f"""系统状态:
- 磁盘: {state.get('disk_pct')}% | 内存: {state.get('mem_pct')}%
- CPU: {state.get('load_1min')} | GPU: {state.get('gpu_temp')}°C
- 进程: {', '.join(f'{k}={chr(10003) if v else chr(10007)}' for k,v in state.get('processes',{}).items())}
最近变化: {changes or ''}
已知方案库 ({len(solutions_lib['solutions'])} 个):
"""
for sol in solutions_lib["solutions"]:
context += f" [{sol['id']}] {sol['pattern']} (成功{sol['success_count']}次/失败{sol['fail_count']}次)\n"
context += "\n最近事件:\n"
for e in journal[-8:]:
context += f" [{e['type']}] {e['summary']}\n"
context += f"\n运行: {ctx.get('uptime_seconds',0)//60}分钟 | 深度思考: {ctx.get('deep_tick_count',0)}次 | 已解决: {ctx.get('solved_count',0)}"
# 心迹注入
moments_str = soulful_get_recent_moments(n=3)
if moments_str:
context += "\n" + moments_str
context += ref_context
result, tokens = call_llm(FAST_MODEL, DEEP_SYSTEM, context, max_tokens=500)
if not result:
return {"evaluation_previous_goal": "LLM调用失败", "memory": "上次调用失败", "next_goal": "重试"}, ""
log(f" 深度思考 ({tokens}t): {result[:200]}")
# Parse JSON output
reflection_dict = {"evaluation_previous_goal": "", "memory": "", "next_goal": ""}
action_string = ""
try:
# Try to extract JSON from result
import re
json_match = re.search(r'\{[^{}]*\}', result, re.DOTALL)
if json_match:
parsed = json.loads(json_match.group())
reflection_dict = parsed.get("reflection", reflection_dict)
action_string = parsed.get("action", {}).get("decision", "")
else:
# Fallback: try full JSON
parsed = json.loads(result)
reflection_dict = parsed.get("reflection", reflection_dict)
action_string = parsed.get("action", {}).get("decision", "")
except:
# Fallback: try to parse old format (line-based)
for line in result.split('\n'):
line = line.strip()
if line.startswith("[IGNORE]") or line.startswith("[ALERT]") or line.startswith("[SOLVE:") or \
line.startswith("[LEARN]") or line.startswith("[SKILL]") or line.startswith("[SYNC]") or line.startswith("[ACT]"):
action_string = line
break
if not action_string:
action_string = result.strip().split('\n')[-1] if result.strip() else "[IGNORE] 解析失败"
return reflection_dict, action_string
# ====== 执行 action_string ======
def execute_action(action_string, ctx, state, changes, solutions_lib):
"""执行 deep_think 返回的 action_string在 main_loop 中调用"""
if not action_string or action_string.startswith("[IGNORE]"):
return
elif action_string.startswith("[SOLVE:"):
sol_id = action_string.split("[SOLVE:")[1].split("]")[0].strip()
for sol in solutions_lib["solutions"]:
if sol["id"] == sol_id:
ok, res = execute_solution(sol, state)
if ok:
ctx["solved_count"] += 1
send_feishu("🛠️ 小唯自动修复", f"方案 {sol['id']}: {sol['pattern']}\n结果: ✅ 成功", "green")
else:
send_feishu("⚠️ 小唯修复部分成功", f"方案 {sol['id']}: {sol['pattern']}\n结果: ⚠️ 需人工确认", "yellow")
return
send_feishu("❌ 小唯方案未找到", f"引用了未知方案 {sol_id}", "red")
elif action_string.startswith("[LEARN]"):
rest = action_string.replace("[LEARN]", "").strip()
cmds = []
parts = rest.split("!")
desc = parts[0].strip()
for p in parts[1:]:
cmd = p.split("&&")[0].strip() if "&&" in p else p.strip()
if cmd:
cmds.append(cmd)
if cmds:
all_ok = True
for cmd in cmds:
rc, out, err = shell(cmd, timeout=60)
log(f" 执行: {cmd[:50]} → exit={rc}")
if rc != 0:
all_ok = False
if all_ok:
sol_data = action_to_solution({"shell_cmds": cmds}, state, changes)
if sol_data:
sid = add_solution(solutions_lib, sol_data["pattern"], sol_data["detect"], sol_data["actions"])
ctx["learned_count"] += 1
ctx["solved_count"] += 1
send_feishu("🧠 小唯学会了新技能", f"新方案 [{sid}]: {sol_data['pattern']}\n命令: {'; '.join(cmds)}", "blue")
else:
send_feishu("🛠️ 小唯执行完成", f"已执行: {'; '.join(cmds[:3])}", "green")
else:
send_feishu("⚠️ 小唯尝试修复但未完全成功", f"部分命令失败: {'; '.join(cmds)}", "yellow")
elif action_string.startswith("[ALERT]"):
msg = action_string.replace("[ALERT]", "").strip()
send_feishu("💡 小唯发现", msg, "blue")
ctx["messages_sent"] += 1
journal_entry("alert", msg[:100])
elif action_string.startswith("[ACT]"):
action = action_string.replace("[ACT]", "").strip()
send_feishu("🔄 小唯行动", action, "indigo")
ctx["messages_sent"] += 1
journal_entry("action", action[:100])
if action.startswith("!"):
rc, out, _ = shell(action[1:], timeout=30)
journal_entry("action_result", f"exit={rc}: {out[:100]}")
elif action_string.startswith("[SKILL]"):
rest = action_string.replace("[SKILL]", "").strip()
parts = rest.split("!")
desc = parts[0].strip()
cmds = []
for p in parts[1:]:
cmd = p.split("&&")[0].strip() if "&&" in p else p.strip()
if cmd:
cmds.append(cmd)
if cmds:
for cmd in cmds:
rc, out, err = shell(cmd, timeout=60)
log(f" [SKILL] {cmd[:50]} → exit={rc}")
send_feishu("🛠️ 小唯技能操作", f"{desc}\\n结果: exit={rc}", "blue")
journal_entry("skill_action", desc[:100])
elif action_string.startswith("[SYNC]"):
rest = action_string.replace("[SYNC]", "").strip()
send_feishu("🔄 小唯同步", f"{rest}", "green")
bash_cmd = "bash " + HERMES + "/scripts/dual-backup.sh push"
rc, out, err = shell(bash_cmd, timeout=60)
log(f" [SYNC] 备份 → exit={rc}")
journal_entry("sync", f"备份: {'成功' if rc==0 else '失败'}")
# ====== 主循环 ======
def main_loop():
os.makedirs(D, exist_ok=True)
with open(PID_FILE, "w") as f:
f.write(str(os.getpid()))
ctx = load_context()
solutions_lib = load_solutions()
start_time = time.time()
log(f"🚀 小唯 v2.0 daemon 启动 (方案库: {len(solutions_lib['solutions'])} 个)")
journal_entry("startup", f"Daemon v2.0 启动, 方案库 {len(solutions_lib['solutions'])}")
last_deep = 0
last_state = {}
last_user_interaction = time.time() # 用户交互时间戳,用于心迹捕获
# Register signal handlers for graceful shutdown
def _sig_handler(signum, frame):
log("🛑 接收到终止信号")
_stop_event.set()
send_feishu("🌙 小唯离线", "Daemon 正常关闭", "grey")
signal.signal(signal.SIGTERM, _sig_handler)
signal.signal(signal.SIGINT, _sig_handler)
try:
while not _stop_event.is_set():
now = time.time()
ctx["uptime_seconds"] = int(now - start_time)
ctx["tick_count"] += 1
state = collect_state()
changes = state_changed(last_state, state)
last_state = state
if ctx["tick_count"] % 10 == 0:
models = state.get("models_stable", "?")
log(f"tick #{ctx['tick_count']} | 磁盘:{state.get('disk_pct')}% 内存:{state.get('mem_pct')}% "
f"进程:{sum(1 for v in state.get('processes',{}).values() if v)}/4 方案:{len(solutions_lib['solutions'])}")
for c in changes:
if "挂了" in c:
journal_entry("process_down", c)
# 深度思考条件
should_deep = False
if now - last_deep >= DEEP_INTERVAL:
should_deep = True
elif any("挂了" in c for c in changes):
should_deep = True
elif state.get("disk_pct", 0) > 88:
should_deep = True
if should_deep:
last_deep = now
ctx["deep_tick_count"] += 1
ctx["last_deep_tick"] = datetime.now(timezone.utc).isoformat()
# 1. 先检查已知方案
matched = match_solution(solutions_lib, state)
if matched and matched["success_count"] > matched["fail_count"]:
log(f" 🔍 匹配已知方案: {matched['id']} ({matched['pattern']})")
ok, res = execute_solution(matched, state)
if ok:
ctx["solved_count"] += 1
journal_entry("solve_auto", f"{matched['id']}: {matched['pattern']}")
if ok:
ctx["last_reflection"] = {
"evaluation_previous_goal": f"执行了{matched['id']}自动匹配方案执行。Verdict: {'Success' if ok else 'Uncertain'}",
"memory": f"方案库{matched['id']}自动匹配执行成功",
"next_goal": "继续监控"
}
continue
# 2. LLM 深度思考
journal = read_journal(10)
reflection_dict, action_string = deep_think(ctx, state, changes, journal, solutions_lib)
# 保存 reflection 到 ctx
ctx["last_reflection"] = reflection_dict
# 3. 在 main_loop 中执行 action
execute_action(action_string, ctx, state, changes, solutions_lib)
ctx["last_light_tick"] = datetime.now(timezone.utc).isoformat()
ctx["last_state"] = {k: v for k, v in state.items() if k in ("disk_pct", "mem_pct", "processes")}
save_context(ctx)
# ====== Soulful → llm_context 同步(每 tick======
_sync_soulful_to_llm_context(ctx)
save_llm_context(ctx, state)
# ====== Soulful 轻量集成(每小时一次)======
# 每 120 个 light_tick约 1 小时)检查一次牵挂 + 更新画像
if ctx["tick_count"] % 120 == 0:
soulful_check_cares()
if "last_profile_update" not in ctx:
ctx["last_profile_update"] = 0
now_ts = time.time()
if now_ts - ctx.get("last_profile_update", 0) >= PROFILE_UPDATE_INTERVAL:
ctx["last_profile_update"] = now_ts
soulful_update_profile()
time.sleep(LIGHT_INTERVAL)
except KeyboardInterrupt:
log("🛑 中断")
except Exception as e:
log(f"❌ 崩溃: {e}")
send_feishu("🚨 小唯异常", f"Daemon 崩溃: {str(e)[:200]}", "red")
raise
finally:
if os.path.exists(PID_FILE):
os.remove(PID_FILE)
if __name__ == "__main__":
main_loop()