mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-04-05 16:53:29 +08:00
fix: use CLI argument for hook phase detection in observe.sh
The observe.sh script receives "pre" or "post" as $1 from the hook config, but the Python code was looking for a "hook_type" field in the stdin JSON. Claude Code does NOT include "hook_type" in the JSON payload passed to hooks, so it always defaulted to "unknown", causing all observations to be recorded as "tool_complete" — PreToolUse events were never distinguished from PostToolUse. Fix: capture $1 as HOOK_PHASE and pass it to Python via env var. This also fixes TIMESTAMP export in the .cursor copy where inline `VAR=val cmd` syntax didn't propagate to the python subprocess.
This commit is contained in:
@@ -36,6 +36,9 @@
|
||||
|
||||
set -e
|
||||
|
||||
# Hook phase from CLI argument: "pre" (PreToolUse) or "post" (PostToolUse)
|
||||
HOOK_PHASE="${1:-post}"
|
||||
|
||||
CONFIG_DIR="${HOME}/.claude/homunculus"
|
||||
OBSERVATIONS_FILE="${CONFIG_DIR}/observations.jsonl"
|
||||
MAX_FILE_SIZE_MB=10
|
||||
@@ -57,15 +60,22 @@ if [ -z "$INPUT_JSON" ]; then
|
||||
fi
|
||||
|
||||
# Parse using python via stdin pipe (safe for all JSON payloads)
|
||||
PARSED=$(echo "$INPUT_JSON" | python3 -c '
|
||||
# Pass HOOK_PHASE via env var since Claude Code does not include hook type in stdin JSON
|
||||
PARSED=$(echo "$INPUT_JSON" | HOOK_PHASE="$HOOK_PHASE" python3 -c '
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
|
||||
# Determine event type from CLI argument passed via env var.
|
||||
# Claude Code does NOT include a "hook_type" field in the stdin JSON,
|
||||
# so we rely on the shell argument ("pre" or "post") instead.
|
||||
hook_phase = os.environ.get("HOOK_PHASE", "post")
|
||||
event = "tool_start" if hook_phase == "pre" else "tool_complete"
|
||||
|
||||
# Extract fields - Claude Code hook format
|
||||
hook_type = data.get("hook_type", "unknown") # PreToolUse or PostToolUse
|
||||
tool_name = data.get("tool_name", data.get("tool", "unknown"))
|
||||
tool_input = data.get("tool_input", data.get("input", {}))
|
||||
tool_output = data.get("tool_output", data.get("output", ""))
|
||||
@@ -82,9 +92,6 @@ try:
|
||||
else:
|
||||
tool_output_str = str(tool_output)[:5000]
|
||||
|
||||
# Determine event type
|
||||
event = "tool_start" if "Pre" in hook_type else "tool_complete"
|
||||
|
||||
print(json.dumps({
|
||||
"parsed": True,
|
||||
"event": event,
|
||||
|
||||
Reference in New Issue
Block a user