Skip to content

fix(codex): build the plugin environment during setup - #2626

Merged
hidb4ai merged 3 commits into
oceanbase:masterfrom
Fengzdadi:fix/codex-first-session-mcp
Nov 14, 2026
Merged

hidb4ai merged 3 commits into
oceanbase:masterfrom
Fengzdadi:fix/codex-first-session-mcp

Conversation

@Fengzdadi

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

No tracking issue. The real-host Codex E2E in #2624 found it: the first session after powercontext setup codex often started without PowerContext's MCP tools.

Rationale for this change

Setup installs the plugin but leaves its Python environment unbuilt. Codex's first session then builds it from two places at once:

  • the MCP header helper (http_headers_helper, uv run ... mcp_headers.py), which Codex runs with a filtered environment (HOME, PATH, USER, SHELL, TMPDIR, ...; no UV_*);
  • the SessionStart hook (uv run ... session_binding.py), which runs with the session's full environment.

Observed with Codex CLI 0.167.2, by wrapping the helper to record its environment, stderr and exit status:

  1. The helper starts first and builds .venv with the interpreter uv finds (3.11 here). The hook starts during that build with UV_PYTHON set (3.13), and uv replaces the environment. The helper fails mid-import (ModuleNotFoundError: No module named 'annotated_types', or a dylib rejected with code signature ... not valid). It exits 1 on all three of Codex's attempts (250 ms and 1 s apart), and the session has no PowerContext MCP at all.
  2. When the helper survives, the cold build still takes longer than Codex waits for an optional MCP server. That wait is mcp_optional_startup_grace_ms, 1000 ms by default; the server is required: false. Codex then lists the first turn's tools without PowerContext.

Same-environment concurrent uv run calls are safe: uv locks the environment, and eight hand-run races never failed. The damage comes from the build happening at session start, under two different environments. Building it once during setup removes both effects. The helper and the hooks then accept the existing environment whatever interpreter they would have picked.

What changes are included in this PR?

  • CodexInstaller.install checks for uv before it runs any Codex command that changes state, so a machine without uv is refused with nothing installed.
  • After codex plugin add, CodexInstaller.install runs uv sync --frozen --quiet --project <installed plugin>. It does this also when it keeps an already-installed disabled plugin.
    • The call drops UV_PROJECT_ENVIRONMENT and VIRTUAL_ENV, so it builds the plugin's own .venv (the one the helper uses) and never syncs into another project's environment.
    • A failure raises SetupError with uv's message and says the plugin is installed and setup can be rerun.
  • powercontext doctor codex reports a new plugin_runtime check, run with uv sync --frozen --check before the MCP probe (whose helper run would otherwise build the environment itself):
    • ok when the environment is ready;
    • degraded, with a hint to rerun setup, when a plugin was installed or updated without setup;
    • failed when uv is missing.
  • _codex_plugin_root holds the plugin cache location shared by the endpoint update, setup and doctor.
  • Docs (en/zh, Codex integration):
    • Setup builds the environment, which needs uv and, on a first install, the package index; doctor flags a plugin installed without setup.
    • Codex waits only mcp_optional_startup_grace_ms for optional MCP servers. For codex exec, setting this top-level key to 0 makes the first turn wait up to startup_timeout_sec.

Are there any user-facing changes?

Yes.

  • powercontext setup codex now builds the plugin environment, so it needs uv on PATH and can take a few seconds longer on first install.
  • Without uv, setup fails before changing Codex.
  • If uv cannot build the environment (for example, offline with an empty cache), setup fails after the plugin is installed instead of reporting success. Before, the hooks and the MCP helper failed later, at session start.
  • doctor codex --json has a new plugin_runtime entry.
  • Setup does not change Codex's mcp_optional_startup_grace_ms; the docs describe it.

