fix(loop): eliminate tmp script copy and trap leak in delegate_job_safe (P2-1/B-6)

This commit is contained in:
2026-08-15 10:46:39 +09:00
parent af3dc1600c
commit b490713471
4 changed files with 211 additions and 18 deletions
@@ -87,16 +87,27 @@ fi
MAM_LOOP_MARKER="${MAM_LOOP_MARKER:-$REPO_ROOT/.mam/loop-guard-active}"
_mam_release_guard() { mam_release_loop_lock "$MAM_LOOP_MARKER" || true; }
# Runs the delegate-job wrapper in place. Deliberately creates no copy and
# installs no trap:
# * a copy inside .agents/skills/ pollutes the source tree and leaks on
# SIGKILL (B-6). It never protected across turns anyway — the copy is made
# per call, so a wrapper broken in turn N is copied broken in turn N+1;
# * every call site is a command substitution, so a trap set here fires when
# that subshell ends. `$$` is still the parent's pid there, so
# _mam_release_guard passed its ownership check and dropped the loop lock
# after the first delegated job (D1).
# The callers' own "Failed to register ..." branches are unreachable when the
# wrapper exits non-zero (set -e aborts the assignment first), so the diagnosis
# has to be emitted here.
delegate_job_safe() {
local orig_script="$REPO_ROOT/.agents/skills/multi-agent-mux-delegate-job/multi-agent-mux-delegate-job"
local tmp_script
tmp_script="${orig_script}.${RANDOM}_$$.tmp"
cp "$orig_script" "$tmp_script"
trap 'rm -f "$tmp_script"' EXIT INT TERM HUP
local rc=0
bash "$tmp_script" "$@" || rc=$?
rm -f "$tmp_script"
trap _mam_release_guard EXIT INT TERM HUP
bash "$orig_script" "$@" || rc=$?
if [ "$rc" -ne 0 ]; then
log_error "delegate_job_safe failed (exit $rc): $orig_script"
log_error " if this loop edits framework skills in place, check that file's syntax:"
log_error " bash -n \"$orig_script\""
fi
return $rc
}
@@ -134,6 +145,7 @@ case "$_mam_acquire_rc" in
;;
esac
trap _mam_release_guard EXIT INT TERM HUP
rm -f "$REPO_ROOT/.agents/skills/multi-agent-mux-delegate-job/multi-agent-mux-delegate-job".*.tmp 2>/dev/null || true
# --all-reviewer silently takes precedence over an explicit --reviewer list;
# warn so the discarded list isn't mistaken for having been honored (P2-1).