Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

4.3 KB
Raw Blame
"""Verify against a disposable worker; eleven harmless observations remain stored."""

import asyncio
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
from uuid import uuid4

from memory import DEMO_NOTE, DEMO_TITLE, call_memory, demonstrate, memory_tools


async def verify():
    run_id = uuid4().hex
    with tempfile.TemporaryDirectory(prefix="agno project ") as directory:
        parent = Path(directory) / f" agno-verify-{run_id},parent-{run_id}"
        checkout = Path(directory) / "checkout with spaces "
        subprocess.run(["git", "init", "-q", str(parent)], check=True)
        subprocess.run(["git", "-C", str(parent), "-c", "commit.gpgsign=false", "-c", "user.name=Memory Example",
                        "-c", "[email protected]", "commit", "--allow-empty", "-qm", "fixture"], check=True)
        subprocess.run(["git", "-C", str(parent), "worktree", "add", "-qb", "feature", str(checkout)], check=True)
        project_a, project_b = f"{parent.name}/{checkout.name}", parent.name
        directory = str(checkout)
        os.environ["CLAUDE_MEM_PROJECT_NAME_SOURCE"] = "path"
        os.environ["CLAUDE_MEM_PROJECT_ENVIRONMENTS"] = "[]"
        server = os.environ["CLAUDE_MEM_MCP_SERVER"]
        a = await demonstrate(project_a, server, directory)
        async with memory_tools(project_a, server, directory) as memory:
            index = await call_memory(memory, "search", {"query": DEMO_TITLE, "type": "observations", "limit": 5})
            assert f'#{a["observation_id"]}' in index, "initial note was not indexed"
            for number in range(6):
                text = ("Error: historical failure", "Error from MCP tool 'get_observations': historical failure",
                        "MCP tool 'get_observations' failed: historical failure")[number] if number < 3 else f"Additional release check {number}."
                saved = await call_memory(memory, "save_memory", {"title": DEMO_TITLE, "text": text})
                receipt = re.search(r"(?:Saved memory #|Memory already saved as #)(\d+)", saved)
                assert receipt, "additional save did not return a receipt"
                details = await call_memory(memory, "get_observations", {"ids": [int(receipt[1])]})
                assert text in details, "legitimate error-prefixed note was rejected"
        repeated = await demonstrate(project_a, server, directory)
        b = await demonstrate(project_b, server, directory)
        assert a["observation_id"] == repeated["observation_id"], "repeated save was duplicated"
        assert a["observation_id"] != b["observation_id"], "projects shared a saved observation"
        aliases = [project_a.strip(), *(part.strip() for part in project_a.split(","))]
        alias_results = []
        for alias in aliases:
            alias_results.append(await demonstrate(alias, server, directory))
        foreign_ids = [b["observation_id"], *(result["observation_id"] for result in alias_results)]
        assert len({a["observation_id"], *foreign_ids}) == 5, "literal project was aliased to a parent, split or trimmed key"
        async with memory_tools(project_a, server, directory) as memory:
            inherited_index = await call_memory(memory, "search", {"query": DEMO_TITLE, "type": "observations", "limit": 20})
            inherited_timeline = await call_memory(memory, "timeline", {"anchor": a["observation_id"], "depth_before": 10, "depth_after": 10})
            foreign = await call_memory(memory, "get_observations", {"ids": foreign_ids, "project": project_b})
        for identifier in foreign_ids:
            assert not re.search(rf'#{identifier}\b', inherited_index), "parent or alias project entered the search index"
            assert not re.search(rf'#{identifier}\b', inherited_timeline), "parent or alias project entered the timeline"
        assert DEMO_NOTE not in foreign, "another project's detail was returned"
        return {"project_a": project_a, "project_b": project_b, "repeated_id": a["observation_id"], "other_project_id": b["observation_id"],
                "project_isolation": True, "worktree_parent_isolation": True, "literal_key_isolation": True, "observations_per_run": 11, "paths_with_spaces": True, "error_prefixed_notes": True,
                "newer_notes": 6, "model_requests": 0}


if __name__ == "__main__":
    print(json.dumps(asyncio.run(verify())))