How was this change tested?

  • New tests in tests/test_system_cli.py; each fails on master and passes here:

    Test What it does Failure on master
    test_setup_codex_prepares_the_plugin_runtime_before_codex_starts_it Runs setup on a copy of the real plugin, then runs the helper command setup wrote, with a Codex-like environment and UV_NO_SYNC=1 so uv must find the environment ready; doctor then reports plugin_runtime ok. ModuleNotFoundError: No module named 'pydantic'
    test_doctor_codex_reports_a_plugin_environment_that_setup_did_not_build Installs the plugin without setup; doctor reports degraded and builds nothing. KeyError: 'plugin_runtime'
    test_setup_codex_requires_uv_before_changing_codex Without uv, setup raises and runs no Codex command that changes state. setup does not raise
  • The first two tests run a real uv sync of the plugin's lock, as tests/test_hook_characterization.py already does, so on a cold uv cache they need the package index.

  • uv run pytest tests/test_system_cli.py tests/test_setup_transport.py tests/test_cli_minimax.py tests/codex_plugin tests/test_cli*.py: 929 passed, 46 skipped. make check passes.

  • Real Codex CLI 0.167.2 on macOS arm64. I ran the test(e2e): run real Codex with the PowerContext plugin against a Server in CI #2624 E2E cases plus five first-session search_memory cases, with the E2E's own prebuild step removed, under pytest -n 5:

    Setup mcp_optional_startup_grace_ms First-session cases passed Helper failures
    master default (1000) 1/5 and 0/5 in two probe runs yes (exit 1, half-built .venv)
    this PR default (1000) 33/40 over eight runs none
    this PR 0 20/20 over four runs (all 40 cases passed) none
    master 0 15/15 over three runs none
    • The remaining misses with this PR and the default grace occur only under 5-way parallel load; the last two runs had a load average near 17. In each miss the environment is ready and the helper succeeds, but MCP connects after the 1 s grace. The docs describe the setting for non-interactive runs.
    • Run one at a time on an idle machine with a warm uv cache, master also passed 5/5: the cold build then fits inside the grace. A real first install downloads the plugin's packages and does not get that.
  • Not verified: Windows (the new test is POSIX-only; the setup step uses the same uv call), and a real first install with an empty uv cache (it would need downloads).

  • After this merges, test(e2e): run real Codex with the PowerContext plugin against a Server in CI #2624's E2E can drop its own prebuild step.

AI usage statement

Claude Code (Claude Opus 5.5) investigated the failure with the real Codex CLI, wrote the change, the test and this description, and ran the verification above.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 08:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One integration regression remains: the new doctor field causes Desktop to reject the entire integration diagnostic report.

Comment thread src/powercontext/cli/system.py Resolved

@hireview001 hireview001 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • CI failed: CI result (failure).
  • CI failed: windows-unit-portability (failure).
  • CI failed: Main (failure). Failed steps: windows-unit-portability / Verify Codex resolves and launches cmd shims on Windows; CI result / Require successful selected checks.

Desktop's diagnostics projection accepts only the check names it knows and
rejects the whole doctor report otherwise, so the plugin_runtime check that
`powercontext doctor codex` now reports made Desktop show the Integrations
report as an invalid response.

Add plugin_runtime to the projection's allowlist. A Codex doctor fixture now
links the two sides: the Desktop test projects every check in it, and a
Python test fails when Codex doctor emits a check the fixture lacks.
Setup now refuses to run without uv on PATH. The Windows cmd shim fixture
sets PATH to its own bin directory only, so every setup test built on it
failed at that check. Put a placeholder uv.exe there; the environment build
itself stays stubbed by the autouse fixture.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 38efa8b. The Desktop projection regression is fixed: the original CLI-output counterexample now passes, runtime statuses remain visible, and private details remain excluded. The shared fixture covers the CLI-to-Desktop contract, and the Windows test fixture now satisfies the uv prerequisite. No remaining correctness findings, new database tables, or new public operations; the existing doctor JSON gains a diagnostic field. Latest-head CI is still running, including validation needed for the Windows fixture change, and must complete before merge.

@hidb4ai
hidb4ai merged commit 728fd5d into oceanbase:master Nov 14, 2026
8 checks passed
@hidb4ai hidb4ai added the 1.5.2 v1.5.2 release tracking label Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.5.2 v1.5.2 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants