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
32 changes: 32 additions & 0 deletions .github/scripts/prepare-replay-red-fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,40 @@ if ! git -C "$tree" grep -q 'func (a \*Account) IsExcelBPSSearchEnabled' "$basel
exit 1
fi
exclude_absent_fixture backend/internal/service/openai_excel_bps_search_test.go "$contract"
# PR872 added independent search diagnostics/race fixtures which also use
# that absent API and the excluded search fixture helper. Keep them in every
# candidate GREEN; only isolate proven post-baseline files in this old RED.
for target in backend/internal/service/openai_excel_bps_search_diagnostics_test.go backend/internal/service/openai_excel_bps_search_race_test.go; do
if git -C "$tree" cat-file -e "$candidate:$target" 2>/dev/null; then
exclude_absent_fixture "$target" "$contract"
fi
done
fi

# Later mixed-owner proofs/diagnostics share the restored recovery struct.
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash "$script_dir/prepare-ws-owner-red-fixtures.sh" "$baseline" "$tree" "$candidate"

# PR874 creation-only OAuth policy is absent in historical recovery sources.
# Keep the selected recovery tests untouched and all candidate GREEN fixtures;
# adapt only these unrelated service fixtures to the exact old Account API.
contract=backend/internal/service/account.go
if ! git -C "$tree" grep -q 'InitialAPIKeyDisposition' "$baseline" -- "$contract"; then
target=backend/internal/service/oauth_apikey_disposition_test.go
if git -C "$tree" cat-file -e "$candidate:$target" 2>/dev/null; then
exclude_absent_fixture "$target" "$contract"
fi
target=backend/internal/service/openai_plugin_account_directory_test.go
expected_contract=$(git -C "$tree" rev-parse "$baseline:$contract")
test "$(git -C "$tree" hash-object "$contract")" = "$expected_contract"
candidate_blob=$(git -C "$tree" rev-parse "$candidate:$target")
test "$(git -C "$tree" hash-object "$target")" = "$candidate_blob"
if git -C "$tree" cat-file -e "$baseline:$target" 2>/dev/null; then
expected=$(git -C "$tree" rev-parse "$baseline:$target")
git -C "$tree" show "$baseline:$target" > "$tree/$target"
test "$(git -C "$tree" hash-object "$target")" = "$expected"
printf 'historical plugin fixture path=%s candidate_blob=%s baseline_blob=%s contract_blob=%s\n' "$target" "$candidate_blob" "$expected" "$expected_contract"
else
exclude_absent_fixture "$target" "$contract"
fi
fi
1 change: 1 addition & 0 deletions .github/workflows/encrypted-history-400.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
git rev-parse HEAD
go test -race -json -p 2 -tags=unit ./internal/service ./internal/handler ./internal/handler/admin -run '^Test(OpenAI400|OpenAI409|OmitOpenAIHistorical|ForwardEncryptedContentSSERetry|UpdateSettingsOpenAI400|UpdateSettingsOpenAI409)' -count=1 -timeout=5m
settings:
# The pinned Playwright 1.56.1 browser packages support Ubuntu 24.04.
runs-on: ubuntu-24.04
timeout-minutes: 15
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/free-fast-customer-cost-details.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jobs:
frontend:
env:
NODE_OPTIONS: --max-old-space-size=4096
# Playwright 1.56.1 has no Chromium package for Ubuntu 26.04.
runs-on: ubuntu-24.04
timeout-minutes: 25
steps:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/future-shadow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ jobs:
while IFS= read -r target; do
case "$target" in *_test.go) continue ;; esac
if git cat-file -e "$BASELINE_SHA:$target" 2>/dev/null; then
mkdir -p -- "$(dirname -- "$target")"
git show "$BASELINE_SHA:$target" > "$target"
git add -N -- "$target"
test "$(git hash-object "$target")" = "$(git rev-parse "$BASELINE_SHA:$target")"
else
rm -- "$target"
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/http-task-rebuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ jobs:
while IFS= read -r target; do
case "$target" in *_test.go) continue ;; esac
if git cat-file -e "$BASELINE_SHA:$target" 2>/dev/null; then
mkdir -p -- "$(dirname -- "$target")"
git show "$BASELINE_SHA:$target" > "$target"
git add -N -- "$target"
expected=$(git rev-parse "$BASELINE_SHA:$target")
test "$(git hash-object "$target")" = "$expected"
echo "$target baseline=$expected" >> "$RUNNER_TEMP/http-task-evidence/red-blobs.txt"
Expand Down Expand Up @@ -211,7 +213,9 @@ jobs:
while IFS= read -r target; do
case "$target" in *_test.go) continue ;; esac
if git cat-file -e "$REVIEW_BASELINE:$target" 2>/dev/null; then
mkdir -p -- "$(dirname -- "$target")"
git show "$REVIEW_BASELINE:$target" > "$target"
git add -N -- "$target"
else
rm -- "$target"
fi
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/shadow-review-repair.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ jobs:
while IFS= read -r target; do
case "$target" in *_test.go) continue ;; esac
if git cat-file -e "$BASELINE_SHA:$target" 2>/dev/null; then
mkdir -p -- "$(dirname -- "$target")"
git show "$BASELINE_SHA:$target" > "$target"
git add -N -- "$target"
test "$(git hash-object "$target")" = "$(git rev-parse "$BASELINE_SHA:$target")"
else
rm -- "$target"
Expand Down Expand Up @@ -154,7 +156,9 @@ jobs:
while IFS= read -r target; do
case "$target" in *_test.go) continue ;; esac
if git cat-file -e "$baseline:$target" 2>/dev/null; then
mkdir -p -- "$(dirname -- "$tested_tree/$target")"
git show "$baseline:$target" > "$tested_tree/$target"
git -C "$tested_tree" add -N -- "$target"
test "$(git hash-object "$tested_tree/$target")" = "$(git rev-parse "$baseline:$target")"
else
rm -- "$tested_tree/$target"
Expand Down
3 changes: 2 additions & 1 deletion backend/internal/service/openai_recovery_subreason.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func safeOpenAIRecoverySubreason(value string) string {
"tool_invalid", "tool_incomplete", "tool_duplicate", "tool_unpaired", "tool_history_unsupported",
"shadow_record_changed", "shadow_epoch_changed", "shadow_fence_changed", "shadow_fence_invalid",
"shadow_store_unavailable", "input_invalid", "input_integrity", "controls_integrity", "readable_intent_missing",
"http_projection_source", "apikey_projection_source", "oauth_projection_source":
"http_projection_source", "apikey_projection_source", "oauth_projection_source",
"ws_body_unreadable", "ws_readable_source", "ws_projection_integrity", "ws_previous_removed":
return value
default:
return "unknown"
Expand Down
47 changes: 42 additions & 5 deletions backend/internal/service/openai_ws_agent_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"sync/atomic"
"time"

"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
coderws "github.com/coder/websocket"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"go.uber.org/zap"
)

