From 1b71e637319b8f86c8466d32e4d53f96f8d8f18a Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 20:31:16 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20sandbox:=20push=20fix=20kicks?= =?UTF-8?q?=20to=20the=20agent's=20own=20PR=20and=20credit=20sandbox=20PRs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A sandboxed fix kick that names one of the agent's open PRs (explicit [hive:target-pr] line, red-PR or red-CI kick header, first FIX-BEFORE-NEW row, or a single topic claim) now checks out that PR's head branch and the push broker adds the kick's commits to it. The push needs the PR open, same-repo, credited to this agent by its agent_pr_created record, still at the checked-out head (re-read before the push and --force-with-lease in the push), and the usual broker scans to pass; otherwise the kick fails with the reason and nothing is pushed. A PR a sandboxed kick opens is credited like a relay-opened PR: attribution trailer, agent_pr_created audit entry and PR-opened hooks through github.Client.CreateAgentPR, or a runtime-emitted agent_pr_created entry when the PR client cannot record it. The governor's head-ref fallback reads the agent out of hive// work branches. Fixes #18574 Fixes #18575 Signed-off-by: Andy Anderson --- .../added-18574-sandbox-existing-pr.md | 1 + src/cmd/hive/merge_eligibility.go | 6 + src/cmd/hive/sandbox_pr_credit_test.go | 47 ++ src/crux/agent/manager_sandbox.go | 76 ++- src/crux/agent/sandbox_executor.go | 64 +- src/crux/agent/sandbox_target_pr.go | 334 ++++++++++ src/crux/agent/sandbox_target_pr_test.go | 575 ++++++++++++++++++ src/crux/agent/sandbox_workspace.go | 13 + src/crux/pr_push_target.go | 56 ++ src/crux/pushbroker/lease_test.go | 85 +++ src/crux/pushbroker/pushbroker.go | 76 ++- src/crux/sandbox/naming.go | 36 ++ src/crux/sandbox/naming_test.go | 19 + src/docs/sandbox-isolation.md | 19 + src/pkg/github/sandbox_agent_pr.go | 133 ++++ src/pkg/github/sandbox_agent_pr_test.go | 210 +++++++ .../scheduler/sandbox_pr_fix_first_test.go | 59 ++ 17 files changed, 1775 insertions(+), 34 deletions(-) create mode 100644 changelog.d/added-18574-sandbox-existing-pr.md create mode 100644 src/cmd/hive/sandbox_pr_credit_test.go create mode 100644 src/crux/agent/sandbox_target_pr.go create mode 100644 src/crux/agent/sandbox_target_pr_test.go create mode 100644 src/crux/pr_push_target.go create mode 100644 src/crux/pushbroker/lease_test.go create mode 100644 src/pkg/github/sandbox_agent_pr.go create mode 100644 src/pkg/github/sandbox_agent_pr_test.go create mode 100644 src/pkg/scheduler/sandbox_pr_fix_first_test.go diff --git a/changelog.d/added-18574-sandbox-existing-pr.md b/changelog.d/added-18574-sandbox-existing-pr.md new file mode 100644 index 0000000000..0e7cbd518a --- /dev/null +++ b/changelog.d/added-18574-sandbox-existing-pr.md @@ -0,0 +1 @@ +- **Sandboxed fix kicks push to the agent's own existing PR, and sandbox-opened PRs are credited to their agent.** A sandboxed kick that names one of the agent's open PRs (an explicit `[hive:target-pr #]` line, the red-PR or red-CI kick header, the first FIX-BEFORE-NEW row, or a single topic claim) checks out that PR's head branch and adds its commits there. Hive pushes only if the PR is open, same-repo, opened by this hive for this agent, still at the head the kick checked out (`--force-with-lease`), and the usual broker scans pass; otherwise the kick fails with the reason and nothing is pushed. A PR a sandboxed kick opens now gets the attribution trailer and `agent_pr_created` audit entry a relay-opened PR gets, so red CI on it kicks the agent that opened it instead of `scanner`. ([#18574](https://github.com/hivecommons/hive/issues/18574), [#18575](https://github.com/hivecommons/hive/issues/18575)) diff --git a/src/cmd/hive/merge_eligibility.go b/src/cmd/hive/merge_eligibility.go index 9917668631..a6dabaf73a 100644 --- a/src/cmd/hive/merge_eligibility.go +++ b/src/cmd/hive/merge_eligibility.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/hivecommons/hive/crux/sandbox" "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/dashboard" "github.com/hivecommons/hive/pkg/escalation" @@ -932,6 +933,11 @@ func prFixAgent(pr github.PullRequest, audited string) string { } } if head := strings.TrimSpace(pr.HeadRef); head != "" { + // A sandboxed kick's work branch is hive//: its + // first segment names no agent (hivecommons/hive#18575). + if agent, ok := sandbox.WorkBranchAgentSlug(head); ok { + return agent + } if agent, _, ok := strings.Cut(head, "/"); ok && agent != "" { return agent } diff --git a/src/cmd/hive/sandbox_pr_credit_test.go b/src/cmd/hive/sandbox_pr_credit_test.go new file mode 100644 index 0000000000..0cfc7020e9 --- /dev/null +++ b/src/cmd/hive/sandbox_pr_credit_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// A sandbox PR's work branch is hive//. Before +// hivecommons/hive#18575 the head-ref fallback credited every one of them to +// an agent named "hive". +func TestPRFixAgentReadsSandboxWorkBranch(t *testing.T) { + pr := github.PullRequest{HeadRef: "hive/dep-bump/20261116120000"} + if got := prFixAgent(pr, ""); got != "dep-bump" { + t.Fatalf("prFixAgent = %q, want dep-bump", got) + } + if got := prFixAgent(github.PullRequest{HeadRef: "quality/fix-1"}, ""); got != "quality" { + t.Fatalf("lane branch fallback changed: %q", got) + } + if got := prFixAgent(pr, "audited"); got != "audited" { + t.Fatalf("the audit record must win: %q", got) + } +} + +// The runtime's fallback credit (a PR client that cannot record it, such as +// the sidecar's) reaches the audit log through the sandbox audit callback, +// which writes the agent as both user and agent. The eval-tick resolver that +// builds ci-failing.json maps it to the agent. +func TestAuditPRAgentsReadsSandboxFallbackCredit(t *testing.T) { + path := filepath.Join(t.TempDir(), "audit.jsonl") + now := time.Now().UTC() + line := `{"ts":"` + now.Format(time.RFC3339) + `","user":"dep-bump","action":"` + github.AuditActionAgentPRCreated + + `","detail":"repo=o/r, number=42, author=hive-app[bot], url=https://github.com/o/r/pull/42, reused=false, head=hive/dep-bump/20261116120000, via=sandbox, agent=dep-bump, backend=claude","agent":"dep-bump"}` + "\n" + if err := os.WriteFile(path, []byte(line), 0o600); err != nil { + t.Fatal(err) + } + got := auditPRAgents("o", now.Add(-auditPRAttributionWindow), path) + if got["o/r#42"] != "dep-bump" { + t.Fatalf("auditPRAgents = %v", got) + } + if owner := github.AuthorAgentForPRFromAudit(path, "o/r", 42); owner != "dep-bump" { + t.Fatalf("red-CI resolver = %q", owner) + } +} diff --git a/src/crux/agent/manager_sandbox.go b/src/crux/agent/manager_sandbox.go index a365511161..88073c6498 100644 --- a/src/crux/agent/manager_sandbox.go +++ b/src/crux/agent/manager_sandbox.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strconv" "strings" "time" @@ -241,7 +242,23 @@ func (m *Manager) startSandboxKickLocked(agent *AgentProcess, message string) er if spec.reviewOnly() { prReader, _ = m.sandboxPRClient.(crux.PRReviewContextReader) } - go m.runSandboxKick(runCtx, agent.Name, runtime, spec, launcher, runner, cloneMinter, pushMinter, pushEnabled, prClient, prReader, mutationBoundary) + // A fix kick that names one of the agent's PRs adds its commits to that + // PR (hivecommons/hive#18574). Only a kick that may push, through a PR + // client that can read the target, gets one: without the reader the + // executor could not check the PR is the agent's own, so the kick opens + // a new PR as before. + var targetReader crux.PRPushTargetReader + if pushEnabled { + if n, source := sandboxTargetPRFromKickMessage(message, m.project.Org+"/"+repo); n > 0 { + if r, ok := m.sandboxPRClient.(crux.PRPushTargetReader); ok { + spec.TargetPR, spec.TargetPRSource, targetReader = n, source, r + } else { + m.logger.Warn("sandbox kick names an existing PR but the PR client cannot read PR targets; the kick opens a new PR instead", + "name", agent.Name, "pr", n, "source", source) + } + } + } + go m.runSandboxKick(runCtx, agent.Name, runtime, spec, launcher, runner, cloneMinter, pushMinter, pushEnabled, prClient, prReader, targetReader, mutationBoundary) return nil } @@ -266,18 +283,19 @@ func (m *Manager) tieredSandboxMinterLocked(tier string) pushbroker.TokenMinter } } -func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec SandboxKickSpec, launcher sandbox.Launcher, runner sandboxCommandRunner, cloneMinter, minter pushbroker.TokenMinter, pushEnabled bool, prClient PRCreator, prReader crux.PRReviewContextReader, mutationBoundary effects.Boundary) { +func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec SandboxKickSpec, launcher sandbox.Launcher, runner sandboxCommandRunner, cloneMinter, minter pushbroker.TokenMinter, pushEnabled bool, prClient PRCreator, prReader crux.PRReviewContextReader, targetReader crux.PRPushTargetReader, mutationBoundary effects.Boundary) { started := time.Now() exec := &SandboxExecutor{ - Launcher: launcher, - Runner: runner, - CloneMinter: cloneMinter, - Minter: minter, - PushEnabled: pushEnabled, - PRClient: prClient, - PRReader: prReader, - Mutation: mutationBoundary, - Logger: m.logger, + Launcher: launcher, + Runner: runner, + CloneMinter: cloneMinter, + Minter: minter, + PushEnabled: pushEnabled, + PRClient: prClient, + PRReader: prReader, + PRTargetReader: targetReader, + Mutation: mutationBoundary, + Logger: m.logger, } res, err := exec.Run(ctx, spec) ended := time.Now() @@ -356,6 +374,9 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec if errors.Is(err, ErrSandboxRunReportedFailure) { m.auditSandbox(name, "sandbox_pr_withheld", fmt.Sprintf("workspace=%s commits=%d branch=%s reason=%s", res.Workspace, res.CommitCount, res.Branch, res.PRWithheld)) } + if errors.Is(err, ErrSandboxTargetPRRefused) { + m.auditSandbox(name, "sandbox_target_pr_refused", fmt.Sprintf("repo=%s/%s, number=%d, source=%s, commits=%d, reason=%s", spec.Org, spec.Repo, spec.TargetPR, spec.TargetPRSource, res.CommitCount, err.Error())) + } m.auditSandbox(name, "sandbox_failed", err.Error()) if res.Broker != nil && res.Broker.Error != "" { m.auditSandbox(name, "sandbox_broker_rejected", res.Broker.Error) @@ -380,7 +401,40 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec if res.PR != nil && res.PR.URL != "" { detail += " pr=" + res.PR.URL } + if res.TargetPR > 0 { + detail += fmt.Sprintf(" target_pr=%d", res.TargetPR) + } m.auditSandbox(name, "sandbox_complete", detail) + m.creditSandboxPR(name, spec, res, effectiveBackend(agent), agent.Config.Model) +} + +// creditSandboxPR records the agent_pr_created entry for a PR the kick +// opened when the PR client did not record it itself (a client without +// crux.AgentPRCreator, such as the sidecar's callback adapter). The detail +// carries the repo=/number= pairs every governor resolver parses, so the PR +// routes back to this agent (hivecommons/hive#18575). A push to an existing +// PR opened nothing and records nothing: that PR's credit already exists. +func (m *Manager) creditSandboxPR(name string, spec SandboxKickSpec, res SandboxKickResult, backend, model string) { + if res.PR == nil || res.PR.Number <= 0 || res.PRAttributed || res.TargetPR > 0 { + return + } + pairs := []string{ + "repo=" + spec.Org + "/" + spec.Repo, + "number=" + strconv.Itoa(res.PR.Number), + "author=" + res.PR.Author, + "url=" + res.PR.URL, + "reused=" + strconv.FormatBool(res.PR.AlreadyExisted), + "head=" + res.Branch, + "via=sandbox", + "agent=" + name, + } + if backend != "" { + pairs = append(pairs, "backend="+backend) + } + if model != "" { + pairs = append(pairs, "model="+model) + } + m.auditSandbox(name, crux.AuditActionAgentPRCreated, strings.Join(pairs, ", ")) } func (m *Manager) auditSandbox(agent, action, detail string) { diff --git a/src/crux/agent/sandbox_executor.go b/src/crux/agent/sandbox_executor.go index 69d218e3d1..73298349bf 100644 --- a/src/crux/agent/sandbox_executor.go +++ b/src/crux/agent/sandbox_executor.go @@ -90,6 +90,20 @@ type SandboxKickSpec struct { // Non-empty makes the kick review-only: workspace prep checks each PR // head out on pr- and writes .hive/pr/.json, and nothing pushes. ReviewPRs []int + // TargetPR is an existing PR (in Org/Repo) whose head branch the kick + // checks out and adds its commits to instead of opening a new PR + // (hivecommons/hive#18574). Zero opens a new PR as before. The manager + // resolves it from the kick message (sandboxTargetPRFromKickMessage); + // the executor refuses any PR this agent may not push to. + TargetPR int + // TargetPRSource says where the kick named TargetPR, for the prompt and + // the run record. + TargetPRSource string + + // targetHead and targetURL are set by workspace prep for a TargetPR + // kick: the head commit checked out (the push's lease) and the PR URL. + targetHead string + targetURL string } // reviewOnly reports whether the kick reviews PRs and so must never push. @@ -125,8 +139,15 @@ type SandboxKickResult struct { Sandbox sandbox.Result Broker *pushbroker.Result PR *crux.PRResult - TimedOut bool - Error string + // PRAttributed is true when the PR client credited a newly opened PR to + // the kick's agent itself (crux.AgentPRCreator). When it is false and a + // PR was opened, the manager records the credit through the sandbox audit + // callback instead (hivecommons/hive#18575). + PRAttributed bool + // TargetPR is the existing PR the run's commits were pushed to, or 0. + TargetPR int + TimedOut bool + Error string } // SandboxVerifyResult is one publish.verify command's outcome. @@ -159,9 +180,13 @@ type SandboxExecutor struct { // PRReader reads a review target's metadata, diff and comments for the // PR file a review kick gets. Nil still checks the PR head out. PRReader crux.PRReviewContextReader - Logger *slog.Logger - Now func() time.Time - Mutation effects.Boundary + // PRTargetReader reads an existing PR a kick pushes to (spec.TargetPR): + // before the checkout and again before the push. Nil refuses every + // TargetPR kick. + PRTargetReader crux.PRPushTargetReader + Logger *slog.Logger + Now func() time.Time + Mutation effects.Boundary } func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (SandboxKickResult, error) { @@ -331,6 +356,16 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo res.Error = err.Error() return res, err } + if spec.TargetPR > 0 { + // The PR may have closed, changed hands or moved while the sandbox + // ran: ask again, and push nothing unless it is still this agent's + // open PR at the head the kick checked out. + if err := e.recheckTargetPR(ctx, spec); err != nil { + res.PRWithheld = err.Error() + res.Error = err.Error() + return res, err + } + } broker := &pushbroker.Broker{ Workspace: workspace, Branch: spec.Branch, @@ -342,6 +377,7 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo Runner: e.runner(), Logger: e.Logger, Mutation: e.Mutation, + LeaseSHA: spec.targetHead, } bres, brokerErr := broker.Run(ctx) res.Broker = &bres @@ -349,9 +385,23 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo res.Error = brokerErr.Error() return res, brokerErr } + if spec.TargetPR > 0 { + // The commits landed on the PR's own branch: there is no PR to + // open, and the PR's credit was recorded when it was opened. + res.TargetPR = spec.TargetPR + res.PR = &crux.PRResult{Number: spec.TargetPR, URL: spec.targetURL, AlreadyExisted: true} + return res, nil + } if e.PRClient != nil && bres.Pushed { title, body := e.sandboxPRText(ctx, workspace, spec.Agent) - pr, prErr := e.PRClient.CreatePR(ctx, spec.Org+"/"+spec.Repo, spec.Branch, spec.BaseRef, title, body) + var pr crux.PRResult + var prErr error + if creator, ok := e.PRClient.(crux.AgentPRCreator); ok { + pr, prErr = creator.CreateAgentPR(ctx, spec.Agent, spec.Org+"/"+spec.Repo, spec.Branch, spec.BaseRef, title, body) + res.PRAttributed = prErr == nil + } else { + pr, prErr = e.PRClient.CreatePR(ctx, spec.Org+"/"+spec.Repo, spec.Branch, spec.BaseRef, title, body) + } res.PR = &pr if prErr != nil { res.Error = prErr.Error() @@ -691,7 +741,7 @@ func (e *SandboxExecutor) now() time.Time { } func (e *SandboxExecutor) branchName(agentName string) string { - return "hive/" + sanitizeBranchPart(agentName) + "/" + e.now().Format("20060102150405") + return sandbox.WorkBranch(agentName, e.now().Format(sandbox.WorkBranchTimeLayout)) } func sanitizeBranchPart(s string) string { return sandbox.AgentSlug(s) } diff --git a/src/crux/agent/sandbox_target_pr.go b/src/crux/agent/sandbox_target_pr.go new file mode 100644 index 0000000000..3605aff494 --- /dev/null +++ b/src/crux/agent/sandbox_target_pr.go @@ -0,0 +1,334 @@ +package agent + +import ( + "context" + "errors" + "fmt" + "os" + "regexp" + "strconv" + "strings" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/crux/pushbroker" +) + +// A sandboxed fix kick can add commits to an existing PR +// (hivecommons/hive#18574). +// +// Without this, a sandboxed kick could only open a NEW PR: a kick that fixed +// the repo side of a red PR came back as a replacement PR, or as a patch in +// the report. Now a kick that names one of the agent's own open PRs gets that +// PR's head branch checked out instead of the base, and after the run the +// push broker adds the kick's commits to it. Every gate fails the kick with +// the reason and pushes nothing: +// +// - the PR is open, its head branch lives in the kick's repository (not a +// fork), and the host's PR-authorship record credits it to this agent +// (the agent_pr_created entry, hivecommons/hive#18575); +// - the head is still the commit hive checked out, both when GitHub is +// asked again before the push and in the push itself +// (--force-with-lease); +// - the broker's secret, protected-path, sign-off and empty-commit scans +// pass on the kick's own commits. + +// ErrSandboxTargetPRRefused wraps every refusal to work on, or push to, a +// kick's target PR. +var ErrSandboxTargetPRRefused = errors.New("sandbox target PR refused") + +// Target sources, in the order sandboxTargetPRFromKickMessage tries them. +const ( + sandboxTargetSourceExplicit = "explicit" + sandboxTargetSourceRedPRKick = "red-pr-kick" + sandboxTargetSourceCIFailure = "ci-failure-kick" + sandboxTargetSourceFixFirst = "fix-before-new" + sandboxTargetSourceTopicClaim = "topic-claim" + sandboxTargetPromptMarker = "[hive:sandbox-target-pr]" + sandboxTargetAgentHeaderPrefix = "[agent:" +) + +// The shapes below are hive-written lines of the kick message. They mirror +// what the hive renders (cmd/hive redPRFixKick, pkg/scheduler +// CIFailureKickMessage, the FIX-BEFORE-NEW block and the topic-claims +// block); crux cannot import those packages, so the formats are restated +// here and pinned by tests. A line a PR author could plant in quoted text +// gains nothing: the target is only a candidate, and the executor still +// refuses any PR this agent did not open. +var ( + // The explicit target an operator or planner puts on a kick's first line. + sandboxTargetExplicitRE = regexp.MustCompile(`^\[hive:target-pr ([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)#([0-9]+)\]$`) + sandboxRedPRKickRE = regexp.MustCompile(`^šŸ”“ FIX-BEFORE-NEW — ([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)#([0-9]+) is red and has not moved$`) + sandboxCIFailureKickRE = regexp.MustCompile(`^CI failed on ([A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)?)#([0-9]+) \(`) + sandboxFixFirstRowRE = regexp.MustCompile(`^ #([0-9]+) ([A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)?)(?: \[topic: [^\]]*\])? — `) + sandboxTopicClaimRowRE = regexp.MustCompile(`^ [^ ]+ → ([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)#([0-9]+)(?: \(branch [^)]*\))?$`) +) + +const ( + sandboxFixFirstHeaderPrefix = "## šŸ”“ FIX-BEFORE-NEW — your open PRs with failing CI or merge conflicts" + sandboxTopicClaimsHeader = "## šŸ“Œ Topic claims (one PR per topic)" + sandboxAdoptedRowPrefix = " ADOPTED contributor PR" + sandboxSectionPrefix = "## " +) + +// sandboxTargetPRFromKickMessage returns the PR in repo (owner/name) a kick +// should add its commits to, and where the kick named it, or 0 when it names +// none. In order: an explicit "[hive:target-pr #]" first line; the +// targeted red-PR kick's first line; the CI-failure kick's first line; the +// first row of the FIX-BEFORE-NEW block that is not an adopted contributor +// PR; the agent's topic claim when it holds exactly one in repo. A first +// line of "[agent:]" is skipped. A PR in another repository is never a +// target: the workspace clones repo only. +func sandboxTargetPRFromKickMessage(message, repo string) (int, string) { + lines := strings.Split(strings.TrimLeft(message, " \t\r\n"), "\n") + for i := range lines { + lines[i] = strings.TrimRight(lines[i], "\r") + } + first := 0 + if strings.HasPrefix(lines[0], sandboxTargetAgentHeaderPrefix) && len(lines) > 1 { + first = 1 + } + head := lines[first] + for _, h := range []struct { + re *regexp.Regexp + source string + }{ + {sandboxTargetExplicitRE, sandboxTargetSourceExplicit}, + {sandboxRedPRKickRE, sandboxTargetSourceRedPRKick}, + {sandboxCIFailureKickRE, sandboxTargetSourceCIFailure}, + } { + if m := h.re.FindStringSubmatch(head); m != nil { + if n := sandboxTargetNumber(m[1], m[2], repo); n > 0 { + return n, h.source + } + } + } + if n := sandboxFixFirstTarget(lines, repo); n > 0 { + return n, sandboxTargetSourceFixFirst + } + if n := sandboxTopicClaimTarget(lines, repo); n > 0 { + return n, sandboxTargetSourceTopicClaim + } + return 0, "" +} + +func sandboxTargetNumber(targetRepo, num, repo string) int { + n, err := strconv.Atoi(num) + if err != nil || n <= 0 || !sandboxSameRepo(targetRepo, repo) { + return 0 + } + return n +} + +// sandboxSameRepo matches owner/name whole when both carry an owner, else by +// name (the FIX-BEFORE-NEW block prints whatever governor.repos says). +func sandboxSameRepo(a, b string) bool { + a, b = strings.ToLower(strings.TrimSpace(a)), strings.ToLower(strings.TrimSpace(b)) + if a == "" || b == "" { + return false + } + if strings.Contains(a, "/") && strings.Contains(b, "/") { + return a == b + } + return a[strings.LastIndex(a, "/")+1:] == b[strings.LastIndex(b, "/")+1:] +} + +// sectionLines returns the lines after the header line that starts with +// prefix, up to the next "## " header. +func sectionLines(lines []string, prefix string) []string { + for i, l := range lines { + if !strings.HasPrefix(l, prefix) { + continue + } + end := len(lines) + for j := i + 1; j < len(lines); j++ { + if strings.HasPrefix(lines[j], sandboxSectionPrefix) { + end = j + break + } + } + return lines[i+1 : end] + } + return nil +} + +func sandboxFixFirstTarget(lines []string, repo string) int { + rows := sectionLines(lines, sandboxFixFirstHeaderPrefix) + for i, l := range rows { + m := sandboxFixFirstRowRE.FindStringSubmatch(l) + if m == nil { + continue + } + // An adopted contributor PR is pushed through the contributor's + // own branch, which a sandbox push never targets. + if i+1 < len(rows) && strings.HasPrefix(rows[i+1], sandboxAdoptedRowPrefix) { + continue + } + if n := sandboxTargetNumber(m[2], m[1], repo); n > 0 { + return n + } + } + return 0 +} + +func sandboxTopicClaimTarget(lines []string, repo string) int { + found := 0 + for _, l := range sectionLines(lines, sandboxTopicClaimsHeader) { + m := sandboxTopicClaimRowRE.FindStringSubmatch(l) + if m == nil { + continue + } + n := sandboxTargetNumber(m[1], m[2], repo) + if n == 0 { + continue + } + if found != 0 && found != n { + // Several topics: the kick does not say which one this + // run is for, so it opens a new PR as before. + return 0 + } + found = n + } + return found +} + +// sandboxTargetRefusal returns why the kick may not work on t for agent in +// repo, or "" when it may. +func sandboxTargetRefusal(t crux.PRPushTarget, repo, agent string, want int) string { + switch { + case t.Number != 0 && t.Number != want: + return fmt.Sprintf("GitHub returned PR #%d when asked for %s#%d", t.Number, repo, want) + case !strings.EqualFold(strings.TrimSpace(t.State), "open"): + state := strings.TrimSpace(t.State) + if state == "" { + state = "in an unknown state" + } + return fmt.Sprintf("%s#%d is %s, not open", repo, want, state) + case t.Fork || !strings.EqualFold(strings.TrimSpace(t.HeadRepo), repo): + where := strings.TrimSpace(t.HeadRepo) + if where == "" { + where = "an unknown repository" + } + return fmt.Sprintf("%s#%d is a fork PR (head branch in %s); hive pushes only to branches in %s", repo, want, where, repo) + case strings.TrimSpace(t.AuthorAgent) == "": + return fmt.Sprintf("%s#%d was not opened by this hive for an agent (no agent_pr_created record credits it)", repo, want) + case !strings.EqualFold(strings.TrimSpace(t.AuthorAgent), strings.TrimSpace(agent)): + return fmt.Sprintf("%s#%d was opened by agent %s, not %s; an agent pushes only to its own PRs", repo, want, strings.TrimSpace(t.AuthorAgent), agent) + case !validSandboxTargetBranch(t.HeadRef): + return fmt.Sprintf("%s#%d has head branch %q, which hive will not check out", repo, want, t.HeadRef) + } + return "" +} + +// validSandboxTargetBranch keeps the head ref to a plain branch name before it +// reaches git's argv: no option-looking, relative or ref-syntax names. +func validSandboxTargetBranch(ref string) bool { + if ref == "" || ref != strings.TrimSpace(ref) || strings.HasPrefix(ref, "-") || strings.HasPrefix(ref, "/") || + strings.HasSuffix(ref, "/") || strings.HasSuffix(ref, ".lock") || strings.HasSuffix(ref, ".") || + strings.Contains(ref, "..") || strings.Contains(ref, "//") || strings.Contains(ref, "@{") { + return false + } + for _, r := range ref { + if r <= ' ' || r == 0x7f || strings.ContainsRune(`~^:?*[\`, r) { + return false + } + } + return true +} + +func sandboxTargetRefused(format string, args ...any) error { + return fmt.Errorf("%w: %s", ErrSandboxTargetPRRefused, fmt.Sprintf(format, args...)) +} + +// prepareTargetPRCheckout checks out the head branch of spec.TargetPR and +// returns the head commit, which is both the base the run's own commits are +// counted from and the lease the push must find. It reads the PR first and +// refuses one this kick may not push to, before the sandbox spends a run on +// it. +func (e *SandboxExecutor) prepareTargetPRCheckout(ctx context.Context, spec *SandboxKickSpec, workspace string, authArgs []string) (string, error) { + repo := spec.Org + "/" + spec.Repo + n := spec.TargetPR + if e.PRTargetReader == nil { + return "", sandboxTargetRefused("the hive's PR client cannot read %s#%d, so the kick cannot check that it may push there; nothing was run", repo, n) + } + t, err := e.PRTargetReader.PRPushTarget(ctx, repo, n) + if err != nil { + return "", sandboxTargetRefused("reading %s#%d: %v", repo, n, err) + } + if reason := sandboxTargetRefusal(t, repo, spec.Agent, n); reason != "" { + return "", sandboxTargetRefused("%s", reason) + } + branch := t.HeadRef + tracking := "refs/remotes/origin/" + branch + fetchArgs := append(append([]string{}, authArgs...), "fetch", "origin", "+refs/heads/"+branch+":"+tracking) + if out, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", fetchArgs...); err != nil { + return "", fmt.Errorf("sandbox target PR: could not fetch %s for %s#%d with the clone token: %w: %s", branch, repo, n, err, strings.TrimSpace(string(out))) + } + shaOut, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", "rev-parse", tracking) + if err != nil { + return "", fmt.Errorf("sandbox target PR: git rev-parse %s: %w", tracking, err) + } + sha := strings.TrimSpace(string(shaOut)) + if t.HeadSHA != "" && sha != t.HeadSHA { + return "", sandboxTargetRefused("%s#%d moved while hive checked it out (GitHub reported head %s, the fetch found %s)", repo, n, t.HeadSHA, sha) + } + if out, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", "checkout", "-B", branch, tracking); err != nil { + return "", fmt.Errorf("sandbox target PR: git checkout %s: %w: %s", branch, err, strings.TrimSpace(string(out))) + } + spec.Branch = branch + if base := strings.TrimSpace(t.BaseRef); base != "" { + spec.BaseRef = base + } + spec.targetHead = sha + spec.targetURL = t.URL + spec.Message = sandboxTargetPromptSection(repo, n, branch, sha, spec.TargetPRSource) + spec.Message + return sha, nil +} + +// recheckTargetPR asks GitHub again, just before the push, whether the kick +// may still push to its target and whether the head is still the one it +// checked out. +func (e *SandboxExecutor) recheckTargetPR(ctx context.Context, spec SandboxKickSpec) error { + repo := spec.Org + "/" + spec.Repo + n := spec.TargetPR + if e.PRTargetReader == nil { + return sandboxTargetRefused("the hive's PR client cannot read %s#%d before the push; nothing pushed", repo, n) + } + t, err := e.PRTargetReader.PRPushTarget(ctx, repo, n) + if err != nil { + return sandboxTargetRefused("re-reading %s#%d before the push: %v; nothing pushed", repo, n, err) + } + if reason := sandboxTargetRefusal(t, repo, spec.Agent, n); reason != "" { + return sandboxTargetRefused("%s; nothing pushed", reason) + } + if t.HeadRef != spec.Branch { + return sandboxTargetRefused("%s#%d's head branch changed from %s to %s during the run; nothing pushed", repo, n, spec.Branch, t.HeadRef) + } + if t.HeadSHA != spec.targetHead { + return sandboxTargetRefused("%s#%d's head moved since checkout (%s, now %s): someone else pushed to it during the run; nothing pushed, and hive never forces over another push", repo, n, shortCommit(spec.targetHead), shortCommit(t.HeadSHA)) + } + return nil +} + +func shortCommit(sha string) string { + const shortLen = 12 + if len(sha) > shortLen { + return sha[:shortLen] + } + return sha +} + +// sandboxTargetPromptSection is the hive-written block prepended to a kick +// whose commits go to an existing PR. +func sandboxTargetPromptSection(repo string, n int, branch, sha, source string) string { + var b strings.Builder + b.WriteString(sandboxTargetPromptMarker + "\n") + fmt.Fprintf(&b, "SANDBOX WORKSPACE ON AN EXISTING PR. Hive checked out %s#%d for you: branch %s at %s", repo, n, branch, sha) + if source != "" { + fmt.Fprintf(&b, " (named by this kick's %s)", source) + } + b.WriteString(".\n") + b.WriteString("This sandbox holds no GitHub credential, so `gh pr checkout` and `git push` fail here, and you do not need them. Commit your fix on top of this branch with git commit -s. When the run ends, hive pushes your new commits to the PR.\n") + b.WriteString("Do not rebase, amend or reset the commits already on the branch: hive pushes only commits that build on the checked-out head, and refuses the push if anyone else pushed in the meantime. Do not open a new PR, and do not write .hive/pr-title.txt or .hive/pr-body.md for this kick.\n") + b.WriteString("---\n\n") + return b.String() +} diff --git a/src/crux/agent/sandbox_target_pr_test.go b/src/crux/agent/sandbox_target_pr_test.go new file mode 100644 index 0000000000..c78b7167e1 --- /dev/null +++ b/src/crux/agent/sandbox_target_pr_test.go @@ -0,0 +1,575 @@ +package agent + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "sync" + "testing" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/crux/config" +) + +const ( + targetHeadSHA = "1111111111111111111111111111111111111111" + targetMovedSHA = "2222222222222222222222222222222222222222" + targetNewSHA = "3333333333333333333333333333333333333333" + targetBranch = "hive/dep-bump/20261116120000" + targetAgent = "dep-bump" + targetPRNumber = 7 + targetRepo = "o/r" +) + +// targetRunner is a fake git for kicks on an existing PR. The PR head is +// fetched at targetHeadSHA (or fetchedHead), and after the run HEAD is the +// kick's own commit, targetNewSHA. +type targetRunner struct { + mu sync.Mutex + fetches []string + checkouts []string + pushArgs []string + diff string + fetchedHead string +} + +func (r *targetRunner) Run(_ context.Context, _ string, _ []string, name string, args ...string) ([]byte, error) { + if name != "git" { + return nil, errors.New("unexpected command") + } + effective := stripGitConfigArgs(args) + joined := strings.Join(effective, " ") + r.mu.Lock() + defer r.mu.Unlock() + switch { + case len(effective) >= 4 && effective[0] == "clone": + return nil, os.MkdirAll(filepath.Join(effective[len(effective)-1], ".git"), 0o770) + case joined == "symbolic-ref --short refs/remotes/origin/HEAD": + return []byte("origin/main\n"), nil + case strings.HasPrefix(joined, "fetch origin "): + r.fetches = append(r.fetches, strings.TrimPrefix(joined, "fetch origin ")) + return nil, nil + case strings.HasPrefix(joined, "rev-parse refs/remotes/origin/"): + head := r.fetchedHead + if head == "" { + head = targetHeadSHA + } + return []byte(head + "\n"), nil + case strings.HasPrefix(joined, "checkout "): + r.checkouts = append(r.checkouts, strings.TrimPrefix(joined, "checkout ")) + return nil, nil + case joined == "rev-parse HEAD": + return []byte(targetNewSHA + "\n"), nil + case strings.HasPrefix(joined, "rev-list --count "): + return []byte("1\n"), nil + case strings.HasPrefix(joined, "diff --no-renames --name-only -z "): + return []byte("fix.go\x00"), nil + case strings.HasPrefix(joined, "diff --text "): + return []byte(r.diff), nil + case len(effective) > 0 && effective[0] == "push": + r.pushArgs = append([]string(nil), effective...) + return []byte("pushed\n"), nil + default: + return nil, nil + } +} + +func (r *targetRunner) pushed() bool { + r.mu.Lock() + defer r.mu.Unlock() + return r.pushArgs != nil +} + +// fakeTargetReader answers PRPushTarget reads in order; the last answer +// repeats. +type fakeTargetReader struct { + mu sync.Mutex + answers []crux.PRPushTarget + err error + reads int +} + +func (f *fakeTargetReader) PRPushTarget(_ context.Context, repo string, n int) (crux.PRPushTarget, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.reads++ + if f.err != nil { + return crux.PRPushTarget{}, f.err + } + if repo != targetRepo || n != targetPRNumber { + return crux.PRPushTarget{}, errors.New("unexpected target " + repo) + } + i := f.reads - 1 + if i >= len(f.answers) { + i = len(f.answers) - 1 + } + return f.answers[i], nil +} + +func ownTargetPR() crux.PRPushTarget { + return crux.PRPushTarget{ + Number: targetPRNumber, URL: "https://github.com/o/r/pull/7", State: "open", + BaseRef: "main", HeadRef: targetBranch, HeadSHA: targetHeadSHA, HeadRepo: targetRepo, + Author: "hive-app[bot]", AuthorAgent: targetAgent, + } +} + +// countingPR is a PR client that is NOT an AgentPRCreator. +type countingPR struct { + mu sync.Mutex + calls int +} + +func (p *countingPR) CreatePR(context.Context, string, string, string, string, string) (crux.PRResult, error) { + p.mu.Lock() + defer p.mu.Unlock() + p.calls++ + return crux.PRResult{Number: 42, URL: "https://github.com/o/r/pull/42", Author: "hive-app[bot]"}, nil +} + +func (p *countingPR) count() int { + p.mu.Lock() + defer p.mu.Unlock() + return p.calls +} + +func runTargetKick(t *testing.T, runner *targetRunner, reader crux.PRPushTargetReader) (SandboxKickResult, *countingPR, error) { + t.Helper() + pr := &countingPR{} + exec := &SandboxExecutor{ + Runner: runner, + Launcher: sandboxFakeLauncher{}, + Minter: sandboxFakeMinter{}, + PushEnabled: true, + PRClient: pr, + Logger: discardLogger(), + } + if reader != nil { + exec.PRTargetReader = reader + } + res, err := exec.Run(context.Background(), SandboxKickSpec{ + Agent: targetAgent, AgentConfig: configSnapshot{Backend: "claude"}, Message: "fix the red PR", + Org: "o", Repo: "r", WorkspaceDir: t.TempDir(), Image: "img", + TargetPR: targetPRNumber, TargetPRSource: sandboxTargetSourceFixFirst, + }) + return res, pr, err +} + +func TestSandboxTargetPRPushesToTheExistingBranch(t *testing.T) { + runner := &targetRunner{} + reader := &fakeTargetReader{answers: []crux.PRPushTarget{ownTargetPR()}} + res, pr, err := runTargetKick(t, runner, reader) + if err != nil { + t.Fatalf("Run: %v (res=%+v)", err, res) + } + if !runner.pushed() { + t.Fatal("the kick's commits were not pushed") + } + joined := strings.Join(runner.pushArgs, " ") + for _, want := range []string{"--force-with-lease=refs/heads/" + targetBranch + ":" + targetHeadSHA, "HEAD:refs/heads/" + targetBranch} { + if !strings.Contains(joined, want) { + t.Fatalf("push %q lacks %q", joined, want) + } + } + if pr.calls != 0 { + t.Fatalf("a push to an existing PR opened %d new PR(s)", pr.calls) + } + if res.TargetPR != targetPRNumber || res.PR == nil || res.PR.Number != targetPRNumber || !res.PR.AlreadyExisted || res.PR.URL != "https://github.com/o/r/pull/7" { + t.Fatalf("result does not name the target PR: %+v pr=%+v", res, res.PR) + } + if res.Branch != targetBranch || res.BaseSHA != targetHeadSHA || res.CommitCount != 1 { + t.Fatalf("branch=%s base=%s commits=%d", res.Branch, res.BaseSHA, res.CommitCount) + } + if res.Broker == nil || res.Broker.Lease != targetHeadSHA { + t.Fatalf("broker result lacks the lease: %+v", res.Broker) + } + if reader.reads != 2 { + t.Fatalf("target read %d times, want before checkout and before push", reader.reads) + } + wantFetch := "+refs/heads/" + targetBranch + ":refs/remotes/origin/" + targetBranch + found := false + for _, f := range runner.fetches { + found = found || f == wantFetch + } + if !found { + t.Fatalf("PR head not fetched: %v", runner.fetches) + } + if len(runner.checkouts) != 1 || runner.checkouts[0] != "-B "+targetBranch+" refs/remotes/origin/"+targetBranch { + t.Fatalf("checkouts = %v", runner.checkouts) + } + prompt, err := os.ReadFile(filepath.Join(res.Workspace, sandboxPromptRelPath)) + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(string(prompt), sandboxTargetPromptMarker) || !strings.Contains(string(prompt), "o/r#7") || !strings.HasSuffix(string(prompt), "fix the red PR") { + t.Fatalf("prompt = %q", prompt) + } +} + +func TestSandboxTargetPRRefusals(t *testing.T) { + fork := ownTargetPR() + fork.Fork, fork.HeadRepo = true, "forker/r" + other := ownTargetPR() + other.AuthorAgent = "scanner" + unowned := ownTargetPR() + unowned.AuthorAgent = "" + closed := ownTargetPR() + closed.State = "closed" + badRef := ownTargetPR() + badRef.HeadRef = "--upload-pack=evil" + wrongNumber := ownTargetPR() + wrongNumber.Number = 8 + moved := ownTargetPR() + moved.HeadSHA = targetMovedSHA + renamed := ownTargetPR() + renamed.HeadRef = "other-branch" + closedLater := ownTargetPR() + closedLater.State = "closed" + + cases := []struct { + name string + answers []crux.PRPushTarget + readErr error + runner *targetRunner + noRead bool + want string + // prep refusals stop the kick before the sandbox runs, so there + // are no commits; push-time refusals withhold the run's commits. + atPush bool + }{ + {name: "fork", answers: []crux.PRPushTarget{fork}, want: "is a fork PR"}, + {name: "other agent", answers: []crux.PRPushTarget{other}, want: "opened by agent scanner, not dep-bump"}, + {name: "not hive-opened", answers: []crux.PRPushTarget{unowned}, want: "not opened by this hive"}, + {name: "closed", answers: []crux.PRPushTarget{closed}, want: "is closed, not open"}, + {name: "bad head ref", answers: []crux.PRPushTarget{badRef}, want: "will not check out"}, + {name: "wrong number", answers: []crux.PRPushTarget{wrongNumber}, want: "GitHub returned PR #8"}, + {name: "read error", readErr: errors.New("boom"), want: "reading o/r#7: boom"}, + {name: "no reader", noRead: true, want: "cannot read o/r#7"}, + {name: "fetch race", answers: []crux.PRPushTarget{ownTargetPR()}, runner: &targetRunner{fetchedHead: targetMovedSHA}, want: "moved while hive checked it out"}, + {name: "moved head", answers: []crux.PRPushTarget{ownTargetPR(), moved}, want: "head moved since checkout", atPush: true}, + {name: "renamed head", answers: []crux.PRPushTarget{ownTargetPR(), renamed}, want: "head branch changed", atPush: true}, + {name: "closed during run", answers: []crux.PRPushTarget{ownTargetPR(), closedLater}, want: "is closed, not open; nothing pushed", atPush: true}, + {name: "unreadable before push", answers: []crux.PRPushTarget{ownTargetPR()}, want: "re-reading o/r#7", atPush: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + runner := tc.runner + if runner == nil { + runner = &targetRunner{} + } + var reader crux.PRPushTargetReader + if !tc.noRead { + fr := &fakeTargetReader{answers: tc.answers, err: tc.readErr} + if tc.name == "unreadable before push" { + reader = &failSecondRead{fakeTargetReader: fr} + } else { + reader = fr + } + } + res, pr, err := runTargetKick(t, runner, reader) + if !errors.Is(err, ErrSandboxTargetPRRefused) { + t.Fatalf("err = %v, want ErrSandboxTargetPRRefused", err) + } + if !strings.Contains(err.Error(), tc.want) { + t.Fatalf("err = %q, want it to say %q", err, tc.want) + } + if runner.pushed() || pr.calls != 0 { + t.Fatalf("refused kick pushed=%v opened=%d", runner.pushed(), pr.calls) + } + if tc.atPush && (res.CommitCount != 1 || res.PRWithheld == "") { + t.Fatalf("push-time refusal did not withhold the commits: %+v", res) + } + if !tc.atPush && len(runner.checkouts) != 0 && tc.name != "fetch race" { + t.Fatalf("refused target was checked out: %v", runner.checkouts) + } + }) + } +} + +// failSecondRead fails every read after the first. +type failSecondRead struct{ *fakeTargetReader } + +func (f *failSecondRead) PRPushTarget(ctx context.Context, repo string, n int) (crux.PRPushTarget, error) { + if f.reads >= 1 { + f.reads++ + return crux.PRPushTarget{}, errors.New("rate limited") + } + return f.fakeTargetReader.PRPushTarget(ctx, repo, n) +} + +func TestSandboxTargetPRScanFailurePushesNothing(t *testing.T) { + runner := &targetRunner{diff: "diff --git a/fix.go b/fix.go\n+token=ghp_abcdefghijklmnopqrstuvwxyz\n"} + reader := &fakeTargetReader{answers: []crux.PRPushTarget{ownTargetPR()}} + res, pr, err := runTargetKick(t, runner, reader) + if err == nil { + t.Fatal("a secret in the outgoing diff must fail the kick") + } + if res.Broker == nil || !res.Broker.SecretRejected || res.Broker.Pushed { + t.Fatalf("broker result = %+v", res.Broker) + } + if runner.pushed() || pr.calls != 0 { + t.Fatalf("scan failure pushed=%v opened=%d", runner.pushed(), pr.calls) + } +} + +func TestSandboxTargetPRRunStageRefused(t *testing.T) { + exec := &SandboxExecutor{Runner: &targetRunner{}, Launcher: sandboxFakeLauncher{}} + _, err := exec.Run(context.Background(), SandboxKickSpec{ + Agent: targetAgent, Org: "o", Repo: "r", WorkspaceDir: t.TempDir(), Image: "img", + Stage: "build", RunKey: "k", Generation: 1, TargetPR: targetPRNumber, + }) + if err == nil || !strings.Contains(err.Error(), "cannot push to an existing PR") { + t.Fatalf("err = %v", err) + } +} + +func TestSandboxTargetPRFromKickMessage(t *testing.T) { + fixFirst := "## šŸ”“ FIX-BEFORE-NEW — your open PRs with failing CI or merge conflicts (2)\n\n" + + "These PRs are YOURS and they are red or conflicted.\n" + + " gh pr checkout → fix using the evidence below → commit -s → git push\n\n" + + " #12 o/r — Contributor change\n" + + " ADOPTED contributor PR (stage 074, head repo c/r): the author is offline\n" + + " #7 r [topic: torch] — Bump torch\n" + + " failing: build\n\n" + topics := "## šŸ“Œ Topic claims (one PR per topic)\n\n" + + "You own these topics.\n" + + " torch → o/r#9 (branch hive/dep-bump/20261101000000)\n" + + "Claim a new topic right after opening its PR.\n" + cases := []struct { + name, msg string + wantN int + wantSource string + }{ + {"explicit", "[hive:target-pr o/r#5]\nfix it\n" + fixFirst, 5, sandboxTargetSourceExplicit}, + {"explicit after agent header", "[agent:dep-bump]\n[hive:target-pr o/r#5]\nfix it", 5, sandboxTargetSourceExplicit}, + {"explicit other repo falls through", "[hive:target-pr x/y#5]\n" + fixFirst, 7, sandboxTargetSourceFixFirst}, + {"red PR kick", "šŸ”“ FIX-BEFORE-NEW — o/r#11 is red and has not moved\n\nRepair it", 11, sandboxTargetSourceRedPRKick}, + {"CI failure kick", "CI failed on o/r#13 (build) — repair it now. Detection was out-of-band\n" + fixFirst, 13, sandboxTargetSourceCIFailure}, + {"fix-before-new skips adopted", "Kick.\n\n" + fixFirst, 7, sandboxTargetSourceFixFirst}, + {"fix-before-new beats topic", topics + "\n" + fixFirst, 7, sandboxTargetSourceFixFirst}, + {"single topic claim", "Kick.\n\n" + topics, 9, sandboxTargetSourceTopicClaim}, + {"two topic claims", "Kick.\n\n" + topics + " numpy → o/r#10\n", 0, ""}, + {"same topic PR twice", "Kick.\n\n" + topics + " torch2 → o/r#9\n", 9, sandboxTargetSourceTopicClaim}, + {"topic in another repo", "Kick.\n\n## šŸ“Œ Topic claims (one PR per topic)\n torch → x/y#9\n", 0, ""}, + {"quoted header mid-message is ignored", "Kick.\nšŸ”“ FIX-BEFORE-NEW — o/r#11 is red and has not moved\n", 0, ""}, + {"row outside its section is ignored", "Kick.\n #7 o/r — Bump torch\n", 0, ""}, + {"plain kick", "Do your cadence work.", 0, ""}, + {"empty", "", 0, ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + n, source := sandboxTargetPRFromKickMessage(tc.msg, "o/r") + if n != tc.wantN || source != tc.wantSource { + t.Fatalf("got (%d, %q), want (%d, %q)", n, source, tc.wantN, tc.wantSource) + } + }) + } +} + +func TestValidSandboxTargetBranch(t *testing.T) { + for _, ok := range []string{"main", "hive/dep-bump/20261116120000", "feature/x-1.2"} { + if !validSandboxTargetBranch(ok) { + t.Errorf("%q should be valid", ok) + } + } + for _, bad := range []string{"", " x", "-x", "/x", "x/", "x.lock", "x.", "a..b", "a//b", "a@{1}", "a b", "a~1", "a^", "a:b", "a?", "a*", "a[", `a\b`, "a\x7f"} { + if validSandboxTargetBranch(bad) { + t.Errorf("%q should be refused", bad) + } + } +} + +func TestSandboxTargetRefusalUnknownStateAndRepo(t *testing.T) { + t1 := ownTargetPR() + t1.State = "" + if r := sandboxTargetRefusal(t1, targetRepo, targetAgent, targetPRNumber); !strings.Contains(r, "in an unknown state") { + t.Fatalf("refusal = %q", r) + } + t2 := ownTargetPR() + t2.HeadRepo = "" + if r := sandboxTargetRefusal(t2, targetRepo, targetAgent, targetPRNumber); !strings.Contains(r, "an unknown repository") { + t.Fatalf("refusal = %q", r) + } + if r := sandboxTargetRefusal(ownTargetPR(), targetRepo, "DEP-BUMP", targetPRNumber); r != "" { + t.Fatalf("agent names compare case-insensitively: %q", r) + } + if got := shortCommit("abc"); got != "abc" { + t.Fatalf("shortCommit = %q", got) + } +} + +// agentPRClient is a PR client that credits the PRs it opens itself. +type agentPRClient struct { + countingPR + agent string +} + +func (p *agentPRClient) CreateAgentPR(_ context.Context, agent, _, _, _, _, _ string) (crux.PRResult, error) { + p.mu.Lock() + defer p.mu.Unlock() + p.agent = agent + return crux.PRResult{Number: 42, URL: "https://github.com/o/r/pull/42"}, nil +} + +func TestSandboxExecutorCreditsNewPRThroughAgentPRCreator(t *testing.T) { + runner := &sandboxFakeRunner{commits: 1} + pr := &agentPRClient{} + exec := &SandboxExecutor{Runner: runner, Launcher: sandboxFakeLauncher{}, Minter: sandboxFakeMinter{}, PushEnabled: true, PRClient: pr} + res, err := exec.Run(context.Background(), SandboxKickSpec{ + Agent: targetAgent, AgentConfig: configSnapshot{Backend: "claude"}, Message: "fix", + Org: "o", Repo: "r", WorkspaceDir: t.TempDir(), Image: "img", + }) + if err != nil { + t.Fatalf("Run: %v", err) + } + if pr.agent != targetAgent || pr.calls != 0 || !res.PRAttributed || res.PR == nil || res.PR.Number != 42 { + t.Fatalf("agent=%q CreatePR calls=%d attributed=%v pr=%+v", pr.agent, pr.calls, res.PRAttributed, res.PR) + } + if !strings.HasPrefix(res.Branch, "hive/dep-bump/") { + t.Fatalf("work branch = %q", res.Branch) + } +} + +type sandboxAuditLine struct{ agent, action, detail string } + +func newSandboxTargetManager(t *testing.T, client PRCreator, runner sandboxCommandRunner) (*Manager, *[]sandboxAuditLine, *sync.Mutex) { + t.Helper() + yes := true + cfg := map[string]config.AgentConfig{ + targetAgent: {Backend: "claude", Model: "sonnet", Sandbox: &config.AgentSandboxOverride{Enabled: &yes}}, + } + m := NewManager(cfg, quietTestLogger(), ProjectContext{Org: "o", Repos: []string{"r"}, ACMMLevel: 5, PRsAllowed: true}) + m.SetSandboxConfig(config.AgentSandboxConfig{Enabled: true, Image: "img", WorkspaceDir: t.TempDir()}) + m.SetSandboxLauncher(sandboxFakeLauncher{}) + m.setSandboxRunnerForTest(runner) + m.SetSandboxPushMinter(sandboxFakeMinter{}) + m.SetSandboxPRClient(client) + var mu sync.Mutex + var lines []sandboxAuditLine + m.SetSandboxAuditCallback(func(agent, action, detail string) { + mu.Lock() + defer mu.Unlock() + lines = append(lines, sandboxAuditLine{agent, action, detail}) + }) + if err := m.Start(context.Background(), targetAgent); err != nil { + t.Fatalf("Start: %v", err) + } + return m, &lines, &mu +} + +func waitSandboxAudit(t *testing.T, lines *[]sandboxAuditLine, mu *sync.Mutex, action string) sandboxAuditLine { + t.Helper() + var got sandboxAuditLine + waitFor(t, "sandbox audit "+action, func() bool { + mu.Lock() + defer mu.Unlock() + for _, l := range *lines { + if l.action == action { + got = l + return true + } + } + return false + }) + return got +} + +// A PR client that cannot credit the PR itself (the sidecar's callback +// adapter) still gets the PR credited: the manager records agent_pr_created +// through the sandbox audit callback, in the detail shape every governor +// resolver parses (hivecommons/hive#18575). +func TestManagerSandboxCreditsPRWhenClientCannot(t *testing.T) { + m, lines, mu := newSandboxTargetManager(t, &countingPR{}, &sandboxFakeRunner{commits: 1}) + if err := m.SendKick(targetAgent, "fix"); err != nil { + t.Fatalf("SendKick: %v", err) + } + line := waitSandboxAudit(t, lines, mu, crux.AuditActionAgentPRCreated) + if line.agent != targetAgent { + t.Fatalf("credited to %q", line.agent) + } + for _, want := range []string{"repo=o/r", "number=42", "url=https://github.com/o/r/pull/42", "reused=false", "via=sandbox", "agent=dep-bump", "backend=claude", "model=sonnet", "head=hive/dep-bump/"} { + if !strings.Contains(line.detail, want) { + t.Fatalf("detail %q lacks %q", line.detail, want) + } + } +} + +func TestManagerSandboxDoesNotDoubleCreditAttributedPR(t *testing.T) { + m, lines, mu := newSandboxTargetManager(t, &agentPRClient{}, &sandboxFakeRunner{commits: 1}) + if err := m.SendKick(targetAgent, "fix"); err != nil { + t.Fatalf("SendKick: %v", err) + } + waitSandboxAudit(t, lines, mu, "sandbox_complete") + mu.Lock() + defer mu.Unlock() + for _, l := range *lines { + if l.action == crux.AuditActionAgentPRCreated { + t.Fatalf("PR credited twice: %+v", l) + } + } +} + +// targetPRClient opens PRs and reads push targets, like the hive's client. +type targetPRClient struct { + countingPR + fakeTargetReader +} + +func TestManagerSandboxResolvesTargetPRFromKick(t *testing.T) { + client := &targetPRClient{fakeTargetReader: fakeTargetReader{answers: []crux.PRPushTarget{ownTargetPR()}}} + runner := &targetRunner{} + m, lines, mu := newSandboxTargetManager(t, client, runner) + if err := m.SendKick(targetAgent, "šŸ”“ FIX-BEFORE-NEW — o/r#7 is red and has not moved\n\nRepair it"); err != nil { + t.Fatalf("SendKick: %v", err) + } + line := waitSandboxAudit(t, lines, mu, "sandbox_complete") + if !strings.Contains(line.detail, "target_pr=7") { + t.Fatalf("sandbox_complete = %q", line.detail) + } + if !runner.pushed() || client.count() != 0 { + t.Fatalf("pushed=%v opened=%d", runner.pushed(), client.count()) + } + mu.Lock() + defer mu.Unlock() + for _, l := range *lines { + if l.action == crux.AuditActionAgentPRCreated { + t.Fatalf("a push to an existing PR re-credited it: %+v", l) + } + } +} + +func TestManagerSandboxAuditsTargetRefusal(t *testing.T) { + other := ownTargetPR() + other.AuthorAgent = "scanner" + client := &targetPRClient{fakeTargetReader: fakeTargetReader{answers: []crux.PRPushTarget{other}}} + runner := &targetRunner{} + m, lines, mu := newSandboxTargetManager(t, client, runner) + if err := m.SendKick(targetAgent, "[hive:target-pr o/r#7]\nfix it"); err != nil { + t.Fatalf("SendKick: %v", err) + } + line := waitSandboxAudit(t, lines, mu, "sandbox_target_pr_refused") + for _, want := range []string{"repo=o/r", "number=7", "source=explicit", "opened by agent scanner"} { + if !strings.Contains(line.detail, want) { + t.Fatalf("detail %q lacks %q", line.detail, want) + } + } + if runner.pushed() { + t.Fatal("refused target pushed") + } +} + +// A kick that names a PR through a client that cannot read targets opens a +// new PR, as before the feature. +func TestManagerSandboxTargetWithoutReaderOpensNewPR(t *testing.T) { + pr := &countingPR{} + m, lines, mu := newSandboxTargetManager(t, pr, &sandboxFakeRunner{commits: 1}) + if err := m.SendKick(targetAgent, "[hive:target-pr o/r#7]\nfix it"); err != nil { + t.Fatalf("SendKick: %v", err) + } + waitSandboxAudit(t, lines, mu, "sandbox_complete") + if got := pr.count(); got != 1 { + t.Fatalf("CreatePR calls = %d", got) + } +} diff --git a/src/crux/agent/sandbox_workspace.go b/src/crux/agent/sandbox_workspace.go index b6f47d00b0..f2e6f10a96 100644 --- a/src/crux/agent/sandbox_workspace.go +++ b/src/crux/agent/sandbox_workspace.go @@ -33,6 +33,9 @@ func (e *SandboxExecutor) prepareWorkspace(ctx context.Context, spec *SandboxKic if spec.reviewOnly() { return "", "", errors.New("sandbox workspace prep: a run-stage kick cannot carry review PR targets") } + if spec.TargetPR > 0 { + return "", "", errors.New("sandbox workspace prep: a run-stage kick cannot push to an existing PR") + } return e.prepareRunStageWorkspace(ctx, spec) } workspace := sandbox.WorkspaceDir(spec.WorkspaceDir, spec.Agent, e.now().UnixNano()) @@ -67,6 +70,16 @@ func (e *SandboxExecutor) prepareWorkspace(ctx context.Context, spec *SandboxKic } return workspace, head, nil } + if spec.TargetPR > 0 { + // A fix kick on an existing PR works on that PR's head branch + // (hivecommons/hive#18574); the returned base is its head, so the + // commits counted after the run are the kick's own. + head, err := e.prepareTargetPRCheckout(ctx, spec, workspace, authArgs) + if err != nil { + return "", "", err + } + return workspace, head, nil + } if out, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", "checkout", "-B", spec.Branch, "FETCH_HEAD"); err != nil { return "", "", fmt.Errorf("git checkout: %w: %s", err, strings.TrimSpace(string(out))) } diff --git a/src/crux/pr_push_target.go b/src/crux/pr_push_target.go new file mode 100644 index 0000000000..b98421127c --- /dev/null +++ b/src/crux/pr_push_target.go @@ -0,0 +1,56 @@ +package crux + +import "context" + +// Sandboxed kicks on existing PRs (hivecommons/hive#18574) and the credit +// for PRs a sandboxed kick opens (hivecommons/hive#18575). +// +// Both are optional capabilities a host's PR client offers alongside +// PRCreator. The sandbox executor type-asserts the client it was given; a +// client without them keeps the older behaviour (CreatePR only, no pushes to +// an existing PR). + +// AuditActionAgentPRCreated is the audit action that credits a PR to the +// hive agent that produced it. It is the same action the host's pr-request +// relay records for a PR it opens from an agent's pane (pkg/github's +// AuditActionAgentPRCreated), and every governor resolver that maps a PR back +// to its agent reads it. The runtime emits it through the sandbox audit +// callback only when its PR client could not record it itself. +const AuditActionAgentPRCreated = "agent_pr_created" + +// AgentPRCreator opens a pull request for a sandboxed kick and credits it to +// agent through the host's PR-authorship record, the path the host's relay +// uses for a PR an agent opens from its pane: the attribution trailer on the +// body and the agent_pr_created audit entry. +type AgentPRCreator interface { + CreateAgentPR(ctx context.Context, agent, repo, head, base, title, body string) (PRResult, error) +} + +// PRPushTarget is what the host knows about an existing pull request a +// sandboxed kick would push to. +type PRPushTarget struct { + Number int + URL string + // State is GitHub's state: "open" or "closed". + State string + BaseRef string + HeadRef string + HeadSHA string + // HeadRepo is the full name of the repository the head branch lives in. + // Empty when GitHub no longer knows it (a deleted fork). + HeadRepo string + // Fork is true when the head branch lives outside the base repository, + // or when the host cannot tell where it lives. + Fork bool + // Author is the login GitHub records as the PR's author. + Author string + // AuthorAgent is the hive agent the host's PR-authorship record credits + // with opening the PR, or "" when no hive agent did. + AuthorAgent string +} + +// PRPushTargetReader reads a PRPushTarget. The executor reads it before the +// sandbox starts and again before the push. +type PRPushTargetReader interface { + PRPushTarget(ctx context.Context, repo string, number int) (PRPushTarget, error) +} diff --git a/src/crux/pushbroker/lease_test.go b/src/crux/pushbroker/lease_test.go new file mode 100644 index 0000000000..49ad7ae82d --- /dev/null +++ b/src/crux/pushbroker/lease_test.go @@ -0,0 +1,85 @@ +package pushbroker + +import ( + "context" + "errors" + "slices" + "strings" + "testing" +) + +// A push to an existing PR branch (hivecommons/hive#18574) carries a lease on +// the head the kick checked out and adds only commits built on it. +func TestBrokerLeasedPushToExistingBranch(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "base.txt", "base\n") + lease := strings.TrimSpace(runGitOutput(t, dir, "rev-parse", "HEAD")) + writeCommit(t, dir, "fix.txt", "fix\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 || res.Lease != lease { + t.Fatalf("pushed=%v lease=%q", res.Pushed, res.Lease) + } + if !slices.Contains(r.argsOnPush, "--force-with-lease=refs/heads/work:"+lease) { + t.Fatalf("push lacks the lease: %v", r.argsOnPush) + } + if got := r.argsOnPush[len(r.argsOnPush)-2:]; got[0] != DefaultRemote || got[1] != "HEAD:refs/heads/work" { + t.Fatalf("push target = %v", got) + } + if !slices.Equal(res.ChangedFiles, []string{"fix.txt"}) { + t.Fatalf("scans judged %v, want only the kick's own commit", res.ChangedFiles) + } +} + +func TestBrokerRefusesLeasedPushThatRewritesTheBranch(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "base.txt", "base\n") + runGit(t, dir, "checkout", "-b", "side") + writeCommit(t, dir, "side.txt", "theirs\n") + lease := strings.TrimSpace(runGitOutput(t, dir, "rev-parse", "HEAD")) + runGit(t, dir, "checkout", "work") + writeCommit(t, dir, "fix.txt", "fix\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 || !strings.Contains(err.Error(), "does not descend from") { + t.Fatalf("err = %v", err) + } + if res.Pushed || r.argsOnPush != nil { + t.Fatal("a push that drops the PR's commits must not run") + } +} + +func TestBrokerRefusesMalformedLease(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "fix.txt", "fix\n") + for _, lease := range []string{"abc", "--upload-pack=x", strings.Repeat("g", 40)} { + _, err := (&Broker{Workspace: dir, Branch: "work", LeaseSHA: lease, Repo: "hivecommons/hive", Minter: fakeMinter{"t"}}).Run(context.Background()) + if err == nil || !strings.Contains(err.Error(), "is not a full commit SHA") { + t.Fatalf("lease %q: err = %v", lease, err) + } + } +} + +// failingPushRunner refuses the push as a remote whose branch moved would. +type failingPushRunner struct{} + +func (failingPushRunner) Run(ctx context.Context, dir string, env []string, name string, args ...string) ([]byte, error) { + if name == "git" && slices.Contains(args, "push") { + return []byte("! [rejected] HEAD -> work (stale info)"), errors.New("exit status 1") + } + return ExecRunner{}.Run(ctx, dir, env, name, args...) +} + +func TestBrokerReportsLeaseRejection(t *testing.T) { + dir := initRepo(t) + writeCommit(t, dir, "base.txt", "base\n") + lease := strings.TrimSpace(runGitOutput(t, dir, "rev-parse", "HEAD")) + writeCommit(t, dir, "fix.txt", "fix\n") + res, err := (&Broker{Workspace: dir, Branch: "work", BaseRef: lease, LeaseSHA: lease, Repo: "hivecommons/hive", Minter: fakeMinter{"t"}, Runner: failingPushRunner{}}).Run(context.Background()) + if err == nil || !strings.Contains(err.Error(), "is never overwritten") || res.Pushed { + t.Fatalf("err = %v pushed=%v", err, res.Pushed) + } +} diff --git a/src/crux/pushbroker/pushbroker.go b/src/crux/pushbroker/pushbroker.go index 785c131cc4..d32042d1d5 100644 --- a/src/crux/pushbroker/pushbroker.go +++ b/src/crux/pushbroker/pushbroker.go @@ -303,25 +303,42 @@ type Broker struct { Logger *slog.Logger Now func() time.Time Mutation effects.Boundary -} + // LeaseSHA, when set, makes the push an update of an EXISTING branch: a + // hive-opened PR's head that a sandboxed kick checked out + // (hivecommons/hive#18574). The outgoing commits must descend from it, + // and the push carries --force-with-lease=refs/heads/:, + // so it lands only while the remote branch still points at the commit + // the kick started from. Together the two make the push a fast-forward + // of exactly that commit: it can never drop a commit someone else + // pushed. Set BaseRef to the same commit so the scans judge only the + // kick's own commits. + LeaseSHA string +} + +// leaseSHARE is a full object name; anything else is refused before it can +// reach git's argv. +var leaseSHARE = regexp.MustCompile(`^[0-9a-f]{40}([0-9a-f]{24})?$`) type Result struct { - Workspace string `json:"workspace"` - Repo string `json:"repo"` - Branch string `json:"branch"` - Remote string `json:"remote"` - Commit string `json:"commit,omitempty"` - ChangedFiles []string `json:"changed_files,omitempty"` - ProtectedReject []string `json:"protected_reject,omitempty"` - SecretRejected bool `json:"secret_rejected"` - Pushed bool `json:"pushed"` - Error string `json:"error,omitempty"` - StartedAt time.Time `json:"started_at"` - FinishedAt time.Time `json:"finished_at"` + Workspace string `json:"workspace"` + Repo string `json:"repo"` + Branch string `json:"branch"` + Remote string `json:"remote"` + Commit string `json:"commit,omitempty"` + ChangedFiles []string `json:"changed_files,omitempty"` + ProtectedReject []string `json:"protected_reject,omitempty"` + SecretRejected bool `json:"secret_rejected"` + Pushed bool `json:"pushed"` + // Lease is the commit the remote branch had to still point at + // (Broker.LeaseSHA); empty for a new branch. + Lease string `json:"lease,omitempty"` + Error string `json:"error,omitempty"` + StartedAt time.Time `json:"started_at"` + FinishedAt time.Time `json:"finished_at"` } func (b *Broker) Run(ctx context.Context) (res Result, err error) { - res = Result{Workspace: b.Workspace, Repo: b.Repo, Branch: b.Branch, Remote: b.remote(), StartedAt: b.now()} + res = Result{Workspace: b.Workspace, Repo: b.Repo, Branch: b.Branch, Remote: b.remote(), Lease: strings.TrimSpace(b.LeaseSHA), StartedAt: b.now()} defer func() { res.FinishedAt = b.now() }() if err := b.validate(); err != nil { res.Error = err.Error() @@ -350,6 +367,11 @@ func (b *Broker) Run(ctx context.Context) (res Result, err error) { return b.fail(res, err) } } + if res.Lease != "" { + if err := b.ensureDescendsFromLease(ctx, res.Lease); err != nil { + return b.fail(res, err) + } + } if err := b.rejectForgedLaneSignoffs(ctx, baseRef, baseExists); err != nil { return b.fail(res, err) } @@ -410,8 +432,14 @@ func (b *Broker) Run(ctx context.Context) (res Result, err error) { args := []string{ "-c", "core.hooksPath=/dev/null", "-c", "credential.helper=" + pushCredentialHelper, - "push", "--no-verify", b.remote(), "HEAD:refs/heads/" + b.Branch, + "push", "--no-verify", + } + inputs := map[string]string{"commit": res.Commit, "remote": b.remote()} + if res.Lease != "" { + args = append(args, "--force-with-lease=refs/heads/"+b.Branch+":"+res.Lease) + inputs["lease"] = res.Lease } + args = append(args, b.remote(), "HEAD:refs/heads/"+b.Branch) // Append AFTER PushEnv: it strips inherited credential variables, and this // one is deliberately supplied rather than inherited. gitEnv also carries // the workspace config overrides, so the repository's own credential @@ -422,12 +450,15 @@ func (b *Broker) Run(ctx context.Context) (res Result, err error) { Repo: b.Repo, Kind: effects.KindBranchPush, Target: b.Branch, - Inputs: map[string]string{"commit": res.Commit, "remote": b.remote()}, + Inputs: inputs, }, func(ctx context.Context) (effects.Result, error) { _, runErr := b.runner().Run(ctx, b.Workspace, env, "git", args...) return effects.Result{Provenance: res.Commit}, runErr }) if err != nil { + if res.Lease != "" { + return b.fail(res, fmt.Errorf("git push to existing branch %q refused or failed; with the lease, a branch that moved off %s since the kick checked it out is never overwritten: %w", b.Branch, shortSHA(res.Lease), err)) + } return b.fail(res, fmt.Errorf("git push failed: %w", err)) } res.Pushed = true @@ -438,6 +469,9 @@ func (b *Broker) validate() error { if strings.TrimSpace(b.Workspace) == "" || strings.TrimSpace(b.Branch) == "" || strings.TrimSpace(b.Repo) == "" { return errors.New("pushbroker: workspace, branch, and repo are required") } + if lease := strings.TrimSpace(b.LeaseSHA); lease != "" && !leaseSHARE.MatchString(lease) { + return fmt.Errorf("pushbroker: lease %q is not a full commit SHA", lease) + } if b.Minter == nil { return errors.New("pushbroker: token minter is required") } @@ -551,6 +585,16 @@ func (b *Broker) ensureFastForward(ctx context.Context, base string) error { return nil } +// ensureDescendsFromLease refuses a push to an existing branch whose +// outgoing commits do not build on the commit the kick checked out: such a +// push would rewrite the PR's history. +func (b *Broker) ensureDescendsFromLease(ctx context.Context, lease string) error { + if _, err := b.git(ctx, "merge-base", "--is-ancestor", lease, "HEAD"); err != nil { + return fmt.Errorf("pushbroker: refusing to push to existing branch %q: HEAD does not descend from %s, the head the kick checked out; add commits on top instead of rewriting the PR: %w", b.Branch, shortSHA(lease), err) + } + return nil +} + var signedOffByRE = regexp.MustCompile(`(?mi)^Signed-off-by:\s*(.*?)\s*<([^<>]+)>\s*$`) // gitIdentRE parses the `git var GIT_AUTHOR_IDENT` output format: diff --git a/src/crux/sandbox/naming.go b/src/crux/sandbox/naming.go index e7b9a8dd2f..b648ed3548 100644 --- a/src/crux/sandbox/naming.go +++ b/src/crux/sandbox/naming.go @@ -123,6 +123,42 @@ func LatestTranscript(root, agent string) (path string, ok bool) { return cands[0].path, true } +// Work branches. A sandboxed kick that opens a new PR pushes it from +// hive//. The governor's last-resort PR-owner +// fallback reads the agent back out of that name (hivecommons/hive#18575): +// splitting at the first "/" credits every sandbox PR to an agent named +// "hive". +const ( + workBranchPrefix = "hive/" + // WorkBranchTimeLayout is the time format of a work branch's last segment. + WorkBranchTimeLayout = "20060102150405" +) + +// WorkBranch is the branch a sandboxed kick for agent started at stamp +// pushes a new PR from. +func WorkBranch(agent, stamp string) string { + return workBranchPrefix + AgentSlug(agent) + "/" + stamp +} + +// WorkBranchAgentSlug returns the agent slug from a branch WorkBranch named, +// or ok=false for any other branch. +func WorkBranchAgentSlug(branch string) (string, bool) { + rest, ok := strings.CutPrefix(strings.TrimSpace(branch), workBranchPrefix) + if !ok { + return "", false + } + slug, stamp, ok := strings.Cut(rest, "/") + if !ok || slug == "" || AgentSlug(slug) != slug || len(stamp) != len(WorkBranchTimeLayout) { + return "", false + } + for _, r := range stamp { + if r < '0' || r > '9' { + return "", false + } + } + return slug, true +} + // TranscriptExitCode reads the trailing "exit code: N" line a transcript // ends with. ok is false for transcripts written before the line existed or // otherwise lacking it. diff --git a/src/crux/sandbox/naming_test.go b/src/crux/sandbox/naming_test.go index 6649b1ef36..c8a531b221 100644 --- a/src/crux/sandbox/naming_test.go +++ b/src/crux/sandbox/naming_test.go @@ -128,3 +128,22 @@ func TestLatestTranscriptSkipsDirectoriesAndBreaksModTimeTies(t *testing.T) { t.Fatalf("LatestTranscript = %q, %v; want %q", got, ok, paths[0]) } } + +func TestWorkBranchRoundTrip(t *testing.T) { + stamp := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC).Format(WorkBranchTimeLayout) + branch := WorkBranch("Dep Bump", stamp) + if branch != "hive/dep-bump/20261116120000" { + t.Fatalf("WorkBranch = %q", branch) + } + if slug, ok := WorkBranchAgentSlug(branch); !ok || slug != "dep-bump" { + t.Fatalf("WorkBranchAgentSlug(%q) = %q, %v", branch, slug, ok) + } + for _, other := range []string{ + "", "scanner/fix-1", "hive/dep-bump", "hive//20261116120000", "hive/Dep/20261116120000", + "hive/dep-bump/2026111612000", "hive/dep-bump/2026111612000x", "hive/dep-bump/20261116120000/x", + } { + if slug, ok := WorkBranchAgentSlug(other); ok { + t.Errorf("WorkBranchAgentSlug(%q) = %q, want no match", other, slug) + } + } +} diff --git a/src/docs/sandbox-isolation.md b/src/docs/sandbox-isolation.md index 5a7b30eebe..a8667fb010 100644 --- a/src/docs/sandbox-isolation.md +++ b/src/docs/sandbox-isolation.md @@ -163,6 +163,25 @@ For sandboxed agents, a kick now follows this path: ``` 5. If the sandbox produced commits, `pkg/pushbroker.Broker` scans the committed diff for token-like secrets and protected-path edits, mints a short-lived scoped GitHub App token outside the sandbox, pushes the branch, and opens a PR through the existing App-authored GitHub client. Broker rejection records audit detail and nothing is pushed. The PR title and body come from `.hive/pr-title.txt` (first line) and `.hive/pr-body.md` if the agent wrote them in the workspace, otherwise from the HEAD commit's subject and body; a generic `[] sandbox changes` placeholder is used only when neither exists. Kick templates for sandboxed agents should ask the agent to write those two files. + **The PR is credited to the agent** ([#18575](https://github.com/hivecommons/hive/issues/18575)). Hive records the PR the same way it records one an agent opens from its pane through the pr-request relay: the `— hive: agent= …` attribution trailer on the body (when `governor.attribution_trailer` is on) and an `agent_pr_created` audit entry for `repo=, number=`, plus `via=sandbox`. Every governor path that maps a PR to its agent reads that entry: the FIX-BEFORE-NEW block, the red-CI webhook kick, the review-thread monitor and held-PR routing. Red CI on a sandbox PR therefore kicks the agent that opened it, not `scanner`. When the PR client cannot record the entry itself (the sidecar runtime's callback adapter), the runtime emits the same `agent_pr_created` entry through the sandbox audit callback, so the PR is still credited, without the trailer. As a last resort, the governor also reads the agent out of the work branch name, `hive//`. + +### Fix kicks on an existing PR + +A sandboxed fix kick can add commits to one of the agent's own open PRs instead of opening a new one ([#18574](https://github.com/hivecommons/hive/issues/18574)). Hive picks the target from hive-written parts of the kick message. The first match wins: + +1. An explicit `[hive:target-pr /#]` first line (after an optional `[agent:]` line). +2. The first line of the targeted red-PR kick, `šŸ”“ FIX-BEFORE-NEW — /# is red and has not moved`. +3. The first line of the red-CI webhook kick, `CI failed on /# () …`. +4. The first row of the kick's FIX-BEFORE-NEW block (`## šŸ”“ FIX-BEFORE-NEW — your open PRs with failing CI or merge conflicts`). An adopted contributor PR is skipped. +5. The agent's topic claim, when the kick's topic-claims block lists exactly one PR. With several claims the kick does not say which topic it is for, so it opens a new PR as before. + +A target in another repository is ignored, because the workspace clones the primary repo only. The kick needs push rights (ACMM `CanPush` and `project.prs_allowed`) and a PR client that can read PR targets: the in-process hive client can, and the sidecar's callback adapter cannot yet. Without one, hive logs a warning and the kick opens a new PR as before. Review kicks and run-stage kicks never get a target. + +**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=`. + +**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. ## Running sandboxed kicks as Kubernetes Jobs diff --git a/src/pkg/github/sandbox_agent_pr.go b/src/pkg/github/sandbox_agent_pr.go new file mode 100644 index 0000000000..e3763ad5da --- /dev/null +++ b/src/pkg/github/sandbox_agent_pr.go @@ -0,0 +1,133 @@ +package github + +import ( + "context" + "fmt" + "strconv" + "strings" + "time" + + "github.com/hivecommons/hive/crux" +) + +// PRs a sandboxed kick opens or pushes to (hivecommons/hive#18574, +// hivecommons/hive#18575). +// +// A sandboxed kick has no GitHub token and no pod-local relays, so the +// sandbox executor opens its PR through this client directly, not through the +// pr-request watcher. CreateAgentPR gives that PR the same credit the watcher +// gives a PR an agent requests from its pane: the attribution trailer on the +// body, the agent_pr_created audit entry, and the PR-opened hooks. Every +// governor resolver that maps a PR to its agent (the FIX-BEFORE-NEW block, +// the red-CI webhook kick, the review-thread monitor, held-PR routing) reads +// that audit entry, so the PR routes back to the agent that produced it. + +// SandboxPRVia is the "via" detail pair on an agent_pr_created entry the +// sandbox path recorded, so the audit log tells the two creation paths apart. +const SandboxPRVia = "sandbox" + +// createPRFunc is CreatePR's signature, injectable for tests. +type createPRFunc func(ctx context.Context, repo, head, base, title, body string) (CreatePRResult, error) + +// CreateAgentPR opens a PR for a sandboxed kick by agent and credits it to +// agent (crux.AgentPRCreator). +func (c *Client) CreateAgentPR(ctx context.Context, agent, repo, head, base, title, body string) (CreatePRResult, error) { + return c.createAgentPR(ctx, agent, repo, head, base, title, body, c.CreatePR) +} + +func (c *Client) createAgentPR(ctx context.Context, agent, repo, head, base, title, body string, create createPRFunc) (CreatePRResult, error) { + agent = strings.TrimSpace(agent) + if agent == "" { + return create(ctx, repo, head, base, title, body) + } + meta := c.attributionMeta(agent) + if c.attributionTrailerOn() { + body = AppendTrailer(body, meta) + } + res, err := create(ctx, repo, head, base, title, body) + if err != nil { + return res, err + } + c.RecordAgentPROpened(meta, repo, head, title, body, res) + return res, nil +} + +// RecordAgentPROpened records the credit for a PR the sandbox path opened +// for meta.Agent: the PR-opened hooks for a new PR, then the agent_pr_created +// audit entry (reuses included, as the watcher records them). A result with +// no PR number records nothing. +func (c *Client) RecordAgentPROpened(meta InvocationMeta, repo, head, title, body string, res CreatePRResult) { + if c == nil || res.Number <= 0 || strings.TrimSpace(meta.Agent) == "" { + return + } + if !res.AlreadyExisted { + if hook := c.prOpenedHook.Load(); hook != nil && *hook != nil { + go (*hook)(meta.Agent, repo, res.Number, res.URL) + } + if hook := c.prOpenedDetailHook.Load(); hook != nil && *hook != nil { + go (*hook)(PROpenedDetail{Agent: meta.Agent, Repo: repo, Number: res.Number, URL: res.URL, Title: title, Body: body, Author: res.Author, Head: head}) + } + } + c.recordWriteAudit(AuditActionAgentPRCreated, meta, + WriteTarget{Repo: repo, Number: res.Number}, + "author", res.Author, + "url", res.URL, + "reused", strconv.FormatBool(res.AlreadyExisted), + "duplicate_tree", strconv.FormatBool(res.DuplicateTree), + "duplicate_reason", res.DuplicateReason, + "head", head, + "via", SandboxPRVia) +} + +// AuthorAgentForPRFromAudit is AuthorAgentForPR over the audit log at path. +func AuthorAgentForPRFromAudit(path, repo string, number int) string { + return auditAuthorAgentForPR(repo, number, path, time.Now().UTC().Add(-reviewVerdictAuditLookback)) +} + +// PRPushTarget reads what the sandbox executor needs to decide whether a +// sandboxed kick may push to an existing PR (crux.PRPushTargetReader). The +// agent credited with the PR is the agent_pr_created audit entry's. Only +// when the audit log has none (an entry older than its lookback) is the +// body's attribution trailer read, and only on a PR the hive's App opened: +// on anyone else's PR the trailer is text the author could have written. +func (c *Client) PRPushTarget(ctx context.Context, repo string, number int) (crux.PRPushTarget, error) { + if c == nil || c.client == nil { + return crux.PRPushTarget{}, ErrNoGitHubClient + } + owner, name := c.splitRepo(repo) + pr, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:sandbox_target_pr"), owner, name, number) + if err != nil { + return crux.PRPushTarget{}, fmt.Errorf("reading %s/%s#%d: %w", owner, name, number, err) + } + out := crux.PRPushTarget{ + Number: pr.GetNumber(), + URL: pr.GetHTMLURL(), + State: pr.GetState(), + BaseRef: pr.GetBase().GetRef(), + HeadRef: pr.GetHead().GetRef(), + HeadSHA: pr.GetHead().GetSHA(), + HeadRepo: pr.GetHead().GetRepo().GetFullName(), + Author: pr.GetUser().GetLogin(), + } + if out.Number == 0 { + out.Number = number + } + baseRepo := pr.GetBase().GetRepo().GetFullName() + if baseRepo == "" { + baseRepo = owner + "/" + name + } + out.Fork = out.HeadRepo == "" || !strings.EqualFold(out.HeadRepo, baseRepo) + out.AuthorAgent = AuthorAgentForPRFromAudit(reviewVerdictAuditPath, owner+"/"+name, number) + if out.AuthorAgent == "" && c.appBotLogin != "" && strings.EqualFold(out.Author, c.appBotLogin) { + if meta, ok := ParseAttributionTrailer(pr.GetBody()); ok { + out.AuthorAgent = strings.TrimSpace(meta.Agent) + } + } + return out, nil +} + +// Compile-time proof that the client offers the sandbox seams. +var ( + _ crux.AgentPRCreator = (*Client)(nil) + _ crux.PRPushTargetReader = (*Client)(nil) +) diff --git a/src/pkg/github/sandbox_agent_pr_test.go b/src/pkg/github/sandbox_agent_pr_test.go new file mode 100644 index 0000000000..a6739848bd --- /dev/null +++ b/src/pkg/github/sandbox_agent_pr_test.go @@ -0,0 +1,210 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/crux" +) + +// The runtime emits this action itself when its PR client cannot credit a +// sandbox PR; the resolvers here read it. The two spellings must be one. +func TestSandboxPRCreditActionMatchesRuntime(t *testing.T) { + if crux.AuditActionAgentPRCreated != AuditActionAgentPRCreated { + t.Fatalf("crux=%q github=%q", crux.AuditActionAgentPRCreated, AuditActionAgentPRCreated) + } +} + +// writeAuditJSONL appends rec to path in the audit log's on-disk shape. +func writeAuditJSONL(t *testing.T, path string, rec AuditRecord) { + t.Helper() + line, err := json.Marshal(map[string]any{ + "ts": time.Now().UTC().Format(time.RFC3339), "user": "system", + "action": rec.Action, "detail": rec.Detail, "agent": rec.Agent, "repo": rec.Repo, "target": rec.Target, + }) + if err != nil { + t.Fatal(err) + } + f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) + if err != nil { + t.Fatal(err) + } + defer f.Close() + if _, err := f.Write(append(line, '\n')); err != nil { + t.Fatal(err) + } +} + +// A PR the sandbox path opens is credited exactly as a relay-opened one: +// trailer on the body, agent_pr_created in the audit log, PR-opened hooks. +// The resolver the red-CI kick uses then maps the PR back to the agent +// (hivecommons/hive#18575). +func TestCreateAgentPRCreditsTheAgent(t *testing.T) { + auditPath := filepath.Join(t.TempDir(), "audit.jsonl") + c := &Client{org: "o"} + c.SetAttributionResolver(func(agent string) InvocationMeta { + return InvocationMeta{Agent: agent, Backend: "claude", Model: "sonnet"} + }) + var recs []AuditRecord + c.SetAttributionAuditRecord(func(rec AuditRecord) { + recs = append(recs, rec) + writeAuditJSONL(t, auditPath, rec) + }) + var wg sync.WaitGroup + wg.Add(2) + var openedAgent string + var detail PROpenedDetail + c.SetPROpenedHook(func(agent, _ string, _ int, _ string) { openedAgent = agent; wg.Done() }) + c.SetPROpenedDetailHook(func(d PROpenedDetail) { detail = d; wg.Done() }) + + var sentBody string + create := func(_ context.Context, repo, head, base, title, body string) (CreatePRResult, error) { + sentBody = body + return CreatePRResult{Number: 42, URL: "https://github.com/o/r/pull/42", Author: "hive-app[bot]"}, nil + } + res, err := c.createAgentPR(context.Background(), "dep-bump", "o/r", "hive/dep-bump/20261116120000", "main", "Bump torch", "Bumps torch.", create) + if err != nil || res.Number != 42 { + t.Fatalf("createAgentPR = %+v, %v", res, err) + } + wg.Wait() + if meta, ok := ParseAttributionTrailer(sentBody); !ok || meta.Agent != "dep-bump" || meta.Backend != "claude" { + t.Fatalf("body %q carries no trailer for the agent (%+v)", sentBody, meta) + } + if len(recs) != 1 || recs[0].Action != AuditActionAgentPRCreated || recs[0].Agent != "dep-bump" || recs[0].Target != 42 { + t.Fatalf("audit = %+v", recs) + } + for _, want := range []string{"repo=o/r", "number=42", "via=sandbox", "reused=false", "head=hive/dep-bump/20261116120000"} { + if !strings.Contains(recs[0].Detail, want) { + t.Fatalf("detail %q lacks %q", recs[0].Detail, want) + } + } + if openedAgent != "dep-bump" || detail.Number != 42 || detail.Head != "hive/dep-bump/20261116120000" { + t.Fatalf("hooks: agent=%q detail=%+v", openedAgent, detail) + } + if got := AuthorAgentForPRFromAudit(auditPath, "o/r", 42); got != "dep-bump" { + t.Fatalf("resolver credits %q", got) + } +} + +func TestCreateAgentPRTrailerOffStillAudits(t *testing.T) { + c := &Client{org: "o"} + c.SetAttributionHooks(AttributionHooks{TrailerEnabled: func() bool { return false }}) + var recs []AuditRecord + c.SetAttributionAuditRecord(func(rec AuditRecord) { recs = append(recs, rec) }) + var sentBody string + create := func(_ context.Context, _, _, _, _, body string) (CreatePRResult, error) { + sentBody = body + return CreatePRResult{Number: 9, AlreadyExisted: true}, nil + } + if _, err := c.createAgentPR(context.Background(), "dep-bump", "o/r", "h", "main", "t", "body", create); err != nil { + t.Fatal(err) + } + if sentBody != "body" { + t.Fatalf("trailer appended with the toggle off: %q", sentBody) + } + if len(recs) != 1 || !strings.Contains(recs[0].Detail, "reused=true") { + t.Fatalf("a reused PR is still credited: %+v", recs) + } +} + +func TestCreateAgentPRErrorsAndAnonymousCreditNothing(t *testing.T) { + c := &Client{org: "o"} + var recs []AuditRecord + c.SetAttributionAuditRecord(func(rec AuditRecord) { recs = append(recs, rec) }) + boom := errors.New("boom") + failing := func(context.Context, string, string, string, string, string) (CreatePRResult, error) { + return CreatePRResult{}, boom + } + if _, err := c.createAgentPR(context.Background(), "dep-bump", "o/r", "h", "main", "t", "b", failing); !errors.Is(err, boom) { + t.Fatalf("err = %v", err) + } + var body string + plain := func(_ context.Context, _, _, _, _, b string) (CreatePRResult, error) { + body = b + return CreatePRResult{Number: 3}, nil + } + if _, err := c.createAgentPR(context.Background(), " ", "o/r", "h", "main", "t", "b", plain); err != nil { + t.Fatal(err) + } + if body != "b" || len(recs) != 0 { + t.Fatalf("anonymous create credited: body=%q recs=%+v", body, recs) + } + c.RecordAgentPROpened(InvocationMeta{Agent: "dep-bump"}, "o/r", "h", "t", "b", CreatePRResult{}) + if len(recs) != 0 { + t.Fatalf("a result without a PR number was credited: %+v", recs) + } + var nilClient *Client + nilClient.RecordAgentPROpened(InvocationMeta{Agent: "dep-bump"}, "o/r", "h", "t", "b", CreatePRResult{Number: 1}) + if _, err := nilClient.CreateAgentPR(context.Background(), "dep-bump", "o/r", "h", "main", "t", "b"); !errors.Is(err, ErrNoGitHubClient) { + t.Fatalf("nil client CreateAgentPR err = %v", err) + } +} + +func TestPRPushTarget(t *testing.T) { + auditPath := filepath.Join(t.TempDir(), "audit.jsonl") + old := reviewVerdictAuditPath + reviewVerdictAuditPath = auditPath + t.Cleanup(func() { reviewVerdictAuditPath = old }) + writeAuditJSONL(t, auditPath, AuditRecord{Action: AuditActionAgentPRCreated, Agent: "dep-bump", Detail: "repo=o/r, number=7, via=sandbox"}) + + prs := map[string]string{ + "7": `{"number":7,"html_url":"https://github.com/o/r/pull/7","state":"open","user":{"login":"hive-app[bot]"},"body":"x", + "base":{"ref":"main","repo":{"full_name":"o/r"}},"head":{"ref":"hive/dep-bump/20261116120000","sha":"abc","repo":{"full_name":"o/r"}}}`, + "8": `{"number":8,"state":"open","user":{"login":"forker"},"body":"x", + "base":{"ref":"main","repo":{"full_name":"o/r"}},"head":{"ref":"feature","sha":"def","repo":{"full_name":"forker/r"}}}`, + "9": `{"number":9,"state":"open","user":{"login":"hive-app[bot]"},"body":"Old PR.\n\n— hive: agent=dep-bump backend=claude", + "base":{"ref":"main","repo":{"full_name":"o/r"}},"head":{"ref":"dep-bump/x","sha":"ghi","repo":{"full_name":"o/r"}}}`, + "10": `{"number":10,"state":"open","user":{"login":"mallory"},"body":"— hive: agent=dep-bump", + "base":{"ref":"main"},"head":{"ref":"x","sha":"jkl"}}`, + } + mux := http.NewServeMux() + mux.HandleFunc("/repos/o/r/pulls/", func(w http.ResponseWriter, r *http.Request) { + body, ok := prs[strings.TrimPrefix(r.URL.Path, "/repos/o/r/pulls/")] + if !ok { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(body)) + }) + server := httptest.NewServer(mux) + defer server.Close() + c := newTestClient(t, server, "o", []string{"o/r"}) + c.SetAppBotLogin("hive-app[bot]") + + own, err := c.PRPushTarget(context.Background(), "o/r", 7) + if err != nil { + t.Fatal(err) + } + if own.AuthorAgent != "dep-bump" || own.Fork || own.State != "open" || own.HeadRef != "hive/dep-bump/20261116120000" || own.HeadSHA != "abc" || own.BaseRef != "main" || own.URL == "" { + t.Fatalf("own PR = %+v", own) + } + fork, err := c.PRPushTarget(context.Background(), "o/r", 8) + if err != nil || !fork.Fork || fork.AuthorAgent != "" { + t.Fatalf("fork PR = %+v, %v", fork, err) + } + trailer, err := c.PRPushTarget(context.Background(), "r", 9) + if err != nil || trailer.AuthorAgent != "dep-bump" || trailer.Fork { + t.Fatalf("App PR credited by trailer only = %+v, %v", trailer, err) + } + forged, err := c.PRPushTarget(context.Background(), "o/r", 10) + if err != nil || forged.AuthorAgent != "" || !forged.Fork { + t.Fatalf("a stranger's trailer must not credit an agent, and an unknown head repo is a fork: %+v, %v", forged, err) + } + if _, err := c.PRPushTarget(context.Background(), "o/r", 11); err == nil { + t.Fatal("a missing PR must be an error") + } + var nilClient *Client + if _, err := nilClient.PRPushTarget(context.Background(), "o/r", 7); !errors.Is(err, ErrNoGitHubClient) { + t.Fatalf("nil client err = %v", err) + } +} diff --git a/src/pkg/scheduler/sandbox_pr_fix_first_test.go b/src/pkg/scheduler/sandbox_pr_fix_first_test.go new file mode 100644 index 0000000000..8588b7c9fe --- /dev/null +++ b/src/pkg/scheduler/sandbox_pr_fix_first_test.go @@ -0,0 +1,59 @@ +package scheduler + +import ( + "encoding/json" + "io" + "log/slog" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// Regression for hivecommons/hive#18575: a PR the hive opened from a +// sandboxed kick used to carry no agent credit, so red CI on it fell to +// scanner's FIX-BEFORE-NEW block and the sandboxed agent never saw it. The +// PR is credited through the same agent_pr_created record a relay-opened PR +// gets, the red-CI resolver reads it back, and the PR lands in the opening +// agent's block, not scanner's. +func TestSandboxOpenedPRAppearsInItsAgentsFixBeforeNew(t *testing.T) { + auditPath := filepath.Join(t.TempDir(), "audit.jsonl") + client := github.NewClient("fake-token", "o", []string{"o/r"}, slog.New(slog.NewTextHandler(io.Discard, nil)), "") + client.SetAttributionAuditRecord(func(rec github.AuditRecord) { + line, err := json.Marshal(map[string]any{ + "ts": time.Now().UTC().Format(time.RFC3339), "user": "system", + "action": rec.Action, "detail": rec.Detail, "agent": rec.Agent, "repo": rec.Repo, "target": rec.Target, + }) + if err != nil { + t.Fatal(err) + } + f, err := os.OpenFile(auditPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) + if err != nil { + t.Fatal(err) + } + defer f.Close() + if _, err := f.Write(append(line, '\n')); err != nil { + t.Fatal(err) + } + }) + const head = "hive/dep-bump/20261116120000" + // The step CreateAgentPR runs once GitHub has opened the PR. + client.RecordAgentPROpened(github.InvocationMeta{Agent: "dep-bump", Backend: "claude"}, "o/r", head, "Bump torch", "Bumps torch.", + github.CreatePRResult{Number: 42, URL: "https://github.com/o/r/pull/42", Author: "hive-app[bot]"}) + + owner := github.AuthorAgentForPRFromAudit(auditPath, "o/r", 42) + if owner != "dep-bump" { + t.Fatalf("red-CI resolver credits the sandbox PR to %q, want dep-bump", owner) + } + signals := []github.CIFailureSignal{{Repo: "o/r", Number: 42, HeadSHA: "abc", Check: "build", Conclusion: "failure", Title: "Bump torch", Agent: owner, At: time.Now()}} + block := formatRedPRFixDataWithSignals(nil, "dep-bump", signals) + if !strings.Contains(block, "FIX-BEFORE-NEW") || !strings.Contains(block, "#42 o/r") || !strings.Contains(block, "build") { + t.Fatalf("the sandboxed agent's block does not list its red PR:\n%s", block) + } + if got := formatRedPRFixDataWithSignals(nil, "scanner", signals); got != "" { + t.Fatalf("scanner still gets the sandbox PR:\n%s", got) + } +} From 3975325c652b2f25d1e9bae0857ec03f0356f74b Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 20:32:33 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8C=B1=20pushbroker:=20move=20workspa?= =?UTF-8?q?ce=20config=20guard=20to=20its=20own=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pushbroker.go passed the crux 800-line limit with the lease support. Signed-off-by: Andy Anderson --- src/crux/pushbroker/pushbroker.go | 172 ---------------------- src/crux/pushbroker/workspace_config.go | 180 ++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 172 deletions(-) create mode 100644 src/crux/pushbroker/workspace_config.go diff --git a/src/crux/pushbroker/pushbroker.go b/src/crux/pushbroker/pushbroker.go index d32042d1d5..ee61b7ca5f 100644 --- a/src/crux/pushbroker/pushbroker.go +++ b/src/crux/pushbroker/pushbroker.go @@ -12,7 +12,6 @@ import ( "path/filepath" "regexp" "slices" - "strconv" "strings" "time" @@ -58,177 +57,6 @@ var credentialEnvPrefixes = []string{ // will hand a minted token to. const pushRemoteHost = "github.com" -// The broker runs git INSIDE a workspace the sandboxed agent wrote. The -// sandbox exists so that the agent never holds a credential; the broker is the -// one process that does, and it runs every git command below as the hive's -// own UID, outside the sandbox. Git, however, reads the repository's own -// configuration — .git/config, .git/hooks/, .gitattributes — before it reads -// anything the broker says on the command line, and several of those keys make -// git EXECUTE a program or REDIRECT the push. An agent that writes any of them -// into the workspace turns the broker into (a) arbitrary code execution as the -// hive's UID on the host and (b) exfiltration of the push token to a host of -// the agent's choosing. Two layers close that: -// -// 1. workspaceConfigOverrides are applied to EVERY broker git invocation at -// command scope (GIT_CONFIG_*), which outranks the repository's own -// files: hooks are routed to /dev/null, the fsmonitor and credential -// helper lists are reset, commit signing is off, and the remote the -// broker pushes to is pinned to the repository it was told to push to, -// not to whatever .git/config says "origin" is now. -// 2. rejectHostileWorkspaceConfig refuses the push outright when the -// repository-scoped configuration contains any key outside the small set -// a fresh clone and an ordinary agent session produce. Keys like -// url.*.insteadOf, http.proxy / http.sslVerify, filter.*.clean and -// core.askPass have no override that is safe against every spelling, so -// the only sound answer to seeing them is to not run git at all. -func workspaceConfigOverrides(remote, repo string) [][2]string { - pairs := [][2]string{ - {"core.hooksPath", os.DevNull}, - {"core.fsmonitor", "false"}, - {"credential.helper", ""}, - {"commit.gpgsign", "false"}, - } - if url := expectedRemoteURL(repo); url != "" { - pairs = append(pairs, - [2]string{"remote." + remote + ".url", url}, - [2]string{"remote." + remote + ".pushurl", url}, - ) - } - return pairs -} - -// expectedRemoteURL is the URL SandboxExecutor cloned repo from, and the only -// URL the broker will push to. Empty when repo is not an "owner/name" pair. -func expectedRemoteURL(repo string) string { - owner, name, ok := strings.Cut(strings.TrimSpace(repo), "/") - if !ok || owner == "" || name == "" || strings.Contains(name, "/") { - return "" - } - return "https://" + pushRemoteHost + "/" + owner + "/" + name + ".git" -} - -// configEnv renders pairs as the GIT_CONFIG_COUNT/KEY/VALUE triplets git reads -// as command-scope configuration. Only the variable names and the values -// above reach the environment; no secret does. -func configEnv(pairs [][2]string) []string { - env := make([]string, 0, 1+2*len(pairs)) - env = append(env, "GIT_CONFIG_COUNT="+strconv.Itoa(len(pairs))) - for i, p := range pairs { - env = append(env, - fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", i, p[0]), - fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", i, p[1]), - ) - } - return env -} - -// allowedWorkspaceConfigKeys are the exact repository-scoped keys a fresh -// `git clone` writes plus the identity keys an agent CLI commonly sets. Keys -// are compared lowercased, as `git config --list` prints them. -var allowedWorkspaceConfigKeys = map[string]struct{}{ - "core.repositoryformatversion": {}, - "core.filemode": {}, - "core.bare": {}, - "core.logallrefupdates": {}, - "core.ignorecase": {}, - "core.precomposeunicode": {}, - "core.symlinks": {}, - "core.autocrlf": {}, - "core.eol": {}, - "core.safecrlf": {}, - "core.hidedotfiles": {}, - "core.protectntfs": {}, - "extensions.worktreeconfig": {}, - "extensions.objectformat": {}, - "user.name": {}, - "user.email": {}, - "safe.directory": {}, - "pull.rebase": {}, - "push.default": {}, - "push.autosetupremote": {}, - "init.defaultbranch": {}, - "fetch.prune": {}, -} - -// allowedWorkspaceConfigPrefixes cover the per-branch tracking keys and the -// purely cosmetic sections, none of which can make git execute or redirect. -var allowedWorkspaceConfigPrefixes = []string{"color.", "advice."} - -// rejectHostileWorkspaceConfig reads the repository-scoped (local and -// worktree) configuration of the workspace and refuses the push when it holds -// any key outside the allowlist, or a remote URL for the push remote that is -// not the repository the broker was told to push to. -func (b *Broker) rejectHostileWorkspaceConfig(ctx context.Context) error { - out, err := b.git(ctx, "config", "--list", "-z", "--show-scope") - if err != nil { - return fmt.Errorf("reading workspace git config: %w", err) - } - remote := strings.ToLower(b.remote()) - want := expectedRemoteURL(b.Repo) - offenders := workspaceConfigOffenders(out, remote, &want) - if len(offenders) == 0 { - return nil - } - return fmt.Errorf("pushbroker: refusing to run git in a workspace whose repository config sets %s; a sandbox workspace may only carry the keys a fresh clone writes", strings.Join(offenders, ", ")) -} - -func workspaceConfigOffenders(out []byte, remote string, expectedURL *string) []string { - var offenders []string - // The record layout is "\x00\n\x00", so splitting on - // NUL yields alternating scope and key/value elements. - fields := strings.Split(string(out), "\x00") - for i := 0; i+1 < len(fields); i += 2 { - scope := fields[i] - key, value, _ := strings.Cut(fields[i+1], "\n") - if scope != "local" && scope != "worktree" { - continue - } - key = strings.ToLower(key) - if workspaceConfigKeyAllowed(key, remote) { - if expectedURL != nil && (key == "remote."+remote+".url" || key == "remote."+remote+".pushurl") && !sameRemoteURL(value, *expectedURL) { - offenders = append(offenders, key+"="+value) - } - continue - } - offenders = append(offenders, key) - } - return offenders -} - -func workspaceConfigKeyAllowed(key, remote string) bool { - if _, ok := allowedWorkspaceConfigKeys[key]; ok { - return true - } - for _, prefix := range allowedWorkspaceConfigPrefixes { - if strings.HasPrefix(key, prefix) { - return true - } - } - switch key { - case "remote." + remote + ".url", "remote." + remote + ".pushurl", "remote." + remote + ".fetch": - return true - } - if rest, ok := strings.CutPrefix(key, "branch."); ok { - switch { - case strings.HasSuffix(rest, ".remote"), strings.HasSuffix(rest, ".merge"), strings.HasSuffix(rest, ".rebase"): - return true - } - } - return false -} - -// sameRemoteURL compares two https remote URLs ignoring case, a trailing -// slash and the optional ".git" suffix. -func sameRemoteURL(got, want string) bool { - norm := func(u string) string { - u = strings.ToLower(strings.TrimSpace(u)) - u = strings.TrimSuffix(u, "/") - u = strings.TrimSuffix(u, ".git") - return u - } - return want != "" && norm(got) == norm(want) -} - type TokenMinter interface { MintPushToken(ctx context.Context, repo string) (string, error) } diff --git a/src/crux/pushbroker/workspace_config.go b/src/crux/pushbroker/workspace_config.go new file mode 100644 index 0000000000..7ff76ea808 --- /dev/null +++ b/src/crux/pushbroker/workspace_config.go @@ -0,0 +1,180 @@ +package pushbroker + +import ( + "context" + "fmt" + "os" + "strconv" + "strings" +) + +// The broker runs git INSIDE a workspace the sandboxed agent wrote. The +// sandbox exists so that the agent never holds a credential; the broker is the +// one process that does, and it runs every git command below as the hive's +// own UID, outside the sandbox. Git, however, reads the repository's own +// configuration — .git/config, .git/hooks/, .gitattributes — before it reads +// anything the broker says on the command line, and several of those keys make +// git EXECUTE a program or REDIRECT the push. An agent that writes any of them +// into the workspace turns the broker into (a) arbitrary code execution as the +// hive's UID on the host and (b) exfiltration of the push token to a host of +// the agent's choosing. Two layers close that: +// +// 1. workspaceConfigOverrides are applied to EVERY broker git invocation at +// command scope (GIT_CONFIG_*), which outranks the repository's own +// files: hooks are routed to /dev/null, the fsmonitor and credential +// helper lists are reset, commit signing is off, and the remote the +// broker pushes to is pinned to the repository it was told to push to, +// not to whatever .git/config says "origin" is now. +// 2. rejectHostileWorkspaceConfig refuses the push outright when the +// repository-scoped configuration contains any key outside the small set +// a fresh clone and an ordinary agent session produce. Keys like +// url.*.insteadOf, http.proxy / http.sslVerify, filter.*.clean and +// core.askPass have no override that is safe against every spelling, so +// the only sound answer to seeing them is to not run git at all. +func workspaceConfigOverrides(remote, repo string) [][2]string { + pairs := [][2]string{ + {"core.hooksPath", os.DevNull}, + {"core.fsmonitor", "false"}, + {"credential.helper", ""}, + {"commit.gpgsign", "false"}, + } + if url := expectedRemoteURL(repo); url != "" { + pairs = append(pairs, + [2]string{"remote." + remote + ".url", url}, + [2]string{"remote." + remote + ".pushurl", url}, + ) + } + return pairs +} + +// expectedRemoteURL is the URL SandboxExecutor cloned repo from, and the only +// URL the broker will push to. Empty when repo is not an "owner/name" pair. +func expectedRemoteURL(repo string) string { + owner, name, ok := strings.Cut(strings.TrimSpace(repo), "/") + if !ok || owner == "" || name == "" || strings.Contains(name, "/") { + return "" + } + return "https://" + pushRemoteHost + "/" + owner + "/" + name + ".git" +} + +// configEnv renders pairs as the GIT_CONFIG_COUNT/KEY/VALUE triplets git reads +// as command-scope configuration. Only the variable names and the values +// above reach the environment; no secret does. +func configEnv(pairs [][2]string) []string { + env := make([]string, 0, 1+2*len(pairs)) + env = append(env, "GIT_CONFIG_COUNT="+strconv.Itoa(len(pairs))) + for i, p := range pairs { + env = append(env, + fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", i, p[0]), + fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", i, p[1]), + ) + } + return env +} + +// allowedWorkspaceConfigKeys are the exact repository-scoped keys a fresh +// `git clone` writes plus the identity keys an agent CLI commonly sets. Keys +// are compared lowercased, as `git config --list` prints them. +var allowedWorkspaceConfigKeys = map[string]struct{}{ + "core.repositoryformatversion": {}, + "core.filemode": {}, + "core.bare": {}, + "core.logallrefupdates": {}, + "core.ignorecase": {}, + "core.precomposeunicode": {}, + "core.symlinks": {}, + "core.autocrlf": {}, + "core.eol": {}, + "core.safecrlf": {}, + "core.hidedotfiles": {}, + "core.protectntfs": {}, + "extensions.worktreeconfig": {}, + "extensions.objectformat": {}, + "user.name": {}, + "user.email": {}, + "safe.directory": {}, + "pull.rebase": {}, + "push.default": {}, + "push.autosetupremote": {}, + "init.defaultbranch": {}, + "fetch.prune": {}, +} + +// allowedWorkspaceConfigPrefixes cover the per-branch tracking keys and the +// purely cosmetic sections, none of which can make git execute or redirect. +var allowedWorkspaceConfigPrefixes = []string{"color.", "advice."} + +// rejectHostileWorkspaceConfig reads the repository-scoped (local and +// worktree) configuration of the workspace and refuses the push when it holds +// any key outside the allowlist, or a remote URL for the push remote that is +// not the repository the broker was told to push to. +func (b *Broker) rejectHostileWorkspaceConfig(ctx context.Context) error { + out, err := b.git(ctx, "config", "--list", "-z", "--show-scope") + if err != nil { + return fmt.Errorf("reading workspace git config: %w", err) + } + remote := strings.ToLower(b.remote()) + want := expectedRemoteURL(b.Repo) + offenders := workspaceConfigOffenders(out, remote, &want) + if len(offenders) == 0 { + return nil + } + return fmt.Errorf("pushbroker: refusing to run git in a workspace whose repository config sets %s; a sandbox workspace may only carry the keys a fresh clone writes", strings.Join(offenders, ", ")) +} + +func workspaceConfigOffenders(out []byte, remote string, expectedURL *string) []string { + var offenders []string + // The record layout is "\x00\n\x00", so splitting on + // NUL yields alternating scope and key/value elements. + fields := strings.Split(string(out), "\x00") + for i := 0; i+1 < len(fields); i += 2 { + scope := fields[i] + key, value, _ := strings.Cut(fields[i+1], "\n") + if scope != "local" && scope != "worktree" { + continue + } + key = strings.ToLower(key) + if workspaceConfigKeyAllowed(key, remote) { + if expectedURL != nil && (key == "remote."+remote+".url" || key == "remote."+remote+".pushurl") && !sameRemoteURL(value, *expectedURL) { + offenders = append(offenders, key+"="+value) + } + continue + } + offenders = append(offenders, key) + } + return offenders +} + +func workspaceConfigKeyAllowed(key, remote string) bool { + if _, ok := allowedWorkspaceConfigKeys[key]; ok { + return true + } + for _, prefix := range allowedWorkspaceConfigPrefixes { + if strings.HasPrefix(key, prefix) { + return true + } + } + switch key { + case "remote." + remote + ".url", "remote." + remote + ".pushurl", "remote." + remote + ".fetch": + return true + } + if rest, ok := strings.CutPrefix(key, "branch."); ok { + switch { + case strings.HasSuffix(rest, ".remote"), strings.HasSuffix(rest, ".merge"), strings.HasSuffix(rest, ".rebase"): + return true + } + } + return false +} + +// sameRemoteURL compares two https remote URLs ignoring case, a trailing +// slash and the optional ".git" suffix. +func sameRemoteURL(got, want string) bool { + norm := func(u string) string { + u = strings.ToLower(strings.TrimSpace(u)) + u = strings.TrimSuffix(u, "/") + u = strings.TrimSuffix(u, ".git") + return u + } + return want != "" && norm(got) == norm(want) +}