summaryrefslogtreecommitdiff
path: root/scripts/debug-execution
blob: b4873b9490e931c342e3b147d77aff4c50f4d906 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
# debug-execution: Show details for a failed task execution from the production DB.
# Usage: ./scripts/debug-execution [execution-id-or-prefix]
# Example: ./scripts/debug-execution c74c877f
# If no ID is given, defaults to the most recent execution.

set -euo pipefail

DB="/site/doot.terst.org/data/claudomator.db"
DATA_DIR="/site/doot.terst.org/data"
PREFIX="${1:-}"

if [[ ! -f "$DB" ]]; then
  echo "ERROR: DB not found at $DB" >&2
  exit 1
fi

# Look up execution
if [[ -z "$PREFIX" ]]; then
  ROW=$(sqlite3 "$DB" "
    SELECT id, task_id, exit_code, status, stdout_path, stderr_path, error_msg
    FROM executions
    ORDER BY start_time DESC
    LIMIT 1;
  ")
else
  ROW=$(sqlite3 "$DB" "
    SELECT id, task_id, exit_code, status, stdout_path, stderr_path, error_msg
    FROM executions
    WHERE id LIKE '${PREFIX}%'
    ORDER BY start_time DESC
    LIMIT 1;
  ")
fi

if [[ -z "$ROW" ]]; then
  if [[ -z "$PREFIX" ]]; then
    echo "ERROR: No executions found in DB" >&2
  else
    echo "ERROR: No execution found matching '${PREFIX}'" >&2
  fi
  exit 1
fi

IFS='|' read -r EXEC_ID TASK_ID EXIT_CODE STATUS STDOUT_PATH STDERR_PATH ERROR_MSG <<< "$ROW"

echo "=== Execution ==="
echo "  ID:        $EXEC_ID"
echo "  Status:    $STATUS"
echo "  Exit code: $EXIT_CODE"
echo "  Error:     ${ERROR_MSG:-(none)}"

# Look up task
TASK_ROW=$(sqlite3 "$DB" "
  SELECT name, json_extract(config_json, '$.working_dir'), json_extract(config_json, '$.model')
  FROM tasks
  WHERE id = '${TASK_ID}';
")

if [[ -n "$TASK_ROW" ]]; then
  IFS='|' read -r TASK_NAME WORKING_DIR MODEL <<< "$TASK_ROW"
  echo ""
  echo "=== Task ==="
  echo "  ID:          $TASK_ID"
  echo "  Name:        $TASK_NAME"
  echo "  Model:       ${MODEL:-(not set)}"
  echo "  Working dir: ${WORKING_DIR:-(not set)}"
fi

# Resolve log paths: use DB value if set, otherwise derive from known convention.
EXEC_LOG_DIR="${DATA_DIR}/executions/${EXEC_ID}"
: "${STDOUT_PATH:=${EXEC_LOG_DIR}/stdout.log}"
: "${STDERR_PATH:=${EXEC_LOG_DIR}/stderr.log}"

# Logs
print_log() {
  local label="$1" path="$2"
  echo ""
  echo "=== $label ==="
  if [[ ! -f "$path" ]]; then
    echo "  (file not found: $path)"
  elif [[ ! -s "$path" ]]; then
    echo "  (empty)"
  else
    tail -n 50 "$path"
  fi
}

print_log "stdout (last 50 lines)" "$STDOUT_PATH"
print_log "stderr (last 50 lines)" "$STDERR_PATH"