Add reasoning control and JSON escape repair
This commit is contained in:
2 files changed
+31
-1
No files matched your search
+22
-1
@@ -165,6 +165,9 @@ CATEGORY_TO_CWE = {
|
||||
RETRYABLE_HTTP_STATUS = {408, 425, 429, 500, 502, 503, 504}
|
||||
FENCE_RE = re.compile(r"^\s*```(?:json)?\s*(.*?)\s*```\s*$", re.IGNORECASE | re.DOTALL)
|
||||
TRAILING_COMMA_RE = re.compile(r",\s*([}\]])")
|
||||
# JSON only allows a fixed set of backslash escapes. Some models emit
|
||||
# Python-style escapes such as \' inside their reasoning strings.
|
||||
INVALID_ESCAPE_RE = re.compile(r'\\(?!["\\/bfnrtu])')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -623,9 +626,12 @@ def _balanced_object(text: str) -> dict[str, Any] | None:
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
candidate = text[start : index + 1]
|
||||
without_trailing_comma = TRAILING_COMMA_RE.sub(r"\1", candidate)
|
||||
for attempt in (
|
||||
candidate,
|
||||
TRAILING_COMMA_RE.sub(r"\1", candidate),
|
||||
without_trailing_comma,
|
||||
INVALID_ESCAPE_RE.sub("", candidate),
|
||||
INVALID_ESCAPE_RE.sub("", without_trailing_comma),
|
||||
):
|
||||
try:
|
||||
parsed = json.loads(attempt)
|
||||
@@ -968,6 +974,10 @@ class Runner:
|
||||
payload["temperature"] = self.args.temperature
|
||||
if self.args.max_tokens is not None:
|
||||
payload["max_tokens"] = self.args.max_tokens
|
||||
if getattr(self.args, "no_reasoning", False):
|
||||
# OpenRouter supports this unified switch for reasoning models.
|
||||
# Other OpenAI-compatible providers generally ignore unknown fields.
|
||||
payload["reasoning"] = {"enabled": False}
|
||||
|
||||
attempts: list[Attempt] = []
|
||||
call_index = start_call_index
|
||||
@@ -1583,6 +1593,7 @@ def print_dry_run(context: RunContext, args: argparse.Namespace) -> None:
|
||||
"Temperature: "
|
||||
f"{'omitted' if args.no_temperature else args.temperature}"
|
||||
)
|
||||
print(f"Reasoning: {'off' if args.no_reasoning else 'provider default'}")
|
||||
|
||||
|
||||
def run(args: argparse.Namespace) -> int:
|
||||
@@ -1939,6 +1950,16 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
default=None,
|
||||
help="Optional output-token limit.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-reasoning",
|
||||
"--no-thinking",
|
||||
dest="no_reasoning",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Disable provider reasoning/thinking where supported. OpenRouter "
|
||||
"receives reasoning={enabled: false}; other endpoints may ignore it."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--delay",
|
||||
type=float,
|
||||
|
||||
@@ -284,6 +284,15 @@ class PipelineTest(unittest.TestCase):
|
||||
self.assertEqual(inferred.category, "redirect")
|
||||
self.assertEqual(inferred.cwe, "CWE-601")
|
||||
|
||||
# Some models emit Python-style \' escapes, which are invalid JSON.
|
||||
repaired_escape = parse_result(
|
||||
r'{"reasoning": "path = f\'{bar}\'", '
|
||||
'"vulnerable": true, "cwe": "CWE-22"}'
|
||||
)
|
||||
self.assertTrue(repaired_escape.ok)
|
||||
self.assertEqual(repaired_escape.category, "pathtraver")
|
||||
self.assertEqual(repaired_escape.cwe, "CWE-22")
|
||||
|
||||
for cwe, category in (
|
||||
("CWE-94", "codeinj"),
|
||||
("CWE-328", "hash"),
|
||||
|
||||
Reference in new issue
Block a user