Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/scripts/exclude-later-http2-red-fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ candidate="${3:-$(git -C "$tree" rev-parse HEAD)}"
# The same historical assembly must not retain tests for an absent QQ package.
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash "$script_dir/prepare-mature-red-fixtures.sh" "$baseline" "$tree" "$candidate"
bash "$script_dir/prepare-free-fast-red-fixtures.sh" "$baseline" "$tree" "$candidate"
# Selective Issue715 overlays retain HTTP2 production/support, so keep its tests.
if test -f "$tree/backend/internal/service/http2_gateway_test_support.go"; then
exit 0
Expand Down
31 changes: 31 additions & 0 deletions .github/scripts/http-recovery-native-controls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Require the same named business failures before the fix and completion after."""
import importlib.util
import json
from pathlib import Path
import sys

phase, code, filename = sys.argv[1:]
spec = importlib.util.spec_from_file_location('clean', Path(__file__).with_name('assert-readable-continuation.py'))
clean = importlib.util.module_from_spec(spec)
spec.loader.exec_module(clean)
raw = Path(filename).read_text()
clean.check_clean(raw)
events = [json.loads(line) for line in raw.splitlines() if line.startswith('{')]
root = 'TestOpenAI409NativeControlsContinuation'
leaves = {f'{root}/{account}/stream={stream}/{shape}'
for account in ('oauth', 'apikey') for stream in ('true', 'false')
for shape in ('web_search', 'image_aliases', 'image_bridge', 'image_strip', 'combined')}
terminal = {e['Test']: e['Action'] for e in events
if e.get('Test') and e.get('Action') in ('pass', 'fail', 'skip')}
assert leaves <= terminal.keys(), 'missing named scenarios'
if phase == 'GREEN':
assert int(code) == 0 and all(value == 'pass' for value in terminal.values()), terminal
else:
assert phase == 'RED' and int(code) == 1, (phase, code)
for name in leaves:
expected = 'pass' if '/apikey/' in name and name.endswith('/web_search') else 'fail'
assert terminal[name] == expected, (name, terminal[name], expected)
if expected == 'fail':
output = ''.join(e.get('Output', '') for e in events if e.get('Test') == name)
assert 'HTTP_REBUILD_B_DISPATCH_MISSING' in output and 'encrypted_agent_recovery_wire_changed' in output, name
print(f'{phase}: verified {len(leaves)} named native-control scenarios')
26 changes: 26 additions & 0 deletions .github/scripts/prepare-free-fast-red-fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Isolate only the later independent Free Fast service tests when an exact
# historical production overlay lacks their private snapshot implementation.
set -euo pipefail
baseline="$1"
tree="${2:-.}"
candidate="${3:-HEAD}"
if test -f "$tree/backend/internal/service/customer_cost_details.go"; then exit 0; fi
if git -C "$tree" cat-file -e "$baseline:backend/internal/service/customer_cost_details.go" 2>/dev/null; then
echo 'baseline snapshot implementation unexpectedly absent' >&2; exit 1
fi
contract=backend/internal/service/billing_service.go
test "$(git -C "$tree" hash-object "$contract")" = "$(git -C "$tree" rev-parse "$baseline:$contract")"
target=backend/internal/service/free_fast_customer_cost_test.go
if ! git -C "$tree" cat-file -e "$candidate:$target" 2>/dev/null; then exit 0; fi
if git -C "$tree" cat-file -e "$baseline:$target" 2>/dev/null; then
echo 'refusing to omit a baseline Free Fast fixture' >&2; exit 1
fi
expected=$(git -C "$tree" rev-parse "$candidate:$target")
if test -f "$tree/$target"; then
test "$(git -C "$tree" hash-object "$target")" = "$expected"
rm -- "$tree/$target"
else
test "$(git -C "$tree" diff --name-only --diff-filter=D "$candidate" -- "$target")" = "$target"
fi
printf 'independent free-fast fixture baseline=%s candidate=%s path=%s blob=%s actual=absent\n' "$baseline" "$candidate" "$target" "$expected"
72 changes: 72 additions & 0 deletions .github/workflows/http-recovery-native-controls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: HTTP recovery native controls
on:
pull_request:
paths:
- 'backend/internal/service/openai*'
- 'backend/internal/handler/openai_409*'
- '.github/workflows/http-recovery-native-controls.yml'
- '.github/scripts/http-recovery-native-controls.py'
permissions:
contents: read
jobs:
behavior:
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
phase: [RED, GREEN]
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- uses: actions/setup-go@v6
with:
go-version-file: backend/go.mod
cache-dependency-path: backend/go.sum
- name: Install isolated Redis
run: |
bash .github/scripts/ci-ubuntu-mirrors.sh
if ! command -v redis-server >/dev/null; then
sudo apt-get update
sudo apt-get install -y --no-install-recommends redis-server
fi
- name: Restore exact pre-fix production implementation for RED
if: matrix.phase == 'RED'
shell: bash
run: |
set -euo pipefail
baseline=c115bef917eae22fc2f5a4d0ba6ac6cbe600e8e4
git diff --name-only "$baseline" HEAD -- backend | while IFS= read -r target; do
case "$target" in
*_test.go) continue ;;
*.go)
if git cat-file -e "$baseline:$target" 2>/dev/null; then
git show "$baseline:$target" > "$target"
test "$(git hash-object "$target")" = "$(git rev-parse "$baseline:$target")"
else
rm -- "$target"
fi ;;
esac
done
- name: Authenticated HTTP task continuation
env:
PHASE: ${{ matrix.phase }}
shell: bash
run: |
set -euo pipefail
set +e
(cd backend && go test -race -json -tags=unit,httpprojection ./internal/handler -run '^TestOpenAI409NativeControlsContinuation$' -count=1 -timeout=8m) > "$RUNNER_TEMP/native-controls.jsonl" 2>&1
result=$?
set -e
python3 .github/scripts/http-recovery-native-controls.py "$PHASE" "$result" "$RUNNER_TEMP/native-controls.jsonl"
- name: Trusted source and late mutation guards
if: matrix.phase == 'GREEN'
working-directory: backend
run: go test -race -tags=unit,httpprojection ./internal/service -run '^TestOpenAI409NativeControlsIntegrity$' -count=1 -v -timeout=3m
- uses: actions/upload-artifact@v7
if: always()
with:
name: http-native-controls-${{ matrix.phase }}-${{ github.event.pull_request.head.sha }}
path: ${{ runner.temp }}/native-controls.jsonl
4 changes: 4 additions & 0 deletions backend/internal/handler/openai_409_http_rebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ type httpRebuildFixtureOptions struct {
accounts func([]service.Account)
gateway func(*OpenAIGatewayHandler, []service.Account, service.HTTPUpstream, service.GatewayCache, *service.SettingService, service.UsageLogRepository)
settings service.SettingRepository
apiKey func(*service.APIKey)
}

func runHTTPRebuildFixture(t *testing.T, accountType string, stream bool, pointer, outcome, boundary, mutation string, options ...httpRebuildFixtureOptions) {
Expand Down Expand Up @@ -630,6 +631,9 @@ func runHTTPRebuildFixture(t *testing.T, accountType string, stream bool, pointe
option.gateway(h, accounts, transport, repository.NewGatewayCache(clients()), settings, usage)
}
template, _ := newIssue715Context(t, "{}")
if option.apiKey != nil {
option.apiKey(template.Keys[string(middleware.ContextKeyAPIKey)].(*service.APIKey))
}
done := make(chan struct{}, 1)
var sessions []string
pendingBeforeWrite := false
Expand Down
110 changes: 110 additions & 0 deletions backend/internal/handler/openai_409_native_controls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//go:build unit && httpprojection

package handler

import (
"fmt"
"net/http"
"testing"

"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// Real authentication, scheduler, native builders, Redis delivery fences and
// a loopback provider. A emits a patch once, becomes unavailable, then B must
// reuse the result, verify once and finish the same client task.
func TestOpenAI409NativeControlsContinuation(t *testing.T) {
for _, account := range []string{service.AccountTypeOAuth, service.AccountTypeAPIKey} {
for _, stream := range []bool{false, true} {
for _, shape := range []string{"web_search", "image_aliases", "image_bridge", "image_strip", "combined"} {
t.Run(fmt.Sprintf("%s/stream=%t/%s", account, stream, shape), func(t *testing.T) {
search := shape == "web_search" || shape == "combined"
bridge := shape == "image_bridge" || shape == "combined"
explicitImage := shape == "image_aliases" || shape == "image_strip" || shape == "combined"
option := httpRebuildFixtureOptions{
apiKey: func(key *service.APIKey) { key.Group.AllowImageGeneration = true },
headers: func(r *http.Request) {
r.Header.Set("User-Agent", "codex_cli_rs/0.110.0")
r.Header.Set("originator", "codex_cli_rs")
},
accounts: func(accounts []service.Account) {
for i := range accounts {
accounts[i].Extra = map[string]any{"codex_image_generation_bridge": bridge}
if shape == "image_strip" {
accounts[i].Extra["codex_image_generation_explicit_tool_policy"] = "strip"
}
}
},
request: func(body map[string]any, continuation bool) {
tools := body["tools"].([]any)
if search {
tools = append(tools, map[string]any{"type": "web_search", "external_web_access": false, "search_context_size": "low"})
}
if explicitImage {
tools = append(tools, map[string]any{"type": "image_generation", "model": "gpt-image-2", "format": "jpeg", "compression": 73, "input_fidelity": "high"})
}
body["tools"] = tools
},
toolsAvailable: func(body []byte) bool {
// The provider validates the original executable function
// declarations separately from hosted compatibility tools.
tools := gjson.GetBytes(body, "tools").Array()
for i := len(tools) - 1; i >= 2; i-- {
kind := tools[i].Get("type").String()
if kind != "web_search" && kind != "image_generation" {
return false
}
var err error
body, err = sjson.DeleteBytes(body, fmt.Sprintf("tools.%d", i))
if err != nil {
return false
}
}
return httpRebuildToolsAvailable(body)
},
provider: func(t *testing.T, body []byte, _ http.Header, n int) {
searchTool := gjson.GetBytes(body, `tools.#(type==web_search)`)
require.Equal(t, search, searchTool.Exists())
if search {
require.Equal(t, "low", searchTool.Get("search_context_size").String())
require.Equal(t, account == service.AccountTypeAPIKey, searchTool.Get("external_web_access").Exists())
if account == service.AccountTypeAPIKey {
require.Equal(t, gjson.False, searchTool.Get("external_web_access").Type)
}
}
imageTool := gjson.GetBytes(body, `tools.#(type==image_generation)`)
wantImage := (explicitImage || bridge) && shape != "image_strip"
require.Equal(t, wantImage, imageTool.Exists())
wantTools := 2
if search {
wantTools++
}
if wantImage {
wantTools++
}
require.Len(t, gjson.GetBytes(body, "tools").Array(), wantTools)
if wantImage && explicitImage {
require.Equal(t, "jpeg", imageTool.Get("output_format").String())
require.Equal(t, int64(73), imageTool.Get("output_compression").Int())
for _, field := range []string{"format", "compression", "input_fidelity"} {
require.False(t, imageTool.Get(field).Exists())
}
}
if bridge {
require.Equal(t, "auto", gjson.GetBytes(body, "tool_choice").String())
require.NotEmpty(t, gjson.GetBytes(body, "instructions").String())
}
if n > 1 {
require.NotContains(t, string(body), "fixture-opaque-A")
}
},
}
runHTTPRebuildFixture(t, account, stream, "previous", "completed", "issue854_auth", "", option)
})
}
}
}
}
65 changes: 65 additions & 0 deletions backend/internal/service/openai_409_native_controls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package service

import (
"context"
"encoding/json"

"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)

// projectOpenAI409ImageControls applies the native builder's explicit image
// policy to a verified readable source BEFORE freezing its representation.
// It never accepts the builder's final body as a new trusted snapshot. The
// ordinary builder still checks permission/model validity and applies these
// idempotent transforms; any other late task or controls mutation is refused.
func (s *OpenAIGatewayService) projectOpenAI409ImageControls(ctx context.Context, c *gin.Context, account *Account, body []byte) ([]byte, error) {
isCodexCLI := openai.IsCodexOfficialClientByHeaders(c.GetHeader("User-Agent"), c.GetHeader("originator")) ||
(s.cfg != nil && s.cfg.Gateway.ForceCodexCLI)
strip := isCodexCLI && account.CodexImageGenerationExplicitToolPolicy() == codexImageGenerationExplicitToolPolicyStrip
apiKey := getAPIKeyFromContext(c)
allowed := GroupAllowsImageGenerationLatest(c.Request.Context(), apiKeyGroup(apiKey))
bridge := isCodexCLI && !isOpenAIResponsesLiteHeader(c.GetHeader(responsesLiteHeader)) &&
allowed && !strip && s.isCodexImageGenerationBridgeEnabled(ctx, account, apiKey)
requestedModel := gjson.GetBytes(body, "model").String()
_, upstreamModel := resolveOpenAIForwardMappedModels(account, requestedModel, false)
normalize := allowed && (bridge || isOpenAIImageGenerationModel(requestedModel) ||
openAIRequestBodyImageGenerationToolNeedsNormalization(body) || isOpenAIImageGenerationModel(upstreamModel))
spark := isCodexSparkModel(upstreamModel) && openAIRequestBodyHasImageGenerationDeclaration(body)
if !strip && !normalize && !spark {
return body, nil
}
var decoded map[string]any
if err := decodeOpenAIJSONUseNumber(body, &decoded); err != nil {
return nil, err
}
// Model routing is independent of input/controls. Evaluate the same target
// model as Forward, then retain the caller's model for normal routing and
// billing; projecting a tool cannot silently select a different account.
originalModel, hadModel := decoded["model"]
decoded["model"] = upstreamModel
if strip {
stripOpenAIImageGenerationTools(decoded)
}
if normalize {
if bridge {
ensureOpenAIResponsesImageGenerationTool(decoded)
ensureOpenAIResponsesImageGenerationToolChoiceAuto(decoded)
}
normalizeOpenAIResponsesImageGenerationTools(decoded)
normalizeOpenAIResponsesImageOnlyModel(decoded)
if bridge {
applyCodexImageGenerationBridgeInstructions(decoded)
}
}
if spark {
stripCodexSparkImageGenerationTools(decoded)
}
if hadModel {
decoded["model"] = originalModel
} else {
delete(decoded, "model")
}
return json.Marshal(decoded)
}
Loading
Loading