fix(cookbook): only block model launch on real port collisions (#4760)

* Fix #4507: only block model launch on real port collisions

Quick-run hardcoded port 8000 and never called _nextAvailablePort(), so
every launch collided. Both pre-launch guards (serve panel + quick-run)
were count-based and fired regardless of port.

- quick-run now auto-assigns a free port (8080 for llama.cpp)
- both guards parse the new port and only prompt on a real overlap,
  stopping only the colliding serve
- dialog reports the actual port instead of a hardcoded 8000

* refactor(cookbook): share _taskPort for port parsing; auto-assign llama.cpp port

Addresses review on #4760:
- _taskPort regex now matches --port= as well as --port (space)
- _nextAvailablePort and both launch guards reuse _taskPort instead of inline regex
- quick-run llama.cpp no longer pins 8080, so two can run concurrently

* fix(cookbook): _taskPort also parses -p; add port-parsing tests

Addresses review on #4760:
- _taskPort now matches -p <n> too, so it's the complete single reader
  (was missing the short flag that other readers already handle)
- add tests/test_cookbook_port_parsing_js.py covering the port forms,
  shared-reader reuse, and llama.cpp auto-assign

* test(cookbook): extract pure port helpers and test behavior

Addresses review on #4760: the prior tests only asserted source strings.
- extract portOf() and nextFreePort() into static/js/cookbookPorts.js
- cookbookRunning.js imports them; _taskPort and _nextAvailablePort delegate
- tests run the helpers via node and assert real behavior: all port forms
  (--port, --port=, -p, -p=), next-free-port skipping taken ports, and the
  same-port-clash / different-port-coexist outcome

---------

Co-authored-by: samy <samy@odysseus.boukouro.com>
This commit is contained in:
Samy
2026-06-24 13:44:09 -04:00
committed by GitHub
parent 22379fe736
commit 5d23495eb2
5 changed files with 130 additions and 56 deletions
+53
View File
@@ -0,0 +1,53 @@
"""Behavioral tests for Cookbook port parsing / picking (#4507 follow-up).
Driven through `node --input-type=module` (same approach as the other
*_js.py tests); skips when `node` is not installed.
"""
import json
import shutil
import subprocess
from pathlib import Path
import pytest
_REPO = Path(__file__).resolve().parent.parent
_HELPER = _REPO / "static" / "js" / "cookbookPorts.js"
_HAS_NODE = shutil.which("node") is not None
def _run(expr):
js = (
f"import {{ portOf, nextFreePort }} from '{_HELPER.as_posix()}';"
f"console.log(JSON.stringify({expr}));"
)
proc = subprocess.run(
["node", "--input-type=module"],
input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout.strip())
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_port_of_handles_all_forms():
assert _run("portOf('vllm serve m --host 0.0.0.0 --port 8000')") == "8000"
assert _run("portOf('x --port=8001')") == "8001"
assert _run("portOf('llama-server -p 8002')") == "8002"
assert _run("portOf('llama-server -p=8003')") == "8003"
assert _run("portOf('serve with no port flag')") == ""
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_next_free_port_skips_taken_including_eq_and_short_flag():
# a --port= serve and a -p serve are both 'taken'; picker skips them
taken = "[portOf('a --port=8000'), portOf('b -p 8001')]"
assert _run(f"nextFreePort({taken})") == "8002"
assert _run("nextFreePort([])") == "8000"
assert _run("nextFreePort(['8000', '8002'])") == "8001"
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_clash_outcome_same_port_flagged_different_ignored():
# the guard's predicate is portOf(cmd) === target
assert _run("portOf('m --port 8000') === '8000'") is True
assert _run("portOf('m --port 8001') === '8000'") is False