Skip to content

fix(api): 按传输阶段判定可重试,修复连接超时不 failover - #54

Merged
im47cn merged 5 commits into
mainfrom
feat/quint-bug-mining
Nov 23, 2026
Merged

im47cn merged 5 commits into
mainfrom
feat/quint-bug-mining

Conversation

@im47cn

@im47cn im47cn commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

问题

FailoverTransport.isPreSendRetryable 用异常类型白名单(ConnectException / UnknownHostException / NoRouteToHostException)判定「发送前失败」,但连接超时不在此列:主网关 TCP 握手超时(丢包、黑洞、防火墙 DROP——正是 failover 最该生效的场景)会直接上抛给调用方,备用网关一次都不尝试。

根因不是漏了一个类型:JDK 与 OkHttp 下连接超时与读超时同为 java.net.SocketTimeoutException(仅消息串不同),按类型永远无法区分二者,而两者的 failover 安全性相反——前者请求字节未发出(重发无副作用),后者已发出(重发可能重复扣款)。

修复

WopSdkException 新增 Phase(UNKNOWN / CONNECT / SENT),由适配器在真正的阶段边界打标;core 先按 phase 判定,UNKNOWN 才退回异常类型白名单(第三方 Transport 行为不变)。

模块 阶段边界 理由
wop-sdk-jdkhttp 显式 connection.connect() 返回后置 SENT connect() 返回后请求体才会写出,是唯一边界。附带约束:setRequestMethod/setDoOutput 必须在 connect() 前调用,故上移
wop-sdk-okhttp network interceptor 内首行置 SENT 链序 RetryAndFollowUp → Bridge → Cache → ConnectInterceptor → network interceptors:连接/DNS/TLS 失败时拦截器不执行(保持 CONNECT),读超时与写体失败时已置 SENT。应用拦截器做不到——它跑在 RetryAndFollowUp 之前,早于建连
wop-sdk-unirest 不改码 其 java.net.http 后端连接超时的 cause 链最内层为 java.net.ConnectException,现有白名单已命中;且模块 release=8 无法编译期引用 java.net.http.*

证据链

1. falsifier:Quint 形式化反例(specs/quint/)

最小实例 N=2 候选 / budget=1,Apalache 穷尽验证(--max-steps=6):

模块 availability boundedAttempts noRetryAfterSend successOnlyWhenUp
failover_current(修复前) violation ok ok ok
failover_fixed(修复后) ok ok ok ok

两者仅差 retryable 一行。复现:quint verify --main=failover_current --invariant=availability --max-steps=6 specs/quint/failover.qnt(quint 0.33.1 + apalache 0.62.4,需 JDK 21)。

2. 原始素材:异常类型实测

JDK 21 探针(对 RFC5737 黑洞地址设 1s 连接超时 / 对 accept 后不响应的服务端设读超时)→ 两者均 SocketTimeoutException,instanceof ConnectException = false。修复后由 OkHttpTransportPhaseTest / JdkHttpTransportPhaseTest 固化为可重跑断言。

3. 门灵敏度先行:4 处变异逐一击穿

变异 被击穿的测试
core 忽略 phase(退回纯类型白名单) 4 个(含 connectPhaseSocketTimeoutFailsOver)
okhttp 去掉 network interceptor 2 个(readTimeoutReportsSentPhase 等)
jdkhttp 去掉显式 connect() 2 个(connectTimeoutReportsConnectPhase、connectionRefusedReportsConnectPhase)

变体后全绿仅因未跑测试的写法未被采纳:三处变异各自导致对应用例失败,还原后全绿。

4. 门禁

mvn -o verify 全绿(core 473 + okhttp 49 + jdkhttp 28 + unirest 46;jacoco 分支 100%);pitest core:1175 变异 / 1132 killed,test strength 96%,阈值 90 通过。api-baseline 快照随 Phase 新增同步刷新。

需要评审注意的行为变化

  1. TLS 握手失败此前不重试,打标后归 CONNECT → 变为可重试。 请求字节未发出,重发无重复副作用,但意味着握手失败时会在备用网关上再试一次。
  2. WopSdkException 默认 serialVersionUID 随公共成员变化而变(japicmp 报 Default serialversionuid changed)。本类非长期序列化载体,未钉 UID;若下游有跨版本反序列化需求请提出。
  3. OkHttp retryOnConnectionFailure 保持默认 true。 已实测确认其与 FailoverTransport 计数不冲突:仅在同 host 多 A 记录时叠加,单 IP 下 true/false 行为完全一致,跨网关绝不发生;唯一影响是把单次 attempt 的耗时上限抬到「DNS 路由数 × connectTimeout」。关掉它会削弱单网关场景韧性而收益为零,故不改。

