From 1128b3dfa80bdc7314f3bce6427415d727b392b7 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Thu, 2 Apr 2026 09:29:30 +0000 Subject: [PATCH] fix(kugetsu): improve session detection in cmd_start with retry logic and logging - Capture fork output to log file for debugging - Track fork PID to detect if process exits early - Retry session detection up to 10 seconds instead of 1 second - Show fork log output when session creation fails - Improve error message to indicate timeout --- skills/kugetsu/scripts/kugetsu | 68 ++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/skills/kugetsu/scripts/kugetsu b/skills/kugetsu/scripts/kugetsu index c7ea0f9..49c3484 100755 --- a/skills/kugetsu/scripts/kugetsu +++ b/skills/kugetsu/scripts/kugetsu @@ -1237,40 +1237,62 @@ cmd_start() { exit 1 fi + local fork_log="$SESSIONS_DIR/$session_file.fork.log" + if [ "$DEBUG_MODE" = true ]; then - opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 | tee "$SESSIONS_DIR/$session_file.debug.log" & + opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 | tee "$fork_log" & else - opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 & + opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" >> "$fork_log" 2>&1 & fi - # Wait briefly for session to be created - sleep 1 - - # Find the new session by comparing before/after lists - # Skip any session that existed before the fork and skip base/pm-agent + local fork_pid=$! + + local max_attempts=10 + local attempt=1 local new_session_id="" - while IFS= read -r sess; do - # Skip base and pm-agent - [ "$sess" = "$base_session_id" ] && continue - [ "$sess" = "$pm_agent_session_id" ] && continue + + while [ $attempt -le $max_attempts ]; do + sleep 1 - # Check if this session existed before - local existed_before=false - for before_sess in "${before_sessions[@]}"; do - if [ "$sess" = "$before_sess" ]; then - existed_before=true - break + if ! kill -0 $fork_pid 2>/dev/null; then + if [ -s "$fork_log" ]; then + echo "Fork command exited. Log output:" >&2 + tail -20 "$fork_log" >&2 fi - done - - if [ "$existed_before" = false ]; then - new_session_id="$sess" break fi - done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+') + + while IFS= read -r sess; do + [ "$sess" = "$base_session_id" ] && continue + [ "$sess" = "$pm_agent_session_id" ] && continue + + local existed_before=false + for before_sess in "${before_sessions[@]}"; do + if [ "$sess" = "$before_sess" ]; then + existed_before=true + break + fi + done + + if [ "$existed_before" = false ]; then + new_session_id="$sess" + break + fi + done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+') + + if [ -n "$new_session_id" ]; then + break + fi + + attempt=$((attempt + 1)) + done if [ -z "$new_session_id" ]; then - echo "Error: Could not find newly created session" >&2 + echo "Error: Could not find newly created session after ${max_attempts}s" >&2 + if [ -f "$fork_log" ] && [ -s "$fork_log" ]; then + echo "Fork log:" >&2 + tail -30 "$fork_log" >&2 + fi remove_worktree_for_issue "$issue_ref" exit 1 fi -- 2.49.1