tokensafe 4.16.3 → 5.0.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/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +1 -1
- package/bin/agy-tokens +1 -1
- package/bin/tokensafe +1 -1
- package/bin/tokensafe-acp.js +1 -1
- package/bin/tokensafe-mcp +1 -1
- package/bin/tokensafe.js +1 -1
- package/hooks/claude_code_hook.py +49 -45
- package/hooks/cursor_hook.py +8 -8
- package/hooks/kimi_hook.py +23 -23
- package/hooks/post_tool_use.py +6 -6
- package/hooks/pre_invocation.py +21 -17
- package/hooks/pre_tool_use.py +27 -27
- package/hooks/session_start.py +17 -17
- package/hooks/stop_hook.py +3 -3
- package/hooks/zcode_hook.py +2 -2
- package/kimi.plugin.json +2 -2
- package/mcp_config.json +1 -1
- package/package.json +5 -4
- package/plugin.json +1 -1
- package/server.json +3 -3
- package/src/__init__.py +9 -5
- package/src/cli.py +11 -2487
- package/src/mcp_server.py +11 -5437
- package/src/proxy.py +11 -1834
- package/tokensafe/__init__.py +6 -0
- package/{src → tokensafe}/acp/__init__.py +2 -2
- package/{src → tokensafe}/acp/server.py +5 -5
- package/{src → tokensafe}/adapter_core.py +21 -18
- package/{src → tokensafe}/agent/__init__.py +1 -1
- package/{src → tokensafe}/agent/context.py +4 -4
- package/{src → tokensafe}/agent/loop.py +4 -4
- package/{src → tokensafe}/agent/providers.py +2 -2
- package/{src → tokensafe}/agent/tools.py +2 -2
- package/{src → tokensafe}/agent_firewall.py +51 -10
- package/{src → tokensafe}/angular_cli.py +4 -4
- package/{src → tokensafe}/auto_inject.py +37 -5
- package/{src → tokensafe}/batch_editor.py +185 -28
- package/tokensafe/batch_search.py +1191 -0
- package/{src → tokensafe}/bench/go_target.py +1 -1
- package/{src → tokensafe}/bench/mechanics.py +1 -1
- package/{src → tokensafe}/bench/react_target.py +1 -1
- package/{src → tokensafe}/bench/runner.py +4 -4
- package/{src → tokensafe}/benchmark.py +12 -12
- package/{src → tokensafe}/benchmark_real.py +18 -18
- package/{src → tokensafe}/build_parsers.py +3 -3
- package/{src → tokensafe}/bundle_probe.py +1 -1
- package/{src → tokensafe}/burn_monitor.py +3 -3
- package/{src → tokensafe}/cache_normalizer.py +1 -1
- package/tokensafe/cli.py +2490 -0
- package/{src → tokensafe}/command_sanitizer.py +94 -26
- package/{src → tokensafe}/compaction_recovery.py +5 -5
- package/{src → tokensafe}/compactor.py +148 -95
- package/{src → tokensafe}/context_index.py +194 -28
- package/{src → tokensafe}/cross_stack.py +2 -2
- package/{src → tokensafe}/dashboard.py +3 -3
- package/{src → tokensafe}/delegation.py +4 -4
- package/{src → tokensafe}/dependency_prefetcher.py +1 -1
- package/{src → tokensafe}/diff_slicer.py +2 -2
- package/{src → tokensafe}/distiller.py +7 -7
- package/{src → tokensafe}/distiller_cache.py +3 -3
- package/{src → tokensafe}/egress_counter.py +1 -1
- package/{src → tokensafe}/environment_auditor.py +7 -7
- package/{src → tokensafe}/file_state.py +2 -2
- package/{src → tokensafe}/format_invariants.py +1 -1
- package/tokensafe/format_parsers/__init__.py +20 -0
- package/{src → tokensafe}/format_parsers/compiler_errors.py +2 -2
- package/{src → tokensafe}/format_parsers/docker_k8s.py +1 -1
- package/{src → tokensafe}/format_parsers/i18n_strings.py +19 -9
- package/{src → tokensafe}/format_parsers/json_log.py +1 -1
- package/{src → tokensafe}/format_parsers/lockfiles.py +4 -4
- package/{src → tokensafe}/format_parsers/tabular.py +1 -1
- package/{src → tokensafe}/fts_index.py +20 -11
- package/{src → tokensafe}/governor.py +143 -31
- package/{src → tokensafe}/graph_engine.py +729 -126
- package/{src → tokensafe}/guard.py +80 -25
- package/{src → tokensafe}/history_compactor.py +407 -133
- package/{src → tokensafe}/i18n_catalog.py +39 -14
- package/{src → tokensafe}/image_downscale.py +1 -1
- package/{src → tokensafe}/ingress_staging.py +9 -9
- package/{src → tokensafe}/installer.py +118 -61
- package/{src → tokensafe}/license_manager.py +9 -67
- package/{src → tokensafe}/live_relay.py +2 -2
- package/{src → tokensafe}/loop_detector.py +2 -2
- package/{src → tokensafe}/mcp_config_manager.py +2 -2
- package/{src → tokensafe}/mcp_proxy.py +4 -4
- package/tokensafe/mcp_server.py +5668 -0
- package/{src → tokensafe}/metrics.py +56 -21
- package/{src → tokensafe}/mistakes.py +4 -4
- package/{src → tokensafe}/oumv_brief.py +21 -3
- package/{src → tokensafe}/oumv_gateway.py +146 -49
- package/{src → tokensafe}/oumv_policy.py +1 -1
- package/{src → tokensafe}/paths.py +1 -1
- package/{src → tokensafe}/project_memory.py +4 -4
- package/tokensafe/proxy.py +2075 -0
- package/{src → tokensafe}/quality_score.py +4 -4
- package/{src → tokensafe}/read_baseline.py +1 -1
- package/{src → tokensafe}/repo_compute.py +1 -1
- package/{src → tokensafe}/result_ledger.py +19 -19
- package/{src → tokensafe}/retrofit.py +6 -6
- package/{src → tokensafe}/savings.py +3 -3
- package/tokensafe/schema_slicer.py +1087 -0
- package/{src → tokensafe}/scoped_search.py +144 -6
- package/{src → tokensafe}/secret_redactor.py +22 -0
- package/{src → tokensafe}/session_brief.py +8 -8
- package/{src → tokensafe}/skeletonizer.py +89 -26
- package/{src → tokensafe}/slicer.py +3 -2
- package/{src → tokensafe}/snapshot.py +1 -1
- package/{src → tokensafe}/stash.py +1 -1
- package/{src → tokensafe}/statusline.py +1 -1
- package/{src → tokensafe}/superseded.py +22 -4
- package/{src → tokensafe}/team_cache.py +3 -3
- package/{src → tokensafe}/telemetry_sync.py +63 -11
- package/{src → tokensafe}/test_parsers.py +3 -3
- package/{src → tokensafe}/thinking_fold.py +3 -3
- package/{src → tokensafe}/tokenizer.py +37 -1
- package/{src → tokensafe}/tool_pruner.py +3 -3
- package/{src → tokensafe}/transcript_analyzer.py +1 -1
- package/{src → tokensafe}/transcript_recall.py +8 -8
- package/{src → tokensafe}/type_slicer.py +3 -3
- package/{src → tokensafe}/usage_ledger.py +4 -4
- package/{src → tokensafe}/version_checker.py +6 -6
- package/{src → tokensafe}/web_reader.py +86 -8
- package/{src → tokensafe}/workspace_roots.py +2 -2
- package/src/batch_search.py +0 -589
- package/src/format_parsers/__init__.py +0 -20
- package/src/schema_slicer.py +0 -578
- /package/{src → tokensafe}/agent/pricing.py +0 -0
- /package/{src → tokensafe}/atomic_io.py +0 -0
- /package/{src → tokensafe}/backend_release.py +0 -0
- /package/{src → tokensafe}/bench/__init__.py +0 -0
- /package/{src → tokensafe}/bench/driver.py +0 -0
- /package/{src → tokensafe}/bench/tokensafe_mcp.py +0 -0
- /package/{src → tokensafe}/bench_scenarios.json +0 -0
- /package/{src → tokensafe}/competitors.py +0 -0
- /package/{src → tokensafe}/format_parsers/ansi.py +0 -0
- /package/{src → tokensafe}/format_parsers/game_assets.py +0 -0
- /package/{src → tokensafe}/globmatch.py +0 -0
- /package/{src → tokensafe}/host_capabilities.py +0 -0
- /package/{src → tokensafe}/json_repair.py +0 -0
- /package/{src → tokensafe}/route_index.py +0 -0
- /package/{src → tokensafe}/scaffold.py +0 -0
- /package/{src → tokensafe}/session_context.py +0 -0
- /package/{src → tokensafe}/ssl_context.py +0 -0
- /package/{src → tokensafe}/stdio_io.py +0 -0
- /package/{src → tokensafe}/syntax_guard.py +0 -0
- /package/{src → tokensafe}/toml_mcp_config.py +0 -0
- /package/{src → tokensafe}/treesitter_backend.py +0 -0
- /package/{src → tokensafe}/win_paths.py +0 -0
package/hooks/pre_invocation.py
CHANGED
|
@@ -21,10 +21,10 @@ PROJECT_ROOT = os.path.dirname(HOOK_DIR)
|
|
|
21
21
|
if PROJECT_ROOT not in sys.path:
|
|
22
22
|
sys.path.insert(0, PROJECT_ROOT)
|
|
23
23
|
|
|
24
|
-
from
|
|
25
|
-
from
|
|
26
|
-
from
|
|
27
|
-
from
|
|
24
|
+
from tokensafe.adapter_core import TOKENSAFE_DIRECTIVES as PROTOCOL_DIRECTIVES, resolve_event_model
|
|
25
|
+
from tokensafe.license_manager import LicenseManager
|
|
26
|
+
from tokensafe.metrics import MetricsTracker
|
|
27
|
+
from tokensafe.paths import resolve_data_dir
|
|
28
28
|
|
|
29
29
|
def _model_name(payload: dict) -> str:
|
|
30
30
|
"""hooks.md contract: all payload keys are camelCase, the model arrives as
|
|
@@ -62,7 +62,7 @@ def _handle(payload: dict) -> dict:
|
|
|
62
62
|
transcript_path = str(payload.get("transcriptPath") or "")
|
|
63
63
|
|
|
64
64
|
try:
|
|
65
|
-
from
|
|
65
|
+
from tokensafe.paths import save_active_workspace
|
|
66
66
|
raw_ws = payload.get("workspacePaths")
|
|
67
67
|
if raw_ws and isinstance(raw_ws, list) and raw_ws[0]:
|
|
68
68
|
save_active_workspace(str(raw_ws[0]))
|
|
@@ -89,7 +89,7 @@ def _handle(payload: dict) -> dict:
|
|
|
89
89
|
|
|
90
90
|
# 0. Asynchronous telemetry sync (detached & throttled to 20s)
|
|
91
91
|
try:
|
|
92
|
-
from
|
|
92
|
+
from tokensafe.telemetry_sync import TelemetrySync
|
|
93
93
|
TelemetrySync.sync_background(throttle_seconds=20.0)
|
|
94
94
|
except Exception:
|
|
95
95
|
pass
|
|
@@ -112,7 +112,7 @@ def _handle(payload: dict) -> dict:
|
|
|
112
112
|
}
|
|
113
113
|
})
|
|
114
114
|
elif last_user_input and len(last_user_input.strip()) >= 5 and not _proxy_is_active():
|
|
115
|
-
from
|
|
115
|
+
from tokensafe.agent import context as kernel
|
|
116
116
|
compaction = kernel.compact_prompt(last_user_input)
|
|
117
117
|
saved_tokens = compaction["saved_tokens"]
|
|
118
118
|
reduction_pct = compaction["reduction_pct"]
|
|
@@ -150,7 +150,7 @@ def _handle(payload: dict) -> dict:
|
|
|
150
150
|
|
|
151
151
|
# 2.5 Proactive Anti-Stuck Guard (Inject recent failure lessons from MistakesLedger)
|
|
152
152
|
try:
|
|
153
|
-
from
|
|
153
|
+
from tokensafe.mistakes import MistakesLedger
|
|
154
154
|
active_guard = MistakesLedger.get_guard_prompt(project_root=hook_cwd)
|
|
155
155
|
if active_guard:
|
|
156
156
|
inject_steps.append({
|
|
@@ -171,7 +171,7 @@ def _handle(payload: dict) -> dict:
|
|
|
171
171
|
fresh_fold = False
|
|
172
172
|
orig_hist_tokens = 0
|
|
173
173
|
if not is_quota_exceeded and invocation_num >= 3 and transcript_path:
|
|
174
|
-
from
|
|
174
|
+
from tokensafe.agent import context as kernel
|
|
175
175
|
folded_text, orig_hist_tokens, folded_tokens, stats = kernel.fold_history(
|
|
176
176
|
transcript_path, invocation_num
|
|
177
177
|
)
|
|
@@ -186,14 +186,18 @@ def _handle(payload: dict) -> dict:
|
|
|
186
186
|
if prev_event is None or delta_orig >= 1500:
|
|
187
187
|
fresh_fold = True
|
|
188
188
|
inc_orig = orig_hist_tokens if prev_event is None else delta_orig
|
|
189
|
+
# The digest is ADDED to the context; the history it summarizes
|
|
190
|
+
# stays there. Record the injected mass with zero savings.
|
|
189
191
|
tracker.record_event(
|
|
190
192
|
session_id=conversation_id,
|
|
191
193
|
event_type="HISTORY_FOLD",
|
|
192
|
-
original_tokens=
|
|
194
|
+
original_tokens=folded_tokens,
|
|
193
195
|
optimized_tokens=folded_tokens,
|
|
194
196
|
details={
|
|
195
197
|
"turns": invocation_num,
|
|
196
198
|
"total_accum_orig": orig_hist_tokens,
|
|
199
|
+
"history_tokens": inc_orig,
|
|
200
|
+
"injected": True,
|
|
197
201
|
"read_files": stats.get("read_files_count", 0),
|
|
198
202
|
"edited_files": stats.get("edited_files_count", 0),
|
|
199
203
|
"model": _model_name(payload) or resolve_event_model(transcript_path, conversation_id)
|
|
@@ -201,8 +205,8 @@ def _handle(payload: dict) -> dict:
|
|
|
201
205
|
)
|
|
202
206
|
|
|
203
207
|
hist_badge = (
|
|
204
|
-
f"⚡ **History Compactor:**
|
|
205
|
-
f"
|
|
208
|
+
f"⚡ **History Compactor:** orientation digest of {orig_hist_tokens:,} tokens of "
|
|
209
|
+
f"previous turns (+{folded_tokens:,} tokens; history unchanged)."
|
|
206
210
|
)
|
|
207
211
|
|
|
208
212
|
inject_steps.append({
|
|
@@ -333,7 +337,7 @@ def _handle(payload: dict) -> dict:
|
|
|
333
337
|
|
|
334
338
|
# 5.6 Delegated result digest: consume once per delegation.
|
|
335
339
|
try:
|
|
336
|
-
from
|
|
340
|
+
from tokensafe.delegation import render as _render_delegation, take as _take_delegation
|
|
337
341
|
_delegated = _take_delegation(conversation_id)
|
|
338
342
|
if _delegated:
|
|
339
343
|
inject_steps.append({
|
|
@@ -348,7 +352,7 @@ def _handle(payload: dict) -> dict:
|
|
|
348
352
|
# 6. Multi-Agent Live Burn Monitor (rate-limited, only fires on real burn spikes)
|
|
349
353
|
if invocation_num >= 2 or transcript_path:
|
|
350
354
|
try:
|
|
351
|
-
from
|
|
355
|
+
from tokensafe.burn_monitor import get_burn_warnings
|
|
352
356
|
burn_warn = get_burn_warnings(
|
|
353
357
|
agent="antigravity",
|
|
354
358
|
session_id=conversation_id,
|
|
@@ -367,8 +371,8 @@ def _handle(payload: dict) -> dict:
|
|
|
367
371
|
# 6.5 Compaction survival: no PreCompact event on this host, so the marker
|
|
368
372
|
# comes from the usage-ledger fallback on the previous Stop.
|
|
369
373
|
try:
|
|
370
|
-
from
|
|
371
|
-
from
|
|
374
|
+
from tokensafe.transcript_recall import remember_session
|
|
375
|
+
from tokensafe.compaction_recovery import consume_digest
|
|
372
376
|
remember_session(conversation_id, transcript_path, agent="antigravity", cwd=hook_cwd)
|
|
373
377
|
_digest = consume_digest(conversation_id, transcript_path, "antigravity")
|
|
374
378
|
if _digest:
|
|
@@ -388,7 +392,7 @@ def _handle(payload: dict) -> dict:
|
|
|
388
392
|
|
|
389
393
|
def main():
|
|
390
394
|
try:
|
|
391
|
-
from
|
|
395
|
+
from tokensafe.stdio_io import read_stdin_text
|
|
392
396
|
raw_input = read_stdin_text()
|
|
393
397
|
if not raw_input.strip():
|
|
394
398
|
print(json.dumps({}))
|
package/hooks/pre_tool_use.py
CHANGED
|
@@ -17,16 +17,16 @@ PROJECT_ROOT = os.path.dirname(HOOK_DIR)
|
|
|
17
17
|
if PROJECT_ROOT not in sys.path:
|
|
18
18
|
sys.path.insert(0, PROJECT_ROOT)
|
|
19
19
|
|
|
20
|
-
from
|
|
21
|
-
from
|
|
22
|
-
from
|
|
23
|
-
from
|
|
24
|
-
from
|
|
20
|
+
from tokensafe.loop_detector import LoopDetector
|
|
21
|
+
from tokensafe.metrics import MetricsTracker
|
|
22
|
+
from tokensafe.tokenizer import count_tokens
|
|
23
|
+
from tokensafe.license_manager import LicenseManager
|
|
24
|
+
from tokensafe.adapter_core import guard_write
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
def _trigger_bg_sync():
|
|
28
28
|
try:
|
|
29
|
-
from
|
|
29
|
+
from tokensafe.telemetry_sync import TelemetrySync
|
|
30
30
|
TelemetrySync.sync_background(throttle_seconds=20.0)
|
|
31
31
|
except Exception:
|
|
32
32
|
pass
|
|
@@ -45,7 +45,7 @@ def _handle(payload: dict) -> dict:
|
|
|
45
45
|
# route through the CC/ZCode PreToolUse evaluator so we return {} / hookSpecificOutput
|
|
46
46
|
# instead of {"decision": "allow"} (which causes hook.run.failed in ZCode).
|
|
47
47
|
if not isinstance(payload.get("toolCall"), dict) and payload.get("tool_name"):
|
|
48
|
-
from
|
|
48
|
+
from tokensafe.adapter_core import evaluate_cc_pre_tool_use
|
|
49
49
|
return evaluate_cc_pre_tool_use(payload, agent=str(payload.get("agent") or "zcode"))
|
|
50
50
|
|
|
51
51
|
tool_call = payload.get("toolCall")
|
|
@@ -55,7 +55,7 @@ def _handle(payload: dict) -> dict:
|
|
|
55
55
|
args = tool_call.get("args")
|
|
56
56
|
if not isinstance(args, dict):
|
|
57
57
|
args = {}
|
|
58
|
-
from
|
|
58
|
+
from tokensafe.adapter_core import canonical_tool_name
|
|
59
59
|
canon_tool = canonical_tool_name(tool_name)
|
|
60
60
|
conversation_id = str(payload.get("conversationId") or "default_session")
|
|
61
61
|
last_user_input = str(payload.get("lastUserInput") or "")
|
|
@@ -65,7 +65,7 @@ def _handle(payload: dict) -> dict:
|
|
|
65
65
|
explicit_cwd = str(payload.get("cwd") or "").strip()
|
|
66
66
|
hook_cwd = explicit_cwd or (workspace_paths[0] if workspace_paths else "") or os.getcwd()
|
|
67
67
|
try:
|
|
68
|
-
from
|
|
68
|
+
from tokensafe.paths import save_active_workspace
|
|
69
69
|
if workspace_paths:
|
|
70
70
|
save_active_workspace(workspace_paths[0])
|
|
71
71
|
elif explicit_cwd and any(
|
|
@@ -81,7 +81,7 @@ def _handle(payload: dict) -> dict:
|
|
|
81
81
|
if is_loop:
|
|
82
82
|
tracker = MetricsTracker()
|
|
83
83
|
try:
|
|
84
|
-
from
|
|
84
|
+
from tokensafe.mistakes import MistakesLedger
|
|
85
85
|
MistakesLedger.record_from_loop(tool_name, args, repeat_count, project_root=hook_cwd)
|
|
86
86
|
except Exception:
|
|
87
87
|
pass
|
|
@@ -103,7 +103,7 @@ def _handle(payload: dict) -> dict:
|
|
|
103
103
|
return response
|
|
104
104
|
|
|
105
105
|
if canon_tool == "batch_edit":
|
|
106
|
-
from
|
|
106
|
+
from tokensafe.adapter_core import guard_batch_payload
|
|
107
107
|
verdict = guard_batch_payload(args, "antigravity", conversation_id)
|
|
108
108
|
if verdict["decision"] == "deny":
|
|
109
109
|
return {"decision": "deny", "reason": verdict["reason"]}
|
|
@@ -120,7 +120,7 @@ def _handle(payload: dict) -> dict:
|
|
|
120
120
|
# Kill switch: TOKENSAFE_STALE_GUARD=0.
|
|
121
121
|
if canon_tool == "edit":
|
|
122
122
|
try:
|
|
123
|
-
from
|
|
123
|
+
from tokensafe.file_state import FileState
|
|
124
124
|
if FileState.guard_enabled():
|
|
125
125
|
raw_path = str(args.get("Path") or args.get("TargetFile") or args.get("path") or args.get("file_path") or "")
|
|
126
126
|
stale = FileState.check_stale(_resolve_hook_path(raw_path, hook_cwd)) if raw_path else None
|
|
@@ -154,7 +154,7 @@ def _handle(payload: dict) -> dict:
|
|
|
154
154
|
# TOKENSAFE_EDIT_REWRITE_GUARD=0.
|
|
155
155
|
if canon_tool == "edit":
|
|
156
156
|
try:
|
|
157
|
-
from
|
|
157
|
+
from tokensafe.adapter_core import guard_edit_rewrite
|
|
158
158
|
raw_path = str(args.get("Path") or args.get("TargetFile") or args.get("path") or args.get("file_path") or "")
|
|
159
159
|
verdict = guard_edit_rewrite(_resolve_hook_path(raw_path, hook_cwd), args, "antigravity", conversation_id)
|
|
160
160
|
if verdict["decision"] == "ask":
|
|
@@ -165,8 +165,8 @@ def _handle(payload: dict) -> dict:
|
|
|
165
165
|
|
|
166
166
|
# 2. TokenSafe Guard: Inspect file writes and edits for security violations
|
|
167
167
|
if canon_tool in ("write", "edit"):
|
|
168
|
-
from
|
|
169
|
-
from
|
|
168
|
+
from tokensafe.guard import TokenSafeGuard
|
|
169
|
+
from tokensafe.adapter_core import extract_write_content
|
|
170
170
|
raw_path = str(args.get("Path") or args.get("TargetFile") or args.get("path") or args.get("file_path") or "")
|
|
171
171
|
path = _resolve_hook_path(raw_path, hook_cwd)
|
|
172
172
|
code_to_check = (
|
|
@@ -192,7 +192,7 @@ def _handle(payload: dict) -> dict:
|
|
|
192
192
|
|
|
193
193
|
# Anti-Stuck hybrid escalation check
|
|
194
194
|
try:
|
|
195
|
-
from
|
|
195
|
+
from tokensafe.mistakes import MistakesLedger
|
|
196
196
|
is_blocked, m_reason = MistakesLedger.should_force_ask(path or raw_path, action_type="edit_file", project_root=hook_cwd)
|
|
197
197
|
if is_blocked:
|
|
198
198
|
return {"decision": "force_ask", "reason": f"[TokenSafe Anti-Stuck Guard] {m_reason}"}
|
|
@@ -216,8 +216,8 @@ def _handle(payload: dict) -> dict:
|
|
|
216
216
|
# 3. read_file Optimization (AST Windowing & Skeletonization).
|
|
217
217
|
# 'view_file' and 'Read' route through the same optimization pipeline.
|
|
218
218
|
elif canon_tool == "read":
|
|
219
|
-
from
|
|
220
|
-
from
|
|
219
|
+
from tokensafe.slicer import ToolSlicer
|
|
220
|
+
from tokensafe.skeletonizer import Skeletonizer
|
|
221
221
|
raw_path = str(args.get("Path") or args.get("path") or args.get("FilePath") or args.get("file_path") or "")
|
|
222
222
|
path = _resolve_hook_path(raw_path, hook_cwd)
|
|
223
223
|
start_line = args.get("StartLine")
|
|
@@ -243,7 +243,7 @@ def _handle(payload: dict) -> dict:
|
|
|
243
243
|
# Step 3-Image: Downscale oversized vision images (>1024px)
|
|
244
244
|
if path and os.path.exists(path) and path.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
|
|
245
245
|
try:
|
|
246
|
-
from
|
|
246
|
+
from tokensafe.image_downscale import ImageDownscaler
|
|
247
247
|
opt_res = ImageDownscaler.optimize(path, max_edge=1024)
|
|
248
248
|
if opt_res.get("success") and opt_res.get("was_downscaled"):
|
|
249
249
|
response["overwrite"] = {path_key: opt_res["optimized_path"]}
|
|
@@ -267,7 +267,7 @@ def _handle(payload: dict) -> dict:
|
|
|
267
267
|
# Step 3-Lockfile: Intercept raw lockfile reads and redirect to generated summary
|
|
268
268
|
if path and os.path.exists(path):
|
|
269
269
|
try:
|
|
270
|
-
from
|
|
270
|
+
from tokensafe.format_parsers.lockfiles import LockfileReducer
|
|
271
271
|
if LockfileReducer.is_lockfile(path):
|
|
272
272
|
if start_line is None or end_line is None or (int(end_line) - int(start_line) > 50):
|
|
273
273
|
summary_path = LockfileReducer.generate_lockfile_summary(path)
|
|
@@ -297,7 +297,7 @@ def _handle(payload: dict) -> dict:
|
|
|
297
297
|
if start_line is not None and end_line is not None:
|
|
298
298
|
if not response.get("reason"):
|
|
299
299
|
try:
|
|
300
|
-
from
|
|
300
|
+
from tokensafe.adapter_core import get_oumv_read_edit_advisory
|
|
301
301
|
oumv_note = get_oumv_read_edit_advisory(hook_cwd, path or raw_path, conversation_id)
|
|
302
302
|
if oumv_note:
|
|
303
303
|
response["reason"] = oumv_note
|
|
@@ -378,7 +378,7 @@ def _handle(payload: dict) -> dict:
|
|
|
378
378
|
|
|
379
379
|
if not response.get("reason"):
|
|
380
380
|
try:
|
|
381
|
-
from
|
|
381
|
+
from tokensafe.adapter_core import get_oumv_read_edit_advisory
|
|
382
382
|
oumv_note = get_oumv_read_edit_advisory(hook_cwd, path or raw_path, conversation_id)
|
|
383
383
|
if oumv_note:
|
|
384
384
|
response["reason"] = oumv_note
|
|
@@ -387,9 +387,9 @@ def _handle(payload: dict) -> dict:
|
|
|
387
387
|
|
|
388
388
|
# 4. run_command Optimization & TokenSafe Guard
|
|
389
389
|
elif canon_tool == "bash":
|
|
390
|
-
from
|
|
391
|
-
from
|
|
392
|
-
from
|
|
390
|
+
from tokensafe.guard import TokenSafeGuard
|
|
391
|
+
from tokensafe.diff_slicer import DiffSlicer
|
|
392
|
+
from tokensafe.command_sanitizer import CommandSanitizer
|
|
393
393
|
cmd = str(args.get("CommandLine") or args.get("command") or "")
|
|
394
394
|
guard_res = TokenSafeGuard.inspect_command(cmd, cwd=hook_cwd)
|
|
395
395
|
if guard_res.blocked:
|
|
@@ -407,7 +407,7 @@ def _handle(payload: dict) -> dict:
|
|
|
407
407
|
|
|
408
408
|
# Anti-Stuck hybrid escalation check
|
|
409
409
|
try:
|
|
410
|
-
from
|
|
410
|
+
from tokensafe.mistakes import MistakesLedger
|
|
411
411
|
is_blocked, m_reason = MistakesLedger.should_force_ask(cmd, action_type="command", project_root=hook_cwd)
|
|
412
412
|
if is_blocked:
|
|
413
413
|
return {"decision": "force_ask", "reason": f"[TokenSafe Anti-Stuck Guard] {m_reason}"}
|
|
@@ -458,7 +458,7 @@ def _handle(payload: dict) -> dict:
|
|
|
458
458
|
|
|
459
459
|
def main():
|
|
460
460
|
try:
|
|
461
|
-
from
|
|
461
|
+
from tokensafe.stdio_io import read_stdin_text
|
|
462
462
|
raw_input = read_stdin_text()
|
|
463
463
|
if not raw_input.strip():
|
|
464
464
|
print(json.dumps({"decision": "allow"}))
|
package/hooks/session_start.py
CHANGED
|
@@ -25,10 +25,10 @@ PROJECT_ROOT = os.path.dirname(HOOK_DIR)
|
|
|
25
25
|
if PROJECT_ROOT not in sys.path:
|
|
26
26
|
sys.path.insert(0, PROJECT_ROOT)
|
|
27
27
|
|
|
28
|
-
from
|
|
29
|
-
from
|
|
30
|
-
from
|
|
31
|
-
from
|
|
28
|
+
from tokensafe.metrics import MetricsTracker
|
|
29
|
+
from tokensafe.license_manager import LicenseManager
|
|
30
|
+
from tokensafe.environment_auditor import EnvironmentAuditor
|
|
31
|
+
from tokensafe.adapter_core import TOKENSAFE_DIRECTIVES as PROTOCOL_DIRECTIVES
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
def _user_markdown(markdown: str) -> dict:
|
|
@@ -53,7 +53,7 @@ def _guest_pitch_step(quota: dict):
|
|
|
53
53
|
limit = quota.get("monthly_limit")
|
|
54
54
|
if limit is None or int(limit) >= 500000:
|
|
55
55
|
return None
|
|
56
|
-
from
|
|
56
|
+
from tokensafe.paths import resolve_data_dir
|
|
57
57
|
marker_path = os.path.join(resolve_data_dir(), ".guest_pitch_shown")
|
|
58
58
|
if os.path.exists(marker_path):
|
|
59
59
|
return None
|
|
@@ -71,7 +71,7 @@ def _guest_pitch_step(quota: dict):
|
|
|
71
71
|
|
|
72
72
|
def main():
|
|
73
73
|
try:
|
|
74
|
-
from
|
|
74
|
+
from tokensafe.stdio_io import read_stdin_text
|
|
75
75
|
raw_input = read_stdin_text()
|
|
76
76
|
if not raw_input.strip():
|
|
77
77
|
print(json.dumps({}))
|
|
@@ -89,7 +89,7 @@ def main():
|
|
|
89
89
|
workspace_paths = [str(w) for w in raw_ws if str(w).strip()] if isinstance(raw_ws, list) else []
|
|
90
90
|
workspace_root = workspace_paths[0] if workspace_paths else os.getcwd()
|
|
91
91
|
try:
|
|
92
|
-
from
|
|
92
|
+
from tokensafe.paths import save_active_workspace, ensure_tokensafe_gitignore
|
|
93
93
|
if workspace_paths:
|
|
94
94
|
save_active_workspace(workspace_root)
|
|
95
95
|
ensure_tokensafe_gitignore(workspace_root)
|
|
@@ -105,7 +105,7 @@ def main():
|
|
|
105
105
|
# gate: folder-opened is the ONLY condition — no project-marker sniffing).
|
|
106
106
|
try:
|
|
107
107
|
if workspace_paths:
|
|
108
|
-
from
|
|
108
|
+
from tokensafe.graph_engine import GraphEngine
|
|
109
109
|
# A warm index answers without a rescan; only a repo that has never
|
|
110
110
|
# been indexed pays the one-time crawl (the banner is the product).
|
|
111
111
|
graph = GraphEngine.get_cached(workspace_root)
|
|
@@ -121,7 +121,7 @@ def main():
|
|
|
121
121
|
)
|
|
122
122
|
# Separate try: an OUMV brief failure must not drop the God-Nodes banner.
|
|
123
123
|
try:
|
|
124
|
-
from
|
|
124
|
+
from tokensafe.retrofit import format_oumv_workspace_brief
|
|
125
125
|
oumv_brief = format_oumv_workspace_brief(workspace_root, include_live=True)
|
|
126
126
|
if oumv_brief:
|
|
127
127
|
extra.append(oumv_brief)
|
|
@@ -131,7 +131,7 @@ def main():
|
|
|
131
131
|
# Every open folder is routed: the first path is the session root, the
|
|
132
132
|
# rest are additional working directories the agent may edit.
|
|
133
133
|
try:
|
|
134
|
-
from
|
|
134
|
+
from tokensafe.session_brief import build_workspace_brief, workspace_roots_from_env
|
|
135
135
|
routing = build_workspace_brief(
|
|
136
136
|
workspace_root,
|
|
137
137
|
include_live=True,
|
|
@@ -149,7 +149,7 @@ def main():
|
|
|
149
149
|
system_sections = [directives]
|
|
150
150
|
|
|
151
151
|
try:
|
|
152
|
-
from
|
|
152
|
+
from tokensafe.mistakes import MistakesLedger
|
|
153
153
|
mistake_guard = MistakesLedger.get_guard_prompt(project_root=workspace_root)
|
|
154
154
|
if mistake_guard:
|
|
155
155
|
system_sections.append(mistake_guard)
|
|
@@ -158,7 +158,7 @@ def main():
|
|
|
158
158
|
|
|
159
159
|
try:
|
|
160
160
|
if workspace_paths:
|
|
161
|
-
from
|
|
161
|
+
from tokensafe.snapshot import build_workspace_snapshot
|
|
162
162
|
snapshot = build_workspace_snapshot(workspace_root, max_bytes=1400)
|
|
163
163
|
if snapshot:
|
|
164
164
|
system_sections.append(snapshot)
|
|
@@ -166,7 +166,7 @@ def main():
|
|
|
166
166
|
pass
|
|
167
167
|
|
|
168
168
|
try:
|
|
169
|
-
from
|
|
169
|
+
from tokensafe.adapter_core import format_cumulative_banner
|
|
170
170
|
cum_banner = format_cumulative_banner()
|
|
171
171
|
if cum_banner:
|
|
172
172
|
system_sections.append(cum_banner)
|
|
@@ -189,7 +189,7 @@ def main():
|
|
|
189
189
|
pass
|
|
190
190
|
|
|
191
191
|
try:
|
|
192
|
-
from
|
|
192
|
+
from tokensafe.version_checker import VersionChecker
|
|
193
193
|
VersionChecker.notify_os_update_background()
|
|
194
194
|
except Exception:
|
|
195
195
|
pass
|
|
@@ -205,7 +205,7 @@ def main():
|
|
|
205
205
|
pass
|
|
206
206
|
|
|
207
207
|
try:
|
|
208
|
-
from
|
|
208
|
+
from tokensafe.burn_monitor import get_burn_warnings
|
|
209
209
|
burn_warn = get_burn_warnings(
|
|
210
210
|
agent="antigravity",
|
|
211
211
|
session_id=str(payload.get("conversationId") or ""),
|
|
@@ -219,8 +219,8 @@ def main():
|
|
|
219
219
|
# Compaction survival (the host has no PreCompact/PostCompact event, so the
|
|
220
220
|
# marker comes from the usage-ledger fallback on the previous Stop).
|
|
221
221
|
try:
|
|
222
|
-
from
|
|
223
|
-
from
|
|
222
|
+
from tokensafe.transcript_recall import remember_session
|
|
223
|
+
from tokensafe.compaction_recovery import consume_digest
|
|
224
224
|
_sid = str(payload.get("conversationId") or "")
|
|
225
225
|
_tp = str(payload.get("transcriptPath") or "")
|
|
226
226
|
remember_session(_sid, _tp, agent="antigravity", cwd=workspace_root)
|
package/hooks/stop_hook.py
CHANGED
|
@@ -34,7 +34,7 @@ BUDGET_NUDGE_REASON = (
|
|
|
34
34
|
|
|
35
35
|
def main():
|
|
36
36
|
try:
|
|
37
|
-
from
|
|
37
|
+
from tokensafe.stdio_io import read_stdin_text
|
|
38
38
|
raw_input = read_stdin_text()
|
|
39
39
|
if not raw_input.strip():
|
|
40
40
|
print(json.dumps({}))
|
|
@@ -53,12 +53,12 @@ def main():
|
|
|
53
53
|
response = {}
|
|
54
54
|
|
|
55
55
|
try:
|
|
56
|
-
from
|
|
56
|
+
from tokensafe.metrics import MetricsTracker
|
|
57
57
|
tracker = MetricsTracker()
|
|
58
58
|
|
|
59
59
|
# 1. Turn-end / idle epilogue: cloud sync + idle retention prune.
|
|
60
60
|
try:
|
|
61
|
-
from
|
|
61
|
+
from tokensafe.telemetry_sync import TelemetrySync
|
|
62
62
|
TelemetrySync.sync_background(throttle_seconds=0.0 if fully_idle else 15.0)
|
|
63
63
|
except Exception:
|
|
64
64
|
pass
|
package/hooks/zcode_hook.py
CHANGED
|
@@ -58,7 +58,7 @@ def handle(event: str, payload: dict) -> dict:
|
|
|
58
58
|
|
|
59
59
|
def main():
|
|
60
60
|
try:
|
|
61
|
-
from
|
|
61
|
+
from tokensafe.stdio_io import read_stdin_text
|
|
62
62
|
raw = read_stdin_text()
|
|
63
63
|
payload = json.loads(raw) if raw.strip() else {}
|
|
64
64
|
if not isinstance(payload, dict):
|
|
@@ -85,7 +85,7 @@ def main():
|
|
|
85
85
|
response = {}
|
|
86
86
|
if event in ("UserPromptSubmit", "PreToolUse", "PostToolUse", "Stop"):
|
|
87
87
|
try:
|
|
88
|
-
from
|
|
88
|
+
from tokensafe.telemetry_sync import TelemetrySync
|
|
89
89
|
TelemetrySync.sync_background(throttle_seconds=0.0 if event == "Stop" else 20.0)
|
|
90
90
|
except Exception:
|
|
91
91
|
pass
|
package/kimi.plugin.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.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",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"tokensafe": {
|
|
26
26
|
"command": "python3",
|
|
27
27
|
"args": [
|
|
28
|
-
"
|
|
28
|
+
"tokensafe/mcp_server.py"
|
|
29
29
|
]
|
|
30
30
|
}
|
|
31
31
|
},
|
package/mcp_config.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"mcpName": "dev.tokensafe/tokensafe",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.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": {
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
"bin/tokensafe-mcp",
|
|
16
16
|
"bin/agy-tokens",
|
|
17
17
|
"bin/python-runtime.json",
|
|
18
|
+
"tokensafe/",
|
|
19
|
+
"!tokensafe/cloud_backend.py",
|
|
20
|
+
"!tokensafe/mailer.py",
|
|
21
|
+
"!tokensafe/weekly_digest.py",
|
|
18
22
|
"src/",
|
|
19
|
-
"!src/cloud_backend.py",
|
|
20
|
-
"!src/mailer.py",
|
|
21
|
-
"!src/weekly_digest.py",
|
|
22
23
|
"!**/__pycache__/**",
|
|
23
24
|
"!hooks/__pycache__/**",
|
|
24
25
|
"!**/*.pyc",
|
package/plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokensafe",
|
|
3
3
|
"displayName": "TokenSafe",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.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": "
|
|
6
|
+
"version": "5.0.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": "
|
|
26
|
+
"version": "5.0.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": "
|
|
42
|
+
"version": "5.0.1",
|
|
43
43
|
"runtimeHint": "uvx",
|
|
44
44
|
"transport": {
|
|
45
45
|
"type": "stdio"
|
package/src/__init__.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
"""
|
|
2
|
-
TokenSafe: Local-first AI coding guardrails, context slicing, and team ROI telemetry for AI coding agents.
|
|
3
|
-
"""
|
|
1
|
+
"""Compatibility shim for the old ``src`` package name.
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
TokenSafe's Python package is ``tokensafe``. This directory holds only the
|
|
4
|
+
legacy launchers that agent configs written by older installs still point at
|
|
5
|
+
(``<install>/src/mcp_server.py``, ``python -m src.proxy``). It ships with the
|
|
6
|
+
npm package and git checkouts, never in the wheel: a top-level ``src`` in
|
|
7
|
+
site-packages collides with other distributions. Re-running
|
|
8
|
+
``tokensafe install`` (``tokensafe update`` does it automatically) rewrites
|
|
9
|
+
those configs to the new paths.
|
|
10
|
+
"""
|