Merge pull request #242 from t-s-li/fix/observe-hook-phase-detection

LGTM — fixes hook phase detection bug in observe.sh. Confirmed safe, no external dependencies.
This commit is contained in:
Affaan Mustafa
2026-02-24 09:23:45 -08:00
committed by GitHub
119 changed files with 14 additions and 25942 deletions

View File

@@ -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,
@@ -137,9 +144,9 @@ observation = {
'session': parsed['session']
}
if parsed['input']:
if parsed['input'] is not None:
observation['input'] = parsed['input']
if parsed['output']:
if parsed['output'] is not None:
observation['output'] = parsed['output']
print(json.dumps(observation))