superlocalmemory 3.6.9 → 3.6.11
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/CHANGELOG.md +112 -10
- package/README.md +67 -9
- package/package.json +1 -1
- package/pyproject.toml +6 -1
- package/skills/slm-optimize/README.md +55 -0
- package/skills/slm-optimize/SKILL.md +139 -0
- package/src/superlocalmemory/__init__.py +1 -1
- package/src/superlocalmemory/cli/compress_cmd.py +32 -70
- package/src/superlocalmemory/cli/optimize_cmd.py +1 -3
- package/src/superlocalmemory/cli/setup_wizard.py +49 -0
- package/src/superlocalmemory/mcp/agent_context.py +111 -0
- package/src/superlocalmemory/mcp/server.py +4 -0
- package/src/superlocalmemory/mcp/tools_active.py +7 -8
- package/src/superlocalmemory/mcp/tools_core.py +16 -0
- package/src/superlocalmemory/mcp/tools_optimize.py +304 -0
- package/src/superlocalmemory/optimize/cache/boundary_store.py +23 -9
- package/src/superlocalmemory/optimize/cache/exact.py +7 -4
- package/src/superlocalmemory/optimize/cache/key_builder.py +13 -0
- package/src/superlocalmemory/optimize/cache/manager.py +70 -8
- package/src/superlocalmemory/optimize/cache/semantic.py +10 -5
- package/src/superlocalmemory/optimize/compress/prose_llmlingua.py +1 -7
- package/src/superlocalmemory/optimize/compress/router.py +82 -87
- package/src/superlocalmemory/optimize/config/__init__.py +16 -0
- package/src/superlocalmemory/optimize/config/defaults.py +1 -6
- package/src/superlocalmemory/optimize/config/schema.py +2 -19
- package/src/superlocalmemory/optimize/config/store.py +15 -1
- package/src/superlocalmemory/optimize/metrics/counters.py +15 -7
- package/src/superlocalmemory/optimize/proxy/_helpers.py +100 -2
- package/src/superlocalmemory/optimize/proxy/anthropic_surface.py +12 -0
- package/src/superlocalmemory/optimize/proxy/capture.py +243 -0
- package/src/superlocalmemory/optimize/proxy/gemini_surface.py +31 -0
- package/src/superlocalmemory/optimize/proxy/openai_surface.py +12 -0
- package/src/superlocalmemory/optimize/proxy/server.py +29 -0
- package/src/superlocalmemory/optimize/storage/db.py +102 -11
- package/src/superlocalmemory/optimize/storage/schema.py +11 -0
- package/src/superlocalmemory/server/routes/optimize.py +6 -8
- package/src/superlocalmemory/server/unified_daemon.py +26 -5
- package/src/superlocalmemory/ui/index.html +18 -14
- package/src/superlocalmemory/ui/js/auto-settings.js +3 -1
- package/src/superlocalmemory/ui/js/ng-shell.js +3 -0
- package/src/superlocalmemory/ui/js/optimize.js +9 -9
- package/src/superlocalmemory.egg-info/PKG-INFO +69 -10
- package/src/superlocalmemory.egg-info/SOURCES.txt +3 -2
- package/src/superlocalmemory.egg-info/requires.txt +1 -0
- package/src/superlocalmemory/optimize/compress/extractive_code.py +0 -311
- package/src/superlocalmemory/optimize/compress/extractive_json.py +0 -72
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# Licensed under AGPL-3.0-or-later - see LICENSE file
|
|
3
3
|
# Part of SuperLocalMemory V3 | https://qualixar.com | https://varunpratap.com
|
|
4
4
|
|
|
5
|
-
"""Handlers for ``slm compress status|mode|
|
|
5
|
+
"""Handlers for ``slm compress status|mode|prose``."""
|
|
6
6
|
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
@@ -19,11 +19,6 @@ def _get_store():
|
|
|
19
19
|
return ConfigStore()
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
def _get_cache_db():
|
|
23
|
-
from superlocalmemory.optimize.storage.db import CacheDB
|
|
24
|
-
return CacheDB()
|
|
25
|
-
|
|
26
|
-
|
|
27
22
|
def _write_config(**fields) -> None:
|
|
28
23
|
"""5-step immutable config-write."""
|
|
29
24
|
store = _get_store()
|
|
@@ -44,18 +39,31 @@ def cmd_compress(args: Namespace) -> None:
|
|
|
44
39
|
sub = getattr(args, "compress_command", None)
|
|
45
40
|
_dispatch = {
|
|
46
41
|
"status": cmd_compress_status,
|
|
47
|
-
"mode":
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
42
|
+
"mode": cmd_compress_mode,
|
|
43
|
+
"prose": cmd_compress_prose,
|
|
44
|
+
# removed in v3.6.10: code, ccr, align (extractive compressors removed)
|
|
45
|
+
"code": _cmd_compress_removed("code"),
|
|
46
|
+
"ccr": _cmd_compress_removed("ccr"),
|
|
47
|
+
"align": _cmd_compress_removed("align"),
|
|
52
48
|
}
|
|
53
49
|
handler = _dispatch.get(sub or "")
|
|
54
50
|
if handler:
|
|
55
51
|
handler(args)
|
|
56
52
|
else:
|
|
57
|
-
print("Usage: slm compress status|mode|
|
|
53
|
+
print("Usage: slm compress status|mode|prose [options]")
|
|
54
|
+
sys.exit(0)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _cmd_compress_removed(name: str):
|
|
58
|
+
def handler(args: Namespace) -> None:
|
|
59
|
+
print(
|
|
60
|
+
f"slm compress {name}: removed in SLM v3.6.10. "
|
|
61
|
+
f"Extractive {name} compression has been replaced by "
|
|
62
|
+
f"Layer 1 (lossless whitespace) + Layer 2 (LLMLingua-2 prose). "
|
|
63
|
+
f"Use 'slm compress prose on' to enable prose compression."
|
|
64
|
+
)
|
|
58
65
|
sys.exit(0)
|
|
66
|
+
return handler
|
|
59
67
|
|
|
60
68
|
|
|
61
69
|
def cmd_compress_status(args: Namespace) -> None:
|
|
@@ -68,21 +76,19 @@ def cmd_compress_status(args: Namespace) -> None:
|
|
|
68
76
|
"status": "ok",
|
|
69
77
|
"compress_enabled": cfg.compress_enabled,
|
|
70
78
|
"compress_mode": cfg.compress_mode,
|
|
71
|
-
"compress_code": cfg.compress_code,
|
|
72
79
|
"compress_prose": cfg.compress_prose,
|
|
73
|
-
"
|
|
80
|
+
"compress_protect_recent": cfg.compress_protect_recent,
|
|
74
81
|
}
|
|
75
82
|
print(json.dumps(data, indent=2))
|
|
76
83
|
return
|
|
77
84
|
|
|
78
85
|
print("Compression status:")
|
|
79
|
-
print(f" Enabled:
|
|
80
|
-
print(f" Mode:
|
|
81
|
-
print(f"
|
|
82
|
-
" (
|
|
83
|
-
print(f"
|
|
84
|
-
print(
|
|
85
|
-
" (reversible context retrieval)")
|
|
86
|
+
print(f" Enabled: {'yes' if cfg.compress_enabled else 'no'}")
|
|
87
|
+
print(f" Mode: {cfg.compress_mode}")
|
|
88
|
+
print(f" Prose (Layer 2): {'ON' if cfg.compress_prose else 'OFF'}"
|
|
89
|
+
" (LLMLingua-2, aggressive mode only)")
|
|
90
|
+
print(f" Protect recent: {cfg.compress_protect_recent} user turns")
|
|
91
|
+
print(" Layer 1 (lossless whitespace normalization) is always ON when enabled.")
|
|
86
92
|
|
|
87
93
|
|
|
88
94
|
def cmd_compress_mode(args: Namespace) -> None:
|
|
@@ -103,23 +109,8 @@ def cmd_compress_mode(args: Namespace) -> None:
|
|
|
103
109
|
print("Daemon hot-reload: active within 2s. No restart required.")
|
|
104
110
|
|
|
105
111
|
|
|
106
|
-
def cmd_compress_code(args: Namespace) -> None:
|
|
107
|
-
"""Enable or disable code/JSON compression."""
|
|
108
|
-
use_json = getattr(args, "json", False)
|
|
109
|
-
value = getattr(args, "code_value", "on")
|
|
110
|
-
|
|
111
|
-
_write_config(compress_code=(value == "on"))
|
|
112
|
-
|
|
113
|
-
if use_json:
|
|
114
|
-
print(json.dumps({"status": "ok", "compress_code": value == "on"}))
|
|
115
|
-
return
|
|
116
|
-
|
|
117
|
-
print(f"Code compression: {'ENABLED' if value == 'on' else 'DISABLED'}.")
|
|
118
|
-
print("Daemon hot-reload: active within 2s.")
|
|
119
|
-
|
|
120
|
-
|
|
121
112
|
def cmd_compress_prose(args: Namespace) -> None:
|
|
122
|
-
"""Enable or disable prose compression."""
|
|
113
|
+
"""Enable or disable prose compression (Layer 2, LLMLingua-2)."""
|
|
123
114
|
use_json = getattr(args, "json", False)
|
|
124
115
|
value = getattr(args, "prose_value", "off")
|
|
125
116
|
|
|
@@ -141,39 +132,10 @@ def cmd_compress_prose(args: Namespace) -> None:
|
|
|
141
132
|
print(json.dumps({"status": "ok", "compress_prose": value == "on"}))
|
|
142
133
|
return
|
|
143
134
|
|
|
144
|
-
print(f"Prose compression: {'ENABLED' if value == 'on' else 'DISABLED'}.")
|
|
135
|
+
print(f"Prose compression (LLMLingua-2): {'ENABLED' if value == 'on' else 'DISABLED'}.")
|
|
136
|
+
if value == "on":
|
|
137
|
+
print(" Requires: compress_mode=aggressive and llmlingua package installed.")
|
|
138
|
+
print(" Run: slm compress mode aggressive (if not already set)")
|
|
145
139
|
if value == "on" and "compress_enabled" in fields:
|
|
146
140
|
print(" (also enabled global compress)")
|
|
147
141
|
print("Daemon hot-reload: active within 2s.")
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def cmd_compress_align(args: Namespace) -> None:
|
|
151
|
-
"""Enable or disable alignment compression."""
|
|
152
|
-
use_json = getattr(args, "json", False)
|
|
153
|
-
value = getattr(args, "align_value", "on")
|
|
154
|
-
|
|
155
|
-
_write_config(compress_align=(value == "on"))
|
|
156
|
-
|
|
157
|
-
if use_json:
|
|
158
|
-
print(json.dumps({"status": "ok", "compress_align": value == "on"}))
|
|
159
|
-
return
|
|
160
|
-
|
|
161
|
-
print(f"Alignment compression: {'ENABLED' if value == 'on' else 'DISABLED'}.")
|
|
162
|
-
print("Daemon hot-reload: active within 2s.")
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
def cmd_compress_ccr(args: Namespace) -> None:
|
|
166
|
-
"""Enable or disable CCR (Compressed Context Retrieval)."""
|
|
167
|
-
use_json = getattr(args, "json", False)
|
|
168
|
-
value = getattr(args, "ccr_value", "off")
|
|
169
|
-
|
|
170
|
-
_write_config(compress_ccr=(value == "on"))
|
|
171
|
-
|
|
172
|
-
if use_json:
|
|
173
|
-
print(json.dumps({"status": "ok", "compress_ccr": value == "on"}))
|
|
174
|
-
return
|
|
175
|
-
|
|
176
|
-
print(f"CCR (Compressed Context Retrieval): {'ENABLED' if value == 'on' else 'DISABLED'}.")
|
|
177
|
-
if value == "on":
|
|
178
|
-
print("Originals stored in llmcache.db for reversible retrieval.")
|
|
179
|
-
print("Daemon hot-reload: active within 2s.")
|
|
@@ -97,9 +97,7 @@ def cmd_optimize_status(args: Namespace) -> None:
|
|
|
97
97
|
f" semantic: {'OFF' if not cfg.semantic_enabled else f'{cfg.ttl.semantic_seconds}s'})")
|
|
98
98
|
print(f" Compress: {'enabled' if cfg.compress_enabled else 'disabled'}"
|
|
99
99
|
f" (mode: {cfg.compress_mode},"
|
|
100
|
-
f"
|
|
101
|
-
f" prose: {'ON' if cfg.compress_prose else 'OFF'},"
|
|
102
|
-
f" CCR: {'ON' if cfg.compress_ccr else 'OFF'})")
|
|
100
|
+
f" prose/L2: {'ON' if cfg.compress_prose else 'OFF'})")
|
|
103
101
|
proxy_status = f"running on :{OPTIMIZE_DEFAULT_PORT}" if proxy_running else "not running"
|
|
104
102
|
print(f" Proxy: {proxy_status}")
|
|
105
103
|
print(f" Config: ~/.superlocalmemory/optimize.json (version {cfg.config_version})")
|
|
@@ -32,6 +32,8 @@ _SLM_HOME = Path(os.environ.get("SL_MEMORY_PATH", Path.home() / ".superlocalmemo
|
|
|
32
32
|
_SETUP_MARKER = _SLM_HOME / ".setup-complete"
|
|
33
33
|
_EMBED_MODEL = "nomic-ai/nomic-embed-text-v1.5"
|
|
34
34
|
_RERANKER_MODEL = "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
|
35
|
+
# v3.6.10: compulsory LLMLingua-2 prose compression model (~560MB, aggressive mode).
|
|
36
|
+
_COMPRESSOR_MODEL = "microsoft/llmlingua-2-xlm-roberta-large-meetingbank"
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
# ---------------------------------------------------------------------------
|
|
@@ -178,6 +180,49 @@ def _download_reranker(model_name: str) -> bool:
|
|
|
178
180
|
return False
|
|
179
181
|
|
|
180
182
|
|
|
183
|
+
def _download_compressor(model_name: str) -> bool:
|
|
184
|
+
"""Download the LLMLingua-2 prose compression model (v3.6.10).
|
|
185
|
+
|
|
186
|
+
Mirrors _download_reranker: a subprocess forces the HF download with visible
|
|
187
|
+
progress. Fail-open — a network hiccup must NOT break setup; the model also
|
|
188
|
+
lazy-downloads on first use in prose_llmlingua.py.
|
|
189
|
+
"""
|
|
190
|
+
print(f"\n Downloading compression model: {model_name}")
|
|
191
|
+
print(f" (LLMLingua-2 prose compressor, ~560MB — aggressive mode only)\n")
|
|
192
|
+
|
|
193
|
+
script = (
|
|
194
|
+
"from llmlingua import PromptCompressor; "
|
|
195
|
+
f"PromptCompressor(model_name='{model_name}', use_llmlingua2=True, "
|
|
196
|
+
"device_map='cpu'); "
|
|
197
|
+
"print('OK')"
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
try:
|
|
201
|
+
result = subprocess.run(
|
|
202
|
+
[sys.executable, "-c", script],
|
|
203
|
+
timeout=900, # 560MB on a slow link can exceed 5 min
|
|
204
|
+
capture_output=False,
|
|
205
|
+
text=True,
|
|
206
|
+
env={
|
|
207
|
+
**os.environ,
|
|
208
|
+
"CUDA_VISIBLE_DEVICES": "",
|
|
209
|
+
"TOKENIZERS_PARALLELISM": "false",
|
|
210
|
+
"TORCH_DEVICE": "cpu",
|
|
211
|
+
},
|
|
212
|
+
)
|
|
213
|
+
if result.returncode == 0:
|
|
214
|
+
print(f" ✓ Compression model ready")
|
|
215
|
+
return True
|
|
216
|
+
print(f" ✗ Compression model download failed (will lazy-download on first use)")
|
|
217
|
+
return False
|
|
218
|
+
except ImportError:
|
|
219
|
+
print(f" ⚠ llmlingua not installed — compression model will download on first use")
|
|
220
|
+
return False
|
|
221
|
+
except Exception as exc:
|
|
222
|
+
print(f" ✗ Compression model error: {exc}")
|
|
223
|
+
return False
|
|
224
|
+
|
|
225
|
+
|
|
181
226
|
# ---------------------------------------------------------------------------
|
|
182
227
|
# Verification
|
|
183
228
|
# ---------------------------------------------------------------------------
|
|
@@ -393,6 +438,10 @@ def run_wizard(auto: bool = False) -> None:
|
|
|
393
438
|
else:
|
|
394
439
|
_download_reranker(_RERANKER_MODEL)
|
|
395
440
|
|
|
441
|
+
print()
|
|
442
|
+
print("─── Step 4c/10: Download Compression Model (LLMLingua-2) ───")
|
|
443
|
+
_download_compressor(_COMPRESSOR_MODEL)
|
|
444
|
+
|
|
396
445
|
# -- Step 5: Daemon Configuration (v3.4.3) --
|
|
397
446
|
print()
|
|
398
447
|
print("─── Step 5/10: Daemon Configuration ───")
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Per-HTTP-request agent ID resolution — ContextVar home.
|
|
2
|
+
|
|
3
|
+
Kept in a standalone module so both tools_core and tools_active can import
|
|
4
|
+
it without creating circular dependencies (server.py → tools_core → here,
|
|
5
|
+
and unified_daemon.py → here independently).
|
|
6
|
+
|
|
7
|
+
Priority chain (HTTP-first, stdio-fallback):
|
|
8
|
+
1. ContextVar set by _AgentIDExtractorASGI middleware from /mcp/{agent_id} URL path.
|
|
9
|
+
2. SLM_AGENT_ID environment variable (stdio transport legacy).
|
|
10
|
+
3. Hard-coded "mcp_client" sentinel.
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import contextvars
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
17
|
+
|
|
18
|
+
_current_agent_id: contextvars.ContextVar[str] = contextvars.ContextVar(
|
|
19
|
+
"slm_agent_id", default="mcp_client"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# Agent ids arrive from an untrusted URL path segment. They are ATTRIBUTION
|
|
23
|
+
# metadata, never an authenticated principal — but they reach loggers, the
|
|
24
|
+
# agent registry, and SQL-bound attribution columns, so we hard-restrict the
|
|
25
|
+
# charset at the single extraction chokepoint. This neutralises log-injection
|
|
26
|
+
# (CRLF / ANSI), oversized ids, and any path-ish characters in one place.
|
|
27
|
+
_AGENT_ID_SANITIZE = re.compile(r"[^A-Za-z0-9._-]")
|
|
28
|
+
_AGENT_ID_MAX_LEN = 64
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def sanitize_agent_id(raw: str) -> str:
|
|
32
|
+
"""Coerce an untrusted agent-id segment to a safe, bounded token."""
|
|
33
|
+
return _AGENT_ID_SANITIZE.sub("_", raw)[:_AGENT_ID_MAX_LEN]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_current_agent_id(env_fallback: bool = True) -> str:
|
|
37
|
+
"""Return the agent_id for the current asyncio task.
|
|
38
|
+
|
|
39
|
+
For HTTP transport the ASGI wrapper sets the ContextVar from the URL path
|
|
40
|
+
before the request reaches any MCP tool, so this returns the URL-derived id.
|
|
41
|
+
For stdio transport the ContextVar holds its default ("mcp_client") and we
|
|
42
|
+
fall through to the SLM_AGENT_ID env var instead.
|
|
43
|
+
"""
|
|
44
|
+
ctx_id = _current_agent_id.get()
|
|
45
|
+
if ctx_id != "mcp_client":
|
|
46
|
+
return ctx_id
|
|
47
|
+
if env_fallback:
|
|
48
|
+
return os.environ.get("SLM_AGENT_ID", "mcp_client")
|
|
49
|
+
return "mcp_client"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class AgentIDExtractorASGI:
|
|
53
|
+
"""ASGI wrapper that maps ``/mcp/{agent_id}`` → the agent-id ContextVar.
|
|
54
|
+
|
|
55
|
+
Mounted at ``/mcp`` in unified_daemon. IMPORTANT: Starlette's ``Mount``
|
|
56
|
+
(≥0.35 / 1.x) does NOT strip the mount prefix from ``scope["path"]`` — it
|
|
57
|
+
records the prefix in ``scope["root_path"]`` and leaves ``path`` as the full
|
|
58
|
+
request path (e.g. ``/mcp/claude`` with ``root_path == "/mcp"``). So we
|
|
59
|
+
compute the mount-relative sub-path ourselves as ``path[len(root_path):]``.
|
|
60
|
+
|
|
61
|
+
Flow for ``POST /mcp/claude``:
|
|
62
|
+
sub-path ``/claude`` → agent id ``claude`` → set ContextVar → rewrite the
|
|
63
|
+
scope path to ``{root_path}/`` so the inner FastMCP app (Starlette, route
|
|
64
|
+
``/``) sees the same mount-relative ``/`` it sees for a bare ``/mcp/``.
|
|
65
|
+
|
|
66
|
+
Backward compatible: bare ``/mcp/`` has sub-path ``/`` → no agent segment →
|
|
67
|
+
the request passes through untouched and the ContextVar keeps its
|
|
68
|
+
``"mcp_client"`` default.
|
|
69
|
+
|
|
70
|
+
Per-request isolation is guaranteed by ContextVar + ``reset(token)`` in a
|
|
71
|
+
``finally``, so concurrent HTTP sessions never see each other's agent id.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
__slots__ = ("_app",)
|
|
75
|
+
|
|
76
|
+
def __init__(self, inner) -> None:
|
|
77
|
+
self._app = inner
|
|
78
|
+
|
|
79
|
+
async def __call__(self, scope, receive, send):
|
|
80
|
+
if scope.get("type") == "http":
|
|
81
|
+
root_path: str = scope.get("root_path", "")
|
|
82
|
+
full_path: str = scope.get("path", "/")
|
|
83
|
+
# Mount-relative sub-path (what comes AFTER /mcp). When a root_path
|
|
84
|
+
# is present the request path MUST start with it (Starlette Mount
|
|
85
|
+
# guarantees this); if it somehow does not, treat it as no-agent and
|
|
86
|
+
# pass through untouched rather than mis-parsing the full path.
|
|
87
|
+
if root_path:
|
|
88
|
+
if not full_path.startswith(root_path):
|
|
89
|
+
await self._app(scope, receive, send)
|
|
90
|
+
return
|
|
91
|
+
subpath = full_path[len(root_path):]
|
|
92
|
+
else:
|
|
93
|
+
subpath = full_path
|
|
94
|
+
first = subpath.lstrip("/").split("/")[0]
|
|
95
|
+
if first:
|
|
96
|
+
first = sanitize_agent_id(first)
|
|
97
|
+
token = _current_agent_id.set(first)
|
|
98
|
+
# Rewrite the path so the inner app sees the bare mount root,
|
|
99
|
+
# exactly as it would for a no-agent /mcp/ request.
|
|
100
|
+
new_full = (root_path + "/") if root_path else "/"
|
|
101
|
+
new_scope = {
|
|
102
|
+
**scope,
|
|
103
|
+
"path": new_full,
|
|
104
|
+
"raw_path": new_full.encode(),
|
|
105
|
+
}
|
|
106
|
+
try:
|
|
107
|
+
await self._app(new_scope, receive, send)
|
|
108
|
+
finally:
|
|
109
|
+
_current_agent_id.reset(token)
|
|
110
|
+
return
|
|
111
|
+
await self._app(scope, receive, send)
|
|
@@ -104,6 +104,8 @@ _ESSENTIAL_TOOLS: set[str] = {
|
|
|
104
104
|
"reinforce_assertion", "contradict_assertion",
|
|
105
105
|
# v3.4.11: Skill evolution (3)
|
|
106
106
|
"evolve_skill", "skill_health", "skill_lineage",
|
|
107
|
+
# v3.6.11: Surface B Optimize tools (5)
|
|
108
|
+
"slm_compress", "slm_retrieve", "slm_cache_set", "slm_cache_get", "slm_optimize_stats",
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
# v3.4.4: Mesh tools — enabled if mesh_enabled in config or SLM_MCP_MESH_TOOLS=1
|
|
@@ -189,6 +191,8 @@ register_code_graph_tools(_target, get_engine) # CodeGraph: filtered like other
|
|
|
189
191
|
register_mesh_tools(_target, get_engine) # v3.4.4: Mesh P2P tools — ships with SLM, no separate slm-mesh needed
|
|
190
192
|
register_learning_tools(_target, get_engine) # v3.4.7: Two-way learning tools
|
|
191
193
|
register_evolution_tools(_target, get_engine) # v3.4.11: Skill evolution tools
|
|
194
|
+
from superlocalmemory.mcp.tools_optimize import register_optimize_tools
|
|
195
|
+
register_optimize_tools(_target) # v3.6.11: Surface B Optimize tools (proxy-free)
|
|
192
196
|
|
|
193
197
|
|
|
194
198
|
# V3.3.21: Eager engine warmup — start initializing BEFORE first tool call.
|
|
@@ -109,15 +109,14 @@ def _sqlite_emergency_recall(
|
|
|
109
109
|
def _get_agent_id(default: str = "mcp_client") -> str:
|
|
110
110
|
"""Resolve the calling agent's ID for attribution.
|
|
111
111
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
v3.4.39+: enables proper per-agent attribution in ``session_init``,
|
|
118
|
-
``observe``, and event emissions.
|
|
112
|
+
Priority chain (v3.6.10+):
|
|
113
|
+
1. ContextVar set by HTTP URL path (/mcp/{agent_id}) — HTTP transport.
|
|
114
|
+
2. SLM_AGENT_ID env var — stdio transport per-process identity.
|
|
115
|
+
3. Provided default (legacy "mcp_client").
|
|
119
116
|
"""
|
|
120
|
-
|
|
117
|
+
from superlocalmemory.mcp.agent_context import get_current_agent_id
|
|
118
|
+
resolved = get_current_agent_id(env_fallback=True)
|
|
119
|
+
return resolved if resolved != "mcp_client" else default
|
|
121
120
|
|
|
122
121
|
|
|
123
122
|
def _emit_event(event_type: str, payload: dict | None = None,
|
|
@@ -110,6 +110,10 @@ def register_core_tools(server, get_engine: Callable) -> None:
|
|
|
110
110
|
Extracts atomic facts, resolves entities, builds graph edges,
|
|
111
111
|
and indexes for 4-channel retrieval.
|
|
112
112
|
"""
|
|
113
|
+
# v3.6.10: resolve "mcp_client" sentinel → URL path (HTTP) or env var (stdio)
|
|
114
|
+
if agent_id == "mcp_client":
|
|
115
|
+
from superlocalmemory.mcp.agent_context import get_current_agent_id
|
|
116
|
+
agent_id = get_current_agent_id()
|
|
113
117
|
meta = {
|
|
114
118
|
"project": project,
|
|
115
119
|
"importance": importance,
|
|
@@ -171,6 +175,10 @@ def register_core_tools(server, get_engine: Callable) -> None:
|
|
|
171
175
|
``CLAUDE_SESSION_ID``. Omitting it degrades to "no closed-loop
|
|
172
176
|
learning for this recall" — the recall itself always works.
|
|
173
177
|
"""
|
|
178
|
+
# v3.6.10: resolve "mcp_client" sentinel → URL path (HTTP) or env var (stdio)
|
|
179
|
+
if agent_id == "mcp_client":
|
|
180
|
+
from superlocalmemory.mcp.agent_context import get_current_agent_id
|
|
181
|
+
agent_id = get_current_agent_id()
|
|
174
182
|
import asyncio
|
|
175
183
|
try:
|
|
176
184
|
from superlocalmemory.mcp._daemon_proxy import choose_pool
|
|
@@ -485,6 +493,10 @@ def register_core_tools(server, get_engine: Callable) -> None:
|
|
|
485
493
|
fact_id: Exact fact ID to delete (from recall or list_recent results).
|
|
486
494
|
agent_id: Identifier of the calling agent (logged for audit).
|
|
487
495
|
"""
|
|
496
|
+
# v3.6.10: resolve "mcp_client" sentinel → URL path (HTTP) or env var (stdio)
|
|
497
|
+
if agent_id == "mcp_client":
|
|
498
|
+
from superlocalmemory.mcp.agent_context import get_current_agent_id
|
|
499
|
+
agent_id = get_current_agent_id()
|
|
488
500
|
try:
|
|
489
501
|
from superlocalmemory.core.worker_pool import WorkerPool
|
|
490
502
|
pool = WorkerPool.shared()
|
|
@@ -519,6 +531,10 @@ def register_core_tools(server, get_engine: Callable) -> None:
|
|
|
519
531
|
content: New content for the memory (cannot be empty).
|
|
520
532
|
agent_id: Identifier of the calling agent (logged for audit).
|
|
521
533
|
"""
|
|
534
|
+
# v3.6.10: resolve "mcp_client" sentinel → URL path (HTTP) or env var (stdio)
|
|
535
|
+
if agent_id == "mcp_client":
|
|
536
|
+
from superlocalmemory.mcp.agent_context import get_current_agent_id
|
|
537
|
+
agent_id = get_current_agent_id()
|
|
522
538
|
try:
|
|
523
539
|
if not content or not content.strip():
|
|
524
540
|
return {"success": False, "error": "content cannot be empty"}
|