{"record":{"id":"a47f4a0f4eac732c","repo":"agentscope-ai/agentscope","slug":"ripgrep-error-code-result-exit-code-error-ms","errorCode":null,"errorMessage":"ripgrep error (code {result.exit_code}): {error_msg}","messagePattern":"ripgrep error \\(code (.+?)\\): (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/tool/_builtin/_grep.py","lineNumber":309,"sourceCode":"        result = await self._backend.exec_shell(\n            command,\n            timeout=float(timeout),\n        )\n\n        if result.exit_code == -1 and result.stderr == b\"timed out\":\n            raise RipgrepTimeoutError(\n                f\"Ripgrep search timed out after {timeout} seconds. \"\n                \"Try searching a more specific path or pattern.\",\n                [],\n            )\n\n        # returncode 0 = matches found, 1 = no matches\n        if result.exit_code not in (0, 1):\n            error_msg = result.stderr.decode(\n                \"utf-8\",\n                errors=\"ignore\",\n            ).strip()\n            raise RuntimeError(\n                f\"ripgrep error (code {result.exit_code}): {error_msg}\",\n            )\n\n        raw = result.stdout.decode(\"utf-8\", errors=\"ignore\")\n\n        lines = [\n            line.rstrip(\"\\r\") for line in raw.split(\"\\n\") if line.rstrip(\"\\r\")\n        ]\n        return lines\n\n    async def call(  # type: ignore[override]\n        self,\n        pattern: str,\n        path: str | None = None,\n        output_mode: Literal[\n            \"content\",\n            \"files_with_matches\",\n            \"count\",","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/tool/_builtin/_grep.py#L291-L327","documentation":"The ripgrep subprocess exited with a code other than 0 (matches) or 1 (no matches) — e.g. 2 for usage/IO errors — and stderr is surfaced in a RuntimeError.","triggerScenarios":"Invalid regex pattern (unbalanced parenthesis), nonexistent path, permission-denied directory, or missing rg binary producing a nonzero shell exit.","commonSituations":"LLM-generated regex that is syntactically invalid; searching paths the process can't read; rg not installed so the shell returns 127.","solutions":["Test the regex with `rg --pcre2 'pattern' testfile` or python re first","Verify the path exists and is readable","Ensure ripgrep is installed and on PATH (command -v rg)","Catch RuntimeError and feed stderr back for pattern correction"],"exampleFix":"# before\nawait grep(pattern='foo(', path='src')\n# after\nawait grep(pattern='foo\\(', path='src')","handlingStrategy":"try-catch","validationCode":"import re\ntry: re.compile(pattern)\nexcept re.error: raise ValueError('invalid regex')","typeGuard":"def valid_regex(p: str) -> bool:\n    try: re.compile(p); return True\n    except re.error: return False","tryCatchPattern":"try:\n    res = await grep(pattern=pat, path=path)\nexcept RuntimeError as e:\n    if 'ripgrep error' in str(e): logger.warning('bad pattern/path: %s', e)\n    raise","preventionTips":["Compile-check regexes before executing search tools","Verify rg is installed (command -v rg) and paths are readable","Feed stderr back to the model so it can fix the pattern"],"tags":["ripgrep","regex","subprocess","tool"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}