缺证声明

  • Unirest 的连接超时异常类型未实测,其正确性依赖上游 java.net.http 保留 ConnectException 于 cause 链。上游若改为裸 HttpTimeoutException 会退化为「不重试」——安全侧,但可用性缺口回归。该模块无法在编译期引用 java.net.http.*,无法自行打标。
  • jdkhttp 的「连接超时」用例必须强制 ProxySelector 为 NO_PROXY:本机 http.proxyHost 会把黑洞地址变成「连接成功」,测试已在 try/finally 内替换并还原。

范围

两个 commit:022bcee(实现 + 测试 + baseline)、f121255(findings 同步)。未触 docs/、pom、crypto/、WopClient.java 等 PERIMETER 面。

Sourcery 摘要

根据请求是否已发送来决定是否进行故障转移,从而让连接超时可以安全重试,同时避免重复发送请求。

新功能:

  • 增加明确的传输阶段,用于区分连接失败与请求字节已发送后发生的失败。

错误修复:

  • 允许在连接超时时进行故障转移,同时防止请求发送后重试。
  • 包装已耗尽故障转移次数的错误时,正确保留最终传输阶段。

改进:

  • 根据传输阶段判断是否重试故障转移;对于第三方传输实现,保留基于旧版异常类型的回退逻辑。
  • 在 JDK HttpURLConnection 和 OkHttp 适配器的传输阶段边界添加埋点。
  • 增加正式的故障转移建模,并全面覆盖核心模块、JDK HTTP 和 OkHttp 传输中的阶段与超时场景。

文档:

  • 记录基于传输阶段的故障转移修复的正式结论、证据、支持的重试行为及已知限制。

测试:

  • 增加回归测试,覆盖连接超时、读取超时、连接被拒绝、重试次数限制和阶段隔离。

杂项:

  • 更新核心 API 基线,以纳入新的公开异常阶段 API。
Original summary in English

Sourcery 总结

在判定请求是否已发送后再执行故障转移,使连接超时能够安全地切换到备用网关,并避免重复发送请求。

新功能:

  • 添加显式传输阶段,用于区分连接失败和请求发送后的失败。

错误修复:

  • 允许连接超时时安全地进行故障转移,同时防止请求数据发送后重试。
  • 在报告故障转移尝试耗尽时保留最后的传输阶段。

增强功能:

  • 使用阶段信息进行故障转移决策,同时保留基于异常类型的回退逻辑,以兼容第三方传输实现。
  • 在 JDK HttpURLConnection 和 OkHttp 传输实现中,对连接与发送之间的边界进行埋点。

文档:

  • 记录正式的故障转移分析、支持的重试行为以及已知限制。

测试:

  • 添加正式的故障转移建模和回归测试,覆盖连接超时、读取超时、连接拒绝、重试次数限制以及阶段隔离。

杂项:

  • 更新核心 API 基线,以纳入新的异常阶段 API。
Original summary in English

Sourcery 摘要

判定请求是否已发送后执行故障转移,使连接超时能够切换到备用网关,同时避免请求发送后的重复重试。

新功能:

  • 在 WopSdkException 上公开明确的传输阶段,以便故障转移决策能够区分连接失败与请求发送后的失败。

错误修复:

  • 在连接超时时安全地执行故障转移,同时避免请求字节发送后重试。
  • 报告故障转移尝试耗尽时,保留最终传输阶段。

增强功能:

  • 使用适配器报告的阶段来决定是否重试,同时保留基于异常类型的回退逻辑,以兼容第三方传输实现。
  • 在 JDK HTTP 和 OkHttp 适配器中标记传输边界。

文档:

  • 添加正式文档和建模证据,说明故障转移行为、支持的重试场景及已知限制。

测试:

  • 添加形式化模型和回归测试,覆盖连接超时、读取超时、连接被拒绝、重试次数限制和阶段隔离。

杂项:

  • 更新核心 API 基线,以纳入新的公共异常阶段 API。
Original summary in English

Summary by Sourcery

判定请求是否已发送后执行故障转移,使连接超时能够切换到备用网关,同时避免请求发送后的重复重试。

New Features:

  • Expose explicit transport phases on WopSdkException so failover decisions can distinguish connection failures from failures after request transmission.

