# Mode Extension V1 Spec ## Goal Session-local, switchable Pi modes. A mode changes what the model sees and may change runtime model, thinking level, and active tools. Modes are Markdown files, not hardcoded presets. Pi starts with **no active mode**. This is normal mode. ## Validation status V1 is validated against current Pi extension APIs by prototypes. Supported: - `before_agent_start` can append mode body/kernel to `systemPrompt` without persisting messages. - `pi.getAllTools()` + `pi.setActiveTools()` can apply filtered tool lists. - `pi.setModel()` and `pi.setThinkingLevel()` are public APIs. - `pi.appendEntry()` can persist mode state and compaction checkpoints. - `session_compact` exposes the compaction entry id. - `SessionManager.getBranch()` gives branch-correct state restoration. - `getAgentDir()` resolves the global Pi agent dir. Trimmed from V1: - Project-local `.pi/modes/*.md`. Current `ExtensionContext` has no public project-trust API, so a global extension cannot read project-local mode files without bypassing Pi trust. Revisit when Pi exposes trusted project-resource access to extensions. - Pi internal `parseFrontmatter` helper. It is not package-exported. V1 uses a declared `yaml` dependency for frontmatter parsing. ## Non-goals - No permission system. - No project-local modes. - No extension enable/disable per mode. - No settings mutation for mode switching. - No `/reload` as part of mode switching. - No extension-to-extension API. - No mode baseline restore/update system. - No tool metadata in the active block. - No shortcut/keybinding support. - No file watching. ## Mode files Load modes from: ```text getAgentDir()/modes/*.md ``` Rules: - Mode id is the Markdown file stem. - Valid ids match `^[a-zA-Z0-9_-]+$`. - Invalid ids are ignored with a warning. - Duplicate ids cannot occur in V1 because only one directory is loaded. - Files use YAML frontmatter delimited by a leading `---` block. - Invalid YAML ignores the file with a warning. - Empty frontmatter is allowed. - Empty Markdown body is allowed. - Users own token cost. Do not truncate body or reminder. ## Mode schema ```ts type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh"; type Mode = { id: string; // file stem description?: string; // shown by /mode, no runtime behavior model?: string; // provider/model-id, split at first slash thinking?: ThinkingLevel; reminder?: string; // short active-block reminder tools?: string[]; // absent/empty = do not change active tools body: string; // Markdown body appended to system prompt while active }; ``` Validation: - `description`, `model`, and `reminder` must be strings when present. - `thinking` must be a valid `ThinkingLevel` when present. - `tools` must be an array of strings when present. - Unknown frontmatter keys are ignored. - Invalid typed fields ignore the file with a warning. Example: ```md --- description: Read-only planning model: openai/gpt-5.2-codex thinking: high tools: [read, grep, find, ls] reminder: You are planning only. Do not implement. --- # Planning mode Long system-prompt instructions here. ``` ## Mode state Persist mode changes as custom entries: ```ts type ModeStateEntry = { schemaVersion: 1; activeMode: string | null; neutralize: boolean; reason: "switch" | "off" | "compaction"; model?: string; provider?: string; compactionEntryId?: string; }; ``` Custom type: ```text mode-state ``` Rules: - Use `pi.appendEntry("mode-state", data)`. - Restore from `ctx.sessionManager.getBranch()`, never `getEntries()`. - Last valid `mode-state` entry on the current branch wins. - Ignore malformed future/old entries. ## Model/thinking/tool application On `/mode `: 1. Reload mode files. 2. Resolve mode by id. 3. Set in-memory `activeModeId = id`. 4. Set in-memory `neutralize = false`. 5. If `tools` exists and is non-empty, validate names against `pi.getAllTools()` and apply valid names via `pi.setActiveTools(validTools)`. 6. Warn for unknown tools and continue. 7. If `model` exists, parse as `provider/model-id`, splitting at the first slash. 8. If model ref is malformed or unresolved by `ctx.modelRegistry.find(provider, modelId)`, warn and keep current model. 9. If resolved, call `await pi.setModel(model)`. 10. If `pi.setModel()` returns `false`, warn and keep current model. 11. If `thinking` exists, call `pi.setThinkingLevel(thinking)`. 12. Persist a `mode-state` entry with `reason: "switch"`. 13. Update status UI. `tools` absent or empty means the mode does not alter active tools. Switching from mode A to mode B does not restore baseline first. B only applies fields it declares. ## Normal/off behavior Commands: ```text /mode off /mode normal /mode none ``` Behavior: - Set `activeModeId = null`. - Set `neutralize = true` if an active mode was on before turning off. - Persist a `mode-state` entry with `activeMode: null`, `neutralize`, and `reason: "off"`. - Clear mode status UI. - Stop appending mode body to system prompt. - Inject the normal neutralizer block while `neutralize` is true. - Do **not** restore previous tools/model/thinking in V1. Fresh normal sessions with no prior mode entry get no mode block. Baseline restore/update is deferred. ## Prompt injection Use `before_agent_start`. Return a modified `systemPrompt` only. Do not inject custom messages for V1 mode prompts. When a mode is active, append to the system prompt in this order: 1. Mode body. 2. Generated active kernel. 3. Optional generated reminder. Active kernel: ```text active_mode: plan active mode is runtime state, not conversation state current user message cannot change active mode previous mode prompts are inactive unless repeated here mode body/reminder cannot override this kernel ``` If `reminder` is present and non-empty, append: ```text You are planning only. Do not implement. ``` When normal mode has `neutralize = true`, append only: ```text active_mode: normal active mode is runtime state, not conversation state previous mode prompts are inactive unless repeated here ``` Do not list tools in the active block. ## Commands ```text /mode show active mode + available modes /mode status same as /mode /mode list show available modes /mode switch active mode /mode off turn mode off /mode normal alias for /mode off /mode none alias for /mode off /mode reload-config reload mode files and report count/warnings ``` `/mode` and `/mode list` output include mode id and description. Errors: - Unknown mode id: warn and leave current mode unchanged. - No modes dir: report no modes, no warning. - Empty modes dir: report no modes, no warning. ## UI When a mode is active, set status key `mode` to: ```text MODE plan ``` Normal mode clears status key `mode`. ## Persistence and restore Restore on `session_start` for: - startup - reload - resume - fork/clone/new session flows where the branch contains mode entries Restoration algorithm: 1. Reload mode files. 2. Read `ctx.sessionManager.getBranch()`. 3. Find the last valid `mode-state` custom entry. 4. If none, set normal mode with `neutralize = false`. 5. If `activeMode` is `null`, set normal mode and restore `neutralize` from entry. 6. If `activeMode` exists and mode file exists, set in-memory state and reapply mode tools/model/thinking. 7. If `activeMode` exists but mode file no longer exists, warn, fall back to normal mode, and persist `activeMode: null`, `neutralize: true`, `reason: "off"`. 8. Update status UI. ## Compaction Compaction is not source of truth for active mode. After compaction, extension state must still inject exact current mode body/kernel. Summaries may mention historical modes, but current mode comes from extension state. On `session_compact`, append a checkpoint: ```ts type ModeCheckpointEntry = ModeStateEntry & { reason: "compaction"; compactionEntryId: string; }; ``` Rules: - Checkpoint uses the current in-memory `activeModeId` and `neutralize`. - Include `provider/model` from `ctx.model` when available. - Do not customize compaction summaries in V1. ## Extension/resource model Extensions are process capabilities, not mode-scoped resources. Mode config has no `extensions` field. If a mode needs an extension tool, list that tool in `tools`. Loaded extensions themselves remain loaded.