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-18513-sandbox-reviewer-pr-checkout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Sandboxed reviewers now get the PR they review. Hive fetches `refs/pull/N/head` (fork PRs included) and the PR's base with its clone token and checks the head out on `pr-N`. It writes `.hive/pr/N.json`, marked untrusted, with the PR's metadata, changed files, capped diff and review comments, and names both in the kick prompt. Review kicks never push. ([#18513](https://github.com/hivecommons/hive/issues/18513))
17 changes: 14 additions & 3 deletions src/crux/agent/manager_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func (m *Manager) startSandboxKickLocked(agent *AgentProcess, message string) er
// Every sandbox kick gets a review spool (hivecommons/hive#14986);
// whether the agent may post through it is the relay's decision.
ReviewSpoolDir: crux.SandboxReviewSpoolsDir(m.workDir),
// A review kick names its PR(s) in its hive-written header; the
// sandbox gets them checked out (hivecommons/hive#18513).
ReviewPRs: reviewTargetsFromKickMessage(message, m.project.Org+"/"+repo),
}
if spec.AgentConfig.Backend == "cline" {
spec.AgentConfig.Model = agent.effectiveModel()
Expand All @@ -226,12 +229,19 @@ func (m *Manager) startSandboxKickLocked(agent *AgentProcess, message string) er
var pushMinter pushbroker.TokenMinter
var prClient PRCreator
pushEnabled := false
if m.agentMode(agent).CanPush() && m.project.PRsAllowed {
// Review-only kicks never get the push broker or the PR client.
if m.agentMode(agent).CanPush() && m.project.PRsAllowed && !spec.reviewOnly() {
pushMinter = cloneMinter
prClient = m.sandboxPRClient
pushEnabled = true
}
go m.runSandboxKick(runCtx, agent.Name, runtime, spec, launcher, runner, cloneMinter, pushMinter, pushEnabled, prClient, mutationBoundary)
// The PR reader is the hive's GitHub client when it offers one; it is
// read-only and needed whether or not the kick may push.
var prReader crux.PRReviewContextReader
if spec.reviewOnly() {
prReader, _ = m.sandboxPRClient.(crux.PRReviewContextReader)
}
go m.runSandboxKick(runCtx, agent.Name, runtime, spec, launcher, runner, cloneMinter, pushMinter, pushEnabled, prClient, prReader, mutationBoundary)
return nil
}

Expand All @@ -256,7 +266,7 @@ 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, 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, mutationBoundary effects.Boundary) {
started := time.Now()
exec := &SandboxExecutor{
Launcher: launcher,
Expand All @@ -265,6 +275,7 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec
Minter: minter,
PushEnabled: pushEnabled,
PRClient: prClient,
PRReader: prReader,
Mutation: mutationBoundary,
Logger: m.logger,
}
Expand Down
34 changes: 26 additions & 8 deletions src/crux/agent/sandbox_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ type SandboxKickSpec struct {
// relay (crux.SandboxReviewSpoolsDir). Empty disables spooling: the
// sandbox then has no way to post a review.
ReviewSpoolDir string
// ReviewPRs are the PR numbers (in Org/Repo) this kick reviews, taken
// from the review kick's hive-written header (hivecommons/hive#18513).
// 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
}

// reviewOnly reports whether the kick reviews PRs and so must never push.
func (s SandboxKickSpec) reviewOnly() bool { return len(s.ReviewPRs) > 0 }

type configSnapshot struct {
Backend string
Model string
Expand Down Expand Up @@ -148,9 +156,12 @@ type SandboxExecutor struct {
Minter pushbroker.TokenMinter
PushEnabled bool
PRClient PRCreator
Logger *slog.Logger
Now func() time.Time
Mutation effects.Boundary
// 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
}

func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (SandboxKickResult, error) {
Expand Down Expand Up @@ -291,6 +302,14 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo
res.Error = err.Error()
return res, err
}
// A review kick never pushes (hivecommons/hive#18513), whatever the
// caller wired: commits a reviewer leaves on pr-<n> are discarded.
if spec.reviewOnly() {
if count > 0 {
res.PRWithheld = fmt.Sprintf("review-only kick: %d commit(s) left on %s were not pushed", count, spec.Branch)
}
return res, nil
}
if count == 0 {
return res, nil
}
Expand Down Expand Up @@ -442,12 +461,11 @@ func sandboxBackendBinary(backend string) string {
}
}

// writeSandboxPrompt writes the kick prompt confined to the workspace: on a
// review kick the tree is the PR author's, and a .hive symlink in it must not
// redirect the host's write (hivecommons/hive#18513).
func writeSandboxPrompt(workspace, message string) error {
path := filepath.Join(workspace, sandboxPromptRelPath)
if err := os.MkdirAll(filepath.Dir(path), 0o770); err != nil {
return err
}
return os.WriteFile(path, []byte(message), 0o660)
return writeWorkspaceFileConfined(workspace, sandboxPromptRelPath, []byte(message))
}

func writeTranscript(path string, res sandbox.Result) error {
Expand Down
Loading
Loading