fix(zcode): resolve the proxy port before choosing the upstream

wrap zcode detected the upstream against the requested port, but
_ensure_proxy moves to another port when the requested one is held by a
non-Headroom service. A backend on the occupied port was skipped as if it
were the proxy, and a provider on the replacement port (the proxy itself)
could be chosen as the upstream. wrap zcode now resolves the port the proxy
will bind first and detects the upstream against that port.

Refs #4409

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 20:44:29 +07:00
co-authored by Claude Sonnet 5.5
parent 8101aa936d
commit 98ff36c71a
2 changed files with 77 additions and 0 deletions
+8
View File
@@ -8110,6 +8110,14 @@ def zcode(
if prepare_only:
return
# _ensure_proxy moves to another port when the requested one is held by a
# non-Headroom service. Resolve that port first so the upstream is chosen
# against the port the proxy will actually bind.
if not no_proxy and not (_check_proxy(port) and not _foreign_listener(port)):
try:
port = _find_available_port(port)
except (OSError, RuntimeError):
pass # _ensure_proxy reports the same error
upstream = _detect_zcode_upstream(proxy_port=port)
anthropic_url, openai_url = _zcode_upstream_to_urls(upstream)
+69
View File
@@ -8,6 +8,7 @@ settings UI.
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
@@ -393,6 +394,74 @@ def test_wrap_zcode_passes_upstream_to_watcher(
assert captured.get("port") == 9000
def _zcode_config_with(tmp_path: Path, base_url: str) -> Path:
home = tmp_path / "zhome"
(home / "v2").mkdir(parents=True)
(home / "v2" / "config.json").write_text(
json.dumps(
{
"provider": {
"p": {
"name": "P",
"kind": "anthropic",
"enabled": True,
"options": {"baseURL": base_url},
}
}
}
),
encoding="utf-8",
)
return home
def _run_zcode_with_port_busy(
runner: CliRunner, tmp_path: Path, base_url: str, *, busy: bool
) -> dict[str, object]:
"""Run ``wrap zcode --port 8787``; port 8787 is held by a foreign service when *busy*."""
captured: dict[str, object] = {}
home = _zcode_config_with(tmp_path, base_url)
with (
patch("headroom.providers.zcode.runtime.zcode_config_dir", return_value=home),
patch.object(wrap_mod, "_check_proxy", return_value=busy),
patch.object(wrap_mod, "_foreign_listener", return_value=busy),
patch.object(wrap_mod, "_find_available_port", return_value=8788 if busy else 8787),
patch.object(wrap_mod, "_run_proxy_only_watcher", side_effect=captured.update),
):
runner.invoke(main, ["wrap", "zcode", "--port", "8787"])
return captured
def test_wrap_zcode_port_fallback_keeps_backend_on_busy_port(
runner: CliRunner, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A backend on the occupied port stays a valid upstream; the proxy moves to 8788."""
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
captured = _run_zcode_with_port_busy(runner, tmp_path, "http://127.0.0.1:8787", busy=True)
assert captured["port"] == 8788
assert captured["anthropic_api_url"] == "http://127.0.0.1:8787"
def test_wrap_zcode_port_fallback_skips_provider_on_replacement_port(
runner: CliRunner, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A provider pointing at the replacement port is the proxy itself, not an upstream."""
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
captured = _run_zcode_with_port_busy(runner, tmp_path, "http://127.0.0.1:8788", busy=True)
assert captured["port"] == 8788
assert captured["anthropic_api_url"] == "https://api.z.ai/api/anthropic"
def test_wrap_zcode_free_port_unchanged(
runner: CliRunner, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""With the requested port free, a provider on it is still skipped as the proxy."""
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
captured = _run_zcode_with_port_busy(runner, tmp_path, "http://127.0.0.1:8787", busy=False)
assert captured["port"] == 8787
assert captured["anthropic_api_url"] == "https://api.z.ai/api/anthropic"
def test_wrap_zcode_passes_openai_upstream(
runner: CliRunner,
tmp_path: Path,