mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
fix: avoid Post(**metadata) crash when frontmatter contains 'content' or 'handler' keys
frontmatter.Post.__init__ takes `content` and `handler` as positional parameters. When user YAML frontmatter contains these as field names, unpacking metadata via **kwargs causes "got multiple values for argument 'content'". Replace frontmatter.loads() with frontmatter.parse() + Post() + update() in entity_parser, and replace the **metadata unpacking in entity_service.update_entity() with the same safe pattern. Fixes basic-memory-cloud#375 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: phernandez <paul@basicmachines.co>
This commit is contained in:
@@ -249,9 +249,15 @@ class EntityParser:
|
||||
|
||||
content = strip_bom(content)
|
||||
|
||||
# Parse frontmatter with proper error handling for malformed YAML
|
||||
# Parse frontmatter with proper error handling for malformed YAML.
|
||||
# We use frontmatter.parse() instead of frontmatter.loads() because
|
||||
# loads() does Post(content, handler, **metadata), which crashes when
|
||||
# the YAML contains reserved keys like 'content' or 'handler'.
|
||||
# See basic-memory-cloud#375.
|
||||
try:
|
||||
post = frontmatter.loads(content)
|
||||
fm_metadata, fm_content = frontmatter.parse(content)
|
||||
post = frontmatter.Post(fm_content)
|
||||
post.metadata.update(fm_metadata)
|
||||
except yaml.YAMLError as e:
|
||||
logger.warning(
|
||||
f"Failed to parse YAML frontmatter in {file_path}: {e}. "
|
||||
|
||||
@@ -361,8 +361,11 @@ class EntityService(BaseService[EntityModel]):
|
||||
# in the existing file. Setting it unconditionally preserves the correct value.
|
||||
existing_markdown.frontmatter.metadata["permalink"] = new_permalink
|
||||
|
||||
# Create a new post with merged metadata
|
||||
merged_post = frontmatter.Post(post.content, **existing_markdown.frontmatter.metadata)
|
||||
# Create a new post with merged metadata.
|
||||
# Avoid **metadata unpacking — user frontmatter may contain reserved keys
|
||||
# like 'content' or 'handler' that conflict with Post.__init__ (cloud#375).
|
||||
merged_post = frontmatter.Post(post.content)
|
||||
merged_post.metadata.update(existing_markdown.frontmatter.metadata)
|
||||
|
||||
# write file
|
||||
final_content = dump_frontmatter(merged_post)
|
||||
|
||||
Reference in New Issue
Block a user