fix(memory): resolve pi scope before injecting tools

This commit is contained in:
oxura
2026-11-03 07:09:24 +06:00
parent fc1ba891bf
commit 7aeb83cf05
12 changed files with 247 additions and 353 deletions
+21 -8
View File
@@ -8,8 +8,8 @@ a request.
Three storage modes:
* ``PROJECT`` (default): one DB per resolved project. The project is
identified from request headers (explicit) or by parsing a Claude
Code / Codex ``<env>`` block for the working directory.
identified from explicit request signals or client working-directory
context, including Claude Code / Codex ``<env>`` and pi ``<cwd>`` blocks.
* ``USER``: one DB per ``x-headroom-user-id`` (no project axis).
* ``GLOBAL``: a single DB shared across everything. Matches the pre-fix
behaviour and is preserved so users can still reach memories written
@@ -183,7 +183,7 @@ class ProjectResolver:
if ident is not None:
return ident
# Tier 4: parse the system prompt for a ``<env>`` cwd line.
# Tier 4: parse client working-directory context from the system prompt.
sys_cwd = self._extract_cwd_from_system_prompt(ctx.system_prompt)
if sys_cwd:
ident = self._identity_from_cwd(sys_cwd)
@@ -207,7 +207,9 @@ class ProjectResolver:
return None
@classmethod
def _extract_cwd_from_system_prompt(cls, system_prompt: str) -> str | None:
def _extract_cwd_from_system_prompt(
cls, system_prompt: str, *, allow_cwd_block: bool = True
) -> str | None:
if not system_prompt:
return None
for prefix in _CWD_PREFIXES:
@@ -220,6 +222,13 @@ class ProjectResolver:
chunk = line[len(prefix) :].strip()
if chunk:
return chunk
if allow_cwd_block:
block_start = system_prompt.find("<cwd>")
if block_start >= 0:
block_start += len("<cwd>")
block_end = system_prompt.find("</cwd>", block_start)
if block_end >= 0:
return system_prompt[block_start:block_end].strip() or None
return None
@classmethod
@@ -491,16 +500,20 @@ def _first_user_text_with_cwd(messages: list[Any]) -> str:
Claude Code 2.x sends its ``<env>`` block as an ``isMeta`` *user*
message rather than inside ``system`` (#3595), and Trae puts it in a
user reminder (#1737). Only text that actually contains one of
``_CWD_PREFIXES`` is returned, so ordinary user content never leaks
into the system prompt.
user reminder (#1737). Only usable legacy cwd lines enable this
fallback; pi cwd blocks are recognized in system context, not
ordinary user text.
"""
for msg in messages:
if not isinstance(msg, dict) or msg.get("role") != "user":
continue
content = msg.get("content")
user_text = content if isinstance(content, str) else _text_from_blocks(content)
if user_text and ProjectResolver._extract_cwd_from_system_prompt(user_text) is not None:
if (
user_text
and ProjectResolver._extract_cwd_from_system_prompt(user_text, allow_cwd_block=False)
is not None
):
return user_text
return ""
+7 -15
View File
@@ -2968,18 +2968,16 @@ class AnthropicHandlerMixin:
except Exception as e:
logger.debug(f"[{request_id}] Traffic learner: {e}")
# Memory: Inject context and tools — gated on MemoryDecision.
# ``inject`` is False under bypass, missing handler, missing
# user_id, or HEADROOM_MEMORY_INJECTION_MODE in disabled/tool.
# Pre-PR-this the gate was a raw conjunction that silently
# ignored bypass; now bypass is honoured here on Anthropic
# /v1/messages just as on /v1/responses.
# Gate both context and tools on request policy and storage scope.
# Missing scope also blocks replay of previously injected definitions.
memory_context_injected = False
memory_tools_injected = False
# Memory tools this proxy injected and must execute itself; the
# streaming path withholds their calls from the client.
server_memory_tool_names: frozenset[str] = frozenset()
if memory_decision.inject:
if memory_decision.inject and not self.memory_handler.is_project_unresolved(
memory_request_ctx
):
# Search and inject memory context
if self.memory_handler.config.inject_context:
try:
@@ -3062,14 +3060,8 @@ class AnthropicHandlerMixin:
except Exception as e:
logger.warning(f"[{request_id}] Memory: Context injection failed: {e!r}")
# Inject memory tools — PR-A7 (P0-6) routes through
# `apply_session_sticky_memory_tools` so tool list bytes
# stay byte-stable across turns: once a session injects,
# every subsequent turn replays the same canonical bytes.
# `inject_this_turn` is True iff memory is enabled this
# turn (i.e. memory_handler.config.inject_tools and we
# have a memory_user_id, which the outer guard at line
# 1192 already enforces).
# Within an available scope, keep memory tool bytes stable across
# turns. Missing scope skips this block even when golden bytes exist.
from headroom.proxy.helpers import (
apply_session_sticky_memory_tools,
)
@@ -32,6 +32,9 @@ class _MemoryHandler:
self.backend = None
self.executed: list[str] = []
def is_project_unresolved(self, request_context: Any) -> bool:
return False
def compute_memory_tool_definitions(self, provider: str) -> list[dict[str, Any]]:
return [
{"name": name, "description": name, "input_schema": {"type": "object"}}
@@ -697,6 +697,9 @@ def test_memory_context_timeout_fails_open_and_releases_semaphore():
self.initialized = False
self.backend = None
def is_project_unresolved(self, _request_context: object) -> bool:
return False
async def search_and_format_context(self, _user_id, _messages, **_kwargs):
await asyncio.sleep(5.0)
return "should-timeout"
-198
View File
@@ -1,198 +0,0 @@
"""AST-walking contract tests locking memory-system invariants.
These tests introspect the four handler modules' source ASTs to assert
two structural properties that, if regressed, would silently re-
introduce the bug classes this PR fixed:
(a) **No raw memory-gate conjunction**: every memory injection block
must be gated by ``MemoryDecision``, not by an inline
``if self.memory_handler and memory_user_id`` conjunction. The
raw conjunction is what allowed sites 1/2/3 to silently ignore
``x-headroom-bypass: true``.
(b) **No memory writes to system/instructions**: memory injection
must target user-message-tail / body["input"] / messages, never
``body["instructions"]`` or system content. Pre-PR-this the WS
handler was the lone outlier writing to instructions; the AST
check ensures it doesn't sneak back.
The checks are static — no handler is invoked. They run in
milliseconds and catch future regressions at PR-review time.
"""
from __future__ import annotations
import ast
import re
from pathlib import Path
import pytest
HANDLER_FILES = [
Path("headroom/proxy/handlers/anthropic.py"),
Path("headroom/proxy/handlers/openai.py"),
Path("headroom/proxy/handlers/gemini.py"),
Path("headroom/proxy/handlers/batch.py"),
]
# ── Invariant A — no raw memory-gate conjunction ──────────────────────
def _file_contains_raw_memory_gate(file_path: Path) -> list[tuple[int, str]]:
"""Find ``if (self.)memory_handler and memory_user_id`` raw
conjunctions in the file. Returns list of (line, snippet).
The acceptable replacements are ``if memory_decision.inject:`` or
``if (responses|ws)_memory_decision.inject:``.
AST-walking the conditional itself is complex (BoolOp + Attribute),
so we use a regex to scan source lines. False positives are caught
by the test author at write time — this is a one-line invariant.
"""
text = file_path.read_text(encoding="utf-8")
# Match "if self.memory_handler and memory_user_id" but NOT inside
# the helper that defines the decision (which references both names
# for documentation purposes) — we only care about handler logic.
pattern = re.compile(r"^\s*if\s+self\.memory_handler\s+and\s+memory_user_id\b")
hits = []
for i, line in enumerate(text.splitlines(), start=1):
if pattern.match(line):
hits.append((i, line.rstrip()))
return hits
def test_no_raw_memory_handler_gate_in_handlers() -> None:
"""Pre-PR-this, sites 1/2/3 used ``if self.memory_handler and
memory_user_id:`` as the memory-injection gate — silently
ignoring bypass. After PR-this, every site routes through
``MemoryDecision.decide(...)`` and gates on
``memory_decision.inject``. This test ensures the raw conjunction
cannot return without explicit review."""
offenders = []
for f in HANDLER_FILES:
offenders.extend([(f, ln, src) for (ln, src) in _file_contains_raw_memory_gate(f)])
if offenders:
formatted = "\n".join(f" {f.name}:{ln} {src!r}" for f, ln, src in offenders)
pytest.fail(
f"{len(offenders)} handler site(s) use the pre-PR raw memory "
"gate `if self.memory_handler and memory_user_id`:\n"
f"{formatted}\n\n"
"Replace with `MemoryDecision.decide(...)` + "
"`if memory_decision.inject:`. See PR for the canonical pattern."
)
# ── Invariant B — memory never writes to system/instructions ─────────
_FORBIDDEN_SYSTEM_WRITES = (
# Direct mutation of cache-hot-zone system fields by the memory
# path. The patterns below are exact assignment forms — they match
# the pre-PR-this WS bug at openai.py:3517.
re.compile(r'ws_response_body\["instructions"\]\s*='),
re.compile(r'response_body\["instructions"\]\s*='),
re.compile(r'body\["instructions"\]\s*='),
re.compile(r'body\["system"\]\s*='),
)
def _line_is_memory_related(line: str) -> bool:
"""Heuristic: a line that contains ``memory_context`` or
``memory_inject`` is in the memory-injection code path."""
return "memory_context" in line or "memory_inject" in line
def _find_system_writes_in_memory_context(file_path: Path) -> list[tuple[int, str]]:
"""Find lines that both:
- Look like a write to body["instructions"] / body["system"]
- Live within ~10 lines of a ``memory_context`` reference
This is a windowed-context check — we don't want false positives
from unrelated instructions-writes (e.g. tool-result handling).
"""
text = file_path.read_text(encoding="utf-8").splitlines()
memory_line_indices = [i for i, line in enumerate(text) if _line_is_memory_related(line)]
hits = []
for i, line in enumerate(text):
if not any(p.search(line) for p in _FORBIDDEN_SYSTEM_WRITES):
continue
# Within 10 lines of any memory-related line?
if any(abs(i - mi) <= 10 for mi in memory_line_indices):
hits.append((i + 1, line.strip()))
return hits
def test_memory_never_writes_to_system_or_instructions() -> None:
"""Pre-PR-this, the WS handler wrote memory context to
``ws_response_body["instructions"]`` — the system / cache-hot-zone
field. That mutated the prefix cache bytes on every turn. All
other sites route to user-message tail / body["input"]. This
test asserts memory_context-related code paths never write to a
forbidden system-field assignment."""
offenders = []
for f in HANDLER_FILES:
offenders.extend([(f, ln, src) for (ln, src) in _find_system_writes_in_memory_context(f)])
if offenders:
formatted = "\n".join(f" {f.name}:{ln} {src!r}" for f, ln, src in offenders)
pytest.fail(
f"{len(offenders)} suspected memory→system write(s):\n"
f"{formatted}\n\n"
"Memory must append to user-message tail (e.g. body['input'] "
"for Responses, optimized_messages for chat). Never write to "
"body['instructions'] or body['system'] — they are the cache "
"hot zone (invariant I2)."
)
# ── Invariant C — every memory-search call passes a MemoryQuery ──────
def _find_search_and_format_context_calls_without_query(
file_path: Path,
) -> list[tuple[int, str]]:
"""Find ``search_and_format_context(...)`` invocations that DON'T
pass a ``query=`` kwarg.
Pre-PR-this no site passed a query — they all relied on the
handler's internal ``_extract_user_query(messages)`` with its
500-char truncation. The new contract: every handler builds a
full-fidelity ``MemoryQuery`` and passes it explicitly.
"""
text = file_path.read_text(encoding="utf-8")
tree = ast.parse(text, filename=str(file_path))
hits = []
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
if not isinstance(node.func, ast.Attribute):
continue
if node.func.attr != "search_and_format_context":
continue
kwarg_keys = {kw.arg for kw in node.keywords if kw.arg is not None}
if "query" not in kwarg_keys:
line_no = node.lineno
snippet = text.splitlines()[line_no - 1].strip()
hits.append((line_no, snippet))
return hits
def test_every_search_and_format_context_call_passes_query_kwarg() -> None:
"""Every handler that searches memory MUST pass a ``query=`` kwarg
(a :class:`MemoryQuery` instance), not rely on the handler's
internal ``_extract_user_query`` (which used to truncate to 500
chars). Locks the full-fidelity-query contract."""
offenders = []
for f in HANDLER_FILES:
offenders.extend(
[(f, ln, src) for (ln, src) in _find_search_and_format_context_calls_without_query(f)]
)
if offenders:
formatted = "\n".join(f" {f.name}:{ln} {src!r}" for f, ln, src in offenders)
pytest.fail(
f"{len(offenders)} search_and_format_context call(s) miss `query=`:\n"
f"{formatted}\n\n"
"Pass `query=MemoryQuery.from_messages(...)` — the multi-source, "
"untruncated query value type. See headroom/proxy/memory_query.py."
)
+59
View File
@@ -181,6 +181,65 @@ def test_resolver_ignores_incidental_cwd_prose() -> None:
)
@pytest.mark.parametrize(
("cwd", "prompt"),
[
("/Users/foo/code/pi", "<cwd>\n/Users/foo/code/pi\n</cwd>"),
("/work/pi", "Before\n<cwd>/work/pi</cwd>\nAfter"),
("/work/acme%2Fapi", "<cwd>\r\n/work/acme%2Fapi\r\n</cwd>"),
("/work/cwd:repo", "<cwd>\n/work/cwd:repo\n</cwd>"),
],
)
def test_resolver_pi_cwd_uses_the_same_project_as_explicit_header(cwd: str, prompt: str) -> None:
from urllib.parse import quote
resolver = ProjectResolver()
expected = resolver.resolve(_ctx(headers={"x-headroom-cwd": quote(cwd, safe="/")}))
assert resolver.resolve(_ctx(system_prompt=prompt)) == expected
def test_pi_system_cwd_wins_over_user_cwd_fallback() -> None:
body = {
"system": "<cwd>\n/trusted/project\n</cwd>",
"messages": [{"role": "user", "content": "cwd: /different/project"}],
}
resolver = ProjectResolver()
expected = resolver.resolve(_ctx(headers={"x-headroom-cwd": "/trusted/project"}))
assert resolver.resolve(_ctx(system_prompt=extract_system_prompt(body))) == expected
@pytest.mark.parametrize(
"content",
[
"<cwd>\n/untrusted/project\n</cwd>",
"cwd:\n<cwd>/untrusted/project</cwd>",
"<cwd>\n/untrusted/cwd:\n</cwd>",
],
)
def test_pi_cwd_in_ordinary_user_content_does_not_resolve_a_project(content: str) -> None:
body = {"system": "You are helpful.", "messages": [{"role": "user", "content": content}]}
assert ProjectResolver().resolve(_ctx(system_prompt=extract_system_prompt(body))) is None
@pytest.mark.parametrize(
"content",
[
"cwd: /legacy/project\n<cwd>/untrusted/project</cwd>",
"<cwd>/untrusted/project</cwd>\ncwd: /legacy/project",
],
)
def test_pi_tags_do_not_override_legacy_user_cwd(content: str) -> None:
body = {"system": "You are helpful.", "messages": [{"role": "user", "content": content}]}
resolver = ProjectResolver()
expected = resolver.resolve(_ctx(headers={"x-headroom-cwd": "/legacy/project"}))
assert resolver.resolve(_ctx(system_prompt=extract_system_prompt(body))) == expected
@pytest.mark.parametrize("prompt", ["<cwd>/work/pi", "<cwd>\n \n</cwd>"])
def test_incomplete_or_empty_pi_cwd_does_not_resolve_a_project(prompt: str) -> None:
assert ProjectResolver().resolve(_ctx(system_prompt=prompt)) is None
def test_resolver_returns_none_when_nothing_resolves() -> None:
r = ProjectResolver()
out = r.resolve(_ctx(system_prompt="A generic system prompt with no env block."))
+137
View File
@@ -0,0 +1,137 @@
"""Unresolved project requests must not acquire Headroom memory tools."""
from __future__ import annotations
from copy import deepcopy
from pathlib import Path
import httpx
import pytest
pytest.importorskip("fastapi")
from fastapi.testclient import TestClient
from headroom.memory.storage_router import BackendRouter, BackendRouterConfig, MemoryStorageMode
from headroom.proxy.memory_handler import MEMORY_TOOL_NAMES
from headroom.proxy.server import ProxyConfig, create_app
@pytest.mark.parametrize(
("storage_mode", "fallback", "allow_unresolved"),
[
(MemoryStorageMode.PROJECT, "empty", False),
(MemoryStorageMode.PROJECT, "global", True),
(MemoryStorageMode.USER, "empty", True),
(MemoryStorageMode.GLOBAL, "empty", True),
],
)
def test_memory_tools_follow_request_scope_without_losing_client_tools(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
storage_mode: MemoryStorageMode,
fallback: str,
allow_unresolved: bool,
) -> None:
monkeypatch.setenv("HEADROOM_MEMORY_INJECTION_MODE", "live_zone_tail")
monkeypatch.delenv("HEADROOM_TOOL_INJECTION_STICKY", raising=False)
# Isolate paths without changing the process-global stateless policy.
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path))
monkeypatch.setenv("HEADROOM_BEACON", "off")
config = ProxyConfig(
optimize=False,
memory_enabled=True,
memory_db_path=str(tmp_path / "memory.db"),
memory_storage_mode=storage_mode.value,
memory_inject_context=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
image_optimize=False,
subscription_tracking_enabled=False,
)
captured: list[dict] = []
async def upstream(method, url, headers, body, stream=False, **kwargs):
captured.append(deepcopy(body))
return httpx.Response(
200,
json={
"id": "msg_scope",
"type": "message",
"role": "assistant",
"model": "claude-haiku-4-5",
"content": [{"type": "text", "text": "ok"}],
"stop_reason": "end_turn",
"stop_sequence": None,
"usage": {"input_tokens": 10, "output_tokens": 1},
},
)
client_tools = [
{
"name": "get_weather",
"description": "Get weather",
"input_schema": {"type": "object", "properties": {}},
},
{
"name": "memory_search",
"description": "Client-owned search, not Headroom memory",
"input_schema": {"type": "object", "properties": {"query": {"type": "string"}}},
},
]
payload = {
"model": "claude-haiku-4-5",
"max_tokens": 16,
"system": "You are helpful.",
"messages": [{"role": "user", "content": "hello"}],
"tools": client_tools,
}
headers = {
"x-api-key": "local-test-key",
"anthropic-version": "2023-06-01",
"x-headroom-user-id": "scope-test-user",
}
with TestClient(create_app(config)) as client:
proxy = client.app.state.proxy
handler = proxy.memory_handler
assert handler is not None
if fallback == "global":
handler._router = BackendRouter(
BackendRouterConfig(
mode=storage_mode,
root_dir=tmp_path / "memories",
global_db_path=tmp_path / "memory.db",
unresolved_project_fallback=fallback,
)
)
monkeypatch.setattr(proxy, "_retry_request", upstream)
monkeypatch.setattr(
proxy.session_tracker_store,
"compute_session_id",
lambda request, model, messages: f"memory-scope-{tmp_path.name}",
)
for project in (None, "project-a", None, "project-a"):
request_headers = dict(headers)
if project:
request_headers["x-headroom-project-id"] = project
response = client.post("/v1/messages", headers=request_headers, json=payload)
assert response.status_code == 200
forwarded = captured[-1]["tools"]
names = {tool["name"] for tool in forwarded}
expected = {"get_weather", "memory_search"}
if project or allow_unresolved:
expected |= MEMORY_TOOL_NAMES
assert names == expected
assert (
next(tool for tool in forwarded if tool["name"] == "memory_search")
== client_tools[1]
)
# Losing scope must not replay tools, but restoring it keeps their golden bytes.
assert captured[1]["tools"] == captured[3]["tools"]
-61
View File
@@ -779,67 +779,6 @@ class TestServerMemoryToolNames:
class TestHandlerPassesServerMemoryTools:
def test_injected_memory_tools_reach_the_stream(self) -> None:
from types import SimpleNamespace
from fastapi.responses import StreamingResponse
from fastapi.testclient import TestClient
from headroom.proxy.server import ProxyConfig, create_app
config = ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
image_optimize=False,
)
captured: dict[str, Any] = {}
async def fake_stream_response(*args: Any, **kwargs: Any) -> StreamingResponse:
captured.update(kwargs, body=args[2])
async def gen() -> Any:
yield b""
return StreamingResponse(gen(), media_type="text/event-stream")
memory_tools = [
{"name": name, "description": name, "input_schema": {"type": "object"}}
for name in sorted(MEMORY_TOOLS)
]
with TestClient(create_app(config)) as client:
proxy = client.app.state.proxy
proxy.memory_handler = SimpleNamespace(
config=SimpleNamespace(inject_context=False, inject_tools=True),
compute_memory_tool_definitions=lambda provider: memory_tools,
get_beta_headers=lambda: {},
has_memory_tool_calls=lambda resp, provider: False,
)
proxy._stream_response = fake_stream_response
client.post(
"/v1/messages",
headers={
"x-api-key": "test-key",
"anthropic-version": "2023-06-01",
"x-headroom-user-id": "u1",
},
json={
"model": "claude-sonnet-4-6",
"max_tokens": 64,
"stream": True,
"tools": [{"name": "Bash", "input_schema": {"type": "object"}}],
"messages": [{"role": "user", "content": "remember the deploy region"}],
},
)
assert captured["server_memory_tool_names"] == MEMORY_TOOLS
assert MEMORY_TOOLS <= {t["name"] for t in captured["body"]["tools"]}
@pytest.mark.parametrize("after_buffered_turn", [False, True])
def test_streamed_openai_chat_gets_no_memory_tools(self, after_buffered_turn: bool) -> None:
from types import SimpleNamespace
@@ -349,74 +349,6 @@ def test_anthropic_ccr_continuation_uses_buffered_timeout() -> None:
assert isinstance(http_client.calls[0]["timeout"], httpx.Timeout)
def test_anthropic_memory_continuation_uses_buffered_timeout() -> None:
config = _make_config()
config.memory_enabled = True
app = create_app(config)
class _MemoryHandler:
def __init__(self) -> None:
self.config = type(
"MemoryConfig",
(),
{
"inject_context": False,
"inject_tools": False,
"project_root_override": "",
},
)()
self.initialized = False
self.backend = None
def get_beta_headers(self) -> dict[str, str]:
return {}
def has_memory_tool_calls(self, response, provider): # noqa: ANN001
return True
async def handle_memory_tool_calls( # noqa: ANN001
self,
response,
user_id,
provider,
**kwargs,
):
return [{"type": "tool_result", "tool_use_id": "mem_1", "content": "memory"}]
with TestClient(app) as client:
proxy = client.app.state.proxy
_install_prefix_tracker(proxy)
proxy.memory_handler = _MemoryHandler()
captured_timeouts: list[httpx.Timeout | None] = []
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
timeout = kwargs.get("timeout")
captured_timeouts.append(timeout)
if len(body["messages"]) > 1:
_assert_buffered_timeout(timeout)
return httpx.Response(200, json=_anthropic_message_response())
proxy._retry_request = _fake_retry # type: ignore[assignment]
client.post(
"/v1/messages",
headers={
"x-api-key": "test-key",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
"x-headroom-user-id": "user-1",
},
json={
"model": "claude-sonnet-4-6",
"max_tokens": 64,
"messages": [{"role": "user", "content": "hello"}],
},
)
assert len(captured_timeouts) == 2
assert all(isinstance(timeout, httpx.Timeout) for timeout in captured_timeouts)
def test_retry_request_without_override_uses_client_default_timeout():
config = _make_config()
app = create_app(config)
@@ -438,6 +438,7 @@ def test_memory_context_avoids_system_mutation_when_prefix_frozen() -> None:
proxy.memory_handler = SimpleNamespace(
config=SimpleNamespace(inject_context=True, inject_tools=False),
is_project_unresolved=lambda request_context: False,
search_and_format_context=AsyncMock(return_value="MEMCTX"),
has_memory_tool_calls=lambda resp, provider: False,
)
@@ -68,6 +68,7 @@ def _install_memory_handler(proxy: object) -> None:
"""Wire a deterministic in-process memory handler that returns 'MEMCTX'."""
proxy.memory_handler = SimpleNamespace( # type: ignore[attr-defined]
config=SimpleNamespace(inject_context=True, inject_tools=False),
is_project_unresolved=lambda request_context: False,
search_and_format_context=AsyncMock(return_value="MEMCTX"),
has_memory_tool_calls=lambda resp, provider: False,
)
+15 -3
View File
@@ -74,9 +74,21 @@ Claude Code Codex CLI Gemini CLI
### Project-Scoped Database
Memory is stored per-project at `{cwd}/.headroom/memory.db`. Each
project has its own memory — no cross-project contamination. Override
with `--memory-db-path` for a custom location.
In local `project` mode (the default), each resolved workspace gets its own
SQLite database at `<memory-db-parent>/memories/projects/<project-key>/memory.db`.
Use `--memory-db-path` to choose the base database path.
Resolution checks `x-headroom-project-id`, then `x-headroom-cwd`, then
`--memory-project-root`, then the client's system-prompt working directory.
Claude Code / Codex cwd lines and pi's `<cwd>...</cwd>` block are supported.
The same directory produces the same project key across these formats;
explicit request headers still win.
With the default `empty` fallback, Anthropic requests with an unresolved project
receive neither memory context nor Headroom memory tools, even when the session
previously injected those tools. Client-owned tools are left alone. Once scope
is restored, the session can replay its memory definitions. Explicit `user` /
`global` storage and configured global fallback keep their existing behavior.
> **Filesystem contract note.** Project-scoped memory paths resolve
> relative to the current working directory and **do not** obey the