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
+32
View File
@@ -0,0 +1,32 @@
import json, urllib.request
msg = """[笑笑] price_events.json 已彻底退役 — 全系统只认 DB
按你的指示,所有和 price_events.json 相关的代码已全部改完:
【代码修改(6个文件)】
1. price_monitor.py — record_event 只写 DBEVENTS_PATH/load_events/save_events 全部删除
2. strategy_feedback.py — 价格事件只从 DB 读,JSON fallback 删除
3. system_health_check.py — 文件检查清单移除 price_events.json,事件统计只读 DB
4. mo_config.py — 删除无调用的 price_events_path 死属性
5. mofin_health.py — 新鲜度检查改回读 DB 表(现在它是权威了)
6. mofin_db.py — 修了一个更大的坑 ⬇️
【顺藤摸出的更大的坑:三个数据库】
mofin_db.py 的 DB_PATH 原来是相对路径(__file__.parent/data),导致每个硬链接副本解析到不同的库:主库、web-dashboard 库、还有 profile 本地一个几乎全空的第三库。健康检查之前读到 0 条事件就是因为读到了那个空库。已改为绝对路径 /home/hmo/MoFin/data4 份 mofin_db 副本已统一硬链接。
【验证结果】
✅ record_event 只写 DBJSON 不再被重建
✅ system_health_check:历史事件 6353 条 / 今日事件 2965 条(之前误报 0/0)
✅ strategy_feedback / price_monitor 完整跑通
✅ price_events.json 已归档 trashbox(数据早已全量回填 DB)
JSON 时代结束。"""
payload = json.dumps({"to": "hmo@yoin.fun", "body": msg, "type": "chat"}).encode()
req = urllib.request.Request("http://127.0.0.1:5805/", data=payload,
headers={"Content-Type": "application/json"})
try:
print("XMPP:", urllib.request.urlopen(req, timeout=10).read().decode()[:80])
except Exception as e:
print("XMPP fail:", e)