-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcodex_disk_cache.py
More file actions
190 lines (166 loc) · 6.69 KB
/
Copy pathcodex_disk_cache.py
File metadata and controls
190 lines (166 loc) · 6.69 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
# 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.
"""Disk persistence for codex_loader's in-memory JSONL parse caches.
The caches themselves, the on-disk path, the schema constant and the
seeded-once flag all live in codex_loader (tests monkeypatch them there);
callers pass everything in and these functions mutate the given caches
in place.
"""
from __future__ import annotations
import contextlib
import json
import logging
import os
import tempfile
from collections import OrderedDict
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from codex_events import _SessionFileInfo
from codex_fork_replay import _ReplayCacheKey
from history_loader import UsageEntry
logger = logging.getLogger(__name__)
_JsonlCache = OrderedDict[Path, tuple[float, int, _ReplayCacheKey, list[UsageEntry]]]
_FileInfoCache = OrderedDict[Path, tuple[float, int, _SessionFileInfo]]
def _serialize_usage_entry(entry: UsageEntry) -> dict[str, Any]:
"""Serialize UsageEntry to dict for JSON storage."""
return {
"timestamp": entry.timestamp.isoformat(),
"session_id": entry.session_id,
"message_id": entry.message_id,
"request_id": entry.request_id,
"model": entry.model,
"input_tokens": entry.input_tokens,
"output_tokens": entry.output_tokens,
"cache_creation_tokens": entry.cache_creation_tokens,
"cache_read_tokens": entry.cache_read_tokens,
"cost_usd": entry.cost_usd,
"project": entry.project,
}
def _deserialize_usage_entry(data: dict[str, Any]) -> UsageEntry:
"""Deserialize dict from JSON back to UsageEntry."""
return UsageEntry(
timestamp=datetime.fromisoformat(data["timestamp"]),
session_id=data["session_id"],
message_id=data["message_id"],
request_id=data["request_id"],
model=data["model"],
input_tokens=data["input_tokens"],
output_tokens=data["output_tokens"],
cache_creation_tokens=data["cache_creation_tokens"],
cache_read_tokens=data["cache_read_tokens"],
cost_usd=data["cost_usd"],
project=data["project"],
)
def seed_caches(
cache_path: Path,
schema_version: int,
maxsize: int,
jsonl_cache: _JsonlCache,
file_info_cache: _FileInfoCache,
) -> None:
"""Seed the given in-memory caches from disk. Silently fails on any error."""
try:
with cache_path.open(encoding="utf-8") as f:
cache = json.load(f)
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
return
if not isinstance(cache, dict):
return
if cache.get("schema_version") != schema_version:
return
files = cache.get("files")
if not isinstance(files, dict):
return
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"]
# Seed file_info_cache
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),
)
# Seed jsonl_cache only for non-fork files (entries not null)
if entries_data is not None:
if not isinstance(entries_data, list):
continue
entries = [_deserialize_usage_entry(e) for e in entries_data]
if len(jsonl_cache) >= maxsize:
jsonl_cache.popitem(last=False)
# Non-fork files have replay_cache_key = None
jsonl_cache[path] = (mtime, size, None, entries)
except (KeyError, TypeError, ValueError):
# Skip malformed entry; continue with others
continue
def flush_caches(
cache_path: Path,
schema_version: int,
jsonl_cache: _JsonlCache,
file_info_cache: _FileInfoCache,
) -> None:
"""Atomically write the given in-memory caches to disk."""
tmp_path: str | None = None
try:
cache_path.parent.mkdir(parents=True, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(dir=cache_path.parent, suffix=".tmp")
files: dict[str, Any] = {}
# Write jsonl_cache entries
for path, (mtime, size, replay_cache_key, entries) in jsonl_cache.items():
path_str = str(path)
# For fork files (replay_cache_key is not None), entries = null
if replay_cache_key is not None:
entries_data = None
# Still need session_id/forked_from_id from file_info_cache
info = file_info_cache.get(path)
if info is None:
continue
session_id = info[2].session_id
forked_from_id = info[2].forked_from_id
else:
entries_data = [_serialize_usage_entry(e) for e in entries]
if not entries:
# Empty entries list: get info from file_info_cache
info = file_info_cache.get(path)
if info is None:
continue
session_id = info[2].session_id
forked_from_id = info[2].forked_from_id
else:
session_id = entries[0].session_id
forked_from_id = "" # Not stored in UsageEntry
files[path_str] = {
"mtime": mtime,
"size": size,
"session_id": session_id,
"forked_from_id": forked_from_id,
"entries": entries_data,
}
payload = {
"schema_version": schema_version,
"cached_at": datetime.now(UTC).timestamp(),
"files": files,
}
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(payload, f, ensure_ascii=False, indent=2, sort_keys=True)
os.replace(tmp_path, cache_path)
tmp_path = None
except Exception as exc:
if os.environ.get("USAGE_DEBUG") == "1":
logger.warning("failed to write codex jsonl cache %s: %s", cache_path, exc)
finally:
if tmp_path and os.path.exists(tmp_path):
with contextlib.suppress(OSError):
os.unlink(tmp_path)