fix(openai): also match bare /responses route in handleStreamingAwareError

The first revision compared GetInboundEndpoint(c) against EndpointResponses
("/v1/responses"). NormalizeInboundEndpoint only recognizes paths that
contain the literal "/v1/responses" substring, but the project actually
registers six /responses routes — three of which (top-level
r.POST("/responses", ...) and codexDirect's "/backend-api/codex/responses")
have FullPath values without the "/v1" prefix and therefore fall through
to the default branch.

Codex CLI users targeting the bare /responses route at the production
deployment (observed 2026-05-24 ~11:05 UTC, user 16) never reached the
new writeResponsesFailedSSE path: the endpoint check was false, the
legacy `event: error` frame fired, and the strict SDK kept reporting
"stream closed before response.completed".

Replace the strict equality check with inboundIsResponses(c), which
uses suffix detection on FullPath (falling back to URL.Path when
FullPath is empty in test fixtures) and covers all six route variants:

  /v1/responses[/...]
  /responses[/...]
  /backend-api/codex/responses[/...]

Add test table covering all routes plus negative cases.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Jamie Wong
2026-05-24 19:32:08 +08:00
co-authored by Claude Opus 4.7
parent 5e5c2062bf
commit cff2f291be
4 changed files with 83 additions and 2 deletions
@@ -61,6 +61,36 @@ func writeResponsesFailedSSE(c *gin.Context, errType, message string) bool {
return true
}
// inboundIsResponses 判断当前请求是否落在任何 /responses 路由上。
//
// 不能直接用 GetInboundEndpoint(c) == EndpointResponses 比较,因为
// NormalizeInboundEndpoint 只识别包含 "/v1/responses" 子串的路径;
// 项目里实际注册了多组路由(gateway_v1、top-level bare、codex direct),
// 其中 r.POST("/responses", ...) 和 codexDirect.POST("/responses", ...)
// 的 c.FullPath() 不含 "/v1/" 前缀,会被归一化为原始路径,
// 导致协议合规终止事件没法发出去。
//
// 这里用 FullPath 的后缀判断,覆盖所有变体:
// - /v1/responses
// - /v1/responses/compact
// - /responses
// - /responses/compact
// - /backend-api/codex/responses
// - /backend-api/codex/responses/compact
func inboundIsResponses(c *gin.Context) bool {
if c == nil {
return false
}
p := strings.TrimRight(c.FullPath(), "/")
if p == "" && c.Request != nil && c.Request.URL != nil {
p = strings.TrimRight(c.Request.URL.Path, "/")
}
if p == "" {
return false
}
return strings.HasSuffix(p, "/responses") || strings.Contains(p, "/responses/")
}
// synthesizeResponseID 为合成的 response.failed 事件生成一个稳定的 id。
// 优先复用 server 端生成的 request_id(存在 request.Context 里,由 request_logger 写入),
// 以便客户端报错能与 server 日志关联;缺失时回退 uuid。