Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dokimi/changelog.d/20261117_150000_json-extract-linear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--
Copyright (C) 2026 Intel Corporation
SPDX-License-Identifier: Apache-2.0
-->

### 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.
25 changes: 17 additions & 8 deletions dokimi/dokimi/llm_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
Expand All @@ -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):
Expand All @@ -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())
Expand All @@ -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):
Expand Down
50 changes: 50 additions & 0 deletions dokimi/tests/test_fuzz_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
vchamarthi marked this conversation as resolved.
Resolved


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.
Expand Down
12 changes: 5 additions & 7 deletions dokimi/tests/test_repo_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Loading