mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
- Track the running response identity in the Codex websocket session and reject interrupts that do not target it, so an interrupt after response.completed is never forwarded upstream while turn teardown is pending. - Gate the handler's local interrupt path by explicit turn ownership and close it before terminal payloads are written downstream, preventing fabricated response.incomplete events and silently dropped claimed frames. - Add deterministic regression tests covering late and unknown interrupts across completion, failure, queued rejection, upstream loss, and HTTP fallback turns. Closes: #7065
73 lines
2.6 KiB
Go
73 lines
2.6 KiB
Go
package executor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
// ErrNoActiveUpstreamWebsocket means response.interrupt has no live upstream socket.
|
|
var ErrNoActiveUpstreamWebsocket = errors.New("no active upstream websocket for response.interrupt")
|
|
|
|
// ErrUpstreamWebsocketInterruptRejected means a live upstream socket owns the
|
|
// current turn, but response.interrupt does not target its running response
|
|
// (unknown id, or that response already finished). Handlers must reject the
|
|
// frame outright and must not fall back to local cancellation, which would
|
|
// cancel a healthy websocket turn.
|
|
var ErrUpstreamWebsocketInterruptRejected = errors.New("response.interrupt does not target the active upstream response")
|
|
|
|
// WebsocketInput is a frame from the single downstream reader. Err terminates
|
|
// the connection; Payload is owned by the receiver and must not be replayed.
|
|
type WebsocketInput struct {
|
|
Payload []byte
|
|
Err error
|
|
// Epoch is the downstream input epoch at read time; a loss advances it.
|
|
Epoch uint64
|
|
}
|
|
type websocketInputKey struct{}
|
|
|
|
func WithWebsocketInput(ctx context.Context, input <-chan WebsocketInput) context.Context {
|
|
return context.WithValue(ctx, websocketInputKey{}, input)
|
|
}
|
|
func WebsocketInputFromContext(ctx context.Context) <-chan WebsocketInput {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
input, _ := ctx.Value(websocketInputKey{}).(<-chan WebsocketInput)
|
|
return input
|
|
}
|
|
|
|
// WithWebsocketAuthCheck supplies the live account-state check for a bound
|
|
// connection. It may reject further frames but never select another account.
|
|
type websocketAuthCheckKey struct{}
|
|
|
|
func WithWebsocketAuthCheck(ctx context.Context, check func(string) bool) context.Context {
|
|
return context.WithValue(ctx, websocketAuthCheckKey{}, check)
|
|
}
|
|
func WebsocketAuthEnabled(ctx context.Context, authID string) bool {
|
|
if ctx == nil {
|
|
return true
|
|
}
|
|
check, _ := ctx.Value(websocketAuthCheckKey{}).(func(string) bool)
|
|
return check == nil || check(authID)
|
|
}
|
|
|
|
type websocketTurnAuthorizerKey struct{}
|
|
|
|
// WithWebsocketTurnAuthorizer supplies a per-turn frontend access check for
|
|
// follow-up response.create and response.append frames on a duplex socket.
|
|
// Returning a non-empty error payload rejects the turn while keeping the socket open.
|
|
func WithWebsocketTurnAuthorizer(ctx context.Context, authorize func([]byte) ([]byte, error)) context.Context {
|
|
return context.WithValue(ctx, websocketTurnAuthorizerKey{}, authorize)
|
|
}
|
|
|
|
func AuthorizeWebsocketTurn(ctx context.Context, payload []byte) ([]byte, error) {
|
|
if ctx == nil {
|
|
return nil, nil
|
|
}
|
|
authorize, _ := ctx.Value(websocketTurnAuthorizerKey{}).(func([]byte) ([]byte, error))
|
|
if authorize == nil {
|
|
return nil, nil
|
|
}
|
|
return authorize(payload)
|
|
}
|