Skip to content

Commit f81fa2b

Browse files
committed
fix(agent): isolate streaming retry state per subscription and observe run stops during backoff (issue #313 review)
1 parent b81355b commit f81fa2b

3 files changed

Lines changed: 94 additions & 10 deletions

File tree

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public ModelResponse interceptModel(ModelRequest request, ModelCallHandler handl
125125
if (modelResponse.isStreaming()) {
126126
return ModelResponse
127127
.stream(JdkFlowAdapter.publisherToFlowPublisher(withStreamingRetry(request, handler,
128-
JdkFlowAdapter.flowPublisherToFlux(modelResponse.getStream()), attempt, controller)));
128+
JdkFlowAdapter.flowPublisherToFlux(modelResponse.getStream()), attempt)));
129129
}
130130
// Assistant content is data, never an out-of-band error signal.
131131
if (attempt > 1) {
@@ -167,16 +167,17 @@ else if (reason == RetryStopReason.NON_RETRYABLE) {
167167

168168
/**
169169
* Wraps a streaming response. Every subscription of the response owns its attempt
170-
* budget, so that a repeated subscription is not charged for the attempts of an
171-
* earlier one.
170+
* budget: the retry state is created per subscription, so a repeated subscription
171+
* is not charged for the attempts of an earlier one.
172172
*/
173173
private Flux<ChatEvent> withStreamingRetry(ModelRequest request, ModelCallHandler handler, Flux<ChatEvent> responseFlux,
174-
int attempt, ModelRetryController controller) {
174+
int attempt) {
175175
return Flux.defer(() -> {
176176
CancellationException cancellation = cancellation(null);
177177
if (cancellation != null) {
178178
return Flux.error(cancellation);
179179
}
180+
ModelRetryController controller = controller(request);
180181
AtomicBoolean chunkEmitted = new AtomicBoolean(false);
181182
return responseFlux.doOnNext(response -> chunkEmitted.set(true)).onErrorResume(error -> {
182183
if (chunkEmitted.get()) {
@@ -210,7 +211,7 @@ private Flux<ChatEvent> retryStreamingModelCall(ModelRequest request, ModelCallH
210211

211212
int nextAttempt = failedAttempt + 1;
212213
long nextDelay = decision.delay().orElseThrow().toMillis();
213-
return delay(nextDelay).thenMany(Flux.defer(() -> {
214+
return delay(nextDelay, controller).thenMany(Flux.defer(() -> {
214215
log.info("Retry model call, on the {}th attempt (out of {} attempts).", nextAttempt, maxAttempts);
215216
try {
216217
checkInterrupted();
@@ -220,7 +221,7 @@ private Flux<ChatEvent> retryStreamingModelCall(ModelRequest request, ModelCallH
220221
.error(terminalFailure(start.stopReason().orElse(RetryStopReason.EXECUTION_STOPPED), exception));
221222
}
222223
ModelResponse retryResponse = handler.call(request);
223-
return toStreamingFlux(request, handler, retryResponse, nextAttempt, controller);
224+
return toStreamingFlux(request, handler, retryResponse, nextAttempt);
224225
}
225226
catch (Exception retryError) {
226227
return retryStreamingModelCall(request, handler, nextAttempt, controller, retryError);
@@ -291,19 +292,33 @@ private CancellationException cancellation(Throwable failure) {
291292
}
292293

293294
private Flux<ChatEvent> toStreamingFlux(ModelRequest request, ModelCallHandler handler, ModelResponse modelResponse,
294-
int attempt, ModelRetryController controller) {
295+
int attempt) {
295296
if (modelResponse.isStreaming()) {
296297
return withStreamingRetry(request, handler, JdkFlowAdapter.flowPublisherToFlux(modelResponse.getStream()),
297-
attempt, controller);
298+
attempt);
298299
}
299300
return Flux.just(ChatEvent.response(modelResponse.getChatResponse()));
300301
}
301302

302-
private Flux<ChatEvent> delay(long currentDelay) {
303+
/**
304+
* 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
308+
* completion, because there is no run control to observe.
309+
*/
310+
private Flux<ChatEvent> delay(long currentDelay, ModelRetryController controller) {
303311
if (currentDelay <= 0) {
304312
return Flux.empty();
305313
}
306-
return Flux.<ChatEvent>empty().delaySubscription(Duration.ofMillis(currentDelay));
314+
ExecutionGuard guard = controller.guard().orElse(null);
315+
if (guard == null) {
316+
return Flux.<ChatEvent>empty().delaySubscription(Duration.ofMillis(currentDelay));
317+
}
318+
long slices = (currentDelay + BACKOFF_SLICE_MILLIS - 1) / BACKOFF_SLICE_MILLIS;
319+
return Flux.interval(Duration.ZERO, Duration.ofMillis(BACKOFF_SLICE_MILLIS)).take(slices)
320+
.doOnNext(tick -> guard.assertActive())
321+
.thenMany(Flux.empty());
307322
}
308323

309324
private Exception toException(Throwable error) {

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package io.github.agentic.ai.agent.orchestration.interceptors;
1717

18+
import io.github.agentic.ai.agent.interceptor.ModelCallHandler;
1819
import io.github.agentic.ai.agent.interceptor.ModelRequest;
1920
import io.github.agentic.ai.agent.interceptor.ModelResponse;
2021
import io.github.agentic.ai.agent.orchestration.interceptor.modelretry.ModelRetryInterceptor;
2122
import io.github.agentic.ai.agent.retry.TypeBasedModelCallFailureClassifier;
23+
import io.github.agentic.ai.api.chat.ChatEvent;
2224
import io.github.agentic.ai.api.chat.ChatMessage;
2325
import io.github.agentic.ai.api.chat.ChatResponse;
2426
import io.github.agentic.ai.api.chat.Usage;
@@ -31,6 +33,8 @@
3133
import io.github.agentic.ai.runtime.execution.TrustedExecutionContext;
3234
import org.junit.jupiter.api.Test;
3335
import org.junit.jupiter.api.Timeout;
36+
import reactor.adapter.JdkFlowAdapter;
37+
import reactor.core.publisher.Flux;
3438

3539
import java.time.Clock;
3640
import java.time.Instant;
@@ -196,6 +200,48 @@ void aCopiedRequestKeepsTheGuard() {
196200
assertThat(ModelRequest.builder(request).build().getExecutionGuard()).contains(control);
197201
}
198202

203+
/**
204+
* 验证流式重试的响应式退避按切片观察运行状态:取消运行后等待在下一个切片终止,
205+
* 而不是等满整个配置的退避时长。
206+
*/
207+
@Test
208+
void aStreamingBackoffTerminatesWhenTheRunIsCancelled() throws Exception {
209+
ExecutionControl control = control(NOW.plusSeconds(60));
210+
ModelRetryInterceptor interceptor = ModelRetryInterceptor.builder()
211+
.maxAttempts(2)
212+
.initialDelay(30000)
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+
flux.subscribe(events -> {
227+
}, error -> {
228+
failure.set(error);
229+
done.countDown();
230+
}, done::countDown);
231+
// The failed streaming attempt is counted and the sliced backoff has started.
232+
Thread.sleep(200);
233+
234+
long cancelAt = System.nanoTime();
235+
control.cancel();
236+
assertThat(done.await(5, TimeUnit.SECONDS)).as("the wait must terminate on cancellation").isTrue();
237+
long elapsedMillis = (System.nanoTime() - cancelAt) / 1_000_000;
238+
239+
assertThat(failure.get()).isInstanceOf(ExecutionStoppedException.class);
240+
assertThat(elapsedMillis).as("the cancellation must be observed by the next 100 ms slice, not after 30 s")
241+
.isLessThan(5000);
242+
assertThat(attempts).hasValue(1);
243+
}
244+
199245
private ModelRequest request(ExecutionControl control) {
200246
return ModelRequest.builder().executionGuard(control).build();
201247
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,29 @@ void testBuilderValidation() {
371371
}, "backoffMultiplier 必须 >= 1.0");
372372
}
373373

374+
@Test
375+
void testStreamingRetryStateDoesNotLeakAcrossSubscriptions() {
376+
ModelRetryInterceptor interceptor = ModelRetryInterceptor.builder().maxAttempts(2).initialDelay(0).build();
377+
378+
AtomicInteger attemptCount = new AtomicInteger(0);
379+
380+
ModelCallHandler handler = request -> {
381+
attemptCount.incrementAndGet();
382+
return streaming(Flux.error(new RuntimeException("I/O error on streaming response")));
383+
};
384+
385+
ModelResponse response = interceptor.interceptModel(ModelRequest.builder().build(), handler);
386+
Flux<ChatEvent> responseFlux = JdkFlowAdapter.flowPublisherToFlux(response.getStream());
387+
388+
// First subscription: consumes its own budget (1 streaming retry) and exhausts.
389+
assertThrows(RuntimeException.class, () -> responseFlux.collectList().block(Duration.ofSeconds(2)));
390+
assertEquals(2, attemptCount.get(), "第一次订阅应拥有自己的尝试预算");
391+
392+
// Second subscription: a fresh budget, not the exhausted state of the first one.
393+
assertThrows(RuntimeException.class, () -> responseFlux.collectList().block(Duration.ofSeconds(2)));
394+
assertEquals(3, attemptCount.get(), "第二次订阅必须获得独立的重试预算");
395+
}
396+
374397
private static ModelResponse streaming(Flux<ChatEvent> events) {
375398
return ModelResponse.stream(JdkFlowAdapter.publisherToFlowPublisher(events));
376399
}

0 commit comments

Comments
 (0)