# Explicit command lifecycle for Waterbrother zsh terminals. Loaded in new
# shells by the installer; no prompt/screen parsing and no terminal reattachment.
[[ -n "$WATERBROTHER_TERMINAL_ID" ]] || return 0
typeset -g _wb_notice_node="$1" _wb_notice_hook="$2" _wb_notice_running=0
autoload -Uz add-zsh-hook

_wb_notice_preexec() {
  local -a words
  words=(${(z)1})
  # Interactive agents report their own individual turns through agent hooks.
  case "${words[1]:t}" in grok|claude|codex|waterbrother) return 0 ;; esac
  _wb_notice_running=1
  WATERBROTHER_NOTICE_SOURCE="shell-$$" "$_wb_notice_node" "$_wb_notice_hook" started </dev/null >/dev/null 2>&1
  return 0
}

_wb_notice_precmd() {
  local command_status=$? notice_kind=finished
  if (( _wb_notice_running )); then
    _wb_notice_running=0
    if (( command_status == 130 || command_status == 143 )); then
      notice_kind=cancelled
    elif (( command_status != 0 )); then
      notice_kind=failed
    fi
    WATERBROTHER_NOTICE_SOURCE="shell-$$" "$_wb_notice_node" "$_wb_notice_hook" "$notice_kind" </dev/null >/dev/null 2>&1
  fi
  return $command_status
}

add-zsh-hook -d preexec _wb_notice_preexec
add-zsh-hook -d precmd _wb_notice_precmd
add-zsh-hook preexec _wb_notice_preexec
add-zsh-hook precmd _wb_notice_precmd
