mirror of
https://github.com/we-promise/sure.git
synced 2026-12-03 10:49:07 +08:00
Don't round a retry's timeout up; a late answer is a Timeout
within_budget rounded the remaining budget up, so 0.2 s left gave a retry a full second, and attempt accepted an answer that arrived after the deadline. The timeout is now the exact time left, and attempt checks the budget again once the answer is in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5f04861ed4
commit
4a27814e18
@@ -50,7 +50,9 @@ class Ai::Transport::Base
|
||||
begin
|
||||
tries += 1
|
||||
check_budget!
|
||||
yield
|
||||
result = yield
|
||||
check_budget!(after: true)
|
||||
result
|
||||
rescue *Ai::Error::RETRYABLE => error
|
||||
delay = retry_delay(tries)
|
||||
remaining = Ai::CallContext.current&.remaining
|
||||
@@ -62,16 +64,17 @@ class Ai::Transport::Base
|
||||
end
|
||||
|
||||
# Requests RubyLLM doesn't send, such as the models list, aren't checked by its instrumenter.
|
||||
def check_budget!
|
||||
# After an attempt too, so an answer that arrives late isn't taken as a pass.
|
||||
def check_budget!(after: false)
|
||||
return unless Ai::CallContext.current&.expired?
|
||||
|
||||
raise Ai::Error::Timeout, "the attempt's time budget was spent before the request was sent"
|
||||
raise Ai::Error::Timeout, "the attempt's time budget was spent #{after ? "before the answer arrived" : "before the request was sent"}"
|
||||
end
|
||||
|
||||
# A request's timeout, cut to what's left of the call's budget, so a retry can't outlast it.
|
||||
def within_budget(timeout)
|
||||
remaining = Ai::CallContext.current&.remaining
|
||||
remaining ? [ timeout, remaining.ceil ].min : timeout
|
||||
remaining ? [ timeout, remaining ].min : timeout
|
||||
end
|
||||
|
||||
def retry_delay(tries) = [ 0.5 * (2**(tries - 1)), 8 ].min * (0.75 + rand * 0.5)
|
||||
|
||||
@@ -73,10 +73,22 @@ class Ai::CallContextTest < ActiveSupport::TestCase
|
||||
model = ai_model(name: "test-model")
|
||||
transport = Ai::Transport.for(model.endpoint)
|
||||
|
||||
with_ai_call_context(model:, budget: 3) { assert_operator transport.send(:within_budget, 60), :<=, 3 }
|
||||
with_ai_call_context(model:, budget: 0.2) { assert_operator transport.send(:within_budget, 60), :<=, 0.2 }
|
||||
assert_equal 60, transport.send(:within_budget, 60)
|
||||
end
|
||||
|
||||
test "an answer that arrives after the budget is spent is a Timeout, not a pass" do
|
||||
model = ai_model(name: "test-model")
|
||||
transport = Ai::Transport.for(model.endpoint)
|
||||
|
||||
with_ai_call_context(model:, budget: 1) do |call|
|
||||
error = assert_raises(Ai::Error::Timeout) do
|
||||
transport.send(:attempt) { Ai::CallContext.stubs(:now).returns(call.deadline + 0.1); :ok }
|
||||
end
|
||||
assert_match(/before the answer arrived/, error.message)
|
||||
end
|
||||
end
|
||||
|
||||
test "the instrumenter forwards every event to RubyLLM's own instrumenter" do
|
||||
delegate = mock
|
||||
delegate.expects(:instrument).with("chat.ruby_llm", { a: 1 }).yields.returns(:done)
|
||||
|
||||
Reference in New Issue
Block a user