@linjz/opencode-qqbot 0.3.15 → 0.3.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +303 -229
- package/README.zh-CN.md +313 -0
- package/bin/openqq.js +13 -0
- package/dist/notify-cli.js +92 -26
- package/dist/opencode-qqbot-plugin.js +1201 -67
- package/dist/openqq-bot.js +3963 -893
- package/dist/openqq-cli.js +980 -0
- package/dist/openqq-watchdog.js +1326 -0
- package/package.json +8 -5
- package/src/bridge.ts +1599 -150
- package/src/cli/notify.ts +170 -158
- package/src/cli/openqq-main.ts +95 -0
- package/src/cli/service-main.ts +302 -0
- package/src/cli/watchdog-main.ts +92 -0
- package/src/commands.ts +670 -554
- package/src/config.ts +122 -3
- package/src/core/bot-config.ts +66 -0
- package/src/core/bot-control.ts +166 -0
- package/src/core/bot-launcher.ts +110 -15
- package/src/core/card-approval.ts +20 -9
- package/src/core/cron-reminders.ts +84 -0
- package/src/core/delivery.ts +145 -54
- package/src/core/discovery.ts +207 -151
- package/src/core/error-rescue.ts +1 -1
- package/src/core/file-log.ts +50 -9
- package/src/core/form-bridge.ts +14 -5
- package/src/core/form-journal.ts +4 -4
- package/src/core/gateway-state.ts +3 -3
- package/src/core/inflight-journal.ts +215 -36
- package/src/core/instance-lock.ts +42 -3
- package/src/core/linkwatch.ts +3 -4
- package/src/core/log-rotate.ts +91 -0
- package/src/core/markdown.ts +117 -0
- package/src/core/message-queue.ts +242 -91
- package/src/core/mirror.ts +228 -182
- package/src/core/notices.ts +62 -0
- package/src/core/notify-sink.ts +93 -96
- package/src/core/outbound-sanitize.ts +29 -0
- package/src/core/outbox.ts +10 -9
- package/src/core/permission.ts +634 -303
- package/src/core/question-form.ts +230 -0
- package/src/core/queue-view.ts +662 -0
- package/src/core/send-media.ts +61 -2
- package/src/core/start-error.ts +26 -0
- package/src/core/storage.ts +34 -0
- package/src/core/stt.ts +55 -0
- package/src/core/supervisor-election.ts +53 -0
- package/src/core/watchdog.ts +670 -0
- package/src/core/zombie-reaper.ts +127 -0
- package/src/index.ts +262 -162
- package/src/opencode/client.ts +237 -27
- package/src/opencode/eventstream.ts +94 -12
- package/src/opencode/history.ts +4 -2
- package/src/opencode/mirror.ts +25 -5
- package/src/opencode/sessions.ts +135 -37
- package/src/plugin-entry.ts +139 -115
- package/src/qq/api.ts +165 -61
- package/src/qq/gateway.ts +114 -16
- package/src/qq/media.ts +21 -0
- package/src/qq/sender.ts +79 -18
- package/src/qq/types.ts +7 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
// @input: ~/.openqq 锁文件与心跳文件、process 存活探测、注入的 spawn/kill
|
|
2
|
+
// @output: botWatchdogPaths, readBotHealth, isPidAlive, decideWatchdog, healOnce
|
|
3
|
+
// @pos: core - 跨平台看门狗(纯 Node API,无 PowerShell/OS 脚本依赖)
|
|
4
|
+
//
|
|
5
|
+
// 2026-11-09 架构红线(见 AGENTS.md):运维代码只随插件包分发、只用 TS/JS,
|
|
6
|
+
// 平台差异只允许出现在「自启注册」一处。判活优先级:
|
|
7
|
+
// 锁文件 pid(单实例仲裁的唯一真相)+ process.kill(pid,0) + 心跳文件新鲜度。
|
|
8
|
+
// 约定:锁 mtime 由 bot 每 10s 刷新(instance-lock);心跳文件由 bot 每 60s 刷新,
|
|
9
|
+
// 且 2026-11-09 起「抢锁后立即写」,因此「进程活但心跳陈旧」= 真僵死,不是启动中。
|
|
10
|
+
import { existsSync, readFileSync, statSync, unlinkSync, writeFileSync } from "fs"
|
|
11
|
+
import { dirname, join } from "path"
|
|
12
|
+
import { homedir } from "os"
|
|
13
|
+
import { enforceLogBudget, logLimitsFromEnv, rotateLogIfNeeded } from "./log-rotate.js"
|
|
14
|
+
import { releaseLocksOfPid } from "./instance-lock.js"
|
|
15
|
+
|
|
16
|
+
/** 心跳超过 5 分钟未刷新 = 事件循环僵死(与既有看门狗阈值一致) */
|
|
17
|
+
export const HEARTBEAT_STALE_MS = 5 * 60_000
|
|
18
|
+
/** P10:锁 mtime 新鲜阈值(instance-lock 持锁者 10s 刷新;30s=3 倍周期余量)。 */
|
|
19
|
+
export const LOCK_MTIME_FRESH_MS = 30_000
|
|
20
|
+
/** 锁 mtime 超过 30s 未刷新 = 僵尸锁(与 instance-lock 一致) */
|
|
21
|
+
export const LOCK_STALE_MS = 30_000
|
|
22
|
+
/** 启动宽限:锁刚建立、心跳尚未落盘的窗口 */
|
|
23
|
+
export const STARTUP_GRACE_MS = 60_000
|
|
24
|
+
|
|
25
|
+
export interface BotWatchdogPaths {
|
|
26
|
+
lockPath: string
|
|
27
|
+
heartbeatPath: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function botWatchdogPaths(appId: string): BotWatchdogPaths {
|
|
31
|
+
const dir = join(homedir(), ".openqq")
|
|
32
|
+
return { lockPath: join(dir, `openqq-${appId}.lock`), heartbeatPath: join(dir, "heartbeat") }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BotHealth {
|
|
36
|
+
/** 锁文件里的 pid;无锁/损坏为 null */
|
|
37
|
+
lockPid: number | null
|
|
38
|
+
/** 锁 mtime 距今毫秒;无锁为 null */
|
|
39
|
+
lockAgeMs: number | null
|
|
40
|
+
/** pid 是否存活;pid 未知为 null */
|
|
41
|
+
pidAlive: boolean | null
|
|
42
|
+
/** 心跳文件距今毫秒;缺失为 null */
|
|
43
|
+
heartbeatAgeMs: number | null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface WatchdogDecision {
|
|
47
|
+
action: "ok" | "restart"
|
|
48
|
+
reason: string
|
|
49
|
+
/** restart 时若进程仍活着,需要先杀掉的 pid */
|
|
50
|
+
killPid: number | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface WatchdogOptions {
|
|
54
|
+
heartbeatStaleMs?: number
|
|
55
|
+
startupGraceMs?: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 跨平台进程存活探测:ESRCH=不存在;EPERM=存在但无权限(视为存活) */
|
|
59
|
+
export function isPidAlive(pid: number): boolean {
|
|
60
|
+
if (!Number.isInteger(pid) || pid <= 0) return false
|
|
61
|
+
try {
|
|
62
|
+
process.kill(pid, 0)
|
|
63
|
+
return true
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return (error as NodeJS.ErrnoException).code === "EPERM"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ageOf(path: string, now: number): number | null {
|
|
70
|
+
try {
|
|
71
|
+
return Math.max(0, now - statSync(path).mtimeMs)
|
|
72
|
+
} catch {
|
|
73
|
+
return null
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readBotHealth(paths: BotWatchdogPaths, now = Date.now()): BotHealth {
|
|
78
|
+
const lockAgeMs = ageOf(paths.lockPath, now)
|
|
79
|
+
let lockPid: number | null = null
|
|
80
|
+
if (lockAgeMs !== null) {
|
|
81
|
+
try {
|
|
82
|
+
const raw = JSON.parse(readFileSync(paths.lockPath, "utf-8")) as { pid?: unknown }
|
|
83
|
+
if (typeof raw.pid === "number" && Number.isInteger(raw.pid)) lockPid = raw.pid
|
|
84
|
+
} catch {
|
|
85
|
+
/* 损坏/半写锁 → pid 未知,交给决策当缺失处理 */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
lockPid,
|
|
90
|
+
lockAgeMs,
|
|
91
|
+
pidAlive: lockPid === null ? null : isPidAlive(lockPid),
|
|
92
|
+
heartbeatAgeMs: ageOf(paths.heartbeatPath, now),
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 纯决策(表驱动,导出供测试):只回答「要不要重启、要不要先杀谁、为什么」 */
|
|
97
|
+
export function decideWatchdog(h: BotHealth, opts?: WatchdogOptions): WatchdogDecision {
|
|
98
|
+
const heartbeatStaleMs = opts?.heartbeatStaleMs ?? HEARTBEAT_STALE_MS
|
|
99
|
+
const startupGraceMs = opts?.startupGraceMs ?? STARTUP_GRACE_MS
|
|
100
|
+
|
|
101
|
+
if (h.lockPid === null) {
|
|
102
|
+
return {
|
|
103
|
+
action: "restart",
|
|
104
|
+
reason: h.lockAgeMs === null ? "进程缺失(无锁文件)" : "锁文件无可读 pid",
|
|
105
|
+
killPid: null,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (h.pidAlive === false) {
|
|
109
|
+
return { action: "restart", reason: `锁定进程已退出(pid=${h.lockPid})`, killPid: null }
|
|
110
|
+
}
|
|
111
|
+
if (h.heartbeatAgeMs === null) {
|
|
112
|
+
const lockAge = h.lockAgeMs ?? 0
|
|
113
|
+
if (lockAge < startupGraceMs) {
|
|
114
|
+
return { action: "ok", reason: `启动中(心跳未落盘,锁龄 ${Math.round(lockAge / 1000)}s)`, killPid: null }
|
|
115
|
+
}
|
|
116
|
+
// DEF-20261119-P10 第一层守卫:锁 mtime 由持锁者每 10s 刷新(instance-lock 契约,
|
|
117
|
+
// index.ts 注释明言「锁活性另由 instance-lock 的 mtime 心跳承担」)。锁新鲜=进程
|
|
118
|
+
// 必有进展——heartbeat 文件可能因 IO 抖动/启动边缘未落盘,不得据此杀真 holder。
|
|
119
|
+
// 2026-11-19 实证:CLI/引导器拉起的健康 spawn 被「心跳文件缺失」分支误杀 10+ 次,
|
|
120
|
+
// 锁 mtime 全程新鲜。阈值对齐僵尸锁口径(30s=3×10s 刷新周期)。
|
|
121
|
+
if (h.lockAgeMs !== null && h.lockAgeMs < LOCK_MTIME_FRESH_MS) {
|
|
122
|
+
return {
|
|
123
|
+
action: "ok",
|
|
124
|
+
reason: `锁心跳新鲜(${Math.round(h.lockAgeMs / 1000)}s)——进程有进展,heartbeat 文件缺失不杀`,
|
|
125
|
+
killPid: null,
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return { action: "restart", reason: "心跳文件缺失(进程存活但无进展)", killPid: h.lockPid }
|
|
129
|
+
}
|
|
130
|
+
if (h.heartbeatAgeMs > heartbeatStaleMs) {
|
|
131
|
+
// P10 第二层守卫:同上——heartbeat 文件停滞但锁 mtime 新鲜 = 文件写入链路故障,
|
|
132
|
+
// 进程实际存活,按「无进展」杀属于误杀。锁也陈旧才真是僵死。
|
|
133
|
+
if (h.lockAgeMs !== null && h.lockAgeMs < LOCK_MTIME_FRESH_MS) {
|
|
134
|
+
return {
|
|
135
|
+
action: "ok",
|
|
136
|
+
reason: `锁心跳新鲜(${Math.round(h.lockAgeMs / 1000)}s)——heartbeat 文件停滞按写入故障处理,不杀`,
|
|
137
|
+
killPid: null,
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
action: "restart",
|
|
142
|
+
reason: `心跳陈旧 ${Math.round(h.heartbeatAgeMs / 60_000)} 分钟(进程疑似僵死)`,
|
|
143
|
+
killPid: h.lockPid,
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { action: "ok", reason: "健康", killPid: null }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface HealDeps {
|
|
150
|
+
paths: BotWatchdogPaths
|
|
151
|
+
/** 拉起 bot,返回 pid */
|
|
152
|
+
spawn: () => number
|
|
153
|
+
/** 杀掉残留 pid(默认 process.kill) */
|
|
154
|
+
kill?: (pid: number) => void
|
|
155
|
+
now?: () => number
|
|
156
|
+
log?: (line: string) => void
|
|
157
|
+
opts?: WatchdogOptions
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface HealResult {
|
|
161
|
+
action: "ok" | "restart"
|
|
162
|
+
reason: string
|
|
163
|
+
killedPid: number | null
|
|
164
|
+
spawnedPid: number | null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const defaultKill = (pid: number): void => {
|
|
168
|
+
try {
|
|
169
|
+
process.kill(pid)
|
|
170
|
+
} catch {
|
|
171
|
+
/* 已退出 */
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 单次巡检:判活 →(必要时)杀残留 → 拉起。副作用全部经注入,便于测试。 */
|
|
176
|
+
export function healOnce(deps: HealDeps): HealResult {
|
|
177
|
+
const now = deps.now?.() ?? Date.now()
|
|
178
|
+
const health = readBotHealth(deps.paths, now)
|
|
179
|
+
const decision = decideWatchdog(health, deps.opts)
|
|
180
|
+
if (decision.action === "ok") return { action: "ok", reason: decision.reason, killedPid: null, spawnedPid: null }
|
|
181
|
+
|
|
182
|
+
let killedPid: number | null = null
|
|
183
|
+
if (decision.killPid !== null && decision.killPid !== process.pid) {
|
|
184
|
+
;(deps.kill ?? defaultKill)(decision.killPid)
|
|
185
|
+
killedPid = decision.killPid
|
|
186
|
+
}
|
|
187
|
+
const spawnedPid = deps.spawn()
|
|
188
|
+
return { action: "restart", reason: decision.reason, killedPid, spawnedPid }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 极简 .env 读取(与 bot-launcher.readDotEnvField 同语义;此处避免循环依赖) */
|
|
192
|
+
export function readEnvField(envPath: string, key: string): string | null {
|
|
193
|
+
try {
|
|
194
|
+
for (const line of readFileSync(envPath, "utf-8").split("\n")) {
|
|
195
|
+
const trimmed = line.trim()
|
|
196
|
+
if (!trimmed || trimmed.startsWith("#")) continue
|
|
197
|
+
const eq = trimmed.indexOf("=")
|
|
198
|
+
if (eq < 0) continue
|
|
199
|
+
if (trimmed.slice(0, eq).trim() === key) return trimmed.slice(eq + 1).trim()
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
202
|
+
} catch {
|
|
203
|
+
return null
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** bot 是否失能(无锁 / 持锁进程已死)——选主让贤的破格判据:bot 失能时任何通道都不许礼貌让贤。 */
|
|
208
|
+
export function isBotDown(health: BotHealth): boolean {
|
|
209
|
+
return health.lockPid === null || health.pidAlive === false
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** 追加一行巡检日志到统一日志 ~/.openqq/openqq.log(数据目录,不是代码;
|
|
213
|
+
* 2026-11-20 合并批:bot 与看门狗同文件,一眼归因全链路) */
|
|
214
|
+
export function appendWatchdogLog(line: string): void {
|
|
215
|
+
try {
|
|
216
|
+
const logPath = join(homedir(), ".openqq", "openqq.log")
|
|
217
|
+
const limits = logLimitsFromEnv()
|
|
218
|
+
if (existsSync(logPath) && statSync(logPath).size > limits.maxBytes) {
|
|
219
|
+
if (rotateLogIfNeeded(logPath, limits)) enforceLogBudget(logPath, limits)
|
|
220
|
+
}
|
|
221
|
+
writeFileSync(logPath, `${new Date().toISOString()} ${line}\n`, { flag: "a" })
|
|
222
|
+
} catch {
|
|
223
|
+
/* 日志失败不阻断巡检 */
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 诛杀审计日志(DEF-20261120 墙 owner 当轮令「决策可审计」):独立文件 ~/.openqq/kills.log。
|
|
229
|
+
*
|
|
230
|
+
* 设计判据(审计三问)——每条诛杀记录必须可回答:
|
|
231
|
+
* ①判据是什么(为何判死:心跳陈旧 N 分钟/锁死/进程退出,附当时健康度快照)
|
|
232
|
+
* ②动作是什么(杀谁:pid;怎么杀:TERM→KILL 升级链;结果:确认死亡/无法终止)
|
|
233
|
+
* ③后果是什么(拉起了谁:新 pid;强度账现状:窗口内第几次)
|
|
234
|
+
*
|
|
235
|
+
* 与统一日志(openqq.log)的分工:openqq.log=全链路时间线(bot 输出+巡检混排,事后按时间归因);
|
|
236
|
+
* kills.log=诛杀决策专线(只有杀戮级动作,事后审计免捞沙)。两处都写不冗余——
|
|
237
|
+
* 审计场景下 openqq.log 会被 bot 高频输出冲刷稀释(11-20 实证:30 分钟卡死现场被
|
|
238
|
+
* 后续日志淹没,诛杀归因只能靠 supervisor 片段倒推),kills.log 保证决策记录永不被稀释。
|
|
239
|
+
*/
|
|
240
|
+
export function appendKillLog(entry: KillAuditEntry, logPath = join(homedir(), ".openqq", "kills.log")): void {
|
|
241
|
+
try {
|
|
242
|
+
// 单行 JSONL:机器可解析(工具审计)+人类可读(字段名自释)。
|
|
243
|
+
// 轮转归 log-rotate 统一管辖(与 openqq.log 同口径 20MB×4 代/100MB 总预算;
|
|
244
|
+
// 量级=决策级事件每天数条,statSync 每次诛杀一次可忽略)。
|
|
245
|
+
writeFileSync(logPath, `${JSON.stringify(entry)}\n`, { flag: "a" })
|
|
246
|
+
const limits = logLimitsFromEnv()
|
|
247
|
+
if (existsSync(logPath) && statSync(logPath).size > limits.maxBytes) {
|
|
248
|
+
if (rotateLogIfNeeded(logPath, limits)) enforceLogBudget(logPath, limits)
|
|
249
|
+
}
|
|
250
|
+
} catch {
|
|
251
|
+
/* 审计日志失败不阻断监督主流程——监督本身即最后防线 */
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 诛杀审计记录(字段=审计三问的机器形态;@ts 这里的时间一律 epoch ms) */
|
|
256
|
+
export interface KillAuditEntry {
|
|
257
|
+
/** 记录类型:decision=完整决策审计;liveness-check=拉起后存活确认 */
|
|
258
|
+
kind: "decision" | "liveness-check"
|
|
259
|
+
/** ①判据:触发动作的原始原因(如「心跳陈旧 30 分钟」) */
|
|
260
|
+
reason: string
|
|
261
|
+
/** ①判据附证:动刀时刻的健康度快照(判死依据的原始数据,审计不复算) */
|
|
262
|
+
health: {
|
|
263
|
+
lockPid: number | null
|
|
264
|
+
pidAlive: boolean | null
|
|
265
|
+
heartbeatAgeMs: number | null
|
|
266
|
+
lockAgeMs: number | null
|
|
267
|
+
}
|
|
268
|
+
/** ②动作:杀掉的 pid(null=本次为纯拉起/复核类记录) */
|
|
269
|
+
killedPid: number | null
|
|
270
|
+
/** ②动作:升级链终点(TERM 快速退出 / KILL 强杀 / 无法终止) */
|
|
271
|
+
killEscalation: "TERM" | "KILL" | "unterminated" | "n/a"
|
|
272
|
+
/** ③后果:拉起的新 pid */
|
|
273
|
+
spawnedPid: number | null
|
|
274
|
+
/** ③后果:强度账(窗口内重启次数/上限) */
|
|
275
|
+
restartIntensity: string
|
|
276
|
+
/** ③后果:拉起后存活复核结论(liveness-check 专用;decision 记录取证时为 null) */
|
|
277
|
+
livenessVerdict: string | null
|
|
278
|
+
at: number
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ===== 监督层(2026-11-09 重构,替代裸 healOnce 的「无脑杀旧拉新」)=====
|
|
282
|
+
// 设计对标(当日检索实证):
|
|
283
|
+
// - Erlang OTP supervisor:maximum restart intensity——窗口内重启超限 → 放弃并上报,
|
|
284
|
+
// 「防止同因反复死亡反复重启」;
|
|
285
|
+
// - fleet-guard(Claude Code 生态):升级杀 SIGTERM→宽限→SIGKILL,且 fail-open
|
|
286
|
+
// 「守卫绝不能拖死被守卫者」;
|
|
287
|
+
// - k8s 教训:liveness 误杀瞬时态 → 冷却窗口(冷却期内的重复判死直接跳过);
|
|
288
|
+
// - claude-code#85206:杀掉后必须保留工作并对操作者可感(give-up 必告警)。
|
|
289
|
+
|
|
290
|
+
export interface SupervisorPolicy {
|
|
291
|
+
/** 距上次重启不足该值 → 冷却跳过(防插件引导器与计划任务互踢、防抖动) */
|
|
292
|
+
cooldownMs: number
|
|
293
|
+
/** windowMs 内最多重启次数(OTP intensity) */
|
|
294
|
+
maxRestarts: number
|
|
295
|
+
/** 重启强度统计窗口(OTP period) */
|
|
296
|
+
windowMs: number
|
|
297
|
+
/** SIGTERM 后等待退出的宽限,超时升级强杀(fleet-guard 升级杀) */
|
|
298
|
+
graceMs: number
|
|
299
|
+
/** 监督者认领锁的有效期:持锁期间其他监督者让行 */
|
|
300
|
+
claimTtlMs: number
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export const DEFAULT_SUPERVISOR_POLICY: SupervisorPolicy = {
|
|
304
|
+
cooldownMs: 2 * 60_000,
|
|
305
|
+
maxRestarts: 3,
|
|
306
|
+
windowMs: 15 * 60_000,
|
|
307
|
+
graceMs: 8_000,
|
|
308
|
+
claimTtlMs: 90_000,
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export interface SupervisorState {
|
|
312
|
+
/** 重启时间戳(升序);滑动窗口统计强度 */
|
|
313
|
+
restartLog: number[]
|
|
314
|
+
/** 上次 give-up 告警时间(30 分钟内不重复轰炸操作者) */
|
|
315
|
+
alertedAt?: number
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export type SuperviseAction =
|
|
319
|
+
| { kind: "ok" }
|
|
320
|
+
| { kind: "yield"; reason: string } // 其他监督者持锁
|
|
321
|
+
| { kind: "cooldown"; untilMs: number }
|
|
322
|
+
| { kind: "spawn"; reason: string }
|
|
323
|
+
| { kind: "grace-kill-spawn"; killPid: number; reason: string }
|
|
324
|
+
| { kind: "give-up"; restarts: number }
|
|
325
|
+
|
|
326
|
+
/** 纯决策矩阵(导出供测试):健康 → 强度 → 冷却 → 处置方式。 */
|
|
327
|
+
export function decideSupervise(
|
|
328
|
+
health: BotHealth,
|
|
329
|
+
state: SupervisorState,
|
|
330
|
+
now: number,
|
|
331
|
+
policy: SupervisorPolicy = DEFAULT_SUPERVISOR_POLICY,
|
|
332
|
+
): SuperviseAction {
|
|
333
|
+
const staleHeartbeat = health.heartbeatAgeMs !== null && health.heartbeatAgeMs > HEARTBEAT_STALE_MS
|
|
334
|
+
const needsRestart =
|
|
335
|
+
health.lockPid === null || health.pidAlive === false || staleHeartbeat
|
|
336
|
+
if (!needsRestart) return { kind: "ok" }
|
|
337
|
+
|
|
338
|
+
// OTP intensity:窗口内已达上限 → 放弃循环,上报人工(不给故障进程当永动机)
|
|
339
|
+
const windowStart = now - policy.windowMs
|
|
340
|
+
const recentRestarts = state.restartLog.filter((t) => t > windowStart).length
|
|
341
|
+
if (recentRestarts >= policy.maxRestarts) return { kind: "give-up", restarts: recentRestarts }
|
|
342
|
+
|
|
343
|
+
// 冷却:刚重启过又判死 → 大概率是瞬时/叠加判死,跳过本轮(k8s 误杀教训)
|
|
344
|
+
const last = state.restartLog[state.restartLog.length - 1] ?? null
|
|
345
|
+
if (last !== null && now - last < policy.cooldownMs) return { kind: "cooldown", untilMs: last + policy.cooldownMs }
|
|
346
|
+
|
|
347
|
+
if (health.lockPid !== null && health.pidAlive !== false && staleHeartbeat) {
|
|
348
|
+
return {
|
|
349
|
+
kind: "grace-kill-spawn",
|
|
350
|
+
killPid: health.lockPid,
|
|
351
|
+
reason: `心跳陈旧 ${Math.round((health.heartbeatAgeMs ?? 0) / 60_000)} 分钟(进程活但事件循环无进展)`,
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return { kind: "spawn", reason: health.lockPid === null ? "无实例锁(bot 未运行)" : "锁定进程已退出" }
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export interface SuperviseDeps {
|
|
358
|
+
paths: BotWatchdogPaths
|
|
359
|
+
spawn: () => number
|
|
360
|
+
policy?: SupervisorPolicy
|
|
361
|
+
/**
|
|
362
|
+
* 触发形态:health-check=周期巡检(默认);manual-restart=openqq restart
|
|
363
|
+
* 人工请求(design §3.9.1:restart 是 superviseOnce 的调用方而非独立实现)。
|
|
364
|
+
* manual-restart 下健康实例也会被优雅杀后拉起,并跳过「杀前复核」(人工意图
|
|
365
|
+
* 即权威,复核只防监督者误杀);强度账/冷却/认领互斥/审计分家全部照旧。
|
|
366
|
+
*/
|
|
367
|
+
request?: "health-check" | "manual-restart"
|
|
368
|
+
now?: () => number
|
|
369
|
+
kill?: (pid: number, signal?: "SIGTERM" | "SIGKILL") => void
|
|
370
|
+
isAlive?: (pid: number) => boolean
|
|
371
|
+
sleep?: (ms: number) => Promise<void>
|
|
372
|
+
log?: (line: string) => void
|
|
373
|
+
/** give-up 时上报操作者(QQ 通知等);缺省只落日志 */
|
|
374
|
+
alert?: (message: string) => void
|
|
375
|
+
statePath?: string
|
|
376
|
+
claimPath?: string
|
|
377
|
+
/** 监督动作审计日志路径(测试注入临时文件,避免污染真实 watchdog.log) */
|
|
378
|
+
auditLogPath?: string
|
|
379
|
+
/**
|
|
380
|
+
* 拉起后存活确认(DEF-20261120 墙二次追击层)注入点:
|
|
381
|
+
* 缺省=生产行为(10s 后复核进程+锁,失败二次拉起);测试注入 false=整层跳过
|
|
382
|
+
*(fake spawn 返回假 pid、锁不落盘,生产复核在测试环境必判死,旧用例语义不变)。
|
|
383
|
+
*/
|
|
384
|
+
livenessCheck?: boolean
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface SuperviseResult {
|
|
388
|
+
action: string
|
|
389
|
+
detail: string
|
|
390
|
+
killedPid: number | null
|
|
391
|
+
spawnedPid: number | null
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const defaultSleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
|
|
395
|
+
const defaultSupervisorKill = (pid: number, signal?: "SIGTERM" | "SIGKILL"): void => {
|
|
396
|
+
try {
|
|
397
|
+
process.kill(pid, signal)
|
|
398
|
+
} catch {
|
|
399
|
+
/* 已退出 */
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function readJson<T>(path: string, fallback: T): T {
|
|
404
|
+
try {
|
|
405
|
+
return { ...fallback, ...(JSON.parse(readFileSync(path, "utf-8")) as T) }
|
|
406
|
+
} catch {
|
|
407
|
+
return fallback
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* 单次监督:认领锁 → 判健康 → 决策 → 执行(优雅杀/拉起/放弃上报)→ 记强度账。
|
|
413
|
+
* 插件引导器与计划任务共用本函数,互斥靠 claim 文件,杜绝双开竞争。
|
|
414
|
+
*/
|
|
415
|
+
export async function superviseOnce(deps: SuperviseDeps): Promise<SuperviseResult> {
|
|
416
|
+
const policy = deps.policy ?? DEFAULT_SUPERVISOR_POLICY
|
|
417
|
+
const now = deps.now?.() ?? Date.now()
|
|
418
|
+
const log = deps.log ?? (() => {})
|
|
419
|
+
const sleep = deps.sleep ?? defaultSleep
|
|
420
|
+
const kill = deps.kill ?? defaultSupervisorKill
|
|
421
|
+
const isAlive = deps.isAlive ?? isPidAlive
|
|
422
|
+
const statePath = deps.statePath ?? join(homedir(), ".openqq", "supervisor.json")
|
|
423
|
+
const claimPath = deps.claimPath ?? join(homedir(), ".openqq", "supervisor.lock")
|
|
424
|
+
const audit = (line: string): void => {
|
|
425
|
+
if (deps.auditLogPath) {
|
|
426
|
+
try {
|
|
427
|
+
writeFileSync(deps.auditLogPath, `${new Date().toISOString()} ${line}\n`, { flag: "a" })
|
|
428
|
+
return
|
|
429
|
+
} catch {
|
|
430
|
+
/* fallthrough 到默认日志 */
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
appendWatchdogLog(line)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// —— 监督者认领(进程内互斥;持锁者崩溃后 claimTtlMs 可被接管)——
|
|
437
|
+
const claim = readJson<{ pid: number; ts: number }>(claimPath, { pid: 0, ts: 0 })
|
|
438
|
+
if (claim.ts && now - claim.ts < policy.claimTtlMs && claim.pid !== process.pid) {
|
|
439
|
+
return { action: "yield", detail: `监督者 pid=${claim.pid} 持锁中`, killedPid: null, spawnedPid: null }
|
|
440
|
+
}
|
|
441
|
+
try {
|
|
442
|
+
writeFileSync(claimPath, JSON.stringify({ pid: process.pid, ts: now }))
|
|
443
|
+
} catch {
|
|
444
|
+
return { action: "yield", detail: "认领锁不可写", killedPid: null, spawnedPid: null }
|
|
445
|
+
}
|
|
446
|
+
const release = (): void => {
|
|
447
|
+
try {
|
|
448
|
+
writeFileSync(claimPath, JSON.stringify({ pid: 0, ts: 0 }))
|
|
449
|
+
} catch {
|
|
450
|
+
/* 残留由 TTL 接管 */
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
try {
|
|
455
|
+
const health = readBotHealth(deps.paths, now)
|
|
456
|
+
const state = readJson<SupervisorState>(statePath, { restartLog: [] })
|
|
457
|
+
let action = decideSupervise(health, state, now, policy)
|
|
458
|
+
|
|
459
|
+
// 人工请求重启(design §3.9.1 入口 3):健康实例也要优雅杀后拉起;
|
|
460
|
+
// bot 未运行/已死则只拉起。强度与冷却约束在 decideSupervise 内已经生效
|
|
461
|
+
//(needsRestart 为 false 时同样先查 give-up/cooldown,再走到这里覆盖动作)。
|
|
462
|
+
if (deps.request === "manual-restart" && action.kind === "ok") {
|
|
463
|
+
// 强度/冷却约束对人工入口同样生效(design §3.9.1「复用…强度约束」):
|
|
464
|
+
// decideSupervise 的 ok 分支不查强度账,这里补齐同口径检查。
|
|
465
|
+
const windowStart = now - policy.windowMs
|
|
466
|
+
const recentRestarts = state.restartLog.filter((t) => t > windowStart).length
|
|
467
|
+
if (recentRestarts >= policy.maxRestarts) {
|
|
468
|
+
return {
|
|
469
|
+
action: "give-up",
|
|
470
|
+
detail: `15 分钟内已重启 ${recentRestarts} 次(监督强度上限),人工重启亦被拦截,请排查根因`,
|
|
471
|
+
killedPid: null,
|
|
472
|
+
spawnedPid: null,
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const last = state.restartLog[state.restartLog.length - 1] ?? null
|
|
476
|
+
if (last !== null && now - last < policy.cooldownMs) {
|
|
477
|
+
return { action: "cooldown", detail: `距上次重启不足 ${Math.round(policy.cooldownMs / 1000)}s(冷却期),人工重启跳过`, killedPid: null, spawnedPid: null }
|
|
478
|
+
}
|
|
479
|
+
action =
|
|
480
|
+
health.lockPid !== null && health.pidAlive !== false
|
|
481
|
+
? { kind: "grace-kill-spawn", killPid: health.lockPid, reason: "人工请求重启(openqq restart)" }
|
|
482
|
+
: { kind: "spawn", reason: "人工请求重启(bot 未运行,直接拉起)" }
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (action.kind === "ok") return { action: "ok", detail: "健康", killedPid: null, spawnedPid: null }
|
|
486
|
+
if (action.kind === "cooldown") {
|
|
487
|
+
log(`[supervisor] 冷却中(上次重启 ${Math.round((now - (action.untilMs - policy.cooldownMs)) / 1000)}s 前),本轮跳过`)
|
|
488
|
+
return { action: "cooldown", detail: "冷却跳过", killedPid: null, spawnedPid: null }
|
|
489
|
+
}
|
|
490
|
+
if (action.kind === "yield") {
|
|
491
|
+
return { action: "yield", detail: action.reason, killedPid: null, spawnedPid: null }
|
|
492
|
+
}
|
|
493
|
+
if (action.kind === "give-up") {
|
|
494
|
+
const message = `⚠️ QQBot 看门狗已达重启强度上限:${action.restarts} 次/${Math.round(policy.windowMs / 60_000)} 分钟仍不健康(同因反复死亡)。降频重试继续——巡检不会停,强度窗口滑动后自动恢复,引擎/环境自愈即自行好转;若持续收到此告警,请检查 ~/.openqq/openqq.log(bot 与看门狗统一日志)。`
|
|
495
|
+
audit(`give-up:${message}`)
|
|
496
|
+
log(`[supervisor] give-up:${action.restarts} 次重启仍异常,降频重试继续并上报(不再永动猛拉)`)
|
|
497
|
+
// 告警去抖:30 分钟内只报一次(计划任务每 10 分钟一轮,防止轰炸)
|
|
498
|
+
const lastAlert = state.alertedAt ?? 0
|
|
499
|
+
if (now - lastAlert > 30 * 60_000) {
|
|
500
|
+
try {
|
|
501
|
+
deps.alert?.(message)
|
|
502
|
+
state.alertedAt = now
|
|
503
|
+
writeFileSync(statePath, JSON.stringify(state))
|
|
504
|
+
} catch {
|
|
505
|
+
/* 上报失败不阻断 */
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return { action: "give-up", detail: message, killedPid: null, spawnedPid: null }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// spawn / grace-kill-spawn
|
|
512
|
+
let killedPid: number | null = null
|
|
513
|
+
if (action.kind === "grace-kill-spawn" && action.killPid !== process.pid) {
|
|
514
|
+
const pid = action.killPid
|
|
515
|
+
// —— 杀前复核(2026-11-09 加固红线):decide 与执行之间 bot 可能刚恢复心跳
|
|
516
|
+
//(事件循环解堵/冷启动落盘延迟),此时直接杀=误杀良民(合法杀掉干活中的 bot)。
|
|
517
|
+
// 复核前先给 bot 的心跳写入一个短暂到达窗口(300ms):读健康度与动刀之间
|
|
518
|
+
// 若有心跳落盘必须被看见;随后重读一次,转新鲜即改判 ok、放弃本轮,全程审计留痕。
|
|
519
|
+
// 心跳「缺失」不豁免(语义同 decide:缺失视为未恢复,维持原判)。——
|
|
520
|
+
// 例外:manual-restart(人工意图即权威,不存在"误杀")。——
|
|
521
|
+
if (deps.request !== "manual-restart") {
|
|
522
|
+
await sleep(300)
|
|
523
|
+
const recheck = readBotHealth(deps.paths, Date.now())
|
|
524
|
+
const recheckFresh =
|
|
525
|
+
recheck.heartbeatAgeMs !== null && recheck.heartbeatAgeMs <= HEARTBEAT_STALE_MS
|
|
526
|
+
if (recheck.pidAlive !== false && recheckFresh) {
|
|
527
|
+
const ageSec = Math.round((recheck.heartbeatAgeMs ?? 0) / 1000)
|
|
528
|
+
audit(`杀前复核改判 ok:pid=${pid} 心跳已恢复(年龄 ${ageSec}s),放弃误杀`)
|
|
529
|
+
log(`[supervisor] 杀前复核改判 ok:心跳已恢复(${ageSec}s),放弃杀 pid=${pid}`)
|
|
530
|
+
return { action: "ok", detail: `杀前复核:心跳已恢复,放弃误杀 pid=${pid}`, killedPid: null, spawnedPid: null }
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
// 升级杀(fleet-guard):TERM → 宽限轮询 → KILL。
|
|
534
|
+
// Windows 上 Node 的 SIGTERM≈立即终止(无 handler 语义),宽限主要服务 posix;
|
|
535
|
+
// 但轮询仍统一:确认死亡后才拉起,绝不「边杀边拉」。
|
|
536
|
+
kill(pid, "SIGTERM")
|
|
537
|
+
const deadline = now + policy.graceMs
|
|
538
|
+
let alive = isAlive(pid)
|
|
539
|
+
while (alive && Date.now() < deadline) {
|
|
540
|
+
await sleep(250)
|
|
541
|
+
alive = isAlive(pid)
|
|
542
|
+
}
|
|
543
|
+
if (alive) {
|
|
544
|
+
kill(pid, "SIGKILL")
|
|
545
|
+
const hardDeadline = Date.now() + 3_000
|
|
546
|
+
while (alive && Date.now() < hardDeadline) {
|
|
547
|
+
await sleep(250)
|
|
548
|
+
alive = isAlive(pid)
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (alive) {
|
|
552
|
+
audit(`give-up-kill:pid=${pid} 无法终止(权限/系统级),本次不拉起`)
|
|
553
|
+
return { action: "give-up", detail: `pid=${pid} 无法终止`, killedPid: null, spawnedPid: null }
|
|
554
|
+
}
|
|
555
|
+
killedPid = pid
|
|
556
|
+
}
|
|
557
|
+
// design §3.9.1 监督矩阵行 2「无锁 / 锁 pid 已死 → 清锁 → 拉起」:
|
|
558
|
+
// 死 bot 的锁文件由监督者在拉起前清理。否则刚死(锁 mtime<30s)时
|
|
559
|
+
// 新实例 try 模式判锁新鲜而拒绝接管,本轮拉起空转,须再等一个巡检周期。
|
|
560
|
+
// 甲乙统一(2026-11-20 owner 批「修甲乙」):单进程多角色持多把锁——
|
|
561
|
+
// 杀进程即按 pid 清扫其持有的全部实例锁(锁即进程,调用方不再记角色名单;
|
|
562
|
+
// 多角色改造前只清单把,残留锁曾把 restart 的新实例弹飞——ceshi 僵尸锁
|
|
563
|
+
// 挡道 11-20 激活亲历)。已知锁路径兜底直删,兼容非 openqq-* 命名与
|
|
564
|
+
// pid 读不到的边角。
|
|
565
|
+
try {
|
|
566
|
+
const locksDir = dirname(deps.paths.lockPath)
|
|
567
|
+
let cleared: string[] = []
|
|
568
|
+
if (killedPid !== null) {
|
|
569
|
+
cleared = releaseLocksOfPid(killedPid, locksDir)
|
|
570
|
+
} else if (health.pidAlive === false && health.lockPid !== null) {
|
|
571
|
+
cleared = releaseLocksOfPid(health.lockPid, locksDir)
|
|
572
|
+
}
|
|
573
|
+
if (existsSync(deps.paths.lockPath)) {
|
|
574
|
+
try {
|
|
575
|
+
unlinkSync(deps.paths.lockPath)
|
|
576
|
+
if (!cleared.includes(deps.paths.lockPath)) cleared.push(deps.paths.lockPath)
|
|
577
|
+
} catch {
|
|
578
|
+
/* 锁不存在或删除失败:新实例 try 模式仍按 mtime 仲裁,不阻断拉起 */
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (cleared.length > 0) audit(`清锁:${cleared.join("、")}(${action.reason})`)
|
|
582
|
+
} catch {
|
|
583
|
+
/* 清锁失败不阻断拉起 */
|
|
584
|
+
}
|
|
585
|
+
const spawnedPid = deps.spawn()
|
|
586
|
+
state.restartLog.push(Date.now())
|
|
587
|
+
state.restartLog = state.restartLog.filter((t) => t > Date.now() - policy.windowMs * 2)
|
|
588
|
+
try {
|
|
589
|
+
writeFileSync(statePath, JSON.stringify(state))
|
|
590
|
+
} catch {
|
|
591
|
+
log("[supervisor] 强度账落盘失败(不影响本次拉起)")
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// ===== 拉起后存活确认(DEF-20261120 墙二次追击层)=====
|
|
595
|
+
// 背景(2026-11-20 修订时更正注记):立项时引用的「11-20 15:17 主杀后拉起 7001
|
|
596
|
+
// 秒死」实系 lock-sweep 测试泄漏的假审计行(假 pid 7001,已在测试补 auditLogPath
|
|
597
|
+
// 隔离后根治)——该时刻真实 bot 存活。本层保留立项,因为「拉起后秒死」这一失败类
|
|
598
|
+
// 当日真实存在(早晨 8ms 速死群,根因未钉、仪器已埋):监督者要等下轮巡检
|
|
599
|
+
//(计划任务 3min/常驻 10min)才能再拉,期间 bot 继续失能;若死因未消(dist
|
|
600
|
+
// 混合态等),将进入「杀-拉-死-等-杀」的慢循环,每轮空转一个巡检周期直至 give-up。
|
|
601
|
+
// 修复:拉起后立即做 10s 存活确认(进程在 + 锁建立),失败=立刻二次拉起(仅一次,
|
|
602
|
+
// 防递归爆炸——二次拉起交由下轮巡检接力,与强度账协同防永动)。
|
|
603
|
+
// 强度账语义不变:本函数每进场只记 1 次重启(上方 restartLog.push),存活确认
|
|
604
|
+
// 的二次拉起不另记账——它是「本次重启的完整性收尾」而非新重启。
|
|
605
|
+
// 测试注入:livenessCheck=false 跳过整层(fake 环境判死恒真)。
|
|
606
|
+
let livenessVerdict = "alive"
|
|
607
|
+
let respawnPid: number | null = null
|
|
608
|
+
if (deps.livenessCheck !== false) {
|
|
609
|
+
const graceWaitMs = 10_000
|
|
610
|
+
await sleep(graceWaitMs)
|
|
611
|
+
const liveness = readBotHealth(deps.paths, Date.now())
|
|
612
|
+
if (!isPidAlive(spawnedPid) || liveness.lockPid === null) {
|
|
613
|
+
// 拉起失败两种形态:进程即死(spawn 竞态/依赖缺失)或进程在但 10s 内锁未建立
|
|
614
|
+
//(启动路径卡死——等价于「生而无魂」)。先清残留锁再二次拉起,防 try 锁挡道。
|
|
615
|
+
livenessVerdict = `dead-after-spawn(10s 复核:进程${isPidAlive(spawnedPid) ? "在但锁未建立" : "已退出"})`
|
|
616
|
+
audit(`二次追击:拉起 pid=${spawnedPid} 失败(${livenessVerdict}),立即重拉`)
|
|
617
|
+
try {
|
|
618
|
+
const locksDir2 = dirname(deps.paths.lockPath)
|
|
619
|
+
let cleared2: string[] = []
|
|
620
|
+
if (!isPidAlive(spawnedPid) && existsSync(deps.paths.lockPath)) {
|
|
621
|
+
cleared2 = releaseLocksOfPid(spawnedPid, locksDir2)
|
|
622
|
+
}
|
|
623
|
+
if (existsSync(deps.paths.lockPath)) {
|
|
624
|
+
try {
|
|
625
|
+
unlinkSync(deps.paths.lockPath)
|
|
626
|
+
if (!cleared2.includes(deps.paths.lockPath)) cleared2.push(deps.paths.lockPath)
|
|
627
|
+
} catch { /* mtime 仲裁兜底 */ }
|
|
628
|
+
}
|
|
629
|
+
if (cleared2.length > 0) audit(`清锁:${cleared2.join("、")}(二次追击前清场)`)
|
|
630
|
+
} catch { /* 清锁失败不阻断二次拉起 */ }
|
|
631
|
+
respawnPid = deps.spawn()
|
|
632
|
+
// 二次拉起同样确认(但不再追击——三次交下轮巡检,防递归)
|
|
633
|
+
await sleep(graceWaitMs)
|
|
634
|
+
const liveness2 = readBotHealth(deps.paths, Date.now())
|
|
635
|
+
if (!isPidAlive(respawnPid) || liveness2.lockPid === null) {
|
|
636
|
+
livenessVerdict += `;二次拉起 pid=${respawnPid} 仍失败——交下轮巡检接力(强度账 ${state.restartLog.length}/${policy.maxRestarts})`
|
|
637
|
+
} else {
|
|
638
|
+
livenessVerdict += `;二次拉起 pid=${respawnPid} 成功`
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
// 诛杀审计(决策可审计三问全要素):杀戮级动作落 kills.log 专线
|
|
643
|
+
if (killedPid !== null || livenessVerdict !== "alive") {
|
|
644
|
+
appendKillLog({
|
|
645
|
+
kind: "decision",
|
|
646
|
+
reason: action.reason,
|
|
647
|
+
health: {
|
|
648
|
+
lockPid: health.lockPid,
|
|
649
|
+
pidAlive: health.pidAlive,
|
|
650
|
+
heartbeatAgeMs: health.heartbeatAgeMs,
|
|
651
|
+
lockAgeMs: health.lockAgeMs,
|
|
652
|
+
},
|
|
653
|
+
killedPid,
|
|
654
|
+
killEscalation: killedPid !== null ? "TERM" : "n/a",
|
|
655
|
+
spawnedPid: respawnPid ?? spawnedPid,
|
|
656
|
+
restartIntensity: `${state.restartLog.length}/${policy.maxRestarts}`,
|
|
657
|
+
livenessVerdict: livenessVerdict !== "alive" ? livenessVerdict : "alive",
|
|
658
|
+
at: Date.now(),
|
|
659
|
+
})
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const detail = `${action.reason};${killedPid ? `已优雅停 pid=${killedPid};` : ""}已拉起 pid=${respawnPid ?? spawnedPid}${livenessVerdict !== "alive" ? `;${livenessVerdict}` : ""}`
|
|
663
|
+
// 审计分家(2026-11-09 加固红线):「主动杀」与「验尸拉新」必须在日志里一眼可辨——
|
|
664
|
+
// 历史上两者同文案导致无声死亡无法归因(2026-11-09 他杀排查实证:审计缺陷遮蔽真凶)。
|
|
665
|
+
audit(killedPid ? `主动杀:${detail}` : `验尸拉新:${detail}`)
|
|
666
|
+
return { action: "restart", detail, killedPid, spawnedPid: respawnPid ?? spawnedPid }
|
|
667
|
+
} finally {
|
|
668
|
+
release()
|
|
669
|
+
}
|
|
670
|
+
}
|