Files
planning-with-files/.github/hooks/scripts/pre-tool-use.ps1
T
OthmanAdi ee6021bcfb fix: honor the planning opt-out on the Cursor route and keep the disabled Copilot hook neutral
The #195 opt-out lives in scripts/inject-plan.sh, and the Cursor hooks read
task_plan.md directly instead of dispatching to it, so PLANNING_DISABLED=1
never reached any of the eight. Each guard reproduces its own hook's
no-plan-file output, so the Cursor protocol shape is unchanged: PreToolUse
still answers {"decision": "allow"} because that is what it emits
unconditionally today, and the other three stay silent.

Three defects found while auditing #222, #223 and #224:

- The Copilot PreToolUse guard answered permissionDecision "allow" on the
  disabled path, so setting the variable to make planning inert also handed
  every tool call a blanket approval. Both copies now emit {}, matching
  their own no-plan-file path, which returns the decision to Copilot.
- .cursor/hooks/stop.ps1 was the last copy the #191 zero-phase guard never
  reached. It answered "0/0 phases done" and auto-continued on a plan that
  was never phase-structured.
- .github/hooks/scripts/error-occurred.ps1 read stdin into $input, which is
  PowerShell's automatic pipeline variable. Under -File the assignment does
  not stick, so the JSON parse always saw an empty string and the hook has
  never logged an error on Windows. Reproduced under pwsh 7 and Windows
  PowerShell 5.1 before the rename.

The opt-out tests now run every hook twice, unset and set, and fail if the
unset run is already inert. The merged versions asserted only the disabled
run, which a fleet gutted to unconditional {} also passed. That baseline is
what caught the $input bug.
2026-08-19 19:27:00 +02:00

41 lines
1.2 KiB
PowerShell

# planning-with-files: Pre-tool-use hook for GitHub Copilot (PowerShell)
# Reads the first 30 lines of task_plan.md to keep goals in context.
# Always allows tool execution — this hook never blocks tools.
# Always exits 0 — outputs JSON to stdout.
# Read stdin (required — Copilot pipes JSON to stdin)
$OutputEncoding = [System.Text.UTF8Encoding]::new($false)
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
$InputData = [Console]::In.ReadToEnd()
# Disabled means no opinion, not blanket approval: emit the same empty object the
# no-plan-file path emits so Copilot's own permission flow decides.
if ($env:PLANNING_DISABLED -eq '1') {
Write-Output '{}'
exit 0
}
$PlanFile = "task_plan.md"
if (-not (Test-Path $PlanFile)) {
Write-Output '{}'
exit 0
}
$Context = (Get-Content $PlanFile -TotalCount 30 -Encoding UTF8 -ErrorAction SilentlyContinue) -join "`n"
if (-not $Context) {
Write-Output '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"}}'
exit 0
}
$output = @{
hookSpecificOutput = @{
hookEventName = "PreToolUse"
permissionDecision = "allow"
additionalContext = $Context
}
}
$output | ConvertTo-Json -Depth 3 -Compress
exit 0