{"record":{"id":"5118ad0c9ffbfead","repo":"microsoft/autogen","slug":"working-directory-cwd-cwd-is-not-valid-it-m","errorCode":null,"errorMessage":"Working directory (cwd) '{cwd}' is not valid. It must be within the base path.","messagePattern":"Working directory \\(cwd\\) '(.+?)' is not valid\\. It must be within the base path\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py","lineNumber":56,"sourceCode":"        self._markdown_converter = MarkItDown()\n        self._base_path = None if base_path is None else os.path.realpath(base_path)\n        self._page_content: str = \"\"\n        self._find_on_page_query: Union[str, None] = None\n        self._find_on_page_last_result: Union[int, None] = None  # Location of the last result\n\n        # Set the working directory\n        if cwd is None:\n            if self._validate_path(os.getcwd()):\n                # Use the current working directory if it's in the base path\n                cwd = os.path.realpath(os.getcwd())\n            elif self._base_path is not None:\n                # Otherwise, use the base path\n                cwd = os.path.realpath(self._base_path)\n            else:\n                raise ValueError(\"No valid working directory (cwd) provided.\")\n        elif not self._validate_path(cwd):\n            # A cwd was provided, but it is not valid\n            raise ValueError(f\"Working directory (cwd) '{cwd}' is not valid. It must be within the base path.\")\n\n        # Populate the history with the current working directory\n        self.set_path(os.path.realpath(cwd))\n\n    @property\n    def path(self) -> str:\n        \"\"\"Return the path of the current page.\"\"\"\n        assert len(self.history) > 0\n        return self.history[-1][0]\n\n    def _validate_path(self, path: str) -> bool:\n        \"\"\"Validates the path to ensure it is within the base path.\n\n        Arguments:\n            path: The path to validate.\n        Returns:\n            True if the path is valid, False otherwise.\n        \"\"\"","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/agents/file_surfer/_markdown_file_browser.py#L38-L74","documentation":"Thrown by MarkdownFileBrowser's constructor when an explicitly passed cwd fails _validate_path(), i.e. the resolved working directory does not fall inside the configured base_path. The browser sandboxes all file access to base_path, so any cwd outside it is rejected at init. Note this only fires when YOU supply cwd; when cwd is None the class falls back to os.getcwd() (if valid) or base_path.","triggerScenarios":"Constructing MarkdownFileBrowser(path_uri=..., base_path='/data') with cwd='/home/user' (or any path outside /data), including cases where symlinks resolve outside base_path since _validate_path works on real paths.","commonSituations":"Running an agent in a Docker container or notebook where os.getcwd() differs from the intended document root; passing a relative cwd that resolves against an unexpected current directory; setting base_path to a subdirectory while forgetting to update cwd; symlinked paths escaping base_path after realpath resolution.","solutions":["Pass a cwd that is inside base_path, e.g. cwd=base_path itself or a subdirectory of it.","Omit cwd entirely so the constructor falls back to base_path when os.getcwd() is not valid.","If the intended working directory is correct, widen base_path so it is an ancestor of cwd (e.g. a common parent directory).","Check for symlinks: the validation uses realpath, so a cwd that looks inside base_path but resolves outside will fail; use os.path.realpath on both before constructing."],"exampleFix":"# before\nbrowser = MarkdownFileBrowser(\n    path_uri=\"file:///home/user/docs/readme.md\",\n    base_path=\"/data\",\n    cwd=\"/home/user/docs\",\n)\n\n# after\nbrowser = MarkdownFileBrowser(\n    path_uri=\"file:///data/docs/readme.md\",\n    base_path=\"/data\",\n    cwd=\"/data/docs\",\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef valid_cwd(base_path: str, cwd: str) -> bool:\n    base = os.path.realpath(base_path)\n    candidate = os.path.realpath(cwd)\n    return candidate == base or candidate.startswith(base + os.sep)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute cwd from base_path (e.g. cwd=base_path) instead of an independent absolute path.","Remember validation uses realpath — resolve symlinks on both paths before comparing.","In notebooks/containers, explicitly pass cwd rather than relying on os.getcwd()."],"tags":["filesystem","path-validation","configuration","file-surfer"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}