tokensafe 4.4.1 → 4.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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.4.1",
2
+ "version": "4.4.3",
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.4.1",
3
+ "version": "4.4.3",
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/mcp_config.json CHANGED
@@ -6,6 +6,13 @@
6
6
  "-c",
7
7
  "exec \"${TOKENSAFE_PYTHON:-python3}\" \"$PLUGIN_ROOT/src/mcp_server.py\""
8
8
  ]
9
+ },
10
+ "tokensafe-mcp": {
11
+ "command": "npx",
12
+ "args": [
13
+ "-y",
14
+ "tokensafe-mcp"
15
+ ]
9
16
  }
10
17
  }
11
18
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tokensafe",
3
3
  "mcpName": "dev.tokensafe/tokensafe",
4
- "version": "4.4.1",
4
+ "version": "4.4.3",
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.4.1",
4
+ "version": "4.4.3",
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.4.1",
6
+ "version": "4.4.3",
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.4.1",
26
+ "version": "4.4.3",
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.4.1",
42
+ "version": "4.4.3",
43
43
  "runtimeHint": "uvx",
44
44
  "transport": {
45
45
  "type": "stdio"
package/src/installer.py CHANGED
@@ -1241,6 +1241,10 @@ WantedBy=default.target
1241
1241
  "command": PYTHON_BIN,
1242
1242
  "args": [MCP_SERVER_PATH]
1243
1243
  }
1244
+ data["mcpServers"]["tokensafe-mcp"] = {
1245
+ "command": "npx",
1246
+ "args": ["-y", "tokensafe-mcp"]
1247
+ }
1244
1248
 
1245
1249
  cls._atomic_write_json(config_file, data)
1246
1250
 
@@ -1282,6 +1286,10 @@ WantedBy=default.target
1282
1286
  "command": PYTHON_BIN,
1283
1287
  "args": [MCP_SERVER_PATH]
1284
1288
  }
1289
+ data["mcpServers"]["tokensafe-mcp"] = {
1290
+ "command": "npx",
1291
+ "args": ["-y", "tokensafe-mcp"]
1292
+ }
1285
1293
 
1286
1294
  cls._atomic_write_json(config_file, data)
1287
1295
 
@@ -1350,6 +1358,10 @@ WantedBy=default.target
1350
1358
  "command": PYTHON_BIN,
1351
1359
  "args": [MCP_SERVER_PATH]
1352
1360
  }
1361
+ data["mcpServers"]["tokensafe-mcp"] = {
1362
+ "command": "npx",
1363
+ "args": ["-y", "tokensafe-mcp"]
1364
+ }
1353
1365
  cls._atomic_write_json(config_file, data)
1354
1366
 
1355
1367
  if "agents_md_path" in info:
@@ -1374,6 +1386,12 @@ WantedBy=default.target
1374
1386
  "args": [MCP_SERVER_PATH],
1375
1387
  "tools": ["*"]
1376
1388
  }
1389
+ data["mcpServers"]["tokensafe-mcp"] = {
1390
+ "type": "stdio",
1391
+ "command": "npx",
1392
+ "args": ["-y", "tokensafe-mcp"],
1393
+ "tools": ["*"]
1394
+ }
1377
1395
  cls._atomic_write_json(config_file, data)
1378
1396
  return True, f"Configured MCP for GitHub Copilot in {config_file}"
1379
1397
 
@@ -1394,6 +1412,10 @@ WantedBy=default.target
1394
1412
  "command": PYTHON_BIN,
1395
1413
  "args": [MCP_SERVER_PATH]
1396
1414
  }
1415
+ data["mcp"]["servers"]["tokensafe-mcp"] = {
1416
+ "command": "npx",
1417
+ "args": ["-y", "tokensafe-mcp"]
1418
+ }
1397
1419
 
1398
1420
  cls._atomic_write_json(config_file, data)
1399
1421
 
@@ -1421,6 +1443,10 @@ WantedBy=default.target
1421
1443
  "command": PYTHON_BIN,
1422
1444
  "args": [MCP_SERVER_PATH]
1423
1445
  }
1446
+ data["mcpServers"]["tokensafe-mcp"] = {
1447
+ "command": "npx",
1448
+ "args": ["-y", "tokensafe-mcp"]
1449
+ }
1424
1450
  cls._atomic_write_json(mcp_file, data)
1425
1451
 
1426
1452
  managed_plugin_path = info.get("managed_plugin_path")
@@ -1518,8 +1544,14 @@ WantedBy=default.target
1518
1544
  if os.path.exists(config_file):
1519
1545
  data, err = cls._load_json_with_backup(config_file)
1520
1546
  if not err and data and isinstance(data.get("mcpServers"), dict):
1547
+ mutated = False
1521
1548
  if "tokensafe" in data["mcpServers"]:
1522
1549
  data["mcpServers"].pop("tokensafe")
1550
+ mutated = True
1551
+ if "tokensafe-mcp" in data["mcpServers"]:
1552
+ data["mcpServers"].pop("tokensafe-mcp")
1553
+ mutated = True
1554
+ if mutated:
1523
1555
  cls._atomic_write_json(config_file, data)
1524
1556
 
1525
1557
  cls._remove_path_safely(info.get("skills_path"))
@@ -1533,8 +1565,14 @@ WantedBy=default.target
1533
1565
  if os.path.exists(config_file):
1534
1566
  data, err = cls._load_json_with_backup(config_file)
1535
1567
  if not err and data and isinstance(data.get("mcpServers"), dict):
1568
+ mutated = False
1536
1569
  if "tokensafe" in data["mcpServers"]:
1537
1570
  data["mcpServers"].pop("tokensafe")
1571
+ mutated = True
1572
+ if "tokensafe-mcp" in data["mcpServers"]:
1573
+ data["mcpServers"].pop("tokensafe-mcp")
1574
+ mutated = True
1575
+ if mutated:
1538
1576
  cls._atomic_write_json(config_file, data)
1539
1577
 
1540
1578
  if "skills_path" in info:
@@ -1579,8 +1617,14 @@ WantedBy=default.target
1579
1617
  config_file = info["config_path"]
1580
1618
  data, err = cls._load_json_with_backup(config_file)
1581
1619
  if not err and data and isinstance(data.get("mcpServers"), dict):
1620
+ mutated = False
1582
1621
  if "tokensafe" in data["mcpServers"]:
1583
1622
  data["mcpServers"].pop("tokensafe")
1623
+ mutated = True
1624
+ if "tokensafe-mcp" in data["mcpServers"]:
1625
+ data["mcpServers"].pop("tokensafe-mcp")
1626
+ mutated = True
1627
+ if mutated:
1584
1628
  cls._atomic_write_json(config_file, data)
1585
1629
 
1586
1630
  if "agents_md_path" in info:
@@ -1596,8 +1640,14 @@ WantedBy=default.target
1596
1640
  if os.path.exists(config_file):
1597
1641
  data, err = cls._load_json_with_backup(config_file)
1598
1642
  if not err and data and isinstance(data.get("mcpServers"), dict):
1643
+ mutated = False
1599
1644
  if "tokensafe" in data["mcpServers"]:
1600
1645
  data["mcpServers"].pop("tokensafe")
1646
+ mutated = True
1647
+ if "tokensafe-mcp" in data["mcpServers"]:
1648
+ data["mcpServers"].pop("tokensafe-mcp")
1649
+ mutated = True
1650
+ if mutated:
1601
1651
  cls._atomic_write_json(config_file, data)
1602
1652
  return True, f"Removed MCP for GitHub Copilot in {config_file}"
1603
1653
 
@@ -1608,8 +1658,14 @@ WantedBy=default.target
1608
1658
  if not err and data:
1609
1659
  mcp = data.get("mcp")
1610
1660
  if isinstance(mcp, dict) and isinstance(mcp.get("servers"), dict):
1661
+ mutated = False
1611
1662
  if "tokensafe" in mcp["servers"]:
1612
1663
  mcp["servers"].pop("tokensafe")
1664
+ mutated = True
1665
+ if "tokensafe-mcp" in mcp["servers"]:
1666
+ mcp["servers"].pop("tokensafe-mcp")
1667
+ mutated = True
1668
+ if mutated:
1613
1669
  cls._atomic_write_json(config_file, data)
1614
1670
 
1615
1671
  if "skills_path" in info:
@@ -1628,8 +1684,14 @@ WantedBy=default.target
1628
1684
  if mcp_file and os.path.exists(mcp_file):
1629
1685
  data, err = cls._load_json_with_backup(mcp_file)
1630
1686
  if not err and data and isinstance(data.get("mcpServers"), dict):
