mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
feat: auto-retry upstream 400 on stale reasoning references
When Zen returns 400 such as "Referenced reasoning item 'rs_...' was not found or has expired", replay once without the Responses reasoning input items and previous_response_id under a fresh upstream session instead of failing the client request. Other 400s pass through unchanged. Adds unit tests for the classifier and payload stripping.
This commit is contained in:
1 parent
3be4b393c0
commit
462a6c8e36
2 files changed
+167
No files matched your search
+103
@@ -371,6 +371,109 @@ func (g *Gateway) prepareRouteBodies(from Protocol, route modelRoute, input map[
|
||||
}
|
||||
|
||||
func (g *Gateway) doUpstream(ctx context.Context, route modelRoute, bodies map[Tier][]byte, ids requestIDs) (*http.Response, modelRoute, error) {
|
||||
resp, effectiveRoute, err := g.doUpstreamTiers(ctx, route, bodies, ids)
|
||||
if err != nil || resp == nil || resp.StatusCode != http.StatusBadRequest {
|
||||
return resp, effectiveRoute, err
|
||||
}
|
||||
errBody, readErr := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if readErr != nil {
|
||||
return resp, effectiveRoute, nil
|
||||
}
|
||||
// Restore the body so downstream error handling still sees the original
|
||||
// payload when no retry happens below.
|
||||
resp.Body = io.NopCloser(bytes.NewReader(errBody))
|
||||
if !isStaleReasoningReference(errBody) {
|
||||
return resp, effectiveRoute, nil
|
||||
}
|
||||
stripped, changed := stripStaleReasoningInputs(effectiveRoute, bodies)
|
||||
if !changed {
|
||||
return resp, effectiveRoute, nil
|
||||
}
|
||||
// The referenced reasoning items belong to an upstream chain this session
|
||||
// can no longer address (e.g. an interrupted stream). Replay once without
|
||||
// them under a fresh upstream session instead of failing the client
|
||||
// request outright.
|
||||
drainAndClose(resp.Body)
|
||||
retryIDs := ids
|
||||
retryIDs.Session = randomID("ses", 12)
|
||||
g.logger.Info("retrying upstream without stale reasoning references", "component", "upstream", "event", "reasoning_reference_retry", "request_id", ids.Request, "model", route.ID, "tier", effectiveRoute.Tier)
|
||||
retryResp, retryRoute, retryErr := g.doUpstreamTiers(ctx, effectiveRoute, stripped, retryIDs)
|
||||
if retryErr != nil || retryResp == nil {
|
||||
g.logger.Warn("reasoning reference retry failed; returning original error", "component", "upstream", "event", "reasoning_reference_retry_failed", "request_id", ids.Request, "model", route.ID, "tier", effectiveRoute.Tier, "error", retryErr)
|
||||
fallback := *resp
|
||||
fallback.Body = io.NopCloser(bytes.NewReader(errBody))
|
||||
return &fallback, effectiveRoute, nil
|
||||
}
|
||||
return retryResp, retryRoute, retryErr
|
||||
}
|
||||
|
||||
// isStaleReasoningReference reports whether an upstream 400 body describes a
|
||||
// reasoning reference the server no longer recognizes, such as
|
||||
// "Referenced reasoning item 'rs_...' was not found or has expired".
|
||||
func isStaleReasoningReference(body []byte) bool {
|
||||
text := strings.ToLower(string(body))
|
||||
if !strings.Contains(text, "reasoning") {
|
||||
return false
|
||||
}
|
||||
for _, marker := range []string{"not found", "expir", "unknown", "no longer", "does not exist", "invalid reference"} {
|
||||
if strings.Contains(text, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// stripStaleReasoningInputs removes server-issued reasoning references from
|
||||
// Responses-protocol upstream payloads: replayed "reasoning" input items and
|
||||
// any previous_response_id chain link. Other tiers/protocols are passed
|
||||
// through untouched. It reports whether any payload actually changed.
|
||||
func stripStaleReasoningInputs(route modelRoute, bodies map[Tier][]byte) (map[Tier][]byte, bool) {
|
||||
changed := false
|
||||
out := make(map[Tier][]byte, len(bodies))
|
||||
for tier, body := range bodies {
|
||||
if len(body) == 0 || route.ProtocolFor(tier) != ProtocolResponses {
|
||||
out[tier] = body
|
||||
continue
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
out[tier] = body
|
||||
continue
|
||||
}
|
||||
tierChanged := false
|
||||
if _, ok := payload["previous_response_id"]; ok {
|
||||
delete(payload, "previous_response_id")
|
||||
tierChanged = true
|
||||
}
|
||||
if raw, ok := payload["input"].([]any); ok {
|
||||
kept := make([]any, 0, len(raw))
|
||||
for _, item := range raw {
|
||||
if m, ok := item.(map[string]any); ok && stringAt(m, "type") == "reasoning" {
|
||||
tierChanged = true
|
||||
continue
|
||||
}
|
||||
kept = append(kept, item)
|
||||
}
|
||||
if tierChanged {
|
||||
payload["input"] = kept
|
||||
}
|
||||
}
|
||||
if !tierChanged {
|
||||
out[tier] = body
|
||||
continue
|
||||
}
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
out[tier] = body
|
||||
continue
|
||||
}
|
||||
out[tier] = encoded
|
||||
changed = true
|
||||
}
|
||||
return out, changed
|
||||
}
|
||||
|
||||
func (g *Gateway) doUpstreamTiers(ctx context.Context, route modelRoute, bodies map[Tier][]byte, ids requestIDs) (*http.Response, modelRoute, error) {
|
||||
var lastResponse *http.Response
|
||||
var lastErr error
|
||||
effectiveRoute := route
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsStaleReasoningReference(t *testing.T) {
|
||||
cases := []struct {
|
||||
body string
|
||||
want bool
|
||||
}{
|
||||
{`{"error":{"message":"Upstream request failed: [invalid_request_error] Referenced reasoning item 'rs_7145a5ca29c1537d081efdb1' was not found or has expired.", "type":"invalid_request_error"}}`, true},
|
||||
{`{"error":{"message":"reasoning item rs_abc has expired", "type":"invalid_request_error"}}`, true},
|
||||
{`{"error":{"message":"Model is unavailable.", "type":"upstream_error"}}`, false},
|
||||
{`{"error":{"message":"invalid_request_error: missing field 'model'", "type":"invalid_request_error"}}`, false},
|
||||
{`not json at all`, false},
|
||||
{``, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := isStaleReasoningReference([]byte(tc.body)); got != tc.want {
|
||||
t.Errorf("isStaleReasoningReference(%q) = %v, want %v", tc.body, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripStaleReasoningInputs(t *testing.T) {
|
||||
route := modelRoute{
|
||||
ID: "test-model",
|
||||
Tier: TierZen,
|
||||
Protocol: ProtocolResponses,
|
||||
Protocols: map[Tier]Protocol{TierZen: ProtocolResponses, TierGo: ProtocolChat},
|
||||
}
|
||||
responsesBody := []byte(`{"model":"test-model","previous_response_id":"resp_123","input":[{"type":"reasoning","id":"rs_old","summary":[]},{"type":"message","role":"user","content":"hi"}]}`)
|
||||
chatBody := []byte(`{"model":"test-model","messages":[{"role":"user","content":"hi"}]}`)
|
||||
bodies := map[Tier][]byte{TierZen: responsesBody, TierGo: chatBody}
|
||||
|
||||
stripped, changed := stripStaleReasoningInputs(route, bodies)
|
||||
if !changed {
|
||||
t.Fatal("expected changed=true when reasoning references exist")
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(stripped[TierZen], &payload); err != nil {
|
||||
t.Fatalf("stripped zen body is not valid JSON: %v", err)
|
||||
}
|
||||
if _, ok := payload["previous_response_id"]; ok {
|
||||
t.Error("previous_response_id was not removed")
|
||||
}
|
||||
input, _ := payload["input"].([]any)
|
||||
if len(input) != 1 {
|
||||
t.Fatalf("expected 1 input item left, got %d", len(input))
|
||||
}
|
||||
if m, _ := input[0].(map[string]any); stringAt(m, "type") != "message" {
|
||||
t.Errorf("unexpected remaining input item: %v", input[0])
|
||||
}
|
||||
if string(stripped[TierGo]) != string(chatBody) {
|
||||
t.Error("non-Responses tier body must pass through untouched")
|
||||
}
|
||||
|
||||
cleanBodies := map[Tier][]byte{TierZen: []byte(`{"model":"m","input":[{"type":"message","role":"user","content":"hi"}]}`)}
|
||||
if _, changed := stripStaleReasoningInputs(route, cleanBodies); changed {
|
||||
t.Error("expected changed=false when no reasoning references exist")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user