Git designed for AI agents.
graft: a model's resolution goes only to a server that holds and verifies it (request capabilities)
Federico Eliaa00af10d8f4c
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.goindex fe1b1f4..d3dbe92 100644--- a/internal/gitaly/service/operations/land_resolve.go+++ b/internal/gitaly/service/operations/land_resolve.go@@ -185,6 +185,9 @@ func (s *Server) landResolveAt(Messages: resolver.Messages{Commit: l.commitMessage},Actor: actor,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,}// 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 666d39c..db173ee 100644--- a/internal/graft/resolver/protocol.go+++ b/internal/graft/resolver/protocol.go@@ -61,8 +61,31 @@ type Request struct {// Resolution is, for OperationCheck only, the agent's resolution of Files to check.Resolution []ResponseFile `json:"resolution,omitempty"`Limits Limits `json:"limits"`+ // Capabilities are what the server that sends the request does with a resolver's answers (the+ // Capability constants). A server that sets none is an older one, which lands every answer that+ // graft-resolve gives without verification: graft-resolve then gives no model's resolution+ // (resolve.Resolver.resolveFile).+ Capabilities []string `json:"capabilities,omitempty"`}+// The capabilities of a server (Request.Capabilities).+const (+ // CapabilityOrigin: the server reads the origin of a resolved file and lands only the files of a rule+ // without verification.+ CapabilityOrigin = "origin"+ // CapabilityProposal: a file the server does not know to be a rule's is a model's, a proposal that+ // lands only on a tree that verify.command passed on.+ CapabilityProposal = "proposal"+ // CapabilityHold: the server never lands a file with a hold (ResponseFile.Hold).+ CapabilityHold = "hold"+ // CapabilityVerify: the server verifies the exact tree that lands (verify.command) and lands a model's+ // resolution on no other.+ CapabilityVerify = "verify"+)++// ServerCapabilities are the capabilities of this server, sent with every resolve request.+var ServerCapabilities = []string{CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify}+// Operations of a request.const (// OperationResolve asks the resolver to resolve Files; an absent operation means it.diff --git a/internal/graft/resolver/protocol_enums_test.go b/internal/graft/resolver/protocol_enums_test.goindex 55d8bed..902a4ce 100644--- a/internal/graft/resolver/protocol_enums_test.go+++ b/internal/graft/resolver/protocol_enums_test.go@@ -21,13 +21,14 @@ func TestProtocolEnumValuesMatchTheGolden(t *testing.T) {require.EqualValues(t, ProtocolVersion, golden["version"])got := map[string][]string{- "kind": {KindContent, KindModifyDelete, KindAddAdd, KindMode},- "action": {ActionWrite, ActionDelete},- "mode": {ModeRegular, ModeExecutable, ModeSymlink},- "operation": {OperationResolve, OperationCheck},- "syntax": {SyntaxChecked, SyntaxUnchecked, SyntaxNone},- "origin": {OriginRule, OriginModel},- "outcome": {OutcomeResolved, OutcomeRejected, OutcomeFailed},+ "kind": {KindContent, KindModifyDelete, KindAddAdd, KindMode},+ "action": {ActionWrite, ActionDelete},+ "mode": {ModeRegular, ModeExecutable, ModeSymlink},+ "operation": {OperationResolve, OperationCheck},+ "syntax": {SyntaxChecked, SyntaxUnchecked, SyntaxNone},+ "origin": {OriginRule, OriginModel},+ "outcome": {OutcomeResolved, OutcomeRejected, OutcomeFailed},+ "capability": {CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify},}for name, want := range got {raw, ok := golden[name].([]any)diff --git a/internal/graft/resolver/testdata/protocol-enums.json b/internal/graft/resolver/testdata/protocol-enums.jsonindex 810cade..a81e3d0 100644--- a/internal/graft/resolver/testdata/protocol-enums.json+++ b/internal/graft/resolver/testdata/protocol-enums.json@@ -7,5 +7,6 @@"operation": ["resolve", "check"],"syntax": ["checked", "unchecked", "none"],"origin": ["rule", "model"],- "outcome": ["resolved", "rejected", "failed"]+ "outcome": ["resolved", "rejected", "failed"],+ "capability": ["origin", "proposal", "hold", "verify"]}diff --git a/internal/graft/resolver/testdata/protocol-fields.json b/internal/graft/resolver/testdata/protocol-fields.jsonindex 938e478..2da7673 100644--- a/internal/graft/resolver/testdata/protocol-fields.json+++ b/internal/graft/resolver/testdata/protocol-fields.json@@ -1,6 +1,6 @@{"comment": "The JSON fields of every message of the resolver protocol v1, by message. Both copies of the protocol (internal/graft/resolver and tools/graft-agents/protocol) are tested against this file, so they cannot drift: a field added to one copy fails the other's test until it is added there and here.",- "request": ["actor", "branch", "commit", "files", "limits", "merge_base", "messages", "operation", "repository", "resolution", "tip", "version"],+ "request": ["actor", "branch", "capabilities", "commit", "files", "limits", "merge_base", "messages", "operation", "repository", "resolution", "tip", "version"],"repository": ["relative_path", "storage"],"messages": ["commit", "tip"],"file": ["base", "kind", "ours", "path", "theirs"],diff --git a/tools/graft-agents/protocol/enums_test.go b/tools/graft-agents/protocol/enums_test.goindex 03d2009..7bf0133 100644--- a/tools/graft-agents/protocol/enums_test.go+++ b/tools/graft-agents/protocol/enums_test.go@@ -24,13 +24,14 @@ func TestEnumValuesMatchTheServerCopy(t *testing.T) {t.Fatalf("version = %v, golden %v", Version, golden["version"])}got := map[string][]string{- "kind": {KindContent, KindModifyDelete, KindAddAdd, KindMode},- "action": {ActionWrite, ActionDelete},- "mode": {ModeFile, ModeExecutable, ModeSymlink},- "operation": {OperationResolve, OperationCheck},- "syntax": {SyntaxChecked, SyntaxUnchecked, SyntaxNone},- "origin": {OriginRule, OriginModel},- "outcome": {OutcomeResolved, OutcomeRejected, OutcomeFailed},+ "kind": {KindContent, KindModifyDelete, KindAddAdd, KindMode},+ "action": {ActionWrite, ActionDelete},+ "mode": {ModeFile, ModeExecutable, ModeSymlink},+ "operation": {OperationResolve, OperationCheck},+ "syntax": {SyntaxChecked, SyntaxUnchecked, SyntaxNone},+ "origin": {OriginRule, OriginModel},+ "outcome": {OutcomeResolved, OutcomeRejected, OutcomeFailed},+ "capability": {CapabilityOrigin, CapabilityProposal, CapabilityHold, CapabilityVerify},}for name, want := range got {raw, ok := golden[name].([]any)diff --git a/tools/graft-agents/protocol/protocol.go b/tools/graft-agents/protocol/protocol.goindex dd5be0c..57ae1df 100644--- a/tools/graft-agents/protocol/protocol.go+++ b/tools/graft-agents/protocol/protocol.go@@ -63,8 +63,42 @@ type Request struct {// Resolution is, for OperationCheck only, the agent's resolution of Files to check.Resolution []ResolvedFile `json:"resolution,omitempty"`Limits Limits `json:"limits"`+ // Capabilities are what the server that sends the request does with a resolver's answers (the+ // Capability constants). A server that sets none is an older one, which lands every answer that+ // graft-resolve gives without verification: graft-resolve then gives no model's resolution+ // (resolve.Resolver.resolveFile).+ Capabilities []string `json:"capabilities,omitempty"`}+// The capabilities of a server (Request.Capabilities).+const (+ // CapabilityOrigin: the server reads ResolvedFile.Origin and lands only the files of a rule+ // without verification.+ CapabilityOrigin = "origin"+ // CapabilityProposal: a file the server does not know to be a rule's is a model's, a proposal that+ // lands only on a tree that verify.command passed on.+ CapabilityProposal = "proposal"+ // CapabilityHold: the server never lands a file with a hold (ResolvedFile.Hold).+ CapabilityHold = "hold"+ // CapabilityVerify: the server verifies the exact tree that lands and lands a model's resolution+ // on no other.+ CapabilityVerify = "verify"+)++// Has reports whether the request says that its server has the capability.+func (r Request) Has(capability string) bool {+ for _, c := range r.Capabilities {+ if c == capability {+ return true+ }+ }+ 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}+// Operations of a request.const (// OperationResolve asks for a resolution of Files; an absent operation means it.diff --git a/tools/graft-agents/resolve/bench/run.go b/tools/graft-agents/resolve/bench/run.goindex 2d9dec7..e182053 100644--- a/tools/graft-agents/resolve/bench/run.go+++ b/tools/graft-agents/resolve/bench/run.go@@ -247,6 +247,8 @@ func (r Runner) runCase(ctx context.Context, c Case, maxBytes int64) (Result, erPath: c.Path, Kind: c.Kind, Base: sides["base"], Ours: sides["ours"], Theirs: sides["theirs"],}},Limits: protocol.Limits{MaxFileBytes: maxBytes},+ // The bench stands in for the server, which holds and verifies a model's resolution.+ Capabilities: protocol.ServerCapabilities,}if c.Kind == protocol.KindContent && sides["ours"] != nil && sides["theirs"] != nil {diff --git a/tools/graft-agents/resolve/llm_strategy_test.go b/tools/graft-agents/resolve/llm_strategy_test.goindex 144edad..7442497 100644--- a/tools/graft-agents/resolve/llm_strategy_test.go+++ b/tools/graft-agents/resolve/llm_strategy_test.go@@ -128,6 +128,8 @@ func wp19Request(files ...protocol.FileConflict) protocol.Request {Actor: "agent-1",Files: files,Limits: protocol.Limits{MaxFileBytes: 1 << 20},+ // The server of the tests holds and verifies a model's resolution.+ Capabilities: protocol.ServerCapabilities,}}diff --git a/tools/graft-agents/resolve/resolve.go b/tools/graft-agents/resolve/resolve.goindex 6f271ea..08f5b1d 100644--- a/tools/graft-agents/resolve/resolve.go+++ b/tools/graft-agents/resolve/resolve.go@@ -446,6 +446,13 @@ func (r *Resolver) resolveFile(ctx context.Context, req protocol.Request, f Fileif !matchPaths(b.Paths, f.Path) {continue}+ // A model's resolution is given only to a server that says it holds and verifies it. An older+ // server ignores the origin, the proposal and the hold, and lands every answer it gets: the model+ // is not even called (it would be paid for), and the file stays unresolved with the reason.+ if why := unsupportedBy(b.Strategy, req); why != "" {+ attempts = append(attempts, Attempt{Strategy: b.Name, Kind: AttemptRejected, Err: errors.New(why)})+ continue+ }res, ok, err := b.Strategy.Resolve(ctx, f, req)if err != nil {if ctxErr := ctx.Err(); ctxErr != nil {@@ -506,6 +513,28 @@ func (r *Resolver) resolveFile(ctx context.Context, req protocol.Request, f Filereturn protocol.ResolvedFile{}, "", attempts, nil}+// holder is implemented by the strategies whose answers may carry a hold (the llm strategy with an+// agree_with): the server must never land a held file, which only a server with CapabilityHold knows.+type holder interface {+ holds() bool+}++// unsupportedBy returns why the server that sent req cannot take the answers of s, or "" when it can. The+// answers of a rule need nothing; those of a model need CapabilityProposal, and CapabilityHold when they+// may be held.+func unsupportedBy(s Strategy, req protocol.Request) string {+ if originOf(s) != protocol.OriginModel {+ return ""+ }+ if !req.Has(protocol.CapabilityProposal) {+ return "the server cannot hold or verify a model's resolution; upgrade the server"+ }+ if h, ok := s.(holder); ok && h.holds() && !req.Has(protocol.CapabilityHold) {+ return "the server cannot hold a model's resolution (agree_with); upgrade the server"+ }+ return ""+}+func modeOf(res Resolution) string {if res.Action == protocol.ActionDelete {return ""diff --git a/tools/graft-agents/resolve/resolve_test.go b/tools/graft-agents/resolve/resolve_test.goindex 4d40db1..15f7cbb 100644--- a/tools/graft-agents/resolve/resolve_test.go+++ b/tools/graft-agents/resolve/resolve_test.go@@ -76,11 +76,14 @@ func conflict(path string) protocol.FileConflict {}}+// request is the request of a server of this release: it says that it holds and verifies a model's+// resolution (protocol.ServerCapabilities).func request(files ...protocol.FileConflict) protocol.Request {return protocol.Request{- Version: protocol.Version,- Limits: protocol.Limits{MaxFileBytes: 1 << 20},- Files: files,+ Version: protocol.Version,+ Limits: protocol.Limits{MaxFileBytes: 1 << 20},+ Files: files,+ Capabilities: protocol.ServerCapabilities,}}@@ -676,3 +679,52 @@ func TestProposalsAreMarkedAndNotRecorded(t *testing.T) {t.Fatalf("rule answer: origin %q, want %q", resp.Files[0].Origin, protocol.OriginRule)}}++// TestAModelsResolutionIsGivenOnlyToAServerThatHoldsAndVerifiesIt: an older server ignores the origin, the+// proposal and the hold of a file, and lands every answer it gets without verification, so graft-resolve+// does not call a model for it: the file stays unresolved, with the reason. The answers of a rule are+// unaffected. A server of this release says what it does in the request (Capabilities).+func TestAModelsResolutionIsGivenOnlyToAServerThatHoldsAndVerifiesIt(t *testing.T) {+ model := &proposingStrategy{fakeStrategy{fn: answer(protocol.ModeFile, "from the model\n")}}+ rule := &fakeStrategy{fn: answer(protocol.ModeFile, "from the rule\n")}++ // An old server: no capabilities.+ old := request(conflict("a.go"))+ old.Capabilities = nil+ r := newTestResolver(t, nil, Binding{Name: "model", Strategy: model})+ _, err := r.Resolve(context.Background(), old)+ var unresolved *UnresolvedError+ if !errors.As(err, &unresolved) || len(unresolved.Failures) != 1 {+ t.Fatalf("an old server got a model's resolution: %v", err)+ }+ if !strings.Contains(err.Error(), "the server cannot hold or verify a model's resolution; upgrade the server") {+ t.Fatalf("the reason is missing: %v", err)+ }+ if model.calls.Load() != 0 {+ t.Fatalf("the model was called %d times for a server that cannot take its answer", model.calls.Load())+ }++ // The rules of the same chain are unaffected.+ r = newTestResolver(t, nil, Binding{Name: "model", Strategy: model}, Binding{Name: "rule", Strategy: rule})+ resp, err := r.Resolve(context.Background(), old)+ if err != nil || len(resp.Files) != 1 || string(resp.Files[0].Content) != "from the rule\n" || resp.Files[0].Origin != protocol.OriginRule {+ t.Fatalf("an old server must get the rule's resolution: %v %+v", err, resp.Files)+ }++ // A server that proposes but does not hold, or any capability list without "proposal": no model.+ for _, caps := range [][]string{{protocol.CapabilityOrigin}, {protocol.CapabilityHold, protocol.CapabilityVerify}} {+ partial := request(conflict("a.go"))+ partial.Capabilities = caps+ 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)+ }+ }++ // A server of this release: the model's resolution is given, as a proposal.+ r = newTestResolver(t, nil, Binding{Name: "model", Strategy: model})+ resp, err = r.Resolve(context.Background(), request(conflict("a.go")))+ if err != nil || len(resp.Files) != 1 || resp.Files[0].Origin != protocol.OriginModel || !resp.Files[0].Proposal {+ t.Fatalf("a server of this release must get the model's proposal: %v %+v", err, resp.Files)+ }+}