Files
headroom/scripts/verify_mem0_dependency_boundaries.py
5c785e6938 chore(deps): bump the uv group across 1 directory with 2 updates (#4537)
Mem0's recent-conversation cache can mix distinct Headroom tenant
identities when an agent/user ID contains `&` or `=`. In the published
mem0ai 2.0.5 code, `(agent_id="alpha&user_id=beta", user_id="gamma")`
and `(agent_id="alpha", user_id="beta&user_id=gamma")` produce the same
cache key. Writing the first tenant's messages to Mem0's actual SQLite
conversation store makes them retrievable through the second tenant's
key. Mem0 2.0.18 escapes those delimiters and prevents that collision.

Upgrade the dependency and adapt Headroom to its stricter identity API:
save agent_id as an identity argument, search/list with identity
filters, use get_all's top_k argument, and restore promoted response
identities with a fallback for older metadata layouts. Otherwise the
upgraded library strips scope metadata, rejects search arguments, and
silently breaks the adapter's listing/query paths. Explicit search
user_id retains precedence over the filter's user_id.

## Dependency assessment

- **mem0ai 2.0.5 -> 2.0.18:** fixes the demonstrated tenant-buffer
isolation bug in a path used by the optional conversational-memory
backend. Headroom accepts these identifier strings; assuming they never
contain reserved characters would not be a safe substitute. The two old
Python source files were compared byte-for-byte with the SHA256-verified
public 2.0.5 wheel before reproducing the leak. [Release notes and fix
#6892](https://github.com/mem0ai/mem0/releases/tag/v2.0.18) describe
delimiter escaping and the one-time reset of recent buffers for IDs
containing `%`, `&`, or `=`. Persisted memory records are unaffected.
The independent 2.0.5 reproduction is the reason for this update; it
does not rely on interpreting the narrower version range in the upstream
security advisory as covering 2.0.5.
- **langgraph-sdk 0.4.4 -> 0.4.6:** fixes thread/assistant IDs being
interpreted as URL paths, query strings, or fragments in the stream
state's REST helpers. [Upstream fix
#9213](https://github.com/langchain-ai/langgraph/pull/9213)
percent-encodes these identifiers. A real loopback HTTP test with
`tenant/../other?query#fragment` reproduced wrong paths on 0.4.4 and
verified one encoded path segment on 0.4.6. This is an optional
graph-client dependency; the proof exercises its real transport helpers
directly, not a claim that Headroom's local LangGraph hook calls a cloud
API.
- **posthog 5.4.0 -> 7.79.0:** a required transitive major update, since
the new Mem0 distribution declares `posthog>=7.14.0`. Keep the locked
compatible 7.79.0 resolution rather than retaining a version outside
that requirement. The real 7.79.0 client passes capture/shutdown against
a loopback endpoint, and Mem0's actual opt-out creates no telemetry
client/event. This is not an independent new analytics feature for
Headroom.
- **Maintenance/security:** Mem0 is maintained by mem0ai (2.0.18
released August 11), the graph SDK by LangChain (0.4.6 released October
6), and PostHog's client by PostHog (7.79.0 uploaded October 28). Public
release/source history and Mem0's published security advisory were
reviewed. PyPI's current 0.4.6/7.79.0 responses list no vulnerability
entries; this is not a guarantee about all upstream security history.
Current branch hosted dependency audits passed before the proof-only
follow-up commit; current-head qualification remains required.
- **Install/runtime surface:** these updated distributions have
Python-only wheels; the lock package set is unchanged and these three
are the only Python package-version changes. Existing
embedding/vector/LLM dependencies still have native components, model
downloads and provider/storage network access. LangGraph SDK uses the
existing HTTP/serialization dependencies. PostHog's mandatory runtime
dependencies are requests, backoff, distro and typing-extensions;
optional async, zstd, OpenTelemetry and development/test extras are not
enabled by this update. Mem0 telemetry remains opt-out and can send
metadata/feature-flag requests to PostHog when enabled.
Exception/code-variable autocapture is not enabled by Headroom or this
change. The proof's telemetry opt-outs are test settings, not a
production-default change or a claim of blocked production egress.

The branch also carries the reviewed glob/minimatch remediation from
#4599 (now merged upstream). Its eight exact bundled advisory/path
exceptions and November 15 expiry remain unchanged.

## Real behavior proof

Windows / Python 3.12.13 with mem0ai 2.0.18, langgraph-sdk 0.4.6 and
posthog 7.79.0 explicitly installed. Actual storage proof uses
sentence-transformers/all-MiniLM-L6-v2 embeddings and temporary local
Qdrant/history storage. The existing locally installed project/embedding
dependencies were reused; this is not a claim of a fresh frozen
installation of every optional package. Model files were downloaded from
Hugging Face. No cloud credential or LLM inference request was used.

From the checkout with those exact versions and the project dependencies
installed:

```powershell
$env:PYTHONPATH='.'
python scripts/verify_mem0_tenant_scope.py
python scripts/verify_mem0_dependency_boundaries.py
python scripts/verify_mem0_agent_scope.py
```

Observed, all exit 0:

- Tenant cache: distinct identity tuples share scope key=False; other
tenant retrieved messages=0. The same storage exercise on
source-verified 2.0.5 reports True/1 and fails the isolation assertion.
Plain identifiers retain their existing keys.
- LangGraph 0.4.6 keeps the assistant/thread identifiers in encoded path
segments. Version 0.4.4 sends `/assistants/other` and
`/threads/other?query` instead and fails the path assertions.
- `MEM0_TELEMETRY=false`: no client/event created. PostHog 7.79.0
capture/shutdown sends exactly one deliberately synthetic event to the
loopback server; no event is sent to PostHog's service.
- Each of two agents retrieves exactly its own persisted memory, with
the user/agent identity and original Headroom ID restored. User listing
and filtered queries see the persisted records, while another user sees
none. `infer=False` disables LLM fact inference but retains actual
embedding, add, search, query and listing operations.

`python -m pytest tests/test_memory/test_mem0_agent_scope.py
tests/test_memory/test_direct_mem0.py
tests/test_memory/test_factory_external.py -q --no-cov -o addopts=''`:
**34 passed**, including nine red/green scope regressions. The real
storage proof was repeated with all three updated packages pinned to the
lock versions. `uv lock --check`, changed-file Ruff lint/format and
whitespace checks pass. Independent review previously found the listing
API defect, then verified its fix and reran all nine scope regressions
successfully.

The carried npm fix was separately qualified with 61 OpenCode tests,
typecheck/build, zero OpenCode audit findings, three E2E dependency
regressions on Node 24, and the exact workflow checker accepting only
eight preexisting bundled exceptions.

Untested: Mem0 cloud API, LLM fact extraction, Neo4j or hosted vector
storage, a live LangGraph deployment or event stream, PostHog
production/feature-flag delivery, sampling under load, other Python/OS
combinations, or a production rollout. The loopback tests establish the
stated identifier and transport boundaries without claiming those
external integrations were exercised.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
Co-authored-by: Kenneth Wong <17061293+KennethWKZ@users.noreply.github.com>
2026-11-02 22:09:52 -06:00

90 lines
3.0 KiB
Python

"""Credential-free checks for the locked graph and telemetry client boundaries."""
import json
import os
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from importlib.metadata import version
from urllib.parse import quote
os.environ["MEM0_TELEMETRY"] = "false"
from langgraph_sdk import get_sync_client
from mem0.memory.telemetry import AnonymousTelemetry
from posthog import Posthog
requests = []
events = []
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def do_GET(self):
requests.append(self.path)
payload = (
{"nodes": [], "edges": []}
if self.path.endswith("/graph")
else {"values": {"result": "fixture"}, "next": [], "tasks": []}
)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(payload).encode())
def do_POST(self):
data = self.rfile.read(int(self.headers["Content-Length"]))
events.append(json.loads(data))
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"status":1}')
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
base_url = f"http://127.0.0.1:{server.server_port}"
try:
thread_id = "tenant/../other?query#fragment"
assistant_id = "assistant/../other?query#fragment"
with get_sync_client(url=base_url, api_key="local-fixture-only") as client:
stream = client.threads.stream(thread_id=thread_id, assistant_id=assistant_id)
tree = stream.agent.get_tree()
output = stream.output
assert requests == [
f"/assistants/{quote(assistant_id, safe='')}/graph",
f"/threads/{quote(thread_id, safe='')}/state",
], requests
assert tree == {"nodes": [], "edges": []}
assert output == {"result": "fixture"}
print(f"langgraph-sdk={version('langgraph-sdk')}: identifiers stay in one encoded path segment")
telemetry = AnonymousTelemetry()
assert telemetry.posthog is None
telemetry.capture_event("headroom.boundary.proof", {"fixture": True})
assert not events
print("MEM0_TELEMETRY=false: no client/event created")
posthog = Posthog(
project_api_key="phc_local_fixture_only",
host=base_url,
sync_mode=True,
feature_flags_request_timeout_seconds=0.5,
enable_exception_autocapture=False,
)
try:
posthog.capture(
"headroom.boundary.proof", distinct_id="local-fixture", properties={"fixture": True}
)
finally:
posthog.shutdown()
assert len(events) == 1, events
assert events[0]["batch"][0]["event"] == "headroom.boundary.proof", events
print(f"posthog={version('posthog')}: capture/shutdown sent one event to loopback only")
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)