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 <noreply@anthropic.com>
This commit is contained in:
Daniel Samson
2026-04-12 14:42:18 +01:00
co-authored by Claude Opus 4.6
parent e801d96410
commit d220623a9f
+17 -13
View File
@@ -428,6 +428,17 @@ resource "coder_agent" "main" {
startup_script = <<-EOT startup_script = <<-EOT
#!/bin/bash #!/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 set -e
# --- Configure git --- # --- Configure git ---
@@ -483,25 +494,18 @@ resource "coder_agent" "main" {
# Read the slash command .md file, strip YAML frontmatter, substitute $ARGUMENTS # Read the slash command .md file, strip YAML frontmatter, substitute $ARGUMENTS
CMD_FILE=".claude/commands/$TASK_TYPE.md" CMD_FILE=".claude/commands/$TASK_TYPE.md"
if [ ! -f "$CMD_FILE" ]; then 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 exit 1
fi fi
PROMPT=$(sed '1{/^---$/!q}; 1,/^---$/d' "$CMD_FILE" | sed "s/\\\$ARGUMENTS/$ARGS/g") PROMPT=$(sed '1{/^---$/!q}; 1,/^---$/d' "$CMD_FILE" | sed "s/\\\$ARGUMENTS/$ARGS/g")
# Run Claude Code in non-interactive mode with tool access # Run Claude Code in non-interactive mode with tool access
claude -p --dangerously-skip-permissions --verbose "$PROMPT" 2>&1 | tee ~/task-output.log claude -p --dangerously-skip-permissions --verbose "$PROMPT"
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
fi fi
# Success — override the trap
trap - ERR EXIT
notify_complete "complete"
EOT EOT
} }