tokensafe 4.2.12 → 4.2.13

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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.2.12",
2
+ "version": "4.2.13",
3
3
  "name": "tokensafe",
4
4
  "description": "Your AI agent is smart enough to code all day. Now it actually can. Local runtime firewall & zero-loss context optimizer for AI coding agents.",
5
5
  "author": {
package/kimi.plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokensafe",
3
- "version": "4.2.12",
3
+ "version": "4.2.13",
4
4
  "description": "Your AI agent is smart enough to code all day. Now it actually can. Local runtime firewall & zero-loss context optimizer for AI coding agents.",
5
5
  "homepage": "https://tokensafe.dev",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tokensafe",
3
3
  "mcpName": "dev.tokensafe/tokensafe",
4
- "version": "4.2.12",
4
+ "version": "4.2.13",
5
5
  "description": "Your AI agent is smart enough to code all day. Now it actually can. Local runtime firewall & zero-loss context optimizer for AI coding agents.",
6
6
  "main": "bin/tokensafe.js",
7
7
  "bin": {
package/plugin.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tokensafe",
3
3
  "displayName": "TokenSafe",
4
- "version": "4.2.12",
4
+ "version": "4.2.13",
5
5
  "description": "Your AI agent is smart enough to code all day. Now it actually can. Local runtime firewall & zero-loss context optimizer for AI coding agents.",
6
6
  "logo": "assets/logo.png",
7
7
  "developerName": "TokenSafe",
package/server.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "dev.tokensafe/tokensafe",
4
4
  "title": "TokenSafe",
5
5
  "description": "Your AI agent is smart enough to code all day. Local firewall & zero-loss runtime.",
6
- "version": "4.2.12",
6
+ "version": "4.2.13",
7
7
  "websiteUrl": "https://tokensafe.dev",
8
8
  "repository": {
9
9
  "url": "https://github.com/tokensafe-dev/tokensafe",
@@ -23,7 +23,7 @@
23
23
  "registryType": "npm",
24
24
  "registryBaseUrl": "https://registry.npmjs.org",
25
25
  "identifier": "tokensafe",
26
- "version": "4.2.12",
26
+ "version": "4.2.13",
27
27
  "runtimeHint": "npx",
28
28
  "transport": {
29
29
  "type": "stdio"
@@ -39,7 +39,7 @@
39
39
  "registryType": "pypi",
40
40
  "registryBaseUrl": "https://pypi.org",
41
41
  "identifier": "tokensafe",
42
- "version": "4.2.12",
42
+ "version": "4.2.13",
43
43
  "runtimeHint": "uvx",
44
44
  "transport": {
45
45
  "type": "stdio"
@@ -17,6 +17,7 @@ import threading
17
17
  from typing import Dict, Any, Tuple, Optional, List
18
18
 
19
19
  from src.paths import resolve_data_dir
20
+ from src.ssl_context import shared_ssl_context
20
21
 
21
22
  PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
22
23
  DATA_DIR = resolve_data_dir()
@@ -226,7 +227,10 @@ class LicenseManager:
226
227
  headers={"Content-Type": "application/json", "User-Agent": "TokenSafe/Community-License"},
227
228
  method="POST"
228
229
  )
229
- with urllib.request.urlopen(req, timeout=2.5) as resp:
230
+ # Shared SSL context: this runs in a daemon thread (session_start
231
+ # hook) that can outlive the main thread; a lazily created context
232
+ # there races OPENSSL_cleanup at interpreter exit (SIGSEGV).
233
+ with urllib.request.urlopen(req, timeout=2.5, context=shared_ssl_context()) as resp:
230
234
  data = json.loads(resp.read().decode("utf-8") or "{}")
231
235
  license_key = str(data.get("license_key", "") or "")
232
236
  if resp.status != 200 or not data.get("success") or not license_key:
@@ -548,7 +552,7 @@ class LicenseManager:
548
552
  headers={"Content-Type": "application/json", "User-Agent": "TokenSafe/Refresh"},
549
553
  method="POST"
550
554
  )
551
- with urllib.request.urlopen(req, timeout=3.5) as resp:
555
+ with urllib.request.urlopen(req, timeout=3.5, context=shared_ssl_context()) as resp:
552
556
  data = json.loads(resp.read().decode("utf-8") or "{}")
553
557
  new_key = data.get("license_key", "")
554
558
  if resp.status == 200 and data.get("success") and new_key:
package/src/live_relay.py CHANGED
@@ -16,6 +16,7 @@ from collections import deque
16
16
  from typing import Dict
17
17
 
18
18
  from src.license_manager import LicenseManager
19
+ from src.ssl_context import shared_ssl_context
19
20
 
20
21
  _MACHINE_HASH = ""
21
22
 
@@ -94,7 +95,7 @@ class LiveRelay:
94
95
  "Authorization": f"Bearer {key}",
95
96
  "User-Agent": "TokenSafe-LiveRelay/2.8",
96
97
  }, method="POST")
97
- with urllib.request.urlopen(req, timeout=4.0) as resp:
98
+ with urllib.request.urlopen(req, timeout=4.0, context=shared_ssl_context()) as resp:
98
99
  resp.read()
99
100
  except Exception:
100
101
  pass # live telemetry is best-effort and must never block the proxy
package/src/mcp_server.py CHANGED
@@ -42,7 +42,7 @@ from src.paths import get_active_workspace, is_plugin_or_system_dir
42
42
 
43
43
  SERVER_INFO = {
44
44
  "name": "tokensafe-mcp",
45
- "version": "4.2.12"
45
+ "version": "4.2.13"
46
46
  }
47
47
 
48
48
  # MCP client attribution: the initialize handshake carries clientInfo, which
@@ -2035,7 +2035,7 @@ def run_mcp_server():
2035
2035
  finally:
2036
2036
  try:
2037
2037
  from src.telemetry_sync import TelemetrySync
2038
- TelemetrySync.sync_background(throttle_seconds=20.0)
2038
+ TelemetrySync.sync_background(throttle_seconds=5.0)
2039
2039
  except Exception:
2040
2040
  pass
2041
2041
  else:
package/src/proxy.py CHANGED
@@ -1032,7 +1032,7 @@ class TokenSafeProxyHandler(BaseHTTPRequestHandler):
1032
1032
  measurement="measured", # bytes literally swapped
1033
1033
  )