Bug Fixes:

  • Fail over safely on connection timeouts while preventing retries after request bytes have been sent.
  • Preserve the final transport phase when reporting exhausted failover attempts.

Enhancements:

  • Use adapter-reported phases for retry decisions while retaining exception-type fallback for third-party transports.
  • Mark transmission boundaries in the JDK HTTP and OkHttp adapters.

Documentation:

  • Add formal documentation and modeling evidence for failover behavior, supported retry scenarios, and known limitations.

Tests:

  • Add formal-model and regression coverage for connection and read timeouts, connection refusal, retry limits, and phase isolation.

Chores:

  • Update the core API baseline for the new public exception phase API.

Read this PR faster    Follow each request

用 Quint/Apalache 对 §7.3 P2 failover 重试策略建模(specs/quint/failover.qnt,
failover_current=现状策略 / failover_fixed=对照,仅差 retryable 一行)。
N=2、budget=1 最小实例下 Apalache 穷尽验证:现状策略唯一违反 availability,
反例 [ConnectTimeout, Up]——主网关握手超时、备用健康却直接失败。
JDK 实测证实连接超时与读超时同为 SocketTimeoutException、均不命中
isPreSendRetryable 白名单,模型与现实逐点对应。
findings.md 记录证据链与三个修复方向(均涉接口变更,待裁决)。

不接 CI/lefthook:quint 为本地工具链,CI 引入是独立决策。
主网关 TCP 握手超时(丢包/黑洞/防火墙 DROP)时请求字节未发出,语义上属于
发送前失败,但 JDK 与 OkHttp 抛出的连接超时与读超时同为
SocketTimeoutException,原白名单(ConnectException/UnknownHostException/
NoRouteToHostException)全部落空,异常直接上抛,备用网关不被尝试——
failover 在最需要它的场景失效。

- WopSdkException 新增 Phase(UNKNOWN/CONNECT/SENT)与三参构造器;
  旧构造器一律 UNKNOWN,第三方 Transport 行为不变。
- FailoverTransport.isPreSendRetryable 先按 phase 判定,UNKNOWN 才退回
  异常类型白名单;wrapExhausted 保留末次失败阶段。
- jdkhttp:以显式 connect() 作为阶段边界(setRequestMethod/setDoOutput
  相应上移至 connect 之前)。
- okhttp:以 network interceptor 为边界——它在 ConnectInterceptor 之后,
  连接/DNS/TLS 失败时不执行,读超时与写体失败时已置 SENT。

行为变化:TLS 握手失败此前不重试,打标后归 CONNECT 变为可重试(请求未发出)。
Unirest 未改码:其 java.net.http 后端的连接超时 cause 链含 ConnectException,
现白名单已命中;模块 release=8 无法引用 java.net.http.*。

api-baseline 快照同步刷新(新增公共类型 Phase 与三参构造器)。
@coldtea-pr-lens

coldtea-pr-lens Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
  • Redraw · drawn at f121255, the branch has moved on

Architecture

Walkthrough of Phase tagging for failover on connect timeout, in five steps

Read this PR faster

Inside the changed components — 1 view

Component view — Transport failover & adapters

Internal interaction between failover policy and tagged HTTP transport adapters

Architecture view of Component view — Transport failover & adapters in wop-platform/wop-java-sdk

Data flow

Data flow diagram for wop-platform/wop-java-sdk at f121255

Follow each request


Review

1 finding addressed · reviewed f121255

Tip

Set github.comment.collapsed: true in .github/pr-lens.yml to fold the comment behind one View architecture… row. Drawing still runs as before

🪧 More tips
  • Run npx skills add coldteadotai/pr-lens, then tell your coding agent: "Diagram the change you just made with PR Lens and attach it to the pull request."
  • Run npx @coldtea/pr-lens-cli analyze --base origin/main on a branch, then npx @coldtea/pr-lens-cli render .pr-lens/graph.json. Same lenses, your own model key, before the pull request exists
  • Click the link under each diagram to open it on a canvas you can zoom, pan and step through
  • The diagrams are links. Click one to open it on the canvas, then press W or click play to walk through the change
  • Open a diagram on the canvas, then press W or click play to walk through the change one step at a time
  • The CLI's render reads .github/pr-lens.yml and applies your renames, exclusions and lane pins at draw time
  • Set github.draw: on-demand in .github/pr-lens.yml and PR Lens stops drawing pull requests on its own. Comment @pr-lens draw on a pull request when you want that one drawn
  • Add .github/workflows/pr-lens.yml with coldteadotai/pr-lens/packages/action@v0 and your model provider's key as its api-key to run PR Lens from your own CI. Any /chat/completions endpoint works
  • After a push, tick Redraw to draw the latest commit
  • Switch GitHub to dark mode and the diagrams follow. The moving dots are this pull request's data in motion
  • Set github.description: false in .github/pr-lens.yml to stop PR Lens adding buttons to your PR descriptions

