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.

fix(core): defer git post-push sync until stream close (#9951)

Sarah Woodersb1c763b5c275
diff --git a/letta/server/rest_api/routers/v1/git_http.py b/letta/server/rest_api/routers/v1/git_http.py
index 793648875..9668cb49e 100644
--- a/letta/server/rest_api/routers/v1/git_http.py
+++ b/letta/server/rest_api/routers/v1/git_http.py
@@ -33,8 +33,6 @@ from letta.services.memory_repo.path_mapping import memory_block_label_from_mark
logger = get_logger(__name__)
-_background_tasks: set[asyncio.Task] = set()
-
def _is_syncable_block_markdown_path(path: str) -> bool:
"""Return whether a markdown path should be mirrored into block cache.
@@ -271,6 +269,7 @@ async def proxy_git_http(
# Resolve org_id from the authenticated actor + agent and forward to memfs.
agent_id = _parse_agent_id_from_repo_path(path)
+ sync_after_push_context: tuple[str, str] | None = None
if agent_id is not None:
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
# Authorization check: ensure the actor can access this agent.
@@ -283,6 +282,11 @@ async def proxy_git_http(
# Use the authenticated actor's org; AgentState may not carry an organization field.
req_headers["X-Organization-Id"] = actor.organization_id
+ # Defer post-push sync until after the upstream response stream has fully
+ # completed, so memfs has finished persisting refs/objects.
+ if request.method == "POST" and path.endswith("git-receive-pack"):
+ sync_after_push_context = (actor.id, agent_id)
+
logger.info(
"proxy_git_http: method=%s path=%s parsed_agent_id=%s actor_id=%s has_user_id_hdr=%s x_org_hdr=%s",
request.method,
@@ -309,26 +313,19 @@ async def proxy_git_http(
resp_headers = _filter_out_hop_by_hop_headers(upstream.headers.items())
- # If this was a push, trigger our sync.
- if request.method == "POST" and path.endswith("git-receive-pack") and upstream.status_code < 400:
- agent_id = _parse_agent_id_from_repo_path(path)
- if agent_id is not None:
- try:
- actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
- # Authorization check: ensure the actor can access this agent.
- await server.agent_manager.get_agent_by_id_async(agent_id=agent_id, actor=actor, include_relationships=[])
- task = asyncio.create_task(_sync_after_push(actor.id, agent_id))
- _background_tasks.add(task)
- task.add_done_callback(_background_tasks.discard)
- except Exception:
- logger.exception("Failed to trigger post-push sync (agent_id=%s)", agent_id)
-
async def _aclose_upstream_and_client() -> None:
try:
await upstream.aclose()
finally:
await client.aclose()
+ if sync_after_push_context is not None and upstream.status_code < 400:
+ actor_id, pushed_agent_id = sync_after_push_context
+ try:
+ await _sync_after_push(actor_id, pushed_agent_id)
+ except Exception:
+ logger.exception("Failed to trigger deferred post-push sync (agent_id=%s)", pushed_agent_id)
+
return StreamingResponse(
upstream.aiter_raw(),
status_code=upstream.status_code,
diff --git a/tests/integration_test_git_http_post_push_sync.py b/tests/integration_test_git_http_post_push_sync.py
new file mode 100644
index 000000000..944e28c70
--- /dev/null
+++ b/tests/integration_test_git_http_post_push_sync.py
@@ -0,0 +1,208 @@
+import os
+import subprocess
+import tempfile
+import time
+from pathlib import Path
+
+import httpx
+import pytest
+from letta_client import Letta
+
+
+def _run(cmd: list[str], cwd: str | None = None) -> None:
+ subprocess.run(cmd, cwd=cwd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+
+
+def _run_capture(cmd: list[str], cwd: str | None = None) -> str:
+ result = subprocess.run(cmd, cwd=cwd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ return result.stdout
+
+
+def _git_push_memory_update(server_url: str, agent_id: str, user_id: str, rel_markdown_path: str, markdown_content: str) -> None:
+ with tempfile.TemporaryDirectory(prefix="git-http-push-") as tmpdir:
+ repo_dir = Path(tmpdir) / "repo"
+ remote = f"{server_url}/v1/git/{agent_id}/state.git"
+
+ _run(["git", "clone", remote, str(repo_dir)])
+ _run(["git", "config", "user.name", "Test User"], cwd=str(repo_dir))
+ _run(["git", "config", "user.email", "[email protected]"], cwd=str(repo_dir))
+
+ target = repo_dir / rel_markdown_path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(markdown_content, encoding="utf-8")
+
+ _run(["git", "add", rel_markdown_path], cwd=str(repo_dir))
+ _run(["git", "commit", "-m", "Update memory via git HTTP"], cwd=str(repo_dir))
+
+ env = os.environ.copy()
+ existing = env.get("GIT_CONFIG_COUNT", "0")
+ idx = int(existing)
+ env["GIT_CONFIG_COUNT"] = str(idx + 2)
+ env[f"GIT_CONFIG_KEY_{idx}"] = "http.extraHeader"
+ env[f"GIT_CONFIG_VALUE_{idx}"] = f"user_id: {user_id}"
+ env[f"GIT_CONFIG_KEY_{idx + 1}"] = "http.extraHeader"
+ env[f"GIT_CONFIG_VALUE_{idx + 1}"] = "Accept: application/x-git-receive-pack-result"
+
+ subprocess.run(
+ ["git", "push", "origin", "HEAD"],
+ cwd=str(repo_dir),
+ check=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ env=env,
+ )
+
+
+def _poll_block_value(client: Letta, agent_id: str, block_label: str, expected_substring: str, timeout_seconds: float = 20.0) -> str:
+ deadline = time.time() + timeout_seconds
+ last_value = ""
+
+ while time.time() < deadline:
+ block = client.agents.blocks.retrieve(agent_id=agent_id, block_label=block_label)
+ last_value = block.value or ""
+ if expected_substring in last_value:
+ return last_value
+ time.sleep(0.25)
+
+ raise AssertionError(f"Timed out waiting for block '{block_label}' to contain '{expected_substring}'. Last value: {last_value!r}")
+
+
[email protected](not bool(os.getenv("LETTA_MEMFS_SERVICE_URL")), reason="requires memfs service configured")
+def test_git_http_push_updates_core_block_cache_end_to_end(server_url: str):
+ client = Letta(base_url=server_url)
+
+ agent = client.agents.create(
+ name="git-http-sync-e2e",
+ model="openai/gpt-4o-mini",
+ embedding="openai/text-embedding-3-small",
+ tags=["git-memory-enabled"],
+ memory_blocks=[
+ {
+ "label": "human",
+ "value": "initial",
+ "description": "human",
+ }
+ ],
+ )
+
+ try:
+ actor_id = "user-00000000-0000-4000-8000-000000000000"
+ marker = "GIT_PUSH_E2E_MARKER_123"
+ markdown = f"---\ndescription: human\n---\n{marker}\n"
+
+ _git_push_memory_update(
+ server_url=server_url,
+ agent_id=agent.id,
+ user_id=actor_id,
+ rel_markdown_path="system/human.md",
+ markdown_content=markdown,
+ )
+
+ updated = _poll_block_value(
+ client=client,
+ agent_id=agent.id,
+ block_label="system/human",
+ expected_substring=marker,
+ )
+ assert marker in updated
+ finally:
+ client.agents.delete(agent.id)
+
+
[email protected](not bool(os.getenv("LETTA_MEMFS_SERVICE_URL")), reason="requires memfs service configured")
+def test_git_http_failed_receive_pack_does_not_sync_blocks(server_url: str):
+ client = Letta(base_url=server_url)
+
+ agent = client.agents.create(
+ name="git-http-fail-no-sync",
+ model="openai/gpt-4o-mini",
+ embedding="openai/text-embedding-3-small",
+ tags=["git-memory-enabled"],
+ memory_blocks=[
+ {
+ "label": "human",
+ "value": "initial-value",
+ "description": "human",
+ }
+ ],
+ )
+
+ try:
+ actor_id = "user-00000000-0000-4000-8000-000000000000"
+ path = f"{agent.id}/state.git/git-receive-pack"
+
+ with httpx.Client(timeout=10.0) as http:
+ resp = http.post(
+ f"{server_url}/v1/git/{path}",
+ headers={
+ "user_id": actor_id,
+ "content-type": "application/x-git-receive-pack-request",
+ "accept": "application/x-git-receive-pack-result",
+ },
+ content=b"not-a-valid-receive-pack-payload",
+ )
+
+ assert resp.status_code >= 400
+
+ block = client.agents.blocks.retrieve(agent_id=agent.id, block_label="system/human")
+ assert block.value == "initial-value"
+ finally:
+ client.agents.delete(agent.id)
+
+
[email protected](not bool(os.getenv("LETTA_MEMFS_SERVICE_URL")), reason="requires memfs service configured")
+def test_git_http_post_push_sync_retries_transient_head_read(server_url: str, monkeypatch):
+ client = Letta(base_url=server_url)
+
+ agent = client.agents.create(
+ name="git-http-retry-e2e",
+ model="openai/gpt-4o-mini",
+ embedding="openai/text-embedding-3-small",
+ tags=["git-memory-enabled"],
+ memory_blocks=[
+ {
+ "label": "human",
+ "value": "initial",
+ "description": "human",
+ }
+ ],
+ )
+
+ try:
+ from letta.server.rest_api.app import server as app_server
+
+ actor_id = "user-00000000-0000-4000-8000-000000000000"
+ marker = "GIT_RETRY_E2E_MARKER_456"
+ markdown = f"---\ndescription: human\n---\n{marker}\n"
+
+ real_get_files = app_server.memory_repo_manager.git.get_files
+ attempts = {"count": 0}
+
+ async def flaky_get_files(agent_id: str, org_id: str, ref: str = "HEAD"):
+ if agent_id == agent.id and attempts["count"] < 2:
+ attempts["count"] += 1
+ raise RuntimeError("simulated transient HEAD-read failure")
+ return await real_get_files(agent_id=agent_id, org_id=org_id, ref=ref)
+
+ monkeypatch.setattr(app_server.memory_repo_manager.git, "get_files", flaky_get_files)
+
+ _git_push_memory_update(
+ server_url=server_url,
+ agent_id=agent.id,
+ user_id=actor_id,
+ rel_markdown_path="system/human.md",
+ markdown_content=markdown,
+ )
+
+ updated = _poll_block_value(
+ client=client,
+ agent_id=agent.id,
+ block_label="system/human",
+ expected_substring=marker,
+ timeout_seconds=25.0,
+ )
+ assert marker in updated
+ assert attempts["count"] == 2
+ finally:
+ client.agents.delete(agent.id)
diff --git a/tests/test_git_http_post_push_sync.py b/tests/test_git_http_post_push_sync.py
new file mode 100644
index 000000000..af6a86d5a
--- /dev/null
+++ b/tests/test_git_http_post_push_sync.py
@@ -0,0 +1,111 @@
+from typing import ClassVar
+
+import pytest
+from starlette.requests import Request
+
+import letta.server.rest_api.routers.v1.git_http as git_http_router
+from letta.server.rest_api.dependencies import HeaderParams
+
+
+def _build_request(method: str, path: str) -> Request:
+ received = False
+
+ async def receive():
+ nonlocal received
+ if received:
+ return {"type": "http.disconnect"}
+ received = True
+ return {"type": "http.request", "body": b"", "more_body": False}
+
+ scope = {
+ "type": "http",
+ "http_version": "1.1",
+ "method": method,
+ "scheme": "http",
+ "path": f"/v1/git/{path}",
+ "raw_path": f"/v1/git/{path}".encode(),
+ "query_string": b"",
+ "headers": [(b"user-agent", b"pytest")],
+ "client": ("127.0.0.1", 12345),
+ "server": ("testserver", 80),
+ }
+ return Request(scope, receive)
+
+
+async def test_post_push_sync_runs_after_stream_close(monkeypatch):
+ events: list[str] = []
+
+ class DummyActor:
+ id = "user-123"
+ organization_id = "org-123"
+
+ class DummyUserManager:
+ async def get_actor_or_default_async(self, actor_id=None):
+ return DummyActor()
+
+ class DummyAgentManager:
+ async def get_agent_by_id_async(self, **kwargs):
+ return object()
+
+ class DummyServer:
+ user_manager = DummyUserManager()
+ agent_manager = DummyAgentManager()
+
+ class DummyUpstream:
+ status_code = 200
+ headers: ClassVar[dict[str, str]] = {"content-type": "application/x-git-receive-pack-result"}
+
+ async def aiter_raw(self):
+ events.append("iter_start")
+ yield b"pkt"
+ events.append("iter_end")
+
+ async def aclose(self):
+ events.append("upstream_close")
+
+ class DummyAsyncClient:
+ def __init__(self, timeout=None):
+ pass
+
+ def build_request(self, **kwargs):
+ return object()
+
+ async def send(self, req, stream=True):
+ events.append("send")
+ return DummyUpstream()
+
+ async def aclose(self):
+ events.append("client_close")
+
+ async def fake_sync_after_push(actor_id: str, agent_id: str):
+ events.append("sync")
+ assert actor_id == "user-123"
+ assert agent_id == "agent-123"
+
+ from letta.settings import settings as core_settings
+
+ monkeypatch.setattr(core_settings, "memfs_service_url", "http://memfs.test")
+ monkeypatch.setattr(git_http_router.httpx, "AsyncClient", DummyAsyncClient)
+ monkeypatch.setattr(git_http_router, "_sync_after_push", fake_sync_after_push)
+
+ path = "agent-123/state.git/git-receive-pack"
+ request = _build_request(method="POST", path=path)
+ response = await git_http_router.proxy_git_http(
+ path=path,
+ request=request,
+ server=DummyServer(),
+ headers=HeaderParams(actor_id="user-123"),
+ )
+
+ body = b""
+ async for chunk in response.body_iterator:
+ body += chunk
+
+ assert body == b"pkt"
+ assert "sync" not in events
+
+ await response.background()
+
+ assert "sync" in events
+ assert events.index("sync") > events.index("iter_end")