ia_dev/ai_working_help/notary-ai/notary-ai-wait-agent-then-loop-10h.sh

90 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Bloquant (premier plan uniquement) :
# 1) Attente illimitée jusqu'à ce que l'agent notaire et les scripts de boucle se soient arrêtés
# d'eux-mêmes — pas de screen, pas de &, pas de détachement : le shell courant reste bloqué.
# 2) Ensuite relance la boucle (notary-ai-loop-n-cycles.sh) au premier plan pendant 10 h :
# chaque itération exécute un cycle ; l'agent n'est invoqué que dans ce script, en foreground.
#
# Usage (un terminal dédié — la session reste occupée jusqu'à la fin des 10 h) :
# export AI_AGENT_TOKEN=... # ou MAIL_TO
# cd /path/to/ia_dev && ./ai_working_help/notary-ai/notary-ai-wait-agent-then-loop-10h.sh
#
# Env optionnel :
# NOTARY_AI_WAIT_SKIP_DAEMON_KILL=1 — ne envoie pas SIGTERM au daemon au démarrage (défaut : tuer
# le daemon pour éviter deux boucles en parallèle pendant l'attente ; l'agent CLI n'est jamais killé).
# NOTARY_AI_WAIT_HEARTBEAT_SEC — message tous les N secondes pendant l'attente (défaut : 20).
#
set -euo pipefail
if [ -n "${HOME:-}" ] && [ -r "$HOME/.bashrc" ]; then
set +u
# shellcheck source=/dev/null
source "$HOME/.bashrc" 2>/dev/null || true
set -u
fi
[ -n "${HOME:-}" ] && [ -d "$HOME/.local/bin" ] && export PATH="$HOME/.local/bin:$PATH"
NOTARY_AI_DIR="${NOTARY_AI_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
IA_DEV_ROOT="$(cd "${NOTARY_AI_DIR}/../.." && pwd)"
cd "$IA_DEV_ROOT"
if [[ -z "${MAIL_TO:-}" && -z "${AI_AGENT_TOKEN:-}" ]]; then
if [[ -f "${IA_DEV_ROOT}/projects/lecoffreio/.secrets/test/ia_token" ]]; then
AI_AGENT_TOKEN="$(tr -d ' \n\r\t' < "${IA_DEV_ROOT}/projects/lecoffreio/.secrets/test/ia_token")"
export AI_AGENT_TOKEN
fi
fi
if [[ -z "${MAIL_TO:-}" && -z "${AI_AGENT_TOKEN:-}" ]]; then
echo "[notary-ai-wait-then-10h] Définir MAIL_TO ou AI_AGENT_TOKEN (ou ia_token test présent)." >&2
exit 1
fi
if [[ "${NOTARY_AI_WAIT_SKIP_DAEMON_KILL:-0}" == "1" ]]; then
echo "[$(date -Iseconds)] NOTARY_AI_WAIT_SKIP_DAEMON_KILL=1 — aucun SIGTERM vers notary-ai-loop-daemon."
else
echo "[$(date -Iseconds)] Arrêt du daemon notary-ai-loop-daemon (SIGTERM) s'il existe…"
pkill -TERM -f 'notary-ai-loop-daemon\.sh' 2>/dev/null || true
sleep 3
fi
LOOP_SCRIPT="${IA_DEV_ROOT}/ai_working_help/notary-ai/notary-ai-loop-n-cycles.sh"
SLEEP_BETWEEN="${NOTARY_AI_LOOP_DAEMON_SLEEP_SEC:-15}"
DURATION_SEC=$((10 * 3600))
HEARTBEAT_SEC="${NOTARY_AI_WAIT_HEARTBEAT_SEC:-20}"
# True tant qu'un composant « notary-ai » bloquant tourne encore (agent CLI, cycles, daemon).
notary_ai_blocking_work_running() {
if pgrep -f 'notary-ai-loop-n-cycles\.sh' >/dev/null 2>&1; then
return 0
fi
if pgrep -f 'notary-ai-loop-daemon\.sh' >/dev/null 2>&1; then
return 0
fi
# Cursor CLI : argv inclut en pratique « notary-ai » (prompt notary-ai-process).
if pgrep -af 'agent' 2>/dev/null | grep -qE 'notary-ai|notary_ai'; then
return 0
fi
return 1
}
echo "[$(date -Iseconds)] Attente bloquante sans limite de durée : arrêt naturel de l'agent / des boucles (ce shell ne rend pas la main)."
last_hb="$(date +%s)"
while notary_ai_blocking_work_running; do
sleep 2
now="$(date +%s)"
if (( now - last_hb >= HEARTBEAT_SEC )); then
echo "[$(date -Iseconds)] … toujours en attente (processus notary-ai ou agent encore actif) — attente illimitée."
last_hb="$now"
fi
done
echo "[$(date -Iseconds)] Plus d'agent notaire ni de boucle notary-ai active — relance boucle 10 h au premier plan (sans background)."
END_TS=$(($(date +%s) + DURATION_SEC))
echo "[$(date -Iseconds)] Fin prévue après 10 h : $(date -d "@${END_TS}" -Iseconds 2>/dev/null || date -r "${END_TS}" -Iseconds 2>/dev/null || echo "${END_TS}")"
while [[ $(date +%s) -lt ${END_TS} ]]; do
bash "${LOOP_SCRIPT}" 1 || true
sleep "${SLEEP_BETWEEN}"
done
echo "[$(date -Iseconds)] 10 h écoulées — fin du script."