Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
Revert "fix(core): canonicalize available_skills at end of system prompt" (#10022)
Sarah Wooders7d43854d1bcb
diff --git a/letta/agents/letta_agent_v2.py b/letta/agents/letta_agent_v2.pyindex 9f52de1b6..7dd1ff502 100644--- a/letta/agents/letta_agent_v2.py+++ b/letta/agents/letta_agent_v2.py@@ -793,6 +793,8 @@ class LettaAgentV2(BaseAgentV2):This avoids a full system prompt rebuild (which would change timestamps andbust prefix caching) by only replacing the skills portion of the prompt."""+ import re+if not in_context_messages:return in_context_messages@@ -802,11 +804,36 @@ class LettaAgentV2(BaseAgentV2):# Compile the new skills section from agent blocks + client skillsnew_skills = self.agent_state.memory.compile_available_skills(client_skills=self.client_skills)- if not new_skills:+ # Replace the STRUCTURAL <available_skills>...</available_skills> block.+ #+ # Memory block content may contain literal <available_skills> and+ # </available_skills> text references (e.g. docs about the skills system).+ # A regex approach cannot reliably distinguish structural tags from text+ # references because non-greedy matches absorb the structural closing tag.+ #+ # Instead we locate the structural block by position: it is always rendered+ # AFTER the last </system/...> memory block closing tag. We find the+ # <available_skills> that starts after that boundary using plain string search.+ block_close_pattern = re.compile(r"</system/[^>]+>")+ block_closes = list(block_close_pattern.finditer(old_text))+ search_from = block_closes[-1].end() if block_closes else 0++ skills_open = old_text.find("<available_skills>", search_from)+ skills_close = old_text.find("</available_skills>", skills_open) if skills_open >= 0 else -1++ if skills_open >= 0 and skills_close >= 0:+ # Consume leading newlines before the tag+ trim_start = skills_open+ while trim_start > 0 and old_text[trim_start - 1] == "\n":+ trim_start -= 1+ new_text = old_text[:trim_start] + new_skills + old_text[skills_close + len("</available_skills>") :]+ elif new_skills:+ # No existing skills section — insert right before <memory_metadata>+ if "<memory_metadata>" in old_text:+ new_text = old_text.replace("<memory_metadata>", new_skills.lstrip("\n") + "\n\n<memory_metadata>")+ else:return in_context_messages- new_text = old_text.rstrip("\n") + "\n\n" + new_skills.lstrip("\n")-if new_text == old_text:return in_context_messagesdiff --git a/tests/test_memory.py b/tests/test_memory.pyindex 2af7320bf..e4f5fb6b5 100644--- a/tests/test_memory.py+++ b/tests/test_memory.py@@ -597,55 +597,6 @@ def test_surgical_skills_update_preserves_blocks_with_literal_skills_text():assert "<name>test-skill</name>" in new_text[structural_start:structural_end]-def test_append_available_skills_to_system_prompt_appends_to_end():- """Rendered skills are appended to the end of the existing system prompt text."""- from letta.schemas.letta_request import ClientSkillSchema-- m = Memory(- agent_type=AgentType.letta_v1_agent,- git_enabled=True,- blocks=[- Block(label="system/human", value="human data", limit=500),- Block(- label="system/project/architecture",- value="Docs mention the `<memory_metadata>` section for context bookkeeping.",- limit=1000,- description="Architecture notes.",- ),- ],- )-- old_text = m.compile() + "\n\n<memory_metadata>\nAGENT_ID: test\n</memory_metadata>"- assert "<available_skills>" not in old_text-- client_skills = [- ClientSkillSchema(name="insert-test-skill", description="A test skill.", location="skills/insert-test/SKILL.md"),- ]- new_skills = m.compile_available_skills(client_skills=client_skills)-- new_text = old_text.rstrip("\n") + "\n\n" + new_skills.lstrip("\n")- assert new_text.count("<available_skills>") == 1- assert new_text.count("</available_skills>") == 1- assert new_text.count("<memory_metadata>") == old_text.count("<memory_metadata>")- assert new_text.rstrip().endswith("</available_skills>")---def test_append_available_skills_to_system_prompt_preserves_literal_references():- """Literal tag references in block content are preserved by append-only behavior."""- old_text = (- "<system/project/architecture.md>\n"- "`<available_skills>` and `<memory_metadata>` are documented here.\n"- "</system/project/architecture.md>\n\n"- "<memory_metadata>\nAGENT_ID: test\n</memory_metadata>"- )- new_skills = "\n\n<available_skills>\n<skill><name>x</name></skill>\n</available_skills>"-- new_text = old_text.rstrip("\n") + "\n\n" + new_skills.lstrip("\n")- assert "`<available_skills>`" in new_text- assert "`<memory_metadata>`" in new_text- assert new_text.rstrip().endswith("</available_skills>")--def test_compile_git_structured_recompile_after_block_edit():"""Editing a block value should produce a different compile() output.