netgescon-day0/scripts/ai/save_periodic_summary.sh

133 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT_DIR"
TOPIC="${1:-Filo Logico Contabilita}"
MODE="${2:-both}" # daily|weekly|both
DATE_TODAY="$(date +%F)"
WEEK_TAG="$(date +%G-W%V)"
NOW="$(date '+%Y-%m-%d %H:%M:%S')"
TOPIC_SLUG="$(echo "$TOPIC" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//;s/-$//')"
TOPIC_FILE="docs/ai/topics/${TOPIC_SLUG}.md"
DAILY_FILE="docs/ai/summaries/daily/${DATE_TODAY}.md"
WEEKLY_FILE="docs/ai/summaries/weekly/${WEEK_TAG}.md"
mkdir -p "docs/ai/summaries/daily" "docs/ai/summaries/weekly"
latest_checkpoint_raw="$(awk '
/^## CHECKPOINT / {line=NR}
{lines[NR]=$0}
END {
if (line == 0) exit
for (i=line; i<=NR; i++) print lines[i]
}
' docs/ai/SESSION_HANDOFF.md 2>/dev/null || true)"
latest_checkpoint="$(awk '
/^### File modificati/ {print "- File modificati: (omessi nel summary periodico)"; exit}
NR<=40 {print}
' <<< "$latest_checkpoint_raw")"
if [[ -z "$latest_checkpoint" ]]; then
latest_checkpoint="(Nessun CHECKPOINT trovato in docs/ai/SESSION_HANDOFF.md)"
elif [[ "$(printf '%s\n' "$latest_checkpoint_raw" | wc -l)" -gt 40 ]]; then
latest_checkpoint+=$'\n\n... (estratto troncato, vedi SESSION_HANDOFF completo)'
fi
git_short_raw="$(git status --short 2>/dev/null || true)"
if [[ -z "$git_short_raw" ]]; then
git_short="(working tree pulita)"
git_stats="M=0 A=0 D=0 R=0 ??=0"
else
git_short="$(printf '%s\n' "$git_short_raw" | awk 'NR<=120{print}')"
if [[ "$(printf '%s\n' "$git_short_raw" | wc -l)" -gt 120 ]]; then
git_short+=$'\n... (troncato)'
fi
cnt_m="$(printf '%s\n' "$git_short_raw" | grep -Ec '^[ MARC][MARC] ' || true)"
cnt_a="$(printf '%s\n' "$git_short_raw" | grep -Ec '^A ' || true)"
cnt_d="$(printf '%s\n' "$git_short_raw" | grep -Ec '^D ' || true)"
cnt_r="$(printf '%s\n' "$git_short_raw" | grep -Ec '^R ' || true)"
cnt_u="$(printf '%s\n' "$git_short_raw" | grep -Ec '^\?\? ' || true)"
git_stats="M=${cnt_m} A=${cnt_a} D=${cnt_d} R=${cnt_r} ??=${cnt_u}"
git_focus_raw="$(printf '%s\n' "$git_short_raw" | grep -E ' (app/|resources/|routes/|config/|database/|docs/ai/|scripts/ai/|\.vscode/|composer\.json|package\.json)' || true)"
git_focus="$(printf '%s\n' "$git_focus_raw" | awk 'NR<=80{print}')"
if [[ -z "$git_focus" ]]; then
git_focus="$git_short"
fi
fi
latest_two_hint="docs/ai/LATEST_2_CHATS_RECOVERY.md"
write_summary() {
local file_path="$1"
local period_label="$2"
cat > "$file_path" <<EOF
# NetGescon ${period_label} Summary - ${NOW}
## Topic attivo
- ${TOPIC}
- Topic file: ${TOPIC_FILE}
## File baseline da leggere per ripartenza
1. docs/ai/SESSION_HANDOFF.md
2. docs/ai/TOPICS_INDEX.md
3. ${latest_two_hint}
4. ${TOPIC_FILE}
5. docs/ai/START-HERE-AGENT.md
## Ultimo checkpoint (estratto)
${latest_checkpoint}
## Stato repository (git status --short)
- Sintesi: ${git_stats}
### File focus (sviluppo corrente)
\`\`\`
${git_focus}
\`\`\`
### Working tree (estratto)
\`\`\`
${git_short}
\`\`\`
## Prossimo step operativo
- Aggiorna questo punto alla fine della sessione con il next action concreto.
## Prompt rapido di riapertura
Riparti da docs/ai/SESSION_HANDOFF.md, docs/ai/TOPICS_INDEX.md, ${latest_two_hint} e ${TOPIC_FILE}. Continua dal prossimo step senza rifare analisi generale.
EOF
}
case "$MODE" in
daily)
write_summary "$DAILY_FILE" "Daily"
echo "Saved: $DAILY_FILE"
;;
weekly)
write_summary "$WEEKLY_FILE" "Weekly"
echo "Saved: $WEEKLY_FILE"
;;
both)
write_summary "$DAILY_FILE" "Daily"
write_summary "$WEEKLY_FILE" "Weekly"
echo "Saved: $DAILY_FILE"
echo "Saved: $WEEKLY_FILE"
;;
*)
echo "Modo non valido: $MODE (usa daily|weekly|both)"
exit 1
;;
esac