|
| 1 | +package runtime |
| 2 | + |
| 3 | +// failure_streak_test.go verifies the consecutive-failure cap counts planner |
| 4 | +// decision points, not individual parallel calls: any budgeted success resets |
| 5 | +// the streak, an all-failure batch consumes exactly one unit, and bookkeeping |
| 6 | +// results never move the counter. |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "goa.design/goa-ai/runtime/agent/planner" |
| 13 | + "goa.design/goa-ai/runtime/agent/policy" |
| 14 | + "goa.design/goa-ai/runtime/agent/tools" |
| 15 | +) |
| 16 | + |
| 17 | +func TestApplyFailureStreak(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + cases := []struct { |
| 21 | + name string |
| 22 | + caps policy.CapsState |
| 23 | + progress bool |
| 24 | + failed bool |
| 25 | + wantRemaining int |
| 26 | + wantTripped bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "budgeted success resets streak", |
| 30 | + caps: policy.CapsState{MaxConsecutiveFailedToolCalls: 3, RemainingConsecutiveFailedToolCalls: 1}, |
| 31 | + progress: true, |
| 32 | + failed: true, |
| 33 | + wantRemaining: 3, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "all-failure batch consumes one unit", |
| 37 | + caps: policy.CapsState{MaxConsecutiveFailedToolCalls: 3, RemainingConsecutiveFailedToolCalls: 3}, |
| 38 | + failed: true, |
| 39 | + wantRemaining: 2, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "final all-failure batch trips the cap", |
| 43 | + caps: policy.CapsState{MaxConsecutiveFailedToolCalls: 3, RemainingConsecutiveFailedToolCalls: 1}, |
| 44 | + failed: true, |
| 45 | + wantRemaining: 0, |
| 46 | + wantTripped: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "bookkeeping-only batch leaves the counter unchanged", |
| 50 | + caps: policy.CapsState{MaxConsecutiveFailedToolCalls: 3, RemainingConsecutiveFailedToolCalls: 2}, |
| 51 | + wantRemaining: 2, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "unset cap never trips", |
| 55 | + caps: policy.CapsState{}, |
| 56 | + failed: true, |
| 57 | + wantRemaining: 0, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range cases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + caps := tc.caps |
| 64 | + tripped := applyFailureStreak(&caps, tc.progress, tc.failed) |
| 65 | + require.Equal(t, tc.wantTripped, tripped) |
| 66 | + require.Equal(t, tc.wantRemaining, caps.RemainingConsecutiveFailedToolCalls) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestBudgetedBatchOutcome(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + budgeted := newAnyJSONSpec("ada.get_energy_rates", "ada") |
| 75 | + budgetedOK := newAnyJSONSpec("ada.get_weather_forecast", "ada") |
| 76 | + progressSpec := newBookkeepingSpec("tasks.progress.update") |
| 77 | + |
| 78 | + rt := New() |
| 79 | + seedTestToolSpecs(rt, budgeted, budgetedOK, progressSpec) |
| 80 | + |
| 81 | + record := func(name tools.Ident, failed bool) stepToolRecord { |
| 82 | + result := &planner.ToolResult{Name: name, ToolCallID: "call-" + string(name)} |
| 83 | + if failed { |
| 84 | + result.Error = planner.NewToolError("boom") |
| 85 | + } |
| 86 | + return stepToolRecord{ |
| 87 | + call: planner.ToolRequest{Name: name, ToolCallID: "call-" + string(name)}, |
| 88 | + result: result, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + cases := []struct { |
| 93 | + name string |
| 94 | + records []stepToolRecord |
| 95 | + wantProgress bool |
| 96 | + wantFailed bool |
| 97 | + }{ |
| 98 | + { |
| 99 | + name: "mixed parallel batch reports progress and failure", |
| 100 | + records: []stepToolRecord{ |
| 101 | + record(budgeted.Name, true), |
| 102 | + record(budgetedOK.Name, false), |
| 103 | + }, |
| 104 | + wantProgress: true, |
| 105 | + wantFailed: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "all budgeted failures report failure only", |
| 109 | + records: []stepToolRecord{ |
| 110 | + record(budgeted.Name, true), |
| 111 | + record(budgetedOK.Name, true), |
| 112 | + }, |
| 113 | + wantFailed: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "bookkeeping success is not progress", |
| 117 | + records: []stepToolRecord{ |
| 118 | + record(progressSpec.Name, false), |
| 119 | + record(budgeted.Name, true), |
| 120 | + }, |
| 121 | + wantFailed: true, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "bookkeeping-only batch reports neither", |
| 125 | + records: []stepToolRecord{ |
| 126 | + record(progressSpec.Name, false), |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tc := range cases { |
| 132 | + t.Run(tc.name, func(t *testing.T) { |
| 133 | + progress, failed := rt.budgetedBatchOutcome(tc.records) |
| 134 | + require.Equal(t, tc.wantProgress, progress) |
| 135 | + require.Equal(t, tc.wantFailed, failed) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments