From d220623a9f2b8a493c697ba9ec8266de3770db90 Mon Sep 17 00:00:00 2001 From: Daniel Samson <12231216+daniel-samson@users.noreply.github.com> Date: Sun, 12 Apr 2026 14:42:18 +0100 Subject: [PATCH] Always fire callback via trap, even on script failure Uses ERR/EXIT trap to ensure the orchestrator is notified when the startup script fails (e.g. clone error, missing command file). Prevents orphaned workspaces that never get cleaned up. Co-Authored-By: Claude Opus 4.6 --- coder/cloudflare-worker/main.tf | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) 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 }