Git designed for AI agents.
graft: the findings wiring is tested, a hold is refused for a server that cannot hold it
Federico Elia0bef2566ae5e
diff --git a/internal/gitaly/service/operations/land_final_pass_test.go b/internal/gitaly/service/operations/land_final_pass_test.goindex e5ee8fb..5d47449 100644--- a/internal/gitaly/service/operations/land_final_pass_test.go+++ b/internal/gitaly/service/operations/land_final_pass_test.go@@ -82,4 +82,5 @@ func TestLandFinalPass_givingUpIsTheKeptMovingError(t *testing.T) {require.NotNil(t, update, "the ReferenceUpdate detail")require.Equal(t, "refs/heads/"+git.DefaultBranch, string(update.GetReferenceName()))require.Equal(t, "1111111111111111111111111111111111111111", update.GetOldOid())+ require.Empty(t, update.GetNewOid(), "where the branch moved to is not known there")}diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.goindex d3dbe92..50585fc 100644--- a/internal/gitaly/service/operations/land_resolve.go+++ b/internal/gitaly/service/operations/land_resolve.go@@ -187,7 +187,7 @@ func (s *Server) landResolveAt(Files: sent,// What this server does with the answer, so that a graft-resolve gives a model's resolution// only to a server that holds and verifies it.- Capabilities: resolver.ServerCapabilities,+ Capabilities: resolver.ServerCapabilities(),}// The client applies the timeout, the request policy and the acceptance rules of thediff --git a/internal/graft/resolver/protocol.go b/internal/graft/resolver/protocol.goindex db173ee..110bdf3 100644--- a/internal/graft/resolver/protocol.go+++ b/internal/graft/resolver/protocol.go@@ -83,8 +83,11 @@ const (CapabilityVerify = "verify")-// ServerCapabilities are the capabilities of this server, sent with every resolve request.-var ServerCapabilities = []string{CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify}+// ServerCapabilities returns the capabilities of this server, sent with every resolve request. Every+// call returns a new slice, which nothing can change for the next request.+func ServerCapabilities() []string {+ return []string{CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify}+}// Operations of a request.const (diff --git a/tools/graft-agents/protocol/protocol.go b/tools/graft-agents/protocol/protocol.goindex 57ae1df..3089e7c 100644--- a/tools/graft-agents/protocol/protocol.go+++ b/tools/graft-agents/protocol/protocol.go@@ -95,9 +95,11 @@ func (r Request) Has(capability string) bool {return false}-// ServerCapabilities are the capabilities of a server of this release, for a client that stands in for-// one (the bench).-var ServerCapabilities = []string{CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify}+// ServerCapabilities returns the capabilities of a server of this release, for a client that stands in+// for one (the bench). Every call returns a new slice.+func ServerCapabilities() []string {+ return []string{CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify}+}// Operations of a request.const (diff --git a/tools/graft-agents/resolve/bench/run.go b/tools/graft-agents/resolve/bench/run.goindex e182053..01a3fa4 100644--- a/tools/graft-agents/resolve/bench/run.go+++ b/tools/graft-agents/resolve/bench/run.go@@ -248,7 +248,7 @@ func (r Runner) runCase(ctx context.Context, c Case, maxBytes int64) (Result, er}},Limits: protocol.Limits{MaxFileBytes: maxBytes},// The bench stands in for the server, which holds and verifies a model's resolution.- Capabilities: protocol.ServerCapabilities,+ Capabilities: protocol.ServerCapabilities(),}if c.Kind == protocol.KindContent && sides["ours"] != nil && sides["theirs"] != nil {diff --git a/tools/graft-agents/resolve/llm_agree_test.go b/tools/graft-agents/resolve/llm_agree_test.goindex 7ebf471..279be46 100644--- a/tools/graft-agents/resolve/llm_agree_test.go+++ b/tools/graft-agents/resolve/llm_agree_test.go@@ -5,6 +5,7 @@ package resolveimport ("context""errors"+ "fmt""regexp""strings""sync/atomic"@@ -336,3 +337,59 @@ func TestAgreeWithNeedsAServerThatHolds(t *testing.T) {t.Fatalf("calls %d, %d", a.calls(), b.calls())}}++// TestAgreeWithHoldsADropPastTheCutOfTheReport is the wiring of LLMResult.Findings, through Resolve and+// two fake providers: both models give the same file, which drops a side's change in the last of 18+// regions, and the report is cut at 16 lines before that region is reached, so that nothing in its text+// says "drops". The file must be held all the same, as "not clean".+func TestAgreeWithHoldsADropPastTheCutOfTheReport(t *testing.T) {+ const regions = 18+ // Region k (1-based) is at lines 4k+1 and 4k+2 of the base: OURS changes the first, THEIRS the+ // second, so that git calls them one conflict, and an answer that keeps both changes is clean.+ base := numbered("line", 4*regions+4)+ ours, theirs := base, base+ for k := 1; k <= regions; k++ {+ ours = strings.Replace(ours, fmt.Sprintf("line %d\n", 4*k+1), fmt.Sprintf("O%d\n", k), 1)+ theirs = strings.Replace(theirs, fmt.Sprintf("line %d\n", 4*k+2), fmt.Sprintf("T%d\n", k), 1)+ }+ file := protocol.FileConflict{Path: "x.txt", Kind: protocol.KindContent,+ Base: wp19Side(protocol.ModeFile, base), Ours: wp19Side(protocol.ModeFile, ours), Theirs: wp19Side(protocol.ModeFile, theirs)}++ // The answer of a model for the k-th region it is asked: the changes of both sides and a line that+ // is in neither (one line of the report each, and no finding), except in the last region, where it+ // keeps OURS' change and loses THEIRS'.+ answers := func() func(context.Context, llm.Request) (*llm.Response, error) {+ var asked atomic.Int32+ return func(context.Context, llm.Request) (*llm.Response, error) {+ k := int(asked.Add(1))+ if k == regions {+ return wp19Answer(fmt.Sprintf("O%d\nline %d\n", k, 4*k+2)), nil+ }+ return wp19Answer(fmt.Sprintf("O%d\nT%d\ninvented %d\n", k, k, k)), nil+ }+ }+ wp19SetAnswer(answers())+ agreeSetB(answers())++ r := wp19Build(t, agreeConfig)+ resp, err := r.Resolve(context.Background(), wp19Request(file))+ if err != nil {+ t.Fatal(err)+ }+ got := resp.Files[0]+ // The last line of the report is the hold itself (which names drops); the rest is the composition report.+ text := strings.Join(got.Report[:len(got.Report)-1], "\n")+ // The test proves something only if the drop is not in the text of the report.+ if strings.Contains(text, "drops") {+ t.Fatalf("the drop is in the text of the report, which is not cut before it:\n%s", text)+ }+ if !strings.Contains(text, "more lines of this report") {+ t.Fatalf("the report is not cut:\n%s", text)+ }+ if !strings.HasPrefix(got.Hold, "held: a composition report was not clean") {+ t.Fatalf("a drop past the cut of the report is not held: hold %q", got.Hold)+ }+ if got.Origin != protocol.OriginModel || !got.Proposal {+ t.Fatalf("file = %+v", got)+ }+}diff --git a/tools/graft-agents/resolve/llm_strategy_test.go b/tools/graft-agents/resolve/llm_strategy_test.goindex 7442497..76a3f73 100644--- a/tools/graft-agents/resolve/llm_strategy_test.go+++ b/tools/graft-agents/resolve/llm_strategy_test.go@@ -129,7 +129,7 @@ func wp19Request(files ...protocol.FileConflict) protocol.Request {Files: files,Limits: protocol.Limits{MaxFileBytes: 1 << 20},// The server of the tests holds and verifies a model's resolution.- Capabilities: protocol.ServerCapabilities,+ Capabilities: protocol.ServerCapabilities(),}}diff --git a/tools/graft-agents/resolve/resolve.go b/tools/graft-agents/resolve/resolve.goindex 08f5b1d..206e30c 100644--- a/tools/graft-agents/resolve/resolve.go+++ b/tools/graft-agents/resolve/resolve.go@@ -474,6 +474,12 @@ func (r *Resolver) resolveFile(ctx context.Context, req protocol.Request, f Fileattempts = append(attempts, Attempt{Strategy: b.Name, Kind: AttemptRejected, Err: err})continue}+ // Resolution.Hold is exported, so a strategy of another package may set it without implementing+ // holder: a hold is never sent to a server that cannot honour it, which would land the file.+ if res.Hold != "" && !req.Has(protocol.CapabilityHold) {+ attempts = append(attempts, Attempt{Strategy: b.Name, Kind: AttemptRejected, Err: errors.New("the server cannot hold a model's resolution; upgrade the server")})+ continue+ }if _, ok := b.Strategy.(syntaxChecked); ok && res.Action == protocol.ActionWrite && res.Mode != protocol.ModeSymlink {if _, err := syntax.CheckMerge(ctx, f.Path, res.Content, sideContent(f.Ours), sideContent(f.Theirs)); err != nil {if ctxErr := ctx.Err(); ctxErr != nil {diff --git a/tools/graft-agents/resolve/resolve_test.go b/tools/graft-agents/resolve/resolve_test.goindex 15f7cbb..1c7e139 100644--- a/tools/graft-agents/resolve/resolve_test.go+++ b/tools/graft-agents/resolve/resolve_test.go@@ -83,7 +83,7 @@ func request(files ...protocol.FileConflict) protocol.Request {Version: protocol.Version,Limits: protocol.Limits{MaxFileBytes: 1 << 20},Files: files,- Capabilities: protocol.ServerCapabilities,+ Capabilities: protocol.ServerCapabilities(),}}@@ -715,9 +715,14 @@ func TestAModelsResolutionIsGivenOnlyToAServerThatHoldsAndVerifiesIt(t *testing.for _, caps := range [][]string{{protocol.CapabilityOrigin}, {protocol.CapabilityHold, protocol.CapabilityVerify}} {partial := request(conflict("a.go"))partial.Capabilities = caps+ before := model.calls.Load()r = newTestResolver(t, nil, Binding{Name: "model", Strategy: model})- if _, err := r.Resolve(context.Background(), partial); err == nil {- t.Fatalf("capabilities %v without proposal got a model's resolution", caps)+ _, err := r.Resolve(context.Background(), partial)+ if err == nil || !strings.Contains(err.Error(), "the server cannot hold or verify a model's resolution; upgrade the server") {+ t.Fatalf("capabilities %v without proposal got a model's resolution, or no reason: %v", caps, err)+ }+ if model.calls.Load() != before {+ t.Fatalf("capabilities %v: the model was called", caps)}}@@ -728,3 +733,26 @@ func TestAModelsResolutionIsGivenOnlyToAServerThatHoldsAndVerifiesIt(t *testing.t.Fatalf("a server of this release must get the model's proposal: %v %+v", err, resp.Files)}}++// TestAHoldIsNeverSentToAServerThatCannotHoldIt: Resolution.Hold is exported, so a strategy of another+// package can set it without implementing the unexported holder interface. A hold is then refused for a+// server that does not announce "hold", which would land the file; a server that does gets it.+func TestAHoldIsNeverSentToAServerThatCannotHoldIt(t *testing.T) {+ holding := &proposingStrategy{fakeStrategy{fn: func(f File) (Resolution, bool, error) {+ return Resolution{Action: protocol.ActionWrite, Mode: protocol.ModeFile, Content: []byte("x\n"), Hold: "held: because"}, true, nil+ }}}+ r := newTestResolver(t, nil, Binding{Name: "model", Strategy: holding})++ noHold := request(conflict("a.go"))+ noHold.Capabilities = []string{protocol.CapabilityOrigin, protocol.CapabilityProposal, protocol.CapabilityVerify}+ _, err := r.Resolve(context.Background(), noHold)+ var unresolved *UnresolvedError+ if !errors.As(err, &unresolved) || !strings.Contains(err.Error(), "the server cannot hold a model's resolution; upgrade the server") {+ t.Fatalf("a hold was sent to a server that cannot hold it: %v", err)+ }++ resp, err := r.Resolve(context.Background(), request(conflict("a.go")))+ if err != nil || len(resp.Files) != 1 || resp.Files[0].Hold != "held: because" {+ t.Fatalf("a server that holds must get the hold: %v %+v", err, resp.Files)+ }+}