nexo-brain 1.3.0 → 1.4.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.
Files changed (33) hide show
  1. package/README.md +10 -5
  2. package/package.json +1 -1
  3. package/src/__pycache__/db.cpython-314.pyc +0 -0
  4. package/src/__pycache__/evolution_cycle.cpython-314.pyc +0 -0
  5. package/src/__pycache__/tools_credentials.cpython-314.pyc +0 -0
  6. package/src/dashboard/__pycache__/__init__.cpython-314.pyc +0 -0
  7. package/src/dashboard/__pycache__/app.cpython-314.pyc +0 -0
  8. package/src/plugins/__pycache__/episodic_memory.cpython-314.pyc +0 -0
  9. package/src/rules/__init__ 2.py +0 -0
  10. package/src/rules/__pycache__/migrate.cpython-314.pyc +0 -0
  11. package/src/rules/core-rules 2.json +329 -0
  12. package/src/rules/migrate 2.py +207 -0
  13. package/src/scripts/__pycache__/check-context.cpython-314.pyc +0 -0
  14. package/src/scripts/__pycache__/nexo-auto-update.cpython-314.pyc +0 -0
  15. package/src/scripts/__pycache__/nexo-catchup.cpython-314.pyc +0 -0
  16. package/src/scripts/__pycache__/nexo-cognitive-decay.cpython-314.pyc +0 -0
  17. package/src/scripts/__pycache__/nexo-daily-self-audit.cpython-314.pyc +0 -0
  18. package/src/scripts/__pycache__/nexo-evolution-run.cpython-314.pyc +0 -0
  19. package/src/scripts/__pycache__/nexo-immune.cpython-314.pyc +0 -0
  20. package/src/scripts/__pycache__/nexo-learning-validator.cpython-314.pyc +0 -0
  21. package/src/scripts/__pycache__/nexo-postmortem-consolidator.cpython-314.pyc +0 -0
  22. package/src/scripts/__pycache__/nexo-reflection.cpython-314.pyc +0 -0
  23. package/src/scripts/__pycache__/nexo-sleep.cpython-314.pyc +0 -0
  24. package/src/scripts/__pycache__/nexo-synthesis.cpython-314.pyc +0 -0
  25. package/src/scripts/check-context.py +257 -0
  26. package/src/scripts/nexo-catchup.py +59 -5
  27. package/src/scripts/nexo-daily-self-audit.py +168 -183
  28. package/src/scripts/nexo-evolution-run.py +24 -32
  29. package/src/scripts/nexo-immune.py +108 -91
  30. package/src/scripts/nexo-learning-validator.py +226 -0
  31. package/src/scripts/nexo-postmortem-consolidator.py +230 -414
  32. package/src/scripts/nexo-sleep.py +283 -527
  33. package/src/scripts/nexo-synthesis.py +141 -432
