tokensafe 4.16.0 → 4.16.1
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/.claude-plugin/hooks.json +124 -0
- package/.claude-plugin/marketplace.json +15 -0
- package/.claude-plugin/plugin.json +21 -0
- package/.codex-plugin/plugin.json +1 -1
- package/.mcp.json +10 -0
- package/README.md +14 -0
- package/bin/tokensafe-claude-hook +18 -0
- package/bin/tokensafe-mcp +17 -0
- package/hooks/claude_code_hook.py +59 -0
- package/hooks/kimi_hook.py +27 -0
- package/hooks/pre_invocation.py +19 -0
- package/hooks/session_start.py +21 -0
- package/kimi.plugin.json +1 -1
- package/package.json +5 -1
- package/plugin.json +1 -1
- package/server.json +3 -3
- package/src/agent_firewall.py +2 -1
- package/src/angular_cli.py +14 -6
- package/src/batch_editor.py +62 -22
- package/src/cli.py +96 -0
- package/src/command_sanitizer.py +19 -7
- package/src/compaction_recovery.py +335 -0
- package/src/format_invariants.py +158 -0
- package/src/graph_engine.py +48 -0
- package/src/host_capabilities.py +180 -0
- package/src/installer.py +39 -5
- package/src/mcp_server.py +146 -7
- package/src/session_brief.py +1 -1
- package/src/statusline.py +44 -0
- package/src/telemetry_sync.py +1 -1
- package/src/transcript_recall.py +565 -0
- package/src/usage_ledger.py +13 -0
- package/src/version_checker.py +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "TokenSafe lifecycle hooks (plugin-relative launcher)",
|
|
3
|
+
"hooks": {
|
|
4
|
+
"SessionStart": [
|
|
5
|
+
{
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
10
|
+
"timeout": 10
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"UserPromptSubmit": [
|
|
16
|
+
{
|
|
17
|
+
"hooks": [
|
|
18
|
+
{
|
|
19
|
+
"type": "command",
|
|
20
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
21
|
+
"timeout": 10
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"PreToolUse": [
|
|
27
|
+
{
|
|
28
|
+
"matcher": "Bash",
|
|
29
|
+
"hooks": [
|
|
30
|
+
{
|
|
31
|
+
"type": "command",
|
|
32
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
33
|
+
"timeout": 10
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"matcher": "Write",
|
|
39
|
+
"hooks": [
|
|
40
|
+
{
|
|
41
|
+
"type": "command",
|
|
42
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
43
|
+
"timeout": 10
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"matcher": "Edit|MultiEdit",
|
|
49
|
+
"hooks": [
|
|
50
|
+
{
|
|
51
|
+
"type": "command",
|
|
52
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
53
|
+
"timeout": 10
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"matcher": "Read",
|
|
59
|
+
"hooks": [
|
|
60
|
+
{
|
|
61
|
+
"type": "command",
|
|
62
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
63
|
+
"timeout": 10
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
"PostToolUse": [
|
|
69
|
+
{
|
|
70
|
+
"matcher": "Bash",
|
|
71
|
+
"hooks": [
|
|
72
|
+
{
|
|
73
|
+
"type": "command",
|
|
74
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
75
|
+
"timeout": 10
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"matcher": "Read|Edit|MultiEdit|Write",
|
|
81
|
+
"hooks": [
|
|
82
|
+
{
|
|
83
|
+
"type": "command",
|
|
84
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
85
|
+
"timeout": 10
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
],
|
|
90
|
+
"PreCompact": [
|
|
91
|
+
{
|
|
92
|
+
"hooks": [
|
|
93
|
+
{
|
|
94
|
+
"type": "command",
|
|
95
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
96
|
+
"timeout": 10
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
"PostCompact": [
|
|
102
|
+
{
|
|
103
|
+
"hooks": [
|
|
104
|
+
{
|
|
105
|
+
"type": "command",
|
|
106
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
107
|
+
"timeout": 10
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
],
|
|
112
|
+
"Stop": [
|
|
113
|
+
{
|
|
114
|
+
"hooks": [
|
|
115
|
+
{
|
|
116
|
+
"type": "command",
|
|
117
|
+
"command": "sh \"${CLAUDE_PLUGIN_ROOT}/bin/tokensafe-claude-hook\"",
|
|
118
|
+
"timeout": 15
|
|
119
|
+
}
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tokensafe",
|
|
3
|
+
"owner": {
|
|
4
|
+
"name": "TokenSafe",
|
|
5
|
+
"url": "https://tokensafe.dev"
|
|
6
|
+
},
|
|
7
|
+
"plugins": [
|
|
8
|
+
{
|
|
9
|
+
"name": "tokensafe",
|
|
10
|
+
"source": "./",
|
|
11
|
+
"description": "Enforced context optimization for Claude Code: blocking guards, PostToolUse output compaction, symbol/AST slicing, post-compaction recovery, and measured savings.",
|
|
12
|
+
"version": "4.16.0"
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tokensafe",
|
|
3
|
+
"version": "4.16.0",
|
|
4
|
+
"description": "Local runtime firewall and zero-loss context optimizer for AI coding agents: enforced tool-output compaction, symbol/AST slicing, post-compaction recovery, and measured savings.",
|
|
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": "Apache-2.0",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"tokens",
|
|
14
|
+
"context",
|
|
15
|
+
"compaction",
|
|
16
|
+
"mcp",
|
|
17
|
+
"claude-code"
|
|
18
|
+
],
|
|
19
|
+
"hooks": "./.claude-plugin/hooks.json",
|
|
20
|
+
"mcpServers": "./.mcp.json"
|
|
21
|
+
}
|
package/.mcp.json
ADDED
package/README.md
CHANGED
|
@@ -211,6 +211,20 @@ Why TokenSafe agents ship working code on the first pass without burning quotas
|
|
|
211
211
|
|
|
212
212
|
Deeper mechanics (language matrix, AST syntax trees, cache tiers, governor) live in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
|
|
213
213
|
|
|
214
|
+
## Context survival, recall, and host coverage
|
|
215
|
+
|
|
216
|
+
Compaction is the most expensive event in a long session: the host rewrites the prompt, re-bills it uncached, and drops turns the task still depends on. TokenSafe now survives it explicitly.
|
|
217
|
+
|
|
218
|
+
| Feature | Surface | What it does |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| **Post-compaction recovery** | Claude Code `PreCompact` / `PostCompact`; fallback detector on every host with a transcript | Reinjects a small digest: goal, last state, next focus, and up to five ranked pointers to the turns the compaction dropped. Each pointer expands with `tokensafe recall --id <session>:<index>`. |
|
|
221
|
+
| **Transcript recall** | CLI `tokensafe recall '<query>' [--sessions this\|project\|all]`, MCP `session_context(action='recall_transcript')` | BM25F search over prior Claude/Codex/Kimi/ZCode/Cursor/Antigravity transcripts. No index copy; snippets are secret-redacted and output is budget-capped. |
|
|
222
|
+
| **Zero-token statusline** | Claude Code | `tokensafe statusline` renders today's measured savings in the host status bar. The line never enters the conversation. |
|
|
223
|
+
| **Claude Code plugin** | `/plugin marketplace add <owner>/tokensafe` then `/plugin install tokensafe@tokensafe` | Marketplace manifest, nine lifecycle hooks (including compact events), and an MCP entry. The plugin fails open if the Python engine is absent. |
|
|
224
|
+
| **Host capability matrix** | `tokensafe doctor` | States exactly which host supports hooks, compact events, context injection, statusline, and transcript parsing, so partial coverage is never sold as full. |
|
|
225
|
+
|
|
226
|
+
Only hosts with a context-injection surface receive hook-injected digests (Claude Code, ZCode, Kimi Code, Antigravity). Cursor, Codex, Claude Desktop, Windsurf, Cline/Roo, and Copilot get the MCP tools, rules, transcript recall, and the fallback compaction detector; their host contracts expose no equivalent injection point today.
|
|
227
|
+
|
|
214
228
|
## What happens on the first prompt
|
|
215
229
|
|
|
216
230
|
You install TokenSafe, open a project in your agent, and the first turn does the wiring:
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Claude Code plugin hook launcher.
|
|
3
|
+
# Fail-open: when the Python engine is missing, emit an empty hook response so
|
|
4
|
+
# the host session continues untouched.
|
|
5
|
+
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." 2>/dev/null && pwd) || { printf '{}\n'; exit 0; }
|
|
6
|
+
PY="${TOKENSAFE_PYTHON:-}"
|
|
7
|
+
if [ -z "$PY" ]; then
|
|
8
|
+
if [ -x "$HOME/.tokensafe/venv/bin/python3" ]; then
|
|
9
|
+
PY="$HOME/.tokensafe/venv/bin/python3"
|
|
10
|
+
else
|
|
11
|
+
PY="python3"
|
|
12
|
+
fi
|
|
13
|
+
fi
|
|
14
|
+
if ! command -v "$PY" >/dev/null 2>&1; then
|
|
15
|
+
printf '{}\n'
|
|
16
|
+
exit 0
|
|
17
|
+
fi
|
|
18
|
+
exec "$PY" "$ROOT/hooks/claude_code_hook.py"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Claude Code plugin MCP launcher (stdio).
|
|
3
|
+
# Requires the Python engine; exits with a clear message instead of a traceback.
|
|
4
|
+
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." 2>/dev/null && pwd) || exit 1
|
|
5
|
+
PY="${TOKENSAFE_PYTHON:-}"
|
|
6
|
+
if [ -z "$PY" ]; then
|
|
7
|
+
if [ -x "$HOME/.tokensafe/venv/bin/python3" ]; then
|
|
8
|
+
PY="$HOME/.tokensafe/venv/bin/python3"
|
|
9
|
+
else
|
|
10
|
+
PY="python3"
|
|
11
|
+
fi
|
|
12
|
+
fi
|
|
13
|
+
if ! command -v "$PY" >/dev/null 2>&1; then
|
|
14
|
+
echo "tokensafe: python3 not found; install TokenSafe with 'tokensafe install' or curl -fsSL https://tokensafe.dev/install.sh | bash" >&2
|
|
15
|
+
exit 1
|
|
16
|
+
fi
|
|
17
|
+
PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" exec "$PY" "$ROOT/src/mcp_server.py"
|
|
@@ -29,6 +29,37 @@ AGENT = "claude_code"
|
|
|
29
29
|
|
|
30
30
|
def handle(event: str, payload: dict, agent: str = AGENT) -> dict:
|
|
31
31
|
session_id = str(payload.get("session_id") or "") or f"hook_{agent}"
|
|
32
|
+
transcript_path = str(payload.get("transcript_path") or "")
|
|
33
|
+
|
|
34
|
+
if event == "PreCompact":
|
|
35
|
+
# Arm the one-shot digest while the pre-compaction turns still exist.
|
|
36
|
+
try:
|
|
37
|
+
from src.transcript_recall import remember_session
|
|
38
|
+
from src.compaction_recovery import mark_pending
|
|
39
|
+
remember_session(session_id, transcript_path, agent=agent, cwd=str(payload.get("cwd") or ""))
|
|
40
|
+
mark_pending(session_id, transcript_path, agent=agent, reason=str(payload.get("trigger") or "pre_compact"))
|
|
41
|
+
except Exception:
|
|
42
|
+
pass
|
|
43
|
+
return {}
|
|
44
|
+
|
|
45
|
+
if event == "PostCompact":
|
|
46
|
+
# The host already rewrote the prompt; hand the model the pointers it
|
|
47
|
+
# would otherwise reconstruct by re-reading files.
|
|
48
|
+
try:
|
|
49
|
+
from src.transcript_recall import remember_session
|
|
50
|
+
from src.compaction_recovery import consume_digest
|
|
51
|
+
remember_session(session_id, transcript_path, agent=agent, cwd=str(payload.get("cwd") or ""))
|
|
52
|
+
digest = consume_digest(session_id, transcript_path, agent)
|
|
53
|
+
if digest:
|
|
54
|
+
return {
|
|
55
|
+
"hookSpecificOutput": {
|
|
56
|
+
"hookEventName": "PostCompact",
|
|
57
|
+
"additionalContext": digest,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
except Exception:
|
|
61
|
+
pass
|
|
62
|
+
return {}
|
|
32
63
|
|
|
33
64
|
if event == "SessionStart":
|
|
34
65
|
# One-shot session injection (mirrors the Antigravity SessionStart
|
|
@@ -116,6 +147,22 @@ def handle(event: str, payload: dict, agent: str = AGENT) -> dict:
|
|
|
116
147
|
extra = f"{extra}\n\n{cum_banner}"
|
|
117
148
|
except Exception:
|
|
118
149
|
pass
|
|
150
|
+
# Compaction survival: remember where this session's transcript lives,
|
|
151
|
+
# sweep abandoned markers, and inject the one-shot recovery digest when
|
|
152
|
+
# the host starts a session from a compaction (or a fallback detector
|
|
153
|
+
# armed a marker on the previous Stop).
|
|
154
|
+
try:
|
|
155
|
+
from src.transcript_recall import remember_session
|
|
156
|
+
from src.compaction_recovery import sweep_stale, mark_pending, consume_digest
|
|
157
|
+
remember_session(session_id, transcript_path, agent=agent, cwd=str(payload.get("cwd") or ""))
|
|
158
|
+
sweep_stale()
|
|
159
|
+
if str(payload.get("source") or "").strip().lower() == "compact":
|
|
160
|
+
mark_pending(session_id, transcript_path, agent=agent, reason="session_start_compact")
|
|
161
|
+
digest = consume_digest(session_id, transcript_path, agent)
|
|
162
|
+
if digest:
|
|
163
|
+
extra = f"{extra}\n\n{digest}"
|
|
164
|
+
except Exception:
|
|
165
|
+
pass
|
|
119
166
|
return {
|
|
120
167
|
"hookSpecificOutput": {
|
|
121
168
|
"hookEventName": "SessionStart",
|
|
@@ -224,6 +271,18 @@ def handle(event: str, payload: dict, agent: str = AGENT) -> dict:
|
|
|
224
271
|
except Exception:
|
|
225
272
|
pass
|
|
226
273
|
|
|
274
|
+
# One-shot post-compaction recovery (PreCompact/PostCompact marker or a
|
|
275
|
+
# fallback detection on the previous Stop).
|
|
276
|
+
try:
|
|
277
|
+
from src.transcript_recall import remember_session
|
|
278
|
+
from src.compaction_recovery import consume_digest
|
|
279
|
+
remember_session(session_id, transcript_path, agent=agent, cwd=str(payload.get("cwd") or ""))
|
|
280
|
+
digest = consume_digest(session_id, transcript_path, agent)
|
|
281
|
+
if digest:
|
|
282
|
+
parts.append(digest)
|
|
283
|
+
except Exception:
|
|
284
|
+
pass
|
|
285
|
+
|
|
227
286
|
if parts:
|
|
228
287
|
return {
|
|
229
288
|
"hookSpecificOutput": {
|
package/hooks/kimi_hook.py
CHANGED
|
@@ -33,6 +33,12 @@ AGENT = "kimi_code"
|
|
|
33
33
|
def handle(event: str, payload: dict) -> dict:
|
|
34
34
|
session_id = str(payload.get("session_id") or "") or f"hook_{AGENT}"
|
|
35
35
|
|
|
36
|
+
if event in ("PreCompact", "PostCompact"):
|
|
37
|
+
# Kimi speaks the Claude Code hook contract; reuse the shared handler so
|
|
38
|
+
# compaction survival stays single-sourced.
|
|
39
|
+
from hooks.claude_code_hook import handle as _cc_handle
|
|
40
|
+
return _cc_handle(event, payload, agent=AGENT)
|
|
41
|
+
|
|
36
42
|
if event == "SessionStart":
|
|
37
43
|
extra = TOKENSAFE_DIRECTIVES
|
|
38
44
|
try:
|
|
@@ -92,6 +98,16 @@ def handle(event: str, payload: dict) -> dict:
|
|
|
92
98
|
extra = f"{extra}\n\n{cum_banner}"
|
|
93
99
|
except Exception:
|
|
94
100
|
pass
|
|
101
|
+
try:
|
|
102
|
+
from src.transcript_recall import remember_session
|
|
103
|
+
from src.compaction_recovery import consume_digest
|
|
104
|
+
_tp = str(payload.get("transcript_path") or "")
|
|
105
|
+
remember_session(session_id, _tp, agent=AGENT, cwd=str(payload.get("cwd") or ""))
|
|
106
|
+
digest = consume_digest(session_id, _tp, AGENT)
|
|
107
|
+
if digest:
|
|
108
|
+
extra = f"{extra}\n\n{digest}"
|
|
109
|
+
except Exception:
|
|
110
|
+
pass
|
|
95
111
|
return {
|
|
96
112
|
"hookSpecificOutput": {
|
|
97
113
|
"hookEventName": "SessionStart",
|
|
@@ -150,6 +166,17 @@ def handle(event: str, payload: dict) -> dict:
|
|
|
150
166
|
except Exception:
|
|
151
167
|
pass
|
|
152
168
|
|
|
169
|
+
try:
|
|
170
|
+
from src.transcript_recall import remember_session
|
|
171
|
+
from src.compaction_recovery import consume_digest
|
|
172
|
+
_tp = str(payload.get("transcript_path") or "")
|
|
173
|
+
remember_session(session_id, _tp, agent=AGENT, cwd=str(payload.get("cwd") or ""))
|
|
174
|
+
digest = consume_digest(session_id, _tp, AGENT)
|
|
175
|
+
if digest:
|
|
176
|
+
parts.append(digest)
|
|
177
|
+
except Exception:
|
|
178
|
+
pass
|
|
179
|
+
|
|
153
180
|
if parts:
|
|
154
181
|
return {
|
|
155
182
|
"hookSpecificOutput": {
|
package/hooks/pre_invocation.py
CHANGED
|
@@ -364,6 +364,25 @@ def _handle(payload: dict) -> dict:
|
|
|
364
364
|
except Exception:
|
|
365
365
|
pass
|
|
366
366
|
|
|
367
|
+
# 6.5 Compaction survival: no PreCompact event on this host, so the marker
|
|
368
|
+
# comes from the usage-ledger fallback on the previous Stop.
|
|
369
|
+
try:
|
|
370
|
+
from src.transcript_recall import remember_session
|
|
371
|
+
from src.compaction_recovery import consume_digest
|
|
372
|
+
remember_session(conversation_id, transcript_path, agent="antigravity", cwd=hook_cwd)
|
|
373
|
+
_digest = consume_digest(conversation_id, transcript_path, "antigravity")
|
|
374
|
+
if _digest:
|
|
375
|
+
inject_steps.append({
|
|
376
|
+
"systemMessage": {
|
|
377
|
+
"systemMessage": _digest,
|
|
378
|
+
"metadata": {
|
|
379
|
+
"user_markdown": "♻️ **TokenSafe**: restored the context the last compaction dropped."
|
|
380
|
+
},
|
|
381
|
+
}
|
|
382
|
+
})
|
|
383
|
+
except Exception:
|
|
384
|
+
pass
|
|
385
|
+
|
|
367
386
|
return {"injectSteps": inject_steps} if inject_steps else {}
|
|
368
387
|
|
|
369
388
|
|
package/hooks/session_start.py
CHANGED
|
@@ -216,6 +216,27 @@ def main():
|
|
|
216
216
|
except Exception:
|
|
217
217
|
pass
|
|
218
218
|
|
|
219
|
+
# Compaction survival (the host has no PreCompact/PostCompact event, so the
|
|
220
|
+
# marker comes from the usage-ledger fallback on the previous Stop).
|
|
221
|
+
try:
|
|
222
|
+
from src.transcript_recall import remember_session
|
|
223
|
+
from src.compaction_recovery import consume_digest
|
|
224
|
+
_sid = str(payload.get("conversationId") or "")
|
|
225
|
+
_tp = str(payload.get("transcriptPath") or "")
|
|
226
|
+
remember_session(_sid, _tp, agent="antigravity", cwd=workspace_root)
|
|
227
|
+
_digest = consume_digest(_sid, _tp, "antigravity")
|
|
228
|
+
if _digest:
|
|
229
|
+
inject_steps.append({
|
|
230
|
+
"systemMessage": {
|
|
231
|
+
"systemMessage": _digest,
|
|
232
|
+
"metadata": {
|
|
233
|
+
"user_markdown": "♻️ **TokenSafe**: restored the context the last compaction dropped."
|
|
234
|
+
},
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
except Exception:
|
|
238
|
+
pass
|
|
239
|
+
|
|
219
240
|
print(json.dumps({"injectSteps": inject_steps}))
|
|
220
241
|
|
|
221
242
|
|
package/kimi.plugin.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
|
-
"version": "4.16.
|
|
3
|
+
"version": "4.16.1",
|
|
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": "Apache-2.0",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"mcpName": "dev.tokensafe/tokensafe",
|
|
4
|
-
"version": "4.16.
|
|
4
|
+
"version": "4.16.1",
|
|
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": {
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"bin/tokensafe.js",
|
|
13
13
|
"bin/tokensafe",
|
|
14
|
+
"bin/tokensafe-claude-hook",
|
|
15
|
+
"bin/tokensafe-mcp",
|
|
14
16
|
"bin/agy-tokens",
|
|
15
17
|
"bin/python-runtime.json",
|
|
16
18
|
"src/",
|
|
@@ -29,6 +31,8 @@
|
|
|
29
31
|
"extensions/",
|
|
30
32
|
"assets/",
|
|
31
33
|
".codex-plugin/",
|
|
34
|
+
".claude-plugin/",
|
|
35
|
+
".mcp.json",
|
|
32
36
|
"plugin.json",
|
|
33
37
|
"kimi.plugin.json",
|
|
34
38
|
"server.json",
|
package/plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"displayName": "TokenSafe",
|
|
4
|
-
"version": "4.16.
|
|
4
|
+
"version": "4.16.1",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"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.",
|
|
7
7
|
"logo": "assets/logo.png",
|
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.16.
|
|
6
|
+
"version": "4.16.1",
|
|
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.16.
|
|
26
|
+
"version": "4.16.1",
|
|
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.16.
|
|
42
|
+
"version": "4.16.1",
|
|
43
43
|
"runtimeHint": "uvx",
|
|
44
44
|
"transport": {
|
|
45
45
|
"type": "stdio"
|
package/src/agent_firewall.py
CHANGED
|
@@ -203,9 +203,10 @@ class AgentFirewall:
|
|
|
203
203
|
|
|
204
204
|
if use_pointer:
|
|
205
205
|
sym_label = f"::{symbol_key}" if symbol_key else ""
|
|
206
|
+
mtime_iso = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(current_mtime))
|
|
206
207
|
payload = (
|
|
207
208
|
f"// [TokenSafe Subagent Firewall: Pointer Hit for '{os.path.basename(abs_path)}{sym_label}' "
|
|
208
|
-
f"(unchanged, sha256:{short_hash}) — reuse the content returned earlier in this session; "
|
|
209
|
+
f"(unchanged, sha256:{short_hash}, mtime={mtime_iso}) — reuse the content returned earlier in this session; "
|
|
209
210
|
f"if you are a fresh subagent or your context was compacted, re-call smart_read_file with force_full=true]\n"
|
|
210
211
|
)
|
|
211
212
|
opt_tokens = count_tokens(payload)
|
package/src/angular_cli.py
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
`ng serve` is the only way into an Angular app that is not on Vite, and it has no
|
|
4
4
|
third-party plugin slot. What it does have is a configurable builder per project:
|
|
5
5
|
`projects.<name>.architect.serve.builder`. This module rewrites that value to
|
|
6
|
-
|
|
6
|
+
`tokensafe-ui:dev-server` (the builder registered by `tokensafe-ui/builders.json`)
|
|
7
|
+
and records the previous value so
|
|
7
8
|
`tokensafe inject --revert` restores byte-for-byte what was there.
|
|
8
9
|
|
|
9
10
|
Refusals are deliberate:
|
|
@@ -17,8 +18,8 @@ import json
|
|
|
17
18
|
import os
|
|
18
19
|
from typing import Any, Dict, Optional
|
|
19
20
|
|
|
20
|
-
BUILDER_ID = "
|
|
21
|
-
INSTALL_COMMAND = "npm install --save-dev
|
|
21
|
+
BUILDER_ID = "tokensafe-ui:dev-server"
|
|
22
|
+
INSTALL_COMMAND = "npm install --save-dev tokensafe-ui"
|
|
22
23
|
STATE_REL_PATH = os.path.join(".tokensafe", "angular-cli.json")
|
|
23
24
|
|
|
24
25
|
|
|
@@ -66,9 +67,16 @@ def _ensure_tokensafe_gitignored(app_root: str) -> None:
|
|
|
66
67
|
|
|
67
68
|
|
|
68
69
|
def package_installed(app_root: str) -> bool:
|
|
69
|
-
"""The builder must be resolvable from the workspace or `ng serve` cannot load it.
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
"""The builder must be resolvable from the workspace or `ng serve` cannot load it.
|
|
71
|
+
|
|
72
|
+
The builder ships as `tokensafe-ui:dev-server` (subpath `tokensafe-ui/angular-builder`),
|
|
73
|
+
so the package itself and its `builders.json` must both be present: an older
|
|
74
|
+
`tokensafe-ui` without the builder must not look installed.
|
|
75
|
+
"""
|
|
76
|
+
package_root = os.path.join(app_root, "node_modules", "tokensafe-ui")
|
|
77
|
+
return os.path.isfile(os.path.join(package_root, "package.json")) and os.path.isfile(
|
|
78
|
+
os.path.join(package_root, "builders.json")
|
|
79
|
+
)
|
|
72
80
|
|
|
73
81
|
|
|
74
82
|
def serve_targets(data: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
|
package/src/batch_editor.py
CHANGED
|
@@ -265,30 +265,70 @@ def _target_occurrence_lines(content: str, target: str, limit: int = 5) -> List[
|
|
|
265
265
|
return out
|
|
266
266
|
|
|
267
267
|
|
|
268
|
-
def _find_closest_line_hint(target: str, content: str, max_lines_preview: int =
|
|
269
|
-
"""
|
|
270
|
-
|
|
271
|
-
|
|
268
|
+
def _find_closest_line_hint(target: str, content: str, max_lines_preview: int = 7) -> str:
|
|
269
|
+
"""Suggest the closest block when an exact target misses.
|
|
270
|
+
|
|
271
|
+
A single-line hint cannot describe a drifted multi-line edit, so the matcher
|
|
272
|
+
slides a window of the target's length (plus/minus one line) across the file
|
|
273
|
+
and scores whole blocks with difflib. This is advisory only: the caller must
|
|
274
|
+
retry with the named range; nothing is applied automatically.
|
|
275
|
+
"""
|
|
276
|
+
target_lines = [line.rstrip() for line in target.splitlines()]
|
|
277
|
+
if not target_lines:
|
|
272
278
|
return "Target string is empty."
|
|
279
|
+
file_lines = content.splitlines()
|
|
280
|
+
if not file_lines:
|
|
281
|
+
return "Target string not found anywhere in file."
|
|
282
|
+
|
|
283
|
+
target_block = "\n".join(target_lines)
|
|
284
|
+
span = len(target_lines)
|
|
285
|
+
first = target_lines[0].strip()
|
|
286
|
+
candidates = []
|
|
287
|
+
for idx, line in enumerate(file_lines):
|
|
288
|
+
if first:
|
|
289
|
+
first_ratio = difflib.SequenceMatcher(None, first, line.strip()).ratio()
|
|
290
|
+
if first_ratio < 0.45:
|
|
291
|
+
continue
|
|
292
|
+
for size in (span, max(1, span - 1), span + 1):
|
|
293
|
+
if idx + size > len(file_lines):
|
|
294
|
+
continue
|
|
295
|
+
block = "\n".join(part.rstrip() for part in file_lines[idx:idx + size])
|
|
296
|
+
ratio = difflib.SequenceMatcher(None, target_block, block).ratio()
|
|
297
|
+
candidates.append((ratio, idx, size))
|
|
298
|
+
if len(candidates) > 900:
|
|
299
|
+
break
|
|
273
300
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
301
|
+
if not candidates:
|
|
302
|
+
return "Target string not found anywhere in file."
|
|
303
|
+
candidates.sort(key=lambda item: (item[0], -item[1]), reverse=True)
|
|
304
|
+
best_ratio, best_idx, best_size = candidates[0]
|
|
305
|
+
if best_ratio < 0.55:
|
|
306
|
+
return "Target string not found anywhere in file."
|
|
307
|
+
|
|
308
|
+
start = best_idx + 1
|
|
309
|
+
end = best_idx + best_size
|
|
310
|
+
rival = next(
|
|
311
|
+
(
|
|
312
|
+
item for item in candidates[1:]
|
|
313
|
+
if item[0] >= best_ratio - 0.03 and abs(item[1] - best_idx) >= best_size
|
|
314
|
+
),
|
|
315
|
+
None,
|
|
316
|
+
)
|
|
317
|
+
ambiguity = ""
|
|
318
|
+
if rival and best_ratio < 0.97:
|
|
319
|
+
ambiguity = f" (a second candidate is at line {rival[1] + 1}, similarity {rival[0]:.0%})"
|
|
320
|
+
|
|
321
|
+
preview_start = max(0, best_idx - 1)
|
|
322
|
+
preview_end = min(len(file_lines), best_idx + best_size + 1)
|
|
323
|
+
preview = []
|
|
324
|
+
for index in range(preview_start, preview_end):
|
|
325
|
+
marker = ">> " if best_idx <= index < best_idx + best_size else " "
|
|
326
|
+
preview.append(f"{marker}L{index + 1}: {file_lines[index]}")
|
|
327
|
+
return (
|
|
328
|
+
f"Closest block at lines {start}-{end} (similarity {best_ratio:.0%}){ambiguity}:\n"
|
|
329
|
+
+ "\n".join(preview)
|
|
330
|
+
+ f"\nRetry with start_line={start}, end_line={end}."
|
|
331
|
+
)
|
|
292
332
|
|
|
293
333
|
|
|
294
334
|
def _dominant_newline(text: str) -> str:
|