From 9dee331e6c69d4a63e5c292f406c70d4735bcea3 Mon Sep 17 00:00:00 2001 From: cthomas Date: Fri, 6 Feb 2026 10:37:24 -0800 Subject: [PATCH] fix(core): backfill missing blocks when git repo is incomplete (#9340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When enable_git_memory_for_agent is called on an agent that already has the git-memory-enabled tag, it was returning early if the repo existed, even if the repo was missing blocks. Now checks if all blocks are present in the repo and backfills any missing ones. 🐾 Generated with [Letta Code](https://letta.com) Co-authored-by: Letta --- letta/services/block_manager_git.py | 32 ++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/letta/services/block_manager_git.py b/letta/services/block_manager_git.py index b1f4d161..43277ac2 100644 --- a/letta/services/block_manager_git.py +++ b/letta/services/block_manager_git.py @@ -361,7 +361,37 @@ class GitEnabledBlockManager(BlockManager): try: # Fast check: does the repo exist in backing storage? await self.memory_repo_manager.git.get_head_sha(agent_id=agent_id, org_id=actor.organization_id) - logger.info(f"Git memory already enabled for agent {agent_id}") + + # Repo exists - check if all blocks are present + blocks = await self.get_blocks_by_agent_async(agent_id, actor) + repo_files = await self.memory_repo_manager.git.get_files(agent_id=agent_id, org_id=actor.organization_id, ref="HEAD") + + # Check which blocks are missing from repo + missing_blocks = [] + for block in blocks: + expected_path = f"memory/{block.label}.md" + if expected_path not in repo_files: + missing_blocks.append(block) + + if missing_blocks: + logger.warning( + "Git memory repo exists but missing %d/%d blocks for agent %s; backfilling", + len(missing_blocks), + len(blocks), + agent_id, + ) + # Commit missing blocks + for block in missing_blocks: + await self.memory_repo_manager.update_block_async( + agent_id=agent_id, + label=block.label, + value=block.value or "", + actor=actor, + message=f"Backfill {block.label} block", + ) + logger.info(f"Backfilled {len(missing_blocks)} missing blocks for agent {agent_id}") + else: + logger.info(f"Git memory already enabled for agent {agent_id}") return except FileNotFoundError: logger.warning(