mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-10 14:27:00 -04:00
## Description Close the passive-memory observability loop by recording access for context rows that survive the final injection budget and tagging requests where context is actually appended. Closes #2211 ## 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 - Track only memory IDs retained after ranking, similarity filtering, entry limits, and final text truncation. - Call optional backend `record_access` with stable de-duplication and fail-open error handling. - Extend structured injection logging to stamp `memory_injected=true` when injected bytes are positive. - Thread request tags through successful Anthropic, OpenAI Chat, OpenAI Responses, Gemini, and Codex WebSocket injection sites. - Add a static contract test that all current successful handler injection logs pass tags. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ uv run --with pytest --with pytest-asyncio --with numpy --with fastapi pytest \ tests/test_memory_handler_native_ops.py \ tests/test_memory_auto_tail.py \ tests/test_memory_handler_project_isolation.py \ tests/test_memory_injection_logging.py -q 51 passed $ uv run --with ruff ruff check <touched Python files> All checks passed! $ git diff --check (no output) ``` ## Real Behavior Proof - Environment: Python 3.13 with synthetic backend and handler fixtures - Exact command / steps: run the focused test set above - Observed result: only IDs present after the final text budget are access-recorded; access-write failures remain fail-open; positive injection logs stamp `memory_injected=true`; all six current successful injection call sites pass tags - Not tested: live provider requests, full repository suite, third-party backends without `record_access` ## Review Readiness - [x] I have performed a self-review - [ ] 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 - [ ] I have made corresponding changes to the documentation - [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 - [ ] I have updated the CHANGELOG.md if applicable ## Screenshots (if applicable) N/A ## Additional Notes Access accounting is intentionally best-effort: unsupported backends and write failures do not delay or fail the upstream model request. Documentation and changelog changes are not needed for this internal observability fix. Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""Tests for memory-injection request tags."""
|
|
|
|
import ast
|
|
from pathlib import Path
|
|
|
|
from headroom.proxy.helpers import log_memory_injection
|
|
|
|
HANDLER_FILES = [
|
|
Path("headroom/proxy/handlers/anthropic.py"),
|
|
Path("headroom/proxy/handlers/openai.py"),
|
|
Path("headroom/proxy/handlers/gemini.py"),
|
|
]
|
|
|
|
|
|
def test_log_memory_injection_marks_only_successful_injection() -> None:
|
|
tags: dict[str, str] = {}
|
|
|
|
log_memory_injection(
|
|
request_id="hr_test_memory",
|
|
session_id=None,
|
|
decision="no_eligible_user_turn",
|
|
bytes_injected=0,
|
|
tags=tags,
|
|
)
|
|
assert "memory_injected" not in tags
|
|
|
|
log_memory_injection(
|
|
request_id="hr_test_memory",
|
|
session_id=None,
|
|
decision="injected_live_zone_tail",
|
|
bytes_injected=42,
|
|
tags=tags,
|
|
)
|
|
assert tags["memory_injected"] == "true"
|
|
|
|
|
|
def test_successful_handler_injection_logs_pass_tags() -> None:
|
|
missing: list[tuple[Path, int]] = []
|
|
successful_sites = 0
|
|
|
|
for file_path in HANDLER_FILES:
|
|
tree = ast.parse(file_path.read_text(encoding="utf-8"), filename=str(file_path))
|
|
for node in ast.walk(tree):
|
|
if not isinstance(node, ast.Call):
|
|
continue
|
|
if not isinstance(node.func, ast.Name) or node.func.id != "log_memory_injection":
|
|
continue
|
|
kwargs = {kw.arg: kw.value for kw in node.keywords if kw.arg is not None}
|
|
bytes_injected = kwargs.get("bytes_injected")
|
|
if isinstance(bytes_injected, ast.Constant) and bytes_injected.value == 0:
|
|
continue
|
|
successful_sites += 1
|
|
if "tags" not in kwargs:
|
|
missing.append((file_path, node.lineno))
|
|
|
|
assert successful_sites >= 6, "Expected all current provider injection sites"
|
|
assert not missing, "Successful memory injections missing tags: " + ", ".join(
|
|
f"{path}:{line}" for path, line in missing
|
|
)
|