fix: detect task completion by checking if session ended and has commits

This commit is contained in:
shokollm
2026-04-05 12:45:59 +00:00
parent 9bb8afe8c5
commit ab0c4e1448

View File

@@ -8,8 +8,52 @@ source "$SCRIPT_DIR/kugetsu-index.sh"
source "$SCRIPT_DIR/kugetsu-worktree.sh" source "$SCRIPT_DIR/kugetsu-worktree.sh"
source "$SCRIPT_DIR/kugetsu-log.sh" source "$SCRIPT_DIR/kugetsu-log.sh"
# Check if a notified task has completed (session ended or has new commits)
check_task_completion() {
local item="$1"
local queue_id=$(basename "$item" .json)
local state=$(python3 -c "import json; print(json.load(open('$item')).get('state', ''))" 2>/dev/null)
[ "$state" = "notified" ] || return 0
local session_id=$(python3 -c "import json; print(json.load(open('$item')).get('session_id', ''))" 2>/dev/null)
local issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null)
# If no session tracked, skip
[ -n "$session_id" ] || return 0
# Check if session still exists in opencode
if ! opencode session list 2>/dev/null | grep -q "$session_id"; then
# Session ended — check if work was done
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees")
local has_commits=false
if [ -d "$worktree_path" ] && [ -d "$worktree_path/.git" ]; then
# Check if worktree has new commits beyond origin/main
if [ -n "$(git -C "$worktree_path" log --oneline origin/main..HEAD 2>/dev/null)" ]; then
has_commits=true
fi
fi
if [ "$has_commits" = true ]; then
update_queue_item_state "$queue_id" "completed"
echo "Task $queue_id ($issue_ref) completed — new commits found"
else
update_queue_item_state "$queue_id" "error"
echo "Task $queue_id ($issue_ref) marked error — no commits found after session ended"
fi
fi
}
while true; do while true; do
# Check completion of notified tasks
if [ -d "$QUEUE_ITEMS_DIR" ]; then if [ -d "$QUEUE_ITEMS_DIR" ]; then
for item in "$QUEUE_ITEMS_DIR"/*.json; do
[ -f "$item" ] || continue
check_task_completion "$item"
done
# Process pending tasks
for item in "$QUEUE_ITEMS_DIR"/*.json; do for item in "$QUEUE_ITEMS_DIR"/*.json; do
[ -f "$item" ] || continue [ -f "$item" ] || continue
state=$(python3 -c "import json; print(json.load(open('$item')).get('state', ''))" 2>/dev/null) state=$(python3 -c "import json; print(json.load(open('$item')).get('state', ''))" 2>/dev/null)
@@ -21,7 +65,7 @@ while true; do
pm_session=$(get_pm_agent_session_id) pm_session=$(get_pm_agent_session_id)
if [ -n "$pm_session" ] && [ "$pm_session" != "null" ]; then if [ -n "$pm_session" ] && [ "$pm_session" != "null" ]; then
log_file="$LOGS_DIR/delegate-$(date +%s).log" log_file="$LOGS_DIR/delegate-$(date +%s).log"
GITEA_TOKEN="${GITEA_TOKEN:-}" nohup sh -c "opencode run '$message' --continue --session '$pm_session' >> '$log_file' 2>&1" > /dev/null 2>&1 & GITEA_TOKEN="***" nohup sh -c "opencode run '$message' --continue --session '$pm_session' >> '$log_file' 2>&1" > /dev/null 2>&1 &
pid=$! pid=$!
update_queue_item_state "$queue_id" "notified" "$pm_session" "$pid" update_queue_item_state "$queue_id" "notified" "$pm_session" "$pid"
fi fi