1687
+ mutated = False
1631
1688
  if "tokensafe" in data["mcpServers"]:
1632
1689
  data["mcpServers"].pop("tokensafe")
1690
+ mutated = True
1691
+ if "tokensafe-mcp" in data["mcpServers"]:
1692
+ data["mcpServers"].pop("tokensafe-mcp")
1693
+ mutated = True
1694
+ if mutated:
1633
1695
  cls._atomic_write_json(mcp_file, data)
1634
1696
 
1635
1697
  managed_plugin_path = info.get("managed_plugin_path")
@@ -1878,6 +1940,10 @@ WantedBy=default.target
1878
1940
  "command": PYTHON_BIN,
1879
1941
  "args": [MCP_SERVER_PATH]
1880
1942
  }
1943
+ data["mcpServers"]["tokensafe-mcp"] = {
1944
+ "command": "npx",
1945
+ "args": ["-y", "tokensafe-mcp"]
1946
+ }
1881
1947
  cls._atomic_write_json(cursor_mcp, data)
1882
1948
  created.append(".cursor/mcp.json")
1883
1949
  except Exception:
@@ -2032,8 +2098,14 @@ WantedBy=default.target
2032
2098
  try:
2033
2099
  data, err = cls._load_json_with_backup(cursor_mcp)
2034
2100
  if not err and data and isinstance(data.get("mcpServers"), dict):
2101
+ mutated = False
2035
2102
  if "tokensafe" in data["mcpServers"]:
2036
2103
  data["mcpServers"].pop("tokensafe")
2104
+ mutated = True
2105
+ if "tokensafe-mcp" in data["mcpServers"]:
2106
+ data["mcpServers"].pop("tokensafe-mcp")
2107
+ mutated = True
2108
+ if mutated:
2037
2109
  if not data["mcpServers"] and list(data.keys()) == ["mcpServers"]:
2038
2110
  os.remove(cursor_mcp)
2039
2111
  else:
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.4.1"
45
+ "version": "4.4.3"
46
46
  }
47
47
 
48
48
  # MCP client attribution: the initialize handshake carries clientInfo, which
@@ -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.4.1"
244
+ "User-Agent": "TokenSafe-Sync/4.4.3"
245
245
  }
246
246
  if active_key:
247
247
  headers["Authorization"] = f"Bearer {active_key}"
@@ -27,7 +27,10 @@ from src.paths import resolve_data_dir
27
27
  class TypeSlicer:
28
28
  """Targeted extractor of external library types and interfaces."""
29
29
 
30
- _mem_cache: "OrderedDict[str, str]" = OrderedDict()
30
+ _mem_cache: "OrderedDict[str, Any]" = OrderedDict()
31
+ # Bumped whenever the slicing algorithm changes: stale slices cached under
32
+ # an older key format can never resurface through L1/L2/L3 (poisoning guard).
33
+ _SLICER_VERSION = "v2"
31
34
  _mem_lock = threading.Lock()
32
35
  _db_init_done = False
33
36
  _db_init_lock = threading.Lock()
@@ -38,10 +41,10 @@ class TypeSlicer:
38
41
  cls._mem_cache.clear()
39
42
 
40
43
  @classmethod
41
- def _mem_put(cls, key: str, val: str, max_items: int = 100) -> None:
44
+ def _mem_put(cls, key: str, val: str, dts_bytes: int = 0, max_items: int = 100) -> None:
42
45
  """Stores entry in LRU memory cache with fixed cap."""
43
46
  with cls._mem_lock:
44
- cls._mem_cache[key] = val
47
+ cls._mem_cache[key] = (val, dts_bytes)
45
48
  cls._mem_cache.move_to_end(key)
46
49
  if len(cls._mem_cache) > max_items:
47
50
  cls._mem_cache.popitem(last=False)
@@ -283,19 +286,22 @@ class TypeSlicer:
283
286
  except Exception:
284
287
  pass
285
288
 
286
- type_key = f"{pkg_name}@{version}::{sym}"
289
+ type_key = f"{pkg_name}@{version}::{sym}::{cls._SLICER_VERSION}"
287
290
 
288
291
  # 1. Check L1 Memory
289
292
  with cls._mem_lock:
290
293
  if type_key in cls._mem_cache:
291
294
  cls._mem_cache.move_to_end(type_key)
