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/fixed-sandbox-pr-title-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Sandbox PRs now take their title/body from `.hive/pr-title.txt` / `.hive/pr-body.md` in the workspace (or the HEAD commit) instead of a fixed placeholder that referenced hive's own tracker
65 changes: 63 additions & 2 deletions src/crux/agent/sandbox_executor.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package agent

import (
Expand Down Expand Up @@ -25,6 +25,12 @@
defaultSandboxNetworkMode = sandbox.NetworkModeRestricted
sandboxPromptRelPath = ".hive/kick-prompt.txt"
sandboxTranscriptRelPath = ".hive/sandbox-transcript.log"
// The sandboxed agent may leave the PR title and body it wants in the
// workspace; the executor (which alone can open the PR) picks them up.
sandboxPRTitleRelPath = ".hive/pr-title.txt"
sandboxPRBodyRelPath = ".hive/pr-body.md"
sandboxPRTitleMaxLen = 200
sandboxPRBodyMaxLen = 60000
)

type sandboxCommandRunner interface {
Expand Down Expand Up @@ -210,8 +216,7 @@
return res, brokerErr
}
if e.PRClient != nil && bres.Pushed {
title := fmt.Sprintf("[%s] sandbox changes", spec.Agent)
body := fmt.Sprintf("Sandboxed credential-free execution result for agent `%s`.\n\nPart of #2804.", spec.Agent)
title, body := e.sandboxPRText(ctx, workspace, spec.Agent)
pr, prErr := e.PRClient.CreatePR(ctx, spec.Org+"/"+spec.Repo, spec.Branch, spec.BaseRef, title, body)
res.PR = &pr
if prErr != nil {
Expand All @@ -222,6 +227,62 @@
return res, nil
}

// sandboxPRText resolves the title and body for the PR the executor opens on
// the agent's behalf. Precedence: files the agent wrote into .hive/ in the
// workspace, then the HEAD commit's subject and body, then a generic
// placeholder naming the agent. The Job never holds a GitHub credential, so
// this is the only channel through which the agent's own PR description can
// reach the PR (hivecommons/hive#14691 follow-up).
func (e *SandboxExecutor) sandboxPRText(ctx context.Context, workspace, agent string) (string, string) {
title := readWorkspaceText(filepath.Join(workspace, sandboxPRTitleRelPath), sandboxPRTitleMaxLen)
body := readWorkspaceText(filepath.Join(workspace, sandboxPRBodyRelPath), sandboxPRBodyMaxLen)
if title == "" || body == "" {
if out, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", "log", "-1", "--format=%s%n%n%b", "HEAD"); err == nil {
subject, rest, _ := strings.Cut(strings.TrimSpace(string(out)), "\n")
if title == "" {
title = truncateRunes(strings.TrimSpace(subject), sandboxPRTitleMaxLen)
}
if body == "" {
body = truncateRunes(strings.TrimSpace(rest), sandboxPRBodyMaxLen)
}
}
}
if title == "" {
title = fmt.Sprintf("[%s] sandbox changes", agent)
}
if body == "" {
body = fmt.Sprintf("Changes produced by hive agent `%s` in a credential-free sandbox run.", agent)
}
return title, body
}

// readWorkspaceText returns the trimmed contents of path, bounded to max
// runes, or "" when the file is absent or unreadable. The title file is
// reduced to its first line so a stray paragraph cannot become a PR title.
func readWorkspaceText(path string, max int) string {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
text := strings.TrimSpace(string(data))
if filepath.Base(path) == filepath.Base(sandboxPRTitleRelPath) {
text, _, _ = strings.Cut(text, "\n")
text = strings.TrimSpace(text)
}
return truncateRunes(text, max)
}

func truncateRunes(s string, max int) string {
if max <= 0 {
return s
}
r := []rune(s)
if len(r) <= max {
return s
}
return string(r[:max])
}

// prepareWorkspace clones the target repo and checks out the work branch off
// the base ref. When spec.BaseRef is empty it is RESOLVED from the clone's own
// refs/remotes/origin/HEAD and written back into spec, so the caller opens its
Expand Down
68 changes: 68 additions & 0 deletions src/crux/agent/sandbox_pr_text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package agent

import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)

type prTextRunner struct{ gitLog string }

func (r prTextRunner) Run(_ context.Context, _ string, _ []string, name string, args ...string) ([]byte, error) {
if name == "git" && len(args) > 0 && args[0] == "log" {
return []byte(r.gitLog), nil
}
return nil, nil
}

func TestSandboxPRText_PrefersWorkspaceFiles(t *testing.T) {
ws := t.TempDir()
if err := os.MkdirAll(filepath.Join(ws, ".hive"), 0o755); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(ws, ".hive", "pr-title.txt"), []byte("deps: bump torch-spyre to b49e05c\nsecond line ignored\n"), 0o644)
os.WriteFile(filepath.Join(ws, ".hive", "pr-body.md"), []byte("## Summary\n\nbuilt + smoke test green\n"), 0o644)
e := &SandboxExecutor{Runner: prTextRunner{gitLog: "commit subject\n\ncommit body"}}
title, body := e.sandboxPRText(context.Background(), ws, "deps")
if title != "deps: bump torch-spyre to b49e05c" {
t.Fatalf("title = %q", title)
}
if !strings.HasPrefix(body, "## Summary") {
t.Fatalf("body = %q", body)
}
}

func TestSandboxPRText_FallsBackToHeadCommit(t *testing.T) {
e := &SandboxExecutor{Runner: prTextRunner{gitLog: "fix: tighten retry\n\nwhy it matters\n\nSigned-off-by: x\n"}}
title, body := e.sandboxPRText(context.Background(), t.TempDir(), "deps")
if title != "fix: tighten retry" {
t.Fatalf("title = %q", title)
}
if !strings.HasPrefix(body, "why it matters") {
t.Fatalf("body = %q", body)
}
}

func TestSandboxPRText_PlaceholderLast(t *testing.T) {
e := &SandboxExecutor{Runner: prTextRunner{}}
title, body := e.sandboxPRText(context.Background(), t.TempDir(), "deps")
if title != "[deps] sandbox changes" || !strings.Contains(body, "`deps`") {
t.Fatalf("title=%q body=%q", title, body)
}
if strings.Contains(body, "#2804") {
t.Fatalf("placeholder must not reference hive's own tracker: %q", body)
}
}

func TestSandboxPRText_TitleIsBounded(t *testing.T) {
ws := t.TempDir()
os.MkdirAll(filepath.Join(ws, ".hive"), 0o755)
os.WriteFile(filepath.Join(ws, ".hive", "pr-title.txt"), []byte(strings.Repeat("x", 500)), 0o644)
e := &SandboxExecutor{Runner: prTextRunner{}}
title, _ := e.sandboxPRText(context.Background(), ws, "deps")
if len([]rune(title)) != sandboxPRTitleMaxLen {
t.Fatalf("title len = %d", len([]rune(title)))
}
}
1 change: 1 addition & 0 deletions src/docs/sandbox-isolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ For sandboxed agents, a kick now follows this path:
3. The manager marks the agent busy while the sandbox runs and returns it to idle or failed when the timeout/completion path finishes. Dashboard status uses the existing agent status structures.
4. Hive collects the transcript at `.hive/sandbox-transcript.log` and any `agent-report*.json` artifact following `pkg/outputschema` conventions.
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 `[<agent>] sandbox changes` placeholder is used only when neither exists. Kick templates for sandboxed agents should ask the agent to write those two files.

## Running sandboxed kicks as Kubernetes Jobs

Expand Down
Loading