@alexgreensh/token-optimizer 5.13.66 → 5.13.68
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 +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/hooks/hooks.json +0 -10
- package/package.json +1 -1
- package/skills/token-optimizer/examples/hooks-starter.json +0 -10
- package/skills/token-optimizer/scripts/_measure/_compat.py +2 -2
- package/skills/token-optimizer/scripts/_measure/_vendor/README.md +15 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/__init__.py +63 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/LICENSE +21 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/__init__.py +39 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/py.typed +1 -0
- package/skills/token-optimizer/scripts/_measure/audit.py +5 -21
- package/skills/token-optimizer/scripts/_measure/cache_policy.py +54 -1
- package/skills/token-optimizer/scripts/_measure/daemon.py +21 -51
- package/skills/token-optimizer/scripts/_measure/lifecycle.py +81 -327
- package/skills/token-optimizer/scripts/_tomli.py +916 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +11 -3
- package/skills/token-optimizer/scripts/antigravity_install.py +3 -0
- package/skills/token-optimizer/scripts/bash_compress.py +3 -3
- package/skills/token-optimizer/scripts/bash_compress_hook.py +6 -7
- package/skills/token-optimizer/scripts/cloud_kit.py +21 -21
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +2 -2
- package/skills/token-optimizer/scripts/codex_doctor.py +33 -7
- package/skills/token-optimizer/scripts/codex_hook_provenance.py +268 -0
- package/skills/token-optimizer/scripts/codex_install.py +33 -69
- package/skills/token-optimizer/scripts/command_filters.py +359 -18
- package/skills/token-optimizer/scripts/copilot_install.py +5 -0
- package/skills/token-optimizer/scripts/cursor_install.py +5 -0
- package/skills/token-optimizer/scripts/devin_install.py +6 -12
- package/skills/token-optimizer/scripts/devin_source.py +6 -5
- package/skills/token-optimizer/scripts/devin_verify.py +16 -0
- package/skills/token-optimizer/scripts/grok_install.py +5 -0
- package/skills/token-optimizer/scripts/heal_report.py +92 -0
- package/skills/token-optimizer/scripts/hook_heal.py +138 -0
- package/skills/token-optimizer/scripts/hook_provenance.py +878 -0
- package/skills/token-optimizer/scripts/measure.py +2 -2
- package/skills/token-optimizer/scripts/owned_hooks.py +420 -0
- package/skills/token-optimizer/scripts/read_cache.py +20 -21
- package/skills/token-optimizer/scripts/reset_epoch.py +177 -0
- package/skills/token-optimizer/scripts/session_store.py +11 -48
- package/skills/token-optimizer/scripts/shipped_codex_hook_templates.json +5815 -0
- package/skills/token-optimizer/scripts/shipped_hook_templates.py +252 -0
- package/skills/token-optimizer/scripts/shipped_starter_hooks.py +55 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Context-reset epoch guard shared by session_store and read_cache (U2).
|
|
2
|
+
|
|
3
|
+
A writer that judged a Read BEFORE a context reset (/clear, compaction, or the
|
|
4
|
+
subagent-ledger clear) but commits its ``file_reads`` upsert AFTER the reset's
|
|
5
|
+
transaction would resurrect a row claiming a file is in context when the model
|
|
6
|
+
no longer holds it. ``reset_context_state`` / ``clear_file_entries`` bump
|
|
7
|
+
``session_meta.reset_epoch`` inside their own transaction; guarded writers pass
|
|
8
|
+
the epoch they snapshotted and the check-and-insert runs in one IMMEDIATE
|
|
9
|
+
transaction. Kept in its own module so the already-over-limit session_store.py
|
|
10
|
+
and read_cache.py do not grow (scripts/check_file_size.py).
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import time
|
|
16
|
+
from typing import Any, Optional
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def bump_reset_epoch(conn) -> None:
|
|
20
|
+
"""Increment the reset epoch inside the caller's open transaction."""
|
|
21
|
+
conn.execute(
|
|
22
|
+
"INSERT INTO session_meta (key, value) VALUES ('reset_epoch', '1') "
|
|
23
|
+
"ON CONFLICT(key) DO UPDATE SET "
|
|
24
|
+
"value = CAST(CAST(value AS INTEGER) + 1 AS TEXT)"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def snapshot_reset_epoch(store: Any) -> Any:
|
|
29
|
+
"""Epoch to hand to guarded writes, or None (legacy unconditional write).
|
|
30
|
+
|
|
31
|
+
Call AFTER any compaction scan that may itself reset the session. Never
|
|
32
|
+
raises: a store that cannot report an epoch degrades to the legacy write.
|
|
33
|
+
"""
|
|
34
|
+
try:
|
|
35
|
+
return store.get_reset_epoch()
|
|
36
|
+
except Exception:
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _reset_replacement_state(entry: dict[str, Any]) -> None:
|
|
41
|
+
entry["last_replacement_fingerprint"] = ""
|
|
42
|
+
entry["last_replacement_type"] = ""
|
|
43
|
+
entry["repeat_replacement_count"] = 0
|
|
44
|
+
entry["consecutive_denials"] = 0
|
|
45
|
+
entry["last_structure_reason"] = ""
|
|
46
|
+
entry["last_structure_confidence"] = 0.0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ResetEpochMixin:
|
|
50
|
+
"""SessionStore methods that read or honor the reset epoch.
|
|
51
|
+
|
|
52
|
+
Requires ``self._connect()`` and ``self.get_meta()`` from SessionStore.
|
|
53
|
+
Public signatures are unchanged (``upsert_file_entry`` gains the optional
|
|
54
|
+
``expected_reset_epoch``).
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def get_reset_epoch(self) -> int:
|
|
58
|
+
"""Current context-reset epoch (0 before any reset). U2 race guard."""
|
|
59
|
+
v = self.get_meta("reset_epoch")
|
|
60
|
+
try:
|
|
61
|
+
return int(v) if v is not None else 0
|
|
62
|
+
except (TypeError, ValueError):
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
def upsert_file_entry(
|
|
66
|
+
self,
|
|
67
|
+
file_path: str,
|
|
68
|
+
entry: dict[str, Any],
|
|
69
|
+
expected_reset_epoch: Optional[int] = None,
|
|
70
|
+
) -> bool:
|
|
71
|
+
"""Record a file read; returns False when the write was dropped.
|
|
72
|
+
|
|
73
|
+
When ``expected_reset_epoch`` is given, the write is conditional: if a
|
|
74
|
+
context reset (/clear, compaction, subagent ledger clear) committed
|
|
75
|
+
after the caller snapshotted the epoch at judgment time, the write is
|
|
76
|
+
dropped instead of resurrecting a pre-clear ``file_reads`` row. A
|
|
77
|
+
resurrected row would make the next Read of a file the model no longer
|
|
78
|
+
holds be judged "unchanged and already in context" and denied or
|
|
79
|
+
outline-served -- silent context loss (U2). The check and the insert
|
|
80
|
+
run in one IMMEDIATE transaction so the reset cannot slip between them.
|
|
81
|
+
``None`` keeps the legacy unconditional write.
|
|
82
|
+
"""
|
|
83
|
+
conn = self._connect()
|
|
84
|
+
if expected_reset_epoch is None:
|
|
85
|
+
self._insert_file_entry(conn, file_path, entry)
|
|
86
|
+
conn.commit()
|
|
87
|
+
return True
|
|
88
|
+
try:
|
|
89
|
+
conn.execute("BEGIN IMMEDIATE")
|
|
90
|
+
row = conn.execute(
|
|
91
|
+
"SELECT value FROM session_meta WHERE key = 'reset_epoch'"
|
|
92
|
+
).fetchone()
|
|
93
|
+
# A corrupt stored epoch must DROP, never be normalized to 0:
|
|
94
|
+
# "no reset since the corruption" and "a real reset landed, THEN the
|
|
95
|
+
# value was corrupted" are indistinguishable from the value alone,
|
|
96
|
+
# and normalizing to 0 would accept a stale epoch-0 writer in the
|
|
97
|
+
# second case -- resurrecting the pre-reset row this guard exists
|
|
98
|
+
# to drop. Dropping is fail-open (the read/edit proceeds; only the
|
|
99
|
+
# ledger write is skipped), and the next reset bump CASTs the value
|
|
100
|
+
# to a clean integer, re-engaging the guard. An unparseable
|
|
101
|
+
# expected epoch drops too: a writer that cannot state its epoch
|
|
102
|
+
# has no business writing conditionally.
|
|
103
|
+
try:
|
|
104
|
+
current = int(row[0]) if row and row[0] is not None else 0
|
|
105
|
+
expected = int(expected_reset_epoch)
|
|
106
|
+
except (TypeError, ValueError):
|
|
107
|
+
conn.rollback()
|
|
108
|
+
return False
|
|
109
|
+
if current != expected:
|
|
110
|
+
conn.rollback()
|
|
111
|
+
return False
|
|
112
|
+
self._insert_file_entry(conn, file_path, entry)
|
|
113
|
+
conn.commit()
|
|
114
|
+
return True
|
|
115
|
+
except Exception:
|
|
116
|
+
try:
|
|
117
|
+
conn.rollback()
|
|
118
|
+
except Exception:
|
|
119
|
+
pass
|
|
120
|
+
raise
|
|
121
|
+
|
|
122
|
+
def _insert_file_entry(self, conn, file_path: str, entry: dict[str, Any]) -> None:
|
|
123
|
+
ranges_json = json.dumps(entry.get("ranges_seen", []))
|
|
124
|
+
conn.execute(
|
|
125
|
+
"""INSERT INTO file_reads
|
|
126
|
+
(file_path, mtime_ns, size_bytes, ranges_seen, tokens_est,
|
|
127
|
+
read_count, content_hash, last_access,
|
|
128
|
+
last_replacement_fingerprint, last_replacement_type,
|
|
129
|
+
repeat_replacement_count, last_structure_reason,
|
|
130
|
+
last_structure_confidence, last_tool_use_id)
|
|
131
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
132
|
+
ON CONFLICT(file_path) DO UPDATE SET
|
|
133
|
+
mtime_ns=excluded.mtime_ns,
|
|
134
|
+
size_bytes=excluded.size_bytes,
|
|
135
|
+
ranges_seen=excluded.ranges_seen,
|
|
136
|
+
tokens_est=excluded.tokens_est,
|
|
137
|
+
read_count=excluded.read_count,
|
|
138
|
+
content_hash=excluded.content_hash,
|
|
139
|
+
last_access=excluded.last_access,
|
|
140
|
+
last_replacement_fingerprint=excluded.last_replacement_fingerprint,
|
|
141
|
+
last_replacement_type=excluded.last_replacement_type,
|
|
142
|
+
repeat_replacement_count=excluded.repeat_replacement_count,
|
|
143
|
+
last_structure_reason=excluded.last_structure_reason,
|
|
144
|
+
last_structure_confidence=excluded.last_structure_confidence,
|
|
145
|
+
last_tool_use_id=excluded.last_tool_use_id
|
|
146
|
+
""",
|
|
147
|
+
(
|
|
148
|
+
file_path,
|
|
149
|
+
int(entry.get("mtime_ns", 0)),
|
|
150
|
+
int(entry.get("size_bytes", 0)),
|
|
151
|
+
ranges_json,
|
|
152
|
+
int(entry.get("tokens_est", 0)),
|
|
153
|
+
int(entry.get("read_count", 1)),
|
|
154
|
+
entry.get("content_hash"),
|
|
155
|
+
float(entry.get("last_access", time.time())),
|
|
156
|
+
entry.get("last_replacement_fingerprint", ""),
|
|
157
|
+
entry.get("last_replacement_type", ""),
|
|
158
|
+
int(entry.get("repeat_replacement_count", 0)),
|
|
159
|
+
entry.get("last_structure_reason", ""),
|
|
160
|
+
float(entry.get("last_structure_confidence", 0.0)),
|
|
161
|
+
entry.get("last_tool_use_id", ""),
|
|
162
|
+
),
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def refresh_message(file_path: str, written: Any) -> str:
|
|
167
|
+
"""Stderr line for a post-edit cache refresh, truthful about the guarded write.
|
|
168
|
+
|
|
169
|
+
``upsert_file_entry`` returns False only when the reset-epoch guard dropped
|
|
170
|
+
the write: a context reset committed after the snapshot, or the stored or
|
|
171
|
+
expected epoch was unreadable. Both mean the ledger row was NOT refreshed.
|
|
172
|
+
Any other value (True, or None from a legacy store) keeps the old wording.
|
|
173
|
+
"""
|
|
174
|
+
if written is False:
|
|
175
|
+
return (f"[Read Cache] Refresh skipped (reset-epoch guard dropped the write: "
|
|
176
|
+
f"context reset or unreadable epoch since the edit): {file_path}")
|
|
177
|
+
return f"[Read Cache] Refreshed after edit: {file_path}"
|
|
@@ -34,6 +34,7 @@ from pathlib import Path
|
|
|
34
34
|
from typing import Any, Optional
|
|
35
35
|
|
|
36
36
|
from plugin_env import resolve_snapshot_dir
|
|
37
|
+
from reset_epoch import ResetEpochMixin, bump_reset_epoch
|
|
37
38
|
|
|
38
39
|
SNAPSHOT_DIR = resolve_snapshot_dir()
|
|
39
40
|
SESSION_STORE_DIR = SNAPSHOT_DIR / "session-store"
|
|
@@ -191,7 +192,7 @@ def _bound_tool_output_index(text: Optional[str]):
|
|
|
191
192
|
return head + _INDEX_TRUNCATED_MARKER + tail, 1
|
|
192
193
|
|
|
193
194
|
|
|
194
|
-
class SessionStore:
|
|
195
|
+
class SessionStore(ResetEpochMixin):
|
|
195
196
|
"""Per-session SQLite store for tool output caching."""
|
|
196
197
|
|
|
197
198
|
def __init__(
|
|
@@ -542,51 +543,6 @@ class SessionStore:
|
|
|
542
543
|
entry["ranges_seen"] = []
|
|
543
544
|
return entry
|
|
544
545
|
|
|
545
|
-
def upsert_file_entry(self, file_path: str, entry: dict[str, Any]) -> None:
|
|
546
|
-
conn = self._connect()
|
|
547
|
-
ranges_json = json.dumps(entry.get("ranges_seen", []))
|
|
548
|
-
conn.execute(
|
|
549
|
-
"""INSERT INTO file_reads
|
|
550
|
-
(file_path, mtime_ns, size_bytes, ranges_seen, tokens_est,
|
|
551
|
-
read_count, content_hash, last_access,
|
|
552
|
-
last_replacement_fingerprint, last_replacement_type,
|
|
553
|
-
repeat_replacement_count, last_structure_reason,
|
|
554
|
-
last_structure_confidence, last_tool_use_id)
|
|
555
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
556
|
-
ON CONFLICT(file_path) DO UPDATE SET
|
|
557
|
-
mtime_ns=excluded.mtime_ns,
|
|
558
|
-
size_bytes=excluded.size_bytes,
|
|
559
|
-
ranges_seen=excluded.ranges_seen,
|
|
560
|
-
tokens_est=excluded.tokens_est,
|
|
561
|
-
read_count=excluded.read_count,
|
|
562
|
-
content_hash=excluded.content_hash,
|
|
563
|
-
last_access=excluded.last_access,
|
|
564
|
-
last_replacement_fingerprint=excluded.last_replacement_fingerprint,
|
|
565
|
-
last_replacement_type=excluded.last_replacement_type,
|
|
566
|
-
repeat_replacement_count=excluded.repeat_replacement_count,
|
|
567
|
-
last_structure_reason=excluded.last_structure_reason,
|
|
568
|
-
last_structure_confidence=excluded.last_structure_confidence,
|
|
569
|
-
last_tool_use_id=excluded.last_tool_use_id
|
|
570
|
-
""",
|
|
571
|
-
(
|
|
572
|
-
file_path,
|
|
573
|
-
int(entry.get("mtime_ns", 0)),
|
|
574
|
-
int(entry.get("size_bytes", 0)),
|
|
575
|
-
ranges_json,
|
|
576
|
-
int(entry.get("tokens_est", 0)),
|
|
577
|
-
int(entry.get("read_count", 1)),
|
|
578
|
-
entry.get("content_hash"),
|
|
579
|
-
float(entry.get("last_access", time.time())),
|
|
580
|
-
entry.get("last_replacement_fingerprint", ""),
|
|
581
|
-
entry.get("last_replacement_type", ""),
|
|
582
|
-
int(entry.get("repeat_replacement_count", 0)),
|
|
583
|
-
entry.get("last_structure_reason", ""),
|
|
584
|
-
float(entry.get("last_structure_confidence", 0.0)),
|
|
585
|
-
entry.get("last_tool_use_id", ""),
|
|
586
|
-
),
|
|
587
|
-
)
|
|
588
|
-
conn.commit()
|
|
589
|
-
|
|
590
546
|
def delete_file_entry(self, file_path: str) -> None:
|
|
591
547
|
conn = self._connect()
|
|
592
548
|
conn.execute("DELETE FROM file_reads WHERE file_path = ?", (file_path,))
|
|
@@ -608,8 +564,11 @@ class SessionStore:
|
|
|
608
564
|
|
|
609
565
|
def clear_file_entries(self) -> None:
|
|
610
566
|
conn = self._connect()
|
|
611
|
-
conn
|
|
612
|
-
|
|
567
|
+
with conn:
|
|
568
|
+
conn.execute("DELETE FROM file_reads")
|
|
569
|
+
# U2: same epoch contract as reset_context_state for the
|
|
570
|
+
# subagent-ledger reset path.
|
|
571
|
+
bump_reset_epoch(conn)
|
|
613
572
|
|
|
614
573
|
def reset_context_state(self) -> None:
|
|
615
574
|
"""Forget everything that records what the model currently holds.
|
|
@@ -635,6 +594,10 @@ class SessionStore:
|
|
|
635
594
|
"DELETE FROM session_meta WHERE key GLOB 'shadow_fr:*' "
|
|
636
595
|
"OR key GLOB 'active_fr:*'"
|
|
637
596
|
)
|
|
597
|
+
# U2: bump the reset epoch in the same transaction so a writer
|
|
598
|
+
# that judged pre-reset state can detect the reset landed before
|
|
599
|
+
# its record write (see upsert_file_entry's expected_reset_epoch).
|
|
600
|
+
bump_reset_epoch(conn)
|
|
638
601
|
|
|
639
602
|
# ----- cached_content -----
|
|
640
603
|
|