headroom/headroom/proxy/tool_injection_config.py
JD Davis 0f846e5a8f
refactor(proxy): extract tool injection config (#2010)
## Description

Extracts memory tool-injection operator config parsing from
`headroom.proxy.helpers` into a focused config policy module. Existing
helper functions and imports remain available while the environment
parsing is now directly testable.

Closes #

## Type of Change

- [ ] 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
- [x] Code refactoring (no functional changes)

## Changes Made

- Added `headroom.proxy.tool_injection_config` for
`HEADROOM_TOOL_INJECTION_STICKY` and
`HEADROOM_TOOL_TRACKER_MAX_SESSIONS` parsing.
- Updated `helpers.get_tool_injection_sticky_mode` and
`helpers.get_tool_tracker_max_sessions` to delegate to the config module
while preserving existing import paths.
- Added direct tests for defaults, valid values, invalid values, and
helper wrapper compatibility.

## Testing

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

### Test Output

```text
python -m pytest tests/test_tool_injection_config.py tests/test_memory_tool_session_sticky.py tests/test_issue_728_empty_tools_injection.py
46 passed in 0.53s

python -m ruff check .
All checks passed!

python -m ruff format --check .
1078 files already formatted

python -m mypy headroom --ignore-missing-imports
Success: no issues found in 415 source files

gitleaks protect --staged --no-banner --redact
no leaks found
```

## Real Behavior Proof

- Environment: Windows, Python 3.13.13, clean worktree from
`headroomlabs/main` at `cb38f793`.
- Exact command / steps: Ran targeted tool-injection config, memory
session sticky, and empty-tool regression tests plus ruff, ruff-format,
mypy, and staged gitleaks scan.
- Observed result: All targeted tests and local gates passed; staged
secret scan found no leaks.
- Not tested: Full Docker/native wrapper CI locally; covered by
repository CI.

## 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
- [ ] 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

Documentation and changelog updates are not applicable for this internal
refactor. The push reported existing default-branch Dependabot
vulnerabilities; this PR's staged gitleaks scan passed and CI security
checks are expected to validate the branch.
2026-07-11 00:03:15 -05:00

43 lines
1.4 KiB
Python

"""Operator configuration policy for proxy tool injection."""
from __future__ import annotations
import os
from typing import Literal, cast
TOOL_INJECTION_STICKY_ENV = "HEADROOM_TOOL_INJECTION_STICKY"
ToolInjectionStickyMode = Literal["enabled", "disabled"]
TOOL_INJECTION_STICKY_DEFAULT: ToolInjectionStickyMode = "enabled"
TOOL_TRACKER_MAX_SESSIONS_ENV = "HEADROOM_TOOL_TRACKER_MAX_SESSIONS"
TOOL_TRACKER_MAX_SESSIONS_DEFAULT = 1000
def get_tool_injection_sticky_mode() -> ToolInjectionStickyMode:
"""Return the active memory-tool stickiness mode."""
raw = os.environ.get(TOOL_INJECTION_STICKY_ENV, "").strip().lower()
if not raw:
return TOOL_INJECTION_STICKY_DEFAULT
if raw in ("enabled", "disabled"):
return cast(ToolInjectionStickyMode, raw)
raise ValueError(
f"Invalid {TOOL_INJECTION_STICKY_ENV}={raw!r}; expected 'enabled' or 'disabled'"
)
def get_tool_tracker_max_sessions() -> int:
"""Return the LRU bound for memory tool session tracking."""
raw = os.environ.get(TOOL_TRACKER_MAX_SESSIONS_ENV, "").strip()
if not raw:
return TOOL_TRACKER_MAX_SESSIONS_DEFAULT
try:
value = int(raw)
except ValueError as exc:
raise ValueError(
f"Invalid {TOOL_TRACKER_MAX_SESSIONS_ENV}={raw!r}; expected positive int"
) from exc
if value <= 0:
raise ValueError(f"Invalid {TOOL_TRACKER_MAX_SESSIONS_ENV}={raw!r}; expected positive int")
return value