Skip to content

[plan] Add panic recovery mechanisms for critical operations #9241

@github-actions

Description

@github-actions

Objective

Add panic recovery mechanisms with proper error conversion for critical operations in workflow compilation, MCP server initialization, and CLI orchestration.

Context

Currently 0 defer recover() implementations exist, leaving crashes unhandled. Adding panic recovery to critical entry points improves stability and provides graceful error messages instead of crashes.

Files to Modify

Critical operation entry points:

  • pkg/workflow/compiler_orchestrator.go - CompileWorkflow()
  • pkg/workflow/mcp-config.go - GenerateMCPConfig()
  • pkg/cli/compile_command.go - RunCompile()
  • pkg/cli/run_command.go - RunWorkflow()

Approach

Add panic recovery to critical entry points:

import (
    "runtime/debug"
    "github.com/githubnext/gh-aw/pkg/logger"
)

var log = logger.New("workflow:compiler")

func CriticalOperation() (err error) {
    defer func() {
        if r := recover(); r != nil {
            log.Printf("Panic recovered: %v\nStack: %s", r, debug.Stack())
            err = fmt.Errorf("internal error during compilation: %v. This is a bug - please report it at github.com/githubnext/gh-aw/issues", r)
        }
    }()
    
    // Critical operation that might panic
    return doWork()
}

Key principles:

  1. Log panic details with stack traces for debugging
  2. Convert panics to user-friendly error messages
  3. Never silently swallow panics (always log)
  4. Include "this is a bug" messaging to encourage reporting
  5. Named return value (err error) to enable error assignment in defer

Acceptance Criteria

  • Panic recovery added to 4 critical entry points
  • Recovered panics converted to proper errors with context
  • Debug logging captures panic details with stack traces
  • Users see graceful error messages instead of crashes
  • Tests verify panic recovery behavior
  • Error messages include link to GitHub issues

Testing

Add tests for panic recovery:

func TestCompileWorkflow_PanicRecovery(t *testing.T) {
    // Test that panics are recovered and converted to errors
    // Mock a function that panics during compilation
}

Run validation:

make test
# Verify panic recovery added
grep -r "defer.*recover" pkg/workflow/ pkg/cli/ | wc -l

Related to #9236

AI generated by Plan Command for discussion #9231

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions