mirror of
https://github.com/openai/codex.git
synced 2026-09-04 15:08:45 +00:00
Split the code-mode protocol and client from the V8-backed runtime so core and
app-server no longer link codex-code-mode in production. ThreadManager now
provisions durable code-mode sessions through a shared external host process,
while tests can still inject the in-process provider.
The IPC protocol uses a persistent stdin/stdout transport. Each frame is a
4-byte big-endian length followed by JSON, with a 16 MiB frame limit. Client
requests carry u64 request IDs so create, execute, wait, terminate, and shutdown
operations can be multiplexed over one process. Session IDs isolate durable
stored values. Execute returns an ExecutionStarted response immediately and an
asynchronous InitialResponse when the initial yield or completion is available.
Nested tool calls and notifications travel from the host back to the client as
delegate requests with their own IDs. Delegate responses, cancellation, and
cell-closed lifecycle messages use the same framed channel. Wire operations
encode errors as Result values. A dead connection fails pending operations,
cancels outstanding delegates, and lets the provider spawn a new host for later
sessions.
Build codex-code-mode-host with V8 pointer-compression sandbox support and add
it to canonical primary and app-server packages, legacy Linux and Windows
bundles, signing verification, installers, Python runtime packages, and release
CI for macOS, Linux, and Windows. The host is discovered next to the current
executable, through CODEX_CODE_MODE_HOST_PATH, or on PATH. OS-level seccomp or
seatbelt restrictions remain a follow-up to this cross-platform process split.
Benchmarks were run from release builds on Linux x86_64 with the V8 sandbox
profile and a text('ok') workload. Cold measurements used 30 samples, warm
session provisioning used 200, and warm command execution used 500. Values are
mean/p50/p95 in milliseconds:
- session startup: in-process 0.002/0.002/0.005, IPC 2.623/2.599/2.894
- fresh-session command: in-process 1.831/1.758/1.915, IPC 7.428/7.252/8.306
- warm session provisioning: in-process 0.002/0.002/0.003,
IPC 0.471/0.463/0.581
- warm command: in-process 1.759/1.757/1.940, IPC 2.005/2.001/2.166
The steady-state median command overhead is approximately 0.244 ms. The median
fresh host plus first command cost is 7.252 ms.
Validation:
- 62/62 core code-mode integration tests passed against the external host
- focused protocol, client, runtime, host, tools, and trace tests passed
- Cargo and Bazel real-process host IPC tests passed
- 11/11 package builder tests passed
- Bazel lock verification, scoped Clippy fixes, and repository formatting passed
182 lines
5.5 KiB
Python
182 lines
5.5 KiB
Python
"""Codex-built V8 artifact overrides for package Cargo builds."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from urllib.request import urlopen
|
|
|
|
from .targets import REPO_ROOT
|
|
from .targets import TargetSpec
|
|
|
|
|
|
DOWNLOAD_TIMEOUT_SECS = 120
|
|
ARTIFACT_PROFILE = "ptrcomp_sandbox_release"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RustyV8ArtifactPair:
|
|
archive: Path
|
|
binding: Path
|
|
|
|
|
|
def resolve_codex_v8_cargo_env(
|
|
spec: TargetSpec,
|
|
*,
|
|
environ: Mapping[str, str] | None = None,
|
|
cache_root: Path | None = None,
|
|
) -> dict[str, str]:
|
|
if spec.is_windows:
|
|
return {}
|
|
|
|
environ = os.environ if environ is None else environ
|
|
if environ.get("V8_FROM_SOURCE") in {"true", "1", "yes"}:
|
|
return {}
|
|
|
|
archive_override = environ.get("RUSTY_V8_ARCHIVE")
|
|
binding_override = environ.get("RUSTY_V8_SRC_BINDING_PATH")
|
|
if archive_override and binding_override:
|
|
return {}
|
|
if archive_override or binding_override:
|
|
raise RuntimeError(
|
|
"Cargo package builds need RUSTY_V8_ARCHIVE and RUSTY_V8_SRC_BINDING_PATH set together."
|
|
)
|
|
|
|
artifacts = fetch_codex_v8_artifacts(spec, cache_root=cache_root)
|
|
return {
|
|
"RUSTY_V8_ARCHIVE": str(artifacts.archive),
|
|
"RUSTY_V8_SRC_BINDING_PATH": str(artifacts.binding),
|
|
}
|
|
|
|
|
|
def fetch_codex_v8_artifacts(
|
|
spec: TargetSpec,
|
|
*,
|
|
version: str | None = None,
|
|
cache_root: Path | None = None,
|
|
) -> RustyV8ArtifactPair:
|
|
if spec.is_windows:
|
|
raise RuntimeError(
|
|
f"No Codex-built V8 release artifacts for target: {spec.target}"
|
|
)
|
|
|
|
version = version or resolved_v8_crate_version()
|
|
release_url = (
|
|
f"https://github.com/openai/codex/releases/download/rusty-v8-v{version}"
|
|
)
|
|
target = spec.target
|
|
cache_dir = (cache_root or default_cache_root()) / f"rusty-v8-{version}-{target}"
|
|
archive = cache_dir / f"librusty_v8_{ARTIFACT_PROFILE}_{target}.a.gz"
|
|
binding = cache_dir / f"src_binding_{ARTIFACT_PROFILE}_{target}.rs"
|
|
checksums = cache_dir / f"rusty_v8_{ARTIFACT_PROFILE}_{target}.sha256"
|
|
|
|
download_file(f"{release_url}/{checksums.name}", checksums)
|
|
expected_checksums = load_checksums(checksums, {archive.name, binding.name})
|
|
for artifact in [archive, binding]:
|
|
ensure_valid_artifact(
|
|
artifact,
|
|
expected_checksums[artifact.name],
|
|
f"{release_url}/{artifact.name}",
|
|
)
|
|
|
|
return RustyV8ArtifactPair(archive=archive, binding=binding)
|
|
|
|
|
|
def resolved_v8_crate_version() -> str:
|
|
import tomllib
|
|
|
|
cargo_lock = tomllib.loads((REPO_ROOT / "codex-rs" / "Cargo.lock").read_text())
|
|
versions = sorted(
|
|
{
|
|
package["version"]
|
|
for package in cargo_lock["package"]
|
|
if package["name"] == "v8"
|
|
}
|
|
)
|
|
if len(versions) != 1:
|
|
raise RuntimeError(
|
|
f"Expected exactly one resolved v8 version, found: {versions}"
|
|
)
|
|
return versions[0]
|
|
|
|
|
|
def default_cache_root() -> Path:
|
|
return Path(tempfile.gettempdir()) / "codex-package"
|
|
|
|
|
|
def load_checksums(checksums_path: Path, artifact_names: set[str]) -> dict[str, str]:
|
|
checksums: dict[str, str] = {}
|
|
lines = checksums_path.read_text(encoding="utf-8").splitlines()
|
|
if len(lines) != len(artifact_names):
|
|
raise RuntimeError(
|
|
f"Expected {len(artifact_names)} V8 checksums in {checksums_path}, found {len(lines)}."
|
|
)
|
|
|
|
for line in lines:
|
|
parts = line.split(maxsplit=1)
|
|
if len(parts) != 2:
|
|
raise RuntimeError(
|
|
f"Invalid V8 checksum line in {checksums_path}: {line!r}"
|
|
)
|
|
|
|
digest, artifact_name = parts[0], parts[1].strip()
|
|
if len(digest) != 64 or any(char not in "0123456789abcdef" for char in digest):
|
|
raise RuntimeError(
|
|
f"Invalid V8 checksum digest in {checksums_path}: {digest}"
|
|
)
|
|
if artifact_name not in artifact_names:
|
|
raise RuntimeError(
|
|
f"Unexpected V8 checksum artifact in {checksums_path}: {artifact_name}"
|
|
)
|
|
checksums[artifact_name] = digest
|
|
|
|
if checksums.keys() != artifact_names:
|
|
raise RuntimeError(
|
|
f"V8 checksum manifest {checksums_path} does not cover {artifact_names}."
|
|
)
|
|
return checksums
|
|
|
|
|
|
def ensure_valid_artifact(artifact: Path, checksum: str, url: str) -> None:
|
|
if has_checksum(artifact, checksum):
|
|
return
|
|
|
|
artifact.unlink(missing_ok=True)
|
|
download_file(url, artifact)
|
|
if has_checksum(artifact, checksum):
|
|
return
|
|
|
|
artifact.unlink(missing_ok=True)
|
|
raise RuntimeError(
|
|
f"Codex-built V8 artifact {artifact} failed checksum validation."
|
|
)
|
|
|
|
|
|
def has_checksum(path: Path, expected: str) -> bool:
|
|
if not path.is_file():
|
|
return False
|
|
|
|
digest = hashlib.sha256()
|
|
with path.open("rb") as artifact:
|
|
for chunk in iter(lambda: artifact.read(1024 * 1024), b""):
|
|
digest.update(chunk)
|
|
return digest.hexdigest() == expected
|
|
|
|
|
|
def download_file(url: str, dest: Path) -> None:
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
temp_path = dest.with_suffix(f"{dest.suffix}.tmp")
|
|
temp_path.unlink(missing_ok=True)
|
|
try:
|
|
with urlopen(url, timeout=DOWNLOAD_TIMEOUT_SECS) as response:
|
|
with temp_path.open("wb") as output:
|
|
shutil.copyfileobj(response, output)
|
|
temp_path.replace(dest)
|
|
finally:
|
|
temp_path.unlink(missing_ok=True)
|