- 前端: React+Vite 首页/AI专区, 算法库(MARD色板/量化/背景去除), API对接 - 后端: Express+SQLite, 兑换码/订单/支付/chibi生成, 风控+云端同步 - CI/CD: GitHub Actions + Docker + nginx 配置 - 文档: PRD/DESIGN/LAUNCH/ROADMAP/E2E/竞品对照等 15 份 - 工程: 任务看板(taskboard) + 健康巡检(agent-health) + Git流程(gitflow)
130 lines
5.8 KiB
JavaScript
130 lines
5.8 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* PERLER LAB · Agent 健康巡检脚本
|
||
* 检测:
|
||
* 1. 任务卡死:认领后超过 STALE_MIN 分钟无状态流转
|
||
* 2. 交付物停滞:开发中任务对应的产出目录超过 FILE_STALE_MIN 分钟无文件变化
|
||
* 3. 无产出风险:开发中任务没有关联任何产出目录
|
||
* 用法: node agent-health.js [--verbose]
|
||
*/
|
||
'use strict';
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const ROOT = path.resolve(__dirname);
|
||
const TASKS_FILE = path.join(ROOT, 'taskboard', 'tasks.json');
|
||
const STALE_MIN = Number(process.env.STALE_MIN) || 30; // 任务认领后无流转超时
|
||
const FILE_STALE_MIN = Number(process.env.FILE_STALE_MIN) || 20; // 产出文件无变化超时
|
||
|
||
// 任务 ID 关键字 → 产出目录映射(粗粒度,用于停滞检测)
|
||
const OUTPUT_MAP = [
|
||
{ re: /T-020|T-021|T-004|BUG-009|T-003/, dir: ROOT + '/perler-lab' },
|
||
{ re: /T-022|T-023|T-005|T-024|T-025|T-026|T-027|T-012/, dir: ROOT + '/perler-lab-server' },
|
||
{ re: /BUG-008|T-002|T-031|T-032|T-033|T-019/, dir: ROOT + '/docs' },
|
||
{ re: /T-016|T-018|T-017|T-013/, dir: ROOT + '/competitor-analysis' },
|
||
{ re: /T-028|T-029|T-030|T-015/, dir: ROOT + '/perler-lab' },
|
||
];
|
||
|
||
function newestMtime(dir) {
|
||
if (!fs.existsSync(dir)) return 0;
|
||
let newest = 0;
|
||
const walk = (d) => {
|
||
let entries;
|
||
try { entries = fs.readdirSync(d, { withFileTypes: true }); } catch { return; }
|
||
for (const e of entries) {
|
||
if (e.name === 'node_modules' || e.name === '.git' || e.name.startsWith('.')) continue;
|
||
const p = path.join(d, e.name);
|
||
if (e.isDirectory()) walk(p);
|
||
else {
|
||
try { const m = fs.statSync(p).mtimeMs; if (m > newest) newest = m; } catch {}
|
||
}
|
||
}
|
||
};
|
||
walk(dir);
|
||
return newest;
|
||
}
|
||
|
||
function loadTasks() {
|
||
if (!fs.existsSync(TASKS_FILE)) return [];
|
||
return JSON.parse(fs.readFileSync(TASKS_FILE, 'utf8')).tasks || [];
|
||
}
|
||
|
||
const now = Date.now();
|
||
const tasks = loadTasks();
|
||
const problems = [];
|
||
const verbose = process.argv.includes('--verbose');
|
||
const watchMode = process.argv.includes('--watch');
|
||
const watchInterval = (Number(process.env.WATCH_INTERVAL) || 10) * 60 * 1000; // 默认每 10 分钟
|
||
const jsonMode = process.argv.includes('--json');
|
||
|
||
function runInspection() {
|
||
const problems = [];
|
||
let checked = 0;
|
||
for (const t of loadTasks()) {
|
||
if (!['in_progress', 'review'].includes(t.status)) continue;
|
||
checked++;
|
||
const agent = t.agent || '?';
|
||
if (t.status === 'in_progress' && t.claimedAt) {
|
||
const claimed = Date.parse(t.claimedAt);
|
||
const mins = (now - claimed) / 60000;
|
||
const flag = mins > STALE_MIN ? '🔴' : '🟢';
|
||
console.log(`${flag} ${t.id} [${agent}] 认领已 ${Math.round(mins)} 分钟` + (flag === '🔴' ? ' ⚠️ 疑似卡死' : ''));
|
||
if (flag === '🔴') problems.push({ task: t.id, agent, type: 'stale', detail: `认领 ${Math.round(mins)} 分钟无流转,疑似卡死` });
|
||
}
|
||
if (t.status === 'review' && t.submittedAt) {
|
||
const submitted = Date.parse(t.submittedAt);
|
||
const mins = (now - submitted) / 60000;
|
||
console.log(`🟡 ${t.id} [${agent}] 待验收已 ${Math.round(mins)} 分钟` + (mins > STALE_MIN ? ' ⚠️ 无人验收' : ''));
|
||
if (mins > STALE_MIN) problems.push({ task: t.id, agent, type: 'review_stale', detail: `待验收 ${Math.round(mins)} 分钟无人处理` });
|
||
}
|
||
const claimedMs = t.claimedAt ? Date.parse(t.claimedAt) : 0;
|
||
const claimMins = claimedMs ? (now - claimedMs) / 60000 : 0;
|
||
const map = OUTPUT_MAP.find(m => m.re.test(t.id));
|
||
if (map && claimMins > FILE_STALE_MIN) {
|
||
const mtime = newestMtime(map.dir);
|
||
const mins = mtime ? (now - mtime) / 60000 : -1;
|
||
if (mtime === 0) {
|
||
console.log(` 📁 ${t.id} 产出目录 ${path.basename(map.dir)} 不存在或为空`);
|
||
if (t.status === 'in_progress') problems.push({ task: t.id, agent, type: 'no_output', detail: `产出目录 ${path.basename(map.dir)} 无文件` });
|
||
} else if (mins > FILE_STALE_MIN && t.status === 'in_progress') {
|
||
console.log(` ⚠️ ${t.id} 产出目录 ${path.basename(map.dir)} 已 ${Math.round(mins)} 分钟无文件变化`);
|
||
problems.push({ task: t.id, agent, type: 'output_stale', detail: `产出停滞 ${Math.round(mins)} 分钟(目录 ${path.basename(map.dir)})` });
|
||
}
|
||
}
|
||
}
|
||
const result = { time: new Date(now + 8 * 3600 * 1000).toISOString().slice(0, 19).replace('T', ' '), checked, problems, ok: problems.length === 0 };
|
||
if (jsonMode) {
|
||
console.log(JSON.stringify(result));
|
||
} else {
|
||
console.log(`\n=== 巡检完成: ${checked} 个活跃任务, ${problems.length} 个问题 ===`);
|
||
if (problems.length) {
|
||
console.log('问题清单:');
|
||
problems.forEach(p => console.log(` ⚠️ [${p.task}] ${p.detail}`));
|
||
process.exitCode = 1;
|
||
} else {
|
||
console.log('✅ 全部 Agent 工作正常,无卡死/循环/停滞迹象');
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
if (watchMode) {
|
||
// 常驻监督模式:定期巡检并保留问题记录文件(供看板显示)
|
||
const HISTORY_FILE = path.join(ROOT, 'taskboard', 'health-history.json');
|
||
const history = fs.existsSync(HISTORY_FILE) ? JSON.parse(fs.readFileSync(HISTORY_FILE, 'utf8')) : [];
|
||
console.log(`[监督] Agent 健康巡检已启动,每 ${watchInterval / 60000} 分钟一次,Ctrl+C 停止`);
|
||
const loop = () => {
|
||
const result = runInspection();
|
||
history.unshift({ ...result, raw: JSON.stringify(result) });
|
||
const trimmed = history.slice(0, 50);
|
||
fs.writeFileSync(HISTORY_FILE, JSON.stringify(trimmed, null, 1), 'utf8');
|
||
if (!result.ok) {
|
||
console.log(`[监督] ⚠️ 发现 ${result.problems.length} 个问题,请主代理处理: ${result.problems.map(p => p.task).join(', ')}`);
|
||
}
|
||
};
|
||
loop();
|
||
setInterval(loop, watchInterval);
|
||
} else {
|
||
runInspection();
|
||
}
|