Files
letta-server/tests/test_context_window_calculator.py
Sarah Wooders 2ffef0fb31 Fix git-memory context preview parsing (#9414)
* fix(core): handle git memory label prefix collisions in filesystem view

Prevent context window preview crashes when a block label is both a leaf and a prefix (e.g. system/human and system/human/context) by rendering a node as both file and directory. Add regression test.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <noreply@letta.com>

* fix(core): parse git-backed core memory in context window preview

ContextWindowCalculator.extract_system_components now detects git-backed memory rendering (<memory_filesystem> and <system/...> tags) when <memory_blocks> wrapper is absent, so core_memory is populated in the context preview. Add regression tests.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <noreply@letta.com>

---------

Co-authored-by: Letta <noreply@letta.com>
2026-02-24 10:52:07 -08:00

55 lines
1.4 KiB
Python

import pytest
from letta.services.context_window_calculator.context_window_calculator import ContextWindowCalculator
def test_extract_system_components_git_backed_memory_without_memory_blocks_wrapper():
system_message = """You are some system prompt.
<memory_filesystem>
Memory Directory: ~/.letta/agents/agent-123/memory
/memory/
└── system/
└── human.md
</memory_filesystem>
<system/human.md>
---
description: test
limit: 10
---
hello
</system/human.md>
<memory_metadata>
- foo=bar
</memory_metadata>
"""
system_prompt, core_memory, external_memory_summary = ContextWindowCalculator.extract_system_components(system_message)
assert "You are some system prompt" in system_prompt
assert "<memory_filesystem>" in core_memory
assert "<system/human.md>" in core_memory
assert external_memory_summary.startswith("<memory_metadata>")
def test_extract_system_components_legacy_memory_blocks_wrapper():
system_message = """<base_instructions>SYS</base_instructions>
<memory_blocks>
<persona>p</persona>
</memory_blocks>
<memory_metadata>
- x=y
</memory_metadata>
"""
system_prompt, core_memory, external_memory_summary = ContextWindowCalculator.extract_system_components(system_message)
assert system_prompt.startswith("<base_instructions>")
assert core_memory.startswith("<memory_blocks>")
assert external_memory_summary.startswith("<memory_metadata>")