nexo-brain 1.2.3 → 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/package.json +1 -1
- package/src/cognitive.py +45 -0
- package/src/evolution_cycle.py +266 -0
- package/src/plugins/guard.py +235 -1
- package/src/scripts/nexo-cognitive-decay.py +8 -0
- package/src/scripts/nexo-evolution-run.py +592 -0
- package/src/scripts/nexo-sleep.py +35 -11
- package/src/tools_sessions.py +20 -12
- 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/src/rules/__init__ 2.py +0 -0
- package/src/rules/__pycache__/migrate.cpython-314.pyc +0 -0
- package/src/rules/core-rules 2.json +0 -329
- package/src/rules/migrate 2.py +0 -207
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
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
|
@@ -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
|
package/src/plugins/guard.py
CHANGED
|
@@ -6,7 +6,7 @@ and provides stats on error prevention effectiveness.
|
|
|
6
6
|
import json
|
|
7
7
|
import os
|
|
8
8
|
from datetime import datetime, timedelta
|
|
9
|
-
from db import get_db, find_similar_learnings, extract_keywords
|
|
9
|
+
from db import get_db, find_similar_learnings, extract_keywords, search_learnings, search_changes
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
@@ -451,10 +451,244 @@ def handle_somatic_stats() -> str:
|
|
|
451
451
|
return "Error: {}".format(e)
|
|
452
452
|
|
|
453
453
|
|
|
454
|
+
def handle_guard_cross_check(findings: list, area: str = "") -> str:
|
|
455
|
+
"""Cross-check audit findings against known learnings to filter false positives.
|
|
456
|
+
|
|
457
|
+
Args:
|
|
458
|
+
findings: List of audit finding strings to cross-check
|
|
459
|
+
area: System area to narrow the learning search (wazion, shopify, etc.)
|
|
460
|
+
"""
|
|
461
|
+
# Common English/Spanish stopwords to skip during keyword extraction
|
|
462
|
+
STOPWORDS = {
|
|
463
|
+
"the", "a", "an", "is", "in", "on", "at", "to", "of", "and", "or", "but",
|
|
464
|
+
"for", "with", "that", "this", "it", "as", "are", "was", "be", "by", "not",
|
|
465
|
+
"has", "have", "from", "which", "when", "if", "then", "do", "does", "can",
|
|
466
|
+
"el", "la", "los", "las", "un", "una", "en", "de", "del", "al", "y", "o",
|
|
467
|
+
"que", "se", "no", "es", "por", "con", "su", "pero", "como", "para",
|
|
468
|
+
"este", "esta", "esto", "son", "hay", "más", "ya",
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
new_issues = []
|
|
472
|
+
known_issues = []
|
|
473
|
+
|
|
474
|
+
for finding in findings:
|
|
475
|
+
if not finding or not finding.strip():
|
|
476
|
+
continue
|
|
477
|
+
|
|
478
|
+
# Extract significant keywords from the finding text
|
|
479
|
+
words = finding.lower().split()
|
|
480
|
+
keywords = [
|
|
481
|
+
w.strip(".,;:!?\"'()[]{}") for w in words
|
|
482
|
+
if len(w) >= 4 and w.lower() not in STOPWORDS
|
|
483
|
+
]
|
|
484
|
+
# Use up to 5 most distinctive keywords to build the search query
|
|
485
|
+
query_keywords = keywords[:5]
|
|
486
|
+
|
|
487
|
+
matched_learnings = []
|
|
488
|
+
if query_keywords:
|
|
489
|
+
query = " ".join(query_keywords)
|
|
490
|
+
try:
|
|
491
|
+
results = search_learnings(query, category=area if area else None)
|
|
492
|
+
if not results and area:
|
|
493
|
+
# Retry without category filter if area-filtered search returns nothing
|
|
494
|
+
results = search_learnings(query)
|
|
495
|
+
matched_learnings = results[:3] # Top 3 matches per finding
|
|
496
|
+
except Exception:
|
|
497
|
+
pass
|
|
498
|
+
|
|
499
|
+
if matched_learnings:
|
|
500
|
+
refs = [
|
|
501
|
+
{"id": r["id"], "title": r["title"], "category": r.get("category", "")}
|
|
502
|
+
for r in matched_learnings
|
|
503
|
+
]
|
|
504
|
+
known_issues.append({
|
|
505
|
+
"finding": finding,
|
|
506
|
+
"status": "known",
|
|
507
|
+
"learning_refs": refs,
|
|
508
|
+
})
|
|
509
|
+
else:
|
|
510
|
+
new_issues.append({
|
|
511
|
+
"finding": finding,
|
|
512
|
+
"status": "new",
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
# Build output
|
|
516
|
+
lines = [
|
|
517
|
+
f"CROSS-CHECK RESULTS: {len(findings)} findings — "
|
|
518
|
+
f"{len(new_issues)} new, {len(known_issues)} already documented",
|
|
519
|
+
"",
|
|
520
|
+
]
|
|
521
|
+
|
|
522
|
+
if new_issues:
|
|
523
|
+
lines.append(f"NEW ISSUES ({len(new_issues)}) — not in learnings, investigate:")
|
|
524
|
+
for i, item in enumerate(new_issues, 1):
|
|
525
|
+
lines.append(f" {i}. {item['finding']}")
|
|
526
|
+
lines.append("")
|
|
527
|
+
|
|
528
|
+
if known_issues:
|
|
529
|
+
lines.append(f"KNOWN ISSUES ({len(known_issues)}) — covered by existing learnings:")
|
|
530
|
+
for i, item in enumerate(known_issues, 1):
|
|
531
|
+
refs_str = ", ".join(
|
|
532
|
+
f"#{r['id']} [{r['category']}] {r['title'][:60]}"
|
|
533
|
+
for r in item["learning_refs"]
|
|
534
|
+
)
|
|
535
|
+
lines.append(f" {i}. {item['finding']}")
|
|
536
|
+
lines.append(f" -> {refs_str}")
|
|
537
|
+
lines.append("")
|
|
538
|
+
|
|
539
|
+
summary = {
|
|
540
|
+
"total": len(findings),
|
|
541
|
+
"new_count": len(new_issues),
|
|
542
|
+
"known_count": len(known_issues),
|
|
543
|
+
"new_issues": [i["finding"] for i in new_issues],
|
|
544
|
+
"known_issues": [
|
|
545
|
+
{"finding": i["finding"], "refs": i["learning_refs"]}
|
|
546
|
+
for i in known_issues
|
|
547
|
+
],
|
|
548
|
+
}
|
|
549
|
+
lines.append(f"SUMMARY JSON: {json.dumps(summary)}")
|
|
550
|
+
|
|
551
|
+
return "\n".join(lines)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def handle_guard_file_check(files: list) -> str:
|
|
555
|
+
"""Pre-edit check: surfaces learnings and recent changes for files about to be modified.
|
|
556
|
+
|
|
557
|
+
Args:
|
|
558
|
+
files: List of file paths about to be edited
|
|
559
|
+
"""
|
|
560
|
+
from pathlib import Path
|
|
561
|
+
import re
|
|
562
|
+
|
|
563
|
+
BLOCKING_KEYWORDS = re.compile(
|
|
564
|
+
r'\bNUNCA\b|\bNEVER\b|\bPROHIBIDO\b|\bFORBIDDEN\b|\bBLOCKING\b',
|
|
565
|
+
re.IGNORECASE
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
if not files:
|
|
569
|
+
return "ERROR: No files provided."
|
|
570
|
+
|
|
571
|
+
file_learnings: dict = {}
|
|
572
|
+
recent_changes: dict = {}
|
|
573
|
+
warnings: list = []
|
|
574
|
+
seen_learning_ids: set = set()
|
|
575
|
+
|
|
576
|
+
for filepath in files:
|
|
577
|
+
p = Path(filepath)
|
|
578
|
+
filename = p.name
|
|
579
|
+
parent_dir = p.parent.name
|
|
580
|
+
stem = p.stem # filename without extension
|
|
581
|
+
|
|
582
|
+
# Build search keywords: filename, stem, parent directory (deduplicated)
|
|
583
|
+
keywords = [kw for kw in [filename, stem, parent_dir] if kw and kw not in (".", "")]
|
|
584
|
+
seen_kw: set = set()
|
|
585
|
+
unique_keywords = []
|
|
586
|
+
for kw in keywords:
|
|
587
|
+
if kw not in seen_kw:
|
|
588
|
+
seen_kw.add(kw)
|
|
589
|
+
unique_keywords.append(kw)
|
|
590
|
+
|
|
591
|
+
file_results = []
|
|
592
|
+
file_seen_ids: set = set()
|
|
593
|
+
|
|
594
|
+
for keyword in unique_keywords:
|
|
595
|
+
try:
|
|
596
|
+
rows = search_learnings(keyword)
|
|
597
|
+
for r in rows:
|
|
598
|
+
lid = r.get("id")
|
|
599
|
+
if lid and lid not in seen_learning_ids and lid not in file_seen_ids:
|
|
600
|
+
file_seen_ids.add(lid)
|
|
601
|
+
seen_learning_ids.add(lid)
|
|
602
|
+
entry = {
|
|
603
|
+
"id": lid,
|
|
604
|
+
"category": r.get("category", ""),
|
|
605
|
+
"title": r.get("title", ""),
|
|
606
|
+
"content": (r.get("content") or "")[:300],
|
|
607
|
+
}
|
|
608
|
+
file_results.append(entry)
|
|
609
|
+
# Flag blocking learnings
|
|
610
|
+
if BLOCKING_KEYWORDS.search(r.get("title", "")) or \
|
|
611
|
+
BLOCKING_KEYWORDS.search(r.get("content") or ""):
|
|
612
|
+
warnings.append(
|
|
613
|
+
f"[BLOCKING] #{lid} ({filepath}): {r.get('title', '')}"
|
|
614
|
+
)
|
|
615
|
+
except Exception:
|
|
616
|
+
pass
|
|
617
|
+
|
|
618
|
+
file_learnings[filepath] = file_results
|
|
619
|
+
|
|
620
|
+
# Search recent changes (last 7 days) for this file by filename/stem
|
|
621
|
+
file_changes = []
|
|
622
|
+
for keyword in unique_keywords[:2]: # filename + stem are most specific
|
|
623
|
+
try:
|
|
624
|
+
changes = search_changes(files=keyword, days=7)
|
|
625
|
+
for c in changes:
|
|
626
|
+
cid = c.get("id")
|
|
627
|
+
if cid and not any(fc.get("id") == cid for fc in file_changes):
|
|
628
|
+
file_changes.append({
|
|
629
|
+
"id": cid,
|
|
630
|
+
"files": c.get("files", ""),
|
|
631
|
+
"what_changed": (c.get("what_changed") or "")[:200],
|
|
632
|
+
"why": (c.get("why") or "")[:150],
|
|
633
|
+
"created_at": (c.get("created_at") or "")[:16],
|
|
634
|
+
})
|
|
635
|
+
except Exception:
|
|
636
|
+
pass
|
|
637
|
+
|
|
638
|
+
recent_changes[filepath] = file_changes
|
|
639
|
+
|
|
640
|
+
# Build summary line
|
|
641
|
+
total_learnings = sum(len(v) for v in file_learnings.values())
|
|
642
|
+
total_changes = sum(len(v) for v in recent_changes.values())
|
|
643
|
+
summary_parts = []
|
|
644
|
+
if total_learnings:
|
|
645
|
+
summary_parts.append(f"{total_learnings} learning(s) found")
|
|
646
|
+
if total_changes:
|
|
647
|
+
summary_parts.append(f"{total_changes} recent change(s) in last 7 days")
|
|
648
|
+
if warnings:
|
|
649
|
+
summary_parts.append(f"{len(warnings)} BLOCKING warning(s)")
|
|
650
|
+
summary = ", ".join(summary_parts) if summary_parts else "No relevant learnings or recent changes found."
|
|
651
|
+
|
|
652
|
+
# Format output
|
|
653
|
+
lines = []
|
|
654
|
+
|
|
655
|
+
if warnings:
|
|
656
|
+
lines.append("WARNINGS — resolve before editing:")
|
|
657
|
+
for w in warnings:
|
|
658
|
+
lines.append(f" {w}")
|
|
659
|
+
lines.append("")
|
|
660
|
+
|
|
661
|
+
for filepath in files:
|
|
662
|
+
learnings = file_learnings.get(filepath, [])
|
|
663
|
+
changes = recent_changes.get(filepath, [])
|
|
664
|
+
if not learnings and not changes:
|
|
665
|
+
continue
|
|
666
|
+
lines.append(f"FILE: {filepath}")
|
|
667
|
+
if learnings:
|
|
668
|
+
lines.append(f" Learnings ({len(learnings)}):")
|
|
669
|
+
for entry in learnings[:10]:
|
|
670
|
+
lines.append(f" #{entry['id']} [{entry['category']}] {entry['title']}")
|
|
671
|
+
if entry["content"]:
|
|
672
|
+
lines.append(f" {entry['content'][:120]}")
|
|
673
|
+
if changes:
|
|
674
|
+
lines.append(f" Recent changes ({len(changes)}, last 7d):")
|
|
675
|
+
for c in changes[:5]:
|
|
676
|
+
lines.append(f" [{c['created_at']}] {c['what_changed'][:100]}")
|
|
677
|
+
if c["why"]:
|
|
678
|
+
lines.append(f" Why: {c['why'][:80]}")
|
|
679
|
+
lines.append("")
|
|
680
|
+
|
|
681
|
+
lines.append(f"SUMMARY: {summary}")
|
|
682
|
+
|
|
683
|
+
return "\n".join(lines) if lines else summary
|
|
684
|
+
|
|
685
|
+
|
|
454
686
|
TOOLS = [
|
|
455
687
|
(handle_guard_check, "nexo_guard_check", "Check learnings relevant to files/area BEFORE editing code. Call this before any code change."),
|
|
456
688
|
(handle_guard_stats, "nexo_guard_stats", "Get guard system statistics: repetition rate, trends, top problem areas"),
|
|
457
689
|
(handle_guard_log_repetition, "nexo_guard_log_repetition", "Log a learning repetition (new learning matches existing one)"),
|
|
458
690
|
(handle_somatic_check, "nexo_somatic_check", "View somatic risk scores for files/areas — pain memory"),
|
|
459
691
|
(handle_somatic_stats, "nexo_somatic_stats", "Top 10 riskiest targets + risk distribution"),
|
|
692
|
+
(handle_guard_cross_check, "nexo_guard_cross_check", "Cross-check audit findings against known learnings to filter false positives"),
|
|
693
|
+
(handle_guard_file_check, "nexo_guard_file_check", "Pre-edit check: surfaces learnings and recent changes for files about to be modified"),
|
|
460
694
|
]
|
|
@@ -37,6 +37,14 @@ def main():
|
|
|
37
37
|
except Exception as e:
|
|
38
38
|
print(f"[{ts}] Quarantine processing error: {e}")
|
|
39
39
|
|
|
40
|
+
# 0b. Purge test/dev memories from STM
|
|
41
|
+
try:
|
|
42
|
+
test_purged = cognitive.gc_test_memories()
|
|
43
|
+
if test_purged > 0:
|
|
44
|
+
print(f"[{ts}] Purged {test_purged} test/dev memories from STM.")
|
|
45
|
+
except Exception as e:
|
|
46
|
+
print(f"[{ts}] Test memory purge error: {e}")
|
|
47
|
+
|
|
40
48
|
# 1. Apply decay
|
|
41
49
|
cognitive.apply_decay()
|
|
42
50
|
print(f"[{ts}] Decay applied.")
|