handoff: survive restarts, route backends, trust the TUI

- submit stores the backend in the task JSON (--backend freebuff works again).
- start_task reads it; the freebuff path calls freebuff-agent.
- ensure_session starts the tmux server in its own transient scope, so a
  'systemctl --user restart handoff' no longer kills every running worker.
- requeue_orphans puts tasks back in the queue when their window or process is
  gone (daemon crash, killed window, tmux server restart); 'handoff list' shows
  them as orphan instead of running forever.
- freebuff-agent only believes the task is finished when the marker stands alone
  on a line AND the TUI is idle, because the prompt we send contains the marker.
This commit is contained in:
Ziad Arafat committed 2026-11-28 22:05:28 -07:00
1 parent 5accb686d3
commit 8d73a773b3
2 files changed
+54 -8

No files matched your search

+21 -3
View File
@@ -69,14 +69,32 @@ PROMPT="Read $RUN/BRIEF.md and complete it. Work only inside $DIR. When you are
tmux send-keys -t "$SESSION" "$PROMPT" Enter
echo "[freebuff-agent] $ID: task sent, waiting up to ${TIMEOUT}s"
# 3. Watch the pane for the marker.
# 3. Watch the pane. Two traps here: the task text we just sent contains the marker
# (so a plain grep matches our own prompt), and the TUI keeps a "working..." line
# while it thinks. So: ignore the first 30 s, require the marker on a line of its
# own, and require the TUI to be idle (prompt back, no "working...") to believe it.
deadline=$(( $(date +%s) + TIMEOUT ))
started=$(date +%s)
state=failed
idle_hits=0
while [ "$(date +%s)" -lt "$deadline" ]; do
sleep 10
pane="$(tmux capture-pane -p -t "$SESSION" 2>/dev/null)"
if printf '%s' "$pane" | grep -q "HANDOFF-DONE"; then state=done; break; fi
if ! tmux has-session -t "$SESSION" 2>/dev/null; then state=failed; break; fi
pane="$(tmux capture-pane -p -t "$SESSION" 2>/dev/null)"
[ $(( $(date +%s) - started )) -lt 30 ] && continue
marker=0
printf '%s\n' "$pane" | sed 's/[[:space:]]*$//' | grep -qx "HANDOFF-DONE" && marker=1
busy=0
printf '%s' "$pane" | grep -q "working\.\.\." && busy=1
prompt=0
printf '%s' "$pane" | grep -q "Enter a coding task" && prompt=1
if [ "$marker" -eq 1 ] && [ "$busy" -eq 0 ]; then state=done; break; fi
if [ "$busy" -eq 0 ] && [ "$prompt" -eq 1 ]; then
idle_hits=$(( idle_hits + 1 ))
[ "$idle_hits" -ge 2 ] && { state=done; break; } # finished, marker missing
else
idle_hits=0
fi
done
snap
+33 -5
View File
@@ -63,10 +63,12 @@ cmd_submit() {
local bpath="$DATA/$id.brief.md"
printf '%s\n' "$body" > "$bpath"
python3 - "$QUEUE/$id.json" "$id" "$dir" "$role" "$bpath" <<'PY'
python3 - "$QUEUE/$id.json" "$id" "$dir" "$role" "$bpath" "$backend" <<'PY'
import json, sys, datetime
path, rid, d, role, brief = sys.argv[1:6]
json.dump({"id": rid, "dir": d, "role": role, "brief": brief, "queuedAt": datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat()+"Z"}, open(path, "w"), indent=1)
path, rid, d, role, brief, backend = sys.argv[1:7]
json.dump({"id": rid, "dir": d, "role": role, "brief": brief, "backend": backend,
"queuedAt": datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat()+"Z"},
open(path, "w"), indent=1)
PY
printf 'queued %s\n dir: %s\n role: %s\n backend: %s\n log: %s/%s/run.log\n' "$id" "$dir" "$role" "$backend" "$STATE" "$id"
echo " start the daemon if it is not running: handoff daemon &"
@@ -74,7 +76,12 @@ PY
ensure_session() {
tmux has-session -t "$TMUX_SESSION" 2>/dev/null && return 0
tmux new-session -d -s "$TMUX_SESSION" -n workers -c "$HOME_DIR"
# Start the tmux server in its own transient scope: a server started inside this
# unit's cgroup dies with a `systemctl --user restart handoff`, taking every
# running worker's window with it.
systemd-run --user --scope --quiet --collect \
tmux new-session -d -s "$TMUX_SESSION" -n workers -c "$HOME_DIR" 2>/dev/null \
|| tmux new-session -d -s "$TMUX_SESSION" -n workers -c "$HOME_DIR"
tmux set-option -t "$TMUX_SESSION" history-limit 20000 >/dev/null 2>&1
}
@@ -86,6 +93,7 @@ start_task() {
dir="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["dir"])' "$f")"
role="$(python3 -c 'import json,sys;d=json.load(open(sys.argv[1]));print(d.get("role","code"))' "$f")"
brief="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["brief"])' "$f")"
backend="$(python3 -c 'import json,sys;d=json.load(open(sys.argv[1]));print(d.get("backend","free-agent"))' "$f")"
mv "$f" "$RUNNING/$id.json" 2>/dev/null || return 0
ensure_session
@@ -120,14 +128,30 @@ collect_finished() {
done
}
# A task can land in running/ and then lose its worker (daemon restart, killed
# window, tmux server death). Put it back in the queue instead of leaving it
# "running" forever.
requeue_orphans() {
local f id
for f in "$RUNNING"/*.json; do
[ -e "$f" ] || continue
id="$(basename "$f" .json)"
tmux list-windows -t "$TMUX_SESSION" -F '#{window_name}' 2>/dev/null | grep -qx "$id" && continue
pgrep -f -- "--id $id" >/dev/null 2>&1 && continue
mv "$f" "$QUEUE/$id.json" 2>/dev/null && echo "[$(now)] requeued orphan $id"
done
}
cmd_daemon() {
ensure_session
echo "[$(now)] handoff daemon: queue=$QUEUE parallel=$MAX_PARALLEL session=$TMUX_SESSION"
requeue_orphans
local unpaused="$DATA/pause"
trap 'echo "[$(now)] handoff daemon: stopping"; exit 0' INT TERM
while :; do
if [ ! -f "$unpaused" ]; then
collect_finished
requeue_orphans
for f in "$QUEUE"/*.json; do
[ -e "$f" ] || continue
[ "$(running_count)" -lt "$MAX_PARALLEL" ] || break
@@ -147,7 +171,11 @@ cmd_list() {
st="$STATE/$id/status.json"
local model="-"
[ -f "$st" ] && model="$(python3 -c 'import json,sys;d=json.load(open(sys.argv[1]));print(d.get("model") or d.get("backend") or d.get("state","-"))' "$st" 2>/dev/null)"
printf '%-8s %-28s %-34s %s\n' "$d" "$id" "$model" "$(dirname "$st")"
local tag="$d"
if [ "$d" = "running" ] && ! tmux list-windows -t "$TMUX_SESSION" -F '#{window_name}' 2>/dev/null | grep -qx "$id"; then
tag="orphan"
fi
printf '%-8s %-28s %-34s %s\n' "$tag" "$id" "$model" "$(dirname "$st")"
done
done
}