fix: recover failed task launches safely (#3437)

This commit is contained in:
Carlos Florêncio
2026-09-07 11:19:09 +01:00
committed by GitHub
parent 3c6af41063
commit 3fba828e2f
69 changed files with 2280 additions and 277 deletions
@@ -20,6 +20,7 @@ const (
LaunchErrorCategoryBaseBranchMissing = "base_branch_missing"
LaunchErrorCategoryPRAlreadyClosed = "pr_already_closed"
LaunchErrorCategoryDefaultBranchUnresolved = "default_branch_unresolved"
LaunchErrorCategoryWorkspaceCheckoutFailed = "workspace_checkout_failed"
LaunchErrorCategoryGenericLaunchFailure = "generic_launch_failure"
)
@@ -28,6 +29,7 @@ const (
RecoveryActionRetryDefault = "retry_default"
RecoveryActionPickBaseBranch = "pick_base_branch"
RecoveryActionMarkReviewDone = "mark_review_done"
RecoveryActionRetryLaunch = "retry_launch"
)
const (
@@ -74,9 +76,42 @@ func NormalizeRecoveryActions(actions []string) []string {
return result
}
// NormalizeRecoveryActionsForCategory retains only the actions that are
// meaningful for a typed launch-failure category. Unknown categories keep the
// legacy generic normalization so unrelated runtime errors remain compatible.
func NormalizeRecoveryActionsForCategory(category string, actions []string) []string {
normalized := NormalizeRecoveryActions(actions)
var allowed map[string]struct{}
switch category {
case LaunchErrorCategoryBaseBranchMissing:
allowed = map[string]struct{}{
RecoveryActionRetryDefault: {}, RecoveryActionPickBaseBranch: {},
}
case LaunchErrorCategoryDefaultBranchUnresolved:
allowed = map[string]struct{}{RecoveryActionPickBaseBranch: {}}
case LaunchErrorCategoryWorkspaceCheckoutFailed, LaunchErrorCategoryGenericLaunchFailure:
if len(normalized) == 0 {
return nil
}
return []string{RecoveryActionRetryLaunch}
case LaunchErrorCategoryPRAlreadyClosed:
allowed = map[string]struct{}{RecoveryActionMarkReviewDone: {}}
default:
return normalized
}
result := make([]string, 0, len(normalized))
for _, action := range normalized {
if _, ok := allowed[action]; ok {
result = append(result, action)
}
}
return result
}
func isKnownRecoveryAction(action string) bool {
switch action {
case RecoveryActionRetryDefault, RecoveryActionPickBaseBranch, RecoveryActionMarkReviewDone:
case RecoveryActionRetryDefault, RecoveryActionPickBaseBranch, RecoveryActionMarkReviewDone, RecoveryActionRetryLaunch:
return true
default:
return false
@@ -85,7 +120,8 @@ func isKnownRecoveryAction(action string) bool {
func normalizeLastAgentError(value LastAgentError) LastAgentError {
value.Message = truncateUTF8Bytes(value.Message, maxLaunchErrorMessageBytes)
value.RecoveryActions = NormalizeRecoveryActions(value.RecoveryActions)
value.Code = truncateUTF8Bytes(value.Code, maxLaunchErrorCategoryBytes)
value.RecoveryActions = NormalizeRecoveryActionsForCategory(value.Code, value.RecoveryActions)
value.TaskRepositoryID = truncateUTF8Bytes(value.TaskRepositoryID, maxTaskRepositoryIDBytes)
value.StampValue = boundedLaunchErrorStamp(value.StampValue)
value.Details = truncateUTF8Bytes(value.Details, maxLaunchErrorDetailsBytes)
@@ -94,10 +130,10 @@ func normalizeLastAgentError(value LastAgentError) LastAgentError {
func normalizeTaskLaunchError(value TaskLaunchError) TaskLaunchError {
value.Message = truncateUTF8Bytes(value.Message, maxLaunchErrorMessageBytes)
value.RecoveryActions = NormalizeRecoveryActions(value.RecoveryActions)
value.Code = truncateUTF8Bytes(value.Code, maxLaunchErrorCategoryBytes)
value.RecoveryActions = NormalizeRecoveryActionsForCategory(value.Code, value.RecoveryActions)
value.TaskRepositoryID = truncateUTF8Bytes(value.TaskRepositoryID, maxTaskRepositoryIDBytes)
value.StampValue = boundedLaunchErrorStamp(value.StampValue)
value.Code = truncateUTF8Bytes(value.Code, maxLaunchErrorCategoryBytes)
value.Details = truncateUTF8Bytes(value.Details, maxLaunchErrorDetailsBytes)
return value
}
@@ -30,6 +30,46 @@ func TestLoadLastAgentErrorUsesExplicitStampAndNormalizesRecoveryFields(t *testi
}, lastError.RecoveryActions)
}
func TestNormalizeRecoveryActionsForCategoryFiltersActionsByLaunchCause(t *testing.T) {
tests := []struct {
name string
category string
input []string
want []string
}{
{
name: "generic launch failure",
category: LaunchErrorCategoryGenericLaunchFailure,
input: []string{RecoveryActionRetryDefault, RecoveryActionPickBaseBranch, RecoveryActionMarkReviewDone},
want: []string{RecoveryActionRetryLaunch},
},
{
name: "workspace checkout failure",
category: LaunchErrorCategoryWorkspaceCheckoutFailed,
input: []string{RecoveryActionRetryDefault, RecoveryActionRetryLaunch},
want: []string{RecoveryActionRetryLaunch},
},
{
name: "missing base branch",
category: LaunchErrorCategoryBaseBranchMissing,
input: []string{RecoveryActionRetryLaunch, RecoveryActionRetryDefault, RecoveryActionPickBaseBranch},
want: []string{RecoveryActionRetryDefault, RecoveryActionPickBaseBranch},
},
{
name: "closed pull request",
category: LaunchErrorCategoryPRAlreadyClosed,
input: []string{RecoveryActionRetryLaunch, RecoveryActionMarkReviewDone},
want: []string{RecoveryActionMarkReviewDone},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.want, NormalizeRecoveryActionsForCategory(test.category, test.input))
})
}
}
func TestTaskLaunchErrorStoreIsNoOpForSameStamp(t *testing.T) {
firstTime := time.Date(2026, 8, 19, 10, 0, 0, 0, time.UTC)
secondTime := firstTime.Add(time.Minute)
@@ -23,6 +23,7 @@ const (
// MaxActiveErrorPreviewBytes keeps an error decoration safe to send with
// every task row without turning it into a message-stream transport.
MaxActiveErrorPreviewBytes = 512
MaxActiveErrorDetailsBytes = 4096
maxSessionIDBytes = 256
maxTaskRepositoryIDBytes = 256
maxPendingActionBytes = 128
@@ -64,6 +65,7 @@ type ActiveErrorSummary struct {
Stamp string `json:"stamp"`
OccurredAt time.Time `json:"occurred_at"`
Preview string `json:"preview"`
Details string `json:"details,omitempty"`
Category string `json:"category,omitempty"`
RecoveryActions []string `json:"recovery_actions,omitempty"`
}
@@ -156,6 +158,7 @@ func validateActiveError(activeError *ActiveErrorSummary) error {
{"active error task repository id", activeError.TaskRepositoryID, maxTaskRepositoryIDBytes},
{"active error stamp", activeError.Stamp, maxActiveErrorStampBytes},
{"active error preview", activeError.Preview, MaxActiveErrorPreviewBytes},
{"active error details", activeError.Details, MaxActiveErrorDetailsBytes},
{"active error category", activeError.Category, maxActiveErrorCategoryBytes},
}
for _, field := range fields {
@@ -166,7 +169,7 @@ func validateActiveError(activeError *ActiveErrorSummary) error {
if len(activeError.RecoveryActions) > 3 {
return fmt.Errorf("active error has more than three recovery actions")
}
if !slices.Equal(activeError.RecoveryActions, models.NormalizeRecoveryActions(activeError.RecoveryActions)) {
if !slices.Equal(activeError.RecoveryActions, models.NormalizeRecoveryActionsForCategory(activeError.Category, activeError.RecoveryActions)) {
return fmt.Errorf("active error has unknown or duplicate recovery actions")
}
return nil
@@ -171,7 +171,7 @@ func TestTaskStatusSummaryProjectsTaskOwnedErrorFields(t *testing.T) {
if got.ActiveError.Category != "pr_already_closed" {
t.Fatalf("active error category = %q", got.ActiveError.Category)
}
wantActions := []string{"mark_review_done", "retry_default"}
wantActions := []string{"mark_review_done"}
if !reflect.DeepEqual(got.ActiveError.RecoveryActions, wantActions) {
t.Fatalf("active error actions = %#v, want %#v", got.ActiveError.RecoveryActions, wantActions)
}
@@ -332,7 +332,7 @@ func applySummaryBaseline(state *projectionState, summary *TaskStatusSummary) {
}
if summary.ActiveError != nil {
copy := *summary.ActiveError
copy.RecoveryActions = normalizeRecoveryActions(copy.RecoveryActions)
copy.RecoveryActions = normalizeRecoveryActionsForCategory(copy.Category, copy.RecoveryActions)
if copy.SessionID != "" {
state.errors[copy.SessionID] = &copy
} else {
@@ -170,18 +170,21 @@ func errorFromMap(now time.Time, sessionID string, data map[string]interface{})
occurredAt = now.UTC()
}
preview := truncateString(message, MaxActiveErrorPreviewBytes)
details := truncateString(stringField(data, "details"), MaxActiveErrorDetailsBytes)
stamp := stringField(data, "stamp")
if stamp == "" {
stamp = occurredAt.UTC().Format(time.RFC3339Nano) + ":" + preview
}
category := truncateString(firstString(data, "category", "code"), maxActiveErrorCategoryBytes)
return &ActiveErrorSummary{
SessionID: truncateString(sessionID, maxSessionIDBytes),
TaskRepositoryID: truncateString(stringField(data, "task_repository_id"), maxTaskRepositoryIDBytes),
Stamp: truncateString(stamp, maxActiveErrorStampBytes),
OccurredAt: occurredAt.UTC(),
Preview: preview,
Category: truncateString(firstString(data, "category", "code"), maxActiveErrorCategoryBytes),
RecoveryActions: normalizeRecoveryActions(recoveryActionsValue(data["recovery_actions"])),
Details: details,
Category: category,
RecoveryActions: normalizeRecoveryActionsForCategory(category, recoveryActionsValue(data["recovery_actions"])),
}, true
}
@@ -191,7 +194,7 @@ func errorEqual(a, b *ActiveErrorSummary) bool {
}
return a.SessionID == b.SessionID && a.TaskRepositoryID == b.TaskRepositoryID &&
a.Stamp == b.Stamp && a.OccurredAt.Equal(b.OccurredAt) &&
a.Preview == b.Preview && a.Category == b.Category &&
a.Preview == b.Preview && a.Details == b.Details && a.Category == b.Category &&
slices.Equal(a.RecoveryActions, b.RecoveryActions)
}
@@ -199,6 +202,10 @@ func normalizeRecoveryActions(actions []string) []string {
return models.NormalizeRecoveryActions(actions)
}
func normalizeRecoveryActionsForCategory(category string, actions []string) []string {
return models.NormalizeRecoveryActionsForCategory(category, actions)
}
func recoveryActionsValue(value interface{}) []string {
switch actions := value.(type) {
case []string:
@@ -140,8 +140,9 @@ func normalizeRebuildError(input *ActiveErrorSummary, now time.Time) *ActiveErro
copy.Preview = truncateString(copy.Preview, MaxActiveErrorPreviewBytes)
copy.SessionID = truncateString(copy.SessionID, maxSessionIDBytes)
copy.TaskRepositoryID = truncateString(copy.TaskRepositoryID, maxTaskRepositoryIDBytes)
copy.Details = truncateString(copy.Details, MaxActiveErrorDetailsBytes)
copy.Category = truncateString(copy.Category, maxActiveErrorCategoryBytes)
copy.RecoveryActions = normalizeRecoveryActions(copy.RecoveryActions)
copy.RecoveryActions = normalizeRecoveryActionsForCategory(copy.Category, copy.RecoveryActions)
if copy.Stamp == "" {
copy.Stamp = copy.OccurredAt.UTC().Format(time.RFC3339Nano) + ":" + copy.Preview
}