nexo-brain 5.3.28 → 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/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
|
@@ -1,306 +1,32 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
# nexo-update.sh —
|
|
3
|
-
# Same logic as the MCP tool but usable when the server itself needs updating.
|
|
4
|
-
#
|
|
2
|
+
# nexo-update.sh — Thin wrapper around the canonical Python update core.
|
|
5
3
|
# Usage:
|
|
6
4
|
# nexo-update.sh # pull from origin main
|
|
7
5
|
# nexo-update.sh origin beta # pull from origin beta
|
|
8
|
-
# NEXO_HOME=/path nexo-update.sh # custom NEXO_HOME
|
|
9
6
|
|
|
10
7
|
set -euo pipefail
|
|
11
8
|
|
|
12
|
-
# --- Configuration ---
|
|
13
9
|
REMOTE="${1:-origin}"
|
|
14
10
|
BRANCH="${2:-main}"
|
|
15
|
-
NEXO_HOME="${NEXO_HOME:-$HOME/.nexo}"
|
|
16
|
-
|
|
17
|
-
# Determine repo directory: script is at src/scripts/, repo root is ../../
|
|
18
11
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
PACKAGE_JSON="$REPO_DIR/package.json"
|
|
22
|
-
|
|
23
|
-
# --- Helpers ---
|
|
24
|
-
RED='\033[0;31m'
|
|
25
|
-
GREEN='\033[0;32m'
|
|
26
|
-
YELLOW='\033[1;33m'
|
|
27
|
-
NC='\033[0m'
|
|
28
|
-
|
|
29
|
-
log() { echo -e "${GREEN}[nexo-update]${NC} $*"; }
|
|
30
|
-
warn() { echo -e "${YELLOW}[nexo-update]${NC} $*"; }
|
|
31
|
-
err() { echo -e "${RED}[nexo-update]${NC} $*" >&2; }
|
|
32
|
-
|
|
33
|
-
read_version() {
|
|
34
|
-
python3 -c "import json; print(json.load(open('$PACKAGE_JSON')).get('version','unknown'))" 2>/dev/null || echo "unknown"
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
# --- Check if this is a git repo ---
|
|
38
|
-
if [ ! -d "$REPO_DIR/.git" ] && [ ! -f "$REPO_DIR/.git" ]; then
|
|
39
|
-
err "ABORTED: Not a git repository at $REPO_DIR"
|
|
40
|
-
err "For packaged installs, use: npm update -g nexo-brain"
|
|
41
|
-
exit 1
|
|
42
|
-
fi
|
|
43
|
-
|
|
44
|
-
# --- Step 1: Check for uncommitted changes in entire worktree ---
|
|
45
|
-
log "Checking for uncommitted changes..."
|
|
46
|
-
cd "$REPO_DIR"
|
|
47
|
-
|
|
48
|
-
if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
|
|
49
|
-
err "ABORTED: Uncommitted changes in worktree"
|
|
50
|
-
git status --short
|
|
51
|
-
exit 1
|
|
52
|
-
fi
|
|
53
|
-
log "Working tree clean."
|
|
54
|
-
|
|
55
|
-
# Record current state
|
|
56
|
-
OLD_VERSION="$(read_version)"
|
|
57
|
-
OLD_COMMIT="$(git rev-parse HEAD)"
|
|
58
|
-
REQ_FILE="$SRC_DIR/requirements.txt"
|
|
59
|
-
OLD_REQ_HASH=""
|
|
60
|
-
if [ -f "$REQ_FILE" ]; then
|
|
61
|
-
OLD_REQ_HASH="$(shasum -a 256 "$REQ_FILE" | cut -d' ' -f1)"
|
|
62
|
-
fi
|
|
63
|
-
log "Current: v${OLD_VERSION} (${OLD_COMMIT:0:8})"
|
|
64
|
-
|
|
65
|
-
# --- Step 2: Backup databases ---
|
|
66
|
-
TIMESTAMP="$(date +%Y-%m-%d-%H%M)"
|
|
67
|
-
BACKUP_DIR="$NEXO_HOME/backups/pre-update-$TIMESTAMP"
|
|
68
|
-
|
|
69
|
-
backup_dbs() {
|
|
70
|
-
local found=0
|
|
71
|
-
# Check data/, NEXO_HOME root, and src/ for .db files
|
|
72
|
-
for dir in "$NEXO_HOME/data" "$NEXO_HOME" "$SRC_DIR"; do
|
|
73
|
-
if [ -d "$dir" ]; then
|
|
74
|
-
for db in "$dir"/*.db; do
|
|
75
|
-
[ -f "$db" ] || continue
|
|
76
|
-
found=1
|
|
77
|
-
mkdir -p "$BACKUP_DIR"
|
|
78
|
-
cp "$db" "$BACKUP_DIR/$(basename "$db")"
|
|
79
|
-
log " Backed up: $(basename "$db")"
|
|
80
|
-
done
|
|
81
|
-
fi
|
|
82
|
-
done
|
|
83
|
-
if [ "$found" -eq 0 ]; then
|
|
84
|
-
log " No databases found to backup."
|
|
85
|
-
fi
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
log "Backing up databases..."
|
|
89
|
-
backup_dbs
|
|
12
|
+
SRC_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
13
|
+
RUNTIME_PYTHON="${NEXO_RUNTIME_PYTHON:-python3}"
|
|
90
14
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
PULL_OUTPUT="$(git pull "$REMOTE" "$BRANCH" 2>&1)" || {
|
|
94
|
-
err "git pull failed:"
|
|
95
|
-
err "$PULL_OUTPUT"
|
|
96
|
-
exit 1
|
|
97
|
-
}
|
|
98
|
-
log "$PULL_OUTPUT"
|
|
15
|
+
export NEXO_CODE="${NEXO_CODE:-$SRC_DIR}"
|
|
16
|
+
export PYTHONPATH="$SRC_DIR${PYTHONPATH:+:$PYTHONPATH}"
|
|
99
17
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
exit 0
|
|
103
|
-
fi
|
|
18
|
+
"$RUNTIME_PYTHON" - "$REMOTE" "$BRANCH" <<'PY'
|
|
19
|
+
from __future__ import annotations
|
|
104
20
|
|
|
105
|
-
|
|
106
|
-
NEW_VERSION="$(read_version)"
|
|
107
|
-
log "New version: v${NEW_VERSION}"
|
|
108
|
-
|
|
109
|
-
# --- Step 4b: Reinstall Python dependencies if requirements.txt changed ---
|
|
110
|
-
NEW_REQ_HASH=""
|
|
111
|
-
if [ -f "$REQ_FILE" ]; then
|
|
112
|
-
NEW_REQ_HASH="$(shasum -a 256 "$REQ_FILE" | cut -d' ' -f1)"
|
|
113
|
-
fi
|
|
114
|
-
|
|
115
|
-
DEPS_CHANGED=false
|
|
116
|
-
if [ "$OLD_REQ_HASH" != "$NEW_REQ_HASH" ]; then
|
|
117
|
-
DEPS_CHANGED=true
|
|
118
|
-
fi
|
|
119
|
-
|
|
120
|
-
reinstall_pip_deps() {
|
|
121
|
-
local VENV_PIP="$NEXO_HOME/.venv/bin/pip"
|
|
122
|
-
if [ -f "$REQ_FILE" ]; then
|
|
123
|
-
if [ -x "$VENV_PIP" ]; then
|
|
124
|
-
"$VENV_PIP" install --quiet -r "$REQ_FILE" || return 1
|
|
125
|
-
else
|
|
126
|
-
python3 -m pip install --quiet -r "$REQ_FILE" --break-system-packages 2>/dev/null || return 1
|
|
127
|
-
fi
|
|
128
|
-
fi
|
|
129
|
-
return 0
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if [ "$DEPS_CHANGED" = true ] || [ "$OLD_VERSION" != "$NEW_VERSION" ]; then
|
|
133
|
-
log "Reinstalling Python dependencies..."
|
|
134
|
-
if ! reinstall_pip_deps; then
|
|
135
|
-
err "pip install failed! Rolling back..."
|
|
136
|
-
git reset --hard "$OLD_COMMIT"
|
|
137
|
-
reinstall_pip_deps || warn "pip rollback also had issues"
|
|
138
|
-
if [ -d "$BACKUP_DIR" ]; then
|
|
139
|
-
for db in "$BACKUP_DIR"/*.db; do
|
|
140
|
-
[ -f "$db" ] || continue
|
|
141
|
-
BASENAME="$(basename "$db")"
|
|
142
|
-
for candidate in "$NEXO_HOME/data/$BASENAME" "$NEXO_HOME/$BASENAME" "$SRC_DIR/$BASENAME"; do
|
|
143
|
-
if [ -f "$candidate" ]; then
|
|
144
|
-
cp "$db" "$candidate"
|
|
145
|
-
warn " Restored: $BASENAME"
|
|
146
|
-
break
|
|
147
|
-
fi
|
|
148
|
-
done
|
|
149
|
-
done
|
|
150
|
-
fi
|
|
151
|
-
err "Rolled back to ${OLD_COMMIT:0:8}. Databases restored."
|
|
152
|
-
exit 1
|
|
153
|
-
fi
|
|
154
|
-
log "Python dependencies updated."
|
|
155
|
-
fi
|
|
156
|
-
|
|
157
|
-
# --- Step 5: Run migrations if version changed ---
|
|
158
|
-
if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then
|
|
159
|
-
log "Version changed: ${OLD_VERSION} -> ${NEW_VERSION}"
|
|
160
|
-
log "Running migrations..."
|
|
161
|
-
if ! (cd "$SRC_DIR" && python3 -c "import db; db.init_db()" 2>&1); then
|
|
162
|
-
err "Migration failed! Rolling back..."
|
|
163
|
-
git reset --hard "$OLD_COMMIT"
|
|
164
|
-
# Reinstall pip deps from restored old requirements.txt
|
|
165
|
-
reinstall_pip_deps || warn "pip rollback also had issues"
|
|
166
|
-
# Restore DB backups
|
|
167
|
-
if [ -d "$BACKUP_DIR" ]; then
|
|
168
|
-
for db in "$BACKUP_DIR"/*.db; do
|
|
169
|
-
[ -f "$db" ] || continue
|
|
170
|
-
BASENAME="$(basename "$db")"
|
|
171
|
-
for candidate in "$NEXO_HOME/data/$BASENAME" "$NEXO_HOME/$BASENAME" "$SRC_DIR/$BASENAME"; do
|
|
172
|
-
if [ -f "$candidate" ]; then
|
|
173
|
-
cp "$db" "$candidate"
|
|
174
|
-
warn " Restored: $BASENAME"
|
|
175
|
-
break
|
|
176
|
-
fi
|
|
177
|
-
done
|
|
178
|
-
done
|
|
179
|
-
fi
|
|
180
|
-
err "Rolled back to ${OLD_COMMIT:0:8}. Databases and deps restored."
|
|
181
|
-
exit 1
|
|
182
|
-
fi
|
|
183
|
-
log "Migrations applied."
|
|
184
|
-
else
|
|
185
|
-
log "Version unchanged (${OLD_VERSION}), skipping migrations."
|
|
186
|
-
fi
|
|
187
|
-
|
|
188
|
-
# --- Step 6: Verify import ---
|
|
189
|
-
log "Verifying server.py import..."
|
|
190
|
-
if ! (cd "$SRC_DIR" && python3 -c "import server" 2>&1); then
|
|
191
|
-
err "Import verification failed! Rolling back..."
|
|
192
|
-
git reset --hard "$OLD_COMMIT"
|
|
193
|
-
# Reinstall pip deps from restored old requirements.txt
|
|
194
|
-
reinstall_pip_deps || warn "pip rollback also had issues"
|
|
195
|
-
if [ -d "$BACKUP_DIR" ]; then
|
|
196
|
-
for db in "$BACKUP_DIR"/*.db; do
|
|
197
|
-
[ -f "$db" ] || continue
|
|
198
|
-
BASENAME="$(basename "$db")"
|
|
199
|
-
for candidate in "$NEXO_HOME/data/$BASENAME" "$NEXO_HOME/$BASENAME" "$SRC_DIR/$BASENAME"; do
|
|
200
|
-
if [ -f "$candidate" ]; then
|
|
201
|
-
cp "$db" "$candidate"
|
|
202
|
-
warn " Restored: $BASENAME"
|
|
203
|
-
break
|
|
204
|
-
fi
|
|
205
|
-
done
|
|
206
|
-
done
|
|
207
|
-
fi
|
|
208
|
-
err "Rolled back to ${OLD_COMMIT:0:8}. Databases and deps restored."
|
|
209
|
-
exit 1
|
|
210
|
-
fi
|
|
211
|
-
|
|
212
|
-
# --- Step 7: Sync hooks to NEXO_HOME ---
|
|
213
|
-
HOOKS_SRC="$SRC_DIR/hooks"
|
|
214
|
-
HOOKS_DEST="$NEXO_HOME/hooks"
|
|
215
|
-
if [ -d "$HOOKS_SRC" ]; then
|
|
216
|
-
mkdir -p "$HOOKS_DEST"
|
|
217
|
-
SYNCED=0
|
|
218
|
-
for hook in "$HOOKS_SRC"/*.sh; do
|
|
219
|
-
[ -f "$hook" ] || continue
|
|
220
|
-
cp "$hook" "$HOOKS_DEST/$(basename "$hook")"
|
|
221
|
-
chmod 755 "$HOOKS_DEST/$(basename "$hook")"
|
|
222
|
-
SYNCED=$((SYNCED + 1))
|
|
223
|
-
done
|
|
224
|
-
if [ "$SYNCED" -gt 0 ]; then
|
|
225
|
-
log "Synced $SYNCED hook(s) to $HOOKS_DEST"
|
|
226
|
-
fi
|
|
227
|
-
fi
|
|
21
|
+
import sys
|
|
228
22
|
|
|
229
|
-
|
|
230
|
-
CRON_SYNC="$SRC_DIR/crons/sync.py"
|
|
231
|
-
CRON_SYNC_OK=false
|
|
232
|
-
if [ -f "$CRON_SYNC" ]; then
|
|
233
|
-
log "Syncing cron definitions..."
|
|
234
|
-
if NEXO_HOME="$NEXO_HOME" NEXO_CODE="$SRC_DIR" python3 "$CRON_SYNC" 2>&1; then
|
|
235
|
-
log "Cron definitions synced."
|
|
236
|
-
CRON_SYNC_OK=true
|
|
237
|
-
else
|
|
238
|
-
warn "Cron sync failed (non-fatal). Installed manifest NOT refreshed to avoid divergence."
|
|
239
|
-
fi
|
|
240
|
-
fi
|
|
23
|
+
from plugins.update import handle_update
|
|
241
24
|
|
|
242
|
-
# --- Step 8b: Refresh installed manifest for catchup/watchdog (only if sync succeeded) ---
|
|
243
|
-
if $CRON_SYNC_OK && [ -d "$SRC_DIR/crons" ]; then
|
|
244
|
-
mkdir -p "$NEXO_HOME/crons"
|
|
245
|
-
cp -f "$SRC_DIR/crons/"*.json "$NEXO_HOME/crons/" 2>/dev/null
|
|
246
|
-
cp -f "$SRC_DIR/crons/"*.py "$NEXO_HOME/crons/" 2>/dev/null
|
|
247
|
-
log "Refreshed installed crons manifest."
|
|
248
|
-
fi
|
|
249
25
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
CLIENT_SYNC_ARGS=()
|
|
255
|
-
if [ -f "$NEXO_HOME/config/schedule.json" ]; then
|
|
256
|
-
while IFS= read -r line; do
|
|
257
|
-
[ -n "$line" ] && CLIENT_SYNC_ARGS+=("--enabled-client" "$line")
|
|
258
|
-
done < <(
|
|
259
|
-
NEXO_HOME="$NEXO_HOME" NEXO_CODE="$SRC_DIR" python3 - <<'PY'
|
|
260
|
-
import json
|
|
261
|
-
import os
|
|
262
|
-
import sys
|
|
263
|
-
from pathlib import Path
|
|
26
|
+
remote = sys.argv[1]
|
|
27
|
+
branch = sys.argv[2]
|
|
28
|
+
result = handle_update(remote=remote, branch=branch)
|
|
29
|
+
print(result)
|
|
264
30
|
|
|
265
|
-
|
|
266
|
-
from client_preferences import normalize_client_preferences
|
|
267
|
-
|
|
268
|
-
schedule_file = Path(os.environ["NEXO_HOME"]) / "config" / "schedule.json"
|
|
269
|
-
schedule_payload = json.loads(schedule_file.read_text()) if schedule_file.exists() else {}
|
|
270
|
-
prefs = normalize_client_preferences(schedule_payload)
|
|
271
|
-
if prefs != {key: schedule_payload.get(key) for key in prefs}:
|
|
272
|
-
merged = dict(schedule_payload)
|
|
273
|
-
merged.update(prefs)
|
|
274
|
-
schedule_file.parent.mkdir(parents=True, exist_ok=True)
|
|
275
|
-
schedule_file.write_text(json.dumps(merged, indent=2, ensure_ascii=False) + "\n")
|
|
276
|
-
enabled = [key for key, value in prefs.get("interactive_clients", {}).items() if value]
|
|
277
|
-
if prefs.get("automation_enabled", True):
|
|
278
|
-
backend = prefs.get("automation_backend")
|
|
279
|
-
if backend and backend != "none" and backend not in enabled:
|
|
280
|
-
enabled.append(backend)
|
|
281
|
-
for item in enabled:
|
|
282
|
-
print(item)
|
|
31
|
+
raise SystemExit(1 if result.startswith(("ABORTED", "UPDATE FAILED")) else 0)
|
|
283
32
|
PY
|
|
284
|
-
)
|
|
285
|
-
fi
|
|
286
|
-
if NEXO_HOME="$NEXO_HOME" NEXO_CODE="$SRC_DIR" python3 "$CLIENT_SYNC" --nexo-home "$NEXO_HOME" --runtime-root "$SRC_DIR" "${CLIENT_SYNC_ARGS[@]}" --json >/dev/null 2>&1; then
|
|
287
|
-
log "Shared client configs synced."
|
|
288
|
-
else
|
|
289
|
-
warn "Client config sync failed (non-fatal). Run 'nexo clients sync' later."
|
|
290
|
-
fi
|
|
291
|
-
fi
|
|
292
|
-
|
|
293
|
-
# --- Done ---
|
|
294
|
-
echo ""
|
|
295
|
-
log "========================================="
|
|
296
|
-
log " UPDATE SUCCESSFUL"
|
|
297
|
-
if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then
|
|
298
|
-
log " Version: ${OLD_VERSION} -> ${NEW_VERSION}"
|
|
299
|
-
else
|
|
300
|
-
log " Version: ${OLD_VERSION} (unchanged)"
|
|
301
|
-
fi
|
|
302
|
-
log " Branch: ${REMOTE}/${BRANCH}"
|
|
303
|
-
log " Backup: ${BACKUP_DIR}"
|
|
304
|
-
log "========================================="
|
|
305
|
-
echo ""
|
|
306
|
-
warn "MCP server restart needed to load new code."
|
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
|