@@ -0,0 +1,257 @@
1
+ #!/usr/bin/env python3
2
+ """Context checker for NEXO operations - prevents duplicate actions.
3
+
4
+ Mechanical checks (email sent, file exists, action done) run in Python.
5
+ When the 'smart' command is used, passes context to Claude CLI for
6
+ intelligent duplicate/conflict detection that goes beyond file checks.
7
+ """
8
+
9
+ import os
10
+ import sys
11
+ import json
12
+ import hashlib
13
+ import subprocess
14
+ from datetime import datetime
15
+ from pathlib import Path
16
+
17
+ CLAUDE_CLI = Path.home() / ".local" / "bin" / "claude"
18
+
19
+ class ContextChecker:
20
+ def __init__(self):
21
+ self.state_dir = Path.home() / 'claude' / 'state'
22
+ self.state_dir.mkdir(exist_ok=True)
23
+
24
+ def check_email_sent(self, to_addr, subject, since_hours=72):
25
+ """Check if email was already sent to address with subject."""
26
+ sent_path = Path.home() / 'mail' / '.nexo-sent' / '.Sent' # Configure for your mail setup
27
+ if not sent_path.exists():
28
+ return False
29
+
30
+ subject_lower = subject.lower()
31
+ to_lower = to_addr.lower()
32
+ cutoff = datetime.now().timestamp() - (since_hours * 3600)
33
+ cur_dir = sent_path / 'cur'
34
+ if not cur_dir.exists():
35
+ return False
36
+
37
+ for msg_file in cur_dir.iterdir():
38
+ try:
39
+ if msg_file.stat().st_mtime < cutoff:
40
+ continue
41
+ content = msg_file.read_text(errors='ignore')
42
+ except (OSError, UnicodeDecodeError):
43
+ continue
44
+
45
+ content_lower = content.lower()
46
+ if f"to:{to_lower}" in content_lower or f"to: {to_lower}" in content_lower:
47
+ if subject_lower in content_lower:
48
+ return True
49
+ return False
50
+
51
+ def check_file_exists(self, pattern, search_dirs=None):
52
+ """Check if file matching pattern exists in common locations."""
53
+ if search_dirs is None:
54
+ search_dirs = [
55
+ '/var/www/vhosts',
56
+ str(Path.home() / 'claude'),
57
+ '/opt'
58
+ ]
59
+
60
+ for base_dir in search_dirs:
61
+ if not os.path.exists(base_dir):
62
+ continue
63
+ matches = []
64
+ try:
65
+ for root, _, files in os.walk(base_dir):
66
+ for filename in files:
67
+ if pattern in filename:
68
+ matches.append(str(Path(root) / filename))
69
+ if len(matches) >= 5:
70
+ return matches
71
+ except OSError:
72
+ continue
73
+ return []
74
+
75
+ def check_action_done(self, action_type, identifier, ttl_days=7):
76
+ """Check if action was already performed recently."""
77
+ action_file = self.state_dir / 'actions.json'
78
+
79
+ # Load existing actions
80
+ actions = {}
81
+ if action_file.exists():
82
+ with open(action_file) as f:
83
+ actions = json.load(f)
84
+
85
+ # Create action key
86
+ key = hashlib.md5(f"{action_type}:{identifier}".encode()).hexdigest()
87
+
88
+ # Check if exists and not expired
89
+ if key in actions:
90
+ action_time = datetime.fromisoformat(actions[key]['timestamp'])
91
+ age_days = (datetime.now() - action_time).days
92
+ if age_days < ttl_days:
93
+ return True, actions[key]
94
+
95
+ return False, None
96
+
97
+ def mark_action_done(self, action_type, identifier, metadata=None):
98
+ """Mark action as completed."""
99
+ action_file = self.state_dir / 'actions.json'
100
+
101
+ # Load existing actions
102
+ actions = {}
103
+ if action_file.exists():
104
+ with open(action_file) as f:
105
+ actions = json.load(f)
106
+
107
+ # Add new action
108
+ key = hashlib.md5(f"{action_type}:{identifier}".encode()).hexdigest()
109
+ actions[key] = {
110
+ 'type': action_type,
111
+ 'identifier': identifier,
112
+ 'timestamp': datetime.now().isoformat(),
113
+ 'metadata': metadata or {}
114
+ }
115
+
116
+ # Save
117
+ with open(action_file, 'w') as f:
118
+ json.dump(actions, f, indent=2)
119
+
120
+ return key
121
+
122
+ def smart_check(action_description: str, context: str = "") -> dict:
123
+ """Use Claude CLI to intelligently check if an action would be redundant.
124
+
125
+ Goes beyond simple file/hash checks — understands intent and context
126
+ to detect semantic duplicates (e.g., "send welcome email" vs
127
+ "email onboarding message" to same person).
128
+ """
129
+ checker = ContextChecker()
130
+
131
+ # Gather mechanical context first
132
+ state_file = checker.state_dir / 'actions.json'
133
+ recent_actions = {}
134
+ if state_file.exists():
135
+ try:
136
+ all_actions = json.loads(state_file.read_text())
137
+ cutoff = datetime.now().timestamp() - (7 * 86400)
138
+ for k, v in all_actions.items():
139
+ try:
140
+ ts = datetime.fromisoformat(v['timestamp']).timestamp()
141
+ if ts > cutoff:
142
+ recent_actions[k] = v
143
+ except (ValueError, KeyError):
144
+ pass
145
+ except Exception:
146
+ pass
147
+
148
+ if not CLAUDE_CLI.exists():
149
+ return {"redundant": False, "reason": "CLI unavailable, cannot smart-check"}
150
+
151
+ prompt = f"""You are a context deduplication engine for NEXO operations.
152
+
153
+ PROPOSED ACTION:
154
+ {action_description}
155
+
156
+ ADDITIONAL CONTEXT:
157
+ {context or "None"}
158
+
159
+ RECENT ACTIONS (last 7 days):
160
+ {json.dumps(list(recent_actions.values()), indent=1, default=str)}
161
+
162
+ Respond with ONLY valid JSON (no markdown):
163
+ {{
164
+ "redundant": true/false,
165
+ "confidence": 0.0-1.0,
166
+ "reason": "<one line explanation>",
167
+ "matching_action": "<identifier of matching action if redundant, else null>"
168
+ }}
169
+
170
+ Rules:
171
+ - Same recipient + same intent within 72h = redundant
172
+ - Same file modification with same content = redundant
173
+ - Similar but different scope (e.g., different recipients) = NOT redundant
174
+ - When in doubt, say not redundant (false negatives are cheaper than false positives)"""
175
+
176
+ env = os.environ.copy()
177
+ env.pop("CLAUDECODE", None)
178
+ env.pop("CLAUDE_CODE", None)
179
+
180
+ try:
181
+ result = subprocess.run(
182
+ [str(CLAUDE_CLI), "-p", prompt, "--model", "opus",
183
+ "--allowedTools", "Read,Write,Edit,Glob,Grep"],
184
+ capture_output=True, text=True, timeout=60, env=env
185
+ )
186
+ if result.returncode == 0:
187
+ text = result.stdout.strip()
188
+ if "```json" in text:
189
+ text = text.split("```json")[1].split("```")[0]
190
+ elif "```" in text:
191
+ text = text.split("```")[1].split("```")[0]
192
+ return json.loads(text.strip())
193
+ except Exception:
194
+ pass
195
+
196
+ return {"redundant": False, "reason": "CLI check failed, defaulting to not redundant"}
197
+
198
+
199
+ def main():
200
+ """CLI interface for context checking."""
201
+ if len(sys.argv) < 3:
202
+ print("Usage: check-context.py <command> <args>")
203
+ print("Commands:")
204
+ print(" email <to> <subject> - Check if email was sent")
205
+ print(" file <pattern> - Check if file exists")
206
+ print(" action <type> <id> - Check if action was done")
207
+ print(" smart <description> [ctx] - Intelligent duplicate check via CLI")
208
+ sys.exit(1)
209
+
210
+ checker = ContextChecker()
211
+ command = sys.argv[1]
212
+
213
+ if command == 'email':
214
+ if len(sys.argv) < 4:
215
+ print("Usage: check-context.py email <to> <subject>")
216
+ sys.exit(1)
217
+ exists = checker.check_email_sent(sys.argv[2], sys.argv[3])
218
+ print("EXISTS" if exists else "NOT_FOUND")
219
+ sys.exit(0 if not exists else 1)
220
+
221
+ elif command == 'file':
222
+ files = checker.check_file_exists(sys.argv[2])
223
+ if files:
224
+ print("\n".join(files))
225
+ sys.exit(1)
226
+ else:
227
+ print("NOT_FOUND")
228
+ sys.exit(0)
229
+
230
+ elif command == 'action':
231
+ if len(sys.argv) < 4:
232
+ print("Usage: check-context.py action <type> <id>")
233
+ sys.exit(1)
234
+ done, data = checker.check_action_done(sys.argv[2], sys.argv[3])
235
+ if done:
236
+ print(f"DONE: {data}")
237
+ sys.exit(1)
238
+ else:
239
+ print("NOT_DONE")
240
+ sys.exit(0)
241
+
242
+ elif command == 'smart':
243
+ if len(sys.argv) < 3:
244
+ print("Usage: check-context.py smart <description> [context]")
245
+ sys.exit(1)
246
+ description = sys.argv[2]
247
+ context = sys.argv[3] if len(sys.argv) > 3 else ""
248
+ result = smart_check(description, context)
249
+ print(json.dumps(result, indent=2))
250
+ sys.exit(1 if result.get("redundant") else 0)
251
+
252
+ else:
253
+ print(f"Unknown command: {command}")
254
+ sys.exit(1)
255
+
256
+ if __name__ == '__main__':
257
+ main()
@@ -24,16 +24,17 @@ import sys
24
24
  from datetime import datetime, timedelta
