feat(hygiene): anti-redundancy enforcement — spec rules + weekly audit

Root cause analysis of the 2026-07-20 redundancy incident:
1. No single-source-of-truth rule -> same file legitimately lived in 4+
   locations, diverging silently
2. Relative path resolution (Path(__file__).parent/'data') -> each
   hardlinked copy of mofin_db.py pointed to a DIFFERENT database
3. 'Backup habit' left .bak/legacy files in production dirs, which
   monitoring then scanned and reported as false alarms
4. Half-done migrations: DB tables created but old JSON writers/readers
   stayed (price_events), old files stayed
5. Dead modules never got buried: xiaoguo 'dead' but bot ran 8 days
   as root eating 2.5GB
6. Monitoring checked 'does it exist' not 'is it alive' -> stale file
   mtime reported as 'pipeline stalled 14 days' (false alarm)
7. No 'system hygiene' as a check category at all

Prevention implemented:
- dev-spec.md v2.0: 五条红线 -> 十条红线
  #6 single source of truth (hardlink only, no independent copies)
  #7 absolute data paths only (no __file__-relative data resolution)
  #8 no backups/legacy in production data dirs (archive immediately)
  #9 dead module burial checklist (6 mandatory steps)
  #10 monitor liveness (DB table freshness) not existence
- File Location Constitution: canonical location per content type
- NEW system_hygiene_audit.py: weekly Monday 07:30 cron checking
  diverged copies / broken hardlinks / zombie processes / orphan data
  files / dead cron scripts / DB freshness -> hygiene_report.json + XMPP
- specs/hygiene.json: module spec per red line #1
- Verified: audit found 5 real issues on first run, all fixed, re-run clean
This commit is contained in:
hmo
2026-07-20 19:04:05 +08:00
parent d5b8bec897
commit 4f83ee8a01
21 changed files with 1286 additions and 3 deletions
+49
View File
@@ -0,0 +1,49 @@
{
"module": "hygiene",
"version": "1.0",
"purpose": "系统卫生审计——防止冗余/废弃/僵尸问题复发的每周自动检查(红线6-10 的 enforcement",
"human_help": {
"title": "系统卫生审计说明",
"description": [
"每周一 07:30 自动运行 system_hygiene_audit.py,检查六类系统卫生问题并推送 XMPP 报告。",
"这是 2026-07-20 冗余事件(三个数据库、31个分叉文件、小果僵尸进程)后建立的防复发机制。"
],
"usage": [
"手动运行: cd /home/hmo/.hermes/profiles/position-analyst/scripts && python3 system_hygiene_audit.py",
"查看报告: /home/hmo/MoFin/gateway/logs/hygiene_report.json"
],
"troubleshooting": [
"分叉副本(diverged_copy): 同名 .py 在不同位置内容不同 → 归档旧副本或硬链接到权威版",
"断裂硬链接(broken_hardlink): deploy 和 profile scripts 内容不一致 → 跑 sync_profile_scripts.sh",
"孤儿文件(orphan_data_file): 生产数据目录 >14 天的文件 → 归档到 archive/(红线8"
]
},
"ai_spec": {
"apis": [],
"dependencies": [
"deploy/profile-scripts/system_hygiene_audit.py",
"deploy/profile-scripts/sync_profile_scripts.sh",
"docs/dev-spec.md 红线 6-10"
],
"constraints": [
"检查项: 分叉副本/断裂硬链接/僵尸进程/孤儿文件/死cron/DB新鲜度",
"docker 容器内进程豁免僵尸检测",
"报告写入 gateway/logs/hygiene_report.json,有问题推 XMPP(:5805)",
"活文件注册表 LIVE_DATA_FILES 需随新数据文件引入而更新"
],
"must_not": [
"禁止只报告不修复——发现问题必须在当轮处理或创建 TODO",
"禁止将生产数据目录下 >14 天文件默认视为正常"
],
"tests": [
{"id": "H1", "name": "人为制造分叉副本,下一轮审计能发现"},
{"id": "H2", "name": "sync 断裂后审计报 broken_hardlink"},
{"id": "H3", "name": "全部检查通过时 status=ok 且不推 XMPP"}
],
"related_files": [
"deploy/profile-scripts/system_hygiene_audit.py",
"deploy/profile-scripts/sync_profile_scripts.sh",
"gateway/logs/hygiene_report.json"
]
}
}