fix(memory): parse update facts JSON with raw_decode

Trailing prose with braces made the greedy rfind slice invalid, so
_parse_facts silently dropped valid facts. Decode the first object.
This commit is contained in:
santhreal
2026-07-18 17:26:33 -07:00
parent b030697df7
commit eee0431fff
2 changed files with 22 additions and 3 deletions

View File

@@ -624,10 +624,13 @@ def _extract_json_object(raw: str) -> str | None:
if text.rstrip().endswith("```"):
text = text.rstrip()[:-3]
start = text.find("{")
end = text.rfind("}")
if start == -1 or end <= start:
if start == -1:
return None
return text[start : end + 1]
try:
_parsed, end = json.JSONDecoder().raw_decode(text[start:])
except json.JSONDecodeError:
return None
return text[start : start + end]
def _append_facts_to_doc(

View File

@@ -0,0 +1,16 @@
"""Regression: memory update fact JSON tolerates trailing brace prose."""
from __future__ import annotations
from deeptutor.services.memory.consolidator.modes import update as update_mode
def test_parse_facts_tolerates_trailing_brace_prose() -> None:
raw = (
'{"facts":[{"text":"hello","section":"Notes","refs":["a:b"]}]}'
" trailing {note}"
)
facts = update_mode._parse_facts(raw)
assert len(facts) == 1
assert facts[0].text == "hello"
assert facts[0].section == "Notes"