@@ -1262,33 +1262,30 @@ def insert_relative_to_section(
12621262 def _prepend_after_frontmatter (self , current_content : str , content : str ) -> str :
12631263 """Prepend content after frontmatter, preserving frontmatter structure."""
12641264
1265- # Check if file has frontmatter
1265+ # Trigger: the note starts with frontmatter delimiters.
1266+ # Why: prepend must preserve the existing YAML block and insert content into the body,
1267+ # not silently rewrite malformed metadata into a corrupted accepted note state.
1268+ # Outcome: valid frontmatter is preserved, and malformed frontmatter fails fast.
12661269 if has_frontmatter (current_content ):
1267- try :
1268- # Parse and separate frontmatter from body
1269- frontmatter_data = parse_frontmatter (current_content )
1270- body_content = remove_frontmatter (current_content )
1271-
1272- # Prepend content to the body
1273- if content and not content .endswith ("\n " ):
1274- new_body = content + "\n " + body_content
1275- else :
1276- new_body = content + body_content
1270+ # Parse and separate frontmatter from body. Parse errors are intentional caller-visible
1271+ # failures so prepare_edit_entity_content can reject unsafe accepted writes.
1272+ frontmatter_data = parse_frontmatter (current_content )
1273+ body_content = remove_frontmatter (current_content )
1274+
1275+ # Prepend content to the body
1276+ if content and not content .endswith ("\n " ):
1277+ new_body = content + "\n " + body_content
1278+ else :
1279+ new_body = content + body_content
12771280
1278- # Reconstruct file with frontmatter + prepended body
1279- yaml_fm = yaml .dump (frontmatter_data , sort_keys = False , allow_unicode = True )
1280- return f"---\n { yaml_fm } ---\n \n { new_body .strip ()} "
1281+ # Reconstruct file with frontmatter + prepended body
1282+ yaml_fm = yaml .dump (frontmatter_data , sort_keys = False , allow_unicode = True )
1283+ return f"---\n { yaml_fm } ---\n \n { new_body .strip ()} "
12811284
1282- except Exception as e : # pragma: no cover
1283- logger .warning (
1284- f"Failed to parse frontmatter during prepend: { e } "
1285- ) # pragma: no cover
1286- # Fall back to simple prepend if frontmatter parsing fails # pragma: no cover
1287-
1288- # No frontmatter or parsing failed - do simple prepend # pragma: no cover
1289- if content and not content .endswith ("\n " ): # pragma: no cover
1290- return content + "\n " + current_content # pragma: no cover
1291- return content + current_content # pragma: no cover
1285+ # No frontmatter means prepend is a plain text edit.
1286+ if content and not content .endswith ("\n " ):
1287+ return content + "\n " + current_content
1288+ return content + current_content
12921289
12931290 async def move_entity (
12941291 self ,
0 commit comments