nexo-brain 7.20.25 → 7.21.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/.claude-plugin/plugin.json +1 -1
- package/README.md +3 -1
- package/bin/nexo-brain.js +21 -1
- package/package.json +1 -1
- package/src/auto_update.py +1 -1
- package/src/local_context/api.py +59 -1
- package/src/runtime_service.py +426 -0
- package/src/runtime_versioning.py +11 -0
- package/src/server.py +42 -2
- package/tool-enforcement-map.json +15 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.21.0",
|
|
4
4
|
"description": "Local cognitive runtime for Claude Code \u2014 persistent memory, overnight learning, doctor diagnostics, personal scripts, recovery-aware jobs, startup preflight, and optional dashboard/power helper.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "NEXO Brain",
|
package/README.md
CHANGED
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
|
|
19
19
|
[Watch the overview video](https://nexo-brain.com/watch/) · [Watch on YouTube](https://www.youtube.com/watch?v=i2lkGhKyVqI) · [Open the infographic](https://nexo-brain.com/assets/nexo-brain-infographic-v5.png)
|
|
20
20
|
|
|
21
|
-
Version `7.
|
|
21
|
+
Version `7.21.0` is the current packaged-runtime line. Minor release over v7.20.25 - MCP now starts through a thin compatibility adapter backed by one resident local Runtime Service, reducing duplicate Brain processes and SQLite contention across Claude Code, Codex, Claude Desktop, and NEXO Desktop. The release also fingerprints Runtime Service state for safe update cutover, keeps document-first Local Memory scanning, and verifies bundled local LLM files before marking them installed.
|
|
22
|
+
|
|
23
|
+
Previously in `7.20.25`: patch release over v7.20.24 — Local Context now uses the pinned local BGE embedding model when available, automatically refreshes old hash embeddings, prioritizes known documents before lower-value files, and treats the Desktop-owned Qwen local-presence model as optional in standalone Brain installs.
|
|
22
24
|
|
|
23
25
|
Previously in `7.20.24`: patch release over v7.20.23 — Local Memory performance profile writes now tolerate active indexing, retry transient SQLite busy states, and shorten indexer write locks between processed files.
|
|
24
26
|
|
package/bin/nexo-brain.js
CHANGED
|
@@ -3879,12 +3879,32 @@ async function runSetup() {
|
|
|
3879
3879
|
const slug = (spec.name || "").trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
3880
3880
|
const targetDir = path.join(runtimeModelsDir, slug, spec.revision);
|
|
3881
3881
|
fs.mkdirSync(targetDir, { recursive: true });
|
|
3882
|
+
const missingFiles = [];
|
|
3882
3883
|
for (const f of (spec.required_files || [])) {
|
|
3883
3884
|
const src = path.join(sourceDir, f.path);
|
|
3884
3885
|
const dst = path.join(targetDir, f.path);
|
|
3885
|
-
if (fs.existsSync(src)
|
|
3886
|
+
if (!fs.existsSync(src)) {
|
|
3887
|
+
missingFiles.push(f.path);
|
|
3888
|
+
continue;
|
|
3889
|
+
}
|
|
3890
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
3891
|
+
if (!fs.existsSync(dst) || (f.size && fs.statSync(dst).size !== f.size)) {
|
|
3886
3892
|
fs.copyFileSync(src, dst);
|
|
3887
3893
|
}
|
|
3894
|
+
if (f.size && fs.statSync(dst).size !== f.size) {
|
|
3895
|
+
missingFiles.push(`${f.path}:size`);
|
|
3896
|
+
continue;
|
|
3897
|
+
}
|
|
3898
|
+
if (f.sha256) {
|
|
3899
|
+
const actual = crypto.createHash("sha256").update(fs.readFileSync(dst)).digest("hex");
|
|
3900
|
+
if (actual !== f.sha256) {
|
|
3901
|
+
missingFiles.push(`${f.path}:sha256`);
|
|
3902
|
+
}
|
|
3903
|
+
}
|
|
3904
|
+
}
|
|
3905
|
+
if (missingFiles.length) {
|
|
3906
|
+
log(` WARN: bundled LLM model ${spec.name} incomplete (${missingFiles.join(", ")})`);
|
|
3907
|
+
continue;
|
|
3888
3908
|
}
|
|
3889
3909
|
// Write the lock file to match revision (avoids re-download).
|
|
3890
3910
|
fs.writeFileSync(path.join(targetDir, ".nexo-model-lock.json"), JSON.stringify({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.21.0",
|
|
4
4
|
"mcpName": "io.github.wazionapps/nexo",
|
|
5
5
|
"description": "NEXO Brain — Shared brain for AI agents. Persistent memory, semantic RAG, natural forgetting, metacognitive guard, trust scoring, 150+ MCP tools. Works with Claude Code, Codex, Claude Desktop & any MCP client. 100% local, free.",
|
|
6
6
|
"homepage": "https://nexo-brain.com",
|
package/src/auto_update.py
CHANGED
|
@@ -3988,7 +3988,7 @@ def _auto_update_check_locked() -> dict:
|
|
|
3988
3988
|
|
|
3989
3989
|
# Backfill runtime CLI modules for existing installs
|
|
3990
3990
|
try:
|
|
3991
|
-
for fname in ("cli.py", "script_registry.py", "skills_runtime.py", "cron_recovery.py", "client_preferences.py", "claude_cli.py", "agent_runner.py", "bootstrap_docs.py", "mcp_required_tools.py"):
|
|
3991
|
+
for fname in ("cli.py", "script_registry.py", "skills_runtime.py", "cron_recovery.py", "client_preferences.py", "claude_cli.py", "agent_runner.py", "bootstrap_docs.py", "mcp_required_tools.py", "runtime_service.py"):
|
|
3992
3992
|
src_file = SRC_DIR / fname
|
|
3993
3993
|
dest_file = NEXO_HOME / fname
|
|
3994
3994
|
if src_file.is_file() and (not dest_file.exists() or src_file.stat().st_mtime > dest_file.stat().st_mtime):
|
package/src/local_context/api.py
CHANGED
|
@@ -71,6 +71,47 @@ EMAIL_DOCUMENT_SUFFIXES = {
|
|
|
71
71
|
".emlx",
|
|
72
72
|
".msg",
|
|
73
73
|
}
|
|
74
|
+
HIGH_VALUE_DIRECTORY_NAMES = {
|
|
75
|
+
"users",
|
|
76
|
+
"home",
|
|
77
|
+
"desktop",
|
|
78
|
+
"documents",
|
|
79
|
+
"downloads",
|
|
80
|
+
"documentos",
|
|
81
|
+
"escritorio",
|
|
82
|
+
"descargas",
|
|
83
|
+
"icloud drive",
|
|
84
|
+
"onedrive",
|
|
85
|
+
"google drive",
|
|
86
|
+
"dropbox",
|
|
87
|
+
"creative cloud files",
|
|
88
|
+
"clientes",
|
|
89
|
+
"clients",
|
|
90
|
+
"facturas",
|
|
91
|
+
"invoices",
|
|
92
|
+
"contratos",
|
|
93
|
+
"contracts",
|
|
94
|
+
"projects",
|
|
95
|
+
"proyectos",
|
|
96
|
+
"work",
|
|
97
|
+
"trabajo",
|
|
98
|
+
}
|
|
99
|
+
LOW_VALUE_DIRECTORY_NAMES = {
|
|
100
|
+
"applications",
|
|
101
|
+
"library",
|
|
102
|
+
"system",
|
|
103
|
+
"private",
|
|
104
|
+
"usr",
|
|
105
|
+
"var",
|
|
106
|
+
"opt",
|
|
107
|
+
"windows",
|
|
108
|
+
"program files",
|
|
109
|
+
"program files (x86)",
|
|
110
|
+
"programdata",
|
|
111
|
+
"appdata",
|
|
112
|
+
".cache",
|
|
113
|
+
"caches",
|
|
114
|
+
}
|
|
74
115
|
RERANKER_MODEL_SPEC = "cross-encoder-reranker"
|
|
75
116
|
PERFORMANCE_PROFILES: dict[str, dict[str, Any]] = {
|
|
76
117
|
"low": {
|
|
@@ -1247,12 +1288,29 @@ def _extraction_priority(path: Path) -> int:
|
|
|
1247
1288
|
return 45
|
|
1248
1289
|
|
|
1249
1290
|
|
|
1291
|
+
def _directory_scan_priority(path: Path) -> int:
|
|
1292
|
+
name = path.name.strip().lower()
|
|
1293
|
+
if name in {"users", "home"}:
|
|
1294
|
+
return 0
|
|
1295
|
+
if name in HIGH_VALUE_DIRECTORY_NAMES:
|
|
1296
|
+
return 10
|
|
1297
|
+
if "icloud" in name or "onedrive" in name or "google drive" in name:
|
|
1298
|
+
return 10
|
|
1299
|
+
if is_local_email_tree(str(path)):
|
|
1300
|
+
return 65
|
|
1301
|
+
if name in LOW_VALUE_DIRECTORY_NAMES:
|
|
1302
|
+
return 90
|
|
1303
|
+
return 40
|
|
1304
|
+
|
|
1305
|
+
|
|
1250
1306
|
def _scan_entry_sort_key(item: Path) -> tuple[int, int, str]:
|
|
1251
1307
|
try:
|
|
1252
1308
|
is_file = item.is_file()
|
|
1253
1309
|
except Exception:
|
|
1254
1310
|
is_file = False
|
|
1255
|
-
|
|
1311
|
+
if is_file:
|
|
1312
|
+
return (1, -_extraction_priority(item), str(item).lower())
|
|
1313
|
+
return (0, _directory_scan_priority(item), str(item).lower())
|
|
1256
1314
|
|
|
1257
1315
|
|
|
1258
1316
|
def _iter_files(
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
"""Resident runtime service and MCP proxy bootstrap.
|
|
3
|
+
|
|
4
|
+
The public MCP entrypoint remains ``server.py`` for compatibility. By
|
|
5
|
+
default, that entrypoint becomes a thin stdio proxy and forwards calls to a
|
|
6
|
+
single resident FastMCP service over loopback HTTP. The resident process is
|
|
7
|
+
the only MCP process that initializes Brain, opens SQLite, and runs tool
|
|
8
|
+
handlers.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import signal
|
|
15
|
+
import socket
|
|
16
|
+
import subprocess
|
|
17
|
+
import sys
|
|
18
|
+
import time
|
|
19
|
+
from contextlib import contextmanager
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
import paths
|
|
24
|
+
|
|
25
|
+
DEFAULT_HOST = "127.0.0.1"
|
|
26
|
+
DEFAULT_PORT = 17872
|
|
27
|
+
PORT_SCAN_LIMIT = 30
|
|
28
|
+
SERVICE_PATH = "/mcp"
|
|
29
|
+
SERVICE_ENV = "NEXO_RUNTIME_SERVICE"
|
|
30
|
+
DIRECT_ENV = "NEXO_MCP_DIRECT"
|
|
31
|
+
ADAPTER_ENV = "NEXO_MCP_RUNTIME_ADAPTER"
|
|
32
|
+
STATE_FILE = "runtime-service.json"
|
|
33
|
+
LOCK_FILE = "runtime-service.lock"
|
|
34
|
+
LOG_FILE = "runtime-service.log"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def env_flag(name: str, *, default: bool = False) -> bool:
|
|
38
|
+
value = os.environ.get(name)
|
|
39
|
+
if value is None:
|
|
40
|
+
return default
|
|
41
|
+
return str(value).strip().lower() in {"1", "true", "yes", "on", "y", "si"}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def service_host() -> str:
|
|
45
|
+
return str(os.environ.get("NEXO_RUNTIME_HOST", DEFAULT_HOST) or DEFAULT_HOST).strip()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def service_path() -> str:
|
|
49
|
+
raw = str(os.environ.get("NEXO_RUNTIME_MCP_PATH", SERVICE_PATH) or SERVICE_PATH).strip()
|
|
50
|
+
return raw if raw.startswith("/") else f"/{raw}"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def service_url(host: str | None = None, port: int | None = None, path: str | None = None) -> str:
|
|
54
|
+
return f"http://{host or service_host()}:{int(port or service_port())}{path or service_path()}"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def service_state_path() -> Path:
|
|
58
|
+
root = paths.runtime_state_dir()
|
|
59
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
60
|
+
return root / STATE_FILE
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def service_log_path() -> Path:
|
|
64
|
+
root = paths.logs_dir()
|
|
65
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
66
|
+
return root / LOG_FILE
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def service_lock_path() -> Path:
|
|
70
|
+
root = paths.runtime_state_dir()
|
|
71
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
72
|
+
return root / LOCK_FILE
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@contextmanager
|
|
76
|
+
def service_start_lock(*, timeout: float = 10.0):
|
|
77
|
+
path = service_lock_path()
|
|
78
|
+
handle = path.open("a+")
|
|
79
|
+
deadline = time.monotonic() + max(timeout, 0.5)
|
|
80
|
+
locked = False
|
|
81
|
+
try:
|
|
82
|
+
while not locked:
|
|
83
|
+
try:
|
|
84
|
+
if os.name == "nt":
|
|
85
|
+
import msvcrt
|
|
86
|
+
|
|
87
|
+
handle.seek(0)
|
|
88
|
+
if not handle.read(1):
|
|
89
|
+
handle.write("0")
|
|
90
|
+
handle.flush()
|
|
91
|
+
handle.seek(0)
|
|
92
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
|
|
93
|
+
else:
|
|
94
|
+
import fcntl
|
|
95
|
+
|
|
96
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
97
|
+
locked = True
|
|
98
|
+
except (BlockingIOError, OSError):
|
|
99
|
+
if time.monotonic() >= deadline:
|
|
100
|
+
raise TimeoutError(f"Timed out waiting for NEXO runtime service lock: {path}")
|
|
101
|
+
time.sleep(0.1)
|
|
102
|
+
handle.seek(0)
|
|
103
|
+
handle.truncate()
|
|
104
|
+
handle.write(f"{os.getpid()}:{time.time()}\n")
|
|
105
|
+
handle.flush()
|
|
106
|
+
yield
|
|
107
|
+
finally:
|
|
108
|
+
if locked:
|
|
109
|
+
try:
|
|
110
|
+
if os.name == "nt":
|
|
111
|
+
import msvcrt
|
|
112
|
+
|
|
113
|
+
handle.seek(0)
|
|
114
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
|
|
115
|
+
else:
|
|
116
|
+
import fcntl
|
|
117
|
+
|
|
118
|
+
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
|
|
119
|
+
except Exception:
|
|
120
|
+
pass
|
|
121
|
+
try:
|
|
122
|
+
handle.close()
|
|
123
|
+
except Exception:
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def read_service_state() -> dict[str, Any]:
|
|
128
|
+
try:
|
|
129
|
+
path = service_state_path()
|
|
130
|
+
if not path.is_file():
|
|
131
|
+
return {}
|
|
132
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
133
|
+
return data if isinstance(data, dict) else {}
|
|
134
|
+
except Exception:
|
|
135
|
+
return {}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def write_service_state(state: dict[str, Any]) -> None:
|
|
139
|
+
path = service_state_path()
|
|
140
|
+
tmp = path.with_suffix(path.suffix + ".tmp")
|
|
141
|
+
payload = dict(state)
|
|
142
|
+
payload.update(current_runtime_identity())
|
|
143
|
+
payload["updated_at"] = time.time()
|
|
144
|
+
tmp.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
145
|
+
os.replace(tmp, path)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def is_runtime_service_process() -> bool:
|
|
149
|
+
return env_flag(SERVICE_ENV)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def should_use_mcp_adapter() -> bool:
|
|
153
|
+
if is_runtime_service_process():
|
|
154
|
+
return False
|
|
155
|
+
if env_flag(DIRECT_ENV):
|
|
156
|
+
return False
|
|
157
|
+
if not env_flag(ADAPTER_ENV, default=True):
|
|
158
|
+
return False
|
|
159
|
+
transport = str(os.environ.get("NEXO_MCP_TRANSPORT", "stdio") or "stdio").strip().lower()
|
|
160
|
+
return transport == "stdio"
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def service_port() -> int:
|
|
164
|
+
raw = os.environ.get("NEXO_RUNTIME_PORT")
|
|
165
|
+
if raw:
|
|
166
|
+
try:
|
|
167
|
+
return int(raw)
|
|
168
|
+
except Exception:
|
|
169
|
+
pass
|
|
170
|
+
state = read_service_state()
|
|
171
|
+
try:
|
|
172
|
+
port = int(state.get("port") or 0)
|
|
173
|
+
if port > 0:
|
|
174
|
+
return port
|
|
175
|
+
except Exception:
|
|
176
|
+
pass
|
|
177
|
+
return DEFAULT_PORT
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def pid_is_running(pid: int) -> bool:
|
|
181
|
+
if pid <= 0:
|
|
182
|
+
return False
|
|
183
|
+
try:
|
|
184
|
+
os.kill(pid, 0)
|
|
185
|
+
return True
|
|
186
|
+
except ProcessLookupError:
|
|
187
|
+
return False
|
|
188
|
+
except PermissionError:
|
|
189
|
+
return True
|
|
190
|
+
except Exception:
|
|
191
|
+
return False
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _port_is_free(host: str, port: int) -> bool:
|
|
195
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
196
|
+
sock.settimeout(0.2)
|
|
197
|
+
try:
|
|
198
|
+
sock.bind((host, port))
|
|
199
|
+
return True
|
|
200
|
+
except OSError:
|
|
201
|
+
return False
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def choose_service_port(host: str | None = None) -> int:
|
|
205
|
+
host = host or service_host()
|
|
206
|
+
preferred = service_port()
|
|
207
|
+
for offset in range(PORT_SCAN_LIMIT):
|
|
208
|
+
port = preferred + offset
|
|
209
|
+
if _port_is_free(host, port):
|
|
210
|
+
return port
|
|
211
|
+
raise RuntimeError(f"No free NEXO runtime service port in range {preferred}-{preferred + PORT_SCAN_LIMIT - 1}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
async def _probe_service_async(url: str, *, timeout: float = 1.5) -> bool:
|
|
215
|
+
from fastmcp import Client
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
client = Client(url, timeout=timeout, init_timeout=timeout)
|
|
219
|
+
async with client:
|
|
220
|
+
return bool(await client.ping())
|
|
221
|
+
except Exception:
|
|
222
|
+
return False
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def probe_service(url: str, *, timeout: float = 1.5) -> bool:
|
|
226
|
+
try:
|
|
227
|
+
return bool(asyncio.run(_probe_service_async(url, timeout=timeout)))
|
|
228
|
+
except RuntimeError:
|
|
229
|
+
# If an event loop is already active, fall back to a tiny socket probe.
|
|
230
|
+
try:
|
|
231
|
+
host_port = url.split("//", 1)[1].split("/", 1)[0]
|
|
232
|
+
host, port_text = host_port.rsplit(":", 1)
|
|
233
|
+
with socket.create_connection((host, int(port_text)), timeout=timeout):
|
|
234
|
+
return True
|
|
235
|
+
except Exception:
|
|
236
|
+
return False
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def current_server_path() -> Path:
|
|
240
|
+
return Path(__file__).resolve().with_name("server.py")
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def current_runtime_identity() -> dict[str, str]:
|
|
244
|
+
try:
|
|
245
|
+
from runtime_versioning import compute_mcp_runtime_fingerprint, read_version_for_path
|
|
246
|
+
|
|
247
|
+
root = current_server_path().parent
|
|
248
|
+
version = read_version_for_path(root) or read_version_for_path(root.parent)
|
|
249
|
+
return {
|
|
250
|
+
"runtime_version": version,
|
|
251
|
+
"runtime_fingerprint": compute_mcp_runtime_fingerprint(root, use_cache=True),
|
|
252
|
+
"server_path": str(current_server_path()),
|
|
253
|
+
}
|
|
254
|
+
except Exception:
|
|
255
|
+
return {"runtime_version": "", "runtime_fingerprint": "", "server_path": str(current_server_path())}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def state_matches_current_runtime(state: dict[str, Any]) -> bool:
|
|
259
|
+
if not state:
|
|
260
|
+
return False
|
|
261
|
+
current = current_runtime_identity()
|
|
262
|
+
state_server = str(state.get("server_path") or "").strip()
|
|
263
|
+
if state_server and state_server != current["server_path"]:
|
|
264
|
+
return False
|
|
265
|
+
|
|
266
|
+
current_fp = str(current.get("runtime_fingerprint") or "").strip()
|
|
267
|
+
state_fp = str(state.get("runtime_fingerprint") or "").strip()
|
|
268
|
+
if current_fp and state_fp and current_fp != state_fp:
|
|
269
|
+
return False
|
|
270
|
+
|
|
271
|
+
current_version = str(current.get("runtime_version") or "").strip()
|
|
272
|
+
state_version = str(state.get("runtime_version") or "").strip()
|
|
273
|
+
if current_version and state_version and current_version != state_version:
|
|
274
|
+
return False
|
|
275
|
+
return True
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _terminate_pid(pid: int, *, timeout: float = 3.0) -> dict[str, Any]:
|
|
279
|
+
if pid <= 0:
|
|
280
|
+
return {"terminated": False, "reason": "no_pid"}
|
|
281
|
+
if not pid_is_running(pid):
|
|
282
|
+
return {"terminated": False, "reason": "not_running"}
|
|
283
|
+
try:
|
|
284
|
+
if os.name == "nt":
|
|
285
|
+
subprocess.run(
|
|
286
|
+
["taskkill", "/PID", str(pid), "/T", "/F"],
|
|
287
|
+
capture_output=True,
|
|
288
|
+
text=True,
|
|
289
|
+
timeout=max(timeout, 1.0),
|
|
290
|
+
)
|
|
291
|
+
else:
|
|
292
|
+
os.kill(pid, signal.SIGTERM)
|
|
293
|
+
deadline = time.monotonic() + max(timeout, 0.2)
|
|
294
|
+
while time.monotonic() < deadline:
|
|
295
|
+
if not pid_is_running(pid):
|
|
296
|
+
return {"terminated": True, "pid": pid, "signal": "SIGTERM"}
|
|
297
|
+
time.sleep(0.1)
|
|
298
|
+
if hasattr(signal, "SIGKILL"):
|
|
299
|
+
os.kill(pid, signal.SIGKILL)
|
|
300
|
+
return {"terminated": True, "pid": pid}
|
|
301
|
+
except Exception as exc:
|
|
302
|
+
return {"terminated": False, "pid": pid, "error": str(exc)[:300]}
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def stop_runtime_service(*, reason: str = "stop", timeout: float = 3.0) -> dict[str, Any]:
|
|
306
|
+
state = read_service_state()
|
|
307
|
+
pid = int(state.get("pid") or 0) if str(state.get("pid") or "").isdigit() else 0
|
|
308
|
+
result = _terminate_pid(pid, timeout=timeout)
|
|
309
|
+
result["reason"] = reason
|
|
310
|
+
result["state_path"] = str(service_state_path())
|
|
311
|
+
try:
|
|
312
|
+
service_state_path().unlink(missing_ok=True)
|
|
313
|
+
result["state_removed"] = True
|
|
314
|
+
except Exception as exc:
|
|
315
|
+
result["state_removed"] = False
|
|
316
|
+
result["state_error"] = str(exc)[:300]
|
|
317
|
+
return result
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _service_env(port: int, host: str) -> dict[str, str]:
|
|
321
|
+
env = os.environ.copy()
|
|
322
|
+
env[SERVICE_ENV] = "1"
|
|
323
|
+
env["NEXO_MCP_TRANSPORT"] = "streamable-http"
|
|
324
|
+
env["NEXO_MCP_HOST"] = host
|
|
325
|
+
env["NEXO_MCP_PORT"] = str(port)
|
|
326
|
+
env["NEXO_MCP_PATH"] = service_path()
|
|
327
|
+
# A probe client may inherit a deliberately tiny plugin mode. The service
|
|
328
|
+
# should use the normal runtime defaults unless explicitly overridden.
|
|
329
|
+
if "NEXO_RUNTIME_SERVICE_PLUGIN_MODE" in env:
|
|
330
|
+
env["NEXO_MCP_PLUGIN_MODE"] = env["NEXO_RUNTIME_SERVICE_PLUGIN_MODE"]
|
|
331
|
+
return env
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def _spawn_service_process(port: int, host: str) -> subprocess.Popen:
|
|
335
|
+
log_path = service_log_path()
|
|
336
|
+
log_file = open(log_path, "ab", buffering=0)
|
|
337
|
+
kwargs: dict[str, Any] = {
|
|
338
|
+
"cwd": str(current_server_path().parent),
|
|
339
|
+
"env": _service_env(port, host),
|
|
340
|
+
"stdin": subprocess.DEVNULL,
|
|
341
|
+
"stdout": log_file,
|
|
342
|
+
"stderr": log_file,
|
|
343
|
+
}
|
|
344
|
+
if os.name == "nt":
|
|
345
|
+
kwargs["creationflags"] = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)
|
|
346
|
+
else:
|
|
347
|
+
kwargs["start_new_session"] = True
|
|
348
|
+
return subprocess.Popen([sys.executable, str(current_server_path())], **kwargs)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def ensure_runtime_service(*, wait_seconds: float = 10.0) -> str:
|
|
352
|
+
with service_start_lock(timeout=wait_seconds):
|
|
353
|
+
host = service_host()
|
|
354
|
+
state = read_service_state()
|
|
355
|
+
state_url = str(state.get("url") or "")
|
|
356
|
+
state_pid = int(state.get("pid") or 0) if str(state.get("pid") or "").isdigit() else 0
|
|
357
|
+
if state_url and (state_pid <= 0 or pid_is_running(state_pid)):
|
|
358
|
+
if state_matches_current_runtime(state) and probe_service(state_url):
|
|
359
|
+
return state_url
|
|
360
|
+
if state_pid > 0:
|
|
361
|
+
stop_runtime_service(reason="stale_runtime")
|
|
362
|
+
|
|
363
|
+
port = choose_service_port(host)
|
|
364
|
+
url = service_url(host, port)
|
|
365
|
+
proc = _spawn_service_process(port, host)
|
|
366
|
+
write_service_state(
|
|
367
|
+
{
|
|
368
|
+
"pid": proc.pid,
|
|
369
|
+
"port": port,
|
|
370
|
+
"host": host,
|
|
371
|
+
"path": service_path(),
|
|
372
|
+
"url": url,
|
|
373
|
+
"server_path": str(current_server_path()),
|
|
374
|
+
"started_at": time.time(),
|
|
375
|
+
"mode": "runtime-service",
|
|
376
|
+
}
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
deadline = time.monotonic() + max(wait_seconds, 0.5)
|
|
380
|
+
delay = 0.15
|
|
381
|
+
while time.monotonic() < deadline:
|
|
382
|
+
if proc.poll() is not None:
|
|
383
|
+
break
|
|
384
|
+
if probe_service(url):
|
|
385
|
+
return url
|
|
386
|
+
time.sleep(delay)
|
|
387
|
+
delay = min(delay * 1.5, 1.0)
|
|
388
|
+
|
|
389
|
+
code = proc.poll()
|
|
390
|
+
raise RuntimeError(
|
|
391
|
+
"NEXO runtime service did not become ready"
|
|
392
|
+
+ (f" (exit={code})" if code is not None else "")
|
|
393
|
+
+ f"; log={service_log_path()}"
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def runtime_service_status() -> dict[str, Any]:
|
|
398
|
+
state = read_service_state()
|
|
399
|
+
current = current_runtime_identity()
|
|
400
|
+
url = str(state.get("url") or "")
|
|
401
|
+
pid = int(state.get("pid") or 0) if str(state.get("pid") or "").isdigit() else 0
|
|
402
|
+
alive = pid_is_running(pid)
|
|
403
|
+
ready = bool(url and probe_service(url, timeout=0.8))
|
|
404
|
+
return {
|
|
405
|
+
"ok": ready,
|
|
406
|
+
"mode": "service" if is_runtime_service_process() else "adapter",
|
|
407
|
+
"pid": pid,
|
|
408
|
+
"pid_alive": alive,
|
|
409
|
+
"url": url,
|
|
410
|
+
"stale": bool(state and not state_matches_current_runtime(state)),
|
|
411
|
+
"runtime_version": current.get("runtime_version", ""),
|
|
412
|
+
"runtime_fingerprint": current.get("runtime_fingerprint", ""),
|
|
413
|
+
"state_runtime_version": str(state.get("runtime_version") or ""),
|
|
414
|
+
"state_runtime_fingerprint": str(state.get("runtime_fingerprint") or ""),
|
|
415
|
+
"state_path": str(service_state_path()),
|
|
416
|
+
"log_path": str(service_log_path()),
|
|
417
|
+
"server_path": str(current_server_path()),
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def run_mcp_proxy_adapter(*, name: str, instructions: str, run_kwargs: dict[str, Any]) -> None:
|
|
422
|
+
from fastmcp.server import create_proxy
|
|
423
|
+
|
|
424
|
+
url = ensure_runtime_service()
|
|
425
|
+
proxy = create_proxy(url, name=name, instructions=instructions)
|
|
426
|
+
proxy.run(**run_kwargs)
|
|
@@ -732,6 +732,16 @@ def build_mcp_status(*, client: str = "") -> dict:
|
|
|
732
732
|
marker = state["marker"]
|
|
733
733
|
installed_fp = state.get("installed_fingerprint", "")
|
|
734
734
|
process_fp = state.get("process_fingerprint", "")
|
|
735
|
+
try:
|
|
736
|
+
from runtime_service import runtime_service_status
|
|
737
|
+
|
|
738
|
+
service_status = runtime_service_status()
|
|
739
|
+
except Exception as exc:
|
|
740
|
+
service_status = {
|
|
741
|
+
"ok": False,
|
|
742
|
+
"error": "runtime_service_status_unavailable",
|
|
743
|
+
"message": str(exc)[:300],
|
|
744
|
+
}
|
|
735
745
|
return {
|
|
736
746
|
"ok": True,
|
|
737
747
|
"schema_version": MCP_STATUS_SCHEMA_VERSION,
|
|
@@ -755,6 +765,7 @@ def build_mcp_status(*, client: str = "") -> dict:
|
|
|
755
765
|
"marker_exists": bool(marker.get("exists")),
|
|
756
766
|
"marker_corrupt": bool(marker.get("corrupt")),
|
|
757
767
|
"continuity_api_level": CONTINUITY_API_LEVEL,
|
|
768
|
+
"runtime_service": service_status,
|
|
758
769
|
"version_match": (
|
|
759
770
|
bool(state["installed_version"])
|
|
760
771
|
and bool(state["process_version"])
|
package/src/server.py
CHANGED
|
@@ -117,6 +117,13 @@ from runtime_versioning import (
|
|
|
117
117
|
prime_process_fingerprint,
|
|
118
118
|
prime_process_version,
|
|
119
119
|
)
|
|
120
|
+
from runtime_service import (
|
|
121
|
+
is_runtime_service_process,
|
|
122
|
+
run_mcp_proxy_adapter,
|
|
123
|
+
runtime_service_status,
|
|
124
|
+
should_use_mcp_adapter,
|
|
125
|
+
write_service_state,
|
|
126
|
+
)
|
|
120
127
|
from local_context import api as local_context_api
|
|
121
128
|
from local_context.db import close_local_context_db
|
|
122
129
|
|
|
@@ -766,6 +773,12 @@ def nexo_status(keyword: str = "") -> str:
|
|
|
766
773
|
return handle_status(keyword if keyword else None)
|
|
767
774
|
|
|
768
775
|
|
|
776
|
+
@mcp.tool
|
|
777
|
+
def nexo_runtime_service_status() -> str:
|
|
778
|
+
"""Return the resident NEXO Runtime Service status for diagnostics."""
|
|
779
|
+
return json.dumps(runtime_service_status(), indent=2, ensure_ascii=False)
|
|
780
|
+
|
|
781
|
+
|
|
769
782
|
@mcp.tool
|
|
770
783
|
def nexo_local_index_status() -> str:
|
|
771
784
|
"""Return local memory index status for Desktop settings and support diagnostics."""
|
|
@@ -2300,5 +2313,32 @@ def nexo_create_app_token(
|
|
|
2300
2313
|
|
|
2301
2314
|
|
|
2302
2315
|
if __name__ == "__main__":
|
|
2303
|
-
|
|
2304
|
-
|
|
2316
|
+
if should_use_mcp_adapter():
|
|
2317
|
+
run_mcp_proxy_adapter(
|
|
2318
|
+
name="nexo",
|
|
2319
|
+
instructions=render_core_prompt(
|
|
2320
|
+
"server-mcp-instructions",
|
|
2321
|
+
assistant_name=_get_ctx().assistant_name,
|
|
2322
|
+
),
|
|
2323
|
+
run_kwargs=_run_kwargs_from_env(),
|
|
2324
|
+
)
|
|
2325
|
+
else:
|
|
2326
|
+
_server_init()
|
|
2327
|
+
run_kwargs = _run_kwargs_from_env()
|
|
2328
|
+
if is_runtime_service_process():
|
|
2329
|
+
host = str(run_kwargs.get("host") or os.environ.get("NEXO_MCP_HOST", "127.0.0.1"))
|
|
2330
|
+
port = int(run_kwargs.get("port") or os.environ.get("NEXO_MCP_PORT", "0") or 0)
|
|
2331
|
+
path = str(run_kwargs.get("path") or os.environ.get("NEXO_MCP_PATH", "/mcp"))
|
|
2332
|
+
write_service_state(
|
|
2333
|
+
{
|
|
2334
|
+
"pid": os.getpid(),
|
|
2335
|
+
"port": port,
|
|
2336
|
+
"host": host,
|
|
2337
|
+
"path": path,
|
|
2338
|
+
"url": f"http://{host}:{port}{path}",
|
|
2339
|
+
"server_path": str(os.path.abspath(__file__)),
|
|
2340
|
+
"started_at": time.time(),
|
|
2341
|
+
"mode": "runtime-service",
|
|
2342
|
+
}
|
|
2343
|
+
)
|
|
2344
|
+
mcp.run(**run_kwargs)
|
|
@@ -2383,6 +2383,21 @@
|
|
|
2383
2383
|
},
|
|
2384
2384
|
"triggers_after": []
|
|
2385
2385
|
},
|
|
2386
|
+
"nexo_runtime_service_status": {
|
|
2387
|
+
"description": "Return resident Runtime Service health, PID, version, fingerprint and state paths",
|
|
2388
|
+
"category": "system",
|
|
2389
|
+
"source": "server",
|
|
2390
|
+
"requires": [],
|
|
2391
|
+
"provides": [
|
|
2392
|
+
"runtime_service_status"
|
|
2393
|
+
],
|
|
2394
|
+
"internal_calls": [],
|
|
2395
|
+
"enforcement": {
|
|
2396
|
+
"level": "none",
|
|
2397
|
+
"rules": []
|
|
2398
|
+
},
|
|
2399
|
+
"triggers_after": []
|
|
2400
|
+
},
|
|
2386
2401
|
"nexo_media_memory_add": {
|
|
2387
2402
|
"description": "Store non-text artifact metadata",
|
|
2388
2403
|
"category": "media",
|