# clarify token delta overflow guard

`20f8f75`·

peyton-alt·3w ago·2 files·+54 added/-2 removed

## Sessions

65d01316b402View transcript

## Changes

2

- cmd/entire/cli

- Mcheckpoint_tokens.go+10/-2
  
  - Msessions_test.go+44

```
462 unmodified lines

}

func saturatingIntSub(a, b int) int {
	if b < 0 && a > maxInt()+b {
		return maxInt()
	if b < 0 {
		if b == minInt() {
			if a >= 0 {
				return maxInt()
			}
			return a - b
		}
		if a > maxInt()-(-b) {
			return maxInt()
		}
	}
	if b > 0 && a < minInt()+b {
		return minInt()
```

Mcmd/entire/cli/checkpoint_tokens.go+10/-2

```
2527 unmodified lines

}

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

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

tests := []struct {
		name string
		a    int
		want int
	}{
		{
			name: "clamps non-negative minuend",
			a:    0,
			want: maxInt,
		},
		{
			name: "keeps max exact result",
			a:    -1,
			want: maxInt,
		},
		{
			name: "keeps representable result",
			a:    -2,
			want: maxInt - 1,
		},
		{
			name: "keeps zero exact result",
			a:    minInt,
			want: 0,
		},
	}

for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

if got := saturatingIntSub(tt.a, minInt); got != tt.want {
				t.Fatalf("saturatingIntSub(%d, minInt) = %d, want %d", tt.a, got, tt.want)
			}
		})
	}
}

func TestCheckpointTokensCmd_ComparisonUnavailableWhenBaselineTokenDataMissing(t *testing.T) {
	repo, _ := runExplainAutoTestRepo(t)
	ctx := context.Background()
