Payload/scripts/kaden/harness/setup-harness-hooks.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Install Cursor and Copilot harness hook configs from repo templates.

.DESCRIPTION
    Copies .github/templates/cursor-hooks.json to .cursor/hooks.json
    and .github/templates/copilot-hooks.json to .github/hooks/harness.json.
    Installed commands start with pwsh and run
    scripts/kaden/harness/harness-stop-gate.ps1 (Cursor stop / Copilot sessionEnd)
    plus the same host for preToolUse and sessionStart. Strict validate
    when any slice claims ownership_status review_ready.

    Copilot cloud agent requires hook files on the default branch to run sessionEnd.

.EXAMPLE
    .\scripts\kaden\harness\setup-harness-hooks.ps1
#>

[CmdletBinding()]
param(
    [string]$RepoRoot = ''
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Import-Module (Join-Path $PSScriptRoot (Join-Path 'lib' 'HarnessContract.psm1')) -Force

if ([string]::IsNullOrWhiteSpace($RepoRoot)) {
    $RepoRoot = Get-HarnessRepoRoot -StartPath $PSScriptRoot
}

$template = Join-HarnessPath -Root $RepoRoot -Child '.github/templates/cursor-hooks.json'
$copilotTemplate = Join-HarnessPath -Root $RepoRoot -Child '.github/templates/copilot-hooks.json'
$cursorHooks = Join-HarnessPath -Root $RepoRoot -Child '.cursor/hooks.json'
$cursorDir = Join-HarnessPath -Root $RepoRoot -Child '.cursor'
$copilotHooksDir = Join-HarnessPath -Root $RepoRoot -Child '.github/hooks'
$copilotHooks = Join-HarnessPath -Root $RepoRoot -Child '.github/hooks/harness.json'

if (-not (Test-Path -LiteralPath $template)) {
    throw "Template not found: $template"
}
if (-not (Test-Path -LiteralPath $cursorDir)) {
    New-Item -ItemType Directory -Path $cursorDir -Force | Out-Null
}

Copy-Item -LiteralPath $template -Destination $cursorHooks -Force
Write-Output "Installed Cursor harness stop hook: $cursorHooks"

if (Test-Path -LiteralPath $copilotTemplate) {
    if (-not (Test-Path -LiteralPath $copilotHooksDir)) {
        New-Item -ItemType Directory -Path $copilotHooksDir -Force | Out-Null
    }
    Copy-Item -LiteralPath $copilotTemplate -Destination $copilotHooks -Force
    Write-Output "Installed Copilot harness hooks: $copilotHooks"
    Write-Output "Copilot cloud agent requires hooks on the default branch."
}
else {
    Write-Warning "Copilot hooks template not found: $copilotTemplate"
}

Write-Output "Gate runs STRICT only when a slice has ownership_status review_ready."