Skip to content

Git designed for AI agents.

graft: a strategy is a model unless it says it is a rule; a verified tree is not verified again

Federico Eliaca6549efbb50
diff --git a/doc/graft/AGENT-SECURITY.md b/doc/graft/AGENT-SECURITY.md
index fbea000..d9760df 100644
--- a/doc/graft/AGENT-SECURITY.md
+++ b/doc/graft/AGENT-SECURITY.md
@@ -234,6 +234,16 @@ start from), and a separate unprivileged user. The post-merge test command of
`graft-verify` ([VERIFY](VERIFY.md)) runs the same kind of code on the tip and
has the same advice. Without a `verify.command` nothing of a model's lands.
+What counts as a model's answer is fail-safe on both sides. The server treats any
+file that is not marked `origin: "rule"` as a model's. `graft-resolve` marks as a
+rule only the strategies that say so (trivial, union, rerere, and a `command`
+that is not configured with `origin = "model"`); every other strategy, including
+one registered from another package, is a model. A third-party resolver, or a
+strategy that claims to be a rule, is the operator's trust: a `command` that
+calls an AI and is left as a rule lands without the verification. The rerere
+store of v0.1.0 and v0.2.0 may hold model answers; its entries without an origin
+are never replayed as rules (see [RESOLVE](RESOLVE.md)).
+
- Automatic conflict resolution is off by default and can be limited to
specific actors and paths (see [RESOLVE](RESOLVE.md)).
- The verifier writes the green ref and reverts with the operator credential,
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index 75b5a0f..ec8e781 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -176,9 +176,14 @@ resolution"). Without a `verify.command` the server never lands what a model
resolved: the agent decides. Every resolved file says what made it, in `origin`
(see "Response"): `rule` or `model`. The check is fail-safe: a file is a rule's
only when it says so, and anything else is a model's, whatever the name of the
-resolver. graft-resolve marks the files of trivial, union, rerere and the
-command strategies as `rule` and those of its `llm` strategy as `model` (with
-the older `"proposal": true`). A land of an answer that holds a model's file and
+resolver. graft-resolve is fail-safe in the same way, one layer before the
+server: it marks the files of trivial, union, rerere and the command strategies
+(unless a command is configured as a model, see below) as `rule`, and every other
+strategy's files as `model`, with the older `"proposal": true`: the `llm`
+strategy, and every strategy that is registered from another package. A
+strategy is a rule only when it says so, by implementing the exported
+`resolve.RuleStrategy`; that claim is the operator's trust, and is never to be
+made for a strategy that asks a model or reads what a model wrote. A land of an answer that holds a model's file and
does not land (no
`verify.command`, a verification that failed, or none finished in time) fails
with the package: the model's files are
@@ -640,10 +645,34 @@ the error response on stdout and the reason on stderr).
|-----------|----------|
| `trivial` | One side equals the base, both sides are identical, the sides differ only in whitespace, or both sides deleted the file. |
| `union` | Concatenates the non-overlapping changes of both sides for configured globs, for example `CHANGELOG.md`. |
-| `rerere` | Reuses a stored resolution keyed by the SHA-256 of the base, ours and theirs contents. Every resolution that passes checks is recorded. |
+| `rerere` | Reuses a stored resolution keyed by the SHA-256 of the base, ours and theirs contents. Every resolution of a rule that passes checks is recorded, with its version and origin; a model's resolution is never recorded. |
| `command` | Runs an external merge tool with `{base} {ours} {theirs} {out}` placeholders in temporary files. |
| `llm` | Optional, see [AI resolvers](#ai-resolvers). |
+**A `command` that calls a model is a model.** A command is a rule by default: it
+is trusted as deterministic code, and its files land without the operator's
+verification of the tree. A command that asks an AI (a wrapper around a model
+API, a coding agent), or that reads what one wrote, must be configured with
+`origin = "model"`:
+
+```toml
+[[strategy]]
+name = "agent-merge"
+type = "command"
+command = ["/usr/local/bin/ai-merge", "{base}", "{ours}", "{theirs}", "{out}"]
+origin = "model" # its files are proposals, and land only if verify.command passes
+```
+
+`origin` is `"rule"` (the default) or `"model"`. A model command's answers are
+not recorded by `rerere`.
+
+**The `rerere` store and the upgrade.** Entries are versioned and record their
+origin. The stores of v0.1.0 and v0.2.0 recorded the `llm` answers as well, with
+no origin: after an upgrade `graft-resolve` never replays an entry without
+a version and an origin as a rule's answer (it skips it, and says so once on
+stderr: the conflict goes on to the next strategy). Clear the store on upgrade
+to drop the old entries: `rm -r <dir of the rerere strategy>`.
+
Checks are per-glob commands run on the resolved file in a temporary
directory, with `{file}` replaced by the file name. A failing check rejects
that strategy's result, and the chain continues with the next strategy.
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.go
index eba7917..d810cea 100644
--- a/internal/gitaly/service/operations/land.go
+++ b/internal/gitaly/service/operations/land.go
@@ -134,7 +134,7 @@ func (s *Server) landPrecheckInQuarantine(ctx context.Context, in *gitalypb.User
// 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, pre *landPreResolution) (*gitalypb.UserLandResponse, error) {
- snapshotPinned := mvcc.CacheFromContext(ctx) != nil
+ snapshotPinned := landSnapshotPinned(ctx)
maxAttempts := int(in.GetMaxAttempts())
if maxAttempts == 0 {
@@ -153,8 +153,8 @@ func (s *Server) landWithRetries(ctx context.Context, in *gitalypb.UserLandReque
return nil, err
}
- if attempt >= maxAttempts || snapshotPinned {
- return nil, landKeptMoving(in, pre, conflict, attempt)
+ if err := landLostRace(in, pre, conflict, attempt, maxAttempts, snapshotPinned); err != nil {
+ return nil, err
}
if err := waitLandBackoff(ctx, attempt); err != nil {
@@ -407,6 +407,27 @@ func landMergeConflictError(tip, commitID git.ObjectID, conflictingFiles [][]byt
// cause: the branch moved on): UserLand goes out again, and at its deadline the agent gets the
// proposals, not an error about a branch it has nothing to do with. The others get
// landKeptMovingError.
+// landSnapshotPinned reports whether the RPC reads a snapshot that is pinned for all of it (MVCC).
+func landSnapshotPinned(ctx context.Context) bool {
+ return mvcc.CacheFromContext(ctx) != nil
+}
+
+// landLostRace decides what a landing attempt that lost the compare-and-swap does next: nil to
+// attempt again, or the error that ends the land. On a pinned snapshot a new attempt in the RPC
+// would read the same tip and build the same tree, so the land ends with the retryable
+// FailedPrecondition for the client to retry, whatever the land holds: not even a model's
+// resolution is handed back to be verified again, as that would verify the same tree once more.
+// Otherwise the attempts run out at max_attempts and the land ends as landKeptMoving says.
+func landLostRace(in *gitalypb.UserLandRequest, pre *landPreResolution, conflict landConflict, attempt, maxAttempts int, pinned bool) error {
+ switch {
+ case pinned:
+ return landKeptMovingError(in, conflict, attempt)
+ case attempt >= maxAttempts:
+ return landKeptMoving(in, pre, conflict, attempt)
+ }
+ return nil
+}
+
func landKeptMoving(in *gitalypb.UserLandRequest, pre *landPreResolution, conflict landConflict, attempts int) error {
if pre.hasModel() {
return &landNeedsResolution{tip: conflict.newOID, commit: git.ObjectID(in.GetCommitId()), moved: true}
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index e805b4a..6b4c229 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -234,6 +234,13 @@ func (s *Server) landResolveAt(
if tree, err = s.landResolvedTree(ctx, repo, l.mergedTree, combined); err != nil {
return l, nil, err
}
+ // The command may already have passed on this very tree, in an earlier round: whatever brought
+ // the land back (a snapshot that reads the same tip, a tip that moved to the same tree),
+ // verifying it again would hold the repository's slot for minutes for the same answer.
+ if pre.isVerified(tree) {
+ verified = tree
+ break
+ }
var verifyErr *landVerifyError
switch err := s.landVerifyOutOfLane(ctx, in.GetRepository(), repo, tree); {
case err == nil:
diff --git a/internal/gitaly/service/operations/land_review_test.go b/internal/gitaly/service/operations/land_review_test.go
index 5ba0980..1c9d798 100644
--- a/internal/gitaly/service/operations/land_review_test.go
+++ b/internal/gitaly/service/operations/land_review_test.go
@@ -15,6 +15,7 @@ import (
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/internal/git/mvcc"
"github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
@@ -187,3 +188,38 @@ func TestLandStoppedReason(t *testing.T) {
require.Equal(t, "the files changed again before they could be resolved", landStoppedReason(&landNeedsResolution{paths: []string{"a"}}, time.Minute))
require.Equal(t, "the time ran out before the conflicts were resolved (deadline of 2m0s)", landStoppedReason(nil, 2*time.Minute))
}
+
+// pinnedCache stands for the cache of an MVCC snapshot: only its presence in the context matters here.
+type pinnedCache struct{ mvcc.Cache }
+
+// TestLandLostRace: what a landing attempt that lost the compare-and-swap does next. On a pinned (MVCC)
+// snapshot a new attempt reads the same tip and builds the same tree, so the land ends at once with
+// the retryable FailedPrecondition, also when it holds a model's resolution: handing that back would
+// verify the same tree again. Off MVCC the attempts run until max_attempts, and then a model's
+// resolution is handed back, as landKeptMoving says.
+func TestLandLostRace(t *testing.T) {
+ t.Parallel()
+
+ in := &gitalypb.UserLandRequest{Branch: []byte("main"), CommitId: strings.Repeat("c", 40)}
+ conflict := landConflict{reference: "refs/heads/main", oldOID: git.ObjectID(strings.Repeat("1", 40)), newOID: git.ObjectID(strings.Repeat("2", 40))}
+ model := &landPreResolution{
+ verdicts: landVerdicts{{path: "a"}: {file: resolver.ResponseFile{Path: "a", Origin: resolver.OriginModel}}},
+ }
+
+ ctx := context.Background()
+ require.False(t, landSnapshotPinned(ctx))
+ require.True(t, landSnapshotPinned(mvcc.ContextWithCache(ctx, pinnedCache{})))
+
+ for _, attempt := range []int{1, 5} {
+ err := landLostRace(in, model, conflict, attempt, 20, true)
+ var need *landNeedsResolution
+ require.False(t, errors.As(err, &need), "attempt %d", attempt)
+ require.Equal(t, codes.FailedPrecondition, status.Code(err), "attempt %d", attempt)
+ require.ErrorContains(t, err, "kept moving")
+ }
+
+ require.NoError(t, landLostRace(in, model, conflict, 3, 20, false), "an attempt that can be retried")
+ var need *landNeedsResolution
+ require.ErrorAs(t, landLostRace(in, model, conflict, 20, 20, false), &need, "the last attempt hands a model's resolution back")
+ require.ErrorContains(t, landLostRace(in, &landPreResolution{}, conflict, 20, 20, false), "kept moving")
+}
diff --git a/internal/gitaly/service/operations/land_verified_test.go b/internal/gitaly/service/operations/land_verified_test.go
index 0d61a60..e0b3786 100644
--- a/internal/gitaly/service/operations/land_verified_test.go
+++ b/internal/gitaly/service/operations/land_verified_test.go
@@ -641,3 +641,26 @@ func TestUserLand_verifiedModelResolution_aBusyRepositoryEndsInProposals(t *test
require.NoError(t, err)
other.Release()
}
+
+// TestUserLand_verifiedModelResolution_aTreeThatPassedIsNotVerifiedAgain: when a land comes back to the
+// pre-resolution with the tree that verify.command already passed on (a snapshot that reads the same tip
+// again, a tip that moved to the same tree), the command does not run again: it takes minutes and holds
+// the repository's one verification slot. The resolver is not called again either.
+func TestUserLand_verifiedModelResolution_aTreeThatPassedIsNotVerifiedAgain(t *testing.T) {
+ t.Parallel()
+
+ rdir, rargv := resolverScript(t, "")
+ writeAnswer(t, rdir, modelAnswer())
+ vdir, vargv := countingVerify(t, "exit 0")
+ rcfg := withVerify(enabledResolver(rargv), vargv, "")
+ fx := newResolveFixture(t, rcfg)
+ s := fx.server(t, rcfg)
+ s.locator = config.NewLocator(fx.cfg) // the land quarantines the objects it makes
+
+ pre := newLandPreResolution()
+ for round := 1; round <= 3; round++ {
+ require.NoError(t, s.landPreResolve(fx.ctx, fx.request("Land"), fx.committer(), pre), "round %d", round)
+ }
+ require.Equal(t, 1, verifyRuns(t, vdir), "the command ran once for the tree, not once per round")
+ require.Equal(t, 1, resolverCalls(t, rdir))
+}
diff --git a/tools/graft-agents/resolve/builtin.go b/tools/graft-agents/resolve/builtin.go
index 30b150f..4a51277 100644
--- a/tools/graft-agents/resolve/builtin.go
+++ b/tools/graft-agents/resolve/builtin.go
@@ -38,6 +38,9 @@ func newTrivial(spec StrategySpec) (Strategy, error) {
return trivial{}, nil
}
+// IsRule: trivial is a rule (RuleStrategy).
+func (trivial) IsRule() bool { return true }
+
func (trivial) Resolve(_ context.Context, f File, _ protocol.Request) (Resolution, bool, error) {
switch {
case sameSide(f.Ours, f.Theirs):
diff --git a/tools/graft-agents/resolve/command.go b/tools/graft-agents/resolve/command.go
index e096b88..dbd14de 100644
--- a/tools/graft-agents/resolve/command.go
+++ b/tools/graft-agents/resolve/command.go
@@ -36,10 +36,15 @@ type command struct {
timeout time.Duration
env []string
stdout bool
+ // model: the command asks a model, or reads what one wrote; its answers are a model's.
+ model bool
}
+// IsRule: a command is a rule unless it is configured with origin = "model" (RuleStrategy).
+func (c command) IsRule() bool { return !c.model }
+
func newCommand(spec StrategySpec) (Strategy, error) {
- if err := allowOnly(spec, "command", "timeout", "output", "env"); err != nil {
+ if err := allowOnly(spec, "command", "timeout", "output", "env", "origin"); err != nil {
return nil, err
}
argv, err := optStringList(spec, "command")
@@ -72,7 +77,19 @@ func newCommand(spec StrategySpec) (Strategy, error) {
if err := validateEnvNames(env); err != nil {
return nil, fmt.Errorf("strategy %q: %w", spec.Name, err)
}
- return command{argv: argv, timeout: timeout, env: env, stdout: stdout}, nil
+ origin, err := optString(spec, "origin")
+ if err != nil {
+ return nil, err
+ }
+ var model bool
+ switch origin {
+ case "", protocol.OriginRule:
+ case protocol.OriginModel:
+ model = true
+ default:
+ return nil, fmt.Errorf("strategy %q: origin must be %q or %q, got %q", spec.Name, protocol.OriginRule, protocol.OriginModel, origin)
+ }
+ return command{argv: argv, timeout: timeout, env: env, stdout: stdout, model: model}, nil
}
func (c command) Resolve(ctx context.Context, f File, _ protocol.Request) (Resolution, bool, error) {
diff --git a/tools/graft-agents/resolve/command_test.go b/tools/graft-agents/resolve/command_test.go
index da912f3..d56bc79 100644
--- a/tools/graft-agents/resolve/command_test.go
+++ b/tools/graft-agents/resolve/command_test.go
@@ -377,3 +377,40 @@ func TestCommandInChainWithRerereRecording(t *testing.T) {
t.Fatalf("rerere did not reuse the recorded answer: %+v", second)
}
}
+
+// TestCommandStrategyOrigin: a command is a rule unless it is configured as a model (origin = "model"),
+// for the command that calls a model; any other origin is refused.
+func TestCommandStrategyOrigin(t *testing.T) {
+ argv := toAny(helperArgv("copy", "{ours}", "{out}"))
+ for origin, want := range map[string]string{"": protocol.OriginRule, "rule": protocol.OriginRule, "model": protocol.OriginModel} {
+ opts := map[string]any{"command": argv}
+ if origin != "" {
+ opts["origin"] = origin
+ }
+ s, err := newCommand(StrategySpec{Name: "cmd", Type: "command", Options: opts})
+ if err != nil {
+ t.Fatalf("origin %q: %v", origin, err)
+ }
+ if got := originOf(s); got != want {
+ t.Errorf("origin %q: %q, want %q", origin, got, want)
+ }
+ }
+ if _, err := newCommand(StrategySpec{Name: "cmd", Type: "command", Options: map[string]any{"command": argv, "origin": "ai"}}); err == nil ||
+ !strings.Contains(err.Error(), "origin must be") {
+ t.Fatalf("origin ai: %v", err)
+ }
+
+ // Through a resolver: a command configured as a model gives a proposal, which is not recorded.
+ s, err := newCommand(StrategySpec{Name: "cmd", Type: "command", Options: map[string]any{"command": argv, "origin": "model"}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ r := newTestResolver(t, nil, Binding{Name: "cmd", Strategy: s})
+ resp, err := r.Resolve(context.Background(), request(conflict("a.go")))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(resp.Files) != 1 || resp.Files[0].Origin != protocol.OriginModel || !resp.Files[0].Proposal {
+ t.Fatalf("files %+v", resp.Files)
+ }
+}
diff --git a/tools/graft-agents/resolve/llm_strategy.go b/tools/graft-agents/resolve/llm_strategy.go
index 15c6ba7..38155e9 100644
--- a/tools/graft-agents/resolve/llm_strategy.go
+++ b/tools/graft-agents/resolve/llm_strategy.go
@@ -180,14 +180,6 @@ func (s *llmStrategy) Resolve(ctx context.Context, f File, req protocol.Request)
// syntaxChecked marks the llm answers for the syntax check (resolveFile).
func (s *llmStrategy) syntaxChecked() {}
-// proposer marks a strategy whose results are proposals: those of a model, which the server
-// proposes to the agent, and lands only when verify.command passed on the tree that lands
-// (PLAN10 14, 15.9).
-type proposer interface{ proposes() }
-
-// proposes marks the llm answers as proposals (resolveFile).
-func (s *llmStrategy) proposes() {}
-
// llmBytes keeps an empty side distinct from an absent one: ResolveLLM treats
// nil as absent.
func llmBytes(b []byte) []byte {
diff --git a/tools/graft-agents/resolve/rerere.go b/tools/graft-agents/resolve/rerere.go
index 34e7577..6c5c5fc 100644
--- a/tools/graft-agents/resolve/rerere.go
+++ b/tools/graft-agents/resolve/rerere.go
@@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"strconv"
+ "sync"
"github.com/feder-cr/git-ai/tools/graft-agents/protocol"
)
@@ -27,12 +28,31 @@ type rerere struct {
dir string
}
+// rerereVersion is the version of the entries this code writes and replays. Version 1 had no
+// version and no origin: the entries of v0.1.0 and v0.2.0 recorded the llm answers as well, so
+// nothing can tell a rule's entry from a model's among them. They are never replayed as a rule's.
+const rerereVersion = 2
+
type storedResolution struct {
+ // Version is rerereVersion; absent (0) in the entries written before it.
+ Version int `json:"version,omitempty"`
+ // Origin is who made the answer: protocol.OriginRule, the only one that is recorded.
+ Origin string `json:"origin,omitempty"`
Action string `json:"action"`
Mode string `json:"mode,omitempty"`
Content []byte `json:"content"`
}
+// rerereLegacy is called once per process, the first time the store shows an entry that is not
+// of the current version or not of a rule's: such an entry is skipped, and this is the line that
+// tells the operator why. Tests replace it.
+var rerereLegacy = func(dir string) {
+ fmt.Fprintf(os.Stderr, "graft-resolve: rerere store %s has entries of an older version or of no known rule origin; "+
+ "they are not replayed (clear the store to remove them)\n", dir)
+}
+
+var rerereLegacyOnce sync.Once
+
func newRerere(spec StrategySpec) (Strategy, error) {
if err := allowOnly(spec, "dir"); err != nil {
return nil, err
@@ -63,6 +83,10 @@ func rerereKey(f File) string {
return hex.EncodeToString(h.Sum(nil))
}
+// IsRule: rerere replays only the answers of rules (RuleStrategy); an entry of another origin, or
+// of no origin, is never replayed (see Resolve).
+func (rerere) IsRule() bool { return true }
+
func (r rerere) entryPath(key string) string {
return filepath.Join(r.dir, key+".json")
}
@@ -79,6 +103,13 @@ func (r rerere) Resolve(_ context.Context, f File, _ protocol.Request) (Resoluti
if err := json.Unmarshal(data, &s); err != nil {
return Resolution{}, false, fmt.Errorf("corrupt rerere entry: %w", err)
}
+ // Fail-safe: only an entry that says it is a rule's, in a version this code knows, is a rule's
+ // answer. The older entries may be a model's (v0.1.0 and v0.2.0 recorded them too), and the
+ // conflict goes on to the next strategy.
+ if s.Version != rerereVersion || s.Origin != protocol.OriginRule {
+ rerereLegacyOnce.Do(func() { rerereLegacy(r.dir) })
+ return Resolution{}, false, nil
+ }
res := Resolution{Action: s.Action, Mode: s.Mode, Content: s.Content}
switch s.Action {
case protocol.ActionDelete:
@@ -95,7 +126,7 @@ func (r rerere) Resolve(_ context.Context, f File, _ protocol.Request) (Resoluti
// Record stores an accepted resolution.
func (r rerere) Record(_ context.Context, f File, res Resolution) error {
- data, err := json.Marshal(storedResolution{Action: res.Action, Mode: res.Mode, Content: res.Content})
+ data, err := json.Marshal(storedResolution{Version: rerereVersion, Origin: protocol.OriginRule, Action: res.Action, Mode: res.Mode, Content: res.Content})
if err != nil {
return err
}
diff --git a/tools/graft-agents/resolve/rerere_test.go b/tools/graft-agents/resolve/rerere_test.go
index 0209a2a..7ccf1a8 100644
--- a/tools/graft-agents/resolve/rerere_test.go
+++ b/tools/graft-agents/resolve/rerere_test.go
@@ -101,10 +101,10 @@ func TestRerereRejectsCorruptEntries(t *testing.T) {
cases := map[string]string{
"not json": `{not json`,
- "bad action": `{"action":"explode","content":null}`,
- "bad mode": `{"action":"write","mode":"777","content":"eA=="}`,
- "missing mode": `{"action":"write","content":"eA=="}`,
- "bad base64": `{"action":"write","mode":"100644","content":"%%%"}`,
+ "bad action": `{"version":2,"origin":"rule","action":"explode","content":null}`,
+ "bad mode": `{"version":2,"origin":"rule","action":"write","mode":"777","content":"eA=="}`,
+ "missing mode": `{"version":2,"origin":"rule","action":"write","content":"eA=="}`,
+ "bad base64": `{"version":2,"origin":"rule","action":"write","mode":"100644","content":"%%%"}`,
}
for name, body := range cases {
t.Run(name, func(t *testing.T) {
@@ -181,3 +181,65 @@ func TestRerereFactoryValidation(t *testing.T) {
t.Fatal("unknown option accepted")
}
}
+
+// TestRerereEntryWithoutOriginIsNotARule: the v0.1.0 and v0.2.0 stores recorded the llm answers as well,
+// with no origin and no version. Such an entry is never replayed as a rule's answer (the conflict goes on
+// to the next strategy), whether it is a rule's or a model's, and the first one says so once. An entry of
+// a version this code does not know, and an entry of the current version that is not a rule's, are the same.
+func TestRerereEntryWithoutOriginIsNotARule(t *testing.T) {
+ dir := t.TempDir()
+ s := newTestRerere(t, dir)
+ f := conflict("a.go")
+ ctx := context.Background()
+ path := s.entryPath(rerereKey(f))
+
+ var notices []string
+ oldNotice := rerereLegacy
+ rerereLegacy = func(d string) { notices = append(notices, d) }
+ rerereLegacyOnce = sync.Once{}
+ t.Cleanup(func() { rerereLegacy = oldNotice; rerereLegacyOnce = sync.Once{} })
+
+ bodies := map[string]string{
+ "written before the version": `{"action":"write","mode":"100644","content":"bW9kZWwK"}`,
+ "a later version": `{"version":3,"origin":"rule","action":"write","mode":"100644","content":"eA=="}`,
+ "current version, a model": `{"version":2,"origin":"model","action":"write","mode":"100644","content":"eA=="}`,
+ "current version, no origin": `{"version":2,"action":"write","mode":"100644","content":"eA=="}`,
+ }
+ for name, body := range bodies {
+ if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if _, ok, err := s.Resolve(ctx, f, protocol.Request{}); err != nil || ok {
+ t.Fatalf("%s: replayed: ok=%v err=%v", name, ok, err)
+ }
+ }
+ if len(notices) != 1 || notices[0] != dir {
+ t.Fatalf("notices %v, want one for %s", notices, dir)
+ }
+
+ // Through a resolver the conflict goes on: the next strategy answers, and as a rule or a model by its own origin.
+ if err := os.WriteFile(path, []byte(bodies["written before the version"]), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ r := newTestResolver(t, nil, Binding{Name: "rr", Strategy: s}, Binding{Name: "next", Strategy: &proposingStrategy{fakeStrategy{fn: answer(protocol.ModeFile, "fresh\n")}}})
+ resp, err := r.Resolve(ctx, request(f))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(resp.Files) != 1 || string(resp.Files[0].Content) == "model\n" || resp.Files[0].Origin != protocol.OriginModel {
+ t.Fatalf("response %+v", resp.Files)
+ }
+
+ // What this code records is replayed: version 2, origin rule.
+ want := Resolution{Action: protocol.ActionWrite, Mode: protocol.ModeFile, Content: []byte("rule\n")}
+ if err := s.Record(ctx, f, want); err != nil {
+ t.Fatal(err)
+ }
+ raw, err := os.ReadFile(path)
+ if err != nil || !strings.Contains(string(raw), `"version":2`) || !strings.Contains(string(raw), `"origin":"rule"`) {
+ t.Fatalf("entry %s (%v)", raw, err)
+ }
+ if got, ok, err := s.Resolve(ctx, f, protocol.Request{}); err != nil || !ok || string(got.Content) != "rule\n" {
+ t.Fatalf("replay: %+v ok=%v err=%v", got, ok, err)
+ }
+}
diff --git a/tools/graft-agents/resolve/resolve.go b/tools/graft-agents/resolve/resolve.go
index 6f5db6c..6e9b86d 100644
--- a/tools/graft-agents/resolve/resolve.go
+++ b/tools/graft-agents/resolve/resolve.go
@@ -72,6 +72,31 @@ type syntaxChecked interface {
syntaxChecked()
}
+// RuleStrategy is implemented by the strategies whose answers are a rule's: deterministic code
+// that the operator trusts (trivial, union, rerere, and command unless it is configured as a
+// model). Their resolutions land on the server without the operator's verify.command.
+//
+// The default is the other way round, fail-safe at this layer: a strategy that does not
+// implement RuleStrategy, or whose IsRule says false, is a MODEL. That is the llm strategy and
+// every strategy that another package registers with RegisterStrategy. Its answers are marked
+// origin "model", and the server lands them only if verify.command passed on the exact merged
+// tree (PLAN10 14, 15.9). A strategy that implements RuleStrategy claims that its answers are
+// a rule's, and that claim is the operator's trust: do not write it for anything that asks a
+// model, or reads what a model wrote.
+type RuleStrategy interface {
+ Strategy
+ // IsRule reports whether the answers of this strategy are a rule's.
+ IsRule() bool
+}
+
+// originOf is the origin of the answers of s: a rule's only when s says so.
+func originOf(s Strategy) string {
+ if r, ok := s.(RuleStrategy); ok && r.IsRule() {
+ return protocol.OriginRule
+ }
+ return protocol.OriginModel
+}
+
// Recorder is implemented by strategies that want to learn accepted answers.
type Recorder interface {
Record(ctx context.Context, f File, res Resolution) error
@@ -451,17 +476,14 @@ func (r *Resolver) resolveFile(ctx context.Context, req protocol.Request, f File
continue
}
// A model resolution is a proposal: the server lands it only after verify.command passed on
- // the tree that lands (PLAN10 14, 15.9). It is not
- // recorded either: a strategy that learns accepted answers (Recorder) would otherwise
- // replay a model's resolution later as a rule's.
- _, proposal := b.Strategy.(proposer)
+ // the tree that lands (PLAN10 14, 15.9). It is not recorded either: a strategy that learns
+ // accepted answers (Recorder) would otherwise replay a model's resolution later as a
+ // rule's. What is not a rule (originOf) is a model.
+ origin := originOf(b.Strategy)
+ proposal := origin == protocol.OriginModel
if !proposal {
r.record(ctx, f, res)
}
- origin := protocol.OriginRule
- if proposal {
- origin = protocol.OriginModel
- }
return protocol.ResolvedFile{
Path: f.Path,
Action: res.Action,
diff --git a/tools/graft-agents/resolve/resolve_test.go b/tools/graft-agents/resolve/resolve_test.go
index a018cfb..4d40db1 100644
--- a/tools/graft-agents/resolve/resolve_test.go
+++ b/tools/graft-agents/resolve/resolve_test.go
@@ -24,6 +24,17 @@ func (s *fakeStrategy) Resolve(_ context.Context, f File, _ protocol.Request) (R
return s.fn(f)
}
+// IsRule: the fake stands for a rule (RuleStrategy); the tests of a model use proposingStrategy, and the
+// tests of a strategy that says nothing use externalStrategy.
+func (s *fakeStrategy) IsRule() bool { return true }
+
+// externalStrategy is a strategy as another package registers it: it implements Strategy and nothing else.
+type externalStrategy struct{}
+
+func (externalStrategy) Resolve(context.Context, File, protocol.Request) (Resolution, bool, error) {
+ return Resolution{Action: protocol.ActionWrite, Mode: protocol.ModeFile, Content: []byte("external\n")}, true, nil
+}
+
// recordingStrategy also implements Recorder.
type recordingStrategy struct {
fakeStrategy
@@ -591,10 +602,47 @@ func TestConcurrentResolve(t *testing.T) {
// proposingStrategy answers like fakeStrategy, as a model does: its answers are proposals.
type proposingStrategy struct{ fakeStrategy }
-func (*proposingStrategy) proposes() {}
+// IsRule: not a rule.
+func (*proposingStrategy) IsRule() bool { return false }
+
+// TestStrategyOriginDefaultsToModel: fail-safe at the resolver's layer. A strategy that another
+// package registers (RegisterStrategy) and does not say it is a rule gives a model's answers: origin
+// "model", a proposal, not recorded. The built-in rules, and a command that is configured as one,
+// are rules; the llm strategy and a command configured as a model are not.
+func TestStrategyOriginDefaultsToModel(t *testing.T) {
+ rec := &recordingStrategy{fakeStrategy: fakeStrategy{fn: decline}}
+ r := newTestResolver(t, nil, Binding{Name: "rec", Strategy: rec}, Binding{Name: "ext", Strategy: externalStrategy{}})
+ resp, err := r.Resolve(context.Background(), request(conflict("a.go")))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(resp.Files) != 1 || resp.Files[0].Origin != protocol.OriginModel || !resp.Files[0].Proposal {
+ t.Fatalf("files %+v, want one answer of origin %q", resp.Files, protocol.OriginModel)
+ }
+ if len(rec.recorded) != 0 {
+ t.Fatalf("an external strategy's answer was recorded: %+v", rec.recorded)
+ }
+
+ for name, s := range map[string]Strategy{
+ "trivial": trivial{}, "union": union{}, "rerere": rerere{dir: t.TempDir()},
+ "command": command{argv: []string{"x"}},
+ "command-model": command{argv: []string{"x"}, model: true},
+ "llm": &llmStrategy{},
+ "external": externalStrategy{},
+ } {
+ want := protocol.OriginModel
+ switch name {
+ case "trivial", "union", "rerere", "command":
+ want = protocol.OriginRule
+ }
+ if got := originOf(s); got != want {
+ t.Errorf("%s: origin %q, want %q", name, got, want)
+ }
+ }
+}
-// The llm strategy's answers are proposals (PLAN10 14).
-var _ proposer = (*llmStrategy)(nil)
+// The llm strategy's answers are a model's (PLAN10 14): it is not a RuleStrategy.
+var _ = func() bool { _, rule := Strategy(&llmStrategy{}).(RuleStrategy); return !rule }()
// TestProposalsAreMarkedAndNotRecorded: a model's answer is marked as a proposal, which the server
// lands only after verification, and no Recorder learns it, so that it can never come back as a rule's answer; a
diff --git a/tools/graft-agents/resolve/union.go b/tools/graft-agents/resolve/union.go
index 1cf72ad..0595d35 100644
--- a/tools/graft-agents/resolve/union.go
+++ b/tools/graft-agents/resolve/union.go
@@ -29,6 +29,9 @@ func newUnion(spec StrategySpec) (Strategy, error) {
return union{}, nil
}
+// IsRule: union is a rule (RuleStrategy).
+func (union) IsRule() bool { return true }
+
func (union) Resolve(_ context.Context, f File, _ protocol.Request) (Resolution, bool, error) {
if !regularFile(f.Ours) || !regularFile(f.Theirs) || f.Ours.Mode != f.Theirs.Mode {
return Resolution{}, false, nil