Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/backend/agent_graphiti_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import asyncio
import os
from pathlib import Path
from datetime import datetime, timezone
from integrations.graphiti.memory import GraphitiMemory

spec_dir = Path.home() / ".auto-claude" / "agent_style_spec"
spec_dir.mkdir(parents=True, exist_ok=True)
project_dir_env = os.getenv("PROJECT_DIR")
# Prefer a portable PROJECT_DIR override; otherwise fall back to the repository root
# (…/apps/backend/agent_graphiti_test.py -> parents[2] == repo root).
project_dir = (
Path(project_dir_env).expanduser().resolve()
if project_dir_env
else Path(__file__).resolve().parents[2]
)
project_dir.mkdir(parents=True, exist_ok=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Avoid creating project_dir if it doesn't exist.

Using project_dir.mkdir(parents=True, exist_ok=True) on the project directory could inadvertently create unexpected directory structures if the path is misconfigured. Since project_dir should represent an existing project root (either from PROJECT_DIR or the repository root), it should already exist.

Consider removing this line or replacing it with a check that raises an error if the directory doesn't exist:

🔎 Proposed fix
-project_dir.mkdir(parents=True, exist_ok=True)
+if not project_dir.exists():
+    raise FileNotFoundError(f"Project directory does not exist: {project_dir}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
project_dir.mkdir(parents=True, exist_ok=True)
if not project_dir.exists():
raise FileNotFoundError(f"Project directory does not exist: {project_dir}")
🤖 Prompt for AI Agents
In apps/backend/agent_graphiti_test.py around line 17, remove the call that
creates the project_dir (project_dir.mkdir(parents=True, exist_ok=True)) and
instead assert or raise if the directory does not exist; replace it with a
simple existence check (e.g., if not project_dir.exists(): raise
FileNotFoundError(...) or an AssertionError) so the test fails fast when
PROJECT_DIR is misconfigured rather than silently creating unexpected
directories.


msg = f"agent-style write {datetime.now(timezone.utc).isoformat()}"

async def main():
mem = GraphitiMemory(spec_dir=spec_dir, project_dir=project_dir)
print("is_enabled:", getattr(mem, "is_enabled", None))

# Write (Agent-style) – with a timeout so it doesn't hang forever
ok = await asyncio.wait_for(
mem.save_session_insights(
session_num=int(datetime.now(timezone.utc).timestamp()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Potential session_num collision with timestamp-based ID.

Using int(datetime.now(timezone.utc).timestamp()) as session_num could cause collisions if this script runs multiple times within the same second, or if other code uses the same timestamp-based ID scheme.

Consider using a UUID or adding millisecond precision.

🔎 Proposed fix
-        session_num=int(datetime.now(timezone.utc).timestamp()),
+        session_num=int(datetime.now(timezone.utc).timestamp() * 1000),  # milliseconds
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
session_num=int(datetime.now(timezone.utc).timestamp()),
session_num=int(datetime.now(timezone.utc).timestamp() * 1000), # milliseconds
🤖 Prompt for AI Agents
In apps/backend/agent_graphiti_test.py around line 28, session_num is set to
int(datetime.now(timezone.utc).timestamp()) which can collide for runs within
the same second; replace it with a collision-safe ID such as a UUID (e.g., use
uuid.uuid4().hex) or use millisecond precision (e.g.,
int(datetime.now(timezone.utc).timestamp() * 1000)), and add the corresponding
import (uuid or adjust datetime usage) so session_num is unique across rapid
executions.

insights={"type":"agent_style_test","message":msg,"timestamp":datetime.now(timezone.utc).isoformat()},
),
timeout=120,
)
print("write_ok:", ok)

# Read (Agent-style)
hits = await asyncio.wait_for(mem.get_relevant_context(query=msg, num_results=20), timeout=60)
print("hits:", len(hits) if hits else 0)
if hits:
print("first_hit:", hits[0])

await mem.close()
print("spec_dir_used:", spec_dir)

asyncio.run(main())
8 changes: 6 additions & 2 deletions apps/backend/agents/memory_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ async def get_graphiti_context(
return None

try:
from graphiti_memory import GraphitiMemory
# Prefer the stable in-package import. Some deployments (e.g. app bundles)
# don't ship a top-level `graphiti_memory` shim.
from integrations.graphiti.memory import GraphitiMemory

# Create memory manager
memory = GraphitiMemory(spec_dir, project_dir)
Expand Down Expand Up @@ -286,7 +288,9 @@ async def save_session_memory(
debug("memory", "Attempting PRIMARY storage: Graphiti")

try:
from graphiti_memory import GraphitiMemory
# Prefer the stable in-package import. Some deployments (e.g. app bundles)
# don't ship a top-level `graphiti_memory` shim.
from integrations.graphiti.memory import GraphitiMemory

memory = GraphitiMemory(spec_dir, project_dir)

Expand Down
Loading
Loading