diff --git a/plugins/home/src/reme_home/agent/structured_report.py b/plugins/home/src/reme_home/agent/structured_report.py
index f4d3bdf1..6ec2bb16 100644
--- a/plugins/home/src/reme_home/agent/structured_report.py
+++ b/plugins/home/src/reme_home/agent/structured_report.py
@@ -72,8 +72,9 @@ def parse_json_object(message: Msg | str, *, report_name: str) -> dict[str, Any]
try:
payload = json.loads(_remove_trailing_commas(text.strip()))
except json.JSONDecodeError:
- text = re.sub(r".*?", "", text, flags=re.DOTALL | re.IGNORECASE).strip()
- if "```" in text:
+ # Strip only leading reasoning blocks; formatting inside report strings is data.
+ text = re.sub(r"^(?:\s*.*?)+\s*", "", text, flags=re.DOTALL | re.IGNORECASE).strip()
+ if re.search(r"^[ \t]*```", text, flags=re.MULTILINE):
fences = list(
re.finditer(
r"^[ \t]*```(?:json)?[ \t]*\r?\n(.*?)^[ \t]*```[ \t]*\r?$",
diff --git a/plugins/home/tests/test_structured_report.py b/plugins/home/tests/test_structured_report.py
index c6e32ede..1bb45653 100644
--- a/plugins/home/tests/test_structured_report.py
+++ b/plugins/home/tests/test_structured_report.py
@@ -3,6 +3,7 @@
# pylint: disable=missing-function-docstring,missing-class-docstring,protected-access,unused-argument
import asyncio
+import json
from unittest.mock import AsyncMock
import pytest
@@ -51,17 +52,19 @@ def test_invalid_or_ambiguous_reports_require_correction(text):
parse_json_object(AssistantMsg(name="review", content=text), report_name="review")
-@pytest.mark.parametrize("template", ["{}", "Completed.\n```json\n{}\n```"])
-def test_text_input_preserves_literal_formatting_inside_json_strings(template):
- text = template.format('{"reason":"literal ```json"}')
- assert parse_json_object(text, report_name="review") == {
- "reason": "literal ```json",
- }
-
-
-def test_bare_json_preserves_literal_thinking_tags():
- text = '{"reason":"literal evidence"}'
- assert parse_json_object(text, report_name="review") == {"reason": "literal evidence"}
+@pytest.mark.parametrize(
+ "template",
+ [
+ "{}",
+ "Completed. {}",
+ "Completed.\n```json\n{}\n```",
+ '{{"draft": "ignored"}}\nMore analysis.\n{}',
+ ],
+)
+@pytest.mark.parametrize("literal", ["literal ```json", "literal evidence", "evidence"])
+def test_text_input_preserves_literal_formatting_inside_json_strings(template, literal):
+ value = {"reason": literal}
+ assert parse_json_object(template.format(json.dumps(value)), report_name="review") == value
def test_repairs_preserve_strings_and_nested_json():
diff --git a/plugins/home/tests/test_workflows.py b/plugins/home/tests/test_workflows.py
index 678da439..e26fbec5 100644
--- a/plugins/home/tests/test_workflows.py
+++ b/plugins/home/tests/test_workflows.py
@@ -115,6 +115,17 @@ def test_parse_optimizer_result_formats(template):
assert optimizer_module.parse_result(template.format(json.dumps(claim))).model_dump() == claim
+@pytest.mark.asyncio
+async def test_optimizer_job_preserves_literal_formatting_in_final_report(app, prepared, sandbox):
+ path = await baseline(app)
+ claim = json.loads(final_report(prepared, path=path))
+ claim["report"] = "Evidence from the evaluated source\n```python\nexample\n```"
+ set_model(app, "home_optimizer", ScriptedModel(final="```json\n" + json.dumps(claim) + "\n```"))
+ response = await app.run_job("home_optimize")
+ assert response.success, response.answer
+ assert response.answer["report"] == claim["report"]
+
+
def test_optimizer_shared_recovery_rejects_competing_and_non_object_reports():
claim = {"branch_name": "candidate", "commit_sha": "a" * 40, "evaluation_path": "run", "report": "Evidence"}
text = json.dumps(claim)