← back to Ventura Claw Leads
scripts/post-commit-hook.sh
61 lines
#!/usr/bin/env bash
# post-commit: fire codex-3way in background to debate this commit's diff.
# Non-blocking — never delays the commit.
#
# Skips commits where the diff is trivial (no .js/.ejs/.sql/.json changed,
# or fewer than 5 lines of effective code change). The codex-3way panel
# costs Mac2 RSS + a Kimi call — not worth burning on README tweaks.
set -u
REPO_DIR="$(git rev-parse --show-toplevel)"
SHA="$(git rev-parse HEAD)"
SHORT="$(git rev-parse --short HEAD)"
SKILL="$HOME/.claude/skills/codex-3way/scripts/run.sh"
LOG_DIR="$REPO_DIR/.git/codex-3way-hook"
mkdir -p "$LOG_DIR"
# Skip-rule 1: only run if at least one of these extensions changed.
if ! git diff --name-only HEAD~1..HEAD | grep -qE '\.(js|ejs|sql|ts|tsx|json|sh)$'; then
exit 0
fi
# Skip-rule 2: skip trivial commits (< 5 added+removed lines).
LINES="$(git diff --shortstat HEAD~1..HEAD | grep -oE '[0-9]+ insertions?|[0-9]+ deletions?' | grep -oE '[0-9]+' | awk '{s+=$1} END {print s+0}')"
if [ "${LINES:-0}" -lt 5 ]; then
exit 0
fi
# Skip-rule 3: don't recurse — if the commit was made BY the hook (defensive).
if [ -n "${CODEX_3WAY_HOOK_RUNNING:-}" ]; then
exit 0
fi
# Skip-rule 4: bail out cleanly if the skill isn't installed.
if [ ! -x "$SKILL" ]; then
echo "[post-commit] codex-3way not installed at $SKILL — skip" >&2
exit 0
fi
NAME="auto-${SHORT}"
LOG="$LOG_DIR/$NAME.log"
LATEST_SUMMARY="$LOG_DIR/latest.summary.md"
# Detach, no terminal, redirect everything. Don't block commit.
nohup bash -c "
CODEX_3WAY_HOOK_RUNNING=1 \
bash '$SKILL' --base HEAD~1 --rounds 1 --name '$NAME' --workdir '$REPO_DIR' >> '$LOG' 2>&1
# Find newest matching run dir and symlink its summary.md
RUN=\$(ls -td '$HOME/.claude/skills/codex-3way/runs/'*-'$NAME' 2>/dev/null | head -1)
if [ -n \"\$RUN\" ] && [ -f \"\$RUN/summary.md\" ]; then
ln -sf \"\$RUN/summary.md\" '$LATEST_SUMMARY'
echo \"[post-commit] codex-3way done for $SHORT — summary at $LATEST_SUMMARY\" >> '$LOG'
fi
" > /dev/null 2>&1 &
# Detach the bg process so the commit returns immediately.
disown 2>/dev/null || true
echo "[post-commit] codex-3way fired for $SHORT (background; tail $LOG)"
exit 0