Skip to content

Git designed for AI agents.

land: fail closed on the resolver, precheck in a quarantine, bounded checks

Federico Eliafcf5f8dc0cc8
diff --git a/cmd/graft/conflicts.go b/cmd/graft/conflicts.go
index 26005e5..d67e89d 100644
--- a/cmd/graft/conflicts.go
+++ b/cmd/graft/conflicts.go
@@ -225,7 +225,9 @@ func fetchSides(ctx context.Context, ws *workspace.Workspace, c *gitalypb.LandCo
}
// 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.
+// the file cannot embed. graft land --resolved refuses a file that still holds such a line, early
+// and naming the directory of the sides; the server refuses it too (resolver.ConflictPointerPrefix,
+// which it equals: the CLI does not link the resolver package).
const conflictPointerPrefix = "graft-conflict-pointer: "
// conflictFileFor is the file an agent edits for a conflicting file of the package, given the
@@ -247,10 +249,14 @@ func conflictFileFor(f *gitalypb.LandConflictFile, sides [3][]byte) []byte {
side := func(i int) (note string, body []byte) {
switch {
- case present[i] == nil && i == 0:
+ case present[i] == nil && i == 0 && present[1] != nil && present[2] != nil:
return ": none, both sides added the file", nil
- case present[i] == nil:
+ case present[i] == nil && i == 0:
+ return ": none", nil
+ case present[i] == nil && present[0] != nil:
return ": deleted", nil
+ case present[i] == nil:
+ return ": absent", nil
case sides[i] == nil:
reason := present[i].GetUnavailable()
if reason == "" {
diff --git a/cmd/graft/conflicts_test.go b/cmd/graft/conflicts_test.go
index b76d0a0..a34cb20 100644
--- a/cmd/graft/conflicts_test.go
+++ b/cmd/graft/conflicts_test.go
@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
+ "github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
@@ -54,11 +55,12 @@ func TestWriteConflictsRefusesAnExistingDirectory(t *testing.T) {
require.ErrorContains(t, err, "already holds a conflicts directory")
}
-// 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
+// TestConflictsWriterRefusesAnExistingFileWithoutOverwriting: the writer creates every file anew,
+// so the second of two paths that the file system takes for one file finds that file in place: it
+// is refused, the error names both paths, and the first file is not overwritten. 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) {
+func TestConflictsWriterRefusesAnExistingFileWithoutOverwriting(t *testing.T) {
t.Parallel()
dir := t.TempDir()
@@ -132,6 +134,18 @@ func TestConflictFileFor(t *testing.T) {
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"},
},
+ {
+ // No base and one side (a file/directory conflict, or a rename): the note says what
+ // the sides are, not that both sides added the file.
+ name: "no base and ours absent", file: file(nil, nil, side),
+ sides: [3][]byte{nil, nil, []byte("theirs\n")},
+ want: []string{"<<<<<<< ours (the branch tip): absent\n||||||| base: none\n=======\ntheirs\n>>>>>>> theirs (the change)\n"},
+ },
+ {
+ name: "no base and theirs absent", file: file(nil, side, nil),
+ sides: [3][]byte{nil, []byte("ours\n"), nil},
+ want: []string{"<<<<<<< ours (the branch tip)\nours\n||||||| base: none\n=======\n>>>>>>> theirs (the change): absent\n"},
+ },
} {
got := string(conflictFileFor(tc.file, tc.sides))
for _, w := range tc.want {
@@ -152,6 +166,13 @@ func writePackage(t *testing.T, dir string, c *gitalypb.LandConflict) {
require.NoError(t, os.WriteFile(filepath.Join(dir, "conflict.json"), raw, 0o644))
}
+// TestConflictPointerPrefixIsTheServers: the pointer line the CLI writes is the one the server's
+// validation refuses.
+func TestConflictPointerPrefixIsTheServers(t *testing.T) {
+ t.Parallel()
+ require.Equal(t, resolver.ConflictPointerPrefix, conflictPointerPrefix)
+}
+
// TestReadResolutionRefusesAPointerLine: a file whose binary pointer the agent left in place is
// refused, markers or not.
func TestReadResolutionRefusesAPointerLine(t *testing.T) {
diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.md
index 7609bc9..65f3dd6 100644
--- a/doc/graft/CONFIGURATION.md
+++ b/doc/graft/CONFIGURATION.md
@@ -179,6 +179,14 @@ never calls a language model: it calls the program or HTTP endpoint configured
here. The protocol, the validation rules and `graft-resolve` are described in
[RESOLVE.md](RESOLVE.md).
+A resolver configuration the server could not use is a startup error: an empty
+`command`, a `url` that is not an `http` or `https` URL with a host, a
+`token_file` that cannot be read or is empty, a bad `timeout`, a negative
+bound, or a `max_file_bytes` over the bound of a check request. A configured
+resolver is never treated as no resolver: if it cannot be built later (its
+token file removed, say), every land that needs it fails closed, with the
+cause.
+
| Key | Type | Default | Description |
|---|---|---|---|
| `enabled` | bool | `false` | Master switch. When false, `resolve_conflicts` in a land request is ignored. |
@@ -187,7 +195,8 @@ here. The protocol, the validation rules and `graft-resolve` are described in
| `token_file` | string | empty | For `url`: file with the bearer token, read at startup. |
| `timeout` | string | `"120s"` | Bound on one resolver call (Go duration). |
| `max_files` | int | `20` | Conflicted files per land. A land with more is not sent to the resolver. |
-| `max_file_bytes` | int | `1048576` (1 MiB) | Bound on every side of every file sent and on every resolved file accepted. |
+| `max_file_bytes` | int | `1048576` (1 MiB) | Bound on every side of every file sent and on every resolved file accepted. At most 12566528 (about 12 MiB): one file, its three sides and its resolution, must fit one check request. |
+| `check_concurrency` | int | `2` | Checks of agents' resolutions that run at once on this server; each holds a request of up to 64 MiB. A check waits for a free slot within its deadline (`timeout`), and fails closed when it expires. The wait is in `graft_resolver_check_slot_wait_seconds`. |
| `paths_allow` | array of strings | empty (every path) | Globs of paths that may be sent to the resolver, with the syntax of policy refs. |
| `paths_deny` | array of strings | empty | Globs of paths that are never sent. Deny wins over allow. |
| `actors` | array of strings | empty (every caller that may land) | Globs of the identities that may request resolution: agent names, or `shared` for the shared token. Without `[[graft.agent]]` no caller has an identity, so a non-empty list admits nobody (the server warns at startup). |
diff --git a/doc/graft/PLAN10.md b/doc/graft/PLAN10.md
index 13465ce..f59e470 100644
--- a/doc/graft/PLAN10.md
+++ b/doc/graft/PLAN10.md
@@ -598,6 +598,36 @@ way.
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.
+* **Review of e661706** (PM: fix before R1).
+ * N1. A resolver configuration the server could not build is a startup error (config
+ validation: empty command, URL without http(s) or host, unreadable or empty token file,
+ max_file_bytes over the check bound, negative bounds). If resolver.New still fails at run time,
+ the server keeps the error (resolverErr) and every land that needs the resolver fails closed
+ with it: no path turns a configured resolver into "no resolver".
+ * N2. The precheck runs in an object quarantine that UserLand discards. Before, merge-tree wrote
+ the merged trees and the blobs with markers into the repository's own objects, unreachable,
+ until gc, in every storage mode (off, shadow, authoritative: all keep objects in the local
+ repository; they never reached the bucket, whose packs carry only the objects that new
+ reference values reach). The lane still merges again in its own quarantine: reusing the precheck's tree
+ would mean keeping its objects; correctness first.
+ * N3. resolver.Client holds check_concurrency slots (default 2); a check takes one per request,
+ its wait counts against the check's deadline, and its expiry fails closed; the wait is the
+ histogram graft_resolver_check_slot_wait_seconds. The client's check is reachable only through
+ a slot.
+ * N4. Before any check the precheck reads the branch: a tip other than resolution.tip, or a change
+ already landed, is not checked; the lane, which reads the tip again under the lane, refuses it
+ (tip moved) or answers it (already landed).
+ * N5. A failure of the precheck is the result's failure with its cause (a git error, the slot
+ wait, a request), which the package reports, still failing closed; a precheck that was skipped,
+ or made for another tip or commit, says so.
+ * Partial 5: everything that stops the check between or during batches keeps the verdicts found
+ before, the rest not attempted, and the land gets the package. Partial 7: the CLI's base note
+ says "both sides added the file" only when both sides are there. Partial 8: tests for several
+ files in one request, a file at the bound with a 4 KiB path fitting one request (one byte more
+ refused by the configuration), and the collision test named for what it tests.
+ * The classify cache is fixed to the land's tip and paths when made; another tip or path is an
+ error. The pointer-line rule is in the server's validation (resolver.ConflictPointerPrefix), so
+ clients of the SDK get it; the CLI keeps its earlier message.
## 13. Prompt first (PM and Federico, approved)
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index da1c020..b978836 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -38,6 +38,12 @@ actors = ["builder", "planner"]
`max_files` bounds a request for an AI resolution only: the conflict package
lists every conflicting file, and the check of an agent's resolution covers
all its files.
+- `check_concurrency` (default 2): the checks of agents' resolutions that run
+ at once (see the check below).
+- A configuration the server could not use is a startup error (see
+ [CONFIGURATION](CONFIGURATION.md)). A configured resolver is never treated
+ as no resolver: if it cannot be built later, every land that needs it fails
+ closed, and the package says why.
- `paths_allow`, `paths_deny`: glob lists with the same semantics as policy
refs (`**` as a suffix matches any depth). A conflict in a denied path, or
in a path that is not allowed, is not sent to the resolver. Apart from the
@@ -87,10 +93,13 @@ A client asks for resolution per call:
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
+ the marker line of a side that is binary, deleted (absent, with a base),
+ absent (without a base) or unavailable, and on the base's marker line `none`,
+ with "both sides added the file" only when both sides are there. 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
+ that a pointer can never land in place of a binary side; the server refuses
+ such a line too, so a client of the SDK gets the same rule. `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
@@ -408,8 +417,9 @@ content) or `delete`. The server accepts it only when:
with a fresh package for the new tip);
- it resolves exactly the conflicting paths, each once;
- every file passes the acceptance rules of an answer (modes, size, conflict
- markers, no symbolic link named like a file git reads from the tree), and
- writes a mode one of the conflict's sides has;
+ markers, no `graft-conflict-pointer:` line, no symbolic link named like a
+ file git reads from the tree), and writes a mode one of the conflict's sides
+ has;
- the merge has at most 1000 conflicting files, the bound of a package (a
merge with more cannot be resolved through a land, and nothing is read or
checked);
@@ -419,20 +429,27 @@ content) or `delete`. The server accepts it only when:
a land);
- 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.
+ A check that cannot run fails the land, with its cause in the package.
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
+ be anyway). It merges the tip and the change in an object quarantine that is
+ then discarded, so it leaves no object in the repository, in every storage
+ mode. When the branch is no longer at `resolution.tip`, or the change has
+ already landed, nothing is checked: the lane refuses the resolution (tip
+ moved) or answers at once (already landed), reading the tip again itself.
+ At most `check_concurrency` checks run at once; a check waits for a slot
+ within its deadline. The whole check has one deadline, the resolver's
+ `timeout`: the wait for a slot and every check request use 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
+ `max_file_bytes` over about 12 MiB. When the check stops (a request that
+ fails, a git error while reading the next files, the deadline), 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
@@ -825,6 +842,7 @@ in blank lines only, and the accepted results that got no syntax verdict.
| resolver answer (stdout or HTTP body), and a refusal's error response | 2 × `max_file_bytes` × files in the request + 64 KiB | server |
| reason of a refusal (stderr, or `error` of the error response) | 4 KiB | server |
| check answer | 64 KiB + 8 KiB per file | server |
+| checks of agents' resolutions at once | 2 | `check_concurrency` |
| verification run | 5 min, output kept 4 KiB | `verify.timeout` |
| request body read by `graft-resolve` | 128 MiB (`protocol.DefaultMaxRequestBytes`) | `--max-request-bytes` |
| land request with an agent's resolution | 4 MiB, the gRPC message limit | server; the SDK refuses larger ones |
diff --git a/internal/gitaly/config/config_graft.go b/internal/gitaly/config/config_graft.go
index ab5740b..455fbc8 100644
--- a/internal/gitaly/config/config_graft.go
+++ b/internal/gitaly/config/config_graft.go
@@ -2,6 +2,8 @@ package config
import (
"fmt"
+ "net/url"
+ "os"
"path"
"regexp"
"strings"
@@ -317,6 +319,10 @@ type GraftResolver struct {
// Verify runs a command on the merged tree of every resolution before it
// lands (a resolver's answer or an agent's resolution). Off by default.
Verify GraftResolverVerify `json:"verify" toml:"verify,omitempty"`
+ // CheckConcurrency bounds the checks of agents' resolutions that run at once on this server:
+ // each holds a check request of up to GraftResolverMaxCheckRequestBytes. Zero means
+ // DefaultGraftResolverCheckConcurrency.
+ CheckConcurrency int `json:"check_concurrency" toml:"check_concurrency,omitempty"`
}
// GraftResolverVerify is the optional verification of a resolution: Command
@@ -407,6 +413,20 @@ const (
DefaultGraftResolverMaxFileBytes = 1 << 20
)
+// The bounds of the check of an agent's resolution (internal/graft/resolver, check.go): a check
+// request is at most GraftResolverMaxCheckRequestBytes encoded, so one file, its three sides and
+// its resolution in base64 with a path of a few KiB, fits one request when max_file_bytes is at
+// most GraftResolverMaxCheckFileBytes.
+const (
+ GraftResolverMaxCheckRequestBytes = 64 << 20
+ // GraftResolverCheckEnvelopeBytes is what a check request holds besides its files.
+ GraftResolverCheckEnvelopeBytes = 64 << 10
+ // GraftResolverCheckBatchBytes bounds the file data of one check request: base64 makes it 4/3.
+ GraftResolverCheckBatchBytes = (GraftResolverMaxCheckRequestBytes - GraftResolverCheckEnvelopeBytes) * 3 / 4
+ // GraftResolverMaxCheckFileBytes is the largest max_file_bytes.
+ GraftResolverMaxCheckFileBytes = GraftResolverCheckBatchBytes/4 - 4<<10
+)
+
// Validate reports malformed policy rules at load time: an invalid Actor or
// Refs glob, or an empty Refs entry (which would never match, so a deny rule
// written that way would silently never fire).
@@ -461,14 +481,34 @@ func (r GraftResolver) validate() error {
if (len(r.Command) == 0) == (r.URL == "") {
return fmt.Errorf("graft land resolver: exactly one of command and url must be set")
}
- if r.URL != "" && !strings.HasPrefix(r.URL, "http://") && !strings.HasPrefix(r.URL, "https://") {
- return fmt.Errorf("graft land resolver: url %q must start with http:// or https://", r.URL)
+ if len(r.Command) > 0 && r.Command[0] == "" {
+ return fmt.Errorf("graft land resolver: empty command")
+ }
+ if r.URL != "" {
+ u, err := url.Parse(r.URL)
+ if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
+ return fmt.Errorf("graft land resolver: url %q must be an http or https URL with a host", r.URL)
+ }
+ if r.TokenFile != "" {
+ raw, err := os.ReadFile(r.TokenFile)
+ if err != nil {
+ return fmt.Errorf("graft land resolver: read token file: %w", err)
+ }
+ if strings.TrimSpace(string(raw)) == "" {
+ return fmt.Errorf("graft land resolver: token file %q is empty", r.TokenFile)
+ }
+ }
}
if _, err := r.TimeoutOrDefault(); err != nil {
return err
}
- if r.MaxFiles < 0 || r.MaxFileBytes < 0 {
- return fmt.Errorf("graft land resolver: max_files and max_file_bytes must not be negative")
+ if r.MaxFiles < 0 || r.MaxFileBytes < 0 || r.CheckConcurrency < 0 {
+ return fmt.Errorf("graft land resolver: max_files, max_file_bytes and check_concurrency 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 := r.MaxFileBytesOrDefault(); mfb > GraftResolverMaxCheckFileBytes {
+ return 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, GraftResolverMaxCheckRequestBytes, GraftResolverMaxCheckFileBytes)
}
for _, list := range [][]string{r.PathsAllow, r.PathsDeny, r.Actors} {
for _, glob := range list {
@@ -520,6 +560,17 @@ func (r GraftResolver) TimeoutOrDefault() (time.Duration, error) {
return d, nil
}
+// DefaultGraftResolverCheckConcurrency is the default of GraftResolver.CheckConcurrency.
+const DefaultGraftResolverCheckConcurrency = 2
+
+// CheckConcurrencyOrDefault returns CheckConcurrency or its default.
+func (r GraftResolver) CheckConcurrencyOrDefault() int {
+ if r.CheckConcurrency > 0 {
+ return r.CheckConcurrency
+ }
+ return DefaultGraftResolverCheckConcurrency
+}
+
// MaxFilesOrDefault returns MaxFiles or its default.
func (r GraftResolver) MaxFilesOrDefault() int {
if r.MaxFiles > 0 {
diff --git a/internal/gitaly/config/config_graft_resolver_test.go b/internal/gitaly/config/config_graft_resolver_test.go
index 1e4b780..9e6c337 100644
--- a/internal/gitaly/config/config_graft_resolver_test.go
+++ b/internal/gitaly/config/config_graft_resolver_test.go
@@ -1,6 +1,8 @@
package config
import (
+ "os"
+ "path/filepath"
"strings"
"testing"
@@ -72,3 +74,62 @@ func TestGraftResolverMinifiedDefaults(t *testing.T) {
require.Equal(t, 10, m.AverageLineBytesOrDefault())
require.Equal(t, 20, m.LongestLineBytesOrDefault())
}
+
+// TestGraftResolverValidateBuildable: a resolver configuration that the server could not build is
+// a startup error, not a resolver turned off: an empty command, a URL that is no http or https URL
+// with a host, a token file that cannot be read or is empty, a negative check_concurrency, or a
+// max_file_bytes over the bound of a check request.
+func TestGraftResolverValidateBuildable(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ token := filepath.Join(dir, "token")
+ require.NoError(t, os.WriteFile(token, []byte("secret\n"), 0o600))
+ empty := filepath.Join(dir, "empty")
+ require.NoError(t, os.WriteFile(empty, []byte(" \n"), 0o600))
+
+ byURL := GraftResolver{Enabled: true, URL: "https://resolver.example:8443/resolve", TokenFile: token}
+ require.NoError(t, byURL.validate())
+
+ // max_file_bytes may be exactly the bound, not one byte more.
+ atBound := GraftResolver{Enabled: true, Command: []string{"graft-resolve"}, MaxFileBytes: GraftResolverMaxCheckFileBytes}
+ require.NoError(t, atBound.validate())
+
+ for name, tc := range map[string]struct {
+ r GraftResolver
+ want string
+ }{
+ "empty command": {GraftResolver{Command: []string{""}}, "empty command"},
+ "url without scheme": {GraftResolver{URL: "resolver.example/resolve"}, "http or https URL with a host"},
+ "url of another kind": {GraftResolver{URL: "ftp://resolver.example/"}, "http or https URL with a host"},
+ "url without host": {GraftResolver{URL: "http:///resolve"}, "http or https URL with a host"},
+ "missing token file": {GraftResolver{URL: "https://resolver.example/", TokenFile: filepath.Join(dir, "absent")}, "read token file"},
+ "empty token file": {GraftResolver{URL: "https://resolver.example/", TokenFile: empty}, "is empty"},
+ "negative check_concurrency": {
+ GraftResolver{Command: []string{"graft-resolve"}, CheckConcurrency: -1}, "must not be negative",
+ },
+ "max_file_bytes over the bound": {
+ GraftResolver{Command: []string{"graft-resolve"}, MaxFileBytes: GraftResolverMaxCheckFileBytes + 1}, "max_file_bytes",
+ },
+ } {
+ t.Run(name, func(t *testing.T) {
+ tc.r.Enabled = true
+ err := tc.r.validate()
+ require.Error(t, err)
+ require.True(t, strings.Contains(err.Error(), tc.want), "error %q lacks %q", err, tc.want)
+ })
+ }
+
+ require.Equal(t, DefaultGraftResolverCheckConcurrency, GraftResolver{}.CheckConcurrencyOrDefault())
+ require.Equal(t, 5, GraftResolver{CheckConcurrency: 5}.CheckConcurrencyOrDefault())
+}
+
+// TestGraftResolverCheckBounds: a file of GraftResolverMaxCheckFileBytes, its three sides and its
+// resolution, with a path of about 4 KiB, fits one check batch, and so one request.
+func TestGraftResolverCheckBounds(t *testing.T) {
+ t.Parallel()
+
+ const path = 4 << 10
+ require.LessOrEqual(t, int64(4*GraftResolverMaxCheckFileBytes+path), int64(GraftResolverCheckBatchBytes))
+ require.LessOrEqual(t, int64(GraftResolverCheckBatchBytes)*4/3+GraftResolverCheckEnvelopeBytes, int64(GraftResolverMaxCheckRequestBytes))
+}
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.go
index e193003..72b5c09 100644
--- a/internal/gitaly/service/operations/land.go
+++ b/internal/gitaly/service/operations/land.go
@@ -75,8 +75,17 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*g
// 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 {
+ if in.GetResolution() != nil && s.conflictResolver != nil {
+ // The precheck merges the tip and the change, which writes objects: it works in a
+ // quarantine that is discarded, so that it leaves nothing in the repository, whatever the
+ // storage mode.
+ _, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, in.GetRepository())
+ if err != nil {
+ return nil, err
+ }
+ precheck, err = s.landPrecheckResolution(ctx, in, quarantineRepo)
+ cleanup()
+ if err != nil {
return nil, err
}
}
diff --git a/internal/gitaly/service/operations/land_package_test.go b/internal/gitaly/service/operations/land_package_test.go
index 2f5e2e2..57f52bb 100644
--- a/internal/gitaly/service/operations/land_package_test.go
+++ b/internal/gitaly/service/operations/land_package_test.go
@@ -3,6 +3,7 @@ package operations
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -19,6 +20,7 @@ import (
"github.com/feder-cr/git-ai/internal/git/gittest"
"github.com/feder-cr/git-ai/internal/gitaly/config"
"github.com/feder-cr/git-ai/internal/graft/resolver"
+ "github.com/feder-cr/git-ai/internal/helper/text"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
@@ -432,14 +434,173 @@ func TestUserLand_reland(t *testing.T) {
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.Run("without a check for its tip and commit the land fails closed, saying why", 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)
+ for name, tc := range map[string]struct {
+ precheck *landPrecheck
+ want string
+ }{
+ "no check": {nil, "it was not checked before the land"},
+ "a skipped check": {&landPrecheck{skipped: "the change had already landed"}, "since the change had already landed"},
+ "a check of another": {&landPrecheck{tip: fx.base, commit: fx.commit}, fmt.Sprintf("it was checked against tip %s and commit %s, not tip %s", fx.base, fx.commit, fx.tip)},
+ } {
+ _, err := s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer(), tc.precheck)
+ pkg := builtUnresolved(t, fx, err)
+ require.Contains(t, pkg.GetReason(), "the resolution could not be checked", name)
+ require.Contains(t, pkg.GetReason(), tc.want, name)
+ }
+ })
+
+ t.Run("a configured resolver that cannot be built fails closed with its cause", func(t *testing.T) {
+ t.Parallel()
+
+ _, argv := perCallChecker(t)
+ fx, s, req := batchedReland(t, argv)
+ s.conflictResolver, s.resolverErr = nil, errors.New("token file is gone")
+
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err)
+ require.Nil(t, pre, "no resolver: no check")
+ _, 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(), "it was not checked against tip")
+ require.Contains(t, pkg.GetReason(), "the configured resolver cannot be built: token file is gone")
+
+ // A land that asks the server to resolve is refused with the same cause.
+ _, err = s.resolveLandConflicts(fx.ctx, fx.request("Land"), fx.repo, fx.tip, fx.commit, fx.committer())
+ pkg = builtUnresolved(t, fx, err)
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_NOT_ATTEMPTED, packageFile(t, pkg, "a").GetOutcome())
+ require.Contains(t, pkg.GetReason(), "conflict resolution is configured, but its resolver cannot be built: token file is gone")
+ })
+
+ t.Run("the wait for a check slot counts against the deadline of the check", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", true, ""))
+ fx, s, req := batchedReland(t, argv)
+ s.resolveCfg.Timeout = "300ms"
+ s.resolveCfg.CheckConcurrency = 1
+ client, err := resolver.New(s.resolveCfg)
+ require.NoError(t, err)
+ s.conflictResolver = client
+
+ // Another check holds the only slot for longer than the deadline.
+ held, err := client.AcquireCheckSlot(fx.ctx)
+ require.NoError(t, err)
+ 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 wait stops at the deadline")
+ held.Release()
+ require.Contains(t, pre.result.failure, "waiting for the resolver")
+ require.Equal(t, 0, resolverCalls(t, dir))
+
+ _, 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.Contains(t, pkg.GetReason(), "context deadline exceeded")
+ })
+
+ t.Run("a git error between batches keeps the earlier verdicts and fails closed", func(t *testing.T) {
+ t.Parallel()
+
+ // a and b have sides of their own. While the first batch (a) is checked, the blob of b's
+ // theirs side disappears: reading the second batch fails, after a got its verdict.
+ fx := newResolveFixtureWith(t, config.GraftResolver{},
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "base\n"}, {Mode: "100644", Path: "b", Content: "base-b\n"}},
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "ours\n"}, {Mode: "100644", Path: "b", Content: "ours-b\n"}},
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "theirs\n"}, {Mode: "100644", Path: "b", Content: "theirs-b\n"}},
+ )
+ theirsB := text.ChompBytes(gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "rev-parse", fx.commit.String()+":b"))
+ loose := filepath.Join(fx.repoPath, "objects", theirsB[:2], theirsB[2:])
+ require.FileExists(t, loose)
+
+ dir, argv := resolverScript(t, fmt.Sprintf(`[ "$N" = 1 ] && rm -f '%s'; [ -s "$DIR/answer-$N.json" ] || exit 1; cp "$DIR/answer-$N.json" "$DIR/answer.json"`, loose))
+ require.NoError(t, os.WriteFile(filepath.Join(dir, "answer-1.json"), []byte(checkAnswer("graft-resolve", "a", false, "a is wrong")), 0o600))
+ require.NoError(t, os.WriteFile(filepath.Join(dir, "answer-2.json"), []byte(checkAnswer("graft-resolve", "b", true, "")), 0o600))
+ 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")
+
+ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)
+ require.NoError(t, err)
+ require.Equal(t, 1, resolverCalls(t, dir), "the second batch was never sent")
+ require.Contains(t, pre.result.failure, "checking: ")
+ require.Contains(t, pre.result.failure, `of "b": invalid object`)
+ require.Equal(t, "a is wrong", pre.result.verdicts["a"].rejected, "the first batch's verdict is kept")
+ _, hasB := pre.result.verdicts["b"]
+ require.False(t, hasB)
+ })
+
+ t.Run("the check of a moved tip is skipped: the lane refuses it at once", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := perCallChecker(t)
+ fx := newResolveFixture(t, enabledResolver(argv))
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ req.Resolution.Tip = fx.base.String()
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ require.Contains(t, pkg.GetReason(), "tip moved")
+ require.Equal(t, 0, resolverCalls(t, dir), "nothing was checked")
+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
+ })
+
+ t.Run("the check of a change that already landed is skipped: the lane answers it at once", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := perCallChecker(t)
+ fx := newResolveFixture(t, enabledResolver(argv))
+ landed := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.tip, fx.commit),
+ gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "merged\n"},
+ gittest.TreeEntry{Mode: "100644", Path: "b", Content: "merged\n"},
+ ))
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ req.Resolution.Tip = landed.String()
+
+ resp, err := fx.client.UserLand(fx.ctx, req)
+ require.NoError(t, err)
+ require.True(t, resp.GetAlreadyLanded())
+ require.Equal(t, 0, resolverCalls(t, dir), "nothing was checked")
+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, landed)
+ })
+
+ t.Run("the check leaves no object in the repository", func(t *testing.T) {
+ t.Parallel()
+
+ // The check merges the tip and the change, which writes trees and blobs; it runs in a
+ // quarantine that is discarded, so a land that the check refuses leaves the objects of the
+ // repository as they were.
+ dir, argv := resolverScript(t, "")
+ writeRawAnswer(t, dir, resolver.CheckResponse{
+ Version: resolver.ProtocolVersion, Operation: resolver.OperationCheck, Resolver: "graft-resolve",
+ Files: []resolver.CheckFile{{Path: "a", OK: false, Reason: "a is wrong", Syntax: resolver.SyntaxNone}, {Path: "b", OK: true, Syntax: resolver.SyntaxNone}},
+ })
+ fx := newResolveFixture(t, enabledResolver(argv))
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ before := gittest.ListObjects(t, fx.cfg, fx.repoPath)
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_REJECTED, packageFile(t, pkg, "a").GetOutcome())
+ require.Equal(t, 1, resolverCalls(t, dir))
+ require.ElementsMatch(t, before, gittest.ListObjects(t, fx.cfg, fx.repoPath))
})
t.Run("the check runs outside the lane: another land of the branch is not held", func(t *testing.T) {
@@ -575,6 +736,62 @@ func TestUserLand_reland(t *testing.T) {
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
})
+ t.Run("a pointer line to a binary side is rejected", func(t *testing.T) {
+ t.Parallel()
+
+ // An SDK client gets the rule of graft land --resolved: the server refuses the pointer
+ // line that stands for a binary side in the file to edit.
+ fx := newResolveFixture(t, config.GraftResolver{})
+ req := fx.request("Land")
+ req.Resolution = resolutionOf(fx, "agent-a\n", "x\n"+resolver.ConflictPointerPrefix+"the binary ours side is sides/ours/b\n")
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ b := packageFile(t, pkg, "b")
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_REJECTED, b.GetOutcome())
+ require.Contains(t, b.GetReason(), "the pointer to a binary side")
+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
+ })
+
+ t.Run("several files go in one check request within its bound", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ 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},
+ {Path: "e", OK: true, Syntax: resolver.SyntaxNone},
+ },
+ })
+ fx := newResolveFixtureWith(t, enabledResolver(argv),
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "base\n"}, {Mode: "100644", Path: "b", Content: "base-b\n"}, {Mode: "100644", Path: "e", Content: "base-e\n"}},
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "ours\n"}, {Mode: "100644", Path: "b", Content: "ours-b\n"}, {Mode: "100644", Path: "e", Content: "ours-e\n"}},
+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "theirs\n"}, {Mode: "100644", Path: "b", Content: "theirs-b\n"}, {Mode: "100644", Path: "e", Content: "theirs-e\n"}},
+ )
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+ req.Resolution.Files = append(req.Resolution.Files, &gitalypb.LandResolvedFile{
+ Path: []byte("e"), Action: gitalypb.LandResolvedFile_ACTION_WRITE, Mode: "100644", Content: []byte("agent-e\n"),
+ })
+
+ resp, err := fx.client.UserLand(fx.ctx, req)
+ require.NoError(t, err)
+ require.ElementsMatch(t, [][]byte{[]byte("a"), []byte("b"), []byte("e")}, resp.GetResolvedPaths())
+ require.Equal(t, 1, resolverCalls(t, dir), "one request for the three files")
+ check := resolverRequest(t, dir, 1)
+ require.Len(t, check.Files, 3)
+ require.Len(t, check.Resolution, 3)
+ for i, p := range []string{"a", "b", "e"} {
+ require.Equal(t, p, check.Files[i].Path)
+ require.Equal(t, p, check.Resolution[i].Path)
+ require.Equal(t, "agent-"+p+"\n", string(check.Resolution[i].Content))
+ }
+ })
+
t.Run("conflict markers are rejected", func(t *testing.T) {
t.Parallel()
diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.go
index fe3bcad..12c1af8 100644
--- a/internal/gitaly/service/operations/land_reland.go
+++ b/internal/gitaly/service/operations/land_reland.go
@@ -9,6 +9,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/localrepo"
@@ -111,10 +113,12 @@ func (s *Server) landReviewResolution(ctx context.Context, in *gitalypb.UserLand
}
// 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.
+// (landPrecheckResolution): for the tip and the commit it was run against, the result; or, when
+// no check was run because the lane refuses or answers the land without one, why (skipped).
type landPrecheck struct {
tip, commit git.ObjectID
result landCheckResult
+ skipped string
}
// landPrecheckResolution runs the resolver's checks of the request's resolution before the land
@@ -125,8 +129,10 @@ type landPrecheck struct {
// 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).
+// The check runs in the quarantine repo, which the caller discards: the merge it computes leaves no
+// object behind. A resolution for a tip the branch has left, or for a change that already landed,
+// is not checked (skipped): the lane refuses or answers it. It returns nil when no resolver is
+// configured. A failure to check is the result's failure, with its cause, which the lane reports.
func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLandRequest, repo *localrepo.Repo) (*landPrecheck, error) {
if s.conflictResolver == nil {
return nil, nil
@@ -137,16 +143,40 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa
}
tip, err := objectHash.FromHex(in.GetResolution().GetTip())
if err != nil {
- return nil, nil
+ return &landPrecheck{skipped: fmt.Sprintf("its tip %q is no object ID", in.GetResolution().GetTip())}, nil
}
commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}"))
if err != nil {
- return nil, nil
+ return &landPrecheck{skipped: fmt.Sprintf("commit_id %q does not resolve to a commit", in.GetCommitId())}, nil
}
+
+ // A resolution for a tip the branch has left, or for a change that already landed, needs no
+ // check: the lane refuses or answers it at once, as it reads the tip again under the lane.
+ branch, err := repo.GetReference(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())))
+ switch {
+ case errors.Is(err, git.ErrReferenceNotFound):
+ return &landPrecheck{skipped: fmt.Sprintf("branch %q does not exist", string(in.GetBranch()))}, nil
+ case err != nil:
+ return &landPrecheck{tip: tip, commit: commitID, result: landCheckResult{failure: fmt.Sprintf("reading the branch: %v", err), failureErr: err}}, nil
+ case git.ObjectID(branch.Target) != tip:
+ return &landPrecheck{skipped: fmt.Sprintf("the branch was at %s, not at the tip of the resolution", branch.Target)}, nil
+ }
+ if landed, err := repo.IsAncestor(ctx, commitID.Revision(), tip.Revision()); err != nil {
+ return &landPrecheck{tip: tip, commit: commitID, result: landCheckResult{failure: fmt.Sprintf("checking whether the change already landed: %v", err), failureErr: err}}, nil
+ } else if landed {
+ return &landPrecheck{skipped: "the change had already landed"}, 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
+ // A refusal of the resolution (its package, or an invalid argument or failed precondition)
+ // is repeated by the lane before any check. Any other error is a failure to check it.
+ var unresolved *landUnresolvedError
+ code := status.Code(err)
+ if err == nil || errors.As(err, &unresolved) || code == codes.InvalidArgument || code == codes.FailedPrecondition {
+ return &landPrecheck{skipped: "the land refuses the resolution before checking it"}, nil
+ }
+ return &landPrecheck{tip: tip, commit: commitID, result: landCheckResult{failure: fmt.Sprintf("listing the conflicts: %v", err), failureErr: err}}, nil
}
timeout, err := s.resolveCfg.TimeoutOrDefault()
@@ -155,10 +185,7 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa
}
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
- }
+ result := s.landCheckResolution(ctx, in, repo, review.listing, review.actor, review.resolved)
return &landPrecheck{tip: tip, commit: commitID, result: result}, nil
}
@@ -187,9 +214,16 @@ func (s *Server) landWithResolution(
l, rejected := review.listing, review.rejected
checkedBy, syntaxUnchecked := "", 0
- if !rejected && s.conflictResolver != nil {
- 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)
+ if !rejected && (s.conflictResolver != nil || s.resolverErr != nil) {
+ switch {
+ case s.conflictResolver == nil:
+ return landResolution{}, l.notAttempted(fmt.Sprintf("the resolution could not be checked: the configured resolver cannot be built: %v", s.resolverErr), s.resolverErr)
+ case precheck == nil:
+ return landResolution{}, l.notAttempted("the resolution could not be checked: it was not checked before the land", nil)
+ case precheck.skipped != "":
+ return landResolution{}, l.notAttempted(fmt.Sprintf("the resolution could not be checked: it was not checked before the land, since %s", precheck.skipped), nil)
+ case precheck.tip != tip || precheck.commit != commitID:
+ return landResolution{}, l.notAttempted(fmt.Sprintf("the resolution could not be checked: it was checked against tip %s and commit %s, not tip %s and commit %s", precheck.tip, precheck.commit, tip, commitID), nil)
}
res := precheck.result
for _, f := range l.files {
@@ -315,11 +349,13 @@ func (s *Server) landCheckBatchBytes() int64 {
// landCheckResolution runs the resolver's checks on the files of the resolution that may be sent
// 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.
+// file data each: for each, a check slot of the resolver is taken first (resolver.Client bounds the
+// checks that run at once, and waiting for a slot counts against the deadline of ctx), then the
+// sides of the batch are read, checked and released, so that no more than one bounded request is
+// held at a time. 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. Anything that stops the checks (a request that fails, a git error, the deadline) is the
+// result's failure, with the verdicts found before: the check fails closed.
func (s *Server) landCheckResolution(
ctx context.Context,
in *gitalypb.UserLandRequest,
@@ -327,68 +363,42 @@ func (s *Server) landCheckResolution(
l landListing,
actor string,
resolved map[string]resolver.ResponseFile,
-) (landCheckResult, error) {
+) landCheckResult {
result := landCheckResult{verdicts: map[string]landCheckVerdict{}}
+ fail := func(what string, err error) landCheckResult {
+ result.failure, result.failureErr = fmt.Sprintf("%s: %v", what, err), err
+ return result
+ }
batches, err := landCheckBatches(ctx, repo, l.files, resolved, s.landCheckBatchBytes())
if err != nil {
- return result, err
+ return fail("sizing the check requests", err)
}
- cache := newLandClassifyCache(l.files)
+ cache := newLandClassifyCache(l.files, l.tip)
var unchecked []string
for _, batch := range batches {
- 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(),
- Actor: actor,
- }
- if l.hasBase {
- req.MergeBase = l.base.String()
- }
- var checked []*landFileState
- for _, f := range batch {
- if !f.sent() {
- // The file may not go to the resolver: it gets the server's rules only.
- unchecked = append(unchecked, fmt.Sprintf("%s (%s)", f.conflict.path, f.reason))
- f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_UNSPECIFIED, ""
- continue
- }
- req.Files = append(req.Files, landRequestFile(f))
- req.Resolution = append(req.Resolution, resolved[f.conflict.path])
- checked = append(checked, f)
+ slot, err := s.conflictResolver.AcquireCheckSlot(ctx)
+ if err != nil {
+ return fail("waiting for the resolver", err)
}
+ checked, resp, err := s.landCheckBatch(ctx, in, repo, l, actor, resolved, batch, cache, slot, &unchecked)
+ slot.Release()
// The sides of the batch are released once it is checked: the next batch reads its own, and
// a package reads again, within its own bounds, what its hunks need.
- release := func() {
- for _, f := range batch {
- f.sides, f.read = [3][]byte{}, false
- }
- }
- if len(checked) == 0 {
- release()
- continue
+ for _, f := range batch {
+ f.sides, f.read = [3][]byte{}, false
}
-
- resp, err := s.conflictResolver.Check(ctx, req)
- release()
switch {
case err != nil:
- result.failure, result.failureErr = err.Error(), err
+ return fail("checking", err)
+ case resp == nil:
+ continue
case !landResolverNameValid(resp.Resolver):
result.failure = fmt.Sprintf("the check names an invalid resolver %q", resp.Resolver)
+ return result
case result.checkedBy != "" && resp.Resolver != result.checkedBy:
result.failure = fmt.Sprintf("its checks named the resolvers %q and %q", result.checkedBy, resp.Resolver)
- }
- if result.failure != "" {
- return result, nil
+ return result
}
result.checkedBy = resp.Resolver
for i, v := range resp.Files {
@@ -401,7 +411,7 @@ func (s *Server) landCheckResolution(
}
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 result, nil
+ return result
}
s.logger.WithFields(log.Fields{
"checked_by": result.checkedBy,
@@ -409,7 +419,57 @@ func (s *Server) landCheckResolution(
"check_requests": len(batches),
"not_checked": unchecked,
}).InfoContext(ctx, "graft: agent's resolution checked")
- return result, nil
+ return result
+}
+
+// landCheckBatch reads and checks one batch of files with a check slot taken. It returns the files
+// sent, in the order of the verdicts, and the check's answer, nil when no file of the batch may be
+// sent; the files that may not are added to unchecked.
+func (s *Server) landCheckBatch(
+ ctx context.Context,
+ in *gitalypb.UserLandRequest,
+ repo *localrepo.Repo,
+ l landListing,
+ actor string,
+ resolved map[string]resolver.ResponseFile,
+ batch []*landFileState,
+ cache *landClassifyCache,
+ slot *resolver.CheckSlot,
+ unchecked *[]string,
+) ([]*landFileState, *resolver.CheckResponse, error) {
+ if err := s.landClassify(ctx, repo, batch, l.base, l.tip, l.commitID, cache); err != nil {
+ return nil, nil, 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(),
+ Actor: actor,
+ }
+ if l.hasBase {
+ req.MergeBase = l.base.String()
+ }
+ var checked []*landFileState
+ for _, f := range batch {
+ if !f.sent() {
+ // The file may not go to the resolver: it gets the server's rules only.
+ *unchecked = append(*unchecked, fmt.Sprintf("%s (%s)", f.conflict.path, f.reason))
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_UNSPECIFIED, ""
+ continue
+ }
+ req.Files = append(req.Files, landRequestFile(f))
+ req.Resolution = append(req.Resolution, resolved[f.conflict.path])
+ checked = append(checked, f)
+ }
+ if len(checked) == 0 {
+ return nil, nil, nil
+ }
+ resp, err := slot.Check(ctx, req)
+ return checked, resp, err
}
// landCheckBatches splits the files into batches of at most budget bytes of file data: the sizes
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index 931d557..18cf630 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -109,6 +109,8 @@ func (s *Server) resolveLandConflicts(
switch {
case len(files) == 0:
return landResolution{}, notAttempted(landNoConflictingFiles)
+ case s.conflictResolver == nil && s.resolverErr != nil:
+ return landResolution{}, notAttempted(fmt.Sprintf("conflict resolution is configured, but its resolver cannot be built: %v", s.resolverErr))
case s.conflictResolver == nil:
return landResolution{}, notAttempted("conflict resolution is not enabled on this server")
case !s.conflictResolver.ActorAllowed(actor):
@@ -119,7 +121,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, newLandClassifyCache(files)); err != nil {
+ if err := s.landClassify(ctx, repo, files, base, tip, commitID, newLandClassifyCache(files, tip)); err != nil {
return landResolution{}, err
}
@@ -477,6 +479,9 @@ func (s *Server) landClassify(
base, tip, commitID git.ObjectID,
cache *landClassifyCache,
) error {
+ if cache.tip != tip {
+ return fmt.Errorf("classifying the conflicts: the cache is for tip %s, not %s", cache.tip, tip)
+ }
rules := s.conflictResolver.SendRules()
listings := cache.listings
for _, f := range files {
@@ -557,7 +562,7 @@ func (s *Server) landClassify(
candidates = append(candidates, f.conflict.path)
}
}
- generated, err := cache.generatedPaths(ctx, repo, tip, candidates)
+ generated, err := cache.generatedPaths(ctx, repo, candidates)
if err != nil {
return err
}
@@ -572,32 +577,42 @@ 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.
+// landClassifyCache holds what landClassify reads from the trees, for every call of one land onto
+// one tip: the listings of the trees (by revision) and the linguist-generated attribute of the
+// paths of the land, read at the tip once for all of them. Both the tip and the paths are fixed
+// when it is made: a call for another tip, or for a path that is not one of the land, is an error.
type landClassifyCache struct {
listings map[git.ObjectID]map[git.ObjectID][]string
+ tip git.ObjectID
paths []string
+ known map[string]bool
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{}}
+// newLandClassifyCache returns the cache of the land of files onto tip.
+func newLandClassifyCache(files []*landFileState, tip git.ObjectID) *landClassifyCache {
+ c := &landClassifyCache{listings: map[git.ObjectID]map[git.ObjectID][]string{}, tip: tip, known: map[string]bool{}}
for _, f := range files {
c.paths = append(c.paths, f.conflict.path)
+ c.known[f.conflict.path] = true
}
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) {
+// generatedPaths returns the linguist-generated attribute at the tip of the land of paths, every
+// one a path of the land. The attributes of all the paths of the land are read on first use, with
+// one git check-attr.
+func (c *landClassifyCache) generatedPaths(ctx context.Context, repo *localrepo.Repo, paths []string) (map[string]bool, error) {
+ for _, p := range paths {
+ if !c.known[p] {
+ return nil, fmt.Errorf("classifying the conflicts: %q is not a conflicting path of the land", p)
+ }
+ }
if len(paths) == 0 {
return map[string]bool{}, nil
}
if c.generated == nil {
- generated, err := landGeneratedPaths(ctx, repo, rev, c.paths)
+ generated, err := landGeneratedPaths(ctx, repo, c.tip, c.paths)
if err != nil {
return nil, err
}
diff --git a/internal/gitaly/service/operations/land_resolve_paths_test.go b/internal/gitaly/service/operations/land_resolve_paths_test.go
index b5f456e..ee2e1f2 100644
--- a/internal/gitaly/service/operations/land_resolve_paths_test.go
+++ b/internal/gitaly/service/operations/land_resolve_paths_test.go
@@ -1,6 +1,7 @@
package operations
import (
+ "fmt"
"strings"
"testing"
@@ -95,11 +96,34 @@ 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, newLandClassifyCache([]*landFileState{f})))
+ require.NoError(t, s.landClassify(fx.ctx, fx.repo, []*landFileState{f}, fx.base, fx.tip, fx.commit, newLandClassifyCache([]*landFileState{f}, fx.tip)))
require.Equal(t, gitalypb.LandConflictFile_OUTCOME_NOT_SENT, f.outcome)
require.Equal(t, landNotSentNoSide, f.reason)
}
+// TestLandClassifyCache_isForOneLand: the cache is made for the paths of one land onto one tip.
+// landClassify with another tip, and the attributes of a path that is not one of the land, are
+// errors, not answers for the wrong revision or path.
+func TestLandClassifyCache_isForOneLand(t *testing.T) {
+ t.Parallel()
+
+ _, argv := resolverScript(t, "")
+ rcfg := enabledResolver(argv)
+ fx := newResolveFixture(t, rcfg)
+ s := fx.server(t, rcfg)
+
+ f := &landFileState{conflict: landFileConflict{path: "a"}}
+ cache := newLandClassifyCache([]*landFileState{f}, fx.tip)
+ err := s.landClassify(fx.ctx, fx.repo, []*landFileState{f}, fx.base, fx.commit, fx.commit, cache)
+ require.ErrorContains(t, err, fmt.Sprintf("the cache is for tip %s, not %s", fx.tip, fx.commit))
+
+ _, err = cache.generatedPaths(fx.ctx, fx.repo, []string{"a", "c"})
+ require.ErrorContains(t, err, `"c" is not a conflicting path of the land`)
+ generated, err := cache.generatedPaths(fx.ctx, fx.repo, []string{"a"})
+ require.NoError(t, err)
+ require.False(t, generated["a"])
+}
+
// TestUserLand_pathStartingWithDashIsResolved covers a conflicting path that starts with a dash.
// Such a path must reach the resolver as a path, not as an option of ls-tree.
func TestUserLand_pathStartingWithDashIsResolved(t *testing.T) {
diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.go
index aedae40..f0228b4 100644
--- a/internal/gitaly/service/operations/server.go
+++ b/internal/gitaly/service/operations/server.go
@@ -38,8 +38,11 @@ type Server struct {
// checkBatchBytes, when set, replaces the bound of the file data of one check request
// (landCheckBatchBytes); only tests set it.
checkBatchBytes int64
- // conflictResolver answers the resolve requests of UserLand. Nil when resolution is disabled.
+ // conflictResolver answers the resolve requests of UserLand. Nil when resolution is disabled,
+ // or when the configured resolver cannot be built: then resolverErr says why, and every land
+ // that needs the resolver fails closed with it (resolveLandConflicts, landWithResolution).
conflictResolver *resolver.Client
+ resolverErr error
// landLanes batches the UserLand requests of a branch. Nil disables batching, and every
// request then lands on its own. The lanes are shared by all servers of the process.
landLanes *landBatchLanes
@@ -55,11 +58,12 @@ type Server struct {
func NewServer(deps *service.Dependencies) *Server {
resolveCfg := deps.GetCfg().Graft.Land.Resolver
- conflictResolver, err := resolver.New(resolveCfg)
- if err != nil {
- // An invalid resolver configuration disables resolution rather than the server. Landings
- // that ask for resolution then fail with the merge conflict, as without resolution.
- deps.GetLogger().WithError(err).Error("graft: conflict resolution of land is disabled")
+ // The configuration is validated at startup (config.GraftResolver.validate), so New fails
+ // only when something changed since (a token file removed, say). A configured resolver is then
+ // never treated as no resolver: resolverErr makes every land that needs it fail closed.
+ conflictResolver, resolverErr := resolver.New(resolveCfg)
+ if resolverErr != nil {
+ deps.GetLogger().WithError(resolverErr).Error("graft: the conflict resolver of land cannot be built: lands that need it fail")
conflictResolver = nil
}
if conflictResolver != nil && resolverActorsUnmatchable(deps.GetCfg().Graft) {
@@ -80,6 +84,7 @@ func NewServer(deps *service.Dependencies) *Server {
localRepoFactory: deps.GetRepositoryFactory(),
resolveCfg: resolveCfg,
conflictResolver: conflictResolver,
+ resolverErr: resolverErr,
shutdownCtx: deps.GetShutdownContext(),
storageSync: deps.GetGraftStorageSyncer(),
}
diff --git a/internal/graft/resolver/check.go b/internal/graft/resolver/check.go
index 255a866..96f0d54 100644
--- a/internal/graft/resolver/check.go
+++ b/internal/graft/resolver/check.go
@@ -5,22 +5,28 @@ import (
"encoding/json"
"errors"
"fmt"
+ "time"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
+
+ "github.com/feder-cr/git-ai/internal/gitaly/config"
)
// MaxCheckRequestBytes bounds the encoded check request. graft-resolve reads 128 MiB by default
// (protocol.DefaultMaxRequestBytes); the server splits an agent's resolution into check requests
// within this bound.
-const MaxCheckRequestBytes = 64 << 20
+const MaxCheckRequestBytes = config.GraftResolverMaxCheckRequestBytes
// 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
+// request's envelope takes at most config.GraftResolverCheckEnvelopeBytes, so the request stays
+// within MaxCheckRequestBytes.
+const CheckBatchBytes = config.GraftResolverCheckBatchBytes
// 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
+// and its resolution, must fit one check request. The configuration refuses a larger one.
+const MaxCheckFileBytes = config.GraftResolverMaxCheckFileBytes
// 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
@@ -33,12 +39,60 @@ func CheckFileOverheadBytes(path string) int64 {
// 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")
-// Check asks the resolver to check an agent's resolution of the request's files with the checks
+// checkSlotWait is the time the checks of agents' resolutions waited for a check slot.
+var checkSlotWait = promauto.NewHistogram(prometheus.HistogramOpts{
+ Name: "graft_resolver_check_slot_wait_seconds",
+ Help: "Time a check of an agent's resolution waited for one of the resolver's check slots.",
+ Buckets: prometheus.ExponentialBuckets(0.01, 4, 8),
+})
+
+// CheckSlot is the right to send one check request at a time: the client runs at most
+// check_concurrency checks at once (AcquireCheckSlot). Release it when the request is done.
+type CheckSlot struct {
+ c *Client
+ released bool
+}
+
+// AcquireCheckSlot waits for a check slot. The wait counts against the deadline of ctx: when ctx
+// ends first, it fails, and the check fails closed.
+func (c *Client) AcquireCheckSlot(ctx context.Context) (*CheckSlot, error) {
+ if c == nil {
+ return nil, ErrDisabled
+ }
+ start := time.Now()
+ select {
+ case c.checkSlots <- struct{}{}:
+ checkSlotWait.Observe(time.Since(start).Seconds())
+ return &CheckSlot{c: c}, nil
+ case <-ctx.Done():
+ checkSlotWait.Observe(time.Since(start).Seconds())
+ return nil, fmt.Errorf("waiting for a check slot of the resolver: %w", ctx.Err())
+ }
+}
+
+// Release gives the slot back; a second call does nothing.
+func (s *CheckSlot) Release() {
+ if s == nil || s.released {
+ return
+ }
+ s.released = true
+ <-s.c.checkSlots
+}
+
+// Check sends one check request with the slot (Client.check).
+func (s *CheckSlot) Check(ctx context.Context, req Request) (*CheckResponse, error) {
+ if s == nil || s.released {
+ return nil, errors.New("check without a check slot")
+ }
+ return s.c.check(ctx, req)
+}
+
+// check asks the resolver to check an agent's resolution of the request's files with the checks
// of its own answers (OperationCheck). It returns the response, with one verdict per file of
// req.Resolution in its order. It fails closed: an error, a refusal, or an answer that is not a
// check response with one verdict per file in order is an error, and the caller must not land the
// resolution.
-func (c *Client) Check(ctx context.Context, req Request) (*CheckResponse, error) {
+func (c *Client) check(ctx context.Context, req Request) (*CheckResponse, error) {
if c == nil {
return nil, ErrDisabled
}
diff --git a/internal/graft/resolver/client.go b/internal/graft/resolver/client.go
index bc08727..f8ce0a6 100644
--- a/internal/graft/resolver/client.go
+++ b/internal/graft/resolver/client.go
@@ -97,6 +97,8 @@ type Client struct {
pathsDeny []string
actors []string
httpClient *http.Client
+ // checkSlots bounds the checks of agents' resolutions that run at once (CheckSlot).
+ checkSlots chan struct{}
// sendRules decide the files that are never sent (SendRules).
sendRules *SendRules
}
@@ -127,8 +129,8 @@ func New(cfg config.GraftResolver) (*Client, error) {
return nil, err
}
- if cfg.MaxFiles < 0 || cfg.MaxFileBytes < 0 {
- return nil, errors.New("graft land resolver: max_files and max_file_bytes must not be negative")
+ if cfg.MaxFiles < 0 || cfg.MaxFileBytes < 0 || cfg.CheckConcurrency < 0 {
+ return nil, errors.New("graft land resolver: max_files, max_file_bytes and check_concurrency 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.
@@ -149,6 +151,7 @@ func New(cfg config.GraftResolver) (*Client, error) {
pathsDeny: slices.Clone(cfg.PathsDeny),
actors: slices.Clone(cfg.Actors),
sendRules: rules,
+ checkSlots: make(chan struct{}, cfg.CheckConcurrencyOrDefault()),
}
if cfg.URL != "" {
@@ -310,4 +313,4 @@ func (c *Client) outputLimit(n int) int64 {
}
// maxEnvelopeBytes is the fixed allowance on top of the per-file output bound.
-const maxEnvelopeBytes = 64 << 10
+const maxEnvelopeBytes = config.GraftResolverCheckEnvelopeBytes
diff --git a/internal/graft/resolver/client_test.go b/internal/graft/resolver/client_test.go
index d34376c..41dd176 100644
--- a/internal/graft/resolver/client_test.go
+++ b/internal/graft/resolver/client_test.go
@@ -426,7 +426,7 @@ func TestCheck_runsForEveryActorAndFileCount(t *testing.T) {
cfg.Actors = []string{"someone-else"}
client := newClient(t, cfg)
- resp, err := client.Check(testhelper.Context(t), req)
+ resp, err := checkWithSlot(t, client, req)
require.NoError(t, err)
require.Len(t, resp.Files, 2)
@@ -455,11 +455,45 @@ func TestCheck_refusesARequestOverItsBound(t *testing.T) {
side.Content = big
}
req.Resolution = validResponse("merged\n").Files
- _, err := client.Check(testhelper.Context(t), req)
+ _, err := checkWithSlot(t, client, req)
require.ErrorContains(t, err, "is over the bound")
require.NoFileExists(t, marker)
}
+// TestCheck_aFileAtTheBoundFitsOneRequest: with max_file_bytes at MaxCheckFileBytes, the largest
+// the configuration accepts (one byte more is refused there: TestGraftResolverValidateBuildable), a file whose three sides and
+// resolution are all that large, with a path of 4 KiB that JSON escapes at six bytes a byte, fits
+// one check request.
+func TestCheck_aFileAtTheBoundFitsOneRequest(t *testing.T) {
+ requireUnix(t)
+
+ path := strings.Repeat("<", 4<<10)
+ big := make([]byte, MaxCheckFileBytes)
+ for i := range big {
+ big[i] = 'x'
+ }
+ req := sampleRequest()
+ req.Files = []File{{
+ Path: path, Kind: KindContent,
+ Base: &Side{OID: "b1", Mode: ModeRegular, Content: big},
+ Ours: &Side{OID: "o1", Mode: ModeRegular, Content: big},
+ Theirs: &Side{OID: "t1", Mode: ModeRegular, Content: big},
+ }}
+ req.Resolution = []ResponseFile{{Path: path, Action: ActionWrite, Mode: ModeRegular, Content: big}}
+
+ answer, err := json.Marshal(CheckResponse{
+ Version: ProtocolVersion, Operation: OperationCheck, Resolver: "graft-resolve",
+ Files: []CheckFile{{Path: path, OK: true, Syntax: SyntaxNone}},
+ })
+ require.NoError(t, err)
+ cfg := commandConfig(cannedCommand(t, answer))
+ cfg.MaxFileBytes = MaxCheckFileBytes
+
+ resp, err := checkWithSlot(t, newClient(t, cfg), req)
+ require.NoError(t, err)
+ require.Len(t, resp.Files, 1)
+}
+
func TestResolve_RequestChecksDoNotRunTheResolver(t *testing.T) {
requireUnix(t)
@@ -628,3 +662,67 @@ func TestValidOutcomes(t *testing.T) {
require.Nil(t, validOutcomes(req, files), name)
}
}
+
+// checkWithSlot sends one check request with a check slot, as the server does.
+func checkWithSlot(t *testing.T, client *Client, req Request) (*CheckResponse, error) {
+ t.Helper()
+ slot, err := client.AcquireCheckSlot(testhelper.Context(t))
+ require.NoError(t, err)
+ defer slot.Release()
+ return slot.Check(testhelper.Context(t), req)
+}
+
+// TestAcquireCheckSlot_boundsTheChecksAtOnce: at most check_concurrency slots are held at once; a
+// wait for one counts against the caller's deadline and fails when it ends; a released slot frees
+// its place once, and cannot check any more.
+func TestAcquireCheckSlot_boundsTheChecksAtOnce(t *testing.T) {
+ cfg := commandConfig([]string{"/bin/true"})
+ cfg.CheckConcurrency = 2
+ client := newClient(t, cfg)
+ ctx := testhelper.Context(t)
+
+ first, err := client.AcquireCheckSlot(ctx)
+ require.NoError(t, err)
+ second, err := client.AcquireCheckSlot(ctx)
+ require.NoError(t, err)
+
+ // Both slots are held: a third waits until its deadline, then fails.
+ waitCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
+ defer cancel()
+ _, err = client.AcquireCheckSlot(waitCtx)
+ require.ErrorIs(t, err, context.DeadlineExceeded)
+ require.ErrorContains(t, err, "waiting for a check slot of the resolver")
+
+ // A slot released twice frees one place only, and checks no more.
+ first.Release()
+ first.Release()
+ _, err = first.Check(ctx, sampleRequest())
+ require.ErrorContains(t, err, "check without a check slot")
+
+ third, err := client.AcquireCheckSlot(ctx)
+ require.NoError(t, err)
+ waitCtx2, cancel2 := context.WithTimeout(ctx, 50*time.Millisecond)
+ defer cancel2()
+ _, err = client.AcquireCheckSlot(waitCtx2)
+ require.ErrorIs(t, err, context.DeadlineExceeded)
+
+ // A waiting acquire gets the slot released while it waits.
+ got := make(chan error, 1)
+ go func() {
+ slot, err := client.AcquireCheckSlot(ctx)
+ if err == nil {
+ slot.Release()
+ }
+ got <- err
+ }()
+ second.Release()
+ require.NoError(t, <-got)
+ third.Release()
+}
+
+// TestAcquireCheckSlot_defaultConcurrency: without check_concurrency, the client holds
+// DefaultGraftResolverCheckConcurrency slots.
+func TestAcquireCheckSlot_defaultConcurrency(t *testing.T) {
+ client := newClient(t, commandConfig([]string{"/bin/true"}))
+ require.Equal(t, config.DefaultGraftResolverCheckConcurrency, cap(client.checkSlots))
+}
diff --git a/internal/graft/resolver/validate.go b/internal/graft/resolver/validate.go
index 4825e6d..c7e47eb 100644
--- a/internal/graft/resolver/validate.go
+++ b/internal/graft/resolver/validate.go
@@ -17,7 +17,8 @@ import (
// - every written mode is one of 100644, 100755, 120000;
// - no written content exceeds req.Limits.MaxFileBytes;
// - no written text file (content without NUL bytes) contains a conflict
-// marker line ("<<<<<<< ", "=======", ">>>>>>> " at line start).
+// marker line ("<<<<<<< ", "=======", ">>>>>>> " at line start);
+// - no written file contains a line starting with ConflictPointerPrefix.
//
// Violations are reported wrapping ErrInvalidResponse. A request without a
// positive req.Limits.MaxFileBytes is a caller error and is reported without
@@ -66,8 +67,14 @@ func Validate(req *Request, resp *Response) error {
return nil
}
+// ConflictPointerPrefix starts the line that graft land --conflicts-dir writes in the file to
+// edit in place of a binary side, which that file cannot embed. A resolution that still holds such
+// a line would land the pointer instead of the side, so no written file may hold one.
+const ConflictPointerPrefix = "graft-conflict-pointer: "
+
// ValidateFile applies the acceptance rules of Validate to one resolved file: the action, the
-// mode, the size and the conflict markers. The server applies it to an agent's resolution too.
+// mode, the size, the conflict markers and the pointer lines. The server applies it to an agent's
+// resolution too.
func ValidateFile(f ResponseFile, maxBytes int64) error {
return validateFile(f, maxBytes)
}
@@ -94,6 +101,10 @@ func validateFile(f ResponseFile, maxBytes int64) error {
return errors.New("content contains a conflict marker line")
}
+ if hasConflictPointer(f.Content) {
+ return fmt.Errorf("content contains a line starting with %q, the pointer to a binary side that graft land --conflicts-dir writes: the side was not put in its place", ConflictPointerPrefix)
+ }
+
return nil
case ActionDelete:
if len(f.Content) > 0 {
@@ -213,6 +224,23 @@ func isBinary(content []byte) bool {
return bytes.IndexByte(content, 0) >= 0
}
+// hasConflictPointer reports whether a line of content starts with ConflictPointerPrefix.
+func hasConflictPointer(content []byte) bool {
+ prefix := []byte(ConflictPointerPrefix)
+ for len(content) > 0 {
+ line := content
+ if i := bytes.IndexByte(content, '\n'); i >= 0 {
+ line, content = content[:i], content[i+1:]
+ } else {
+ content = nil
+ }
+ if bytes.HasPrefix(line, prefix) {
+ return true
+ }
+ }
+ return false
+}
+
// hasConflictMarker reports whether a line of content starts with a conflict
// marker: "<<<<<<< " or ">>>>>>> " followed by a label, or "=======" optionally
// followed by whitespace only. A trailing carriage return is ignored.
diff --git a/internal/graft/resolver/validate_test.go b/internal/graft/resolver/validate_test.go
index cba70cd..2d09880 100644
--- a/internal/graft/resolver/validate_test.go
+++ b/internal/graft/resolver/validate_test.go
@@ -310,3 +310,27 @@ func TestValidateFile_specialFileSymlinks(t *testing.T) {
require.NoError(t, ValidateFile(ResponseFile{Path: name, Action: ActionWrite, Mode: ModeSymlink, Content: []byte("target")}, 1<<20), "%q", name)
}
}
+
+// TestValidateFile_pointerLine: a written file holding the pointer line of a binary side, as
+// graft land --conflicts-dir writes it, is refused, text or binary; the prefix elsewhere in a line
+// is content like any other.
+func TestValidateFile_pointerLine(t *testing.T) {
+ t.Parallel()
+
+ for name, content := range map[string]string{
+ "alone": ConflictPointerPrefix + "the binary ours side is sides/ours/c.bin\n",
+ "after text": "x\n" + ConflictPointerPrefix + "the binary theirs side is sides/theirs/c.bin",
+ "in binary": "\x00bin\n" + ConflictPointerPrefix + "the binary ours side is sides/ours/c.bin\n",
+ "with a CR ending": "x\r\n" + ConflictPointerPrefix + "p\r\n",
+ } {
+ err := ValidateFile(ResponseFile{Path: "c.bin", Action: ActionWrite, Mode: ModeRegular, Content: []byte(content)}, 1<<20)
+ require.ErrorContains(t, err, "the pointer to a binary side", name)
+ }
+ require.NoError(t, ValidateFile(ResponseFile{Path: "c.txt", Action: ActionWrite, Mode: ModeRegular, Content: []byte("see " + ConflictPointerPrefix + "x\n")}, 1<<20))
+
+ // Validate applies it to a resolver's answer too.
+ req := &Request{Files: []File{{Path: "c.bin"}}, Limits: Limits{MaxFileBytes: 1 << 20}}
+ err := Validate(req, &Response{Version: ProtocolVersion, Files: []ResponseFile{{Path: "c.bin", Action: ActionWrite, Mode: ModeRegular, Content: []byte(ConflictPointerPrefix + "p\n")}}})
+ require.ErrorIs(t, err, ErrInvalidResponse)
+ require.ErrorContains(t, err, "the pointer to a binary side")
+}