diff --git a/dokimi/changelog.d/20261117_150000_json-extract-linear.md b/dokimi/changelog.d/20261117_150000_json-extract-linear.md new file mode 100644 index 000000000..f51cd582c --- /dev/null +++ b/dokimi/changelog.d/20261117_150000_json-extract-linear.md @@ -0,0 +1,8 @@ + + +### Fixed + +- **A model reply that opens many `[` or `{` and never closes them is rejected at once** — `extract_json_array` and `extract_json_object` took their last-resort block with a regex that backtracked quadratically (about 10 s for 100000 openers), and the fence regex did the same on a fence followed by a long run of whitespace. Both now find the block with `str.find` and `str.rfind`, and the fence regex no longer retries per space. The text each function returns or rejects is unchanged. diff --git a/dokimi/dokimi/llm_json.py b/dokimi/dokimi/llm_json.py index 582fffee5..c528c7360 100644 --- a/dokimi/dokimi/llm_json.py +++ b/dokimi/dokimi/llm_json.py @@ -8,6 +8,15 @@ import json import re +# No \s* before the body: callers strip it, and \s* retries the lazy body once per space (#883). +_FENCE = re.compile(r"```(?:json)?([\s\S]*?)```") + + +def _outer_block(text: str, opener: str, closer: str) -> str | None: + """From the first ``opener`` to the last ``closer`` after it; a regex backtracks quadratically on a run of openers.""" + i, j = text.find(opener), text.rfind(closer) + return text[i : j + 1] if i != -1 and j > i else None + def extract_json_object(text: str) -> dict: """Extract a JSON object from LLM output, handling markdown fences and leading text. @@ -28,7 +37,7 @@ def extract_json_object(text: str) -> dict: except (ValueError, RecursionError): # not JSON here: try the next form pass # Markdown fence - fence = re.search(r"```(?:json)?\s*([\s\S]*?)```", text) + fence = _FENCE.search(text) if fence: try: result = json.loads(fence.group(1).strip()) @@ -37,10 +46,10 @@ def extract_json_object(text: str) -> dict: except (ValueError, RecursionError): pass # First { ... } block - m = re.search(r"\{[\s\S]*\}", text) - if m: + block = _outer_block(text, "{", "}") + if block is not None: try: - result = json.loads(m.group(0)) + result = json.loads(block) except RecursionError as exc: raise ValueError(f"JSON in LLM response is nested too deeply: {text[:200]!r}") from exc if isinstance(result, dict): @@ -61,7 +70,7 @@ def extract_json_array(text: str) -> list: except (ValueError, RecursionError): # not JSON here: try the next form pass # Markdown fence - fence = re.search(r"```(?:json)?\s*([\s\S]*?)```", text) + fence = _FENCE.search(text) if fence: try: result = json.loads(fence.group(1).strip()) @@ -70,10 +79,10 @@ def extract_json_array(text: str) -> list: except (ValueError, RecursionError): pass # First [ ... ] block - m = re.search(r"\[[\s\S]*\]", text) - if m: + block = _outer_block(text, "[", "]") + if block is not None: try: - result = json.loads(m.group(0)) + result = json.loads(block) except RecursionError as exc: raise ValueError(f"JSON in LLM response is nested too deeply: {text[:200]!r}") from exc if isinstance(result, list): diff --git a/dokimi/tests/test_fuzz_targets.py b/dokimi/tests/test_fuzz_targets.py index a1e1e3626..18623843b 100644 --- a/dokimi/tests/test_fuzz_targets.py +++ b/dokimi/tests/test_fuzz_targets.py @@ -84,6 +84,56 @@ def test_a_bare_deep_reply_is_a_parse_error_not_a_crash(extract_name: str, text: getattr(llm_json, extract_name)(text) +def _outcome(name: str, text: str) -> str: + from dokimi import llm_json + + try: + return repr(getattr(llm_json, name)(text)) + except ValueError: + return "ValueError" + + +@pytest.mark.parametrize("name", ["extract_json_object", "extract_json_array"]) +@pytest.mark.parametrize( + "shape", + [ + lambda k: "[" * k, + lambda k: "{" * k, + lambda k: "```json" + " " * k + "x", + ], + ids=["brackets", "braces", "unclosed-fence-spaces"], +) +def test_extractors_scan_a_reply_without_a_closer_in_linear_time(name: str, shape) -> None: + # CPU time at 4n over n, not a wall-clock bound: the last-block regexes were quadratic (#883). + from tests.growth_timing import GROWTH_LIMIT, growth + + ratio, outcome = growth(lambda k: lambda: _outcome(name, shape(k)), 4000) + assert outcome == "ValueError" + assert ratio < GROWTH_LIMIT + + +@pytest.mark.parametrize( + ("name", "text", "want"), + [ + ("extract_json_array", "x [1, [2]] y", "[1, [2]]"), + ("extract_json_array", "a ] [1] b", "[1]"), + ("extract_json_array", "x [1]z", "[1]"), + ("extract_json_object", '[{"a": 1}]', "{'a': 1}"), + ("extract_json_object", 'x {"a": {"b": 2}} y', "{'a': {'b': 2}}"), + ("extract_json_object", '{"a": 1} then }', "ValueError"), + ], +) +def test_the_last_fallback_still_takes_the_outermost_block(name: str, text: str, want: str) -> None: + assert _outcome(name, text) == want + + +def test_a_reply_with_a_closer_and_no_opener_keeps_the_head_message() -> None: + from dokimi.llm_json import extract_json_array + + with pytest.raises(ValueError, match="No JSON array found"): + extract_json_array("a]") + + def test_a_date_that_is_not_real_is_invalid_frontmatter() -> None: # PyYAML raises a plain ValueError for 2026-02-30: the lenient reader reads no metadata, # and the strict one raises the YAML error its docstring names. diff --git a/dokimi/tests/test_repo_budget.py b/dokimi/tests/test_repo_budget.py index ad11ef7d4..4e59edcf0 100644 --- a/dokimi/tests/test_repo_budget.py +++ b/dokimi/tests/test_repo_budget.py @@ -15,6 +15,7 @@ from dokimi.repos.budget import budget as _budget from dokimi.repos.source import GitTree, SourceError, WorkTree from tests.cli_refusal import refusal +from tests.growth_timing import GROWTH_LIMIT, growth NAMES = ("CLAUDE.md", "CLAUDE.local.md", "AGENTS.md") budget = partial(_budget, claude_displaces_agents=True) # as `repo budget --harness claude-code` calls it @@ -692,15 +693,12 @@ def test_a_code_span_wrapped_onto_the_next_line_hides_its_import(tmp_path): def test_code_span_stripping_is_linear_on_backtick_runs(): """Long backtick runs once made the code-span regex backtrack exponentially (CodeQL py/redos).""" - import time - from dokimi.repos.budget import _strip_code_spans - for text in ("`" * 50_000, "`" + "`a" * 50_000, "``" + "` " * 50_000 + "\n\n@x.md"): - started = time.monotonic() - _strip_code_spans(text) - # Permissive timing check to avoid flakiness on slower/contended runners - assert time.monotonic() - started < 5 + shapes = (lambda k: "`" * k, lambda k: "`" + "`a" * k, lambda k: "``" + "` " * k + "\n\n@x.md") + for shape in shapes: + ratio, _ = growth(lambda k, shape=shape: partial(_strip_code_spans, shape(k)), 1_000) + assert ratio < GROWTH_LIMIT assert _strip_code_spans("a `b` c ``d`e`` f") == "a c f" assert _strip_code_spans("`a\nb` @x.md") == " @x.md" assert _strip_code_spans("`a\n\nb` @x.md") == "`a\n\nb` @x.md"