from pathlib import Path
import json,hashlib,shutil,subprocess,sys,platform
base=Path(__file__).resolve().parent; repo=base/'working/repository'
def H(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest()
def version(exe,args):
    if not exe: return None
    try:
        r=subprocess.run([exe,*args],cwd=repo,text=True,capture_output=True,timeout=10)
        return (r.stdout or r.stderr).strip() or None
    except Exception as e:
        return f'UNAVAILABLE:{type(e).__name__}'
turbo=shutil.which('turbo')
if turbo is None:
    local=repo/'node_modules/.bin/turbo'
    if local.exists(): turbo=str(local.resolve())
node=shutil.which('node')
print(json.dumps({
  'turbo': {'executable': str(Path(turbo).resolve()) if turbo else None, 'version': version(turbo,['--version']) if turbo else None, 'status':'AVAILABLE' if turbo else 'TURBO_UNAVAILABLE'},
  'node': {'executable': str(Path(node).resolve()) if node else None, 'version': version(node,['--version']) if node else None},
  'python': {'executable': str(Path(sys.executable).resolve()), 'version': platform.python_version()},
  'platform': {'system':platform.system(),'release':platform.release(),'machine':platform.machine()},
  'cwd':str(repo.resolve()),
  'turbo_json_sha256':H(repo/'turbo.json'),
  'package_lock_sha256':H(repo/'package-lock.json'),
  'execution_controls': {'env_mode':'loose','cache_scope':'local:rw,remote:','remote_cache':'disabled_by_runner','network_download':'forbidden','telemetry':'disabled'},
  'cache_boundary': str((repo/'.turbo').resolve())
},indent=2))
