nexo-brain 5.3.13 → 5.3.15

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 (230) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/bin/nexo-brain.js +52 -1
  3. package/package.json +1 -1
  4. package/src/crons/sync.py +18 -4
  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/_entities.py +1 -1
  43. package/src/db/_episodic 2.py +762 -0
  44. package/src/db/_evolution 2.py +54 -0
  45. package/src/db/_fts 2.py +406 -0
  46. package/src/db/_goal_profiles 2.py +376 -0
  47. package/src/db/_hot_context 2.py +660 -0
  48. package/src/db/_outcomes 2.py +800 -0
  49. package/src/db/_personal_scripts 2.py +582 -0
  50. package/src/db/_sessions 2.py +330 -0
  51. package/src/db/_tasks 2.py +91 -0
  52. package/src/db/_watchers 2.py +173 -0
  53. package/src/doctor/formatters 2.py +52 -0
  54. package/src/doctor/models 2.py +69 -0
  55. package/src/doctor/planes 2.py +87 -0
  56. package/src/doctor/providers/__init__ 2.py +1 -0
  57. package/src/doctor/providers/deep 2.py +367 -0
  58. package/src/evolution_cycle 2.py +519 -0
  59. package/src/hooks/auto_capture 2.py +208 -0
  60. package/src/hooks/caffeinate-guard 2.sh +8 -0
  61. package/src/hooks/capture-session 2.sh +21 -0
  62. package/src/hooks/capture-tool-logs 2.sh +158 -0
  63. package/src/hooks/daily-briefing-check 2.sh +33 -0
  64. package/src/hooks/heartbeat-enforcement 2.py +90 -0
  65. package/src/hooks/heartbeat-posttool 2.sh +18 -0
  66. package/src/hooks/inbox-hook 2.sh +76 -0
  67. package/src/hooks/post-compact 2.sh +152 -0
  68. package/src/hooks/pre-compact 2.sh +169 -0
  69. package/src/hooks/protocol-guardrail 2.sh +10 -0
  70. package/src/hooks/protocol-pretool-guardrail 2.sh +9 -0
  71. package/src/hooks/session-stop 2.sh +52 -0
  72. package/src/kg_populate 2.py +292 -0
  73. package/src/maintenance 2.py +53 -0
  74. package/src/memory_backends 2.py +71 -0
  75. package/src/migrate_embeddings 2.py +124 -0
  76. package/src/nexo_sdk 2.py +103 -0
  77. package/src/observability 2.py +199 -0
  78. package/src/plugin_loader 2.py +217 -0
  79. package/src/plugins/__init__ 2.py +0 -0
  80. package/src/plugins/agents.py +10 -3
  81. package/src/plugins/artifact_registry 2.py +450 -0
  82. package/src/plugins/backup 2.py +127 -0
  83. package/src/plugins/claims_tools 2.py +119 -0
  84. package/src/plugins/cognitive_memory 2.py +609 -0
  85. package/src/plugins/core_rules 2.py +252 -0
  86. package/src/plugins/cortex 2.py +1155 -0
  87. package/src/plugins/entities 2.py +67 -0
  88. package/src/plugins/episodic_memory 2.py +560 -0
  89. package/src/plugins/evolution 2.py +167 -0
  90. package/src/plugins/goal_engine 2.py +142 -0
  91. package/src/plugins/guard 2.py +862 -0
  92. package/src/plugins/impact 2.py +29 -0
  93. package/src/plugins/knowledge_graph_tools 2.py +137 -0
  94. package/src/plugins/media_memory_tools 2.py +98 -0
  95. package/src/plugins/memory_export 2.py +196 -0
  96. package/src/plugins/outcomes 2.py +130 -0
  97. package/src/plugins/personal_scripts 2.py +117 -0
  98. package/src/plugins/preferences 2.py +47 -0
  99. package/src/plugins/protocol 2.py +1449 -0
  100. package/src/plugins/schedule.py +2 -1
  101. package/src/plugins/simple_api 2.py +106 -0
  102. package/src/plugins/skills 2.py +341 -0
  103. package/src/plugins/state_watchers 2.py +79 -0
  104. package/src/plugins/update 2.py +986 -0
  105. package/src/plugins/user_state_tools 2.py +43 -0
  106. package/src/plugins/workflow 2.py +588 -0
  107. package/src/protocol_settings 2.py +59 -0
  108. package/src/public_contribution 2.py +466 -0
  109. package/src/public_evolution_queue 2.py +241 -0
  110. package/src/requirements 2.txt +14 -0
  111. package/src/requirements.txt +1 -1
  112. package/src/retroactive_learnings 2.py +373 -0
  113. package/src/rules/__init__ 2.py +0 -0
  114. package/src/rules/core-rules 2.json +331 -0
  115. package/src/rules/migrate 2.py +207 -0
  116. package/src/runtime_power 2.py +874 -0
  117. package/src/runtime_power.py +18 -1
  118. package/src/script_registry 2.py +1559 -0
  119. package/src/scripts/check-context 2.py +272 -0
  120. package/src/scripts/deep-sleep/apply_findings 2.py +2327 -0
  121. package/src/scripts/deep-sleep/collect 2.py +928 -0
  122. package/src/scripts/deep-sleep/extract 2.py +330 -0
  123. package/src/scripts/deep-sleep/extract-prompt 2.md +285 -0
  124. package/src/scripts/deep-sleep/synthesize 2.py +312 -0
  125. package/src/scripts/deep-sleep/synthesize-prompt 2.md +336 -0
  126. package/src/scripts/nexo-agent-run 2.py +75 -0
  127. package/src/scripts/nexo-auto-update 2.py +6 -0
  128. package/src/scripts/nexo-backup 2.sh +25 -0
  129. package/src/scripts/nexo-brain-activation 2.sh +140 -0
  130. package/src/scripts/nexo-catchup 2.py +300 -0
  131. package/src/scripts/nexo-cognitive-decay 2.py +257 -0
  132. package/src/scripts/nexo-cortex-cycle 2.py +293 -0
  133. package/src/scripts/nexo-cron-wrapper 2.sh +53 -0
  134. package/src/scripts/nexo-cron-wrapper.sh +7 -0
  135. package/src/scripts/nexo-daily-self-audit 2.py +2161 -0
  136. package/src/scripts/nexo-dashboard 2.sh +29 -0
  137. package/src/scripts/nexo-deep-sleep 2.sh +86 -0
  138. package/src/scripts/nexo-evolution-run 2.py +1664 -0
  139. package/src/scripts/nexo-followup-hygiene 2.py +139 -0
  140. package/src/scripts/nexo-hook-record 2.py +42 -0
  141. package/src/scripts/nexo-immune 2.py +936 -0
  142. package/src/scripts/nexo-impact-scorer 2.py +117 -0
  143. package/src/scripts/nexo-inbox-hook 2.sh +74 -0
  144. package/src/scripts/nexo-install 2.py +6 -0
  145. package/src/scripts/nexo-learning-housekeep 2.py +401 -0
  146. package/src/scripts/nexo-learning-validator 2.py +266 -0
  147. package/src/scripts/nexo-migrate 2.py +260 -0
  148. package/src/scripts/nexo-outcome-checker 2.py +127 -0
  149. package/src/scripts/nexo-postmortem-consolidator 2.py +456 -0
  150. package/src/scripts/nexo-pre-commit 2.py +120 -0
  151. package/src/scripts/nexo-prevent-sleep 2.sh +35 -0
  152. package/src/scripts/nexo-proactive-dashboard 2.py +354 -0
  153. package/src/scripts/nexo-reflection 2.py +256 -0
  154. package/src/scripts/nexo-runtime-preflight 2.py +274 -0
  155. package/src/scripts/nexo-sleep 2.py +631 -0
  156. package/src/scripts/nexo-snapshot-restore 2.sh +35 -0
  157. package/src/scripts/nexo-sync-clients 2.py +16 -0
  158. package/src/scripts/nexo-synthesis 2.py +475 -0
  159. package/src/scripts/nexo-tcc-approve 2.sh +79 -0
  160. package/src/scripts/nexo-update 2.sh +306 -0
  161. package/src/scripts/nexo-watchdog 2.sh +1207 -0
  162. package/src/scripts/nexo-watchdog-smoke 2.py +119 -0
  163. package/src/scripts/rehydrate_learnings_from_archive 2.py +245 -0
  164. package/src/server 2.py +1296 -0
  165. package/src/skills/run-nexo-audit-phase/guide 2.md +43 -0
  166. package/src/skills/run-nexo-audit-phase/skill 2.json +59 -0
  167. package/src/skills/run-nexo-core-fix-cycle/guide 2.md +17 -0
  168. package/src/skills/run-nexo-core-fix-cycle/script 2.py +276 -0
  169. package/src/skills/run-nexo-core-fix-cycle/skill 2.json +58 -0
  170. package/src/skills/run-release-final-audit/guide 2.md +16 -0
  171. package/src/skills/run-release-final-audit/script 2.py +259 -0
  172. package/src/skills/run-release-final-audit/skill 2.json +77 -0
  173. package/src/skills/run-runtime-doctor/guide 2.md +12 -0
  174. package/src/skills/run-runtime-doctor/script 2.py +21 -0
  175. package/src/skills/run-runtime-doctor/skill 2.json +25 -0
  176. package/src/skills_runtime 2.py +932 -0
  177. package/src/state_watchers_runtime 2.py +475 -0
  178. package/src/storage_router 2.py +32 -0
  179. package/src/system_catalog 2.py +786 -0
  180. package/src/tools_coordination 2.py +103 -0
  181. package/src/tools_credentials 2.py +68 -0
  182. package/src/tools_drive 2.py +487 -0
  183. package/src/tools_hot_context 2.py +163 -0
  184. package/src/tools_learnings 2.py +612 -0
  185. package/src/tools_menu 2.py +229 -0
  186. package/src/tools_reminders 2.py +88 -0
  187. package/src/tools_reminders_crud 2.py +363 -0
  188. package/src/tools_sessions 2.py +1054 -0
  189. package/src/tools_system_catalog 2.py +19 -0
  190. package/src/tools_task_history 2.py +57 -0
  191. package/src/tools_transcripts 2.py +98 -0
  192. package/src/transcript_utils 2.py +412 -0
  193. package/src/user_context 2.py +46 -0
  194. package/src/user_data_portability 2.py +328 -0
  195. package/src/user_state_model 2.py +170 -0
  196. package/templates/CLAUDE.md 2.template +108 -0
  197. package/templates/CODEX.AGENTS.md 2.template +66 -0
  198. package/templates/launchagents/README 2.md +132 -0
  199. package/templates/launchagents/com.nexo.auto-close-sessions 2.plist +39 -0
  200. package/templates/launchagents/com.nexo.auto-close-sessions.plist +1 -1
  201. package/templates/launchagents/com.nexo.catchup 2.plist +39 -0
  202. package/templates/launchagents/com.nexo.catchup.plist +1 -1
  203. package/templates/launchagents/com.nexo.cognitive-decay 2.plist +40 -0
  204. package/templates/launchagents/com.nexo.dashboard 2.plist +43 -0
  205. package/templates/launchagents/com.nexo.dashboard.plist +1 -1
  206. package/templates/launchagents/com.nexo.deep-sleep 2.plist +43 -0
  207. package/templates/launchagents/com.nexo.deep-sleep.plist +1 -1
  208. package/templates/launchagents/com.nexo.evolution 2.plist +44 -0
  209. package/templates/launchagents/com.nexo.evolution.plist +1 -1
  210. package/templates/launchagents/com.nexo.followup-hygiene 2.plist +45 -0
  211. package/templates/launchagents/com.nexo.followup-hygiene.plist +1 -1
  212. package/templates/launchagents/com.nexo.immune 2.plist +41 -0
  213. package/templates/launchagents/com.nexo.immune.plist +1 -1
  214. package/templates/launchagents/com.nexo.postmortem 2.plist +45 -0
  215. package/templates/launchagents/com.nexo.postmortem.plist +1 -1
  216. package/templates/launchagents/com.nexo.self-audit 2.plist +47 -0
  217. package/templates/launchagents/com.nexo.self-audit.plist +1 -1
  218. package/templates/launchagents/com.nexo.synthesis 2.plist +45 -0
  219. package/templates/launchagents/com.nexo.synthesis.plist +1 -1
  220. package/templates/launchagents/com.nexo.watchdog 2.plist +37 -0
  221. package/templates/launchagents/com.nexo.watchdog.plist +1 -1
  222. package/templates/nexo_helper 2.py +301 -0
  223. package/templates/openclaw 2.json +13 -0
  224. package/templates/plugin-template 2.py +40 -0
  225. package/templates/script-template 2.py +59 -0
  226. package/templates/script-template 2.sh +13 -0
  227. package/templates/script-template.py +5 -4
  228. package/templates/skill-script-template 2.py +48 -0
  229. package/templates/skill-script-template.py +2 -1
  230. package/templates/skill-template 2.md +33 -0
