Two folders sharing a remote no longer force-push over each other's triple-c/wip/<run-id>; the folder is part of the branch name (sanitised, check-ref-format validated, hash fallback). A setup chain that dies with the setup still running records failed so the idle stop is not blocked. TRIPLEC_MAX_LIFETIME_MINUTES 00 means no cap and 08 means 8. The run record is written after the agent-token block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
307 lines
13 KiB
Bash
Executable File
307 lines
13 KiB
Bash
Executable File
#!/bin/bash
|
|
# triple-c-idle-watch [--check]
|
|
#
|
|
# Idle auto-stop for an AWS workspace (plan ruling P7, R11). Started by
|
|
# entrypoint.sh, as root, only when TRIPLEC_HOST=aws and
|
|
# TRIPLEC_IDLE_STOP_MINUTES is set. Every interval it asks whether the
|
|
# workspace is idle:
|
|
#
|
|
# * the agent's /v1/activity (read with the agent's own token file) shows
|
|
# no authenticated activity for N minutes. The agent counts requests and
|
|
# the bytes of attaches and tunnels, so an open but silent terminal tab
|
|
# ages like no tab at all, and the app's background polls (the auth
|
|
# bridge, marked X-Triple-C-Background) never count. The open exec and
|
|
# tunnel counters are *not* consulted (R11);
|
|
# * no `claude` process is running;
|
|
# * no scheduler job is running (`triple-c-scheduler status`, as claude);
|
|
# * the start's background repository clone has finished (entrypoint.sh,
|
|
# start_clone_repos: /run/triple-c-clone exists without `done`), and so
|
|
# has the setup script (/run/triple-c-setup/status says `running`);
|
|
# * no work would be lost: `triple-c-wip-push --dry-run` (as claude) finds
|
|
# no dirty repository and no unpushed commit. With the WIP push on
|
|
# (TRIPLEC_WIP_PUSH=1) such work is pushed here, before the stop, and the
|
|
# workspace stops only once that push succeeded and the workspace is
|
|
# still quiet afterwards (activity during the push sends it back to
|
|
# watching); a failed push keeps it running and is retried later (backing
|
|
# off). With it off, unsaved work simply keeps the workspace up.
|
|
#
|
|
# Anything it cannot read counts as NOT idle: a stop is the one thing here
|
|
# that cannot be undone, so every doubt keeps the workspace running.
|
|
#
|
|
# When idle it records "idle" as the stop reason and sends SIGTERM to the
|
|
# entrypoint (TRIPLEC_IDLE_SIGNAL_PID). The entrypoint's TERM trap sees
|
|
# "idle", does not push again (this script already did), stops the keepalive
|
|
# and exits with the idle status (75) so the app can tell an idle stop from a
|
|
# crash. A stop from ECS (SIGTERM, no marker) still pushes in the trap, best
|
|
# effort; a SIGTERM from ECS arriving mid-way cannot start a second push.
|
|
#
|
|
# Why not `kill -TERM 1`: with initProcessEnabled ECS runs the container under
|
|
# docker-init, so PID 1 is the init, not the entrypoint. The init would
|
|
# forward the signal, but signalling the entrypoint directly does not depend on
|
|
# that and also works without an init.
|
|
#
|
|
# Lifetime cap (phase 6, Q8; TRIPLEC_MAX_LIFETIME_MINUTES, 0 or empty = none):
|
|
# once the run's start (the entrypoint's /run/triple-c/start-epoch) is that
|
|
# many minutes ago, whatever the activity, it pushes work in progress first
|
|
# when the WIP push is on (best effort, bounded by TRIPLEC_WIP_TIMEOUT) and
|
|
# then records "lifetime" and sends SIGTERM even if that push failed; the
|
|
# entrypoint exits 76. A `pushed`/`discard` marker (the app is stopping it
|
|
# already) is kept, with no push of its own. The cap is advisory: a workspace
|
|
# user has sudo and could stop this watcher.
|
|
#
|
|
# Either the idle stop or the lifetime cap must be configured; the watcher
|
|
# runs when at least one is.
|
|
#
|
|
# --check evaluates once and exits 0 (idle and nothing unsaved, or unsaved
|
|
# work the WIP push would take) or 1 (not idle); for tests and for looking at
|
|
# a live task. It never pushes.
|
|
#
|
|
# Configuration (environment; the entrypoint sets the first four, the rest
|
|
# exist for tests):
|
|
# TRIPLEC_IDLE_STOP_MINUTES N (> 0; empty: no idle stop)
|
|
# TRIPLEC_MAX_LIFETIME_MINUTES the lifetime cap (empty or 0: none)
|
|
# TRIPLEC_IDLE_SIGNAL_PID the entrypoint's PID
|
|
# TRIPLEC_AGENT_TOKEN_FILE default /dev/shm/triple-c/agent-token
|
|
# TRIPLEC_WIP_PUSH 1: push unsaved work before an idle stop
|
|
# TRIPLEC_AGENT_URL default http://127.0.0.1:47900
|
|
# TRIPLEC_IDLE_INTERVAL seconds between checks, default 60
|
|
# TRIPLEC_STOP_REASON_FILE default /run/triple-c/stop-reason
|
|
# TRIPLEC_SCHEDULER_BIN default /usr/local/bin/triple-c-scheduler
|
|
# TRIPLEC_SCHEDULER_USER default claude; empty runs it as this user
|
|
# (also the user the WIP push runs as)
|
|
# TRIPLEC_WIP_PUSH_BIN default /usr/local/bin/triple-c-wip-push
|
|
# TRIPLEC_WIP_TIMEOUT seconds for the push, default 90
|
|
# TRIPLEC_WIP_BACKOFF_MAX longest wait between push retries, default 1800
|
|
# TRIPLEC_IDLE_PROC_NAME default claude
|
|
# TRIPLEC_CLONE_STATUS_DIR default /run/triple-c-clone
|
|
# TRIPLEC_SETUP_STATUS_DIR default /run/triple-c-setup
|
|
# TRIPLEC_START_EPOCH_FILE default /run/triple-c/start-epoch
|
|
|
|
set -u
|
|
|
|
minutes="${TRIPLEC_IDLE_STOP_MINUTES:-}"
|
|
signal_pid="${TRIPLEC_IDLE_SIGNAL_PID:-}"
|
|
token_file="${TRIPLEC_AGENT_TOKEN_FILE:-/dev/shm/triple-c/agent-token}"
|
|
agent_url="${TRIPLEC_AGENT_URL:-http://127.0.0.1:47900}"
|
|
interval="${TRIPLEC_IDLE_INTERVAL:-60}"
|
|
reason_file="${TRIPLEC_STOP_REASON_FILE:-/run/triple-c/stop-reason}"
|
|
scheduler="${TRIPLEC_SCHEDULER_BIN:-/usr/local/bin/triple-c-scheduler}"
|
|
scheduler_user="${TRIPLEC_SCHEDULER_USER-claude}"
|
|
proc_name="${TRIPLEC_IDLE_PROC_NAME:-claude}"
|
|
clone_dir="${TRIPLEC_CLONE_STATUS_DIR:-/run/triple-c-clone}"
|
|
wip_push="${TRIPLEC_WIP_PUSH:-0}"
|
|
wip_bin="${TRIPLEC_WIP_PUSH_BIN:-/usr/local/bin/triple-c-wip-push}"
|
|
wip_timeout="${TRIPLEC_WIP_TIMEOUT:-90}"
|
|
backoff_max="${TRIPLEC_WIP_BACKOFF_MAX:-1800}"
|
|
setup_dir="${TRIPLEC_SETUP_STATUS_DIR:-/run/triple-c-setup}"
|
|
lifetime="${TRIPLEC_MAX_LIFETIME_MINUTES:-}"
|
|
start_file="${TRIPLEC_START_EPOCH_FILE:-/run/triple-c/start-epoch}"
|
|
|
|
# >>> is_idle
|
|
# is_idle now last_activity_unix claude_running jobs_running minutes
|
|
# Pure: exit 0 when idle, 1 otherwise. Every argument must be a plain
|
|
# non-negative integer; anything else (an empty or garbled read) is not idle.
|
|
# Open execs and tunnels are deliberately not an input (R11): their traffic
|
|
# already moves last_activity_unix, and an open but silent one is idle.
|
|
is_idle() {
|
|
local a
|
|
[ $# -eq 5 ] || return 1
|
|
for a in "$@"; do
|
|
case "$a" in ''|*[!0-9]*) return 1 ;; esac
|
|
done
|
|
local now=$1 last=$2 claude=$3 jobs=$4 mins=$5
|
|
[ "$mins" -gt 0 ] || return 1
|
|
[ "$claude" -eq 0 ] && [ "$jobs" -eq 0 ] || return 1
|
|
[ $((now - last)) -ge $((mins * 60)) ]
|
|
}
|
|
# <<< is_idle
|
|
|
|
# Prints last_activity_unix, or fails.
|
|
read_activity() {
|
|
local token body
|
|
token=$(cat "$token_file" 2>/dev/null) || return 1
|
|
[ -n "$token" ] || return 1
|
|
# The token goes in through a config on stdin, never argv: /proc/<pid>/cmdline
|
|
# is readable by every user in the container.
|
|
body=$(printf 'header = "Authorization: Bearer %s"\n' "$token" \
|
|
| curl -sf --max-time 5 --config - "$agent_url/v1/activity" 2>/dev/null) || return 1
|
|
printf '%s' "$body" | jq -r '.last_activity_unix
|
|
| if type == "number" and . >= 0 and . == floor then tostring else "x" end' 2>/dev/null
|
|
}
|
|
|
|
# Run "$@" as the scheduler user (claude) when we are root, else as is.
|
|
as_user() {
|
|
if [ -n "$scheduler_user" ] && [ "$(id -u)" = 0 ]; then
|
|
su -s /bin/bash -c '"$@"' -- "$scheduler_user" _ "$@"
|
|
else
|
|
"$@"
|
|
fi
|
|
}
|
|
|
|
# 1 when a scheduler job is running *or* the question cannot be answered.
|
|
jobs_running() {
|
|
local out
|
|
out=$(as_user "$scheduler" status 2>/dev/null) || { echo 1; return; }
|
|
if [ "$out" = "Nothing running." ]; then echo 0; else echo 1; fi
|
|
}
|
|
|
|
# A clone still under way counts as a running job.
|
|
clone_running() {
|
|
[ -d "$clone_dir" ] && [ ! -e "$clone_dir/done" ]
|
|
}
|
|
|
|
# So does the setup script (entrypoint.sh, aws_run_setup).
|
|
setup_running() {
|
|
[ "$(head -n 1 "$setup_dir/status" 2>/dev/null)" = running ]
|
|
}
|
|
|
|
# The folders with unsaved work (dirty, or commits no remote has), one per
|
|
# line; nothing when there is none. Fails when it cannot tell.
|
|
unsaved_work() {
|
|
local out
|
|
out=$(as_user timeout -k 5 60 "$wip_bin" --dry-run 2>/dev/null) || return 1
|
|
printf '%s\n' "$out" | jq -r 'select(.dirty == true or (.unpushed // 0) > 0) | .folder' 2>/dev/null
|
|
}
|
|
|
|
# The activity, process, job and clone conditions.
|
|
quiet() {
|
|
local last claude jobs
|
|
last=$(read_activity) || return 1
|
|
if pgrep -x "$proc_name" >/dev/null 2>&1; then claude=1; else claude=0; fi
|
|
jobs=$(jobs_running)
|
|
clone_running && jobs=1
|
|
setup_running && jobs=1
|
|
is_idle "$(date +%s)" "${last:-}" "$claude" "$jobs" "$minutes"
|
|
}
|
|
|
|
# Say something once per change of state, not every interval.
|
|
said=""
|
|
say_once() {
|
|
[ "$1" = "$said" ] && return 0
|
|
said="$1"
|
|
echo "triple-c-idle-watch: $1"
|
|
}
|
|
|
|
# --check: quiet, and no unsaved work unless the WIP push would take it.
|
|
check() {
|
|
local dirty
|
|
quiet || return 1
|
|
dirty=$(unsaved_work) || return 1
|
|
[ -z "$dirty" ] || [ "$wip_push" = 1 ]
|
|
}
|
|
|
|
case "$minutes" in
|
|
'') ;;
|
|
*[!0-9]*) echo "triple-c-idle-watch: TRIPLEC_IDLE_STOP_MINUTES must be a positive integer" >&2; exit 2 ;;
|
|
*) minutes=$((10#$minutes))
|
|
[ "$minutes" -gt 0 ] || { echo "triple-c-idle-watch: TRIPLEC_IDLE_STOP_MINUTES must be a positive integer" >&2; exit 2; } ;;
|
|
esac
|
|
case "$lifetime" in
|
|
'') ;;
|
|
*[!0-9]*) echo "triple-c-idle-watch: TRIPLEC_MAX_LIFETIME_MINUTES must be a whole number of minutes" >&2; exit 2 ;;
|
|
# Decimal even with leading zeros (`08` is 8); 0, 00, … mean no cap.
|
|
*) lifetime=$((10#$lifetime)); [ "$lifetime" -gt 0 ] || lifetime="" ;;
|
|
esac
|
|
|
|
if [ "${1:-}" = "--check" ]; then
|
|
[ -n "$minutes" ] || { echo "triple-c-idle-watch: --check needs TRIPLEC_IDLE_STOP_MINUTES" >&2; exit 2; }
|
|
check
|
|
exit $?
|
|
fi
|
|
|
|
if [ -z "$minutes" ] && [ -z "$lifetime" ]; then
|
|
echo "triple-c-idle-watch: neither TRIPLEC_IDLE_STOP_MINUTES nor TRIPLEC_MAX_LIFETIME_MINUTES is set — nothing to watch" >&2
|
|
exit 2
|
|
fi
|
|
|
|
case "$signal_pid" in
|
|
''|*[!0-9]*|0|1) echo "triple-c-idle-watch: TRIPLEC_IDLE_SIGNAL_PID must be the entrypoint's PID" >&2; exit 2 ;;
|
|
esac
|
|
|
|
# The run's start, for the lifetime cap. The entrypoint wrote it before
|
|
# starting this watcher; should it be unreadable the cap counts from now
|
|
# (later rather than never).
|
|
started_at=$(head -n 1 "$start_file" 2>/dev/null)
|
|
case "$started_at" in
|
|
''|*[!0-9]*)
|
|
[ -z "$lifetime" ] || echo "triple-c-idle-watch: no start time in $start_file — the lifetime cap counts from now" >&2
|
|
started_at=$(date +%s) ;;
|
|
esac
|
|
|
|
lifetime_reached() {
|
|
[ -n "$lifetime" ] && [ $(( $(date +%s) - started_at )) -ge $((lifetime * 60)) ]
|
|
}
|
|
|
|
# The cap is reached: push (best effort) unless the app has decided already,
|
|
# record "lifetime", signal the entrypoint and end.
|
|
lifetime_stop() {
|
|
echo "triple-c-idle-watch: the environment's maximum workspace lifetime (${lifetime} minutes) was reached — stopping the workspace"
|
|
mkdir -p "$(dirname "$reason_file")" 2>/dev/null
|
|
case "$(cat "$reason_file" 2>/dev/null)" in
|
|
pushed|discard)
|
|
echo "triple-c-idle-watch: Triple-C is already stopping it — keeping its decision" ;;
|
|
*)
|
|
if [ "$wip_push" = 1 ]; then
|
|
echo "triple-c-idle-watch: pushing work in progress before the stop..."
|
|
as_user timeout -k 5 "$wip_timeout" "$wip_bin" --reason lifetime \
|
|
|| echo "triple-c-idle-watch: the WIP push failed — stopping anyway (the lifetime cap is reached)"
|
|
fi
|
|
printf 'lifetime\n' > "$reason_file" ;;
|
|
esac
|
|
kill -TERM "$signal_pid"
|
|
exit 0
|
|
}
|
|
|
|
push_failures=0
|
|
next_push_at=0
|
|
while :; do
|
|
sleep "$interval"
|
|
lifetime_reached && lifetime_stop
|
|
[ -n "$minutes" ] || continue
|
|
if ! quiet; then
|
|
said=""
|
|
continue
|
|
fi
|
|
if ! dirty=$(unsaved_work); then
|
|
say_once "idle, but the repositories' state cannot be read — not stopping"
|
|
continue
|
|
fi
|
|
if [ -n "$dirty" ]; then
|
|
folders=$(printf '%s' "$dirty" | paste -sd, -)
|
|
if [ "$wip_push" != 1 ]; then
|
|
say_once "idle, but $folders has unsaved work and the WIP push is off — not stopping"
|
|
continue
|
|
fi
|
|
[ "$(date +%s)" -ge "$next_push_at" ] || continue
|
|
echo "triple-c-idle-watch: idle — pushing work in progress ($folders) before stopping..."
|
|
if ! as_user timeout -k 5 "$wip_timeout" "$wip_bin" --reason idle; then
|
|
push_failures=$((push_failures + 1))
|
|
wait_s=$((interval << (push_failures < 10 ? push_failures : 10)))
|
|
[ "$wait_s" -le "$backoff_max" ] || wait_s=$backoff_max
|
|
next_push_at=$(( $(date +%s) + wait_s ))
|
|
said=""
|
|
echo "triple-c-idle-watch: the WIP push failed — staying up; retrying in ${wait_s}s"
|
|
continue
|
|
fi
|
|
# The push took a while: someone may have come back meanwhile (a
|
|
# terminal, a `claude`, a scheduler job). Their new work would be
|
|
# lost, since the stop hook does not push again after an idle push.
|
|
if ! quiet; then
|
|
echo "triple-c-idle-watch: activity during the WIP push — not stopping"
|
|
said=""
|
|
continue
|
|
fi
|
|
fi
|
|
echo "triple-c-idle-watch: idle for ${minutes} minutes — stopping the workspace"
|
|
mkdir -p "$(dirname "$reason_file")" 2>/dev/null
|
|
# A pushed/discard marker means the app's stop guard has decided and
|
|
# its stop is under way: keep that decision rather than overwrite it.
|
|
# (Nor a lifetime stop's marker, should one be there.)
|
|
case "$(cat "$reason_file" 2>/dev/null)" in
|
|
pushed|discard|lifetime) ;;
|
|
*) printf 'idle\n' > "$reason_file" ;;
|
|
esac
|
|
kill -TERM "$signal_pid"
|
|
exit 0
|
|
done
|