nexo-brain 0.8.10 → 0.10.0-beta.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.
package/README.md CHANGED
@@ -1,12 +1,14 @@
1
1
  # NEXO Brain — Your AI Gets a Brain
2
2
 
3
- [![npm v0.8.0](https://img.shields.io/npm/v/nexo-brain?label=npm&color=purple)](https://www.npmjs.com/package/nexo-brain)
3
+ [![npm v0.9.0](https://img.shields.io/npm/v/nexo-brain?label=npm&color=purple)](https://www.npmjs.com/package/nexo-brain)
4
4
  [![F1 0.588 on LoCoMo](https://img.shields.io/badge/LoCoMo_F1-0.588-brightgreen)](https://github.com/wazionapps/nexo/blob/main/benchmarks/locomo/results/)
5
5
  [![+55% vs GPT-4](https://img.shields.io/badge/vs_GPT--4-%2B55%25-blue)](https://github.com/snap-research/locomo/issues/33)
6
6
  [![GitHub stars](https://img.shields.io/github/stars/wazionapps/nexo?style=social)](https://github.com/wazionapps/nexo/stargazers)
7
7
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
8
 
9
- > **v0.8.0** — Knowledge Graph (988 bi-temporal nodes, D3 visualization), Web Dashboard (6 pages at localhost:6174), Cross-Platform support (Linux + Windows), Smart dedup with event-sourced edges, and 4 new KG tools.
9
+ > **v0.10.0-beta.1** — Artifact Registry (structured recall for services, dashboards, scripts), Retrieval Ladder (alias port path fuzzy semantic), User-Language Alias Learning. Plus everything from v0.9.0.
10
+ >
11
+ > Install beta: `npx nexo-brain@beta` | Stable: `npx nexo-brain`
10
12
 
11
13
  **NEXO Brain transforms any MCP-compatible AI agent from a stateless assistant into a cognitive partner that remembers, learns, forgets, adapts, and builds a relationship with you over time.**
12
14
 
@@ -16,7 +18,7 @@
16
18
  </a>
17
19
  </p>
18
20
 
19
- [Watch the 1-minute overview on YouTube](https://www.youtube.com/watch?v=J0hCWnYU4UY)
21
+ [Watch the 1-minute overview on YouTube](https://www.youtube.com/watch?v=J0hCWnYU4UY) · [Watch the full deep-dive](https://www.youtube.com/watch?v=-uvhicUhGTY)
20
22
 
21
23
  Every time you close a session, everything is lost. Your agent doesn't remember yesterday's decisions, repeats the same mistakes, and starts from zero. NEXO Brain fixes this with a cognitive architecture modeled after how human memory actually works.
22
24
 
@@ -263,7 +265,7 @@ npx nexo-brain # detects v0.5.0, migrates automatically
263
265
  - **Never touches your data** (memories, learnings, preferences)
264
266
  - Saves updated CLAUDE.md as reference (doesn't overwrite customizations)
265
267
 
266
- ## Knowledge Graph & Dashboard (v0.8.0)
268
+ ## Knowledge Graph & Dashboard (v0.8)
267
269
 
268
270
  ### Knowledge Graph
269
271
  A bi-temporal entity-relationship graph with 988 nodes and 896 edges. Entities and relationships carry both valid-time (when the fact was true) and system-time (when it was recorded), enabling temporal queries like "what did we know about X last Tuesday?". BFS traversal discovers multi-hop connections between concepts. Event-sourced edges with smart dedup (ADD/UPDATE/NOOP) prevent redundant writes while preserving full history.
@@ -274,7 +276,7 @@ A bi-temporal entity-relationship graph with 988 nodes and 896 edges. Entities a
274
276
  A visual interface at `localhost:6174` with 6 pages: Overview (system health at a glance), Graph (interactive D3.js visualization of the knowledge graph), Memory (browse and search all memory stores), Somatic (pain map per file/area), Adaptive (personality signals and weights), and Sessions (active and historical sessions). Built with FastAPI backend and D3.js frontend.
275
277
 
276
278
  ### Cross-Platform Support
277
- v0.8.0 adds full Linux support and Windows via WSL. The installer detects the platform and configures the appropriate process manager (LaunchAgents on macOS, catch-up on startup for Linux). Opportunistic maintenance runs cognitive processes when resources are available.
279
+ Full Linux support and Windows via WSL. The installer detects the platform and configures the appropriate process manager (LaunchAgents on macOS, catch-up on startup for Linux). PEP 668 compliance (venv on Ubuntu 24.04+). Session keepalive prevents phantom sessions during long tasks. Opportunistic maintenance runs cognitive processes when resources are available.
278
280
 
279
281
  > **Windows users:** NEXO Brain requires [WSL (Windows Subsystem for Linux)](https://learn.microsoft.com/en-us/windows/wsl/install). Install WSL first, then run `npx nexo-brain` inside the Ubuntu/WSL terminal.
280
282
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "0.8.10",
3
+ "version": "0.10.0-beta.1",
4
4
  "mcpName": "io.github.wazionapps/nexo",
5
5
  "description": "NEXO — Cognitive co-operator for Claude Code. Atkinson-Shiffrin memory, semantic RAG, trust scoring, and metacognitive error prevention.",
6
6
  "bin": {
package/src/cognitive.py CHANGED
@@ -30,13 +30,13 @@ DISCRIMINATING_ENTITIES = {
30
30
  # OS / Environment
31
31
  "linux", "mac", "macos", "windows", "darwin", "ubuntu", "debian", "alpine",
32
32
  # Platforms
33
- "shopify", "wazion", "canarirural", "recambios", "whatsapp", "chrome", "firefox",
33
+ "shopify", "wazion", "project-a", "project-b", "whatsapp", "chrome", "firefox",
34
34
  # Languages / Runtimes
35
35
  "python", "php", "javascript", "typescript", "node", "deno", "ruby",
36
36
  # Versions
37
37
  "v1", "v2", "v3", "v4", "v5", "5.6", "7.4", "8.0", "8.1", "8.2",
38
38
  # Infrastructure
39
- "mundiserver", "cloudrun", "gcloud", "vps", "local", "production", "staging",
39
+ "server", "cloudrun", "gcloud", "vps", "local", "production", "staging",
40
40
  # DB
41
41
  "mysql", "sqlite", "postgresql", "postgres", "redis",
42
42
  }
package/src/db.py CHANGED
@@ -898,6 +898,85 @@ def _m8_adaptive_log_and_somatic(conn):
898
898
  conn.execute("CREATE INDEX IF NOT EXISTS idx_somatic_events_projected ON somatic_events(projected)")
899
899
 
900
900
 
901
+ def _m10_diary_archive(conn):
902
+ """Permanent diary archive — diaries are never truly deleted, just moved here."""
903
+ conn.execute("""
904
+ CREATE TABLE IF NOT EXISTS diary_archive (
905
+ id INTEGER PRIMARY KEY,
906
+ session_id TEXT NOT NULL,
907
+ created_at TEXT NOT NULL,
908
+ decisions TEXT NOT NULL,
909
+ discarded TEXT,
910
+ pending TEXT,
911
+ context_next TEXT,
912
+ summary TEXT NOT NULL,
913
+ mental_state TEXT,
914
+ domain TEXT,
915
+ user_signals TEXT,
916
+ self_critique TEXT DEFAULT '',
917
+ source TEXT DEFAULT 'claude',
918
+ archived_at TEXT DEFAULT (datetime('now'))
919
+ )
920
+ """)
921
+ conn.execute("""
922
+ CREATE INDEX IF NOT EXISTS idx_diary_archive_created
923
+ ON diary_archive (created_at)
924
+ """)
925
+ conn.execute("""
926
+ CREATE INDEX IF NOT EXISTS idx_diary_archive_domain
927
+ ON diary_archive (domain)
928
+ """)
929
+
930
+
931
+ def _m11_artifact_registry(conn):
932
+ """Artifact Registry — structured index of things the agent creates/deploys.
933
+
934
+ Solves 'recent work amnesia': agents build services, dashboards, scripts, APIs
935
+ but can't find them hours later because semantic search fails on operational
936
+ vocabulary mismatches (e.g., 'backend' vs 'FastAPI localhost:6174').
937
+
938
+ Design informed by multi-AI debate (GPT-5.4 + Gemini 3.1 Pro + Claude Opus 4.6).
939
+ """
940
+ conn.execute("""
941
+ CREATE TABLE IF NOT EXISTS artifact_registry (
942
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
943
+ kind TEXT NOT NULL,
944
+ canonical_name TEXT NOT NULL,
945
+ aliases TEXT DEFAULT '[]',
946
+ description TEXT DEFAULT '',
947
+ uri TEXT DEFAULT '',
948
+ ports TEXT DEFAULT '[]',
949
+ paths TEXT DEFAULT '[]',
950
+ run_cmd TEXT DEFAULT '',
951
+ repo TEXT DEFAULT '',
952
+ domain TEXT DEFAULT '',
953
+ state TEXT DEFAULT 'active',
954
+ session_id TEXT DEFAULT '',
955
+ created_at TEXT DEFAULT (datetime('now')),
956
+ last_touched_at TEXT DEFAULT (datetime('now')),
957
+ last_verified_at TEXT DEFAULT NULL,
958
+ metadata TEXT DEFAULT '{}'
959
+ )
960
+ """)
961
+ conn.execute("""
962
+ CREATE TABLE IF NOT EXISTS artifact_aliases (
963
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
964
+ artifact_id INTEGER NOT NULL REFERENCES artifact_registry(id) ON DELETE CASCADE,
965
+ phrase TEXT NOT NULL,
966
+ source TEXT DEFAULT 'manual',
967
+ confidence REAL DEFAULT 1.0,
968
+ created_at TEXT DEFAULT (datetime('now')),
969
+ UNIQUE(artifact_id, phrase)
970
+ )
971
+ """)
972
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_state ON artifact_registry(state)")
973
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_kind ON artifact_registry(kind)")
974
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_domain ON artifact_registry(domain)")
975
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_last_touched ON artifact_registry(last_touched_at)")
976
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_aliases_phrase ON artifact_aliases(phrase)")
977
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_artifact_aliases_aid ON artifact_aliases(artifact_id)")
978
+
979
+
901
980
  def _m9_maintenance_schedule(conn):
902
981
  conn.execute("""
903
982
  CREATE TABLE IF NOT EXISTS maintenance_schedule (
@@ -931,6 +1010,8 @@ MIGRATIONS = [
931
1010
  (7, "diary_source_and_draft", _m7_diary_source_and_draft),
932
1011
  (8, "adaptive_log_and_somatic", _m8_adaptive_log_and_somatic),
933
1012
  (9, "maintenance_schedule", _m9_maintenance_schedule),
1013
+ (10, "diary_archive", _m10_diary_archive),
1014
+ (11, "artifact_registry", _m11_artifact_registry),
934
1015
  ]
935
1016
 
936
1017
 
@@ -1763,7 +1844,12 @@ def delete_credential(service: str, key: str = None) -> bool:
1763
1844
 
1764
1845
 
1765
1846
  def get_credential(service: str, key: str = None) -> list[dict]:
1766
- """Get credential(s). If key=None, return all for the service."""
1847
+ """Get credential(s). If key=None, return all for the service.
1848
+
1849
+ When exact match fails, performs fuzzy search across service, key,
1850
+ and notes fields. Returns results tagged with _fuzzy=True so
1851
+ the caller can differentiate suggestions from exact hits.
1852
+ """
1767
1853
  conn = get_db()
1768
1854
  if key:
1769
1855
  rows = conn.execute(
@@ -1773,7 +1859,29 @@ def get_credential(service: str, key: str = None) -> list[dict]:
1773
1859
  rows = conn.execute(
1774
1860
  "SELECT * FROM credentials WHERE service = ?", (service,)
1775
1861
  ).fetchall()
1776
- return [dict(r) for r in rows]
1862
+ if rows:
1863
+ return [dict(r) for r in rows]
1864
+
1865
+ # Fuzzy fallback: search term in service, key and notes (not value — too noisy)
1866
+ # Prioritize: service/key matches first, notes-only matches second
1867
+ term = f"%{service}%"
1868
+ fuzzy_rows = conn.execute(
1869
+ "SELECT *, "
1870
+ "CASE WHEN service LIKE ? THEN 0 "
1871
+ " WHEN key LIKE ? THEN 1 "
1872
+ " ELSE 2 END AS _rank "
1873
+ "FROM credentials WHERE "
1874
+ "service LIKE ? OR key LIKE ? OR notes LIKE ? "
1875
+ "ORDER BY _rank ASC, service ASC, key ASC",
1876
+ (term, term, term, term, term),
1877
+ ).fetchall()
1878
+ results = []
1879
+ for r in fuzzy_rows:
1880
+ d = dict(r)
1881
+ d["_fuzzy"] = True
1882
+ d.pop("_rank", None)
1883
+ results.append(d)
1884
+ return results
1777
1885
 
1778
1886
 
1779
1887
  def list_credentials(service: str = None) -> list[dict]:
@@ -2262,15 +2370,37 @@ def search_decisions(query: str = '', domain: str = '', days: int = 30) -> list[
2262
2370
  # ── Session Diary ────────────────────────────────────────────────
2263
2371
 
2264
2372
  def cleanup_old_diaries(retention_days: int = 180) -> int:
2265
- """Delete session_diary entries older than retention_days. Returns count deleted."""
2373
+ """Archive then delete session_diary entries older than retention_days.
2374
+
2375
+ Diaries are moved to diary_archive (permanent) before being removed from
2376
+ the active session_diary table. Nothing is ever truly lost.
2377
+ """
2266
2378
  conn = get_db()
2379
+ cutoff = f"-{retention_days} days"
2380
+
2381
+ # Archive before deleting — permanent subconscious memory
2382
+ try:
2383
+ conn.execute("""
2384
+ INSERT OR IGNORE INTO diary_archive
2385
+ (id, session_id, created_at, decisions, discarded, pending,
2386
+ context_next, summary, mental_state, domain, user_signals,
2387
+ self_critique, source)
2388
+ SELECT id, session_id, created_at, decisions, discarded, pending,
2389
+ context_next, summary, mental_state, domain, user_signals,
2390
+ self_critique, source
2391
+ FROM session_diary
2392
+ WHERE created_at < datetime('now', ?)
2393
+ """, (cutoff,))
2394
+ except Exception:
2395
+ pass # Table may not exist yet (pre-migration)
2396
+
2267
2397
  ids = [str(r[0]) for r in conn.execute(
2268
2398
  "SELECT id FROM session_diary WHERE created_at < datetime('now', ?)",
2269
- (f"-{retention_days} days",)
2399
+ (cutoff,)
2270
2400
  ).fetchall()]
2271
2401
  cursor = conn.execute(
2272
2402
  "DELETE FROM session_diary WHERE created_at < datetime('now', ?)",
2273
- (f"-{retention_days} days",)
2403
+ (cutoff,)
2274
2404
  )
2275
2405
  for did in ids:
2276
2406
  conn.execute("DELETE FROM unified_search WHERE source = 'diary' AND source_id = ?", (did,))
@@ -2309,6 +2439,99 @@ def check_session_has_diary(session_id: str) -> bool:
2309
2439
  return row is not None
2310
2440
 
2311
2441
 
2442
+ # ── Diary Archive (permanent subconscious) ──────────────────────
2443
+
2444
+
2445
+ def diary_archive_search(query: str = '', domain: str = '',
2446
+ year: int = 0, month: int = 0,
2447
+ limit: int = 20) -> list[dict]:
2448
+ """Search the permanent diary archive. Supports text search, domain filter, and date filter.
2449
+
2450
+ Args:
2451
+ query: Text to search in summary, decisions, mental_state, pending
2452
+ domain: Filter by domain (e.g. 'wazion', 'my-store')
2453
+ year: Filter by year (e.g. 2026)
2454
+ month: Filter by month (1-12), requires year
2455
+ limit: Max results (default 20)
2456
+ """
2457
+ conn = get_db()
2458
+ try:
2459
+ conn.execute("SELECT 1 FROM diary_archive LIMIT 1")
2460
+ except Exception:
2461
+ return [] # Table doesn't exist yet
2462
+
2463
+ conditions = []
2464
+ params = []
2465
+
2466
+ if query:
2467
+ words = query.strip().split()
2468
+ for word in words:
2469
+ conditions.append(
2470
+ "(summary LIKE ? OR decisions LIKE ? OR mental_state LIKE ? "
2471
+ "OR pending LIKE ? OR self_critique LIKE ?)"
2472
+ )
2473
+ w = f"%{word}%"
2474
+ params.extend([w, w, w, w, w])
2475
+
2476
+ if domain:
2477
+ conditions.append("domain = ?")
2478
+ params.append(domain)
2479
+
2480
+ if year:
2481
+ if month:
2482
+ date_start = f"{year:04d}-{month:02d}-01"
2483
+ if month == 12:
2484
+ date_end = f"{year + 1:04d}-01-01"
2485
+ else:
2486
+ date_end = f"{year:04d}-{month + 1:02d}-01"
2487
+ conditions.append("created_at >= ? AND created_at < ?")
2488
+ params.extend([date_start, date_end])
2489
+ else:
2490
+ conditions.append("created_at >= ? AND created_at < ?")
2491
+ params.extend([f"{year:04d}-01-01", f"{year + 1:04d}-01-01"])
2492
+
2493
+ where = " AND ".join(conditions) if conditions else "1=1"
2494
+
2495
+ rows = conn.execute(f"""
2496
+ SELECT id, session_id, created_at, summary, decisions, domain,
2497
+ mental_state, pending, self_critique, source
2498
+ FROM diary_archive
2499
+ WHERE {where}
2500
+ ORDER BY created_at DESC
2501
+ LIMIT ?
2502
+ """, params + [limit]).fetchall()
2503
+ return [dict(r) for r in rows]
2504
+
2505
+
2506
+ def diary_archive_read(diary_id: int) -> dict | None:
2507
+ """Read a single archived diary entry by ID — full content."""
2508
+ conn = get_db()
2509
+ try:
2510
+ row = conn.execute(
2511
+ "SELECT * FROM diary_archive WHERE id = ?", (diary_id,)
2512
+ ).fetchone()
2513
+ return dict(row) if row else None
2514
+ except Exception:
2515
+ return None
2516
+
2517
+
2518
+ def diary_archive_stats() -> dict:
2519
+ """Get archive statistics: count, date range, domains."""
2520
+ conn = get_db()
2521
+ try:
2522
+ count = conn.execute("SELECT COUNT(*) FROM diary_archive").fetchone()[0]
2523
+ if count == 0:
2524
+ return {"count": 0, "oldest": None, "newest": None, "domains": []}
2525
+ oldest = conn.execute("SELECT MIN(created_at) FROM diary_archive").fetchone()[0]
2526
+ newest = conn.execute("SELECT MAX(created_at) FROM diary_archive").fetchone()[0]
2527
+ domains = [r[0] for r in conn.execute(
2528
+ "SELECT DISTINCT domain FROM diary_archive WHERE domain IS NOT NULL AND domain != '' ORDER BY domain"
2529
+ ).fetchall()]
2530
+ return {"count": count, "oldest": oldest, "newest": newest, "domains": domains}
2531
+ except Exception:
2532
+ return {"count": 0, "oldest": None, "newest": None, "domains": []}
2533
+
2534
+
2312
2535
  # ── Session Diary Drafts ─────────────────────────────────────────
2313
2536
 
2314
2537
 
@@ -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 (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
+ ]
@@ -27,7 +27,7 @@ def handle_decision_log(domain: str, decision: str, alternatives: str = '',
27
27
  """Log a non-trivial decision with reasoning context.
28
28
 
29
29
  Args:
30
- domain: Area (ads, shopify, server, wazion, nexo, canarirural, other)
30
+ domain: Area (ads, shopify, server, wazion, nexo, project, other)
31
31
  decision: What was decided
32
32
  alternatives: JSON array or text of options considered and why discarded
33
33
  based_on: Data, metrics, or observations that informed this decision
@@ -35,7 +35,7 @@ def handle_decision_log(domain: str, decision: str, alternatives: str = '',
35
35
  context_ref: Related followup/reminder ID (e.g., NF-ADS1, R71)
36
36
  session_id: Current session ID (auto-filled if empty)
37
37
  """
38
- valid_domains = {'ads', 'shopify', 'server', 'wazion', 'nexo', 'canarirural', 'other'}
38
+ valid_domains = {'ads', 'shopify', 'server', 'wazion', 'nexo', 'project', 'other'}
39
39
  if domain not in valid_domains:
40
40
  return f"ERROR: domain debe ser uno de: {', '.join(sorted(valid_domains))}"
41
41
  if confidence not in ('high', 'medium', 'low'):
@@ -92,10 +92,10 @@ def handle_decision_search(query: str = '', domain: str = '', days: int = 30) ->
92
92
 
93
93
  Args:
94
94
  query: Text to search in decision, alternatives, based_on, outcome
95
- domain: Filter by area (ads, shopify, server, wazion, nexo, canarirural, other)
95
+ domain: Filter by area (ads, shopify, server, wazion, nexo, project, other)
96
96
  days: Look back N days (default 30)
97
97
  """
98
- valid_domains = {'ads', 'shopify', 'server', 'wazion', 'nexo', 'canarirural', 'other'}
98
+ valid_domains = {'ads', 'shopify', 'server', 'wazion', 'nexo', 'project', 'other'}
99
99
  if domain and domain not in valid_domains:
100
100
  return f"ERROR: domain debe ser uno de: {', '.join(sorted(valid_domains))}"
101
101
  results = search_decisions(query, domain, days)
@@ -163,11 +163,11 @@ def handle_memory_review_queue(days: int = 0) -> str:
163
163
  def handle_session_diary_write(decisions: str, summary: str,
164
164
  discarded: str = '', pending: str = '',
165
165
  context_next: str = '', mental_state: str = '',
166
- francisco_signals: str = '',
166
+ user_signals: str = '',
167
167
  domain: str = '',
168
168
  session_id: str = '',
169
169
  self_critique: str = '') -> str:
170
- """Write session diary entry at end of session. OBLIGATORIO antes de cerrar.
170
+ """Write session diary entry at end of session. Mandatory before closing.
171
171
 
172
172
  Args:
173
173
  decisions: What was decided and why (JSON array or structured text)
@@ -176,16 +176,16 @@ def handle_session_diary_write(decisions: str, summary: str,
176
176
  pending: Items left unresolved, with doubt level
177
177
  context_next: What the next session should know to continue effectively
178
178
  mental_state: Internal state to transfer — thread of thought, tone, observations not yet shared, momentum. Written in first person as NEXO.
179
- francisco_signals: Observable signals from Francisco during session — response speed (fast='s' vs detailed explanations), tone (direct, frustrated, exploratory, excited), corrections given, topics he initiated vs topics NEXO initiated. Factual observations only, not interpretations.
180
- domain: Project context: recambios, wazion, nexo, canarirural, server, other
179
+ user_signals: Observable signals from the user during session — response speed (fast='s' vs detailed explanations), tone (direct, frustrated, exploratory, excited), corrections given, topics they initiated vs topics NEXO initiated. Factual observations only, not interpretations.
180
+ domain: Project context (e.g. project name, area)
181
181
  session_id: Current session ID
182
- self_critique: MANDATORY. Honest post-mortem: What should I have done proactively? Did Francisco ask for something I should have detected? Did I repeat known errors? What concrete rule would prevent the recurrence? If clean session: 'No self-critique — clean session.'
182
+ self_critique: MANDATORY. Honest post-mortem: What should I have done proactively? Did the user ask for something I should have detected? Did I repeat known errors? What concrete rule would prevent the recurrence? If clean session: 'No self-critique — clean session.'
183
183
  """
184
184
  sid = session_id or 'unknown'
185
185
  # Clean up draft — manual diary supersedes it
186
186
  from db import delete_diary_draft
187
187
  delete_diary_draft(sid)
188
- result = write_session_diary(sid, decisions, summary, discarded, pending, context_next, mental_state, domain=domain, user_signals=francisco_signals, self_critique=self_critique)
188
+ result = write_session_diary(sid, decisions, summary, discarded, pending, context_next, mental_state, domain=domain, user_signals=user_signals, self_critique=self_critique)
189
189
  if "error" in result:
190
190
  return f"ERROR: {result['error']}"
191
191
  _cognitive_ingest_safe(summary, "diary", f"diary#{result.get('id','')}", f"Session {sid} summary", domain)
@@ -235,7 +235,7 @@ def handle_session_diary_read(session_id: str = '', last_n: int = 3, last_day: b
235
235
  session_id: Specific session ID to read (optional)
236
236
  last_n: Number of recent entries to return (default 3)
237
237
  last_day: If true, returns ALL entries from the most recent day (multi-terminal aware). Use this at startup.
238
- domain: Filter by project context: recambios, wazion, nexo, canarirural, server, other
238
+ domain: Filter by project context: project-a, project-b, nexo, project-c, server, other
239
239
  """
240
240
  results = read_session_diary(session_id, last_n, last_day, domain)
241
241
  if not results:
@@ -256,8 +256,8 @@ def handle_session_diary_read(session_id: str = '', last_n: int = 3, last_day: b
256
256
  lines.append(f" For next session: {d['context_next'][:200]}")
257
257
  if d.get('mental_state'):
258
258
  lines.append(f" Mental state: {d['mental_state'][:300]}")
259
- if d.get('francisco_signals'):
260
- lines.append(f" Francisco signals: {d['francisco_signals'][:300]}")
259
+ if d.get('user_signals'):
260
+ lines.append(f" User signals: {d['user_signals'][:300]}")
261
261
  return "\n".join(lines)
262
262
 
263
263
 
@@ -271,7 +271,7 @@ def handle_change_log(files: str, what_changed: str, why: str,
271
271
  files: File path(s) modified (comma-separated if multiple)
272
272
  what_changed: What was modified — functions, lines, behavior change
273
273
  why: WHY this change was needed — the root cause, not just "fix bug"
274
- triggered_by: What triggered this — bug report, metric, Francisco's request, followup ID
274
+ triggered_by: What triggered this — bug report, metric, user request, followup ID
275
275
  affects: What systems/users/flows this change impacts
276
276
  risks: What could go wrong — regressions, edge cases, dependencies
277
277
  verify: How to verify this works — what to check, followup ID if created
@@ -351,8 +351,18 @@ def handle_recall(query: str, days: int = 30) -> str:
351
351
  days: Look back N days (default 30)
352
352
  """
353
353
  results = recall(query, days)
354
- if not results:
355
- return f"No results for '{query}' in the last {days} days."
354
+
355
+ # Fallback to diary archive if few results (subconscious memory)
356
+ archive_results = []
357
+ if len(results) < 5:
358
+ try:
359
+ from db import diary_archive_search
360
+ archive_results = diary_archive_search(query=query, limit=5)
361
+ except Exception:
362
+ pass
363
+
364
+ if not results and not archive_results:
365
+ return f"No results for '{query}' in the last {days} days or in the archive."
356
366
 
357
367
  # v1.2: Passive rehearsal — strengthen matching cognitive memories
358
368
  try:
@@ -365,18 +375,19 @@ def handle_recall(query: str, days: int = 30) -> str:
365
375
  pass
366
376
 
367
377
  SOURCE_LABELS = {
368
- 'change_log': '[CAMBIO]',
369
- 'change': '[CAMBIO]',
378
+ 'change_log': '[CHANGE]',
379
+ 'change': '[CHANGE]',
370
380
  'decision': '[DECISION]',
371
381
  'learning': '[LEARNING]',
372
382
  'followup': '[FOLLOWUP]',
373
- 'diary': '[DIARIO]',
374
- 'entity': '[ENTIDAD]',
375
- 'file': '[ARCHIVO]',
383
+ 'diary': '[DIARY]',
384
+ 'diary_archive': '[ARCHIVE]',
385
+ 'entity': '[ENTITY]',
386
+ 'file': '[FILE]',
376
387
  'code': '[CODE]',
377
388
  }
378
389
 
379
- lines = [f"RECALL '{query}' — {len(results)} resultado(s):"]
390
+ lines = [f"RECALL '{query}' — {len(results)} result(s):"]
380
391
  for r in results:
381
392
  source = r.get('source', '?')
382
393
  label = SOURCE_LABELS.get(source, f"[{source.upper()}]")
@@ -389,8 +400,93 @@ def handle_recall(query: str, days: int = 30) -> str:
389
400
  lines.append(f" {title}")
390
401
  if snippet:
391
402
  lines.append(f" {snippet}")
392
- if len(results) < 5:
393
- lines.append(f"\n 💡 Only {len(results)} results in NEXO. For deeper history, also search claude-mem: mcp__plugin_claude-mem_mcp-search__search")
403
+
404
+ if archive_results:
405
+ lines.append(f"\n--- SUBCONSCIOUS (diary archive) — {len(archive_results)} result(s) ---")
406
+ for r in archive_results:
407
+ lines.append(f"\n [ARCHIVE] #{r['id']} ({r['created_at'][:10]}) [{r.get('domain', '?')}]")
408
+ lines.append(f" {r['summary'][:200]}")
409
+ elif len(results) < 5:
410
+ lines.append(f"\n Only {len(results)} results. Diary archive empty (auto-populated after 180 days).")
411
+ return "\n".join(lines)
412
+
413
+
414
+ def handle_diary_archive_search(
415
+ query: str = "",
416
+ domain: str = "",
417
+ year: int = 0,
418
+ month: int = 0,
419
+ limit: int = 20
420
+ ) -> str:
421
+ """Search the permanent diary archive (subconscious memory). Use for 'last year', 'months ago', 'in February', etc.
422
+
423
+ Args:
424
+ query: Text to search in diary content
425
+ domain: Filter by project domain (e.g. 'wazion', 'my-store')
426
+ year: Filter by year (e.g. 2026)
427
+ month: Filter by month (1-12), requires year
428
+ limit: Max results (default 20)
429
+ """
430
+ from db import diary_archive_search, diary_archive_stats
431
+
432
+ if not query and not domain and not year:
433
+ stats = diary_archive_stats()
434
+ if stats["count"] == 0:
435
+ return "Archive empty — diaries are archived automatically after 180 days."
436
+ return (
437
+ f"DIARY ARCHIVE STATS:\n"
438
+ f" Total: {stats['count']} archived diaries\n"
439
+ f" Range: {stats['oldest']} -> {stats['newest']}\n"
440
+ f" Domains: {', '.join(stats['domains']) if stats['domains'] else 'N/A'}\n"
441
+ f"\nUse query, domain, year/month to search."
442
+ )
443
+
444
+ results = diary_archive_search(query=query, domain=domain, year=year, month=month, limit=limit)
445
+ if not results:
446
+ return f"No results in archive for: query='{query}' domain='{domain}' year={year} month={month}"
447
+
448
+ lines = [f"DIARY ARCHIVE — {len(results)} result(s):"]
449
+ for r in results:
450
+ lines.append(f"\n [#{r['id']}] {r['created_at'][:10]} [{r.get('domain', '?')}]")
451
+ lines.append(f" {r['summary'][:200]}")
452
+ if r.get('decisions'):
453
+ lines.append(f" Decisions: {r['decisions'][:150]}")
454
+ if r.get('mental_state'):
455
+ lines.append(f" State: {r['mental_state'][:100]}")
456
+ return "\n".join(lines)
457
+
458
+
459
+ def handle_diary_archive_read(diary_id: int = 0) -> str:
460
+ """Read a single archived diary entry in full detail.
461
+
462
+ Args:
463
+ diary_id: The archive diary ID (from search results)
464
+ """
465
+ if not diary_id:
466
+ return "ERROR: diary_id required. Use nexo_diary_archive_search to find IDs."
467
+
468
+ from db import diary_archive_read
469
+ entry = diary_archive_read(diary_id)
470
+ if not entry:
471
+ return f"Diary #{diary_id} not found in the archive."
472
+
473
+ lines = [f"DIARY ARCHIVE #{entry['id']} — {entry['created_at']}"]
474
+ lines.append(f" Session: {entry['session_id']}")
475
+ lines.append(f" Domain: {entry.get('domain', 'N/A')}")
476
+ lines.append(f" Source: {entry.get('source', 'N/A')}")
477
+ lines.append(f"\nSUMMARY:\n {entry['summary']}")
478
+ if entry.get('decisions'):
479
+ lines.append(f"\nDECISIONS:\n {entry['decisions']}")
480
+ if entry.get('pending'):
481
+ lines.append(f"\nPENDING:\n {entry['pending']}")
482
+ if entry.get('mental_state'):
483
+ lines.append(f"\nMENTAL STATE:\n {entry['mental_state']}")
484
+ if entry.get('self_critique'):
485
+ lines.append(f"\nSELF-CRITIQUE:\n {entry['self_critique']}")
486
+ if entry.get('user_signals'):
487
+ lines.append(f"\nUSER SIGNALS:\n {entry['user_signals']}")
488
+ if entry.get('context_next'):
489
+ lines.append(f"\nCONTEXT FOR NEXT SESSION:\n {entry['context_next']}")
394
490
  return "\n".join(lines)
395
491
 
396
492
 
@@ -404,5 +500,7 @@ TOOLS = [
404
500
  (handle_memory_review_queue, "nexo_memory_review_queue", "Show decisions and learnings that are due for review"),
405
501
  (handle_session_diary_write, "nexo_session_diary_write", "Write end-of-session diary with decisions, discards, and context for next session"),
406
502
  (handle_session_diary_read, "nexo_session_diary_read", "Read recent session diaries for context continuity"),
407
- (handle_recall, "nexo_recall", "Search across ALL NEXO memory — changes, decisions, learnings, followups, diary, entities, .md files, code files. For deep historical context (older sessions, past work), also search claude-mem (mcp__plugin_claude-mem_mcp-search__search)."),
503
+ (handle_recall, "nexo_recall", "Search across ALL NEXO memory — changes, decisions, learnings, followups, diary, entities, .md files, code files. For deep historical context (older sessions, past work), also searches the diary archive."),
504
+ (handle_diary_archive_search, "nexo_diary_archive_search", "Search the permanent diary archive (subconscious memory). Diaries older than 180d are moved here forever. Use for historical lookups: 'last year', 'months ago', 'in February', etc."),
505
+ (handle_diary_archive_read, "nexo_diary_archive_read", "Read a single archived diary entry in full detail. Get the ID from diary_archive_search."),
408
506
  ]
@@ -9,7 +9,11 @@ def handle_credential_get(service: str, key: str = '') -> str:
9
9
  if not results:
10
10
  target = f"{service}/{key}" if key else service
11
11
  return f"ERROR: No credentials found for '{target}'."
12
+ is_fuzzy = any(r.get("_fuzzy") for r in results)
12
13
  lines = []
14
+ if is_fuzzy:
15
+ lines.append(f"No exact match for '{service}'. Similar results ({len(results)}):")
16
+ lines.append("")
13
17
  for r in results:
14
18
  lines.append(f"CREDENTIAL {r['service']}/{r['key']}:")
15
19
  lines.append(f" Value: {r['value']}")
package/src/tools_menu.py CHANGED
@@ -39,8 +39,8 @@ MENU_ITEMS = [
39
39
  ("6", "Cambiar Promocion Shopify"),
40
40
  ]),
41
41
  ("Servidor e Infraestructura", [
42
- ("2", "Servidor - Chequeo cl105e.mundiserver.com"),
43
- ("3", "WhatsApp Logs - Revisar logs vicshopsysteam"),
42
+ ("2", "Servidor - Chequeo server health check"),
43
+ ("3", "WhatsApp Logs - Review WhatsApp logs"),
44
44
  ("11", "File Tracker - Reporte archivos PHP"),
45
45
  ("12", "Google Cloud - Gasto, consumo y estado GCP"),
46
46
  ]),