mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-03-30 13:43:26 +08:00
fix: sync .cursor observe.sh and fix suggest-compact.sh session tracking
- Sync .cursor/observe.sh with corrected main version (use stdin pipe instead of broken heredoc json.loads pattern) - Fix suggest-compact.sh to use CLAUDE_SESSION_ID instead of $$ which gives a new PID per invocation, preventing counter from incrementing
This commit is contained in:
@@ -56,20 +56,20 @@ if [ -z "$INPUT_JSON" ]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Parse using python (more reliable than jq for complex JSON)
|
# Parse using python via stdin pipe (safe for all JSON payloads)
|
||||||
PARSED=$(python3 << EOF
|
PARSED=$(echo "$INPUT_JSON" | python3 -c '
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads('''$INPUT_JSON''')
|
data = json.load(sys.stdin)
|
||||||
|
|
||||||
# Extract fields - Claude Code hook format
|
# Extract fields - Claude Code hook format
|
||||||
hook_type = data.get('hook_type', 'unknown') # PreToolUse or PostToolUse
|
hook_type = data.get("hook_type", "unknown") # PreToolUse or PostToolUse
|
||||||
tool_name = data.get('tool_name', data.get('tool', 'unknown'))
|
tool_name = data.get("tool_name", data.get("tool", "unknown"))
|
||||||
tool_input = data.get('tool_input', data.get('input', {}))
|
tool_input = data.get("tool_input", data.get("input", {}))
|
||||||
tool_output = data.get('tool_output', data.get('output', ''))
|
tool_output = data.get("tool_output", data.get("output", ""))
|
||||||
session_id = data.get('session_id', 'unknown')
|
session_id = data.get("session_id", "unknown")
|
||||||
|
|
||||||
# Truncate large inputs/outputs
|
# Truncate large inputs/outputs
|
||||||
if isinstance(tool_input, dict):
|
if isinstance(tool_input, dict):
|
||||||
@@ -83,20 +83,19 @@ try:
|
|||||||
tool_output_str = str(tool_output)[:5000]
|
tool_output_str = str(tool_output)[:5000]
|
||||||
|
|
||||||
# Determine event type
|
# Determine event type
|
||||||
event = 'tool_start' if 'Pre' in hook_type else 'tool_complete'
|
event = "tool_start" if "Pre" in hook_type else "tool_complete"
|
||||||
|
|
||||||
print(json.dumps({
|
print(json.dumps({
|
||||||
'parsed': True,
|
"parsed": True,
|
||||||
'event': event,
|
"event": event,
|
||||||
'tool': tool_name,
|
"tool": tool_name,
|
||||||
'input': tool_input_str if event == 'tool_start' else None,
|
"input": tool_input_str if event == "tool_start" else None,
|
||||||
'output': tool_output_str if event == 'tool_complete' else None,
|
"output": tool_output_str if event == "tool_complete" else None,
|
||||||
'session': session_id
|
"session": session_id
|
||||||
}))
|
}))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(json.dumps({'parsed': False, 'error': str(e)}))
|
print(json.dumps({"parsed": False, "error": str(e)}))
|
||||||
EOF
|
')
|
||||||
)
|
|
||||||
|
|
||||||
# Check if parsing succeeded
|
# Check if parsing succeeded
|
||||||
PARSED_OK=$(echo "$PARSED" | python3 -c "import json,sys; print(json.load(sys.stdin).get('parsed', False))")
|
PARSED_OK=$(echo "$PARSED" | python3 -c "import json,sys; print(json.load(sys.stdin).get('parsed', False))")
|
||||||
@@ -104,7 +103,11 @@ PARSED_OK=$(echo "$PARSED" | python3 -c "import json,sys; print(json.load(sys.st
|
|||||||
if [ "$PARSED_OK" != "True" ]; then
|
if [ "$PARSED_OK" != "True" ]; then
|
||||||
# Fallback: log raw input for debugging
|
# Fallback: log raw input for debugging
|
||||||
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||||
echo "{\"timestamp\":\"$timestamp\",\"event\":\"parse_error\",\"raw\":$(echo "$INPUT_JSON" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()[:1000]))')}" >> "$OBSERVATIONS_FILE"
|
echo "$INPUT_JSON" | python3 -c "
|
||||||
|
import json, sys
|
||||||
|
raw = sys.stdin.read()[:2000]
|
||||||
|
print(json.dumps({'timestamp': '$timestamp', 'event': 'parse_error', 'raw': raw}))
|
||||||
|
" >> "$OBSERVATIONS_FILE"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -121,10 +124,10 @@ fi
|
|||||||
# Build and write observation
|
# Build and write observation
|
||||||
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||||
|
|
||||||
python3 << EOF
|
echo "$PARSED" | python3 -c "
|
||||||
import json
|
import json, sys
|
||||||
|
|
||||||
parsed = json.loads('''$PARSED''')
|
parsed = json.load(sys.stdin)
|
||||||
observation = {
|
observation = {
|
||||||
'timestamp': '$timestamp',
|
'timestamp': '$timestamp',
|
||||||
'event': parsed['event'],
|
'event': parsed['event'],
|
||||||
@@ -139,7 +142,7 @@ if parsed['output']:
|
|||||||
|
|
||||||
with open('$OBSERVATIONS_FILE', 'a') as f:
|
with open('$OBSERVATIONS_FILE', 'a') as f:
|
||||||
f.write(json.dumps(observation) + '\n')
|
f.write(json.dumps(observation) + '\n')
|
||||||
EOF
|
"
|
||||||
|
|
||||||
# Signal observer if running
|
# Signal observer if running
|
||||||
OBSERVER_PID_FILE="${CONFIG_DIR}/.observer.pid"
|
OBSERVER_PID_FILE="${CONFIG_DIR}/.observer.pid"
|
||||||
|
|||||||
@@ -28,7 +28,9 @@
|
|||||||
# - Plan has been finalized
|
# - Plan has been finalized
|
||||||
|
|
||||||
# Track tool call count (increment in a temp file)
|
# Track tool call count (increment in a temp file)
|
||||||
COUNTER_FILE="/tmp/claude-tool-count-$$"
|
# Use CLAUDE_SESSION_ID for session-specific counter (not $$ which changes per invocation)
|
||||||
|
SESSION_ID="${CLAUDE_SESSION_ID:-${PPID:-default}}"
|
||||||
|
COUNTER_FILE="/tmp/claude-tool-count-${SESSION_ID}"
|
||||||
THRESHOLD=${COMPACT_THRESHOLD:-50}
|
THRESHOLD=${COMPACT_THRESHOLD:-50}
|
||||||
|
|
||||||
# Initialize or increment counter
|
# Initialize or increment counter
|
||||||
|
|||||||
Reference in New Issue
Block a user