fix token percent overflow · Entire

fix token percent overflow

00b6077→main

Sessions

10bc0917044fView transcript

Changes

2

5 unmodified lines

6
7
8
9
10
11
12
224 unmodified lines

237
238
239
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

5 unmodified lines

"errors"
    "fmt"
    "io"
    "math/bits"
    "strings"

"github.com/entireio/cli/cmd/entire/cli/agent"
224 unmodified lines

if total <= 0 {
        return 0
    }
    return (value*100 + total/2) / total
    if value <= 0 {
        return 0
    }

hi, lo := bits.Mul64(uint64(value), 100)
    lo, carry := bits.Add64(lo, uint64(total)/2, 0)
    hi += carry

divisor := uint64(total)
    if hi >= divisor {
        return int(^uint(0) >> 1)
    }
    quotient, _ := bits.Div64(hi, lo, divisor)
    maxInt := uint64(^uint(0) >> 1)
    if quotient > maxInt {
        return int(maxInt)
    }
    return int(quotient)
}

func recommendationRules(signals tokenRecommendationSignals) []sessionTokensRecommendation {

Mcmd/entire/cli/session_tokens.go+18/-1

1343 unmodified lines

1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359

1343 unmodified lines

}
}

func TestRoundedPercentAvoidsIntermediateOverflow(t *testing.T) {
    t.Parallel()

maxInt := int(^uint(0) >> 1)

if got := roundedPercent(maxInt/2, maxInt); got != 50 {
        t.Fatalf("roundedPercent() = %d, want 50", got)
    }
}

// --- checkpoint tokens tests ---

func TestCheckpointTokensCmd_TextOutputWithRealCheckpointShape(t *testing.T) {