295
+ cached = cls._mem_cache[type_key]
296
+ code_val, dts_val = cached if isinstance(cached, tuple) else (cached, 0)
292
297
  return {
293
298
  "found": True,
294
299
  "package": pkg_name,
295
300
  "version": version,
296
301
  "symbol": sym,
297
- "type_code": cls._mem_cache[type_key],
298
- "source": "L1_memory"
302
+ "type_code": code_val,
303
+ "source": "L1_memory",
304
+ "_dts_bytes": dts_val
299
305
  }
300
306
 
301
307
  # 2. Check L2 SQLite
@@ -312,7 +318,7 @@ class TypeSlicer:
312
318
  if row:
313
319
  code = row[0]
314
320
  dts_b = row[1] if len(row) > 1 and row[1] else 0
315
- cls._mem_put(type_key, code, max_items=100)
321
+ cls._mem_put(type_key, code, dts_bytes=dts_b, max_items=100)
316
322
  return {
317
323
  "found": True,
318
324
  "package": pkg_name,
@@ -336,15 +342,26 @@ class TypeSlicer:
336
342
  from src.team_cache import TeamCache
337
343
  team_val = TeamCache.get("3RD_PARTY_DTS", type_key, timeout=0.5)
338
344
  if team_val:
339
- cls._mem_put(type_key, team_val, max_items=100)
345
+ code_block = team_val
346
+ dts_bytes = 0
347
+ if team_val.startswith("{"):
348
+ try:
349
+ p_val = json.loads(team_val)
350
+ if isinstance(p_val, dict) and "code" in p_val:
351
+ code_block = str(p_val["code"])
352
+ dts_bytes = int(p_val.get("dts_bytes", 0) or 0)
353
+ except Exception:
354
+ pass
355
+
356
+ cls._mem_put(type_key, code_block, dts_bytes=dts_bytes, max_items=100)
340
357
  # Backfill L2
341
358
  back_conn = None
342
359
  try:
343
360
  back_conn = sqlite3.connect(db_path, timeout=5.0)
344
361
  back_conn.execute("""
345
- INSERT OR REPLACE INTO type_cache (type_key, package_name, version, symbol, type_code, cached_at)
346
- VALUES (?, ?, ?, ?, ?, ?)
347
- """, (type_key, pkg_name, version, sym, team_val, int(time.time())))
362
+ INSERT OR REPLACE INTO type_cache (type_key, package_name, version, symbol, type_code, dts_bytes, cached_at)
363
+ VALUES (?, ?, ?, ?, ?, ?, ?)
364
+ """, (type_key, pkg_name, version, sym, code_block, dts_bytes, int(time.time())))
348
365
  cls._maybe_evict(back_conn)
349
366
  back_conn.commit()
350
367
  except Exception:
@@ -360,8 +377,9 @@ class TypeSlicer:
360
377
  "package": pkg_name,
361
378
  "version": version,
362
379
  "symbol": sym,
363
- "type_code": team_val,
364
- "source": "L3_team_cache"
380
+ "type_code": code_block,
381
+ "source": "L3_team_cache",
382
+ "_dts_bytes": dts_bytes
365
383
  }
366
384
  except Exception:
367
385
  pass
@@ -397,7 +415,7 @@ class TypeSlicer:
397
415
  }
398
416
 
399
417
  # 5. Save to L1, L2 SQLite, and broadcast to L3 Team Sync
400
- cls._mem_put(type_key, code_block, max_items=100)
418
+ cls._mem_put(type_key, code_block, dts_bytes=dts_bytes, max_items=100)
401
419
 
402
420
  save_conn = None
403
421
  try:
@@ -419,7 +437,9 @@ class TypeSlicer:
419
437
 
420
438
  try:
421
439
  from src.team_cache import TeamCache
422
- TeamCache.put("3RD_PARTY_DTS", type_key, code_block, tokens_saved=max(30, len(code_block)//4))
440
+ payload = json.dumps({"code": code_block, "dts_bytes": dts_bytes}, ensure_ascii=False)
441
+ tokens_saved = max(30, (dts_bytes // 4) if dts_bytes else len(code_block) // 4)
442
+ TeamCache.put("3RD_PARTY_DTS", type_key, payload, tokens_saved=tokens_saved)
423
443
  except Exception:
424
444
  pass
425
445
 
@@ -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.4.1"
20
+ CURRENT_VERSION = "4.4.3"
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