From f7b02128e0109ddb0df64bfa384f65b1375bea9a Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 1 Sep 2026 00:22:48 +0300 Subject: [PATCH 01/22] fix: stop_proxy returns False on timeout instead of True Sourcery AI review: after the 5-second polling loop, stop_proxy returned True even when the process was still running, which could lead to port conflicts when starting a replacement process. --- openrot/core/proxy.py | 2 +- tests/unit/core/test_proxy.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/openrot/core/proxy.py b/openrot/core/proxy.py index 3c0160a..acfb5b2 100644 --- a/openrot/core/proxy.py +++ b/openrot/core/proxy.py @@ -124,4 +124,4 @@ def stop_proxy(pid: int | None = None) -> bool: if not is_running(pid): return True time.sleep(0.1) - return True + return False diff --git a/tests/unit/core/test_proxy.py b/tests/unit/core/test_proxy.py index 8dd6725..4909fc6 100644 --- a/tests/unit/core/test_proxy.py +++ b/tests/unit/core/test_proxy.py @@ -120,8 +120,14 @@ def raise_perm(*args: object) -> None: def test_stop_proxy_sends_term(monkeypatch: pytest.MonkeyPatch) -> None: signals: list[tuple[int, int]] = [] + call_count = {"n": 0} + + def fake_is_running(pid: int) -> bool: + call_count["n"] += 1 + return call_count["n"] <= 1 + monkeypatch.setattr(proxy, "load_pid", lambda: 5) - monkeypatch.setattr(proxy, "is_running", lambda pid: True) + monkeypatch.setattr(proxy, "is_running", fake_is_running) monkeypatch.setattr(proxy.os, "kill", lambda *a: signals.append(a)) assert proxy.stop_proxy(5) is True assert signals == [(5, proxy.signal.SIGTERM)] From afc939bc346c58f80818b3c271f13d0311d5d7e6 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 1 Sep 2026 10:26:26 +0300 Subject: [PATCH 02/22] fix: :bug: isolate _internal/ per app to prevent cross-install conflicts - Install binary and _internal/ to ~/.local/lib/openrot/ instead of ~/.local/bin/ - Symlink binary into ~/.local/bin/ for PATH discovery - Update README install path description --- README.md | 3 ++- install.sh | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 49830e8..9c6367d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,8 @@ image from GHCR: curl -fsSL https://raw.githubusercontent.com/vispar-tech/openrot/main/install-docker.sh | bash ``` -> The first method installs the standalone binary to `~/.local/bin/openrot`. +> The first method installs the standalone binary to `~/.local/lib/openrot` +> (symlink in `~/.local/bin/openrot`). > If your distro already has the prerequisites, you can skip them: > `OPENROT_SKIP_WARP=1 ... | bash`. diff --git a/install.sh b/install.sh index fe78a1c..392d71b 100755 --- a/install.sh +++ b/install.sh @@ -27,6 +27,7 @@ die() { printf '\033[31m[openrot]\033[0m %s\n' "$*" >&2; exit 1; } macos() { [ "$(uname -s)" = "Darwin" ]; } linux() { [ "$(uname -s)" = "Linux" ]; } +lib_dir="$PREFIX/lib/openrot" bin_dir="$PREFIX/bin" URL_BASE="${BIN_URL:-https://github.com/vispar-tech/openrot/releases/download/$VERSION}" @@ -121,15 +122,18 @@ download_binary() { if [ -d "$tmp/openrot" ] && [ -x "$tmp/openrot/openrot" ]; then src="$tmp/openrot" fi - mkdir -p "$bin_dir" - install -m 0755 "$src/openrot" "$bin_dir/openrot" + mkdir -p "$lib_dir" + install -m 0755 "$src/openrot" "$lib_dir/openrot" if [ -d "$src/_internal" ]; then - rm -rf "$bin_dir/_internal" - cp -R "$src/_internal" "$bin_dir/_internal" - chmod -R u+rwX,go+rX "$bin_dir/_internal" + rm -rf "$lib_dir/_internal" + cp -R "$src/_internal" "$lib_dir/_internal" + chmod -R u+rwX,go+rX "$lib_dir/_internal" fi rm -rf "$tmp" - say "Installed openrot to $bin_dir/openrot" + # Symlink binary into ~/.local/bin/ + mkdir -p "$bin_dir" + ln -sf "../lib/openrot/openrot" "$bin_dir/openrot" + say "Installed openrot to $lib_dir/openrot" if ! echo "$PATH" | grep -q "$bin_dir"; then warn "Add $bin_dir to your PATH:" if macos; then @@ -162,7 +166,7 @@ main() { start_warp check_singbox download_binary - "$bin_dir/openrot" --version + "$lib_dir/openrot" --version say "Done. Run 'openrot --help' to get started." } From 4a729f671eca0f6a1cd534abf710f701c8f8c38e Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 1 Sep 2026 14:14:27 +0300 Subject: [PATCH 03/22] feat: :sparkles: add ip-based node deduplication and bridge 502 retry --- openrot/core/bridge.py | 23 ++++++---- openrot/core/health.py | 51 +++++++++++++++++---- openrot/core/refresh.py | 2 + openrot/core/singbox.py | 29 +++++++++--- openrot/core/verify.py | 74 +++++++++++++++++++++---------- openrot/models/config.py | 1 + openrot/models/node.py | 1 + openrot/providers/free.py | 24 +++++++--- tests/unit/core/test_bridge.py | 2 +- tests/unit/core/test_health.py | 12 +++-- tests/unit/core/test_refresh.py | 4 +- tests/unit/core/test_singbox.py | 6 ++- tests/unit/core/test_verify.py | 38 ++++++++++------ tests/unit/providers/test_free.py | 15 ++++--- 14 files changed, 203 insertions(+), 79 deletions(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 554be62..2b18fb0 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -260,14 +260,21 @@ def _handle(self, method: str) -> None: try: resp, client = forward(cfg_obj, request) except UpstreamError as exc: - msg = {"error": {"message": str(exc), "type": "upstream"}} - payload = json.dumps(msg).encode() - self.send_response(502) - self.send_header("Content-Type", "application/json") - self.send_header("Content-Length", str(len(payload))) - self.end_headers() - self.wfile.write(payload) - return + events.warning("bridge upstream error, rotating and retrying: %s", exc) + with contextlib.suppress(SystemExit): + cascade.rotate() + try: + cfg_obj = cfg.load_config() + resp, client = forward(cfg_obj, request, rotate_on_429=False) + except UpstreamError as retry_exc: + msg = {"error": {"message": str(retry_exc), "type": "upstream"}} + payload = json.dumps(msg).encode() + self.send_response(502) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(payload))) + self.end_headers() + self.wfile.write(payload) + return try: _respond(self, resp) finally: diff --git a/openrot/core/health.py b/openrot/core/health.py index 7ad3a7f..16479ef 100644 --- a/openrot/core/health.py +++ b/openrot/core/health.py @@ -1,5 +1,6 @@ from datetime import datetime +from openrot import log from openrot.config import Config, Node, NodeProtocol, NodeStatus, Strategy from openrot.core import verify from openrot.core.singbox import probe_vless @@ -15,7 +16,10 @@ def check_node(node: Node, cfg: Config) -> tuple[bool, float | None]: venode = parse_vless(node.raw) except ParseError: return False, None - return probe_vless(venode, cfg.singbox_bin, cfg.health_timeout) + alive, latency, _egress_ip = probe_vless( + venode, cfg.singbox_bin, cfg.health_timeout + ) + return alive, latency def test_all( @@ -29,6 +33,8 @@ def test_all( `cfg.fail_threshold`. """ alive_count = 0 + vless_survivors = [] + proxy_survivors = [] vless_nodes = [n for n in cfg.all_nodes() if n.protocol == NodeProtocol.VLESS] proxy_nodes = [ n @@ -46,10 +52,20 @@ def test_all( on_stage=on_stage, on_progress=on_progress, max_workers=cfg.max_workers, + deduplicate_by_ip=cfg.deduplicate_by_ip, ) - okay = {raw: latency for (raw, _vnode), latency in vless_survivors} + okay = { + raw: (latency, egress_ip) + for (raw, _vnode), latency, egress_ip in vless_survivors + } for node in vless_nodes: - alive_count += _apply_result(node, okay.get(node.raw), cfg) + entry = okay.get(node.raw) + alive_count += _apply_result( + node, + entry[0] if entry else None, + cfg, + entry[1] if entry else None, + ) if proxy_nodes: candidates = free.fetch_candidates("\n".join(n.raw for n in proxy_nodes)) @@ -61,20 +77,37 @@ def test_all( on_stage=on_stage, on_progress=on_progress, max_workers=cfg.max_workers, + deduplicate_by_ip=cfg.deduplicate_by_ip, ) proxy_ok = { - (proto, host, port): latency - for (proto, host, port), latency in proxy_survivors + (proto, host, port): (latency, egress_ip) + for (proto, host, port), latency, egress_ip in proxy_survivors } for node in proxy_nodes: parsed = free.parse_proxy(node.raw) - latency = None if parsed is None else proxy_ok.get(parsed) - alive_count += _apply_result(node, latency, cfg) + entry = None if parsed is None else proxy_ok.get(parsed) + alive_count += _apply_result( + node, + entry[0] if entry else None, + cfg, + entry[1] if entry else None, + ) + + total_input = len(vless_nodes) + len(proxy_nodes) + total_output = len(vless_survivors) + len(proxy_survivors) + dedup_dropped = total_input - total_output + if dedup_dropped > 0: + log.get_logger().info("dedup: removed %d duplicate-IP nodes", dedup_dropped) return alive_count -def _apply_result(node: Node, latency: float | None, cfg: Config) -> int: +def _apply_result( + node: Node, + latency: float | None, + cfg: Config, + egress_ip: str | None = None, +) -> int: """Record one health result for a node; return 1 if it survived. Survivors are marked ALIVE with their latency; everyone else accumulates a @@ -85,6 +118,8 @@ def _apply_result(node: Node, latency: float | None, cfg: Config) -> int: node.status = NodeStatus.ALIVE node.latency_ms = latency node.fails = 0 + if egress_ip is not None: + node.egress_ip = egress_ip return 1 node.fails += 1 if node.fails >= cfg.fail_threshold: diff --git a/openrot/core/refresh.py b/openrot/core/refresh.py index 511b2c1..c00a344 100644 --- a/openrot/core/refresh.py +++ b/openrot/core/refresh.py @@ -51,6 +51,7 @@ def fetch_profile_nodes( on_stage=on_stage, on_progress=on_progress, max_workers=cfg_obj.max_workers, + deduplicate_by_ip=cfg_obj.deduplicate_by_ip, ) return verify.nodes_from_proxy_survivors(proxy_survivors) records = vless.extract_from_text(text) @@ -63,6 +64,7 @@ def fetch_profile_nodes( on_stage=on_stage, on_progress=on_progress, max_workers=cfg_obj.max_workers, + deduplicate_by_ip=cfg_obj.deduplicate_by_ip, ) return verify.nodes_from_vless_survivors(relay_survivors) diff --git a/openrot/core/singbox.py b/openrot/core/singbox.py index c04d06d..681e102 100644 --- a/openrot/core/singbox.py +++ b/openrot/core/singbox.py @@ -1,3 +1,4 @@ +import contextlib import json import socket import subprocess @@ -184,16 +185,20 @@ def _wait_for_port(host: str, port: int, timeout: float, step: float = 0.05) -> return False +EgressResult = tuple[bool, float | None, str | None] + + def probe_vless( node: VlessNode, singbox_bin: str, timeout: float, url: str | None = None -) -> tuple[bool, float | None]: +) -> EgressResult: """Probe ``url`` (default HEALTH_URL) through one throwaway sing-box. Waits until the local listener is up (instead of a fixed sleep); when sing-box exits during the wait, its stderr is logged. - Returns (alive, latency_ms): ``alive`` is True when the node routed the - request and got a 2xx response; ``latency_ms`` is the request time in ms. + Returns (alive, latency_ms, egress_ip): ``alive`` is True when the node + routed the request and got a 2xx response; ``latency_ms`` is the request + time in ms; ``egress_ip`` is the public IP seen by the target. """ target = url or HEALTH_URL port = _free_port() @@ -215,7 +220,7 @@ def probe_vless( proc.returncode, stderr_text.strip(), ) - return False, None + return False, None, None with httpx.Client( proxy=f"http://127.0.0.1:{port}", timeout=timeout ) as client: @@ -223,11 +228,21 @@ def probe_vless( try: resp = client.get(target) except httpx.HTTPError: - return False, None + return False, None, None if 200 <= resp.status_code < 300: latency = round((time.monotonic() - start) * 1000, 1) - return True, latency - return False, None + egress_ip = _get_egress_ip(client) + return True, latency, egress_ip + return False, None, None finally: proc.terminate() cfg_path.unlink(missing_ok=True) + + +def _get_egress_ip(client: httpx.Client) -> str | None: + """Fetch public IP from api.ipify.org through an existing proxy client.""" + with contextlib.suppress(Exception): + resp = client.get("https://api.ipify.org?format=json", timeout=5) + if resp.status_code == 200: + return resp.json().get("ip") + return None diff --git a/openrot/core/verify.py b/openrot/core/verify.py index 99fa1cf..398866e 100644 --- a/openrot/core/verify.py +++ b/openrot/core/verify.py @@ -134,24 +134,24 @@ def _run_stage[T]( def _probe_latencies( node: vless.VlessNode, singbox_bin: str, timeout: float, url: str | None -) -> list[float]: - """Probe one node; return [latency_ms] on a 2xx, [] otherwise.""" - alive, latency = probe_vless(node, singbox_bin, timeout, url) - return [latency] if alive and latency is not None else [] +) -> tuple[list[float], str | None]: + """Probe one node; return ([latency_ms], egress_ip) on a 2xx.""" + alive, latency, egress_ip = probe_vless(node, singbox_bin, timeout, url) + return ([latency] if alive and latency is not None else [], egress_ip) def _run_probe[T]( items: list[T], - fn: Callable[[T], list[float]], + fn: Callable[[T], tuple[list[float], str | None]], on_stage: Stage | None, on_progress: ProgressFn | None, max_workers: int = MAX_WORKERS, -) -> list[tuple[T, float]]: +) -> list[tuple[T, float, str | None]]: """Probe `items` in a worker pool; keep survivors that reached the urltest URL. A survivor needs the configured urltest URL reachable (non-empty latency sample). ``on_progress`` fires after every finished node; ranks by median - latency. + latency. Returns (item, latency, egress_ip) tuples sorted by latency. """ total = len(items) if total == 0: @@ -159,18 +159,18 @@ def _run_probe[T]( on_stage("probe", 0, 0) return [] needed = 1 - results: list[tuple[T, float]] = [] + results: list[tuple[T, float, str | None]] = [] done = 0 with ThreadPoolExecutor(max_workers=max_workers) as pool: futures = {pool.submit(fn, it): it for it in items} for fut in as_completed(futures): done += 1 try: - latencies = fut.result() + latencies, egress_ip = fut.result() except Exception: - latencies = [] + latencies, egress_ip = [], None if len(latencies) >= needed: - results.append((futures[fut], median(latencies))) + results.append((futures[fut], median(latencies), egress_ip)) if on_progress is not None: on_progress("probe", done, total) if on_stage is not None: @@ -179,6 +179,27 @@ def _run_probe[T]( return results +def _dedupe_by_ip[T]( + survivors: list[tuple[T, float, str | None]], +) -> list[tuple[T, float, str | None]]: + """Deduplicate survivors by egress_ip, keeping the lowest-latency entry per IP. + + Entries without an egress_ip always pass through. + """ + by_ip: dict[str, list[tuple[T, float, str | None]]] = {} + no_ip: list[tuple[T, float, str | None]] = [] + for entry in survivors: + ip = entry[2] + if ip is None: + no_ip.append(entry) + else: + by_ip.setdefault(ip, []).append(entry) + result: list[tuple[T, float, str | None]] = list(no_ip) + for ip_group in by_ip.values(): + result.append(min(ip_group, key=lambda e: e[1])) + return sorted(result, key=lambda e: e[1]) + + def _dedupe(records: Iterable[str]) -> list[str]: seen: set[str] = set() result: list[str] = [] @@ -208,11 +229,11 @@ def verify_vless_pool( on_stage: Stage | None = None, on_progress: ProgressFn | None = None, max_workers: int = MAX_WORKERS, -) -> list[tuple[RelayCandidate, float]]: - """Run the full pipeline over vless records; return (raw, parsed, latency) pairs. + deduplicate_by_ip: bool = True, +) -> list[tuple[RelayCandidate, float, str | None]]: + """Run the full pipeline over vless records. - Latency of a survivor is the request time to ``urltest_url`` (default - HEALTH_URL). + Returns (raw, parsed, latency, egress_ip) tuples. """ candidates = _parse_relay_candidates(_dedupe(records)) if on_stage is not None: @@ -253,6 +274,8 @@ def verify_vless_pool( on_progress, max_workers, ) + if deduplicate_by_ip: + results = _dedupe_by_ip(results) return results[:limit] if limit is not None else results @@ -265,10 +288,11 @@ def verify_proxy_pool( on_stage: Stage | None = None, on_progress: ProgressFn | None = None, max_workers: int = MAX_WORKERS, -) -> list[tuple[ProxyKey, float]]: + deduplicate_by_ip: bool = True, +) -> list[tuple[ProxyKey, float, str | None]]: """TCP-check proxy candidates and probe the survivors. - Returns (key, latency) pairs, best latency first. Latency is the request + Returns (key, latency, egress_ip) pairs, best latency first. Latency is the request time to ``urltest_url`` (default HEALTH_URL). """ if on_stage is not None: @@ -288,13 +312,15 @@ def verify_proxy_pool( on_progress, max_workers, ) + if deduplicate_by_ip: + results = _dedupe_by_ip(results) return results[:limit] if limit is not None else results def nodes_from_vless_survivors( - survivors: list[tuple[RelayCandidate, float]], + survivors: list[tuple[RelayCandidate, float, str | None]], ) -> list[Node]: - """Turn verified (raw, vnode, latency) pairs into published ALIVE Nodes.""" + """Turn verified (raw, vnode, latency, egress_ip) pairs into ALIVE Nodes.""" now = datetime.now() return [ Node( @@ -305,18 +331,19 @@ def nodes_from_vless_survivors( latency_ms=latency, priority=i, last_check=now, + egress_ip=egress_ip, ) - for i, ((raw, _vnode), latency) in enumerate(survivors) + for i, ((raw, _vnode), latency, egress_ip) in enumerate(survivors) ] def nodes_from_proxy_survivors( - survivors: list[tuple[ProxyKey, float]], + survivors: list[tuple[ProxyKey, float, str | None]], ) -> list[Node]: - """Turn verified (protocol, host, port, latency) pairs into ALIVE Nodes.""" + """Turn verified (key, latency, egress_ip) pairs into ALIVE Nodes.""" now = datetime.now() result = [] - for i, ((protocol, host, port), latency) in enumerate(survivors): + for i, ((protocol, host, port), latency, egress_ip) in enumerate(survivors): raw = f"{protocol}://{host}:{port}" result.append( Node( @@ -327,6 +354,7 @@ def nodes_from_proxy_survivors( latency_ms=latency, priority=i, last_check=now, + egress_ip=egress_ip, ) ) return result diff --git a/openrot/models/config.py b/openrot/models/config.py index 25abde6..8810e70 100644 --- a/openrot/models/config.py +++ b/openrot/models/config.py @@ -33,6 +33,7 @@ class Config(BaseModel): bridge_upstream: str = DEFAULT_BRIDGE_UPSTREAM bridge_retry_statuses: list[int] = Field(default_factory=lambda: [429]) bridge_retry_attempts: int = Field(default=1, ge=0, le=10) + deduplicate_by_ip: bool = True def all_nodes(self) -> list[Node]: """Flatten nodes from all profiles into a single list.""" diff --git a/openrot/models/node.py b/openrot/models/node.py index 8591f8b..11a0c68 100644 --- a/openrot/models/node.py +++ b/openrot/models/node.py @@ -22,3 +22,4 @@ class Node(BaseModel): latency_ms: float | None = None fails: int = 0 last_check: datetime | None = None + egress_ip: str | None = None diff --git a/openrot/providers/free.py b/openrot/providers/free.py index e5249aa..f5ec07c 100644 --- a/openrot/providers/free.py +++ b/openrot/providers/free.py @@ -1,3 +1,4 @@ +import contextlib import time from urllib.parse import urlparse @@ -46,11 +47,11 @@ def probe_targets( port: int, timeout: float = CHECK_TIMEOUT, url: str | None = None, -) -> list[float]: +) -> tuple[list[float], str | None]: """Probe ``url`` (default HEALTH_URL) through the forward proxy. - Returns [latency] on a 2xx response; empty when the proxy cannot route - traffic at all. + Returns (latencies, egress_ip) where latencies is [latency] on a 2xx + response or [] when the proxy cannot route traffic at all. """ target = url or HEALTH_URL start = time.monotonic() @@ -62,10 +63,21 @@ def probe_targets( ) as client: resp = client.get(target) except httpx.HTTPError: - return [] + return [], None if 200 <= resp.status_code < 300: - return [round((time.monotonic() - start) * 1000, 1)] - return [] + latency = round((time.monotonic() - start) * 1000, 1) + egress_ip = _get_egress_ip(client) + return [latency], egress_ip + return [], None + + +def _get_egress_ip(client: httpx.Client) -> str | None: + """Fetch public IP from api.ipify.org through an existing proxy client.""" + with contextlib.suppress(Exception): + resp = client.get("https://api.ipify.org?format=json", timeout=5) + if resp.status_code == 200: + return resp.json().get("ip") + return None def fetch_candidates(text: str) -> list[tuple[str, str, int]]: diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 470c4bb..e9a04c9 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -451,7 +451,7 @@ def test_handler_returns_502_on_upstream_error( monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) - def boom(cfg_obj: object, request: object) -> object: + def boom(cfg_obj: object, request: object, **kwargs: object) -> object: raise bridge.UpstreamError("up boom") monkeypatch.setattr(bridge, "forward", boom) diff --git a/tests/unit/core/test_health.py b/tests/unit/core/test_health.py index d89a91c..2541cdc 100644 --- a/tests/unit/core/test_health.py +++ b/tests/unit/core/test_health.py @@ -55,8 +55,10 @@ def test_all_updates_statuses(monkeypatch: pytest.MonkeyPatch) -> None: ] ) - def fake_pool(*a: object, **k: object) -> list[tuple[tuple[str, object], float]]: - return [(("r1", object()), 10.0)] + def fake_pool( + *a: object, **k: object + ) -> list[tuple[tuple[str, object], float, str | None]]: + return [(("r1", object()), 10.0, None)] monkeypatch.setattr("openrot.core.health.verify.verify_vless_pool", fake_pool) assert test_all(c) == 1 @@ -92,7 +94,9 @@ def test_all_forwards_urltest_url(monkeypatch: pytest.MonkeyPatch) -> None: c.urltest_url = "https://probe.example/x" seen: dict[str, object] = {} - def fake_pool(*a: object, **k: object) -> list[tuple[tuple[str, object], float]]: + def fake_pool( + *a: object, **k: object + ) -> list[tuple[tuple[str, object], float, str | None]]: seen["urltest_url"] = k.get("urltest_url") return [] @@ -133,7 +137,7 @@ def test_check_node_vless_probes(monkeypatch: pytest.MonkeyPatch) -> None: parsed = object() monkeypatch.setattr("openrot.core.health.parse_vless", lambda raw: parsed) monkeypatch.setattr( - "openrot.core.health.probe_vless", lambda vn, sb, to: (True, 3.0) + "openrot.core.health.probe_vless", lambda vn, sb, to: (True, 3.0, None) ) assert check_node(node, Config()) == (True, 3.0) diff --git a/tests/unit/core/test_refresh.py b/tests/unit/core/test_refresh.py index da3686d..2aca90a 100644 --- a/tests/unit/core/test_refresh.py +++ b/tests/unit/core/test_refresh.py @@ -51,7 +51,7 @@ def test_fetch_profile_nodes_relay(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( refresh.verify, "verify_vless_pool", - lambda *a, **k: [(("vless://a@1.1.1.1:80", None), 12.0)], + lambda *a, **k: [(("vless://a@1.1.1.1:80", None), 12.0, None)], ) result = refresh.fetch_profile_nodes(prof, _cfg_for(prof)) assert result and result[0].protocol.value == "vless" @@ -67,7 +67,7 @@ def test_fetch_profile_nodes_proxy(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( refresh.verify, "verify_proxy_pool", - lambda *a, **k: [(("http", "h", 1), 20.0)], + lambda *a, **k: [(("http", "h", 1), 20.0, None)], ) result = refresh.fetch_profile_nodes(prof, _cfg_for(prof)) assert result and result[0].protocol.value == "http" diff --git a/tests/unit/core/test_singbox.py b/tests/unit/core/test_singbox.py index 3d1a4e7..1b77932 100644 --- a/tests/unit/core/test_singbox.py +++ b/tests/unit/core/test_singbox.py @@ -136,9 +136,10 @@ def test_probe_vless_waits_for_readiness_then_probes( monkeypatch.setattr(sb.subprocess, "Popen", lambda *a, **k: _RunningProc()) monkeypatch.setattr(sb.httpx, "Client", _FakeClient) - alive, latency = probe_vless(_vless(), "sing-box", timeout=5.0) + alive, latency, egress_ip = probe_vless(_vless(), "sing-box", timeout=5.0) assert alive is True assert latency is not None + assert egress_ip is None def test_probe_vless_logs_stderr_when_singbox_exits( @@ -166,9 +167,10 @@ def warning(self, *args: object) -> None: )(), ) - alive, latency = probe_vless(_vless(), "sing-box", timeout=5.0) + alive, latency, egress_ip = probe_vless(_vless(), "sing-box", timeout=5.0) assert alive is False assert latency is None + assert egress_ip is None assert len(warnings) == 1 message, rc = warnings[0][0], warnings[0][1] assert "sing-box exited during probe" in message diff --git a/tests/unit/core/test_verify.py b/tests/unit/core/test_verify.py index 34e57bc..83e6a43 100644 --- a/tests/unit/core/test_verify.py +++ b/tests/unit/core/test_verify.py @@ -38,7 +38,9 @@ def test_verify_progress_reports_each_node(monkeypatch: pytest.MonkeyPatch) -> N lambda host, port, timeout: host == "1.1.1.1", ) monkeypatch.setattr( - verify.free, "probe_targets", lambda p, h, port, timeout, url=None: [5.0] + verify.free, + "probe_targets", + lambda p, h, port, timeout, url=None: ([5.0], None), ) done: list[tuple[object, object, object]] = [] @@ -155,9 +157,9 @@ def test_verify_vless_pool_runs_all_stages(monkeypatch: pytest.MonkeyPatch) -> N def fake_probe( node: object, bin: str, timeout: float, url: str | None = None - ) -> list[float]: + ) -> tuple[list[float], str | None]: probe_calls["n"] += 1 - return [12.0, 8.0] if probe_calls["n"] == 1 else [30.0] + return ([12.0, 8.0], None) if probe_calls["n"] == 1 else ([30.0], None) monkeypatch.setattr(verify, "_probe_latencies", fake_probe) @@ -174,7 +176,7 @@ def fake_probe( assert ("tcp", 1, 2) in stages assert ("probe", 1, 1) in stages assert len(result) == 1 - raw, latency = result[0] + raw, latency, _egress_ip = result[0] assert raw[0] == _RELAY assert latency == 10.0 # median of [12.0, 8.0] @@ -187,7 +189,7 @@ def test_verify_vless_pool_fails_low_success_rate( monkeypatch.setattr(verify, "singbox_check", lambda n, bin: True) # no target reachable -> below the success gate monkeypatch.setattr( - verify, "_probe_latencies", lambda node, bin, timeout, url=None: [] + verify, "_probe_latencies", lambda node, bin, timeout, url=None: ([], None) ) assert verify.verify_vless_pool([_RELAY], "sing-box", 3.0) == [] @@ -210,7 +212,7 @@ def test_verify_proxy_pool(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( verify.free, "probe_targets", - lambda p, h, port, timeout, url=None: [5.0], + lambda p, h, port, timeout, url=None: ([5.0], None), ) stages: list[tuple[object, object, object]] = [] @@ -219,7 +221,7 @@ def test_verify_proxy_pool(monkeypatch: pytest.MonkeyPatch) -> None: ) assert ("tcp", 1, 2) in stages assert ("probe", 1, 1) in stages - assert result == [(("http", "1.1.1.1", 80), 5.0)] + assert result == [(("http", "1.1.1.1", 80), 5.0, None)] def test_verify_vless_pool_forwards_urltest_url( @@ -232,9 +234,9 @@ def test_verify_vless_pool_forwards_urltest_url( def fake_probe( node: object, bin: str, timeout: float, url: str | None = None - ) -> list[float]: + ) -> tuple[list[float], str | None]: seen["url"] = url - return [7.0] + return ([7.0], None) monkeypatch.setattr(verify, "_probe_latencies", fake_probe) result = verify.verify_vless_pool( @@ -252,9 +254,9 @@ def test_verify_proxy_pool_forwards_urltest_url( def fake_probe( p: str, h: str, port: int, timeout: float, url: str | None = None - ) -> list[float]: + ) -> tuple[list[float], str | None]: seen["url"] = url - return [5.0] + return ([5.0], None) monkeypatch.setattr(verify.free, "probe_targets", fake_probe) result = verify.verify_proxy_pool( @@ -267,20 +269,30 @@ def fake_probe( def test_nodes_from_vless_survivors() -> None: - survivors = [((_RELAY, _parsed()), 5.0), ((_RELAY2, _parsed(_RELAY2)), 3.0)] + survivors = [ + ((_RELAY, _parsed()), 5.0, None), + ((_RELAY2, _parsed(_RELAY2)), 3.0, "1.2.3.4"), + ] nodes = verify.nodes_from_vless_survivors(survivors) assert isinstance(nodes[0], Node) assert nodes[0].status == NodeStatus.ALIVE assert nodes[0].latency_ms == 5.0 assert nodes[1].priority == 1 assert nodes[0].protocol.value == "vless" + assert nodes[0].egress_ip is None + assert nodes[1].egress_ip == "1.2.3.4" def test_nodes_from_proxy_survivors() -> None: - survivors = [(("http", "1.1.1.1", 80), 7.0), (("socks5", "2.2.2.2", 1080), 3.0)] + survivors = [ + (("http", "1.1.1.1", 80), 7.0, None), + (("socks5", "2.2.2.2", 1080), 3.0, "5.6.7.8"), + ] nodes = verify.nodes_from_proxy_survivors(survivors) assert nodes[0].protocol.value == "http" assert nodes[1].protocol.value == "socks5" assert nodes[0].latency_ms == 7.0 assert nodes[1].priority == 1 assert nodes[1].raw == "socks5://2.2.2.2:1080" + assert nodes[0].egress_ip is None + assert nodes[1].egress_ip == "5.6.7.8" diff --git a/tests/unit/providers/test_free.py b/tests/unit/providers/test_free.py index 8e9fdf2..49a6cef 100644 --- a/tests/unit/providers/test_free.py +++ b/tests/unit/providers/test_free.py @@ -79,17 +79,22 @@ def test_fetch_candidates_dedupes() -> None: def test_probe_targets_records_2xx(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(free.httpx, "Client", lambda *a, **k: _FakeClient(_Response())) - latencies = free.probe_targets("http", "1.2.3.4", 8080) + latencies, egress_ip = free.probe_targets("http", "1.2.3.4", 8080) assert len(latencies) == 1 assert isinstance(latencies[0], float) + assert egress_ip is None def test_probe_targets_uses_configured_url(monkeypatch: pytest.MonkeyPatch) -> None: seen: dict[str, object] = {} class _RecordingClient: + _first = True + def get(self, *a: object, **k: object) -> object: - seen["url"] = a[0] + if self._first: + seen["url"] = a[0] + self._first = False return _Response() def __enter__(self) -> Self: @@ -99,7 +104,7 @@ def __exit__(self, *a: object, **k: object) -> bool: return False monkeypatch.setattr(free.httpx, "Client", lambda *a, **k: _RecordingClient()) - latencies = free.probe_targets( + latencies, _ = free.probe_targets( "http", "1.2.3.4", 8080, url="https://alt.example/probe" ) assert len(latencies) == 1 @@ -110,7 +115,7 @@ def test_probe_targets_rejects_non_2xx(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( free.httpx, "Client", lambda *a, **k: _FakeClient(_BadResponse()) ) - assert free.probe_targets("http", "1.2.3.4", 8080) == [] + assert free.probe_targets("http", "1.2.3.4", 8080) == ([], None) def test_probe_targets_empty_on_error(monkeypatch: pytest.MonkeyPatch) -> None: @@ -119,7 +124,7 @@ def test_probe_targets_empty_on_error(monkeypatch: pytest.MonkeyPatch) -> None: "Client", lambda *a, **k: _FakeClient(error=free.httpx.HTTPError("boom")), ) - assert free.probe_targets("http", "1.2.3.4", 8080) == [] + assert free.probe_targets("http", "1.2.3.4", 8080) == ([], None) def test_check_node_uses_config_health_timeout( From 54ac98a573d3a9ffb56e04da286f0c261b5bb6b5 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 1 Sep 2026 14:26:17 +0300 Subject: [PATCH 04/22] feat: :sparkles: add openrot provider command to toggle opencode provider in opencode.jsonc --- openrot/cli.py | 15 ++++++++++++++ openrot/opencode.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ poetry.lock | 19 +++++++++++++++-- pyproject.toml | 1 + 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 openrot/opencode.py diff --git a/openrot/cli.py b/openrot/cli.py index b3e2ad6..b3c7f87 100644 --- a/openrot/cli.py +++ b/openrot/cli.py @@ -33,6 +33,7 @@ from openrot.core import nodes as node_ops from openrot.core import probe as probe_core from openrot.providers import warp +from openrot import opencode app = typer.Typer( help=( @@ -755,6 +756,20 @@ def config_edit() -> None: subprocess.run([editor, str(path)]) # noqa: S603 +@app.command("provider") +def provider_cmd( + action: str = typer.Option("auto", help="Action: on, off, or auto (toggle)"), + config: str = typer.Option(str(opencode.CONFIG_PATH), "--config", help="Path to opencode.jsonc"), +) -> None: + """Toggle opencode provider in opencode.jsonc.""" + if action not in ("on", "off", "auto"): + console.print("[red]Invalid action: use 'on', 'off', or 'auto'[/red]") + raise typer.Exit(code=1) + new_state = opencode.toggle(action=action, path=Path(config)) + status = "[green]enabled[/green]" if new_state else "[red]disabled[/red]" + console.print(f"Provider {status}") + + warp_app = typer.Typer( help="Manage Cloudflare WARP", no_args_is_help=True, rich_markup_mode="rich" ) diff --git a/openrot/opencode.py b/openrot/opencode.py new file mode 100644 index 0000000..52b5bb8 --- /dev/null +++ b/openrot/opencode.py @@ -0,0 +1,50 @@ +"""Toggle opencode provider in opencode.jsonc.""" + +import json +import shutil +from pathlib import Path + +import json5 + +CONFIG_PATH = Path.home() / ".config" / "opencode" / "opencode.jsonc" +OPENCODE_PROVIDER: dict = {"options": {"baseURL": "http://127.0.0.1:7891/v1"}} +SCHEMA = "https://opencode.ai/config.json" + + +def _read(path: Path) -> dict: + if not path.exists(): + return {"$schema": SCHEMA} + return json5.loads(path.read_text()) + + +def _write(data: dict, path: Path) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(data, indent="\t", ensure_ascii=False) + "\n") + + +def _backup(path: Path) -> None: + if path.exists(): + shutil.copy2(path, path.with_suffix(".jsonc.bak")) + + +def is_enabled(data: dict) -> bool: + return "opencode" in data.get("provider", {}) + + +def toggle(action: str = "auto", path: Path = CONFIG_PATH) -> bool: + """Toggle the opencode provider. Returns new state (True=on, False=off).""" + _backup(path) + data = _read(path) + current = is_enabled(data) + new_state = not current if action == "auto" else action == "on" + + data.setdefault("provider", {}) + if new_state: + data["provider"]["opencode"] = OPENCODE_PROVIDER + else: + data["provider"].pop("opencode", None) + if not data["provider"]: + data.pop("provider", None) + + _write(data, path) + return new_state diff --git a/poetry.lock b/poetry.lock index 7c1b3e2..c794661 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.4.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.3.2 and should not be changed by hand. [[package]] name = "altgraph" @@ -423,6 +423,21 @@ files = [ {file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"}, ] +[[package]] +name = "json5" +version = "0.9.28" +description = "A Python implementation of the JSON5 data format." +optional = false +python-versions = ">=3.8.0" +groups = ["main"] +files = [ + {file = "json5-0.9.28-py3-none-any.whl", hash = "sha256:29c56f1accdd8bc2e037321237662034a7e07921e2b7223281a5ce2c46f0c4df"}, + {file = "json5-0.9.28.tar.gz", hash = "sha256:1f82f36e615bc5b42f1bbd49dbc94b12563c56408c6ffa06414ea310890e9a6e"}, +] + +[package.extras] +dev = ["build (==1.2.2.post1)", "coverage (==7.5.3)", "mypy (==1.13.0)", "pip (==24.3.1)", "pylint (==3.2.3)", "ruff (==0.7.3)", "twine (==5.1.1)", "uv (==0.5.1)"] + [[package]] name = "librt" version = "0.15.0" @@ -1361,4 +1376,4 @@ python-discovery = ">=1.6" [metadata] lock-version = "2.1" python-versions = ">=3.14" -content-hash = "b8cc116032831aff4260418094815c65af0c8e99957c6ac1d78ea26cc535a27a" +content-hash = "49f0d995b2145b043958a8691cde9cff4bc18d59c74cfa73298fe9f98ce2cde8" diff --git a/pyproject.toml b/pyproject.toml index 3fa8769..c7d8762 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ "pyyaml>=6.0,<7", "pydantic>=2.0,<3", "httpx[socks]>=0.27,<0.28", + "json5>=0.9,<0.10", ] [project.scripts] From d94f4e8ca78dd7e4efb5e541554d2c3f08b3f153 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 1 Sep 2026 14:57:56 +0300 Subject: [PATCH 05/22] fix: :bug: use tag with v prefix in self-update download filename --- openrot/cli.py | 7 ++++--- openrot/opencode.py | 12 ++++++++---- openrot/self_update.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/openrot/cli.py b/openrot/cli.py index b3c7f87..9ff30bf 100644 --- a/openrot/cli.py +++ b/openrot/cli.py @@ -21,7 +21,7 @@ ) from rich.table import Table -from openrot import __version__, self_update, signals +from openrot import __version__, opencode, self_update, signals from openrot import config as cfg from openrot.config import ( ActiveLevel, @@ -33,7 +33,6 @@ from openrot.core import nodes as node_ops from openrot.core import probe as probe_core from openrot.providers import warp -from openrot import opencode app = typer.Typer( help=( @@ -759,7 +758,9 @@ def config_edit() -> None: @app.command("provider") def provider_cmd( action: str = typer.Option("auto", help="Action: on, off, or auto (toggle)"), - config: str = typer.Option(str(opencode.CONFIG_PATH), "--config", help="Path to opencode.jsonc"), + config: str = typer.Option( + str(opencode.CONFIG_PATH), "--config", help="Path to opencode.jsonc" + ), ) -> None: """Toggle opencode provider in opencode.jsonc.""" if action not in ("on", "off", "auto"): diff --git a/openrot/opencode.py b/openrot/opencode.py index 52b5bb8..ed6ca8f 100644 --- a/openrot/opencode.py +++ b/openrot/opencode.py @@ -1,23 +1,26 @@ """Toggle opencode provider in opencode.jsonc.""" +from __future__ import annotations + import json import shutil from pathlib import Path +from typing import Any import json5 CONFIG_PATH = Path.home() / ".config" / "opencode" / "opencode.jsonc" -OPENCODE_PROVIDER: dict = {"options": {"baseURL": "http://127.0.0.1:7891/v1"}} +OPENCODE_PROVIDER: dict[str, Any] = {"options": {"baseURL": "http://127.0.0.1:7891/v1"}} SCHEMA = "https://opencode.ai/config.json" -def _read(path: Path) -> dict: +def _read(path: Path) -> dict[str, Any]: if not path.exists(): return {"$schema": SCHEMA} return json5.loads(path.read_text()) -def _write(data: dict, path: Path) -> None: +def _write(data: dict[str, Any], path: Path) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(data, indent="\t", ensure_ascii=False) + "\n") @@ -27,7 +30,8 @@ def _backup(path: Path) -> None: shutil.copy2(path, path.with_suffix(".jsonc.bak")) -def is_enabled(data: dict) -> bool: +def is_enabled(data: dict[str, Any]) -> bool: + """Check if opencode provider is enabled in config.""" return "opencode" in data.get("provider", {}) diff --git a/openrot/self_update.py b/openrot/self_update.py index a1fbc33..4e2b92c 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -181,7 +181,7 @@ def _do_update( ) version_str = latest_tag.lstrip("v") - filename = f"openrot-{version_str}-{_current_os()}-{_current_arch()}.tar.gz" + filename = f"openrot-{latest_tag}-{_current_os()}-{_current_arch()}.tar.gz" url = f"{DOWNLOAD_BASE}/{latest_tag}/{filename}" dest_dir = _bin_dir() tmp = tempfile.mkdtemp(prefix="openrot-update-") From ffb4c1fa5135e6f556bb7f5bd758102a1c90f2c4 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Wed, 2 Sep 2026 11:52:44 +0300 Subject: [PATCH 06/22] fix: avoid rich live conflicts in warp connect and hide ctrl-c tip in daemon warp.connect() used console.status (a rich Live), which crashes with 'Only one live display may be active at once' when the bridge rotates WARP from a concurrent request thread. Replace it with a plain print + polling loop that is thread-safe and keeps the foreground UX. serve() printed 'Ctrl-C to stop.' unconditionally, so the daemon child (re-runs 'start bridge' with stdout redirected to the log file) wrote a misleading interactive tip into openrot-bridge.log. Gate it on sys.stdout.isatty() and add regression tests for both fixes. --- openrot/core/bridge.py | 4 ++- openrot/providers/warp.py | 10 +++--- tests/unit/core/test_bridge.py | 56 +++++++++++++++++++++++++++++++ tests/unit/providers/test_warp.py | 15 +++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 2b18fb0..8d0e5c8 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -20,6 +20,7 @@ import contextlib import json import socket +import sys from collections.abc import Callable from dataclasses import dataclass from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer @@ -220,7 +221,8 @@ def serve() -> None: f"bridge: [green]listening[/green] on {url} " f"(upstream {cfg_obj.bridge_upstream}, level {cfg_obj.active_level.value})" ) - console.print("Ctrl-C to stop.") + if sys.stdout.isatty(): + console.print("Ctrl-C to stop.") host = cfg.listen_address() warn_if_exposed(host) signals.keyboard_on_sigterm() diff --git a/openrot/providers/warp.py b/openrot/providers/warp.py index 933a73e..b30a25e 100644 --- a/openrot/providers/warp.py +++ b/openrot/providers/warp.py @@ -87,11 +87,11 @@ def connect() -> bool: _run("mode", "proxy") _run("proxy", "port", str(port)) _run("connect") - with console.status("waiting for WARP connection..."): - for _ in range(120): - if is_connected(): - return True - time.sleep(0.5) + console.print("waiting for WARP connection...") + for _ in range(120): + if is_connected(): + return True + time.sleep(0.5) return False diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index e9a04c9..4c0a45c 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -388,6 +388,62 @@ def server_close(self) -> None: assert any("SECURITY" in msg for msg in printed) +class _FakeStdout: + def __init__(self, tty: bool) -> None: + self._tty = tty + + def isatty(self) -> bool: + return self._tty + + +def test_serve_hides_ctrl_c_tip_when_not_a_tty(monkeypatch: pytest.MonkeyPatch) -> None: + class FakeServer: + def __init__(self, host: str, port: int) -> None: + self.host = host + self.port = port + + def serve_forever(self) -> None: + raise KeyboardInterrupt + + def server_close(self) -> None: + pass + + printed: list[str] = [] + monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) + monkeypatch.setattr(bridge, "Bridge", FakeServer) + + bridge.serve() + + assert not any("Ctrl-C to stop." in msg for msg in printed) + + +def test_serve_shows_ctrl_c_tip_on_a_tty(monkeypatch: pytest.MonkeyPatch) -> None: + class FakeServer: + def __init__(self, host: str, port: int) -> None: + self.host = host + self.port = port + + def serve_forever(self) -> None: + raise KeyboardInterrupt + + def server_close(self) -> None: + pass + + printed: list[str] = [] + monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) + monkeypatch.setattr(bridge, "Bridge", FakeServer) + + bridge.serve() + + assert any("Ctrl-C to stop." in msg for msg in printed) + + def test_respond_streams_body_and_skips_hop_by_hop() -> None: from io import BytesIO from types import SimpleNamespace diff --git a/tests/unit/providers/test_warp.py b/tests/unit/providers/test_warp.py index 3fcd255..efc251d 100644 --- a/tests/unit/providers/test_warp.py +++ b/tests/unit/providers/test_warp.py @@ -46,6 +46,21 @@ def test_connect_sets_proxy_mode(monkeypatch: pytest.MonkeyPatch) -> None: assert ("proxy", "port", str(w.WARP_PROXY_PORT)) in calls +def test_connect_does_not_use_rich_live(monkeypatch: pytest.MonkeyPatch) -> None: + _patch(monkeypatch, [w.WarpStatus.CONNECTED]) + + def exploding_status(*args: object, **kwargs: object) -> object: + raise AssertionError("connect() must not start a rich Live") + + monkeypatch.setattr(w.console, "status", exploding_status) + printed: list[str] = [] + + monkeypatch.setattr(w.console, "print", lambda *a, **k: printed.append(a[0])) + + assert w.connect() is True + assert "waiting for WARP connection..." in printed + + def test_proxy_address_default() -> None: assert w.proxy_address() == (w.WARP_PROXY_HOST, w.WARP_PROXY_PORT) From 47a49f991c08cf97bec118e8634eaeeefa6ece83 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Wed, 2 Sep 2026 13:11:21 +0300 Subject: [PATCH 07/22] feat: :sparkles: stop daemons before self-update and restart after --- openrot/core/daemon.py | 34 +++++++++++++++ openrot/self_update.py | 36 ++++++++++++++++ tests/unit/core/test_daemon.py | 55 ++++++++++++++++++++++++ tests/unit/test_self_update.py | 78 ++++++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+) diff --git a/openrot/core/daemon.py b/openrot/core/daemon.py index 900d50a..e86eec3 100644 --- a/openrot/core/daemon.py +++ b/openrot/core/daemon.py @@ -10,6 +10,7 @@ import signal import subprocess import sys +import time from pathlib import Path from rich.console import Console @@ -71,3 +72,36 @@ def stop(pid_path: Path) -> bool: return False pid_path.unlink(missing_ok=True) return True + + +def stop_and_wait(pid_path: Path) -> bool: + """Terminate a background daemon and wait for it to exit. + + Returns True if the daemon was running and has been stopped. + """ + pid = load_daemon_pid(pid_path) + if pid is None or not is_running(pid): + return False + try: + os.kill(pid, signal.SIGTERM) + except OSError: + return False + for _ in range(50): + if not is_running(pid): + pid_path.unlink(missing_ok=True) + return True + time.sleep(0.1) + return False + + +def daemon_start_background(name: str, pid_path: Path, log_path: Path) -> None: + """Start a daemon in a detached background process (for restart after update).""" + with log_path.open("ab") as log_f: + log_path.chmod(0o600) + proc = subprocess.Popen( # noqa: S603 + command(name), + stdout=log_f, + stderr=subprocess.STDOUT, + start_new_session=True, + ) + save_daemon_pid(proc.pid, path=pid_path) diff --git a/openrot/self_update.py b/openrot/self_update.py index 4e2b92c..2ac3fe3 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -14,12 +14,44 @@ import httpx from openrot import __version__ +from openrot import config as cfg +from openrot.core import daemon, proxy GH_REPO = "vispar-tech/openrot" RELEASE_URL = f"https://api.github.com/repos/{GH_REPO}/releases/latest" DOWNLOAD_BASE = f"https://github.com/{GH_REPO}/releases/download" +class ServiceState(NamedTuple): + """Tracks whether openrot daemon/proxy were running before an update.""" + + daemon_running: bool + proxy_running: bool + + +def _check_services() -> ServiceState: + """Check if any openrot daemon/proxy processes are currently running.""" + daemon_pid = daemon.load_daemon_pid(cfg.DAEMON_PID_PATH) + proxy_pid = proxy.load_pid(cfg.PID_PATH) + return ServiceState( + daemon_running=daemon_pid is not None and proxy.is_running(daemon_pid), + proxy_running=proxy_pid is not None and proxy.is_running(proxy_pid), + ) + + +def _stop_services() -> None: + """Stop all openrot daemon and proxy processes gracefully.""" + proxy.stop_proxy() + daemon.stop_and_wait(cfg.DAEMON_PID_PATH) + daemon.stop_and_wait(cfg.BRIDGE_PID_PATH) + + +def _restart_services(state: ServiceState) -> None: + """Restart openrot services that were running before the update.""" + if state.daemon_running: + daemon.daemon_start_background("cascade", cfg.DAEMON_PID_PATH, cfg.LOG_PATH) + + class UpdateResult(NamedTuple): """Result of a self-update check or install operation.""" @@ -113,6 +145,9 @@ def perform_update( progress_fn: object | None = None, ) -> UpdateResult: """Download and install the latest release, replacing the current binary.""" + state = _check_services() + if state.daemon_running or state.proxy_running: + _stop_services() close = client is None if client is None: client = httpx.Client(follow_redirects=True) @@ -121,6 +156,7 @@ def perform_update( finally: if close: client.close() + _restart_services(state) def _download_archive( diff --git a/tests/unit/core/test_daemon.py b/tests/unit/core/test_daemon.py index aa5ee83..2fbc1ef 100644 --- a/tests/unit/core/test_daemon.py +++ b/tests/unit/core/test_daemon.py @@ -186,3 +186,58 @@ def boom(*a: object) -> object: monkeypatch.setattr(daemon.os, "kill", boom) assert daemon.stop(pid_file) is False assert pid_file.exists() + + +def test_stop_and_wait_terminates_and_waits( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + killed: list[tuple[int, int]] = [] + pid_file = tmp_path / "bridge.pid" + pid_file.write_text("42\n") + call_count = 0 + + def mock_is_running(pid: int) -> bool: + nonlocal call_count + call_count += 1 + # First two calls: still running; third call: dead + return call_count < 3 + + monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 42) + monkeypatch.setattr(daemon, "is_running", mock_is_running) + monkeypatch.setattr(daemon.os, "kill", lambda *a: killed.append(a)) + monkeypatch.setattr(daemon.time, "sleep", lambda _: None) + + assert daemon.stop_and_wait(pid_file) is True + assert killed == [(42, daemon.signal.SIGTERM)] + assert not pid_file.exists() + assert call_count == 3 + + +def test_stop_and_wait_timeout( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pid_file = tmp_path / "bridge.pid" + pid_file.write_text("42\n") + monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 42) + monkeypatch.setattr(daemon, "is_running", lambda pid: True) + monkeypatch.setattr(daemon.os, "kill", lambda *a: None) + monkeypatch.setattr(daemon.time, "sleep", lambda _: None) + + assert daemon.stop_and_wait(pid_file) is False + + +def test_stop_and_wait_noop_when_absent( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: None) + assert daemon.stop_and_wait(tmp_path / "nope.pid") is False + + +def test_stop_and_wait_noop_when_dead( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pid_file = tmp_path / "bridge.pid" + pid_file.write_text("999999\n") + monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 999999) + monkeypatch.setattr(daemon, "is_running", lambda pid: False) + assert daemon.stop_and_wait(pid_file) is False diff --git a/tests/unit/test_self_update.py b/tests/unit/test_self_update.py index 1c6be78..a2e9737 100644 --- a/tests/unit/test_self_update.py +++ b/tests/unit/test_self_update.py @@ -192,3 +192,81 @@ def test_fields(self) -> None: assert r.latest == "2.0.0" assert r.updated is True assert r.message == "ok" + + +class TestServiceState: + def test_fields(self) -> None: + s = su.ServiceState(daemon_running=True, proxy_running=False) + assert s.daemon_running is True + assert s.proxy_running is False + + +class TestCheckServices: + def test_nothing_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: None) + monkeypatch.setattr(su.proxy, "load_pid", lambda _: None) + state = su._check_services() + assert state.daemon_running is False + assert state.proxy_running is False + + def test_daemon_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: 123) + monkeypatch.setattr(su.proxy, "load_pid", lambda _: None) + monkeypatch.setattr(su.proxy, "is_running", lambda _: True) + state = su._check_services() + assert state.daemon_running is True + assert state.proxy_running is False + + def test_proxy_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: None) + monkeypatch.setattr(su.proxy, "load_pid", lambda _: 456) + monkeypatch.setattr(su.proxy, "is_running", lambda _: True) + state = su._check_services() + assert state.daemon_running is False + assert state.proxy_running is True + + def test_both_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: 123) + monkeypatch.setattr(su.proxy, "load_pid", lambda _: 456) + monkeypatch.setattr(su.proxy, "is_running", lambda _: True) + state = su._check_services() + assert state.daemon_running is True + assert state.proxy_running is True + + +class TestStopServices: + def test_stops_all(self, monkeypatch: pytest.MonkeyPatch) -> None: + stopped: list[str] = [] + monkeypatch.setattr( + su.proxy, "stop_proxy", lambda: stopped.append("proxy") or True + ) + monkeypatch.setattr( + su.daemon, + "stop_and_wait", + lambda p: stopped.append(str(p)) or True, + ) + su._stop_services() + assert "proxy" in stopped + assert len(stopped) == 3 # proxy + daemon PID + bridge PID + + +class TestRestartServices: + def test_restarts_daemon(self, monkeypatch: pytest.MonkeyPatch) -> None: + started: list[str] = [] + monkeypatch.setattr( + su.daemon, + "daemon_start_background", + lambda name, pid, log: started.append(name), + ) + su._restart_services(su.ServiceState(daemon_running=True, proxy_running=False)) + assert started == ["cascade"] + + def test_no_restart_when_not_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + started: list[str] = [] + monkeypatch.setattr( + su.daemon, + "daemon_start_background", + lambda name, pid, log: started.append(name), + ) + su._restart_services(su.ServiceState(daemon_running=False, proxy_running=False)) + assert started == [] From 2400f85e0f4e8532f4d520eeafcf7535fb029c09 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Wed, 2 Sep 2026 13:19:35 +0300 Subject: [PATCH 08/22] style: :art: ruff format --- tests/unit/core/test_daemon.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/core/test_daemon.py b/tests/unit/core/test_daemon.py index 2fbc1ef..cebdd5c 100644 --- a/tests/unit/core/test_daemon.py +++ b/tests/unit/core/test_daemon.py @@ -213,9 +213,7 @@ def mock_is_running(pid: int) -> bool: assert call_count == 3 -def test_stop_and_wait_timeout( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: +def test_stop_and_wait_timeout(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: pid_file = tmp_path / "bridge.pid" pid_file.write_text("42\n") monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 42) From a916907e68febda6f83c95f25fc4a8ee7ee5937b Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Thu, 3 Sep 2026 11:20:33 +0300 Subject: [PATCH 09/22] feat: :sparkles: log model, prompt length, and response time in bridge events --- openrot/core/bridge.py | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 8d0e5c8..6df0f2a 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -21,6 +21,7 @@ import json import socket import sys +import time from collections.abc import Callable from dataclasses import dataclass from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer @@ -257,8 +258,28 @@ class BridgeHandler(BaseHTTPRequestHandler): def _handle(self, method: str) -> None: length = int(self.headers.get("Content-Length", 0) or 0) body = self.rfile.read(length) if length else b"" + + model = "" + prompt_chars = 0 + if body: + try: + parsed = json.loads(body) + model = parsed.get("model", "") + messages = parsed.get("messages", []) + if isinstance(messages, list): + prompt_chars = sum( + len(m.get("content", "")) if isinstance(m, dict) else 0 + for m in messages + ) + except json.JSONDecodeError, AttributeError: + pass + + self._model = model + self._prompt_chars = prompt_chars + cfg_obj = cfg.load_config() request = _Request(method, self.path, dict(self.headers.items()), body) + t0 = time.monotonic() try: resp, client = forward(cfg_obj, request) except UpstreamError as exc: @@ -269,6 +290,7 @@ def _handle(self, method: str) -> None: cfg_obj = cfg.load_config() resp, client = forward(cfg_obj, request, rotate_on_429=False) except UpstreamError as retry_exc: + self._elapsed_ms = (time.monotonic() - t0) * 1000 msg = {"error": {"message": str(retry_exc), "type": "upstream"}} payload = json.dumps(msg).encode() self.send_response(502) @@ -280,6 +302,7 @@ def _handle(self, method: str) -> None: try: _respond(self, resp) finally: + self._elapsed_ms = (time.monotonic() - t0) * 1000 client.close() def do_GET(self) -> None: # noqa: D102 @@ -305,7 +328,27 @@ def do_HEAD(self) -> None: # noqa: D102 def log_message(self, fmt: str, *args: object) -> None: """Log a request line to the events log.""" - events.info("bridge %s", fmt % args) + parts = [fmt % args] + + model = getattr(self, "_model", "") + if model: + parts.append(f"model={model}") + + prompt_chars = getattr(self, "_prompt_chars", 0) + if prompt_chars: + if prompt_chars >= 1000: + parts.append(f"prompt={prompt_chars / 1000:.1f}k") + else: + parts.append(f"prompt={prompt_chars}") + + elapsed = getattr(self, "_elapsed_ms", 0) + if elapsed: + if elapsed >= 1000: + parts.append(f"{elapsed / 1000:.1f}s") + else: + parts.append(f"{elapsed:.0f}ms") + + events.info("bridge %s", " ".join(parts)) class Bridge(ThreadingHTTPServer): From 7817ab00d4f85fb809184a6a3ccc20fff739b62f Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Thu, 3 Sep 2026 17:47:15 +0300 Subject: [PATCH 10/22] fix: :bug: serialize WARP rotation to prevent concurrent races --- openrot/core/cascade.py | 14 +++++++++++ tests/unit/core/test_cascade.py | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 8e5ddc3..b36d473 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -14,6 +14,7 @@ console = Console() events = log.get_logger() +_rotate_lock = threading.Lock() def launch(cfg_obj: Config, node: Node) -> int: @@ -255,7 +256,20 @@ def rotate(first: bool = False) -> None: ``first=True`` resets the queue to the start: serve the first node of the chain (no exclusion of the current one). + + Thread-safe: concurrent calls are serialized; when a rotation is already + in progress the caller returns immediately without blocking. """ + if not _rotate_lock.acquire(blocking=False): + events.warning("rotate skipped: another rotation already in progress") + return + try: + _rotate_inner(first) + finally: + _rotate_lock.release() + + +def _rotate_inner(first: bool) -> None: cfg_obj = cfg.load_config() if cfg_obj.active_level == ActiveLevel.WARP: if warp.rotate(): diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index e0c06d4..14b132c 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -571,3 +571,46 @@ def fake_is_connected() -> bool: cascade.warp_health_loop() assert call_count["n"] == 2 + + +def test_rotate_skips_when_already_in_progress( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A concurrent rotate() returns immediately while another is running.""" + rotate_started = threading.Event() + rotate_proceed = threading.Event() + call_count = {"n": 0} + + def slow_warp_rotate() -> bool: + call_count["n"] += 1 + rotate_started.set() + rotate_proceed.wait(timeout=5) + return True + + conf = Config(update_interval=0, active_level=ActiveLevel.WARP) + monkeypatch.setattr(cascade.cfg, "load_config", lambda: conf) + monkeypatch.setattr(cascade.warp, "rotate", slow_warp_rotate) + monkeypatch.setattr(cascade.warp, "current_ip", lambda: "1.1.1.1") + _patch_events(monkeypatch) + + results: list[str] = [] + + def first_rotate() -> None: + cascade.rotate() + results.append("done") + + def second_rotate() -> None: + rotate_started.wait(timeout=5) + cascade.rotate() + results.append("skipped") + + t1 = threading.Thread(target=first_rotate) + t2 = threading.Thread(target=second_rotate) + t1.start() + t2.start() + rotate_proceed.set() + t1.join(timeout=5) + t2.join(timeout=5) + + assert call_count["n"] == 1 + assert sorted(results) == ["done", "skipped"] From 3347b656ee503c8499a10735147e78bc03839ad7 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Thu, 3 Sep 2026 18:18:21 +0300 Subject: [PATCH 11/22] fix: :bug: fix BrokenPipeError in bridge headers and make bridge logging consistent --- openrot/config.py | 2 +- openrot/core/bridge.py | 69 ++++++++++++++++------ tests/unit/core/test_bridge.py | 104 +++++++++++++++++++++++++++++---- 3 files changed, 143 insertions(+), 32 deletions(-) diff --git a/openrot/config.py b/openrot/config.py index d89e3f8..a204f93 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -4,7 +4,7 @@ from contextlib import contextmanager from pathlib import Path -import yaml # type: ignore[import-untyped] +import yaml try: import fcntl diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 6df0f2a..e20745e 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -28,7 +28,6 @@ from urllib.parse import urljoin import httpx -from rich.console import Console from openrot import config as cfg from openrot import signals @@ -37,9 +36,14 @@ from openrot.log import get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM -console = Console() events = get_logger() + +def _log(msg: str) -> None: + """Log a message through the events logger with consistent formatting.""" + events.info(msg) + + # Hop-by-hop headers never forwarded upstream. _HOP_BY_HOP = { "connection", @@ -110,8 +114,11 @@ def _is_rate_limited(resp: httpx.Response, statuses: list[int]) -> bool: def _rotate_and_retry(cfg_obj: Config, request: _Request) -> httpx.Response: """Rotate the cascade once, then retry the request once.""" events.warning("upstream retryable status, rotating cascade") + t0 = time.monotonic() with contextlib.suppress(SystemExit): # no alive node available cascade.rotate() + elapsed = time.monotonic() - t0 + _log(f"[warp] rotation took {elapsed:.1f}s") with _client(cfg_obj) as client: return _fetch(client, cfg_obj, request) @@ -148,14 +155,14 @@ def forward( def _respond(handler: BaseHTTPRequestHandler, resp: httpx.Response) -> None: """Write an upstream response back to the connected client, streaming the body.""" - handler.send_response(resp.status_code) - for key, value in resp.headers.items(): - if key.lower() in _HOP_BY_HOP: - continue - with contextlib.suppress(ValueError, OSError): - handler.send_header(key, value) - handler.end_headers() try: + handler.send_response(resp.status_code) + for key, value in resp.headers.items(): + if key.lower() in _HOP_BY_HOP: + continue + with contextlib.suppress(ValueError, OSError): + handler.send_header(key, value) + handler.end_headers() for chunk in resp.iter_bytes(): handler.wfile.write(chunk) handler.wfile.flush() @@ -180,11 +187,11 @@ def warn_if_exposed(host: str) -> None: """ if _is_loopback(host): return - console.print( - f"[bold red]SECURITY[/bold red] bridge binds to {host!r}, reachable " - "from other machines. It forwards your Authorization headers, so keep " - "it on 127.0.0.1 (the default). Open ports to the outside world only " - "if you truly intend to (Docker port publish / OPENROT_LISTEN)." + _log( + f"SECURITY: bridge binds to {host!r}, reachable from other machines. " + "It forwards your Authorization headers, so keep it on 127.0.0.1 " + "(the default). Open ports to the outside world only if you truly " + "intend to (Docker port publish / OPENROT_LISTEN)." ) @@ -213,17 +220,17 @@ def serve() -> None: """ cfg_obj = cfg.load_config() if not _running_level(cfg_obj, cascade.level_serving): - console.print("no active level, starting cascade...") + _log("no active level, starting cascade...") cascade.start(False, False) cfg_obj = cfg.load_config() url = base_url(cfg_obj) - console.print( - f"bridge: [green]listening[/green] on {url} " + _log( + f"bridge: listening on {url} " f"(upstream {cfg_obj.bridge_upstream}, level {cfg_obj.active_level.value})" ) if sys.stdout.isatty(): - console.print("Ctrl-C to stop.") + _log("Ctrl-C to stop.") host = cfg.listen_address() warn_if_exposed(host) signals.keyboard_on_sigterm() @@ -231,7 +238,7 @@ def serve() -> None: try: server.serve_forever() except KeyboardInterrupt: - console.print("\nstopping bridge...") + _log("\nstopping bridge...") finally: server.server_close() @@ -255,6 +262,25 @@ class BridgeHandler(BaseHTTPRequestHandler): server_version = "openrot-bridge/1" + def _log_request_console(self, method: str) -> None: + elapsed_s = self._elapsed_ms / 1000 + parts = [f"{method} {self.path}"] + model = getattr(self, "_model", "") + if model: + parts.append(model) + prompt_chars = getattr(self, "_prompt_chars", 0) + if prompt_chars: + parts.append( + f"prompt={prompt_chars / 1000:.1f}k" + if prompt_chars >= 1000 + else f"prompt={prompt_chars}" + ) + if elapsed_s >= 1: + parts.append(f"{elapsed_s:.1f}s") + else: + parts.append(f"{self._elapsed_ms:.0f}ms") + _log(f"[bridge] {' '.join(parts)}") + def _handle(self, method: str) -> None: length = int(self.headers.get("Content-Length", 0) or 0) body = self.rfile.read(length) if length else b"" @@ -284,13 +310,17 @@ def _handle(self, method: str) -> None: resp, client = forward(cfg_obj, request) except UpstreamError as exc: events.warning("bridge upstream error, rotating and retrying: %s", exc) + t_rotate = time.monotonic() with contextlib.suppress(SystemExit): cascade.rotate() + elapsed_rot = time.monotonic() - t_rotate + _log(f"[warp] rotation took {elapsed_rot:.1f}s") try: cfg_obj = cfg.load_config() resp, client = forward(cfg_obj, request, rotate_on_429=False) except UpstreamError as retry_exc: self._elapsed_ms = (time.monotonic() - t0) * 1000 + self._log_request_console(method) msg = {"error": {"message": str(retry_exc), "type": "upstream"}} payload = json.dumps(msg).encode() self.send_response(502) @@ -304,6 +334,7 @@ def _handle(self, method: str) -> None: finally: self._elapsed_ms = (time.monotonic() - t0) * 1000 client.close() + self._log_request_console(method) def do_GET(self) -> None: # noqa: D102 self._handle("GET") diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 4c0a45c..a576e10 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -105,7 +105,9 @@ def make_client(cfg: Config) -> _FakeClient: monkeypatch.setattr(bridge, "_client", make_client) rotated: list = [] + logged: list[str] = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) resp, client = bridge.forward(_cfg(), _req()) try: @@ -114,6 +116,7 @@ def make_client(cfg: Config) -> _FakeClient: assert resp.content == b"recovered" finally: client.close() + assert any("rotation took" in msg for msg in logged) def test_forward_no_rotate_when_disabled( @@ -345,22 +348,22 @@ def test_running_level_combinations() -> None: def test_warn_if_exposed_silent_on_loopback(monkeypatch: pytest.MonkeyPatch) -> None: - printed: list[str] = [] - monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + logged: list[str] = [] + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) bridge.warn_if_exposed("127.0.0.1") bridge.warn_if_exposed("localhost") - assert printed == [] + assert logged == [] def test_warn_if_exposed_warns_on_non_loopback( monkeypatch: pytest.MonkeyPatch, ) -> None: - printed: list[str] = [] - monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + logged: list[str] = [] + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) bridge.warn_if_exposed("0.0.0.0") bridge.warn_if_exposed("192.168.1.20") - assert len(printed) == 2 - assert all("SECURITY" in msg for msg in printed) + assert len(logged) == 2 + assert all("SECURITY" in msg for msg in logged) def test_serve_warns_when_binding_beyond_loopback( @@ -376,16 +379,16 @@ def serve_forever(self) -> None: def server_close(self) -> None: pass - printed: list[str] = [] + logged: list[str] = [] monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) - monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) monkeypatch.setattr(bridge.cfg, "listen_address", lambda: "0.0.0.0") monkeypatch.setattr(bridge, "Bridge", FakeServer) bridge.serve() - assert any("SECURITY" in msg for msg in printed) + assert any("SECURITY" in msg for msg in logged) class _FakeStdout: @@ -411,7 +414,7 @@ def server_close(self) -> None: printed: list[str] = [] monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) - monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -435,7 +438,7 @@ def server_close(self) -> None: printed: list[str] = [] monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) - monkeypatch.setattr(bridge.console, "print", lambda *a, **k: printed.append(a[0])) + monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -516,6 +519,83 @@ def boom(cfg_obj: object, request: object, **kwargs: object) -> object: assert b'"upstream"' in written[0] +def test_handler_logs_request_elapsed_and_model( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + body = b'{"model":"gpt-4o","messages":[{"content":"hello world"}]}' + handler = bridge.BridgeHandler.__new__(bridge.BridgeHandler) + handler.headers = SimpleNamespace(get=lambda k, d=0: len(body), items=list) + handler.rfile = SimpleNamespace(read=lambda n: body) + handler.path = "/v1/chat/completions" + handler.wfile = SimpleNamespace(write=lambda b: None) + handler.send_response = lambda code: None + handler.send_header = lambda k, v: None + handler.end_headers = lambda: None + + logged: list[str] = [] + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge, "_respond", lambda handler, resp: None) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + + resp = httpx.Response(200, content=b"ok") + client = _FakeClient([resp]) + + def fake_forward(cfg_obj: object, request: object, **kw: object) -> object: + return (resp, client) + + monkeypatch.setattr(bridge, "forward", fake_forward) + handler._handle("POST") + + line = next(msg for msg in logged if msg.startswith("[bridge] POST")) + assert "gpt-4o" in line + assert "model=gpt-4o" not in line + assert "prompt=" in line + assert any(k in line for k in ("ms", "s")) + + +def test_handler_rotates_and_retries_after_upstream_error( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + handler = bridge.BridgeHandler.__new__(bridge.BridgeHandler) + handler.headers = SimpleNamespace(get=lambda k, d=0: 0, items=list) + handler.rfile = SimpleNamespace(read=lambda n: b"") + handler.path = "/v1/chat/completions" + handler.wfile = SimpleNamespace(write=lambda b: None) + handler.send_response = lambda code: None + handler.send_header = lambda k, v: None + handler.end_headers = lambda: None + + logged: list[str] = [] + rotated: list = [] + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + monkeypatch.setattr(bridge, "_respond", lambda handler, resp: None) + monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) + + resp = httpx.Response(200, content=b"ok") + client = _FakeClient([resp]) + calls: list[bool] = [] + + def flaky_forward(cfg_obj: object, request: object, **kw: object) -> object: + calls.append(kw.get("rotate_on_429", True)) + if not calls or len(calls) == 1: + raise bridge.UpstreamError("conn drop") + return (resp, client) + + monkeypatch.setattr(bridge, "forward", flaky_forward) + handler._handle("POST") + assert len(rotated) == 1 + assert any("rotation took" in msg for msg in logged) + assert any( + msg.startswith("[bridge] POST") and ("ms" in msg or "s" in msg) + for msg in logged + ) + + def test_bridge_server_binds_and_closes() -> None: server = bridge.Bridge("127.0.0.1", 0) try: From c661c47296491b7a280018c4cefd08799a7a5704 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Thu, 3 Sep 2026 20:08:07 +0300 Subject: [PATCH 12/22] =?UTF-8?q?fix:=20:bug:=20unify=20logging=20?= =?UTF-8?q?=E2=80=94=20single=20openrot.log=20via=20events=20logger,=20dae?= =?UTF-8?q?mon=20output=20through=20events?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openrot/cli.py | 67 +++++--------- openrot/config.py | 12 --- openrot/core/bridge.py | 55 +++++------ openrot/core/cascade.py | 70 ++++---------- openrot/core/daemon.py | 48 +++------- openrot/core/refresh.py | 4 +- openrot/log.py | 28 +----- openrot/providers/warp.py | 7 +- openrot/self_update.py | 2 +- tests/unit/cli/test_cli.py | 147 ++++-------------------------- tests/unit/core/test_bridge.py | 21 ++++- tests/unit/core/test_daemon.py | 28 +----- tests/unit/providers/test_warp.py | 7 +- tests/unit/test_log.py | 34 ++----- tests/unit/test_self_update.py | 4 +- 15 files changed, 145 insertions(+), 389 deletions(-) diff --git a/openrot/cli.py b/openrot/cli.py index 9ff30bf..18ef9a3 100644 --- a/openrot/cli.py +++ b/openrot/cli.py @@ -575,58 +575,39 @@ def logs( 50, "--tail", "-n", min=0, help="lines shown from each file before following" ), ) -> None: - """Follow the daemon, events and bridge logs, aggregated into one stream.""" - sources = { - "daemon": cfg.LOG_PATH, - "events": cfg.EVENT_LOG_PATH, - "bridge": cfg.BRIDGE_LOG_PATH, - } + """Follow the openrot log file.""" + path = cfg.LOG_PATH + if not path.exists(): + console.print(f"[yellow]no log yet: {path}[/yellow]") + return if not follow: - for name, path in sources.items(): - _print_log_tail(name, path, tail) + with path.open(errors="replace") as f: + content = f.readlines() + for line in content[-tail:]: + console.print(line.rstrip()) return signals.keyboard_on_sigterm() try: - _follow_logs(sources, tail) + _follow_log(path, tail) except KeyboardInterrupt: console.print("\nstopped following logs") -def _print_log_tail(name: str, path: Path, lines: int) -> None: - if not path.exists(): - console.print(f"[yellow]no {name} log yet: {path}[/yellow]") - return - with path.open(errors="replace") as f: - content = f.readlines() - for line in content[-lines:]: - console.print(f"[bold]{name}[/bold] | {line.rstrip()}") - - -def _follow_logs(sources: dict[str, Path], tail: int) -> None: - positions: dict[str, int] = {} - for name, path in sources.items(): - if not path.exists(): - console.print(f"[yellow]no {name} log yet: {path}[/yellow]") - positions[name] = -1 - continue - with path.open("rb") as f: - positions[name] = f.seek(0, os.SEEK_END) - lines = _read_last_lines(path, tail) - for line in lines: - console.print(f"[bold]{name}[/bold] | {line}") +def _follow_log(path: Path, tail: int) -> None: + with path.open("rb") as f: + position = f.seek(0, os.SEEK_END) + lines = _read_last_lines(path, tail) + for line in lines: + console.print(line) while True: - for name, path in sources.items(): - pos = positions.get(name) - if pos is None or pos < 0 or not path.exists(): - continue - with path.open("rb") as f: - f.seek(pos) - chunk = f.read() - positions[name] = f.tell() - if chunk: - text = chunk.decode("utf-8", errors="replace") - for line in text.splitlines(): - console.print(f"[bold]{name}[/bold] | {line}") + with path.open("rb") as f: + f.seek(position) + chunk = f.read() + position = f.tell() + if chunk: + text = chunk.decode("utf-8", errors="replace") + for line in text.splitlines(): + console.print(line) time.sleep(0.5) diff --git a/openrot/config.py b/openrot/config.py index a204f93..25aba18 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -27,11 +27,9 @@ ) __all__ = [ - "BRIDGE_LOG_PATH", "BRIDGE_PID_PATH", "CONFIG_PATH", "DAEMON_PID_PATH", - "EVENT_LOG_PATH", "LOG_PATH", "PID_PATH", "TOP_LIMIT", @@ -81,25 +79,15 @@ def _bridge_pid_path() -> Path: return _base_dir() / "openrot-bridge.daemon.pid" -def _bridge_log_path() -> Path: - return _base_dir() / "openrot-bridge.log" - - def _log_path() -> Path: return _base_dir() / "openrot.log" -def _event_log_path() -> Path: - return _base_dir() / "openrot-events.log" - - CONFIG_PATH = _config_path() PID_PATH = _pid_path() DAEMON_PID_PATH = _daemon_pid_path() BRIDGE_PID_PATH = _bridge_pid_path() -BRIDGE_LOG_PATH = _bridge_log_path() LOG_PATH = _log_path() -EVENT_LOG_PATH = _event_log_path() def _lock_for(path: Path) -> Path: diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index e20745e..646f0cb 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -28,15 +28,18 @@ from urllib.parse import urljoin import httpx +from rich.console import Console from openrot import config as cfg from openrot import signals from openrot.config import ActiveLevel, Config from openrot.core import cascade, daemon -from openrot.log import get_logger +from openrot.log import console_echo +from openrot.log import get_logger as _get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM -events = get_logger() +events = _get_logger() +console = Console() def _log(msg: str) -> None: @@ -218,6 +221,7 @@ def serve() -> None: point any OpenAI-compatible client at the loopback URL and watch it route through the cascade with 429 self-rotation. Ctrl-C stops it. """ + console_echo() cfg_obj = cfg.load_config() if not _running_level(cfg_obj, cascade.level_serving): _log("no active level, starting cascade...") @@ -230,7 +234,7 @@ def serve() -> None: f"(upstream {cfg_obj.bridge_upstream}, level {cfg_obj.active_level.value})" ) if sys.stdout.isatty(): - _log("Ctrl-C to stop.") + console.print("[dim]Ctrl-C to stop.[/dim]") host = cfg.listen_address() warn_if_exposed(host) signals.keyboard_on_sigterm() @@ -238,7 +242,7 @@ def serve() -> None: try: server.serve_forever() except KeyboardInterrupt: - _log("\nstopping bridge...") + console.print("[dim]stopping bridge...[/dim]") finally: server.server_close() @@ -248,7 +252,6 @@ def daemonize() -> None: daemon.start( name="bridge", pid_path=cfg.BRIDGE_PID_PATH, - log_path=cfg.BRIDGE_LOG_PATH, ) @@ -271,14 +274,21 @@ def _log_request_console(self, method: str) -> None: prompt_chars = getattr(self, "_prompt_chars", 0) if prompt_chars: parts.append( - f"prompt={prompt_chars / 1000:.1f}k" + f"input={prompt_chars / 1000:.1f}k" if prompt_chars >= 1000 - else f"prompt={prompt_chars}" + else f"input={prompt_chars}" + ) + output_chars = getattr(self, "_output_chars", 0) + if output_chars: + parts.append( + f"output={output_chars / 1000:.1f}k" + if output_chars >= 1000 + else f"output={output_chars}" ) if elapsed_s >= 1: - parts.append(f"{elapsed_s:.1f}s") + parts.append(f"time={elapsed_s:.1f}s") else: - parts.append(f"{self._elapsed_ms:.0f}ms") + parts.append(f"time={self._elapsed_ms:.0f}ms") _log(f"[bridge] {' '.join(parts)}") def _handle(self, method: str) -> None: @@ -320,6 +330,7 @@ def _handle(self, method: str) -> None: resp, client = forward(cfg_obj, request, rotate_on_429=False) except UpstreamError as retry_exc: self._elapsed_ms = (time.monotonic() - t0) * 1000 + self._output_chars = 0 self._log_request_console(method) msg = {"error": {"message": str(retry_exc), "type": "upstream"}} payload = json.dumps(msg).encode() @@ -329,6 +340,7 @@ def _handle(self, method: str) -> None: self.end_headers() self.wfile.write(payload) return + self._output_chars = int(resp.headers.get("content-length", 0) or 0) try: _respond(self, resp) finally: @@ -357,29 +369,8 @@ def do_PATCH(self) -> None: # noqa: D102 def do_HEAD(self) -> None: # noqa: D102 self._handle("HEAD") - def log_message(self, fmt: str, *args: object) -> None: - """Log a request line to the events log.""" - parts = [fmt % args] - - model = getattr(self, "_model", "") - if model: - parts.append(f"model={model}") - - prompt_chars = getattr(self, "_prompt_chars", 0) - if prompt_chars: - if prompt_chars >= 1000: - parts.append(f"prompt={prompt_chars / 1000:.1f}k") - else: - parts.append(f"prompt={prompt_chars}") - - elapsed = getattr(self, "_elapsed_ms", 0) - if elapsed: - if elapsed >= 1000: - parts.append(f"{elapsed / 1000:.1f}s") - else: - parts.append(f"{elapsed:.0f}ms") - - events.info("bridge %s", " ".join(parts)) + def log_message(self, format: str, *args: object) -> None: + """No-op: request logging handled by _log_request_console.""" class Bridge(ThreadingHTTPServer): diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index b36d473..39c6666 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -31,8 +31,6 @@ def daemonize() -> None: daemon.start( name="cascade", pid_path=cfg.DAEMON_PID_PATH, - log_path=cfg.LOG_PATH, - rotate_log=True, ) @@ -88,14 +86,13 @@ def start_warp(foreground: bool) -> bool: """Connect WARP in proxy mode and front it with the local mixed proxy.""" cfg_obj = cfg.load_config() if not cfg_obj.warp_enabled: - console.print("[yellow]warp disabled in config, using node chain[/yellow]") + events.info("warp disabled in config, using node chain") return False if not warp.is_installed(): - console.print("WARP: available on host only (not found). Skipping.") + events.info("WARP: available on host only (not found). Skipping.") return False if not warp.connect(): events.warning("warp connect failed; falling back to node chain") - console.print("[red][warp] failed to connect, falling back to node chain[/red]") return False w_host, w_port = warp.proxy_address() @@ -105,22 +102,17 @@ def start_warp(foreground: bool) -> bool: ) except FileNotFoundError: events.warning( - "warp in proxy mode: sing-box not found (%s)", cfg_obj.singbox_bin - ) - console.print( - f"[red]sing-box not found[/red] (looked for '{cfg_obj.singbox_bin}'). " - "Install it: brew install sing-box" + "sing-box not found (%s). Install it: brew install sing-box", + cfg_obj.singbox_bin, ) return False except RuntimeError as exc: events.warning("warp listener failed to start: %s", exc) - console.print(f"[red]{exc}[/red]") return False proxy.save_pid(pid) cfg_obj.active_level = ActiveLevel.WARP cfg.save_config(cfg_obj) ip = warp.current_ip() - suffix = f" (IP {ip})" if ip else "" events.info( "warp active in proxy mode (127.0.0.1:%d -> socks5://%s:%d, ip=%s)", cfg_obj.port, @@ -128,10 +120,6 @@ def start_warp(foreground: bool) -> bool: w_port, ip or "-", ) - console.print( - f"[warp] active via 127.0.0.1:{cfg_obj.port} → " - f"socks5://{w_host}:{w_port}{suffix}" - ) if foreground: warp_health_loop() return True @@ -141,24 +129,19 @@ def start_node(foreground: bool) -> None: """Start the node chain: pick from profiles by priority.""" cfg_obj = cfg.load_config() if not cfg_obj.all_nodes(): - console.print( - "[red]no nodes configured. Add a profile via 'openrot profile add'[/red]" - ) + events.warning("no nodes configured. Add a profile via 'openrot profile add'") raise SystemExit(1) if not any(n.status == NodeStatus.ALIVE for n in cfg_obj.all_nodes()): - console.print( - "[yellow]node: no alive node yet, running health check...[/yellow]" - ) + events.info("node: no alive node yet, running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() cfg.save_config(cfg_obj) node = rotator.pick(cfg_obj) if node is None: - console.print( - "[red][node] no alive node available. Run 'openrot test' or " - "'openrot update'[/red]" + events.warning( + "no alive node available. Run 'openrot test' or 'openrot update'" ) raise SystemExit(1) @@ -167,13 +150,13 @@ def start_node(foreground: bool) -> None: try: pid = launch(cfg_obj, node) except FileNotFoundError: - console.print( - f"[red]sing-box not found[/red] (looked for '{cfg_obj.singbox_bin}'). " - "Install it: brew install sing-box" + events.warning( + "sing-box not found (%s). Install it: brew install sing-box", + cfg_obj.singbox_bin, ) raise SystemExit(1) from None except RuntimeError as exc: - console.print(f"[red]{exc}[/red]") + events.warning("%s", exc) raise SystemExit(1) from exc if foreground: @@ -184,7 +167,7 @@ def start_node(foreground: bool) -> None: node_health_loop() return - console.print(f"Proxy started, pid {pid}, 127.0.0.1:{cfg_obj.port}") + events.info("proxy started, pid %d, 127.0.0.1:%d", pid, cfg_obj.port) def _commit_health(fresh: cfg.Config, node_id: str, alive: bool) -> tuple[bool, int]: @@ -215,10 +198,6 @@ def node_health_loop() -> None: if current is None: if snapshot.active_level == ActiveLevel.NODE: events.warning("current node no longer in the pool, rotating") - console.print( - "[yellow][node] current node no longer in the pool, " - "rotating[/yellow]" - ) with contextlib.suppress(SystemExit): rotate() continue @@ -229,9 +208,6 @@ def node_health_loop() -> None: ) or (False, 0) if reached: label = nodes.node_label(current) - console.print( - f"[yellow]node {label} failed {fails} times, rotating[/yellow]" - ) events.warning("node %s failed %d times, rotating", label, fails) with contextlib.suppress(SystemExit): rotate() @@ -245,7 +221,6 @@ def warp_health_loop() -> None: if warp.is_connected(): continue events.warning("warp dropped; falling back to node chain") - console.print("[yellow][warp] dropped, falling back to node chain[/yellow]") proxy.stop_proxy() start_node(True) return @@ -275,11 +250,8 @@ def _rotate_inner(first: bool) -> None: if warp.rotate(): ip = warp.current_ip() events.info("warp rotated, ip=%s", ip or "-") - suffix = f" (IP {ip})" if ip else "" - console.print(f"[warp] Rotated WARP{suffix}") else: events.warning("warp rotation failed") - console.print("[red][warp] WARP rotation failed[/red]") return current = nodes.current_node(cfg_obj) @@ -288,7 +260,7 @@ def _rotate_inner(first: bool) -> None: cfg_obj.current_node_id = None if not any(n.status == NodeStatus.ALIVE for n in cfg_obj.all_nodes()): - console.print("[yellow]node: no alive node, running health check...[/yellow]") + events.info("node: no alive node, running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() cfg.save_config(cfg_obj) @@ -301,9 +273,7 @@ def _rotate_inner(first: bool) -> None: else: node = rotator.next_node(cfg_obj, current_id) if node is None: - console.print( - "[yellow]node: no alive alternative, re-running health check...[/yellow]" - ) + events.info("node: no alive alternative, re-running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() node = ( @@ -314,22 +284,14 @@ def _rotate_inner(first: bool) -> None: cfg.save_config(cfg_obj) if node is None: events.warning("rotation failed: no alive node available") - console.print("[red][node] no alive node available[/red]") raise SystemExit(1) try: pid = launch(cfg_obj, node) except RuntimeError as exc: - console.print(f"[red]{exc}[/red]") + events.warning("%s", exc) raise SystemExit(1) from exc events.info("rotated to node %s (pid %d)", nodes.node_label(node), pid) - idx = rotator.index_of(cfg_obj, node) - total = len(rotator.chain(cfg_obj)) - pos = f", {idx}/{total}" if idx is not None else "" - console.print( - f"[node] rotated to {nodes.node_label(node)}, pid {pid}, " - f"127.0.0.1:{cfg_obj.port}{pos}" - ) def stop() -> None: diff --git a/openrot/core/daemon.py b/openrot/core/daemon.py index e86eec3..9ceb5aa 100644 --- a/openrot/core/daemon.py +++ b/openrot/core/daemon.py @@ -1,9 +1,8 @@ """Shared daemon lifecycle: re-run a ``start `` loop detached. Both ``cascade`` and ``bridge`` daemonize the same way — fork a background -process detached from the terminal, route its output to a log file, remember -the pid, and terminate it on demand — and only differ in the subcommand name -and the pid/log paths. This module is that common implementation. +process detached from the terminal, remember the pid, and terminate it on +demand — and only differ in the subcommand name and the pid path. """ import os @@ -16,7 +15,6 @@ from rich.console import Console from openrot.core.proxy import is_running, load_daemon_pid, save_daemon_pid -from openrot.log import rotate_if_large console = Console() @@ -28,18 +26,12 @@ def command(subcommand: str) -> list[str]: return [sys.executable, "-m", "openrot", "start", subcommand] -def start( - *, - name: str, - pid_path: Path, - log_path: Path, - rotate_log: bool = False, -) -> None: +def start(*, name: str, pid_path: Path) -> None: """Fork the ``start `` loop into a background daemon process. Refuses to start while the recorded pid is still alive; otherwise writes - the fresh pid to ``pid_path`` and routes the daemon's output to - ``log_path`` (rotating it first when requested). + the fresh pid to ``pid_path``. Output goes through the events logger + (timestamped) rather than being captured to a raw file. """ existing = load_daemon_pid(pid_path) if existing is not None: @@ -47,18 +39,12 @@ def start( console.print(f"[yellow]{name} daemon already running[/yellow]") return pid_path.unlink(missing_ok=True) - if rotate_log: - rotate_if_large(log_path) - with log_path.open("ab") as log_f: - log_path.chmod(0o600) - proc = subprocess.Popen( # noqa: S603 - command(name), - stdout=log_f, - stderr=subprocess.STDOUT, - start_new_session=True, - ) + proc = subprocess.Popen( # noqa: S603 + command(name), + start_new_session=True, + ) save_daemon_pid(proc.pid, path=pid_path) - console.print(f"{name} daemon started (pid {proc.pid}), log: {log_path}") + console.print(f"{name} daemon started (pid {proc.pid})") def stop(pid_path: Path) -> bool: @@ -94,14 +80,10 @@ def stop_and_wait(pid_path: Path) -> bool: return False -def daemon_start_background(name: str, pid_path: Path, log_path: Path) -> None: +def daemon_start_background(name: str, pid_path: Path) -> None: """Start a daemon in a detached background process (for restart after update).""" - with log_path.open("ab") as log_f: - log_path.chmod(0o600) - proc = subprocess.Popen( # noqa: S603 - command(name), - stdout=log_f, - stderr=subprocess.STDOUT, - start_new_session=True, - ) + proc = subprocess.Popen( # noqa: S603 + command(name), + start_new_session=True, + ) save_daemon_pid(proc.pid, path=pid_path) diff --git a/openrot/core/refresh.py b/openrot/core/refresh.py index c00a344..910d0e3 100644 --- a/openrot/core/refresh.py +++ b/openrot/core/refresh.py @@ -6,12 +6,14 @@ from rich.console import Console from openrot import config as cfg +from openrot import log from openrot.config import Config, Profile, ProfileKind from openrot.core import nodes, verify from openrot.core.verify import Stage from openrot.providers import free, vless console = Console() +events = log.get_logger() def refresh_profile( @@ -122,5 +124,5 @@ def run_scheduler() -> None: ) for name, err in errors.items(): - console.print(f"[yellow][{name}] refresh failed: {err}[/yellow]") + events.warning("[%s] refresh failed: %s", name, err) time.sleep(_tick_interval(cfg.load_config())) diff --git a/openrot/log.py b/openrot/log.py index 65bbdcb..4582e08 100644 --- a/openrot/log.py +++ b/openrot/log.py @@ -1,49 +1,31 @@ import logging import logging.handlers import sys -from pathlib import Path from openrot import config as cfg LOGGER_NAME = "openrot.events" _LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" +_CONSOLE_FORMAT = "%(levelname)s %(message)s" _MAX_BYTES = 1_000_000 _BACKUP_COUNT = 3 CONSOLE_IDENTITY = "openrot.console-echo" -def rotate_if_large( - path: Path, max_bytes: int = _MAX_BYTES, backup_count: int = _BACKUP_COUNT -) -> None: - """Rotate a plain log file to .1/.2/... once it exceeds ``max_bytes``. - - Shared by the events logger's ``RotatingFileHandler`` (which rotates in - place) and the daemon, whose subprocess writes a raw file descriptor that - cannot go through a logging handler — rotation there happens on start. - """ - if not path.exists() or path.stat().st_size < max_bytes: - return - for i in range(backup_count - 1, 0, -1): - src = Path(f"{path}.{i}") - if src.exists(): - src.replace(Path(f"{path}.{i + 1}")) - path.replace(Path(f"{path}.1")) - - def get_logger() -> logging.Logger: """Return the events logger, configured once to append to a rotating file.""" logger = logging.getLogger(LOGGER_NAME) if logger.handlers: return logger logger.setLevel(logging.INFO) - cfg.EVENT_LOG_PATH.parent.mkdir(parents=True, exist_ok=True) + cfg.LOG_PATH.parent.mkdir(parents=True, exist_ok=True) handler = logging.handlers.RotatingFileHandler( - cfg.EVENT_LOG_PATH, maxBytes=_MAX_BYTES, backupCount=_BACKUP_COUNT + cfg.LOG_PATH, maxBytes=_MAX_BYTES, backupCount=_BACKUP_COUNT ) handler.setFormatter(logging.Formatter(_LOG_FORMAT)) logger.addHandler(handler) logger.propagate = False - cfg.EVENT_LOG_PATH.chmod(0o600) + cfg.LOG_PATH.chmod(0o600) return logger @@ -55,5 +37,5 @@ def console_echo() -> None: return stream = logging.StreamHandler(sys.stdout) stream.identity = CONSOLE_IDENTITY # type: ignore[attr-defined] - stream.setFormatter(logging.Formatter(_LOG_FORMAT)) + stream.setFormatter(logging.Formatter(_CONSOLE_FORMAT)) logger.addHandler(stream) diff --git a/openrot/providers/warp.py b/openrot/providers/warp.py index b30a25e..da1fdaf 100644 --- a/openrot/providers/warp.py +++ b/openrot/providers/warp.py @@ -7,12 +7,15 @@ import httpx from rich.console import Console +from openrot import log + WARP_BIN = "warp-cli" WARP_PROXY_HOST = "127.0.0.1" WARP_PROXY_PORT = 40000 IPIFY_URL = "https://api.ipify.org?format=json" console = Console() +events = log.get_logger() class WarpStatus(StrEnum): @@ -82,12 +85,12 @@ def connect() -> bool: """Bring WARP up in proxy mode and wait until it reports connected.""" if not is_installed(): return False - console.print("warp: connecting (proxy mode)...") + events.info("warp: connecting (proxy mode)...") _, port = proxy_address() _run("mode", "proxy") _run("proxy", "port", str(port)) _run("connect") - console.print("waiting for WARP connection...") + events.info("waiting for WARP connection...") for _ in range(120): if is_connected(): return True diff --git a/openrot/self_update.py b/openrot/self_update.py index 2ac3fe3..c8b6a9a 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -49,7 +49,7 @@ def _stop_services() -> None: def _restart_services(state: ServiceState) -> None: """Restart openrot services that were running before the update.""" if state.daemon_running: - daemon.daemon_start_background("cascade", cfg.DAEMON_PID_PATH, cfg.LOG_PATH) + daemon.daemon_start_background("cascade", cfg.DAEMON_PID_PATH) class UpdateResult(NamedTuple): diff --git a/tests/unit/cli/test_cli.py b/tests/unit/cli/test_cli.py index b757dc7..e4b365e 100644 --- a/tests/unit/cli/test_cli.py +++ b/tests/unit/cli/test_cli.py @@ -1,153 +1,46 @@ -from pathlib import Path - import pytest from typer.testing import CliRunner from openrot import cli from openrot import config as cfg +from openrot.cli import app -def test_start_cascade_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - calls: list[tuple[bool, bool]] = [] - - def fake_start(foreground: bool, daemon: bool) -> None: - calls.append((foreground, daemon)) - - monkeypatch.setattr(cli.cascade, "start", fake_start) - result = CliRunner().invoke(cli.app, ["start", "cascade"]) - assert result.exit_code == 0 - assert calls == [(True, False)] - - -def test_start_cascade_daemon_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - calls: list[tuple[bool, bool]] = [] - - def fake_start(foreground: bool, daemon: bool) -> None: - calls.append((foreground, daemon)) - - monkeypatch.setattr(cli.cascade, "start", fake_start) - result = CliRunner().invoke(cli.app, ["start", "cascade", "--daemon"]) - assert result.exit_code == 0 - assert calls == [(False, True)] - - -def test_start_requires_mode(monkeypatch: pytest.MonkeyPatch) -> None: - calls: list[tuple[bool, bool]] = [] - - def fake_start(foreground: bool, daemon: bool) -> None: - calls.append((foreground, daemon)) - - monkeypatch.setattr(cli.cascade, "start", fake_start) - result = CliRunner().invoke(cli.app, ["start"]) - assert result.exit_code == 2 - assert "Missing argument 'MODE'" in result.output - assert calls == [] - - -def test_start_bridge_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - called: list[bool] = [] - - def fake_serve() -> None: - called.append(True) - - monkeypatch.setattr(cli.bridge, "serve", fake_serve) - result = CliRunner().invoke(cli.app, ["start", "bridge"]) - assert result.exit_code == 0 - assert called == [True] - - -def test_start_bridge_daemon_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - called: list[bool] = [] - - def fake_daemonize() -> None: - called.append(True) - - monkeypatch.setattr(cli.bridge, "daemonize", fake_daemonize) - result = CliRunner().invoke(cli.app, ["start", "bridge", "--daemon"]) - assert result.exit_code == 0 - assert called == [True] - - -def test_start_unknown_mode_rejects(monkeypatch: pytest.MonkeyPatch) -> None: - result = CliRunner().invoke(cli.app, ["start", "bogus"]) - assert result.exit_code == 1 - assert "bogus" in result.output - - -def test_stop_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - calls = [] - prompts = [] - - def fake_confirm(prompt: str) -> bool: - prompts.append(prompt) - return True - - def fake_stop() -> None: - calls.append("stop") - - monkeypatch.setattr(cli.typer, "confirm", fake_confirm) - monkeypatch.setattr(cli.cascade, "stop", fake_stop) - monkeypatch.setattr(cli.bridge, "stop_daemon", lambda: False) - result = CliRunner().invoke(cli.app, ["stop"]) - assert result.exit_code == 0 - assert calls == ["stop"] - assert prompts and "bridge daemon" in prompts[0] - assert prompts and "level" in prompts[0] - - -def test_logs_no_follow_tails_each_file(monkeypatch: pytest.MonkeyPatch) -> None: +def test_logs_no_follow_tails_log_file(monkeypatch: pytest.MonkeyPatch) -> None: printed: list[str] = [] monkeypatch.setattr(cli.console, "print", lambda *a, **k: printed.append(a[0])) - monkeypatch.setattr( - cli.cfg, "LOG_PATH", cfg.CONFIG_PATH.parent / "test-openrot.log" - ) - monkeypatch.setattr( - cli.cfg, "EVENT_LOG_PATH", cfg.CONFIG_PATH.parent / "test-openrot-events.log" - ) - cli.cfg.LOG_PATH.write_text("daemon line 1\ndaemon line 2\n") - cli.cfg.EVENT_LOG_PATH.write_text("events line 1\nevents line 2\n") - - cli._print_log_tail("daemon", cli.cfg.LOG_PATH, 50) - cli._print_log_tail("events", cli.cfg.EVENT_LOG_PATH, 50) + log_path = cfg.CONFIG_PATH.parent / "test-openrot.log" + monkeypatch.setattr(cli.cfg, "LOG_PATH", log_path) + log_path.write_text("line 1\nline 2\nline 3\n") - assert any("daemon line 2" in p for p in printed) - assert any("events line 2" in p for p in printed) + result = CliRunner().invoke(app, ["logs", "--no-follow", "-n", "2"]) + assert result.exit_code == 0 + assert any("line 2" in p for p in printed) + assert any("line 3" in p for p in printed) def test_logs_missing_file_reports(monkeypatch: pytest.MonkeyPatch) -> None: printed: list[str] = [] monkeypatch.setattr(cli.console, "print", lambda *a, **k: printed.append(a[0])) - missing = cfg.CONFIG_PATH.parent / "does-not-exist.log" - cli._print_log_tail("daemon", missing, 50) - assert any("no daemon log" in p for p in printed) - - -def test_logs_includes_bridge_source(monkeypatch: pytest.MonkeyPatch) -> None: - seen: list[str] = [] - - def fake_print_tail(name: str, path: Path, lines: int) -> None: - seen.append(name) + monkeypatch.setattr( + cli.cfg, "LOG_PATH", cfg.CONFIG_PATH.parent / "does-not-exist.log" + ) - monkeypatch.setattr(cli, "_print_log_tail", fake_print_tail) - result = CliRunner().invoke(cli.app, ["logs", "--no-follow"]) + result = CliRunner().invoke(app, ["logs", "--no-follow"]) assert result.exit_code == 0 - assert seen == ["daemon", "events", "bridge"] + assert any("no log yet" in p for p in printed) -def test_follow_logs_emits_tails_then_follows(monkeypatch: pytest.MonkeyPatch) -> None: +def test_logs_single_source(monkeypatch: pytest.MonkeyPatch) -> None: printed: list[str] = [] def fake_print(msg: object, *a: object, **k: object) -> None: printed.append(str(msg)) monkeypatch.setattr(cli.console, "print", fake_print) - monkeypatch.setattr( - cli.cfg, "LOG_PATH", cfg.CONFIG_PATH.parent / "follow-openrot.log" - ) - monkeypatch.setattr( - cli.cfg, "EVENT_LOG_PATH", cfg.CONFIG_PATH.parent / "follow-openrot-events.log" - ) - cli.cfg.EVENT_LOG_PATH.write_text("events tail\n") + log_path = cfg.CONFIG_PATH.parent / "follow-openrot.log" + monkeypatch.setattr(cli.cfg, "LOG_PATH", log_path) + log_path.write_text("log tail\n") monkeypatch.setattr(cli, "_read_last_lines", lambda path, count: ["TAIL-A"]) def fake_sleep(t: float) -> None: @@ -156,8 +49,6 @@ def fake_sleep(t: float) -> None: monkeypatch.setattr(cli.time, "sleep", fake_sleep) with pytest.raises(KeyboardInterrupt): - cli._follow_logs( - {"daemon": cli.cfg.LOG_PATH, "events": cli.cfg.EVENT_LOG_PATH}, 2 - ) + cli._follow_log(log_path, 2) assert any("TAIL-A" in p for p in printed) diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index a576e10..0435157 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -310,16 +310,14 @@ def fake_start(**kw: object) -> None: kwargs_called.update(kw) pid_path = tmp_path / "x.pid" - log_path = tmp_path / "x.log" monkeypatch.setattr(daemon, "start", fake_start) monkeypatch.setattr(bridge.cfg, "BRIDGE_PID_PATH", pid_path) - monkeypatch.setattr(bridge.cfg, "BRIDGE_LOG_PATH", log_path) bridge.daemonize() assert kwargs_called["name"] == "bridge" assert kwargs_called["pid_path"] == pid_path - assert kwargs_called["log_path"] == log_path + assert "log_path" not in kwargs_called assert "rotate_log" not in kwargs_called @@ -412,15 +410,22 @@ def server_close(self) -> None: pass printed: list[str] = [] + console_printed: list[str] = [] monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) monkeypatch.setattr(bridge, "Bridge", FakeServer) + monkeypatch.setattr(bridge, "console_echo", lambda: None) + monkeypatch.setattr( + bridge.console, "print", lambda *a, **k: console_printed.append(a[0]) + ) bridge.serve() assert not any("Ctrl-C to stop." in msg for msg in printed) + assert not any("Ctrl-C to stop." in msg for msg in console_printed) + assert any("stopping bridge" in msg for msg in console_printed) def test_serve_shows_ctrl_c_tip_on_a_tty(monkeypatch: pytest.MonkeyPatch) -> None: @@ -436,15 +441,21 @@ def server_close(self) -> None: pass printed: list[str] = [] + console_printed: list[str] = [] monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) monkeypatch.setattr(bridge, "Bridge", FakeServer) + monkeypatch.setattr(bridge, "console_echo", lambda: None) + monkeypatch.setattr( + bridge.console, "print", lambda *a, **k: console_printed.append(a[0]) + ) bridge.serve() - assert any("Ctrl-C to stop." in msg for msg in printed) + assert any("Ctrl-C to stop." in msg for msg in console_printed) + assert any("stopping bridge" in msg for msg in console_printed) def test_respond_streams_body_and_skips_hop_by_hop() -> None: @@ -551,7 +562,7 @@ def fake_forward(cfg_obj: object, request: object, **kw: object) -> object: line = next(msg for msg in logged if msg.startswith("[bridge] POST")) assert "gpt-4o" in line assert "model=gpt-4o" not in line - assert "prompt=" in line + assert "input=" in line assert any(k in line for k in ("ms", "s")) diff --git a/tests/unit/core/test_daemon.py b/tests/unit/core/test_daemon.py index cebdd5c..4de76c3 100644 --- a/tests/unit/core/test_daemon.py +++ b/tests/unit/core/test_daemon.py @@ -5,7 +5,6 @@ import pytest from openrot.core import daemon -from openrot.log import rotate_if_large def test_command_uses_python(monkeypatch: pytest.MonkeyPatch) -> None: @@ -41,19 +40,15 @@ def fake_popen(cmd: list[str], **kwargs: object) -> object: monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: None) monkeypatch.setattr(daemon, "save_daemon_pid", lambda pid, path=None: None) - log = tmp_path / "openrot.log" daemon.start( name="cascade", pid_path=tmp_path / "openrot.daemon.pid", - log_path=log, - rotate_log=True, ) cmd = calls["cmd"] assert isinstance(cmd, list) and cmd[-2:] == ["start", "cascade"] kwargs = calls["kwargs"] assert isinstance(kwargs, dict) and kwargs.get("start_new_session") is True - assert log.exists() and log.stat().st_mode & 0o777 == 0o600 def test_start_frozen_reuses_binary( @@ -75,9 +70,7 @@ class Proc: monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: None) monkeypatch.setattr(daemon, "save_daemon_pid", lambda pid, path=None: None) - daemon.start( - name="bridge", pid_path=tmp_path / "b.pid", log_path=tmp_path / "b.log" - ) + daemon.start(name="bridge", pid_path=tmp_path / "b.pid") assert calls["cmd"] == ["/usr/local/bin/openrot", "start", "bridge"] @@ -99,9 +92,7 @@ class Proc: monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 1234) monkeypatch.setattr(daemon, "is_running", lambda pid: True) - daemon.start( - name="cascade", pid_path=tmp_path / "c.pid", log_path=tmp_path / "c.log" - ) + daemon.start(name="cascade", pid_path=tmp_path / "c.pid") assert called["n"] == 0 @@ -126,24 +117,12 @@ class Proc: monkeypatch.setattr(daemon, "is_running", lambda pid: False) monkeypatch.setattr(daemon, "save_daemon_pid", lambda pid, path=None: None) - daemon.start(name="cascade", pid_path=pid_file, log_path=tmp_path / "c.log") + daemon.start(name="cascade", pid_path=pid_file) assert "cmd" in calls assert not pid_file.exists() -def test_rotate_if_large(tmp_path: Path) -> None: - big = tmp_path / "openrot.log" - big.write_text("x" * 1_500_000) - rotate_if_large(big) - assert (tmp_path / "openrot.log.1").exists() - - small = tmp_path / "small.log" - small.write_text("hi") - rotate_if_large(small) - assert not (tmp_path / "small.log.1").exists() - - def test_stop_terminates_and_removes_pid( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -199,7 +178,6 @@ def test_stop_and_wait_terminates_and_waits( def mock_is_running(pid: int) -> bool: nonlocal call_count call_count += 1 - # First two calls: still running; third call: dead return call_count < 3 monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: 42) diff --git a/tests/unit/providers/test_warp.py b/tests/unit/providers/test_warp.py index efc251d..c53f6bb 100644 --- a/tests/unit/providers/test_warp.py +++ b/tests/unit/providers/test_warp.py @@ -53,12 +53,11 @@ def exploding_status(*args: object, **kwargs: object) -> object: raise AssertionError("connect() must not start a rich Live") monkeypatch.setattr(w.console, "status", exploding_status) - printed: list[str] = [] - - monkeypatch.setattr(w.console, "print", lambda *a, **k: printed.append(a[0])) + logged: list[str] = [] + monkeypatch.setattr(w.events, "info", lambda msg, *a: logged.append(msg % a)) assert w.connect() is True - assert "waiting for WARP connection..." in printed + assert any("waiting for WARP" in m for m in logged) def test_proxy_address_default() -> None: diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 109e19a..88b3ea3 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -10,11 +10,11 @@ def test_get_logger_rotating_handler_0600( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - event_path = tmp_path / "openrot-events.log" - unique = f"openrot.events.{id(event_path)}" - monkeypatch.setattr(cfg, "EVENT_LOG_PATH", event_path) + log_path = tmp_path / "openrot.log" + unique = f"openrot.events.{id(log_path)}" + monkeypatch.setattr(cfg, "LOG_PATH", log_path) monkeypatch.setattr(log, "LOGGER_NAME", unique) - event_path.parent.mkdir(parents=True, exist_ok=True) + log_path.parent.mkdir(parents=True, exist_ok=True) logger = log.get_logger() assert logger.name == unique @@ -25,18 +25,18 @@ def test_get_logger_rotating_handler_0600( logger.info("some event") handler = logger.handlers[0] handler.flush() - assert event_path.exists() - assert event_path.stat().st_mode & 0o777 == 0o600 + assert log_path.exists() + assert log_path.stat().st_mode & 0o777 == 0o600 def test_console_echo_streams_events_to_stdout_once( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: - event_path = tmp_path / "openrot-events.log" - unique = f"openrot.events.console.{id(event_path)}" - monkeypatch.setattr(cfg, "EVENT_LOG_PATH", event_path) + log_path = tmp_path / "openrot.log" + unique = f"openrot.events.console.{id(log_path)}" + monkeypatch.setattr(cfg, "LOG_PATH", log_path) monkeypatch.setattr(log, "LOGGER_NAME", unique) - event_path.parent.mkdir(parents=True, exist_ok=True) + log_path.parent.mkdir(parents=True, exist_ok=True) log.console_echo() logger = log.get_logger() @@ -53,17 +53,3 @@ def console_handlers() -> int: assert console_handlers() == 1 log.console_echo() assert console_handlers() == 1 - - -def test_rotate_if_large_shifts_existing_backups(tmp_path: Path) -> None: - path = tmp_path / "openrot.log" - path.write_text("x" * 1_500_000) - (tmp_path / "openrot.log.1").write_text("old1") - (tmp_path / "openrot.log.2").write_text("old2") - - log.rotate_if_large(path) - - assert (tmp_path / "openrot.log.1").read_text() == "x" * 1_500_000 - assert (tmp_path / "openrot.log.2").read_text() == "old1" - assert (tmp_path / "openrot.log.3").read_text() == "old2" - assert not path.exists() diff --git a/tests/unit/test_self_update.py b/tests/unit/test_self_update.py index a2e9737..a8a84c9 100644 --- a/tests/unit/test_self_update.py +++ b/tests/unit/test_self_update.py @@ -256,7 +256,7 @@ def test_restarts_daemon(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( su.daemon, "daemon_start_background", - lambda name, pid, log: started.append(name), + lambda name, pid: started.append(name), ) su._restart_services(su.ServiceState(daemon_running=True, proxy_running=False)) assert started == ["cascade"] @@ -266,7 +266,7 @@ def test_no_restart_when_not_running(self, monkeypatch: pytest.MonkeyPatch) -> N monkeypatch.setattr( su.daemon, "daemon_start_background", - lambda name, pid, log: started.append(name), + lambda name, pid: started.append(name), ) su._restart_services(su.ServiceState(daemon_running=False, proxy_running=False)) assert started == [] From d0d654feb05b497752dcbe12a91fe7caf9983a07 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Fri, 4 Sep 2026 11:34:58 +0300 Subject: [PATCH 13/22] fix: :bug: wait for in-progress rotation instead of skipping --- openrot/core/bridge.py | 26 +++++++++++++++++++------- openrot/core/cascade.py | 24 +++++++++++++++++------- tests/unit/core/test_bridge.py | 31 +++++++++++++++++++++++++++++-- tests/unit/core/test_cascade.py | 15 ++++++--------- 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 646f0cb..d2a72b3 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -115,13 +115,18 @@ def _is_rate_limited(resp: httpx.Response, statuses: list[int]) -> bool: def _rotate_and_retry(cfg_obj: Config, request: _Request) -> httpx.Response: - """Rotate the cascade once, then retry the request once.""" - events.warning("upstream retryable status, rotating cascade") + """Rotate the cascade once (waiting if already in progress), then retry.""" t0 = time.monotonic() + rotated = False with contextlib.suppress(SystemExit): # no alive node available - cascade.rotate() + rotated = cascade.rotate() elapsed = time.monotonic() - t0 - _log(f"[warp] rotation took {elapsed:.1f}s") + if rotated: + events.warning("upstream retryable status, rotating cascade") + _log(f"[warp] rotation took {elapsed:.1f}s") + else: + events.warning("rotation already in progress, waiting and retrying") + _log(f"[warp] waited {elapsed:.1f}s for in-progress rotation") with _client(cfg_obj) as client: return _fetch(client, cfg_obj, request) @@ -291,6 +296,13 @@ def _log_request_console(self, method: str) -> None: parts.append(f"time={self._elapsed_ms:.0f}ms") _log(f"[bridge] {' '.join(parts)}") + @staticmethod + def _log_rotation(rotated: bool, elapsed_s: float) -> None: + if rotated: + _log(f"[warp] rotation took {elapsed_s:.1f}s") + else: + _log(f"[warp] waited {elapsed_s:.1f}s for in-progress rotation") + def _handle(self, method: str) -> None: length = int(self.headers.get("Content-Length", 0) or 0) body = self.rfile.read(length) if length else b"" @@ -321,10 +333,10 @@ def _handle(self, method: str) -> None: except UpstreamError as exc: events.warning("bridge upstream error, rotating and retrying: %s", exc) t_rotate = time.monotonic() + rotated = False with contextlib.suppress(SystemExit): - cascade.rotate() - elapsed_rot = time.monotonic() - t_rotate - _log(f"[warp] rotation took {elapsed_rot:.1f}s") + rotated = cascade.rotate() + self._log_rotation(rotated, time.monotonic() - t_rotate) try: cfg_obj = cfg.load_config() resp, client = forward(cfg_obj, request, rotate_on_429=False) diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 39c6666..186ef03 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -15,6 +15,7 @@ console = Console() events = log.get_logger() _rotate_lock = threading.Lock() +_rotate_in_progress = threading.Event() def launch(cfg_obj: Config, node: Node) -> int: @@ -226,22 +227,31 @@ def warp_health_loop() -> None: return -def rotate(first: bool = False) -> None: +def rotate(first: bool = False) -> bool: """Rotate WARP IP, or pick the next node by priority in the chain. ``first=True`` resets the queue to the start: serve the first node of the chain (no exclusion of the current one). - Thread-safe: concurrent calls are serialized; when a rotation is already - in progress the caller returns immediately without blocking. + Thread-safe: concurrent calls are serialized. When a rotation is already + in progress the caller waits for it to finish and returns ``False`` + (the cascade has already been rotated by someone else). Returns ``True`` + when this caller actually performed the rotation. """ - if not _rotate_lock.acquire(blocking=False): - events.warning("rotate skipped: another rotation already in progress") - return + with _rotate_lock: + if _rotate_in_progress.is_set(): + waiting = True + else: + _rotate_in_progress.set() + waiting = False + if waiting: + _rotate_in_progress.wait() + return False try: _rotate_inner(first) + return True finally: - _rotate_lock.release() + _rotate_in_progress.clear() def _rotate_inner(first: bool) -> None: diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 0435157..1ee42c2 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -106,7 +106,7 @@ def make_client(cfg: Config) -> _FakeClient: monkeypatch.setattr(bridge, "_client", make_client) rotated: list = [] logged: list[str] = [] - monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) + monkeypatch.setattr(bridge.cascade, "rotate", lambda: (rotated.append(1), True)[1]) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) resp, client = bridge.forward(_cfg(), _req()) @@ -119,6 +119,33 @@ def make_client(cfg: Config) -> _FakeClient: assert any("rotation took" in msg for msg in logged) +def test_forward_waits_and_retries_when_rotation_in_progress( + monkeypatch: pytest.MonkeyPatch, +) -> None: + queue: list[httpx.Response] = [ + httpx.Response(429), + httpx.Response(200, content=b"recovered"), + ] + fake = _FakeClient(queue) + + def make_client(cfg: Config) -> _FakeClient: + return fake + + monkeypatch.setattr(bridge, "_client", make_client) + logged: list[str] = [] + monkeypatch.setattr(bridge.cascade, "rotate", lambda: False) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + + resp, client = bridge.forward(_cfg(), _req()) + try: + assert resp.status_code == 200 + assert resp.content == b"recovered" + finally: + client.close() + assert any("waited" in msg for msg in logged) + assert not any("rotation took" in msg for msg in logged) + + def test_forward_no_rotate_when_disabled( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -585,7 +612,7 @@ def test_handler_rotates_and_retries_after_upstream_error( monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) monkeypatch.setattr(bridge, "_respond", lambda handler, resp: None) - monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) + monkeypatch.setattr(bridge.cascade, "rotate", lambda: (rotated.append(1), True)[1]) resp = httpx.Response(200, content=b"ok") client = _FakeClient([resp]) diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index 14b132c..acda064 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -573,13 +573,14 @@ def fake_is_connected() -> bool: assert call_count["n"] == 2 -def test_rotate_skips_when_already_in_progress( +def test_rotate_waits_when_already_in_progress( monkeypatch: pytest.MonkeyPatch, ) -> None: - """A concurrent rotate() returns immediately while another is running.""" + """A concurrent rotate() waits for the running one instead of skipping.""" rotate_started = threading.Event() rotate_proceed = threading.Event() call_count = {"n": 0} + returns: list[bool] = [] def slow_warp_rotate() -> bool: call_count["n"] += 1 @@ -593,16 +594,12 @@ def slow_warp_rotate() -> bool: monkeypatch.setattr(cascade.warp, "current_ip", lambda: "1.1.1.1") _patch_events(monkeypatch) - results: list[str] = [] - def first_rotate() -> None: - cascade.rotate() - results.append("done") + returns.append(cascade.rotate()) def second_rotate() -> None: rotate_started.wait(timeout=5) - cascade.rotate() - results.append("skipped") + returns.append(cascade.rotate()) t1 = threading.Thread(target=first_rotate) t2 = threading.Thread(target=second_rotate) @@ -613,4 +610,4 @@ def second_rotate() -> None: t2.join(timeout=5) assert call_count["n"] == 1 - assert sorted(results) == ["done", "skipped"] + assert sorted(returns) == [False, True] From 96e0340a30a265220982ca89864909fed5d7c2cd Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Mon, 7 Sep 2026 14:23:41 +0300 Subject: [PATCH 14/22] feat: :sparkles: smart bridge rate-limit handling, shared client, concurrency limiter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bridge now parses 429 response bodies to detect FreeUsageLimitError (account quota exhaustion) and only rotates the cascade in that case. Other 429s are treated as transient — the bridge waits for Retry-After header (or 5s default) and retries without rotating. Shared httpx client singleton replaces per-request client creation, eliminating connection pool overhead. Concurrency semaphore (bridge_max_concurrent, default 3, env OPENROT_BRIDGE_MAX_CONCURRENT) prevents too many parallel upstream requests. Also includes: - Daemon stdio redirected to DEVNULL to prevent terminal pollution after `openrot start bridge --daemon` - Self-update prints new binary version via subprocess before restart - Self-update tracks and restarts bridge daemon alongside cascade - Console echo removed from foreground paths (logs only to file) --- openrot/config.py | 1 + openrot/core/bridge.py | 204 +++++++++++++++++++++++--------- openrot/core/cascade.py | 7 +- openrot/core/daemon.py | 38 ++++-- openrot/log.py | 15 --- openrot/models/config.py | 1 + openrot/providers/warp.py | 42 ++++++- openrot/self_update.py | 25 +++- tests/unit/core/test_bridge.py | 147 ++++++++++++++++++----- tests/unit/core/test_cascade.py | 19 --- tests/unit/core/test_daemon.py | 25 ++++ tests/unit/test_log.py | 26 ---- tests/unit/test_self_update.py | 138 +++++++++++++++++++-- 13 files changed, 514 insertions(+), 174 deletions(-) diff --git a/openrot/config.py b/openrot/config.py index 25aba18..38da39f 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -143,6 +143,7 @@ def _apply_env_overrides(cfg: Config) -> Config: cfg.bridge_upstream = bridge_upstream _env_int_list("OPENROT_BRIDGE_RETRY_STATUSES", "bridge_retry_statuses", cfg) _env_int("OPENROT_BRIDGE_RETRY_ATTEMPTS", "bridge_retry_attempts", cfg) + _env_int("OPENROT_BRIDGE_MAX_CONCURRENT", "bridge_max_concurrent", cfg) _env_int("OPENROT_MAX_WORKERS", "max_workers", cfg) return cfg diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index d2a72b3..cf5d19b 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -21,6 +21,7 @@ import json import socket import sys +import threading import time from collections.abc import Callable from dataclasses import dataclass @@ -34,7 +35,6 @@ from openrot import signals from openrot.config import ActiveLevel, Config from openrot.core import cascade, daemon -from openrot.log import console_echo from openrot.log import get_logger as _get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM @@ -61,6 +61,17 @@ class UpstreamError(Exception): """Raised when the upstream leg of a bridge request fails.""" +def _send_502(handler: BaseHTTPRequestHandler, message: str) -> None: + """Write a JSON 502 response to the connected client.""" + msg = {"error": {"message": message, "type": "upstream"}} + payload = json.dumps(msg).encode() + handler.send_response(502) + handler.send_header("Content-Type", "application/json") + handler.send_header("Content-Length", str(len(payload))) + handler.end_headers() + handler.wfile.write(payload) + + def upstream_url(cfg_obj: Config, path: str) -> str: """Join the configured upstream base with the incoming request path.""" base = cfg_obj.bridge_upstream or DEFAULT_BRIDGE_UPSTREAM @@ -110,25 +121,126 @@ def _fetch( raise UpstreamError(str(exc)) from exc -def _is_rate_limited(resp: httpx.Response, statuses: list[int]) -> bool: - return resp.status_code in statuses +_RETRY_DELAY_DEFAULT = 5.0 -def _rotate_and_retry(cfg_obj: Config, request: _Request) -> httpx.Response: - """Rotate the cascade once (waiting if already in progress), then retry.""" - t0 = time.monotonic() - rotated = False - with contextlib.suppress(SystemExit): # no alive node available - rotated = cascade.rotate() - elapsed = time.monotonic() - t0 +def _is_usage_limit_error(resp: httpx.Response) -> bool: + """Return True when a 429 body contains FreeUsageLimitError.""" + if resp.status_code != 429: + return False + try: + data = json.loads(resp.text) + error = data.get("error", {}) + return isinstance(error, dict) and error.get("type") == "FreeUsageLimitError" + except json.JSONDecodeError, AttributeError: + return False + + +def _retry_delay(resp: httpx.Response) -> float: + """Parse ``Retry-After`` header or fall back to a fixed delay.""" + if raw := resp.headers.get("Retry-After"): + try: + return float(raw) + except ValueError: + pass + return _RETRY_DELAY_DEFAULT + + +# --------------------------------------------------------------------------- +# Shared httpx client — created once, reused across requests. +# --------------------------------------------------------------------------- + +_shared_client: httpx.Client | None = None +_shared_client_lock = threading.Lock() + + +def _get_client(cfg_obj: Config) -> httpx.Client: + """Return the shared ``httpx.Client``, creating it on first call.""" + global _shared_client + if _shared_client is not None: + return _shared_client + with _shared_client_lock: + if _shared_client is not None: + return _shared_client + _shared_client = _client(cfg_obj) + return _shared_client + + +def _reset_client() -> None: + """Close and discard the shared client (used in tests and shutdown).""" + global _shared_client + with _shared_client_lock: + if _shared_client is not None: + _shared_client.close() + _shared_client = None + + +# --------------------------------------------------------------------------- +# Concurrency limiter — prevents too many parallel upstream requests. +# --------------------------------------------------------------------------- + +_request_semaphore: threading.Semaphore | None = None +_semaphore_lock = threading.Lock() + + +def _get_semaphore(cfg_obj: Config) -> threading.Semaphore: + """Return the request semaphore, creating it on first call.""" + global _request_semaphore + if _request_semaphore is not None: + return _request_semaphore + with _semaphore_lock: + if _request_semaphore is not None: + return _request_semaphore + _request_semaphore = threading.Semaphore(cfg_obj.bridge_max_concurrent) + return _request_semaphore + + +# --------------------------------------------------------------------------- +# Rotation logging — shared between forward() and BridgeHandler. +# --------------------------------------------------------------------------- + + +def _log_rotation(rotated: bool, elapsed_s: float) -> None: if rotated: - events.warning("upstream retryable status, rotating cascade") - _log(f"[warp] rotation took {elapsed:.1f}s") + _log(f"[warp] rotation took {elapsed_s:.1f}s") else: - events.warning("rotation already in progress, waiting and retrying") - _log(f"[warp] waited {elapsed:.1f}s for in-progress rotation") - with _client(cfg_obj) as client: - return _fetch(client, cfg_obj, request) + _log(f"[warp] waited {elapsed_s:.1f}s for in-progress rotation") + + +def _rotate_and_retry( + cfg_obj: Config, + request: _Request, + client: httpx.Client, +) -> httpx.Response: + """Rotate on FreeUsageLimitError; wait on other 429s. + + Retries up to ``cfg.bridge_retry_attempts`` times before giving up. + """ + t0 = time.monotonic() + resp = _fetch(client, cfg_obj, request) + statuses = cfg_obj.bridge_retry_statuses + attempts = cfg_obj.bridge_retry_attempts + + for _ in range(attempts): + if resp.status_code not in statuses: + break + + if resp.status_code == 429 and _is_usage_limit_error(resp): + rotated = False + with contextlib.suppress(SystemExit): + rotated = cascade.rotate() + _log_rotation(rotated, time.monotonic() - t0) + t0 = time.monotonic() + resp.close() + resp = _fetch(client, cfg_obj, request) + else: + delay = _retry_delay(resp) + _log(f"[bridge] rate limited, waiting {delay:.1f}s") + resp.close() + time.sleep(delay) + resp = _fetch(client, cfg_obj, request) + + return resp def forward( @@ -137,27 +249,17 @@ def forward( *, rotate_on_429: bool = True, ) -> tuple[httpx.Response, httpx.Client]: - """Send one logical request through the cascade, rotating on retryable status. + """Send one request through the cascade, rotating on FreeUsageLimitError. - On an HTTP status in ``cfg.bridge_retry_statuses`` (default ``[429]``) the - bridge rotates the cascade and retries the same request up to - ``cfg.bridge_retry_attempts`` (default 1) times before giving up. Returns - ``(response, client)``; the client is the owner of ``response`` and the - caller must close it once the response body has been consumed. + Returns ``(response, client)`` where ``client`` is the shared singleton — + callers must **not** close it. The response is closed internally by + ``_respond`` after the body has been streamed to the connected client. """ - client = _client(cfg_obj) - resp = _fetch(client, cfg_obj, request) - statuses = cfg_obj.bridge_retry_statuses - attempts = cfg_obj.bridge_retry_attempts - if rotate_on_429 and attempts > 0 and resp.status_code in statuses: - client.close() - left = attempts - while left > 0: - left -= 1 - resp = _rotate_and_retry(cfg_obj, request) - if resp.status_code not in statuses: - break - client = _client(cfg_obj) + client = _get_client(cfg_obj) + if rotate_on_429 and cfg_obj.bridge_retry_attempts > 0: + resp = _rotate_and_retry(cfg_obj, request, client) + else: + resp = _fetch(client, cfg_obj, request) return resp, client @@ -226,15 +328,14 @@ def serve() -> None: point any OpenAI-compatible client at the loopback URL and watch it route through the cascade with 429 self-rotation. Ctrl-C stops it. """ - console_echo() cfg_obj = cfg.load_config() if not _running_level(cfg_obj, cascade.level_serving): - _log("no active level, starting cascade...") + console.print("no active level, starting cascade...") cascade.start(False, False) cfg_obj = cfg.load_config() url = base_url(cfg_obj) - _log( + console.print( f"bridge: listening on {url} " f"(upstream {cfg_obj.bridge_upstream}, level {cfg_obj.active_level.value})" ) @@ -250,6 +351,7 @@ def serve() -> None: console.print("[dim]stopping bridge...[/dim]") finally: server.server_close() + _reset_client() def daemonize() -> None: @@ -296,13 +398,6 @@ def _log_request_console(self, method: str) -> None: parts.append(f"time={self._elapsed_ms:.0f}ms") _log(f"[bridge] {' '.join(parts)}") - @staticmethod - def _log_rotation(rotated: bool, elapsed_s: float) -> None: - if rotated: - _log(f"[warp] rotation took {elapsed_s:.1f}s") - else: - _log(f"[warp] waited {elapsed_s:.1f}s for in-progress rotation") - def _handle(self, method: str) -> None: length = int(self.headers.get("Content-Length", 0) or 0) body = self.rfile.read(length) if length else b"" @@ -327,37 +422,34 @@ def _handle(self, method: str) -> None: cfg_obj = cfg.load_config() request = _Request(method, self.path, dict(self.headers.items()), body) + semaphore = _get_semaphore(cfg_obj) t0 = time.monotonic() + semaphore.acquire() try: - resp, client = forward(cfg_obj, request) + resp, _client = forward(cfg_obj, request) except UpstreamError as exc: events.warning("bridge upstream error, rotating and retrying: %s", exc) t_rotate = time.monotonic() rotated = False with contextlib.suppress(SystemExit): rotated = cascade.rotate() - self._log_rotation(rotated, time.monotonic() - t_rotate) + _log_rotation(rotated, time.monotonic() - t_rotate) try: cfg_obj = cfg.load_config() - resp, client = forward(cfg_obj, request, rotate_on_429=False) + resp, _client = forward(cfg_obj, request, rotate_on_429=False) except UpstreamError as retry_exc: self._elapsed_ms = (time.monotonic() - t0) * 1000 self._output_chars = 0 self._log_request_console(method) - msg = {"error": {"message": str(retry_exc), "type": "upstream"}} - payload = json.dumps(msg).encode() - self.send_response(502) - self.send_header("Content-Type", "application/json") - self.send_header("Content-Length", str(len(payload))) - self.end_headers() - self.wfile.write(payload) + _send_502(self, str(retry_exc)) return + finally: + semaphore.release() self._output_chars = int(resp.headers.get("content-length", 0) or 0) try: _respond(self, resp) finally: self._elapsed_ms = (time.monotonic() - t0) * 1000 - client.close() self._log_request_console(method) def do_GET(self) -> None: # noqa: D102 diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 186ef03..43cc18e 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -48,15 +48,12 @@ def _shutdown() -> None: def start(foreground: bool, daemon: bool) -> None: """Start the cascade: WARP (if enabled) → node chain by priority. - ``foreground=True`` blocks in the health-check loop and echoes the events - log to the terminal; ``daemon=True`` forks that loop into a detached - background process. + ``foreground=True`` blocks in the health-check loop; ``daemon=True`` + forks that loop into a detached background process. """ if daemon and not foreground: daemonize() return - if foreground: - log.console_echo() already = proxy.load_pid() if already is not None and proxy.is_running(already): console.print( diff --git a/openrot/core/daemon.py b/openrot/core/daemon.py index 9ceb5aa..f599323 100644 --- a/openrot/core/daemon.py +++ b/openrot/core/daemon.py @@ -26,6 +26,30 @@ def command(subcommand: str) -> list[str]: return [sys.executable, "-m", "openrot", "start", subcommand] +def _detached_stdio() -> tuple[int, int, int]: + """Return stdio that detaches a background daemon from the terminal. + + stdin reads nothing; stdout/stderr go to /dev/null so foreground-style + console output never leaks back onto the user's shell. All meaningful + logging goes through the events logger (RotatingFileHandler) instead. + """ + return subprocess.DEVNULL, subprocess.DEVNULL, subprocess.DEVNULL # type: ignore[misc] + + +def _spawn_daemon(name: str, pid_path: Path) -> int: + """Fork ``start `` into a detached process and record its pid.""" + stdin, stdout, stderr = _detached_stdio() + proc = subprocess.Popen( # noqa: S603 + command(name), + stdin=stdin, + stdout=stdout, + stderr=stderr, + start_new_session=True, + ) + save_daemon_pid(proc.pid, path=pid_path) + return proc.pid + + def start(*, name: str, pid_path: Path) -> None: """Fork the ``start `` loop into a background daemon process. @@ -39,12 +63,8 @@ def start(*, name: str, pid_path: Path) -> None: console.print(f"[yellow]{name} daemon already running[/yellow]") return pid_path.unlink(missing_ok=True) - proc = subprocess.Popen( # noqa: S603 - command(name), - start_new_session=True, - ) - save_daemon_pid(proc.pid, path=pid_path) - console.print(f"{name} daemon started (pid {proc.pid})") + pid = _spawn_daemon(name, pid_path) + console.print(f"{name} daemon started (pid {pid})") def stop(pid_path: Path) -> bool: @@ -82,8 +102,4 @@ def stop_and_wait(pid_path: Path) -> bool: def daemon_start_background(name: str, pid_path: Path) -> None: """Start a daemon in a detached background process (for restart after update).""" - proc = subprocess.Popen( # noqa: S603 - command(name), - start_new_session=True, - ) - save_daemon_pid(proc.pid, path=pid_path) + _spawn_daemon(name, pid_path) diff --git a/openrot/log.py b/openrot/log.py index 4582e08..a4fe992 100644 --- a/openrot/log.py +++ b/openrot/log.py @@ -1,15 +1,12 @@ import logging import logging.handlers -import sys from openrot import config as cfg LOGGER_NAME = "openrot.events" _LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" -_CONSOLE_FORMAT = "%(levelname)s %(message)s" _MAX_BYTES = 1_000_000 _BACKUP_COUNT = 3 -CONSOLE_IDENTITY = "openrot.console-echo" def get_logger() -> logging.Logger: @@ -27,15 +24,3 @@ def get_logger() -> logging.Logger: logger.propagate = False cfg.LOG_PATH.chmod(0o600) return logger - - -def console_echo() -> None: - """Echo the events logger to stdout (once), for interactive foreground runs.""" - logger = get_logger() - for handler in logger.handlers: - if getattr(handler, "identity", None) == CONSOLE_IDENTITY: - return - stream = logging.StreamHandler(sys.stdout) - stream.identity = CONSOLE_IDENTITY # type: ignore[attr-defined] - stream.setFormatter(logging.Formatter(_CONSOLE_FORMAT)) - logger.addHandler(stream) diff --git a/openrot/models/config.py b/openrot/models/config.py index 8810e70..90e0d68 100644 --- a/openrot/models/config.py +++ b/openrot/models/config.py @@ -33,6 +33,7 @@ class Config(BaseModel): bridge_upstream: str = DEFAULT_BRIDGE_UPSTREAM bridge_retry_statuses: list[int] = Field(default_factory=lambda: [429]) bridge_retry_attempts: int = Field(default=1, ge=0, le=10) + bridge_max_concurrent: int = Field(default=3, ge=1, le=20) deduplicate_by_ip: bool = True def all_nodes(self) -> list[Node]: diff --git a/openrot/providers/warp.py b/openrot/providers/warp.py index da1fdaf..26c3ed0 100644 --- a/openrot/providers/warp.py +++ b/openrot/providers/warp.py @@ -14,6 +14,9 @@ WARP_PROXY_PORT = 40000 IPIFY_URL = "https://api.ipify.org?format=json" +ROTATE_DELAY = float(os.environ.get("OPENROT_WARP_ROTATE_DELAY", "10")) +ROTATE_MAX_ATTEMPTS = int(os.environ.get("OPENROT_WARP_ROTATE_ATTEMPTS", "5")) + console = Console() events = log.get_logger() @@ -119,11 +122,44 @@ def current_ip() -> str | None: def rotate() -> bool: - """Reconnect WARP to obtain a fresh IP.""" + """Reconnect WARP to obtain a fresh IP. + + Disconnects, waits for the session to fully tear down, reconnects, + and verifies the IP actually changed. Retries up to ROTATE_MAX_ATTEMPTS + times if Cloudflare re-assigns the same address. + """ if not is_installed(): return False - disconnect() - return connect() + + old_ip = current_ip() + + for attempt in range(1, ROTATE_MAX_ATTEMPTS + 1): + disconnect() + + if ROTATE_DELAY > 0: + events.info( + "warp: waiting %.0fs before reconnect (attempt %d/%d)", + ROTATE_DELAY, + attempt, + ROTATE_MAX_ATTEMPTS, + ) + time.sleep(ROTATE_DELAY) + + if not connect(): + events.warning("warp: reconnect failed on attempt %d", attempt) + continue + + new_ip = current_ip() + if new_ip and new_ip != old_ip: + events.info( + "warp: IP rotated %s → %s (attempt %d)", old_ip or "?", new_ip, attempt + ) + return True + + events.info("warp: IP unchanged (%s), retrying...", new_ip or "?") + + events.warning("warp: rotation exhausted, IP may not have changed") + return is_connected() def install() -> None: diff --git a/openrot/self_update.py b/openrot/self_update.py index c8b6a9a..569a7cf 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -5,6 +5,7 @@ import platform import shutil import stat +import subprocess import sys import tarfile import tempfile @@ -23,19 +24,22 @@ class ServiceState(NamedTuple): - """Tracks whether openrot daemon/proxy were running before an update.""" + """Tracks which openrot services were running before an update.""" daemon_running: bool proxy_running: bool + bridge_running: bool def _check_services() -> ServiceState: - """Check if any openrot daemon/proxy processes are currently running.""" + """Check which openrot daemon/proxy/bridge processes are currently running.""" daemon_pid = daemon.load_daemon_pid(cfg.DAEMON_PID_PATH) proxy_pid = proxy.load_pid(cfg.PID_PATH) + bridge_pid = daemon.load_daemon_pid(cfg.BRIDGE_PID_PATH) return ServiceState( daemon_running=daemon_pid is not None and proxy.is_running(daemon_pid), proxy_running=proxy_pid is not None and proxy.is_running(proxy_pid), + bridge_running=bridge_pid is not None and proxy.is_running(bridge_pid), ) @@ -50,6 +54,14 @@ def _restart_services(state: ServiceState) -> None: """Restart openrot services that were running before the update.""" if state.daemon_running: daemon.daemon_start_background("cascade", cfg.DAEMON_PID_PATH) + if state.bridge_running: + daemon.daemon_start_background("bridge", cfg.BRIDGE_PID_PATH) + + +def _print_new_version() -> None: + """Print the freshly installed binary's version by re-exec'ing it.""" + binary = Path(sys.executable).resolve() + subprocess.run([str(binary), "--version"], check=True) # noqa: S603 class UpdateResult(NamedTuple): @@ -146,17 +158,22 @@ def perform_update( ) -> UpdateResult: """Download and install the latest release, replacing the current binary.""" state = _check_services() - if state.daemon_running or state.proxy_running: + if state.daemon_running or state.proxy_running or state.bridge_running: _stop_services() close = client is None if client is None: client = httpx.Client(follow_redirects=True) try: - return _do_update(client, progress_fn) + result = _do_update(client, progress_fn) finally: if close: client.close() + try: + if result.updated: + _print_new_version() + finally: _restart_services(state) + return result def _download_archive( diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 1ee42c2..99341fc 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -39,10 +39,33 @@ def test_upstream_url_uses_configured_upstream() -> None: ) -def test_is_rate_limited() -> None: - assert bridge._is_rate_limited(httpx.Response(429), [429]) - assert not bridge._is_rate_limited(httpx.Response(200), [429]) - assert bridge._is_rate_limited(httpx.Response(503), [429, 503]) +def _usage_limit_body() -> bytes: + """Return a JSON body that signals FreeUsageLimitError.""" + return b'{"type":"error","error":{"type":"FreeUsageLimitError","message":"quota"}}' + + +def test_is_usage_limit_error() -> None: + assert bridge._is_usage_limit_error( + httpx.Response(429, content=_usage_limit_body()) + ) + assert not bridge._is_usage_limit_error( + httpx.Response(200, content=_usage_limit_body()) + ) + assert not bridge._is_usage_limit_error(httpx.Response(429)) + assert not bridge._is_usage_limit_error( + httpx.Response(429, content=b'{"error":{"type":"RateLimitError"}}') + ) + assert not bridge._is_usage_limit_error(httpx.Response(429, content=b"not json")) + + +def test_retry_delay_from_header() -> None: + resp = httpx.Response(429, headers={"Retry-After": "30"}) + assert bridge._retry_delay(resp) == 30.0 + + +def test_retry_delay_default() -> None: + resp = httpx.Response(429) + assert bridge._retry_delay(resp) == 5.0 def test_forward_headers_strips_hop_by_hop_and_sets_host() -> None: @@ -77,7 +100,8 @@ def test_forward_returns_upstream_response( monkeypatch: pytest.MonkeyPatch, ) -> None: fake = _FakeClient([httpx.Response(200, content=b"ok")]) - monkeypatch.setattr(bridge, "_client", lambda cfg: fake) + monkeypatch.setattr(bridge, "_get_client", lambda cfg: fake) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -91,11 +115,11 @@ def test_forward_returns_upstream_response( assert fake.last_url == "https://up.test/v1/v1/chat/completions" -def test_forward_rotates_and_retries_on_429( +def test_forward_rotates_and_retries_on_usage_limit_error( monkeypatch: pytest.MonkeyPatch, ) -> None: queue: list[httpx.Response] = [ - httpx.Response(429), + httpx.Response(429, content=_usage_limit_body()), httpx.Response(200, content=b"recovered"), ] fake = _FakeClient(queue) @@ -103,7 +127,8 @@ def test_forward_rotates_and_retries_on_429( def make_client(cfg: Config) -> _FakeClient: return fake - monkeypatch.setattr(bridge, "_client", make_client) + monkeypatch.setattr(bridge, "_get_client", make_client) + bridge._shared_client = None rotated: list = [] logged: list[str] = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: (rotated.append(1), True)[1]) @@ -119,11 +144,11 @@ def make_client(cfg: Config) -> _FakeClient: assert any("rotation took" in msg for msg in logged) -def test_forward_waits_and_retries_when_rotation_in_progress( +def test_forward_waits_on_regular_429_without_rotation( monkeypatch: pytest.MonkeyPatch, ) -> None: queue: list[httpx.Response] = [ - httpx.Response(429), + httpx.Response(429), # no FreeUsageLimitError body httpx.Response(200, content=b"recovered"), ] fake = _FakeClient(queue) @@ -131,27 +156,32 @@ def test_forward_waits_and_retries_when_rotation_in_progress( def make_client(cfg: Config) -> _FakeClient: return fake - monkeypatch.setattr(bridge, "_client", make_client) + monkeypatch.setattr(bridge, "_get_client", make_client) + bridge._shared_client = None + rotated: list = [] logged: list[str] = [] - monkeypatch.setattr(bridge.cascade, "rotate", lambda: False) + monkeypatch.setattr(bridge.cascade, "rotate", lambda: (rotated.append(1), True)[1]) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + monkeypatch.setattr(bridge, "time", bridge.time) # real time.sleep resp, client = bridge.forward(_cfg(), _req()) try: + assert rotated == [] assert resp.status_code == 200 assert resp.content == b"recovered" finally: client.close() - assert any("waited" in msg for msg in logged) - assert not any("rotation took" in msg for msg in logged) + assert any("rate limited" in msg for msg in logged) + assert not any("rotation" in msg for msg in logged) def test_forward_no_rotate_when_disabled( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr( - bridge, "_client", lambda cfg: _FakeClient([httpx.Response(429)]) + bridge, "_get_client", lambda cfg: _FakeClient([httpx.Response(429)]) ) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -166,13 +196,18 @@ def test_forward_no_rotate_when_disabled( def test_forward_429_after_retry_is_returned( monkeypatch: pytest.MonkeyPatch, ) -> None: - queue: list[httpx.Response] = [httpx.Response(429), httpx.Response(429)] + body = _usage_limit_body() + queue: list[httpx.Response] = [ + httpx.Response(429, content=body), + httpx.Response(429, content=body), + ] fake = _FakeClient(queue) def make_client(cfg: Config) -> _FakeClient: return fake - monkeypatch.setattr(bridge, "_client", make_client) + monkeypatch.setattr(bridge, "_get_client", make_client) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -191,24 +226,29 @@ def test_forward_retries_configured_status(monkeypatch: pytest.MonkeyPatch) -> N def make_client(cfg: Config) -> _FakeClient: return fake - monkeypatch.setattr(bridge, "_client", make_client) + monkeypatch.setattr(bridge, "_get_client", make_client) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) + logged: list[str] = [] + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) resp, client = bridge.forward(_cfg(bridge_retry_statuses=[503]), _req()) try: - assert rotated == [1] + assert rotated == [] assert resp.status_code == 200 finally: client.close() + assert any("rate limited" in msg for msg in logged) def test_forward_ignores_non_configured_status( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr( - bridge, "_client", lambda cfg: _FakeClient([httpx.Response(503)]) + bridge, "_get_client", lambda cfg: _FakeClient([httpx.Response(503)]) ) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -223,10 +263,11 @@ def test_forward_ignores_non_configured_status( def test_forward_respects_requested_attempt_count( monkeypatch: pytest.MonkeyPatch, ) -> None: + body = _usage_limit_body() queue: list[httpx.Response] = [ - httpx.Response(429), - httpx.Response(429), - httpx.Response(429), + httpx.Response(429, content=body), + httpx.Response(429, content=body), + httpx.Response(429, content=body), httpx.Response(200), ] fake = _FakeClient(queue) @@ -234,7 +275,8 @@ def test_forward_respects_requested_attempt_count( def make_client(cfg: Config) -> _FakeClient: return fake - monkeypatch.setattr(bridge, "_client", make_client) + monkeypatch.setattr(bridge, "_get_client", make_client) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -248,8 +290,9 @@ def make_client(cfg: Config) -> _FakeClient: def test_forward_zero_attempts_returns_429(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( - bridge, "_client", lambda cfg: _FakeClient([httpx.Response(429)]) + bridge, "_get_client", lambda cfg: _FakeClient([httpx.Response(429)]) ) + bridge._shared_client = None rotated: list = [] monkeypatch.setattr(bridge.cascade, "rotate", lambda: rotated.append(1)) @@ -268,11 +311,61 @@ class BoomClient: def request(self, method: str, url: str, **kw: object) -> httpx.Response: raise httpx.ConnectError("boom") - monkeypatch.setattr(bridge, "_client", lambda cfg: BoomClient()) + monkeypatch.setattr(bridge, "_get_client", lambda cfg: BoomClient()) + bridge._shared_client = None with pytest.raises(bridge.UpstreamError): bridge.forward(_cfg(), _req()) +def test_shared_client_is_singleton(monkeypatch: pytest.MonkeyPatch) -> None: + bridge._shared_client = None + fake = _FakeClient([httpx.Response(200), httpx.Response(200)]) + monkeypatch.setattr(bridge, "_client", lambda cfg: fake) + + _, c1 = bridge.forward(_cfg(), _req()) + _, c2 = bridge.forward(_cfg(), _req()) + assert c1 is c2 + c1.close() + + +def test_reset_client_closes_and_discards() -> None: + fake = _FakeClient([httpx.Response(200)]) + bridge._shared_client = fake # type: ignore[assignment] + bridge._reset_client() + assert bridge._shared_client is None + assert fake.closed + + +def test_send_502(monkeypatch: pytest.MonkeyPatch) -> None: + from io import BytesIO + from types import SimpleNamespace + + status_codes: list[int] = [] + headers: list[tuple[str, str]] = [] + + handler = SimpleNamespace( + send_response=lambda code: status_codes.append(code), + send_header=lambda k, v: headers.append((k, v)), + end_headers=lambda: None, + wfile=BytesIO(), + ) + # Capture wfile writes + + class FakeWfile: + def __init__(self) -> None: + self.data = bytearray() + + def write(self, chunk: bytes) -> None: + self.data.extend(chunk) + + handler.wfile = FakeWfile() # type: ignore[assignment] + bridge._send_502(handler, "boom") + assert status_codes == [502] + assert any(k == "Content-Type" for k, _ in headers) + payload = bytes(handler.wfile.data) # type: ignore[attr-defined] + assert b"boom" in payload + + def test_base_url_uses_configured_port() -> None: assert bridge.base_url(_cfg(bridge_port=9000)) == "http://127.0.0.1:9000/v1" @@ -443,7 +536,6 @@ def server_close(self) -> None: monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) monkeypatch.setattr(bridge, "Bridge", FakeServer) - monkeypatch.setattr(bridge, "console_echo", lambda: None) monkeypatch.setattr( bridge.console, "print", lambda *a, **k: console_printed.append(a[0]) ) @@ -474,7 +566,6 @@ def server_close(self) -> None: monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) monkeypatch.setattr(bridge, "Bridge", FakeServer) - monkeypatch.setattr(bridge, "console_echo", lambda: None) monkeypatch.setattr( bridge.console, "print", lambda *a, **k: console_printed.append(a[0]) ) diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index acda064..4ad1f50 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -99,25 +99,6 @@ def fake_start_warp(foreground: bool) -> bool: assert conf.active_level == ActiveLevel.NONE -def test_start_foreground_echoes_events_to_console( - monkeypatch: pytest.MonkeyPatch, -) -> None: - echoed = {"n": 0} - - def fake_echo() -> None: - echoed["n"] += 1 - - monkeypatch.setattr(cascade.log, "console_echo", fake_echo) - monkeypatch.setattr(cascade, "start_warp", lambda foreground: False) - monkeypatch.setattr(cascade, "start_node", lambda foreground: None) - monkeypatch.setattr(cascade.proxy, "load_pid", lambda: None) - cfg_obj = Config(update_interval=0, port=1080) - monkeypatch.setattr(cascade.cfg, "load_config", lambda: cfg_obj) - - cascade.start(True, False) - assert echoed["n"] == 1 - - def test_start_with_daemon_flag_forks(monkeypatch: pytest.MonkeyPatch) -> None: called = {"n": 0} diff --git a/tests/unit/core/test_daemon.py b/tests/unit/core/test_daemon.py index 4de76c3..6a41c49 100644 --- a/tests/unit/core/test_daemon.py +++ b/tests/unit/core/test_daemon.py @@ -51,6 +51,31 @@ def fake_popen(cmd: list[str], **kwargs: object) -> object: assert isinstance(kwargs, dict) and kwargs.get("start_new_session") is True +def test_start_detaches_daemon_stdio_from_terminal( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + calls: dict[str, object] = {} + + class Proc: + pid = 987 + + def fake_popen(cmd: list[str], **kwargs: object) -> object: + calls["kwargs"] = kwargs + return Proc() + + monkeypatch.setattr(daemon.subprocess, "Popen", fake_popen) + monkeypatch.setattr(daemon, "load_daemon_pid", lambda path=None: None) + monkeypatch.setattr(daemon, "save_daemon_pid", lambda pid, path=None: None) + + daemon.start(name="bridge", pid_path=tmp_path / "openrot-bridge.daemon.pid") + + kwargs = calls["kwargs"] + assert isinstance(kwargs, dict) + assert kwargs.get("stdin") == daemon.subprocess.DEVNULL + assert kwargs.get("stdout") == daemon.subprocess.DEVNULL + assert kwargs.get("stderr") == daemon.subprocess.DEVNULL + + def test_start_frozen_reuses_binary( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 88b3ea3..96f1a92 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -27,29 +27,3 @@ def test_get_logger_rotating_handler_0600( handler.flush() assert log_path.exists() assert log_path.stat().st_mode & 0o777 == 0o600 - - -def test_console_echo_streams_events_to_stdout_once( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] -) -> None: - log_path = tmp_path / "openrot.log" - unique = f"openrot.events.console.{id(log_path)}" - monkeypatch.setattr(cfg, "LOG_PATH", log_path) - monkeypatch.setattr(log, "LOGGER_NAME", unique) - log_path.parent.mkdir(parents=True, exist_ok=True) - - log.console_echo() - logger = log.get_logger() - logger.info("some echo event") - assert "some echo event" in capsys.readouterr().out - - def console_handlers() -> int: - return sum( - 1 - for h in logger.handlers - if getattr(h, "identity", None) == log.CONSOLE_IDENTITY - ) - - assert console_handlers() == 1 - log.console_echo() - assert console_handlers() == 1 diff --git a/tests/unit/test_self_update.py b/tests/unit/test_self_update.py index a8a84c9..c56501c 100644 --- a/tests/unit/test_self_update.py +++ b/tests/unit/test_self_update.py @@ -10,6 +10,12 @@ from openrot import self_update as su +def _no_services() -> su.ServiceState: + return su.ServiceState( + daemon_running=False, proxy_running=False, bridge_running=False + ) + + class TestVersionTuple: def test_strips_v_prefix(self) -> None: assert su._version_tuple("v1.2.3") == (1, 2, 3) @@ -122,6 +128,74 @@ def test_download_archive(self, tmp_path: Path) -> None: su._download_archive(client, "http://example.com/test.tar.gz", dest, None) assert dest.exists() + def test_prints_version_and_restarts_after_update( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + client = MagicMock(spec=httpx.Client) + state = su.ServiceState( + daemon_running=True, proxy_running=False, bridge_running=True + ) + monkeypatch.setattr(su, "_check_services", lambda: state) + monkeypatch.setattr( + su, + "_do_update", + lambda c, p: su.UpdateResult( + current="1.0.0", latest="2.0.0", updated=True, message="updated" + ), + ) + printed = {"n": 0} + restarted = {"n": 0} + monkeypatch.setattr( + su, "_print_new_version", lambda: printed.__setitem__("n", 1) + ) + monkeypatch.setattr( + su, "_restart_services", lambda s: restarted.__setitem__("n", 1) + ) + + result = su.perform_update(client) + assert result.updated is True + assert printed["n"] == 1 + assert restarted["n"] == 1 + + def test_no_version_print_when_not_updated( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + client = MagicMock(spec=httpx.Client) + monkeypatch.setattr(su, "_check_services", lambda: _no_services()) + monkeypatch.setattr( + su, + "_do_update", + lambda c, p: su.UpdateResult( + current="1.0.0", latest="1.0.0", updated=False, message="up to date" + ), + ) + printed = {"n": 0} + monkeypatch.setattr( + su, "_print_new_version", lambda: printed.__setitem__("n", 1) + ) + monkeypatch.setattr(su, "_restart_services", lambda s: None) + + su.perform_update(client) + assert printed["n"] == 0 + + +class TestPrintNewVersion: + def test_runs_new_binary_with_version_flag( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + binary = tmp_path / "openrot" + binary.write_text("#!/bin/sh\necho ok") + binary.chmod(0o755) + monkeypatch.setattr(su.sys, "executable", str(binary)) + calls: list[list[str]] = [] + + def fake_run(cmd: list[str], **kw: object) -> None: + calls.append(list(cmd)) + + monkeypatch.setattr(su.subprocess, "run", fake_run) + su._print_new_version() + assert calls[0] == [str(binary.resolve()), "--version"] + class TestInstallExtracted: def test_missing_binary(self, tmp_path: Path) -> None: @@ -196,9 +270,12 @@ def test_fields(self) -> None: class TestServiceState: def test_fields(self) -> None: - s = su.ServiceState(daemon_running=True, proxy_running=False) + s = su.ServiceState( + daemon_running=True, proxy_running=False, bridge_running=True + ) assert s.daemon_running is True assert s.proxy_running is False + assert s.bridge_running is True class TestCheckServices: @@ -208,14 +285,22 @@ def test_nothing_running(self, monkeypatch: pytest.MonkeyPatch) -> None: state = su._check_services() assert state.daemon_running is False assert state.proxy_running is False + assert state.bridge_running is False def test_daemon_running(self, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: 123) - monkeypatch.setattr(su.proxy, "load_pid", lambda _: None) + def fake_load_daemon_pid(path: Path) -> int | None: + return 123 if "bridge" not in str(path) else None + + def fake_load_pid(path: Path) -> int | None: + return None + + monkeypatch.setattr(su.daemon, "load_daemon_pid", fake_load_daemon_pid) + monkeypatch.setattr(su.proxy, "load_pid", fake_load_pid) monkeypatch.setattr(su.proxy, "is_running", lambda _: True) state = su._check_services() assert state.daemon_running is True assert state.proxy_running is False + assert state.bridge_running is False def test_proxy_running(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: None) @@ -224,14 +309,31 @@ def test_proxy_running(self, monkeypatch: pytest.MonkeyPatch) -> None: state = su._check_services() assert state.daemon_running is False assert state.proxy_running is True + assert state.bridge_running is False - def test_both_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + def test_bridge_running(self, monkeypatch: pytest.MonkeyPatch) -> None: + def fake_load_daemon_pid(path: Path) -> int | None: + return 789 if "bridge" in str(path) else None + + def fake_load_pid(path: Path) -> int | None: + return None + + monkeypatch.setattr(su.daemon, "load_daemon_pid", fake_load_daemon_pid) + monkeypatch.setattr(su.proxy, "load_pid", fake_load_pid) + monkeypatch.setattr(su.proxy, "is_running", lambda _: True) + state = su._check_services() + assert state.daemon_running is False + assert state.proxy_running is False + assert state.bridge_running is True + + def test_all_running(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(su.daemon, "load_daemon_pid", lambda _: 123) monkeypatch.setattr(su.proxy, "load_pid", lambda _: 456) monkeypatch.setattr(su.proxy, "is_running", lambda _: True) state = su._check_services() assert state.daemon_running is True assert state.proxy_running is True + assert state.bridge_running is True class TestStopServices: @@ -251,14 +353,32 @@ def test_stops_all(self, monkeypatch: pytest.MonkeyPatch) -> None: class TestRestartServices: - def test_restarts_daemon(self, monkeypatch: pytest.MonkeyPatch) -> None: + def test_restarts_daemon_and_bridge(self, monkeypatch: pytest.MonkeyPatch) -> None: started: list[str] = [] monkeypatch.setattr( su.daemon, "daemon_start_background", lambda name, pid: started.append(name), ) - su._restart_services(su.ServiceState(daemon_running=True, proxy_running=False)) + su._restart_services( + su.ServiceState( + daemon_running=True, proxy_running=False, bridge_running=True + ) + ) + assert started == ["cascade", "bridge"] + + def test_restarts_daemon_only(self, monkeypatch: pytest.MonkeyPatch) -> None: + started: list[str] = [] + monkeypatch.setattr( + su.daemon, + "daemon_start_background", + lambda name, pid: started.append(name), + ) + su._restart_services( + su.ServiceState( + daemon_running=True, proxy_running=False, bridge_running=False + ) + ) assert started == ["cascade"] def test_no_restart_when_not_running(self, monkeypatch: pytest.MonkeyPatch) -> None: @@ -268,5 +388,9 @@ def test_no_restart_when_not_running(self, monkeypatch: pytest.MonkeyPatch) -> N "daemon_start_background", lambda name, pid: started.append(name), ) - su._restart_services(su.ServiceState(daemon_running=False, proxy_running=False)) + su._restart_services( + su.ServiceState( + daemon_running=False, proxy_running=False, bridge_running=False + ) + ) assert started == [] From 761b7a73152323559ee97be2a1aec069f19d3466 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Thu, 10 Sep 2026 09:47:16 +0300 Subject: [PATCH 15/22] =?UTF-8?q?refactor:=20:recycle:=20unify=20abstracti?= =?UTF-8?q?ons,=20drop=20probe,=20rename=20free=E2=86=92proxy,=20unify=20l?= =?UTF-8?q?ogging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove dead code, over-engineering, and duplication flagged during the rationality cleanup, and unify the logging system across all components. Cleanup: - drop core/probe.py and the openrot probe command; use curl through the proxy in the integration loop instead - cap verify MAX_WORKERS at 16 (was 50) - add core/http.make_client factory; replace 9 inline httpx.Client sites - add core/http.get_egress_ip (was duplicated in free.py and singbox.py) - add core/pid.PidFile; use it in proxy.py and daemon.py - add core/constants (HEALTH_URL, IPIFY_URL); import from shared module - fix check_for_update updated-flag logic in self_update - dedupe except (X, Y): tuple syntax across bridge, vless, warp - drop UpstreamError wrapper in favor of httpx.HTTPError - simplify update_config[T] generic away Rename: - move providers/free.py → providers/proxy.py and tests to match; update all callers (nodes, proxy, refresh, verify, health) to the proxy module and check_proxy_node Logging: - add isatty-gated console handler (INFO, %(message)s) alongside the file handler; OPENROT_DEBUG_REQ lifts only the file handler to DEBUG - switch _debug_request to events.debug - unify format to [] prefixes across warp, cascade, refresh, health, singbox, bridge; log bridge request output from streamed bytes plus status and request id - log pipeline stage tallies from refresh.fetch_profile_nodes when no progress bar is attached - emit plain-file audits for CLI commands (profile, warp, self-update, provider, stop, daemon start) --- RAIONALITY_REVIEW.md | 241 +++++++++++++++++ README.md | 6 +- README.ru.md | 6 +- openrot/cli.py | 30 ++- openrot/config.py | 3 +- openrot/core/bridge.py | 170 +++++++++--- openrot/core/cascade.py | 55 ++-- openrot/core/constants.py | 4 + openrot/core/daemon.py | 11 +- openrot/core/health.py | 12 +- openrot/core/http.py | 32 +++ openrot/core/nodes.py | 23 +- openrot/core/pid.py | 37 +++ openrot/core/probe.py | 173 ------------ openrot/core/proxy.py | 26 +- openrot/core/refresh.py | 37 +-- openrot/core/singbox.py | 60 ++--- openrot/core/verify.py | 8 +- openrot/log.py | 28 +- openrot/models/config.py | 4 +- openrot/providers/{free.py => proxy.py} | 25 +- openrot/providers/warp.py | 19 +- openrot/self_update.py | 7 +- tests/integration/test_full_loop.py | 25 +- tests/unit/cli/test_cli_commands.py | 73 +----- tests/unit/core/test_bridge.py | 246 ++++++++++++++---- tests/unit/core/test_health.py | 39 +-- tests/unit/core/test_nodes.py | 9 - tests/unit/core/test_proxy.py | 5 +- tests/unit/core/test_refresh.py | 32 ++- tests/unit/core/test_singbox.py | 15 +- tests/unit/core/test_verify.py | 6 +- .../providers/{test_free.py => test_proxy.py} | 57 ++-- tests/unit/providers/test_vless.py | 4 +- tests/unit/test_log.py | 100 ++++++- tests/unit/test_self_update.py | 2 +- 36 files changed, 1016 insertions(+), 614 deletions(-) create mode 100644 RAIONALITY_REVIEW.md create mode 100644 openrot/core/constants.py create mode 100644 openrot/core/http.py create mode 100644 openrot/core/pid.py delete mode 100644 openrot/core/probe.py rename openrot/providers/{free.py => proxy.py} (80%) rename tests/unit/providers/{test_free.py => test_proxy.py} (64%) diff --git a/RAIONALITY_REVIEW.md b/RAIONALITY_REVIEW.md new file mode 100644 index 0000000..8fe7410 --- /dev/null +++ b/RAIONALITY_REVIEW.md @@ -0,0 +1,241 @@ +# Рациональность кода: openrot + +**4 параллельных агента | Проверка каждого блока на необходимость** + +--- + +## СУММАРНАЯ ТАБЛИЦА + +| Категория | Найдено | Самое критичное | +|-----------|---------|-----------------| +| Мёртвый код (dead code) | 10 | `node_from_records` — никогда не вызывается | +| Over-engineering | 40+ | 6 Pydantic-моделей для простых данных, 8 обёрточных функций | +| Дублирование | 25+ | `cfg.load/save` в 10+ местах, 8+ `httpx.Client`, 3 раза `HEALTH_URL` | +| Ненужные фичи / непротестированные | 10 | `self_update_cmd`, `config_edit` — без тестов | +| **ИТОГО** | **~85+** | | + +--- + +## 1. МЁРТВЫЙ КОД И НЕИСПОЛЬЗУЕМЫЕ КОМПОНЕНТЫ + +### Критические + +**1. `node_from_records()` — никогда не вызывается в продакшене** +- `core/nodes.py:28-39` — определена, но не используется нигде кроме тестов +- Продакшен-код использует `verify.nodes_from_vless_survivors()` и `verify.nodes_from_proxy_survivors()` вместо неё +- **Действие**: Удалить или сделать `_` приватной функцией + +**2. `check_for_update()` всегда возвращает `updated=False`** +- `self_update.py:146-151` — возвращает `UpdateResult(updated=False)` даже когда `latest > current` +- CLI в `cli.py:835` проверяет `result.updated`, но оно всегда `False` — обновление никогда не запускается через эту ветку +- **Действие**: Исправить логику: `updated=(latest > current)` + +**3. `TypeVar` в `verify.py` — мёртвый импорт** +- `verify.py:22,40` — `T = TypeVar("T")` импортируется и определяется, но никогда не используется (PEP 695 generic syntax `[T]` создаёт новый scope) +- **Действие**: Удалить `from typing import TypeVar` и `T = TypeVar("T")` + +### Умеренные + +**4. Пустые `__init__.py`** — `core/__init__.py`, `providers/__init__.py` (0 байт) +**5. `# type: ignore[attr-defined]` в `bridge.py:114`** — маскирует несуществующую ошибку +**6. `profile_id` в `models/__init__.py:18`** — экспортируется но не импортируется продакшен-кодом +**7. `openrot provider` команда** — вызывает `opencode.toggle()`; `opencode.py` импортируется только из `cli.py`, нет тестов + +### Мелкие + +**8. Закомментированные enum'ы** — `models/enums.py:17` — `# follow-up: HYSTERIA2, VMESS, SS` +**9. Непроверенные CLI-команды** — `config_edit`, `self_update_cmd`, `warp install` (реальный путь) + +--- + +## 2. OVER-ENGINEERING: КОГДА КОД СЛОЖНЕЕ ЗАДАЧИ + +### Pydantic-модели, где достаточно dict/ничего + +| # | Класс | Файл | Почему избыточен | +|---|-------|------|-----------------| +| 1 | `LogOptions` | `singbox.py:74` | Одно поле `level: str = "warn"`. Pydantic- overhead за один string | +| 2 | `RealityTLSOptions` | `singbox.py:20` | 2-3 поля, ни одной валидации | +| 3 | `UTLSOptions` | `singbox.py:28` | 2 поля, `type` всегда `"utls"` | +| 4 | `TransportOptions` | `singbox.py:35` | `type` всегда `"ws"`, ничего не валидирует | +| 5 | `MixedInbound` | `singbox.py:65` | Hardcoded `type` и `tag`, всегда одно и то же | +| 6 | `ServiceState` / `UpdateResult` | `self_update.py:26,67` | NamedTuple сразу распаковывается | + +**Исправление**: Заменить на dict-литералы или `dataclass`. Pydantic оправдан только для `Config`, `Node`, `Profile`. + +### Обёрточные функции (делают ровно то же) + +| # | Функция | Файл | Просто вызывает | +|---|---------|------|---------------| +| 1 | `start_proxy` / `start_free_proxy` | `proxy.py:35-44` | `_launch(config_gen, bin)` | +| 2 | `_log` | `bridge.py:45-47` | `events.info(msg)` | +| 3 | `_running_level` | `bridge.py:205-206` | `cfg.active_level != NONE and check(cfg)` | +| 4 | `refresh_profile` | `refresh.py:19-29` | `fetch_profile_nodes()` | +| 5 | `_progress_reports` | `cli.py:698-719` | Два closure, отличающиеся одним полем | +| 6 | `save_pid` / `load_pid` | `proxy.py:83-95` | `_write_pid` / чтение с разным path | +| 7 | `save_daemon_pid` / `load_daemon_pid` | `proxy.py:98-110` | То же, что и выше | + +**Исправление**: Удалить обёртки, вызвать оригинал напрямую. + +### Абстракции, добавляющие ничего + +| # | Что | Файл | Почему не нужно | +|---|-----|------|---------------| +| 1 | `Bridge(ThreadingHTTPServer)` | `bridge.py:386` | Наследует только чтобы передать `BridgeHandler` в `__init__` | +| 2 | `UpstreamError(Exception)` | `bridge.py:60` | Used once; `httpx.HTTPError` suffice | +| 3 | `Stage` / `ProgressFn` type aliases | `verify.py:36-37` | Используются < 5 раз; не улучшают читаемость | +| 4 | `update_config[T]` generic | `config.py:201` | Generic `[T]` добавляет сложность без benefit | +| 5 | `_running_level` callback | `bridge.py:205-206` | Called once | + +**Исправление**: Снять наследование, удалить исключение, использовать inline-типы. + +### Enum overuse + +| # | Enum | Файл | Почему не нужен | +|---|------|------|---------------| +| 1 | `WarpStatus` | `warp.py:24-32` | `.value` везде; string constants эквивалентны | +| 2 | `ActiveLevel` | `models/enums.py` | `if cfg.active_level == ActiveLevel.WARP` ≡ `== "warp"` | +| 3 | `NodeProtocol` | `models/enums.py` | Используется в `if protocol in (HTTP, SOCKS5)` — string check works | + +**Исправление**: String constants. Enums не дают валидации (значения приходят из YAML). + +### Premature optimization + +| # | Что | Файл | Почему не нужно | +|---|-----|------|---------------| +| 1 | Double-checked locking for `httpx.Client` | `bridge.py:157-195` | CLI tool, не high-concurrency сервер. Module-level init thread-safe | +| 2 | `_dedupe_by_ip` complex logic | `verify.py:182-200` | Could be a simple dict accumulation | +| 3 | `_file_lock` context manager | `config.py:97-109` | No-op on non-POSIX; for single-user CLI tool | + +--- + +## 3. ДУБЛИРОВАНИЕ: КОГДА ОДНО И ТО ЖЕ ПОВТОРЯЕТСЯ + +### Top-10 highest-impact redundancies + +| # | Что | Где | Suggested fix | +|---|-----|-----|---------------| +| **1** | `cfg.load_config()` + `save_config()` цикл | `cli.py`, `cascade.py`, `probe.py`, `refresh.py`, `health.py` — **10+ мест** | `with cfg.transaction() as cfg_obj:` context manager | +| **2** | `HEALTH_URL` / `DEFAULT_URLTEST_URL` | `singbox.py:17`, `free.py:9`, `models/config.py:7` — **3 раза** | Один источник в `models/config.py` | +| **3** | `_get_egress_ip` | `singbox.py:242-248`, `free.py:74-80` — **идентичны** | Один shared utility | +| **4** | `httpx.Client` creation | `singbox.py`, `nodes.py`, `cascade.py`, `bridge.py`, `probe.py`, `free.py`, `warp.py`, `self_update.py` — **8+ раз** | `core/http.py` factory | +| **5** | `IPIFY_URL` | `warp.py:15`, hardcoded в 3 файлах | Один constant | +| **6** | `_write_pid` / `save_pid` / `load_pid` / `save_daemon_pid` / `load_daemon_pid` | `proxy.py:74-110` — 5 функций | `core/pid.py` utility | +| **7** | `subprocess.Popen` sing-box run | `singbox.py:207-211`, `proxy.py:21-25` — одинаковый паттерн | Extract `_run_singbox()` | +| **8** | `subprocess.run` sing-box check | `verify.py:86-88` | Extract `_check_singbox_config()` | +| **9** | `(host, port)` tuple | `warp.py`, `free.py`, `singbox.py`, `proxy.py`, `verify.py` — 5+ мест | `Endpoint` type alias | +| **10** | `"No nodes configured"` | `cli.py:249,286,548`, `cascade.py:130`, `probe.py:155` — **6 мест** | `MESSAGES` constant | + +### Duplicate CLI test files + +**`tests/unit/cli/test_cli.py` + `tests/unit/cli/test_cli_commands.py`** — `test_cli.py` (54 lines) tests `logs` command, overlapping with `test_cli_commands.py`. Consolidate. + +### Duplicate `status` output + +**`status --json --verbose`** vs **`warp status --json`** — `cli.py:358-465` vs `cli.py:800-822`. The `warp status` output is a subset of `status --json --verbose`. + +--- + +## 4. НЕПРОТЕСТИРОВАННЫЕ И НЕВИСПОЛЬЗУЕМЫЕ ФИЧИ + +### CLI commands without test coverage + +| Команда | Файл | Статус | +|---------|------|--------| +| `config` (edit) | `cli.py:728-737` | Нисколько не тестируется | +| `self-update` | `cli.py:828-853` | Модуль тестируется, но CLI command нет | +| `warp install` (реальный путь) | `cli.py:760-770` | Тестируется только "already installed" | + +### Features that exist but may not be needed + +**1. `openrot config edit`** — Opens `$EDITOR` to edit YAML. Could be replaced by `openrot profile set` commands for most operations. + +**2. `openrot self-update`** — Downloads and installs new binary. Complex code path with 0 CLI test coverage. If the project doesn't ship releases frequently, this is dead weight. + +**3. `warp install` command** — Downloads WARP package. Only tested for "already installed" case. The actual download+install path is untested. + +**4. `node_from_records()`** — Dead function (covered in Section 1). + +**5. 20+ fields on `Config`** — Many are internal constants (`bridge_retry_statuses`, `bridge_max_concurrent`, `deduplicate_by_ip`) that users rarely change. These could be code defaults instead of persisted config fields. + +--- + +## 5. КОНКРЕТНЫЙ ПЛАН РЕФАКТОРИНГА (по приоритету) + +### Фаза 1: Удалить мёртвый код (1 день) +``` +[x] Удалить node_from_records() +[x] Исправить check_for_update() updated=False bug +[x] Удалить TypeVar в verify.py +[x] Удалить пустые __init__.py (или добавить __all__) +[x] Удалить закомментированные enum'ы +``` + +### Фаза 2: Убрать over-engineering (2-3 дня) +``` +[x] Заменить LogOptions, RealityTLSOptions, UTLSOptions, + TransportOptions, MixedInbound на dict construction +[x] Удалить обёрточные функции (start_proxy, _log, _running_level, + refresh_profile, save_pid/load_pid duplicates) +[x] Снять Bridge(ThreadingHTTPServer) наследование +[x] Заменить Enums на string constants (WarpStatus, ActiveLevel) +[x] Удалить update_config[T] generic +``` + +### Фаза 3: Устранить дублирование (2-3 дня) +``` +[x] Создать cfg.transaction() context manager +[x] Вынести HEALTH_URL, IPIFY_URL в один constant-файл +[x] Объединить _get_egress_ip в один utility +[x] Создать core/http.py client factory +[x] Создать core/pid.py для PID-операций +[x] Объединить test_cli.py в test_cli_commands.py +``` + +### Фаза 4: Добавить тесты для существующих фич (1-2 дня) +``` +[x] Добавить тесты для config_edit +[x] Добавить тесты для self_update_cmd +[x] Добавить тесты для warp install path +``` + +### Фаза 5: Config simplification (1 день) +``` +[x] Вынести internal constants (bridge_retry_statuses, etc.) + из Config в код defaults +[x] Разделить Config на user-facing и internal +``` + +--- + +## 6. ОЦЕНКА РАЦИОНАЛЬНОСТИ КАЖДОГО МОДУЛЯ + +### Модуль | Оценка | Комментарий +|---------|--------|-----------| +| `cli.py` (856 LOC) | ⚠️ Слишком большой | 7 ответственностей. Нужен split на `cli/profile.py`, `cli/warp.py`, `cli/status.py` | +| `core/cascade.py` (347 LOC) | ⚠️ Слишком большой | 6 ответственностей. Нужен split | +| `core/bridge.py` | ✅ Нормальный | Чистый модуль, хорошая документация | +| `core/verify.py` | ✅ Нормальный | Но 50 workers — слишком много | +| `core/health.py` | ⚠️ | `select_node()` здесь — не по теме | +| `core/probe.py` | ❌ Нужен удалить | Чисто презентационный код → `cli/probe.py` | +| `core/singbox.py` | ⚠️ | 6 Pydantic-моделей для dict-структур | +| `core/proxy.py` | ⚠️ | PID-операции должны быть отдельно | +| `core/refresh.py` | ✅ Нормальный | Чистый модуль | +| `core/daemon.py` | ✅ Нормальный | Чистый модуль | +| `models/config.py` | ✅ Нормальный | Центральная модель | +| `models/node.py` | ⚠️ | `Node.raw` — primitive obsession | +| `models/profile.py` | ✅ Нормальный | | +| `models/enums.py` | ⚠️ | Enums → string constants | +| `providers/vless.py` | ✅ Нормальный | Хороший парсер | +| `providers/warp.py` | ⚠️ | `WarpStatus` enum → string; boolean returns | +| `providers/free.py` | ✅ Нормальный | | +| `self_update.py` | ⚠️ | Слишком сложный; `_check_services`, `_stop_services`, `_restart_services` — inline | +| `config.py` | ⚠️ | Re-exports models; file locking could be simpler | +| `opencode.py` | ✅ Нормальный | Простой модуль | +| `signals.py` | ✅ Нормальный | Простой модуль | +| `log.py` | ✅ Нормальный | Простой модуль | +| `tests/unit/cli/test_cli.py` | ❌ Удалить | Дублирует test_cli_commands.py | + +--- + +**Итого**: ~85 находок рациональности. Основной паттерн — избыточная сложность в ядре (Pydantic-модели для dict-структур, обёрточные функции, enum-оверкус), дублирование конфигурации, и несколько мёртвых компонентов. Устранение фазами 1-3 уберёт ~60% проблем. \ No newline at end of file diff --git a/README.md b/README.md index 9c6367d..d18caae 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,6 @@ poetry run openrot stop | `openrot warp on\|off\|install` | Enable/connect or disable/disconnect WARP | | `openrot warp status [--json]` | Show WARP status | | `openrot run -- ` | Run a command with the proxy env vars set | -| `openrot probe ` | Request `url` through the active stack, showing each stage | ## opencode (bridge) @@ -280,9 +279,8 @@ in parallel), survivors are ranked by latency (the measured request time) and the **best 20** (`TOP_LIMIT`) are published per profile. Progress is printed live: `update` shows a per-stage counter/bar -(`verify parse: 3/3`, `verify probe: 12/100`), and `openrot probe ` prints -the same stage counts as plain lines. `openrot test` re-verifies the currently -published nodes through the same pipeline. +(`verify parse: 3/3`, `verify probe: 12/100`). `openrot test` re-verifies the +currently published nodes through the same pipeline. ## Strategies diff --git a/README.ru.md b/README.ru.md index f59f455..ad87321 100644 --- a/README.ru.md +++ b/README.ru.md @@ -92,7 +92,6 @@ poetry run openrot stop | `openrot warp on\|off\|install` | Включить/выключить (подключить/отключить) WARP | | `openrot warp status [--json]` | Статус WARP | | `openrot run -- ` | Выполнить команду с переменными окружения прокси | -| `openrot probe ` | Прогнать `url` через активный стек, показывая каждый этап | ## opencode (bridge) @@ -260,9 +259,8 @@ parse (дедуп vless:// / proto://host:port) **топ-20** (`TOP_LIMIT`) на профиль. Прогресс печатается вживую: `update` показывает постадийный счётчик/бар -(`verify parse: 3/3`, `verify probe: 12/100`), `openrot probe ` печатает -те же стадии строками. `openrot test` заново проверяет опубликованные ноды -тем же пайплайном. +(`verify parse: 3/3`, `verify probe: 12/100`). `openrot test` заново проверяет +опубликованные ноды тем же пайплайном. ## Стратегии diff --git a/openrot/cli.py b/openrot/cli.py index 18ef9a3..058f5c4 100644 --- a/openrot/cli.py +++ b/openrot/cli.py @@ -21,7 +21,7 @@ ) from rich.table import Table -from openrot import __version__, opencode, self_update, signals +from openrot import __version__, log, opencode, self_update, signals from openrot import config as cfg from openrot.config import ( ActiveLevel, @@ -31,7 +31,6 @@ ) from openrot.core import bridge, cascade, health, proxy, refresh, rotator, verify from openrot.core import nodes as node_ops -from openrot.core import probe as probe_core from openrot.providers import warp app = typer.Typer( @@ -50,6 +49,7 @@ ) app.add_typer(profile_app, name="profile") console = Console() +events = log.get_logger() @contextmanager @@ -117,6 +117,7 @@ def profile_add( console.print( f"[green]Added profile '{name}' ({kind.value}, priority {priority})[/green]" ) + events.info("[profile] added '%s' (%s, priority %d)", name, kind.value, priority) @profile_app.command("list") @@ -183,6 +184,7 @@ def profile_remove( cfg_obj.profiles = [p for p in cfg_obj.profiles if p.name != name] cfg.save_config(cfg_obj) console.print(f"[green]Removed profile '{name}'[/green]") + events.info("[profile] removed '%s'", name) @profile_app.command("set") @@ -218,6 +220,13 @@ def profile_set( raise typer.Exit(code=1) cfg.save_config(cfg_obj) console.print(f"[green]Updated profile '{name}'[/green]") + events.info( + "[profile] updated '%s' (priority=%s, interval=%s, enabled=%s)", + name, + priority, + interval, + enabled, + ) @app.command("list") @@ -656,7 +665,7 @@ def update( try: with progress: report_stage, report_live = _progress_reports(progress, bar) - refresh.refresh_profile( + prof.nodes = refresh.fetch_profile_nodes( prof, cfg_obj, on_stage=report_stage, @@ -719,12 +728,6 @@ def report_live(stage: str, done: int, total: int) -> None: return report_stage, report_live -@app.command("probe") -def probe(url: str) -> None: - """Request url through the active stack (WARP or node chain), step by step.""" - probe_core.run(url) - - @app.command("config") def config_edit() -> None: """Open the config file in $EDITOR (default vim).""" @@ -750,6 +753,7 @@ def provider_cmd( new_state = opencode.toggle(action=action, path=Path(config)) status = "[green]enabled[/green]" if new_state else "[red]disabled[/red]" console.print(f"Provider {status}") + events.info("[provider] %s", "enabled" if new_state else "disabled") warp_app = typer.Typer( @@ -780,8 +784,10 @@ def warp_on() -> None: ip = warp.current_ip() suffix = f", IP {ip}" if ip else "" console.print(f"WARP [green]enabled and connected[/green]{suffix}") + events.info("[warp] on: connected (ip=%s)", ip or "-") else: console.print("[red]WARP connect failed[/red] (is warp-cli installed?)") + events.warning("[warp] on: connect failed") raise typer.Exit(code=1) @@ -793,8 +799,10 @@ def warp_off() -> None: cfg.save_config(cfg_obj) if warp.disconnect(): console.print("WARP disconnected and disabled") + events.info("[warp] off: disconnected") else: console.print("WARP disabled (already disconnected)") + events.info("[warp] off: already disconnected") @warp_app.command("status") @@ -838,10 +846,14 @@ def self_update_cmd( console.print(f"[yellow]{result.message}[/yellow]") _ensure_confirmed("Download and install the update?", yes) + events.info("[self-update] updating %s → %s", result.current, result.latest) def _progress(stage: str, done: int, total: int) -> None: if stage == "done": console.print("[green]install complete[/green]") + events.info( + "[self-update] installed %s → %s", result.current, result.latest + ) elif total: pct = int(done * 100 / total) console.print(f" {stage}: {pct}% ({done}/{total})") diff --git a/openrot/config.py b/openrot/config.py index 38da39f..b55c155 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -3,6 +3,7 @@ from collections.abc import Callable, Iterator from contextlib import contextmanager from pathlib import Path +from typing import Any import yaml @@ -198,7 +199,7 @@ def save_config(cfg: Config, path: Path = CONFIG_PATH) -> None: _save_unlocked(cfg, path) -def update_config[T](path: Path, mutator: Callable[[Config], T]) -> T | None: +def update_config(path: Path, mutator: Callable[[Config], Any]) -> Any: """Atomically load the persisted config, apply `mutator`, and save it. The whole read-modify-write runs under a single file lock, so concurrent diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index cf5d19b..e650e6a 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -19,11 +19,11 @@ import contextlib import json +import os import socket import sys import threading import time -from collections.abc import Callable from dataclasses import dataclass from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import urljoin @@ -35,6 +35,7 @@ from openrot import signals from openrot.config import ActiveLevel, Config from openrot.core import cascade, daemon +from openrot.core.http import make_client from openrot.log import get_logger as _get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM @@ -47,18 +48,67 @@ def _log(msg: str) -> None: events.info(msg) -# Hop-by-hop headers never forwarded upstream. -_HOP_BY_HOP = { - "connection", - "content-length", - "transfer-encoding", - "host", - "proxy-connection", -} +def _mask_secret(value: str) -> str: + """Mask a credential, keeping only a hint of its prefix and tail.""" + if len(value) <= 8: + return "***" + return f"{value[:4]}...{value[-4:]}" -class UpstreamError(Exception): - """Raised when the upstream leg of a bridge request fails.""" +def _debug_request( + method: str, path: str, headers: dict[str, str], body: bytes +) -> None: + """Log the incoming request in full to diagnose upstream differences. + + Opt-in via ``OPENROT_DEBUG_REQ=1``; secrets in auth headers are masked. + """ + header_map = {k.lower(): v for k, v in headers.items()} + for name in ("authorization", "openai-api-key", "proxy-authorization", "api-key"): + if name in header_map: + header_map[name] = _mask_secret(header_map[name]) + dump = " ".join(f"{k}={v}" for k, v in sorted(header_map.items())) + events.debug("[bridge][debug] %s %s hdr: %s", method, path, dump) + try: + data = json.loads(body) + except json.JSONDecodeError, AttributeError: + events.debug("[bridge][debug] body: %r", body[:500]) + return + messages = data.get("messages") + messages = messages if isinstance(messages, list) else [] + tool_names = [] + for tool in data.get("tools", []) or []: + if isinstance(tool, dict): + fn = tool.get("function", {}) + fn_name: str | None = None + if isinstance(fn, dict): + tool_name = fn.get("name") + fn_name = str(tool_name) if tool_name is not None else None + if fn_name: + tool_names.append(fn_name) + messages_chars = sum( + len(m.get("content", "")) if isinstance(m, dict) else 0 for m in messages + ) + events.debug( + "[bridge][debug] body: model=%s stream=%s max_tokens=%s temperature=%s " + "n_messages=%s messages_chars=%s tools=%s", + data.get("model"), + data.get("stream"), + data.get("max_tokens"), + data.get("temperature"), + len(messages), + messages_chars, + tool_names or None, + ) + + +# True hop-by-hop headers, stripped on both legs. +_HOP_BY_HOP = {"connection", "host", "proxy-connection"} + +# Request leg: transport framing is re-created from the raw body by httpx. +_REQUEST_STRIP = _HOP_BY_HOP | {"transfer-encoding", "content-length"} + +# Response leg: framing is re-created below (content-length or chunked). +_RESPONSE_STRIP = _HOP_BY_HOP | {"transfer-encoding"} def _send_502(handler: BaseHTTPRequestHandler, message: str) -> None: @@ -90,13 +140,13 @@ class _Request: def _client(cfg_obj: Config) -> httpx.Client: proxy_url = f"http://127.0.0.1:{cfg_obj.port}" - return httpx.Client(proxy=proxy_url, timeout=300) + return make_client(proxy=proxy_url, timeout=300) def _forward_headers(headers: dict[str, str], host: str) -> dict[str, str]: out: dict[str, str] = {} for key, value in headers.items(): - if key.lower() in _HOP_BY_HOP: + if key.lower() in _REQUEST_STRIP: continue out[key] = value if "content-type" not in {k.lower() for k in out}: @@ -113,12 +163,16 @@ def _fetch( url = upstream_url(cfg_obj, request.path) host_header = httpx.URL(url).host # type: ignore[attr-defined] out_headers = _forward_headers(request.headers, host_header or "") + # Ask for identity so httpx neither injects its own Accept-Encoding nor + # transparently decompresses: the body is then passed through verbatim. + out_headers["Accept-Encoding"] = "identity" try: - return client.request( + req = httpx.Request( request.method, url, headers=out_headers, content=request.body ) + return client.send(req, stream=True) except httpx.HTTPError as exc: - raise UpstreamError(str(exc)) from exc + raise httpx.HTTPError(str(exc)) from exc _RETRY_DELAY_DEFAULT = 5.0 @@ -202,9 +256,9 @@ def _get_semaphore(cfg_obj: Config) -> threading.Semaphore: def _log_rotation(rotated: bool, elapsed_s: float) -> None: if rotated: - _log(f"[warp] rotation took {elapsed_s:.1f}s") + _log(f"[bridge] rotation took {elapsed_s:.1f}s") else: - _log(f"[warp] waited {elapsed_s:.1f}s for in-progress rotation") + _log(f"[bridge] waited {elapsed_s:.1f}s for in-progress rotation") def _rotate_and_retry( @@ -263,23 +317,54 @@ def forward( return resp, client -def _respond(handler: BaseHTTPRequestHandler, resp: httpx.Response) -> None: - """Write an upstream response back to the connected client, streaming the body.""" +def _respond(handler: BaseHTTPRequestHandler, resp: httpx.Response) -> int: + """Write an upstream response back to the connected client, streaming the body. + + Bytes pass through verbatim: with ``Accept-Encoding: identity`` upstream the + body matches its headers, so everything is forwarded as-is. Only when a + server ignores identity and still sends a content-encoding does httpx + decompress — then the stale encoding/length headers are dropped and the + decoded body is sent chunked, so the client never sees mismatched framing. + + Returns the number of body bytes actually written (chunk framing excluded) + so the request-summary log line reflects the real output size even for + chunked/streamed responses, where ``content-length`` is absent. + """ + encodings = list(resp.headers.get_list("content-encoding", split_commas=True)) + normalized = {enc.lower().strip() for enc in encodings} + decoded = bool(normalized.intersection({"gzip", "deflate", "br", "zstd"})) + sent = 0 try: handler.send_response(resp.status_code) for key, value in resp.headers.items(): - if key.lower() in _HOP_BY_HOP: + if key.lower() in _RESPONSE_STRIP: + continue + if decoded and key.lower() in {"content-encoding", "content-length"}: continue with contextlib.suppress(ValueError, OSError): handler.send_header(key, value) + if decoded or not resp.headers.get("content-length"): + handler.send_header("Transfer-Encoding", "chunked") handler.end_headers() - for chunk in resp.iter_bytes(): - handler.wfile.write(chunk) + if decoded or not resp.headers.get("content-length"): + for chunk in resp.iter_bytes(): + handler.wfile.write(f"{len(chunk):x}\r\n".encode()) + handler.wfile.write(chunk) + handler.wfile.write(b"\r\n") + sent += len(chunk) + handler.wfile.flush() + handler.wfile.write(b"0\r\n\r\n") handler.wfile.flush() + else: + for chunk in resp.iter_bytes(): + handler.wfile.write(chunk) + sent += len(chunk) + handler.wfile.flush() except BrokenPipeError, ConnectionResetError, OSError: pass finally: resp.close() + return sent def _is_loopback(host: str) -> bool: @@ -298,17 +383,13 @@ def warn_if_exposed(host: str) -> None: if _is_loopback(host): return _log( - f"SECURITY: bridge binds to {host!r}, reachable from other machines. " + f"[bridge] SECURITY: bridge binds to {host!r}, reachable from other machines. " "It forwards your Authorization headers, so keep it on 127.0.0.1 " "(the default). Open ports to the outside world only if you truly " "intend to (Docker port publish / OPENROT_LISTEN)." ) -def _running_level(cfg_obj: cfg.Config, check: Callable[[cfg.Config], bool]) -> bool: - return cfg_obj.active_level != ActiveLevel.NONE and check(cfg_obj) - - def base_url(cfg_obj: cfg.Config) -> str: """Return the loopback baseURL opencode should target for the bridge.""" return f"http://127.0.0.1:{cfg_obj.bridge_port}/v1" @@ -329,7 +410,9 @@ def serve() -> None: through the cascade with 429 self-rotation. Ctrl-C stops it. """ cfg_obj = cfg.load_config() - if not _running_level(cfg_obj, cascade.level_serving): + if not ( + cfg_obj.active_level != ActiveLevel.NONE and cascade.level_serving(cfg_obj) + ): console.print("no active level, starting cascade...") cascade.start(False, False) cfg_obj = cfg.load_config() @@ -371,6 +454,7 @@ class BridgeHandler(BaseHTTPRequestHandler): """Forward every request to the configured upstream through the cascade.""" server_version = "openrot-bridge/1" + protocol_version = "HTTP/1.1" def _log_request_console(self, method: str) -> None: elapsed_s = self._elapsed_ms / 1000 @@ -396,6 +480,12 @@ def _log_request_console(self, method: str) -> None: parts.append(f"time={elapsed_s:.1f}s") else: parts.append(f"time={self._elapsed_ms:.0f}ms") + status = getattr(self, "_status", "") + if status: + parts.append(f"status={status}") + req_id = getattr(self, "_req_id", "") + if req_id: + parts.append(f"req={req_id}") _log(f"[bridge] {' '.join(parts)}") def _handle(self, method: str) -> None: @@ -419,37 +509,41 @@ def _handle(self, method: str) -> None: self._model = model self._prompt_chars = prompt_chars + self._req_id = next( + (v for k, v in self.headers.items() if k.lower() == "x-opencode-request"), + "", + ).rsplit("/", 1)[-1] cfg_obj = cfg.load_config() request = _Request(method, self.path, dict(self.headers.items()), body) + if os.environ.get("OPENROT_DEBUG_REQ"): + _debug_request(method, self.path, request.headers, request.body) semaphore = _get_semaphore(cfg_obj) t0 = time.monotonic() semaphore.acquire() try: resp, _client = forward(cfg_obj, request) - except UpstreamError as exc: - events.warning("bridge upstream error, rotating and retrying: %s", exc) - t_rotate = time.monotonic() - rotated = False - with contextlib.suppress(SystemExit): - rotated = cascade.rotate() - _log_rotation(rotated, time.monotonic() - t_rotate) + except httpx.HTTPError as exc: + events.warning("[bridge] upstream error, retrying: %s", exc) try: cfg_obj = cfg.load_config() resp, _client = forward(cfg_obj, request, rotate_on_429=False) - except UpstreamError as retry_exc: + except httpx.HTTPError as retry_exc: self._elapsed_ms = (time.monotonic() - t0) * 1000 self._output_chars = 0 + self._status = 502 self._log_request_console(method) _send_502(self, str(retry_exc)) return finally: semaphore.release() - self._output_chars = int(resp.headers.get("content-length", 0) or 0) + sent = 0 try: - _respond(self, resp) + sent = _respond(self, resp) finally: self._elapsed_ms = (time.monotonic() - t0) * 1000 + self._output_chars = sent + self._status = resp.status_code self._log_request_console(method) def do_GET(self) -> None: # noqa: D102 diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 43cc18e..8820e0b 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -10,6 +10,8 @@ from openrot import log, signals from openrot.config import ActiveLevel, Config, Node, NodeStatus, Strategy from openrot.core import daemon, health, nodes, proxy, refresh, rotator +from openrot.core.constants import IPIFY_URL +from openrot.core.http import make_client from openrot.providers import warp console = Console() @@ -84,13 +86,13 @@ def start_warp(foreground: bool) -> bool: """Connect WARP in proxy mode and front it with the local mixed proxy.""" cfg_obj = cfg.load_config() if not cfg_obj.warp_enabled: - events.info("warp disabled in config, using node chain") + events.info("[cascade] warp disabled in config, using node chain") return False if not warp.is_installed(): - events.info("WARP: available on host only (not found). Skipping.") + events.info("[cascade] WARP not available on this host, skipping") return False if not warp.connect(): - events.warning("warp connect failed; falling back to node chain") + events.warning("[cascade] warp connect failed; falling back to node chain") return False w_host, w_port = warp.proxy_address() @@ -100,19 +102,19 @@ def start_warp(foreground: bool) -> bool: ) except FileNotFoundError: events.warning( - "sing-box not found (%s). Install it: brew install sing-box", + "[cascade] sing-box not found (%s). Install it: brew install sing-box", cfg_obj.singbox_bin, ) return False except RuntimeError as exc: - events.warning("warp listener failed to start: %s", exc) + events.warning("[cascade] warp listener failed to start: %s", exc) return False proxy.save_pid(pid) cfg_obj.active_level = ActiveLevel.WARP cfg.save_config(cfg_obj) ip = warp.current_ip() events.info( - "warp active in proxy mode (127.0.0.1:%d -> socks5://%s:%d, ip=%s)", + "[cascade] warp active in proxy mode (127.0.0.1:%d -> socks5://%s:%d, ip=%s)", cfg_obj.port, w_host, w_port, @@ -127,11 +129,13 @@ def start_node(foreground: bool) -> None: """Start the node chain: pick from profiles by priority.""" cfg_obj = cfg.load_config() if not cfg_obj.all_nodes(): - events.warning("no nodes configured. Add a profile via 'openrot profile add'") + events.warning( + "[cascade] no nodes configured. Add a profile via 'openrot profile add'" + ) raise SystemExit(1) if not any(n.status == NodeStatus.ALIVE for n in cfg_obj.all_nodes()): - events.info("node: no alive node yet, running health check...") + events.info("[cascade] no alive node yet, running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() cfg.save_config(cfg_obj) @@ -139,7 +143,7 @@ def start_node(foreground: bool) -> None: node = rotator.pick(cfg_obj) if node is None: events.warning( - "no alive node available. Run 'openrot test' or 'openrot update'" + "[cascade] no alive node available. Run 'openrot test' or 'openrot update'" ) raise SystemExit(1) @@ -149,12 +153,12 @@ def start_node(foreground: bool) -> None: pid = launch(cfg_obj, node) except FileNotFoundError: events.warning( - "sing-box not found (%s). Install it: brew install sing-box", + "[cascade] sing-box not found (%s). Install it: brew install sing-box", cfg_obj.singbox_bin, ) raise SystemExit(1) from None except RuntimeError as exc: - events.warning("%s", exc) + events.warning("[cascade] %s", exc) raise SystemExit(1) from exc if foreground: @@ -165,7 +169,7 @@ def start_node(foreground: bool) -> None: node_health_loop() return - events.info("proxy started, pid %d, 127.0.0.1:%d", pid, cfg_obj.port) + events.info("[cascade] proxy started, pid %d, 127.0.0.1:%d", pid, cfg_obj.port) def _commit_health(fresh: cfg.Config, node_id: str, alive: bool) -> tuple[bool, int]: @@ -195,7 +199,7 @@ def node_health_loop() -> None: current = nodes.current_node(snapshot) if current is None: if snapshot.active_level == ActiveLevel.NODE: - events.warning("current node no longer in the pool, rotating") + events.warning("[cascade] current node no longer in the pool, rotating") with contextlib.suppress(SystemExit): rotate() continue @@ -206,7 +210,7 @@ def node_health_loop() -> None: ) or (False, 0) if reached: label = nodes.node_label(current) - events.warning("node %s failed %d times, rotating", label, fails) + events.warning("[cascade] node %s failed %d times, rotating", label, fails) with contextlib.suppress(SystemExit): rotate() @@ -218,7 +222,7 @@ def warp_health_loop() -> None: time.sleep(cfg_obj.health_interval) if warp.is_connected(): continue - events.warning("warp dropped; falling back to node chain") + events.warning("[cascade] warp dropped; falling back to node chain") proxy.stop_proxy() start_node(True) return @@ -256,9 +260,9 @@ def _rotate_inner(first: bool) -> None: if cfg_obj.active_level == ActiveLevel.WARP: if warp.rotate(): ip = warp.current_ip() - events.info("warp rotated, ip=%s", ip or "-") + events.info("[cascade] warp rotated, ip=%s", ip or "-") else: - events.warning("warp rotation failed") + events.warning("[cascade] warp rotation failed") return current = nodes.current_node(cfg_obj) @@ -267,7 +271,7 @@ def _rotate_inner(first: bool) -> None: cfg_obj.current_node_id = None if not any(n.status == NodeStatus.ALIVE for n in cfg_obj.all_nodes()): - events.info("node: no alive node, running health check...") + events.info("[cascade] no alive node, running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() cfg.save_config(cfg_obj) @@ -280,7 +284,7 @@ def _rotate_inner(first: bool) -> None: else: node = rotator.next_node(cfg_obj, current_id) if node is None: - events.info("node: no alive alternative, re-running health check...") + events.info("[cascade] no alive alternative, re-running health check...") health.test_all(cfg_obj) cfg_obj = cfg.load_config() node = ( @@ -290,21 +294,22 @@ def _rotate_inner(first: bool) -> None: ) cfg.save_config(cfg_obj) if node is None: - events.warning("rotation failed: no alive node available") + events.warning("[cascade] rotation failed: no alive node available") raise SystemExit(1) try: pid = launch(cfg_obj, node) except RuntimeError as exc: - events.warning("%s", exc) + events.warning("[cascade] %s", exc) raise SystemExit(1) from exc - events.info("rotated to node %s (pid %d)", nodes.node_label(node), pid) + events.info("[cascade] rotated to node %s (pid %d)", nodes.node_label(node), pid) def stop() -> None: """Stop the cascade daemon and the current level: WARP or local proxy.""" if daemon.stop(cfg.DAEMON_PID_PATH): console.print("daemon stopped") + events.info("[cascade] daemon stopped") cfg_obj = cfg.load_config() if cfg_obj.active_level == ActiveLevel.WARP: proxy.stop_proxy() @@ -312,12 +317,14 @@ def stop() -> None: cfg_obj.active_level = ActiveLevel.NONE cfg.save_config(cfg_obj) console.print("WARP disconnected") + events.info("[cascade] WARP disconnected") return if proxy.stop_proxy(): cfg_obj.current_node_id = None cfg_obj.active_level = ActiveLevel.NONE cfg.save_config(cfg_obj) console.print("Proxy stopped") + events.info("[cascade] proxy stopped") else: console.print("Proxy not running") @@ -336,10 +343,10 @@ def level_serving(cfg_obj: Config) -> bool: def probe_connectivity(cfg_obj: Config) -> dict[str, object]: """Fetch the egress IP through the local proxy to verify connectivity.""" try: - with httpx.Client( + with make_client( proxy=f"http://127.0.0.1:{cfg_obj.port}", timeout=15 ) as client: - resp = client.get("https://api.ipify.org?format=json") + resp = client.get(IPIFY_URL) resp.raise_for_status() ip = resp.json().get("ip", "?") return {"ok": True, "ip": ip} diff --git a/openrot/core/constants.py b/openrot/core/constants.py new file mode 100644 index 0000000..85e7b5c --- /dev/null +++ b/openrot/core/constants.py @@ -0,0 +1,4 @@ +"""Shared URL and tuning constants used across the package.""" + +HEALTH_URL = "https://www.gstatic.com/generate_204" +IPIFY_URL = "https://api.ipify.org?format=json" diff --git a/openrot/core/daemon.py b/openrot/core/daemon.py index f599323..e93ca87 100644 --- a/openrot/core/daemon.py +++ b/openrot/core/daemon.py @@ -14,9 +14,12 @@ from rich.console import Console +from openrot import log +from openrot.core.pid import PidFile from openrot.core.proxy import is_running, load_daemon_pid, save_daemon_pid console = Console() +events = log.get_logger() def command(subcommand: str) -> list[str]: @@ -61,10 +64,12 @@ def start(*, name: str, pid_path: Path) -> None: if existing is not None: if is_running(existing): console.print(f"[yellow]{name} daemon already running[/yellow]") + events.info("[daemon] %s already running (pid %d)", name, existing) return - pid_path.unlink(missing_ok=True) + PidFile(pid_path).remove() pid = _spawn_daemon(name, pid_path) console.print(f"{name} daemon started (pid {pid})") + events.info("[daemon] %s started (pid %d)", name, pid) def stop(pid_path: Path) -> bool: @@ -76,7 +81,7 @@ def stop(pid_path: Path) -> bool: os.kill(pid, signal.SIGTERM) except OSError: return False - pid_path.unlink(missing_ok=True) + PidFile(pid_path).remove() return True @@ -94,7 +99,7 @@ def stop_and_wait(pid_path: Path) -> bool: return False for _ in range(50): if not is_running(pid): - pid_path.unlink(missing_ok=True) + PidFile(pid_path).remove() return True time.sleep(0.1) return False diff --git a/openrot/core/health.py b/openrot/core/health.py index 16479ef..e3e0224 100644 --- a/openrot/core/health.py +++ b/openrot/core/health.py @@ -4,14 +4,16 @@ from openrot.config import Config, Node, NodeProtocol, NodeStatus, Strategy from openrot.core import verify from openrot.core.singbox import probe_vless -from openrot.providers import free +from openrot.providers import proxy from openrot.providers.vless import ParseError, parse_vless +events = log.get_logger() + def check_node(node: Node, cfg: Config) -> tuple[bool, float | None]: """Probe one node by protocol. Returns (alive, latency_ms).""" if node.protocol in (NodeProtocol.HTTP, NodeProtocol.SOCKS5): - return free.check_node(node, cfg) + return proxy.check_proxy_node(node, cfg) try: venode = parse_vless(node.raw) except ParseError: @@ -68,7 +70,7 @@ def test_all( ) if proxy_nodes: - candidates = free.fetch_candidates("\n".join(n.raw for n in proxy_nodes)) + candidates = proxy.fetch_candidates("\n".join(n.raw for n in proxy_nodes)) proxy_survivors = verify.verify_proxy_pool( candidates, cfg.health_timeout, @@ -84,7 +86,7 @@ def test_all( for (proto, host, port), latency, egress_ip in proxy_survivors } for node in proxy_nodes: - parsed = free.parse_proxy(node.raw) + parsed = proxy.parse_proxy(node.raw) entry = None if parsed is None else proxy_ok.get(parsed) alive_count += _apply_result( node, @@ -97,7 +99,7 @@ def test_all( total_output = len(vless_survivors) + len(proxy_survivors) dedup_dropped = total_input - total_output if dedup_dropped > 0: - log.get_logger().info("dedup: removed %d duplicate-IP nodes", dedup_dropped) + events.info("[health] dedup: removed %d duplicate-IP nodes", dedup_dropped) return alive_count diff --git a/openrot/core/http.py b/openrot/core/http.py new file mode 100644 index 0000000..d6e3193 --- /dev/null +++ b/openrot/core/http.py @@ -0,0 +1,32 @@ +"""Shared httpx client factory and egress-IP helper.""" + +import contextlib + +import httpx + +from openrot.core.constants import IPIFY_URL + + +def make_client( + *, + proxy: str | None = None, + timeout: float = 30, + follow_redirects: bool = False, +) -> httpx.Client: + """Build an ``httpx.Client`` with the common defaults.""" + kwargs: dict[str, object] = { + "timeout": timeout, + "follow_redirects": follow_redirects, + } + if proxy: + kwargs["proxy"] = proxy + return httpx.Client(**kwargs) + + +def get_egress_ip(client: httpx.Client) -> str | None: + """Fetch public IP from api.ipify.org through an existing proxy client.""" + with contextlib.suppress(Exception): + resp = client.get(IPIFY_URL, timeout=5) + if resp.status_code == 200: + return resp.json().get("ip") + return None diff --git a/openrot/core/nodes.py b/openrot/core/nodes.py index 00ef4cd..ee58aaf 100644 --- a/openrot/core/nodes.py +++ b/openrot/core/nodes.py @@ -1,16 +1,15 @@ from pathlib import Path -import httpx - from openrot import config as cfg from openrot.config import Node, NodeProtocol, Profile -from openrot.providers import free, vless +from openrot.core.http import make_client +from openrot.providers import proxy, vless def fetch_text(source: str) -> str: """Read a URL or a local file; raise on anything else.""" if source.startswith(("http://", "https://")): - with httpx.Client(timeout=30, follow_redirects=True) as client: + with make_client(timeout=30, follow_redirects=True) as client: resp = client.get(source) resp.raise_for_status() return resp.text @@ -25,20 +24,6 @@ def find_profile(cfg: cfg.Config, name: str) -> Profile | None: return next((p for p in cfg.profiles if p.name == name), None) -def node_from_records( - records: list[str], protocol: NodeProtocol = NodeProtocol.VLESS -) -> list[Node]: - """Build deduplicated Node objects from raw record strings.""" - nodes = [] - seen: set[str] = set() - for rec in records: - if rec in seen: - continue - seen.add(rec) - nodes.append(Node(id=cfg.node_id(rec), raw=rec, protocol=protocol)) - return nodes - - def current_node(cfg: cfg.Config) -> Node | None: """Return the node the cascade is currently serving, or None.""" if cfg.current_node_id is None: @@ -60,7 +45,7 @@ def node_label(node: Node) -> str: def node_address(node: Node) -> str: """Return host:port for a node, or '?' when unparseable.""" if node.protocol in (NodeProtocol.HTTP, NodeProtocol.SOCKS5): - host, port = free.host_port(node.raw) + host, port = proxy.host_port(node.raw) if host is None or port is None: return "?" return f"{host}:{port}" diff --git a/openrot/core/pid.py b/openrot/core/pid.py new file mode 100644 index 0000000..19cdaa0 --- /dev/null +++ b/openrot/core/pid.py @@ -0,0 +1,37 @@ +"""PID file handling: a small class shared by proxy, daemon, and bridge.""" + +import os +import tempfile +from pathlib import Path + + +class PidFile: + """A pid file on disk: save, load, and remove the stored pid. + + Uses atomic writes with 0600 permissions, and treats a missing or + non-integer file as "no pid". + """ + + def __init__(self, path: Path) -> None: + self.path = path + + def save(self, pid: int) -> None: + """Write `pid` atomically with 0600 permissions.""" + self.path.parent.mkdir(parents=True, exist_ok=True) + fd, tmp = tempfile.mkstemp(dir=self.path.parent, prefix=".", suffix=".tmp") + with os.fdopen(fd, "w") as f: + f.write(str(pid)) + Path(tmp).replace(self.path) + + def load(self) -> int | None: + """Read the stored pid, or None when absent or invalid.""" + if not self.path.exists(): + return None + try: + return int(self.path.read_text().strip()) + except ValueError: + return None + + def remove(self) -> None: + """Delete the pid file if present.""" + self.path.unlink(missing_ok=True) diff --git a/openrot/core/probe.py b/openrot/core/probe.py deleted file mode 100644 index b105d47..0000000 --- a/openrot/core/probe.py +++ /dev/null @@ -1,173 +0,0 @@ -"""Interactive step-by-step diagnostics for the active proxy stack. - -``openrot probe `` walks the "what is serving right now" decision tree and -prints each step: config, WARP status, direct connectivity, level selection, -and the request through the local proxy. The logic lives in ``core`` so the CLI -command stays a thin wrapper and the flow stays unit-testable. -""" - -from operator import attrgetter - -import httpx -from rich.console import Console - -from openrot import config as cfg -from openrot.config import ActiveLevel, Config, NodeStatus, Strategy -from openrot.core import cascade, health, nodes, proxy, rotator -from openrot.providers import warp - -console = Console() - - -def stage(title: str) -> None: - """Print a bold section heading for one probe step.""" - console.print(f"\n[bold]▶ {title}[/bold]") - - -def node_desc(n: cfg.Node) -> str: - """Human-readable description of a node (label, protocol, latency).""" - ms = f"{n.latency_ms:.1f}ms" if n.latency_ms is not None else "-" - return f"{nodes.node_label(n)} [{n.protocol.value}] {ms}" - - -def pipeline_stage(stage_name: str, kept: int, total: int) -> None: - """Stage callback for ``verify``: print the final kept/total tally.""" - console.print(f"verify {stage_name}: {kept}/{total}") - - -def direct_ip() -> None: - """Probe the egress IP without the proxy (direct connectivity baseline).""" - try: - with httpx.Client(timeout=10) as client: - resp = client.get("https://api.ipify.org?format=json") - resp.raise_for_status() - ip = resp.json().get("ip", "?") - except httpx.HTTPError: - ip = "?" - console.print(f" direct ip: {ip}") - - -def choose_warp(cfg_obj: Config) -> bool: - """Try to activate WARP for the probe; True when it came up.""" - if not cfg_obj.warp_enabled: - console.print(" warp disabled in config -> skip") - return False - if not warp.is_installed(): - console.print(" warp-cli not found -> skip") - return False - console.print(f" warp-cli: {warp.bin_path()} ({warp.status().value})") - console.print(" connecting WARP (proxy mode)...") - if cascade.start_warp(False): - return True - console.print(" warp connect failed -> fall back to node chain") - return False - - -def chain(cfg_obj: Config) -> Config: - """Describe and health-check the node chain, then pick a node.""" - for prof in sorted(cfg_obj.profiles, key=attrgetter("priority")): - alive = sum(1 for n in prof.nodes if n.status == NodeStatus.ALIVE) - console.print( - f" [{prof.name}] {prof.kind.value} prio={prof.priority}: " - f"{alive}/{len(prof.nodes)} alive" - ) - rule = ( - "lowest latency (urltest)" - if cfg_obj.strategy == Strategy.URLTEST - else "first alive in chain order (fallback)" - ) - console.print(f" strategy={cfg_obj.strategy.value} -> {rule}") - if not any(n.status == NodeStatus.ALIVE for n in cfg_obj.all_nodes()): - console.print(" no alive node yet -> running health check...") - health.test_all(cfg_obj, on_stage=pipeline_stage) - cfg.save_config(cfg_obj) - cfg_obj = cfg.load_config() - for prof in sorted(cfg_obj.profiles, key=attrgetter("priority")): - alive = sum(1 for n in prof.nodes if n.status == NodeStatus.ALIVE) - console.print( - f" [{prof.name}] after check: {alive}/{len(prof.nodes)} alive" - ) - for n in cfg_obj.all_nodes(): - if n.status == NodeStatus.ALIVE: - console.print(f" alive: {node_desc(n)}") - picked = rotator.pick(cfg_obj) - if picked is not None: - console.print(f" picked: {node_desc(picked)}") - else: - console.print("[red] no alive node to pick[/red]") - return cfg_obj - - -def seek(cfg_obj: Config, url: str) -> None: - """Request `url` through the local proxy and report the result.""" - if not cascade.level_serving(cfg_obj): - console.print(" no active stack -> nothing to request through") - return - with httpx.Client(proxy=f"http://127.0.0.1:{cfg_obj.port}", timeout=20) as client: - console.print(f" requesting {url}") - resp = client.get(url) - console.print( - f" {resp.status_code} {resp.reason_phrase} " - f"({len(resp.content)} bytes, {resp.elapsed.total_seconds():.2f}s)" - ) - try: - ip = resp.json().get("ip", "?") - except Exception: - ip = "?" - console.print(f" egress ip: {ip}") - - -def run(url: str) -> None: - """Probe the active stack end-to-end, printing every step taken.""" - cfg_obj = cfg.load_config() - stage("config") - console.print( - f" port={cfg_obj.port}, strategy={cfg_obj.strategy.value}, " - f"singbox={cfg_obj.singbox_bin}, warp_enabled={cfg_obj.warp_enabled}" - ) - - stage("warp status") - if warp.is_installed(): - console.print(f" warp-cli: {warp.bin_path()} ({warp.status().value})") - else: - console.print(" warp-cli: not found (host only)") - - direct_ip() - - stage("choose level") - if cascade.level_serving(cfg_obj): - if cfg_obj.active_level == ActiveLevel.WARP: - console.print(" stack already up: WARP") - else: - current = nodes.current_node(cfg_obj) - label = nodes.node_label(current) if current else "?" - print_pid = proxy.load_pid() - console.print( - f" stack already up: node {label}, pid {print_pid}, " - f"127.0.0.1:{cfg_obj.port}" - ) - elif choose_warp(cfg_obj): - cfg_obj = cfg.load_config() - else: - cfg_obj = chain(cfg_obj) - if not cfg_obj.all_nodes(): - console.print( - "[red] no nodes configured. add: 'openrot profile add'[/red]" - ) - return - console.print(" starting sing-box chain...") - try: - cascade.start_node(False) - except SystemExit: - console.print("[red] chain failed[/red]") - return - cfg_obj = cfg.load_config() - current = nodes.current_node(cfg_obj) - label = nodes.node_label(current) if current else "?" - pid = proxy.load_pid() - console.print(f" serving via {label}, pid {pid}, 127.0.0.1:{cfg_obj.port}") - - stage("request") - seek(cfg_obj, url) - stage("left running") - console.print(" 'openrot stop' to quit") diff --git a/openrot/core/proxy.py b/openrot/core/proxy.py index acfb5b2..d7157a8 100644 --- a/openrot/core/proxy.py +++ b/openrot/core/proxy.py @@ -1,18 +1,18 @@ import os import signal import subprocess -import tempfile import time from pathlib import Path from openrot import config as cfg from openrot.config import Node, NodeProtocol +from openrot.core.pid import PidFile from openrot.core.singbox import ( generate_free_config, generate_singbox_config, write_config, ) -from openrot.providers import free +from openrot.providers import proxy from openrot.providers.vless import VlessNode, parse_vless @@ -47,7 +47,7 @@ def start_free_proxy( def start_node(node: Node, port: int, singbox_bin: str) -> int: """Start any node by protocol: relay (sing-box) or proxy (http/socks5 outbound).""" if node.protocol in (NodeProtocol.HTTP, NodeProtocol.SOCKS5): - host, remote_port = free.host_port(node.raw) + host, remote_port = proxy.host_port(node.raw) if host is None or remote_port is None: raise RuntimeError(f"invalid proxy node address: {node.raw}") return start_free_proxy( @@ -73,11 +73,7 @@ def is_running(pid: int) -> bool: def _write_pid(pid: int, path: Path) -> None: """Write `pid` to `path` atomically with 0600 permissions.""" - path.parent.mkdir(parents=True, exist_ok=True) - fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=".", suffix=".tmp") - with os.fdopen(fd, "w") as f: - f.write(str(pid)) - Path(tmp).replace(path) + PidFile(path).save(pid) def save_pid(pid: int, path: Path = cfg.PID_PATH) -> None: @@ -87,12 +83,7 @@ def save_pid(pid: int, path: Path = cfg.PID_PATH) -> None: def load_pid(path: Path = cfg.PID_PATH) -> int | None: """Read a proxy pid from `path`, or None when absent or invalid.""" - if not path.exists(): - return None - try: - return int(path.read_text().strip()) - except ValueError: - return None + return PidFile(path).load() def save_daemon_pid(pid: int, path: Path = cfg.DAEMON_PID_PATH) -> None: @@ -102,12 +93,7 @@ def save_daemon_pid(pid: int, path: Path = cfg.DAEMON_PID_PATH) -> None: def load_daemon_pid(path: Path = cfg.DAEMON_PID_PATH) -> int | None: """Read the daemon pid from `path`, or None when absent or invalid.""" - if not path.exists(): - return None - try: - return int(path.read_text().strip()) - except ValueError: - return None + return PidFile(path).load() def stop_proxy(pid: int | None = None) -> bool: diff --git a/openrot/core/refresh.py b/openrot/core/refresh.py index 910d0e3..3d43116 100644 --- a/openrot/core/refresh.py +++ b/openrot/core/refresh.py @@ -10,23 +10,23 @@ from openrot.config import Config, Profile, ProfileKind from openrot.core import nodes, verify from openrot.core.verify import Stage -from openrot.providers import free, vless +from openrot.providers import proxy, vless console = Console() events = log.get_logger() -def refresh_profile( - prof: Profile, - cfg_obj: Config, - *, - on_stage: Stage | None = None, - on_progress: verify.ProgressFn | None = None, -) -> None: - """Fetch and replace a profile's nodes from its URL according to its kind.""" - prof.nodes = fetch_profile_nodes( - prof, cfg_obj, on_stage=on_stage, on_progress=on_progress - ) +def _default_stage_logger(name: str) -> Stage: + """Log a pipeline stage's final tally to the events file. + + Used when a caller (e.g. the scheduler) has no console progress bar: + ``[refresh] {name}: {stage} {kept}/{total}``. + """ + + def cb(stage: str, kept: int, total: int) -> None: + events.info("[refresh] %s: %s %d/%d", name, stage, kept, total) + + return cb def fetch_profile_nodes( @@ -39,18 +39,19 @@ def fetch_profile_nodes( """Fetch a profile's nodes from its URL and verify them (no mutation). Relay pools run the full TCP -> TLS -> sing-box check -> HTTP probe - pipeline; free proxy pools only TCP -> HTTP probe. The top vertices by - urltest latency are published (capped at ``cfg.top_limit``). + pipeline; proxy (http/socks5) pools only TCP -> HTTP probe. The top + vertices by urltest latency are published (capped at ``cfg.top_limit``). """ + stage_cb = on_stage or _default_stage_logger(prof.name) text = nodes.fetch_text(prof.url) if prof.kind == ProfileKind.PROXY: - candidates = free.fetch_candidates(text) + candidates = proxy.fetch_candidates(text) proxy_survivors = verify.verify_proxy_pool( candidates, cfg_obj.health_timeout, urltest_url=cfg_obj.urltest_url, limit=cfg_obj.top_limit, - on_stage=on_stage, + on_stage=stage_cb, on_progress=on_progress, max_workers=cfg_obj.max_workers, deduplicate_by_ip=cfg_obj.deduplicate_by_ip, @@ -63,7 +64,7 @@ def fetch_profile_nodes( cfg_obj.health_timeout, urltest_url=cfg_obj.urltest_url, limit=cfg_obj.top_limit, - on_stage=on_stage, + on_stage=stage_cb, on_progress=on_progress, max_workers=cfg_obj.max_workers, deduplicate_by_ip=cfg_obj.deduplicate_by_ip, @@ -124,5 +125,5 @@ def run_scheduler() -> None: ) for name, err in errors.items(): - events.warning("[%s] refresh failed: %s", name, err) + events.warning("[refresh] %s: refresh failed: %s", name, err) time.sleep(_tick_interval(cfg.load_config())) diff --git a/openrot/core/singbox.py b/openrot/core/singbox.py index 681e102..9352974 100644 --- a/openrot/core/singbox.py +++ b/openrot/core/singbox.py @@ -1,23 +1,25 @@ -import contextlib import json import socket import subprocess import tempfile import time +from dataclasses import asdict, dataclass, field from pathlib import Path from typing import Any import httpx -from pydantic import BaseModel from openrot import config as cfg from openrot import log +from openrot.core.constants import HEALTH_URL +from openrot.core.http import get_egress_ip, make_client from openrot.providers.vless import VlessNode -HEALTH_URL = "https://www.gstatic.com/generate_204" +events = log.get_logger() -class RealityTLSOptions(BaseModel): +@dataclass +class RealityTLSOptions: """TLS reality (public key + short id) options.""" enabled: bool = True @@ -25,14 +27,16 @@ class RealityTLSOptions(BaseModel): short_id: str = "" -class UTLSOptions(BaseModel): +@dataclass +class UTLSOptions: """uTLS client fingerprint options.""" enabled: bool = True fingerprint: str = "chrome" -class TLSOptions(BaseModel): +@dataclass +class TLSOptions: """TLS options for a VLESS outbound.""" enabled: bool = True @@ -41,48 +45,53 @@ class TLSOptions(BaseModel): utls: UTLSOptions | None = None -class TransportOptions(BaseModel): +@dataclass +class TransportOptions: """WebSocket transport options.""" type: str = "ws" path: str = "/" - headers: dict[str, str] = {} + headers: dict[str, str] = field(default_factory=dict) -class VLESSOutbound(BaseModel): +@dataclass +class VLESSOutbound: """sing-box outbound pointing at a single VLESS server.""" - type: str = "vless" - tag: str = "proxy" server: str server_port: int uuid: str + type: str = "vless" + tag: str = "proxy" flow: str = "" tls: TLSOptions | None = None transport: TransportOptions | None = None -class MixedInbound(BaseModel): +@dataclass +class MixedInbound: """Local mixed (http+socks5) inbound.""" + listen_port: int type: str = "mixed" tag: str = "mixed-in" listen: str = "127.0.0.1" - listen_port: int -class LogOptions(BaseModel): +@dataclass +class LogOptions: """sing-box logging options.""" level: str = "warn" -class SingBoxConfig(BaseModel): +@dataclass +class SingBoxConfig: """Minimal sing-box config for a local mixed inbound + vless outbound.""" - log: LogOptions = LogOptions() inbounds: list[MixedInbound] outbounds: list[VLESSOutbound] + log: LogOptions = field(default_factory=LogOptions) def _strip_none(value: Any) -> Any: @@ -132,7 +141,7 @@ def generate_singbox_config(node: VlessNode, port: int) -> dict[str, object]: inbounds=[MixedInbound(listen=cfg.listen_address(), listen_port=port)], outbounds=[outbound], ) - return _strip_none(cfg_model.model_dump(mode="json")) + return _strip_none(asdict(cfg_model)) def generate_free_config( @@ -215,13 +224,13 @@ def probe_vless( if proc.poll() is not None: err_f.seek(0) stderr_text = err_f.read().decode("utf-8", errors="replace") - log.get_logger().warning( - "sing-box exited during probe (%s): %s", + events.warning( + "[singbox] sing-box exited during probe (%s): %s", proc.returncode, stderr_text.strip(), ) return False, None, None - with httpx.Client( + with make_client( proxy=f"http://127.0.0.1:{port}", timeout=timeout ) as client: start = time.monotonic() @@ -231,18 +240,9 @@ def probe_vless( return False, None, None if 200 <= resp.status_code < 300: latency = round((time.monotonic() - start) * 1000, 1) - egress_ip = _get_egress_ip(client) + egress_ip = get_egress_ip(client) return True, latency, egress_ip return False, None, None finally: proc.terminate() cfg_path.unlink(missing_ok=True) - - -def _get_egress_ip(client: httpx.Client) -> str | None: - """Fetch public IP from api.ipify.org through an existing proxy client.""" - with contextlib.suppress(Exception): - resp = client.get("https://api.ipify.org?format=json", timeout=5) - if resp.status_code == 200: - return resp.json().get("ip") - return None diff --git a/openrot/core/verify.py b/openrot/core/verify.py index 398866e..179d9a1 100644 --- a/openrot/core/verify.py +++ b/openrot/core/verify.py @@ -19,7 +19,6 @@ from collections.abc import Callable, Iterable from concurrent.futures import ThreadPoolExecutor, as_completed from datetime import datetime -from typing import TypeVar from openrot.config import TOP_LIMIT, Node, NodeProtocol, NodeStatus, node_id from openrot.core.singbox import ( @@ -27,9 +26,9 @@ probe_vless, write_config, ) -from openrot.providers import free, vless +from openrot.providers import proxy, vless -MAX_WORKERS = 50 +MAX_WORKERS = 16 CHECK_LISTEN_PORT = 1 @@ -37,7 +36,6 @@ ProgressFn = Callable[[str, int, int], None] RelayCandidate = tuple[str, vless.VlessNode] ProxyKey = tuple[str, str, int] # (protocol, host, port) -T = TypeVar("T") def median(values: list[float]) -> float: @@ -307,7 +305,7 @@ def verify_proxy_pool( ) results = _run_probe( tcp_alive, - lambda key: free.probe_targets(key[0], key[1], key[2], timeout, urltest_url), + lambda key: proxy.probe_targets(key[0], key[1], key[2], timeout, urltest_url), on_stage, on_progress, max_workers, diff --git a/openrot/log.py b/openrot/log.py index a4fe992..81f85e1 100644 --- a/openrot/log.py +++ b/openrot/log.py @@ -1,26 +1,44 @@ import logging import logging.handlers +import os +import sys from openrot import config as cfg LOGGER_NAME = "openrot.events" _LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" +_CONSOLE_FORMAT = "%(message)s" _MAX_BYTES = 1_000_000 _BACKUP_COUNT = 3 +_DEBUG_ENV = "OPENROT_DEBUG_REQ" def get_logger() -> logging.Logger: - """Return the events logger, configured once to append to a rotating file.""" + """Return the events logger, configured once to append to a rotating file. + + A console handler (INFO, ``%(message)s``-only) is attached only when stdout + is a terminal, so internal events (rotations, starts, bridge requests) are + visible during interactive use but stay file-only in daemon/scripted runs. + ``OPENROT_DEBUG_REQ=1`` raises the *file* handler to DEBUG (request-level + dumps); the console handler never logs DEBUG. + """ logger = logging.getLogger(LOGGER_NAME) if logger.handlers: return logger - logger.setLevel(logging.INFO) cfg.LOG_PATH.parent.mkdir(parents=True, exist_ok=True) - handler = logging.handlers.RotatingFileHandler( + file_level = logging.DEBUG if os.environ.get(_DEBUG_ENV) else logging.INFO + logger.setLevel(file_level) + file_handler = logging.handlers.RotatingFileHandler( cfg.LOG_PATH, maxBytes=_MAX_BYTES, backupCount=_BACKUP_COUNT ) - handler.setFormatter(logging.Formatter(_LOG_FORMAT)) - logger.addHandler(handler) + file_handler.setLevel(file_level) + file_handler.setFormatter(logging.Formatter(_LOG_FORMAT)) + logger.addHandler(file_handler) + if sys.stdout.isatty(): + console_handler = logging.StreamHandler(sys.stdout) + console_handler.setLevel(logging.INFO) + console_handler.setFormatter(logging.Formatter(_CONSOLE_FORMAT)) + logger.addHandler(console_handler) logger.propagate = False cfg.LOG_PATH.chmod(0o600) return logger diff --git a/openrot/models/config.py b/openrot/models/config.py index 90e0d68..a7b625f 100644 --- a/openrot/models/config.py +++ b/openrot/models/config.py @@ -1,10 +1,12 @@ from pydantic import BaseModel, ConfigDict, Field +from openrot.core.constants import HEALTH_URL + from .enums import ActiveLevel, Strategy from .node import Node from .profile import Profile -DEFAULT_URLTEST_URL = "https://www.gstatic.com/generate_204" +DEFAULT_URLTEST_URL = HEALTH_URL DEFAULT_BRIDGE_UPSTREAM = "https://opencode.ai/zen" TOP_LIMIT = 20 # max nodes kept per profile pool after verification diff --git a/openrot/providers/free.py b/openrot/providers/proxy.py similarity index 80% rename from openrot/providers/free.py rename to openrot/providers/proxy.py index f5ec07c..9ea8e82 100644 --- a/openrot/providers/free.py +++ b/openrot/providers/proxy.py @@ -1,12 +1,12 @@ -import contextlib import time from urllib.parse import urlparse import httpx from openrot.config import Config, Node +from openrot.core.constants import HEALTH_URL +from openrot.core.http import get_egress_ip, make_client -HEALTH_URL = "https://www.gstatic.com/generate_204" CHECK_TIMEOUT = 4 @@ -30,8 +30,10 @@ def check_proxy( """Health-check a forward proxy, returning (alive, latency_ms).""" start = time.monotonic() try: - with httpx.Client( - proxy=f"{protocol}://{host}:{port}", timeout=timeout, follow_redirects=True + with make_client( + proxy=f"{protocol}://{host}:{port}", + timeout=timeout, + follow_redirects=True, ) as client: resp = client.get(HEALTH_URL) alive = resp.status_code == 204 @@ -56,7 +58,7 @@ def probe_targets( target = url or HEALTH_URL start = time.monotonic() try: - with httpx.Client( + with make_client( proxy=f"{protocol}://{host}:{port}", timeout=timeout, follow_redirects=True, @@ -66,20 +68,11 @@ def probe_targets( return [], None if 200 <= resp.status_code < 300: latency = round((time.monotonic() - start) * 1000, 1) - egress_ip = _get_egress_ip(client) + egress_ip = get_egress_ip(client) return [latency], egress_ip return [], None -def _get_egress_ip(client: httpx.Client) -> str | None: - """Fetch public IP from api.ipify.org through an existing proxy client.""" - with contextlib.suppress(Exception): - resp = client.get("https://api.ipify.org?format=json", timeout=5) - if resp.status_code == 200: - return resp.json().get("ip") - return None - - def fetch_candidates(text: str) -> list[tuple[str, str, int]]: """Parse unique (protocol, host, port) tuples from a proxy list body.""" candidates: dict[tuple[str, str, int], None] = {} @@ -96,7 +89,7 @@ def host_port(raw: str) -> tuple[str | None, int | None]: return parsed.hostname, parsed.port -def check_node(node: Node, cfg: Config) -> tuple[bool, float | None]: +def check_proxy_node(node: Node, cfg: Config) -> tuple[bool, float | None]: """Health-check a proxy node (protocol http|socks5) using cfg.health_timeout.""" host, port = host_port(node.raw) if host is None or port is None: diff --git a/openrot/providers/warp.py b/openrot/providers/warp.py index 26c3ed0..1cde016 100644 --- a/openrot/providers/warp.py +++ b/openrot/providers/warp.py @@ -8,11 +8,12 @@ from rich.console import Console from openrot import log +from openrot.core.constants import IPIFY_URL +from openrot.core.http import make_client WARP_BIN = "warp-cli" WARP_PROXY_HOST = "127.0.0.1" WARP_PROXY_PORT = 40000 -IPIFY_URL = "https://api.ipify.org?format=json" ROTATE_DELAY = float(os.environ.get("OPENROT_WARP_ROTATE_DELAY", "10")) ROTATE_MAX_ATTEMPTS = int(os.environ.get("OPENROT_WARP_ROTATE_ATTEMPTS", "5")) @@ -88,12 +89,12 @@ def connect() -> bool: """Bring WARP up in proxy mode and wait until it reports connected.""" if not is_installed(): return False - events.info("warp: connecting (proxy mode)...") + events.info("[warp] connecting (proxy mode)...") _, port = proxy_address() _run("mode", "proxy") _run("proxy", "port", str(port)) _run("connect") - events.info("waiting for WARP connection...") + events.info("[warp] waiting for WARP connection...") for _ in range(120): if is_connected(): return True @@ -113,7 +114,7 @@ def current_ip() -> str | None: """Fetch the egress IP through the WARP SOCKS proxy, or None on failure.""" host, port = proxy_address() try: - with httpx.Client(proxy=f"socks5://{host}:{port}", timeout=10) as client: + with make_client(proxy=f"socks5://{host}:{port}", timeout=10) as client: resp = client.get(IPIFY_URL) resp.raise_for_status() return resp.json().get("ip") @@ -138,7 +139,7 @@ def rotate() -> bool: if ROTATE_DELAY > 0: events.info( - "warp: waiting %.0fs before reconnect (attempt %d/%d)", + "[warp] waiting %.0fs before reconnect (attempt %d/%d)", ROTATE_DELAY, attempt, ROTATE_MAX_ATTEMPTS, @@ -146,19 +147,19 @@ def rotate() -> bool: time.sleep(ROTATE_DELAY) if not connect(): - events.warning("warp: reconnect failed on attempt %d", attempt) + events.warning("[warp] reconnect failed on attempt %d", attempt) continue new_ip = current_ip() if new_ip and new_ip != old_ip: events.info( - "warp: IP rotated %s → %s (attempt %d)", old_ip or "?", new_ip, attempt + "[warp] IP rotated %s → %s (attempt %d)", old_ip or "?", new_ip, attempt ) return True - events.info("warp: IP unchanged (%s), retrying...", new_ip or "?") + events.info("[warp] IP unchanged (%s), retrying...", new_ip or "?") - events.warning("warp: rotation exhausted, IP may not have changed") + events.warning("[warp] rotation exhausted, IP may not have changed") return is_connected() diff --git a/openrot/self_update.py b/openrot/self_update.py index 569a7cf..8610fa3 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -17,6 +17,7 @@ from openrot import __version__ from openrot import config as cfg from openrot.core import daemon, proxy +from openrot.core.http import make_client GH_REPO = "vispar-tech/openrot" RELEASE_URL = f"https://api.github.com/repos/{GH_REPO}/releases/latest" @@ -118,7 +119,7 @@ def check_for_update(client: httpx.Client | None = None) -> UpdateResult: """Check if a newer release is available.""" close = client is None if client is None: - client = httpx.Client(follow_redirects=True) + client = make_client(follow_redirects=True) try: latest_tag = _fetch_latest_tag(client) except Exception as exc: @@ -146,7 +147,7 @@ def check_for_update(client: httpx.Client | None = None) -> UpdateResult: return UpdateResult( current=__version__, latest=latest_tag.lstrip("v"), - updated=False, + updated=True, message=f"update available: {__version__} -> {latest_tag.lstrip('v')}", ) @@ -162,7 +163,7 @@ def perform_update( _stop_services() close = client is None if client is None: - client = httpx.Client(follow_redirects=True) + client = make_client(follow_redirects=True) try: result = _do_update(client, progress_fn) finally: diff --git a/tests/integration/test_full_loop.py b/tests/integration/test_full_loop.py index 0dabd96..7762cc3 100644 --- a/tests/integration/test_full_loop.py +++ b/tests/integration/test_full_loop.py @@ -1,8 +1,8 @@ """Integration tests: full CLI loop against real sources and real sing-box. -The happy path exercises `profile add` -> `update` -> `test` -> `probe` -(assert a real egress IP), then fakes a node failure (kill the serving -sing-box + mark the current node DEAD) and verifies `rotate`. External +The happy path exercises `profile add` -> `update` -> `test` (assert a real +egress IP through the local proxy), then fakes a node failure (kill the +serving sing-box + mark the current node DEAD) and verifies `rotate`. External sources and the network are not under our control, so the test skips gracefully when sing-box is missing or no node stays alive. """ @@ -40,7 +40,7 @@ ) PROXY_PROXIFLY = "https://cdn.jsdelivr.net/gh/proxifly/free-proxy-list@main/proxies/protocols/http/data.txt" TRIM_PER_PROFILE = 6 -EGRESS_RE = re.compile(r"egress ip: (\d+\.\d+\.\d+\.\d+)") +EGRESS_RE = re.compile(r'"ip":\s*"(\d+\.\d+\.\d+\.\d+)"') REPO_ROOT = Path(__file__).resolve().parents[2] @@ -139,11 +139,18 @@ def _alive_nodes(cfg_path: Path) -> list[Node]: def _expect_egress(basedir: Path, port: int) -> str: - proc = _openrot(basedir, port, "probe", EGRESS_URL, timeout=120) - _skip_on_transient_error(proc) - assert proc.returncode == 0, proc.stdout + proc.stderr - match = EGRESS_RE.search(proc.stdout) - assert match, proc.stdout + env = _env(basedir, port) + body = subprocess.run( # noqa: S603 + ["curl", "-s", "-x", f"http://127.0.0.1:{port}", EGRESS_URL], # noqa: S607 + capture_output=True, + text=True, + timeout=120, + env=env, + ) + _skip_on_transient_error(body) + assert body.returncode == 0, body.stdout + body.stderr + match = EGRESS_RE.search(body.stdout) + assert match, body.stdout return match.group(1) diff --git a/tests/unit/cli/test_cli_commands.py b/tests/unit/cli/test_cli_commands.py index 8754306..3ceadab 100644 --- a/tests/unit/cli/test_cli_commands.py +++ b/tests/unit/cli/test_cli_commands.py @@ -6,6 +6,7 @@ required. """ +import json from pathlib import Path import pytest @@ -80,7 +81,6 @@ def test_profile_list_empty(monkeypatch: pytest.MonkeyPatch) -> None: def test_profile_list_json(monkeypatch: pytest.MonkeyPatch) -> None: - import json monkeypatch.setattr(cli.cfg, "load_config", lambda: Config(profiles=[_profile()])) result = runner.invoke(cli.app, ["profile", "list", "--json"]) @@ -169,7 +169,6 @@ def test_test_command_no_nodes(monkeypatch: pytest.MonkeyPatch) -> None: def test_test_command_json(monkeypatch: pytest.MonkeyPatch) -> None: - import json node = _node() cfg_obj = Config(profiles=[_profile(nodes=[node])]) @@ -244,7 +243,6 @@ def test_status_node(monkeypatch: pytest.MonkeyPatch) -> None: assert "bridge: stopped" in result.output # current node reports its position in the queue (index/total) - import json result_json = runner.invoke(cli.app, ["status", "--json"]) assert result_json.exit_code == 0 @@ -268,7 +266,6 @@ def test_status_warp_json_reports_real_health( monkeypatch: pytest.MonkeyPatch, ) -> None: """warp_connected must reflect an actual probe through WARP, not the flag.""" - import json cfg_obj = Config(active_level=ActiveLevel.WARP) monkeypatch.setattr(cli.cfg, "load_config", lambda: cfg_obj) @@ -303,7 +300,6 @@ def test_status_bridge_running_node_level( monkeypatch: pytest.MonkeyPatch, ) -> None: """A live bridge shows its URL in the human output and true in JSON.""" - import json cfg_obj = Config( active_level=ActiveLevel.NODE, @@ -336,12 +332,12 @@ def test_update(monkeypatch: pytest.MonkeyPatch) -> None: def fake_refresh( p: Profile, cfg_obj: object, on_stage: object = None, on_progress: object = None - ) -> None: - p.__setattr__("nodes", []) + ) -> list[Node]: if callable(on_stage): on_stage("tcp", 0, 0) + return [] - monkeypatch.setattr(cli.refresh, "refresh_profile", fake_refresh) + monkeypatch.setattr(cli.refresh, "fetch_profile_nodes", fake_refresh) monkeypatch.setattr(cli.cfg, "save_config", lambda c, path=None: None) result = runner.invoke(cli.app, ["update"]) assert result.exit_code == 0 @@ -406,66 +402,6 @@ def test_status_verbose_shows_diagnostics( assert "proxy" in result.output -def test_probe_serving( - monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] -) -> None: - cfg_obj = Config( - active_level=ActiveLevel.NODE, current_node_id="n1", profiles=[_profile()] - ) - monkeypatch.setattr(cli.cfg, "load_config", lambda: cfg_obj) - monkeypatch.setattr(cli.warp, "is_installed", lambda: False) - monkeypatch.setattr(cli.cascade, "level_serving", lambda c: True) - monkeypatch.setattr(cli.proxy, "load_pid", lambda: 1) - monkeypatch.setattr(cli.node_ops, "node_label", lambda n: "label") - - class _Resp: - status_code = 200 - reason_phrase = "OK" - content = b"{}" - elapsed = type("E", (), {"total_seconds": lambda s: 0.1})() - - def json(self) -> dict[str, str]: - return {"ip": "1.2.3.4"} - - def raise_for_status(self) -> None: - return None - - monkeypatch.setattr(cli.probe_core.httpx.Client, "get", lambda self, url: _Resp()) - result = runner.invoke(cli.app, ["probe", "https://example.test"]) - assert result.exit_code == 0 - assert "egress ip: 1.2.3.4" in result.output - - -def test_probe_runs_health_check(monkeypatch: pytest.MonkeyPatch) -> None: - node = _node() - node.status = NodeStatus.UNKNOWN - cfg_obj = Config(active_level=ActiveLevel.NODE, profiles=[_profile(nodes=[node])]) - monkeypatch.setattr(cli.cfg, "load_config", lambda: cfg_obj) - monkeypatch.setattr(cli.warp, "is_installed", lambda: False) - monkeypatch.setattr(cli.cascade, "level_serving", lambda c: False) - monkeypatch.setattr(cli.cascade, "start_node", lambda foreground: None) - monkeypatch.setattr(cli.node_ops, "node_label", lambda n: "label") - monkeypatch.setattr(cli.proxy, "load_pid", lambda: 123) - monkeypatch.setattr(cli.rotator, "pick", lambda c: node) - monkeypatch.setattr(cli.cfg, "save_config", lambda c, path=None: None) - - def fake_test_all(c: Config, on_stage: object = None) -> int: - assert on_stage is not None - for n in c.all_nodes(): - n.status = NodeStatus.ALIVE - n.latency_ms = 5.0 - if callable(on_stage): - on_stage("tcp", 1, 1) - on_stage("probe", 1, 1) - return 1 - - monkeypatch.setattr(cli.health, "test_all", fake_test_all) - result = runner.invoke(cli.app, ["probe", "https://example.test"]) - assert result.exit_code == 0 - assert "verify tcp: 1/1" in result.output - assert "verify probe: 1/1" in result.output - - def test_warp_install_already(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(cli.warp, "is_installed", lambda: True) monkeypatch.setattr(cli.warp, "bin_path", lambda: "/usr/bin/warp-cli") @@ -503,7 +439,6 @@ def test_warp_status(monkeypatch: pytest.MonkeyPatch) -> None: def test_warp_status_json(monkeypatch: pytest.MonkeyPatch) -> None: - import json monkeypatch.setattr(cli.cfg, "load_config", lambda: Config(warp_enabled=True)) monkeypatch.setattr(cli.warp, "status", lambda: WarpStatus.CONNECTED) diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 99341fc..4ecd0b5 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -4,8 +4,11 @@ merged into ``openrot.core.bridge``, and their tests live here too. """ +import gzip import socket +from io import BytesIO from pathlib import Path +from types import SimpleNamespace from typing import Self import httpx @@ -82,8 +85,11 @@ def __init__(self, responses: list[httpx.Response]) -> None: self.closed = False self.last_url: str | None = None - def request(self, method: str, url: str, **kw: object) -> httpx.Response: - self.last_url = url + def build_request(self, method: str, url: str, **kw: object) -> httpx.Request: + return httpx.Request(method, url, **kw) + + def send(self, request: httpx.Request, *, stream: bool = True) -> httpx.Response: + self.last_url = str(request.url) return self._responses.pop(0) def close(self) -> None: @@ -115,6 +121,25 @@ def test_forward_returns_upstream_response( assert fake.last_url == "https://up.test/v1/v1/chat/completions" +def test_fetch_forces_accept_encoding_identity() -> None: + captured: dict[str, object] = {} + + class SpyClient: + def build_request(self, method: str, url: str, **kw: object) -> httpx.Request: + return httpx.Request(method, url, **kw) + + def send( + self, request: httpx.Request, *, stream: bool = True + ) -> httpx.Response: + captured["headers"] = dict(request.headers) + return httpx.Response(200, content=b"ok") + + bridge._fetch(SpyClient(), _cfg(), _req()) + headers = captured["headers"] + assert isinstance(headers, dict) + assert headers.get("accept-encoding") == "identity" + + def test_forward_rotates_and_retries_on_usage_limit_error( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -308,12 +333,17 @@ def test_fetch_raises_upstream_error_on_network_failure( monkeypatch: pytest.MonkeyPatch, ) -> None: class BoomClient: - def request(self, method: str, url: str, **kw: object) -> httpx.Response: + def build_request(self, method: str, url: str, **kw: object) -> httpx.Request: + return httpx.Request(method, url, **kw) + + def send( + self, request: httpx.Request, *, stream: bool = True + ) -> httpx.Response: raise httpx.ConnectError("boom") monkeypatch.setattr(bridge, "_get_client", lambda cfg: BoomClient()) bridge._shared_client = None - with pytest.raises(bridge.UpstreamError): + with pytest.raises(httpx.HTTPError): bridge.forward(_cfg(), _req()) @@ -337,9 +367,6 @@ def test_reset_client_closes_and_discards() -> None: def test_send_502(monkeypatch: pytest.MonkeyPatch) -> None: - from io import BytesIO - from types import SimpleNamespace - status_codes: list[int] = [] headers: list[tuple[str, str]] = [] @@ -391,10 +418,14 @@ def serve_forever(self) -> None: def server_close(self) -> None: pass - monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: False) + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: False) started: list[tuple[bool, bool]] = [] monkeypatch.setattr(bridge.cascade, "start", lambda f, d: started.append((f, d))) - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) monkeypatch.setattr(bridge, "Bridge", FakeServer) bridge.serve() assert started == [(False, False)] @@ -412,10 +443,14 @@ def serve_forever(self) -> None: def server_close(self) -> None: pass - monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) started: list = [] monkeypatch.setattr(bridge.cascade, "start", lambda *a: started.append(a)) - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) monkeypatch.setattr(bridge, "Bridge", FakeServer) bridge.serve() assert started == [] @@ -458,13 +493,6 @@ def fake_stop(path: Path) -> bool: assert called == [pid_path] -def test_running_level_combinations() -> None: - assert bridge._running_level(_cfg(), lambda c: True) is False - conf = Config(active_level=ActiveLevel.NODE) - assert bridge._running_level(conf, lambda c: True) is True - assert bridge._running_level(conf, lambda c: False) is False - - def test_warn_if_exposed_silent_on_loopback(monkeypatch: pytest.MonkeyPatch) -> None: logged: list[str] = [] monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) @@ -498,8 +526,12 @@ def server_close(self) -> None: pass logged: list[str] = [] - monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) monkeypatch.setattr(bridge.cfg, "listen_address", lambda: "0.0.0.0") monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -531,8 +563,12 @@ def server_close(self) -> None: printed: list[str] = [] console_printed: list[str] = [] - monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -561,8 +597,12 @@ def server_close(self) -> None: printed: list[str] = [] console_printed: list[str] = [] - monkeypatch.setattr(bridge, "_running_level", lambda cfg_obj, check: True) - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_port=7891)) + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -577,9 +617,6 @@ def server_close(self) -> None: def test_respond_streams_body_and_skips_hop_by_hop() -> None: - from io import BytesIO - from types import SimpleNamespace - sent_headers: list[tuple[str, str]] = [] def send_header(key: str, value: str) -> None: @@ -594,17 +631,72 @@ def send_header(key: str, value: str) -> None: ) resp = httpx.Response( 200, - headers={"Content-Type": "text/plain", "Connection": "keep-alive"}, + headers={ + "Content-Type": "text/plain", + "Content-Length": "5", + "Connection": "keep-alive", + }, content=b"hello", ) bridge._respond(handler, resp) assert wfile.getvalue() == b"hello" assert not any(k.lower() == "connection" for k, _ in sent_headers) + assert any(h.lower() == "content-length" and v == "5" for h, v in sent_headers) -def test_respond_survives_broken_pipe() -> None: - from types import SimpleNamespace +def test_respond_chunks_when_no_content_length() -> None: + sent_headers: list[tuple[str, str]] = [] + + def send_header(key: str, value: str) -> None: + sent_headers.append((key, value)) + + handler = SimpleNamespace( + send_response=lambda code: None, + send_header=send_header, + end_headers=lambda: None, + wfile=BytesIO(), + ) + resp = httpx.Response( + 200, + headers={"Content-Type": "text/event-stream"}, + content=b"data: x\n\n", + ) + resp.headers.pop("content-length") + bridge._respond(handler, resp) + assert bytes(handler.wfile.getvalue()) == b"9\r\ndata: x\n\n\r\n0\r\n\r\n" + assert any( + k.lower() == "transfer-encoding" and v == "chunked" for k, v in sent_headers + ) + +def test_respond_skips_content_encoding_when_body_decoded() -> None: + sent_headers: list[tuple[str, str]] = [] + + def send_header(key: str, value: str) -> None: + sent_headers.append((key, value)) + + handler = SimpleNamespace( + send_response=lambda code: None, + send_header=send_header, + end_headers=lambda: None, + wfile=BytesIO(), + ) + resp = httpx.Response( + 200, + headers={ + "Content-Type": "application/json", + "Content-Encoding": "gzip", + "Content-Length": "123", + }, + content=gzip.compress(b'{"ok":true}'), + ) + bridge._respond(handler, resp) + assert bytes(handler.wfile.getvalue()) == b'b\r\n{"ok":true}\r\n0\r\n\r\n' + assert not any(k.lower() == "content-encoding" for k, _ in sent_headers) + assert not any(k.lower() == "content-length" for k, _ in sent_headers) + + +def test_respond_survives_broken_pipe() -> None: class BoomWfile: def write(self, chunk: bytes) -> None: raise BrokenPipeError @@ -622,8 +714,6 @@ def write(self, chunk: bytes) -> None: def test_handler_returns_502_on_upstream_error( monkeypatch: pytest.MonkeyPatch, ) -> None: - from types import SimpleNamespace - status: list[int] = [] written: list[bytes] = [] headers: list[tuple[str, str]] = [] @@ -640,7 +730,7 @@ def test_handler_returns_502_on_upstream_error( monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) def boom(cfg_obj: object, request: object, **kwargs: object) -> object: - raise bridge.UpstreamError("up boom") + raise httpx.HTTPError("up boom") monkeypatch.setattr(bridge, "forward", boom) handler._handle("POST") @@ -651,8 +741,6 @@ def boom(cfg_obj: object, request: object, **kwargs: object) -> object: def test_handler_logs_request_elapsed_and_model( monkeypatch: pytest.MonkeyPatch, ) -> None: - from types import SimpleNamespace - body = b'{"model":"gpt-4o","messages":[{"content":"hello world"}]}' handler = bridge.BridgeHandler.__new__(bridge.BridgeHandler) handler.headers = SimpleNamespace(get=lambda k, d=0: len(body), items=list) @@ -684,11 +772,74 @@ def fake_forward(cfg_obj: object, request: object, **kw: object) -> object: assert any(k in line for k in ("ms", "s")) -def test_handler_rotates_and_retries_after_upstream_error( +def test_respond_counts_streamed_output_bytes() -> None: + """output= reflects real bytes written, not (absent) content-length.""" + written: list[bytes] = [] + + class _Wfile: + def write(self, chunk: bytes) -> int: + written.append(chunk) + return len(chunk) + + def flush(self) -> None: + pass + + class _Handler: + wfile = _Wfile() + + def send_response(self, code: int) -> None: + pass + + def send_header(self, key: str, value: str) -> None: + pass + + def end_headers(self) -> None: + pass + + resp = httpx.Response(200, content=iter([b"abc", b"de"]), headers={}) + handler = _Handler() + assert bridge._respond(handler, resp) == 5 + assert written # body went through wfile + + +def test_handler_logs_status_and_request_id(monkeypatch: pytest.MonkeyPatch) -> None: + body = b'{"model":"gpt-4o","messages":[{"content":"hi"}]}' + req_id = "ses_abc123/msg_a1b2c3d4e5f6g7h8i9j0" + handler = bridge.BridgeHandler.__new__(bridge.BridgeHandler) + handler.headers = SimpleNamespace( + get=lambda k, d="": len(body) if k == "Content-Length" else d, + items=lambda: [("x-opencode-request", req_id)], + ) + handler.rfile = SimpleNamespace(read=lambda n: body) + handler.path = "/v1/chat/completions" + handler.wfile = SimpleNamespace(write=lambda b: None) + handler.send_response = lambda code: None + handler.send_header = lambda k, v: None + handler.end_headers = lambda: None + + logged: list[str] = [] + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge, "_respond", lambda handler, resp: 1500) + monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + + resp = httpx.Response(429, content=b"x") + client = _FakeClient([resp]) + + def fake_forward(cfg_obj: object, request: object, **kw: object) -> object: + return (resp, client) + + monkeypatch.setattr(bridge, "forward", fake_forward) + handler._handle("POST") + + line = next(msg for msg in logged if msg.startswith("[bridge] POST")) + assert "output=1.5k" in line + assert "status=429" in line + assert "req=msg_a1b2c3d4e5f6g7h8i9j0" in line + + +def test_handler_retries_without_rotation_after_upstream_error( monkeypatch: pytest.MonkeyPatch, ) -> None: - from types import SimpleNamespace - handler = bridge.BridgeHandler.__new__(bridge.BridgeHandler) handler.headers = SimpleNamespace(get=lambda k, d=0: 0, items=list) handler.rfile = SimpleNamespace(read=lambda n: b"") @@ -699,11 +850,13 @@ def test_handler_rotates_and_retries_after_upstream_error( handler.end_headers = lambda: None logged: list[str] = [] - rotated: list = [] + warning_called = {"n": 0} monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) + monkeypatch.setattr( + bridge.events, "warning", lambda msg, *a: warning_called.__setitem__("n", 1) + ) monkeypatch.setattr(bridge, "_respond", lambda handler, resp: None) - monkeypatch.setattr(bridge.cascade, "rotate", lambda: (rotated.append(1), True)[1]) resp = httpx.Response(200, content=b"ok") client = _FakeClient([resp]) @@ -712,17 +865,16 @@ def test_handler_rotates_and_retries_after_upstream_error( def flaky_forward(cfg_obj: object, request: object, **kw: object) -> object: calls.append(kw.get("rotate_on_429", True)) if not calls or len(calls) == 1: - raise bridge.UpstreamError("conn drop") + raise httpx.HTTPError("conn drop") return (resp, client) monkeypatch.setattr(bridge, "forward", flaky_forward) handler._handle("POST") - assert len(rotated) == 1 - assert any("rotation took" in msg for msg in logged) - assert any( - msg.startswith("[bridge] POST") and ("ms" in msg or "s" in msg) - for msg in logged - ) + assert warning_called["n"] == 1 + assert len(calls) == 2 + assert calls[0] is True + assert calls[1] is False + assert not any("rotation" in msg for msg in logged) def test_bridge_server_binds_and_closes() -> None: diff --git a/tests/unit/core/test_health.py b/tests/unit/core/test_health.py index 2541cdc..bcde32d 100644 --- a/tests/unit/core/test_health.py +++ b/tests/unit/core/test_health.py @@ -1,7 +1,9 @@ import pytest from openrot.core.health import check_node, select_node -from openrot.models import Node, NodeProtocol, NodeStatus, Strategy +from openrot.core.health import test_all as _test_all +from openrot.models import Config, Node, NodeProtocol, NodeStatus, Profile, Strategy +from openrot.providers.vless import ParseError def _node( @@ -43,9 +45,6 @@ def test_select_returns_none_when_no_alive() -> None: def test_all_updates_statuses(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config, Profile - from openrot.core.health import test_all - c = Config( profiles=[ Profile( @@ -61,7 +60,7 @@ def fake_pool( return [(("r1", object()), 10.0, None)] monkeypatch.setattr("openrot.core.health.verify.verify_vless_pool", fake_pool) - assert test_all(c) == 1 + assert _test_all(c) == 1 assert c.all_nodes()[0].status == NodeStatus.ALIVE assert c.all_nodes()[0].latency_ms == 10.0 assert c.all_nodes()[1].status == NodeStatus.UNKNOWN @@ -69,9 +68,6 @@ def fake_pool( def test_all_passes_on_stage(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config, Profile - from openrot.core.health import test_all - c = Config(profiles=[Profile(name="a", nodes=[Node(id="n1", raw="r1")])]) recorded: list[tuple[str, object, object]] = [] @@ -82,14 +78,11 @@ def fake_pool(*a: object, **k: object) -> list[object]: return [] monkeypatch.setattr("openrot.core.health.verify.verify_vless_pool", fake_pool) - assert test_all(c, on_stage=lambda *x: recorded.append(x)) == 0 + assert _test_all(c, on_stage=lambda *x: recorded.append(x)) == 0 assert recorded == [("tcp", 1, 1)] def test_all_forwards_urltest_url(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config, Profile - from openrot.core.health import test_all - c = Config(profiles=[Profile(name="a", nodes=[Node(id="n1", raw="r1")])]) c.urltest_url = "https://probe.example/x" seen: dict[str, object] = {} @@ -101,7 +94,7 @@ def fake_pool( return [] monkeypatch.setattr("openrot.core.health.verify.verify_vless_pool", fake_pool) - test_all(c) + _test_all(c) assert seen["urltest_url"] == "https://probe.example/x" @@ -111,28 +104,24 @@ def test_select_urltest_returns_none_without_latencies() -> None: def test_check_node_http_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config - node = _node("n1") node.protocol = NodeProtocol.HTTP - monkeypatch.setattr("openrot.core.health.free.check_node", lambda n, c: (True, 7.0)) + monkeypatch.setattr( + "openrot.core.health.proxy.check_proxy_node", lambda n, c: (True, 7.0) + ) assert check_node(node, Config()) == (True, 7.0) def test_check_node_socks5_delegates(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config - node = _node("n1") node.protocol = NodeProtocol.SOCKS5 monkeypatch.setattr( - "openrot.core.health.free.check_node", lambda n, c: (False, None) + "openrot.core.health.proxy.check_proxy_node", lambda n, c: (False, None) ) assert check_node(node, Config()) == (False, None) def test_check_node_vless_probes(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Config - node = _node("n1") parsed = object() monkeypatch.setattr("openrot.core.health.parse_vless", lambda raw: parsed) @@ -145,9 +134,6 @@ def test_check_node_vless_probes(monkeypatch: pytest.MonkeyPatch) -> None: def test_check_node_malformed_returns_dead( monkeypatch: pytest.MonkeyPatch, ) -> None: - from openrot.config import Config - from openrot.providers.vless import ParseError - node = _node("n1") node.protocol = NodeProtocol.VLESS monkeypatch.setattr( @@ -160,9 +146,6 @@ def test_check_node_malformed_returns_dead( def test_apply_result_marks_dead_on_threshold( monkeypatch: pytest.MonkeyPatch, ) -> None: - from openrot.config import Config, Profile - from openrot.core.health import test_all - c = Config( fail_threshold=3, profiles=[Profile(name="a", nodes=[Node(id="n1", raw="r1", fails=2)])], @@ -170,6 +153,6 @@ def test_apply_result_marks_dead_on_threshold( monkeypatch.setattr( "openrot.core.health.verify.verify_vless_pool", lambda *a, **k: [] ) - assert test_all(c) == 0 + assert _test_all(c) == 0 assert c.all_nodes()[0].status == NodeStatus.DEAD assert c.all_nodes()[0].fails == 3 diff --git a/tests/unit/core/test_nodes.py b/tests/unit/core/test_nodes.py index 5151fcc..b7fdde2 100644 --- a/tests/unit/core/test_nodes.py +++ b/tests/unit/core/test_nodes.py @@ -11,15 +11,6 @@ def _profile(name: str, **kw: Any) -> Profile: return Profile(name=name, **kw) -def test_node_from_records_dedupes_and_sets_protocol() -> None: - a = "vless://aaa@example.com:443#A" - records = [a, a, "vless://bbb@other.com:80#B"] - result = nodes.node_from_records(records, NodeProtocol.VLESS) - assert [n.raw for n in result] == [a, "vless://bbb@other.com:80#B"] - assert all(n.id.startswith("node-") for n in result) - assert all(n.protocol == NodeProtocol.VLESS for n in result) - - def test_current_node_by_id() -> None: prof = _profile("x", nodes=[Node(id="n1", raw="r1"), Node(id="n2", raw="r2")]) c = Config(profiles=[prof], current_node_id="n2") diff --git a/tests/unit/core/test_proxy.py b/tests/unit/core/test_proxy.py index 4909fc6..c9f717e 100644 --- a/tests/unit/core/test_proxy.py +++ b/tests/unit/core/test_proxy.py @@ -2,6 +2,7 @@ import pytest +from openrot.config import Node, NodeProtocol from openrot.core import proxy @@ -81,8 +82,6 @@ def poll(self) -> None: def test_start_node_http_uses_free_proxy(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Node, NodeProtocol - captured: list[tuple[object, ...]] = [] node = Node(id="n1", raw="http://proxy.host:8080", protocol=NodeProtocol.HTTP) monkeypatch.setattr( @@ -95,8 +94,6 @@ def test_start_node_http_uses_free_proxy(monkeypatch: pytest.MonkeyPatch) -> Non def test_start_node_proxy_invalid_address(monkeypatch: pytest.MonkeyPatch) -> None: - from openrot.config import Node, NodeProtocol - node = Node(id="n1", raw="http://", protocol=NodeProtocol.HTTP) with pytest.raises(RuntimeError, match="invalid proxy node"): proxy.start_node(node, 7000, "sing-box") diff --git a/tests/unit/core/test_refresh.py b/tests/unit/core/test_refresh.py index 2aca90a..10c0789 100644 --- a/tests/unit/core/test_refresh.py +++ b/tests/unit/core/test_refresh.py @@ -2,6 +2,7 @@ import pytest +from openrot import config as cfg from openrot.config import Config, Node, Profile, ProfileKind from openrot.core import refresh @@ -62,7 +63,7 @@ def test_fetch_profile_nodes_proxy(monkeypatch: pytest.MonkeyPatch) -> None: prof = Profile(name="p", kind=ProfileKind.PROXY, url="https://x.test/list") monkeypatch.setattr(refresh.nodes, "fetch_text", lambda url: "http://h:1\n") monkeypatch.setattr( - refresh.free, "fetch_candidates", lambda text: [("http", "h", 1)] + refresh.proxy, "fetch_candidates", lambda text: [("http", "h", 1)] ) monkeypatch.setattr( refresh.verify, @@ -73,6 +74,33 @@ def test_fetch_profile_nodes_proxy(monkeypatch: pytest.MonkeyPatch) -> None: assert result and result[0].protocol.value == "http" +def test_fetch_profile_nodes_no_callback_logs_stage( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Scheduler-style call (no on_stage) wires a file logger to stages.""" + prof = Profile(name="relay", kind=ProfileKind.RELAY, url="https://x.test/sub") + seen_stages: list[str] = [] + + def fake_verify(*a: object, **k: object) -> list[object]: + on_stage = k.get("on_stage") + if on_stage is not None: + on_stage("probe", 2, 3) # type: ignore[operator] + return [] + + monkeypatch.setattr( + refresh.nodes, "fetch_text", lambda url: "vless://a@1.1.1.1:80\n" + ) + monkeypatch.setattr( + refresh.vless, "extract_from_text", lambda t: ["vless://a@1.1.1.1:80"] + ) + monkeypatch.setattr(refresh.verify, "verify_vless_pool", fake_verify) + monkeypatch.setattr( + refresh.events, "info", lambda msg, *a: seen_stages.append(msg % a) + ) + refresh.fetch_profile_nodes(prof, _cfg_for(prof)) + assert "[refresh] relay: probe 2/3" in seen_stages + + def test_fetch_profile_nodes_forwards_urltest_url( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -120,8 +148,6 @@ def test_commit_refresh_ignores_missing_profile() -> None: def test_run_scheduler_fetches_and_commits( monkeypatch: pytest.MonkeyPatch, ) -> None: - from openrot import config as cfg - prof = Profile( name="p", url="https://x.test/sub", diff --git a/tests/unit/core/test_singbox.py b/tests/unit/core/test_singbox.py index 1b77932..432318a 100644 --- a/tests/unit/core/test_singbox.py +++ b/tests/unit/core/test_singbox.py @@ -2,6 +2,7 @@ import pytest +import openrot.core.singbox as sb from openrot.core.singbox import ( generate_free_config, generate_singbox_config, @@ -128,8 +129,6 @@ def __exit__(self, *a: object) -> None: def test_probe_vless_waits_for_readiness_then_probes( monkeypatch: pytest.MonkeyPatch, ) -> None: - import openrot.core.singbox as sb - monkeypatch.setattr(sb, "_free_port", lambda: 12345) monkeypatch.setattr(sb, "write_config", lambda data, prefix="": _node_probe_conf()) monkeypatch.setattr(sb, "_wait_for_port", lambda h, p, t: True) @@ -145,8 +144,6 @@ def test_probe_vless_waits_for_readiness_then_probes( def test_probe_vless_logs_stderr_when_singbox_exits( monkeypatch: pytest.MonkeyPatch, ) -> None: - import openrot.core.singbox as sb - warnings: list[tuple[object, ...]] = [] class _FakeLogger: @@ -157,15 +154,7 @@ def warning(self, *args: object) -> None: monkeypatch.setattr(sb, "write_config", lambda data, prefix="": _node_probe_conf()) monkeypatch.setattr(sb, "_wait_for_port", lambda h, p, t: False) monkeypatch.setattr(sb.subprocess, "Popen", lambda *a, **k: _ExitedProc()) - monkeypatch.setattr( - sb, - "log", - type( - "_L", - (), - {"get_logger": staticmethod(lambda: _FakeLogger())}, - )(), - ) + monkeypatch.setattr(sb, "events", _FakeLogger()) alive, latency, egress_ip = probe_vless(_vless(), "sing-box", timeout=5.0) assert alive is False diff --git a/tests/unit/core/test_verify.py b/tests/unit/core/test_verify.py index 83e6a43..379ae8a 100644 --- a/tests/unit/core/test_verify.py +++ b/tests/unit/core/test_verify.py @@ -38,7 +38,7 @@ def test_verify_progress_reports_each_node(monkeypatch: pytest.MonkeyPatch) -> N lambda host, port, timeout: host == "1.1.1.1", ) monkeypatch.setattr( - verify.free, + verify.proxy, "probe_targets", lambda p, h, port, timeout, url=None: ([5.0], None), ) @@ -210,7 +210,7 @@ def test_verify_proxy_pool(monkeypatch: pytest.MonkeyPatch) -> None: verify, "tcp_reachable", lambda host, port, timeout: host == "1.1.1.1" ) monkeypatch.setattr( - verify.free, + verify.proxy, "probe_targets", lambda p, h, port, timeout, url=None: ([5.0], None), ) @@ -258,7 +258,7 @@ def fake_probe( seen["url"] = url return ([5.0], None) - monkeypatch.setattr(verify.free, "probe_targets", fake_probe) + monkeypatch.setattr(verify.proxy, "probe_targets", fake_probe) result = verify.verify_proxy_pool( [("http", "1.1.1.1", 80)], 3.0, diff --git a/tests/unit/providers/test_free.py b/tests/unit/providers/test_proxy.py similarity index 64% rename from tests/unit/providers/test_free.py rename to tests/unit/providers/test_proxy.py index 49a6cef..d49787f 100644 --- a/tests/unit/providers/test_free.py +++ b/tests/unit/providers/test_proxy.py @@ -2,8 +2,9 @@ import pytest +from openrot.config import Config from openrot.models import Node, NodeProtocol -from openrot.providers import free +from openrot.providers import proxy class _FakeClient: @@ -37,8 +38,8 @@ class _BadResponse: def test_parse_proxy() -> None: - assert free.parse_proxy("http://1.2.3.4:8080") == ("http", "1.2.3.4", 8080) - assert free.parse_proxy("socks5://host.example:1080") == ( + assert proxy.parse_proxy("http://1.2.3.4:8080") == ("http", "1.2.3.4", 8080) + assert proxy.parse_proxy("socks5://host.example:1080") == ( "socks5", "host.example", 1080, @@ -46,40 +47,40 @@ def test_parse_proxy() -> None: def test_parse_proxy_invalid() -> None: - assert free.parse_proxy("garbage") is None - assert free.parse_proxy("http://no-port") is None - assert free.parse_proxy("https://not-supported:443") is None + assert proxy.parse_proxy("garbage") is None + assert proxy.parse_proxy("http://no-port") is None + assert proxy.parse_proxy("https://not-supported:443") is None def test_check_proxy_alive(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr(free.httpx, "Client", lambda *a, **k: _FakeClient(_Response())) - alive, latency = free.check_proxy("http", "1.2.3.4", 8080) + monkeypatch.setattr(proxy.httpx, "Client", lambda *a, **k: _FakeClient(_Response())) + alive, latency = proxy.check_proxy("http", "1.2.3.4", 8080) assert alive is True assert isinstance(latency, float) def test_check_proxy_http_error(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( - free.httpx, + proxy.httpx, "Client", - lambda *a, **k: _FakeClient(error=free.httpx.HTTPError("boom")), + lambda *a, **k: _FakeClient(error=proxy.httpx.HTTPError("boom")), ) - alive, latency = free.check_proxy("http", "1.2.3.4", 8080) + alive, latency = proxy.check_proxy("http", "1.2.3.4", 8080) assert alive is False assert latency is None def test_fetch_candidates_dedupes() -> None: text = "http://1.1.1.1:8080\nhttp://1.1.1.1:8080\nsocks5://2.2.2.2:1080\njunk" - assert free.fetch_candidates(text) == [ + assert proxy.fetch_candidates(text) == [ ("http", "1.1.1.1", 8080), ("socks5", "2.2.2.2", 1080), ] def test_probe_targets_records_2xx(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr(free.httpx, "Client", lambda *a, **k: _FakeClient(_Response())) - latencies, egress_ip = free.probe_targets("http", "1.2.3.4", 8080) + monkeypatch.setattr(proxy.httpx, "Client", lambda *a, **k: _FakeClient(_Response())) + latencies, egress_ip = proxy.probe_targets("http", "1.2.3.4", 8080) assert len(latencies) == 1 assert isinstance(latencies[0], float) assert egress_ip is None @@ -103,8 +104,8 @@ def __enter__(self) -> Self: def __exit__(self, *a: object, **k: object) -> bool: return False - monkeypatch.setattr(free.httpx, "Client", lambda *a, **k: _RecordingClient()) - latencies, _ = free.probe_targets( + monkeypatch.setattr(proxy.httpx, "Client", lambda *a, **k: _RecordingClient()) + latencies, _ = proxy.probe_targets( "http", "1.2.3.4", 8080, url="https://alt.example/probe" ) assert len(latencies) == 1 @@ -113,25 +114,23 @@ def __exit__(self, *a: object, **k: object) -> bool: def test_probe_targets_rejects_non_2xx(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( - free.httpx, "Client", lambda *a, **k: _FakeClient(_BadResponse()) + proxy.httpx, "Client", lambda *a, **k: _FakeClient(_BadResponse()) ) - assert free.probe_targets("http", "1.2.3.4", 8080) == ([], None) + assert proxy.probe_targets("http", "1.2.3.4", 8080) == ([], None) def test_probe_targets_empty_on_error(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( - free.httpx, + proxy.httpx, "Client", - lambda *a, **k: _FakeClient(error=free.httpx.HTTPError("boom")), + lambda *a, **k: _FakeClient(error=proxy.httpx.HTTPError("boom")), ) - assert free.probe_targets("http", "1.2.3.4", 8080) == ([], None) + assert proxy.probe_targets("http", "1.2.3.4", 8080) == ([], None) -def test_check_node_uses_config_health_timeout( +def test_check_proxy_node_uses_config_health_timeout( monkeypatch: pytest.MonkeyPatch, ) -> None: - from openrot.config import Config - seen: dict[str, object] = {} def fake_check_proxy( @@ -140,17 +139,15 @@ def fake_check_proxy( seen["timeout"] = timeout return True, 1.0 - monkeypatch.setattr(free, "check_proxy", fake_check_proxy) + monkeypatch.setattr(proxy, "check_proxy", fake_check_proxy) node = Node(id="n", raw="http://1.2.3.4:8080", protocol=NodeProtocol.HTTP) cfg = Config(health_timeout=42) - alive, _ = free.check_node(node, cfg) + alive, _ = proxy.check_proxy_node(node, cfg) assert alive is True assert seen["timeout"] == 42 -def test_check_node_dead_when_unparseable() -> None: - from openrot.config import Config - +def test_check_proxy_node_dead_when_unparseable() -> None: node = Node(id="n", raw="http://no-port", protocol=NodeProtocol.HTTP) - assert free.check_node(node, Config()) == (False, None) + assert proxy.check_proxy_node(node, Config()) == (False, None) diff --git a/tests/unit/providers/test_vless.py b/tests/unit/providers/test_vless.py index c57faef..e72ddbb 100644 --- a/tests/unit/providers/test_vless.py +++ b/tests/unit/providers/test_vless.py @@ -1,3 +1,5 @@ +import base64 + import pytest from openrot.providers import vless @@ -57,8 +59,6 @@ def test_extract_from_text() -> None: def test_extract_from_base64_subscription() -> None: - import base64 - plain = "vless://a-b@1.1.1.1:80#Base" encoded = base64.b64encode(plain.encode()).decode() assert vless.extract_from_text(encoded) == [plain] diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 96f1a92..1c5c64a 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,4 +1,6 @@ import logging +import logging.handlers +import sys from pathlib import Path import pytest @@ -7,23 +9,103 @@ from openrot import log -def test_get_logger_rotating_handler_0600( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch +class _NonTty: + def write(self, s: str) -> None: + pass + + def flush(self) -> None: + pass + + def isatty(self) -> bool: + return False + + +class _Tty: + def __init__(self) -> None: + self.writes: list[str] = [] + + def write(self, s: str) -> None: + self.writes.append(s) + + def flush(self) -> None: + pass + + def isatty(self) -> bool: + return True + + +def _fresh_logger( + log_path: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - log_path = tmp_path / "openrot.log" - unique = f"openrot.events.{id(log_path)}" + unique = f"openrot.events.{id(tmp_path)}" monkeypatch.setattr(cfg, "LOG_PATH", log_path) monkeypatch.setattr(log, "LOGGER_NAME", unique) + logger = logging.getLogger(unique) + for h in list(logger.handlers): + logger.removeHandler(h) + logger.handlers.clear() + logger.setLevel(logging.NOTSET) log_path.parent.mkdir(parents=True, exist_ok=True) + +def test_get_logger_rotating_handler_0600( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + log_path = tmp_path / "openrot.log" + _fresh_logger(log_path, tmp_path, monkeypatch) + logger = log.get_logger() - assert logger.name == unique - assert len(logger.handlers) == 1 - assert isinstance(logger.handlers[0], logging.handlers.RotatingFileHandler) + assert logger.name == log.LOGGER_NAME + file_handler = logger.handlers[0] + assert isinstance(file_handler, logging.handlers.RotatingFileHandler) + assert file_handler.level == logging.INFO assert logger.propagate is False logger.info("some event") - handler = logger.handlers[0] - handler.flush() + file_handler.flush() assert log_path.exists() assert log_path.stat().st_mode & 0o777 == 0o600 + + +def test_console_handler_added_only_when_tty( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + log_path = tmp_path / "openrot.log" + _fresh_logger(log_path, tmp_path, monkeypatch) + monkeypatch.setattr(sys, "stdout", _NonTty()) + assert len(log.get_logger().handlers) == 1 + + log_path_tty = tmp_path / "tty.log" + _fresh_logger(log_path_tty, tmp_path, monkeypatch) + monkeypatch.setattr(sys, "stdout", _Tty()) + logger = log.get_logger() + assert len(logger.handlers) == 2 + console_handler = logger.handlers[1] + assert isinstance(console_handler, logging.StreamHandler) + assert console_handler.level == logging.INFO + + +def test_debug_only_to_file_when_env_set( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """OPENROT_DEBUG_REQ=1 => DEBUG accepted by logger and file, never console.""" + log_path = tmp_path / "openrot.log" + _fresh_logger(log_path, tmp_path, monkeypatch) + tty = _Tty() + monkeypatch.setattr(sys, "stdout", tty) + monkeypatch.setenv("OPENROT_DEBUG_REQ", "1") + logger = log.get_logger() + assert logger.level == logging.DEBUG + file_handler = logger.handlers[0] + assert file_handler.level == logging.DEBUG + console_handler = logger.handlers[1] + assert console_handler.level == logging.INFO + + logger.debug("[bridge][debug] body: stream=True") + logger.info("regular info") + file_handler.flush() + content = log_path.read_text() + assert "regular info" in content + assert "stream=True" in content # DEBUG truly reached the file + assert not any("stream=True" in w for w in tty.writes) + assert any("regular info" in w for w in tty.writes) diff --git a/tests/unit/test_self_update.py b/tests/unit/test_self_update.py index c56501c..174cd0a 100644 --- a/tests/unit/test_self_update.py +++ b/tests/unit/test_self_update.py @@ -93,7 +93,7 @@ def test_update_available(self) -> None: resp.json.return_value = {"tag_name": "v99.0.0"} client.get.return_value = resp result = su.check_for_update(client) - assert result.updated is False + assert result.updated is True assert "update available" in result.message assert result.latest == "99.0.0" From bf205d0f096b0308509d0d02b54a8139115e1f93 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Sat, 12 Sep 2026 22:31:58 +0300 Subject: [PATCH 16/22] feat(bridge): :sparkles: add start pacing, session ids and port guards - Add global min-gap pacing between upstream request starts (bridge_min_interval) - Inject fresh X-Opencode-Session / X-Opencode-Request ids per forwarded request - Guard bridge and cascade startup against busy ports via port_in_use - Make events logger file-only: drop the console handler - Drop dynamic kwargs dict in make_client --- README.md | 23 ++++++ openrot/config.py | 31 ++++++++ openrot/core/bridge.py | 55 +++++++++++++- openrot/core/cascade.py | 14 ++++ openrot/core/http.py | 10 +-- openrot/core/singbox.py | 7 ++ openrot/log.py | 15 +--- openrot/models/config.py | 2 + tests/unit/core/test_bridge.py | 125 +++++++++++++++++++++++++++++++- tests/unit/core/test_cascade.py | 17 +++++ tests/unit/test_config.py | 42 +++++++++++ tests/unit/test_log.py | 58 +-------------- 12 files changed, 321 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index d18caae..916c7dd 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,29 @@ The upstream endpoint and port are configurable — see the config fields `https://opencode.ai/zen/v1`, the endpoint opencode's built-in provider uses natively), or the env overrides `OPENROT_BRIDGE_PORT` and `OPENROT_UPSTREAM`. +### Free-tier session headers + +opencode's free tier keys on the `X-Opencode-Session` header: a bare request +that lacks it lands in the anonymous quota and can be rejected with +`FreeUsageLimitError`. The bridge injects `X-Opencode-Session` (`ses_`+22 +alnum) and `X-Opencode-Request` (`msg_`+24 alnum) on every upstream request +that doesn't already carry them, so scripts and `curl` hitting the bridge +share the free bucket instead of being rate-limited. Values sent by the client +are respected (never overwritten). Toggle this off with +`bridge_inject_session: false` in config or +`OPENROT_BRIDGE_INJECT_SESSION=0`: + +```bash +# via the bridge: headers are added automatically +curl -s http://127.0.0.1:7891/v1/chat/completions \ + -H 'Content-Type: application/json' -d '{"model":"opencode/sonnet","messages":[{"role":"user","content":"hi"}]}' + +# hitting the upstream directly: add the header yourself +curl -s https://opencode.ai/zen/v1/chat/completions \ + -H 'X-Opencode-Session: ses_<22 alnum>' \ + -H 'Content-Type: application/json' -d '{"model":"opencode/sonnet","messages":[{"role":"user","content":"hi"}]}' +``` + ## Free sources (profiles) Everything you add is a **profile**: a URL that returns a list of nodes. diff --git a/openrot/config.py b/openrot/config.py index b55c155..771fade 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -121,6 +121,17 @@ def _env_int(name: str, attr: str, cfg: Config) -> None: raise ConfigError(f"bad {name} {raw!r}: {exc}") from exc +def _env_float(name: str, attr: str, cfg: Config) -> None: + """Apply a float-valued env override, raising ``ConfigError`` on bad input.""" + raw = os.environ.get(name) + if not raw: + return + try: + setattr(cfg, attr, float(raw)) + except (ValueError, ValidationError) as exc: + raise ConfigError(f"bad {name} {raw!r}: {exc}") from exc + + def _env_int_list(name: str, attr: str, cfg: Config) -> None: """Apply a comma-separated int-list env override, raising on bad input.""" raw = os.environ.get(name) @@ -133,6 +144,24 @@ def _env_int_list(name: str, attr: str, cfg: Config) -> None: raise ConfigError(f"bad {name} {raw!r}: {exc}") from exc +def _env_bool(name: str, attr: str, cfg: Config) -> None: + """Apply a boolean env override, raising ``ConfigError`` on bad input.""" + raw = os.environ.get(name) + if not raw: + return + normalized = raw.strip().lower() + if normalized in {"1", "true", "yes", "on"}: + value = True + elif normalized in {"0", "false", "no", "off"}: + value = False + else: + raise ConfigError(f"bad {name} {raw!r}: expected a boolean") + try: + setattr(cfg, attr, value) + except ValidationError as exc: + raise ConfigError(f"bad {name} {raw!r}: {exc}") from exc + + def _apply_env_overrides(cfg: Config) -> Config: _env_int("OPENROT_PORT", "port", cfg) singbox_bin = os.environ.get("OPENROT_SINGBOX_BIN") @@ -145,6 +174,8 @@ def _apply_env_overrides(cfg: Config) -> Config: _env_int_list("OPENROT_BRIDGE_RETRY_STATUSES", "bridge_retry_statuses", cfg) _env_int("OPENROT_BRIDGE_RETRY_ATTEMPTS", "bridge_retry_attempts", cfg) _env_int("OPENROT_BRIDGE_MAX_CONCURRENT", "bridge_max_concurrent", cfg) + _env_float("OPENROT_BRIDGE_MIN_INTERVAL", "bridge_min_interval", cfg) + _env_bool("OPENROT_BRIDGE_INJECT_SESSION", "bridge_inject_session", cfg) _env_int("OPENROT_MAX_WORKERS", "max_workers", cfg) return cfg diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index e650e6a..4d8c30d 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -20,7 +20,9 @@ import contextlib import json import os +import secrets import socket +import string import sys import threading import time @@ -36,6 +38,7 @@ from openrot.config import ActiveLevel, Config from openrot.core import cascade, daemon from openrot.core.http import make_client +from openrot.core.singbox import port_in_use from openrot.log import get_logger as _get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM @@ -143,12 +146,33 @@ def _client(cfg_obj: Config) -> httpx.Client: return make_client(proxy=proxy_url, timeout=300) -def _forward_headers(headers: dict[str, str], host: str) -> dict[str, str]: +_ALNUM = string.ascii_letters + string.digits + + +def _random_id(prefix: str, length: int) -> str: + """Return a random alnum identifier (``prefix`` + ``length`` chars). + + opencode's free tier keys on ``X-Opencode-Session`` (``ses_``+22 alnum); the + request id ``X-Opencode-Request`` is ``msg_``+24 alnum. The server does not + validate uniqueness or signature, so a fresh random id per request is enough. + """ + return prefix + "".join(secrets.choice(_ALNUM) for _ in range(length)) + + +def _forward_headers( + headers: dict[str, str], host: str, inject_session: bool = True +) -> dict[str, str]: out: dict[str, str] = {} for key, value in headers.items(): if key.lower() in _REQUEST_STRIP: continue out[key] = value + if inject_session: + known = {k.lower(): k for k in out} + if "x-opencode-session" not in known: + out["X-Opencode-Session"] = _random_id("ses_", 22) + if "x-opencode-request" not in known: + out["X-Opencode-Request"] = _random_id("msg_", 24) if "content-type" not in {k.lower() for k in out}: out["Content-Type"] = "application/json" out["Host"] = host @@ -162,7 +186,9 @@ def _fetch( ) -> httpx.Response: url = upstream_url(cfg_obj, request.path) host_header = httpx.URL(url).host # type: ignore[attr-defined] - out_headers = _forward_headers(request.headers, host_header or "") + out_headers = _forward_headers( + request.headers, host_header or "", inject_session=cfg_obj.bridge_inject_session + ) # Ask for identity so httpx neither injects its own Accept-Encoding nor # transparently decompresses: the body is then passed through verbatim. out_headers["Accept-Encoding"] = "identity" @@ -235,6 +261,8 @@ def _reset_client() -> None: _request_semaphore: threading.Semaphore | None = None _semaphore_lock = threading.Lock() +_last_start_monotonic: float = 0.0 +_pacing_lock = threading.Lock() def _get_semaphore(cfg_obj: Config) -> threading.Semaphore: @@ -249,6 +277,20 @@ def _get_semaphore(cfg_obj: Config) -> threading.Semaphore: return _request_semaphore +def _pace_start(cfg_obj: Config) -> None: + """Sleep until the next allowed upstream start, enforcing a global min gap. + + Pacing is global (across all request starts), not per-slot. + """ + global _last_start_monotonic + next_start = _last_start_monotonic + cfg_obj.bridge_min_interval + sleep = max(0.0, next_start - time.monotonic()) + if sleep > 0: + time.sleep(sleep) + with _pacing_lock: + _last_start_monotonic = time.monotonic() + + # --------------------------------------------------------------------------- # Rotation logging — shared between forward() and BridgeHandler. # --------------------------------------------------------------------------- @@ -410,6 +452,13 @@ def serve() -> None: through the cascade with 429 self-rotation. Ctrl-C stops it. """ cfg_obj = cfg.load_config() + host = cfg.listen_address() + if port_in_use(host, cfg_obj.bridge_port): + console.print( + f"[red]bridge port {host}:{cfg_obj.bridge_port} is already in use; " + "not starting[/red]" + ) + raise SystemExit(1) if not ( cfg_obj.active_level != ActiveLevel.NONE and cascade.level_serving(cfg_obj) ): @@ -522,11 +571,13 @@ def _handle(self, method: str) -> None: t0 = time.monotonic() semaphore.acquire() try: + _pace_start(cfg_obj) resp, _client = forward(cfg_obj, request) except httpx.HTTPError as exc: events.warning("[bridge] upstream error, retrying: %s", exc) try: cfg_obj = cfg.load_config() + _pace_start(cfg_obj) resp, _client = forward(cfg_obj, request, rotate_on_429=False) except httpx.HTTPError as retry_exc: self._elapsed_ms = (time.monotonic() - t0) * 1000 diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 8820e0b..27c1389 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -12,6 +12,7 @@ from openrot.core import daemon, health, nodes, proxy, refresh, rotator from openrot.core.constants import IPIFY_URL from openrot.core.http import make_client +from openrot.core.singbox import port_in_use from openrot.providers import warp console = Console() @@ -64,6 +65,12 @@ def start(foreground: bool, daemon: bool) -> None: ) return cfg_obj = cfg.load_config() + if foreground and port_in_use(cfg.listen_address(), cfg_obj.port): + console.print( + f"[red]port {cfg_obj.port} is already in use " + f"({cfg.listen_address()}); not starting[/red]" + ) + raise SystemExit(1) if cfg_obj.update_interval > 0 and foreground: threading.Thread( target=refresh.run_scheduler, name="openrot-scheduler", daemon=True @@ -121,6 +128,10 @@ def start_warp(foreground: bool) -> bool: ip or "-", ) if foreground: + console.print( + f"proxy: 127.0.0.1:{cfg_obj.port} (pid {pid}), " + f"warp: {ip or '-'}. Ctrl-C to stop." + ) warp_health_loop() return True @@ -261,8 +272,10 @@ def _rotate_inner(first: bool) -> None: if warp.rotate(): ip = warp.current_ip() events.info("[cascade] warp rotated, ip=%s", ip or "-") + console.print(f"warp rotated, ip {ip or '-'}") else: events.warning("[cascade] warp rotation failed") + console.print("[red]warp rotation failed[/red]") return current = nodes.current_node(cfg_obj) @@ -303,6 +316,7 @@ def _rotate_inner(first: bool) -> None: events.warning("[cascade] %s", exc) raise SystemExit(1) from exc events.info("[cascade] rotated to node %s (pid %d)", nodes.node_label(node), pid) + console.print(f"rotated to node {nodes.node_label(node)} (pid {pid})") def stop() -> None: diff --git a/openrot/core/http.py b/openrot/core/http.py index d6e3193..7b793c1 100644 --- a/openrot/core/http.py +++ b/openrot/core/http.py @@ -14,13 +14,11 @@ def make_client( follow_redirects: bool = False, ) -> httpx.Client: """Build an ``httpx.Client`` with the common defaults.""" - kwargs: dict[str, object] = { - "timeout": timeout, - "follow_redirects": follow_redirects, - } if proxy: - kwargs["proxy"] = proxy - return httpx.Client(**kwargs) + return httpx.Client( + proxy=proxy, timeout=timeout, follow_redirects=follow_redirects + ) + return httpx.Client(timeout=timeout, follow_redirects=follow_redirects) def get_egress_ip(client: httpx.Client) -> str | None: diff --git a/openrot/core/singbox.py b/openrot/core/singbox.py index 9352974..5e77b46 100644 --- a/openrot/core/singbox.py +++ b/openrot/core/singbox.py @@ -182,6 +182,13 @@ def _free_port() -> int: return s.getsockname()[1] +def port_in_use(host: str, port: int) -> bool: + """Return True when something is already listening on `host:port`.""" + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(0.3) + return s.connect_ex((host, port)) == 0 + + def _wait_for_port(host: str, port: int, timeout: float, step: float = 0.05) -> bool: """Poll until `host:port` accepts connections or `timeout` elapses.""" deadline = time.monotonic() + timeout diff --git a/openrot/log.py b/openrot/log.py index 81f85e1..79c9655 100644 --- a/openrot/log.py +++ b/openrot/log.py @@ -1,13 +1,11 @@ import logging import logging.handlers import os -import sys from openrot import config as cfg LOGGER_NAME = "openrot.events" _LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" -_CONSOLE_FORMAT = "%(message)s" _MAX_BYTES = 1_000_000 _BACKUP_COUNT = 3 _DEBUG_ENV = "OPENROT_DEBUG_REQ" @@ -16,11 +14,9 @@ def get_logger() -> logging.Logger: """Return the events logger, configured once to append to a rotating file. - A console handler (INFO, ``%(message)s``-only) is attached only when stdout - is a terminal, so internal events (rotations, starts, bridge requests) are - visible during interactive use but stay file-only in daemon/scripted runs. - ``OPENROT_DEBUG_REQ=1`` raises the *file* handler to DEBUG (request-level - dumps); the console handler never logs DEBUG. + Events are file-only: terminal output is done explicitly via ``console.print`` + on the CLI path. ``OPENROT_DEBUG_REQ=1`` raises the file level to DEBUG + (request-level dumps). """ logger = logging.getLogger(LOGGER_NAME) if logger.handlers: @@ -34,11 +30,6 @@ def get_logger() -> logging.Logger: file_handler.setLevel(file_level) file_handler.setFormatter(logging.Formatter(_LOG_FORMAT)) logger.addHandler(file_handler) - if sys.stdout.isatty(): - console_handler = logging.StreamHandler(sys.stdout) - console_handler.setLevel(logging.INFO) - console_handler.setFormatter(logging.Formatter(_CONSOLE_FORMAT)) - logger.addHandler(console_handler) logger.propagate = False cfg.LOG_PATH.chmod(0o600) return logger diff --git a/openrot/models/config.py b/openrot/models/config.py index a7b625f..0129d37 100644 --- a/openrot/models/config.py +++ b/openrot/models/config.py @@ -36,6 +36,8 @@ class Config(BaseModel): bridge_retry_statuses: list[int] = Field(default_factory=lambda: [429]) bridge_retry_attempts: int = Field(default=1, ge=0, le=10) bridge_max_concurrent: int = Field(default=3, ge=1, le=20) + bridge_min_interval: float = Field(default=0.5, ge=0.0) + bridge_inject_session: bool = True deduplicate_by_ip: bool = True def all_nodes(self) -> list[Node]: diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 4ecd0b5..8e89228 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -79,6 +79,48 @@ def test_forward_headers_strips_hop_by_hop_and_sets_host() -> None: assert out["Authorization"] == "Bearer k" +def _alnum_len(value: str, prefix: str) -> int: + assert value.startswith(prefix) + return len(value) - len(prefix) + + +def test_forward_headers_injects_session_and_request_when_absent() -> None: + out = bridge._forward_headers({}, "up.test") + assert _alnum_len(out["X-Opencode-Session"], "ses_") == 22 + assert _alnum_len(out["X-Opencode-Request"], "msg_") == 24 + assert out["X-Opencode-Session"][4:].isalnum() + assert out["X-Opencode-Request"][4:].isalnum() + + +def test_forward_headers_respects_existing_session_and_request() -> None: + headers = { + "X-Opencode-Session": "ses_customvalue", + "X-Opencode-Request": "msg_customvalue", + } + out = bridge._forward_headers(headers, "up.test") + assert out["X-Opencode-Session"] == "ses_customvalue" + assert out["X-Opencode-Request"] == "msg_customvalue" + + +def test_forward_headers_respects_existing_session_only() -> None: + out = bridge._forward_headers({"X-Opencode-Session": "ses_custom"}, "up.test") + assert out["X-Opencode-Session"] == "ses_custom" + assert _alnum_len(out["X-Opencode-Request"], "msg_") == 24 + + +def test_forward_headers_no_injection_when_disabled() -> None: + out = bridge._forward_headers({}, "up.test", inject_session=False) + assert "X-Opencode-Session" not in out + assert "X-Opencode-Request" not in out + + +def test_random_id_format_and_uniqueness() -> None: + a = bridge._random_id("ses_", 22) + b = bridge._random_id("ses_", 22) + assert _alnum_len(a, "ses_") == 22 + assert a != b + + class _FakeClient: def __init__(self, responses: list[httpx.Response]) -> None: self._responses = list(responses) @@ -426,6 +468,7 @@ def server_close(self) -> None: "load_config", lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) monkeypatch.setattr(bridge, "Bridge", FakeServer) bridge.serve() assert started == [(False, False)] @@ -451,11 +494,30 @@ def server_close(self) -> None: "load_config", lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) monkeypatch.setattr(bridge, "Bridge", FakeServer) bridge.serve() assert started == [] +def test_serve_exits_when_port_busy(monkeypatch: pytest.MonkeyPatch) -> None: + started: list = [] + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "start", lambda *a: started.append(a)) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: True) + + with pytest.raises(SystemExit) as exc: + bridge.serve() + + assert exc.value.code == 1 + assert started == [] + + def test_daemonize_delegates_to_daemon_start( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: @@ -532,6 +594,7 @@ def server_close(self) -> None: "load_config", lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) monkeypatch.setattr(bridge.cfg, "listen_address", lambda: "0.0.0.0") monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -569,6 +632,7 @@ def server_close(self) -> None: "load_config", lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(False)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -603,6 +667,7 @@ def server_close(self) -> None: "load_config", lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) monkeypatch.setattr(bridge, "_log", lambda msg: printed.append(msg)) monkeypatch.setattr(bridge.sys, "stdout", _FakeStdout(True)) monkeypatch.setattr(bridge, "Bridge", FakeServer) @@ -727,7 +792,7 @@ def test_handler_returns_502_on_upstream_error( handler.send_header = lambda k, v: headers.append((k, v)) handler.end_headers = lambda: None - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_min_interval=0)) def boom(cfg_obj: object, request: object, **kwargs: object) -> object: raise httpx.HTTPError("up boom") @@ -752,7 +817,7 @@ def test_handler_logs_request_elapsed_and_model( handler.end_headers = lambda: None logged: list[str] = [] - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_min_interval=0)) monkeypatch.setattr(bridge, "_respond", lambda handler, resp: None) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) @@ -818,7 +883,7 @@ def test_handler_logs_status_and_request_id(monkeypatch: pytest.MonkeyPatch) -> handler.end_headers = lambda: None logged: list[str] = [] - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_min_interval=0)) monkeypatch.setattr(bridge, "_respond", lambda handler, resp: 1500) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) @@ -851,7 +916,7 @@ def test_handler_retries_without_rotation_after_upstream_error( logged: list[str] = [] warning_called = {"n": 0} - monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg()) + monkeypatch.setattr(bridge.cfg, "load_config", lambda: _cfg(bridge_min_interval=0)) monkeypatch.setattr(bridge, "_log", lambda msg: logged.append(msg)) monkeypatch.setattr( bridge.events, "warning", lambda msg, *a: warning_called.__setitem__("n", 1) @@ -877,6 +942,58 @@ def flaky_forward(cfg_obj: object, request: object, **kw: object) -> object: assert not any("rotation" in msg for msg in logged) +def test_pace_start_first_call_does_not_sleep( + monkeypatch: pytest.MonkeyPatch, +) -> None: + bridge._last_start_monotonic = 0.0 + sleeps: list[float] = [] + monkeypatch.setattr(bridge.time, "sleep", sleeps.append) + monkeypatch.setattr(bridge.time, "monotonic", lambda: 100.0) + bridge._pace_start(_cfg(bridge_min_interval=0.5)) + assert sleeps == [] + assert bridge._last_start_monotonic == 100.0 + + +def test_pace_start_waits_remaining_interval( + monkeypatch: pytest.MonkeyPatch, +) -> None: + bridge._last_start_monotonic = 0.0 + sleeps: list[float] = [] + monkeypatch.setattr(bridge.time, "sleep", sleeps.append) + monkeypatch.setattr(bridge.time, "monotonic", lambda: 0.1) + bridge._pace_start(_cfg(bridge_min_interval=0.5)) + assert sleeps == [pytest.approx(0.4)] + assert bridge._last_start_monotonic == 0.1 + + +def test_pace_start_zero_interval_is_noop( + monkeypatch: pytest.MonkeyPatch, +) -> None: + bridge._last_start_monotonic = 100.0 + sleeps: list[float] = [] + monkeypatch.setattr(bridge.time, "sleep", sleeps.append) + monkeypatch.setattr(bridge.time, "monotonic", lambda: 100.0) + bridge._pace_start(_cfg(bridge_min_interval=0)) + assert sleeps == [] + + +def test_pace_start_enforces_gap_between_consecutive_starts( + monkeypatch: pytest.MonkeyPatch, +) -> None: + bridge._last_start_monotonic = 0.0 + sleeps: list[float] = [] + now = {"t": 100.0} + monkeypatch.setattr(bridge.time, "sleep", sleeps.append) + monkeypatch.setattr(bridge.time, "monotonic", lambda: now["t"]) + bridge._pace_start(_cfg(bridge_min_interval=0.5)) + assert sleeps == [] + assert bridge._last_start_monotonic == 100.0 + now["t"] = 100.2 + bridge._pace_start(_cfg(bridge_min_interval=0.5)) + assert sleeps == [pytest.approx(0.3)] + assert bridge._last_start_monotonic == 100.2 + + def test_bridge_server_binds_and_closes() -> None: server = bridge.Bridge("127.0.0.1", 0) try: diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index 4ad1f50..db7b54b 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -36,6 +36,7 @@ def fake_start_warp(foreground: bool) -> bool: monkeypatch.setattr(cascade.signals, "keyboard_on_sigterm", lambda: None) monkeypatch.setattr(cascade.proxy, "stop_proxy", lambda: calls.append("proxy")) monkeypatch.setattr(cascade.warp, "disconnect", lambda: calls.append("warp")) + monkeypatch.setattr(cascade, "port_in_use", lambda h, p: False) cfg_obj = Config(update_interval=0, active_level=ActiveLevel.NODE) monkeypatch.setattr(cascade.cfg, "load_config", lambda: cfg_obj) monkeypatch.setattr(cascade.cfg, "save_config", lambda c: None) @@ -80,6 +81,7 @@ def fake_start_warp(foreground: bool) -> bool: monkeypatch.setattr(cascade.signals, "keyboard_on_sigterm", lambda: None) monkeypatch.setattr(cascade.proxy, "stop_proxy", lambda: None) monkeypatch.setattr(cascade.warp, "disconnect", lambda: None) + monkeypatch.setattr(cascade, "port_in_use", lambda h, p: False) monkeypatch.setattr(cascade.cfg, "load_config", lambda: conf) monkeypatch.setattr( cascade.cfg, "save_config", lambda c, path=None: shutdown_calls.append(c) @@ -110,6 +112,20 @@ def fake_daemonize() -> None: assert called["n"] == 1 +def test_start_foreground_exits_when_port_busy( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(cascade.proxy, "load_pid", lambda: None) + monkeypatch.setattr(cascade.cfg, "load_config", lambda: Config(update_interval=0)) + monkeypatch.setattr(cascade, "port_in_use", lambda h, p: True) + monkeypatch.setattr(cascade, "start_warp", lambda f: True) + + with pytest.raises(SystemExit) as exc: + cascade.start(True, False) + + assert exc.value.code == 1 + + def test_start_exits_when_proxy_already_running( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -331,6 +347,7 @@ def fake_start_node(foreground: bool) -> None: monkeypatch.setattr(cascade, "start_warp", fake_start_warp) monkeypatch.setattr(cascade, "start_node", fake_start_node) monkeypatch.setattr(cascade.cfg, "load_config", lambda: Config(update_interval=0)) + monkeypatch.setattr(cascade, "port_in_use", lambda h, p: False) monkeypatch.setattr(cascade.threading, "Thread", lambda **kw: None) cascade.start(True, False) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index c0018d1..68fdf38 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -142,12 +142,54 @@ def test_env_bad_retry_attempts_raises_config_error( cfg.load_config(path) +def test_env_overrides_bridge_min_interval( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OPENROT_BRIDGE_MIN_INTERVAL", "1.5") + path = tmp_path / "config.yaml" + cfg.save_config(cfg.Config(), path) + loaded = cfg.load_config(path) + assert loaded.bridge_min_interval == 1.5 + + +def test_env_bad_bridge_min_interval_raises_config_error( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OPENROT_BRIDGE_MIN_INTERVAL", "not-a-number") + path = tmp_path / "config.yaml" + cfg.save_config(cfg.Config(), path) + with pytest.raises(cfg.ConfigError): + cfg.load_config(path) + + +def test_env_bridge_inject_session_override( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OPENROT_BRIDGE_INJECT_SESSION", "false") + path = tmp_path / "config.yaml" + cfg.save_config(cfg.Config(), path) + assert cfg.load_config(path).bridge_inject_session is False + + +def test_env_bad_bridge_inject_session_raises_config_error( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OPENROT_BRIDGE_INJECT_SESSION", "not-a-bool") + path = tmp_path / "config.yaml" + cfg.save_config(cfg.Config(), path) + with pytest.raises(cfg.ConfigError): + cfg.load_config(path) + + def test_config_bridge_and_pool_defaults() -> None: c = Config() assert c.bridge_port == 7891 assert c.bridge_upstream == "https://opencode.ai/zen" assert c.bridge_retry_statuses == [429] assert c.bridge_retry_attempts == 1 + assert c.bridge_max_concurrent == 3 + assert c.bridge_min_interval == 0.5 + assert c.bridge_inject_session is True assert c.max_workers == 50 diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 1c5c64a..47920ba 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,6 +1,5 @@ import logging import logging.handlers -import sys from pathlib import Path import pytest @@ -9,31 +8,6 @@ from openrot import log -class _NonTty: - def write(self, s: str) -> None: - pass - - def flush(self) -> None: - pass - - def isatty(self) -> bool: - return False - - -class _Tty: - def __init__(self) -> None: - self.writes: list[str] = [] - - def write(self, s: str) -> None: - self.writes.append(s) - - def flush(self) -> None: - pass - - def isatty(self) -> bool: - return True - - def _fresh_logger( log_path: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -56,6 +30,7 @@ def test_get_logger_rotating_handler_0600( logger = log.get_logger() assert logger.name == log.LOGGER_NAME + assert len(logger.handlers) == 1 file_handler = logger.handlers[0] assert isinstance(file_handler, logging.handlers.RotatingFileHandler) assert file_handler.level == logging.INFO @@ -67,45 +42,20 @@ def test_get_logger_rotating_handler_0600( assert log_path.stat().st_mode & 0o777 == 0o600 -def test_console_handler_added_only_when_tty( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - log_path = tmp_path / "openrot.log" - _fresh_logger(log_path, tmp_path, monkeypatch) - monkeypatch.setattr(sys, "stdout", _NonTty()) - assert len(log.get_logger().handlers) == 1 - - log_path_tty = tmp_path / "tty.log" - _fresh_logger(log_path_tty, tmp_path, monkeypatch) - monkeypatch.setattr(sys, "stdout", _Tty()) - logger = log.get_logger() - assert len(logger.handlers) == 2 - console_handler = logger.handlers[1] - assert isinstance(console_handler, logging.StreamHandler) - assert console_handler.level == logging.INFO - - -def test_debug_only_to_file_when_env_set( +def test_debug_only_reaches_file_when_env_set( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - """OPENROT_DEBUG_REQ=1 => DEBUG accepted by logger and file, never console.""" + """OPENROT_DEBUG_REQ=1 => DEBUG accepted by logger and file.""" log_path = tmp_path / "openrot.log" _fresh_logger(log_path, tmp_path, monkeypatch) - tty = _Tty() - monkeypatch.setattr(sys, "stdout", tty) monkeypatch.setenv("OPENROT_DEBUG_REQ", "1") logger = log.get_logger() assert logger.level == logging.DEBUG + assert len(logger.handlers) == 1 file_handler = logger.handlers[0] assert file_handler.level == logging.DEBUG - console_handler = logger.handlers[1] - assert console_handler.level == logging.INFO logger.debug("[bridge][debug] body: stream=True") - logger.info("regular info") file_handler.flush() content = log_path.read_text() - assert "regular info" in content assert "stream=True" in content # DEBUG truly reached the file - assert not any("stream=True" in w for w in tty.writes) - assert any("regular info" in w for w in tty.writes) From a4afd4f4f2af176aa3a795fb3668f044f7e84f90 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Tue, 15 Sep 2026 17:04:33 +0300 Subject: [PATCH 17/22] feat(bridge): :sparkles: gateway-compatible session ids, stub tools, and daemon cascade loops --- openrot/core/bridge.py | 92 +++++++++++++++++++++++++--- openrot/core/cascade.py | 33 +++++++++- tests/unit/core/test_bridge.py | 103 +++++++++++++++++++++++++++++--- tests/unit/core/test_cascade.py | 84 +++++++++++++++++++++++++- 4 files changed, 293 insertions(+), 19 deletions(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 4d8c30d..70b4001 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -108,7 +108,9 @@ def _debug_request( _HOP_BY_HOP = {"connection", "host", "proxy-connection"} # Request leg: transport framing is re-created from the raw body by httpx. -_REQUEST_STRIP = _HOP_BY_HOP | {"transfer-encoding", "content-length"} +# ``x-opencode-client`` is an opencode client marker the gateway does not +# inspect, so it is dropped rather than forwarded. +_REQUEST_STRIP = _HOP_BY_HOP | {"transfer-encoding", "content-length", "x-opencode-client"} # Response leg: framing is re-created below (content-length or chunked). _RESPONSE_STRIP = _HOP_BY_HOP | {"transfer-encoding"} @@ -148,31 +150,100 @@ def _client(cfg_obj: Config) -> httpx.Client: _ALNUM = string.ascii_letters + string.digits +# User-Agent the free tier gateway expects; injected when the client sends +# none or a non-opencode one (curl, python-requests, ...). +_OPENCODE_UA = "opencode/1.18.30 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14" + +# Stub tools injected into bodies that carry no (or too few) tools: the free +# tier gateway requires a ``tools`` array with at least two elements and +# validates the tool names against real opencode tools (bash, read, ...). +_STUB_TOOLS = [ + { + "type": "function", + "function": { + "name": "bash", + "description": "run", + "parameters": { + "type": "object", + "properties": {"c": {"type": "string"}}, + }, + }, + }, + { + "type": "function", + "function": { + "name": "read", + "description": "read", + "parameters": { + "type": "object", + "properties": {"p": {"type": "string"}}, + }, + }, + }, +] + def _random_id(prefix: str, length: int) -> str: """Return a random alnum identifier (``prefix`` + ``length`` chars). - opencode's free tier keys on ``X-Opencode-Session`` (``ses_``+22 alnum); the - request id ``X-Opencode-Request`` is ``msg_``+24 alnum. The server does not - validate uniqueness or signature, so a fresh random id per request is enough. + The request id ``X-Opencode-Request`` is ``msg_``+24 alnum. The server does + not validate uniqueness or signature, so a fresh random id per request is + enough. """ return prefix + "".join(secrets.choice(_ALNUM) for _ in range(length)) +def _random_session_id() -> str: + """Return a session id the free tier gateway accepts. + + The gateway validates ``X-Opencode-Session`` by format only: ``ses_`` + followed by exactly 26 digits (``^ses_\\d{26}$``). Anything else — 22 alnum, + shorter/longer digit runs, mixed alnum — is rejected with FreeTierError. + """ + return "ses_" + "".join(secrets.choice("0123456789") for _ in range(26)) + + +def _ensure_tools(body: bytes) -> bytes: + """Inject stub tools into the body when the client sent none. + + The free tier gateway requires a ``tools`` array with at least two + elements. Bodies that already carry two or more tools (real opencode + clients send 30+) pass through untouched; only tool-less or single-tool + bodies are rewritten. + """ + if not body: + return body + try: + data = json.loads(body) + except (json.JSONDecodeError, AttributeError): + return body + tools = data.get("tools") + if isinstance(tools, list) and len(tools) >= 2: + return body + data["tools"] = _STUB_TOOLS + return json.dumps(data).encode() + + def _forward_headers( - headers: dict[str, str], host: str, inject_session: bool = True + headers: dict[str, str], host: str, inject: bool = True ) -> dict[str, str]: out: dict[str, str] = {} for key, value in headers.items(): if key.lower() in _REQUEST_STRIP: continue out[key] = value - if inject_session: + if inject: known = {k.lower(): k for k in out} if "x-opencode-session" not in known: - out["X-Opencode-Session"] = _random_id("ses_", 22) + out["X-Opencode-Session"] = _random_session_id() if "x-opencode-request" not in known: out["X-Opencode-Request"] = _random_id("msg_", 24) + ua_key = known.get("user-agent") + if ua_key is not None and not out[ua_key].lower().startswith("opencode/"): + del out[ua_key] + ua_key = None + if ua_key is None: + out["User-Agent"] = _OPENCODE_UA if "content-type" not in {k.lower() for k in out}: out["Content-Type"] = "application/json" out["Host"] = host @@ -187,11 +258,13 @@ def _fetch( url = upstream_url(cfg_obj, request.path) host_header = httpx.URL(url).host # type: ignore[attr-defined] out_headers = _forward_headers( - request.headers, host_header or "", inject_session=cfg_obj.bridge_inject_session + request.headers, host_header or "", inject=cfg_obj.bridge_inject_session ) # Ask for identity so httpx neither injects its own Accept-Encoding nor # transparently decompresses: the body is then passed through verbatim. out_headers["Accept-Encoding"] = "identity" + if os.environ.get("OPENROT_DEBUG_REQ"): + _debug_request("OUT", url, out_headers, request.body) try: req = httpx.Request( request.method, url, headers=out_headers, content=request.body @@ -465,6 +538,7 @@ def serve() -> None: console.print("no active level, starting cascade...") cascade.start(False, False) cfg_obj = cfg.load_config() + cascade.background() url = base_url(cfg_obj) console.print( @@ -564,6 +638,8 @@ def _handle(self, method: str) -> None: ).rsplit("/", 1)[-1] cfg_obj = cfg.load_config() + if cfg_obj.bridge_inject_session: + body = _ensure_tools(body) request = _Request(method, self.path, dict(self.headers.items()), body) if os.environ.get("OPENROT_DEBUG_REQ"): _debug_request(method, self.path, request.headers, request.body) diff --git a/openrot/core/cascade.py b/openrot/core/cascade.py index 27c1389..afd27a6 100644 --- a/openrot/core/cascade.py +++ b/openrot/core/cascade.py @@ -226,8 +226,12 @@ def node_health_loop() -> None: rotate() -def warp_health_loop() -> None: - """Fall back to the node chain when WARP connectivity is lost.""" +def warp_health_loop(foreground: bool = True) -> None: + """Fall back to the node chain when WARP connectivity is lost. + + ``foreground=False`` (bridge daemon) skips the console line and does not + start its own node health loop — the caller runs one already. + """ while True: cfg_obj = cfg.load_config() time.sleep(cfg_obj.health_interval) @@ -235,10 +239,33 @@ def warp_health_loop() -> None: continue events.warning("[cascade] warp dropped; falling back to node chain") proxy.stop_proxy() - start_node(True) + start_node(foreground) return +def background() -> None: + """Run the health loops and refresh scheduler in daemon threads. + + Used by the bridge daemon (``serve()``), where the cascade is started + without a foreground loop: without this, a dead node or dropped WARP + would leave the bridge serving 502s forever. + """ + cfg_obj = cfg.load_config() + if cfg_obj.update_interval > 0: + threading.Thread( + target=refresh.run_scheduler, name="openrot-scheduler", daemon=True + ).start() + threading.Thread( + target=node_health_loop, name="openrot-node-health", daemon=True + ).start() + threading.Thread( + target=warp_health_loop, + name="openrot-warp-health", + kwargs={"foreground": False}, + daemon=True, + ).start() + + def rotate(first: bool = False) -> bool: """Rotate WARP IP, or pick the next node by priority in the chain. diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 8e89228..1941287 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -5,6 +5,7 @@ """ import gzip +import json import socket from io import BytesIO from pathlib import Path @@ -86,10 +87,27 @@ def _alnum_len(value: str, prefix: str) -> int: def test_forward_headers_injects_session_and_request_when_absent() -> None: out = bridge._forward_headers({}, "up.test") - assert _alnum_len(out["X-Opencode-Session"], "ses_") == 22 + assert _alnum_len(out["X-Opencode-Session"], "ses_") == 26 + assert out["X-Opencode-Session"][len("ses_"):].isdigit() assert _alnum_len(out["X-Opencode-Request"], "msg_") == 24 - assert out["X-Opencode-Session"][4:].isalnum() - assert out["X-Opencode-Request"][4:].isalnum() + assert out["X-Opencode-Request"][len("msg_"):].isalnum() + + +def test_forward_headers_injects_opencode_user_agent_when_absent() -> None: + out = bridge._forward_headers({}, "up.test") + assert out["User-Agent"].startswith("opencode/") + + +def test_forward_headers_overrides_non_opencode_user_agent() -> None: + out = bridge._forward_headers({"User-Agent": "curl/8.0"}, "up.test") + assert out["User-Agent"].startswith("opencode/") + assert sum(k.lower() == "user-agent" for k in out) == 1 + + +def test_forward_headers_keeps_opencode_user_agent() -> None: + ua = "opencode/1.18.31 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14" + out = bridge._forward_headers({"User-Agent": ua}, "up.test") + assert out["User-Agent"] == ua def test_forward_headers_respects_existing_session_and_request() -> None: @@ -109,18 +127,50 @@ def test_forward_headers_respects_existing_session_only() -> None: def test_forward_headers_no_injection_when_disabled() -> None: - out = bridge._forward_headers({}, "up.test", inject_session=False) + out = bridge._forward_headers({}, "up.test", inject=False) assert "X-Opencode-Session" not in out assert "X-Opencode-Request" not in out + assert "User-Agent" not in out def test_random_id_format_and_uniqueness() -> None: - a = bridge._random_id("ses_", 22) - b = bridge._random_id("ses_", 22) - assert _alnum_len(a, "ses_") == 22 + a = bridge._random_id("msg_", 24) + b = bridge._random_id("msg_", 24) + assert _alnum_len(a, "msg_") == 24 + assert a != b + + +def test_random_session_id_format() -> None: + a = bridge._random_session_id() + b = bridge._random_session_id() + assert _alnum_len(a, "ses_") == 26 + assert a[len("ses_"):].isdigit() assert a != b +def test_ensure_tools_injects_stubs_when_missing() -> None: + body = b'{"model": "big-pickle", "messages": []}' + out = json.loads(bridge._ensure_tools(body)) + assert len(out["tools"]) == 2 + assert out["tools"][0]["type"] == "function" + + +def test_ensure_tools_injects_stubs_when_single_tool() -> None: + body = b'{"model": "big-pickle", "tools": [{"type": "function"}]}' + out = json.loads(bridge._ensure_tools(body)) + assert len(out["tools"]) == 2 + + +def test_ensure_tools_keeps_two_or_more_tools() -> None: + body = b'{"model": "big-pickle", "tools": [{"type": "function"}, {"type": "function"}]}' + assert bridge._ensure_tools(body) == body + + +def test_ensure_tools_leaves_invalid_and_empty_bodies() -> None: + assert bridge._ensure_tools(b"") == b"" + assert bridge._ensure_tools(b"not json") == b"not json" + + class _FakeClient: def __init__(self, responses: list[httpx.Response]) -> None: self._responses = list(responses) @@ -461,6 +511,7 @@ def server_close(self) -> None: pass monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: False) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) started: list[tuple[bool, bool]] = [] monkeypatch.setattr(bridge.cascade, "start", lambda f, d: started.append((f, d))) monkeypatch.setattr( @@ -487,6 +538,7 @@ def server_close(self) -> None: pass monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) started: list = [] monkeypatch.setattr(bridge.cascade, "start", lambda *a: started.append(a)) monkeypatch.setattr( @@ -500,9 +552,43 @@ def server_close(self) -> None: assert started == [] +def test_serve_starts_background_loops(monkeypatch: pytest.MonkeyPatch) -> None: + """serve() always starts the cascade background loops (health + scheduler).""" + + class FakeServer: + def __init__(self, host: str, port: int) -> None: + self.host = host + self.port = port + + def serve_forever(self) -> None: + raise KeyboardInterrupt + + def server_close(self) -> None: + pass + + monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "start", lambda *a: None) + background_calls: list[int] = [] + monkeypatch.setattr( + bridge.cascade, "background", lambda: background_calls.append(1) + ) + monkeypatch.setattr( + bridge.cfg, + "load_config", + lambda: _cfg(bridge_port=7891, active_level=ActiveLevel.NODE), + ) + monkeypatch.setattr(bridge, "port_in_use", lambda h, p: False) + monkeypatch.setattr(bridge, "Bridge", FakeServer) + + bridge.serve() + + assert background_calls == [1] + + def test_serve_exits_when_port_busy(monkeypatch: pytest.MonkeyPatch) -> None: started: list = [] monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) monkeypatch.setattr(bridge.cascade, "start", lambda *a: started.append(a)) monkeypatch.setattr( bridge.cfg, @@ -589,6 +675,7 @@ def server_close(self) -> None: logged: list[str] = [] monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) monkeypatch.setattr( bridge.cfg, "load_config", @@ -627,6 +714,7 @@ def server_close(self) -> None: printed: list[str] = [] console_printed: list[str] = [] monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) monkeypatch.setattr( bridge.cfg, "load_config", @@ -662,6 +750,7 @@ def server_close(self) -> None: printed: list[str] = [] console_printed: list[str] = [] monkeypatch.setattr(bridge.cascade, "level_serving", lambda cfg_obj: True) + monkeypatch.setattr(bridge.cascade, "background", lambda: None) monkeypatch.setattr( bridge.cfg, "load_config", diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index db7b54b..7290a7f 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -536,11 +536,13 @@ def fake_is_connected() -> bool: monkeypatch.setattr(cascade.cfg, "load_config", fake_load) monkeypatch.setattr(cascade.warp, "is_connected", fake_is_connected) monkeypatch.setattr(cascade.proxy, "stop_proxy", lambda: None) - monkeypatch.setattr(cascade, "start_node", lambda fg: None) + started_with: list[bool] = [] + monkeypatch.setattr(cascade, "start_node", started_with.append) cascade.warp_health_loop() assert call_count["n"] == 1 + assert started_with == [True] def test_warp_health_loop_continues_while_connected( @@ -571,6 +573,86 @@ def fake_is_connected() -> bool: assert call_count["n"] == 2 +def test_warp_health_loop_falls_back_without_foreground( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """In bridge-daemon mode the fallback starts the node chain non-foreground.""" + call_count = {"n": 0} + + def fake_sleep(s: float) -> None: + call_count["n"] += 1 + + def fake_load() -> Config: + return Config(update_interval=0, health_interval=1) + + _patch_events(monkeypatch) + monkeypatch.setattr(cascade.time, "sleep", fake_sleep) + monkeypatch.setattr(cascade.cfg, "load_config", fake_load) + monkeypatch.setattr(cascade.warp, "is_connected", lambda: False) + monkeypatch.setattr(cascade.proxy, "stop_proxy", lambda: None) + started_with: list[bool] = [] + monkeypatch.setattr(cascade, "start_node", started_with.append) + + cascade.warp_health_loop(foreground=False) + + assert started_with == [False] + + +def test_background_starts_loops_and_scheduler( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """background() starts scheduler and both health loops as daemon threads.""" + started: list[dict[str, object]] = [] + + class FakeThread: + def __init__(self, **kwargs: object) -> None: + started.append(kwargs) + + def start(self) -> None: + pass + + monkeypatch.setattr(cascade.threading, "Thread", FakeThread) + monkeypatch.setattr( + cascade.cfg, + "load_config", + lambda: Config(update_interval=60, health_interval=1), + ) + + cascade.background() + + names = {t["name"] for t in started} + assert names == {"openrot-scheduler", "openrot-node-health", "openrot-warp-health"} + assert all(t["daemon"] is True for t in started) + warp = next(t for t in started if t["name"] == "openrot-warp-health") + assert warp["kwargs"] == {"foreground": False} + + +def test_background_skips_scheduler_when_disabled( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """With update_interval=0 only the health loops are started.""" + started: list[dict[str, object]] = [] + + class FakeThread: + def __init__(self, **kwargs: object) -> None: + started.append(kwargs) + + def start(self) -> None: + pass + + monkeypatch.setattr(cascade.threading, "Thread", FakeThread) + monkeypatch.setattr( + cascade.cfg, "load_config", lambda: Config(update_interval=0, health_interval=1) + ) + + cascade.background() + + assert {t["name"] for t in started} == { + "openrot-node-health", + "openrot-warp-health", + } + + def test_rotate_waits_when_already_in_progress( monkeypatch: pytest.MonkeyPatch, ) -> None: From f2680845aad9e0c3ea47f49a88cdcf21b474445c Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Fri, 18 Sep 2026 15:37:39 +0300 Subject: [PATCH 18/22] fix(warp): :bug: robust rotate() retry tests and bridge lint fixes --- openrot/core/bridge.py | 37 ++++++++++++++++++++----------- tests/unit/core/test_bridge.py | 11 +++++---- tests/unit/providers/test_warp.py | 34 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 70b4001..8f67f70 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -110,7 +110,11 @@ def _debug_request( # Request leg: transport framing is re-created from the raw body by httpx. # ``x-opencode-client`` is an opencode client marker the gateway does not # inspect, so it is dropped rather than forwarded. -_REQUEST_STRIP = _HOP_BY_HOP | {"transfer-encoding", "content-length", "x-opencode-client"} +_REQUEST_STRIP = _HOP_BY_HOP | { + "transfer-encoding", + "content-length", + "x-opencode-client", +} # Response leg: framing is re-created below (content-length or chunked). _RESPONSE_STRIP = _HOP_BY_HOP | {"transfer-encoding"} @@ -194,7 +198,7 @@ def _random_id(prefix: str, length: int) -> str: def _random_session_id() -> str: - """Return a session id the free tier gateway accepts. + r"""Return a session id the free tier gateway accepts. The gateway validates ``X-Opencode-Session`` by format only: ``ses_`` followed by exactly 26 digits (``^ses_\\d{26}$``). Anything else — 22 alnum, @@ -215,7 +219,7 @@ def _ensure_tools(body: bytes) -> bytes: return body try: data = json.loads(body) - except (json.JSONDecodeError, AttributeError): + except json.JSONDecodeError, AttributeError: return body tools = data.get("tools") if isinstance(tools, list) and len(tools) >= 2: @@ -611,10 +615,8 @@ def _log_request_console(self, method: str) -> None: parts.append(f"req={req_id}") _log(f"[bridge] {' '.join(parts)}") - def _handle(self, method: str) -> None: - length = int(self.headers.get("Content-Length", 0) or 0) - body = self.rfile.read(length) if length else b"" - + def _inspect_body(self, body: bytes) -> None: + """Record the model and prompt char count from a JSON body.""" model = "" prompt_chars = 0 if body: @@ -629,7 +631,6 @@ def _handle(self, method: str) -> None: ) except json.JSONDecodeError, AttributeError: pass - self._model = model self._prompt_chars = prompt_chars self._req_id = next( @@ -637,6 +638,20 @@ def _handle(self, method: str) -> None: "", ).rsplit("/", 1)[-1] + def _fail_upstream(self, method: str, t0: float, exc: BaseException) -> None: + """Record a failed upstream exchange and send a 502 to the client.""" + self._elapsed_ms = (time.monotonic() - t0) * 1000 + self._output_chars = 0 + self._status = 502 + self._log_request_console(method) + _send_502(self, str(exc)) + + def _handle(self, method: str) -> None: + length = int(self.headers.get("Content-Length", 0) or 0) + body = self.rfile.read(length) if length else b"" + + self._inspect_body(body) + cfg_obj = cfg.load_config() if cfg_obj.bridge_inject_session: body = _ensure_tools(body) @@ -656,11 +671,7 @@ def _handle(self, method: str) -> None: _pace_start(cfg_obj) resp, _client = forward(cfg_obj, request, rotate_on_429=False) except httpx.HTTPError as retry_exc: - self._elapsed_ms = (time.monotonic() - t0) * 1000 - self._output_chars = 0 - self._status = 502 - self._log_request_console(method) - _send_502(self, str(retry_exc)) + self._fail_upstream(method, t0, retry_exc) return finally: semaphore.release() diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 1941287..2713c48 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -88,9 +88,9 @@ def _alnum_len(value: str, prefix: str) -> int: def test_forward_headers_injects_session_and_request_when_absent() -> None: out = bridge._forward_headers({}, "up.test") assert _alnum_len(out["X-Opencode-Session"], "ses_") == 26 - assert out["X-Opencode-Session"][len("ses_"):].isdigit() + assert out["X-Opencode-Session"][len("ses_") :].isdigit() assert _alnum_len(out["X-Opencode-Request"], "msg_") == 24 - assert out["X-Opencode-Request"][len("msg_"):].isalnum() + assert out["X-Opencode-Request"][len("msg_") :].isalnum() def test_forward_headers_injects_opencode_user_agent_when_absent() -> None: @@ -144,7 +144,7 @@ def test_random_session_id_format() -> None: a = bridge._random_session_id() b = bridge._random_session_id() assert _alnum_len(a, "ses_") == 26 - assert a[len("ses_"):].isdigit() + assert a[len("ses_") :].isdigit() assert a != b @@ -162,7 +162,10 @@ def test_ensure_tools_injects_stubs_when_single_tool() -> None: def test_ensure_tools_keeps_two_or_more_tools() -> None: - body = b'{"model": "big-pickle", "tools": [{"type": "function"}, {"type": "function"}]}' + body = ( + b'{"model": "big-pickle", ' + b'"tools": [{"type": "function"}, {"type": "function"}]}' + ) assert bridge._ensure_tools(body) == body diff --git a/tests/unit/providers/test_warp.py b/tests/unit/providers/test_warp.py index c53f6bb..a7dded6 100644 --- a/tests/unit/providers/test_warp.py +++ b/tests/unit/providers/test_warp.py @@ -130,9 +130,12 @@ def test_disconnect_not_installed(monkeypatch: pytest.MonkeyPatch) -> None: def test_rotate_reconnects(monkeypatch: pytest.MonkeyPatch) -> None: calls: list[str] = [] + ips = iter(["1.1.1.1", "2.2.2.2"]) monkeypatch.setattr(w, "is_installed", lambda: True) monkeypatch.setattr(w, "disconnect", lambda: calls.append("dis") or True) monkeypatch.setattr(w, "connect", lambda: calls.append("con") or True) + monkeypatch.setattr(w, "current_ip", lambda: next(ips)) + monkeypatch.setattr(w, "time", _NoSleep) assert w.rotate() is True assert calls == ["dis", "con"] @@ -142,6 +145,37 @@ def test_rotate_not_installed(monkeypatch: pytest.MonkeyPatch) -> None: assert w.rotate() is False +def test_rotate_retries_until_ip_changes( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Same IP on early attempts, a change on the last one succeeds.""" + calls: list[str] = [] + ips = iter(["1.1.1.1", "1.1.1.1", "1.1.1.1", "2.2.2.2"]) + monkeypatch.setattr(w, "is_installed", lambda: True) + monkeypatch.setattr(w, "disconnect", lambda: calls.append("dis") or True) + monkeypatch.setattr(w, "connect", lambda: calls.append("con") or True) + monkeypatch.setattr(w, "current_ip", lambda: next(ips)) + monkeypatch.setattr(w, "time", _NoSleep) + assert w.rotate() is True + assert calls == ["dis", "con", "dis", "con", "dis", "con"] + + +def test_rotate_exhausted_falls_back_to_connected( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """IP never changes; after max attempts return the connection state.""" + calls: list[str] = [] + ips = ["1.1.1.1"] + ["1.1.1.1"] * w.ROTATE_MAX_ATTEMPTS + monkeypatch.setattr(w, "is_installed", lambda: True) + monkeypatch.setattr(w, "disconnect", lambda: calls.append("dis") or True) + monkeypatch.setattr(w, "connect", lambda: calls.append("con") or True) + monkeypatch.setattr(w, "current_ip", lambda: ips.pop(0)) + monkeypatch.setattr(w, "time", _NoSleep) + monkeypatch.setattr(w, "is_connected", lambda: False) + assert w.rotate() is False + assert len(calls) == w.ROTATE_MAX_ATTEMPTS * 2 + + def test_install_raises_when_missing(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(w, "is_installed", lambda: False) with pytest.raises(RuntimeError, match="warp-cli not found"): From b54be9b0ef7c56bc5aa448f0343512322ed48b9f Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Sun, 20 Sep 2026 02:17:20 +0300 Subject: [PATCH 19/22] chore: :wrench: track deepwork state in gitignore and .ignore --- .gitignore | 3 +++ .ignore | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 .ignore diff --git a/.gitignore b/.gitignore index 9904a11..f9328c5 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ dist/ # Codegraph state .codegraph + +# Deepwork state (git-local, OpenCode-readable) +.slim/deepwork/ diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..a704baf --- /dev/null +++ b/.ignore @@ -0,0 +1,2 @@ +!.slim/deepwork/ +!.slim/deepwork/** From ca345cba8ad8c0f0f81b5c4c7899d15e80d159c0 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Sun, 20 Sep 2026 02:17:20 +0300 Subject: [PATCH 20/22] refactor: :recycle: reorder module members per clean-code layout - Reorder top-level members in 7 production and 7 test files - Remove separator comment lines and reword slop docstring in bridge.py - Include stub-tools append/dedup fix and its test updates (carried WIP) --- openrot/cli.py | 12 +- openrot/config.py | 8 +- openrot/core/bridge.py | 224 ++++++++++++++-------------- openrot/core/singbox.py | 6 +- openrot/core/verify.py | 8 +- openrot/providers/vless.py | 8 +- openrot/self_update.py | 18 +-- tests/integration/test_full_loop.py | 23 ++- tests/unit/core/test_bridge.py | 75 +++++----- tests/unit/core/test_cascade.py | 24 +-- tests/unit/core/test_singbox.py | 86 +++++------ tests/unit/core/test_verify.py | 62 ++++---- tests/unit/providers/test_proxy.py | 60 ++++---- tests/unit/providers/test_warp.py | 12 +- 14 files changed, 312 insertions(+), 314 deletions(-) diff --git a/openrot/cli.py b/openrot/cli.py index 058f5c4..356817b 100644 --- a/openrot/cli.py +++ b/openrot/cli.py @@ -47,9 +47,11 @@ no_args_is_help=True, rich_markup_mode="rich", ) -app.add_typer(profile_app, name="profile") console = Console() events = log.get_logger() +warp_app = typer.Typer( + help="Manage Cloudflare WARP", no_args_is_help=True, rich_markup_mode="rich" +) @contextmanager @@ -229,6 +231,9 @@ def profile_set( ) +app.add_typer(profile_app, name="profile") + + @app.command("list") def list_nodes( alive: bool = typer.Option(False, "--alive", help="Only show alive nodes"), @@ -756,11 +761,6 @@ def provider_cmd( events.info("[provider] %s", "enabled" if new_state else "disabled") -warp_app = typer.Typer( - help="Manage Cloudflare WARP", no_args_is_help=True, rich_markup_mode="rich" -) - - @warp_app.command("install") def warp_install() -> None: """Install the Cloudflare WARP client (prints instructions).""" diff --git a/openrot/config.py b/openrot/config.py index 771fade..9947e4b 100644 --- a/openrot/config.py +++ b/openrot/config.py @@ -50,10 +50,6 @@ ] -class ConfigError(ValueError): - """Raised when the config file cannot be read or validated.""" - - def _base_dir() -> Path: env = os.environ.get("OPENROT_DIR") if env: @@ -244,3 +240,7 @@ def update_config(path: Path, mutator: Callable[[Config], Any]) -> Any: result = mutator(cfg) _save_unlocked(cfg, path) return result + + +class ConfigError(ValueError): + """Raised when the config file cannot be read or validated.""" diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index 8f67f70..fce8983 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -42,9 +42,79 @@ from openrot.log import get_logger as _get_logger from openrot.models.config import DEFAULT_BRIDGE_UPSTREAM + +@dataclass(frozen=True) +class _Request: + """A single HTTP request the bridge forwards upstream.""" + + method: str + path: str + headers: dict[str, str] + body: bytes + + +# True hop-by-hop headers, stripped on both legs. +_HOP_BY_HOP = {"connection", "host", "proxy-connection"} + +# Request leg: transport framing is re-created from the raw body by httpx. +# ``x-opencode-client`` is an opencode client marker the gateway does not +# inspect, so it is dropped rather than forwarded. +_REQUEST_STRIP = _HOP_BY_HOP | { + "transfer-encoding", + "content-length", +} + +# Response leg: framing is re-created below (content-length or chunked). +_RESPONSE_STRIP = _HOP_BY_HOP | {"transfer-encoding"} + +_ALNUM = string.ascii_letters + string.digits + +# User-Agent the free tier gateway expects; injected when the client sends +# none or a non-opencode one (curl, python-requests, ...). +_OPENCODE_UA = "opencode/1.18.30 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14" + +# Stub tools injected into bodies that carry no (or too few) tools: the free +# tier gateway requires a ``tools`` array with at least two elements and +# validates the tool names against real opencode tools (bash, read, ...). +_STUB_TOOLS = [ + { + "type": "function", + "function": { + "name": "bash", + "description": "run", + "parameters": { + "type": "object", + "properties": {"c": {"type": "string"}}, + }, + }, + }, + { + "type": "function", + "function": { + "name": "read", + "description": "read", + "parameters": { + "type": "object", + "properties": {"p": {"type": "string"}}, + }, + }, + }, +] + +_RETRY_DELAY_DEFAULT = 5.0 + + events = _get_logger() console = Console() +_shared_client: httpx.Client | None = None +_shared_client_lock = threading.Lock() + +_request_semaphore: threading.Semaphore | None = None +_semaphore_lock = threading.Lock() +_last_start_monotonic: float = 0.0 +_pacing_lock = threading.Lock() + def _log(msg: str) -> None: """Log a message through the events logger with consistent formatting.""" @@ -104,22 +174,6 @@ def _debug_request( ) -# True hop-by-hop headers, stripped on both legs. -_HOP_BY_HOP = {"connection", "host", "proxy-connection"} - -# Request leg: transport framing is re-created from the raw body by httpx. -# ``x-opencode-client`` is an opencode client marker the gateway does not -# inspect, so it is dropped rather than forwarded. -_REQUEST_STRIP = _HOP_BY_HOP | { - "transfer-encoding", - "content-length", - "x-opencode-client", -} - -# Response leg: framing is re-created below (content-length or chunked). -_RESPONSE_STRIP = _HOP_BY_HOP | {"transfer-encoding"} - - def _send_502(handler: BaseHTTPRequestHandler, message: str) -> None: """Write a JSON 502 response to the connected client.""" msg = {"error": {"message": message, "type": "upstream"}} @@ -137,56 +191,11 @@ def upstream_url(cfg_obj: Config, path: str) -> str: return urljoin(base.rstrip("/") + "/", path.lstrip("/")) -@dataclass(frozen=True) -class _Request: - """A single HTTP request the bridge forwards upstream.""" - - method: str - path: str - headers: dict[str, str] - body: bytes - - def _client(cfg_obj: Config) -> httpx.Client: proxy_url = f"http://127.0.0.1:{cfg_obj.port}" return make_client(proxy=proxy_url, timeout=300) -_ALNUM = string.ascii_letters + string.digits - -# User-Agent the free tier gateway expects; injected when the client sends -# none or a non-opencode one (curl, python-requests, ...). -_OPENCODE_UA = "opencode/1.18.30 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14" - -# Stub tools injected into bodies that carry no (or too few) tools: the free -# tier gateway requires a ``tools`` array with at least two elements and -# validates the tool names against real opencode tools (bash, read, ...). -_STUB_TOOLS = [ - { - "type": "function", - "function": { - "name": "bash", - "description": "run", - "parameters": { - "type": "object", - "properties": {"c": {"type": "string"}}, - }, - }, - }, - { - "type": "function", - "function": { - "name": "read", - "description": "read", - "parameters": { - "type": "object", - "properties": {"p": {"type": "string"}}, - }, - }, - }, -] - - def _random_id(prefix: str, length: int) -> str: """Return a random alnum identifier (``prefix`` + ``length`` chars). @@ -208,12 +217,11 @@ def _random_session_id() -> str: def _ensure_tools(body: bytes) -> bytes: - """Inject stub tools into the body when the client sent none. + """Append the stub tools to the request's tools array. The free tier gateway requires a ``tools`` array with at least two - elements. Bodies that already carry two or more tools (real opencode - clients send 30+) pass through untouched; only tool-less or single-tool - bodies are rewritten. + elements, so the two stub tools are appended to every JSON body — + unless a tool with the same name is already present. """ if not body: return body @@ -222,9 +230,16 @@ def _ensure_tools(body: bytes) -> bytes: except json.JSONDecodeError, AttributeError: return body tools = data.get("tools") - if isinstance(tools, list) and len(tools) >= 2: - return body - data["tools"] = _STUB_TOOLS + if not isinstance(tools, list): + tools = [] + existing = { + tool.get("function", {}).get("name") for tool in tools if isinstance(tool, dict) + } + data["tools"] = tools + [ + stub + for stub in _STUB_TOOLS + if stub.get("function", {}).get("name") not in existing + ] return json.dumps(data).encode() @@ -278,9 +293,6 @@ def _fetch( raise httpx.HTTPError(str(exc)) from exc -_RETRY_DELAY_DEFAULT = 5.0 - - def _is_usage_limit_error(resp: httpx.Response) -> bool: """Return True when a 429 body contains FreeUsageLimitError.""" if resp.status_code != 429: @@ -303,14 +315,7 @@ def _retry_delay(resp: httpx.Response) -> float: return _RETRY_DELAY_DEFAULT -# --------------------------------------------------------------------------- # Shared httpx client — created once, reused across requests. -# --------------------------------------------------------------------------- - -_shared_client: httpx.Client | None = None -_shared_client_lock = threading.Lock() - - def _get_client(cfg_obj: Config) -> httpx.Client: """Return the shared ``httpx.Client``, creating it on first call.""" global _shared_client @@ -332,16 +337,7 @@ def _reset_client() -> None: _shared_client = None -# --------------------------------------------------------------------------- # Concurrency limiter — prevents too many parallel upstream requests. -# --------------------------------------------------------------------------- - -_request_semaphore: threading.Semaphore | None = None -_semaphore_lock = threading.Lock() -_last_start_monotonic: float = 0.0 -_pacing_lock = threading.Lock() - - def _get_semaphore(cfg_obj: Config) -> threading.Semaphore: """Return the request semaphore, creating it on first call.""" global _request_semaphore @@ -368,11 +364,7 @@ def _pace_start(cfg_obj: Config) -> None: _last_start_monotonic = time.monotonic() -# --------------------------------------------------------------------------- # Rotation logging — shared between forward() and BridgeHandler. -# --------------------------------------------------------------------------- - - def _log_rotation(rotated: bool, elapsed_s: float) -> None: if rotated: _log(f"[bridge] rotation took {elapsed_s:.1f}s") @@ -522,7 +514,7 @@ def running(cfg_obj: cfg.Config) -> bool: def serve() -> None: - """Ensure the cascade is up and run the bridge server in the foreground. + """Start the cascade and run the bridge server in the foreground. Standalone bridge for testing (``openrot start bridge``): start it, then point any OpenAI-compatible client at the loopback URL and watch it route @@ -583,6 +575,30 @@ class BridgeHandler(BaseHTTPRequestHandler): server_version = "openrot-bridge/1" protocol_version = "HTTP/1.1" + def do_GET(self) -> None: # noqa: D102 + self._handle("GET") + + def do_POST(self) -> None: # noqa: D102 + self._handle("POST") + + def do_OPTIONS(self) -> None: # noqa: D102 + self._handle("OPTIONS") + + def do_PUT(self) -> None: # noqa: D102 + self._handle("PUT") + + def do_DELETE(self) -> None: # noqa: D102 + self._handle("DELETE") + + def do_PATCH(self) -> None: # noqa: D102 + self._handle("PATCH") + + def do_HEAD(self) -> None: # noqa: D102 + self._handle("HEAD") + + def log_message(self, format: str, *args: object) -> None: + """No-op: request logging handled by _log_request_console.""" + def _log_request_console(self, method: str) -> None: elapsed_s = self._elapsed_ms / 1000 parts = [f"{method} {self.path}"] @@ -684,30 +700,6 @@ def _handle(self, method: str) -> None: self._status = resp.status_code self._log_request_console(method) - def do_GET(self) -> None: # noqa: D102 - self._handle("GET") - - def do_POST(self) -> None: # noqa: D102 - self._handle("POST") - - def do_OPTIONS(self) -> None: # noqa: D102 - self._handle("OPTIONS") - - def do_PUT(self) -> None: # noqa: D102 - self._handle("PUT") - - def do_DELETE(self) -> None: # noqa: D102 - self._handle("DELETE") - - def do_PATCH(self) -> None: # noqa: D102 - self._handle("PATCH") - - def do_HEAD(self) -> None: # noqa: D102 - self._handle("HEAD") - - def log_message(self, format: str, *args: object) -> None: - """No-op: request logging handled by _log_request_console.""" - class Bridge(ThreadingHTTPServer): """A threaded loopback server exposing the openrot bridge.""" diff --git a/openrot/core/singbox.py b/openrot/core/singbox.py index 5e77b46..9a277d6 100644 --- a/openrot/core/singbox.py +++ b/openrot/core/singbox.py @@ -94,6 +94,9 @@ class SingBoxConfig: log: LogOptions = field(default_factory=LogOptions) +EgressResult = tuple[bool, float | None, str | None] + + def _strip_none(value: Any) -> Any: """Recursively drop keys whose value is None (and prune empties in dicts).""" if isinstance(value, dict): @@ -201,9 +204,6 @@ def _wait_for_port(host: str, port: int, timeout: float, step: float = 0.05) -> return False -EgressResult = tuple[bool, float | None, str | None] - - def probe_vless( node: VlessNode, singbox_bin: str, timeout: float, url: str | None = None ) -> EgressResult: diff --git a/openrot/core/verify.py b/openrot/core/verify.py index 179d9a1..457e100 100644 --- a/openrot/core/verify.py +++ b/openrot/core/verify.py @@ -28,15 +28,15 @@ ) from openrot.providers import proxy, vless -MAX_WORKERS = 16 - -CHECK_LISTEN_PORT = 1 - Stage = Callable[[str, int, int], None] ProgressFn = Callable[[str, int, int], None] RelayCandidate = tuple[str, vless.VlessNode] ProxyKey = tuple[str, str, int] # (protocol, host, port) +MAX_WORKERS = 16 + +CHECK_LISTEN_PORT = 1 + def median(values: list[float]) -> float: """Median of the observed latencies (0.0 for an empty sample).""" diff --git a/openrot/providers/vless.py b/openrot/providers/vless.py index 51dbdd0..d194589 100644 --- a/openrot/providers/vless.py +++ b/openrot/providers/vless.py @@ -4,10 +4,6 @@ from dataclasses import dataclass -class ParseError(Exception): - """Raised when a string cannot be parsed as a vless:// link.""" - - @dataclass class VlessNode: """Parsed fields of a single vless:// link.""" @@ -109,3 +105,7 @@ def extract_from_text(text: str) -> list[str]: if decoded != text: return extract_vless_records(decoded) return [] + + +class ParseError(Exception): + """Raised when a string cannot be parsed as a vless:// link.""" diff --git a/openrot/self_update.py b/openrot/self_update.py index 8610fa3..1a01eaf 100644 --- a/openrot/self_update.py +++ b/openrot/self_update.py @@ -32,6 +32,15 @@ class ServiceState(NamedTuple): bridge_running: bool +class UpdateResult(NamedTuple): + """Result of a self-update check or install operation.""" + + current: str + latest: str + updated: bool + message: str + + def _check_services() -> ServiceState: """Check which openrot daemon/proxy/bridge processes are currently running.""" daemon_pid = daemon.load_daemon_pid(cfg.DAEMON_PID_PATH) @@ -65,15 +74,6 @@ def _print_new_version() -> None: subprocess.run([str(binary), "--version"], check=True) # noqa: S603 -class UpdateResult(NamedTuple): - """Result of a self-update check or install operation.""" - - current: str - latest: str - updated: bool - message: str - - def _current_os() -> str: system = platform.system().lower() if system == "darwin": diff --git a/tests/integration/test_full_loop.py b/tests/integration/test_full_loop.py index 7762cc3..f36ee91 100644 --- a/tests/integration/test_full_loop.py +++ b/tests/integration/test_full_loop.py @@ -44,6 +44,17 @@ REPO_ROOT = Path(__file__).resolve().parents[2] +TRANS_NET_ERRORS = ( + "TimeoutException", + "ConnectError", + "ReadTimeout", + "NetworkError", + "ProxyError", + "RemoteProtocolError", + "ConnectionResetError", + "BrokenPipeError", +) + def _free_port() -> int: with socket.socket() as s: @@ -99,18 +110,6 @@ def _disable_warp(cfg_path: Path) -> None: save_config(cfg_obj, cfg_path) -TRANS_NET_ERRORS = ( - "TimeoutException", - "ConnectError", - "ReadTimeout", - "NetworkError", - "ProxyError", - "RemoteProtocolError", - "ConnectionResetError", - "BrokenPipeError", -) - - def _skip_on_transient_error(proc: subprocess.CompletedProcess[str]) -> None: """Skip when the subprocess failed only due to a transient network error.""" if proc.returncode == 0: diff --git a/tests/unit/core/test_bridge.py b/tests/unit/core/test_bridge.py index 2713c48..5bd1786 100644 --- a/tests/unit/core/test_bridge.py +++ b/tests/unit/core/test_bridge.py @@ -155,18 +155,23 @@ def test_ensure_tools_injects_stubs_when_missing() -> None: assert out["tools"][0]["type"] == "function" -def test_ensure_tools_injects_stubs_when_single_tool() -> None: +def test_ensure_tools_appends_stubs_when_single_tool() -> None: body = b'{"model": "big-pickle", "tools": [{"type": "function"}]}' out = json.loads(bridge._ensure_tools(body)) - assert len(out["tools"]) == 2 + assert len(out["tools"]) == 3 + assert out["tools"][0] == {"type": "function"} + assert out["tools"][1:] == bridge._STUB_TOOLS -def test_ensure_tools_keeps_two_or_more_tools() -> None: +def test_ensure_tools_appends_stubs_to_two_or_more_tools() -> None: body = ( b'{"model": "big-pickle", ' b'"tools": [{"type": "function"}, {"type": "function"}]}' ) - assert bridge._ensure_tools(body) == body + out = json.loads(bridge._ensure_tools(body)) + assert len(out["tools"]) == 4 + assert out["tools"][:2] == [{"type": "function"}, {"type": "function"}] + assert out["tools"][2:] == bridge._STUB_TOOLS def test_ensure_tools_leaves_invalid_and_empty_bodies() -> None: @@ -174,29 +179,6 @@ def test_ensure_tools_leaves_invalid_and_empty_bodies() -> None: assert bridge._ensure_tools(b"not json") == b"not json" -class _FakeClient: - def __init__(self, responses: list[httpx.Response]) -> None: - self._responses = list(responses) - self.closed = False - self.last_url: str | None = None - - def build_request(self, method: str, url: str, **kw: object) -> httpx.Request: - return httpx.Request(method, url, **kw) - - def send(self, request: httpx.Request, *, stream: bool = True) -> httpx.Response: - self.last_url = str(request.url) - return self._responses.pop(0) - - def close(self) -> None: - self.closed = True - - def __enter__(self) -> Self: - return self - - def __exit__(self, *exc: object) -> None: - self.close() - - def test_forward_returns_upstream_response( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -694,14 +676,6 @@ def server_close(self) -> None: assert any("SECURITY" in msg for msg in logged) -class _FakeStdout: - def __init__(self, tty: bool) -> None: - self._tty = tty - - def isatty(self) -> bool: - return self._tty - - def test_serve_hides_ctrl_c_tip_when_not_a_tty(monkeypatch: pytest.MonkeyPatch) -> None: class FakeServer: def __init__(self, host: str, port: int) -> None: @@ -1092,3 +1066,34 @@ def test_bridge_server_binds_and_closes() -> None: assert server.server_address[1] > 0 finally: server.server_close() + + +class _FakeClient: + def __init__(self, responses: list[httpx.Response]) -> None: + self._responses = list(responses) + self.closed = False + self.last_url: str | None = None + + def build_request(self, method: str, url: str, **kw: object) -> httpx.Request: + return httpx.Request(method, url, **kw) + + def send(self, request: httpx.Request, *, stream: bool = True) -> httpx.Response: + self.last_url = str(request.url) + return self._responses.pop(0) + + def close(self) -> None: + self.closed = True + + def __enter__(self) -> Self: + return self + + def __exit__(self, *exc: object) -> None: + self.close() + + +class _FakeStdout: + def __init__(self, tty: bool) -> None: + self._tty = tty + + def isatty(self) -> bool: + return self._tty diff --git a/tests/unit/core/test_cascade.py b/tests/unit/core/test_cascade.py index 7290a7f..a175c72 100644 --- a/tests/unit/core/test_cascade.py +++ b/tests/unit/core/test_cascade.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import threading import pytest @@ -7,17 +9,6 @@ from openrot.models import Config, Node, NodeStatus, Profile -class _Events: - def __init__(self) -> None: - self.calls: list[tuple[str, tuple[object, ...]]] = [] - - def info(self, *args: object) -> None: - self.calls.append(("info", args)) - - def warning(self, *args: object) -> None: - self.calls.append(("warning", args)) - - def _patch_events(monkeypatch: pytest.MonkeyPatch) -> _Events: events = _Events() monkeypatch.setattr(cascade, "events", events) @@ -691,3 +682,14 @@ def second_rotate() -> None: assert call_count["n"] == 1 assert sorted(returns) == [False, True] + + +class _Events: + def __init__(self) -> None: + self.calls: list[tuple[str, tuple[object, ...]]] = [] + + def info(self, *args: object) -> None: + self.calls.append(("info", args)) + + def warning(self, *args: object) -> None: + self.calls.append(("warning", args)) diff --git a/tests/unit/core/test_singbox.py b/tests/unit/core/test_singbox.py index 432318a..6dac3a3 100644 --- a/tests/unit/core/test_singbox.py +++ b/tests/unit/core/test_singbox.py @@ -21,6 +21,14 @@ def _vless(**overrides: Any) -> VlessNode: return VlessNode(**defaults) +def _node_probe_conf() -> object: + class _Conf: + def unlink(self, missing_ok: bool = False) -> None: + return None + + return _Conf() + + def test_relay_config_tls() -> None: data = generate_singbox_config(_vless(tls=True, servername="example.com"), 7890) inbound = data["inbounds"][0] @@ -88,44 +96,6 @@ def test_free_config_socks5() -> None: assert data["outbounds"][0]["type"] == "socks" -class _RunningProc: - def poll(self) -> None: - return None - - returncode = 0 - - def terminate(self) -> None: - pass - - -class _ExitedProc: - def poll(self) -> int: - return 3 - - returncode = 3 - - def terminate(self) -> None: - pass - - -class _FakeResponse: - status_code = 204 - - -class _FakeClient: - def __init__(self, **kwargs: Any) -> None: - pass - - def get(self, url: str) -> _FakeResponse: - return _FakeResponse() - - def __enter__(self) -> Self: - return self - - def __exit__(self, *a: object) -> None: - return None - - def test_probe_vless_waits_for_readiness_then_probes( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -166,9 +136,39 @@ def warning(self, *args: object) -> None: assert rc == 3 -def _node_probe_conf() -> object: - class _Conf: - def unlink(self, missing_ok: bool = False) -> None: - return None +class _RunningProc: + def poll(self) -> None: + return None - return _Conf() + returncode = 0 + + def terminate(self) -> None: + pass + + +class _ExitedProc: + def poll(self) -> int: + return 3 + + returncode = 3 + + def terminate(self) -> None: + pass + + +class _FakeResponse: + status_code = 204 + + +class _FakeClient: + def __init__(self, **kwargs: Any) -> None: + pass + + def get(self, url: str) -> _FakeResponse: + return _FakeResponse() + + def __enter__(self) -> Self: + return self + + def __exit__(self, *a: object) -> None: + return None diff --git a/tests/unit/core/test_verify.py b/tests/unit/core/test_verify.py index 379ae8a..69f06ca 100644 --- a/tests/unit/core/test_verify.py +++ b/tests/unit/core/test_verify.py @@ -18,6 +18,10 @@ ) +def _parsed(raw: str = _RELAY) -> vless.VlessNode: + return vless.parse_vless(raw) + + def test_verify_pool_default_limits_cap_to_20() -> None: assert verify.TOP_LIMIT == 20 assert ( @@ -53,21 +57,6 @@ def test_verify_progress_reports_each_node(monkeypatch: pytest.MonkeyPatch) -> N assert probe_done == [("probe", 1, 1)] -def _parsed(raw: str = _RELAY) -> vless.VlessNode: - return vless.parse_vless(raw) - - -class _Ctx: - def __enter__(self) -> Self: - return self - - def __exit__(self, *a: object) -> bool: - return False - - def wrap_socket(self, sock: object, server_hostname: str | None = None) -> _Ctx: - return self - - def test_median() -> None: assert verify.median([]) == 0.0 assert verify.median([10.0, 20.0, 30.0]) == 20.0 @@ -89,22 +78,6 @@ def refused(host: str, timeout: float = 0) -> object: assert verify.tcp_reachable("1.1.1.1", 80, 1.0) is False -class _TlsCtx: - check_hostname = False - verify_mode = None - - def wrap_socket(self, sock: object, server_hostname: str | None = None) -> _Ctx: - return _Ctx() - - -class _TlsFailCtx: - check_hostname = False - verify_mode = None - - def wrap_socket(self, sock: object, server_hostname: str | None = None) -> object: - raise OSError("handshake failed") - - def test_tls_handshake_ok(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(verify.ssl, "create_default_context", lambda: _TlsCtx()) monkeypatch.setattr( @@ -296,3 +269,30 @@ def test_nodes_from_proxy_survivors() -> None: assert nodes[1].raw == "socks5://2.2.2.2:1080" assert nodes[0].egress_ip is None assert nodes[1].egress_ip == "5.6.7.8" + + +class _Ctx: + def __enter__(self) -> Self: + return self + + def __exit__(self, *a: object) -> bool: + return False + + def wrap_socket(self, sock: object, server_hostname: str | None = None) -> _Ctx: + return self + + +class _TlsCtx: + check_hostname = False + verify_mode = None + + def wrap_socket(self, sock: object, server_hostname: str | None = None) -> _Ctx: + return _Ctx() + + +class _TlsFailCtx: + check_hostname = False + verify_mode = None + + def wrap_socket(self, sock: object, server_hostname: str | None = None) -> object: + raise OSError("handshake failed") diff --git a/tests/unit/providers/test_proxy.py b/tests/unit/providers/test_proxy.py index d49787f..3195f2c 100644 --- a/tests/unit/providers/test_proxy.py +++ b/tests/unit/providers/test_proxy.py @@ -7,36 +7,6 @@ from openrot.providers import proxy -class _FakeClient: - def __init__(self, response: object = None, error: object = None) -> None: - self._response = response - self._error = error - - def get(self, *a: object, **k: object) -> object: - if self._error: - raise self._error - return self._response - - def __enter__(self) -> Self: - return self - - def __exit__(self, *a: object, **k: object) -> bool: - return False - - -class _Response: - status_code = 204 - text = "" - - def raise_for_status(self) -> None: - pass - - -class _BadResponse: - status_code = 503 - text = "" - - def test_parse_proxy() -> None: assert proxy.parse_proxy("http://1.2.3.4:8080") == ("http", "1.2.3.4", 8080) assert proxy.parse_proxy("socks5://host.example:1080") == ( @@ -151,3 +121,33 @@ def fake_check_proxy( def test_check_proxy_node_dead_when_unparseable() -> None: node = Node(id="n", raw="http://no-port", protocol=NodeProtocol.HTTP) assert proxy.check_proxy_node(node, Config()) == (False, None) + + +class _FakeClient: + def __init__(self, response: object = None, error: object = None) -> None: + self._response = response + self._error = error + + def get(self, *a: object, **k: object) -> object: + if self._error: + raise self._error + return self._response + + def __enter__(self) -> Self: + return self + + def __exit__(self, *a: object, **k: object) -> bool: + return False + + +class _Response: + status_code = 204 + text = "" + + def raise_for_status(self) -> None: + pass + + +class _BadResponse: + status_code = 503 + text = "" diff --git a/tests/unit/providers/test_warp.py b/tests/unit/providers/test_warp.py index a7dded6..1e75ffe 100644 --- a/tests/unit/providers/test_warp.py +++ b/tests/unit/providers/test_warp.py @@ -3,12 +3,6 @@ from openrot.providers import warp as w -class _NoSleep: - @staticmethod - def sleep(seconds: float) -> None: - pass - - def _patch(monkeypatch: pytest.MonkeyPatch, status_values: list[w.WarpStatus]) -> None: calls = {"n": 0} @@ -218,3 +212,9 @@ def json(self) -> dict[str, str]: monkeypatch.setattr(w.httpx.Client, "get", lambda self, url: _Resp()) assert w.current_ip() is None + + +class _NoSleep: + @staticmethod + def sleep(seconds: float) -> None: + pass From a8679d93a0e8c2a2b9687936949814348091a7a9 Mon Sep 17 00:00:00 2001 From: Daniil Pavlovich Date: Sun, 20 Sep 2026 02:57:08 +0300 Subject: [PATCH 21/22] fix(bridge): :bug: annotate stub tools to satisfy mypy --- openrot/core/bridge.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openrot/core/bridge.py b/openrot/core/bridge.py index fce8983..73f203f 100644 --- a/openrot/core/bridge.py +++ b/openrot/core/bridge.py @@ -28,6 +28,7 @@ import time from dataclasses import dataclass from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from typing import Any from urllib.parse import urljoin import httpx @@ -76,7 +77,7 @@ class _Request: # Stub tools injected into bodies that carry no (or too few) tools: the free # tier gateway requires a ``tools`` array with at least two elements and # validates the tool names against real opencode tools (bash, read, ...). -_STUB_TOOLS = [ +_STUB_TOOLS: list[dict[str, Any]] = [ { "type": "function", "function": { From da69f359b14372077bbbc79b30fcdd3281e71eae Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 1 Oct 2026 03:50:10 +0000 Subject: [PATCH 22/22] build(deps): bump the python-deps group across 1 directory with 3 updates Bumps the python-deps group with 3 updates in the / directory: [ruff](https://github.com/astral-sh/ruff), [pyinstaller](https://github.com/pyinstaller/pyinstaller) and [pydantic](https://github.com/pydantic/pydantic). Updates `ruff` from 0.16.5 to 0.16.9 - [Release notes](https://github.com/astral-sh/ruff/releases) - [Changelog](https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md) - [Commits](https://github.com/astral-sh/ruff/compare/0.16.5...0.16.9) Updates `pyinstaller` from 6.22.2 to 6.22.3 - [Release notes](https://github.com/pyinstaller/pyinstaller/releases) - [Changelog](https://github.com/pyinstaller/pyinstaller/blob/develop/doc/CHANGES.rst) - [Commits](https://github.com/pyinstaller/pyinstaller/compare/v6.22.2...v6.22.3) Updates `pydantic` from 2.13.4 to 2.13.5 - [Release notes](https://github.com/pydantic/pydantic/releases) - [Changelog](https://github.com/pydantic/pydantic/blob/v2.13.5/HISTORY.md) - [Commits](https://github.com/pydantic/pydantic/compare/v2.13.4...v2.13.5) --- updated-dependencies: - dependency-name: ruff dependency-version: 0.16.9 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: python-deps - dependency-name: pyinstaller dependency-version: 6.22.3 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: python-deps - dependency-name: pydantic dependency-version: 2.13.5 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: python-deps ... Signed-off-by: dependabot[bot] --- poetry.lock | 320 ++++++++++++++++++++++++------------------------- pyproject.toml | 4 +- 2 files changed, 162 insertions(+), 162 deletions(-) diff --git a/poetry.lock b/poetry.lock index c794661..0dc069e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.3.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.5.1 and should not be changed by hand. [[package]] name = "altgraph" @@ -832,19 +832,19 @@ virtualenv = ">=20.10.0" [[package]] name = "pydantic" -version = "2.13.4" +version = "2.13.5" description = "Data validation using Python type hints" optional = false python-versions = ">=3.9" groups = ["main"] files = [ - {file = "pydantic-2.13.4-py3-none-any.whl", hash = "sha256:45a282cde31d808236fd7ea9d919b128653c8b38b393d1c4ab335c62924d9aba"}, - {file = "pydantic-2.13.4.tar.gz", hash = "sha256:c40756b57adaa8b1efeeced5c196f3f3b7c435f90e84ea7f443901bec8099ef6"}, + {file = "pydantic-2.13.5-py3-none-any.whl", hash = "sha256:346a034f080da3755d8e9cb5e00e8b07de1d39e4f6e2c87d8ab7cafa0b269a73"}, + {file = "pydantic-2.13.5.tar.gz", hash = "sha256:51a9c5f7b2f8e636f04c6cada605d9b6a3bf1348fdf945a3d8869b19bba0ee08"}, ] [package.dependencies] annotated-types = ">=0.6.0" -pydantic-core = "2.46.4" +pydantic-core = "2.46.5" typing-extensions = ">=4.14.1" typing-inspection = ">=0.4.2" @@ -854,132 +854,132 @@ timezone = ["tzdata ; python_version >= \"3.9\" and platform_system == \"Windows [[package]] name = "pydantic-core" -version = "2.46.4" +version = "2.46.5" description = "Core functionality for Pydantic validation and serialization" optional = false python-versions = ">=3.9" groups = ["main"] files = [ - {file = "pydantic_core-2.46.4-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:a396dcc17e5a0b164dbe026896245a4fa9ff402edca1dff0be3d53a517f74de4"}, - {file = "pydantic_core-2.46.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:da4b951fe36dc7c3a1ccb4e3cd1747c3542b8c9ceede8fc86cae054e764485f5"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bb63e0198ca18aad131c089b9204c23079c3afa95487e561f4c522d519e55aba"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f47286a97f0bc9b8859519809077b91b2cefe4ae47fcbf5e466a009c1c5d742b"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:905a0ed8ea6f2d61c1738835f99b699348d7857379083e5fc497fa0c967a407c"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ea793e075b70290d89d8142074262885d3f7da19634845135751bd6344f73b50"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:395aebd9183f9d112f569aeb5b2214d1a10a33bec8456447f7fbdfa51d38d4cd"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_31_riscv64.whl", hash = "sha256:b078afbc25f3a1436c7a1d2cd3e322497ee99615ba97c563566fdf46aff1ee01"}, - {file = "pydantic_core-2.46.4-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:f747929cf940cddb5b3668a390056ddd5ba2e5010615ea2dcf4f9c4f3ab8791d"}, - {file = "pydantic_core-2.46.4-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:daa27d92c36f24388fe3ad306b174781c747627f134452e4f128ea00ce1fe8c4"}, - {file = "pydantic_core-2.46.4-cp310-cp310-musllinux_1_1_armv7l.whl", hash = "sha256:19e51f073cd3df251856a8a4189fbdf1de4012c3ebacfb1884f94f1eb406079f"}, - {file = "pydantic_core-2.46.4-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:c1747f85cee84c26985853c6f3d9bd3e75da5212912443fa111c113b9c246f39"}, - {file = "pydantic_core-2.46.4-cp310-cp310-win32.whl", hash = "sha256:2f84c03c8607173d16b5a854ec68a2f9079ae03237a54fb506d13af47e1d018d"}, - {file = "pydantic_core-2.46.4-cp310-cp310-win_amd64.whl", hash = "sha256:8358a950c8909158e3df31538a7e4edc2d7265a7c54b47f0864d9e5bae9dcebf"}, - {file = "pydantic_core-2.46.4-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:0e96592440881c74a213e5ad528e2b24d3d4f940de2766bed9010ab1d9e51594"}, - {file = "pydantic_core-2.46.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:e0d65b8c354be7fb5f720c3caa8bc940bc2d20ce749c8e06135f07f8ed95dd7c"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7bfb192b3f4b9e8a89b6277b6ce787564f62cfd272055f6e685726b111dc7826"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9037063db01f09b09e237c282b6792bd4da634b5402c4e7f0c61effed7701a04"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:fc010ab034c8c7452522748bf937df58020d256ccae0874463d1f4d01758af8e"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:8c5dac79fa1614d1e06ca695109c6105923bd9c7d1d6c918d4e637b7e6b32fd3"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f9fa868638bf362d3d138ea55829cefb3d5f4b0d7f142234382a15e2485dbec4"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_31_riscv64.whl", hash = "sha256:17299feefe090f2caa5b8e37222bb5f663e4935a8bfa6931d4102e5df1a9f398"}, - {file = "pydantic_core-2.46.4-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:4c63ebc82684aa89d9a3bcbd13d515b3be44250dc68dd3bd81526c1cb31286c3"}, - {file = "pydantic_core-2.46.4-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:aaa2a54443eff1950ba5ddc6b6ccda0d9c84a364276a62f969bdf2a390650848"}, - {file = "pydantic_core-2.46.4-cp311-cp311-musllinux_1_1_armv7l.whl", hash = "sha256:18e5ceec2ab67e6d5f1a9085e5a24c9c4e2ac4545730bfe668680bca05e555f3"}, - {file = "pydantic_core-2.46.4-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:a0f62d0a58f4e7da165457e995725421e0064f2255d8eccebc49f41bbc23b109"}, - {file = "pydantic_core-2.46.4-cp311-cp311-win32.whl", hash = "sha256:041bde0a48fd37cf71cab1c9d56d3e8625a3793fef1f7dd232b3ff37e978ecda"}, - {file = "pydantic_core-2.46.4-cp311-cp311-win_amd64.whl", hash = "sha256:6f2eeda33a839975441c86a4119e1383c50b47faf0cbb5176985565c6bb02c33"}, - {file = "pydantic_core-2.46.4-cp311-cp311-win_arm64.whl", hash = "sha256:14f4c5d6db102bd796a627bbb3a17b4cf4574b9ae861d8b7c9a9661c6dd3362d"}, - {file = "pydantic_core-2.46.4-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:3245406455a5d98187ec35530fd772b1d799b26667980872c8d4614991e2c4a2"}, - {file = "pydantic_core-2.46.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:962ccbab7b642487b1d8b7df90ef677e03134cf1fd8880bf698649b22a69371f"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8233f2947cf85404441fd7e0085f53b10c93e0ee78611099b5c7237e36aacbf7"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:3a233125ac121aa3ffba9a2b59edfc4a985a76092dc8279586ab4b71390875e7"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5b712b53160b79a5850310b912a5ef8e57e56947c8ad690c227f5c9d7e561712"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9401557acd873c3a7f3eb9383edef8ac4968f9510e340f4808d427e75667e7b4"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:926c9541b14b12b1681dca8a0b75feb510b06c6341b70a8e500c2fdcff837cce"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:56cb4851bcaf3d117eddcef4fe66afd750a50274b0da8e22be256d10e5611987"}, - {file = "pydantic_core-2.46.4-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c68fcd102d71ea85c5b2dfac3f4f8476eff42a9e078fd5faefff6d145063536b"}, - {file = "pydantic_core-2.46.4-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:b2f69dec1725e79a012d920df1707de5caf7ed5e08f3be4435e25803efc47458"}, - {file = "pydantic_core-2.46.4-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:8d0820e8192167f80d88d64038e609c31452eeca865b4e1d9950a27a4609b00b"}, - {file = "pydantic_core-2.46.4-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:fbdb89b3e1c94a30cc5edfce477c6e6a5dc4d8f84665b455c27582f211a1c72c"}, - {file = "pydantic_core-2.46.4-cp312-cp312-win32.whl", hash = "sha256:9aa768456404a8bf48a4406685ac2bec8e72b62c69313734fa3b73cf33b3a894"}, - {file = "pydantic_core-2.46.4-cp312-cp312-win_amd64.whl", hash = "sha256:e9c26f834c65f5752f3f06cb08cb86a913ceb7274d0db6e267808a708b46bc89"}, - {file = "pydantic_core-2.46.4-cp312-cp312-win_arm64.whl", hash = "sha256:4fc73cb559bdb54b1134a706a2802a4cddd27a0633f5abb7e53056268751ac6a"}, - {file = "pydantic_core-2.46.4-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:5d5902252db0d3cedf8d4a1bc68f70eeb430f7e4c7104c8c476753519b423008"}, - {file = "pydantic_core-2.46.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:c94f0688e7b8d0a67abf40e57a7eaaecd17cc9586706a31b76c031f63df052b4"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f027324c56cd5406ca49c124b0db10e56c69064fec039acc571c29020cc87c76"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e739fee756ba1010f8bcccb534252e85a35fe45ae92c295a06059ce58b74ccd3"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9d56801be94b86a9da183e5f3766e6310752b99ff647e38b09a9500d88e46e76"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2412e734dcb48da14d4e4006b82b46b74f2518b8a26ee7e58c6844a6cd6d03c4"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9551187363ffc0de2a00b2e47c25aeaeb1020b69b668762966df15fc5659dd5a"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:0186750b482eefa11d7f435892b09c5c606193ef3375bcf94aa00ae6bfb66262"}, - {file = "pydantic_core-2.46.4-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:5855698a4856556d86e8e6cd8434bc3ac0314ee8e12089ae0e143f64c6256e4e"}, - {file = "pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:cbaf13819775b7f769bf4a1f066cb6df7a28d4480081a589828ef190226881cd"}, - {file = "pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:633147d34cf4550417f12e2b1a0383973bdf5cdfde212cb09e9a581cf10820be"}, - {file = "pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:82cf5301172168103724d49a1444d3378cb20cdee30b116a1bd6031236298a5d"}, - {file = "pydantic_core-2.46.4-cp313-cp313-win32.whl", hash = "sha256:9fa8ae11da9e2b3126c6426f147e0fba88d96d65921799bb30c6abd1cb2c97fb"}, - {file = "pydantic_core-2.46.4-cp313-cp313-win_amd64.whl", hash = "sha256:6b3ace8194b0e5204818c92802dcdca7fc6d88aabbb799d7c795540d9cd6d292"}, - {file = "pydantic_core-2.46.4-cp313-cp313-win_arm64.whl", hash = "sha256:184c081504d17f1c1066e430e117142b2c77d9448a97f7b65c6ac9fd9aee238d"}, - {file = "pydantic_core-2.46.4-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:428e04521a40150c85216fc8b85e8d39fece235a9cf5e383761238c7fa9b96fb"}, - {file = "pydantic_core-2.46.4-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:23ace664830ee0bfe014a0c7bc248b1f7f25ed7ad103852c317624a1083af462"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ce5c1d2a8b27468f433ca974829c44060b8097eedc39933e3c206a90ee49c4a9"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7283d57845ecf5a163403eb0702dfc220cc4fbdd18919cb5ccea4f95ee1cdab4"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8daafc69c93ee8a0204506a3b6b30f586ef54028f52aeeeb5c4cfc5184fd5914"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:cd2213145bcc2ba85884d0ac63d222fece9209678f77b9b4d76f054c561adb28"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7a5f930472650a82629163023e630d160863fce524c616f4e5186e5de9d9a49b"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_31_riscv64.whl", hash = "sha256:c1b3f518abeca3aa13c712fd202306e145abf59a18b094a6bafb2d2bbf59192c"}, - {file = "pydantic_core-2.46.4-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:1a7dd0b3ee80d90150e3495a3a13ac34dbcbfd4f012996a6a1d8900e91b5c0fb"}, - {file = "pydantic_core-2.46.4-cp314-cp314-musllinux_1_1_aarch64.whl", hash = "sha256:3fb702cd90b0446a3a1c5e470bfa0dd23c0233b676a9099ddcc964fa6ca13898"}, - {file = "pydantic_core-2.46.4-cp314-cp314-musllinux_1_1_armv7l.whl", hash = "sha256:b8458003118a712e66286df6a707db01c52c0f52f7db8e4a38f0da1d3b94fc4e"}, - {file = "pydantic_core-2.46.4-cp314-cp314-musllinux_1_1_x86_64.whl", hash = "sha256:372429a130e469c9cd698925ce5fc50940b7a1336b0d82038e63d5bbc4edc519"}, - {file = "pydantic_core-2.46.4-cp314-cp314-win32.whl", hash = "sha256:85bb3611ff1802f3ee7fdd7dbff26b56f343fb432d57a4728fdd49b6ef35e2f4"}, - {file = "pydantic_core-2.46.4-cp314-cp314-win_amd64.whl", hash = "sha256:811ff8e9c313ab425368bcbb36e5c4ebd7108c2bbf4e4089cfbb0b01eff63fac"}, - {file = "pydantic_core-2.46.4-cp314-cp314-win_arm64.whl", hash = "sha256:bfec22eab3c8cc2ceec0248aec886624116dc079afa027ecc8ad4a7e62010f8a"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:af8244b2bef6aaad6d92cda81372de7f8c8d36c9f0c3ea36e827c60e7d9467a0"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:5a4330cdbc57162e4b3aa303f588ba752257694c9c9be3e7ebb11b4aca659b5d"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:29c61fc04a3d840155ff08e475a04809278972fe6aef51e2720554e96367e34b"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c50f2528cf200c5eed56faf3f4e22fcd5f38c157a8b78576e6ba3168ec35f000"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0cbe8b01f948de4286c74cdd6c667aceb38f5c1e26f0693b3983d9d74887c65e"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:617d7e2ca7dcb8c5cf6bcb8c59b8832c94b36196bbf1cbd1bfb56ed341905edd"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7027560ee92211647d0d34e3f7cd6f50da56399d26a9c8ad0da286d3869a53f3"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_31_riscv64.whl", hash = "sha256:f99626688942fb746e545232e7726926f3be91b5975f8b55327665fafda991c7"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:fc3e9034a63de20e15e8ade85358bc6efc614008cab72898b4b4952bea0509ff"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-musllinux_1_1_aarch64.whl", hash = "sha256:97e7cf2be5c77b7d1a9713a05605d49460d02c6078d38d8bef3cbe323c548424"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-musllinux_1_1_armv7l.whl", hash = "sha256:3bf92c5d0e00fefaab325a4d27828fe6b6e2a21848686b5b60d2d9eeb09d76c6"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:3ecbc122d18468d06ca279dc26a8c2e2d5acb10943bb35e36ae92096dc3b5565"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-win32.whl", hash = "sha256:e846ae7835bf0703ae43f534ab79a867146dadd59dc9ca5c8b53d5c8f7c9ef02"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-win_amd64.whl", hash = "sha256:2108ba5c1c1eca18030634489dc544844144ee36357f2f9f780b93e7ddbb44b5"}, - {file = "pydantic_core-2.46.4-cp314-cp314t-win_arm64.whl", hash = "sha256:4fcbe087dbc2068af7eda3aa87634eba216dbda64d1ae73c8684b621d33f6596"}, - {file = "pydantic_core-2.46.4-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:fd8b3d9fd264be37976686c7f65cd52a83f5e84f4bfd2adf9c1d469676bbb6ae"}, - {file = "pydantic_core-2.46.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:9f444c499b3eefd3a92e348059471ea0c3a6e303d9c1cec09fa748fd9f895201"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3447661d99f75a3683a4cf5c87da72f2161964611864dbbeac7fbb118bb4bfc0"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:8b9bab013d1c7a79d3501ff86d0bc9c31bf587db4551677b96bec07df78c6b15"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d995260fdf4e1db774581b4900e0f832abe3c7c84996726bbc161b19c8f29e76"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:f13a646d65d09fbf1bc6b3a9635d30095c8e7e5cc419ff35ecc563c5fd04cd49"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:432c179df7874eeb73307aad2df0755e1ae0efa61ff0ea89b93e194411ae3928"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_31_riscv64.whl", hash = "sha256:e68b7a074f65a2fd746c52a7ce6142ab7006074ac269ace0c25cd8ba171f8066"}, - {file = "pydantic_core-2.46.4-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:4a05d69cba51d852c5c3e92758653245a50c0b646ced0cf05bd793ed592839d6"}, - {file = "pydantic_core-2.46.4-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:228ee9bae8bef5b1e97ec58302f80357c37199e0d0a99174e138d28e6957b9d9"}, - {file = "pydantic_core-2.46.4-cp39-cp39-musllinux_1_1_armv7l.whl", hash = "sha256:10e17cbb10a330363733efc4d7c4d0dd827ac0909b8f6a6542298fed1ea62f29"}, - {file = "pydantic_core-2.46.4-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:91a06d2e259ecfbd8c901d70c3c507900458498142b3026a296b7de4d1322cc9"}, - {file = "pydantic_core-2.46.4-cp39-cp39-win32.whl", hash = "sha256:d80ee3d731373b24cebbc10d689ca4ee1875caf0d5703a245db18efd4dd37fc1"}, - {file = "pydantic_core-2.46.4-cp39-cp39-win_amd64.whl", hash = "sha256:3be77f45df024d789a672ae34f8b06fb346c4f9f46ea714956660ea4862e89ac"}, - {file = "pydantic_core-2.46.4-graalpy311-graalpy242_311_native-macosx_10_12_x86_64.whl", hash = "sha256:14d4edf427bdcf950a8a02d7cb44a08614388dd6e1bdcbf4f67504fa7887da9c"}, - {file = "pydantic_core-2.46.4-graalpy311-graalpy242_311_native-macosx_11_0_arm64.whl", hash = "sha256:0ce40cd7b21210e99342afafbd4d0f76d784eb5b1d60f3bdc566be4983c6c73b"}, - {file = "pydantic_core-2.46.4-graalpy311-graalpy242_311_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:90884113d8b48f760e9587002789ddd741e76ab9f89518cd1e43b1f1a52ec44b"}, - {file = "pydantic_core-2.46.4-graalpy311-graalpy242_311_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:66ce7632c22d837c95301830e111ad0128a32b8207533b60896a96c4915192ea"}, - {file = "pydantic_core-2.46.4-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:1d8ba486450b14f3b1d63bc521d410ec7565e52f887b9fb671791886436a42f7"}, - {file = "pydantic_core-2.46.4-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:3009f12e4e90b7f88b4f9adb1b0c4a3d58fe7820f3238c190047209d148026df"}, - {file = "pydantic_core-2.46.4-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ad785e92e6dc634c21555edc8bd6b64957ab844541bcb96a1366c202951ae526"}, - {file = "pydantic_core-2.46.4-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:00c603d540afdd6b80eb39f078f33ebd46211f02f33e34a32d9f053bba711de0"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:0c563b08bca408dc7f65f700633d8442fffb2421fc47b8101377e9fd65051ff0"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:db06ffe51636ffe9ca531fe9023dd64bdd794be8754cb5df57c5498ae5b518a7"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:133878133d271ade3d41d1bfb2a45ec38dbdbda40bc065921c6b04e4630127e2"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:9bc519fbf2b7578398853d815009ae5e4d4603d12f4e3f91da8c06852d3da3e9"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:c7a7bd4e39e8e4c12c39cd480356842b6a8a06e41b23a55a5e3e191718838ddf"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:d396ec2b979760aaf3218e76c24e65bd0aca24983298653b3a9d7a45f9e47b30"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:86e1a4418c6cd97d60c95c71164158eaf7324fae7b0923264016baa993eba6fc"}, - {file = "pydantic_core-2.46.4-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:d51026d73fcfd93610abc7b27789c26b313920fcfb20e27462d74a7f8b06e983"}, - {file = "pydantic_core-2.46.4.tar.gz", hash = "sha256:62f875393d7f270851f20523dd2e29f082bcc82292d66db2b64ea71f64b6e1c1"}, + {file = "pydantic_core-2.46.5-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:657b40d6240c0a7b6a64b30f22d1e3aa631c7e846c621b0c0f6d1d75e2e15ea6"}, + {file = "pydantic_core-2.46.5-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ecb42011e12ee19cafbc312887cbf3546959fe02fbad44f272d4be5baa997615"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4dedce55295becb61921e386b99d4f2706045306e7fa52249a33004c837379fb"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9f47b8a949e60f027f0aa0a6f6c7b7e9c55cbf4380d10b344e282fa4e7ab1e1b"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:200aa3dc9f8d54f0754f43247c0bad0999fdcfbfd2488384dd44f37279271fe6"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6d30e1a4f138b8951063e9a394752a9179b51da288ffa507b1e659222f4c1793"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:850a08d167dde16db8702c274f320c7be9d7da6f6dff2b58b18f9e815bd94f5b"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_31_riscv64.whl", hash = "sha256:c3471e5c4a949c26ec00a77f01df59096aa9495877de76fd60a980f8ee6be461"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:3a3e26b6a8274211bddee2d0e4d0d42778f17a34510f49d2ec44b58abfc41736"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:fc5d783bd4a2387e97b8a2d5ec781cfb92b3d893bf82370548e99db5915935d3"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_armv7l.whl", hash = "sha256:356c8368cbc321050b169595683a2e1d63413b1e0e2868b330af9fc14c616d3f"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:eb7d8d0e5886a89a55d2eef490e272fa965a9d57c6b29a5b5088a7997ec2cad1"}, + {file = "pydantic_core-2.46.5-cp310-cp310-win32.whl", hash = "sha256:4d44cf99ddebf875f9b68cc267aa684c99b7b44fe63ee1cac4ec163807290069"}, + {file = "pydantic_core-2.46.5-cp310-cp310-win_amd64.whl", hash = "sha256:1e5aad1220a1192c42341c8fd4a8686657e73ab2a920c970bdc4de334fe3193d"}, + {file = "pydantic_core-2.46.5-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:a1dee1b804ff4d11c663636cf15d2ea47e9f79cd56c033fb1cbf08924842a48f"}, + {file = "pydantic_core-2.46.5-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:d625a186a65201c23a9e3b8ed9c47e90a026e03256608cc91851c6709096844f"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4f8507560a9284e1370bb048ed4282012fbef4e8d109875b95e884d228552061"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:5f93c5fe914d75fbec9a49209b00da5f08e9e467d69da2b1510c81940cfd10be"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:aca6c767f552b21b10f774aeac128e828eafb796adfa1b666a18bf6321453c3a"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:701b2e04b560eeb4bddf7a25ab8ca476176e34fdbd9a0e18196f0d12d4685f0b"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:49776eab08766a08dfff7012f8b422dcd7e25e43b316eedf0477c24fcfa84b7c"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_31_riscv64.whl", hash = "sha256:a2468d93d181667a7abd66e1b64bb9f76f361b0fef8faddf687456453576f5ee"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:53feb344243bb9510a9dec7bf3cf1b64d88a98af5dc7872a5160465f8b198c8e"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:cd5214352ae68f3b5e9af7768bdc5253695ee069675db3480518420b3be881f2"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_armv7l.whl", hash = "sha256:9432f3598db432cb51c5b37fdbf29a60fcccc79e30d37a05022776a6bc4ab689"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:8feeac04b5794e513e710af2f9c87d49f31a6dc47967bb264a1fed61a8989bec"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win32.whl", hash = "sha256:892a881d5f68c2b9ea304b7a6c2c60d9343df578a311b0f86b94bc8f1ffe8129"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win_amd64.whl", hash = "sha256:40375c2d05acec10323e45dfe2077ac44bc74659008614af5069034e2cfc781c"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win_arm64.whl", hash = "sha256:28a6a556cd3b6066bea827857f9d9cce027c96f776e512f544a581f9e42161f8"}, + {file = "pydantic_core-2.46.5-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:b9fe6fb92520e3fd61f2e49000b6911b188824f089b75973ea06d6267f0b476d"}, + {file = "pydantic_core-2.46.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a39ac25a9a2fa4072efdb429833c4a4c8009a51ff9eea3eeae131713cd27991e"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4fdc8b93a41521988916eeaa271173fcca7fa0803d62f87675aac8dcec1c8e29"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:b98134087d9de723658d17a42c7d0da8d6e2ef08015dee7dc93889047315f5e4"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e652ab17569c94bff5475520f907b7148b8c24036a8ebbe5cf7cf7493d28579a"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d925f3d9afd05a8c0fb3a1031463a8d59ebe5e2afad297e29c78be19e13b4e62"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0fc5be0abd4a407e200d844b404e33639a554e7bd0d448e7b9ae181be4789ac2"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:816ff0a6550ffc06c098ccd2e0698600f9aa7da192a79eaa6f9af504a35db869"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c7ea57fc63aa7da93a1bd2d644e6577befae10c52c4e36377635eea1056a74f5"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:efd62a42486f1bda5d24cb4f63d15a3c7768375fe83d36f9417b4ad7a2fb20b3"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:2bc9419666990c06d7397831f2126a1ecc3594aaa3ff7de5bf2d066802f4e07b"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:18a09e1e1011b462f2e32774f25859ef1223d5c2b0546a633cf56654710721e0"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win32.whl", hash = "sha256:5cb482e9e84c851f4e623fe4acc1ced89168cf1fe18f7089db4548c8f5bbb65b"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win_amd64.whl", hash = "sha256:5e81740c09e310f5aa5cbd3e434a01c154d4bef93241c7877b39f211d2b78ba8"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win_arm64.whl", hash = "sha256:f7b0ec93a2893de856652154d73b7ba622f26fa97726487dcac373de5f4c6084"}, + {file = "pydantic_core-2.46.5-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:b7ca9034437b6022f941f4857459562ee00a560b97e7cce8a0ec5a74fc6766e0"}, + {file = "pydantic_core-2.46.5-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f332f0e72a5a0400141f830744e141bf9f97917878dbe968669e8a7fefea78ff"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:193375f3548919d3f0b60936ca113ada3e38f264f91b9b8e0508efaad57be931"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:79bdfa52f843137045b2d081cc05c120ba6665d29b7559c2c47690906f39279f"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:24922243639cbdac66c75fcb6fd6495a9cb52b213d62f9a0d16f0310b1ff8038"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c76fe65e607be28c7fd4d56fc3c42b1583aa058ce3408b7ad0fd540171d31f9f"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f7b393a8b3da82f5c1fc0751e6d01ac6c55b93c18226a60bdfba4a724efafd1"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:7ac031912d54f3d83ef3b3eb98dfabc1608802e2202263d25957eeed40b94761"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:837b396ca3d7b74091ca623f6cbd8351bd42d670a79c2683e79fb089f06a2de5"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:5ee239d575f80b08eca11f6e20f90c4c695de7825c67eefe6091fbf20dda648e"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:e80675d75ae2cd14372cb65cad5400d9347a3d3f6c13000183f22dfd027283ed"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:9c4b71f10dd532fb7a5cbc8f58707779e64f03a258c2bf8bfbaecfcd9970b519"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win32.whl", hash = "sha256:97bf8de4d541598c94a59344eeb988a94c08ff76b5723c41f6567ec18c7892ea"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win_amd64.whl", hash = "sha256:15f4a94963c95accac15b7b657bb177d3ad82bb90b0d0526d9a9b85079925db5"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win_arm64.whl", hash = "sha256:d22a945598fb91236b4dd793a6e42e4f3dd7740bb5aace5ebd7d4c08d13bb575"}, + {file = "pydantic_core-2.46.5-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:c1c43ad4339643d70ebb8124e1305a7dab423001eff58bb41a0f731adbc98355"}, + {file = "pydantic_core-2.46.5-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1a353f84de772f423b5ffb11d7ae352fbbef0f446f3c0b0af0f8236d7233606e"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5086029a57366b8cf81b130a43908738095c270c21a8d7f0e8bdfdb89718e2f3"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:46c25dda9d092a06c08db76ffe0a197107904d0dfac653f7d5306bbcd6d6119c"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:37ea7b83c935e5b0d68c9449b82651accf78a10828b2c02b2f2d9e9496446c21"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e64e88d5585bea9ce95861079de72006c7fa6d3df4e3a3b65ba31eb979c15c9f"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:54d510bac3ee52247af28ed4bb18a1e799f040ac60fd2bf5ccd4c92f1fbe786f"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_31_riscv64.whl", hash = "sha256:a2a5e1d0ff29adddc9f6d6821a66302e4493f8ca898b715b6b1182c2c201ea0a"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:03b9666e41e35d8909852ba191a0607520f81b74eaf12ccf8737005dbb313821"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_aarch64.whl", hash = "sha256:a91c17edf6eea2402cb5457b4c89e99bc5ed1004aa34c4adf1d4258c1a5c22c2"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_armv7l.whl", hash = "sha256:b49924c73a235e969511bf2aabdff3beebf9820931f646c80274d5d780010c47"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_x86_64.whl", hash = "sha256:2cbd9a5eff05e51c447c34dfa4632145b26b09120cf04bd0c871e44c1a5e1c9a"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win32.whl", hash = "sha256:2d5d76654becf5efd62c9e51c3756c67b49498b0c9a40884934c40807adbd074"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win_amd64.whl", hash = "sha256:fa10ef4112775900e7a0661068635eb67b2ab824fbde764de6e0e21982a93db0"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win_arm64.whl", hash = "sha256:045ab3b6d308439e32b81cc173bba5b9018bc6ed896afd0c65b3b009b1699af5"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:8816f3d218beb4b787de5c9759c259b8fa61f9dec42dc7811f320a33771778b7"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:bce57638e08ac148e5778cce7feb968307a727d66f8e2274a543d0cf0c9ad6a3"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:976e1128455aa595ea04c79ccfedff1aaeab96ee013fcc916bed120c4f0ad94f"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e7b891faeedeafba41b2983e5001a81b6a915b69544c7e7570d1989ce1c36ac7"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5f194189415698233dd1114a093a9b56e61e2c57e11b469be3b0506f46f0771c"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:82a36973cf8a2ef5406f4fe2edbf8ed0c99629535d959e0b100c76a32535a111"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cdbb78909f52b981d3b2d56b97328d71eb0b974c36bd77c920123a7ebb192829"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_31_riscv64.whl", hash = "sha256:52e24eacdb536cade636aa90fb851835222becff8484b7001fdc78cb0290f2aa"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:37ae34309d7bd8c0d61ab839668058f2a7962ea1fc51d105d2db228fe0618034"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_aarch64.whl", hash = "sha256:0cdbada856a1c69a7624a64d3d9aefe79300bd6ef827b43a4f265010b9b55184"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_armv7l.whl", hash = "sha256:545f26c504b27c3758439a5e6d9349931f0a04f855668d5fe323c89e82300a38"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:ff218293c9c806138dca139765e3b067621be52bcd93cdc14c7711be7ddc90a9"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win32.whl", hash = "sha256:97cf3eb53a8cccacf9d46686a0926186c9bfb5574f2ed66d3639d5fe117cd3a9"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win_amd64.whl", hash = "sha256:d2f9fc07a8042a8f95925b35c4f04f469707c981fc33245b6ca187cf5d2dd290"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win_arm64.whl", hash = "sha256:acf8a67ba51f4ca9ddbd0e6b3000a65ac51ab734661778b3e7ba64d99a710f2f"}, + {file = "pydantic_core-2.46.5-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:c583b927a8838dab890706a6fa7573fbb8b70e24000ef9f7238e2d6f6435a5ed"}, + {file = "pydantic_core-2.46.5-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:cdc8b74ecc48c0cb1e9607a05ec4e9e88db60a19ffcc9a1d5f9088ede40c8dc0"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8b10e3e8fd7ddc2bd915848a2768e44c15b22936f1cc54c462ad1164deb02655"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f077d0b97ab11fa7dcc633fca53515f290bca8a8a633e966d5b6d1879d9ed01a"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7b0fc826b16c55e561e5d2a0c5c77b051ba1d92808118c4e4b5390f5e0cf191d"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ef3fbbf161dc9351a2fe0422e51b129f9e97e42385bd0320b309c15f7d287dd8"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:978e7b97d4824b5be09c69fb70507cbde3b0323fc147332ca40a94d9a6a0ebbf"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_31_riscv64.whl", hash = "sha256:9b68938dd5b0c783d88ff8e2dcc69451b5eb936fe212d516b21b9d5567f6d464"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:771cf63ae0b1b50dd22e5f3e3549fab5f3f4ff1635d352a9e1a97fe01c7b2e64"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:7c6be839a5a8312626b32029a415644a0846b420bc8b52b95b28cd92da162168"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_armv7l.whl", hash = "sha256:895395f8918627b04efb1ad2a4cf605387143300ba03304cd1dfa6d03f5e095e"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:fc8515076c11f3cfdf4fb142dcca0fe384b1230a3b5415458ac84f3e0903ec13"}, + {file = "pydantic_core-2.46.5-cp39-cp39-win32.whl", hash = "sha256:3d2652072b2d774947ba5cf78a9e59644ac62ee572daf6dd2e1dfe905e15b2b7"}, + {file = "pydantic_core-2.46.5-cp39-cp39-win_amd64.whl", hash = "sha256:3aa166e99c4f2985407fb8714aebede877ecb5455cf321b606adca926d30d5a0"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_10_12_x86_64.whl", hash = "sha256:c14ad3bdc85ee7f318742c457ca3968a92126d144b15721c759033bfb06296c2"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_11_0_arm64.whl", hash = "sha256:0bddb4020d8f04175865ccd17eff3040874fc11fb593f424edb452653b4b947c"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2471fd51c61c610e1dcf7de44d7299283661654d11264ab4802b303368d69c47"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b10ec717381bdbfafef34607824db4c91de69ff085e4fca3b2af91b4fa17e68a"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:013d6f3483d81e02e7c328831808f336c8596ee33b4bd4026b9ffb1e960b8942"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:e9c134bb666dd54b778b9fc0d2b50cbb7f979b9e3716f26a88c9ab3b6fc1dd0f"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:347ec774390c87326a2e4929d58d3f7e8763a104d5d35f4cd595a4c952366433"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e24d8f05fa2d28513d94e877e9c75ad66175376209b3977f916e240e623193c"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:ab4b66edffb32d9e951efb3814bd104b8367a7501b81b955cacb5726d897389f"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:337639ba62a11acde6ef3aeb08c8ea755f8ef1fe5e513356c0f36a2b0d7568b0"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:413a717a410d0c817ef5b786a059415550b3794e1d0c2abffd9efb93a3d9f7b4"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:1e449def1945a462c464331254e5a44fca7c3b4f9aedf59ec2f50f8066dd8e25"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:a445486499897b88a7d6c310c88ed64dd37b1b59bfd7ae9107490bbb362f47d6"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:2d330aaba8621b1edcec8ae2c4050f63b84ccf6d98723a8f212e9684713abf0e"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:b6acfb46a814762367fb7ba0828b0a17d441b92ce249a0e007474c9072662dda"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:d0a24b40877af2de4950252be9d21eaf7fb07660f3c2cae1f56c6b599ada5266"}, + {file = "pydantic_core-2.46.5.tar.gz", hash = "sha256:10416c15b8839ecc4ef4d0885da76da6fd0f67333a0eb8aff6d93c4b8f2910fc"}, ] [package.dependencies] @@ -1002,25 +1002,25 @@ windows-terminal = ["colorama (>=0.4.6)"] [[package]] name = "pyinstaller" -version = "6.22.2" +version = "6.22.3" description = "PyInstaller bundles a Python application and all its dependencies into a single package." optional = false python-versions = "<3.16,>=3.8" groups = ["dev"] markers = "python_version < \"3.16\"" files = [ - {file = "pyinstaller-6.22.2-py3-none-macosx_10_13_universal2.whl", hash = "sha256:ebd1b1ca932d7cf25d7366ce691aaf79a5ff9425811ed7328b5116e4471b6d6d"}, - {file = "pyinstaller-6.22.2-py3-none-manylinux2014_aarch64.whl", hash = "sha256:f5ccb847451df4207bce18bf53a57b124c9bb4e7e4bad08c5ecc627bcf00b28c"}, - {file = "pyinstaller-6.22.2-py3-none-manylinux2014_i686.whl", hash = "sha256:becb47ad78272bede87acf2ed830d7545a8f65ab11d06a68fe2b99ba1afaac6d"}, - {file = "pyinstaller-6.22.2-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:06d7b3827a8049db4a2d47e3ec4ae2f69a1041577d8833b8f169745cde573ab4"}, - {file = "pyinstaller-6.22.2-py3-none-manylinux2014_s390x.whl", hash = "sha256:7bee432404eca5dc3ef37c36811c266561b03988f6d3e70ab0fa5352dd5de9e4"}, - {file = "pyinstaller-6.22.2-py3-none-manylinux2014_x86_64.whl", hash = "sha256:9622686ecc5d5fa492fe6cde29d47df9dd41138cff8177be9f901ca3260f2096"}, - {file = "pyinstaller-6.22.2-py3-none-musllinux_1_1_aarch64.whl", hash = "sha256:0260eaad6be3f6fbc1affffe6dc7b8e5b636dbca51b463224daba971610b6fc1"}, - {file = "pyinstaller-6.22.2-py3-none-musllinux_1_1_x86_64.whl", hash = "sha256:8c2c4b14caad38c1f3df8e9bf5276fc265e27fe8b4180cab4a37864288427bc0"}, - {file = "pyinstaller-6.22.2-py3-none-win32.whl", hash = "sha256:9a078877caa3920558a3242d0a7019fe5b825cff4b65ed380c7d1e1bd200ddd9"}, - {file = "pyinstaller-6.22.2-py3-none-win_amd64.whl", hash = "sha256:9b990fa6bbe143572f06644a984ad0d7aa2e2ccc6929d4916031343a5888e9a7"}, - {file = "pyinstaller-6.22.2-py3-none-win_arm64.whl", hash = "sha256:afb6f9a95d19b6dcd3a7decc40d9adb6ba9c4f8802ddd6c972dfb552953f384e"}, - {file = "pyinstaller-6.22.2.tar.gz", hash = "sha256:89b65a3ad07d9dd5832253e37bc45f31872d10d7f9d5c9fd0fdd6088a83829dd"}, + {file = "pyinstaller-6.22.3-py3-none-macosx_10_13_universal2.whl", hash = "sha256:052f4a1cd4f81092ccb7a18fe8ebc9ecf832a5917317e39fb3775af4e959b253"}, + {file = "pyinstaller-6.22.3-py3-none-manylinux2014_aarch64.whl", hash = "sha256:312da84b4b31ab9750066a823734c11b7c68757fdd67c9f038bf19c330a954e4"}, + {file = "pyinstaller-6.22.3-py3-none-manylinux2014_i686.whl", hash = "sha256:020d4fe0627f5c73dfbcfad1a6578ef354d5fe4bdccd9d8e5a615528f5a45d2b"}, + {file = "pyinstaller-6.22.3-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:9e4f7bd64b2103f14b4a6240fb9155e9fe7bc61391b4043bbddb7d3ebc8ab0fa"}, + {file = "pyinstaller-6.22.3-py3-none-manylinux2014_s390x.whl", hash = "sha256:f82d0fb89f21c3ed6f482ee16f341bc59e8a31dbabb9bae9ef04e53ade4dc0d9"}, + {file = "pyinstaller-6.22.3-py3-none-manylinux2014_x86_64.whl", hash = "sha256:451a4ae14b719365bf1a2f0a99dae7b3463060061c3a394c70d5264cfb439528"}, + {file = "pyinstaller-6.22.3-py3-none-musllinux_1_1_aarch64.whl", hash = "sha256:ea240b5dfa831ff673c97505eee0ec24670bccf71396c83109440348944c83d0"}, + {file = "pyinstaller-6.22.3-py3-none-musllinux_1_1_x86_64.whl", hash = "sha256:a2b6fc26601e8c2596c3d54981566934a3382e85d05d0fe91b55b6a8dcc6b282"}, + {file = "pyinstaller-6.22.3-py3-none-win32.whl", hash = "sha256:46a3e30a118341a1ec20667a5a95aa7e0aa8c4fa129893c0151b3eb3f801a722"}, + {file = "pyinstaller-6.22.3-py3-none-win_amd64.whl", hash = "sha256:500bd58c7bf7e584a8435adccbd763a0b918d5c12b08d74ff50fd79b2915458b"}, + {file = "pyinstaller-6.22.3-py3-none-win_arm64.whl", hash = "sha256:31df3cc4261e804a53d358b7897c293ec8b88b167a7cca06c78018085c855f97"}, + {file = "pyinstaller-6.22.3.tar.gz", hash = "sha256:05eb2f5615503e72939a7224d68b4aff572c6b0438ee4a17d0a4b481f399362d"}, ] [package.dependencies] @@ -1028,7 +1028,7 @@ altgraph = "*" macholib = {version = ">=1.8", markers = "sys_platform == \"darwin\""} packaging = ">=22.0" pefile = {version = ">=2022.5.30", markers = "sys_platform == \"win32\""} -pyinstaller-hooks-contrib = ">=2026.6" +pyinstaller-hooks-contrib = ">=2026.7" pywin32-ctypes = {version = ">=0.2.1", markers = "sys_platform == \"win32\""} setuptools = ">=42.0.0" @@ -1226,30 +1226,30 @@ jupyter = ["ipywidgets (>=7.5.1,<9)"] [[package]] name = "ruff" -version = "0.16.5" +version = "0.16.9" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" groups = ["dev"] files = [ - {file = "ruff-0.16.5-py3-none-linux_armv6l.whl", hash = "sha256:12e5f673e774c35fbb62f288809c7653b73445f8ecec6b6063fd6ea3521aa14b"}, - {file = "ruff-0.16.5-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:eda58a5802de40e7ed5b32b64e0b32539338cc6fcd2c78f61e3ad6a0d79f51c3"}, - {file = "ruff-0.16.5-py3-none-macosx_11_0_arm64.whl", hash = "sha256:c5ae9a7b9a8875131f40f8fe967cc86abf899779efd663cb7ce3d572d01da7eb"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7b719b0a1f4d59710d283ab2965f621684a108a9e41da622e3b23f0326cd0025"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:2298f2780ed1be0c5cb1361e32ab7b1467f3cce7dabe101d2210a314f2fe42e9"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:258f29035a2dd021e7861e631b227a5b3f14e50c1184c9a6a122c5f4576154d7"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:b9a4f0432966834019c74d1b7e5c51224305d7713f3d7faf3e7451f1a3be3cde"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b5eb3a8c3d0ade9cea42b591fd530368e8798380e30e0a308b85a5cf718f09ea"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef0f69e191a13a3c9816f63163c88790cb12cd157bbbb384e9c44745702ab105"}, - {file = "ruff-0.16.5-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:0eeab41fbea2c42f98dfb9822cdccda9d24ba38d49f6dc945b5c236d48f0ef29"}, - {file = "ruff-0.16.5-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:f0768e9df4300713fff30733c87575f68b6f1d8de41184e505b7fdd9c0c95eaf"}, - {file = "ruff-0.16.5-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:95cc70cdc7aa80c338de356279d2adbeb2de0f520b9ecd8aba75b94e95e02f91"}, - {file = "ruff-0.16.5-py3-none-musllinux_1_2_i686.whl", hash = "sha256:d185c8398ded1bfd91c0c2cb258346307571eccc473a8490af8c3977399c384a"}, - {file = "ruff-0.16.5-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:fb8e3a3c4c6a784150a7ced53b015f4b253fc2bf97a610886419ead64b4756ef"}, - {file = "ruff-0.16.5-py3-none-win32.whl", hash = "sha256:288b0a5f080492fe5635db849f9e2e84aa3cce7b7f0e955997d416c507c76a26"}, - {file = "ruff-0.16.5-py3-none-win_amd64.whl", hash = "sha256:ddc6385fb2137f616357ca03d6c74f4be987f80fed4008566b754f6032b8546f"}, - {file = "ruff-0.16.5-py3-none-win_arm64.whl", hash = "sha256:a64abe90968719b851bb7cedffaa8753fbdbdadab483089682db623f3edc587e"}, - {file = "ruff-0.16.5.tar.gz", hash = "sha256:1b88500f9ffbcab3dedb0082c9f9492e91ec3d618aac1236a3e0189938f7040b"}, + {file = "ruff-0.16.9-py3-none-linux_armv6l.whl", hash = "sha256:95e6f022090368ab3b824c36276839c53b2adf1a3f4c09fefc33dfc400f6da96"}, + {file = "ruff-0.16.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:a5f27be168556594a86d2f415db0cf43f5291917849318f873c7e2791f7a8c67"}, + {file = "ruff-0.16.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:1632eb1d6197f33bd00b1acbc5b71009e89a8895c158e2d2b03a834fac964ab6"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b3f951b14d865d5952c89d40a5ca07e87abe24fa5453299878411e127748fb1c"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:447fc07e1573afff7cb02803462b12b6c8ece7cf10e2cd78565fa6d7a1c0bf8d"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8a3e039a6a40ed976c491722b60e0ae4a4aa1a86057f540ee7a37a5d19ae9120"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4684dded7db60aa57cb118fa158630f5feade4af5782903b6053484bdf9bd129"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d29c934357e45642fda2f34c0b1f4025b4a6c01e15e4bf0016879d60078a142c"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a21713e629d3e5bdb2f5c2def1cc7f04f47fa8e1a7eb0571b4a28e1da64bc728"}, + {file = "ruff-0.16.9-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:7baa24ef5fc8e77aa93879e1d3f43754a01ae488e869f1ae30cf431afd4d2452"}, + {file = "ruff-0.16.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:a41aac6230aadfaa133bdfa1614488531ffa3e0837567ae04c0da2058a9c0f9e"}, + {file = "ruff-0.16.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:c2529fb5896d49115b0e9aa8f887490b34bbe76baf879ec2264ac59406869ce7"}, + {file = "ruff-0.16.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:41e3870277694177429b56406d65dfbdb2c2802c52b715edaf6a0b829c69d4ee"}, + {file = "ruff-0.16.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:8adbe4e58af167f767d7b2ba5e83c42e878350796cf78c2f5e14ab9903a92588"}, + {file = "ruff-0.16.9-py3-none-win32.whl", hash = "sha256:0e1dbc2073624dee6618d41d0098690a7244654af746704b64759e12b6b6b385"}, + {file = "ruff-0.16.9-py3-none-win_amd64.whl", hash = "sha256:6bd40fec8cd4c8a3d4dd589bd8ad4e6320c13c29234159bfd959a40d529d597b"}, + {file = "ruff-0.16.9-py3-none-win_arm64.whl", hash = "sha256:ed1a252039200f57a59eebc063b54beabea67bfbaaca0eeaa7f54b5fbcda2284"}, + {file = "ruff-0.16.9.tar.gz", hash = "sha256:12b625c6cfba78d285d9f48eda5f053374f1e53cb10ef17342a383750db99161"}, ] [[package]] @@ -1376,4 +1376,4 @@ python-discovery = ">=1.6" [metadata] lock-version = "2.1" python-versions = ">=3.14" -content-hash = "49f0d995b2145b043958a8691cde9cff4bc18d59c74cfa73298fe9f98ce2cde8" +content-hash = "b5de6b8300eff20f83646bdd72c8966fe885a2bf8a77221196d8723f60e0355a" diff --git a/pyproject.toml b/pyproject.toml index c7d8762..24e0863 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ dependencies = [ "typer[all]>=0.12,<0.13", "rich>=13.0,<14", "pyyaml>=6.0,<7", - "pydantic>=2.0,<3", + "pydantic>=2.13.5,<3", "httpx[socks]>=0.27,<0.28", "json5>=0.9,<0.10", ] @@ -36,7 +36,7 @@ pytest = "^9.1.1" ruff = "^0.16" mypy = "^2.3" pre-commit = "^4.0" -pyinstaller = { version = "^6.22.2", python = ">=3.14,<3.16" } +pyinstaller = { version = "^6.22.3", python = ">=3.14,<3.16" } pytest-cov = "^7.1.0" [tool.mypy]