-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcodex_disk_cache.py
More file actions
290 lines (253 loc) · 10.3 KB
/
Copy pathcodex_disk_cache.py
File metadata and controls
290 lines (253 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 lollapalooza <https://github.com/aqua5230>
#
# Part of "usage". Free software licensed under the GNU Affero General Public
# License v3.0 only; see the LICENSE file for full terms and the warranty disclaimer.
"""Sharded disk persistence for codex_loader's parse caches."""
from __future__ import annotations
import contextlib
import hashlib
import json
import logging
import os
import tempfile
from collections import OrderedDict
from pathlib import Path
from typing import Any
from codex_events import _SessionFileInfo, _TokenUsage
from history_disk_cache import _deserialize_usage_entry, _serialize_usage_entry
logger = logging.getLogger(__name__)
__all__ = [
"_deserialize_usage_entry",
"_serialize_usage_entry",
"flush_caches",
"seed_caches",
]
_JsonlCache = OrderedDict[Path, Any]
_FileInfoCache = OrderedDict[Path, tuple[float, int, _SessionFileInfo]]
_SHARD_COUNT = 32
def _serialize_token_usage(value: _TokenUsage | None) -> dict[str, int] | None:
if value is None:
return None
return {
"input_tokens": value.input_tokens,
"output_tokens": value.output_tokens,
"cache_read_tokens": value.cache_read_tokens,
}
def _deserialize_token_usage(value: Any) -> _TokenUsage | None:
if not isinstance(value, dict):
return None
try:
return _TokenUsage(
input_tokens=int(value["input_tokens"]),
output_tokens=int(value["output_tokens"]),
cache_read_tokens=int(value["cache_read_tokens"]),
)
except (KeyError, TypeError, ValueError):
return None
def _cache_dir(cache_path: Path) -> Path:
return cache_path.with_suffix(f"{cache_path.suffix}.d")
def _shard_index(path: Path) -> int:
digest = hashlib.sha256(str(path).encode("utf-8", errors="surrogatepass")).digest()
return digest[0] % _SHARD_COUNT
def _shard_path(cache_path: Path, index: int) -> Path:
return _cache_dir(cache_path) / f"files-{index:02x}.json"
def _sqlite_log_path(cache_path: Path) -> Path:
return _cache_dir(cache_path) / "sqlite-log.json"
def _remove_legacy_cache(cache_path: Path) -> None:
with contextlib.suppress(OSError):
cache_path.unlink()
def _load_payload(path: Path, schema_version: int) -> dict[str, Any] | None:
try:
with path.open(encoding="utf-8") as file:
payload = json.load(file)
if not isinstance(payload, dict) or payload.get("schema_version") != schema_version:
path.unlink(missing_ok=True)
return None
return payload
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
with contextlib.suppress(OSError):
path.unlink()
return None
def _encoded_payload(payload: dict[str, Any]) -> bytes:
return json.dumps(
payload,
ensure_ascii=False,
separators=(",", ":"),
sort_keys=True,
).encode("utf-8")
def _write_if_changed(path: Path, payload: bytes) -> None:
try:
if path.read_bytes() == payload:
return
except OSError:
pass
tmp_path: str | None = None
try:
path.parent.mkdir(parents=True, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(dir=path.parent, suffix=".tmp")
with os.fdopen(fd, "wb") as file:
file.write(payload)
file.flush()
os.fsync(file.fileno())
os.replace(tmp_path, path)
tmp_path = None
finally:
if tmp_path:
with contextlib.suppress(OSError):
os.unlink(tmp_path)
def seed_caches(
cache_path: Path,
schema_version: int,
maxsize: int,
jsonl_cache: _JsonlCache,
file_info_cache: _FileInfoCache,
sqlite_log_cache: Any,
) -> None:
"""Seed valid shards, skipping only corrupt or stale shards."""
from codex_loader import _JsonlCacheEntry, _JsonlParseState
_remove_legacy_cache(cache_path)
sqlite_payload = _load_payload(_sqlite_log_path(cache_path), schema_version)
if sqlite_payload is not None:
try:
watermark = sqlite_payload["watermark"]
entries_data = sqlite_payload["entries"]
if (
isinstance(watermark, list)
and len(watermark) == 3
and isinstance(entries_data, list)
):
sqlite_log_cache.watermark = tuple(int(item) for item in watermark)
sqlite_log_cache.entries = [
_deserialize_usage_entry(entry) for entry in entries_data
]
except (KeyError, TypeError, ValueError):
pass
for index in range(_SHARD_COUNT):
payload = _load_payload(_shard_path(cache_path, index), schema_version)
if payload is None:
continue
files = payload.get("files")
if not isinstance(files, dict):
continue
for path_str, file_data in files.items():
if not isinstance(file_data, dict):
continue
try:
path = Path(path_str)
mtime = file_data["mtime"]
size = file_data["size"]
session_id = file_data["session_id"]
forked_from_id = file_data["forked_from_id"]
entries_data = file_data["entries"]
confirmed_offset = int(file_data.get("confirmed_offset", 0))
digest_hex = file_data.get("confirmed_prefix_digest", "")
parse_state_data = file_data.get("parse_state", {})
if not isinstance(parse_state_data, dict):
parse_state_data = {}
if len(file_info_cache) >= maxsize:
file_info_cache.popitem(last=False)
file_info_cache[path] = (
mtime,
size,
_SessionFileInfo(session_id=session_id, forked_from_id=forked_from_id),
)
if entries_data is None:
continue
if not isinstance(entries_data, list):
continue
if (
"confirmed_offset" not in file_data
or "confirmed_prefix_digest" not in file_data
or "parse_state" not in file_data
):
continue
state = _JsonlParseState(
session_timestamp=str(parse_state_data.get("session_timestamp", "")),
project=str(parse_state_data.get("project", "unknown")),
session_model=str(parse_state_data.get("session_model", "unknown")),
previous_usage=_deserialize_token_usage(parse_state_data.get("previous_usage")),
token_count_index=int(parse_state_data.get("token_count_index", 0)),
)
if len(jsonl_cache) >= maxsize:
jsonl_cache.popitem(last=False)
jsonl_cache[path] = _JsonlCacheEntry(
mtime=mtime,
size=size,
replay_cache_key=None,
entries=[_deserialize_usage_entry(entry) for entry in entries_data],
confirmed_offset=confirmed_offset,
confirmed_prefix_digest=bytes.fromhex(digest_hex)
if isinstance(digest_hex, str)
else b"",
state=state,
)
except (KeyError, TypeError, ValueError):
continue
def flush_caches(
cache_path: Path,
schema_version: int,
jsonl_cache: _JsonlCache,
file_info_cache: _FileInfoCache,
sqlite_log_cache: Any,
) -> None:
"""Atomically replace only shards whose serialized contents changed."""
try:
_remove_legacy_cache(cache_path)
shards: list[dict[str, Any]] = [{} for _ in range(_SHARD_COUNT)]
for path, entry in jsonl_cache.items():
info = file_info_cache.get(path)
if entry.replay_cache_key is not None:
if info is None:
continue
entries_data = None
session_id = info[2].session_id
forked_from_id = info[2].forked_from_id
else:
entries_data = [_serialize_usage_entry(item) for item in entry.entries]
if entry.entries:
session_id = entry.entries[0].session_id
forked_from_id = ""
elif info is not None:
session_id = info[2].session_id
forked_from_id = info[2].forked_from_id
else:
continue
shards[_shard_index(path)][str(path)] = {
"mtime": entry.mtime,
"size": entry.size,
"session_id": session_id,
"forked_from_id": forked_from_id,
"entries": entries_data,
"confirmed_offset": entry.confirmed_offset,
"confirmed_prefix_digest": entry.confirmed_prefix_digest.hex(),
"parse_state": {
"session_timestamp": entry.state.session_timestamp,
"project": entry.state.project,
"session_model": entry.state.session_model,
"previous_usage": _serialize_token_usage(entry.state.previous_usage),
"token_count_index": entry.state.token_count_index,
},
}
for index, files in enumerate(shards):
path = _shard_path(cache_path, index)
if files:
_write_if_changed(
path,
_encoded_payload({"schema_version": schema_version, "files": files}),
)
else:
path.unlink(missing_ok=True)
sqlite_payload = {
"schema_version": schema_version,
"watermark": list(sqlite_log_cache.watermark)
if sqlite_log_cache.watermark is not None
else None,
"entries": [
_serialize_usage_entry(entry) for entry in sqlite_log_cache.entries
],
}
_write_if_changed(_sqlite_log_path(cache_path), _encoded_payload(sqlite_payload))
except Exception as exc:
if os.environ.get("USAGE_DEBUG") == "1":
logger.warning("failed to write codex jsonl cache %s: %s", cache_path, exc)