{"record":{"id":"d5b7cb858b0bf891","repo":"oraios/serena","slug":"must-be-positive-or-the-default-1-got-max-an","errorCode":null,"errorMessage":"Must be positive or the default (-1), got: {max_answer_chars=}","messagePattern":"Must be positive or the default \\(-1\\), got: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/serena/tools/tools_base.py","lineNumber":298,"sourceCode":"\n    def _limit_length(\n        self,\n        result: str,\n        max_answer_chars: int,\n        shortened_result_factories: list[Callable[[], str]] | None = None,\n    ) -> str:\n        \"\"\"Limit the length of the result string, optionally trying progressively shorter versions.\n\n        :param result: the full result string\n        :param max_answer_chars: maximum allowed characters. -1 means use the default from config.\n        :param shortened_result_factories: optional list of closures, each producing a progressively shorter\n            version of the result. They are tried in order until one fits within ``max_answer_chars``.\n        :return: the result string, potentially replaced by a shortened version\n        \"\"\"\n        if max_answer_chars == -1:\n            max_answer_chars = self.agent.serena_config.default_max_tool_answer_chars\n        if max_answer_chars <= 0:\n            raise ValueError(f\"Must be positive or the default (-1), got: {max_answer_chars=}\")\n        if (n_chars := len(result)) > max_answer_chars:\n            too_long_msg = (\n                f\"The answer is too long ({n_chars} characters). \" + \"You can adjust your query or raise the max_answer_chars parameter.\"\n            )\n            if shortened_result_factories is not None:\n                # try each shortening closure in order;\n                for make_shorter in shortened_result_factories:\n                    shortened = make_shorter()\n                    candidate = f\"{too_long_msg}\\n{shortened}\"\n                    if len(candidate) <= max_answer_chars:\n                        return candidate\n            result = too_long_msg\n        return result\n\n    def is_active(self) -> bool:\n        return self.agent.tool_is_active(self.get_name())\n\n    def is_readonly(self) -> bool:","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/tools/tools_base.py#L280-L316","documentation":"_limit_length validates max_answer_chars before using it: it must be the sentinel default -1 or a positive integer. Any other value (0, negative numbers other than -1) is rejected with this ValueError.","triggerScenarios":"Calling a listing tool with max_answer_chars=0 or a negative value like -5; programmatically computing a limit that rounds down to 0.","commonSituations":"Config default mis-set to 0; caller passes -1 meaning 'auto' in one tool but 0 in another; dynamic limits computed from empty values (e.g. int(None or 0)).","solutions":["Pass -1 to use serena_config.default_max_tool_answer_chars","Pass a positive integer, e.g. max_answer_chars=10000","Fix the config/computation that produced the invalid value"],"exampleFix":"// before\nlisting_tool.apply(max_answer_chars=0)\n// ValueError: Must be positive or the default (-1), got: max_answer_chars=0\n\n// after\nlisting_tool.apply(max_answer_chars=-1)  # use config default\n# or\nlisting_tool.apply(max_answer_chars=50000)","handlingStrategy":"validation","validationCode":"def sane_limit(v: int, default: int) -> int:\n    return default if v == -1 else (v if v > 0 else default)\nresult = tool.apply(max_answer_chars=sane_limit(requested, agent.serena_config.default_max_tool_answer_chars))","typeGuard":"def valid_limit(v: int) -> bool:\n    return v == -1 or v > 0","tryCatchPattern":"try:\n    result = tool.apply(max_answer_chars=limit)\nexcept ValueError as e:\n    if 'Must be positive' in str(e):\n        result = tool.apply(max_answer_chars=-1)\n    else:\n        raise","preventionTips":["Use -1 (default) or positive integers only","Guard computed limits against 0/negatives with max(1, value)","Audit serena_config.default_max_tool_answer_chars so it is positive"],"tags":["python","validation","parameters","limit-exceeded"],"backgroundTag":"invalid-parameter-value","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}