headroom/tests/test_memory/test_mcp_server.py
Rod Boev ad7993bf15
fix(codex): stop pinning Codex memory MCP to one project db (#1269)
## Description

Stop `headroom wrap codex --memory` from pinning the global
`headroom_memory` MCP server to one absolute SQLite path. Today the
wrapper writes `--db <wrap-cwd>/.headroom/memory.db` into
`~/.codex/config.toml`, which makes later Codex sessions either reopen a
stale project-local DB or fail with `unable to open database file` when
that original path disappears. This change lets the MCP server use its
existing per-cwd default again, so each Codex session resolves
`.headroom/memory.db` from the active project instead of a serialized
past cwd. Closes #1147

The current Codex-memory config surface was shaped by
https://github.com/chopratejas/headroom/issues/462 and
https://github.com/chopratejas/headroom/issues/730; this PR keeps that
surface project-scoped again instead of globally pinning one DB.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- remove the injected `--db` argument from the global `headroom_memory`
Codex MCP block while keeping `--user` intact
- preserve the wrap-time local `.headroom/memory.db` setup and
Claude-memory import path for the current project
- treat only wrap-owned Codex markers as snapshot-suppression and
unwrap-cleanup signals, so pre-existing named MCP blocks still back up
and restore
- log a startup diagnostic from `headroom.memory.mcp_server` that
records the configured DB path, config source, cwd/project root,
resolved storage scope, path existence/readability, and whether the path
was static or cwd-derived
- add a shared MCP SDK test stub so both the memory MCP and CCR MCP test
surfaces still run in CI when `mcp` is absent
- make the shared MCP stub re-import target modules under the stubbed
dependency set and restore any pre-existing target module object plus
dotted parent-package attribute state after cleanup
- add focused regressions and guard coverage for the persisted Codex
config shape, named-MCP marker backup and restore, the no-backup
memory-only unwrap path, the wrap-memory-then-unwrap cleanup path, the
failed-wrap memory-only cleanup path, the startup-diagnostic path
classification, the shared-store CCR retrieval path, and the shared MCP
stub import lifecycle
- add a `CHANGELOG.md` entry for the user-visible Codex memory scoping
fix

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`uv run mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
uv run pytest tests/test_ccr_mcp_server.py tests/test_memory/test_mcp_server.py tests/test_cli/test_wrap_codex.py tests/test_mcp_stub.py
======================== 78 passed, 1 warning in 5.96s ========================
Pytest warning:
PytestConfigWarning: Unknown config option: asyncio_mode
Pytest post-success atexit noise:
PermissionError: [WinError 5] Access is denied: 'C:\Users\Rod\AppData\Local\Temp\pytest-of-Rod\pytest-current'

uv run ruff check headroom/cli/wrap.py headroom/memory/mcp_server.py tests/_mcp_stub.py tests/test_ccr_mcp_server.py tests/test_cli/test_wrap_codex.py tests/test_mcp_stub.py tests/test_memory/test_mcp_server.py
All checks passed!

uv run ruff format headroom/cli/wrap.py headroom/memory/mcp_server.py tests/_mcp_stub.py tests/test_ccr_mcp_server.py tests/test_cli/test_wrap_codex.py tests/test_mcp_stub.py tests/test_memory/test_mcp_server.py --check
7 files already formatted
```

## Real Behavior Proof

- Environment: isolated temp project directories, a temp Codex home, the
real `wrap codex` and `unwrap codex` CLI commands under pytest, a mocked
missing-`codex` launch path for the failed-wrap cleanup case, and shared
MCP-SDK stubs for the memory MCP and CCR MCP test modules so CI still
exercises those paths without a real `mcp` install.
- Exact command / steps: run `uv run pytest tests/test_ccr_mcp_server.py
tests/test_memory/test_mcp_server.py tests/test_cli/test_wrap_codex.py
tests/test_mcp_stub.py`; prove the persisted config shape with
`TestCodexMemoryMcpConfig::test_inject_omits_db_and_replaces_existing_memory_block`;
prove prepare-only wrap cleanup with
`test_wrap_codex_memory_prepare_only_unwrap_removes_memory_mcp_without_prior_config`;
prove failed-wrap cleanup with
`test_wrap_codex_memory_launch_failure_unwrap_cleans_memory_only_config`;
guard pre-existing named Codex MCP preservation with
`test_memory_only_wrap_restores_preexisting_named_mcp_block` and
`test_memory_only_wrap_without_backup_preserves_named_mcp_block`; prove
the startup diagnostic classifications with
`test_memory_mcp_startup_context_reports_dynamic_project_db` and
`test_memory_mcp_startup_context_reports_static_external_db`; prove the
shared-store CCR retrieval path with
`test_mcp_uses_shared_singleton_store` and
`test_mcp_retrieves_proxy_stored_content`; prove stub import cleanup
with `test_import_module_with_mcp_stub_imports_target_and_cleans_up`,
`test_import_module_with_mcp_stub_reimports_target_and_restores_originals`,
and
`test_import_module_with_mcp_stub_cleans_up_dotted_target_attribute`.
- Observed result: the persisted global `headroom_memory` block now
keeps `--user` but omits `--db`; prepare-only memory setup still
bootstraps the current project's `.headroom/memory.db`; `headroom unwrap
codex --no-stop-proxy` now removes both the prepare-only generated
config and the failed-wrap memory-only config instead of leaving
`[mcp_servers.headroom_memory]` behind; pre-existing named Codex MCP
blocks remain restorable across both normal and no-backup memory-only
unwrap paths because only wrap-owned markers suppress backups or trigger
named-block cleanup; the memory MCP server now logs whether its DB path
came from the cwd default or an explicit static path, along with the
resolved path and scope it will open; CI can exercise both MCP test
modules even when the `mcp` package is absent from the shard
environment, and the shared stub now re-imports target modules under the
stubbed SDK while restoring both dependency and dotted parent-package
target-module import state after cleanup.
- Not tested: full end-to-end interactive Codex CLI launch.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I have updated the CHANGELOG.md if applicable

## Additional Notes

The code change stays narrowly scoped to Codex memory config
persistence, cleanup, and startup observability. It does not widen into
larger memory-routing redesign or startup-failure recovery logic.
2026-06-23 07:49:07 -05:00

165 lines
5.4 KiB
Python

import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock
import numpy as np
from headroom.memory.models import Memory
from tests._mcp_stub import import_module_with_mcp_stub
mcp_server_mod = import_module_with_mcp_stub("headroom.memory.mcp_server")
def test_warm_up_backend_batches_embedding_and_indexing() -> None:
"""Warm-up should batch missing embeddings and vector indexing."""
warmup_embedding = np.ones(384, dtype=np.float32)
batch_embeddings = [
np.full(384, 2.0, dtype=np.float32),
np.full(384, 3.0, dtype=np.float32),
]
embedder = SimpleNamespace(
embed=AsyncMock(return_value=warmup_embedding),
embed_batch=AsyncMock(return_value=batch_embeddings),
)
store = SimpleNamespace(save_batch=AsyncMock())
vector_index = SimpleNamespace(index_batch=AsyncMock(return_value=3))
memory_without_embedding_a = Memory(content="First", user_id="alice")
memory_with_embedding = Memory(
content="Second",
user_id="alice",
embedding=np.full(384, 5.0, dtype=np.float32),
)
memory_without_embedding_b = Memory(content="Third", user_id="alice")
memories = [
memory_without_embedding_a,
memory_with_embedding,
memory_without_embedding_b,
]
backend = SimpleNamespace(
_ensure_initialized=AsyncMock(),
_hierarchical_memory=SimpleNamespace(
_embedder=embedder,
_store=store,
_vector_index=vector_index,
),
get_user_memories=AsyncMock(return_value=memories),
)
asyncio.run(mcp_server_mod._warm_up_backend(backend, "alice"))
backend._ensure_initialized.assert_awaited_once()
backend.get_user_memories.assert_awaited_once_with("alice", limit=500)
embedder.embed.assert_awaited_once_with("warmup")
embedder.embed_batch.assert_awaited_once_with(["First", "Third"])
store.save_batch.assert_awaited_once_with(
[memory_without_embedding_a, memory_without_embedding_b]
)
vector_index.index_batch.assert_awaited_once_with(memories)
assert np.array_equal(memory_without_embedding_a.embedding, batch_embeddings[0])
assert np.array_equal(memory_without_embedding_b.embedding, batch_embeddings[1])
def test_memory_mcp_startup_context_reports_dynamic_project_db(tmp_path) -> None:
project_dir = tmp_path / "project-a"
project_dir.mkdir()
configured_db = str(project_dir / ".headroom" / "memory.db")
context = mcp_server_mod._memory_mcp_startup_context(
configured_db,
project_dir,
db_flag_present=False,
)
assert context == {
"configured_db": configured_db,
"resolved_db": configured_db,
"config_source": "cwd-default",
"cwd": str(project_dir),
"project_root": str(project_dir),
"storage_scope": "active-project",
"path_exists": False,
"path_readable": False,
"resolution": "dynamic-cwd",
}
def test_memory_mcp_startup_context_reports_static_external_db(tmp_path) -> None:
project_dir = tmp_path / "project-a"
project_dir.mkdir()
external_db = tmp_path / "shared-memory" / "memory.db"
external_db.parent.mkdir()
external_db.write_text("sqlite placeholder")
context = mcp_server_mod._memory_mcp_startup_context(
str(external_db),
project_dir,
db_flag_present=True,
)
assert context == {
"configured_db": str(external_db),
"resolved_db": str(external_db.resolve(strict=False)),
"config_source": "cli-flag",
"cwd": str(project_dir),
"project_root": str(project_dir),
"storage_scope": "external-memory-db",
"path_exists": True,
"path_readable": True,
"resolution": "static-cli",
}
def test_memory_mcp_startup_context_reports_custom_db_path(tmp_path) -> None:
project_dir = tmp_path / "project-a"
project_dir.mkdir()
custom_db = tmp_path / "queries.db"
context = mcp_server_mod._memory_mcp_startup_context(
str(custom_db),
project_dir,
db_flag_present=True,
)
assert context["storage_scope"] == "custom-db-path"
assert context["config_source"] == "cli-flag"
assert context["path_exists"] is False
assert context["path_readable"] is False
def test_main_logs_memory_mcp_startup_context(monkeypatch, tmp_path, caplog) -> None:
project_dir = tmp_path / "project-a"
project_dir.mkdir()
monkeypatch.chdir(project_dir)
monkeypatch.setenv("USER", "codex-user")
monkeypatch.setattr(mcp_server_mod.logging, "basicConfig", lambda **kwargs: None)
monkeypatch.setattr(mcp_server_mod.sys, "argv", ["memory-mcp"])
captured_run_payloads: list[object] = []
monkeypatch.setattr(
mcp_server_mod,
"_run",
lambda db_path, user_id: ("run", db_path, user_id),
)
monkeypatch.setattr(
mcp_server_mod.asyncio,
"run",
lambda payload: captured_run_payloads.append(payload),
)
caplog.set_level("INFO", logger="headroom.memory.mcp")
mcp_server_mod.main()
assert captured_run_payloads == [
("run", str(project_dir / ".headroom" / "memory.db"), "codex-user")
]
assert any(
"Memory MCP startup: configured_db=" in record.message
and "config_source=cwd-default" in record.message
and "storage_scope=active-project" in record.message
and "resolution=dynamic-cwd" in record.message
for record in caplog.records
)