Skip to content

Commit 6d12c7b

Browse files
phernandezclaude
andcommitted
fix: respect frontmatter entity_type in write_note operations
- Add frontmatter type extraction in EntityService.create_entity() and update_entity() - Override schema.entity_type when content frontmatter contains 'type' field - Fix lint error in alembic/env.py with noqa comment - Update test formatting for consistency - Ensures write_note respects both parameter and frontmatter entity_type values Fixes #144 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 26786ce commit 6d12c7b

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/basic_memory/alembic/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# set config.env to "test" for pytest to prevent logging to file in utils.setup_logging()
1414
os.environ["BASIC_MEMORY_ENV"] = "test"
1515

16-
from basic_memory.config import app_config
16+
from basic_memory.config import app_config # noqa: E402
1717

1818
# this is the Alembic Config object, which provides
1919
# access to the values within the .ini file in use.

src/basic_memory/services/entity_service.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,15 @@ async def create_entity(self, schema: EntitySchema) -> EntityModel:
117117
f"file for entity {schema.folder}/{schema.title} already exists: {file_path}"
118118
)
119119

120-
# Parse content frontmatter to check for user-specified permalink
120+
# Parse content frontmatter to check for user-specified permalink and entity_type
121121
content_markdown = None
122122
if schema.content and has_frontmatter(schema.content):
123123
content_frontmatter = parse_frontmatter(schema.content)
124+
125+
# If content has entity_type/type, use it to override the schema entity_type
126+
if "type" in content_frontmatter:
127+
schema.entity_type = content_frontmatter["type"]
128+
124129
if "permalink" in content_frontmatter:
125130
# Create a minimal EntityMarkdown object for permalink resolution
126131
from basic_memory.markdown.schemas import EntityFrontmatter
@@ -172,10 +177,15 @@ async def update_entity(self, entity: EntityModel, schema: EntitySchema) -> Enti
172177
# Read existing frontmatter from the file if it exists
173178
existing_markdown = await self.entity_parser.parse_file(file_path)
174179

175-
# Parse content frontmatter to check for user-specified permalink
180+
# Parse content frontmatter to check for user-specified permalink and entity_type
176181
content_markdown = None
177182
if schema.content and has_frontmatter(schema.content):
178183
content_frontmatter = parse_frontmatter(schema.content)
184+
185+
# If content has entity_type/type, use it to override the schema entity_type
186+
if "type" in content_frontmatter:
187+
schema.entity_type = content_frontmatter["type"]
188+
179189
if "permalink" in content_frontmatter:
180190
# Create a minimal EntityMarkdown object for permalink resolution
181191
from basic_memory.markdown.schemas import EntityFrontmatter

tests/mcp/test_tool_write_note.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ async def test_write_note_preserves_content_frontmatter(app):
418418
@pytest.mark.asyncio
419419
async def test_write_note_with_custom_entity_type(app):
420420
"""Test creating a note with custom entity_type parameter.
421-
422-
This test verifies the fix for Issue #144 where entity_type parameter
421+
422+
This test verifies the fix for Issue #144 where entity_type parameter
423423
was hardcoded to "note" instead of allowing custom types.
424424
"""
425425
result = await write_note.fn(
@@ -503,7 +503,7 @@ async def test_write_note_with_config_entity_type(app):
503503
@pytest.mark.asyncio
504504
async def test_write_note_entity_type_default_behavior(app):
505505
"""Test that the entity_type parameter defaults to "note" when not specified.
506-
506+
507507
This ensures backward compatibility - existing code that doesn't specify
508508
entity_type should continue to work as before.
509509
"""
@@ -559,10 +559,10 @@ async def test_write_note_update_existing_with_different_entity_type(app):
559559
assert "- guide" in content
560560

561561

562-
@pytest.mark.asyncio
562+
@pytest.mark.asyncio
563563
async def test_write_note_respects_frontmatter_entity_type(app):
564564
"""Test that entity_type in frontmatter is respected when parameter is not provided.
565-
565+
566566
This verifies that when write_note is called without entity_type parameter,
567567
but the content includes frontmatter with a 'type' field, that type is respected
568568
instead of defaulting to 'note'.
@@ -582,11 +582,7 @@ async def test_write_note_respects_frontmatter_entity_type(app):
582582
""").strip()
583583

584584
# Call write_note without entity_type parameter - it should respect frontmatter type
585-
result = await write_note.fn(
586-
title="Test Guide",
587-
folder="guides",
588-
content=note
589-
)
585+
result = await write_note.fn(title="Test Guide", folder="guides", content=note)
590586

591587
assert result
592588
assert "# Created note" in result

0 commit comments

Comments
 (0)