Statewright

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/statewright

Or add to ~/.pi/agent/settings.json:

{ "extensions": ["path/to/statewright/plugins/pi"] }

Setup

  1. Get an API key at statewright.ai/keys
  2. Save it:
    mkdir -p ~/.statewright
    echo 'sw_live_...' > ~/.statewright/api_key
  3. 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 model inherit meta.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:

  1. Define a fork transition in your workflow (see Fork/Join Guide)
  2. The model calls statewright_fork with branch tasks
  3. Each branch runs as a separate Pi process with its own gateway session
  4. Branches get the implementing state's model, tools, and instructions
  5. After completion, BRANCH_DONE events 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

CommandDescription
/statewright load <name>Load and activate a workflow
/statewright deactivateDeactivate enforcement
/statewright pausePause (resume with --resume)
/statewright listList available workflows
/statewright statusGateway 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 pi

Shows model switches, tool set changes, fork branch connections, BRANCH_DONE events in mauve-colored output.

Extension Hooks

HookWhat it does
before_agent_startRefreshes state, applies model/thinking/tools routing, injects phase context
tool_callBlocks unauthorized tools, bash discernment, final state protection
tool_resultTracks state changes, applies model routing after transitions, interrupt detection
message_endTool call recovery from text, auto-continuation

Source

plugins/pi/

On this page