#!/bin/sh
# Start a woken session's Claude CLI with the names it was not given ABSENT.
#
# The Agent SDK lays the session's environment over the daemon's own, so a
# name the daemon carries can only be withheld by setting it empty.  Empty is
# not absent to every reader:  azure-identity counts an empty AZURE_CLIENT_ID
# as set, builds a credential from it and raises before DefaultAzureCredential
# reaches the CLI credential (kar-azure-env-cred).  The spawner lists those
# names in SAVI_COMMS_UNSET and the SDK starts this script as the CLI, so the
# real CLI and everything it runs see the names absent.
#
# A listed name is removed only while it is empty, so a credential that was
# deliberately provisioned to the session is never taken away here.
for name in ${SAVI_COMMS_UNSET:-}; do
  case "$name" in
    ''|*[!A-Za-z0-9_]*) continue ;;
  esac
  if [ -z "$(printenv "$name")" ]; then
    unset "$name"
  fi
done
unset SAVI_COMMS_UNSET

cli="${SAVI_COMMS_SESSION_CLI:-}"
unset SAVI_COMMS_SESSION_CLI
if [ -z "$cli" ]; then
  cli="$(command -v claude)" || {
    echo "session-exec:  no Claude CLI named and none on PATH" >&2
    exit 127
  }
fi
exec "$cli" "$@"
