@langwatch/server 3.1.0-beta.8 → 3.1.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/package.json +1 -6
  2. package/packages/server/dist/cli.cjs +19 -16
  3. package/cmd/service/main.go +0 -68
  4. package/go.mod +0 -117
  5. package/go.sum +0 -299
  6. package/pkg/breaker/breaker.go +0 -157
  7. package/pkg/breaker/breaker_test.go +0 -131
  8. package/pkg/clog/LICENSE +0 -26
  9. package/pkg/clog/clog.go +0 -90
  10. package/pkg/clog/clog_test.go +0 -56
  11. package/pkg/clog/panic.go +0 -39
  12. package/pkg/config/LICENSE +0 -26
  13. package/pkg/config/config.go +0 -86
  14. package/pkg/config/config_test.go +0 -70
  15. package/pkg/config/otel.go +0 -50
  16. package/pkg/config/server.go +0 -61
  17. package/pkg/config/validate.go +0 -80
  18. package/pkg/config/validate_test.go +0 -74
  19. package/pkg/contexts/contexts_test.go +0 -34
  20. package/pkg/contexts/request_id.go +0 -1
  21. package/pkg/contexts/service_info.go +0 -33
  22. package/pkg/errfuncs/errfuncs.go +0 -9
  23. package/pkg/errfuncs/errfuncs_test.go +0 -28
  24. package/pkg/forkedcontext/forkedcontext.go +0 -57
  25. package/pkg/health/health.go +0 -123
  26. package/pkg/health/health_test.go +0 -84
  27. package/pkg/herr/LICENSE +0 -26
  28. package/pkg/herr/code.go +0 -14
  29. package/pkg/herr/herr.go +0 -112
  30. package/pkg/herr/herr_test.go +0 -78
  31. package/pkg/herr/http.go +0 -133
  32. package/pkg/herr/http_test.go +0 -103
  33. package/pkg/herr/validate.go +0 -72
  34. package/pkg/herr/validate_test.go +0 -75
  35. package/pkg/httpmiddleware/bearer.go +0 -47
  36. package/pkg/httpmiddleware/httputil_test.go +0 -167
  37. package/pkg/httpmiddleware/inflight.go +0 -23
  38. package/pkg/httpmiddleware/maxbody.go +0 -30
  39. package/pkg/httpmiddleware/recover.go +0 -38
  40. package/pkg/httpmiddleware/requestid.go +0 -40
  41. package/pkg/httpmiddleware/telemetry.go +0 -96
  42. package/pkg/httpmiddleware/version.go +0 -16
  43. package/pkg/jwtverify/verifier.go +0 -72
  44. package/pkg/jwtverify/verifier_test.go +0 -134
  45. package/pkg/ksuid/LICENSE +0 -26
  46. package/pkg/ksuid/ksuid.go +0 -157
  47. package/pkg/ksuid/ksuid_test.go +0 -107
  48. package/pkg/ksuid/resources.go +0 -13
  49. package/pkg/lifecycle/group.go +0 -175
  50. package/pkg/lifecycle/group_test.go +0 -236
  51. package/pkg/lifecycle/service.go +0 -112
  52. package/pkg/netprobe/netprobe.go +0 -89
  53. package/pkg/netprobe/netprobe_test.go +0 -55
  54. package/pkg/otelsetup/otelsetup.go +0 -226
  55. package/pkg/otelsetup/otelsetup_test.go +0 -130
  56. package/pkg/ptr/LICENSE +0 -26
  57. package/pkg/ptr/ptr.go +0 -34
  58. package/pkg/ptr/ptr_test.go +0 -66
  59. package/pkg/retry/bench_test.go +0 -67
  60. package/pkg/retry/retry.go +0 -188
  61. package/pkg/retry/retry_test.go +0 -222
  62. package/pkg/slicefuncs/LICENSE +0 -26
  63. package/pkg/slicefuncs/slicefuncs.go +0 -10
  64. package/pkg/slicefuncs/slicefuncs_test.go +0 -29
  65. package/pkg/stacktrace/LICENSE +0 -26
  66. package/pkg/stacktrace/frames.go +0 -77
  67. package/pkg/stacktrace/frames_test.go +0 -73
  68. package/services/aigateway/BENCHMARKS.md +0 -110
  69. package/services/aigateway/README.md +0 -156
  70. package/services/aigateway/adapters/authresolver/bench_test.go +0 -13
  71. package/services/aigateway/adapters/authresolver/redis.go +0 -61
  72. package/services/aigateway/adapters/authresolver/service.go +0 -442
  73. package/services/aigateway/adapters/authresolver/service_test.go +0 -435
  74. package/services/aigateway/adapters/budget/bench_test.go +0 -50
  75. package/services/aigateway/adapters/budget/budget.go +0 -59
  76. package/services/aigateway/adapters/cacherules/evaluator.go +0 -134
  77. package/services/aigateway/adapters/cacherules/evaluator_test.go +0 -207
  78. package/services/aigateway/adapters/controlplane/bench_test.go +0 -40
  79. package/services/aigateway/adapters/controlplane/client.go +0 -213
  80. package/services/aigateway/adapters/controlplane/client_test.go +0 -73
  81. package/services/aigateway/adapters/controlplane/config_wire.go +0 -310
  82. package/services/aigateway/adapters/controlplane/guardrails.go +0 -88
  83. package/services/aigateway/adapters/controlplane/signer.go +0 -95
  84. package/services/aigateway/adapters/controlplane/signer_test.go +0 -64
  85. package/services/aigateway/adapters/customertracebridge/context.go +0 -48
  86. package/services/aigateway/adapters/customertracebridge/context_test.go +0 -24
  87. package/services/aigateway/adapters/customertracebridge/emitter.go +0 -329
  88. package/services/aigateway/adapters/customertracebridge/emitter_test.go +0 -93
  89. package/services/aigateway/adapters/customertracebridge/registry.go +0 -82
  90. package/services/aigateway/adapters/customertracebridge/transport.go +0 -122
  91. package/services/aigateway/adapters/gatewaytracer/attrs.go +0 -28
  92. package/services/aigateway/adapters/gatewaytracer/attrs_genai.go +0 -24
  93. package/services/aigateway/adapters/gatewaytracer/headers.go +0 -8
  94. package/services/aigateway/adapters/gatewaytracer/tracer.go +0 -101
  95. package/services/aigateway/adapters/httpapi/bench_test.go +0 -83
  96. package/services/aigateway/adapters/httpapi/middleware.go +0 -123
  97. package/services/aigateway/adapters/httpapi/router.go +0 -635
  98. package/services/aigateway/adapters/httpapi/router_test.go +0 -633
  99. package/services/aigateway/adapters/modelresolver/resolver.go +0 -107
  100. package/services/aigateway/adapters/modelresolver/resolver_test.go +0 -140
  101. package/services/aigateway/adapters/policy/matcher.go +0 -250
  102. package/services/aigateway/adapters/policy/matcher_test.go +0 -124
  103. package/services/aigateway/adapters/providers/bifrost.go +0 -798
  104. package/services/aigateway/adapters/providers/bifrost_parser.go +0 -201
  105. package/services/aigateway/adapters/providers/bifrost_test.go +0 -181
  106. package/services/aigateway/adapters/ratelimit/limiter.go +0 -122
  107. package/services/aigateway/adapters/ratelimit/limiter_test.go +0 -81
  108. package/services/aigateway/app/app.go +0 -104
  109. package/services/aigateway/app/dispatch.go +0 -89
  110. package/services/aigateway/app/dispatch_test.go +0 -475
  111. package/services/aigateway/app/handlers.go +0 -79
  112. package/services/aigateway/app/peek_bench_test.go +0 -21
  113. package/services/aigateway/app/pipeline/bench_test.go +0 -34
  114. package/services/aigateway/app/pipeline/budget.go +0 -55
  115. package/services/aigateway/app/pipeline/cache.go +0 -34
  116. package/services/aigateway/app/pipeline/cachecontrol.go +0 -86
  117. package/services/aigateway/app/pipeline/cachecontrol_test.go +0 -110
  118. package/services/aigateway/app/pipeline/guardrail.go +0 -121
  119. package/services/aigateway/app/pipeline/pipeline.go +0 -159
  120. package/services/aigateway/app/pipeline/policy.go +0 -23
  121. package/services/aigateway/app/pipeline/ratelimit.go +0 -21
  122. package/services/aigateway/app/pipeline/rawframing_test.go +0 -67
  123. package/services/aigateway/app/pipeline/resolve.go +0 -44
  124. package/services/aigateway/app/pipeline/trace.go +0 -129
  125. package/services/aigateway/app/ports.go +0 -61
  126. package/services/aigateway/cmd/root.go +0 -42
  127. package/services/aigateway/config.go +0 -86
  128. package/services/aigateway/deps.go +0 -177
  129. package/services/aigateway/domain/bundle.go +0 -203
  130. package/services/aigateway/domain/errors.go +0 -23
  131. package/services/aigateway/domain/model.go +0 -24
  132. package/services/aigateway/domain/provider.go +0 -38
  133. package/services/aigateway/domain/request.go +0 -65
  134. package/services/aigateway/domain/response.go +0 -56
  135. package/services/aigateway/domain/verdict.go +0 -55
  136. package/services/aigateway/serve.go +0 -45
  137. package/services/aigateway/tests/matrix/README.md +0 -71
  138. package/services/aigateway/tests/matrix/anthropic_test.go +0 -61
  139. package/services/aigateway/tests/matrix/azure_test.go +0 -52
  140. package/services/aigateway/tests/matrix/bedrock_test.go +0 -69
  141. package/services/aigateway/tests/matrix/gemini_test.go +0 -117
  142. package/services/aigateway/tests/matrix/matrix_test.go +0 -601
  143. package/services/aigateway/tests/matrix/openai_test.go +0 -66
  144. package/services/aigateway/tests/matrix/vertex_test.go +0 -160
