fix(kugetsu): improve session detection in cmd_start with retry logic and logging #107

Merged
shoko merged 2 commits from fix/start-session-detection into main 2026-04-02 11:41:53 +02:00
Showing only changes of commit 1128b3dfa8 - Show all commits

View File

@@ -1237,24 +1237,35 @@ 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
local fork_pid=$!
local max_attempts=10
local attempt=1
local new_session_id=""
while [ $attempt -le $max_attempts ]; do
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 new_session_id=""
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
break
fi
while IFS= read -r sess; do
# Skip base and pm-agent
[ "$sess" = "$base_session_id" ] && continue
[ "$sess" = "$pm_agent_session_id" ] && continue
# Check if this session existed before
local existed_before=false
for before_sess in "${before_sessions[@]}"; do
if [ "$sess" = "$before_sess" ]; then
@@ -1269,8 +1280,19 @@ cmd_start() {
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