From beb9f4be16444535782db4b8a91eca3c7868e53b Mon Sep 17 00:00:00 2001 From: Hermes Date: Wed, 18 Nov 2026 23:03:22 +0000 Subject: [PATCH] tests: refuse to start when two sections use the same port A port collision is the most misleading failure this suite can produce: the second listener dies with "Address already in use" and the checks then blame its behaviour. Two happened on 2026-11-18 -- a new stub on 9995 (section 16's RICH/POOR) and a new router on PORT+16 (ctx-vision's) -- and both cost a full suite run to diagnose. check_ports.py resolves PORT+N, FPORT+N and the P_* assignments across every sourced script, then reports any listener port used twice. run.sh aborts on a collision before starting anything. Verified by planting both collisions in a copy: the scan names cache.sh:67 vs run.sh:641 (9995) and cache.sh:70 vs ctx-vision.sh:83 (8151); the real tree passes. Full suite with the check in place: 537 OK, 0 FAIL, PASS. --- tests/check_ports.py | 88 ++++++++++++++++++++++++++++++++++++++++++++ tests/run.sh | 8 ++++ 2 files changed, 96 insertions(+) create mode 100755 tests/check_ports.py diff --git a/tests/check_ports.py b/tests/check_ports.py new file mode 100755 index 0000000..a148d8a --- /dev/null +++ b/tests/check_ports.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +"""Two sections must never use the same port. + +A port collision is the most misleading failure this suite can produce: the +second router dies with "Address already in use", the checks then fail with a +message about its behaviour ("missing 'served by POOR'"), and the next person +debugs the wrong file. Two collisions happened on 2026-11-18: a new stub on +9995 (section 16 owns it) and a new router on PORT+16 (ctx-vision owns it). + +The scan covers what actually binds: `listen :` and `stub `. +Ports that only appear as a base URL or in a throwaway `printf` config are not +listeners and are ignored. +""" +import re +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent +ASSIGN = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)=(.+)$") +EXPR = re.compile(r"^\$\(\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\+\s*(\d+)\s*\)\)$") +VAR = re.compile(r"^\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?$") +LISTEN = re.compile(r"\blisten\s+[^:\s]+:(\S+)") +STUB = re.compile(r"^\s*(?:tests/)?stub\s+(\S+)") + + +def assignments(text): + for line in text.splitlines(): + line = line.split("#", 1)[0] + for part in line.split(";"): + m = ASSIGN.match(part.strip()) + if m: + yield m.group(1), m.group(2).strip().strip("\"'") + + +def resolve(tok, values): + tok = tok.strip().strip("\"'") + if tok.isdigit(): + return int(tok) + m = EXPR.match(tok) + if m and m.group(1) in values: + return values[m.group(1)] + int(m.group(2)) + m = VAR.match(tok) + if m: + return values.get(m.group(1)) + return None + + +def main(): + sources = {p.name: p.read_text() for p in sorted(ROOT.glob("*.sh"))} + values = {} + # Fixed point: a variable may be defined in a file sourced later, and may + # itself be $((OTHER+7)). + for _ in range(8): + for text in sources.values(): + for name, raw in assignments(text): + v = resolve(raw, values) + if v is not None: + values[name] = v + + sites = {} + unresolved = 0 + for name, text in sources.items(): + for no, line in enumerate(text.splitlines(), 1): + if "printf" in line: + continue # throwaway configs that are only --check'ed + m = LISTEN.search(line) or STUB.match(line) + if not m: + continue + port = resolve(m.group(1), values) + if port is None: + unresolved += 1 + continue + sites.setdefault(port, []).append(f"{name}:{no}") + + dups = {p: where for p, where in sites.items() if len(where) > 1} + if dups: + print("port collision in the test suite:", file=sys.stderr) + for port in sorted(dups): + print(f" {port}: {', '.join(dups[port])}", file=sys.stderr) + return 1 + if unresolved: + print(f"check_ports: {unresolved} listener(s) use a port this scan " + "cannot resolve (checked the rest)", file=sys.stderr) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/run.sh b/tests/run.sh index 994d17e..dc42655 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -2,6 +2,14 @@ # junction test suite — stub endpoints only, no network, no models. ~150 s. set -uo pipefail cd "$(dirname "$0")/.." +# Two sections on the same port is the most misleading failure this suite can +# produce: the second listener dies with "Address already in use" and the checks +# then blame its behaviour ("missing 'served by POOR'"). Two such collisions +# happened on 2026-11-18. The scan is static, so it costs nothing. +python3 "$(dirname "$0")/check_ports.py" || { + echo "aborting: port collision in the test suite" >&2 + exit 1 +} # Hermetisch: dieser Lauf darf nicht davon abhängen, welche Schlüssel zufällig in der # Umgebung stehen. Sonst legt junction Endpunkte an, die kein Test erwartet. unset OPENCODE_GO_API_KEY OLLAMA_KEY ANTHROPIC_API_KEY CLAUDE_API_KEY 2>/dev/null || true