Thanks for using PR Lens! It's free for OSS. If it helped, a star helps other teams find it.  Star PR Lens on GitHub

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1540d671-3532-4e3c-a0d3-501fd5b24141
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

审阅者指南

API

添加公共传输阶段 API,并更新 API 基线以记录新增的枚举和构造函数。

  • wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java L10-48
  • wop-sdk-core/api-baseline/wop-sdk-core-api-baseline.jar

实现

在故障转移中使用感知阶段的重试决策,并在 JDK 和 OkHttp 适配器中标记连接到发送的边界。

  • wop-sdk-core/src/main/java/com/wanlianyida/wop/FailoverTransport.java L47-50, L69-86(+3 处小幅修改)
  • wop-sdk-jdkhttp/src/main/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransport.java L83, L91-102, L122-123
  • wop-sdk-okhttp/src/main/java/com/wanlianyida/wop/okhttp/OkHttpTransport.java L110-118(+3 处小幅修改)

文档

记录正式的故障转移分析结果、实现证据、行为变更和已知限制。

  • specs/quint/findings.md L1-70

测试

将故障转移可用性的反例和修正后的策略指定为形式模型。

  • specs/quint/failover.qnt L1-196

验证感知阶段的故障转移决策、适配器超时分类,以及每次调用之间的阶段隔离。

  • wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java L1-214
  • wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java L1-139
  • wop-sdk-okhttp/src/test/java/com/wanlianyida/wop/okhttp/OkHttpTransportPhaseTest.java L1-86

提示和命令

与 Sourcery 交互

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review。
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 根据审阅评论创建 GitHub issue: 回复审阅评论,请 Sourcery 根据该评论创建 issue。也可以在审阅评论中回复 @sourcery-ai issue 来创建 issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai,即可随时生成标题。也可以在拉取请求中评论 @sourcery-ai title,随时重新生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中需要的位置写入 @sourcery-ai summary,即可随时在该位置生成 PR 摘要。也可以在拉取请求中评论 @sourcery-ai summary,随时重新生成摘要。
  • 生成审阅者指南: 在拉取请求中评论 @sourcery-ai guide,即可随时重新生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。如果你已处理完所有评论且不想再看到它们,这项功能会很有用。
  • 隐藏所有 Sourcery 审阅: 在拉取请求中评论 @sourcery-ai dismiss,即可隐藏所有现有的 Sourcery 审阅。如果你想从头开始进行新的审阅,这项功能尤其有用——别忘了评论 @sourcery-ai review 来触发新的审阅!

自定义使用体验

访问你的控制面板以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide

API

Add the public transport phase API and update the API baseline to record the new enum and constructor.

  • wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java L10-48
  • wop-sdk-core/api-baseline/wop-sdk-core-api-baseline.jar

Implementation

Use phase-aware retry decisions in failover and mark the connection-to-send boundary in the JDK and OkHttp adapters.

  • wop-sdk-core/src/main/java/com/wanlianyida/wop/FailoverTransport.java L47-50, L69-86 (+3 small edits)
  • wop-sdk-jdkhttp/src/main/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransport.java L83, L91-102, L122-123
  • wop-sdk-okhttp/src/main/java/com/wanlianyida/wop/okhttp/OkHttpTransport.java L110-118 (+3 small edits)

Documentation

Record the formal failover findings, implementation evidence, behavior changes, and known limitations.

  • specs/quint/findings.md L1-70

Tests

Specify the failover availability counterexample and the corrected policy as a formal model.

  • specs/quint/failover.qnt L1-196

Verify phase-aware failover decisions, adapter timeout classification, and per-call phase isolation.

  • wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java L1-214
  • wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java L1-139
  • wop-sdk-okhttp/src/test/java/com/wanlianyida/wop/okhttp/OkHttpTransportPhaseTest.java L1-86

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗨——我发现了 5 个问题

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java" line_range="28" />
<code_context>
+        SENT
+    }
+
+    private final Phase phase;
+
+    /** 以明确语义消息构造(阶段 {@link Phase#UNKNOWN})。 */
</code_context>
<issue_to_address>
**Older exceptions stop deserializing**

