nexo-brain 2.2.0 → 2.3.1

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 (256) hide show
  1. package/README.md +5 -5
  2. package/package.json +6 -3
  3. package/src/auto_update.py +26 -0
  4. package/src/crons/manifest.json +6 -13
  5. package/src/crons/sync.py +150 -6
  6. package/src/db/__init__.py +13 -0
  7. package/src/db/_core.py +1 -0
  8. package/src/db/_cron_runs.py +74 -0
  9. package/src/db/_entities.py +1 -0
  10. package/src/db/_episodic.py +41 -6
  11. package/src/db/_learnings.py +1 -0
  12. package/src/db/_reminders.py +1 -0
  13. package/src/db/_schema.py +64 -0
  14. package/src/db/_sessions.py +1 -0
  15. package/src/db/_skills.py +515 -0
  16. package/src/hooks/session-stop.sh +13 -101
  17. package/src/plugin_loader.py +1 -0
  18. package/src/plugins/episodic_memory.py +5 -3
  19. package/src/plugins/schedule.py +212 -0
  20. package/src/plugins/skills.py +264 -0
  21. package/src/plugins/update.py +1 -0
  22. package/src/scripts/deep-sleep/apply_findings.py +111 -8
  23. package/src/scripts/deep-sleep/collect.py +34 -11
  24. package/src/scripts/deep-sleep/extract-prompt.md +38 -0
  25. package/src/scripts/deep-sleep/extract.py +81 -8
  26. package/src/scripts/deep-sleep/synthesize-prompt.md +29 -1
  27. package/src/scripts/deep-sleep/synthesize.py +4 -1
  28. package/src/scripts/nexo-catchup.py +65 -29
  29. package/src/scripts/nexo-cron-wrapper.sh +53 -0
  30. package/src/scripts/nexo-daily-self-audit.py +4 -2
  31. package/src/scripts/nexo-deep-sleep.sh +66 -77
  32. package/src/scripts/nexo-evolution-run.py +13 -0
  33. package/src/scripts/nexo-learning-housekeep.py +157 -1
  34. package/src/scripts/nexo-learning-validator.py +19 -0
  35. package/src/scripts/nexo-postmortem-consolidator.py +3 -2
  36. package/src/scripts/nexo-sleep.py +16 -11
  37. package/src/scripts/nexo-synthesis.py +46 -3
  38. package/src/scripts/nexo-watchdog.sh +91 -30
  39. package/src/server.py +6 -1
  40. package/src/tools_coordination.py +1 -0
  41. package/src/tools_sessions.py +1 -0
  42. package/scripts/migrate-to-unified 2.sh +0 -813
  43. package/scripts/migrate-to-unified.sh +0 -813
  44. package/scripts/migrate-v1.5-to-v1.6 2.py +0 -778
  45. package/scripts/migrate-v1.5-to-v1.6.py +0 -778
  46. package/scripts/migrate-v1.7-to-v1.8 2.py +0 -214
  47. package/scripts/migrate-v1.7-to-v1.8.py +0 -214
  48. package/scripts/pre-commit-check 2.sh +0 -55
  49. package/scripts/pre-commit-check.sh +0 -55
  50. package/src/__pycache__/auto_update.cpython-310.pyc +0 -0
  51. package/src/__pycache__/hnsw_index.cpython-310.pyc +0 -0
  52. package/src/__pycache__/kg_populate.cpython-310.pyc +0 -0
  53. package/src/__pycache__/knowledge_graph.cpython-310.pyc +0 -0
  54. package/src/__pycache__/plugin_loader.cpython-310.pyc +0 -0
  55. package/src/__pycache__/tools_coordination.cpython-310.pyc +0 -0
  56. package/src/__pycache__/tools_credentials.cpython-310.pyc +0 -0
  57. package/src/__pycache__/tools_learnings.cpython-310.pyc +0 -0
  58. package/src/__pycache__/tools_menu.cpython-310.pyc +0 -0
  59. package/src/__pycache__/tools_reminders.cpython-310.pyc +0 -0
  60. package/src/__pycache__/tools_reminders_crud.cpython-310.pyc +0 -0
  61. package/src/__pycache__/tools_sessions.cpython-310.pyc +0 -0
  62. package/src/__pycache__/tools_task_history.cpython-310.pyc +0 -0
  63. package/src/auto_close_sessions 2.py +0 -159
  64. package/src/auto_update 2.py +0 -634
  65. package/src/claim_graph 2.py +0 -323
  66. package/src/cognitive/__init__ 2.py +0 -62
  67. package/src/cognitive/__pycache__/__init__.cpython-310.pyc +0 -0
  68. package/src/cognitive/__pycache__/_core.cpython-310.pyc +0 -0
  69. package/src/cognitive/__pycache__/_decay.cpython-310.pyc +0 -0
  70. package/src/cognitive/__pycache__/_ingest.cpython-310.pyc +0 -0
  71. package/src/cognitive/__pycache__/_memory.cpython-310.pyc +0 -0
  72. package/src/cognitive/__pycache__/_search.cpython-310.pyc +0 -0
  73. package/src/cognitive/__pycache__/_trust.cpython-310.pyc +0 -0
  74. package/src/cognitive/_core 2.py +0 -567
  75. package/src/cognitive/_decay 2.py +0 -382
  76. package/src/cognitive/_ingest 2.py +0 -892
  77. package/src/cognitive/_memory 2.py +0 -912
  78. package/src/cognitive/_search 2.py +0 -949
  79. package/src/cognitive/_trust 2.py +0 -464
  80. package/src/crons/manifest 2.json +0 -106
  81. package/src/crons/sync 2.py +0 -217
  82. package/src/dashboard/__init__ 2.py +0 -0
  83. package/src/dashboard/__pycache__/__init__.cpython-310.pyc +0 -0
  84. package/src/dashboard/__pycache__/app.cpython-310.pyc +0 -0
  85. package/src/dashboard/app 2.py +0 -789
  86. package/src/db/__init__ 2.py +0 -89
  87. package/src/db/__pycache__/__init__.cpython-310.pyc +0 -0
  88. package/src/db/__pycache__/__init__.cpython-312.pyc +0 -0
  89. package/src/db/__pycache__/__init__.cpython-314.pyc +0 -0
  90. package/src/db/__pycache__/_core.cpython-310.pyc +0 -0
  91. package/src/db/__pycache__/_core.cpython-312.pyc +0 -0
  92. package/src/db/__pycache__/_core.cpython-314.pyc +0 -0
  93. package/src/db/__pycache__/_credentials.cpython-310.pyc +0 -0
  94. package/src/db/__pycache__/_credentials.cpython-312.pyc +0 -0
  95. package/src/db/__pycache__/_credentials.cpython-314.pyc +0 -0
  96. package/src/db/__pycache__/_entities.cpython-310.pyc +0 -0
  97. package/src/db/__pycache__/_entities.cpython-312.pyc +0 -0
  98. package/src/db/__pycache__/_entities.cpython-314.pyc +0 -0
  99. package/src/db/__pycache__/_episodic.cpython-310.pyc +0 -0
  100. package/src/db/__pycache__/_episodic.cpython-312.pyc +0 -0
  101. package/src/db/__pycache__/_episodic.cpython-314.pyc +0 -0
  102. package/src/db/__pycache__/_evolution.cpython-310.pyc +0 -0
  103. package/src/db/__pycache__/_evolution.cpython-312.pyc +0 -0
  104. package/src/db/__pycache__/_evolution.cpython-314.pyc +0 -0
  105. package/src/db/__pycache__/_fts.cpython-310.pyc +0 -0
  106. package/src/db/__pycache__/_fts.cpython-312.pyc +0 -0
  107. package/src/db/__pycache__/_fts.cpython-314.pyc +0 -0
  108. package/src/db/__pycache__/_learnings.cpython-310.pyc +0 -0
  109. package/src/db/__pycache__/_learnings.cpython-312.pyc +0 -0
  110. package/src/db/__pycache__/_learnings.cpython-314.pyc +0 -0
  111. package/src/db/__pycache__/_reminders.cpython-310.pyc +0 -0
  112. package/src/db/__pycache__/_reminders.cpython-312.pyc +0 -0
  113. package/src/db/__pycache__/_reminders.cpython-314.pyc +0 -0
  114. package/src/db/__pycache__/_schema.cpython-310.pyc +0 -0
  115. package/src/db/__pycache__/_schema.cpython-312.pyc +0 -0
  116. package/src/db/__pycache__/_schema.cpython-314.pyc +0 -0
  117. package/src/db/__pycache__/_sessions.cpython-310.pyc +0 -0
  118. package/src/db/__pycache__/_sessions.cpython-312.pyc +0 -0
  119. package/src/db/__pycache__/_sessions.cpython-314.pyc +0 -0
  120. package/src/db/__pycache__/_tasks.cpython-310.pyc +0 -0
  121. package/src/db/__pycache__/_tasks.cpython-312.pyc +0 -0
  122. package/src/db/__pycache__/_tasks.cpython-314.pyc +0 -0
  123. package/src/db/_core 2.py +0 -417
  124. package/src/db/_credentials 2.py +0 -124
  125. package/src/db/_entities 2.py +0 -178
  126. package/src/db/_episodic 2.py +0 -738
  127. package/src/db/_evolution 2.py +0 -54
  128. package/src/db/_fts 2.py +0 -406
  129. package/src/db/_learnings 2.py +0 -168
  130. package/src/db/_reminders 2.py +0 -338
  131. package/src/db/_schema 2.py +0 -364
  132. package/src/db/_sessions 2.py +0 -300
  133. package/src/db/_tasks 2.py +0 -91
  134. package/src/evolution_cycle 2.py +0 -266
  135. package/src/hnsw_index 2.py +0 -254
  136. package/src/hooks/auto_capture 2.py +0 -208
  137. package/src/hooks/caffeinate-guard 2.sh +0 -8
  138. package/src/hooks/capture-session 2.sh +0 -21
  139. package/src/hooks/capture-tool-logs 2.sh +0 -127
  140. package/src/hooks/daily-briefing-check 2.sh +0 -33
  141. package/src/hooks/inbox-hook 2.sh +0 -76
  142. package/src/hooks/post-compact 2.sh +0 -148
  143. package/src/hooks/pre-compact 2.sh +0 -151
  144. package/src/hooks/session-start 2.sh +0 -268
  145. package/src/hooks/session-stop 2.sh +0 -140
  146. package/src/kg_populate 2.py +0 -290
  147. package/src/knowledge_graph 2.py +0 -257
  148. package/src/maintenance 2.py +0 -59
  149. package/src/migrate_embeddings 2.py +0 -122
  150. package/src/plugin_loader 2.py +0 -202
  151. package/src/plugins/__init__ 2.py +0 -0
  152. package/src/plugins/__pycache__/__init__ 2.cpython-310.pyc +0 -0
  153. package/src/plugins/__pycache__/__init__.cpython-310.pyc +0 -0
  154. package/src/plugins/__pycache__/adaptive_mode 2.cpython-310.pyc +0 -0
  155. package/src/plugins/__pycache__/adaptive_mode.cpython-310.pyc +0 -0
  156. package/src/plugins/__pycache__/agents 2.cpython-310.pyc +0 -0
  157. package/src/plugins/__pycache__/agents.cpython-310.pyc +0 -0
  158. package/src/plugins/__pycache__/artifact_registry 2.cpython-310.pyc +0 -0
  159. package/src/plugins/__pycache__/artifact_registry.cpython-310.pyc +0 -0
  160. package/src/plugins/__pycache__/backup 2.cpython-310.pyc +0 -0
  161. package/src/plugins/__pycache__/backup.cpython-310.pyc +0 -0
  162. package/src/plugins/__pycache__/cognitive_memory 2.cpython-310.pyc +0 -0
  163. package/src/plugins/__pycache__/cognitive_memory.cpython-310.pyc +0 -0
  164. package/src/plugins/__pycache__/core_rules 2.cpython-310.pyc +0 -0
  165. package/src/plugins/__pycache__/core_rules.cpython-310.pyc +0 -0
  166. package/src/plugins/__pycache__/cortex 2.cpython-310.pyc +0 -0
  167. package/src/plugins/__pycache__/cortex.cpython-310.pyc +0 -0
  168. package/src/plugins/__pycache__/entities 2.cpython-310.pyc +0 -0
  169. package/src/plugins/__pycache__/entities.cpython-310.pyc +0 -0
  170. package/src/plugins/__pycache__/episodic_memory 2.cpython-310.pyc +0 -0
  171. package/src/plugins/__pycache__/episodic_memory.cpython-310.pyc +0 -0
  172. package/src/plugins/__pycache__/evolution 2.cpython-310.pyc +0 -0
  173. package/src/plugins/__pycache__/evolution.cpython-310.pyc +0 -0
  174. package/src/plugins/__pycache__/guard 2.cpython-310.pyc +0 -0
  175. package/src/plugins/__pycache__/guard.cpython-310.pyc +0 -0
  176. package/src/plugins/__pycache__/knowledge_graph_tools 2.cpython-310.pyc +0 -0
  177. package/src/plugins/__pycache__/knowledge_graph_tools.cpython-310.pyc +0 -0
  178. package/src/plugins/__pycache__/preferences 2.cpython-310.pyc +0 -0
  179. package/src/plugins/__pycache__/preferences.cpython-310.pyc +0 -0
  180. package/src/plugins/__pycache__/update 2.cpython-310.pyc +0 -0
  181. package/src/plugins/__pycache__/update.cpython-310.pyc +0 -0
  182. package/src/plugins/adaptive_mode 2.py +0 -805
  183. package/src/plugins/agents 2.py +0 -52
  184. package/src/plugins/artifact_registry 2.py +0 -450
  185. package/src/plugins/backup 2.py +0 -104
  186. package/src/plugins/cognitive_memory 2.py +0 -564
  187. package/src/plugins/core_rules 2.py +0 -252
  188. package/src/plugins/cortex 2.py +0 -299
  189. package/src/plugins/entities 2.py +0 -67
  190. package/src/plugins/episodic_memory 2.py +0 -533
  191. package/src/plugins/evolution 2.py +0 -115
  192. package/src/plugins/guard 2.py +0 -746
  193. package/src/plugins/knowledge_graph_tools 2.py +0 -105
  194. package/src/plugins/preferences 2.py +0 -47
  195. package/src/plugins/update 2.py +0 -256
  196. package/src/requirements 2.txt +0 -12
  197. package/src/rules/__init__ 2.py +0 -0
  198. package/src/rules/core-rules 2.json +0 -331
  199. package/src/rules/migrate 2.py +0 -207
  200. package/src/scripts/check-context 2.py +0 -264
  201. package/src/scripts/nexo-auto-update 2.py +0 -6
  202. package/src/scripts/nexo-backup 2.sh +0 -25
  203. package/src/scripts/nexo-brain-activation 2.sh +0 -140
  204. package/src/scripts/nexo-catchup 2.py +0 -242
  205. package/src/scripts/nexo-cognitive-decay 2.py +0 -182
  206. package/src/scripts/nexo-daily-self-audit 2.py +0 -552
  207. package/src/scripts/nexo-deep-sleep 2.sh +0 -97
  208. package/src/scripts/nexo-evolution-run 2.py +0 -597
  209. package/src/scripts/nexo-followup-hygiene 2.py +0 -112
  210. package/src/scripts/nexo-github-monitor 2.py +0 -256
  211. package/src/scripts/nexo-github-monitor.py +0 -256
  212. package/src/scripts/nexo-immune 2.py +0 -927
  213. package/src/scripts/nexo-inbox-hook 2.sh +0 -74
  214. package/src/scripts/nexo-install 2.py +0 -6
  215. package/src/scripts/nexo-learning-housekeep 2.py +0 -245
  216. package/src/scripts/nexo-learning-validator 2.py +0 -207
  217. package/src/scripts/nexo-migrate 2.py +0 -232
  218. package/src/scripts/nexo-postmortem-consolidator 2.py +0 -421
  219. package/src/scripts/nexo-pre-commit 2.py +0 -120
  220. package/src/scripts/nexo-prevent-sleep 2.sh +0 -29
  221. package/src/scripts/nexo-proactive-dashboard 2.py +0 -345
  222. package/src/scripts/nexo-reflection 2.py +0 -253
  223. package/src/scripts/nexo-runtime-preflight 2.py +0 -274
  224. package/src/scripts/nexo-send-email 2.py +0 -25
  225. package/src/scripts/nexo-send-email.py +0 -25
  226. package/src/scripts/nexo-send-reply 2.py +0 -178
  227. package/src/scripts/nexo-send-reply.py +0 -178
  228. package/src/scripts/nexo-sleep 2.py +0 -592
  229. package/src/scripts/nexo-snapshot-restore 2.sh +0 -35
  230. package/src/scripts/nexo-synthesis 2.py +0 -253
  231. package/src/scripts/nexo-tcc-approve 2.sh +0 -79
  232. package/src/scripts/nexo-update 2.sh +0 -161
  233. package/src/scripts/nexo-watchdog 2.sh +0 -878
  234. package/src/scripts/nexo-watchdog-smoke 2.py +0 -119
  235. package/src/server 2.py +0 -733
  236. package/src/storage_router 2.py +0 -32
  237. package/src/tools_coordination 2.py +0 -102
  238. package/src/tools_credentials 2.py +0 -68
  239. package/src/tools_learnings 2.py +0 -220
  240. package/src/tools_menu 2.py +0 -227
  241. package/src/tools_reminders 2.py +0 -86
  242. package/src/tools_reminders_crud 2.py +0 -159
  243. package/src/tools_sessions 2.py +0 -476
  244. package/src/tools_task_history 2.py +0 -57
  245. package/templates/CLAUDE.md 2.template +0 -63
  246. package/templates/openclaw 2.json +0 -13
  247. package/tests/__init__ 2.py +0 -0
  248. package/tests/__init__.py +0 -0
  249. package/tests/conftest 2.py +0 -71
  250. package/tests/conftest.py +0 -71
  251. package/tests/test_cognitive 2.py +0 -205
  252. package/tests/test_cognitive.py +0 -205
  253. package/tests/test_knowledge_graph 2.py +0 -140
  254. package/tests/test_knowledge_graph.py +0 -140
  255. package/tests/test_migrations 2.py +0 -137
  256. package/tests/test_migrations.py +0 -137
