@@ -568,17 +568,25 @@ async def move_note(
568568 # Use typed KnowledgeClient for API calls
569569 knowledge_client = KnowledgeClient (client , active_project .external_id )
570570
571- # Get the source entity information for extension validation
571+ # Resolve once and reuse the entity ID across extension validation and move.
572572 source_ext = "md" # Default to .md if we can't determine source extension
573+ resolved_entity_id : str | None = None
574+ source_entity = None
575+
576+ async def _ensure_resolved_entity_id () -> str :
577+ """Resolve and cache the source entity ID for the duration of this move."""
578+ nonlocal resolved_entity_id
579+ if resolved_entity_id is None :
580+ resolved_entity_id = await knowledge_client .resolve_entity (identifier )
581+ return resolved_entity_id
582+
573583 try :
574- # Resolve identifier to entity ID
575- entity_id = await knowledge_client .resolve_entity (identifier )
576- # Fetch source entity information to get the current file extension
577- source_entity = await knowledge_client .get_entity (entity_id )
584+ resolved_entity_id = await _ensure_resolved_entity_id ()
585+ source_entity = await knowledge_client .get_entity (resolved_entity_id )
578586 if "." in source_entity .file_path :
579587 source_ext = source_entity .file_path .split ("." )[- 1 ]
580588 except Exception as e :
581- # If we can't fetch the source entity, default to .md extension
589+ # If we can't fetch source metadata, continue with extension defaults.
582590 logger .debug (f"Could not fetch source entity for extension check: { e } " )
583591
584592 # Validate that destination path includes a file extension
@@ -612,14 +620,15 @@ async def move_note(
612620 All examples in Basic Memory expect file extensions to be explicitly provided.
613621 """ ).strip ()
614622
615- # Get the source entity to check its file extension
616- try :
617- # Resolve identifier to entity ID (might already be cached from above)
618- entity_id = await knowledge_client .resolve_entity (identifier )
619- # Fetch source entity information
620- source_entity = await knowledge_client .get_entity (entity_id )
623+ # Validate extension consistency when source metadata is available.
624+ if source_entity is None :
625+ try :
626+ resolved_entity_id = await _ensure_resolved_entity_id ()
627+ source_entity = await knowledge_client .get_entity (resolved_entity_id )
628+ except Exception as e :
629+ logger .debug (f"Could not fetch source entity for extension check: { e } " )
621630
622- # Extract file extensions
631+ if source_entity is not None :
623632 source_ext = (
624633 source_entity .file_path .split ("." )[- 1 ] if "." in source_entity .file_path else ""
625634 )
@@ -656,17 +665,13 @@ async def move_note(
656665 move_note("{ identifier } ", "{ destination_path .rsplit ("." , 1 )[0 ]} .{ source_ext } ")
657666 ```
658667 """ ).strip ()
659- except Exception as e :
660- # If we can't fetch the source entity, log it but continue
661- # This might happen if the identifier is not yet resolved
662- logger .debug (f"Could not fetch source entity for extension check: { e } " )
663668
664669 try :
665- # Resolve identifier to entity ID for the move operation
666- entity_id = await knowledge_client . resolve_entity ( identifier )
670+ # Resolve identifier only if earlier checks could not.
671+ resolved_entity_id = await _ensure_resolved_entity_id ( )
667672
668673 # Call the move API using KnowledgeClient
669- result = await knowledge_client .move_entity (entity_id , destination_path )
674+ result = await knowledge_client .move_entity (resolved_entity_id , destination_path )
670675 if output_format == "json" :
671676 return {
672677 "moved" : True ,
0 commit comments