When a consumer deserializes a `WopSdkException` serialized by an earlier SDK version, the added field and API change the computed `serialVersionUID`, so Java throws `InvalidClassException` instead of restoring the exception.

Declare an explicit `serialVersionUID` that matches the value used by the earlier SDK version.

Also at `wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java:40-48`.
</issue_to_address>

### Comment 2
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="76" />
<code_context>
+
+    @Test
+    void connectTimeoutReportsConnectPhase() throws Exception {
+        String root = "http://10.255.255.1:81";
+        WopSdkException ex = withoutProxy(() -> assertThrows(WopSdkException.class,
+                () -> new JdkHttpTransport().send(draft(), fastCall(root))));
</code_context>
<issue_to_address>
**Connect-timeout tests fail on some networks**

When the test runner rejects, routes, or otherwise handles `10.255.255.1:81` instead of silently dropping the connection, the connect-timeout tests in the JDK and OkHttp transports receive a connection or routing exception instead of `SocketTimeoutException`, so `mvn verify` fails even when phase classification is correct.

Replace the hard-coded endpoint with a deterministic test setup that reliably produces a connect timeout.

Also at `wop-sdk-okhttp/src/test/java/com/wanlianyida/wop/okhttp/OkHttpTransportPhaseTest.java:46-48`.
</issue_to_address>

### Comment 3
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="125-128" />
<code_context>
+    }
+
+    private static int freePort() {
+        try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) {
+            return socket.getLocalPort();
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+    }
</code_context>
<issue_to_address>
**Connection-refused test is flaky**

When another process binds the selected port after `freePort()` closes its socket but before the transport connects, the request can connect to that listener or fail after connection, so the connection-refused phase assertions fail even when the transport is correct.

Change the refusal test setup so the port cannot be claimed by another process between selection and the connection attempt.

Also at `wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:101-102`.
</issue_to_address>

### Comment 4
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="53-66" />
<code_context>
+    /** 在禁代理上下文中执行:否则 http.proxyHost 会把连接劫持成功。 */
+    private static <T> T withoutProxy(ThrowingSupplier<T> body) throws Exception {
+        ProxySelector original = ProxySelector.getDefault();
+        ProxySelector.setDefault(new ProxySelector() {
+            @Override
+            public List<Proxy> select(URI uri) {
+                return Collections.singletonList(Proxy.NO_PROXY);
+            }
+
+            @Override
+            public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+            }
+        });
+        try {
+            return body.get();
+        } finally {
+            ProxySelector.setDefault(original);
+        }
+    }
</code_context>
<issue_to_address>
**Concurrent tests corrupt proxy selection**

When JUnit runs multiple tests using `withoutProxy` concurrently, two test threads can save and replace the JVM-wide `ProxySelector`, then restore their saved values out of order; the final selector can differ from the original, routing later requests unexpectedly and making tests flaky.

Avoid changing the JVM-wide selector, or serialize all code that changes and depends on the default selector.
</issue_to_address>

### Comment 5
<location path="wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java" line_range="70" />
<code_context>
+                            new SocketTimeoutException("Connect timed out"),
+                            WopSdkException.Phase.CONNECT);
+                }
+                return new TransportResponse(200, new LinkedHashMap<String, String>(), new byte[0]);
+            }
+        };
</code_context>
<issue_to_address>
**Backup gateway can go untested**

When retry logic repeats the primary server root instead of advancing to the backup, `connectPhaseSocketTimeoutFailsOver` returns 200 on every call after the first, regardless of `call.serverRoot()`, and only checks the response status and attempt count. A regression that retries the primary instead of selecting the backup therefore still passes.

Assert that the second call uses the configured backup server root.
</issue_to_address>

Sourcery 评估

等待批准。 请先处理 5 项发现。

阻塞项:wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java:28、wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:76、wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:128、wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:66、wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java:70


Sourcery 对开源项目免费使用——如果您喜欢我们的评审,请考虑分享 ✨
Original comment in English

Hey - I've found 5 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java" line_range="28" />
<code_context>
+        SENT
+    }
+
+    private final Phase phase;
+
+    /** 以明确语义消息构造(阶段 {@link Phase#UNKNOWN})。 */
</code_context>
<issue_to_address>
**Older exceptions stop deserializing**

