tokensafe 4.5.0 → 4.5.2
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 +1 -1
- package/bin/agy-tokens +10 -1
- package/bin/tokensafe.js +2 -1
- package/hooks/claude_code_hook.py +38 -126
- package/hooks/cursor_hook.py +45 -1
- package/hooks/kimi_hook.py +46 -90
- package/hooks/pre_invocation.py +23 -4
- package/hooks/pre_tool_use.py +40 -22
- package/hooks/session_start.py +28 -10
- package/hooks/zcode_hook.py +95 -0
- package/hooks.json +1 -1
- package/kimi.plugin.json +17 -1
- package/mcp_config.json +10 -4
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/rules/AGENTS.md +24 -24
- package/server.json +3 -3
- package/skills/token-optimizer/SKILL.md +61 -86
- package/skills/token-optimizer/references/mcp-tools.md +32 -132
- package/src/adapter_core.py +231 -7
- package/src/batch_editor.py +73 -22
- package/src/batch_search.py +31 -11
- package/src/burn_monitor.py +581 -0
- package/src/cache_normalizer.py +2 -0
- package/src/cli.py +34 -4
- package/src/compactor.py +1 -1
- package/src/dashboard.py +17 -9
- package/src/diff_slicer.py +1 -1
- package/src/distiller.py +11 -1
- package/src/environment_auditor.py +30 -6
- package/src/format_parsers/ansi.py +1 -1
- package/src/format_parsers/lockfiles.py +22 -17
- package/src/format_parsers/tabular.py +6 -3
- package/src/fts_index.py +26 -20
- package/src/governor.py +2 -2
- package/src/graph_engine.py +31 -9
- package/src/guard.py +3 -3
- package/src/image_downscale.py +26 -0
- package/src/installer.py +61 -48
- package/src/license_manager.py +49 -16
- package/src/live_relay.py +9 -8
- package/src/mcp_server.py +487 -48
- package/src/metrics.py +78 -13
- package/src/mistakes.py +4 -2
- package/src/paths.py +4 -1
- package/src/proxy.py +302 -123
- package/src/result_ledger.py +39 -1
- package/src/schema_slicer.py +40 -18
- package/src/scoped_search.py +66 -23
- package/src/skeletonizer.py +22 -11
- package/src/slicer.py +6 -4
- package/src/stash.py +37 -7
- package/src/superseded.py +42 -23
- package/src/syntax_guard.py +2 -0
- package/src/team_cache.py +3 -0
- package/src/telemetry_sync.py +96 -54
- package/src/thinking_fold.py +50 -12
- package/src/transcript_analyzer.py +270 -68
- package/src/treesitter_backend.py +2 -0
- package/src/type_slicer.py +60 -18
- package/src/version_checker.py +1 -1
- package/src/web_reader.py +55 -22
package/src/metrics.py
CHANGED
|
@@ -150,17 +150,34 @@ class MetricsTracker:
|
|
|
150
150
|
user_ver = int(uv_row[0]) if uv_row else 0
|
|
151
151
|
except Exception:
|
|
152
152
|
user_ver = 0
|
|
153
|
+
if user_ver >= 4:
|
|
154
|
+
_INITIALIZED_METRICS_DBS.add(self.db_path)
|
|
155
|
+
return
|
|
153
156
|
if user_ver >= 2:
|
|
154
157
|
tbl = cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='events';").fetchone()
|
|
155
158
|
if tbl is not None:
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
159
|
+
if user_ver < 3:
|
|
160
|
+
cursor.execute(
|
|
161
|
+
"UPDATE events SET measurement = 'estimated' "
|
|
162
|
+
"WHERE measurement = 'measured' AND event_type IN "
|
|
163
|
+
"('GRAPH_TOPOLOGY', 'HISTORY_FOLD', 'PROMPT', 'ROLLUP_LIFETIME', "
|
|
164
|
+
"'COMMAND_FILTER', 'LOOP_GUARD', 'SCOPED_SEARCH', 'DIR_LIST', "
|
|
165
|
+
"'GUARD_SECRET', 'GUARD_DESTRUCTIVE', 'GUARD_TLS_VERIFY_BYPASS', "
|
|
166
|
+
"'GUARD_JUNK_READ', 'GUARD_INFINITE', 'GUARD_RAW_CODE_SEARCH');"
|
|
167
|
+
)
|
|
168
|
+
for alter_sql in (
|
|
169
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_orig_tokens INTEGER NOT NULL DEFAULT 0;",
|
|
170
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_saved_tokens INTEGER NOT NULL DEFAULT 0;",
|
|
171
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_usd REAL NOT NULL DEFAULT 0.0;",
|
|
172
|
+
):
|
|
173
|
+
try:
|
|
174
|
+
cursor.execute(alter_sql)
|
|
175
|
+
except sqlite3.OperationalError:
|
|
176
|
+
pass
|
|
177
|
+
try:
|
|
178
|
+
cursor.execute("PRAGMA user_version = 4;")
|
|
179
|
+
except sqlite3.Error:
|
|
180
|
+
pass
|
|
164
181
|
_INITIALIZED_METRICS_DBS.add(self.db_path)
|
|
165
182
|
return
|
|
166
183
|
try:
|
|
@@ -219,9 +236,21 @@ class MetricsTracker:
|
|
|
219
236
|
total_optimized_tokens INTEGER DEFAULT 0,
|
|
220
237
|
total_saved_tokens INTEGER DEFAULT 0,
|
|
221
238
|
total_usd_saved REAL DEFAULT 0.0,
|
|
222
|
-
breakdown TEXT DEFAULT '{}'
|
|
239
|
+
breakdown TEXT DEFAULT '{}',
|
|
240
|
+
pruned_synced_orig_tokens INTEGER NOT NULL DEFAULT 0,
|
|
241
|
+
pruned_synced_saved_tokens INTEGER NOT NULL DEFAULT 0,
|
|
242
|
+
pruned_synced_usd REAL NOT NULL DEFAULT 0.0
|
|
223
243
|
)
|
|
224
244
|
""")
|
|
245
|
+
for alter_sql in (
|
|
246
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_orig_tokens INTEGER NOT NULL DEFAULT 0;",
|
|
247
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_saved_tokens INTEGER NOT NULL DEFAULT 0;",
|
|
248
|
+
"ALTER TABLE lifetime_aggregates ADD COLUMN pruned_synced_usd REAL NOT NULL DEFAULT 0.0;",
|
|
249
|
+
):
|
|
250
|
+
try:
|
|
251
|
+
cursor.execute(alter_sql)
|
|
252
|
+
except sqlite3.OperationalError:
|
|
253
|
+
pass
|
|
225
254
|
cursor.execute("""
|
|
226
255
|
INSERT OR IGNORE INTO lifetime_aggregates (
|
|
227
256
|
id, total_events, total_original_tokens, total_optimized_tokens,
|
|
@@ -250,7 +279,7 @@ class MetricsTracker:
|
|
|
250
279
|
GROUP BY substr(timestamp, 1, 10)
|
|
251
280
|
""", (cutoff_30,))
|
|
252
281
|
try:
|
|
253
|
-
cursor.execute("PRAGMA user_version =
|
|
282
|
+
cursor.execute("PRAGMA user_version = 4;")
|
|
254
283
|
except sqlite3.Error:
|
|
255
284
|
pass
|
|
256
285
|
_INITIALIZED_METRICS_DBS.add(self.db_path)
|
|
@@ -829,6 +858,8 @@ class MetricsTracker:
|
|
|
829
858
|
combined_where = " OR ".join(where_clauses)
|
|
830
859
|
|
|
831
860
|
with self._get_connection() as conn:
|
|
861
|
+
conn.commit()
|
|
862
|
+
conn.execute("BEGIN IMMEDIATE")
|
|
832
863
|
cursor = conn.cursor()
|
|
833
864
|
|
|
834
865
|
# 1. Calculate aggregates for events to be pruned
|
|
@@ -874,6 +905,33 @@ class MetricsTracker:
|
|
|
874
905
|
current_breakdown[ev_type]["saved"] += vals["saved"]
|
|
875
906
|
current_breakdown[ev_type]["estimated_saved"] += vals["estimated_saved"]
|
|
876
907
|
|
|
908
|
+
# Track how much of the pruned mass was already synced individually to Cloud
|
|
909
|
+
last_synced_id = 0
|
|
910
|
+
sync_state_path = os.path.join(os.path.dirname(self.db_path), "sync_state.json")
|
|
911
|
+
if os.path.exists(sync_state_path):
|
|
912
|
+
try:
|
|
913
|
+
with open(sync_state_path, "r", encoding="utf-8") as sf:
|
|
914
|
+
sdata = json.load(sf)
|
|
915
|
+
if isinstance(sdata, dict):
|
|
916
|
+
last_synced_id = int(sdata.get("last_synced_event_id", 0) or 0)
|
|
917
|
+
except Exception:
|
|
918
|
+
last_synced_id = 0
|
|
919
|
+
synced_orig, synced_saved, synced_usd = 0, 0, 0.0
|
|
920
|
+
if last_synced_id > 0:
|
|
921
|
+
cursor.execute(f"""
|
|
922
|
+
SELECT
|
|
923
|
+
COALESCE(SUM(original_tokens), 0),
|
|
924
|
+
COALESCE(SUM(saved_tokens), 0),
|
|
925
|
+
COALESCE(SUM(est_usd_saved), 0.0)
|
|
926
|
+
FROM events
|
|
927
|
+
WHERE ({combined_where}) AND id <= ?
|
|
928
|
+
""", [*params, last_synced_id])
|
|
929
|
+
s_row = cursor.fetchone()
|
|
930
|
+
if s_row:
|
|
931
|
+
synced_orig = int(s_row[0] or 0)
|
|
932
|
+
synced_saved = int(s_row[1] or 0)
|
|
933
|
+
synced_usd = float(s_row[2] or 0.0)
|
|
934
|
+
|
|
877
935
|
# 4. Update lifetime_aggregates
|
|
878
936
|
cursor.execute("""
|
|
879
937
|
UPDATE lifetime_aggregates SET
|
|
@@ -882,12 +940,16 @@ class MetricsTracker:
|
|
|
882
940
|
total_optimized_tokens = total_optimized_tokens + ?,
|
|
883
941
|
total_saved_tokens = total_saved_tokens + ?,
|
|
884
942
|
total_usd_saved = total_usd_saved + ?,
|
|
885
|
-
breakdown =
|
|
943
|
+
breakdown = ?,
|
|
944
|
+
pruned_synced_orig_tokens = COALESCE(pruned_synced_orig_tokens, 0) + ?,
|
|
945
|
+
pruned_synced_saved_tokens = COALESCE(pruned_synced_saved_tokens, 0) + ?,
|
|
946
|
+
pruned_synced_usd = COALESCE(pruned_synced_usd, 0.0) + ?
|
|
886
947
|
WHERE id = 1
|
|
887
948
|
""", (
|
|
888
949
|
agg_row["cnt"], agg_row["orig"], agg_row["opt"],
|
|
889
950
|
agg_row["saved"], agg_row["usd"],
|
|
890
|
-
json.dumps(current_breakdown, ensure_ascii=False)
|
|
951
|
+
json.dumps(current_breakdown, ensure_ascii=False),
|
|
952
|
+
synced_orig, synced_saved, synced_usd,
|
|
891
953
|
))
|
|
892
954
|
|
|
893
955
|
# 5. Delete pruned events
|
|
@@ -935,7 +997,10 @@ class MetricsTracker:
|
|
|
935
997
|
total_optimized_tokens = 0,
|
|
936
998
|
total_saved_tokens = 0,
|
|
937
999
|
total_usd_saved = 0.0,
|
|
938
|
-
breakdown = '{}'
|
|
1000
|
+
breakdown = '{}',
|
|
1001
|
+
pruned_synced_orig_tokens = 0,
|
|
1002
|
+
pruned_synced_saved_tokens = 0,
|
|
1003
|
+
pruned_synced_usd = 0.0
|
|
939
1004
|
WHERE id = 1
|
|
940
1005
|
""")
|
|
941
1006
|
self.vacuum()
|
package/src/mistakes.py
CHANGED
|
@@ -200,7 +200,7 @@ class MistakesLedger:
|
|
|
200
200
|
"""Retrieves scrubbed dead-end summaries shared across team members with 300s TTL cache."""
|
|
201
201
|
now = time.time()
|
|
202
202
|
with cls._team_mistakes_lock:
|
|
203
|
-
if cls.
|
|
203
|
+
if cls._team_mistakes_fetched_at > 0 and (now - cls._team_mistakes_fetched_at < 300.0):
|
|
204
204
|
return list(cls._team_mistakes_cache)
|
|
205
205
|
|
|
206
206
|
try:
|
|
@@ -226,7 +226,9 @@ class MistakesLedger:
|
|
|
226
226
|
cls._team_mistakes_fetched_at = now
|
|
227
227
|
return list(parsed)
|
|
228
228
|
except Exception:
|
|
229
|
-
|
|
229
|
+
with cls._team_mistakes_lock:
|
|
230
|
+
cls._team_mistakes_fetched_at = now
|
|
231
|
+
return list(cls._team_mistakes_cache)
|
|
230
232
|
|
|
231
233
|
@classmethod
|
|
232
234
|
def should_force_ask(
|
package/src/paths.py
CHANGED
|
@@ -134,11 +134,14 @@ def save_active_workspace(path: str) -> None:
|
|
|
134
134
|
if is_plugin_or_system_dir(path):
|
|
135
135
|
return
|
|
136
136
|
try:
|
|
137
|
+
import threading
|
|
137
138
|
data_dir = resolve_data_dir()
|
|
138
139
|
os.makedirs(data_dir, exist_ok=True)
|
|
139
140
|
marker = os.path.join(data_dir, ".active_workspace")
|
|
140
|
-
|
|
141
|
+
tmp_marker = f"{marker}.tmp.{os.getpid()}.{threading.get_ident()}"
|
|
142
|
+
with open(tmp_marker, "w", encoding="utf-8") as f:
|
|
141
143
|
f.write(os.path.abspath(path))
|
|
144
|
+
os.replace(tmp_marker, marker)
|
|
142
145
|
except Exception:
|
|
143
146
|
pass
|
|
144
147
|
|