nexo-brain 5.3.27 → 5.3.30
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/.claude-plugin/plugin.json +1 -1
- package/README.md +5 -1
- package/bin/nexo-brain.js +24 -7
- package/package.json +1 -1
- package/src/auto_update.py +37 -16
- package/src/cli.py +23 -0
- package/src/desktop_bridge.py +459 -0
- package/src/hook_guardrails.py +44 -0
- package/src/plugin_loader.py +5 -0
- package/src/plugins/update.py +5 -4
- package/src/scripts/nexo-cron-wrapper.sh +78 -22
- package/src/scripts/nexo-update.sh +14 -288
- package/src/server.py +140 -99
- package/src/tree_hygiene.py +56 -0
package/src/server.py
CHANGED
|
@@ -72,6 +72,140 @@ def _shutdown_handler(signum, frame):
|
|
|
72
72
|
sys.exit(0)
|
|
73
73
|
|
|
74
74
|
|
|
75
|
+
def _resolved_nexo_home() -> str:
|
|
76
|
+
return os.environ.get("NEXO_HOME", os.path.join(os.path.expanduser("~"), ".nexo"))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _data_dir() -> str:
|
|
80
|
+
return os.path.join(_resolved_nexo_home(), "data")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _backup_dir() -> str:
|
|
84
|
+
return os.path.join(_resolved_nexo_home(), "backups")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _allow_fresh_db_on_corruption() -> bool:
|
|
88
|
+
value = str(os.environ.get("NEXO_ALLOW_FRESH_DB_ON_CORRUPTION", "") or "").strip().lower()
|
|
89
|
+
return value in {"1", "true", "yes", "on"}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _quarantine_corrupt_db_file(db_path: str) -> None:
|
|
93
|
+
if os.path.exists(db_path):
|
|
94
|
+
corrupt_path = db_path + ".corrupt"
|
|
95
|
+
os.rename(db_path, corrupt_path)
|
|
96
|
+
print(f"[NEXO] Corrupt DB moved to {os.path.basename(corrupt_path)}", file=sys.stderr)
|
|
97
|
+
for ext in (".db-wal", ".db-shm"):
|
|
98
|
+
wal_path = db_path.replace(".db", ext)
|
|
99
|
+
if os.path.exists(wal_path):
|
|
100
|
+
os.remove(wal_path)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _restore_valid_db_backup() -> bool:
|
|
104
|
+
import glob
|
|
105
|
+
import shutil
|
|
106
|
+
import sqlite3
|
|
107
|
+
|
|
108
|
+
from db._core import DB_PATH as db_path
|
|
109
|
+
|
|
110
|
+
backups = sorted(glob.glob(os.path.join(_backup_dir(), "nexo-*.db")), reverse=True)
|
|
111
|
+
for backup_path in backups:
|
|
112
|
+
try:
|
|
113
|
+
test_conn = sqlite3.connect(backup_path)
|
|
114
|
+
integrity = test_conn.execute("PRAGMA integrity_check").fetchone()
|
|
115
|
+
test_conn.close()
|
|
116
|
+
if not integrity or integrity[0] != "ok":
|
|
117
|
+
continue
|
|
118
|
+
try:
|
|
119
|
+
close_db()
|
|
120
|
+
except Exception:
|
|
121
|
+
pass
|
|
122
|
+
shutil.copy2(backup_path, db_path)
|
|
123
|
+
print(f"[NEXO] Restored DB from backup: {os.path.basename(backup_path)}", file=sys.stderr)
|
|
124
|
+
init_db()
|
|
125
|
+
return True
|
|
126
|
+
except Exception:
|
|
127
|
+
continue
|
|
128
|
+
return False
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _init_db_or_exit() -> None:
|
|
132
|
+
import sqlite3
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
init_db()
|
|
136
|
+
return
|
|
137
|
+
except sqlite3.DatabaseError as exc:
|
|
138
|
+
print(f"[NEXO] DB init failed: {exc}", file=sys.stderr)
|
|
139
|
+
|
|
140
|
+
restored = False
|
|
141
|
+
try:
|
|
142
|
+
restored = _restore_valid_db_backup()
|
|
143
|
+
except Exception as restore_exc:
|
|
144
|
+
print(f"[NEXO] Backup restore failed: {restore_exc}", file=sys.stderr)
|
|
145
|
+
|
|
146
|
+
if restored:
|
|
147
|
+
return
|
|
148
|
+
|
|
149
|
+
try:
|
|
150
|
+
close_db()
|
|
151
|
+
except Exception:
|
|
152
|
+
pass
|
|
153
|
+
|
|
154
|
+
try:
|
|
155
|
+
from db._core import DB_PATH as db_path
|
|
156
|
+
_quarantine_corrupt_db_file(db_path)
|
|
157
|
+
except Exception:
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
if not _allow_fresh_db_on_corruption():
|
|
161
|
+
print(
|
|
162
|
+
"[NEXO] Refusing to create a fresh empty database automatically. "
|
|
163
|
+
"Restore a valid backup or set NEXO_ALLOW_FRESH_DB_ON_CORRUPTION=1 to override.",
|
|
164
|
+
file=sys.stderr,
|
|
165
|
+
)
|
|
166
|
+
sys.exit(1)
|
|
167
|
+
|
|
168
|
+
try:
|
|
169
|
+
init_db()
|
|
170
|
+
print("[NEXO] Fresh database created because override is enabled.", file=sys.stderr)
|
|
171
|
+
except Exception as fresh_exc:
|
|
172
|
+
print(f"[NEXO] FATAL: Cannot initialize database: {fresh_exc}", file=sys.stderr)
|
|
173
|
+
print("[NEXO] Check permissions on NEXO_HOME/data/ and disk space.", file=sys.stderr)
|
|
174
|
+
sys.exit(1)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _emit_startup_preflight_messages(result: dict) -> None:
|
|
178
|
+
if result.get("updated"):
|
|
179
|
+
print("[NEXO] Startup update applied.", file=sys.stderr)
|
|
180
|
+
if result.get("deferred_reason"):
|
|
181
|
+
print(f"[NEXO] Startup update deferred: {result['deferred_reason']}", file=sys.stderr)
|
|
182
|
+
if result.get("git_update"):
|
|
183
|
+
print(f"[NEXO] {result['git_update']}", file=sys.stderr)
|
|
184
|
+
if result.get("npm_notice"):
|
|
185
|
+
print(f"[NEXO] {result['npm_notice']}", file=sys.stderr)
|
|
186
|
+
if result.get("claude_md_update"):
|
|
187
|
+
print(f"[NEXO] {result['claude_md_update']}", file=sys.stderr)
|
|
188
|
+
for message in result.get("client_bootstrap_updates", []):
|
|
189
|
+
if message != result.get("claude_md_update"):
|
|
190
|
+
print(f"[NEXO] {message}", file=sys.stderr)
|
|
191
|
+
for migration in result.get("migrations", []):
|
|
192
|
+
if migration.get("status") == "failed":
|
|
193
|
+
print(
|
|
194
|
+
f"[NEXO] Migration {migration.get('file', '?')} FAILED: {migration.get('message', '')}",
|
|
195
|
+
file=sys.stderr,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _run_startup_preflight_sync() -> None:
|
|
200
|
+
try:
|
|
201
|
+
from auto_update import startup_preflight
|
|
202
|
+
|
|
203
|
+
result = startup_preflight(entrypoint="server", interactive=False)
|
|
204
|
+
_emit_startup_preflight_messages(result)
|
|
205
|
+
except Exception as e:
|
|
206
|
+
print(f"[NEXO auto-update] error: {e}", file=sys.stderr)
|
|
207
|
+
|
|
208
|
+
|
|
75
209
|
def _server_init():
|
|
76
210
|
"""Run all side effects: signals, PID, DB, auto-update, plugins.
|
|
77
211
|
|
|
@@ -81,110 +215,17 @@ def _server_init():
|
|
|
81
215
|
signal.signal(signal.SIGINT, _shutdown_handler)
|
|
82
216
|
|
|
83
217
|
# ── Write PID file for stale process detection ─────────────────
|
|
84
|
-
|
|
85
|
-
os.makedirs(
|
|
86
|
-
_pid_file = os.path.join(
|
|
218
|
+
data_dir = _data_dir()
|
|
219
|
+
os.makedirs(data_dir, exist_ok=True)
|
|
220
|
+
_pid_file = os.path.join(data_dir, "nexo.pid")
|
|
87
221
|
with open(_pid_file, "w") as f:
|
|
88
222
|
f.write(str(os.getpid()))
|
|
89
223
|
|
|
90
224
|
# ── Database initialization with recovery ─────────────────────
|
|
91
|
-
|
|
92
|
-
try:
|
|
93
|
-
init_db()
|
|
94
|
-
except sqlite3.DatabaseError as exc:
|
|
95
|
-
# Corruption or unreadable DB — attempt restore from backup
|
|
96
|
-
print(f"[NEXO] DB init failed: {exc}", file=sys.stderr)
|
|
97
|
-
_recovered = False
|
|
98
|
-
try:
|
|
99
|
-
from db._core import DB_PATH as _db_path
|
|
100
|
-
import glob as _glob
|
|
101
|
-
_backup_dir = os.path.join(
|
|
102
|
-
os.environ.get("NEXO_HOME", os.path.join(os.path.expanduser("~"), ".nexo")),
|
|
103
|
-
"backups",
|
|
104
|
-
)
|
|
105
|
-
_backups = sorted(_glob.glob(os.path.join(_backup_dir, "nexo-*.db")), reverse=True)
|
|
106
|
-
for _bk in _backups:
|
|
107
|
-
try:
|
|
108
|
-
_test = sqlite3.connect(_bk)
|
|
109
|
-
_result = _test.execute("PRAGMA integrity_check").fetchone()
|
|
110
|
-
_test.close()
|
|
111
|
-
if _result and _result[0] == "ok":
|
|
112
|
-
# Valid backup found — replace corrupt DB
|
|
113
|
-
import shutil
|
|
114
|
-
# Close any open connection before replacing
|
|
115
|
-
try:
|
|
116
|
-
close_db()
|
|
117
|
-
except Exception:
|
|
118
|
-
pass
|
|
119
|
-
shutil.copy2(_bk, _db_path)
|
|
120
|
-
print(f"[NEXO] Restored DB from backup: {os.path.basename(_bk)}", file=sys.stderr)
|
|
121
|
-
init_db()
|
|
122
|
-
_recovered = True
|
|
123
|
-
break
|
|
124
|
-
except Exception:
|
|
125
|
-
continue
|
|
126
|
-
except Exception as restore_exc:
|
|
127
|
-
print(f"[NEXO] Backup restore failed: {restore_exc}", file=sys.stderr)
|
|
128
|
-
|
|
129
|
-
if not _recovered:
|
|
130
|
-
# No valid backup — nuke corrupt file and start fresh
|
|
131
|
-
try:
|
|
132
|
-
close_db()
|
|
133
|
-
except Exception:
|
|
134
|
-
pass
|
|
135
|
-
try:
|
|
136
|
-
from db._core import DB_PATH as _db_path
|
|
137
|
-
if os.path.exists(_db_path):
|
|
138
|
-
_corrupt_path = _db_path + ".corrupt"
|
|
139
|
-
os.rename(_db_path, _corrupt_path)
|
|
140
|
-
print(f"[NEXO] Corrupt DB moved to {os.path.basename(_corrupt_path)}", file=sys.stderr)
|
|
141
|
-
# Remove WAL/SHM files too
|
|
142
|
-
for _ext in (".db-wal", ".db-shm"):
|
|
143
|
-
_wal = _db_path.replace(".db", _ext)
|
|
144
|
-
if os.path.exists(_wal):
|
|
145
|
-
os.remove(_wal)
|
|
146
|
-
except Exception:
|
|
147
|
-
pass
|
|
148
|
-
try:
|
|
149
|
-
init_db()
|
|
150
|
-
print("[NEXO] Fresh database created.", file=sys.stderr)
|
|
151
|
-
except Exception as fresh_exc:
|
|
152
|
-
print(f"[NEXO] FATAL: Cannot initialize database: {fresh_exc}", file=sys.stderr)
|
|
153
|
-
print("[NEXO] Check permissions on NEXO_HOME/data/ and disk space.", file=sys.stderr)
|
|
154
|
-
sys.exit(1)
|
|
155
|
-
|
|
156
|
-
# ── Auto-update check (non-blocking, max 5s) ──────────────────
|
|
157
|
-
try:
|
|
158
|
-
from auto_update import startup_preflight
|
|
159
|
-
import threading
|
|
225
|
+
_init_db_or_exit()
|
|
160
226
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
result = startup_preflight(entrypoint="server", interactive=False)
|
|
164
|
-
if result.get("updated"):
|
|
165
|
-
print("[NEXO] Startup update applied.", file=sys.stderr)
|
|
166
|
-
if result.get("deferred_reason"):
|
|
167
|
-
print(f"[NEXO] Startup update deferred: {result['deferred_reason']}", file=sys.stderr)
|
|
168
|
-
if result.get("git_update"):
|
|
169
|
-
print(f"[NEXO] {result['git_update']}", file=sys.stderr)
|
|
170
|
-
if result.get("npm_notice"):
|
|
171
|
-
print(f"[NEXO] {result['npm_notice']}", file=sys.stderr)
|
|
172
|
-
if result.get("claude_md_update"):
|
|
173
|
-
print(f"[NEXO] {result['claude_md_update']}", file=sys.stderr)
|
|
174
|
-
for message in result.get("client_bootstrap_updates", []):
|
|
175
|
-
if message != result.get("claude_md_update"):
|
|
176
|
-
print(f"[NEXO] {message}", file=sys.stderr)
|
|
177
|
-
for m in result.get("migrations", []):
|
|
178
|
-
if m["status"] == "failed":
|
|
179
|
-
print(f"[NEXO] Migration {m['file']} FAILED: {m['message']}", file=sys.stderr)
|
|
180
|
-
except Exception as e:
|
|
181
|
-
print(f"[NEXO auto-update] error: {e}", file=sys.stderr)
|
|
182
|
-
|
|
183
|
-
_update_thread = threading.Thread(target=_bg_update, daemon=True)
|
|
184
|
-
_update_thread.start()
|
|
185
|
-
_update_thread.join(timeout=5) # Wait at most 5 seconds
|
|
186
|
-
except Exception:
|
|
187
|
-
pass # Never break startup
|
|
227
|
+
# ── Auto-update / startup preflight (synchronous) ─────────────
|
|
228
|
+
_run_startup_preflight_sync()
|
|
188
229
|
|
|
189
230
|
# ── Load plugins ───────────────────────────────────────────────
|
|
190
231
|
load_all_plugins(mcp)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""Shared tree hygiene helpers for runtime/install/release flows."""
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_DUPLICATE_COPY_RE = re.compile(r"^(?P<base>.+) (?P<copy>[2-9]\d*)$")
|
|
10
|
+
_IGNORED_DIRS = {
|
|
11
|
+
".git",
|
|
12
|
+
".hg",
|
|
13
|
+
".svn",
|
|
14
|
+
".venv",
|
|
15
|
+
"__pycache__",
|
|
16
|
+
".pytest_cache",
|
|
17
|
+
".mypy_cache",
|
|
18
|
+
"node_modules",
|
|
19
|
+
"dist",
|
|
20
|
+
"build",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def canonical_artifact_name(name: str) -> str | None:
|
|
25
|
+
"""Return the canonical sibling name for a macOS-style duplicate copy."""
|
|
26
|
+
path = Path(name)
|
|
27
|
+
match = _DUPLICATE_COPY_RE.match(path.stem)
|
|
28
|
+
if not match:
|
|
29
|
+
return None
|
|
30
|
+
return f"{match.group('base')}{path.suffix}"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def is_duplicate_artifact_name(path_like: str | Path) -> bool:
|
|
34
|
+
"""True when the path looks like a duplicate copy and its canonical sibling exists."""
|
|
35
|
+
path = Path(path_like)
|
|
36
|
+
canonical_name = canonical_artifact_name(path.name)
|
|
37
|
+
if canonical_name is None:
|
|
38
|
+
return False
|
|
39
|
+
parent = path.parent
|
|
40
|
+
if str(parent) in {"", "."} and not path.is_absolute():
|
|
41
|
+
return False
|
|
42
|
+
return path.with_name(canonical_name).exists()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def find_duplicate_artifact_paths(root: str | Path) -> list[Path]:
|
|
46
|
+
"""Find duplicate copy artifacts under a tree, skipping generated/vendor directories."""
|
|
47
|
+
root_path = Path(root).resolve()
|
|
48
|
+
duplicates: list[Path] = []
|
|
49
|
+
for path in sorted(root_path.rglob("*")):
|
|
50
|
+
if any(part in _IGNORED_DIRS for part in path.parts):
|
|
51
|
+
continue
|
|
52
|
+
if not path.is_file():
|
|
53
|
+
continue
|
|
54
|
+
if is_duplicate_artifact_name(path):
|
|
55
|
+
duplicates.append(path)
|
|
56
|
+
return duplicates
|