File size: 9,714 Bytes
88b583a
 
 
 
 
 
 
 
 
 
 
711f273
 
88b583a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eed3e2f
 
 
88b583a
 
 
 
 
 
 
eed3e2f
88b583a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139d9be
 
 
 
 
 
 
 
 
 
88b583a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
711f273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72dc684
 
 
 
711f273
72dc684
 
 
 
 
711f273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c3abff
711f273
 
 
 
 
 
72dc684
 
711f273
 
 
7c3abff
 
 
 
711f273
 
 
 
 
 
 
 
 
 
88b583a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139d9be
88b583a
 
 
 
 
 
711f273
 
88b583a
 
 
711f273
 
88b583a
711f273
 
 
 
 
 
 
 
 
88b583a
 
 
 
 
 
eed3e2f
 
 
 
88b583a
eed3e2f
88b583a
711f273
 
 
 
 
 
eed3e2f
 
139d9be
88b583a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3

import argparse
import hashlib
import json
import os
import shutil
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError


def env_required(name: str) -> str:
    value = os.environ.get(name, "").strip()
    if not value:
        raise SystemExit(f"missing required env: {name}")
    return value


MC_BIN = os.environ.get("MC_BIN", "mc")
ALIAS = os.environ.get("OBJECTSTORE_ALIAS", "daili-objectstore").strip() or "daili-objectstore"
ENDPOINT = env_required("OBJECTSTORE_ENDPOINT")
ACCESS_KEY = env_required("OBJECTSTORE_ACCESS_KEY")
SECRET_KEY = env_required("OBJECTSTORE_SECRET_KEY")
BUCKET = env_required("OBJECTSTORE_BUCKET")
ROOT = Path(env_required("OBJECTSTORE_ROOT")).resolve()
CONFIG_FALLBACK = os.environ.get("OBJECTSTORE_CONFIG_FALLBACK", "").strip()
REMOTE_CONFIG_KEY = "config/config.yaml"
REMOTE_AUTHS_PREFIX = "auths"


def mc_env() -> dict:
    env = os.environ.copy()
    for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"):
        env.pop(key, None)
    return env


MC_TIMEOUT = int(os.environ.get("MC_TIMEOUT", "30"))


def run_mc(args: list[str], check: bool = True) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        [MC_BIN, *args],
        check=check,
        env=mc_env(),
        capture_output=True,
        text=True,
        timeout=MC_TIMEOUT,
    )


def ensure_alias() -> None:
    run_mc(["alias", "set", ALIAS, ENDPOINT, ACCESS_KEY, SECRET_KEY, "--api", "S3v4", "--path", "on"])


def remote_path(key: str) -> str:
    return f"{ALIAS}/{BUCKET}/{key}"


def parse_ts(value: str) -> float:
    return datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp()


def local_path(rel: str) -> Path:
    return ROOT / rel


def touch_local(path: Path, ts: float) -> None:
    os.utime(path, (ts, ts))


def prune_empty_dirs(root: Path) -> None:
    if not root.exists():
        return
    for path in sorted((p for p in root.rglob("*") if p.is_dir()), reverse=True):
        try:
            path.rmdir()
        except OSError:
            pass


def file_md5(path: Path) -> str:
    digest = hashlib.md5()
    with path.open("rb") as handle:
        while True:
            chunk = handle.read(1024 * 1024)
            if not chunk:
                break
            digest.update(chunk)
    return digest.hexdigest()


def remote_inventory() -> dict[str, dict]:
    inventory: dict[str, dict] = {}

    config_proc = run_mc(["stat", "--json", remote_path(REMOTE_CONFIG_KEY)], check=False)
    if config_proc.returncode == 0 and config_proc.stdout.strip():
        raw = json.loads(config_proc.stdout)
        inventory[REMOTE_CONFIG_KEY] = {
            "etag": raw["etag"],
            "last_modified": parse_ts(raw["lastModified"]),
            "remote": remote_path(REMOTE_CONFIG_KEY),
        }

    auths_proc = run_mc(["ls", "--json", "--recursive", remote_path(REMOTE_AUTHS_PREFIX)], check=False)
    if auths_proc.returncode == 0:
        for line in auths_proc.stdout.splitlines():
            if not line.strip():
                continue
            raw = json.loads(line)
            if raw.get("status") != "success" or raw.get("type") != "file":
                continue
            rel = f"{REMOTE_AUTHS_PREFIX}/{raw['key']}"
            inventory[rel] = {
                "etag": raw["etag"],
                "last_modified": parse_ts(raw["lastModified"]),
                "remote": remote_path(rel),
            }

    return inventory


def local_inventory() -> dict[str, dict]:
    inventory: dict[str, dict] = {}

    config_path = local_path(REMOTE_CONFIG_KEY)
    if config_path.is_file():
        inventory[REMOTE_CONFIG_KEY] = {
            "md5": file_md5(config_path),
            "mtime": config_path.stat().st_mtime,
        }

    auth_root = local_path(REMOTE_AUTHS_PREFIX)
    if auth_root.is_dir():
        for path in sorted(auth_root.rglob("*")):
            if not path.is_file():
                continue
            rel = path.relative_to(ROOT).as_posix()
            inventory[rel] = {
                "md5": file_md5(path),
                "mtime": path.stat().st_mtime,
            }

    return inventory


