refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
#!/usr/bin/env bash
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# agent-session.sh — Shared tmux + Claude interactive session helpers
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
#
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# Source this into agent orchestrator scripts for reusable session management.
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
#
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# Functions:
|
|
|
|
|
|
# agent_wait_for_claude_ready SESSION_NAME [TIMEOUT_SECS]
|
|
|
|
|
|
# agent_inject_into_session SESSION_NAME TEXT
|
|
|
|
|
|
# agent_kill_session SESSION_NAME
|
2026-03-18 20:15:14 +00:00
|
|
|
|
# monitor_phase_loop PHASE_FILE IDLE_TIMEOUT_SECS CALLBACK_FN [SESSION_NAME]
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# Wait for the Claude ❯ ready prompt in a tmux pane.
|
|
|
|
|
|
# Returns 0 if ready within TIMEOUT_SECS (default 120), 1 otherwise.
|
|
|
|
|
|
agent_wait_for_claude_ready() {
|
|
|
|
|
|
local session="$1"
|
|
|
|
|
|
local timeout="${2:-120}"
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
local elapsed=0
|
|
|
|
|
|
while [ "$elapsed" -lt "$timeout" ]; do
|
2026-03-18 16:21:07 +01:00
|
|
|
|
if tmux capture-pane -t "$session" -p 2>/dev/null | grep -q '❯'; then
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
return 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
sleep 2
|
|
|
|
|
|
elapsed=$((elapsed + 2))
|
|
|
|
|
|
done
|
|
|
|
|
|
return 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# Paste TEXT into SESSION (waits for Claude to be ready first), then press Enter.
|
|
|
|
|
|
agent_inject_into_session() {
|
|
|
|
|
|
local session="$1"
|
|
|
|
|
|
local text="$2"
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
local tmpfile
|
2026-03-18 16:21:07 +01:00
|
|
|
|
agent_wait_for_claude_ready "$session" 120 || true
|
|
|
|
|
|
tmpfile=$(mktemp /tmp/agent-inject-XXXXXX)
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
printf '%s' "$text" > "$tmpfile"
|
2026-03-18 16:21:07 +01:00
|
|
|
|
tmux load-buffer -b "agent-inject-$$" "$tmpfile"
|
|
|
|
|
|
tmux paste-buffer -t "$session" -b "agent-inject-$$"
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
sleep 0.5
|
2026-03-18 16:21:07 +01:00
|
|
|
|
tmux send-keys -t "$session" "" Enter
|
|
|
|
|
|
tmux delete-buffer -b "agent-inject-$$" 2>/dev/null || true
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
rm -f "$tmpfile"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:21:05 +00:00
|
|
|
|
# Create a tmux session running Claude in the given workdir.
|
|
|
|
|
|
# Returns 0 if session is ready, 1 otherwise.
|
|
|
|
|
|
create_agent_session() {
|
|
|
|
|
|
local session="$1"
|
|
|
|
|
|
local workdir="${2:-.}"
|
|
|
|
|
|
tmux new-session -d -s "$session" -c "$workdir" \
|
|
|
|
|
|
"claude --dangerously-skip-permissions" 2>/dev/null
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
tmux has-session -t "$session" 2>/dev/null || return 1
|
|
|
|
|
|
agent_wait_for_claude_ready "$session" 120 || return 1
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Inject a prompt/formula into a session (alias for agent_inject_into_session).
|
|
|
|
|
|
inject_formula() {
|
|
|
|
|
|
agent_inject_into_session "$@"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:40:51 +00:00
|
|
|
|
# Monitor a phase file, calling a callback on changes and handling idle timeout.
|
|
|
|
|
|
# Sets _MONITOR_LOOP_EXIT to the exit reason (idle_timeout, done, failed, break).
|
2026-03-19 01:05:21 +00:00
|
|
|
|
# Sets _MONITOR_SESSION to the resolved session name (arg 4 or $SESSION_NAME).
|
|
|
|
|
|
# Callbacks should reference _MONITOR_SESSION instead of $SESSION_NAME directly.
|
2026-03-18 20:15:14 +00:00
|
|
|
|
# Args: phase_file idle_timeout_secs callback_fn [session_name]
|
|
|
|
|
|
# session_name — tmux session to health-check; falls back to $SESSION_NAME global
|
2026-03-18 16:40:51 +00:00
|
|
|
|
monitor_phase_loop() {
|
|
|
|
|
|
local phase_file="$1"
|
|
|
|
|
|
local idle_timeout="$2"
|
|
|
|
|
|
local callback="$3"
|
2026-03-18 20:15:14 +00:00
|
|
|
|
local _session="${4:-${SESSION_NAME:-}}"
|
2026-03-19 01:05:21 +00:00
|
|
|
|
# Export resolved session name so callbacks can reference it regardless of
|
|
|
|
|
|
# which session was passed to monitor_phase_loop (analogous to _MONITOR_LOOP_EXIT).
|
|
|
|
|
|
export _MONITOR_SESSION="$_session"
|
2026-03-18 16:40:51 +00:00
|
|
|
|
local poll_interval="${PHASE_POLL_INTERVAL:-10}"
|
|
|
|
|
|
local last_mtime=0
|
|
|
|
|
|
local idle_elapsed=0
|
|
|
|
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
|
|
sleep "$poll_interval"
|
|
|
|
|
|
idle_elapsed=$(( idle_elapsed + poll_interval ))
|
|
|
|
|
|
|
|
|
|
|
|
# Session health check
|
2026-03-18 20:15:14 +00:00
|
|
|
|
if ! tmux has-session -t "${_session}" 2>/dev/null; then
|
2026-03-18 16:40:51 +00:00
|
|
|
|
local current_phase
|
|
|
|
|
|
current_phase=$(head -1 "$phase_file" 2>/dev/null | tr -d '[:space:]' || true)
|
|
|
|
|
|
case "$current_phase" in
|
|
|
|
|
|
PHASE:done|PHASE:failed|PHASE:merged)
|
|
|
|
|
|
;; # terminal — fall through to phase handler
|
|
|
|
|
|
*)
|
|
|
|
|
|
# Call callback with "crashed" — let agent-specific code handle recovery
|
|
|
|
|
|
if type "${callback}" &>/dev/null; then
|
|
|
|
|
|
"$callback" "PHASE:crashed"
|
|
|
|
|
|
fi
|
|
|
|
|
|
# If callback didn't restart session, break
|
2026-03-18 20:15:14 +00:00
|
|
|
|
if ! tmux has-session -t "${_session}" 2>/dev/null; then
|
2026-03-18 16:40:51 +00:00
|
|
|
|
_MONITOR_LOOP_EXIT="crashed"
|
|
|
|
|
|
return 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
idle_elapsed=0
|
|
|
|
|
|
continue
|
|
|
|
|
|
;;
|
|
|
|
|
|
esac
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Check phase file for changes
|
|
|
|
|
|
local phase_mtime
|
|
|
|
|
|
phase_mtime=$(stat -c %Y "$phase_file" 2>/dev/null || echo 0)
|
|
|
|
|
|
local current_phase
|
|
|
|
|
|
current_phase=$(head -1 "$phase_file" 2>/dev/null | tr -d '[:space:]' || true)
|
|
|
|
|
|
|
|
|
|
|
|
if [ -z "$current_phase" ] || [ "$phase_mtime" -le "$last_mtime" ]; then
|
|
|
|
|
|
# No phase change — check idle timeout
|
|
|
|
|
|
if [ "$idle_elapsed" -ge "$idle_timeout" ]; then
|
|
|
|
|
|
_MONITOR_LOOP_EXIT="idle_timeout"
|
2026-03-18 20:15:14 +00:00
|
|
|
|
agent_kill_session "${_session}"
|
2026-03-18 16:40:51 +00:00
|
|
|
|
return 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
continue
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Phase changed
|
|
|
|
|
|
last_mtime="$phase_mtime"
|
2026-03-18 17:26:54 +00:00
|
|
|
|
# shellcheck disable=SC2034 # read by phase-handler.sh callback
|
|
|
|
|
|
LAST_PHASE_MTIME="$phase_mtime"
|
2026-03-18 16:40:51 +00:00
|
|
|
|
idle_elapsed=0
|
|
|
|
|
|
|
|
|
|
|
|
# Terminal phases
|
|
|
|
|
|
case "$current_phase" in
|
|
|
|
|
|
PHASE:done|PHASE:merged)
|
|
|
|
|
|
_MONITOR_LOOP_EXIT="done"
|
|
|
|
|
|
if type "${callback}" &>/dev/null; then
|
|
|
|
|
|
"$callback" "$current_phase"
|
|
|
|
|
|
fi
|
|
|
|
|
|
return 0
|
|
|
|
|
|
;;
|
|
|
|
|
|
PHASE:failed|PHASE:needs_human)
|
|
|
|
|
|
_MONITOR_LOOP_EXIT="$current_phase"
|
|
|
|
|
|
if type "${callback}" &>/dev/null; then
|
|
|
|
|
|
"$callback" "$current_phase"
|
|
|
|
|
|
fi
|
|
|
|
|
|
return 0
|
|
|
|
|
|
;;
|
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
# Non-terminal phase — call callback
|
|
|
|
|
|
if type "${callback}" &>/dev/null; then
|
|
|
|
|
|
"$callback" "$current_phase"
|
|
|
|
|
|
fi
|
|
|
|
|
|
done
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:21:07 +01:00
|
|
|
|
# Kill a tmux session gracefully (no-op if not found).
|
|
|
|
|
|
agent_kill_session() {
|
2026-03-18 16:24:58 +00:00
|
|
|
|
local session="${1:-}"
|
|
|
|
|
|
[ -n "$session" ] && tmux kill-session -t "$session" 2>/dev/null || true
|
refactor: extract lib/agent-session.sh — reusable tmux + Claude agent runtime (#158)
Move generic agent infrastructure from dev/dev-agent.sh into lib/agent-session.sh:
- log, status, notify, notify_ctx, read_phase, wait_for_claude_ready,
inject_into_session, kill_tmux_session extracted verbatim
- create_agent_session(session_name, workdir) — new: tmux session creation
- inject_formula(session_name, formula_text, context) — new: prompt injection
- monitor_phase_loop(phase_file, idle_timeout, callback_fn) — new: phase loop
with session health check, crash recovery, and idle timeout detection
dev-agent.sh: sources the library, implements _on_phase_change() callback,
calls monitor_phase_loop(); idle-timeout and crash-recovery-failed cleanup
handled via _MONITOR_LOOP_EXIT signal variable. Behavior unchanged.
2026-03-18 14:36:36 +00:00
|
|
|
|
}
|
2026-03-18 17:11:02 +00:00
|
|
|
|
|
|
|
|
|
|
# Read the current phase from a phase file, stripped of whitespace.
|
|
|
|
|
|
# Usage: read_phase [file] — defaults to $PHASE_FILE
|
|
|
|
|
|
read_phase() {
|
|
|
|
|
|
local file="${1:-${PHASE_FILE:-}}"
|
|
|
|
|
|
{ cat "$file" 2>/dev/null || true; } | head -1 | tr -d '[:space:]'
|
|
|
|
|
|
}
|