Git designed for AI agents.
graft: the resolution goes out of the lane, bound to the files (m6, PLAN10 15.8)
Federico Elia9586fd20832e
diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.mdindex 84664ed..db97313 100644--- a/doc/graft/CONFIGURATION.md+++ b/doc/graft/CONFIGURATION.md@@ -198,6 +198,8 @@ cause.| `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. || `proposal_ttl` | string | `"168h"` | How long the proposals of a failed land are kept under `refs/graft/proposals/` (Go duration, by the server's clock); at most 20 are kept per branch. Lands with a resolution and the housekeeping pass prune them. || `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`. |+| `resolve_concurrency` | int | `2` | Resolutions that run at once in the Gitaly process, in a pool apart from the checks', so that long resolutions never delay a check. A resolution waits for a free slot within the land's deadline (`timeout`), and fails closed when it expires. The wait is in `graft_resolver_resolve_slot_wait_seconds`. |+| `resolve_rounds` | int | `32` | Safety cap on the rounds of a land whose conflicting files keep changing while it is resolved out of the lane. The rounds are bounded by `timeout`; the cap is set well above realistic contention and is no contract. || `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 53152fc..5671d46 100644--- a/doc/graft/PLAN10.md+++ b/doc/graft/PLAN10.md@@ -990,7 +990,10 @@ not exist yet; (not enabled) marks what is designed and measured but switched of| resolver check, bounded slots | `landCheckResolution`, `landCheckSlotted`, `Client.AcquireCheckSlot` | operations/land_reland.go; graft/resolver/check.go || LANE, re-land | `landAttempt`, `landingAttempt.decide`, `landWithResolution` | operations/land.go, land_attempt.go, land_reland.go || 3-way merge, listing | `landingAttempt.decide`, `landListConflicts` | operations/land_attempt.go, land_resolve.go |-| per-file verdicts, decide | `resolveLandConflicts`, `decideLand`, `landAnswerProposes` | operations/land_resolve.go, land_proposals.go |+| out of the lane: rounds, deadline | `landResolving`, `landPreResolve` | operations/land_preresolve.go |+| per-file verdicts, decide | `landResolveAt`, `decideLand`, `landAnswerProposes`, `landKeyOf` | operations/land_resolve.go, land_proposals.go, land_preresolve.go |+| verdicts applied in the lane | `landApplyVerdicts`, `landNeedsResolution` | operations/land_resolve.go, land_preresolve.go |+| resolver call, bounded slots | `Client.AcquireResolveSlot`, `ResolveSlot.Resolve` | graft/resolver/check.go || policy before resolving | `hook.CheckAgentPolicy`, `landPolicyDeniedError` | operations/land_resolve.go, land_proposals.go || classify | `landClassify`, `resolver.SendRules` | operations/land_resolve.go; graft/resolver || operator rules, file checks | `Resolver.resolveFile`, `Resolver.runChecks`, `syntax.CheckMerge` | tools/graft-agents/resolve/resolve.go |@@ -1095,10 +1098,11 @@ The resolution is bound to the files instead (the PM's correction):runs the classification again on every file it reuses: local git work, bounded like thepackage's, never the resolver. A file whose classification at the lane's tip differs from itsverdict's counts as changed.- * The request also carries the tip's commit message. R1's prompt sends only THEIRS' message,- which is fixed for the land; an operator's command strategy could read the tip's message, and- its verdict would then be reused across tips. That is stated in RESOLVE.md: a resolver's- answer for a file must depend on the file, not on the tip's message.+ * The request no longer carries the tip, its merge base or its commit message: the fields+ `tip`, `merge_base` and `messages.tip` are always empty, kept in the protocol for+ compatibility (a test asserts it). A verdict reused at another tip is then correct by+ construction: no resolver, the operator's command strategy included, can read the tip.+ RESOLVE.md states that an answer must depend on the file only.* **Pre-resolve before the lane.** UserLand reads the tip T as the lane does and, in a quarantineit discards, merges T and the change. Skipped (the lane answers as today) when the branch doesnot exist, the change already landed, or the merge is clean. Otherwise it runs the per-file flow@@ -1117,9 +1121,16 @@ The resolution is bound to the files instead (the PM's correction):* **Needs resolution.** When a conflicting file has no verdict with its key (it is new, or a sidechanged), the lane returns the attempt with those files, without resolving. UserLandpre-resolves only them against the new tip, out of the lane, keeps every other verdict, and- enters the lane again. The rounds are bounded by the resolver's timeout for the whole land and- by resolve_rounds (default 3); past them the land fails with the merge conflict and the reason- "files kept changing: a, b". A tip that moves on unrelated files costs no resolver call at all.+ enters the lane again. A tip that moves on unrelated files costs no resolver call at all.+* **Progress, and its limit.** A round is lost only when the lane found a file whose sides+ changed, that is when another land on that file landed. So the lands that contend for a file+ all land, at least one per round, and N contenders need at most N rounds each. The rounds are+ therefore bounded by the resolver's timeout for the whole land only; resolve_rounds (default+ 32) is a safety cap well above realistic contention, documented as a cap and not a contract+ (decided after the 12-concurrent graft-bench test failed with a cap of 3). The limit, stated:+ under continuous new arrivals on the same file, one land can keep losing until its deadline,+ and then fails with the merge conflict and the reason "files kept changing while the land was+ resolved, until the deadline of <timeout>: a"; past the cap, "after <n> rounds".* **Rules alike.** The operator rules' verdicts (trivial, union, rerere, command) are kept andreused by the same key, so the policy is the same for every strategy.* **Slots: two pools.** check_concurrency bounds the checks of agents' resolutions and@@ -1131,14 +1142,34 @@ The resolution is bound to the files instead (the PM's correction):* **Still in the lane, stated:** verify.command, the operator's tests on the merged tree, runs inthe lane for the rules' resolutions and for agents' resolutions alike. It is the next candidateto move out, as its own step.+* **Measured (TestLandResolveBench, GRAFT_LAND_BENCH=1).** Six lands that the server resolves,+ all on the same file, and six plain lands that conflict with nothing, at once through the lanes+ of a standalone server, with a resolver that takes 500 ms; the same bench on the tree before+ and after, three runs each:++ | | before (resolver in the lane) | after (out of the lane) |+ |---|---|---|+ | lane held | 3.48-3.54 s | 0.72-0.93 s |+ | plain lands, p50 | 3.47-3.53 s | 151-205 ms |+ | resolve lands, p95 | 3.44-3.58 s | 3.21-3.22 s |+ | resolver calls | 6 | 11 |++ The plain lands no longer wait for the resolutions, and the lane is held for git work only.+ The cost is in resolver calls: every land on the same file loses a round to each land that+ lands before it, and resolves again. That is the worst case by construction (one file, every+ land on it); lands on different files, or a tip that moves on other files, cost no extra call.* **Tests.*** The resolver is never called with the lane held: a resolver that blocks while another land ofthe branch proceeds.* A not-landable answer never enters the lane.* A tip that moves on an unrelated file reuses every verdict, with no second resolver call.* A tip that changes one conflicting file re-resolves only that file.- * Files that keep changing fail after resolve_rounds, with the reason naming them.+ * Files that keep changing fail at the deadline, or after resolve_rounds, with the reason+ naming them.* A batch member reuses its verdicts.* A high-churn test: many concurrent lands on the branch, the resolve lands among them land- eventually, with no starvation.+ eventually, with no starvation; and with a short deadline, every land ends, landed or with+ its reason, and none hangs.+ * A proposal kept while a batch holds the lane has the published tip as parent (review of+ 6090d5b, item 4): proposals are written before the lane only.* The two pools: a held resolve slot does not delay a check.diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex 0ca39e7..6e08fe4 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -41,6 +41,11 @@ actors = ["builder", "planner"]- `check_concurrency` (default 2): the checks of agents' resolutions that runat once in the Gitaly process, whatever listener the lands arrive on (seethe check below).+- `resolve_concurrency` (default 2): the resolutions that run at once in the+ Gitaly process, in a pool apart from the checks', so that long resolutions+ never delay the short checks. `resolve_rounds` (default 32) is a safety cap+ on the rounds of a land whose files keep changing (see "Out of the lane"+ below); the rounds are bounded by `timeout`, and the cap is no contract.- 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@@ -209,8 +214,8 @@ Request (server to resolver):```json{"version": 1,"repository": {"storage": "default", "relative_path": "x.git"},- "branch": "main", "tip": "<oid>", "commit": "<oid>", "merge_base": "<oid>",- "messages": {"tip": "<tip commit message>", "commit": "<landed commit message>"},+ "branch": "main", "tip": "", "commit": "<oid>", "merge_base": "",+ "messages": {"tip": "", "commit": "<landed commit message>"},"actor": "agent-7","files": [{"path": "a/b.go", "kind": "content","base": {"oid": "<oid>", "mode": "100644", "content": "<base64>"},@@ -220,6 +225,12 @@ Request (server to resolver):```- `ours` is the current branch tip side. `theirs` is the change being landed.+- `tip`, `merge_base` and `messages.tip` are always empty since this version;+ they stay in the protocol for compatibility. The server reuses a file's+ answer at any tip where the file's sides are the same (see "Out of the lane"+ below), so an answer for a file must depend only on the file: its path, its+ sides and their modes, the change's message and the resolver's own+ configuration, never on the tip.- Any side may be `null` when it is absent (for example in an add-addconflict the base is `null`).- `kind` is one of `content`, `modify-delete`, `add-add`, `mode`.@@ -402,6 +413,37 @@ Every resolution carries its trailers, so it can be found and undone:bisects a failure to the commit that broke it and reverts it with`UserRevert`, whether it was an AI resolution or not.+## Out of the lane++The server calls the resolver before a land enters its branch's lane, never+while the lane is held, so a resolution that takes minutes does not hold the+branch for the other lands:++1. The server merges the branch's current tip and the change in a quarantine it+ discards, and resolves the conflicting files there: the policy first, the+ classification, one request with a resolve slot, the validation and the+ decision. Each landable answer for a file is kept by the file's key: its+ path and its three sides, each by object ID and mode.+2. An answer that does not land (a model's proposal, a refusal, a file left)+ never enters the lane: the land fails with the package, and the proposals+ are kept.+3. A landable answer enters the lane, which merges its own tip and the change+ and writes the merge commit when every conflicting file has an answer with+ its key; the resolver is not called. A tip that moved on other files costs+ nothing. When a file changed (or is new), the lane hands the land back, and+ the server resolves only that file, out of the lane, and enters the lane+ again.++A round is lost only when another land changed one of the files, so the lands+that contend for a file all land, one per round. The rounds are bounded by the+resolver's `timeout` for the whole land, and by `resolve_rounds`, a safety cap+well above realistic contention. The limit, stated: under continuous new+arrivals on the same file, one land can keep losing until its deadline; it then+fails with the merge conflict and the reason `files kept changing while the land+was resolved, until the deadline of <timeout>: <paths>`.++`verify.command` still runs in the lane, on the merged tree of the lane's tip.+## The conflict package and the re-landWhen a land with `resolve_conflicts` (or with a `resolution`) fails on its@@ -903,6 +945,8 @@ in blank lines only, and the accepted results that got no syntax verdict.| 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` |+| resolutions at once | 2 | `resolve_concurrency` |+| rounds of a land whose files keep changing | 32 (a safety cap; `timeout` bounds the land) | `resolve_rounds` || 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.goindex 15ef829..0afb605 100644--- a/internal/gitaly/config/config_graft.go+++ b/internal/gitaly/config/config_graft.go@@ -323,6 +323,15 @@ type GraftResolver struct {// each holds a check request of up to GraftResolverMaxCheckRequestBytes. Zero means// DefaultGraftResolverCheckConcurrency.CheckConcurrency int `json:"check_concurrency" toml:"check_concurrency,omitempty"`+ // ResolveConcurrency bounds the resolutions that run at once on this server (PLAN10 15.8), in a+ // pool of their own so that long resolutions never delay the short checks. Zero means+ // DefaultGraftResolverResolveConcurrency.+ ResolveConcurrency int `json:"resolve_concurrency" toml:"resolve_concurrency,omitempty"`+ // ResolveRounds is a safety cap on the rounds of a land whose conflicting files keep changing+ // while it is resolved (PLAN10 15.8). The rounds are bounded by the resolver's timeout; the cap+ // is set well above realistic contention and is no contract. Zero means+ // DefaultGraftResolverResolveRounds.+ ResolveRounds int `json:"resolve_rounds" toml:"resolve_rounds,omitempty"`// ProposalTTL is how long the server keeps the proposals of a failed land// (refs/graft/proposals/), as a Go duration ("168h"). Empty means// DefaultGraftResolverProposalTTL.@@ -509,8 +518,8 @@ func (r GraftResolver) validate() error {if _, err := r.ProposalTTLOrDefault(); err != nil {return err}- 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")+ if r.MaxFiles < 0 || r.MaxFileBytes < 0 || r.CheckConcurrency < 0 || r.ResolveConcurrency < 0 || r.ResolveRounds < 0 {+ return fmt.Errorf("graft land resolver: max_files, max_file_bytes, check_concurrency, resolve_concurrency and resolve_rounds 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.@@ -578,6 +587,29 @@ func (r GraftResolver) CheckConcurrencyOrDefault() int {return DefaultGraftResolverCheckConcurrency}+// DefaultGraftResolverResolveConcurrency is the default of GraftResolver.ResolveConcurrency.+const DefaultGraftResolverResolveConcurrency = 2++// ResolveConcurrencyOrDefault returns ResolveConcurrency or its default.+func (r GraftResolver) ResolveConcurrencyOrDefault() int {+ if r.ResolveConcurrency > 0 {+ return r.ResolveConcurrency+ }+ return DefaultGraftResolverResolveConcurrency+}++// DefaultGraftResolverResolveRounds is the default of GraftResolver.ResolveRounds: a cap well+// above the lands that contend for one file at once.+const DefaultGraftResolverResolveRounds = 32++// ResolveRoundsOrDefault returns ResolveRounds or its default.+func (r GraftResolver) ResolveRoundsOrDefault() int {+ if r.ResolveRounds > 0 {+ return r.ResolveRounds+ }+ return DefaultGraftResolverResolveRounds+}+// DefaultGraftResolverProposalTTL is the default of GraftResolver.ProposalTTL: a week.const DefaultGraftResolverProposalTTL = "168h"diff --git a/internal/gitaly/config/config_graft_resolver_test.go b/internal/gitaly/config/config_graft_resolver_test.goindex 0b8950c..4d248c6 100644--- a/internal/gitaly/config/config_graft_resolver_test.go+++ b/internal/gitaly/config/config_graft_resolver_test.go@@ -77,8 +77,8 @@ func TestGraftResolverMinifiedDefaults(t *testing.T) {// 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.+// with a host, a token file that cannot be read or is empty, a negative check_concurrency,+// resolve_concurrency or resolve_rounds, or a max_file_bytes over the bound of a check request.func TestGraftResolverValidateBuildable(t *testing.T) {t.Parallel()@@ -110,6 +110,12 @@ func TestGraftResolverValidateBuildable(t *testing.T) {"negative check_concurrency": {GraftResolver{Command: []string{"graft-resolve"}, CheckConcurrency: -1}, "must not be negative",},+ "negative resolve_concurrency": {+ GraftResolver{Command: []string{"graft-resolve"}, ResolveConcurrency: -1}, "must not be negative",+ },+ "negative resolve_rounds": {+ GraftResolver{Command: []string{"graft-resolve"}, ResolveRounds: -1}, "must not be negative",+ },"max_file_bytes over the bound": {GraftResolver{Command: []string{"graft-resolve"}, MaxFileBytes: GraftResolverMaxCheckFileBytes + 1}, "max_file_bytes",},@@ -127,6 +133,10 @@ func TestGraftResolverValidateBuildable(t *testing.T) {require.Equal(t, "168h0m0s", ttl.String())require.Equal(t, DefaultGraftResolverCheckConcurrency, GraftResolver{}.CheckConcurrencyOrDefault())require.Equal(t, 5, GraftResolver{CheckConcurrency: 5}.CheckConcurrencyOrDefault())+ require.Equal(t, DefaultGraftResolverResolveConcurrency, GraftResolver{}.ResolveConcurrencyOrDefault())+ require.Equal(t, 4, GraftResolver{ResolveConcurrency: 4}.ResolveConcurrencyOrDefault())+ require.Equal(t, DefaultGraftResolverResolveRounds, GraftResolver{}.ResolveRoundsOrDefault())+ require.Equal(t, 7, GraftResolver{ResolveRounds: 7}.ResolveRoundsOrDefault())}// TestGraftResolverCheckBounds: a file of GraftResolverMaxCheckFileBytes, its three sides and itsdiff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.goindex d106627..8a24dcd 100644--- a/internal/gitaly/service/operations/land.go+++ b/internal/gitaly/service/operations/land.go@@ -81,8 +81,20 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*g}}+ // A land that asks the server to resolve its conflicts is resolved out of the lane (PLAN10+ // 15.8).+ if in.GetResolveConflicts() && in.GetResolution() == nil {+ return s.landResolving(ctx, in, committer)+ }+ return s.landOnce(ctx, in, committer, precheck, nil)+}++// landOnce lands the request once through its lane, in a batch or alone, with the precheck of an+// agent's resolution or the pre-resolution of the server's, if any, and prunes the branch's+// proposals once it landed.+func (s *Server) landOnce(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck, pre *landPreResolution) (*gitalypb.UserLandResponse, error) {if s.landBatchable(ctx, in) {- resp, err := s.userLandBatched(ctx, in, committer, precheck)+ resp, err := s.userLandBatched(ctx, in, committer, precheck, pre)if err != nil {return nil, err}@@ -90,7 +102,7 @@ func (s *Server) UserLand(ctx context.Context, in *gitalypb.UserLandRequest) (*greturn resp, nil}- resp, err := s.landWithRetries(ctx, in, committer, precheck)+ resp, err := s.landWithRetries(ctx, in, committer, precheck, pre)if err != nil {return nil, err}@@ -121,7 +133,7 @@ func (s *Server) landPrecheckInQuarantine(ctx context.Context, in *gitalypb.User// On MVCC repositories, the attempt runs against a snapshot that is pinned for the whole RPC.// Retrying within the RPC would read the same tip again, so the conflict is reported to the// client instead and the client has to retry the landing.-func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {+func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck, pre *landPreResolution) (*gitalypb.UserLandResponse, error) {snapshotPinned := mvcc.CacheFromContext(ctx) != nilmaxAttempts := int(in.GetMaxAttempts())@@ -130,7 +142,7 @@ func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandReque}for attempt := 1; ; attempt++ {- resp, err := s.landAttempt(ctx, in, committer, precheck)+ resp, err := s.landAttempt(ctx, in, committer, precheck, pre)if err == nil {resp.Attempts = int32(attempt)return resp, nil@@ -194,7 +206,7 @@ func (s *Server) landAncestry(ctx context.Context, quarantineRepo *localrepo.Rep// landAttempt performs a single compare-and-swap attempt of UserLand. It returns a landConflict// if the branch was moved by a concurrent writer, in which case the caller retries.-func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {+func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck, pre *landPreResolution) (*gitalypb.UserLandResponse, error) {attempt, cleanup, err := s.newLandingAttempt(ctx, in.GetRepository(), landPublishLoose, ignoreLandPhase)if err != nil {return nil, err@@ -209,7 +221,7 @@ func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest,// The request is decided as a member of a batch is (landingAttempt.decide). The policy is not// evaluated here: the update runs with the identity of the request, so the hooks judge it.- req := &landBatchRequest{ctx: ctx, in: in, committer: committer, actor: landBatchActor(ctx, in), precheck: precheck}+ req := &landBatchRequest{ctx: ctx, in: in, committer: committer, actor: landBatchActor(ctx, in), precheck: precheck, preResolution: pre}step, err := attempt.decide(ctx, tip, req)if err != nil {return nil, errdiff --git a/internal/gitaly/service/operations/land_attempt.go b/internal/gitaly/service/operations/land_attempt.goindex 36e59b5..ce3422f 100644--- a/internal/gitaly/service/operations/land_attempt.go+++ b/internal/gitaly/service/operations/land_attempt.go@@ -224,10 +224,16 @@ func (a *landingAttempt) decide(ctx context.Context, tip git.ObjectID, req *landreturn landStep{}, err}- // The conflict is resolved into a merge commit. When that fails, the request gets the- // merge conflict it would have got without resolution, with the conflict package as a+ // The conflict is resolved into a merge commit from the verdicts made before the lane+ // (PLAN10 15.8); the resolver is never called here. Files without a verdict go back to+ // UserLand to be resolved out of the lane. When the land fails otherwise, the request gets+ // the merge conflict it would have got without resolution, with the conflict package as a// second detail when the server listed the conflicts.- res, resolveErr := a.server.resolveLandConflicts(ctx, in, a.repo, tip, commitID, req.committer)+ res, resolveErr := a.server.landApplyVerdicts(ctx, in, a.repo, tip, commitID, req.committer, req.preResolution)+ var need *landNeedsResolution+ if errors.As(resolveErr, &need) {+ return landStep{}, need+ }if resolveErr != nil {a.server.logger.WithError(resolveErr).WithField("branch", string(in.GetBranch())).Info("graft: conflict resolution of land failed")diff --git a/internal/gitaly/service/operations/land_batch.go b/internal/gitaly/service/operations/land_batch.goindex 603e5df..5d79d9f 100644--- a/internal/gitaly/service/operations/land_batch.go+++ b/internal/gitaly/service/operations/land_batch.go@@ -31,6 +31,9 @@ type landBatchRequest struct {actor string// precheck is the check of the request's resolution, run before the request entered its lane.precheck *landPrecheck+ // preResolution holds the verdicts the server made on the request's conflicting files before+ // it entered its lane (PLAN10 15.8); nil unless the request asked for resolution.+ preResolution *landPreResolution}// reference returns the branch the request lands on.diff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.goindex 47b8b1c..df4bb01 100644--- a/internal/gitaly/service/operations/land_batch_server.go+++ b/internal/gitaly/service/operations/land_batch_server.go@@ -78,7 +78,7 @@ func (s *Server) landBatchable(ctx context.Context, in *gitalypb.UserLandRequest// userLandBatched lands the request through its lane. The response is built from the outcome of// the batch that executed the request.-func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck) (*gitalypb.UserLandResponse, error) {+func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, precheck *landPrecheck, pre *landPreResolution) (*gitalypb.UserLandResponse, error) {repo := in.GetRepository()key := landlane.Key{Storage: repo.GetStorageName(),@@ -87,11 +87,12 @@ func (s *Server) userLandBatched(ctx context.Context, in *gitalypb.UserLandReque}req := &landBatchRequest{- ctx: ctx,- in: in,- committer: committer,- actor: landBatchActor(ctx, in),- precheck: precheck,+ ctx: ctx,+ in: in,+ committer: committer,+ actor: landBatchActor(ctx, in),+ precheck: precheck,+ preResolution: pre,}resp, err := s.landLanes.Submit(ctx, key, req)@@ -142,7 +143,7 @@ func (s *Server) executeLandBatch(ctx context.Context, _ landlane.Key, reqs []*lreturn stepper, cleanup, nil},fallback: func(req *landBatchRequest) (*gitalypb.UserLandResponse, error) {- return s.landWithRetries(req.ctx, req.in, req.committer, req.precheck)+ return s.landWithRetries(req.ctx, req.in, req.committer, req.precheck, req.preResolution)},giveUp: landBatchGiveUp,backoff: func(ctx context.Context, attempt int) error {diff --git a/internal/gitaly/service/operations/land_preresolve.go b/internal/gitaly/service/operations/land_preresolve.gonew file mode 100644index 0000000..aa4c752--- /dev/null+++ b/internal/gitaly/service/operations/land_preresolve.go@@ -0,0 +1,169 @@+package operations++import (+ "context"+ "errors"+ "fmt"+ "strings"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/graft/resolver"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// A land with resolve_conflicts is resolved out of the branch's lane (PLAN10 15.8): the resolver+// is called before the lane, and a file's verdict is kept by the file's sides, so that the lane,+// whatever its tip, applies every verdict whose file it finds unchanged and never calls the+// resolver itself.++// landFileKey identifies a conflicting file by what its verdict depends on: its path and its+// three sides, each by object ID and mode (empty for an absent side).+type landFileKey struct {+ path string+ baseOID, oursOID, theirsOID git.ObjectID+ baseMode, oursMode, theirsMode string+}++// landKeyOf returns the key of a conflicting file.+func landKeyOf(c landFileConflict) landFileKey {+ k := landFileKey{path: c.path}+ if c.base != nil {+ k.baseOID, k.baseMode = c.base.oid, c.base.mode+ }+ if c.ours != nil {+ k.oursOID, k.oursMode = c.ours.oid, c.ours.mode+ }+ if c.theirs != nil {+ k.theirsOID, k.theirsMode = c.theirs.oid, c.theirs.mode+ }+ return k+}++// landVerdict is a landable verdict on a file: the resolution the resolver gave it, and the name+// of that resolver.+type landVerdict struct {+ file resolver.ResponseFile+ resolver string+}++// landVerdicts are the landable verdicts of a land, by file key.+type landVerdicts map[landFileKey]landVerdict++// landPreResolution is what the pre-resolution of a land hands to its lane.+type landPreResolution struct {+ verdicts landVerdicts+}++// landNeedsResolution is the answer of a lane that met conflicting files without a verdict: new+// files, or files whose sides changed since they were resolved. UserLand resolves them out of the+// lane and lands again.+type landNeedsResolution struct {+ tip, commit git.ObjectID+ paths []string+}++func (e *landNeedsResolution) Error() string {+ return fmt.Sprintf("conflicting files to resolve at tip %s: %s", e.tip, strings.Join(e.paths, ", "))+}++// keptChanging is the merge conflict of a land whose files kept changing until it gave up, when+// says when.+func (e *landNeedsResolution) keptChanging(when string) error {+ paths := make([][]byte, 0, len(e.paths))+ for _, p := range e.paths {+ paths = append(paths, []byte(p))+ }+ return landMergeConflictError(e.tip, e.commit, paths,+ fmt.Errorf("files kept changing while the land was resolved, %s: %s", when, strings.Join(e.paths, ", ")))+}++// landPreResolve resolves, out of the lane and in a quarantine it discards, the conflicting files+// of the land at the branch's current tip that have no verdict in pre yet. It returns nil when the+// lane can take the land: the verdicts are in pre, or there is nothing to resolve (no branch, the+// change already landed, a clean merge: the lane answers those). Otherwise it returns the error of+// the land: the merge conflict with the package (the proposals kept), or the policy's refusal.+func (s *Server) landPreResolve(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature, pre *landPreResolution) error {+ // The lane resolves only the merge conflict of a rebase or a merge: a fast-forward-only land+ // that cannot fast-forward is refused without resolution.+ if in.GetStrategy() == gitalypb.UserLandRequest_STRATEGY_FF_ONLY {+ return nil+ }+ _, repo, cleanup, err := s.quarantinedRepo(ctx, in.GetRepository())+ if err != nil {+ return err+ }+ defer cleanup()++ tip, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())+ if err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ return nil+ }+ return fmt.Errorf("reading the branch: %w", err)+ }+ commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}"))+ if err != nil {+ return nil // the lane refuses an invalid change+ }+ if landed, err := repo.IsAncestor(ctx, commitID.Revision(), tip.Revision()); err != nil {+ return fmt.Errorf("checking whether the change already landed: %w", err)+ } else if landed {+ return nil+ }++ l, _, err := s.landResolveAt(ctx, in, repo, tip, commitID, committer, pre.verdicts)+ switch {+ case err == nil, errors.Is(err, errLandNoConflicts):+ return nil+ }+ files := l.files[:min(len(l.files), landMaxPackageFiles)]+ paths := make([][]byte, 0, len(files))+ for _, f := range files {+ paths = append(paths, []byte(f.conflict.path))+ }+ s.logger.WithError(err).WithField("branch", string(in.GetBranch())).Info("graft: conflict resolution of land failed")+ return landWithPackage(ctx, landMergeConflictError(tip, commitID, paths, err), err)+}++// landResolving lands a request with resolve_conflicts (PLAN10 15.8): it pre-resolves out of the+// lane and lands; when the lane meets files without a verdict, it resolves only those, out of the+// lane, and lands again. The rounds are bounded by the resolver's timeout for the whole land, and+// by resolve_rounds, a safety cap. A round is lost only when another land changed one of the+// files, so the lands that contend for a file all land, one per round; a land that keeps losing+// to new arrivals fails at its deadline with the merge conflict, naming the files that kept+// changing.+func (s *Server) landResolving(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature) (*gitalypb.UserLandResponse, error) {+ timeout, err := s.resolveCfg.TimeoutOrDefault()+ if err != nil {+ return nil, fmt.Errorf("resolver timeout: %w", err)+ }+ resolveCtx, cancel := context.WithTimeout(ctx, timeout)+ defer cancel()++ pre := &landPreResolution{verdicts: landVerdicts{}}+ var need *landNeedsResolution+ // deadline reports whether the land ran out of the resolver's timeout, and not its caller.+ deadline := func() bool {+ return ctx.Err() == nil && errors.Is(resolveCtx.Err(), context.DeadlineExceeded)+ }+ until := fmt.Sprintf("until the deadline of %s", timeout)+ rounds := s.resolveCfg.ResolveRoundsOrDefault()+ for round := 1; round <= rounds; round++ {+ if err := s.landPreResolve(resolveCtx, in, committer, pre); err != nil {+ // A land that lost a round to its files changing, and then ran out of its deadline+ // while it resolved them again, failed because they kept changing.+ if need != nil && deadline() {+ return nil, need.keptChanging(until)+ }+ return nil, err+ }+ resp, err := s.landOnce(ctx, in, committer, nil, pre)+ if !errors.As(err, &need) {+ return resp, err+ }+ if deadline() {+ return nil, need.keptChanging(until)+ }+ }+ return nil, need.keptChanging(fmt.Sprintf("after %d rounds", rounds))+}diff --git a/internal/gitaly/service/operations/land_preresolve_test.go b/internal/gitaly/service/operations/land_preresolve_test.gonew file mode 100755index 0000000..52928b8--- /dev/null+++ b/internal/gitaly/service/operations/land_preresolve_test.go@@ -0,0 +1,564 @@+package operations++import (+ "encoding/json"+ "errors"+ "fmt"+ "io/fs"+ "os"+ "path/filepath"+ "strconv"+ "strings"+ "sync"+ "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/graft/resolver"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// The tests of the resolution out of the lane (PLAN10 15.8): the resolver is called before the+// lane, a file's verdict is kept by its sides, and the lane applies the verdicts whose files it+// finds unchanged without ever calling the resolver.++// answerByCall is the body of a resolver script (resolverScript) that answers its n-th request with+// dir/answer-n.json when the test wrote one (writeAnswerN), and with dir/answer.json otherwise, and+// that moves the branch to the commit in dir/move-n, when the test wrote one, before it answers.+// The paths of git and of the repository are in dir/git and dir/repo (writeMoveFiles).+var answerByCall = fmt.Sprintf(`if [ -f "$DIR/move-$N" ]; then+ "$(cat "$DIR/git")" -C "$(cat "$DIR/repo")" update-ref refs/heads/%s "$(cat "$DIR/move-$N")"+fi+if [ -f "$DIR/answer-$N.json" ]; then cat "$DIR/answer-$N.json"; exit 0; fi`, git.DefaultBranch)++// writeAnswerN sets the answer the resolver script of dir gives to its n-th request.+func writeAnswerN(t *testing.T, dir string, n int, resp resolver.Response) {+ t.Helper()++ raw, err := json.Marshal(resp)+ require.NoError(t, err)+ require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("answer-%d.json", n)), raw, 0o600))+}++// writeMoves makes the resolver script of dir move the branch of fx to moves[n-1] while it answers+// its n-th request.+func writeMoves(t *testing.T, dir string, fx resolveFixture, moves ...git.ObjectID) {+ t.Helper()++ require.NoError(t, os.WriteFile(filepath.Join(dir, "git"), []byte(gitBinary(t, fx.cfg)), 0o600))+ require.NoError(t, os.WriteFile(filepath.Join(dir, "repo"), []byte(fx.repoPath), 0o600))+ for i, move := range moves {+ require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("move-%d", i+1)), []byte(move.String()), 0o600))+ }+}++// answerOf resolves each of paths to "merged-<path>".+func answerOf(paths ...string) resolver.Response {+ resp := resolver.Response{Version: resolver.ProtocolVersion, Resolver: "graft-resolve/fake"}+ for _, p := range paths {+ resp.Files = append(resp.Files, resolver.ResponseFile{+ Path: p, Action: resolver.ActionWrite, Mode: resolver.ModeRegular, Content: []byte("merged-" + p + "\n"),+ })+ }+ return resp+}++// requestPaths returns the paths of the files of the n-th request the resolver script of dir got.+func requestPaths(t *testing.T, dir string, n int) []string {+ t.Helper()++ var paths []string+ for _, f := range resolverRequest(t, dir, n).Files {+ paths = append(paths, f.Path)+ }+ return paths+}++// concurrentCallsBody is the body of a resolver script (resolverScript) that is called at once by+// several lands: it appends a line per call, which resolverCalls' counter, read, incremented and+// written, would lose when calls overlap. Read the count with concurrentCalls.+const concurrentCallsBody = `echo x >> "$DIR/lines"`++// concurrentCalls returns the number of calls counted by concurrentCallsBody.+func concurrentCalls(t *testing.T, dir string) int {+ t.Helper()++ raw, err := os.ReadFile(filepath.Join(dir, "lines"))+ if errors.Is(err, fs.ErrNotExist) {+ return 0+ }+ require.NoError(t, err)+ return len(raw) / 2+}++// countIn returns the number in the file path, 0 when there is none yet.+func countIn(t *testing.T, path string) int {+ t.Helper()++ raw, err := os.ReadFile(path)+ if err != nil {+ return 0+ }+ n, err := strconv.Atoi(strings.TrimSpace(string(raw)))+ if err != nil {+ return 0 // the script is writing it+ }+ return n+}++// entries returns regular files with the given contents, path and content alternating.+func entries(pathsAndContents ...string) []gittest.TreeEntry {+ var out []gittest.TreeEntry+ for i := 0; i < len(pathsAndContents); i += 2 {+ out = append(out, gittest.TreeEntry{Mode: "100644", Path: pathsAndContents[i], Content: pathsAndContents[i+1]})+ }+ return out+}++// landResult is the outcome of a UserLand call run in a goroutine.+type landResult struct {+ resp *gitalypb.UserLandResponse+ err error+}++// landAsync runs UserLand for commit in a goroutine and returns its outcome's channel.+func landAsync(fx resolveFixture, commit git.ObjectID, message string) <-chan landResult {+ req := fx.request(message)+ req.CommitId = commit.String()+ done := make(chan landResult, 1)+ go func() {+ resp, err := fx.client.UserLand(fx.ctx, req)+ done <- landResult{resp: resp, err: err}+ }()+ return done+}++// requireRunning asserts that the land of done has not ended.+func requireRunning(t *testing.T, done <-chan landResult, what string) {+ t.Helper()++ select {+ case r := <-done:+ require.FailNow(t, what+" ended", "response %v, error %v", r.resp, r.err)+ default:+ }+}++// requireLanded waits for the land of done and asserts that it landed.+func requireLanded(t *testing.T, done <-chan landResult) *gitalypb.UserLandResponse {+ t.Helper()++ select {+ case r := <-done:+ require.NoError(t, r.err)+ return r.resp+ case <-time.After(60 * time.Second):+ require.FailNow(t, "the land did not end")+ return nil+ }+}++// heldLane is a standalone server whose lane for the branch is held. Branch main and the commits+// a, b and c conflict on the file of their name each. The land of fx.commit (commit a) resolves a+// and then waits in its verification, which runs in the lane, until release is called; first is its+// outcome. The resolver answers the land of a (its first request), and the test sets the answers of+// the next requests.+type heldLane struct {+ fx resolveFixture+ dir string+ vdir string+ b, c git.ObjectID+ first <-chan landResult+ release func()+}++func newHeldLane(t *testing.T) heldLane {+ t.Helper()++ dir, argv := resolverScript(t, answerByCall)+ vdir := t.TempDir()+ // The first verification, the land of a's, waits for the release, at most a minute so that a+ // failed test never hangs; the others pass at once.+ verify := verifyScript(t, fmt.Sprintf(`V='%s'+N=$(cat "$V/calls" 2>/dev/null || echo 0)+N=$((N+1))+echo "$N" > "$V/calls"+if [ "$N" = 1 ]; then+ i=0+ while [ ! -f "$V/release" ] && [ "$i" -lt 600 ]; do sleep 0.1; i=$((i+1)); done+fi+exit 0`, vdir))+ graft := batchingGraft()+ graft.Land.Resolver = withVerify(enabledResolver(argv), verify, "")++ fx := newResolveFixtureIn(t, graft,+ entries("a", "base\n", "b", "base\n", "c", "base\n"),+ entries("a", "ours\n", "b", "ours\n", "c", "ours\n"),+ entries("a", "theirs-a\n", "b", "base\n", "c", "base\n"),+ )+ b := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base),+ gittest.WithTreeEntries(entries("a", "base\n", "b", "theirs-b\n", "c", "base\n")...))+ c := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base),+ gittest.WithTreeEntries(entries("a", "base\n", "b", "base\n", "c", "theirs-c\n")...))+ writeAnswerN(t, dir, 1, answerOf("a"))++ var once sync.Once+ release := func() {+ once.Do(func() { require.NoError(t, os.WriteFile(filepath.Join(vdir, "release"), nil, 0o600)) })+ }+ t.Cleanup(release)++ first := landAsync(fx, fx.commit, "Land a")+ require.Eventually(t, func() bool { return countIn(t, filepath.Join(vdir, "calls")) == 1 },+ 30*time.Second, 10*time.Millisecond, "the land of a never reached its verification")++ return heldLane{fx: fx, dir: dir, vdir: vdir, b: b, c: c, first: first, release: release}+}++// verifications returns the number of verifications run so far.+func (h heldLane) verifications(t *testing.T) int {+ return countIn(t, filepath.Join(h.vdir, "calls"))+}++func TestUserLand_resolvesOutOfTheLane(t *testing.T) {+ t.Parallel()++ t.Run("the resolver answers while another land holds the lane", func(t *testing.T) {+ t.Parallel()++ h := newHeldLane(t)+ writeAnswerN(t, h.dir, 2, answerOf("b"))++ second := landAsync(h.fx, h.b, "Land b")+ require.Eventually(t, func() bool { return countIn(t, filepath.Join(h.dir, "calls")) == 2 },+ 30*time.Second, 10*time.Millisecond, "b was not resolved while the lane was held")+ requireRunning(t, h.first, "the land of a")+ require.Equal(t, 1, h.verifications(t))++ h.release()+ first := requireLanded(t, h.first)+ resp := requireLanded(t, second)++ // b's verdict, made at the old tip, holds at a's merge, which left b alone.+ require.Equal(t, 2, resolverCalls(t, h.dir))+ require.Equal(t, first.GetLandedCommitId(), resp.GetOldTip())+ landed := git.ObjectID(resp.GetLandedCommitId())+ commit, err := h.fx.repo.ReadCommit(h.fx.ctx, landed.Revision())+ require.NoError(t, err)+ require.Equal(t, []string{first.GetLandedCommitId(), h.b.String()}, commit.GetParentIds())+ require.Equal(t, "merged-a\n", h.fx.blobAt(t, landed, "a"))+ require.Equal(t, "merged-b\n", h.fx.blobAt(t, landed, "b"))+ require.Equal(t, "ours\n", h.fx.blobAt(t, landed, "c"))+ })++ t.Run("an answer that does not land never enters the lane", func(t *testing.T) {+ t.Parallel()++ h := newHeldLane(t)+ proposal := answerOf("b")+ proposal.Resolver = "graft-resolve/llm"+ writeAnswerN(t, h.dir, 2, proposal)++ // The land of b returns while the lane is still held: it never queued behind a.+ req := h.fx.request("Land b")+ req.CommitId = h.b.String()+ _, err := h.fx.client.UserLand(h.fx.ctx, req)+ requireMergeConflict(t, err)+ require.Equal(t, [][]byte{[]byte("b")}, landErrorDetail(t, err).GetMergeConflict().GetConflictingFiles())+ requireRunning(t, h.first, "the land of a")+ require.Equal(t, 1, h.verifications(t))++ // The proposal was kept although the lane was busy with a batch: it was written before the+ // lane, on the published tip, never on a tip that exists only in a batch's quarantine+ // (review of 6090d5b, item 4).+ pkg := landConflictDetail(err)+ require.NotNil(t, pkg)+ require.NotEmpty(t, pkg.GetProposalRef())+ kept, err := h.fx.repo.ResolveRevision(h.fx.ctx, git.Revision(pkg.GetProposalRef()))+ require.NoError(t, err)+ commit, err := h.fx.repo.ReadCommit(h.fx.ctx, kept.Revision())+ require.NoError(t, err)+ require.Equal(t, []string{h.fx.tip.String(), h.b.String()}, commit.GetParentIds())+ require.Equal(t, "merged-b\n", h.fx.blobAt(t, kept, "b"))++ h.release()+ requireLanded(t, h.first)+ require.Equal(t, 2, resolverCalls(t, h.dir))+ })++ t.Run("verdicts made before a batch are applied in it, each on the one before", func(t *testing.T) {+ t.Parallel()++ h := newHeldLane(t)+ writeAnswerN(t, h.dir, 2, answerOf("b"))+ writeAnswerN(t, h.dir, 3, answerOf("c"))++ // b and c are resolved one after the other while a holds the lane, and wait in the lane+ // together: they land in one batch once a has landed.+ second := landAsync(h.fx, h.b, "Land b")+ require.Eventually(t, func() bool { return countIn(t, filepath.Join(h.dir, "calls")) == 2 }, 30*time.Second, 10*time.Millisecond)+ third := landAsync(h.fx, h.c, "Land c")+ require.Eventually(t, func() bool { return countIn(t, filepath.Join(h.dir, "calls")) == 3 }, 30*time.Second, 10*time.Millisecond)+ // The two requests submit to the lane right after their answers; the lane is held for as+ // long as the test wants, so this wait only lets them queue.+ time.Sleep(500 * time.Millisecond)+ requireRunning(t, h.first, "the land of a")++ h.release()+ first := requireLanded(t, h.first)+ respB := requireLanded(t, second)+ respC := requireLanded(t, third)++ require.Equal(t, 3, resolverCalls(t, h.dir), "no verdict was made again")+ require.Equal(t, int32(2), respB.GetBatchSize())+ require.Equal(t, int32(2), respC.GetBatchSize())+ earlier, later, laterCommit := respB, respC, h.c+ if respB.GetBatchPosition() == 2 {+ earlier, later, laterCommit = respC, respB, h.b+ }+ require.Equal(t, int32(1), earlier.GetBatchPosition())+ require.Equal(t, int32(2), later.GetBatchPosition())+ require.Equal(t, first.GetLandedCommitId(), earlier.GetOldTip())++ landed := git.ObjectID(later.GetLandedCommitId())+ requireBranchAt(t, h.fx.cfg, h.fx.repoPath, git.DefaultBranch, landed)+ commit, err := h.fx.repo.ReadCommit(h.fx.ctx, landed.Revision())+ require.NoError(t, err)+ require.Equal(t, []string{earlier.GetLandedCommitId(), laterCommit.String()}, commit.GetParentIds())+ for _, p := range []string{"a", "b", "c"} {+ require.Equal(t, "merged-"+p+"\n", h.fx.blobAt(t, landed, p), p)+ }+ })+}++func TestUserLand_resolveRounds(t *testing.T) {+ t.Parallel()++ t.Run("only the conflicting file that changed is resolved again", func(t *testing.T) {+ t.Parallel()++ // While a and b are resolved, a writer changes a: the verdict on b holds at the writer,+ // the one on a does not, and only a is sent again.+ dir, argv := resolverScript(t, answerByCall)+ writeAnswer(t, dir, validAnswer())+ second := answerOf("a")+ second.Files[0].Content = []byte("merged-a-again\n")+ writeAnswerN(t, dir, 2, second)+ fx := newResolveFixture(t, enabledResolver(argv))+ writer := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.tip),+ gittest.WithTreeEntries(entries("a", "ours-again\n", "b", "ours\n", "c", "c\n")...))+ writeMoves(t, dir, fx, writer)++ resp, err := fx.client.UserLand(fx.ctx, fx.request("Land agent work"))+ require.NoError(t, err)+ require.Equal(t, 2, resolverCalls(t, dir))+ require.Equal(t, []string{"a", "b"}, requestPaths(t, dir, 1))+ require.Equal(t, []string{"a"}, requestPaths(t, dir, 2))+ require.Equal(t, "ours-again\n", string(resolverRequest(t, dir, 2).Files[0].Ours.Content))+ require.Equal(t, writer.String(), resp.GetOldTip())+ require.Equal(t, "graft-resolve/fake", resp.GetResolver())+ require.Equal(t, [][]byte{[]byte("a"), []byte("b")}, resp.GetResolvedPaths())++ landed := git.ObjectID(resp.GetLandedCommitId())+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, landed)+ commit, err := fx.repo.ReadCommit(fx.ctx, landed.Revision())+ require.NoError(t, err)+ require.Equal(t, []string{writer.String(), fx.commit.String()}, commit.GetParentIds())+ require.Equal(t, "merged-a-again\n", fx.blobAt(t, landed, "a"))+ require.Equal(t, "merged-b\n", fx.blobAt(t, landed, "b"))+ })++ t.Run("files that keep changing fail after resolve_rounds, by name", func(t *testing.T) {+ t.Parallel()++ dir, argv := resolverScript(t, answerByCall)+ writeAnswer(t, dir, validAnswer())+ writeAnswerN(t, dir, 2, answerOf("a"))+ rcfg := enabledResolver(argv)+ rcfg.ResolveRounds = 2+ fx := newResolveFixture(t, rcfg)+ w1 := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.tip),+ gittest.WithTreeEntries(entries("a", "ours-1\n", "b", "ours\n", "c", "c\n")...))+ w2 := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(w1),+ gittest.WithTreeEntries(entries("a", "ours-2\n", "b", "ours\n", "c", "c\n")...))+ writeMoves(t, dir, fx, w1, w2)++ _, err := fx.client.UserLand(fx.ctx, fx.request("Land"))+ requireMergeConflict(t, err)+ require.ErrorContains(t, err, "files kept changing while the land was resolved, after 2 rounds: a")+ conflict := landErrorDetail(t, err).GetMergeConflict()+ require.Equal(t, [][]byte{[]byte("a")}, conflict.GetConflictingFiles())+ require.Equal(t, []string{w2.String(), fx.commit.String()}, conflict.GetConflictingCommitIds())+ require.Equal(t, 2, resolverCalls(t, dir))+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, w2)+ })++ t.Run("files that keep changing fail at the deadline, by name", func(t *testing.T) {+ t.Parallel()++ // A writer changes a and b while every round is resolved, more often than the land can+ // keep up with: the land fails at the resolver's deadline, however many rounds the cap+ // would allow, and says why.+ dir, argv := resolverScript(t, "sleep 0.2\n"+answerByCall)+ writeAnswer(t, dir, validAnswer())+ rcfg := enabledResolver(argv)+ rcfg.Timeout = "3s"+ rcfg.ResolveRounds = 1000+ fx := newResolveFixture(t, rcfg)+ var writers []git.ObjectID+ parent := fx.tip+ for i := 0; i < 100; i++ {+ parent = gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(parent),+ gittest.WithTreeEntries(entries("a", fmt.Sprintf("ours-%d\n", i), "b", fmt.Sprintf("ours-%d\n", i), "c", "c\n")...))+ writers = append(writers, parent)+ }+ writeMoves(t, dir, fx, writers...)++ start := time.Now()+ _, err := fx.client.UserLand(fx.ctx, fx.request("Land"))+ requireMergeConflict(t, err)+ require.ErrorContains(t, err, "files kept changing while the land was resolved, until the deadline of 3s: a, b")+ require.Less(t, time.Since(start), 30*time.Second)+ require.GreaterOrEqual(t, resolverCalls(t, dir), 2)+ require.Less(t, resolverCalls(t, dir), len(writers), "the deadline ended it, not the writers")+ })++ t.Run("under churn and a short deadline every land ends, landed or with its reason", func(t *testing.T) {+ t.Parallel()++ // More lands contend for a than the resolver's deadline lets land: each one either lands+ // or fails with a merge conflict that says why, and none hangs.+ const lands = 10+ dir, argv := resolverScript(t, "sleep 0.3\n"+concurrentCallsBody)+ writeAnswer(t, dir, answerOf("a"))+ rcfg := enabledResolver(argv)+ rcfg.Timeout = "2s"+ graft := batchingGraft()+ graft.Land.Resolver = rcfg+ fx := newResolveFixtureIn(t, graft,+ entries("a", "base\n"),+ entries("a", "ours\n"),+ entries("a", "theirs-0\n", "f0", "0\n"),+ )+ commits := []git.ObjectID{fx.commit}+ for i := 1; i < lands; i++ {+ commits = append(commits, gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base),+ gittest.WithTreeEntries(entries("a", fmt.Sprintf("theirs-%d\n", i), fmt.Sprintf("f%d", i), fmt.Sprintf("%d\n", i))...)))+ }++ var dones []<-chan landResult+ for i, commit := range commits {+ dones = append(dones, landAsync(fx, commit, fmt.Sprintf("Land %d", i)))+ }+ landed, timedOut := 0, 0+ for i, done := range dones {+ select {+ case r := <-done:+ if r.err == nil {+ landed+++ continue+ }+ requireMergeConflict(t, r.err)+ require.True(t, strings.Contains(r.err.Error(), "files kept changing") || strings.Contains(r.err.Error(), "deadline"),+ "land %d fails with its reason: %v", i, r.err)+ timedOut+++ case <-time.After(60 * time.Second):+ require.FailNow(t, "a land hangs", "land %d", i)+ }+ }+ require.Equal(t, lands, landed+timedOut)+ require.Positive(t, landed)+ t.Logf("%d lands: %d landed, %d ended at the deadline, %d resolutions", lands, landed, timedOut, concurrentCalls(t, dir))+ })++ t.Run("lands that move the branch under each other all land", func(t *testing.T) {+ t.Parallel()++ // Every change conflicts with the branch on a and adds a file of its own, and they all land+ // at once through the lane: each that lands changes a under the others, which resolve it+ // again. A round fails only when another land landed, so resolve_rounds of the number of+ // lands lets every one land.+ const lands = 6+ dir, argv := resolverScript(t, concurrentCallsBody)+ writeAnswer(t, dir, answerOf("a"))+ rcfg := enabledResolver(argv)+ rcfg.ResolveRounds = lands+ graft := batchingGraft()+ graft.Land.Resolver = rcfg+ fx := newResolveFixtureIn(t, graft,+ entries("a", "base\n"),+ entries("a", "ours\n"),+ entries("a", "theirs-0\n", "f0", "0\n"),+ )+ commits := []git.ObjectID{fx.commit}+ for i := 1; i < lands; i++ {+ commits = append(commits, gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base),+ gittest.WithTreeEntries(entries("a", fmt.Sprintf("theirs-%d\n", i), fmt.Sprintf("f%d", i), fmt.Sprintf("%d\n", i))...)))+ }++ var dones []<-chan landResult+ for i, commit := range commits {+ dones = append(dones, landAsync(fx, commit, fmt.Sprintf("Land %d", i)))+ }+ for _, done := range dones {+ requireLanded(t, done)+ }++ tip, err := fx.repo.ResolveRevision(fx.ctx, git.NewReferenceNameFromBranchName(git.DefaultBranch).Revision())+ require.NoError(t, err)+ require.Equal(t, "merged-a\n", fx.blobAt(t, tip, "a"))+ for i := range commits {+ require.Equal(t, fmt.Sprintf("%d\n", i), fx.blobAt(t, tip, fmt.Sprintf("f%d", i)))+ }+ calls := concurrentCalls(t, dir)+ require.GreaterOrEqual(t, calls, lands, "every land is resolved at least once")+ t.Logf("%d lands took %d resolutions", lands, calls)+ })+}++// TestLandApplyVerdicts_neverCallsTheResolver: the lane's part of a resolution writes the verdicts+// made before it and nothing else; a file without a verdict goes back to UserLand by name.+func TestLandApplyVerdicts_neverCallsTheResolver(t *testing.T) {+ t.Parallel()++ dir, argv := resolverScript(t, "")+ writeAnswer(t, dir, validAnswer())+ rcfg := enabledResolver(argv)+ fx := newResolveFixture(t, rcfg)+ s := fx.server(t, rcfg)+ in := fx.request("Land agent work")++ verdicts := landVerdicts{}+ _, _, err := s.landResolveAt(fx.ctx, in, fx.repo, fx.tip, fx.commit, fx.committer(), verdicts)+ require.NoError(t, err)+ require.Equal(t, 1, resolverCalls(t, dir))+ require.Len(t, verdicts, 2)+ pre := &landPreResolution{verdicts: verdicts}++ // At the tip they were made at, the verdicts are written.+ res, err := s.landApplyVerdicts(fx.ctx, in, fx.repo, fx.tip, fx.commit, fx.committer(), pre)+ require.NoError(t, err)+ require.Equal(t, "graft-resolve/fake", res.resolver)+ require.Equal(t, [][]byte{[]byte("a"), []byte("b")}, res.resolvedPaths)+ require.Equal(t, "merged-a\n", fx.blobAt(t, res.commit, "a"))+ require.Equal(t, "merged-b\n", fx.blobAt(t, res.commit, "b"))++ // At a tip that changed a, a has no verdict.+ writer := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.tip),+ gittest.WithTreeEntries(entries("a", "ours-again\n", "b", "ours\n", "c", "c\n")...))+ _, err = s.landApplyVerdicts(fx.ctx, in, fx.repo, writer, fx.commit, fx.committer(), pre)+ var need *landNeedsResolution+ require.ErrorAs(t, err, &need)+ require.Equal(t, []string{"a"}, need.paths)+ require.Equal(t, writer, need.tip)++ // Without a pre-resolution, every file is.+ _, err = s.landApplyVerdicts(fx.ctx, in, fx.repo, fx.tip, fx.commit, fx.committer(), nil)+ require.ErrorAs(t, err, &need)+ require.Equal(t, []string{"a", "b"}, need.paths)++ require.Equal(t, 1, resolverCalls(t, dir), "the lane never calls the resolver")+}diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.goindex dd585b3..a9deac4 100644--- a/internal/gitaly/service/operations/land_resolve.go+++ b/internal/gitaly/service/operations/land_resolve.go@@ -7,6 +7,7 @@ import ("fmt""path""slices"+ "sort""strconv""strings""unicode/utf8"@@ -75,35 +76,37 @@ type landFileConflict struct {theirs *landBlob}-// resolveLandConflicts lands commitID onto tip by resolving the files that conflict between the-// two. The result is a merge commit with parents [tip, commitID].-//-// Every conflicting file is decided on its own. A file that may not or cannot be sent (its path,-// its sides, or the never-send rules of resolver.SendRules) goes to the agent; the others go to-// the resolver in one request. Nothing lands partially: unless every file is resolved, the land-// fails with a *landUnresolvedError, whose conflict package says what happened to each file. A-// request the server does not attempt at all (resolution disabled, the actor not allowed, too many-// files, a criss-cross history) also gets a package, with the reason. Any other error means the-// conflicts could not even be listed, and the caller reports the merge conflict alone.-func (s *Server) resolveLandConflicts(+// landResolveAt resolves, against tip, the conflicting files of the land that have no verdict in+// verdicts yet (PLAN10 15.8). It runs out of the lane: the policy first (an agent that may not+// update the branch gets nothing resolved), the listing, the classification of every file, one+// resolver request for the files to resolve, taken with a resolve slot, and the validation; then+// decideLand over every file, the verdicts reused included. A request carries only what a file's+// verdict may depend on: the sides, the path and modes, the change's message, the configuration;+// not the tip, its merge base or its message, so that a verdict can be reused at another tip+// (15.8). When the land is landable, the new verdicts are added to verdicts, and the listing and+// the answer to write are returned. Otherwise the answer is kept as proposals (14) and the land+// fails with a *landUnresolvedError, whose package says what happened to each file; a request the+// server does not attempt at all also gets a package, with the reason. Any other error means the+// conflicts could not be listed, and the caller reports the merge conflict alone.+func (s *Server) landResolveAt(ctx context.Context,in *gitalypb.UserLandRequest,repo *localrepo.Repo,tip, commitID git.ObjectID,committer git.Signature,-) (landResolution, error) {+ verdicts landVerdicts,+) (landListing, *resolver.Response, error) {// An agent that may not update the branch gets nothing resolved, and no proposal is kept for// it: the policy decides on the actor and the reference alone, so it is checked first.reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))if err := hook.CheckAgentPolicy(ctx, s.graft, fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {- return landResolution{}, &landPolicyDeniedError{err: err}+ return landListing{}, nil, &landPolicyDeniedError{err: err}}l, err := landListConflicts(ctx, repo, tip, commitID, s.resolveCfg.MaxFileBytesOrDefault())if err != nil {- return landResolution{}, err+ return l, nil, err}- base, hasBase, conflicts, files := l.base, l.hasBase, l.conflicts, l.files- tipMessage, commitMessage := l.tipMessage, l.commitMessage+ base, conflicts, files := l.base, l.conflicts, l.files// max_files bounds the request to the resolver only: the package lists every conflicting file.maxFiles := s.resolveCfg.MaxFilesOrDefault()notAttempted := func(reason string) error { return l.notAttempted(reason, nil) }@@ -115,28 +118,38 @@ func (s *Server) resolveLandConflicts(actor := identity.Actor(ctx, "")switch {case len(files) == 0:- return landResolution{}, notAttempted(landNoConflictingFiles)+ return l, nil, 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))+ return l, nil, 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")+ return l, nil, notAttempted("conflict resolution is not enabled on this server")case !s.conflictResolver.ActorAllowed(actor):- return landResolution{}, notAttempted(fmt.Sprintf("actor %q may not resolve conflicts", actor))+ return l, nil, notAttempted(fmt.Sprintf("actor %q may not resolve conflicts", actor))case criss > 1:- return landResolution{}, notAttempted(fmt.Sprintf("the change has %d merge bases (a criss-cross history), which are not resolved", criss))+ return l, nil, notAttempted(fmt.Sprintf("the change has %d merge bases (a criss-cross history), which are not resolved", criss))case len(conflicts) > maxFiles:- return landResolution{}, notAttempted(fmt.Sprintf("%d conflicting files exceed max_files %d", len(conflicts), maxFiles))+ return l, nil, 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, tip)); err != nil {- return landResolution{}, err+ return l, nil, err}+ // A file the classification sends and that has a verdict with its key is not resolved again.+ reused := map[string]resolver.ResponseFile{}+ var reusedNames []stringvar sent []resolver.Filefor _, f := range files {- if f.sent() {- sent = append(sent, landRequestFile(f))+ if !f.sent() {+ continue}+ if v, ok := verdicts[landKeyOf(f.conflict)]; ok {+ reused[f.conflict.path] = v.file+ reusedNames = append(reusedNames, v.resolver)+ f.outcome = gitalypb.LandConflictFile_OUTCOME_RESOLVED+ continue+ }+ sent = append(sent, landRequestFile(f))}// answer is the resolver's accepted answer, the only thing ever written. A refusal is never@@ -145,7 +158,6 @@ func (s *Server) resolveLandConflicts(// agent, who lands its own resolution (PLAN10 14).var answer *resolver.Responsevar resolveErr error- proposes := falseif len(sent) > 0 {req := resolver.Request{Version: resolver.ProtocolVersion,@@ -154,18 +166,22 @@ func (s *Server) resolveLandConflicts(RelativePath: in.GetRepository().GetRelativePath(),},Branch: string(in.GetBranch()),- Tip: tip.String(),Commit: commitID.String(),- Messages: resolver.Messages{Tip: tipMessage, Commit: commitMessage},+ Messages: resolver.Messages{Commit: l.commitMessage},Actor: actor,Files: sent,}- if hasBase {- req.MergeBase = base.String()- }- // The client applies the timeout, the request policy and the acceptance rules of the answer.- resp, err := s.conflictResolver.Resolve(ctx, req)+ // The client applies the timeout, the request policy and the acceptance rules of the+ // answer; the request waits for a resolve slot within the deadline of ctx.+ var resp *resolver.Response+ var err error+ if slot, slotErr := s.conflictResolver.AcquireResolveSlot(ctx); slotErr != nil {+ err = slotErr+ } else {+ resp, err = slot.Resolve(ctx, req)+ slot.Release()+ }resolveErr = errswitch {case err != nil:@@ -175,7 +191,7 @@ func (s *Server) resolveLandConflicts(landFailSent(files, gitalypb.LandConflictFile_OUTCOME_FAILED, fmt.Sprintf("the resolver's answer names an invalid resolver %q", resp.Resolver))default:answer = resp- proposes = landAnswerProposes(resp)+ proposes := landAnswerProposes(resp)// A resolver that names the llm strategy but marks no file is taken to have proposed// every file: the mark may be missing, never the other way round.flagged := landAnswerFlagsProposals(resp)@@ -199,27 +215,139 @@ func (s *Server) resolveLandConflicts(}}- landed := decideLand(answer, files)+ combined := landCombinedAnswer(answer, reused, reusedNames, len(sent) > 0)+ landed := decideLand(combined, files)// An answer that does not land, because a model made part of it or another file is left, is// kept for the agent: the files that rules resolved can be landed as they are. The proposals// are an aid and the package is the contract: when they cannot be kept, the package still goes// to the agent, each file of the answer saying that its proposal was not stored, and the// failure is logged and counted.- if answer != nil && !landed {- ref, err := s.landWriteProposals(ctx, in, l, answer, committer)+ if combined != nil && !landed {+ ref, err := s.landWriteProposals(ctx, in, l, combined, committer)if err != nil {landProposalWriteErrors.Inc()s.logger.WithError(err).WithField("branch", string(in.GetBranch())).ErrorContext(ctx, "graft: the resolver's answer could not be kept as proposals")- landProposalsNotStored(files, answer, err)+ landProposalsNotStored(files, combined, err)} else {l.proposalRef = ref}}if !landed {- return landResolution{}, l.unresolved("", landUnresolvedCause(files), resolveErr)+ return l, nil, l.unresolved("", landUnresolvedCause(files), resolveErr)+ }++ if answer != nil {+ for _, f := range files {+ for _, rf := range answer.Files {+ if rf.Path == f.conflict.path {+ verdicts[landKeyOf(f.conflict)] = landVerdict{file: rf, resolver: answer.Resolver}+ }+ }+ }+ }+ return l, combined, nil+}++// landCombinedAnswer is the answer of a land whose files are resolved in rounds: the files the+// resolver answered now and the verdicts reused, under the names of the resolvers that made them.+// It is nil when the resolver was asked and did not answer, or when there is nothing at all.+func landCombinedAnswer(answer *resolver.Response, reused map[string]resolver.ResponseFile, reusedNames []string, asked bool) *resolver.Response {+ if asked && answer == nil {+ return nil+ }+ if answer == nil && len(reused) == 0 {+ return nil+ }+ combined := &resolver.Response{Version: resolver.ProtocolVersion}+ var names []string+ if answer != nil {+ combined.Files = append(combined.Files, answer.Files...)+ combined.Explanation = answer.Explanation+ names = append(names, answer.Resolver)+ }+ paths := make([]string, 0, len(reused))+ for p := range reused {+ paths = append(paths, p)+ }+ sort.Strings(paths)+ for _, p := range paths {+ combined.Files = append(combined.Files, reused[p])+ }+ names = append(names, reusedNames...)+ combined.Resolver = landJoinResolverNames(names)+ return combined+}++// landJoinResolverNames joins the distinct resolver names of an answer made in rounds, within+// the bound of a resolver name: the names that fit, and "+N" for the others.+func landJoinResolverNames(names []string) string {+ seen := map[string]bool{}+ var distinct []string+ for _, n := range names {+ if !seen[n] {+ seen[n] = true+ distinct = append(distinct, n)+ }+ }+ joined := strings.Join(distinct, ",")+ if len(joined) <= maxResolverNameBytes {+ return joined+ }+ for kept := len(distinct) - 1; kept > 0; kept-- {+ cut := strings.Join(distinct[:kept], ",") + fmt.Sprintf(",+%d", len(distinct)-kept)+ if len(cut) <= maxResolverNameBytes {+ return cut+ }+ }+ return distinct[0][:min(len(distinct[0]), maxResolverNameBytes)]+}++// landApplyVerdicts lands, in the lane, the verdicts that the pre-resolution made out of it+// (PLAN10 15.8). It lists the conflicts at the lane's tip and classifies every file again (the+// classification reads the tip), and writes the merge commit only when every conflicting file is+// sent and has a verdict with its key; the resolver is never called. A file without one makes it+// return *landNeedsResolution, which UserLand resolves out of the lane. A clean merge of the tip+// and the change is errLandNoConflicts, as before.+func (s *Server) landApplyVerdicts(+ ctx context.Context,+ in *gitalypb.UserLandRequest,+ repo *localrepo.Repo,+ tip, commitID git.ObjectID,+ committer git.Signature,+ pre *landPreResolution,+) (landResolution, error) {+ l, err := landListConflicts(ctx, repo, tip, commitID, s.resolveCfg.MaxFileBytesOrDefault())+ if err != nil {+ return landResolution{}, err+ }+ var missing []string+ if pre == nil || s.conflictResolver == nil || l.bases > 1 {+ for _, f := range l.files {+ missing = append(missing, f.conflict.path)+ }+ } else {+ if err := s.landClassify(ctx, repo, l.files, l.base, tip, commitID, newLandClassifyCache(l.files, tip)); err != nil {+ return landResolution{}, err+ }+ for _, f := range l.files {+ if _, ok := pre.verdicts[landKeyOf(f.conflict)]; !ok || !f.sent() {+ missing = append(missing, f.conflict.path)+ }+ }+ }+ if len(missing) > 0 || len(l.files) == 0 {+ return landResolution{}, &landNeedsResolution{tip: tip, commit: commitID, paths: missing}}- res, err := s.landWriteResolution(ctx, in, repo, tip, commitID, l.mergedTree, committer, actor, answer, "")+ reused := map[string]resolver.ResponseFile{}+ var names []string+ for _, f := range l.files {+ v := pre.verdicts[landKeyOf(f.conflict)]+ reused[f.conflict.path] = v.file+ names = append(names, v.resolver)+ }+ answer := landCombinedAnswer(nil, reused, names, false)+ res, err := s.landWriteResolution(ctx, in, repo, tip, commitID, l.mergedTree, committer, identity.Actor(ctx, ""), answer, "")return res, l.verifyRejected(err)}diff --git a/internal/gitaly/service/operations/land_resolve_bench_test.go b/internal/gitaly/service/operations/land_resolve_bench_test.gonew file mode 100755index 0000000..caa2e9f--- /dev/null+++ b/internal/gitaly/service/operations/land_resolve_bench_test.go@@ -0,0 +1,166 @@+package operations++import (+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "sort"+ "strconv"+ "sync"+ "testing"+ "time"++ dto "github.com/prometheus/client_model/go"+ "github.com/stretchr/testify/require"++ "github.com/feder-cr/git-ai/internal/featureflag"+ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/gitaly/config"+ "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"+)++// TestLandResolveBench measures, under churn, what a land that the server resolves costs the+// lands around it (PLAN10 15.8): resolve lands that all conflict on one file land at once with+// plain lands that conflict with nothing, through the lanes of a standalone server, with a resolver+// that takes GRAFT_LAND_BENCH_RESOLVE_MS to answer. It reports the latency of both kinds of land+// and how long the lane was held (graft_land_batch_phase_seconds, phase total). It runs only with+// GRAFT_LAND_BENCH=1, alone, and builds on the trees before and after the resolution left the lane.+func TestLandResolveBench(t *testing.T) {+ if os.Getenv("GRAFT_LAND_BENCH") != "1" {+ t.Skip("set GRAFT_LAND_BENCH=1 to run the bench")+ }+ resolveLands := benchEnvInt(t, "GRAFT_LAND_BENCH_RESOLVE", 6)+ plainLands := benchEnvInt(t, "GRAFT_LAND_BENCH_PLAIN", 6)+ resolveDelay := time.Duration(benchEnvInt(t, "GRAFT_LAND_BENCH_RESOLVE_MS", 500)) * time.Millisecond++ dir := t.TempDir()+ script := filepath.Join(dir, "resolver.sh")+ answer, err := json.Marshal(answerOfPaths("a"))+ require.NoError(t, err)+ require.NoError(t, os.WriteFile(filepath.Join(dir, "answer.json"), answer, 0o600))+ require.NoError(t, os.WriteFile(script, []byte(fmt.Sprintf(`#!/bin/sh+DIR='%s'+cat > /dev/null+echo x >> "$DIR/calls"+sleep %.3f+cat "$DIR/answer.json"+`, dir, resolveDelay.Seconds())), 0o755))++ // resolve_rounds is set through JSON, which the tree before it ignores.+ var rcfg config.GraftResolver+ require.NoError(t, json.Unmarshal([]byte(fmt.Sprintf(`{"enabled":true,"command":[%q],"timeout":"10m","resolve_rounds":%d}`,+ script, resolveLands+1)), &rcfg))++ cfg := testcfg.Build(t)+ cfg.Graft = config.Graft{+ Standalone: true,+ Land: config.GraftLand{Batch: config.GraftLandBatch{Window: "20ms"}, Resolver: rcfg},+ }+ ctx, cfg, client := setupOperationsServiceWithCfg(t, testhelper.Context(t), cfg)+ ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.GPGSigning, true)+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ file := func(path, content string) gittest.TreeEntry {+ return gittest.TreeEntry{Mode: "100644", Path: path, Content: content}+ }+ base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(file("a", "base\n")))+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base), gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(file("a", "ours\n")))++ type land struct {+ commit git.ObjectID+ resolve bool+ }+ var lands []land+ for i := 0; i < resolveLands; i++ {+ lands = append(lands, land{resolve: true, commit: gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base),+ gittest.WithTreeEntries(file("a", fmt.Sprintf("theirs-%d\n", i)), file(fmt.Sprintf("r%d", i), "r\n")))})+ }+ for i := 0; i < plainLands; i++ {+ lands = append(lands, land{commit: gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base),+ gittest.WithTreeEntries(file("a", "base\n"), file(fmt.Sprintf("p%d", i), "p\n")))})+ }++ laneBefore := benchLaneSeconds(t)+ latencies := map[bool][]time.Duration{}+ failures := map[bool]int{}+ var mu sync.Mutex+ var wg sync.WaitGroup+ start := time.Now()+ for i, l := range lands {+ wg.Add(1)+ go func(i int, l land) {+ defer wg.Done()+ // The plain lands start once the resolve lands are on their way, as the churn of a busy+ // branch would.+ if !l.resolve {+ time.Sleep(50 * time.Millisecond)+ }+ began := time.Now()+ _, err := client.UserLand(ctx, &gitalypb.UserLandRequest{+ Repository: repo, User: gittest.TestUser, Branch: []byte(git.DefaultBranch),+ CommitId: l.commit.String(), ResolveConflicts: l.resolve, MergeMessage: []byte(fmt.Sprintf("Land %d", i)),+ })+ mu.Lock()+ defer mu.Unlock()+ if err != nil {+ failures[l.resolve]+++ t.Logf("land %d (resolve %v): %v", i, l.resolve, err)+ return+ }+ latencies[l.resolve] = append(latencies[l.resolve], time.Since(began))+ }(i, l)+ }+ wg.Wait()+ wall := time.Since(start)+ lane := benchLaneSeconds(t) - laneBefore+ raw, err := os.ReadFile(filepath.Join(dir, "calls"))+ require.NoError(t, err)+ calls := len(raw) / 2++ t.Logf("BENCH resolve_lands=%d plain_lands=%d resolve_delay=%s wall=%s lane_held=%.2fs resolver_calls=%d",+ resolveLands, plainLands, resolveDelay, wall.Round(time.Millisecond), lane, calls)+ for _, resolve := range []bool{true, false} {+ name := map[bool]string{true: "resolve", false: "plain"}[resolve]+ d := latencies[resolve]+ sort.Slice(d, func(i, j int) bool { return d[i] < d[j] })+ if len(d) == 0 {+ t.Logf("BENCH %s: landed 0, failed %d", name, failures[resolve])+ continue+ }+ t.Logf("BENCH %s: landed %d, failed %d, p50 %s, p95 %s, max %s", name, len(d), failures[resolve],+ d[len(d)/2].Round(time.Millisecond), d[(len(d)*95)/100].Round(time.Millisecond), d[len(d)-1].Round(time.Millisecond))+ }+}++// answerOfPaths resolves each of paths to "merged-<path>", by rules, so that it lands.+func answerOfPaths(paths ...string) resolver.Response {+ resp := resolver.Response{Version: resolver.ProtocolVersion, Resolver: "graft-resolve/bench"}+ for _, p := range paths {+ resp.Files = append(resp.Files, resolver.ResponseFile{+ Path: p, Action: resolver.ActionWrite, Mode: resolver.ModeRegular, Content: []byte("merged-" + p + "\n"),+ })+ }+ return resp+}++// benchLaneSeconds is the time the lanes of the process were held so far: the sum of the phase+// total of every executed batch.+func benchLaneSeconds(t *testing.T) float64 {+ var m dto.Metric+ require.NoError(t, landBatchPhaseSeconds.WithLabelValues(landBatchPhaseTotal).(interface{ Write(*dto.Metric) error }).Write(&m))+ return m.GetHistogram().GetSampleSum()+}++func benchEnvInt(t *testing.T, name string, fallback int) int {+ v := os.Getenv(name)+ if v == "" {+ return fallback+ }+ n, err := strconv.Atoi(v)+ require.NoError(t, err, name)+ return n+}diff --git a/internal/gitaly/service/operations/land_resolve_helper_test.go b/internal/gitaly/service/operations/land_resolve_helper_test.gonew file mode 100644index 0000000..4f6dc9a--- /dev/null+++ b/internal/gitaly/service/operations/land_resolve_helper_test.go@@ -0,0 +1,22 @@+package operations++import (+ "context"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/localrepo"+ "github.com/feder-cr/git-ai/internal/graft/identity"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// resolveLandConflicts resolves the conflicts of tip and commitID and writes the merge commit when+// the land is landable: the pre-resolution (landResolveAt) and the lane's write in one call, as the+// tests of the resolution itself need it. UserLand runs the two apart, the first out of the lane.+func (s *Server) resolveLandConflicts(ctx context.Context, in *gitalypb.UserLandRequest, repo *localrepo.Repo, tip, commitID git.ObjectID, committer git.Signature) (landResolution, error) {+ l, answer, err := s.landResolveAt(ctx, in, repo, tip, commitID, committer, landVerdicts{})+ if err != nil {+ return landResolution{}, err+ }+ res, err := s.landWriteResolution(ctx, in, repo, tip, commitID, l.mergedTree, committer, identity.Actor(ctx, ""), answer, "")+ return res, l.verifyRejected(err)+}diff --git a/internal/gitaly/service/operations/land_resolve_test.go b/internal/gitaly/service/operations/land_resolve_test.goindex d98a786..77cb60b 100644--- a/internal/gitaly/service/operations/land_resolve_test.go+++ b/internal/gitaly/service/operations/land_resolve_test.go@@ -163,8 +163,16 @@ func newResolveFixture(t *testing.T, rcfg config.GraftResolver) resolveFixture {func newResolveFixtureWith(t *testing.T, rcfg config.GraftResolver, baseEntries, tipEntries, commitEntries []gittest.TreeEntry) resolveFixture {t.Helper()+ return newResolveFixtureIn(t, config.Graft{Land: config.GraftLand{Resolver: rcfg}}, baseEntries, tipEntries, commitEntries)+}++// newResolveFixtureIn is newResolveFixtureWith with the whole Graft configuration of the service,+// for the tests that land through the lanes of a standalone server.+func newResolveFixtureIn(t *testing.T, graft config.Graft, baseEntries, tipEntries, commitEntries []gittest.TreeEntry) resolveFixture {+ t.Helper()+cfg := testcfg.Build(t)- cfg.Graft.Land.Resolver = rcfg+ cfg.Graft = graftctx, cfg, client := setupOperationsServiceWithCfg(t, testhelper.Context(t), cfg)// The resolution writes commits directly, outside an RPC, so the feature flags that// commit writing checks are set explicitly.@@ -258,9 +266,12 @@ func TestResolveLandConflicts_mergesResolvedFiles(t *testing.T) {req := resolverRequest(t, dir, 1)require.Equal(t, resolver.ProtocolVersion, req.Version)require.Equal(t, git.DefaultBranch, req.Branch)- require.Equal(t, fx.tip.String(), req.Tip)require.Equal(t, fx.commit.String(), req.Commit)- require.Equal(t, fx.base.String(), req.MergeBase)+ // A request carries only what a file's verdict may depend on (PLAN10 15.8): not the tip, its+ // merge base or its message, which a verdict reused at another tip must not depend on.+ require.Empty(t, req.Tip)+ require.Empty(t, req.MergeBase)+ require.Empty(t, req.Messages.Tip)// There is no authenticated identity, so the request carries no actor, whatever the user name.require.Empty(t, req.Actor)require.Equal(t, int64(1048576), req.Limits.MaxFileBytes)@@ -923,13 +934,14 @@ func TestUserLand_resolveConflicts(t *testing.T) {requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)})- t.Run("retries against the new tip after a concurrent writer", func(t *testing.T) {+ t.Run("a writer that moves the tip on another file costs no second resolution", func(t *testing.T) {t.Parallel()- // The script moves the branch to the writer commit while the first attempt is being- // resolved. The first compare-and-swap then fails, and the second attempt resolves- // against the writer. The paths of the repository and the commits are read from files- // the test writes, because the repository exists only once the service runs.+ // The script moves the branch to the writer commit while the land is being resolved, out+ // of the lane. The writer adds e and leaves a and b as they were: their verdicts hold at+ // the writer (PLAN10 15.8), so the lane lands them on the writer without calling the+ // resolver again. The paths of the repository and the commits are read from files the test+ // writes, because the repository exists only once the service runs.dir, argv := resolverScript(t, fmt.Sprintf(`if [ "$N" = 1 ]; then"$(cat "$DIR/git")" -C "$(cat "$DIR/repo")" update-ref refs/heads/%s "$(cat "$DIR/writer")" "$(cat "$DIR/tip")"fi`, git.DefaultBranch))@@ -957,9 +969,8 @@ fi`, git.DefaultBranch))resp, err := fx.client.UserLand(fx.ctx, fx.request("Land agent work"))require.NoError(t, err)- require.Equal(t, int32(2), resp.GetAttempts())- require.Equal(t, 2, resolverCalls(t, dir))- require.Equal(t, writer.String(), resolverRequest(t, dir, 2).Tip)+ require.Equal(t, int32(1), resp.GetAttempts())+ require.Equal(t, 1, resolverCalls(t, dir), "the verdicts on a and b are reused at the writer")// The old tip reported is the tip of the attempt that landed, the writer commit.require.Equal(t, writer.String(), resp.GetOldTip())diff --git a/internal/graft/resolver/check.go b/internal/graft/resolver/check.goindex bc54562..1359c1a 100644--- a/internal/graft/resolver/check.go+++ b/internal/graft/resolver/check.go@@ -79,6 +79,55 @@ func (s *CheckSlot) Release() {<-s.c.checkSlots}+// resolveSlotWait is the time the resolutions waited for a resolve slot.+var resolveSlotWait = promauto.NewHistogram(prometheus.HistogramOpts{+ Name: "graft_resolver_resolve_slot_wait_seconds",+ Help: "Time a resolution waited for one of the resolver's resolve slots.",+ Buckets: prometheus.ExponentialBuckets(0.01, 4, 8),+})++// ResolveSlot is the right to send one resolve request at a time: the client runs at most+// resolve_concurrency resolutions at once (AcquireResolveSlot), in a pool apart from the checks'.+// Release it when the request is done.+type ResolveSlot struct {+ c *Client+ released bool+}++// AcquireResolveSlot waits for a resolve slot. The wait counts against the deadline of ctx: when+// ctx ends first, it fails, and the resolution fails closed.+func (c *Client) AcquireResolveSlot(ctx context.Context) (*ResolveSlot, error) {+ if c == nil {+ return nil, ErrDisabled+ }+ start := time.Now()+ select {+ case c.resolveSlots <- struct{}{}:+ resolveSlotWait.Observe(time.Since(start).Seconds())+ return &ResolveSlot{c: c}, nil+ case <-ctx.Done():+ resolveSlotWait.Observe(time.Since(start).Seconds())+ return nil, fmt.Errorf("waiting for a resolve slot of the resolver: %w", ctx.Err())+ }+}++// Release gives the slot back; a second call does nothing.+func (s *ResolveSlot) Release() {+ if s == nil || s.released {+ return+ }+ s.released = true+ <-s.c.resolveSlots+}++// Resolve sends one resolve request with the slot (Client.resolve).+func (s *ResolveSlot) Resolve(ctx context.Context, req Request) (*Response, error) {+ if s == nil || s.released {+ return nil, errors.New("resolve without a resolve slot")+ }+ return s.c.resolve(ctx, req)+}+// 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 {diff --git a/internal/graft/resolver/client.go b/internal/graft/resolver/client.goindex 38e4f38..70ef97b 100644--- a/internal/graft/resolver/client.go+++ b/internal/graft/resolver/client.go@@ -97,8 +97,11 @@ type Client struct {pathsDeny []stringactors []stringhttpClient *http.Client- // checkSlots bounds the checks of agents' resolutions that run at once (CheckSlot).- checkSlots chan struct{}+ // checkSlots bounds the checks of agents' resolutions that run at once (CheckSlot), and+ // resolveSlots the resolutions (ResolveSlot): two pools, so that long resolutions never delay+ // the short checks (PLAN10 15.8).+ checkSlots chan struct{}+ resolveSlots chan struct{}// sendRules decide the files that are never sent (SendRules).sendRules *SendRules}@@ -132,8 +135,8 @@ func New(cfg config.GraftResolver) (*Client, error) {return nil, err}- 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")+ if cfg.MaxFiles < 0 || cfg.MaxFileBytes < 0 || cfg.CheckConcurrency < 0 || cfg.ResolveConcurrency < 0 || cfg.ResolveRounds < 0 {+ return nil, errors.New("graft land resolver: max_files, max_file_bytes, check_concurrency, resolve_concurrency and resolve_rounds 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.@@ -155,6 +158,7 @@ func New(cfg config.GraftResolver) (*Client, error) {actors: slices.Clone(cfg.Actors),sendRules: rules,checkSlots: make(chan struct{}, cfg.CheckConcurrencyOrDefault()),+ resolveSlots: make(chan struct{}, cfg.ResolveConcurrencyOrDefault()),}if cfg.URL != "" {@@ -188,13 +192,13 @@ func New(cfg config.GraftResolver) (*Client, error) {return c, nil}-// Resolve sends req to the resolver and returns its validated answer. The-// request's Version and Limits are set by the client. A refusal is an error-// matching ErrUnresolvable; any other failure (timeout, bound exceeded, invalid-// answer, transport error) is a distinct error. Resolve returns an error-// without calling the resolver when the request breaks the client's limits or-// policy.-func (c *Client) Resolve(ctx context.Context, req Request) (*Response, error) {+// resolve sends req to the resolver and returns its validated answer; callers+// send it with a resolve slot (ResolveSlot.Resolve). The request's Version and+// Limits are set by the client. A refusal is an error matching+// ErrUnresolvable; any other failure (timeout, bound exceeded, invalid answer,+// transport error) is a distinct error. It returns an error without calling+// the resolver when the request breaks the client's limits or policy.+func (c *Client) resolve(ctx context.Context, req Request) (*Response, error) {if c == nil {return nil, ErrDisabled}diff --git a/internal/graft/resolver/client_test.go b/internal/graft/resolver/client_test.goindex e4a9503..6997d2f 100644--- a/internal/graft/resolver/client_test.go+++ b/internal/graft/resolver/client_test.go@@ -170,7 +170,7 @@ func TestNilClient(t *testing.T) {t.Parallel()var client *Client- resp, err := client.Resolve(context.Background(), sampleRequest())+ resp, err := client.resolve(context.Background(), sampleRequest())require.ErrorIs(t, err, ErrDisabled)require.Nil(t, resp)}@@ -184,7 +184,7 @@ func TestResolve_CommandSuccess(t *testing.T) {req := sampleRequest()req.Limits = Limits{MaxFileBytes: 1 << 30} // the caller's value must not leak through- resp, err := client.Resolve(testhelper.Context(t), req)+ resp, err := client.resolve(testhelper.Context(t), req)require.NoError(t, err)require.Equal(t, "graft-resolve/test", resp.Resolver)require.Equal(t, "merged both sides", resp.Explanation)@@ -218,7 +218,7 @@ func TestResolve_NonASCIIPathRoundTrips(t *testing.T) {req := sampleRequest()req.Files[0].Path = p- got, err := client.Resolve(testhelper.Context(t), req)+ got, err := client.resolve(testhelper.Context(t), req)require.NoError(t, err)require.Equal(t, p, got.Files[0].Path)@@ -236,7 +236,7 @@ func TestResolve_CommandDefaultLimits(t *testing.T) {command, reqFile := capturingCommand(t, mustJSON(t, validResponse("x")))client := newClient(t, commandConfig(command))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.NoError(t, err)raw, err := os.ReadFile(reqFile)@@ -252,7 +252,7 @@ func TestResolve_CommandRefusal(t *testing.T) {client := newClient(t, commandConfig([]string{writeScript(t, "cat > /dev/null\necho 'binary conflict' >&2\nexit 3\n")}))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrUnresolvable)require.False(t, errors.Is(err, ErrInvalidResponse))@@ -267,7 +267,7 @@ func TestResolve_CommandRefusalWithoutStderr(t *testing.T) {client := newClient(t, commandConfig([]string{writeScript(t, "cat > /dev/null\nexit 3\n")}))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.ErrorIs(t, err, ErrUnresolvable)require.ErrorContains(t, err, "exit status 3")}@@ -279,7 +279,7 @@ func TestResolve_CommandRefusalWithOutputIsStillRefusal(t *testing.T) {stdout := mustJSON(t, validResponse("merged"))client := newClient(t, commandConfig([]string{writeScript(t, fmt.Sprintf("cat > /dev/null\nprintf '%%s' '%s'\necho nope >&2\nexit 1\n", stdout))}))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.ErrorIs(t, err, ErrUnresolvable)require.ErrorContains(t, err, "nope")}@@ -312,7 +312,7 @@ func TestResolve_CommandInvalidAnswers(t *testing.T) {t.Run(tc.name, func(t *testing.T) {client := newClient(t, commandConfig(cannedCommand(t, tc.stdout)))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrInvalidResponse)require.False(t, errors.Is(err, ErrUnresolvable))@@ -328,7 +328,7 @@ func TestResolve_CommandOversizedOutput(t *testing.T) {// of output is far beyond that bound.client := newClient(t, config.GraftResolver{Enabled: true, Command: []string{writeScript(t, "cat > /dev/null\nhead -c 300000 /dev/zero\n")}, Timeout: "10s", MaxFileBytes: 1024})- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrInvalidResponse)require.ErrorContains(t, err, "exceeds")@@ -340,7 +340,7 @@ func TestResolve_CommandTimeout(t *testing.T) {client := newClient(t, config.GraftResolver{Enabled: true, Command: []string{writeScript(t, "cat > /dev/null\nexec sleep 30\n")}, Timeout: "200ms"})start := time.Now()- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())elapsed := time.Since(start)require.Nil(t, resp)@@ -358,7 +358,7 @@ func TestResolve_CommandTimeoutWithGrandchild(t *testing.T) {client := newClient(t, config.GraftResolver{Enabled: true, Command: []string{writeScript(t, "cat > /dev/null\nsleep 5 &\nwait\n")}, Timeout: "200ms"})start := time.Now()- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.ErrorIs(t, err, context.DeadlineExceeded)require.Less(t, time.Since(start), 15*time.Second)}@@ -371,7 +371,7 @@ func TestResolve_CallerCanceled(t *testing.T) {ctx, cancel := context.WithCancel(testhelper.Context(t))time.AfterFunc(100*time.Millisecond, cancel)- _, err := client.Resolve(ctx, sampleRequest())+ _, err := client.resolve(ctx, sampleRequest())require.ErrorIs(t, err, context.Canceled)require.False(t, errors.Is(err, ErrUnresolvable))}@@ -381,7 +381,7 @@ func TestResolve_CommandNotFound(t *testing.T) {client := newClient(t, commandConfig([]string{filepath.Join(t.TempDir(), "no-such-resolver")}))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.Error(t, err)require.False(t, errors.Is(err, ErrUnresolvable))@@ -398,7 +398,7 @@ func TestResolve_CommandGetsMinimalEnvironment(t *testing.T) {client := newClient(t, commandConfig(command))// The output is invalid on purpose: only the environment matters here.- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.ErrorIs(t, err, ErrInvalidResponse)raw, err := os.ReadFile(envFile)@@ -430,10 +430,10 @@ func TestCheck_runsForEveryActorAndFileCount(t *testing.T) {require.NoError(t, err)require.Len(t, resp.Files, 2)- _, err = client.Resolve(testhelper.Context(t), req)+ _, err = client.resolve(testhelper.Context(t), req)require.ErrorContains(t, err, "2 conflicted files, limit is 1")cfg.MaxFiles = 0- _, err = newClient(t, cfg).Resolve(testhelper.Context(t), req)+ _, err = newClient(t, cfg).resolve(testhelper.Context(t), req)require.ErrorContains(t, err, `actor "agent-7" may not request conflict resolution`)}@@ -586,7 +586,7 @@ func TestResolve_RequestChecksDoNotRunTheResolver(t *testing.T) {tc.mutate(&req)}- resp, err := client.Resolve(testhelper.Context(t), req)+ resp, err := client.resolve(testhelper.Context(t), req)require.Nil(t, resp)require.ErrorContains(t, err, tc.wantErr)require.NoFileExists(t, marker, "a rejected request must never reach the resolver")@@ -607,7 +607,7 @@ func TestResolve_ConcurrentCalls(t *testing.T) {go func() {defer wg.Done()- resp, err := client.Resolve(context.Background(), sampleRequest())+ resp, err := client.resolve(context.Background(), sampleRequest())if err == nil && string(resp.Files[0].Content) != "merged\n" {err = fmt.Errorf("unexpected content %q", resp.Files[0].Content)}@@ -727,6 +727,94 @@ func TestAcquireCheckSlot_defaultConcurrency(t *testing.T) {require.Equal(t, config.DefaultGraftResolverCheckConcurrency, cap(client.checkSlots))}+// TestAcquireResolveSlot_boundsTheResolutionsAtOnce: at most resolve_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 resolve any more.+func TestAcquireResolveSlot_boundsTheResolutionsAtOnce(t *testing.T) {+ cfg := commandConfig([]string{"/bin/true"})+ cfg.ResolveConcurrency = 1+ client := newClient(t, cfg)+ ctx := testhelper.Context(t)++ first, err := client.AcquireResolveSlot(ctx)+ require.NoError(t, err)++ waitCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)+ defer cancel()+ _, err = client.AcquireResolveSlot(waitCtx)+ require.ErrorIs(t, err, context.DeadlineExceeded)+ require.ErrorContains(t, err, "waiting for a resolve slot of the resolver")++ first.Release()+ first.Release()+ _, err = first.Resolve(ctx, sampleRequest())+ require.ErrorContains(t, err, "resolve without a resolve slot")++ second, err := client.AcquireResolveSlot(ctx)+ require.NoError(t, err)+ waitCtx2, cancel2 := context.WithTimeout(ctx, 50*time.Millisecond)+ defer cancel2()+ _, err = client.AcquireResolveSlot(waitCtx2)+ require.ErrorIs(t, err, context.DeadlineExceeded, "a second release must not free a second place")+ second.Release()++ var none *ResolveSlot+ _, err = none.Resolve(ctx, sampleRequest())+ require.ErrorContains(t, err, "resolve without a resolve slot")+ none.Release()++ var disabled *Client+ _, err = disabled.AcquireResolveSlot(ctx)+ require.ErrorIs(t, err, ErrDisabled)+}++// TestResolveSlots_apartFromTheCheckSlots: the resolutions and the checks have pools of their own+// (PLAN10 15.8): every resolve slot held does not delay a check, and every check slot held does not+// delay a resolution.+func TestResolveSlots_apartFromTheCheckSlots(t *testing.T) {+ cfg := commandConfig([]string{"/bin/true"})+ cfg.CheckConcurrency = 1+ cfg.ResolveConcurrency = 1+ client := newClient(t, cfg)+ ctx := testhelper.Context(t)++ resolveSlot, err := client.AcquireResolveSlot(ctx)+ require.NoError(t, err)+ quick, cancel := context.WithTimeout(ctx, time.Second)+ defer cancel()+ checkSlot, err := client.AcquireCheckSlot(quick)+ require.NoError(t, err, "a held resolve slot delays no check")++ resolveSlot.Release()+ quick2, cancel2 := context.WithTimeout(ctx, time.Second)+ defer cancel2()+ again, err := client.AcquireResolveSlot(quick2)+ require.NoError(t, err, "a held check slot delays no resolution")+ again.Release()+ checkSlot.Release()+}++// TestAcquireResolveSlot_defaultConcurrency: without resolve_concurrency, the client holds+// DefaultGraftResolverResolveConcurrency slots.+func TestAcquireResolveSlot_defaultConcurrency(t *testing.T) {+ client := newClient(t, commandConfig([]string{"/bin/true"}))+ require.Equal(t, config.DefaultGraftResolverResolveConcurrency, cap(client.resolveSlots))+}++// TestNew_refusesNegativePools: a negative resolve_concurrency or resolve_rounds is refused, as a+// negative check_concurrency is.+func TestNew_refusesNegativePools(t *testing.T) {+ for name, set := range map[string]func(*config.GraftResolver){+ "resolve_concurrency": func(c *config.GraftResolver) { c.ResolveConcurrency = -1 },+ "resolve_rounds": func(c *config.GraftResolver) { c.ResolveRounds = -1 },+ } {+ cfg := commandConfig([]string{"/bin/true"})+ set(&cfg)+ _, err := New(cfg)+ require.ErrorContains(t, err, "must not be negative", name)+ }+}+// 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) {diff --git a/internal/graft/resolver/http_test.go b/internal/graft/resolver/http_test.goindex 0bc9e39..5dd9427 100644--- a/internal/graft/resolver/http_test.go+++ b/internal/graft/resolver/http_test.go@@ -84,7 +84,7 @@ func TestResolve_HTTPSuccess(t *testing.T) {client := newClient(t, httpConfig(srv.URL, writeToken(t, "s3cret")))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.NoError(t, err)require.Equal(t, []byte("merged\n"), resp.Files[0].Content)require.Equal(t, "graft-resolve/test", resp.Resolver)@@ -100,7 +100,7 @@ func TestResolve_HTTPWithoutTokenSendsNoAuthorization(t *testing.T) {client := newClient(t, httpConfig(srv.URL, ""))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.NoError(t, err)}@@ -117,7 +117,7 @@ func TestResolve_HTTPTokenIsReadOnce(t *testing.T) {// Rotating the file afterwards does not change what the client sends.require.NoError(t, os.WriteFile(tokenFile, []byte("second\n"), 0o600))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.NoError(t, err)}@@ -146,7 +146,7 @@ func TestResolve_HTTPRefusal(t *testing.T) {client := newClient(t, httpConfig(srv.URL, ""))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrUnresolvable)require.False(t, errors.Is(err, ErrInvalidResponse))@@ -173,7 +173,7 @@ func TestResolve_HTTPOtherStatusIsFailure(t *testing.T) {client := newClient(t, httpConfig(srv.URL, ""))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.Error(t, err)require.False(t, errors.Is(err, ErrUnresolvable), "only HTTP 422 is a refusal")@@ -198,7 +198,7 @@ func TestResolve_HTTPRedirectIsNotFollowed(t *testing.T) {client := newClient(t, httpConfig(redirect.URL, writeToken(t, "s3cret")))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.Error(t, err)require.False(t, errors.Is(err, ErrUnresolvable))require.Equal(t, int32(0), target.Load(), "the bearer token must not follow a redirect")@@ -228,7 +228,7 @@ func TestResolve_HTTPInvalidAnswers(t *testing.T) {client := newClient(t, httpConfig(srv.URL, ""))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrInvalidResponse)require.False(t, errors.Is(err, ErrUnresolvable))@@ -249,7 +249,7 @@ func TestResolve_HTTPOversizedAnswer(t *testing.T) {cfg.MaxFileBytes = 1024client := newClient(t, cfg)- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, ErrInvalidResponse)require.ErrorContains(t, err, "exceeds")@@ -271,7 +271,7 @@ func TestResolve_HTTPTimeout(t *testing.T) {client := newClient(t, cfg)start := time.Now()- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.ErrorIs(t, err, context.DeadlineExceeded)require.False(t, errors.Is(err, ErrUnresolvable))@@ -287,7 +287,7 @@ func TestResolve_HTTPUnreachable(t *testing.T) {client := newClient(t, httpConfig(url, ""))- resp, err := client.Resolve(testhelper.Context(t), sampleRequest())+ resp, err := client.resolve(testhelper.Context(t), sampleRequest())require.Nil(t, resp)require.Error(t, err)require.False(t, errors.Is(err, ErrUnresolvable))@@ -303,7 +303,7 @@ func TestResolve_HTTPErrorDoesNotLeakToken(t *testing.T) {client := newClient(t, httpConfig(srv.URL, writeToken(t, "s3cret-token-value")))- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.Error(t, err)require.NotContains(t, err.Error(), "s3cret-token-value")}@@ -321,7 +321,7 @@ func TestResolve_HTTPRejectedRequestNeverSent(t *testing.T) {cfg.PathsDeny = []string{"a.*"}client := newClient(t, cfg)- _, err := client.Resolve(testhelper.Context(t), sampleRequest())+ _, err := client.resolve(testhelper.Context(t), sampleRequest())require.ErrorContains(t, err, "is not allowed to be resolved")require.Equal(t, int32(0), hits.Load())}@@ -342,7 +342,7 @@ func TestResolve_HTTPCallerCanceled(t *testing.T) {ctx, cancel := context.WithCancel(testhelper.Context(t))time.AfterFunc(100*time.Millisecond, cancel)- _, err := client.Resolve(ctx, sampleRequest())+ _, err := client.resolve(ctx, sampleRequest())require.ErrorIs(t, err, context.Canceled)}@@ -363,7 +363,7 @@ func TestResolve_HTTPConcurrentCalls(t *testing.T) {go func() {defer wg.Done()- _, err := client.Resolve(context.Background(), sampleRequest())+ _, err := client.resolve(context.Background(), sampleRequest())errs <- err}()}diff --git a/tools/graft-agents/resolve/bench/run.go b/tools/graft-agents/resolve/bench/run.goindex dea4cbe..9bf3700 100644--- a/tools/graft-agents/resolve/bench/run.go+++ b/tools/graft-agents/resolve/bench/run.go@@ -217,23 +217,19 @@ func (r Runner) runCase(ctx context.Context, c Case, maxBytes int64) (Result, erreturn res, nil}- oursMessage, err := g.message(ctx, c.Ours)- if err != nil {- return res, err- }theirsMessage, err := g.message(ctx, c.Theirs)if err != nil {return res, err}+ // The request is the server's (PLAN10 15.8): no tip, no merge base, no message of the tip, which+ // a verdict reused at another tip must not depend on.req := protocol.Request{Version: protocol.Version,Repository: protocol.Repository{Storage: "bench", RelativePath: c.Repo},Branch: "bench",- Tip: c.Ours,Commit: c.Theirs,- MergeBase: c.Base,- Messages: protocol.Messages{Tip: oursMessage, Commit: theirsMessage},+ Messages: protocol.Messages{Commit: theirsMessage},Actor: "bench",Files: []protocol.FileConflict{{Path: c.Path, Kind: c.Kind, Base: sides["base"], Ours: sides["ours"], Theirs: sides["theirs"],