fix: init script captures wrong session IDs when old sessions exist

The init script used 'tail -1' to find newly created sessions, which
fails when old sessions exist because it picks an existing session
instead of the newly created one.

The fix captures session IDs before and after creating new sessions,
then diffs to identify newly created sessions.

Fixes #172
This commit is contained in:
shokollm
2026-04-06 01:08:09 +00:00
parent 91b51f62c0
commit 85a4239383

View File

@@ -81,9 +81,20 @@ EOF
echo "Press Ctrl+C to cancel or wait for session to be created" echo "Press Ctrl+C to cancel or wait for session to be created"
sleep 2 sleep 2
local before_sessions=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' || true)
opencode opencode
local session_ids=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' | tail -1) local after_sessions=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' || true)
local session_ids=""
while IFS= read -r line; do
local sid=$(echo "$line" | awk '{print $1}')
if [ -n "$sid" ] && ! echo "$before_sessions" | grep -q "^${sid}$"; then
session_ids="$sid"
break
fi
done <<< "$after_sessions"
if [ -z "$session_ids" ]; then if [ -z "$session_ids" ]; then
echo "Error: Could not find newly created session" >&2 echo "Error: Could not find newly created session" >&2
exit 1 exit 1
@@ -95,9 +106,20 @@ EOF
echo "Base session created: $session_ids" echo "Base session created: $session_ids"
echo "Starting PM agent..." echo "Starting PM agent..."
before_sessions="$after_sessions"
opencode opencode
local pm_session_ids=$(opencode session list 2>/dev/null | grep -E '^ses_' | grep -v "$session_ids" | tail -1) after_sessions=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' || true)
local pm_session_ids=""
while IFS= read -r line; do
local sid=$(echo "$line" | awk '{print $1}')
if [ -n "$sid" ] && ! echo "$before_sessions" | grep -q "^${sid}$"; then
pm_session_ids="$sid"
break
fi
done <<< "$after_sessions"
if [ -z "$pm_session_ids" ]; then if [ -z "$pm_session_ids" ]; then
echo "Warning: Could not find separate PM agent session" >&2 echo "Warning: Could not find separate PM agent session" >&2
pm_session_ids="$session_ids" pm_session_ids="$session_ids"