Python
Packaged app: the sidecar runs as the tool runner, and ships the runner sources
Devika Verma5ce2a18a9204
diff --git a/coworker/sandbox/bundle.py b/coworker/sandbox/bundle.pyindex 3e006a61..034146a3 100644--- a/coworker/sandbox/bundle.py+++ b/coworker/sandbox/bundle.py@@ -8,6 +8,7 @@ mount that folder into a sandbox whether the server runs on the host or in a sanfrom __future__ import annotations+import sysimport hashlibimport osimport zipfile@@ -39,7 +40,12 @@ def tool(fn=None, **_kwargs):def _sources() -> list[Path]:root = Path(__file__).parent / "runner"- return sorted(p for p in root.glob("*.py"))+ if getattr(sys, "frozen", False): # the packaged app ships the sources as data (the spec)+ root = Path(getattr(sys, "_MEIPASS", "")) / "coworker" / "sandbox" / "runner_src"+ found = sorted(p for p in root.glob("*.py"))+ if not found:+ raise RuntimeError(f"the tool runner's sources are missing from this build ({root})")+ return founddef _toolkit_sources() -> list[Path]:@@ -47,10 +53,14 @@ def _toolkit_sources() -> list[Path]:found = []for name in _AISUITE_TOOLKITS:- spec = importlib.util.find_spec(f"aisuite.toolkits.{name}")- if spec is None or not spec.origin or not spec.origin.endswith(".py"):+ if getattr(sys, "frozen", False): # the packaged app ships the sources as data (the spec)+ origin = Path(getattr(sys, "_MEIPASS", "")) / "aisuite" / "toolkits_src" / f"{name}.py"+ else:+ spec = importlib.util.find_spec(f"aisuite.toolkits.{name}")+ origin = Path(spec.origin) if spec is not None and spec.origin else None+ if origin is None or origin.suffix != ".py" or not origin.is_file():raise RuntimeError(f"cannot find the source of aisuite.toolkits.{name} to pack into the tool runner")- found.append(Path(spec.origin))+ found.append(origin)return founddiff --git a/coworker/sandbox/launch.py b/coworker/sandbox/launch.pynew file mode 100644index 00000000..cc79d561--- /dev/null+++ b/coworker/sandbox/launch.py@@ -0,0 +1,50 @@+"""How a provider starts the tool runner on THIS machine.++From a source or pip install the runner is the zipapp, run by the same Python that runs the+server, with `-S` so no site-packages leak in. In the packaged desktop app there is no+Python to call: the frozen sidecar starts ITSELF in runner mode (`openworker-server+sandbox-runner ...`), which loads only the standard-library runner package. Providers that+run the runner inside a Linux container (OpenShell) keep using the zipapp, because the+Mac binary cannot run there.+"""++from __future__ import annotations++import os+import sys+from pathlib import Path+++def frozen() -> bool:+ return bool(getattr(sys, "frozen", False))+++def runner_command(runner_path: Path) -> list[str]:+ """The argv prefix that runs the runner here; `serve ...` or `attach ...` follows."""+ if frozen():+ return [sys.executable, "sandbox-runner"]+ return [sys.executable, "-S", str(runner_path)]+++def read_paths() -> list[str]:+ """What the process that runs the runner must be able to read: the interpreter and its+ standard library, or the frozen app's folder (the bootloader reads `_internal`)."""+ paths = {os.path.dirname(os.path.realpath(sys.executable))}+ if frozen():+ meipass = getattr(sys, "_MEIPASS", "")+ if meipass:+ paths.add(os.path.realpath(meipass))+ else:+ paths.update({sys.prefix, sys.base_prefix})+ return sorted(p for p in paths if p)+++def maybe_run_runner(argv: list[str]) -> bool:+ """`openworker-server sandbox-runner <serve|attach> ...`: run the runner and exit.+ Returns False when argv is anything else. Imports only the runner package, so the+ daemon inside a sandbox stays small and starts fast."""+ if argv[:1] != ["sandbox-runner"]:+ return False+ from .runner.__main__ import main as runner_main++ raise SystemExit(runner_main(argv[1:]))diff --git a/coworker/sandbox/providers/runner_local.py b/coworker/sandbox/providers/runner_local.pyindex 224af972..770f2a05 100644--- a/coworker/sandbox/providers/runner_local.py+++ b/coworker/sandbox/providers/runner_local.py@@ -17,6 +17,7 @@ from pathlib import Pathfrom typing import Any, Optionalfrom ..bundle import build_runner_zipapp+from ..launch import runner_commandfrom ..transport import PipeTransport, Transport@@ -38,7 +39,7 @@ class RunnerLocalProvider:def create(self) -> None:self._daemon = subprocess.Popen(- [sys.executable, "-S", str(self._runner), "serve", "--socket", self.socket_path, "--cwd", self.cwd, "--exit-with-parent"],+ [*runner_command(self._runner), "serve", "--socket", self.socket_path, "--cwd", self.cwd, "--exit-with-parent"],stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,@@ -53,7 +54,7 @@ class RunnerLocalProvider:time.sleep(0.02)def open_runner(self) -> Transport:- argv = [sys.executable, "-S", str(self._runner), "attach", "--socket", self.socket_path]+ argv = [*runner_command(self._runner), "attach", "--socket", self.socket_path]if self._relay_silence is not None:argv += ["--silence-seconds", str(self._relay_silence)]return PipeTransport(diff --git a/coworker/sandbox/providers/seatbelt.py b/coworker/sandbox/providers/seatbelt.pyindex 1bf339bc..ba84d2b8 100644--- a/coworker/sandbox/providers/seatbelt.py+++ b/coworker/sandbox/providers/seatbelt.py@@ -31,6 +31,7 @@ from typing import Any, Optional, Sequencefrom .. import credentials as credsfrom .. import netproxy, network_profilesfrom ..bundle import build_runner_zipapp+from ..launch import read_paths, runner_commandfrom ..transport import PipeTransport, Transportfrom . import seatbelt_profile@@ -58,8 +59,6 @@ def preflight() -> None:raise SeatbeltUnavailable("The Seatbelt sandbox exists only on macOS.")if not os.access(SANDBOX_EXEC, os.X_OK):raise SeatbeltUnavailable(f"{SANDBOX_EXEC} is missing on this Mac.")- if getattr(sys, "frozen", False):- raise SeatbeltUnavailable("The Seatbelt sandbox is not available in the packaged desktop app yet.")probe = subprocess.run([SANDBOX_EXEC, "-p", "(version 1)(allow default)", "/usr/bin/true"],stdin=subprocess.DEVNULL,@@ -79,13 +78,6 @@ def clean_environment(base: Optional[dict[str, str]] = None) -> dict[str, str]:return {k: v for k, v in source.items() if not k.startswith(_OWN_PREFIXES) and not _SECRET_NAME.search(k)}-def _python_read_paths() -> list[str]:- """What the Python that runs the runner needs to read: itself and its standard library.- In a virtual environment the real interpreter lives elsewhere (`base_prefix`)."""- paths = {sys.prefix, sys.base_prefix, os.path.dirname(os.path.realpath(sys.executable))}- return sorted(p for p in paths if p)--class SeatbeltProvider:name = "seatbelt"@@ -133,7 +125,7 @@ class SeatbeltProvider:return seatbelt_profile.render(self.roots,runtime_dir=self._dir,- read_only=[str(self._runner), *_python_read_paths(), str(toolchain.bin_dir())],+ read_only=[str(self._runner), *read_paths(), str(toolchain.bin_dir())],proxy_port=self._proxy.port if self._proxy is not None else None,)@@ -175,7 +167,7 @@ class SeatbeltProvider:self._daemon = subprocess.Popen([SANDBOX_EXEC, "-p", self.profile_text(),- sys.executable, "-S", str(self._runner),+ *runner_command(self._runner),"serve", "--socket", self.socket_path, "--cwd", self.cwd, "--exit-with-parent",], # fmt: skipstdin=subprocess.DEVNULL,@@ -196,7 +188,7 @@ class SeatbeltProvider:time.sleep(0.02)def open_runner(self) -> Transport:- argv = [sys.executable, "-S", str(self._runner), "attach", "--socket", self.socket_path]+ argv = [*runner_command(self._runner), "attach", "--socket", self.socket_path]if self._relay_silence is not None:argv += ["--silence-seconds", str(self._relay_silence)]return PipeTransport(diff --git a/coworker/server/run.py b/coworker/server/run.pyindex 40cd9ac9..dbc99733 100644--- a/coworker/server/run.py+++ b/coworker/server/run.py@@ -158,6 +158,9 @@ def _warn_if_state_shared() -> None:def main(argv=None) -> None:+ from ..sandbox.launch import maybe_run_runner++ maybe_run_runner(list(sys.argv[1:] if argv is None else argv)) # `sandbox-runner ...`_ensure_ca_bundle()cfg = load_config() # global config supplies defaultsparser = argparse.ArgumentParser(prog="openworker-server")diff --git a/packaging/openworker-server.spec b/packaging/openworker-server.specindex 0f77857d..f56fb7bf 100644--- a/packaging/openworker-server.spec+++ b/packaging/openworker-server.spec@@ -20,6 +20,7 @@ spawns this sidecar with the Windows CREATE_NO_WINDOW flag (see src-tauri/src/lihides the window while keeping stdio intact."""+import globimport osimport sys@@ -52,6 +53,19 @@ for pkg in ("coworker", "aisuite", "mcp", "ddgs", "croniter", "docstring_parser"# collects whatever non-.py files the package carries.datas += collect_data_files("coworker")+# The tool runner is packed into a zipapp FROM SOURCE at run time (coworker/sandbox/bundle.py)+# and mounted into sandboxes, so its .py files, and aisuite's two toolkit modules it carries,+# must ship as data next to their compiled modules. Nothing else needs sources.+# They go under a folder of their own (`*_src`): PyInstaller drops a data file whose path+# is also a collected module's, so shipping them beside the compiled modules loses them.+# Listed by path from ROOT, not through collect_data_files: that resolves `coworker` through+# the environment's import path, which in a development checkout can be another worktree.+datas += [+ (path, "coworker/sandbox/runner_src")+ for path in sorted(glob.glob(os.path.join(ROOT, "coworker", "sandbox", "runner", "*.py")))+]+datas += [(src, "aisuite/toolkits_src") for src, _ in collect_data_files("aisuite.toolkits", include_py_files=True)]+if not INCLUDE_EXPERIMENTAL:hiddenimports = [m for m in hiddenimports if not m.startswith("coworker.connectors.experimental")diff --git a/packaging/server_entry.py b/packaging/server_entry.pyindex edbfff97..f197fdcb 100644--- a/packaging/server_entry.py+++ b/packaging/server_entry.py@@ -4,7 +4,14 @@ Thin wrapper so PyInstaller has a concrete script to analyze (the console_script`openworker-server` is generated metadata, not a file). Runs the same `main()`."""-from coworker.server.run import main+import sysif __name__ == "__main__":+ # Runner mode first, before the server's imports: inside a sandbox the frozen binary+ # is the tool runner (coworker/sandbox/launch.py), and it must not load the server.+ from coworker.sandbox.launch import maybe_run_runner++ maybe_run_runner(sys.argv[1:])+ from coworker.server.run import main+main()diff --git a/tests/test_sandbox_runner.py b/tests/test_sandbox_runner.pyindex ffa98525..eb55f90c 100644--- a/tests/test_sandbox_runner.py+++ b/tests/test_sandbox_runner.py@@ -322,3 +322,20 @@ def test_the_provider_setting_selects_the_runner(tmp_path, monkeypatch):ws.close()with pytest.raises(ValueError):open_workspace(cwd=tmp_path, provider="no-such-thing")+++def test_the_server_binary_can_run_as_the_runner(tmp_path):+ """`openworker-server sandbox-runner ...` is how the packaged app starts the runner+ (coworker/sandbox/launch.py): the same daemon, without loading the server."""+ import subprocess+ import sys++ from coworker.sandbox import launch++ assert launch.maybe_run_runner(["--port", "1"]) is False+ with pytest.raises(SystemExit):+ launch.maybe_run_runner(["sandbox-runner", "--help"])+ # From source the runner is the zipapp under -S; frozen it is the binary itself.+ assert launch.runner_command(tmp_path / "r.pyz")[:2] == [sys.executable, "-S"]+ done = subprocess.run([sys.executable, "-m", "coworker.server.run", "sandbox-runner", "--help"], capture_output=True, text=True, timeout=60)+ assert done.returncode == 0 and "serve" in done.stdout and "attach" in done.stdout