8000 fix(base): honest launch status (#24) by simon-inkie · Pull Request #46 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,33 @@ again and an agent whose pane already has its runtime running in it is left
alone; one whose runtime has exited is relaunched, which is what makes this
safe to fire on boot.

On first boot, attach to the session and answer any runtime setup prompts
manually. Folder trust defaults to No: do not press Enter while No is
preselected. Choose Yes manually only if you trust the folder. Claude Code
uses its default permission mode (currently auto) unless you configure it.
The resume picker and fullscreen offer also need a manual answer.
The launcher reports ready launches and confirmed monitors separately; a
live process alone does not prove a fresh launch is ready. Readiness waits
up to 45 seconds per fresh launch; set `BOOT_READY_SECS` to a positive integer
for slower machines. Recognised setup prompts stop the wait early.

Exit statuses:

- `0`: fresh launches are ready and requested monitors are confirmed.
- `1`: a launch failed.
- `3`: readiness or monitor confirmation is pending.

Callers that treat non-zero statuses as failures, including a default systemd
boot unit, will report failure even when a healthy first boot only needs a
manual trust answer. A unit that accepts this pending state can set `SuccessExitStatus=3`.
After answering prompts, run the launcher again to check known setup dialogs.
Existing sessions, including busy conversations, are left running and are
not automatically re-armed. If a fresh launch exceeds the readiness budget
or stops at a setup prompt, its opted-in monitor is not armed on that run or
on a later re-run: once the composer is ready, type `arm your monitor` manually.
Monitor instructions are sent once, with confirmation polled from bounded
pane history to avoid duplicate watcher requests.

```sh
tmux list-windows -t agents -F '#{window_name}'
```
Expand Down
206 changes: 160 additions & 46 deletions packages/base/bin/boot-agents.sh
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,98 @@ codex_verify_resume_or_fallback() {
return 1
}

# Readiness is positive evidence, not merely a live node process. Never answer
# first-run permission prompts here; unknown screens remain waiting.
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/pane-idle"
if [[ "${PANE_IDLE_LIB_VERSION:-0}" -lt 3 ]]; then
echo "boot-agents: pane-idle library version 3 required" >&2
exit 1
fi

pane_readiness() {
local target="$1" runtime="$2" pane cmd composer raw line previous="" anchored=0
pane=$(tmux capture-pane -t "$target" -p 2>/dev/null) || { echo "pane unavailable"; return 1; }
pane=$(printf '%s\n' "$pane" | tail -n 30)
if pane_has_codex_approval_dialog "$pane"; then echo "approval prompt: answer manually"; return 1; fi
cmd=$(tmux list-panes -t "$target" -F '#{pane_current_command}' 2>/dev/null) || { echo "process unavailable"; return 1; }
case "$cmd" in
node|claude|codex|"${runtime##*/}") ;;
*) echo "runtime not running"; return 1 ;;
esac
# Claude and Gemini composers have rules. Codex identifies its composer
# through its banner and prompt glyph. Menu options are not ready evidence.
case "$runtime" in
codex) printf '%s\n' "$pane" | grep -qF 'OpenAI Codex' || { echo "unrecognised screen"; return 1; } ;;
*) printf '%s\n' "$pane" | grep -qF '────────' || { echo "unrecognised screen"; return 1; } ;;
esac
if check_idle "$pane"; then echo "runtime busy"; return 1; fi
composer=$(find_composer_line "$pane")
[[ -n "$composer" ]] || { echo "composer unavailable"; return 1; }
if [[ "$runtime" == codex ]]; then
printf '%s\n' "$composer" | grep -qE '^[[:space:]]*›' || { echo "composer unconfirmed"; return 1; }
else
while IFS= read -r line; do
if [[ "$line" == "$composer" && "$previous" == *────────* ]]; then anchored=1; fi
previous="$line"
done <<< "$pane"
[[ "$anchored" -eq 1 ]] || { echo "composer unconfirmed"; return 1; }
fi
if printf '%s\n' "$composer" | sed -E -e 's/^[[:space:]]*(❯|›|>)[[:space:]]*//' -e $'s/\xc2\xa0//g' | grep -q '[^[:space:]]'; then
raw=$(tmux capture-pane -t "$target" -p -e 2>/dev/null) || { echo "composer unconfirmed"; return 1; }
raw=$(raw_line_for "$raw" "$composer")
printf '%s' "$raw" | grep -qF "$(printf '\033[2m')" || { echo "composer contains text or menu selection"; return 1; }
fi
echo "ready"
return 0
}

