Shell
#!/bin/bash
# =============================================================================
# METACOGNITIVE CHECK HOOK
# Agentic Architecture Pattern #17: Reflexive Metacognitive
#
# Prompts confidence assessment for complex or ambiguous requests.
# Encourages "I don't know" when appropriate.
# =============================================================================
HOOK_DIR="$(dirname "$0")"
LOG_FILE="$HOME/.claude/debug/metacognitive.log"
CONFIDENCE_FILE="$HOME/.claude/session-env/confidence-context.json"
mkdir -p "$(dirname "$LOG_FILE")"
mkdir -p "$(dirname "$CONFIDENCE_FILE")"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
# Get the user's message from stdin or environment
USER_MESSAGE="${CLAUDE_USER_MESSAGE:-}"
# If no message provided, try to read from stdin
if [[ -z "$USER_MESSAGE" ]]; then
if [[ -p /dev/stdin ]]; then
USER_MESSAGE=$(cat)
fi
fi
# Skip if no message
if [[ -z "$USER_MESSAGE" ]]; then
exit 0
fi
log "Analyzing message for metacognitive signals..."
# =============================================================================
# DETECT HIGH-UNCERTAINTY PATTERNS
# =============================================================================
# Keywords that suggest high-stakes decisions
HIGH_STAKES_KEYWORDS=(
"production"
"deploy"
"security"
"authentication"
"payment"
"financial"
"medical"
"legal"
"compliance"
"HIPAA"
"GDPR"
"PCI"
"delete all"
"drop table"
"migrate"
)
# Keywords that suggest speculation or uncertainty is appropriate
SPECULATIVE_KEYWORDS=(
"best practice"
"should I"
"is it better"
"what's the right way"
"how would you"
"opinion"
"recommend"
"future"
"predict"
"trend"
"will this work"
)
# Keywords that suggest the user expects certainty
CERTAINTY_EXPECTED=(
"exactly"
"precisely"
"guarantee"
"always"
"never"
"definitely"
"100%"
"for sure"
)
# =============================================================================
# ANALYZE MESSAGE
# =============================================================================
CONFIDENCE_LEVEL="high"
CONFIDENCE_SIGNALS=()
METACOGNITIVE_PROMPT=""
MESSAGE_LOWER=$(echo "$USER_MESSAGE" | tr '[:upper:]' '[:lower:]')
# Check for high-stakes keywords
for keyword in "${HIGH_STAKES_KEYWORDS[@]}"; do
if [[ "$MESSAGE_LOWER" == *"$keyword"* ]]; then
CONFIDENCE_SIGNALS+=("high-stakes-domain:$keyword")
CONFIDENCE_LEVEL="requires-verification"
log "Detected high-stakes keyword: $keyword"
fi
done
# Check for speculative keywords
for keyword in "${SPECULATIVE_KEYWORDS[@]}"; do
if [[ "$MESSAGE_LOWER" == *"$keyword"* ]]; then
CONFIDENCE_SIGNALS+=("speculative-request:$keyword")
if [[ "$CONFIDENCE_LEVEL" != "requires-verification" ]]; then
CONFIDENCE_LEVEL="medium"
fi
log "Detected speculative keyword: $keyword"
fi
done
# Check for certainty expectations
for keyword in "${CERTAINTY_EXPECTED[@]}"; do
if [[ "$MESSAGE_LOWER" == *"$keyword"* ]]; then
CONFIDENCE_SIGNALS+=("certainty-expected:$keyword")
log "Detected certainty expectation: $keyword"
fi
done
# =============================================================================
# GENERATE METACOGNITIVE GUIDANCE
# =============================================================================
case "$CONFIDENCE_LEVEL" in
"requires-verification")
METACOGNITIVE_PROMPT="METACOGNITIVE_ALERT: This request involves high-stakes domain ($keyword). Express confidence levels explicitly. If uncertain, say so. Consider: 'I'm confident about X, but less certain about Y because...'"
;;
"medium")
METACOGNITIVE_PROMPT="METACOGNITIVE_NOTE: This appears to be a speculative or opinion-based question. Consider offering multiple perspectives and expressing uncertainty where appropriate."
;;
*)
# No special guidance needed
METACOGNITIVE_PROMPT=""
;;
esac
# =============================================================================
# SAVE CONTEXT FOR CLAUDE
# =============================================================================
SIGNALS_JSON=$(printf '%s\n' "${CONFIDENCE_SIGNALS[@]}" | jq -R . | jq -s .)
cat > "$CONFIDENCE_FILE" << EOF
{
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"confidence_level": "$CONFIDENCE_LEVEL",
"signals": $SIGNALS_JSON,
"metacognitive_prompt": "$METACOGNITIVE_PROMPT"
}
EOF
# =============================================================================
# OUTPUT TO CLAUDE
# =============================================================================
if [[ -n "$METACOGNITIVE_PROMPT" ]]; then
echo "$METACOGNITIVE_PROMPT"
fi
exit 0