Skip to content

test: 修复 UDS 与熔断器测试在 Bun 1.4 及全量运行中的失败 - #1378

Open
q0wkp wants to merge 2 commits into
claude-code-best:mainfrom
q0wkp:fix/flaky-uds-and-circuit-breaker-tests
Open

q0wkp wants to merge 2 commits into
claude-code-best:mainfrom
q0wkp:fix/flaky-uds-and-circuit-breaker-tests

Conversation

@q0wkp

@q0wkp q0wkp commented Nov 16, 2026 •

Copy link
Copy Markdown

概述

在干净的 main 上有 6 个测试失败:3 个 UDS 消息测试和 3 个 H7 熔断器测试。都是测试本身的问题,不是生产代码的 bug:UDS 测试依赖 Bun 1.4 之前的 socket 行为和干净的运行环境;熔断器测试真的调用了 queryHaiku,后台异步抛出的错误被算到了当时正在运行的测试头上。

改动

问题 1:UDS 测试在 Bun 1.4 上超时

原因分析

从 Bun 1.4 起,一个没有人读取数据的 socket 收不到 EOF,因此不会触发 close,server.close() 也会一直等下去。这与 Node 的行为一致;Bun 1.3.11 的行为不同,而测试依赖了旧行为。已用独立脚本在 Bun 1.4.2、Bun 1.3.11 和 Node 上复现对比。CI 使用 bun-version: latest,同样会受影响。

  • destroys oversized frames before enqueueing inbox work:客户端从不读取服务端返回的错误帧,所以收不到 close。
  • rejects closed receiver responses without waiting for timeout:receiver 的服务端 socket 从不读取,finally 里的 receiver.close() 永远等不到结束。

修复:在不读取数据的一端调用 resume()。生产代码的收发两端都挂了数据读取器,不受影响。

问题 2:在 Claude Code 会话中运行时 UDS drain 测试失败

drainInbox returns each pending socket message once 断言 CLAUDE_CODE_MESSAGING_TOKEN 未设置,但 Claude Code 会话会把它(以及 CLAUDE_CODE_MESSAGING_SOCKET)导出给子进程。现在测试文件在每个用例前清除这两个变量,用例结束后恢复。

问题 3:全量运行时 H7 熔断器测试失败

原因分析

H7 用例通过真实的 queryHaiku 驱动 llmObserverBackend。单独运行时请求很早就失败(没有凭据);全量运行时,前面测试文件留下的认证状态和 mock 让请求走得更远,进入 queryModel,其中不等待结果的 logAPIQuery 抛出 ReferenceError: MACRO is not defined。Bun 把这个未处理的 rejection 算到当时正在运行的测试上。observerBackend.test.ts 中也有一个用例发起了真实请求并丢弃了 Promise,表现为后续某个文件里的 "Unhandled error between tests"。

  • llmObserverBackend.ts:新增 setLlmObserverQueryForTest(),用于替换模型调用(传 null 恢复 queryHaiku)。
  • throttleAndCircuitBreaker.test.ts:H7 用例改用必定失败的替身,并按调用次数断言:达到阈值、熔断打开后不再调用模型;resetCircuitBreaker() 之后重新调用。原来的用例只检查返回值是不是数组,测不出熔断器是否生效。
  • observerBackend.test.ts:llm 路由用例中替换模型调用。

测试方法

  1. bun run precheck:类型检查、lint 和全量测试通过(6022 pass、0 fail,没有未处理错误)。
  2. bun test src/utils/__tests__/udsMessaging.test.ts src/services/skillLearning/__tests__/throttleAndCircuitBreaker.test.ts 在 Bun 1.4.2 和 Bun 1.3.11 上都通过。
  3. 在 Claude Code 会话中(已设置 CLAUDE_CODE_MESSAGING_TOKEN)运行第 2 步,仍然通过。