# Match rendered setup dialogs, rather than phrases in conversation output.
manual_prompt_reason() {
local pane="$1"
if pane_has_codex_approval_dialog "$pane"; then
echo "approval prompt: answer manually"
return 0
fi
printf '%s\n' "$pane" | grep -qE '^[[:space:]]*(❯|›)[[:space:]]*[^[:space:]]' || return 1
if printf '%s\n' "$pane" | grep -qiE '^[[:space:]]*(Do you trust (the files|this folder|this workspace)|Trust (this folder|the files|this workspace))'; then
echo "folder trust: attach and choose Yes manually if you trust the folder; do not press Enter, No is preselected"
elif printf '%s\n' "$pane" | grep -qiE '^[[:space:]]*(Resume a conversation|Select a conversation|Resume picker)([[:space:]?:]|$)'; then
echo "resume picker: choose a conversation manually"
elif printf '%s\n' "$pane" | grep -qiE '^[[:space:]]*(Try|Use|Enable|Switch to) (the )?full.?screen (renderer|mode)'; then
echo "fullscreen offer: answer manually"
else
return 1
fi
}

existing_readiness() {
local pane reason
pane=$(tmux capture-pane -t "$1" -p 2>/dev/null) || { echo "pane unavailable"; return 1; }
pane=$(printf '%s\n' "$pane" | tail -n 30)
if reason=$(manual_prompt_reason "$pane"); then echo "$reason"; return 1; fi
# launch_agent already confirmed a running runtime. Busy output and a banner
# scrolled out of view are normal for an existing conversation.
return 0
}

wait_for_readiness() {
local target="$1" runtime="$2" reason="unconfirmed" attempt pane prompt
local budget="${BOOT_READY_SECS:-45}"
case "$budget" in ''|*[!0-9]*|0) budget=45 ;; esac
for ((attempt=0; attempt<budget; attempt++)); do
if reason=$(pane_readiness "$target" "$runtime"); then return 0; fi
pane=$(tmux capture-pane -t "$target" -p 2>/dev/null | tail -n 30)
if prompt=$(manual_prompt_reason "$pane"); then
reason="$prompt"
break
fi
case "$reason" in *manually*) break ;; esac
sleep 1
done
printf '%s\n' "$reason"
return 1
}

# codex_home_for_agent <agent>
# Provider state is written by bin/codex-provider-switch. A committed rollback
# must survive the next boot even though the dormant Azure home remains on disk.
Expand Down Expand Up @@ -606,7 +698,7 @@ fi

tmux start-server 2>/dev/null || true
if ! tmux has-session -t "$SESSION" 2>/dev/null; then
tmux new-session -d -s "$SESSION" -n "${AGENTS[0]}" -c "$AGENTS_HOME/${AGENTS[0]}"
tmux new-session -d -s "$SESSION" -n "${AGENTS[0]}" -c "$AGENTS_HOME/${AGENTS[0]}" || { echo "boot-agents: session creation failed" >&2; exit 1; }
fi

