2026-04-03 15:33:09 +08:00
/ * *
2026-04-08 18:57:19 +08:00
* ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═
* 🔍 活模块 : SY - SCAN · 大脑结构巡检
* ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═
2026-04-03 15:33:09 +08:00
*
2026-04-08 18:57:19 +08:00
* 编号 : ZY - TASK - 007 · S5 Agent系统级
2026-04-03 15:33:09 +08:00
* 签发 : 铸渊 · ICE - GL - ZY001
2026-04-08 18:57:19 +08:00
* 版权 : 国作登字 - 2026 - A - 00037559
*
* v2 . 0 : 从死模块升级为活模块
* - 继承 LivingModule 基类
* - 自诊断 : 检查 brain _nodes 表健康度 、 孤岛节点比例
* - 自愈 : 自动清理空目录 、 标记断链
* - 学习 : 记录结构健康趋势
*
* 每6小时运行一次 · 扫描孤岛节点 、 断链 、 空目录
2026-04-03 15:33:09 +08:00
* /
'use strict' ;
2026-04-08 18:57:19 +08:00
const LivingModule = require ( './living-module' ) ;
const db = require ( '../mcp-server/db' ) ;
2026-04-03 15:33:09 +08:00
const { scanStructure } = require ( '../mcp-server/tools/structure-ops' ) ;
2026-04-08 18:57:19 +08:00
class LivingSyScan extends LivingModule {
constructor ( options = { } ) {
super ( {
moduleId : 'ZY-MOD-SY-SCAN' ,
name : '大脑结构巡检活模块' ,
moduleType : 'guard' ,
owner : 'zhuyuan' ,
db : options . db || db ,
config : {
version : '2.0.0' ,
description : '大脑认知结构健康巡检' ,
heartbeatInterval : 120000 // 巡检模块心跳2分钟
}
} ) ;
// 上次巡检结果缓存
this . _lastReport = null ;
}
/ * *
* 自定义诊断检查
* /
async _diagnoseChecks ( ) {
const issues = [ ] ;
// 检查 brain_nodes 表是否可用
try {
const result = await db . query (
"SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE parent_id IS NULL AND node_type != 'folder') as orphans FROM brain_nodes WHERE status = 'active'"
) ;
const total = parseInt ( result . rows [ 0 ] . total , 10 ) ;
const orphans = parseInt ( result . rows [ 0 ] . orphans , 10 ) ;
if ( total > 0 && orphans / total > 0.3 ) {
issues . push ( {
code : 'HIGH_ORPHAN_RATIO' ,
severity : 'warning' ,
message : ` 孤岛节点比例过高: ${ orphans } / ${ total } ( ${ Math . round ( orphans / total * 100 ) } %) ` ,
suggestion : '运行分类Agent或手动整理'
} ) ;
}
} catch ( err ) {
issues . push ( {
code : 'SCAN_DB_FAIL' ,
severity : 'warning' ,
message : ` 大脑结构查询失败: ${ err . message } ` ,
suggestion : '检查数据库状态'
} ) ;
}
// 如果有上次巡检结果,检查健康分
if ( this . _lastReport && this . _lastReport . health _score < 60 ) {
issues . push ( {
code : 'LOW_BRAIN_HEALTH' ,
severity : 'warning' ,
message : ` 大脑结构健康分偏低: ${ this . _lastReport . health _score } ` ,
suggestion : '清理断链和空目录'
} ) ;
}
return issues ;
}
/ * *
* 自愈动作
* /
async _healAction ( issue ) {
switch ( issue . code ) {
case 'HIGH_ORPHAN_RATIO' :
// 记录并上报·不自动修改认知结构
return { action : 'orphan_report_logged' , success : true , details : { reported : true } } ;
case 'LOW_BRAIN_HEALTH' :
// 尝试清理空目录
try {
await db . query (
2026-04-08 19:00:48 +08:00
"UPDATE brain_nodes bn SET status = 'archived' WHERE bn.node_type = 'folder' AND bn.status = 'active' AND NOT EXISTS (SELECT 1 FROM brain_nodes child WHERE child.parent_id = bn.id AND child.status = 'active')"
2026-04-08 18:57:19 +08:00
) ;
return { action : 'empty_folders_archived' , success : true } ;
} catch ( err ) {
return { action : 'empty_folders_archived' , success : false , error : err . message } ;
}
default :
return await super . _healAction ( issue ) ;
}
}
}
/ * *
* 兼容旧调度器的 run ( ) 接口
* /
2026-04-03 15:33:09 +08:00
async function run ( config ) {
const report = await scanStructure ( {
checks : [ 'orphans' , 'broken_links' , 'duplicates' , 'empty_folders' ]
} ) ;
const issues = [ ] ;
if ( report . orphan _nodes . length > 0 ) {
issues . push ( ` ${ report . orphan _nodes . length } 个孤岛节点 ` ) ;
}
if ( report . broken _links . length > 0 ) {
issues . push ( ` ${ report . broken _links . length } 个断链 ` ) ;
}
if ( report . duplicate _titles . length > 0 ) {
issues . push ( ` ${ report . duplicate _titles . length } 组重复标题 ` ) ;
}
if ( report . empty _folders . length > 0 ) {
issues . push ( ` ${ report . empty _folders . length } 个空目录 ` ) ;
}
2026-04-08 18:57:19 +08:00
// 通过HLDP上报
if ( config && config . bus ) {
try {
await config . bus . send ( 'ZY-MOD-SY-SCAN' , 'ZY-MOD-SCHEDULER' , 'event' , {
event : 'brain_scan_complete' ,
health _score : report . health _score ,
total _nodes : report . total _nodes ,
issueCount : issues . length
} ) ;
} catch ( err ) {
// 非关键
}
}
2026-04-03 15:33:09 +08:00
return {
message : issues . length === 0
? ` 大脑结构健康 · ${ report . total _nodes } 个节点 · 评分 ${ report . health _score } `
: ` 发现 ${ issues . length } 类问题: ${ issues . join ( ', ' ) } · 评分 ${ report . health _score } ` ,
details : report
} ;
}
2026-04-08 18:57:19 +08:00
module . exports = { run , LivingSyScan } ;