Skip to content

Commit fbc7670

Browse files
committed
fix(agent): preserve the full configured backoff across reactive delay slices (issue #313 review)
1 parent f81fa2b commit fbc7670

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎argi-orchestration/argi-agent-framework/src/main/java/io/github/agentic/ai/agent/orchestration/interceptor/modelretry/ModelRetryInterceptor.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,11 @@ private Flux<ChatEvent> toStreamingFlux(ModelRequest request, ModelCallHandler h
302302

303303
/**
304304
* Waits for the backoff of a streaming retry. A run that carries a guard waits in
305-
* slices, mirroring the synchronous {@link #pause(long, ModelRetryController)}: a
306-
* cancellation or an expired deadline terminates the wait instead of being noticed
307-
* when the next attempt would start. Without a guard the wait is a plain delayed
305+
* slices, mirroring the synchronous {@link #pause(long, ModelRetryController)}: the
306+
* guard is checked once up front, then at every slice boundary, so a cancellation
307+
* or an expired deadline terminates the wait instead of being noticed when the next
308+
* attempt would start. The first tick arrives after a full slice, so the total wait
309+
* covers the whole configured delay. Without a guard the wait is a plain delayed
308310
* completion, because there is no run control to observe.
309311
*/
310312
private Flux<ChatEvent> delay(long currentDelay, ModelRetryController controller) {
@@ -315,8 +317,11 @@ private Flux<ChatEvent> delay(long currentDelay, ModelRetryController controller
315317
if (guard == null) {
316318
return Flux.<ChatEvent>empty().delaySubscription(Duration.ofMillis(currentDelay));
317319
}
320+
// The run may already be stopped when the backoff begins.
321+
guard.assertActive();
318322
long slices = (currentDelay + BACKOFF_SLICE_MILLIS - 1) / BACKOFF_SLICE_MILLIS;
319-
return Flux.interval(Duration.ZERO, Duration.ofMillis(BACKOFF_SLICE_MILLIS)).take(slices)
323+
return Flux.interval(Duration.ofMillis(BACKOFF_SLICE_MILLIS), Duration.ofMillis(BACKOFF_SLICE_MILLIS))
324+
.take(slices)
320325
.doOnNext(tick -> guard.assertActive())
321326
.thenMany(Flux.empty());
322327
}

‎argi-orchestration/argi-agent-framework/src/test/java/io/github/agentic/ai/agent/orchestration/interceptors/ModelRetryInterceptorControlledTest.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,46 @@ void aCopiedRequestKeepsTheGuard() {
200200
assertThat(ModelRequest.builder(request).build().getExecutionGuard()).contains(control);
201201
}
202202

203+
/**
204+
* 验证切片化退避不吞时长:首个切片在整整一个切片之后到达,配置的退避时长被完整保留
205+
* (首拍立即到达时 200ms 的退避会在 ~100ms 结束)。
206+
*/
207+
@Test
208+
void theStreamingBackoffPreservesTheFullConfiguredDelay() throws Exception {
209+
ExecutionControl control = control(NOW.plusSeconds(60));
210+
ModelRetryInterceptor interceptor = ModelRetryInterceptor.builder()
211+
.maxAttempts(2)
212+
.initialDelay(200)
213+
.clock(CLOCK)
214+
.build();
215+
AtomicInteger attempts = new AtomicInteger();
216+
ModelCallHandler handler = request -> {
217+
attempts.incrementAndGet();
218+
return ModelResponse.stream(JdkFlowAdapter.publisherToFlowPublisher(Flux
219+
.error(new RuntimeException("I/O error: connection lost"))));
220+
};
221+
ModelResponse response = interceptor.interceptModel(request(control), handler);
222+
Flux<ChatEvent> flux = JdkFlowAdapter.flowPublisherToFlux(response.getStream());
223+
224+
AtomicReference<Throwable> failure = new AtomicReference<>();
225+
CountDownLatch done = new CountDownLatch(1);
226+
long start = System.nanoTime();
227+
flux.subscribe(events -> {
228+
}, error -> {
229+
failure.set(error);
230+
done.countDown();
231+
}, done::countDown);
232+
assertThat(done.await(5, TimeUnit.SECONDS)).isTrue();
233+
long elapsedMillis = (System.nanoTime() - start) / 1_000_000;
234+
235+
assertThat(failure.get()).isInstanceOf(RuntimeException.class);
236+
assertThat(elapsedMillis).as("the 200 ms backoff must not end after the first immediate slice (~105 ms)")
237+
.isGreaterThanOrEqualTo(190);
238+
// The sequence is bounded by maxAttempts: the backoff ends and the second
239+
// attempt runs before the final exhaustion.
240+
assertThat(attempts).hasValue(2);
241+
}
242+
203243
/**
204244
* 验证流式重试的响应式退避按切片观察运行状态:取消运行后等待在下一个切片终止,
205245
* 而不是等满整个配置的退避时长。

0 commit comments

Comments
 (0)