mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(memory): fail close unresolved project operations (#2637)
## Description An unresolved project scope still carried the global database path. Custom-tool, native-memory, and learner paths could therefore initialize or reuse global state after context injection had already skipped. This PR makes unresolved project scopes backend-less and checks request scope before any memory or learning operation. Optional memory failures still allow the main model request to continue. This supersedes #502 at reviewed commit `9d45c11233959815da651393c3b3737359497882` and preserves attribution to @chopratejas in the commit trailer. ## 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 database path and backend from unresolved project scopes. - Gate injection, custom tools, native operations, and learning before backend acquisition. - Pass request context through OpenAI and Anthropic memory-tool paths. - Preserve explicit global/user modes and fail-open model traffic. ## Testing ### October 27 combined-tree refresh Merged upstream `17d38e5699b7d7c69ac6addcd762f39e42dd7f00` into `5f625dba9bccee6b38ec2e9de6c04023c29358e7`. No manual source repairs were needed. - Focused memory, routing, WebSocket, Anthropic, and gateway tests: **308 passed, 2 skipped**. - The two skips cover the macOS non-UTF8 symlink fixture and optional native tool-search extension. - The Python pre-push test list, invoked directly against the built extension: **177 passed, 4 skipped**. - The real-file proof below passed again on this combined tree. - Independent static review found no integration blocker. - Ruff, whitespace, and commit hooks passed, including mypy and the new commit's commitlint check. - Full `make ci-precheck` is not green: Rust 1.99.1 reports `clippy::question_mark` in `crates/headroom-core/src/ccr/backends/in_memory.rs:163`. The same command fails on clean upstream `17d38e56`. - The Python wrapper requires `pip`, absent from this uv environment. Its test list was run directly after `uv sync` built the extension. - The history-wide commitlint target rejects an older commit footer. The new merge commit passed its commit hook. - The push hook was bypassed for these documented limitations. Hosted qualification for the new head remains required. The focused command below was extended with `tests/gateway/test_hook_tool_growth.py tests/gateway/test_native_deferral.py tests/gateway/test_tool_compaction_accounting.py`. ### October 25 validation Current-main refresh: `ba3320e49f202c5880e4ef8e28efe810b78eeb07`, with `3fef15d376d9899f77a28464e2e91f8b99514930` merged as the second parent. - [x] Focused regression tests pass - [x] Ruff lint and format checks pass for all ten PR files - [x] PR diff whitespace check passes - [ ] Type checking rerun for this refresh - [x] Real-file behavior proof completed - [x] Independent review found no remaining integration issue The merge required no manual code repairs. No dependency or API changes were added to the PR. ### Exact commands ```bash uv sync --frozen --extra dev --extra proxy uv run --frozen --extra dev --extra proxy python -m pytest tests/test_memory_storage_router.py tests/test_memory_handler_project_isolation.py tests/test_memory_handler_native_ops.py tests/test_memory_decision.py tests/test_memory_decision_policy.py tests/test_openai_codex_routing.py tests/test_openai_codex_ws_lifecycle.py tests/test_codex_ws_per_frame_memory.py tests/test_openai_responses_traffic_learner.py tests/test_anthropic_buffered_memory_continuation.py tests/test_memory_handler_concurrent_init.py -q uv run --frozen --extra dev --extra proxy ruff check $(git diff --name-only 3fef15d3...HEAD) uv run --frozen --extra dev --extra proxy ruff format --check $(git diff --name-only 3fef15d3...HEAD) git diff --check 3fef15d3...HEAD ``` ### Test Output ```text 259 passed, 1 skipped, 1 warning in 27.35s All checks passed! 10 files already formatted ``` The skip is the non-UTF8 symlink fixture, which this macOS filesystem cannot create. The warning is an existing Starlette/httpx deprecation. The native extension was built from the combined source. These results replace the earlier PR-only test counts for this refresh. ## Real Behavior Proof I ran this on macOS 27.0.1 with Python 3.14.5 at `ba3320e49f202c5880e4ef8e28efe810b78eeb07`. The script uses the actual router and memory handler, with no mocked backend. It creates a temporary SQLite canary table and native-memory file. A request without project identity returns no backend or database path. Context lookup, five custom tools, six native commands, and Anthropic tool dispatch skip memory access. The script checks that the backend remains uninitialized, no project backend is cached, and file paths and byte hashes remain unchanged. It also reads the SQLite canary afterward. This proves rejection before backend access. The canary database is not a populated Headroom memory schema, so this does not demonstrate resolved-project retrieval. No provider request, model-request forwarding, live proxy, service restart, or native Windows/Linux execution was tested by this script. Temporary canary files are removed automatically. ### Reproduce Save the script below as `memory-behavior-proof.py` in the repository root after running the environment setup above. ```bash HEADROOM_WORKSPACE_DIR="$PWD/proof-workspace" HEADROOM_CONFIG_DIR="$PWD/proof-config" HEADROOM_SETTINGS_PATH="$PWD/proof-settings.json" .venv/bin/python -u memory-behavior-proof.py ``` <details> <summary>Executable script</summary> ```python """Check unresolved memory scope using real files and production handlers.""" import asyncio import hashlib import json import os from pathlib import Path import sqlite3 import tempfile os.environ["HEADROOM_BEACON"] = "off" os.environ["HEADROOM_TELEMETRY"] = "off" os.environ["HEADROOM_OFFLINE"] = "1" from headroom.memory.storage_router import MemoryStorageMode, RequestContext from headroom.proxy.memory_handler import MemoryConfig, MemoryHandler, MemoryMode def snapshot(root): return { str(path.relative_to(root)): hashlib.sha256(path.read_bytes()).hexdigest() for path in root.rglob("*") if path.is_file() } async def exercise(root): database = root / "memory.db" with sqlite3.connect(database) as connection: connection.execute("CREATE TABLE canary (content TEXT)") connection.execute("INSERT INTO canary VALUES ('private-existing-memory')") native_dir = root / "native" native_dir.mkdir() (native_dir / "canary.txt").write_text("private-existing-memory\n", encoding="utf-8", newline="\n") handler = MemoryHandler(MemoryConfig( enabled=True, backend="local", db_path=str(database), storage_root=str(root / "projects"), storage_mode=MemoryStorageMode.PROJECT, inject_context=True, mode=MemoryMode.AUTO_TAIL, use_native_tool=True, native_memory_dir=str(native_dir), )) before = snapshot(root) ctx = RequestContext(headers={}, system_prompt="You are helpful.", base_user_id="alice") backend, scope = handler._router.backend_for(ctx) assert backend is None and scope.db_path is None and scope.project_key is None print(f"scope: project_key={scope.project_key}, db_path={scope.db_path}, backend={backend}") context = await handler.search_and_format_context( "alice", [{"role": "user", "content": "Find my private memory"}], ctx, ) assert context is None print(f"context lookup: {context}") expected = {"status": "skipped", "reason": "project_unresolved"} tools = { "memory_save": {"content": "must not persist"}, "memory_search": {"query": "private-existing-memory"}, "memory_update": {"memory_id": "m1", "new_content": "must not update"}, "memory_delete": {"memory_id": "m1"}, "memory_list": {}, } for name, arguments in tools.items(): result = json.loads(await handler._execute_memory_tool(name, arguments, "alice", request_context=ctx)) assert result == expected print(f"{name}: {json.dumps(result, sort_keys=True)}") for command in ("view", "create", "str_replace", "insert", "delete", "rename"): arguments = {"command": command, "path": "/memories/canary.txt", "file_text": "must not persist", "old_str": "private", "new_str": "public", "insert_line": 0, "insert_text": "must not persist", "old_path": "/memories/canary.txt", "new_path": "/memories/renamed.txt"} result = json.loads(await handler._execute_native_memory_tool(arguments, "alice", request_context=ctx)) assert result == expected print(f"native {command}: {json.dumps(result, sort_keys=True)}") results = await handler.handle_memory_tool_calls({"content": [ {"type": "tool_use", "id": "native-create", "name": "memory", "input": {"command": "create", "path": "/memories/canary.txt", "file_text": "must not persist"}}, {"type": "tool_use", "id": "custom-save", "name": "memory_save", "input": {"content": "must not persist"}}, ]}, "alice", "anthropic", request_context=ctx) assert len(results) == 2 and all(json.loads(item["content"]) == expected for item in results) print("Anthropic tool dispatch: both calls skipped") assert not handler.initialized and handler._backend is None and not handler._router._backends assert snapshot(root) == before with sqlite3.connect(database) as connection: assert connection.execute("SELECT content FROM canary").fetchall() == [("private-existing-memory",)] print("backend initialization: false; cached project backends: 0") print("SQLite and native files: byte hashes unchanged; no files added or removed") print("Provider traffic and model-request forwarding: not exercised by this proof") with tempfile.TemporaryDirectory(prefix="headroom-pr2637-proof-") as directory: asyncio.run(exercise(Path(directory))) print("Cleanup: temporary canary files removed") ``` </details> <details> <summary>Captured output</summary> ```text event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' scope: project_key=None, db_path=None, backend=None event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' context lookup: None event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' memory_save: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' memory_search: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' memory_update: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' memory_delete: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' memory_list: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native view: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native create: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native str_replace: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native insert: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native delete: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' native rename: {"reason": "project_unresolved", "status": "skipped"} event=memory_project_unresolved behavior=empty user_id=alice hint='set x-headroom-project-id, x-headroom-cwd, or provide a cwd in the system prompt, or set memory.unresolved_project_fallback=global to opt-in to legacy cross-project GLOBAL pooling (cross-project leak risk).' Anthropic tool dispatch: both calls skipped backend initialization: false; cached project backends: 0 SQLite and native files: byte hashes unchanged; no files added or removed Provider traffic and model-request forwarding: not exercised by this proof Cleanup: temporary canary files removed ``` </details> ## Runtime Rollout Safety - Rollout-managed feature(s): Project-scoped memory injection, tools, native operations, and traffic learning. - Minimum rollout channel: Stable/default. - Stable/default behavior changed: Unresolved project requests skip memory and learning instead of acquiring the global backend. - Kill switch / disable path: Run without `--memory`; use `--no-learn` to disable traffic learning. - Unsafe override required: No. - Qualification impact: Project memory isolation, explicit global/user modes, and fail-open model traffic. - Rollback path: Revert this PR. No schema or data migration is required. ## 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 - [x] I have commented my code, particularly in hard-to-understand areas - [x] Documentation changes are not required for this internal safety fix - [x] 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 did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) Not applicable. ## Additional Notes Waiting follow-up: [project-scoped TrafficLearner](https://github.com/krzkraw/headroom/tree/krz/project-scoped-traffic-learner). It depends on the request-scoped APIs completed here. No schema or data migration is required. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/2637" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: chopratejas <chopratejas@gmail.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
chopratejas
JerrettDavis
parent
33f204406c
commit
957ab1830d
@@ -91,7 +91,7 @@ class ResolvedScope:
|
||||
"""
|
||||
|
||||
mode: MemoryStorageMode
|
||||
db_path: Path
|
||||
db_path: Path | None
|
||||
display_name: str # human-readable label, e.g. project basename
|
||||
project_key: str | None # stable hash, None for USER/GLOBAL
|
||||
|
||||
@@ -304,13 +304,20 @@ class BackendRouter:
|
||||
self._backends: OrderedDict[Path, LocalBackend] = OrderedDict()
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def backend_for(self, ctx: RequestContext) -> tuple[LocalBackend, ResolvedScope]:
|
||||
def backend_for(self, ctx: RequestContext) -> tuple[LocalBackend | None, ResolvedScope]:
|
||||
"""Return the backend + scope metadata to use for this request."""
|
||||
|
||||
scope = self._resolve_scope(ctx)
|
||||
if scope.db_path is None:
|
||||
return None, scope
|
||||
backend = self._get_or_create_backend(scope.db_path)
|
||||
return backend, scope
|
||||
|
||||
def scope_for(self, ctx: RequestContext) -> ResolvedScope:
|
||||
"""Resolve request scope without acquiring or creating a backend."""
|
||||
|
||||
return self._resolve_scope(ctx)
|
||||
|
||||
def _resolve_scope(self, ctx: RequestContext) -> ResolvedScope:
|
||||
mode = self._config.mode
|
||||
|
||||
@@ -363,7 +370,7 @@ class BackendRouter:
|
||||
)
|
||||
return ResolvedScope(
|
||||
mode=MemoryStorageMode.PROJECT,
|
||||
db_path=self._config.global_db_path, # Unused — caller checks project_key.
|
||||
db_path=None,
|
||||
display_name="unresolved (no memory)",
|
||||
project_key=None,
|
||||
)
|
||||
|
||||
@@ -2910,7 +2910,10 @@ class AnthropicHandlerMixin:
|
||||
)
|
||||
|
||||
# Traffic Learner: Extract patterns from inbound tool results
|
||||
if self.traffic_learner:
|
||||
if self.traffic_learner and not (
|
||||
self.memory_handler
|
||||
and self.memory_handler.is_project_unresolved(memory_request_ctx)
|
||||
):
|
||||
try:
|
||||
# Wire backend on first use (lazy init after memory handler is ready)
|
||||
if (
|
||||
|
||||
@@ -1965,6 +1965,7 @@ class OpenAIHandlerMixin:
|
||||
body: dict[str, Any],
|
||||
*,
|
||||
request_id: str,
|
||||
request_context: Any = None,
|
||||
) -> None:
|
||||
"""Feed one Responses HTTP request into the live traffic learner."""
|
||||
traffic_learner = getattr(self, "traffic_learner", None)
|
||||
@@ -1972,6 +1973,8 @@ class OpenAIHandlerMixin:
|
||||
return
|
||||
try:
|
||||
memory_handler = getattr(self, "memory_handler", None)
|
||||
if memory_handler and memory_handler.is_project_unresolved(request_context):
|
||||
return
|
||||
if (
|
||||
traffic_learner._backend is None
|
||||
and memory_handler
|
||||
@@ -2001,6 +2004,7 @@ class OpenAIHandlerMixin:
|
||||
messages: list[dict[str, Any]],
|
||||
*,
|
||||
request_id: str,
|
||||
request_context: Any = None,
|
||||
) -> None:
|
||||
"""Feed one chat/completions request into the live traffic learner.
|
||||
|
||||
@@ -2017,6 +2021,8 @@ class OpenAIHandlerMixin:
|
||||
return
|
||||
try:
|
||||
memory_handler = getattr(self, "memory_handler", None)
|
||||
if memory_handler and memory_handler.is_project_unresolved(request_context):
|
||||
return
|
||||
if (
|
||||
traffic_learner._backend is None
|
||||
and memory_handler
|
||||
@@ -2044,6 +2050,7 @@ class OpenAIHandlerMixin:
|
||||
seen_call_ids: set[str],
|
||||
baseline: bool,
|
||||
request_id: str,
|
||||
request_context: Any = None,
|
||||
) -> None:
|
||||
"""Feed one Codex WS ``response.create`` turn into the traffic learner.
|
||||
|
||||
@@ -2067,6 +2074,8 @@ class OpenAIHandlerMixin:
|
||||
return
|
||||
try:
|
||||
memory_handler = getattr(self, "memory_handler", None)
|
||||
if memory_handler and memory_handler.is_project_unresolved(request_context):
|
||||
return
|
||||
if (
|
||||
traffic_learner._backend is None
|
||||
and memory_handler
|
||||
@@ -3454,8 +3463,6 @@ class OpenAIHandlerMixin:
|
||||
# compression mutates it, mirroring the Responses and Anthropic
|
||||
# ingestion paths. Without this, chat/completions traffic (Copilot CLI,
|
||||
# opencode, OpenAI SDKs) fed nothing to the learner (part of #2060).
|
||||
await self._observe_openai_chat_traffic(original_client_messages, request_id=request_id)
|
||||
|
||||
# Bypass: skip ALL compression for explicit opt-out
|
||||
_bypass = self._headroom_bypass_enabled(request.headers)
|
||||
if _bypass:
|
||||
@@ -3605,6 +3612,12 @@ class OpenAIHandlerMixin:
|
||||
),
|
||||
)
|
||||
|
||||
await self._observe_openai_chat_traffic(
|
||||
original_client_messages,
|
||||
request_id=request_id,
|
||||
request_context=memory_request_ctx,
|
||||
)
|
||||
|
||||
# Canonical memory-injection gate (parallels Anthropic). Pre-
|
||||
# PR-this the inline conjunction at the memory site silently
|
||||
# ignored `x-headroom-bypass: true`, mutating request bytes
|
||||
@@ -6010,12 +6023,6 @@ class OpenAIHandlerMixin:
|
||||
resolved_project_root_override = codex_project_root_override or (
|
||||
str(codex_project.cwd) if codex_project and codex_project.cwd else None
|
||||
)
|
||||
# The shared learner cannot isolate project state. Scoped Codex turns
|
||||
# skip learning until a project-scoped learner is available.
|
||||
if not codex_project_scope_required and (
|
||||
codex_project is None or codex_project.reason == "metadata_missing"
|
||||
):
|
||||
await self._observe_openai_responses_traffic(body, request_id=request_id)
|
||||
memory_client = (
|
||||
"codex"
|
||||
if is_chatgpt_auth
|
||||
@@ -6090,6 +6097,17 @@ class OpenAIHandlerMixin:
|
||||
project_root_override=resolved_project_root_override,
|
||||
)
|
||||
|
||||
# The shared learner cannot isolate project state. Scoped Codex turns
|
||||
# skip learning until a project-scoped learner is available.
|
||||
if not codex_project_scope_required and (
|
||||
codex_project is None or codex_project.reason == "metadata_missing"
|
||||
):
|
||||
await self._observe_openai_responses_traffic(
|
||||
body,
|
||||
request_id=request_id,
|
||||
request_context=memory_request_ctx,
|
||||
)
|
||||
|
||||
# Rate limiting
|
||||
if self.rate_limiter:
|
||||
rate_key = rate_limit_identity(request, headers)
|
||||
@@ -6998,13 +7016,13 @@ class OpenAIHandlerMixin:
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
args = {}
|
||||
|
||||
await self.memory_handler._ensure_initialized()
|
||||
if self.memory_handler._backend:
|
||||
result = await self.memory_handler._execute_memory_tool(
|
||||
name, args, memory_user_id, "openai"
|
||||
)
|
||||
else:
|
||||
result = json.dumps({"error": "Memory backend not initialized"})
|
||||
result = await self.memory_handler._execute_memory_tool(
|
||||
name,
|
||||
args,
|
||||
memory_user_id,
|
||||
"openai",
|
||||
request_context=memory_request_ctx,
|
||||
)
|
||||
|
||||
tool_outputs.append(
|
||||
{
|
||||
@@ -8131,21 +8149,37 @@ class OpenAIHandlerMixin:
|
||||
resolved_project.project_key if ws_turn_project_features_allowed else None
|
||||
)
|
||||
|
||||
if first_project_resolution and ws_turn_learning_allowed:
|
||||
inner_payload = frame_body.get("response", frame_body)
|
||||
if isinstance(inner_payload, dict):
|
||||
await self._observe_openai_ws_response_create(
|
||||
inner_payload,
|
||||
seen_call_ids=ws_learner_seen_call_ids,
|
||||
baseline=True,
|
||||
request_id=request_id,
|
||||
)
|
||||
|
||||
memory_user_id_candidate = (
|
||||
resolve_memory_identity(websocket)
|
||||
if self.memory_handler and ws_turn_project_features_allowed
|
||||
else None
|
||||
)
|
||||
ws_response_body = frame_body.get("response", frame_body)
|
||||
if memory_user_id_candidate is not None and isinstance(ws_response_body, dict):
|
||||
from headroom.memory.storage_router import (
|
||||
RequestContext as _MemRequestContext,
|
||||
)
|
||||
|
||||
memory_request_ctx = _MemRequestContext(
|
||||
headers=dict(ws_headers),
|
||||
system_prompt=str(ws_response_body.get("instructions") or ""),
|
||||
base_user_id=memory_user_id_candidate,
|
||||
project_root_override=resolved_project_root_override,
|
||||
)
|
||||
|
||||
if (
|
||||
first_project_resolution
|
||||
and ws_turn_learning_allowed
|
||||
and isinstance(ws_response_body, dict)
|
||||
):
|
||||
await self._observe_openai_ws_response_create(
|
||||
ws_response_body,
|
||||
seen_call_ids=ws_learner_seen_call_ids,
|
||||
baseline=True,
|
||||
request_id=request_id,
|
||||
request_context=memory_request_ctx,
|
||||
)
|
||||
|
||||
memory_decision = MemoryDecision.decide(
|
||||
headers=ws_headers,
|
||||
memory_handler=(
|
||||
@@ -8162,24 +8196,8 @@ class OpenAIHandlerMixin:
|
||||
|
||||
memory_user_id = memory_user_id_candidate
|
||||
try:
|
||||
# Unwrap response.create envelope to access the response body
|
||||
ws_response_body = frame_body.get("response", frame_body)
|
||||
if not isinstance(ws_response_body, dict):
|
||||
return frame_raw
|
||||
# Per-project memory routing (GH #462). For WS,
|
||||
# ``ws_response_body`` carries ``instructions`` —
|
||||
# that's the system-prompt-equivalent we feed to the
|
||||
# resolver.
|
||||
from headroom.memory.storage_router import (
|
||||
RequestContext as _MemRequestContext,
|
||||
)
|
||||
|
||||
memory_request_ctx = _MemRequestContext(
|
||||
headers=dict(ws_headers),
|
||||
system_prompt=str(ws_response_body.get("instructions") or ""),
|
||||
base_user_id=memory_user_id,
|
||||
project_root_override=resolved_project_root_override,
|
||||
)
|
||||
|
||||
# Debug: log what Codex sends so we can see the full tool list
|
||||
existing_tool_names = [
|
||||
@@ -8761,6 +8779,7 @@ class OpenAIHandlerMixin:
|
||||
seen_call_ids=ws_learner_seen_call_ids,
|
||||
baseline=False,
|
||||
request_id=request_id,
|
||||
request_context=memory_request_ctx,
|
||||
)
|
||||
store_forced = _ensure_chatgpt_responses_store_false(
|
||||
inner_payload,
|
||||
@@ -9627,19 +9646,19 @@ class OpenAIHandlerMixin:
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
fc_args = {}
|
||||
|
||||
await self.memory_handler._ensure_initialized()
|
||||
if not self.memory_handler.is_project_unresolved(
|
||||
response_memory_request_ctx
|
||||
):
|
||||
await self.memory_handler._ensure_initialized()
|
||||
if response_memory_generation != ws_memory_generation:
|
||||
break
|
||||
if self.memory_handler._backend:
|
||||
result = await self.memory_handler._execute_memory_tool(
|
||||
fc_name,
|
||||
fc_args,
|
||||
response_memory_user_id,
|
||||
"openai",
|
||||
request_context=response_memory_request_ctx,
|
||||
)
|
||||
else:
|
||||
result = json.dumps({"error": "backend not ready"})
|
||||
result = await self.memory_handler._execute_memory_tool(
|
||||
fc_name,
|
||||
fc_args,
|
||||
response_memory_user_id,
|
||||
"openai",
|
||||
request_context=response_memory_request_ctx,
|
||||
)
|
||||
|
||||
tool_outputs.append(
|
||||
{
|
||||
|
||||
@@ -185,12 +185,18 @@ class MemoryHandler:
|
||||
self.config = config
|
||||
self.agent_type = agent_type
|
||||
self._backend: LocalBackend | Any = None
|
||||
# Per-project routing for the local backend. Built in
|
||||
# ``_init_backend_locked`` so a single, shared resolver / LRU is
|
||||
# kept on the handler. Qdrant deployments use composite user-id
|
||||
# partitioning instead (see ``_compose_effective_user_id``) — the
|
||||
# router stays None in that case.
|
||||
self._router: BackendRouter | None = None
|
||||
storage_root = (
|
||||
Path(config.storage_root)
|
||||
if config.storage_root
|
||||
else (Path(config.db_path).resolve().parent / "memories")
|
||||
)
|
||||
self._router = BackendRouter(
|
||||
BackendRouterConfig(
|
||||
mode=config.storage_mode,
|
||||
root_dir=storage_root,
|
||||
global_db_path=Path(config.db_path).resolve(),
|
||||
)
|
||||
)
|
||||
self._initialized = False
|
||||
# Async singleflight guard for backend init. Ensures concurrent first
|
||||
# callers land on one init (double-checked pattern inside
|
||||
@@ -410,19 +416,8 @@ class MemoryHandler:
|
||||
# remains the GLOBAL-mode fallback / legacy compatibility
|
||||
# backend; callers that pass a ``RequestContext`` route
|
||||
# through ``self._router`` instead.
|
||||
storage_root = (
|
||||
Path(self.config.storage_root)
|
||||
if self.config.storage_root
|
||||
else (Path(self.config.db_path).resolve().parent / "memories")
|
||||
)
|
||||
global_db_path = Path(self.config.db_path).resolve()
|
||||
router_cfg = BackendRouterConfig(
|
||||
mode=self.config.storage_mode,
|
||||
root_dir=storage_root,
|
||||
global_db_path=global_db_path,
|
||||
backend_config_template=backend_config,
|
||||
)
|
||||
self._router = BackendRouter(router_cfg)
|
||||
self._router._config.backend_config_template = backend_config
|
||||
# Seed the router's LRU with the already-initialized
|
||||
# legacy backend so GLOBAL-mode requests reuse it instead
|
||||
# of opening a second handle to the same file.
|
||||
@@ -431,7 +426,7 @@ class MemoryHandler:
|
||||
logger.info(
|
||||
"event=memory_router_initialized mode=%s root=%s global_db=%s",
|
||||
self.config.storage_mode.value,
|
||||
storage_root,
|
||||
self._router._config.root_dir,
|
||||
global_db_path,
|
||||
)
|
||||
|
||||
@@ -670,7 +665,9 @@ class MemoryHandler:
|
||||
# Non-local backends: derive scope but keep one shared backend
|
||||
# and compose the user_id so the partition lives in the user_id
|
||||
# column instead of in a separate file.
|
||||
scope = self._router._resolve_scope(request_context)
|
||||
scope = self._router.scope_for(request_context)
|
||||
if scope.db_path is None:
|
||||
return None, scope, base_user_id
|
||||
composed = (
|
||||
base_user_id
|
||||
if scope.project_key is None or scope.mode is MemoryStorageMode.GLOBAL
|
||||
@@ -678,6 +675,18 @@ class MemoryHandler:
|
||||
)
|
||||
return self._backend, scope, composed
|
||||
|
||||
def is_project_unresolved(self, request_context: RequestContext | None) -> bool:
|
||||
"""Return whether fail-closed project routing has no usable target."""
|
||||
|
||||
if request_context is None or self._router is None:
|
||||
return False
|
||||
scope = self._router.scope_for(request_context)
|
||||
return (
|
||||
scope.mode is MemoryStorageMode.PROJECT
|
||||
and scope.project_key is None
|
||||
and scope.db_path is None
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _unresolved_project_error(scope: ResolvedScope | None) -> str | None:
|
||||
if (
|
||||
@@ -771,6 +780,8 @@ class MemoryHandler:
|
||||
)
|
||||
return None
|
||||
|
||||
if self.is_project_unresolved(request_context):
|
||||
return None
|
||||
await self._ensure_initialized()
|
||||
if not self._backend:
|
||||
return None
|
||||
@@ -1105,6 +1116,8 @@ your responses, not to drive new actions."""
|
||||
"""
|
||||
tool_calls = self._extract_tool_calls(response, provider)
|
||||
results: list[dict[str, Any]] = []
|
||||
project_unresolved = self.is_project_unresolved(request_context)
|
||||
unresolved_result = json.dumps({"status": "skipped", "reason": "project_unresolved"})
|
||||
|
||||
for tc in tool_calls:
|
||||
# `tc.get("function", {})` returns None for an explicit
|
||||
@@ -1129,8 +1142,14 @@ your responses, not to drive new actions."""
|
||||
input_data = {}
|
||||
|
||||
# Handle native memory tool
|
||||
if tool_name == NATIVE_MEMORY_TOOL_NAME:
|
||||
result_content = await self._execute_native_memory_tool(input_data, user_id)
|
||||
if tool_name in ({NATIVE_MEMORY_TOOL_NAME} | MEMORY_TOOL_NAMES) and project_unresolved:
|
||||
result_content = unresolved_result
|
||||
elif tool_name == NATIVE_MEMORY_TOOL_NAME:
|
||||
result_content = await self._execute_native_memory_tool(
|
||||
input_data,
|
||||
user_id,
|
||||
request_context=request_context,
|
||||
)
|
||||
elif tool_name in MEMORY_TOOL_NAMES:
|
||||
# Custom memory tools need backend
|
||||
await self._ensure_initialized()
|
||||
@@ -1178,7 +1197,12 @@ your responses, not to drive new actions."""
|
||||
request_context: RequestContext | None = None,
|
||||
) -> str:
|
||||
"""Execute a memory tool and return result string."""
|
||||
if self.is_project_unresolved(request_context):
|
||||
return json.dumps({"status": "skipped", "reason": "project_unresolved"})
|
||||
try:
|
||||
await self._ensure_initialized()
|
||||
if self._backend is None:
|
||||
return json.dumps({"status": "error", "error": "Memory backend not initialized"})
|
||||
if tool_name == "memory_save":
|
||||
return await self._execute_save(input_data, user_id, provider, request_context)
|
||||
elif tool_name == "memory_search":
|
||||
@@ -1537,7 +1561,13 @@ your responses, not to drive new actions."""
|
||||
# str_replace → Update memory content
|
||||
# =========================================================================
|
||||
|
||||
async def _execute_native_memory_tool(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _execute_native_memory_tool(
|
||||
self,
|
||||
input_data: dict[str, Any],
|
||||
user_id: str,
|
||||
*,
|
||||
request_context: RequestContext | None = None,
|
||||
) -> str:
|
||||
"""Execute Anthropic's native memory tool with semantic backend.
|
||||
|
||||
This is a TRANSLATION LAYER: Claude thinks it's doing file operations,
|
||||
@@ -1551,24 +1581,40 @@ your responses, not to drive new actions."""
|
||||
- delete: Remove from vector store
|
||||
- rename: Update memory tags/path
|
||||
"""
|
||||
if self.is_project_unresolved(request_context):
|
||||
return json.dumps({"status": "skipped", "reason": "project_unresolved"})
|
||||
|
||||
# Ensure our semantic backend is initialized
|
||||
await self._ensure_initialized()
|
||||
|
||||
command = input_data.get("command", "")
|
||||
|
||||
try:
|
||||
backend, _scope, effective_user_id = self._resolve_for_request(user_id, request_context)
|
||||
if command == "view":
|
||||
return await self._native_view_semantic(input_data, user_id)
|
||||
return await self._native_view_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
elif command == "create":
|
||||
return await self._native_create_semantic(input_data, user_id)
|
||||
return await self._native_create_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
elif command == "str_replace":
|
||||
return await self._native_update_semantic(input_data, user_id)
|
||||
return await self._native_update_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
elif command == "insert":
|
||||
return await self._native_append_semantic(input_data, user_id)
|
||||
return await self._native_append_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
elif command == "delete":
|
||||
return await self._native_delete_semantic(input_data, user_id)
|
||||
return await self._native_delete_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
elif command == "rename":
|
||||
return await self._native_rename_semantic(input_data, user_id)
|
||||
return await self._native_rename_semantic(
|
||||
input_data, effective_user_id, backend=backend
|
||||
)
|
||||
else:
|
||||
return f"Error: Unknown command '{command}'"
|
||||
except Exception as e:
|
||||
@@ -1579,7 +1625,9 @@ your responses, not to drive new actions."""
|
||||
# Semantic Translation Methods (Native Tool → Vector Store)
|
||||
# =========================================================================
|
||||
|
||||
async def _native_view_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_view_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle VIEW command with semantic search capabilities.
|
||||
|
||||
Path patterns:
|
||||
@@ -1602,31 +1650,35 @@ your responses, not to drive new actions."""
|
||||
query = subpath[len("search/") :]
|
||||
if not query:
|
||||
return "Error: Please provide a search query. Example: view /memories/search/food preferences"
|
||||
return await self._semantic_search(query, user_id)
|
||||
return await self._semantic_search(query, user_id, backend=backend)
|
||||
|
||||
# CASE 2: /memories/recent → Recent memories
|
||||
if subpath == "recent":
|
||||
return await self._get_recent_memories(user_id, limit=10)
|
||||
return await self._get_recent_memories(user_id, limit=10, backend=backend)
|
||||
|
||||
# CASE 3: /memories/all → List all (paginated)
|
||||
if subpath == "all":
|
||||
return await self._list_all_memories(user_id, limit=20)
|
||||
return await self._list_all_memories(user_id, limit=20, backend=backend)
|
||||
|
||||
# CASE 4: /memories (root) → Overview with instructions
|
||||
if not subpath or subpath == "":
|
||||
return await self._get_memory_overview(user_id)
|
||||
return await self._get_memory_overview(user_id, backend=backend)
|
||||
|
||||
# CASE 5: /memories/<something> → Search by topic
|
||||
# Treat the path as a search query
|
||||
return await self._semantic_search(subpath.replace("/", " ").replace("_", " "), user_id)
|
||||
return await self._semantic_search(
|
||||
subpath.replace("/", " ").replace("_", " "), user_id, backend=backend
|
||||
)
|
||||
|
||||
async def _semantic_search(self, query: str, user_id: str, top_k: int = 5) -> str:
|
||||
async def _semantic_search(
|
||||
self, query: str, user_id: str, top_k: int = 5, *, backend: Any
|
||||
) -> str:
|
||||
"""Perform semantic search and format results."""
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query=query,
|
||||
user_id=user_id,
|
||||
top_k=top_k,
|
||||
@@ -1657,15 +1709,15 @@ your responses, not to drive new actions."""
|
||||
logger.error(f"Memory: Semantic search failed: {e}")
|
||||
return f"Error searching memories: {e}"
|
||||
|
||||
async def _get_recent_memories(self, user_id: str, limit: int = 10) -> str:
|
||||
async def _get_recent_memories(self, user_id: str, limit: int = 10, *, backend: Any) -> str:
|
||||
"""Get most recent memories."""
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
# Use a generic query to get recent items
|
||||
# Most backends will return by recency when query is broad
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query="recent memories",
|
||||
user_id=user_id,
|
||||
top_k=limit,
|
||||
@@ -1692,14 +1744,14 @@ your responses, not to drive new actions."""
|
||||
logger.error(f"Memory: Get recent failed: {e}")
|
||||
return f"Error getting recent memories: {e}"
|
||||
|
||||
async def _list_all_memories(self, user_id: str, limit: int = 20) -> str:
|
||||
async def _list_all_memories(self, user_id: str, limit: int = 20, *, backend: Any) -> str:
|
||||
"""List all memories (paginated)."""
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
# Get all memories with a broad search
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query="*", # Broad query
|
||||
user_id=user_id,
|
||||
top_k=limit,
|
||||
@@ -1724,14 +1776,14 @@ your responses, not to drive new actions."""
|
||||
logger.error(f"Memory: List all failed: {e}")
|
||||
return f"Error listing memories: {e}"
|
||||
|
||||
async def _get_memory_overview(self, user_id: str) -> str:
|
||||
async def _get_memory_overview(self, user_id: str, *, backend: Any) -> str:
|
||||
"""Get memory directory overview with search instructions."""
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
# Get count of memories
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query="*",
|
||||
user_id=user_id,
|
||||
top_k=100, # Just to get a count
|
||||
@@ -1783,7 +1835,9 @@ To see RECENT: view /memories/recent
|
||||
To SAVE: create /memories/<topic>.txt "content"
|
||||
"""
|
||||
|
||||
async def _native_create_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_create_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle CREATE command - save to semantic vector store."""
|
||||
path = input_data.get("path", "")
|
||||
file_text = input_data.get("file_text", "")
|
||||
@@ -1793,7 +1847,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
if not file_text:
|
||||
return "Error: file_text is required (the memory content)"
|
||||
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
@@ -1806,7 +1860,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
)
|
||||
|
||||
# Save to our semantic backend
|
||||
memory = await self._backend.save_memory(
|
||||
memory = await backend.save_memory(
|
||||
content=file_text,
|
||||
user_id=user_id,
|
||||
importance=0.5,
|
||||
@@ -1820,7 +1874,9 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
logger.error(f"Memory: Semantic create failed: {e}")
|
||||
return f"Error: {e}"
|
||||
|
||||
async def _native_update_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_update_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle STR_REPLACE command - update memory content."""
|
||||
path = input_data.get("path", "")
|
||||
old_str = input_data.get("old_str", "")
|
||||
@@ -1831,12 +1887,12 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
if not old_str:
|
||||
return "Error: old_str is required"
|
||||
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
# Search for memory containing old_str
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query=old_str,
|
||||
user_id=user_id,
|
||||
top_k=5,
|
||||
@@ -1860,15 +1916,15 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
new_content = matching_memory.content.replace(old_str, new_str, 1)
|
||||
|
||||
# Update via delete + create (or update if backend supports it)
|
||||
if hasattr(self._backend, "update_memory"):
|
||||
await self._backend.update_memory(
|
||||
if hasattr(backend, "update_memory"):
|
||||
await backend.update_memory(
|
||||
memory_id=matching_memory.id,
|
||||
new_content=new_content,
|
||||
user_id=user_id,
|
||||
)
|
||||
else:
|
||||
await self._backend.delete_memory(matching_memory.id)
|
||||
await self._backend.save_memory(
|
||||
await backend.delete_memory(matching_memory.id)
|
||||
await backend.save_memory(
|
||||
content=new_content,
|
||||
user_id=user_id,
|
||||
importance=0.5,
|
||||
@@ -1885,7 +1941,9 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
logger.error(f"Memory: Semantic update failed: {e}")
|
||||
return f"Error: {e}"
|
||||
|
||||
async def _native_append_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_append_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle INSERT command - append to memory or create new."""
|
||||
path = input_data.get("path", "")
|
||||
insert_text = input_data.get("insert_text", "")
|
||||
@@ -1896,7 +1954,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
if not insert_text:
|
||||
return "Error: insert_text is required"
|
||||
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
@@ -1904,7 +1962,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
# with the additional context
|
||||
topic = path.replace("/memories/", "").replace("/", "_").replace(".txt", "")
|
||||
|
||||
await self._backend.save_memory(
|
||||
await backend.save_memory(
|
||||
content=insert_text,
|
||||
user_id=user_id,
|
||||
importance=0.5,
|
||||
@@ -1918,14 +1976,16 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
logger.error(f"Memory: Semantic append failed: {e}")
|
||||
return f"Error: {e}"
|
||||
|
||||
async def _native_delete_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_delete_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle DELETE command - remove from vector store."""
|
||||
path = input_data.get("path", "")
|
||||
|
||||
if not path:
|
||||
return "Error: path is required"
|
||||
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
@@ -1937,7 +1997,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
.replace(".txt", "")
|
||||
)
|
||||
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query=topic,
|
||||
user_id=user_id,
|
||||
top_k=10,
|
||||
@@ -1952,7 +2012,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
# Check if metadata matches path
|
||||
metadata = getattr(r.memory, "metadata", {}) or {}
|
||||
if metadata.get("virtual_path") == path or r.score > 0.8:
|
||||
await self._backend.delete_memory(r.memory.id)
|
||||
await backend.delete_memory(r.memory.id)
|
||||
deleted_count += 1
|
||||
|
||||
if deleted_count == 0:
|
||||
@@ -1967,7 +2027,9 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
logger.error(f"Memory: Semantic delete failed: {e}")
|
||||
return f"Error: {e}"
|
||||
|
||||
async def _native_rename_semantic(self, input_data: dict[str, Any], user_id: str) -> str:
|
||||
async def _native_rename_semantic(
|
||||
self, input_data: dict[str, Any], user_id: str, *, backend: Any
|
||||
) -> str:
|
||||
"""Handle RENAME command - update memory path/topic."""
|
||||
old_path = input_data.get("old_path", "")
|
||||
new_path = input_data.get("new_path", "")
|
||||
@@ -1977,7 +2039,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
if not new_path:
|
||||
return "Error: new_path is required"
|
||||
|
||||
if not self._backend:
|
||||
if not backend:
|
||||
return "Error: Memory backend not initialized"
|
||||
|
||||
try:
|
||||
@@ -1989,7 +2051,7 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
.replace(".txt", "")
|
||||
)
|
||||
|
||||
results = await self._backend.search_memories(
|
||||
results = await backend.search_memories(
|
||||
query=old_topic,
|
||||
user_id=user_id,
|
||||
top_k=10,
|
||||
@@ -2006,8 +2068,8 @@ To SAVE: create /memories/<topic>.txt "content"
|
||||
metadata = getattr(r.memory, "metadata", {}) or {}
|
||||
if metadata.get("virtual_path") == old_path or r.score > 0.8:
|
||||
# Delete old and create with new path
|
||||
await self._backend.delete_memory(r.memory.id)
|
||||
await self._backend.save_memory(
|
||||
await backend.delete_memory(r.memory.id)
|
||||
await backend.save_memory(
|
||||
content=r.memory.content,
|
||||
user_id=user_id,
|
||||
importance=getattr(r.memory, "importance", 0.5),
|
||||
|
||||
@@ -27,6 +27,7 @@ def handler(tmp_path: Path) -> MemoryHandler:
|
||||
class FakeBackend:
|
||||
def __init__(self) -> None:
|
||||
self.search_results: list[object] = []
|
||||
self.searched: list[dict[str, object]] = []
|
||||
self.saved: list[dict[str, object]] = []
|
||||
self.updated: list[dict[str, object]] = []
|
||||
self.deleted: list[str] = []
|
||||
@@ -34,6 +35,7 @@ class FakeBackend:
|
||||
self.raise_on: str | None = None
|
||||
|
||||
async def search_memories(self, **kwargs): # noqa: ANN003
|
||||
self.searched.append(kwargs)
|
||||
if self.raise_on == "search":
|
||||
raise RuntimeError("search failed")
|
||||
return self.search_results
|
||||
@@ -96,11 +98,13 @@ async def test_execute_native_memory_tool_dispatches_and_wraps_errors(
|
||||
async def fake_ensure_initialized() -> None:
|
||||
return None
|
||||
|
||||
async def fake_view(input_data, user_id): # noqa: ANN001
|
||||
async def fake_view(input_data, user_id, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
called.append(("view", input_data, user_id))
|
||||
return "viewed"
|
||||
|
||||
async def fake_create(input_data, user_id): # noqa: ANN001
|
||||
async def fake_create(input_data, user_id, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
called.append(("create", input_data, user_id))
|
||||
return "created"
|
||||
|
||||
@@ -115,7 +119,7 @@ async def test_execute_native_memory_tool_dispatches_and_wraps_errors(
|
||||
== "Error: Unknown command 'bad'"
|
||||
)
|
||||
|
||||
async def boom(input_data, user_id): # noqa: ANN001
|
||||
async def boom(input_data, user_id, *, backend): # noqa: ANN001
|
||||
raise RuntimeError("oops")
|
||||
|
||||
monkeypatch.setattr(handler, "_native_view_semantic", boom)
|
||||
@@ -138,44 +142,87 @@ async def test_semantic_search_recent_all_and_overview(handler: MemoryHandler) -
|
||||
make_result("m2", "Bob prefers ramen", score=0.83),
|
||||
]
|
||||
|
||||
search_text = await handler._semantic_search("pizza", "u1")
|
||||
search_text = await handler._semantic_search("pizza", "u1", backend=handler._backend)
|
||||
assert "Found 2 memories matching 'pizza'" in search_text
|
||||
assert "[91% match] Alice likes pizza and pasta" in search_text
|
||||
assert "Related: Alice, pizza" in search_text
|
||||
|
||||
recent_text = await handler._get_recent_memories("u1", limit=2)
|
||||
recent_text = await handler._get_recent_memories("u1", limit=2, backend=handler._backend)
|
||||
assert "Recent memories:" in recent_text
|
||||
assert "(2026-04-22)" in recent_text
|
||||
|
||||
all_text = await handler._list_all_memories("u1", limit=2)
|
||||
all_text = await handler._list_all_memories("u1", limit=2, backend=handler._backend)
|
||||
assert "Showing up to 2 memories:" in all_text
|
||||
assert "Showing first 2" in all_text
|
||||
|
||||
overview = await handler._get_memory_overview("u1")
|
||||
overview = await handler._get_memory_overview("u1", backend=handler._backend)
|
||||
assert "Memory System (2 memories stored)" in overview
|
||||
assert "view /memories/search/<your query>" in overview
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_semantic_helpers_handle_empty_backend_and_errors(handler: MemoryHandler) -> None:
|
||||
assert await handler._semantic_search("x", "u1") == "Error: Memory backend not initialized"
|
||||
assert await handler._get_recent_memories("u1") == "Error: Memory backend not initialized"
|
||||
assert await handler._list_all_memories("u1") == "Error: Memory backend not initialized"
|
||||
assert await handler._get_memory_overview("u1") == "Error: Memory backend not initialized"
|
||||
assert (
|
||||
await handler._semantic_search(
|
||||
"x",
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: Memory backend not initialized"
|
||||
)
|
||||
assert (
|
||||
await handler._get_recent_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: Memory backend not initialized"
|
||||
)
|
||||
assert (
|
||||
await handler._list_all_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: Memory backend not initialized"
|
||||
)
|
||||
assert (
|
||||
await handler._get_memory_overview(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: Memory backend not initialized"
|
||||
)
|
||||
|
||||
backend = FakeBackend()
|
||||
handler._backend = backend
|
||||
assert "No memories found matching 'x'" in await handler._semantic_search("x", "u1")
|
||||
assert "No memories stored yet." in await handler._list_all_memories("u1")
|
||||
assert "No memories stored yet." in await handler._get_recent_memories("u1")
|
||||
assert "No memories found matching 'x'" in await handler._semantic_search(
|
||||
"x",
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "No memories stored yet." in await handler._list_all_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "No memories stored yet." in await handler._get_recent_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
|
||||
backend.raise_on = "search"
|
||||
assert "Error searching memories: search failed" == await handler._semantic_search("x", "u1")
|
||||
assert "Error getting recent memories: search failed" == await handler._get_recent_memories(
|
||||
"u1"
|
||||
assert "Error searching memories: search failed" == await handler._semantic_search(
|
||||
"x",
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "Error listing memories: search failed" == await handler._list_all_memories("u1")
|
||||
overview = await handler._get_memory_overview("u1")
|
||||
assert "Error getting recent memories: search failed" == await handler._get_recent_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "Error listing memories: search failed" == await handler._list_all_memories(
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
overview = await handler._get_memory_overview("u1", backend=handler._backend)
|
||||
assert "📁 Memory System" in overview
|
||||
assert "To SEARCH memories" in overview
|
||||
|
||||
@@ -186,19 +233,23 @@ async def test_native_view_semantic_routes_paths(
|
||||
) -> None:
|
||||
seen: list[tuple[str, object]] = []
|
||||
|
||||
async def fake_search(query, user_id, top_k=5): # noqa: ANN001
|
||||
async def fake_search(query, user_id, top_k=5, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
seen.append(("search", query))
|
||||
return "search-result"
|
||||
|
||||
async def fake_recent(user_id, limit=10): # noqa: ANN001
|
||||
async def fake_recent(user_id, limit=10, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
seen.append(("recent", limit))
|
||||
return "recent-result"
|
||||
|
||||
async def fake_all(user_id, limit=20): # noqa: ANN001
|
||||
async def fake_all(user_id, limit=20, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
seen.append(("all", limit))
|
||||
return "all-result"
|
||||
|
||||
async def fake_overview(user_id): # noqa: ANN001
|
||||
async def fake_overview(user_id, *, backend): # noqa: ANN001
|
||||
assert backend is handler._backend
|
||||
seen.append(("overview", user_id))
|
||||
return "overview-result"
|
||||
|
||||
@@ -208,21 +259,52 @@ async def test_native_view_semantic_routes_paths(
|
||||
monkeypatch.setattr(handler, "_get_memory_overview", fake_overview)
|
||||
|
||||
assert (
|
||||
await handler._native_view_semantic({"path": "/memories/search/pizza"}, "u1")
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories/search/pizza"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "search-result"
|
||||
)
|
||||
assert (
|
||||
await handler._native_view_semantic({"path": "/memories/recent"}, "u1") == "recent-result"
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories/recent"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "recent-result"
|
||||
)
|
||||
assert await handler._native_view_semantic({"path": "/memories/all"}, "u1") == "all-result"
|
||||
assert await handler._native_view_semantic({"path": "/memories"}, "u1") == "overview-result"
|
||||
assert (
|
||||
await handler._native_view_semantic({"path": "/memories/work/projects"}, "u1")
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories/all"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "all-result"
|
||||
)
|
||||
assert (
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "overview-result"
|
||||
)
|
||||
assert (
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories/work/projects"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "search-result"
|
||||
)
|
||||
assert (await handler._native_view_semantic({"path": "/memories/search/"}, "u1")).startswith(
|
||||
"Error: Please provide a search query"
|
||||
)
|
||||
assert (
|
||||
await handler._native_view_semantic(
|
||||
{"path": "/memories/search/"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: Please provide a search query")
|
||||
assert seen == [
|
||||
("search", "pizza"),
|
||||
("recent", 10),
|
||||
@@ -237,15 +319,27 @@ async def test_native_semantic_create_append_delete_and_rename(handler: MemoryHa
|
||||
backend = FakeBackend()
|
||||
handler._backend = backend
|
||||
|
||||
assert await handler._native_create_semantic({}, "u1") == "Error: path is required"
|
||||
assert (
|
||||
await handler._native_create_semantic({"path": "/memories/topic.txt"}, "u1")
|
||||
await handler._native_create_semantic(
|
||||
{},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_create_semantic(
|
||||
{"path": "/memories/topic.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: file_text is required (the memory content)"
|
||||
)
|
||||
|
||||
created = await handler._native_create_semantic(
|
||||
{"path": "/memories/topic.txt", "file_text": "prefers pizza"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert created == "File created successfully at: /memories/topic.txt"
|
||||
assert backend.saved[-1]["metadata"] == {
|
||||
@@ -253,14 +347,26 @@ async def test_native_semantic_create_append_delete_and_rename(handler: MemoryHa
|
||||
"topic": "topic",
|
||||
}
|
||||
|
||||
assert await handler._native_append_semantic({}, "u1") == "Error: path is required"
|
||||
assert (
|
||||
await handler._native_append_semantic({"path": "/memories/topic.txt"}, "u1")
|
||||
await handler._native_append_semantic(
|
||||
{},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_append_semantic(
|
||||
{"path": "/memories/topic.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: insert_text is required"
|
||||
)
|
||||
appended = await handler._native_append_semantic(
|
||||
{"path": "/memories/topic.txt", "insert_text": "and pasta"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert appended == "The file /memories/topic.txt has been edited."
|
||||
assert backend.saved[-1]["metadata"]["appended"] is True
|
||||
@@ -271,7 +377,11 @@ async def test_native_semantic_create_append_delete_and_rename(handler: MemoryHa
|
||||
),
|
||||
make_result("m2", "prefers pasta", metadata={}, score=0.91),
|
||||
]
|
||||
deleted = await handler._native_delete_semantic({"path": "/memories/topic.txt"}, "u1")
|
||||
deleted = await handler._native_delete_semantic(
|
||||
{"path": "/memories/topic.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert deleted == "Successfully deleted /memories/topic.txt"
|
||||
assert backend.deleted == ["m1", "m2"]
|
||||
|
||||
@@ -283,6 +393,7 @@ async def test_native_semantic_create_append_delete_and_rename(handler: MemoryHa
|
||||
renamed = await handler._native_rename_semantic(
|
||||
{"old_path": "/memories/old.txt", "new_path": "/memories/new/topic.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert renamed == "Successfully renamed /memories/old.txt to /memories/new/topic.txt"
|
||||
assert backend.deleted[-1] == "m3"
|
||||
@@ -299,9 +410,20 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
backend = FakeBackend()
|
||||
handler._backend = backend
|
||||
|
||||
assert await handler._native_update_semantic({}, "u1") == "Error: path is required"
|
||||
assert (
|
||||
await handler._native_update_semantic({"path": "/memories/t.txt"}, "u1")
|
||||
await handler._native_update_semantic(
|
||||
{},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_update_semantic(
|
||||
{"path": "/memories/t.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: old_str is required"
|
||||
)
|
||||
|
||||
@@ -311,6 +433,7 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
multi = await handler._native_update_semantic(
|
||||
{"path": "/memories/t.txt", "old_str": "hello", "new_str": "bye"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "Multiple occurrences of old_str `hello`" in multi
|
||||
|
||||
@@ -320,6 +443,7 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
edited = await handler._native_update_semantic(
|
||||
{"path": "/memories/t.txt", "old_str": "hello", "new_str": "bye"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "The memory file has been edited." in edited
|
||||
assert backend.updated[-1]["new_content"] == "bye world"
|
||||
@@ -349,6 +473,7 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
fallback = await handler._native_update_semantic(
|
||||
{"path": "/memories/t.txt", "old_str": "alpha", "new_str": "omega"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
assert "The memory file has been edited." in fallback
|
||||
assert no_update_backend.deleted[-1] == "m2"
|
||||
@@ -356,20 +481,43 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
|
||||
backend = FakeBackend()
|
||||
handler._backend = backend
|
||||
assert await handler._native_delete_semantic({}, "u1") == "Error: path is required"
|
||||
assert await handler._native_rename_semantic({}, "u1") == "Error: old_path is required"
|
||||
assert (
|
||||
await handler._native_rename_semantic({"old_path": "/memories/a.txt"}, "u1")
|
||||
await handler._native_delete_semantic(
|
||||
{},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_rename_semantic(
|
||||
{},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: old_path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_rename_semantic(
|
||||
{"old_path": "/memories/a.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: new_path is required"
|
||||
)
|
||||
assert (
|
||||
await handler._native_delete_semantic({"path": "/memories/x.txt"}, "u1")
|
||||
await handler._native_delete_semantic(
|
||||
{"path": "/memories/x.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: The path /memories/x.txt does not exist"
|
||||
)
|
||||
assert (
|
||||
await handler._native_rename_semantic(
|
||||
{"old_path": "/memories/x.txt", "new_path": "/memories/y.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: The path /memories/x.txt does not exist"
|
||||
)
|
||||
@@ -378,13 +526,18 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
make_result("m9", "content", metadata={"virtual_path": "/memories/other.txt"}, score=0.1)
|
||||
]
|
||||
assert (
|
||||
await handler._native_delete_semantic({"path": "/memories/x.txt"}, "u1")
|
||||
await handler._native_delete_semantic(
|
||||
{"path": "/memories/x.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: The path /memories/x.txt does not exist"
|
||||
)
|
||||
assert (
|
||||
await handler._native_rename_semantic(
|
||||
{"old_path": "/memories/x.txt", "new_path": "/memories/y.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "Error: The path /memories/x.txt does not exist"
|
||||
)
|
||||
@@ -394,37 +547,277 @@ async def test_native_semantic_update_delete_rename_and_backend_errors(
|
||||
await handler._native_create_semantic(
|
||||
{"path": "/memories/topic.txt", "file_text": "content"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
== "File created successfully at: /memories/topic.txt"
|
||||
)
|
||||
backend.raise_on = "save"
|
||||
assert (
|
||||
await handler._native_create_semantic(
|
||||
{"path": "/memories/topic.txt", "file_text": "content"}, "u1"
|
||||
{"path": "/memories/topic.txt", "file_text": "content"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: ")
|
||||
assert (
|
||||
await handler._native_append_semantic(
|
||||
{"path": "/memories/topic.txt", "insert_text": "content"}, "u1"
|
||||
{"path": "/memories/topic.txt", "insert_text": "content"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: ")
|
||||
backend.raise_on = "search"
|
||||
assert (
|
||||
await handler._native_update_semantic(
|
||||
{"path": "/memories/t.txt", "old_str": "a", "new_str": "b"}, "u1"
|
||||
{"path": "/memories/t.txt", "old_str": "a", "new_str": "b"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: ")
|
||||
assert (
|
||||
await handler._native_delete_semantic(
|
||||
{"path": "/memories/x.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: ")
|
||||
assert (await handler._native_delete_semantic({"path": "/memories/x.txt"}, "u1")).startswith(
|
||||
"Error: "
|
||||
)
|
||||
assert (
|
||||
await handler._native_rename_semantic(
|
||||
{"old_path": "/memories/x.txt", "new_path": "/memories/y.txt"},
|
||||
"u1",
|
||||
backend=handler._backend,
|
||||
)
|
||||
).startswith("Error: ")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"command",
|
||||
[
|
||||
pytest.param(
|
||||
{"command": "create", "path": "/memories/topic.txt", "file_text": "new"},
|
||||
id="create",
|
||||
),
|
||||
pytest.param({"command": "view", "path": "/memories/search/hello"}, id="search"),
|
||||
pytest.param({"command": "view", "path": "/memories/recent"}, id="recent"),
|
||||
pytest.param({"command": "view", "path": "/memories/all"}, id="all"),
|
||||
pytest.param({"command": "view", "path": "/memories"}, id="overview"),
|
||||
pytest.param({"command": "view", "path": "/memories/topic.txt"}, id="topic"),
|
||||
pytest.param(
|
||||
{
|
||||
"command": "str_replace",
|
||||
"path": "/memories/topic.txt",
|
||||
"old_str": "hello",
|
||||
"new_str": "bye",
|
||||
},
|
||||
id="update",
|
||||
),
|
||||
pytest.param(
|
||||
{"command": "insert", "path": "/memories/topic.txt", "insert_text": "more"},
|
||||
id="insert",
|
||||
),
|
||||
pytest.param({"command": "delete", "path": "/memories/topic.txt"}, id="delete"),
|
||||
pytest.param(
|
||||
{
|
||||
"command": "rename",
|
||||
"old_path": "/memories/topic.txt",
|
||||
"new_path": "/memories/new.txt",
|
||||
},
|
||||
id="rename",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_native_commands_use_only_the_resolved_project_backend(
|
||||
handler: MemoryHandler, monkeypatch: pytest.MonkeyPatch, command: dict
|
||||
) -> None:
|
||||
from headroom.memory.storage_router import RequestContext
|
||||
|
||||
global_backend = FakeBackend()
|
||||
handler._backend = global_backend
|
||||
handler._initialized = True
|
||||
contexts = [
|
||||
RequestContext(
|
||||
headers={"x-headroom-project-id": project},
|
||||
system_prompt="",
|
||||
base_user_id="same-user",
|
||||
)
|
||||
for project in ("project-one", "project-two")
|
||||
]
|
||||
backends = [FakeBackend(), FakeBackend()]
|
||||
scopes = [handler._router.scope_for(context) for context in contexts]
|
||||
routed = []
|
||||
|
||||
def _get_or_create_backend(db_path):
|
||||
routed.append(db_path)
|
||||
return backends[[scope.db_path for scope in scopes].index(db_path)]
|
||||
|
||||
monkeypatch.setattr(handler._router, "_get_or_create_backend", _get_or_create_backend)
|
||||
operation_fields = ("searched", "saved", "updated", "deleted", "accessed")
|
||||
for index, backend in enumerate(backends):
|
||||
backend.search_results = [
|
||||
make_result(
|
||||
f"memory-{index}",
|
||||
f"hello project-{index}",
|
||||
metadata={"virtual_path": "/memories/topic.txt"},
|
||||
)
|
||||
]
|
||||
|
||||
for index, (context, backend) in enumerate(zip(contexts, backends)):
|
||||
other_backend = backends[1 - index]
|
||||
other_counts = [len(getattr(other_backend, field)) for field in operation_fields]
|
||||
result = await handler._execute_native_memory_tool(
|
||||
command, "same-user", request_context=context
|
||||
)
|
||||
assert not result.startswith("Error:"), result
|
||||
assert routed == [scope.db_path for scope in scopes[: index + 1]]
|
||||
assert [len(getattr(other_backend, field)) for field in operation_fields] == other_counts
|
||||
assert handler._backend is global_backend
|
||||
assert all(getattr(global_backend, field) == [] for field in operation_fields)
|
||||
assert all(
|
||||
operation["user_id"] == "same-user"
|
||||
for operation in backend.searched + backend.saved + backend.updated
|
||||
)
|
||||
if command["command"] == "view":
|
||||
assert f"hello project-{index}" in result
|
||||
assert f"hello project-{1 - index}" not in result
|
||||
assert backend.searched
|
||||
elif command["command"] in {"create", "insert"}:
|
||||
assert len(backend.saved) == 1
|
||||
elif command["command"] == "str_replace":
|
||||
assert backend.updated == [
|
||||
{
|
||||
"memory_id": f"memory-{index}",
|
||||
"new_content": f"bye project-{index}",
|
||||
"user_id": "same-user",
|
||||
}
|
||||
]
|
||||
elif command["command"] == "delete":
|
||||
assert backend.deleted == [f"memory-{index}"]
|
||||
else:
|
||||
assert backend.deleted == [f"memory-{index}"]
|
||||
assert backend.saved[-1]["content"] == f"hello project-{index}"
|
||||
assert backend.saved[-1]["metadata"]["virtual_path"] == "/memories/new.txt"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("scope_mode", ["no-context", "global", "user", "composite"])
|
||||
async def test_native_dispatch_preserves_legacy_scope_and_composite_identity(
|
||||
handler: MemoryHandler, monkeypatch: pytest.MonkeyPatch, scope_mode: str
|
||||
) -> None:
|
||||
from headroom.memory.storage_router import MemoryStorageMode, RequestContext
|
||||
|
||||
global_backend = FakeBackend()
|
||||
selected_backend = FakeBackend()
|
||||
handler._backend = global_backend
|
||||
handler._initialized = True
|
||||
context = None
|
||||
expected_backend = global_backend
|
||||
expected_user_id = "same-user"
|
||||
if scope_mode != "no-context":
|
||||
handler._router._config.mode = {
|
||||
"global": MemoryStorageMode.GLOBAL,
|
||||
"user": MemoryStorageMode.USER,
|
||||
"composite": MemoryStorageMode.PROJECT,
|
||||
}[scope_mode]
|
||||
context = RequestContext(
|
||||
headers={"x-headroom-project-id": "selected-project"},
|
||||
system_prompt="",
|
||||
base_user_id="same-user",
|
||||
)
|
||||
if scope_mode == "composite":
|
||||
handler.config.backend = "qdrant-neo4j"
|
||||
scope = handler._router.scope_for(context)
|
||||
expected_user_id = f"same-user::{scope.project_key}"
|
||||
else:
|
||||
expected_backend = selected_backend
|
||||
monkeypatch.setattr(
|
||||
handler._router, "_get_or_create_backend", lambda db_path: selected_backend
|
||||
)
|
||||
|
||||
result = await handler._execute_native_memory_tool(
|
||||
{"command": "create", "path": "/memories/topic.txt", "file_text": "content"},
|
||||
"same-user",
|
||||
request_context=context,
|
||||
)
|
||||
assert result == "File created successfully at: /memories/topic.txt"
|
||||
assert len(expected_backend.saved) == 1
|
||||
assert expected_backend.saved[0]["user_id"] == expected_user_id
|
||||
other_backend = selected_backend if expected_backend is global_backend else global_backend
|
||||
assert other_backend.saved == []
|
||||
assert handler._backend is global_backend
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_overlapping_native_rename_keeps_its_selected_backend(
|
||||
handler: MemoryHandler, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
from headroom.memory.storage_router import RequestContext
|
||||
|
||||
global_backend = FakeBackend()
|
||||
handler._backend = global_backend
|
||||
handler._initialized = True
|
||||
contexts = [
|
||||
RequestContext(
|
||||
headers={"x-headroom-project-id": project}, system_prompt="", base_user_id=user_id
|
||||
)
|
||||
for project, user_id in (("project-one", "user-one"), ("project-two", "user-two"))
|
||||
]
|
||||
backends = [FakeBackend(), FakeBackend()]
|
||||
scopes = [handler._router.scope_for(context) for context in contexts]
|
||||
monkeypatch.setattr(
|
||||
handler._router,
|
||||
"_get_or_create_backend",
|
||||
lambda db_path: backends[[scope.db_path for scope in scopes].index(db_path)],
|
||||
)
|
||||
backends[0].search_results = [
|
||||
make_result(
|
||||
"private-one", "private content", metadata={"virtual_path": "/memories/old.txt"}
|
||||
)
|
||||
]
|
||||
search_started = asyncio.Event()
|
||||
second_request_finished = asyncio.Event()
|
||||
search = backends[0].search_memories
|
||||
|
||||
async def _blocked_search(**kwargs):
|
||||
search_started.set()
|
||||
await second_request_finished.wait()
|
||||
return await search(**kwargs)
|
||||
|
||||
monkeypatch.setattr(backends[0], "search_memories", _blocked_search)
|
||||
first_request = asyncio.create_task(
|
||||
handler._execute_native_memory_tool(
|
||||
{"command": "rename", "old_path": "/memories/old.txt", "new_path": "/memories/new.txt"},
|
||||
"user-one",
|
||||
request_context=contexts[0],
|
||||
)
|
||||
)
|
||||
try:
|
||||
await asyncio.wait_for(search_started.wait(), timeout=2.0)
|
||||
second_result = await handler._execute_native_memory_tool(
|
||||
{"command": "create", "path": "/memories/two.txt", "file_text": "second content"},
|
||||
"user-two",
|
||||
request_context=contexts[1],
|
||||
)
|
||||
assert second_result == "File created successfully at: /memories/two.txt"
|
||||
second_request_finished.set()
|
||||
first_result = await asyncio.wait_for(first_request, timeout=2.0)
|
||||
finally:
|
||||
second_request_finished.set()
|
||||
first_request.cancel()
|
||||
await asyncio.gather(first_request, return_exceptions=True)
|
||||
|
||||
assert first_result == "Successfully renamed /memories/old.txt to /memories/new.txt"
|
||||
assert backends[0].deleted == ["private-one"]
|
||||
assert backends[0].saved[0]["content"] == "private content"
|
||||
assert backends[0].saved[0]["user_id"] == "user-one"
|
||||
assert backends[0].searched[0]["user_id"] == "user-one"
|
||||
assert len(backends[1].saved) == 1
|
||||
assert backends[1].saved[0]["user_id"] == "user-two"
|
||||
assert backends[1].deleted == []
|
||||
assert handler._backend is global_backend
|
||||
assert global_backend.searched == global_backend.saved == global_backend.deleted == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_search_update_delete_and_handler_status(
|
||||
handler: MemoryHandler, monkeypatch: pytest.MonkeyPatch
|
||||
@@ -858,7 +1251,7 @@ async def test_search_and_format_context_and_handle_memory_tool_calls(
|
||||
): # noqa: ANN001
|
||||
return f"ran:{tool_name}:{user_id}:{provider}:{input_data}"
|
||||
|
||||
async def fake_execute_native(input_data, user_id): # noqa: ANN001
|
||||
async def fake_execute_native(input_data, user_id, request_context=None): # noqa: ANN001
|
||||
return f"native:{user_id}:{input_data}"
|
||||
|
||||
monkeypatch.setattr(handler, "_ensure_initialized", fake_ensure_initialized)
|
||||
|
||||
@@ -268,7 +268,8 @@ def test_user_mode_partitions_by_user_id(tmp_path: Path) -> None:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_unresolved_project_returns_no_context(tmp_path: Path) -> None:
|
||||
@pytest.mark.parametrize("preinitialized", [False, True])
|
||||
def test_unresolved_project_returns_no_context(tmp_path: Path, preinitialized: bool) -> None:
|
||||
"""No project signals + PROJECT mode + empty fallback → no memory injection."""
|
||||
cfg = MemoryConfig(
|
||||
enabled=True,
|
||||
@@ -283,7 +284,9 @@ def test_unresolved_project_returns_no_context(tmp_path: Path) -> None:
|
||||
handler = MemoryHandler(cfg, agent_type="test")
|
||||
|
||||
async def run() -> None:
|
||||
await handler._ensure_initialized()
|
||||
if preinitialized:
|
||||
await handler._ensure_initialized()
|
||||
backend_count = len(_FakeBackend.instances)
|
||||
|
||||
# Request with NO project-resolution signal: no header, no cwd,
|
||||
# no parseable system-prompt cwd: line.
|
||||
@@ -309,6 +312,9 @@ def test_unresolved_project_returns_no_context(tmp_path: Path) -> None:
|
||||
msgs = [{"role": "user", "content": "Just a friendly hello"}]
|
||||
context = await handler.search_and_format_context("alice", msgs, ctx_unresolved)
|
||||
|
||||
assert len(_FakeBackend.instances) == backend_count
|
||||
assert handler.initialized is preinitialized
|
||||
|
||||
# Fail-closed: no memory injected even though backends have data.
|
||||
assert context is None, (
|
||||
"Unresolved project in PROJECT mode must skip injection — "
|
||||
@@ -342,7 +348,6 @@ def test_unresolved_project_memory_tools_fail_closed(
|
||||
handler = MemoryHandler(cfg, agent_type="test")
|
||||
|
||||
async def run() -> None:
|
||||
await handler._ensure_initialized()
|
||||
ctx = sr_mod.RequestContext(
|
||||
headers={}, system_prompt="You are helpful.", base_user_id="alice"
|
||||
)
|
||||
@@ -350,7 +355,57 @@ def test_unresolved_project_memory_tools_fail_closed(
|
||||
tool_name, input_data, "alice", request_context=ctx
|
||||
)
|
||||
payload = __import__("json").loads(result)
|
||||
assert payload["status"] == "error"
|
||||
assert "project" in payload["error"].lower()
|
||||
assert payload == {"status": "skipped", "reason": "project_unresolved"}
|
||||
assert _FakeBackend.instances == []
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_unresolved_project_native_memory_tool_fails_closed(tmp_path: Path) -> None:
|
||||
handler = MemoryHandler(
|
||||
MemoryConfig(
|
||||
enabled=True,
|
||||
backend="local",
|
||||
db_path=str(tmp_path / "memory.db"),
|
||||
storage_mode=sr_mod.MemoryStorageMode.PROJECT,
|
||||
use_native_tool=True,
|
||||
native_memory_dir=str(tmp_path / "native"),
|
||||
),
|
||||
agent_type="test",
|
||||
)
|
||||
ctx = sr_mod.RequestContext(headers={}, system_prompt="You are helpful.", base_user_id="alice")
|
||||
|
||||
results = asyncio.run(
|
||||
handler.handle_memory_tool_calls(
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": "native-create",
|
||||
"name": "memory",
|
||||
"input": {
|
||||
"command": "create",
|
||||
"path": "/memories/canary.txt",
|
||||
"file_text": "must not persist",
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": "custom-save",
|
||||
"name": "memory_save",
|
||||
"input": {"content": "must not persist"},
|
||||
},
|
||||
]
|
||||
},
|
||||
"alice",
|
||||
"anthropic",
|
||||
request_context=ctx,
|
||||
)
|
||||
)
|
||||
|
||||
assert all(
|
||||
__import__("json").loads(result["content"])
|
||||
== {"status": "skipped", "reason": "project_unresolved"}
|
||||
for result in results
|
||||
)
|
||||
assert _FakeBackend.instances == []
|
||||
|
||||
@@ -456,11 +456,14 @@ def test_router_project_mode_unresolved_fails_closed_by_default(
|
||||
"""
|
||||
router = _make_router(tmp_path, MemoryStorageMode.PROJECT, monkeypatch)
|
||||
|
||||
_, scope = router.backend_for(_ctx(system_prompt="no env block"))
|
||||
backend, scope = router.backend_for(_ctx(system_prompt="no env block"))
|
||||
# Fail-closed signal: PROJECT mode preserved, project_key is None.
|
||||
assert scope.mode is MemoryStorageMode.PROJECT
|
||||
assert scope.project_key is None
|
||||
assert scope.db_path is None
|
||||
assert scope.display_name == "unresolved (no memory)"
|
||||
assert backend is None
|
||||
assert router.open_backends() == []
|
||||
|
||||
|
||||
def test_router_project_mode_unresolved_global_fallback_when_opted_in(
|
||||
|
||||
@@ -12,6 +12,7 @@ import httpx
|
||||
import pytest
|
||||
from fastapi import Request
|
||||
|
||||
from headroom.memory.storage_router import RequestContext
|
||||
from headroom.proxy.handlers.openai import (
|
||||
OpenAIHandlerMixin,
|
||||
_is_allowed_websocket_origin,
|
||||
@@ -312,7 +313,11 @@ class _MemoryContinuationHandler(_MemoryToolsOnlyHandler):
|
||||
args: dict,
|
||||
user_id: str,
|
||||
provider: str,
|
||||
*,
|
||||
request_context: RequestContext | None = None,
|
||||
) -> str:
|
||||
assert request_context is not None
|
||||
assert request_context.base_user_id == user_id
|
||||
assert (name, args, user_id, provider) == (
|
||||
"memory_search",
|
||||
{},
|
||||
|
||||
@@ -15,7 +15,7 @@ import math
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock, PropertyMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -97,6 +97,9 @@ class _MemoryWsHandler:
|
||||
async def _ensure_initialized(self) -> None:
|
||||
self._backend = True
|
||||
|
||||
def is_project_unresolved(self, request_context) -> bool:
|
||||
return False
|
||||
|
||||
async def _execute_memory_tool(
|
||||
self,
|
||||
name: str,
|
||||
@@ -106,6 +109,9 @@ class _MemoryWsHandler:
|
||||
*,
|
||||
request_context=None,
|
||||
) -> str:
|
||||
await self._ensure_initialized()
|
||||
if not self._backend:
|
||||
return '{"error": "backend not ready"}'
|
||||
assert (name, args, user_id, provider) == (
|
||||
"memory_search",
|
||||
{},
|
||||
@@ -2085,6 +2091,249 @@ async def test_ws_late_memory_call_after_streamed_message_passes_through():
|
||||
assert executed == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("preinitialized", [False, True])
|
||||
async def test_ws_unresolved_project_memory_tool_skips_backend_initialization(
|
||||
tmp_path, monkeypatch, preinitialized
|
||||
):
|
||||
from headroom.memory.storage_router import MemoryStorageMode
|
||||
from headroom.proxy.memory_handler import MemoryConfig, MemoryHandler
|
||||
|
||||
function_call = {
|
||||
"type": "function_call",
|
||||
"id": "fc-1",
|
||||
"call_id": "call-1",
|
||||
"name": "memory_search",
|
||||
"arguments": '{"query": "private memory"}',
|
||||
}
|
||||
upstream = _FakeUpstream(
|
||||
[
|
||||
json.dumps({"type": "response.created", "response": {"id": "r-1"}}),
|
||||
json.dumps({"type": "response.output_item.added", "item": function_call}),
|
||||
json.dumps({"type": "response.output_item.done", "item": function_call}),
|
||||
json.dumps({"type": "response.completed", "response": {"id": "r-1"}}),
|
||||
]
|
||||
)
|
||||
first_frame = json.loads(_first_frame())
|
||||
first_frame["response"]["tools"] = [
|
||||
{"type": "function", "name": "client_tool", "parameters": {"type": "object"}}
|
||||
]
|
||||
client_ws = _FakeWebSocket(
|
||||
frames=[json.dumps(first_frame)],
|
||||
headers={
|
||||
"authorization": "Bearer test",
|
||||
"user-agent": "generic-ws-client",
|
||||
"x-headroom-user-id": "user-1",
|
||||
},
|
||||
hold_after_initial=True,
|
||||
)
|
||||
handler = _DummyOpenAIHandler()
|
||||
memory_handler = MemoryHandler(
|
||||
MemoryConfig(
|
||||
enabled=True,
|
||||
inject_context=False,
|
||||
inject_tools=True,
|
||||
storage_mode=MemoryStorageMode.PROJECT,
|
||||
db_path=str(tmp_path / "global.db"),
|
||||
)
|
||||
)
|
||||
handler.memory_handler = memory_handler
|
||||
global_backend = object() if preinitialized else None
|
||||
memory_handler._backend = global_backend
|
||||
memory_handler._initialized = preinitialized
|
||||
learner = MagicMock()
|
||||
learner._backend = None
|
||||
handler.traffic_learner = learner
|
||||
initialized = []
|
||||
executed = []
|
||||
execute_memory_tool = memory_handler._execute_memory_tool
|
||||
|
||||
async def _unexpected_initialization():
|
||||
initialized.append(True)
|
||||
raise AssertionError("Unresolved project initialized the global memory backend")
|
||||
|
||||
async def _execute_memory_tool(name, args, user_id, provider, *, request_context=None):
|
||||
executed.append((name, args, user_id, provider, request_context))
|
||||
return await execute_memory_tool(
|
||||
name, args, user_id, provider, request_context=request_context
|
||||
)
|
||||
|
||||
monkeypatch.setattr(memory_handler, "_ensure_initialized", _unexpected_initialization)
|
||||
monkeypatch.setattr(memory_handler, "_execute_memory_tool", _execute_memory_tool)
|
||||
monkeypatch.setenv("HEADROOM_MEMORY_INJECTION_MODE", "live_zone_tail")
|
||||
with (
|
||||
patch.dict(sys.modules, {"websockets": _make_fake_websockets_module(upstream)}),
|
||||
patch.object(MemoryHandler, "backend", new_callable=PropertyMock) as backend_access,
|
||||
):
|
||||
backend_access.return_value = global_backend
|
||||
await asyncio.wait_for(handler.handle_openai_responses_ws(client_ws), timeout=2.0)
|
||||
|
||||
backend_access.assert_not_called()
|
||||
learner.set_backend.assert_not_called()
|
||||
learner.extract_tool_results_from_messages.assert_not_called()
|
||||
assert learner._backend is None
|
||||
assert initialized == []
|
||||
assert memory_handler._backend is global_backend
|
||||
assert not (tmp_path / "global.db").exists()
|
||||
assert len(executed) == 1
|
||||
name, args, user_id, provider, request_context = executed[0]
|
||||
assert (name, args, user_id, provider) == (
|
||||
"memory_search",
|
||||
{"query": "private memory"},
|
||||
"user-1",
|
||||
"openai",
|
||||
)
|
||||
assert memory_handler.is_project_unresolved(request_context)
|
||||
assert request_context.project_root_override is None
|
||||
assert len(upstream.sent) == 2
|
||||
assert "memory_search" in [
|
||||
tool["name"] for tool in json.loads(upstream.sent[0])["response"]["tools"]
|
||||
]
|
||||
continuation = json.loads(upstream.sent[1])["response"]
|
||||
assert continuation["input"][-2] == function_call
|
||||
assert continuation["input"][-1]["call_id"] == "call-1"
|
||||
assert json.loads(continuation["input"][-1]["output"]) == {
|
||||
"status": "skipped",
|
||||
"reason": "project_unresolved",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("change_at", ["before-completion", "during-initialization"])
|
||||
async def test_ws_workspace_and_user_change_discards_stale_memory_response(
|
||||
tmp_path, monkeypatch, change_at
|
||||
):
|
||||
from headroom.memory.storage_router import MemoryStorageMode
|
||||
from headroom.proxy.memory_handler import MemoryConfig, MemoryHandler
|
||||
|
||||
function_call = {
|
||||
"type": "function_call",
|
||||
"id": "fc-1",
|
||||
"call_id": "call-1",
|
||||
"name": "memory_search",
|
||||
"arguments": "{}",
|
||||
}
|
||||
events = [
|
||||
json.dumps({"type": "response.created", "response": {"id": "r-1"}}),
|
||||
json.dumps({"type": "response.output_item.added", "item": function_call}),
|
||||
json.dumps({"type": "response.output_item.done", "item": function_call}),
|
||||
json.dumps({"type": "response.completed", "response": {"id": "r-1"}}),
|
||||
]
|
||||
workspaces = [tmp_path / "workspace-one", tmp_path / "workspace-two"]
|
||||
for workspace in workspaces:
|
||||
workspace.mkdir()
|
||||
frames = [
|
||||
json.dumps(
|
||||
{
|
||||
"type": "response.create",
|
||||
"response": {
|
||||
"model": "gpt-5.4",
|
||||
"input": workspace.name,
|
||||
"instructions": f"Working directory: {workspace}",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "client_tool",
|
||||
"parameters": {"type": "object"},
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
)
|
||||
for workspace in workspaces
|
||||
]
|
||||
client_ws = _FakeWebSocket(
|
||||
frames=[frames[0]],
|
||||
headers={
|
||||
"authorization": "Bearer test",
|
||||
"user-agent": "generic-ws-client",
|
||||
"x-headroom-user-id": "user-1",
|
||||
},
|
||||
hold_after_initial=True,
|
||||
)
|
||||
upstream = _FakeUpstream([])
|
||||
handler = _DummyOpenAIHandler()
|
||||
memory_handler = MemoryHandler(
|
||||
MemoryConfig(
|
||||
enabled=True,
|
||||
inject_context=False,
|
||||
inject_tools=True,
|
||||
storage_mode=MemoryStorageMode.PROJECT,
|
||||
db_path=str(tmp_path / "global.db"),
|
||||
)
|
||||
)
|
||||
handler.memory_handler = memory_handler
|
||||
change_ready = asyncio.Event()
|
||||
changed_frame_sent = asyncio.Event()
|
||||
initialized = []
|
||||
executed = []
|
||||
identities = []
|
||||
resolve_identity = openai_module.resolve_memory_identity
|
||||
receive_text = client_ws.receive_text
|
||||
send = upstream.send
|
||||
execute_memory_tool = memory_handler._execute_memory_tool
|
||||
|
||||
def _resolve_identity(websocket):
|
||||
user_id = resolve_identity(websocket)
|
||||
identities.append(user_id)
|
||||
return user_id
|
||||
|
||||
async def _receive_text():
|
||||
if client_ws._frames:
|
||||
return await receive_text()
|
||||
await change_ready.wait()
|
||||
client_ws.headers["x-headroom-user-id"] = "user-2"
|
||||
monkeypatch.setattr(client_ws, "receive_text", receive_text)
|
||||
return frames[1]
|
||||
|
||||
async def _send(payload):
|
||||
await send(payload)
|
||||
if json.loads(payload)["response"]["input"] == workspaces[1].name:
|
||||
changed_frame_sent.set()
|
||||
|
||||
async def _iter():
|
||||
for event in events[:-1]:
|
||||
yield event
|
||||
if change_at == "before-completion":
|
||||
change_ready.set()
|
||||
await changed_frame_sent.wait()
|
||||
yield events[-1]
|
||||
|
||||
async def _ensure_initialized():
|
||||
initialized.append(True)
|
||||
change_ready.set()
|
||||
await changed_frame_sent.wait()
|
||||
|
||||
async def _execute_memory_tool(name, args, user_id, provider, *, request_context=None):
|
||||
executed.append((name, args, user_id, provider, request_context))
|
||||
return await execute_memory_tool(
|
||||
name, args, user_id, provider, request_context=request_context
|
||||
)
|
||||
|
||||
monkeypatch.setattr(openai_module, "resolve_memory_identity", _resolve_identity)
|
||||
monkeypatch.setattr(client_ws, "receive_text", _receive_text)
|
||||
monkeypatch.setattr(upstream, "send", _send)
|
||||
monkeypatch.setattr(upstream, "_iter", _iter)
|
||||
monkeypatch.setattr(memory_handler, "_ensure_initialized", _ensure_initialized)
|
||||
monkeypatch.setattr(memory_handler, "_execute_memory_tool", _execute_memory_tool)
|
||||
monkeypatch.setenv("HEADROOM_MEMORY_INJECTION_MODE", "live_zone_tail")
|
||||
with patch.dict(sys.modules, {"websockets": _make_fake_websockets_module(upstream)}):
|
||||
await asyncio.wait_for(handler.handle_openai_responses_ws(client_ws), timeout=2.0)
|
||||
|
||||
assert changed_frame_sent.is_set()
|
||||
assert identities == ["user-1", "user-2"]
|
||||
assert initialized == ([True] if change_at == "during-initialization" else [])
|
||||
assert executed == []
|
||||
assert len(upstream.sent) == 2
|
||||
assert [json.loads(frame)["response"]["input"] for frame in upstream.sent] == [
|
||||
workspace.name for workspace in workspaces
|
||||
]
|
||||
assert json.loads(upstream.sent[1])["response"]["instructions"].startswith(
|
||||
f"Working directory: {workspaces[1]}"
|
||||
)
|
||||
assert client_ws.sent_text == events
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ws_memory_continuation_handles_invalid_item_arguments_and_unavailable_backend():
|
||||
function_call = {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -50,6 +51,24 @@ class _RecordingLearner:
|
||||
self.message_batches.append(messages)
|
||||
|
||||
|
||||
def test_responses_traffic_skips_unresolved_project() -> None:
|
||||
handler = OpenAIHandlerMixin()
|
||||
learner = _RecordingLearner()
|
||||
handler.traffic_learner = learner
|
||||
handler.memory_handler = SimpleNamespace(is_project_unresolved=lambda _ctx: True)
|
||||
|
||||
asyncio.run(
|
||||
handler._observe_openai_responses_traffic(
|
||||
{"input": _responses_input()},
|
||||
request_id="r",
|
||||
request_context=object(),
|
||||
)
|
||||
)
|
||||
|
||||
assert learner.tool_results == []
|
||||
assert learner.message_batches == []
|
||||
|
||||
|
||||
def _responses_input() -> list[dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user