Fetch missing git-refs checkpoints on demand for reads · Entire

Fetch missing git-refs checkpoints on demand for reads

f8f714d·

Soph·2w ago·5 files·+28 added/-8 removed

A git-refs primary previously read only checkpoint refs already present locally, so checkpoints written on another machine (or not yet fetched after clone) read as not-found. FetchCheckpointRef fetches one ref via the checkpoint remote into the local ref of the same name; it's wired into the read commands that already inject FetchBlobsByHash (resume, explain, attribution, tokens profile) via the new OpenOptions.RefFetcher. gitRefsStore.resolveRefMaybeFetch invokes it once on a local miss and retries; a failed fetch resolves to ErrCheckpointNotFound. The git-branch backend ignores it; strategy-side Opens (condensation/rewind) read just-written local refs and pass no fetcher.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

696096934b50View transcript

Changes

5

316 unmodified lines

317
318
319
320
320
321
322
323

316 unmodified lines

return nil, fmt.Errorf("not a git repository: %w", err)
    }

stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return nil, fmt.Errorf("open checkpoint store: %w", err)
    }

Mcmd/entire/cli/attribution.go+1/-1

695 unmodified lines

696
697
698
699
699
700
701
702
173 unmodified lines

876
877
878
879
879
880
881
882

695 unmodified lines

}
        // Reload to get the updated summary.
        stopLoad = startSpinner(errW, fmt.Sprintf("Reloading checkpoint %s", fullCheckpointID))
        reopened, openErr := checkpoint.Open(ctx, lookup.repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
        reopened, openErr := checkpoint.Open(ctx, lookup.repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
        if openErr != nil {
            stopLoad(false)
            return fmt.Errorf("open checkpoint store: %w", openErr)
        }
173 unmodified lines

// `git fetch` fails against partial-clone repos with "did not send all
    // necessary objects"). Falls back to a full metadata-branch fetch if
    // fetch-pack also can't reach the blobs.
    stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return nil, fmt.Errorf("open checkpoint store: %w", err)
    }

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

533 unmodified lines

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

533 unmodified lines

return "origin"
}

// FetchCheckpointRef fetches a single per-checkpoint ref (refs/entire/checkpoints/
// <shard>/<id>) from the checkpoint remote into the local ref of the same name,
// so the git-refs store can resolve a checkpoint written on another machine.
// Best-effort: the caller treats a fetch failure as "checkpoint not found".
func FetchCheckpointRef(ctx context.Context, ref plumbing.ReferenceName) error {
    ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
    defer cancel()

fetchTarget := resolveCheckpointFetchTarget(ctx)
    refSpec := "+" + ref.String() + ":" + ref.String()
    if _, err := remote.Fetch(ctx, remote.FetchOptions{
        Remote:   fetchTarget,
        RefSpecs: []string{refSpec},
        NoTags:   true,
    }); err != nil {
        return fmt.Errorf("fetch checkpoint ref %s from %s: %w", ref, fetchTarget, err)
    }
    return nil
}

// FetchBlobsByHash fetches specific blob objects from the remote by their SHA-1 hashes.
// Uses "git fetch <target> <hash>" which goes through normal credential helpers,
// unlike fetch-pack which bypasses them. Requires the server to support 

Mcmd/entire/cli/git_operations.go+20

202 unmodified lines

203
204
205
206
206
207
208
209
64 unmodified lines

274
275
276
277
277
278
279
280
127 unmodified lines

408
409
410
411
411
412
413
414
548 unmodified lines

963
964
965
966
966
967
968
969

202 unmodified lines

}
    defer repo.Close()

stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return fmt.Errorf("open checkpoint store: %w", err)
    }
64 unmodified lines

checkpointID := result.checkpointIDs[0]
    var metadata *strategy.CheckpointInfo

stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return fmt.Errorf("open checkpoint store: %w", err)
    }
127 unmodified lines

refs checkpoint.PersistentRefs,
    checkpointID id.CheckpointID,
) (*strategy.CheckpointInfo, error) {
    stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{Refs: &refs, BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{Refs: &refs, BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return nil, fmt.Errorf("open checkpoint store: %w", err)
    }
548 unmodified lines

return fmt.Errorf("failed to open repository: %w", repoErr)
    }
    defer repo.Close()
    stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return fmt.Errorf("open checkpoint store: %w", err)
    }

Mcmd/entire/cli/resume.go+4/-4

112 unmodified lines

113
114
115
116
116
117
118
119

112 unmodified lines

}
    defer repo.Close()

stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash})
stores, err := checkpoint.Open(ctx, repo, checkpoint.OpenOptions{BlobFetcher: FetchBlobsByHash, RefFetcher: FetchCheckpointRef})
if err != nil {
        return fmt.Errorf("failed to open checkpoint stores: %w", err)
    }

Mcmd/entire/cli/tokens_profile.go+1/-1