Skip to content

write_note should preserve frontmatter passed in via content of note #47

Closed

Description

@phernandez

Feature Description

The write_note tool currently ignores any frontmatter passed in via the content of the note. It should try to preserve it.

For example

{
  `title`: `Custom Frontmatter Test`,
  `folder`: `tests`,
  `content`: `---
Status: In Progress
Priority: High
CustomField: TestValue
Version: 1.0
DueDate: 2025-04-30
---

# Custom Frontmatter Test

This is a test note to verify that custom frontmatter fields are preserved when updating notes.

- [task] Test frontmatter preservation #test #bug
- [note] This note has several custom fields (Status, Priority, CustomField, Version, DueDate)
`
}

will only contain the following frontmatter fields:

title: Custom Frontmatter Test
type: note
permalink: tests/custom-frontmatter-test

Activity

  1. added this to the Core Functionality milestone on Mar 25, 2025
  2. phernandez commented on Apr 6, 2025

    @phernandez
    MemberAuthor

    claude: how can we fix this?

  3. github-actions commented on Apr 6, 2025

    @github-actions
    Contributor

    Claude Code Analysis

    Based on my analysis of the issue and the codebase, here's what's happening with the frontmatter preservation problem:

    Problem Analysis

    When using the write_note tool with content that contains frontmatter, the custom frontmatter fields are being stripped out during processing. The issue is in how incoming content with frontmatter is handled:

    1. In schema_to_markdown (markdown/utils.py lines 83-84), any frontmatter in the incoming content is detected and removed:

      # if the content contains frontmatter, remove it and merge
      if has_frontmatter(content):
          content = remove_frontmatter(content)
    2. Then a new Post is created with only the standard fields (title, type, permalink) plus any metadata from entity_metadata:

      post = Post(
          content,
          title=schema.title,
          type=schema.entity_type,
          permalink=schema.permalink,
          **frontmatter_metadata,
      )
    3. For existing notes, the system correctly merges with existing frontmatter, but for new notes or when creating the initial Post object, it discards any frontmatter in the content.

    Proposed Solution

    The fix would require modifying the schema_to_markdown function in src/basic_memory/markdown/utils.py to parse and preserve frontmatter from the incoming content:

    async def schema_to_markdown(schema: Any) -> Post:
        """Convert schema to markdown Post object."""
        # Extract content and metadata
        content = schema.content or ""
        frontmatter_metadata = dict(schema.entity_metadata or {})
        
        # Parse frontmatter from content if present
        content_frontmatter = {}
        if has_frontmatter(content):
            content_frontmatter = parse_frontmatter(content)
            content = remove_frontmatter(content)
            
            # Merge content frontmatter with entity metadata
            # (entity_metadata takes precedence for conflicts)
            content_frontmatter.update(frontmatter_metadata)
            frontmatter_metadata = content_frontmatter
    
        # Remove special fields for ordered frontmatter
        for field in ["type", "title", "permalink"]:
            frontmatter_metadata.pop(field, None)
    
        # Create Post with ordered fields
        post = Post(
            content,
            title=schema.title,
            type=schema.entity_type,
            permalink=schema.permalink,
            **frontmatter_metadata,
        )
        return post

    This change would:

    1. Detect if the incoming content has frontmatter
    2. Parse that frontmatter into a dictionary
    3. Merge it with any entity_metadata that was passed separately
    4. Use the merged metadata when creating the Post object

    Files to Modify

    Only one file needs to be modified:

    • /home/runner/work/basic-memory/basic-memory/src/basic_memory/markdown/utils.py

    Testing

    A test could be added to verify this behavior:

    • Create a note with frontmatter in the content
    • Verify the frontmatter fields are preserved in the saved file
    • Update the note with modified frontmatter
    • Verify all fields (original and updated) are present

    The current issue happens because the write_note tool discards any frontmatter in the incoming content, rather than parsing and including it in the note's metadata.


    🤖 Generated with Claude Code GitHub Action

  4. self-assigned this
    on Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions