From 57a990fafa327decc9df7ab87b508023d7fbd273 Mon Sep 17 00:00:00 2001 From: dzolv Date: Thu, 3 Dec 2026 11:05:52 +0800 Subject: [PATCH] fix(openai): keep HTTP handler missing-call-id validation and scope handshake window_id --- .../service/openai_tool_continuation.go | 14 +--------- .../service/openai_ws_forwarder_ingress.go | 20 +++++++++++--- .../service/openai_ws_forwarder_payload.go | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/backend/internal/service/openai_tool_continuation.go b/backend/internal/service/openai_tool_continuation.go index 4e4f16baf6..c85e731947 100644 --- a/backend/internal/service/openai_tool_continuation.go +++ b/backend/internal/service/openai_tool_continuation.go @@ -187,11 +187,8 @@ func ValidateFunctionCallOutputContextBytes(body []byte) FunctionCallOutputValid itemType := item.Get("type").String() switch { case isCodexToolCallOutputItemType(itemType): - callID := strings.TrimSpace(item.Get("call_id").String()) - if isStandaloneNamedFunctionCallOutput(itemType, callID, item.Get("name").String()) { - return true - } result.HasFunctionCallOutput = true + callID := strings.TrimSpace(item.Get("call_id").String()) if callID == "" { result.HasFunctionCallOutputMissingCallID = true return true @@ -341,11 +338,6 @@ func ValidateFunctionCallOutputContext(reqBody map[string]any) FunctionCallOutpu itemType, _ := itemMap["type"].(string) switch { case isCodexToolCallOutputItemType(itemType): - callID, _ := itemMap["call_id"].(string) - name, _ := itemMap["name"].(string) - if isStandaloneNamedFunctionCallOutput(itemType, callID, name) { - continue - } result.HasFunctionCallOutput = true case isCodexToolCallContextItemType(itemType): callID, _ := itemMap["call_id"].(string) @@ -374,10 +366,6 @@ func ValidateFunctionCallOutputContext(reqBody map[string]any) FunctionCallOutpu case isCodexToolCallOutputItemType(itemType): callID, _ := itemMap["call_id"].(string) callID = strings.TrimSpace(callID) - name, _ := itemMap["name"].(string) - if isStandaloneNamedFunctionCallOutput(itemType, callID, name) { - continue - } if callID == "" { result.HasFunctionCallOutputMissingCallID = true continue diff --git a/backend/internal/service/openai_ws_forwarder_ingress.go b/backend/internal/service/openai_ws_forwarder_ingress.go index 909100ccec..e6797ef2a8 100644 --- a/backend/internal/service/openai_ws_forwarder_ingress.go +++ b/backend/internal/service/openai_ws_forwarder_ingress.go @@ -203,10 +203,10 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( } ingressSessionOriginalModel := "" activeTurnMetadata := "" - initialHandshakeWindowID := "" + activeWindowID := "" if c != nil { activeTurnMetadata = strings.TrimSpace(c.GetHeader(openAIWSTurnMetadataHeader)) - initialHandshakeWindowID = strings.TrimSpace(c.GetHeader("x-codex-window-id")) + activeWindowID = strings.TrimSpace(c.GetHeader("x-codex-window-id")) } applyPayloadMutation := func(current []byte, path string, value any) ([]byte, error) { @@ -225,6 +225,8 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( payload[path] = value case "client_metadata." + openAIWSTurnMetadataHeader: setOpenAIWSTurnMetadata(payload, fmt.Sprintf("%v", value)) + case "client_metadata.x-codex-window-id": + setOpenAIWSWindowIDMetadata(payload, fmt.Sprintf("%v", value)) default: return nil, err } @@ -328,7 +330,11 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( frameWindowID := strings.TrimSpace(gjson.GetBytes(normalized, "client_metadata.x-codex-window-id").String()) if frameTurnMetadata != "" { activeTurnMetadata = frameTurnMetadata + if w := strings.TrimSpace(gjson.Get(frameTurnMetadata, "window_id").String()); w != "" { + activeWindowID = w + } } else if frameWindowID != "" { + activeWindowID = frameWindowID if activeTurnMetadata != "" && strings.TrimSpace(gjson.Get(activeTurnMetadata, "window_id").String()) != frameWindowID { activeTurnMetadata = "" } @@ -338,6 +344,12 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( return openAIWSClientPayload{}, NewOpenAIWSClientCloseError(coderws.StatusPolicyViolation, "invalid websocket request payload", setErr) } normalized = next + } else if activeWindowID != "" { + next, setErr := applyPayloadMutation(normalized, "client_metadata.x-codex-window-id", activeWindowID) + if setErr != nil { + return openAIWSClientPayload{}, NewOpenAIWSClientCloseError(coderws.StatusPolicyViolation, "invalid websocket request payload", setErr) + } + normalized = next } accountIdentitySourceRaw := append([]byte(nil), normalized...) accountScopedPayload, accountScoped, scopeErr := applyCodexAccountIdentityClientMetadataRaw(normalized, codexAccountIdentitySource(c, account), getAPIKeyIDFromContext(c), codexClientSessionID(c)) @@ -608,7 +620,7 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( } defer history.close() historyLost := false - lastBridgeWindowID := initialHandshakeWindowID + lastBridgeWindowID := "" var bridgeReplayInput []json.RawMessage bridgeReplayInputExists := false var bridgeAccountFailoverInput []json.RawMessage @@ -1496,7 +1508,7 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient( turnPrevRecoveryTried := false lastTurnFinishedAt := time.Time{} lastTurnResponseID := "" - lastTurnWindowID := initialHandshakeWindowID + lastTurnWindowID := "" lastTurnPayload := []byte(nil) var lastTurnStrictState *openAIWSIngressPreviousTurnStrictState lastTurnReplayInput := []json.RawMessage(nil) diff --git a/backend/internal/service/openai_ws_forwarder_payload.go b/backend/internal/service/openai_ws_forwarder_payload.go index 21711a9f2e..f563f86c77 100644 --- a/backend/internal/service/openai_ws_forwarder_payload.go +++ b/backend/internal/service/openai_ws_forwarder_payload.go @@ -247,6 +247,32 @@ func setOpenAIWSTurnMetadata(payload map[string]any, turnMetadata string) { } } +func setOpenAIWSWindowIDMetadata(payload map[string]any, windowID string) { + if len(payload) == 0 { + return + } + id := strings.TrimSpace(windowID) + if id == "" { + return + } + switch existing := payload["client_metadata"].(type) { + case map[string]any: + existing["x-codex-window-id"] = id + payload["client_metadata"] = existing + case map[string]string: + next := make(map[string]any, len(existing)+1) + for k, v := range existing { + next[k] = v + } + next["x-codex-window-id"] = id + payload["client_metadata"] = next + default: + payload["client_metadata"] = map[string]any{ + "x-codex-window-id": id, + } + } +} + func (s *OpenAIGatewayService) isOpenAIWSStoreRecoveryAllowed(account *Account) bool { if account != nil && account.IsOpenAIWSAllowStoreRecoveryEnabled() { return true