@@ -1,66 +0,0 @@
1
- package ptr
2
-
3
- import (
4
- "testing"
5
-
6
- "github.com/stretchr/testify/assert"
7
- "github.com/stretchr/testify/require"
8
- )
9
-
10
- func TestP(t *testing.T) {
11
- tests := []struct {
12
- name string
13
- val int
14
- }{
15
- {name: "positive", val: 42},
16
- {name: "zero", val: 0},
17
- {name: "negative", val: -7},
18
- }
19
-
20
- for _, tc := range tests {
21
- t.Run(tc.name, func(t *testing.T) {
22
- p := P(tc.val)
23
- require.NotNil(t, p)
24
- assert.Equal(t, tc.val, *p)
25
- })
26
- }
27
- }
28
-
29
- func TestValueOrNil_NonZero(t *testing.T) {
30
- result := ValueOrNil(42)
31
- require.NotNil(t, result)
32
- assert.Equal(t, 42, *result)
33
- }
34
-
35
- func TestValueOrNil_Zero(t *testing.T) {
36
- result := ValueOrNil(0)
37
- assert.Nil(t, result)
38
- }
39
-
40
- func TestValueOrZero_NonNil(t *testing.T) {
41
- v := 42
42
- assert.Equal(t, 42, ValueOrZero(&v))
43
- }
44
-
45
- func TestValueOrZero_Nil(t *testing.T) {
46
- var p *int
47
- assert.Equal(t, 0, ValueOrZero(p))
48
- }
49
-
50
- func TestShallowCopy(t *testing.T) {
51
- original := 42
52
- copied := ShallowCopy(&original)
53
-
54
- require.NotNil(t, copied)
55
- assert.Equal(t, 42, *copied)
56
-
57
- // Mutate copy; original unchanged.
58
- *copied = 99
59
- assert.Equal(t, 42, original)
60
- assert.Equal(t, 99, *copied)
61
- }
62
-
63
- func TestShallowCopy_Nil(t *testing.T) {
64
- var p *int
65
- assert.Nil(t, ShallowCopy(p))
66
- }
@@ -1,67 +0,0 @@
1
- package retry
2
-
3
- import (
4
- "context"
5
- "errors"
6
- "testing"
7
- )
8
-
9
- var errRetryableBench = errors.New("upstream 503")
10
- var errFatalBench = errors.New("upstream 400")
11
-
12
- func retryableClassifierBench(err error) Reason {
13
- if errors.Is(err, errRetryableBench) {
14
- return ReasonRetryable5xx
15
- }
16
- return ReasonNonRetryable
17
- }
18
-
19
- // BenchmarkWalk_PrimarySuccess is the happy path: one chain slot, no
20
- // failures, no fallback. Every /v1 request pays this exactly once.
21
- func BenchmarkWalk_PrimarySuccess(b *testing.B) {
22
- attempt := func(_ context.Context, _ string) (struct{}, error) {
23
- return struct{}{}, nil
24
- }
25
- chain := []string{"pc_primary", "pc_secondary"}
26
- ctx := context.Background()
27
- opts := Options{}
28
- b.ReportAllocs()
29
- for i := 0; i < b.N; i++ {
30
- _, el, _ := Walk(ctx, opts, chain, attempt, retryableClassifierBench)
31
- el.Release()
32
- }
33
- }
34
-
35
- // BenchmarkWalk_FallsOver measures the cost when the primary fails
36
- // and the secondary serves.
37
- func BenchmarkWalk_FallsOver(b *testing.B) {
38
- attempt := func(_ context.Context, slot string) (struct{}, error) {
39
- if slot == "pc_primary" {
40
- return struct{}{}, errRetryableBench
41
- }
42
- return struct{}{}, nil
43
- }
44
- chain := []string{"pc_primary", "pc_secondary"}
45
- ctx := context.Background()
46
- opts := Options{}
47
- b.ReportAllocs()
48
- for i := 0; i < b.N; i++ {
49
- _, el, _ := Walk(ctx, opts, chain, attempt, retryableClassifierBench)
50
- el.Release()
51
- }
52
- }
53
-
54
- // BenchmarkWalk_NonRetryableStops verifies fast exit on non-retryable error.
55
- func BenchmarkWalk_NonRetryableStops(b *testing.B) {
56
- attempt := func(_ context.Context, _ string) (struct{}, error) {
57
- return struct{}{}, errFatalBench
58
- }
59
- chain := []string{"pc_primary", "pc_secondary", "pc_tertiary"}
60
- ctx := context.Background()
61
- opts := Options{}
62
- b.ReportAllocs()
63
- for i := 0; i < b.N; i++ {
64
- _, el, _ := Walk(ctx, opts, chain, attempt, retryableClassifierBench)
65
- el.Release()
66
- }
67
- }
@@ -1,188 +0,0 @@
1
- // Package retry implements a generic fallback/retry engine that walks an
2
- // ordered chain of slots, advancing on retryable failures.
3
- package retry
4
-
5
- import (
6
- "context"
7
- "errors"
8
- "fmt"
9
- "sync"
10
- "time"
11
- )
12
-
13
- // Reason classifies why an attempt was abandoned.
14
- type Reason string
15
-
16
- const (
17
- ReasonSuccess Reason = "success"
18
- ReasonFallback Reason = "fallback_success"
19
- ReasonRetryable5xx Reason = "retryable_5xx"
20
- ReasonRateLimit Reason = "rate_limit"
21
- ReasonTimeout Reason = "timeout"
22
- ReasonNetwork Reason = "network"
23
- ReasonCircuitOpen Reason = "circuit_open"
24
- ReasonNonRetryable Reason = "non_retryable"
25
- ReasonChainExhausted Reason = "chain_exhausted"
26
- ReasonContextDone Reason = "context_done"
27
- )
28
-
29
- // Attempt is the operation retried across chain slots. Return (result, nil)
30
- // on success, or (zero, err) on failure. The classify function determines
31
- // whether to retry.
32
- type Attempt[R any] func(ctx context.Context, slotID string) (R, error)
33
-
34
- // Classifier maps an error to a Reason. Return ReasonNonRetryable to stop.
35
- type Classifier func(err error) Reason
36
-
37
- // BreakerChecker reports whether a slot is allowed (circuit closed/half-open).
38
- type BreakerChecker interface {
39
- Allow(id string) bool
40
- RecordSuccess(id string)
41
- RecordFailure(id string)
42
- }
43
-
44
- // Event describes one attempt in the chain.
45
- type Event struct {
46
- Slot int
47
- SlotID string
48
- Reason Reason
49
- Duration time.Duration
50
- Err error
51
- }
52
-
53
- // EventLog holds a pooled slice of events from a Walk call. The struct wrapper
54
- // keeps the *EventLog pointer stable through Get/Put — avoids re-allocating a
55
- // pointer on every Release (which defeats the pool). Call Release when done.
56
- type EventLog struct {
57
- events []Event
58
- }
59
-
60
- // Events returns the recorded events.
61
- func (el *EventLog) Events() []Event {
62
- if el == nil {
63
- return nil
64
- }
65
- return el.events
66
- }
67
-
68
- // Release returns the backing memory to the pool. The EventLog must not be
69
- // used after calling Release.
70
- func (el *EventLog) Release() {
71
- if el == nil {
72
- return
73
- }
74
- for i := range el.events {
75
- el.events[i] = Event{}
76
- }
77
- el.events = el.events[:0]
78
- eventsPool.Put(el)
79
- }
80
-
81
- var eventsPool = sync.Pool{
82
- New: func() any {
83
- return &EventLog{events: make([]Event, 0, 4)}
84
- },
85
- }
86
-
87
- // defaultTriggers is a package-level read-only map so withDefaults() doesn't
88
- // allocate a new map on every Walk call.
89
- var defaultTriggers = map[Reason]bool{
90
- ReasonRetryable5xx: true,
91
- ReasonRateLimit: true,
92
- ReasonTimeout: true,
93
- ReasonNetwork: true,
94
- }
95
-
96
- // Options configures the retry engine.
97
- type Options struct {
98
- Triggers map[Reason]bool // reasons that trigger retry (default: 5xx, rate_limit, timeout, network)
99
- MaxAttempts int // 0 = walk full chain
100
- Breaker BreakerChecker // optional circuit breaker
101
- PerAttemptTimeout time.Duration // 0 = inherit parent ctx
102
- }
103
-
104
- func (o *Options) withDefaults() {
105
- if o.Triggers == nil {
106
- o.Triggers = defaultTriggers
107
- }
108
- }
109
-
110
- // Walk executes the attempt across the chain. Returns the first successful
111
- // result, an EventLog (call Release when done), or an error if the chain is
112
- // exhausted.
113
- func Walk[R any](ctx context.Context, opts Options, chain []string, try Attempt[R], classify Classifier) (R, *EventLog, error) {
114
- var zero R
115
- opts.withDefaults()
116
-
117
- if len(chain) == 0 {
118
- chain = []string{""}
119
- }
120
-
121
- el := eventsPool.Get().(*EventLog)
122
- var firstErr error
123
- attempts := 0
124
-
125
- for i, slotID := range chain {
126
- if err := ctx.Err(); err != nil {
127
- el.events = append(el.events, Event{Slot: i, SlotID: slotID, Reason: ReasonContextDone, Err: err})
128
- return zero, el, err
129
- }
130
- if slotID != "" && opts.Breaker != nil && !opts.Breaker.Allow(slotID) {
131
- el.events = append(el.events, Event{Slot: i, SlotID: slotID, Reason: ReasonCircuitOpen})
132
- continue
133
- }
134
- if opts.MaxAttempts > 0 && attempts >= opts.MaxAttempts {
135
- el.events = append(el.events, Event{Slot: i, SlotID: slotID, Reason: ReasonChainExhausted})
136
- break
137
- }
138
- attempts++
139
-
140
- attemptCtx := ctx
141
- var cancel context.CancelFunc
142
- if opts.PerAttemptTimeout > 0 {
143
- attemptCtx, cancel = context.WithTimeout(ctx, opts.PerAttemptTimeout)
144
- }
145
-
146
- start := time.Now()
147
- result, err := try(attemptCtx, slotID)
148
- duration := time.Since(start)
149
-
150
- if cancel != nil {
151
- cancel()
152
- }
153
-
154
- if err == nil {
155
- reason := ReasonSuccess
156
- if i > 0 {
157
- reason = ReasonFallback
158
- }
159
- el.events = append(el.events, Event{Slot: i, SlotID: slotID, Reason: reason, Duration: duration})
160
- if slotID != "" && opts.Breaker != nil {
161
- opts.Breaker.RecordSuccess(slotID)
162
- }
163
- return result, el, nil
164
- }
165
-
166
- if firstErr == nil {
167
- firstErr = err
168
- }
169
-
170
- reason := ReasonNonRetryable
171
- if classify != nil {
172
- reason = classify(err)
173
- }
174
- el.events = append(el.events, Event{Slot: i, SlotID: slotID, Reason: reason, Duration: duration, Err: err})
175
-
176
- if slotID != "" && opts.Breaker != nil {
177
- opts.Breaker.RecordFailure(slotID)
178
- }
179
- if !opts.Triggers[reason] {
180
- return zero, el, err
181
- }
182
- }
183
-
184
- if firstErr == nil {
185
- firstErr = errors.New("retry chain exhausted with no attempts")
186
- }
187
- return zero, el, fmt.Errorf("retry chain exhausted: %w", firstErr)
188
- }
@@ -1,222 +0,0 @@
1
- package retry
2
-
3
- import (
4
- "context"
5
- "errors"
6
- "fmt"
7
- "testing"
8
-
9
- "github.com/stretchr/testify/assert"
10
- "github.com/stretchr/testify/require"
11
- )
12
-
13
- // mockBreaker is a simple BreakerChecker for testing.
14
- type mockBreaker struct {
15
- blocked map[string]bool
16
- recorded map[string]string // last action: "success" or "failure"
17
- }
18
-
19
- func newMockBreaker() *mockBreaker {
20
- return &mockBreaker{
21
- blocked: make(map[string]bool),
22
- recorded: make(map[string]string),
23
- }
24
- }
25
-
26
- func (m *mockBreaker) Allow(id string) bool { return !m.blocked[id] }
27
- func (m *mockBreaker) RecordSuccess(id string) { m.recorded[id] = "success" }
28
- func (m *mockBreaker) RecordFailure(id string) { m.recorded[id] = "failure" }
29
-
30
- var errRetryable = errors.New("retryable error")
31
- var errFatal = errors.New("fatal error")
32
-
33
- func retryableClassifier(err error) Reason {
34
- if errors.Is(err, errRetryable) {
35
- return ReasonRetryable5xx
36
- }
37
- return ReasonNonRetryable
38
- }
39
-
40
- func TestWalk_SuccessOnFirstSlot(t *testing.T) {
41
- chain := []string{"a", "b"}
42
- attempt := func(_ context.Context, slot string) (string, error) {
43
- return "ok-" + slot, nil
44
- }
45
-
46
- result, el, err := Walk(context.Background(), Options{}, chain, attempt, retryableClassifier)
47
- defer el.Release()
48
-
49
- events := el.Events()
50
- require.NoError(t, err)
51
- assert.Equal(t, "ok-a", result)
52
- require.Len(t, events, 1)
53
- assert.Equal(t, ReasonSuccess, events[0].Reason)
54
- }
55
-
56
- func TestWalk_FallbackSuccess(t *testing.T) {
57
- calls := 0
58
- chain := []string{"a", "b"}
59
- attempt := func(_ context.Context, slot string) (string, error) {
60
- calls++
61
- if slot == "a" {
62
- return "", errRetryable
63
- }
64
- return "ok-" + slot, nil
65
- }
66
-
67
- result, el, err := Walk(context.Background(), Options{}, chain, attempt, retryableClassifier)
68
- defer el.Release()
69
-
70
- events := el.Events()
71
- require.NoError(t, err)
72
- assert.Equal(t, "ok-b", result)
73
- assert.Equal(t, 2, calls)
74
- require.Len(t, events, 2)
75
- assert.Equal(t, ReasonRetryable5xx, events[0].Reason)
76
- assert.Equal(t, ReasonFallback, events[1].Reason)
77
- }
78
-
79
- func TestWalk_ChainExhausted(t *testing.T) {
80
- chain := []string{"a", "b", "c"}
81
- attempt := func(_ context.Context, _ string) (string, error) {
82
- return "", errRetryable
83
- }
84
-
85
- _, el, err := Walk(context.Background(), Options{}, chain, attempt, retryableClassifier)
86
- defer el.Release()
87
-
88
- require.Error(t, err)
89
- assert.Contains(t, err.Error(), "retry chain exhausted")
90
- assert.Len(t, el.Events(), 3)
91
- }
92
-
93
- func TestWalk_NonRetryableStops(t *testing.T) {
94
- chain := []string{"a", "b", "c"}
95
- attempt := func(_ context.Context, _ string) (string, error) {
96
- return "", errFatal
97
- }
98
-
99
- _, el, err := Walk(context.Background(), Options{}, chain, attempt, retryableClassifier)
100
- defer el.Release()
101
-
102
- events := el.Events()
103
- require.Error(t, err)
104
- require.ErrorIs(t, err, errFatal)
105
- require.Len(t, events, 1, "should stop after first non-retryable error")
106
- assert.Equal(t, ReasonNonRetryable, events[0].Reason)
107
- }
108
-
109
- func TestWalk_MaxAttempts(t *testing.T) {
110
- chain := []string{"a", "b", "c", "d"}
111
- calls := 0
112
- attempt := func(_ context.Context, _ string) (string, error) {
113
- calls++
114
- return "", errRetryable
115
- }
116
-
117
- _, el, err := Walk(context.Background(), Options{MaxAttempts: 2}, chain, attempt, retryableClassifier)
118
- defer el.Release()
119
-
120
- events := el.Events()
121
- require.Error(t, err)
122
- assert.Equal(t, 2, calls)
123
- // 2 attempt events + 1 chain_exhausted event
124
- require.Len(t, events, 3)
125
- assert.Equal(t, ReasonChainExhausted, events[2].Reason)
126
- }
127
-
128
- func TestWalk_ContextCanceled(t *testing.T) {
129
- ctx, cancel := context.WithCancel(context.Background())
130
- cancel() // cancel immediately
131
-
132
- chain := []string{"a"}
133
- attempt := func(_ context.Context, _ string) (string, error) {
134
- return "should not reach", nil
135
- }
136
-
137
- _, el, err := Walk(ctx, Options{}, chain, attempt, retryableClassifier)
138
- defer el.Release()
139
-
140
- events := el.Events()
141
- require.Error(t, err)
142
- require.Len(t, events, 1)
143
- assert.Equal(t, ReasonContextDone, events[0].Reason)
144
- }
145
-
146
- func TestWalk_BreakerSkipsSlot(t *testing.T) {
147
- b := newMockBreaker()
148
- b.blocked["b"] = true
149
-
150
- chain := []string{"a", "b", "c"}
151
- calls := []string{}
152
- attempt := func(_ context.Context, slot string) (string, error) {
153
- calls = append(calls, slot)
154
- if slot == "a" {
155
- return "", errRetryable
156
- }
157
- return "ok-" + slot, nil
158
- }
159
-
160
- result, el, err := Walk(context.Background(), Options{Breaker: b}, chain, attempt, retryableClassifier)
161
- defer el.Release()
162
-
163
- events := el.Events()
164
- require.NoError(t, err)
165
- assert.Equal(t, "ok-c", result)
166
- assert.Equal(t, []string{"a", "c"}, calls, "should skip slot b")
167
-
168
- // Find the circuit_open event for slot b.
169
- var circuitEvent *Event
170
- for i := range events {
171
- if events[i].Reason == ReasonCircuitOpen {
172
- circuitEvent = &events[i]
173
- break
174
- }
175
- }
176
- require.NotNil(t, circuitEvent)
177
- assert.Equal(t, "b", circuitEvent.SlotID)
178
- }
179
-
180
- func TestWalk_CircuitOpenDoesNotConsumeAttempts(t *testing.T) {
181
- b := newMockBreaker()
182
- b.blocked["a"] = true
183
- b.blocked["b"] = true
184
-
185
- chain := []string{"a", "b", "c", "d"}
186
- calls := 0
187
- attempt := func(_ context.Context, _ string) (string, error) {
188
- calls++
189
- return "", errRetryable
190
- }
191
-
192
- // MaxAttempts=1 but first two slots are circuit-open — they should NOT
193
- // consume the budget. Only slot "c" should actually be attempted.
194
- _, el, err := Walk(context.Background(), Options{MaxAttempts: 1, Breaker: b}, chain, attempt, retryableClassifier)
195
- defer el.Release()
196
-
197
- events := el.Events()
198
- require.Error(t, err)
199
- assert.Equal(t, 1, calls, "only 1 real attempt should be made")
200
-
201
- // Events: circuit_open(a), circuit_open(b), retryable(c), chain_exhausted(d)
202
- var reasons []Reason
203
- for _, e := range events {
204
- reasons = append(reasons, e.Reason)
205
- }
206
- assert.Equal(t, []Reason{ReasonCircuitOpen, ReasonCircuitOpen, ReasonRetryable5xx, ReasonChainExhausted}, reasons)
207
- }
208
-
209
- func TestWalk_EmptyChain(t *testing.T) {
210
- attempt := func(_ context.Context, slot string) (string, error) {
211
- return fmt.Sprintf("result-slot:%s", slot), nil
212
- }
213
-
214
- result, el, err := Walk(context.Background(), Options{}, nil, attempt, retryableClassifier)
215
- defer el.Release()
216
-
217
- events := el.Events()
218
- require.NoError(t, err)
219
- assert.Equal(t, "result-slot:", result)
220
- require.Len(t, events, 1)
221
- assert.Equal(t, ReasonSuccess, events[0].Reason)
222
- }
@@ -1,26 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022-2024 Mojo Men Ltd
4
- Copyright (c) 2026 Reasoning Engine B.V.
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
23
-
24
- ---
25
-
26
- This package is derived from https://github.com/wearemojo/mojo-public-go/tree/main/lib/slicefn
@@ -1,10 +0,0 @@
1
- package slicefuncs
2
-
3
- // Map applies f to each element of ts and returns the results.
4
- func Map[T, U any](ts []T, f func(T) U) []U {
5
- us := make([]U, len(ts))
6
- for i := range ts {
7
- us[i] = f(ts[i])
8
- }
9
- return us
10
- }
@@ -1,29 +0,0 @@
1
- package slicefuncs
2
-
3
- import (
4
- "strconv"
5
- "testing"
6
-
7
- "github.com/stretchr/testify/assert"
8
- )
9
-
10
- func TestMap(t *testing.T) {
11
- ints := []int{1, 2, 3}
12
- result := Map(ints, strconv.Itoa)
13
-
14
- assert.Equal(t, []string{"1", "2", "3"}, result)
15
- }
16
-
17
- func TestMap_Empty(t *testing.T) {
18
- result := Map([]int{}, strconv.Itoa)
19
-
20
- assert.Equal(t, []string{}, result)
21
- assert.Empty(t, result)
22
- }
23
-
24
- func TestMap_Nil(t *testing.T) {
25
- result := Map(nil, strconv.Itoa)
26
-
27
- assert.NotNil(t, result, "nil input should return empty slice, not nil")
28
- assert.Empty(t, result)
29
- }
@@ -1,26 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022-2024 Mojo Men Ltd
4
- Copyright (c) 2026 Reasoning Engine B.V.
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
23
-
24
- ---
25
-
26
- This package is derived from https://github.com/wearemojo/mojo-public-go/tree/main/lib/stacktrace