tokensafe 4.1.7 → 4.1.8
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/.codex-plugin/plugin.json +36 -0
- package/assets/logo.png +0 -0
- package/assets/logo.svg +13 -0
- package/assets/social/linkedin_banner.png +0 -0
- package/assets/social/tokensafe_logo_square.png +0 -0
- package/hooks/kimi_hook.py +237 -0
- package/kimi.plugin.json +62 -0
- package/package.json +5 -1
- package/plugin.json +6 -5
- package/server.json +4 -4
- package/src/installer.py +134 -2
- package/src/mcp_server.py +2 -2
- package/src/telemetry_sync.py +1 -1
- package/src/version_checker.py +1 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "4.1.8",
|
|
3
|
+
"name": "tokensafe",
|
|
4
|
+
"description": "Your coding agent isn't getting dumber. Your context is just full of trash. Local context firewall & zero-loss runtime for Cursor, Claude Code, and autonomous agents.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "TokenSafe",
|
|
7
|
+
"url": "https://tokensafe.dev"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://tokensafe.dev",
|
|
10
|
+
"repository": "https://github.com/tokensafe-dev/tokensafe",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"tokensafe",
|
|
14
|
+
"token-optimizer",
|
|
15
|
+
"mcp",
|
|
16
|
+
"compression",
|
|
17
|
+
"ast-slicer",
|
|
18
|
+
"context-window"
|
|
19
|
+
],
|
|
20
|
+
"skills": "./skills/",
|
|
21
|
+
"interface": {
|
|
22
|
+
"displayName": "TokenSafe",
|
|
23
|
+
"shortDescription": "Stop coding agent context rot, destructive rewrites & secret leaks",
|
|
24
|
+
"longDescription": "TokenSafe is a local context firewall that strips massive lockfiles, verbose build logs, and repetitive file reads before they reach model attention. Zero-loss AST slicing, prompt cache prefix locking, and pre-commit secret guard for AI coding agents.",
|
|
25
|
+
"developerName": "TokenSafe",
|
|
26
|
+
"category": "Engineering",
|
|
27
|
+
"capabilities": [
|
|
28
|
+
"Read",
|
|
29
|
+
"Write",
|
|
30
|
+
"Analyze"
|
|
31
|
+
],
|
|
32
|
+
"websiteURL": "https://tokensafe.dev",
|
|
33
|
+
"brandColor": "#10B981",
|
|
34
|
+
"logo": "https://tokensafe.dev/logo.png"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/assets/logo.png
ADDED
|
Binary file
|
package/assets/logo.svg
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<svg width="256" height="256" viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<!-- Clean Obsidian Slate Squircle -->
|
|
3
|
+
<rect x="8" y="8" width="240" height="240" rx="52" fill="#090C10"/>
|
|
4
|
+
<rect x="8" y="8" width="240" height="240" rx="52" stroke="#1E293B" stroke-width="4"/>
|
|
5
|
+
|
|
6
|
+
<!-- Geometric Protective Vault Shield -->
|
|
7
|
+
<path d="M128 42 L194 68 V128 C194 170 166 204 128 218 C90 204 62 170 62 128 V68 L128 42 Z"
|
|
8
|
+
fill="#11161E" stroke="#10B981" stroke-width="10" stroke-linejoin="round"/>
|
|
9
|
+
|
|
10
|
+
<!-- Crisp Compression Bolt (High Contrast, 16px-ready) -->
|
|
11
|
+
<path d="M137 74 L97 132 H125 L119 182 L159 124 H131 L137 74 Z"
|
|
12
|
+
fill="#F3F4F6"/>
|
|
13
|
+
</svg>
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
TokenSafe adapter for Kimi Code lifecycle hooks.
|
|
4
|
+
|
|
5
|
+
Registered via kimi.plugin.json or ~/.kimi-code/config.toml:
|
|
6
|
+
SessionStart -> injects TokenSafe protocol directives once per session.
|
|
7
|
+
UserPromptSubmit -> compacts prompt into TOCP intent expression.
|
|
8
|
+
PreToolUse (Bash/Write/Read) -> denies secret leaks, destructive commands,
|
|
9
|
+
and unindexed lockfile/bundle reads.
|
|
10
|
+
|
|
11
|
+
Contract: stdin = one JSON payload, stdout = one JSON response.
|
|
12
|
+
Fail-open: any internal error emits `{}`.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import sys
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
HOOK_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
20
|
+
PROJECT_ROOT = os.path.dirname(HOOK_DIR)
|
|
21
|
+
if PROJECT_ROOT not in sys.path:
|
|
22
|
+
sys.path.insert(0, PROJECT_ROOT)
|
|
23
|
+
|
|
24
|
+
from src.adapter_core import (
|
|
25
|
+
TOKENSAFE_DIRECTIVES,
|
|
26
|
+
compact_for_injection,
|
|
27
|
+
guard_command,
|
|
28
|
+
guard_read,
|
|
29
|
+
guard_write,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
AGENT = "kimi_code"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def handle(event: str, payload: dict) -> dict:
|
|
36
|
+
session_id = str(payload.get("session_id") or "") or f"hook_{AGENT}"
|
|
37
|
+
|
|
38
|
+
if event == "SessionStart":
|
|
39
|
+
extra = TOKENSAFE_DIRECTIVES
|
|
40
|
+
try:
|
|
41
|
+
from src.version_checker import VersionChecker
|
|
42
|
+
VersionChecker.notify_os_update_background()
|
|
43
|
+
except Exception:
|
|
44
|
+
pass
|
|
45
|
+
try:
|
|
46
|
+
from src.metrics import MetricsTracker
|
|
47
|
+
from src.license_manager import LicenseManager
|
|
48
|
+
quota = LicenseManager.check_quota(MetricsTracker())
|
|
49
|
+
if quota.get("warning"):
|
|
50
|
+
extra = f"{extra}\n\n{quota['warning']}"
|
|
51
|
+
except Exception:
|
|
52
|
+
pass
|
|
53
|
+
try:
|
|
54
|
+
from src.mistakes import MistakesLedger
|
|
55
|
+
guard = MistakesLedger.get_guard_prompt(project_root=str(payload.get("cwd") or os.getcwd()))
|
|
56
|
+
if guard:
|
|
57
|
+
extra = f"{extra}\n\n{guard}"
|
|
58
|
+
except Exception:
|
|
59
|
+
pass
|
|
60
|
+
return {
|
|
61
|
+
"hookSpecificOutput": {
|
|
62
|
+
"hookEventName": "SessionStart",
|
|
63
|
+
"additionalContext": extra,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if event == "UserPromptSubmit":
|
|
68
|
+
prompt = str(payload.get("prompt") or "")
|
|
69
|
+
parts = []
|
|
70
|
+
quota = {"is_quota_exceeded": False, "warning": None}
|
|
71
|
+
tracker = None
|
|
72
|
+
try:
|
|
73
|
+
from src.metrics import MetricsTracker
|
|
74
|
+
from src.license_manager import LicenseManager
|
|
75
|
+
tracker = MetricsTracker()
|
|
76
|
+
quota = LicenseManager.check_quota(tracker)
|
|
77
|
+
except Exception:
|
|
78
|
+
pass
|
|
79
|
+
|
|
80
|
+
if quota.get("is_quota_exceeded"):
|
|
81
|
+
try:
|
|
82
|
+
from src.license_manager import LicenseManager
|
|
83
|
+
if LicenseManager.should_emit_quota_banner(tracker, session_id, every_n_turns=5) and quota.get("warning"):
|
|
84
|
+
parts.append(quota["warning"])
|
|
85
|
+
except Exception:
|
|
86
|
+
pass
|
|
87
|
+
if parts:
|
|
88
|
+
return {
|
|
89
|
+
"hookSpecificOutput": {
|
|
90
|
+
"hookEventName": "UserPromptSubmit",
|
|
91
|
+
"additionalContext": "\n\n".join(parts),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {}
|
|
95
|
+
|
|
96
|
+
context, meta = compact_for_injection(prompt, AGENT, session_id)
|
|
97
|
+
if context and meta.get("compacted"):
|
|
98
|
+
parts.append(
|
|
99
|
+
f"{context}\n"
|
|
100
|
+
f"(TokenSafe: compacted ~{meta['saved_tokens']} prompt tokens, "
|
|
101
|
+
f"-{meta.get('reduction_pct', 0)}% — original intent preserved.)"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
if parts:
|
|
105
|
+
return {
|
|
106
|
+
"hookSpecificOutput": {
|
|
107
|
+
"hookEventName": "UserPromptSubmit",
|
|
108
|
+
"additionalContext": "\n\n".join(parts),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {}
|
|
112
|
+
|
|
113
|
+
if event == "PreToolUse":
|
|
114
|
+
tool = str(payload.get("tool_name") or "")
|
|
115
|
+
tool_input = payload.get("tool_input")
|
|
116
|
+
if not isinstance(tool_input, dict):
|
|
117
|
+
tool_input = {}
|
|
118
|
+
|
|
119
|
+
if tool in ("Bash", "Shell", "bash"):
|
|
120
|
+
command = str(tool_input.get("command") or "")
|
|
121
|
+
verdict = guard_command(command, AGENT, session_id, cwd=str(payload.get("cwd") or ""))
|
|
122
|
+
if verdict["decision"] == "deny":
|
|
123
|
+
return {
|
|
124
|
+
"hookSpecificOutput": {
|
|
125
|
+
"hookEventName": "PreToolUse",
|
|
126
|
+
"permissionDecision": "deny",
|
|
127
|
+
"permissionDecisionReason": verdict["reason"],
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if verdict["decision"] == "ask":
|
|
131
|
+
return {
|
|
132
|
+
"hookSpecificOutput": {
|
|
133
|
+
"hookEventName": "PreToolUse",
|
|
134
|
+
"permissionDecision": "ask",
|
|
135
|
+
"permissionDecisionReason": verdict["reason"],
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
elif tool in ("Write", "write_file", "replace_file_content"):
|
|
140
|
+
file_path = str(tool_input.get("file_path") or tool_input.get("Path") or tool_input.get("path") or "")
|
|
141
|
+
content = tool_input.get("content") or tool_input.get("Content") or tool_input.get("file_text") or ""
|
|
142
|
+
append = bool(tool_input.get("append") or tool_input.get("Append") or False)
|
|
143
|
+
verdict = guard_write(file_path, str(content or ""), append, AGENT, session_id)
|
|
144
|
+
if verdict["decision"] == "deny":
|
|
145
|
+
return {
|
|
146
|
+
"hookSpecificOutput": {
|
|
147
|
+
"hookEventName": "PreToolUse",
|
|
148
|
+
"permissionDecision": "deny",
|
|
149
|
+
"permissionDecisionReason": verdict["reason"],
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if verdict["decision"] == "ask":
|
|
153
|
+
return {
|
|
154
|
+
"hookSpecificOutput": {
|
|
155
|
+
"hookEventName": "PreToolUse",
|
|
156
|
+
"permissionDecision": "ask",
|
|
157
|
+
"permissionDecisionReason": verdict["reason"],
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
elif tool in ("Read", "read_file", "view_file"):
|
|
162
|
+
file_path = str(tool_input.get("file_path") or tool_input.get("path") or tool_input.get("Path") or "")
|
|
163
|
+
verdict = guard_read(file_path, AGENT, session_id)
|
|
164
|
+
if verdict["decision"] == "deny":
|
|
165
|
+
return {
|
|
166
|
+
"hookSpecificOutput": {
|
|
167
|
+
"hookEventName": "PreToolUse",
|
|
168
|
+
"permissionDecision": "deny",
|
|
169
|
+
"permissionDecisionReason": verdict["reason"],
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {}
|
|
174
|
+
|
|
175
|
+
if event == "PostToolUse":
|
|
176
|
+
tool = str(payload.get("tool_name") or "")
|
|
177
|
+
compacted, orig, new = None, 0, 0
|
|
178
|
+
try:
|
|
179
|
+
from src.command_sanitizer import CommandSanitizer
|
|
180
|
+
res = CommandSanitizer.compact_tool_output(tool, payload.get("tool_response"))
|
|
181
|
+
if res:
|
|
182
|
+
compacted, orig, new = res
|
|
183
|
+
except Exception:
|
|
184
|
+
compacted = None
|
|
185
|
+
if compacted:
|
|
186
|
+
try:
|
|
187
|
+
from src.metrics import MetricsTracker
|
|
188
|
+
from src.tokenizer import count_tokens
|
|
189
|
+
MetricsTracker().record_event(
|
|
190
|
+
session_id=session_id,
|
|
191
|
+
event_type="TOOL_OUTPUT_COMPACT",
|
|
192
|
+
original_tokens=count_tokens(str(payload.get("tool_response") or "")),
|
|
193
|
+
optimized_tokens=count_tokens(compacted),
|
|
194
|
+
details={"agent": AGENT, "tool": tool, "chars": f"{orig}->{new}"},
|
|
195
|
+
)
|
|
196
|
+
except Exception:
|
|
197
|
+
pass
|
|
198
|
+
return {
|
|
199
|
+
"decision": "block",
|
|
200
|
+
"reason": (
|
|
201
|
+
f"[TokenSafe] {tool} output auto-compacted "
|
|
202
|
+
f"({orig:,} → {new:,} chars). Output summary:\n{compacted}"
|
|
203
|
+
),
|
|
204
|
+
}
|
|
205
|
+
return {}
|
|
206
|
+
|
|
207
|
+
return {}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def main():
|
|
211
|
+
try:
|
|
212
|
+
raw = sys.stdin.read()
|
|
213
|
+
payload = json.loads(raw) if raw.strip() else {}
|
|
214
|
+
if not isinstance(payload, dict):
|
|
215
|
+
payload = {}
|
|
216
|
+
except Exception:
|
|
217
|
+
print(json.dumps({}))
|
|
218
|
+
return
|
|
219
|
+
event = ""
|
|
220
|
+
try:
|
|
221
|
+
event = str(payload.get("hook_event_name") or "")
|
|
222
|
+
response = handle(event, payload)
|
|
223
|
+
if not isinstance(response, dict):
|
|
224
|
+
response = {}
|
|
225
|
+
except Exception:
|
|
226
|
+
response = {}
|
|
227
|
+
if event in ("UserPromptSubmit", "PreToolUse", "PostToolUse", "Stop"):
|
|
228
|
+
try:
|
|
229
|
+
from src.telemetry_sync import TelemetrySync
|
|
230
|
+
TelemetrySync.sync_background(throttle_seconds=0.0 if event == "Stop" else 20.0)
|
|
231
|
+
except Exception:
|
|
232
|
+
pass
|
|
233
|
+
print(json.dumps(response))
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
if __name__ == "__main__":
|
|
237
|
+
main()
|
package/kimi.plugin.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tokensafe",
|
|
3
|
+
"version": "4.1.8",
|
|
4
|
+
"description": "Your coding agent isn't getting dumber. Your context is just full of trash. Local context firewall & zero-loss runtime for Cursor, Claude Code, and autonomous agents.",
|
|
5
|
+
"homepage": "https://tokensafe.dev",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"logo": "https://tokensafe.dev/logo.png",
|
|
8
|
+
"icon": "https://tokensafe.dev/logo.png",
|
|
9
|
+
"iconUrl": "https://tokensafe.dev/logo.png",
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "TokenSafe"
|
|
12
|
+
},
|
|
13
|
+
"interface": {
|
|
14
|
+
"displayName": "TokenSafe",
|
|
15
|
+
"shortDescription": "Stop coding agent context rot, destructive rewrites & secret leaks",
|
|
16
|
+
"developerName": "TokenSafe",
|
|
17
|
+
"websiteURL": "https://tokensafe.dev",
|
|
18
|
+
"logo": "https://tokensafe.dev/logo.png",
|
|
19
|
+
"iconUrl": "https://tokensafe.dev/logo.png"
|
|
20
|
+
},
|
|
21
|
+
"skills": [
|
|
22
|
+
"skills/token-optimizer"
|
|
23
|
+
],
|
|
24
|
+
"mcpServers": {
|
|
25
|
+
"tokensafe": {
|
|
26
|
+
"command": "python3",
|
|
27
|
+
"args": [
|
|
28
|
+
"src/mcp_server.py"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"hooks": [
|
|
33
|
+
{
|
|
34
|
+
"event": "SessionStart",
|
|
35
|
+
"command": "python3 hooks/kimi_hook.py",
|
|
36
|
+
"timeout": 10
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"event": "UserPromptSubmit",
|
|
40
|
+
"command": "python3 hooks/kimi_hook.py",
|
|
41
|
+
"timeout": 10
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"event": "PreToolUse",
|
|
45
|
+
"matcher": "Bash",
|
|
46
|
+
"command": "python3 hooks/kimi_hook.py",
|
|
47
|
+
"timeout": 10
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"event": "PreToolUse",
|
|
51
|
+
"matcher": "Write",
|
|
52
|
+
"command": "python3 hooks/kimi_hook.py",
|
|
53
|
+
"timeout": 10
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"event": "PreToolUse",
|
|
57
|
+
"matcher": "Read",
|
|
58
|
+
"command": "python3 hooks/kimi_hook.py",
|
|
59
|
+
"timeout": 10
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"mcpName": "dev.tokensafe/tokensafe",
|
|
4
|
-
"version": "4.1.
|
|
4
|
+
"version": "4.1.8",
|
|
5
5
|
"description": "Your coding agent isn't getting dumber. Your context is just full of trash. Local context firewall & zero-loss runtime for Cursor, Claude Code, and autonomous agents.",
|
|
6
6
|
"main": "bin/tokensafe.js",
|
|
7
7
|
"bin": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"!src/mailer.py",
|
|
17
17
|
"!src/weekly_digest.py",
|
|
18
18
|
"!**/__pycache__/**",
|
|
19
|
+
"!hooks/__pycache__/**",
|
|
19
20
|
"!**/*.pyc",
|
|
20
21
|
"rules/",
|
|
21
22
|
"skills/",
|
|
@@ -24,7 +25,10 @@
|
|
|
24
25
|
"mcp_config.json",
|
|
25
26
|
"proto/",
|
|
26
27
|
"extensions/",
|
|
28
|
+
"assets/",
|
|
29
|
+
".codex-plugin/",
|
|
27
30
|
"plugin.json",
|
|
31
|
+
"kimi.plugin.json",
|
|
28
32
|
"server.json",
|
|
29
33
|
"README.md",
|
|
30
34
|
"LICENSE"
|
package/plugin.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"displayName": "TokenSafe",
|
|
4
|
-
"version": "4.1.
|
|
5
|
-
"description": "
|
|
6
|
-
"logo": "
|
|
4
|
+
"version": "4.1.8",
|
|
5
|
+
"description": "Your coding agent isn't getting dumber. Your context is just full of trash. Local context firewall & zero-loss runtime for Cursor, Claude Code, and autonomous agents.",
|
|
6
|
+
"logo": "https://tokensafe.dev/logo.png",
|
|
7
|
+
"developerName": "TokenSafe",
|
|
7
8
|
"author": {
|
|
8
|
-
"name": "
|
|
9
|
-
"url": "https://
|
|
9
|
+
"name": "TokenSafe",
|
|
10
|
+
"url": "https://tokensafe.dev"
|
|
10
11
|
},
|
|
11
12
|
"homepage": "https://tokensafe.dev",
|
|
12
13
|
"repository": {
|
package/server.json
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
|
|
3
3
|
"name": "dev.tokensafe/tokensafe",
|
|
4
4
|
"title": "TokenSafe",
|
|
5
|
-
"description": "
|
|
6
|
-
"version": "4.1.
|
|
5
|
+
"description": "Your coding agent isn't getting dumber. Your context is just full of trash. Local context firewall & zero-loss runtime for Cursor, Claude Code, and autonomous agents.",
|
|
6
|
+
"version": "4.1.8",
|
|
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": "4.1.
|
|
33
|
+
"version": "4.1.8",
|
|
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": "4.1.
|
|
49
|
+
"version": "4.1.8",
|
|
50
50
|
"runtimeHint": "uvx",
|
|
51
51
|
"transport": {
|
|
52
52
|
"type": "stdio"
|
package/src/installer.py
CHANGED
|
@@ -70,7 +70,7 @@ UNIFIED_TOKENSAFE_RULE = (
|
|
|
70
70
|
POINTER_RULE = (
|
|
71
71
|
"## TOKENSAFE GRAPH-FIRST OPTIMIZATION PROTOCOL\n"
|
|
72
72
|
"The full 12-point protocol is installed globally in the agent-level instructions "
|
|
73
|
-
"(~/.zcode/AGENTS.md, ~/.claude/CLAUDE.md, ~/.codex/AGENTS.md) and applies to this project. "
|
|
73
|
+
"(~/.zcode/AGENTS.md, ~/.claude/CLAUDE.md, ~/.codex/AGENTS.md, ~/.kimi-code/AGENTS.md) and applies to this project. "
|
|
74
74
|
"Core rules: never raw grep/find or `ls -la` — use `tokensafe:scoped_search`, "
|
|
75
75
|
"`tokensafe:smart_read_file(path, symbol)`, `tokensafe:smart_bundle` and `tokensafe:list_dir`; "
|
|
76
76
|
"sanitize high-output commands via `tokensafe:sanitize_command`; check `tokensafe:context_budget` (Grade S-F) "
|
|
@@ -146,6 +146,16 @@ AGENTS_REGISTRY = {
|
|
|
146
146
|
"plugin_path": os.path.join(HOME, ".zcode", "cli", "plugins", "tokensafe"),
|
|
147
147
|
"agents_md_path": os.path.join(HOME, ".zcode", "AGENTS.md"),
|
|
148
148
|
"detect_path": os.path.join(HOME, ".zcode")
|
|
149
|
+
},
|
|
150
|
+
"kimi": {
|
|
151
|
+
"name": "Kimi Code",
|
|
152
|
+
"type": "kimi_plugin",
|
|
153
|
+
"config_path": os.path.join(HOME, ".kimi-code", "mcp.json"),
|
|
154
|
+
"installed_json_path": os.path.join(HOME, ".kimi-code", "plugins", "installed.json"),
|
|
155
|
+
"managed_plugin_path": os.path.join(HOME, ".kimi-code", "plugins", "managed", "tokensafe"),
|
|
156
|
+
"skills_path": os.path.join(HOME, ".kimi-code", "skills", "token-optimizer"),
|
|
157
|
+
"agents_md_path": os.path.join(HOME, ".kimi-code", "AGENTS.md"),
|
|
158
|
+
"detect_path": os.path.join(HOME, ".kimi-code")
|
|
149
159
|
}
|
|
150
160
|
}
|
|
151
161
|
|
|
@@ -161,6 +171,12 @@ class AgentInstaller:
|
|
|
161
171
|
# Fallback for cursor / windsurf on macOS
|
|
162
172
|
if not is_present and key == "cursor":
|
|
163
173
|
is_present = os.path.exists(os.path.join(HOME, "Library", "Application Support", "Cursor"))
|
|
174
|
+
if not is_present and key == "kimi":
|
|
175
|
+
is_present = (
|
|
176
|
+
os.path.exists("/Applications/Kimi Code.app")
|
|
177
|
+
or os.path.exists(os.path.join(HOME, "Applications", "Kimi Code.app"))
|
|
178
|
+
or shutil.which("kimi") is not None
|
|
179
|
+
)
|
|
164
180
|
detected[key] = {
|
|
165
181
|
**info,
|
|
166
182
|
"installed": is_present
|
|
@@ -211,11 +227,62 @@ class AgentInstaller:
|
|
|
211
227
|
Codex has no lifecycle hooks (MCP + AGENTS.md remain the integration)."""
|
|
212
228
|
if agent_key == "codex":
|
|
213
229
|
return False, "Codex CLI has no lifecycle hooks — MCP + AGENTS.md stay the integration"
|
|
214
|
-
if agent_key not in ("claude_code", "cursor"):
|
|
230
|
+
if agent_key not in ("claude_code", "cursor", "kimi"):
|
|
215
231
|
return False, "hooks not applicable for this agent"
|
|
216
232
|
|
|
217
233
|
claude_cmd = cls._tokensafe_hook_command("claude_code_hook.py")
|
|
218
234
|
cursor_cmd = cls._tokensafe_hook_command("cursor_hook.py")
|
|
235
|
+
kimi_cmd = cls._tokensafe_hook_command("kimi_hook.py")
|
|
236
|
+
|
|
237
|
+
if agent_key == "kimi":
|
|
238
|
+
config_file = os.path.join(HOME, ".kimi-code", "config.toml")
|
|
239
|
+
os.makedirs(os.path.dirname(config_file), exist_ok=True)
|
|
240
|
+
content = ""
|
|
241
|
+
if os.path.exists(config_file):
|
|
242
|
+
try:
|
|
243
|
+
shutil.copy2(config_file, f"{config_file}.backup")
|
|
244
|
+
except Exception:
|
|
245
|
+
pass
|
|
246
|
+
try:
|
|
247
|
+
with open(config_file, "r", encoding="utf-8") as f:
|
|
248
|
+
content = f.read()
|
|
249
|
+
except Exception:
|
|
250
|
+
pass
|
|
251
|
+
|
|
252
|
+
if "kimi_hook.py" in content:
|
|
253
|
+
return True, "TokenSafe Kimi Code hooks already registered in config.toml"
|
|
254
|
+
|
|
255
|
+
toml_hook_cmd = json.dumps(kimi_cmd)
|
|
256
|
+
hook_sections = (
|
|
257
|
+
f'\n# TokenSafe Lifecycle Hooks\n'
|
|
258
|
+
f'[[hooks]]\n'
|
|
259
|
+
f'event = "SessionStart"\n'
|
|
260
|
+
f'command = {toml_hook_cmd}\n'
|
|
261
|
+
f'timeout = 10\n\n'
|
|
262
|
+
f'[[hooks]]\n'
|
|
263
|
+
f'event = "UserPromptSubmit"\n'
|
|
264
|
+
f'command = {toml_hook_cmd}\n'
|
|
265
|
+
f'timeout = 10\n\n'
|
|
266
|
+
f'[[hooks]]\n'
|
|
267
|
+
f'event = "PreToolUse"\n'
|
|
268
|
+
f'matcher = "Bash"\n'
|
|
269
|
+
f'command = {toml_hook_cmd}\n'
|
|
270
|
+
f'timeout = 10\n\n'
|
|
271
|
+
f'[[hooks]]\n'
|
|
272
|
+
f'event = "PreToolUse"\n'
|
|
273
|
+
f'matcher = "Write"\n'
|
|
274
|
+
f'command = {toml_hook_cmd}\n'
|
|
275
|
+
f'timeout = 10\n\n'
|
|
276
|
+
f'[[hooks]]\n'
|
|
277
|
+
f'event = "PreToolUse"\n'
|
|
278
|
+
f'matcher = "Read"\n'
|
|
279
|
+
f'command = {toml_hook_cmd}\n'
|
|
280
|
+
f'timeout = 10\n'
|
|
281
|
+
)
|
|
282
|
+
content = (content.rstrip() + "\n" + hook_sections) if content.strip() else hook_sections.lstrip()
|
|
283
|
+
with open(config_file, "w", encoding="utf-8") as f:
|
|
284
|
+
f.write(content)
|
|
285
|
+
return True, "hooks registered in ~/.kimi-code/config.toml (SessionStart + UserPromptSubmit + PreToolUse Bash/Write/Read)"
|
|
219
286
|
|
|
220
287
|
if agent_key == "claude_code":
|
|
221
288
|
config_file = os.path.join(HOME, ".claude", "settings.json")
|
|
@@ -834,6 +901,71 @@ WantedBy=default.target
|
|
|
834
901
|
|
|
835
902
|
return True, f"Configured MCP, Plugin, Skill & AGENTS.md in {config_file}"
|
|
836
903
|
|
|
904
|
+
elif info["type"] == "kimi_plugin":
|
|
905
|
+
mcp_file = info.get("config_path")
|
|
906
|
+
if mcp_file:
|
|
907
|
+
os.makedirs(os.path.dirname(mcp_file), exist_ok=True)
|
|
908
|
+
data, err = cls._load_json_with_backup(mcp_file)
|
|
909
|
+
if err or data is None:
|
|
910
|
+
data = {}
|
|
911
|
+
if "mcpServers" not in data or not isinstance(data["mcpServers"], dict):
|
|
912
|
+
data["mcpServers"] = {}
|
|
913
|
+
data["mcpServers"]["tokensafe"] = {
|
|
914
|
+
"command": PYTHON_BIN,
|
|
915
|
+
"args": [MCP_SERVER_PATH]
|
|
916
|
+
}
|
|
917
|
+
cls._atomic_write_json(mcp_file, data)
|
|
918
|
+
|
|
919
|
+
managed_plugin_path = info.get("managed_plugin_path")
|
|
920
|
+
if managed_plugin_path:
|
|
921
|
+
cls._symlink_dir(PROJECT_ROOT, managed_plugin_path)
|
|
922
|
+
|
|
923
|
+
installed_json = info.get("installed_json_path")
|
|
924
|
+
if installed_json:
|
|
925
|
+
os.makedirs(os.path.dirname(installed_json), exist_ok=True)
|
|
926
|
+
inst_data, err = cls._load_json_with_backup(installed_json)
|
|
927
|
+
if err or inst_data is None:
|
|
928
|
+
inst_data = {"version": 1, "plugins": []}
|
|
929
|
+
if "plugins" not in inst_data or not isinstance(inst_data["plugins"], list):
|
|
930
|
+
inst_data["plugins"] = []
|
|
931
|
+
|
|
932
|
+
import datetime
|
|
933
|
+
iso_now = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
934
|
+
plugin_root = managed_plugin_path or PROJECT_ROOT
|
|
935
|
+
|
|
936
|
+
entry_found = False
|
|
937
|
+
for p in inst_data["plugins"]:
|
|
938
|
+
if isinstance(p, dict) and p.get("id") == "tokensafe":
|
|
939
|
+
p["root"] = plugin_root
|
|
940
|
+
p["enabled"] = True
|
|
941
|
+
p["updatedAt"] = iso_now
|
|
942
|
+
p["originalSource"] = PROJECT_ROOT
|
|
943
|
+
p["iconUrl"] = "https://tokensafe.dev/logo.png"
|
|
944
|
+
p["logo"] = "https://tokensafe.dev/logo.png"
|
|
945
|
+
entry_found = True
|
|
946
|
+
break
|
|
947
|
+
if not entry_found:
|
|
948
|
+
inst_data["plugins"].append({
|
|
949
|
+
"id": "tokensafe",
|
|
950
|
+
"root": plugin_root,
|
|
951
|
+
"enabled": True,
|
|
952
|
+
"installedAt": iso_now,
|
|
953
|
+
"updatedAt": iso_now,
|
|
954
|
+
"originalSource": PROJECT_ROOT,
|
|
955
|
+
"origin": "custom",
|
|
956
|
+
"iconUrl": "https://tokensafe.dev/logo.png",
|
|
957
|
+
"logo": "https://tokensafe.dev/logo.png"
|
|
958
|
+
})
|
|
959
|
+
cls._atomic_write_json(installed_json, inst_data)
|
|
960
|
+
|
|
961
|
+
if "skills_path" in info:
|
|
962
|
+
cls._symlink_skill(info["skills_path"])
|
|
963
|
+
|
|
964
|
+
if "agents_md_path" in info:
|
|
965
|
+
cls._inject_global_rule(info["agents_md_path"], UNIFIED_TOKENSAFE_RULE)
|
|
966
|
+
|
|
967
|
+
return True, f"Configured MCP, Plugin, Skill & AGENTS.md for Kimi Code"
|
|
968
|
+
|
|
837
969
|
return False, "Unsupported type"
|
|
838
970
|
|
|
839
971
|
@staticmethod
|
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.1.
|
|
45
|
+
"version": "4.1.8"
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
# MCP client attribution: the initialize handshake carries clientInfo, which
|
|
@@ -54,7 +54,7 @@ _EPOCH_GEN = 0
|
|
|
54
54
|
_IN_PROCESS_DELIVERED = {}
|
|
55
55
|
_KNOWN_POINTER_CLIENTS = (
|
|
56
56
|
"antigravity", "claude", "cursor", "windsurf", "cline", "roo",
|
|
57
|
-
"codex", "zcode", "copilot", "gemini", "zed", "vscode",
|
|
57
|
+
"codex", "zcode", "kimi", "copilot", "gemini", "zed", "vscode",
|
|
58
58
|
)
|
|
59
59
|
|
|
60
60
|
|
package/src/telemetry_sync.py
CHANGED
|
@@ -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.1.
|
|
244
|
+
"User-Agent": "TokenSafe-Sync/4.1.8"
|
|
245
245
|
}
|
|
246
246
|
if active_key:
|
|
247
247
|
headers["Authorization"] = f"Bearer {active_key}"
|
package/src/version_checker.py
CHANGED
|
@@ -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.1.
|
|
20
|
+
CURRENT_VERSION = "4.1.8"
|
|
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
|