Files
boshu2__agentops/hooks/codex-parity-warn.sh
T
Boden Fuller 8f7e80a239 fix(hooks): add blocking mode to codex-parity-warn + cleanup stale worktrees
- codex-parity-warn.sh: opt-in blocking mode via AGENTOPS_CODEX_PARITY_BLOCK=1
  (exit 2 instead of 0 when codex counterpart needs sync). Default unchanged.
- Enabled git rerere for future conflict resolution memory
- Pruned 25 stale worktrees and deleted 8 orphaned local branches
- Synced embedded hook copy
2026-04-05 10:55:28 -04:00

72 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# codex-parity-warn.sh - PreToolUse hook (matcher: Edit)
# Warns when editing skills/ files that have a skills-codex/ counterpart needing sync.
# Non-blocking (always exit 0). Warning via additionalContext injection.
set -euo pipefail
# Kill switches
[[ "${AGENTOPS_HOOKS_DISABLED:-}" == "1" ]] && exit 0
[[ "${AGENTOPS_CODEX_PARITY_DISABLED:-}" == "1" ]] && exit 0
# Only fire on Edit tool
TOOL_NAME="${CLAUDE_TOOL_NAME:-}"
if [[ -z "$TOOL_NAME" ]]; then
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // ""' 2>/dev/null) || exit 0
fi
[[ "$TOOL_NAME" != "Edit" ]] && exit 0
# Find repo root
ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || exit 0
# Extract file path being edited
FILE_PATH="${CLAUDE_TOOL_INPUT_FILE_PATH:-}"
if [[ -z "$FILE_PATH" ]]; then
[[ -z "${INPUT:-}" ]] && INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // ""' 2>/dev/null) || exit 0
fi
[[ -z "$FILE_PATH" ]] && exit 0
# Normalize to relative path
REL_PATH="${FILE_PATH#"$ROOT/"}"
# Only fire for skills/ edits (not skills-codex/)
[[ "$REL_PATH" == skills/* ]] || exit 0
[[ "$REL_PATH" == skills-codex/* ]] && exit 0
# Extract skill name: skills/<name>/...
SKILL_NAME=$(echo "$REL_PATH" | cut -d'/' -f2)
[[ -z "$SKILL_NAME" ]] && exit 0
# Check if codex counterpart exists
CODEX_DIR="$ROOT/skills-codex/$SKILL_NAME"
[[ -d "$CODEX_DIR" ]] || exit 0
# Determine what kind of file was edited
EDITED_FILE=$(basename "$REL_PATH")
CODEX_COUNTERPART="$CODEX_DIR/$EDITED_FILE"
if [[ "$REL_PATH" == */references/* ]]; then
REF_FILE="${REL_PATH/skills\//skills-codex/}"
CODEX_COUNTERPART="$ROOT/$REF_FILE"
fi
# Build warning message
WARNING="Codex parity: skills-codex/$SKILL_NAME/ exists and may need sync."
if [[ -f "$CODEX_COUNTERPART" ]]; then
WARNING="$WARNING File exists at both locations — copy changes after editing."
fi
WARNING="$WARNING Run: scripts/regen-codex-hashes.sh after sync."
# Opt-in blocking mode: set AGENTOPS_CODEX_PARITY_BLOCK=1 to block edits
# that drift from codex copies. Default remains non-blocking (advisory).
if [[ "${AGENTOPS_CODEX_PARITY_BLOCK:-}" == "1" ]]; then
cat <<BLOCK
{"decision":"block","reason":"$WARNING"}
BLOCK
exit 2
fi
cat <<EOF
{"hookSpecificOutput":{"hookEventName":"PreToolUse","additionalContext":"$WARNING"}}
EOF