File size: 22,072 Bytes
c60bea4 19773eb 0bd7547 c60bea4 0bd7547 c60bea4 19773eb c60bea4 0bd7547 c60bea4 990ecba 0bd7547 2a2e170 0bd7547 754345f 0bd7547 754345f 0bd7547 990ecba c60bea4 19773eb 0bd7547 19773eb 0bd7547 19773eb 0bd7547 19773eb c60bea4 19773eb c60bea4 19773eb c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 19773eb 0bd7547 19773eb 0bd7547 19773eb c60bea4 19773eb 0bd7547 19773eb c60bea4 0bd7547 19773eb 0bd7547 19773eb 0bd7547 19773eb 0bd7547 19773eb c60bea4 0bd7547 19773eb c60bea4 19773eb 0bd7547 19773eb c60bea4 19773eb c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 c60bea4 0bd7547 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | #!/usr/bin/env python3
"""
Standalone script for uploading session trajectories to HuggingFace.
This runs as a separate process to avoid blocking the main agent.
Uses individual file uploads to avoid race conditions.
Two formats are supported:
* ``row`` β single-line JSONL row used by the existing org telemetry/KPI
pipeline (``smolagents/ml-intern-sessions``). Compatible with
``backend/kpis_scheduler.py``.
* ``claude_code`` β one event per line in the Claude Code JSONL schema,
auto-detected by the HF Agent Trace Viewer
(https://huggingface.co/changelog/agent-trace-viewer). Used for the
per-user private dataset (default ``{hf_user}/ml-intern-sessions``).
"""
import argparse
import hashlib
import json
import os
import sys
from datetime import datetime
from pathlib import Path
from typing import Any
from dotenv import load_dotenv
load_dotenv()
# Token resolution for the org KPI dataset. Fallback chain (least-privilege
# first) β matches backend/kpis_scheduler.py so one write-scoped token on the
# Space covers every telemetry dataset. Never hardcode tokens in source.
_ORG_TOKEN_FALLBACK_CHAIN = (
"HF_SESSION_UPLOAD_TOKEN",
"HF_TOKEN",
"HF_ADMIN_TOKEN",
)
_PERSONAL_TOKEN_ENV = "_ML_INTERN_PERSONAL_TOKEN"
def _resolve_token(token_env: str | None) -> str:
"""Resolve an HF token from env. ``token_env`` overrides the fallback chain."""
if token_env == "HF_TOKEN":
try:
from agent.core.hf_tokens import resolve_hf_token
return (
resolve_hf_token(
os.environ.get(_PERSONAL_TOKEN_ENV),
os.environ.get("HF_TOKEN"),
)
or ""
)
except Exception:
token = os.environ.get(_PERSONAL_TOKEN_ENV) or os.environ.get("HF_TOKEN")
return token or ""
if token_env:
return os.environ.get(token_env, "") or ""
for var in _ORG_TOKEN_FALLBACK_CHAIN:
val = os.environ.get(var)
if val:
return val
return ""
def _scrub(obj: Any) -> Any:
"""Best-effort regex scrub for HF tokens / API keys before upload."""
try:
from agent.core.redact import scrub # type: ignore
except Exception:
# Fallback for environments where the agent package isn't importable
# (shouldn't happen in our subprocess, but be defensive).
import importlib.util
_spec = importlib.util.spec_from_file_location(
"_redact",
Path(__file__).parent / "redact.py",
)
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod) # type: ignore
scrub = _mod.scrub
return scrub(obj)
def _msg_uuid(session_id: str, role: str, idx: int) -> str:
"""Deterministic UUID-shaped id for a Claude Code message.
Uses sha1 of ``session_id::role::idx`` so re-uploads/heartbeats keep the
parent/child chain stable. Same convention as the example dataset
https://huggingface.co/datasets/clem/hf-coding-tools-traces.
"""
digest = hashlib.sha1(f"{session_id}::{role}::{idx}".encode("utf-8")).hexdigest()
# Format like a UUID for visual familiarity (32 hex chars w/ dashes).
return (
f"{digest[0:8]}-{digest[8:12]}-{digest[12:16]}-{digest[16:20]}-{digest[20:32]}"
)
def _content_to_text(content: Any) -> str:
"""Best-effort flatten of a litellm/openai content field to plain text."""
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts: list[str] = []
for block in content:
if isinstance(block, dict):
text = block.get("text")
if isinstance(text, str):
parts.append(text)
else:
# Unknown content block β keep round-trippable representation.
parts.append(json.dumps(block, default=str))
else:
parts.append(str(block))
return "\n".join(parts)
return str(content)
def _parse_tool_args(raw: Any) -> Any:
"""Tool call arguments arrive as a JSON-encoded string from LLMs."""
if isinstance(raw, dict):
return raw
if isinstance(raw, str):
try:
return json.loads(raw)
except (json.JSONDecodeError, TypeError):
return {"_raw": raw}
return raw
def to_claude_code_jsonl(trajectory: dict) -> list[dict]:
"""Convert an internal trajectory dict to Claude Code JSONL events.
Schema reference (per the HF Agent Trace Viewer auto-detector):
{"type":"user","message":{"role":"user","content":"..."},
"uuid":"...","parentUuid":null,"sessionId":"...","timestamp":"..."}
{"type":"assistant",
"message":{"role":"assistant","model":"...",
"content":[{"type":"text","text":"..."},
{"type":"tool_use","id":"...","name":"...","input":{...}}]},
"uuid":"...","parentUuid":"<prev>","sessionId":"...","timestamp":"..."}
{"type":"user","message":{"role":"user",
"content":[{"type":"tool_result",
"tool_use_id":"...","content":"..."}]},
"uuid":"...","parentUuid":"<prev>","sessionId":"...","timestamp":"..."}
System messages are skipped (they're not part of the viewer schema and
contain large prompts that pollute the trace viewer UI).
"""
session_id = trajectory["session_id"]
model_name = trajectory.get("model_name") or ""
fallback_timestamp = (
trajectory.get("session_start_time") or datetime.now().isoformat()
)
messages: list[dict] = trajectory.get("messages") or []
out: list[dict] = []
parent_uuid: str | None = None
for idx, msg in enumerate(messages):
if not isinstance(msg, dict):
continue
role = msg.get("role")
if role == "system":
continue
timestamp = msg.get("timestamp") or fallback_timestamp
if role == "user":
content = _content_to_text(msg.get("content"))
event_uuid = _msg_uuid(session_id, "user", idx)
out.append(
{
"type": "user",
"message": {"role": "user", "content": content},
"uuid": event_uuid,
"parentUuid": parent_uuid,
"sessionId": session_id,
"timestamp": timestamp,
}
)
parent_uuid = event_uuid
elif role == "assistant":
content_text = _content_to_text(msg.get("content"))
content_blocks: list[dict] = []
if content_text:
content_blocks.append({"type": "text", "text": content_text})
for tc in msg.get("tool_calls") or []:
if not isinstance(tc, dict):
continue
fn = tc.get("function") or {}
content_blocks.append(
{
"type": "tool_use",
"id": tc.get("id") or "",
"name": fn.get("name") or "",
"input": _parse_tool_args(fn.get("arguments")),
}
)
if not content_blocks:
# Edge case: empty assistant turn (shouldn't normally happen,
# but skip rather than emit an empty content array which
# confuses the viewer).
continue
event_uuid = _msg_uuid(session_id, "assistant", idx)
out.append(
{
"type": "assistant",
"message": {
"role": "assistant",
"model": model_name,
"content": content_blocks,
},
"uuid": event_uuid,
"parentUuid": parent_uuid,
"sessionId": session_id,
"timestamp": timestamp,
}
)
parent_uuid = event_uuid
elif role == "tool":
tool_call_id = msg.get("tool_call_id") or ""
content_text = _content_to_text(msg.get("content"))
event_uuid = _msg_uuid(session_id, "tool", idx)
out.append(
{
"type": "user",
"message": {
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_call_id,
"content": content_text,
}
],
},
"uuid": event_uuid,
"parentUuid": parent_uuid,
"sessionId": session_id,
"timestamp": timestamp,
}
)
parent_uuid = event_uuid
return out
def _scrub_session_for_upload(data: dict) -> dict:
"""Best-effort scrub of transcript fields before any upload temp file."""
scrubbed = dict(data)
scrubbed["messages"] = _scrub(data.get("messages") or [])
scrubbed["events"] = _scrub(data.get("events") or [])
scrubbed["tools"] = _scrub(data.get("tools") or [])
return scrubbed
def _write_row_payload(data: dict, tmp_path: str) -> None:
"""Single-row JSONL (existing format) β used by KPI scheduler."""
scrubbed = _scrub_session_for_upload(data)
session_row = {
"session_id": data["session_id"],
"user_id": data.get("user_id"),
"session_start_time": data["session_start_time"],
"session_end_time": data["session_end_time"],
"model_name": data["model_name"],
"total_cost_usd": data.get("total_cost_usd"),
"messages": json.dumps(scrubbed["messages"]),
"events": json.dumps(scrubbed["events"]),
"tools": json.dumps(scrubbed["tools"]),
}
with open(tmp_path, "w") as tmp:
json.dump(session_row, tmp)
def _write_claude_code_payload(data: dict, tmp_path: str) -> None:
"""Multi-line JSONL in Claude Code schema for the HF trace viewer."""
# Scrub before conversion so secrets never reach the upload temp file.
scrubbed = _scrub_session_for_upload(data)
events = to_claude_code_jsonl(scrubbed)
with open(tmp_path, "w") as tmp:
for event in events:
tmp.write(json.dumps(event))
tmp.write("\n")
def _status_field(format: str) -> str:
"""Per-format upload status field on the local trajectory file."""
return "personal_upload_status" if format == "claude_code" else "upload_status"
def _url_field(format: str) -> str:
return "personal_upload_url" if format == "claude_code" else "upload_url"
def _read_session_file(session_file: str) -> dict:
"""Read a local session file while respecting uploader file locks."""
import fcntl
with open(session_file, "r") as f:
fcntl.flock(f, fcntl.LOCK_SH)
try:
return json.load(f)
finally:
fcntl.flock(f, fcntl.LOCK_UN)
def _update_upload_status(
session_file: str,
status_key: str,
url_key: str,
status: str,
dataset_url: str | None = None,
) -> None:
"""Atomically update only this uploader's status fields.
The org and personal uploaders run as separate processes against the same
local session JSON file. Re-read under an exclusive lock so one uploader
cannot clobber fields written by the other.
"""
import fcntl
with open(session_file, "r+") as f:
fcntl.flock(f, fcntl.LOCK_EX)
try:
data = json.load(f)
data[status_key] = status
if dataset_url is not None:
data[url_key] = dataset_url
data["last_save_time"] = datetime.now().isoformat()
f.seek(0)
json.dump(data, f, indent=2)
f.truncate()
f.flush()
os.fsync(f.fileno())
finally:
fcntl.flock(f, fcntl.LOCK_UN)
def dataset_card_readme(repo_id: str) -> str:
"""Dataset card for personal ML Intern session trace repos."""
return """---
pretty_name: "ML Intern Session Traces"
language:
- en
license: other
task_categories:
- text-generation
tags:
- agent-traces
- coding-agent
- ml-intern
- session-traces
- claude-code
- hf-agent-trace-viewer
configs:
- config_name: default
data_files:
- split: train
path: "sessions/**/*.jsonl"
---
# ML Intern session traces
This dataset contains ML Intern coding agent session traces uploaded from local
ML Intern runs. The traces are stored as JSON Lines files under `sessions/`,
with one file per session.
## Links
- ML Intern demo: https://smolagents-ml-intern.hf.space
- ML Intern CLI: https://github.com/huggingface/ml-intern
## Data description
Each `*.jsonl` file contains a single ML Intern session converted to a
Claude-Code-style event stream for the Hugging Face Agent Trace Viewer. Entries
can include user messages, assistant messages, tool calls, tool results, model
metadata, and timestamps.
Session files are written to paths of the form:
```text
sessions/YYYY-MM-DD/<session_id>.jsonl
```
## Redaction and review
**WARNING: no comprehensive redaction or human review has been performed for this dataset.**
ML Intern applies automated best-effort scrubbing for common secret patterns
such as Hugging Face, Anthropic, OpenAI, GitHub, and AWS tokens before upload.
This is not a privacy guarantee.
These traces may contain sensitive information, including prompts, code,
terminal output, file paths, repository names, private task context, tool
outputs, or other data from the local development environment. Treat every
session as potentially sensitive.
Do not make this dataset public unless you have manually inspected the uploaded
sessions and are comfortable sharing their full contents.
## Limitations
Coding agent transcripts can include private or off-topic content, failed
experiments, credentials accidentally pasted by a user, and outputs copied from
local files or services. Use with appropriate caution, especially before
changing repository visibility.
"""
def _upload_dataset_card(api: Any, repo_id: str, token: str, format: str) -> None:
"""Create/update a README for personal trace datasets."""
if format != "claude_code":
return
api.upload_file(
path_or_fileobj=dataset_card_readme(repo_id).encode("utf-8"),
path_in_repo="README.md",
repo_id=repo_id,
repo_type="dataset",
token=token,
commit_message="Update dataset card",
)
def upload_session_as_file(
session_file: str,
repo_id: str,
max_retries: int = 3,
format: str = "row",
token_env: str | None = None,
private: bool = False,
) -> bool:
"""Upload a single session as an individual JSONL file (no race conditions).
Args:
session_file: Path to local session JSON file
repo_id: HuggingFace dataset repo ID
max_retries: Number of retry attempts
format: ``row`` (default, KPI-compatible) or ``claude_code`` (HF
Agent Trace Viewer compatible).
token_env: Name of the env var holding the HF token. ``None`` falls
back to the org-token chain (``HF_SESSION_UPLOAD_TOKEN`` β
``HF_TOKEN`` β ``HF_ADMIN_TOKEN``).
private: When creating the repo for the first time, mark it private.
Returns:
True if successful, False otherwise
"""
try:
from huggingface_hub import HfApi
except ImportError:
print("Error: huggingface_hub library not available", file=sys.stderr)
return False
status_key = _status_field(format)
url_key = _url_field(format)
try:
data = _read_session_file(session_file)
# Skip if already uploaded for this format.
if data.get(status_key) == "success":
return True
hf_token = _resolve_token(token_env)
if not hf_token:
_update_upload_status(session_file, status_key, url_key, "failed")
return False
# Build temp upload payload in the requested format.
import tempfile
with tempfile.NamedTemporaryFile(
mode="w", suffix=".jsonl", delete=False
) as tmp:
tmp_path = tmp.name
try:
if format == "claude_code":
_write_claude_code_payload(data, tmp_path)
else:
_write_row_payload(data, tmp_path)
session_id = data["session_id"]
date_str = datetime.fromisoformat(data["session_start_time"]).strftime(
"%Y-%m-%d"
)
repo_path = f"sessions/{date_str}/{session_id}.jsonl"
api = HfApi()
for attempt in range(max_retries):
try:
# Idempotent create β visibility is set on first creation
# only. Existing repos keep whatever the user picked via
# /share-traces.
try:
api.create_repo(
repo_id=repo_id,
repo_type="dataset",
private=private,
token=hf_token,
exist_ok=True,
)
except Exception:
pass
_upload_dataset_card(api, repo_id, hf_token, format)
api.upload_file(
path_or_fileobj=tmp_path,
path_in_repo=repo_path,
repo_id=repo_id,
repo_type="dataset",
token=hf_token,
commit_message=f"Add session {session_id}",
)
_update_upload_status(
session_file,
status_key,
url_key,
"success",
f"https://huggingface.co/datasets/{repo_id}",
)
return True
except Exception:
if attempt < max_retries - 1:
import time
wait_time = 2**attempt
time.sleep(wait_time)
else:
_update_upload_status(
session_file, status_key, url_key, "failed"
)
return False
finally:
try:
os.unlink(tmp_path)
except Exception:
pass
except Exception as e:
print(f"Error uploading session: {e}", file=sys.stderr)
return False
def retry_failed_uploads(
directory: str,
repo_id: str,
format: str = "row",
token_env: str | None = None,
private: bool = False,
):
"""Retry all failed/pending uploads in a directory for the given format."""
log_dir = Path(directory)
if not log_dir.exists():
return
status_key = _status_field(format)
session_files = list(log_dir.glob("session_*.json"))
for filepath in session_files:
try:
data = _read_session_file(str(filepath))
# Only retry pending or failed uploads. Files predating this
# field don't have it; treat unknown as "not yet attempted" for
# the row format (legacy behavior) and "skip" for claude_code
# so we don't suddenly re-upload pre-existing sessions to a
# newly-introduced personal repo.
status = data.get(status_key, "unknown")
if format == "claude_code" and status_key not in data:
continue
if status in ("pending", "failed", "unknown"):
upload_session_as_file(
str(filepath),
repo_id,
format=format,
token_env=token_env,
private=private,
)
except Exception:
pass
def _str2bool(v: str) -> bool:
return str(v).strip().lower() in {"1", "true", "yes", "on"}
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="session_uploader.py")
sub = parser.add_subparsers(dest="command", required=True)
p_upload = sub.add_parser("upload")
p_upload.add_argument("session_file")
p_upload.add_argument("repo_id")
p_upload.add_argument(
"--format",
choices=["row", "claude_code"],
default="row",
)
p_upload.add_argument(
"--token-env",
default=None,
help="Env var name holding the HF token (default: org fallback chain).",
)
p_upload.add_argument("--private", default="false")
p_retry = sub.add_parser("retry")
p_retry.add_argument("directory")
p_retry.add_argument("repo_id")
p_retry.add_argument(
"--format",
choices=["row", "claude_code"],
default="row",
)
p_retry.add_argument("--token-env", default=None)
p_retry.add_argument("--private", default="false")
args = parser.parse_args()
if args.command == "upload":
ok = upload_session_as_file(
args.session_file,
args.repo_id,
format=args.format,
token_env=args.token_env,
private=_str2bool(args.private),
)
sys.exit(0 if ok else 1)
if args.command == "retry":
retry_failed_uploads(
args.directory,
args.repo_id,
format=args.format,
token_env=args.token_env,
private=_str2bool(args.private),
)
sys.exit(0)
parser.print_help()
sys.exit(1)
|