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:
copilot-swe-agent[bot] 2026-03-26 09:13:55 +00:00 committed by GitHub
parent 09fb609688
commit 2771eca06e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 57 deletions

View File

@ -77,7 +77,7 @@ class NearbyQuery {
results.push({ cell, data, distance }); results.push({ cell, data, distance });
} }
} catch { } catch {
// 跳过无效键 // 跳过无效键格式fromKey 解析失败),不影响其他键的查询
} }
} }

View File

@ -80,7 +80,7 @@ class RangeScanner {
results.push({ cell, data }); results.push({ cell, data });
} }
} catch { } catch {
// 跳过无效键 // 跳过无效键格式fromKey 解析失败),不影响其他键的扫描
} }
} }

View File

@ -262,14 +262,18 @@ if (require.main === module) {
if (fs.existsSync(statusPath)) { if (fs.existsSync(statusPath)) {
try { try {
state = JSON.parse(fs.readFileSync(statusPath, 'utf8')); state = JSON.parse(fs.readFileSync(statusPath, 'utf8'));
} catch (_) { /* 忽略解析错误 */ } } catch {
// twin-status.json 解析失败,使用空状态生成 README
}
} }
if (fs.existsSync(bulletinPath)) { if (fs.existsSync(bulletinPath)) {
try { try {
const bulletin = JSON.parse(fs.readFileSync(bulletinPath, 'utf8')); const bulletin = JSON.parse(fs.readFileSync(bulletinPath, 'utf8'));
state.events = bulletin.events || []; state.events = bulletin.events || [];
} catch (_) { /* 忽略解析错误 */ } } catch {
// bulletin-data.json 解析失败,跳过公告数据
}
} }
state.building = true; state.building = true;

View File

@ -12,6 +12,21 @@ const ROOT = path.resolve(__dirname, '../..');
const TIANYEN_DIR = path.join(ROOT, '.github/tianyen'); const TIANYEN_DIR = path.join(ROOT, '.github/tianyen');
const DISPATCH_PATH = path.join(TIANYEN_DIR, 'bulletin-dispatch.json'); 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 状态 * 采集所有 Agent 状态
* @returns {object[]} * @returns {object[]}
@ -20,34 +35,28 @@ function collectAllAgentStatus() {
const statuses = []; const statuses = [];
// 签到记录 // 签到记录
const checkinPath = path.join(TIANYEN_DIR, 'checkin-log.json'); const log = safeReadJSON(path.join(TIANYEN_DIR, 'checkin-log.json'));
if (fs.existsSync(checkinPath)) { if (log) {
try { for (const [agentId, record] of Object.entries(log.checkins || {})) {
const log = JSON.parse(fs.readFileSync(checkinPath, 'utf8')); statuses.push({
for (const [agentId, record] of Object.entries(log.checkins || {})) { agentId,
statuses.push({ status: record.status || 'unknown',
agentId, lastSeen: record.timestamp || null
status: record.status || 'unknown', });
lastSeen: record.timestamp || null }
});
}
} catch (_) { /* 忽略 */ }
} }
// 调度配置 // 调度配置
const schedulePath = path.join(TIANYEN_DIR, 'agent-schedule.json'); const schedule = safeReadJSON(path.join(TIANYEN_DIR, 'agent-schedule.json'));
if (fs.existsSync(schedulePath)) { if (schedule) {
try { for (const [agentId, config] of Object.entries(schedule.agents || {})) {
const schedule = JSON.parse(fs.readFileSync(schedulePath, 'utf8')); const existing = statuses.find(s => s.agentId === agentId);
for (const [agentId, config] of Object.entries(schedule.agents || {})) { if (existing) {
const existing = statuses.find(s => s.agentId === agentId); existing.schedule = config;
if (existing) { } else {
existing.schedule = config; statuses.push({ agentId, status: 'configured', lastSeen: null, schedule: config });
} else {
statuses.push({ agentId, status: 'configured', lastSeen: null, schedule: config });
}
} }
} catch (_) { /* 忽略 */ } }
} }
return statuses; return statuses;

View File

@ -11,6 +11,21 @@ const path = require('path');
const ROOT = path.resolve(__dirname, '../..'); const ROOT = path.resolve(__dirname, '../..');
const TIANYEN_DIR = path.join(ROOT, '.github/tianyen'); 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 的状态汇总 * @returns {object} 所有 Agent 的状态汇总
@ -18,36 +33,12 @@ const TIANYEN_DIR = path.join(ROOT, '.github/tianyen');
function collectGlobalState() { function collectGlobalState() {
const state = { const state = {
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
twin: null, twin: safeReadJSON(path.join(TIANYEN_DIR, 'twin-data.json')),
bulletin: null, bulletin: safeReadJSON(path.join(TIANYEN_DIR, 'bulletin-data.json')),
schedule: null, schedule: safeReadJSON(path.join(TIANYEN_DIR, 'agent-schedule.json')),
checkins: null 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; return state;
} }

View File

@ -125,7 +125,9 @@ if (require.main === module) {
try { try {
const status = JSON.parse(fs.readFileSync(statusPath, 'utf8')); const status = JSON.parse(fs.readFileSync(statusPath, 'utf8'));
metrics = { balance: status.balance, drift: status.drift, recentChanges: 0 }; metrics = { balance: status.balance, drift: status.drift, recentChanges: 0 };
} catch (_) { /* 忽略 */ } } catch {
// twin-status.json 损坏或不可读,使用默认空指标继续评估
}
} }
const evaluation = evaluateSchedule('AG-ZY-TWIN', metrics); const evaluation = evaluateSchedule('AG-ZY-TWIN', metrics);