tokensafe 2.6.0 → 2.7.0
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/hooks/claude_code_hook.py +6 -0
- package/hooks/cursor_hook.py +6 -0
- package/hooks/post_tool_use.py +1 -1
- package/hooks/pre_invocation.py +2 -2
- package/hooks/pre_tool_use.py +13 -0
- package/hooks/stop_hook.py +6 -6
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/server.json +3 -3
- package/src/cli.py +5 -8
- package/src/dependency_prefetcher.py +2 -2
- package/src/graph_engine.py +653 -140
- package/src/mcp_server.py +2 -2
- package/src/proxy.py +2 -0
- package/src/skeletonizer.py +68 -13
- package/src/slicer.py +10 -4
- package/src/telemetry_sync.py +278 -185
- package/src/treesitter_backend.py +538 -0
- package/src/version_checker.py +1 -1
package/src/mcp_server.py
CHANGED
|
@@ -40,7 +40,7 @@ from src.license_manager import LicenseManager
|
|
|
40
40
|
|
|
41
41
|
SERVER_INFO = {
|
|
42
42
|
"name": "tokensafe-mcp",
|
|
43
|
-
"version": "2.
|
|
43
|
+
"version": "2.7.0"
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
# MCP client attribution: the initialize handshake carries clientInfo, which
|
|
@@ -1250,7 +1250,7 @@ def run_mcp_server():
|
|
|
1250
1250
|
finally:
|
|
1251
1251
|
try:
|
|
1252
1252
|
from src.telemetry_sync import TelemetrySync
|
|
1253
|
-
TelemetrySync.sync_background(throttle_seconds=
|
|
1253
|
+
TelemetrySync.sync_background(throttle_seconds=20.0)
|
|
1254
1254
|
except Exception:
|
|
1255
1255
|
pass
|
|
1256
1256
|
else:
|
package/src/proxy.py
CHANGED
|
@@ -219,6 +219,8 @@ class TokenSafeProxyHandler(BaseHTTPRequestHandler):
|
|
|
219
219
|
},
|
|
220
220
|
measurement="measured", # bytes literally swapped
|
|
221
221
|
)
|
|
222
|
+
from src.telemetry_sync import TelemetrySync
|
|
223
|
+
TelemetrySync.sync_background(throttle_seconds=20.0)
|
|
222
224
|
except Exception:
|
|
223
225
|
pass
|
|
224
226
|
target_symbol = compaction.get("symbol")
|
package/src/skeletonizer.py
CHANGED
|
@@ -21,14 +21,14 @@ CONTROL_KEYWORDS = {
|
|
|
21
21
|
'return', 'throw', 'yield', 'await', 'import', 'export', 'record'
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
# 1. Standard function declarations (JS/TS/PHP/
|
|
24
|
+
# 1. Standard function declarations (JS/TS/PHP/Kotlin)
|
|
25
25
|
FN_DECL_REGEX = re.compile(
|
|
26
|
-
r'^(?:export\s+)?(?:default\s+)?(?:async\s+)?(?:function\s*\*?|fun)\s+([a-zA-Z0-9_$]+)?\s*(?:<[^>]+>)?\s*\('
|
|
26
|
+
r'^(?:export\s+)?(?:default\s+)?(?:(?:public|private|protected|internal|open|override|suspend|final|abstract|static|inline|tailrec|operator|infix|external)\s+)*(?:async\s+)?(?:function\s*\*?|fun)\s+([a-zA-Z0-9_$]+)?\s*(?:<[^>]+>)?\s*\('
|
|
27
27
|
)
|
|
28
28
|
|
|
29
|
-
# 2. Go func: func Foo(...) or func (r *Recv) Foo(...)
|
|
29
|
+
# 2. Go / Swift func: func Foo(...) or func (r *Recv) Foo(...) or public mutating func Foo(...)
|
|
30
30
|
GO_FN_REGEX = re.compile(
|
|
31
|
-
r'^func\s+(?:\([^)]+\)\s+)?([a-zA-Z0-9_]+)\s*(?:\[[^\]]+\])?\s*\('
|
|
31
|
+
r'^(?:(?:@[a-zA-Z0-9_]+(?:\([^)]*\))?\s+)*(?:public|private|fileprivate|internal|open|override|static|class|mutating|nonmutating|final)\s+)*func\s+(?:\([^)]+\)\s+)?([a-zA-Z0-9_]+)\s*(?:\[[^\]]+\]|<[^>]+>)?\s*\('
|
|
32
32
|
)
|
|
33
33
|
|
|
34
34
|
# 3. Rust fn: fn foo(...) or pub async fn foo(...)
|
|
@@ -66,12 +66,19 @@ ARROW_MULTILINE_START = re.compile(
|
|
|
66
66
|
|
|
67
67
|
# 5. Method definition (Java, C#, C++, TS, PHP)
|
|
68
68
|
METHOD_REGEX = re.compile(
|
|
69
|
-
r'^(?:(?:public|private|protected|static|final|native|synchronized|abstract|async|override|virtual|inline|explicit|const|get|set)\s+)*'
|
|
69
|
+
r'^(?:(?:public|private|protected|internal|open|static|final|native|synchronized|abstract|async|override|virtual|inline|explicit|const|get|set|suspend|mutating)\s+)*'
|
|
70
70
|
r'(?:[a-zA-Z0-9_<>\[\],.?*&:]+\s+)*'
|
|
71
71
|
r'([a-zA-Z0-9_$:~]+)\s*(?:<[^>]+>)?\s*\([^)]*\)\s*'
|
|
72
72
|
r'(?:const\s+|override\s+|final\s+|noexcept\s+|throws\s+[^{]+|->\s*[^{]+|:\s*[^{]+)*\{'
|
|
73
73
|
)
|
|
74
74
|
|
|
75
|
+
# 5b. Multi-line / Allman-style method header (no '{' on first line; requires at least one modifier/type or trailing '(')
|
|
76
|
+
METHOD_MULTILINE_REGEX = re.compile(
|
|
77
|
+
r'^(?:(?:public|private|protected|internal|open|static|final|native|synchronized|abstract|async|override|virtual|inline|explicit|const|get|set|suspend|mutating)\s+)*'
|
|
78
|
+
r'(?:[a-zA-Z0-9_<>\[\],.?*&:]+\s+)+'
|
|
79
|
+
r'([a-zA-Z0-9_$:~]+)\s*(?:<[^>]+>)?\s*\([^;]*$'
|
|
80
|
+
)
|
|
81
|
+
|
|
75
82
|
class BraceLexer:
|
|
76
83
|
"""Accurate tokenizer tracking multi-line comments and string literals for brace counting."""
|
|
77
84
|
def __init__(self):
|
|
@@ -246,9 +253,17 @@ class Skeletonizer:
|
|
|
246
253
|
return cls._collapse_import_lines(res, ext) if (collapse_imports and keep_symbols) else res
|
|
247
254
|
elif ext in (
|
|
248
255
|
".ts", ".js", ".tsx", ".jsx", ".mjs", ".cjs",
|
|
249
|
-
".go", ".java", ".rs", ".cpp", ".cc", ".cxx", ".c", ".h", ".hpp",
|
|
256
|
+
".go", ".java", ".rs", ".cpp", ".cc", ".cxx", ".c", ".h", ".hpp",
|
|
257
|
+
".cs", ".php", ".kt", ".kts", ".swift",
|
|
250
258
|
):
|
|
251
|
-
res =
|
|
259
|
+
res = None
|
|
260
|
+
try:
|
|
261
|
+
from src.treesitter_backend import TreeSitterBackend
|
|
262
|
+
res = TreeSitterBackend.skeletonize_braces(code, ext, keep_symbols=keep_symbols)
|
|
263
|
+
except Exception:
|
|
264
|
+
res = None
|
|
265
|
+
if res is None:
|
|
266
|
+
res = cls._skeletonize_braces(code, ext, keep_symbols=keep_symbols)
|
|
252
267
|
return cls._collapse_import_lines(res, ext) if (collapse_imports and keep_symbols) else res
|
|
253
268
|
elif ext in (".css", ".scss", ".sass", ".less"):
|
|
254
269
|
return cls._skeletonize_css(code, ext, keep_symbols=keep_symbols)
|
|
@@ -265,12 +280,20 @@ class Skeletonizer:
|
|
|
265
280
|
first_line = doc_val.strip().split("\n")[0].strip()
|
|
266
281
|
tree.body[0].value.value = f"{first_line} ..."
|
|
267
282
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
283
|
+
def _fold_py_node(node: ast.AST, class_name: Optional[str] = None):
|
|
284
|
+
for child in ast.iter_child_nodes(node):
|
|
285
|
+
if isinstance(child, ast.ClassDef):
|
|
286
|
+
_fold_py_node(child, class_name=child.name)
|
|
287
|
+
elif isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
288
|
+
qual_name = f"{class_name}.{child.name}" if class_name else child.name
|
|
289
|
+
if keep_symbols is not None and (child.name in keep_symbols or qual_name in keep_symbols):
|
|
290
|
+
_fold_py_node(child, class_name=class_name)
|
|
291
|
+
continue
|
|
292
|
+
child.body = [ast.Expr(value=ast.Constant(value=Ellipsis))]
|
|
293
|
+
else:
|
|
294
|
+
_fold_py_node(child, class_name=class_name)
|
|
295
|
+
|
|
296
|
+
_fold_py_node(tree)
|
|
274
297
|
return ast.unparse(tree)
|
|
275
298
|
except Exception:
|
|
276
299
|
return code
|
|
@@ -325,6 +348,14 @@ class Skeletonizer:
|
|
|
325
348
|
if clean_name not in CONTROL_KEYWORDS:
|
|
326
349
|
return (clean_name, "method")
|
|
327
350
|
|
|
351
|
+
if not line_clean.endswith(";"):
|
|
352
|
+
m = METHOD_MULTILINE_REGEX.match(line_clean)
|
|
353
|
+
if m:
|
|
354
|
+
name = m.group(1)
|
|
355
|
+
clean_name = name.split("::")[-1]
|
|
356
|
+
if clean_name not in CONTROL_KEYWORDS and clean_name not in ("new", "delete", "sizeof"):
|
|
357
|
+
return (clean_name, "method_multiline")
|
|
358
|
+
|
|
328
359
|
return None
|
|
329
360
|
|
|
330
361
|
@classmethod
|
|
@@ -489,6 +520,15 @@ class Skeletonizer:
|
|
|
489
520
|
pending_name = None
|
|
490
521
|
pending_kind = None
|
|
491
522
|
continue
|
|
523
|
+
elif len(pending_header_lines) > 25:
|
|
524
|
+
out.extend(pending_header_lines)
|
|
525
|
+
for phl in pending_header_lines:
|
|
526
|
+
op, cl = lexer.count_braces(phl)
|
|
527
|
+
current_depth += op - cl
|
|
528
|
+
pending_header_lines = []
|
|
529
|
+
pending_name = None
|
|
530
|
+
pending_kind = None
|
|
531
|
+
continue
|
|
492
532
|
else:
|
|
493
533
|
continue
|
|
494
534
|
|
|
@@ -535,11 +575,20 @@ class Skeletonizer:
|
|
|
535
575
|
in_folding = True
|
|
536
576
|
continue
|
|
537
577
|
|
|
578
|
+
if kind in ('fn_decl', 'go_func', 'rust_fn', 'method_multiline') and not raw_line.rstrip().endswith(";"):
|
|
579
|
+
pending_header_lines = [raw_line]
|
|
580
|
+
pending_name = name
|
|
581
|
+
pending_kind = 'fn_multiline'
|
|
582
|
+
continue
|
|
583
|
+
|
|
538
584
|
# 4. Normal line
|
|
539
585
|
out.append(raw_line)
|
|
540
586
|
open_b, close_b = lexer.count_braces(count_src)
|
|
541
587
|
current_depth += open_b - close_b
|
|
542
588
|
|
|
589
|
+
if pending_header_lines:
|
|
590
|
+
out.extend(pending_header_lines)
|
|
591
|
+
|
|
543
592
|
return "\n".join(out)
|
|
544
593
|
|
|
545
594
|
@classmethod
|
|
@@ -551,6 +600,9 @@ class Skeletonizer:
|
|
|
551
600
|
Container at-rules (@media, @supports, @container, @layer) preserve structure and track depth.
|
|
552
601
|
Component selectors and mixins are folded into '{ /* ... */ }' unless matching keep_symbols.
|
|
553
602
|
"""
|
|
603
|
+
if ext == ".sass" and "{" not in code:
|
|
604
|
+
return code
|
|
605
|
+
|
|
554
606
|
lines = code.splitlines()
|
|
555
607
|
out = []
|
|
556
608
|
lexer = BraceLexer()
|
|
@@ -671,6 +723,9 @@ class Skeletonizer:
|
|
|
671
723
|
open_b, close_b = lexer.count_braces(raw_line)
|
|
672
724
|
current_depth += open_b - close_b
|
|
673
725
|
|
|
726
|
+
if pending_sel:
|
|
727
|
+
out.extend(pending_sel)
|
|
728
|
+
|
|
674
729
|
return "\n".join(out)
|
|
675
730
|
|
|
676
731
|
@classmethod
|
package/src/slicer.py
CHANGED
|
@@ -117,15 +117,21 @@ class ToolSlicer:
|
|
|
117
117
|
# Single pass over the file for ALL candidates (previously O(words x lines)
|
|
118
118
|
# regex scans per read_file hook call on large files).
|
|
119
119
|
unique_candidates = list(dict.fromkeys(candidates))
|
|
120
|
+
cand_alt = "|".join(re.escape(c) for c in unique_candidates)
|
|
120
121
|
combined = re.compile(
|
|
121
|
-
r'
|
|
122
|
-
+
|
|
122
|
+
r'(?:\b(?:def|function|fun|class|const|let|var|type|interface|func|fn|impl|struct|enum|trait|record|namespace)\s+('
|
|
123
|
+
+ cand_alt
|
|
124
|
+
+ r')\b|^\s*(?:(?:public|private|protected|internal|static|final|async|override|virtual|suspend|mutating)\s+)+[\w<>\[\],.?*&:]+\s+('
|
|
125
|
+
+ cand_alt
|
|
126
|
+
+ r')\s*(?:<[^>]+>)?\s*\()'
|
|
123
127
|
)
|
|
124
128
|
first_hit: Dict[str, int] = {}
|
|
125
129
|
for idx, line in enumerate(lines, 1):
|
|
126
130
|
m = combined.search(line)
|
|
127
|
-
if m
|
|
128
|
-
|
|
131
|
+
if m:
|
|
132
|
+
hit = m.group(1) or m.group(2)
|
|
133
|
+
if hit and hit not in first_hit:
|
|
134
|
+
first_hit[hit] = idx
|
|
129
135
|
# Preserve original semantics: highest-priority candidate that has a match
|
|
130
136
|
for candidate in candidates:
|
|
131
137
|
if candidate in first_hit:
|