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
Owner

Summary

Fixes issue #105 where opencode run --fork/--continue --dir <path> fails to create sessions.

Problem

Using --dir flag with --fork or --continue breaks session creation:

opencode run "test" --fork --session <id> --dir /path  # Session NOT created
opencode run "test" --fork --session <id>              # Session created

Root Cause

The --dir flag in opencode conflicts with session creation when using --fork or --continue.

Solution

Instead of using --dir, create worktrees inside the parent session's directory and use cd to change directory:

Before:

opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path"

After:

(cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id")

Worktrees are now created at $PWD/.kugetsu-worktrees/{issue-ref}/:

  • Hidden directory (git ignored by default - no .gitignore entry needed)
  • Inside parent session's directory tree (permissions work correctly)
  • Session gets created with correct directory set

Changes

  1. issue_ref_to_worktree_path() - now creates worktrees at $parent_dir/.kugetsu-worktrees/
  2. create_worktree() - accepts parent_dir parameter
  3. cmd_start() - uses cd && opencode run instead of --dir
  4. cmd_continue() - uses cd && opencode run instead of --dir

Testing

Both cd && opencode run and env -C ... opencode run work correctly with backgrounding (&).

## Summary Fixes issue #105 where `opencode run --fork/--continue --dir <path>` fails to create sessions. ### Problem Using `--dir` flag with `--fork` or `--continue` breaks session creation: ```bash opencode run "test" --fork --session <id> --dir /path # Session NOT created opencode run "test" --fork --session <id> # Session created ``` ### Root Cause The `--dir` flag in opencode conflicts with session creation when using `--fork` or `--continue`. ### Solution Instead of using `--dir`, create worktrees inside the parent session's directory and use `cd` to change directory: **Before:** ```bash opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" ``` **After:** ```bash (cd "$worktree_path" && opencode run "$message" --fork --session "$base_session_id") ``` Worktrees are now created at `$PWD/.kugetsu-worktrees/{issue-ref}/`: - Hidden directory (git ignored by default - no .gitignore entry needed) - Inside parent session's directory tree (permissions work correctly) - Session gets created with correct directory set ### Changes 1. `issue_ref_to_worktree_path()` - now creates worktrees at `$parent_dir/.kugetsu-worktrees/` 2. `create_worktree()` - accepts `parent_dir` parameter 3. `cmd_start()` - uses `cd && opencode run` instead of `--dir` 4. `cmd_continue()` - uses `cd && opencode run` instead of `--dir` ### Testing Both `cd && opencode run` and `env -C ... opencode run` work correctly with backgrounding (`&`).
shoko added 1 commit 2026-04-02 10:22:29 +02:00
Issue #105: opencode run --fork/--continue --dir <path> fails to create sessions

Root cause: The --dir flag breaks session creation in opencode. Sessions
fail to be created when --dir is used with --fork or --continue.

Solution: Instead of using --dir flag, create worktrees inside the parent
session's directory and use 'cd $worktree_path && opencode run ...' to
change directory before running opencode.

Key changes:
- Worktrees now created at $PWD/.kugetsu-worktrees/{issue-ref}/ instead
  of $WORKTREES_DIR/{issue-ref}/
- .kugetsu-worktrees is a hidden directory (git ignored by default)
- cmd_start and cmd_continue now use 'cd && opencode run' instead of
  'opencode run --dir'

This approach works because:
1. Worktree is inside parent's directory tree (permission granted)
2. cd properly changes working directory before opencode runs
3. Session gets created with correct directory set
4. No .gitignore entry needed (. prefix makes it hidden from git)
han approved these changes 2026-04-02 10:27:15 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit 90f46a778a into main 2026-04-02 10:29:33 +02:00
Sign in to join this conversation.