nexo-brain 1.2.2 → 1.3.0

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/LICENSE CHANGED
@@ -1,21 +1,26 @@
1
- MIT License
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
2
3
 
3
4
  Copyright (c) 2026 WAzion Apps
4
5
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Affero General Public License as published
8
+ by the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
11
10
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Affero General Public License for more details.
14
15
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
16
+ You should have received a copy of the GNU Affero General Public License
17
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ Additional permission under GNU AGPL version 3 section 7:
20
+
21
+ If you modify this Program, or any covered work, by linking or combining
22
+ it with other code, such combination is not by itself cause for the
23
+ Program to be covered by the GNU Affero General Public License.
24
+
25
+ For the full license text, see:
26
+ https://www.gnu.org/licenses/agpl-3.0.en.html
package/README.md CHANGED
@@ -4,7 +4,7 @@
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
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL--3.0-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
8
8
 
9
9
  > **v1.1.1** — Context Continuity via auto-compaction hooks. PreCompact saves a full session checkpoint; PostCompact re-injects it so long sessions (8+ hours) feel like one continuous conversation. Plus: Cognitive Cortex, 30 Core Rules as DNA, Smart Startup, Context Packets, Auto-Prime. The first AI memory system with architectural inhibitory control — the agent reasons about whether to act before acting. Battle-tested from 6 months of production use, validated via multi-AI debate (Claude Opus + GPT-5.4 + Gemini 3.1 Pro).
10
10
 
@@ -650,7 +650,7 @@ If NEXO Brain is useful to you, consider:
650
650
 
651
651
  ## License
652
652
 
653
- MIT -- see [LICENSE](LICENSE)
653
+ AGPL-3.0 -- see [LICENSE](LICENSE)
654
654
 
655
655
  ---
656
656
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "mcpName": "io.github.wazionapps/nexo",
5
- "description": "NEXO \u2014 Cognitive co-operator for Claude Code. Atkinson-Shiffrin memory, semantic RAG, trust scoring, and metacognitive error prevention.",
5
+ "description": "NEXO Cognitive co-operator for Claude Code. Atkinson-Shiffrin memory, semantic RAG, trust scoring, and metacognitive error prevention.",
6
6
  "bin": {
7
7
  "nexo-brain": "./bin/nexo-brain.js"
8
8
  },
@@ -18,7 +18,7 @@
18
18
  "openclaw-plugin"
19
19
  ],
20
20
  "author": "NEXO Brain <info@nexo-brain.com>",
21
- "license": "MIT",
21
+ "license": "AGPL-3.0",
22
22
  "repository": {
23
23
  "type": "git",
24
24
  "url": "git+https://github.com/wazionapps/nexo.git"
@@ -32,4 +32,4 @@
32
32
  "templates/",
33
33
  "scripts/"
34
34
  ]
35
- }
35
+ }
package/src/cognitive.py CHANGED
@@ -1857,6 +1857,51 @@ def gc_stm():
1857
1857
  return (cur1.rowcount or 0) + (cur2.rowcount or 0)
1858
1858
 
1859
1859
 
