Compare commits

..

1 Commits

Author SHA1 Message Date
shokollm
1128b3dfa8 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
2026-04-02 09:29:30 +00:00

View File

@@ -146,9 +146,8 @@ issue_ref_to_worktree_name() {
issue_ref_to_worktree_path() {
local issue_ref="$1"
local parent_dir="${2:-$WORKTREES_DIR}"
local worktree_name=$(issue_ref_to_worktree_name "$issue_ref")
echo "$parent_dir/.kugetsu-worktrees/$worktree_name"
echo "$WORKTREES_DIR/$worktree_name"
}
issue_ref_to_branch_name() {
@@ -196,15 +195,13 @@ get_repo_url() {
worktree_exists() {
local issue_ref="$1"
local parent_dir="${2:-$PWD}"
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
[ -d "$worktree_path" ]
}
create_worktree() {
local issue_ref="$1"
local parent_dir="${2:-$PWD}"
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
local branch_name=$(issue_ref_to_branch_name "$issue_ref")
local repo_url=$(get_repo_url "$issue_ref")
@@ -214,10 +211,9 @@ create_worktree() {
exit 1
fi
local worktree_parent_dir=$(dirname "$worktree_path")
mkdir -p "$worktree_parent_dir"
ensure_worktree_dir
if worktree_exists "$issue_ref" "$parent_dir"; then
if worktree_exists "$issue_ref"; then
echo "Removing existing worktree at '$worktree_path'..."
git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path"
fi
@@ -238,10 +234,9 @@ create_worktree() {
remove_worktree_for_issue() {
local issue_ref="$1"
local parent_dir="${2:-$PWD}"
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
if worktree_exists "$issue_ref" "$parent_dir"; then
if worktree_exists "$issue_ref"; then
echo "Removing worktree at '$worktree_path'..."
git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path"
fi
@@ -1220,9 +1215,8 @@ cmd_start() {
exit 1
fi
local parent_dir="$PWD"
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
create_worktree "$issue_ref" "$parent_dir"
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
create_worktree "$issue_ref"
local session_file="$(issue_ref_to_filename "$issue_ref").json"
@@ -1239,44 +1233,66 @@ cmd_start() {
if [ "$active_count" -ge "$MAX_CONCURRENT_AGENTS" ]; then
echo "Error: Max concurrent agents ($MAX_CONCURRENT_AGENTS) reached" >&2
echo "Active sessions: $active_count" >&2
remove_worktree_for_issue "$issue_ref" "$parent_dir"
remove_worktree_for_issue "$issue_ref"
exit 1
fi
local fork_log="$SESSIONS_DIR/$session_file.fork.log"
if [ "$DEBUG_MODE" = true ]; then
(cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id" 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
(cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id" 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
local fork_pid=$!
# Find the new session by comparing before/after lists
# Skip any session that existed before the fork and skip base/pm-agent
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
# 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
while [ $attempt -le $max_attempts ]; do
sleep 1
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
@@ -1337,9 +1353,9 @@ cmd_continue() {
if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
echo "Using worktree: $worktree_path"
if [ "$DEBUG_MODE" = true ]; then
(cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" 2>&1) | tee "$session_path.debug.log" &
opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 | tee "$session_path.debug.log" &
else
(cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" 2>&1) &
opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 &
fi
else
if [ "$DEBUG_MODE" = true ]; then