# strategy: rename EnsureMetadataBranch to EnsurePrimaryRef

`00066c0`→[main](/content/gh/entireio/cli/commits/main/index.html)·

pfleidi·1mo ago·4 files·+69 added/-67 removed

The function now operates on refs.Primary, not the v1 branch literal.
Local ref, remote-tracking name, and user-facing output use Primary's
short name. The origin-bootstrap path is gated on PrimaryFetchableFromOrigin
so refs that aren't pushed skip it and create an empty orphan directly.

## Sessions

d72a9bae0615View transcript

## Changes

4

- cmd/entire/cli

- Mresume_test.go+2/-2

- strategy

- Mcommon.go+33/-31

- Mcommon_test.go+26/-26

- Mmetadata_reconcile_test.go+8/-8

```
151 unmodified lines

152
153
154
155
155
156
157
158
182 unmodified lines

341
342
343
344
344
345
346
347

151 unmodified lines

}

// Ensure entire/checkpoints/v1 branch exists
  if err := strategy.EnsureMetadataBranch(t.Context(), repo); err != nil {
  if err := strategy.EnsurePrimaryRef(t.Context(), repo); err != nil {
    t.Fatalf("Failed to create metadata branch: %v", err)
  }

182 unmodified lines

t.Helper()

// Get existing metadata branch or create it
  if err := strategy.EnsureMetadataBranch(t.Context(), repo); err != nil {
  if err := strategy.EnsurePrimaryRef(t.Context(), repo); err != nil {
    t.Fatalf("Failed to ensure metadata branch: %v", err)
  }
  ``

Mcmd/entire/cli/resume_test.go+2/-2

```
75 unmodified lines

76
77
78
79
79
80
81
82
366 unmodified lines

449
450
451
452
453
454
452
453
454
455
456
456
457
458
458
459
460
461
462
463
464
460
461
462
463
464
465
466
467
468
469
467
468
470
471
472
470
473
474
475
476
474
477
478
479
480
481
479
482
483
481
484
485
486
487
488
486
489
490
491
492
2 unmodified lines

495
496
497
495
498
499
500
498
499
501
502
503
501
504
505
503
506
507
508
509
507
510
511
512
513
5 unmodified lines

519
520
521
519
522
523
524
525
9 unmodified lines

535
536
537
535
538
539
540
541
542
540
543
544
545
543
546
547
548
549
6 unmodified lines

556
557
558
556
559
558
560
561
562
561
563
564
565
566

75 unmodified lines

if err := vercelconfig.InitSettings(ctx); err != nil {
    return fmt.Errorf("failed to initialize vercel settings: %w", err)
  }
  if err := EnsureMetadataBranch(ctx, repo); err != nil {
  if err := EnsurePrimaryRef(ctx, repo); err != nil {
    return fmt.Errorf("failed to ensure metadata branch: %w", err)
  }

366 unmodified lines

return ctxAgentType
}

