Git designed for AI agents.
operations, docs: a batch attempt judges each member with one policy snapshot
Federico Elia64226082be48
diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.mdindex f398c0d..c9a945b 100644--- a/doc/graft/CONFIGURATION.md+++ b/doc/graft/CONFIGURATION.md@@ -124,7 +124,9 @@ with the graceful-upgrade bootstrap.reference-transaction hook runs, while git holds the references' locks: atightening applies to it even if its earlier checks allowed it. A land batchwhose members were admitted under another policy is refused there, and its- requests land alone, each decided by the policy in force.+ requests land alone, each decided by the policy in force. A batch attempt+ judges each of its members with the policy in force when the attempt+ started: a loosening reaches the members of the next attempt.- Each node reloads its own file: reload every node, then compare their`graft_policy_info`.diff --git a/doc/graft/PLAN12.md b/doc/graft/PLAN12.mdindex 5c0c572..d4b8998 100644--- a/doc/graft/PLAN12.md+++ b/doc/graft/PLAN12.md@@ -91,8 +91,16 @@ The one exception today is the land batch: the hook does not re-check a batch'sthe batch's commit would not apply to it. Fix: the batch records the generation it admittedits members under, and carries it in the hooks payload of its combined update; the hook refusesa batch update whose admission generation is not the current one (`policy changed during the-batch`), and the batch executor rebuilds (its existing rebuild path), admitting every member-again under the new snapshot. A member the new policy denies then fails alone, as today.+batch`), and the batch executor treats it as a refused combined update: each of its requests+lands alone, decided by its pre-receive check and the hook under the policy in force. A member+the new policy denies then fails alone, as today. (As implemented: the requests land alone, the+fallback of a refused batch, rather than in a rebuilt batch; a batch of N then costs N lands+after a reload.)++Within one batch attempt, every policy check of a member (its admission, and the policy check+before a resolution) decides with the snapshot the attempt took when it started, so that one+member is never judged by two generations. A loosening during an attempt applies to the members+of the next attempt; a member denied by the attempt's snapshot is denied, and the client retries.Callers that are not agents (shared token, unauthenticated) are checked only by pre-receive,before the references are locked: a swap between their pre-receive and their commit can let one@@ -144,7 +152,7 @@ closed.the hook; the branch is unchanged;- loosening: an agent refused, the policy reloaded to allow it, the same land again → lands;- batch: a batch whose admission generation changes before its commit → the hook refuses the- combined update, the batch rebuilds under the new policy, the newly denied member fails+ combined update, its requests land alone under the new policy, the newly denied member failsalone, the others land.- (PM) An invalid policy reloaded while agents land: no decision is ever taken without a policy(no nil snapshot, no empty rule list where the startup policy had rules); every land is decideddiff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.goindex 16b7a13..90fef1a 100644--- a/internal/gitaly/service/operations/land_batch_server.go+++ b/internal/gitaly/service/operations/land_batch_server.go@@ -255,7 +255,8 @@ type serverLandBatchStepper struct {func (st *serverLandBatchStepper) Land(ctx context.Context, tip git.ObjectID, req *landBatchRequest) (landStep, error) {defer observeLandBatchPhase(landBatchPhaseLandMember, time.Now())- step, err := st.decide(ctx, tip, req)+ // The member's own policy checks during the attempt decide with the policy that admits it.+ step, err := st.decide(policystore.WithSnapshot(ctx, st.policy), tip, req)if err != nil {return step, err}diff --git a/internal/gitaly/service/operations/land_inline.go b/internal/gitaly/service/operations/land_inline.goindex e8dd0dc..9970e5b 100644--- a/internal/gitaly/service/operations/land_inline.go+++ b/internal/gitaly/service/operations/land_inline.go@@ -98,7 +98,7 @@ func (s *Server) landResolveInline(ctx context.Context, in *gitalypb.UserLandReq// An agent that may not update the branch gets nothing resolved, as out of the lane (the policy first):// the land goes out of the lane, which refuses it with the policy's message.reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))- if err := hook.CheckAgentPolicy(ctx, s.graftInForce(), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {+ if err := hook.CheckAgentPolicy(ctx, s.graftInForce(ctx), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {return landInlineNotTried}diff --git a/internal/gitaly/service/operations/land_policy_reload_test.go b/internal/gitaly/service/operations/land_policy_reload_test.goindex 3afe8f9..dc54327 100644--- a/internal/gitaly/service/operations/land_policy_reload_test.go+++ b/internal/gitaly/service/operations/land_policy_reload_test.go@@ -141,7 +141,7 @@ func testUserLandPolicyReloadedInFlight(t *testing.T, ctx context.Context) {require.ElementsMatch(t, []string{"base", "b"}, mainFiles(t, cfg, repoPath))})- t.Run("a batch admitted under another policy is admitted again, and the newly denied member fails alone", func(t *testing.T) {+ t.Run("a batch admitted under another policy lands its requests alone, and the newly denied one fails alone", func(t *testing.T) {t.Parallel()ctx, cfg, client, reloader := setupPolicyReload(t, ctx, agentBatchingGraft(3, nil))@@ -380,3 +380,23 @@ func testUserLandPolicyReloadsDuringConcurrentLands(t *testing.T, ctx context.Corequire.Len(t, mainFiles(t, cfg, repoPath), 1+landsPerAgent*(agents-1)+landed["agent-0"])}++// TestServer_graftInForce: a policy check decides with the snapshot its context carries, which a+// land batch attempt gives its members, even after a reload; without one, with the policy in force.+func TestServer_graftInForce(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ admitting := []config.GraftPolicyRule{{Actor: "*", Allow: true}}+ reloaded := denyMainFor("bot")+ store := policystore.New(admitting)+ s := &Server{graft: config.Graft{Standalone: true}, graftPolicy: store}+ snapshot := store.Current()++ outcome, err := store.Reload(config.Graft{Standalone: true, Policy: reloaded}, config.Graft{Standalone: true})+ require.NoError(t, err)+ require.Equal(t, policystore.Applied, outcome.Result)++ require.Equal(t, reloaded, s.graftInForce(ctx).Policy)+ require.Equal(t, admitting, s.graftInForce(policystore.WithSnapshot(ctx, snapshot)).Policy)+}diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.goindex 53a2b42..6284814 100644--- a/internal/gitaly/service/operations/land_reland.go+++ b/internal/gitaly/service/operations/land_reland.go@@ -197,7 +197,7 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa// alone, so it comes first, as in landResolveAt. Otherwise a denied agent could hold the// repository's verify slot, and its tree would run in the operator's sandbox.reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))- if err := hook.CheckAgentPolicy(ctx, s.graftInForce(), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {+ if err := hook.CheckAgentPolicy(ctx, s.graftInForce(ctx), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {return nil, err}diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.goindex e426ebd..e92e457 100644--- a/internal/gitaly/service/operations/land_resolve.go+++ b/internal/gitaly/service/operations/land_resolve.go@@ -103,7 +103,7 @@ func (s *Server) landResolveAt(// 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.graftInForce(), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {+ if err := hook.CheckAgentPolicy(ctx, s.graftInForce(ctx), fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {return landListing{}, nil, &landPolicyDeniedError{err: err}}l, err := landListConflicts(ctx, repo, tip, commitID, s.resolveCfg.MaxFileBytesOrDefault())diff --git a/internal/gitaly/service/operations/merge_to_ref.go b/internal/gitaly/service/operations/merge_to_ref.goindex 912f572..8d2692e 100644--- a/internal/gitaly/service/operations/merge_to_ref.go+++ b/internal/gitaly/service/operations/merge_to_ref.go@@ -154,7 +154,7 @@ func (s *Server) UserMergeToRef(ctx context.Context, request *gitalypb.UserMerge// implementation doesn't invoke hooks, so we don't either.// The reference-transaction hook enforces the standalone policy, but Git only// reports that it failed. Checking the policy here returns the denial itself.- if err := hook.CheckAgentPolicy(ctx, s.graftInForce(), fmt.Sprintf("%s %s %s\n", oldTargetOID, mergeOID, request.GetTargetRef())); err != nil {+ if err := hook.CheckAgentPolicy(ctx, s.graftInForce(ctx), fmt.Sprintf("%s %s %s\n", oldTargetOID, mergeOID, request.GetTargetRef())); err != nil {return nil, err}diff --git a/internal/gitaly/service/operations/rebase_to_ref.go b/internal/gitaly/service/operations/rebase_to_ref.goindex fd67d6f..7b47d49 100644--- a/internal/gitaly/service/operations/rebase_to_ref.go+++ b/internal/gitaly/service/operations/rebase_to_ref.go@@ -104,7 +104,7 @@ func (s *Server) UserRebaseToRef(ctx context.Context, request *gitalypb.UserReba// The reference-transaction hook enforces the standalone policy, but Git only// reports that it failed. Checking the policy here returns the denial itself.- if err := hook.CheckAgentPolicy(ctx, s.graftInForce(), fmt.Sprintf("%s %s %s\n", oldTargetOID, rebasedOID, request.GetTargetRef())); err != nil {+ if err := hook.CheckAgentPolicy(ctx, s.graftInForce(ctx), fmt.Sprintf("%s %s %s\n", oldTargetOID, rebasedOID, request.GetTargetRef())); err != nil {return nil, err}diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.goindex 0c17a37..4b13a04 100644--- a/internal/gitaly/service/operations/server.go+++ b/internal/gitaly/service/operations/server.go@@ -168,10 +168,15 @@ func (s *Server) ensureStorageFresh(ctx context.Context, repo *gitalypb.Repositoreturn s.storageSync.EnsureFresh(ctx, repo)}-// graftInForce returns the [graft] configuration with the standalone access policy in force: the-// policy checks of the RPCs decide with the policy the server holds now.-func (s *Server) graftInForce() config.Graft {+// graftInForce returns the [graft] configuration with the standalone access policy that decides the+// policy checks of ctx: the snapshot ctx carries, which a land batch attempt gives its members+// (policystore.WithSnapshot), or else the policy the server holds now.+func (s *Server) graftInForce(ctx context.Context) config.Graft {graft := s.graft- graft.Policy = s.graftPolicy.Current().Rules+ if snapshot, ok := policystore.SnapshotFrom(ctx); ok {+ graft.Policy = snapshot.Rules+ } else {+ graft.Policy = s.graftPolicy.Current().Rules+ }return graft}diff --git a/internal/graft/policystore/store.go b/internal/graft/policystore/store.goindex 791bdc0..8eeaa34 100644--- a/internal/graft/policystore/store.go+++ b/internal/graft/policystore/store.go@@ -264,3 +264,18 @@ func AdmissionGeneration(ctx context.Context) uint64 {generation, _ := ctx.Value(admissionKey{}).(uint64)return generation}++type snapshotKey struct{}++// WithSnapshot returns ctx carrying the policy snapshot that decides the checks made with it: a+// land batch attempt admits each member with one snapshot, and the member's own checks during the+// attempt, such as the policy check before a resolution, use the same one (PLAN12, section 5).+func WithSnapshot(ctx context.Context, snapshot *Snapshot) context.Context {+ return context.WithValue(ctx, snapshotKey{}, snapshot)+}++// SnapshotFrom returns the snapshot of WithSnapshot, if ctx carries one.+func SnapshotFrom(ctx context.Context) (*Snapshot, bool) {+ snapshot, ok := ctx.Value(snapshotKey{}).(*Snapshot)+ return snapshot, ok && snapshot != nil+}