fix(update): the legacy start_watchdog matches the monitor exactly too; a restart is reported only when the monitor runs (Issue #72, review round 4)

With MAM_DELEGATION_PY=0 the legacy helper still found the monitor with an
unanchored pgrep -f, so a process that only named the monitor path was
reported as the restarted monitor and none was started. lib.sh _monitor_pids
is the shell twin of daemon.find_running; update.sh and post_update.sh check
the PID start_watchdog printed with mam_monitor_started before saying
"monitor: restarted".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GodopuandClaude Opus 5.5 committed 2026-10-28 04:10:15 +09:00
1 parent cf1b693e85
commit 1d63e882bc
5 files changed
+103 -12

No files matched your search

+36 -1
View File
@@ -2235,6 +2235,41 @@ _delegate_publish_event_legacy() {
"$py_bin" "$pub" --job "$job_id" --event "$event" --detail "$detail" ${extra[@]+"${extra[@]}"} || true
}
# _monitor_pids <monitor_script>
# Issue #72: PIDs whose command line is exactly `<bash> <monitor_script>
# --subscribe[ ...]` - the shell twin of lib_py.delegation.daemon.find_running,
# so both start_watchdog branches recognise the same daemons. Not a `pgrep -f`
# regex: that also matched another workspace's daemon (an unescaped `.`) and a
# process that only names the path in an argument.
_monitor_pids() {
ps -A -ww -o pid= -o args= | MAM_MONITOR_WANT="$1 --subscribe" awk '
{ line = $0; sub(/^ +/, "", line)
i = index(line, " "); if (!i) next
pid = substr(line, 1, i - 1); args = substr(line, i + 1)
j = index(args, " "); if (!j) next
n = split(substr(args, 1, j - 1), a0, "/"); rest = substr(args, j + 1)
want = ENVIRON["MAM_MONITOR_WANT"]
if (a0[n] == "bash" && (rest == want || index(rest, want " ") == 1)) print pid }'
}
# mam_monitor_started <workspace> <pid...>
# Issue #72: 0 once one of the PIDs start_watchdog printed is WORKSPACE's
# monitor (_monitor_pids), so a restart is only reported when it happened.
# Polled for up to 5 s: a daemon just started from the shell carries its
# parent's command line until it execs bash.
mam_monitor_started() {
local script="$1/.agents/skills/multi-agent-mux-monitor/scripts/reconcile.sh" n=0 p
shift
while [ "$n" -lt 50 ]; do
for p in $(_monitor_pids "$script"); do
case " $* " in *" $p "*) return 0 ;; esac
done
sleep 0.1
n=$((n + 1))
done
return 1
}
# start_watchdog <job_id> [workdir]
# Spawns a watchdog process to monitor a delegate-job JOB in the background.
# The watchdog re-spawns the subscriber every 2 minutes (or whatever hard
@@ -2253,7 +2288,7 @@ _start_watchdog_legacy() {
# Check if reconcile.sh --subscribe is already running on this workspace
local pid
pid=$(pgrep -f "bash $monitor_script --subscribe" || true)
pid=$(_monitor_pids "$monitor_script" || true)
# Issue #19: MAM_NO_SUBSCRIBER_DAEMON=1 reports a running daemon but never
# starts one (test sandboxes; externally supervised monitors).
+2 -2
View File
@@ -42,8 +42,8 @@ def find_running(monitor_script: str) -> List[str]:
A subscriber is a process whose command line is `<bash> <monitor_script>
--subscribe[ ...]`, argv0 a bash, as both this module and the shell helper
start it. Issue #72: matched exactly, not as the shell helper's `pgrep -f`
regex, because the update skill signals what this returns - a daemon of
start it. Issue #72: matched exactly (lib.sh _monitor_pids is the shell
twin the legacy helper uses), not as a `pgrep -f` regex, because the update skill signals what this returns - a daemon of
another workspace the unescaped path also matched, or a process that only
names the path in an argument, is not this workspace's monitor. Multiple
matches are returned in full: the legacy helper echoed every line and
@@ -37,7 +37,8 @@ fi
if [ "$MON" = "1" ]; then
if [ "${MAM_NO_SUBSCRIBER_DAEMON:-0}" = "1" ]; then
echo "monitor: not restarted (MAM_NO_SUBSCRIBER_DAEMON=1)"
elif pid="$(start_watchdog update "$WS")"; then
# shellcheck disable=SC2086
elif pid="$(start_watchdog update "$WS")" && mam_monitor_started "$WS" $pid; then
echo "monitor: restarted (PID $pid)"
else
echo "ERROR: could not restart the monitor daemon" >&2
@@ -104,6 +104,8 @@ _restart_monitor() {
fi
local pid
pid=$(start_watchdog update "$WS") || { echo "ERROR: could not restart the monitor daemon" >&2; return 1; }
# shellcheck disable=SC2086
mam_monitor_started "$WS" $pid || { echo "ERROR: could not restart the monitor daemon (PID ${pid:-none} is not its monitor)" >&2; return 1; }
echo "monitor: restarted (PID $pid)"
}