nexo-brain 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (299) hide show
  1. package/README.md +1 -1
  2. package/bin/nexo-brain.js +92 -9
  3. package/bin/postinstall.js +22 -15
  4. package/package.json +7 -4
  5. package/src/auto_update.py +194 -5
  6. package/src/crons/sync.py +6 -2
  7. package/src/db/_core.py +1 -0
  8. package/src/db/_entities.py +1 -0
  9. package/src/db/_episodic.py +1 -0
  10. package/src/db/_learnings.py +1 -0
  11. package/src/db/_reminders.py +1 -0
  12. package/src/db/_schema.py +11 -1
  13. package/src/db/_sessions.py +1 -0
  14. package/src/db/_skills.py +1 -0
  15. package/src/hooks/capture-tool-logs.sh +23 -6
  16. package/src/hooks/session-start.sh +4 -3
  17. package/src/plugin_loader.py +1 -0
  18. package/src/plugins/update.py +377 -26
  19. package/src/scripts/deep-sleep/apply_findings.py +1 -0
  20. package/src/scripts/deep-sleep/collect.py +1 -0
  21. package/src/scripts/deep-sleep/extract.py +1 -0
  22. package/src/scripts/deep-sleep/synthesize.py +1 -0
  23. package/src/scripts/nexo-catchup.py +29 -4
  24. package/src/scripts/nexo-daily-self-audit.py +21 -1
  25. package/src/scripts/nexo-evolution-run.py +21 -1
  26. package/src/scripts/nexo-learning-housekeep.py +1 -0
  27. package/src/scripts/nexo-postmortem-consolidator.py +34 -9
  28. package/src/scripts/nexo-sleep.py +32 -10
  29. package/src/scripts/nexo-synthesis.py +29 -9
  30. package/src/scripts/nexo-update.sh +109 -7
  31. package/src/scripts/nexo-watchdog.sh +122 -58
  32. package/src/server.py +66 -1
  33. package/src/tools_coordination.py +1 -0
  34. package/src/tools_sessions.py +1 -0
  35. package/scripts/migrate-to-unified 2.sh +0 -813
  36. package/scripts/migrate-to-unified.sh +0 -813
  37. package/scripts/migrate-v1.5-to-v1.6 2.py +0 -778
  38. package/scripts/migrate-v1.5-to-v1.6.py +0 -778
  39. package/scripts/migrate-v1.7-to-v1.8 2.py +0 -214
  40. package/scripts/migrate-v1.7-to-v1.8.py +0 -214
  41. package/scripts/nexo-preflight.sh +0 -236
  42. package/scripts/pre-commit-check 2.sh +0 -55
  43. package/scripts/pre-commit-check.sh +0 -55
  44. package/src/__pycache__/auto_close_sessions.cpython-314.pyc +0 -0
  45. package/src/__pycache__/auto_update.cpython-310.pyc +0 -0
  46. package/src/__pycache__/hnsw_index.cpython-310.pyc +0 -0
  47. package/src/__pycache__/hnsw_index.cpython-314.pyc +0 -0
  48. package/src/__pycache__/kg_populate.cpython-310.pyc +0 -0
  49. package/src/__pycache__/knowledge_graph.cpython-310.pyc +0 -0
  50. package/src/__pycache__/plugin_loader.cpython-310.pyc +0 -0
  51. package/src/__pycache__/plugin_loader.cpython-314.pyc +0 -0
  52. package/src/__pycache__/tools_coordination.cpython-310.pyc +0 -0
  53. package/src/__pycache__/tools_credentials.cpython-310.pyc +0 -0
  54. package/src/__pycache__/tools_learnings.cpython-310.pyc +0 -0
  55. package/src/__pycache__/tools_menu.cpython-310.pyc +0 -0
  56. package/src/__pycache__/tools_reminders.cpython-310.pyc +0 -0
  57. package/src/__pycache__/tools_reminders_crud.cpython-310.pyc +0 -0
  58. package/src/__pycache__/tools_sessions.cpython-310.pyc +0 -0
  59. package/src/__pycache__/tools_task_history.cpython-310.pyc +0 -0
  60. package/src/auto_close_sessions 2.py +0 -159
  61. package/src/auto_update 2.py +0 -634
  62. package/src/claim_graph 2.py +0 -323
  63. package/src/cognitive/__init__ 2.py +0 -62
  64. package/src/cognitive/__pycache__/__init__.cpython-310.pyc +0 -0
  65. package/src/cognitive/__pycache__/__init__.cpython-312.pyc +0 -0
  66. package/src/cognitive/__pycache__/__init__.cpython-314.pyc +0 -0
  67. package/src/cognitive/__pycache__/_core.cpython-310.pyc +0 -0
  68. package/src/cognitive/__pycache__/_core.cpython-312.pyc +0 -0
  69. package/src/cognitive/__pycache__/_core.cpython-314.pyc +0 -0
  70. package/src/cognitive/__pycache__/_decay.cpython-310.pyc +0 -0
  71. package/src/cognitive/__pycache__/_decay.cpython-312.pyc +0 -0
  72. package/src/cognitive/__pycache__/_decay.cpython-314.pyc +0 -0
  73. package/src/cognitive/__pycache__/_ingest.cpython-310.pyc +0 -0
  74. package/src/cognitive/__pycache__/_ingest.cpython-312.pyc +0 -0
  75. package/src/cognitive/__pycache__/_ingest.cpython-314.pyc +0 -0
  76. package/src/cognitive/__pycache__/_memory.cpython-310.pyc +0 -0
  77. package/src/cognitive/__pycache__/_memory.cpython-312.pyc +0 -0
  78. package/src/cognitive/__pycache__/_memory.cpython-314.pyc +0 -0
  79. package/src/cognitive/__pycache__/_search.cpython-310.pyc +0 -0
  80. package/src/cognitive/__pycache__/_search.cpython-312.pyc +0 -0
  81. package/src/cognitive/__pycache__/_search.cpython-314.pyc +0 -0
  82. package/src/cognitive/__pycache__/_trust.cpython-310.pyc +0 -0
  83. package/src/cognitive/__pycache__/_trust.cpython-312.pyc +0 -0
  84. package/src/cognitive/__pycache__/_trust.cpython-314.pyc +0 -0
  85. package/src/cognitive/_core 2.py +0 -567
  86. package/src/cognitive/_decay 2.py +0 -382
  87. package/src/cognitive/_ingest 2.py +0 -892
  88. package/src/cognitive/_memory 2.py +0 -912
  89. package/src/cognitive/_search 2.py +0 -949
  90. package/src/cognitive/_trust 2.py +0 -464
  91. package/src/crons/__pycache__/sync.cpython-314.pyc +0 -0
  92. package/src/crons/manifest 2.json +0 -106
  93. package/src/crons/sync 2.py +0 -217
  94. package/src/dashboard/__init__ 2.py +0 -0
  95. package/src/dashboard/__pycache__/__init__.cpython-310.pyc +0 -0
  96. package/src/dashboard/__pycache__/app.cpython-310.pyc +0 -0
  97. package/src/dashboard/app 2.py +0 -789
  98. package/src/db/__init__ 2.py +0 -89
  99. package/src/db/__pycache__/__init__.cpython-310.pyc +0 -0
  100. package/src/db/__pycache__/__init__.cpython-312.pyc +0 -0
  101. package/src/db/__pycache__/__init__.cpython-314.pyc +0 -0
  102. package/src/db/__pycache__/_core.cpython-310.pyc +0 -0
  103. package/src/db/__pycache__/_core.cpython-312.pyc +0 -0
  104. package/src/db/__pycache__/_core.cpython-314.pyc +0 -0
  105. package/src/db/__pycache__/_credentials.cpython-310.pyc +0 -0
  106. package/src/db/__pycache__/_credentials.cpython-312.pyc +0 -0
  107. package/src/db/__pycache__/_credentials.cpython-314.pyc +0 -0
  108. package/src/db/__pycache__/_cron_runs.cpython-310.pyc +0 -0
  109. package/src/db/__pycache__/_cron_runs.cpython-314.pyc +0 -0
  110. package/src/db/__pycache__/_entities.cpython-310.pyc +0 -0
  111. package/src/db/__pycache__/_entities.cpython-312.pyc +0 -0
  112. package/src/db/__pycache__/_entities.cpython-314.pyc +0 -0
  113. package/src/db/__pycache__/_episodic.cpython-310.pyc +0 -0
  114. package/src/db/__pycache__/_episodic.cpython-312.pyc +0 -0
  115. package/src/db/__pycache__/_episodic.cpython-314.pyc +0 -0
  116. package/src/db/__pycache__/_evolution.cpython-310.pyc +0 -0
  117. package/src/db/__pycache__/_evolution.cpython-312.pyc +0 -0
  118. package/src/db/__pycache__/_evolution.cpython-314.pyc +0 -0
  119. package/src/db/__pycache__/_fts.cpython-310.pyc +0 -0
  120. package/src/db/__pycache__/_fts.cpython-312.pyc +0 -0
  121. package/src/db/__pycache__/_fts.cpython-314.pyc +0 -0
  122. package/src/db/__pycache__/_learnings.cpython-310.pyc +0 -0
  123. package/src/db/__pycache__/_learnings.cpython-312.pyc +0 -0
  124. package/src/db/__pycache__/_learnings.cpython-314.pyc +0 -0
  125. package/src/db/__pycache__/_reminders.cpython-310.pyc +0 -0
  126. package/src/db/__pycache__/_reminders.cpython-312.pyc +0 -0
  127. package/src/db/__pycache__/_reminders.cpython-314.pyc +0 -0
  128. package/src/db/__pycache__/_schema.cpython-310.pyc +0 -0
  129. package/src/db/__pycache__/_schema.cpython-312.pyc +0 -0
  130. package/src/db/__pycache__/_schema.cpython-314.pyc +0 -0
  131. package/src/db/__pycache__/_sessions.cpython-310.pyc +0 -0
  132. package/src/db/__pycache__/_sessions.cpython-312.pyc +0 -0
  133. package/src/db/__pycache__/_sessions.cpython-314.pyc +0 -0
  134. package/src/db/__pycache__/_skills.cpython-310.pyc +0 -0
  135. package/src/db/__pycache__/_skills.cpython-312.pyc +0 -0
  136. package/src/db/__pycache__/_skills.cpython-314.pyc +0 -0
  137. package/src/db/__pycache__/_tasks.cpython-310.pyc +0 -0
  138. package/src/db/__pycache__/_tasks.cpython-312.pyc +0 -0
  139. package/src/db/__pycache__/_tasks.cpython-314.pyc +0 -0
  140. package/src/db/_core 2.py +0 -417
  141. package/src/db/_credentials 2.py +0 -124
  142. package/src/db/_entities 2.py +0 -178
  143. package/src/db/_episodic 2.py +0 -738
  144. package/src/db/_evolution 2.py +0 -54
  145. package/src/db/_fts 2.py +0 -406
  146. package/src/db/_learnings 2.py +0 -168
  147. package/src/db/_reminders 2.py +0 -338
  148. package/src/db/_schema 2.py +0 -364
  149. package/src/db/_sessions 2.py +0 -300
  150. package/src/db/_tasks 2.py +0 -91
  151. package/src/evolution_cycle 2.py +0 -266
  152. package/src/hnsw_index 2.py +0 -254
  153. package/src/hooks/auto_capture 2.py +0 -208
  154. package/src/hooks/caffeinate-guard 2.sh +0 -8
  155. package/src/hooks/capture-session 2.sh +0 -21
  156. package/src/hooks/capture-tool-logs 2.sh +0 -127
  157. package/src/hooks/daily-briefing-check 2.sh +0 -33
  158. package/src/hooks/inbox-hook 2.sh +0 -76
  159. package/src/hooks/post-compact 2.sh +0 -148
  160. package/src/hooks/pre-compact 2.sh +0 -151
  161. package/src/hooks/session-start 2.sh +0 -268
  162. package/src/hooks/session-stop 2.sh +0 -140
  163. package/src/kg_populate 2.py +0 -290
  164. package/src/knowledge_graph 2.py +0 -257
  165. package/src/maintenance 2.py +0 -59
  166. package/src/migrate_embeddings 2.py +0 -122
  167. package/src/plugin_loader 2.py +0 -202
  168. package/src/plugins/__init__ 2.py +0 -0
  169. package/src/plugins/__pycache__/__init__ 2.cpython-310.pyc +0 -0
  170. package/src/plugins/__pycache__/__init__.cpython-310.pyc +0 -0
  171. package/src/plugins/__pycache__/__init__.cpython-314.pyc +0 -0
  172. package/src/plugins/__pycache__/adaptive_mode 2.cpython-310.pyc +0 -0
  173. package/src/plugins/__pycache__/adaptive_mode.cpython-310.pyc +0 -0
  174. package/src/plugins/__pycache__/adaptive_mode.cpython-314.pyc +0 -0
  175. package/src/plugins/__pycache__/agents 2.cpython-310.pyc +0 -0
  176. package/src/plugins/__pycache__/agents.cpython-310.pyc +0 -0
  177. package/src/plugins/__pycache__/artifact_registry 2.cpython-310.pyc +0 -0
  178. package/src/plugins/__pycache__/artifact_registry.cpython-310.pyc +0 -0
  179. package/src/plugins/__pycache__/backup 2.cpython-310.pyc +0 -0
  180. package/src/plugins/__pycache__/backup.cpython-310.pyc +0 -0
  181. package/src/plugins/__pycache__/cognitive_memory 2.cpython-310.pyc +0 -0
  182. package/src/plugins/__pycache__/cognitive_memory.cpython-310.pyc +0 -0
  183. package/src/plugins/__pycache__/core_rules 2.cpython-310.pyc +0 -0
  184. package/src/plugins/__pycache__/core_rules.cpython-310.pyc +0 -0
  185. package/src/plugins/__pycache__/cortex 2.cpython-310.pyc +0 -0
  186. package/src/plugins/__pycache__/cortex.cpython-310.pyc +0 -0
  187. package/src/plugins/__pycache__/entities 2.cpython-310.pyc +0 -0
  188. package/src/plugins/__pycache__/entities.cpython-310.pyc +0 -0
  189. package/src/plugins/__pycache__/episodic_memory 2.cpython-310.pyc +0 -0
  190. package/src/plugins/__pycache__/episodic_memory.cpython-310.pyc +0 -0
  191. package/src/plugins/__pycache__/evolution 2.cpython-310.pyc +0 -0
  192. package/src/plugins/__pycache__/evolution.cpython-310.pyc +0 -0
  193. package/src/plugins/__pycache__/guard 2.cpython-310.pyc +0 -0
  194. package/src/plugins/__pycache__/guard.cpython-310.pyc +0 -0
  195. package/src/plugins/__pycache__/knowledge_graph_tools 2.cpython-310.pyc +0 -0
  196. package/src/plugins/__pycache__/knowledge_graph_tools.cpython-310.pyc +0 -0
  197. package/src/plugins/__pycache__/preferences 2.cpython-310.pyc +0 -0
  198. package/src/plugins/__pycache__/preferences.cpython-310.pyc +0 -0
  199. package/src/plugins/__pycache__/schedule.cpython-310.pyc +0 -0
  200. package/src/plugins/__pycache__/schedule.cpython-314.pyc +0 -0
  201. package/src/plugins/__pycache__/skills.cpython-310.pyc +0 -0
  202. package/src/plugins/__pycache__/skills.cpython-314.pyc +0 -0
  203. package/src/plugins/__pycache__/update 2.cpython-310.pyc +0 -0
  204. package/src/plugins/__pycache__/update.cpython-310.pyc +0 -0
  205. package/src/plugins/adaptive_mode 2.py +0 -805
  206. package/src/plugins/agents 2.py +0 -52
  207. package/src/plugins/artifact_registry 2.py +0 -450
  208. package/src/plugins/backup 2.py +0 -104
  209. package/src/plugins/cognitive_memory 2.py +0 -564
  210. package/src/plugins/core_rules 2.py +0 -252
  211. package/src/plugins/cortex 2.py +0 -299
  212. package/src/plugins/entities 2.py +0 -67
  213. package/src/plugins/episodic_memory 2.py +0 -533
  214. package/src/plugins/evolution 2.py +0 -115
  215. package/src/plugins/guard 2.py +0 -746
  216. package/src/plugins/knowledge_graph_tools 2.py +0 -105
  217. package/src/plugins/preferences 2.py +0 -47
  218. package/src/plugins/update 2.py +0 -256
  219. package/src/requirements 2.txt +0 -12
  220. package/src/rules/__init__ 2.py +0 -0
  221. package/src/rules/core-rules 2.json +0 -331
  222. package/src/rules/migrate 2.py +0 -207
  223. package/src/scripts/__pycache__/nexo-auto-update.cpython-314.pyc +0 -0
  224. package/src/scripts/__pycache__/nexo-catchup.cpython-314.pyc +0 -0
  225. package/src/scripts/__pycache__/nexo-cognitive-decay.cpython-314.pyc +0 -0
  226. package/src/scripts/__pycache__/nexo-daily-self-audit.cpython-314.pyc +0 -0
  227. package/src/scripts/__pycache__/nexo-evolution-run.cpython-314.pyc +0 -0
  228. package/src/scripts/__pycache__/nexo-followup-hygiene.cpython-314.pyc +0 -0
  229. package/src/scripts/__pycache__/nexo-immune.cpython-314.pyc +0 -0
  230. package/src/scripts/__pycache__/nexo-install.cpython-314.pyc +0 -0
  231. package/src/scripts/__pycache__/nexo-learning-housekeep.cpython-314.pyc +0 -0
  232. package/src/scripts/__pycache__/nexo-learning-validator.cpython-314.pyc +0 -0
  233. package/src/scripts/__pycache__/nexo-migrate.cpython-314.pyc +0 -0
  234. package/src/scripts/__pycache__/nexo-postmortem-consolidator.cpython-314.pyc +0 -0
  235. package/src/scripts/__pycache__/nexo-pre-commit.cpython-314.pyc +0 -0
  236. package/src/scripts/__pycache__/nexo-proactive-dashboard.cpython-314.pyc +0 -0
  237. package/src/scripts/__pycache__/nexo-reflection.cpython-314.pyc +0 -0
  238. package/src/scripts/__pycache__/nexo-runtime-preflight.cpython-314.pyc +0 -0
  239. package/src/scripts/__pycache__/nexo-send-email.cpython-314.pyc +0 -0
  240. package/src/scripts/__pycache__/nexo-send-reply.cpython-314.pyc +0 -0
  241. package/src/scripts/__pycache__/nexo-sleep.cpython-314.pyc +0 -0
  242. package/src/scripts/__pycache__/nexo-synthesis.cpython-314.pyc +0 -0
  243. package/src/scripts/__pycache__/nexo-watchdog-smoke.cpython-314.pyc +0 -0
  244. package/src/scripts/check-context 2.py +0 -264
  245. package/src/scripts/nexo-auto-update 2.py +0 -6
  246. package/src/scripts/nexo-backup 2.sh +0 -25
  247. package/src/scripts/nexo-brain-activation 2.sh +0 -140
  248. package/src/scripts/nexo-catchup 2.py +0 -242
  249. package/src/scripts/nexo-cognitive-decay 2.py +0 -182
  250. package/src/scripts/nexo-daily-self-audit 2.py +0 -552
  251. package/src/scripts/nexo-deep-sleep 2.sh +0 -97
  252. package/src/scripts/nexo-evolution-run 2.py +0 -597
  253. package/src/scripts/nexo-followup-hygiene 2.py +0 -112
  254. package/src/scripts/nexo-github-monitor 2.py +0 -256
  255. package/src/scripts/nexo-immune 2.py +0 -927
  256. package/src/scripts/nexo-inbox-hook 2.sh +0 -74
  257. package/src/scripts/nexo-install 2.py +0 -6
  258. package/src/scripts/nexo-learning-housekeep 2.py +0 -245
  259. package/src/scripts/nexo-learning-validator 2.py +0 -207
  260. package/src/scripts/nexo-migrate 2.py +0 -232
  261. package/src/scripts/nexo-postmortem-consolidator 2.py +0 -421
  262. package/src/scripts/nexo-pre-commit 2.py +0 -120
  263. package/src/scripts/nexo-prevent-sleep 2.sh +0 -29
  264. package/src/scripts/nexo-proactive-dashboard 2.py +0 -345
  265. package/src/scripts/nexo-reflection 2.py +0 -253
  266. package/src/scripts/nexo-runtime-preflight 2.py +0 -274
  267. package/src/scripts/nexo-send-email 2.py +0 -25
  268. package/src/scripts/nexo-send-email.py +0 -25
  269. package/src/scripts/nexo-send-reply 2.py +0 -178
  270. package/src/scripts/nexo-send-reply.py +0 -178
  271. package/src/scripts/nexo-sleep 2.py +0 -592
  272. package/src/scripts/nexo-snapshot-restore 2.sh +0 -35
  273. package/src/scripts/nexo-synthesis 2.py +0 -253
  274. package/src/scripts/nexo-tcc-approve 2.sh +0 -79
  275. package/src/scripts/nexo-update 2.sh +0 -161
  276. package/src/scripts/nexo-watchdog 2.sh +0 -878
  277. package/src/scripts/nexo-watchdog-smoke 2.py +0 -119
  278. package/src/server 2.py +0 -733
  279. package/src/storage_router 2.py +0 -32
  280. package/src/tools_coordination 2.py +0 -102
  281. package/src/tools_credentials 2.py +0 -68
  282. package/src/tools_learnings 2.py +0 -220
  283. package/src/tools_menu 2.py +0 -227
  284. package/src/tools_reminders 2.py +0 -86
  285. package/src/tools_reminders_crud 2.py +0 -159
  286. package/src/tools_sessions 2.py +0 -476
  287. package/src/tools_task_history 2.py +0 -57
  288. package/templates/CLAUDE.md 2.template +0 -63
  289. package/templates/openclaw 2.json +0 -13
  290. package/tests/__init__ 2.py +0 -0
  291. package/tests/__init__.py +0 -0
  292. package/tests/conftest 2.py +0 -71
  293. package/tests/conftest.py +0 -71
  294. package/tests/test_cognitive 2.py +0 -205
  295. package/tests/test_cognitive.py +0 -205
  296. package/tests/test_knowledge_graph 2.py +0 -140
  297. package/tests/test_knowledge_graph.py +0 -140
  298. package/tests/test_migrations 2.py +0 -137
  299. package/tests/test_migrations.py +0 -137
