-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry_policy_spec.rb
More file actions
235 lines (195 loc) · 8.09 KB
/
Copy pathretry_policy_spec.rb
File metadata and controls
235 lines (195 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# frozen_string_literal: true
RSpec.describe RubyLLM::Contract::Step::RetryPolicy do
describe "configuration" do
it "defaults to 1 attempt" do
policy = described_class.new
expect(policy.max_attempts).to eq(1)
end
it "sets max attempts" do
policy = described_class.new { attempts 3 }
expect(policy.max_attempts).to eq(3)
end
it "sets escalation models" do
policy = described_class.new { escalate "nano", "mini", "full" }
expect(policy.model_list).to eq(%w[nano mini full])
end
it "auto-sets max_attempts from escalation model count" do
policy = described_class.new { escalate "nano", "mini", "full" }
expect(policy.max_attempts).to eq(3)
end
it "keeps higher max_attempts if set explicitly" do
policy = described_class.new do
attempts 5
escalate "nano", "mini"
end
expect(policy.max_attempts).to eq(5)
end
it "defaults retryable statuses (breaking 0.7.0: :adapter_error removed — opt in explicitly)" do
policy = described_class.new
expect(policy.retryable_statuses).to eq(%i[validation_failed parse_error])
end
it "allows custom retryable statuses" do
policy = described_class.new { retry_on :parse_error }
expect(policy.retryable_statuses).to eq([:parse_error])
end
end
describe "#retryable?" do
let(:policy) { described_class.new }
it "returns true for retryable statuses" do
result = RubyLLM::Contract::Step::Result.new(status: :parse_error, raw_output: nil, parsed_output: nil)
expect(policy.retryable?(result)).to be true
end
it "returns false for :ok" do
result = RubyLLM::Contract::Step::Result.new(status: :ok, raw_output: nil, parsed_output: nil)
expect(policy.retryable?(result)).to be false
end
it "returns false for :input_error (not retryable — bad input won't improve)" do
result = RubyLLM::Contract::Step::Result.new(status: :input_error, raw_output: nil, parsed_output: nil)
expect(policy.retryable?(result)).to be false
end
end
describe "#model_for_attempt" do
context "with escalation models" do
let(:policy) { described_class.new { escalate "nano", "mini", "full" } }
it "returns model for each attempt" do
expect(policy.model_for_attempt(0, "default")).to eq("nano")
expect(policy.model_for_attempt(1, "default")).to eq("mini")
expect(policy.model_for_attempt(2, "default")).to eq("full")
end
it "returns last model for overflow attempts" do
expect(policy.model_for_attempt(5, "default")).to eq("full")
end
end
context "without escalation models" do
let(:policy) { described_class.new { attempts 3 } }
it "returns default model for all attempts" do
expect(policy.model_for_attempt(0, "default")).to eq("default")
expect(policy.model_for_attempt(2, "default")).to eq("default")
end
end
end
describe "validation" do
it "raises ArgumentError when attempts is 0" do
expect { described_class.new { attempts 0 } }.to raise_error(ArgumentError, /attempts must be at least 1/)
end
it "raises ArgumentError when attempts is negative" do
expect { described_class.new { attempts(-1) } }.to raise_error(ArgumentError, /attempts must be at least 1/)
end
it "raises ArgumentError when attempts is not an integer" do
expect { described_class.new { attempts "three" } }.to raise_error(ArgumentError, /attempts must be at least 1/)
end
it "raises ArgumentError for keyword attempts: 0" do
expect do
described_class.new(attempts: 0)
end.to raise_error(ArgumentError, /attempts must be at least 1/)
end
it "raises ArgumentError for keyword attempts: -1" do
expect do
described_class.new(attempts: -1)
end.to raise_error(ArgumentError, /attempts must be at least 1/)
end
end
describe "keyword API" do
it "accepts retry_on: keyword to override retryable statuses" do
policy = described_class.new(retry_on: [:parse_error])
expect(policy.retryable_statuses).to eq([:parse_error])
end
it "accepts models: keyword" do
policy = described_class.new(models: %w[nano mini])
expect(policy.model_list).to eq(%w[nano mini])
expect(policy.max_attempts).to eq(2)
end
it "accepts attempts: keyword" do
policy = described_class.new(attempts: 4)
expect(policy.max_attempts).to eq(4)
end
it "keyword models: works with config_list" do
policy = described_class.new(models: %w[gpt-4.1-nano gpt-4.1-mini])
expect(policy.config_list).to eq([{ model: "gpt-4.1-nano" }, { model: "gpt-4.1-mini" }])
end
end
describe "config-based features (v0.6)" do
describe "#config_list with string args" do
it "returns normalized configs for string escalation" do
policy = described_class.new { escalate "gpt-4.1-nano", "gpt-4.1-mini" }
expect(policy.config_list).to eq([{ model: "gpt-4.1-nano" }, { model: "gpt-4.1-mini" }])
end
end
describe "#config_list with hash args" do
it "preserves reasoning_effort in config" do
policy = described_class.new do
escalate({ model: "gpt-4.1-nano" }, { model: "gpt-4.1-mini", reasoning_effort: "high" })
end
expect(policy.config_list).to eq([
{ model: "gpt-4.1-nano" },
{ model: "gpt-4.1-mini", reasoning_effort: "high" }
])
end
end
describe "#config_list with mixed args" do
it "normalizes strings and hashes correctly" do
policy = described_class.new do
escalate "gpt-4.1-nano", { model: "gpt-4.1-mini", reasoning_effort: "high" }
end
expect(policy.config_list).to eq([
{ model: "gpt-4.1-nano" },
{ model: "gpt-4.1-mini", reasoning_effort: "high" }
])
end
end
describe "#config_for_attempt" do
let(:policy) do
described_class.new do
escalate(
{ model: "gpt-4.1-nano" },
{ model: "gpt-4.1-mini", reasoning_effort: "high" }
)
end
end
it "returns correct config per attempt" do
expect(policy.config_for_attempt(0, {})).to eq({ model: "gpt-4.1-nano" })
expect(policy.config_for_attempt(1, {})).to eq({ model: "gpt-4.1-mini", reasoning_effort: "high" })
end
it "returns last config for overflow index" do
expect(policy.config_for_attempt(5, {})).to eq({ model: "gpt-4.1-mini", reasoning_effort: "high" })
end
it "returns default_config when no configs" do
empty_policy = described_class.new { attempts 3 }
default = { model: "gpt-5-mini" }
expect(empty_policy.config_for_attempt(0, default)).to eq(default)
expect(empty_policy.config_for_attempt(2, default)).to eq(default)
end
end
describe "#model_for_attempt backward compatibility" do
it "returns string model name even with hash configs" do
policy = described_class.new do
escalate({ model: "gpt-4.1-nano", reasoning_effort: "low" }, { model: "gpt-4.1-mini" })
end
expect(policy.model_for_attempt(0, "default")).to eq("gpt-4.1-nano")
expect(policy.model_for_attempt(1, "default")).to eq("gpt-4.1-mini")
end
end
describe "#model_list" do
it "returns frozen array of model name strings" do
policy = described_class.new do
escalate({ model: "gpt-4.1-nano" }, { model: "gpt-4.1-mini", reasoning_effort: "high" })
end
list = policy.model_list
expect(list).to eq(%w[gpt-4.1-nano gpt-4.1-mini])
expect(list).to be_frozen
end
end
describe "normalize_config raises on invalid type" do
it "raises ArgumentError for non-String/Hash via escalate" do
expect do
described_class.new { escalate 42 }
end.to raise_error(ArgumentError, /Expected String or Hash, got Integer/)
end
it "raises ArgumentError for Symbol via escalate" do
expect do
described_class.new { escalate :some_model }
end.to raise_error(ArgumentError, /Expected String or Hash, got Symbol/)
end
end
end
end