fix: use array-based session detection for robust concurrent fork handling

Replace string-based session comparison with array-based approach:

- Store before_sessions in an array instead of pipe-delimited string
- This is more robust against word-splitting issues in bash
- Skip base_session_id and pm_agent_session_id explicitly
- Compare each after-session against the before array

This approach is more reliable when multiple agents fork concurrently
because it properly compares each session ID individually rather than
relying on regex matching in a string.

Fixes #81
This commit is contained in:
shokollm
2026-04-01 23:43:16 +00:00
parent ae99f86f9d
commit 3df99d571f

View File

@@ -829,8 +829,11 @@ cmd_start() {
local session_file="$(issue_ref_to_filename "$issue_ref").json"
local before_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
local before_set="${before_sessions//$'\n'/|}"
# Get list of sessions before fork to compare against after
declare -a before_sessions=()
while IFS= read -r sess; do
before_sessions+=("$sess")
done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+')
echo "Forking session for '$issue_ref'..."
@@ -849,14 +852,31 @@ cmd_start() {
opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 &
fi
local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
# Wait briefly for session to be created
sleep 1
# Find the new session by comparing before/after lists
# Skip any session that existed before the fork and skip base/pm-agent
local new_session_id=""
while IFS= read -r sess; do
if [[ ! "$before_set" =~ \|${sess}\| ]] && [[ "$sess" != "$base_session_id" ]] && [[ "$sess" != "$pm_agent_session_id" ]]; then
# Skip base and pm-agent
[ "$sess" = "$base_session_id" ] && continue
[ "$sess" = "$pm_agent_session_id" ] && continue
# Check if this session existed before
local existed_before=false
for before_sess in "${before_sessions[@]}"; do
if [ "$sess" = "$before_sess" ]; then
existed_before=true
break
fi
done
if [ "$existed_before" = false ]; then
new_session_id="$sess"
break
fi
done <<< "$after_sessions"
done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+')
if [ -z "$new_session_id" ]; then
echo "Error: Could not find newly created session" >&2