nexo-brain 5.3.19 → 5.3.21

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 (211) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/bin/nexo-brain.js +52 -10
  3. package/package.json +1 -1
  4. package/src/auto_update.py +11 -8
  5. package/src/dashboard/static/favicon 2.svg +32 -0
  6. package/src/dashboard/static/nexo-logo 2.png +0 -0
  7. package/src/dashboard/static/nexo-logo 2.svg +40 -0
  8. package/src/dashboard/static/style 2.css +2458 -0
  9. package/src/dashboard/templates/adaptive 2.html +118 -0
  10. package/src/dashboard/templates/artifacts 2.html +133 -0
  11. package/src/dashboard/templates/backups 2.html +136 -0
  12. package/src/dashboard/templates/base 2.html +417 -0
  13. package/src/dashboard/templates/calendar 2.html +591 -0
  14. package/src/dashboard/templates/chat 2.html +356 -0
  15. package/src/dashboard/templates/claims 2.html +259 -0
  16. package/src/dashboard/templates/cortex 2.html +321 -0
  17. package/src/dashboard/templates/credentials 2.html +128 -0
  18. package/src/dashboard/templates/crons 2.html +370 -0
  19. package/src/dashboard/templates/dashboard 2.html +494 -0
  20. package/src/dashboard/templates/dreams 2.html +252 -0
  21. package/src/dashboard/templates/email 2.html +160 -0
  22. package/src/dashboard/templates/evolution 2.html +189 -0
  23. package/src/dashboard/templates/feed 2.html +249 -0
  24. package/src/dashboard/templates/followup_health 2.html +170 -0
  25. package/src/dashboard/templates/graph 2.html +201 -0
  26. package/src/dashboard/templates/guard 2.html +259 -0
  27. package/src/dashboard/templates/inbox 2.html +251 -0
  28. package/src/dashboard/templates/memory 2.html +420 -0
  29. package/src/dashboard/templates/operations 2.html +608 -0
  30. package/src/dashboard/templates/plugins 2.html +185 -0
  31. package/src/dashboard/templates/protocol 2.html +199 -0
  32. package/src/dashboard/templates/rules 2.html +246 -0
  33. package/src/dashboard/templates/sentiment 2.html +247 -0
  34. package/src/dashboard/templates/sessions 2.html +218 -0
  35. package/src/dashboard/templates/skills 2.html +329 -0
  36. package/src/dashboard/templates/somatic 2.html +73 -0
  37. package/src/dashboard/templates/triggers 2.html +133 -0
  38. package/src/dashboard/templates/trust 2.html +360 -0
  39. package/src/db/__init__ 2.py +259 -0
  40. package/src/db/_core 2.py +437 -0
  41. package/src/db/_credentials 2.py +124 -0
  42. package/src/db/_episodic 2.py +762 -0
  43. package/src/db/_evolution 2.py +54 -0
  44. package/src/db/_fts 2.py +406 -0
  45. package/src/db/_goal_profiles 2.py +376 -0
  46. package/src/db/_hot_context 2.py +660 -0
  47. package/src/db/_outcomes 2.py +800 -0
  48. package/src/db/_personal_scripts 2.py +582 -0
  49. package/src/db/_sessions 2.py +330 -0
  50. package/src/db/_tasks 2.py +91 -0
  51. package/src/db/_watchers 2.py +173 -0
  52. package/src/doctor/formatters 2.py +52 -0
  53. package/src/doctor/models 2.py +69 -0
  54. package/src/doctor/planes 2.py +87 -0
  55. package/src/doctor/providers/__init__ 2.py +1 -0
  56. package/src/doctor/providers/deep 2.py +367 -0
  57. package/src/evolution_cycle 2.py +519 -0
  58. package/src/hooks/auto_capture 2.py +208 -0
  59. package/src/hooks/caffeinate-guard 2.sh +8 -0
  60. package/src/hooks/capture-session 2.sh +21 -0
  61. package/src/hooks/capture-tool-logs 2.sh +158 -0
  62. package/src/hooks/daily-briefing-check 2.sh +33 -0
  63. package/src/hooks/heartbeat-enforcement 2.py +90 -0
  64. package/src/hooks/heartbeat-posttool 2.sh +18 -0
  65. package/src/hooks/inbox-hook 2.sh +76 -0
  66. package/src/hooks/post-compact 2.sh +152 -0
  67. package/src/hooks/pre-compact 2.sh +169 -0
  68. package/src/hooks/protocol-guardrail 2.sh +10 -0
  69. package/src/hooks/protocol-pretool-guardrail 2.sh +9 -0
  70. package/src/hooks/session-stop 2.sh +52 -0
  71. package/src/kg_populate 2.py +292 -0
  72. package/src/maintenance 2.py +53 -0
  73. package/src/memory_backends 2.py +71 -0
  74. package/src/migrate_embeddings 2.py +124 -0
  75. package/src/nexo_sdk 2.py +103 -0
  76. package/src/observability 2.py +199 -0
  77. package/src/plugin_loader 2.py +217 -0
  78. package/src/plugins/__init__ 2.py +0 -0
  79. package/src/plugins/artifact_registry 2.py +450 -0
  80. package/src/plugins/backup 2.py +127 -0
  81. package/src/plugins/claims_tools 2.py +119 -0
  82. package/src/plugins/cognitive_memory 2.py +609 -0
  83. package/src/plugins/core_rules 2.py +252 -0
  84. package/src/plugins/cortex 2.py +1155 -0
  85. package/src/plugins/entities 2.py +67 -0
  86. package/src/plugins/episodic_memory 2.py +560 -0
  87. package/src/plugins/evolution 2.py +167 -0
  88. package/src/plugins/goal_engine 2.py +142 -0
  89. package/src/plugins/guard 2.py +862 -0
  90. package/src/plugins/impact 2.py +29 -0
  91. package/src/plugins/knowledge_graph_tools 2.py +137 -0
  92. package/src/plugins/media_memory_tools 2.py +98 -0
  93. package/src/plugins/memory_export 2.py +196 -0
  94. package/src/plugins/outcomes 2.py +130 -0
  95. package/src/plugins/personal_scripts 2.py +117 -0
  96. package/src/plugins/preferences 2.py +47 -0
  97. package/src/plugins/protocol 2.py +1449 -0
  98. package/src/plugins/simple_api 2.py +106 -0
  99. package/src/plugins/skills 2.py +341 -0
  100. package/src/plugins/state_watchers 2.py +79 -0
  101. package/src/plugins/update 2.py +986 -0
  102. package/src/plugins/user_state_tools 2.py +43 -0
  103. package/src/plugins/workflow 2.py +588 -0
  104. package/src/protocol_settings 2.py +59 -0
  105. package/src/public_contribution 2.py +466 -0
  106. package/src/public_evolution_queue 2.py +241 -0
  107. package/src/requirements 2.txt +14 -0
  108. package/src/retroactive_learnings 2.py +373 -0
  109. package/src/rules/__init__ 2.py +0 -0
  110. package/src/rules/core-rules 2.json +331 -0
  111. package/src/rules/migrate 2.py +207 -0
  112. package/src/runtime_power 2.py +874 -0
  113. package/src/script_registry 2.py +1559 -0
  114. package/src/scripts/check-context 2.py +272 -0
  115. package/src/scripts/deep-sleep/apply_findings 2.py +2327 -0
  116. package/src/scripts/deep-sleep/collect 2.py +928 -0
  117. package/src/scripts/deep-sleep/extract 2.py +330 -0
  118. package/src/scripts/deep-sleep/extract-prompt 2.md +285 -0
  119. package/src/scripts/deep-sleep/synthesize 2.py +312 -0
  120. package/src/scripts/deep-sleep/synthesize-prompt 2.md +336 -0
  121. package/src/scripts/nexo-agent-run 2.py +75 -0
  122. package/src/scripts/nexo-auto-update 2.py +6 -0
  123. package/src/scripts/nexo-backup 2.sh +25 -0
  124. package/src/scripts/nexo-brain-activation 2.sh +140 -0
  125. package/src/scripts/nexo-catchup 2.py +300 -0
  126. package/src/scripts/nexo-cognitive-decay 2.py +257 -0
  127. package/src/scripts/nexo-cortex-cycle 2.py +293 -0
  128. package/src/scripts/nexo-cron-wrapper 2.sh +53 -0
  129. package/src/scripts/nexo-daily-self-audit 2.py +2161 -0
  130. package/src/scripts/nexo-dashboard 2.sh +29 -0
  131. package/src/scripts/nexo-deep-sleep 2.sh +86 -0
  132. package/src/scripts/nexo-evolution-run 2.py +1664 -0
  133. package/src/scripts/nexo-followup-hygiene 2.py +139 -0
  134. package/src/scripts/nexo-hook-record 2.py +42 -0
  135. package/src/scripts/nexo-immune 2.py +936 -0
  136. package/src/scripts/nexo-impact-scorer 2.py +117 -0
  137. package/src/scripts/nexo-inbox-hook 2.sh +74 -0
  138. package/src/scripts/nexo-install 2.py +6 -0
  139. package/src/scripts/nexo-learning-housekeep 2.py +401 -0
  140. package/src/scripts/nexo-learning-validator 2.py +266 -0
  141. package/src/scripts/nexo-migrate 2.py +260 -0
  142. package/src/scripts/nexo-outcome-checker 2.py +127 -0
  143. package/src/scripts/nexo-postmortem-consolidator 2.py +456 -0
  144. package/src/scripts/nexo-pre-commit 2.py +120 -0
  145. package/src/scripts/nexo-prevent-sleep 2.sh +35 -0
  146. package/src/scripts/nexo-proactive-dashboard 2.py +354 -0
  147. package/src/scripts/nexo-reflection 2.py +256 -0
  148. package/src/scripts/nexo-runtime-preflight 2.py +274 -0
  149. package/src/scripts/nexo-sleep 2.py +631 -0
  150. package/src/scripts/nexo-snapshot-restore 2.sh +35 -0
  151. package/src/scripts/nexo-sync-clients 2.py +16 -0
  152. package/src/scripts/nexo-synthesis 2.py +475 -0
  153. package/src/scripts/nexo-tcc-approve 2.sh +79 -0
  154. package/src/scripts/nexo-update 2.sh +306 -0
  155. package/src/scripts/nexo-watchdog 2.sh +1207 -0
  156. package/src/scripts/nexo-watchdog-smoke 2.py +119 -0
  157. package/src/scripts/rehydrate_learnings_from_archive 2.py +245 -0
  158. package/src/server 2.py +1296 -0
  159. package/src/skills/run-nexo-audit-phase/guide 2.md +43 -0
  160. package/src/skills/run-nexo-audit-phase/skill 2.json +59 -0
  161. package/src/skills/run-nexo-core-fix-cycle/guide 2.md +17 -0
  162. package/src/skills/run-nexo-core-fix-cycle/script 2.py +276 -0
  163. package/src/skills/run-nexo-core-fix-cycle/skill 2.json +58 -0
  164. package/src/skills/run-release-final-audit/guide 2.md +16 -0
  165. package/src/skills/run-release-final-audit/script 2.py +259 -0
  166. package/src/skills/run-release-final-audit/skill 2.json +77 -0
  167. package/src/skills/run-runtime-doctor/guide 2.md +12 -0
  168. package/src/skills/run-runtime-doctor/script 2.py +21 -0
  169. package/src/skills/run-runtime-doctor/skill 2.json +25 -0
  170. package/src/skills_runtime 2.py +932 -0
  171. package/src/state_watchers_runtime 2.py +475 -0
  172. package/src/storage_router 2.py +32 -0
  173. package/src/system_catalog 2.py +786 -0
  174. package/src/tools_coordination 2.py +103 -0
  175. package/src/tools_credentials 2.py +68 -0
  176. package/src/tools_drive 2.py +487 -0
  177. package/src/tools_hot_context 2.py +163 -0
  178. package/src/tools_learnings 2.py +612 -0
  179. package/src/tools_menu 2.py +229 -0
  180. package/src/tools_reminders 2.py +88 -0
  181. package/src/tools_reminders_crud 2.py +363 -0
  182. package/src/tools_sessions 2.py +1054 -0
  183. package/src/tools_system_catalog 2.py +19 -0
  184. package/src/tools_task_history 2.py +57 -0
  185. package/src/tools_transcripts 2.py +98 -0
  186. package/src/transcript_utils 2.py +412 -0
  187. package/src/user_context 2.py +46 -0
  188. package/src/user_data_portability 2.py +328 -0
  189. package/src/user_state_model 2.py +170 -0
  190. package/templates/CLAUDE.md 2.template +108 -0
  191. package/templates/CODEX.AGENTS.md 2.template +66 -0
  192. package/templates/launchagents/README 2.md +132 -0
  193. package/templates/launchagents/com.nexo.auto-close-sessions 2.plist +39 -0
  194. package/templates/launchagents/com.nexo.catchup 2.plist +39 -0
  195. package/templates/launchagents/com.nexo.cognitive-decay 2.plist +40 -0
  196. package/templates/launchagents/com.nexo.dashboard 2.plist +43 -0
  197. package/templates/launchagents/com.nexo.deep-sleep 2.plist +43 -0
  198. package/templates/launchagents/com.nexo.evolution 2.plist +44 -0
  199. package/templates/launchagents/com.nexo.followup-hygiene 2.plist +45 -0
  200. package/templates/launchagents/com.nexo.immune 2.plist +41 -0
  201. package/templates/launchagents/com.nexo.postmortem 2.plist +45 -0
  202. package/templates/launchagents/com.nexo.self-audit 2.plist +47 -0
  203. package/templates/launchagents/com.nexo.synthesis 2.plist +45 -0
  204. package/templates/launchagents/com.nexo.watchdog 2.plist +37 -0
  205. package/templates/nexo_helper 2.py +301 -0
  206. package/templates/openclaw 2.json +13 -0
  207. package/templates/plugin-template 2.py +40 -0
  208. package/templates/script-template 2.py +59 -0
  209. package/templates/script-template 2.sh +13 -0
  210. package/templates/skill-script-template 2.py +48 -0
  211. package/templates/skill-template 2.md +33 -0