type openAIWSAgentRecoveryKey struct{}
Expand Down Expand Up @@ -258,12 +260,12 @@ func (r *openAIWSAgentRecovery) begin(account *Account, source, payload []byte,
// bridge payload and accidentally granting a transformed opaque item.
reader, err := req.GetBody()
if err != nil {
return openAIAgentRecoveryError("wire_changed")
return openAIWSWireRefusal(r.ctx, "ws_body_unreadable", source, payload)
}
actual, err := io.ReadAll(reader)
_ = reader.Close()
if err != nil {
return openAIAgentRecoveryError("wire_changed")
return openAIWSWireRefusal(r.ctx, "ws_body_unreadable", source, payload)
}
payload = actual
}
Expand Down Expand Up @@ -297,7 +299,7 @@ func (r *openAIWSAgentRecovery) begin(account *Account, source, payload []byte,
}
if r.readable != nil {
if openAIAgentRecoveryInputDigest(source) != r.readableSource || openAI409ControlsDigest(source) != r.readableControls {
return openAIAgentRecoveryError("wire_changed")
return openAIWSWireRefusal(ctx, "ws_readable_source", source, payload)
}
ctx = context.WithValue(ctx, openAIAgentRecoveryContextKey{}, r.readable)
if mode == "http_bridge" && account.IsOpenAIApiKey() {
Expand Down Expand Up @@ -348,7 +350,7 @@ func (r *openAIWSAgentRecovery) begin(account *Account, source, payload []byte,
gjson.GetBytes(source, "store").Type != gjson.False || gjson.GetBytes(payload, "previous_response_id").Exists() ||
proof.sourceInput != openAIAgentRecoveryInputDigest(source) || proof.sourceControls != openAI409ControlsDigest(source) || proof.input != openAIAgentRecoveryInputDigest(payload) ||
proof.controls != openAI409ControlsDigest(payload) {
return openAIAgentRecoveryError("wire_changed")
return openAIWSWireRefusal(ctx, "ws_projection_integrity", source, payload)
}
// Recheck the original pointer on the actual selected account before
// allowing its HTTP representation to omit that pointer.
Expand All @@ -374,7 +376,7 @@ func (r *openAIWSAgentRecovery) begin(account *Account, source, payload []byte,
}
if !projected && !r.request.omittedHistory && previous != "" && (r.request.owner != nil || r.request.disableOwnerBinding || r.request.previousResponseID != "") &&
gjson.GetBytes(payload, "previous_response_id").String() != previous {
return openAIAgentRecoveryError("wire_changed")
return openAIWSWireRefusal(ctx, "ws_previous_removed", source, payload)
}
if err := r.service.checkOpenAIAgentRecoveryAccount(ctx, account); err != nil {
if r.request.hasCompletedMixedOwners() {
Expand Down Expand Up @@ -561,3 +563,38 @@ func (r *openAIWSAgentRecovery) write(payload []byte, write func() error) error
func openAIWSAgentRecoveryClose(err error) error {
return NewOpenAIWSClientCloseError(coderws.StatusPolicyViolation, "agent task continuation is unavailable; original task was preserved", err)
}

// Fixed field-presence/count metadata only. Never log response IDs, hashes,
// input text, tool content, headers, credentials or arbitrary JSON keys.
func openAIWSWireRefusal(ctx context.Context, stage string, source, payload []byte) error {
sourceInput, wireInput := gjson.GetBytes(source, "input"), gjson.GetBytes(payload, "input")
inputKind := "absent"
switch {
case sourceInput.IsArray():
inputKind = "array"
case sourceInput.Type == gjson.String:
inputKind = "string"
case sourceInput.Exists():
inputKind = "other"
}
storeKind := "absent"
switch gjson.GetBytes(source, "store").Type {
case gjson.True:
storeKind = "true"
case gjson.False:
storeKind = "false"
default:
if gjson.GetBytes(source, "store").Exists() {
storeKind = "other"
}
}
logger.FromContext(ctx).Warn("openai.encrypted_history_409_refused", zap.String("stage", stage),
zap.String("source_input_kind", inputKind), zap.String("source_store", storeKind),
zap.Int("source_input_items", len(sourceInput.Array())), zap.Int("wire_input_items", len(wireInput.Array())),
zap.Bool("source_previous_present", gjson.GetBytes(source, "previous_response_id").String() != ""),
zap.Bool("wire_previous_present", gjson.GetBytes(payload, "previous_response_id").String() != ""),
zap.Bool("source_model_present", gjson.GetBytes(source, "model").String() != ""),
zap.Bool("input_changed", openAIAgentRecoveryInputDigest(source) != openAIAgentRecoveryInputDigest(payload)),
zap.Bool("controls_changed", openAI409ControlsDigest(source) != openAI409ControlsDigest(payload)))
return openAIRecoveryDiagnosticError("wire_changed", stage, openAIAgentRecoveryFromContext(ctx))
}
6 changes: 6 additions & 0 deletions backend/internal/service/openai_ws_plaintext_owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ func TestOpenAIWSPlaintextBridgeProjectionGuards(t *testing.T) {
require.Empty(t, manager.request.previousResponseID)
} else {
require.Error(t, err, "changed projection must never authorize an upstream write")
if change != "owner" && change != "unproven" {
var recovery *OpenAIAgentRecoveryError
require.ErrorAs(t, err, &recovery)
require.Equal(t, "ws_projection_integrity", recovery.InternalSubreason())
require.Equal(t, "encrypted_agent_recovery_wire_changed", recovery.ClientCode())
}
}
})
}
Expand Down
Loading