Skip to content

Git designed for AI agents.

gitai land: fixes of the second re-review of 38d8bb1 (c1)

Federico Eliae6617068145c
diff --git a/cmd/graft/conflicts.go b/cmd/graft/conflicts.go
index 1af9fb9..26005e5 100644
--- a/cmd/graft/conflicts.go
+++ b/cmd/graft/conflicts.go
@@ -44,10 +44,6 @@ const (
// conflictSides are the sides of a conflicting file, in the order of the package.
var conflictSides = []string{"base", "ours", "theirs"}
-// conflictsUnreadable is the content of the file to edit when no side of the file can be read: a
-// conflict that the marker rule of the server rejects until the agent writes the resolution.
-const conflictsUnreadable = "<<<<<<< ours (the branch tip): cannot be read, see conflict.json\n=======\n>>>>>>> theirs (the change): cannot be read, see conflict.json\n"
-
// printConflict describes a conflict package: the reason the server gave, each file with its
// outcome and what it lacks, and the merge's messages. The paths, reasons and messages come from
// the server and are escaped with displayText.
@@ -118,7 +114,6 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
return err
}
var contents [3][]byte
- readable := true
for i, s := range []*gitalypb.LandConflictSide{f.GetBase(), f.GetOurs(), f.GetTheirs()} {
if s == nil {
continue
@@ -126,10 +121,8 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
b, ok := blobs[s.GetOid()]
switch {
case s.GetOid() == "":
- readable = false
continue
case !ok:
- readable = false
fmt.Fprintf(out, " %s: the %s side %s cannot be read: %s\n", displayText(p), conflictSides[i], shortOID(s.GetOid()), displayText(missing[s.GetOid()]))
continue
}
@@ -145,10 +138,7 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
return err
}
}
- content := conflictFileContent(contents)
- if !readable {
- content = conflictFileWithout(f, contents)
- }
+ content := conflictFileFor(f, contents)
if err := create(target, p, content); err != nil {
return err
}
@@ -195,51 +185,6 @@ func (w conflictsWriter) create(target, p string, data []byte) error {
return f.Close()
}
-// conflictFileWithout is the file to edit when a present side cannot be read. Without the base it
-// is the two-way merge of the sides. Without ours or theirs it is a conflict the marker rule of the
-// server refuses until the agent resolves it: the side that can be read (named by its file under
-// sides/ when it is binary) against a note that the other is unavailable. A side is never dropped
-// silently.
-func conflictFileWithout(f *gitalypb.LandConflictFile, sides [3][]byte) []byte {
- ours, theirs := sides[1], sides[2]
- oursPresent, theirsPresent := f.GetOurs() != nil, f.GetTheirs() != nil
- switch {
- case ours != nil && theirs != nil:
- return conflictFileContent([3][]byte{nil, ours, theirs})
- case ours == nil && theirs == nil:
- return []byte(conflictsUnreadable)
- }
- const unavailable = "unavailable, see conflict.json"
- body := func(name string, b []byte) []byte {
- if isBinaryContent(b) {
- return []byte(fmt.Sprintf("(binary: see sides/%s/%s)\n", name, string(f.GetPath())))
- }
- if len(b) > 0 && b[len(b)-1] != '\n' {
- return append(append([]byte{}, b...), '\n')
- }
- return b
- }
- var out bytes.Buffer
- switch {
- case ours == nil && oursPresent:
- out.WriteString("<<<<<<< ours (the branch tip): " + unavailable + "\n=======\n")
- out.Write(body("theirs", theirs))
- out.WriteString(">>>>>>> theirs (the change)\n")
- case theirs == nil && theirsPresent:
- out.WriteString("<<<<<<< ours (the branch tip)\n")
- out.Write(body("ours", ours))
- out.WriteString("=======\n>>>>>>> theirs (the change): " + unavailable + "\n")
- default:
- // The side that is missing is absent (a deletion), and the one there is cannot be read
- // apart from the base: the side there is, as for any deletion conflict.
- if ours != nil {
- return ours
- }
- return theirs
- }
- return out.Bytes()
-}
-
// fetchSides reads the sides of the package that have an object ID, all at once. When the server
// cannot return them all, it reads them one by one, so that a side it cannot return costs only that
// side: missing gives the error of each side that could not be read.
@@ -279,20 +224,60 @@ func fetchSides(ctx context.Context, ws *workspace.Workspace, c *gitalypb.LandCo
return blobs, missing
}
-// conflictFileContent is the file an agent edits: the merge with markers when both sides are text,
-// otherwise the tip's side (or the landed commit's when the tip deleted the file).
-func conflictFileContent(sides [3][]byte) []byte {
- base, ours, theirs := sides[0], sides[1], sides[2]
- text := ours != nil && theirs != nil && !isBinaryContent(base) && !isBinaryContent(ours) && !isBinaryContent(theirs)
- if text {
- if r, ok := merge3.Merge(base, ours, theirs); ok {
+// conflictPointerPrefix starts the line that stands in the file to edit for a binary side, which
+// the file cannot embed. graft land --resolved refuses a file that still holds such a line.
+const conflictPointerPrefix = "graft-conflict-pointer: "
+
+// conflictFileFor is the file an agent edits for a conflicting file of the package, given the
+// contents of the sides that could be read (nil for an absent side or one that cannot be read).
+// When the CLI can merge the file (ours and theirs text, the base text or absent, and a diff
+// within merge3's bound) it is the merge with git's diff3 markers around each conflict. Otherwise
+// it is one conflict between diff3 markers holding every side: the text of a text side, a pointer
+// line to the file of a binary side under sides/, and on the marker line a note on a side that is
+// binary, absent or unavailable. The marker rule of the server refuses it until the agent resolves
+// it, and --resolved refuses a pointer line: a side is never dropped silently.
+func conflictFileFor(f *gitalypb.LandConflictFile, sides [3][]byte) []byte {
+ present := [3]*gitalypb.LandConflictSide{f.GetBase(), f.GetOurs(), f.GetTheirs()}
+ text := func(i int) bool { return sides[i] != nil && !isBinaryContent(sides[i]) }
+ if text(1) && text(2) && (present[0] == nil || text(0)) {
+ if r, ok := merge3.Merge(sides[0], sides[1], sides[2]); ok {
return r.WithMarkers(merge3.Labels{Ours: "ours (the branch tip)", Base: "base", Theirs: "theirs (the change)"})
}
}
- if ours != nil {
- return ours
+
+ side := func(i int) (note string, body []byte) {
+ switch {
+ case present[i] == nil && i == 0:
+ return ": none, both sides added the file", nil
+ case present[i] == nil:
+ return ": deleted", nil
+ case sides[i] == nil:
+ reason := present[i].GetUnavailable()
+ if reason == "" {
+ reason = "the server did not return it"
+ }
+ return ": unavailable, " + reason, nil
+ case isBinaryContent(sides[i]):
+ return ": binary", []byte(fmt.Sprintf("%sthe binary %s side is sides/%s/%s\n", conflictPointerPrefix, conflictSides[i], conflictSides[i], string(f.GetPath())))
+ }
+ body = sides[i]
+ if len(body) > 0 && body[len(body)-1] != '\n' {
+ body = append(append([]byte{}, body...), '\n')
+ }
+ return "", body
}
- return theirs
+ oursNote, oursBody := side(1)
+ baseNote, baseBody := side(0)
+ theirsNote, theirsBody := side(2)
+ var out bytes.Buffer
+ out.WriteString("<<<<<<< ours (the branch tip)" + oursNote + "\n")
+ out.Write(oursBody)
+ out.WriteString("||||||| base" + baseNote + "\n")
+ out.Write(baseBody)
+ out.WriteString("=======\n")
+ out.Write(theirsBody)
+ out.WriteString(">>>>>>> theirs (the change)" + theirsNote + "\n")
+ return out.Bytes()
}
func isBinaryContent(b []byte) bool {
@@ -300,8 +285,10 @@ func isBinaryContent(b []byte) bool {
}
// readResolution reads the resolution of a conflicts directory: the package's tip and commit, and
-// for every conflicting path its file under DIR/files, or a deletion when the file is absent. The mode is the
-// tip's side's, else the landed commit's.
+// for every conflicting path its file under DIR/files, or a deletion when the file is absent. The
+// mode is the file mode of the tip's side, else of the landed commit's, else of the base's: a side
+// that is a submodule (a gitlink) or absent gives none, and a path none of whose sides is a file
+// can only be deleted. A file that still holds the pointer line of a binary side is refused.
func readResolution(dir string) (*workspace.Resolution, error) {
raw, err := os.ReadFile(filepath.Join(dir, conflictsPackage))
if err != nil {
@@ -326,12 +313,22 @@ func readResolution(dir string) (*workspace.Resolution, error) {
if err != nil {
return nil, err
}
- mode := f.GetOurs().GetMode()
- if mode == "" {
- mode = f.GetTheirs().GetMode()
+ for _, line := range bytes.Split(content, []byte("\n")) {
+ if bytes.HasPrefix(line, []byte(conflictPointerPrefix)) {
+ return nil, fmt.Errorf("%s still holds the pointer to a binary side: copy the side you want over it from %s, or remove it to delete the path", target, filepath.Join(dir, conflictsSidesDir))
+ }
+ }
+ mode := ""
+ for _, side := range []*gitalypb.LandConflictSide{f.GetOurs(), f.GetTheirs(), f.GetBase()} {
+ switch side.GetMode() {
+ case "100644", "100755", "120000":
+ if mode == "" {
+ mode = side.GetMode()
+ }
+ }
}
if mode == "" {
- mode = "100644"
+ return nil, fmt.Errorf("conflicting path %q is a file on none of its sides (a submodule, say): it can only be deleted, by removing %s, or resolved outside graft", p, target)
}
res.Files = append(res.Files, workspace.ResolvedFile{Path: p, Mode: mode, Content: content})
}
diff --git a/cmd/graft/conflicts_server_test.go b/cmd/graft/conflicts_server_test.go
index 066b26e..7bd445d 100644
--- a/cmd/graft/conflicts_server_test.go
+++ b/cmd/graft/conflicts_server_test.go
@@ -177,8 +177,8 @@ func TestCLILandConflictsWithAnUnavailableSide(t *testing.T) {
require.Equal(t, "added by theirs\n", string(theirs))
edit, err := os.ReadFile(filepath.Join(conflicts, "files", "c.txt"))
require.NoError(t, err)
- require.Equal(t, "<<<<<<< ours (the branch tip): unavailable, see conflict.json\n=======\nadded by theirs\n>>>>>>> theirs (the change)\n", string(edit),
- "the side there is against a note on the other, between markers")
+ require.Contains(t, string(edit), "<<<<<<< ours (the branch tip): unavailable, ", "the unavailable side is said so")
+ require.Contains(t, string(edit), "=======\nadded by theirs\n>>>>>>> theirs (the change)\n", "the side there is, between markers")
// Landed unedited, the file is refused by the marker rule: the tip's side is never dropped.
code, _, stderr = runCLI(t, "land", "-C", dir, "-author", "Dev <[email protected]>", "-resolved", conflicts)
diff --git a/cmd/graft/conflicts_test.go b/cmd/graft/conflicts_test.go
index a245f86..b76d0a0 100644
--- a/cmd/graft/conflicts_test.go
+++ b/cmd/graft/conflicts_test.go
@@ -5,10 +5,12 @@ import (
"os"
"path/filepath"
"runtime"
+ "strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/encoding/protojson"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
@@ -52,42 +54,146 @@ func TestWriteConflictsRefusesAnExistingDirectory(t *testing.T) {
require.ErrorContains(t, err, "already holds a conflicts directory")
}
-// TestConflictsWriterNamesBothPathsOfACollision: on a file system that folds case, the second of
-// two paths finds the first one's file. Here the folding is simulated by a target that exists under
-// the folded name; the error names both paths.
+// TestConflictsWriterNamesBothPathsOfACollision: the second of two paths that the file system
+// takes for one file finds that file in place, and the error names both paths. The file system of
+// the test does not fold case, so README.md is made to be readme.md by a symbolic link: the
+// collision goes through the file system, as a case-insensitive one would make it.
func TestConflictsWriterNamesBothPathsOfACollision(t *testing.T) {
t.Parallel()
dir := t.TempDir()
w := conflictsWriter{written: map[string]string{}}
require.NoError(t, w.create(filepath.Join(dir, "readme.md"), "readme.md", []byte("a")))
- // A case-insensitive file system would resolve README.md to readme.md.
- w.written[strings.ToLower(filepath.Join(dir, "readme.md"))] = "README.md"
- err := w.create(filepath.Join(dir, "readme.md"), "readme.md", []byte("b"))
- require.ErrorContains(t, err, `conflicting paths "README.md" and "readme.md" are the same file`)
+ require.NoError(t, os.Symlink("readme.md", filepath.Join(dir, "README.md")))
+ err := w.create(filepath.Join(dir, "README.md"), "README.md", []byte("b"))
+ require.ErrorContains(t, err, `conflicting paths "readme.md" and "README.md" are the same file`)
+ content, err := os.ReadFile(filepath.Join(dir, "readme.md"))
+ require.NoError(t, err)
+ require.Equal(t, "a", string(content), "the first file is not overwritten")
}
-// TestConflictFileWithoutASide: a side that cannot be read is never dropped silently.
-func TestConflictFileWithoutASide(t *testing.T) {
+// TestConflictFileFor: the file to edit is a true merge when the CLI can make one, and otherwise
+// a conflict holding every side, which the marker rule refuses unedited: a side is never dropped.
+func TestConflictFileFor(t *testing.T) {
t.Parallel()
- side := &gitalypb.LandConflictSide{Mode: "100644"}
- file := &gitalypb.LandConflictFile{Path: []byte("c.txt"), Base: side, Ours: side, Theirs: side}
+ side := &gitalypb.LandConflictSide{Mode: "100644", Oid: "1"}
+ unavailable := &gitalypb.LandConflictSide{Mode: "100644", Unavailable: "gone"}
+ file := func(base, ours, theirs *gitalypb.LandConflictSide) *gitalypb.LandConflictFile {
+ return &gitalypb.LandConflictFile{Path: []byte("c.txt"), Base: base, Ours: ours, Theirs: theirs}
+ }
+ long := func(prefix string) []byte {
+ var b strings.Builder
+ for i := 0; i < 3000; i++ {
+ b.WriteString(prefix + strconv.Itoa(i) + "\n")
+ }
+ return []byte(b.String())
+ }
- // Without the base: the two-way merge of the sides, with markers where they differ.
- got := string(conflictFileWithout(file, [3][]byte{nil, []byte("a\nours\n"), []byte("a\ntheirs\n")}))
- require.Contains(t, got, "<<<<<<< ours (the branch tip)\n")
- require.Contains(t, got, "ours\n")
- require.Contains(t, got, "theirs\n")
+ for _, tc := range []struct {
+ name string
+ file *gitalypb.LandConflictFile
+ sides [3][]byte
+ want []string
+ }{
+ {
+ name: "a true merge", file: file(side, side, side),
+ sides: [3][]byte{[]byte("a\nb\n"), []byte("a\nours\n"), []byte("a\ntheirs\n")},
+ want: []string{"a\n<<<<<<< ours (the branch tip)\nours\n||||||| base\nb\n=======\ntheirs\n>>>>>>> theirs (the change)\n"},
+ },
+ {
+ name: "the base unavailable, a binary ours", file: file(unavailable, side, side),
+ sides: [3][]byte{nil, []byte("\x00bin"), []byte("theirs\n")},
+ want: []string{"<<<<<<< ours (the branch tip): binary\n" + conflictPointerPrefix + "the binary ours side is sides/ours/c.txt\n" +
+ "||||||| base: unavailable, gone\n=======\ntheirs\n>>>>>>> theirs (the change)\n"},
+ },
+ {
+ name: "both readable, a binary theirs", file: file(side, side, side),
+ sides: [3][]byte{[]byte("b\n"), []byte("ours\n"), []byte("\x00bin")},
+ want: []string{"<<<<<<< ours (the branch tip)\nours\n||||||| base\nb\n=======\n" + conflictPointerPrefix + "the binary theirs side is sides/theirs/c.txt\n>>>>>>> theirs (the change): binary\n"},
+ },
+ {
+ name: "a diff over merge3's bound", file: file(side, side, side),
+ sides: [3][]byte{long("b"), long("o"), long("t")},
+ want: []string{"<<<<<<< ours (the branch tip)\no0\n", "||||||| base\nb0\n", "=======\nt0\n", "t2999\n>>>>>>> theirs (the change)\n"},
+ },
+ {
+ name: "modify/delete with the base", file: file(side, nil, side),
+ sides: [3][]byte{[]byte("b\n"), nil, []byte("theirs\n")},
+ want: []string{"<<<<<<< ours (the branch tip): deleted\n||||||| base\nb\n=======\ntheirs\n>>>>>>> theirs (the change)\n"},
+ },
+ {
+ name: "modify/delete without the base", file: file(unavailable, side, nil),
+ sides: [3][]byte{nil, []byte("ours\n"), nil},
+ want: []string{"<<<<<<< ours (the branch tip)\nours\n||||||| base: unavailable, gone\n=======\n>>>>>>> theirs (the change): deleted\n"},
+ },
+ {
+ name: "add/add with ours unavailable", file: file(nil, unavailable, side),
+ sides: [3][]byte{nil, nil, []byte("added by theirs")},
+ want: []string{"<<<<<<< ours (the branch tip): unavailable, gone\n||||||| base: none, both sides added the file\n=======\nadded by theirs\n>>>>>>> theirs (the change)\n"},
+ },
+ } {
+ got := string(conflictFileFor(tc.file, tc.sides))
+ for _, w := range tc.want {
+ require.Contains(t, got, w, tc.name)
+ }
+ if tc.name != "a true merge" {
+ require.True(t, strings.HasPrefix(got, "<<<<<<< ours"), "%s: one conflict holding every side:\n%s", tc.name, got)
+ }
+ }
+}
- // Without ours: the side there is against a note, between markers.
- got = string(conflictFileWithout(file, [3][]byte{[]byte("b\n"), nil, []byte("added by theirs")}))
- require.Equal(t, "<<<<<<< ours (the branch tip): unavailable, see conflict.json\n=======\nadded by theirs\n>>>>>>> theirs (the change)\n", got)
+// writePackage writes the conflict.json of a conflicts directory.
+func writePackage(t *testing.T, dir string, c *gitalypb.LandConflict) {
+ t.Helper()
+ raw, err := protojson.Marshal(c)
+ require.NoError(t, err)
+ require.NoError(t, os.MkdirAll(filepath.Join(dir, "files"), 0o755))
+ require.NoError(t, os.WriteFile(filepath.Join(dir, "conflict.json"), raw, 0o644))
+}
+
+// TestReadResolutionRefusesAPointerLine: a file whose binary pointer the agent left in place is
+// refused, markers or not.
+func TestReadResolutionRefusesAPointerLine(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ side := &gitalypb.LandConflictSide{Mode: "100644", Oid: "1"}
+ writePackage(t, dir, &gitalypb.LandConflict{Files: []*gitalypb.LandConflictFile{{Path: []byte("c.bin"), Base: side, Ours: side, Theirs: side}}})
+ require.NoError(t, os.WriteFile(filepath.Join(dir, "files", "c.bin"), []byte("x\n"+conflictPointerPrefix+"the binary ours side is sides/ours/c.bin\n"), 0o644))
+ _, err := readResolution(dir)
+ require.ErrorContains(t, err, "still holds the pointer to a binary side")
+}
- // Without theirs, a binary ours is named by its side file rather than embedded.
- got = string(conflictFileWithout(file, [3][]byte{[]byte("b\n"), []byte("\x00bin"), nil}))
- require.Equal(t, "<<<<<<< ours (the branch tip)\n(binary: see sides/ours/c.txt)\n=======\n>>>>>>> theirs (the change): unavailable, see conflict.json\n", got)
+// TestReadResolutionModes: the mode is the first file mode of ours, theirs and the base; a path
+// that is a file on none of its sides (a submodule) can only be deleted.
+func TestReadResolutionModes(t *testing.T) {
+ t.Parallel()
- // Without either: a conflict to resolve.
- require.Equal(t, conflictsUnreadable, string(conflictFileWithout(file, [3][]byte{})))
+ gitlink := &gitalypb.LandConflictSide{Mode: "160000", Unavailable: "submodule"}
+ exec := &gitalypb.LandConflictSide{Mode: "100755", Oid: "1"}
+ for name, tc := range map[string]struct {
+ file *gitalypb.LandConflictFile
+ mode string
+ err string
+ }{
+ "ours a submodule, theirs a file": {&gitalypb.LandConflictFile{Path: []byte("s"), Ours: gitlink, Theirs: exec}, "100755", ""},
+ "a submodule only": {&gitalypb.LandConflictFile{Path: []byte("s"), Ours: gitlink}, "", "is a file on none of its sides"},
+ } {
+ dir := t.TempDir()
+ writePackage(t, dir, &gitalypb.LandConflict{Files: []*gitalypb.LandConflictFile{tc.file}})
+ require.NoError(t, os.WriteFile(filepath.Join(dir, "files", "s"), []byte("x\n"), 0o644), name)
+ res, err := readResolution(dir)
+ if tc.err != "" {
+ require.ErrorContains(t, err, tc.err, name)
+ // Removing the file deletes the path.
+ require.NoError(t, os.Remove(filepath.Join(dir, "files", "s")), name)
+ res, err = readResolution(dir)
+ require.NoError(t, err, name)
+ require.True(t, res.Files[0].Delete, name)
+ continue
+ }
+ require.NoError(t, err, name)
+ require.Equal(t, tc.mode, res.Files[0].Mode, name)
+ }
}
diff --git a/doc/graft/PLAN10.md b/doc/graft/PLAN10.md
index ed467ae..b0acb49 100644
--- a/doc/graft/PLAN10.md
+++ b/doc/graft/PLAN10.md
@@ -582,3 +582,19 @@ way.
names follow git's rules, the hashed 8.3 forms included; only the last paragraph of a message
is its trailer block, and the server's trailers are always that paragraph (documented,
tested).
+* **Second re-review of 38d8bb1** (PM: fix before R1).
+ * The check of an agent's resolution runs before the land enters its lane (landPrecheckResolution
+ in UserLand), against resolution.tip: the only tip the land accepts the resolution at, so no
+ re-check is needed. In the lane the land applies the verdicts when the tip and the commit are
+ those the check ran on, and fails closed otherwise; if the branch moved, the resolution is
+ refused with a fresh package before any verdict is used. No branch is held while the external
+ process runs. The whole check has one deadline (the resolver's timeout); each request uses what
+ remains, and expiry fails closed, the files checked before keeping their verdicts.
+ * Check requests carry no commit message; their file data is bounded by resolver.CheckBatchBytes
+ with a per-file allowance for paths, and max_file_bytes is refused at startup above
+ resolver.MaxCheckFileBytes (about 12 MiB), so that one file always fits one request.
+ * The listings of the trees and the attributes are read once per land, not per batch.
+ * The CLI never falls back to one side: when it cannot merge a file, the file to edit is one diff3
+ conflict holding every side (pointer lines for binary sides, the text of text sides, notes for
+ deleted and unavailable ones). --resolved refuses a pointer line, and a path that is a file on
+ none of its sides can only be deleted.
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index 3a97eca..51fa932 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -81,18 +81,26 @@ A client asks for resolution per call:
with a side, another path's side or the package. A side the package gives no
object ID (see `unavailable` below), or that the server cannot return, is
left out and reported; it fails neither its file nor the directory, and it is
- never dropped silently. Without the base, the file to edit is the two-way
- merge of the sides, with markers where they differ. Without ours or theirs,
- it is a conflict between markers: the side there is (named by its file under
- `sides/` when it is binary) against a note that the other is unavailable, so
- that the marker rule refuses it until the agent resolves it. `DIR` must not
+ never dropped silently. When the CLI can merge the file (ours and theirs
+ text, the base text or absent, and a diff within bound), the file to edit is
+ the merge with diff3 markers. Otherwise (a binary side, a side that is deleted
+ or unavailable, a diff too large) it is one conflict between diff3 markers
+ that holds every side: the text of each text side, and for a binary side a
+ `graft-conflict-pointer:` line naming its file under `sides/`, with a note on
+ the marker line of a side that is binary, deleted or unavailable. The marker
+ rule of the server refuses it until the agent resolves it, and
+ `graft land --resolved` refuses a file that still holds a pointer line, so
+ that a pointer can never land in place of a binary side. `DIR` must not
hold a conflicts directory yet: one being edited is never overwritten. A
path that cannot be a file under `DIR` on this system (a backslash, on
Windows a colon or a name the system reserves) is an error, and so are two paths that
the file system takes for one file (names that differ only in case on a
case-insensitive file system). Edit the files under `DIR/files`, then
- `graft land --resolved DIR` lands them: a present file is written (with the
- tip side's mode), an absent one is deleted.
+ `graft land --resolved DIR` lands them: a present file is written, with the
+ file mode of the tip's side, else of the landed commit's, else of the base's;
+ an absent one is deleted. A path that is a file on none of its sides (a
+ submodule) can only be deleted, by removing its file, or resolved outside
+ graft.
- RPC: `UserLandRequest.resolve_conflicts` (field 10). The response carries
`resolved_paths` (field 7) and `resolver` (field 8). The re-land is
`UserLandRequest.resolution` (field 11).
@@ -412,12 +420,20 @@ content) or `delete`. The server accepts it only when:
- when a resolver is configured, the files that may be sent to it pass its
`check`: the syntax check and the configured checks, as an `llm` answer.
A check that cannot run fails the land, with the reason in the package.
- The files go to the check in requests of at most 32 MiB of file data each
- (about 43 MiB encoded, within the 64 MiB a check request may take and the
- 128 MiB graft-resolve reads by default), checked one after the other, so
- that neither side holds more than one such request at a time. A single file
- larger than that is a request of its own: raise graft-resolve's
- `--max-request-bytes` with `max_file_bytes`.
+ The check runs before the land enters the branch's lane, against the tip
+ the resolution was made against, so that no branch is held while an
+ external process runs; the lane only applies its verdicts (if the branch
+ moved meanwhile, the resolution is refused with a fresh package, as it would
+ be anyway). The whole check has one deadline, the resolver's `timeout`:
+ every check request uses what remains of it, and its expiry fails the check
+ closed. The files go to the check in requests of at most 48 MiB of file data
+ each (sides and resolutions, within the 64 MiB a check request may take
+ once encoded, and the 128 MiB graft-resolve reads by default), checked one
+ after the other, so that neither side holds more than one such request at a
+ time; the requests carry no commit message. One file, its three sides and
+ its resolution, must fit one request, so the server refuses at startup a
+ `max_file_bytes` over about 12 MiB. When a check request fails, the files
+ checked before keep their verdict and the others are not attempted.
A file with a `syntax: unchecked` verdict lands, and is counted in
`graft_land_resolution_syntax_unchecked_total`, once, when the land
succeeds, and in the server log.
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.go
index 8342560..e193003 100644
--- a/internal/gitaly/service/operations/land.go
+++ b/internal/gitaly/service/operations/land.go
@@ -72,11 +72,20 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*g
return nil, structerr.NewInvalidArgument("%w", err)
}
+ // The check of an agent's resolution runs an external process: it runs here, before the land
+ // enters its lane, so that no branch is held while it runs (landPrecheckResolution).
+ var precheck *landPrecheck
+ if in.GetResolution() != nil {
+ if precheck, err = s.landPrecheckResolution(ctx, in, s.localRepoFactory.Build(in.GetRepository())); err != nil {
+ return nil, err
+ }
+ }
+
if s.landBatchable(ctx, in) {
- return s.userLandBatched(ctx, in, committer)
+ return s.userLandBatched(ctx, in, committer, precheck)
}
- resp, err := s.landWithRetries(ctx, in, committer)
+ resp, err := s.landWithRetries(ctx, in, committer, precheck)
if err != nil {
return nil, err
}
@@ -94,7 +103,7 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*g
// On MVCC repositories, the attempt runs against a snapshot that is pinned for the whole RPC.
// Retrying within the RPC would read the same tip again, so the conflict is reported to the
// client instead and the client has to retry the landing.
-func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature) (*gitalypb.UserLandResponse, error) {
+func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {
snapshotPinned := mvcc.CacheFromContext(ctx) != nil
maxAttempts := int(in.GetMaxAttempts())
@@ -103,7 +112,7 @@ func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandReque
}
for attempt := 1; ; attempt++ {
- resp, err := s.landAttempt(ctx, in, committer)
+ resp, err := s.landAttempt(ctx, in, committer, precheck)
if err == nil {
resp.Attempts = int32(attempt)
return resp, nil
@@ -167,7 +176,7 @@ func (s *Server) landAncestry(ctx context.Context, quarantineRepo *localrepo.Rep
// landAttempt performs a single compare-and-swap attempt of UserLand. It returns a landConflict
// if the branch was moved by a concurrent writer, in which case the caller retries.
-func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature) (*gitalypb.UserLandResponse, error) {
+func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {
attempt, cleanup, err := s.newLandingAttempt(ctx, in.GetRepository(), landPublishLoose, ignoreLandPhase)
if err != nil {
return nil, err
@@ -182,7 +191,7 @@ func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest,
// The request is decided as a member of a batch is (landingAttempt.decide). The policy is not
// evaluated here: the update runs with the identity of the request, so the hooks judge it.
- req := &landBatchRequest{ctx: ctx, in: in, committer: committer, actor: landBatchActor(ctx, in)}
+ req := &landBatchRequest{ctx: ctx, in: in, committer: committer, actor: landBatchActor(ctx, in), precheck: precheck}
step, err := attempt.decide(ctx, tip, req)
if err != nil {
return nil, err
diff --git a/internal/gitaly/service/operations/land_attempt.go b/internal/gitaly/service/operations/land_attempt.go
index 7009a5b..36e59b5 100644
--- a/internal/gitaly/service/operations/land_attempt.go
+++ b/internal/gitaly/service/operations/land_attempt.go
@@ -200,7 +200,7 @@ func (a *landingAttempt) decide(ctx context.Context, tip git.ObjectID, req *land
// A resolution from the caller lands as a merge commit of the tip and the change, when it was
// made against this tip; it replaces the server's attempt to merge or rebase.
if in.GetResolution() != nil {
- res, err := a.server.landWithResolution(ctx, in, a.repo, tip, commitID, req.committer)
+ res, err := a.server.landWithResolution(ctx, in, a.repo, tip, commitID, req.committer, req.precheck)
if err != nil {
var unresolved *landUnresolvedError
if errors.As(err, &unresolved) {
diff --git a/internal/gitaly/service/operations/land_batch.go b/internal/gitaly/service/operations/land_batch.go
index a8a0483..603e5df 100644
--- a/internal/gitaly/service/operations/land_batch.go
+++ b/internal/gitaly/service/operations/land_batch.go
@@ -29,6 +29,8 @@ type landBatchRequest struct {
// request, or its GitLab user ID. The standalone access policy is evaluated for the actor before the
// request changes the running tip of the batch, and the changelog records it for the member.
actor string
+ // precheck is the check of the request's resolution, run before the request entered its lane.
+ precheck *landPrecheck
}
// reference returns the branch the request lands on.
diff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.go
index 688acc8..47b8b1c 100644
--- a/internal/gitaly/service/operations/land_batch_server.go
+++ b/internal/gitaly/service/operations/land_batch_server.go
@@ -78,7 +78,7 @@ func (s *Server) landBatchable(ctx context.Context, in *gitalypb.UserLandRequest
// userLandBatched lands the request through its lane. The response is built from the outcome of
// the batch that executed the request.
-func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature) (*gitalypb.UserLandResponse, error) {
+func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {
repo := in.GetRepository()
key := landlane.Key{
Storage: repo.GetStorageName(),
@@ -91,6 +91,7 @@ func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandReque
in: in,
committer: committer,
actor: landBatchActor(ctx, in),
+ precheck: precheck,
}
resp, err := s.landLanes.Submit(ctx, key, req)
@@ -141,7 +142,7 @@ func (s *Server) executeLandBatch(ctx context.Context, _ landlane.Key, reqs []*l
return stepper, cleanup, nil
},
fallback: func(req *landBatchRequest) (*gitalypb.UserLandResponse, error) {
- return s.landWithRetries(req.ctx, req.in, req.committer)
+ return s.landWithRetries(req.ctx, req.in, req.committer, req.precheck)
},
giveUp: landBatchGiveUp,
backoff: func(ctx context.Context, attempt int) error {
diff --git a/internal/gitaly/service/operations/land_package_test.go b/internal/gitaly/service/operations/land_package_test.go
index 9ee5664..2f5e2e2 100644
--- a/internal/gitaly/service/operations/land_package_test.go
+++ b/internal/gitaly/service/operations/land_package_test.go
@@ -7,7 +7,9 @@ import (
"os"
"path/filepath"
"strings"
+ "sync"
"testing"
+ "time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
@@ -346,30 +348,21 @@ func TestUserLand_reland(t *testing.T) {
t.Run("the check goes in bounded requests, one batch at a time", func(t *testing.T) {
t.Parallel()
- // A bound of one byte: every file is a check request of its own, answered in turn.
- dir, argv := resolverScript(t, `cp "$DIR/answer-$N.json" "$DIR/answer.json"`)
- for n, p := range []string{"a", "b"} {
- raw, err := json.Marshal(resolver.CheckResponse{
- Version: resolver.ProtocolVersion, Operation: resolver.OperationCheck, Resolver: "graft-resolve",
- Files: []resolver.CheckFile{{Path: p, OK: true, Syntax: resolver.SyntaxUnchecked}},
- })
- require.NoError(t, err)
- require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("answer-%d.json", n+1)), raw, 0o600))
- }
- fx := newResolveFixture(t, config.GraftResolver{})
- s := fx.server(t, enabledResolver(argv))
- s.checkBatchBytes = 1
- req := fx.request("Land")
- req.ResolveConflicts = false
- req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ // A bound of one byte: every file is larger than the bound, so each is a check request of
+ // its own, answered in turn.
+ dir, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", true, ""))
+ fx, s, req := batchedReland(t, argv)
- res, err := s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer())
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err)
+ res, err := s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), pre)
require.NoError(t, err)
require.Equal(t, 2, resolverCalls(t, dir))
for n, p := range []string{"a", "b"} {
check := resolverRequest(t, dir, n+1)
require.Len(t, check.Files, 1)
require.Equal(t, p, check.Files[0].Path)
+ require.Empty(t, check.Messages, "the check carries no commit message")
}
require.Equal(t, 2, res.syntaxUnchecked, "the verdicts of every batch count")
commit, err := fx.repo.ReadCommit(fx.ctx, res.commit.Revision())
@@ -377,6 +370,121 @@ func TestUserLand_reland(t *testing.T) {
require.Contains(t, string(commit.GetBody()), "Graft-Checked-By: graft-resolve\n")
})
+ t.Run("a later batch that rejects keeps the verdict of the earlier one", func(t *testing.T) {
+ t.Parallel()
+
+ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", false, "b is wrong"))
+ fx, s, req := batchedReland(t, argv)
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err)
+ _, err = s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), pre)
+ pkg := builtUnresolved(t, fx, err)
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_RESOLVED, packageFile(t, pkg, "a").GetOutcome())
+ b := packageFile(t, pkg, "b")
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_REJECTED, b.GetOutcome())
+ require.Equal(t, "b is wrong", b.GetReason())
+ })
+
+ t.Run("a later batch that cannot be checked keeps the earlier verdicts and fails closed", func(t *testing.T) {
+ t.Parallel()
+
+ for name, second := range map[string]string{
+ "an error": "",
+ "another checker answering": checkAnswer("other-checker", "b", true, ""),
+ } {
+ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", false, "a is wrong"), second)
+ fx, s, req := batchedReland(t, argv)
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err, name)
+ _, err = s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), pre)
+ pkg := builtUnresolved(t, fx, err)
+ require.Contains(t, pkg.GetReason(), "the resolution could not be checked", name)
+ a := packageFile(t, pkg, "a")
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_REJECTED, a.GetOutcome(), name)
+ require.Equal(t, "a is wrong", a.GetReason(), name)
+ b := packageFile(t, pkg, "b")
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_NOT_ATTEMPTED, b.GetOutcome(), name)
+ require.False(t, b.GetSyntaxUnchecked(), name)
+ }
+ })
+
+ t.Run("the whole check has one deadline", func(t *testing.T) {
+ t.Parallel()
+
+ // Each check request takes 0.7 s and the deadline of the whole check is 1 s: the second
+ // request runs out of time, and the check fails closed.
+ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", true, ""))
+ slow := append([]string{"sh", "-c", `sleep 0.7; exec "$0"`}, argv...)
+ fx, s, req := batchedReland(t, slow)
+ s.resolveCfg.Timeout = "1s"
+ client, err := resolver.New(s.resolveCfg)
+ require.NoError(t, err)
+ s.conflictResolver = client
+
+ start := time.Now()
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err)
+ require.Less(t, time.Since(start), 2*time.Second, "the check stops at its deadline")
+ _, err = s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), pre)
+ pkg := builtUnresolved(t, fx, err)
+ require.Contains(t, pkg.GetReason(), "the resolution could not be checked")
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_RESOLVED, packageFile(t, pkg, "a").GetOutcome())
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_NOT_ATTEMPTED, packageFile(t, pkg, "b").GetOutcome())
+ })
+
+ t.Run("without a check for its tip the land fails closed", func(t *testing.T) {
+ t.Parallel()
+
+ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""))
+ fx, s, req := batchedReland(t, argv)
+ _, err := s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), nil)
+ pkg := builtUnresolved(t, fx, err)
+ require.Contains(t, pkg.GetReason(), "it was not checked against tip")
+ })
+
+ t.Run("the check runs outside the lane: another land of the branch is not held", func(t *testing.T) {
+ t.Parallel()
+
+ // The checker takes 3 s; meanwhile a fast-forward of the branch lands at once.
+ dir, argv := resolverScript(t, "sleep 3")
+ writeRawAnswer(t, dir, resolver.CheckResponse{
+ Version: resolver.ProtocolVersion, Operation: resolver.OperationCheck, Resolver: "graft-resolve",
+ Files: []resolver.CheckFile{{Path: "a", OK: true, Syntax: resolver.SyntaxNone}, {Path: "b", OK: true, Syntax: resolver.SyntaxNone}},
+ })
+ fx := newResolveFixture(t, enabledResolver(argv))
+ forward := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.tip), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "ours\n"},
+ gittest.TreeEntry{Mode: "100644", Path: "b", Content: "ours\n"},
+ gittest.TreeEntry{Mode: "100644", Path: "c", Content: "c\n"},
+ gittest.TreeEntry{Mode: "100644", Path: "e", Content: "e\n"},
+ ))
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+
+ var wg sync.WaitGroup
+ var relandDone, forwardDone time.Time
+ var relandErr, forwardErr error
+ wg.Add(2)
+ go func() {
+ defer wg.Done()
+ _, relandErr = fx.client.UserLand(fx.ctx, req)
+ relandDone = time.Now()
+ }()
+ go func() {
+ defer wg.Done()
+ time.Sleep(300 * time.Millisecond)
+ _, forwardErr = fx.client.UserLand(fx.ctx, landRequest(fx.repoProto, git.DefaultBranch, forward))
+ forwardDone = time.Now()
+ }()
+ wg.Wait()
+ require.NoError(t, forwardErr)
+ require.True(t, forwardDone.Before(relandDone), "the fast-forward landed while the check ran")
+ // The branch moved, so the resolution made for the old tip is refused with a fresh package.
+ requireMergeConflict(t, relandErr)
+ require.Contains(t, landConflictDetail(relandErr).GetReason(), "the branch tip moved")
+ })
+
t.Run("a merge with more conflicting files than a package lists is refused", func(t *testing.T) {
t.Parallel()
@@ -722,6 +830,37 @@ func TestUserLand_conflictPackageSidesAreKept(t *testing.T) {
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, tip)
})
+ t.Run("a criss-cross history whose base is virtual", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ writeAnswer(t, dir, validAnswer())
+ fx := newResolveFixture(t, enabledResolver(argv))
+ // The merge bases x and y disagree on a: merge-tree merges them into a virtual base of its
+ // own, a blob in no tree, which the failed land discards.
+ x := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "x\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ y := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "y\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ tip := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(x, y), gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "tip\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ commit := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(y, x), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "commit\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ fx.tip, fx.commit = tip, commit
+
+ req := fx.request("Land")
+ req.Strategy = gitalypb.UserLandRequest_STRATEGY_MERGE
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ a := packageFile(t, pkg, "a")
+ require.Empty(t, a.GetBase().GetOid(), "the virtual base is not kept")
+ require.Equal(t, landSideUnavailable, a.GetBase().GetUnavailable())
+ require.Empty(t, a.GetHunks())
+ requirePackageOIDsKept(t, fx, pkg)
+ })
+
t.Run("an unclean merge without a conflicting file", func(t *testing.T) {
t.Parallel()
@@ -753,3 +892,45 @@ func TestUserLand_conflictPackageSidesAreKept(t *testing.T) {
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
})
}
+
+// checkAnswer is the check response of a checker named checker for one file; an empty answer
+// stands for a call that fails.
+func checkAnswer(checker, path string, ok bool, reason string) string {
+ raw, _ := json.Marshal(resolver.CheckResponse{
+ Version: resolver.ProtocolVersion, Operation: resolver.OperationCheck, Resolver: checker,
+ Files: []resolver.CheckFile{{Path: path, OK: ok, Reason: reason, Syntax: resolver.SyntaxUnchecked}},
+ })
+ return string(raw)
+}
+
+// perCallChecker is a resolver script that gives answers[N-1] to its N-th call, and fails a call
+// whose answer is empty.
+func perCallChecker(t *testing.T, answers ...string) (string, []string) {
+ t.Helper()
+ dir, argv := resolverScript(t, `[ -s "$DIR/answer-$N.json" ] || exit 1; cp "$DIR/answer-$N.json" "$DIR/answer.json"`)
+ for n, a := range answers {
+ require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("answer-%d.json", n+1)), []byte(a), 0o600))
+ }
+ return dir, argv
+}
+
+// batchedReland is the fixture's re-land of a and b on a server that checks every file in a
+// request of its own.
+func batchedReland(t *testing.T, argv []string) (resolveFixture, *Server, *gitalypb.UserLandRequest) {
+ t.Helper()
+ fx := newResolveFixture(t, config.GraftResolver{})
+ s := fx.server(t, enabledResolver(argv))
+ s.checkBatchBytes = 1
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ return fx, s, req
+}
+
+// builtUnresolved returns the package of an unresolved land.
+func builtUnresolved(t *testing.T, fx resolveFixture, err error) *gitalypb.LandConflict {
+ t.Helper()
+ var unresolved *landUnresolvedError
+ require.ErrorAs(t, err, &unresolved)
+ return builtPackage(t, fx.ctx, unresolved)
+}
diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.go
index 27ce705..fe3bcad 100644
--- a/internal/gitaly/service/operations/land_reland.go
+++ b/internal/gitaly/service/operations/land_reland.go
@@ -34,69 +34,68 @@ func landAgentResolver(actor string) string {
return "agent/" + actor
}
-// landWithResolution lands commitID onto tip with the caller's resolution of the conflicts
-// (UserLandRequest.resolution, PLAN10 3.3 and 12). It is accepted only when it was made against
-// this tip and this commit and resolves exactly the paths that conflict, each once. Every file
-// gets the acceptance rules of a resolver answer; when a resolver is configured, the files that
-// may be sent to it also get its checks (the check operation: the syntax check and the configured
-// checks), and a failing or failed check fails the land. Nothing lands partially: an unaccepted
-// resolution fails with a fresh conflict package (*landUnresolvedError).
-//
-// The merge commit carries Graft-Resolved-By: agent/<actor> and, when the resolver checked the
-// resolution, Graft-Checked-By: <resolver>.
-func (s *Server) landWithResolution(
- ctx context.Context,
- in *gitalypb.UserLandRequest,
- repo *localrepo.Repo,
- tip, commitID git.ObjectID,
- committer git.Signature,
-) (landResolution, error) {
+// landReview is an agent's resolution held against the merge it resolves, as landReviewResolution
+// leaves it: the conflicting files, what resolves each, and whether a rule of an answer rejected a
+// file (its outcome says which).
+type landReview struct {
+ listing landListing
+ resolved map[string]resolver.ResponseFile
+ actor string
+ rejected bool
+}
+
+// landReviewResolution holds the resolution of the request against the merge of tip and commitID:
+// it is accepted only when it was made against this tip and this commit and resolves exactly the
+// paths that conflict, each once, and every file gets the acceptance rules of a resolver answer.
+// A refusal is the error (a *landUnresolvedError with its package, or a gRPC error); a file that
+// breaks a rule of an answer is REJECTED in the review.
+func (s *Server) landReviewResolution(ctx context.Context, in *gitalypb.UserLandRequest, repo *localrepo.Repo, tip, commitID git.ObjectID) (landReview, error) {
r := in.GetResolution()
if r.GetCommit() != commitID.String() {
- return landResolution{}, structerr.NewInvalidArgument("resolution is for commit %q, not commit_id %q", r.GetCommit(), commitID)
+ return landReview{}, structerr.NewInvalidArgument("resolution is for commit %q, not commit_id %q", r.GetCommit(), commitID)
}
l, err := landListConflicts(ctx, repo, tip, commitID, s.resolveCfg.MaxFileBytesOrDefault())
if errors.Is(err, errLandNoConflicts) {
- return landResolution{}, structerr.NewFailedPrecondition("the merge of tip %s and commit %s has no conflicts, so there is nothing to resolve: land without a resolution", tip, commitID)
+ return landReview{}, structerr.NewFailedPrecondition("the merge of tip %s and commit %s has no conflicts, so there is nothing to resolve: land without a resolution", tip, commitID)
}
if err != nil {
- return landResolution{}, err
+ return landReview{}, err
}
if len(l.files) == 0 {
- return landResolution{}, l.notAttempted(landNoConflictingFiles, nil)
+ return landReview{}, l.notAttempted(landNoConflictingFiles, nil)
}
// A merge with more conflicting files than a package lists cannot be resolved through a land,
// whatever the resolution: its package says so, and the land refuses it before reading anything.
if len(l.files) > landMaxPackageFiles {
- return landResolution{}, l.notAttempted(fmt.Sprintf("the merge has %d conflicting files, more than the %d that a conflict package lists: it cannot be resolved through a land", len(l.files), landMaxPackageFiles), nil)
+ return landReview{}, l.notAttempted(fmt.Sprintf("the merge has %d conflicting files, more than the %d that a conflict package lists: it cannot be resolved through a land", len(l.files), landMaxPackageFiles), nil)
}
if r.GetTip() != tip.String() {
- return landResolution{}, l.notAttempted(fmt.Sprintf("the branch tip moved from %s to %s since the resolution was made: resolve the conflicts of this package", r.GetTip(), tip), nil)
+ return landReview{}, l.notAttempted(fmt.Sprintf("the branch tip moved from %s to %s since the resolution was made: resolve the conflicts of this package", r.GetTip(), tip), nil)
}
if l.bases > 1 {
- return landResolution{}, l.notAttempted(fmt.Sprintf("the change has %d merge bases (a criss-cross history), which are not resolved", l.bases), nil)
+ return landReview{}, l.notAttempted(fmt.Sprintf("the change has %d merge bases (a criss-cross history), which are not resolved", l.bases), nil)
}
// The merge commit names the actor and every resolved path in its trailers: a value that cannot
// be one trailer line is refused before anything else, and resolving the path again cannot help.
actor := identity.Actor(ctx, "")
if !landTrailerValueValid(landAgentResolver(actor)) {
- return landResolution{}, structerr.NewFailedPrecondition("the actor %q cannot be recorded in the %s trailer: it must be one line of printable UTF-8", actor, landTrailerResolvedBy)
+ return landReview{}, structerr.NewFailedPrecondition("the actor %q cannot be recorded in the %s trailer: it must be one line of printable UTF-8", actor, landTrailerResolvedBy)
}
for _, f := range l.files {
if !landTrailerValueValid(f.conflict.path) {
- return landResolution{}, structerr.NewFailedPrecondition("the conflicting path %q cannot be recorded in the %s trailer: it must be one line of printable UTF-8, so its conflict cannot be resolved through a land", f.conflict.path, landTrailerResolvedPath)
+ return landReview{}, structerr.NewFailedPrecondition("the conflicting path %q cannot be recorded in the %s trailer: it must be one line of printable UTF-8, so its conflict cannot be resolved through a land", f.conflict.path, landTrailerResolvedPath)
}
}
resolved, reason := landResolutionFiles(r, l.files)
if reason != "" {
- return landResolution{}, l.notAttempted(reason, nil)
+ return landReview{}, l.notAttempted(reason, nil)
}
+ review := landReview{listing: l, resolved: resolved, actor: actor}
maxBytes := s.resolveCfg.MaxFileBytesOrDefault()
- rejected := false
for _, f := range l.files {
rf := resolved[f.conflict.path]
err := resolver.ValidateFile(rf, maxBytes)
@@ -105,14 +104,122 @@ func (s *Server) landWithResolution(
}
if err != nil {
f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_REJECTED, err.Error()
- rejected = true
+ review.rejected = true
}
}
+ return review, nil
+}
+
+// landPrecheck is the check of an agent's resolution, run before the land enters its lane
+// (landPrecheckResolution): for the tip and the commit it was run against, the result.
+type landPrecheck struct {
+ tip, commit git.ObjectID
+ result landCheckResult
+}
+
+// landPrecheckResolution runs the resolver's checks of the request's resolution before the land
+// enters its lane, so that no branch is held while an external process runs: the lane only applies
+// the verdicts (landWithResolution). The check is run against the tip the resolution was made
+// against, which is the only tip the land accepts it at; if the branch has moved by the time the
+// lane runs the land, the land refuses the resolution with a fresh package, as it would anyway.
+// The whole check has one deadline, the resolver's timeout: every check request uses what remains
+// of it, and its expiry fails the check closed.
+//
+// It returns nil when there is nothing to check: no resolver, or a resolution the land refuses
+// before checking (the lane reports why).
+func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLandRequest, repo *localrepo.Repo) (*landPrecheck, error) {
+ if s.conflictResolver == nil {
+ return nil, nil
+ }
+ objectHash, err := repo.ObjectHash(ctx)
+ if err != nil {
+ return nil, structerr.NewInternal("detecting object hash: %w", err)
+ }
+ tip, err := objectHash.FromHex(in.GetResolution().GetTip())
+ if err != nil {
+ return nil, nil
+ }
+ commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}"))
+ if err != nil {
+ return nil, nil
+ }
+ review, err := s.landReviewResolution(ctx, in, repo, tip, commitID)
+ if err != nil || review.rejected {
+ // The lane refuses it the same way, before any check.
+ return nil, nil
+ }
+
+ timeout, err := s.resolveCfg.TimeoutOrDefault()
+ if err != nil {
+ return nil, structerr.NewInternal("resolver timeout: %w", err)
+ }
+ ctx, cancel := context.WithTimeout(ctx, timeout)
+ defer cancel()
+ result, err := s.landCheckResolution(ctx, in, repo, review.listing, review.actor, review.resolved)
+ if err != nil {
+ return nil, err
+ }
+ return &landPrecheck{tip: tip, commit: commitID, result: result}, nil
+}
+
+// landWithResolution lands commitID onto tip with the caller's resolution of the conflicts
+// (UserLandRequest.resolution, PLAN10 3.3 and 12), held against the merge by
+// landReviewResolution. When a resolver is configured, the files that may be sent to it must pass
+// its checks (the check operation: the syntax check and the configured checks), which ran before
+// the lane (precheck, for this tip and commit): a failing or failed check fails the land, and a
+// resolution without a check for this tip and commit fails closed. Nothing lands partially: an
+// unaccepted resolution fails with a fresh conflict package (*landUnresolvedError).
+//
+// The merge commit carries Graft-Resolved-By: agent/<actor> and, when the resolver checked the
+// resolution, Graft-Checked-By: <resolver>.
+func (s *Server) landWithResolution(
+ ctx context.Context,
+ in *gitalypb.UserLandRequest,
+ repo *localrepo.Repo,
+ tip, commitID git.ObjectID,
+ committer git.Signature,
+ precheck *landPrecheck,
+) (landResolution, error) {
+ review, err := s.landReviewResolution(ctx, in, repo, tip, commitID)
+ if err != nil {
+ return landResolution{}, err
+ }
+ l, rejected := review.listing, review.rejected
checkedBy, syntaxUnchecked := "", 0
if !rejected && s.conflictResolver != nil {
- if checkedBy, syntaxUnchecked, err = s.landCheckResolution(ctx, in, repo, l, actor, resolved); err != nil {
- return landResolution{}, err
+ if precheck == nil || precheck.tip != tip || precheck.commit != commitID {
+ return landResolution{}, l.notAttempted(fmt.Sprintf("the resolution could not be checked: it was not checked against tip %s and commit %s before the land", tip, commitID), nil)
+ }
+ res := precheck.result
+ for _, f := range l.files {
+ v, ok := res.verdicts[f.conflict.path]
+ if !ok {
+ continue
+ }
+ if v.rejected != "" {
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_REJECTED, v.rejected
+ }
+ f.syntaxUnchecked = v.syntaxUnchecked
+ }
+ if res.failure != "" {
+ // The files checked before the check failed keep their verdict; the others were not
+ // attempted.
+ for _, f := range l.files {
+ if v, ok := res.verdicts[f.conflict.path]; !ok {
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_NOT_ATTEMPTED, ""
+ } else if v.rejected == "" {
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_RESOLVED, ""
+ }
+ }
+ reason := "the resolution could not be checked: " + res.failure
+ return landResolution{}, l.unresolved(reason, reason, res.failureErr)
+ }
+ checkedBy = res.checkedBy
+ for _, v := range res.verdicts {
+ if v.syntaxUnchecked {
+ syntaxUnchecked++
+ }
}
}
for _, f := range l.files {
@@ -129,11 +236,11 @@ func (s *Server) landWithResolution(
return landResolution{}, l.unresolved("", "the resolution was rejected: "+landUnresolvedCause(l.files), nil)
}
- resp := &resolver.Response{Version: resolver.ProtocolVersion, Resolver: landAgentResolver(actor)}
+ resp := &resolver.Response{Version: resolver.ProtocolVersion, Resolver: landAgentResolver(review.actor)}
for _, f := range l.files {
- resp.Files = append(resp.Files, resolved[f.conflict.path])
+ resp.Files = append(resp.Files, review.resolved[f.conflict.path])
}
- res, err := s.landWriteResolution(ctx, in, repo, tip, commitID, l.mergedTree, committer, actor, resp, checkedBy)
+ res, err := s.landWriteResolution(ctx, in, repo, tip, commitID, l.mergedTree, committer, review.actor, resp, checkedBy)
res.syntaxUnchecked = syntaxUnchecked
return res, l.verifyRejected(err)
}
@@ -180,25 +287,39 @@ func landResolutionFiles(r *gitalypb.LandResolution, files []*landFileState) (ma
return resolved, ""
}
-// landCheckBatchBytes bounds the file data (the sides and the resolution) of one check request:
-// half of resolver.MaxCheckRequestBytes, since base64 and JSON make the request about 4/3 of it.
-// A test may set a smaller bound on the server (checkBatchBytes).
+// landCheckVerdict is the verdict of the resolver's check on one file.
+type landCheckVerdict struct {
+ // rejected is the reason of a check the file failed, empty when it passed.
+ rejected string
+ syntaxUnchecked bool
+}
+
+// landCheckResult is what the checks of an agent's resolution found: the checker's name (for the
+// Graft-Checked-By trailer, "" when no file could be sent), the verdict of every file checked, and,
+// when a check could not run, why (the check fails closed: the land does not proceed).
+type landCheckResult struct {
+ checkedBy string
+ verdicts map[string]landCheckVerdict
+ failure string
+ failureErr error
+}
+
+// landCheckBatchBytes bounds the file data (the sides and the resolution) of one check request,
+// resolver.CheckBatchBytes. A test may set a smaller bound on the server (checkBatchBytes).
func (s *Server) landCheckBatchBytes() int64 {
if s.checkBatchBytes > 0 {
return s.checkBatchBytes
}
- return resolver.MaxCheckRequestBytes / 2
+ return resolver.CheckBatchBytes
}
// landCheckResolution runs the resolver's checks on the files of the resolution that may be sent
-// to the resolver (landClassify), and sets the outcome of each checked file that fails them to
-// REJECTED. It returns the checker's name for the Graft-Checked-By trailer, or "" when no file
-// could be sent, and the number of files accepted without a syntax verdict.
-//
-// The files go in check requests of at most landCheckBatchBytes of file data each, so that
-// neither the server nor graft-resolve holds more than one bounded request at a time: each batch
-// is read, checked and released before the next. Every batch must name the same checker. The
-// check fails closed: a check that cannot run is a *landUnresolvedError with a fresh package.
+// to the resolver (landClassify). The files go in check requests of at most landCheckBatchBytes of
+// file data each, so that neither the server nor graft-resolve holds more than one bounded
+// request at a time: each batch is read, checked and released before the next, and every batch must
+// name the same checker. The listing and the attributes of the trees are read once for all batches.
+// The requests carry no commit message: the checks judge the files. A check that cannot run (ctx
+// expired included) stops the checks: the result says why, with the verdicts found before.
func (s *Server) landCheckResolution(
ctx context.Context,
in *gitalypb.UserLandRequest,
@@ -206,29 +327,28 @@ func (s *Server) landCheckResolution(
l landListing,
actor string,
resolved map[string]resolver.ResponseFile,
-) (string, int, error) {
+) (landCheckResult, error) {
+ result := landCheckResult{verdicts: map[string]landCheckVerdict{}}
batches, err := landCheckBatches(ctx, repo, l.files, resolved, s.landCheckBatchBytes())
if err != nil {
- return "", 0, err
+ return result, err
}
+ cache := newLandClassifyCache(l.files)
- checkedBy := ""
- syntaxUnchecked, checkedFiles := 0, 0
var unchecked []string
for _, batch := range batches {
- if err := s.landClassify(ctx, repo, batch, l.base, l.tip, l.commitID); err != nil {
- return "", 0, err
+ if err := s.landClassify(ctx, repo, batch, l.base, l.tip, l.commitID, cache); err != nil {
+ return result, err
}
req := resolver.Request{
Repository: resolver.Repository{
Storage: in.GetRepository().GetStorageName(),
RelativePath: in.GetRepository().GetRelativePath(),
},
- Branch: string(in.GetBranch()),
- Tip: l.tip.String(),
- Commit: l.commitID.String(),
- Messages: resolver.Messages{Tip: l.tipMessage, Commit: l.commitMessage},
- Actor: actor,
+ Branch: string(in.GetBranch()),
+ Tip: l.tip.String(),
+ Commit: l.commitID.String(),
+ Actor: actor,
}
if l.hasBase {
req.MergeBase = l.base.String()
@@ -257,56 +377,52 @@ func (s *Server) landCheckResolution(
continue
}
- // Fail closed: a check that cannot run (an error, an answer that is no valid check
- // response, a resolver that does not know the check operation) fails the land.
resp, err := s.conflictResolver.Check(ctx, req)
release()
- if err != nil {
- return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: %v", err), err)
+ switch {
+ case err != nil:
+ result.failure, result.failureErr = err.Error(), err
+ case !landResolverNameValid(resp.Resolver):
+ result.failure = fmt.Sprintf("the check names an invalid resolver %q", resp.Resolver)
+ case result.checkedBy != "" && resp.Resolver != result.checkedBy:
+ result.failure = fmt.Sprintf("its checks named the resolvers %q and %q", result.checkedBy, resp.Resolver)
}
- if !landResolverNameValid(resp.Resolver) {
- return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: the check names an invalid resolver %q", resp.Resolver), nil)
+ if result.failure != "" {
+ return result, nil
}
- if checkedBy != "" && resp.Resolver != checkedBy {
- return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: its checks named the resolvers %q and %q", checkedBy, resp.Resolver), nil)
- }
- checkedBy = resp.Resolver
+ result.checkedBy = resp.Resolver
for i, v := range resp.Files {
- f := checked[i]
+ verdict := landCheckVerdict{syntaxUnchecked: v.OK && v.Syntax == resolver.SyntaxUnchecked}
if !v.OK {
- f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_REJECTED, v.Reason
- continue
- }
- if v.Syntax == resolver.SyntaxUnchecked {
- f.syntaxUnchecked = true
- syntaxUnchecked++
+ verdict.rejected = v.Reason
}
+ result.verdicts[checked[i].conflict.path] = verdict
}
- checkedFiles += len(checked)
}
- if checkedFiles == 0 {
+ if len(result.verdicts) == 0 {
s.logger.WithField("unchecked", unchecked).InfoContext(ctx, "graft: no file of the agent's resolution may be sent to the resolver's checks")
- return "", 0, nil
+ return result, nil
}
s.logger.WithFields(log.Fields{
- "checked_by": checkedBy,
- "checked": checkedFiles,
- "check_requests": len(batches),
- "syntax_unchecked": syntaxUnchecked,
- "not_checked": unchecked,
+ "checked_by": result.checkedBy,
+ "checked": len(result.verdicts),
+ "check_requests": len(batches),
+ "not_checked": unchecked,
}).InfoContext(ctx, "graft: agent's resolution checked")
- return checkedBy, syntaxUnchecked, nil
+ return result, nil
}
// landCheckBatches splits the files into batches of at most budget bytes of file data: the sizes
-// of their sides (from the object info, before reading) and of their resolution. A file larger than
-// the budget is a batch of its own; its sides are within max_file_bytes, or it is not sent.
+// of their sides (from the object info, before reading) and of their resolution, plus what a file
+// adds to the request around them (resolver.CheckFileOverheadBytes for its path). A file larger
+// than the budget is a batch of its own, which fits a check request since max_file_bytes is at
+// most resolver.MaxCheckFileBytes.
func landCheckBatches(ctx context.Context, repo *localrepo.Repo, files []*landFileState, resolved map[string]resolver.ResponseFile, budget int64) ([][]*landFileState, error) {
var batches [][]*landFileState
var cur []*landFileState
var size int64
for _, f := range files {
- n := int64(len(resolved[f.conflict.path].Content))
+ n := int64(len(resolved[f.conflict.path].Content)) + resolver.CheckFileOverheadBytes(f.conflict.path)
for _, b := range []*landBlob{f.conflict.base, f.conflict.ours, f.conflict.theirs} {
if b == nil || (b.mode != resolver.ModeRegular && b.mode != resolver.ModeExecutable && b.mode != resolver.ModeSymlink) {
continue
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index 201f026..931d557 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -119,7 +119,7 @@ func (s *Server) resolveLandConflicts(
return landResolution{}, notAttempted(fmt.Sprintf("%d conflicting files exceed max_files %d", len(conflicts), maxFiles))
}
- if err := s.landClassify(ctx, repo, files, base, tip, commitID); err != nil {
+ if err := s.landClassify(ctx, repo, files, base, tip, commitID, newLandClassifyCache(files)); err != nil {
return landResolution{}, err
}
@@ -475,9 +475,10 @@ func (s *Server) landClassify(
repo *localrepo.Repo,
files []*landFileState,
base, tip, commitID git.ObjectID,
+ cache *landClassifyCache,
) error {
rules := s.conflictResolver.SendRules()
- listings := map[git.ObjectID]map[git.ObjectID][]string{}
+ listings := cache.listings
for _, f := range files {
c := f.conflict
switch {
@@ -556,7 +557,7 @@ func (s *Server) landClassify(
candidates = append(candidates, f.conflict.path)
}
}
- generated, err := landGeneratedPaths(ctx, repo, tip, candidates)
+ generated, err := cache.generatedPaths(ctx, repo, tip, candidates)
if err != nil {
return err
}
@@ -571,6 +572,40 @@ func (s *Server) landClassify(
return nil
}
+// landClassifyCache holds what landClassify reads from the trees, for every call of one land: the
+// listings of the trees (by revision) and the linguist-generated attribute of the paths, read once
+// for all the paths of the land.
+type landClassifyCache struct {
+ listings map[git.ObjectID]map[git.ObjectID][]string
+ paths []string
+ generated map[string]bool
+}
+
+// newLandClassifyCache returns the cache of the land of files.
+func newLandClassifyCache(files []*landFileState) *landClassifyCache {
+ c := &landClassifyCache{listings: map[git.ObjectID]map[git.ObjectID][]string{}}
+ for _, f := range files {
+ c.paths = append(c.paths, f.conflict.path)
+ }
+ return c
+}
+
+// generatedPaths returns the linguist-generated attribute of the paths (all of them paths of the
+// land), read at rev with one git check-attr for every path of the land, on first use.
+func (c *landClassifyCache) generatedPaths(ctx context.Context, repo *localrepo.Repo, rev git.ObjectID, paths []string) (map[string]bool, error) {
+ if len(paths) == 0 {
+ return map[string]bool{}, nil
+ }
+ if c.generated == nil {
+ generated, err := landGeneratedPaths(ctx, repo, rev, c.paths)
+ if err != nil {
+ return nil, err
+ }
+ c.generated = generated
+ }
+ return c.generated, nil
+}
+
// landShapeReason returns why the shape of the sides keeps a conflict from the resolver, or "". A
// file/directory conflict has no version of the file on either side under its name. A conflict
// whose base is absent while one side is absent is not a conflict between two versions of a file,
diff --git a/internal/gitaly/service/operations/land_resolve_paths_test.go b/internal/gitaly/service/operations/land_resolve_paths_test.go
index 7b25bd3..b5f456e 100644
--- a/internal/gitaly/service/operations/land_resolve_paths_test.go
+++ b/internal/gitaly/service/operations/land_resolve_paths_test.go
@@ -95,7 +95,7 @@ func TestLandClassify_absentFromBothSides(t *testing.T) {
s := fx.server(t, rcfg)
f := &landFileState{conflict: landFileConflict{path: "x~" + fx.tip.String()}}
- require.NoError(t, s.landClassify(fx.ctx, fx.repo, []*landFileState{f}, fx.base, fx.tip, fx.commit))
+ require.NoError(t, s.landClassify(fx.ctx, fx.repo, []*landFileState{f}, fx.base, fx.tip, fx.commit, newLandClassifyCache([]*landFileState{f})))
require.Equal(t, gitalypb.LandConflictFile_OUTCOME_NOT_SENT, f.outcome)
require.Equal(t, landNotSentNoSide, f.reason)
}
diff --git a/internal/graft/resolver/check.go b/internal/graft/resolver/check.go
index bd3e511..255a866 100644
--- a/internal/graft/resolver/check.go
+++ b/internal/graft/resolver/check.go
@@ -12,6 +12,23 @@ import (
// within this bound.
const MaxCheckRequestBytes = 64 << 20
+// CheckBatchBytes bounds the file data of one check request: the sides and the resolution of its
+// files, plus CheckFileOverheadBytes for each. base64 makes the contents 4/3 larger, and the
+// request's envelope takes at most maxEnvelopeBytes, so the request stays within
+// MaxCheckRequestBytes.
+const CheckBatchBytes = (MaxCheckRequestBytes - maxEnvelopeBytes) * 3 / 4
+
+// MaxCheckFileBytes is the largest max_file_bytes a resolver can have: one file, its three sides
+// and its resolution, must fit one check request.
+const MaxCheckFileBytes = CheckBatchBytes/4 - 4<<10
+
+// CheckFileOverheadBytes is what a file adds to a check request besides its contents: its path,
+// written in the file and in the resolution, escaped in JSON (at most 6 bytes a byte), and the
+// names and values of the other fields.
+func CheckFileOverheadBytes(path string) int64 {
+ return int64(2*6*len(path)) + 1<<10
+}
+
// ErrCheckUnsupported is returned by Check when the resolver answers a check request with
// something other than a check response, as a resolver that predates the check operation does.
var ErrCheckUnsupported = errors.New("the resolver does not support the check operation")
diff --git a/internal/graft/resolver/client.go b/internal/graft/resolver/client.go
index a63b749..bc08727 100644
--- a/internal/graft/resolver/client.go
+++ b/internal/graft/resolver/client.go
@@ -130,6 +130,11 @@ func New(cfg config.GraftResolver) (*Client, error) {
if cfg.MaxFiles < 0 || cfg.MaxFileBytes < 0 {
return nil, errors.New("graft land resolver: max_files and max_file_bytes must not be negative")
}
+ // The check of an agent's resolution sends a file's three sides and its resolution in one
+ // request, which must fit its bound.
+ if mfb := cfg.MaxFileBytesOrDefault(); mfb > MaxCheckFileBytes {
+ return nil, fmt.Errorf("graft land resolver: max_file_bytes %d is too large: the check of a resolution sends a file's three sides and its resolution in one request of at most %d bytes, so max_file_bytes can be at most %d", mfb, MaxCheckRequestBytes, MaxCheckFileBytes)
+ }
rules, err := NewSendRules(cfg)
if err != nil {
diff --git a/internal/graft/resolver/client_test.go b/internal/graft/resolver/client_test.go
index ee3be60..d34376c 100644
--- a/internal/graft/resolver/client_test.go
+++ b/internal/graft/resolver/client_test.go
@@ -148,6 +148,8 @@ func TestNew_Errors(t *testing.T) {
{name: "zero timeout", cfg: config.GraftResolver{Enabled: true, Command: []string{"/bin/true"}, Timeout: "0s"}},
{name: "negative max files", cfg: config.GraftResolver{Enabled: true, Command: []string{"/bin/true"}, MaxFiles: -1}},
{name: "negative max file bytes", cfg: config.GraftResolver{Enabled: true, Command: []string{"/bin/true"}, MaxFileBytes: -1}},
+ // The three sides and the resolution of a file must fit one check request.
+ {name: "max file bytes over the check bound", cfg: config.GraftResolver{Enabled: true, Command: []string{"/bin/true"}, MaxFileBytes: MaxCheckFileBytes + 1}},
{name: "url without scheme", cfg: config.GraftResolver{Enabled: true, URL: "r.example/resolve"}},
{name: "url with ftp scheme", cfg: config.GraftResolver{Enabled: true, URL: "ftp://r.example/resolve"}},
{name: "url without host", cfg: config.GraftResolver{Enabled: true, URL: "https:///resolve"}},
@@ -443,11 +445,15 @@ func TestCheck_refusesARequestOverItsBound(t *testing.T) {
marker := filepath.Join(t.TempDir(), "ran")
cfg := commandConfig([]string{writeScript(t, fmt.Sprintf("touch '%s'\ncat > /dev/null\n", marker))})
- cfg.MaxFileBytes = MaxCheckRequestBytes
+ cfg.MaxFileBytes = MaxCheckFileBytes
client := newClient(t, cfg)
+ // Five sides of max_file_bytes each, over the bound once encoded.
req := sampleRequest()
- req.Files[0].Ours.Content = make([]byte, MaxCheckRequestBytes*3/4+1)
+ big := make([]byte, MaxCheckFileBytes)
+ for _, side := range []*Side{req.Files[0].Base, req.Files[0].Ours, req.Files[0].Theirs, req.Files[1].Base, req.Files[1].Theirs} {
+ side.Content = big
+ }
req.Resolution = validResponse("merged\n").Files
_, err := client.Check(testhelper.Context(t), req)
require.ErrorContains(t, err, "is over the bound")