token-runtime 0.1.0a2__py3-none-any.whl
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.
- token_runtime/__init__.py +1 -0
- token_runtime/adapters.py +181 -0
- token_runtime/agent_integrations.py +114 -0
- token_runtime/anthropic_adapter.py +301 -0
- token_runtime/anthropic_conformance.py +336 -0
- token_runtime/anthropic_preserved_thinking_cert.py +213 -0
- token_runtime/benchmark.py +106 -0
- token_runtime/capabilities.py +111 -0
- token_runtime/cli.py +284 -0
- token_runtime/codex_recertification.py +115 -0
- token_runtime/compat_cert.py +462 -0
- token_runtime/compatibility.py +81 -0
- token_runtime/config.py +72 -0
- token_runtime/conformance.py +36 -0
- token_runtime/contracts.py +70 -0
- token_runtime/engine.py +136 -0
- token_runtime/feature_flags.py +78 -0
- token_runtime/gateway.py +151 -0
- token_runtime/gemini_adapter.py +260 -0
- token_runtime/gemini_conformance.py +351 -0
- token_runtime/integrations.py +272 -0
- token_runtime/metrics.py +79 -0
- token_runtime/model.py +39 -0
- token_runtime/openai_certification.py +310 -0
- token_runtime/planner.py +96 -0
- token_runtime/reducers.py +179 -0
- token_runtime/store.py +36 -0
- token_runtime/strategies.py +50 -0
- token_runtime/terminal_ui.py +96 -0
- token_runtime-0.1.0a2.dist-info/METADATA +238 -0
- token_runtime-0.1.0a2.dist-info/RECORD +35 -0
- token_runtime-0.1.0a2.dist-info/WHEEL +5 -0
- token_runtime-0.1.0a2.dist-info/entry_points.txt +2 -0
- token_runtime-0.1.0a2.dist-info/licenses/LICENSE +202 -0
- token_runtime-0.1.0a2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from hashlib import sha256
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
from .anthropic_conformance import build_anthropic_conformance
|
|
9
|
+
from .capabilities import CapabilityKey
|
|
10
|
+
from .codex_recertification import build_codex_01540_recertification
|
|
11
|
+
from .compatibility import CompatibilityRecord, CompatibilityState
|
|
12
|
+
from .gemini_conformance import build_gemini_conformance
|
|
13
|
+
from .openai_certification import build_openai_certification
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class OSFamily(str, Enum):
|
|
17
|
+
LINUX = "linux"
|
|
18
|
+
WINDOWS = "windows"
|
|
19
|
+
MACOS = "macos"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OSScopeKind(str, Enum):
|
|
23
|
+
EXACT = "EXACT"
|
|
24
|
+
TESTED_SET = "TESTED_SET"
|
|
25
|
+
OS_AGNOSTIC = "OS_AGNOSTIC"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class UpgradeDimension(str, Enum):
|
|
29
|
+
CLIENT_UI = "client_ui"
|
|
30
|
+
CLIENT_VERSION = "client_version"
|
|
31
|
+
SDK = "sdk"
|
|
32
|
+
TOKENIZER = "tokenizer"
|
|
33
|
+
CACHE_SEMANTICS = "cache_semantics"
|
|
34
|
+
TOOL_SCHEMA = "tool_schema"
|
|
35
|
+
OS_RUNTIME = "os_runtime"
|
|
36
|
+
BENCHMARK_GENERATION = "benchmark_generation"
|
|
37
|
+
PROTOCOL_WIRE = "protocol_wire"
|
|
38
|
+
SECURITY_CRITICAL = "security_critical"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class UpgradeImpact(str, Enum):
|
|
42
|
+
NO_ACTION = "NO_ACTION"
|
|
43
|
+
REVIEW = "REVIEW"
|
|
44
|
+
TARGETED_RECERTIFY = "TARGETED_RECERTIFY"
|
|
45
|
+
FULL_RECERTIFY = "FULL_RECERTIFY"
|
|
46
|
+
BLOCK = "BLOCK"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True, slots=True)
|
|
50
|
+
class BenchmarkGeneration:
|
|
51
|
+
generation_id: str
|
|
52
|
+
schema_version: int
|
|
53
|
+
corpus_version: str
|
|
54
|
+
evaluator_version: str
|
|
55
|
+
policy_version: str
|
|
56
|
+
evidence_digest: str
|
|
57
|
+
evidence_ids: tuple[str, ...]
|
|
58
|
+
|
|
59
|
+
def __post_init__(self) -> None:
|
|
60
|
+
if not self.generation_id:
|
|
61
|
+
raise ValueError("generation_id must be non-empty")
|
|
62
|
+
if self.schema_version < 1:
|
|
63
|
+
raise ValueError("schema_version must be positive")
|
|
64
|
+
for name in (
|
|
65
|
+
"corpus_version",
|
|
66
|
+
"evaluator_version",
|
|
67
|
+
"policy_version",
|
|
68
|
+
"evidence_digest",
|
|
69
|
+
):
|
|
70
|
+
if not getattr(self, name):
|
|
71
|
+
raise ValueError(f"{name} must be non-empty")
|
|
72
|
+
evidence = tuple(sorted(set(self.evidence_ids)))
|
|
73
|
+
if not evidence or any(not item.strip() for item in evidence):
|
|
74
|
+
raise ValueError("evidence_ids must contain non-blank values")
|
|
75
|
+
object.__setattr__(self, "evidence_ids", evidence)
|
|
76
|
+
encoded = json.dumps(list(evidence), separators=(",", ":")).encode("utf-8")
|
|
77
|
+
if sha256(encoded).hexdigest() != self.evidence_digest:
|
|
78
|
+
raise ValueError("evidence_digest does not match evidence_ids")
|
|
79
|
+
|
|
80
|
+
def to_primitive(self) -> dict[str, object]:
|
|
81
|
+
return {
|
|
82
|
+
"generation_id": self.generation_id,
|
|
83
|
+
"schema_version": self.schema_version,
|
|
84
|
+
"corpus_version": self.corpus_version,
|
|
85
|
+
"evaluator_version": self.evaluator_version,
|
|
86
|
+
"policy_version": self.policy_version,
|
|
87
|
+
"evidence_digest": self.evidence_digest,
|
|
88
|
+
"evidence_ids": list(self.evidence_ids),
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True, order=True, slots=True)
|
|
93
|
+
class CertificationTarget:
|
|
94
|
+
client_family: str
|
|
95
|
+
protocol_family: str
|
|
96
|
+
provider_family: str
|
|
97
|
+
model_family: str | None = None
|
|
98
|
+
|
|
99
|
+
@classmethod
|
|
100
|
+
def from_key(cls, key: CapabilityKey) -> "CertificationTarget":
|
|
101
|
+
return cls(
|
|
102
|
+
client_family=key.client_family,
|
|
103
|
+
protocol_family=key.protocol_family,
|
|
104
|
+
provider_family=key.provider_family,
|
|
105
|
+
model_family=key.model_family,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
def matches(self, key: CapabilityKey) -> bool:
|
|
109
|
+
return (
|
|
110
|
+
self.client_family == key.client_family
|
|
111
|
+
and self.protocol_family == key.protocol_family
|
|
112
|
+
and self.provider_family == key.provider_family
|
|
113
|
+
and self.model_family == key.model_family
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def to_primitive(self) -> dict[str, object]:
|
|
117
|
+
return {
|
|
118
|
+
"client_family": self.client_family,
|
|
119
|
+
"protocol_family": self.protocol_family,
|
|
120
|
+
"provider_family": self.provider_family,
|
|
121
|
+
"model_family": self.model_family,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass(frozen=True, slots=True)
|
|
126
|
+
class CertificationScope:
|
|
127
|
+
target: CertificationTarget
|
|
128
|
+
client_versions: tuple[str, ...] = ()
|
|
129
|
+
os_scope: OSScopeKind = OSScopeKind.OS_AGNOSTIC
|
|
130
|
+
os_families: tuple[OSFamily, ...] = ()
|
|
131
|
+
|
|
132
|
+
def __post_init__(self) -> None:
|
|
133
|
+
versions = tuple(sorted(set(self.client_versions)))
|
|
134
|
+
families = tuple(sorted(set(self.os_families), key=lambda item: item.value))
|
|
135
|
+
object.__setattr__(self, "client_versions", versions)
|
|
136
|
+
object.__setattr__(self, "os_families", families)
|
|
137
|
+
|
|
138
|
+
if not isinstance(self.os_scope, OSScopeKind):
|
|
139
|
+
raise TypeError("os_scope must be OSScopeKind")
|
|
140
|
+
if any(not version for version in versions):
|
|
141
|
+
raise ValueError("client_versions must be non-empty strings")
|
|
142
|
+
if self.os_scope is OSScopeKind.EXACT and len(families) != 1:
|
|
143
|
+
raise ValueError("EXACT os scope requires exactly one OS family")
|
|
144
|
+
if self.os_scope is OSScopeKind.TESTED_SET and not families:
|
|
145
|
+
raise ValueError("TESTED_SET os scope requires at least one OS family")
|
|
146
|
+
if self.os_scope is OSScopeKind.OS_AGNOSTIC and families:
|
|
147
|
+
raise ValueError("OS_AGNOSTIC scope cannot enumerate OS families")
|
|
148
|
+
|
|
149
|
+
def matches(self, key: CapabilityKey, os_family: OSFamily) -> bool:
|
|
150
|
+
if not isinstance(os_family, OSFamily):
|
|
151
|
+
return False
|
|
152
|
+
if not self.target.matches(key):
|
|
153
|
+
return False
|
|
154
|
+
if self.client_versions:
|
|
155
|
+
if key.client_version not in self.client_versions:
|
|
156
|
+
return False
|
|
157
|
+
elif key.client_version:
|
|
158
|
+
return False
|
|
159
|
+
|
|
160
|
+
if self.os_scope is OSScopeKind.OS_AGNOSTIC:
|
|
161
|
+
return True
|
|
162
|
+
return os_family in self.os_families
|
|
163
|
+
|
|
164
|
+
def to_primitive(self) -> dict[str, object]:
|
|
165
|
+
return {
|
|
166
|
+
"target": self.target.to_primitive(),
|
|
167
|
+
"client_versions": list(self.client_versions),
|
|
168
|
+
"os_scope": self.os_scope.value,
|
|
169
|
+
"os_families": [item.value for item in self.os_families],
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@dataclass(frozen=True, slots=True)
|
|
174
|
+
class CertificationEntry:
|
|
175
|
+
scope: CertificationScope
|
|
176
|
+
state: CompatibilityState
|
|
177
|
+
evidence_ids: tuple[str, ...]
|
|
178
|
+
benchmark_generation_id: str | None = None
|
|
179
|
+
reason: str | None = None
|
|
180
|
+
|
|
181
|
+
def __post_init__(self) -> None:
|
|
182
|
+
if not isinstance(self.state, CompatibilityState):
|
|
183
|
+
raise TypeError("state must be CompatibilityState")
|
|
184
|
+
evidence = tuple(sorted(set(self.evidence_ids)))
|
|
185
|
+
if any(not item.strip() for item in evidence):
|
|
186
|
+
raise ValueError("evidence_ids must contain non-blank values")
|
|
187
|
+
object.__setattr__(self, "evidence_ids", evidence)
|
|
188
|
+
|
|
189
|
+
def to_primitive(self) -> dict[str, object]:
|
|
190
|
+
return {
|
|
191
|
+
"scope": self.scope.to_primitive(),
|
|
192
|
+
"state": self.state.value,
|
|
193
|
+
"evidence_ids": list(self.evidence_ids),
|
|
194
|
+
"benchmark_generation_id": self.benchmark_generation_id,
|
|
195
|
+
"reason": self.reason,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _entry_sort_key(entry: CertificationEntry) -> tuple[object, ...]:
|
|
200
|
+
target = entry.scope.target
|
|
201
|
+
return (
|
|
202
|
+
target.client_family,
|
|
203
|
+
target.protocol_family,
|
|
204
|
+
target.provider_family,
|
|
205
|
+
target.model_family or "",
|
|
206
|
+
entry.scope.client_versions,
|
|
207
|
+
entry.scope.os_scope.value,
|
|
208
|
+
tuple(item.value for item in entry.scope.os_families),
|
|
209
|
+
entry.state.value,
|
|
210
|
+
entry.evidence_ids,
|
|
211
|
+
entry.benchmark_generation_id or "",
|
|
212
|
+
entry.reason or "",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
@dataclass(frozen=True, slots=True)
|
|
217
|
+
class CertificationMatrix:
|
|
218
|
+
entries: tuple[CertificationEntry, ...]
|
|
219
|
+
|
|
220
|
+
def __post_init__(self) -> None:
|
|
221
|
+
object.__setattr__(self, "entries", tuple(sorted(self.entries, key=_entry_sort_key)))
|
|
222
|
+
|
|
223
|
+
def matches(
|
|
224
|
+
self,
|
|
225
|
+
key: CapabilityKey,
|
|
226
|
+
os_family: OSFamily,
|
|
227
|
+
) -> tuple[CertificationEntry, ...]:
|
|
228
|
+
return tuple(
|
|
229
|
+
entry for entry in self.entries if entry.scope.matches(key, os_family)
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
def to_primitive(self) -> list[dict[str, object]]:
|
|
233
|
+
return [entry.to_primitive() for entry in self.entries]
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@dataclass(frozen=True, slots=True)
|
|
237
|
+
class UpgradeSignal:
|
|
238
|
+
dimension: UpgradeDimension
|
|
239
|
+
before: str
|
|
240
|
+
after: str
|
|
241
|
+
|
|
242
|
+
def __post_init__(self) -> None:
|
|
243
|
+
if not isinstance(self.dimension, UpgradeDimension):
|
|
244
|
+
raise TypeError("dimension must be UpgradeDimension")
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _effective_evidence_id(entry: CertificationEntry) -> str:
|
|
248
|
+
if not entry.evidence_ids:
|
|
249
|
+
return "unknown"
|
|
250
|
+
if len(entry.evidence_ids) == 1:
|
|
251
|
+
return entry.evidence_ids[0]
|
|
252
|
+
joined = "\n".join(entry.evidence_ids).encode("utf-8")
|
|
253
|
+
return f"compat-cert:{sha256(joined).hexdigest()}"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _passthrough(key: CapabilityKey, reason: str) -> CompatibilityRecord:
|
|
257
|
+
return CompatibilityRecord(
|
|
258
|
+
key=key,
|
|
259
|
+
state=CompatibilityState.PASSTHROUGH_ONLY,
|
|
260
|
+
evidence_id="unknown",
|
|
261
|
+
reason=reason,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class CompatibilityPolicy:
|
|
266
|
+
_UPGRADE_IMPACT = {
|
|
267
|
+
UpgradeDimension.CLIENT_UI: UpgradeImpact.REVIEW,
|
|
268
|
+
UpgradeDimension.CLIENT_VERSION: UpgradeImpact.TARGETED_RECERTIFY,
|
|
269
|
+
UpgradeDimension.SDK: UpgradeImpact.REVIEW,
|
|
270
|
+
UpgradeDimension.TOKENIZER: UpgradeImpact.TARGETED_RECERTIFY,
|
|
271
|
+
UpgradeDimension.CACHE_SEMANTICS: UpgradeImpact.TARGETED_RECERTIFY,
|
|
272
|
+
UpgradeDimension.TOOL_SCHEMA: UpgradeImpact.TARGETED_RECERTIFY,
|
|
273
|
+
UpgradeDimension.OS_RUNTIME: UpgradeImpact.TARGETED_RECERTIFY,
|
|
274
|
+
UpgradeDimension.BENCHMARK_GENERATION: UpgradeImpact.TARGETED_RECERTIFY,
|
|
275
|
+
UpgradeDimension.PROTOCOL_WIRE: UpgradeImpact.FULL_RECERTIFY,
|
|
276
|
+
UpgradeDimension.SECURITY_CRITICAL: UpgradeImpact.BLOCK,
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
def __init__(self, generations: tuple[BenchmarkGeneration, ...]) -> None:
|
|
280
|
+
by_id: dict[str, BenchmarkGeneration] = {}
|
|
281
|
+
for generation in generations:
|
|
282
|
+
existing = by_id.get(generation.generation_id)
|
|
283
|
+
if existing is not None and existing != generation:
|
|
284
|
+
raise ValueError(
|
|
285
|
+
f"conflicting benchmark generation: {generation.generation_id}"
|
|
286
|
+
)
|
|
287
|
+
by_id[generation.generation_id] = generation
|
|
288
|
+
self._generations = by_id
|
|
289
|
+
|
|
290
|
+
def resolve(
|
|
291
|
+
self,
|
|
292
|
+
matrix: CertificationMatrix,
|
|
293
|
+
key: CapabilityKey,
|
|
294
|
+
os_family: OSFamily,
|
|
295
|
+
*,
|
|
296
|
+
required_benchmark_generation_id: str | None = None,
|
|
297
|
+
) -> CompatibilityRecord:
|
|
298
|
+
matches = matrix.matches(key, os_family)
|
|
299
|
+
if not matches:
|
|
300
|
+
return _passthrough(key, "unknown_capability")
|
|
301
|
+
if len(matches) != 1:
|
|
302
|
+
return _passthrough(key, "ambiguous_certification_scope")
|
|
303
|
+
|
|
304
|
+
entry = matches[0]
|
|
305
|
+
if entry.state is CompatibilityState.CERTIFIED:
|
|
306
|
+
if not entry.evidence_ids:
|
|
307
|
+
return _passthrough(key, "missing_certification_evidence")
|
|
308
|
+
generation_id = entry.benchmark_generation_id
|
|
309
|
+
if not generation_id:
|
|
310
|
+
return _passthrough(key, "missing_benchmark_generation")
|
|
311
|
+
generation = self._generations.get(generation_id)
|
|
312
|
+
if generation is None:
|
|
313
|
+
return _passthrough(key, "unknown_benchmark_generation")
|
|
314
|
+
if not set(entry.evidence_ids).issubset(generation.evidence_ids):
|
|
315
|
+
return _passthrough(key, "benchmark_evidence_mismatch")
|
|
316
|
+
if (
|
|
317
|
+
required_benchmark_generation_id is not None
|
|
318
|
+
and required_benchmark_generation_id != generation_id
|
|
319
|
+
):
|
|
320
|
+
return _passthrough(key, "benchmark_generation_mismatch")
|
|
321
|
+
|
|
322
|
+
return CompatibilityRecord(
|
|
323
|
+
key=key,
|
|
324
|
+
state=entry.state,
|
|
325
|
+
evidence_id=_effective_evidence_id(entry),
|
|
326
|
+
reason=entry.reason,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
def classify_upgrade(self, signal: UpgradeSignal) -> UpgradeImpact:
|
|
330
|
+
if signal.before == signal.after:
|
|
331
|
+
return UpgradeImpact.NO_ACTION
|
|
332
|
+
return self._UPGRADE_IMPACT[signal.dimension]
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
@dataclass(frozen=True, slots=True)
|
|
336
|
+
class CompatibilityCertificationBundle:
|
|
337
|
+
matrix: CertificationMatrix
|
|
338
|
+
benchmark_generations: tuple[BenchmarkGeneration, ...]
|
|
339
|
+
|
|
340
|
+
def resolve(
|
|
341
|
+
self,
|
|
342
|
+
key: CapabilityKey,
|
|
343
|
+
os_family: OSFamily,
|
|
344
|
+
*,
|
|
345
|
+
required_benchmark_generation_id: str | None = None,
|
|
346
|
+
) -> CompatibilityRecord:
|
|
347
|
+
return CompatibilityPolicy(self.benchmark_generations).resolve(
|
|
348
|
+
self.matrix,
|
|
349
|
+
key,
|
|
350
|
+
os_family,
|
|
351
|
+
required_benchmark_generation_id=required_benchmark_generation_id,
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
def to_primitive(self) -> dict[str, object]:
|
|
355
|
+
generations = sorted(
|
|
356
|
+
self.benchmark_generations,
|
|
357
|
+
key=lambda item: item.generation_id,
|
|
358
|
+
)
|
|
359
|
+
return {
|
|
360
|
+
"benchmark_generations": [item.to_primitive() for item in generations],
|
|
361
|
+
"matrix": self.matrix.to_primitive(),
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _scope_for_key(
|
|
366
|
+
key: CapabilityKey,
|
|
367
|
+
*,
|
|
368
|
+
os_scope: OSScopeKind,
|
|
369
|
+
os_families: tuple[OSFamily, ...] = (),
|
|
370
|
+
) -> CertificationScope:
|
|
371
|
+
versions = (key.client_version,) if key.client_version else ()
|
|
372
|
+
return CertificationScope(
|
|
373
|
+
target=CertificationTarget.from_key(key),
|
|
374
|
+
client_versions=versions,
|
|
375
|
+
os_scope=os_scope,
|
|
376
|
+
os_families=os_families,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _benchmark_generation(evidence_ids: tuple[str, ...]) -> BenchmarkGeneration:
|
|
381
|
+
encoded = json.dumps(sorted(evidence_ids), separators=(",", ":")).encode("utf-8")
|
|
382
|
+
return BenchmarkGeneration(
|
|
383
|
+
generation_id="token-compat-cert-1:compat-v1",
|
|
384
|
+
schema_version=1,
|
|
385
|
+
corpus_version="c3-c6-conformance-v1",
|
|
386
|
+
evaluator_version="token-conformance-v1",
|
|
387
|
+
policy_version="compat-policy-v1",
|
|
388
|
+
evidence_digest=sha256(encoded).hexdigest(),
|
|
389
|
+
evidence_ids=tuple(sorted(evidence_ids)),
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def build_current_certification() -> CompatibilityCertificationBundle:
|
|
394
|
+
openai = build_openai_certification()
|
|
395
|
+
codex = build_codex_01540_recertification()
|
|
396
|
+
anthropic = build_anthropic_conformance()
|
|
397
|
+
gemini = build_gemini_conformance()
|
|
398
|
+
|
|
399
|
+
certified_evidence_ids = tuple(
|
|
400
|
+
sorted(
|
|
401
|
+
{
|
|
402
|
+
*(item.evidence_id for item in openai.evidence),
|
|
403
|
+
codex.evidence.evidence_id,
|
|
404
|
+
}
|
|
405
|
+
)
|
|
406
|
+
)
|
|
407
|
+
generation = _benchmark_generation(certified_evidence_ids)
|
|
408
|
+
entries: list[CertificationEntry] = []
|
|
409
|
+
|
|
410
|
+
for record in openai.records:
|
|
411
|
+
if record.key.client_family == "codex":
|
|
412
|
+
scope = _scope_for_key(
|
|
413
|
+
record.key,
|
|
414
|
+
os_scope=OSScopeKind.EXACT,
|
|
415
|
+
os_families=(OSFamily.LINUX,),
|
|
416
|
+
)
|
|
417
|
+
else:
|
|
418
|
+
scope = _scope_for_key(
|
|
419
|
+
record.key,
|
|
420
|
+
os_scope=OSScopeKind.OS_AGNOSTIC,
|
|
421
|
+
)
|
|
422
|
+
entries.append(
|
|
423
|
+
CertificationEntry(
|
|
424
|
+
scope=scope,
|
|
425
|
+
state=record.state,
|
|
426
|
+
evidence_ids=(record.evidence_id,),
|
|
427
|
+
benchmark_generation_id=generation.generation_id,
|
|
428
|
+
reason=record.reason,
|
|
429
|
+
)
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
entries.append(
|
|
433
|
+
CertificationEntry(
|
|
434
|
+
scope=_scope_for_key(
|
|
435
|
+
codex.record.key,
|
|
436
|
+
os_scope=OSScopeKind.EXACT,
|
|
437
|
+
os_families=(OSFamily.LINUX,),
|
|
438
|
+
),
|
|
439
|
+
state=codex.record.state,
|
|
440
|
+
evidence_ids=(codex.record.evidence_id,),
|
|
441
|
+
benchmark_generation_id=generation.generation_id,
|
|
442
|
+
reason=codex.record.reason,
|
|
443
|
+
)
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
for bundle in (anthropic, gemini):
|
|
447
|
+
entries.append(
|
|
448
|
+
CertificationEntry(
|
|
449
|
+
scope=_scope_for_key(
|
|
450
|
+
bundle.record.key,
|
|
451
|
+
os_scope=OSScopeKind.OS_AGNOSTIC,
|
|
452
|
+
),
|
|
453
|
+
state=bundle.record.state,
|
|
454
|
+
evidence_ids=(bundle.record.evidence_id,),
|
|
455
|
+
reason=bundle.record.reason,
|
|
456
|
+
)
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
return CompatibilityCertificationBundle(
|
|
460
|
+
matrix=CertificationMatrix(tuple(entries)),
|
|
461
|
+
benchmark_generations=(generation,),
|
|
462
|
+
)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import Enum
|
|
5
|
+
|
|
6
|
+
from .capabilities import CapabilityKey
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CompatibilityState(str, Enum):
|
|
10
|
+
CERTIFIED = "CERTIFIED"
|
|
11
|
+
CANARY = "CANARY"
|
|
12
|
+
EXPERIMENTAL = "EXPERIMENTAL"
|
|
13
|
+
PASSTHROUGH_ONLY = "PASSTHROUGH_ONLY"
|
|
14
|
+
UNSUPPORTED = "UNSUPPORTED"
|
|
15
|
+
BLOCKED = "BLOCKED"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class CompatibilityRecord:
|
|
20
|
+
key: CapabilityKey
|
|
21
|
+
state: CompatibilityState
|
|
22
|
+
evidence_id: str
|
|
23
|
+
reason: str | None = None
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
if not isinstance(self.state, CompatibilityState):
|
|
27
|
+
raise TypeError("state must be CompatibilityState")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class DuplicateCompatibilityError(ValueError):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
def _unknown_record(key: CapabilityKey) -> CompatibilityRecord:
|
|
34
|
+
return CompatibilityRecord(
|
|
35
|
+
key=key,
|
|
36
|
+
state=CompatibilityState.PASSTHROUGH_ONLY,
|
|
37
|
+
evidence_id="unknown",
|
|
38
|
+
reason="unknown_capability",
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, slots=True)
|
|
43
|
+
class CompatibilityMatrix:
|
|
44
|
+
records: tuple[CompatibilityRecord, ...]
|
|
45
|
+
|
|
46
|
+
def __post_init__(self) -> None:
|
|
47
|
+
object.__setattr__(
|
|
48
|
+
self,
|
|
49
|
+
"records",
|
|
50
|
+
tuple(sorted(self.records, key=lambda record: record.key)),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def get(self, key: CapabilityKey) -> CompatibilityRecord:
|
|
54
|
+
for record in self.records:
|
|
55
|
+
if record.key == key:
|
|
56
|
+
return record
|
|
57
|
+
return _unknown_record(key)
|
|
58
|
+
|
|
59
|
+
class CompatibilityRegistry:
|
|
60
|
+
def __init__(self) -> None:
|
|
61
|
+
self._records: dict[CapabilityKey, CompatibilityRecord] = {}
|
|
62
|
+
|
|
63
|
+
def register(self, record: CompatibilityRecord) -> CompatibilityRecord:
|
|
64
|
+
existing = self._records.get(record.key)
|
|
65
|
+
if existing is None:
|
|
66
|
+
self._records[record.key] = record
|
|
67
|
+
return record
|
|
68
|
+
if existing == record:
|
|
69
|
+
return existing
|
|
70
|
+
raise DuplicateCompatibilityError(
|
|
71
|
+
f"conflicting compatibility record: {record.key!r}"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def get(self, key: CapabilityKey) -> CompatibilityRecord:
|
|
75
|
+
return self._records.get(key, _unknown_record(key))
|
|
76
|
+
|
|
77
|
+
def snapshot(self) -> tuple[CompatibilityRecord, ...]:
|
|
78
|
+
return tuple(self._records[key] for key in sorted(self._records))
|
|
79
|
+
|
|
80
|
+
def matrix(self) -> CompatibilityMatrix:
|
|
81
|
+
return CompatibilityMatrix(self.snapshot())
|
token_runtime/config.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict, dataclass
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import tempfile
|
|
8
|
+
from urllib.parse import urlparse
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
_ALLOWED_HOSTS = {"127.0.0.1", "::1", "localhost"}
|
|
12
|
+
_ALLOWED_KEYS = {"upstream", "host", "port", "state_dir"}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True, slots=True)
|
|
16
|
+
class TokenConfig:
|
|
17
|
+
upstream: str
|
|
18
|
+
host: str = "127.0.0.1"
|
|
19
|
+
port: int = 8788
|
|
20
|
+
state_dir: str = ""
|
|
21
|
+
|
|
22
|
+
def __post_init__(self) -> None:
|
|
23
|
+
parsed = urlparse(self.upstream)
|
|
24
|
+
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
|
|
25
|
+
raise ValueError("upstream must be an http(s) URL")
|
|
26
|
+
if self.host not in _ALLOWED_HOSTS:
|
|
27
|
+
raise ValueError("V1 gateway bind must be loopback")
|
|
28
|
+
if not 1 <= int(self.port) <= 65535:
|
|
29
|
+
raise ValueError("port must be in 1..65535")
|
|
30
|
+
if not self.state_dir:
|
|
31
|
+
raise ValueError("state_dir is required")
|
|
32
|
+
|
|
33
|
+
def default_config_path(home: str | Path | None = None) -> Path:
|
|
34
|
+
base = Path(home) if home is not None else Path.home()
|
|
35
|
+
return base / ".config" / "token" / "config.json"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def default_state_dir(home: str | Path | None = None) -> Path:
|
|
39
|
+
base = Path(home) if home is not None else Path.home()
|
|
40
|
+
return base / ".local" / "state" / "token"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def load_config(path: str | Path) -> TokenConfig:
|
|
44
|
+
data = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
45
|
+
if not isinstance(data, dict):
|
|
46
|
+
raise ValueError("config must be a JSON object")
|
|
47
|
+
unknown = set(data) - _ALLOWED_KEYS
|
|
48
|
+
if unknown:
|
|
49
|
+
raise ValueError(f"unknown config fields: {sorted(unknown)}")
|
|
50
|
+
missing = _ALLOWED_KEYS - set(data)
|
|
51
|
+
if missing:
|
|
52
|
+
raise ValueError(f"missing config fields: {sorted(missing)}")
|
|
53
|
+
return TokenConfig(**data)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def save_config(config: TokenConfig, path: str | Path) -> None:
|
|
57
|
+
target = Path(path)
|
|
58
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
59
|
+
content = json.dumps(asdict(config), indent=2, sort_keys=True) + "\n"
|
|
60
|
+
fd, tmp_name = tempfile.mkstemp(prefix=".token-config-", dir=target.parent)
|
|
61
|
+
try:
|
|
62
|
+
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
|
63
|
+
handle.write(content)
|
|
64
|
+
handle.flush()
|
|
65
|
+
os.fsync(handle.fileno())
|
|
66
|
+
os.chmod(tmp_name, 0o600)
|
|
67
|
+
os.replace(tmp_name, target)
|
|
68
|
+
if os.name == "posix":
|
|
69
|
+
os.chmod(target, 0o600)
|
|
70
|
+
finally:
|
|
71
|
+
if os.path.exists(tmp_name):
|
|
72
|
+
os.unlink(tmp_name)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True, slots=True)
|
|
8
|
+
class ConformanceResult:
|
|
9
|
+
name: str
|
|
10
|
+
passed: bool
|
|
11
|
+
detail: str
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ConformanceFailure(AssertionError):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run_conformance(
|
|
19
|
+
cases: Mapping[str, Callable[[], bool]],
|
|
20
|
+
) -> tuple[ConformanceResult, ...]:
|
|
21
|
+
results: list[ConformanceResult] = []
|
|
22
|
+
for name in sorted(cases):
|
|
23
|
+
try:
|
|
24
|
+
passed = cases[name]() is True
|
|
25
|
+
detail = "passed" if passed else "returned_false"
|
|
26
|
+
except Exception as exc:
|
|
27
|
+
passed = False
|
|
28
|
+
detail = f"{type(exc).__name__}: {exc}"
|
|
29
|
+
results.append(ConformanceResult(name=name, passed=passed, detail=detail))
|
|
30
|
+
return tuple(results)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def require_conformance(results: Sequence[ConformanceResult]) -> None:
|
|
34
|
+
failed = sorted(result.name for result in results if not result.passed)
|
|
35
|
+
if failed:
|
|
36
|
+
raise ConformanceFailure(f"conformance failed: {', '.join(failed)}")
|