Spaces:
Running on Zero
Running on Zero
build-store: mirror-persisted memory cache (survives rebuilds)
Browse files418 unmodified lines41942042142242342434 unmodified lines459460461462463464465466418 unmodified lines"progress_pushes": pushes}def stage_build_store() -> dict:"""Rebuild the frozen memory store: one full benchmark-locomo deployment runwith the paper's hyperparameters (m=15, t_pages=15, k=15), gpt-oss-20b via34 unmodified lines"--memory-cache-dir", "/app/.trajpatch_cache_build","--verbose",]result = _run_with_progress(cmd, env, WORK / "store_build_log.txt", "build_store",interval_s=300)payload: dict = {"cmd_head": cmd[:12], "result": {k: v for k, v in result.items() if k != "log"},"log_tail": result["log"][-4000:]}418 unmodified lines41942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045134 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520418 unmodified lines"progress_pushes": pushes}BUILD_CACHE_DIR = Path("/app/.trajpatch_cache_build")def _restore_build_cache(api: HfApi) -> str | None:"""Download the newest persisted memory-cache tarball from the mirror andextract it under /app so a rebuilt Space resumes instead of restarting."""files = api.list_repo_files(repo_id=DEST_MIRROR, repo_type="dataset")match = [f for f in files if f.endswith("buildstore_cache.tar")]if not match:return Nonelocal = api.hf_hub_download(repo_id=DEST_MIRROR, repo_type="dataset",filename=sorted(match)[-1])with tarfile.open(local) as tar:tar.extractall("/app")return sorted(match)[-1]def _persist_build_cache(api: HfApi) -> str | None:if not BUILD_CACHE_DIR.exists():return Nonetar_path = WORK / "buildstore_cache.tar"with tarfile.open(tar_path, "w") as tar:tar.add(BUILD_CACHE_DIR, arcname=".trajpatch_cache_build")push_file("buildstore_cache", tar_path.read_bytes())return tar_path.namedef stage_build_store() -> dict:"""Rebuild the frozen memory store: one full benchmark-locomo deployment runwith the paper's hyperparameters (m=15, t_pages=15, k=15), gpt-oss-20b via34 unmodified lines"--memory-cache-dir", "/app/.trajpatch_cache_build","--verbose",]restored = _restore_build_cache(api)result_log = WORK / "store_build_log.txt"log_path = result_loglog_path.parent.mkdir(parents=True, exist_ok=True)proc = subprocess.Popen(cmd, stdout=open(log_path, "w"),stderr=subprocess.STDOUT, text=True, env=env)pushes = 0while proc.poll() is None:time.sleep(300)pushes += 1try:tail = log_path.read_text(encoding="utf-8", errors="replace")[-3000:]push_file("build_store_progress", (json.dumps({"stage": "build_store", "running": True,"cache_restored_from": restored,"elapsed_pushes": pushes, "tail": tail})).encode("utf-8"))if pushes % 4 == 0: # persist the memory cache every ~20 min_persist_build_cache(api)except Exception: # noqa: BLE001 - progress/cache pushes must never kill the stagepasscode = proc.wait()result = {"returncode": code,"log": log_path.read_text(encoding="utf-8", errors="replace"),"progress_pushes": pushes}try:_persist_build_cache(api)except Exception: # noqa: BLE001passpayload: dict = {"cmd_head": cmd[:12], "result": {k: v for k, v in result.items() if k != "log"},"log_tail": result["log"][-4000:]}
executor_entrypoint.py+56-2