Files
CLIProxyAPI/sdk/cliproxy/executor/context.go
Luis Pater 7fcdfcfe17 feat(plugin): add bounded Codex scheduler continuity hook
- Add optional `scheduler.continuity` plugin ABI/RPC hook with bounded `pre_admission` and `pre_output_429` wait gates, persisted cooldown provenance separation, and SSE/WebSocket keepalive handoff.
- Enforce per-request continuity budgets, `disable_cooling` retry limits, and stable turn `RequestID` across SSE bootstrap retries and WebSocket in-place failover.
- Add race-verified unit and handler integration tests covering continuity gates, cooldown provenance, keepalive suppression, and session isolation.

Closes: #7589
2026-11-17 03:22:51 +08:00

168 lines
5.3 KiB
Go

package executor
import (
"context"
"sync/atomic"
)
type downstreamWebsocketContextKey struct{}
type requireUpstreamWebsocketContextKey struct{}
type upstreamAttemptTrackerContextKey struct{}
type localWebsocketTurnTrackerContextKey struct{}
type continuityWaitObserverContextKey struct{}
// ContinuityWaitObserver notifies a downstream transport when a real continuity wait starts and ends.
type ContinuityWaitObserver interface {
OnContinuityWaitStart()
OnContinuityWaitEnd()
}
type upstreamAttemptTracker struct {
attempted atomic.Bool
}
type localWebsocketTurnTracker struct {
executed atomic.Bool
}
// WithDownstreamWebsocket marks the current request as coming from a downstream websocket connection.
func WithDownstreamWebsocket(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, downstreamWebsocketContextKey{}, true)
}
// DownstreamWebsocket reports whether the current request originates from a downstream websocket connection.
func DownstreamWebsocket(ctx context.Context) bool {
if ctx == nil {
return false
}
raw := ctx.Value(downstreamWebsocketContextKey{})
enabled, ok := raw.(bool)
return ok && enabled
}
// WithRequiredUpstreamWebsocket marks a request whose incremental context is valid only on the current upstream websocket.
func WithRequiredUpstreamWebsocket(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, requireUpstreamWebsocketContextKey{}, true)
}
// RequiredUpstreamWebsocket reports whether falling back to an HTTP upstream would lose request context.
func RequiredUpstreamWebsocket(ctx context.Context) bool {
if ctx == nil {
return false
}
raw := ctx.Value(requireUpstreamWebsocketContextKey{})
enabled, ok := raw.(bool)
return ok && enabled
}
// WithUpstreamAttemptTracker installs a fresh tracker for one provider execution attempt.
func WithUpstreamAttemptTracker(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, upstreamAttemptTrackerContextKey{}, &upstreamAttemptTracker{})
}
// MarkUpstreamAttempt records that the provider execution reached an upstream transport boundary.
func MarkUpstreamAttempt(ctx context.Context) {
if ctx == nil {
return
}
tracker, ok := ctx.Value(upstreamAttemptTrackerContextKey{}).(*upstreamAttemptTracker)
if !ok || tracker == nil {
return
}
tracker.attempted.Store(true)
}
// UpstreamAttempted reports whether the tracked provider execution reached an upstream transport boundary.
func UpstreamAttempted(ctx context.Context) bool {
if ctx == nil {
return false
}
tracker, ok := ctx.Value(upstreamAttemptTrackerContextKey{}).(*upstreamAttemptTracker)
return ok && tracker != nil && tracker.attempted.Load()
}
// WithLocalWebsocketTurnTracker installs a tracker that records when a downstream
// websocket turn was fulfilled locally over HTTP rather than establishing or
// owning an upstream websocket response.
func WithLocalWebsocketTurnTracker(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, localWebsocketTurnTrackerContextKey{}, &localWebsocketTurnTracker{})
}
// MarkLocalWebsocketTurn records that the current downstream websocket turn was
// answered locally without creating an upstream websocket response.
func MarkLocalWebsocketTurn(ctx context.Context) {
if ctx == nil {
return
}
tracker, ok := ctx.Value(localWebsocketTurnTrackerContextKey{}).(*localWebsocketTurnTracker)
if !ok || tracker == nil {
return
}
tracker.executed.Store(true)
}
// ResetLocalWebsocketTurn clears the local websocket turn marker before a new auth attempt.
func ResetLocalWebsocketTurn(ctx context.Context) {
if ctx == nil {
return
}
tracker, ok := ctx.Value(localWebsocketTurnTrackerContextKey{}).(*localWebsocketTurnTracker)
if !ok || tracker == nil {
return
}
tracker.executed.Store(false)
}
// LocalWebsocketTurnExecuted reports whether the current downstream websocket turn
// was fulfilled locally rather than on an upstream websocket.
func LocalWebsocketTurnExecuted(ctx context.Context) bool {
if ctx == nil {
return false
}
tracker, ok := ctx.Value(localWebsocketTurnTrackerContextKey{}).(*localWebsocketTurnTracker)
return ok && tracker != nil && tracker.executed.Load()
}
// WithContinuityWaitObserver attaches an observer notified only while a real continuity wait is active.
func WithContinuityWaitObserver(ctx context.Context, observer ContinuityWaitObserver) context.Context {
if observer == nil {
return ctx
}
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, continuityWaitObserverContextKey{}, observer)
}
// NotifyContinuityWaitStart notifies any attached observer that a real continuity wait has started.
func NotifyContinuityWaitStart(ctx context.Context) {
if ctx == nil {
return
}
if observer, ok := ctx.Value(continuityWaitObserverContextKey{}).(ContinuityWaitObserver); ok && observer != nil {
observer.OnContinuityWaitStart()
}
}
// NotifyContinuityWaitEnd notifies any attached observer that a continuity wait has ended.
func NotifyContinuityWaitEnd(ctx context.Context) {
if ctx == nil {
return
}
if observer, ok := ctx.Value(continuityWaitObserverContextKey{}).(ContinuityWaitObserver); ok && observer != nil {
observer.OnContinuityWaitEnd()
}
}