diff --git a/changelog.d/fixed-19267-agent-signoff-only-own-commits.md b/changelog.d/fixed-19267-agent-signoff-only-own-commits.md new file mode 100644 index 0000000000..a3d22cea17 --- /dev/null +++ b/changelog.d/fixed-19267-agent-signoff-only-own-commits.md @@ -0,0 +1 @@ +- Agents repairing a `no-dco` PR now sign off only the commits they authored. The FIX-BEFORE-NEW kick used to say `git rebase --signoff `, which appended the agent's bot `Signed-off-by` to a person's commit and failed the "Sign-off survives the squash" check (#19102); it now names a `git rebase --exec` that amends with `--signoff` only when the commit's author is the agent, and tells the agent to leave other people's commits alone and ask their author (#19267). The sandbox push broker's refusal of a lane `Signed-off-by` on a commit another identity authored now says why and what to do instead. diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index d1295e20d4..8599dba9e7 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -335,6 +335,14 @@ func resolveRedPRFixer(cfg *config.Config, owner string, available func(string) return "" } +// redPRFixOwnSignoffExec is the `git rebase --exec` step the no-dco repair +// advice names: it amends with --signoff only a commit whose author email is +// the agent's own (git var GIT_AUTHOR_IDENT honours the lane's pinned +// GIT_AUTHOR_* env), so a person's commit on the PR keeps exactly the +// trailers its author wrote. A blanket `git rebase --signoff` appended the +// agent's bot sign-off to a human's commit on hivecommons/hive#19102. +const redPRFixOwnSignoffExec = `'case "$(git var GIT_AUTHOR_IDENT)" in *"<$(git log -1 --format=%ae)>"*) git commit --amend --no-edit --signoff;; esac'` + // redPRFixKick renders the targeted FIX-BEFORE-NEW kick a re-engagement // delivers. It names ONE PR — the stuck one — with its evidence, so the kick is // answerable on its own rather than relying on the owner's next cadence kick @@ -347,7 +355,8 @@ func redPRFixKick(o escalation.Observation, rerouted bool) string { } b.WriteString("Repair it BEFORE claiming new issues or opening any new PR:\n") if slices.Contains(o.Labels, stagelabels.NoDCOLabel) { - fmt.Fprintf(&b, " FIRST (required): the PR is %s — it inherited unsigned commits. Re-sign them: gh pr checkout %d --repo %s → git rebase --signoff (original authors preserved) → git push --force-with-lease\n", stagelabels.NoDCOLabel, o.Number, o.Repo) + fmt.Fprintf(&b, " FIRST (required): the PR is %s, a commit is missing its Signed-off-by. Sign off ONLY the commits you authored: gh pr checkout %d --repo %s → git rebase --exec %s → git push --force-with-lease\n", stagelabels.NoDCOLabel, o.Number, o.Repo, redPRFixOwnSignoffExec) + b.WriteString(" NEVER add your Signed-off-by to a commit someone else wrote (git rebase --signoff signs every commit in the range, theirs too): leave their commits as they are and comment on the PR asking the author to sign them off.\n") b.WriteString(" THEN fix any failing CI the same way.\n") } fmt.Fprintf(&b, " gh pr checkout %d --repo %s → fix → git commit -s → git push\n", o.Number, o.Repo) diff --git a/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go b/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go index d744b54888..34920b124f 100644 --- a/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go +++ b/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go @@ -29,14 +29,36 @@ func TestRedPRFixKickForbidsCIPolling(t *testing.T) { func TestRedPRFixKickNamesDCORehomeFirstForNoDCO(t *testing.T) { got := redPRFixKick(escalation.Observation{Repo: "hivecommons/hive", Number: 14618, Labels: []string{"no-dco"}, FailingChecks: []string{"DCO"}}, false) first := strings.Index(got, "FIRST (required)") - if first < 0 || !strings.Contains(got, "git rebase --signoff") { + if first < 0 || !strings.Contains(got, "--exec "+redPRFixOwnSignoffExec) { t.Fatalf("no-dco kick lacks the DCO re-sign action:\n%s", got) } if first > strings.Index(got, "→ fix → git commit -s") { t.Errorf("DCO re-sign must come before the generic fix step:\n%s", got) } plain := redPRFixKick(escalation.Observation{Repo: "hivecommons/hive", Number: 1}, false) - if strings.Contains(plain, "git rebase --signoff") { + if strings.Contains(plain, "FIRST (required)") || strings.Contains(plain, "--signoff") { t.Errorf("DCO action leaked into a signed PR's kick:\n%s", plain) } } + +// hivecommons/hive#19102: `git rebase --signoff ` appended the agent's +// bot Signed-off-by to a person's commit and broke the "Sign-off survives +// the squash" check. The no-dco advice signs off only the agent's own +// commits and tells it to leave everyone else's alone. +func TestRedPRFixKickSignsOffOnlyOwnCommits(t *testing.T) { + got := redPRFixKick(escalation.Observation{Repo: "hivecommons/hive", Number: 19102, Labels: []string{"no-dco"}}, false) + if strings.Contains(got, "git rebase --signoff ") { + t.Errorf("no-dco kick still tells the agent to rebase --signoff every commit:\n%s", got) + } + for _, want := range []string{"Sign off ONLY the commits you authored", "NEVER add your Signed-off-by to a commit someone else wrote", "asking the author to sign them off"} { + if !strings.Contains(got, want) { + t.Errorf("no-dco kick missing %q:\n%s", want, got) + } + } + // The exec amends only when the commit's author email is the agent's own. + for _, want := range []string{"git var GIT_AUTHOR_IDENT", "git log -1 --format=%ae", "git commit --amend --no-edit --signoff"} { + if !strings.Contains(redPRFixOwnSignoffExec, want) { + t.Errorf("own-signoff exec %q lacks %q", redPRFixOwnSignoffExec, want) + } + } +} diff --git a/src/crux/pushbroker/lease_test.go b/src/crux/pushbroker/lease_test.go index 49ad7ae82d..5e9189efcf 100644 --- a/src/crux/pushbroker/lease_test.go +++ b/src/crux/pushbroker/lease_test.go @@ -3,6 +3,8 @@ package pushbroker import ( "context" "errors" + "os" + "path/filepath" "slices" "strings" "testing" @@ -83,3 +85,56 @@ func TestBrokerReportsLeaseRejection(t *testing.T) { t.Fatalf("err = %v pushed=%v", err, res.Pushed) } } + +// hivecommons/hive#19102: an agent ran `git rebase --signoff` over a PR that +// carried a person's commit, appending its own bot Signed-off-by under the +// author's. On a leased push the outgoing commit of exactly that shape (a +// human author, the lane as committer, the author's and the lane's +// trailers) is refused with the reason, and nothing is pushed. +func TestBrokerRefusesLaneSignoffAddedToAnotherAuthorsCommit(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "base.txt", "base\n") + lease := strings.TrimSpace(runGitOutput(t, dir, "rev-parse", "HEAD")) + if err := os.WriteFile(filepath.Join(dir, "fix.txt"), []byte("fix\n"), 0o644); err != nil { + t.Fatal(err) + } + runGit(t, dir, "add", "fix.txt") + runGit(t, dir, "commit", "--author", "Human Author ", "-m", + "fix from a person\n\nSigned-off-by: Human Author \nSigned-off-by: Hive Test ") + r := &recordingRunner{} + res, err := (&Broker{Workspace: dir, Branch: "work", BaseRef: lease, LeaseSHA: lease, Repo: "hivecommons/hive", Minter: fakeMinter{"ghs_lease"}, Runner: r}).Run(context.Background()) + if err == nil { + t.Fatalf("a lane sign-off on another author's commit was pushed: %+v", res) + } + for _, want := range []string{"refusing to push commit", "authored by Human Author ", "Hive Test's Signed-off-by trailer", "signs off only commits it authored", "git rebase --signoff"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("refusal %q lacks %q", err, want) + } + } + if res.Pushed || r.argsOnPush != nil { + t.Fatalf("broker pushed after refusing the sign-off: res=%+v args=%v", res, r.argsOnPush) + } +} + +// The same leased push still carries the lane's own signed commits, and a +// person's commit left exactly as they signed it passes beside them: only a +// lane trailer on someone else's commit is refused. +func TestBrokerPushesOwnSignedCommitsBesideAnUntouchedAuthorsCommit(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "base.txt", "base\n") + lease := strings.TrimSpace(runGitOutput(t, dir, "rev-parse", "HEAD")) + if err := os.WriteFile(filepath.Join(dir, "theirs.txt"), []byte("theirs\n"), 0o644); err != nil { + t.Fatal(err) + } + runGit(t, dir, "add", "theirs.txt") + runGit(t, dir, "commit", "--author", "Human Author ", "-m", "their fix\n\nSigned-off-by: Human Author ") + writeCommit(t, dir, "mine.txt", "mine\n") + r := &recordingRunner{} + res, err := (&Broker{Workspace: dir, Branch: "work", BaseRef: lease, LeaseSHA: lease, Repo: "hivecommons/hive", Minter: fakeMinter{"ghs_lease"}, Runner: r}).Run(context.Background()) + if err != nil { + t.Fatalf("Run: %v (res=%+v)", err, res) + } + if !res.Pushed || r.argsOnPush == nil { + t.Fatalf("own signed commits were not pushed: %+v", res) + } +} diff --git a/src/crux/pushbroker/pushbroker.go b/src/crux/pushbroker/pushbroker.go index 707bceac1f..6d7665d3a6 100644 --- a/src/crux/pushbroker/pushbroker.go +++ b/src/crux/pushbroker/pushbroker.go @@ -443,6 +443,18 @@ var signedOffByRE = regexp.MustCompile(`(?mi)^Signed-off-by:\s*(.*?)\s*<([^<>]+) // "Name ". Only the name/email prefix is used. var gitIdentRE = regexp.MustCompile(`^(.*?)\s*<([^<>]*)>`) +// foreignSignoffAdvice closes the refusal of a lane Signed-off-by on a +// commit the lane did not write. A sign-off certifies the signer wrote the +// change (DCO), so a lane may only sign its own commits; `git rebase +// --signoff` over a branch with someone else's commits appends the lane's +// trailer to theirs too and breaks the "Sign-off survives the squash" check +// (seen on hivecommons/hive#19102). +const foreignSignoffAdvice = "a lane signs off only commits it authored. Do not run git rebase --signoff over someone else's commits; keep their commits as they wrote them and leave DCO remediation to the author (ask them on the PR to sign off)" + +// rejectForgedLaneSignoffs judges every outgoing non-merge commit against +// the pushing lane's identity: a commit the lane authored and committed must +// carry the lane's Signed-off-by, and a commit someone else authored (or +// committed) must not. func (b *Broker) rejectForgedLaneSignoffs(ctx context.Context, base string, baseExists bool) error { laneName, laneEmail, err := b.laneGitIdentity(ctx) if err != nil { @@ -500,7 +512,7 @@ func (b *Broker) rejectForgedLaneSignoffs(ctx context.Context, base string, base "lane", fmt.Sprintf("%s <%s>", laneName, laneEmail)) } if laneSigned { - return fmt.Errorf("pushbroker: refusing to push commit %s authored by %s <%s> and committed by %s <%s> with %s's Signed-off-by trailer; leave DCO remediation to the author", shortSHA(sha), authorName, authorEmail, committerName, committerEmail, laneName) + return fmt.Errorf("pushbroker: refusing to push commit %s authored by %s <%s> and committed by %s <%s> with %s's Signed-off-by trailer: %s", shortSHA(sha), authorName, authorEmail, committerName, committerEmail, laneName, foreignSignoffAdvice) } } return nil diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index d3280d51ec..d38d17c2ea 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -492,6 +492,18 @@ is re-read and returns the PR to the normal path. A PR with no non-merge commits, or whose commits could not be read, is *unknown*: its existing DCO pills are left alone and no stage decision is made from DCO. +When a hive agent repairs a red `no-dco` PR (the FIX-BEFORE-NEW kick, +`redPRFixKick` in `cmd/hive/main_sweeps.go`), it signs off **only the commits +it authored**: `git rebase --exec` amends with `--signoff` only a commit +whose author email is the agent's own lane identity. A commit someone else +wrote keeps exactly the trailers its author gave it, and the agent asks that +author on the PR to sign it off. A blanket `git rebase --signoff` appended a +bot `Signed-off-by` under a person's own and failed the "Sign-off survives the +squash" check ([#19102](https://github.com/hivecommons/hive/pull/19102)). The +sandbox push broker enforces the same rule: it refuses to push a commit that +carries the lane's `Signed-off-by` when another identity authored or committed +it (see [Sandbox isolation](../sandbox-isolation.md)). + The labels are written on every managed repo; the **072 routing is per repo**. It applies only where the repo actually enforces DCO — detected when a `dco` / `DCO` context is in the branch's required checks or reported on diff --git a/src/docs/sandbox-isolation.md b/src/docs/sandbox-isolation.md index e2d6a8a7fc..fa751c7324 100644 --- a/src/docs/sandbox-isolation.md +++ b/src/docs/sandbox-isolation.md @@ -189,7 +189,7 @@ A target in another repository is ignored, because the workspace clones the prim **Workspace prep.** Hive reads the PR and checks it before the sandbox starts. The PR must be open. Its head branch must live in the kick's repository, so a fork PR is refused. The PR's `agent_pr_created` record must credit it to this agent. If that record is older than the audit log's 90-day lookback, hive reads the body's attribution trailer instead, but only on a PR the hive's App authored. Hive then fetches `+refs/heads/:refs/remotes/origin/` with the clone token and checks the head out on ``. If the fetched commit differs from the head GitHub reported, the kick is refused. Hive also prepends a `[hive:sandbox-target-pr]` section to the prompt. It names the PR, branch and head commit, and tells the agent to commit on top, without rebasing or opening a PR. The run's commits are counted from that head. -**The push.** After the publish gate and `publish.verify`, hive reads the PR again. It must still pass every check above, keep the same head branch, and sit at the same head commit. The broker then runs its usual empty-commit, sign-off, protected-path and secret scans over the kick's own commits only (`base = `). It also requires those commits to descend from the checked-out head. It pushes `HEAD:refs/heads/` with `--force-with-lease=refs/heads/:`, so the push lands only if the remote branch still points at that commit. Together these make the push a fast-forward of exactly the head the kick started from: it never overwrites a commit someone else pushed. No new PR is opened. The run record's PR link is the existing PR, and `sandbox_complete` carries `target_pr=`. +**The push.** After the publish gate and `publish.verify`, hive reads the PR again. It must still pass every check above, keep the same head branch, and sit at the same head commit. The broker then runs its usual empty-commit, sign-off, protected-path and secret scans over the kick's own commits only (`base = `). The sign-off scan refuses an outgoing commit that carries the lane's own `Signed-off-by` when a different identity authored or committed it: a lane signs off only commits it wrote, so a `git rebase --signoff` over a person's commit never leaves the sandbox, and the refusal names the commit, its author and the fix (leave the author's commits as written and ask them to sign off). Commits the lane authored and committed must carry its trailer; a person's commit left untouched passes. It also requires those commits to descend from the checked-out head. It pushes `HEAD:refs/heads/` with `--force-with-lease=refs/heads/:`, so the push lands only if the remote branch still points at that commit. Together these make the push a fast-forward of exactly the head the kick started from: it never overwrites a commit someone else pushed. No new PR is opened. The run record's PR link is the existing PR, and `sandbox_complete` carries `target_pr=`. **Refusals fail the kick and push nothing.** A closed PR, a fork PR, a PR another agent opened, a PR no hive agent opened, a head that moved since checkout, an unreadable PR or a failed scan all fail the kick with a reason that names the PR. The run record carries the reason, and the audit log gets a `sandbox_target_pr_refused` line with `repo=`, `number=`, `source=` and `reason=`, or the broker's usual `sandbox_broker_rejected`. If the refusal comes after the run, the commits stay in the workspace, and the run record notes them as withheld.