mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
## Description `headroom install status` could report `Status: stopped` next to `Healthy: yes`, while `headroom doctor` reported the same proxy as running (#3072, bug 2). The commands used independent process views: `install status` trusted PID/container bookkeeping for `Status`, while both commands used the live HTTP health endpoints to establish health. This change treats a successful `/readyz` probe as authoritative evidence that the deployment is running. If the proxy is not ready, `runtime_status` still distinguishes a running-but-unhealthy process from a stopped process. The PR also adds a focused macOS pytest lane for CLI and install code, closing the Darwin test-coverage gap described in #3065 without duplicating the full four-shard Linux suite. Bug 1 from #3072 (`HEADROOM_PORT` being ignored by `install apply` and `deploy`) was already fixed on `main` by #3085 and is not reimplemented here. Closes #3072 Closes #3065 ## 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 - Updated `headroom/cli/install.py` so `install_status` performs one readiness probe and reports `Status: running` whenever `/readyz` is healthy. - Kept `runtime_status` as the fallback when the proxy is not ready, preserving the distinction between running-but-unhealthy and stopped. - Added regression coverage for the formerly contradictory `Status: stopped` plus `Healthy: yes` output. - Added regression coverage for the not-ready case where process status must still come from `runtime_status`. - Added a `test-macos` CI job that runs the CLI/install pytest subset on `macos-latest`. ## 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 $ uv run --frozen --extra dev pytest tests/test_cli/test_install_cli.py -q ============================= test session starts ============================== platform darwin -- Python 3.11.15, pytest-9.0.3, pluggy-1.6.0 collected 42 items tests/test_cli/test_install_cli.py ..................................... [ 88%] ..... [100%] ============================== 42 passed in 1.79s ============================== $ uv run --frozen ruff check headroom/cli/install.py tests/test_cli/test_install_cli.py All checks passed! $ uv run --frozen ruff format --check headroom/cli/install.py tests/test_cli/test_install_cli.py 2 files already formatted $ uv run --frozen mypy headroom/cli/install.py --ignore-missing-imports Success: no issues found in 1 source file ``` ## Real Behavior Proof - Environment: macOS (Apple Silicon), CPython 3.11.15 via `uv`, pytest 9.0.3. - Exact command / steps: ran `uv run --frozen --extra dev pytest tests/test_cli/test_install_cli.py -q`. The new regression test invokes `headroom install status` through Click's `CliRunner` with `/readyz` returning healthy while PID/runtime bookkeeping returns `stopped`. - Observed result: the command exits successfully and prints `Status: running` with `Healthy: yes`; `Status: stopped` is absent. A second test verifies that when readiness is false, a live runtime still prints `Status: running` with `Healthy: no`. - Not tested: an end-to-end persistent-service installation with a real OS supervisor or Docker daemon. The new macOS CI job itself will be exercised by GitHub Actions rather than locally. ## Runtime Rollout Safety - Rollout-managed feature(s): none; this changes diagnostic display logic and CI coverage only. - Minimum rollout channel: stable/default patch release. - Stable/default behavior changed: yes; `install status` now reports a ready deployment as running even if PID/container bookkeeping says stopped. Unready deployments retain the existing runtime-status behavior. - Kill switch / disable path: no runtime flag; revert this PR if the readiness precedence causes an unforeseen regression. - Unsafe override required: none. - Qualification impact: `tests/test_cli/test_install_cli.py` and the new `test-macos` CI job must remain green. - Rollback path: revert commit `f3ae269b`; no persisted state, schema, manifest, or configuration migration is involved. ## 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 - [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; this is terminal status output and CI configuration. ## Additional Notes - Documentation was not changed because no command, option, configuration contract, or documented workflow changed; this corrects contradictory diagnostic output. - #2787 was also reviewed as an urgent author-reported issue, but it already has active implementation PR #2992. This PR deliberately avoids duplicating or conflicting with that work. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3141" 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 --> ### Maintainer current-main requalification Exact head `f550bf1d05873b20b773d935aa7cb93025b63262`, upstream main `2d3ee51846435ef5b320eea4806af14e602b0d8c`. Re-reviewed latest integration head3c11d4a96and current discussion; clean integration with upstream/main2d3ee51846. Install status still treats a ready reachable proxy as running, preserving runtime-status fallback when unhealthy. All 160 install-status/release/version/MCP tests pass; no PR-specific production-code blocker found in scope. Downloaded the latest failing native macOS job 126368233730 from run 41765101507: four persistent-wrap recovery tests still failed because _Manifest/TaskManifest lacked runtime_kind. Repaired test doubles with the Python runtime declaration and explicitly mocked the readiness boundary those tests control through health probes; real ownership classification remains exercised. Parametrized the four affected recovery cases across Darwin/Linux/Windows by replacing only runtime module's sys reference, without global platform emulation. All 83 persistent-wrap tests pass, preserving matching deployment recovery, bound-socket recovery, healthy reuse, and task warning coverage. No production recovery logic changed. Changed-file Ruff check/format and whitespace pass. These local platform variants are not native macOS validation; exact-head hosted macOS lane must qualify before approval. Existing native extension supports Python startup, not a fresh native build. Maintainers handle CI; nothing merged. Exact-head hosted checks are being handled by maintainers. No new finding in the reviewed scope; formal approval awaits exact-head hosted qualification; nothing merged. --------- Co-authored-by: vishal veerareddy <vishalveera.reddy@servicenow.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2081 lines
77 KiB
Python
2081 lines
77 KiB
Python
from __future__ import annotations
|
|
|
|
import errno
|
|
from types import SimpleNamespace
|
|
|
|
import click
|
|
import pytest
|
|
|
|
import headroom.cli.wrap as wrap_cli
|
|
import headroom.install.health as install_health
|
|
import headroom.install.runtime as install_runtime
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _no_attached_wrappers(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Default: no other wrap clients attached, so restart paths are hermetic.
|
|
|
|
The ephemeral restart guards consult ``_live_proxy_clients``; without this, a
|
|
real ``headroom wrap`` session on the dev's machine could make these tests
|
|
flaky. Individual tests override this to simulate attached wrappers.
|
|
"""
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [])
|
|
# These recovery tests control readiness through the health probe; process
|
|
# identity and launchd ownership have their own runtime-level coverage.
|
|
monkeypatch.setattr(
|
|
install_runtime,
|
|
"runtime_ready",
|
|
lambda manifest: install_health.probe_ready(manifest.health_url),
|
|
)
|
|
for name in (
|
|
"ANTHROPIC_TARGET_API_URL",
|
|
"OPENAI_TARGET_API_URL",
|
|
"GEMINI_TARGET_API_URL",
|
|
"CLOUDCODE_TARGET_API_URL",
|
|
"VERTEX_TARGET_API_URL",
|
|
"AUGMENT_TARGET_API_URL",
|
|
"FACTORY_TARGET_API_URL",
|
|
"HEADROOM_BACKEND",
|
|
):
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
class _Manifest:
|
|
profile = "default"
|
|
preset = "persistent-service"
|
|
supervisor_kind = "service"
|
|
health_url = "http://127.0.0.1:8787/readyz"
|
|
runtime_kind = "python"
|
|
|
|
|
|
@pytest.fixture(params=["darwin", "linux", "win32"])
|
|
def runtime_platform(monkeypatch: pytest.MonkeyPatch, request) -> None:
|
|
# Replace only this module's sys reference, never the process-wide object.
|
|
monkeypatch.setattr(install_runtime, "sys", SimpleNamespace(platform=request.param))
|
|
|
|
|
|
def test_ensure_proxy_recovers_matching_persistent_deployment(
|
|
monkeypatch, runtime_platform
|
|
) -> None:
|
|
calls: list[str] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: False)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(
|
|
"headroom.install.supervisors.start_supervisor",
|
|
lambda manifest: calls.append(f"start:{manifest.profile}"),
|
|
)
|
|
monkeypatch.setattr(
|
|
"headroom.install.runtime.wait_ready",
|
|
lambda manifest, timeout_seconds=45, require_identity=False: True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("ephemeral proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == ["start:default"]
|
|
|
|
|
|
def test_ensure_proxy_recovers_persistent_deployment_when_socket_is_bound(
|
|
monkeypatch, runtime_platform
|
|
) -> None:
|
|
calls: list[str] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
# A bare TCP accept no longer proves the listener is Headroom;
|
|
# identify it via /health so the recovery path still applies.
|
|
monkeypatch.setattr(
|
|
wrap_cli, "_query_proxy_health", lambda port: {"version": "0.0.0-test", "config": {}}
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(
|
|
"headroom.install.supervisors.start_supervisor",
|
|
lambda manifest: calls.append(f"start:{manifest.profile}"),
|
|
)
|
|
monkeypatch.setattr(
|
|
"headroom.install.runtime.wait_ready",
|
|
lambda manifest, timeout_seconds=45, require_identity=False: True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_query_proxy_health",
|
|
lambda port: {"version": wrap_cli._HEADROOM_VERSION, "config": {"backend": "anthropic"}},
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("matching recovered proxy should be reused")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == ["start:default"]
|
|
|
|
|
|
def test_ensure_proxy_rejects_unhealthy_persistent_deployment(monkeypatch) -> None:
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
# Headroom-identified listener: a foreign squatter would fall
|
|
# through to a fresh port instead of raising.
|
|
monkeypatch.setattr(
|
|
wrap_cli, "_query_proxy_health", lambda port: {"version": "0.0.0-test", "config": {}}
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: False)
|
|
|
|
try:
|
|
wrap_cli._ensure_proxy(8787, False)
|
|
except click.ClickException as exc:
|
|
assert "is not healthy" in str(exc)
|
|
else:
|
|
raise AssertionError("expected unhealthy persistent deployment to raise")
|
|
|
|
|
|
def test_ensure_proxy_falls_back_when_persistent_manifest_is_stale(monkeypatch) -> None:
|
|
calls: list[str] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: False)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: False)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_find_available_port", lambda port, **kw: port)
|
|
monkeypatch.setattr(wrap_cli, "_start_proxy", lambda *args, **kwargs: calls.append("start"))
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == ["start"]
|
|
|
|
|
|
def test_ensure_proxy_reports_unbindable_port_before_starting_subprocess(monkeypatch) -> None:
|
|
calls: list[str] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: False)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda port, **kw: (_ for _ in ()).throw(
|
|
OSError(errno.EADDRNOTAVAIL, "address not available")
|
|
),
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_start_proxy", lambda *args, **kwargs: calls.append("start"))
|
|
|
|
try:
|
|
wrap_cli._ensure_proxy(8787, False, agent_type="cursor")
|
|
except click.ClickException as exc:
|
|
message = str(exc)
|
|
else:
|
|
raise AssertionError("expected unbindable port to raise before starting proxy")
|
|
|
|
assert "Port 8787 is unavailable" in message
|
|
assert calls == []
|
|
|
|
|
|
def test_ensure_proxy_restarts_idle_stale_persistent_deployment(monkeypatch) -> None:
|
|
calls: list[str] = []
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": 12345},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda manifest, port: calls.append(f"restart:{manifest.profile}:{port}") or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("ephemeral proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == ["restart:default:8787"]
|
|
|
|
|
|
def test_ensure_proxy_restarts_stale_proxy_from_dev_build(monkeypatch) -> None:
|
|
"""A source (-dev) CLI still restarts a stale proxy: the -dev marker is
|
|
display-only and must not disable a real version-mismatch restart."""
|
|
calls: list[str] = []
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": 12345},
|
|
}
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "0.32.0-dev")
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda manifest, port: calls.append(f"restart:{manifest.profile}:{port}") or True,
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == ["restart:default:8787"]
|
|
|
|
|
|
def test_ensure_proxy_leaves_active_stale_persistent_deployment_running(monkeypatch) -> None:
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 1, "active_relay_tasks": 2}},
|
|
"config": {"pid": 12345},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("active deployment should not restart")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_defers_persistent_restart_when_http_wrapper_attached(
|
|
monkeypatch,
|
|
) -> None:
|
|
"""A stale persistent proxy is left running while marker-tracked HTTP
|
|
wrappers are attached, even when WebSocket session count is zero."""
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": 12345},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [999])
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("attached persistent proxy should not restart")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_find_persistent_manifest_prefers_default_profile(monkeypatch) -> None:
|
|
class DefaultManifest:
|
|
profile = "default"
|
|
port = 8787
|
|
|
|
class OtherManifest:
|
|
profile = "custom"
|
|
port = 8787
|
|
|
|
monkeypatch.setattr(
|
|
"headroom.install.state.list_manifests",
|
|
lambda: [OtherManifest(), DefaultManifest()],
|
|
)
|
|
|
|
manifest = wrap_cli._find_persistent_manifest(8787)
|
|
|
|
assert manifest.profile == "default"
|
|
|
|
|
|
def test_recover_persistent_proxy_reuses_healthy_deployment(monkeypatch, runtime_platform) -> None:
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
|
|
assert wrap_cli._recover_persistent_proxy(8787) is True
|
|
|
|
|
|
def test_recover_persistent_proxy_warns_for_task_deployment(monkeypatch, runtime_platform) -> None:
|
|
class TaskManifest(_Manifest):
|
|
supervisor_kind = "task"
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: TaskManifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
|
|
assert wrap_cli._recover_persistent_proxy(8787) is False
|
|
|
|
|
|
def test_recover_persistent_proxy_routes_docker_task_to_docker_supervisor(monkeypatch) -> None:
|
|
calls: list[str] = []
|
|
|
|
class DockerTaskManifest(_Manifest):
|
|
preset = "persistent-task"
|
|
runtime_kind = "docker"
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: DockerTaskManifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr("headroom.install.runtime.runtime_ready", lambda manifest: False)
|
|
monkeypatch.setattr(
|
|
"headroom.install.runtime.start_persistent_docker",
|
|
lambda manifest: calls.append("docker"),
|
|
)
|
|
waits: list[dict[str, object]] = []
|
|
monkeypatch.setattr(
|
|
"headroom.install.runtime.wait_ready",
|
|
lambda manifest, **kwargs: waits.append(kwargs) or True,
|
|
)
|
|
|
|
assert wrap_cli._recover_persistent_proxy(8787) is True
|
|
assert calls == ["docker"]
|
|
assert waits == [{"timeout_seconds": 45, "require_identity": True}]
|
|
|
|
|
|
def test_ensure_proxy_restarts_idle_stale_ephemeral_proxy(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_proxy_version_restart_ignores_non_release_source_labels(monkeypatch) -> None:
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "0.29.0")
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "source-build+g6266a1d774b5"}) is False
|
|
assert (
|
|
wrap_cli._proxy_needs_version_restart({"version": "source-build+sha.abcdef123456"}) is False
|
|
)
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "6266a1d"}) is False
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.29.0+gabcdef0"}) is False
|
|
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "source-build+sha.abcdef123456")
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.29.0"}) is False
|
|
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "0.29.1")
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.29.0"}) is True
|
|
|
|
|
|
def test_proxy_version_restart_strips_dev_suffix_on_both_sides(monkeypatch) -> None:
|
|
"""A dev-built proxy must be compared by its base release, like the CLI is.
|
|
|
|
Regression: "0.34.0-dev" failed the release regex, normalized to None, and
|
|
the check never fired, so a stale source-built proxy was silently reused
|
|
forever (observed: a 21-day-old 0.34.0-dev proxy reused by a 0.37.0-dev
|
|
wrap).
|
|
"""
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "0.37.0-dev")
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.34.0-dev"}) is True
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.37.0-dev"}) is False
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.36.4"}) is True
|
|
|
|
monkeypatch.setattr(wrap_cli, "_HEADROOM_VERSION", "0.37.0")
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "0.36.4-dev"}) is True
|
|
|
|
# Unparseable running versions still never trigger a restart.
|
|
assert wrap_cli._proxy_needs_version_restart({"version": "unknown"}) is False
|
|
assert wrap_cli._proxy_needs_version_restart(None) is False
|
|
assert wrap_cli._proxy_needs_version_restart({}) is False
|
|
|
|
|
|
def test_ensure_proxy_restarts_ephemeral_proxy_for_openai_api_url_mismatch(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": "https://api.githubcopilot.com",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
openai_api_url="https://api.individual.githubcopilot.com",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][2]["openai_api_url"] == "https://api.individual.githubcopilot.com"
|
|
|
|
|
|
def test_proxy_routing_mismatches_detects_backend_and_urls(monkeypatch) -> None:
|
|
monkeypatch.delenv("HEADROOM_BACKEND", raising=False)
|
|
check = wrap_cli._proxy_routing_mismatches
|
|
|
|
# Stock proxy vs stock request: compatible.
|
|
assert check({"backend": "anthropic"}, backend=None) == []
|
|
# Running non-default backend vs default request: mismatch.
|
|
assert check({"backend": "anyllm"}, backend=None) == ["backend"]
|
|
# Requested backend vs different running backend: mismatch.
|
|
assert check({"backend": "anthropic"}, backend="anyllm") == ["backend"]
|
|
# HEADROOM_BACKEND env feeds the effective requested backend.
|
|
monkeypatch.setenv("HEADROOM_BACKEND", "anyllm")
|
|
assert check({"backend": "anyllm"}, backend=None) == []
|
|
monkeypatch.delenv("HEADROOM_BACKEND")
|
|
|
|
# Running proxy carries an upstream the caller never requested (the
|
|
# 2026-08-22 incident: a wrap-auggie proxy reused by wrap codex).
|
|
assert check(
|
|
{"backend": "anthropic", "augment_api_url": "https://xlb.api.augmentcode.com"},
|
|
backend=None,
|
|
) == ["augment_api_url"]
|
|
# Requested upstream the running proxy does not have: mismatch.
|
|
assert check(
|
|
{"backend": "anthropic"},
|
|
backend=None,
|
|
requested_api_urls={"openai_api_url": "https://api.githubcopilot.com"},
|
|
) == ["openai_api_url"]
|
|
# Same upstream on both sides, modulo normalization noise: compatible.
|
|
assert (
|
|
check(
|
|
{"backend": "anthropic", "openai_api_url": "https://api.githubcopilot.com/v1/"},
|
|
backend=None,
|
|
requested_api_urls={"openai_api_url": "https://api.githubcopilot.com"},
|
|
)
|
|
== []
|
|
)
|
|
# Captive provider paths are semantic; /v1 is not a removable transport
|
|
# convention for their REST catch-all routing.
|
|
assert check(
|
|
{"backend": "anthropic", "factory_api_url": "https://factory.example/v1"},
|
|
backend=None,
|
|
requested_api_urls={"factory_api_url": "https://factory.example"},
|
|
) == ["factory_api_url"]
|
|
# Missing backend key (older proxy): treated as unknown, not a mismatch.
|
|
assert check({"openai_api_url": None}, backend=None) == []
|
|
|
|
|
|
def test_effective_requested_proxy_routing_uses_startup_precedence(monkeypatch) -> None:
|
|
monkeypatch.setenv("HEADROOM_BACKEND", "anyllm")
|
|
monkeypatch.setenv("OPENAI_TARGET_API_URL", "https://env.openai.test")
|
|
monkeypatch.setenv("VERTEX_TARGET_API_URL", "https://env.vertex.test")
|
|
monkeypatch.setenv("AUGMENT_TARGET_API_URL", "https://env.augment.test/v1")
|
|
monkeypatch.setenv("FACTORY_TARGET_API_URL", "https://env.factory.test/v1")
|
|
|
|
backend, api_urls = wrap_cli._effective_requested_proxy_routing(
|
|
backend=None,
|
|
openai_api_url="https://explicit.openai.test",
|
|
anthropic_api_url=None,
|
|
vertex_api_url=None,
|
|
clear_vertex_api_url=True,
|
|
)
|
|
|
|
assert backend == "anyllm"
|
|
assert api_urls["openai_api_url"] == "https://explicit.openai.test"
|
|
assert api_urls["vertex_api_url"] is None
|
|
assert api_urls["augment_api_url"] == "https://env.augment.test/v1"
|
|
assert api_urls["factory_api_url"] == "https://env.factory.test/v1"
|
|
|
|
|
|
def test_ensure_proxy_restarts_idle_proxy_for_routing_mismatch(monkeypatch) -> None:
|
|
"""A clientless proxy with captive routing (augment upstream) is restarted."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"backend": "anthropic",
|
|
"augment_api_url": "https://xlb.api.augmentcode.com",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [])
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def _routing_mismatch_health(config_overrides: dict) -> dict:
|
|
"""Shared fixture: running proxy with captive augment routing (mismatch)."""
|
|
config = {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"backend": "anthropic",
|
|
"augment_api_url": "https://xlb.api.augmentcode.com",
|
|
}
|
|
config.update(config_overrides)
|
|
return {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": config,
|
|
}
|
|
|
|
|
|
def test_ensure_proxy_routing_mismatch_without_pid_raises(monkeypatch) -> None:
|
|
"""Incompatible proxy that exposes no PID cannot be restarted: fail loud."""
|
|
health = _routing_mismatch_health({})
|
|
del health["config"]["pid"]
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *a, **kw: (_ for _ in ()).throw(AssertionError("must not start")),
|
|
)
|
|
|
|
with pytest.raises(click.ClickException, match="did not expose a PID"):
|
|
wrap_cli._ensure_proxy(8787, False)
|
|
|
|
|
|
def test_ensure_proxy_routing_mismatch_kill_failure_raises(monkeypatch) -> None:
|
|
"""When the incompatible proxy survives the kill attempt: fail loud."""
|
|
health = _routing_mismatch_health({})
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_kill_proxy_by_pid", lambda pid, port: False)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *a, **kw: (_ for _ in ()).throw(AssertionError("must not start")),
|
|
)
|
|
|
|
with pytest.raises(click.ClickException, match="Failed to stop incompatible proxy"):
|
|
wrap_cli._ensure_proxy(8787, False)
|
|
|
|
|
|
def test_ensure_proxy_bumps_port_for_routing_mismatch_with_attached_clients(
|
|
monkeypatch,
|
|
) -> None:
|
|
"""With live clients attached, an incompatible proxy is neither reused
|
|
nor restarted: the wrap starts a dedicated proxy on a free port."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"backend": "anthropic",
|
|
"augment_api_url": "https://xlb.api.augmentcode.com",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [999])
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("incompatible proxy with live clients must not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][1][0] == 8799
|
|
|
|
|
|
def test_ensure_proxy_reuses_proxy_with_matching_routing_config(monkeypatch) -> None:
|
|
"""Happy path: identical routing config reuses the running proxy."""
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"backend": "anthropic",
|
|
"openai_api_url": None,
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("must not kill")),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("must not start")),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_restarts_idle_proxy_for_backend_mismatch(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "backend": "anyllm"},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [])
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_never_reuses_configless_proxy(monkeypatch) -> None:
|
|
"""A proxy exposing no config block cannot be compatibility-checked; it
|
|
must not be silently reused (the persistent branch already treats
|
|
config-unavailable as fall-through). No PID is known, so the wrap starts
|
|
a dedicated proxy on a free port instead of killing blindly."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_config", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("configless proxy exposes no PID; must not kill")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_routing_mismatch_on_persistent_deployment_uses_dedicated_port(
|
|
monkeypatch,
|
|
) -> None:
|
|
"""A persistent deployment's routing comes from its manifest; a wrap with
|
|
different routing must not reuse it AND must not let the ephemeral path
|
|
kill it (the manifest would point at a hijacked port). The wrap starts a
|
|
dedicated proxy on a free port and the deployment keeps running."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"backend": "anthropic",
|
|
"augment_api_url": "https://xlb.api.augmentcode.com",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not restart the deployment")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not kill the deployment's proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][1][0] == 8799
|
|
|
|
|
|
def test_ensure_proxy_healthy_persistent_uses_config_fallback(monkeypatch) -> None:
|
|
config = {"pid": 12345, "backend": "anthropic"}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_query_proxy_health",
|
|
lambda port: {"version": wrap_cli._HEADROOM_VERSION},
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_config", lambda port: config)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("matching persistent proxy should be reused")
|
|
),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False) == (None, 8787)
|
|
|
|
|
|
def test_ensure_proxy_healthy_configless_persistent_uses_dedicated_port(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_query_proxy_health",
|
|
lambda port: {"version": wrap_cli._HEADROOM_VERSION},
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_config", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("configless persistent proxy must not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False) == (None, 8799)
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("env_name", "config_key"),
|
|
[
|
|
("ANTHROPIC_TARGET_API_URL", "anthropic_api_url"),
|
|
("OPENAI_TARGET_API_URL", "openai_api_url"),
|
|
("GEMINI_TARGET_API_URL", "gemini_api_url"),
|
|
("CLOUDCODE_TARGET_API_URL", "cloudcode_api_url"),
|
|
("VERTEX_TARGET_API_URL", "vertex_api_url"),
|
|
("AUGMENT_TARGET_API_URL", "augment_api_url"),
|
|
("FACTORY_TARGET_API_URL", "factory_api_url"),
|
|
],
|
|
)
|
|
def test_ensure_proxy_reuses_matching_inherited_routing_target(
|
|
monkeypatch,
|
|
env_name: str,
|
|
config_key: str,
|
|
) -> None:
|
|
target = "https://tenant.example/api"
|
|
for name in (
|
|
"ANTHROPIC_TARGET_API_URL",
|
|
"OPENAI_TARGET_API_URL",
|
|
"GEMINI_TARGET_API_URL",
|
|
"CLOUDCODE_TARGET_API_URL",
|
|
"VERTEX_TARGET_API_URL",
|
|
"AUGMENT_TARGET_API_URL",
|
|
"FACTORY_TARGET_API_URL",
|
|
):
|
|
monkeypatch.delenv(name, raising=False)
|
|
monkeypatch.setenv(env_name, target)
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": 12345, "backend": "anthropic", config_key: target},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("matching inherited routing must reuse the proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("matching inherited routing must not kill the proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("matching inherited routing must not start another proxy")
|
|
),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False) == (None, 8787)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("ensure_kwargs", "config_overrides", "env_override"),
|
|
[
|
|
(
|
|
{"anthropic_api_url": "https://expected.anthropic.test"},
|
|
{"anthropic_api_url": "https://running.anthropic.test"},
|
|
None,
|
|
),
|
|
(
|
|
{"vertex_api_url": "https://expected.vertex.test"},
|
|
{"vertex_api_url": "https://running.vertex.test"},
|
|
None,
|
|
),
|
|
(
|
|
{"backend": "anyllm"},
|
|
{"backend": "anthropic"},
|
|
None,
|
|
),
|
|
(
|
|
{},
|
|
{"factory_api_url": "https://running.factory.test"},
|
|
("FACTORY_TARGET_API_URL", "https://expected.factory.test"),
|
|
),
|
|
],
|
|
)
|
|
def test_ensure_proxy_recovered_routing_mismatch_uses_dedicated_port(
|
|
monkeypatch,
|
|
ensure_kwargs: dict[str, object],
|
|
config_overrides: dict[str, object],
|
|
env_override: tuple[str, str] | None,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
if env_override is not None:
|
|
monkeypatch.setenv(*env_override)
|
|
config = {"pid": 12345, "backend": "anthropic"}
|
|
config.update(config_overrides)
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": config,
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not restart the persistent proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not kill the persistent proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False, **ensure_kwargs) == (None, 8799)
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_stale_attached_persistent_routing_mismatch_uses_dedicated_port(
|
|
monkeypatch,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
health = _routing_mismatch_health({})
|
|
health["version"] = "0.0.1"
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *args, **kwargs: [999])
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("active persistent proxy must not restart")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("active persistent proxy must not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False, memory=True) == (None, 8799)
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_stale_attached_ephemeral_routing_mismatch_uses_dedicated_port(
|
|
monkeypatch,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
health = _routing_mismatch_health({})
|
|
health["version"] = "0.0.1"
|
|
health["runtime"]["websocket_sessions"]["active_sessions"] = 1
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *args, **kwargs: [])
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("active proxy must not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
assert wrap_cli._ensure_proxy(8787, False, memory=True) == (None, 8799)
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("pid", "kill_result", "message"),
|
|
[
|
|
(None, True, "did not expose a PID"),
|
|
(12345, False, "Failed to stop stale proxy"),
|
|
],
|
|
)
|
|
def test_ensure_proxy_stale_idle_restart_failures_are_actionable(
|
|
monkeypatch,
|
|
pid: int | None,
|
|
kill_result: bool,
|
|
message: str,
|
|
) -> None:
|
|
config: dict[str, object] = {"backend": "anthropic"}
|
|
if pid is not None:
|
|
config["pid"] = pid
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": config,
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_kill_proxy_by_pid", lambda found_pid, port: kill_result)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("failed stale restart must not start another proxy")
|
|
),
|
|
)
|
|
|
|
with pytest.raises(click.ClickException, match=message):
|
|
wrap_cli._ensure_proxy(8787, False)
|
|
|
|
|
|
def test_ensure_proxy_starts_isolated_ephemeral_proxy_for_copilot_subscription_seed(
|
|
monkeypatch,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8788,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("subscription-seeded session should not restart the shared proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
copilot_api_token="tid-session-token",
|
|
copilot_refresh_oauth_token="gho-refresh",
|
|
copilot_api_token_expires_at=456.5,
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8788
|
|
assert calls[0] == ("find_port", 8788)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][1][0] == 8788
|
|
assert calls[1][2]["copilot_api_token"] == "tid-session-token"
|
|
assert calls[1][2]["copilot_refresh_oauth_token"] == "gho-refresh"
|
|
assert calls[1][2]["copilot_api_token_expires_at"] == 456.5
|
|
|
|
|
|
def test_ensure_proxy_isolates_copilot_subscription_seed_with_api_token_only(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8788,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
copilot_api_token="direct-token-only",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8788
|
|
assert calls[0] == ("find_port", 8788)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][2]["copilot_api_token"] == "direct-token-only"
|
|
|
|
|
|
def test_ensure_proxy_starts_isolated_ephemeral_proxy_when_subscription_seed_targets_persistent_port(
|
|
monkeypatch,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr(
|
|
"headroom.install.health.probe_ready",
|
|
lambda url: (_ for _ in ()).throw(
|
|
AssertionError("subscription-seeded session should skip persistent probing")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("subscription-seeded session should not restart the persistent proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8788,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
copilot_api_token="tid-session-token",
|
|
copilot_refresh_oauth_token="gho-refresh",
|
|
copilot_api_token_expires_at=456.5,
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8788
|
|
assert calls[0] == ("find_port", 8788)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][1][0] == 8788
|
|
assert calls[1][2]["copilot_api_token"] == "tid-session-token"
|
|
assert calls[1][2]["copilot_refresh_oauth_token"] == "gho-refresh"
|
|
assert calls[1][2]["copilot_api_token_expires_at"] == 456.5
|
|
|
|
|
|
def test_ensure_proxy_reuses_agent_proxy_without_savings_profile(monkeypatch) -> None:
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.delenv("HEADROOM_SAVINGS_PROFILE", raising=False)
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("default agent proxy should not restart for savings profile")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, agent_type="codex")
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_restarts_for_explicit_agent_savings_profile(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setenv("HEADROOM_SAVINGS_PROFILE", "agent-90")
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, agent_type="codex")
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_reuses_agent_proxy_with_savings_profile(monkeypatch) -> None:
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"savings_profile": "agent-90",
|
|
"target_ratio": 0.10,
|
|
"compress_user_messages": True,
|
|
"compress_system_messages": True,
|
|
"protect_recent": 2,
|
|
"protect_analysis_context": True,
|
|
"min_tokens_to_crush": 120,
|
|
"max_items_after_crush": 8,
|
|
"smart_crusher_with_compaction": False,
|
|
"accuracy_guard": "strict",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setenv("HEADROOM_SAVINGS_PROFILE", "agent-90")
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("configured proxy should not restart")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, agent_type="cursor")
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_leaves_active_stale_ephemeral_proxy_running(monkeypatch) -> None:
|
|
health = {
|
|
"version": "0.0.1",
|
|
"runtime": {"websocket_sessions": {"active_sessions": 2, "active_relay_tasks": 2}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("active proxy should not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_defers_version_restart_when_http_wrapper_attached(monkeypatch) -> None:
|
|
"""A stale-version proxy is NOT restarted while a marker-tracked HTTP
|
|
wrapper is attached, even though the WebSocket session count is zero."""
|
|
health = {
|
|
"version": "0.0.1", # stale → version restart wanted
|
|
# No WebSocket relay sessions — the gap that let the old code kill it.
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
# Another HTTP wrapper (PID 999) is attached per the marker registry.
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [999])
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("attached proxy must not be killed for a version restart")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy must not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_defers_flag_restart_when_other_wrapper_attached(monkeypatch) -> None:
|
|
"""Requesting --memory must not restart the proxy out from under another
|
|
attached wrapper; reuse the running proxy as-is instead."""
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION, # same version → no version restart
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
# Running proxy lacks `memory`; this session asks for it.
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [999])
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("attached proxy must not be killed to add flags")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("replacement proxy must not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, memory=True)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
def test_ensure_proxy_restarts_for_flags_when_no_other_wrapper(monkeypatch) -> None:
|
|
"""Control: with no other wrapper attached, a missing-flag restart still
|
|
happens — the guard must not block the single-client upgrade path."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {"pid": "12345", "memory": False, "learn": False, "code_graph": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: len(calls) == 0)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [])
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, memory=True)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_diverts_persistent_deployment_for_routing_mismatch(
|
|
monkeypatch,
|
|
) -> None:
|
|
"""A routing-URL mismatch against a persistent deployment starts a
|
|
dedicated proxy on a free port. Killing the deployment's proxy and
|
|
starting an ephemeral one with the requested routing (the old behavior)
|
|
orphaned the manifest: it still pointed at the port while the routing it
|
|
declares no longer matched what served it."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": 12345,
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": None,
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
# Persistent proxy is running, so _check_proxy returns True
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kw: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not kill the deployment's proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
# Request openai_api_url that differs from running config (None)
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
openai_api_url="https://api.githubcopilot.com",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
# Deployment left running; dedicated proxy started on the free port with
|
|
# the requested routing.
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][1][0] == 8799
|
|
assert calls[1][2]["openai_api_url"] == "https://api.githubcopilot.com"
|
|
|
|
|
|
def test_ensure_proxy_restarts_persistent_deployment_for_memory_mismatch(monkeypatch) -> None:
|
|
"""Persistent deployment should restart when memory is requested but not enabled."""
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": 12345,
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": None,
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
# Persistent proxy is running, so _check_proxy returns True
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_port_bind_error", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda pid, port: calls.append(("kill", pid, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
# Request memory that differs from running config (False)
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, memory=True)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
# Proxy should be killed and restarted due to memory mismatch
|
|
assert calls[0] == ("kill", 12345, 8787)
|
|
assert calls[1][0] == "start"
|
|
assert calls[1][2]["memory"] is True
|
|
|
|
|
|
def test_ensure_proxy_restarts_recovered_persistent_for_openai_api_url_mismatch(
|
|
monkeypatch,
|
|
) -> None:
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": 12345,
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": None,
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not restart the persistent proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("routing mismatch must not kill the persistent proxy")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
openai_api_url="https://api.business.githubcopilot.com",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_restarts_recovered_persistent_when_config_unavailable(monkeypatch) -> None:
|
|
calls: list[object] = []
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: {"version": "x"})
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_config", lambda port: None)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_find_available_port",
|
|
lambda start_port, **kwargs: calls.append(("find_port", start_port)) or 8799,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("configless recovered proxy must not restart")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("configless recovered proxy must not be killed")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: calls.append(("start", args, kwargs)),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
openai_api_url="https://api.business.githubcopilot.com",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8799
|
|
assert calls[0] == ("find_port", 8787)
|
|
assert calls[1][0] == "start"
|
|
|
|
|
|
def test_ensure_proxy_reuses_persistent_deployment_when_features_match(monkeypatch) -> None:
|
|
"""Persistent deployment should be reused when all requested features match."""
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": 12345,
|
|
"memory": True,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": "https://api.githubcopilot.com",
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("should not restart when features match")
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("should not start ephemeral proxy when features match")
|
|
),
|
|
)
|
|
|
|
# Request same features as running config
|
|
proc, actual_port = wrap_cli._ensure_proxy(
|
|
8787,
|
|
False,
|
|
memory=True,
|
|
openai_api_url="https://api.githubcopilot.com",
|
|
)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"ensure_kwargs",
|
|
[
|
|
{"memory": True},
|
|
{"learn": True},
|
|
{"code_graph": True},
|
|
],
|
|
)
|
|
def test_ensure_proxy_recovered_persistent_deployment_checks_feature_mismatch(
|
|
monkeypatch,
|
|
ensure_kwargs: dict[str, bool],
|
|
) -> None:
|
|
"""Recovered persistent deployments must still restart on feature mismatch.
|
|
|
|
Regression guard for the recover path: when wrap requests memory, do not
|
|
early-return right after recover; restart the deployment if needed.
|
|
"""
|
|
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"memory": False,
|
|
"learn": False,
|
|
"code_graph": False,
|
|
"openai_api_url": None,
|
|
},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_restart_persistent_proxy",
|
|
lambda manifest, port: calls.append(("restart", manifest.profile, port)) or True,
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("ephemeral proxy should not start")
|
|
),
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False, **ensure_kwargs)
|
|
|
|
assert proc is None
|
|
assert actual_port == 8787
|
|
assert calls == [("restart", "default", 8787)]
|
|
|
|
|
|
def test_ensure_proxy_recovered_feature_restart_failure_raises(monkeypatch) -> None:
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"config": {"pid": 12345, "backend": "anthropic", "memory": False},
|
|
}
|
|
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: _Manifest())
|
|
monkeypatch.setattr("headroom.install.health.probe_ready", lambda url: False)
|
|
monkeypatch.setattr(wrap_cli, "_recover_persistent_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_restart_persistent_proxy", lambda manifest, port: False)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_start_proxy",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(
|
|
AssertionError("failed persistent restart must not start an ephemeral proxy")
|
|
),
|
|
)
|
|
|
|
with pytest.raises(click.ClickException, match="could not be restarted"):
|
|
wrap_cli._ensure_proxy(8787, False, memory=True)
|
|
|
|
|
|
def _dedicated_start(monkeypatch, *, health, manifest=None):
|
|
"""Drive a Copilot-seeded wrap that must start a dedicated proxy beside port 8787."""
|
|
calls: list[object] = []
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: manifest)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_find_available_port", lambda start_port, **kw: 8788)
|
|
monkeypatch.setattr(
|
|
wrap_cli, "_start_proxy", lambda *args, **kwargs: calls.append(("start", args, kwargs))
|
|
)
|
|
wrap_cli._ensure_proxy(8787, False, copilot_api_token="tid-session-token")
|
|
assert calls and calls[0][0] == "start"
|
|
return calls[0][2]
|
|
|
|
|
|
def test_dedicated_copilot_proxy_carries_the_shared_proxys_extensions(monkeypatch, capsys) -> None:
|
|
"""An enterprise shared proxy enables its savings sinks with --proxy-extension;
|
|
the dedicated proxy a Copilot seed forces must keep reporting through them."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
health = {"config": {"pid": "1", "proxy_extensions": ["routemegood", "observability"]}}
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health=health)
|
|
|
|
assert kwargs["proxy_extensions"] == ["observability", "routemegood"]
|
|
assert "Extensions carried over from the proxy on port 8787: observability,routemegood" in (
|
|
capsys.readouterr().out
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("configured", ["teams", ""])
|
|
def test_dedicated_copilot_proxy_env_extensions_win(monkeypatch, capsys, configured) -> None:
|
|
"""The env var is inherited by the subprocess and outranks the carry-over;
|
|
an empty value is the documented way to start a bare dedicated proxy."""
|
|
monkeypatch.setenv("HEADROOM_PROXY_EXTENSIONS", configured)
|
|
health = {"config": {"pid": "1", "proxy_extensions": ["observability"]}}
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health=health)
|
|
|
|
assert kwargs["proxy_extensions"] is None
|
|
out = capsys.readouterr().out
|
|
assert "carried over" not in out
|
|
assert "could not read the extensions" not in out
|
|
|
|
|
|
def test_dedicated_copilot_proxy_ignores_a_foreign_port_holder(monkeypatch, capsys) -> None:
|
|
"""A non-Headroom service on the port ran no extensions: nothing to carry, no warning."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health={"status": "ok"})
|
|
|
|
assert not kwargs["proxy_extensions"]
|
|
assert "could not read the extensions" not in capsys.readouterr().out
|
|
|
|
|
|
def test_dedicated_copilot_proxy_warns_when_the_shared_proxy_hides_its_extensions(
|
|
monkeypatch, capsys
|
|
) -> None:
|
|
"""A proxy that predates the /health field cannot be read; say so instead of
|
|
silently starting bare."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health={"config": {"pid": "1"}})
|
|
|
|
assert kwargs["proxy_extensions"] is None
|
|
out = capsys.readouterr().out
|
|
assert "Warning: could not read the extensions of the proxy on port 8787" in out
|
|
assert "HEADROOM_PROXY_EXTENSIONS=<names>" in out
|
|
|
|
|
|
def test_dedicated_copilot_proxy_reads_extensions_from_the_manifest(monkeypatch) -> None:
|
|
"""With no reachable /health, the manifest's recorded environment says what ran."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
class _ExtManifest(_Manifest):
|
|
base_env = {"HEADROOM_PROXY_EXTENSIONS": "observability, lossless_guard"}
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health=None, manifest=_ExtManifest())
|
|
|
|
assert kwargs["proxy_extensions"] == ["lossless_guard", "observability"]
|
|
|
|
|
|
def test_dedicated_copilot_proxy_reads_extensions_from_the_manifest_proxy_args(monkeypatch) -> None:
|
|
"""A manifest edited by hand can enable extensions as ``--proxy-extension``
|
|
flags in ``proxy_args``; the runtime passes them to the proxy verbatim, so
|
|
they count, in both flag forms. Click lets the flag outrank
|
|
``HEADROOM_PROXY_EXTENSIONS``, so a ``base_env`` value next to the flags is
|
|
not something the shared proxy ran and must not be carried."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
class _ArgsManifest(_Manifest):
|
|
base_env = {"HEADROOM_PROXY_EXTENSIONS": "skill_search"}
|
|
proxy_args = [
|
|
"--host",
|
|
"127.0.0.1",
|
|
"--proxy-extension",
|
|
"observability,control_plane",
|
|
"--proxy-extension=lossless_guard",
|
|
]
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health=None, manifest=_ArgsManifest())
|
|
|
|
assert kwargs["proxy_extensions"] == ["control_plane", "lossless_guard", "observability"]
|
|
|
|
|
|
def test_dedicated_copilot_proxy_falls_back_to_the_manifest_for_an_older_proxy(
|
|
monkeypatch, capsys
|
|
) -> None:
|
|
"""A running proxy that predates the /health field is not the last word when
|
|
the deployment's manifest records what it was installed with."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
class _ExtManifest(_Manifest):
|
|
base_env = {"HEADROOM_PROXY_EXTENSIONS": "control_plane"}
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health={"config": {"pid": "1"}}, manifest=_ExtManifest())
|
|
|
|
assert kwargs["proxy_extensions"] == ["control_plane"]
|
|
assert "could not read the extensions" not in capsys.readouterr().out
|
|
|
|
|
|
def test_dedicated_copilot_proxy_warns_for_an_older_proxy_whose_manifest_is_silent(
|
|
monkeypatch, capsys
|
|
) -> None:
|
|
"""A manifest without HEADROOM_PROXY_EXTENSIONS does not prove the running
|
|
proxy has none (its runtime env layers on the launch environment), so an
|
|
older proxy plus a silent manifest is unknown and warns."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
class _SilentManifest(_Manifest):
|
|
base_env = {"ANTHROPIC_API_KEY": "sk-ant-redacted"}
|
|
|
|
kwargs = _dedicated_start(
|
|
monkeypatch, health={"config": {"pid": "1"}}, manifest=_SilentManifest()
|
|
)
|
|
|
|
assert kwargs["proxy_extensions"] is None
|
|
assert "Warning: could not read the extensions of the proxy on port 8787" in (
|
|
capsys.readouterr().out
|
|
)
|
|
|
|
|
|
def test_dedicated_copilot_proxy_trusts_an_explicitly_empty_manifest_value(
|
|
monkeypatch, capsys
|
|
) -> None:
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
|
|
class _BareManifest(_Manifest):
|
|
base_env = {"HEADROOM_PROXY_EXTENSIONS": ""}
|
|
|
|
kwargs = _dedicated_start(monkeypatch, health=None, manifest=_BareManifest())
|
|
|
|
assert not kwargs["proxy_extensions"]
|
|
assert "could not read the extensions" not in capsys.readouterr().out
|
|
|
|
|
|
def test_attached_routing_mismatch_carries_the_shared_proxys_extensions(
|
|
monkeypatch, capsys
|
|
) -> None:
|
|
"""The non-persistent shared proxy with live clients and other routing is
|
|
left running; the dedicated proxy started beside it keeps its extensions."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
calls: list[object] = []
|
|
health = {
|
|
"version": wrap_cli._HEADROOM_VERSION,
|
|
"runtime": {"websocket_sessions": {"active_sessions": 0, "active_relay_tasks": 0}},
|
|
"config": {
|
|
"pid": "12345",
|
|
"backend": "anthropic",
|
|
"augment_api_url": "https://xlb.api.augmentcode.com",
|
|
"proxy_extensions": ["control_plane", "observability"],
|
|
},
|
|
}
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: port == 8787)
|
|
monkeypatch.setattr(wrap_cli, "_query_proxy_health", lambda port: health)
|
|
monkeypatch.setattr(wrap_cli, "_live_proxy_clients", lambda *a, **kw: [999])
|
|
monkeypatch.setattr(wrap_cli, "_find_available_port", lambda start_port, **kw: 8799)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_kill_proxy_by_pid",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("must not be killed")),
|
|
)
|
|
monkeypatch.setattr(
|
|
wrap_cli, "_start_proxy", lambda *args, **kwargs: calls.append(("start", args, kwargs))
|
|
)
|
|
|
|
proc, actual_port = wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert (proc, actual_port) == (None, 8799)
|
|
assert calls[0][2]["proxy_extensions"] == ["control_plane", "observability"]
|
|
assert "Extensions carried over from the proxy on port 8787" in capsys.readouterr().out
|
|
|
|
|
|
def test_plain_start_passes_no_extensions(monkeypatch) -> None:
|
|
"""Nothing owns the port: the proxy starts from the shell env as before."""
|
|
monkeypatch.delenv("HEADROOM_PROXY_EXTENSIONS", raising=False)
|
|
calls: list[object] = []
|
|
monkeypatch.setattr(wrap_cli, "_find_persistent_manifest", lambda port: None)
|
|
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: False)
|
|
monkeypatch.setattr(
|
|
wrap_cli,
|
|
"_query_proxy_health",
|
|
lambda port: (_ for _ in ()).throw(AssertionError("no proxy to ask")),
|
|
)
|
|
monkeypatch.setattr(wrap_cli, "_find_available_port", lambda start_port, **kw: 8787)
|
|
monkeypatch.setattr(
|
|
wrap_cli, "_start_proxy", lambda *args, **kwargs: calls.append(("start", args, kwargs))
|
|
)
|
|
|
|
wrap_cli._ensure_proxy(8787, False)
|
|
|
|
assert calls[0][2]["proxy_extensions"] is None
|