Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
jobs-tool-context-management
Browse filesFilter UV package installation output from HF Jobs logs
Files changed (2)
6 unmodified lines78910111263 unmodified lines767778798081307 unmodified lines3893903913923933943953966 unmodified linesimport asyncioimport base64import osfrom typing import Any, Dict, Literal, Optionalfrom huggingface_hub import HfApi63 unmodified linesUV_DEFAULT_IMAGE = "ghcr.io/astral-sh/uv:python3.12-bookworm"def _add_environment_variables(params: Dict[str, Any] | None) -> Dict[str, Any]:token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") or ""307 unmodified linesnamespace=self.namespace,)# Format all logs for the agentlog_text = "\n".join(all_logs) if all_logs else "(no logs)"response = f"""{job_type} job completed!6 unmodified lines7891011121363 unmodified lines7778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120307 unmodified lines4284294304314324334344354364374386 unmodified linesimport asyncioimport base64import osimport refrom typing import Any, Dict, Literal, Optionalfrom huggingface_hub import HfApi63 unmodified linesUV_DEFAULT_IMAGE = "ghcr.io/astral-sh/uv:python3.12-bookworm"def _filter_uv_install_output(logs: list[str]) -> list[str]:"""Filter out UV package installation output from logs.Replaces installation details with "[installs truncated]" and keepsthe "Installed X packages in Y ms/s" summary line.Args:logs: List of log linesReturns:Filtered list of log lines"""if not logs:return logs# Regex pattern to match: "Installed X packages in Y ms" or "Installed X package in Y s"install_pattern = re.compile(r"^Installed\s+\d+\s+packages?\s+in\s+\d+(?:\.\d+)?\s*(?:ms|s)$")# Find the index of the "Installed X packages" lineinstall_line_idx = Nonefor idx, line in enumerate(logs):if install_pattern.match(line.strip()):install_line_idx = idxbreak# If pattern found, replace installation details with truncation messageif install_line_idx is not None and install_line_idx > 0:# Keep logs from the "Installed X packages" line onward# Add truncation message before the "Installed" linereturn ["[installs truncated]"] + logs[install_line_idx:]# If pattern not found, return original logsreturn logsdef _add_environment_variables(params: Dict[str, Any] | None) -> Dict[str, Any]:token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") or ""307 unmodified linesnamespace=self.namespace,)# Filter out UV package installation outputfiltered_logs = _filter_uv_install_output(all_logs)# Format all logs for the agentlog_text = "\n".join(filtered_logs) if filtered_logs else "(no logs)"response = f"""{job_type} job completed!
451 unmodified lines452453454451 unmodified linesassert "job-3" in result["formatted"]assert "job-1" not in result["formatted"]assert result["resultsShared"] == 1451 unmodified lines452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537451 unmodified linesassert "job-3" in result["formatted"]assert "job-1" not in result["formatted"]assert result["resultsShared"] == 1def test_filter_uv_install_output():"""Test filtering of UV package installation output"""from agent.tools.jobs_tool import _filter_uv_install_output# Test case 1: Logs with UV installation outputlogs_with_install = ["Resolved 68 packages in 1.01s","Installed 68 packages in 251ms","Hello from the script!","Script execution completed",]filtered = _filter_uv_install_output(logs_with_install)assert len(filtered) == 4assert filtered[0] == "[installs truncated]"assert filtered[1] == "Installed 68 packages in 251ms"assert filtered[2] == "Hello from the script!"assert filtered[3] == "Script execution completed"# Test case 2: Logs without UV installation outputlogs_without_install = ["Script started","Processing data...","Done!",]filtered = _filter_uv_install_output(logs_without_install)assert len(filtered) == 3assert filtered == logs_without_install# Test case 3: Empty logsassert _filter_uv_install_output([]) == []# Test case 4: Different time formats (ms vs s)logs_with_seconds = ["Downloading packages...","Installed 10 packages in 2s","Running main.py",]filtered = _filter_uv_install_output(logs_with_seconds)assert len(filtered) == 3assert filtered[0] == "[installs truncated]"assert filtered[1] == "Installed 10 packages in 2s"assert filtered[2] == "Running main.py"# Test case 5: Single packagelogs_single_package = ["Resolving dependencies","Installed 1 package in 50ms","Import successful",]filtered = _filter_uv_install_output(logs_single_package)assert len(filtered) == 3assert filtered[0] == "[installs truncated]"assert filtered[1] == "Installed 1 package in 50ms"assert filtered[2] == "Import successful"# Test case 6: Decimal time valueslogs_decimal_time = ["Starting installation","Installed 25 packages in 125.5ms","All dependencies ready",]filtered = _filter_uv_install_output(logs_decimal_time)assert len(filtered) == 3assert filtered[0] == "[installs truncated]"assert filtered[1] == "Installed 25 packages in 125.5ms"assert filtered[2] == "All dependencies ready"# Test case 7: "Installed" line is first (no truncation needed)logs_install_first = ["Installed 5 packages in 100ms","Running script...",]filtered = _filter_uv_install_output(logs_install_first)# No truncation message if "Installed" is the first lineassert filtered == logs_install_first
agent/tools/jobs_tool.py+43-1
tests/unit/tools/test_jobs_tool.py+83