From addbf37fc5ba80b26998e46b59532139551bea8e Mon Sep 17 00:00:00 2001 From: FragginWagon Date: Tue, 30 Jun 2026 17:30:20 -0400 Subject: [PATCH] Add SessionStop hook and session-events.tsv duration tracking - session-audit.json: add SessionStop entry pointing to new end-of-session scripts - report-hook-event-end.sh/.ps1: log payload to hook-events.log and write TSV row to session-events.tsv - report-hook-event.sh/.ps1: also write SessionStart TSV row to session-events.tsv so start/end pairs are queryable for duration --- resources/hooks/session-audit.json | 8 ++++++++ resources/scripts/report-hook-event-end.ps1 | 12 ++++++++++++ resources/scripts/report-hook-event-end.sh | 14 ++++++++++++++ resources/scripts/report-hook-event.ps1 | 3 +++ resources/scripts/report-hook-event.sh | 3 +++ 5 files changed, 40 insertions(+) create mode 100644 resources/scripts/report-hook-event-end.ps1 create mode 100755 resources/scripts/report-hook-event-end.sh diff --git a/resources/hooks/session-audit.json b/resources/hooks/session-audit.json index 0d45796..c3eb7ce 100644 --- a/resources/hooks/session-audit.json +++ b/resources/hooks/session-audit.json @@ -7,6 +7,14 @@ "linux": "bash ~/.copilot-resources/resources/scripts/report-hook-event.sh", "windows": "powershell -NoProfile -File \"$HOME/.copilot-resources/resources/scripts/report-hook-event.ps1\"" } + ], + "SessionStop": [ + { + "type": "command", + "osx": "bash ~/.copilot-resources/resources/scripts/report-hook-event-end.sh", + "linux": "bash ~/.copilot-resources/resources/scripts/report-hook-event-end.sh", + "windows": "powershell -NoProfile -File \"$HOME/.copilot-resources/resources/scripts/report-hook-event-end.ps1\"" + } ] } } diff --git a/resources/scripts/report-hook-event-end.ps1 b/resources/scripts/report-hook-event-end.ps1 new file mode 100644 index 0000000..3338ab6 --- /dev/null +++ b/resources/scripts/report-hook-event-end.ps1 @@ -0,0 +1,12 @@ +$ErrorActionPreference = 'Stop' + +$StateDir = if ($env:COPILOT_RESOURCES_STATE_DIR) { $env:COPILOT_RESOURCES_STATE_DIR } else { Join-Path $HOME '.copilot-resources-state' } +New-Item -ItemType Directory -Path $StateDir -Force | Out-Null + +$Payload = [Console]::In.ReadToEnd() +$Payload | Add-Content -LiteralPath (Join-Path $StateDir 'hook-events.log') + +# Append a TSV row to session-events.tsv for duration tracking. +"$(Get-Date -AsUTC -Format o)`tSessionEnd" | Add-Content -LiteralPath (Join-Path $StateDir 'session-events.tsv') + +'{"continue": true}' diff --git a/resources/scripts/report-hook-event-end.sh b/resources/scripts/report-hook-event-end.sh new file mode 100755 index 0000000..a79a8fc --- /dev/null +++ b/resources/scripts/report-hook-event-end.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -euo pipefail + +state_dir="${COPILOT_RESOURCES_STATE_DIR:-$HOME/.copilot-resources-state}" +mkdir -p -- "$state_dir" + +event_payload="$(cat)" +printf '%s\n' "$event_payload" >> "$state_dir/hook-events.log" + +# Append a TSV row to session-events.tsv for duration tracking. +printf '%s\tSessionEnd\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$state_dir/session-events.tsv" + +printf '{"continue": true}\n' diff --git a/resources/scripts/report-hook-event.ps1 b/resources/scripts/report-hook-event.ps1 index a30515b..3653aad 100644 --- a/resources/scripts/report-hook-event.ps1 +++ b/resources/scripts/report-hook-event.ps1 @@ -6,6 +6,9 @@ New-Item -ItemType Directory -Path $StateDir -Force | Out-Null $Payload = [Console]::In.ReadToEnd() $Payload | Add-Content -LiteralPath (Join-Path $StateDir 'hook-events.log') +# Append a TSV row to session-events.tsv for duration tracking. +"$(Get-Date -AsUTC -Format o)`tSessionStart" | Add-Content -LiteralPath (Join-Path $StateDir 'session-events.tsv') + $PortRegistryScript = Join-Path $PSScriptRoot 'update-port-registry.mjs' $NodeCommand = Get-Command node -ErrorAction SilentlyContinue if ($NodeCommand -and (Test-Path -LiteralPath $PortRegistryScript)) { diff --git a/resources/scripts/report-hook-event.sh b/resources/scripts/report-hook-event.sh index cddcb27..3b88826 100755 --- a/resources/scripts/report-hook-event.sh +++ b/resources/scripts/report-hook-event.sh @@ -10,6 +10,9 @@ mkdir -p -- "$state_dir" event_payload="$(cat)" printf '%s\n' "$event_payload" >> "$state_dir/hook-events.log" +# Append a TSV row to session-events.tsv for duration tracking. +printf '%s\tSessionStart\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$state_dir/session-events.tsv" + if command -v node >/dev/null 2>&1 && [[ -f "$port_registry_script" ]]; then if ! printf '%s' "$event_payload" | node "$port_registry_script"; then printf '%s update-port-registry failed\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$state_dir/project-ports-errors.log"