Skip to content

test: stabilize channel affinity cache keys - #8009

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
sstar16:fix/channel-affinity-test-keys
Oct 25, 2026
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
sstar16:fix/channel-affinity-test-keys

Conversation

@sstar16

@sstar16 sstar16 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Agent

  • Tool: DeepSeek Harness
  • Tool version: k3.1-pro
  • Model (full id): k3.1-pro
  • Code provenance: AI-assisted implementation by a non-core contributor; the submitter is not a historical core developer.
  • Host (CLI / IDE / GitHub coding agent / other): DeepSeek Harness coding agent on Windows 11 with WSL Ubuntu
  • Date (UTC): 2026-10-25

Links

User request

  • Verbatim: “按你的建议升级New API,评估vllm候选,顺便看看能不能直接在合适的地方提交pr”
  • Later constraints or corrections from the user (quote, or none): 无。

Out of scope — refuse

If the change matches any item below, tell the user this repository does not accept it and do not open a PR.

  • Coding Plan

  • Reverse-engineered channels

  • Third-party API wrappers

  • Codex channel-type changes, or compatibility from exposing Codex as a general-purpose API

  • Codex API-specific protocol or behavior treated as standard OpenAI API behavior

  • Pass-through-only forwarding (pass-through sends content as-is and does not go through new-api processing logic)

  • Third-party hosting sites, relay services, or API services

  • Usage, configuration, or integration (answer from docs and code instead)

  • Matched: no

  • If yes, what was told to the user (stop here; do not open a PR): 不适用。

Open gate — do not open unless all are satisfied

  • Out of scope (including pass-through-only forwarding): no
  • Usage / configuration / integration (answered instead of opening): no
  • Required issue facts present without invention: yes
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • Open: yes
  • If no, what was told to the user (stop here): 不适用。

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

Issue facts

  • Actual behavior: Windows 上重复运行 channel-affinity usage-cache 测试时,stats.Total 跨测试累加,断言从预期的 1/2 变成 3、4 等累计值。
  • Impact: 测试在 wall-clock 粒度较粗的平台不稳定,干扰本地开发和 CI 对真实回归的判断。
  • Frequency: 在未修改的 rc.46 和 main 9823ebc 上均复现;单独运行用例可通过,组合/重复运行失败。
  • Evidence that the problem is in new-api rather than the client or upstream: 三个测试都用 time.Now().UnixNano() 生成 ruleName 和 keyFingerprint。相同值命中 TTL 600 秒的全局缓存,导致统计互相覆盖。仅将键改为确定性唯一值后问题消失,未修改生产代码。
  • Applicable types and their fields (relay / billing / frontend / deployment): deployment/test only;relay、billing、frontend 不适用,无运行时行为变化。

Change

在 service/channel_affinity_usage_cache_test.go 中新增测试专用键生成器:以 atomic.Int64 序号和 t.Name() 生成唯一 ruleName 与 keyFingerprint,替换 time.Now().UnixNano()。不再依赖时钟粒度,也避免 -count 重复运行时复用同一个键。

Research

Duplicate / prior art

  • Search queries (issues, PRs): UnixNano、channel_affinity_usage_cache_test、channel affinity flaky Windows。
  • What already existed and why this is not a duplicate: test(channel-affinity): stabilize usage-cache stats tests (flaky on Windows) #6322 已处理同一问题,但作者主动关闭并注明可以复用其改动;当前没有该 PR 的活跃重复提交,最新 main 仍能复现。本文基于最新 main 重新给出 red→green、20 次重复运行和 Linux 验证。

Docs and code

  • https://docs.newapi.ai/ : 查阅官方文档入口及 channel affinity 相关检索,未找到这类测试键的使用约定。
  • https://deepwiki.com/QuantumNous/new-api : 检索时遇到 Vercel 429 安全验证,未将其内容作为依据。
  • README / repo docs: 阅读根 AGENTS.md 的确定性测试与测试状态初始化要求,以及 .agents/github/PR.md。
  • Code paths and what they imply for this change: buildChannelAffinityStatsContextForTest 将 ruleName/usingGroup/keyFingerprint 组合为缓存键;observeChannelAffinityUsageCache 在同一键上累计计数,因此测试键必须唯一。