@@ -0,0 +1,257 @@
1
+ #!/usr/bin/env python3
2
+ """NEXO Cognitive Decay — Daily Ebbinghaus sweep + STM→LTM promotion."""
3
+
4
+ import json
5
+ import os
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
10
+ # Auto-detect: if running from repo (src/scripts/), use src/ as NEXO_CODE
11
+ _script_dir = Path(__file__).resolve().parent
12
+ _repo_src = _script_dir.parent # src/scripts/ -> src/
13
+ NEXO_CODE = Path(os.environ.get("NEXO_CODE", str(_repo_src) if (_repo_src / "server.py").exists() else str(NEXO_HOME)))
14
+ from datetime import datetime, timedelta
15
+
16
+ sys.path.insert(0, str(NEXO_CODE))
17
+ import cognitive
18
+
19
+ STATE_FILE = NEXO_HOME / "operations" / ".catchup-state.json"
20
+
21
+
22
+ CORRECTION_FATIGUE_FOLLOWUP_ID = "NF-CORRECTION-FATIGUE"
23
+
24
+
25
+ def _open_correction_fatigue_followup(fatigued: list) -> str:
26
+ """Surface fatigued memories as a single high-priority followup.
27
+
28
+ Closes Fase 3 item 4 of NEXO-AUDIT-2026-04-11. check_correction_fatigue
29
+ already auto-decays the strength of memories corrected 3+ times in a
30
+ week and tags them under_review, but the under_review tag is never
31
+ read elsewhere — the user could lose hours debugging a wrong memory
32
+ before noticing it had been silently decayed. This helper writes a
33
+ single deterministic followup so the operator sees the list at the
34
+ next morning briefing.
35
+
36
+ Idempotent across daily runs via the fixed id NF-CORRECTION-FATIGUE
37
+ (INSERT OR REPLACE). Best-effort: never raises.
38
+ """
39
+ import sqlite3 as _sqlite3
40
+ if not fatigued:
41
+ return "no_signal"
42
+
43
+ db_path = (
44
+ os.environ.get("NEXO_TEST_DB")
45
+ or os.environ.get("NEXO_DB")
46
+ or str(NEXO_HOME / "data" / "nexo.db")
47
+ )
48
+ if not Path(db_path).exists():
49
+ return "skipped_no_db"
50
+
51
+ lines = [
52
+ f"NEXO detected {len(fatigued)} memory/memories corrected 3+ times in the last 7 days.",
53
+ "These have been auto-decayed to strength <= 0.2 and tagged under_review.",
54
+ "Verify each one and either delete it, refine its content, or accept the decay.",
55
+ "",
56
+ ]
57
+ for f in fatigued[:10]:
58
+ lines.append(
59
+ f"- LTM #{f.get('memory_id')} ({f.get('corrections_7d')}x corrections, "
60
+ f"strength={f.get('strength')}): {(f.get('content') or '')[:160]}"
61
+ )
62
+ if len(fatigued) > 10:
63
+ lines.append(f"... and {len(fatigued) - 10} more")
64
+ description = "\n".join(lines)
65
+ verification = (
66
+ "sqlite3 ~/.nexo/data/cognitive.db \"SELECT id, content, strength, tags "
67
+ "FROM ltm_memories WHERE tags LIKE '%under_review%' ORDER BY strength ASC LIMIT 50\""
68
+ )
69
+ now_epoch = datetime.now().timestamp()
70
+
71
+ conn = _sqlite3.connect(db_path)
72
+ try:
73
+ conn.execute(
74
+ "INSERT OR REPLACE INTO followups (id, description, date, status, "
75
+ "verification, created_at, updated_at, priority) "
76
+ "VALUES (?, ?, NULL, 'PENDING', ?, ?, ?, 'high')",
77
+ (CORRECTION_FATIGUE_FOLLOWUP_ID, description, verification, now_epoch, now_epoch),
78
+ )
79
+ conn.commit()
80
+ except Exception as e:
81
+ return f"failed: {e}"
82
+ finally:
83
+ conn.close()
84
+ return "opened_or_refreshed"
85
+
86
+
87
+ def update_catchup_state():
88
+ """Register successful run so catch-up script knows we ran."""
89
+ try:
90
+ state = json.loads(STATE_FILE.read_text()) if STATE_FILE.exists() else {}
91
+ except Exception:
92
+ state = {}
93
+ state["cognitive-decay"] = datetime.now().isoformat()
94
+ STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
95
+ STATE_FILE.write_text(json.dumps(state, indent=2))
96
+
97
+
98
+ def main():
99
+ ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
100
+ print(f"[{ts}] Cognitive decay starting...")
101
+
102
+ # 0. Process quarantine FIRST — promote/reject/expire pending items
103
+ # BUG FIX 26-Mar-2026: quarantine was NEVER processed automatically.
104
+ # 78 items were stuck as pending indefinitely.
105
+ try:
106
+ q_result = cognitive.process_quarantine()
107
+ print(f"[{ts}] Quarantine: {q_result['promoted']} promoted, {q_result['rejected']} rejected, "
108
+ f"{q_result['expired']} expired, {q_result['still_pending']} still pending.")
109
+ except Exception as e:
110
+ print(f"[{ts}] Quarantine processing error: {e}")
111
+
112
+ # 0b. Purge test/dev memories from STM
113
+ try:
114
+ test_purged = cognitive.gc_test_memories()
115
+ if test_purged > 0:
116
+ print(f"[{ts}] Purged {test_purged} test/dev memories from STM.")
117
+ except Exception as e:
118
+ print(f"[{ts}] Test memory purge error: {e}")
119
+
120
+ # 1. Apply decay
121
+ cognitive.apply_decay()
122
+ print(f"[{ts}] Decay applied.")
123
+
124
+ # 2. Promote eligible STM → LTM
125
+ promoted = cognitive.promote_stm_to_ltm()
126
+ print(f"[{ts}] Promoted {promoted} STM memories to LTM.")
127
+
128
+ # 3. Garbage collect expired STM + sensory
129
+ gc_count = cognitive.gc_stm()
130
+ try:
131
+ gc_sensory = cognitive.gc_sensory(max_age_hours=48)
132
+ print(f"[{ts}] GC: removed {gc_count} expired STM, {gc_sensory} expired sensory.")
133
+ except Exception as e:
134
+ print(f"[{ts}] GC: removed {gc_count} expired STM. Sensory GC error: {e}")
135
+
136
+ # 4. Semantic consolidation — merge near-duplicate LTM (cosine > 0.9)
137
+ # With discriminative fusion: siblings (different environments) are linked, not merged
138
+ try:
139
+ result = cognitive.consolidate_semantic(threshold=0.9, dry_run=False)
140
+ merged = result.get("merged", [])
141
+ siblings = result.get("siblings", [])
142
+ if merged:
143
+ print(f"[{ts}] Consolidated {len(merged)} duplicate LTM pairs:")
144
+ for m in merged[:10]:
145
+ print(f"[{ts}] [{m['score']}] kept #{m['keep_id']} ({m['keep_access']} accesses), merged #{m['drop_id']}")
146
+ if siblings:
147
+ print(f"[{ts}] Linked {len(siblings)} sibling pairs (similar-but-incompatible):")
148
+ for s in siblings[:10]:
149
+ print(f"[{ts}] [{s['score']}] #{s['memory_a_id']} <> #{s['memory_b_id']} differ in: {', '.join(s['discriminators'])}")
150
+ if not merged and not siblings:
151
+ print(f"[{ts}] No semantic duplicates or siblings found (threshold=0.9)")
152
+ except Exception as e:
153
+ print(f"[{ts}] Consolidation error: {e}")
154
+
155
+ # 5. Correction fatigue — mark memories corrected 3+ times as unreliable
156
+ # Closes Fase 3 item 4 of NEXO-AUDIT-2026-04-11. check_correction_fatigue
157
+ # already auto-decays strength to 0.2 and tags the memory as
158
+ # 'under_review', but the under_review tag is never read elsewhere in the
159
+ # codebase. The user could lose hours debugging a wrong memory before
160
+ # noticing it had been silently decayed. Surface as a daily followup so
161
+ # the operator sees fatigued memories at the next morning briefing.
162
+ try:
163
+ fatigued = cognitive.check_correction_fatigue()
164
+ if fatigued:
165
+ print(f"[{ts}] CORRECTION FATIGUE: {len(fatigued)} memories corrected 3+ times in 7d:")
166
+ for f in fatigued:
167
+ print(f"[{ts}] LTM #{f['memory_id']} ({f['corrections_7d']}x): {f['content'][:80]}...")
168
+ try:
169
+ _open_correction_fatigue_followup(fatigued)
170
+ except Exception as fe:
171
+ print(f"[{ts}] Correction fatigue followup error: {fe}")
172
+ else:
173
+ print(f"[{ts}] No correction fatigue detected.")
174
+ except Exception as e:
175
+ print(f"[{ts}] Correction fatigue check error: {e}")
176
+
177
+ # 6. Memory Dreaming — discover hidden connections between recent memories
178
+ try:
179
+ dream_result = cognitive.dream_cycle(max_insights=15)
180
+ scanned = dream_result["memories_scanned"]
181
+ created = dream_result["insights_created"]
182
+ candidates = dream_result["candidates_found"]
183
+ print(f"[{ts}] Dream cycle: scanned {scanned} recent memories, {candidates} candidates, {created} insights created.")
184
+ for insight in dream_result["insights"][:10]:
185
+ print(f"[{ts}] [{insight['similarity']}] {insight['title_a'][:40]} <-> {insight['title_b'][:40]}")
186
+ except Exception as e:
187
+ print(f"[{ts}] Dream cycle error: {e}")
188
+
189
+ # 7. Auto-merge duplicates (runs AFTER dream_cycle, higher threshold than consolidation)
190
+ try:
191
+ merge_result = cognitive.auto_merge_duplicates(threshold=0.92)
192
+ if merge_result["merged"] > 0:
193
+ print(f"[{ts}] Auto-merge: scanned {merge_result['scanned']}, merged {merge_result['merged']} duplicates, {merge_result['kept']} kept.")
194
+ for m in merge_result["merge_log"][:10]:
195
+ print(f"[{ts}] [{m['similarity']}] kept #{m['kept_id']}, dropped #{m['dropped_id']}")
196
+ else:
197
+ print(f"[{ts}] Auto-merge: scanned {merge_result['scanned']}, no duplicates above 0.92 threshold.")
198
+ except Exception as e:
199
+ print(f"[{ts}] Auto-merge error: {e}")
200
+
201
+ # 9. Adaptive weight learning — Ridge regression from feedback-annotated entries
202
+ try:
203
+ sys.path.insert(0, str(NEXO_CODE / "plugins"))
204
+ from adaptive_mode import learn_weights, prune_adaptive_log, check_weight_rollback
205
+
206
+ rollback = check_weight_rollback()
207
+ if rollback["status"] == "rolled_back":
208
+ print(f"[{ts}] WEIGHT ROLLBACK: {rollback['reason']}")
209
+ elif rollback["status"] == "ok":
210
+ print(f"[{ts}] Weight health: pre={rollback['pre_rate']}/day, post={rollback['post_rate']}/day")
211
+ elif rollback["status"] != "no_learned_weights":
212
+ print(f"[{ts}] Weight rollback: {rollback['status']}")
213
+
214
+ result = learn_weights()
215
+ if result["status"] in ("shadow", "active"):
216
+ mode_label = "SHADOW" if result["status"] == "shadow" else "ACTIVE"
217
+ print(f"[{ts}] Learned weights ({mode_label}) from {result['samples']} samples. Max drift: {result['max_drift']:.4f}")
218
+ for signal, weight in result["weights"].items():
219
+ drift = result["drift"][signal]
220
+ arrow = "+" if drift > 0 else "" if drift < 0 else "="
221
+ print(f"[{ts}] {signal}: {weight:.4f} ({arrow}{drift:.4f} from static)")
222
+ elif result["status"] == "insufficient_data":
223
+ print(f"[{ts}] Weight learning: {result['samples']}/{result['min_required']} samples (waiting)")
224
+ else:
225
+ print(f"[{ts}] Weight learning: {result['status']}")
226
+
227
+ pruned = prune_adaptive_log(max_age_days=90)
228
+ if pruned > 0:
229
+ print(f"[{ts}] Pruned {pruned} adaptive_log entries >90 days")
230
+ except Exception as e:
231
+ print(f"[{ts}] Adaptive weight learning error: {e}")
232
+
233
+ # 10. Project somatic events from nexo.db -> cognitive.db
234
+ try:
235
+ projected = cognitive.somatic_project_events()
236
+ if projected > 0:
237
+ print(f"[{ts}] Somatic projection: {projected} events projected to cognitive.db")
238
+ except Exception as e:
239
+ print(f"[{ts}] Somatic projection error: {e}")
240
+
241
+ # 11. Somatic marker nightly decay
242
+ try:
243
+ decayed = cognitive.somatic_nightly_decay(gamma=0.95)
244
+ print(f"[{ts}] Somatic decay: {decayed} markers processed (x0.95)")
245
+ except Exception as e:
246
+ print(f"[{ts}] Somatic decay error: {e}")
247
+
248
+ # 8. Stats
249
+ stats = cognitive.get_stats()
250
+ print(f"[{ts}] STM: {stats['stm_active']} active (+{stats.get('stm_promoted', 0)} promoted, {stats.get('stm_total', 0)} total) | LTM: {stats['ltm_active']} active, {stats['ltm_dormant']} dormant")
251
+ print(f"[{ts}] Done.")
252
+
253
+ update_catchup_state()
254
+
255
+
256
+ if __name__ == "__main__":
257
+ main()
@@ -0,0 +1,293 @@
1
+ #!/usr/bin/env python3
2
+ """NEXO Cortex Cycle — continuous quality validation.
3
+
4
+ Scheduled every 6 hours by src/crons/manifest.json (id: cortex-cycle).
5
+ Closes Fase 2 item 6 of NEXO-AUDIT-2026-04-11.
6
+
7
+ Until this script existed, Cortex evaluations only ran when an agent
8
+ explicitly invoked nexo_cortex_decide / nexo_cortex_check during a task.
9
+ There was no continuous loop watching for quality drops, so a degraded
10
+ recommendation pattern could persist indefinitely between user reports.
11
+
12
+ What this script does (idempotent and best-effort):
13
+
14
+ 1. Loads cortex_evaluation_summary for the last 7 days and last 1 day.
15
+ 2. Persists the snapshot to ~/.nexo/operations/cortex-quality-latest.json
16
+ so dashboards / morning briefings can read fresh metrics without
17
+ re-running the SQL.
18
+ 3. Detects degradation signals on the 7-day window. The criteria are
19
+ intentionally conservative to avoid false alarms on small samples:
20
+ a. recommendation_accept_rate < 50% AND total_evaluations >= 10
21
+ b. linked_outcome_success_rate < 50% AND linked_outcomes_resolved >= 5
22
+ c. override_success_rate > recommended_success_rate by >= 20pp
23
+ AND linked_outcomes_resolved >= 5
24
+ 4. Opens (or refreshes) NF-CORTEX-QUALITY-DROP followup with the offending
25
+ metrics when degradation is detected. Idempotent: if a non-PENDING /
26
+ resolved followup of the same id already exists, it is updated in
27
+ place rather than duplicated.
28
+ 5. Logs every run to ~/.nexo/logs/cortex-cycle.log.
29
+
30
+ Catchup-friendly: a stale plist firing twice in quick succession is fine.
31
+ The quality file is rewritten in place, the followup is upserted, no
32
+ mutating side effects beyond those.
33
+ """
34
+
35
+ from __future__ import annotations
36
+
37
+ import json
38
+ import os
39
+ import sys
40
+ from datetime import datetime
41
+ from pathlib import Path
42
+
43
+ NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
44
+ _script_dir = Path(__file__).resolve().parent
45
+ _repo_src = _script_dir.parent # src/scripts/ -> src/
46
+ NEXO_CODE = Path(
47
+ os.environ.get(
48
+ "NEXO_CODE",
49
+ str(_repo_src) if (_repo_src / "server.py").exists() else str(NEXO_HOME),
50
+ )
51
+ )
52
+ sys.path.insert(0, str(NEXO_CODE))
53
+
54
+ OPERATIONS_DIR = NEXO_HOME / "operations"
55
+ LOGS_DIR = NEXO_HOME / "logs"
56
+ QUALITY_FILE = OPERATIONS_DIR / "cortex-quality-latest.json"
57
+ LOG_FILE = LOGS_DIR / "cortex-cycle.log"
58
+ FOLLOWUP_ID = "NF-CORTEX-QUALITY-DROP"
59
+
60
+ ACCEPT_RATE_FLOOR = 50.0
61
+ ACCEPT_RATE_MIN_SAMPLE = 10
62
+ LINKED_SUCCESS_FLOOR = 50.0
63
+ LINKED_MIN_SAMPLE = 5
64
+ OVERRIDE_GAP_THRESHOLD = 20.0 # percentage points
65
+
66
+
67
+ def _log(msg: str) -> None:
68
+ """Append a timestamped line to LOG_FILE. Best-effort, never raises."""
69
+ try:
70
+ LOGS_DIR.mkdir(parents=True, exist_ok=True)
71
+ ts = datetime.now().isoformat(timespec="seconds")
72
+ with LOG_FILE.open("a", encoding="utf-8") as fh:
73
+ fh.write(f"[{ts}] {msg}\n")
74
+ except Exception:
75
+ pass
76
+ print(msg)
77
+
78
+
79
+ def detect_quality_signals(summary: dict) -> list[dict]:
80
+ """Inspect a cortex_evaluation_summary dict and return degradation signals.
81
+
82
+ Each signal is a dict with at least:
83
+ - kind: short identifier (accept_rate / linked_success / override_gap)
84
+ - severity: warn | error
85
+ - message: human-readable explanation
86
+ - metric_value: the failing measurement
87
+ - threshold: the floor it dropped below
88
+
89
+ Returns an empty list when nothing is degraded. Pure function so it can
90
+ be unit-tested without touching the DB.
91
+ """
92
+ signals: list[dict] = []
93
+ if not isinstance(summary, dict):
94
+ return signals
95
+
96
+ total = int(summary.get("total_evaluations") or 0)
97
+ accept_rate = float(summary.get("recommendation_accept_rate") or 0.0)
98
+ linked_total = int(summary.get("linked_outcomes_total") or 0)
99
+ linked_met = int(summary.get("linked_outcomes_met") or 0)
100
+ linked_missed = int(summary.get("linked_outcomes_missed") or 0)
101
+ linked_pending = int(summary.get("linked_outcomes_pending") or 0)
102
+ linked_resolved = linked_met + linked_missed
103
+ if linked_resolved <= 0 and linked_total > 0:
104
+ # Older callers may omit the met/missed counters; fall back to total minus pending.
105
+ linked_resolved = max(0, linked_total - linked_pending)
106
+ linked_success = float(summary.get("linked_outcome_success_rate") or 0.0)
107
+ recommended_success = float(summary.get("recommended_success_rate") or 0.0)
108
+ override_success = float(summary.get("override_success_rate") or 0.0)
109
+
110
+ if total >= ACCEPT_RATE_MIN_SAMPLE and accept_rate < ACCEPT_RATE_FLOOR:
111
+ signals.append({
112
+ "kind": "accept_rate",
113
+ "severity": "warn",
114
+ "metric_value": accept_rate,
115
+ "threshold": ACCEPT_RATE_FLOOR,
116
+ "sample_size": total,
117
+ "message": (
118
+ f"Cortex recommendation accept rate {accept_rate:.1f}% on {total} "
119
+ f"evaluations is below the {ACCEPT_RATE_FLOOR:.0f}% floor. Users "
120
+ "are overriding the recommended choice more often than not."
121
+ ),
122
+ })
123
+
124
+ linked_scope = f"{linked_resolved} resolved linked outcomes"
125
+ if linked_pending > 0:
126
+ linked_scope += f" ({linked_total} total, {linked_pending} pending)"
127
+
128
+ if linked_resolved >= LINKED_MIN_SAMPLE and linked_success < LINKED_SUCCESS_FLOOR:
129
+ signals.append({
130
+ "kind": "linked_success",
131
+ "severity": "warn",
132
+ "metric_value": linked_success,
133
+ "threshold": LINKED_SUCCESS_FLOOR,
134
+ "sample_size": linked_resolved,
135
+ "message": (
136
+ f"Cortex linked-outcome success rate {linked_success:.1f}% on "
137
+ f"{linked_scope} is below the "
138
+ f"{LINKED_SUCCESS_FLOOR:.0f}% floor."
139
+ ),
140
+ })
141
+
142
+ if linked_resolved >= LINKED_MIN_SAMPLE:
143
+ gap = override_success - recommended_success
144
+ if gap >= OVERRIDE_GAP_THRESHOLD:
145
+ signals.append({
146
+ "kind": "override_gap",
147
+ "severity": "error",
148
+ "metric_value": gap,
149
+ "threshold": OVERRIDE_GAP_THRESHOLD,
150
+ "sample_size": linked_resolved,
151
+ "message": (
152
+ f"Cortex overrides outperform recommendations by {gap:.1f}pp "
153
+ f"(override {override_success:.1f}% vs recommended "
154
+ f"{recommended_success:.1f}% on {linked_scope}). The "
155
+ "recommender is mis-ranking choices."
156
+ ),
157
+ })
158
+
159
+ return signals
160
+
161
+
162
+ def _persist_quality_snapshot(window_7d: dict, window_1d: dict, signals: list[dict]) -> None:
163
+ payload = {
164
+ "captured_at": datetime.now().isoformat(timespec="seconds"),
165
+ "window_7d": window_7d,
166
+ "window_1d": window_1d,
167
+ "signals": signals,
168
+ "schema": 1,
169
+ }
170
+ try:
171
+ OPERATIONS_DIR.mkdir(parents=True, exist_ok=True)
172
+ QUALITY_FILE.write_text(json.dumps(payload, ensure_ascii=False, indent=2))
173
+ except Exception as e:
174
+ _log(f"WARN: failed to persist quality snapshot: {e}")
175
+
176
+
177
+ def _upsert_quality_followup(signals: list[dict]) -> str:
178
+ """Open or refresh NF-CORTEX-QUALITY-DROP. Returns the action taken.
179
+
180
+ Idempotent: if the followup already exists in PENDING status it is
181
+ updated in place rather than duplicated. If it exists but was already
182
+ resolved, a fresh row is inserted with the same id (REPLACE) so the
183
+ new degradation pattern is visible.
184
+ """
185
+ try:
186
+ from db import complete_followup, get_followup, get_db
187
+ except Exception as e:
188
+ _log(f"WARN: cannot import db helpers: {e}")
189
+ return "skipped_no_db"
190
+
191
+ try:
192
+ existing = get_followup(FOLLOWUP_ID)
193
+ except Exception as e:
194
+ _log(f"WARN: get_followup raised: {e}")
195
+ existing = None
196
+
197
+ if not signals:
198
+ if not existing:
199
+ return "no_signal"
200
+ status = str(existing.get("status") or "").upper()
201
+ if status.startswith("COMPLETED") or status in {"DELETED", "ARCHIVED", "BLOCKED", "WAITING", "CANCELLED"}:
202
+ return "no_signal"
203
+ try:
204
+ complete_followup(
205
+ FOLLOWUP_ID,
206
+ result=(
207
+ "Auto-resolved by cortex-cycle: no active degradation signals in the "
208
+ "current 7d window."
209
+ ),
210
+ )
211
+ except Exception as e:
212
+ _log(f"WARN: failed to close followup: {e}")
213
+ return "failed_close"
214
+ return "closed"
215
+
216
+ summary_lines = ["Cortex continuous validation found quality degradation:"]
217
+ for sig in signals:
218
+ summary_lines.append(
219
+ f"- [{sig['severity'].upper()}] {sig['kind']}: {sig['message']}"
220
+ )
221
+ summary_lines.append("")
222
+ summary_lines.append(
223
+ "Investigate cortex_evaluations recent rows, review goal profiles, "
224
+ "and consider tightening or relaxing the recommender heuristics."
225
+ )
226
+ description = "\n".join(summary_lines)
227
+ verification = (
228
+ "SELECT id, goal, recommended_choice, selected_choice, selection_source, "
229
+ "created_at FROM cortex_evaluations ORDER BY id DESC LIMIT 20"
230
+ )
231
+ now_epoch = datetime.now().timestamp()
232
+
233
+ try:
234
+ conn = get_db()
235
+ conn.execute(
236
+ "INSERT OR REPLACE INTO followups (id, description, date, status, "
237
+ "verification, created_at, updated_at, priority) "
238
+ "VALUES (?, ?, NULL, 'PENDING', ?, ?, ?, 'high')",
239
+ (FOLLOWUP_ID, description, verification, now_epoch, now_epoch),
240
+ )
241
+ try:
242
+ conn.commit()
243
+ except Exception:
244
+ pass
245
+ except Exception as e:
246
+ _log(f"WARN: failed to upsert followup: {e}")
247
+ return "failed"
248
+
249
+ return "refreshed" if existing else "opened"
250
+
251
+
252
+ def run() -> int:
253
+ """Main cron entry point. Returns 0 on success, 2 on partial failure."""
254
+ try:
255
+ from db import cortex_evaluation_summary
256
+ except Exception as e:
257
+ _log(f"FATAL: cannot import cortex_evaluation_summary: {e}")
258
+ return 2
259
+
260
+ try:
261
+ window_7d = cortex_evaluation_summary(days=7)
262
+ except Exception as e:
263
+ _log(f"FATAL: cortex_evaluation_summary(7d) raised: {e}")
264
+ return 2
265
+
266
+ try:
267
+ window_1d = cortex_evaluation_summary(days=1)
268
+ except Exception as e:
269
+ _log(f"WARN: cortex_evaluation_summary(1d) raised: {e}")
270
+ window_1d = {"days": 1, "total_evaluations": 0, "error": str(e)}
271
+
272
+ signals = detect_quality_signals(window_7d)
273
+ _persist_quality_snapshot(window_7d, window_1d, signals)
274
+
275
+ total = int(window_7d.get("total_evaluations") or 0)
276
+ accept_rate = float(window_7d.get("recommendation_accept_rate") or 0.0)
277
+ if total == 0:
278
+ _log("Cortex cycle: no evaluations in 7d window — nothing to validate")
279
+ else:
280
+ _log(
281
+ f"Cortex cycle: {total} evaluations in 7d, accept_rate={accept_rate:.1f}%, "
282
+ f"signals={len(signals)}"
283
+ )
284
+
285
+ action = _upsert_quality_followup(signals)
286
+ if signals or action not in {"no_signal"}:
287
+ _log(f"Cortex cycle: followup {FOLLOWUP_ID} {action} ({len(signals)} signal(s))")
288
+
289
+ return 0
290
+
291
+
292
+ if __name__ == "__main__":
293
+ sys.exit(run())
@@ -0,0 +1,53 @@
1
+ #!/bin/bash
2
+ # NEXO Cron Wrapper — Records execution in cron_runs table.
3
+ # Usage: nexo-cron-wrapper.sh <cron_id> <command...>
4
+ # Example: nexo-cron-wrapper.sh deep-sleep bash nexo-deep-sleep.sh
5
+ #
6
+ # Wraps any cron command to automatically record start/end/exit_code/summary.
7
+ # Used by sync.py when generating LaunchAgents from manifest.json.
8
+
9
+ set -uo pipefail
10
+
11
+ CRON_ID="${1:?Usage: nexo-cron-wrapper.sh <cron_id> <command...>}"
12
+ shift
13
+
14
+ NEXO_HOME="${NEXO_HOME:-$HOME/.nexo}"
15
+ DB="$NEXO_HOME/data/nexo.db"
16
+
17
+ # Record start
18
+ RUN_ID=$(sqlite3 "$DB" "INSERT INTO cron_runs (cron_id) VALUES ('$CRON_ID'); SELECT last_insert_rowid();" 2>/dev/null)
19
+
20
+ if [ -z "$RUN_ID" ]; then
21
+ # DB not ready — run without tracking
22
+ exec "$@"
23
+ fi
24
+
25
+ # Run the actual command, capture output
26
+ OUTPUT_FILE=$(mktemp)
27
+ "$@" > "$OUTPUT_FILE" 2>&1
28
+ EXIT_CODE=$?
29
+
30
+ # Extract summary (last meaningful line, max 500 chars)
31
+ SUMMARY=$(tail -5 "$OUTPUT_FILE" | grep -v "^$" | tail -1 | head -c 500 | sed "s/'/''/g")
32
+
33
+ # Extract error if failed
34
+ ERROR=""
35
+ if [ $EXIT_CODE -ne 0 ]; then
36
+ ERROR=$(grep -i "error\|exception\|fail\|traceback" "$OUTPUT_FILE" | tail -1 | head -c 500 | sed "s/'/''/g")
37
+ fi
38
+
39
+ # Record end
40
+ sqlite3 "$DB" "
41
+ UPDATE cron_runs SET
42
+ ended_at = datetime('now'),
43
+ exit_code = $EXIT_CODE,
44
+ summary = '$SUMMARY',
45
+ error = '$ERROR',
46
+ duration_secs = ROUND((julianday(datetime('now')) - julianday(started_at)) * 86400, 1)
47
+ WHERE id = $RUN_ID;
48
+ " 2>/dev/null
49
+
50
+ # Clean output
51
+ rm -f "$OUTPUT_FILE"
52
+
53
+ exit $EXIT_CODE