mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-04-09 10:53:34 +08:00
fix: clean up observer sessions on lifecycle end
This commit is contained in:
@@ -14,6 +14,9 @@ ANALYZING=0
|
||||
LAST_ANALYSIS_EPOCH=0
|
||||
# Minimum seconds between analyses (prevents rapid re-triggering)
|
||||
ANALYSIS_COOLDOWN="${ECC_OBSERVER_ANALYSIS_COOLDOWN:-60}"
|
||||
IDLE_TIMEOUT_SECONDS="${ECC_OBSERVER_IDLE_TIMEOUT_SECONDS:-1800}"
|
||||
SESSION_LEASE_DIR="${PROJECT_DIR}/.observer-sessions"
|
||||
ACTIVITY_FILE="${PROJECT_DIR}/.observer-last-activity"
|
||||
|
||||
cleanup() {
|
||||
[ -n "$SLEEP_PID" ] && kill "$SLEEP_PID" 2>/dev/null
|
||||
@@ -24,6 +27,62 @@ cleanup() {
|
||||
}
|
||||
trap cleanup TERM INT
|
||||
|
||||
file_mtime_epoch() {
|
||||
local file="$1"
|
||||
if [ ! -f "$file" ]; then
|
||||
printf '0\n'
|
||||
return
|
||||
fi
|
||||
|
||||
if stat -c %Y "$file" >/dev/null 2>&1; then
|
||||
stat -c %Y "$file" 2>/dev/null || printf '0\n'
|
||||
return
|
||||
fi
|
||||
|
||||
if stat -f %m "$file" >/dev/null 2>&1; then
|
||||
stat -f %m "$file" 2>/dev/null || printf '0\n'
|
||||
return
|
||||
fi
|
||||
|
||||
printf '0\n'
|
||||
}
|
||||
|
||||
has_active_session_leases() {
|
||||
if [ ! -d "$SESSION_LEASE_DIR" ]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
find "$SESSION_LEASE_DIR" -type f -name '*.json' -print -quit 2>/dev/null | grep -q .
|
||||
}
|
||||
|
||||
latest_activity_epoch() {
|
||||
local observations_epoch activity_epoch
|
||||
observations_epoch="$(file_mtime_epoch "$OBSERVATIONS_FILE")"
|
||||
activity_epoch="$(file_mtime_epoch "$ACTIVITY_FILE")"
|
||||
|
||||
if [ "$activity_epoch" -gt "$observations_epoch" ] 2>/dev/null; then
|
||||
printf '%s\n' "$activity_epoch"
|
||||
else
|
||||
printf '%s\n' "$observations_epoch"
|
||||
fi
|
||||
}
|
||||
|
||||
exit_if_idle_without_sessions() {
|
||||
if has_active_session_leases; then
|
||||
return
|
||||
fi
|
||||
|
||||
local last_activity now_epoch idle_for
|
||||
last_activity="$(latest_activity_epoch)"
|
||||
now_epoch="$(date +%s)"
|
||||
idle_for=$(( now_epoch - last_activity ))
|
||||
|
||||
if [ "$last_activity" -eq 0 ] || [ "$idle_for" -ge "$IDLE_TIMEOUT_SECONDS" ]; then
|
||||
echo "[$(date)] Observer idle without active session leases for ${idle_for}s; exiting" >> "$LOG_FILE"
|
||||
cleanup
|
||||
fi
|
||||
}
|
||||
|
||||
analyze_observations() {
|
||||
if [ ! -f "$OBSERVATIONS_FILE" ]; then
|
||||
return
|
||||
@@ -197,11 +256,13 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
"${CLV2_PYTHON_CMD:-python3}" "${SCRIPT_DIR}/../scripts/instinct-cli.py" prune --quiet >> "$LOG_FILE" 2>&1 || echo "[$(date)] Warning: instinct prune failed (non-fatal)" >> "$LOG_FILE"
|
||||
|
||||
while true; do
|
||||
exit_if_idle_without_sessions
|
||||
sleep "$OBSERVER_INTERVAL_SECONDS" &
|
||||
SLEEP_PID=$!
|
||||
wait "$SLEEP_PID" 2>/dev/null
|
||||
SLEEP_PID=""
|
||||
|
||||
exit_if_idle_without_sessions
|
||||
if [ "$USR1_FIRED" -eq 1 ]; then
|
||||
USR1_FIRED=0
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user