diff --git a/docs/cli/plan-mode.md b/docs/cli/plan-mode.md index 33d557843f..b46acaf966 100644 --- a/docs/cli/plan-mode.md +++ b/docs/cli/plan-mode.md @@ -109,16 +109,6 @@ switch back to another mode. - **Keyboard shortcut:** Press `Shift+Tab` to cycle to the desired mode. - **Natural language:** Ask Gemini CLI to "exit plan mode" or "stop planning." -## Customization and best practices - -Plan Mode is secure by default, but you can adapt it to fit your specific -workflows. You can customize how Gemini CLI plans by using skills, adjusting -safety policies, or changing where plans are stored. - -## Commands - -- **`/plan copy`**: Copy the currently approved plan to your clipboard. - ## Tool Restrictions Plan Mode enforces strict safety policies to prevent accidental changes. @@ -146,6 +136,12 @@ These are the only allowed tools: - **Skills:** [`activate_skill`](../cli/skills.md) (allows loading specialized instructions and resources in a read-only manner) +## Customization and best practices + +Plan Mode is secure by default, but you can adapt it to fit your specific +workflows. You can customize how Gemini CLI plans by using skills, adjusting +safety policies, changing where plans are stored, or adding hooks. + ### Custom planning with skills You can use [Agent Skills](../cli/skills.md) to customize how Gemini CLI @@ -294,6 +290,71 @@ modes = ["plan"] argsPattern = "\"file_path\":\"[^\"]+[\\\\/]+\\.gemini[\\\\/]+plans[\\\\/]+[\\w-]+\\.md\"" ``` +### Using hooks with Plan Mode + +You can use the [hook system](../hooks/writing-hooks.md) to automate parts of +the planning workflow or enforce additional checks when Gemini CLI transitions +into or out of Plan Mode. + +Hooks such as `BeforeTool` or `AfterTool` can be configured to intercept the +`enter_plan_mode` and `exit_plan_mode` tool calls. + +> [!WARNING] When hooks are triggered by **tool executions**, they do **not** +> run when you manually toggle Plan Mode using the `/plan` command or the +> `Shift+Tab` keyboard shortcut. If you need hooks to execute on mode changes, +> ensure the transition is initiated by the agent (e.g., by asking "start a plan +> for..."). + +#### Example: Archive approved plans to GCS (`AfterTool`) + +If your organizational policy requires a record of all execution plans, you can +use an `AfterTool` hook to securely copy the plan artifact to Google Cloud +Storage whenever Gemini CLI exits Plan Mode to start the implementation. + +**`.gemini/hooks/archive-plan.sh`:** + +```bash +#!/usr/bin/env bash +# Extract the plan path from the tool input JSON +plan_path=$(jq -r '.tool_input.plan_path // empty') + +if [ -f "$plan_path" ]; then + # Generate a unique filename using a timestamp + filename="$(date +%s)_$(basename "$plan_path")" + + # Upload the plan to GCS in the background so it doesn't block the CLI + gsutil cp "$plan_path" "gs://my-audit-bucket/gemini-plans/$filename" > /dev/null 2>&1 & +fi + +# AfterTool hooks should generally allow the flow to continue +echo '{"decision": "allow"}' +``` + +To register this `AfterTool` hook, add it to your `settings.json`: + +```json +{ + "hooks": { + "AfterTool": [ + { + "matcher": "exit_plan_mode", + "hooks": [ + { + "name": "archive-plan", + "type": "command", + "command": "./.gemini/hooks/archive-plan.sh" + } + ] + } + ] + } +} +``` + +## Commands + +- **`/plan copy`**: Copy the currently approved plan to your clipboard. + ## Planning workflows Plan Mode provides building blocks for structured research and design. These are