Skip to content

Git designed for AI agents.

land: hide refs/graft/ from agents; proposals never fail the package

Federico Elia6090d5b0bd6b
diff --git a/doc/graft/AGENT-SECURITY.md b/doc/graft/AGENT-SECURITY.md
index 4650bb3..09cdb35 100644
--- a/doc/graft/AGENT-SECURITY.md
+++ b/doc/graft/AGENT-SECURITY.md
@@ -59,6 +59,18 @@ These can never be enabled, not even through `graft.agent_rpcs`:
- `CleanupService`, `ObjectPoolService`, `HookService`, `InternalGitaly`,
`PartitionService`.
+An agent does not see `refs/graft/` at all (`internal/graft/agentrefs`): the
+agent guard refuses a request whose revision, reference or pattern names a
+reference under it (including the short forms `graft/proposals/...` and
+`graft/green/...`, which git completes to the server's namespaces; other
+`graft/...` short names, such as the workspace's temporary branches, are
+branches), and removes such references from every response; git upload-pack and
+receive-pack hide the prefix from agents. A test classifies every string or
+bytes field of every RPC agents may call by default, so a new field cannot
+escape the filter; RPCs added with `graft.agent_rpcs` are checked by field
+name. Other callers still see graft-verify's green references, which are
+documented for consumers.
+
Whatever the RPC and whatever the policy says, an agent never creates,
updates or deletes a reference under `refs/graft/`: the reference-transaction
hook refuses it, so a push, `UpdateReferences`, `DeleteRefs`, `WriteRef` or an
diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.md
index 8225137..277c368 100644
--- a/doc/graft/CONFIGURATION.md
+++ b/doc/graft/CONFIGURATION.md
@@ -196,7 +196,7 @@ cause.
| `timeout` | string | `"120s"` | Bound on one resolver call (Go duration). |
| `max_files` | int | `20` | Conflicted files per land. A land with more is not sent to the resolver. |
| `max_file_bytes` | int | `1048576` (1 MiB) | Bound on every side of every file sent and on every resolved file accepted. At most 12566528 (about 12 MiB): one file, its three sides and its resolution, must fit one check request. |
-| `proposal_ttl` | string | `"168h"` | How long the proposals of a failed land are kept under `refs/graft/proposals/` (Go duration); at most 20 are kept per branch. |
+| `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`. |
| `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. |
diff --git a/doc/graft/PLAN10.md b/doc/graft/PLAN10.md
index 9252650..737077b 100644
--- a/doc/graft/PLAN10.md
+++ b/doc/graft/PLAN10.md
@@ -755,8 +755,9 @@ flag.
of that commit. On every land of a branch that asks for or brings a resolution (a plain land pays
nothing; only those lands write proposals) the server deletes the branch's proposal references
older than `proposal_ttl` (default 7 days) and keeps at most 20 per branch; the agent's land of
- the change drops its proposal. A proposal reference
- that cannot be written fails the land with Internal, not with a package without proposals.
+ the change drops its proposal. When the proposals cannot be stored the package still goes to
+ the agent, each file of the answer saying "proposal not stored: <cause>" (logged and counted):
+ the proposals are an aid, the package is the contract (review ruling M2).
* **(a) Authoritative storage mode (checked).** UserLand updates references through the
reference-transaction hook (STORAGE.md, the voter): the proposal reference and its expiry
deletions go through it like the branch, so in authoritative mode each is a changeset committed
@@ -800,6 +801,40 @@ flag.
example policy that gives the agent no refs/graft/ rule: the server proposes, the SDK reads the
proposal, the agent lands it as its own (Graft-Resolved-By: agent/<actor>), and the proposal
reference is dropped.
+* **Review of e33a723 + b54b529 (PM rulings), implemented.**
+ * M1, condition 3: refs/graft/** is internal for agents. One rule (internal/graft/agentrefs),
+ applied once per transport: the agent guard of the gRPC server refuses a request whose
+ revision, reference or pattern names refs/graft/ (the short forms graft/proposals/... and
+ graft/green/..., which git completes to the server's namespaces, included; other graft/...
+ short names are branches, the workspace's temporary branches graft/<agent>/... among them,
+ which the first, broader rule refused and the suite caught), and removes such references
+ from every response; git
+ upload-pack and receive-pack hide the prefix from agents. A table-driven test classifies every
+ string or bytes field of every RPC agents may call by default (a new one fails it); RPCs added
+ with agent_rpcs are checked by field name. graft-verify's refs/graft/green/ stays advertised
+ to other callers: VERIFY.md documents it for consumers (the newest tested tip), which are not
+ agents; agents do not see it.
+ * M2: a proposal that cannot be stored no longer turns the conflict into Internal: the package
+ comes without it, each file of the answer saying "proposal not stored: <cause>", logged and
+ counted (graft_land_proposal_write_errors_total).
+ * M3: the proposals' commit is dated by the server's clock, never the request's timestamp; the
+ housekeeping pass prunes the proposals of every branch (WithGraftProposalTTL), so idle and
+ deleted branches expire, except in a repository housekept in a WAL transaction, where a
+ reference update would bypass the transaction (logged; those expire through their branch's
+ lands); an agent the policy does not let update the branch gets PermissionDenied before
+ anything is resolved, and no proposal is kept.
+ * m1: every deletion expects the target it read (internal/graft/proposals, the one writer and
+ the one pruner, used by UserLand and housekeeping). m2: decideLand is the one decision point,
+ with the guard "a file that proposes is never landed"; that is where a future gate lives.
+ m4: the Resolver and Requested-By values pass landTrailerValueValid, and are quoted
+ otherwise (the user ID is the caller's). m5: the e2e test names the llm strategy "model", so
+ the flag, not the name fallback, makes the proposal; the release note (the PM's file) says
+ that rerere entries recorded from llm answers before b54b529 must be cleared.
+ * m6, measured: writing the proposals adds a median of 97 ms (min 69, p90 113, max 134 ms on the
+ test machine) to the lane's hold; the resolver's own call, already in the lane for
+ resolve_conflicts, takes seconds. Moving both out of the lane, like the precheck of an agent's
+ resolution, is a step of its own.
+
* **Amendment: Federico's product decision (2026-10-19, relayed by the PM).** "When the server AI
is configured, the server always tries to resolve, and it LANDS its resolution by itself ONLY if
the proposal passes every deterministic check: syntax, anchors, no markers, and the composition
@@ -854,12 +889,16 @@ not exist yet; (not enabled) marks what is designed and measured but switched of
list the conflicting files (bounded) -> one VERDICT per file (15.2)
|
v
+ agent may not update the branch (policy) -> PermissionDenied, nothing resolved
decide: LAND only if every file is resolved by an operator rule (15.4)
|-- yes -> operator verify.command; passes -> merge commit signed by the rules -> LAND
| fails -> PACKAGE (every file REJECTED, reason)
|-- no -> PACKAGE to the agent (merge_conflict detail): sides by OID, hunks,
per-file outcome + reason, per-file proposal (+ its report, planned),
- the proposals kept under refs/graft/proposals/ (14)
+ the proposals kept under refs/graft/proposals/ (14), or, when they
+ cannot be stored, "proposal not stored: <cause>" on each file
+ every request and response of an agent: the refs/graft/ visibility filter (agentrefs);
+ git upload-pack / receive-pack of an agent: refs/graft/ hidden
|
v
the agent resolves (takes, edits or replaces the proposals) and
@@ -926,7 +965,7 @@ not exist yet; (not enabled) marks what is designed and measured but switched of
### 15.4 The one decision point
```
- decide (today: resolveLandConflicts, with landAnswerProposes; one function, planned: decideLand)
+ decideLand (resolveLandConflicts calls it; landAnswerProposes is its guard)
every file resolved by an operator rule ...... LAND (then verify.command)
anything else, including any model answer .... PACKAGE (the model's files are proposals)
No other code path lands a model's resolution. No check disables itself: a configuration that
@@ -943,7 +982,8 @@ 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`, `landAnswerProposes` (planned: `decideLand`) | operations/land_resolve.go, land_proposals.go |
+| per-file verdicts, decide | `resolveLandConflicts`, `decideLand`, `landAnswerProposes` | operations/land_resolve.go, land_proposals.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 |
| AI per file | `llmStrategy.Resolve`, `ResolveLLM`, `llmResolvePatch` | tools/.../resolve/llm_strategy.go, llm_core.go, llm_patch.go |
@@ -953,7 +993,10 @@ not exist yet; (not enabled) marks what is designed and measured but switched of
| verify.command | `landVerify`, `verifyRejected` | operations/land_verify.go, land_resolve.go |
| merge commit, trailers | `landWriteResolution` | operations/land_resolve.go |
| PACKAGE | `landWithPackage`, `landListing.conflictPackage` | operations/land_package.go |
-| proposals, hidden reference | `landWriteProposals`, `landUpdateProposalRefs`, `landPruneProposals` | operations/land_proposals.go |
+| proposals, hidden reference | `landWriteProposals`, `proposals.Update`, `proposals.Prune`, `landProposalsNotStored` | operations/land_proposals.go; graft/proposals |
+| proposals' expiry in housekeeping | `RepositoryManager.pruneGraftProposals` | git/housekeeping/manager/optimize_repository.go |
+| refs/graft/ visibility filter (agents) | `checkAgentRefs`, `filterAgentRefs`, `agentRefField`; `agentrefs.RevisionHidden` | gitaly/server/auth/agent_refs.go; graft/agentrefs |
+| refs/graft/ hidden from agents' git | `hiddenUploadPackRefPrefixes`, `hiddenReceivePackRefPrefixes` | git/gitcmd/command_description.go |
| refs/graft/ reserved for the server | `graftReservedDenial` | gitaly/hook/referencetransaction.go |
| composition report | (planned) | (planned: graft/resolver, with a parity-tested copy in tools/) |
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index 070b66d..0ca39e7 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -174,17 +174,24 @@ The proposals are kept under a hidden reference,
`refs/graft/proposals/<first 16 hex of sha256(branch)>/<commit>`, named in the
package's `proposal_ref`: a commit whose tree holds the proposed files by
path, with parents `[tip, commit]`, whose message names the resolver and
-`Requested-By:` the actor of the land. One reference per branch and landed
+`Requested-By:` the actor of the land, and whose date is the server's clock
+(never the request's timestamp). One reference per branch and landed
commit, replaced by every failed land of that commit. The server writes it
itself, through the reference transaction like any update (and so the storage
core in authoritative mode), and refuses to write any other reference this
-way. The agent reads the proposals by OID with `GetBlobs`; it cannot see the
-reference in an advertisement nor push to it, and no agent may update
-anything under `refs/graft/`, whatever the policy says. Every land that asks
-for resolution or brings one prunes the branch's proposals older than
-`proposal_ttl` (default 7 days) and keeps at most 20; the agent's land of the
-change drops that change's proposal. A proposal that cannot be written fails
-the land with `Internal`, never with a package that lacks it.
+way. The agent reads the proposals by OID with `GetBlobs`. Nothing under
+`refs/graft/` is visible to an agent: no ref RPC lists it or accepts a name
+under it, upload-pack does not advertise it, and no agent may update it,
+whatever the policy says (see [AGENT-SECURITY](AGENT-SECURITY.md)). An agent
+the policy does not let update the branch gets `PermissionDenied` before
+anything is resolved, and no proposal is kept for it. Every land that asks for
+resolution or brings one prunes the branch's proposals older than
+`proposal_ttl` (default 7 days) and keeps at most 20, and the housekeeping
+pass prunes those of every branch, idle or deleted ones included; the agent's
+land of the change drops that change's proposal. The proposals are an aid and
+the package is the contract: when they cannot be stored, the package still
+comes, each file of the answer saying `proposal not stored: <cause>`, and the
+failure is logged and counted (`graft_land_proposal_write_errors_total`).
Every resolution is logged at info level as `graft: conflicts of land
resolved`, with the branch, the tip, the landed commit, the merge commit, the
diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.go
index 24225f2..02838b3 100644
--- a/internal/cli/gitaly/serve.go
+++ b/internal/cli/gitaly/serve.go
@@ -533,8 +533,14 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {
defer func() { _ = mvccCacheFactory.Close(ctx) }()
prometheus.MustRegister(mvccCacheFactory)
+ // The proposals of the land expire in the housekeeping pass too, so that those of idle and
+ // deleted branches go (PLAN10 14). proposal_ttl was validated with the configuration.
+ proposalTTL, err := cfg.Graft.Land.Resolver.ProposalTTLOrDefault()
+ if err != nil {
+ return fmt.Errorf("graft land resolver: %w", err)
+ }
housekeepingManager := housekeepingmgr.New(cfg.Prometheus, logger, transactionManager, nil,
- housekeepingmgr.WithMVCCCacheSupport(mvccCacheFactory))
+ housekeepingmgr.WithMVCCCacheSupport(mvccCacheFactory), housekeepingmgr.WithGraftProposalTTL(proposalTTL))
prometheus.MustRegister(housekeepingManager)
housekeepingMiddlewareConfig := housekeepingmw.DefaultMiddlewareConfig()
diff --git a/internal/git/gitcmd/command_description.go b/internal/git/gitcmd/command_description.go
index 34d6118..bae5fb0 100644
--- a/internal/git/gitcmd/command_description.go
+++ b/internal/git/gitcmd/command_description.go
@@ -8,6 +8,8 @@ import (
"strings"
"github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/graft/agentrefs"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
"github.com/feder-cr/git-ai/internal/structerr"
)
@@ -451,7 +453,11 @@ func validatePositionalArg(arg string) error {
}
func hiddenReceivePackRefPrefixes(ctx context.Context) []GlobalOption {
- config := make([]GlobalOption, 0, len(git.InternalRefPrefixes))
+ config := make([]GlobalOption, 0, len(git.InternalRefPrefixes)+1)
+ // An agent does not see refs/graft/ at all (agentrefs); others see what the prefixes allow.
+ if id, ok := identity.FromContext(ctx); ok && id.Source == identity.SourceAgent {
+ config = append(config, ConfigPair{Key: "receive.hideRefs", Value: agentrefs.HiddenPrefix})
+ }
for refPrefix, refType := range git.InternalRefPrefixes {
switch refType {
@@ -467,8 +473,13 @@ func hiddenReceivePackRefPrefixes(ctx context.Context) []GlobalOption {
return config
}
-func hiddenUploadPackRefPrefixes(context.Context) []GlobalOption {
- config := make([]GlobalOption, 0, len(git.InternalRefPrefixes))
+func hiddenUploadPackRefPrefixes(ctx context.Context) []GlobalOption {
+ config := make([]GlobalOption, 0, len(git.InternalRefPrefixes)+1)
+ // An agent does not see refs/graft/ at all (agentrefs), graft-verify's green references
+ // included, which other clients may fetch.
+ if id, ok := identity.FromContext(ctx); ok && id.Source == identity.SourceAgent {
+ config = append(config, ConfigPair{Key: "uploadpack.hideRefs", Value: agentrefs.HiddenPrefix})
+ }
for refPrefix, refType := range git.InternalRefPrefixes {
switch refType {
diff --git a/internal/git/gitcmd/command_description_agentrefs_test.go b/internal/git/gitcmd/command_description_agentrefs_test.go
new file mode 100755
index 0000000..1077025
--- /dev/null
+++ b/internal/git/gitcmd/command_description_agentrefs_test.go
@@ -0,0 +1,34 @@
+package gitcmd
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+)
+
+// TestHiddenRefPrefixes_agents: git upload-pack and receive-pack hide refs/graft/ from an agent
+// (agentrefs), on top of the internal prefixes every caller has hidden; another caller sees what
+// the internal prefixes allow, graft-verify's green references included.
+func TestHiddenRefPrefixes_agents(t *testing.T) {
+ t.Parallel()
+
+ values := func(opts []GlobalOption, key string) []string {
+ var out []string
+ for _, o := range opts {
+ if p, ok := o.(ConfigPair); ok && p.Key == key {
+ out = append(out, p.Value)
+ }
+ }
+ return out
+ }
+ agent := identity.NewContext(context.Background(), identity.Identity{Name: "agent-a", Source: identity.SourceAgent})
+ other := context.Background()
+
+ require.Contains(t, values(hiddenUploadPackRefPrefixes(agent), "uploadpack.hideRefs"), "refs/graft/")
+ require.Contains(t, values(hiddenReceivePackRefPrefixes(agent), "receive.hideRefs"), "refs/graft/")
+ require.NotContains(t, values(hiddenUploadPackRefPrefixes(other), "uploadpack.hideRefs"), "refs/graft/")
+ require.Contains(t, values(hiddenUploadPackRefPrefixes(other), "uploadpack.hideRefs"), "refs/graft/proposals/")
+}
diff --git a/internal/git/housekeeping/manager/manager.go b/internal/git/housekeeping/manager/manager.go
index 4481b47..a142ae4 100644
--- a/internal/git/housekeeping/manager/manager.go
+++ b/internal/git/housekeeping/manager/manager.go
@@ -1,6 +1,8 @@
package manager
import (
+ "time"
+
"context"
"sync"
@@ -198,11 +200,23 @@ type RepositoryManager struct {
// cacheFactory allows housekeeping to run in the context of an MVCC cache.
cacheFactory *mvcc.CacheFactory
+ // proposalTTL, when set, makes every housekeeping pass prune the proposal references of the
+ // land (refs/graft/proposals/) older than it, on every branch (WithGraftProposalTTL).
+ proposalTTL time.Duration
}
// Option configures a RepositoryManager.
type Option func(*RepositoryManager)
+// WithGraftProposalTTL makes every housekeeping pass prune the proposals of the land
+// (proposals.Prune over refs/graft/proposals/) older than ttl and beyond the cap of each branch,
+// so that the proposals of idle and deleted branches expire too (PLAN10 14).
+func WithGraftProposalTTL(ttl time.Duration) Option {
+ return func(m *RepositoryManager) {
+ m.proposalTTL = ttl
+ }
+}
+
// WithMVCCCacheSupport enables MVCC-snapshot-aware housekeeping for background
// OptimizeRepository runs (daily maintenance, scheduled housekeeping) that do not
// pass through the mvcc gRPC middleware. cacheFactory should be shared with the
diff --git a/internal/git/housekeeping/manager/optimize_repository.go b/internal/git/housekeeping/manager/optimize_repository.go
index c67284f..4659e37 100644
--- a/internal/git/housekeeping/manager/optimize_repository.go
+++ b/internal/git/housekeeping/manager/optimize_repository.go
@@ -5,6 +5,9 @@ import (
"context"
"errors"
"fmt"
+ "github.com/feder-cr/git-ai/internal/graft/proposals"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
"os"
"time"
@@ -101,11 +104,18 @@ func (m *RepositoryManager) OptimizeRepository(
}
if tx != nil {
+ if m.proposalTTL > 0 {
+ // A reference update here would bypass the transaction: the proposals then expire
+ // only through the lands of their branch (PLAN10 14).
+ m.logger.InfoContext(ctx, "graft: proposal pruning skipped: the repository is housekept in a transaction")
+ }
return m.optimizeRepositoryWithTransaction(ctx, repo, cfg)
}
return m.withMVCCCache(ctx, repo, func(ctx context.Context) error {
- return m.optimizeRepository(ctx, repo, cfg)
+ err := m.optimizeRepository(ctx, repo, cfg)
+ m.pruneGraftProposals(ctx, repo)
+ return err
})
}); err != nil {
return err
@@ -614,3 +624,27 @@ func (m *RepositoryManager) CleanStaleData(ctx context.Context, repo *localrepo.
return nil
}
+
+// graftProposalPruneErrors counts the housekeeping passes whose pruning of the land's proposals
+// failed; the pass itself does not fail for it.
+var graftProposalPruneErrors = promauto.NewCounter(prometheus.CounterOpts{
+ Name: "graft_housekeeping_proposal_prune_errors_total",
+ Help: "Housekeeping passes whose pruning of the proposal references of the land failed.",
+})
+
+// pruneGraftProposals prunes the proposals of the land on every branch (WithGraftProposalTTL). A
+// failure is logged and counted, and fails nothing: the proposals are an aid.
+func (m *RepositoryManager) pruneGraftProposals(ctx context.Context, repo *localrepo.Repo) {
+ if m.proposalTTL <= 0 {
+ return
+ }
+ n, err := proposals.Prune(ctx, repo, proposals.RefPrefix, m.proposalTTL, "", time.Now())
+ if err != nil {
+ graftProposalPruneErrors.Inc()
+ m.logger.WithError(err).ErrorContext(ctx, "graft: pruning the proposals of the land failed")
+ return
+ }
+ if n > 0 {
+ m.logger.WithField("pruned", n).InfoContext(ctx, "graft: pruned the expired proposals of the land")
+ }
+}
diff --git a/internal/git/housekeeping/manager/optimize_repository_proposals_ext_test.go b/internal/git/housekeeping/manager/optimize_repository_proposals_ext_test.go
new file mode 100755
index 0000000..d043323
--- /dev/null
+++ b/internal/git/housekeeping/manager/optimize_repository_proposals_ext_test.go
@@ -0,0 +1,50 @@
+package manager_test
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ housekeepingmgr "github.com/feder-cr/git-ai/internal/git/housekeeping/manager"
+ "github.com/feder-cr/git-ai/internal/git/localrepo"
+ gitalycfgprom "github.com/feder-cr/git-ai/internal/gitaly/config/prometheus"
+ "github.com/feder-cr/git-ai/internal/graft/proposals"
+ "github.com/feder-cr/git-ai/internal/helper/text"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"
+)
+
+// TestOptimizeRepository_prunesGraftProposals: the housekeeping pass expires the proposals of the
+// land on every branch, those of a branch nothing lands on any more included, and keeps the
+// fresh ones; without the option it leaves them.
+func TestOptimizeRepository_prunesGraftProposals(t *testing.T) {
+ t.Parallel()
+
+ ctx := testhelper.Context(t)
+ cfg := testcfg.Build(t)
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})
+ repo := localrepo.NewTestRepo(t, cfg, repoProto)
+ now := time.Now()
+ zero := gittest.DefaultObjectHash.ZeroOID
+ old := git.ReferenceName(fmt.Sprintf("%s%040x", proposals.BranchDir("idle"), 1))
+ fresh := git.ReferenceName(fmt.Sprintf("%s%040x", proposals.BranchDir("main"), 2))
+ require.NoError(t, proposals.Update(ctx, repo, []proposals.Op{
+ {Name: old, NewOID: gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("old"), gittest.WithCommitterDate(now.Add(-30*24*time.Hour))), OldOID: zero},
+ {Name: fresh, NewOID: gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("fresh"), gittest.WithCommitterDate(now.Add(-time.Hour))), OldOID: zero},
+ }))
+ refs := func() []string {
+ return strings.Fields(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "for-each-ref", "--format=%(refname)", proposals.RefPrefix)))
+ }
+
+ require.NoError(t, housekeepingmgr.New(gitalycfgprom.Config{}, testhelper.SharedLogger(t), nil, nil).OptimizeRepository(ctx, repo))
+ require.ElementsMatch(t, []string{old.String(), fresh.String()}, refs(), "without the option nothing is pruned")
+
+ manager := housekeepingmgr.New(gitalycfgprom.Config{}, testhelper.SharedLogger(t), nil, nil, housekeepingmgr.WithGraftProposalTTL(7*24*time.Hour))
+ require.NoError(t, manager.OptimizeRepository(ctx, repo))
+ require.Equal(t, []string{fresh.String()}, refs(), "the idle branch's old proposal expired")
+}
diff --git a/internal/gitaly/server/auth/agent.go b/internal/gitaly/server/auth/agent.go
index 8c30f28..efa058f 100644
--- a/internal/gitaly/server/auth/agent.go
+++ b/internal/gitaly/server/auth/agent.go
@@ -105,7 +105,14 @@ func InterceptorsWithAgentRPCs(conf gitalycfgauth.Config, agents []config.GraftA
if err := checkAgentRequest(ctx, req); err != nil {
return nil, err
}
- return handler(ctx, req)
+ if err := checkAgentRefs(ctx, req); err != nil {
+ return nil, err
+ }
+ resp, err := handler(ctx, req)
+ if err == nil {
+ filterAgentRefs(ctx, resp)
+ }
+ return resp, err
})
}
@@ -176,7 +183,8 @@ func checkAgentRequest(ctx context.Context, req any) error {
return nil
}
-// agentRequestGuardStream checks every message an agent sends on a stream.
+// agentRequestGuardStream checks every message an agent sends on a stream, and
+// removes from every message it receives the references it may not see.
type agentRequestGuardStream struct {
grpc.ServerStream
}
@@ -188,7 +196,16 @@ func (s *agentRequestGuardStream) RecvMsg(m any) error {
return err
}
- return checkAgentRequest(s.Context(), m)
+ if err := checkAgentRequest(s.Context(), m); err != nil {
+ return err
+ }
+ return checkAgentRefs(s.Context(), m)
+}
+
+// SendMsg sends m without the references under refs/graft/ it lists.
+func (s *agentRequestGuardStream) SendMsg(m any) error {
+ filterAgentRefs(s.Context(), m)
+ return s.ServerStream.SendMsg(m)
}
// loadAgentCredentials validates the agent configuration and resolves every
diff --git a/internal/gitaly/server/auth/agent_refs.go b/internal/gitaly/server/auth/agent_refs.go
new file mode 100644
index 0000000..1218f58
--- /dev/null
+++ b/internal/gitaly/server/auth/agent_refs.go
@@ -0,0 +1,267 @@
+package auth
+
+import (
+ "context"
+ "strings"
+
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/reflect/protoreflect"
+
+ "github.com/feder-cr/git-ai/internal/graft/agentrefs"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+ "github.com/feder-cr/git-ai/internal/structerr"
+)
+
+// The agent reference-visibility filter (agentrefs): an agent neither names nor lists a reference
+// under refs/graft/. It is applied here, once, to every request and response of an agent:
+// - a request field that names a revision, a reference or a reference pattern is refused when it
+// names refs/graft/ (agentrefs.RevisionHidden);
+// - a response loses every reference under refs/graft/ it lists: a list element that is such a
+// name, or a message that names one in a reference field.
+// Which fields name references is decided by agentRefField: an exact table for the messages of
+// the RPCs agents may call by default (a test fails on any string or bytes field that the table
+// does not classify), and a conservative rule by field name for the RPCs an operator adds with
+// [graft] agent_rpcs.
+
+// refFieldKind is what a string or bytes field of a request or a response holds.
+type refFieldKind int
+
+const (
+ notRef refFieldKind = iota
+ // isRef: a revision, a reference or a reference pattern: checked, or filtered.
+ isRef
+ // isBranchName: a branch name the server completes under refs/heads/ when it is short; only a
+ // full name under refs/graft/ is refused (no revision rule completes it to refs/graft/).
+ isBranchName
+)
+
+// agentRefFieldsByName classifies a field by its name alone.
+var agentRefFieldsByName = map[protoreflect.Name]refFieldKind{
+ // Revisions, references and patterns of requests, and references of responses.
+ "commit": isRef, "commit_id": isRef, "commit_ids": isRef, "commit_revision": isRef, "commits": isRef,
+ "expected_head_commit_id": isRef, "expected_old_oid": isRef, "head_commit_id": isRef, "base_commit_id": isRef,
+ "include_referenced_by": isRef, "left_commit_id": isRef, "right_commit_id": isRef,
+ "left_tree_revision": isRef, "right_tree_revision": isRef, "new_revision": isRef, "old_revision": isRef,
+ "oid": isRef, "oids": isRef, "parent_commit_revisions": isRef, "patterns": isRef, "pointing_at_oids": isRef,
+ "ref_names": isRef, "ref_patterns": isRef, "resume_oid": isRef, "revision": isRef, "revisions": isRef,
+ "start_point": isRef, "start_sha": isRef, "tag_ids": isRef, "tag_revisions": isRef, "tip": isRef,
+ "tree_oid": isRef, "blob_ids": isRef, "refs": isRef, "ref_name": isRef, "reference": isRef,
+ "referenced_by": isRef, "branch_names": isRef, "tag_names": isRef, "target": isRef,
+ // Branch and tag names: the server completes them under refs/heads/ and refs/tags/, so they
+ // cannot name refs/graft/; and fields that hold no reference.
+ "branch": notRef, "branch_name": notRef, "start_branch_name": notRef, "target_branch": notRef,
+ "merged_branches": notRef, "tag_name": notRef, "remote_name": notRef, "exclude_patterns": notRef,
+ "author": notRef, "body": notRef, "commit_author_email": notRef, "commit_author_name": notRef,
+ "commit_message": notRef, "commit_message_patterns": notRef, "content": notRef, "description": notRef,
+ "email": notRef, "file_path": notRef, "git_alternate_object_directories": notRef,
+ "git_object_directory": notRef, "gl_id": notRef, "gl_project_path": notRef, "gl_repository": notRef,
+ "gl_username": notRef, "id": notRef, "merge_message": notRef, "message_regex": notRef, "mode": notRef,
+ "page_token": notRef, "path": notRef, "paths": notRef, "previous_path": notRef, "query": notRef,
+ "relative_path": notRef, "sort_field": notRef, "storage_name": notRef, "timezone": notRef, "title": notRef,
+ "actor": notRef, "commit_oid": notRef, "data": notRef, "encoding": notRef, "file_oid": notRef,
+ "filesystem_id": notRef, "flat_path": notRef, "following_lines": notRef, "fs_type": notRef,
+ "git_version": notRef, "index_error": notRef, "key": notRef, "landed_commit_id": notRef, "line": notRef,
+ "message": notRef, "new_blob_id": notRef, "new_oid": notRef, "next_cursor": notRef, "old_blob_id": notRef,
+ "old_oid": notRef, "old_path": notRef, "old_tip": notRef, "operation": notRef, "original_commit_id": notRef,
+ "parent_ids": notRef, "patch_id": notRef, "path_bytes": notRef, "peeled_target": notRef,
+ "pre_receive_error": notRef, "preceding_lines": notRef, "reason": notRef, "resolved_paths": notRef,
+ "resolver": notRef, "rule": notRef, "server_version": notRef, "signature": notRef, "signed_text": notRef,
+ "subject": notRef, "tag_id": notRef, "tree_id": notRef, "value": notRef,
+}
+
+// agentRefNameFields classifies the field "name", whose meaning depends on its message.
+var agentRefNameFields = map[protoreflect.FullName]refFieldKind{
+ "gitaly.FindBranchRequest": isBranchName,
+ "gitaly.Branch": isRef,
+ "gitaly.FindAllBranchesResponse.Branch": isRef,
+ "gitaly.FindDefaultBranchNameResponse": isRef,
+ "gitaly.ListRefsResponse.Reference": isRef,
+ "gitaly.Tag": isRef,
+ "gitaly.User": notRef,
+ "gitaly.CommitAuthor": notRef,
+}
+
+// agentRefField returns what a string or bytes field holds, and whether the exact table knows it.
+func agentRefField(md protoreflect.MessageDescriptor, fd protoreflect.FieldDescriptor) (refFieldKind, bool) {
+ if fd.Name() == "name" {
+ kind, ok := agentRefNameFields[md.FullName()]
+ if ok {
+ return kind, true
+ }
+ } else if kind, ok := agentRefFieldsByName[fd.Name()]; ok {
+ return kind, true
+ }
+ // A field of an RPC an operator added: a name that may hold a revision, a reference or a
+ // pattern is checked.
+ n := string(fd.Name())
+ for _, part := range []string{"revision", "ref", "pattern", "commit", "oid", "sha", "branch", "tag", "name", "target"} {
+ if strings.Contains(n, part) {
+ return isRef, false
+ }
+ }
+ return notRef, false
+}
+
+// isAgent reports whether the caller of ctx authenticated with a per-agent token.
+func isAgent(ctx context.Context) (string, bool) {
+ id, ok := identity.FromContext(ctx)
+ return id.Name, ok && id.Source == identity.SourceAgent
+}
+
+// checkAgentRefs refuses a request of an agent that names a reference under refs/graft/.
+func checkAgentRefs(ctx context.Context, req any) error {
+ name, ok := isAgent(ctx)
+ if !ok {
+ return nil
+ }
+ msg, ok := req.(proto.Message)
+ if !ok {
+ return nil
+ }
+ if field, value, hidden := findHiddenRef(msg.ProtoReflect()); hidden {
+ return structerr.NewPermissionDenied("agent %q may not name %q (%s): references under %s are internal to the server",
+ name, value, field, agentrefs.HiddenPrefix)
+ }
+ return nil
+}
+
+// findHiddenRef returns the first reference field of m, at any depth, whose value names refs/graft/.
+func findHiddenRef(m protoreflect.Message) (protoreflect.FullName, string, bool) {
+ var field protoreflect.FullName
+ var value string
+ var hidden bool
+ m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
+ switch fd.Kind() {
+ case protoreflect.StringKind, protoreflect.BytesKind:
+ kind, _ := agentRefField(m.Descriptor(), fd)
+ if kind == notRef {
+ return true
+ }
+ for _, s := range scalarStrings(fd, v) {
+ if (kind == isRef && agentrefs.RevisionHidden(s)) || (kind == isBranchName && agentrefs.Hidden(s)) {
+ field, value, hidden = fd.FullName(), s, true
+ return false
+ }
+ }
+ case protoreflect.MessageKind, protoreflect.GroupKind:
+ if fd.IsMap() {
+ return true
+ }
+ for _, sub := range subMessages(fd, v) {
+ if f, val, h := findHiddenRef(sub); h {
+ field, value, hidden = f, val, true
+ return false
+ }
+ }
+ }
+ return true
+ })
+ return field, value, hidden
+}
+
+// filterAgentRefs removes from a response to an agent every reference under refs/graft/ it lists.
+func filterAgentRefs(ctx context.Context, resp any) {
+ if _, ok := isAgent(ctx); !ok {
+ return
+ }
+ if msg, ok := resp.(proto.Message); ok && msg != nil {
+ filterMessage(msg.ProtoReflect(), true)
+ }
+}
+
+// filterMessage filters m in place, and reports whether m itself names a hidden reference in a
+// singular reference field, so that the list holding it drops it. At the top level such a field
+// is cleared instead. The changes are collected while ranging over the fields and applied after.
+func filterMessage(m protoreflect.Message, top bool) bool {
+ drop := false
+ type change struct {
+ fd protoreflect.FieldDescriptor
+ value protoreflect.Value
+ clear bool
+ }
+ var changes []change
+ m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
+ switch fd.Kind() {
+ case protoreflect.StringKind, protoreflect.BytesKind:
+ if kind, _ := agentRefField(m.Descriptor(), fd); kind == notRef {
+ return true
+ }
+ if fd.IsList() {
+ list := v.List()
+ kept := m.NewField(fd).List()
+ for i := 0; i < list.Len(); i++ {
+ if !agentrefs.Hidden(valueString(fd, list.Get(i))) {
+ kept.Append(list.Get(i))
+ }
+ }
+ if kept.Len() != list.Len() {
+ changes = append(changes, change{fd: fd, value: protoreflect.ValueOfList(kept), clear: kept.Len() == 0})
+ }
+ } else if agentrefs.Hidden(valueString(fd, v)) {
+ if top {
+ changes = append(changes, change{fd: fd, clear: true})
+ } else {
+ drop = true
+ }
+ }
+ case protoreflect.MessageKind, protoreflect.GroupKind:
+ if fd.IsMap() {
+ return true
+ }
+ if fd.IsList() {
+ list := v.List()
+ kept := m.NewField(fd).List()
+ for i := 0; i < list.Len(); i++ {
+ if !filterMessage(list.Get(i).Message(), false) {
+ kept.Append(list.Get(i))
+ }
+ }
+ if kept.Len() != list.Len() {
+ changes = append(changes, change{fd: fd, value: protoreflect.ValueOfList(kept), clear: kept.Len() == 0})
+ }
+ } else if filterMessage(v.Message(), false) {
+ changes = append(changes, change{fd: fd, clear: true})
+ }
+ }
+ return true
+ })
+ for _, c := range changes {
+ if c.clear {
+ m.Clear(c.fd)
+ } else {
+ m.Set(c.fd, c.value)
+ }
+ }
+ return drop
+}
+
+func scalarStrings(fd protoreflect.FieldDescriptor, v protoreflect.Value) []string {
+ if !fd.IsList() {
+ return []string{valueString(fd, v)}
+ }
+ list := v.List()
+ out := make([]string, 0, list.Len())
+ for i := 0; i < list.Len(); i++ {
+ out = append(out, valueString(fd, list.Get(i)))
+ }
+ return out
+}
+
+func valueString(fd protoreflect.FieldDescriptor, v protoreflect.Value) string {
+ if fd.Kind() == protoreflect.BytesKind {
+ return string(v.Bytes())
+ }
+ return v.String()
+}
+
+func subMessages(fd protoreflect.FieldDescriptor, v protoreflect.Value) []protoreflect.Message {
+ if !fd.IsList() {
+ return []protoreflect.Message{v.Message()}
+ }
+ list := v.List()
+ out := make([]protoreflect.Message, 0, list.Len())
+ for i := 0; i < list.Len(); i++ {
+ out = append(out, list.Get(i).Message())
+ }
+ return out
+}
diff --git a/internal/gitaly/server/auth/agent_refs_test.go b/internal/gitaly/server/auth/agent_refs_test.go
new file mode 100755
index 0000000..5ba9f0e
--- /dev/null
+++ b/internal/gitaly/server/auth/agent_refs_test.go
@@ -0,0 +1,205 @@
+package auth
+
+import (
+ "context"
+ "sort"
+ "strings"
+ "testing"
+
+ gitalycfgauth "github.com/feder-cr/git-ai/internal/gitaly/config/auth"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+ "github.com/feder-cr/git-ai/proto/go/gitalypb"
+ "github.com/stretchr/testify/require"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/reflect/protoreflect"
+ "google.golang.org/protobuf/reflect/protoregistry"
+)
+
+// TestAgentRefFields_everyAllowedRPCIsClassified: every string or bytes field of the requests and
+// responses of every RPC agents may call by default, at any depth, is classified by the exact table
+// of agentRefField, as a reference or not. A new RPC or field fails here until it is classified, so
+// the filter cannot miss it.
+func TestAgentRefFields_everyAllowedRPCIsClassified(t *testing.T) {
+ t.Parallel()
+
+ var unclassified []string
+ for method := range builtinAgentRPCs {
+ service, name, ok := splitFullMethod(method)
+ require.True(t, ok, method)
+ d, err := protoregistry.GlobalFiles.FindDescriptorByName(protoreflect.FullName(service))
+ require.NoError(t, err, method)
+ md := d.(protoreflect.ServiceDescriptor).Methods().ByName(protoreflect.Name(name))
+ require.NotNil(t, md, method)
+ for _, root := range []protoreflect.MessageDescriptor{md.Input(), md.Output()} {
+ seen := map[protoreflect.FullName]bool{}
+ var walk func(protoreflect.MessageDescriptor)
+ walk = func(m protoreflect.MessageDescriptor) {
+ if seen[m.FullName()] {
+ return
+ }
+ seen[m.FullName()] = true
+ for i := 0; i < m.Fields().Len(); i++ {
+ fd := m.Fields().Get(i)
+ switch fd.Kind() {
+ case protoreflect.StringKind, protoreflect.BytesKind:
+ if _, exact := agentRefField(m, fd); !exact {
+ unclassified = append(unclassified, string(m.FullName())+"."+string(fd.Name())+" ("+method+")")
+ }
+ case protoreflect.MessageKind, protoreflect.GroupKind:
+ if !fd.IsMap() {
+ walk(fd.Message())
+ }
+ }
+ }
+ }
+ walk(root)
+ }
+ }
+ sort.Strings(unclassified)
+ require.Empty(t, unclassified, "string or bytes fields of agent RPCs that agentRefField does not classify")
+}
+
+func agentContext() context.Context {
+ return identity.NewContext(context.Background(), identity.Identity{Name: "agent-a", Source: identity.SourceAgent})
+}
+
+// TestCheckAgentRefs: an agent may not name a reference under refs/graft/, in full, through git's
+// revision rules (graft/...), or in a pattern; paths, contents and branch names that only look
+// alike pass, and callers that are not agents are not restricted.
+func TestCheckAgentRefs(t *testing.T) {
+ t.Parallel()
+
+ for name, tc := range map[string]struct {
+ req proto.Message
+ denied bool
+ }{
+ "ListRefs pattern under refs/graft": {&gitalypb.ListRefsRequest{Patterns: [][]byte{[]byte("refs/heads/"), []byte("refs/graft/")}}, true},
+ "ListRefs pattern of every ref": {&gitalypb.ListRefsRequest{Patterns: [][]byte{[]byte("refs/")}}, false},
+ "FindCommit by the short name": {&gitalypb.FindCommitRequest{Revision: []byte("graft/green/main")}, true},
+ "FindCommit by a range": {&gitalypb.FindCommitRequest{Revision: []byte("main..refs/graft/green/main")}, true},
+ "FindCommit of a branch": {&gitalypb.FindCommitRequest{Revision: []byte("refs/heads/graft/x")}, false},
+ "GetBlobs of a proposal by reference": {&gitalypb.GetBlobsRequest{RevisionPaths: []*gitalypb.GetBlobsRequest_RevisionPath{{Revision: "refs/graft/proposals/0123456789abcdef/1", Path: []byte("a")}}}, true},
+ "GetBlobs of a path named graft": {&gitalypb.GetBlobsRequest{RevisionPaths: []*gitalypb.GetBlobsRequest_RevisionPath{{Revision: "main", Path: []byte("graft/x.go")}}}, false},
+ "TreeEntry of a proposal": {&gitalypb.TreeEntryRequest{Revision: []byte("refs/graft/proposals/0123456789abcdef/1"), Path: []byte("a")}, true},
+ "FindRefsByOID pattern": {&gitalypb.FindRefsByOIDRequest{Oid: "0123", RefPatterns: []string{"refs/graft/green"}}, true},
+ "FindBranch of a full graft name": {&gitalypb.FindBranchRequest{Name: []byte("refs/graft/green/main")}, true},
+ "FindBranch of a branch named graft/": {&gitalypb.FindBranchRequest{Name: []byte("graft/x")}, false},
+ "UserCommitFiles content": {&gitalypb.UserCommitFilesRequest{UserCommitFilesRequestPayload: &gitalypb.UserCommitFilesRequest_Action{Action: &gitalypb.UserCommitFilesAction{UserCommitFilesActionPayload: &gitalypb.UserCommitFilesAction_Content{Content: []byte("see refs/graft/green/main")}}}}, false},
+ "GetObjects of an object ID": {&gitalypb.GetObjectsRequest{Oids: []string{"0123456789abcdef0123456789abcdef01234567"}}, false},
+ } {
+ err := checkAgentRefs(agentContext(), tc.req)
+ if tc.denied {
+ require.Equal(t, codes.PermissionDenied, status.Code(err), "%s: %v", name, err)
+ require.ErrorContains(t, err, "internal to the server", name)
+ } else {
+ require.NoError(t, err, name)
+ }
+ require.NoError(t, checkAgentRefs(context.Background(), tc.req), "%s: not an agent", name)
+ }
+}
+
+// TestFilterAgentRefs: a response to an agent loses every reference under refs/graft/ it lists;
+// a response to another caller is unchanged.
+func TestFilterAgentRefs(t *testing.T) {
+ t.Parallel()
+
+ refs := func() *gitalypb.ListRefsResponse {
+ return &gitalypb.ListRefsResponse{References: []*gitalypb.ListRefsResponse_Reference{
+ {Name: []byte("refs/heads/main"), Target: "1"},
+ {Name: []byte("refs/graft/green/main"), Target: "1"},
+ {Name: []byte("refs/graft/proposals/0123456789abcdef/1"), Target: "2"},
+ }}
+ }
+ r := refs()
+ filterAgentRefs(agentContext(), r)
+ require.Len(t, r.GetReferences(), 1)
+ require.Equal(t, "refs/heads/main", string(r.GetReferences()[0].GetName()))
+ r = refs()
+ filterAgentRefs(context.Background(), r)
+ require.Len(t, r.GetReferences(), 3, "not an agent")
+
+ byOID := &gitalypb.FindRefsByOIDResponse{Refs: []string{"refs/heads/a", "refs/graft/green/main", "refs/tags/v1"}}
+ filterAgentRefs(agentContext(), byOID)
+ require.Equal(t, []string{"refs/heads/a", "refs/tags/v1"}, byOID.GetRefs())
+
+ commits := &gitalypb.FindCommitsResponse{Commits: []*gitalypb.GitCommit{{Id: "1", ReferencedBy: [][]byte{[]byte("refs/heads/a"), []byte("refs/graft/green/main")}}}}
+ filterAgentRefs(agentContext(), commits)
+ require.Len(t, commits.GetCommits(), 1, "a commit is kept; only the hidden name goes")
+ require.Equal(t, [][]byte{[]byte("refs/heads/a")}, commits.GetCommits()[0].GetReferencedBy())
+
+ def := &gitalypb.FindDefaultBranchNameResponse{Name: []byte("refs/graft/green/main")}
+ filterAgentRefs(agentContext(), def)
+ require.Empty(t, def.GetName(), "a top-level hidden name is cleared")
+}
+
+// recordingServerStream records what a handler sends.
+type recordingServerStream struct {
+ grpc.ServerStream
+ ctx context.Context
+ sent []proto.Message
+}
+
+func (s *recordingServerStream) Context() context.Context { return s.ctx }
+
+func (s *recordingServerStream) SendMsg(m any) error {
+ s.sent = append(s.sent, proto.Clone(m.(proto.Message)))
+ return nil
+}
+
+// TestAgentRefFilter_throughTheInterceptors: the guard runs on the interceptors of a real agent
+// configuration: a unary request that names refs/graft/ is refused before its handler, and the
+// responses of a unary and of a streaming RPC to an agent lose the hidden references.
+func TestAgentRefFilter_throughTheInterceptors(t *testing.T) {
+ t.Parallel()
+
+ unary, stream, err := InterceptorsWithAgentRPCs(gitalycfgauth.Config{Token: sharedSecretTest}, testAgents(), nil, true)
+ require.NoError(t, err)
+ agentCtx := contextWithAuthorization(t, "Bearer "+agentASecret)
+
+ called := false
+ _, err = unary(agentCtx, &gitalypb.FindCommitRequest{Revision: []byte("refs/graft/green/main")},
+ &grpc.UnaryServerInfo{FullMethod: gitalypb.CommitService_FindCommit_FullMethodName},
+ func(context.Context, any) (any, error) { called = true; return &gitalypb.FindCommitResponse{}, nil })
+ require.Equal(t, codes.PermissionDenied, status.Code(err), "err = %v", err)
+ require.False(t, called)
+
+ resp, err := unary(agentCtx, &gitalypb.FindRefsByOIDRequest{Oid: "1"},
+ &grpc.UnaryServerInfo{FullMethod: gitalypb.RefService_FindRefsByOID_FullMethodName},
+ func(context.Context, any) (any, error) {
+ return &gitalypb.FindRefsByOIDResponse{Refs: []string{"refs/heads/a", "refs/graft/green/main"}}, nil
+ })
+ require.NoError(t, err)
+ require.Equal(t, []string{"refs/heads/a"}, resp.(*gitalypb.FindRefsByOIDResponse).GetRefs())
+
+ ss := &recordingServerStream{ctx: agentCtx}
+ err = stream(nil, ss, &grpc.StreamServerInfo{FullMethod: gitalypb.RefService_ListRefs_FullMethodName},
+ func(srv any, s grpc.ServerStream) error {
+ return s.SendMsg(&gitalypb.ListRefsResponse{References: []*gitalypb.ListRefsResponse_Reference{
+ {Name: []byte("refs/heads/main")}, {Name: []byte("refs/graft/green/main")},
+ }})
+ })
+ require.NoError(t, err)
+ require.Len(t, ss.sent, 1)
+ var names []string
+ for _, r := range ss.sent[0].(*gitalypb.ListRefsResponse).GetReferences() {
+ names = append(names, string(r.GetName()))
+ }
+ require.Equal(t, "refs/heads/main", strings.Join(names, ","))
+}
+
+// TestAgentRefNameFields_messagesExist: every message the classification of "name" names exists and
+// has a field "name", so that a typo cannot leave a field unclassified.
+func TestAgentRefNameFields_messagesExist(t *testing.T) {
+ t.Parallel()
+
+ for name := range agentRefNameFields {
+ d, err := protoregistry.GlobalFiles.FindDescriptorByName(name)
+ require.NoError(t, err, name)
+ md, ok := d.(protoreflect.MessageDescriptor)
+ require.True(t, ok, name)
+ require.NotNil(t, md.Fields().ByName("name"), name)
+ }
+}
diff --git a/internal/gitaly/service/operations/land_package.go b/internal/gitaly/service/operations/land_package.go
index 052d0fb..8ba2bfe 100644
--- a/internal/gitaly/service/operations/land_package.go
+++ b/internal/gitaly/service/operations/land_package.go
@@ -142,12 +142,12 @@ func (e *landUnresolvedError) Unwrap() error {
// landWithPackage adds the conflict package of resolveErr, when it carries one, to the merge
// conflict error of the land as a second detail, after UserLandError. Clients that know only
// UserLandError see the error unchanged. A package that cannot be built (a side that cannot be
-// read), or a resolver's answer that cannot be kept as proposals, is an internal error: the merge
-// conflict alone would hide it.
+// read) is an internal error: the merge conflict alone would hide it. An agent the policy does not
+// let update the branch gets the policy's refusal, not a package.
func landWithPackage(ctx context.Context, mergeConflict, resolveErr error) error {
- var proposalsErr *landProposalsError
- if errors.As(resolveErr, &proposalsErr) {
- return structerr.NewInternal("%w", proposalsErr)
+ var denied *landPolicyDeniedError
+ if errors.As(resolveErr, &denied) {
+ return denied.err
}
var unresolved *landUnresolvedError
var se structerr.Error
diff --git a/internal/gitaly/service/operations/land_proposals.go b/internal/gitaly/service/operations/land_proposals.go
index 0485648..34dc72b 100644
--- a/internal/gitaly/service/operations/land_proposals.go
+++ b/internal/gitaly/service/operations/land_proposals.go
@@ -3,12 +3,10 @@ package operations
import (
"bytes"
"context"
- "crypto/sha256"
- "encoding/hex"
"errors"
"fmt"
"path"
- "sort"
+ "strconv"
"strings"
"time"
@@ -17,8 +15,7 @@ import (
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/localrepo"
- "github.com/feder-cr/git-ai/internal/git/updateref"
- "github.com/feder-cr/git-ai/internal/graft/identity"
+ "github.com/feder-cr/git-ai/internal/graft/proposals"
"github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/internal/log"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
@@ -28,40 +25,18 @@ import (
// reviews, edits or replaces, and lands as its own (PLAN10 14). The server keeps the proposals of a
// failed land under a hidden reference, which the agent reads by object ID (GetObjects).
-const (
- // landProposalRefPrefix is the hidden namespace of the proposals (git.InternalRefPrefixes).
- landProposalRefPrefix = "refs/graft/proposals/"
- // landMaxProposalsPerBranch bounds the proposal references kept for one branch.
- landMaxProposalsPerBranch = 20
-)
-
-// landProposalPruneErrors counts the proposal references that could not be pruned after a land.
+// landProposalPruneErrors counts the prunings of proposal references that failed after a land.
var landProposalPruneErrors = promauto.NewCounter(prometheus.CounterOpts{
Name: "graft_land_proposal_prune_errors_total",
Help: "Prunings of the proposal references of a branch that failed after a land.",
})
-// landProposalBranchDir is the directory of the proposal references of a branch: a hash of the
-// branch name, so that any branch name gives a valid and fixed-length component.
-func landProposalBranchDir(branch string) string {
- sum := sha256.Sum256([]byte(branch))
- return landProposalRefPrefix + hex.EncodeToString(sum[:])[:16] + "/"
-}
-
-// landProposalRef is the reference that keeps the proposals of the land of commit onto branch.
-func landProposalRef(branch string, commit git.ObjectID) git.ReferenceName {
- return git.ReferenceName(landProposalBranchDir(branch) + commit.String())
-}
-
-// landProposalsError is the failure to keep a resolver's answer as proposals. The land fails with
-// Internal (landWithPackage): a package without the proposals would hide the answer.
-type landProposalsError struct{ err error }
-
-func (e *landProposalsError) Error() string {
- return "keeping the resolver's answer as proposals: " + e.err.Error()
-}
-
-func (e *landProposalsError) Unwrap() error { return e.err }
+// landProposalWriteErrors counts the answers that could not be kept as proposals: the land still
+// fails with its package, whose files say "proposal not stored".
+var landProposalWriteErrors = promauto.NewCounter(prometheus.CounterOpts{
+ Name: "graft_land_proposal_write_errors_total",
+ Help: "Resolver answers of a failed land that could not be kept as proposals.",
+})
// landProposedReason is the reason of a file that a model resolved.
const landProposedReason = "a model resolved the file: the server never lands a model's resolution, it proposes it (see proposal); land it, edited or not, as your own resolution"
@@ -106,6 +81,10 @@ func (s *Server) landWriteProposals(ctx context.Context, in *gitalypb.UserLandRe
// Who caused the proposals is in their commit, the trace of the reference: the update itself
// is the server's (landUpdateProposalRefs).
actor := landBatchActor(ctx, in)
+ // The date of the proposals' commit is the server's clock, never the request's timestamp:
+ // their age decides when they expire.
+ now := time.Now()
+ start := now
repo := s.localRepoFactory.Build(in.GetRepository())
tree := &localrepo.TreeEntry{Type: localrepo.Tree, Mode: "040000"}
byPath := make(map[string]*gitalypb.LandConflictProposal, len(answer.Files))
@@ -130,21 +109,21 @@ func (s *Server) landWriteProposals(ctx context.Context, in *gitalypb.UserLandRe
}
commit, err := repo.WriteCommit(ctx, localrepo.WriteCommitConfig{
TreeID: tree.OID,
- Message: fmt.Sprintf("graft: proposals for landing %s onto %s\n\nResolver: %s\nRequested-By: %s\n", l.commitID, l.tip, answer.Resolver, actor),
+ Message: fmt.Sprintf("graft: proposals for landing %s onto %s\n\nResolver: %s\nRequested-By: %s\n", l.commitID, l.tip, landTrailerValueOrQuoted(answer.Resolver), landTrailerValueOrQuoted(actor)),
Parents: []git.ObjectID{l.tip, l.commitID},
AuthorName: committer.Name,
AuthorEmail: committer.Email,
- AuthorDate: committer.When,
+ AuthorDate: now,
CommitterName: committer.Name,
CommitterEmail: committer.Email,
- CommitterDate: committer.When,
+ CommitterDate: now,
GitConfig: s.gitConfig,
})
if err != nil {
return "", fmt.Errorf("writing the commit of the proposals: %w", err)
}
- ref := landProposalRef(string(in.GetBranch()), l.commitID)
+ ref := proposals.Ref(string(in.GetBranch()), l.commitID)
old, err := repo.GetReference(ctx, ref)
oldOID := git.ObjectID("")
switch {
@@ -160,10 +139,10 @@ func (s *Server) landWriteProposals(ctx context.Context, in *gitalypb.UserLandRe
if oldOID == "" {
oldOID = objectHash.ZeroOID
}
- if err := s.landUpdateProposalRefs(ctx, repo, []landProposalRefOp{{name: ref, newOID: commit, oldOID: oldOID}}); err != nil {
+ if err := proposals.Update(ctx, repo, []proposals.Op{{Name: ref, NewOID: commit, OldOID: oldOID}}); err != nil {
return "", err
}
- s.logger.WithFields(log.Fields{"ref": ref.String(), "actor": actor, "resolver": answer.Resolver}).InfoContext(ctx, "graft: kept the resolver's answer as proposals")
+ s.logger.WithFields(log.Fields{"ref": ref.String(), "actor": actor, "resolver": answer.Resolver, "seconds": time.Since(start).Seconds()}).InfoContext(ctx, "graft: kept the resolver's answer as proposals")
for _, f := range l.files {
f.proposal = byPath[f.conflict.path]
@@ -175,139 +154,90 @@ func (s *Server) landWriteProposals(ctx context.Context, in *gitalypb.UserLandRe
return ref, nil
}
-// landPruneProposals deletes the proposal references of a branch that are older than proposal_ttl
-// (by the committer date of their commit) or beyond the newest landMaxProposalsPerBranch, and drop,
-// when set: the proposal of a commit that landed. The deletions go through the reference
-// transaction, as the writes do.
-func (s *Server) landPruneProposals(ctx context.Context, repo *localrepo.Repo, branch string, drop git.ReferenceName) error {
- ttl, err := s.resolveCfg.ProposalTTLOrDefault()
- if err != nil {
- return err
- }
- refs, err := repo.GetReferences(ctx, landProposalBranchDir(branch))
- if err != nil {
- return fmt.Errorf("listing the proposals of %q: %w", branch, err)
- }
- type dated struct {
- name git.ReferenceName
- when time.Time
- }
- var kept []dated
- var remove []git.ReferenceName
- now := time.Now()
- for _, r := range refs {
- if r.Name == drop {
- remove = append(remove, r.Name)
- continue
- }
- c, err := repo.ReadCommit(ctx, git.Revision(r.Target))
- if err != nil {
- return fmt.Errorf("reading %s: %w", r.Name, err)
- }
- when := c.GetCommitter().GetDate().AsTime()
- if now.Sub(when) > ttl {
- remove = append(remove, r.Name)
- continue
- }
- kept = append(kept, dated{r.Name, when})
- }
- sort.Slice(kept, func(i, j int) bool { return kept[i].when.After(kept[j].when) })
- for i := landMaxProposalsPerBranch; i < len(kept); i++ {
- remove = append(remove, kept[i].name)
- }
- if len(remove) == 0 {
- return nil
+// landPruneAfterLand prunes the proposals of the branch after a land that asked for resolution or
+// brought one, and drops the proposal of the change that landed. Only those lands prune, so that a
+// plain land pays nothing for proposals; the bound of landMaxProposalsPerBranch holds anyway, since
+// only those lands write proposals. The land has happened: a failure is logged and counted, and
+// the land's result stands.
+func (s *Server) landPruneAfterLand(ctx context.Context, in *gitalypb.UserLandRequest) {
+ if !in.GetResolveConflicts() && in.GetResolution() == nil {
+ return
}
- deletions := make([]landProposalRefOp, 0, len(remove))
- for _, name := range remove {
- deletions = append(deletions, landProposalRefOp{name: name, delete: true})
+ repo := s.localRepoFactory.Build(in.GetRepository())
+ drop := git.ReferenceName("")
+ if commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}")); err == nil {
+ drop = proposals.Ref(string(in.GetBranch()), commitID)
}
- if err := s.landUpdateProposalRefs(ctx, repo, deletions); err != nil {
- return fmt.Errorf("pruning proposals: %w", err)
+ if err := s.landPruneProposals(ctx, repo, string(in.GetBranch()), drop); err != nil {
+ landProposalPruneErrors.Inc()
+ s.logger.WithError(err).WithField("branch", string(in.GetBranch())).ErrorContext(ctx, "graft: pruning the proposals of the branch failed after the land")
}
- return nil
}
-// landProposalRefOp is one update of a proposal reference: to newOID from oldOID, or a deletion.
-type landProposalRefOp struct {
- name git.ReferenceName
- newOID, oldOID git.ObjectID
- delete bool
-}
-
-// landUpdateProposalRefs is the one writer of the proposal references (PLAN10 14): it updates
-// only references of the form refs/graft/proposals/<16 hex of the branch>/<commit ID>, and refuses
-// anything else before git runs. It updates them as the server, without the identity of the
-// request, through the reference transaction like any update: the agent whose land made the
-// proposals may not write refs/graft/ itself (the reference-transaction hook reserves it), and
-// its policy does not judge the server's write. Who caused it is in the proposals' commit.
-func (s *Server) landUpdateProposalRefs(ctx context.Context, repo *localrepo.Repo, ops []landProposalRefOp) error {
- for _, op := range ops {
- if !landProposalRefNameValid(op.name) {
- return fmt.Errorf("refusing to update %q: not a proposal reference", op.name)
- }
- }
- ctx = identity.NewContext(ctx, identity.Identity{})
- updater, err := updateref.New(ctx, repo)
+// landPruneProposals prunes the proposals of a branch (proposals.Prune), with drop when set.
+func (s *Server) landPruneProposals(ctx context.Context, repo *localrepo.Repo, branch string, drop git.ReferenceName) error {
+ ttl, err := s.resolveCfg.ProposalTTLOrDefault()
if err != nil {
- return fmt.Errorf("updating proposal references: %w", err)
- }
- defer func() { _ = updater.Close() }()
- if err := updater.Start(); err != nil {
- return fmt.Errorf("updating proposal references: %w", err)
- }
- for _, op := range ops {
- if op.delete {
- err = updater.Delete(op.name)
- } else {
- err = updater.Update(op.name, op.newOID, op.oldOID)
- }
- if err != nil {
- return fmt.Errorf("updating %s: %w", op.name, err)
- }
- }
- if err := updater.Commit(); err != nil {
- return fmt.Errorf("updating proposal references: %w", err)
+ return err
}
- return nil
+ _, err = proposals.Prune(ctx, repo, proposals.BranchDir(branch), ttl, drop, time.Now())
+ return err
}
-// landProposalRefNameValid reports whether name is refs/graft/proposals/<16 hex>/<object ID>.
-func landProposalRefNameValid(name git.ReferenceName) bool {
- rest, ok := strings.CutPrefix(name.String(), landProposalRefPrefix)
- if !ok {
- return false
+// landTrailerValueOrQuoted returns v when it is a valid trailer value (landTrailerValueValid), and
+// v quoted otherwise, so that a value the caller chose (the user ID of the request) can never add
+// a line to the proposals' message.
+func landTrailerValueOrQuoted(v string) string {
+ if landTrailerValueValid(v) {
+ return v
}
- dir, commit, ok := strings.Cut(rest, "/")
- return ok && len(dir) == 16 && landHex(dir) && (len(commit) == 40 || len(commit) == 64) && landHex(commit)
+ return strconv.QuoteToASCII(v)
}
-// landHex reports whether s is lower-case hexadecimal.
-func landHex(s string) bool {
- for _, c := range s {
- if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
+// decideLand is the one decision point of a land that asked the server to resolve its conflicts
+// (PLAN10 15.4): it lands only when the resolver answered, the answer holds no model's resolution,
+// and every conflicting file is resolved. A file that proposes is never landed. This is where a
+// landing gate for model resolutions would live; today there is none.
+func decideLand(answer *resolver.Response, files []*landFileState) bool {
+ if answer == nil {
+ return false
+ }
+ if landAnswerProposes(answer) {
+ return false
+ }
+ for _, f := range files {
+ if f.outcome != gitalypb.LandConflictFile_OUTCOME_RESOLVED {
return false
}
}
return true
}
-// landPruneAfterLand prunes the proposals of the branch after a land that asked for resolution or
-// brought one, and drops the proposal of the change that landed. Only those lands prune, so that a
-// plain land pays nothing for proposals; the bound of landMaxProposalsPerBranch holds anyway, since
-// only those lands write proposals. The land has happened: a failure is logged and counted, and
-// the land's result stands.
-func (s *Server) landPruneAfterLand(ctx context.Context, in *gitalypb.UserLandRequest) {
- if !in.GetResolveConflicts() && in.GetResolution() == nil {
- return
- }
- repo := s.localRepoFactory.Build(in.GetRepository())
- drop := git.ReferenceName("")
- if commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}")); err == nil {
- drop = landProposalRef(string(in.GetBranch()), commitID)
+// landProposalsNotStored marks every file of the answer: its proposal was not stored, and why.
+func landProposalsNotStored(files []*landFileState, answer *resolver.Response, cause error) {
+ inAnswer := make(map[string]bool, len(answer.Files))
+ for _, f := range answer.Files {
+ inAnswer[f.Path] = true
}
- if err := s.landPruneProposals(ctx, repo, string(in.GetBranch()), drop); err != nil {
- landProposalPruneErrors.Inc()
- s.logger.WithError(err).WithField("branch", string(in.GetBranch())).ErrorContext(ctx, "graft: pruning the proposals of the branch failed after the land")
+ for _, f := range files {
+ if !inAnswer[f.conflict.path] {
+ continue
+ }
+ f.proposal = nil
+ note := "proposal not stored: " + cause.Error()
+ if f.reason == "" {
+ f.reason = note
+ } else {
+ f.reason += "; " + note
+ }
}
}
+
+// landPolicyDeniedError is the policy's refusal of a land that asked the server to resolve: the
+// agent may not update the branch, so nothing is resolved and no proposal is kept. err is the
+// refusal (PermissionDenied).
+type landPolicyDeniedError struct{ err error }
+
+func (e *landPolicyDeniedError) Error() string { return e.err.Error() }
+
+func (e *landPolicyDeniedError) Unwrap() error { return e.err }
diff --git a/internal/gitaly/service/operations/land_proposals_test.go b/internal/gitaly/service/operations/land_proposals_test.go
index 4c582f4..7232000 100755
--- a/internal/gitaly/service/operations/land_proposals_test.go
+++ b/internal/gitaly/service/operations/land_proposals_test.go
@@ -2,6 +2,7 @@ package operations
import (
"fmt"
+ "strconv"
"strings"
"testing"
"time"
@@ -9,12 +10,17 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/types/known/timestamppb"
"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"
+ gitalycfg "github.com/feder-cr/git-ai/internal/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+ "github.com/feder-cr/git-ai/internal/graft/proposals"
"github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/internal/helper/text"
+ "github.com/feder-cr/git-ai/internal/structerr"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
@@ -33,7 +39,7 @@ func modelAnswer() resolver.Response {
// "" when there is none.
func proposalRefTarget(t *testing.T, fx resolveFixture) string {
t.Helper()
- out, err := gittest.NewCommand(t, fx.cfg, "-C", fx.repoPath, "rev-parse", "--verify", "--quiet", landProposalRef(git.DefaultBranch, fx.commit).String()).Output()
+ out, err := gittest.NewCommand(t, fx.cfg, "-C", fx.repoPath, "rev-parse", "--verify", "--quiet", proposals.Ref(git.DefaultBranch, fx.commit).String()).Output()
if err != nil {
return ""
}
@@ -55,7 +61,7 @@ func TestUserLand_proposals(t *testing.T) {
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
pkg := landConflictDetail(err)
require.NotNil(t, pkg)
- require.Equal(t, landProposalRef(git.DefaultBranch, fx.commit).String(), pkg.GetProposalRef())
+ require.Equal(t, proposals.Ref(git.DefaultBranch, fx.commit).String(), pkg.GetProposalRef())
target := proposalRefTarget(t, fx)
require.NotEmpty(t, target, "the proposal reference exists")
@@ -178,7 +184,7 @@ func TestUserLand_proposals(t *testing.T) {
dir, argv := resolverScript(t, "")
writeAnswer(t, dir, modelAnswer())
fx := newResolveFixture(t, enabledResolver(argv))
- branchDir := landProposalBranchDir(git.DefaultBranch)
+ branchDir := proposals.BranchDir(git.DefaultBranch)
now := time.Now()
// Proposal references are named by the commit they are for: fake commit IDs name these.
name := func(i int) string { return fmt.Sprintf("%s%040x", branchDir, i+1) }
@@ -194,26 +200,69 @@ func TestUserLand_proposals(t *testing.T) {
refs := strings.Fields(text.ChompBytes(gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "for-each-ref", "--format=%(refname)", branchDir)))
require.Len(t, refs, 20)
- require.Contains(t, refs, landProposalRef(git.DefaultBranch, fx.commit).String(), "the newest is kept")
+ require.Contains(t, refs, proposals.Ref(git.DefaultBranch, fx.commit).String(), "the newest is kept")
require.NotContains(t, refs, name(100), "past proposal_ttl")
require.NotContains(t, refs, name(20), "the oldest beyond 20")
require.NotContains(t, refs, name(19), "the oldest beyond 20")
require.Contains(t, refs, name(18))
})
- t.Run("a proposal reference that cannot be written fails the land with Internal", func(t *testing.T) {
+ t.Run("proposals that cannot be stored still give the package, saying so", func(t *testing.T) {
t.Parallel()
dir, argv := resolverScript(t, "")
writeAnswer(t, dir, modelAnswer())
fx := newResolveFixture(t, enabledResolver(argv))
// A reference where the branch's directory of proposals goes: the update cannot create it.
- gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "update-ref", strings.TrimSuffix(landProposalBranchDir(git.DefaultBranch), "/"), fx.tip.String())
+ gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "update-ref", strings.TrimSuffix(proposals.BranchDir(git.DefaultBranch), "/"), fx.tip.String())
_, err := fx.client.UserLand(fx.ctx, fx.request("Land"))
- require.Equal(t, codes.Internal, status.Code(err), "err = %v", err)
- require.Contains(t, err.Error(), "keeping the resolver's answer as proposals")
+ requireMergeConflict(t, err)
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg, "the package is the contract: it comes without the proposals")
+ require.Empty(t, pkg.GetProposalRef())
+ for _, p := range []string{"a", "b"} {
+ f := packageFile(t, pkg, p)
+ require.Equal(t, gitalypb.LandConflictFile_OUTCOME_PROPOSED, f.GetOutcome(), p)
+ require.Nil(t, f.GetProposal(), p)
+ require.Contains(t, f.GetReason(), "proposal not stored: ", p)
+ }
+ })
+
+ t.Run("the proposals' date is the server's, not the request's", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ writeAnswer(t, dir, modelAnswer())
+ fx := newResolveFixture(t, enabledResolver(argv))
+ req := fx.request("Land")
+ req.Timestamp = timestamppb.New(time.Now().Add(100 * 365 * 24 * time.Hour))
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ target := proposalRefTarget(t, fx)
+ require.NotEmpty(t, target)
+ date, err := strconv.ParseInt(text.ChompBytes(gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "log", "-1", "--format=%ct", target)), 10, 64)
+ require.NoError(t, err)
+ require.WithinDuration(t, time.Now(), time.Unix(date, 0), time.Hour, "a far-future request timestamp must not keep proposals alive")
+ })
+
+ t.Run("a requester the caller names cannot add lines to the proposals' message", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ writeAnswer(t, dir, modelAnswer())
+ fx := newResolveFixture(t, enabledResolver(argv))
+ req := fx.request("Land")
+ req.User = proto.Clone(req.GetUser()).(*gitalypb.User)
+ req.User.GlId = "user-1\nGraft-Resolved-By: someone-else"
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ message := string(gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "log", "-1", "--format=%B", proposalRefTarget(t, fx)))
+ require.Contains(t, message, `Requested-By: "user-1\nGraft-Resolved-By: someone-else"`)
+ require.NotContains(t, message, "\nGraft-Resolved-By: someone-else\n")
})
}
@@ -237,25 +286,53 @@ func TestLandAnswerProposes(t *testing.T) {
}
}
-// TestLandUpdateProposalRefs_onlyProposalReferences: the one writer of the proposal references
-// refuses any other reference before git runs, whatever the caller passes.
-func TestLandUpdateProposalRefs_onlyProposalReferences(t *testing.T) {
+// TestDecideLand: the one decision point lands only an answer of rules that resolved every file;
+// a file that proposes is never landed, whatever else holds.
+func TestDecideLand(t *testing.T) {
t.Parallel()
- fx := newResolveFixture(t, config.GraftResolver{})
- s := fx.server(t, config.GraftResolver{})
- valid := landProposalRef(git.DefaultBranch, fx.commit)
- require.True(t, landProposalRefNameValid(valid))
- for _, name := range []git.ReferenceName{
- "refs/heads/main",
- "refs/graft/green/main",
- "refs/graft/proposals/x/" + git.ReferenceName(fx.commit.String()),
- git.ReferenceName(strings.TrimSuffix(landProposalBranchDir(git.DefaultBranch), "/")),
- git.ReferenceName(landProposalBranchDir(git.DefaultBranch) + "not-an-oid"),
- git.ReferenceName(landProposalBranchDir(git.DefaultBranch) + fx.commit.String() + "/x"),
- } {
- err := s.landUpdateProposalRefs(fx.ctx, fx.repo, []landProposalRefOp{{name: valid, newOID: fx.tip, oldOID: gittest.DefaultObjectHash.ZeroOID}, {name: name, newOID: fx.tip, oldOID: gittest.DefaultObjectHash.ZeroOID}})
- require.ErrorContains(t, err, "not a proposal reference", "%s", name)
+ resolved := func() []*landFileState {
+ return []*landFileState{{outcome: gitalypb.LandConflictFile_OUTCOME_RESOLVED}, {outcome: gitalypb.LandConflictFile_OUTCOME_RESOLVED}}
}
- require.Empty(t, proposalRefTarget(t, fx), "nothing was written")
+ rules := &resolver.Response{Resolver: "graft-resolve/trivial,union", Files: []resolver.ResponseFile{{Path: "a"}, {Path: "b"}}}
+ require.True(t, decideLand(rules, resolved()), "rules resolved every file")
+ require.False(t, decideLand(nil, resolved()), "no answer")
+
+ left := resolved()
+ left[1].outcome = gitalypb.LandConflictFile_OUTCOME_NOT_SENT
+ require.False(t, decideLand(rules, left), "a file left to the agent")
+
+ flagged := &resolver.Response{Resolver: "graft-resolve/trivial", Files: []resolver.ResponseFile{{Path: "a"}, {Path: "b", Proposal: true}}}
+ require.False(t, decideLand(flagged, resolved()), "a file that proposes, even with every outcome RESOLVED")
+ named := &resolver.Response{Resolver: "graft-resolve/trivial,llm", Files: []resolver.ResponseFile{{Path: "a"}, {Path: "b"}}}
+ require.False(t, decideLand(named, resolved()), "a resolver that names the llm strategy")
+}
+
+// TestResolveLandConflicts_policyFirst: an agent the policy does not let update the branch gets
+// PermissionDenied before anything is resolved, and no proposal is kept for the branch.
+func TestResolveLandConflicts_policyFirst(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ writeAnswer(t, dir, modelAnswer())
+ fx := newResolveFixture(t, enabledResolver(argv))
+ s := fx.server(t, enabledResolver(argv))
+ s.graft = gitalycfg.Graft{Standalone: true, Policy: []gitalycfg.GraftPolicyRule{{Actor: "agent-7", Refs: []string{"refs/heads/other"}, Allow: true}}}
+ ctx := identity.NewContext(fx.ctx, identity.Identity{Name: "agent-7", Source: identity.SourceAgent})
+
+ _, err := s.resolveLandConflicts(ctx, fx.request("Land"), fx.repo, fx.tip, fx.commit, fx.committer())
+ var denied *landPolicyDeniedError
+ require.ErrorAs(t, err, &denied)
+ merged := landWithPackage(ctx, structerr.NewFailedPrecondition("merge conflict"), err)
+ require.Equal(t, codes.PermissionDenied, status.Code(merged), "err = %v", merged)
+ require.Contains(t, merged.Error(), `policy denies "agent-7"`)
+ require.Equal(t, 0, resolverCalls(t, dir), "nothing was resolved")
+ require.Empty(t, proposalRefTarget(t, fx), "no proposal is kept")
+
+ // An agent the policy lets update the branch gets its proposals.
+ s.graft.Policy = []gitalycfg.GraftPolicyRule{{Actor: "agent-7", Refs: []string{"refs/heads/**"}, Allow: true}}
+ _, err = s.resolveLandConflicts(ctx, fx.request("Land"), fx.repo, fx.tip, fx.commit, fx.committer())
+ var unresolved *landUnresolvedError
+ require.ErrorAs(t, err, &unresolved)
+ require.NotEmpty(t, proposalRefTarget(t, fx))
}
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index 04d5d55..dd585b3 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -17,6 +17,7 @@ import (
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/gitcmd"
"github.com/feder-cr/git-ai/internal/git/localrepo"
+ "github.com/feder-cr/git-ai/internal/gitaly/hook"
"github.com/feder-cr/git-ai/internal/graft/identity"
"github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/internal/log"
@@ -91,6 +92,12 @@ func (s *Server) resolveLandConflicts(
tip, commitID git.ObjectID,
committer git.Signature,
) (landResolution, 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}
+ }
l, err := landListConflicts(ctx, repo, tip, commitID, s.resolveCfg.MaxFileBytesOrDefault())
if err != nil {
return landResolution{}, err
@@ -192,17 +199,23 @@ func (s *Server) resolveLandConflicts(
}
}
+ landed := decideLand(answer, 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. Failing to keep
- // it fails the land with Internal, not with a package that lacks the proposals.
- if answer != nil && (proposes || !landAllResolved(files)) {
+ // 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 err != nil {
- return landResolution{}, &landProposalsError{err: err}
+ 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)
+ } else {
+ l.proposalRef = ref
}
- l.proposalRef = ref
}
- if answer == nil || !landAllResolved(files) {
+ if !landed {
return landResolution{}, l.unresolved("", landUnresolvedCause(files), resolveErr)
}
diff --git a/internal/graft/agentrefs/agentrefs.go b/internal/graft/agentrefs/agentrefs.go
new file mode 100644
index 0000000..62da05a
--- /dev/null
+++ b/internal/graft/agentrefs/agentrefs.go
@@ -0,0 +1,48 @@
+// Package agentrefs is the one rule of what references agents may see: refs/graft/ is internal to
+// the server (the proposals of a land, graft-verify's green references), so an agent neither lists
+// nor names a reference under it (PLAN10 14). The rule is enforced on the gRPC requests and
+// responses of agents (server/auth) and in the advertisement of git upload-pack and receive-pack
+// for agents (gitcmd). The blobs of a proposal reach an agent by their object IDs, in its package.
+package agentrefs
+
+import "strings"
+
+// HiddenPrefix is the namespace that agents do not see.
+const HiddenPrefix = "refs/graft/"
+
+// ServerNamespaces are the namespaces the server keeps under HiddenPrefix: the proposals of a land
+// (refs/graft/proposals/) and graft-verify's green references (refs/graft/green/). Git completes a
+// short revision "graft/<namespace>/..." to "refs/graft/<namespace>/..." when that reference
+// exists, so an agent may not use that short form either. A new namespace under refs/graft/ is
+// listed here.
+var ServerNamespaces = []string{"proposals", "green"}
+
+// Hidden reports whether a full reference name is one agents do not see.
+func Hidden(ref string) bool {
+ return strings.HasPrefix(ref, HiddenPrefix)
+}
+
+// RevisionHidden reports whether a revision, reference or pattern an agent sends names a reference
+// under HiddenPrefix: a part of its revision (before a ":path", between "..", "...", "^", "~" or
+// "@{") that starts with refs/graft/, or with graft/<namespace>/ for a namespace of the server
+// (ServerNamespaces), which git completes to refs/graft/<namespace>/. Other short names under
+// graft/ are branches (the workspace's temporary branches are graft/<agent>/...): git completes
+// them under refs/heads/, since nothing else exists under refs/graft/. The path after the first
+// ":" is a path, not a reference.
+func RevisionHidden(rev string) bool {
+ rev, _, _ = strings.Cut(rev, ":")
+ for _, part := range strings.FieldsFunc(rev, func(r rune) bool {
+ return r == '.' || r == '^' || r == '~' || r == '@' || r == '{' || r == '}' || r == ' '
+ }) {
+ part = strings.TrimPrefix(part, "!")
+ if strings.HasPrefix(part, HiddenPrefix) {
+ return true
+ }
+ for _, ns := range ServerNamespaces {
+ if part == "graft/"+ns || strings.HasPrefix(part, "graft/"+ns+"/") {
+ return true
+ }
+ }
+ }
+ return false
+}
diff --git a/internal/graft/agentrefs/agentrefs_test.go b/internal/graft/agentrefs/agentrefs_test.go
new file mode 100644
index 0000000..50da016
--- /dev/null
+++ b/internal/graft/agentrefs/agentrefs_test.go
@@ -0,0 +1,37 @@
+package agentrefs
+
+import "testing"
+
+func TestRevisionHidden(t *testing.T) {
+ t.Parallel()
+
+ for rev, want := range map[string]bool{
+ "refs/graft/proposals/0123/abcd": true,
+ "refs/graft/green/main": true,
+ "refs/graft/*": true,
+ "graft/green/main": true, // git completes it to refs/graft/green/main
+ "graft/proposals/0123/abcd": true,
+ "graft/alice/6446a710c36ed315": false, // a workspace's temporary branch
+ "graft/greenhouse": false,
+ "refs/graft/green/main:README.md": true,
+ "main..refs/graft/green/main": true,
+ "refs/graft/green/main^{commit}": true,
+ "refs/graft/green/main~2": true,
+ "^refs/graft/green/main": true,
+ "refs/heads/main": false,
+ "refs/heads/graft/x": false,
+ "main": false,
+ "refs/*": false,
+ "0123456789abcdef0123456789abcdef01234": false,
+ "main:refs/notes/graft": false,
+ "main:graft/x.go": false, // a path, not a reference
+ "main:refs/graft/x": false, // a path too
+ } {
+ if got := RevisionHidden(rev); got != want {
+ t.Errorf("RevisionHidden(%q) = %v, want %v", rev, got, want)
+ }
+ }
+ if !Hidden("refs/graft/x") || Hidden("refs/heads/graft/x") {
+ t.Error("Hidden")
+ }
+}
diff --git a/internal/graft/e2e/proposals_test.go b/internal/graft/e2e/proposals_test.go
index 0848125..acef3eb 100755
--- a/internal/graft/e2e/proposals_test.go
+++ b/internal/graft/e2e/proposals_test.go
@@ -24,6 +24,8 @@ sys.stdout.write("===== BEGIN PATCHED CODE %s =====\n%s===== END PATCHED CODE %s
// TestPhase2ModelResolutionIsProposedThenLandedByTheAgent is decision 1 of PLAN10 14 end to end:
// the real graft-resolve answers with its llm strategy, the server lands nothing and proposes the
// answer in the conflict package, the SDK reads the proposal, and the agent lands it as its own.
+// The strategy is named "model", not "llm": the server's fallback on the resolver's name cannot
+// catch it, so the proposal flag that graft-resolve sets is what makes it a proposal.
func TestPhase2ModelResolutionIsProposedThenLandedByTheAgent(t *testing.T) {
t.Parallel()
@@ -32,6 +34,7 @@ func TestPhase2ModelResolutionIsProposedThenLandedByTheAgent(t *testing.T) {
require.NoError(t, os.WriteFile(model, []byte(fakeModel), 0o600))
resolveConfig := filepath.Join(dir, "resolve.toml")
require.NoError(t, os.WriteFile(resolveConfig, []byte(`[[strategy]]
+name = "model"
type = "llm"
paths = ["shared-log.txt"]
@@ -72,7 +75,7 @@ model = "fake"
require.Len(t, pkg.GetFiles(), 1)
f := pkg.GetFiles()[0]
require.Equal(t, gitalypb.LandConflictFile_OUTCOME_PROPOSED, f.GetOutcome())
- require.Contains(t, f.GetProposal().GetResolver(), "llm")
+ require.Equal(t, "graft-resolve/model", f.GetProposal().GetResolver(), "the name holds no \"llm\": the flag decided")
sides, err := repo.ConflictSides(ctx, pkg)
require.NoError(t, err)
diff --git a/internal/graft/proposals/proposals.go b/internal/graft/proposals/proposals.go
new file mode 100644
index 0000000..e16739b
--- /dev/null
+++ b/internal/graft/proposals/proposals.go
@@ -0,0 +1,157 @@
+// Package proposals keeps the proposals of a land the server did not resolve (PLAN10 14): one
+// reference per branch and landed commit under refs/graft/proposals/, written only by the server.
+// It is the one writer of those references (Update) and the one place they expire (Prune), used
+// by UserLand and by the housekeeping pass.
+package proposals
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "sort"
+ "strings"
+ "time"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/localrepo"
+ "github.com/feder-cr/git-ai/internal/git/updateref"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+)
+
+const (
+ // RefPrefix is the hidden namespace of the proposals (git.InternalRefPrefixes).
+ RefPrefix = "refs/graft/proposals/"
+ // MaxPerBranch bounds the proposal references kept for one branch.
+ MaxPerBranch = 20
+)
+
+// BranchDir is the directory of the proposal references of a branch: a hash of the branch name,
+// so that any branch name gives a valid, fixed-length component.
+func BranchDir(branch string) string {
+ sum := sha256.Sum256([]byte(branch))
+ return RefPrefix + hex.EncodeToString(sum[:])[:16] + "/"
+}
+
+// Ref is the reference that keeps the proposals of the land of commit onto branch.
+func Ref(branch string, commit git.ObjectID) git.ReferenceName {
+ return git.ReferenceName(BranchDir(branch) + commit.String())
+}
+
+// RefNameValid reports whether name is refs/graft/proposals/<16 hex>/<object ID>.
+func RefNameValid(name git.ReferenceName) bool {
+ rest, ok := strings.CutPrefix(name.String(), RefPrefix)
+ if !ok {
+ return false
+ }
+ dir, commit, ok := strings.Cut(rest, "/")
+ return ok && len(dir) == 16 && isHex(dir) && (len(commit) == 40 || len(commit) == 64) && isHex(commit)
+}
+
+func isHex(s string) bool {
+ for _, c := range s {
+ if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
+ return false
+ }
+ }
+ return true
+}
+
+// Op is one update of a proposal reference, from OldOID to NewOID; a NewOID that is the zero
+// object ID deletes it, and an OldOID that is the zero object ID creates it. Every update is a
+// compare-and-swap against OldOID.
+type Op struct {
+ Name git.ReferenceName
+ NewOID, OldOID git.ObjectID
+}
+
+// Update is the one writer of the proposal references: it refuses any name but
+// refs/graft/proposals/<16 hex>/<object ID> before git runs, and updates them as the server,
+// without the identity of the request, through the reference transaction like any update (and so
+// the voter in authoritative storage mode). The agent whose land made the proposals may not write
+// refs/graft/ itself (the reference-transaction hook reserves it), and its policy does not judge
+// the server's write; who caused it is in the proposals' commit.
+func Update(ctx context.Context, repo *localrepo.Repo, ops []Op) error {
+ for _, op := range ops {
+ if !RefNameValid(op.Name) {
+ return fmt.Errorf("refusing to update %q: not a proposal reference", op.Name)
+ }
+ }
+ ctx = identity.NewContext(ctx, identity.Identity{})
+ updater, err := updateref.New(ctx, repo)
+ if err != nil {
+ return fmt.Errorf("updating proposal references: %w", err)
+ }
+ defer func() { _ = updater.Close() }()
+ if err := updater.Start(); err != nil {
+ return fmt.Errorf("updating proposal references: %w", err)
+ }
+ for _, op := range ops {
+ if err := updater.Update(op.Name, op.NewOID, op.OldOID); err != nil {
+ return fmt.Errorf("updating %s: %w", op.Name, err)
+ }
+ }
+ if err := updater.Commit(); err != nil {
+ return fmt.Errorf("updating proposal references: %w", err)
+ }
+ return nil
+}
+
+// Prune deletes, among the proposal references under dir (a branch's BranchDir, or RefPrefix for
+// every branch), those older than ttl, those beyond the newest MaxPerBranch of each branch, and
+// drop when it is set. The age is the committer date of the proposals' commit, which the server
+// writes with its own clock (never the request's). Each deletion expects the target it read. It
+// returns the number of references deleted.
+func Prune(ctx context.Context, repo *localrepo.Repo, dir string, ttl time.Duration, drop git.ReferenceName, now time.Time) (int, error) {
+ if !strings.HasPrefix(dir, RefPrefix) {
+ return 0, fmt.Errorf("pruning proposals: %q is not under %s", dir, RefPrefix)
+ }
+ refs, err := repo.GetReferences(ctx, dir)
+ if err != nil {
+ return 0, fmt.Errorf("listing the proposals under %s: %w", dir, err)
+ }
+ objectHash, err := repo.ObjectHash(ctx)
+ if err != nil {
+ return 0, fmt.Errorf("detecting object hash: %w", err)
+ }
+ type dated struct {
+ ref git.Reference
+ when time.Time
+ }
+ byBranch := map[string][]dated{}
+ var remove []Op
+ for _, r := range refs {
+ if !RefNameValid(r.Name) || r.IsSymbolic {
+ continue // not written by Update; nothing of the server's to expire
+ }
+ del := Op{Name: r.Name, NewOID: objectHash.ZeroOID, OldOID: git.ObjectID(r.Target)}
+ if r.Name == drop {
+ remove = append(remove, del)
+ continue
+ }
+ c, err := repo.ReadCommit(ctx, git.Revision(r.Target))
+ if err != nil {
+ return 0, fmt.Errorf("reading %s: %w", r.Name, err)
+ }
+ when := c.GetCommitter().GetDate().AsTime()
+ if now.Sub(when) > ttl {
+ remove = append(remove, del)
+ continue
+ }
+ branch := strings.SplitN(strings.TrimPrefix(r.Name.String(), RefPrefix), "/", 2)[0]
+ byBranch[branch] = append(byBranch[branch], dated{r, when})
+ }
+ for _, kept := range byBranch {
+ sort.Slice(kept, func(i, j int) bool { return kept[i].when.After(kept[j].when) })
+ for _, d := range kept[min(MaxPerBranch, len(kept)):] {
+ remove = append(remove, Op{Name: d.ref.Name, NewOID: objectHash.ZeroOID, OldOID: git.ObjectID(d.ref.Target)})
+ }
+ }
+ if len(remove) == 0 {
+ return 0, nil
+ }
+ if err := Update(ctx, repo, remove); err != nil {
+ return 0, fmt.Errorf("pruning proposals: %w", err)
+ }
+ return len(remove), nil
+}
diff --git a/internal/graft/proposals/proposals_test.go b/internal/graft/proposals/proposals_test.go
new file mode 100755
index 0000000..f359d34
--- /dev/null
+++ b/internal/graft/proposals/proposals_test.go
@@ -0,0 +1,139 @@
+package proposals
+
+import (
+ "fmt"
+ "strings"
+ "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/git/localrepo"
+ "github.com/feder-cr/git-ai/internal/graft/agentrefs"
+ "github.com/feder-cr/git-ai/internal/helper/text"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"
+)
+
+func TestMain(m *testing.M) {
+ testhelper.Run(m)
+}
+
+// setup returns a repository and a writer of commits dated as given.
+func setup(t *testing.T) (*localrepo.Repo, string, func(time.Time) git.ObjectID, func(string) []string) {
+ t.Helper()
+ ctx := testhelper.Context(t)
+ cfg := testcfg.Build(t)
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})
+ repo := localrepo.NewTestRepo(t, cfg, repoProto)
+ n := 0
+ commit := func(when time.Time) git.ObjectID {
+ n++
+ return gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage(fmt.Sprintf("proposals %d", n)), gittest.WithCommitterDate(when))
+ }
+ list := func(prefix string) []string {
+ return strings.Fields(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "for-each-ref", "--format=%(refname)", prefix)))
+ }
+ return repo, repoPath, commit, list
+}
+
+// TestUpdate_onlyProposalReferences: the one writer of the proposal references refuses any other
+// reference before git runs, whatever the caller passes; nothing of the batch is written.
+func TestUpdate_onlyProposalReferences(t *testing.T) {
+ t.Parallel()
+
+ repo, _, commit, list := setup(t)
+ ctx := testhelper.Context(t)
+ c := commit(time.Now())
+ valid := Ref("main", c)
+ require.True(t, RefNameValid(valid))
+ for _, name := range []git.ReferenceName{
+ "refs/heads/main",
+ "refs/graft/green/main",
+ git.ReferenceName("refs/graft/proposals/x/" + c.String()),
+ git.ReferenceName(strings.TrimSuffix(BranchDir("main"), "/")),
+ git.ReferenceName(BranchDir("main") + "not-an-oid"),
+ git.ReferenceName(BranchDir("main") + c.String() + "/x"),
+ } {
+ err := Update(ctx, repo, []Op{{Name: valid, NewOID: c, OldOID: gittest.DefaultObjectHash.ZeroOID}, {Name: name, NewOID: c, OldOID: gittest.DefaultObjectHash.ZeroOID}})
+ require.ErrorContains(t, err, "not a proposal reference", "%s", name)
+ }
+ require.Empty(t, list(RefPrefix), "nothing was written")
+}
+
+// TestPrune: per branch, proposals past the TTL go, and at most MaxPerBranch of the newest stay;
+// the whole prefix prunes every branch, idle or deleted ones included.
+func TestPrune(t *testing.T) {
+ t.Parallel()
+
+ repo, _, commit, list := setup(t)
+ ctx := testhelper.Context(t)
+ now := time.Now()
+ zero := gittest.DefaultObjectHash.ZeroOID
+ name := func(branch string, i int) git.ReferenceName {
+ return git.ReferenceName(fmt.Sprintf("%s%040x", BranchDir(branch), i+1))
+ }
+ var ops []Op
+ // main: 22 recent proposals and an old one; idle: one old proposal, on a branch nothing lands on.
+ for i := 0; i < 22; i++ {
+ ops = append(ops, Op{Name: name("main", i), NewOID: commit(now.Add(-time.Duration(i+1) * time.Minute)), OldOID: zero})
+ }
+ ops = append(ops, Op{Name: name("main", 100), NewOID: commit(now.Add(-8 * 24 * time.Hour)), OldOID: zero})
+ ops = append(ops, Op{Name: name("idle", 0), NewOID: commit(now.Add(-8 * 24 * time.Hour)), OldOID: zero})
+ ops = append(ops, Op{Name: name("fresh", 0), NewOID: commit(now.Add(-time.Hour)), OldOID: zero})
+ require.NoError(t, Update(ctx, repo, ops))
+
+ // The branch's prune touches its own directory only.
+ n, err := Prune(ctx, repo, BranchDir("main"), 7*24*time.Hour, name("main", 0), now)
+ require.NoError(t, err)
+ // 22 recent: the dropped one goes, and of the 21 left the oldest is beyond 20; plus the old one.
+ require.Equal(t, 3, n, "the dropped one, the old one, and the oldest beyond 20")
+ refs := list(BranchDir("main"))
+ require.Len(t, refs, 20)
+ require.NotContains(t, refs, name("main", 0).String(), "dropped")
+ require.NotContains(t, refs, name("main", 100).String(), "past the TTL")
+ require.NotContains(t, refs, name("main", 21).String(), "beyond 20")
+ require.Contains(t, refs, name("main", 20).String())
+ require.Contains(t, list(BranchDir("idle")), name("idle", 0).String(), "another branch is not touched")
+
+ // The housekeeping prune covers every branch: the idle branch's old proposal expires.
+ n, err = Prune(ctx, repo, RefPrefix, 7*24*time.Hour, "", now)
+ require.NoError(t, err)
+ require.Equal(t, 1, n)
+ require.Empty(t, list(BranchDir("idle")))
+ require.Len(t, list(BranchDir("fresh")), 1)
+
+ _, err = Prune(ctx, repo, "refs/heads/", time.Hour, "", now)
+ require.ErrorContains(t, err, "is not under")
+}
+
+// TestUpdate_compareAndSwap: a deletion that expects a target the reference no longer has fails,
+// and the reference stays.
+func TestUpdate_compareAndSwap(t *testing.T) {
+ t.Parallel()
+
+ repo, _, commit, list := setup(t)
+ ctx := testhelper.Context(t)
+ zero := gittest.DefaultObjectHash.ZeroOID
+ first, second := commit(time.Now()), commit(time.Now())
+ ref := Ref("main", first)
+ require.NoError(t, Update(ctx, repo, []Op{{Name: ref, NewOID: second, OldOID: zero}}))
+ require.Error(t, Update(ctx, repo, []Op{{Name: ref, NewOID: zero, OldOID: first}}), "the reference moved to second")
+ require.Equal(t, []string{ref.String()}, list(RefPrefix))
+ require.NoError(t, Update(ctx, repo, []Op{{Name: ref, NewOID: zero, OldOID: second}}))
+ require.Empty(t, list(RefPrefix))
+}
+
+// TestRefPrefix_isAServerNamespace: the proposals' namespace is one agentrefs lists, so that an
+// agent cannot reach it by its short form either.
+func TestRefPrefix_isAServerNamespace(t *testing.T) {
+ t.Parallel()
+
+ var listed []string
+ for _, ns := range agentrefs.ServerNamespaces {
+ listed = append(listed, agentrefs.HiddenPrefix+ns+"/")
+ }
+ require.Contains(t, listed, RefPrefix)
+}
diff --git a/internal/graft/verify/green_namespace_test.go b/internal/graft/verify/green_namespace_test.go
new file mode 100644
index 0000000..a7d2981
--- /dev/null
+++ b/internal/graft/verify/green_namespace_test.go
@@ -0,0 +1,21 @@
+package verify
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/graft/agentrefs"
+)
+
+// TestGreenRefPrefix_isAServerNamespace: the green references' namespace is one agentrefs lists,
+// so that agents do not see it, nor reach it by its short form.
+func TestGreenRefPrefix_isAServerNamespace(t *testing.T) {
+ t.Parallel()
+
+ var listed []string
+ for _, ns := range agentrefs.ServerNamespaces {
+ listed = append(listed, agentrefs.HiddenPrefix+ns+"/")
+ }
+ require.Contains(t, listed, greenRefPrefix)
+}