Pi
Install statewright in Pi coding agent — model routing, thinking levels, parallel fork/join, local models
Pi
The statewright Pi plugin provides per-state model routing, thinking level control, native tool restrictions, parallel fork/join dispatch, and a corrective layer for local models.
Built for Pi + Ollama specifically, though it works with any provider Pi supports (OpenAI Codex subscription, Anthropic, Google, etc).
Install
# If Pi is installed globally:
pi install /path/to/statewright/plugins/pi
# Or manually copy to extensions:
cp -r plugins/pi ~/.pi/agent/extensions/statewrightOr add to ~/.pi/agent/settings.json:
{ "extensions": ["path/to/statewright/plugins/pi"] }Setup
- Get an API key at statewright.ai/keys
- Save it:
mkdir -p ~/.statewright echo 'sw_live_...' > ~/.statewright/api_key - Start Pi. The extension connects to the managed gateway on startup.
Per-State Model Routing
Switch the active model when entering each workflow state. Frontier plans, commodity executes:
{
"meta": { "default_model": "openai-codex/gpt-5.4" },
"states": {
"planning": { "model": "openai-codex/gpt-5.5" },
"implementing": { "model": "ollama-qwen/qwen3.6:35b" },
"testing": { "model": "ollama/gemma4:31b" }
}
}- States without
modelinheritmeta.default_model - If neither is set, Pi's current model is unchanged
- Cross-provider switching works (openai → ollama → anthropic)
- The original model is saved and restored when entering a state with no model
- Status bar shows model with directional indicators:
[statewright] planning [gpt-5.5 ↑]
Custom Ollama Providers
Add to ~/.pi/agent/models.json:
{
"providers": {
"ollama": {
"baseUrl": "https://your-ollama-host/v1",
"api": "openai-completions",
"apiKey": "ollama",
"models": [{
"id": "gemma4:31b",
"name": "Gemma 4 31B",
"contextWindow": 32768,
"maxTokens": 4096,
"cost": { "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0 }
}]
}
}
}Per-State Thinking Level
Control reasoning effort per state:
"planning": { "thinking_level": "xhigh" },
"implementing": { "thinking_level": "off" }Valid levels depend on the model. Common: off, minimal, low, medium, high, xhigh. The plugin warns if a level is clamped by the model's capabilities.
Native Tool Restrictions
Tools not in allowed_tools are removed from the LLM's schema entirely via Pi's setActiveTools() API. The model can't hallucinate calls to tools it doesn't see.
"planning": { "allowed_tools": ["Read", "Grep", "Glob", "Bash"] },
"implementing": { "allowed_tools": ["Read", "Edit", "Write", "Bash"] }Bash Discernment
When Bash is allowed but Edit/Write are not:
- Safe read-only commands pass:
ls,cat,grep,pytest,git status - Blocked: output redirects (
>), scripting interpreters (python,node), destructive commands (rm), directory traversal
Parallel Fork/Join
The statewright_fork tool spawns parallel Pi subprocesses for each branch:
- Define a fork transition in your workflow (see Fork/Join Guide)
- The model calls
statewright_forkwith branch tasks - Each branch runs as a separate Pi process with its own gateway session
- Branches get the implementing state's model, tools, and instructions
- After completion,
BRANCH_DONEevents fire and the gateway joins
"on": {
"FORK": {
"fork": {
"branches": {
"fix-converter": { "initial": "implementing", "terminal": "branch-done" },
"fix-formatter": { "initial": "implementing", "terminal": "branch-done" }
},
"join": "all",
"on_complete": "integration-testing"
}
}
}/statewright Command
| Command | Description |
|---|---|
/statewright load <name> | Load and activate a workflow |
/statewright deactivate | Deactivate enforcement |
/statewright pause | Pause (resume with --resume) |
/statewright list | List available workflows |
/statewright status | Gateway status |
Local Model Corrective Layer
Tool call execution recovery
Parses JSON tool calls from text output, executes via shell. Normalizes edit parameter variants ({file, old, new}, {path, old_text, new_text}, etc.) to Pi's schema.
Rambling watchdog
Aborts + steers models that generate text without tool calls. 30s for action states, 90s for states with thinking levels set.
Auto-continuation
Nudges stalled models with current phase instructions and available tools. 30s cooldown prevents flail loops.
Debug Logging
STATEWRIGHT_DEBUG=1 piShows model switches, tool set changes, fork branch connections, BRANCH_DONE events in mauve-colored output.
Extension Hooks
| Hook | What it does |
|---|---|
before_agent_start | Refreshes state, applies model/thinking/tools routing, injects phase context |
tool_call | Blocks unauthorized tools, bash discernment, final state protection |
tool_result | Tracks state changes, applies model routing after transitions, interrupt detection |
message_end | Tool call recovery from text, auto-continuation |