zefancainlp's picture
download
raw
2.83 kB
"""Exact three-update API boundary; no paid work while importing/preparing."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import threading
import time
NEW_STEPS = 3
IMAGE = 'sha256:63e5cf4304efec771bdf3ad56291db7ad0168d86ea8d34a7b4629e1ce43d905b'
IMAGE_MANIFEST = 'sha256:10c2bd0309edcea7cdc78723f9b8a8e893995c1b014d8305c33cf7b38876a075'
def sha(value):
return hashlib.sha256(value.encode() if isinstance(value, str) else value).hexdigest()
def read(path):
return json.loads(Path(path).read_text())
def atomic(path, value):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(path.name + f'.tmp-{os.getpid()}-{threading.get_ident()}')
temporary.write_text(json.dumps(value, indent=2, ensure_ascii=False, allow_nan=False))
temporary.replace(path)
def install_api_guard(transport, root):
original = transport.Ledger.require_paid_authorization
def guarded(ledger):
original(ledger)
if os.environ.get('TTT_ALLOW_KERNEL_PRIVATE_API3') != '1':
raise transport.RequestSafetyError('Explicit Kernel private-entry launch gate closed')
if not 1 <= ledger.logical_step <= NEW_STEPS:
raise transport.RequestSafetyError('Only NEW Steps 1..3; no Step0 or fourth batch')
if Path(root, 'control/user_stop.json').exists():
raise transport.RequestSafetyError('Human stop; no new paid requests')
path = Path(root, 'control/paid_steps', f'{ledger.logical_step:02d}.json')
with ledger.lock:
if not path.exists():
atomic(path, dict(logical_step=ledger.logical_step, maximum_new_steps=3,
expected_episodes=512, time=time.time()))
transport.Ledger.require_paid_authorization = guarded
def verify_ready(root):
root = Path(root)
ready = read(root / 'control/ready.json')
if not ready.get('ready') or ready['maximum_new_steps'] != 3 or ready['paid_preflight_requests'] != 0:
raise RuntimeError('Missing completed three-step preflight')
for relative, expected in ready['files'].items():
if sha((root / relative).read_bytes()) != expected:
raise RuntimeError('Frozen source/config changed: ' + relative)
if (root / 'control/user_stop.json').exists():
raise RuntimeError('User stopped this experiment')
for name in ['actor.json', 'actor_create_intent.json']:
if (root / 'control' / name).exists():
raise RuntimeError('Existing API actor/intent; never fresh-restart or reset Adam')
path = root / 'run/api_audit/api_requests.jsonl'
if path.exists() and path.stat().st_size:
raise RuntimeError('Existing paid history must be reconciled before continuation')
return ready

Xet Storage Details

Size:
2.83 kB
·
Xet hash:
381b10d4f86284348fa486561f40460a971c69e8cebe2b53545340b72cfde609

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.