1860
+ def gc_test_memories() -> int:
1861
+ """Purge STM memories from test/dev sessions that pollute strength metrics.
1862
+ Removes memories with test domains or known test content patterns.
1863
+ Returns count of deleted memories.
1864
+ """
1865
+ db = _get_db()
1866
+ test_domains = ("test", "test_session")
1867
+ deleted = 0
1868
+
1869
+ # 1. Delete by test domain
1870
+ for domain in test_domains:
1871
+ cur = db.execute(
1872
+ "DELETE FROM stm_memories WHERE domain = ? "
1873
+ "AND (lifecycle_state IS NULL OR lifecycle_state != 'pinned')",
1874
+ (domain,)
1875
+ )
1876
+ deleted += cur.rowcount or 0
1877
+
1878
+ # 2. Delete known test content patterns (empty domain, test-like content)
1879
+ test_patterns = [
1880
+ "%Secret redact test%",
1881
+ "%quarantine test fact%",
1882
+ "%Pin test memory%",
1883
+ "%API rate limit%AM10%",
1884
+ "%xyzzy server%",
1885
+ "%Quantum entanglement enables FTL%",
1886
+ "%Install Docker%AM10%",
1887
+ "%normal safe content about coding%",
1888
+ "%test diary%",
1889
+ "%test critique%",
1890
+ "%integration test diary%",
1891
+ ]
1892
+ for pattern in test_patterns:
1893
+ cur = db.execute(
1894
+ "DELETE FROM stm_memories WHERE content LIKE ? "
1895
+ "AND (lifecycle_state IS NULL OR lifecycle_state != 'pinned')",
1896
+ (pattern,)
1897
+ )
1898
+ deleted += cur.rowcount or 0
1899
+
1900
+ if deleted > 0:
1901
+ db.commit()
1902
+ return deleted
1903
+
1904
+
1860
1905
  def ingest_sensory(
1861
1906
  content: str,
1862
1907
  source_id: str = "",
@@ -0,0 +1,266 @@
1
+ """NEXO Evolution Cycle — Self-improvement via Opus API.
2
+
3
+ Runs weekly after DMN. Analyzes patterns, proposes improvements.
4
+ v1: observe-only (all proposals logged as 'proposed' for the owner to review).
5
+ v1.1 (future): sandbox execution of auto-approved changes.
6
+ """
7
+
8
+ import json
9
+ import os
10
+ import shutil
11
+ import subprocess
12
+ import sqlite3
13
+ import time
14
+ from datetime import datetime, date, timedelta
15
+ from pathlib import Path
16
+
17
+ NEXO_DB = Path.home() / "claude" / "nexo-mcp" / "nexo.db"
18
+ CORTEX_DIR = Path(__file__).parent
19
+ CLAUDE_DIR = Path.home() / "claude"
20
+ SANDBOX_DIR = CLAUDE_DIR / "sandbox" / "workspace"
21
+ SNAPSHOTS_DIR = CLAUDE_DIR / "snapshots"
22
+ OBJECTIVE_FILE = CORTEX_DIR / "evolution-objective.json"
23
+ PROMPT_FILE = CORTEX_DIR / "evolution-prompt.md"
24
+ RESTORE_LOG = CLAUDE_DIR / "logs" / "snapshot-restores.log"
25
+
26
+ MAX_SNAPSHOTS = 8
27
+
28
+
29
+ def load_objective() -> dict:
30
+ if OBJECTIVE_FILE.exists():
31
+ return json.loads(OBJECTIVE_FILE.read_text())
32
+ return {}
33
+
34
+
35
+ def save_objective(obj: dict):
36
+ OBJECTIVE_FILE.write_text(json.dumps(obj, indent=2, ensure_ascii=False))
37
+
38
+
39
+ def get_week_data(db_path: str) -> dict:
40
+ """Gather last 7 days of learnings, decisions, changes, diaries."""
41
+ conn = sqlite3.connect(db_path, timeout=10)
42
+ conn.row_factory = sqlite3.Row
43
+ cutoff_epoch = time.time() - 7 * 86400
44
+ cutoff_date = (date.today() - timedelta(days=7)).isoformat()
45
+
46
+ data = {}
47
+
48
+ rows = conn.execute(
49
+ "SELECT category, title, content FROM learnings WHERE created_at > ? ORDER BY created_at DESC LIMIT 50",
50
+ (cutoff_epoch,)
51
+ ).fetchall()
52
+ data["learnings"] = [dict(r) for r in rows]
53
+
54
+ rows = conn.execute(
55
+ "SELECT domain, decision, alternatives, based_on, confidence, outcome FROM decisions "
56
+ "WHERE created_at > ? ORDER BY created_at DESC LIMIT 20",
57
+ (cutoff_date,)
58
+ ).fetchall()
59
+ data["decisions"] = [dict(r) for r in rows]
60
+
61
+ rows = conn.execute(
62
+ "SELECT files, what_changed, why, affects, risks FROM change_log "
63
+ "WHERE created_at > ? ORDER BY created_at DESC LIMIT 30",
64
+ (cutoff_date,)
65
+ ).fetchall()
66
+ data["changes"] = [dict(r) for r in rows]
67
+
68
+ rows = conn.execute(
69
+ "SELECT summary, decisions as diary_decisions, pending, mental_state, domain, user_signals "
70
+ "FROM session_diary WHERE created_at > ? ORDER BY created_at DESC LIMIT 20",
71
+ (cutoff_date,)
72
+ ).fetchall()
73
+ data["diaries"] = [dict(r) for r in rows]
74
+
75
+ rows = conn.execute(
76
+ "SELECT * FROM evolution_log ORDER BY id DESC LIMIT 20"
77
+ ).fetchall()
78
+ data["evolution_history"] = [dict(r) for r in rows]
79
+
80
+ rows = conn.execute(
81
+ "SELECT dimension, score, delta, measured_at FROM evolution_metrics "
82
+ "WHERE id IN (SELECT MAX(id) FROM evolution_metrics GROUP BY dimension)"
83
+ ).fetchall()
84
+ data["current_metrics"] = {r["dimension"]: dict(r) for r in rows}
85
+
86
+ conn.close()
87
+ return data
88
+
89
+
90
+ def create_snapshot(files_to_backup: list) -> str:
91
+ """Create a snapshot of specific files before modification."""
92
+ ts = datetime.now().strftime("%Y-%m-%dT%H:%M")
93
+ snap_dir = SNAPSHOTS_DIR / ts
94
+ files_dir = snap_dir / "files"
95
+
96
+ manifest = {
97
+ "created_at": datetime.now().isoformat(),
98
+ "files": [],
99
+ "reason": "evolution_cycle"
100
+ }
101
+
102
+ for filepath in files_to_backup:
103
+ fp = Path(filepath).expanduser()
104
+ if fp.exists():
105
+ rel = str(fp).replace(str(Path.home()) + "/", "")
106
+ dest = files_dir / rel
107
+ dest.parent.mkdir(parents=True, exist_ok=True)
108
+ shutil.copy2(fp, dest)
109
+ manifest["files"].append(rel)
110
+
111
+ snap_dir.mkdir(parents=True, exist_ok=True)
112
+ (snap_dir / "manifest.json").write_text(json.dumps(manifest, indent=2))
113
+
114
+ latest = SNAPSHOTS_DIR / "latest"
115
+ if latest.is_symlink():
116
+ latest.unlink()
117
+ latest.symlink_to(snap_dir)
118
+
119
+ _cleanup_snapshots()
120
+ return str(snap_dir)
121
+
122
+
123
+ def _cleanup_snapshots():
124
+ """Remove old snapshots, keeping MAX_SNAPSHOTS most recent + golden."""
125
+ if not SNAPSHOTS_DIR.exists():
126
+ return
127
+ snaps = sorted(
128
+ [d for d in SNAPSHOTS_DIR.iterdir()
129
+ if d.is_dir() and d.name not in ("latest", "golden")],
130
+ key=lambda d: d.stat().st_mtime,
131
+ reverse=True
132
+ )
133
+ for old in snaps[MAX_SNAPSHOTS:]:
134
+ shutil.rmtree(old)
135
+
136
+
137
+ def dry_run_restore_test() -> bool:
138
+ """Test that snapshot+restore works before making real changes."""
139
+ test_file = SANDBOX_DIR / "restore-test.txt"
140
+ test_file.parent.mkdir(parents=True, exist_ok=True)
141
+ test_file.write_text("original_content")
142
+
143
+ snap_dir = create_snapshot([str(test_file)])
144
+
145
+ test_file.write_text("modified_content")
146
+
147
+ try:
148
+ subprocess.run(
149
+ [str(CLAUDE_DIR / "scripts" / "nexo-snapshot-restore.sh"), snap_dir],
150
+ capture_output=True, timeout=10, check=True
151
+ )
152
+ content = test_file.read_text()
153
+ test_file.unlink(missing_ok=True)
154
+ # Clean up test snapshot
155
+ snap_path = Path(snap_dir)
156
+ if snap_path.exists():
157
+ shutil.rmtree(snap_path)
158
+ return content == "original_content"
159
+ except Exception:
160
+ test_file.unlink(missing_ok=True)
161
+ return False
162
+
163
+
164
+ def build_evolution_prompt(week_data: dict, objective: dict) -> str:
165
+ """Build the prompt for the Opus Evolution cycle."""
166
+ if PROMPT_FILE.exists():
167
+ template = PROMPT_FILE.read_text()
168
+ else:
169
+ template = "You are NEXO Evolution. Analyze the data and propose improvements."
170
+
171
+ prompt = template + "\n\n## WEEKLY DATA\n\n"
172
+ prompt += f"### Learnings ({len(week_data.get('learnings', []))} this week)\n"
173
+ for l in week_data.get("learnings", [])[:30]:
174
+ prompt += f"- [{l['category']}] {l['title']}: {str(l['content'])[:150]}\n"
175
+
176
+ prompt += f"\n### Decisions ({len(week_data.get('decisions', []))} this week)\n"
177
+ for d in week_data.get("decisions", [])[:15]:
178
+ outcome = f" → {str(d['outcome'])[:80]}" if d.get("outcome") else " → no outcome yet"
179
+ prompt += f"- [{d['domain']}] {str(d['decision'])[:150]}{outcome}\n"
180
+
181
+ prompt += f"\n### Changes ({len(week_data.get('changes', []))} this week)\n"
182
+ for c in week_data.get("changes", [])[:20]:
183
+ prompt += f"- {str(c['files'])[:60]}: {str(c['what_changed'])[:100]}\n"
184
+
185
+ prompt += f"\n### Session Diaries ({len(week_data.get('diaries', []))} this week)\n"
186
+ for s in week_data.get("diaries", [])[:10]:
187
+ prompt += f"- [{s.get('domain','')}] {str(s['summary'])[:150]}\n"
188
+ if s.get("user_signals"):
189
+ prompt += f" the owner: {str(s['user_signals'])[:100]}\n"
190
+
191
+ prompt += "\n### Current Dimension Scores\n"
192
+ for dim, m in week_data.get("current_metrics", {}).items():
193
+ prompt += f"- {dim}: {m['score']}% (delta: {m.get('delta', 0)})\n"
194
+
195
+ prompt += f"\n### Evolution History ({len(week_data.get('evolution_history', []))} entries)\n"
196
+ for h in week_data.get("evolution_history", [])[:10]:
197
+ prompt += f"- #{h['id']} [{h['status']}] {str(h['proposal'])[:100]}\n"
198
+
199
+ prompt += f"\n### Objective\n{json.dumps(objective, indent=2)}\n"
200
+
201
+ # Guard stats — error prevention effectiveness
202
+ try:
203
+ guard_conn = sqlite3.connect(str(NEXO_DB), timeout=10)
204
+ cutoff_7d = (date.today() - timedelta(days=7)).isoformat()
205
+ cutoff_epoch_7d = time.time() - 7 * 86400
206
+
207
+ total_reps = guard_conn.execute(
208
+ "SELECT COUNT(*) FROM error_repetitions WHERE created_at > ?", (cutoff_7d,)
209
+ ).fetchone()[0]
210
+ new_learnings_7d = guard_conn.execute(
211
+ "SELECT COUNT(*) FROM learnings WHERE created_at > ?", (cutoff_epoch_7d,)
212
+ ).fetchone()[0]
213
+ rep_rate = round(total_reps / new_learnings_7d, 2) if new_learnings_7d > 0 else 0.0
214
+ guard_checks = guard_conn.execute(
215
+ "SELECT COUNT(*) FROM guard_checks WHERE created_at > ?", (cutoff_7d,)
216
+ ).fetchone()[0]
217
+
218
+ top_areas = guard_conn.execute(
219
+ "SELECT area, COUNT(*) as cnt FROM error_repetitions WHERE created_at > ? GROUP BY area ORDER BY cnt DESC LIMIT 5",
220
+ (cutoff_7d,)
221
+ ).fetchall()
222
+
223
+ most_ignored = guard_conn.execute(
224
+ "SELECT original_learning_id, COUNT(*) as cnt FROM error_repetitions "
225
+ "GROUP BY original_learning_id HAVING cnt >= 3 ORDER BY cnt DESC LIMIT 5"
226
+ ).fetchall()
227
+
228
+ guard_conn.close()
229
+
230
+ prompt += "\n### Guard Stats (Error Prevention)\n"
231
+ prompt += f"- Repetition rate: {rep_rate:.0%} (target: <15%)\n"
232
+ prompt += f"- Guard checks this week: {guard_checks} (target: >5/session)\n"
233
+ prompt += f"- New learnings: {new_learnings_7d}, Repetitions: {total_reps}\n"
234
+ if top_areas:
235
+ prompt += "- Top problem areas: " + ", ".join(f"{r[0]}({r[1]})" for r in top_areas) + "\n"
236
+ if most_ignored:
237
+ prompt += "- Most ignored learnings (3+ repeats): " + ", ".join(f"#{r[0]}({r[1]}x)" for r in most_ignored) + "\n"
238
+ prompt += "- Propose more aggressive rules for areas with high repetition rate.\n"
239
+ except Exception:
240
+ pass
241
+
242
+ # Infrastructure inventory — so Opus knows what exists before proposing changes
243
+ inventory_script = Path.home() / "claude" / "scripts" / "nexo-infra-inventory.sh"
244
+ if inventory_script.exists():
245
+ try:
246
+ result = subprocess.run(
247
+ ["bash", str(inventory_script)],
248
+ capture_output=True, text=True, timeout=10
249
+ )
250
+ if result.stdout.strip():
251
+ prompt += f"\n### Infrastructure Inventory (hooks, scripts, memory, crons)\n"
252
+ prompt += "Before proposing any change, check this inventory to avoid duplicating existing infrastructure.\n"
253
+ prompt += f"```json\n{result.stdout.strip()}\n```\n"
254
+ except Exception:
255
+ pass
256
+
257
+ return prompt
258
+
259
+
260
+ def max_auto_changes(total_evolutions: int) -> int:
261
+ """Progressive trust: 1 for first 4 cycles, 2 for next 4, then 3."""
262
+ if total_evolutions < 4:
263
+ return 1
264
+ elif total_evolutions < 8:
265
+ return 2
266
+ return 3