|
| 1 | +--- |
| 2 | +title: About hooks |
| 3 | +shortTitle: Hooks |
| 4 | +intro: 'Extend and customize {% data variables.product.prodname_copilot %} agent behavior by executing custom shell commands at key points during agent execution.' |
| 5 | +product: '{% data reusables.gated-features.copilot-coding-agent %}<br><a href="https://github.com/features/copilot/plans?ref_product=copilot&ref_type=purchase&ref_style=button" target="_blank" class="btn btn-primary mt-3 mr-3 no-underline"><span>Sign up for {% data variables.product.prodname_copilot_short %}</span> {% octicon "link-external" height:16 %}</a>' |
| 6 | +versions: |
| 7 | + feature: copilot |
| 8 | +topics: |
| 9 | + - Copilot |
| 10 | +contentType: concepts |
| 11 | +category: |
| 12 | + - Configure Copilot |
| 13 | +--- |
| 14 | + |
| 15 | +## About hooks |
| 16 | + |
| 17 | +Hooks enable you to execute custom shell commands at strategic points in an agent's workflow, such as when an agent session starts or ends, or before and after a prompt is entered or a tool is called. |
| 18 | + |
| 19 | +Hooks receive detailed information about agent actions via JSON input, enabling context-aware automation. For example, you can use hooks to: |
| 20 | + |
| 21 | +* Programmatically approve or deny tool executions. |
| 22 | +* Utilize built-in security features like secret scanning to prevent credential leaks. |
| 23 | +* Implement custom validation rules and audit logging for compliance. |
| 24 | + |
| 25 | +{% data variables.product.prodname_copilot_short %} agents support hooks stored in JSON files in your repository at `.github/hooks/*.json`. |
| 26 | + |
| 27 | +Hooks are available for use with: |
| 28 | + |
| 29 | +* {% data variables.copilot.copilot_coding_agent %} on {% data variables.product.github %} |
| 30 | +* {% data variables.copilot.copilot_cli %} in the terminal |
| 31 | + |
| 32 | +## Types of hooks |
| 33 | + |
| 34 | +The following types of hooks are available: |
| 35 | + |
| 36 | +* **sessionStart**: Executed when a new agent session begins or when resuming an existing session. Can be used to initialize environments, log session starts for auditing, validate project state, and set up temporary resources. |
| 37 | +* **sessionEnd**: Executed when the agent session completes or is terminated. Can be used to cleanup temporary resources, generate and archive session reports and logs, or send notifications about session completion. |
| 38 | +* **userPromptSubmitted**: Executed when the user submits a prompt to the agent. Can be used to log user requests for auditing and usage analysis. |
| 39 | +* **preToolUse**: Executed before the agent uses any tool (such as `bash`, `edit`, `view`). This is the most powerful hook as it can **approve or deny tool executions**. Use this hook to block dangerous commands, enforce security policies and coding standards, require approval for sensitive operations, or log tool usage for compliance. |
| 40 | +* **postToolUse**: Executed after a tool completes execution (whether successful or failed). Can be used to log execution results, track usage statistics, generate audit trails, monitor performance metrics, and send failure alerts. |
| 41 | +* **errorOccurred**: Executed when an error occurs during agent execution. Can be used to log errors for debugging, send notifications, track error patterns, and generate reports. |
| 42 | + |
| 43 | +To see a complete reference of hook types with example use cases, best practices, and advanced patterns, see [AUTOTITLE](/copilot/reference/hooks-configuration). |
| 44 | + |
| 45 | +## Hook configuration format |
| 46 | + |
| 47 | +You configure hooks using a special JSON format. The JSON must contain a `version` field with a value of `1` and a `hooks` object containing arrays of hook definitions. |
| 48 | + |
| 49 | +```json copy |
| 50 | +{ |
| 51 | + "version": 1, |
| 52 | + "hooks": { |
| 53 | + "sessionStart": [ |
| 54 | + { |
| 55 | + "type": "command", |
| 56 | + "bash": "string (optional)", |
| 57 | + "powershell": "string (optional)", |
| 58 | + "cwd": "string (optional)", |
| 59 | + "env": { "KEY": "value" }, |
| 60 | + "timeoutSec": 30 |
| 61 | + } |
| 62 | + ], |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The hook object can contain the following keys: |
| 68 | + |
| 69 | +| Property | Required | Description | |
| 70 | +| --- | --- | --- | |
| 71 | +| `type` | Yes | Must be `"command"` | |
| 72 | +| `bash` | Yes (on Unix systems) | Path to the bash script to execute | |
| 73 | +| `powershell` | Yes (on Windows) | Path to the PowerShell script to execute | |
| 74 | +| `cwd` | No | Working directory for the script (relative to repository root) | |
| 75 | +| `env` | No | Additional environment variables that are merged with the existing environment | |
| 76 | +| `timeoutSec` | No | Maximum execution time in seconds (default: 30) | |
| 77 | + |
| 78 | +## Example hook configuration file |
| 79 | + |
| 80 | +This is an example configuration file that lives in `~/.github/hooks/project-hooks.json` within a repository. |
| 81 | + |
| 82 | +```json copy |
| 83 | +{ |
| 84 | + "version": 1, |
| 85 | + "hooks": { |
| 86 | + "sessionStart": [ |
| 87 | + { |
| 88 | + "type": "command", |
| 89 | + "bash": "echo \"Session started: $(date)\" >> logs/session.log", |
| 90 | + "powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"", |
| 91 | + "cwd": ".", |
| 92 | + "timeoutSec": 10 |
| 93 | + } |
| 94 | + ], |
| 95 | + "userPromptSubmitted": [ |
| 96 | + { |
| 97 | + "type": "command", |
| 98 | + "bash": "./scripts/log-prompt.sh", |
| 99 | + "powershell": "./scripts/log-prompt.ps1", |
| 100 | + "cwd": "scripts", |
| 101 | + "env": { |
| 102 | + "LOG_LEVEL": "INFO" |
| 103 | + } |
| 104 | + } |
| 105 | + ], |
| 106 | + "preToolUse": [ |
| 107 | + { |
| 108 | + "type": "command", |
| 109 | + "bash": "./scripts/security-check.sh", |
| 110 | + "powershell": "./scripts/security-check.ps1", |
| 111 | + "cwd": "scripts", |
| 112 | + "timeoutSec": 15 |
| 113 | + }, |
| 114 | + { |
| 115 | + "type": "command", |
| 116 | + "bash": "./scripts/log-tool-use.sh", |
| 117 | + "powershell": "./scripts/log-tool-use.ps1", |
| 118 | + "cwd": "scripts" |
| 119 | + } |
| 120 | + ], |
| 121 | + "postToolUse": [ |
| 122 | + { |
| 123 | + "type": "command", |
| 124 | + "bash": "cat >> logs/tool-results.jsonl", |
| 125 | + "powershell": "$input | Add-Content -Path logs/tool-results.jsonl" |
| 126 | + } |
| 127 | + ], |
| 128 | + "sessionEnd": [ |
| 129 | + { |
| 130 | + "type": "command", |
| 131 | + "bash": "./scripts/cleanup.sh", |
| 132 | + "powershell": "./scripts/cleanup.ps1", |
| 133 | + "cwd": "scripts", |
| 134 | + "timeoutSec": 60 |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Performance considerations |
| 142 | + |
| 143 | +Hooks run synchronously and block agent execution. To ensure a responsive experience, keep the following considerations in mind: |
| 144 | + |
| 145 | +* **Minimize execution time**: Keep hook execution time under 5 seconds when possible. |
| 146 | +* **Optimize logging**: Use asynchronous logging, like appending to files, rather than synchronous I/O. |
| 147 | +* **Use background processing**: For expensive operations, consider background processing. |
| 148 | +* **Cache results**: Cache expensive computations when possible. |
| 149 | + |
| 150 | +## Security considerations |
| 151 | + |
| 152 | +To ensure security is maintained when using hooks, keep the following considerations in mind: |
| 153 | + |
| 154 | +* **Always validate and sanitize the input processed by hooks**. Untrusted input could lead to unexpected behavior. |
| 155 | +* **Use proper shell escaping when constructing commands**. This prevents command injection vulnerabilities. |
| 156 | +* **Never log sensitive data, such as tokens or passwords**. |
| 157 | +* **Ensure hook scripts and logs have the appropriate permissions**. |
| 158 | +* **Be cautious with hooks that make external network calls**. These can introduce latency, failures, or expose data to third parties. |
| 159 | +* **Set appropriate timeouts to prevent resource exhaustion**. Long-running hooks can block agent execution and degrade performance. |
| 160 | + |
| 161 | +## Next steps |
| 162 | + |
| 163 | +To start creating hooks, see [AUTOTITLE](/copilot/how-tos/use-copilot-agents/coding-agent/use-hooks). |
| 164 | + |
0 commit comments