Spaces:
Runtime error
Runtime error
| """Per-worktree dev port computation. | |
| Single source of truth so the FastAPI launcher, the LangGraph Studio wrapper, | |
| and the kill-ports helper all agree on which two ports this worktree owns. | |
| Conductor exposes ``CONDUCTOR_WORKSPACE_NAME`` for every worktree it manages. | |
| We hash that into a stable 1-99 offset so two worktrees never collide. The | |
| original (non-worktree) checkout has no such env var and keeps the canonical | |
| 8000 / 2024 ports. | |
| Override either port at any time by exporting ``PORT`` or ``LANGGRAPH_PORT`` | |
| before starting the dev process — these win unconditionally. | |
| CLI: | |
| uv run python scripts/_ports.py # human-readable summary | |
| uv run python scripts/_ports.py server # server port only | |
| uv run python scripts/_ports.py studio # studio port only | |
| uv run python scripts/_ports.py export # printable shell exports | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import os | |
| import sys | |
| _BASE_SERVER_PORT = 8000 | |
| _BASE_STUDIO_PORT = 2024 | |
| def workspace_name() -> str | None: | |
| """Return the Conductor workspace name, or None outside Conductor.""" | |
| return os.environ.get("CONDUCTOR_WORKSPACE_NAME") or None | |
| def _offset() -> int: | |
| name = workspace_name() | |
| if not name: | |
| return 0 | |
| digest = hashlib.sha1(name.encode("utf-8")).hexdigest() | |
| return int(digest, 16) % 99 + 1 | |
| def server_port() -> int: | |
| if (override := os.environ.get("PORT")): | |
| return int(override) | |
| return _BASE_SERVER_PORT + _offset() | |
| def studio_port() -> int: | |
| if (override := os.environ.get("LANGGRAPH_PORT")): | |
| return int(override) | |
| return _BASE_STUDIO_PORT + _offset() | |
| def main() -> None: | |
| cmd = sys.argv[1] if len(sys.argv) > 1 else "info" | |
| if cmd == "server": | |
| print(server_port()) | |
| elif cmd == "studio": | |
| print(studio_port()) | |
| elif cmd == "export": | |
| print(f"export PORT={server_port()}") | |
| print(f"export LANGGRAPH_PORT={studio_port()}") | |
| elif cmd == "info": | |
| name = workspace_name() or "(none — canonical ports)" | |
| print(f"workspace: {name}") | |
| print(f"offset: +{_offset()}") | |
| print(f"server: http://127.0.0.1:{server_port()}") | |
| print(f"studio: http://127.0.0.1:{studio_port()}") | |
| else: | |
| sys.exit(f"unknown command: {cmd}") | |
| if __name__ == "__main__": | |
| main() | |