# Launch an agent if it is not already running. Echoes "launched" or "skipped".
Expand All @@ -621,9 +713,10 @@ launch_agent() {
# Per-agent model override (selector): $AGENTS_HOME/<a>/.model -> --model flag.
# Absent = no flag = inherit the global default. Applies to all runtimes.
[[ -f "$AGENTS_HOME/$a/.model" ]] && model_flag="--model $(cat "$AGENTS_HOME/$a/.model")"
tmux list-windows -t "$SESSION" -F '#{window_name}' | grep -qx "$a" \
|| tmux new-window -t "$SESSION" -n "$a" -c "$AGENTS_HOME/$a"
if tmux list-panes -t "$target" -F '#{pane_current_command}' 2>/dev/null | grep -qiE 'claude|node'; then
if ! tmux list-windows -t "$SESSION" -F '#{window_name}' | grep -qx "$a"; then
tmux new-window -t "$SESSION" -n "$a" -c "$AGENTS_HOME/$a" >/dev/null || return 1
fi
if tmux list-panes -t "$target" -F '#{pane_current_command}' 2>/dev/null | grep -qiE '^(claude|codex|node)$'; then
echo "skipped"; return
fi
if [[ "$runtime" == "claude" ]]; then
Expand All @@ -643,7 +736,7 @@ launch_agent() {
echo "boot-agents: could not pin a session id for $a; launching an unpinned session" >&2
session_args="-n $(printf '%q' "$a")"
}
tmux send-keys -t "$target" "printf '\033]0;${a}\007'; [ -f $AGENTS_HOME/$a/.env.secrets ] && . $AGENTS_HOME/$a/.env.secrets; claude $session_args $model_flag" Enter
tmux send-keys -t "$target" "printf '\033]0;${a}\007'; [ -f $AGENTS_HOME/$a/.env.secrets ] && . $AGENTS_HOME/$a/.env.secrets; claude $session_args $model_flag" Enter || return 1
elif [[ "$runtime" == "codex" ]]; then
# codex (Codex CLI runtime). AGENT_NAME/AGENT_ROLE must
# be REAL OS-level env vars set before the codex process starts.
Expand All @@ -659,19 +752,18 @@ launch_agent() {
local sid; sid="$(codex_latest_session_for_workspace "$a")"
local codex_cmd
codex_cmd="$(codex_launch_command "$a" "$role" "$codex_home" "$sid" "$model_flag")"
tmux send-keys -t "$target" "$codex_cmd" Enter
tmux send-keys -t "$target" "$codex_cmd" Enter || return 1
# A selected id is not a promise the resume succeeded, so verify the pane
# and recover it if it did not come up (codex_verify_resume_or_fallback,
# above). The same call dismisses Codex's update-available dialog, which
# is deliberately not gated on $sid so a first boot gets it too.
codex_verify_resume_or_fallback "$target" "$a" "$sid" "$role" "$codex_home" "$model_flag"

codex_verify_resume_or_fallback "$target" "$a" "$sid" "$role" "$codex_home" "$model_flag" || return 1
else
# Any other runtime: launch it plainly. If it needs env vars set before
# the process starts rather than after, set them here explicitly -- some
# runtimes only apply hook-declared env to their own shell tool calls, not
# to the process itself, so a hook-only approach can silently under-scope.
tmux send-keys -t "$target" "printf '\033]0;${a}\007'; AGENT_NAME=$a $runtime $model_flag" Enter
tmux send-keys -t "$target" "printf '\033]0;${a}\007'; AGENT_NAME=$a $runtime $model_flag" Enter || return 1
fi
echo "launched"
}
Expand All @@ -684,10 +776,32 @@ launch_agent() {
# BOOT_STAGGER_SECS=0 for a fast serial boot on a quiet box.
STAGGER_SECS="${BOOT_STAGGER_SECS:-12}"
declare -a FRESH=()
LAUNCHED=0
ALREADY_UP=0
WAITING=0
FAILED=0
ARMED=0
for a in "${AGENTS[@]}"; do
if [[ "$(launch_agent "$a")" == "launched" ]]; then
FRESH+=("$a")
[[ "$STAGGER_SECS" -gt 0 ]] && sleep "$STAGGER_SECS"
if outcome=$(launch_agent "$a"); then
readiness_check=wait_for_readiness
[[ "$outcome" == skipped ]] && readiness_check=existing_readiness
if reason=$("$readiness_check" "$SESSION:$a" "$(get_runtime "$a")"); then
if [[ "$outcome" == "launched" ]]; then
FRESH+=("$a")
LAUNCHED=$((LAUNCHED + 1))
echo "boot-agents: $a launched, ready"
[[ "$STAGGER_SECS" -gt 0 ]] && sleep "$STAGGER_SECS"
else
ALREADY_UP=$((ALREADY_UP + 1))
echo "boot-agents: $a already up, ready"
fi
else
WAITING=$((WAITING + 1))
echo "boot-agents: $a waiting: $reason" >&2
fi
else
FAILED=$((FAILED + 1))
echo "boot-agents: $a launch failed or verification unconfirmed" >&2
fi
done

Expand All @@ -702,39 +816,36 @@ should_auto_arm() {
[[ -f "$AGENTS_HOME/$a/.auto-arm-watcher" ]]
}

# Auto-arm the watcher on freshly-launched, opt-in agents. Wait for the TUI to
# be ready, then type the prompt. Adjust "arm your monitor" to whatever your
# own watcher-arming instruction is.
# Only ready, freshly launched, opted-in agents receive monitor instructions.
for a in "${FRESH[@]}"; do
should_auto_arm "$a" || continue
target="$SESSION:$a"
# wait up to ~45s for the runtime to be the running command in the pane
for _ in $(seq 1 45); do
tmux list-panes -t "$target" -F '#{pane_current_command}' 2>/dev/null | grep -qiE 'claude|node' && break
sleep 1
done
sleep 6 # extra settle so the input box is ready to accept keys
# text and Enter sent separately: one send-keys call carrying both text and
# Enter can race a TUI that hasn't finished drawing its composer yet.
tmux send-keys -t "$target" "arm your monitor"
sleep 0.7
tmux send-keys -t "$target" Enter
done

# Verify each fresh agent actually started its watcher, and re-send if it
# missed (a resume can land mid-compaction, or a slow TUI can drop the keys).
# Up to a few retries makes the arm self-healing.
for a in "${FRESH[@]}"; do
should_auto_arm "$a" || continue
if ! should_auto_arm "$a"; then
echo "boot-agents: $a monitor not requested"
continue
fi
target="$SESSION:$a"
for _ in 1 2 3 4; do
sleep 8
tmux capture-pane -t "$target" -p 2>/dev/null \
| grep -qiE 'Monitor\(|inbox events|Monitor started' && break
tmux send-keys -t "$target" "arm your monitor"
sleep 0.7
tmux send-keys -t "$target" Enter
done
confirmed=0
monitor_pattern='^[^[:alnum:]]*(Monitor\(|Monitor started([[:space:]]|$))'
baseline=$(tmux capture-pane -t "$target" -p -S -2000 2>/dev/null | grep -ciE "$monitor_pattern")
# Send once: repeated prompts can create duplicate watchers. Poll only.
if ! reason=$(wait_for_readiness "$target" "$(get_runtime "$a")"); then
echo "boot-agents: $a monitor waiting: $reason" >&2
elif tmux send-keys -t "$target" "arm your monitor" && sleep 0.7 && tmux send-keys -t "$target" Enter; then
for poll in 1 2 3 4; do
sleep 8
observed=$(tmux capture-pane -t "$target" -p -S -2000 2>/dev/null | grep -ciE "$monitor_pattern")
if [[ "$observed" -gt "$baseline" ]]; then
confirmed=1
break
fi
done
fi
if [[ "$confirmed" -eq 1 ]]; then
ARMED=$((ARMED + 1))
echo "boot-agents: $a monitor armed (pane confirmation)"
else
WAITING=$((WAITING + 1))
echo "boot-agents: $a monitor unconfirmed" >&2
fi
done

# tmux quality-of-life (no-op if already set)
Expand All @@ -743,7 +854,10 @@ tmux set -g set-titles on 2>/dev/null || true
tmux set -g set-titles-string "#{window_name}" 2>/dev/null || true
tmux set -g allow-passthrough on 2>/dev/null || true

echo "boot-agents: ${#FRESH[@]} launched + armed (${FRESH[*]:-none}), $(( ${#AGENTS[@]} - ${#FRESH[@]} )) already up"
echo "boot-agents: $LAUNCHED launched and ready, $ARMED monitors armed, $ALREADY_UP already up and ready, $WAITING waiting, $FAILED failed"

[[ $ATTACH -eq 1 ]] && exec tmux attach -t "$SESSION"
exit 0
BOOT_RC=0
[[ "$WAITING" -eq 0 ]] || BOOT_RC=3
[[ "$FAILED" -eq 0 ]] || BOOT_RC=1
if [[ "$ATTACH" -eq 1 ]]; then tmux attach -t "$SESSION"; fi
exit "$BOOT_RC"
Loading
Loading
0