Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
16 changes: 8 additions & 8 deletions scripts/ci/noema_review_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,15 @@ def redirect_request(


def extract_json_object(text: str) -> dict[str, Any]:
"""Extract a JSON object from a strict or lightly wrapped LLM response."""
stripped = text.strip()
if stripped.startswith("{"):
return json.loads(stripped)
start = stripped.find("{")
end = stripped.rfind("}")
if start < 0 or end < start:
"""Extract the first JSON object from a strict or lightly wrapped response."""
start = text.find("{")
if start < 0:
raise RuntimeError("Noema LLM response did not contain a JSON object")
try:
value, _ = json.JSONDecoder().raw_decode(text, start)
return value
except json.JSONDecodeError:
raise RuntimeError("Noema LLM response did not contain a JSON object")
Comment thread
seonghobae marked this conversation as resolved.
Outdated
return json.loads(stripped[start : end + 1])


def call_llm(
Expand Down
6 changes: 5 additions & 1 deletion tests/test_noema_review_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,12 @@ def test_current_actor_fetch_diff_and_json_extraction(monkeypatch):

assert noema.extract_json_object('{"decision":"approve"}') == {"decision": "approve"}
assert noema.extract_json_object('prefix {"decision":"comment"} suffix') == {"decision": "comment"}
assert noema.extract_json_object('{"decision":"comment"} and some extra trailing text } that could break rfind') == {"decision": "comment"}
with pytest.raises(RuntimeError, match="did not contain"):
noema.extract_json_object("not-json")
noema.extract_json_object('{not a valid json}')
for non_object in ("not-json", "[]"):
with pytest.raises(RuntimeError, match="did not contain"):
noema.extract_json_object(non_object)


def test_review_context_builders_include_codegraph_threads_and_files(monkeypatch, tmp_path):
Expand Down
Loading