25
25
  from pathlib import Path
26
26
 
27
+ CLAUDE_CLI = Path.home() / ".local" / "bin" / "claude"
28
+
27
29
  HOME = Path.home()
28
- NEXO_HOME = os.environ.get("NEXO_HOME", str(Path.home() / ".nexo"))
29
- LOG_DIR = HOME / "claude" / "logs"
30
+ LOG_DIR = HOME / ".nexo" / "logs"
30
31
  LOG_DIR.mkdir(parents=True, exist_ok=True)
31
32
  LOG_FILE = LOG_DIR / "catchup.log"
32
- STATE_FILE = HOME / "claude" / "operations" / ".catchup-state.json"
33
+ STATE_FILE = HOME / ".nexo" / "operations" / ".catchup-state.json"
33
34
 
34
35
  PYTHON_BREW = "/opt/homebrew/bin/python3"
35
36
  PYTHON_SYS = "/Library/Frameworks/Python.framework/Versions/3.12/bin/python3"
36
- SCRIPTS = HOME / "claude" / "scripts"
37
+ SCRIPTS = HOME / ".nexo" / "scripts"
37
38
 
38
39
 
39
40
  def log(msg: str):
@@ -140,7 +141,7 @@ def main():
140
141
  subprocess.run(
141
142
  [PYTHON_BREW if os.path.exists(PYTHON_BREW) else PYTHON_SYS, str(update_script)],
142
143
  capture_output=True, text=True, timeout=60,
143
- env={**os.environ, "HOME": str(HOME), "NEXO_HOME": NEXO_HOME}
144
+ env={**os.environ, "HOME": str(HOME), "NEXO_HOME": str(HOME / ".nexo")}
144
145
  )
