From 2771eca06efea39abb8b3709438018045a1f99bc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Mar 2026 09:13:55 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20address=20code=20review=20=E2=80=94=20im?= =?UTF-8?q?prove=20empty=20catch=20blocks,=20extract=20safeReadJSON=20help?= =?UTF-8?q?ers,=20document=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com> Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/b47885f0-73db-4678-b3d5-55b55611d9b0 --- grid-db/src/query/nearby.js | 2 +- grid-db/src/query/scan.js | 2 +- scripts/agents/readme-generator.js | 8 +++- scripts/tianyen/bulletin-dispatcher.js | 57 +++++++++++++++----------- scripts/tianyen/context-injector.js | 47 +++++++++------------ scripts/tianyen/scheduler.js | 4 +- 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/grid-db/src/query/nearby.js b/grid-db/src/query/nearby.js index 9148bcd1..5c433872 100644 --- a/grid-db/src/query/nearby.js +++ b/grid-db/src/query/nearby.js @@ -77,7 +77,7 @@ class NearbyQuery { results.push({ cell, data, distance }); } } catch { - // 跳过无效键 + // 跳过无效键格式(fromKey 解析失败),不影响其他键的查询 } } diff --git a/grid-db/src/query/scan.js b/grid-db/src/query/scan.js index 4c0e868f..ab75c8fb 100644 --- a/grid-db/src/query/scan.js +++ b/grid-db/src/query/scan.js @@ -80,7 +80,7 @@ class RangeScanner { results.push({ cell, data }); } } catch { - // 跳过无效键 + // 跳过无效键格式(fromKey 解析失败),不影响其他键的扫描 } } diff --git a/scripts/agents/readme-generator.js b/scripts/agents/readme-generator.js index 7c9cb71c..92581a37 100644 --- a/scripts/agents/readme-generator.js +++ b/scripts/agents/readme-generator.js @@ -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; diff --git a/scripts/tianyen/bulletin-dispatcher.js b/scripts/tianyen/bulletin-dispatcher.js index b8bc9212..725baafb 100644 --- a/scripts/tianyen/bulletin-dispatcher.js +++ b/scripts/tianyen/bulletin-dispatcher.js @@ -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; diff --git a/scripts/tianyen/context-injector.js b/scripts/tianyen/context-injector.js index e8451a10..df2d37cf 100644 --- a/scripts/tianyen/context-injector.js +++ b/scripts/tianyen/context-injector.js @@ -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; } diff --git a/scripts/tianyen/scheduler.js b/scripts/tianyen/scheduler.js index 36da5449..67b1d70c 100644 --- a/scripts/tianyen/scheduler.js +++ b/scripts/tianyen/scheduler.js @@ -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);