GitHub Copilot Guide: Hooks for Permissions, Auditing, and Recovery
Answer in brief
GitHub Copilot hooks run external commands at defined lifecycle points in Copilot CLI and Copilot cloud agent. They can enforce pre-tool permission decisions, create audit records, and trigger cleanup or alerts, but configuration sources, supported events, and output persistence differ by surface.
Key facts at a glance
| Product / model | Current ID or version | Use case | Evidence |
|---|---|---|---|
| github-copilot | Official source does not specify a selectable model ID | Confirm the current product surface | Official source Official source |
Failure modes and verification
| Failure mode | Verification action |
|---|---|
| Stale model or version reference | Compare the model name and ID with the official source before release. |
| Unstructured or incomplete output | Validate the response against the documented contract and a deterministic fixture. |
| Unverified factual claim | Keep the claim qualified or remove the claim when the official source does not support it. |
FAQ
Where can GitHub Copilot hook configuration be stored?
Repository hooks use .github/hooks/*.json. Copilot CLI also supports user directories, inline settings, installed plugins, and administrator policy sources. Copilot cloud agent discovers only repository hook files by default. See the official hooks reference.
Are the same hook events available in Copilot CLI and Copilot cloud agent?
No. Copilot CLI supports all events described in the hooks reference, while Copilot cloud agent fires only a subset. Confirm the target surface before relying on a lifecycle event.
Which hook can approve or deny tool execution?
preToolUse can programmatically approve or deny a proposed tool execution. Copilot cloud agent runs with tool permissions pre-granted and does not show permission dialogs, so this control is programmatic rather than interactive. See the official hooks reference.
Which events are useful for audit logging?
sessionStart, sessionEnd, userPromptSubmitted, preToolUse, postToolUse, stop events, and errorOccurred can record different execution stages. postToolUse runs after both successful and failed tool executions. Logs should exclude secrets and unnecessary prompt content.
Do hooks automatically roll back failed changes?
The supplied documentation does not define automatic rollback behavior. Events such as errorOccurred, sessionEnd, and a resumed sessionStart can launch custom recovery, cleanup, or validation commands, but any rollback logic must be designed and tested explicitly.
How can cloud-agent audit output be retained?
Files in the ephemeral Linux sandbox are discarded when the job ends. Output that must persist must be sent through an http hook entry. Destinations outside GitHub and Copilot require an administrator-configured firewall allow rule.
What happens when hook configuration is invalid?
For directory-loaded files, an invalid individual hook is dropped and logged while valid siblings continue loading. Invalid JSON, an unsupported version, or a non-array event list rejects the whole file. An item-level error in inline settings.json hooks rejects the entire hooks field.
Sources and freshness
- Official source
- Official source
- Last verified: 2026-08-30
Extended guide
GitHub Copilot hooks execute external commands at defined points in an agent session. They are available in Copilot CLI and Copilot cloud agent, but the two surfaces differ in configuration discovery, supported events, execution environment, and output persistence. Hooks can gate tool calls, capture audit data, and initiate cleanup or alerts. The documentation does not define hooks as an automatic rollback mechanism.
Scope and hook locations
Configuration uses JSON with version: 1 and a hooks object whose event keys contain arrays of hook definitions. Command hooks run synchronously and block agent execution, so they should have bounded timeouts and remain fast. GitHub recommends keeping execution under five seconds when possible.
| Area | Documented behavior |
|---|---|
| Copilot CLI | Hook sources are combined in this order: policy, user, project, and plugins. Repository files use .github/hooks/*.json. User files default to ~/.copilot/hooks/ on macOS and Linux or %USERPROFILE%\.copilot\hooks\ on Windows; COPILOT_HOME changes the location to $COPILOT_HOME/hooks/. Inline settings and plugin-provided hooks are also supported. All entries for the same event are run. |
| Policy hooks | These CLI-only, machine-wide hooks load before other hooks and cannot be disabled by disableAllHooks. Files are read from /etc/github-copilot/policy.d/*.json or C:\ProgramData\GitHub\Copilot\policy.d\*.json; Windows Registry policies are also supported. They require administrator control and remain available regardless of folder trust. |
| Copilot cloud agent | By default, configuration is discovered only from .github/hooks/*.json in the cloned repository. Hooks run non-interactively in an ephemeral Linux sandbox. bash is honored, command is a fallback, and powershell entries are ignored. The working directory is /workspace when a repository is cloned and /root otherwise. |
| Lifecycle events | Documented events include sessionStart, sessionEnd, userPromptSubmitted, preToolUse, postToolUse, agentStop, subagentStop, and errorOccurred. Copilot CLI supports all events described in the reference, whereas Copilot cloud agent fires only a subset. |
| Permissions and auditing | preToolUse can approve or deny a proposed tool execution. postToolUse runs after a tool succeeds or fails. Session, prompt, tool, stop, and error events can provide complementary audit records. |
Operational design
- Choose the enforcement scope. Put shared repository controls in
.github/hooks/*.json. Use CLI user hooks for personal behavior and administrator-installed policy hooks for machine-wide requirements. Review every combined source because hooks for the same event do not replace one another. - Map each event to one responsibility. Use
sessionStartto initialize or validate state, including when a session resumes. UseuserPromptSubmittedto record requests,preToolUseto evaluate the next tool call, andpostToolUseto capture its outcome. Use stop and session-end events for final records and cleanup. - Make permission decisions safely. Treat hook input as untrusted JSON, validate its structure, sanitize values, and apply correct shell escaping. Never include passwords, tokens, or unnecessary prompt content in commands or logs. In Copilot cloud agent, tool permissions are pre-granted and no permission dialogs are shown, so a
preToolUsepolicy is programmatic rather than interactive. See the hooks reference. - Build a bounded audit trail. Record only the identifiers, decisions, timestamps, outcomes, and errors required by the audit policy. Set
timeoutSec, protect scripts and logs with suitable permissions, and ensure logging failures do not create an uncontrolled delay. Test both approval and denial paths and both successful and failed tool results. - Design recovery explicitly. Use
errorOccurredfor error records or alerts,sessionEndfor cleanup and reports, andsessionStartto revalidate state after resumption. These events can launch custom recovery procedures, but they do not themselves restore files or reverse completed actions. Define any rollback command, eligibility check, and audit record as part of the hook implementation.
Cloud-agent files are discarded when the job ends. Output that must survive the job must be sent through an http hook entry; destinations outside GitHub and Copilot require an administrator-configured firewall allow rule. Validate this export path without recording sandbox tokens. For directory-loaded configuration, an invalid individual hook is dropped and logged while valid siblings continue to load. Invalid JSON, an unsupported version, or a non-array event list rejects the entire file. Inline settings.json hooks are stricter: an item-level validation error rejects the whole hooks field.
Model availability note: The official source does not specify a selectable model ID.
Evidence and freshness
Evidence level: Documentation-verified
AI-assisted editorial content; verify current product details against the linked official sources.
Last verified: