opencode run --fork --dir does not set session working directory correctly #127

Closed
opened 2026-04-03 14:16:57 +02:00 by shoko · 0 comments
Owner

Problem

When forking a session with opencode run --fork --session <base> --dir <worktree>, the --dir flag sets the directory for the initial command only. The forked session's directory column in opencode's SQLite DB inherits from the parent session's shell working directory, not from --dir.

Impact

All agents forked via kugetsu start or kugetsu continue run from the base session's directory (e.g. ~/repositories/project or /tmp), completely ignoring the intended worktree. This makes isolated per-issue sessions unusable.

Root Cause

The --dir flag in opencode run only affects the subprocess's working directory for that specific command — it does not update the session's stored directory value in opencode's SQLite DB.

Findings

v0.1.10 partial fix

cmd_start was updated to wrap the command in a subshell with explicit cd:

(cd "$worktree_path" && opencode run "$full_message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1) | tee "$fork_log" &

This ensures the session is created with the correct working directory. Verified working in v0.1.10 + re-init.

cmd_continue still affected

cmd_continue uses the same --dir pattern but WITHOUT the subshell cd:

opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 &

The --dir flag does not update the session's persistent directory on --continue.

Apply the same subshell pattern to cmd_continue:

if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
    (cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1) &
else
    opencode run "$message" --continue --session "$opencode_session_id" 2>&1 &
fi

Workaround (for users)

Session existence check to avoid duplicate sessions and use correct command:

#!/bin/bash
issue_ref="$1"; shift; message="$@"
session_key=$(echo "$issue_ref" | tr '/#' '-')
session_file="$HOME/.kugetsu/sessions/${session_key}.json"

if [ -f "$session_file" ]; then
    kugetsu continue "$issue_ref" "$message"
else
    kugetsu start "$issue_ref" "$message"
fi
## Problem When forking a session with `opencode run --fork --session <base> --dir <worktree>`, the `--dir` flag sets the directory for the initial command only. The forked session's `directory` column in opencode's SQLite DB inherits from the parent session's shell working directory, not from `--dir`. ## Impact All agents forked via `kugetsu start` or `kugetsu continue` run from the base session's directory (e.g. `~/repositories/project` or `/tmp`), completely ignoring the intended worktree. This makes isolated per-issue sessions unusable. ## Root Cause The `--dir` flag in `opencode run` only affects the subprocess's working directory for that specific command — it does not update the session's stored `directory` value in opencode's SQLite DB. ## Findings ### v0.1.10 partial fix `cmd_start` was updated to wrap the command in a subshell with explicit `cd`: ```bash (cd "$worktree_path" && opencode run "$full_message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1) | tee "$fork_log" & ``` This ensures the session is created with the correct working directory. Verified working in v0.1.10 + re-init. ### `cmd_continue` still affected `cmd_continue` uses the same `--dir` pattern but WITHOUT the subshell `cd`: ```bash opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1 & ``` The `--dir` flag does not update the session's persistent directory on `--continue`. ## Recommended Fix Apply the same subshell pattern to `cmd_continue`: ```bash if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then (cd "$worktree_path" && opencode run "$message" --continue --session "$opencode_session_id" --dir "$worktree_path" 2>&1) & else opencode run "$message" --continue --session "$opencode_session_id" 2>&1 & fi ``` ## Workaround (for users) Session existence check to avoid duplicate sessions and use correct command: ```bash #!/bin/bash issue_ref="$1"; shift; message="$@" session_key=$(echo "$issue_ref" | tr '/#' '-') session_file="$HOME/.kugetsu/sessions/${session_key}.json" if [ -f "$session_file" ]; then kugetsu continue "$issue_ref" "$message" else kugetsu start "$issue_ref" "$message" fi ```
shoko closed this issue 2026-04-03 15:58:20 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shoko/kugetsu#127