ec801665bc
- Migrated environment variable access from direct process.env to centralized getEnv() utility function across all packages. - Renamed environment variable prefix from OMP_ to PI_ throughout codebase (e.g., OMP_CODING_AGENT_DIR -> PI_CODING_AGENT_DIR). - Removed automatic environment variable migration from PI_ to OMP_ prefixes via migrate-env.ts module. - Removed env setting from configuration schema and applyEnvironmentVariables() method from settings. - Updated CI/CD build configuration to use PI_COMPILED flag instead of OMP_COMPILED. - Changed venvPath property in PythonRuntime from nullable (string | null) to optional (string | undefined).
4.2 KiB
4.2 KiB
Python REPL (Jupyter Kernel Gateway)
Requirements
- Python 3 available on PATH (or via an active virtualenv)
jupyter-kernel-gateway(kernel_gatewaymodule) andipykernelinstalled in the selected Python environment
Install:
python -m pip install jupyter_kernel_gateway ipykernel
How It Works
The Python tool uses a Jupyter Kernel Gateway and talks to it over REST and WebSocket APIs. By default it uses a shared local gateway so multiple pi instances reuse the same gateway process.
Shared-gateway startup flow:
- Filter the environment and resolve the Python runtime (including venv detection)
- Acquire the shared gateway (reuse a healthy gateway or spawn
python -m kernel_gatewayon 127.0.0.1:PORT) - Wait for gateway readiness (
GET /api/kernelspecs) - Create a kernel (
POST /api/kernels) - Connect WebSocket for execution messages
- Initialize kernel environment, run prelude helpers, and load extension modules
External Gateway Support
Instead of spawning a local gateway, you can connect to an already-running Jupyter Kernel Gateway:
# Connect to external gateway
export PI_PYTHON_GATEWAY_URL="http://127.0.0.1:8888"
# Optional: auth token if gateway requires it (KG_AUTH_TOKEN)
export PI_PYTHON_GATEWAY_TOKEN="your-token-here"
When PI_PYTHON_GATEWAY_URL is set:
- No local gateway process is spawned
- Kernels are created on the external gateway
- The gateway process is not killed on shutdown
- Availability check uses
/api/kernelspecsendpoint instead of local module check
This is useful for:
- Remote kernel execution
- Shared kernel environments
- Pre-configured gateway setups
Environment Propagation
- The kernel inherits a filtered environment (explicit allowlist + denylist)
- Allowlisted prefixes include
LC_,XDG_, andPI_; known API-key vars are removed PYTHONPATHis passed through if present- Virtual environments are detected via
VIRTUAL_ENV,.venv/, orvenv/and preferred when present
Prelude Extensions
Optional .py modules are loaded after the prelude from:
~/.omp/agent/modulesand~/.pi/agent/modules<project>/.omp/modulesand<project>/.pi/modules
Project modules override user modules with the same filename.
Kernel Modes
Settings under python control exposure and reuse:
toolMode:both(default),ipy-only,bash-onlykernelMode:session(default) orper-callsharedGateway:true(default). Setting tofalsethrows an error because local (per-process) gateways are not supported; the shared gateway is required.
Mode behavior:
session: reuse kernels per session id, serialize execution, evict after 5 minutes of idle time (max 4 sessions)per-call: create a fresh kernel per tool call and shut it down afterward
Environment override:
PI_PY=0|bash→bash-onlyPI_PY=1|py→ipy-onlyPI_PY=mix|both→both
Shell Helper
The Python prelude exposes run() which executes a shell command via bash -c (or sh -c fallback)
and returns a ShellResult with stdout, stderr, and code.
Output Handling
- Streams
stdout/stderras text application/x-omp-statusemits structured status events for the TUIimage/pngdisplay data renders inline in TUIapplication/jsondisplay data renders as a collapsible treetext/markdownis rendered as-is,text/plainis used as a fallbacktext/htmldisplay data is converted to basic markdown
Troubleshooting
- Kernel unavailable: Ensure
python+jupyter-kernel-gateway+ipykernelare installed; the session will fall back to bash-only. - Python mode override: Check
python.toolModeorPI_PYif the Python tool is missing. - Shared gateway disabled:
python.sharedGateway=falsecauses the Python tool to error because local (per-process) gateways are not supported. - Skip preflight checks: Set
PI_PYTHON_SKIP_CHECK=1to bypass kernel availability checks. - External gateway unreachable: Check the URL is correct and the gateway is running. If auth is required, set
PI_PYTHON_GATEWAY_TOKEN. - IPC tracing: Set
PI_PYTHON_IPC_TRACE=1to log kernel message flow. - Stdin requests: Interactive input is not supported; refactor code to avoid
input()or provide data programmatically.