Repository navigation
Expand file tree
/
Copy pathstore.py
More file actions
302 lines (268 loc) · 12.3 KB
/
Copy pathstore.py
File metadata and controls
302 lines (268 loc) · 12.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
291
292
293
294
295
296
297
298
299
300
301
302
"""Account pool and task storage (JSON persisted, atomic writes)."""
from __future__ import annotations
import json
import os
import tempfile
import threading
import time
import uuid
_LOCK = threading.Lock()
# Core cookies that decide account life/death (kept in sync with engine.ESSENTIAL_COOKIES)
ESSENTIAL_COOKIES = ("hatch_sess", "hatch_gw", "hatch_vml",
"hatch_native_auth_device")
def _is_pos(v) -> bool:
try:
return int(v) > 0
except (TypeError, ValueError):
return False
# hatch_vml is fixed at ~2 days and is NOT extended by usage -- it is the cookie that decides account lifetime.
# Measured comparison: synced_at changed and new cookies were fetched after generation, but the expires of
# the 4 core cookies never moved. So when hatch_vml expires is unavailable, estimate "import time + 2 days"
# instead of falling back to the 30-day hatch_native_auth_device (which does not decide life/death and would mislead).
VML_TTL = 2 * 86400
def min_expiry(cookies_exp: dict | None) -> int | None:
"""The earliest-expiring core cookie (generic fallback, regardless of which one decides lifetime)."""
if not cookies_exp:
return None
vals = []
for name in ESSENTIAL_COOKIES:
v = cookies_exp.get(name)
try:
v = int(v)
except (TypeError, ValueError):
continue
if v > 0:
vals.append(v)
if not vals:
for v in cookies_exp.values():
try:
v = int(v)
except (TypeError, ValueError):
continue
if v > 0:
vals.append(v)
return min(vals) if vals else None
def account_expiry(cookies_exp: dict | None,
base_ts: int | None = None) -> int | None:
"""Actual account expiry time.
Priority:
1) real hatch_vml expires (most accurate);
2) when unavailable, estimate base_ts (import/sync moment) + VML_TTL -- lifetime is 2 days;
3) otherwise fall back to the earliest core-cookie expiry.
"""
ce = cookies_exp or {}
vml = ce.get("hatch_vml")
if _is_pos(vml):
return int(vml)
if base_ts:
return int(base_ts) + VML_TTL
return min_expiry(ce)
def _read(path: str, default):
if not os.path.isfile(path):
return default
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except Exception: # noqa: BLE001
return default
def _write(path: str, obj):
d = os.path.dirname(path)
os.makedirs(d, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=d)
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(obj, f, ensure_ascii=False, indent=1)
os.replace(tmp, path)
finally:
if os.path.exists(tmp):
os.unlink(tmp)
class Store:
def __init__(self, cfg):
self.cfg = cfg
self.accounts: list[dict] = _read(cfg.accounts_file, [])
for a in self.accounts:
ce = a.get("cookies_exp") or {}
if _is_pos(ce.get("hatch_vml")):
a["expires_at"] = int(ce["hatch_vml"])
self.tasks: dict[str, dict] = _read(cfg.tasks_file, {})
# ---------- Accounts ----------
def add_account(self, cookies: dict, label: str = "",
cookies_exp: dict | None = None) -> dict:
with _LOCK:
aid = uuid.uuid4().hex[:12]
exp = {k: int(v) for k, v in (cookies_exp or {}).items()
if _is_pos(v)}
now = int(time.time())
acc = {"id": aid, "label": label or aid, "cookies": cookies,
"cookies_exp": exp, "expires_at": account_expiry(exp, now),
"expiry_anchor": None,
"enabled": True, "created_at": now,
"last_used": None, "last_keepalive": None, "use_count": 0,
"ok": None, "note": "", "synced_at": None}
self.accounts.append(acc)
_write(self.cfg.accounts_file, self.accounts)
return acc
def list_accounts(self) -> list[dict]:
out = []
for a in self.accounts:
item = {k: v for k, v in a.items() if k != "cookies"}
ce = a.get("cookies_exp") or {}
if _is_pos(ce.get("hatch_vml")):
item["expires_at"] = int(ce["hatch_vml"])
item["cookie_count"] = len(a.get("cookies", {}))
item["essential_ok"] = all(
a.get("cookies", {}).get(n) for n in ESSENTIAL_COOKIES)
# Whether expiry is a measured hatch_vml expires or estimated from the "2-day lifetime"
item["expires_estimated"] = not _is_pos(ce.get("hatch_vml"))
out.append(item)
return out
def delete_account(self, aid: str) -> bool:
with _LOCK:
n = len(self.accounts)
self.accounts = [a for a in self.accounts if a["id"] != aid]
_write(self.cfg.accounts_file, self.accounts)
return len(self.accounts) != n
def pick_account(self, preferred_id: str | None = None, rotate: bool = True,
force_rotate: bool = False, exclude_id: str | None = None) -> dict | None:
"""Pick an available account: prefer reusing the warmed-up healthy account (avoids re-connect cost); rotate every 15 uses or on error."""
with _LOCK:
live = [a for a in self.accounts if a.get("enabled", True) and a.get("cookies")]
if not live:
return None
if exclude_id and len(live) > 1:
live = [a for a in live if a["id"] != exclude_id] or live
if preferred_id and not force_rotate:
for a in live:
if a["id"] == preferred_id and a.get("ok") is not False:
sticky_n = getattr(self, "_sticky_count", 0) if getattr(self, "_sticky_id", None) == preferred_id else 0
if not rotate or sticky_n < 15:
self._sticky_id = preferred_id
self._sticky_count = sticky_n + 1
a["last_used"] = time.time()
a["use_count"] = a.get("use_count", 0) + 1
_write(self.cfg.accounts_file, self.accounts)
return a
healthy = [a for a in live if a.get("ok") is not False]
candidates = healthy if healthy else live
candidates.sort(key=lambda a: (a.get("last_used") or 0.0, a.get("use_count") or 0))
acc = candidates[0]
self._sticky_id = acc["id"]
self._sticky_count = 1
acc["last_used"] = time.time()
acc["use_count"] = acc.get("use_count", 0) + 1
_write(self.cfg.accounts_file, self.accounts)
return acc
def touch_keepalive(self, aid: str, ok: bool | None = True, note: str = ""):
"""ok=None only records an unconfirmed check; keeps the last account state, last successful keepalive time, and expiry."""
with _LOCK:
now_ts = int(time.time())
for a in self.accounts:
if a["id"] == aid:
if ok is not None:
a["ok"] = ok
if ok is True:
a["last_keepalive"] = now_ts
# Only trust the actual cookie expiry or the original import anchor; never push +48h on a mere successful check.
a["expires_at"] = account_expiry(
a.get("cookies_exp"), a.get("expiry_anchor") or a.get("created_at"))
if note:
a["note"] = note[:300]
a["checked_at"] = now_ts
_write(self.cfg.accounts_file, self.accounts)
def mark(self, aid: str, ok: bool, note: str = ""):
with _LOCK:
for a in self.accounts:
if a["id"] == aid:
a["ok"] = ok
a["note"] = note[:300]
a["checked_at"] = int(time.time())
_write(self.cfg.accounts_file, self.accounts)
def get_account(self, aid: str) -> dict | None:
for a in self.accounts:
if a["id"] == aid:
return a
return None
def update_account(self, aid: str, **kw) -> dict | None:
"""Update label / enabled state / cookies / expiry, etc.; fields with None values are skipped.
Passing cookies_exp auto-recomputes expires_at, so callers do not need to compute it.
Maintenance rules for the estimate anchor expiry_anchor (avoid repeatedly inflating expiry):
- real hatch_vml expires was read -> clear the anchor, use the real value;
- unavailable and anchor is empty -> pin the anchor to now (only once);
- fresh-session cookies added (relogin) -> caller explicitly passes expiry_anchor=now to reset.
"""
with _LOCK:
for a in self.accounts:
if a["id"] == aid:
for k, v in kw.items():
if v is not None:
a[k] = v
if "cookies_exp" in kw and kw["cookies_exp"] is not None:
ce = a["cookies_exp"]
if _is_pos(ce.get("hatch_vml")):
a["expiry_anchor"] = None
elif not a.get("expiry_anchor"):
a["expiry_anchor"] = int(time.time())
a["expires_at"] = account_expiry(
ce, a.get("expiry_anchor") or a.get("created_at"))
_write(self.cfg.accounts_file, self.accounts)
return a
return None
def stats(self) -> dict:
now = int(time.time())
total = len(self.accounts)
enabled = sum(1 for a in self.accounts if a.get("enabled", True))
bad = sum(1 for a in self.accounts
if a.get("enabled", True) and a.get("ok") is False)
healthy = sum(1 for a in self.accounts
if a.get("enabled", True) and a.get("ok") is True)
expiring = sum(1 for a in self.accounts
if a.get("enabled", True)
and a.get("expires_at")
and a["expires_at"] - now < 3 * 86400)
expired = sum(1 for a in self.accounts
if a.get("enabled", True)
and a.get("expires_at") and a["expires_at"] <= now)
return {"total": total, "enabled": enabled, "disabled": total - enabled,
"healthy": healthy, "error": bad,
"expiring": expiring, "expired": expired}
# ---------- Tasks ----------
def create_task(self, kind: str, prompt: str) -> dict:
with _LOCK:
tid = "task_" + uuid.uuid4().hex[:20]
t = {"id": tid, "object": "task", "kind": kind, "prompt": prompt,
"status": "queued", "created_at": int(time.time()),
"updated_at": int(time.time()), "result": None, "error": None}
self.tasks[tid] = t
_write(self.cfg.tasks_file, self.tasks)
return t
def update_task(self, tid: str, **kw):
with _LOCK:
t = self.tasks.get(tid)
if not t:
return None
t.update(kw)
t["updated_at"] = int(time.time())
_write(self.cfg.tasks_file, self.tasks)
return t
def get_task(self, tid: str) -> dict | None:
return self.tasks.get(tid)
def list_tasks(self, limit: int = 50) -> list[dict]:
items = sorted(self.tasks.values(),
key=lambda t: t.get("created_at") or 0, reverse=True)
return items[:limit]
def delete_task(self, tid: str) -> bool:
with _LOCK:
if tid in self.tasks:
del self.tasks[tid]
_write(self.cfg.tasks_file, self.tasks)
return True
return False
def clear_tasks(self, keep: int = 0) -> int:
with _LOCK:
items = sorted(self.tasks.values(),
key=lambda t: t.get("created_at") or 0, reverse=True)
keep_ids = {t["id"] for t in items[:keep]}
removed = len(self.tasks) - len(keep_ids)
self.tasks = {k: v for k, v in self.tasks.items() if k in keep_ids}
_write(self.cfg.tasks_file, self.tasks)
return max(0, removed)