fix: address code review — improve empty catch blocks, extract safeReadJSON helpers, document constants
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com> Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/b47885f0-73db-4678-b3d5-55b55611d9b0
This commit is contained in:
parent
09fb609688
commit
2771eca06e
|
|
@ -77,7 +77,7 @@ class NearbyQuery {
|
|||
results.push({ cell, data, distance });
|
||||
}
|
||||
} catch {
|
||||
// 跳过无效键
|
||||
// 跳过无效键格式(fromKey 解析失败),不影响其他键的查询
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ class RangeScanner {
|
|||
results.push({ cell, data });
|
||||
}
|
||||
} catch {
|
||||
// 跳过无效键
|
||||
// 跳过无效键格式(fromKey 解析失败),不影响其他键的扫描
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -262,14 +262,18 @@ if (require.main === module) {
|
|||
if (fs.existsSync(statusPath)) {
|
||||
try {
|
||||
state = JSON.parse(fs.readFileSync(statusPath, 'utf8'));
|
||||
} catch (_) { /* 忽略解析错误 */ }
|
||||
} catch {
|
||||
// twin-status.json 解析失败,使用空状态生成 README
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync(bulletinPath)) {
|
||||
try {
|
||||
const bulletin = JSON.parse(fs.readFileSync(bulletinPath, 'utf8'));
|
||||
state.events = bulletin.events || [];
|
||||
} catch (_) { /* 忽略解析错误 */ }
|
||||
} catch {
|
||||
// bulletin-data.json 解析失败,跳过公告数据
|
||||
}
|
||||
}
|
||||
|
||||
state.building = true;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,21 @@ const ROOT = path.resolve(__dirname, '../..');
|
|||
const TIANYEN_DIR = path.join(ROOT, '.github/tianyen');
|
||||
const DISPATCH_PATH = path.join(TIANYEN_DIR, 'bulletin-dispatch.json');
|
||||
|
||||
/**
|
||||
* 安全读取 JSON 文件
|
||||
* @param {string} filePath
|
||||
* @returns {object|null}
|
||||
*/
|
||||
function safeReadJSON(filePath) {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
} catch {
|
||||
// JSON 损坏,跳过该数据源
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 采集所有 Agent 状态
|
||||
* @returns {object[]}
|
||||
|
|
@ -20,34 +35,28 @@ function collectAllAgentStatus() {
|
|||
const statuses = [];
|
||||
|
||||
// 签到记录
|
||||
const checkinPath = path.join(TIANYEN_DIR, 'checkin-log.json');
|
||||
if (fs.existsSync(checkinPath)) {
|
||||
try {
|
||||
const log = JSON.parse(fs.readFileSync(checkinPath, 'utf8'));
|
||||
for (const [agentId, record] of Object.entries(log.checkins || {})) {
|
||||
statuses.push({
|
||||
agentId,
|
||||
status: record.status || 'unknown',
|
||||
lastSeen: record.timestamp || null
|
||||
});
|
||||
}
|
||||
} catch (_) { /* 忽略 */ }
|
||||
const log = safeReadJSON(path.join(TIANYEN_DIR, 'checkin-log.json'));
|
||||
if (log) {
|
||||
for (const [agentId, record] of Object.entries(log.checkins || {})) {
|
||||
statuses.push({
|
||||
agentId,
|
||||
status: record.status || 'unknown',
|
||||
lastSeen: record.timestamp || null
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 调度配置
|
||||
const schedulePath = path.join(TIANYEN_DIR, 'agent-schedule.json');
|
||||
if (fs.existsSync(schedulePath)) {
|
||||
try {
|
||||
const schedule = JSON.parse(fs.readFileSync(schedulePath, 'utf8'));
|
||||
for (const [agentId, config] of Object.entries(schedule.agents || {})) {
|
||||
const existing = statuses.find(s => s.agentId === agentId);
|
||||
if (existing) {
|
||||
existing.schedule = config;
|
||||
} else {
|
||||
statuses.push({ agentId, status: 'configured', lastSeen: null, schedule: config });
|
||||
}
|
||||
const schedule = safeReadJSON(path.join(TIANYEN_DIR, 'agent-schedule.json'));
|
||||
if (schedule) {
|
||||
for (const [agentId, config] of Object.entries(schedule.agents || {})) {
|
||||
const existing = statuses.find(s => s.agentId === agentId);
|
||||
if (existing) {
|
||||
existing.schedule = config;
|
||||
} else {
|
||||
statuses.push({ agentId, status: 'configured', lastSeen: null, schedule: config });
|
||||
}
|
||||
} catch (_) { /* 忽略 */ }
|
||||
}
|
||||
}
|
||||
|
||||
return statuses;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,21 @@ const path = require('path');
|
|||
const ROOT = path.resolve(__dirname, '../..');
|
||||
const TIANYEN_DIR = path.join(ROOT, '.github/tianyen');
|
||||
|
||||
/**
|
||||
* 安全读取 JSON 文件,解析失败返回 null
|
||||
* @param {string} filePath 文件路径
|
||||
* @returns {object|null}
|
||||
*/
|
||||
function safeReadJSON(filePath) {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
} catch {
|
||||
// 文件存在但 JSON 格式损坏,跳过该数据源继续采集其他状态
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 采集全局状态 · 汇聚各路信号
|
||||
* @returns {object} 所有 Agent 的状态汇总
|
||||
|
|
@ -18,36 +33,12 @@ const TIANYEN_DIR = path.join(ROOT, '.github/tianyen');
|
|||
function collectGlobalState() {
|
||||
const state = {
|
||||
timestamp: new Date().toISOString(),
|
||||
twin: null,
|
||||
bulletin: null,
|
||||
schedule: null,
|
||||
checkins: null
|
||||
twin: safeReadJSON(path.join(TIANYEN_DIR, 'twin-data.json')),
|
||||
bulletin: safeReadJSON(path.join(TIANYEN_DIR, 'bulletin-data.json')),
|
||||
schedule: safeReadJSON(path.join(TIANYEN_DIR, 'agent-schedule.json')),
|
||||
checkins: safeReadJSON(path.join(TIANYEN_DIR, 'checkin-log.json'))
|
||||
};
|
||||
|
||||
// 双子天平数据
|
||||
const twinPath = path.join(TIANYEN_DIR, 'twin-data.json');
|
||||
if (fs.existsSync(twinPath)) {
|
||||
try { state.twin = JSON.parse(fs.readFileSync(twinPath, 'utf8')); } catch (_) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
// 公告板数据
|
||||
const bulletinPath = path.join(TIANYEN_DIR, 'bulletin-data.json');
|
||||
if (fs.existsSync(bulletinPath)) {
|
||||
try { state.bulletin = JSON.parse(fs.readFileSync(bulletinPath, 'utf8')); } catch (_) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
// 调度配置
|
||||
const schedulePath = path.join(TIANYEN_DIR, 'agent-schedule.json');
|
||||
if (fs.existsSync(schedulePath)) {
|
||||
try { state.schedule = JSON.parse(fs.readFileSync(schedulePath, 'utf8')); } catch (_) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
// 签到记录
|
||||
const checkinPath = path.join(TIANYEN_DIR, 'checkin-log.json');
|
||||
if (fs.existsSync(checkinPath)) {
|
||||
try { state.checkins = JSON.parse(fs.readFileSync(checkinPath, 'utf8')); } catch (_) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -125,7 +125,9 @@ if (require.main === module) {
|
|||
try {
|
||||
const status = JSON.parse(fs.readFileSync(statusPath, 'utf8'));
|
||||
metrics = { balance: status.balance, drift: status.drift, recentChanges: 0 };
|
||||
} catch (_) { /* 忽略 */ }
|
||||
} catch {
|
||||
// twin-status.json 损坏或不可读,使用默认空指标继续评估
|
||||
}
|
||||
}
|
||||
|
||||
const evaluation = evaluateSchedule('AG-ZY-TWIN', metrics);
|
||||
|
|
|
|||
Loading…
Reference in New Issue