diff --git a/coder/cloudflare-worker/main.tf b/coder/cloudflare-worker/main.tf index c1c892a..9e1a90b 100644 --- a/coder/cloudflare-worker/main.tf +++ b/coder/cloudflare-worker/main.tf @@ -428,6 +428,17 @@ resource "coder_agent" "main" { startup_script = <<-EOT #!/bin/bash + + # Always notify the orchestrator when done, even on failure + notify_complete() { + if [ -n "$CALLBACK_URL" ]; then + curl -s -X POST -H "Content-Type: application/json" \ + -d "{\"status\":\"$1\"}" \ + "$CALLBACK_URL" || echo "Callback failed (non-fatal)" + fi + } + trap 'notify_complete "failed"' ERR EXIT + set -e # --- Configure git --- @@ -483,25 +494,18 @@ resource "coder_agent" "main" { # Read the slash command .md file, strip YAML frontmatter, substitute $ARGUMENTS CMD_FILE=".claude/commands/$TASK_TYPE.md" if [ ! -f "$CMD_FILE" ]; then - echo "ERROR: Command file not found: $CMD_FILE" | tee ~/task-output.log + echo "ERROR: Command file not found: $CMD_FILE" exit 1 fi PROMPT=$(sed '1{/^---$/!q}; 1,/^---$/d' "$CMD_FILE" | sed "s/\\\$ARGUMENTS/$ARGS/g") # Run Claude Code in non-interactive mode with tool access - claude -p --dangerously-skip-permissions --verbose "$PROMPT" 2>&1 | tee ~/task-output.log - EXIT_CODE=$? - echo "Claude exited with code: $EXIT_CODE" | tee -a ~/task-output.log - - echo "Task completed. Output saved to ~/task-output.log" - - # Notify orchestrator that the task is done - if [ -n "$CALLBACK_URL" ]; then - curl -s -X POST -H "Content-Type: application/json" \ - -d '{"status":"complete"}' \ - "$CALLBACK_URL" || echo "Callback failed (non-fatal)" - fi + claude -p --dangerously-skip-permissions --verbose "$PROMPT" fi + + # Success — override the trap + trap - ERR EXIT + notify_complete "complete" EOT }