Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

refactor(core): restore filetree rendering for external memory and skills (#10238)

Sarah Wooders064036c56cb7
diff --git a/letta/prompts/prompt_generator.py b/letta/prompts/prompt_generator.py
index 817b3db80..b305ccb9b 100644
--- a/letta/prompts/prompt_generator.py
+++ b/letta/prompts/prompt_generator.py
@@ -71,7 +71,7 @@ class PromptGenerator:
f"- AGENT_ID: {agent_id}",
f"- CONVERSATION_ID: {conversation_id}",
f"- System prompt last recompiled: {timestamp_str}",
- f"- {previous_message_count} previous messages between you and the user are stored in recall memory (use tools to access them)",
+ f"- {previous_message_count} previous messages between you and the user are stored in recall memory",
]
# Only include archival memory line if there are archival memories
diff --git a/letta/schemas/memory.py b/letta/schemas/memory.py
index 63937f457..4c04f3ad0 100644
--- a/letta/schemas/memory.py
+++ b/letta/schemas/memory.py
@@ -1,5 +1,6 @@
import asyncio
import logging
+import os
from datetime import datetime
from io import StringIO
from typing import List, Optional, Union
@@ -209,8 +210,7 @@ class Memory(BaseModel, validate_assignment=True):
derived from their slash-separated labels (dropping the `system/`
prefix).
- Files outside `system/` and `skills/` are rendered under
- `<memory><external-memory>...</external-memory></memory>` as metadata-only
- projections.
+ `<memory><external>...</external></memory>` as a file tree.
"""
renderable = self._get_renderable_blocks()
if not renderable:
@@ -283,12 +283,18 @@ class Memory(BaseModel, validate_assignment=True):
system_tree = _build_tree(non_persona, strip_prefix="system/")
- def _render_nested(node: dict, indent: int = 0):
+ def _render_nested(node: dict, indent: int = 0, path_parts: list[str] | None = None):
pad = " " * indent
+ curr_parts = path_parts or []
for key in sorted(k for k in node.keys() if k not in (LEAF_KEY, LEAF_DESC_KEY, LEAF_LABEL_KEY)):
child = node[key]
+ child_parts = [*curr_parts, key]
s.write(f"{pad}<{key}>\n")
if isinstance(child, dict):
+ if LEAF_KEY in child:
+ projection_path = "/".join(child_parts)
+ s.write(f"{pad} <projection>$MEMORY_DIR/system/{projection_path}.md</projection>\n")
+
desc = str(child.get(LEAF_DESC_KEY) or "").rstrip("\n")
if desc:
s.write(f"{pad} <description>{desc}</description>\n")
@@ -296,27 +302,49 @@ class Memory(BaseModel, validate_assignment=True):
value = str(child[LEAF_KEY] or "").rstrip("\n")
if value:
s.write(f"{pad} {value}\n")
- _render_nested(child, indent + 1)
+ _render_nested(child, indent + 1, child_parts)
s.write(f"{pad}</{key}>\n")
s.write("\n\n<memory>\n")
_render_nested(system_tree)
- # 3) External memory projections (all files outside system/ and skills/)
+ # 3) External memory file tree (all files outside system/ and skills/)
if external_blocks:
- s.write("<external-memory>\n")
- s.write("Reminder: external memory must be actively retrieved into context on-demand. Only descriptions are shown below.\n")
+ s.write("<external_projection>\n")
+
+ tree: dict = {}
for block in sorted(external_blocks, key=lambda b: b.label or ""):
label = (block.label or "").strip()
if not label:
continue
- s.write(f"<{label}>\n")
- desc = (block.description or "").strip()
- if desc:
- s.write(f"{desc.splitlines()[0].strip()}\n")
- s.write(f"<projection>$MEMORY_DIR/{label}.md</projection>\n")
- s.write(f"</{label}>\n")
- s.write("</external-memory>\n")
+
+ parts = [p for p in label.split("/") if p]
+ if not parts:
+ continue
+
+ node = tree
+ for part in parts[:-1]:
+ node = node.setdefault(part, {})
+ node[f"{parts[-1]}.md"] = None
+
+ def _render_tree(node: dict, prefix: str = ""):
+ dirs = sorted(k for k, v in node.items() if isinstance(v, dict))
+ files = sorted(k for k, v in node.items() if v is None)
+ entries = [(d, True) for d in dirs] + [(f, False) for f in files]
+
+ for i, (name, is_dir) in enumerate(entries):
+ is_last = i == len(entries) - 1
+ connector = "└── " if is_last else "├── "
+ if is_dir:
+ s.write(f"{prefix}{connector}{name}/\n")
+ extension = " " if is_last else "│ "
+ _render_tree(node[name], prefix + extension)
+ else:
+ s.write(f"{prefix}{connector}{name}\n")
+
+ s.write("${MEMORY_DIR}/\n")
+ _render_tree(tree)
+ s.write("</external_projection>\n")
s.write("</memory>")
@@ -491,23 +519,69 @@ class Memory(BaseModel, validate_assignment=True):
seen_skill_names.add(name)
desc = (cs.description or "").strip().split("\n")[0].strip()
- location = cs.location or ""
+ location = (cs.location or "").strip() or f"${{MEMORY_DIR}}/skills/{name}/SKILL.md"
all_skill_entries.append((name, desc, location))
if not all_skill_entries:
return ""
+ def _skill_root(skill_name: str, location: str) -> tuple[str, str]:
+ norm = location.strip()
+ if norm.endswith("/SKILL.md"):
+ skill_dir = os.path.dirname(norm)
+ root = os.path.dirname(skill_dir)
+ rel = os.path.relpath(norm, root)
+ if os.path.basename(skill_dir) == skill_name.split("/")[-1]:
+ return root, rel
+ root = os.path.dirname(norm)
+ rel = os.path.basename(norm)
+ return root, rel
+
+ grouped: dict[str, list[tuple[str, str]]] = {}
+ for name, desc, location in all_skill_entries:
+ root, relative_path = _skill_root(name, location)
+ grouped.setdefault(root, []).append((relative_path, desc))
+
s = StringIO()
- all_skill_entries.sort(key=lambda e: e[0])
s.write("\n\n<available_skills>\n")
- for name, desc, location in all_skill_entries:
- s.write("<skill>\n")
- s.write(f"<name>{name}</name>\n")
- if desc:
- s.write(f"<description>{desc}</description>\n")
- if location:
- s.write(f"<location>{location}</location>\n")
- s.write("</skill>\n")
+
+ root_paths = sorted(grouped.keys())
+ for root_index, root in enumerate(root_paths):
+ s.write(f"{root}\n")
+
+ # Build a tree for each top-level location root.
+ tree: dict = {}
+ for rel_path, desc in sorted(grouped[root], key=lambda e: e[0]):
+ parts = [p for p in rel_path.split("/") if p]
+ if not parts:
+ continue
+
+ node = tree
+ for part in parts[:-1]:
+ node = node.setdefault(part, {})
+ node[parts[-1]] = desc
+
+ def _render_tree(node: dict, prefix: str = ""):
+ dirs = sorted(k for k, v in node.items() if isinstance(v, dict))
+ files = sorted(k for k, v in node.items() if isinstance(v, str))
+ entries = [(d, True) for d in dirs] + [(f, False) for f in files]
+
+ for i, (name, is_dir) in enumerate(entries):
+ is_last = i == len(entries) - 1
+ connector = "└── " if is_last else "├── "
+ if is_dir:
+ s.write(f"{prefix}{connector}{name}/\n")
+ extension = " " if is_last else "│ "
+ _render_tree(node[name], prefix + extension)
+ else:
+ desc = (node[name] or "").strip()
+ desc_suffix = f" ({desc})" if desc else ""
+ s.write(f"{prefix}{connector}{name}{desc_suffix}\n")
+
+ _render_tree(tree)
+ if root_index != len(root_paths) - 1:
+ s.write("\n")
+
s.write("</available_skills>")
return s.getvalue()
diff --git a/tests/test_letta_agent_v2_skills.py b/tests/test_letta_agent_v2_skills.py
index 295d7ec07..e12752cfc 100644
--- a/tests/test_letta_agent_v2_skills.py
+++ b/tests/test_letta_agent_v2_skills.py
@@ -59,7 +59,7 @@ def _build_memory_with_literal_tag_refs() -> Memory:
def _assert_one_structural_skills_block_at_tail(text: str) -> None:
- system_block_ends = [m.end() for m in re.finditer(r"</system/[^>]+>", text)]
+ system_block_ends = [m.end() for m in re.finditer(r"</memory>", text)]
assert system_block_ends, "Expected system blocks in test prompt"
tail = text[max(system_block_ends) :]
assert tail.count("<available_skills>") == 1
@@ -81,7 +81,8 @@ def test_generate_request_system_prompt_appends_skills_and_preserves_literals():
_assert_one_structural_skills_block_at_tail(new_text)
assert "`<available_skills>`" in new_text
- assert "<name>fresh-skill</name>" in new_text
+ assert "/tmp/fresh" in new_text
+ assert "SKILL.md (fresh)" in new_text
assert new_text.rstrip().endswith(new_skills.rstrip())
@@ -143,7 +144,8 @@ def test_generate_request_system_prompt_includes_request_scoped_client_skills_wi
_assert_one_structural_skills_block_at_tail(request_system_text)
assert "`<available_skills>`" in request_system_text
- assert "<name>fresh-skill</name>" in request_system_text
+ assert "/tmp/fresh" in request_system_text
+ assert "SKILL.md (fresh)" in request_system_text
# Ensure original persisted message object is unchanged (request-scoped only)
assert system_message.content[0].text == old_text
@@ -175,7 +177,7 @@ def test_generate_request_system_prompt_is_stable_for_same_stored_system_prompt(
assert request_system_text is not None
_assert_one_structural_skills_block_at_tail(request_system_text)
- assert request_system_text.count("<name>fresh-skill</name>") == 1
+ assert request_system_text.count("SKILL.md (fresh)") == 1
@pytest.mark.asyncio
diff --git a/tests/test_memory.py b/tests/test_memory.py
index 52fe0705e..f918c4765 100644
--- a/tests/test_memory.py
+++ b/tests/test_memory.py
@@ -244,11 +244,7 @@ def test_compile_git_structured_core_memory_rendering():
def test_compile_git_structured_skills_section():
- """skills/ blocks should render in compile_available_skills() as metadata-only entries.
-
- Nested skill files should NOT appear. Only top-level skill entries
- with name, description, and location.
- """
+ """skills/ blocks should render in compile_available_skills() as filetree entries."""
m = Memory(
agent_type=AgentType.letta_v1_agent,
@@ -281,11 +277,12 @@ def test_compile_git_structured_skills_section():
assert "<available_skills>" in out
assert "</available_skills>" in out
- # Top-level skill entries with descriptions
- assert "<name>searching-messages</name>" in out
- assert "<description>Search past messages to recall context.</description>" in out
- assert "<name>creating-skills</name>" in out
- assert "<description>Guide for creating effective skills.</description>" in out
+ # Top-level location and skill tree entries with descriptions
+ assert "${MEMORY_DIR}/skills" in out
+ assert "searching-messages/" in out
+ assert "SKILL.md (Search past messages to recall context.)" in out
+ assert "creating-skills/" in out
+ assert "SKILL.md (Guide for creating effective skills.)" in out
# Skill content should NOT be rendered (metadata only)
assert "# searching messages" not in out
@@ -297,7 +294,7 @@ def test_compile_git_structured_skills_section():
def test_compile_git_structured_archival_memory_section():
- """Non-system, non-skills blocks should render in <external-memory> as projections."""
+ """Non-system, non-skills blocks should render in <external_projection> as a file tree."""
m = Memory(
agent_type=AgentType.letta_v1_agent,
@@ -312,13 +309,18 @@ def test_compile_git_structured_archival_memory_section():
out = m.compile()
assert "<memory>" in out
- assert "<external-memory>" in out
- assert "<notes>" in out
- assert "<reference/api>" in out
- assert "<projection>$MEMORY_DIR/notes.md</projection>" in out
- assert "<projection>$MEMORY_DIR/reference/api.md</projection>" in out
- assert "Personal notes" in out
- assert "API docs" in out
+ assert "<projection>$MEMORY_DIR/system/human.md</projection>" in out
+ assert "<external_projection>" in out
+ assert "${MEMORY_DIR}/" in out
+ assert "reference/" in out
+ assert "api.md" in out
+ assert "notes.md" in out
+ assert "${MEMORY_DIR}/reference/" not in out
+ assert "${MEMORY_DIR}/notes.md" not in out
+
+ # Descriptions are not rendered in the external tree.
+ assert "Personal notes" not in out
+ assert "API docs" not in out
# Archival memory content should NOT be rendered
assert "my notes" not in out
@@ -326,7 +328,7 @@ def test_compile_git_structured_archival_memory_section():
def test_compile_git_structured_client_skills():
- """client_skills should be merged with agent skills in compile_available_skills()."""
+ """client_skills should be merged with agent skills in compile_available_skills() as trees grouped by root."""
from letta.schemas.letta_request import ClientSkillSchema
m = Memory(
@@ -354,15 +356,17 @@ def test_compile_git_structured_client_skills():
assert "<available_skills>" in out
assert "</available_skills>" in out
- # Agent skill present
- assert "<name>searching-messages</name>" in out
+ # Agent skill present under memory skills root
+ assert "${MEMORY_DIR}/skills" in out
+ assert "searching-messages/" in out
+ assert "SKILL.md (Search past messages.)" in out
- # Client skills present with descriptions and locations
- assert "<name>playwright-skill</name>" in out
- assert "<description>Browser automation.</description>" in out
- assert "<location>skills/playwright-skill/SKILL.md</location>" in out
- assert "<name>google</name>" in out
- assert "<description>Google Workspace CLI.</description>" in out
+ # Client skills grouped under their location roots
+ assert "skills" in out
+ assert "playwright-skill/" in out
+ assert "SKILL.md (Browser automation.)" in out
+ assert "google/" in out
+ assert "SKILL.md (Google Workspace CLI.)" in out
def test_compile_git_structured_dedup_client_skills():
@@ -386,15 +390,16 @@ def test_compile_git_structured_dedup_client_skills():
# Agent skill present, client duplicate deduplicated
assert "<available_skills>" in out
- assert "<name>my-skill</name>" in out
+ assert "my-skill/" in out
+ assert "SKILL.md" in out
# Agent skill wins — description comes from agent block, not client
- assert "<description>Agent skill.</description>" in out
+ assert "(Agent skill.)" in out
# Only one entry for the skill (deduplicated)
- assert out.count("<name>my-skill</name>") == 1
+ assert out.count("my-skill/") == 1
def test_compile_git_structured_client_skills_omits_empty_description_location_tags():
- """Client skills with blank description/location should still render name but omit empty XML tags."""
+ """Client skills with blank description/location should still render name without empty suffix."""
from letta.schemas.letta_request import ClientSkillSchema
m = Memory(
@@ -412,13 +417,13 @@ def test_compile_git_structured_client_skills_omits_empty_description_location_t
out = m.compile_available_skills(client_skills=client_skills)
assert "<available_skills>" in out
- assert "<name>blanky</name>" in out
- assert "<description></description>" not in out
- assert "<location></location>" not in out
+ assert "blanky/" in out
+ assert "SKILL.md" in out
+ assert "SKILL.md ()" not in out
def test_compile_available_skills_standalone():
- """compile_available_skills() returns the skills block without recompiling the full prompt."""
+ """compile_available_skills() returns grouped file trees without recompiling the full prompt."""
from letta.schemas.letta_request import ClientSkillSchema
m = Memory(
@@ -439,12 +444,12 @@ def test_compile_available_skills_standalone():
assert "<available_skills>" in result
assert "</available_skills>" in result
# Agent skill present
- assert "<name>my-skill</name>" in result
- assert "<description>Agent skill.</description>" in result
+ assert "my-skill/" in result
+ assert "SKILL.md (Agent skill.)" in result
# Client skill present
- assert "<name>client-only</name>" in result
- assert "<description>From client.</description>" in result
- assert "<location>/tmp/client-only/SKILL.md</location>" in result
+ assert "/tmp" in result
+ assert "client-only/" in result
+ assert "SKILL.md (From client.)" in result
# No skills → empty string
m_empty = Memory(
@@ -538,7 +543,8 @@ def test_append_available_skills_preserves_blocks_with_literal_skills_text():
structural_end = new_text.find("</available_skills>", structural_start)
assert structural_start > 0
assert structural_end > structural_start
- assert "<name>test-skill</name>" in new_text[structural_start:structural_end]
+ assert "test-skill/" in new_text[structural_start:structural_end]
+ assert "SKILL.md (A test skill.)" in new_text[structural_start:structural_end]
def test_compile_git_structured_recompile_after_block_edit():