fix: use cd + worktree inside parent dir instead of --dir flag (fixes #105) #106

Merged
shoko merged 1 commits from fix/worktree-isolation-via-cd into main 2026-04-02 10:29:33 +02:00
Showing only changes of commit ede47439b0 - Show all commits

View File

@@ -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