Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
perf(build): compile bytecode the runtimes actually load
trohitg2615d2104f3d
diff --git a/cli/commands/build.py b/cli/commands/build.pyindex deb39567..274d31a1 100644--- a/cli/commands/build.py+++ b/cli/commands/build.py@@ -33,12 +33,13 @@ from cli.platform_ import node_modules_dir, project_root, server_dir# Source dirs / entry-point modules under ``server/`` that ``company build``-# pre-compiles to optimised bytecode in step [5/6]. Public so tests and+# pre-compiles to plain .pyc bytecode in step [5/6]. Public so tests and# ``scripts/install.js`` (which mirrors this list) stay in sync — both# the install pipeline and tests should read this constant rather than-# duplicate the list. Excludes ``.venv/`` (uv compiles deps at install-# time and some site-packages contain non-Python templates) and-# ``tests/`` (ships outside the production tarball).+# duplicate the list. Excludes ``.venv/`` (compiled by ``uv sync`` via+# ``[tool.uv] compile-bytecode = true`` in server/pyproject.toml; some+# site-packages also contain non-Python templates) and ``tests/``+# (ships outside the production tarball).COMPILEALL_SOURCE_DIRS: tuple[str, ...] = ("services","core",@@ -201,20 +202,24 @@ def build_command() -> None:# is idempotent and creates the venv on first run.run(["uv", "sync"], cwd=server_cwd)- # Pre-compile our Python sources to optimised bytecode (.opt-1.pyc).- # `-O` strips `assert` statements + `__debug__` branches; `-q`- # silences per-file output (errors still print); `-j 0` parallelises- # across all CPU cores. Cuts a measurable few seconds off cold- # start by avoiding source-to-bytecode work on first import.+ # Pre-compile our Python sources to plain .pyc. No `-O`: every+ # runtime (uvicorn via `uv run`, `company serve`'s venv python) runs+ # WITHOUT -O, and per PEP 488 a non-optimized interpreter only loads+ # plain .pyc — `-O`-produced .opt-1.pyc files are never loaded and+ # the sources get recompiled on first import anyway. `-q` silences+ # per-file output (errors still print); `-j 0` parallelises across+ # all CPU cores.#- # Scoped to the project's own source dirs — `uv sync` already- # compiles `.venv/` packages, and `tests/` ships outside the- # tarball. Compiling `.venv/` is wasted work and fails on cookiecutter- # template files inside packages like crawlee that aren't real Python.+ # Scoped to the project's own source dirs — `.venv/` packages are+ # compiled by `uv sync` itself via `[tool.uv] compile-bytecode = true`+ # in server/pyproject.toml, and `tests/` ships outside the tarball.+ # Compiling `.venv/` here would be duplicate work and fails on+ # cookiecutter template files inside packages like crawlee that+ # aren't real Python.console.log("[5/6] Compiling Python bytecode...")run(uv_run(- "python", "-O", "-m", "compileall", "-q", "-j", "0", *COMPILEALL_SOURCE_DIRS+ "python", "-m", "compileall", "-q", "-j", "0", *COMPILEALL_SOURCE_DIRS),cwd=server_cwd,check=False, # missing pyc is non-fatal — runtime regenerates as neededdiff --git a/cli/tests/test_build_compile_pipeline.py b/cli/tests/test_build_compile_pipeline.pyindex 0dc6b9e3..f51ef2f1 100644--- a/cli/tests/test_build_compile_pipeline.py+++ b/cli/tests/test_build_compile_pipeline.py@@ -3,7 +3,7 @@Covers the two new steps wired into ``cli.commands.build.build_command``:- ``[3/6] Building Node.js sidecar`` -> ``pnpm --filter opencompany-nodejs-executor run build``-- ``[5/6] Compiling Python bytecode`` -> ``uv run python -O -m compileall -q -j 0 ...``+- ``[5/6] Compiling Python bytecode`` -> ``uv run python -m compileall -q -j 0 ...``The orchestrator is exercised once per test with every external surfacemocked (toolchain probes, ``run``, ``project_root``) so the assertions@@ -163,9 +163,12 @@ def test_only_one_sidecar_bundle_invocation(tmp_path: Path):# ----------------------------------------------------------------------------def test_compileall_uses_optimised_flag_quiet_and_parallel(tmp_path: Path):- """``-O`` strips asserts; ``-q`` silences per-file output; ``-j 0``- parallelises across all CPU cores.+def test_compileall_emits_plain_pyc_quiet_and_parallel(tmp_path: Path):+ """No ``-O``: every runtime launches python without ``-O`` and per+ PEP 488 a non-optimized interpreter only loads plain ``.pyc`` —+ ``.opt-1.pyc`` from ``-O`` would never be loaded and sources would+ recompile on first import anyway. ``-q`` silences per-file output;+ ``-j 0`` parallelises across all CPU cores."""captured = _run_build_capture_invocations(tmp_path)match = _find_call(captured, lambda c: "compileall" in c)@@ -173,7 +176,10 @@ def test_compileall_uses_optimised_flag_quiet_and_parallel(tmp_path: Path):match is not None), f"compileall step missing in {[c['argv'] for c in captured]}"argv = match[1]["argv"]- assert "-O" in argv, "must use -O for .opt-1.pyc output"+ assert "-O" not in argv, (+ "-O emits .opt-1.pyc that the non-optimized runtime never loads "+ "(PEP 488) — compile plain .pyc instead"+ )assert "-q" in argv, "must use -q to silence per-file logging"assert "-j" in argv, "must enable parallelism via -j"j_idx = argv.index("-j")@@ -195,9 +201,9 @@ def test_compileall_runs_via_uv_run_python(tmp_path: Path):"run","--no-sync","python",- "-O","-m",- ], f"expected `uv run --no-sync python -O -m compileall ...`, got {argv[:7]}"+ "compileall",+ ], f"expected `uv run --no-sync python -m compileall ...`, got {argv[:7]}"def test_compileall_path_list_matches_source_dirs_constant(tmp_path: Path):@@ -349,7 +355,7 @@ def test_pipeline_order_client_then_sidecar_then_uv_then_compileall(tmp_path: Pa1. client build (``pnpm --filter react-flow-client run build``)2. sidecar bundle (``pnpm --filter opencompany-nodejs-executor run build``)3. uv sync (``uv sync``)- 4. compileall (``uv run python -O -m compileall ...``)+ 4. compileall (``uv run python -m compileall ...``)compileall MUST follow uv sync because it invokes ``uv run python``,which requires the venv to exist.diff --git a/cli/tests/test_release_pipeline_config.py b/cli/tests/test_release_pipeline_config.pyindex 3a91744f..400bef68 100644--- a/cli/tests/test_release_pipeline_config.py+++ b/cli/tests/test_release_pipeline_config.py@@ -472,21 +472,42 @@ def test_preinstall_never_removes_current_package_directory(preinstall_js_src: s# Single source of truth for parsing install.js's compileall command line.# Must match the shape that build.py emits exactly:-# uv run python -O -m compileall -q -j 0 <dirs...>+# uv run python -m compileall -q -j 0 <dirs...>+# No -O: runtimes launch python without -O and per PEP 488 only load+# plain .pyc — .opt-1.pyc output would never be used._INSTALL_JS_COMPILEALL_RE = re.compile(- r"""['"]uv\s+run\s+python\s+-O\s+-m\s+compileall\s+-q\s+-j\s+0\s+([^'"]+)['"]"""+ r"""['"]uv\s+run\s+python\s+-m\s+compileall\s+-q\s+-j\s+0\s+([^'"]+)['"]""")def test_install_js_compileall_command_shape(install_js_src: str):"""End-user ``npm install opencompany`` runs install.js. Thecompileall step must use the same shape as ``company build`` —- ``uv run python -O -m compileall -q -j 0`` — so cold-start gains- apply to the npm-tarball path too.+ ``uv run python -m compileall -q -j 0`` (plain .pyc, no -O) — so+ cold-start gains apply to the npm-tarball path too."""assert (_INSTALL_JS_COMPILEALL_RE.search(install_js_src) is not None- ), "install.js must run `uv run python -O -m compileall -q -j 0 ...`"+ ), "install.js must run `uv run python -m compileall -q -j 0 ...`"+++def test_server_pyproject_enables_uv_compile_bytecode(root: Path):+ """``[tool.uv] compile-bytecode = true`` must stay set in+ server/pyproject.toml — uv's default is false, which leaves all+ site-package ``.py`` files to compile lazily on the FIRST import+ after a fresh ``uv sync`` (tens of seconds on a post-clean cold+ boot). The compileall step above only covers project source; this+ setting covers ``.venv/``.+ """+ import tomllib++ pyproject = tomllib.loads(+ (root / "server" / "pyproject.toml").read_text(encoding="utf-8")+ )+ assert pyproject.get("tool", {}).get("uv", {}).get("compile-bytecode") is True, (+ "server/pyproject.toml must set [tool.uv] compile-bytecode = true "+ "(cold-boot bytecode precompilation for site-packages)"+ )def test_install_js_compileall_paths_match_source_dirs_constant(install_js_src: str):diff --git a/scripts/install.js b/scripts/install.jsindex b9706e0f..088fcbfa 100755--- a/scripts/install.js+++ b/scripts/install.js@@ -209,17 +209,20 @@ try {}run('uv sync', serverDir, 600000); // 10 min timeout- // Pre-compile our Python sources to optimised bytecode (.opt-1.pyc).- // `-O` strips assertions and `__debug__` branches; `-q` silences- // per-file output; `-j 0` parallelises across CPU cores. Scoped to- // our own source dirs — `uv sync` already compiles `.venv/` and- // some site-packages contain non-Python template files that would- // log spurious errors. Failure is non-fatal: the runtime regenerates- // missing .pyc on first import. Trims a few seconds off cold start.+ // Pre-compile our Python sources to plain .pyc. No `-O`: every runtime+ // launches python without -O, and per PEP 488 a non-optimized+ // interpreter never loads `.opt-1.pyc` — plain .pyc is the file that+ // actually gets used. `-q` silences per-file output; `-j 0`+ // parallelises across CPU cores. Scoped to our own source dirs —+ // `.venv/` is compiled by `uv sync` itself ([tool.uv]+ // compile-bytecode = true in server/pyproject.toml) and some+ // site-packages contain non-Python template files that would log+ // spurious errors. Failure is non-fatal: the runtime regenerates+ // missing .pyc on first import. Trims tens of seconds off cold start.step++;console.log(`[${step}/${totalSteps}] Compiling Python bytecode...`);try {- run('uv run python -O -m compileall -q -j 0 services core nodes routers models middleware main.py constants.py', serverDir, 120000);+ run('uv run python -m compileall -q -j 0 services core nodes routers models middleware main.py constants.py', serverDir, 120000);} catch (err) {console.log(` Warning: bytecode compilation failed (non-fatal): ${err.message}`);}diff --git a/server/pyproject.toml b/server/pyproject.tomlindex 01663937..a92cde74 100644--- a/server/pyproject.toml+++ b/server/pyproject.toml@@ -245,3 +245,9 @@ ignore = [[tool.uv]index-url = "https://pypi.org/simple"+# Compile site-packages to .pyc at `uv sync` time (uv default is false —+# without this, all ~9,700 dependency .py files compile lazily on the+# FIRST import after a fresh sync, adding tens of seconds to a post-clean+# cold boot on Windows). Trades longer sync for faster startup, per the+# uv docs' recommendation for startup-critical applications.+compile-bytecode = true