summaryrefslogtreecommitdiff
path: root/scripts/next-task
blob: eeeca2e3a362e25804e93f6d2ecfc8ed35640cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# next-task — print the ID of the next PENDING task to run, or nothing if none.
#
# Selection logic:
#   1. Find the most recently updated COMPLETED or READY task.
#   2. If COMPLETED, prefer its next PENDING sibling (same parent) to continue a series.
#   3. If no sibling exists (series done, or READY state), fall back to the
#      highest-priority oldest PENDING task across the whole queue.
#
# Usage: next_id=$(scripts/next-task)
# Example: scripts/start-next-task

DB_PATH="/site/doot.terst.org/data/claudomator.db"

# 1. Fetch the most recently updated COMPLETED or READY 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;")
fi

if [ -z "$next_task" ]; then
    # 3. No sibling found (or READY state): find next task 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"