From 4ec76b1248f3bf49cba5a8f4054530add0153ec4 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 01:29:49 +0000 Subject: [PATCH] fix: improve worktree/session handling in cmd_start and cmd_continue cmd_continue: - Check if worktree exists before continuing - If worktree is missing, remove stale session and exit with error - Tell user to use cmd_start instead cmd_start: - Check BOTH worktree AND session existence - If only worktree exists (not session): remove worktree, recreate both - If only session exists (not worktree): remove session, recreate both - If both exist: tell user to use continue Daemon: - Fixed wrong path in check_task_completion ($HOME/.kugetsu-worktrees -> $WORKTREES_DIR) --- .../kugetsu/scripts/kugetsu-queue-daemon.sh | 4 +- skills/kugetsu/scripts/kugetsu-session.sh | 55 +++++++++++++------ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/skills/kugetsu/scripts/kugetsu-queue-daemon.sh b/skills/kugetsu/scripts/kugetsu-queue-daemon.sh index 79ee84e..a36f1cb 100644 --- a/skills/kugetsu/scripts/kugetsu-queue-daemon.sh +++ b/skills/kugetsu/scripts/kugetsu-queue-daemon.sh @@ -44,7 +44,7 @@ check_task_completion() { if [ -n "$pid" ] && [ "$pid" != "None" ]; then if ! kill -0 "$pid" 2>/dev/null; then - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees") + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$WORKTREES_DIR") local has_commits=false if [ -d "$worktree_path" ] && [ -d "$worktree_path/.git" ]; then @@ -64,7 +64,7 @@ check_task_completion() { fi else if [ -n "$session_id" ] && ! opencode session list 2>/dev/null | grep -q "$session_id"; then - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees") + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$WORKTREES_DIR") local has_commits=false if [ -d "$worktree_path" ] && [ -d "$worktree_path/.git" ]; then diff --git a/skills/kugetsu/scripts/kugetsu-session.sh b/skills/kugetsu/scripts/kugetsu-session.sh index 644ec9f..37cf51f 100755 --- a/skills/kugetsu/scripts/kugetsu-session.sh +++ b/skills/kugetsu/scripts/kugetsu-session.sh @@ -309,26 +309,44 @@ cmd_start() { exit 1 fi + local session_file=$(issue_ref_to_filename "$issue_ref") + local session_path="$SESSIONS_DIR/$session_file" + local worktree_exists=false + if worktree_exists "$issue_ref"; then - echo "Issue '$issue_ref' already has a worktree. Use 'kugetsu continue' instead." + worktree_exists=true + fi + + local session_exists=false + if [ -f "$session_path" ]; then + session_exists=true + fi + + if $worktree_exists && $session_exists; then + echo "Issue '$issue_ref' already has a worktree and session." >&2 + echo "Use 'kugetsu continue $issue_ref' to continue work." >&2 exit 1 fi + if $worktree_exists && ! $session_exists; then + echo "Warning: Worktree exists but session is missing. Removing worktree to recreate both..." >&2 + remove_worktree_for_issue "$issue_ref" + worktree_exists=false + fi + + if ! $worktree_exists && $session_exists; then + echo "Warning: Session exists but worktree is missing. Removing stale session to recreate both..." >&2 + rm -f "$session_path" + remove_issue_from_index "$issue_ref" + session_exists=false + fi + local active_count=$(count_active_dev_sessions) if [ "$active_count" -ge "${MAX_CONCURRENT_AGENTS:-3}" ]; then echo "Error: Max concurrent agents (${MAX_CONCURRENT_AGENTS:-3}) reached. Use 'kugetsu continue' or wait for an agent to finish." >&2 exit 1 fi - local session_file=$(issue_ref_to_filename "$issue_ref") - local session_path="$SESSIONS_DIR/$session_file" - - if [ -f "$session_path" ]; then - echo "Session file already exists: $session_file" - echo "Use 'kugetsu continue $issue_ref' to continue work." - exit 1 - fi - create_worktree "$issue_ref" "$WORKTREES_DIR" local new_session_id=$(create_session "$base_session_id") @@ -400,16 +418,21 @@ cmd_continue() { local worktree_path=$(python3 -c "import json; print(json.load(open('$session_path')).get('worktree_path', ''))" 2>/dev/null || echo "") local issue_ref=$(python3 -c "import json; print(json.load(open('$session_path')).get('issue_ref', ''))" 2>/dev/null || echo "") + if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then + echo "Error: Worktree is missing for '$session_name'" >&2 + echo "Removing stale session file..." >&2 + rm -f "$session_path" + remove_issue_from_index "$session_name" + echo "Use 'kugetsu start $session_name' to create a new session." >&2 + exit 1 + fi + if [ -z "$message" ]; then message=$(build_dev_agent_message "$issue_ref" "") fi - if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then - cd "$worktree_path" - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & - else - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & - fi + cd "$worktree_path" + nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & } cmd_list() {