freebuff: enroll as a handoff backend
- bin/freebuff-agent drives the Freebuff TUI in its own tmux session and keeps the same brief -> report contract, so handoff can route to it with --backend freebuff (verified: queue -> worker -> diff -> tests pass). - handoff list shows the backend when a run has no model id.
This commit is contained in:
1 parent
710b5f59cb
commit
acd1c86393
3 files changed
+109
-6
No files matched your search
Executable
+86
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bash
|
||||
# freebuff-agent — run one task brief through the Freebuff TUI inside tmux.
|
||||
#
|
||||
# Same contract as bin/free-agent (brief in, HANDOFF-REPORT.md out), so `handoff`
|
||||
# can treat it as another backend. Freebuff has no headless mode, so this drives
|
||||
# its TUI with tmux send-keys and watches the pane for the completion marker.
|
||||
#
|
||||
# freebuff-agent --brief BRIEF.md --dir ~/Projects/thing [--id slug] [--timeout 1800] [--keep]
|
||||
#
|
||||
# Exit: 0 done, 1 failed/timeout, 2 usage.
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
HOME_DIR="$HOME"
|
||||
STATE="${XDG_STATE_HOME:-$HOME_DIR/.local/state}/free-agent/runs"
|
||||
BRIEF=""; DIR=""; ID=""; TIMEOUT=1800; KEEP=0
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--brief) BRIEF="${2:-}"; shift 2 ;;
|
||||
--dir) DIR="${2:-}"; shift 2 ;;
|
||||
--id) ID="${2:-}"; shift 2 ;;
|
||||
--timeout) TIMEOUT="${2:-1800}"; shift 2 ;;
|
||||
--keep) KEEP=1; shift ;;
|
||||
-h|--help) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
|
||||
*) echo "freebuff-agent: unknown option $1" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -n "$BRIEF" ] || { echo "freebuff-agent: --brief is required" >&2; exit 2; }
|
||||
[ -f "$BRIEF" ] || { echo "freebuff-agent: no such brief $BRIEF" >&2; exit 2; }
|
||||
[ -n "$DIR" ] || DIR="$(dirname "$BRIEF")"
|
||||
[ -d "$DIR" ] || { echo "freebuff-agent: no such dir $DIR" >&2; exit 2; }
|
||||
|
||||
slug() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+|-+$//g' | cut -c1-40; }
|
||||
[ -n "$ID" ] || ID="$(slug "$(basename "$BRIEF" .md)")-$(date +%H%M%S)"
|
||||
|
||||
RUN="$STATE/$ID"
|
||||
mkdir -p "$RUN"
|
||||
cp "$BRIEF" "$RUN/BRIEF.md"
|
||||
LOG="$RUN/run.log"
|
||||
SESSION="fb-$ID"
|
||||
: > "$LOG"
|
||||
|
||||
write_status() { printf '%s\n' "$1" > "$RUN/status.json"; }
|
||||
snap() { tmux capture-pane -p -t "$SESSION" 2>/dev/null | sed -e 's/[[:space:]]*$//' >> "$LOG"; }
|
||||
|
||||
write_status "{\"id\": \"$ID\", \"dir\": \"$(cd "$DIR" && pwd)\", \"state\": \"running\", \"backend\": \"freebuff\", \"startedAt\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}"
|
||||
|
||||
tmux kill-session -t "$SESSION" 2>/dev/null
|
||||
tmux new-session -d -s "$SESSION" -x 220 -y 55 -c "$DIR" "freebuff --cwd $(printf '%q' "$DIR")"
|
||||
echo "[freebuff-agent] $ID: started tmux session $SESSION in $DIR"
|
||||
|
||||
# 1. Wait for the TUI prompt to appear.
|
||||
ready=0
|
||||
for _ in $(seq 1 30); do
|
||||
if tmux capture-pane -p -t "$SESSION" 2>/dev/null | grep -q "Enter a coding task"; then ready=1; break; fi
|
||||
sleep 2
|
||||
done
|
||||
if [ "$ready" -ne 1 ]; then
|
||||
snap; write_status "{\"id\": \"$ID\", \"state\": \"failed\", \"backend\": \"freebuff\", \"reason\": \"freebuff TUI never became ready\"}"
|
||||
echo "[freebuff-agent] $ID: TUI never became ready" >&2
|
||||
[ "$KEEP" -eq 1 ] || tmux kill-session -t "$SESSION" 2>/dev/null
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 2. Send the task. Freebuff reads AGENTS.md itself; the brief has the specifics.
|
||||
PROMPT="Read $RUN/BRIEF.md and complete it. Work only inside $DIR. When you are finished, write your report to $RUN/HANDOFF-REPORT.md (Summary, Files, Verified, Risks) and then reply with exactly HANDOFF-DONE."
|
||||
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.
|
||||
deadline=$(( $(date +%s) + TIMEOUT ))
|
||||
state=failed
|
||||
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
|
||||
done
|
||||
|
||||
snap
|
||||
write_status "{\"id\": \"$ID\", \"state\": \"$state\", \"backend\": \"freebuff\", \"model\": \"freebuff/deepseek\", \"endedAt\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"report\": \"$RUN/HANDOFF-REPORT.md\"}"
|
||||
[ "$KEEP" -eq 1 ] || tmux kill-session -t "$SESSION" 2>/dev/null
|
||||
echo "[freebuff-agent] $ID: $state ($RUN)"
|
||||
[ "$state" = "done" ] && exit 0 || exit 1
|
||||
+13
-6
@@ -38,12 +38,13 @@ need_id() { [ -n "${1:-}" ] || die "missing run id"; }
|
||||
task_file() { local id="$1" d; for d in "$RUNNING" "$QUEUE" "$DONE" "$FAILED"; do [ -f "$d/$id.json" ] && { printf '%s' "$d/$id.json"; return 0; }; done; return 1; }
|
||||
|
||||
cmd_submit() {
|
||||
local brief="" dir="" role="code" id=""
|
||||
local brief="" dir="" role="code" id="" backend="free-agent"
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--dir) dir="${2:-}"; shift 2 ;;
|
||||
--role) role="${2:-code}"; shift 2 ;;
|
||||
--id) id="${2:-}"; shift 2 ;;
|
||||
--backend) backend="${2:-free-agent}"; shift 2 ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
-) brief="STDIN"; shift ;;
|
||||
*) brief="$1"; shift ;;
|
||||
@@ -67,7 +68,7 @@ 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)
|
||||
PY
|
||||
printf 'queued %s\n dir: %s\n role: %s\n log: %s/%s/run.log\n' "$id" "$dir" "$role" "$STATE" "$id"
|
||||
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 &"
|
||||
}
|
||||
|
||||
@@ -80,7 +81,7 @@ ensure_session() {
|
||||
running_count() { find "$RUNNING" -maxdepth 1 -name '*.json' | wc -l | tr -d ' '; }
|
||||
|
||||
start_task() {
|
||||
local f="$1" id dir role brief
|
||||
local f="$1" id dir role brief backend
|
||||
id="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["id"])' "$f")"
|
||||
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")"
|
||||
@@ -88,10 +89,16 @@ start_task() {
|
||||
|
||||
mv "$f" "$RUNNING/$id.json" 2>/dev/null || return 0
|
||||
ensure_session
|
||||
local cmd
|
||||
if [ "$backend" = "freebuff" ]; then
|
||||
cmd="freebuff-agent --brief $(printf '%q' "$brief") --dir $(printf '%q' "$dir") --id $(printf '%q' "$id")"
|
||||
else
|
||||
cmd="free-agent --brief $(printf '%q' "$brief") --dir $(printf '%q' "$dir") --role $(printf '%q' "$role") --id $(printf '%q' "$id")"
|
||||
fi
|
||||
tmux new-window -d -t "$TMUX_SESSION" -n "$id" -c "$dir" \
|
||||
"free-agent --brief $(printf '%q' "$brief") --dir $(printf '%q' "$dir") --role $(printf '%q' "$role") --id $(printf '%q' "$id"); printf '\nhandoff window finished: %s\n' \$?; sleep 2" \
|
||||
"$cmd; printf '\nhandoff window finished: %s\n' \$?; sleep 2" \
|
||||
>/dev/null 2>&1 || { echo "handoff: tmux failed for $id" >&2; mv "$RUNNING/$id.json" "$FAILED/$id.json"; return 1; }
|
||||
echo "[$(now)] started $id ($role) in $dir"
|
||||
echo "[$(now)] started $id ($backend/$role) in $dir"
|
||||
}
|
||||
|
||||
collect_finished() {
|
||||
@@ -139,7 +146,7 @@ cmd_list() {
|
||||
id="$(basename "$f" .json)"
|
||||
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("state","-"))' "$st" 2>/dev/null)"
|
||||
[ -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")"
|
||||
done
|
||||
done
|
||||
|
||||
@@ -40,6 +40,11 @@ handoff submit BRIEF.md --dir ~/Projects/thing --role code --id fix-tests
|
||||
config/runbooks. The daemon (systemd user unit `handoff.service`, tmux session
|
||||
`handoff`) picks it up within 5 s and runs it in a new window.
|
||||
|
||||
Add `--backend freebuff` to hand that task to **Freebuff** instead (the
|
||||
ad-supported agent on the account already logged in; it drives its TUI in tmux
|
||||
via `bin/freebuff-agent`). Freebuff ignores the pool and its budgets entirely, so
|
||||
use it when the free API lanes are spent or the task needs a long session.
|
||||
|
||||
```sh
|
||||
handoff list # queue / running / done / failed
|
||||
handoff log fix-tests 60 # last 60 lines of the worker's transcript
|
||||
@@ -79,3 +84,8 @@ Ns" reply it sleeps the delay and retries the same model before spending another
|
||||
lane. Free tiers are small: Gemini Flash is ~5 requests/minute, which a tool
|
||||
loop can burn in one turn — expect waits, keep briefs short, and prefer
|
||||
`--role code` tasks with few steps.
|
||||
|
||||
Lane health: `free-agent --probe` pings every lane and prints OK / BUSY / DEAD.
|
||||
`handoff-probe.timer` runs it Mondays at 08:00 and appends to
|
||||
`~/.local/state/free-agent/probe.log`; the unit only fails for DEAD lanes (retired
|
||||
slug, bad key, broken template), which is the signal that the pool needs an edit.
|
||||
Reference in new issue
Block a user