@@ -1,927 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- NEXO Immune System — Health monitor & auto-repair.
4
-
5
- Runs every 30 minutes via LaunchAgent. Checks tokens, LaunchAgents, DBs,
6
- scripts, logs, disk, and remote server crons. Auto-repairs what it can,
7
- alerts via notification on NEW failures.
8
-
9
- Zero external dependencies. Stdlib + sqlite3 + urllib only.
10
- """
11
-
12
- import fcntl
13
- import json
14
- import os
15
- import re
16
- import shlex
17
- import signal
18
- import sqlite3
19
- import ssl
20
- import subprocess
21
- import sys
22
- import time
23
- from datetime import datetime, date, timedelta
24
- from pathlib import Path
25
-
26
- NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
27
- from urllib.request import Request, urlopen
28
- from urllib.error import URLError, HTTPError
29
-
30
- # ─── SSL context for macOS (certifi or system certs) ─────────────────────────
31
- def _make_ssl_context():
32
- """Create an SSL context that works on macOS with Python.org Python."""
33
- # Try certifi first (pip-installed)
34
- try:
35
- import certifi
36
- ctx = ssl.create_default_context(cafile=certifi.where())
37
- return ctx
38
- except ImportError:
39
- pass
40
- # Try macOS system certificates
41
- for ca_path in [
42
- "/etc/ssl/cert.pem",
43
- "/usr/local/etc/openssl/cert.pem",
44
- "/usr/local/etc/openssl@3/cert.pem",
45
- "/opt/homebrew/etc/openssl@3/cert.pem",
46
- ]:
47
- if os.path.exists(ca_path):
48
- ctx = ssl.create_default_context(cafile=ca_path)
49
- return ctx
50
- # Last resort: unverified (still better than crashing)
51
- ctx = ssl.create_default_context()
52
- ctx.check_hostname = False
53
- ctx.verify_mode = ssl.CERT_NONE
54
- return ctx
55
-
56
- SSL_CTX = _make_ssl_context()
57
-
58
- # ─── Paths ────────────────────────────────────────────────────────────────────
59
- HOME = Path.home()
60
- CLAUDE_DIR = NEXO_HOME
61
- COORD_DIR = CLAUDE_DIR / "coordination"
62
- BRAIN_DIR = CLAUDE_DIR / "brain"
63
- SCRIPTS_DIR = CLAUDE_DIR / "scripts"
64
-
65
- IMMUNE_STATUS = COORD_DIR / "immune-status.json"
66
- IMMUNE_LOG = COORD_DIR / "immune-log.json"
67
- LOCK_FILE = COORD_DIR / "immune-process.lock"
68
-
69
- # Configure your alert script here (optional)
70
- # ALERT_SCRIPT = SCRIPTS_DIR / "my-notify.sh"
71
-
72
- CLAUDE_MEM_DB = HOME / ".claude-mem" / "claude-mem.db"
73
-
74
- LAUNCH_AGENTS_DIR = HOME / "Library" / "LaunchAgents"
75
- CLAUDE_CLI = HOME / ".local" / "bin" / "claude"
76
-
77
- NOW = datetime.now()
78
- TODAY = date.today()
79
-
80
- # ─── Config ───────────────────────────────────────────────────────────────────
81
-
82
- # Token checks — configure for your services.
83
- # Supported types: file_text (read file, optional test_url), json_field (check for refresh_token),
84
- # service_account (check for private_key/client_email), hardcoded (direct URL test)
85
- TOKEN_CHECKS = [
86
- # Example: uncomment and configure for your services
87
- # {
88
- # "name": "My API",
89
- # "path": "~/.nexo/my_api_token.txt",
90
- # "type": "file_text",
91
- # "test_url": "https://api.example.com/health?token={token}",
92
- # },
93
- # {
94
- # "name": "My Service Account",
95
- # "path": "~/.nexo/service-account.json",
96
- # "type": "service_account",
97
- # },
98
- ]
99
-
100
- EXPECTED_AGENTS = [
101
- "com.nexo.immune",
102
- "com.nexo.sleep",
103
- "com.nexo.synthesis",
104
- ]
105
-
106
- # SSH check interval — only every 2 hours, not every 30 min
107
- SSH_CHECK_INTERVAL_HOURS = 2
108
-
109
- # Log size thresholds (bytes)
110
- LOG_WARN_SIZE = 10 * 1024 * 1024 # 10 MB
111
- LOG_FAIL_SIZE = 50 * 1024 * 1024 # 50 MB
112
- LOG_TRUNCATE_SIZE = 50 * 1024 * 1024 # 50 MB — auto-truncate threshold
113
-
114
- # Disk thresholds (percentage used)
115
- DISK_WARN_PCT = 85
116
- DISK_FAIL_PCT = 95
117
-
118
- # Quiet hours — no WhatsApp alerts
119
- QUIET_START = 23 # 23:00
120
- QUIET_END = 7 # 07:00
121
-
122
- # Skip execution hours (deep night)
123
- SKIP_START = 0 # 00:00
124
- SKIP_END = 6 # 06:00
125
-
126
- # Max entries in immune-log.json
127
- MAX_LOG_ENTRIES = 500
128
-
129
- # HTTP timeout for token checks
130
- HTTP_TIMEOUT = 10
131
-
132
- # SSH timeout
133
- SSH_TIMEOUT = 15
134
-
135
-
136
- # ─── Helpers ──────────────────────────────────────────────────────────────────
137
-
138
- def load_json(path, default=None):
139
- if not path.exists():
140
- return default if default is not None else {}
141
- try:
142
- return json.loads(path.read_text())
143
- except Exception:
144
- return default if default is not None else {}
145
-
146
-
147
- def save_json(path, data):
148
- path.write_text(json.dumps(data, indent=2, ensure_ascii=False))
149
-
150
-
151
- def is_quiet_hours():
152
- """Check if within WhatsApp quiet hours (23:00 - 07:00)."""
153
- h = NOW.hour
154
- if QUIET_START > QUIET_END:
155
- return h >= QUIET_START or h < QUIET_END
156
- return QUIET_START <= h < QUIET_END
157
-
158
-
159
- def is_skip_hours():
160
- """Check if within skip hours (00:00 - 06:00)."""
161
- return SKIP_START <= NOW.hour < SKIP_END
162
-
163
-
164
- def send_alert(title, message):
165
- """Send alert notification if not in quiet hours.
166
-
167
- Configure ALERT_SCRIPT at the top of this file to enable.
168
- Override this function for custom alerting (email, Slack, etc.).
169
- """
170
- if is_quiet_hours():
171
- print(f" [QUIET] Suppressed alert: {title}")
172
- return False
173
- # Default: log only. Configure ALERT_SCRIPT for active notifications.
174
- print(f" [ALERT] {title}: {message}")
175
- return True
176
-
177
-
178
- def http_get(url, headers=None, timeout=HTTP_TIMEOUT):
179
- """Simple HTTP GET, returns (status_code, body) or (0, error_string)."""
180
- try:
181
- req = Request(url)
182
- if headers:
183
- for k, v in headers.items():
184
- req.add_header(k, v)
185
- with urlopen(req, timeout=timeout, context=SSL_CTX) as resp:
186
- body = resp.read().decode("utf-8", errors="replace")
187
- return resp.status, body
188
- except HTTPError as e:
189
- return e.code, str(e)
190
- except URLError as e:
191
- return 0, str(e.reason)
192
- except Exception as e:
193
- return 0, str(e)
194
-
195
-
196
- def run_cmd(cmd, timeout=30):
197
- """Run a command without invoking a shell. Accepts string or argv list."""
198
- try:
199
- argv = shlex.split(cmd) if isinstance(cmd, str) else list(cmd)
200
- r = subprocess.run(
201
- argv, capture_output=True, text=True, timeout=timeout
202
- )
203
- return r.returncode, r.stdout.strip(), r.stderr.strip()
204
- except subprocess.TimeoutExpired:
205
- return -1, "", "timeout"
206
- except Exception as e:
207
- return -1, "", str(e)
208
-
209
-
210
- def pid_alive(pid):
211
- """Check if a PID is still running."""
212
- try:
213
- os.kill(pid, 0)
214
- return True
215
- except (OSError, ProcessLookupError):
216
- return False
217
-
218
-
219
- # ─── Check Functions ──────────────────────────────────────────────────────────
220
-
221
- def check_tokens():
222
- """Check all configured tokens. Returns list of result dicts."""
223
- results = []
224
-
225
- for tc in TOKEN_CHECKS:
226
- name = tc["name"]
227
- result = {"name": name, "status": "OK", "detail": ""}
228
-
229
- try:
230
- if tc["type"] == "file_text":
231
- path = Path(tc["path"]).expanduser()
232
- if not path.exists():
233
- result["status"] = "FAIL"
234
- result["detail"] = f"Token file missing: {path}"
235
- else:
236
- token = path.read_text().strip()
237
- if not token:
238
- result["status"] = "FAIL"
239
- result["detail"] = "Token file empty"
240
- elif "test_url" in tc:
241
- url = tc["test_url"].format(token=token)
242
- code, body = http_get(url)
243
- if code == 200:
244
- result["detail"] = "HTTP 200 OK"
245
- elif code == 190 or (isinstance(body, str) and "expired" in body.lower()):
246
- result["status"] = "FAIL"
247
- result["detail"] = f"Token expired (HTTP {code})"
248
- else:
249
- result["status"] = "FAIL"
250
- result["detail"] = f"HTTP {code}: {body[:200]}"
251
-
252
- elif tc["type"] == "json_field":
253
- path = Path(tc["path"]).expanduser()
254
- if not path.exists():
255
- result["status"] = "FAIL"
256
- result["detail"] = f"Token file missing: {path}"
257
- else:
258
- data = load_json(path, default=None)
259
- if data is None:
260
- result["status"] = "FAIL"
261
- result["detail"] = "Invalid JSON"
262
- elif "refresh_token" not in data:
263
- result["status"] = "FAIL"
264
- result["detail"] = "No refresh_token in JSON"
265
- else:
266
- result["detail"] = "refresh_token present"
267
-
268
- elif tc["type"] == "service_account":
269
- path = Path(tc["path"]).expanduser()
270
- if not path.exists():
271
- result["status"] = "FAIL"
272
- result["detail"] = f"Service account file missing: {path}"
273
- else:
274
- data = load_json(path, default=None)
275
- if data is None:
276
- result["status"] = "FAIL"
277
- result["detail"] = "Invalid JSON"
278
- elif "private_key" not in data or "client_email" not in data:
279
- result["status"] = "FAIL"
280
- result["detail"] = "Missing private_key or client_email"
281
- else:
282
- result["detail"] = f"SA: {data.get('client_email', '?')[:40]}"
283
-
284
- elif tc["type"] == "hardcoded":
285
- url = tc["test_url"]
286
- headers = {tc["header"]: tc["token"]}
287
- code, body = http_get(url, headers=headers)
288
- if code == 200:
289
- result["detail"] = "HTTP 200 OK"
290
- elif code == 401:
291
- result["status"] = "FAIL"
292
- result["detail"] = "Token unauthorized (401)"
293
- else:
294
- result["status"] = "FAIL"
295
- result["detail"] = f"HTTP {code}: {body[:200]}"
296
-
297
- except Exception as e:
298
- result["status"] = "FAIL"
299
- result["detail"] = f"Exception: {str(e)[:200]}"
300
-
301
- results.append(result)
302
-
303
- return results
304
-
305
-
306
- def check_launch_agents():
307
- """Check that expected LaunchAgents are loaded. Auto-repair if not."""
308
- results = []
309
-
310
- # Get list of loaded agents
311
- rc, stdout, _ = run_cmd("launchctl list")
312
- loaded_labels = set()
313
- if rc == 0:
314
- for line in stdout.splitlines():
315
- parts = line.split("\t")
316
- if len(parts) >= 3:
317
- loaded_labels.add(parts[2])
318
-
319
- for agent in EXPECTED_AGENTS:
320
- result = {"name": agent, "status": "OK", "detail": "", "repaired": False}
321
-
322
- if agent in loaded_labels:
323
- result["detail"] = "Loaded"
324
- else:
325
- # Try auto-repair
326
- plist = LAUNCH_AGENTS_DIR / f"{agent}.plist"
327
- if plist.exists():
328
- rc, out, err = run_cmd(f"launchctl load '{plist}'")
329
- if rc == 0:
330
- result["status"] = "WARN"
331
- result["detail"] = f"Was unloaded, auto-loaded successfully"
332
- result["repaired"] = True
333
- else:
334
- result["status"] = "FAIL"
335
- result["detail"] = f"Unloaded, auto-load failed: {err[:100]}"
336
- else:
337
- result["status"] = "FAIL"
338
- result["detail"] = f"Unloaded, plist not found: {plist}"
339
-
340
- results.append(result)
341
-
342
- return results
343
-
344
-
345
- def check_databases():
346
- """Run PRAGMA integrity_check on known databases."""
347
- results = []
348
-
349
- dbs = [
350
- ("nexo.db", NEXO_HOME / "data" / "nexo.db"),
351
- ("cognitive.db", NEXO_HOME / "data" / "cognitive.db"),
352
- ("claude-mem.db", CLAUDE_MEM_DB),
353
- ]
354
-
355
- for name, path in dbs:
356
- result = {"name": name, "status": "OK", "detail": ""}
357
-
358
- if not path.exists():
359
- result["status"] = "FAIL"
360
- result["detail"] = f"File missing: {path}"
361
- else:
362
- try:
363
- conn = sqlite3.connect(str(path), timeout=5)
364
- cursor = conn.execute("PRAGMA integrity_check")
365
- check_result = cursor.fetchone()[0]
366
- conn.close()
367
- if check_result == "ok":
368
- size_mb = path.stat().st_size / (1024 * 1024)
369
- result["detail"] = f"Integrity OK ({size_mb:.1f} MB)"
370
- else:
371
- result["status"] = "FAIL"
372
- result["detail"] = f"Integrity failed: {check_result[:200]}"
373
- except Exception as e:
374
- result["status"] = "FAIL"
375
- result["detail"] = f"Error: {str(e)[:200]}"
376
-
377
- results.append(result)
378
-
379
- return results
380
-
381
-
382
- def check_scripts():
383
- """Check stale lock files."""
384
- results = []
385
-
386
- # Stale lock files (PID dead)
387
- lock_files = list(COORD_DIR.glob("*.lock"))
388
- for lf in lock_files:
389
- if lf == LOCK_FILE:
390
- continue # Skip our own lock
391
- result = {"name": f"lock:{lf.name}", "status": "OK", "detail": "", "repaired": False}
392
- try:
393
- content = lf.read_text().strip()
394
- if content and content.isdigit():
395
- pid = int(content)
396
- if pid_alive(pid):
397
- result["detail"] = f"PID {pid} alive"
398
- else:
399
- # Auto-repair: remove stale lock
400
- lf.unlink()
401
- result["status"] = "WARN"
402
- result["detail"] = f"PID {pid} dead — lock removed"
403
- result["repaired"] = True
404
- elif content:
405
- # Lock file has non-PID content — check if size 0 (normal flock pattern)
406
- if lf.stat().st_size == 0:
407
- result["detail"] = "Empty lock (flock pattern)"
408
- else:
409
- result["detail"] = f"Non-PID content: {content[:50]}"
410
- else:
411
- result["detail"] = "Empty lock file"
412
- except Exception as e:
413
- result["detail"] = f"Error checking: {e}"
414
- results.append(result)
415
-
416
- return results
417
-
418
-
419
- def check_logs():
420
- """Check log file sizes. Auto-truncate if > 50 MB."""
421
- results = []
422
-
423
- # JSON logs to check
424
- json_logs = [
425
- COORD_DIR / "heartbeat-log.json",
426
- COORD_DIR / "reflection-log.json",
427
- COORD_DIR / "immune-log.json",
428
- COORD_DIR / "ops-board.json",
429
- COORD_DIR / "messages.json",
430
- ]
431
-
432
- # Text logs to check
433
- text_logs = [
434
- COORD_DIR / "heartbeat-stdout.log",
435
- COORD_DIR / "heartbeat-stderr.log",
436
- COORD_DIR / "reflection-stdout.log",
437
- COORD_DIR / "reflection-stderr.log",
438
- COORD_DIR / "immune-stdout.log",
439
- COORD_DIR / "immune-stderr.log",
440
- ]
441
-
442
- for log_path in json_logs + text_logs:
443
- if not log_path.exists():
444
- continue
445
-
446
- result = {"name": log_path.name, "status": "OK", "detail": "", "repaired": False}
447
- size = log_path.stat().st_size
448
- size_mb = size / (1024 * 1024)
449
-
450
- if size >= LOG_FAIL_SIZE:
451
- result["status"] = "FAIL"
452
- result["detail"] = f"{size_mb:.1f} MB — exceeds {LOG_FAIL_SIZE // (1024*1024)} MB"
453
-
454
- # Auto-truncate
455
- try:
456
- if log_path.suffix == ".json":
457
- _truncate_json_log(log_path, keep_entries=200)
458
- else:
459
- _truncate_text_log(log_path, keep_lines=1000)
460
- new_size = log_path.stat().st_size / (1024 * 1024)
461
- result["detail"] += f" -> truncated to {new_size:.1f} MB"
462
- result["repaired"] = True
463
- except Exception as e:
464
- result["detail"] += f" -> truncate failed: {e}"
465
-
466
- elif size >= LOG_WARN_SIZE:
467
- result["status"] = "WARN"
468
- result["detail"] = f"{size_mb:.1f} MB — approaching limit"
469
- else:
470
- result["detail"] = f"{size_mb:.2f} MB"
471
-
472
- results.append(result)
473
-
474
- return results
475
-
476
-
477
- def _truncate_json_log(path, keep_entries=200):
478
- """Truncate a JSON log file to the last N entries."""
479
- data = load_json(path, default=[])
480
- if isinstance(data, list) and len(data) > keep_entries:
481
- data = data[-keep_entries:]
482
- save_json(path, data)
483
- elif isinstance(data, dict):
484
- # Some logs are dicts with a list value
485
- for key in data:
486
- if isinstance(data[key], list) and len(data[key]) > keep_entries:
487
- data[key] = data[key][-keep_entries:]
488
- save_json(path, data)
489
-
490
-
491
- def _truncate_text_log(path, keep_lines=1000):
492
- """Truncate a text log to the last N lines."""
493
- lines = path.read_text().splitlines()
494
- if len(lines) > keep_lines:
495
- path.write_text("\n".join(lines[-keep_lines:]) + "\n")
496
-
497
-
498
- def check_disk():
499
- """Check disk usage via os.statvfs."""
500
- results = []
501
- result = {"name": "disk:/", "status": "OK", "detail": ""}
502
-
503
- try:
504
- st = os.statvfs("/")
505
- total = st.f_frsize * st.f_blocks
506
- avail = st.f_frsize * st.f_bavail
507
- used = total - avail
508
- pct = (used / total) * 100 if total > 0 else 0
509
-
510
- avail_gb = avail / (1024 ** 3)
511
- total_gb = total / (1024 ** 3)
512
-
513
- if pct >= DISK_FAIL_PCT:
514
- result["status"] = "FAIL"
515
- result["detail"] = f"{pct:.1f}% used ({avail_gb:.1f} GB free of {total_gb:.0f} GB)"
516
- elif pct >= DISK_WARN_PCT:
517
- result["status"] = "WARN"
518
- result["detail"] = f"{pct:.1f}% used ({avail_gb:.1f} GB free of {total_gb:.0f} GB)"
519
- else:
520
- result["detail"] = f"{pct:.1f}% used ({avail_gb:.1f} GB free of {total_gb:.0f} GB)"
521
- except Exception as e:
522
- result["status"] = "FAIL"
523
- result["detail"] = f"Error: {e}"
524
-
525
- results.append(result)
526
- return results
527
-
528
-
529
- def check_server_crons():
530
- """Check remote server crons via SSH. Only runs every 2 hours.
531
-
532
- Configure SSH_SERVER_CMD below with your server details if you want
533
- remote health checks. Leave empty to skip.
534
- """
535
- results = []
536
- result = {"name": "remote-server", "status": "OK", "detail": ""}
537
-
538
- # Configure your SSH health check command here (empty = skip)
539
- # Example: 'ssh -p 22 user@myserver.example.com "echo OK"'
540
- SSH_SERVER_CMD = ""
541
-
542
- if not SSH_SERVER_CMD:
543
- result["detail"] = "No remote server configured (SSH_SERVER_CMD empty)"
544
- results.append(result)
545
- return results, False
546
-
547
- # Check if we should run (every 2 hours based on last check)
548
- status = load_json(IMMUNE_STATUS)
549
- last_ssh_str = status.get("last_ssh_check", "")
550
- should_run = True
551
-
552
- if last_ssh_str:
553
- try:
554
- last_ssh = datetime.strptime(last_ssh_str, "%Y-%m-%d %H:%M")
555
- hours_ago = (NOW - last_ssh).total_seconds() / 3600
556
- if hours_ago < SSH_CHECK_INTERVAL_HOURS:
557
- result["detail"] = f"Skipped (last check {hours_ago:.1f}h ago, interval {SSH_CHECK_INTERVAL_HOURS}h)"
558
- should_run = False
559
- except Exception:
560
- pass
561
-
562
- if should_run:
563
- rc, stdout, stderr = run_cmd(SSH_SERVER_CMD, timeout=SSH_TIMEOUT)
564
-
565
- if rc == 0:
566
- result["detail"] = f"Server OK: {stdout[:100]}"
567
- else:
568
- result["status"] = "FAIL"
569
- err_short = (stderr or "unknown error")[:150]
570
- result["detail"] = f"SSH failed (rc={rc}): {err_short}"
571
-
572
- results.append(result)
573
- return results, should_run
574
-
575
-
576
- # ─── Alerting ─────────────────────────────────────────────────────────────────
577
-
578
- def get_system_uptime_minutes():
579
- """Get system uptime in minutes via sysctl."""
580
- try:
581
- r = subprocess.run(
582
- ["sysctl", "-n", "kern.boottime"],
583
- capture_output=True, text=True, timeout=5
584
- )
585
- if r.returncode == 0:
586
- # Format: { sec = 1709000000, usec = 0 } ...
587
- import re as _re
588
- m = _re.search(r'sec\s*=\s*(\d+)', r.stdout)
589
- if m:
590
- boot_ts = int(m.group(1))
591
- return (time.time() - boot_ts) / 60
592
- except Exception:
593
- pass
594
- return 9999 # Assume long uptime if we can't determine
595
-
596
-
597
- def detect_new_failures(current_results, previous_status):
598
- """Compare current results with previous to find NEW failures.
599
-
600
- Includes debounce: SSH/server checks need 2 consecutive failures before alerting.
601
- Includes boot grace: suppresses all alerts within 10 min of system boot.
602
- """
603
- # Boot grace period — suppress alerts when network may still be settling
604
- uptime = get_system_uptime_minutes()
605
- if uptime < 10:
606
- print(f" [GRACE] System uptime {uptime:.0f}min < 10min — suppressing alerts")
607
- return []
608
-
609
- prev_checks = {}
610
- for category in previous_status.get("checks", {}):
611
- for item in previous_status["checks"][category]:
612
- key = f"{category}:{item.get('name', '')}"
613
- prev_checks[key] = item.get("status", "OK")
614
-
615
- # Load consecutive failure counts for debounce
616
- consec_file = COORD_DIR / "immune-consecutive-failures.json"
617
- consec = load_json(consec_file, default={})
618
-
619
- new_failures = []
620
- for category, items in current_results.items():
621
- for item in items:
622
- key = f"{category}:{item.get('name', '')}"
623
- current_status = item.get("status", "OK")
624
- prev_stat = prev_checks.get(key, "OK")
625
-
626
- if current_status in ("FAIL", "WARN"):
627
- consec[key] = consec.get(key, 0) + 1
628
- else:
629
- consec[key] = 0
630
-
631
- # Debounce: server/SSH checks need 2+ consecutive failures
632
- is_server_check = category == "server" or "ssh" in key.lower()
633
- min_consecutive = 2 if is_server_check else 1
634
-
635
- if current_status == "FAIL" and prev_stat != "FAIL":
636
- if consec.get(key, 0) >= min_consecutive:
637
- new_failures.append(item)
638
- elif current_status == "WARN" and prev_stat == "OK":
639
- if consec.get(key, 0) >= min_consecutive:
640
- new_failures.append(item)
641
-
642
- save_json(consec_file, consec)
643
- return new_failures
644
-
645
-
646
- def send_failure_alerts(new_failures):
647
- """Send WhatsApp alerts for new failures. Max 1 alert per 30 min."""
648
- if not new_failures:
649
- return
650
-
651
- # Global alert cooldown — max 1 WhatsApp alert per 30 minutes
652
- cooldown_file = COORD_DIR / "immune-last-alert.txt"
653
- if cooldown_file.exists():
654
- try:
655
- last_alert = datetime.strptime(cooldown_file.read_text().strip(), "%Y-%m-%d %H:%M")
656
- minutes_since = (NOW - last_alert).total_seconds() / 60
657
- if minutes_since < 30:
658
- print(f" [COOLDOWN] Last alert {minutes_since:.0f}min ago — suppressing")
659
- return
660
- except Exception:
661
- pass
662
-
663
- fails = [f for f in new_failures if f["status"] == "FAIL"]
664
- warns = [f for f in new_failures if f["status"] == "WARN"]
665
-
666
- sent = False
667
- if fails:
668
- lines = [f"- {f['name']}: {f['detail']}" for f in fails[:5]]
669
- msg = "\n".join(lines)
670
- if len(fails) > 5:
671
- msg += f"\n... +{len(fails) - 5} more"
672
- sent = send_alert(
673
- "NEXO Immune FAIL",
674
- f"{len(fails)} new failure(s):\n{msg}"
675
- )
676
-
677
- if warns and not fails:
678
- lines = [f"- {f['name']}: {f['detail']}" for f in warns[:3]]
679
- msg = "\n".join(lines)
680
- sent = send_alert(
681
- "NEXO Immune WARN",
682
- f"{len(warns)} new warning(s):\n{msg}"
683
- )
684
-
685
- if sent:
686
- cooldown_file.write_text(NOW.strftime("%Y-%m-%d %H:%M"))
687
-
688
-
689
- # ─── Main ─────────────────────────────────────────────────────────────────────
690
-
691
- def main():
692
- print(f"\n{'='*60}")
693
- print(f"NEXO Immune System — {NOW.strftime('%Y-%m-%d %H:%M:%S')}")
694
- print(f"{'='*60}")
695
-
696
- # Skip hours gate
697
- if is_skip_hours():
698
- print(f"[SKIP] Hour {NOW.hour} is within skip range ({SKIP_START}:00-{SKIP_END}:00). Exiting.")
699
- return
700
-
701
- # Ensure coordination directory exists
702
- COORD_DIR.mkdir(parents=True, exist_ok=True)
703
-
704
- # Process lock (fcntl)
705
- lock_fd = None
706
- try:
707
- lock_fd = open(LOCK_FILE, "w")
708
- fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
709
- except (IOError, OSError):
710
- print("[LOCKED] Another immune instance is running. Exiting.")
711
- if lock_fd:
712
- lock_fd.close()
713
- return
714
-
715
- try:
716
- _run_checks(lock_fd)
717
- finally:
718
- try:
719
- fcntl.flock(lock_fd, fcntl.LOCK_UN)
720
- lock_fd.close()
721
- except Exception:
722
- pass
723
-
724
-
725
- def _run_checks(lock_fd):
726
- """Execute all checks and produce report."""
727
- previous_status = load_json(IMMUNE_STATUS)
728
-
729
- all_results = {}
730
- repairs = []
731
-
732
- # 1. Tokens
733
- print("\n[1/7] Checking tokens...")
734
- all_results["tokens"] = check_tokens()
735
- for r in all_results["tokens"]:
736
- icon = "OK" if r["status"] == "OK" else r["status"]
737
- print(f" [{icon}] {r['name']}: {r['detail']}")
738
-
739
- # 2. LaunchAgents
740
- print("\n[2/7] Checking LaunchAgents...")
741
- all_results["agents"] = check_launch_agents()
742
- for r in all_results["agents"]:
743
- icon = "OK" if r["status"] == "OK" else r["status"]
744
- print(f" [{icon}] {r['name']}: {r['detail']}")
745
- if r.get("repaired"):
746
- repairs.append(f"LaunchAgent {r['name']} reloaded")
747
-
748
- # 3. Databases
749
- print("\n[3/7] Checking databases...")
750
- all_results["databases"] = check_databases()
751
- for r in all_results["databases"]:
752
- icon = "OK" if r["status"] == "OK" else r["status"]
753
- print(f" [{icon}] {r['name']}: {r['detail']}")
754
-
755
- # 4. Scripts & locks
756
- print("\n[4/7] Checking scripts & locks...")
757
- all_results["scripts"] = check_scripts()
758
- for r in all_results["scripts"]:
759
- icon = "OK" if r["status"] == "OK" else r["status"]
760
- print(f" [{icon}] {r['name']}: {r['detail']}")
761
- if r.get("repaired"):
762
- repairs.append(f"Stale lock {r['name']} removed")
763
-
764
- # 5. Logs
765
- print("\n[5/7] Checking log sizes...")
766
- all_results["logs"] = check_logs()
767
- for r in all_results["logs"]:
768
- icon = "OK" if r["status"] == "OK" else r["status"]
769
- print(f" [{icon}] {r['name']}: {r['detail']}")
770
- if r.get("repaired"):
771
- repairs.append(f"Log {r['name']} truncated")
772
-
773
- # 6. Disk
774
- print("\n[6/7] Checking disk usage...")
775
- all_results["disk"] = check_disk()
776
- for r in all_results["disk"]:
777
- icon = "OK" if r["status"] == "OK" else r["status"]
778
- print(f" [{icon}] {r['name']}: {r['detail']}")
779
-
780
- # 7. Server crons
781
- print("\n[7/7] Checking server crons...")
782
- server_results, ssh_ran = check_server_crons()
783
- all_results["server"] = server_results
784
- for r in all_results["server"]:
785
- icon = "OK" if r["status"] == "OK" else r["status"]
786
- print(f" [{icon}] {r['name']}: {r['detail']}")
787
-
788
- # ─── Summary ──────────────────────────────────────────────────────────
789
- counts = {"OK": 0, "WARN": 0, "FAIL": 0}
790
- for category_items in all_results.values():
791
- for item in category_items:
792
- s = item.get("status", "OK")
793
- if s in counts:
794
- counts[s] += 1
795
-
796
- total = sum(counts.values())
797
-
798
- print(f"\n{'─'*60}")
799
- print(f"SUMMARY: {total} checks — {counts['OK']} OK, {counts['WARN']} WARN, {counts['FAIL']} FAIL")
800
- if repairs:
801
- print(f"AUTO-REPAIRS: {len(repairs)}")
802
- for r in repairs:
803
- print(f" - {r}")
804
- print(f"{'─'*60}\n")
805
-
806
- # ─── Detect new failures & alert ──────────────────────────────────────
807
- new_failures = detect_new_failures(all_results, previous_status)
808
- if new_failures:
809
- print(f"[ALERT] {len(new_failures)} new failure(s)/warning(s) detected:")
810
- for nf in new_failures:
811
- print(f" - [{nf['status']}] {nf['name']}: {nf['detail']}")
812
- send_failure_alerts(new_failures)
813
- else:
814
- print("[OK] No new failures.")
815
-
816
- # ─── Save status ──────────────────────────────────────────────────────
817
- status = {
818
- "last_run": NOW.strftime("%Y-%m-%d %H:%M"),
819
- "counts": counts,
820
- "repairs": repairs,
821
- "new_failures": len(new_failures),
822
- "checks": all_results,
823
- }
824
- if ssh_ran:
825
- status["last_ssh_check"] = NOW.strftime("%Y-%m-%d %H:%M")
826
- elif "last_ssh_check" in previous_status:
827
- status["last_ssh_check"] = previous_status["last_ssh_check"]
828
-
829
- save_json(IMMUNE_STATUS, status)
830
-
831
- # ─── Append to log ────────────────────────────────────────────────────
832
- log_entry = {
833
- "ts": NOW.strftime("%Y-%m-%d %H:%M"),
834
- "ok": counts["OK"],
835
- "warn": counts["WARN"],
836
- "fail": counts["FAIL"],
837
- "repairs": len(repairs),
838
- "new_failures": len(new_failures),
839
- }
840
-
841
- log = load_json(IMMUNE_LOG, default=[])
842
- if not isinstance(log, list):
843
- log = []
844
- log.append(log_entry)
845
- if len(log) > MAX_LOG_ENTRIES:
846
- log = log[-MAX_LOG_ENTRIES:]
847
- save_json(IMMUNE_LOG, log)
848
-
849
- print(f"Status saved to {IMMUNE_STATUS}")
850
- print(f"Log appended to {IMMUNE_LOG} ({len(log)} entries)")
851
-
852
- # ─── Stage B: CLI interpretation (only when issues found) ────────────
853
- if counts["FAIL"] > 0 or counts["WARN"] > 2 or repairs:
854
- _run_cli_triage(all_results, repairs, counts)
855
-
856
-
857
- def _run_cli_triage(all_results: dict, repairs: list, counts: dict):
858
- """Pass all findings to Claude CLI for intelligent triage and recommendations."""
859
- if not CLAUDE_CLI.exists():
860
- print("[SKIP] Claude CLI not found, skipping triage")
861
- return
862
-
863
- triage_file = COORD_DIR / "immune-triage.md"
864
- findings_json = json.dumps({
865
- "timestamp": NOW.strftime("%Y-%m-%d %H:%M"),
866
- "counts": counts,
867
- "repairs": repairs,
868
- "checks": all_results,
869
- }, indent=2, default=str)
870
-
871
- prompt = f"""You are the NEXO Immune System triage analyst.
872
-
873
- Below are the raw health check results from a scheduled scan. Your job:
874
-
875
- 1. Identify which failures are REAL problems vs transient/expected
876
- 2. Group related issues (e.g. SSH failure + server cron failure = same root cause)
877
- 3. Prioritize: what needs attention NOW vs can wait
878
- 4. For each real issue, suggest a specific remediation action
879
- 5. Note any patterns across recent runs if visible
880
-
881
- Write a concise triage report to: {triage_file}
882
-
883
- Format:
884
- ## Immune Triage — YYYY-MM-DD HH:MM
885
-
886
- ### Critical (act now)
887
- - ...
888
-
889
- ### Monitor (watch next run)
890
- - ...
891
-
892
- ### Resolved (auto-repaired)
893
- - ...
894
-
895
- ### Patterns
896
- - ...
897
-
898
- Raw findings:
899
- {findings_json}
900
-
901
- Write the report. Be concise — max 40 lines."""
902
-
903
- print("\n[TRIAGE] Running CLI interpretation...")
904
- env = os.environ.copy()
905
- env["NEXO_HEADLESS"] = "1" # Skip stop hook post-mortem
906
- env.pop("CLAUDECODE", None)
907
- env.pop("CLAUDE_CODE", None)
908
-
909
- try:
910
- result = subprocess.run(
911
- [str(CLAUDE_CLI), "-p", prompt, "--model", "opus",
912
- "--output-format", "text",
913
- "--allowedTools", "Read,Write,Edit,Glob,Grep,Bash,mcp__nexo__*"],
914
- capture_output=True, text=True, timeout=21600, env=env
915
- )
916
- if result.returncode == 0:
917
- print(f"[TRIAGE] Report written to {triage_file}")
918
- else:
919
- print(f"[TRIAGE] CLI exited {result.returncode}: {result.stderr[:200]}")
920
- except subprocess.TimeoutExpired:
921
- print("[TRIAGE] CLI timed out (120s)")
922
- except Exception as e:
923
- print(f"[TRIAGE] Error: {e}")
924
-
925
-
926
- if __name__ == "__main__":
927
- main()