diff --git a/skills/kugetsu/scripts/kugetsu b/skills/kugetsu/scripts/kugetsu index c7ea0f9..d199d98 100755 --- a/skills/kugetsu/scripts/kugetsu +++ b/skills/kugetsu/scripts/kugetsu @@ -146,8 +146,9 @@ 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 "$WORKTREES_DIR/$worktree_name" + echo "$parent_dir/.kugetsu-worktrees/$worktree_name" } issue_ref_to_branch_name() { @@ -195,13 +196,15 @@ get_repo_url() { worktree_exists() { local issue_ref="$1" - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref") + local parent_dir="${2:-$PWD}" + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir") [ -d "$worktree_path" ] } create_worktree() { local issue_ref="$1" - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref") + local parent_dir="${2:-$PWD}" + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir") local branch_name=$(issue_ref_to_branch_name "$issue_ref") local repo_url=$(get_repo_url "$issue_ref") @@ -211,9 +214,10 @@ create_worktree() { exit 1 fi - ensure_worktree_dir + local worktree_parent_dir=$(dirname "$worktree_path") + mkdir -p "$worktree_parent_dir" - if worktree_exists "$issue_ref"; then + if worktree_exists "$issue_ref" "$parent_dir"; then echo "Removing existing worktree at '$worktree_path'..." git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path" fi @@ -234,9 +238,10 @@ create_worktree() { remove_worktree_for_issue() { local issue_ref="$1" - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref") + local parent_dir="${2:-$PWD}" + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir") - if worktree_exists "$issue_ref"; then + if worktree_exists "$issue_ref" "$parent_dir"; then echo "Removing worktree at '$worktree_path'..." git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path" fi @@ -1215,8 +1220,9 @@ cmd_start() { exit 1 fi - local worktree_path=$(issue_ref_to_worktree_path "$issue_ref") - create_worktree "$issue_ref" + local parent_dir="$PWD" + local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir") + create_worktree "$issue_ref" "$parent_dir" local session_file="$(issue_ref_to_filename "$issue_ref").json" @@ -1233,14 +1239,14 @@ 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" + remove_worktree_for_issue "$issue_ref" "$parent_dir" exit 1 fi 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" & + (cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id" 2>&1) | tee "$SESSIONS_DIR/$session_file.debug.log" & else - opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 & + (cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id" 2>&1) & fi # Wait briefly for session to be created @@ -1331,9 +1337,9 @@ cmd_continue() { if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then echo "Using worktree: $worktree_path" if [ "$DEBUG_MODE" = true ]; then - opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 | tee "$session_path.debug.log" & + (cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" 2>&1) | tee "$session_path.debug.log" & else - opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 & + (cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" 2>&1) & fi else if [ "$DEBUG_MODE" = true ]; then