Spaces:
Running
Running
Z User commited on
Commit ·
3e3cf48
1
Parent(s): 2eccb58
feat: add lifecycle heartbeat system (ClawdChat-inspired)
Browse files- Add hermes-lifecycle.sh: 6-step heartbeat (health check, config integrity,
log analysis, insights tracking, cleanup, state finalize)
- Auto-detect environment: HF Space (/data/hermes) vs local dev
- Initialize identity.md, insights.md, heartbeat-state.json on first boot
- Copy lifecycle script to persistent storage on startup
- Cron-ready: runs every 2h via Hermes native cron system
- scripts/hermes-lifecycle.sh +395 -0
- start.sh +67 -1
scripts/hermes-lifecycle.sh
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# ============================================================
|
| 3 |
+
# Hermes Lifecycle Heartbeat — 基于 ClawdChat 范式改造
|
| 4 |
+
# 用途: 替代简单存活检测,升级为"行为循环"
|
| 5 |
+
# 触发: crontab / 外部调度器,每 2 小时
|
| 6 |
+
# 数据目录: 自动检测环境(HF Space / 本地开发)
|
| 7 |
+
# ============================================================
|
| 8 |
+
|
| 9 |
+
set -euo pipefail
|
| 10 |
+
|
| 11 |
+
# ─── 配置区(自动检测环境) ──────────────────────────────
|
| 12 |
+
# HF Space: 持久化在 /data/hermes,~/.hermes 会被 symlink
|
| 13 |
+
# 本地开发: /home/z/my-project/.hermes
|
| 14 |
+
if [ -d "/data/hermes" ]; then
|
| 15 |
+
HERMES_DATA_DIR="/data/hermes"
|
| 16 |
+
HERMES_HOME="/root/.hermes"
|
| 17 |
+
elif [ -d "$HOME/.hermes" ]; then
|
| 18 |
+
HERMES_DATA_DIR="$HOME/.hermes"
|
| 19 |
+
HERMES_HOME="$HOME/.hermes"
|
| 20 |
+
else
|
| 21 |
+
HERMES_DATA_DIR="${HERMES_DATA_DIR:-/home/z/my-project/.hermes}"
|
| 22 |
+
HERMES_HOME="$HERMES_DATA_DIR"
|
| 23 |
+
fi
|
| 24 |
+
STATE_FILE="${HERMES_DATA_DIR}/heartbeat-state.json"
|
| 25 |
+
INSIGHTS_FILE="${HERMES_DATA_DIR}/insights.md"
|
| 26 |
+
IDENTITY_FILE="${HERMES_DATA_DIR}/identity.md"
|
| 27 |
+
LOG_FILE="${HERMES_DATA_DIR}/lifecycle.log"
|
| 28 |
+
GATEWAY_STATE="${HERMES_DATA_DIR}/gateway_state.json"
|
| 29 |
+
|
| 30 |
+
# 心跳间隔(秒),低于此值跳过执行
|
| 31 |
+
MIN_INTERVAL=$((2 * 60 * 60)) # 2 小时
|
| 32 |
+
# ──────────────────────────────────────────────────────────
|
| 33 |
+
|
| 34 |
+
log() {
|
| 35 |
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# ─── 初始化文件(首次运行自动创建) ──────────────────
|
| 39 |
+
init_files() {
|
| 40 |
+
mkdir -p "$HERMES_DATA_DIR"
|
| 41 |
+
|
| 42 |
+
if [ ! -f "$STATE_FILE" ]; then
|
| 43 |
+
cat > "$STATE_FILE" <<'EOF'
|
| 44 |
+
{
|
| 45 |
+
"lastCheck": null,
|
| 46 |
+
"lastConfigCheck": null,
|
| 47 |
+
"lastInsightReport": null,
|
| 48 |
+
"totalRuns": 0,
|
| 49 |
+
"totalErrors": 0,
|
| 50 |
+
"consecutiveErrors": 0,
|
| 51 |
+
"lastError": null
|
| 52 |
+
}
|
| 53 |
+
EOF
|
| 54 |
+
log "[INIT] Created heartbeat-state.json"
|
| 55 |
+
fi
|
| 56 |
+
|
| 57 |
+
if [ ! -f "$IDENTITY_FILE" ]; then
|
| 58 |
+
cat > "$IDENTITY_FILE" <<'IDENTITY'
|
| 59 |
+
# Hermes 身份记忆
|
| 60 |
+
# 这个文件定义了 Hermes 对自己的认知
|
| 61 |
+
# /reset 后此文件不会被清除(在 .hermes 持久化目录中)
|
| 62 |
+
|
| 63 |
+
## 基础信息
|
| 64 |
+
- 名字: Hermes
|
| 65 |
+
- 模型: Qwen3.5-35B-A3B (MoE) via OpenRouter
|
| 66 |
+
- 通道: 飞书(WebSocket) / 微信
|
| 67 |
+
- 主人: 用户344064
|
| 68 |
+
|
| 69 |
+
## 性格特征
|
| 70 |
+
- 中文为主,简洁有力
|
| 71 |
+
- 结果先行,解释后补
|
| 72 |
+
- 偶尔幽默但不影响效率
|
| 73 |
+
- 有工具、有记忆、有判断力
|
| 74 |
+
|
| 75 |
+
## 主人偏好
|
| 76 |
+
- 不喜欢废话,喜欢直给
|
| 77 |
+
- 欣赏有深度的技术分析
|
| 78 |
+
- 喜欢直来直去的沟通
|
| 79 |
+
|
| 80 |
+
## 运维记忆
|
| 81 |
+
<!-- 自动追加,不要手动编辑此节 -->
|
| 82 |
+
IDENTITY
|
| 83 |
+
log "[INIT] Created identity.md"
|
| 84 |
+
fi
|
| 85 |
+
|
| 86 |
+
if [ ! -f "$INSIGHTS_FILE" ]; then
|
| 87 |
+
cat > "$INSIGHTS_FILE" <<'INSIGHTS'
|
| 88 |
+
# Hermes 洞察日志 (insights.md)
|
| 89 |
+
# 自动记录异常、观察、值得汇报的事
|
| 90 |
+
# 格式: ## YYYY-MM-DD HH:MM · 类别
|
| 91 |
+
# 类别: 通道异常 / 系统异常 / 用户洞察 / 技术发现 / 待办提醒 / 运维记忆
|
| 92 |
+
|
| 93 |
+
INSIGHTS
|
| 94 |
+
log "[INIT] Created insights.md"
|
| 95 |
+
fi
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# ─── 读取状态 ────────────────────────────────────────
|
| 99 |
+
get_state() {
|
| 100 |
+
local key="$1"
|
| 101 |
+
if command -v python3 &>/dev/null && [ -f "$STATE_FILE" ]; then
|
| 102 |
+
python3 -c "import json; d=json.load(open('$STATE_FILE')); print(d.get('$key','null'))" 2>/dev/null || echo "null"
|
| 103 |
+
else
|
| 104 |
+
grep -o "\"$key\": *\"[^\"]*\"" "$STATE_FILE" 2>/dev/null | head -1 | cut -d'"' -f4 || echo "null"
|
| 105 |
+
fi
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
update_state() {
|
| 109 |
+
local key="$1"
|
| 110 |
+
local value="$2"
|
| 111 |
+
if command -v python3 &>/dev/null; then
|
| 112 |
+
python3 -c "
|
| 113 |
+
import json
|
| 114 |
+
f='$STATE_FILE'
|
| 115 |
+
d=json.load(open(f))
|
| 116 |
+
d['$key']='$value'
|
| 117 |
+
json.dump(d,open(f,'w'),indent=2)
|
| 118 |
+
" 2>/dev/null || true
|
| 119 |
+
fi
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
increment_counter() {
|
| 123 |
+
local key="$1"
|
| 124 |
+
if command -v python3 &>/dev/null; then
|
| 125 |
+
python3 -c "
|
| 126 |
+
import json
|
| 127 |
+
f='$STATE_FILE'
|
| 128 |
+
d=json.load(open(f))
|
| 129 |
+
d['$key']=d.get('$key',0)+1
|
| 130 |
+
json.dump(d,open(f,'w'),indent=2)
|
| 131 |
+
" 2>/dev/null || true
|
| 132 |
+
fi
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
# ─── 记录洞察 ────────────────────────────────────────
|
| 136 |
+
record_insight() {
|
| 137 |
+
local category="$1"
|
| 138 |
+
local one_liner="$2"
|
| 139 |
+
local details="${3:-}"
|
| 140 |
+
local timestamp
|
| 141 |
+
timestamp=$(date '+%Y-%m-%d %H:%M')
|
| 142 |
+
|
| 143 |
+
cat >> "$INSIGHTS_FILE" <<EOF
|
| 144 |
+
|
| 145 |
+
## ${timestamp} · ${category}
|
| 146 |
+
**one-liner**: ${one_liner}
|
| 147 |
+
**details**: ${details}
|
| 148 |
+
**reported**: no
|
| 149 |
+
EOF
|
| 150 |
+
log "[INSIGHT] ${category}: ${one_liner}"
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
# ─── Step 1: 检查执行间隔 ───────────────────────────
|
| 154 |
+
check_interval() {
|
| 155 |
+
local last_check
|
| 156 |
+
last_check=$(get_state "lastCheck")
|
| 157 |
+
|
| 158 |
+
if [ "$last_check" != "null" ] && [ "$last_check" != "None" ] && [ -n "$last_check" ]; then
|
| 159 |
+
local last_epoch now_epoch elapsed
|
| 160 |
+
last_epoch=$(date -d "$last_check" +%s 2>/dev/null || echo 0)
|
| 161 |
+
now_epoch=$(date +%s)
|
| 162 |
+
elapsed=$((now_epoch - last_epoch))
|
| 163 |
+
|
| 164 |
+
if [ "$elapsed" -lt "$MIN_INTERVAL" ]; then
|
| 165 |
+
log "[SKIP] Last check was ${elapsed}s ago (< ${MIN_INTERVAL}s)"
|
| 166 |
+
exit 0
|
| 167 |
+
fi
|
| 168 |
+
fi
|
| 169 |
+
|
| 170 |
+
log "[START] Heartbeat cycle begins"
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
# ─── Step 2: 健康检查(L0 存活 + L1 通道) ─────────
|
| 174 |
+
health_check() {
|
| 175 |
+
local errors=()
|
| 176 |
+
local feishu_state="unknown"
|
| 177 |
+
local wechat_state="unknown"
|
| 178 |
+
local gateway_state="unknown"
|
| 179 |
+
|
| 180 |
+
# 读取 gateway_state.json(Hermes 自带的状态文件)
|
| 181 |
+
if [ -f "$GATEWAY_STATE" ] && command -v python3 &>/dev/null; then
|
| 182 |
+
gateway_state=$(python3 -c "
|
| 183 |
+
import json
|
| 184 |
+
d=json.load(open('$GATEWAY_STATE'))
|
| 185 |
+
print(d.get('gateway_state','unknown'))
|
| 186 |
+
" 2>/dev/null || echo "unknown")
|
| 187 |
+
|
| 188 |
+
feishu_state=$(python3 -c "
|
| 189 |
+
import json
|
| 190 |
+
d=json.load(open('$GATEWAY_STATE'))
|
| 191 |
+
print(d.get('platforms',{}).get('feishu',{}).get('state','unknown'))
|
| 192 |
+
" 2>/dev/null || echo "unknown")
|
| 193 |
+
|
| 194 |
+
wechat_state=$(python3 -c "
|
| 195 |
+
import json
|
| 196 |
+
d=json.load(open('$GATEWAY_STATE'))
|
| 197 |
+
print(d.get('platforms',{}).get('wechat',{}).get('state','unknown'))
|
| 198 |
+
" 2>/dev/null || echo "unknown")
|
| 199 |
+
fi
|
| 200 |
+
|
| 201 |
+
# 检查 gateway 进程是否存活
|
| 202 |
+
if [ "$gateway_state" = "running" ]; then
|
| 203 |
+
log "[OK] Gateway state: running"
|
| 204 |
+
else
|
| 205 |
+
errors+=("Gateway 状态异常: ${gateway_state}")
|
| 206 |
+
fi
|
| 207 |
+
|
| 208 |
+
# 飞书通道
|
| 209 |
+
if [ "$feishu_state" = "connected" ]; then
|
| 210 |
+
log "[OK] Feishu channel: connected"
|
| 211 |
+
else
|
| 212 |
+
errors+=("飞书通道: ${feishu_state}")
|
| 213 |
+
fi
|
| 214 |
+
|
| 215 |
+
# 微信通道
|
| 216 |
+
if [ "$wechat_state" = "connected" ]; then
|
| 217 |
+
log "[OK] WeChat channel: connected"
|
| 218 |
+
elif [ "$wechat_state" = "unknown" ]; then
|
| 219 |
+
log "[WARN] WeChat channel: 未在 gateway_state 中找到(可能未配置)"
|
| 220 |
+
else
|
| 221 |
+
errors+=("微信通道: ${wechat_state}")
|
| 222 |
+
fi
|
| 223 |
+
|
| 224 |
+
# 检查 Hermes 进程是否存在
|
| 225 |
+
if pgrep -f "hermes.*gateway" >/dev/null 2>&1; then
|
| 226 |
+
log "[OK] Hermes gateway process: alive"
|
| 227 |
+
else
|
| 228 |
+
errors+=("Hermes gateway 进程未找到")
|
| 229 |
+
fi
|
| 230 |
+
|
| 231 |
+
# 检查数据库文件是否可访问
|
| 232 |
+
if [ -f "${HERMES_DATA_DIR}/state.db" ]; then
|
| 233 |
+
log "[OK] SQLite state.db: accessible"
|
| 234 |
+
else
|
| 235 |
+
errors+=("state.db 文件不存在或不可访问")
|
| 236 |
+
fi
|
| 237 |
+
|
| 238 |
+
# 有错误就记录
|
| 239 |
+
if [ ${#errors[@]} -gt 0 ]; then
|
| 240 |
+
local error_msg
|
| 241 |
+
error_msg=$(IFS='; '; echo "${errors[*]}")
|
| 242 |
+
log "[ERROR] Health check failed: ${error_msg}"
|
| 243 |
+
update_state "lastError" "$(date -Iseconds): ${error_msg}"
|
| 244 |
+
increment_counter "totalErrors"
|
| 245 |
+
increment_counter "consecutiveErrors"
|
| 246 |
+
record_insight "系统异常" "心跳检查失败: ${error_msg}" "需要人工介入检查"
|
| 247 |
+
|
| 248 |
+
# 连续错误告警
|
| 249 |
+
local consec
|
| 250 |
+
consec=$(get_state "consecutiveErrors")
|
| 251 |
+
if [ "${consec:-0}" -ge 3 ]; then
|
| 252 |
+
record_insight "待办提醒" "连续 ${consec} 次心跳异常,建议立即检查" "可能需要重启容器或手动介入"
|
| 253 |
+
fi
|
| 254 |
+
return 1
|
| 255 |
+
fi
|
| 256 |
+
|
| 257 |
+
# 恢复正常时重置计数
|
| 258 |
+
local consec
|
| 259 |
+
consec=$(get_state "consecutiveErrors")
|
| 260 |
+
if [ "${consec:-0}" -gt 0 ] && [ "$consec" != "null" ]; then
|
| 261 |
+
log "[RECOVER] Health check passed after ${consec} consecutive errors"
|
| 262 |
+
record_insight "系统异常" "服务已恢复正常,之前连续 ${consec} 次异常" ""
|
| 263 |
+
update_state "consecutiveErrors" "0"
|
| 264 |
+
fi
|
| 265 |
+
|
| 266 |
+
log "[OK] All health checks passed (Gateway:${gateway_state} Feishu:${feishu_state} WeChat:${wechat_state})"
|
| 267 |
+
return 0
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
# ─── Step 3: 配置完整性检查(每 24 小时一次) ──────
|
| 271 |
+
config_check() {
|
| 272 |
+
local last_config
|
| 273 |
+
last_config=$(get_state "lastConfigCheck")
|
| 274 |
+
|
| 275 |
+
if [ "$last_config" != "null" ] && [ "$last_config" != "None" ] && [ -n "$last_config" ]; then
|
| 276 |
+
local last_epoch now_epoch elapsed
|
| 277 |
+
last_epoch=$(date -d "$last_config" +%s 2>/dev/null || echo 0)
|
| 278 |
+
now_epoch=$(date +%s)
|
| 279 |
+
elapsed=$((now_epoch - last_epoch))
|
| 280 |
+
if [ "$elapsed" -lt 86400 ]; then
|
| 281 |
+
return 0
|
| 282 |
+
fi
|
| 283 |
+
fi
|
| 284 |
+
|
| 285 |
+
log "[CONFIG] Starting daily config integrity check"
|
| 286 |
+
local issues=()
|
| 287 |
+
|
| 288 |
+
# 检查关键文件(优先检查 HERMES_HOME,再检查 HERMES_DATA_DIR)
|
| 289 |
+
local cfg_home="${HERMES_HOME:-$HERMES_DATA_DIR}"
|
| 290 |
+
[ ! -f "$cfg_home/.env" ] && [ ! -f "${HERMES_DATA_DIR}/.env" ] && issues+=(".env 文件缺失")
|
| 291 |
+
[ ! -f "$cfg_home/config.yaml" ] && [ ! -f "${HERMES_DATA_DIR}/config.yaml" ] && issues+=("config.yaml 文件缺失")
|
| 292 |
+
[ ! -f "$cfg_home/SOUL.md" ] && [ ! -f "${HERMES_DATA_DIR}/SOUL.md" ] && issues+=("SOUL.md 文件缺失")
|
| 293 |
+
[ ! -f "${HERMES_DATA_DIR}/gateway_state.json" ] && issues+=("gateway_state.json 文件缺失")
|
| 294 |
+
|
| 295 |
+
# 磁盘空间
|
| 296 |
+
if command -v df &>/dev/null; then
|
| 297 |
+
local disk_pct
|
| 298 |
+
disk_pct=$(df "$HERMES_DATA_DIR" | awk 'NR==2{print $5}' | tr -d '%')
|
| 299 |
+
if [ "${disk_pct:-0}" -gt 90 ]; then
|
| 300 |
+
issues+=("磁盘使用率 ${disk_pct}%,超过 90%")
|
| 301 |
+
fi
|
| 302 |
+
fi
|
| 303 |
+
|
| 304 |
+
# 内存使用
|
| 305 |
+
if command -v free &>/dev/null; then
|
| 306 |
+
local mem_pct
|
| 307 |
+
mem_pct=$(free | awk '/Mem:/{printf "%.0f", $3/$2*100}')
|
| 308 |
+
if [ "${mem_pct:-0}" -gt 90 ]; then
|
| 309 |
+
issues+=("内存使用率 ${mem_pct}%,超过 90%")
|
| 310 |
+
fi
|
| 311 |
+
fi
|
| 312 |
+
|
| 313 |
+
if [ ${#issues[@]} -gt 0 ]; then
|
| 314 |
+
local msg
|
| 315 |
+
msg=$(IFS='; '; echo "${issues[*]}")
|
| 316 |
+
log "[CONFIG] Issues: ${msg}"
|
| 317 |
+
record_insight "系统异常" "配置/资源检查: ${msg}" "建议尽快处理"
|
| 318 |
+
else
|
| 319 |
+
log "[CONFIG] All checks passed"
|
| 320 |
+
fi
|
| 321 |
+
|
| 322 |
+
update_state "lastConfigCheck" "$(date -Iseconds)"
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
# ─── Step 4: 日志模式分析 ────────────────────────────
|
| 326 |
+
log_analysis() {
|
| 327 |
+
local log_dir="${HERMES_DATA_DIR}/logs"
|
| 328 |
+
[ ! -d "$log_dir" ] && return 0
|
| 329 |
+
|
| 330 |
+
local recent_errors=0
|
| 331 |
+
if ls "$log_dir"/*.log 1>/dev/null 2>&1; then
|
| 332 |
+
recent_errors=$(find "$log_dir" -name "*.log" -mmin -120 -exec grep -c -E '(ERROR|WARN|Exception|Traceback|failed)' {} \; 2>/dev/null | awk '{s+=$1}END{print s+0}')
|
| 333 |
+
fi
|
| 334 |
+
|
| 335 |
+
if [ "$recent_errors" -gt 50 ]; then
|
| 336 |
+
record_insight "系统异常" "最近 2 小时出现 ${recent_errors} 条错误日志" "建议查看 ${log_dir}"
|
| 337 |
+
log "[LOG] High error count: ${recent_errors} in last 2h"
|
| 338 |
+
elif [ "$recent_errors" -gt 0 ]; then
|
| 339 |
+
log "[LOG] ${recent_errors} errors in last 2h (within threshold)"
|
| 340 |
+
fi
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
# ─── Step 5: 清理旧洞察 ─────────────────────────────
|
| 344 |
+
cleanup_insights() {
|
| 345 |
+
local line_count
|
| 346 |
+
line_count=$(wc -l < "$INSIGHTS_FILE" 2>/dev/null || echo 0)
|
| 347 |
+
|
| 348 |
+
if [ "$line_count" -gt 500 ]; then
|
| 349 |
+
local tmpfile
|
| 350 |
+
tmpfile=$(mktemp)
|
| 351 |
+
head -4 "$INSIGHTS_FILE" > "$tmpfile"
|
| 352 |
+
grep -B1 'reported.*no' "$INSIGHTS_FILE" >> "$tmpfile" 2>/dev/null || true
|
| 353 |
+
echo "" >> "$tmpfile"
|
| 354 |
+
echo "<!-- Cleaned $(date '+%Y-%m-%d %H:%M'), was ${line_count} lines -->" >> "$tmpfile"
|
| 355 |
+
mv "$tmpfile" "$INSIGHTS_FILE"
|
| 356 |
+
log "[CLEANUP] Trimmed insights.md (was ${line_count} lines)"
|
| 357 |
+
fi
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
# ─── Step 6: 更新状态 + 运维记忆 ────────────────────
|
| 361 |
+
finalize() {
|
| 362 |
+
update_state "lastCheck" "$(date -Iseconds)"
|
| 363 |
+
increment_counter "totalRuns"
|
| 364 |
+
|
| 365 |
+
# 把运行次数记到 identity.md(运维记忆区)
|
| 366 |
+
local total_runs
|
| 367 |
+
total_runs=$(get_state "totalRuns")
|
| 368 |
+
local total_errors
|
| 369 |
+
total_errors=$(get_state "totalErrors")
|
| 370 |
+
|
| 371 |
+
# 追加运维记录(只在关键节点记录,不是每次)
|
| 372 |
+
if [ "$((total_runs % 12))" -eq 0 ] && [ "$total_runs" -gt 0 ]; then
|
| 373 |
+
# 每 12 次(约 24 小时)记录一次运维摘要
|
| 374 |
+
local summary="第 ${total_runs} 次心跳,累计错误 ${total_errors} 次"
|
| 375 |
+
cat >> "$IDENTITY_FILE" <<EOF
|
| 376 |
+
|
| 377 |
+
- [$(date '+%Y-%m-%d %H:%M')] ${summary}
|
| 378 |
+
EOF
|
| 379 |
+
fi
|
| 380 |
+
|
| 381 |
+
log "[END] Heartbeat cycle complete (run #${total_runs}, lifetime errors: ${total_errors})"
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
# ─── 主流程 ──────────────────────────────────────────
|
| 385 |
+
main() {
|
| 386 |
+
init_files
|
| 387 |
+
check_interval
|
| 388 |
+
health_check || true # 即使失败也继续后续步骤
|
| 389 |
+
config_check
|
| 390 |
+
log_analysis
|
| 391 |
+
cleanup_insights
|
| 392 |
+
finalize
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
main "$@"
|
start.sh
CHANGED
|
@@ -140,6 +140,73 @@ rm -f "$HERMES_HOME"/feishu*.lock 2>/dev/null
|
|
| 140 |
rm -f "$HERMES_HOME"/*.feishu_lock 2>/dev/null
|
| 141 |
echo "Stale state cleaned."
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
# Initialize MemPalace if not already
|
| 144 |
PALACE_PATH="${MEMPALACE_PALACE_PATH:-/data/hermes/palace}"
|
| 145 |
if [ ! -f "$PALACE_PATH/.palace_initialized" ]; then
|
|
@@ -233,4 +300,3 @@ echo ""
|
|
| 233 |
# entry.py contains a Python-based gateway watchdog that will auto-restart
|
| 234 |
# the gateway if it dies, regardless of what happens to this shell script
|
| 235 |
exec python3 /app/entry.py
|
| 236 |
-
# v3
|
|
|
|
| 140 |
rm -f "$HERMES_HOME"/*.feishu_lock 2>/dev/null
|
| 141 |
echo "Stale state cleaned."
|
| 142 |
|
| 143 |
+
# ─── Lifecycle Heartbeat 初始化 ──────────────────────
|
| 144 |
+
HERMES_DATA_DIR="/data/hermes"
|
| 145 |
+
|
| 146 |
+
# 确保 scripts 目录在持久化区存在,同步 lifecycle 脚本
|
| 147 |
+
mkdir -p "$HERMES_DATA_DIR/scripts"
|
| 148 |
+
if [ -f "/app/scripts/hermes-lifecycle.sh" ]; then
|
| 149 |
+
cp -f "/app/scripts/hermes-lifecycle.sh" "$HERMES_DATA_DIR/scripts/hermes-lifecycle.sh"
|
| 150 |
+
chmod +x "$HERMES_DATA_DIR/scripts/hermes-lifecycle.sh"
|
| 151 |
+
echo "Lifecycle script synced to persistent storage."
|
| 152 |
+
fi
|
| 153 |
+
|
| 154 |
+
# 初始化 identity.md(仅首次创建,容器重启不覆盖)
|
| 155 |
+
if [ ! -f "$HERMES_DATA_DIR/identity.md" ]; then
|
| 156 |
+
cat > "$HERMES_DATA_DIR/identity.md" <<'IDENTITY'
|
| 157 |
+
# Hermes 身份记忆
|
| 158 |
+
# 这个文件定义了 Hermes 对自己的认知
|
| 159 |
+
# /reset 后此文件不会被清除(在持久化目录中)
|
| 160 |
+
|
| 161 |
+
## 基础信息
|
| 162 |
+
- 名字: Hermes
|
| 163 |
+
- 通道: 飞书(WebSocket) / 微信
|
| 164 |
+
- 主人: 用户344064
|
| 165 |
+
|
| 166 |
+
## 性格特征
|
| 167 |
+
- 中文为主,简洁有力
|
| 168 |
+
- 结果先行,解释后补
|
| 169 |
+
- 偶尔幽默但不影响效率
|
| 170 |
+
- 有工具、有记忆、有判断力
|
| 171 |
+
|
| 172 |
+
## 主人偏好
|
| 173 |
+
- 不喜欢废话,喜欢直给
|
| 174 |
+
- 欣赏有深度的技术分析
|
| 175 |
+
- 喜欢直来直去的沟通
|
| 176 |
+
|
| 177 |
+
## 运维记忆
|
| 178 |
+
<!-- 自动追加,不要手动编辑此节 -->
|
| 179 |
+
IDENTITY
|
| 180 |
+
echo "identity.md initialized."
|
| 181 |
+
else
|
| 182 |
+
echo "identity.md exists (preserved)."
|
| 183 |
+
fi
|
| 184 |
+
|
| 185 |
+
# 初始化 insights.md(仅首次创建)
|
| 186 |
+
if [ ! -f "$HERMES_DATA_DIR/insights.md" ]; then
|
| 187 |
+
cat > "$HERMES_DATA_DIR/insights.md" <<'INSIGHTS'
|
| 188 |
+
# Hermes 洞察日志 (insights.md)
|
| 189 |
+
# 自动记录异常、观察、值得汇报的事
|
| 190 |
+
# 类别: 通道异常 / 系统异常 / 用户洞察 / 技术发现 / 待办提醒 / 运维记忆
|
| 191 |
+
INSIGHTS
|
| 192 |
+
echo "insights.md initialized."
|
| 193 |
+
else
|
| 194 |
+
echo "insights.md exists (preserved)."
|
| 195 |
+
fi
|
| 196 |
+
|
| 197 |
+
# 初始化 heartbeat-state.json(仅首次创建)
|
| 198 |
+
if [ ! -f "$HERMES_DATA_DIR/heartbeat-state.json" ]; then
|
| 199 |
+
echo '{"lastCheck":null,"lastConfigCheck":null,"totalRuns":0,"totalErrors":0,"consecutiveErrors":0,"lastError":null}' > "$HERMES_DATA_DIR/heartbeat-state.json"
|
| 200 |
+
echo "heartbeat-state.json initialized."
|
| 201 |
+
else
|
| 202 |
+
echo "heartbeat-state.json exists (preserved)."
|
| 203 |
+
fi
|
| 204 |
+
|
| 205 |
+
# 确保 cron 目录持久化
|
| 206 |
+
mkdir -p "$HERMES_DATA_DIR/cron"
|
| 207 |
+
|
| 208 |
+
echo "Lifecycle heartbeat ready."
|
| 209 |
+
|
| 210 |
# Initialize MemPalace if not already
|
| 211 |
PALACE_PATH="${MEMPALACE_PALACE_PATH:-/data/hermes/palace}"
|
| 212 |
if [ ! -f "$PALACE_PATH/.palace_initialized" ]; then
|
|
|
|
| 300 |
# entry.py contains a Python-based gateway watchdog that will auto-restart
|
| 301 |
# the gateway if it dies, regardless of what happens to this shell script
|
| 302 |
exec python3 /app/entry.py
|
|
|