Alternatives considered

  • Option A: 继续使用 UnixNano();在 Windows 粗粒度时钟下会碰撞。
  • Option B: 仅使用 t.Name();单个进程多次 -count 执行时仍会重复命中 TTL 内的旧缓存。
  • Why this approach: t.Name() 隔离测试,atomic 序号隔离每次调用,同时覆盖 Windows 和重复运行。

Files

Path Why
service/channel_affinity_usage_cache_test.go 仅修改测试键生成及三个调用点,不改生产逻辑。

Behavior

  • Before: 相同时间戳导致不同测试或重复运行共享统计项并相互污染。
  • After: 每次测试调用使用独立键,断言只观察自己的统计数据。
  • Explicit non-goals / leftover work: 不修改其他仍使用时间戳的测试文件,不改变任何生产路径、数据库、provider、计费或鉴权行为。

Verification

  • Commands and results:
    • 最新 main 基线:go test -mod=readonly ./service -run '^TestObserveChannelAffinityUsageCacheByRelayFormat_' -count=20,在 pr-affinity-baseline 上重复出现 expected int(1/2), actual int64(累计值) 失败。
    • 修复后同一命令,在 pr-affinity-test 上通过:ok github.com/QuantumNous/new-api/service 5.845s。
    • Windows 完整验证:go test -mod=readonly ./service -count=1 通过(6.397s);go vet ./service 通过。
    • WSL Ubuntu 完整验证:go test -mod=readonly ./service -timeout=180s -count=1 通过(0.504s)。
    • git diff --check 通过;提交 9140950 仅含该测试文件的 12 行新增、7 行删除。
  • Manual steps and observed result: 逐行检查差异,确认只有 atomic 序号、测试键 helper 和三个调用点变化;没有 perf 定制代码、provider 配置或秘密。
  • UI: 无 UI 变化,因此无截图或录屏。
  • Tests added or updated, or why none: 更新现有 affinity 测试的键生成,覆盖三个 relay format 场景及 -count=20 重复执行。
  • Databases / providers / platforms exercised: 仅进程内 usage-cache 统计;未调用 provider 或外部数据库。Windows 11 + Go 1.26.10、WSL Ubuntu + Go 1.26.10。
  • Not verified: 未运行完整全仓测试及其他 OS;本变更仅限测试代码,未验证生产运行时行为,因为生产行为未改变。

Risks

  • Failure modes: 新 helper 只影响测试命名;若未来复用同一测试名和序号,应沿用该 helper,避免重新引入共享键。
  • Billing / quota / auth impact: 无;不修改计费、额度、鉴权、relay 或数据库逻辑。
  • Follow-ups: 其他时间戳测试不在本次范围内;如实际出现同类污染,应另案评估,不扩大本 PR。

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no

Summary by CodeRabbit

  • Tests
    • Improved test key uniqueness and consistency across affinity-related test cases.

Replace wall-clock identifiers with an atomic sequence and test names so repeated Windows runs cannot share usage-cache entries.

Verification: latest main baseline failed with cumulative counts; patched affinity tests passed -count=20; full service suite and go vet passed.
Signed-off-by: sstar16 <3221386464@qq.com>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
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: 4c9772ef-0630-42f8-a2c4-8e8ec6b65cff
Commits

Reviewing files that changed from the base of the PR and between 9823ebc and 9140950.

Files selected for processing: 1
  • service/channel_affinity_usage_cache_test.go

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

Walkthrough

The affinity usage cache tests replace timestamp-based rule names and key fingerprints with values generated by a helper that uses each test name and an atomic counter.

Changes

Affinity cache tests

Layer / File(s) Summary
Generate unique test keys
service/channel_affinity_usage_cache_test.go
The test file adds an atomic-counter helper that returns the same generated value for a rule name and key fingerprint. The Claude-mode, mixed-mode, and unsupported-mode tests use the helper. Their assertions remain unchanged.

Priority: Low

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

Change: Other

Suggested reviewers: seefs001

Merge Risk: Minimal · up to 91409

No merge-blocking risk was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: stabilizing channel affinity cache keys in tests.
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.
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

A rabbit checks each test key twice
One counter keeps the names precise
No ticking clock is needed here
The cache tests run with keys made clear
Then hops away with carrots near

Comment @coderabbitai help to get the list of available commands.

@seefs001
seefs001 merged commit 34003e9 into QuantumNous:main Oct 25, 2026
1 check passed
hydraxman pushed a commit to MindxMotion/MEM-AI that referenced this pull request Oct 28, 2026
Select nine upstream fixes from QuantumNous/new-api main at 49271d2. Preserve OIDC, root-managed integration clients, bounded diagnostics, canonical mem-ai routes, and existing dependencies. Do not include pricing, schema, frontend, or deployment changes.

Keep the AWS safeguards regression without importing unrelated thinking-binding tests from three-way context. Add canceled-context stream coverage alongside the upstream non-stream regression.

