Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/en/docs/integrations/codex.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ PowerContext tool.

The plugin's hooks and its MCP credential helper run through `uv` from `PATH`, so setup needs `uv` and, on a first
install, access to the package index. Building the environment at setup keeps the first session from building it
while Codex starts the hooks and MCP together. If the plugin is installed or updated by other means, `powercontext
doctor codex` reports `plugin_runtime` as degraded until you rerun setup.
while Codex starts the hooks and MCP together. uv rebuilds the environment when `UV_PYTHON` names a different
interpreter from the one setup built it with: if Codex starts with such a `UV_PYTHON`, the first session's
`SessionStart` hook rebuilds it, and that session can start without PowerContext's MCP tools. Later sessions reuse the
rebuilt environment. To avoid this, run setup with the same `UV_PYTHON` as Codex, or leave it unset when starting
Codex. If the plugin is installed or updated by other means, `powercontext doctor codex` reports `plugin_runtime` as
degraded until you rerun setup.

PowerContext's MCP server is optional to Codex, so an unavailable Server never blocks a session. Codex waits for
optional servers for one second by default (`mcp_optional_startup_grace_ms`) before it lists a session's first tools;
Expand Down
7 changes: 5 additions & 2 deletions docs/zh/docs/integrations/codex.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ powercontext doctor codex
重复执行是安全的。`--ref` 应与安装 PowerContext 工具时使用的 ref 一致。

插件的 Hook 和 MCP 凭据助手都通过 `PATH` 中的 `uv` 运行,因此 setup 需要 `uv`,首次安装时还需要能访问包索引。
在 setup 时构建环境,可避免首次会话在 Codex 同时启动 Hook 和 MCP 时才去构建。如果插件通过其他方式安装或更新,
在重新执行 setup 之前,`powercontext doctor codex` 会把 `plugin_runtime` 报告为 degraded。
在 setup 时构建环境,可避免首次会话在 Codex 同时启动 Hook 和 MCP 时才去构建。`UV_PYTHON` 指向的解释器与 setup
构建环境时所用的不同时,uv 会重建环境:如果 Codex 启动时带着这样的 `UV_PYTHON`,首次会话的 `SessionStart` Hook
会重建环境,该会话可能没有 PowerContext 的 MCP 工具,之后的会话复用重建后的环境。要避免这种情况,请用与 Codex
相同的 `UV_PYTHON` 运行 setup,或在启动 Codex 时不设置它。如果插件通过其他方式安装或更新,在重新执行 setup
之前,`powercontext doctor codex` 会把 `plugin_runtime` 报告为 degraded。

PowerContext 的 MCP server 对 Codex 是可选的,因此 Server 不可用时不会阻塞会话。Codex 默认只等待可选 server 一秒
(`mcp_optional_startup_grace_ms`),然后列出会话首轮可用的工具;在较慢或负载较高的机器上,首轮可能没有 PowerContext
Expand Down
18 changes: 3 additions & 15 deletions tests/e2e/test_codex_host_service_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _codex_config(model_url: str) -> str:


def _isolated_environment(home: Path, codex_home: Path, tmp_path: Path) -> dict[str, str]:
# The plugin hooks build their environment with uv; keep the runner's uv cache so they can do so offline.
# Setup builds the plugin's environment with uv; keep the runner's uv cache so it can do so offline.
uv_cache = subprocess.run(["uv", "cache", "dir"], capture_output=True, text=True, check=True).stdout.strip() # noqa: S607
# Proxies would route the loopback Server and model traffic away from this host.
prefixes = ("POWERCONTEXT_", "CODEX_", "OPENAI_", "VIRTUAL_ENV", "UV_PROJECT_ENVIRONMENT")
Expand All @@ -286,7 +286,8 @@ def _isolated_environment(home: Path, codex_home: Path, tmp_path: Path) -> dict[
XDG_CACHE_HOME=str(home / "cache"),
XDG_STATE_HOME=str(home / "state"),
UV_CACHE_DIR=uv_cache,
# Without managed interpreters in the isolated home, the hooks use the interpreter running these tests.
# Setup and the hooks both read this, so the hooks reuse the environment setup built; uv rebuilds it for a
# different interpreter. Without managed interpreters in the isolated home, both use the one running the tests.
UV_PYTHON=sys.executable,
POWERCONTEXT_CLIENT_CONFIG_FILE=str(tmp_path / "clients.json"),
CODEX_FIXTURE_MODEL_KEY="synthetic-only",
Expand Down Expand Up @@ -319,19 +320,6 @@ def _install_plugin(executable: str, environment: dict[str, str], base_url: str,
)
log.write_text(f"$ powercontext setup codex\n{completed.stdout}\n{completed.stderr}\n", encoding="utf-8")
assert completed.returncode == 0, f"powercontext_setup_codex_failed; see {log}"
# On first use the MCP header helper and the SessionStart hook would both build the plugin's environment while
# Codex starts, under different interpreters, and the helper can fail on the half-replaced environment. Build it
# once beforehand.
(installed,) = Path(environment["CODEX_HOME"]).glob("plugins/cache/*/powercontext/*/.codex-plugin")
synced = subprocess.run(
["uv", "sync", "--frozen", "--quiet", "--project", str(installed.parent)], # noqa: S607 - uv from PATH.
env=environment,
capture_output=True,
text=True,
timeout=180,
check=False,
)
assert synced.returncode == 0, f"codex_plugin_environment_failed: {synced.stderr[-2000:]}"


@contextmanager
Expand Down
Loading