145
146
  except Exception as e:
146
147
  log(f" Update check failed: {e}")
@@ -167,11 +168,64 @@ def main():
167
168
 
168
169
  if ran == 0:
169
170
  log("All tasks up to date, nothing to catch up.")
171
+ elif ran >= 3:
172
+ # Many tasks caught up — ask CLI to assess system state
173
+ _cli_post_catchup_assessment(ran, skipped, state)
170
174
  else:
171
175
  log(f"Caught up {ran} tasks, {skipped} already current.")
172
176
 
173
177
  log("=== Catch-Up complete ===")
174
178
 
175
179
 
180
+ def _cli_post_catchup_assessment(ran: int, skipped: int, state: dict):
181
+ """When 3+ tasks were missed, use CLI to assess if there are concerns."""
182
+ if not CLAUDE_CLI.exists():
183
+ log(f"Caught up {ran} tasks, {skipped} already current. (CLI unavailable for assessment)")
184
+ return
185
+
186
+ assessment_file = LOG_DIR / "catchup-assessment.md"
187
+ state_summary = json.dumps(state, indent=2, default=str)
188
+
189
+ prompt = f"""You are the NEXO Catch-Up system. The Mac was off/asleep and {ran} scheduled tasks just ran as catch-up ({skipped} were already current).
190
+
191
+ Task run state (timestamps of last successful runs):
192
+ {state_summary}
193
+
194
+ Assess:
195
+ 1. How long was the system likely offline? (compare timestamps to now)
196
+ 2. Are there any tasks that depend on each other where order matters?
197
+ 3. Any tasks that may have produced stale results because they ran late?
198
+ 4. Should any task be re-run at its normal time today?
199
+
200
+ Write a brief assessment (max 20 lines) to: {assessment_file}
201
+
202
+ Format:
203
+ ## Catch-Up Assessment — {datetime.now().strftime('%Y-%m-%d %H:%M')}
204
+ - Offline duration: ~Xh
205
+ - Tasks caught up: {ran}
206
+ - Concerns: ...
207
+ - Recommendation: ..."""
208
+
209
+ log(f"Caught up {ran} tasks — running CLI assessment...")
210
+ env = os.environ.copy()
211
+ env.pop("CLAUDECODE", None)
212
+ env.pop("CLAUDE_CODE", None)
213
+
214
+ try:
215
+ result = subprocess.run(
216
+ [str(CLAUDE_CLI), "-p", prompt, "--model", "opus",
217
+ "--allowedTools", "Read,Write,Edit,Glob,Grep"],
218
+ capture_output=True, text=True, timeout=90, env=env
219
+ )
220
+ if result.returncode == 0:
221
+ log(f"Assessment written to {assessment_file}")
222
+ else:
223
+ log(f"CLI assessment exited {result.returncode}")
224
+ except subprocess.TimeoutExpired:
225
+ log("CLI assessment timed out (90s)")
226
+ except Exception as e:
227
+ log(f"CLI assessment error: {e}")
228
+
229
+
176
230
  if __name__ == "__main__":
177
231
  main()