fix: recover failed task launches safely (#3437)
This commit is contained in:
+13
-4
@@ -23,6 +23,15 @@ func TestClassifyLaunchFailureUsesTypedBaseBranchCategory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyLaunchFailureUsesWorkspaceCheckoutCategory(t *testing.T) {
|
||||
classification := classifyLaunchFailure(errors.Join(
|
||||
errors.New("PR ref could not be materialized"), worktree.ErrWorkspaceCheckoutFailed,
|
||||
))
|
||||
if classification.code != models.LaunchErrorCategoryWorkspaceCheckoutFailed {
|
||||
t.Fatalf("classification code = %q, want %q", classification.code, models.LaunchErrorCategoryWorkspaceCheckoutFailed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransitionLaunchFailurePersistsTypedErrorAndExactTaskRepository(t *testing.T) {
|
||||
repo := newMockRepository()
|
||||
repo.sessions["session-1"] = &models.TaskSession{
|
||||
@@ -58,21 +67,21 @@ func TestTransitionLaunchFailurePersistsTypedErrorAndExactTaskRepository(t *test
|
||||
}
|
||||
}
|
||||
|
||||
func TestLaunchFailureReviewActionRequiresSuccessfulEligibilityResolver(t *testing.T) {
|
||||
func TestGenericLaunchFailureAlwaysOffersRetryLaunch(t *testing.T) {
|
||||
exec := &Executor{}
|
||||
exec.launchFailureReviewEligibility = func(context.Context, string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
errorValue := exec.buildLastAgentError(context.Background(), "task-1", "", errors.New("start failed"))
|
||||
if len(errorValue.RecoveryActions) != 1 || errorValue.RecoveryActions[0] != models.RecoveryActionMarkReviewDone {
|
||||
t.Fatalf("eligible recovery actions = %#v, want mark_review_done", errorValue.RecoveryActions)
|
||||
if len(errorValue.RecoveryActions) != 1 || errorValue.RecoveryActions[0] != models.RecoveryActionRetryLaunch {
|
||||
t.Fatalf("eligible recovery actions = %#v, want retry_launch", errorValue.RecoveryActions)
|
||||
}
|
||||
|
||||
exec.launchFailureReviewEligibility = func(context.Context, string) (bool, error) {
|
||||
return false, errors.New("lookup failed")
|
||||
}
|
||||
errorValue = exec.buildLastAgentError(context.Background(), "task-1", "", errors.New("start failed"))
|
||||
if len(errorValue.RecoveryActions) != 0 {
|
||||
if len(errorValue.RecoveryActions) != 1 || errorValue.RecoveryActions[0] != models.RecoveryActionRetryLaunch {
|
||||
t.Fatalf("failed eligibility lookup exposed recovery actions = %#v", errorValue.RecoveryActions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ type launchFailureClassification struct {
|
||||
|
||||
func classifyLaunchFailure(err error) launchFailureClassification {
|
||||
switch {
|
||||
case errors.Is(err, worktree.ErrWorkspaceCheckoutFailed):
|
||||
return launchFailureClassification{
|
||||
code: models.LaunchErrorCategoryWorkspaceCheckoutFailed,
|
||||
message: "The workspace could not be prepared for this launch.",
|
||||
}
|
||||
case errors.Is(err, worktree.ErrInvalidBaseBranch):
|
||||
return launchFailureClassification{
|
||||
code: models.LaunchErrorCategoryBaseBranchMissing,
|
||||
@@ -38,20 +43,27 @@ func classifyLaunchFailure(err error) launchFailureClassification {
|
||||
}
|
||||
}
|
||||
|
||||
func launchFailureRecoveryActions(taskRepositoryID string, markReviewDone bool) []string {
|
||||
func launchFailureRecoveryActions(category, taskRepositoryID string, markReviewDone bool) []string {
|
||||
actions := make([]string, 0, 3)
|
||||
if strings.TrimSpace(taskRepositoryID) != "" {
|
||||
actions = append(actions,
|
||||
models.RecoveryActionRetryDefault,
|
||||
models.RecoveryActionPickBaseBranch,
|
||||
)
|
||||
switch category {
|
||||
case models.LaunchErrorCategoryBaseBranchMissing:
|
||||
actions = append(actions,
|
||||
models.RecoveryActionRetryDefault,
|
||||
models.RecoveryActionPickBaseBranch,
|
||||
)
|
||||
case models.LaunchErrorCategoryDefaultBranchUnresolved:
|
||||
actions = append(actions, models.RecoveryActionPickBaseBranch)
|
||||
}
|
||||
}
|
||||
if markReviewDone {
|
||||
if category == models.LaunchErrorCategoryWorkspaceCheckoutFailed || category == models.LaunchErrorCategoryGenericLaunchFailure {
|
||||
actions = append(actions, models.RecoveryActionRetryLaunch)
|
||||
}
|
||||
if category == models.LaunchErrorCategoryPRAlreadyClosed && markReviewDone {
|
||||
actions = append(actions, models.RecoveryActionMarkReviewDone)
|
||||
}
|
||||
return models.NormalizeRecoveryActions(actions)
|
||||
return models.NormalizeRecoveryActionsForCategory(category, actions)
|
||||
}
|
||||
|
||||
func (e *Executor) buildLastAgentError(
|
||||
ctx context.Context,
|
||||
taskID, taskRepositoryID string,
|
||||
@@ -80,7 +92,7 @@ func (e *Executor) buildLastAgentError(
|
||||
OccurredAt: occurredAt,
|
||||
Code: classification.code,
|
||||
Details: details,
|
||||
RecoveryActions: launchFailureRecoveryActions(taskRepositoryID, markReviewDone),
|
||||
RecoveryActions: launchFailureRecoveryActions(classification.code, taskRepositoryID, markReviewDone),
|
||||
TaskRepositoryID: taskRepositoryID,
|
||||
StampValue: models.StableLaunchErrorStamp(
|
||||
taskID, classification.code, taskRepositoryID, occurredAt.Format(time.RFC3339Nano),
|
||||
|
||||
Reference in New Issue
Block a user