#!/bin/bash DB_PATH="/site/doot.terst.org/data/claudomator.db" # 1. Fetch the most recently updated task target=$(sqlite3 "$DB_PATH" "SELECT id, state, parent_task_id FROM tasks WHERE state IN ('COMPLETED', 'READY') ORDER BY updated_at DESC LIMIT 1;") if [ -z "$target" ]; then exit 0 fi IFS='|' read -r id state parent_id <<< "$target" if [ "$state" == "COMPLETED" ]; then # 2. Find next sibling task that is currently PENDING next_task=$(sqlite3 "$DB_PATH" "SELECT id FROM tasks WHERE parent_task_id = '$parent_id' AND state = 'PENDING' LIMIT 1;") elif [ "$state" == "READY" ]; then # 3. Find next task ordered by priority then oldest next_task=$(sqlite3 "$DB_PATH" "SELECT id FROM tasks WHERE state = 'PENDING' AND id != '$id' ORDER BY CASE priority WHEN 'critical' THEN 4 WHEN 'high' THEN 3 WHEN 'normal' THEN 2 WHEN 'low' THEN 1 ELSE 0 END DESC, created_at ASC LIMIT 1;") fi echo "$next_task"