from pathlib import Path
import json, shutil, subprocess, sys
root=Path(__file__).resolve().parent/'work/repository'
artifact=root/'prepared_store/artifacts/fixture-external-1.0.0.tgz'
store=root/'prepared_store/pnpm-store'
if not artifact.exists():
 print(json.dumps({'status':'OFFLINE_ARTIFACT_BOUNDARY_MISSING','failure_layer':'OFFLINE_ARTIFACT_AVAILABILITY','artifact':str(artifact),'claim':'Required packaged local artifact is absent; pnpm success is not fabricated.'},indent=2)); raise SystemExit(24)
pnpm=shutil.which('pnpm')
if not pnpm:
 print(json.dumps({'status':'PNPM_UNAVAILABLE','claim':'No runtime execution performed; record missing evidence, do not simulate success.'},indent=2)); raise SystemExit(3)
# The assessed run owns only fixture-local state. Never delete or alter a host/global pnpm store.
if store.exists(): shutil.rmtree(store)
for nm in [root/'node_modules', *root.glob('apps/*/node_modules'), *root.glob('packages/*/node_modules'), *root.glob('services/*/node_modules'), *root.glob('libs/*/node_modules')]:
 if nm.exists(): shutil.rmtree(nm)
seed=[pnpm,'store','add',str(artifact),'--store-dir',str(store)]
print('SEED', ' '.join(seed))
r1=subprocess.run(seed,cwd=root,text=True,capture_output=True)
print(r1.stdout); print(r1.stderr,file=sys.stderr)
if r1.returncode:
 print(json.dumps({'status':'LOCAL_STORE_PROVISION_FAILED','failure_layer':'RUNTIME_OR_TOOL_CONTEXT','returncode':r1.returncode},indent=2)); raise SystemExit(r1.returncode)
cmd=[pnpm,'install','--frozen-lockfile','--offline','--ignore-scripts','--store-dir',str(store)]
print('RUN', ' '.join(cmd))
r2=subprocess.run(cmd,cwd=root,text=True,capture_output=True)
print(r2.stdout); print(r2.stderr,file=sys.stderr)
print(json.dumps({'status':'PNPM_PROBE_COMPLETE','returncode':r2.returncode,'pnpm':pnpm,'store_dir':str(store),'artifact':str(artifact)},indent=2))
raise SystemExit(r2.returncode)
