Skip to content

The agent that grows with you

fix(cli): refuse to persist a truncated JSON reply as profile description

Edizzier1f16bbf108a8
diff --git a/hermes_cli/profile_describer.py b/hermes_cli/profile_describer.py
index 97f04aa9f95..2e17e0b19f9 100644
--- a/hermes_cli/profile_describer.py
+++ b/hermes_cli/profile_describer.py
@@ -62,7 +62,7 @@ Notable skills (up to {skill_cap}):
"""
-_FENCE_RE = re.compile(r"^```(?:json)?\s*|\s*```$", re.MULTILINE)
+_FENCE_RE = re.compile(r"^```(?:json)?\s*|\s*```$", re.MULTILINE | re.IGNORECASE)
@dataclass
@@ -166,6 +166,19 @@ def describe_profile(profile_name: str, *, overwrite: bool = False, timeout: Opt
raw = ""
parsed = _extract_json_blob(raw)
if parsed is None:
+ # A response that is JSON-SHAPED (starts with `{`, once code fences are stripped) but
+ # failed to parse is a malformed/truncated structured reply -- e.g. the aux model or
+ # transport cut it off mid-object -- not free-form prose. Persisting it verbatim writes
+ # a raw JSON fragment (literal `{`, `\n` escapes, a sentence chopped mid-word) into
+ # profile.yaml's description field (#104067). Only fall back to "whole reply is prose"
+ # when the reply never looked like JSON in the first place.
+ stripped = _FENCE_RE.sub("", raw.strip())
+ if stripped.startswith("{"):
+ logger.info(
+ "describe: %s aux response looked JSON-shaped but failed to parse "
+ "(likely truncated) -- refusing to persist the raw fragment", canon,
+ )
+ return DescribeOutcome(canon, False, "LLM returned malformed/truncated JSON response")
# Fall back: raw text trimmed to one paragraph.
text = raw.strip().split("\n\n", 1)[0]
if not text:
diff --git a/tests/hermes_cli/test_profile_describer.py b/tests/hermes_cli/test_profile_describer.py
index d056fc2580a..649c44da222 100644
--- a/tests/hermes_cli/test_profile_describer.py
+++ b/tests/hermes_cli/test_profile_describer.py
@@ -76,6 +76,103 @@ def test_describer_writes_description_with_auto_true(profile_env, monkeypatch):
assert meta["description_auto"] is True
+def test_describer_rejects_truncated_json_response(profile_env, monkeypatch):
+ # Real-world shape (#104067): the aux model's response is cut off mid-object -- no
+ # closing brace/quote -- so `_extract_json_blob` can't parse it. The old fallback
+ # persisted this raw fragment verbatim as the description; it must now be refused.
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda n: n == "myprof")
+ monkeypatch.setattr(profiles_mod, "normalize_profile_name", lambda n: n)
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda n: profile_env)
+
+ truncated = '{\n "description": "Generalist agent that writes and debugs code, orchestrates autonomous sub-agents, and automates macOS/App'
+ with _patch_aux_client(truncated), patch(
+ "agent.auxiliary_client.get_auxiliary_extra_body", return_value={}
+ ):
+ outcome = describer.describe_profile("myprof")
+
+ assert outcome.ok is False
+ assert "malformed" in outcome.reason.lower() or "truncated" in outcome.reason.lower()
+ # Nothing was written: no profile.yaml, or if one exists it has no description key.
+ meta = profiles_mod.read_profile_meta(profile_env)
+ assert not meta.get("description")
+
+
+def test_describer_rejects_json_shaped_response_with_no_closing_brace_at_all(profile_env, monkeypatch):
+ # Even more truncated: cut off right after the opening brace, before any key.
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda n: n == "myprof")
+ monkeypatch.setattr(profiles_mod, "normalize_profile_name", lambda n: n)
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda n: profile_env)
+
+ with _patch_aux_client("{\n \"desc"), patch(
+ "agent.auxiliary_client.get_auxiliary_extra_body", return_value={}
+ ):
+ outcome = describer.describe_profile("myprof")
+
+ assert outcome.ok is False
+ meta = profiles_mod.read_profile_meta(profile_env)
+ assert not meta.get("description")
+
+
+def test_describer_rejects_fenced_json_shaped_truncated_response(profile_env, monkeypatch):
+ # A ```json fence around a truncated object must still be recognized as JSON-shaped
+ # after fence stripping, not fall through to the raw-text fallback.
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda n: n == "myprof")
+ monkeypatch.setattr(profiles_mod, "normalize_profile_name", lambda n: n)
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda n: profile_env)
+
+ fenced_truncated = '```json\n{\n "description": "Generalist agent that writes and debugs cod'
+ with _patch_aux_client(fenced_truncated), patch(
+ "agent.auxiliary_client.get_auxiliary_extra_body", return_value={}
+ ):
+ outcome = describer.describe_profile("myprof")
+
+ assert outcome.ok is False
+ meta = profiles_mod.read_profile_meta(profile_env)
+ assert not meta.get("description")
+
+
+def test_describer_rejects_uppercase_fenced_json_shaped_truncated_response(profile_env, monkeypatch):
+ # Regression for review feedback on #104075: an uppercase ```JSON fence must be
+ # stripped the same as a lowercase one. Before, _FENCE_RE was case-sensitive, so
+ # stripping this left "JSON\n{...}" -- which doesn't start with "{" -- and the
+ # truncated fragment fell through to the raw-text-as-prose fallback and got persisted
+ # anyway, defeating the fix for this fence variant.
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda n: n == "myprof")
+ monkeypatch.setattr(profiles_mod, "normalize_profile_name", lambda n: n)
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda n: profile_env)
+
+ fenced_truncated = '```JSON\n{\n "description": "Generalist agent that writes and debugs cod'
+ with _patch_aux_client(fenced_truncated), patch(
+ "agent.auxiliary_client.get_auxiliary_extra_body", return_value={}
+ ):
+ outcome = describer.describe_profile("myprof")
+
+ assert outcome.ok is False
+ meta = profiles_mod.read_profile_meta(profile_env)
+ assert not meta.get("description")
+
+
+def test_describer_still_accepts_plain_prose_fallback(profile_env, monkeypatch):
+ # Regression guard: a model that ignores the JSON instruction and just replies in
+ # plain prose (never looked JSON-shaped) must still hit the existing lenient
+ # raw-text fallback -- this fix must not make the describer stricter than before
+ # for genuinely non-JSON replies.
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda n: n == "myprof")
+ monkeypatch.setattr(profiles_mod, "normalize_profile_name", lambda n: n)
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda n: profile_env)
+
+ with _patch_aux_client("Writes and debugs Python codebases end to end."), patch(
+ "agent.auxiliary_client.get_auxiliary_extra_body", return_value={}
+ ):
+ outcome = describer.describe_profile("myprof")
+
+ assert outcome.ok, outcome.reason
+ assert outcome.description == "Writes and debugs Python codebases end to end."
+ meta = profiles_mod.read_profile_meta(profile_env)
+ assert meta["description"] == "Writes and debugs Python codebases end to end."
+ assert meta["description_auto"] is True
+
+
def test_describer_refuses_to_overwrite_user_authored(profile_env, monkeypatch):
profiles_mod.write_profile_meta(
profile_env, description="curated", description_auto=False,