@@ -0,0 +1,450 @@
1
+ """Artifact Registry plugin — structured index of things NEXO creates/deploys.
2
+
3
+ Solves 'recent work amnesia': NEXO builds services, dashboards, scripts, APIs
4
+ but can't find them hours later because semantic search ('backend') doesn't
5
+ match operational terms ('FastAPI localhost:6174').
6
+
7
+ Architecture (from 3-way AI debate — GPT-5.4 + Gemini 3.1 Pro + Claude Opus 4.6):
8
+ 1. Structured SQLite table with aliases, ports, paths, run commands
9
+ 2. Retrieval ladder: exact alias → port/path match → fuzzy token → semantic fallback
10
+ 3. User-language alias learning: when the user says 'backend' and it resolves
11
+ to dashboard:6174, store that mapping for O(1) next time
12
+ 4. Temporal filtering: 'last night' → hard SQL constraint before any search
13
+ """
14
+
15
+ import json
16
+ import datetime
17
+ from db import get_db
18
+
19
+
20
+ # Valid artifact kinds
21
+ VALID_KINDS = {
22
+ 'service', 'dashboard', 'script', 'api', 'cron', 'website',
23
+ 'database', 'repo', 'config', 'tool', 'plugin', 'other',
24
+ }
25
+
26
+ VALID_STATES = {'active', 'inactive', 'broken', 'archived'}
27
+
28
+
29
+ def _cognitive_ingest_safe(content, source_type, source_id="", source_title="", domain=""):
30
+ """Ingest to cognitive STM. Silently fails if cognitive engine unavailable."""
31
+ try:
32
+ import cognitive
33
+ cognitive.ingest(content, source_type, source_id, source_title, domain)
34
+ except Exception:
35
+ pass
36
+
37
+
38
+ def handle_artifact_create(
39
+ kind: str,
40
+ canonical_name: str,
41
+ aliases: str = '[]',
42
+ description: str = '',
43
+ uri: str = '',
44
+ ports: str = '[]',
45
+ paths: str = '[]',
46
+ run_cmd: str = '',
47
+ repo: str = '',
48
+ domain: str = '',
49
+ session_id: str = '',
50
+ metadata: str = '{}',
51
+ ) -> str:
52
+ """Register a new artifact (service, dashboard, script, API, etc.).
53
+
54
+ Call this whenever NEXO creates, deploys, or discovers a runnable/accessible artifact.
55
+
56
+ Args:
57
+ kind: Type — service, dashboard, script, api, cron, website, database, repo, config, tool, plugin, other
58
+ canonical_name: Primary name (e.g., 'NEXO Brain Dashboard')
59
+ aliases: JSON array of alternative names users might use (e.g., '["backend", "dashboard", "nexo web"]')
60
+ description: What it does (1-2 sentences)
61
+ uri: Access URL or address (e.g., 'localhost:6174', 'nexo-brain.com')
62
+ ports: JSON array of ports (e.g., '[6174]')
63
+ paths: JSON array of file paths (e.g., '["/Users/x/nexo/src/dashboard/app.py"]')
64
+ run_cmd: Command to start/open it (e.g., 'python3 -m dashboard.app --port 6174')
65
+ repo: Repository path or URL
66
+ domain: Project domain (nexo, my-project, project-a, project-b, etc.)
67
+ session_id: Current session ID
68
+ metadata: JSON object with extra key-value pairs
69
+ """
70
+ if kind not in VALID_KINDS:
71
+ return f"ERROR: kind must be one of: {', '.join(sorted(VALID_KINDS))}"
72
+
73
+ # Parse aliases
74
+ try:
75
+ alias_list = json.loads(aliases) if aliases and aliases != '[]' else []
76
+ except (json.JSONDecodeError, TypeError):
77
+ alias_list = [a.strip() for a in aliases.split(',') if a.strip()]
78
+
79
+ conn = get_db()
80
+ cur = conn.execute(
81
+ """INSERT INTO artifact_registry
82
+ (kind, canonical_name, aliases, description, uri, ports, paths,
83
+ run_cmd, repo, domain, state, session_id, metadata)
84
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?)""",
85
+ (kind, canonical_name, json.dumps(alias_list), description, uri, ports,
86
+ paths, run_cmd, repo, domain, session_id, metadata),
87
+ )
88
+ artifact_id = cur.lastrowid
89
+ conn.commit()
90
+
91
+ # Insert aliases into lookup table
92
+ for alias in alias_list + [canonical_name.lower()]:
93
+ alias_clean = alias.strip().lower()
94
+ if alias_clean:
95
+ try:
96
+ conn.execute(
97
+ "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'create')",
98
+ (artifact_id, alias_clean),
99
+ )
100
+ except Exception:
101
+ pass
102
+ conn.commit()
103
+
104
+ # Ingest to cognitive memory
105
+ content = f"Artifact: {canonical_name} ({kind}). {description}. URI: {uri}. Aliases: {', '.join(alias_list)}"
106
+ _cognitive_ingest_safe(content, "artifact", f"A{artifact_id}", canonical_name[:80], domain)
107
+
108
+ return f"Artifact #{artifact_id} created: {canonical_name} ({kind}) — {uri or 'no URI'}"
109
+
110
+
111
+ def handle_artifact_find(query: str, kind: str = '', state: str = 'active') -> str:
112
+ """Find artifacts using the retrieval ladder: exact alias → port/path → fuzzy token → all recent.
113
+
114
+ This is the PRIMARY retrieval tool. Use it when the user references something
115
+ they or NEXO built/deployed/created. Designed for natural language like
116
+ 'the backend', 'that script from yesterday', 'localhost something'.
117
+
118
+ Args:
119
+ query: What to search for — name, alias, port, path, or description fragment
120
+ kind: Filter by kind (optional)
121
+ state: Filter by state — default 'active'. Use 'all' for everything.
122
+ """
123
+ conn = get_db()
124
+ results = []
125
+ query_lower = query.strip().lower()
126
+
127
+ state_filter = "AND state = ?" if state != 'all' else ""
128
+ state_params = (state,) if state != 'all' else ()
129
+
130
+ kind_filter = "AND kind = ?" if kind else ""
131
+ kind_params = (kind,) if kind else ()
132
+
133
+ extra_filters = state_filter + " " + kind_filter
134
+ extra_params = state_params + kind_params
135
+
136
+ # --- STAGE 1: Exact alias match (fastest, O(1)) ---
137
+ rows = conn.execute(
138
+ f"""SELECT DISTINCT r.* FROM artifact_registry r
139
+ JOIN artifact_aliases a ON a.artifact_id = r.id
140
+ WHERE a.phrase = ? {extra_filters}
141
+ ORDER BY r.last_touched_at DESC LIMIT 5""",
142
+ (query_lower,) + extra_params,
143
+ ).fetchall()
144
+ if rows:
145
+ results = [dict(r) for r in rows]
146
+ return _format_results(results, "alias match", query)
147
+
148
+ # --- STAGE 2: Port or URI match ---
149
+ rows = conn.execute(
150
+ f"""SELECT * FROM artifact_registry
151
+ WHERE (uri LIKE ? OR ports LIKE ?) {extra_filters}
152
+ ORDER BY last_touched_at DESC LIMIT 5""",
153
+ (f"%{query_lower}%", f"%{query_lower}%") + extra_params,
154
+ ).fetchall()
155
+ if rows:
156
+ results = [dict(r) for r in rows]
157
+ return _format_results(results, "URI/port match", query)
158
+
159
+ # --- STAGE 3: Path match ---
160
+ rows = conn.execute(
161
+ f"""SELECT * FROM artifact_registry
162
+ WHERE paths LIKE ? {extra_filters}
163
+ ORDER BY last_touched_at DESC LIMIT 5""",
164
+ (f"%{query_lower}%",) + extra_params,
165
+ ).fetchall()
166
+ if rows:
167
+ results = [dict(r) for r in rows]
168
+ return _format_results(results, "path match", query)
169
+
170
+ # --- STAGE 4: Fuzzy token match on name, description, aliases ---
171
+ tokens = query_lower.split()
172
+ if tokens:
173
+ conditions = " AND ".join(
174
+ "(LOWER(canonical_name) LIKE ? OR LOWER(description) LIKE ? OR LOWER(aliases) LIKE ?)"
175
+ for _ in tokens
176
+ )
177
+ params = []
178
+ for t in tokens:
179
+ p = f"%{t}%"
180
+ params.extend([p, p, p])
181
+ rows = conn.execute(
182
+ f"""SELECT * FROM artifact_registry
183
+ WHERE {conditions} {extra_filters}
184
+ ORDER BY last_touched_at DESC LIMIT 10""",
185
+ tuple(params) + extra_params,
186
+ ).fetchall()
187
+ if rows:
188
+ results = [dict(r) for r in rows]
189
+ return _format_results(results, "token match", query)
190
+
191
+ # --- STAGE 5: Recent artifacts (last 72h) as fallback ---
192
+ cutoff = (datetime.datetime.now() - datetime.timedelta(hours=72)).isoformat()
193
+ rows = conn.execute(
194
+ f"""SELECT * FROM artifact_registry
195
+ WHERE last_touched_at >= ? {extra_filters}
196
+ ORDER BY last_touched_at DESC LIMIT 10""",
197
+ (cutoff,) + extra_params,
198
+ ).fetchall()
199
+ if rows:
200
+ results = [dict(r) for r in rows]
201
+ return _format_results(results, "recent (72h)", query)
202
+
203
+ return f"No artifacts found for '{query}'. Use artifact_list to see all registered artifacts."
204
+
205
+
206
+ def handle_artifact_update(
207
+ id: int,
208
+ canonical_name: str = '',
209
+ aliases: str = '',
210
+ description: str = '',
211
+ uri: str = '',
212
+ ports: str = '',
213
+ paths: str = '',
214
+ run_cmd: str = '',
215
+ state: str = '',
216
+ domain: str = '',
217
+ metadata: str = '',
218
+ ) -> str:
219
+ """Update an artifact. Only non-empty fields are changed.
220
+
221
+ Args:
222
+ id: Artifact ID to update
223
+ canonical_name: New primary name
224
+ aliases: New JSON array of aliases (replaces existing)
225
+ description: New description
226
+ uri: New URI
227
+ ports: New ports JSON array
228
+ paths: New paths JSON array
229
+ run_cmd: New run command
230
+ state: New state (active, inactive, broken, archived)
231
+ domain: New domain
232
+ metadata: New metadata JSON (merged with existing)
233
+ """
234
+ conn = get_db()
235
+ row = conn.execute("SELECT * FROM artifact_registry WHERE id = ?", (id,)).fetchone()
236
+ if not row:
237
+ return f"ERROR: Artifact #{id} not found."
238
+
239
+ updates = []
240
+ params = []
241
+
242
+ if canonical_name:
243
+ updates.append("canonical_name = ?"); params.append(canonical_name)
244
+ if description:
245
+ updates.append("description = ?"); params.append(description)
246
+ if uri:
247
+ updates.append("uri = ?"); params.append(uri)
248
+ if ports:
249
+ updates.append("ports = ?"); params.append(ports)
250
+ if paths:
251
+ updates.append("paths = ?"); params.append(paths)
252
+ if run_cmd:
253
+ updates.append("run_cmd = ?"); params.append(run_cmd)
254
+ if domain:
255
+ updates.append("domain = ?"); params.append(domain)
256
+ if state:
257
+ if state not in VALID_STATES:
258
+ return f"ERROR: state must be one of: {', '.join(sorted(VALID_STATES))}"
259
+ updates.append("state = ?"); params.append(state)
260
+ if metadata:
261
+ try:
262
+ existing = json.loads(row["metadata"] or '{}')
263
+ new = json.loads(metadata)
264
+ existing.update(new)
265
+ updates.append("metadata = ?"); params.append(json.dumps(existing))
266
+ except (json.JSONDecodeError, TypeError):
267
+ pass
268
+
269
+ if aliases:
270
+ try:
271
+ alias_list = json.loads(aliases) if aliases.startswith('[') else [a.strip() for a in aliases.split(',')]
272
+ except (json.JSONDecodeError, TypeError):
273
+ alias_list = [a.strip() for a in aliases.split(',')]
274
+ updates.append("aliases = ?"); params.append(json.dumps(alias_list))
275
+ # Rebuild alias lookup table
276
+ conn.execute("DELETE FROM artifact_aliases WHERE artifact_id = ?", (id,))
277
+ for alias in alias_list:
278
+ alias_clean = alias.strip().lower()
279
+ if alias_clean:
280
+ conn.execute(
281
+ "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'update')",
282
+ (id, alias_clean),
283
+ )
284
+
285
+ if not updates:
286
+ return "Nothing to update."
287
+
288
+ updates.append("last_touched_at = datetime('now')")
289
+ params.append(id)
290
+ conn.execute(f"UPDATE artifact_registry SET {', '.join(updates)} WHERE id = ?", tuple(params))
291
+ conn.commit()
292
+ return f"Artifact #{id} updated."
293
+
294
+
295
+ def handle_artifact_learn_alias(id: int, phrase: str) -> str:
296
+ """Learn a new alias from user language. Call this when the user refers to an
297
+ artifact with a term not yet registered (e.g., the user says 'backend' for dashboard:6174).
298
+
299
+ Args:
300
+ id: Artifact ID
301
+ phrase: The user's term (e.g., 'backend', 'that api thing')
302
+ """
303
+ conn = get_db()
304
+ row = conn.execute("SELECT * FROM artifact_registry WHERE id = ?", (id,)).fetchone()
305
+ if not row:
306
+ return f"ERROR: Artifact #{id} not found."
307
+
308
+ phrase_clean = phrase.strip().lower()
309
+ if not phrase_clean:
310
+ return "ERROR: Empty phrase."
311
+
312
+ # Add to alias lookup table
313
+ conn.execute(
314
+ "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'user_language')",
315
+ (id, phrase_clean),
316
+ )
317
+
318
+ # Also add to the artifact's aliases JSON array
319
+ try:
320
+ existing = json.loads(row["aliases"] or '[]')
321
+ except (json.JSONDecodeError, TypeError):
322
+ existing = []
323
+ if phrase_clean not in [a.lower() for a in existing]:
324
+ existing.append(phrase_clean)
325
+ conn.execute(
326
+ "UPDATE artifact_registry SET aliases = ?, last_touched_at = datetime('now') WHERE id = ?",
327
+ (json.dumps(existing), id),
328
+ )
329
+
330
+ conn.commit()
331
+ return f"Alias '{phrase_clean}' learned for artifact #{id} ({row['canonical_name']})."
332
+
333
+
334
+ def handle_artifact_list(kind: str = '', state: str = 'active', recent_hours: int = 0) -> str:
335
+ """List all artifacts, optionally filtered.
336
+
337
+ Args:
338
+ kind: Filter by kind (service, dashboard, script, etc.)
339
+ state: Filter by state — 'active' (default), 'all', 'inactive', 'broken', 'archived'
340
+ recent_hours: If >0, only show artifacts touched in the last N hours
341
+ """
342
+ conn = get_db()
343
+ conditions = []
344
+ params = []
345
+
346
+ if state != 'all':
347
+ conditions.append("state = ?"); params.append(state)
348
+ if kind:
349
+ conditions.append("kind = ?"); params.append(kind)
350
+ if recent_hours > 0:
351
+ cutoff = (datetime.datetime.now() - datetime.timedelta(hours=recent_hours)).isoformat()
352
+ conditions.append("last_touched_at >= ?"); params.append(cutoff)
353
+
354
+ where = "WHERE " + " AND ".join(conditions) if conditions else ""
355
+ rows = conn.execute(
356
+ f"SELECT * FROM artifact_registry {where} ORDER BY last_touched_at DESC",
357
+ tuple(params),
358
+ ).fetchall()
359
+
360
+ if not rows:
361
+ filters = []
362
+ if kind: filters.append(f"kind={kind}")
363
+ if state != 'all': filters.append(f"state={state}")
364
+ if recent_hours: filters.append(f"last {recent_hours}h")
365
+ return f"No artifacts found{' (' + ', '.join(filters) + ')' if filters else ''}."
366
+
367
+ lines = [f"ARTIFACT REGISTRY ({len(rows)}):"]
368
+ for r in rows:
369
+ r = dict(r)
370
+ aliases_str = ""
371
+ try:
372
+ aliases = json.loads(r.get("aliases", "[]"))
373
+ if aliases:
374
+ aliases_str = f" aka [{', '.join(aliases[:3])}]"
375
+ except (json.JSONDecodeError, TypeError):
376
+ pass
377
+ uri_str = f" → {r['uri']}" if r.get("uri") else ""
378
+ cmd_str = f" | cmd: {r['run_cmd'][:60]}" if r.get("run_cmd") else ""
379
+ touched = r.get("last_touched_at", "")[:16]
380
+ lines.append(
381
+ f" #{r['id']} [{r['kind']}] {r['canonical_name']}{aliases_str}{uri_str}{cmd_str} "
382
+ f"({r['state']}, {touched})"
383
+ )
384
+ return "\n".join(lines)
385
+
386
+
387
+ def handle_artifact_delete(id: int) -> str:
388
+ """Delete an artifact from the registry.
389
+
390
+ Args:
391
+ id: Artifact ID to delete
392
+ """
393
+ conn = get_db()
394
+ row = conn.execute("SELECT canonical_name FROM artifact_registry WHERE id = ?", (id,)).fetchone()
395
+ if not row:
396
+ return f"ERROR: Artifact #{id} not found."
397
+ name = row["canonical_name"]
398
+ conn.execute("DELETE FROM artifact_aliases WHERE artifact_id = ?", (id,))
399
+ conn.execute("DELETE FROM artifact_registry WHERE id = ?", (id,))
400
+ conn.commit()
401
+ return f"Artifact #{id} ({name}) deleted."
402
+
403
+
404
+ def _format_results(results, method, query):
405
+ """Format search results for display."""
406
+ lines = [f"ARTIFACTS FOUND ({len(results)}, via {method} for '{query}'):"]
407
+ for r in results:
408
+ aliases_str = ""
409
+ try:
410
+ aliases = json.loads(r.get("aliases", "[]"))
411
+ if aliases:
412
+ aliases_str = f" aka [{', '.join(aliases[:4])}]"
413
+ except (json.JSONDecodeError, TypeError):
414
+ pass
415
+ uri_str = f" → {r['uri']}" if r.get("uri") else ""
416
+ cmd_str = f"\n Run: {r['run_cmd']}" if r.get("run_cmd") else ""
417
+ paths_str = ""
418
+ try:
419
+ paths = json.loads(r.get("paths", "[]"))
420
+ if paths:
421
+ paths_str = f"\n Paths: {', '.join(paths[:3])}"
422
+ except (json.JSONDecodeError, TypeError):
423
+ pass
424
+ touched = r.get("last_touched_at", "")[:16]
425
+ lines.append(
426
+ f" #{r['id']} [{r['kind']}] {r['canonical_name']}{aliases_str}{uri_str} "
427
+ f"({r['state']}, {touched}){cmd_str}{paths_str}"
428
+ )
429
+ return "\n".join(lines)
430
+
431
+
432
+ # Plugin registration — TOOLS array consumed by plugin_loader.py
433
+ TOOLS = [
434
+ (handle_artifact_create, "nexo_artifact_create",
435
+ "Register a new artifact (service, dashboard, script, API, etc.) in the Artifact Registry. "
436
+ "Call this whenever NEXO creates, deploys, or discovers a runnable/accessible artifact."),
437
+ (handle_artifact_find, "nexo_artifact_find",
438
+ "Find artifacts using the retrieval ladder: exact alias → port/path → fuzzy token → recent. "
439
+ "PRIMARY retrieval tool for when users reference something built/deployed. Handles natural "
440
+ "language like 'the backend', 'that script', 'localhost something'."),
441
+ (handle_artifact_update, "nexo_artifact_update",
442
+ "Update an existing artifact. Only non-empty fields are changed."),
443
+ (handle_artifact_learn_alias, "nexo_artifact_learn_alias",
444
+ "Learn a new alias from user language. Call when the user refers to an artifact with "
445
+ "an unregistered term (e.g., 'backend' for the NEXO Brain Dashboard)."),
446
+ (handle_artifact_list, "nexo_artifact_list",
447
+ "List all registered artifacts, optionally filtered by kind, state, or recency."),
448
+ (handle_artifact_delete, "nexo_artifact_delete",
449
+ "Delete an artifact from the registry."),
450
+ ]
@@ -0,0 +1,127 @@
1
+ """Backup plugin — hourly SQLite backups with 7-day retention."""
2
+ import os
3
+ import shutil
4
+ import time
5
+ import glob
6
+ from db import get_db
7
+
8
+ NEXO_HOME = os.environ.get("NEXO_HOME", os.path.expanduser("~/.nexo"))
9
+ DB_PATH = os.path.join(NEXO_HOME, "data", "nexo.db")
10
+ BACKUP_DIR = os.path.join(NEXO_HOME, "backups")
11
+
12
+ RETENTION_DAYS = 7
13
+
14
+
15
+ def handle_backup_now() -> str:
16
+ """Create an immediate backup of the NEXO database."""
17
+ os.makedirs(BACKUP_DIR, exist_ok=True)
18
+ timestamp = time.strftime("%Y-%m-%d-%H%M")
19
+ dest = os.path.join(BACKUP_DIR, f"nexo-{timestamp}.db")
20
+
21
+ # Use SQLite backup API for consistency
22
+ import sqlite3
23
+ src_conn = sqlite3.connect(DB_PATH)
24
+ try:
25
+ dst_conn = sqlite3.connect(dest)
26
+ try:
27
+ src_conn.backup(dst_conn)
28
+ finally:
29
+ dst_conn.close()
30
+ finally:
31
+ src_conn.close()
32
+
33
+ size_kb = os.path.getsize(dest) / 1024
34
+ _cleanup_old()
35
+ return f"Backup created: {os.path.basename(dest)} ({size_kb:.0f} KB)"
36
+
37
+
38
+ def handle_backup_list() -> str:
39
+ """List available backups with dates and sizes."""
40
+ if not os.path.isdir(BACKUP_DIR):
41
+ return "No backups."
42
+ files = sorted(glob.glob(os.path.join(BACKUP_DIR, "nexo-*.db")), reverse=True)
43
+ if not files:
44
+ return "No backups."
45
+ lines = [f"BACKUPS ({len(files)}):"]
46
+ total_size = 0
47
+ for f in files:
48
+ size = os.path.getsize(f) / 1024
49
+ total_size += size
50
+ name = os.path.basename(f)
51
+ lines.append(f" {name} ({size:.0f} KB)")
52
+ lines.append(f"\n Total: {total_size/1024:.1f} MB")
53
+ return "\n".join(lines)
54
+
55
+
56
+ def handle_backup_restore(filename: str) -> str:
57
+ """Restore database from a backup file. DESTRUCTIVE — replaces current DB.
58
+
59
+ Args:
60
+ filename: Backup filename (e.g., 'nexo-2026-03-11-1200.db')
61
+ """
62
+ src = os.path.join(BACKUP_DIR, filename)
63
+ if not os.path.isfile(src):
64
+ return f"Backup not found: {filename}"
65
+
66
+ # Create safety backup first
67
+ safety = os.path.join(BACKUP_DIR, f"nexo-pre-restore-{time.strftime('%Y%m%d%H%M%S')}.db")
68
+ import sqlite3
69
+ src_conn = sqlite3.connect(DB_PATH)
70
+ try:
71
+ dst_conn = sqlite3.connect(safety)
72
+ try:
73
+ src_conn.backup(dst_conn)
74
+ finally:
75
+ dst_conn.close()
76
+ finally:
77
+ src_conn.close()
78
+
79
+ # Restore
80
+ restore_conn = sqlite3.connect(src)
81
+ try:
82
+ target_conn = sqlite3.connect(DB_PATH)
83
+ try:
84
+ restore_conn.backup(target_conn)
85
+ finally:
86
+ target_conn.close()
87
+ finally:
88
+ restore_conn.close()
89
+
90
+ # Invalidate shared connection so db.py reconnects to restored data
91
+ import db
92
+ if db._shared_conn is not None:
93
+ try:
94
+ db._shared_conn.close()
95
+ except Exception:
96
+ pass
97
+ db._shared_conn = None
98
+
99
+ return f"DB restaurada desde {filename}. Safety backup: {os.path.basename(safety)}"
100
+
101
+
102
+ def _cleanup_old():
103
+ """Remove backups older than RETENTION_DAYS.
104
+
105
+ Covers both the hourly `nexo-YYYY-MM-DD-HHMM.db` snapshots and the
106
+ `nexo-pre-restore-*.db` safety snapshots created by handle_backup_restore.
107
+ Failures are swallowed — housekeeping must never interrupt the caller.
108
+ """
109
+ if not os.path.isdir(BACKUP_DIR):
110
+ return
111
+ cutoff = time.time() - (RETENTION_DAYS * 86400)
112
+ # glob `nexo-*.db` matches both the hourly pattern and pre-restore
113
+ # snapshots, so a single loop prunes both with a single pass.
114
+ for f in glob.glob(os.path.join(BACKUP_DIR, "nexo-*.db")):
115
+ try:
116
+ if os.path.getmtime(f) < cutoff:
117
+ os.remove(f)
118
+ except OSError:
119
+ # Permission / concurrent removal — skip silently.
120
+ pass
121
+
122
+
123
+ TOOLS = [
124
+ (handle_backup_now, "nexo_backup_now", "Create an immediate backup of the NEXO database"),
125
+ (handle_backup_list, "nexo_backup_list", "List available backups with dates and sizes"),
126
+ (handle_backup_restore, "nexo_backup_restore", "Restore database from a backup (DESTRUCTIVE)"),
127
+ ]