fix(kugetsu): create session file before opencode fork

Create placeholder session file and add to index BEFORE running
opencode. This ensures we have a record even if opencode takes
long time or times out. Update with real session ID after fork.
This commit is contained in:
shokollm
2026-03-29 20:19:38 +00:00
parent c51a886aa6
commit 636a41f41b

View File

@@ -249,6 +249,11 @@ cmd_start() {
local session_file="$(issue_ref_to_filename "$issue_ref").json"
printf '{"type": "forked", "issue_ref": "%s", "opencode_session_id": "pending", "created_at": "%s", "state": "starting"}\n' \
"$issue_ref" "$(date -Iseconds)" > "$SESSIONS_DIR/$session_file"
add_issue_to_index "$issue_ref" "$session_file"
echo "Forking session for '$issue_ref'..."
local fork_output
if [ "$DEBUG_MODE" = true ]; then
@@ -260,19 +265,19 @@ cmd_start() {
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)
new_session_id=$(opencode session list 2>/dev/null | grep -E '^ses_' | awk '{print $1}' | grep -v "^$base_session_id$" | head -1)
fi
if [ -z "$new_session_id" ]; then
echo "Error: Could not find newly created session" >&2
printf '{"type": "forked", "issue_ref": "%s", "opencode_session_id": "failed", "created_at": "%s", "state": "failed"}\n' \
"$issue_ref" "$(date -Iseconds)" > "$SESSIONS_DIR/$session_file"
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"
add_issue_to_index "$issue_ref" "$session_file"
echo "Session started for '$issue_ref': $new_session_id"
}