Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-18574-sandbox-existing-pr.md
Original file line number Diff line number Diff line change
@@ -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 <repo>#<n>]` 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))
6 changes: 6 additions & 0 deletions src/cmd/hive/merge_eligibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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/<agent>/<stamp>: 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
}
Expand Down
47 changes: 47 additions & 0 deletions src/cmd/hive/sandbox_pr_credit_test.go
Original file line number Diff line number Diff line change
@@ -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/<agent>/<stamp>. 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)
}
}
76 changes: 65 additions & 11 deletions src/crux/agent/manager_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -253,7 +254,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
}

Expand All @@ -278,18 +295,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()
Expand Down Expand Up @@ -369,6 +387,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)
Expand All @@ -393,7 +414,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) {
Expand Down
64 changes: 57 additions & 7 deletions src/crux/agent/sandbox_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ type SandboxKickSpec struct {
// Non-empty makes the kick review-only: workspace prep checks each PR
// head out on pr-<n> and writes .hive/pr/<n>.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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -342,16 +377,31 @@ 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
if brokerErr != nil {
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()
Expand Down Expand Up @@ -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) }
Expand Down
Loading
Loading