# Merge pull request #42 from entireio/soph/multi-have

`9eefade`→[main](/content/gh/entireio/git-sync/commits/main/index.html)·

Soph·2mo ago·2 files·+102 added/-0 removed

Recombine checkpoints to recover pack granularity after heavy regions

## Changes

2

- internal/strategy/bootstrap

- Mbootstrap.go+48

- Mbootstrap_test.go+54

```
578 unmodified lines

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
378 unmodified lines

984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016

578 unmodified lines

current = checkpoint
		pushedCheckpoints = append(pushedCheckpoints, checkpoint)
		result.BatchCount++

// Recombine: subdivision is a one-way ratchet, so the fine
		// granularity needed for one heavy commit sticks around for
		// the rest of the chain even when the deltas afterward are
		// tiny. Aim for the next pack to be roughly half the target
		// limit by dropping enough checkpoints that the span doubles
		// approximately log2(target/2 / sent) times. If we
		// overshoot, the abort-early + subdivision path re-splits.
		// Leave at least the final checkpoint after idx (it carries
		// the SourceHash cutover), so the cap is len-idx-2.
		if dropCount := recombineDropCount(sentBytes, p.TargetMaxPack, len(batch.Checkpoints)-idx-2); dropCount > 0 {
			dropped := batch.Checkpoints[idx+1]
			batch.Checkpoints = append(batch.Checkpoints[:idx+1], batch.Checkpoints[idx+1+dropCount:]...)
			p.log("bootstrap batch recombining after small push",
				"branch", batch.Plan.TargetRef.String(),
				"sent_bytes", sentBytes,
				"target_limit_bytes", p.TargetMaxPack,
				"dropped_count", dropCount,
				"first_dropped_checkpoint", planner.ShortHash(dropped),
				"remaining_checkpoints", len(batch.Checkpoints))
		}
		idx++
	}

378 unmodified lines

return -1
}

// recombineDropCount picks how many of the upcoming checkpoints to
// drop after a small successful push. Each dropped checkpoint roughly
// doubles the span of the next pack — so doubling sentBytes until
// hitting target/2 gives the count. Capped by maxDrop (always leave
// at least one checkpoint ahead, including the final one) and by a
// hard ceiling that keeps any single overshoot's recovery cost
// bounded. Returns 0 when sentBytes already used at least half the
// limit, when we have no headroom to estimate, or when nothing can be
// dropped.
func recombineDropCount(sentBytes, targetLimit int64, maxDrop int) int {
	const hardCap = 8
	if sentBytes <= 0 || targetLimit <= 0 || maxDrop <= 0 {
		return 0
	}
	target := targetLimit / 2
	if sentBytes >= target {
		return 0
	}
	count := 0
	span := sentBytes
	for span*2 <= target && count < maxDrop && count < hardCap {
		span *= 2
		count++
	}
	return count
}

// minBytesBeforeAbort is the floor below which the projection-based
// abort heuristic stays silent. The first few KB of a pack are header
// + small objects; their bytes/object ratio doesn't represent the
```

Minternal/strategy/bootstrap/bootstrap.go+48

```
852 unmodified lines

853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912

852 unmodified lines

}
}

func TestRecombineDropCount(t *testing.T) {
	t.Parallel()
	const limit = 50_000_000
	cases := []struct {
		name      string
		sentBytes int64
		limit     int64
		maxDrop   int
		want      int
	}{
		{
			// Pack used over half the limit: span already in the right
			// ballpark, no recombination.
			name: "above half target, no drop", sentBytes: 30_000_000, limit: limit, maxDrop: 100, want: 0,
		},
		{
			// Just under half: one doubling overshoots, so no drop.
			name: "just under half overshoots on double, no drop", sentBytes: 13_000_000, limit: limit, maxDrop: 100, want: 0,
		},
		{
			// 1 MB pack out of 50 MB limit: aim for 25 MB. log2(25/1) ≈ 4.6 → 4 doublings.
			name: "small pack ramps several doublings", sentBytes: 1_000_000, limit: limit, maxDrop: 100, want: 4,
		},
		{
			// 6.2 KB pack (the case from the trace): aim for 25 MB.
			// log2(25_000_000/6200) ≈ 11.97 → capped at hardCap=8.
			name: "tiny pack hits hard cap", sentBytes: 6_200, limit: limit, maxDrop: 100, want: 8,
		},
		{
			// Same tiny pack but only 3 checkpoints to drop: respect maxDrop.
			name: "maxDrop limits drop count", sentBytes: 6_200, limit: limit, maxDrop: 3, want: 3,
		},
		{
			name: "no headroom returns zero", sentBytes: 0, limit: limit, maxDrop: 100, want: 0,
		},
		{
			name: "no limit returns zero", sentBytes: 1024, limit: 0, maxDrop: 100, want: 0,
		},
		{
			name: "no slack returns zero", sentBytes: 1024, limit: limit, maxDrop: 0, want: 0,
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			t.Parallel()
			got := recombineDropCount(c.sentBytes, c.limit, c.maxDrop)
			if got != c.want {
				 t.Errorf("recombineDropCount(%d, %d, %d) = %d, want %d",
					c.sentBytes, c.limit, c.maxDrop, got, c.want)
			}
		})
	}
}

func TestPackStreamObserverTracksBytes(t *testing.T) {
	t.Parallel()
	body := []byte("a packfile worth of bytes")
