Skip to content

Git designed for AI agents.

sdk, operations: what a land retried after a lost response does, exactly

Federico Elia2474b248be3f
diff --git a/doc/graft/LAND-BATCHING.md b/doc/graft/LAND-BATCHING.md
index b4f6f27..5e32f6a 100644
--- a/doc/graft/LAND-BATCHING.md
+++ b/doc/graft/LAND-BATCHING.md
@@ -483,8 +483,11 @@ member) and removed it; see [PLAN8.md](PLAN8.md).
an exponential backoff from 100 ms to 2 s with full jitter, stopped by the
context), and reports the retries in `LandResult.UnavailableRetries`. A client
that does not use the SDK must do the same: retry `Unavailable`, and only
- `Unavailable`, with a bounded backoff. A land is safe to retry: a change that
- already reached the branch is reported as already landed.
+ `Unavailable`, with a bounded backoff. A land is safe to retry after a lost
+ response: with a merge, a fast-forward or a resolved land the change is
+ reported as already landed; with a rebase the change is replayed onto the tip,
+ comes out empty, and the branch is left unchanged (a change that a later
+ commit reverted in between is replayed again, as a cherry-pick would be).
- A land that arrives alone waits up to `window` before its batch runs. Set
`window = "0s"` to remove the wait, or `disabled = true` to remove batching.
- Requests that do not qualify (see *When a land is batched*) run unbatched and
diff --git a/internal/gitaly/service/operations/land_retry_test.go b/internal/gitaly/service/operations/land_retry_test.go
new file mode 100644
index 0000000..e661290
--- /dev/null
+++ b/internal/gitaly/service/operations/land_retry_test.go
@@ -0,0 +1,122 @@
+package operations
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/featureflag"
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/proto/go/gitalypb"
+)
+
+// TestUserLand_retryAfterALostResponse lands a change twice, as a client does that retries
+// Unavailable after the response of a land that succeeded was lost (sdk.UnavailableRetry): the
+// change is never landed twice. With a merge, the change itself is on the branch, so the second
+// land reports it already landed. With a rebase, the branch holds a rebased copy: the second land
+// replays the change, which comes out empty, and leaves the branch unchanged.
+func TestUserLand_retryAfterALostResponse(t *testing.T) {
+ t.Parallel()
+
+ testhelper.NewFeatureSets(featureflag.GPGSigning).Run(t, testUserLandRetryAfterALostResponse)
+}
+
+func testUserLandRetryAfterALostResponse(t *testing.T, ctx context.Context) {
+ t.Parallel()
+
+ ctx, cfg, client := setupOperationsService(t, ctx)
+
+ for _, tc := range []struct {
+ desc string
+ strategy gitalypb.UserLandRequest_Strategy
+ // alreadyLanded is what the second land reports.
+ alreadyLanded bool
+ }{
+ {desc: "rebase", strategy: gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED},
+ {desc: "merge", strategy: gitalypb.UserLandRequest_STRATEGY_MERGE, alreadyLanded: true},
+ } {
+ t.Run(tc.desc, func(t *testing.T) {
+ t.Parallel()
+
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ base := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"}),
+ )
+ gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithParents(base),
+ gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
+ gittest.TreeEntry{Mode: "100644", Path: "x", Content: "x"},
+ ),
+ )
+ change := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithParents(base),
+ gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
+ gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"},
+ ),
+ )
+
+ request := landRequest(repoProto, git.DefaultBranch, change)
+ request.Strategy = tc.strategy
+
+ first, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ require.True(t, first.GetRewritten())
+ landed := git.ObjectID(first.GetLandedCommitId())
+ commits := gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--count", landed.String())
+
+ // The retry of the same request.
+ second, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ require.Equal(t, tc.alreadyLanded, second.GetAlreadyLanded())
+ require.False(t, second.GetRewritten())
+ require.Equal(t, landed.String(), second.GetLandedCommitId(), "the branch is left at the first land")
+ requireBranchAt(t, cfg, repoPath, git.DefaultBranch, landed)
+ require.Equal(t, commits, gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--count", git.DefaultBranch))
+ })
+ }
+
+ t.Run("rebase, after a revert of the change", func(t *testing.T) {
+ t.Parallel()
+
+ // A change that a later commit reverted is replayed again, as git's cherry-pick would.
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ withoutY := []gittest.TreeEntry{
+ {Mode: "100644", Path: "a", Content: "a"},
+ {Mode: "100644", Path: "x", Content: "x"},
+ }
+ base := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"}),
+ )
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithParents(base), gittest.WithTreeEntries(withoutY...))
+ change := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithParents(base),
+ gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
+ gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"},
+ ),
+ )
+
+ request := landRequest(repoProto, git.DefaultBranch, change)
+ first, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ reverted := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithParents(git.ObjectID(first.GetLandedCommitId())),
+ gittest.WithTreeEntries(withoutY...),
+ )
+
+ second, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ require.True(t, second.GetRewritten())
+ require.Equal(t, reverted.String(), second.GetOldTip())
+ gittest.RequireTree(t, cfg, repoPath, second.GetLandedCommitId(), append(withoutY, gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"}))
+ })
+}
diff --git a/sdk/land.go b/sdk/land.go
index bf1ee43..97b55f4 100644
--- a/sdk/land.go
+++ b/sdk/land.go
@@ -70,12 +70,19 @@ type LandOptions struct {
// backoff with full jitter: before retry n it waits a random time between zero
// and min(Cap, Base x 2^(n-1)). A context that ends stops the waiting.
//
-// A land is safe to retry: the server lands a change by compare-and-swap, and
-// a change that already reached the branch, for example because the response
-// of an earlier call was lost, is reported as AlreadyLanded instead of being
-// landed again. A retried land with ResolveConflicts may run the conflict
-// resolver again, a model included, for the same conflict; LandResult reports
-// the retries (UnavailableRetries), so that this cost is visible.
+// A land is safe to retry when the response of an earlier call was lost after
+// the server landed the change: the server lands by compare-and-swap, and the
+// retry finds the change on the branch. With LandMerge, LandFastForwardOnly or
+// ResolveConflicts, Commit is then reachable from the tip, and the retry is
+// reported as AlreadyLanded. With LandRebase the branch holds a rebased copy,
+// not Commit itself: the retry replays the change onto the tip, every commit
+// of it comes out empty and is dropped, and the branch is left unchanged; the
+// result then has LandedCommitID equal to the tip, Rewritten false and
+// AlreadyLanded false. A change that a later commit reverted in between is
+// replayed again, as git's cherry-pick would. A retried land with
+// ResolveConflicts may run the conflict resolver again, a model included, for
+// the same conflict; LandResult reports the retries (UnavailableRetries), so
+// that this cost is visible.
type UnavailableRetry struct {
// Disabled turns the retries off: Land returns Unavailable at once.
Disabled bool