When a consumer deserializes a `WopSdkException` serialized by an earlier SDK version, the added field and API change the computed `serialVersionUID`, so Java throws `InvalidClassException` instead of restoring the exception.

Declare an explicit `serialVersionUID` that matches the value used by the earlier SDK version.

Also at `wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java:40-48`.
</issue_to_address>

### Comment 2
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="76" />
<code_context>
+
+    @Test
+    void connectTimeoutReportsConnectPhase() throws Exception {
+        String root = "http://10.255.255.1:81";
+        WopSdkException ex = withoutProxy(() -> assertThrows(WopSdkException.class,
+                () -> new JdkHttpTransport().send(draft(), fastCall(root))));
</code_context>
<issue_to_address>
**Connect-timeout tests fail on some networks**

When the test runner rejects, routes, or otherwise handles `10.255.255.1:81` instead of silently dropping the connection, the connect-timeout tests in the JDK and OkHttp transports receive a connection or routing exception instead of `SocketTimeoutException`, so `mvn verify` fails even when phase classification is correct.

Replace the hard-coded endpoint with a deterministic test setup that reliably produces a connect timeout.

Also at `wop-sdk-okhttp/src/test/java/com/wanlianyida/wop/okhttp/OkHttpTransportPhaseTest.java:46-48`.
</issue_to_address>

### Comment 3
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="125-128" />
<code_context>
+    }
+
+    private static int freePort() {
+        try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) {
+            return socket.getLocalPort();
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+    }
</code_context>
<issue_to_address>
**Connection-refused test is flaky**

When another process binds the selected port after `freePort()` closes its socket but before the transport connects, the request can connect to that listener or fail after connection, so the connection-refused phase assertions fail even when the transport is correct.

Change the refusal test setup so the port cannot be claimed by another process between selection and the connection attempt.

Also at `wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:101-102`.
</issue_to_address>

### Comment 4
<location path="wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java" line_range="53-66" />
<code_context>
+    /** 在禁代理上下文中执行:否则 http.proxyHost 会把连接劫持成功。 */
+    private static <T> T withoutProxy(ThrowingSupplier<T> body) throws Exception {
+        ProxySelector original = ProxySelector.getDefault();
+        ProxySelector.setDefault(new ProxySelector() {
+            @Override
+            public List<Proxy> select(URI uri) {
+                return Collections.singletonList(Proxy.NO_PROXY);
+            }
+
+            @Override
+            public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+            }
+        });
+        try {
+            return body.get();
+        } finally {
+            ProxySelector.setDefault(original);
+        }
+    }
</code_context>
<issue_to_address>
**Concurrent tests corrupt proxy selection**

When JUnit runs multiple tests using `withoutProxy` concurrently, two test threads can save and replace the JVM-wide `ProxySelector`, then restore their saved values out of order; the final selector can differ from the original, routing later requests unexpectedly and making tests flaky.

Avoid changing the JVM-wide selector, or serialize all code that changes and depends on the default selector.
</issue_to_address>

### Comment 5
<location path="wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java" line_range="70" />
<code_context>
+                            new SocketTimeoutException("Connect timed out"),
+                            WopSdkException.Phase.CONNECT);
+                }
+                return new TransportResponse(200, new LinkedHashMap<String, String>(), new byte[0]);
+            }
+        };
</code_context>
<issue_to_address>
**Backup gateway can go untested**

When retry logic repeats the primary server root instead of advancing to the backup, `connectPhaseSocketTimeoutFailsOver` returns 200 on every call after the first, regardless of `call.serverRoot()`, and only checks the response status and attempt count. A regression that retries the primary instead of selecting the backup therefore still passes.

Assert that the second call uses the configured backup server root.
</issue_to_address>

Sourcery assessment

Approval pending. 5 findings to address first.

Blocking findings: wop-sdk-core/src/main/java/com/wanlianyida/wop/WopSdkException.java:28, wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:76, wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:128, wop-sdk-jdkhttp/src/test/java/com/wanlianyida/wop/jdkhttp/JdkHttpTransportPhaseTest.java:66, wop-sdk-core/src/test/java/com/wanlianyida/wop/FailoverTransportPhaseTest.java:70


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b9eb834

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@im47cn

im47cn commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@sourcery-ai resolve

@im47cn
im47cn merged commit b870d9d into main Nov 23, 2026
26 of 27 checks passed
@im47cn
im47cn deleted the feat/quint-bug-mining branch November 23, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants