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..b004acd 100755 --- a/skills/kugetsu/scripts/kugetsu-session.sh +++ b/skills/kugetsu/scripts/kugetsu-session.sh @@ -83,6 +83,9 @@ EOF if [ -n "$existing_base" ] && [ "$existing_base" != "null" ]; then if [ "$force" = true ]; then echo "Warning: Reinitializing sessions (force mode)" >&2 + echo "Destroying existing sessions..." >&2 + cmd_destroy --base -y 2>/dev/null || true + cmd_destroy --pm-agent -y 2>/dev/null || true else echo "Error: Base session already exists: $existing_base" >&2 echo "Use --force to reinitialize" >&2 @@ -309,26 +312,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 +421,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 "Warning: Worktree is missing for '$session_name'. Recovering..." >&2 + rm -f "$session_path" + remove_issue_from_index "$session_name" + echo "Calling cmd_start to create new session and worktree..." >&2 + cmd_start "$session_name" "$message" + return $? + 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() {