fix(kugetsu): capture forked session ID from opencode output

The --fork flag outputs the new session ID. Parse that instead of
relying on session list which may return wrong session when multiple
exist. Added fallback to session list parsing.
This commit is contained in:
shokollm
2026-03-29 20:16:51 +00:00
parent 7f3952ff9d
commit c51a886aa6

View File

@@ -250,14 +250,23 @@ cmd_start() {
local session_file="$(issue_ref_to_filename "$issue_ref").json"
echo "Forking session for '$issue_ref'..."
local fork_output
if [ "$DEBUG_MODE" = true ]; then
opencode run --fork --session "$base_session_id" "$message" 2>&1 | tee "$SESSIONS_DIR/$session_file.debug.log"
fork_output=$(opencode run --fork --session "$base_session_id" "$message" 2>&1 | tee "$SESSIONS_DIR/$session_file.debug.log")
else
opencode run --fork --session "$base_session_id" "$message"
fork_output=$(opencode run --fork --session "$base_session_id" "$message" 2>&1)
fi
local new_session_ids=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' | tail -1)
local new_session_id=$(echo "$new_session_ids" | tail -1)
local new_session_id=$(echo "$fork_output" | grep -oP 'A new forked session was created: \Kses_\w+' | tail -1)
if [ -z "$new_session_id" ]; then
new_session_id=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' | grep -v "^$base_session_id$" | tail -1)
fi
if [ -z "$new_session_id" ]; then
echo "Error: Could not find newly created session" >&2
exit 1
fi
printf '{"type": "forked", "issue_ref": "%s", "opencode_session_id": "%s", "created_at": "%s", "state": "idle"}\n' \
"$issue_ref" "$new_session_id" "$(date -Iseconds)" > "$SESSIONS_DIR/$session_file"