Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(memory): resolve partial IDs beyond the first 1000 records (#4389)

Ehsan-hashc4ea31b15647
diff --git a/headroom/cli/memory.py b/headroom/cli/memory.py
index e981f13ce..bdefc7637 100644
--- a/headroom/cli/memory.py
+++ b/headroom/cli/memory.py
@@ -598,7 +598,7 @@ def show_memory(
# If not found with exact ID, try partial match
if mem is None:
# Query all and filter by prefix
- all_memories = asyncio.run(store.query(MemoryFilter(limit=1000)))
+ all_memories = asyncio.run(store.query(MemoryFilter()))
matches = [m for m in all_memories if m.id.startswith(memory_id)]
if len(matches) == 0:
diff --git a/tests/cli/test_memory.py b/tests/cli/test_memory.py
index 6a9acb65a..f4b5d9359 100644
--- a/tests/cli/test_memory.py
+++ b/tests/cli/test_memory.py
@@ -165,6 +165,79 @@ class TestMemoryShow:
assert result.exit_code == 0
assert "TypeScript" in result.output
+ def test_show_by_partial_id_beyond_default_query_limit(
+ self, runner: CliRunner, temp_db: str
+ ) -> None:
+ """Show a unique partial ID when the match is older than 1,000 rows."""
+ store = SQLiteMemoryStore(temp_db)
+ filler_start = datetime(2024, 1, 1)
+ memories = [
+ Memory(
+ id=f"filler-{index:04d}",
+ content=f"Filler memory {index}",
+ user_id="test-user",
+ created_at=filler_start + timedelta(days=index),
+ valid_from=filler_start + timedelta(days=index),
+ )
+ for index in range(1000)
+ ]
+ memories.append(
+ Memory(
+ id="legacy-target-memory",
+ content="Legacy target outside the first page",
+ user_id="test-user",
+ created_at=datetime(2020, 1, 1),
+ valid_from=datetime(2020, 1, 1),
+ )
+ )
+ asyncio.run(store.save_batch(memories))
+
+ result = runner.invoke(main, ["memory", "show", "--db-path", temp_db, "legacy-target-"])
+
+ assert result.exit_code == 0
+ assert "Legacy target outside the first page" in result.output
+
+ def test_show_by_partial_id_reports_ambiguity_beyond_default_query_limit(
+ self, runner: CliRunner, temp_db: str
+ ) -> None:
+ """Report ambiguity when matching IDs span beyond the first 1,000 rows."""
+ store = SQLiteMemoryStore(temp_db)
+ filler_start = datetime(2024, 1, 1)
+ memories = [
+ Memory(
+ id=f"filler-{index:04d}",
+ content=f"Filler memory {index}",
+ user_id="test-user",
+ created_at=filler_start + timedelta(days=index),
+ valid_from=filler_start + timedelta(days=index),
+ )
+ for index in range(1000)
+ ]
+ memories.extend(
+ [
+ Memory(
+ id="shared-prefix-old",
+ content="Older matching memory",
+ user_id="test-user",
+ created_at=datetime(2020, 1, 1),
+ valid_from=datetime(2020, 1, 1),
+ ),
+ Memory(
+ id="shared-prefix-new",
+ content="Newer matching memory",
+ user_id="test-user",
+ created_at=datetime(2026, 1, 1),
+ valid_from=datetime(2026, 1, 1),
+ ),
+ ]
+ )
+ asyncio.run(store.save_batch(memories))
+
+ result = runner.invoke(main, ["memory", "show", "--db-path", temp_db, "shared-prefix-"])
+
+ assert result.exit_code != 0
+ assert "Ambiguous ID 'shared-prefix-'" in result.output
+
def test_show_json_output(self, runner: CliRunner, populated_db: str) -> None:
"""Show memory as JSON."""
result = runner.invoke(