tools: install Codex into the worker executable path

Use a global npm install within the service user's prefix so the configured .local/bin launcher exists. Exercise npm with an offline synthetic package and check the installed launcher without contacting a provider.

Refs #19
This commit is contained in:
Florian Schmidt
2026-11-15 11:15:06 +01:00
parent 583f920307
commit e1488243e1
3 changed files with 58 additions and 1 deletions
+1 -1
View File
@@ -121,7 +121,7 @@ def official_clis(providers):
script.chmod(0o644)
as_user("finaltoken", WORKER_HOME, ["bash", str(script), "stable"])
if "codex" in providers and not (WORKER_HOME / ".local/bin/codex").exists():
as_user("finaltoken", WORKER_HOME, ["npm", "install", "--prefix", str(WORKER_HOME / ".local"), "@openai/codex@latest"])
as_user("finaltoken", WORKER_HOME, ["npm", "install", "--global", "--prefix", str(WORKER_HOME / ".local"), "@openai/codex@latest"])
for provider in providers:
as_user("finaltoken", WORKER_HOME, [str(WORKER_HOME / ".local/bin" / provider), "--version"])
+52
View File
@@ -6,6 +6,7 @@ import io
import json
import os
from pathlib import Path, PurePosixPath
import shutil
import subprocess
import sys
import tarfile
@@ -208,6 +209,57 @@ class HostTests(unittest.TestCase):
class GuestTests(unittest.TestCase):
def test_codex_npm_install_creates_the_configured_launcher(self):
node, npm = shutil.which("node"), shutil.which("npm")
if not node or not npm:
self.skipTest("Offline npm launcher integration requires Node.js and npm")
npm_path = Path(npm).resolve()
npm_cli = npm_path if npm_path.suffix == ".js" else npm_path.parent / "node_modules/npm/bin/npm-cli.js"
self.assertTrue(npm_cli.is_file(), "Cannot locate the installed npm CLI")
with tempfile.TemporaryDirectory(prefix="finaltoken-npm-fixture-") as tmp:
root = Path(tmp)
package = root / "codex-fixture.tgz"
with tarfile.open(package, "w:gz") as archive:
files = {"package/package.json": json.dumps({"name": "@finaltoken/offline-codex-fixture", "version": "1.0.0",
"bin": {"codex": "cli.js"}}), "package/cli.js": "#!/usr/bin/env node\nconsole.log('synthetic codex fixture');\n"}
for name, value in files.items():
content = value.encode("utf-8")
item = tarfile.TarInfo(name)
item.size, item.mode = len(content), 0o755
archive.addfile(item, io.BytesIO(content))
user_config, global_config = root / "user.npmrc", root / "global.npmrc"
user_config.write_text(""); global_config.write_text("")
env = {name: os.environ[name] for name in ("PATH", "SystemRoot", "TEMP", "TMP", "ComSpec", "PATHEXT") if name in os.environ}
env.update(NPM_CONFIG_USERCONFIG=str(user_config), NPM_CONFIG_GLOBALCONFIG=str(global_config))
home = root / "worker"
home.mkdir()
versions = []
def local_user(name, directory, command, **kwargs):
self.assertEqual("finaltoken", name)
self.assertEqual(home, directory)
if command[0] == "npm":
arguments = list(command[1:-1])
prefix_index = arguments.index("--prefix") + 1
prefix = Path(arguments[prefix_index])
# npm puts global launchers directly in the prefix on Windows;
# adapt that platform boundary to exercise the guest's Linux bin path.
if os.name == "nt":
arguments[prefix_index] = str(prefix / "bin")
return subprocess.run([node, str(npm_cli), *arguments, str(package), "--offline", "--ignore-scripts",
"--no-audit", "--no-fund", "--cache", str(root / "cache")], env=env, cwd=root,
text=True, capture_output=True, check=True, timeout=30)
launcher = home / ".local/bin/codex"
self.assertEqual([str(launcher), "--version"], command)
self.assertTrue(launcher.is_file(), "npm did not install the configured Codex launcher")
installed = home / (".local/bin/node_modules" if os.name == "nt" else ".local/lib/node_modules")
result = subprocess.run([node, str(installed / "@finaltoken/offline-codex-fixture/cli.js"), "--version"],
env=env, cwd=root, text=True, capture_output=True, check=True, timeout=10)
versions.append(result.stdout.strip())
with patch.object(guest, "WORKER_HOME", home), patch.object(guest, "as_user", side_effect=local_user):
guest.official_clis(["codex"])
guest.official_clis(["codex"]) # Existing launcher is used on subsequent runs.
self.assertEqual(["synthetic codex fixture"] * 2, versions)
def test_server_update_uses_persisted_identity_and_preserves_config_and_history(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)