{"record":{"id":"abf14a6529c9672d","repo":"OpenBMB/ChatDev","slug":"context-is-required-for-uv-tools","errorCode":null,"errorMessage":"_context is required for uv tools","messagePattern":"_context is required for uv tools","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"functions/function_calling/uv_related.py","lineNumber":43,"sourceCode":"def _build_timeout_message(step: str | None, timeout_value: float, stdout: str, stderr: str) -> str:\n    \"\"\"Create a descriptive timeout error message with optional output preview.\"\"\"\n\n    label = \"uv command\"\n    if step:\n        label = f\"{label} ({step})\"\n    message = f\"{label} timed out after {timeout_value} seconds\"\n    preview = _trim_output_preview(stdout, stderr)\n    if preview:\n        return f\"{message}. Last output: {preview}\"\n    return message\n\n\nclass WorkspaceCommandContext:\n    \"\"\"Resolve the workspace root from the injected runtime context.\"\"\"\n\n    def __init__(self, ctx: Dict[str, Any] | None):\n        if ctx is None:\n            raise ValueError(\"_context is required for uv tools\")\n        self.workspace_root = self._require_workspace(ctx.get(\"python_workspace_root\"))\n        self._raw_ctx = ctx\n\n    @staticmethod\n    def _require_workspace(raw_path: Any) -> Path:\n        if raw_path is None:\n            raise ValueError(\"python_workspace_root missing from _context\")\n        path = Path(raw_path).expanduser().resolve()\n        path.mkdir(parents=True, exist_ok=True)\n        return path\n\n    def resolve_under_workspace(self, relative_path: str | Path) -> Path:\n        candidate = Path(relative_path)\n        absolute = candidate if candidate.is_absolute() else self.workspace_root / candidate\n        absolute = absolute.expanduser().resolve()\n        if self.workspace_root not in absolute.parents and absolute != self.workspace_root:\n            raise ValueError(\"script path is outside workspace root\")\n        return absolute","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/OpenBMB/ChatDev/blob/4fb2db0ea90375ce1059f44fe03ffbd191a7a169/functions/function_calling/uv_related.py#L25-L61","documentation":"WorkspaceCommandContext (the context object backing uv_run and install_python_packages) requires the runtime-injected _context dict. Constructing it with None — because the tool was called without context injection or the parameter was dropped — raises ValueError immediately. The context carries python_workspace_root, which these tools cannot function without.","triggerScenarios":"Invoking uv_run or install_python_packages without the injected _context argument; manually instantiating WorkspaceCommandContext(None) in tests; a tool-adapter layer that fails to forward the runtime context dict.","commonSituations":"Unit tests constructing tool objects directly; refactoring the function-calling layer so _context is no longer passed through; calling the tool from a framework that does not support context injection.","solutions":["Ensure the tool is invoked through the runtime that injects _context (include the _context parameter in the tool-call payload)","In tests, pass a dict containing python_workspace_root pointing at a temp directory","Check the adapter/bridge forwards _context when registering these tools"],"exampleFix":"# before\nctx = WorkspaceCommandContext(None)\n# after\nctx = WorkspaceCommandContext({\"python_workspace_root\": \"/tmp/ws\"})","handlingStrategy":"validation","validationCode":"if _context is None:\n    _context = {\"python_workspace_root\": str(tempfile.mkdtemp())}  # e.g. in tests\nuv_run(script=\"run.py\", _context=_context)","typeGuard":"def has_context(ctx) -> bool:\n    return isinstance(ctx, dict)","tryCatchPattern":"try:\n    uv_run(script=\"run.py\", _context=_context)\nexcept ValueError as e:\n    if \"_context is required\" in str(e):\n        raise RuntimeError(\"tool invoked without runtime context injection\") from e\n    raise","preventionTips":["Always call uv tools through the runtime that injects _context","In tests, build a minimal context dict with python_workspace_root set to a tmpdir","Ensure adapters/bridges forward _context when registering tools"],"tags":["configuration","context","uv","tool"],"backgroundTag":"missing-context-injection","analyzedSha":"4fb2db0ea90375ce1059f44fe03ffbd191a7a169","analyzedAt":"2026-08-27T14:35:29.622Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}