nexo-brain 0.9.0 → 0.10.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,14 @@
1
1
  # NEXO Brain — Your AI Gets a Brain
2
2
 
3
- [![npm v0.8.10](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.10** — Knowledge Graph (988 bi-temporal nodes, D3 visualization), Web Dashboard (6 pages at localhost:6174), Cross-Platform support (Linux + Windows + WSL), Session keepalive, PEP 668 compliance, full English translation, 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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "0.9.0",
3
+ "version": "0.10.0-beta.2",
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", "whatsapp", "chrome", "firefox", "slack", "notion", "github",
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
+ "cloudrun", "gcloud", "aws", "azure", "vps", "local", "production", "staging",
40
40
  # DB
41
41
  "mysql", "sqlite", "postgresql", "postgres", "redis",
42
42
  }
@@ -65,12 +65,12 @@ URGENCY_SIGNALS = {
65
65
  _DEFAULT_TRUST_EVENTS = {
66
66
  # Positive
67
67
  "explicit_thanks": +3,
68
- "delegation": +2, # Francisco delegates new task without micromanaging
69
- "paradigm_shift": +2, # Francisco teaches, NEXO learns
68
+ "delegation": +2, # User delegates new task without micromanaging
69
+ "paradigm_shift": +2, # User teaches, agent learns
70
70
  "sibling_detected": +3, # NEXO avoided context error on its own
71
71
  "proactive_action": +2, # NEXO did something useful without being asked
72
72
  # Negative
73
- "correction": -3, # Francisco corrects NEXO
73
+ "correction": -3, # User corrects agent
74
74
  "repeated_error": -7, # Error on something NEXO already had a learning for
75
75
  "override": -5, # NEXO's memory was wrong
76
76
  "correction_fatigue": -10, # Same memory corrected 3+ times
@@ -395,7 +395,7 @@ def _init_tables(conn: sqlite3.Connection):
395
395
  created_at TEXT DEFAULT (datetime('now'))
396
396
  );
397
397
 
398
- -- Sentiment readings: Francisco's detected mood per interaction
398
+ -- Sentiment readings: User.s detected mood per interaction
399
399
  CREATE TABLE IF NOT EXISTS sentiment_log (
400
400
  id INTEGER PRIMARY KEY AUTOINCREMENT,
401
401
  sentiment TEXT NOT NULL, -- 'positive', 'negative', 'neutral', 'urgent'
@@ -421,13 +421,13 @@ def _init_tables(conn: sqlite3.Connection):
421
421
  status TEXT DEFAULT 'pending'
422
422
  );
423
423
 
424
- -- Correction tracking: when Francisco overrides a memory's guidance
424
+ -- Correction tracking: when user overrides a memory's guidance
425
425
  CREATE TABLE IF NOT EXISTS memory_corrections (
426
426
  id INTEGER PRIMARY KEY AUTOINCREMENT,
427
427
  memory_id INTEGER NOT NULL,
428
428
  store TEXT NOT NULL, -- 'stm' or 'ltm'
429
429
  correction_type TEXT NOT NULL, -- 'override', 'exception', 'paradigm_shift'
430
- context TEXT DEFAULT '', -- what Francisco said
430
+ context TEXT DEFAULT '', -- what user said
431
431
  created_at TEXT DEFAULT (datetime('now'))
432
432
  );
433
433
  """)
@@ -732,35 +732,140 @@ def bm25_search(query_text: str, stores: str = "both", top_k: int = 20,
732
732
 
733
733
  def _rrf_fuse(vector_results: list[dict], bm25_results: list[dict],
734
734
  k: int = 60, alpha: float = 0.7) -> list[dict]:
735
- """Reciprocal Rank Fusion: boost vector results with BM25 keyword matches.
735
+ """Reciprocal Rank Fusion: merge vector and BM25 results.
736
736
 
737
- BM25 only BOOSTS existing vector results never adds new ones.
738
- This preserves vector search recall while improving precision for keyword-heavy queries.
737
+ Unlike the old version that only boosted vector-found results, this now
738
+ ALSO ADDS BM25-only results. This is critical for vocabulary mismatches
739
+ where semantic search misses but keyword search finds the right memory
740
+ (e.g., user says 'backend', memory contains 'FastAPI dashboard localhost:6174').
739
741
 
740
- RRF score = vector_score + (1-alpha) * 1/(k + bm25_rank) for items found by both.
741
- Items only in vector results keep their original score.
742
+ RRF score = alpha * 1/(k + vec_rank) + (1-alpha) * 1/(k + bm25_rank)
743
+ Items found by only one source get a penalty rank for the missing source.
742
744
  """
743
- # Build BM25 lookup by (store, id)
745
+ # Build lookups by (store, id)
746
+ vec_lookup = {}
747
+ for rank, r in enumerate(vector_results):
748
+ key = (r["store"], r["id"])
749
+ vec_lookup[key] = (rank + 1, r)
750
+
744
751
  bm25_lookup = {}
745
752
  for rank, r in enumerate(bm25_results):
746
753
  key = (r["store"], r["id"])
747
- bm25_lookup[key] = rank + 1 # 1-based rank
754
+ if key not in bm25_lookup: # keep best rank
755
+ bm25_lookup[key] = (rank + 1, r)
756
+
757
+ # Merge all unique keys
758
+ all_keys = set(vec_lookup.keys()) | set(bm25_lookup.keys())
759
+ miss_rank = max(len(vector_results), len(bm25_results)) + 10 # penalty rank for missing source
748
760
 
749
- # Boost vector results that also appear in BM25
750
761
  fused = []
751
- for r in vector_results:
752
- result = r.copy()
753
- key = (r["store"], r["id"])
754
- if key in bm25_lookup:
755
- bm25_rank = bm25_lookup[key]
756
- boost = (1 - alpha) * (1.0 / (k + bm25_rank))
757
- result["score"] = r["score"] + boost
758
- result["bm25_boosted"] = True
762
+ for key in all_keys:
763
+ vec_rank, vec_result = vec_lookup.get(key, (miss_rank, None))
764
+ bm25_rank, bm25_result = bm25_lookup.get(key, (miss_rank, None))
765
+
766
+ # Use whichever result has the data
767
+ base = vec_result if vec_result else bm25_result
768
+ result = base.copy()
769
+
770
+ rrf_score = alpha * (1.0 / (k + vec_rank)) + (1 - alpha) * (1.0 / (k + bm25_rank))
771
+
772
+ # If we have the original cosine score, blend it in to preserve semantic confidence
773
+ if vec_result and "score" in vec_result:
774
+ # Weighted blend: RRF for ranking + cosine for confidence
775
+ result["score"] = 0.6 * vec_result["score"] + 0.4 * (rrf_score * k * 3)
776
+ else:
777
+ # BM25-only result: use RRF score scaled to ~0.5-0.7 range
778
+ result["score"] = min(0.85, rrf_score * k * 3)
779
+
780
+ result["bm25_boosted"] = key in bm25_lookup
781
+ result["bm25_only"] = key not in vec_lookup
782
+ result["rrf_score"] = rrf_score
759
783
  fused.append(result)
760
784
 
785
+ # Sort by score descending
786
+ fused.sort(key=lambda x: x["score"], reverse=True)
761
787
  return fused
762
788
 
763
789
 
790
+ # ── Temporal Boosting ────────────────────────────────────────────────
791
+ # Recent memories get a bounded additive boost at query time.
792
+ # Design from multi-AI debate (GPT-5.4 + Gemini 3.1 Pro + Claude Opus 4.6):
793
+ # - Additive, not multiplicative (preserves old strong matches)
794
+ # - Relevance-gated (only boost if already above threshold)
795
+ # - Query-adaptive alpha (operational queries get more boost)
796
+
797
+ # Operational keywords that suggest the user wants recent/active things
798
+ _OPERATIONAL_CUES = frozenset({
799
+ "current", "latest", "now", "running", "active", "today", "yesterday",
800
+ "tonight", "backend", "server", "dashboard", "service", "localhost",
801
+ "anoche", "ayer", "ahora", "actual", "corriendo", "activo", "hoy",
802
+ "madrugada", "esta mañana", "last night", "this morning",
803
+ })
804
+
805
+ # Historical keywords that suggest the user wants old things
806
+ _HISTORICAL_CUES = frozenset({
807
+ "ago", "month", "months", "year", "years", "previous", "earlier",
808
+ "cuando", "hace", "meses", "año", "anterior", "antes",
809
+ })
810
+
811
+
812
+ def _apply_temporal_boost(results: list[dict], query_text: str) -> list[dict]:
813
+ """Apply bounded temporal boost to retrieval results.
814
+
815
+ Recent memories (hours/days) get a small additive bonus, but only if they
816
+ already have a reasonable relevance score (gated at 0.45). This prevents
817
+ recent junk from outranking strong old matches.
818
+
819
+ The boost decays with a 3-day half-life:
820
+ boost = alpha * exp(-ln(2) * age_days / 3)
821
+
822
+ Alpha is query-adaptive:
823
+ - Operational queries ('backend', 'active', 'today'): alpha = 0.06
824
+ - Default queries: alpha = 0.02
825
+ - Historical queries ('ago', 'months', 'year'): alpha = 0.0 (disabled)
826
+ """
827
+ if not results:
828
+ return results
829
+
830
+ # Determine alpha based on query intent
831
+ query_tokens = set(query_text.lower().split())
832
+ if query_tokens & _HISTORICAL_CUES:
833
+ return results # No temporal boost for historical queries
834
+ elif query_tokens & _OPERATIONAL_CUES:
835
+ alpha = 0.06
836
+ else:
837
+ alpha = 0.02
838
+
839
+ now = datetime.now()
840
+ ln2 = math.log(2)
841
+ half_life_days = 3.0
842
+
843
+ for r in results:
844
+ # Only boost if already reasonably relevant (relevance gate)
845
+ if r.get("score", 0) < 0.45:
846
+ continue
847
+
848
+ # Calculate age in days
849
+ created_str = r.get("created_at", "")
850
+ if not created_str:
851
+ continue
852
+ try:
853
+ created = datetime.fromisoformat(created_str.replace("Z", "+00:00").replace("+00:00", ""))
854
+ age_days = max(0, (now - created).total_seconds() / 86400)
855
+ except (ValueError, TypeError):
856
+ continue
857
+
858
+ # Bounded exponential decay boost
859
+ boost = alpha * math.exp(-ln2 * age_days / half_life_days)
860
+
861
+ # Apply boost (capped at 1.0)
862
+ r["score"] = min(1.0, r["score"] + boost)
863
+ if boost > 0.001:
864
+ r["temporal_boost"] = round(boost, 4)
865
+
866
+ return results
867
+
868
+
764
869
 
765
870
  # ============================================================================
766
871
  # FEATURE 1: HyDE Query Expansion (adapted from Vestige hyde.rs)
@@ -1283,6 +1388,9 @@ def search(
1283
1388
  if r.get("temporal_date"):
1284
1389
  r["score"] = min(1.0, r["score"] + 0.05)
1285
1390
 
1391
+ # Recency temporal boost: recent memories get additive bonus (query-adaptive)
1392
+ results = _apply_temporal_boost(results, query_text)
1393
+
1286
1394
  # Sort by score descending, take top-20 for reranking
1287
1395
  results.sort(key=lambda x: x.get("score", 0), reverse=True)
1288
1396
 
@@ -2544,12 +2652,12 @@ def get_siblings(memory_id: int) -> list[dict]:
2544
2652
  def detect_dissonance(new_instruction: str, min_score: float = 0.65) -> list[dict]:
2545
2653
  """Detect cognitive dissonance: find LTM memories that contradict a new instruction.
2546
2654
 
2547
- When Francisco gives a new instruction that conflicts with established LTM memories
2655
+ When User gives a new instruction that conflicts with established LTM memories
2548
2656
  (strength > 0.8), this function surfaces the conflict so NEXO can verbalize it
2549
2657
  rather than silently obeying or silently resisting.
2550
2658
 
2551
2659
  Args:
2552
- new_instruction: The new instruction or preference from Francisco
2660
+ new_instruction: The new instruction or preference from user
2553
2661
  min_score: Minimum cosine similarity to consider as potential conflict
2554
2662
 
2555
2663
  Returns:
@@ -2584,12 +2692,12 @@ def detect_dissonance(new_instruction: str, min_score: float = 0.65) -> list[dic
2584
2692
 
2585
2693
 
2586
2694
  def resolve_dissonance(memory_id: int, resolution: str, context: str = "") -> str:
2587
- """Resolve a cognitive dissonance by applying Francisco's decision.
2695
+ """Resolve a cognitive dissonance by applying the user.s decision.
2588
2696
 
2589
2697
  Args:
2590
2698
  memory_id: The LTM memory that conflicts with the new instruction
2591
2699
  resolution: One of:
2592
- - 'paradigm_shift': Francisco changed his mind permanently. Decay old memory,
2700
+ - 'paradigm_shift': User changed their mind permanently. Decay old memory,
2593
2701
  new instruction becomes the standard.
2594
2702
  - 'exception': This is a one-time override. Keep old memory as standard.
2595
2703
  - 'override': Old memory was wrong. Mark as corrupted and decay to dormant.
@@ -2640,7 +2748,7 @@ def resolve_dissonance(memory_id: int, resolution: str, context: str = "") -> st
2640
2748
  def check_correction_fatigue() -> list[dict]:
2641
2749
  """Find memories corrected 3+ times in the last 7 days — mark as 'under review'.
2642
2750
 
2643
- These memories are unreliable: Francisco keeps overriding them, suggesting
2751
+ These memories are unreliable: User keeps overriding them, suggesting
2644
2752
  the memory itself may be wrong or outdated.
2645
2753
 
2646
2754
  Returns:
@@ -2688,7 +2796,7 @@ def check_correction_fatigue() -> list[dict]:
2688
2796
 
2689
2797
 
2690
2798
  def detect_sentiment(text: str) -> dict:
2691
- """Analyze Francisco's text for sentiment signals.
2799
+ """Analyze user's text for sentiment signals.
2692
2800
 
2693
2801
  Returns detected sentiment, intensity, and action guidance for NEXO.
2694
2802
  Not a model — keyword + heuristic based. Fast and deterministic.
@@ -2727,17 +2835,17 @@ def detect_sentiment(text: str) -> dict:
2727
2835
  sentiment = "negative"
2728
2836
  intensity = min(1.0, 0.3 + neg_score * 0.15)
2729
2837
  if intensity > 0.7:
2730
- guidance = "MODE: Ultra-concise. Zero explanations. Resolve and show result."
2838
+ guidance = "MODE: Ultra-conciso. Cero explicaciones. Resolver y mostrar resultado."
2731
2839
  else:
2732
- guidance = "MODE: Concise. Less context, more direct action."
2840
+ guidance = "MODE: Conciso. Menos contexto, más acción directa."
2733
2841
  elif pos_score > neg_score and pos_score >= 1:
2734
2842
  sentiment = "positive"
2735
2843
  intensity = min(1.0, 0.3 + pos_score * 0.15)
2736
- guidance = "MODE: Normal. Good moment to propose backlog ideas or improvements."
2844
+ guidance = "MODE: Normal. Buen momento para proponer ideas de backlog o mejoras."
2737
2845
  elif urgency_hits:
2738
2846
  sentiment = "urgent"
2739
2847
  intensity = 0.8
2740
- guidance = "MODE: Immediate action. No preamble."
2848
+ guidance = "MODE: Acción inmediata. Sin preámbulos."
2741
2849
  else:
2742
2850
  sentiment = "neutral"
2743
2851
  intensity = 0.5
@@ -2752,7 +2860,7 @@ def detect_sentiment(text: str) -> dict:
2752
2860
 
2753
2861
 
2754
2862
  def log_sentiment(text: str) -> dict:
2755
- """Detect and log Francisco's sentiment. Returns the detection result."""
2863
+ """Detect and log user's sentiment. Returns the detection result."""
2756
2864
  result = detect_sentiment(text)
2757
2865
  if result["sentiment"] != "neutral":
2758
2866
  db = _get_db()
package/src/db.py CHANGED
@@ -928,6 +928,55 @@ def _m10_diary_archive(conn):
928
928
  """)
929
929
 
930
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
+
931
980
  def _m9_maintenance_schedule(conn):
932
981
  conn.execute("""
933
982
  CREATE TABLE IF NOT EXISTS maintenance_schedule (
@@ -962,6 +1011,7 @@ MIGRATIONS = [
962
1011
  (8, "adaptive_log_and_somatic", _m8_adaptive_log_and_somatic),
963
1012
  (9, "maintenance_schedule", _m9_maintenance_schedule),
964
1013
  (10, "diary_archive", _m10_diary_archive),
1014
+ (11, "artifact_registry", _m11_artifact_registry),
965
1015
  ]
966
1016
 
967
1017
 
@@ -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)
@@ -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:
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
  ]),