fix: address code review feedback on cos-watcher
- Use crypto.randomBytes for receipt key + ID uniqueness - Log PERSONA_REPO_MAP parse errors instead of silently catching - Cache cosWatcher.getStatus() call in health endpoint - Extract INITIAL_SCAN_DELAY_MS constant with explanatory comment Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/991cbbcf-9e83-43c1-9225-e235e0a61f06 Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
b487b6bc57
commit
bccdae0ba0
|
|
@ -28,6 +28,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const cron = require('node-cron');
|
const cron = require('node-cron');
|
||||||
|
const crypto = require('crypto');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cos = require('./cos');
|
const cos = require('./cos');
|
||||||
|
|
@ -36,6 +37,8 @@ const cos = require('./cos');
|
||||||
const DEFAULT_INTERVAL = process.env.COS_WATCHER_INTERVAL || '*/5 * * * *'; // 每5分钟
|
const DEFAULT_INTERVAL = process.env.COS_WATCHER_INTERVAL || '*/5 * * * *'; // 每5分钟
|
||||||
const STATE_FILE = path.join(__dirname, 'cos-watcher-state.json');
|
const STATE_FILE = path.join(__dirname, 'cos-watcher-state.json');
|
||||||
const MAX_LOG_ENTRIES = 200;
|
const MAX_LOG_ENTRIES = 200;
|
||||||
|
// 启动后延迟执行首次扫描,等待MCP Server Express + DB连接完全就绪
|
||||||
|
const INITIAL_SCAN_DELAY_MS = 5000;
|
||||||
|
|
||||||
// 9个人格体ID
|
// 9个人格体ID
|
||||||
const PERSONA_IDS = [
|
const PERSONA_IDS = [
|
||||||
|
|
@ -255,10 +258,10 @@ async function handleNewReport(report) {
|
||||||
// 生成自动回执(基础版:确认收到)
|
// 生成自动回执(基础版:确认收到)
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const dateStr = now.toISOString().slice(0, 10);
|
const dateStr = now.toISOString().slice(0, 10);
|
||||||
const receiptKey = `${report.persona_id}/receipts/${dateStr}/auto-receipt-${Date.now()}.json`;
|
const receiptKey = `${report.persona_id}/receipts/${dateStr}/auto-receipt-${crypto.randomBytes(6).toString('hex')}.json`;
|
||||||
|
|
||||||
const receipt = {
|
const receipt = {
|
||||||
receipt_id: `RCPT-${Date.now()}`,
|
receipt_id: `RCPT-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`,
|
||||||
report_key: report.key,
|
report_key: report.key,
|
||||||
persona_id: report.persona_id,
|
persona_id: report.persona_id,
|
||||||
status: 'green',
|
status: 'green',
|
||||||
|
|
@ -303,8 +306,8 @@ async function handleNewReceipt(receipt) {
|
||||||
try {
|
try {
|
||||||
const mapStr = process.env.PERSONA_REPO_MAP || '{}';
|
const mapStr = process.env.PERSONA_REPO_MAP || '{}';
|
||||||
personaRepoMap = JSON.parse(mapStr);
|
personaRepoMap = JSON.parse(mapStr);
|
||||||
} catch {
|
} catch (err) {
|
||||||
// 映射解析失败
|
logEvent('config_error', `PERSONA_REPO_MAP parse failed: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetRepo = personaRepoMap[receipt.persona_id];
|
const targetRepo = personaRepoMap[receipt.persona_id];
|
||||||
|
|
@ -515,12 +518,12 @@ function start(interval) {
|
||||||
console.log(`[COS-Watcher] COS桶轮询守护进程已启动 · 间隔: ${cronExpr}`);
|
console.log(`[COS-Watcher] COS桶轮询守护进程已启动 · 间隔: ${cronExpr}`);
|
||||||
logEvent('started', `轮询间隔: ${cronExpr}`);
|
logEvent('started', `轮询间隔: ${cronExpr}`);
|
||||||
|
|
||||||
// 启动后立即执行一次扫描
|
// 启动后延迟执行首次扫描,等待MCP Server完全就绪
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
runScan().catch(err => {
|
runScan().catch(err => {
|
||||||
console.error(`[COS-Watcher] 初始扫描异常: ${err.message}`);
|
console.error(`[COS-Watcher] 初始扫描异常: ${err.message}`);
|
||||||
});
|
});
|
||||||
}, 5000); // 延迟5秒,等MCP Server完全启动
|
}, INITIAL_SCAN_DELAY_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -338,6 +338,8 @@ app.get('/health', async (_req, res) => {
|
||||||
githubClient ? githubClient.checkConnection().catch(e => ({ connected: false, error: e.message })) : Promise.resolve({ connected: false, reason: '模块未加载' })
|
githubClient ? githubClient.checkConnection().catch(e => ({ connected: false, error: e.message })) : Promise.resolve({ connected: false, reason: '模块未加载' })
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const watcherStatus = cosWatcher.getStatus();
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
server: 'ZY-MCP-001',
|
server: 'ZY-MCP-001',
|
||||||
identity: '铸渊 · AGE OS MCP Server',
|
identity: '铸渊 · AGE OS MCP Server',
|
||||||
|
|
@ -353,10 +355,10 @@ app.get('/health', async (_req, res) => {
|
||||||
database: dbStatus,
|
database: dbStatus,
|
||||||
cos: cosStatus,
|
cos: cosStatus,
|
||||||
cos_watcher: {
|
cos_watcher: {
|
||||||
enabled: cosWatcher.getStatus().enabled,
|
enabled: watcherStatus.enabled,
|
||||||
last_scan: cosWatcher.getStatus().last_scan,
|
last_scan: watcherStatus.last_scan,
|
||||||
scan_count: cosWatcher.getStatus().scan_count,
|
scan_count: watcherStatus.scan_count,
|
||||||
errors: cosWatcher.getStatus().errors
|
errors: watcherStatus.errors
|
||||||
},
|
},
|
||||||
notion: notionStatus,
|
notion: notionStatus,
|
||||||
github: githubStatus
|
github: githubStatus
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue