[Bug] 已正常完成的流式请求因 context 取消被模型健康统计忽略
提交前必读(请勿删除本节)
- 文档:https://docs.newapi.ai/
- 使用问题先看或先问:https://deepwiki.com/QuantumNous/new-api
- 开启透传请勿反馈问题;透传会直接发送内容,不会经过 new-api 的逻辑处理。
- 请以简练语言陈述事实与证据。Issue 正文及后续评论均不得直接粘贴未经人工过滤的大段 AI 文本。
- 转发类问题必须先确认问题发生在 new-api 这一层:请使用等价的脱敏请求分别直连上游和经过 new-api,对比响应状态、错误体与服务端日志。未经对比、仅展示上游报错的反馈不接受 issue。
- 不接受 Coding Plan、逆向渠道、第三方封装接口,以及将 Codex 接口反代为通用 API 后产生的兼容性问题;Codex API 自身特有的协议或行为也不应被当作标准 OpenAI API 行为,相关问题请先向渠道或接口提供方确认。
- 本项目不提供官方托管服务,也不为第三方托管站、中转站或 API 服务提供技术支持;此类问题请联系对应运营方。
- 警告:删除本模板、删除小节标题或随意清空内容的 issue,可能会被直接关闭;多次提交未经人工过滤的大段 AI 文本,或重复恶意提交者,可能会被 block。
部署来源
本仓库 main 分支源码。以下证据来自未修改的上游分类函数的本地最小复现,不宣称已完成官方实例的端到端部署验证。
您当前的 newapi 版本
上游 commit 1a4166d8e8ba9802d2ca56fe8ecf0ed5404e80d5。
提交确认
- [x] 非重复 issue: 以
created:>=2026-09-18搜索相关 Issues 和 PRs,未找到相同分类路径的报告或修复;查询和区别见 Duplicate check。 - [x] 提交前必读: 已检查文档、DeepWiki、README 和源码,确认不是使用、配置或接入问题。
- [x] 受支持版本: 已在上述未修改的上游分类函数上复现,版本精确到 commit。
- [x] 非第三方服务: 最小复现不依赖第三方托管服务或非公开补丁。
- [x] 非透传: 最小复现不执行转发或透传,只调用 new-api 自身的健康分类函数。
- [x] 问题归属: 同一个已完成的 StreamStatus 仅因 context 状态改变,由 success 变为 ignored;不涉及上游报错。
- [x] 渠道与协议边界: 不涉及 Coding Plan、逆向渠道、第三方封装或 Codex 专用协议。
- [x] 维护成本: 仅保留版本、复现、源码证据及未验证范围;不包含账户、服务器或消费明细。
问题描述
实际现象:流式请求已有 completed 终态、以 eof 或 done 正常结束,且没有 API 错误、流错误或结束错误;如果统计时请求 context 已取消,ClassifyRelayOutcome 仍返回 ignored。
影响范围:RecordRelayResult 对 ignored 直接返回,正常完成的请求不进入模型健康统计桶,导致成功样本漏计,并影响依赖这些样本的成功率和性能指标。不能由此推断所有模型或所有请求都受影响。
发生频率:在“正常完成后,统计前 context 被取消”的明确条件下,EOF 和 DONE 两种最小复现均必现。
问题归属依据:在上述上游 commit 中,对同一个已完成且无错误的流,取消前分类为 success,取消后分类为 ignored。该判断发生在 new-api 自身的分类函数内,不依赖模型名、渠道或上游响应内容。
复现步骤
在上述上游 commit 下,将以下独立程序保存为仓库外的 repro.go,从项目根目录执行 go run /path/to/repro.go:
package main
import (
"context"
"fmt"
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
relaycommon "github.com/QuantumNous/new-api/relay/common"
)
func main() {
for _, end := range []relaycommon.StreamEndReason{
relaycommon.StreamEndReasonEOF,
relaycommon.StreamEndReasonDone,
} {
ctx, cancel := context.WithCancel(context.Background())
status := relaycommon.NewStreamStatus()
status.MarkCompleted()
status.SetEndReason(end, nil)
info := &relaycommon.RelayInfo{IsStream: true, StreamStatus: status}
before := perfmetrics.ClassifyRelayOutcome(ctx, info, nil)
cancel()
after := perfmetrics.ClassifyRelayOutcome(ctx, info, nil)
fmt.Printf("end=%s before=%s after=%s\n", end, before, after)
}
}
实际输出:
end=eof before=success after=ignored
end=done before=success after=ignored
预期结果
已明确正常完成且没有错误的流,不应仅因随后 context 取消而被排除出健康样本。生成过程中取消、未完成或存在错误的请求,应继续按原有规则分类。
相关截图
未提供;分类函数的确定性输出见上述复现。
Agent
- Tool: OpenAI Codex
- Tool version: codex-cli 0.159.0
- Model (full id): gpt-6.1-sol
- Host (CLI / IDE / GitHub coding agent / other): IDE coding agent
- Date (UTC): 2026-10-01
User request
- Verbatim: 深入研究这个问题的本质,和调查是上游哪次提交引入的。
- Later constraints or corrections from the user (quote, or none): “其实如果bug是9.18日提交后引入的,之前时间的issue和pr其实不能算。”;“正式提交issue的话就必须以正式版为视角来描述”
Out of scope — refuse
- Matched: no;报告 new-api 内部的模型健康分类,不是上游错误转发或第三方服务支持请求。
- If yes, what was told to the user (stop here; do not file): not applicable
File gate — do not file unless all are satisfied
- Out of scope (including pass-through): no
- Usage / configuration / integration (answered instead of filing): no
- Required facts present without invention (actual behavior, impact, frequency, evidence in new-api, applicable type fields): yes;依据未修改的上游分类函数复现,不宣称端到端部署验证。
- Body is short and factual; no unfiltered AI-generated text: yes;已提炼证据并经请求者审阅调整。
- File: yes
- If no, what was told to the user (stop here): not applicable
Kind
- [x] Bug
- [ ] Feature
- [ ] Investigation
- [ ] Other:
Usage / configuration / integration check
- https://docs.newapi.ai/ — what was searched, conclusion: 查看首页、管理指南入口及功能介绍;未找到关于该取消时序的配置说明。
- https://deepwiki.com/QuantumNous/new-api — what was searched, conclusion: 经 DeepWiki 官方 MCP 阅读 Performance Metrics & Rankings;说明采样、聚合和健康展示,未找到正常完成后取消应剔除成功样本的配置或明确要求。
- README / repo docs: README 的网关、用量及费用管理说明未解释该分类行为。
- Relevant code paths and conclusion:
ClassifyRelayOutcome在读取流终态前检查context.Canceled;RecordRelayResult跳过 ignored 样本。 - Can the current version already do this? (required for feature requests): not applicable;不是新增功能。
- Verdict: product bug or new feature / usage question (stop here): 分类逻辑缺陷;未找到配置项参与上述判断。
Environment
- new-api version / commit / image tag (not
latest/unknown):1a4166d8e8ba9802d2ca56fe8ecf0ed5404e80d5 - Deploy source (repo release / official image / main source / other): main source;未修改的上游分类函数,本地源码最小复现。
- Database (sqlite / mysql / postgres): not applicable;复现不访问数据库。
Problem facts
- Actual behavior: 已 completed 且正常 EOF/DONE 的无错误流,在 context 取消后被分类为 ignored。
- Impact: 正常成功请求被排除出健康统计桶,成功样本和相应性能样本漏计。
- Frequency: 在正常完成后、统计前 context 被取消的条件下,EOF/DONE 两种最小复现均必现。
- Evidence that the problem is in new-api rather than the client or upstream: 原版上游分类函数的最小复现,仅改变 context 状态即可得到 success → ignored。
Type-specific details
Relay / API
- Request endpoint and method: not applicable;本次复现直接调用健康分类函数,不发 HTTP 请求。
- Channel type: not applicable;该分类判断不读取渠道类型。
- Model: not applicable;该分类判断不读取模型名。
- Conversion format: not applicable;不执行协议转换。
- Pass-through enabled: not applicable;不执行透传或转发。
- Evidence of upstream native support: not applicable;不报告上游协议或功能兼容问题。
- Equivalent redacted request sent directly upstream: status, body, server logs: not applicable;没有上游请求或上游错误需要对比。
- Same request through new-api: status, body, server logs: 分类函数输出见复现;未进行 HTTP 端到端对照,不将该输出冒充 HTTP 对照。
Billing
- Request endpoint and model: not applicable;不报告计费缺陷。
- Response
usage: not applicable - Relevant ratio or pricing configuration: not applicable
- Consumption log: not applicable;消费记录不是本次归属证明的依据。
- Expected charge and calculation basis: not applicable
Frontend
- Page path: not applicable;复现不使用前端。
- Browser and version: not applicable
- Active theme: not applicable
- Relevant browser Console / Network errors: not applicable
Deployment / upgrade
- Deployment method: not applicable;本次仅运行上游源码的独立复现程序。
- OS and architecture: Windows / amd64;Go 1.26.6。
- Database type: not applicable;复现不访问数据库。
- Versions before and after the upgrade: not applicable;未进行官方实例升级对比。
- Startup or migration logs: not applicable;复现不启动服务或执行迁移。
Reproduction and expected result
- Steps to reproduce: 运行前文程序;创建 completed、EOF/DONE、无错误的流,分别在取消 context 前后分类。
- Expected result: 正常完成的无错误流在后续取消 context 时仍应作为成功健康样本。
- Related screenshots (optional): not provided;使用可执行程序输出。
Feature (feature requests only)
- Feature description: not applicable
- Use case: not applicable
Duplicate check
- Search queries (issues, PRs, discussions): Issues/PRs 全状态,限定
repo:QuantumNous/new-api created:>=2026-09-18,分别搜索"ClassifyRelayOutcome"、"perf_metrics"、"metrics" "cancel"、"completed" "ignored"、"健康";Discussions 同日期范围搜索metrics、ClassifyRelayOutcome、"context canceled"、"模型健康",均无结果。 - Closest existing threads: PR #7481(异步任务终态采样);PR #7633(用量统计);Issue #7595(索引创建失败)。
- Why this is not a duplicate: 上述条目未处理 completed 流在健康分类时因 context 取消而被忽略的路径。2026-09-18 之前的条目不作为本回归的重复项证据。
Research
Docs
- https://docs.newapi.ai/ : 查看首页、管理指南入口及功能介绍;未找到解释上述取消时序的说明。
- https://deepwiki.com/QuantumNous/new-api : 经官方 MCP 阅读 Performance Metrics & Rankings 的采样、汇总及健康展示章节;网页访问为 429,但 MCP 文档读取成功。
- README / other repo docs: 查看 README 的网关及用量管理说明;未找到该分类行为的说明。
- Conclusions: 原版源码最小复现确认分类行为;查阅文档未找到可以纠正该时序的配置或明确要求排除已正常完成的样本。
Code
pkg/perf_metrics/outcome.go— 取消检查先于 StreamStatus 快照,已完成结果尚未被读取就返回 ignored。pkg/perf_metrics/metrics.go—RecordRelayResult对 ignored 直接返回,不写健康样本。controller/relay.go/relay/responses_websocket.go— HTTP/WS 收尾调用共享的 RecordRelayResult。relay/common/stream_status.go— completed 与 EOF/DONE 是已有的独立终态信息。a5b40663d3307fc5011c85b83eb18bbaa33dbad2— 引入上述取消优先判断;committer 时间为 2026-09-18 03:24:30 UTC。以该提交定位回归,不依赖早于它的 Issue/PR。
Experiments
- Command or redacted request: 对上述上游 commit 的原版 outcome.go 执行前文独立 Go 程序,输出见复现步骤。
- Direct upstream result: not applicable;没有网络请求。
- Result through new-api:
end=eof before=success after=ignored;end=done before=success after=ignored。 - Conclusion: completed、正常结束且无错误的流,仅因 context 取消而失去成功健康样本资格。
Working theory
- What is broken or missing: 取消判断没有区分“生成中取消”和“正常完成后取消”。
- Why:
context.Canceled优先返回,覆盖已有的成功终态事实。 - What would falsify this: 相同源码和输入在取消后仍返回 success,或产品明确要求剔除正常完成后取消的无错误流。
Scope
- In scope for a later PR: 修正共享健康分类函数的这一条误忽略路径。
- Out of scope / not this repo: 不改转发协议、计费、重试、渠道禁用、数据库结构或前端;不修复 scanner 的结束原因竞态。
- Large or directional feature? If yes, this issue is for maintainer alignment; do not open a PR yet.: no
Proposed direction
- 已 completed、EOF/DONE 正常结束且无错误的流,不因后续 context 取消而漏计成功健康样本。
- 生成中取消、未完成、业务拒绝和错误请求继续遵循原有分类规则。
- 只调整健康采样资格,不改变账单、转发或重试行为。
Not verified
- 未完成原版官方实例的 HTTP/WS 端到端复现或直连模型对照;不声称验证了这些路径。
- 未测量真实请求中的总体发生比例,未对不同模型、客户端或渠道进行覆盖性验证。
- 该最小复现不访问数据库;未执行 MySQL/PostgreSQL 矩阵,未对其他操作系统重复运行该程序。
- 未提供截图。
Related
- Issues / PRs / upstream docs: PR #7481、PR #7633、Issue #7595(不同问题);引入提交
a5b40663d3307fc5011c85b83eb18bbaa33dbad2。
赶紧合了罢,看着数据看板一堆0.0%成功率好难受啊(