Files
Claude-Code-Game-Studios/.claude/hooks/log-agent.sh
Donchitos 666e0fcb5a Fix log-agent hooks reading wrong field — audit trail always logged "unknown" (#21)
Both SubagentStart/SubagentStop hooks were extracting `.agent_name` from the
hook payload, but Claude Code emits the agent name in `agent_type`. This caused
every audit log entry to fall through to the "unknown" fallback, making the
entire agent audit trail useless.

Fix: swap `.agent_name` -> `.agent_type` in both the jq path and the grep/sed
fallback for systems without jq. Log output format is unchanged so existing
audit logs remain valid.

Bug reported and fix authored by @bobloy in issue #20:
https://github.com/Donchitos/Claude-Code-Game-Studios/issues/20

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 20:28:38 +10:00

30 lines
1.1 KiB
Bash

#!/bin/bash
# Claude Code SubagentStart hook: Log agent invocations for audit trail
# Tracks which agents are being used and when
#
# Input schema (SubagentStart) — per Claude Code hooks reference:
# { "session_id": "...", "agent_id": "agent-abc123", "agent_type": "Explore", ... }
#
# The agent name is in `agent_type`, NOT `agent_name`. Reading `.agent_name`
# returns null on every invocation, so the fallback "unknown" is always used
# and the audit trail captures nothing useful.
INPUT=$(cat)
# Parse agent name -- use jq if available, fall back to grep
if command -v jq >/dev/null 2>&1; then
AGENT_NAME=$(echo "$INPUT" | jq -r '.agent_type // "unknown"' 2>/dev/null)
else
AGENT_NAME=$(echo "$INPUT" | grep -oE '"agent_type"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/"agent_type"[[:space:]]*:[[:space:]]*"//;s/"$//')
[ -z "$AGENT_NAME" ] && AGENT_NAME="unknown"
fi
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
SESSION_LOG_DIR="production/session-logs"
mkdir -p "$SESSION_LOG_DIR" 2>/dev/null
echo "$TIMESTAMP | Agent invoked: $AGENT_NAME" >> "$SESSION_LOG_DIR/agent-audit.log" 2>/dev/null
exit 0