1034
1034
  from src.telemetry_sync import TelemetrySync
1035
- TelemetrySync.sync_background(throttle_seconds=20.0)
1035
+ TelemetrySync.sync_background(throttle_seconds=5.0)
1036
1036
  except Exception:
1037
1037
  pass
1038
1038
  target_symbol = compaction.get("symbol")
@@ -0,0 +1,22 @@
1
+ """Shared SSL context, created once on the importing (main) thread.
2
+
3
+ Short-lived hook processes exit while daemon threads (live-relay flush,
4
+ team-cache put) are still opening HTTPS. With urllib's default handler
5
+ each thread lazily builds its own SSLContext, and on Python 3.13 +
6
+ OpenSSL 3.x that lazy set_default_verify_paths can collide with
7
+ OPENSSL_cleanup running on the main thread during interpreter shutdown
8
+ (observed as SIGSEGV in pthread_rwlock_wrlock from EVP_PKEY_asn1_find_str
9
+ while the main thread is already in exit()). Sharing one pre-created
10
+ context removes the lazy per-thread store mutation entirely, and importing
11
+ this module also warms the OpenSSL store on the main thread before any
12
+ daemon thread starts.
13
+ """
14
+
15
+ import ssl
16
+
17
+ _CONTEXT = ssl.create_default_context()
18
+
19
+
20
+ def shared_ssl_context() -> ssl.SSLContext:
21
+ """Returns the process-wide SSLContext (safe to share across threads)."""
22
+ return _CONTEXT
package/src/team_cache.py CHANGED
@@ -14,6 +14,7 @@ import urllib.error
14
14
  import urllib.parse
15
15
  from typing import Optional, Dict, Any, Tuple
16
16
  from src.license_manager import LicenseManager
17
+ from src.ssl_context import shared_ssl_context
17
18
 
18
19
  class TeamCache:
19
20
  """Distributed cross-developer cache client for TokenSafe Team tier."""
@@ -105,7 +106,7 @@ class TeamCache:
105
106
 
106
107
  eff_timeout = cls.DEFAULT_TIMEOUT if timeout is None else max(0.05, float(timeout))
107
108
  try:
108
- with urllib.request.urlopen(req, timeout=eff_timeout) as resp:
109
+ with urllib.request.urlopen(req, timeout=eff_timeout, context=shared_ssl_context()) as resp:
109
110
  if resp.status == 200:
110
111
  data = json.loads(resp.read().decode("utf-8"))
111
112
  val = data.get("value") or data.get("cached_value")
@@ -176,7 +177,7 @@ class TeamCache:
176
177
  }
177
178
  )
178
179
  try:
179
- with urllib.request.urlopen(req, timeout=1.5):
180
+ with urllib.request.urlopen(req, timeout=1.5, context=shared_ssl_context()):
180
181
  cls._clear_cooldown(cloud_url)
181
182
  except urllib.error.HTTPError as he:
182
183
  if he.code in (401, 403) or he.code >= 500:
@@ -200,7 +201,7 @@ class TeamCache:
200
201
  headers={"Authorization": f"Bearer {lic_key}", "User-Agent": "TokenSafe-TeamCache/1.0"}
