nexo-brain 0.10.0-beta.3 → 0.10.0-beta.4
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 +2 -2
- package/package.json +1 -1
- package/src/cognitive.py +221 -53
- package/src/scripts/nexo-cognitive-decay.py +23 -14
- package/src/__pycache__/db.cpython-314.pyc +0 -0
- package/src/__pycache__/tools_credentials.cpython-314.pyc +0 -0
- package/src/dashboard/__pycache__/__init__.cpython-314.pyc +0 -0
- package/src/dashboard/__pycache__/app.cpython-314.pyc +0 -0
- package/src/plugins/__pycache__/episodic_memory.cpython-314.pyc +0 -0
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# NEXO Brain — Your AI Gets a Brain
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/nexo-brain)
|
|
4
4
|
[](https://github.com/wazionapps/nexo/blob/main/benchmarks/locomo/results/)
|
|
5
5
|
[](https://github.com/snap-research/locomo/issues/33)
|
|
6
6
|
[](https://github.com/wazionapps/nexo/stargazers)
|
|
7
7
|
[](https://opensource.org/licenses/MIT)
|
|
8
8
|
|
|
9
|
-
> **v0.
|
|
9
|
+
> **v0.10.0-beta.4** — Cognitive memory pipeline hardened: quarantine automation, embedding migration fix, smarter STM→LTM promotion, 21% noise reduction from dream insights, zero-decay pinning for corrections, and extended GC windows.
|
|
10
10
|
|
|
11
11
|
**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
12
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "0.10.0-beta.
|
|
3
|
+
"version": "0.10.0-beta.4",
|
|
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", "
|
|
33
|
+
"shopify", "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
|
-
"
|
|
39
|
+
"cloudrun", "gcloud", "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, #
|
|
69
|
-
"paradigm_shift": +2, #
|
|
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, #
|
|
73
|
+
"correction": -3, # user corrects the 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
|
|
@@ -303,7 +303,7 @@ def _auto_migrate_embeddings(conn: sqlite3.Connection):
|
|
|
303
303
|
# Need migration: 384 → 768
|
|
304
304
|
model = _get_model()
|
|
305
305
|
|
|
306
|
-
for table in ("stm_memories", "ltm_memories"):
|
|
306
|
+
for table in ("stm_memories", "ltm_memories", "quarantine"):
|
|
307
307
|
rows = conn.execute(f"SELECT id, content FROM {table}").fetchall()
|
|
308
308
|
if not rows:
|
|
309
309
|
continue
|
|
@@ -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:
|
|
398
|
+
-- Sentiment readings: the 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
|
|
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
|
|
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:
|
|
735
|
+
"""Reciprocal Rank Fusion: merge vector and BM25 results.
|
|
736
736
|
|
|
737
|
-
|
|
738
|
-
|
|
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 =
|
|
741
|
-
Items only
|
|
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
|
|
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
|
-
|
|
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
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
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)
|
|
@@ -1106,12 +1211,16 @@ def search(
|
|
|
1106
1211
|
hybrid_alpha: float = 0.6,
|
|
1107
1212
|
spreading_depth: int = 0,
|
|
1108
1213
|
decompose: bool = True,
|
|
1214
|
+
exclude_dreams: bool = True,
|
|
1109
1215
|
) -> list[dict]:
|
|
1110
1216
|
"""Full vector search across STM and/or LTM with rehearsal and dormant reactivation.
|
|
1111
1217
|
|
|
1112
1218
|
Args:
|
|
1113
1219
|
use_hyde: If True, use HyDE query expansion for richer embedding (default False)
|
|
1114
1220
|
spreading_depth: If >0, fetch co-activated neighbors and boost their scores (default 0)
|
|
1221
|
+
exclude_dreams: If True (default), exclude dream_insight memories from results.
|
|
1222
|
+
Dream insights are 21% of LTM and dilute search precision.
|
|
1223
|
+
Set to False only when explicitly looking for cross-domain patterns.
|
|
1115
1224
|
hybrid: If True, boost results with BM25 keyword matches (default True)
|
|
1116
1225
|
hybrid_alpha: Weight for vector vs BM25. Higher = more vector. (default 0.6)
|
|
1117
1226
|
decompose: If True, decompose complex queries into sub-queries for better multi-hop (default True)
|
|
@@ -1214,6 +1323,8 @@ def search(
|
|
|
1214
1323
|
if source_type_filter:
|
|
1215
1324
|
where += " AND source_type = ?"
|
|
1216
1325
|
params.append(source_type_filter)
|
|
1326
|
+
if exclude_dreams and not source_type_filter:
|
|
1327
|
+
where += " AND source_type != 'dream_insight'"
|
|
1217
1328
|
rows = db.execute(f"SELECT * FROM ltm_memories {where}", params).fetchall()
|
|
1218
1329
|
|
|
1219
1330
|
for row in rows:
|
|
@@ -1283,6 +1394,9 @@ def search(
|
|
|
1283
1394
|
if r.get("temporal_date"):
|
|
1284
1395
|
r["score"] = min(1.0, r["score"] + 0.05)
|
|
1285
1396
|
|
|
1397
|
+
# Recency temporal boost: recent memories get additive bonus (query-adaptive)
|
|
1398
|
+
results = _apply_temporal_boost(results, query_text)
|
|
1399
|
+
|
|
1286
1400
|
# Sort by score descending, take top-20 for reranking
|
|
1287
1401
|
results.sort(key=lambda x: x.get("score", 0), reverse=True)
|
|
1288
1402
|
|
|
@@ -1395,7 +1509,8 @@ def ingest(
|
|
|
1395
1509
|
source: str = "inferred",
|
|
1396
1510
|
skip_quarantine: bool = False,
|
|
1397
1511
|
bypass_gate: bool = False,
|
|
1398
|
-
bypass_security: bool = False
|
|
1512
|
+
bypass_security: bool = False,
|
|
1513
|
+
auto_pin: bool = False,
|
|
1399
1514
|
) -> int:
|
|
1400
1515
|
"""Embed and store content. Routes through quarantine unless skip_quarantine=True or source='user_direct'.
|
|
1401
1516
|
|
|
@@ -1443,6 +1558,13 @@ def ingest(
|
|
|
1443
1558
|
blob = _array_to_blob(vec)
|
|
1444
1559
|
temporal = extract_temporal_date(content)
|
|
1445
1560
|
|
|
1561
|
+
# Auto-pin: corrections and blocking learnings get pinned (zero decay, +0.2 boost)
|
|
1562
|
+
# This ensures user corrections NEVER fade away
|
|
1563
|
+
_pin_lifecycle = None
|
|
1564
|
+
if auto_pin or (source_type in ('learning', 'feedback') and
|
|
1565
|
+
any(kw in content.upper() for kw in ('BLOCKING', 'CRÍTICO', 'CRITICAL', 'NUNCA', 'NEVER', 'PROHIBIDO'))):
|
|
1566
|
+
_pin_lifecycle = 'pinned'
|
|
1567
|
+
|
|
1446
1568
|
# user_direct = fast-track: quarantine then immediate promote
|
|
1447
1569
|
if source == "user_direct" and not skip_quarantine:
|
|
1448
1570
|
cur = db.execute(
|
|
@@ -1453,9 +1575,9 @@ def ingest(
|
|
|
1453
1575
|
db.commit()
|
|
1454
1576
|
# Now actually store in STM
|
|
1455
1577
|
cur2 = db.execute(
|
|
1456
|
-
"""INSERT INTO stm_memories (content, embedding, source_type, source_id, source_title, domain, redaction_applied, temporal_date)
|
|
1457
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
1458
|
-
(clean_content, blob, source_type, source_id, source_title, domain, was_redacted, temporal)
|
|
1578
|
+
"""INSERT INTO stm_memories (content, embedding, source_type, source_id, source_title, domain, redaction_applied, temporal_date, lifecycle_state)
|
|
1579
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
1580
|
+
(clean_content, blob, source_type, source_id, source_title, domain, was_redacted, temporal, _pin_lifecycle)
|
|
1459
1581
|
)
|
|
1460
1582
|
db.commit()
|
|
1461
1583
|
return cur2.lastrowid
|
|
@@ -1463,9 +1585,9 @@ def ingest(
|
|
|
1463
1585
|
# skip_quarantine = direct STM (backward compatibility)
|
|
1464
1586
|
if skip_quarantine:
|
|
1465
1587
|
cur = db.execute(
|
|
1466
|
-
"""INSERT INTO stm_memories (content, embedding, source_type, source_id, source_title, domain, redaction_applied, temporal_date)
|
|
1467
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
1468
|
-
(clean_content, blob, source_type, source_id, source_title, domain, was_redacted, temporal)
|
|
1588
|
+
"""INSERT INTO stm_memories (content, embedding, source_type, source_id, source_title, domain, redaction_applied, temporal_date, lifecycle_state)
|
|
1589
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
1590
|
+
(clean_content, blob, source_type, source_id, source_title, domain, was_redacted, temporal, _pin_lifecycle)
|
|
1469
1591
|
)
|
|
1470
1592
|
db.commit()
|
|
1471
1593
|
return cur.lastrowid
|
|
@@ -1669,10 +1791,29 @@ def apply_decay(adaptive: bool = True):
|
|
|
1669
1791
|
|
|
1670
1792
|
|
|
1671
1793
|
def promote_stm_to_ltm():
|
|
1672
|
-
"""Promote STM memories
|
|
1794
|
+
"""Promote STM memories to LTM based on multiple criteria.
|
|
1795
|
+
|
|
1796
|
+
Promotion rules (any one is sufficient):
|
|
1797
|
+
1. access_count >= 3 (actively retrieved = important)
|
|
1798
|
+
2. age > 5 days AND strength > 0.4 (survived decay = worth keeping)
|
|
1799
|
+
3. source_type in ('learning', 'decision', 'feedback') (high-value by nature)
|
|
1800
|
+
"""
|
|
1673
1801
|
db = _get_db()
|
|
1802
|
+
now = datetime.utcnow()
|
|
1803
|
+
age_cutoff = (now - timedelta(days=5)).isoformat()
|
|
1804
|
+
|
|
1805
|
+
# Rule 1: frequently accessed
|
|
1806
|
+
# Rule 2: old + strong (survived decay)
|
|
1807
|
+
# Rule 3: high-value source types (always promote if in STM)
|
|
1674
1808
|
rows = db.execute(
|
|
1675
|
-
"SELECT * FROM stm_memories
|
|
1809
|
+
"""SELECT * FROM stm_memories
|
|
1810
|
+
WHERE promoted_to_ltm = 0
|
|
1811
|
+
AND (
|
|
1812
|
+
access_count >= 3
|
|
1813
|
+
OR (created_at < ? AND strength > 0.4)
|
|
1814
|
+
OR source_type IN ('learning', 'decision', 'feedback')
|
|
1815
|
+
)""",
|
|
1816
|
+
(age_cutoff,)
|
|
1676
1817
|
).fetchall()
|
|
1677
1818
|
|
|
1678
1819
|
promoted = 0
|
|
@@ -1692,21 +1833,25 @@ def promote_stm_to_ltm():
|
|
|
1692
1833
|
|
|
1693
1834
|
|
|
1694
1835
|
def gc_stm():
|
|
1695
|
-
"""Garbage collect STM: delete weak old memories and anything >
|
|
1836
|
+
"""Garbage collect STM: delete weak old memories and anything > 45 days.
|
|
1837
|
+
Pinned memories are never deleted.
|
|
1838
|
+
"""
|
|
1696
1839
|
db = _get_db()
|
|
1697
1840
|
now = datetime.utcnow()
|
|
1698
1841
|
cutoff_7d = (now - timedelta(days=7)).isoformat()
|
|
1699
|
-
|
|
1842
|
+
cutoff_45d = (now - timedelta(days=45)).isoformat()
|
|
1700
1843
|
|
|
1701
|
-
# Delete STM with strength < 0.3 and older than 7 days
|
|
1844
|
+
# Delete STM with strength < 0.3 and older than 7 days (skip pinned)
|
|
1702
1845
|
cur1 = db.execute(
|
|
1703
|
-
"DELETE FROM stm_memories WHERE strength < 0.3 AND created_at < ? AND promoted_to_ltm = 0"
|
|
1846
|
+
"DELETE FROM stm_memories WHERE strength < 0.3 AND created_at < ? AND promoted_to_ltm = 0 "
|
|
1847
|
+
"AND (lifecycle_state IS NULL OR lifecycle_state != 'pinned')",
|
|
1704
1848
|
(cutoff_7d,)
|
|
1705
1849
|
)
|
|
1706
|
-
# Delete any STM older than
|
|
1850
|
+
# Delete any STM older than 45 days (skip pinned)
|
|
1707
1851
|
cur2 = db.execute(
|
|
1708
|
-
"DELETE FROM stm_memories WHERE created_at < ? AND promoted_to_ltm = 0"
|
|
1709
|
-
(
|
|
1852
|
+
"DELETE FROM stm_memories WHERE created_at < ? AND promoted_to_ltm = 0 "
|
|
1853
|
+
"AND (lifecycle_state IS NULL OR lifecycle_state != 'pinned')",
|
|
1854
|
+
(cutoff_45d,)
|
|
1710
1855
|
)
|
|
1711
1856
|
db.commit()
|
|
1712
1857
|
return (cur1.rowcount or 0) + (cur2.rowcount or 0)
|
|
@@ -2544,12 +2689,12 @@ def get_siblings(memory_id: int) -> list[dict]:
|
|
|
2544
2689
|
def detect_dissonance(new_instruction: str, min_score: float = 0.65) -> list[dict]:
|
|
2545
2690
|
"""Detect cognitive dissonance: find LTM memories that contradict a new instruction.
|
|
2546
2691
|
|
|
2547
|
-
When
|
|
2692
|
+
When the user gives a new instruction that conflicts with established LTM memories
|
|
2548
2693
|
(strength > 0.8), this function surfaces the conflict so NEXO can verbalize it
|
|
2549
2694
|
rather than silently obeying or silently resisting.
|
|
2550
2695
|
|
|
2551
2696
|
Args:
|
|
2552
|
-
new_instruction: The new instruction or preference from
|
|
2697
|
+
new_instruction: The new instruction or preference from the user
|
|
2553
2698
|
min_score: Minimum cosine similarity to consider as potential conflict
|
|
2554
2699
|
|
|
2555
2700
|
Returns:
|
|
@@ -2584,12 +2729,12 @@ def detect_dissonance(new_instruction: str, min_score: float = 0.65) -> list[dic
|
|
|
2584
2729
|
|
|
2585
2730
|
|
|
2586
2731
|
def resolve_dissonance(memory_id: int, resolution: str, context: str = "") -> str:
|
|
2587
|
-
"""Resolve a cognitive dissonance by applying
|
|
2732
|
+
"""Resolve a cognitive dissonance by applying the user's decision.
|
|
2588
2733
|
|
|
2589
2734
|
Args:
|
|
2590
2735
|
memory_id: The LTM memory that conflicts with the new instruction
|
|
2591
2736
|
resolution: One of:
|
|
2592
|
-
- 'paradigm_shift':
|
|
2737
|
+
- 'paradigm_shift': user changed their mind permanently. Decay old memory,
|
|
2593
2738
|
new instruction becomes the standard.
|
|
2594
2739
|
- 'exception': This is a one-time override. Keep old memory as standard.
|
|
2595
2740
|
- 'override': Old memory was wrong. Mark as corrupted and decay to dormant.
|
|
@@ -2640,7 +2785,7 @@ def resolve_dissonance(memory_id: int, resolution: str, context: str = "") -> st
|
|
|
2640
2785
|
def check_correction_fatigue() -> list[dict]:
|
|
2641
2786
|
"""Find memories corrected 3+ times in the last 7 days — mark as 'under review'.
|
|
2642
2787
|
|
|
2643
|
-
These memories are unreliable:
|
|
2788
|
+
These memories are unreliable: user keeps overriding them, suggesting
|
|
2644
2789
|
the memory itself may be wrong or outdated.
|
|
2645
2790
|
|
|
2646
2791
|
Returns:
|
|
@@ -2688,7 +2833,7 @@ def check_correction_fatigue() -> list[dict]:
|
|
|
2688
2833
|
|
|
2689
2834
|
|
|
2690
2835
|
def detect_sentiment(text: str) -> dict:
|
|
2691
|
-
"""Analyze
|
|
2836
|
+
"""Analyze the user's text for sentiment signals.
|
|
2692
2837
|
|
|
2693
2838
|
Returns detected sentiment, intensity, and action guidance for NEXO.
|
|
2694
2839
|
Not a model — keyword + heuristic based. Fast and deterministic.
|
|
@@ -2727,17 +2872,17 @@ def detect_sentiment(text: str) -> dict:
|
|
|
2727
2872
|
sentiment = "negative"
|
|
2728
2873
|
intensity = min(1.0, 0.3 + neg_score * 0.15)
|
|
2729
2874
|
if intensity > 0.7:
|
|
2730
|
-
guidance = "MODE: Ultra-
|
|
2875
|
+
guidance = "MODE: Ultra-conciso. Cero explicaciones. Resolver y mostrar resultado."
|
|
2731
2876
|
else:
|
|
2732
|
-
guidance = "MODE:
|
|
2877
|
+
guidance = "MODE: Conciso. Menos contexto, más acción directa."
|
|
2733
2878
|
elif pos_score > neg_score and pos_score >= 1:
|
|
2734
2879
|
sentiment = "positive"
|
|
2735
2880
|
intensity = min(1.0, 0.3 + pos_score * 0.15)
|
|
2736
|
-
guidance = "MODE: Normal.
|
|
2881
|
+
guidance = "MODE: Normal. Buen momento para proponer ideas de backlog o mejoras."
|
|
2737
2882
|
elif urgency_hits:
|
|
2738
2883
|
sentiment = "urgent"
|
|
2739
2884
|
intensity = 0.8
|
|
2740
|
-
guidance = "MODE:
|
|
2885
|
+
guidance = "MODE: Acción inmediata. Sin preámbulos."
|
|
2741
2886
|
else:
|
|
2742
2887
|
sentiment = "neutral"
|
|
2743
2888
|
intensity = 0.5
|
|
@@ -2752,7 +2897,7 @@ def detect_sentiment(text: str) -> dict:
|
|
|
2752
2897
|
|
|
2753
2898
|
|
|
2754
2899
|
def log_sentiment(text: str) -> dict:
|
|
2755
|
-
"""Detect and log
|
|
2900
|
+
"""Detect and log the user's sentiment. Returns the detection result."""
|
|
2756
2901
|
result = detect_sentiment(text)
|
|
2757
2902
|
if result["sentiment"] != "neutral":
|
|
2758
2903
|
db = _get_db()
|
|
@@ -3018,12 +3163,35 @@ def dream_cycle(max_insights: int = 50) -> dict:
|
|
|
3018
3163
|
|
|
3019
3164
|
db.commit()
|
|
3020
3165
|
|
|
3021
|
-
|
|
3166
|
+
# Dream cap: archive oldest dream_insights if total exceeds MAX_DREAM_INSIGHTS
|
|
3167
|
+
MAX_DREAM_INSIGHTS = 50
|
|
3168
|
+
dream_count = db.execute(
|
|
3169
|
+
"SELECT COUNT(*) FROM ltm_memories WHERE source_type = 'dream_insight' AND is_dormant = 0"
|
|
3170
|
+
).fetchone()[0]
|
|
3171
|
+
archived_dreams = 0
|
|
3172
|
+
if dream_count > MAX_DREAM_INSIGHTS:
|
|
3173
|
+
excess = dream_count - MAX_DREAM_INSIGHTS
|
|
3174
|
+
oldest = db.execute(
|
|
3175
|
+
"SELECT id FROM ltm_memories WHERE source_type = 'dream_insight' AND is_dormant = 0 "
|
|
3176
|
+
"ORDER BY strength ASC, created_at ASC LIMIT ?", (excess,)
|
|
3177
|
+
).fetchall()
|
|
3178
|
+
for row in oldest:
|
|
3179
|
+
db.execute(
|
|
3180
|
+
"UPDATE ltm_memories SET lifecycle_state = 'archived' WHERE id = ?", (row["id"],)
|
|
3181
|
+
)
|
|
3182
|
+
archived_dreams += 1
|
|
3183
|
+
db.commit()
|
|
3184
|
+
|
|
3185
|
+
result = {
|
|
3022
3186
|
"insights_created": len(insights),
|
|
3023
3187
|
"insights": insights,
|
|
3024
3188
|
"memories_scanned": len(recent_memories),
|
|
3025
3189
|
"candidates_found": len(candidate_pairs),
|
|
3026
3190
|
}
|
|
3191
|
+
if archived_dreams > 0:
|
|
3192
|
+
result["dreams_archived"] = archived_dreams
|
|
3193
|
+
result["dreams_total"] = dream_count
|
|
3194
|
+
return result
|
|
3027
3195
|
|
|
3028
3196
|
|
|
3029
3197
|
def get_stats() -> dict:
|
|
@@ -2,18 +2,14 @@
|
|
|
2
2
|
"""NEXO Cognitive Decay — Daily Ebbinghaus sweep + STM→LTM promotion."""
|
|
3
3
|
|
|
4
4
|
import json
|
|
5
|
-
import os
|
|
6
5
|
import sys
|
|
7
6
|
from pathlib import Path
|
|
8
7
|
from datetime import datetime
|
|
9
8
|
|
|
10
|
-
NEXO_HOME = os.environ.get("NEXO_HOME", str(Path.home() / ".nexo"))
|
|
11
|
-
sys.path.insert(0, NEXO_HOME)
|
|
12
|
-
# Fallback for development installs
|
|
13
9
|
sys.path.insert(0, str(Path.home() / "claude" / "nexo-mcp"))
|
|
14
10
|
import cognitive
|
|
15
11
|
|
|
16
|
-
STATE_FILE = Path(
|
|
12
|
+
STATE_FILE = Path.home() / "claude" / "operations" / ".catchup-state.json"
|
|
17
13
|
|
|
18
14
|
|
|
19
15
|
def update_catchup_state():
|
|
@@ -31,6 +27,16 @@ def main():
|
|
|
31
27
|
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
32
28
|
print(f"[{ts}] Cognitive decay starting...")
|
|
33
29
|
|
|
30
|
+
# 0. Process quarantine FIRST — promote/reject/expire pending items
|
|
31
|
+
# BUG FIX 26-Mar-2026: quarantine was NEVER processed automatically.
|
|
32
|
+
# 78 items were stuck as pending indefinitely.
|
|
33
|
+
try:
|
|
34
|
+
q_result = cognitive.process_quarantine()
|
|
35
|
+
print(f"[{ts}] Quarantine: {q_result['promoted']} promoted, {q_result['rejected']} rejected, "
|
|
36
|
+
f"{q_result['expired']} expired, {q_result['still_pending']} still pending.")
|
|
37
|
+
except Exception as e:
|
|
38
|
+
print(f"[{ts}] Quarantine processing error: {e}")
|
|
39
|
+
|
|
34
40
|
# 1. Apply decay
|
|
35
41
|
cognitive.apply_decay()
|
|
36
42
|
print(f"[{ts}] Decay applied.")
|
|
@@ -39,9 +45,13 @@ def main():
|
|
|
39
45
|
promoted = cognitive.promote_stm_to_ltm()
|
|
40
46
|
print(f"[{ts}] Promoted {promoted} STM memories to LTM.")
|
|
41
47
|
|
|
42
|
-
# 3. Garbage collect expired STM
|
|
48
|
+
# 3. Garbage collect expired STM + sensory
|
|
43
49
|
gc_count = cognitive.gc_stm()
|
|
44
|
-
|
|
50
|
+
try:
|
|
51
|
+
gc_sensory = cognitive.gc_sensory(max_age_hours=48)
|
|
52
|
+
print(f"[{ts}] GC: removed {gc_count} expired STM, {gc_sensory} expired sensory.")
|
|
53
|
+
except Exception as e:
|
|
54
|
+
print(f"[{ts}] GC: removed {gc_count} expired STM. Sensory GC error: {e}")
|
|
45
55
|
|
|
46
56
|
# 4. Semantic consolidation — merge near-duplicate LTM (cosine > 0.9)
|
|
47
57
|
# With discriminative fusion: siblings (different environments) are linked, not merged
|
|
@@ -76,7 +86,7 @@ def main():
|
|
|
76
86
|
|
|
77
87
|
# 6. Memory Dreaming — discover hidden connections between recent memories
|
|
78
88
|
try:
|
|
79
|
-
dream_result = cognitive.dream_cycle(max_insights=
|
|
89
|
+
dream_result = cognitive.dream_cycle(max_insights=15)
|
|
80
90
|
scanned = dream_result["memories_scanned"]
|
|
81
91
|
created = dream_result["insights_created"]
|
|
82
92
|
candidates = dream_result["candidates_found"]
|
|
@@ -98,10 +108,9 @@ def main():
|
|
|
98
108
|
except Exception as e:
|
|
99
109
|
print(f"[{ts}] Auto-merge error: {e}")
|
|
100
110
|
|
|
101
|
-
#
|
|
111
|
+
# 9. Adaptive weight learning — Ridge regression from feedback-annotated entries
|
|
102
112
|
try:
|
|
103
|
-
|
|
104
|
-
sys.path.insert(0, plugins_dir)
|
|
113
|
+
sys.path.insert(0, str(Path.home() / "claude" / "nexo-mcp" / "plugins"))
|
|
105
114
|
from adaptive_mode import learn_weights, prune_adaptive_log, check_weight_rollback
|
|
106
115
|
|
|
107
116
|
rollback = check_weight_rollback()
|
|
@@ -131,7 +140,7 @@ def main():
|
|
|
131
140
|
except Exception as e:
|
|
132
141
|
print(f"[{ts}] Adaptive weight learning error: {e}")
|
|
133
142
|
|
|
134
|
-
#
|
|
143
|
+
# 10. Project somatic events from nexo.db -> cognitive.db
|
|
135
144
|
try:
|
|
136
145
|
projected = cognitive.somatic_project_events()
|
|
137
146
|
if projected > 0:
|
|
@@ -139,14 +148,14 @@ def main():
|
|
|
139
148
|
except Exception as e:
|
|
140
149
|
print(f"[{ts}] Somatic projection error: {e}")
|
|
141
150
|
|
|
142
|
-
#
|
|
151
|
+
# 11. Somatic marker nightly decay
|
|
143
152
|
try:
|
|
144
153
|
decayed = cognitive.somatic_nightly_decay(gamma=0.95)
|
|
145
154
|
print(f"[{ts}] Somatic decay: {decayed} markers processed (x0.95)")
|
|
146
155
|
except Exception as e:
|
|
147
156
|
print(f"[{ts}] Somatic decay error: {e}")
|
|
148
157
|
|
|
149
|
-
#
|
|
158
|
+
# 8. Stats
|
|
150
159
|
stats = cognitive.get_stats()
|
|
151
160
|
print(f"[{ts}] STM: {stats['stm_active']} | LTM: {stats['ltm_active']} active, {stats['ltm_dormant']} dormant")
|
|
152
161
|
print(f"[{ts}] Done.")
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|