Skip to content

Commit 184a7ab

Browse files
levbclaude
andcommitted
fix(storage): re-resolve merge conflicts from main
The initial merge left unresolved conflict markers and didn't account for main's []*BuildMap → []BuildMap migration (PR #2319) or the simplified mapping resolution (PR #2318). This properly reconciles our compression branch (FrameTable support, BuildFiles, V4 format) with those changes: value-type BuildMap slices, sort.Search lookup, and ApplyFrames as a standalone function to avoid mixed receivers. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8614831 commit 184a7ab

File tree

8 files changed

+59
-161
lines changed

8 files changed

+59
-161
lines changed

packages/orchestrator/pkg/sandbox/build_upload.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func (p *PendingBuildInfo) applyToHeader(h *headers.Header, fileType string) err
200200
// Track frame cursor per build to avoid O(N²) rescanning.
201201
cursors := make(map[string]int)
202202

203-
for _, mapping := range h.Mapping {
203+
for i := range h.Mapping {
204+
mapping := &h.Mapping[i]
204205
key := pendingBuildInfoKey(mapping.BuildId.String(), fileType)
205206
info := p.get(key)
206207

@@ -209,7 +210,7 @@ func (p *PendingBuildInfo) applyToHeader(h *headers.Header, fileType string) err
209210
}
210211

211212
cursor := cursors[key]
212-
next, err := mapping.SetFrames(info.ft, cursor)
213+
next, err := headers.ApplyFrames(mapping, info.ft, cursor)
213214
if err != nil {
214215
return fmt.Errorf("apply frames to mapping at offset %d for build %s: %w",
215216
mapping.Offset, mapping.BuildId.String(), err)

packages/shared/pkg/storage/header/mapping.go

Lines changed: 20 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,12 @@ type BuildMap struct {
2222
FrameTable *storage.FrameTable
2323
}
2424

25-
<<<<<<< HEAD
26-
func (mapping *BuildMap) Copy() *BuildMap {
27-
return &BuildMap{
28-
Offset: mapping.Offset,
29-
Length: mapping.Length,
30-
BuildId: mapping.BuildId,
31-
BuildStorageOffset: mapping.BuildStorageOffset,
32-
FrameTable: mapping.FrameTable,
33-
}
34-
}
35-
36-
// SetFrames associates compression frame information with this mapping,
37-
// starting the scan from frame index `from` and
38-
// returning the next cursor position. Use this when applying frames to a
39-
// sorted sequence of mappings to avoid O(N²) rescanning.
40-
func (mapping *BuildMap) SetFrames(frameTable *storage.FrameTable, from int) (int, error) {
25+
// setFrames associates compression frame information with mapping,
26+
// ApplyFrames extracts the FrameTable subset that covers mapping's storage range,
27+
// assigns it to mapping.FrameTable, and returns the next cursor position.
28+
// Use cursor-chaining when applying frames to a sorted sequence of mappings
29+
// to avoid O(N²) rescanning.
30+
func ApplyFrames(mapping *BuildMap, frameTable *storage.FrameTable, from int) (int, error) {
4131
if frameTable == nil {
4232
return from, nil
4333
}
@@ -58,8 +48,6 @@ func (mapping *BuildMap) SetFrames(frameTable *storage.FrameTable, from int) (in
5848
return next, nil
5949
}
6050

61-
=======
62-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
6351
func CreateMapping(
6452
buildId *uuid.UUID,
6553
dirty *bitset.BitSet,
@@ -114,15 +102,9 @@ func CreateMapping(
114102
//
115103
// It returns a new set of mappings that covers the whole size.
116104
func MergeMappings(
117-
<<<<<<< HEAD
118-
baseMapping []*BuildMap,
119-
diffMapping []*BuildMap,
120-
) ([]*BuildMap, error) {
121-
=======
122105
baseMapping []BuildMap,
123106
diffMapping []BuildMap,
124-
) []BuildMap {
125-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
107+
) ([]BuildMap, error) {
126108
if len(diffMapping) == 0 {
127109
return baseMapping, nil
128110
}
@@ -195,24 +177,19 @@ func MergeMappings(
195177
frameCursor := 0
196178

197179
if leftBaseLength > 0 {
198-
mappings = append(mappings, BuildMap{
199-
Offset: base.Offset,
200-
Length: uint64(leftBaseLength),
201-
BuildId: base.BuildId,
202-
// the build storage offset is the same as the base mapping
180+
leftBase := BuildMap{
181+
Offset: base.Offset,
182+
Length: uint64(leftBaseLength),
183+
BuildId: base.BuildId,
203184
BuildStorageOffset: base.BuildStorageOffset,
204-
<<<<<<< HEAD
205185
}
206186
var err error
207-
frameCursor, err = leftBase.SetFrames(base.FrameTable, 0)
187+
frameCursor, err = ApplyFrames(&leftBase, base.FrameTable, 0)
208188
if err != nil {
209189
return nil, fmt.Errorf("set frames for left split at offset %d: %w", leftBase.Offset, err)
210190
}
211191

212192
mappings = append(mappings, leftBase)
213-
=======
214-
})
215-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
216193
}
217194

218195
mappings = append(mappings, diff)
@@ -223,20 +200,17 @@ func MergeMappings(
223200
rightBaseLength := int64(base.Length) - rightBaseShift
224201

225202
if rightBaseLength > 0 {
226-
baseMapping[baseIdx] = BuildMap{
203+
rightBase := BuildMap{
227204
Offset: base.Offset + uint64(rightBaseShift),
228205
Length: uint64(rightBaseLength),
229206
BuildId: base.BuildId,
230207
BuildStorageOffset: base.BuildStorageOffset + uint64(rightBaseShift),
231208
}
232-
<<<<<<< HEAD
233-
if _, err := rightBase.SetFrames(base.FrameTable, frameCursor); err != nil {
209+
if _, err := ApplyFrames(&rightBase, base.FrameTable, frameCursor); err != nil {
234210
return nil, fmt.Errorf("set frames for right split at offset %d: %w", rightBase.Offset, err)
235211
}
236212

237213
baseMapping[baseIdx] = rightBase
238-
=======
239-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
240214
} else {
241215
baseIdx++
242216
}
@@ -256,20 +230,17 @@ func MergeMappings(
256230
rightBaseLength := int64(base.Length) - rightBaseShift
257231

258232
if rightBaseLength > 0 {
259-
baseMapping[baseIdx] = BuildMap{
233+
rightBase := BuildMap{
260234
Offset: base.Offset + uint64(rightBaseShift),
261235
Length: uint64(rightBaseLength),
262236
BuildId: base.BuildId,
263237
BuildStorageOffset: base.BuildStorageOffset + uint64(rightBaseShift),
264238
}
265-
<<<<<<< HEAD
266-
if _, err := rightBase.SetFrames(base.FrameTable, 0); err != nil {
239+
if _, err := ApplyFrames(&rightBase, base.FrameTable, 0); err != nil {
267240
return nil, fmt.Errorf("set frames for right split at offset %d: %w", rightBase.Offset, err)
268241
}
269242

270243
baseMapping[baseIdx] = rightBase
271-
=======
272-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
273244
} else {
274245
baseIdx++
275246
}
@@ -283,21 +254,17 @@ func MergeMappings(
283254
leftBaseLength := int64(diff.Offset) - int64(base.Offset)
284255

285256
if leftBaseLength > 0 {
286-
mappings = append(mappings, BuildMap{
257+
leftBase := BuildMap{
287258
Offset: base.Offset,
288259
Length: uint64(leftBaseLength),
289260
BuildId: base.BuildId,
290261
BuildStorageOffset: base.BuildStorageOffset,
291-
<<<<<<< HEAD
292262
}
293-
if _, err := leftBase.SetFrames(base.FrameTable, 0); err != nil {
263+
if _, err := ApplyFrames(&leftBase, base.FrameTable, 0); err != nil {
294264
return nil, fmt.Errorf("set frames for left split at offset %d: %w", leftBase.Offset, err)
295265
}
296266

297267
mappings = append(mappings, leftBase)
298-
=======
299-
})
300-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
301268
}
302269

303270
baseIdx++
@@ -315,55 +282,34 @@ func MergeMappings(
315282
}
316283

317284
// NormalizeMappings joins adjacent mappings that have the same buildId.
318-
<<<<<<< HEAD
319285
// When merging mappings, FrameTables are also merged by extending the first
320286
// mapping's FrameTable with frames from subsequent mappings.
321-
func NormalizeMappings(mappings []*BuildMap) []*BuildMap {
322-
=======
323287
func NormalizeMappings(mappings []BuildMap) []BuildMap {
324-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
325288
if len(mappings) == 0 {
326289
return nil
327290
}
328291

329292
result := make([]BuildMap, 0, len(mappings))
330293

331-
<<<<<<< HEAD
332-
// Start with a copy of the first mapping (Copy() now includes FrameTable)
333-
current := mappings[0].Copy()
294+
current := mappings[0]
334295

335296
for i := 1; i < len(mappings); i++ {
336297
mp := mappings[i]
337298
if mp.BuildId != current.BuildId {
338-
// BuildId changed, add the current map to results and start a new one
339299
result = append(result, current)
340-
current = mp.Copy() // New copy (includes FrameTable)
300+
current = mp
341301
} else {
342302
// Same BuildId, merge: add the length and extend FrameTable
343303
current.Length += mp.Length
344304

345305
// Extend FrameTable if the mapping being merged has one
346306
if mp.FrameTable != nil {
347307
if current.FrameTable == nil {
348-
// Current has no FrameTable but merged one does - take it
349308
current.FrameTable = mp.FrameTable
350309
} else {
351-
// Both have FrameTables - extend current's with mp's frames
352-
// The frames are contiguous subsets, so we append non-overlapping frames
353310
current.FrameTable = mergeFrameTables(current.FrameTable, mp.FrameTable)
354311
}
355312
}
356-
=======
357-
current := mappings[0]
358-
359-
for i := 1; i < len(mappings); i++ {
360-
mp := mappings[i]
361-
if mp.BuildId == current.BuildId {
362-
current.Length += mp.Length
363-
} else {
364-
result = append(result, current)
365-
current = mp
366-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
367313
}
368314
}
369315

packages/shared/pkg/storage/header/mapping_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ func makeFrameTable(n int, frameU, frameC int32) *storage.FrameTable {
727727
return ft
728728
}
729729

730-
func assertFrameTable(t *testing.T, label string, m *BuildMap, startU, startC int64, nFrames int, frameU, frameC int32) {
730+
func assertFrameTable(t *testing.T, label string, m BuildMap, startU, startC int64, nFrames int, frameU, frameC int32) {
731731
t.Helper()
732732

733733
require.NotNil(t, m.FrameTable, "%s: FrameTable should not be nil", label)
@@ -756,21 +756,21 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
756756
baseFT := makeFrameTable(6, frameU, frameC)
757757

758758
tests := map[string]struct {
759-
base []*BuildMap
760-
diff []*BuildMap
761-
validate func(t *testing.T, merged []*BuildMap)
759+
base []BuildMap
760+
diff []BuildMap
761+
validate func(t *testing.T, merged []BuildMap)
762762
}{
763763
"diff inside base — left and right get correct frame subsets": {
764-
base: []*BuildMap{{
764+
base: []BuildMap{{
765765
Offset: 0, Length: 6 * blockSize,
766766
BuildId: compBaseID, BuildStorageOffset: 0,
767767
FrameTable: baseFT,
768768
}},
769-
diff: []*BuildMap{{
769+
diff: []BuildMap{{
770770
Offset: 2 * blockSize, Length: 2 * blockSize,
771771
BuildId: compDiffID,
772772
}},
773-
validate: func(t *testing.T, m []*BuildMap) {
773+
validate: func(t *testing.T, m []BuildMap) {
774774
t.Helper()
775775
require.Len(t, m, 3)
776776

@@ -788,19 +788,19 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
788788
},
789789

790790
"base after diff with overlap — right split keeps tail frames": {
791-
base: []*BuildMap{
791+
base: []BuildMap{
792792
{Offset: 0, Length: 1 * blockSize, BuildId: plainID},
793793
{
794794
Offset: 1 * blockSize, Length: 4 * blockSize,
795795
BuildId: compBaseID, BuildStorageOffset: 0,
796796
FrameTable: makeFrameTable(4, frameU, frameC),
797797
},
798798
},
799-
diff: []*BuildMap{{
799+
diff: []BuildMap{{
800800
Offset: 0, Length: 3 * blockSize,
801801
BuildId: compDiffID,
802802
}},
803-
validate: func(t *testing.T, m []*BuildMap) {
803+
validate: func(t *testing.T, m []BuildMap) {
804804
t.Helper()
805805
require.Len(t, m, 2)
806806

@@ -814,19 +814,19 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
814814
},
815815

816816
"diff after base with overlap — left split keeps head frames": {
817-
base: []*BuildMap{
817+
base: []BuildMap{
818818
{
819819
Offset: 0, Length: 4 * blockSize,
820820
BuildId: compBaseID, BuildStorageOffset: 0,
821821
FrameTable: makeFrameTable(4, frameU, frameC),
822822
},
823823
{Offset: 4 * blockSize, Length: 2 * blockSize, BuildId: plainID},
824824
},
825-
diff: []*BuildMap{{
825+
diff: []BuildMap{{
826826
Offset: 2 * blockSize, Length: 4 * blockSize,
827827
BuildId: compDiffID,
828828
}},
829-
validate: func(t *testing.T, m []*BuildMap) {
829+
validate: func(t *testing.T, m []BuildMap) {
830830
t.Helper()
831831
require.Len(t, m, 2)
832832

@@ -839,16 +839,16 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
839839
},
840840

841841
"two diffs split same base into three pieces": {
842-
base: []*BuildMap{{
842+
base: []BuildMap{{
843843
Offset: 0, Length: 6 * blockSize,
844844
BuildId: compBaseID, BuildStorageOffset: 0,
845845
FrameTable: baseFT,
846846
}},
847-
diff: []*BuildMap{
847+
diff: []BuildMap{
848848
{Offset: 1 * blockSize, Length: 1 * blockSize, BuildId: compDiffID},
849849
{Offset: 4 * blockSize, Length: 1 * blockSize, BuildId: compDiffID},
850850
},
851-
validate: func(t *testing.T, m []*BuildMap) {
851+
validate: func(t *testing.T, m []BuildMap) {
852852
t.Helper()
853853
require.Len(t, m, 5)
854854

@@ -863,15 +863,15 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
863863
},
864864

865865
"nil FrameTable base — splits work without frames": {
866-
base: []*BuildMap{{
866+
base: []BuildMap{{
867867
Offset: 0, Length: 4 * blockSize,
868868
BuildId: compBaseID, BuildStorageOffset: 0,
869869
}},
870-
diff: []*BuildMap{{
870+
diff: []BuildMap{{
871871
Offset: 1 * blockSize, Length: 2 * blockSize,
872872
BuildId: compDiffID,
873873
}},
874-
validate: func(t *testing.T, m []*BuildMap) {
874+
validate: func(t *testing.T, m []BuildMap) {
875875
t.Helper()
876876
require.Len(t, m, 3)
877877
assert.Nil(t, m[0].FrameTable)
@@ -883,12 +883,12 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
883883
// Simulates a real multi-layer header: three builds (A, B, C) each
884884
// with their own FrameTable. A diff lands inside build B, splitting
885885
// it. Builds A and C must pass through with FrameTables intact.
886-
base: func() []*BuildMap {
886+
base: func() []BuildMap {
887887
buildA := uuid.New()
888888
buildB := compBaseID
889889
buildC := uuid.New()
890890

891-
return []*BuildMap{
891+
return []BuildMap{
892892
{
893893
Offset: 0, Length: 2 * blockSize,
894894
BuildId: buildA, BuildStorageOffset: 0,
@@ -906,11 +906,11 @@ func TestMergeMappings_FrameTableSplits(t *testing.T) {
906906
},
907907
}
908908
}(),
909-
diff: []*BuildMap{{
909+
diff: []BuildMap{{
910910
Offset: 3 * blockSize, Length: 2 * blockSize,
911911
BuildId: compDiffID,
912912
}},
913-
validate: func(t *testing.T, m []*BuildMap) {
913+
validate: func(t *testing.T, m []BuildMap) {
914914
t.Helper()
915915
// Expected: A(untouched) | B-left[0..1) | diff | B-right[3..4) | C(untouched)
916916
require.Len(t, m, 5)

packages/shared/pkg/storage/header/metadata.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ type DiffMetadata struct {
8282
func (d *DiffMetadata) toDiffMapping(
8383
ctx context.Context,
8484
buildID uuid.UUID,
85-
<<<<<<< HEAD
86-
) ([]*BuildMap, error) {
87-
=======
88-
) (mapping []BuildMap) {
89-
>>>>>>> f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
85+
) ([]BuildMap, error) {
9086
dirtyMappings := CreateMapping(
9187
&buildID,
9288
d.Dirty,

0 commit comments

Comments
 (0)