Debian Codegraph @

assert/numeric_guidance.go

raw
1 //go:build !no_antithesis_sdk
2
3 package assert
4
5 import (
6 "math"
7 "sync"
8 "sync/atomic"
9
10 "github.com/antithesishq/antithesis-sdk-go/internal"
11 )
12
13 // --------------------------------------------------------------------------------
14 // IntegerGap is used for:
15 // - int, int8, int16, int32, int64:
16 // - uint, uint8, uint16, uint32, uint64, uintptr:
17 //
18 // FloatGap is used for:
19 // - float32, float64
20 // --------------------------------------------------------------------------------
21 type numericGapType int
22
23 const (
24 integerGapType numericGapType = iota
25 floatGapType
26 )
27
28 func gapTypeForOperand[T Number](num T) numericGapType {
29 gapType := integerGapType
30
31 switch any(num).(type) {
32 case float32, float64:
33 gapType = floatGapType
34 }
35 return gapType
36 }
37
38 // --------------------------------------------------------------------------------
39 // numericGuidanceTracker - Tracking Info for Numeric Guidance
40 //
41 // For GuidanceFnMaximize:
42 // - gap is the largest value sent so far
43 //
44 // For GuidanceFnMinimize:
45 // - gap is the most negative value sent so far
46 //
47 // --------------------------------------------------------------------------------
48 type numericGuidanceInfo struct {
49 descriminator numericGapType
50 maximize bool
51 // The best gap sent so far (a gapValue[uint64] or gapValue[float64],
52 // fixed per entry). Read lock-free on every evaluation; updated under mu.
53 gap atomic.Value
54 // Serializes improvements (compare, update, emit) so guidance emissions
55 // form an ordered monotone chain; the no-improvement fast path never
56 // takes it.
57 mu sync.Mutex
58 }
59
60 var numericGuidanceTracker sync.Map // message key -> *numericGuidanceInfo
61
62 func getNumericGuidanceEntry(messageKey string, trackerType numericGapType, maximize bool) *numericGuidanceInfo {
63 if entry, ok := numericGuidanceTracker.Load(messageKey); ok {
64 return entry.(*numericGuidanceInfo)
65 }
66 entry, _ := numericGuidanceTracker.LoadOrStore(messageKey,
67 newNumericGuidanceInfo(trackerType, maximize))
68 return entry.(*numericGuidanceInfo)
69 }
70
71 // Create an numeric guidance entry
72 func newNumericGuidanceInfo(trackerType numericGapType, maximize bool) *numericGuidanceInfo {
73
74 var gap any
75 if trackerType == integerGapType {
76 gap = newGapValue(uint64(math.MaxUint64), maximize)
77 } else {
78 gap = newGapValue(float64(math.MaxFloat64), maximize)
79 }
80 trackerInfo := numericGuidanceInfo{
81 maximize: maximize,
82 descriminator: trackerType,
83 }
84 trackerInfo.gap.Store(gap)
85 return &trackerInfo
86 }
87
88 func (tI *numericGuidanceInfo) should_maximize() bool {
89 return tI.maximize
90 }
91
92 func (tI *numericGuidanceInfo) is_integer_gap() bool {
93 return tI.descriminator == integerGapType
94 }
95
96 // --------------------------------------------------------------------------------
97 // Represents integral and floating point extremes
98 // --------------------------------------------------------------------------------
99 type gapValue[T numConstraint] struct {
100 gap_size T
101 gap_is_negative bool
102 }
103
104 func newGapValue[T numConstraint](sz T, is_neg bool) any {
105 switch any(sz).(type) {
106 case uint64:
107 return gapValue[uint64]{gap_size: uint64(sz), gap_is_negative: is_neg}
108
109 case float64:
110 return gapValue[float64]{gap_size: float64(sz), gap_is_negative: is_neg}
111 }
112 return nil
113 }
114
115 func is_same_sign(left_val int64, right_val int64) bool {
116 same_sign := false
117 if left_val < 0 {
118 // left is negative
119 if right_val < 0 {
120 same_sign = true
121 }
122 } else {
123 // left is non-negative
124 if right_val >= 0 {
125 same_sign = true
126 }
127 }
128 return same_sign
129 }
130
131 func abs_int64(val int64) uint64 {
132 if val >= 0 {
133 return uint64(val)
134 }
135 return uint64(0 - val)
136 }
137
138 func is_greater_than[T numConstraint](left gapValue[T], right gapValue[T]) bool {
139 if !left.gap_is_negative && !right.gap_is_negative {
140 return left.gap_size > right.gap_size
141 }
142 if !left.gap_is_negative && right.gap_is_negative {
143 return true // any positive is greater than a negative
144 }
145 if left.gap_is_negative && right.gap_is_negative {
146 return right.gap_size > left.gap_size
147 }
148 if left.gap_is_negative && !right.gap_is_negative {
149 return false // any negative is less than a positive
150 }
151 return false
152 }
153
154 func is_less_than[T numConstraint](left gapValue[T], right gapValue[T]) bool {
155 if !left.gap_is_negative && !right.gap_is_negative {
156 return left.gap_size < right.gap_size
157 }
158 if !left.gap_is_negative && right.gap_is_negative {
159 return false // any positive is greater than a negative
160 }
161 if left.gap_is_negative && right.gap_is_negative {
162 return right.gap_size < left.gap_size
163 }
164 if left.gap_is_negative && !right.gap_is_negative {
165 return true // any negative is less than a positive
166 }
167 return true
168 }
169
170 // The candidate gap for a pair of guidance operands, in the tracker's
171 // representation (nil when the operand type resolves to none).
172 func candidateGap(data any) any {
173 // Needs to have individual case statements to assist
174 // the compiler to infer the actual type of the var named 'operands'
175 switch operands := data.(type) {
176 case numericOperands[int32]:
177 return makeGap(operands)
178 case numericOperands[int64]:
179 return makeGap(operands)
180 case numericOperands[uint64]:
181 return makeGap(operands)
182 case numericOperands[float64]:
183 return makeFloatGap(operands)
184 }
185 return nil
186 }
187
188 // Whether candidate improves on the entry's tracked extremum.
189 func (tI *numericGuidanceInfo) improves(candidate any) bool {
190 maximize := tI.should_maximize()
191 switch prev := tI.gap.Load().(type) {
192 case gapValue[uint64]:
193 if gap, ok := candidate.(gapValue[uint64]); ok {
194 if maximize {
195 return is_greater_than(gap, prev)
196 }
197 return is_less_than(gap, prev)
198 }
199 case gapValue[float64]:
200 if gap, ok := candidate.(gapValue[float64]); ok {
201 if maximize {
202 return is_greater_than(gap, prev)
203 }
204 return is_less_than(gap, prev)
205 }
206 }
207 return false
208 }
209
210 func send_value_if_needed(tI *numericGuidanceInfo, gI *guidanceInfo) {
211 if tI == nil {
212 return
213 }
214
215 // if this is a catalog entry (gI.hit is false)
216 // do not update the reference gap in the tracker (tI *numericGuidanceInfo)
217 if !gI.Hit {
218 emitGuidance(gI)
219 return
220 }
221
222 // Most evaluations do not improve on the tracked extremum; that path is
223 // a lock-free load and compare. The mutex serializes only improvements.
224 candidate := candidateGap(gI.Data)
225 if !tI.improves(candidate) {
226 return
227 }
228 tI.mu.Lock()
229 defer tI.mu.Unlock()
230 if !tI.improves(candidate) {
231 return
232 }
233 tI.gap.Store(candidate)
234 emitGuidance(gI)
235 }
236
237 func emitGuidance(gI *guidanceInfo) error {
238 return internal.Json_data(map[string]any{"antithesis_guidance": gI})
239 }
240
241 // When left and right are the same sign (both negative, or both non-negative)
242 // Calculate: <result> = (left - right). The gap_size is abs(<result>) and
243 // gap_is_negative is (right > left)
244 func makeGap[Op operandConstraint](operand numericOperands[Op]) gapValue[uint64] {
245
246 var gap_size uint64
247 var gap_is_negative bool
248
249 switch this_op := any(operand).(type) {
250 case numericOperands[int32]:
251 result := int64(this_op.Left) - int64(this_op.Right)
252 gap_size = abs_int64(result)
253 gap_is_negative = result < 0
254
255 case numericOperands[int64]:
256 if is_same_sign(this_op.Left, this_op.Right) {
257 result := int64(this_op.Left) - int64(this_op.Right)
258 gap_size = abs_int64(result)
259 gap_is_negative = result < 0
260 break
261 }
262
263 // Otherwise left and right are opposite signs
264 // gap = abs(left) + abs(right)
265 // gap_is_negative = left < right
266 left_gap_size := abs_int64(this_op.Left)
267 right_gap_size := abs_int64(this_op.Right)
268 gap_size = left_gap_size + right_gap_size
269 gap_is_negative = this_op.Left < this_op.Right
270
271 case numericOperands[uint64]:
272 left_val := this_op.Left
273 right_val := this_op.Right
274 gap_is_negative = false
275 if left_val < right_val {
276 gap_is_negative = true
277 gap_size = right_val - left_val
278 } else {
279 gap_size = left_val - right_val
280 }
281
282 default:
283 zero_gap, _ := newGapValue(uint64(0), false).(gapValue[uint64])
284 return zero_gap
285 }
286
287 this_gap, _ := newGapValue(gap_size, gap_is_negative).(gapValue[uint64])
288 return this_gap
289 } // MakeGap
290
291 func makeFloatGap[Op operandConstraint](operand numericOperands[Op]) gapValue[float64] {
292 switch this_op := any(operand).(type) {
293 case numericOperands[float64]:
294 left_val := this_op.Left
295 right_val := this_op.Right
296 gap_is_negative := false
297 var gap_size float64
298 if left_val < right_val {
299 gap_is_negative = true
300 gap_size = right_val - left_val
301 } else {
302 gap_size = left_val - right_val
303 }
304
305 this_gap, _ := newGapValue(gap_size, gap_is_negative).(gapValue[float64])
306 return this_gap
307
308 default:
309 zero_gap, _ := newGapValue(float64(0.0), false).(gapValue[float64])
310 return zero_gap
311 }
312 } // MakeFloatGap