Skip to content

Commit 40c6753

Browse files
committed
fix(core): honor header overrides and retain realtime failure causes
1 parent 3331a59 commit 40c6753

7 files changed

Lines changed: 77 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Repeated default header settings now replace earlier values case-insensitively, while per-request headers retain precedence.
8+
- Realtime handshake and connection failures now retain the underlying exception through `RealtimeException.getCause()` for diagnostics.
9+
510
### Validation
611

712
- Manual Maven Central release workflow with source/CI checks, verified artifact digests, signing, retained deployment recovery, public package verification and matching GitHub Release notes. Publishing uses repository Actions secrets and proceeds after preflight without a separate environment approval.

‎docs/realtime.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Creation requires an application-generated stable `Idempotency-Key`; retry the s
4242
- Sends are serialized. Successful completion means the transport wrote the message, not that the server processed it. Cancelling a queued send removes it; cancelling or timing out a started send closes the connection because delivery is uncertain. Input is never replayed automatically.
4343
- Async continuations can execute on SDK/transport threads. Use `thenAcceptAsync`/an application executor for blocking work; do not synchronously wait for another event inside a completion callback.
4444
- A normal peer close ends `receive` with `null`. Buffered events drain before a terminal error. Abnormal closes, replacement and fatal server error events terminate the connection; `RealtimeException` preserves conversation/request IDs, close code/reason and the triggering error event through explicit getters. Nonfatal `error` and `work.failed` remain events for the application to handle.
45+
- Transport failures wrapped in `RealtimeException` retain their underlying exception through `getCause()`, while the top-level message remains generic.
4546
- Closing the parent client stops its connections. Always close a connection or use try-with-resources, including after asynchronous use. Closing a non-owning `client.async()` view does not close its parent.
4647

4748
There is no automatic reconnect or input replay. A caller can explicitly reconnect with the conversation ID and query persisted History to decide how to recover. WS `sequence`/`event_id` are not History or SSE cursors.

‎src/main/java/com/qoder/cloudagents/ForwardClient.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public Builder maxRetries(int value) {
219219

220220
public Builder defaultHeader(String name, String value) {
221221
RequestOptions.builder().header(name, value);
222+
headers.keySet().removeIf(key -> key.equalsIgnoreCase(name));
222223
headers.put(name, value);
223224
return this;
224225
}

‎src/main/java/com/qoder/cloudagents/errors/RealtimeException.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ public RealtimeException(
1717
Integer closeCode,
1818
String closeReason,
1919
JsonValue event) {
20-
super(message);
20+
this(message, conversationId, requestId, closeCode, closeReason, event, null);
21+
}
22+
23+
public RealtimeException(
24+
String message,
25+
String conversationId,
26+
String requestId,
27+
Integer closeCode,
28+
String closeReason,
29+
JsonValue event,
30+
Throwable cause) {
31+
super(message, cause);
2132
this.conversationId = conversationId;
2233
this.requestId = requestId;
2334
this.closeCode = closeCode;

‎src/main/java/com/qoder/cloudagents/forward/realtime/RealtimeConnection.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,14 @@ private RuntimeException asRuntime(Throwable e) {
435435
Throwable cause = Futures.unwrap(e);
436436
return cause instanceof RuntimeException
437437
? (RuntimeException) cause
438-
: problem("Realtime connection failed", null);
438+
: new RealtimeException(
439+
"Realtime connection failed",
440+
conversationId(),
441+
requestId(),
442+
closeCode,
443+
closeReason,
444+
null,
445+
cause);
439446
}
440447

441448
private RealtimeException problem(String message, JsonValue event) {

‎src/test/java/com/qoder/cloudagents/ForwardClientTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ void modelContractPreservesPrecisionAndRequestMetadata() throws Exception {
6868
assertEquals("0", request.getHeader("X-Qoder-Retry-Count"));
6969
}
7070

71+
@Test
72+
void defaultHeadersUseLastValueRegardlessOfCaseAndAllowRequestOverrides() throws Exception {
73+
try (ForwardClient configured =
74+
builder()
75+
.defaultHeader("X-Test-Header", "initial")
76+
.defaultHeader("x-test-header", "middle")
77+
.defaultHeader("X-Test-Header", "final")
78+
.build()) {
79+
server.enqueue(json(MODELS));
80+
configured.models().list();
81+
RecordedRequest request = server.takeRequest(2, TimeUnit.SECONDS);
82+
assertNotNull(request);
83+
assertEquals(
84+
java.util.Collections.singletonList("final"),
85+
request.getHeaders().values("x-test-header"));
86+
87+
server.enqueue(json(MODELS));
88+
configured
89+
.async()
90+
.models()
91+
.list(RequestOptions.builder().header("X-TEST-HEADER", "request").build())
92+
.get(3, TimeUnit.SECONDS);
93+
RecordedRequest overridden = server.takeRequest(2, TimeUnit.SECONDS);
94+
assertNotNull(overridden);
95+
assertEquals(
96+
java.util.Collections.singletonList("request"),
97+
overridden.getHeaders().values("x-test-header"));
98+
}
99+
}
100+
71101
@Test
72102
void asyncTemplateQueriesAndOpaquePathIdsAreEncoded() throws Exception {
73103
server.enqueue(

‎src/test/java/com/qoder/cloudagents/RealtimeConnectionTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.qoder.cloudagents.core.*;
77
import com.qoder.cloudagents.errors.*;
88
import com.qoder.cloudagents.forward.realtime.*;
9+
import java.io.IOException;
910
import java.time.Duration;
1011
import java.util.*;
1112
import java.util.concurrent.*;
@@ -417,10 +418,29 @@ void abruptDisconnectIsNotReconnected() throws Exception {
417418
RealtimeException e =
418419
assertThrows(RealtimeException.class, () -> c.receive(Duration.ofSeconds(3)));
419420
assertEquals(1006, e.closeCode());
421+
assertEquals("conv_test", e.conversationId());
422+
assertEquals("req-ws", e.requestId());
423+
assertInstanceOf(IOException.class, e.getCause());
424+
assertEquals("Realtime connection failed", e.getMessage());
420425
assertEquals(1, server.getRequestCount());
421426
}
422427
}
423428

429+
@ParameterizedTest
430+
@ValueSource(booleans = {false, true})
431+
void handshakeDisconnectPreservesTransportCause(boolean async) {
432+
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
433+
Throwable error =
434+
async
435+
? failed(client.async().realtime().connect(params().build()))
436+
: assertThrows(RealtimeException.class, this::connect);
437+
RealtimeException failure = assertInstanceOf(RealtimeException.class, error);
438+
assertInstanceOf(IOException.class, failure.getCause());
439+
assertEquals("conv_test", failure.conversationId());
440+
assertEquals(1006, failure.closeCode());
441+
assertEquals("Realtime connection failed", failure.getMessage());
442+
}
443+
424444
@Test
425445
void normalCloseReturnsEndOfStream() throws Exception {
426446
upgrade(true, false);

0 commit comments

Comments
 (0)