nexo-brain 0.10.0-beta.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/cognitive.py +143 -35
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "0.10.0-beta.1",
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", "project-a", "project-b", "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
- "server", "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()