#!/bin/zsh -f

# Exact selected-process boundary. Recovery intentionally relaunches without
# reconstructing a whitespace-lossy argv string; Claude restores its own state.

sc_verify_process_identity() {
	local app=${1:A} pid=$2 uid=$3 executable=${4:A}
	[[ "$pid" == <1-> ]] || sc_fail 'Invalid Claude process ID.'
	[[ "$uid" == "$EUID" ]] || sc_fail 'Claude belongs to a different user.'
	[[ "$executable" == "$app/Contents/MacOS/Claude" ]] || sc_fail 'PID does not belong to the exact selected Claude executable.'
}

sc_verify_process() {
	local app=${1:A} pid=$2 uid executable
	[[ "$pid" == <1-> ]] || sc_fail 'Invalid Claude process ID.'
	uid=$(/bin/ps -p "$pid" -o uid= 2>/dev/null | /usr/bin/tr -d ' ')
	executable=$(/bin/ps -p "$pid" -o comm= 2>/dev/null)
	sc_verify_process_identity "$app" "$pid" "$uid" "$executable"
}
