refactor(kugetsu): daemon uses cmd_start/cmd_continue instead of direct opencode calls

- Move issue_ref_to_filename and filename_to_issue_ref to kugetsu-index.sh
  (where they logically belong, instead of in main kugetsu script)
- Refactor queue daemon to use cmd_start/cmd_continue for session management
- Daemon now checks if worktree/session exists → cmd_continue, else → cmd_start
- Removes ~40 lines of direct opencode session forking logic from daemon
- cmd_start/cmd_continue handle worktree creation, session forking, and tracking

This simplifies the daemon significantly and centralizes session management
in kugetsu-session.sh where it belongs.
This commit is contained in:
shokollm
2026-04-05 21:29:34 +00:00
parent 7fa669b4c3
commit cbfc8a0646
3 changed files with 29 additions and 53 deletions

View File

@@ -99,17 +99,6 @@ ensure_worktree_dir() {
mkdir -p "$WORKTREES_DIR" mkdir -p "$WORKTREES_DIR"
} }
issue_ref_to_filename() {
local issue_ref="$1"
echo "$issue_ref" | sed 's/[\/:]/-/g' | sed 's/#/-/'
}
filename_to_issue_ref() {
local filename="$1"
local name="${filename%.json}"
echo "$name" | sed 's/-\([0-9]*\)$/#\1/' | sed 's/-/\//g'
}
issue_ref_to_context_file() { issue_ref_to_context_file() {
local issue_ref="$1" local issue_ref="$1"
local context_filename=$(issue_ref_to_filename "$issue_ref") local context_filename=$(issue_ref_to_filename "$issue_ref")

View File

@@ -133,3 +133,16 @@ with open(session_path, 'w') as f:
print(f"Updated PR URL for $issue_ref: $pr_url") print(f"Updated PR URL for $issue_ref: $pr_url")
PYEOF PYEOF
} }
# Convert issue ref to session filename
issue_ref_to_filename() {
local issue_ref="$1"
echo "$issue_ref" | sed 's/[\/:]/-/g' | sed 's/#/-/'
}
# Convert session filename back to issue ref
filename_to_issue_ref() {
local filename="$1"
local name="${filename%.json}"
echo "$name" | sed 's-\([0-9]*\)$-#\1-' | sed 's/-/\//g'
}

View File

@@ -68,49 +68,23 @@ while true; do
issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null) issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null)
message=$(python3 -c "import json; print(json.load(open('$item')).get('message', ''))" 2>/dev/null) message=$(python3 -c "import json; print(json.load(open('$item')).get('message', ''))" 2>/dev/null)
pm_session=$(get_pm_agent_session_id) # Source session management and use cmd_start/cmd_continue
if [ -n "$pm_session" ] && [ "$pm_session" != "null" ]; then source "$SCRIPT_DIR/kugetsu-session.sh"
# Compute worktree path for this issue
worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees")
# Ensure worktree exists, create if needed
if [ ! -d "$worktree_path" ]; then
echo "Creating worktree for $issue_ref at $worktree_path..."
create_worktree "$issue_ref" "$HOME/.kugetsu-worktrees"
fi
# Capture sessions before fork to identify the new forked session
sessions_before=$(opencode session list 2>/dev/null || echo "")
if worktree_exists "$issue_ref" "$HOME/.kugetsu-worktrees" || [ -f "$SESSIONS_DIR/$(issue_ref_to_filename "$issue_ref").json" ]; then
# Continue existing session
log_file="$LOGS_DIR/delegate-$(date +%s).log" log_file="$LOGS_DIR/delegate-$(date +%s).log"
# Use --fork --dir to run in the correct worktree directory cmd_continue "$issue_ref" "$message" >> "$log_file" 2>&1 &
# The forked session will end when the task completes, while parent pm_session continues
(cd "$worktree_path" && nohup bash -c "export GITEA_TOKEN=$GITEA_TOKEN; opencode run '$message' --fork --session '$pm_session' --dir '$worktree_path'" >> "$log_file" 2>&1) &
pid=$! pid=$!
# Wait for fork to initialize and capture new session
sleep 3
# Find the forked session ID by comparing session lists
sessions_after=$(opencode session list 2>/dev/null || echo "")
forked_session=""
while IFS= read -r line; do
session=$(echo "$line" | awk '{print $1}' | tr -d '[:space:]')
if [ -n "$session" ] && ! echo "$sessions_before" | grep -q "$session"; then
forked_session="$session"
break
fi
done <<< "$sessions_after"
# Store the forked session ID (not the parent pm_session) for completion detection
if [ -n "$forked_session" ]; then
update_queue_item_state "$queue_id" "notified" "$forked_session" "$pid"
echo "Task $queue_id notified with forked session $forked_session for $issue_ref"
else
# Fallback: still update with empty session so task isn't stuck
update_queue_item_state "$queue_id" "notified" "" "$pid" update_queue_item_state "$queue_id" "notified" "" "$pid"
echo "Task $queue_id notified but could not capture forked session for $issue_ref" echo "Task $queue_id continued for $issue_ref"
fi else
# Start new session
log_file="$LOGS_DIR/delegate-$(date +%s).log"
cmd_start "$issue_ref" "$message" >> "$log_file" 2>&1 &
pid=$!
update_queue_item_state "$queue_id" "notified" "" "$pid"
echo "Task $queue_id started for $issue_ref"
fi fi
fi fi
done done