// EnsureMetadataBranch creates or updates the local entire/checkpoints/v1 branch.
// If the remote-tracking branch (origin/entire/checkpoints/v1) exists and the local
// branch is missing or empty, creates/updates the local branch from it.
// EnsurePrimaryRef creates or updates the local primary metadata ref. If
// Primary is in Push (i.e. origin tracks it) and the local ref is missing or
// empty, creates/updates the local ref from origin's remote-tracking ref.
// Otherwise creates an empty orphan.
func EnsureMetadataBranch(ctx context.Context, repo *git.Repository) error {
func EnsurePrimaryRef(ctx context.Context, repo *git.Repository) error {
  refs := checkpoint.ResolveCommittedRefs(ctx)
  refName := plumbing.NewBranchReferenceName(paths.MetadataBranchName)
  primaryName := refs.Primary.Short()

// Check if remote-tracking branch exists (e.g., after clone/fetch)
  remoteRefName := plumbing.NewRemoteReferenceName("origin", paths.MetadataBranchName)
  remoteRef, remoteErr := repo.Reference(remoteRefName, true)
  if remoteErr != nil && !errors.Is(remoteErr, plumbing.ErrReferenceNotFound) {
    return fmt.Errorf("failed to check remote metadata branch: %w", remoteErr)
  // Origin only tracks Primary when Primary is in Push.
  var remoteRef *plumbing.Reference
  if refs.PrimaryFetchableFromOrigin() {
    var remoteErr error
    remoteRef, remoteErr = repo.Reference(plumbing.NewRemoteReferenceName("origin", primaryName), true)
    if remoteErr != nil && !errors.Is(remoteErr, plumbing.ErrReferenceNotFound) {
      return fmt.Errorf("failed to check remote metadata ref: %w", remoteErr)
    }
  }

// Check if local branch already exists
  localRef, err := repo.Reference(refName, true)
  // Check if local ref already exists
  localRef, err := repo.Reference(refs.Primary, true)
  if err == nil {
    if remoteErr == nil && localRef.Hash() != remoteRef.Hash() {
    if remoteRef != nil && localRef.Hash() != remoteRef.Hash() {
      // Local and remote exist but differ — determine relationship
      isEmpty, checkErr := isEmptyMetadataBranch(repo, localRef)
      if checkErr != nil {
        return fmt.Errorf("failed to check metadata branch contents: %w", checkErr)
        return fmt.Errorf("failed to check metadata ref contents: %w", checkErr)
      }
      if isEmpty {
        // Empty orphan — just point to remote
        if setErr := AdvanceCommittedPrimary(ctx, repo, refs, remoteRef.Hash()); setErr != nil {
          return fmt.Errorf("failed to update metadata branch from remote: %w", setErr)
          return fmt.Errorf("failed to update metadata ref from remote: %w", setErr)
        }
        fmt.Fprintf(os.Stderr, "[entire] Updated local branch '%s' from origin\n", paths.MetadataBranchName)
        fmt.Fprintf(os.Stderr, "[entire] Updated local ref '%s' from origin\n", primaryName)
      } else {
        // Local has real data and differs from remote — if disconnected
        // (no common ancestor), reconciliation happens at pre-push time
        // or via 'entire doctor'. Read paths warn but do not auto-fix.
        logging.Debug(ctx, "metadata branch differs from remote, reconciliation deferred to read/write time",
        logging.Debug(ctx, "metadata ref differs from remote, reconciliation deferred to read/write time",
          "local_hash", localRef.Hash().String()[:7],
          "remote_hash", remoteRef.Hash().String()[:7],
        )

return nil
}
if !errors.Is(err, plumbing.ErrReferenceNotFound) {
  return fmt.Errorf("failed to check metadata branch: %w", err)
  return fmt.Errorf("failed to check metadata ref: %w", err)
}

// Local branch doesn't exist — create from remote if available
if remoteErr == nil {
// Local ref doesn't exist — create from remote if available
if remoteRef != nil {
  if err := AdvanceCommittedPrimary(ctx, repo, refs, remoteRef.Hash()); err != nil {
    return fmt.Errorf("failed to create metadata branch from remote: %w", err)
    return fmt.Errorf("failed to create metadata ref from remote: %w", err)
  }
  fmt.Fprintf(os.Stderr, "✓ Created local branch '%s' from origin\n", paths.MetadataBranchName)
  fmt.Fprintf(os.Stderr, "✓ Created local ref '%s' from origin\n", primaryName)
  return nil
}

// No local or remote branch — create empty orphan
// No local ref and nothing to bootstrap from — create empty orphan
emptyTree := &object.Tree{Entries: []object.TreeEntry{}}
obj := repo.Storer.NewEncodedObject()
if err := emptyTree.Encode(obj); err != nil {

emptyTreeHash, err = vercelconfig.MaybeMergeMetadataBranchConfig(repo, emptyTreeHash)
if err != nil {
  return fmt.Errorf("failed to initialize metadata branch vercel config: %w", err)
  return fmt.Errorf("failed to initialize metadata ref vercel config: %w", err)
}

// Create orphan commit (no parent)

TreeHash:  emptyTreeHash,
Author:    sig,
Committer: sig,
Message:   "Initialize metadata branch\n\nThis branch stores session metadata.\n",
Message:   "Initialize metadata ref\n\nThis ref stores session metadata.\n",
}
// Note: No ParentHashes - this is an orphan commit

// Sign the orphan commit when signing is enabled, matching the path used
// for every other metadata-branch commit (see metadata_reconcile.go and
// for every other metadata commit (see metadata_reconcile.go and
// push_common.go). Without this, repos that enforce a "verified
// signatures" ruleset on entire/* refs reject the very first push of
// the metadata branch with GH013, even though every later commit on it
// the metadata ref with GH013, even though every later commit on it
// is correctly signed.
checkpoint.SignCommitBestEffort(ctx, commit)

return fmt.Errorf("failed to store orphan commit: %w", err)
}

// Create branch reference
if err := AdvanceCommittedPrimary(ctx, repo, refs, commitHash); err != nil {
  return fmt.Errorf("failed to create metadata branch: %w", err)
  return fmt.Errorf("failed to create metadata ref: %w", err)
}

fmt.Fprintf(os.Stderr, "  ✓ Created orphan branch %s for session metadata\n", paths.MetadataBranchName)
fmt.Fprintf(os.Stderr, "  ✓ Created orphan ref %s for session metadata\n", primaryName)
return nil
}
```

Mcmd/entire/cli/strategy/common.go+33/-31

```
951 unmodified lines

952
953
954
955
955
956
957
958
9 unmodified lines

968
969
970
971
972
971
972
973
974
975
56 unmodified lines

1032
1033
1034
1035
1036
1035
1036
1037
1038
1039
14 unmodified lines

1054
1055
1056
1057
1058
1057
1058
1059
1060
1061
14 unmodified lines

1076
1077
1078
1079
1079
1080
1081
1082
15 unmodified lines

1098
1099
1100
1101
1102
1101
1102
1103
1104
1105
26 unmodified lines

1132
1133
1134
1135
1135
1136
1137
1138
9 unmodified lines

1148
1149
1150
1151
1151
1152
1153
1154
1155
1156
1157
1157
1158
1159
1160
19 unmodified lines

1180
1181
1182
1183
1183
1184
1185
1186
21 unmodified lines

1208
1209
1210
1211
1211
1212
1213
1214
1215
1216
1217
1218
1219
1218
1219
1220
1221
1222
1222
1223
1224
1225
1 unmodified line

1227
1228
1229
1230
1230
1231
1232
1233
1234
1234
1235
1236
1237
4 unmodified lines

1242
1243
1244
1245
1246
1245
1246
1247
1248
1249
32 unmodified lines

1282
1283
1284
1285
1286
1285
1286
1287
1288
1289
1289
1290
1291
1292
1293
1294
1295
1296
1296
1297
1298
1299

951 unmodified lines

return bareDir
}

func TestEnsureMetadataBranch(t *testing.T) {
func TestEnsurePrimaryRef(t *testing.T) {
  t.Parallel()

t.Run("creates from remote on fresh clone", func(t *testing.T) {
9 unmodified lines

t.Fatalf("failed to open repo: %v", err)
      }

if err := EnsureMetadataBranch(t.Context(), repo); err != nil {
      t.Fatalf("EnsureMetadataBranch() failed: %v", err)
    if err := EnsurePrimaryRef(t.Context(), repo); err != nil {
      t.Fatalf("EnsurePrimaryRef() failed: %v", err)
    }

// Local branch should exist with data (not empty)
56 unmodified lines

t.Fatalf("failed to set ref: %v", err)
    }

// Should have been updated from remote — no longer empty
14 unmodified lines

if err != nil {
      t.Fatalf("failed to open repo: %v", err)
    }
    if err := EnsureMetadataBranch(t.Context(), repo); err != nil {
      t.Fatalf("EnsureMetadataBranch() failed: %v", err)
    if err := EnsurePrimaryRef(t.Context(), repo); err != nil {
      t.Fatalf("EnsurePrimaryRef() failed: %v", err)
    }

ref, err := repo.Reference(plumbing.NewBranchReferenceName(paths.MetadataBranchName), true)
14 unmodified lines

})
}

func TestEnsureMetadataBranch_WritesVercelConfigWhenEnabled(t *testing.T) {
func TestEnsurePrimaryRef_WritesVercelConfigWhenEnabled(t *testing.T) {
  vercelconfig.ResetSettingsCache()
  t.Cleanup(vercelconfig.ResetSettingsCache)

15 unmodified lines

t.Fatalf("InitSettings() failed: %v", err)
  }

ref, err := repo.Reference(plumbing.NewBranchReferenceName(paths.MetadataBranchName), true)
26 unmodified lines

}

// Not parallel: uses t.Chdir so settings.Load picks up the v1.1 opt-in.
func TestEnsureMetadataBranch_MirrorsV11WhenSeedingFromRemote(t *testing.T) {
func TestEnsurePrimaryRef_MirrorsV11WhenSeedingFromRemote(t *testing.T) {
bareDir := initBareWithMetadataBranch(t)
cloneDir, _ := cloneWithConfig(t, bareDir)

9 unmodified lines

repo, err := git.PlainOpen(cloneDir)
require.NoError(t, err)

require.NoError(t, EnsureMetadataBranch(t.Context(), repo))
require.NoError(t, EnsurePrimaryRef(t.Context(), repo))

v1Ref, err := repo.Reference(plumbing.NewBranchReferenceName(paths.MetadataBranchName), true)
require.NoError(t, err, "local v1 branch should be seeded from origin")

mirrorRef, err := repo.Reference(plumbing.ReferenceName(paths.MetadataRefName), true)
require.NoError(t, err, "v1.1 mirror should track the v1 write performed by EnsureMetadataBranch")
require.NoError(t, err, "v1.1 mirror should track the v1 write performed by EnsurePrimaryRef")
assert.Equal(t, v1Ref.Hash(), mirrorRef.Hash())
}

19 unmodified lines

return cloneDir, run
}

func TestEnsureMetadataBranch_DisconnectedBranchesNotReconciledInEnable(t *testing.T) {
func TestEnsurePrimaryRef_DisconnectedBranchesNotReconciledInEnable(t *testing.T) {
  t.Parallel()

bareDir := initBareWithMetadataBranch(t)
21 unmodified lines

t.Fatalf("failed to open repo: %v", err)
  }

// Get local ref hash before EnsureMetadataBranch
  // Get local ref hash before EnsurePrimaryRef
  refName := plumbing.NewBranchReferenceName(paths.MetadataBranchName)
  localRefBefore, err := repo.Reference(refName, true)
  if err != nil {
    t.Fatalf("local branch not found: %v", err)
  }

// EnsureMetadataBranch should NOT reconcile disconnected branches.
    // EnsurePrimaryRef should NOT reconcile disconnected branches.
    // Reconciliation happens at pre-push time or via 'entire doctor'.
    // The local branch should be unchanged.
    localRefAfter, err := repo.Reference(refName, true)
1 unmodified line

t.Fatalf("local branch not found: %v", err)
  }
  if localRefAfter.Hash() != localRefBefore.Hash() {
    t.Error("EnsureMetadataBranch should not modify disconnected local branch with real data")
    t.Error("EnsurePrimaryRef should not modify disconnected local branch with real data")
  }
}

func TestEnsureMetadataBranch_DoesNotFastForwardWhenBehind(t *testing.T) {
func TestEnsurePrimaryRef_DoesNotFastForwardWhenBehind(t *testing.T) {
  t.Parallel()

bareDir := initBareWithMetadataBranch(t)
4 unmodified lines

if err != nil {
    t.Fatalf("failed to open repo: %v", err)
  }
  if err := EnsureMetadataBranch(t.Context(), repo); err != nil {
    t.Fatalf("first EnsureMetadataBranch() failed: %v", err)
    if err := EnsurePrimaryRef(t.Context(), repo); err != nil {
      t.Fatalf("first EnsurePrimaryRef() failed: %v", err)
    }

// Remember current local hash
32 unmodified lines

t.Fatalf("failed to reopen repo: %v", err)
  }

if err := EnsureMetadataBranch(t.Context(), repo); err != nil {
    t.Fatalf("second EnsureMetadataBranch() failed: %v", err)
    if err := EnsurePrimaryRef(t.Context(), repo); err != nil {
      t.Fatalf("second EnsurePrimaryRef() failed: %v", err)
    }

// EnsureMetadataBranch no longer fast-forwards diverged branches (handled by push path).
    // EnsurePrimaryRef no longer fast-forwards diverged branches (handled by push path).
    // Local should be unchanged since it has real data and shares ancestry with remote.
    localAfter, err := repo.Reference(refName, true)
    if err != nil {
      t.Fatalf("local branch not found: %v", err)
    }
    if localAfter.Hash() != localBefore.Hash() {
      t.Error("EnsureMetadataBranch should not modify local branch with shared ancestry")
      t.Error("EnsurePrimaryRef should not modify local branch with shared ancestry")
    }
  }
}