检查清单

  • 已添加/更新测试
  • 已更新文档(如适用)
  • 无破坏性变更(或已在上文说明)

AI 披露

  • AI 参与编码
    • Claude Opus 5.5
  • AI 参与测试与调试
    • Claude Opus 5.5
  • AI 参与写作(PR 描述、文档、提交信息)
    • Claude Opus 5.5
  • AI 参与审查

Summary by CodeRabbit

  • Tests
    • Improved reliability of automated checks for observer behavior, circuit-breaker handling, and messaging, including cases involving failures, resets, and empty inputs.
    • Tests now run with more isolated model and messaging setup.

Bun 1.4 起与 Node 一致:不读取数据的 socket 收不到 EOF,因此不会触发
close,server.close() 也会一直等待。oversized frame 用例的客户端和
closed receiver 用例的服务端都没有读取,测试超时。改为显式 resume()。

在 Claude Code 会话中运行测试时,环境里已有 CLAUDE_CODE_MESSAGING_TOKEN
和 CLAUDE_CODE_MESSAGING_SOCKET,导致 drainInbox 用例的断言失败。测试前
清除、测试后恢复这两个变量。
H7 熔断器用例和 observerBackend 的 llm 路由用例会走完整的 API 调用链。
全量运行时,前面的测试文件留下的认证和 mock 让请求走得更远,触发
logAPIQuery 中未定义 MACRO 的异步异常,H7 用例失败或报出测试间未处理
错误。

llmObserverBackend 增加 setLlmObserverQueryForTest 替换模型调用,相关
用例改用必定失败的替身,并按调用次数断言熔断器确实打开、重置后恢复。
@mintlify

mintlify Bot commented Nov 16, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 40ea96c.

Approve preview

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The LLM observer now supports a replaceable query function used by tests. Skill-learning tests verify routing and circuit-breaker behavior. UDS messaging tests isolate environment variables and resume sockets in two cases.

Changes: Skill-Learning Observer Tests

Layer / File(s) Summary
Configurable LLM query and observer checks
src/services/skillLearning/llmObserverBackend.ts, src/services/skillLearning/__tests__/observerBackend.test.ts, src/services/skillLearning/__tests__/throttleAndCircuitBreaker.test.ts
The observer uses a replaceable query function. Tests stub it to check synchronous routing, circuit-breaker thresholds and reset behavior, and that empty observations do not call the model.

UDS Messaging Tests

Layer / File(s) Summary
Messaging test environment and socket handling
src/utils/__tests__/udsMessaging.test.ts
Tests save and restore messaging environment variables. The oversized-frame and closed-response tests resume their sockets.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Merge Risk: Minimal · up to 40ea9

This PR stabilizes tests for UDS messaging and the circuit breaker, and it adds a test-only hook that defaults to the existing model query. It carries no production behavior change and is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title is a short, single-line conventional-commit style summary: it names the test-only fix for UDS and circuit-breaker test failures under Bun 1.4 and full-suite runs. The raw summary and PR obje…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title is a short, single-line conventional-commit style summary: it names the test-only fix for UDS and circuit-breaker test failures under Bun 1.4 and full-suite runs. The raw summary and PR objectives confirm the changeset is test-only: a test-side socket resume, env var save/restore, and a new setLlmObserverQueryForTest stub for the circuit-breaker tests. The title covers the main change without claiming a production fix, which matches the objectives. The title is written in Chinese, which is consistent with the repository's PR context and does not reduce its clarity.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6f465088-b2bd-4e51-8fd3-d79a09704239
Commits

Reviewing files that changed from the base of the PR and between 963e001 and 40ea96c.

Files selected for processing: 4

  • src/services/skillLearning/__tests__/observerBackend.test.ts
  • src/services/skillLearning/__tests__/throttleAndCircuitBreaker.test.ts
  • src/services/skillLearning/llmObserverBackend.ts
  • src/utils/__tests__/udsMessaging.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant