tokensafe 2.4.2 → 2.4.3
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/README.md +2 -1
- package/package.json +2 -2
- package/plugin.json +2 -2
- package/server.json +3 -3
- package/src/distiller.py +1 -10
- package/src/installer.py +7 -1
- package/src/license_manager.py +72 -27
- package/src/mcp_server.py +1 -1
- package/src/team_cache.py +1 -19
- package/src/telemetry_sync.py +1 -1
- package/src/version_checker.py +17 -7
package/README.md
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
<p align="center">
|
|
9
9
|
<b>Local-First AI Guardrails, Focused Context & Auditable Team ROI</b><br>
|
|
10
|
-
|
|
10
|
+
Reduce your AI bills by up to 90%. Let your team ship clean code faster and cheaper.<br>
|
|
11
|
+
TokenSafe runs locally between your agents and your repos: agents read only the code they need (context spend 60-90% lower, measured / estimated) and secrets get blocked before commit.<br>
|
|
11
12
|
Built for teams using <b>Cursor, Claude Code, Codex, Windsurf, Google Antigravity, Gemini CLI, and ChatGPT</b>.
|
|
12
13
|
</p>
|
|
13
14
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"mcpName": "dev.tokensafe/tokensafe",
|
|
4
|
-
"version": "2.4.
|
|
5
|
-
"description": "Local-first AI
|
|
4
|
+
"version": "2.4.3",
|
|
5
|
+
"description": "Local-first AI guardrails: reduce AI bills by up to 90%, focused context, zero leaked secrets.",
|
|
6
6
|
"main": "bin/tokensafe.js",
|
|
7
7
|
"bin": {
|
|
8
8
|
"tokensafe": "bin/tokensafe.js"
|
package/plugin.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"displayName": "TokenSafe",
|
|
4
|
-
"version": "2.4.
|
|
5
|
-
"description": "Local-first guardrails
|
|
4
|
+
"version": "2.4.3",
|
|
5
|
+
"description": "Local-first AI guardrails: reduce AI bills by up to 90%, focused context, zero leaked secrets.",
|
|
6
6
|
"logo": "assets/logo.svg",
|
|
7
7
|
"author": {
|
|
8
8
|
"name": "Oleg Savelev",
|
package/server.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "dev.tokensafe/tokensafe",
|
|
4
4
|
"title": "TokenSafe",
|
|
5
5
|
"description": "Cut AI coding agent token usage up to 90% via AST slicing, dependency graphs & security guards.",
|
|
6
|
-
"version": "2.4.
|
|
6
|
+
"version": "2.4.3",
|
|
7
7
|
"websiteUrl": "https://tokensafe.dev",
|
|
8
8
|
"repository": {
|
|
9
9
|
"url": "https://github.com/tokensafe-dev/tokensafe",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"registryType": "npm",
|
|
31
31
|
"registryBaseUrl": "https://registry.npmjs.org",
|
|
32
32
|
"identifier": "tokensafe",
|
|
33
|
-
"version": "2.4.
|
|
33
|
+
"version": "2.4.3",
|
|
34
34
|
"runtimeHint": "npx",
|
|
35
35
|
"transport": {
|
|
36
36
|
"type": "stdio"
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"registryType": "pypi",
|
|
47
47
|
"registryBaseUrl": "https://pypi.org",
|
|
48
48
|
"identifier": "tokensafe",
|
|
49
|
-
"version": "2.4.
|
|
49
|
+
"version": "2.4.3",
|
|
50
50
|
"runtimeHint": "uvx",
|
|
51
51
|
"transport": {
|
|
52
52
|
"type": "stdio"
|
package/src/distiller.py
CHANGED
|
@@ -213,16 +213,7 @@ Rules:
|
|
|
213
213
|
|
|
214
214
|
# 1. Managed Cloud Distillation (Default for Solo/Team SaaS)
|
|
215
215
|
if "cloud" in providers:
|
|
216
|
-
lic_key =
|
|
217
|
-
if not lic_key:
|
|
218
|
-
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
219
|
-
lic_file = os.path.join(base_dir, "data", "license.json")
|
|
220
|
-
if os.path.exists(lic_file):
|
|
221
|
-
try:
|
|
222
|
-
with open(lic_file, "r", encoding="utf-8") as f:
|
|
223
|
-
lic_key = json.load(f).get("license_key", "").strip()
|
|
224
|
-
except Exception:
|
|
225
|
-
pass
|
|
216
|
+
lic_key = LicenseManager.get_active_license_key()
|
|
226
217
|
|
|
227
218
|
if lic_key:
|
|
228
219
|
cloud_res = cls._call_cloud_distill(raw_text, lic_key)
|
package/src/installer.py
CHANGED
|
@@ -922,7 +922,7 @@ class AgentInstaller:
|
|
|
922
922
|
else:
|
|
923
923
|
print(f" {YELLOW}⚠{RESET} {info['name']}: {msg}")
|
|
924
924
|
|
|
925
|
-
# 5. Refresh local version cache
|
|
925
|
+
# 5. Refresh local version cache and report the newly installed version in a fresh process
|
|
926
926
|
try:
|
|
927
927
|
from src.paths import resolve_data_dir
|
|
928
928
|
cache_file = os.path.join(resolve_data_dir(), "version_cache.json")
|
|
@@ -930,6 +930,12 @@ class AgentInstaller:
|
|
|
930
930
|
os.remove(cache_file)
|
|
931
931
|
except Exception:
|
|
932
932
|
pass
|
|
933
|
+
try:
|
|
934
|
+
tokensafe_bin = shutil.which("tokensafe")
|
|
935
|
+
sync_cmd = [tokensafe_bin, "version", "--check"] if tokensafe_bin else [sys.executable, os.path.join(PROJECT_ROOT, "src", "cli.py"), "version", "--check"]
|
|
936
|
+
subprocess.run(sync_cmd, capture_output=True, text=True, timeout=5)
|
|
937
|
+
except Exception:
|
|
938
|
+
pass
|
|
933
939
|
|
|
934
940
|
# 6. Restart running MCP server daemons so IDEs load the new version
|
|
935
941
|
restarted_count = cls.restart_mcp_daemons()
|
package/src/license_manager.py
CHANGED
|
@@ -12,7 +12,7 @@ import hmac
|
|
|
12
12
|
import hashlib
|
|
13
13
|
import base64
|
|
14
14
|
import threading
|
|
15
|
-
from typing import Dict, Any, Tuple, Optional
|
|
15
|
+
from typing import Dict, Any, Tuple, Optional, List
|
|
16
16
|
|
|
17
17
|
from src.paths import resolve_data_dir
|
|
18
18
|
|
|
@@ -21,6 +21,8 @@ DATA_DIR = resolve_data_dir()
|
|
|
21
21
|
LICENSE_FILE = os.path.join(DATA_DIR, "license.json")
|
|
22
22
|
GLOBAL_DATA_DIR = os.path.expanduser("~/.tokensafe")
|
|
23
23
|
GLOBAL_LICENSE_FILE = os.path.join(GLOBAL_DATA_DIR, "license.json")
|
|
24
|
+
_DEFAULT_LICENSE_FILE = LICENSE_FILE
|
|
25
|
+
_DEFAULT_GLOBAL_LICENSE_FILE = GLOBAL_LICENSE_FILE
|
|
24
26
|
|
|
25
27
|
# Master Public Key for client-side offline verification (RFC 8032 Ed25519)
|
|
26
28
|
DEFAULT_ED25519_PUBLIC_KEY = os.environ.get("TOKENSAFE_PUBLIC_KEY", "3911e2ffe5ad07ee23be392b88f238af302f9d3a0bf51881de8b5e0e6af71430")
|
|
@@ -297,40 +299,81 @@ class LicenseManager:
|
|
|
297
299
|
return True, payload, "License is active and valid"
|
|
298
300
|
|
|
299
301
|
@classmethod
|
|
300
|
-
def
|
|
301
|
-
"""
|
|
302
|
-
|
|
303
|
-
if
|
|
302
|
+
def _candidate_license_files(cls) -> List[str]:
|
|
303
|
+
"""Returns license.json paths to inspect, honoring explicit TOKENSAFE_DATA_DIR and test patches."""
|
|
304
|
+
explicit_dir = os.environ.get("TOKENSAFE_DATA_DIR", "").strip()
|
|
305
|
+
if LICENSE_FILE != _DEFAULT_LICENSE_FILE:
|
|
306
|
+
paths = [LICENSE_FILE]
|
|
307
|
+
if GLOBAL_LICENSE_FILE != _DEFAULT_GLOBAL_LICENSE_FILE:
|
|
308
|
+
paths.append(GLOBAL_LICENSE_FILE)
|
|
309
|
+
elif explicit_dir:
|
|
310
|
+
paths = [os.path.join(explicit_dir, "license.json")]
|
|
311
|
+
else:
|
|
312
|
+
paths = [LICENSE_FILE, GLOBAL_LICENSE_FILE]
|
|
313
|
+
|
|
314
|
+
seen = set()
|
|
315
|
+
unique: List[str] = []
|
|
316
|
+
for p in paths:
|
|
317
|
+
ap = os.path.abspath(p)
|
|
318
|
+
if ap not in seen:
|
|
319
|
+
seen.add(ap)
|
|
320
|
+
unique.append(p)
|
|
321
|
+
return unique
|
|
322
|
+
|
|
323
|
+
@classmethod
|
|
324
|
+
def _resolve_active_license_record(cls) -> Tuple[Optional[Tuple[str, Dict[str, Any]]], str]:
|
|
325
|
+
"""Selects the freshest valid license across env and candidate files."""
|
|
326
|
+
env_key = os.environ.get("TOKENSAFE_LICENSE_KEY", "").strip()
|
|
327
|
+
if env_key:
|
|
328
|
+
valid, payload, _ = cls.validate_license(env_key)
|
|
329
|
+
return ((env_key, payload) if valid else None), env_key
|
|
330
|
+
|
|
331
|
+
best_valid: Optional[Tuple[Tuple[int, int, float], str, Dict[str, Any]]] = None
|
|
332
|
+
best_raw: Optional[Tuple[Tuple[int, float], str]] = None
|
|
333
|
+
|
|
334
|
+
for path in cls._candidate_license_files():
|
|
335
|
+
if not os.path.exists(path):
|
|
336
|
+
continue
|
|
304
337
|
try:
|
|
305
|
-
with open(
|
|
338
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
306
339
|
data = json.load(f)
|
|
307
|
-
|
|
340
|
+
candidate = str(data.get("license_key", "")).strip()
|
|
341
|
+
if not candidate:
|
|
342
|
+
continue
|
|
343
|
+
saved_at = int(data.get("saved_at") or 0)
|
|
344
|
+
mtime = os.path.getmtime(path)
|
|
308
345
|
except Exception:
|
|
309
|
-
|
|
346
|
+
continue
|
|
310
347
|
|
|
311
|
-
|
|
312
|
-
|
|
348
|
+
raw_rank = (saved_at, mtime)
|
|
349
|
+
if best_raw is None or raw_rank > best_raw[0]:
|
|
350
|
+
best_raw = (raw_rank, candidate)
|
|
351
|
+
|
|
352
|
+
valid, payload, _ = cls.validate_license(candidate)
|
|
313
353
|
if valid:
|
|
314
|
-
|
|
354
|
+
issued_at = int(payload.get("issued_at") or 0)
|
|
355
|
+
valid_rank = (saved_at, issued_at, mtime)
|
|
356
|
+
if best_valid is None or valid_rank > best_valid[0]:
|
|
357
|
+
best_valid = (valid_rank, candidate, payload)
|
|
358
|
+
|
|
359
|
+
if best_valid is not None:
|
|
360
|
+
return (best_valid[1], best_valid[2]), best_valid[1]
|
|
361
|
+
return None, (best_raw[1] if best_raw is not None else "")
|
|
362
|
+
|
|
363
|
+
@classmethod
|
|
364
|
+
def get_active_license(cls) -> Tuple[str, Dict[str, Any]]:
|
|
365
|
+
"""Retrieves active license from env or persistent license.json files, falling back to free tier."""
|
|
366
|
+
resolved, _ = cls._resolve_active_license_record()
|
|
367
|
+
if resolved is not None:
|
|
368
|
+
_, payload = resolved
|
|
369
|
+
return payload.get("tier", "solo"), payload
|
|
315
370
|
|
|
316
371
|
return "free", {"tier": "free", "max_tokens_monthly": 500_000}
|
|
317
372
|
|
|
318
373
|
@classmethod
|
|
319
374
|
def get_active_license_key(cls) -> str:
|
|
320
|
-
"""Retrieves active raw license key string from env or persistent license.json files."""
|
|
321
|
-
key =
|
|
322
|
-
if not key:
|
|
323
|
-
for p in (LICENSE_FILE, GLOBAL_LICENSE_FILE):
|
|
324
|
-
if os.path.exists(p):
|
|
325
|
-
try:
|
|
326
|
-
with open(p, "r", encoding="utf-8") as f:
|
|
327
|
-
data = json.load(f)
|
|
328
|
-
candidate = data.get("license_key", "").strip()
|
|
329
|
-
if candidate:
|
|
330
|
-
key = candidate
|
|
331
|
-
break
|
|
332
|
-
except Exception:
|
|
333
|
-
pass
|
|
375
|
+
"""Retrieves the freshest active raw license key string from env or persistent license.json files."""
|
|
376
|
+
_, key = cls._resolve_active_license_record()
|
|
334
377
|
return key
|
|
335
378
|
|
|
336
379
|
@classmethod
|
|
@@ -349,9 +392,11 @@ class LicenseManager:
|
|
|
349
392
|
"saved_at": int(time.time())
|
|
350
393
|
}
|
|
351
394
|
|
|
352
|
-
for
|
|
395
|
+
for target_file in cls._candidate_license_files():
|
|
396
|
+
target_dir = os.path.dirname(target_file)
|
|
353
397
|
try:
|
|
354
|
-
|
|
398
|
+
if target_dir:
|
|
399
|
+
os.makedirs(target_dir, exist_ok=True)
|
|
355
400
|
with open(target_file, "w", encoding="utf-8") as f:
|
|
356
401
|
json.dump(record, f, indent=2)
|
|
357
402
|
except Exception:
|
package/src/mcp_server.py
CHANGED
package/src/team_cache.py
CHANGED
|
@@ -26,25 +26,7 @@ class TeamCache:
|
|
|
26
26
|
@classmethod
|
|
27
27
|
def _is_team_tier(cls) -> Tuple[bool, str]:
|
|
28
28
|
"""Checks if current active license belongs to a Team tier subscription."""
|
|
29
|
-
key =
|
|
30
|
-
if not key:
|
|
31
|
-
from src.paths import resolve_data_dir
|
|
32
|
-
candidate_paths = [
|
|
33
|
-
os.path.join(resolve_data_dir(), "license.json"),
|
|
34
|
-
os.path.join(os.getcwd(), "data", "license.json"),
|
|
35
|
-
os.path.expanduser("~/.tokensafe/license.json")
|
|
36
|
-
]
|
|
37
|
-
for lic_file in candidate_paths:
|
|
38
|
-
if os.path.exists(lic_file):
|
|
39
|
-
try:
|
|
40
|
-
with open(lic_file, "r", encoding="utf-8") as f:
|
|
41
|
-
data = json.load(f)
|
|
42
|
-
key = data.get("license_key", "").strip()
|
|
43
|
-
if key:
|
|
44
|
-
break
|
|
45
|
-
except Exception:
|
|
46
|
-
pass
|
|
47
|
-
|
|
29
|
+
key = LicenseManager.get_active_license_key()
|
|
48
30
|
if not key:
|
|
49
31
|
return False, ""
|
|
50
32
|
|
package/src/telemetry_sync.py
CHANGED
|
@@ -266,7 +266,7 @@ class TelemetrySync:
|
|
|
266
266
|
rollup_synced = bool(state.get("lifetime_rollup_synced", False))
|
|
267
267
|
headers = {
|
|
268
268
|
"Content-Type": "application/json",
|
|
269
|
-
"User-Agent": "TokenSafe-Sync/2.4.
|
|
269
|
+
"User-Agent": "TokenSafe-Sync/2.4.3"
|
|
270
270
|
}
|
|
271
271
|
if active_key:
|
|
272
272
|
headers["Authorization"] = f"Bearer {active_key}"
|
package/src/version_checker.py
CHANGED
|
@@ -16,7 +16,7 @@ import urllib.request
|
|
|
16
16
|
import urllib.error
|
|
17
17
|
from typing import Dict, Any, Optional, Tuple
|
|
18
18
|
|
|
19
|
-
CURRENT_VERSION = "2.4.
|
|
19
|
+
CURRENT_VERSION = "2.4.3"
|
|
20
20
|
CACHE_TTL = 86400 # 24 hours
|
|
21
21
|
# When the cloud is unreachable we must NOT cache the synthesized "up to date"
|
|
22
22
|
# answer for a full day — otherwise an update released right after an outage
|
|
@@ -91,6 +91,15 @@ class VersionChecker:
|
|
|
91
91
|
except Exception:
|
|
92
92
|
pass
|
|
93
93
|
|
|
94
|
+
@staticmethod
|
|
95
|
+
def _cache_matches_version(cached: Optional[Dict[str, Any]], current_version: str) -> bool:
|
|
96
|
+
"""True when cached payload was recorded for `current_version` (invalidates cache on upgrade)."""
|
|
97
|
+
if not cached:
|
|
98
|
+
return False
|
|
99
|
+
cached_ver = str(cached.get("client_version") or "").strip().lstrip("v")
|
|
100
|
+
curr_ver = str(current_version or "").strip().lstrip("v")
|
|
101
|
+
return bool(cached_ver) and cached_ver == curr_ver
|
|
102
|
+
|
|
94
103
|
@classmethod
|
|
95
104
|
def check_version(
|
|
96
105
|
cls,
|
|
@@ -100,8 +109,8 @@ class VersionChecker:
|
|
|
100
109
|
timeout: float = 1.5
|
|
101
110
|
) -> Dict[str, Any]:
|
|
102
111
|
"""
|
|
103
|
-
Retrieves version status. Returns cached result if within TTL
|
|
104
|
-
otherwise makes a fast network request (< 1.5s timeout).
|
|
112
|
+
Retrieves version status. Returns cached result if within TTL and matching
|
|
113
|
+
`current_version`, otherwise makes a fast network request (< 1.5s timeout).
|
|
105
114
|
Falls back gracefully on network or offline scenarios.
|
|
106
115
|
"""
|
|
107
116
|
now = int(time.time())
|
|
@@ -113,8 +122,9 @@ class VersionChecker:
|
|
|
113
122
|
license_key = ""
|
|
114
123
|
|
|
115
124
|
cached = cls.load_cache()
|
|
125
|
+
cache_matches = cls._cache_matches_version(cached, current_version)
|
|
116
126
|
|
|
117
|
-
if not force_fetch and
|
|
127
|
+
if not force_fetch and cache_matches:
|
|
118
128
|
checked_at = cached.get("checked_at", 0)
|
|
119
129
|
ttl = int(cached.get("ttl", CACHE_TTL))
|
|
120
130
|
if now - checked_at < ttl:
|
|
@@ -161,8 +171,8 @@ class VersionChecker:
|
|
|
161
171
|
except Exception:
|
|
162
172
|
pass
|
|
163
173
|
|
|
164
|
-
# Stale cache fallback if available
|
|
165
|
-
if
|
|
174
|
+
# Stale cache fallback if available for the same installed version
|
|
175
|
+
if cache_matches:
|
|
166
176
|
return cached
|
|
167
177
|
cls.save_cache(payload)
|
|
168
178
|
return payload
|
|
@@ -397,7 +407,7 @@ class VersionChecker:
|
|
|
397
407
|
"""
|
|
398
408
|
now = int(time.time())
|
|
399
409
|
cached = cls.load_cache()
|
|
400
|
-
if not force and cached:
|
|
410
|
+
if not force and cls._cache_matches_version(cached, current_version):
|
|
401
411
|
checked_at = cached.get("checked_at", 0)
|
|
402
412
|
update_available = cached.get("update_available", False)
|
|
403
413
|
latest = cached.get("latest_version", current_version)
|