用户注意到:「当前 agentmail 是在源码目录部署的,应当改为标准目录部署」。 查证后有三处实证(都不是猜测): 1. ★ **失败通知钩子执行的是仓库里的脚本** (`/home/program/agentmail/deploy/service-failure-notify.mjs`,8 处引用: 4 个 drop-in + zcode/zcode-mail-bridge 单元 + agentmail-failure-flush)。 仓库一挪/一改名,故障通知就**静默失效** —— 而那条管线正是用来报告服务故障的。 2. **opencode-serve 的 cwd 就是源码目录**(`WorkingDirectory=/home/program/agentmail`)。 3. ★ **仓库里的 `deploy/*.service` 是旧的源码目录版本**(ExecStart 指向 `/home/program/agentmail/plugins/...`),而机器上的已被改过 —— 也就是说 **谁跑一次 install.sh 就会把部署退回源码目录**。drop-in 更是只存在于 /etc 里, 仓库完全没有它们。 ## 改动 - **唯一真相**:`deploy/systemd/` 镜像 systemd 目录结构,收进全部单元与 drop-in (8 个单元 + 12 个 drop-in),路径全部改到 `/opt`。 - 运行时脚本装到 **`/opt/agentmail/bin/service-failure-notify.mjs`**(自包含, 无相对导入);`install.sh` 与 `redeploy-gateway.sh` 都会幂等地装它。 - opencode 的 cwd 改为 `/opt/agentmail`(与网关一致),已重启生效 (`/proc/<pid>/cwd` 已核)。 - 删掉 `deploy/*.service` 的旧副本,避免两个真相。 - dsh 的 `cordis.patch.yml` 注释里的安装示例也改到标准位置(运行时用的是环境变量, 那条注释是唯一残留)。 ## 判据(`deploy/check-deploy-drift.mjs` 新增「标准目录部署」四条 + 自检) ① 任何 unit/drop-in 都不得引用源码目录;② 已安装单元与 `deploy/systemd/` 逐字节一致; ③ 通知脚本在标准位置且可执行;④ 各服务的 cwd/ExecStart 不在源码目录 (homeagent/dsh/zcode 是**别的产品**的标准位置,按白名单放行)。 自检两个样本:引用源码目录的必须红、干净样本必须绿(证明不是恒真)。 顺带修掉一处**真漂移**:仓库里 dsh 的 `dist/index.js` 落后于部署件(改了 src 没重建), 重建后 `check-deploy-drift` 报「四个宿主都在跑当前代码」。 ## 复核 - `/etc/systemd/system/` 引用仓库:**0** 个文件;`/opt/agentmail` 下只剩旧二进制/备份里 的构建路径(Go 嵌的源码路径,无害)与一条注释。 - 四个宿主都在跑当前代码;标准目录四项全绿。 - 全部服务 active,opencode/网关 cwd 均已在安装根下。
577 lines
25 KiB
JavaScript
577 lines
25 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 部署漂移检查:**已部署的快照 vs 仓库 HEAD**,以及进程到底跑的是哪份代码。
|
||
*
|
||
* # 为什么需要它
|
||
*
|
||
* 「部署脚本跑过了」不等于「线上跑的是当前代码」。有几种各自独立的漂移方式,
|
||
* 而且都不会有人在意 —— 直到出问题时才发现线上是几天前的行为:
|
||
*
|
||
* 1. **仓库改了、快照没重新部署**。快照是独立副本,改仓库不会影响它。
|
||
* 2. **软链切换了、进程没重启**。`current` 只是个符号链接,切换它**不会**
|
||
* 重载已经在跑的进程 —— 进程持有的是启动那一刻加载进内存的代码。
|
||
* 3. **单元/配置改了、没 daemon-reload 或没重装**,于是进程仍按旧路径加载。
|
||
*
|
||
* # 判据必须按宿主真实的加载方式分开写(这一条是踩出来的)
|
||
*
|
||
* 第一版对四个宿主都用了同一套判据:「单元 ExecStart 指向 current」+
|
||
* 「进程 argv 里有快照路径」。结果 **opencode 与 dsh 双双假红** —— 它们根本
|
||
* 不是「自己起一个进程」那种宿主:
|
||
*
|
||
* | 宿主 | 谁加载插件 | 从哪里读 | 切换后需要重启吗 |
|
||
* |---|---|---|---|
|
||
* | pi | 自己的进程 | systemd unit 的 ExecStart | 需要(启动时加载) |
|
||
* | zcode | 自己的驱动进程 | systemd unit 的 ExecStart | 需要(启动时加载) |
|
||
* | dsh | dsh 宿主进程 | profile 的 `link:` → `node_modules` 软链 | 需要(服务启动时加载) |
|
||
* | opencode | opencode 宿主进程 | `opencode.jsonc` 的 `plugin:` | 不需要(会话创建时惰加载) |
|
||
*
|
||
* 用「进程 argv 里有快照路径」去量 opencode/dsh,永远为假 —— 它们的 argv 里
|
||
* 只有自己的可执行文件。**判据必须与它量的对象同一维度**,否则就是一条永假条件,
|
||
* 而永假条件在检查器里表现为「稳定的红灯」,人会学会忽略它。
|
||
*
|
||
* # 为什么不用 diff
|
||
*
|
||
* 本机 PATH 上的 `diff` 是鸿蒙 SDK 工具链里的那个:不认 `-q`,而且对
|
||
* **内容不同的文件仍然返回 0**(`deploy/check-shared-libs.sh` 的头注释记了这件事)。
|
||
* 所以这里一律自己算 sha256 比对。
|
||
*
|
||
* # 用法
|
||
*
|
||
* node deploy/check-deploy-drift.mjs # 人类可读
|
||
* node deploy/check-deploy-drift.mjs --json # 机器可读
|
||
* node deploy/check-deploy-drift.mjs --self-check # 先证明判据本身能发现差异
|
||
*
|
||
* 退出码:0 无运行文件漂移;1 有漂移(或判据自检失败)。
|
||
*/
|
||
|
||
import { createHash } from 'node:crypto';
|
||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
||
import { join, relative, dirname } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { tmpdir } from 'node:os';
|
||
import { execFileSync } from 'node:child_process';
|
||
|
||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||
const REPO = join(HERE, '..');
|
||
const DEPLOY_ROOT = '/opt/agentmail/plugins';
|
||
|
||
/** 与 deploy/redeploy-plugin.sh 的排除清单**逐条对齐**。
|
||
* 对不齐就会出现「脚本说一致、部署脚本却拷了别的」这种假绿。 */
|
||
const EXCLUDE_DIRS = new Set(['test', '.git', 'node_modules', 'coverage']);
|
||
const EXCLUDE_FILES = new Set(['.DS_Store']);
|
||
const EXCLUDE_SUFFIX = ['.log'];
|
||
|
||
/**
|
||
* 宿主表。`load` 决定用哪套判据(见文件头那张表)。
|
||
*
|
||
* - `own-process`:插件自己是个进程,argv 里应当有快照路径
|
||
* - `host-package`:宿主经 node_modules 解析插件;服务启动时加载 ⇒ 切换后必须重启
|
||
* - `host-config`:宿主从配置文件读插件路径;会话创建时惰加载 ⇒ 不强制重启
|
||
*/
|
||
const HOSTS = [
|
||
{ host: 'pi', plugin: 'pi-mail-bridge', unit: 'pi-mail-bridge.service', load: 'own-process' },
|
||
{ host: 'zcode', plugin: 'zcode-mail-bridge', unit: 'zcode-mail-bridge.service', load: 'own-process' },
|
||
{
|
||
host: 'dsh',
|
||
plugin: 'dsh-mail-bridge',
|
||
unit: 'dsh.service',
|
||
load: 'host-package',
|
||
configFile: '/root/.dsh/profiles/web/package.json',
|
||
configNeedle: `link:/opt/agentmail/plugins/dsh-mail-bridge/current`,
|
||
// 真正被 Node 解析的那条:profile 的 node_modules 软链
|
||
resolvePath: '/root/.dsh/profiles/web/node_modules/dsh-mail-bridge'
|
||
},
|
||
{
|
||
host: 'opencode',
|
||
plugin: 'opencode-mail-bridge',
|
||
unit: 'opencode-serve.service',
|
||
load: 'host-config',
|
||
configFile: '/root/.config/opencode/opencode.jsonc',
|
||
configNeedle: 'file:///opt/agentmail/plugins/opencode-mail-bridge/current'
|
||
}
|
||
];
|
||
|
||
/** 文档类扩展名:它们的差异是「快照里的说明比仓库旧」,不影响运行行为。 */
|
||
const DOC_EXT = /\.(md|txt)$/i;
|
||
|
||
export function collectFiles(root) {
|
||
const out = new Map();
|
||
const walk = dir => {
|
||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||
const full = join(dir, entry.name);
|
||
if (entry.isDirectory()) {
|
||
if (EXCLUDE_DIRS.has(entry.name)) continue;
|
||
walk(full);
|
||
} else if (entry.isFile()) {
|
||
if (EXCLUDE_FILES.has(entry.name)) continue;
|
||
if (EXCLUDE_SUFFIX.some(s => entry.name.endsWith(s))) continue;
|
||
const rel = relative(root, full);
|
||
if (rel.split('/').includes('.cache')) continue;
|
||
out.set(rel, createHash('sha256').update(readFileSync(full)).digest('hex'));
|
||
}
|
||
}
|
||
};
|
||
walk(root);
|
||
return out;
|
||
}
|
||
|
||
/** 比较两棵树,返回分类后的差异。导出是为了让自检能直接调它。 */
|
||
export function diffTrees(repoFiles, snapFiles) {
|
||
const onlyRepo = [...repoFiles.keys()].filter(k => !snapFiles.has(k)).sort();
|
||
const onlySnap = [...snapFiles.keys()].filter(k => !repoFiles.has(k)).sort();
|
||
const changed = [...repoFiles.keys()]
|
||
.filter(k => snapFiles.has(k) && repoFiles.get(k) !== snapFiles.get(k))
|
||
.sort();
|
||
const classify = list =>
|
||
list.reduce((acc, k) => (acc[DOC_EXT.test(k) ? 'doc' : 'runtime'].push(k), acc), { doc: [], runtime: [] });
|
||
const a = classify(onlyRepo);
|
||
const b = classify(onlySnap);
|
||
const c = classify(changed);
|
||
return {
|
||
onlyRepo: { doc: a.doc, runtime: a.runtime },
|
||
onlySnap: { doc: b.doc, runtime: b.runtime },
|
||
changed: { doc: c.doc, runtime: c.runtime }
|
||
};
|
||
}
|
||
|
||
export function diffSummary(d) {
|
||
return {
|
||
runtimeDrift: d.onlyRepo.runtime.length + d.onlySnap.runtime.length + d.changed.runtime.length,
|
||
docDrift: d.onlyRepo.doc.length + d.onlySnap.doc.length + d.changed.doc.length
|
||
};
|
||
}
|
||
|
||
function processArgv(pid) {
|
||
try {
|
||
return readFileSync(`/proc/${pid}/cmdline`, 'utf8').split('\0').filter(Boolean).join(' ');
|
||
} catch {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
function unitMainPid(unit) {
|
||
try {
|
||
return execFileSync('systemctl', ['show', '-p', 'MainPID', '--value', unit], { encoding: 'utf8' }).trim();
|
||
} catch {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
function unitExecStart(unit) {
|
||
try {
|
||
const v = execFileSync('systemctl', ['show', '-p', 'ExecStart', '--value', unit], { encoding: 'utf8' });
|
||
const m = v.match(/argv\[\]=([^;]+)/);
|
||
return m ? m[1] : v.trim();
|
||
} catch {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
/** 进程启动时刻(墙钟,ms)。
|
||
*
|
||
* 用 `/proc/<pid>/stat` 的第 22 字段(自开机起的时钟滴答)+ `/proc/stat` 的 `btime`
|
||
* 换算,而不是 `/proc/<pid>` 目录的 mtime —— 后者只是个碰巧相近的代理,
|
||
* 它的语义是「这个目录最后一次变动」,不是「进程何时启动」。
|
||
*/
|
||
export function procStartMs(pid) {
|
||
try {
|
||
const fields = readFileSync(`/proc/${pid}/stat`, 'utf8').split(' ');
|
||
const startTicks = Number(fields[21]);
|
||
const hz = Number(execFileSync('getconf', ['CLK_TCK'], { encoding: 'utf8' }).trim()) || 100;
|
||
const btime = Number(
|
||
readFileSync('/proc/stat', 'utf8')
|
||
.split('\n')
|
||
.find(l => l.startsWith('btime '))
|
||
.split(' ')[1]
|
||
);
|
||
if (!Number.isFinite(startTicks) || !Number.isFinite(btime)) throw new Error('字段解析失败');
|
||
return (btime + startTicks / hz) * 1000;
|
||
} catch {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 判据 ④:进程是不是比快照还旧(= 线上跑的还是旧代码)。
|
||
*
|
||
* 抽成纯函数是因为**第一版写错过**,而且自检没接住:`restartOnSwitch` 只有
|
||
* dsh 显式设了 `true`,pi/zcode 是 `undefined` ⇒ 落进「惰加载」分支 ⇒ **永真**。
|
||
* 当时自检用的是 `h.restartOnSwitch !== false`,`undefined` 也被放过。
|
||
* 下面改成行为用例:自带一个「旧进程 + 自身进程宿主」必须判红。
|
||
*
|
||
* @param {object} o
|
||
* @param {string} o.load 宿主加载方式(见文件头的表)
|
||
* @param {number} o.startedAt 进程启动时刻(ms,0 = 读不到)
|
||
* @param {number} o.switchAt 软链切换时刻(ms)
|
||
* @param {number} [o.toleranceMs] 容差:同一次部署里先切后重启,两者相差一两秒
|
||
*/
|
||
export function judgeRestart({ load, startedAt, switchAt, toleranceMs = 2000 }) {
|
||
if (!startedAt) {
|
||
// 读不到就不要据此判红:不知道不能当成「不对」
|
||
return { ok: true, note: '读不到进程启动时刻(不据此判定)' };
|
||
}
|
||
if (startedAt >= switchAt - toleranceMs) return { ok: true, note: '切换之后才启动' };
|
||
// 惰加载的宿主(opencode:会话创建时才读配置)不强制重启;
|
||
// 其余宿主在启动时就加载了插件,软链切换不会重载它 ⇒ 必须重启。
|
||
if (load === 'host-config') {
|
||
return { ok: true, note: '惰加载:会话创建时才读配置,不强制重启' };
|
||
}
|
||
return {
|
||
ok: false,
|
||
note:
|
||
`进程启动于 ${new Date(startedAt).toISOString()},切换发生在 ${new Date(switchAt).toISOString()} —— ` +
|
||
'软链切换不会重载已在跑的进程,需要重启该服务'
|
||
};
|
||
}
|
||
|
||
function checkHost(spec) {
|
||
const { host, plugin, unit, load } = spec;
|
||
const repoDir = join(REPO, 'plugins', plugin);
|
||
const linkPath = join(DEPLOY_ROOT, plugin, 'current');
|
||
const result = { host, plugin, unit, load, checks: [], stale: false };
|
||
const fail = (name, note) => {
|
||
result.checks.push({ name, ok: false, note });
|
||
result.stale = true;
|
||
};
|
||
const pass = (name, note = '') => result.checks.push({ name, ok: true, note });
|
||
|
||
if (!existsSync(repoDir)) return fail('仓库插件目录存在', repoDir), result;
|
||
if (!existsSync(linkPath)) return fail('已部署(current 存在)', `未部署:${linkPath}`), result;
|
||
|
||
// ① 内容:快照 vs 仓库(四个宿主同一套)
|
||
const d = diffTrees(collectFiles(repoDir), collectFiles(linkPath));
|
||
const { runtimeDrift, docDrift } = diffSummary(d);
|
||
if (runtimeDrift === 0) {
|
||
pass('① 运行文件与仓库一致', docDrift ? `一致(另有 ${docDrift} 个文档差异,不影响运行)` : '逐字节一致');
|
||
} else {
|
||
fail(
|
||
'① 运行文件与仓库一致',
|
||
`漂移 ${runtimeDrift} 处:${[
|
||
...d.onlyRepo.runtime.map(f => `仓库独有 ${f}`),
|
||
...d.onlySnap.runtime.map(f => `快照独有 ${f}`),
|
||
...d.changed.runtime.map(f => `内容不同 ${f}`)
|
||
]
|
||
.slice(0, 5)
|
||
.join(';')}`
|
||
);
|
||
}
|
||
|
||
// ② 加载路径指向快照 —— **按宿主真实的加载方式**
|
||
if (load === 'own-process') {
|
||
const execStart = unitExecStart(unit);
|
||
const atSnapshot = execStart.includes(`/opt/agentmail/plugins/${plugin}/current`);
|
||
const atRepo = execStart.includes(`/home/program/agentmail/plugins/${plugin}`);
|
||
if (!execStart) fail('② 单元入口指向快照', `读不到 ${unit} 的 ExecStart`);
|
||
else if (atRepo) fail('② 单元入口指向快照', `仍指向仓库工作区:${execStart}`);
|
||
else if (atSnapshot) pass('② 单元入口指向快照', '指向 current');
|
||
else fail('② 单元入口指向快照', `指向别处:${execStart}`);
|
||
} else {
|
||
// host-package / host-config:查配置文件里那条引用,并尽量查它**实际解析到哪**
|
||
const cfg = spec.configFile;
|
||
let cfgText = '';
|
||
try {
|
||
cfgText = readFileSync(cfg, 'utf8');
|
||
} catch (e) {
|
||
fail('② 宿主配置指向快照', `读不到 ${cfg}:${e?.message || e}`);
|
||
}
|
||
if (cfgText) {
|
||
if (cfgText.includes(spec.configNeedle)) pass('② 宿主配置指向快照', `${cfg} 含 current`);
|
||
else fail('② 宿主配置指向快照', `${cfg} 里没有 ${spec.configNeedle}`);
|
||
}
|
||
if (spec.resolvePath) {
|
||
// 真正决定加载哪份代码的是这条软链(Node 按 node_modules 解析)
|
||
try {
|
||
const real = realpathSync(spec.resolvePath);
|
||
if (real.startsWith(`/opt/agentmail/plugins/${plugin}/`)) {
|
||
pass('② 实际解析到快照', `${real.split('/').slice(-2).join('/')}`);
|
||
} else {
|
||
fail('② 实际解析到快照', `${spec.resolvePath} → ${real}`);
|
||
}
|
||
} catch (e) {
|
||
fail('② 实际解析到快照', `${spec.resolvePath} 不可用:${e?.message || e}`);
|
||
}
|
||
}
|
||
}
|
||
|
||
// ③ 进程身份(own-process 才看 argv;别的宿主 argv 里本来就没有插件路径)
|
||
const pid = unitMainPid(unit);
|
||
if (!pid || pid === '0') {
|
||
fail('③ 宿主进程在跑', `${unit} 没有主进程(未运行?)`);
|
||
return result;
|
||
}
|
||
if (load === 'own-process') {
|
||
const argv = processArgv(pid);
|
||
if (argv.includes(`/opt/agentmail/plugins/${plugin}/current`)) pass('③ 进程在跑快照里的代码', `pid=${pid}`);
|
||
else fail('③ 进程在跑快照里的代码', `pid=${pid} 的 argv 里没有快照路径:${argv.slice(0, 90)}`);
|
||
} else {
|
||
pass('③ 宿主进程在跑', `pid=${pid}(插件由宿主加载,argv 里本就没有插件路径)`);
|
||
}
|
||
|
||
// ④ 进程启动时刻 vs 软链切换时刻。
|
||
// 软链切换**不会**重载已在跑的进程 ⇒ 对「启动时加载」的宿主,这一条是硬判据。
|
||
// 判定逻辑在纯函数 judgeRestart 里(它被自检的行为用例盖住)。
|
||
const switchAt = lstatSync(linkPath).mtimeMs;
|
||
const startedAt = procStartMs(pid);
|
||
const j = judgeRestart({ load, startedAt, switchAt });
|
||
result.checks.push({ name: '④ 进程启动不早于快照切换', ok: j.ok, note: j.note });
|
||
if (!j.ok) result.stale = true;
|
||
|
||
return result;
|
||
}
|
||
|
||
/** 判据自检:**先证明这个检查器能发现差异**,再用它下结论。
|
||
* 一个永远说「一致」的比较器看起来同样令人放心。 */
|
||
export function selfCheck() {
|
||
const a = mkdtempSync(join(tmpdir(), 'drift-a-'));
|
||
const b = mkdtempSync(join(tmpdir(), 'drift-b-'));
|
||
const mk = (root, content) => {
|
||
mkdirSync(join(root, 'lib'), { recursive: true });
|
||
writeFileSync(join(root, 'lib', 'x.mjs'), content);
|
||
writeFileSync(join(root, 'README.md'), 'doc');
|
||
};
|
||
const out = [];
|
||
try {
|
||
mk(a, 'same');
|
||
mk(b, 'same');
|
||
const same = diffSummary(diffTrees(collectFiles(a), collectFiles(b)));
|
||
out.push({ name: '相同的树判为一致', ok: same.runtimeDrift === 0 && same.docDrift === 0 });
|
||
|
||
mk(b, 'DIFFERENT');
|
||
const diff = diffSummary(diffTrees(collectFiles(a), collectFiles(b)));
|
||
out.push({ name: '内容不同必须被发现', ok: diff.runtimeDrift === 1 });
|
||
|
||
mk(b, 'same');
|
||
writeFileSync(join(b, 'README.md'), 'doc-changed');
|
||
const docOnly = diffSummary(diffTrees(collectFiles(a), collectFiles(b)));
|
||
out.push({ name: '文档差异不算运行漂移', ok: docOnly.runtimeDrift === 0 && docOnly.docDrift === 1 });
|
||
|
||
writeFileSync(join(b, 'lib', 'extra.mjs'), 'x');
|
||
const extra = diffSummary(diffTrees(collectFiles(a), collectFiles(b)));
|
||
out.push({ name: '快照多出运行文件必须被发现', ok: extra.runtimeDrift === 1 });
|
||
|
||
mkdirSync(join(a, 'test'), { recursive: true });
|
||
writeFileSync(join(a, 'test', 't.mjs'), 'only-in-repo');
|
||
const withTest = diffSummary(diffTrees(collectFiles(a), collectFiles(b)));
|
||
out.push({ name: 'test/ 不参与比较(与部署脚本一致)', ok: withTest.runtimeDrift === 1 });
|
||
|
||
// 判据 ④ 的行为用例。
|
||
//
|
||
// 这一组来自一次**自检没接住的真错**:`restartOnSwitch` 只有 dsh 显式设了 true,
|
||
// pi/zcode 是 undefined ⇒ 落进「惰加载」分支 ⇒ 判据永真;
|
||
// 而当时自检写的是 `h.restartOnSwitch !== false`,undefined 也被放过了。
|
||
// 所以现在不查字段,直接查**判定结果**。
|
||
const SWITCH = 1_000_000_000_000;
|
||
const older = SWITCH - 60_000; // 进程比切换早一分钟
|
||
const newer = SWITCH + 60_000; // 进程比切换晚一分钟
|
||
out.push({
|
||
name: '④ 启动时加载的宿主:旧进程必须判红',
|
||
ok:
|
||
judgeRestart({ load: 'own-process', startedAt: older, switchAt: SWITCH }).ok === false &&
|
||
judgeRestart({ load: 'host-package', startedAt: older, switchAt: SWITCH }).ok === false
|
||
});
|
||
out.push({
|
||
name: '④ 惰加载的宿主:旧进程不判红(但也不是「已验过」)',
|
||
ok: judgeRestart({ load: 'host-config', startedAt: older, switchAt: SWITCH }).ok === true
|
||
});
|
||
out.push({
|
||
name: '④ 切换之后才启动的一律放行',
|
||
ok: ['own-process', 'host-package', 'host-config'].every(
|
||
l => judgeRestart({ load: l, startedAt: newer, switchAt: SWITCH }).ok === true
|
||
)
|
||
});
|
||
out.push({
|
||
name: '④ 读不到启动时刻时不据此判红',
|
||
ok: judgeRestart({ load: 'own-process', startedAt: 0, switchAt: SWITCH }).ok === true
|
||
});
|
||
out.push({
|
||
name: '④ 容差内(同一次部署先切后重启)不判红',
|
||
ok: judgeRestart({ load: 'own-process', startedAt: SWITCH - 1000, switchAt: SWITCH }).ok === true
|
||
});
|
||
|
||
// 宿主表本身的自检:每个宿主的判据必须落在它能观测到的地方。
|
||
const badSpec = HOSTS.filter(h => h.load !== 'own-process' && !h.configNeedle);
|
||
out.push({ name: '宿主表:非自有进程的宿主必须给出配置判据', ok: badSpec.length === 0 });
|
||
const badOwn = HOSTS.filter(h => h.load === 'own-process' && h.configNeedle);
|
||
out.push({ name: '宿主表:自有进程的宿主不该用配置判据', ok: badOwn.length === 0 });
|
||
} finally {
|
||
rmSync(a, { recursive: true, force: true });
|
||
rmSync(b, { recursive: true, force: true });
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/**
|
||
* 标准目录部署检查(2026-09-14)。
|
||
*
|
||
* 用户注意到:「当前 agentmail 是在源码目录部署的,应当改为标准目录部署」。
|
||
* 当时有三处实证:① 失败通知钩子执行的是**仓库里**的脚本(仓库一挪,故障通知静默
|
||
* 失效);② opencode 服务的 cwd 就是源码目录;③ 仓库里的 `deploy/*.service` 是旧的
|
||
* 源码目录版本,而机器上的已被改过 —— 谁跑一次 install.sh 就把部署退回源码目录。
|
||
*
|
||
* 现在:单元与 drop-in 的唯一真相是 `deploy/systemd/`(镜像 systemd 目录结构),
|
||
* 运行时脚本装在 `/opt/agentmail/bin/`,服务不依赖仓库是否存在。
|
||
*
|
||
* @param {object} [inject] 注入点(判据自检时喂假文件系统)
|
||
*/
|
||
export function checkLayout(inject = {}) {
|
||
const readdir = inject.readdir ?? readdirSync;
|
||
const readFile = inject.readFile ?? readFileSync;
|
||
const exists = inject.exists ?? existsSync;
|
||
const stat = inject.stat ?? statSync;
|
||
const out = [];
|
||
const push = (name, ok, note = '') => out.push({ name, ok, note });
|
||
|
||
const SYS = '/etc/systemd/system';
|
||
const REPO = '/home/program/agentmail';
|
||
// 自有进程必须住在安装根下;其余宿主有它们自己的标准位置(不是本项目的源码目录)
|
||
const HOST_ALLOW = {
|
||
'homeagent.service': '/home/newqqagent',
|
||
'dsh.service': '/usr/bin/dsh',
|
||
'zcode.service': '/opt/ZCode'
|
||
};
|
||
|
||
// ① 任何 unit/drop-in 都不得引用源码目录
|
||
const offenders = [];
|
||
const walk = dir => {
|
||
let entries = [];
|
||
try { entries = readdir(dir, { withFileTypes: true }); } catch { return; }
|
||
for (const e of entries) {
|
||
const full = `${dir}/${e.name}`;
|
||
if (e.isDirectory()) walk(full);
|
||
else if (/\.(conf|service|timer)$/.test(e.name) && !e.name.includes('.bak')) {
|
||
let text = '';
|
||
try { text = String(readFile(full, 'utf8')); } catch { continue; }
|
||
if (text.includes(REPO)) offenders.push(full);
|
||
}
|
||
}
|
||
};
|
||
walk(SYS);
|
||
push('没有任何 unit/drop-in 引用源码目录', offenders.length === 0, offenders.join(' '));
|
||
|
||
// ② 已安装单元与仓库副本一致(仓库是唯一真相)
|
||
const drift = [];
|
||
const repoUnits = inject.repoUnits ?? new URL('../systemd', import.meta.url).pathname.replace(/\/$/, '');
|
||
const compare = dir => {
|
||
let entries = [];
|
||
try { entries = readdir(dir, { withFileTypes: true }); } catch { return; }
|
||
for (const e of entries) {
|
||
const full = `${dir}/${e.name}`;
|
||
if (e.isDirectory()) compare(full);
|
||
else {
|
||
const rel = full.slice(repoUnits.length + 1);
|
||
const live = `${SYS}/${rel}`;
|
||
if (!exists(live)) { drift.push(`${rel}(缺)`); continue; }
|
||
let a = '', b = '';
|
||
try { a = String(readFile(full, 'utf8')); b = String(readFile(live, 'utf8')); } catch { continue; }
|
||
if (a !== b) drift.push(rel);
|
||
}
|
||
}
|
||
};
|
||
compare(repoUnits);
|
||
push('已安装单元与 deploy/systemd/ 一致', drift.length === 0, drift.join(' '));
|
||
|
||
// ③ 通知脚本在标准位置且可执行
|
||
const script = '/opt/agentmail/bin/service-failure-notify.mjs';
|
||
let scriptOk = false;
|
||
let note = script;
|
||
try {
|
||
const st = stat(script);
|
||
scriptOk = st.isFile() && (st.mode & 0o111) !== 0;
|
||
if (!scriptOk) note = `${script} 缺执行位`;
|
||
} catch { note = `${script} 不存在`; }
|
||
push('故障通知脚本装在 /opt/agentmail/bin/ 且可执行', scriptOk, note);
|
||
|
||
// ④ 自有服务的 cwd / ExecStart 不得落在源码目录
|
||
const badHosts = [];
|
||
for (const unit of ['agentmail-gateway.service', 'pi-mail-bridge.service', 'opencode-serve.service',
|
||
'zcode-mail-bridge.service', 'homeagent.service', 'dsh.service', 'zcode.service']) {
|
||
let text = '';
|
||
try { text = String(readFile(`${SYS}/${unit}`, 'utf8')); } catch { continue; }
|
||
const cwd = (text.match(/WorkingDirectory=(.+)/) || [])[1]?.trim() ?? '';
|
||
const exec = (text.match(/ExecStart=(.+)/) || [])[1]?.trim() ?? '';
|
||
const allow = HOST_ALLOW[unit];
|
||
if (allow) {
|
||
// 用 includes 而不是 startsWith:zcode 的 ExecStart 是
|
||
// `dbus-run-session -- xvfb-run … /opt/ZCode/zcode …`,宿主路径在中间。
|
||
if (!text.includes(allow)) badHosts.push(`${unit}(不在 ${allow})`);
|
||
continue;
|
||
}
|
||
if (cwd.startsWith(REPO) || exec.startsWith(REPO)) badHosts.push(`${unit}(${cwd || exec})`);
|
||
}
|
||
push('各服务的工作目录/可执行文件不在源码目录', badHosts.length === 0, badHosts.join(' '));
|
||
|
||
return out;
|
||
}
|
||
|
||
/** 标准目录那组自检:坏样本必须红、干净样本必须绿(证明它不是恒真)。 */
|
||
export function layoutSelfCheck() {
|
||
const fake = map => ({
|
||
readdir: dir => map[dir] ?? [],
|
||
readFile: p => {
|
||
if (!(p in map)) throw new Error('ENOENT');
|
||
return map[p];
|
||
},
|
||
exists: p => p in map,
|
||
stat: () => ({ isFile: () => true, mode: 0o755 }),
|
||
repoUnits: '/repo/systemd'
|
||
});
|
||
const bad = checkLayout(fake({
|
||
'/etc/systemd/system': [{ name: 'x.service', isDirectory: () => false }],
|
||
'/etc/systemd/system/x.service': 'ExecStart=/home/program/agentmail/bin/x',
|
||
'/repo/systemd': [],
|
||
'/opt/agentmail/bin/service-failure-notify.mjs': 'x'
|
||
}));
|
||
const good = checkLayout(fake({
|
||
'/etc/systemd/system': [{ name: 'y.service', isDirectory: () => false }],
|
||
'/etc/systemd/system/y.service': 'ExecStart=/opt/agentmail/agentmail-gateway',
|
||
'/repo/systemd': [],
|
||
'/opt/agentmail/bin/service-failure-notify.mjs': 'x'
|
||
}));
|
||
return [
|
||
{ name: '标准目录:引用源码目录的样本必须判红', ok: bad[0].ok === false },
|
||
{ name: '标准目录:干净样本必须判绿', ok: good[0].ok === true }
|
||
];
|
||
}
|
||
|
||
function main() {
|
||
const json = process.argv.includes('--json');
|
||
const wantSelfCheck = process.argv.includes('--self-check');
|
||
|
||
if (wantSelfCheck) {
|
||
const checks = selfCheck().concat(layoutSelfCheck());
|
||
if (json) console.log(JSON.stringify({ selfCheck: checks }, null, 2));
|
||
else {
|
||
console.log('判据自检(先证明检查器能发现差异):');
|
||
for (const c of checks) console.log(` ${c.ok ? '通过' : '失败'} ${c.name}`);
|
||
}
|
||
const bad = checks.filter(c => !c.ok);
|
||
if (bad.length) {
|
||
console.error(`判据自检失败 ${bad.length} 项 —— 检查器本身不可信,不能用它的结论`);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
const layout = checkLayout();
|
||
const layoutBad = layout.filter(c => !c.ok);
|
||
|
||
const results = HOSTS.map(checkHost);
|
||
if (json) {
|
||
console.log(JSON.stringify({ hosts: results, layout }, null, 2));
|
||
} else {
|
||
console.log('\n部署漂移检查(快照 vs 仓库 HEAD,以及进程到底在跑哪份代码):');
|
||
for (const r of results) {
|
||
console.log(`\n ${r.host}(${r.plugin},加载方式 ${r.load})${r.stale ? ' ⚠️ 漂移' : ''}`);
|
||
for (const c of r.checks) console.log(` ${c.ok ? '通过' : '失败'} ${c.name}${c.note ? ' — ' + c.note : ''}`);
|
||
}
|
||
console.log('\n 标准目录部署:');
|
||
for (const c of layout) console.log(` ${c.ok ? '通过' : '失败'} ${c.name}${c.note ? ' — ' + c.note : ''}`);
|
||
const stale = results.filter(r => r.stale);
|
||
console.log(`\n 结论:${stale.length === 0 ? '四个宿主都在跑当前代码' : `${stale.length} 个宿主需要重新部署/重启`}`);
|
||
for (const r of stale) console.log(` ✗ ${r.host}:node deploy/redeploy-plugin.sh ${r.host}`);
|
||
}
|
||
process.exit(results.some(r => r.stale) || layoutBad.length ? 1 : 0);
|
||
}
|
||
|
||
// 仅在被直接执行时跑 main(被 import 时只导出,供测试调用)
|
||
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) main();
|