Add ML Intern Hub artifact metadata (#225)
Browse files* Add ML Intern Hub artifact metadata
Co-authored-by: OpenAI Codex <codex@openai.com>
* Extend Hub artifact hooks to sandbox bash
Co-authored-by: OpenAI Codex <codex@openai.com>
* Create Hub artifact collections at session start
Co-authored-by: OpenAI Codex <codex@openai.com>
* Address Hub artifact PR review comments
Co-authored-by: OpenAI Codex <codex@openai.com>
* Respect Hub collection title limits
Co-authored-by: OpenAI Codex <codex@openai.com>
* Shorten collection session UUID fragments
Co-authored-by: OpenAI Codex <codex@openai.com>
---------
Co-authored-by: OpenAI Codex <codex@openai.com>
Files changed (10)
- agent/core/agent_loop.py+2
- agent/core/hub_artifacts.pyAdded+765
- agent/tools/hf_repo_files_tool.py+14-2
- agent/tools/hf_repo_git_tool.py+15-2
- agent/tools/jobs_tool.py+26
- agent/tools/local_tools.py+6-1
- agent/tools/sandbox_tool.py+9
- backend/session_manager.py+30-1
- tests/unit/test_hub_artifacts.pyAdded+505
- tests/unit/test_session_manager_persistence.py+57
25 unmodified lines2627282930311966 unmodified lines19981999200020012002200325 unmodified linesfrom agent.messaging.gateway import NotificationGatewayfrom agent.core import telemetryfrom agent.core.doom_loop import check_for_doom_loopfrom agent.core.llm_params import _resolve_llm_paramsfrom agent.core.prompt_caching import with_prompt_cachingfrom agent.core.session import Event, OpType, Session1966 unmodified lines)if session_holder is not None:session_holder[0] = sessionlogger.info("Agent loop started")# Retry any failed uploads from previous sessions (fire-and-forget).25 unmodified lines262728293031321966 unmodified lines199920002001200220032004200525 unmodified linesfrom agent.messaging.gateway import NotificationGatewayfrom agent.core import telemetryfrom agent.core.doom_loop import check_for_doom_loopfrom agent.core.hub_artifacts import start_session_artifact_collection_taskfrom agent.core.llm_params import _resolve_llm_paramsfrom agent.core.prompt_caching import with_prompt_cachingfrom agent.core.session import Event, OpType, Session1966 unmodified lines)if session_holder is not None:session_holder[0] = sessionstart_session_artifact_collection_task(session, token=hf_token)logger.info("Agent loop started")# Retry any failed uploads from previous sessions (fire-and-forget).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765"""Best-effort Hub metadata for artifacts generated by ML Intern sessions."""
import asyncioimport base64import loggingimport reimport shleximport tempfileimport textwrapfrom datetime import datetimefrom pathlib import Pathfrom typing import Any
from huggingface_hub import HfApi, hf_hub_downloadfrom huggingface_hub.repocard import metadata_load, metadata_savefrom huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError
logger = logging.getLogger(__name__)
ML_INTERN_TAG = "ml-intern"SUPPORTED_REPO_TYPES = {"model", "dataset", "space"}PROVENANCE_MARKER = "<!-- ml-intern-provenance -->"_COLLECTION_TITLE_PREFIX = "ml-intern-artifacts"_COLLECTION_TITLE_MAX_LENGTH = 59_UUID_SESSION_ID_RE = re.compile( r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-" r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")_KNOWN_ARTIFACTS_ATTR = "_ml_intern_known_hub_artifacts"_REGISTERED_ARTIFACTS_ATTR = "_ml_intern_registered_hub_artifacts"_COLLECTION_SLUG_ATTR = "_ml_intern_artifact_collection_slug"_COLLECTION_TASK_ATTR = "_ml_intern_artifact_collection_task"_SESSION_ARTIFACT_SET_FALLBACK: dict[tuple[int, str], set[str]] = {}_USAGE_HEADING_RE = re.compile( r"^#{2,6}\s+(usage|how to use|using this (model|dataset)|use this (model|dataset))\b", re.IGNORECASE | re.MULTILINE,)_FRONT_MATTER_RE = re.compile(r"\A---\s*\n.*?\n---\s*\n?", re.DOTALL)
def _safe_session_id(session: Any) -> str: raw = str(getattr(session, "session_id", "") or "unknown-session") safe = re.sub(r"[^A-Za-z0-9._-]+", "-", raw).strip("-") return safe or "unknown-session"
def session_artifact_date(session: Any) -> str: """Return the YYYY-MM-DD partition date for a session.""" raw = getattr(session, "session_start_time", None) if raw: try: return datetime.fromisoformat(str(raw).replace("Z", "+00:00")).strftime( "%Y-%m-%d" ) except ValueError: logger.debug("Could not parse session_start_time=%r", raw) return datetime.utcnow().strftime("%Y-%m-%d")
def _collection_session_id_fragment(session: Any) -> str: safe_id = _safe_session_id(session) if _UUID_SESSION_ID_RE.match(safe_id): return safe_id[:8] stem = f"{_COLLECTION_TITLE_PREFIX}-{session_artifact_date(session)}-" max_id_length = max(1, _COLLECTION_TITLE_MAX_LENGTH - len(stem)) if len(safe_id) <= max_id_length: return safe_id return safe_id[:max_id_length].rstrip("-._") or safe_id[:max_id_length]
def artifact_collection_title(session: Any) -> str: return ( f"{_COLLECTION_TITLE_PREFIX}-{session_artifact_date(session)}-" f"{_collection_session_id_fragment(session)}" )
def _artifact_key(repo_id: str, repo_type: str | None) -> str: return f"{repo_type or 'model'}:{repo_id}"
def _session_artifact_set(session: Any, attr: str) -> set[str]: current = getattr(session, attr, None) if isinstance(current, set): return current current = set() try: setattr(session, attr, current) except Exception: logger.warning( "Could not attach %s to session; using process-local fallback state", attr, ) return _SESSION_ARTIFACT_SET_FALLBACK.setdefault((id(session), attr), set()) return current
def remember_hub_artifact(session: Any, repo_id: str, repo_type: str | None) -> None: if session is None or not repo_id: return _session_artifact_set(session, _KNOWN_ARTIFACTS_ATTR).add( _artifact_key(repo_id, repo_type) )
def is_known_hub_artifact(session: Any, repo_id: str, repo_type: str | None) -> bool: if session is None or not repo_id: return False return _artifact_key(repo_id, repo_type) in _session_artifact_set( session, _KNOWN_ARTIFACTS_ATTR )
def _merge_tags(metadata: dict[str, Any], tag: str = ML_INTERN_TAG) -> dict[str, Any]: merged = dict(metadata) raw_tags = merged.get("tags") if raw_tags is None: tags: list[str] = [] elif isinstance(raw_tags, str): tags = [raw_tags] elif isinstance(raw_tags, list): tags = [str(item) for item in raw_tags] else: tags = [str(raw_tags)]
if tag not in tags: tags.append(tag) merged["tags"] = tags return merged
def _metadata_from_content(content: str) -> dict[str, Any]: with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir) / "README.md" path.write_text(content, encoding="utf-8") return metadata_load(path) or {}
def _content_with_metadata(content: str, metadata: dict[str, Any]) -> str: with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir) / "README.md" path.write_text(content, encoding="utf-8") metadata_save(path, metadata) return path.read_text(encoding="utf-8")
def _body_without_metadata(content: str) -> str: return _FRONT_MATTER_RE.sub("", content, count=1).strip()
def _append_section(content: str, section: str) -> str: base = content.rstrip() if base: return f"{base}\n\n{section.strip()}\n" return f"{section.strip()}\n"
def _provenance_section(repo_type: str) -> str: label = {"model": "model", "dataset": "dataset"}.get(repo_type, "Hub") return f"""{PROVENANCE_MARKER}## Generated by ML Intern
This {label} repository was generated by [ML Intern](https://github.com/huggingface/ml-intern), an agent for machine learning research and development on the Hugging Face Hub.
- Try ML Intern: https://smolagents-ml-intern.hf.space- Source code: https://github.com/huggingface/ml-intern"""
def _usage_section(repo_id: str, repo_type: str) -> str: if repo_type == "dataset": return f"""## Usage
```pythonfrom datasets import load_dataset
dataset = load_dataset("{repo_id}")```"""
return f"""## Usage
```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "{repo_id}"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id)```
For non-causal architectures, replace `AutoModelForCausalLM` with the appropriate `AutoModel` class."""
def augment_repo_card_content( content: str | None, repo_id: str, repo_type: str = "model", *, extra_metadata: dict[str, Any] | None = None,) -> str: """Return README content with ML Intern metadata and provenance added.""" repo_type = repo_type or "model" content = content or "" metadata = _metadata_from_content(content) if extra_metadata: metadata = {**extra_metadata, **metadata} metadata = _merge_tags(metadata) updated = _content_with_metadata(content, metadata)
if not _body_without_metadata(updated): updated = _append_section(updated, f"# {repo_id}")
if repo_type in {"model", "dataset"} and PROVENANCE_MARKER not in updated: updated = _append_section(updated, _provenance_section(repo_type)) if not _USAGE_HEADING_RE.search(content): updated = _append_section(updated, _usage_section(repo_id, repo_type))
return updated
def _read_remote_readme( api: Any, repo_id: str, repo_type: str, *, token: str | bool | None = None,) -> str: token_value = token if token is not None else getattr(api, "token", None) try: readme_path = hf_hub_download( repo_id=repo_id, filename="README.md", repo_type=repo_type, token=token_value, ) except (EntryNotFoundError, RepositoryNotFoundError): return "" return Path(readme_path).read_text(encoding="utf-8")
def _update_repo_card( api: Any, repo_id: str, repo_type: str, *, token: str | bool | None = None, extra_metadata: dict[str, Any] | None = None,) -> None: current = _read_remote_readme(api, repo_id, repo_type, token=token) updated = augment_repo_card_content( current, repo_id, repo_type, extra_metadata=extra_metadata, ) if updated == current: return api.upload_file( path_or_fileobj=updated.encode("utf-8"), path_in_repo="README.md", repo_id=repo_id, repo_type=repo_type, token=token, commit_message="Update ML Intern artifact metadata", )
def _ensure_collection_slug( api: Any, session: Any, *, token: str | bool | None = None,) -> str | None: slug = getattr(session, _COLLECTION_SLUG_ATTR, None) if slug: return slug
title = artifact_collection_title(session) collection = api.create_collection( title=title, description=( f"Artifacts generated by ML Intern session {_safe_session_id(session)} " f"on {session_artifact_date(session)}." ), private=True, exists_ok=True, token=token, ) slug = getattr(collection, "slug", None) if slug: setattr(session, _COLLECTION_SLUG_ATTR, slug) return slug
async def ensure_session_artifact_collection( session: Any, *, token: str | bool | None = None,) -> str | None: """Create/cache the per-session artifact collection without raising.""" if session is None or not getattr(session, "session_id", None): return None token_value = token if token is not None else getattr(session, "hf_token", None) if not token_value: return None
try: api = HfApi(token=token_value) return await asyncio.to_thread( _ensure_collection_slug, api, session, token=token_value, ) except Exception as e: logger.warning( "ML Intern session collection creation failed for %s: %s", _safe_session_id(session), e, ) return None
def start_session_artifact_collection_task( session: Any, *, token: str | bool | None = None,) -> asyncio.Task | None: """Schedule best-effort collection creation for a newly started session.""" if session is None or not getattr(session, "session_id", None): return None if getattr(session, _COLLECTION_SLUG_ATTR, None): return None
token_value = token if token is not None else getattr(session, "hf_token", None) if not token_value: return None
existing = getattr(session, _COLLECTION_TASK_ATTR, None) if isinstance(existing, asyncio.Task) and not existing.done(): return existing
try: loop = asyncio.get_running_loop() except RuntimeError: return None
async def _run() -> None: await ensure_session_artifact_collection(session, token=token_value)
task = loop.create_task(_run()) try: setattr(session, _COLLECTION_TASK_ATTR, task) except Exception: logger.debug("Could not attach ML Intern collection task to session") return task
def _add_to_collection( api: Any, session: Any, repo_id: str, repo_type: str, *, token: str | bool | None = None,) -> None: slug = _ensure_collection_slug(api, session, token=token) if not slug: return api.add_collection_item( collection_slug=slug, item_id=repo_id, item_type=repo_type, note=( f"Generated by ML Intern session {_safe_session_id(session)} " f"on {session_artifact_date(session)}." ), exists_ok=True, token=token, )
def register_hub_artifact( api: Any, repo_id: str, repo_type: str = "model", *, session: Any = None, token: str | bool | None = None, extra_metadata: dict[str, Any] | None = None, force: bool = False,) -> bool: """Tag, card, and collection-register a Hub artifact without raising.""" if session is None or not repo_id: return False repo_type = repo_type or "model" if repo_type not in SUPPORTED_REPO_TYPES: return False
key = _artifact_key(repo_id, repo_type) remember_hub_artifact(session, repo_id, repo_type) registered = _session_artifact_set(session, _REGISTERED_ARTIFACTS_ATTR) if key in registered and not force: return True
token_value = token if token is not None else getattr(api, "token", None) card_updated = False collection_updated = False try: _update_repo_card( api, repo_id, repo_type, token=token_value, extra_metadata=extra_metadata, ) card_updated = True except Exception as e: logger.debug("ML Intern repo-card update failed for %s: %s", repo_id, e)
try: _add_to_collection(api, session, repo_id, repo_type, token=token_value) collection_updated = True except Exception as e: logger.debug("ML Intern collection update failed for %s: %s", repo_id, e)
if card_updated and collection_updated: registered.add(key) return True return False
def build_hub_artifact_sitecustomize(session: Any) -> str: """Build standalone sitecustomize.py code for HF Jobs Python processes.""" if session is None or not getattr(session, "session_id", None): return ""
session_id = _safe_session_id(session) session_date = session_artifact_date(session) collection_title = artifact_collection_title(session) collection_slug = getattr(session, _COLLECTION_SLUG_ATTR, None)
return ( textwrap.dedent( f""" # Auto-generated by ML Intern. Best-effort Hub artifact metadata only. def _install_ml_intern_artifact_hooks(): import os import re import tempfile from pathlib import Path
try: import huggingface_hub as _hub from huggingface_hub import HfApi, hf_hub_download from huggingface_hub.repocard import metadata_load, metadata_save from huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError except Exception: return
session_id = {session_id!r} session_date = {session_date!r} collection_title = {collection_title!r} tag = {ML_INTERN_TAG!r} marker = {PROVENANCE_MARKER!r} supported = {sorted(SUPPORTED_REPO_TYPES)!r} registering = False collection_slug = {collection_slug!r} registered = set() usage_re = re.compile( r"^#{{2,6}}\\s+(usage|how to use|using this (model|dataset)|use this (model|dataset))\\b", re.IGNORECASE | re.MULTILINE, ) front_matter_re = re.compile(r"\\A---\\s*\\n.*?\\n---\\s*\\n?", re.DOTALL)
def _token(value=None, api=None): if isinstance(value, str) and value: return value api_token = getattr(api, "token", None) if isinstance(api_token, str) and api_token: return api_token return ( os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") or None )
def _merge_tags(metadata): metadata = dict(metadata or {{}}) raw_tags = metadata.get("tags") if raw_tags is None: tags = [] elif isinstance(raw_tags, str): tags = [raw_tags] elif isinstance(raw_tags, list): tags = [str(item) for item in raw_tags] else: tags = [str(raw_tags)] if tag not in tags: tags.append(tag) metadata["tags"] = tags return metadata
def _metadata_from_content(content): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir) / "README.md" path.write_text(content or "", encoding="utf-8") return metadata_load(path) or {{}}
def _content_with_metadata(content, metadata): with tempfile.TemporaryDirectory() as tmp_dir: path = Path(tmp_dir) / "README.md" path.write_text(content or "", encoding="utf-8") metadata_save(path, metadata) return path.read_text(encoding="utf-8")
def _body_without_metadata(content): return front_matter_re.sub("", content or "", count=1).strip()
def _append_section(content, section): base = (content or "").rstrip() if base: return base + "\\n\\n" + section.strip() + "\\n" return section.strip() + "\\n"
def _provenance(repo_type): label = {{"model": "model", "dataset": "dataset"}}.get( repo_type, "Hub" ) return ( marker + "\\n## Generated by ML Intern\\n\\n" + f"This {{label}} repository was generated by [ML Intern](https://github.com/huggingface/ml-intern), an agent for machine learning research and development on the Hugging Face Hub.\\n\\n" + "- Try ML Intern: https://smolagents-ml-intern.hf.space\\n" + "- Source code: https://github.com/huggingface/ml-intern\\n" )
def _usage(repo_id, repo_type): if repo_type == "dataset": return ( "## Usage\\n\\n" "```python\\n" "from datasets import load_dataset\\n\\n" f"dataset = load_dataset({{repo_id!r}})\\n" "```\\n" ) return ( "## Usage\\n\\n" "```python\\n" "from transformers import AutoModelForCausalLM, AutoTokenizer\\n\\n" f"model_id = {{repo_id!r}}\\n" "tokenizer = AutoTokenizer.from_pretrained(model_id)\\n" "model = AutoModelForCausalLM.from_pretrained(model_id)\\n" "```\\n\\n" "For non-causal architectures, replace `AutoModelForCausalLM` with the appropriate `AutoModel` class.\\n" )
def _augment(content, repo_id, repo_type, extra_metadata=None): metadata = _metadata_from_content(content or "") if extra_metadata: metadata = {{**extra_metadata, **metadata}} updated = _content_with_metadata(content or "", _merge_tags(metadata)) if not _body_without_metadata(updated): updated = _append_section(updated, f"# {{repo_id}}") if repo_type in {{"model", "dataset"}} and marker not in updated: updated = _append_section(updated, _provenance(repo_type)) if not usage_re.search(content or ""): updated = _append_section(updated, _usage(repo_id, repo_type)) return updated
def _readme(api, repo_id, repo_type, token_value): try: path = hf_hub_download( repo_id=repo_id, filename="README.md", repo_type=repo_type, token=token_value, ) except (EntryNotFoundError, RepositoryNotFoundError): return "" return Path(path).read_text(encoding="utf-8")
def _ensure_collection(api, token_value): nonlocal collection_slug if collection_slug: return collection_slug collection = api.create_collection( title=collection_title, description=( f"Artifacts generated by ML Intern session {{session_id}} " f"on {{session_date}}." ), private=True, exists_ok=True, token=token_value, ) collection_slug = getattr(collection, "slug", None) return collection_slug
def _register( repo_id, repo_type="model", token_value=None, extra_metadata=None, force=False, ): nonlocal registering if registering or not repo_id: return repo_type = repo_type or "model" if repo_type not in supported: return key = f"{{repo_type}}:{{repo_id}}" if key in registered and not force: return registering = True try: token_value = _token(token_value) api = HfApi(token=token_value) try: current = _readme(api, repo_id, repo_type, token_value) updated = _augment( current, repo_id, repo_type, extra_metadata=extra_metadata ) if updated != current: _original_upload_file( api, path_or_fileobj=updated.encode("utf-8"), path_in_repo="README.md", repo_id=repo_id, repo_type=repo_type, token=token_value, commit_message="Update ML Intern artifact metadata", ) except Exception: pass try: slug = _ensure_collection(api, token_value) if slug: api.add_collection_item( collection_slug=slug, item_id=repo_id, item_type=repo_type, note=( f"Generated by ML Intern session {{session_id}} " f"on {{session_date}}." ), exists_ok=True, token=token_value, ) except Exception: pass registered.add(key) finally: registering = False
_original_create_repo = HfApi.create_repo _original_upload_file = HfApi.upload_file _original_upload_folder = getattr(HfApi, "upload_folder", None) _original_create_commit = getattr(HfApi, "create_commit", None)
def _repo_id(args, kwargs): return kwargs.get("repo_id") or (args[0] if args else None)
def _repo_type(kwargs): return kwargs.get("repo_type") or "model"
def _patched_create_repo(self, *args, **kwargs): result = _original_create_repo(self, *args, **kwargs) repo_id = _repo_id(args, kwargs) repo_type = _repo_type(kwargs) extra = None if repo_type == "space" and kwargs.get("space_sdk"): extra = {{"sdk": kwargs.get("space_sdk")}} _register(repo_id, repo_type, _token(kwargs.get("token"), self), extra) return result
def _patched_upload_file(self, *args, **kwargs): result = _original_upload_file(self, *args, **kwargs) if not kwargs.get("create_pr"): force = kwargs.get("path_in_repo") == "README.md" _register( kwargs.get("repo_id"), _repo_type(kwargs), _token(kwargs.get("token"), self), force=force, ) return result
def _patched_upload_folder(self, *args, **kwargs): result = _original_upload_folder(self, *args, **kwargs) if not kwargs.get("create_pr"): _register( kwargs.get("repo_id"), _repo_type(kwargs), _token(kwargs.get("token"), self), force=True, ) return result
def _patched_create_commit(self, *args, **kwargs): result = _original_create_commit(self, *args, **kwargs) if not kwargs.get("create_pr"): _register( _repo_id(args, kwargs), _repo_type(kwargs), _token(kwargs.get("token"), self), force=True, ) return result
HfApi.create_repo = _patched_create_repo HfApi.upload_file = _patched_upload_file if _original_upload_folder is not None: HfApi.upload_folder = _patched_upload_folder if _original_create_commit is not None: HfApi.create_commit = _patched_create_commit
def _patch_module_func(name, method_name): original = getattr(_hub, name, None) if original is None: return method = getattr(HfApi, method_name)
def _patched(*args, **kwargs): api = HfApi(token=_token(kwargs.get("token"))) return method(api, *args, **kwargs)
setattr(_hub, name, _patched)
_patch_module_func("create_repo", "create_repo") _patch_module_func("upload_file", "upload_file") if _original_upload_folder is not None: _patch_module_func("upload_folder", "upload_folder") if _original_create_commit is not None: _patch_module_func("create_commit", "create_commit")
try: _install_ml_intern_artifact_hooks() except Exception: pass """ ).strip() + "\n" )
def wrap_shell_command_with_hub_artifact_bootstrap( command: str, session: Any,) -> str: """Prefix a shell command so child Python processes load Hub hooks.""" sitecustomize = build_hub_artifact_sitecustomize(session) if not sitecustomize or not command: return command
encoded = base64.b64encode(sitecustomize.encode("utf-8")).decode("ascii") bootstrap = ( '_ml_intern_artifacts_dir="$(mktemp -d 2>/dev/null)" ' f"&& printf %s {shlex.quote(encoded)} | base64 -d " '> "$_ml_intern_artifacts_dir/sitecustomize.py" ' '&& export PYTHONPATH="$_ml_intern_artifacts_dir${PYTHONPATH:+:$PYTHONPATH}"' ) return f"{bootstrap}; {command}"9 unmodified lines10111213141523 unmodified lines3940414243444546167 unmodified lines214215216217218219123 unmodified lines3433443453463473483499 unmodified linesfrom huggingface_hub import HfApi, hf_hub_downloadfrom huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundErrorfrom agent.tools.types import ToolResultOperationType = Literal["list", "read", "upload", "delete"]23 unmodified linesclass HfRepoFilesTool:"""Tool for file operations on HF repos."""def __init__(self, hf_token: Optional[str] = None):self.api = HfApi(token=hf_token)async def execute(self, args: Dict[str, Any]) -> ToolResult:"""Execute the specified operation."""167 unmodified linescreate_pr=create_pr,)url = _build_repo_url(repo_id, repo_type)if create_pr and hasattr(result, "pr_url"):response = f"**Uploaded as PR**\n{result.pr_url}"123 unmodified lines"""Handler for agent tool router."""try:hf_token = session.hf_token if session else Nonetool = HfRepoFilesTool(hf_token=hf_token)result = await tool.execute(arguments)return result["formatted"], not result.get("isError", False)except Exception as e:9 unmodified lines1011121314151623 unmodified lines404142434445464748167 unmodified lines216217218219220221222223224225226227228229230231123 unmodified lines3553563573583593603619 unmodified linesfrom huggingface_hub import HfApi, hf_hub_downloadfrom huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundErrorfrom agent.core.hub_artifacts import is_known_hub_artifact, register_hub_artifactfrom agent.tools.types import ToolResultOperationType = Literal["list", "read", "upload", "delete"]23 unmodified linesclass HfRepoFilesTool:"""Tool for file operations on HF repos."""def __init__(self, hf_token: Optional[str] = None, session: Any = None):self.api = HfApi(token=hf_token)self.session = sessionasync def execute(self, args: Dict[str, Any]) -> ToolResult:"""Execute the specified operation."""167 unmodified linescreate_pr=create_pr,)if not create_pr and is_known_hub_artifact(self.session, repo_id, repo_type):await _async_call(register_hub_artifact,self.api,repo_id,repo_type,session=self.session,force=path == "README.md",)url = _build_repo_url(repo_id, repo_type)if create_pr and hasattr(result, "pr_url"):response = f"**Uploaded as PR**\n{result.pr_url}"123 unmodified lines"""Handler for agent tool router."""try:hf_token = session.hf_token if session else Nonetool = HfRepoFilesTool(hf_token=hf_token, session=session)result = await tool.execute(arguments)return result["formatted"], not result.get("isError", False)except Exception as e:
9 unmodified lines10111213141529 unmodified lines4546474849505152499 unmodified lines552553554555556557189 unmodified lines7477487497507517527539 unmodified linesfrom huggingface_hub import HfApifrom huggingface_hub.utils import RepositoryNotFoundErrorfrom agent.tools.types import ToolResultOperationType = Literal[29 unmodified linesclass HfRepoGitTool:"""Tool for git-like operations on HF repos."""def __init__(self, hf_token: Optional[str] = None):self.api = HfApi(token=hf_token)async def execute(self, args: Dict[str, Any]) -> ToolResult:"""Execute the specified operation."""499 unmodified lineskwargs["space_sdk"] = space_sdkresult = await _async_call(self.api.create_repo, **kwargs)return {"formatted": f"**Repository created:** {repo_id}\n**Private:** {private}\n{result}",189 unmodified lines"""Handler for agent tool router."""try:hf_token = session.hf_token if session else Nonetool = HfRepoGitTool(hf_token=hf_token)result = await tool.execute(arguments)return result["formatted"], not result.get("isError", False)except Exception as e:9 unmodified lines1011121314151629 unmodified lines464748495051525354499 unmodified lines554555556557558559560561562563564565566567568569570189 unmodified lines7607617627637647657669 unmodified linesfrom huggingface_hub import HfApifrom huggingface_hub.utils import RepositoryNotFoundErrorfrom agent.core.hub_artifacts import register_hub_artifactfrom agent.tools.types import ToolResultOperationType = Literal[29 unmodified linesclass HfRepoGitTool:"""Tool for git-like operations on HF repos."""def __init__(self, hf_token: Optional[str] = None, session: Any = None):self.api = HfApi(token=hf_token)self.session = sessionasync def execute(self, args: Dict[str, Any]) -> ToolResult:"""Execute the specified operation."""499 unmodified lineskwargs["space_sdk"] = space_sdkresult = await _async_call(self.api.create_repo, **kwargs)extra_metadata = Noneif repo_type == "space" and space_sdk:extra_metadata = {"sdk": space_sdk}await _async_call(register_hub_artifact,self.api,repo_id,repo_type,session=self.session,extra_metadata=extra_metadata,)return {"formatted": f"**Repository created:** {repo_id}\n**Private:** {private}\n{result}",189 unmodified lines"""Handler for agent tool router."""try:hf_token = session.hf_token if session else Nonetool = HfRepoGitTool(hf_token=hf_token, session=session)result = await tool.execute(arguments)return result["formatted"], not result.get("isError", False)except Exception as e:
8 unmodified lines910111213145 unmodified lines202122232425211 unmodified lines237238239240241242317 unmodified lines560561562563564565346 unmodified lines9129139149159169178 unmodified linesimport http.clientimport loggingimport refrom typing import Any, Awaitable, Callable, Dict, Literal, Optionalimport httpx5 unmodified linesis_billing_error,resolve_jobs_namespace,)from agent.core.session import Eventfrom agent.tools.trackio_seed import ensure_trackio_dashboardfrom agent.tools.types import ToolResult211 unmodified linesreturn _build_uv_command(script, with_deps, python, script_args)async def _async_call(func, *args, **kwargs):"""Wrap synchronous HfApi calls for async context"""return await asyncio.to_thread(func, *args, **kwargs)317 unmodified linesimage = args.get("image", "python:3.12")job_type = "Docker"# Run the jobflavor = args.get("hardware_flavor", "cpu-basic")timeout_str = args.get("timeout", "30m")346 unmodified linesimage = args.get("image", "python:3.12")job_type = "Docker"# Create scheduled jobscheduled_job = await _async_call(self.api.create_scheduled_job,8 unmodified lines91011121314155 unmodified lines21222324252627211 unmodified lines239240241242243244245246247248249250251252253254255256257258259260261262263264317 unmodified lines582583584585586587588589346 unmodified lines9369379389399409419429438 unmodified linesimport http.clientimport loggingimport reimport shlexfrom typing import Any, Awaitable, Callable, Dict, Literal, Optionalimport httpx5 unmodified linesis_billing_error,resolve_jobs_namespace,)from agent.core.hub_artifacts import build_hub_artifact_sitecustomizefrom agent.core.session import Eventfrom agent.tools.trackio_seed import ensure_trackio_dashboardfrom agent.tools.types import ToolResult211 unmodified linesreturn _build_uv_command(script, with_deps, python, script_args)def _wrap_command_with_artifact_bootstrap(command: list[str], session: Any = None) -> list[str]:"""Install sitecustomize hooks before the user command runs in HF Jobs."""sitecustomize = build_hub_artifact_sitecustomize(session)if not sitecustomize:return commandencoded = base64.b64encode(sitecustomize.encode("utf-8")).decode("ascii")original_command = shlex.join(command)shell = ('set -e; _ml_intern_artifacts_dir="$(mktemp -d)"; 'f"printf %s {shlex.quote(encoded)} | base64 -d "'> "$_ml_intern_artifacts_dir/sitecustomize.py"; ''export PYTHONPATH="$_ml_intern_artifacts_dir${PYTHONPATH:+:$PYTHONPATH}"; 'f"exec {original_command}")return ["/bin/sh", "-lc", shell]async def _async_call(func, *args, **kwargs):"""Wrap synchronous HfApi calls for async context"""return await asyncio.to_thread(func, *args, **kwargs)317 unmodified linesimage = args.get("image", "python:3.12")job_type = "Docker"command = _wrap_command_with_artifact_bootstrap(command, self.session)# Run the jobflavor = args.get("hardware_flavor", "cpu-basic")timeout_str = args.get("timeout", "30m")346 unmodified linesimage = args.get("image", "python:3.12")job_type = "Docker"command = _wrap_command_with_artifact_bootstrap(command, self.session)# Create scheduled jobscheduled_job = await _async_call(self.api.create_scheduled_job,
14 unmodified lines15161718192077 unmodified lines989910010110210310410510610714 unmodified linesfrom pathlib import Pathfrom typing import AnyMAX_OUTPUT_CHARS = 25_000MAX_LINE_LENGTH = 400077 unmodified lines# ── Handlers ────────────────────────────────────────────────────────────async def _bash_handler(args: dict[str, Any], **_kw) -> tuple[str, bool]:command = args.get("command", "")if not command:return "No command provided.", Falsework_dir = args.get("work_dir", ".")timeout = min(args.get("timeout") or DEFAULT_TIMEOUT, MAX_TIMEOUT)try:14 unmodified lines151617181920212277 unmodified lines10010110210310410510610710810911011111214 unmodified linesfrom pathlib import Pathfrom typing import Anyfrom agent.core.hub_artifacts import wrap_shell_command_with_hub_artifact_bootstrapMAX_OUTPUT_CHARS = 25_000MAX_LINE_LENGTH = 400077 unmodified lines# ── Handlers ────────────────────────────────────────────────────────────async def _bash_handler(args: dict[str, Any], session: Any = None, **_kw) -> tuple[str, bool]:command = args.get("command", "")if not command:return "No command provided.", Falsecommand = wrap_shell_command_with_hub_artifact_bootstrap(command, session)work_dir = args.get("work_dir", ".")timeout = min(args.get("timeout") or DEFAULT_TIMEOUT, MAX_TIMEOUT)try:
20 unmodified lines212223242526702 unmodified lines72973073173273373420 unmodified linesfrom huggingface_hub import HfApi, SpaceHardwarefrom agent.core.session import Eventfrom agent.tools.sandbox_client import Sandboxfrom agent.tools.trackio_seed import ensure_trackio_dashboard702 unmodified linesreturn "Sandbox is still starting. Please retry shortly.", Falsetry:result = await asyncio.to_thread(sb.call_tool, sandbox_tool_name, args)if result.success:output = result.output or "(no output)"20 unmodified lines21222324252627702 unmodified lines73073173273373473573673773873974074174274320 unmodified linesfrom huggingface_hub import HfApi, SpaceHardwarefrom agent.core.hub_artifacts import wrap_shell_command_with_hub_artifact_bootstrapfrom agent.core.session import Eventfrom agent.tools.sandbox_client import Sandboxfrom agent.tools.trackio_seed import ensure_trackio_dashboard702 unmodified linesreturn "Sandbox is still starting. Please retry shortly.", Falsetry:if sandbox_tool_name == "bash" and args.get("command"):args = {**args,"command": wrap_shell_command_with_hub_artifact_bootstrap(args["command"],session,),}result = await asyncio.to_thread(sb.call_tool, sandbox_tool_name, args)if result.success:output = result.output or "(no output)"
11 unmodified lines12131415161718192021113 unmodified lines135136137138139140270 unmodified lines411412413414415416150 unmodified lines56756856957057157215 unmodified lines58858959059159259380 unmodified lines67467567667767867968076 unmodified lines75775875976076176211 unmodified linesfrom agent.config import load_configfrom agent.core.agent_loop import process_submissionfrom agent.messaging.gateway import NotificationGatewayfrom agent.core.session import Event, OpType, Sessionfrom agent.core.session_persistence import get_session_storefrom agent.core.tools import ToolRouter# Get project root (parent of backend directory)PROJECT_ROOT = Path(__file__).parent.parent113 unmodified linesself.sessions: dict[str, AgentSession] = {}self._lock = asyncio.Lock()self.persistence_store = Noneasync def start(self) -> None:"""Start shared background resources."""270 unmodified linessession.sandbox_preload_cancel_event = Noneself._start_cpu_sandbox_preload(agent_session)async def _clear_persisted_sandbox_metadata(self, session_id: str) -> None:try:await self._store().update_session_fields(150 unmodified linesexisting,preload_sandbox=preload_sandbox,)return existingreturn None15 unmodified linesexisting,preload_sandbox=preload_sandbox,)return existingreturn None80 unmodified lineshf_token=hf_token,hf_username=hf_username,)return startedif preload_sandbox:self._start_cpu_sandbox_preload(agent_session)logger.info("Restored session %s for user %s", session_id, owner or user_id)76 unmodified linesevent_queue=event_queue,tool_router=tool_router,)await self.persist_session_snapshot(agent_session, runtime_state="idle")self._start_cpu_sandbox_preload(agent_session)11 unmodified lines1213141516171819202122113 unmodified lines136137138139140141142270 unmodified lines413414415416417418419420421422423424425426427428429430431432433434435436437438439440150 unmodified lines59159259359459559659715 unmodified lines61361461561661761861980 unmodified lines70070170270370470570670770876 unmodified lines78578678778878979079111 unmodified linesfrom agent.config import load_configfrom agent.core.agent_loop import process_submissionfrom agent.core.hub_artifacts import start_session_artifact_collection_taskfrom agent.core.session import Event, OpType, Sessionfrom agent.core.session_persistence import get_session_storefrom agent.core.tools import ToolRouterfrom agent.messaging.gateway import NotificationGateway# Get project root (parent of backend directory)PROJECT_ROOT = Path(__file__).parent.parent113 unmodified linesself.sessions: dict[str, AgentSession] = {}self._lock = asyncio.Lock()self.persistence_store = Noneself.enable_hub_artifact_collections = Trueasync def start(self) -> None:"""Start shared background resources."""270 unmodified linessession.sandbox_preload_cancel_event = Noneself._start_cpu_sandbox_preload(agent_session)def _start_hub_artifact_collection(self, agent_session: AgentSession) -> None:"""Kick off best-effort Hub collection creation for the session."""if not getattr(self, "enable_hub_artifact_collections", False):returnsession = agent_session.sessionif not getattr(session, "session_id", None):try:session.session_id = agent_session.session_idexcept Exception:logger.debug("Could not attach session id for Hub artifact collection")token = agent_session.hf_token or getattr(session, "hf_token", None)if not token:returntry:start_session_artifact_collection_task(session, token=token)except Exception as e:logger.debug("Failed to schedule Hub artifact collection for %s: %s",agent_session.session_id,e,)async def _clear_persisted_sandbox_metadata(self, session_id: str) -> None:try:await self._store().update_session_fields(150 unmodified linesexisting,preload_sandbox=preload_sandbox,)self._start_hub_artifact_collection(existing)return existingreturn None15 unmodified linesexisting,preload_sandbox=preload_sandbox,)self._start_hub_artifact_collection(existing)return existingreturn None80 unmodified lineshf_token=hf_token,hf_username=hf_username,)self._start_hub_artifact_collection(started)return startedself._start_hub_artifact_collection(agent_session)if preload_sandbox:self._start_cpu_sandbox_preload(agent_session)logger.info("Restored session %s for user %s", session_id, owner or user_id)76 unmodified linesevent_queue=event_queue,tool_router=tool_router,)self._start_hub_artifact_collection(agent_session)await self.persist_session_snapshot(agent_session, runtime_state="idle")self._start_cpu_sandbox_preload(agent_session)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505import asyncioimport loggingfrom types import SimpleNamespace
import pytest
from agent.core import hub_artifactsfrom agent.core.hub_artifacts import ( ML_INTERN_TAG, PROVENANCE_MARKER, artifact_collection_title, augment_repo_card_content, build_hub_artifact_sitecustomize, ensure_session_artifact_collection, is_known_hub_artifact, register_hub_artifact, remember_hub_artifact, start_session_artifact_collection_task, wrap_shell_command_with_hub_artifact_bootstrap,)from agent.tools import local_tools, sandbox_toolfrom agent.tools.hf_repo_files_tool import HfRepoFilesToolfrom agent.tools.hf_repo_git_tool import HfRepoGitToolfrom agent.tools.jobs_tool import _wrap_command_with_artifact_bootstrap
def _session() -> SimpleNamespace: return SimpleNamespace( session_id="session-123", session_start_time="2026-05-05T10:20:30", )
def test_artifact_collection_title_uses_session_date_and_id(): assert ( artifact_collection_title(_session()) == "ml-intern-artifacts-2026-05-05-session-123" )
def test_artifact_collection_title_uses_short_uuid_fragment(): session = SimpleNamespace( session_id="fadcbc77-3439-4c2b-bc52-50d7f6353af3", session_start_time="2026-05-05T10:20:30", )
title = artifact_collection_title(session)
assert title == "ml-intern-artifacts-2026-05-05-fadcbc77" assert len(title) < 60
def test_artifact_collection_title_still_truncates_long_non_uuid_ids(): session = SimpleNamespace( session_id="custom-session-id-that-is-longer-than-the-hub-title-limit", session_start_time="2026-05-05T10:20:30", )
title = artifact_collection_title(session)
assert title.startswith("ml-intern-artifacts-2026-05-05-custom-session-id") assert len(title) < 60
def test_model_card_merges_tags_and_appends_provenance_and_usage(): content = """---license: apache-2.0tags:- text-generation---# Existing Model
Existing details stay here."""
updated = augment_repo_card_content(content, "alice/model", "model") second_pass = augment_repo_card_content(updated, "alice/model", "model")
assert "license: apache-2.0" in updated assert "- text-generation" in updated assert f"- {ML_INTERN_TAG}" in updated assert "# Existing Model" in updated assert "Existing details stay here." in updated assert PROVENANCE_MARKER in updated assert "AutoModelForCausalLM" in updated assert second_pass.count(PROVENANCE_MARKER) == 1 assert second_pass.count("AutoModelForCausalLM") == updated.count( "AutoModelForCausalLM" )
def test_dataset_card_adds_load_dataset_usage(): updated = augment_repo_card_content("", "alice/dataset", "dataset")
assert f"- {ML_INTERN_TAG}" in updated assert "# alice/dataset" in updated assert "from datasets import load_dataset" in updated assert 'load_dataset("alice/dataset")' in updated
def test_existing_usage_section_is_preserved_without_duplicate_usage(): content = """# Existing Dataset
## Usage
Use the custom loader in this repository."""
updated = augment_repo_card_content(content, "alice/dataset", "dataset")
assert "Use the custom loader in this repository." in updated assert "from datasets import load_dataset" not in updated assert PROVENANCE_MARKER in updated
def test_space_card_gets_metadata_without_provenance_body(): updated = augment_repo_card_content("# Existing Space\n", "alice/space", "space")
assert f"- {ML_INTERN_TAG}" in updated assert "# Existing Space" in updated assert PROVENANCE_MARKER not in updated
def test_register_hub_artifact_creates_private_collection_and_adds_item_once( monkeypatch,): session = _session()
class FakeApi: token = "hf-token"
def __init__(self): self.created_collections = [] self.collection_items = [] self.uploads = []
def create_collection(self, **kwargs): self.created_collections.append(kwargs) return SimpleNamespace(slug="alice/ml-intern-artifacts")
def add_collection_item(self, **kwargs): self.collection_items.append(kwargs)
def upload_file(self, **kwargs): self.uploads.append(kwargs)
api = FakeApi() monkeypatch.setattr(hub_artifacts, "_read_remote_readme", lambda *_, **__: "")
assert register_hub_artifact(api, "alice/model", "model", session=session) assert register_hub_artifact(api, "alice/model", "model", session=session)
assert is_known_hub_artifact(session, "alice/model", "model") assert len(api.created_collections) == 1 assert api.created_collections[0]["title"] == artifact_collection_title(session) assert api.created_collections[0]["private"] is True assert len(api.collection_items) == 1 assert api.collection_items[0]["item_id"] == "alice/model" assert api.collection_items[0]["item_type"] == "model" assert api.collection_items[0]["exists_ok"] is True assert len(api.uploads) == 1 assert b"ml-intern" in api.uploads[0]["path_or_fileobj"]
def test_register_hub_artifact_retries_after_partial_failure(monkeypatch): session = _session() api = SimpleNamespace(token="hf-token") card_attempts = 0 collection_attempts = 0
def flaky_update_repo_card(*args, **kwargs): nonlocal card_attempts card_attempts += 1 if card_attempts == 1: raise RuntimeError("temporary card failure")
def add_to_collection(*args, **kwargs): nonlocal collection_attempts collection_attempts += 1
monkeypatch.setattr( hub_artifacts, "_update_repo_card", flaky_update_repo_card, ) monkeypatch.setattr(hub_artifacts, "_add_to_collection", add_to_collection)
assert not register_hub_artifact(api, "alice/model", "model", session=session) assert register_hub_artifact(api, "alice/model", "model", session=session) assert register_hub_artifact(api, "alice/model", "model", session=session)
assert card_attempts == 2 assert collection_attempts == 2
def test_register_hub_artifact_retries_after_collection_failure(monkeypatch): session = _session() api = SimpleNamespace(token="hf-token") card_attempts = 0 collection_attempts = 0
def update_repo_card(*args, **kwargs): nonlocal card_attempts card_attempts += 1
def flaky_add_to_collection(*args, **kwargs): nonlocal collection_attempts collection_attempts += 1 if collection_attempts == 1: raise RuntimeError("temporary collection failure")
monkeypatch.setattr(hub_artifacts, "_update_repo_card", update_repo_card) monkeypatch.setattr( hub_artifacts, "_add_to_collection", flaky_add_to_collection, )
assert not register_hub_artifact(api, "alice/model", "model", session=session) assert register_hub_artifact(api, "alice/model", "model", session=session) assert register_hub_artifact(api, "alice/model", "model", session=session)
assert card_attempts == 2 assert collection_attempts == 2
def test_session_artifact_set_falls_back_when_session_rejects_attrs(caplog): class SlottedSession: __slots__ = ("session_id", "session_start_time")
def __init__(self): self.session_id = "session-123" self.session_start_time = "2026-05-05T10:20:30"
session = SlottedSession()
with caplog.at_level(logging.WARNING): remember_hub_artifact(session, "alice/model", "model")
assert is_known_hub_artifact(session, "alice/model", "model") assert "using process-local fallback state" in caplog.text
@pytest.mark.asyncioasync def test_ensure_session_artifact_collection_uses_user_token(monkeypatch): session = _session() calls = []
class FakeApi: def __init__(self, token): self.token = token
def fake_ensure_collection_slug(api, seen_session, **kwargs): calls.append((api.token, seen_session, kwargs)) return "alice/ml-intern-artifacts"
monkeypatch.setattr(hub_artifacts, "HfApi", FakeApi) monkeypatch.setattr( hub_artifacts, "_ensure_collection_slug", fake_ensure_collection_slug, )
slug = await ensure_session_artifact_collection(session, token="hf-token")
assert slug == "alice/ml-intern-artifacts" assert calls == [ ("hf-token", session, {"token": "hf-token"}), ]
@pytest.mark.asyncioasync def test_start_session_artifact_collection_task_dedupes(monkeypatch): session = _session() calls = []
async def fake_ensure_session_artifact_collection(seen_session, **kwargs): calls.append((seen_session, kwargs)) await asyncio.sleep(0) return "alice/ml-intern-artifacts"
monkeypatch.setattr( hub_artifacts, "ensure_session_artifact_collection", fake_ensure_session_artifact_collection, )
task = start_session_artifact_collection_task(session, token="hf-token") second = start_session_artifact_collection_task(session, token="hf-token")
assert task is not None assert second is task await task assert calls == [(session, {"token": "hf-token"})]
def test_start_session_artifact_collection_task_skips_without_token(): assert start_session_artifact_collection_task(_session()) is None
@pytest.mark.asyncioasync def test_hf_repo_git_create_repo_registers_artifact(monkeypatch): session = _session() calls = []
class FakeApi: token = "hf-token"
def create_repo(self, **kwargs): self.create_kwargs = kwargs return "https://huggingface.co/spaces/alice/demo"
def fake_register(api, repo_id, repo_type, **kwargs): calls.append((api, repo_id, repo_type, kwargs)) return True
monkeypatch.setattr( "agent.tools.hf_repo_git_tool.register_hub_artifact", fake_register, ) tool = HfRepoGitTool(hf_token="hf-token", session=session) tool.api = FakeApi()
result = await tool._create_repo( { "repo_id": "alice/demo", "repo_type": "space", "space_sdk": "gradio", "private": True, } )
assert result["totalResults"] == 1 assert calls == [ ( tool.api, "alice/demo", "space", {"session": session, "extra_metadata": {"sdk": "gradio"}}, ) ]
@pytest.mark.asyncioasync def test_hf_repo_files_upload_registers_known_artifact_with_force(monkeypatch): session = _session() calls = [] uploads = []
class FakeApi: token = "hf-token"
def upload_file(self, **kwargs): uploads.append(kwargs) return SimpleNamespace()
def fake_register(api, repo_id, repo_type, **kwargs): calls.append((api, repo_id, repo_type, kwargs)) return True
monkeypatch.setattr( "agent.tools.hf_repo_files_tool.register_hub_artifact", fake_register, ) remember_hub_artifact(session, "alice/model", "model")
tool = HfRepoFilesTool(hf_token="hf-token", session=session) tool.api = FakeApi()
result = await tool._upload( { "repo_id": "alice/model", "repo_type": "model", "path": "weights.bin", "content": b"weights", } ) readme_result = await tool._upload( { "repo_id": "alice/model", "repo_type": "model", "path": "README.md", "content": "# Model", } )
assert result["totalResults"] == 1 assert readme_result["totalResults"] == 1 assert [upload["path_in_repo"] for upload in uploads] == [ "weights.bin", "README.md", ] assert calls == [ ( tool.api, "alice/model", "model", {"session": session, "force": False}, ), ( tool.api, "alice/model", "model", {"session": session, "force": True}, ), ]
def test_hf_jobs_artifact_bootstrap_wraps_command_without_changing_exec_target(): command = ["uv", "run", "train.py"] wrapped = _wrap_command_with_artifact_bootstrap(command, _session())
assert wrapped[0:2] == ["/bin/sh", "-lc"] assert "sitecustomize.py" in wrapped[2] assert "PYTHONPATH" in wrapped[2] assert "exec uv run train.py" in wrapped[2] assert _wrap_command_with_artifact_bootstrap(command, None) == command
def test_shell_bootstrap_wraps_capybara_push_to_hub_pattern(): command = ( "pip install -q datasets huggingface_hub && python -c " "\"subset.push_to_hub('lewtun/Capybara-100', private=False)\"" )
wrapped = wrap_shell_command_with_hub_artifact_bootstrap(command, _session())
assert "sitecustomize.py" in wrapped assert "PYTHONPATH" in wrapped assert command in wrapped assert wrap_shell_command_with_hub_artifact_bootstrap(command, None) == command assert ( wrap_shell_command_with_hub_artifact_bootstrap( command, SimpleNamespace(session_start_time="2026-05-05T10:20:30"), ) == command )
@pytest.mark.asyncioasync def test_sandbox_bash_wraps_command_for_session_artifact_hooks(): calls = []
class FakeSandbox: def call_tool(self, name, args): calls.append((name, args)) return SimpleNamespace(success=True, output="ok", error="")
session = _session() session.sandbox = FakeSandbox()
handler = sandbox_tool._make_tool_handler("bash") output, ok = await handler({"command": "python make_dataset.py"}, session=session)
assert ok is True assert output == "ok" assert calls[0][0] == "bash" assert "sitecustomize.py" in calls[0][1]["command"] assert "python make_dataset.py" in calls[0][1]["command"]
@pytest.mark.asyncioasync def test_local_bash_wraps_command_for_session_artifact_hooks(monkeypatch): seen = {}
def fake_run(command, **kwargs): seen["command"] = command seen["kwargs"] = kwargs return SimpleNamespace(stdout="ok", stderr="", returncode=0)
monkeypatch.setattr(local_tools.subprocess, "run", fake_run)
output, ok = await local_tools._bash_handler( {"command": "python make_dataset.py"}, session=_session(), )
assert ok is True assert output == "ok" assert "sitecustomize.py" in seen["command"] assert "python make_dataset.py" in seen["command"]
def test_sitecustomize_bootstrap_is_valid_python(): code = build_hub_artifact_sitecustomize(_session())
compile(code, "sitecustomize.py", "exec") assert "ml-intern-artifacts-2026-05-05-session-123" in code
def test_sitecustomize_bootstrap_reuses_existing_collection_slug(): session = _session() setattr( session, hub_artifacts._COLLECTION_SLUG_ATTR, "alice/ml-intern-artifacts-2026-05-05-session-123", )
code = build_hub_artifact_sitecustomize(session)
compile(code, "sitecustomize.py", "exec") assert ( "collection_slug = 'alice/ml-intern-artifacts-2026-05-05-session-123'" in code )429 unmodified lines43043143243343443518 unmodified lines454455456457458459429 unmodified linesawait _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_lazy_restore_schedules_cpu_sandbox_preload():manager = _manager_with_store(RestoreStore())18 unmodified linesawait _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_lazy_restore_deletes_persisted_sandbox_before_preload(monkeypatch):deleted: list[tuple[str, str, str]] = []429 unmodified lines43043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046118 unmodified lines480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516429 unmodified linesawait _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_create_session_starts_hub_artifact_collection(monkeypatch):manager = _manager_with_store(NoopSessionStore())manager.enable_hub_artifact_collections = Truestop = _install_fake_runtime(manager)started: list[tuple[str, str]] = []def fake_start_session_artifact_collection_task(session, **kwargs):started.append((session.session_id, kwargs["token"]))return Nonemonkeypatch.setattr("session_manager.start_session_artifact_collection_task",fake_start_session_artifact_collection_task,)manager._start_cpu_sandbox_preload = lambda _: None # type: ignore[method-assign]try:session_id = await manager.create_session(user_id="owner", hf_token="token")assert started == [(session_id, "token")]finally:stop.set()await _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_lazy_restore_schedules_cpu_sandbox_preload():manager = _manager_with_store(RestoreStore())18 unmodified linesawait _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_lazy_restore_starts_hub_artifact_collection(monkeypatch):manager = _manager_with_store(RestoreStore())manager.enable_hub_artifact_collections = Truestop = _install_fake_runtime(manager)started: list[tuple[str, str]] = []def fake_start_session_artifact_collection_task(session, **kwargs):started.append((session.session_id, kwargs["token"]))return Nonemonkeypatch.setattr("session_manager.start_session_artifact_collection_task",fake_start_session_artifact_collection_task,)manager._start_cpu_sandbox_preload = lambda _: None # type: ignore[method-assign]try:restored = await manager.ensure_session_loaded("persisted-session",user_id="owner",hf_token="token",)assert restored is not Noneassert started == [("persisted-session", "token")]finally:stop.set()await _cancel_runtime_tasks(manager)@pytest.mark.asyncioasync def test_lazy_restore_deletes_persisted_sandbox_before_preload(monkeypatch):deleted: list[tuple[str, str, str]] = []
agent/core/agent_loop.py+2
agent/core/hub_artifacts.pyAdded+765
agent/tools/hf_repo_files_tool.py+14-2
agent/tools/hf_repo_git_tool.py+15-2
agent/tools/jobs_tool.py+26
agent/tools/local_tools.py+6-1
agent/tools/sandbox_tool.py+9
backend/session_manager.py+30-1
tests/unit/test_hub_artifacts.pyAdded+505
tests/unit/test_session_manager_persistence.py+57