tokensafe 4.2.10 → 4.2.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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.2.10",
2
+ "version": "4.2.11",
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.10",
3
+ "version": "4.2.11",
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.10",
4
+ "version": "4.2.11",
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.10",
4
+ "version": "4.2.11",
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.10",
6
+ "version": "4.2.11",
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.10",
26
+ "version": "4.2.11",
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.10",
42
+ "version": "4.2.11",
43
43
  "runtimeHint": "uvx",
44
44
  "transport": {
45
45
  "type": "stdio"
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.10"
45
+ "version": "4.2.11"
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.2.10"
244
+ "User-Agent": "TokenSafe-Sync/4.2.11"
245
245
  }
246
246
  if active_key:
247
247
  headers["Authorization"] = f"Bearer {active_key}"
package/src/tokenizer.py CHANGED
@@ -33,6 +33,16 @@ class TokenCounter:
33
33
  r"""'s|'t|'re|'ve|'m|'ll|'d|[a-zA-Z\u00C0-\u024F\u0400-\u04FF_]+|\d{1,3}|[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\u3040-\u30FF\u31F0-\u31FF\uAC00-\uD7AF]+|[^\w\s]+|\s+"""
34
34
  )
35
35
 
36
+ # Script detection via C-level regex scans — replaces per-char Python loops
37
+ # that previously dominated the hot path (~46% of runtime on mixed text).
38
+ _CYRILLIC_RE = re.compile(r"[\u0400-\u04FF]")
39
+ _CJK_RE = re.compile(
40
+ r"[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\u3040-\u30FF\u31F0-\u31FF\uAC00-\uD7AF]"
41
+ )
42
+ # English / code identifier subword split (hoisted: avoids the re-module
43
+ # cache lookup on every word of every payload).
44
+ _SUBWORD_REGEX = re.compile(r"[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|\W|$)|\d+")
45
+
36
46
  @classmethod
37
47
  def count(cls, text: Optional[str]) -> int:
38
48
  """Calculate or estimate token count for any text."""
@@ -59,9 +69,9 @@ class TokenCounter:
59
69
  # Check if whitespace
60
70
  if chunk.isspace():
61
71
  if "\n" in chunk:
62
- tokens += chunk.count("\n")
63
- remaining_spaces = len(chunk) - chunk.count("\n")
64
- tokens += remaining_spaces // 4
72
+ newlines = chunk.count("\n")
73
+ tokens += newlines
74
+ tokens += (len(chunk) - newlines) // 4
65
75
  else:
66
76
  # In BPE, single spaces are merged into adjacent word tokens (e.g. " world").
67
77
  # Only indentations (e.g., 4 spaces) add extra tokens.
@@ -79,17 +89,13 @@ class TokenCounter:
79
89
  tokens += max(1, (len(chunk) + 2) // 3)
80
90
  continue
81
91
 
82
- # Alphabetic words: Check script
83
- is_cyrillic = any('\u0400' <= char <= '\u04FF' for char in chunk)
84
- is_cjk = any(
85
- '\u4E00' <= char <= '\u9FFF' # CJK Unified Ideographs
86
- or '\u3400' <= char <= '\u4DBF' # CJK Extension A
87
- or '\uF900' <= char <= '\uFAFF' # CJK Compatibility Ideographs
88
- or '\u3040' <= char <= '\u30FF' # Hiragana / Katakana
89
- or '\u31F0' <= char <= '\u31FF' # Katakana Phonetic Extensions
90
- or '\uAC00' <= char <= '\uD7AF' # Hangul syllables
91
- for char in chunk
92
- )
92
+ # Alphabetic words: Check script. ASCII chunks (the dominant case for
93
+ # code and English payloads) cannot be Cyrillic or CJK — skip the scan.
94
+ if chunk.isascii():
95
+ is_cyrillic = is_cjk = False
96
+ else:
97
+ is_cyrillic = cls._CYRILLIC_RE.search(chunk) is not None
98
+ is_cjk = cls._CJK_RE.search(chunk) is not None
93
99
 
94
100
  if is_cjk:
95
101
  # CJK: typically ~1.8 tokens per character
@@ -106,7 +112,7 @@ class TokenCounter:
106
112
  tokens += max(3, (len(chunk) + 2) // 3)
107
113
  else:
108
114
  # English / Code identifier
109
- subwords = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|\W|$)|\d+', chunk)
115
+ subwords = cls._SUBWORD_REGEX.findall(chunk)
110
116
  if subwords:
111
117
  for sub in subwords:
112
118
  if len(sub) <= 7:
@@ -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.10"
20
+ CURRENT_VERSION = "4.2.11"
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