Git designed for AI agents.
land: one resolver per process, panic-safe slots, pointer exemption
Federico Elia462b098bade1
diff --git a/cmd/graft/conflicts.go b/cmd/graft/conflicts.goindex d67e89d..34e7a68 100644--- a/cmd/graft/conflicts.go+++ b/cmd/graft/conflicts.go@@ -290,6 +290,39 @@ func isBinaryContent(b []byte) bool {return bytes.IndexByte(b, 0) >= 0}+// hasPointerLine reports whether a line of content starts with conflictPointerPrefix.+func hasPointerLine(content []byte) bool {+ for _, line := range bytes.Split(content, []byte("\n")) {+ if bytes.HasPrefix(line, []byte(conflictPointerPrefix)) {+ return true+ }+ }+ return false+}++// sideHasPointerLine reports whether a side of the conflicting path p under DIR/sides has a pointer+// line of its own: the file then holds such a line legitimately (the server exempts it the same+// way). A side that is absent has none.+func sideHasPointerLine(dir, p string) (bool, error) {+ for _, side := range conflictSides {+ sideFile, err := conflictPath(dir, filepath.Join(conflictsSidesDir, side), p)+ if err != nil {+ return false, err+ }+ content, err := os.ReadFile(sideFile)+ if errors.Is(err, fs.ErrNotExist) {+ continue+ }+ if err != nil {+ return false, err+ }+ if hasPointerLine(content) {+ return true, nil+ }+ }+ return false, nil+}+// 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 file mode of the tip's side, else of the landed commit's, else of the base's: a side@@ -319,8 +352,12 @@ func readResolution(dir string) (*workspace.Resolution, error) {if err != nil {return nil, err}- for _, line := range bytes.Split(content, []byte("\n")) {- if bytes.HasPrefix(line, []byte(conflictPointerPrefix)) {+ if hasPointerLine(content) {+ own, err := sideHasPointerLine(dir, p)+ if err != nil {+ return nil, err+ }+ if !own {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))}}diff --git a/cmd/graft/conflicts_test.go b/cmd/graft/conflicts_test.goindex a34cb20..f42333e 100644--- a/cmd/graft/conflicts_test.go+++ b/cmd/graft/conflicts_test.go@@ -186,6 +186,24 @@ func TestReadResolutionRefusesAPointerLine(t *testing.T) {require.ErrorContains(t, err, "still holds the pointer to a binary side")}+// TestReadResolutionAcceptsAPointerLineOfTheSides: a file that holds a pointer line which a side+// has of its own (a document about the line, say) is content, as the server exempts it too.+func TestReadResolutionAcceptsAPointerLineOfTheSides(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("doc/c.md"), Base: side, Ours: side, Theirs: side}}})+ line := conflictPointerPrefix + "an example of the line\n"+ require.NoError(t, os.MkdirAll(filepath.Join(dir, "sides", "ours", "doc"), 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(dir, "sides", "ours", "doc", "c.md"), []byte("doc\n"+line), 0o644))+ require.NoError(t, os.MkdirAll(filepath.Join(dir, "files", "doc"), 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(dir, "files", "doc", "c.md"), []byte("doc, resolved\n"+line), 0o644))+ res, err := readResolution(dir)+ require.NoError(t, err)+ require.Len(t, res.Files, 1)+}+// 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) {diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.mdindex 65f3dd6..8b75ec0 100644--- a/doc/graft/CONFIGURATION.md+++ b/doc/graft/CONFIGURATION.md@@ -196,7 +196,7 @@ cause.| `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. 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`. |+| `check_concurrency` | int | `2` | Checks of agents' resolutions that run at once in the Gitaly process, across all its listeners (one resolver client is shared by them); 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.mdindex c52af22..39edb16 100644--- a/doc/graft/PLAN10.md+++ b/doc/graft/PLAN10.md@@ -629,6 +629,20 @@ way.error. The pointer-line rule is in the server's validation (resolver.ConflictPointerPrefix), soclients of the SDK get it; the CLI keeps its earlier message.+* **Re-review of fcf5f8d** (PM: fix first).+ * The resolver client is built once per process (resolver.Shared in service.Dependencies, as the+ land lanes are): its check slots bound the process across listeners, and a configuration that+ cannot be built is one error. A server whose dependencies carry none builds its own.+ * A panic can no longer lose a check slot or the precheck's quarantine: both are released by+ defer (a test panics inside a batch and takes the only slot again).+ * The pointer-line rule exempts a file whose conflict has a side with such a line of its own, in+ Validate (sides from the request), on the server for agents' resolutions (the sides are read+ only when the rule fires) and in the CLI.+ * The precheck reads the branch as the lane does (ResolveRevision), so a branch that is a+ symbolic reference is checked at its target's tip.+ * A check request that runs out of the check's deadline says so; one that runs out of its own+ timeout names the timeout.+## 13. Prompt first (PM and Federico, approved)Direction from Federico: the prompt first, then the models; adopt a structure that is already useddiff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex b841065..40b517d 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -39,7 +39,8 @@ actors = ["builder", "planner"]lists every conflicting file, and the check of an agent's resolution coversall its files.- `check_concurrency` (default 2): the checks of agents' resolutions that run- at once (see the check below).+ at once in the Gitaly process, whatever listener the lands arrive on (see+ the check below).- A configuration the server could not use is a startup error (see[CONFIGURATION](CONFIGURATION.md)). A configured resolver is never treatedas no resolver: if it cannot be built later, every land that needs it fails@@ -99,7 +100,9 @@ A client asks for resolution per call:rule of the server refuses it until the agent resolves it, and`graft land --resolved` refuses a file that still holds a pointer line, sothat 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+ such a line too, so a client of the SDK gets the same rule. Both accept the+ line when a side of the conflict has one of its own (a file that documents+ the line, say). `DIR` must nothold a conflicts directory yet: one being edited is never overwritten. Apath that cannot be a file under `DIR` on this system (a backslash, onWindows a colon or a name the system reserves) is an error, and so are two paths that@@ -417,9 +420,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 `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;+ markers, no `graft-conflict-pointer:` line unless a side of the conflict has+ one of its own, 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 (amerge with more cannot be resolved through a land, and nothing is read orchecked);diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.goindex 6e2599c..24225f2 100644--- a/internal/cli/gitaly/serve.go+++ b/internal/cli/gitaly/serve.go@@ -44,6 +44,7 @@ import (_ "github.com/feder-cr/git-ai/internal/graft/derived/graph" // registers the graph warmer"github.com/feder-cr/git-ai/internal/graft/eager""github.com/feder-cr/git-ai/internal/graft/landlane"+ "github.com/feder-cr/git-ai/internal/graft/resolver""github.com/feder-cr/git-ai/internal/grpc/backchannel""github.com/feder-cr/git-ai/internal/grpc/client"housekeepingmw "github.com/feder-cr/git-ai/internal/grpc/middleware/housekeeping"@@ -714,6 +715,14 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {prometheus.MustRegister(landLanes)defer landLanes.Close()+ // The conflict resolver is built once and shared by the operations servers of every+ // listener, so that its check slots bound the process (check_concurrency) and a resolver+ // that cannot be built is one error, which makes every land that needs it fail closed.+ graftResolver := resolver.NewShared(cfg.Graft.Land.Resolver)+ if graftResolver.Err != nil {+ logger.WithError(graftResolver.Err).Error("graft: the conflict resolver of land cannot be built: lands that need it fail")+ }+for _, c := range []starter.Config{{Name: starter.Unix, Addr: cfg.SocketPath, HandoverOnUpgrade: true},{Name: starter.Unix, Addr: cfg.InternalSocketPath(), HandoverOnUpgrade: false},@@ -774,6 +783,7 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {ObjectPoolStateManager: objectPoolStateManager,PostReceiveRegistry: postReceiveRegistry,GraftLandLanes: landLanes,+ GraftResolver: graftResolver,GraftStorage: storageCore,GraftStorageSyncer: storageSyncer,ShutdownContext: shutdownCtx,diff --git a/internal/gitaly/service/dependencies.go b/internal/gitaly/service/dependencies.goindex 85bc5f9..5be6e97 100644--- a/internal/gitaly/service/dependencies.go+++ b/internal/gitaly/service/dependencies.go@@ -22,6 +22,7 @@ import ("github.com/feder-cr/git-ai/internal/gitlab""github.com/feder-cr/git-ai/internal/graft/corestore""github.com/feder-cr/git-ai/internal/graft/landlane"+ "github.com/feder-cr/git-ai/internal/graft/resolver""github.com/feder-cr/git-ai/internal/grpc/backchannel""github.com/feder-cr/git-ai/internal/grpc/client""github.com/feder-cr/git-ai/internal/grpc/middleware/limithandler"@@ -64,6 +65,10 @@ type Dependencies struct {// listener, and every operations server takes its lanes from here, so that the requests for// one branch are batched in one lane whichever listener they arrive on.GraftLandLanes *landlane.Shared+ // GraftResolver is the conflict resolver of the process, built once: every operations server+ // takes it from here, so that its check slots bound the whole process. Nil makes each server+ // build its own (setups that register one server).+ GraftResolver *resolver.Shared// GraftStorage is the storage core (doc/graft/STORAGE.md), nil when it is off.GraftStorage *corestore.Core// GraftStorageSyncer catches up the local repositories in authoritative mode. It is nil when@@ -228,6 +233,12 @@ func (dc *Dependencies) GetGraftLandLanes() *landlane.Shared {return dc.GraftLandLanes}+// GetGraftResolver returns the conflict resolver of the process, nil when the dependencies were+// built without it.+func (dc *Dependencies) GetGraftResolver() *resolver.Shared {+ return dc.GraftResolver+}+// GetGraftStorage returns the storage core, nil when it is off.func (dc *Dependencies) GetGraftStorage() *corestore.Core {return dc.GraftStoragediff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.goindex 72b5c09..c884158 100644--- a/internal/gitaly/service/operations/land.go+++ b/internal/gitaly/service/operations/land.go@@ -76,16 +76,7 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*g// enters its lane, so that no branch is held while it runs (landPrecheckResolution).var precheck *landPrecheckif 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 {+ if precheck, err = s.landPrecheckInQuarantine(ctx, in); err != nil {return nil, err}}@@ -106,6 +97,18 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*greturn resp, nil}+// landPrecheckInQuarantine runs the precheck in a quarantine that it discards, even on a panic:+// the precheck merges the tip and the change, which writes objects, and it leaves nothing in the+// repository, whatever the storage mode.+func (s *Server) landPrecheckInQuarantine(ctx context.Context, in *gitalypb.UserLandRequest) (*landPrecheck, error) {+ _, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, in.GetRepository())+ if err != nil {+ return nil, err+ }+ defer cleanup()+ return s.landPrecheckResolution(ctx, in, quarantineRepo)+}+// landWithRetries lands the request alone, retrying the compare-and-swap while the branch moves. It// makes at most max_attempts attempts.//diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.goindex 12c1af8..080ecb7 100644--- a/internal/gitaly/service/operations/land_reland.go+++ b/internal/gitaly/service/operations/land_reland.go@@ -101,6 +101,10 @@ func (s *Server) landReviewResolution(ctx context.Context, in *gitalypb.UserLandfor _, f := range l.files {rf := resolved[f.conflict.path]err := resolver.ValidateFile(rf, maxBytes)+ if errors.Is(err, resolver.ErrConflictPointer) {+ // A pointer line is the agent's own when a side has one too: read the sides only then.+ err = landValidateWithSides(ctx, repo, f.conflict, rf, maxBytes, err)+ }if err == nil {err = landModeOfSides(f.conflict, rf)}@@ -112,6 +116,32 @@ func (s *Server) landReviewResolution(ctx context.Context, in *gitalypb.UserLandreturn review, nil}+// landValidateWithSides validates rf again with the sides of its conflict, which exempt a pointer+// line when one of them has one (resolver.ValidateFileWithSides). A side over maxBytes, or a+// gitlink, cannot be one of the agent's files and is not read; a side that cannot be read keeps+// the rejection, with why.+func landValidateWithSides(ctx context.Context, repo *localrepo.Repo, c landFileConflict, rf resolver.ResponseFile, maxBytes int64, rejection error) error {+ var sides [][]byte+ for _, b := range []*landBlob{c.base, c.ours, c.theirs} {+ if b == nil || b.mode == landModeGitlink {+ continue+ }+ info, err := repo.ReadObjectInfo(ctx, b.oid.Revision())+ if err != nil {+ return fmt.Errorf("%w (reading its sides for a pointer line of their own: %v)", rejection, err)+ }+ if info.Size > maxBytes {+ continue+ }+ content, err := repo.ReadObject(ctx, b.oid)+ if err != nil {+ return fmt.Errorf("%w (reading its sides for a pointer line of their own: %v)", rejection, err)+ }+ sides = append(sides, content)+ }+ return resolver.ValidateFileWithSides(rf, maxBytes, sides...)+}+// 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; or, when// no check was run because the lane refuses or answers the land without one, why (skipped).@@ -152,14 +182,16 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa// 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())))+ // The branch is read as the lane reads it (landingAttempt.Tip): a symbolic reference resolves+ // to its target's commit.+ branchTip, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())switch {case errors.Is(err, git.ErrReferenceNotFound):return &landPrecheck{skipped: fmt.Sprintf("branch %q does not exist", string(in.GetBranch()))}, nilcase 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+ case branchTip != tip:+ return &landPrecheck{skipped: fmt.Sprintf("the branch was at %s, not at the tip of the resolution", branchTip)}, 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@@ -377,20 +409,10 @@ func (s *Server) landCheckResolution(var unchecked []stringfor _, batch := range batches {- 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.- for _, f := range batch {- f.sides, f.read = [3][]byte{}, false- }+ checked, resp, stage, err := s.landCheckSlotted(ctx, in, repo, l, actor, resolved, batch, cache, &unchecked)switch {case err != nil:- return fail("checking", err)+ return fail(stage, err)case resp == nil:continuecase !landResolverNameValid(resp.Resolver):@@ -422,6 +444,39 @@ func (s *Server) landCheckResolution(return result}+// landCheckSlotted takes a check slot and checks one batch with it (landCheckBatch). The slot and+// the sides of the batch are released when it returns, even on a panic: a lost slot would refuse+// every later check of the process. stage names what failed, for the result's failure.+func (s *Server) landCheckSlotted(+ ctx context.Context,+ in *gitalypb.UserLandRequest,+ repo *localrepo.Repo,+ l landListing,+ actor string,+ resolved map[string]resolver.ResponseFile,+ batch []*landFileState,+ cache *landClassifyCache,+ unchecked *[]string,+) ([]*landFileState, *resolver.CheckResponse, string, error) {+ slot, err := s.conflictResolver.AcquireCheckSlot(ctx)+ if err != nil {+ return nil, nil, "waiting for the resolver", err+ }+ defer 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.+ defer func() {+ for _, f := range batch {+ f.sides, f.read = [3][]byte{}, false+ }+ }()+ if s.landCheckBatchHook != nil {+ s.landCheckBatchHook()+ }+ checked, resp, err := s.landCheckBatch(ctx, in, repo, l, actor, resolved, batch, cache, slot, unchecked)+ return checked, resp, "checking", err+}+// 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.diff --git a/internal/gitaly/service/operations/land_resolver_shared_test.go b/internal/gitaly/service/operations/land_resolver_shared_test.gonew file mode 100755index 0000000..03c6a26--- /dev/null+++ b/internal/gitaly/service/operations/land_resolver_shared_test.go@@ -0,0 +1,136 @@+package operations++import (+ "context"+ "path/filepath"+ "testing"+ "time"++ "github.com/stretchr/testify/require"++ "github.com/feder-cr/git-ai/internal/git"+ "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/gitaly/service"+ "github.com/feder-cr/git-ai/internal/graft/resolver"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// TestNewServer_sharesTheResolverOfTheProcess: the operations servers of every listener take the+// resolver of the process from their dependencies, so that its check slots bound the process: a+// slot that one server holds is one the other cannot take.+func TestNewServer_sharesTheResolverOfTheProcess(t *testing.T) {+ t.Parallel()++ cfg := testcfg.Build(t)+ cfg.Graft.Land.Resolver = config.GraftResolver{Enabled: true, Command: []string{"/bin/true"}, CheckConcurrency: 1}+ shared := resolver.NewShared(cfg.Graft.Land.Resolver)+ require.NoError(t, shared.Err)+ newDeps := func() *service.Dependencies {+ return &service.Dependencies{Cfg: cfg, Logger: testhelper.SharedLogger(t), GraftResolver: shared}+ }++ unixServer, tcpServer := NewServer(newDeps()), NewServer(newDeps())+ defer unixServer.landLanes.Close()+ defer tcpServer.landLanes.Close()+ require.Same(t, unixServer.conflictResolver, tcpServer.conflictResolver)++ ctx := testhelper.Context(t)+ slot, err := unixServer.conflictResolver.AcquireCheckSlot(ctx)+ require.NoError(t, err)+ waitCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)+ defer cancel()+ _, err = tcpServer.conflictResolver.AcquireCheckSlot(waitCtx)+ require.ErrorIs(t, err, context.DeadlineExceeded, "the bound is the process's, not the listener's")+ slot.Release()+}++// TestNewServer_resolverThatCannotBeBuilt: a resolver configuration that fails to build at run time+// (its token file gone since the configuration was validated) gives no resolver and the error,+// whether the server builds it or takes it from its dependencies.+func TestNewServer_resolverThatCannotBeBuilt(t *testing.T) {+ t.Parallel()++ cfg := testcfg.Build(t)+ cfg.Graft.Land.Resolver = config.GraftResolver{Enabled: true, URL: "https://resolver.example/", TokenFile: filepath.Join(t.TempDir(), "gone")}++ alone := NewServer(&service.Dependencies{Cfg: cfg, Logger: testhelper.SharedLogger(t)})+ defer alone.landLanes.Close()+ require.Nil(t, alone.conflictResolver)+ require.ErrorContains(t, alone.resolverErr, "read token file")++ withShared := NewServer(&service.Dependencies{Cfg: cfg, Logger: testhelper.SharedLogger(t), GraftResolver: resolver.NewShared(cfg.Graft.Land.Resolver)})+ defer withShared.landLanes.Close()+ require.Nil(t, withShared.conflictResolver)+ require.ErrorContains(t, withShared.resolverErr, "read token file")+}++func TestUserLand_relandRobustness(t *testing.T) {+ t.Parallel()++ t.Run("a batch that panics gives its check slot back", func(t *testing.T) {+ t.Parallel()++ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", true, ""))+ fx, s, req := batchedReland(t, argv)+ s.resolveCfg.CheckConcurrency = 1+ client, err := resolver.New(s.resolveCfg)+ require.NoError(t, err)+ s.conflictResolver = client+ s.landCheckBatchHook = func() { panic("a batch failed") }++ require.PanicsWithValue(t, "a batch failed", func() { _, _ = s.landPrecheckResolution(fx.ctx, req, fx.repo) })++ // The panic was recovered (as the gRPC server does): the only slot is free again.+ ctx, cancel := context.WithTimeout(fx.ctx, time.Second)+ defer cancel()+ slot, err := client.AcquireCheckSlot(ctx)+ require.NoError(t, err, "the slot of the panicking batch was given back")+ slot.Release()+ })++ t.Run("a pointer line that a side has of its own is not refused", func(t *testing.T) {+ t.Parallel()++ const pointer = "graft-conflict-pointer: an example of the line\n"+ fx := newResolveFixtureWith(t, config.GraftResolver{},+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "base\n"}, {Mode: "100644", Path: "b", Content: "doc\n" + pointer + "base\n"}},+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "ours\n"}, {Mode: "100644", Path: "b", Content: "doc\n" + pointer + "ours\n"}},+ []gittest.TreeEntry{{Mode: "100644", Path: "a", Content: "theirs\n"}, {Mode: "100644", Path: "b", Content: "doc\n" + pointer + "theirs\n"}},+ )+ req := fx.request("Land")+ req.ResolveConflicts = false+ req.Resolution = resolutionOf(fx, "agent-a\n", "doc\n"+pointer+"agent-b\n")+ _, err := fx.client.UserLand(fx.ctx, req)+ require.NoError(t, err)++ // a's sides have no such line: there it is still a pointer left in place.+ fx = newResolveFixture(t, config.GraftResolver{})+ req = fx.request("Land")+ req.ResolveConflicts = false+ req.Resolution = resolutionOf(fx, "x\n"+pointer, "agent-b\n")+ _, err = fx.client.UserLand(fx.ctx, req)+ pkg := landConflictDetail(err)+ require.NotNil(t, pkg)+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_REJECTED, packageFile(t, pkg, "a").GetOutcome())+ require.Contains(t, packageFile(t, pkg, "a").GetReason(), "the pointer to a binary side")+ })++ t.Run("a branch that is a symbolic reference is checked at its target's tip", func(t *testing.T) {+ t.Parallel()++ _, argv := perCallChecker(t, checkAnswer("graft-resolve", "a", true, ""), checkAnswer("graft-resolve", "b", true, ""))+ fx, s, req := batchedReland(t, argv)+ gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "symbolic-ref", "refs/heads/alias", git.NewReferenceNameFromBranchName(git.DefaultBranch).String())+ req.Branch = []byte("alias")++ pre, err := s.landPrecheckResolution(fx.ctx, req, fx.repo)+ require.NoError(t, err)+ require.Empty(t, pre.skipped, "the tip of the alias is the tip of main")+ require.Equal(t, fx.tip, pre.tip)+ require.Empty(t, pre.result.failure)+ require.Len(t, pre.result.verdicts, 2)+ })+}diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.goindex f0228b4..973ab7d 100644--- a/internal/gitaly/service/operations/server.go+++ b/internal/gitaly/service/operations/server.go@@ -38,6 +38,9 @@ type Server struct {// checkBatchBytes, when set, replaces the bound of the file data of one check request// (landCheckBatchBytes); only tests set it.checkBatchBytes int64+ // landCheckBatchHook, when set, runs in every check batch while it holds its slot; only tests+ // set it.+ landCheckBatchHook func()// 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).@@ -58,14 +61,19 @@ type Server struct {func NewServer(deps *service.Dependencies) *Server {resolveCfg := deps.GetCfg().Graft.Land.Resolver- // 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+ // The configuration is validated at startup (config.GraftResolver.validate), so the resolver+ // fails to build 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. The resolver of the process is shared by every server (service.Dependencies);+ // a server without one builds its own.+ shared := deps.GetGraftResolver()+ if shared == nil {+ shared = resolver.NewShared(resolveCfg)+ if shared.Err != nil {+ deps.GetLogger().WithError(shared.Err).Error("graft: the conflict resolver of land cannot be built: lands that need it fail")+ }}+ conflictResolver, resolverErr := shared.Client, shared.Errif conflictResolver != nil && resolverActorsUnmatchable(deps.GetCfg().Graft) {deps.GetLogger().Warn("graft: [graft.land.resolver] actors is set but no [[graft.agent]] is " +"configured, so callers carry no identity and no landing may resolve conflicts")diff --git a/internal/graft/resolver/check.go b/internal/graft/resolver/check.goindex 96f0d54..bc54562 100644--- a/internal/graft/resolver/check.go+++ b/internal/graft/resolver/check.go@@ -121,6 +121,7 @@ func (c *Client) check(ctx context.Context, req Request) (*CheckResponse, error)return nil, fmt.Errorf("check request of %d bytes is over the bound of %d", len(body), MaxCheckRequestBytes)}+ parent := ctxctx, cancel := context.WithTimeout(ctx, c.timeout)defer cancel()@@ -134,8 +135,12 @@ func (c *Client) check(ctx context.Context, req Request) (*CheckResponse, error)raw, err = c.callCommand(ctx, body, limit)}if err != nil {- if errors.Is(ctx.Err(), context.DeadlineExceeded) {- return nil, fmt.Errorf("resolver check timed out after %s: %w", c.timeout, ctx.Err())+ switch {+ case parent.Err() != nil:+ // The deadline of the whole check (or the caller's) ended first.+ return nil, fmt.Errorf("resolver check stopped at the deadline of the check: %w", parent.Err())+ case errors.Is(ctx.Err(), context.DeadlineExceeded):+ return nil, fmt.Errorf("resolver check request timed out after %s: %w", c.timeout, ctx.Err())}return nil, fmt.Errorf("resolver check: %w", err)}diff --git a/internal/graft/resolver/client_test.go b/internal/graft/resolver/client_test.goindex 41dd176..e4a9503 100644--- a/internal/graft/resolver/client_test.go+++ b/internal/graft/resolver/client_test.go@@ -726,3 +726,43 @@ func TestAcquireCheckSlot_defaultConcurrency(t *testing.T) {client := newClient(t, commandConfig([]string{"/bin/true"}))require.Equal(t, config.DefaultGraftResolverCheckConcurrency, cap(client.checkSlots))}++// TestCheck_namesTheDeadlineThatEnded: a check request that runs out of its own timeout says so; one+// that runs out of the deadline of the whole check names that deadline instead.+func TestCheck_namesTheDeadlineThatEnded(t *testing.T) {+ requireUnix(t)++ cfg := commandConfig([]string{writeScript(t, "cat > /dev/null\nsleep 5\n")})+ req := sampleRequest()+ req.Resolution = validResponse("merged\n").Files++ cfg.Timeout = "100ms"+ _, err := checkWithSlot(t, newClient(t, cfg), req)+ require.ErrorContains(t, err, "resolver check request timed out after 100ms")++ cfg.Timeout = "1m"+ client := newClient(t, cfg)+ slot, err := client.AcquireCheckSlot(testhelper.Context(t))+ require.NoError(t, err)+ defer slot.Release()+ ctx, cancel := context.WithTimeout(testhelper.Context(t), 100*time.Millisecond)+ defer cancel()+ _, err = slot.Check(ctx, req)+ require.ErrorContains(t, err, "resolver check stopped at the deadline of the check")+ require.ErrorIs(t, err, context.DeadlineExceeded)+}++// TestNewShared: the resolver of the process carries the client, or why it cannot be built.+func TestNewShared(t *testing.T) {+ shared := NewShared(commandConfig([]string{"/bin/true"}))+ require.NoError(t, shared.Err)+ require.NotNil(t, shared.Client)++ shared = NewShared(config.GraftResolver{Enabled: true, URL: "https://resolver.example/", TokenFile: filepath.Join(t.TempDir(), "gone")})+ require.ErrorContains(t, shared.Err, "read token file")+ require.Nil(t, shared.Client)++ shared = NewShared(config.GraftResolver{})+ require.NoError(t, shared.Err)+ require.Nil(t, shared.Client, "resolution disabled")+}diff --git a/internal/graft/resolver/shared.go b/internal/graft/resolver/shared.gonew file mode 100644index 0000000..8cfcd83--- /dev/null+++ b/internal/graft/resolver/shared.go@@ -0,0 +1,22 @@+package resolver++import "github.com/feder-cr/git-ai/internal/gitaly/config"++// Shared is the resolver client of the process: built once from the configuration and shared by+// the operations servers of every listener, so that the check slots bound the checks of the whole+// process and a configuration that cannot be built gives one error.+type Shared struct {+ // Client is nil when resolution is disabled or Err is set.+ Client *Client+ // Err is why the configured resolver cannot be built, if it cannot.+ Err error+}++// NewShared builds the resolver client of the process from cfg.+func NewShared(cfg config.GraftResolver) *Shared {+ client, err := New(cfg)+ if err != nil {+ return &Shared{Err: err}+ }+ return &Shared{Client: client}+}diff --git a/internal/graft/resolver/validate.go b/internal/graft/resolver/validate.goindex c7e47eb..eeef5e8 100644--- a/internal/graft/resolver/validate.go+++ b/internal/graft/resolver/validate.go@@ -18,7 +18,8 @@ import (// - no written content exceeds req.Limits.MaxFileBytes;// - no written text file (content without NUL bytes) contains a conflict// marker line ("<<<<<<< ", "=======", ">>>>>>> " at line start);-// - no written file contains a line starting with ConflictPointerPrefix.+// - no written file contains a line starting with ConflictPointerPrefix,+// unless a side of its conflict has such a line of its own.//// Violations are reported wrapping ErrInvalidResponse. A request without a// positive req.Limits.MaxFileBytes is a caller error and is reported without@@ -52,7 +53,7 @@ func Validate(req *Request, resp *Response) error {}answered[f.Path] = struct{}{}- if err := validateFile(f, req.Limits.MaxFileBytes); err != nil {+ if err := validateFile(f, req.Limits.MaxFileBytes, sidesHavePointer(req.Files, f.Path)); err != nil {return fmt.Errorf("%w: path %q: %w", ErrInvalidResponse, f.Path, err)}}@@ -69,18 +70,49 @@ func Validate(req *Request, resp *Response) error {// 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.+// a line would land the pointer instead of the side, so no written file may hold one, unless a+// side of its conflict has such a line of its own (as the patch-lines rule of graft-resolve does).const ConflictPointerPrefix = "graft-conflict-pointer: "+// ErrConflictPointer is the rejection of a written file that holds a pointer line. A caller that+// did not give the sides (ValidateFile) may read them and validate again with+// ValidateFileWithSides, which exempts a file whose sides have such a line.+var ErrConflictPointer = errors.New("the pointer to a binary side that graft land --conflicts-dir writes was not replaced by the side")+// ValidateFile applies the acceptance rules of Validate to one resolved file: the action, the// 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)+ return validateFile(f, maxBytes, false)+}++// ValidateFileWithSides is ValidateFile for a file whose conflict has the given sides: a pointer+// line is accepted when one of them has one too.+func ValidateFileWithSides(f ResponseFile, maxBytes int64, sides ...[]byte) error {+ pointerAllowed := false+ for _, s := range sides {+ pointerAllowed = pointerAllowed || HasConflictPointer(s)+ }+ return validateFile(f, maxBytes, pointerAllowed)+}++// sidesHavePointer reports whether a side of the conflict of path in files has a pointer line.+func sidesHavePointer(files []File, path string) bool {+ for _, f := range files {+ if f.Path != path {+ continue+ }+ for _, s := range []*Side{f.Base, f.Ours, f.Theirs} {+ if s != nil && HasConflictPointer(s.Content) {+ return true+ }+ }+ }+ return false}-// validateFile checks one answered file.-func validateFile(f ResponseFile, maxBytes int64) error {+// validateFile checks one answered file. pointerAllowed exempts it from the pointer-line rule.+func validateFile(f ResponseFile, maxBytes int64, pointerAllowed bool) error {switch f.Action {case ActionWrite:if !validMode(f.Mode) {@@ -101,8 +133,8 @@ 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)+ if !pointerAllowed && HasConflictPointer(f.Content) {+ return fmt.Errorf("content contains a line starting with %q: %w", ConflictPointerPrefix, ErrConflictPointer)}return nil@@ -224,8 +256,8 @@ 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 {+// HasConflictPointer reports whether a line of content starts with ConflictPointerPrefix.+func HasConflictPointer(content []byte) bool {prefix := []byte(ConflictPointerPrefix)for len(content) > 0 {line := contentdiff --git a/internal/graft/resolver/validate_test.go b/internal/graft/resolver/validate_test.goindex 2d09880..688a1bc 100644--- a/internal/graft/resolver/validate_test.go+++ b/internal/graft/resolver/validate_test.go@@ -334,3 +334,24 @@ func TestValidateFile_pointerLine(t *testing.T) {require.ErrorIs(t, err, ErrInvalidResponse)require.ErrorContains(t, err, "the pointer to a binary side")}++// TestValidateFile_pointerLineOfTheSides: a pointer line that a side of the conflict has of its own+// is content, not a pointer left in place: Validate takes the sides from the request, and+// ValidateFileWithSides from its caller. Without such a side the rule holds.+func TestValidateFile_pointerLineOfTheSides(t *testing.T) {+ t.Parallel()++ content := []byte("doc\n" + ConflictPointerPrefix + "an example of the line\n")+ f := ResponseFile{Path: "c.md", Action: ActionWrite, Mode: ModeRegular, Content: content}++ require.ErrorIs(t, ValidateFile(f, 1<<20), ErrConflictPointer)+ require.NoError(t, ValidateFileWithSides(f, 1<<20, []byte("base\n"), content))+ require.ErrorIs(t, ValidateFileWithSides(f, 1<<20, []byte("base\n"), []byte("ours\n")), ErrConflictPointer)++ req := &Request{Files: []File{{Path: "c.md", Ours: &Side{Content: content}, Theirs: &Side{Content: []byte("t\n")}}}, Limits: Limits{MaxFileBytes: 1 << 20}}+ require.NoError(t, Validate(req, &Response{Version: ProtocolVersion, Files: []ResponseFile{f}}))+ req.Files[0].Ours.Content = []byte("o\n")+ err := Validate(req, &Response{Version: ProtocolVersion, Files: []ResponseFile{f}})+ require.ErrorIs(t, err, ErrInvalidResponse)+ require.ErrorIs(t, err, ErrConflictPointer)+}