From fc73630002b5dff77cbf74b05dca60d1be21d575 Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Fri, 13 Nov 2026 21:27:25 -0500 Subject: [PATCH] test(e2e): rely on setup to build the Codex plugin environment `powercontext setup codex` builds the installed plugin's uv environment, so the host E2E's own post-setup `uv sync` repeated setup's work and hid a regression in it. Setup and the Codex session read the same UV_PYTHON from the fixture's environment, so the session's hooks reuse what setup built. Document that uv rebuilds the plugin environment when Codex starts with a UV_PYTHON naming a different interpreter from the one setup used, and that the first session can then miss PowerContext's MCP tools. --- docs/en/docs/integrations/codex.md | 8 ++++++-- docs/zh/docs/integrations/codex.md | 7 +++++-- tests/e2e/test_codex_host_service_chain.py | 18 +++--------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/docs/en/docs/integrations/codex.md b/docs/en/docs/integrations/codex.md index 0d06f919d9..3e7f17ebfa 100644 --- a/docs/en/docs/integrations/codex.md +++ b/docs/en/docs/integrations/codex.md @@ -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; diff --git a/docs/zh/docs/integrations/codex.md b/docs/zh/docs/integrations/codex.md index 34a3b83cef..b1549df370 100644 --- a/docs/zh/docs/integrations/codex.md +++ b/docs/zh/docs/integrations/codex.md @@ -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 diff --git a/tests/e2e/test_codex_host_service_chain.py b/tests/e2e/test_codex_host_service_chain.py index c9d0d55606..a0aeb3e081 100644 --- a/tests/e2e/test_codex_host_service_chain.py +++ b/tests/e2e/test_codex_host_service_chain.py @@ -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") @@ -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", @@ -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