201
202
  )
202
203
  try:
203
- with urllib.request.urlopen(req, timeout=max(2.5, cls.DEFAULT_TIMEOUT)) as resp:
204
+ with urllib.request.urlopen(req, timeout=max(2.5, cls.DEFAULT_TIMEOUT), context=shared_ssl_context()) as resp:
204
205
  if resp.status == 200:
205
206
  res = json.loads(resp.read().decode("utf-8"))
206
207
  res["active"] = True
@@ -241,7 +241,7 @@ class TelemetrySync:
241
241
  current_last_id = last_id
242
242
  headers = {
243
243
  "Content-Type": "application/json",
244
- "User-Agent": "TokenSafe-Sync/4.2.12"
244
+ "User-Agent": "TokenSafe-Sync/4.2.13"
245
245
  }
246
246
  if active_key:
247
247
  headers["Authorization"] = f"Bearer {active_key}"
@@ -496,11 +496,15 @@ class TelemetrySync:
496
496
  return cls._is_pid_alive(pid)
497
497
 
498
498
  @classmethod
499
- def sync_background(cls, throttle_seconds: float = 20.0, force: bool = False):
499
+ def sync_background(cls, throttle_seconds: float = 5.0, force: bool = False):
500
500
  """
501
501
  Spawns a detached, non-blocking background OS process to sync metrics.
502
- Throttled to run at most once every `throttle_seconds` (default: 20s)
503
- using persistent state from sync_state.json.
502
+ Throttled to run at most once every `throttle_seconds` (default: 5s) so
503
+ cloud-side live feeds (public network panel, dashboards) reflect fresh
504
+ events within seconds instead of tens of seconds. Sync only fires when
505
+ pending events exist, and failure backoff (2^n up to 300s) still guards
506
+ flaky connections, so the tighter cadence adds no idle traffic.
507
+ Uses persistent state from sync_state.json.
504
508
  Detached from parent process group so it survives the termination of short-lived CLI lifecycle hooks.
505
509
  """
506
510
  if _telemetry_disabled():
@@ -8,11 +8,14 @@ languages:
8
8
 
9
9
  All language grammars are standard mandatory dependencies with pre-compiled wheels.
10
10
  Fail-safe fallback to stdlib ast / regex lexer is preserved if a grammar cannot load.
11
+ Isolated runs (`python -I` / `-s`, sanitized env) hide user site-packages where the
12
+ wheels usually live; the backend probes that directory explicitly before degrading.
11
13
  """
12
14
 
13
15
  import importlib
14
16
  import os
15
17
  import re
18
+ import sys
16
19
  from typing import Any, Callable, Dict, List, Optional, Set, Tuple
17
20
 
18
21
  # Extension -> (module_name, loader_attr)
@@ -134,6 +137,22 @@ class TreeSitterBackend:
134
137
  flag = os.environ.get("TOKENSAFE_TREESITTER", "auto").strip().lower()
135
138
  return flag not in ("0", "false", "off", "no", "disabled")
136
139
 
140
+ @staticmethod
141
+ def _probe_user_site() -> bool:
142
+ """Isolated runs (`python -I` / `-s`) skip user site-packages, where the
143
+ grammar wheels typically live. If that directory exists on disk but is
144
+ missing from sys.path, append it so the lazy imports can recover."""
145
+ try:
146
+ import site
147
+
148
+ user_sp = site.getusersitepackages()
149
+ except Exception:
150
+ return False
151
+ if user_sp and os.path.isdir(user_sp) and user_sp not in sys.path:
152
+ sys.path.append(user_sp)
153
+ return True
154
+ return False
155
+
137
156
  @classmethod
138
157
  def is_available(cls) -> bool:
139
158
  if not cls.is_enabled():
@@ -144,6 +163,11 @@ class TreeSitterBackend:
144
163
  cls._ts_mod = importlib.import_module("tree_sitter")
145
164
  except Exception:
146
165
  cls._ts_mod = None
166
+ try:
167
+ if cls._probe_user_site():
168
+ cls._ts_mod = importlib.import_module("tree_sitter")
169
+ except Exception:
170
+ cls._ts_mod = None
147
171
  return cls._ts_mod is not None
148
172
 
149
173
  @classmethod
@@ -17,7 +17,7 @@ import urllib.request
17
17
  import urllib.error
18
18
  from typing import Dict, Any, Optional, Tuple
19
19
 
20
- CURRENT_VERSION = "4.2.12"
20
+ CURRENT_VERSION = "4.2.13"
21
21
  CACHE_TTL = 86400 # 24 hours
22
22
  # When the cloud is unreachable we must NOT cache the synthesized "up to date"
23
23
  # answer for a full day — otherwise an update released right after an outage