perf(strategy): memoize empty-remote guard so it costs nothing post-bootstrap · Entire
perf(strategy): memoize empty-remote guard so it costs nothing post-bootstrap
65e002a→main·
karthik-rameshkumar·3d ago·2 files·+144 added/-0 removed
Addresses review feedback on #1744: deferCheckpointPushOnEmptyRemote ran an ls-remote network round trip per push target on every push, even though it only matters during the first-push window and always returns "publish" once the remote is populated.
Once every push target has been observed with at least one branch, record a fingerprint of that target set in a repo-level marker under the git common dir (.git/entire/checkpoint-push-bootstrap, shared across worktrees). Later pushes short-circuit on the marker instead of probing the network. The fingerprint is order-independent and self-invalidates if the push URLs change, so a reconfigured remote is re-checked. The marker lives outside .git/config to avoid polluting the user's git configuration.
Tests: fingerprint order-independence, and a behavioral proof that the marker skips the network probe (guard returns "publish" against an unreachable remote only via the cached marker; a stale fingerprint falls back to probing).
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Sessions
01KXFYDXDMXBHQAPMYXC3SVEQFView transcript
[?
Fix Checkpoint Push on Empty RemoteClaude Code·Opus 4.8[1m]·4 steps](/content/gh/entireio/cli/session/b46b1cd5-a38d-49b5-9758-c373a036b37f#timeline-01KXFYDXDMXBHQAPMYXC3SVEQF/index.html)
Changes
2
cmd/entire/cli/strategy
Mmanual_commit_push.go+77
Amanual_commit_push_test.go+67
1 unmodified line
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
188 unmodified lines
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
20 unmodified lines
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
1 unmodified line
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"sort"
"strings"
git "github.com/go-git/go-git/v6"
188 unmodified lines
return true
}
// The empty-remote hazard exists only during the first-push window. Once
// every push target has been observed with at least one branch we record a
// fingerprint of that target set locally, so subsequent pushes short-circuit
// here instead of paying an ls-remote network round trip on every push
// forever. The fingerprint self-invalidates if the push URLs change.
fingerprint := pushTargetsFingerprint(targets)
if readPushBootstrapMarker(ctx) == fingerprint {
return false
}
for _, target := range targets {
out, lsErr := checkpointremote.LsRemoteInDir(ctx, dir, target, "refs/heads/*")
if lsErr != nil {
20 unmodified lines
}
}
// Every push target now has a branch; remember it so the network probe above
// is skipped on future pushes for this target set.
writePushBootstrapMarker(ctx, fingerprint)
return false
}
// pushTargetsFingerprint returns a stable, order-independent digest of the push
// targets. Hashing keeps the stored value bounded and avoids writing a push URL
// (which can embed credentials) verbatim to the marker.
func pushTargetsFingerprint(targets []string) string {
sorted := append([]string(nil), targets...)
sort.Strings(sorted)
sum := sha256.Sum256([]byte(strings.Join(sorted, "\n")))
return hex.EncodeToString(sum[:])
}
// pushBootstrapMarkerPath is the repo-level file recording that every resolved
// push target has been observed to carry at least one branch. It lives under
// the git common dir (shared across worktrees) rather than in .git/config so it
// never pollutes the user's git configuration.
func pushBootstrapMarkerPath(ctx context.Context) (string, error) {
commonDir, err := GetGitCommonDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(commonDir, "entire", "checkpoint-push-bootstrap"), nil
}
// readPushBootstrapMarker returns the stored fingerprint, or "" if the marker is
// absent or unreadable.
func readPushBootstrapMarker(ctx context.Context) string {
path, err := pushBootstrapMarkerPath(ctx)
if err != nil {
return ""
}
data, err := os.ReadFile(path) //nolint:gosec // path is git common dir + constant, not user input
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// writePushBootstrapMarker records fingerprint. Best-effort: a failure only
// means the next push re-runs the (correct) network probe, so it warns rather
// than surfacing an error into the push path.
func writePushBootstrapMarker(ctx context.Context, fingerprint string) {
path, err := pushBootstrapMarkerPath(ctx)
if err != nil {
logging.Warn(ctx, "failed to resolve checkpoint push bootstrap marker path",
slog.String("error", err.Error()),
)
return
}
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
logging.Warn(ctx, "failed to create checkpoint push bootstrap marker dir",
slog.String("error", err.Error()),
)
return
}
if err := os.WriteFile(path, []byte(fingerprint+"\n"), 0o600); err != nil {
logging.Warn(ctx, "failed to persist checkpoint push bootstrap marker",
slog.String("error", err.Error()),
)
}
}
// prePushCheckpointRefs drains the per-checkpoint push queue and batch-pushes the
// recorded refs fast-forward-only (git-refs primary; never a force push — a
// diverged ref is recovered via fetch+replay). Transient push failures are logged and
---
Acmd/entire/cli/strategy/manual_commit_push.go+77
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
package strategy
import ( "context" "os/exec" "testing"
checkpointremote "github.com/entireio/cli/cmd/entire/cli/checkpoint/remote" "github.com/entireio/cli/cmd/entire/cli/paths" "github.com/entireio/cli/cmd/entire/cli/testutil"
"github.com/stretchr/testify/require" )
func TestPushTargetsFingerprint_OrderIndependent(t *testing.T) { t.Parallel()
a := pushTargetsFingerprint([]string{"https://example.com/a.git", "https://example.com/b.git"}) b := pushTargetsFingerprint([]string{"https://example.com/b.git", "https://example.com/a.git"}) require.Equal(t, a, b, "fingerprint must not depend on target order")
c := pushTargetsFingerprint([]string{"https://example.com/a.git"}) require.NotEqual(t, a, c, "a different target set must produce a different fingerprint") }
// TestDeferCheckpointPushOnEmptyRemote_BootstrapMarkerSkipsNetwork proves the // memoized fast path avoids the ls-remote probe. The remote points at an // unreachable path, so the guard can only avoid deferring by short-circuiting // on the persisted bootstrap marker rather than inspecting the remote. func TestDeferCheckpointPushOnEmptyRemote_BootstrapMarkerSkipsNetwork(t *testing.T) { // No t.Parallel: uses t.Chdir and the process-wide worktree-root cache. dir := t.TempDir() testutil.InitRepo(t, dir)
// A local path that does not exist: ls-remote fails fast, no network. unreachable := t.TempDir() + "/nonexistent-remote.git" add := exec.CommandContext(t.Context(), "git", "remote", "add", "origin", unreachable) add.Dir = dir require.NoError(t, add.Run())
t.Chdir(dir) paths.ClearWorktreeRootCache() t.Cleanup(paths.ClearWorktreeRootCache)
ctx := context.Background() ps := pushSettings{remote: "origin"}
// Without a marker the guard must probe the (unreachable) remote and fail // closed to deferral. require.True(t, deferCheckpointPushOnEmptyRemote(ctx, ps), "an unreachable remote with no marker must fail closed to deferral")
// Record the marker for the resolved push targets, then the guard must // short-circuit to "publish" without touching the unreachable remote. targets, err := checkpointremote.PushTargetsInDir(ctx, dir, "origin") require.NoError(t, err) writePushBootstrapMarker(ctx, pushTargetsFingerprint(targets))
require.False(t, deferCheckpointPushOnEmptyRemote(ctx, ps), "the bootstrap marker must short-circuit the network probe")
// A stale marker (targets changed) must not short-circuit: it falls back to // probing and defers on the unreachable remote. writePushBootstrapMarker(ctx, "stale-fingerprint") require.True(t, deferCheckpointPushOnEmptyRemote(ctx, ps), "a marker that does not match the current targets must not short-circuit") }