2026-03-26 15:20:29 -04:00
|
|
|
#!/bin/bash
|
2026-04-16 15:41:28 -04:00
|
|
|
# Bail if another agent is in the same phase-group as us.
|
2026-03-26 15:20:29 -04:00
|
|
|
#
|
2026-04-16 15:41:28 -04:00
|
|
|
# $1 = our pid file name (e.g. "pid-12345")
|
|
|
|
|
# $2 = the phase we're about to enter (e.g. "surface", "observe")
|
|
|
|
|
# cwd = state dir
|
|
|
|
|
#
|
|
|
|
|
# Also refreshes our own pid file with the current phase on each call,
|
|
|
|
|
# so concurrent agents can read each other's phase by cat'ing the pid
|
|
|
|
|
# files in the state dir.
|
|
|
|
|
#
|
|
|
|
|
# Phase groups: "surface" vs everything else ("post-surface"). We allow
|
|
|
|
|
# at most one agent per group to be alive at a time — so surface can run
|
|
|
|
|
# at a higher frequency than the slower organize/observe tail.
|
|
|
|
|
#
|
|
|
|
|
# Exit 0 = continue, exit 1 = bail (another agent in our group is alive).
|
2026-03-26 15:20:29 -04:00
|
|
|
|
2026-04-08 10:39:07 -04:00
|
|
|
shopt -s nullglob
|
|
|
|
|
|
|
|
|
|
my_pid_file="$1"
|
2026-04-16 15:41:28 -04:00
|
|
|
my_phase="$2"
|
|
|
|
|
|
|
|
|
|
# Refresh our own pid file with the current phase.
|
|
|
|
|
printf '%s' "$my_phase" > "$my_pid_file"
|
|
|
|
|
|
|
|
|
|
group_of() {
|
|
|
|
|
if [[ "$1" == "surface" ]]; then
|
|
|
|
|
echo "surface"
|
|
|
|
|
else
|
|
|
|
|
echo "post-surface"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my_group=$(group_of "$my_phase")
|
2026-03-26 15:20:29 -04:00
|
|
|
|
|
|
|
|
for f in pid-*; do
|
2026-04-16 15:41:28 -04:00
|
|
|
[[ "$f" == "$my_pid_file" ]] && continue
|
2026-04-11 18:41:44 -04:00
|
|
|
pid="${f#pid-}"
|
2026-04-16 15:41:28 -04:00
|
|
|
if ! kill -0 "$pid" 2>/dev/null; then
|
|
|
|
|
rm -f "$f" # stale pid file, clean up
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
other_phase=$(cat "$f" 2>/dev/null)
|
|
|
|
|
other_group=$(group_of "$other_phase")
|
|
|
|
|
if [[ "$my_group" == "$other_group" ]]; then
|
|
|
|
|
exit 1
|
2026-04-11 18:41:44 -04:00
|
|
|
fi
|
2026-03-26 15:20:29 -04:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
exit 0
|