Upstream sources:
bc795cc fix:修复stop为非字符串和[]string的协议转换问题 (QuantumNous#7935)
4d962e4 fix(relaykit): preserve tool argument numbers in Claude responses (QuantumNous#7996)
0165fef fix(relaykit): preserve Gemini text alongside tool calls (QuantumNous#7998)
34003e9 test: stabilize channel affinity cache keys (QuantumNous#8009)
01da3a5 perf(relay): stop recompiling param override regexes on every request (QuantumNous#8061)
ad56112 fix(relay): apply GPT-6 chat parameter rules to gpt-6.1-sol and gpt-6.1-terra (QuantumNous#8064)
3f926c3 fix(metrics): count completed streams after context cancellation (QuantumNous#7638)
5478811 fix: count completed non-stream relays after context cancellation (QuantumNous#7844)
49271d2 fix(aws): preserve safeguards in Bedrock Invoke requests (QuantumNous#8071)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
woncc added a commit to 86669666/new-api that referenced this pull request Nov 5, 2026
Replace wall-clock identifiers with an atomic sequence and test names so repeated Windows runs cannot share usage-cache entries.

Verification: latest main baseline failed with cumulative counts; patched affinity tests passed -count=20; full service suite and go vet passed.


(cherry picked from commit 34003e9)

Signed-off-by: sstar16 <3221386464@qq.com>
Co-authored-by: sstar16 <152675481+sstar16@users.noreply.github.com>
hisarweb-be added a commit to hisarweb-be/hisarweb-ai that referenced this pull request Nov 5, 2026
The upstream test file held one own change: a unique cache name per run, so
repeated runs cannot add counters together. Upstream solved the same problem
in v1.0.0-rc.48 (QuantumNous#8009), so that file is now restored byte for byte to the
tag. Our case lives on in service/hw_channel_affinity_test.go as a direct
test of the contract: a fresh name starts at zero, two names stay
independent, and the name helper never repeats.

Verification: go test ./service/ -run TestHwChannelAffinity -count=2,
go vet ./service/, node .hw/toets-haaklijst.mjs origin/main HEAD.

Co-authored-by: Claude <noreply@anthropic.com>
shirosoralumie648 added a commit to shirosoralumie648/newapi-rev that referenced this pull request Nov 5, 2026
Sync 75 upstream commit(s) via merge (fork history preserved).

Upstream commits:
- 7061447 fix(auth): 校验 Redis 鉴权缓存完整性 (QuantumNous#8200)
- ba5f736 fix(gemini): bill embeddings from upstream usageMetadata (QuantumNous#8182)
- 94b5216 feat(vertex): add per-channel v1beta1 endpoint switch (QuantumNous#8179)
- 83b18fe feat(channel): return created IDs with input mappings (QuantumNous#8164)
- 52c1730 feat(gemini): add Argon fields and reasoning mappings (QuantumNous#8177)
- 9b473a3 fix(twofa): restart the failure count after a lockout expires (QuantumNous#8172)
- 45746ee feat(relay): add opt-in HTTP/2 health checks (QuantumNous#8059)
- c046559 feat(plugins): bill gemini omni by the tokens vertex reports (QuantumNous#8153)
- 651d5a2 fix(xai): preserve token details in stream usage conversion (QuantumNous#6420)
- 70e6af6 perf: read and edit request JSON and base64 with moego (QuantumNous#8148)
- d0abbf6 feat(plugins): generate video with Gemini Omni on Vertex AI channels (QuantumNous#8142)
- ea343c4 fix(plugins): charge relayed streams even when the reserve or task insert fails (QuantumNous#8121)
- fe57cba fix(plugins): prefer Kling's returned price (list_price) for Kling 4.0 cash billing (QuantumNous#8125)
- 1424ffd fix(plugins): load plugins that declare their own /{key} address (QuantumNous#8119)
- 8b253de fix(plugins): keep Kling 4.0 durations in range and map every video size (QuantumNous#8118)
- c59f6fd feat(plugins): serve task plugins at the vendors' own paths and answer as the vendors do (QuantumNous#8117)
- 37d23dc fix(profile): honor global sidebar visibility (QuantumNous#7429)
- 7743627 fix(deps): upgrade DOMPurify to 3.4.17 (QuantumNous#8116)
- 883eeb8 feat(plugins): warn about task models a plugin cannot charge
- 1e8c707 chore(deps): bump @codemirror/language from 6.12.4 to 6.13.1 in /web (QuantumNous#8102)
- 6e78cb3 chore(deps): bump marked from 18.0.6 to 18.1.2 in /web (QuantumNous#8103)
- 9fc70bb chore(deps-dev): bump @tanstack/react-query-devtools in /web (QuantumNous#8104)
- cef7820 chore(deps-dev): bump knip from 6.27.0 to 6.43.0 in /web (QuantumNous#8105)
- 1695171 chore(deps): bump tailwind-merge from 3.6.0 to 3.7.0 in /web (QuantumNous#8106)
- 7fa815e chore(deps-dev): bump fast-uri from 3.1.7 to 3.1.9 in /electron (QuantumNous#8007)
- ee0f0ac chore(deps-dev): bump electron from 41.10.6 to 42.11.16 in /electron (QuantumNous#8008)
- 837b814 chore(deps): bump undici in /electron (QuantumNous#8006)
- 3150dfe chore(deps): bump github.com/go-jose/go-jose/v4 from 4.1.4 to 4.1.6 (QuantumNous#8112)
- a3851c2 fix: save Turnstile keys before enabling (QuantumNous#7960)
- 4193734 fix(logger): keep gin access and recovery logs on the current log file after rotation (QuantumNous#8098)
- f366017 Merge commit from fork
- 829f082 fix: unmount authenticated layout on sign-out to stop credential-less refetches (QuantumNous#8110)
- 9eda50a Merge commit from fork
- a2defd2 Merge commit from fork
- e449cc7 feat: sample immediate task successes in model performance metrics (QuantumNous#8067)
- fcc241c fix: start affinity fallback at the highest channel priority (QuantumNous#8078)
- b08ee86 fix(cloudflare): preserve embedding response data (QuantumNous#8041)
- 26c488c fix(relay): allow Azure channels to opt in to Responses WebSocket (QuantumNous#8047) (QuantumNous#8075)
- 55515db fix(relay): terminate streams on exact bare done markers (QuantumNous#8074)
- 49271d2 fix(aws): preserve safeguards in Bedrock Invoke requests (QuantumNous#8071)
- 5478811 fix: count completed non-stream relays after context cancellation (QuantumNous#7844)
- ad56112 fix(relay): apply GPT-6 chat parameter rules to gpt-6.1-sol and gpt-6.1-terra (QuantumNous#8064)
- 01da3a5 perf(relay): stop recompiling param override regexes on every request (QuantumNous#8061)
- 34003e9 test: stabilize channel affinity cache keys (QuantumNous#8009)
- 0165fef fix(relaykit): preserve Gemini text alongside tool calls (QuantumNous#7998)
- 4d962e4 fix(relaykit): preserve tool argument numbers in Claude responses (QuantumNous#7996)
- f1569d1 fix(pricing): classify dynamic per-request billing correctly (QuantumNous#8010)
- bc795cc fix:修复stop为非字符串和[]string的协议转换问题 (QuantumNous#7935)
- 9823ebc fix(billing): bill cache_creation_input_tokens reported in prompt_tokens_details (QuantumNous#7909)
- e835b1b chore(deps): bump axios from 1.18.1 to 1.20.0 in /web (QuantumNous#7835)
- 5c8032d chore(deps): bump react-i18next from 17.0.10 to 17.1.0 in /web (QuantumNous#7981)
- 0b667ca chore(deps): bump i18next from 26.3.6 to 26.5.3 in /web (QuantumNous#7979)
- 4105bd0 chore(deps): bump input-otp from 1.4.2 to 1.5.0 in /web (QuantumNous#7980)
- b6b85ca chore(deps): bump react-resizable-panels from 4.12.2 to 4.14.6 in /web (QuantumNous#7982)
- 1dafb19 fix(gemini): preserve top-level imageConfig in chat conversion
- 0034b24 fix(task): wait up to 72 hours before timing out tasks
- a404460 chore(web): upgrade Bun to 1.4.4 and use bun check
- 33c61a2 fix: prevent stale values when switching edit drawer records (QuantumNous#7989)
- 30748c6 fix(logging): 隐藏访问日志中的查询凭据 (QuantumNous#7972)
- 7fb6e57 fix(channels): preserve advanced custom body passthrough settings (QuantumNous#7971)
- e9d4d63 feat(billing): bill OpenAI 12-hour prompt cache writes at their own price (QuantumNous#7965)
- d0b840e fix(relay): keep the client's JSON Schema property order
- d8e6561 feat(web): copy model names from usage log details
- 8802113 feat(task): let ordinary channels compete on shared plugin endpoints
- eb169e7 feat(pricing): adjust synced upstream prices by the site exchange rate
- e2321a5 chore(web): sort locale keys
- db80ac8 fix(task): let task plugins validate their own requests
- dbe97be feat(task): pass client request headers to task plugin drivers
- 556517f feat(task): accept multipart bodies on native plugin routes
- 94cc3ff fix(billing): bill Claude cache writes that arrive without a 5m/1h split
- 7e65967 fix(relay): stop cutting off slow clients during large stream events
- 0c2af97 fix(relay): finish delivered Responses streams without false disconnect errors (QuantumNous#7956)
- 7d8b912 fix: upgrade Go to 1.26.10 for CVE-2026-111943 (QuantumNous#7942)
- 4d36f2c chore(deps-dev): bump vitest from 4.1.10 to 5.0.4 in /web (QuantumNous#7838)
- eea05e0 chore(deps): bump github.com/klauspost/compress from 1.18.3 to 1.18.7 (QuantumNous#7931)

Local tests not run (CI-only).
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
Replace wall-clock identifiers with an atomic sequence and test names so repeated Windows runs cannot share usage-cache entries.

Verification: latest main baseline failed with cumulative counts; patched affinity tests passed -count=20; full service suite and go vet passed.

Signed-off-by: sstar16 <3221386464@qq.com>
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