relay-codex 0.0.0-stage → 0.4.1

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.
@@ -0,0 +1,97 @@
1
+ """Versioned native runtime snapshots; never replace the user's Codex installation."""
2
+
3
+ import json
4
+ import shutil
5
+ import time
6
+
7
+ import compatibility
8
+ from settings import atomic_json
9
+
10
+
11
+ def index(state):
12
+ file = state / "runtimes.json"
13
+ return (
14
+ json.loads(file.read_text())
15
+ if file.exists()
16
+ else {"runtimes": [], "selected": None}
17
+ )
18
+
19
+
20
+ def capture(binary, state):
21
+ verified = compatibility.check(binary, state)
22
+ native = compatibility.executable(binary)
23
+ digest = compatibility.digest(native)
24
+ root = state / "native-runtimes" / digest[:20]
25
+ target = root / "vendor" / "bin" / "codex"
26
+ if not target.exists():
27
+ root.mkdir(parents=True, exist_ok=True, mode=0o700)
28
+ temporary = root / "staging"
29
+ if temporary.exists():
30
+ shutil.rmtree(temporary)
31
+ if native.parent.name == "bin":
32
+ shutil.copytree(native.parent.parent, temporary)
33
+ else:
34
+ (temporary / "bin").mkdir(parents=True)
35
+ shutil.copy2(native, temporary / "bin" / "codex")
36
+ temporary.rename(root / "vendor")
37
+ if compatibility.digest(target) != digest:
38
+ raise ValueError("Cached runtime integrity check failed")
39
+ value = index(state)
40
+ if not any(row["sha256"] == digest for row in value["runtimes"]):
41
+ value["runtimes"].append(
42
+ {
43
+ "version": verified["version"],
44
+ "sha256": digest,
45
+ "binary": str(target),
46
+ "captured": time.time(),
47
+ }
48
+ )
49
+ atomic_json(state / "runtimes.json", value)
50
+ return str(target), verified
51
+
52
+
53
+ def select(binary, state):
54
+ value = index(state)
55
+ if value.get("selected"):
56
+ choices = [r for r in value["runtimes"] if r["sha256"] == value["selected"]]
57
+ if not choices:
58
+ raise ValueError("Selected runtime snapshot is missing")
59
+ selected = choices[0]
60
+ if compatibility.digest(selected["binary"]) != selected["sha256"]:
61
+ raise ValueError("Selected runtime snapshot was modified")
62
+ return (
63
+ selected["binary"],
64
+ compatibility.check(selected["binary"], state),
65
+ "selected_snapshot",
66
+ )
67
+ try:
68
+ path, verified = capture(binary, state)
69
+ return path, verified, "installed_snapshot"
70
+ except Exception as error:
71
+ for row in reversed(value["runtimes"]):
72
+ try:
73
+ if compatibility.digest(row["binary"]) != row["sha256"]:
74
+ continue
75
+ verified = compatibility.check(row["binary"], state)
76
+ return row["binary"], verified, "compatible_fallback"
77
+ except Exception:
78
+ continue
79
+ raise error
80
+
81
+
82
+ def choose(state, prefix=None):
83
+ value = index(state)
84
+ if prefix in (None, "latest", "installed"):
85
+ value["selected"] = None
86
+ else:
87
+ matching = [
88
+ row
89
+ for row in value["runtimes"]
90
+ if row["sha256"].startswith(prefix) or row["version"] == prefix
91
+ ]
92
+ if len(matching) != 1:
93
+ raise ValueError(
94
+ "Choose a unique runtime hash or version from runtime list"
95
+ )
96
+ value["selected"] = matching[0]["sha256"]
97
+ atomic_json(state / "runtimes.json", value)