def download_file(rel: str, meta: dict) -> None:
    dest = local_path(rel)
    dest.parent.mkdir(parents=True, exist_ok=True)
    run_mc(["cp", meta["remote"], str(dest)])
    touch_local(dest, meta["last_modified"])


def upload_file(rel: str) -> None:
    src = local_path(rel)
    if not src.is_file():
        return
    run_mc(["cp", str(src), remote_path(rel)])


def delete_remote(rel: str) -> None:
    run_mc(["rm", "--force", remote_path(rel)], check=False)


INVALID_STATUS_KEYWORDS = [
    "token_invalidated",
    "token_revoked",
    "invalidated oauth token",
    "authentication token has been invalidated",
    "account has been deactivated",
    "no_organization",
    "unauthorized",
    "401",
]


def _is_free_auth_file_name(file_name: str) -> bool:
    return file_name.strip().endswith("-free.json")


def _fetch_invalid_auth_names() -> set[str]:
    """Query local CLIProxyAPI management API for invalid free auth names.

    Team auth lifecycle is managed externally; runtime sync must not evict team
    auths for transient states such as cooldown or temporary unavailability.
    """
    mgmt_key = os.environ.get("MANAGEMENT_PASSWORD") or os.environ.get("API_KEY") or ""
    port = os.environ.get("PORT", "8317")
    if not mgmt_key:
        return set()
    url = f"http://127.0.0.1:{port}/v0/management/auth-files"
    req = Request(url)
    req.add_header("Authorization", f"Bearer {mgmt_key}")
    try:
        with urlopen(req, timeout=10) as resp:
            data = json.loads(resp.read())
    except (HTTPError, URLError, OSError, ValueError):
        return set()
    if not isinstance(data, dict):
        return set()
    files = data.get("files", [])
    if not isinstance(files, list):
        return set()

    invalid_names: set[str] = set()
    now = datetime.now(timezone.utc).isoformat()
    for entry in files:
        if not isinstance(entry, dict):
            continue
        name = str(entry.get("name") or "").strip()
        if not name:
            continue
        if not _is_free_auth_file_name(name):
            continue
        status = str(entry.get("status") or "").strip().lower()
        status_message = str(entry.get("status_message") or "").strip().lower()
        unavailable = entry.get("unavailable", False)
        # Check next_retry_after β€” if still in cooldown, treat as invalid
        next_retry = str(entry.get("next_retry_after") or "").strip()
        in_cooldown = bool(next_retry and next_retry > now)
        if status == "error" or unavailable or in_cooldown:
            invalid_names.add(name)
            continue
        if status_message:
            for kw in INVALID_STATUS_KEYWORDS:
                if kw in status_message:
                    invalid_names.add(name)
                    break
    return invalid_names


def restore() -> None:
    ensure_alias()
    ROOT.mkdir(parents=True, exist_ok=True)
    local_path("config").mkdir(parents=True, exist_ok=True)
    shutil.rmtree(local_path(REMOTE_AUTHS_PREFIX), ignore_errors=True)
    local_path(REMOTE_AUTHS_PREFIX).mkdir(parents=True, exist_ok=True)

    remote = remote_inventory()
    if REMOTE_CONFIG_KEY in remote:
        download_file(REMOTE_CONFIG_KEY, remote[REMOTE_CONFIG_KEY])
    elif CONFIG_FALLBACK:
        fallback = Path(CONFIG_FALLBACK)
        if fallback.is_file():
            target = local_path(REMOTE_CONFIG_KEY)
            target.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy2(fallback, target)

    run_mc(["cp", "--recursive", remote_path(f"{REMOTE_AUTHS_PREFIX}/"), str(local_path(REMOTE_AUTHS_PREFIX))], check=False)


def sync() -> None:
    ensure_alias()
    ROOT.mkdir(parents=True, exist_ok=True)

    invalid_names = _fetch_invalid_auth_names()

    remote = remote_inventory()
    local = local_inventory()

    # Remote has, local doesn't β†’ download (unless invalid)
    # Both have, md5 differs β†’ compare mtime, newer wins (unless invalid)
    for rel, meta in remote.items():
        # Extract auth file name from rel path (e.g. "auths/codex-xxx-free.json" β†’ "codex-xxx-free.json")
        file_name = rel.rsplit("/", 1)[-1] if "/" in rel else rel
        if file_name in invalid_names:
            delete_remote(rel)
            dest = local_path(rel)
            if dest.is_file():
                dest.unlink()
            continue

        local_meta = local.get(rel)
        if local_meta is None:
            download_file(rel, meta)
            continue
        if local_meta["md5"] == meta["etag"]:
            continue
        if local_meta["mtime"] > meta["last_modified"]:
            upload_file(rel)
        else:
            download_file(rel, meta)

    # Local has, remote doesn't β†’ upload to remote (new file from management UI)
    for rel in sorted(set(local) - set(remote)):
        file_name = rel.rsplit("/", 1)[-1] if "/" in rel else rel
        if file_name in invalid_names:
            dest = local_path(rel)
            if dest.is_file():
                dest.unlink()
            continue
        upload_file(rel)

    prune_empty_dirs(local_path(REMOTE_AUTHS_PREFIX))


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("mode", choices=("restore", "sync"))
    args = parser.parse_args()

    if args.mode == "restore":
        restore()
    else:
        sync()
    return 0


if __name__ == "__main__":
    sys.exit(main())