ia_dev/gitea-issues/agent-loop.sh
Nicolas Cantu 907807f4d6 Generic project config, deploy scripts, gitea-issues, no reverse dependency
**Motivations:**
- Single config file per project (projects/<id>/conf.json)
- Project must not depend on ia_dev; only ia_dev solicits the project

**Root causes:**
- N/A (evolution)

**Correctifs:**
- N/A

**Evolutions:**
- lib/project_config.sh: resolve PROJECT_SLUG from IA_PROJECT, .ia_project, ai_project_id; PROJECT_CONFIG_PATH = projects/<id>/conf.json
- projects/lecoffreio.json moved to projects/lecoffreio/conf.json; projects/ia_dev/conf.json added
- deploy: branch-align, bump-version, change-to-all-branches, pousse, deploy-by-script-to use PROJECT_ROOT/IA_DEV_ROOT and project_config.sh; SCRIPT_REAL for symlink-safe paths
- deploy/_lib: shared colors, env-map, ssh, git-flow
- gitea-issues: mail list/mark-read/get-thread/send-reply, thread log, agent-loop, wiki scripts; lib.sh loads project config
- README: principle no dependency from host project; invoke ./ia_dev/deploy/bump-version.sh etc. from repo root

**Pages affectées:**
- README.md, projects/README.md, lib/, deploy/, gitea-issues/, projects/lecoffreio/, projects/ia_dev/
2026-03-12 22:35:15 +01:00

87 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Agent loop: poll for unread mails periodically and maintain a witness file.
# Run from repo root. Use a fichier témoin (status) to know if the loop is active.
#
# Usage:
# ./gitea-issues/agent-loop.sh [interval_seconds]
# AGENT_LOOP_INTERVAL_SEC=120 ./gitea-issues/agent-loop.sh
#
# Witness file: logs/gitea-issues/agent-loop.status
# Updated every iteration. If mtime is older than 2*interval, consider the loop stopped.
# Pending file: logs/gitea-issues/agent-loop.pending
# Written when unread mails exist; contains timestamp and mail list. Clear after agent run.
#
# Optional: set AGENT_LOOP_RUN_AGENT=1 to run the Cursor Agent CLI when mails are detected.
# Requires Cursor Agent CLI (https://cursor.com/docs/cli/using). If "agent" is not in PATH, the loop only updates status/pending.
#
# Optional: AGENT_LOOP_MODEL=<model> to force the model (e.g. sonnet-4.6, gpt-5.4-low). Default: sonnet-4.6 to avoid Opus usage limits when running unattended.
#
set -euo pipefail
# Source user env so PATH includes ~/.local/bin (Cursor Agent CLI, etc.)
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"
GITEA_ISSUES_DIR="${GITEA_ISSUES_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" || ROOT="$(cd "${GITEA_ISSUES_DIR}/.." && pwd)"
export GITEA_ISSUES_DIR
export REPO_ROOT="${ROOT}"
cd "$ROOT"
# Load agent-loop parameters from .secrets (optional)
AGENT_LOOP_ENV="$ROOT/.secrets/gitea-issues/agent-loop.env"
if [ -r "$AGENT_LOOP_ENV" ]; then
set +u
# shellcheck source=/dev/null
source "$AGENT_LOOP_ENV"
set -u
fi
INTERVAL="${1:-${AGENT_LOOP_INTERVAL_SEC:-60}}"
STATUS_FILE="${AGENT_LOOP_STATUS_FILE:-$ROOT/logs/gitea-issues/agent-loop.status}"
PENDING_FILE="${AGENT_LOOP_PENDING_FILE:-$ROOT/logs/gitea-issues/agent-loop.pending}"
mkdir -p "$(dirname "$STATUS_FILE")"
write_status() {
local status="$1"
local detail="${2:-}"
printf "%s\n%s\n%s\n" "$(date -Iseconds)" "$status" "$detail" > "$STATUS_FILE"
}
while true; do
write_status "running" "interval=${INTERVAL}s"
out=""
if out=$(./gitea-issues/mail-list-unread.sh 2>&1); then
if echo "$out" | grep -q "UID="; then
write_status "mails_pending" "Des mails non lus. Lancer l'agent gitea-issues-process dans Cursor."
printf "%s\n%s\n%s\n%s\n" "$(date -Iseconds)" "mails_pending" "---" "$out" > "$PENDING_FILE"
echo "[agent-loop] $(date -Iseconds) — Mails non lus détectés. Lancer l'agent gitea-issues-process dans Cursor."
if [ "${AGENT_LOOP_RUN_AGENT:-0}" = "1" ] && command -v agent >/dev/null 2>&1; then
write_status "running_agent" "Lancement de l'agent Cursor pour traiter les mails."
echo "[agent-loop] $(date -Iseconds) — Lancement de l'agent Cursor (workflow gitea-issues-process mails)."
AGENT_MODEL="${AGENT_LOOP_MODEL:-sonnet-4.6}"
echo "[agent-loop] $(date -Iseconds) — Modèle: $AGENT_MODEL"
AGENT_OPTS=(-p "Exécute le workflow mails entrants de l'agent gitea-issues-process : les mails non lus viennent d'être détectés. 1) Pour chaque mail listé (voir contenu dans logs/gitea-issues/agent-loop.pending) : exécuter ./gitea-issues/mail-get-thread.sh <uid>, puis ./gitea-issues/mail-thread-log.sh init --uid <uid>, conserver THREAD_ID. 2) Pour chaque mail : rédiger une réponse (non technique, didactique, contexte LeCoffre.io ; pour bug demander l'environnement, pour évolution considérer test), envoyer avec ./gitea-issues/mail-send-reply.sh, puis ./gitea-issues/mail-thread-log.sh append-sent, puis ./gitea-issues/mail-mark-read.sh <uid>. 3) Uniquement branche test, ne pas modifier les agents ni scripts d'agents. Répondre à tous les mails avant de marquer comme lu." -f --model "$AGENT_MODEL")
if agent "${AGENT_OPTS[@]}" 2>&1; then
write_status "agent_done" "Agent terminé."
else
write_status "mails_pending" "Agent terminé avec erreur ou interruption. Relancer l'agent manuellement si besoin."
fi
fi
else
write_status "idle" "Aucun mail non lu."
if [ -f "$PENDING_FILE" ]; then
: > "$PENDING_FILE"
fi
fi
else
write_status "error" "mail-list-unread a échoué"
echo "[agent-loop] $(date -Iseconds) — Erreur mail-list-unread" >&2
fi
sleep "$INTERVAL"
done