@alexgreensh/token-optimizer 5.13.65 → 5.13.67
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/hooks/module_runner.py +62 -13
- package/hooks/userpromptsubmit_runner.py +13 -1
- package/package.json +1 -1
- package/skills/token-optimizer/examples/hooks-starter.json +0 -10
- package/skills/token-optimizer/scripts/_measure/__init__.py +16 -0
- package/skills/token-optimizer/scripts/_measure/_compat.py +200 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/README.md +9 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/__init__.py +23 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/LICENSE +21 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/__init__.py +8 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/_parser.py +770 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/_re.py +112 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/_types.py +10 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/py.typed +1 -0
- package/skills/token-optimizer/scripts/_measure/audit.py +6949 -0
- package/skills/token-optimizer/scripts/_measure/cache_policy.py +7050 -0
- package/skills/token-optimizer/scripts/_measure/cli.py +2722 -0
- package/skills/token-optimizer/scripts/_measure/continuity.py +4630 -0
- package/skills/token-optimizer/scripts/_measure/daemon.py +6773 -0
- package/skills/token-optimizer/scripts/_measure/dashboard.py +4358 -0
- package/skills/token-optimizer/scripts/_measure/lifecycle.py +6145 -0
- package/skills/token-optimizer/scripts/_measure/pricing.py +2101 -0
- package/skills/token-optimizer/scripts/_measure/quality_archive.py +7138 -0
- package/skills/token-optimizer/scripts/_measure/runtime.py +1231 -0
- package/skills/token-optimizer/scripts/_measure/savings.py +7702 -0
- package/skills/token-optimizer/scripts/_measure/sessions.py +4784 -0
- package/skills/token-optimizer/scripts/_measure/trends.py +2432 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +11 -3
- 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 +7 -8
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +2 -2
- package/skills/token-optimizer/scripts/codex_doctor.py +14 -0
- package/skills/token-optimizer/scripts/command_filters.py +630 -19
- package/skills/token-optimizer/scripts/devin_source.py +2 -1
- package/skills/token-optimizer/scripts/hermes_doctor.py +13 -0
- package/skills/token-optimizer/scripts/hermes_install.py +11 -0
- package/skills/token-optimizer/scripts/measure.py +2430 -60930
- 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
|
@@ -34,6 +34,7 @@ from typing import Any, Optional
|
|
|
34
34
|
from hook_io import read_stdin_hook_input
|
|
35
35
|
from plugin_env import is_v5_flag_enabled, resolve_snapshot_dir
|
|
36
36
|
from runtime_env import project_config_value, run_origin, runtime_home
|
|
37
|
+
from reset_epoch import _reset_replacement_state, refresh_message, snapshot_reset_epoch
|
|
37
38
|
|
|
38
39
|
try:
|
|
39
40
|
from session_store import SessionStore, cleanup_old_stores, mark_context_reset
|
|
@@ -599,16 +600,6 @@ def _decisions_log_path(session_id: str = "unknown") -> Path:
|
|
|
599
600
|
return decisions_dir / f"{safe_id}.jsonl"
|
|
600
601
|
|
|
601
602
|
|
|
602
|
-
|
|
603
|
-
def _reset_replacement_state(entry: dict[str, Any]) -> None:
|
|
604
|
-
entry["last_replacement_fingerprint"] = ""
|
|
605
|
-
entry["last_replacement_type"] = ""
|
|
606
|
-
entry["repeat_replacement_count"] = 0
|
|
607
|
-
entry["consecutive_denials"] = 0
|
|
608
|
-
entry["last_structure_reason"] = ""
|
|
609
|
-
entry["last_structure_confidence"] = 0.0
|
|
610
|
-
|
|
611
|
-
|
|
612
603
|
def _check_escape_hatch(
|
|
613
604
|
entry: dict[str, Any],
|
|
614
605
|
store: Any,
|
|
@@ -623,6 +614,7 @@ def _check_escape_hatch(
|
|
|
623
614
|
summary: Any,
|
|
624
615
|
save_hook_context_enabled: bool,
|
|
625
616
|
quiet: bool,
|
|
617
|
+
reset_epoch: Any = None,
|
|
626
618
|
) -> bool:
|
|
627
619
|
"""Allow the read if consecutive denials reach the threshold. Returns True if escaped."""
|
|
628
620
|
consecutive_denials = int(entry.get("consecutive_denials", 0) or 0) + 1
|
|
@@ -632,7 +624,7 @@ def _check_escape_hatch(
|
|
|
632
624
|
|
|
633
625
|
entry["consecutive_denials"] = 0
|
|
634
626
|
_reset_replacement_state(entry)
|
|
635
|
-
store.upsert_file_entry(file_path, entry)
|
|
627
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
636
628
|
_log_decision(
|
|
637
629
|
"allow",
|
|
638
630
|
file_path,
|
|
@@ -1506,6 +1498,9 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1506
1498
|
except Exception:
|
|
1507
1499
|
pass
|
|
1508
1500
|
|
|
1501
|
+
# U2: snapshot the reset epoch AFTER the compaction scan above; a reset
|
|
1502
|
+
# landing before the record write drops the guarded upsert (reset_epoch.py).
|
|
1503
|
+
reset_epoch = snapshot_reset_epoch(store)
|
|
1509
1504
|
try:
|
|
1510
1505
|
entry = store.get_file_entry(file_path)
|
|
1511
1506
|
except Exception:
|
|
@@ -1557,7 +1552,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1557
1552
|
except Exception:
|
|
1558
1553
|
pass
|
|
1559
1554
|
_reset_replacement_state(entry)
|
|
1560
|
-
store.upsert_file_entry(file_path, entry)
|
|
1555
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
1561
1556
|
# U5: first-read skeleton. Active cohorts serve a skeleton (returns True,
|
|
1562
1557
|
# we stop here); others fall through to serve the full file + shadow-log.
|
|
1563
1558
|
# Reuses content delta mode already read this turn (even when too large to
|
|
@@ -1748,7 +1743,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1748
1743
|
else:
|
|
1749
1744
|
entry["content_hash"] = None
|
|
1750
1745
|
store.delete_cached_content(file_path)
|
|
1751
|
-
store.upsert_file_entry(file_path, entry)
|
|
1746
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
1752
1747
|
|
|
1753
1748
|
net_saved = max(0, old_tokens - delta_tokens)
|
|
1754
1749
|
|
|
@@ -1858,7 +1853,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1858
1853
|
except Exception:
|
|
1859
1854
|
pass
|
|
1860
1855
|
_reset_replacement_state(entry)
|
|
1861
|
-
store.upsert_file_entry(file_path, entry)
|
|
1856
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
1862
1857
|
_log_decision(
|
|
1863
1858
|
"allow",
|
|
1864
1859
|
file_path,
|
|
@@ -1900,7 +1895,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1900
1895
|
decision = "warn" if mode == "warn" else "allow"
|
|
1901
1896
|
entry["last_structure_reason"] = reason_code
|
|
1902
1897
|
entry["last_structure_confidence"] = summary.confidence if summary else 0.0
|
|
1903
|
-
store.upsert_file_entry(file_path, entry)
|
|
1898
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
1904
1899
|
_log_decision(
|
|
1905
1900
|
decision,
|
|
1906
1901
|
file_path,
|
|
@@ -1929,7 +1924,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1929
1924
|
if eligible_structure and summary is not None:
|
|
1930
1925
|
if _check_escape_hatch(
|
|
1931
1926
|
entry, store, file_path, session_id, mode, offset, limit,
|
|
1932
|
-
tokens_est, language, True, summary, save_hook_context_enabled, quiet,
|
|
1927
|
+
tokens_est, language, True, summary, save_hook_context_enabled, quiet, reset_epoch,
|
|
1933
1928
|
):
|
|
1934
1929
|
return
|
|
1935
1930
|
|
|
@@ -1940,7 +1935,7 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
1940
1935
|
entry["repeat_replacement_count"] = repeat_count
|
|
1941
1936
|
entry["last_structure_reason"] = reason_code
|
|
1942
1937
|
entry["last_structure_confidence"] = summary.confidence
|
|
1943
|
-
store.upsert_file_entry(file_path, entry)
|
|
1938
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
1944
1939
|
|
|
1945
1940
|
if repeat_count == 1:
|
|
1946
1941
|
replacement_tokens_est = summary.replacement_tokens_est
|
|
@@ -2022,11 +2017,11 @@ def handle_read(hook_input: dict[str, Any], mode: str, quiet: bool) -> None:
|
|
|
2022
2017
|
if mode == "block":
|
|
2023
2018
|
if _check_escape_hatch(
|
|
2024
2019
|
entry, store, file_path, session_id, mode, offset, limit,
|
|
2025
|
-
tokens_est, language, False, summary, save_hook_context_enabled, quiet,
|
|
2020
|
+
tokens_est, language, False, summary, save_hook_context_enabled, quiet, reset_epoch,
|
|
2026
2021
|
):
|
|
2027
2022
|
return
|
|
2028
2023
|
|
|
2029
|
-
store.upsert_file_entry(file_path, entry)
|
|
2024
|
+
store.upsert_file_entry(file_path, entry, expected_reset_epoch=reset_epoch)
|
|
2030
2025
|
block_net_saved = max(0, tokens_est - REASON_ONLY_TOKENS_EST)
|
|
2031
2026
|
_log_decision(
|
|
2032
2027
|
"block",
|
|
@@ -2211,6 +2206,10 @@ def handle_invalidate(hook_input: dict[str, Any], quiet: bool) -> None:
|
|
|
2211
2206
|
if store is None:
|
|
2212
2207
|
return
|
|
2213
2208
|
|
|
2209
|
+
# Same U2 contract as handle_read: a reset landing before the rewrite below
|
|
2210
|
+
# must drop the write, not resurrect a pre-clear row.
|
|
2211
|
+
_invalidate_epoch = snapshot_reset_epoch(store)
|
|
2212
|
+
|
|
2214
2213
|
# Checkpoints can predate this edit. Preserve its path independently of
|
|
2215
2214
|
# cache entries, including edits to files that have never been read.
|
|
2216
2215
|
try:
|
|
@@ -2313,9 +2312,9 @@ def handle_invalidate(hook_input: dict[str, Any], quiet: bool) -> None:
|
|
|
2313
2312
|
existing["content_hash"] = None
|
|
2314
2313
|
store.delete_cached_content(file_path)
|
|
2315
2314
|
|
|
2316
|
-
store.upsert_file_entry(file_path, existing)
|
|
2315
|
+
written = store.upsert_file_entry(file_path, existing, expected_reset_epoch=_invalidate_epoch)
|
|
2317
2316
|
if not quiet:
|
|
2318
|
-
print(
|
|
2317
|
+
print(refresh_message(file_path, written), file=sys.stderr)
|
|
2319
2318
|
except Exception:
|
|
2320
2319
|
# Fallback: purge on any unexpected error so a stale entry is never
|
|
2321
2320
|
# served.
|
|
@@ -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
|
|