@@ -1,52 +0,0 @@
1
- """Agents plugin — registry of known agent types with their configs."""
2
- from db import create_agent, get_agent, list_agents, update_agent, delete_agent
3
-
4
- def handle_agent_get(id: str) -> str:
5
- """Get an agent's full profile by ID."""
6
- a = get_agent(id)
7
- if not a: return f"Agent '{id}' not found."
8
- lines = [f"AGENT: {a['name']} ({a['id']})", f" Specialization: {a['specialization']}", f" Model: {a['model']}"]
9
- if a["tools"]: lines.append(f" Tools: {a['tools']}")
10
- if a["context_files"]: lines.append(f" Contexto: {a['context_files']}")
11
- if a["rules"]: lines.append(f" Reglas: {a['rules']}")
12
- return "\n".join(lines)
13
-
14
- def handle_agent_create(id: str, name: str, specialization: str, model: str = "sonnet",
15
- tools: str = "", context_files: str = "", rules: str = "") -> str:
16
- """Register a new agent in the registry."""
17
- create_agent(id, name, specialization, model, tools, context_files, rules)
18
- return f"Agent '{id}' ({name}) registered. Model: {model}"
19
-
20
- def handle_agent_update(id: str, name: str = "", specialization: str = "", model: str = "",
21
- tools: str = "", context_files: str = "", rules: str = "") -> str:
22
- """Update agent fields. Only non-empty fields are changed."""
23
- kwargs = {}
24
- for k, v in [("name", name), ("specialization", specialization), ("model", model),
25
- ("tools", tools), ("context_files", context_files), ("rules", rules)]:
26
- if v: kwargs[k] = v
27
- if not kwargs: return "Nothing to update."
28
- update_agent(id, **kwargs)
29
- return f"Agent '{id}' updated."
30
-
31
- def handle_agent_list() -> str:
32
- """List all registered agents."""
33
- agents = list_agents()
34
- if not agents: return "No agents registered."
35
- lines = ["REGISTERED AGENTS:"]
36
- for a in agents:
37
- lines.append(f" {a['id']} — {a['name']} ({a['model']}) — {a['specialization'][:60]}")
38
- return "\n".join(lines)
39
-
40
- def handle_agent_delete(id: str) -> str:
41
- """Remove an agent from the registry."""
42
- if not delete_agent(id):
43
- return f"ERROR: Agent '{id}' not found."
44
- return f"Agent '{id}' deleted."
45
-
46
- TOOLS = [
47
- (handle_agent_get, "nexo_agent_get", "Get an agent's full profile"),
48
- (handle_agent_create, "nexo_agent_create", "Register a new agent"),
49
- (handle_agent_update, "nexo_agent_update", "Update agent fields"),
50
- (handle_agent_list, "nexo_agent_list", "List all registered agents"),
51
- (handle_agent_delete, "nexo_agent_delete", "Remove an agent from registry"),
52
- ]
@@ -1,450 +0,0 @@
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
- ]
@@ -1,104 +0,0 @@
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
- dst_conn = sqlite3.connect(dest)
25
- src_conn.backup(dst_conn)
26
- dst_conn.close()
27
- src_conn.close()
28
-
29
- size_kb = os.path.getsize(dest) / 1024
30
- _cleanup_old()
31
- return f"Backup created: {os.path.basename(dest)} ({size_kb:.0f} KB)"
32
-
33
-
34
- def handle_backup_list() -> str:
35
- """List available backups with dates and sizes."""
36
- if not os.path.isdir(BACKUP_DIR):
37
- return "No backups."
38
- files = sorted(glob.glob(os.path.join(BACKUP_DIR, "nexo-*.db")), reverse=True)
39
- if not files:
40
- return "No backups."
41
- lines = [f"BACKUPS ({len(files)}):"]
42
- total_size = 0
43
- for f in files:
44
- size = os.path.getsize(f) / 1024
45
- total_size += size
46
- name = os.path.basename(f)
47
- lines.append(f" {name} ({size:.0f} KB)")
48
- lines.append(f"\n Total: {total_size/1024:.1f} MB")
49
- return "\n".join(lines)
50
-
51
-
52
- def handle_backup_restore(filename: str) -> str:
53
- """Restore database from a backup file. DESTRUCTIVE — replaces current DB.
54
-
55
- Args:
56
- filename: Backup filename (e.g., 'nexo-2026-03-11-1200.db')
57
- """
58
- src = os.path.join(BACKUP_DIR, filename)
59
- if not os.path.isfile(src):
60
- return f"Backup not found: {filename}"
61
-
62
- # Create safety backup first
63
- safety = os.path.join(BACKUP_DIR, f"nexo-pre-restore-{time.strftime('%Y%m%d%H%M%S')}.db")
64
- import sqlite3
65
- src_conn = sqlite3.connect(DB_PATH)
66
- dst_conn = sqlite3.connect(safety)
67
- src_conn.backup(dst_conn)
68
- dst_conn.close()
69
- src_conn.close()
70
-
71
- # Restore
72
- restore_conn = sqlite3.connect(src)
73
- target_conn = sqlite3.connect(DB_PATH)
74
- restore_conn.backup(target_conn)
75
- target_conn.close()
76
- restore_conn.close()
77
-
78
- # Invalidate shared connection so db.py reconnects to restored data
79
- import db
80
- if db._shared_conn is not None:
81
- try:
82
- db._shared_conn.close()
83
- except Exception:
84
- pass
85
- db._shared_conn = None
86
-
87
- return f"DB restaurada desde {filename}. Safety backup: {os.path.basename(safety)}"
88
-
89
-
90
- def _cleanup_old():
91
- """Remove backups older than RETENTION_DAYS."""
92
- if not os.path.isdir(BACKUP_DIR):
93
- return
94
- cutoff = time.time() - (RETENTION_DAYS * 86400)
95
- for f in glob.glob(os.path.join(BACKUP_DIR, "nexo-*.db")):
96
- if os.path.getmtime(f) < cutoff:
97
- os.remove(f)
98
-
99
-
100
- TOOLS = [
101
- (handle_backup_now, "nexo_backup_now", "Create an immediate backup of the NEXO database"),
102
- (handle_backup_list, "nexo_backup_list", "List available backups with dates and sizes"),
103
- (handle_backup_restore, "nexo_backup_restore", "Restore database from a backup (DESTRUCTIVE)"),
104
- ]